include/elf/
[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 HOWTO (R_PPC64_TOCSAVE,
1286 0, /* rightshift */
1287 2, /* size (0 = byte, 1 = short, 2 = long) */
1288 32, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 bfd_elf_generic_reloc, /* special_function */
1293 "R_PPC64_TOCSAVE", /* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 0, /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
1299 /* Computes the load module index of the load module that contains the
1300 definition of its TLS sym. */
1301 HOWTO (R_PPC64_DTPMOD64,
1302 0, /* rightshift */
1303 4, /* size (0 = byte, 1 = short, 2 = long) */
1304 64, /* bitsize */
1305 FALSE, /* pc_relative */
1306 0, /* bitpos */
1307 complain_overflow_dont, /* complain_on_overflow */
1308 ppc64_elf_unhandled_reloc, /* special_function */
1309 "R_PPC64_DTPMOD64", /* name */
1310 FALSE, /* partial_inplace */
1311 0, /* src_mask */
1312 ONES (64), /* dst_mask */
1313 FALSE), /* pcrel_offset */
1314
1315 /* Computes a dtv-relative displacement, the difference between the value
1316 of sym+add and the base address of the thread-local storage block that
1317 contains the definition of sym, minus 0x8000. */
1318 HOWTO (R_PPC64_DTPREL64,
1319 0, /* rightshift */
1320 4, /* size (0 = byte, 1 = short, 2 = long) */
1321 64, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_dont, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL64", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 ONES (64), /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* A 16 bit dtprel reloc. */
1333 HOWTO (R_PPC64_DTPREL16,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_signed, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16, but no overflow. */
1348 HOWTO (R_PPC64_DTPREL16_LO,
1349 0, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_LO", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HI,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HI", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HA,
1379 16, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HA", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHER,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHER", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1409 32, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHERA", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHEST", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1438 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1439 48, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xffff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16, but for insns with a DS field. */
1453 HOWTO (R_PPC64_DTPREL16_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_signed, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Like DTPREL16_DS, but no overflow. */
1468 HOWTO (R_PPC64_DTPREL16_LO_DS,
1469 0, /* rightshift */
1470 1, /* size (0 = byte, 1 = short, 2 = long) */
1471 16, /* bitsize */
1472 FALSE, /* pc_relative */
1473 0, /* bitpos */
1474 complain_overflow_dont, /* complain_on_overflow */
1475 ppc64_elf_unhandled_reloc, /* special_function */
1476 "R_PPC64_DTPREL16_LO_DS", /* name */
1477 FALSE, /* partial_inplace */
1478 0, /* src_mask */
1479 0xfffc, /* dst_mask */
1480 FALSE), /* pcrel_offset */
1481
1482 /* Computes a tp-relative displacement, the difference between the value of
1483 sym+add and the value of the thread pointer (r13). */
1484 HOWTO (R_PPC64_TPREL64,
1485 0, /* rightshift */
1486 4, /* size (0 = byte, 1 = short, 2 = long) */
1487 64, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_dont, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL64", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 ONES (64), /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* A 16 bit tprel reloc. */
1499 HOWTO (R_PPC64_TPREL16,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_signed, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16, but no overflow. */
1514 HOWTO (R_PPC64_TPREL16_LO,
1515 0, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_LO", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_LO, but next higher group of 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HI,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HI", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but adjust for low 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HA,
1545 16, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HA", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HI, but next higher group of 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHER,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHER", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHERA,
1575 32, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHERA", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHEST,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHEST", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1604 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1605 48, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_dont, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_HIGHESTA", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xffff, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16, but for insns with a DS field. */
1619 HOWTO (R_PPC64_TPREL16_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_signed, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Like TPREL16_DS, but no overflow. */
1634 HOWTO (R_PPC64_TPREL16_LO_DS,
1635 0, /* rightshift */
1636 1, /* size (0 = byte, 1 = short, 2 = long) */
1637 16, /* bitsize */
1638 FALSE, /* pc_relative */
1639 0, /* bitpos */
1640 complain_overflow_dont, /* complain_on_overflow */
1641 ppc64_elf_unhandled_reloc, /* special_function */
1642 "R_PPC64_TPREL16_LO_DS", /* name */
1643 FALSE, /* partial_inplace */
1644 0, /* src_mask */
1645 0xfffc, /* dst_mask */
1646 FALSE), /* pcrel_offset */
1647
1648 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1649 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1650 to the first entry relative to the TOC base (r2). */
1651 HOWTO (R_PPC64_GOT_TLSGD16,
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_signed, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_GOT_TLSGD16", /* name */
1660 FALSE, /* partial_inplace */
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
1663 FALSE), /* pcrel_offset */
1664
1665 /* Like GOT_TLSGD16, but no overflow. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1667 0, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
1670 FALSE, /* pc_relative */
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
1673 ppc64_elf_unhandled_reloc, /* special_function */
1674 "R_PPC64_GOT_TLSGD16_LO", /* name */
1675 FALSE, /* partial_inplace */
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
1678 FALSE), /* pcrel_offset */
1679
1680 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
1685 FALSE, /* pc_relative */
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
1688 ppc64_elf_unhandled_reloc, /* special_function */
1689 "R_PPC64_GOT_TLSGD16_HI", /* name */
1690 FALSE, /* partial_inplace */
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
1693 FALSE), /* pcrel_offset */
1694
1695 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1696 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1697 16, /* rightshift */
1698 1, /* size (0 = byte, 1 = short, 2 = long) */
1699 16, /* bitsize */
1700 FALSE, /* pc_relative */
1701 0, /* bitpos */
1702 complain_overflow_dont, /* complain_on_overflow */
1703 ppc64_elf_unhandled_reloc, /* special_function */
1704 "R_PPC64_GOT_TLSGD16_HA", /* name */
1705 FALSE, /* partial_inplace */
1706 0, /* src_mask */
1707 0xffff, /* dst_mask */
1708 FALSE), /* pcrel_offset */
1709
1710 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1711 with values (sym+add)@dtpmod and zero, and computes the offset to the
1712 first entry relative to the TOC base (r2). */
1713 HOWTO (R_PPC64_GOT_TLSLD16,
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
1717 FALSE, /* pc_relative */
1718 0, /* bitpos */
1719 complain_overflow_signed, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16", /* name */
1722 FALSE, /* partial_inplace */
1723 0, /* src_mask */
1724 0xffff, /* dst_mask */
1725 FALSE), /* pcrel_offset */
1726
1727 /* Like GOT_TLSLD16, but no overflow. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1729 0, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
1732 FALSE, /* pc_relative */
1733 0, /* bitpos */
1734 complain_overflow_dont, /* complain_on_overflow */
1735 ppc64_elf_unhandled_reloc, /* special_function */
1736 "R_PPC64_GOT_TLSLD16_LO", /* name */
1737 FALSE, /* partial_inplace */
1738 0, /* src_mask */
1739 0xffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1741
1742 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1744 16, /* rightshift */
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
1747 FALSE, /* pc_relative */
1748 0, /* bitpos */
1749 complain_overflow_dont, /* complain_on_overflow */
1750 ppc64_elf_unhandled_reloc, /* special_function */
1751 "R_PPC64_GOT_TLSLD16_HI", /* name */
1752 FALSE, /* partial_inplace */
1753 0, /* src_mask */
1754 0xffff, /* dst_mask */
1755 FALSE), /* pcrel_offset */
1756
1757 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1758 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1759 16, /* rightshift */
1760 1, /* size (0 = byte, 1 = short, 2 = long) */
1761 16, /* bitsize */
1762 FALSE, /* pc_relative */
1763 0, /* bitpos */
1764 complain_overflow_dont, /* complain_on_overflow */
1765 ppc64_elf_unhandled_reloc, /* special_function */
1766 "R_PPC64_GOT_TLSLD16_HA", /* name */
1767 FALSE, /* partial_inplace */
1768 0, /* src_mask */
1769 0xffff, /* dst_mask */
1770 FALSE), /* pcrel_offset */
1771
1772 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1773 the offset to the entry relative to the TOC base (r2). */
1774 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1775 0, /* rightshift */
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
1778 FALSE, /* pc_relative */
1779 0, /* bitpos */
1780 complain_overflow_signed, /* complain_on_overflow */
1781 ppc64_elf_unhandled_reloc, /* special_function */
1782 "R_PPC64_GOT_DTPREL16_DS", /* name */
1783 FALSE, /* partial_inplace */
1784 0, /* src_mask */
1785 0xfffc, /* dst_mask */
1786 FALSE), /* pcrel_offset */
1787
1788 /* Like GOT_DTPREL16_DS, but no overflow. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1790 0, /* rightshift */
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
1793 FALSE, /* pc_relative */
1794 0, /* bitpos */
1795 complain_overflow_dont, /* complain_on_overflow */
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1798 FALSE, /* partial_inplace */
1799 0, /* src_mask */
1800 0xfffc, /* dst_mask */
1801 FALSE), /* pcrel_offset */
1802
1803 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1805 16, /* rightshift */
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
1808 FALSE, /* pc_relative */
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HI", /* name */
1813 FALSE, /* partial_inplace */
1814 0, /* src_mask */
1815 0xffff, /* dst_mask */
1816 FALSE), /* pcrel_offset */
1817
1818 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1819 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1820 16, /* rightshift */
1821 1, /* size (0 = byte, 1 = short, 2 = long) */
1822 16, /* bitsize */
1823 FALSE, /* pc_relative */
1824 0, /* bitpos */
1825 complain_overflow_dont, /* complain_on_overflow */
1826 ppc64_elf_unhandled_reloc, /* special_function */
1827 "R_PPC64_GOT_DTPREL16_HA", /* name */
1828 FALSE, /* partial_inplace */
1829 0, /* src_mask */
1830 0xffff, /* dst_mask */
1831 FALSE), /* pcrel_offset */
1832
1833 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1834 offset to the entry relative to the TOC base (r2). */
1835 HOWTO (R_PPC64_GOT_TPREL16_DS,
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
1839 FALSE, /* pc_relative */
1840 0, /* bitpos */
1841 complain_overflow_signed, /* complain_on_overflow */
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_DS", /* name */
1844 FALSE, /* partial_inplace */
1845 0, /* src_mask */
1846 0xfffc, /* dst_mask */
1847 FALSE), /* pcrel_offset */
1848
1849 /* Like GOT_TPREL16_DS, but no overflow. */
1850 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1851 0, /* rightshift */
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
1854 FALSE, /* pc_relative */
1855 0, /* bitpos */
1856 complain_overflow_dont, /* complain_on_overflow */
1857 ppc64_elf_unhandled_reloc, /* special_function */
1858 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1859 FALSE, /* partial_inplace */
1860 0, /* src_mask */
1861 0xfffc, /* dst_mask */
1862 FALSE), /* pcrel_offset */
1863
1864 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HI,
1866 16, /* rightshift */
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
1869 FALSE, /* pc_relative */
1870 0, /* bitpos */
1871 complain_overflow_dont, /* complain_on_overflow */
1872 ppc64_elf_unhandled_reloc, /* special_function */
1873 "R_PPC64_GOT_TPREL16_HI", /* name */
1874 FALSE, /* partial_inplace */
1875 0, /* src_mask */
1876 0xffff, /* dst_mask */
1877 FALSE), /* pcrel_offset */
1878
1879 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1880 HOWTO (R_PPC64_GOT_TPREL16_HA,
1881 16, /* rightshift */
1882 1, /* size (0 = byte, 1 = short, 2 = long) */
1883 16, /* 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_GOT_TPREL16_HA", /* name */
1889 FALSE, /* partial_inplace */
1890 0, /* src_mask */
1891 0xffff, /* dst_mask */
1892 FALSE), /* pcrel_offset */
1893
1894 HOWTO (R_PPC64_JMP_IREL, /* type */
1895 0, /* rightshift */
1896 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1897 0, /* bitsize */
1898 FALSE, /* pc_relative */
1899 0, /* bitpos */
1900 complain_overflow_dont, /* complain_on_overflow */
1901 ppc64_elf_unhandled_reloc, /* special_function */
1902 "R_PPC64_JMP_IREL", /* name */
1903 FALSE, /* partial_inplace */
1904 0, /* src_mask */
1905 0, /* dst_mask */
1906 FALSE), /* pcrel_offset */
1907
1908 HOWTO (R_PPC64_IRELATIVE, /* type */
1909 0, /* rightshift */
1910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1911 64, /* bitsize */
1912 FALSE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_dont, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_IRELATIVE", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 ONES (64), /* dst_mask */
1920 FALSE), /* pcrel_offset */
1921
1922 /* A 16 bit relative relocation. */
1923 HOWTO (R_PPC64_REL16, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_bitfield, /* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* A 16 bit relative relocation without overflow. */
1938 HOWTO (R_PPC64_REL16_LO, /* type */
1939 0, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont,/* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_LO", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1951
1952 /* The high order 16 bits of a relative address. */
1953 HOWTO (R_PPC64_REL16_HI, /* type */
1954 16, /* rightshift */
1955 1, /* size (0 = byte, 1 = short, 2 = long) */
1956 16, /* bitsize */
1957 TRUE, /* pc_relative */
1958 0, /* bitpos */
1959 complain_overflow_dont, /* complain_on_overflow */
1960 bfd_elf_generic_reloc, /* special_function */
1961 "R_PPC64_REL16_HI", /* name */
1962 FALSE, /* partial_inplace */
1963 0, /* src_mask */
1964 0xffff, /* dst_mask */
1965 TRUE), /* pcrel_offset */
1966
1967 /* The high order 16 bits of a relative address, plus 1 if the contents of
1968 the low 16 bits, treated as a signed number, is negative. */
1969 HOWTO (R_PPC64_REL16_HA, /* type */
1970 16, /* rightshift */
1971 1, /* size (0 = byte, 1 = short, 2 = long) */
1972 16, /* bitsize */
1973 TRUE, /* pc_relative */
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 ppc64_elf_ha_reloc, /* special_function */
1977 "R_PPC64_REL16_HA", /* name */
1978 FALSE, /* partial_inplace */
1979 0, /* src_mask */
1980 0xffff, /* dst_mask */
1981 TRUE), /* pcrel_offset */
1982
1983 /* GNU extension to record C++ vtable hierarchy. */
1984 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
1988 FALSE, /* pc_relative */
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTINHERIT", /* name */
1993 FALSE, /* partial_inplace */
1994 0, /* src_mask */
1995 0, /* dst_mask */
1996 FALSE), /* pcrel_offset */
1997
1998 /* GNU extension to record C++ vtable member usage. */
1999 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2000 0, /* rightshift */
2001 0, /* size (0 = byte, 1 = short, 2 = long) */
2002 0, /* bitsize */
2003 FALSE, /* pc_relative */
2004 0, /* bitpos */
2005 complain_overflow_dont, /* complain_on_overflow */
2006 NULL, /* special_function */
2007 "R_PPC64_GNU_VTENTRY", /* name */
2008 FALSE, /* partial_inplace */
2009 0, /* src_mask */
2010 0, /* dst_mask */
2011 FALSE), /* pcrel_offset */
2012 };
2013
2014 \f
2015 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2016 be done. */
2017
2018 static void
2019 ppc_howto_init (void)
2020 {
2021 unsigned int i, type;
2022
2023 for (i = 0;
2024 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2025 i++)
2026 {
2027 type = ppc64_elf_howto_raw[i].type;
2028 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2029 / sizeof (ppc64_elf_howto_table[0])));
2030 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2031 }
2032 }
2033
2034 static reloc_howto_type *
2035 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2036 bfd_reloc_code_real_type code)
2037 {
2038 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2039
2040 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2041 /* Initialize howto table if needed. */
2042 ppc_howto_init ();
2043
2044 switch (code)
2045 {
2046 default:
2047 return NULL;
2048
2049 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2050 break;
2051 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2052 break;
2053 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2054 break;
2055 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2056 break;
2057 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2058 break;
2059 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2060 break;
2061 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2062 break;
2063 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2064 break;
2065 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2066 break;
2067 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2068 break;
2069 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2070 break;
2071 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2072 break;
2073 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2074 break;
2075 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2076 break;
2077 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2078 break;
2079 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2080 break;
2081 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2082 break;
2083 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2084 break;
2085 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2086 break;
2087 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2088 break;
2089 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2090 break;
2091 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2092 break;
2093 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2094 break;
2095 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2096 break;
2097 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2098 break;
2099 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2100 break;
2101 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2102 break;
2103 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2104 break;
2105 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2106 break;
2107 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2108 break;
2109 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2110 break;
2111 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2112 break;
2113 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2114 break;
2115 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2116 break;
2117 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2118 break;
2119 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2120 break;
2121 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2122 break;
2123 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2124 break;
2125 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2126 break;
2127 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2128 break;
2129 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2130 break;
2131 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2132 break;
2133 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2134 break;
2135 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2136 break;
2137 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2138 break;
2139 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2140 break;
2141 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2142 break;
2143 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2144 break;
2145 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2146 break;
2147 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2148 break;
2149 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2150 break;
2151 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2152 break;
2153 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2154 break;
2155 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2156 break;
2157 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2158 break;
2159 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2160 break;
2161 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2162 break;
2163 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2164 break;
2165 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2166 break;
2167 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2168 break;
2169 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2170 break;
2171 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2172 break;
2173 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2174 break;
2175 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2176 break;
2177 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2178 break;
2179 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2180 break;
2181 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2182 break;
2183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2184 break;
2185 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2186 break;
2187 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2188 break;
2189 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2190 break;
2191 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2192 break;
2193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2194 break;
2195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2196 break;
2197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2198 break;
2199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2200 break;
2201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2202 break;
2203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2204 break;
2205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2206 break;
2207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2208 break;
2209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2210 break;
2211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2212 break;
2213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2214 break;
2215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2216 break;
2217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2218 break;
2219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2220 break;
2221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2222 break;
2223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2224 break;
2225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2226 break;
2227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2228 break;
2229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2230 break;
2231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2232 break;
2233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2234 break;
2235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2236 break;
2237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2238 break;
2239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2240 break;
2241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2242 break;
2243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2244 break;
2245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2246 break;
2247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2248 break;
2249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2250 break;
2251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2252 break;
2253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2254 break;
2255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2256 break;
2257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2258 break;
2259 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2260 break;
2261 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2262 break;
2263 }
2264
2265 return ppc64_elf_howto_table[r];
2266 };
2267
2268 static reloc_howto_type *
2269 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2270 const char *r_name)
2271 {
2272 unsigned int i;
2273
2274 for (i = 0;
2275 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2276 i++)
2277 if (ppc64_elf_howto_raw[i].name != NULL
2278 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2279 return &ppc64_elf_howto_raw[i];
2280
2281 return NULL;
2282 }
2283
2284 /* Set the howto pointer for a PowerPC ELF reloc. */
2285
2286 static void
2287 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2288 Elf_Internal_Rela *dst)
2289 {
2290 unsigned int type;
2291
2292 /* Initialize howto table if needed. */
2293 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2294 ppc_howto_init ();
2295
2296 type = ELF64_R_TYPE (dst->r_info);
2297 if (type >= (sizeof (ppc64_elf_howto_table)
2298 / sizeof (ppc64_elf_howto_table[0])))
2299 {
2300 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2301 abfd, (int) type);
2302 type = R_PPC64_NONE;
2303 }
2304 cache_ptr->howto = ppc64_elf_howto_table[type];
2305 }
2306
2307 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2308
2309 static bfd_reloc_status_type
2310 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2311 void *data, asection *input_section,
2312 bfd *output_bfd, char **error_message)
2313 {
2314 /* If this is a relocatable link (output_bfd test tells us), just
2315 call the generic function. Any adjustment will be done at final
2316 link time. */
2317 if (output_bfd != NULL)
2318 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2319 input_section, output_bfd, error_message);
2320
2321 /* Adjust the addend for sign extension of the low 16 bits.
2322 We won't actually be using the low 16 bits, so trashing them
2323 doesn't matter. */
2324 reloc_entry->addend += 0x8000;
2325 return bfd_reloc_continue;
2326 }
2327
2328 static bfd_reloc_status_type
2329 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2330 void *data, asection *input_section,
2331 bfd *output_bfd, char **error_message)
2332 {
2333 if (output_bfd != NULL)
2334 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2335 input_section, output_bfd, error_message);
2336
2337 if (strcmp (symbol->section->name, ".opd") == 0
2338 && (symbol->section->owner->flags & DYNAMIC) == 0)
2339 {
2340 bfd_vma dest = opd_entry_value (symbol->section,
2341 symbol->value + reloc_entry->addend,
2342 NULL, NULL);
2343 if (dest != (bfd_vma) -1)
2344 reloc_entry->addend = dest - (symbol->value
2345 + symbol->section->output_section->vma
2346 + symbol->section->output_offset);
2347 }
2348 return bfd_reloc_continue;
2349 }
2350
2351 static bfd_reloc_status_type
2352 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2353 void *data, asection *input_section,
2354 bfd *output_bfd, char **error_message)
2355 {
2356 long insn;
2357 enum elf_ppc64_reloc_type r_type;
2358 bfd_size_type octets;
2359 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2360 bfd_boolean is_power4 = FALSE;
2361
2362 /* If this is a relocatable link (output_bfd test tells us), just
2363 call the generic function. Any adjustment will be done at final
2364 link time. */
2365 if (output_bfd != NULL)
2366 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2367 input_section, output_bfd, error_message);
2368
2369 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2370 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2371 insn &= ~(0x01 << 21);
2372 r_type = reloc_entry->howto->type;
2373 if (r_type == R_PPC64_ADDR14_BRTAKEN
2374 || r_type == R_PPC64_REL14_BRTAKEN)
2375 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2376
2377 if (is_power4)
2378 {
2379 /* Set 'a' bit. This is 0b00010 in BO field for branch
2380 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2381 for branch on CTR insns (BO == 1a00t or 1a01t). */
2382 if ((insn & (0x14 << 21)) == (0x04 << 21))
2383 insn |= 0x02 << 21;
2384 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2385 insn |= 0x08 << 21;
2386 else
2387 goto out;
2388 }
2389 else
2390 {
2391 bfd_vma target = 0;
2392 bfd_vma from;
2393
2394 if (!bfd_is_com_section (symbol->section))
2395 target = symbol->value;
2396 target += symbol->section->output_section->vma;
2397 target += symbol->section->output_offset;
2398 target += reloc_entry->addend;
2399
2400 from = (reloc_entry->address
2401 + input_section->output_offset
2402 + input_section->output_section->vma);
2403
2404 /* Invert 'y' bit if not the default. */
2405 if ((bfd_signed_vma) (target - from) < 0)
2406 insn ^= 0x01 << 21;
2407 }
2408 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2409 out:
2410 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2411 input_section, output_bfd, error_message);
2412 }
2413
2414 static bfd_reloc_status_type
2415 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2416 void *data, asection *input_section,
2417 bfd *output_bfd, char **error_message)
2418 {
2419 /* If this is a relocatable link (output_bfd test tells us), just
2420 call the generic function. Any adjustment will be done at final
2421 link time. */
2422 if (output_bfd != NULL)
2423 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2424 input_section, output_bfd, error_message);
2425
2426 /* Subtract the symbol section base address. */
2427 reloc_entry->addend -= symbol->section->output_section->vma;
2428 return bfd_reloc_continue;
2429 }
2430
2431 static bfd_reloc_status_type
2432 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2433 void *data, asection *input_section,
2434 bfd *output_bfd, char **error_message)
2435 {
2436 /* If this is a relocatable link (output_bfd test tells us), just
2437 call the generic function. Any adjustment will be done at final
2438 link time. */
2439 if (output_bfd != NULL)
2440 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2441 input_section, output_bfd, error_message);
2442
2443 /* Subtract the symbol section base address. */
2444 reloc_entry->addend -= symbol->section->output_section->vma;
2445
2446 /* Adjust the addend for sign extension of the low 16 bits. */
2447 reloc_entry->addend += 0x8000;
2448 return bfd_reloc_continue;
2449 }
2450
2451 static bfd_reloc_status_type
2452 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2453 void *data, asection *input_section,
2454 bfd *output_bfd, char **error_message)
2455 {
2456 bfd_vma TOCstart;
2457
2458 /* If this is a relocatable link (output_bfd test tells us), just
2459 call the generic function. Any adjustment will be done at final
2460 link time. */
2461 if (output_bfd != NULL)
2462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2463 input_section, output_bfd, error_message);
2464
2465 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2466 if (TOCstart == 0)
2467 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2468
2469 /* Subtract the TOC base address. */
2470 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2471 return bfd_reloc_continue;
2472 }
2473
2474 static bfd_reloc_status_type
2475 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2476 void *data, asection *input_section,
2477 bfd *output_bfd, char **error_message)
2478 {
2479 bfd_vma TOCstart;
2480
2481 /* If this is a relocatable link (output_bfd test tells us), just
2482 call the generic function. Any adjustment will be done at final
2483 link time. */
2484 if (output_bfd != NULL)
2485 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2486 input_section, output_bfd, error_message);
2487
2488 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2489 if (TOCstart == 0)
2490 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2491
2492 /* Subtract the TOC base address. */
2493 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2494
2495 /* Adjust the addend for sign extension of the low 16 bits. */
2496 reloc_entry->addend += 0x8000;
2497 return bfd_reloc_continue;
2498 }
2499
2500 static bfd_reloc_status_type
2501 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2502 void *data, asection *input_section,
2503 bfd *output_bfd, char **error_message)
2504 {
2505 bfd_vma TOCstart;
2506 bfd_size_type octets;
2507
2508 /* If this is a relocatable link (output_bfd test tells us), just
2509 call the generic function. Any adjustment will be done at final
2510 link time. */
2511 if (output_bfd != NULL)
2512 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2513 input_section, output_bfd, error_message);
2514
2515 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2516 if (TOCstart == 0)
2517 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2518
2519 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2520 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2521 return bfd_reloc_ok;
2522 }
2523
2524 static bfd_reloc_status_type
2525 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
2528 {
2529 /* If this is a relocatable link (output_bfd test tells us), just
2530 call the generic function. Any adjustment will be done at final
2531 link time. */
2532 if (output_bfd != NULL)
2533 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2534 input_section, output_bfd, error_message);
2535
2536 if (error_message != NULL)
2537 {
2538 static char buf[60];
2539 sprintf (buf, "generic linker can't handle %s",
2540 reloc_entry->howto->name);
2541 *error_message = buf;
2542 }
2543 return bfd_reloc_dangerous;
2544 }
2545
2546 /* Track GOT entries needed for a given symbol. We might need more
2547 than one got entry per symbol. */
2548 struct got_entry
2549 {
2550 struct got_entry *next;
2551
2552 /* The symbol addend that we'll be placing in the GOT. */
2553 bfd_vma addend;
2554
2555 /* Unlike other ELF targets, we use separate GOT entries for the same
2556 symbol referenced from different input files. This is to support
2557 automatic multiple TOC/GOT sections, where the TOC base can vary
2558 from one input file to another. After partitioning into TOC groups
2559 we merge entries within the group.
2560
2561 Point to the BFD owning this GOT entry. */
2562 bfd *owner;
2563
2564 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2565 TLS_TPREL or TLS_DTPREL for tls entries. */
2566 unsigned char tls_type;
2567
2568 /* Non-zero if got.ent points to real entry. */
2569 unsigned char is_indirect;
2570
2571 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 struct got_entry *ent;
2577 } got;
2578 };
2579
2580 /* The same for PLT. */
2581 struct plt_entry
2582 {
2583 struct plt_entry *next;
2584
2585 bfd_vma addend;
2586
2587 union
2588 {
2589 bfd_signed_vma refcount;
2590 bfd_vma offset;
2591 } plt;
2592 };
2593
2594 struct ppc64_elf_obj_tdata
2595 {
2596 struct elf_obj_tdata elf;
2597
2598 /* Shortcuts to dynamic linker sections. */
2599 asection *got;
2600 asection *relgot;
2601
2602 /* Used during garbage collection. We attach global symbols defined
2603 on removed .opd entries to this section so that the sym is removed. */
2604 asection *deleted_section;
2605
2606 /* TLS local dynamic got entry handling. Support for multiple GOT
2607 sections means we potentially need one of these for each input bfd. */
2608 struct got_entry tlsld_got;
2609
2610 /* A copy of relocs before they are modified for --emit-relocs. */
2611 Elf_Internal_Rela *opd_relocs;
2612
2613 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2614 the reloc to be in the range -32768 to 32767. */
2615 unsigned int has_small_toc_reloc : 1;
2616
2617 /* Set if toc/got ha relocs detected not using r2. */
2618 unsigned int ha_relocs_not_using_r2 : 1;
2619 };
2620
2621 #define ppc64_elf_tdata(bfd) \
2622 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2623
2624 #define ppc64_tlsld_got(bfd) \
2625 (&ppc64_elf_tdata (bfd)->tlsld_got)
2626
2627 #define is_ppc64_elf(bfd) \
2628 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2629 && elf_object_id (bfd) == PPC64_ELF_DATA)
2630
2631 /* Override the generic function because we store some extras. */
2632
2633 static bfd_boolean
2634 ppc64_elf_mkobject (bfd *abfd)
2635 {
2636 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2637 PPC64_ELF_DATA);
2638 }
2639
2640 /* Fix bad default arch selected for a 64 bit input bfd when the
2641 default is 32 bit. */
2642
2643 static bfd_boolean
2644 ppc64_elf_object_p (bfd *abfd)
2645 {
2646 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2647 {
2648 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2649
2650 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2651 {
2652 /* Relies on arch after 32 bit default being 64 bit default. */
2653 abfd->arch_info = abfd->arch_info->next;
2654 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2655 }
2656 }
2657 return TRUE;
2658 }
2659
2660 /* Support for core dump NOTE sections. */
2661
2662 static bfd_boolean
2663 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2664 {
2665 size_t offset, size;
2666
2667 if (note->descsz != 504)
2668 return FALSE;
2669
2670 /* pr_cursig */
2671 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2672
2673 /* pr_pid */
2674 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2675
2676 /* pr_reg */
2677 offset = 112;
2678 size = 384;
2679
2680 /* Make a ".reg/999" section. */
2681 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2682 size, note->descpos + offset);
2683 }
2684
2685 static bfd_boolean
2686 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2687 {
2688 if (note->descsz != 136)
2689 return FALSE;
2690
2691 elf_tdata (abfd)->core_pid
2692 = bfd_get_32 (abfd, note->descdata + 24);
2693 elf_tdata (abfd)->core_program
2694 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2695 elf_tdata (abfd)->core_command
2696 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2697
2698 return TRUE;
2699 }
2700
2701 static char *
2702 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2703 ...)
2704 {
2705 switch (note_type)
2706 {
2707 default:
2708 return NULL;
2709
2710 case NT_PRPSINFO:
2711 {
2712 char data[136];
2713 va_list ap;
2714
2715 va_start (ap, note_type);
2716 memset (data, 0, 40);
2717 strncpy (data + 40, va_arg (ap, const char *), 16);
2718 strncpy (data + 56, va_arg (ap, const char *), 80);
2719 va_end (ap);
2720 return elfcore_write_note (abfd, buf, bufsiz,
2721 "CORE", note_type, data, sizeof (data));
2722 }
2723
2724 case NT_PRSTATUS:
2725 {
2726 char data[504];
2727 va_list ap;
2728 long pid;
2729 int cursig;
2730 const void *greg;
2731
2732 va_start (ap, note_type);
2733 memset (data, 0, 112);
2734 pid = va_arg (ap, long);
2735 bfd_put_32 (abfd, pid, data + 32);
2736 cursig = va_arg (ap, int);
2737 bfd_put_16 (abfd, cursig, data + 12);
2738 greg = va_arg (ap, const void *);
2739 memcpy (data + 112, greg, 384);
2740 memset (data + 496, 0, 8);
2741 va_end (ap);
2742 return elfcore_write_note (abfd, buf, bufsiz,
2743 "CORE", note_type, data, sizeof (data));
2744 }
2745 }
2746 }
2747
2748 /* Add extra PPC sections. */
2749
2750 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2751 {
2752 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2753 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2754 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2755 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2756 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2757 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2758 { NULL, 0, 0, 0, 0 }
2759 };
2760
2761 enum _ppc64_sec_type {
2762 sec_normal = 0,
2763 sec_opd = 1,
2764 sec_toc = 2
2765 };
2766
2767 struct _ppc64_elf_section_data
2768 {
2769 struct bfd_elf_section_data elf;
2770
2771 union
2772 {
2773 /* An array with one entry for each opd function descriptor. */
2774 struct _opd_sec_data
2775 {
2776 /* Points to the function code section for local opd entries. */
2777 asection **func_sec;
2778
2779 /* After editing .opd, adjust references to opd local syms. */
2780 long *adjust;
2781 } opd;
2782
2783 /* An array for toc sections, indexed by offset/8. */
2784 struct _toc_sec_data
2785 {
2786 /* Specifies the relocation symbol index used at a given toc offset. */
2787 unsigned *symndx;
2788
2789 /* And the relocation addend. */
2790 bfd_vma *add;
2791 } toc;
2792 } u;
2793
2794 enum _ppc64_sec_type sec_type:2;
2795
2796 /* Flag set when small branches are detected. Used to
2797 select suitable defaults for the stub group size. */
2798 unsigned int has_14bit_branch:1;
2799 };
2800
2801 #define ppc64_elf_section_data(sec) \
2802 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2803
2804 static bfd_boolean
2805 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2806 {
2807 if (!sec->used_by_bfd)
2808 {
2809 struct _ppc64_elf_section_data *sdata;
2810 bfd_size_type amt = sizeof (*sdata);
2811
2812 sdata = bfd_zalloc (abfd, amt);
2813 if (sdata == NULL)
2814 return FALSE;
2815 sec->used_by_bfd = sdata;
2816 }
2817
2818 return _bfd_elf_new_section_hook (abfd, sec);
2819 }
2820
2821 static struct _opd_sec_data *
2822 get_opd_info (asection * sec)
2823 {
2824 if (sec != NULL
2825 && ppc64_elf_section_data (sec) != NULL
2826 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2827 return &ppc64_elf_section_data (sec)->u.opd;
2828 return NULL;
2829 }
2830 \f
2831 /* Parameters for the qsort hook. */
2832 static bfd_boolean synthetic_relocatable;
2833
2834 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2835
2836 static int
2837 compare_symbols (const void *ap, const void *bp)
2838 {
2839 const asymbol *a = * (const asymbol **) ap;
2840 const asymbol *b = * (const asymbol **) bp;
2841
2842 /* Section symbols first. */
2843 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2844 return -1;
2845 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2846 return 1;
2847
2848 /* then .opd symbols. */
2849 if (strcmp (a->section->name, ".opd") == 0
2850 && strcmp (b->section->name, ".opd") != 0)
2851 return -1;
2852 if (strcmp (a->section->name, ".opd") != 0
2853 && strcmp (b->section->name, ".opd") == 0)
2854 return 1;
2855
2856 /* then other code symbols. */
2857 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2858 == (SEC_CODE | SEC_ALLOC)
2859 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2860 != (SEC_CODE | SEC_ALLOC))
2861 return -1;
2862
2863 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2864 != (SEC_CODE | SEC_ALLOC)
2865 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2866 == (SEC_CODE | SEC_ALLOC))
2867 return 1;
2868
2869 if (synthetic_relocatable)
2870 {
2871 if (a->section->id < b->section->id)
2872 return -1;
2873
2874 if (a->section->id > b->section->id)
2875 return 1;
2876 }
2877
2878 if (a->value + a->section->vma < b->value + b->section->vma)
2879 return -1;
2880
2881 if (a->value + a->section->vma > b->value + b->section->vma)
2882 return 1;
2883
2884 /* For syms with the same value, prefer strong dynamic global function
2885 syms over other syms. */
2886 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2887 return -1;
2888
2889 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2890 return 1;
2891
2892 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2893 return -1;
2894
2895 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2896 return 1;
2897
2898 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2899 return -1;
2900
2901 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2902 return 1;
2903
2904 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2905 return -1;
2906
2907 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2908 return 1;
2909
2910 return 0;
2911 }
2912
2913 /* Search SYMS for a symbol of the given VALUE. */
2914
2915 static asymbol *
2916 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2917 {
2918 long mid;
2919
2920 if (id == -1)
2921 {
2922 while (lo < hi)
2923 {
2924 mid = (lo + hi) >> 1;
2925 if (syms[mid]->value + syms[mid]->section->vma < value)
2926 lo = mid + 1;
2927 else if (syms[mid]->value + syms[mid]->section->vma > value)
2928 hi = mid;
2929 else
2930 return syms[mid];
2931 }
2932 }
2933 else
2934 {
2935 while (lo < hi)
2936 {
2937 mid = (lo + hi) >> 1;
2938 if (syms[mid]->section->id < id)
2939 lo = mid + 1;
2940 else if (syms[mid]->section->id > id)
2941 hi = mid;
2942 else if (syms[mid]->value < value)
2943 lo = mid + 1;
2944 else if (syms[mid]->value > value)
2945 hi = mid;
2946 else
2947 return syms[mid];
2948 }
2949 }
2950 return NULL;
2951 }
2952
2953 static bfd_boolean
2954 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2955 {
2956 bfd_vma vma = *(bfd_vma *) ptr;
2957 return ((section->flags & SEC_ALLOC) != 0
2958 && section->vma <= vma
2959 && vma < section->vma + section->size);
2960 }
2961
2962 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2963 entry syms. Also generate @plt symbols for the glink branch table. */
2964
2965 static long
2966 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2967 long static_count, asymbol **static_syms,
2968 long dyn_count, asymbol **dyn_syms,
2969 asymbol **ret)
2970 {
2971 asymbol *s;
2972 long i;
2973 long count;
2974 char *names;
2975 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2976 asection *opd;
2977 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2978 asymbol **syms;
2979
2980 *ret = NULL;
2981
2982 opd = bfd_get_section_by_name (abfd, ".opd");
2983 if (opd == NULL)
2984 return 0;
2985
2986 symcount = static_count;
2987 if (!relocatable)
2988 symcount += dyn_count;
2989 if (symcount == 0)
2990 return 0;
2991
2992 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2993 if (syms == NULL)
2994 return -1;
2995
2996 if (!relocatable && static_count != 0 && dyn_count != 0)
2997 {
2998 /* Use both symbol tables. */
2999 memcpy (syms, static_syms, static_count * sizeof (*syms));
3000 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3001 }
3002 else if (!relocatable && static_count == 0)
3003 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3004 else
3005 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3006
3007 synthetic_relocatable = relocatable;
3008 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3009
3010 if (!relocatable && symcount > 1)
3011 {
3012 long j;
3013 /* Trim duplicate syms, since we may have merged the normal and
3014 dynamic symbols. Actually, we only care about syms that have
3015 different values, so trim any with the same value. */
3016 for (i = 1, j = 1; i < symcount; ++i)
3017 if (syms[i - 1]->value + syms[i - 1]->section->vma
3018 != syms[i]->value + syms[i]->section->vma)
3019 syms[j++] = syms[i];
3020 symcount = j;
3021 }
3022
3023 i = 0;
3024 if (strcmp (syms[i]->section->name, ".opd") == 0)
3025 ++i;
3026 codesecsym = i;
3027
3028 for (; i < symcount; ++i)
3029 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3030 != (SEC_CODE | SEC_ALLOC))
3031 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3032 break;
3033 codesecsymend = i;
3034
3035 for (; i < symcount; ++i)
3036 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3037 break;
3038 secsymend = i;
3039
3040 for (; i < symcount; ++i)
3041 if (strcmp (syms[i]->section->name, ".opd") != 0)
3042 break;
3043 opdsymend = i;
3044
3045 for (; i < symcount; ++i)
3046 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3047 != (SEC_CODE | SEC_ALLOC))
3048 break;
3049 symcount = i;
3050
3051 count = 0;
3052
3053 if (relocatable)
3054 {
3055 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3056 arelent *r;
3057 size_t size;
3058 long relcount;
3059
3060 if (opdsymend == secsymend)
3061 goto done;
3062
3063 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3064 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3065 if (relcount == 0)
3066 goto done;
3067
3068 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3069 {
3070 count = -1;
3071 goto done;
3072 }
3073
3074 size = 0;
3075 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3076 {
3077 asymbol *sym;
3078
3079 while (r < opd->relocation + relcount
3080 && r->address < syms[i]->value + opd->vma)
3081 ++r;
3082
3083 if (r == opd->relocation + relcount)
3084 break;
3085
3086 if (r->address != syms[i]->value + opd->vma)
3087 continue;
3088
3089 if (r->howto->type != R_PPC64_ADDR64)
3090 continue;
3091
3092 sym = *r->sym_ptr_ptr;
3093 if (!sym_exists_at (syms, opdsymend, symcount,
3094 sym->section->id, sym->value + r->addend))
3095 {
3096 ++count;
3097 size += sizeof (asymbol);
3098 size += strlen (syms[i]->name) + 2;
3099 }
3100 }
3101
3102 s = *ret = bfd_malloc (size);
3103 if (s == NULL)
3104 {
3105 count = -1;
3106 goto done;
3107 }
3108
3109 names = (char *) (s + count);
3110
3111 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3112 {
3113 asymbol *sym;
3114
3115 while (r < opd->relocation + relcount
3116 && r->address < syms[i]->value + opd->vma)
3117 ++r;
3118
3119 if (r == opd->relocation + relcount)
3120 break;
3121
3122 if (r->address != syms[i]->value + opd->vma)
3123 continue;
3124
3125 if (r->howto->type != R_PPC64_ADDR64)
3126 continue;
3127
3128 sym = *r->sym_ptr_ptr;
3129 if (!sym_exists_at (syms, opdsymend, symcount,
3130 sym->section->id, sym->value + r->addend))
3131 {
3132 size_t len;
3133
3134 *s = *syms[i];
3135 s->flags |= BSF_SYNTHETIC;
3136 s->section = sym->section;
3137 s->value = sym->value + r->addend;
3138 s->name = names;
3139 *names++ = '.';
3140 len = strlen (syms[i]->name);
3141 memcpy (names, syms[i]->name, len + 1);
3142 names += len + 1;
3143 /* Have udata.p point back to the original symbol this
3144 synthetic symbol was derived from. */
3145 s->udata.p = syms[i];
3146 s++;
3147 }
3148 }
3149 }
3150 else
3151 {
3152 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3153 bfd_byte *contents;
3154 size_t size;
3155 long plt_count = 0;
3156 bfd_vma glink_vma = 0, resolv_vma = 0;
3157 asection *dynamic, *glink = NULL, *relplt = NULL;
3158 arelent *p;
3159
3160 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3161 {
3162 if (contents)
3163 {
3164 free_contents_and_exit:
3165 free (contents);
3166 }
3167 count = -1;
3168 goto done;
3169 }
3170
3171 size = 0;
3172 for (i = secsymend; i < opdsymend; ++i)
3173 {
3174 bfd_vma ent;
3175
3176 /* Ignore bogus symbols. */
3177 if (syms[i]->value > opd->size - 8)
3178 continue;
3179
3180 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3181 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3182 {
3183 ++count;
3184 size += sizeof (asymbol);
3185 size += strlen (syms[i]->name) + 2;
3186 }
3187 }
3188
3189 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3190 if (dyn_count != 0
3191 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3192 {
3193 bfd_byte *dynbuf, *extdyn, *extdynend;
3194 size_t extdynsize;
3195 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3196
3197 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3198 goto free_contents_and_exit;
3199
3200 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3201 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3202
3203 extdyn = dynbuf;
3204 extdynend = extdyn + dynamic->size;
3205 for (; extdyn < extdynend; extdyn += extdynsize)
3206 {
3207 Elf_Internal_Dyn dyn;
3208 (*swap_dyn_in) (abfd, extdyn, &dyn);
3209
3210 if (dyn.d_tag == DT_NULL)
3211 break;
3212
3213 if (dyn.d_tag == DT_PPC64_GLINK)
3214 {
3215 /* The first glink stub starts at offset 32; see comment in
3216 ppc64_elf_finish_dynamic_sections. */
3217 glink_vma = dyn.d_un.d_val + 32;
3218 /* The .glink section usually does not survive the final
3219 link; search for the section (usually .text) where the
3220 glink stubs now reside. */
3221 glink = bfd_sections_find_if (abfd, section_covers_vma,
3222 &glink_vma);
3223 break;
3224 }
3225 }
3226
3227 free (dynbuf);
3228 }
3229
3230 if (glink != NULL)
3231 {
3232 /* Determine __glink trampoline by reading the relative branch
3233 from the first glink stub. */
3234 bfd_byte buf[4];
3235 if (bfd_get_section_contents (abfd, glink, buf,
3236 glink_vma + 4 - glink->vma, 4))
3237 {
3238 unsigned int insn = bfd_get_32 (abfd, buf);
3239 insn ^= B_DOT;
3240 if ((insn & ~0x3fffffc) == 0)
3241 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3242 }
3243
3244 if (resolv_vma)
3245 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3246
3247 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3248 if (relplt != NULL)
3249 {
3250 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3251 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3252 goto free_contents_and_exit;
3253
3254 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3255 size += plt_count * sizeof (asymbol);
3256
3257 p = relplt->relocation;
3258 for (i = 0; i < plt_count; i++, p++)
3259 {
3260 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3261 if (p->addend != 0)
3262 size += sizeof ("+0x") - 1 + 16;
3263 }
3264 }
3265 }
3266
3267 s = *ret = bfd_malloc (size);
3268 if (s == NULL)
3269 goto free_contents_and_exit;
3270
3271 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3272
3273 for (i = secsymend; i < opdsymend; ++i)
3274 {
3275 bfd_vma ent;
3276
3277 if (syms[i]->value > opd->size - 8)
3278 continue;
3279
3280 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3281 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3282 {
3283 long lo, hi;
3284 size_t len;
3285 asection *sec = abfd->sections;
3286
3287 *s = *syms[i];
3288 lo = codesecsym;
3289 hi = codesecsymend;
3290 while (lo < hi)
3291 {
3292 long mid = (lo + hi) >> 1;
3293 if (syms[mid]->section->vma < ent)
3294 lo = mid + 1;
3295 else if (syms[mid]->section->vma > ent)
3296 hi = mid;
3297 else
3298 {
3299 sec = syms[mid]->section;
3300 break;
3301 }
3302 }
3303
3304 if (lo >= hi && lo > codesecsym)
3305 sec = syms[lo - 1]->section;
3306
3307 for (; sec != NULL; sec = sec->next)
3308 {
3309 if (sec->vma > ent)
3310 break;
3311 /* SEC_LOAD may not be set if SEC is from a separate debug
3312 info file. */
3313 if ((sec->flags & SEC_ALLOC) == 0)
3314 break;
3315 if ((sec->flags & SEC_CODE) != 0)
3316 s->section = sec;
3317 }
3318 s->flags |= BSF_SYNTHETIC;
3319 s->value = ent - s->section->vma;
3320 s->name = names;
3321 *names++ = '.';
3322 len = strlen (syms[i]->name);
3323 memcpy (names, syms[i]->name, len + 1);
3324 names += len + 1;
3325 /* Have udata.p point back to the original symbol this
3326 synthetic symbol was derived from. */
3327 s->udata.p = syms[i];
3328 s++;
3329 }
3330 }
3331 free (contents);
3332
3333 if (glink != NULL && relplt != NULL)
3334 {
3335 if (resolv_vma)
3336 {
3337 /* Add a symbol for the main glink trampoline. */
3338 memset (s, 0, sizeof *s);
3339 s->the_bfd = abfd;
3340 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3341 s->section = glink;
3342 s->value = resolv_vma - glink->vma;
3343 s->name = names;
3344 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3345 names += sizeof ("__glink_PLTresolve");
3346 s++;
3347 count++;
3348 }
3349
3350 /* FIXME: It would be very much nicer to put sym@plt on the
3351 stub rather than on the glink branch table entry. The
3352 objdump disassembler would then use a sensible symbol
3353 name on plt calls. The difficulty in doing so is
3354 a) finding the stubs, and,
3355 b) matching stubs against plt entries, and,
3356 c) there can be multiple stubs for a given plt entry.
3357
3358 Solving (a) could be done by code scanning, but older
3359 ppc64 binaries used different stubs to current code.
3360 (b) is the tricky one since you need to known the toc
3361 pointer for at least one function that uses a pic stub to
3362 be able to calculate the plt address referenced.
3363 (c) means gdb would need to set multiple breakpoints (or
3364 find the glink branch itself) when setting breakpoints
3365 for pending shared library loads. */
3366 p = relplt->relocation;
3367 for (i = 0; i < plt_count; i++, p++)
3368 {
3369 size_t len;
3370
3371 *s = **p->sym_ptr_ptr;
3372 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3373 we are defining a symbol, ensure one of them is set. */
3374 if ((s->flags & BSF_LOCAL) == 0)
3375 s->flags |= BSF_GLOBAL;
3376 s->flags |= BSF_SYNTHETIC;
3377 s->section = glink;
3378 s->value = glink_vma - glink->vma;
3379 s->name = names;
3380 s->udata.p = NULL;
3381 len = strlen ((*p->sym_ptr_ptr)->name);
3382 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3383 names += len;
3384 if (p->addend != 0)
3385 {
3386 memcpy (names, "+0x", sizeof ("+0x") - 1);
3387 names += sizeof ("+0x") - 1;
3388 bfd_sprintf_vma (abfd, names, p->addend);
3389 names += strlen (names);
3390 }
3391 memcpy (names, "@plt", sizeof ("@plt"));
3392 names += sizeof ("@plt");
3393 s++;
3394 glink_vma += 8;
3395 if (i >= 0x8000)
3396 glink_vma += 4;
3397 }
3398 count += plt_count;
3399 }
3400 }
3401
3402 done:
3403 free (syms);
3404 return count;
3405 }
3406 \f
3407 /* The following functions are specific to the ELF linker, while
3408 functions above are used generally. Those named ppc64_elf_* are
3409 called by the main ELF linker code. They appear in this file more
3410 or less in the order in which they are called. eg.
3411 ppc64_elf_check_relocs is called early in the link process,
3412 ppc64_elf_finish_dynamic_sections is one of the last functions
3413 called.
3414
3415 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3416 functions have both a function code symbol and a function descriptor
3417 symbol. A call to foo in a relocatable object file looks like:
3418
3419 . .text
3420 . x:
3421 . bl .foo
3422 . nop
3423
3424 The function definition in another object file might be:
3425
3426 . .section .opd
3427 . foo: .quad .foo
3428 . .quad .TOC.@tocbase
3429 . .quad 0
3430 .
3431 . .text
3432 . .foo: blr
3433
3434 When the linker resolves the call during a static link, the branch
3435 unsurprisingly just goes to .foo and the .opd information is unused.
3436 If the function definition is in a shared library, things are a little
3437 different: The call goes via a plt call stub, the opd information gets
3438 copied to the plt, and the linker patches the nop.
3439
3440 . x:
3441 . bl .foo_stub
3442 . ld 2,40(1)
3443 .
3444 .
3445 . .foo_stub:
3446 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3447 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3448 . std 2,40(1) # this is the general idea
3449 . ld 11,0(12)
3450 . ld 2,8(12)
3451 . mtctr 11
3452 . ld 11,16(12)
3453 . bctr
3454 .
3455 . .section .plt
3456 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3457
3458 The "reloc ()" notation is supposed to indicate that the linker emits
3459 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3460 copying.
3461
3462 What are the difficulties here? Well, firstly, the relocations
3463 examined by the linker in check_relocs are against the function code
3464 sym .foo, while the dynamic relocation in the plt is emitted against
3465 the function descriptor symbol, foo. Somewhere along the line, we need
3466 to carefully copy dynamic link information from one symbol to the other.
3467 Secondly, the generic part of the elf linker will make .foo a dynamic
3468 symbol as is normal for most other backends. We need foo dynamic
3469 instead, at least for an application final link. However, when
3470 creating a shared library containing foo, we need to have both symbols
3471 dynamic so that references to .foo are satisfied during the early
3472 stages of linking. Otherwise the linker might decide to pull in a
3473 definition from some other object, eg. a static library.
3474
3475 Update: As of August 2004, we support a new convention. Function
3476 calls may use the function descriptor symbol, ie. "bl foo". This
3477 behaves exactly as "bl .foo". */
3478
3479 /* Of those relocs that might be copied as dynamic relocs, this function
3480 selects those that must be copied when linking a shared library,
3481 even when the symbol is local. */
3482
3483 static int
3484 must_be_dyn_reloc (struct bfd_link_info *info,
3485 enum elf_ppc64_reloc_type r_type)
3486 {
3487 switch (r_type)
3488 {
3489 default:
3490 return 1;
3491
3492 case R_PPC64_REL32:
3493 case R_PPC64_REL64:
3494 case R_PPC64_REL30:
3495 return 0;
3496
3497 case R_PPC64_TPREL16:
3498 case R_PPC64_TPREL16_LO:
3499 case R_PPC64_TPREL16_HI:
3500 case R_PPC64_TPREL16_HA:
3501 case R_PPC64_TPREL16_DS:
3502 case R_PPC64_TPREL16_LO_DS:
3503 case R_PPC64_TPREL16_HIGHER:
3504 case R_PPC64_TPREL16_HIGHERA:
3505 case R_PPC64_TPREL16_HIGHEST:
3506 case R_PPC64_TPREL16_HIGHESTA:
3507 case R_PPC64_TPREL64:
3508 return !info->executable;
3509 }
3510 }
3511
3512 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3513 copying dynamic variables from a shared lib into an app's dynbss
3514 section, and instead use a dynamic relocation to point into the
3515 shared lib. With code that gcc generates, it's vital that this be
3516 enabled; In the PowerPC64 ABI, the address of a function is actually
3517 the address of a function descriptor, which resides in the .opd
3518 section. gcc uses the descriptor directly rather than going via the
3519 GOT as some other ABI's do, which means that initialized function
3520 pointers must reference the descriptor. Thus, a function pointer
3521 initialized to the address of a function in a shared library will
3522 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3523 redefines the function descriptor symbol to point to the copy. This
3524 presents a problem as a plt entry for that function is also
3525 initialized from the function descriptor symbol and the copy reloc
3526 may not be initialized first. */
3527 #define ELIMINATE_COPY_RELOCS 1
3528
3529 /* Section name for stubs is the associated section name plus this
3530 string. */
3531 #define STUB_SUFFIX ".stub"
3532
3533 /* Linker stubs.
3534 ppc_stub_long_branch:
3535 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3536 destination, but a 24 bit branch in a stub section will reach.
3537 . b dest
3538
3539 ppc_stub_plt_branch:
3540 Similar to the above, but a 24 bit branch in the stub section won't
3541 reach its destination.
3542 . addis %r12,%r2,xxx@toc@ha
3543 . ld %r11,xxx@toc@l(%r12)
3544 . mtctr %r11
3545 . bctr
3546
3547 ppc_stub_plt_call:
3548 Used to call a function in a shared library. If it so happens that
3549 the plt entry referenced crosses a 64k boundary, then an extra
3550 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3551 . addis %r12,%r2,xxx@toc@ha
3552 . std %r2,40(%r1)
3553 . ld %r11,xxx+0@toc@l(%r12)
3554 . mtctr %r11
3555 . ld %r2,xxx+8@toc@l(%r12)
3556 . ld %r11,xxx+16@toc@l(%r12)
3557 . bctr
3558
3559 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3560 code to adjust the value and save r2 to support multiple toc sections.
3561 A ppc_stub_long_branch with an r2 offset looks like:
3562 . std %r2,40(%r1)
3563 . addis %r2,%r2,off@ha
3564 . addi %r2,%r2,off@l
3565 . b dest
3566
3567 A ppc_stub_plt_branch with an r2 offset looks like:
3568 . std %r2,40(%r1)
3569 . addis %r12,%r2,xxx@toc@ha
3570 . ld %r11,xxx@toc@l(%r12)
3571 . addis %r2,%r2,off@ha
3572 . addi %r2,%r2,off@l
3573 . mtctr %r11
3574 . bctr
3575
3576 In cases where the "addis" instruction would add zero, the "addis" is
3577 omitted and following instructions modified slightly in some cases.
3578 */
3579
3580 enum ppc_stub_type {
3581 ppc_stub_none,
3582 ppc_stub_long_branch,
3583 ppc_stub_long_branch_r2off,
3584 ppc_stub_plt_branch,
3585 ppc_stub_plt_branch_r2off,
3586 ppc_stub_plt_call
3587 };
3588
3589 struct ppc_stub_hash_entry {
3590
3591 /* Base hash table entry structure. */
3592 struct bfd_hash_entry root;
3593
3594 enum ppc_stub_type stub_type;
3595
3596 /* The stub section. */
3597 asection *stub_sec;
3598
3599 /* Offset within stub_sec of the beginning of this stub. */
3600 bfd_vma stub_offset;
3601
3602 /* Given the symbol's value and its section we can determine its final
3603 value when building the stubs (so the stub knows where to jump. */
3604 bfd_vma target_value;
3605 asection *target_section;
3606
3607 /* The symbol table entry, if any, that this was derived from. */
3608 struct ppc_link_hash_entry *h;
3609 struct plt_entry *plt_ent;
3610
3611 /* And the reloc addend that this was derived from. */
3612 bfd_vma addend;
3613
3614 /* Where this stub is being called from, or, in the case of combined
3615 stub sections, the first input section in the group. */
3616 asection *id_sec;
3617 };
3618
3619 struct ppc_branch_hash_entry {
3620
3621 /* Base hash table entry structure. */
3622 struct bfd_hash_entry root;
3623
3624 /* Offset within branch lookup table. */
3625 unsigned int offset;
3626
3627 /* Generation marker. */
3628 unsigned int iter;
3629 };
3630
3631 struct ppc_link_hash_entry
3632 {
3633 struct elf_link_hash_entry elf;
3634
3635 union {
3636 /* A pointer to the most recently used stub hash entry against this
3637 symbol. */
3638 struct ppc_stub_hash_entry *stub_cache;
3639
3640 /* A pointer to the next symbol starting with a '.' */
3641 struct ppc_link_hash_entry *next_dot_sym;
3642 } u;
3643
3644 /* Track dynamic relocs copied for this symbol. */
3645 struct elf_dyn_relocs *dyn_relocs;
3646
3647 /* Link between function code and descriptor symbols. */
3648 struct ppc_link_hash_entry *oh;
3649
3650 /* Flag function code and descriptor symbols. */
3651 unsigned int is_func:1;
3652 unsigned int is_func_descriptor:1;
3653 unsigned int fake:1;
3654
3655 /* Whether global opd/toc sym has been adjusted or not.
3656 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3657 should be set for all globals defined in any opd/toc section. */
3658 unsigned int adjust_done:1;
3659
3660 /* Set if we twiddled this symbol to weak at some stage. */
3661 unsigned int was_undefined:1;
3662
3663 /* Contexts in which symbol is used in the GOT (or TOC).
3664 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3665 corresponding relocs are encountered during check_relocs.
3666 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3667 indicate the corresponding GOT entry type is not needed.
3668 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3669 a TPREL one. We use a separate flag rather than setting TPREL
3670 just for convenience in distinguishing the two cases. */
3671 #define TLS_GD 1 /* GD reloc. */
3672 #define TLS_LD 2 /* LD reloc. */
3673 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3674 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3675 #define TLS_TLS 16 /* Any TLS reloc. */
3676 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3677 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3678 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3679 unsigned char tls_mask;
3680 };
3681
3682 /* ppc64 ELF linker hash table. */
3683
3684 struct ppc_link_hash_table
3685 {
3686 struct elf_link_hash_table elf;
3687
3688 /* The stub hash table. */
3689 struct bfd_hash_table stub_hash_table;
3690
3691 /* Another hash table for plt_branch stubs. */
3692 struct bfd_hash_table branch_hash_table;
3693
3694 /* Hash table for function prologue tocsave. */
3695 htab_t tocsave_htab;
3696
3697 /* Linker stub bfd. */
3698 bfd *stub_bfd;
3699
3700 /* Linker call-backs. */
3701 asection * (*add_stub_section) (const char *, asection *);
3702 void (*layout_sections_again) (void);
3703
3704 /* Array to keep track of which stub sections have been created, and
3705 information on stub grouping. */
3706 struct map_stub {
3707 /* This is the section to which stubs in the group will be attached. */
3708 asection *link_sec;
3709 /* The stub section. */
3710 asection *stub_sec;
3711 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3712 bfd_vma toc_off;
3713 } *stub_group;
3714
3715 /* Temp used when calculating TOC pointers. */
3716 bfd_vma toc_curr;
3717 bfd *toc_bfd;
3718 asection *toc_first_sec;
3719
3720 /* Highest input section id. */
3721 int top_id;
3722
3723 /* Highest output section index. */
3724 int top_index;
3725
3726 /* Used when adding symbols. */
3727 struct ppc_link_hash_entry *dot_syms;
3728
3729 /* List of input sections for each output section. */
3730 asection **input_list;
3731
3732 /* Short-cuts to get to dynamic linker sections. */
3733 asection *got;
3734 asection *plt;
3735 asection *relplt;
3736 asection *iplt;
3737 asection *reliplt;
3738 asection *dynbss;
3739 asection *relbss;
3740 asection *glink;
3741 asection *sfpr;
3742 asection *brlt;
3743 asection *relbrlt;
3744 asection *glink_eh_frame;
3745
3746 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3747 struct ppc_link_hash_entry *tls_get_addr;
3748 struct ppc_link_hash_entry *tls_get_addr_fd;
3749
3750 /* The size of reliplt used by got entry relocs. */
3751 bfd_size_type got_reli_size;
3752
3753 /* Statistics. */
3754 unsigned long stub_count[ppc_stub_plt_call];
3755
3756 /* Number of stubs against global syms. */
3757 unsigned long stub_globals;
3758
3759 /* Set if PLT call stubs should load r11. */
3760 unsigned int plt_static_chain:1;
3761
3762 /* Set if we should emit symbols for stubs. */
3763 unsigned int emit_stub_syms:1;
3764
3765 /* Set if __tls_get_addr optimization should not be done. */
3766 unsigned int no_tls_get_addr_opt:1;
3767
3768 /* Support for multiple toc sections. */
3769 unsigned int do_multi_toc:1;
3770 unsigned int multi_toc_needed:1;
3771 unsigned int second_toc_pass:1;
3772 unsigned int do_toc_opt:1;
3773
3774 /* Set on error. */
3775 unsigned int stub_error:1;
3776
3777 /* Temp used by ppc64_elf_process_dot_syms. */
3778 unsigned int twiddled_syms:1;
3779
3780 /* Incremented every time we size stubs. */
3781 unsigned int stub_iteration;
3782
3783 /* Small local sym cache. */
3784 struct sym_cache sym_cache;
3785 };
3786
3787 /* Rename some of the generic section flags to better document how they
3788 are used here. */
3789
3790 /* Nonzero if this section has TLS related relocations. */
3791 #define has_tls_reloc sec_flg0
3792
3793 /* Nonzero if this section has a call to __tls_get_addr. */
3794 #define has_tls_get_addr_call sec_flg1
3795
3796 /* Nonzero if this section has any toc or got relocs. */
3797 #define has_toc_reloc sec_flg2
3798
3799 /* Nonzero if this section has a call to another section that uses
3800 the toc or got. */
3801 #define makes_toc_func_call sec_flg3
3802
3803 /* Recursion protection when determining above flag. */
3804 #define call_check_in_progress sec_flg4
3805 #define call_check_done sec_flg5
3806
3807 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3808
3809 #define ppc_hash_table(p) \
3810 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3811 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3812
3813 #define ppc_stub_hash_lookup(table, string, create, copy) \
3814 ((struct ppc_stub_hash_entry *) \
3815 bfd_hash_lookup ((table), (string), (create), (copy)))
3816
3817 #define ppc_branch_hash_lookup(table, string, create, copy) \
3818 ((struct ppc_branch_hash_entry *) \
3819 bfd_hash_lookup ((table), (string), (create), (copy)))
3820
3821 /* Create an entry in the stub hash table. */
3822
3823 static struct bfd_hash_entry *
3824 stub_hash_newfunc (struct bfd_hash_entry *entry,
3825 struct bfd_hash_table *table,
3826 const char *string)
3827 {
3828 /* Allocate the structure if it has not already been allocated by a
3829 subclass. */
3830 if (entry == NULL)
3831 {
3832 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3833 if (entry == NULL)
3834 return entry;
3835 }
3836
3837 /* Call the allocation method of the superclass. */
3838 entry = bfd_hash_newfunc (entry, table, string);
3839 if (entry != NULL)
3840 {
3841 struct ppc_stub_hash_entry *eh;
3842
3843 /* Initialize the local fields. */
3844 eh = (struct ppc_stub_hash_entry *) entry;
3845 eh->stub_type = ppc_stub_none;
3846 eh->stub_sec = NULL;
3847 eh->stub_offset = 0;
3848 eh->target_value = 0;
3849 eh->target_section = NULL;
3850 eh->h = NULL;
3851 eh->id_sec = NULL;
3852 }
3853
3854 return entry;
3855 }
3856
3857 /* Create an entry in the branch hash table. */
3858
3859 static struct bfd_hash_entry *
3860 branch_hash_newfunc (struct bfd_hash_entry *entry,
3861 struct bfd_hash_table *table,
3862 const char *string)
3863 {
3864 /* Allocate the structure if it has not already been allocated by a
3865 subclass. */
3866 if (entry == NULL)
3867 {
3868 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3869 if (entry == NULL)
3870 return entry;
3871 }
3872
3873 /* Call the allocation method of the superclass. */
3874 entry = bfd_hash_newfunc (entry, table, string);
3875 if (entry != NULL)
3876 {
3877 struct ppc_branch_hash_entry *eh;
3878
3879 /* Initialize the local fields. */
3880 eh = (struct ppc_branch_hash_entry *) entry;
3881 eh->offset = 0;
3882 eh->iter = 0;
3883 }
3884
3885 return entry;
3886 }
3887
3888 /* Create an entry in a ppc64 ELF linker hash table. */
3889
3890 static struct bfd_hash_entry *
3891 link_hash_newfunc (struct bfd_hash_entry *entry,
3892 struct bfd_hash_table *table,
3893 const char *string)
3894 {
3895 /* Allocate the structure if it has not already been allocated by a
3896 subclass. */
3897 if (entry == NULL)
3898 {
3899 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3900 if (entry == NULL)
3901 return entry;
3902 }
3903
3904 /* Call the allocation method of the superclass. */
3905 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3906 if (entry != NULL)
3907 {
3908 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3909
3910 memset (&eh->u.stub_cache, 0,
3911 (sizeof (struct ppc_link_hash_entry)
3912 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3913
3914 /* When making function calls, old ABI code references function entry
3915 points (dot symbols), while new ABI code references the function
3916 descriptor symbol. We need to make any combination of reference and
3917 definition work together, without breaking archive linking.
3918
3919 For a defined function "foo" and an undefined call to "bar":
3920 An old object defines "foo" and ".foo", references ".bar" (possibly
3921 "bar" too).
3922 A new object defines "foo" and references "bar".
3923
3924 A new object thus has no problem with its undefined symbols being
3925 satisfied by definitions in an old object. On the other hand, the
3926 old object won't have ".bar" satisfied by a new object.
3927
3928 Keep a list of newly added dot-symbols. */
3929
3930 if (string[0] == '.')
3931 {
3932 struct ppc_link_hash_table *htab;
3933
3934 htab = (struct ppc_link_hash_table *) table;
3935 eh->u.next_dot_sym = htab->dot_syms;
3936 htab->dot_syms = eh;
3937 }
3938 }
3939
3940 return entry;
3941 }
3942
3943 struct tocsave_entry {
3944 asection *sec;
3945 bfd_vma offset;
3946 };
3947
3948 static hashval_t
3949 tocsave_htab_hash (const void *p)
3950 {
3951 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3952 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3953 }
3954
3955 static int
3956 tocsave_htab_eq (const void *p1, const void *p2)
3957 {
3958 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3959 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3960 return e1->sec == e2->sec && e1->offset == e2->offset;
3961 }
3962
3963 /* Create a ppc64 ELF linker hash table. */
3964
3965 static struct bfd_link_hash_table *
3966 ppc64_elf_link_hash_table_create (bfd *abfd)
3967 {
3968 struct ppc_link_hash_table *htab;
3969 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3970
3971 htab = bfd_zmalloc (amt);
3972 if (htab == NULL)
3973 return NULL;
3974
3975 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3976 sizeof (struct ppc_link_hash_entry),
3977 PPC64_ELF_DATA))
3978 {
3979 free (htab);
3980 return NULL;
3981 }
3982
3983 /* Init the stub hash table too. */
3984 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3985 sizeof (struct ppc_stub_hash_entry)))
3986 return NULL;
3987
3988 /* And the branch hash table. */
3989 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3990 sizeof (struct ppc_branch_hash_entry)))
3991 return NULL;
3992
3993 htab->tocsave_htab = htab_try_create (1024,
3994 tocsave_htab_hash,
3995 tocsave_htab_eq,
3996 NULL);
3997 if (htab->tocsave_htab == NULL)
3998 return NULL;
3999
4000 /* Initializing two fields of the union is just cosmetic. We really
4001 only care about glist, but when compiled on a 32-bit host the
4002 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4003 debugger inspection of these fields look nicer. */
4004 htab->elf.init_got_refcount.refcount = 0;
4005 htab->elf.init_got_refcount.glist = NULL;
4006 htab->elf.init_plt_refcount.refcount = 0;
4007 htab->elf.init_plt_refcount.glist = NULL;
4008 htab->elf.init_got_offset.offset = 0;
4009 htab->elf.init_got_offset.glist = NULL;
4010 htab->elf.init_plt_offset.offset = 0;
4011 htab->elf.init_plt_offset.glist = NULL;
4012
4013 return &htab->elf.root;
4014 }
4015
4016 /* Free the derived linker hash table. */
4017
4018 static void
4019 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4020 {
4021 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4022
4023 bfd_hash_table_free (&htab->stub_hash_table);
4024 bfd_hash_table_free (&htab->branch_hash_table);
4025 if (htab->tocsave_htab)
4026 htab_delete (htab->tocsave_htab);
4027 _bfd_generic_link_hash_table_free (hash);
4028 }
4029
4030 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4031
4032 void
4033 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4034 {
4035 struct ppc_link_hash_table *htab;
4036
4037 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4038
4039 /* Always hook our dynamic sections into the first bfd, which is the
4040 linker created stub bfd. This ensures that the GOT header is at
4041 the start of the output TOC section. */
4042 htab = ppc_hash_table (info);
4043 if (htab == NULL)
4044 return;
4045 htab->stub_bfd = abfd;
4046 htab->elf.dynobj = abfd;
4047 }
4048
4049 /* Build a name for an entry in the stub hash table. */
4050
4051 static char *
4052 ppc_stub_name (const asection *input_section,
4053 const asection *sym_sec,
4054 const struct ppc_link_hash_entry *h,
4055 const Elf_Internal_Rela *rel)
4056 {
4057 char *stub_name;
4058 bfd_size_type len;
4059
4060 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4061 offsets from a sym as a branch target? In fact, we could
4062 probably assume the addend is always zero. */
4063 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4064
4065 if (h)
4066 {
4067 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4068 stub_name = bfd_malloc (len);
4069 if (stub_name == NULL)
4070 return stub_name;
4071
4072 sprintf (stub_name, "%08x.%s+%x",
4073 input_section->id & 0xffffffff,
4074 h->elf.root.root.string,
4075 (int) rel->r_addend & 0xffffffff);
4076 }
4077 else
4078 {
4079 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4080 stub_name = bfd_malloc (len);
4081 if (stub_name == NULL)
4082 return stub_name;
4083
4084 sprintf (stub_name, "%08x.%x:%x+%x",
4085 input_section->id & 0xffffffff,
4086 sym_sec->id & 0xffffffff,
4087 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4088 (int) rel->r_addend & 0xffffffff);
4089 }
4090 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4091 stub_name[len - 2] = 0;
4092 return stub_name;
4093 }
4094
4095 /* Look up an entry in the stub hash. Stub entries are cached because
4096 creating the stub name takes a bit of time. */
4097
4098 static struct ppc_stub_hash_entry *
4099 ppc_get_stub_entry (const asection *input_section,
4100 const asection *sym_sec,
4101 struct ppc_link_hash_entry *h,
4102 const Elf_Internal_Rela *rel,
4103 struct ppc_link_hash_table *htab)
4104 {
4105 struct ppc_stub_hash_entry *stub_entry;
4106 const asection *id_sec;
4107
4108 /* If this input section is part of a group of sections sharing one
4109 stub section, then use the id of the first section in the group.
4110 Stub names need to include a section id, as there may well be
4111 more than one stub used to reach say, printf, and we need to
4112 distinguish between them. */
4113 id_sec = htab->stub_group[input_section->id].link_sec;
4114
4115 if (h != NULL && h->u.stub_cache != NULL
4116 && h->u.stub_cache->h == h
4117 && h->u.stub_cache->id_sec == id_sec)
4118 {
4119 stub_entry = h->u.stub_cache;
4120 }
4121 else
4122 {
4123 char *stub_name;
4124
4125 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4126 if (stub_name == NULL)
4127 return NULL;
4128
4129 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4130 stub_name, FALSE, FALSE);
4131 if (h != NULL)
4132 h->u.stub_cache = stub_entry;
4133
4134 free (stub_name);
4135 }
4136
4137 return stub_entry;
4138 }
4139
4140 /* Add a new stub entry to the stub hash. Not all fields of the new
4141 stub entry are initialised. */
4142
4143 static struct ppc_stub_hash_entry *
4144 ppc_add_stub (const char *stub_name,
4145 asection *section,
4146 struct bfd_link_info *info)
4147 {
4148 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4149 asection *link_sec;
4150 asection *stub_sec;
4151 struct ppc_stub_hash_entry *stub_entry;
4152
4153 link_sec = htab->stub_group[section->id].link_sec;
4154 stub_sec = htab->stub_group[section->id].stub_sec;
4155 if (stub_sec == NULL)
4156 {
4157 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4158 if (stub_sec == NULL)
4159 {
4160 size_t namelen;
4161 bfd_size_type len;
4162 char *s_name;
4163
4164 namelen = strlen (link_sec->name);
4165 len = namelen + sizeof (STUB_SUFFIX);
4166 s_name = bfd_alloc (htab->stub_bfd, len);
4167 if (s_name == NULL)
4168 return NULL;
4169
4170 memcpy (s_name, link_sec->name, namelen);
4171 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4172 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4173 if (stub_sec == NULL)
4174 return NULL;
4175 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4176 }
4177 htab->stub_group[section->id].stub_sec = stub_sec;
4178 }
4179
4180 /* Enter this entry into the linker stub hash table. */
4181 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4182 TRUE, FALSE);
4183 if (stub_entry == NULL)
4184 {
4185 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4186 section->owner, stub_name);
4187 return NULL;
4188 }
4189
4190 stub_entry->stub_sec = stub_sec;
4191 stub_entry->stub_offset = 0;
4192 stub_entry->id_sec = link_sec;
4193 return stub_entry;
4194 }
4195
4196 /* Create sections for linker generated code. */
4197
4198 static bfd_boolean
4199 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4200 {
4201 struct ppc_link_hash_table *htab;
4202 flagword flags;
4203
4204 htab = ppc_hash_table (info);
4205 if (htab == NULL)
4206 return FALSE;
4207
4208 /* Create .sfpr for code to save and restore fp regs. */
4209 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4210 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4211 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4212 flags);
4213 if (htab->sfpr == NULL
4214 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4215 return FALSE;
4216
4217 /* Create .glink for lazy dynamic linking support. */
4218 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4219 flags);
4220 if (htab->glink == NULL
4221 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4222 return FALSE;
4223
4224 if (!info->no_ld_generated_unwind_info)
4225 {
4226 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4227 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4228 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4229 ".eh_frame",
4230 flags);
4231 if (htab->glink_eh_frame == NULL
4232 || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4233 return FALSE;
4234 }
4235
4236 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4237 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4238 if (htab->iplt == NULL
4239 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4240 return FALSE;
4241
4242 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4243 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4244 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4245 ".rela.iplt",
4246 flags);
4247 if (htab->reliplt == NULL
4248 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4249 return FALSE;
4250
4251 /* Create branch lookup table for plt_branch stubs. */
4252 flags = (SEC_ALLOC | SEC_LOAD
4253 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4254 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4255 flags);
4256 if (htab->brlt == NULL
4257 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4258 return FALSE;
4259
4260 if (!info->shared)
4261 return TRUE;
4262
4263 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4264 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4265 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4266 ".rela.branch_lt",
4267 flags);
4268 if (htab->relbrlt == NULL
4269 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4270 return FALSE;
4271
4272 return TRUE;
4273 }
4274
4275 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4276 not already done. */
4277
4278 static bfd_boolean
4279 create_got_section (bfd *abfd, struct bfd_link_info *info)
4280 {
4281 asection *got, *relgot;
4282 flagword flags;
4283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4284
4285 if (!is_ppc64_elf (abfd))
4286 return FALSE;
4287 if (htab == NULL)
4288 return FALSE;
4289
4290 if (!htab->got)
4291 {
4292 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4293 return FALSE;
4294
4295 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4296 if (!htab->got)
4297 abort ();
4298 }
4299
4300 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4301 | SEC_LINKER_CREATED);
4302
4303 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4304 if (!got
4305 || !bfd_set_section_alignment (abfd, got, 3))
4306 return FALSE;
4307
4308 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4309 flags | SEC_READONLY);
4310 if (!relgot
4311 || ! bfd_set_section_alignment (abfd, relgot, 3))
4312 return FALSE;
4313
4314 ppc64_elf_tdata (abfd)->got = got;
4315 ppc64_elf_tdata (abfd)->relgot = relgot;
4316 return TRUE;
4317 }
4318
4319 /* Create the dynamic sections, and set up shortcuts. */
4320
4321 static bfd_boolean
4322 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4323 {
4324 struct ppc_link_hash_table *htab;
4325
4326 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4327 return FALSE;
4328
4329 htab = ppc_hash_table (info);
4330 if (htab == NULL)
4331 return FALSE;
4332
4333 if (!htab->got)
4334 htab->got = bfd_get_section_by_name (dynobj, ".got");
4335 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4336 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4337 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4338 if (!info->shared)
4339 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4340
4341 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4342 || (!info->shared && !htab->relbss))
4343 abort ();
4344
4345 return TRUE;
4346 }
4347
4348 /* Follow indirect and warning symbol links. */
4349
4350 static inline struct bfd_link_hash_entry *
4351 follow_link (struct bfd_link_hash_entry *h)
4352 {
4353 while (h->type == bfd_link_hash_indirect
4354 || h->type == bfd_link_hash_warning)
4355 h = h->u.i.link;
4356 return h;
4357 }
4358
4359 static inline struct elf_link_hash_entry *
4360 elf_follow_link (struct elf_link_hash_entry *h)
4361 {
4362 return (struct elf_link_hash_entry *) follow_link (&h->root);
4363 }
4364
4365 static inline struct ppc_link_hash_entry *
4366 ppc_follow_link (struct ppc_link_hash_entry *h)
4367 {
4368 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4369 }
4370
4371 /* Merge PLT info on FROM with that on TO. */
4372
4373 static void
4374 move_plt_plist (struct ppc_link_hash_entry *from,
4375 struct ppc_link_hash_entry *to)
4376 {
4377 if (from->elf.plt.plist != NULL)
4378 {
4379 if (to->elf.plt.plist != NULL)
4380 {
4381 struct plt_entry **entp;
4382 struct plt_entry *ent;
4383
4384 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4385 {
4386 struct plt_entry *dent;
4387
4388 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4389 if (dent->addend == ent->addend)
4390 {
4391 dent->plt.refcount += ent->plt.refcount;
4392 *entp = ent->next;
4393 break;
4394 }
4395 if (dent == NULL)
4396 entp = &ent->next;
4397 }
4398 *entp = to->elf.plt.plist;
4399 }
4400
4401 to->elf.plt.plist = from->elf.plt.plist;
4402 from->elf.plt.plist = NULL;
4403 }
4404 }
4405
4406 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4407
4408 static void
4409 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4410 struct elf_link_hash_entry *dir,
4411 struct elf_link_hash_entry *ind)
4412 {
4413 struct ppc_link_hash_entry *edir, *eind;
4414
4415 edir = (struct ppc_link_hash_entry *) dir;
4416 eind = (struct ppc_link_hash_entry *) ind;
4417
4418 edir->is_func |= eind->is_func;
4419 edir->is_func_descriptor |= eind->is_func_descriptor;
4420 edir->tls_mask |= eind->tls_mask;
4421 if (eind->oh != NULL)
4422 edir->oh = ppc_follow_link (eind->oh);
4423
4424 /* If called to transfer flags for a weakdef during processing
4425 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4426 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4427 if (!(ELIMINATE_COPY_RELOCS
4428 && eind->elf.root.type != bfd_link_hash_indirect
4429 && edir->elf.dynamic_adjusted))
4430 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4431
4432 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4433 edir->elf.ref_regular |= eind->elf.ref_regular;
4434 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4435 edir->elf.needs_plt |= eind->elf.needs_plt;
4436
4437 /* If we were called to copy over info for a weak sym, that's all. */
4438 if (eind->elf.root.type != bfd_link_hash_indirect)
4439 return;
4440
4441 /* Copy over any dynamic relocs we may have on the indirect sym. */
4442 if (eind->dyn_relocs != NULL)
4443 {
4444 if (edir->dyn_relocs != NULL)
4445 {
4446 struct elf_dyn_relocs **pp;
4447 struct elf_dyn_relocs *p;
4448
4449 /* Add reloc counts against the indirect sym to the direct sym
4450 list. Merge any entries against the same section. */
4451 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4452 {
4453 struct elf_dyn_relocs *q;
4454
4455 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4456 if (q->sec == p->sec)
4457 {
4458 q->pc_count += p->pc_count;
4459 q->count += p->count;
4460 *pp = p->next;
4461 break;
4462 }
4463 if (q == NULL)
4464 pp = &p->next;
4465 }
4466 *pp = edir->dyn_relocs;
4467 }
4468
4469 edir->dyn_relocs = eind->dyn_relocs;
4470 eind->dyn_relocs = NULL;
4471 }
4472
4473 /* Copy over got entries that we may have already seen to the
4474 symbol which just became indirect. */
4475 if (eind->elf.got.glist != NULL)
4476 {
4477 if (edir->elf.got.glist != NULL)
4478 {
4479 struct got_entry **entp;
4480 struct got_entry *ent;
4481
4482 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4483 {
4484 struct got_entry *dent;
4485
4486 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4487 if (dent->addend == ent->addend
4488 && dent->owner == ent->owner
4489 && dent->tls_type == ent->tls_type)
4490 {
4491 dent->got.refcount += ent->got.refcount;
4492 *entp = ent->next;
4493 break;
4494 }
4495 if (dent == NULL)
4496 entp = &ent->next;
4497 }
4498 *entp = edir->elf.got.glist;
4499 }
4500
4501 edir->elf.got.glist = eind->elf.got.glist;
4502 eind->elf.got.glist = NULL;
4503 }
4504
4505 /* And plt entries. */
4506 move_plt_plist (eind, edir);
4507
4508 if (eind->elf.dynindx != -1)
4509 {
4510 if (edir->elf.dynindx != -1)
4511 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4512 edir->elf.dynstr_index);
4513 edir->elf.dynindx = eind->elf.dynindx;
4514 edir->elf.dynstr_index = eind->elf.dynstr_index;
4515 eind->elf.dynindx = -1;
4516 eind->elf.dynstr_index = 0;
4517 }
4518 }
4519
4520 /* Find the function descriptor hash entry from the given function code
4521 hash entry FH. Link the entries via their OH fields. */
4522
4523 static struct ppc_link_hash_entry *
4524 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4525 {
4526 struct ppc_link_hash_entry *fdh = fh->oh;
4527
4528 if (fdh == NULL)
4529 {
4530 const char *fd_name = fh->elf.root.root.string + 1;
4531
4532 fdh = (struct ppc_link_hash_entry *)
4533 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4534 if (fdh == NULL)
4535 return fdh;
4536
4537 fdh->is_func_descriptor = 1;
4538 fdh->oh = fh;
4539 fh->is_func = 1;
4540 fh->oh = fdh;
4541 }
4542
4543 return ppc_follow_link (fdh);
4544 }
4545
4546 /* Make a fake function descriptor sym for the code sym FH. */
4547
4548 static struct ppc_link_hash_entry *
4549 make_fdh (struct bfd_link_info *info,
4550 struct ppc_link_hash_entry *fh)
4551 {
4552 bfd *abfd;
4553 asymbol *newsym;
4554 struct bfd_link_hash_entry *bh;
4555 struct ppc_link_hash_entry *fdh;
4556
4557 abfd = fh->elf.root.u.undef.abfd;
4558 newsym = bfd_make_empty_symbol (abfd);
4559 newsym->name = fh->elf.root.root.string + 1;
4560 newsym->section = bfd_und_section_ptr;
4561 newsym->value = 0;
4562 newsym->flags = BSF_WEAK;
4563
4564 bh = NULL;
4565 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4566 newsym->flags, newsym->section,
4567 newsym->value, NULL, FALSE, FALSE,
4568 &bh))
4569 return NULL;
4570
4571 fdh = (struct ppc_link_hash_entry *) bh;
4572 fdh->elf.non_elf = 0;
4573 fdh->fake = 1;
4574 fdh->is_func_descriptor = 1;
4575 fdh->oh = fh;
4576 fh->is_func = 1;
4577 fh->oh = fdh;
4578 return fdh;
4579 }
4580
4581 /* Fix function descriptor symbols defined in .opd sections to be
4582 function type. */
4583
4584 static bfd_boolean
4585 ppc64_elf_add_symbol_hook (bfd *ibfd,
4586 struct bfd_link_info *info,
4587 Elf_Internal_Sym *isym,
4588 const char **name ATTRIBUTE_UNUSED,
4589 flagword *flags ATTRIBUTE_UNUSED,
4590 asection **sec,
4591 bfd_vma *value ATTRIBUTE_UNUSED)
4592 {
4593 if ((ibfd->flags & DYNAMIC) == 0
4594 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4595 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4596
4597 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4598 {
4599 if ((ibfd->flags & DYNAMIC) == 0)
4600 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4601 }
4602 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4603 ;
4604 else if (*sec != NULL
4605 && strcmp ((*sec)->name, ".opd") == 0)
4606 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4607
4608 return TRUE;
4609 }
4610
4611 /* This function makes an old ABI object reference to ".bar" cause the
4612 inclusion of a new ABI object archive that defines "bar".
4613 NAME is a symbol defined in an archive. Return a symbol in the hash
4614 table that might be satisfied by the archive symbols. */
4615
4616 static struct elf_link_hash_entry *
4617 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4618 struct bfd_link_info *info,
4619 const char *name)
4620 {
4621 struct elf_link_hash_entry *h;
4622 char *dot_name;
4623 size_t len;
4624
4625 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4626 if (h != NULL
4627 /* Don't return this sym if it is a fake function descriptor
4628 created by add_symbol_adjust. */
4629 && !(h->root.type == bfd_link_hash_undefweak
4630 && ((struct ppc_link_hash_entry *) h)->fake))
4631 return h;
4632
4633 if (name[0] == '.')
4634 return h;
4635
4636 len = strlen (name);
4637 dot_name = bfd_alloc (abfd, len + 2);
4638 if (dot_name == NULL)
4639 return (struct elf_link_hash_entry *) 0 - 1;
4640 dot_name[0] = '.';
4641 memcpy (dot_name + 1, name, len + 1);
4642 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4643 bfd_release (abfd, dot_name);
4644 return h;
4645 }
4646
4647 /* This function satisfies all old ABI object references to ".bar" if a
4648 new ABI object defines "bar". Well, at least, undefined dot symbols
4649 are made weak. This stops later archive searches from including an
4650 object if we already have a function descriptor definition. It also
4651 prevents the linker complaining about undefined symbols.
4652 We also check and correct mismatched symbol visibility here. The
4653 most restrictive visibility of the function descriptor and the
4654 function entry symbol is used. */
4655
4656 static bfd_boolean
4657 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4658 {
4659 struct ppc_link_hash_table *htab;
4660 struct ppc_link_hash_entry *fdh;
4661
4662 if (eh->elf.root.type == bfd_link_hash_indirect)
4663 return TRUE;
4664
4665 if (eh->elf.root.type == bfd_link_hash_warning)
4666 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4667
4668 if (eh->elf.root.root.string[0] != '.')
4669 abort ();
4670
4671 htab = ppc_hash_table (info);
4672 if (htab == NULL)
4673 return FALSE;
4674
4675 fdh = lookup_fdh (eh, htab);
4676 if (fdh == NULL)
4677 {
4678 if (!info->relocatable
4679 && (eh->elf.root.type == bfd_link_hash_undefined
4680 || eh->elf.root.type == bfd_link_hash_undefweak)
4681 && eh->elf.ref_regular)
4682 {
4683 /* Make an undefweak function descriptor sym, which is enough to
4684 pull in an --as-needed shared lib, but won't cause link
4685 errors. Archives are handled elsewhere. */
4686 fdh = make_fdh (info, eh);
4687 if (fdh == NULL)
4688 return FALSE;
4689 fdh->elf.ref_regular = 1;
4690 }
4691 }
4692 else
4693 {
4694 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4695 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4696 if (entry_vis < descr_vis)
4697 fdh->elf.other += entry_vis - descr_vis;
4698 else if (entry_vis > descr_vis)
4699 eh->elf.other += descr_vis - entry_vis;
4700
4701 if ((fdh->elf.root.type == bfd_link_hash_defined
4702 || fdh->elf.root.type == bfd_link_hash_defweak)
4703 && eh->elf.root.type == bfd_link_hash_undefined)
4704 {
4705 eh->elf.root.type = bfd_link_hash_undefweak;
4706 eh->was_undefined = 1;
4707 htab->twiddled_syms = 1;
4708 }
4709 }
4710
4711 return TRUE;
4712 }
4713
4714 /* Process list of dot-symbols we made in link_hash_newfunc. */
4715
4716 static bfd_boolean
4717 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4718 {
4719 struct ppc_link_hash_table *htab;
4720 struct ppc_link_hash_entry **p, *eh;
4721
4722 if (!is_ppc64_elf (info->output_bfd))
4723 return TRUE;
4724 htab = ppc_hash_table (info);
4725 if (htab == NULL)
4726 return FALSE;
4727
4728 if (is_ppc64_elf (ibfd))
4729 {
4730 p = &htab->dot_syms;
4731 while ((eh = *p) != NULL)
4732 {
4733 *p = NULL;
4734 if (!add_symbol_adjust (eh, info))
4735 return FALSE;
4736 p = &eh->u.next_dot_sym;
4737 }
4738 }
4739
4740 /* Clear the list for non-ppc64 input files. */
4741 p = &htab->dot_syms;
4742 while ((eh = *p) != NULL)
4743 {
4744 *p = NULL;
4745 p = &eh->u.next_dot_sym;
4746 }
4747
4748 /* We need to fix the undefs list for any syms we have twiddled to
4749 undef_weak. */
4750 if (htab->twiddled_syms)
4751 {
4752 bfd_link_repair_undef_list (&htab->elf.root);
4753 htab->twiddled_syms = 0;
4754 }
4755 return TRUE;
4756 }
4757
4758 /* Undo hash table changes when an --as-needed input file is determined
4759 not to be needed. */
4760
4761 static bfd_boolean
4762 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4763 struct bfd_link_info *info)
4764 {
4765 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4766
4767 if (htab == NULL)
4768 return FALSE;
4769
4770 htab->dot_syms = NULL;
4771 return TRUE;
4772 }
4773
4774 /* If --just-symbols against a final linked binary, then assume we need
4775 toc adjusting stubs when calling functions defined there. */
4776
4777 static void
4778 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4779 {
4780 if ((sec->flags & SEC_CODE) != 0
4781 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4782 && is_ppc64_elf (sec->owner))
4783 {
4784 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4785 if (got != NULL
4786 && got->size >= elf_backend_got_header_size
4787 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4788 sec->has_toc_reloc = 1;
4789 }
4790 _bfd_elf_link_just_syms (sec, info);
4791 }
4792
4793 static struct plt_entry **
4794 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4795 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4796 {
4797 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4798 struct plt_entry **local_plt;
4799 unsigned char *local_got_tls_masks;
4800
4801 if (local_got_ents == NULL)
4802 {
4803 bfd_size_type size = symtab_hdr->sh_info;
4804
4805 size *= (sizeof (*local_got_ents)
4806 + sizeof (*local_plt)
4807 + sizeof (*local_got_tls_masks));
4808 local_got_ents = bfd_zalloc (abfd, size);
4809 if (local_got_ents == NULL)
4810 return NULL;
4811 elf_local_got_ents (abfd) = local_got_ents;
4812 }
4813
4814 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4815 {
4816 struct got_entry *ent;
4817
4818 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4819 if (ent->addend == r_addend
4820 && ent->owner == abfd
4821 && ent->tls_type == tls_type)
4822 break;
4823 if (ent == NULL)
4824 {
4825 bfd_size_type amt = sizeof (*ent);
4826 ent = bfd_alloc (abfd, amt);
4827 if (ent == NULL)
4828 return FALSE;
4829 ent->next = local_got_ents[r_symndx];
4830 ent->addend = r_addend;
4831 ent->owner = abfd;
4832 ent->tls_type = tls_type;
4833 ent->is_indirect = FALSE;
4834 ent->got.refcount = 0;
4835 local_got_ents[r_symndx] = ent;
4836 }
4837 ent->got.refcount += 1;
4838 }
4839
4840 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4841 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4842 local_got_tls_masks[r_symndx] |= tls_type;
4843
4844 return local_plt + r_symndx;
4845 }
4846
4847 static bfd_boolean
4848 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4849 {
4850 struct plt_entry *ent;
4851
4852 for (ent = *plist; ent != NULL; ent = ent->next)
4853 if (ent->addend == addend)
4854 break;
4855 if (ent == NULL)
4856 {
4857 bfd_size_type amt = sizeof (*ent);
4858 ent = bfd_alloc (abfd, amt);
4859 if (ent == NULL)
4860 return FALSE;
4861 ent->next = *plist;
4862 ent->addend = addend;
4863 ent->plt.refcount = 0;
4864 *plist = ent;
4865 }
4866 ent->plt.refcount += 1;
4867 return TRUE;
4868 }
4869
4870 static bfd_boolean
4871 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4872 {
4873 return (r_type == R_PPC64_REL24
4874 || r_type == R_PPC64_REL14
4875 || r_type == R_PPC64_REL14_BRTAKEN
4876 || r_type == R_PPC64_REL14_BRNTAKEN
4877 || r_type == R_PPC64_ADDR24
4878 || r_type == R_PPC64_ADDR14
4879 || r_type == R_PPC64_ADDR14_BRTAKEN
4880 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4881 }
4882
4883 /* Look through the relocs for a section during the first phase, and
4884 calculate needed space in the global offset table, procedure
4885 linkage table, and dynamic reloc sections. */
4886
4887 static bfd_boolean
4888 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4889 asection *sec, const Elf_Internal_Rela *relocs)
4890 {
4891 struct ppc_link_hash_table *htab;
4892 Elf_Internal_Shdr *symtab_hdr;
4893 struct elf_link_hash_entry **sym_hashes;
4894 const Elf_Internal_Rela *rel;
4895 const Elf_Internal_Rela *rel_end;
4896 asection *sreloc;
4897 asection **opd_sym_map;
4898 struct elf_link_hash_entry *tga, *dottga;
4899
4900 if (info->relocatable)
4901 return TRUE;
4902
4903 /* Don't do anything special with non-loaded, non-alloced sections.
4904 In particular, any relocs in such sections should not affect GOT
4905 and PLT reference counting (ie. we don't allow them to create GOT
4906 or PLT entries), there's no possibility or desire to optimize TLS
4907 relocs, and there's not much point in propagating relocs to shared
4908 libs that the dynamic linker won't relocate. */
4909 if ((sec->flags & SEC_ALLOC) == 0)
4910 return TRUE;
4911
4912 BFD_ASSERT (is_ppc64_elf (abfd));
4913
4914 htab = ppc_hash_table (info);
4915 if (htab == NULL)
4916 return FALSE;
4917
4918 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4919 FALSE, FALSE, TRUE);
4920 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4921 FALSE, FALSE, TRUE);
4922 symtab_hdr = &elf_symtab_hdr (abfd);
4923 sym_hashes = elf_sym_hashes (abfd);
4924 sreloc = NULL;
4925 opd_sym_map = NULL;
4926 if (strcmp (sec->name, ".opd") == 0)
4927 {
4928 /* Garbage collection needs some extra help with .opd sections.
4929 We don't want to necessarily keep everything referenced by
4930 relocs in .opd, as that would keep all functions. Instead,
4931 if we reference an .opd symbol (a function descriptor), we
4932 want to keep the function code symbol's section. This is
4933 easy for global symbols, but for local syms we need to keep
4934 information about the associated function section. */
4935 bfd_size_type amt;
4936
4937 amt = sec->size * sizeof (*opd_sym_map) / 8;
4938 opd_sym_map = bfd_zalloc (abfd, amt);
4939 if (opd_sym_map == NULL)
4940 return FALSE;
4941 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4942 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4943 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4944 }
4945
4946 if (htab->sfpr == NULL
4947 && !create_linkage_sections (htab->elf.dynobj, info))
4948 return FALSE;
4949
4950 rel_end = relocs + sec->reloc_count;
4951 for (rel = relocs; rel < rel_end; rel++)
4952 {
4953 unsigned long r_symndx;
4954 struct elf_link_hash_entry *h;
4955 enum elf_ppc64_reloc_type r_type;
4956 int tls_type;
4957 struct _ppc64_elf_section_data *ppc64_sec;
4958 struct plt_entry **ifunc;
4959
4960 r_symndx = ELF64_R_SYM (rel->r_info);
4961 if (r_symndx < symtab_hdr->sh_info)
4962 h = NULL;
4963 else
4964 {
4965 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4966 h = elf_follow_link (h);
4967 }
4968
4969 tls_type = 0;
4970 ifunc = NULL;
4971 if (h != NULL)
4972 {
4973 if (h->type == STT_GNU_IFUNC)
4974 {
4975 h->needs_plt = 1;
4976 ifunc = &h->plt.plist;
4977 }
4978 }
4979 else
4980 {
4981 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4982 abfd, r_symndx);
4983 if (isym == NULL)
4984 return FALSE;
4985
4986 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4987 {
4988 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4989 rel->r_addend, PLT_IFUNC);
4990 if (ifunc == NULL)
4991 return FALSE;
4992 }
4993 }
4994 r_type = ELF64_R_TYPE (rel->r_info);
4995 if (is_branch_reloc (r_type))
4996 {
4997 if (h != NULL && (h == tga || h == dottga))
4998 {
4999 if (rel != relocs
5000 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5001 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5002 /* We have a new-style __tls_get_addr call with a marker
5003 reloc. */
5004 ;
5005 else
5006 /* Mark this section as having an old-style call. */
5007 sec->has_tls_get_addr_call = 1;
5008 }
5009
5010 /* STT_GNU_IFUNC symbols must have a PLT entry. */
5011 if (ifunc != NULL
5012 && !update_plt_info (abfd, ifunc, rel->r_addend))
5013 return FALSE;
5014 }
5015
5016 switch (r_type)
5017 {
5018 case R_PPC64_TLSGD:
5019 case R_PPC64_TLSLD:
5020 /* These special tls relocs tie a call to __tls_get_addr with
5021 its parameter symbol. */
5022 break;
5023
5024 case R_PPC64_GOT_TLSLD16:
5025 case R_PPC64_GOT_TLSLD16_LO:
5026 case R_PPC64_GOT_TLSLD16_HI:
5027 case R_PPC64_GOT_TLSLD16_HA:
5028 tls_type = TLS_TLS | TLS_LD;
5029 goto dogottls;
5030
5031 case R_PPC64_GOT_TLSGD16:
5032 case R_PPC64_GOT_TLSGD16_LO:
5033 case R_PPC64_GOT_TLSGD16_HI:
5034 case R_PPC64_GOT_TLSGD16_HA:
5035 tls_type = TLS_TLS | TLS_GD;
5036 goto dogottls;
5037
5038 case R_PPC64_GOT_TPREL16_DS:
5039 case R_PPC64_GOT_TPREL16_LO_DS:
5040 case R_PPC64_GOT_TPREL16_HI:
5041 case R_PPC64_GOT_TPREL16_HA:
5042 if (!info->executable)
5043 info->flags |= DF_STATIC_TLS;
5044 tls_type = TLS_TLS | TLS_TPREL;
5045 goto dogottls;
5046
5047 case R_PPC64_GOT_DTPREL16_DS:
5048 case R_PPC64_GOT_DTPREL16_LO_DS:
5049 case R_PPC64_GOT_DTPREL16_HI:
5050 case R_PPC64_GOT_DTPREL16_HA:
5051 tls_type = TLS_TLS | TLS_DTPREL;
5052 dogottls:
5053 sec->has_tls_reloc = 1;
5054 /* Fall thru */
5055
5056 case R_PPC64_GOT16:
5057 case R_PPC64_GOT16_DS:
5058 case R_PPC64_GOT16_HA:
5059 case R_PPC64_GOT16_HI:
5060 case R_PPC64_GOT16_LO:
5061 case R_PPC64_GOT16_LO_DS:
5062 /* This symbol requires a global offset table entry. */
5063 sec->has_toc_reloc = 1;
5064 if (r_type == R_PPC64_GOT_TLSLD16
5065 || r_type == R_PPC64_GOT_TLSGD16
5066 || r_type == R_PPC64_GOT_TPREL16_DS
5067 || r_type == R_PPC64_GOT_DTPREL16_DS
5068 || r_type == R_PPC64_GOT16
5069 || r_type == R_PPC64_GOT16_DS)
5070 {
5071 htab->do_multi_toc = 1;
5072 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5073 }
5074
5075 if (ppc64_elf_tdata (abfd)->got == NULL
5076 && !create_got_section (abfd, info))
5077 return FALSE;
5078
5079 if (h != NULL)
5080 {
5081 struct ppc_link_hash_entry *eh;
5082 struct got_entry *ent;
5083
5084 eh = (struct ppc_link_hash_entry *) h;
5085 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5086 if (ent->addend == rel->r_addend
5087 && ent->owner == abfd
5088 && ent->tls_type == tls_type)
5089 break;
5090 if (ent == NULL)
5091 {
5092 bfd_size_type amt = sizeof (*ent);
5093 ent = bfd_alloc (abfd, amt);
5094 if (ent == NULL)
5095 return FALSE;
5096 ent->next = eh->elf.got.glist;
5097 ent->addend = rel->r_addend;
5098 ent->owner = abfd;
5099 ent->tls_type = tls_type;
5100 ent->is_indirect = FALSE;
5101 ent->got.refcount = 0;
5102 eh->elf.got.glist = ent;
5103 }
5104 ent->got.refcount += 1;
5105 eh->tls_mask |= tls_type;
5106 }
5107 else
5108 /* This is a global offset table entry for a local symbol. */
5109 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5110 rel->r_addend, tls_type))
5111 return FALSE;
5112 break;
5113
5114 case R_PPC64_PLT16_HA:
5115 case R_PPC64_PLT16_HI:
5116 case R_PPC64_PLT16_LO:
5117 case R_PPC64_PLT32:
5118 case R_PPC64_PLT64:
5119 /* This symbol requires a procedure linkage table entry. We
5120 actually build the entry in adjust_dynamic_symbol,
5121 because this might be a case of linking PIC code without
5122 linking in any dynamic objects, in which case we don't
5123 need to generate a procedure linkage table after all. */
5124 if (h == NULL)
5125 {
5126 /* It does not make sense to have a procedure linkage
5127 table entry for a local symbol. */
5128 bfd_set_error (bfd_error_bad_value);
5129 return FALSE;
5130 }
5131 else
5132 {
5133 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5134 return FALSE;
5135 h->needs_plt = 1;
5136 if (h->root.root.string[0] == '.'
5137 && h->root.root.string[1] != '\0')
5138 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5139 }
5140 break;
5141
5142 /* The following relocations don't need to propagate the
5143 relocation if linking a shared object since they are
5144 section relative. */
5145 case R_PPC64_SECTOFF:
5146 case R_PPC64_SECTOFF_LO:
5147 case R_PPC64_SECTOFF_HI:
5148 case R_PPC64_SECTOFF_HA:
5149 case R_PPC64_SECTOFF_DS:
5150 case R_PPC64_SECTOFF_LO_DS:
5151 case R_PPC64_DTPREL16:
5152 case R_PPC64_DTPREL16_LO:
5153 case R_PPC64_DTPREL16_HI:
5154 case R_PPC64_DTPREL16_HA:
5155 case R_PPC64_DTPREL16_DS:
5156 case R_PPC64_DTPREL16_LO_DS:
5157 case R_PPC64_DTPREL16_HIGHER:
5158 case R_PPC64_DTPREL16_HIGHERA:
5159 case R_PPC64_DTPREL16_HIGHEST:
5160 case R_PPC64_DTPREL16_HIGHESTA:
5161 break;
5162
5163 /* Nor do these. */
5164 case R_PPC64_REL16:
5165 case R_PPC64_REL16_LO:
5166 case R_PPC64_REL16_HI:
5167 case R_PPC64_REL16_HA:
5168 break;
5169
5170 case R_PPC64_TOC16:
5171 case R_PPC64_TOC16_DS:
5172 htab->do_multi_toc = 1;
5173 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5174 case R_PPC64_TOC16_LO:
5175 case R_PPC64_TOC16_HI:
5176 case R_PPC64_TOC16_HA:
5177 case R_PPC64_TOC16_LO_DS:
5178 sec->has_toc_reloc = 1;
5179 break;
5180
5181 /* This relocation describes the C++ object vtable hierarchy.
5182 Reconstruct it for later use during GC. */
5183 case R_PPC64_GNU_VTINHERIT:
5184 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5185 return FALSE;
5186 break;
5187
5188 /* This relocation describes which C++ vtable entries are actually
5189 used. Record for later use during GC. */
5190 case R_PPC64_GNU_VTENTRY:
5191 BFD_ASSERT (h != NULL);
5192 if (h != NULL
5193 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5194 return FALSE;
5195 break;
5196
5197 case R_PPC64_REL14:
5198 case R_PPC64_REL14_BRTAKEN:
5199 case R_PPC64_REL14_BRNTAKEN:
5200 {
5201 asection *dest = NULL;
5202
5203 /* Heuristic: If jumping outside our section, chances are
5204 we are going to need a stub. */
5205 if (h != NULL)
5206 {
5207 /* If the sym is weak it may be overridden later, so
5208 don't assume we know where a weak sym lives. */
5209 if (h->root.type == bfd_link_hash_defined)
5210 dest = h->root.u.def.section;
5211 }
5212 else
5213 {
5214 Elf_Internal_Sym *isym;
5215
5216 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5217 abfd, r_symndx);
5218 if (isym == NULL)
5219 return FALSE;
5220
5221 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5222 }
5223
5224 if (dest != sec)
5225 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5226 }
5227 /* Fall through. */
5228
5229 case R_PPC64_REL24:
5230 if (h != NULL && ifunc == NULL)
5231 {
5232 /* We may need a .plt entry if the function this reloc
5233 refers to is in a shared lib. */
5234 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5235 return FALSE;
5236 h->needs_plt = 1;
5237 if (h->root.root.string[0] == '.'
5238 && h->root.root.string[1] != '\0')
5239 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5240 if (h == tga || h == dottga)
5241 sec->has_tls_reloc = 1;
5242 }
5243 break;
5244
5245 case R_PPC64_TPREL64:
5246 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5247 if (!info->executable)
5248 info->flags |= DF_STATIC_TLS;
5249 goto dotlstoc;
5250
5251 case R_PPC64_DTPMOD64:
5252 if (rel + 1 < rel_end
5253 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5254 && rel[1].r_offset == rel->r_offset + 8)
5255 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5256 else
5257 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5258 goto dotlstoc;
5259
5260 case R_PPC64_DTPREL64:
5261 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5262 if (rel != relocs
5263 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5264 && rel[-1].r_offset == rel->r_offset - 8)
5265 /* This is the second reloc of a dtpmod, dtprel pair.
5266 Don't mark with TLS_DTPREL. */
5267 goto dodyn;
5268
5269 dotlstoc:
5270 sec->has_tls_reloc = 1;
5271 if (h != NULL)
5272 {
5273 struct ppc_link_hash_entry *eh;
5274 eh = (struct ppc_link_hash_entry *) h;
5275 eh->tls_mask |= tls_type;
5276 }
5277 else
5278 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5279 rel->r_addend, tls_type))
5280 return FALSE;
5281
5282 ppc64_sec = ppc64_elf_section_data (sec);
5283 if (ppc64_sec->sec_type != sec_toc)
5284 {
5285 bfd_size_type amt;
5286
5287 /* One extra to simplify get_tls_mask. */
5288 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5289 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5290 if (ppc64_sec->u.toc.symndx == NULL)
5291 return FALSE;
5292 amt = sec->size * sizeof (bfd_vma) / 8;
5293 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5294 if (ppc64_sec->u.toc.add == NULL)
5295 return FALSE;
5296 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5297 ppc64_sec->sec_type = sec_toc;
5298 }
5299 BFD_ASSERT (rel->r_offset % 8 == 0);
5300 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5301 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5302
5303 /* Mark the second slot of a GD or LD entry.
5304 -1 to indicate GD and -2 to indicate LD. */
5305 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5306 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5307 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5308 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5309 goto dodyn;
5310
5311 case R_PPC64_TPREL16:
5312 case R_PPC64_TPREL16_LO:
5313 case R_PPC64_TPREL16_HI:
5314 case R_PPC64_TPREL16_HA:
5315 case R_PPC64_TPREL16_DS:
5316 case R_PPC64_TPREL16_LO_DS:
5317 case R_PPC64_TPREL16_HIGHER:
5318 case R_PPC64_TPREL16_HIGHERA:
5319 case R_PPC64_TPREL16_HIGHEST:
5320 case R_PPC64_TPREL16_HIGHESTA:
5321 if (info->shared)
5322 {
5323 if (!info->executable)
5324 info->flags |= DF_STATIC_TLS;
5325 goto dodyn;
5326 }
5327 break;
5328
5329 case R_PPC64_ADDR64:
5330 if (opd_sym_map != NULL
5331 && rel + 1 < rel_end
5332 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5333 {
5334 if (h != NULL)
5335 {
5336 if (h->root.root.string[0] == '.'
5337 && h->root.root.string[1] != 0
5338 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5339 ;
5340 else
5341 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5342 }
5343 else
5344 {
5345 asection *s;
5346 Elf_Internal_Sym *isym;
5347
5348 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5349 abfd, r_symndx);
5350 if (isym == NULL)
5351 return FALSE;
5352
5353 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5354 if (s != NULL && s != sec)
5355 opd_sym_map[rel->r_offset / 8] = s;
5356 }
5357 }
5358 /* Fall through. */
5359
5360 case R_PPC64_REL30:
5361 case R_PPC64_REL32:
5362 case R_PPC64_REL64:
5363 case R_PPC64_ADDR14:
5364 case R_PPC64_ADDR14_BRNTAKEN:
5365 case R_PPC64_ADDR14_BRTAKEN:
5366 case R_PPC64_ADDR16:
5367 case R_PPC64_ADDR16_DS:
5368 case R_PPC64_ADDR16_HA:
5369 case R_PPC64_ADDR16_HI:
5370 case R_PPC64_ADDR16_HIGHER:
5371 case R_PPC64_ADDR16_HIGHERA:
5372 case R_PPC64_ADDR16_HIGHEST:
5373 case R_PPC64_ADDR16_HIGHESTA:
5374 case R_PPC64_ADDR16_LO:
5375 case R_PPC64_ADDR16_LO_DS:
5376 case R_PPC64_ADDR24:
5377 case R_PPC64_ADDR32:
5378 case R_PPC64_UADDR16:
5379 case R_PPC64_UADDR32:
5380 case R_PPC64_UADDR64:
5381 case R_PPC64_TOC:
5382 if (h != NULL && !info->shared)
5383 /* We may need a copy reloc. */
5384 h->non_got_ref = 1;
5385
5386 /* Don't propagate .opd relocs. */
5387 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5388 break;
5389
5390 /* If we are creating a shared library, and this is a reloc
5391 against a global symbol, or a non PC relative reloc
5392 against a local symbol, then we need to copy the reloc
5393 into the shared library. However, if we are linking with
5394 -Bsymbolic, we do not need to copy a reloc against a
5395 global symbol which is defined in an object we are
5396 including in the link (i.e., DEF_REGULAR is set). At
5397 this point we have not seen all the input files, so it is
5398 possible that DEF_REGULAR is not set now but will be set
5399 later (it is never cleared). In case of a weak definition,
5400 DEF_REGULAR may be cleared later by a strong definition in
5401 a shared library. We account for that possibility below by
5402 storing information in the dyn_relocs field of the hash
5403 table entry. A similar situation occurs when creating
5404 shared libraries and symbol visibility changes render the
5405 symbol local.
5406
5407 If on the other hand, we are creating an executable, we
5408 may need to keep relocations for symbols satisfied by a
5409 dynamic library if we manage to avoid copy relocs for the
5410 symbol. */
5411 dodyn:
5412 if ((info->shared
5413 && (must_be_dyn_reloc (info, r_type)
5414 || (h != NULL
5415 && (! info->symbolic
5416 || h->root.type == bfd_link_hash_defweak
5417 || !h->def_regular))))
5418 || (ELIMINATE_COPY_RELOCS
5419 && !info->shared
5420 && h != NULL
5421 && (h->root.type == bfd_link_hash_defweak
5422 || !h->def_regular))
5423 || (!info->shared
5424 && ifunc != NULL))
5425 {
5426 struct elf_dyn_relocs *p;
5427 struct elf_dyn_relocs **head;
5428
5429 /* We must copy these reloc types into the output file.
5430 Create a reloc section in dynobj and make room for
5431 this reloc. */
5432 if (sreloc == NULL)
5433 {
5434 sreloc = _bfd_elf_make_dynamic_reloc_section
5435 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5436
5437 if (sreloc == NULL)
5438 return FALSE;
5439 }
5440
5441 /* If this is a global symbol, we count the number of
5442 relocations we need for this symbol. */
5443 if (h != NULL)
5444 {
5445 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5446 }
5447 else
5448 {
5449 /* Track dynamic relocs needed for local syms too.
5450 We really need local syms available to do this
5451 easily. Oh well. */
5452 asection *s;
5453 void *vpp;
5454 Elf_Internal_Sym *isym;
5455
5456 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5457 abfd, r_symndx);
5458 if (isym == NULL)
5459 return FALSE;
5460
5461 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5462 if (s == NULL)
5463 s = sec;
5464
5465 vpp = &elf_section_data (s)->local_dynrel;
5466 head = (struct elf_dyn_relocs **) vpp;
5467 }
5468
5469 p = *head;
5470 if (p == NULL || p->sec != sec)
5471 {
5472 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5473 if (p == NULL)
5474 return FALSE;
5475 p->next = *head;
5476 *head = p;
5477 p->sec = sec;
5478 p->count = 0;
5479 p->pc_count = 0;
5480 }
5481
5482 p->count += 1;
5483 if (!must_be_dyn_reloc (info, r_type))
5484 p->pc_count += 1;
5485 }
5486 break;
5487
5488 default:
5489 break;
5490 }
5491 }
5492
5493 return TRUE;
5494 }
5495
5496 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5497 of the code entry point, and its section. */
5498
5499 static bfd_vma
5500 opd_entry_value (asection *opd_sec,
5501 bfd_vma offset,
5502 asection **code_sec,
5503 bfd_vma *code_off)
5504 {
5505 bfd *opd_bfd = opd_sec->owner;
5506 Elf_Internal_Rela *relocs;
5507 Elf_Internal_Rela *lo, *hi, *look;
5508 bfd_vma val;
5509
5510 /* No relocs implies we are linking a --just-symbols object. */
5511 if (opd_sec->reloc_count == 0)
5512 {
5513 char buf[8];
5514
5515 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5516 return (bfd_vma) -1;
5517
5518 val = bfd_get_64 (opd_bfd, buf);
5519 if (code_sec != NULL)
5520 {
5521 asection *sec, *likely = NULL;
5522 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5523 if (sec->vma <= val
5524 && (sec->flags & SEC_LOAD) != 0
5525 && (sec->flags & SEC_ALLOC) != 0)
5526 likely = sec;
5527 if (likely != NULL)
5528 {
5529 *code_sec = likely;
5530 if (code_off != NULL)
5531 *code_off = val - likely->vma;
5532 }
5533 }
5534 return val;
5535 }
5536
5537 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5538
5539 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5540 if (relocs == NULL)
5541 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5542
5543 /* Go find the opd reloc at the sym address. */
5544 lo = relocs;
5545 BFD_ASSERT (lo != NULL);
5546 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5547 val = (bfd_vma) -1;
5548 while (lo < hi)
5549 {
5550 look = lo + (hi - lo) / 2;
5551 if (look->r_offset < offset)
5552 lo = look + 1;
5553 else if (look->r_offset > offset)
5554 hi = look;
5555 else
5556 {
5557 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5558
5559 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5560 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5561 {
5562 unsigned long symndx = ELF64_R_SYM (look->r_info);
5563 asection *sec;
5564
5565 if (symndx < symtab_hdr->sh_info)
5566 {
5567 Elf_Internal_Sym *sym;
5568
5569 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5570 if (sym == NULL)
5571 {
5572 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5573 symtab_hdr->sh_info,
5574 0, NULL, NULL, NULL);
5575 if (sym == NULL)
5576 break;
5577 symtab_hdr->contents = (bfd_byte *) sym;
5578 }
5579
5580 sym += symndx;
5581 val = sym->st_value;
5582 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5583 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5584 }
5585 else
5586 {
5587 struct elf_link_hash_entry **sym_hashes;
5588 struct elf_link_hash_entry *rh;
5589
5590 sym_hashes = elf_sym_hashes (opd_bfd);
5591 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5592 rh = elf_follow_link (rh);
5593 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5594 || rh->root.type == bfd_link_hash_defweak);
5595 val = rh->root.u.def.value;
5596 sec = rh->root.u.def.section;
5597 }
5598 val += look->r_addend;
5599 if (code_off != NULL)
5600 *code_off = val;
5601 if (code_sec != NULL)
5602 *code_sec = sec;
5603 if (sec != NULL && sec->output_section != NULL)
5604 val += sec->output_section->vma + sec->output_offset;
5605 }
5606 break;
5607 }
5608 }
5609
5610 return val;
5611 }
5612
5613 /* Return true if symbol is defined in a regular object file. */
5614
5615 static bfd_boolean
5616 is_static_defined (struct elf_link_hash_entry *h)
5617 {
5618 return ((h->root.type == bfd_link_hash_defined
5619 || h->root.type == bfd_link_hash_defweak)
5620 && h->root.u.def.section != NULL
5621 && h->root.u.def.section->output_section != NULL);
5622 }
5623
5624 /* If FDH is a function descriptor symbol, return the associated code
5625 entry symbol if it is defined. Return NULL otherwise. */
5626
5627 static struct ppc_link_hash_entry *
5628 defined_code_entry (struct ppc_link_hash_entry *fdh)
5629 {
5630 if (fdh->is_func_descriptor)
5631 {
5632 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5633 if (fh->elf.root.type == bfd_link_hash_defined
5634 || fh->elf.root.type == bfd_link_hash_defweak)
5635 return fh;
5636 }
5637 return NULL;
5638 }
5639
5640 /* If FH is a function code entry symbol, return the associated
5641 function descriptor symbol if it is defined. Return NULL otherwise. */
5642
5643 static struct ppc_link_hash_entry *
5644 defined_func_desc (struct ppc_link_hash_entry *fh)
5645 {
5646 if (fh->oh != NULL
5647 && fh->oh->is_func_descriptor)
5648 {
5649 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5650 if (fdh->elf.root.type == bfd_link_hash_defined
5651 || fdh->elf.root.type == bfd_link_hash_defweak)
5652 return fdh;
5653 }
5654 return NULL;
5655 }
5656
5657 /* Mark all our entry sym sections, both opd and code section. */
5658
5659 static void
5660 ppc64_elf_gc_keep (struct bfd_link_info *info)
5661 {
5662 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5663 struct bfd_sym_chain *sym;
5664
5665 if (htab == NULL)
5666 return;
5667
5668 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5669 {
5670 struct ppc_link_hash_entry *eh, *fh;
5671 asection *sec;
5672
5673 eh = (struct ppc_link_hash_entry *)
5674 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5675 if (eh == NULL)
5676 continue;
5677 if (eh->elf.root.type != bfd_link_hash_defined
5678 && eh->elf.root.type != bfd_link_hash_defweak)
5679 continue;
5680
5681 fh = defined_code_entry (eh);
5682 if (fh != NULL)
5683 {
5684 sec = fh->elf.root.u.def.section;
5685 sec->flags |= SEC_KEEP;
5686 }
5687 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5688 && opd_entry_value (eh->elf.root.u.def.section,
5689 eh->elf.root.u.def.value,
5690 &sec, NULL) != (bfd_vma) -1)
5691 sec->flags |= SEC_KEEP;
5692
5693 sec = eh->elf.root.u.def.section;
5694 sec->flags |= SEC_KEEP;
5695 }
5696 }
5697
5698 /* Mark sections containing dynamically referenced symbols. When
5699 building shared libraries, we must assume that any visible symbol is
5700 referenced. */
5701
5702 static bfd_boolean
5703 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5704 {
5705 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5706 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5707 struct ppc_link_hash_entry *fdh;
5708
5709 /* Dynamic linking info is on the func descriptor sym. */
5710 fdh = defined_func_desc (eh);
5711 if (fdh != NULL)
5712 eh = fdh;
5713
5714 if ((eh->elf.root.type == bfd_link_hash_defined
5715 || eh->elf.root.type == bfd_link_hash_defweak)
5716 && (eh->elf.ref_dynamic
5717 || (!info->executable
5718 && eh->elf.def_regular
5719 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5720 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5721 {
5722 asection *code_sec;
5723 struct ppc_link_hash_entry *fh;
5724
5725 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5726
5727 /* Function descriptor syms cause the associated
5728 function code sym section to be marked. */
5729 fh = defined_code_entry (eh);
5730 if (fh != NULL)
5731 {
5732 code_sec = fh->elf.root.u.def.section;
5733 code_sec->flags |= SEC_KEEP;
5734 }
5735 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5736 && opd_entry_value (eh->elf.root.u.def.section,
5737 eh->elf.root.u.def.value,
5738 &code_sec, NULL) != (bfd_vma) -1)
5739 code_sec->flags |= SEC_KEEP;
5740 }
5741
5742 return TRUE;
5743 }
5744
5745 /* Return the section that should be marked against GC for a given
5746 relocation. */
5747
5748 static asection *
5749 ppc64_elf_gc_mark_hook (asection *sec,
5750 struct bfd_link_info *info,
5751 Elf_Internal_Rela *rel,
5752 struct elf_link_hash_entry *h,
5753 Elf_Internal_Sym *sym)
5754 {
5755 asection *rsec;
5756
5757 /* Syms return NULL if we're marking .opd, so we avoid marking all
5758 function sections, as all functions are referenced in .opd. */
5759 rsec = NULL;
5760 if (get_opd_info (sec) != NULL)
5761 return rsec;
5762
5763 if (h != NULL)
5764 {
5765 enum elf_ppc64_reloc_type r_type;
5766 struct ppc_link_hash_entry *eh, *fh, *fdh;
5767
5768 r_type = ELF64_R_TYPE (rel->r_info);
5769 switch (r_type)
5770 {
5771 case R_PPC64_GNU_VTINHERIT:
5772 case R_PPC64_GNU_VTENTRY:
5773 break;
5774
5775 default:
5776 switch (h->root.type)
5777 {
5778 case bfd_link_hash_defined:
5779 case bfd_link_hash_defweak:
5780 eh = (struct ppc_link_hash_entry *) h;
5781 fdh = defined_func_desc (eh);
5782 if (fdh != NULL)
5783 eh = fdh;
5784
5785 /* Function descriptor syms cause the associated
5786 function code sym section to be marked. */
5787 fh = defined_code_entry (eh);
5788 if (fh != NULL)
5789 {
5790 /* They also mark their opd section. */
5791 eh->elf.root.u.def.section->gc_mark = 1;
5792
5793 rsec = fh->elf.root.u.def.section;
5794 }
5795 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5796 && opd_entry_value (eh->elf.root.u.def.section,
5797 eh->elf.root.u.def.value,
5798 &rsec, NULL) != (bfd_vma) -1)
5799 eh->elf.root.u.def.section->gc_mark = 1;
5800 else
5801 rsec = h->root.u.def.section;
5802 break;
5803
5804 case bfd_link_hash_common:
5805 rsec = h->root.u.c.p->section;
5806 break;
5807
5808 default:
5809 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5810 }
5811 }
5812 }
5813 else
5814 {
5815 struct _opd_sec_data *opd;
5816
5817 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5818 opd = get_opd_info (rsec);
5819 if (opd != NULL && opd->func_sec != NULL)
5820 {
5821 rsec->gc_mark = 1;
5822
5823 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5824 }
5825 }
5826
5827 return rsec;
5828 }
5829
5830 /* Update the .got, .plt. and dynamic reloc reference counts for the
5831 section being removed. */
5832
5833 static bfd_boolean
5834 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5835 asection *sec, const Elf_Internal_Rela *relocs)
5836 {
5837 struct ppc_link_hash_table *htab;
5838 Elf_Internal_Shdr *symtab_hdr;
5839 struct elf_link_hash_entry **sym_hashes;
5840 struct got_entry **local_got_ents;
5841 const Elf_Internal_Rela *rel, *relend;
5842
5843 if (info->relocatable)
5844 return TRUE;
5845
5846 if ((sec->flags & SEC_ALLOC) == 0)
5847 return TRUE;
5848
5849 elf_section_data (sec)->local_dynrel = NULL;
5850
5851 htab = ppc_hash_table (info);
5852 if (htab == NULL)
5853 return FALSE;
5854
5855 symtab_hdr = &elf_symtab_hdr (abfd);
5856 sym_hashes = elf_sym_hashes (abfd);
5857 local_got_ents = elf_local_got_ents (abfd);
5858
5859 relend = relocs + sec->reloc_count;
5860 for (rel = relocs; rel < relend; rel++)
5861 {
5862 unsigned long r_symndx;
5863 enum elf_ppc64_reloc_type r_type;
5864 struct elf_link_hash_entry *h = NULL;
5865 unsigned char tls_type = 0;
5866
5867 r_symndx = ELF64_R_SYM (rel->r_info);
5868 r_type = ELF64_R_TYPE (rel->r_info);
5869 if (r_symndx >= symtab_hdr->sh_info)
5870 {
5871 struct ppc_link_hash_entry *eh;
5872 struct elf_dyn_relocs **pp;
5873 struct elf_dyn_relocs *p;
5874
5875 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5876 h = elf_follow_link (h);
5877 eh = (struct ppc_link_hash_entry *) h;
5878
5879 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5880 if (p->sec == sec)
5881 {
5882 /* Everything must go for SEC. */
5883 *pp = p->next;
5884 break;
5885 }
5886 }
5887
5888 if (is_branch_reloc (r_type))
5889 {
5890 struct plt_entry **ifunc = NULL;
5891 if (h != NULL)
5892 {
5893 if (h->type == STT_GNU_IFUNC)
5894 ifunc = &h->plt.plist;
5895 }
5896 else if (local_got_ents != NULL)
5897 {
5898 struct plt_entry **local_plt = (struct plt_entry **)
5899 (local_got_ents + symtab_hdr->sh_info);
5900 unsigned char *local_got_tls_masks = (unsigned char *)
5901 (local_plt + symtab_hdr->sh_info);
5902 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5903 ifunc = local_plt + r_symndx;
5904 }
5905 if (ifunc != NULL)
5906 {
5907 struct plt_entry *ent;
5908
5909 for (ent = *ifunc; ent != NULL; ent = ent->next)
5910 if (ent->addend == rel->r_addend)
5911 break;
5912 if (ent == NULL)
5913 abort ();
5914 if (ent->plt.refcount > 0)
5915 ent->plt.refcount -= 1;
5916 continue;
5917 }
5918 }
5919
5920 switch (r_type)
5921 {
5922 case R_PPC64_GOT_TLSLD16:
5923 case R_PPC64_GOT_TLSLD16_LO:
5924 case R_PPC64_GOT_TLSLD16_HI:
5925 case R_PPC64_GOT_TLSLD16_HA:
5926 tls_type = TLS_TLS | TLS_LD;
5927 goto dogot;
5928
5929 case R_PPC64_GOT_TLSGD16:
5930 case R_PPC64_GOT_TLSGD16_LO:
5931 case R_PPC64_GOT_TLSGD16_HI:
5932 case R_PPC64_GOT_TLSGD16_HA:
5933 tls_type = TLS_TLS | TLS_GD;
5934 goto dogot;
5935
5936 case R_PPC64_GOT_TPREL16_DS:
5937 case R_PPC64_GOT_TPREL16_LO_DS:
5938 case R_PPC64_GOT_TPREL16_HI:
5939 case R_PPC64_GOT_TPREL16_HA:
5940 tls_type = TLS_TLS | TLS_TPREL;
5941 goto dogot;
5942
5943 case R_PPC64_GOT_DTPREL16_DS:
5944 case R_PPC64_GOT_DTPREL16_LO_DS:
5945 case R_PPC64_GOT_DTPREL16_HI:
5946 case R_PPC64_GOT_DTPREL16_HA:
5947 tls_type = TLS_TLS | TLS_DTPREL;
5948 goto dogot;
5949
5950 case R_PPC64_GOT16:
5951 case R_PPC64_GOT16_DS:
5952 case R_PPC64_GOT16_HA:
5953 case R_PPC64_GOT16_HI:
5954 case R_PPC64_GOT16_LO:
5955 case R_PPC64_GOT16_LO_DS:
5956 dogot:
5957 {
5958 struct got_entry *ent;
5959
5960 if (h != NULL)
5961 ent = h->got.glist;
5962 else
5963 ent = local_got_ents[r_symndx];
5964
5965 for (; ent != NULL; ent = ent->next)
5966 if (ent->addend == rel->r_addend
5967 && ent->owner == abfd
5968 && ent->tls_type == tls_type)
5969 break;
5970 if (ent == NULL)
5971 abort ();
5972 if (ent->got.refcount > 0)
5973 ent->got.refcount -= 1;
5974 }
5975 break;
5976
5977 case R_PPC64_PLT16_HA:
5978 case R_PPC64_PLT16_HI:
5979 case R_PPC64_PLT16_LO:
5980 case R_PPC64_PLT32:
5981 case R_PPC64_PLT64:
5982 case R_PPC64_REL14:
5983 case R_PPC64_REL14_BRNTAKEN:
5984 case R_PPC64_REL14_BRTAKEN:
5985 case R_PPC64_REL24:
5986 if (h != NULL)
5987 {
5988 struct plt_entry *ent;
5989
5990 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5991 if (ent->addend == rel->r_addend)
5992 break;
5993 if (ent != NULL && ent->plt.refcount > 0)
5994 ent->plt.refcount -= 1;
5995 }
5996 break;
5997
5998 default:
5999 break;
6000 }
6001 }
6002 return TRUE;
6003 }
6004
6005 /* The maximum size of .sfpr. */
6006 #define SFPR_MAX (218*4)
6007
6008 struct sfpr_def_parms
6009 {
6010 const char name[12];
6011 unsigned char lo, hi;
6012 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6013 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6014 };
6015
6016 /* Auto-generate _save*, _rest* functions in .sfpr. */
6017
6018 static bfd_boolean
6019 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6020 {
6021 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6022 unsigned int i;
6023 size_t len = strlen (parm->name);
6024 bfd_boolean writing = FALSE;
6025 char sym[16];
6026
6027 if (htab == NULL)
6028 return FALSE;
6029
6030 memcpy (sym, parm->name, len);
6031 sym[len + 2] = 0;
6032
6033 for (i = parm->lo; i <= parm->hi; i++)
6034 {
6035 struct elf_link_hash_entry *h;
6036
6037 sym[len + 0] = i / 10 + '0';
6038 sym[len + 1] = i % 10 + '0';
6039 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6040 if (h != NULL
6041 && !h->def_regular)
6042 {
6043 h->root.type = bfd_link_hash_defined;
6044 h->root.u.def.section = htab->sfpr;
6045 h->root.u.def.value = htab->sfpr->size;
6046 h->type = STT_FUNC;
6047 h->def_regular = 1;
6048 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6049 writing = TRUE;
6050 if (htab->sfpr->contents == NULL)
6051 {
6052 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6053 if (htab->sfpr->contents == NULL)
6054 return FALSE;
6055 }
6056 }
6057 if (writing)
6058 {
6059 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6060 if (i != parm->hi)
6061 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6062 else
6063 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6064 htab->sfpr->size = p - htab->sfpr->contents;
6065 }
6066 }
6067
6068 return TRUE;
6069 }
6070
6071 static bfd_byte *
6072 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6073 {
6074 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6075 return p + 4;
6076 }
6077
6078 static bfd_byte *
6079 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6080 {
6081 p = savegpr0 (abfd, p, r);
6082 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6083 p = p + 4;
6084 bfd_put_32 (abfd, BLR, p);
6085 return p + 4;
6086 }
6087
6088 static bfd_byte *
6089 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6090 {
6091 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6092 return p + 4;
6093 }
6094
6095 static bfd_byte *
6096 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6097 {
6098 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6099 p = p + 4;
6100 p = restgpr0 (abfd, p, r);
6101 bfd_put_32 (abfd, MTLR_R0, p);
6102 p = p + 4;
6103 if (r == 29)
6104 {
6105 p = restgpr0 (abfd, p, 30);
6106 p = restgpr0 (abfd, p, 31);
6107 }
6108 bfd_put_32 (abfd, BLR, p);
6109 return p + 4;
6110 }
6111
6112 static bfd_byte *
6113 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6114 {
6115 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6116 return p + 4;
6117 }
6118
6119 static bfd_byte *
6120 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6121 {
6122 p = savegpr1 (abfd, p, r);
6123 bfd_put_32 (abfd, BLR, p);
6124 return p + 4;
6125 }
6126
6127 static bfd_byte *
6128 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6129 {
6130 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6131 return p + 4;
6132 }
6133
6134 static bfd_byte *
6135 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6136 {
6137 p = restgpr1 (abfd, p, r);
6138 bfd_put_32 (abfd, BLR, p);
6139 return p + 4;
6140 }
6141
6142 static bfd_byte *
6143 savefpr (bfd *abfd, bfd_byte *p, int r)
6144 {
6145 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6146 return p + 4;
6147 }
6148
6149 static bfd_byte *
6150 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6151 {
6152 p = savefpr (abfd, p, r);
6153 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6154 p = p + 4;
6155 bfd_put_32 (abfd, BLR, p);
6156 return p + 4;
6157 }
6158
6159 static bfd_byte *
6160 restfpr (bfd *abfd, bfd_byte *p, int r)
6161 {
6162 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6163 return p + 4;
6164 }
6165
6166 static bfd_byte *
6167 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6168 {
6169 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6170 p = p + 4;
6171 p = restfpr (abfd, p, r);
6172 bfd_put_32 (abfd, MTLR_R0, p);
6173 p = p + 4;
6174 if (r == 29)
6175 {
6176 p = restfpr (abfd, p, 30);
6177 p = restfpr (abfd, p, 31);
6178 }
6179 bfd_put_32 (abfd, BLR, p);
6180 return p + 4;
6181 }
6182
6183 static bfd_byte *
6184 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6185 {
6186 p = savefpr (abfd, p, r);
6187 bfd_put_32 (abfd, BLR, p);
6188 return p + 4;
6189 }
6190
6191 static bfd_byte *
6192 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6193 {
6194 p = restfpr (abfd, p, r);
6195 bfd_put_32 (abfd, BLR, p);
6196 return p + 4;
6197 }
6198
6199 static bfd_byte *
6200 savevr (bfd *abfd, bfd_byte *p, int r)
6201 {
6202 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6203 p = p + 4;
6204 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6205 return p + 4;
6206 }
6207
6208 static bfd_byte *
6209 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6210 {
6211 p = savevr (abfd, p, r);
6212 bfd_put_32 (abfd, BLR, p);
6213 return p + 4;
6214 }
6215
6216 static bfd_byte *
6217 restvr (bfd *abfd, bfd_byte *p, int r)
6218 {
6219 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6220 p = p + 4;
6221 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6222 return p + 4;
6223 }
6224
6225 static bfd_byte *
6226 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6227 {
6228 p = restvr (abfd, p, r);
6229 bfd_put_32 (abfd, BLR, p);
6230 return p + 4;
6231 }
6232
6233 /* Called via elf_link_hash_traverse to transfer dynamic linking
6234 information on function code symbol entries to their corresponding
6235 function descriptor symbol entries. */
6236
6237 static bfd_boolean
6238 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6239 {
6240 struct bfd_link_info *info;
6241 struct ppc_link_hash_table *htab;
6242 struct plt_entry *ent;
6243 struct ppc_link_hash_entry *fh;
6244 struct ppc_link_hash_entry *fdh;
6245 bfd_boolean force_local;
6246
6247 fh = (struct ppc_link_hash_entry *) h;
6248 if (fh->elf.root.type == bfd_link_hash_indirect)
6249 return TRUE;
6250
6251 info = inf;
6252 htab = ppc_hash_table (info);
6253 if (htab == NULL)
6254 return FALSE;
6255
6256 /* Resolve undefined references to dot-symbols as the value
6257 in the function descriptor, if we have one in a regular object.
6258 This is to satisfy cases like ".quad .foo". Calls to functions
6259 in dynamic objects are handled elsewhere. */
6260 if (fh->elf.root.type == bfd_link_hash_undefweak
6261 && fh->was_undefined
6262 && (fdh = defined_func_desc (fh)) != NULL
6263 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6264 && opd_entry_value (fdh->elf.root.u.def.section,
6265 fdh->elf.root.u.def.value,
6266 &fh->elf.root.u.def.section,
6267 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6268 {
6269 fh->elf.root.type = fdh->elf.root.type;
6270 fh->elf.forced_local = 1;
6271 fh->elf.def_regular = fdh->elf.def_regular;
6272 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6273 }
6274
6275 /* If this is a function code symbol, transfer dynamic linking
6276 information to the function descriptor symbol. */
6277 if (!fh->is_func)
6278 return TRUE;
6279
6280 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6281 if (ent->plt.refcount > 0)
6282 break;
6283 if (ent == NULL
6284 || fh->elf.root.root.string[0] != '.'
6285 || fh->elf.root.root.string[1] == '\0')
6286 return TRUE;
6287
6288 /* Find the corresponding function descriptor symbol. Create it
6289 as undefined if necessary. */
6290
6291 fdh = lookup_fdh (fh, htab);
6292 if (fdh == NULL
6293 && !info->executable
6294 && (fh->elf.root.type == bfd_link_hash_undefined
6295 || fh->elf.root.type == bfd_link_hash_undefweak))
6296 {
6297 fdh = make_fdh (info, fh);
6298 if (fdh == NULL)
6299 return FALSE;
6300 }
6301
6302 /* Fake function descriptors are made undefweak. If the function
6303 code symbol is strong undefined, make the fake sym the same.
6304 If the function code symbol is defined, then force the fake
6305 descriptor local; We can't support overriding of symbols in a
6306 shared library on a fake descriptor. */
6307
6308 if (fdh != NULL
6309 && fdh->fake
6310 && fdh->elf.root.type == bfd_link_hash_undefweak)
6311 {
6312 if (fh->elf.root.type == bfd_link_hash_undefined)
6313 {
6314 fdh->elf.root.type = bfd_link_hash_undefined;
6315 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6316 }
6317 else if (fh->elf.root.type == bfd_link_hash_defined
6318 || fh->elf.root.type == bfd_link_hash_defweak)
6319 {
6320 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6321 }
6322 }
6323
6324 if (fdh != NULL
6325 && !fdh->elf.forced_local
6326 && (!info->executable
6327 || fdh->elf.def_dynamic
6328 || fdh->elf.ref_dynamic
6329 || (fdh->elf.root.type == bfd_link_hash_undefweak
6330 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6331 {
6332 if (fdh->elf.dynindx == -1)
6333 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6334 return FALSE;
6335 fdh->elf.ref_regular |= fh->elf.ref_regular;
6336 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6337 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6338 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6339 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6340 {
6341 move_plt_plist (fh, fdh);
6342 fdh->elf.needs_plt = 1;
6343 }
6344 fdh->is_func_descriptor = 1;
6345 fdh->oh = fh;
6346 fh->oh = fdh;
6347 }
6348
6349 /* Now that the info is on the function descriptor, clear the
6350 function code sym info. Any function code syms for which we
6351 don't have a definition in a regular file, we force local.
6352 This prevents a shared library from exporting syms that have
6353 been imported from another library. Function code syms that
6354 are really in the library we must leave global to prevent the
6355 linker dragging in a definition from a static library. */
6356 force_local = (!fh->elf.def_regular
6357 || fdh == NULL
6358 || !fdh->elf.def_regular
6359 || fdh->elf.forced_local);
6360 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6361
6362 return TRUE;
6363 }
6364
6365 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6366 this hook to a) provide some gcc support functions, and b) transfer
6367 dynamic linking information gathered so far on function code symbol
6368 entries, to their corresponding function descriptor symbol entries. */
6369
6370 static bfd_boolean
6371 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6372 struct bfd_link_info *info)
6373 {
6374 struct ppc_link_hash_table *htab;
6375 unsigned int i;
6376 const struct sfpr_def_parms funcs[] =
6377 {
6378 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6379 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6380 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6381 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6382 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6383 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6384 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6385 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6386 { "._savef", 14, 31, savefpr, savefpr1_tail },
6387 { "._restf", 14, 31, restfpr, restfpr1_tail },
6388 { "_savevr_", 20, 31, savevr, savevr_tail },
6389 { "_restvr_", 20, 31, restvr, restvr_tail }
6390 };
6391
6392 htab = ppc_hash_table (info);
6393 if (htab == NULL)
6394 return FALSE;
6395
6396 if (htab->sfpr == NULL)
6397 /* We don't have any relocs. */
6398 return TRUE;
6399
6400 /* Provide any missing _save* and _rest* functions. */
6401 htab->sfpr->size = 0;
6402 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6403 if (!sfpr_define (info, &funcs[i]))
6404 return FALSE;
6405
6406 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6407
6408 if (htab->sfpr->size == 0)
6409 htab->sfpr->flags |= SEC_EXCLUDE;
6410
6411 return TRUE;
6412 }
6413
6414 /* Adjust a symbol defined by a dynamic object and referenced by a
6415 regular object. The current definition is in some section of the
6416 dynamic object, but we're not including those sections. We have to
6417 change the definition to something the rest of the link can
6418 understand. */
6419
6420 static bfd_boolean
6421 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6422 struct elf_link_hash_entry *h)
6423 {
6424 struct ppc_link_hash_table *htab;
6425 asection *s;
6426
6427 htab = ppc_hash_table (info);
6428 if (htab == NULL)
6429 return FALSE;
6430
6431 /* Deal with function syms. */
6432 if (h->type == STT_FUNC
6433 || h->type == STT_GNU_IFUNC
6434 || h->needs_plt)
6435 {
6436 /* Clear procedure linkage table information for any symbol that
6437 won't need a .plt entry. */
6438 struct plt_entry *ent;
6439 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6440 if (ent->plt.refcount > 0)
6441 break;
6442 if (ent == NULL
6443 || (h->type != STT_GNU_IFUNC
6444 && (SYMBOL_CALLS_LOCAL (info, h)
6445 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6446 && h->root.type == bfd_link_hash_undefweak))))
6447 {
6448 h->plt.plist = NULL;
6449 h->needs_plt = 0;
6450 }
6451 }
6452 else
6453 h->plt.plist = NULL;
6454
6455 /* If this is a weak symbol, and there is a real definition, the
6456 processor independent code will have arranged for us to see the
6457 real definition first, and we can just use the same value. */
6458 if (h->u.weakdef != NULL)
6459 {
6460 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6461 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6462 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6463 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6464 if (ELIMINATE_COPY_RELOCS)
6465 h->non_got_ref = h->u.weakdef->non_got_ref;
6466 return TRUE;
6467 }
6468
6469 /* If we are creating a shared library, we must presume that the
6470 only references to the symbol are via the global offset table.
6471 For such cases we need not do anything here; the relocations will
6472 be handled correctly by relocate_section. */
6473 if (info->shared)
6474 return TRUE;
6475
6476 /* If there are no references to this symbol that do not use the
6477 GOT, we don't need to generate a copy reloc. */
6478 if (!h->non_got_ref)
6479 return TRUE;
6480
6481 /* Don't generate a copy reloc for symbols defined in the executable. */
6482 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6483 return TRUE;
6484
6485 if (ELIMINATE_COPY_RELOCS)
6486 {
6487 struct ppc_link_hash_entry * eh;
6488 struct elf_dyn_relocs *p;
6489
6490 eh = (struct ppc_link_hash_entry *) h;
6491 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6492 {
6493 s = p->sec->output_section;
6494 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6495 break;
6496 }
6497
6498 /* If we didn't find any dynamic relocs in read-only sections, then
6499 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6500 if (p == NULL)
6501 {
6502 h->non_got_ref = 0;
6503 return TRUE;
6504 }
6505 }
6506
6507 if (h->plt.plist != NULL)
6508 {
6509 /* We should never get here, but unfortunately there are versions
6510 of gcc out there that improperly (for this ABI) put initialized
6511 function pointers, vtable refs and suchlike in read-only
6512 sections. Allow them to proceed, but warn that this might
6513 break at runtime. */
6514 info->callbacks->einfo
6515 (_("%P: copy reloc against `%s' requires lazy plt linking; "
6516 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6517 h->root.root.string);
6518 }
6519
6520 /* This is a reference to a symbol defined by a dynamic object which
6521 is not a function. */
6522
6523 if (h->size == 0)
6524 {
6525 info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
6526 h->root.root.string);
6527 return TRUE;
6528 }
6529
6530 /* We must allocate the symbol in our .dynbss section, which will
6531 become part of the .bss section of the executable. There will be
6532 an entry for this symbol in the .dynsym section. The dynamic
6533 object will contain position independent code, so all references
6534 from the dynamic object to this symbol will go through the global
6535 offset table. The dynamic linker will use the .dynsym entry to
6536 determine the address it must put in the global offset table, so
6537 both the dynamic object and the regular object will refer to the
6538 same memory location for the variable. */
6539
6540 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6541 to copy the initial value out of the dynamic object and into the
6542 runtime process image. We need to remember the offset into the
6543 .rela.bss section we are going to use. */
6544 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6545 {
6546 htab->relbss->size += sizeof (Elf64_External_Rela);
6547 h->needs_copy = 1;
6548 }
6549
6550 s = htab->dynbss;
6551
6552 return _bfd_elf_adjust_dynamic_copy (h, s);
6553 }
6554
6555 /* If given a function descriptor symbol, hide both the function code
6556 sym and the descriptor. */
6557 static void
6558 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6559 struct elf_link_hash_entry *h,
6560 bfd_boolean force_local)
6561 {
6562 struct ppc_link_hash_entry *eh;
6563 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6564
6565 eh = (struct ppc_link_hash_entry *) h;
6566 if (eh->is_func_descriptor)
6567 {
6568 struct ppc_link_hash_entry *fh = eh->oh;
6569
6570 if (fh == NULL)
6571 {
6572 const char *p, *q;
6573 struct ppc_link_hash_table *htab;
6574 char save;
6575
6576 /* We aren't supposed to use alloca in BFD because on
6577 systems which do not have alloca the version in libiberty
6578 calls xmalloc, which might cause the program to crash
6579 when it runs out of memory. This function doesn't have a
6580 return status, so there's no way to gracefully return an
6581 error. So cheat. We know that string[-1] can be safely
6582 accessed; It's either a string in an ELF string table,
6583 or allocated in an objalloc structure. */
6584
6585 p = eh->elf.root.root.string - 1;
6586 save = *p;
6587 *(char *) p = '.';
6588 htab = ppc_hash_table (info);
6589 if (htab == NULL)
6590 return;
6591
6592 fh = (struct ppc_link_hash_entry *)
6593 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6594 *(char *) p = save;
6595
6596 /* Unfortunately, if it so happens that the string we were
6597 looking for was allocated immediately before this string,
6598 then we overwrote the string terminator. That's the only
6599 reason the lookup should fail. */
6600 if (fh == NULL)
6601 {
6602 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6603 while (q >= eh->elf.root.root.string && *q == *p)
6604 --q, --p;
6605 if (q < eh->elf.root.root.string && *p == '.')
6606 fh = (struct ppc_link_hash_entry *)
6607 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6608 }
6609 if (fh != NULL)
6610 {
6611 eh->oh = fh;
6612 fh->oh = eh;
6613 }
6614 }
6615 if (fh != NULL)
6616 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6617 }
6618 }
6619
6620 static bfd_boolean
6621 get_sym_h (struct elf_link_hash_entry **hp,
6622 Elf_Internal_Sym **symp,
6623 asection **symsecp,
6624 unsigned char **tls_maskp,
6625 Elf_Internal_Sym **locsymsp,
6626 unsigned long r_symndx,
6627 bfd *ibfd)
6628 {
6629 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6630
6631 if (r_symndx >= symtab_hdr->sh_info)
6632 {
6633 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6634 struct elf_link_hash_entry *h;
6635
6636 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6637 h = elf_follow_link (h);
6638
6639 if (hp != NULL)
6640 *hp = h;
6641
6642 if (symp != NULL)
6643 *symp = NULL;
6644
6645 if (symsecp != NULL)
6646 {
6647 asection *symsec = NULL;
6648 if (h->root.type == bfd_link_hash_defined
6649 || h->root.type == bfd_link_hash_defweak)
6650 symsec = h->root.u.def.section;
6651 *symsecp = symsec;
6652 }
6653
6654 if (tls_maskp != NULL)
6655 {
6656 struct ppc_link_hash_entry *eh;
6657
6658 eh = (struct ppc_link_hash_entry *) h;
6659 *tls_maskp = &eh->tls_mask;
6660 }
6661 }
6662 else
6663 {
6664 Elf_Internal_Sym *sym;
6665 Elf_Internal_Sym *locsyms = *locsymsp;
6666
6667 if (locsyms == NULL)
6668 {
6669 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6670 if (locsyms == NULL)
6671 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6672 symtab_hdr->sh_info,
6673 0, NULL, NULL, NULL);
6674 if (locsyms == NULL)
6675 return FALSE;
6676 *locsymsp = locsyms;
6677 }
6678 sym = locsyms + r_symndx;
6679
6680 if (hp != NULL)
6681 *hp = NULL;
6682
6683 if (symp != NULL)
6684 *symp = sym;
6685
6686 if (symsecp != NULL)
6687 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6688
6689 if (tls_maskp != NULL)
6690 {
6691 struct got_entry **lgot_ents;
6692 unsigned char *tls_mask;
6693
6694 tls_mask = NULL;
6695 lgot_ents = elf_local_got_ents (ibfd);
6696 if (lgot_ents != NULL)
6697 {
6698 struct plt_entry **local_plt = (struct plt_entry **)
6699 (lgot_ents + symtab_hdr->sh_info);
6700 unsigned char *lgot_masks = (unsigned char *)
6701 (local_plt + symtab_hdr->sh_info);
6702 tls_mask = &lgot_masks[r_symndx];
6703 }
6704 *tls_maskp = tls_mask;
6705 }
6706 }
6707 return TRUE;
6708 }
6709
6710 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6711 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6712 type suitable for optimization, and 1 otherwise. */
6713
6714 static int
6715 get_tls_mask (unsigned char **tls_maskp,
6716 unsigned long *toc_symndx,
6717 bfd_vma *toc_addend,
6718 Elf_Internal_Sym **locsymsp,
6719 const Elf_Internal_Rela *rel,
6720 bfd *ibfd)
6721 {
6722 unsigned long r_symndx;
6723 int next_r;
6724 struct elf_link_hash_entry *h;
6725 Elf_Internal_Sym *sym;
6726 asection *sec;
6727 bfd_vma off;
6728
6729 r_symndx = ELF64_R_SYM (rel->r_info);
6730 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6731 return 0;
6732
6733 if ((*tls_maskp != NULL && **tls_maskp != 0)
6734 || sec == NULL
6735 || ppc64_elf_section_data (sec) == NULL
6736 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6737 return 1;
6738
6739 /* Look inside a TOC section too. */
6740 if (h != NULL)
6741 {
6742 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6743 off = h->root.u.def.value;
6744 }
6745 else
6746 off = sym->st_value;
6747 off += rel->r_addend;
6748 BFD_ASSERT (off % 8 == 0);
6749 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6750 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6751 if (toc_symndx != NULL)
6752 *toc_symndx = r_symndx;
6753 if (toc_addend != NULL)
6754 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6755 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6756 return 0;
6757 if ((h == NULL || is_static_defined (h))
6758 && (next_r == -1 || next_r == -2))
6759 return 1 - next_r;
6760 return 1;
6761 }
6762
6763 /* Find (or create) an entry in the tocsave hash table. */
6764
6765 static struct tocsave_entry *
6766 tocsave_find (struct ppc_link_hash_table *htab,
6767 enum insert_option insert,
6768 Elf_Internal_Sym **local_syms,
6769 const Elf_Internal_Rela *irela,
6770 bfd *ibfd)
6771 {
6772 unsigned long r_indx;
6773 struct elf_link_hash_entry *h;
6774 Elf_Internal_Sym *sym;
6775 struct tocsave_entry ent, *p;
6776 hashval_t hash;
6777 struct tocsave_entry **slot;
6778
6779 r_indx = ELF64_R_SYM (irela->r_info);
6780 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6781 return NULL;
6782 if (ent.sec == NULL || ent.sec->output_section == NULL)
6783 {
6784 (*_bfd_error_handler)
6785 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6786 return NULL;
6787 }
6788
6789 if (h != NULL)
6790 ent.offset = h->root.u.def.value;
6791 else
6792 ent.offset = sym->st_value;
6793 ent.offset += irela->r_addend;
6794
6795 hash = tocsave_htab_hash (&ent);
6796 slot = ((struct tocsave_entry **)
6797 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6798 if (slot == NULL)
6799 return NULL;
6800
6801 if (*slot == NULL)
6802 {
6803 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6804 if (p == NULL)
6805 return NULL;
6806 *p = ent;
6807 *slot = p;
6808 }
6809 return *slot;
6810 }
6811
6812 /* Adjust all global syms defined in opd sections. In gcc generated
6813 code for the old ABI, these will already have been done. */
6814
6815 static bfd_boolean
6816 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6817 {
6818 struct ppc_link_hash_entry *eh;
6819 asection *sym_sec;
6820 struct _opd_sec_data *opd;
6821
6822 if (h->root.type == bfd_link_hash_indirect)
6823 return TRUE;
6824
6825 if (h->root.type != bfd_link_hash_defined
6826 && h->root.type != bfd_link_hash_defweak)
6827 return TRUE;
6828
6829 eh = (struct ppc_link_hash_entry *) h;
6830 if (eh->adjust_done)
6831 return TRUE;
6832
6833 sym_sec = eh->elf.root.u.def.section;
6834 opd = get_opd_info (sym_sec);
6835 if (opd != NULL && opd->adjust != NULL)
6836 {
6837 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6838 if (adjust == -1)
6839 {
6840 /* This entry has been deleted. */
6841 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6842 if (dsec == NULL)
6843 {
6844 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6845 if (elf_discarded_section (dsec))
6846 {
6847 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6848 break;
6849 }
6850 }
6851 eh->elf.root.u.def.value = 0;
6852 eh->elf.root.u.def.section = dsec;
6853 }
6854 else
6855 eh->elf.root.u.def.value += adjust;
6856 eh->adjust_done = 1;
6857 }
6858 return TRUE;
6859 }
6860
6861 /* Handles decrementing dynamic reloc counts for the reloc specified by
6862 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6863 have already been determined. */
6864
6865 static bfd_boolean
6866 dec_dynrel_count (bfd_vma r_info,
6867 asection *sec,
6868 struct bfd_link_info *info,
6869 Elf_Internal_Sym **local_syms,
6870 struct elf_link_hash_entry *h,
6871 asection *sym_sec)
6872 {
6873 enum elf_ppc64_reloc_type r_type;
6874 struct elf_dyn_relocs *p;
6875 struct elf_dyn_relocs **pp;
6876
6877 /* Can this reloc be dynamic? This switch, and later tests here
6878 should be kept in sync with the code in check_relocs. */
6879 r_type = ELF64_R_TYPE (r_info);
6880 switch (r_type)
6881 {
6882 default:
6883 return TRUE;
6884
6885 case R_PPC64_TPREL16:
6886 case R_PPC64_TPREL16_LO:
6887 case R_PPC64_TPREL16_HI:
6888 case R_PPC64_TPREL16_HA:
6889 case R_PPC64_TPREL16_DS:
6890 case R_PPC64_TPREL16_LO_DS:
6891 case R_PPC64_TPREL16_HIGHER:
6892 case R_PPC64_TPREL16_HIGHERA:
6893 case R_PPC64_TPREL16_HIGHEST:
6894 case R_PPC64_TPREL16_HIGHESTA:
6895 if (!info->shared)
6896 return TRUE;
6897
6898 case R_PPC64_TPREL64:
6899 case R_PPC64_DTPMOD64:
6900 case R_PPC64_DTPREL64:
6901 case R_PPC64_ADDR64:
6902 case R_PPC64_REL30:
6903 case R_PPC64_REL32:
6904 case R_PPC64_REL64:
6905 case R_PPC64_ADDR14:
6906 case R_PPC64_ADDR14_BRNTAKEN:
6907 case R_PPC64_ADDR14_BRTAKEN:
6908 case R_PPC64_ADDR16:
6909 case R_PPC64_ADDR16_DS:
6910 case R_PPC64_ADDR16_HA:
6911 case R_PPC64_ADDR16_HI:
6912 case R_PPC64_ADDR16_HIGHER:
6913 case R_PPC64_ADDR16_HIGHERA:
6914 case R_PPC64_ADDR16_HIGHEST:
6915 case R_PPC64_ADDR16_HIGHESTA:
6916 case R_PPC64_ADDR16_LO:
6917 case R_PPC64_ADDR16_LO_DS:
6918 case R_PPC64_ADDR24:
6919 case R_PPC64_ADDR32:
6920 case R_PPC64_UADDR16:
6921 case R_PPC64_UADDR32:
6922 case R_PPC64_UADDR64:
6923 case R_PPC64_TOC:
6924 break;
6925 }
6926
6927 if (local_syms != NULL)
6928 {
6929 unsigned long r_symndx;
6930 Elf_Internal_Sym *sym;
6931 bfd *ibfd = sec->owner;
6932
6933 r_symndx = ELF64_R_SYM (r_info);
6934 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6935 return FALSE;
6936 }
6937
6938 if ((info->shared
6939 && (must_be_dyn_reloc (info, r_type)
6940 || (h != NULL
6941 && (!info->symbolic
6942 || h->root.type == bfd_link_hash_defweak
6943 || !h->def_regular))))
6944 || (ELIMINATE_COPY_RELOCS
6945 && !info->shared
6946 && h != NULL
6947 && (h->root.type == bfd_link_hash_defweak
6948 || !h->def_regular)))
6949 ;
6950 else
6951 return TRUE;
6952
6953 if (h != NULL)
6954 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6955 else
6956 {
6957 if (sym_sec != NULL)
6958 {
6959 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6960 pp = (struct elf_dyn_relocs **) vpp;
6961 }
6962 else
6963 {
6964 void *vpp = &elf_section_data (sec)->local_dynrel;
6965 pp = (struct elf_dyn_relocs **) vpp;
6966 }
6967
6968 /* elf_gc_sweep may have already removed all dyn relocs associated
6969 with local syms for a given section. Don't report a dynreloc
6970 miscount. */
6971 if (*pp == NULL)
6972 return TRUE;
6973 }
6974
6975 while ((p = *pp) != NULL)
6976 {
6977 if (p->sec == sec)
6978 {
6979 if (!must_be_dyn_reloc (info, r_type))
6980 p->pc_count -= 1;
6981 p->count -= 1;
6982 if (p->count == 0)
6983 *pp = p->next;
6984 return TRUE;
6985 }
6986 pp = &p->next;
6987 }
6988
6989 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
6990 sec->owner, sec);
6991 bfd_set_error (bfd_error_bad_value);
6992 return FALSE;
6993 }
6994
6995 /* Remove unused Official Procedure Descriptor entries. Currently we
6996 only remove those associated with functions in discarded link-once
6997 sections, or weakly defined functions that have been overridden. It
6998 would be possible to remove many more entries for statically linked
6999 applications. */
7000
7001 bfd_boolean
7002 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7003 {
7004 bfd *ibfd;
7005 bfd_boolean some_edited = FALSE;
7006 asection *need_pad = NULL;
7007
7008 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7009 {
7010 asection *sec;
7011 Elf_Internal_Rela *relstart, *rel, *relend;
7012 Elf_Internal_Shdr *symtab_hdr;
7013 Elf_Internal_Sym *local_syms;
7014 bfd_vma offset;
7015 struct _opd_sec_data *opd;
7016 bfd_boolean need_edit, add_aux_fields;
7017 bfd_size_type cnt_16b = 0;
7018
7019 if (!is_ppc64_elf (ibfd))
7020 continue;
7021
7022 sec = bfd_get_section_by_name (ibfd, ".opd");
7023 if (sec == NULL || sec->size == 0)
7024 continue;
7025
7026 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
7027 continue;
7028
7029 if (sec->output_section == bfd_abs_section_ptr)
7030 continue;
7031
7032 /* Look through the section relocs. */
7033 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7034 continue;
7035
7036 local_syms = NULL;
7037 symtab_hdr = &elf_symtab_hdr (ibfd);
7038
7039 /* Read the relocations. */
7040 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7041 info->keep_memory);
7042 if (relstart == NULL)
7043 return FALSE;
7044
7045 /* First run through the relocs to check they are sane, and to
7046 determine whether we need to edit this opd section. */
7047 need_edit = FALSE;
7048 need_pad = sec;
7049 offset = 0;
7050 relend = relstart + sec->reloc_count;
7051 for (rel = relstart; rel < relend; )
7052 {
7053 enum elf_ppc64_reloc_type r_type;
7054 unsigned long r_symndx;
7055 asection *sym_sec;
7056 struct elf_link_hash_entry *h;
7057 Elf_Internal_Sym *sym;
7058
7059 /* .opd contains a regular array of 16 or 24 byte entries. We're
7060 only interested in the reloc pointing to a function entry
7061 point. */
7062 if (rel->r_offset != offset
7063 || rel + 1 >= relend
7064 || (rel + 1)->r_offset != offset + 8)
7065 {
7066 /* If someone messes with .opd alignment then after a
7067 "ld -r" we might have padding in the middle of .opd.
7068 Also, there's nothing to prevent someone putting
7069 something silly in .opd with the assembler. No .opd
7070 optimization for them! */
7071 broken_opd:
7072 (*_bfd_error_handler)
7073 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7074 need_edit = FALSE;
7075 break;
7076 }
7077
7078 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7079 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7080 {
7081 (*_bfd_error_handler)
7082 (_("%B: unexpected reloc type %u in .opd section"),
7083 ibfd, r_type);
7084 need_edit = FALSE;
7085 break;
7086 }
7087
7088 r_symndx = ELF64_R_SYM (rel->r_info);
7089 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7090 r_symndx, ibfd))
7091 goto error_ret;
7092
7093 if (sym_sec == NULL || sym_sec->owner == NULL)
7094 {
7095 const char *sym_name;
7096 if (h != NULL)
7097 sym_name = h->root.root.string;
7098 else
7099 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7100 sym_sec);
7101
7102 (*_bfd_error_handler)
7103 (_("%B: undefined sym `%s' in .opd section"),
7104 ibfd, sym_name);
7105 need_edit = FALSE;
7106 break;
7107 }
7108
7109 /* opd entries are always for functions defined in the
7110 current input bfd. If the symbol isn't defined in the
7111 input bfd, then we won't be using the function in this
7112 bfd; It must be defined in a linkonce section in another
7113 bfd, or is weak. It's also possible that we are
7114 discarding the function due to a linker script /DISCARD/,
7115 which we test for via the output_section. */
7116 if (sym_sec->owner != ibfd
7117 || sym_sec->output_section == bfd_abs_section_ptr)
7118 need_edit = TRUE;
7119
7120 rel += 2;
7121 if (rel == relend
7122 || (rel + 1 == relend && rel->r_offset == offset + 16))
7123 {
7124 if (sec->size == offset + 24)
7125 {
7126 need_pad = NULL;
7127 break;
7128 }
7129 if (rel == relend && sec->size == offset + 16)
7130 {
7131 cnt_16b++;
7132 break;
7133 }
7134 goto broken_opd;
7135 }
7136
7137 if (rel->r_offset == offset + 24)
7138 offset += 24;
7139 else if (rel->r_offset != offset + 16)
7140 goto broken_opd;
7141 else if (rel + 1 < relend
7142 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7143 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7144 {
7145 offset += 16;
7146 cnt_16b++;
7147 }
7148 else if (rel + 2 < relend
7149 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7150 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7151 {
7152 offset += 24;
7153 rel += 1;
7154 }
7155 else
7156 goto broken_opd;
7157 }
7158
7159 add_aux_fields = non_overlapping && cnt_16b > 0;
7160
7161 if (need_edit || add_aux_fields)
7162 {
7163 Elf_Internal_Rela *write_rel;
7164 Elf_Internal_Shdr *rel_hdr;
7165 bfd_byte *rptr, *wptr;
7166 bfd_byte *new_contents;
7167 bfd_boolean skip;
7168 long opd_ent_size;
7169 bfd_size_type amt;
7170
7171 new_contents = NULL;
7172 amt = sec->size * sizeof (long) / 8;
7173 opd = &ppc64_elf_section_data (sec)->u.opd;
7174 opd->adjust = bfd_zalloc (sec->owner, amt);
7175 if (opd->adjust == NULL)
7176 return FALSE;
7177 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7178
7179 /* This seems a waste of time as input .opd sections are all
7180 zeros as generated by gcc, but I suppose there's no reason
7181 this will always be so. We might start putting something in
7182 the third word of .opd entries. */
7183 if ((sec->flags & SEC_IN_MEMORY) == 0)
7184 {
7185 bfd_byte *loc;
7186 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7187 {
7188 if (loc != NULL)
7189 free (loc);
7190 error_ret:
7191 if (local_syms != NULL
7192 && symtab_hdr->contents != (unsigned char *) local_syms)
7193 free (local_syms);
7194 if (elf_section_data (sec)->relocs != relstart)
7195 free (relstart);
7196 return FALSE;
7197 }
7198 sec->contents = loc;
7199 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7200 }
7201
7202 elf_section_data (sec)->relocs = relstart;
7203
7204 new_contents = sec->contents;
7205 if (add_aux_fields)
7206 {
7207 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7208 if (new_contents == NULL)
7209 return FALSE;
7210 need_pad = FALSE;
7211 }
7212 wptr = new_contents;
7213 rptr = sec->contents;
7214
7215 write_rel = relstart;
7216 skip = FALSE;
7217 offset = 0;
7218 opd_ent_size = 0;
7219 for (rel = relstart; rel < relend; rel++)
7220 {
7221 unsigned long r_symndx;
7222 asection *sym_sec;
7223 struct elf_link_hash_entry *h;
7224 Elf_Internal_Sym *sym;
7225
7226 r_symndx = ELF64_R_SYM (rel->r_info);
7227 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7228 r_symndx, ibfd))
7229 goto error_ret;
7230
7231 if (rel->r_offset == offset)
7232 {
7233 struct ppc_link_hash_entry *fdh = NULL;
7234
7235 /* See if the .opd entry is full 24 byte or
7236 16 byte (with fd_aux entry overlapped with next
7237 fd_func). */
7238 opd_ent_size = 24;
7239 if ((rel + 2 == relend && sec->size == offset + 16)
7240 || (rel + 3 < relend
7241 && rel[2].r_offset == offset + 16
7242 && rel[3].r_offset == offset + 24
7243 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7244 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7245 opd_ent_size = 16;
7246
7247 if (h != NULL
7248 && h->root.root.string[0] == '.')
7249 {
7250 struct ppc_link_hash_table *htab;
7251
7252 htab = ppc_hash_table (info);
7253 if (htab != NULL)
7254 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7255 htab);
7256 if (fdh != NULL
7257 && fdh->elf.root.type != bfd_link_hash_defined
7258 && fdh->elf.root.type != bfd_link_hash_defweak)
7259 fdh = NULL;
7260 }
7261
7262 skip = (sym_sec->owner != ibfd
7263 || sym_sec->output_section == bfd_abs_section_ptr);
7264 if (skip)
7265 {
7266 if (fdh != NULL && sym_sec->owner == ibfd)
7267 {
7268 /* Arrange for the function descriptor sym
7269 to be dropped. */
7270 fdh->elf.root.u.def.value = 0;
7271 fdh->elf.root.u.def.section = sym_sec;
7272 }
7273 opd->adjust[rel->r_offset / 8] = -1;
7274 }
7275 else
7276 {
7277 /* We'll be keeping this opd entry. */
7278
7279 if (fdh != NULL)
7280 {
7281 /* Redefine the function descriptor symbol to
7282 this location in the opd section. It is
7283 necessary to update the value here rather
7284 than using an array of adjustments as we do
7285 for local symbols, because various places
7286 in the generic ELF code use the value
7287 stored in u.def.value. */
7288 fdh->elf.root.u.def.value = wptr - new_contents;
7289 fdh->adjust_done = 1;
7290 }
7291
7292 /* Local syms are a bit tricky. We could
7293 tweak them as they can be cached, but
7294 we'd need to look through the local syms
7295 for the function descriptor sym which we
7296 don't have at the moment. So keep an
7297 array of adjustments. */
7298 opd->adjust[rel->r_offset / 8]
7299 = (wptr - new_contents) - (rptr - sec->contents);
7300
7301 if (wptr != rptr)
7302 memcpy (wptr, rptr, opd_ent_size);
7303 wptr += opd_ent_size;
7304 if (add_aux_fields && opd_ent_size == 16)
7305 {
7306 memset (wptr, '\0', 8);
7307 wptr += 8;
7308 }
7309 }
7310 rptr += opd_ent_size;
7311 offset += opd_ent_size;
7312 }
7313
7314 if (skip)
7315 {
7316 if (!NO_OPD_RELOCS
7317 && !info->relocatable
7318 && !dec_dynrel_count (rel->r_info, sec, info,
7319 NULL, h, sym_sec))
7320 goto error_ret;
7321 }
7322 else
7323 {
7324 /* We need to adjust any reloc offsets to point to the
7325 new opd entries. While we're at it, we may as well
7326 remove redundant relocs. */
7327 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7328 if (write_rel != rel)
7329 memcpy (write_rel, rel, sizeof (*rel));
7330 ++write_rel;
7331 }
7332 }
7333
7334 sec->size = wptr - new_contents;
7335 sec->reloc_count = write_rel - relstart;
7336 if (add_aux_fields)
7337 {
7338 free (sec->contents);
7339 sec->contents = new_contents;
7340 }
7341
7342 /* Fudge the header size too, as this is used later in
7343 elf_bfd_final_link if we are emitting relocs. */
7344 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7345 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7346 some_edited = TRUE;
7347 }
7348 else if (elf_section_data (sec)->relocs != relstart)
7349 free (relstart);
7350
7351 if (local_syms != NULL
7352 && symtab_hdr->contents != (unsigned char *) local_syms)
7353 {
7354 if (!info->keep_memory)
7355 free (local_syms);
7356 else
7357 symtab_hdr->contents = (unsigned char *) local_syms;
7358 }
7359 }
7360
7361 if (some_edited)
7362 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7363
7364 /* If we are doing a final link and the last .opd entry is just 16 byte
7365 long, add a 8 byte padding after it. */
7366 if (need_pad != NULL && !info->relocatable)
7367 {
7368 bfd_byte *p;
7369
7370 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7371 {
7372 BFD_ASSERT (need_pad->size > 0);
7373
7374 p = bfd_malloc (need_pad->size + 8);
7375 if (p == NULL)
7376 return FALSE;
7377
7378 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7379 p, 0, need_pad->size))
7380 return FALSE;
7381
7382 need_pad->contents = p;
7383 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7384 }
7385 else
7386 {
7387 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7388 if (p == NULL)
7389 return FALSE;
7390
7391 need_pad->contents = p;
7392 }
7393
7394 memset (need_pad->contents + need_pad->size, 0, 8);
7395 need_pad->size += 8;
7396 }
7397
7398 return TRUE;
7399 }
7400
7401 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7402
7403 asection *
7404 ppc64_elf_tls_setup (struct bfd_link_info *info,
7405 int no_tls_get_addr_opt,
7406 int *no_multi_toc)
7407 {
7408 struct ppc_link_hash_table *htab;
7409
7410 htab = ppc_hash_table (info);
7411 if (htab == NULL)
7412 return NULL;
7413
7414 if (*no_multi_toc)
7415 htab->do_multi_toc = 0;
7416 else if (!htab->do_multi_toc)
7417 *no_multi_toc = 1;
7418
7419 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7420 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7421 FALSE, FALSE, TRUE));
7422 /* Move dynamic linking info to the function descriptor sym. */
7423 if (htab->tls_get_addr != NULL)
7424 func_desc_adjust (&htab->tls_get_addr->elf, info);
7425 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7426 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7427 FALSE, FALSE, TRUE));
7428 if (!no_tls_get_addr_opt)
7429 {
7430 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7431
7432 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7433 FALSE, FALSE, TRUE);
7434 if (opt != NULL)
7435 func_desc_adjust (opt, info);
7436 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7437 FALSE, FALSE, TRUE);
7438 if (opt_fd != NULL
7439 && (opt_fd->root.type == bfd_link_hash_defined
7440 || opt_fd->root.type == bfd_link_hash_defweak))
7441 {
7442 /* If glibc supports an optimized __tls_get_addr call stub,
7443 signalled by the presence of __tls_get_addr_opt, and we'll
7444 be calling __tls_get_addr via a plt call stub, then
7445 make __tls_get_addr point to __tls_get_addr_opt. */
7446 tga_fd = &htab->tls_get_addr_fd->elf;
7447 if (htab->elf.dynamic_sections_created
7448 && tga_fd != NULL
7449 && (tga_fd->type == STT_FUNC
7450 || tga_fd->needs_plt)
7451 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7452 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7453 && tga_fd->root.type == bfd_link_hash_undefweak)))
7454 {
7455 struct plt_entry *ent;
7456
7457 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7458 if (ent->plt.refcount > 0)
7459 break;
7460 if (ent != NULL)
7461 {
7462 tga_fd->root.type = bfd_link_hash_indirect;
7463 tga_fd->root.u.i.link = &opt_fd->root;
7464 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7465 if (opt_fd->dynindx != -1)
7466 {
7467 /* Use __tls_get_addr_opt in dynamic relocations. */
7468 opt_fd->dynindx = -1;
7469 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7470 opt_fd->dynstr_index);
7471 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7472 return NULL;
7473 }
7474 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7475 tga = &htab->tls_get_addr->elf;
7476 if (opt != NULL && tga != NULL)
7477 {
7478 tga->root.type = bfd_link_hash_indirect;
7479 tga->root.u.i.link = &opt->root;
7480 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7481 _bfd_elf_link_hash_hide_symbol (info, opt,
7482 tga->forced_local);
7483 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7484 }
7485 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7486 htab->tls_get_addr_fd->is_func_descriptor = 1;
7487 if (htab->tls_get_addr != NULL)
7488 {
7489 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7490 htab->tls_get_addr->is_func = 1;
7491 }
7492 }
7493 }
7494 }
7495 else
7496 no_tls_get_addr_opt = TRUE;
7497 }
7498 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7499 return _bfd_elf_tls_setup (info->output_bfd, info);
7500 }
7501
7502 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7503 HASH1 or HASH2. */
7504
7505 static bfd_boolean
7506 branch_reloc_hash_match (const bfd *ibfd,
7507 const Elf_Internal_Rela *rel,
7508 const struct ppc_link_hash_entry *hash1,
7509 const struct ppc_link_hash_entry *hash2)
7510 {
7511 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7512 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7513 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7514
7515 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7516 {
7517 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7518 struct elf_link_hash_entry *h;
7519
7520 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7521 h = elf_follow_link (h);
7522 if (h == &hash1->elf || h == &hash2->elf)
7523 return TRUE;
7524 }
7525 return FALSE;
7526 }
7527
7528 /* Run through all the TLS relocs looking for optimization
7529 opportunities. The linker has been hacked (see ppc64elf.em) to do
7530 a preliminary section layout so that we know the TLS segment
7531 offsets. We can't optimize earlier because some optimizations need
7532 to know the tp offset, and we need to optimize before allocating
7533 dynamic relocations. */
7534
7535 bfd_boolean
7536 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7537 {
7538 bfd *ibfd;
7539 asection *sec;
7540 struct ppc_link_hash_table *htab;
7541 unsigned char *toc_ref;
7542 int pass;
7543
7544 if (info->relocatable || !info->executable)
7545 return TRUE;
7546
7547 htab = ppc_hash_table (info);
7548 if (htab == NULL)
7549 return FALSE;
7550
7551 /* Make two passes over the relocs. On the first pass, mark toc
7552 entries involved with tls relocs, and check that tls relocs
7553 involved in setting up a tls_get_addr call are indeed followed by
7554 such a call. If they are not, we can't do any tls optimization.
7555 On the second pass twiddle tls_mask flags to notify
7556 relocate_section that optimization can be done, and adjust got
7557 and plt refcounts. */
7558 toc_ref = NULL;
7559 for (pass = 0; pass < 2; ++pass)
7560 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7561 {
7562 Elf_Internal_Sym *locsyms = NULL;
7563 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7564
7565 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7566 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7567 {
7568 Elf_Internal_Rela *relstart, *rel, *relend;
7569 bfd_boolean found_tls_get_addr_arg = 0;
7570
7571 /* Read the relocations. */
7572 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7573 info->keep_memory);
7574 if (relstart == NULL)
7575 return FALSE;
7576
7577 relend = relstart + sec->reloc_count;
7578 for (rel = relstart; rel < relend; rel++)
7579 {
7580 enum elf_ppc64_reloc_type r_type;
7581 unsigned long r_symndx;
7582 struct elf_link_hash_entry *h;
7583 Elf_Internal_Sym *sym;
7584 asection *sym_sec;
7585 unsigned char *tls_mask;
7586 unsigned char tls_set, tls_clear, tls_type = 0;
7587 bfd_vma value;
7588 bfd_boolean ok_tprel, is_local;
7589 long toc_ref_index = 0;
7590 int expecting_tls_get_addr = 0;
7591 bfd_boolean ret = FALSE;
7592
7593 r_symndx = ELF64_R_SYM (rel->r_info);
7594 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7595 r_symndx, ibfd))
7596 {
7597 err_free_rel:
7598 if (elf_section_data (sec)->relocs != relstart)
7599 free (relstart);
7600 if (toc_ref != NULL)
7601 free (toc_ref);
7602 if (locsyms != NULL
7603 && (elf_symtab_hdr (ibfd).contents
7604 != (unsigned char *) locsyms))
7605 free (locsyms);
7606 return ret;
7607 }
7608
7609 if (h != NULL)
7610 {
7611 if (h->root.type == bfd_link_hash_defined
7612 || h->root.type == bfd_link_hash_defweak)
7613 value = h->root.u.def.value;
7614 else if (h->root.type == bfd_link_hash_undefweak)
7615 value = 0;
7616 else
7617 {
7618 found_tls_get_addr_arg = 0;
7619 continue;
7620 }
7621 }
7622 else
7623 /* Symbols referenced by TLS relocs must be of type
7624 STT_TLS. So no need for .opd local sym adjust. */
7625 value = sym->st_value;
7626
7627 ok_tprel = FALSE;
7628 is_local = FALSE;
7629 if (h == NULL
7630 || !h->def_dynamic)
7631 {
7632 is_local = TRUE;
7633 if (h != NULL
7634 && h->root.type == bfd_link_hash_undefweak)
7635 ok_tprel = TRUE;
7636 else
7637 {
7638 value += sym_sec->output_offset;
7639 value += sym_sec->output_section->vma;
7640 value -= htab->elf.tls_sec->vma;
7641 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7642 < (bfd_vma) 1 << 32);
7643 }
7644 }
7645
7646 r_type = ELF64_R_TYPE (rel->r_info);
7647 /* If this section has old-style __tls_get_addr calls
7648 without marker relocs, then check that each
7649 __tls_get_addr call reloc is preceded by a reloc
7650 that conceivably belongs to the __tls_get_addr arg
7651 setup insn. If we don't find matching arg setup
7652 relocs, don't do any tls optimization. */
7653 if (pass == 0
7654 && sec->has_tls_get_addr_call
7655 && h != NULL
7656 && (h == &htab->tls_get_addr->elf
7657 || h == &htab->tls_get_addr_fd->elf)
7658 && !found_tls_get_addr_arg
7659 && is_branch_reloc (r_type))
7660 {
7661 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7662 "TLS optimization disabled\n"),
7663 ibfd, sec, rel->r_offset);
7664 ret = TRUE;
7665 goto err_free_rel;
7666 }
7667
7668 found_tls_get_addr_arg = 0;
7669 switch (r_type)
7670 {
7671 case R_PPC64_GOT_TLSLD16:
7672 case R_PPC64_GOT_TLSLD16_LO:
7673 expecting_tls_get_addr = 1;
7674 found_tls_get_addr_arg = 1;
7675 /* Fall thru */
7676
7677 case R_PPC64_GOT_TLSLD16_HI:
7678 case R_PPC64_GOT_TLSLD16_HA:
7679 /* These relocs should never be against a symbol
7680 defined in a shared lib. Leave them alone if
7681 that turns out to be the case. */
7682 if (!is_local)
7683 continue;
7684
7685 /* LD -> LE */
7686 tls_set = 0;
7687 tls_clear = TLS_LD;
7688 tls_type = TLS_TLS | TLS_LD;
7689 break;
7690
7691 case R_PPC64_GOT_TLSGD16:
7692 case R_PPC64_GOT_TLSGD16_LO:
7693 expecting_tls_get_addr = 1;
7694 found_tls_get_addr_arg = 1;
7695 /* Fall thru */
7696
7697 case R_PPC64_GOT_TLSGD16_HI:
7698 case R_PPC64_GOT_TLSGD16_HA:
7699 if (ok_tprel)
7700 /* GD -> LE */
7701 tls_set = 0;
7702 else
7703 /* GD -> IE */
7704 tls_set = TLS_TLS | TLS_TPRELGD;
7705 tls_clear = TLS_GD;
7706 tls_type = TLS_TLS | TLS_GD;
7707 break;
7708
7709 case R_PPC64_GOT_TPREL16_DS:
7710 case R_PPC64_GOT_TPREL16_LO_DS:
7711 case R_PPC64_GOT_TPREL16_HI:
7712 case R_PPC64_GOT_TPREL16_HA:
7713 if (ok_tprel)
7714 {
7715 /* IE -> LE */
7716 tls_set = 0;
7717 tls_clear = TLS_TPREL;
7718 tls_type = TLS_TLS | TLS_TPREL;
7719 break;
7720 }
7721 continue;
7722
7723 case R_PPC64_TLSGD:
7724 case R_PPC64_TLSLD:
7725 found_tls_get_addr_arg = 1;
7726 /* Fall thru */
7727
7728 case R_PPC64_TLS:
7729 case R_PPC64_TOC16:
7730 case R_PPC64_TOC16_LO:
7731 if (sym_sec == NULL || sym_sec != toc)
7732 continue;
7733
7734 /* Mark this toc entry as referenced by a TLS
7735 code sequence. We can do that now in the
7736 case of R_PPC64_TLS, and after checking for
7737 tls_get_addr for the TOC16 relocs. */
7738 if (toc_ref == NULL)
7739 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7740 if (toc_ref == NULL)
7741 goto err_free_rel;
7742
7743 if (h != NULL)
7744 value = h->root.u.def.value;
7745 else
7746 value = sym->st_value;
7747 value += rel->r_addend;
7748 BFD_ASSERT (value < toc->size && value % 8 == 0);
7749 toc_ref_index = (value + toc->output_offset) / 8;
7750 if (r_type == R_PPC64_TLS
7751 || r_type == R_PPC64_TLSGD
7752 || r_type == R_PPC64_TLSLD)
7753 {
7754 toc_ref[toc_ref_index] = 1;
7755 continue;
7756 }
7757
7758 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7759 continue;
7760
7761 tls_set = 0;
7762 tls_clear = 0;
7763 expecting_tls_get_addr = 2;
7764 break;
7765
7766 case R_PPC64_TPREL64:
7767 if (pass == 0
7768 || sec != toc
7769 || toc_ref == NULL
7770 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7771 continue;
7772 if (ok_tprel)
7773 {
7774 /* IE -> LE */
7775 tls_set = TLS_EXPLICIT;
7776 tls_clear = TLS_TPREL;
7777 break;
7778 }
7779 continue;
7780
7781 case R_PPC64_DTPMOD64:
7782 if (pass == 0
7783 || sec != toc
7784 || toc_ref == NULL
7785 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7786 continue;
7787 if (rel + 1 < relend
7788 && (rel[1].r_info
7789 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7790 && rel[1].r_offset == rel->r_offset + 8)
7791 {
7792 if (ok_tprel)
7793 /* GD -> LE */
7794 tls_set = TLS_EXPLICIT | TLS_GD;
7795 else
7796 /* GD -> IE */
7797 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7798 tls_clear = TLS_GD;
7799 }
7800 else
7801 {
7802 if (!is_local)
7803 continue;
7804
7805 /* LD -> LE */
7806 tls_set = TLS_EXPLICIT;
7807 tls_clear = TLS_LD;
7808 }
7809 break;
7810
7811 default:
7812 continue;
7813 }
7814
7815 if (pass == 0)
7816 {
7817 if (!expecting_tls_get_addr
7818 || !sec->has_tls_get_addr_call)
7819 continue;
7820
7821 if (rel + 1 < relend
7822 && branch_reloc_hash_match (ibfd, rel + 1,
7823 htab->tls_get_addr,
7824 htab->tls_get_addr_fd))
7825 {
7826 if (expecting_tls_get_addr == 2)
7827 {
7828 /* Check for toc tls entries. */
7829 unsigned char *toc_tls;
7830 int retval;
7831
7832 retval = get_tls_mask (&toc_tls, NULL, NULL,
7833 &locsyms,
7834 rel, ibfd);
7835 if (retval == 0)
7836 goto err_free_rel;
7837 if (toc_tls != NULL)
7838 {
7839 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7840 found_tls_get_addr_arg = 1;
7841 if (retval > 1)
7842 toc_ref[toc_ref_index] = 1;
7843 }
7844 }
7845 continue;
7846 }
7847
7848 if (expecting_tls_get_addr != 1)
7849 continue;
7850
7851 /* Uh oh, we didn't find the expected call. We
7852 could just mark this symbol to exclude it
7853 from tls optimization but it's safer to skip
7854 the entire optimization. */
7855 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7856 "TLS optimization disabled\n"),
7857 ibfd, sec, rel->r_offset);
7858 ret = TRUE;
7859 goto err_free_rel;
7860 }
7861
7862 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7863 {
7864 struct plt_entry *ent;
7865 for (ent = htab->tls_get_addr->elf.plt.plist;
7866 ent != NULL;
7867 ent = ent->next)
7868 if (ent->addend == 0)
7869 {
7870 if (ent->plt.refcount > 0)
7871 {
7872 ent->plt.refcount -= 1;
7873 expecting_tls_get_addr = 0;
7874 }
7875 break;
7876 }
7877 }
7878
7879 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7880 {
7881 struct plt_entry *ent;
7882 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7883 ent != NULL;
7884 ent = ent->next)
7885 if (ent->addend == 0)
7886 {
7887 if (ent->plt.refcount > 0)
7888 ent->plt.refcount -= 1;
7889 break;
7890 }
7891 }
7892
7893 if (tls_clear == 0)
7894 continue;
7895
7896 if ((tls_set & TLS_EXPLICIT) == 0)
7897 {
7898 struct got_entry *ent;
7899
7900 /* Adjust got entry for this reloc. */
7901 if (h != NULL)
7902 ent = h->got.glist;
7903 else
7904 ent = elf_local_got_ents (ibfd)[r_symndx];
7905
7906 for (; ent != NULL; ent = ent->next)
7907 if (ent->addend == rel->r_addend
7908 && ent->owner == ibfd
7909 && ent->tls_type == tls_type)
7910 break;
7911 if (ent == NULL)
7912 abort ();
7913
7914 if (tls_set == 0)
7915 {
7916 /* We managed to get rid of a got entry. */
7917 if (ent->got.refcount > 0)
7918 ent->got.refcount -= 1;
7919 }
7920 }
7921 else
7922 {
7923 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7924 we'll lose one or two dyn relocs. */
7925 if (!dec_dynrel_count (rel->r_info, sec, info,
7926 NULL, h, sym_sec))
7927 return FALSE;
7928
7929 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7930 {
7931 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7932 NULL, h, sym_sec))
7933 return FALSE;
7934 }
7935 }
7936
7937 *tls_mask |= tls_set;
7938 *tls_mask &= ~tls_clear;
7939 }
7940
7941 if (elf_section_data (sec)->relocs != relstart)
7942 free (relstart);
7943 }
7944
7945 if (locsyms != NULL
7946 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7947 {
7948 if (!info->keep_memory)
7949 free (locsyms);
7950 else
7951 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7952 }
7953 }
7954
7955 if (toc_ref != NULL)
7956 free (toc_ref);
7957 return TRUE;
7958 }
7959
7960 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7961 the values of any global symbols in a toc section that has been
7962 edited. Globals in toc sections should be a rarity, so this function
7963 sets a flag if any are found in toc sections other than the one just
7964 edited, so that futher hash table traversals can be avoided. */
7965
7966 struct adjust_toc_info
7967 {
7968 asection *toc;
7969 unsigned long *skip;
7970 bfd_boolean global_toc_syms;
7971 };
7972
7973 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7974
7975 static bfd_boolean
7976 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7977 {
7978 struct ppc_link_hash_entry *eh;
7979 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7980 unsigned long i;
7981
7982 if (h->root.type != bfd_link_hash_defined
7983 && h->root.type != bfd_link_hash_defweak)
7984 return TRUE;
7985
7986 eh = (struct ppc_link_hash_entry *) h;
7987 if (eh->adjust_done)
7988 return TRUE;
7989
7990 if (eh->elf.root.u.def.section == toc_inf->toc)
7991 {
7992 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7993 i = toc_inf->toc->rawsize >> 3;
7994 else
7995 i = eh->elf.root.u.def.value >> 3;
7996
7997 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7998 {
7999 (*_bfd_error_handler)
8000 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8001 do
8002 ++i;
8003 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8004 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8005 }
8006
8007 eh->elf.root.u.def.value -= toc_inf->skip[i];
8008 eh->adjust_done = 1;
8009 }
8010 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8011 toc_inf->global_toc_syms = TRUE;
8012
8013 return TRUE;
8014 }
8015
8016 /* Examine all relocs referencing .toc sections in order to remove
8017 unused .toc entries. */
8018
8019 bfd_boolean
8020 ppc64_elf_edit_toc (struct bfd_link_info *info)
8021 {
8022 bfd *ibfd;
8023 struct adjust_toc_info toc_inf;
8024 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8025
8026 htab->do_toc_opt = 1;
8027 toc_inf.global_toc_syms = TRUE;
8028 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8029 {
8030 asection *toc, *sec;
8031 Elf_Internal_Shdr *symtab_hdr;
8032 Elf_Internal_Sym *local_syms;
8033 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8034 unsigned long *skip, *drop;
8035 unsigned char *used;
8036 unsigned char *keep, last, some_unused;
8037
8038 if (!is_ppc64_elf (ibfd))
8039 continue;
8040
8041 toc = bfd_get_section_by_name (ibfd, ".toc");
8042 if (toc == NULL
8043 || toc->size == 0
8044 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
8045 || elf_discarded_section (toc))
8046 continue;
8047
8048 toc_relocs = NULL;
8049 local_syms = NULL;
8050 symtab_hdr = &elf_symtab_hdr (ibfd);
8051
8052 /* Look at sections dropped from the final link. */
8053 skip = NULL;
8054 relstart = NULL;
8055 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8056 {
8057 if (sec->reloc_count == 0
8058 || !elf_discarded_section (sec)
8059 || get_opd_info (sec)
8060 || (sec->flags & SEC_ALLOC) == 0
8061 || (sec->flags & SEC_DEBUGGING) != 0)
8062 continue;
8063
8064 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8065 if (relstart == NULL)
8066 goto error_ret;
8067
8068 /* Run through the relocs to see which toc entries might be
8069 unused. */
8070 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8071 {
8072 enum elf_ppc64_reloc_type r_type;
8073 unsigned long r_symndx;
8074 asection *sym_sec;
8075 struct elf_link_hash_entry *h;
8076 Elf_Internal_Sym *sym;
8077 bfd_vma val;
8078
8079 r_type = ELF64_R_TYPE (rel->r_info);
8080 switch (r_type)
8081 {
8082 default:
8083 continue;
8084
8085 case R_PPC64_TOC16:
8086 case R_PPC64_TOC16_LO:
8087 case R_PPC64_TOC16_HI:
8088 case R_PPC64_TOC16_HA:
8089 case R_PPC64_TOC16_DS:
8090 case R_PPC64_TOC16_LO_DS:
8091 break;
8092 }
8093
8094 r_symndx = ELF64_R_SYM (rel->r_info);
8095 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8096 r_symndx, ibfd))
8097 goto error_ret;
8098
8099 if (sym_sec != toc)
8100 continue;
8101
8102 if (h != NULL)
8103 val = h->root.u.def.value;
8104 else
8105 val = sym->st_value;
8106 val += rel->r_addend;
8107
8108 if (val >= toc->size)
8109 continue;
8110
8111 /* Anything in the toc ought to be aligned to 8 bytes.
8112 If not, don't mark as unused. */
8113 if (val & 7)
8114 continue;
8115
8116 if (skip == NULL)
8117 {
8118 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8119 if (skip == NULL)
8120 goto error_ret;
8121 }
8122
8123 skip[val >> 3] = ref_from_discarded;
8124 }
8125
8126 if (elf_section_data (sec)->relocs != relstart)
8127 free (relstart);
8128 }
8129
8130 /* For largetoc loads of address constants, we can convert
8131 . addis rx,2,addr@got@ha
8132 . ld ry,addr@got@l(rx)
8133 to
8134 . addis rx,2,addr@toc@ha
8135 . addi ry,rx,addr@toc@l
8136 when addr is within 2G of the toc pointer. This then means
8137 that the word storing "addr" in the toc is no longer needed. */
8138
8139 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8140 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8141 && toc->reloc_count != 0)
8142 {
8143 /* Read toc relocs. */
8144 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8145 info->keep_memory);
8146 if (toc_relocs == NULL)
8147 goto error_ret;
8148
8149 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8150 {
8151 enum elf_ppc64_reloc_type r_type;
8152 unsigned long r_symndx;
8153 asection *sym_sec;
8154 struct elf_link_hash_entry *h;
8155 Elf_Internal_Sym *sym;
8156 bfd_vma val, addr;
8157
8158 r_type = ELF64_R_TYPE (rel->r_info);
8159 if (r_type != R_PPC64_ADDR64)
8160 continue;
8161
8162 r_symndx = ELF64_R_SYM (rel->r_info);
8163 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8164 r_symndx, ibfd))
8165 goto error_ret;
8166
8167 if (sym_sec == NULL
8168 || elf_discarded_section (sym_sec))
8169 continue;
8170
8171 if (!SYMBOL_CALLS_LOCAL (info, h))
8172 continue;
8173
8174 if (h != NULL)
8175 {
8176 if (h->type == STT_GNU_IFUNC)
8177 continue;
8178 val = h->root.u.def.value;
8179 }
8180 else
8181 {
8182 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8183 continue;
8184 val = sym->st_value;
8185 }
8186 val += rel->r_addend;
8187 val += sym_sec->output_section->vma + sym_sec->output_offset;
8188
8189 /* We don't yet know the exact toc pointer value, but we
8190 know it will be somewhere in the toc section. Don't
8191 optimize if the difference from any possible toc
8192 pointer is outside [ff..f80008000, 7fff7fff]. */
8193 addr = toc->output_section->vma + TOC_BASE_OFF;
8194 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8195 continue;
8196
8197 addr = toc->output_section->vma + toc->output_section->rawsize;
8198 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8199 continue;
8200
8201 if (skip == NULL)
8202 {
8203 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8204 if (skip == NULL)
8205 goto error_ret;
8206 }
8207
8208 skip[rel->r_offset >> 3]
8209 |= can_optimize | ((rel - toc_relocs) << 2);
8210 }
8211 }
8212
8213 if (skip == NULL)
8214 continue;
8215
8216 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8217 if (used == NULL)
8218 {
8219 error_ret:
8220 if (local_syms != NULL
8221 && symtab_hdr->contents != (unsigned char *) local_syms)
8222 free (local_syms);
8223 if (sec != NULL
8224 && relstart != NULL
8225 && elf_section_data (sec)->relocs != relstart)
8226 free (relstart);
8227 if (toc_relocs != NULL
8228 && elf_section_data (toc)->relocs != toc_relocs)
8229 free (toc_relocs);
8230 if (skip != NULL)
8231 free (skip);
8232 return FALSE;
8233 }
8234
8235 /* Now check all kept sections that might reference the toc.
8236 Check the toc itself last. */
8237 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8238 : ibfd->sections);
8239 sec != NULL;
8240 sec = (sec == toc ? NULL
8241 : sec->next == NULL ? toc
8242 : sec->next == toc && toc->next ? toc->next
8243 : sec->next))
8244 {
8245 int repeat;
8246
8247 if (sec->reloc_count == 0
8248 || elf_discarded_section (sec)
8249 || get_opd_info (sec)
8250 || (sec->flags & SEC_ALLOC) == 0
8251 || (sec->flags & SEC_DEBUGGING) != 0)
8252 continue;
8253
8254 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8255 info->keep_memory);
8256 if (relstart == NULL)
8257 goto error_ret;
8258
8259 /* Mark toc entries referenced as used. */
8260 repeat = 0;
8261 do
8262 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8263 {
8264 enum elf_ppc64_reloc_type r_type;
8265 unsigned long r_symndx;
8266 asection *sym_sec;
8267 struct elf_link_hash_entry *h;
8268 Elf_Internal_Sym *sym;
8269 bfd_vma val;
8270
8271 r_type = ELF64_R_TYPE (rel->r_info);
8272 switch (r_type)
8273 {
8274 default:
8275 break;
8276
8277 case R_PPC64_GOT_TLSLD16_HA:
8278 case R_PPC64_GOT_TLSGD16_HA:
8279 case R_PPC64_GOT_TPREL16_HA:
8280 case R_PPC64_GOT_DTPREL16_HA:
8281 case R_PPC64_GOT16_HA:
8282 case R_PPC64_TOC16_HA:
8283 {
8284 bfd_vma off = rel->r_offset & ~3;
8285 unsigned char buf[4];
8286 unsigned int insn;
8287
8288 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8289 {
8290 free (used);
8291 goto error_ret;
8292 }
8293 insn = bfd_get_32 (ibfd, buf);
8294 if ((insn & ((0x3f << 26) | 0x1f << 16))
8295 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8296 ppc64_elf_tdata (ibfd)->ha_relocs_not_using_r2 = 1;
8297 }
8298 break;
8299 }
8300
8301 switch (r_type)
8302 {
8303 case R_PPC64_TOC16:
8304 case R_PPC64_TOC16_LO:
8305 case R_PPC64_TOC16_HI:
8306 case R_PPC64_TOC16_HA:
8307 case R_PPC64_TOC16_DS:
8308 case R_PPC64_TOC16_LO_DS:
8309 /* In case we're taking addresses of toc entries. */
8310 case R_PPC64_ADDR64:
8311 break;
8312
8313 default:
8314 continue;
8315 }
8316
8317 r_symndx = ELF64_R_SYM (rel->r_info);
8318 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8319 r_symndx, ibfd))
8320 {
8321 free (used);
8322 goto error_ret;
8323 }
8324
8325 if (sym_sec != toc)
8326 continue;
8327
8328 if (h != NULL)
8329 val = h->root.u.def.value;
8330 else
8331 val = sym->st_value;
8332 val += rel->r_addend;
8333
8334 if (val >= toc->size)
8335 continue;
8336
8337 if ((skip[val >> 3] & can_optimize) != 0)
8338 {
8339 bfd_vma off;
8340 unsigned char opc;
8341
8342 switch (r_type)
8343 {
8344 case R_PPC64_TOC16_HA:
8345 break;
8346
8347 case R_PPC64_TOC16_LO_DS:
8348 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8349 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8350 {
8351 free (used);
8352 goto error_ret;
8353 }
8354 if ((opc & (0x3f << 2)) == (58u << 2))
8355 break;
8356 /* Fall thru */
8357
8358 default:
8359 /* Wrong sort of reloc, or not a ld. We may
8360 as well clear ref_from_discarded too. */
8361 skip[val >> 3] = 0;
8362 }
8363 }
8364
8365 /* For the toc section, we only mark as used if
8366 this entry itself isn't unused. */
8367 if (sec == toc
8368 && !used[val >> 3]
8369 && (used[rel->r_offset >> 3]
8370 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8371 /* Do all the relocs again, to catch reference
8372 chains. */
8373 repeat = 1;
8374
8375 used[val >> 3] = 1;
8376 }
8377 while (repeat);
8378
8379 if (elf_section_data (sec)->relocs != relstart)
8380 free (relstart);
8381 }
8382
8383 /* Merge the used and skip arrays. Assume that TOC
8384 doublewords not appearing as either used or unused belong
8385 to to an entry more than one doubleword in size. */
8386 for (drop = skip, keep = used, last = 0, some_unused = 0;
8387 drop < skip + (toc->size + 7) / 8;
8388 ++drop, ++keep)
8389 {
8390 if (*keep)
8391 {
8392 *drop &= ~ref_from_discarded;
8393 if ((*drop & can_optimize) != 0)
8394 some_unused = 1;
8395 last = 0;
8396 }
8397 else if ((*drop & ref_from_discarded) != 0)
8398 {
8399 some_unused = 1;
8400 last = ref_from_discarded;
8401 }
8402 else
8403 *drop = last;
8404 }
8405
8406 free (used);
8407
8408 if (some_unused)
8409 {
8410 bfd_byte *contents, *src;
8411 unsigned long off;
8412 Elf_Internal_Sym *sym;
8413 bfd_boolean local_toc_syms = FALSE;
8414
8415 /* Shuffle the toc contents, and at the same time convert the
8416 skip array from booleans into offsets. */
8417 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8418 goto error_ret;
8419
8420 elf_section_data (toc)->this_hdr.contents = contents;
8421
8422 for (src = contents, off = 0, drop = skip;
8423 src < contents + toc->size;
8424 src += 8, ++drop)
8425 {
8426 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8427 off += 8;
8428 else if (off != 0)
8429 {
8430 *drop = off;
8431 memcpy (src - off, src, 8);
8432 }
8433 }
8434 *drop = off;
8435 toc->rawsize = toc->size;
8436 toc->size = src - contents - off;
8437
8438 /* Adjust addends for relocs against the toc section sym,
8439 and optimize any accesses we can. */
8440 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8441 {
8442 if (sec->reloc_count == 0
8443 || elf_discarded_section (sec))
8444 continue;
8445
8446 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8447 info->keep_memory);
8448 if (relstart == NULL)
8449 goto error_ret;
8450
8451 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8452 {
8453 enum elf_ppc64_reloc_type r_type;
8454 unsigned long r_symndx;
8455 asection *sym_sec;
8456 struct elf_link_hash_entry *h;
8457 bfd_vma val;
8458
8459 r_type = ELF64_R_TYPE (rel->r_info);
8460 switch (r_type)
8461 {
8462 default:
8463 continue;
8464
8465 case R_PPC64_TOC16:
8466 case R_PPC64_TOC16_LO:
8467 case R_PPC64_TOC16_HI:
8468 case R_PPC64_TOC16_HA:
8469 case R_PPC64_TOC16_DS:
8470 case R_PPC64_TOC16_LO_DS:
8471 case R_PPC64_ADDR64:
8472 break;
8473 }
8474
8475 r_symndx = ELF64_R_SYM (rel->r_info);
8476 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8477 r_symndx, ibfd))
8478 goto error_ret;
8479
8480 if (sym_sec != toc)
8481 continue;
8482
8483 if (h != NULL)
8484 val = h->root.u.def.value;
8485 else
8486 {
8487 val = sym->st_value;
8488 if (val != 0)
8489 local_toc_syms = TRUE;
8490 }
8491
8492 val += rel->r_addend;
8493
8494 if (val > toc->rawsize)
8495 val = toc->rawsize;
8496 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8497 continue;
8498 else if ((skip[val >> 3] & can_optimize) != 0)
8499 {
8500 Elf_Internal_Rela *tocrel
8501 = toc_relocs + (skip[val >> 3] >> 2);
8502 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8503
8504 switch (r_type)
8505 {
8506 case R_PPC64_TOC16_HA:
8507 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8508 break;
8509
8510 case R_PPC64_TOC16_LO_DS:
8511 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8512 break;
8513
8514 default:
8515 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8516 ppc_howto_init ();
8517 info->callbacks->einfo
8518 (_("%P: %H: %s relocation references "
8519 "optimized away TOC entry\n"),
8520 ibfd, sec, rel->r_offset,
8521 ppc64_elf_howto_table[r_type]->name);
8522 bfd_set_error (bfd_error_bad_value);
8523 goto error_ret;
8524 }
8525 rel->r_addend = tocrel->r_addend;
8526 elf_section_data (sec)->relocs = relstart;
8527 continue;
8528 }
8529
8530 if (h != NULL || sym->st_value != 0)
8531 continue;
8532
8533 rel->r_addend -= skip[val >> 3];
8534 elf_section_data (sec)->relocs = relstart;
8535 }
8536
8537 if (elf_section_data (sec)->relocs != relstart)
8538 free (relstart);
8539 }
8540
8541 /* We shouldn't have local or global symbols defined in the TOC,
8542 but handle them anyway. */
8543 if (local_syms != NULL)
8544 for (sym = local_syms;
8545 sym < local_syms + symtab_hdr->sh_info;
8546 ++sym)
8547 if (sym->st_value != 0
8548 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8549 {
8550 unsigned long i;
8551
8552 if (sym->st_value > toc->rawsize)
8553 i = toc->rawsize >> 3;
8554 else
8555 i = sym->st_value >> 3;
8556
8557 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8558 {
8559 if (local_toc_syms)
8560 (*_bfd_error_handler)
8561 (_("%s defined on removed toc entry"),
8562 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8563 do
8564 ++i;
8565 while ((skip[i] & (ref_from_discarded | can_optimize)));
8566 sym->st_value = (bfd_vma) i << 3;
8567 }
8568
8569 sym->st_value -= skip[i];
8570 symtab_hdr->contents = (unsigned char *) local_syms;
8571 }
8572
8573 /* Adjust any global syms defined in this toc input section. */
8574 if (toc_inf.global_toc_syms)
8575 {
8576 toc_inf.toc = toc;
8577 toc_inf.skip = skip;
8578 toc_inf.global_toc_syms = FALSE;
8579 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8580 &toc_inf);
8581 }
8582
8583 if (toc->reloc_count != 0)
8584 {
8585 Elf_Internal_Shdr *rel_hdr;
8586 Elf_Internal_Rela *wrel;
8587 bfd_size_type sz;
8588
8589 /* Remove unused toc relocs, and adjust those we keep. */
8590 if (toc_relocs == NULL)
8591 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8592 info->keep_memory);
8593 if (toc_relocs == NULL)
8594 goto error_ret;
8595
8596 wrel = toc_relocs;
8597 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8598 if ((skip[rel->r_offset >> 3]
8599 & (ref_from_discarded | can_optimize)) == 0)
8600 {
8601 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8602 wrel->r_info = rel->r_info;
8603 wrel->r_addend = rel->r_addend;
8604 ++wrel;
8605 }
8606 else if (!dec_dynrel_count (rel->r_info, toc, info,
8607 &local_syms, NULL, NULL))
8608 goto error_ret;
8609
8610 elf_section_data (toc)->relocs = toc_relocs;
8611 toc->reloc_count = wrel - toc_relocs;
8612 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8613 sz = rel_hdr->sh_entsize;
8614 rel_hdr->sh_size = toc->reloc_count * sz;
8615 }
8616 }
8617 else if (toc_relocs != NULL
8618 && elf_section_data (toc)->relocs != toc_relocs)
8619 free (toc_relocs);
8620
8621 if (local_syms != NULL
8622 && symtab_hdr->contents != (unsigned char *) local_syms)
8623 {
8624 if (!info->keep_memory)
8625 free (local_syms);
8626 else
8627 symtab_hdr->contents = (unsigned char *) local_syms;
8628 }
8629 free (skip);
8630 }
8631
8632 return TRUE;
8633 }
8634
8635 /* Return true iff input section I references the TOC using
8636 instructions limited to +/-32k offsets. */
8637
8638 bfd_boolean
8639 ppc64_elf_has_small_toc_reloc (asection *i)
8640 {
8641 return (is_ppc64_elf (i->owner)
8642 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8643 }
8644
8645 /* Allocate space for one GOT entry. */
8646
8647 static void
8648 allocate_got (struct elf_link_hash_entry *h,
8649 struct bfd_link_info *info,
8650 struct got_entry *gent)
8651 {
8652 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8653 bfd_boolean dyn;
8654 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8655 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8656 ? 16 : 8);
8657 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8658 ? 2 : 1) * sizeof (Elf64_External_Rela);
8659 asection *got = ppc64_elf_tdata (gent->owner)->got;
8660
8661 gent->got.offset = got->size;
8662 got->size += entsize;
8663
8664 dyn = htab->elf.dynamic_sections_created;
8665 if ((info->shared
8666 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8667 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8668 || h->root.type != bfd_link_hash_undefweak))
8669 {
8670 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8671 relgot->size += rentsize;
8672 }
8673 else if (h->type == STT_GNU_IFUNC)
8674 {
8675 asection *relgot = htab->reliplt;
8676 relgot->size += rentsize;
8677 htab->got_reli_size += rentsize;
8678 }
8679 }
8680
8681 /* This function merges got entries in the same toc group. */
8682
8683 static void
8684 merge_got_entries (struct got_entry **pent)
8685 {
8686 struct got_entry *ent, *ent2;
8687
8688 for (ent = *pent; ent != NULL; ent = ent->next)
8689 if (!ent->is_indirect)
8690 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8691 if (!ent2->is_indirect
8692 && ent2->addend == ent->addend
8693 && ent2->tls_type == ent->tls_type
8694 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8695 {
8696 ent2->is_indirect = TRUE;
8697 ent2->got.ent = ent;
8698 }
8699 }
8700
8701 /* Allocate space in .plt, .got and associated reloc sections for
8702 dynamic relocs. */
8703
8704 static bfd_boolean
8705 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8706 {
8707 struct bfd_link_info *info;
8708 struct ppc_link_hash_table *htab;
8709 asection *s;
8710 struct ppc_link_hash_entry *eh;
8711 struct elf_dyn_relocs *p;
8712 struct got_entry **pgent, *gent;
8713
8714 if (h->root.type == bfd_link_hash_indirect)
8715 return TRUE;
8716
8717 info = (struct bfd_link_info *) inf;
8718 htab = ppc_hash_table (info);
8719 if (htab == NULL)
8720 return FALSE;
8721
8722 if ((htab->elf.dynamic_sections_created
8723 && h->dynindx != -1
8724 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8725 || h->type == STT_GNU_IFUNC)
8726 {
8727 struct plt_entry *pent;
8728 bfd_boolean doneone = FALSE;
8729 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8730 if (pent->plt.refcount > 0)
8731 {
8732 if (!htab->elf.dynamic_sections_created
8733 || h->dynindx == -1)
8734 {
8735 s = htab->iplt;
8736 pent->plt.offset = s->size;
8737 s->size += PLT_ENTRY_SIZE;
8738 s = htab->reliplt;
8739 }
8740 else
8741 {
8742 /* If this is the first .plt entry, make room for the special
8743 first entry. */
8744 s = htab->plt;
8745 if (s->size == 0)
8746 s->size += PLT_INITIAL_ENTRY_SIZE;
8747
8748 pent->plt.offset = s->size;
8749
8750 /* Make room for this entry. */
8751 s->size += PLT_ENTRY_SIZE;
8752
8753 /* Make room for the .glink code. */
8754 s = htab->glink;
8755 if (s->size == 0)
8756 s->size += GLINK_CALL_STUB_SIZE;
8757 /* We need bigger stubs past index 32767. */
8758 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8759 s->size += 4;
8760 s->size += 2*4;
8761
8762 /* We also need to make an entry in the .rela.plt section. */
8763 s = htab->relplt;
8764 }
8765 s->size += sizeof (Elf64_External_Rela);
8766 doneone = TRUE;
8767 }
8768 else
8769 pent->plt.offset = (bfd_vma) -1;
8770 if (!doneone)
8771 {
8772 h->plt.plist = NULL;
8773 h->needs_plt = 0;
8774 }
8775 }
8776 else
8777 {
8778 h->plt.plist = NULL;
8779 h->needs_plt = 0;
8780 }
8781
8782 eh = (struct ppc_link_hash_entry *) h;
8783 /* Run through the TLS GD got entries first if we're changing them
8784 to TPREL. */
8785 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8786 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8787 if (gent->got.refcount > 0
8788 && (gent->tls_type & TLS_GD) != 0)
8789 {
8790 /* This was a GD entry that has been converted to TPREL. If
8791 there happens to be a TPREL entry we can use that one. */
8792 struct got_entry *ent;
8793 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8794 if (ent->got.refcount > 0
8795 && (ent->tls_type & TLS_TPREL) != 0
8796 && ent->addend == gent->addend
8797 && ent->owner == gent->owner)
8798 {
8799 gent->got.refcount = 0;
8800 break;
8801 }
8802
8803 /* If not, then we'll be using our own TPREL entry. */
8804 if (gent->got.refcount != 0)
8805 gent->tls_type = TLS_TLS | TLS_TPREL;
8806 }
8807
8808 /* Remove any list entry that won't generate a word in the GOT before
8809 we call merge_got_entries. Otherwise we risk merging to empty
8810 entries. */
8811 pgent = &h->got.glist;
8812 while ((gent = *pgent) != NULL)
8813 if (gent->got.refcount > 0)
8814 {
8815 if ((gent->tls_type & TLS_LD) != 0
8816 && !h->def_dynamic)
8817 {
8818 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8819 *pgent = gent->next;
8820 }
8821 else
8822 pgent = &gent->next;
8823 }
8824 else
8825 *pgent = gent->next;
8826
8827 if (!htab->do_multi_toc)
8828 merge_got_entries (&h->got.glist);
8829
8830 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8831 if (!gent->is_indirect)
8832 {
8833 /* Make sure this symbol is output as a dynamic symbol.
8834 Undefined weak syms won't yet be marked as dynamic,
8835 nor will all TLS symbols. */
8836 if (h->dynindx == -1
8837 && !h->forced_local
8838 && h->type != STT_GNU_IFUNC
8839 && htab->elf.dynamic_sections_created)
8840 {
8841 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8842 return FALSE;
8843 }
8844
8845 if (!is_ppc64_elf (gent->owner))
8846 abort ();
8847
8848 allocate_got (h, info, gent);
8849 }
8850
8851 if (eh->dyn_relocs == NULL
8852 || (!htab->elf.dynamic_sections_created
8853 && h->type != STT_GNU_IFUNC))
8854 return TRUE;
8855
8856 /* In the shared -Bsymbolic case, discard space allocated for
8857 dynamic pc-relative relocs against symbols which turn out to be
8858 defined in regular objects. For the normal shared case, discard
8859 space for relocs that have become local due to symbol visibility
8860 changes. */
8861
8862 if (info->shared)
8863 {
8864 /* Relocs that use pc_count are those that appear on a call insn,
8865 or certain REL relocs (see must_be_dyn_reloc) that can be
8866 generated via assembly. We want calls to protected symbols to
8867 resolve directly to the function rather than going via the plt.
8868 If people want function pointer comparisons to work as expected
8869 then they should avoid writing weird assembly. */
8870 if (SYMBOL_CALLS_LOCAL (info, h))
8871 {
8872 struct elf_dyn_relocs **pp;
8873
8874 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8875 {
8876 p->count -= p->pc_count;
8877 p->pc_count = 0;
8878 if (p->count == 0)
8879 *pp = p->next;
8880 else
8881 pp = &p->next;
8882 }
8883 }
8884
8885 /* Also discard relocs on undefined weak syms with non-default
8886 visibility. */
8887 if (eh->dyn_relocs != NULL
8888 && h->root.type == bfd_link_hash_undefweak)
8889 {
8890 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8891 eh->dyn_relocs = NULL;
8892
8893 /* Make sure this symbol is output as a dynamic symbol.
8894 Undefined weak syms won't yet be marked as dynamic. */
8895 else if (h->dynindx == -1
8896 && !h->forced_local)
8897 {
8898 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8899 return FALSE;
8900 }
8901 }
8902 }
8903 else if (h->type == STT_GNU_IFUNC)
8904 {
8905 if (!h->non_got_ref)
8906 eh->dyn_relocs = NULL;
8907 }
8908 else if (ELIMINATE_COPY_RELOCS)
8909 {
8910 /* For the non-shared case, discard space for relocs against
8911 symbols which turn out to need copy relocs or are not
8912 dynamic. */
8913
8914 if (!h->non_got_ref
8915 && !h->def_regular)
8916 {
8917 /* Make sure this symbol is output as a dynamic symbol.
8918 Undefined weak syms won't yet be marked as dynamic. */
8919 if (h->dynindx == -1
8920 && !h->forced_local)
8921 {
8922 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8923 return FALSE;
8924 }
8925
8926 /* If that succeeded, we know we'll be keeping all the
8927 relocs. */
8928 if (h->dynindx != -1)
8929 goto keep;
8930 }
8931
8932 eh->dyn_relocs = NULL;
8933
8934 keep: ;
8935 }
8936
8937 /* Finally, allocate space. */
8938 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8939 {
8940 asection *sreloc = elf_section_data (p->sec)->sreloc;
8941 if (!htab->elf.dynamic_sections_created)
8942 sreloc = htab->reliplt;
8943 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8944 }
8945
8946 return TRUE;
8947 }
8948
8949 /* Find any dynamic relocs that apply to read-only sections. */
8950
8951 static bfd_boolean
8952 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8953 {
8954 struct ppc_link_hash_entry *eh;
8955 struct elf_dyn_relocs *p;
8956
8957 eh = (struct ppc_link_hash_entry *) h;
8958 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8959 {
8960 asection *s = p->sec->output_section;
8961
8962 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8963 {
8964 struct bfd_link_info *info = inf;
8965
8966 info->flags |= DF_TEXTREL;
8967
8968 /* Not an error, just cut short the traversal. */
8969 return FALSE;
8970 }
8971 }
8972 return TRUE;
8973 }
8974
8975 /* Set the sizes of the dynamic sections. */
8976
8977 static bfd_boolean
8978 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8979 struct bfd_link_info *info)
8980 {
8981 struct ppc_link_hash_table *htab;
8982 bfd *dynobj;
8983 asection *s;
8984 bfd_boolean relocs;
8985 bfd *ibfd;
8986 struct got_entry *first_tlsld;
8987
8988 htab = ppc_hash_table (info);
8989 if (htab == NULL)
8990 return FALSE;
8991
8992 dynobj = htab->elf.dynobj;
8993 if (dynobj == NULL)
8994 abort ();
8995
8996 if (htab->elf.dynamic_sections_created)
8997 {
8998 /* Set the contents of the .interp section to the interpreter. */
8999 if (info->executable)
9000 {
9001 s = bfd_get_section_by_name (dynobj, ".interp");
9002 if (s == NULL)
9003 abort ();
9004 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9005 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9006 }
9007 }
9008
9009 /* Set up .got offsets for local syms, and space for local dynamic
9010 relocs. */
9011 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9012 {
9013 struct got_entry **lgot_ents;
9014 struct got_entry **end_lgot_ents;
9015 struct plt_entry **local_plt;
9016 struct plt_entry **end_local_plt;
9017 unsigned char *lgot_masks;
9018 bfd_size_type locsymcount;
9019 Elf_Internal_Shdr *symtab_hdr;
9020 asection *srel;
9021
9022 if (!is_ppc64_elf (ibfd))
9023 continue;
9024
9025 for (s = ibfd->sections; s != NULL; s = s->next)
9026 {
9027 struct elf_dyn_relocs *p;
9028
9029 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9030 {
9031 if (!bfd_is_abs_section (p->sec)
9032 && bfd_is_abs_section (p->sec->output_section))
9033 {
9034 /* Input section has been discarded, either because
9035 it is a copy of a linkonce section or due to
9036 linker script /DISCARD/, so we'll be discarding
9037 the relocs too. */
9038 }
9039 else if (p->count != 0)
9040 {
9041 srel = elf_section_data (p->sec)->sreloc;
9042 if (!htab->elf.dynamic_sections_created)
9043 srel = htab->reliplt;
9044 srel->size += p->count * sizeof (Elf64_External_Rela);
9045 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9046 info->flags |= DF_TEXTREL;
9047 }
9048 }
9049 }
9050
9051 lgot_ents = elf_local_got_ents (ibfd);
9052 if (!lgot_ents)
9053 continue;
9054
9055 symtab_hdr = &elf_symtab_hdr (ibfd);
9056 locsymcount = symtab_hdr->sh_info;
9057 end_lgot_ents = lgot_ents + locsymcount;
9058 local_plt = (struct plt_entry **) end_lgot_ents;
9059 end_local_plt = local_plt + locsymcount;
9060 lgot_masks = (unsigned char *) end_local_plt;
9061 s = ppc64_elf_tdata (ibfd)->got;
9062 srel = ppc64_elf_tdata (ibfd)->relgot;
9063 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9064 {
9065 struct got_entry **pent, *ent;
9066
9067 pent = lgot_ents;
9068 while ((ent = *pent) != NULL)
9069 if (ent->got.refcount > 0)
9070 {
9071 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9072 {
9073 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9074 *pent = ent->next;
9075 }
9076 else
9077 {
9078 unsigned int num = 1;
9079 ent->got.offset = s->size;
9080 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9081 num = 2;
9082 s->size += num * 8;
9083 if (info->shared)
9084 srel->size += num * sizeof (Elf64_External_Rela);
9085 else if ((*lgot_masks & PLT_IFUNC) != 0)
9086 {
9087 htab->reliplt->size
9088 += num * sizeof (Elf64_External_Rela);
9089 htab->got_reli_size
9090 += num * sizeof (Elf64_External_Rela);
9091 }
9092 pent = &ent->next;
9093 }
9094 }
9095 else
9096 *pent = ent->next;
9097 }
9098
9099 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9100 for (; local_plt < end_local_plt; ++local_plt)
9101 {
9102 struct plt_entry *ent;
9103
9104 for (ent = *local_plt; ent != NULL; ent = ent->next)
9105 if (ent->plt.refcount > 0)
9106 {
9107 s = htab->iplt;
9108 ent->plt.offset = s->size;
9109 s->size += PLT_ENTRY_SIZE;
9110
9111 htab->reliplt->size += sizeof (Elf64_External_Rela);
9112 }
9113 else
9114 ent->plt.offset = (bfd_vma) -1;
9115 }
9116 }
9117
9118 /* Allocate global sym .plt and .got entries, and space for global
9119 sym dynamic relocs. */
9120 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9121
9122 first_tlsld = NULL;
9123 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9124 {
9125 struct got_entry *ent;
9126
9127 if (!is_ppc64_elf (ibfd))
9128 continue;
9129
9130 ent = ppc64_tlsld_got (ibfd);
9131 if (ent->got.refcount > 0)
9132 {
9133 if (!htab->do_multi_toc && first_tlsld != NULL)
9134 {
9135 ent->is_indirect = TRUE;
9136 ent->got.ent = first_tlsld;
9137 }
9138 else
9139 {
9140 if (first_tlsld == NULL)
9141 first_tlsld = ent;
9142 s = ppc64_elf_tdata (ibfd)->got;
9143 ent->got.offset = s->size;
9144 ent->owner = ibfd;
9145 s->size += 16;
9146 if (info->shared)
9147 {
9148 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9149 srel->size += sizeof (Elf64_External_Rela);
9150 }
9151 }
9152 }
9153 else
9154 ent->got.offset = (bfd_vma) -1;
9155 }
9156
9157 /* We now have determined the sizes of the various dynamic sections.
9158 Allocate memory for them. */
9159 relocs = FALSE;
9160 for (s = dynobj->sections; s != NULL; s = s->next)
9161 {
9162 if ((s->flags & SEC_LINKER_CREATED) == 0)
9163 continue;
9164
9165 if (s == htab->brlt || s == htab->relbrlt)
9166 /* These haven't been allocated yet; don't strip. */
9167 continue;
9168 else if (s == htab->got
9169 || s == htab->plt
9170 || s == htab->iplt
9171 || s == htab->glink
9172 || s == htab->dynbss)
9173 {
9174 /* Strip this section if we don't need it; see the
9175 comment below. */
9176 }
9177 else if (s == htab->glink_eh_frame)
9178 {
9179 if (!bfd_is_abs_section (s->output_section))
9180 /* Not sized yet. */
9181 continue;
9182 }
9183 else if (CONST_STRNEQ (s->name, ".rela"))
9184 {
9185 if (s->size != 0)
9186 {
9187 if (s != htab->relplt)
9188 relocs = TRUE;
9189
9190 /* We use the reloc_count field as a counter if we need
9191 to copy relocs into the output file. */
9192 s->reloc_count = 0;
9193 }
9194 }
9195 else
9196 {
9197 /* It's not one of our sections, so don't allocate space. */
9198 continue;
9199 }
9200
9201 if (s->size == 0)
9202 {
9203 /* If we don't need this section, strip it from the
9204 output file. This is mostly to handle .rela.bss and
9205 .rela.plt. We must create both sections in
9206 create_dynamic_sections, because they must be created
9207 before the linker maps input sections to output
9208 sections. The linker does that before
9209 adjust_dynamic_symbol is called, and it is that
9210 function which decides whether anything needs to go
9211 into these sections. */
9212 s->flags |= SEC_EXCLUDE;
9213 continue;
9214 }
9215
9216 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9217 continue;
9218
9219 /* Allocate memory for the section contents. We use bfd_zalloc
9220 here in case unused entries are not reclaimed before the
9221 section's contents are written out. This should not happen,
9222 but this way if it does we get a R_PPC64_NONE reloc in .rela
9223 sections instead of garbage.
9224 We also rely on the section contents being zero when writing
9225 the GOT. */
9226 s->contents = bfd_zalloc (dynobj, s->size);
9227 if (s->contents == NULL)
9228 return FALSE;
9229 }
9230
9231 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9232 {
9233 if (!is_ppc64_elf (ibfd))
9234 continue;
9235
9236 s = ppc64_elf_tdata (ibfd)->got;
9237 if (s != NULL && s != htab->got)
9238 {
9239 if (s->size == 0)
9240 s->flags |= SEC_EXCLUDE;
9241 else
9242 {
9243 s->contents = bfd_zalloc (ibfd, s->size);
9244 if (s->contents == NULL)
9245 return FALSE;
9246 }
9247 }
9248 s = ppc64_elf_tdata (ibfd)->relgot;
9249 if (s != NULL)
9250 {
9251 if (s->size == 0)
9252 s->flags |= SEC_EXCLUDE;
9253 else
9254 {
9255 s->contents = bfd_zalloc (ibfd, s->size);
9256 if (s->contents == NULL)
9257 return FALSE;
9258 relocs = TRUE;
9259 s->reloc_count = 0;
9260 }
9261 }
9262 }
9263
9264 if (htab->elf.dynamic_sections_created)
9265 {
9266 /* Add some entries to the .dynamic section. We fill in the
9267 values later, in ppc64_elf_finish_dynamic_sections, but we
9268 must add the entries now so that we get the correct size for
9269 the .dynamic section. The DT_DEBUG entry is filled in by the
9270 dynamic linker and used by the debugger. */
9271 #define add_dynamic_entry(TAG, VAL) \
9272 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9273
9274 if (info->executable)
9275 {
9276 if (!add_dynamic_entry (DT_DEBUG, 0))
9277 return FALSE;
9278 }
9279
9280 if (htab->plt != NULL && htab->plt->size != 0)
9281 {
9282 if (!add_dynamic_entry (DT_PLTGOT, 0)
9283 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9284 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9285 || !add_dynamic_entry (DT_JMPREL, 0)
9286 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9287 return FALSE;
9288 }
9289
9290 if (NO_OPD_RELOCS)
9291 {
9292 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9293 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9294 return FALSE;
9295 }
9296
9297 if (!htab->no_tls_get_addr_opt
9298 && htab->tls_get_addr_fd != NULL
9299 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9300 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9301 return FALSE;
9302
9303 if (relocs)
9304 {
9305 if (!add_dynamic_entry (DT_RELA, 0)
9306 || !add_dynamic_entry (DT_RELASZ, 0)
9307 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9308 return FALSE;
9309
9310 /* If any dynamic relocs apply to a read-only section,
9311 then we need a DT_TEXTREL entry. */
9312 if ((info->flags & DF_TEXTREL) == 0)
9313 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9314
9315 if ((info->flags & DF_TEXTREL) != 0)
9316 {
9317 if (!add_dynamic_entry (DT_TEXTREL, 0))
9318 return FALSE;
9319 }
9320 }
9321 }
9322 #undef add_dynamic_entry
9323
9324 return TRUE;
9325 }
9326
9327 /* Determine the type of stub needed, if any, for a call. */
9328
9329 static inline enum ppc_stub_type
9330 ppc_type_of_stub (asection *input_sec,
9331 const Elf_Internal_Rela *rel,
9332 struct ppc_link_hash_entry **hash,
9333 struct plt_entry **plt_ent,
9334 bfd_vma destination)
9335 {
9336 struct ppc_link_hash_entry *h = *hash;
9337 bfd_vma location;
9338 bfd_vma branch_offset;
9339 bfd_vma max_branch_offset;
9340 enum elf_ppc64_reloc_type r_type;
9341
9342 if (h != NULL)
9343 {
9344 struct plt_entry *ent;
9345 struct ppc_link_hash_entry *fdh = h;
9346 if (h->oh != NULL
9347 && h->oh->is_func_descriptor)
9348 {
9349 fdh = ppc_follow_link (h->oh);
9350 *hash = fdh;
9351 }
9352
9353 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9354 if (ent->addend == rel->r_addend
9355 && ent->plt.offset != (bfd_vma) -1)
9356 {
9357 *plt_ent = ent;
9358 return ppc_stub_plt_call;
9359 }
9360
9361 /* Here, we know we don't have a plt entry. If we don't have a
9362 either a defined function descriptor or a defined entry symbol
9363 in a regular object file, then it is pointless trying to make
9364 any other type of stub. */
9365 if (!is_static_defined (&fdh->elf)
9366 && !is_static_defined (&h->elf))
9367 return ppc_stub_none;
9368 }
9369 else if (elf_local_got_ents (input_sec->owner) != NULL)
9370 {
9371 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9372 struct plt_entry **local_plt = (struct plt_entry **)
9373 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9374 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9375
9376 if (local_plt[r_symndx] != NULL)
9377 {
9378 struct plt_entry *ent;
9379
9380 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9381 if (ent->addend == rel->r_addend
9382 && ent->plt.offset != (bfd_vma) -1)
9383 {
9384 *plt_ent = ent;
9385 return ppc_stub_plt_call;
9386 }
9387 }
9388 }
9389
9390 /* Determine where the call point is. */
9391 location = (input_sec->output_offset
9392 + input_sec->output_section->vma
9393 + rel->r_offset);
9394
9395 branch_offset = destination - location;
9396 r_type = ELF64_R_TYPE (rel->r_info);
9397
9398 /* Determine if a long branch stub is needed. */
9399 max_branch_offset = 1 << 25;
9400 if (r_type != R_PPC64_REL24)
9401 max_branch_offset = 1 << 15;
9402
9403 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9404 /* We need a stub. Figure out whether a long_branch or plt_branch
9405 is needed later. */
9406 return ppc_stub_long_branch;
9407
9408 return ppc_stub_none;
9409 }
9410
9411 /* Build a .plt call stub. */
9412
9413 static inline bfd_byte *
9414 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r,
9415 bfd_boolean plt_static_chain)
9416 {
9417 #define PPC_LO(v) ((v) & 0xffff)
9418 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9419 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9420
9421 if (PPC_HA (offset) != 0)
9422 {
9423 if (r != NULL)
9424 {
9425 r[0].r_offset += 4;
9426 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9427 r[1].r_offset = r[0].r_offset + 4;
9428 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9429 r[1].r_addend = r[0].r_addend;
9430 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9431 {
9432 r[2].r_offset = r[1].r_offset + 4;
9433 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9434 r[2].r_addend = r[0].r_addend;
9435 }
9436 else
9437 {
9438 r[2].r_offset = r[1].r_offset + 8;
9439 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9440 r[2].r_addend = r[0].r_addend + 8;
9441 if (plt_static_chain)
9442 {
9443 r[3].r_offset = r[2].r_offset + 4;
9444 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9445 r[3].r_addend = r[0].r_addend + 16;
9446 }
9447 }
9448 }
9449 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9450 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9451 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9452 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9453 {
9454 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9455 offset = 0;
9456 }
9457 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9458 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9459 if (plt_static_chain)
9460 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9461 bfd_put_32 (obfd, BCTR, p), p += 4;
9462 }
9463 else
9464 {
9465 if (r != NULL)
9466 {
9467 r[0].r_offset += 4;
9468 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9469 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9470 {
9471 r[1].r_offset = r[0].r_offset + 4;
9472 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9473 r[1].r_addend = r[0].r_addend;
9474 }
9475 else
9476 {
9477 r[1].r_offset = r[0].r_offset + 8;
9478 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9479 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9480 if (plt_static_chain)
9481 {
9482 r[2].r_offset = r[1].r_offset + 4;
9483 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9484 r[2].r_addend = r[0].r_addend + 8;
9485 }
9486 }
9487 }
9488 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9489 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9490 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9491 {
9492 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9493 offset = 0;
9494 }
9495 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9496 if (plt_static_chain)
9497 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9498 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9499 bfd_put_32 (obfd, BCTR, p), p += 4;
9500 }
9501 return p;
9502 }
9503
9504 /* Build a special .plt call stub for __tls_get_addr. */
9505
9506 #define LD_R11_0R3 0xe9630000
9507 #define LD_R12_0R3 0xe9830000
9508 #define MR_R0_R3 0x7c601b78
9509 #define CMPDI_R11_0 0x2c2b0000
9510 #define ADD_R3_R12_R13 0x7c6c6a14
9511 #define BEQLR 0x4d820020
9512 #define MR_R3_R0 0x7c030378
9513 #define MFLR_R11 0x7d6802a6
9514 #define STD_R11_0R1 0xf9610000
9515 #define BCTRL 0x4e800421
9516 #define LD_R11_0R1 0xe9610000
9517 #define LD_R2_0R1 0xe8410000
9518 #define MTLR_R11 0x7d6803a6
9519
9520 static inline bfd_byte *
9521 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9522 Elf_Internal_Rela *r, bfd_boolean plt_static_chain)
9523 {
9524 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9525 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9526 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9527 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9528 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9529 bfd_put_32 (obfd, BEQLR, p), p += 4;
9530 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9531 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9532 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9533
9534 if (r != NULL)
9535 r[0].r_offset += 9 * 4;
9536 p = build_plt_stub (obfd, p, offset, r, plt_static_chain);
9537 bfd_put_32 (obfd, BCTRL, p - 4);
9538
9539 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9540 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9541 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9542 bfd_put_32 (obfd, BLR, p), p += 4;
9543
9544 return p;
9545 }
9546
9547 static Elf_Internal_Rela *
9548 get_relocs (asection *sec, int count)
9549 {
9550 Elf_Internal_Rela *relocs;
9551 struct bfd_elf_section_data *elfsec_data;
9552
9553 elfsec_data = elf_section_data (sec);
9554 relocs = elfsec_data->relocs;
9555 if (relocs == NULL)
9556 {
9557 bfd_size_type relsize;
9558 relsize = sec->reloc_count * sizeof (*relocs);
9559 relocs = bfd_alloc (sec->owner, relsize);
9560 if (relocs == NULL)
9561 return NULL;
9562 elfsec_data->relocs = relocs;
9563 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9564 sizeof (Elf_Internal_Shdr));
9565 if (elfsec_data->rela.hdr == NULL)
9566 return NULL;
9567 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9568 * sizeof (Elf64_External_Rela));
9569 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9570 sec->reloc_count = 0;
9571 }
9572 relocs += sec->reloc_count;
9573 sec->reloc_count += count;
9574 return relocs;
9575 }
9576
9577 static bfd_vma
9578 get_r2off (struct bfd_link_info *info,
9579 struct ppc_stub_hash_entry *stub_entry)
9580 {
9581 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9582 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9583
9584 if (r2off == 0)
9585 {
9586 /* Support linking -R objects. Get the toc pointer from the
9587 opd entry. */
9588 char buf[8];
9589 asection *opd = stub_entry->h->elf.root.u.def.section;
9590 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9591
9592 if (strcmp (opd->name, ".opd") != 0
9593 || opd->reloc_count != 0)
9594 {
9595 info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
9596 stub_entry->h->elf.root.root.string);
9597 bfd_set_error (bfd_error_bad_value);
9598 return 0;
9599 }
9600 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9601 return 0;
9602 r2off = bfd_get_64 (opd->owner, buf);
9603 r2off -= elf_gp (info->output_bfd);
9604 }
9605 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9606 return r2off;
9607 }
9608
9609 static bfd_boolean
9610 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9611 {
9612 struct ppc_stub_hash_entry *stub_entry;
9613 struct ppc_branch_hash_entry *br_entry;
9614 struct bfd_link_info *info;
9615 struct ppc_link_hash_table *htab;
9616 bfd_byte *loc;
9617 bfd_byte *p;
9618 bfd_vma dest, off;
9619 int size;
9620 Elf_Internal_Rela *r;
9621 asection *plt;
9622
9623 /* Massage our args to the form they really have. */
9624 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9625 info = in_arg;
9626
9627 htab = ppc_hash_table (info);
9628 if (htab == NULL)
9629 return FALSE;
9630
9631 /* Make a note of the offset within the stubs for this entry. */
9632 stub_entry->stub_offset = stub_entry->stub_sec->size;
9633 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9634
9635 htab->stub_count[stub_entry->stub_type - 1] += 1;
9636 switch (stub_entry->stub_type)
9637 {
9638 case ppc_stub_long_branch:
9639 case ppc_stub_long_branch_r2off:
9640 /* Branches are relative. This is where we are going to. */
9641 off = dest = (stub_entry->target_value
9642 + stub_entry->target_section->output_offset
9643 + stub_entry->target_section->output_section->vma);
9644
9645 /* And this is where we are coming from. */
9646 off -= (stub_entry->stub_offset
9647 + stub_entry->stub_sec->output_offset
9648 + stub_entry->stub_sec->output_section->vma);
9649
9650 size = 4;
9651 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9652 {
9653 bfd_vma r2off = get_r2off (info, stub_entry);
9654
9655 if (r2off == 0)
9656 {
9657 htab->stub_error = TRUE;
9658 return FALSE;
9659 }
9660 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9661 loc += 4;
9662 size = 12;
9663 if (PPC_HA (r2off) != 0)
9664 {
9665 size = 16;
9666 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9667 loc += 4;
9668 }
9669 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9670 loc += 4;
9671 off -= size - 4;
9672 }
9673 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9674
9675 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9676 {
9677 info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
9678 stub_entry->root.string);
9679 htab->stub_error = TRUE;
9680 return FALSE;
9681 }
9682
9683 if (info->emitrelocations)
9684 {
9685 r = get_relocs (stub_entry->stub_sec, 1);
9686 if (r == NULL)
9687 return FALSE;
9688 r->r_offset = loc - stub_entry->stub_sec->contents;
9689 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9690 r->r_addend = dest;
9691 if (stub_entry->h != NULL)
9692 {
9693 struct elf_link_hash_entry **hashes;
9694 unsigned long symndx;
9695 struct ppc_link_hash_entry *h;
9696
9697 hashes = elf_sym_hashes (htab->stub_bfd);
9698 if (hashes == NULL)
9699 {
9700 bfd_size_type hsize;
9701
9702 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9703 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9704 if (hashes == NULL)
9705 return FALSE;
9706 elf_sym_hashes (htab->stub_bfd) = hashes;
9707 htab->stub_globals = 1;
9708 }
9709 symndx = htab->stub_globals++;
9710 h = stub_entry->h;
9711 hashes[symndx] = &h->elf;
9712 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9713 if (h->oh != NULL && h->oh->is_func)
9714 h = ppc_follow_link (h->oh);
9715 if (h->elf.root.u.def.section != stub_entry->target_section)
9716 /* H is an opd symbol. The addend must be zero. */
9717 r->r_addend = 0;
9718 else
9719 {
9720 off = (h->elf.root.u.def.value
9721 + h->elf.root.u.def.section->output_offset
9722 + h->elf.root.u.def.section->output_section->vma);
9723 r->r_addend -= off;
9724 }
9725 }
9726 }
9727 break;
9728
9729 case ppc_stub_plt_branch:
9730 case ppc_stub_plt_branch_r2off:
9731 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9732 stub_entry->root.string + 9,
9733 FALSE, FALSE);
9734 if (br_entry == NULL)
9735 {
9736 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
9737 stub_entry->root.string);
9738 htab->stub_error = TRUE;
9739 return FALSE;
9740 }
9741
9742 dest = (stub_entry->target_value
9743 + stub_entry->target_section->output_offset
9744 + stub_entry->target_section->output_section->vma);
9745
9746 bfd_put_64 (htab->brlt->owner, dest,
9747 htab->brlt->contents + br_entry->offset);
9748
9749 if (br_entry->iter == htab->stub_iteration)
9750 {
9751 br_entry->iter = 0;
9752
9753 if (htab->relbrlt != NULL)
9754 {
9755 /* Create a reloc for the branch lookup table entry. */
9756 Elf_Internal_Rela rela;
9757 bfd_byte *rl;
9758
9759 rela.r_offset = (br_entry->offset
9760 + htab->brlt->output_offset
9761 + htab->brlt->output_section->vma);
9762 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9763 rela.r_addend = dest;
9764
9765 rl = htab->relbrlt->contents;
9766 rl += (htab->relbrlt->reloc_count++
9767 * sizeof (Elf64_External_Rela));
9768 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9769 }
9770 else if (info->emitrelocations)
9771 {
9772 r = get_relocs (htab->brlt, 1);
9773 if (r == NULL)
9774 return FALSE;
9775 /* brlt, being SEC_LINKER_CREATED does not go through the
9776 normal reloc processing. Symbols and offsets are not
9777 translated from input file to output file form, so
9778 set up the offset per the output file. */
9779 r->r_offset = (br_entry->offset
9780 + htab->brlt->output_offset
9781 + htab->brlt->output_section->vma);
9782 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9783 r->r_addend = dest;
9784 }
9785 }
9786
9787 dest = (br_entry->offset
9788 + htab->brlt->output_offset
9789 + htab->brlt->output_section->vma);
9790
9791 off = (dest
9792 - elf_gp (htab->brlt->output_section->owner)
9793 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9794
9795 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9796 {
9797 info->callbacks->einfo
9798 (_("%P: linkage table error against `%s'\n"),
9799 stub_entry->root.string);
9800 bfd_set_error (bfd_error_bad_value);
9801 htab->stub_error = TRUE;
9802 return FALSE;
9803 }
9804
9805 if (info->emitrelocations)
9806 {
9807 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9808 if (r == NULL)
9809 return FALSE;
9810 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9811 if (bfd_big_endian (info->output_bfd))
9812 r[0].r_offset += 2;
9813 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9814 r[0].r_offset += 4;
9815 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9816 r[0].r_addend = dest;
9817 if (PPC_HA (off) != 0)
9818 {
9819 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9820 r[1].r_offset = r[0].r_offset + 4;
9821 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9822 r[1].r_addend = r[0].r_addend;
9823 }
9824 }
9825
9826 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9827 {
9828 if (PPC_HA (off) != 0)
9829 {
9830 size = 16;
9831 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9832 loc += 4;
9833 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9834 }
9835 else
9836 {
9837 size = 12;
9838 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9839 }
9840 }
9841 else
9842 {
9843 bfd_vma r2off = get_r2off (info, stub_entry);
9844
9845 if (r2off == 0)
9846 {
9847 htab->stub_error = TRUE;
9848 return FALSE;
9849 }
9850
9851 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9852 loc += 4;
9853 size = 20;
9854 if (PPC_HA (off) != 0)
9855 {
9856 size += 4;
9857 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9858 loc += 4;
9859 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9860 loc += 4;
9861 }
9862 else
9863 {
9864 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9865 loc += 4;
9866 }
9867
9868 if (PPC_HA (r2off) != 0)
9869 {
9870 size += 4;
9871 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9872 loc += 4;
9873 }
9874 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9875 }
9876 loc += 4;
9877 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9878 loc += 4;
9879 bfd_put_32 (htab->stub_bfd, BCTR, loc);
9880 break;
9881
9882 case ppc_stub_plt_call:
9883 if (stub_entry->h != NULL
9884 && stub_entry->h->is_func_descriptor
9885 && stub_entry->h->oh != NULL)
9886 {
9887 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9888
9889 /* If the old-ABI "dot-symbol" is undefined make it weak so
9890 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9891 FIXME: We used to define the symbol on one of the call
9892 stubs instead, which is why we test symbol section id
9893 against htab->top_id in various places. Likely all
9894 these checks could now disappear. */
9895 if (fh->elf.root.type == bfd_link_hash_undefined)
9896 fh->elf.root.type = bfd_link_hash_undefweak;
9897 /* Stop undo_symbol_twiddle changing it back to undefined. */
9898 fh->was_undefined = 0;
9899 }
9900
9901 /* Now build the stub. */
9902 dest = stub_entry->plt_ent->plt.offset & ~1;
9903 if (dest >= (bfd_vma) -2)
9904 abort ();
9905
9906 plt = htab->plt;
9907 if (!htab->elf.dynamic_sections_created
9908 || stub_entry->h == NULL
9909 || stub_entry->h->elf.dynindx == -1)
9910 plt = htab->iplt;
9911
9912 dest += plt->output_offset + plt->output_section->vma;
9913
9914 if (stub_entry->h == NULL
9915 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9916 {
9917 Elf_Internal_Rela rela;
9918 bfd_byte *rl;
9919
9920 rela.r_offset = dest;
9921 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9922 rela.r_addend = (stub_entry->target_value
9923 + stub_entry->target_section->output_offset
9924 + stub_entry->target_section->output_section->vma);
9925
9926 rl = (htab->reliplt->contents
9927 + (htab->reliplt->reloc_count++
9928 * sizeof (Elf64_External_Rela)));
9929 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9930 stub_entry->plt_ent->plt.offset |= 1;
9931 }
9932
9933 off = (dest
9934 - elf_gp (plt->output_section->owner)
9935 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9936
9937 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9938 {
9939 info->callbacks->einfo
9940 (_("%P: linkage table error against `%s'\n"),
9941 stub_entry->h != NULL
9942 ? stub_entry->h->elf.root.root.string
9943 : "<local sym>");
9944 bfd_set_error (bfd_error_bad_value);
9945 htab->stub_error = TRUE;
9946 return FALSE;
9947 }
9948
9949 r = NULL;
9950 if (info->emitrelocations)
9951 {
9952 r = get_relocs (stub_entry->stub_sec,
9953 (2
9954 + (PPC_HA (off) != 0)
9955 + (htab->plt_static_chain
9956 && PPC_HA (off + 16) == PPC_HA (off))));
9957 if (r == NULL)
9958 return FALSE;
9959 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9960 if (bfd_big_endian (info->output_bfd))
9961 r[0].r_offset += 2;
9962 r[0].r_addend = dest;
9963 }
9964 if (stub_entry->h != NULL
9965 && (stub_entry->h == htab->tls_get_addr_fd
9966 || stub_entry->h == htab->tls_get_addr)
9967 && !htab->no_tls_get_addr_opt)
9968 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r,
9969 htab->plt_static_chain);
9970 else
9971 p = build_plt_stub (htab->stub_bfd, loc, off, r,
9972 htab->plt_static_chain);
9973 size = p - loc;
9974 break;
9975
9976 default:
9977 BFD_FAIL ();
9978 return FALSE;
9979 }
9980
9981 stub_entry->stub_sec->size += size;
9982
9983 if (htab->emit_stub_syms)
9984 {
9985 struct elf_link_hash_entry *h;
9986 size_t len1, len2;
9987 char *name;
9988 const char *const stub_str[] = { "long_branch",
9989 "long_branch_r2off",
9990 "plt_branch",
9991 "plt_branch_r2off",
9992 "plt_call" };
9993
9994 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9995 len2 = strlen (stub_entry->root.string);
9996 name = bfd_malloc (len1 + len2 + 2);
9997 if (name == NULL)
9998 return FALSE;
9999 memcpy (name, stub_entry->root.string, 9);
10000 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10001 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10002 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10003 if (h == NULL)
10004 return FALSE;
10005 if (h->root.type == bfd_link_hash_new)
10006 {
10007 h->root.type = bfd_link_hash_defined;
10008 h->root.u.def.section = stub_entry->stub_sec;
10009 h->root.u.def.value = stub_entry->stub_offset;
10010 h->ref_regular = 1;
10011 h->def_regular = 1;
10012 h->ref_regular_nonweak = 1;
10013 h->forced_local = 1;
10014 h->non_elf = 0;
10015 }
10016 }
10017
10018 return TRUE;
10019 }
10020
10021 /* As above, but don't actually build the stub. Just bump offset so
10022 we know stub section sizes, and select plt_branch stubs where
10023 long_branch stubs won't do. */
10024
10025 static bfd_boolean
10026 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10027 {
10028 struct ppc_stub_hash_entry *stub_entry;
10029 struct bfd_link_info *info;
10030 struct ppc_link_hash_table *htab;
10031 bfd_vma off;
10032 int size;
10033
10034 /* Massage our args to the form they really have. */
10035 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10036 info = in_arg;
10037
10038 htab = ppc_hash_table (info);
10039 if (htab == NULL)
10040 return FALSE;
10041
10042 if (stub_entry->stub_type == ppc_stub_plt_call)
10043 {
10044 asection *plt;
10045 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10046 if (off >= (bfd_vma) -2)
10047 abort ();
10048 plt = htab->plt;
10049 if (!htab->elf.dynamic_sections_created
10050 || stub_entry->h == NULL
10051 || stub_entry->h->elf.dynindx == -1)
10052 plt = htab->iplt;
10053 off += (plt->output_offset
10054 + plt->output_section->vma
10055 - elf_gp (plt->output_section->owner)
10056 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10057
10058 size = PLT_CALL_STUB_SIZE;
10059 if (!htab->plt_static_chain)
10060 size -= 4;
10061 if (PPC_HA (off) == 0)
10062 size -= 4;
10063 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
10064 size += 4;
10065 if (stub_entry->h != NULL
10066 && (stub_entry->h == htab->tls_get_addr_fd
10067 || stub_entry->h == htab->tls_get_addr)
10068 && !htab->no_tls_get_addr_opt)
10069 size += 13 * 4;
10070 if (info->emitrelocations)
10071 {
10072 stub_entry->stub_sec->reloc_count
10073 += (2
10074 + (PPC_HA (off) != 0)
10075 + (htab->plt_static_chain
10076 && PPC_HA (off + 16) == PPC_HA (off)));
10077 stub_entry->stub_sec->flags |= SEC_RELOC;
10078 }
10079 }
10080 else
10081 {
10082 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10083 variants. */
10084 bfd_vma r2off = 0;
10085
10086 off = (stub_entry->target_value
10087 + stub_entry->target_section->output_offset
10088 + stub_entry->target_section->output_section->vma);
10089 off -= (stub_entry->stub_sec->size
10090 + stub_entry->stub_sec->output_offset
10091 + stub_entry->stub_sec->output_section->vma);
10092
10093 /* Reset the stub type from the plt variant in case we now
10094 can reach with a shorter stub. */
10095 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10096 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10097
10098 size = 4;
10099 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10100 {
10101 r2off = get_r2off (info, stub_entry);
10102 if (r2off == 0)
10103 {
10104 htab->stub_error = TRUE;
10105 return FALSE;
10106 }
10107 size = 12;
10108 if (PPC_HA (r2off) != 0)
10109 size = 16;
10110 off -= size - 4;
10111 }
10112
10113 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
10114 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10115 {
10116 struct ppc_branch_hash_entry *br_entry;
10117
10118 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10119 stub_entry->root.string + 9,
10120 TRUE, FALSE);
10121 if (br_entry == NULL)
10122 {
10123 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10124 stub_entry->root.string);
10125 htab->stub_error = TRUE;
10126 return FALSE;
10127 }
10128
10129 if (br_entry->iter != htab->stub_iteration)
10130 {
10131 br_entry->iter = htab->stub_iteration;
10132 br_entry->offset = htab->brlt->size;
10133 htab->brlt->size += 8;
10134
10135 if (htab->relbrlt != NULL)
10136 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10137 else if (info->emitrelocations)
10138 {
10139 htab->brlt->reloc_count += 1;
10140 htab->brlt->flags |= SEC_RELOC;
10141 }
10142 }
10143
10144 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10145 off = (br_entry->offset
10146 + htab->brlt->output_offset
10147 + htab->brlt->output_section->vma
10148 - elf_gp (htab->brlt->output_section->owner)
10149 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10150
10151 if (info->emitrelocations)
10152 {
10153 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10154 stub_entry->stub_sec->flags |= SEC_RELOC;
10155 }
10156
10157 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10158 {
10159 size = 12;
10160 if (PPC_HA (off) != 0)
10161 size = 16;
10162 }
10163 else
10164 {
10165 size = 20;
10166 if (PPC_HA (off) != 0)
10167 size += 4;
10168
10169 if (PPC_HA (r2off) != 0)
10170 size += 4;
10171 }
10172 }
10173 else if (info->emitrelocations)
10174 {
10175 stub_entry->stub_sec->reloc_count += 1;
10176 stub_entry->stub_sec->flags |= SEC_RELOC;
10177 }
10178 }
10179
10180 stub_entry->stub_sec->size += size;
10181 return TRUE;
10182 }
10183
10184 /* Set up various things so that we can make a list of input sections
10185 for each output section included in the link. Returns -1 on error,
10186 0 when no stubs will be needed, and 1 on success. */
10187
10188 int
10189 ppc64_elf_setup_section_lists
10190 (struct bfd_link_info *info,
10191 asection *(*add_stub_section) (const char *, asection *),
10192 void (*layout_sections_again) (void))
10193 {
10194 bfd *input_bfd;
10195 int top_id, top_index, id;
10196 asection *section;
10197 asection **input_list;
10198 bfd_size_type amt;
10199 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10200
10201 if (htab == NULL)
10202 return -1;
10203 /* Stash our params away. */
10204 htab->add_stub_section = add_stub_section;
10205 htab->layout_sections_again = layout_sections_again;
10206
10207 if (htab->brlt == NULL)
10208 return 0;
10209
10210 /* Find the top input section id. */
10211 for (input_bfd = info->input_bfds, top_id = 3;
10212 input_bfd != NULL;
10213 input_bfd = input_bfd->link_next)
10214 {
10215 for (section = input_bfd->sections;
10216 section != NULL;
10217 section = section->next)
10218 {
10219 if (top_id < section->id)
10220 top_id = section->id;
10221 }
10222 }
10223
10224 htab->top_id = top_id;
10225 amt = sizeof (struct map_stub) * (top_id + 1);
10226 htab->stub_group = bfd_zmalloc (amt);
10227 if (htab->stub_group == NULL)
10228 return -1;
10229
10230 /* Set toc_off for com, und, abs and ind sections. */
10231 for (id = 0; id < 3; id++)
10232 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10233
10234 /* We can't use output_bfd->section_count here to find the top output
10235 section index as some sections may have been removed, and
10236 strip_excluded_output_sections doesn't renumber the indices. */
10237 for (section = info->output_bfd->sections, top_index = 0;
10238 section != NULL;
10239 section = section->next)
10240 {
10241 if (top_index < section->index)
10242 top_index = section->index;
10243 }
10244
10245 htab->top_index = top_index;
10246 amt = sizeof (asection *) * (top_index + 1);
10247 input_list = bfd_zmalloc (amt);
10248 htab->input_list = input_list;
10249 if (input_list == NULL)
10250 return -1;
10251
10252 return 1;
10253 }
10254
10255 /* Set up for first pass at multitoc partitioning. */
10256
10257 void
10258 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10259 {
10260 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10261
10262 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10263 htab->toc_curr = elf_gp (info->output_bfd);
10264 htab->toc_bfd = NULL;
10265 htab->toc_first_sec = NULL;
10266 }
10267
10268 /* The linker repeatedly calls this function for each TOC input section
10269 and linker generated GOT section. Group input bfds such that the toc
10270 within a group is less than 64k in size. */
10271
10272 bfd_boolean
10273 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10274 {
10275 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10276 bfd_vma addr, off, limit;
10277
10278 if (htab == NULL)
10279 return FALSE;
10280
10281 if (!htab->second_toc_pass)
10282 {
10283 /* Keep track of the first .toc or .got section for this input bfd. */
10284 if (htab->toc_bfd != isec->owner)
10285 {
10286 htab->toc_bfd = isec->owner;
10287 htab->toc_first_sec = isec;
10288 }
10289
10290 addr = isec->output_offset + isec->output_section->vma;
10291 off = addr - htab->toc_curr;
10292 limit = 0x80008000;
10293 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10294 limit = 0x10000;
10295 if (off + isec->size > limit)
10296 {
10297 addr = (htab->toc_first_sec->output_offset
10298 + htab->toc_first_sec->output_section->vma);
10299 htab->toc_curr = addr;
10300 }
10301
10302 /* toc_curr is the base address of this toc group. Set elf_gp
10303 for the input section to be the offset relative to the
10304 output toc base plus 0x8000. Making the input elf_gp an
10305 offset allows us to move the toc as a whole without
10306 recalculating input elf_gp. */
10307 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10308 off += TOC_BASE_OFF;
10309
10310 /* Die if someone uses a linker script that doesn't keep input
10311 file .toc and .got together. */
10312 if (elf_gp (isec->owner) != 0
10313 && elf_gp (isec->owner) != off)
10314 return FALSE;
10315
10316 elf_gp (isec->owner) = off;
10317 return TRUE;
10318 }
10319
10320 /* During the second pass toc_first_sec points to the start of
10321 a toc group, and toc_curr is used to track the old elf_gp.
10322 We use toc_bfd to ensure we only look at each bfd once. */
10323 if (htab->toc_bfd == isec->owner)
10324 return TRUE;
10325 htab->toc_bfd = isec->owner;
10326
10327 if (htab->toc_first_sec == NULL
10328 || htab->toc_curr != elf_gp (isec->owner))
10329 {
10330 htab->toc_curr = elf_gp (isec->owner);
10331 htab->toc_first_sec = isec;
10332 }
10333 addr = (htab->toc_first_sec->output_offset
10334 + htab->toc_first_sec->output_section->vma);
10335 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10336 elf_gp (isec->owner) = off;
10337
10338 return TRUE;
10339 }
10340
10341 /* Called via elf_link_hash_traverse to merge GOT entries for global
10342 symbol H. */
10343
10344 static bfd_boolean
10345 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10346 {
10347 if (h->root.type == bfd_link_hash_indirect)
10348 return TRUE;
10349
10350 merge_got_entries (&h->got.glist);
10351
10352 return TRUE;
10353 }
10354
10355 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10356 symbol H. */
10357
10358 static bfd_boolean
10359 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10360 {
10361 struct got_entry *gent;
10362
10363 if (h->root.type == bfd_link_hash_indirect)
10364 return TRUE;
10365
10366 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10367 if (!gent->is_indirect)
10368 allocate_got (h, (struct bfd_link_info *) inf, gent);
10369 return TRUE;
10370 }
10371
10372 /* Called on the first multitoc pass after the last call to
10373 ppc64_elf_next_toc_section. This function removes duplicate GOT
10374 entries. */
10375
10376 bfd_boolean
10377 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10378 {
10379 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10380 struct bfd *ibfd, *ibfd2;
10381 bfd_boolean done_something;
10382
10383 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10384
10385 if (!htab->do_multi_toc)
10386 return FALSE;
10387
10388 /* Merge global sym got entries within a toc group. */
10389 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10390
10391 /* And tlsld_got. */
10392 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10393 {
10394 struct got_entry *ent, *ent2;
10395
10396 if (!is_ppc64_elf (ibfd))
10397 continue;
10398
10399 ent = ppc64_tlsld_got (ibfd);
10400 if (!ent->is_indirect
10401 && ent->got.offset != (bfd_vma) -1)
10402 {
10403 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10404 {
10405 if (!is_ppc64_elf (ibfd2))
10406 continue;
10407
10408 ent2 = ppc64_tlsld_got (ibfd2);
10409 if (!ent2->is_indirect
10410 && ent2->got.offset != (bfd_vma) -1
10411 && elf_gp (ibfd2) == elf_gp (ibfd))
10412 {
10413 ent2->is_indirect = TRUE;
10414 ent2->got.ent = ent;
10415 }
10416 }
10417 }
10418 }
10419
10420 /* Zap sizes of got sections. */
10421 htab->reliplt->rawsize = htab->reliplt->size;
10422 htab->reliplt->size -= htab->got_reli_size;
10423 htab->got_reli_size = 0;
10424
10425 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10426 {
10427 asection *got, *relgot;
10428
10429 if (!is_ppc64_elf (ibfd))
10430 continue;
10431
10432 got = ppc64_elf_tdata (ibfd)->got;
10433 if (got != NULL)
10434 {
10435 got->rawsize = got->size;
10436 got->size = 0;
10437 relgot = ppc64_elf_tdata (ibfd)->relgot;
10438 relgot->rawsize = relgot->size;
10439 relgot->size = 0;
10440 }
10441 }
10442
10443 /* Now reallocate the got, local syms first. We don't need to
10444 allocate section contents again since we never increase size. */
10445 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10446 {
10447 struct got_entry **lgot_ents;
10448 struct got_entry **end_lgot_ents;
10449 struct plt_entry **local_plt;
10450 struct plt_entry **end_local_plt;
10451 unsigned char *lgot_masks;
10452 bfd_size_type locsymcount;
10453 Elf_Internal_Shdr *symtab_hdr;
10454 asection *s, *srel;
10455
10456 if (!is_ppc64_elf (ibfd))
10457 continue;
10458
10459 lgot_ents = elf_local_got_ents (ibfd);
10460 if (!lgot_ents)
10461 continue;
10462
10463 symtab_hdr = &elf_symtab_hdr (ibfd);
10464 locsymcount = symtab_hdr->sh_info;
10465 end_lgot_ents = lgot_ents + locsymcount;
10466 local_plt = (struct plt_entry **) end_lgot_ents;
10467 end_local_plt = local_plt + locsymcount;
10468 lgot_masks = (unsigned char *) end_local_plt;
10469 s = ppc64_elf_tdata (ibfd)->got;
10470 srel = ppc64_elf_tdata (ibfd)->relgot;
10471 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10472 {
10473 struct got_entry *ent;
10474
10475 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10476 {
10477 unsigned int num = 1;
10478 ent->got.offset = s->size;
10479 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10480 num = 2;
10481 s->size += num * 8;
10482 if (info->shared)
10483 srel->size += num * sizeof (Elf64_External_Rela);
10484 else if ((*lgot_masks & PLT_IFUNC) != 0)
10485 {
10486 htab->reliplt->size
10487 += num * sizeof (Elf64_External_Rela);
10488 htab->got_reli_size
10489 += num * sizeof (Elf64_External_Rela);
10490 }
10491 }
10492 }
10493 }
10494
10495 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10496
10497 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10498 {
10499 struct got_entry *ent;
10500
10501 if (!is_ppc64_elf (ibfd))
10502 continue;
10503
10504 ent = ppc64_tlsld_got (ibfd);
10505 if (!ent->is_indirect
10506 && ent->got.offset != (bfd_vma) -1)
10507 {
10508 asection *s = ppc64_elf_tdata (ibfd)->got;
10509 ent->got.offset = s->size;
10510 s->size += 16;
10511 if (info->shared)
10512 {
10513 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10514 srel->size += sizeof (Elf64_External_Rela);
10515 }
10516 }
10517 }
10518
10519 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10520 if (!done_something)
10521 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10522 {
10523 asection *got;
10524
10525 if (!is_ppc64_elf (ibfd))
10526 continue;
10527
10528 got = ppc64_elf_tdata (ibfd)->got;
10529 if (got != NULL)
10530 {
10531 done_something = got->rawsize != got->size;
10532 if (done_something)
10533 break;
10534 }
10535 }
10536
10537 if (done_something)
10538 (*htab->layout_sections_again) ();
10539
10540 /* Set up for second pass over toc sections to recalculate elf_gp
10541 on input sections. */
10542 htab->toc_bfd = NULL;
10543 htab->toc_first_sec = NULL;
10544 htab->second_toc_pass = TRUE;
10545 return done_something;
10546 }
10547
10548 /* Called after second pass of multitoc partitioning. */
10549
10550 void
10551 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10552 {
10553 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10554
10555 /* After the second pass, toc_curr tracks the TOC offset used
10556 for code sections below in ppc64_elf_next_input_section. */
10557 htab->toc_curr = TOC_BASE_OFF;
10558 }
10559
10560 /* No toc references were found in ISEC. If the code in ISEC makes no
10561 calls, then there's no need to use toc adjusting stubs when branching
10562 into ISEC. Actually, indirect calls from ISEC are OK as they will
10563 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10564 needed, and 2 if a cyclical call-graph was found but no other reason
10565 for a stub was detected. If called from the top level, a return of
10566 2 means the same as a return of 0. */
10567
10568 static int
10569 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10570 {
10571 int ret;
10572
10573 /* Mark this section as checked. */
10574 isec->call_check_done = 1;
10575
10576 /* We know none of our code bearing sections will need toc stubs. */
10577 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10578 return 0;
10579
10580 if (isec->size == 0)
10581 return 0;
10582
10583 if (isec->output_section == NULL)
10584 return 0;
10585
10586 ret = 0;
10587 if (isec->reloc_count != 0)
10588 {
10589 Elf_Internal_Rela *relstart, *rel;
10590 Elf_Internal_Sym *local_syms;
10591 struct ppc_link_hash_table *htab;
10592
10593 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10594 info->keep_memory);
10595 if (relstart == NULL)
10596 return -1;
10597
10598 /* Look for branches to outside of this section. */
10599 local_syms = NULL;
10600 htab = ppc_hash_table (info);
10601 if (htab == NULL)
10602 return -1;
10603
10604 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10605 {
10606 enum elf_ppc64_reloc_type r_type;
10607 unsigned long r_symndx;
10608 struct elf_link_hash_entry *h;
10609 struct ppc_link_hash_entry *eh;
10610 Elf_Internal_Sym *sym;
10611 asection *sym_sec;
10612 struct _opd_sec_data *opd;
10613 bfd_vma sym_value;
10614 bfd_vma dest;
10615
10616 r_type = ELF64_R_TYPE (rel->r_info);
10617 if (r_type != R_PPC64_REL24
10618 && r_type != R_PPC64_REL14
10619 && r_type != R_PPC64_REL14_BRTAKEN
10620 && r_type != R_PPC64_REL14_BRNTAKEN)
10621 continue;
10622
10623 r_symndx = ELF64_R_SYM (rel->r_info);
10624 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10625 isec->owner))
10626 {
10627 ret = -1;
10628 break;
10629 }
10630
10631 /* Calls to dynamic lib functions go through a plt call stub
10632 that uses r2. */
10633 eh = (struct ppc_link_hash_entry *) h;
10634 if (eh != NULL
10635 && (eh->elf.plt.plist != NULL
10636 || (eh->oh != NULL
10637 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10638 {
10639 ret = 1;
10640 break;
10641 }
10642
10643 if (sym_sec == NULL)
10644 /* Ignore other undefined symbols. */
10645 continue;
10646
10647 /* Assume branches to other sections not included in the
10648 link need stubs too, to cover -R and absolute syms. */
10649 if (sym_sec->output_section == NULL)
10650 {
10651 ret = 1;
10652 break;
10653 }
10654
10655 if (h == NULL)
10656 sym_value = sym->st_value;
10657 else
10658 {
10659 if (h->root.type != bfd_link_hash_defined
10660 && h->root.type != bfd_link_hash_defweak)
10661 abort ();
10662 sym_value = h->root.u.def.value;
10663 }
10664 sym_value += rel->r_addend;
10665
10666 /* If this branch reloc uses an opd sym, find the code section. */
10667 opd = get_opd_info (sym_sec);
10668 if (opd != NULL)
10669 {
10670 if (h == NULL && opd->adjust != NULL)
10671 {
10672 long adjust;
10673
10674 adjust = opd->adjust[sym->st_value / 8];
10675 if (adjust == -1)
10676 /* Assume deleted functions won't ever be called. */
10677 continue;
10678 sym_value += adjust;
10679 }
10680
10681 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10682 if (dest == (bfd_vma) -1)
10683 continue;
10684 }
10685 else
10686 dest = (sym_value
10687 + sym_sec->output_offset
10688 + sym_sec->output_section->vma);
10689
10690 /* Ignore branch to self. */
10691 if (sym_sec == isec)
10692 continue;
10693
10694 /* If the called function uses the toc, we need a stub. */
10695 if (sym_sec->has_toc_reloc
10696 || sym_sec->makes_toc_func_call)
10697 {
10698 ret = 1;
10699 break;
10700 }
10701
10702 /* Assume any branch that needs a long branch stub might in fact
10703 need a plt_branch stub. A plt_branch stub uses r2. */
10704 else if (dest - (isec->output_offset
10705 + isec->output_section->vma
10706 + rel->r_offset) + (1 << 25) >= (2 << 25))
10707 {
10708 ret = 1;
10709 break;
10710 }
10711
10712 /* If calling back to a section in the process of being
10713 tested, we can't say for sure that no toc adjusting stubs
10714 are needed, so don't return zero. */
10715 else if (sym_sec->call_check_in_progress)
10716 ret = 2;
10717
10718 /* Branches to another section that itself doesn't have any TOC
10719 references are OK. Recursively call ourselves to check. */
10720 else if (!sym_sec->call_check_done)
10721 {
10722 int recur;
10723
10724 /* Mark current section as indeterminate, so that other
10725 sections that call back to current won't be marked as
10726 known. */
10727 isec->call_check_in_progress = 1;
10728 recur = toc_adjusting_stub_needed (info, sym_sec);
10729 isec->call_check_in_progress = 0;
10730
10731 if (recur != 0)
10732 {
10733 ret = recur;
10734 if (recur != 2)
10735 break;
10736 }
10737 }
10738 }
10739
10740 if (local_syms != NULL
10741 && (elf_symtab_hdr (isec->owner).contents
10742 != (unsigned char *) local_syms))
10743 free (local_syms);
10744 if (elf_section_data (isec)->relocs != relstart)
10745 free (relstart);
10746 }
10747
10748 if ((ret & 1) == 0
10749 && isec->map_head.s != NULL
10750 && (strcmp (isec->output_section->name, ".init") == 0
10751 || strcmp (isec->output_section->name, ".fini") == 0))
10752 {
10753 if (isec->map_head.s->has_toc_reloc
10754 || isec->map_head.s->makes_toc_func_call)
10755 ret = 1;
10756 else if (!isec->map_head.s->call_check_done)
10757 {
10758 int recur;
10759 isec->call_check_in_progress = 1;
10760 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10761 isec->call_check_in_progress = 0;
10762 if (recur != 0)
10763 ret = recur;
10764 }
10765 }
10766
10767 if (ret == 1)
10768 isec->makes_toc_func_call = 1;
10769
10770 return ret;
10771 }
10772
10773 /* The linker repeatedly calls this function for each input section,
10774 in the order that input sections are linked into output sections.
10775 Build lists of input sections to determine groupings between which
10776 we may insert linker stubs. */
10777
10778 bfd_boolean
10779 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10780 {
10781 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10782
10783 if (htab == NULL)
10784 return FALSE;
10785
10786 if ((isec->output_section->flags & SEC_CODE) != 0
10787 && isec->output_section->index <= htab->top_index)
10788 {
10789 asection **list = htab->input_list + isec->output_section->index;
10790 /* Steal the link_sec pointer for our list. */
10791 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10792 /* This happens to make the list in reverse order,
10793 which is what we want. */
10794 PREV_SEC (isec) = *list;
10795 *list = isec;
10796 }
10797
10798 if (htab->multi_toc_needed)
10799 {
10800 /* If a code section has a function that uses the TOC then we need
10801 to use the right TOC (obviously). Also, make sure that .opd gets
10802 the correct TOC value for R_PPC64_TOC relocs that don't have or
10803 can't find their function symbol (shouldn't ever happen now).
10804 Also specially treat .fixup for the linux kernel. .fixup
10805 contains branches, but only back to the function that hit an
10806 exception. */
10807 if (isec->has_toc_reloc
10808 || (isec->flags & SEC_CODE) == 0
10809 || strcmp (isec->name, ".fixup") == 0)
10810 {
10811 if (elf_gp (isec->owner) != 0)
10812 htab->toc_curr = elf_gp (isec->owner);
10813 }
10814 else
10815 {
10816 if (!isec->call_check_done
10817 && toc_adjusting_stub_needed (info, isec) < 0)
10818 return FALSE;
10819 /* If we make a local call from this section, ie. a branch
10820 without a following nop, then we have no place to put a
10821 toc restoring insn. We must use the same toc group as
10822 the callee.
10823 Testing makes_toc_func_call actually tests for *any*
10824 calls to functions that need a good toc pointer. A more
10825 precise test would be better, as this one will set
10826 incorrect values for pasted .init/.fini fragments.
10827 (Fixed later in check_pasted_section.) */
10828 if (isec->makes_toc_func_call
10829 && elf_gp (isec->owner) != 0)
10830 htab->toc_curr = elf_gp (isec->owner);
10831 }
10832 }
10833
10834 /* Functions that don't use the TOC can belong in any TOC group.
10835 Use the last TOC base. */
10836 htab->stub_group[isec->id].toc_off = htab->toc_curr;
10837 return TRUE;
10838 }
10839
10840 /* Check that all .init and .fini sections use the same toc, if they
10841 have toc relocs. */
10842
10843 static bfd_boolean
10844 check_pasted_section (struct bfd_link_info *info, const char *name)
10845 {
10846 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10847
10848 if (o != NULL)
10849 {
10850 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10851 bfd_vma toc_off = 0;
10852 asection *i;
10853
10854 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10855 if (i->has_toc_reloc)
10856 {
10857 if (toc_off == 0)
10858 toc_off = htab->stub_group[i->id].toc_off;
10859 else if (toc_off != htab->stub_group[i->id].toc_off)
10860 return FALSE;
10861 }
10862
10863 if (toc_off == 0)
10864 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10865 if (i->makes_toc_func_call)
10866 {
10867 toc_off = htab->stub_group[i->id].toc_off;
10868 break;
10869 }
10870
10871 /* Make sure the whole pasted function uses the same toc offset. */
10872 if (toc_off != 0)
10873 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10874 htab->stub_group[i->id].toc_off = toc_off;
10875 }
10876 return TRUE;
10877 }
10878
10879 bfd_boolean
10880 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10881 {
10882 return (check_pasted_section (info, ".init")
10883 & check_pasted_section (info, ".fini"));
10884 }
10885
10886 /* See whether we can group stub sections together. Grouping stub
10887 sections may result in fewer stubs. More importantly, we need to
10888 put all .init* and .fini* stubs at the beginning of the .init or
10889 .fini output sections respectively, because glibc splits the
10890 _init and _fini functions into multiple parts. Putting a stub in
10891 the middle of a function is not a good idea. */
10892
10893 static void
10894 group_sections (struct ppc_link_hash_table *htab,
10895 bfd_size_type stub_group_size,
10896 bfd_boolean stubs_always_before_branch)
10897 {
10898 asection **list;
10899 bfd_size_type stub14_group_size;
10900 bfd_boolean suppress_size_errors;
10901
10902 suppress_size_errors = FALSE;
10903 stub14_group_size = stub_group_size;
10904 if (stub_group_size == 1)
10905 {
10906 /* Default values. */
10907 if (stubs_always_before_branch)
10908 {
10909 stub_group_size = 0x1e00000;
10910 stub14_group_size = 0x7800;
10911 }
10912 else
10913 {
10914 stub_group_size = 0x1c00000;
10915 stub14_group_size = 0x7000;
10916 }
10917 suppress_size_errors = TRUE;
10918 }
10919
10920 list = htab->input_list + htab->top_index;
10921 do
10922 {
10923 asection *tail = *list;
10924 while (tail != NULL)
10925 {
10926 asection *curr;
10927 asection *prev;
10928 bfd_size_type total;
10929 bfd_boolean big_sec;
10930 bfd_vma curr_toc;
10931
10932 curr = tail;
10933 total = tail->size;
10934 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10935 && ppc64_elf_section_data (tail)->has_14bit_branch
10936 ? stub14_group_size : stub_group_size);
10937 if (big_sec && !suppress_size_errors)
10938 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10939 tail->owner, tail);
10940 curr_toc = htab->stub_group[tail->id].toc_off;
10941
10942 while ((prev = PREV_SEC (curr)) != NULL
10943 && ((total += curr->output_offset - prev->output_offset)
10944 < (ppc64_elf_section_data (prev) != NULL
10945 && ppc64_elf_section_data (prev)->has_14bit_branch
10946 ? stub14_group_size : stub_group_size))
10947 && htab->stub_group[prev->id].toc_off == curr_toc)
10948 curr = prev;
10949
10950 /* OK, the size from the start of CURR to the end is less
10951 than stub_group_size and thus can be handled by one stub
10952 section. (or the tail section is itself larger than
10953 stub_group_size, in which case we may be toast.) We
10954 should really be keeping track of the total size of stubs
10955 added here, as stubs contribute to the final output
10956 section size. That's a little tricky, and this way will
10957 only break if stubs added make the total size more than
10958 2^25, ie. for the default stub_group_size, if stubs total
10959 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10960 do
10961 {
10962 prev = PREV_SEC (tail);
10963 /* Set up this stub group. */
10964 htab->stub_group[tail->id].link_sec = curr;
10965 }
10966 while (tail != curr && (tail = prev) != NULL);
10967
10968 /* But wait, there's more! Input sections up to stub_group_size
10969 bytes before the stub section can be handled by it too.
10970 Don't do this if we have a really large section after the
10971 stubs, as adding more stubs increases the chance that
10972 branches may not reach into the stub section. */
10973 if (!stubs_always_before_branch && !big_sec)
10974 {
10975 total = 0;
10976 while (prev != NULL
10977 && ((total += tail->output_offset - prev->output_offset)
10978 < (ppc64_elf_section_data (prev) != NULL
10979 && ppc64_elf_section_data (prev)->has_14bit_branch
10980 ? stub14_group_size : stub_group_size))
10981 && htab->stub_group[prev->id].toc_off == curr_toc)
10982 {
10983 tail = prev;
10984 prev = PREV_SEC (tail);
10985 htab->stub_group[tail->id].link_sec = curr;
10986 }
10987 }
10988 tail = prev;
10989 }
10990 }
10991 while (list-- != htab->input_list);
10992 free (htab->input_list);
10993 #undef PREV_SEC
10994 }
10995
10996 static const unsigned char glink_eh_frame_cie[] =
10997 {
10998 0, 0, 0, 16, /* length. */
10999 0, 0, 0, 0, /* id. */
11000 1, /* CIE version. */
11001 'z', 'R', 0, /* Augmentation string. */
11002 4, /* Code alignment. */
11003 0x78, /* Data alignment. */
11004 65, /* RA reg. */
11005 1, /* Augmentation size. */
11006 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11007 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11008 };
11009
11010 /* Stripping output sections is normally done before dynamic section
11011 symbols have been allocated. This function is called later, and
11012 handles cases like htab->brlt which is mapped to its own output
11013 section. */
11014
11015 static void
11016 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11017 {
11018 if (isec->size == 0
11019 && isec->output_section->size == 0
11020 && !bfd_section_removed_from_list (info->output_bfd,
11021 isec->output_section)
11022 && elf_section_data (isec->output_section)->dynindx == 0)
11023 {
11024 isec->output_section->flags |= SEC_EXCLUDE;
11025 bfd_section_list_remove (info->output_bfd, isec->output_section);
11026 info->output_bfd->section_count--;
11027 }
11028 }
11029
11030 /* Determine and set the size of the stub section for a final link.
11031
11032 The basic idea here is to examine all the relocations looking for
11033 PC-relative calls to a target that is unreachable with a "bl"
11034 instruction. */
11035
11036 bfd_boolean
11037 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11038 bfd_boolean plt_static_chain)
11039 {
11040 bfd_size_type stub_group_size;
11041 bfd_boolean stubs_always_before_branch;
11042 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11043
11044 if (htab == NULL)
11045 return FALSE;
11046
11047 htab->plt_static_chain = plt_static_chain;
11048 stubs_always_before_branch = group_size < 0;
11049 if (group_size < 0)
11050 stub_group_size = -group_size;
11051 else
11052 stub_group_size = group_size;
11053
11054 group_sections (htab, stub_group_size, stubs_always_before_branch);
11055
11056 while (1)
11057 {
11058 bfd *input_bfd;
11059 unsigned int bfd_indx;
11060 asection *stub_sec;
11061
11062 htab->stub_iteration += 1;
11063
11064 for (input_bfd = info->input_bfds, bfd_indx = 0;
11065 input_bfd != NULL;
11066 input_bfd = input_bfd->link_next, bfd_indx++)
11067 {
11068 Elf_Internal_Shdr *symtab_hdr;
11069 asection *section;
11070 Elf_Internal_Sym *local_syms = NULL;
11071
11072 if (!is_ppc64_elf (input_bfd))
11073 continue;
11074
11075 /* We'll need the symbol table in a second. */
11076 symtab_hdr = &elf_symtab_hdr (input_bfd);
11077 if (symtab_hdr->sh_info == 0)
11078 continue;
11079
11080 /* Walk over each section attached to the input bfd. */
11081 for (section = input_bfd->sections;
11082 section != NULL;
11083 section = section->next)
11084 {
11085 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11086
11087 /* If there aren't any relocs, then there's nothing more
11088 to do. */
11089 if ((section->flags & SEC_RELOC) == 0
11090 || (section->flags & SEC_ALLOC) == 0
11091 || (section->flags & SEC_LOAD) == 0
11092 || (section->flags & SEC_CODE) == 0
11093 || section->reloc_count == 0)
11094 continue;
11095
11096 /* If this section is a link-once section that will be
11097 discarded, then don't create any stubs. */
11098 if (section->output_section == NULL
11099 || section->output_section->owner != info->output_bfd)
11100 continue;
11101
11102 /* Get the relocs. */
11103 internal_relocs
11104 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11105 info->keep_memory);
11106 if (internal_relocs == NULL)
11107 goto error_ret_free_local;
11108
11109 /* Now examine each relocation. */
11110 irela = internal_relocs;
11111 irelaend = irela + section->reloc_count;
11112 for (; irela < irelaend; irela++)
11113 {
11114 enum elf_ppc64_reloc_type r_type;
11115 unsigned int r_indx;
11116 enum ppc_stub_type stub_type;
11117 struct ppc_stub_hash_entry *stub_entry;
11118 asection *sym_sec, *code_sec;
11119 bfd_vma sym_value, code_value;
11120 bfd_vma destination;
11121 bfd_boolean ok_dest;
11122 struct ppc_link_hash_entry *hash;
11123 struct ppc_link_hash_entry *fdh;
11124 struct elf_link_hash_entry *h;
11125 Elf_Internal_Sym *sym;
11126 char *stub_name;
11127 const asection *id_sec;
11128 struct _opd_sec_data *opd;
11129 struct plt_entry *plt_ent;
11130
11131 r_type = ELF64_R_TYPE (irela->r_info);
11132 r_indx = ELF64_R_SYM (irela->r_info);
11133
11134 if (r_type >= R_PPC64_max)
11135 {
11136 bfd_set_error (bfd_error_bad_value);
11137 goto error_ret_free_internal;
11138 }
11139
11140 /* Only look for stubs on branch instructions. */
11141 if (r_type != R_PPC64_REL24
11142 && r_type != R_PPC64_REL14
11143 && r_type != R_PPC64_REL14_BRTAKEN
11144 && r_type != R_PPC64_REL14_BRNTAKEN)
11145 continue;
11146
11147 /* Now determine the call target, its name, value,
11148 section. */
11149 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11150 r_indx, input_bfd))
11151 goto error_ret_free_internal;
11152 hash = (struct ppc_link_hash_entry *) h;
11153
11154 ok_dest = FALSE;
11155 fdh = NULL;
11156 sym_value = 0;
11157 if (hash == NULL)
11158 {
11159 sym_value = sym->st_value;
11160 ok_dest = TRUE;
11161 }
11162 else if (hash->elf.root.type == bfd_link_hash_defined
11163 || hash->elf.root.type == bfd_link_hash_defweak)
11164 {
11165 sym_value = hash->elf.root.u.def.value;
11166 if (sym_sec->output_section != NULL)
11167 ok_dest = TRUE;
11168 }
11169 else if (hash->elf.root.type == bfd_link_hash_undefweak
11170 || hash->elf.root.type == bfd_link_hash_undefined)
11171 {
11172 /* Recognise an old ABI func code entry sym, and
11173 use the func descriptor sym instead if it is
11174 defined. */
11175 if (hash->elf.root.root.string[0] == '.'
11176 && (fdh = lookup_fdh (hash, htab)) != NULL)
11177 {
11178 if (fdh->elf.root.type == bfd_link_hash_defined
11179 || fdh->elf.root.type == bfd_link_hash_defweak)
11180 {
11181 sym_sec = fdh->elf.root.u.def.section;
11182 sym_value = fdh->elf.root.u.def.value;
11183 if (sym_sec->output_section != NULL)
11184 ok_dest = TRUE;
11185 }
11186 else
11187 fdh = NULL;
11188 }
11189 }
11190 else
11191 {
11192 bfd_set_error (bfd_error_bad_value);
11193 goto error_ret_free_internal;
11194 }
11195
11196 destination = 0;
11197 if (ok_dest)
11198 {
11199 sym_value += irela->r_addend;
11200 destination = (sym_value
11201 + sym_sec->output_offset
11202 + sym_sec->output_section->vma);
11203 }
11204
11205 code_sec = sym_sec;
11206 code_value = sym_value;
11207 opd = get_opd_info (sym_sec);
11208 if (opd != NULL)
11209 {
11210 bfd_vma dest;
11211
11212 if (hash == NULL && opd->adjust != NULL)
11213 {
11214 long adjust = opd->adjust[sym_value / 8];
11215 if (adjust == -1)
11216 continue;
11217 code_value += adjust;
11218 sym_value += adjust;
11219 }
11220 dest = opd_entry_value (sym_sec, sym_value,
11221 &code_sec, &code_value);
11222 if (dest != (bfd_vma) -1)
11223 {
11224 destination = dest;
11225 if (fdh != NULL)
11226 {
11227 /* Fixup old ABI sym to point at code
11228 entry. */
11229 hash->elf.root.type = bfd_link_hash_defweak;
11230 hash->elf.root.u.def.section = code_sec;
11231 hash->elf.root.u.def.value = code_value;
11232 }
11233 }
11234 }
11235
11236 /* Determine what (if any) linker stub is needed. */
11237 plt_ent = NULL;
11238 stub_type = ppc_type_of_stub (section, irela, &hash,
11239 &plt_ent, destination);
11240
11241 if (stub_type != ppc_stub_plt_call)
11242 {
11243 /* Check whether we need a TOC adjusting stub.
11244 Since the linker pastes together pieces from
11245 different object files when creating the
11246 _init and _fini functions, it may be that a
11247 call to what looks like a local sym is in
11248 fact a call needing a TOC adjustment. */
11249 if (code_sec != NULL
11250 && code_sec->output_section != NULL
11251 && (htab->stub_group[code_sec->id].toc_off
11252 != htab->stub_group[section->id].toc_off)
11253 && (code_sec->has_toc_reloc
11254 || code_sec->makes_toc_func_call))
11255 stub_type = ppc_stub_long_branch_r2off;
11256 }
11257
11258 if (stub_type == ppc_stub_none)
11259 continue;
11260
11261 /* __tls_get_addr calls might be eliminated. */
11262 if (stub_type != ppc_stub_plt_call
11263 && hash != NULL
11264 && (hash == htab->tls_get_addr
11265 || hash == htab->tls_get_addr_fd)
11266 && section->has_tls_reloc
11267 && irela != internal_relocs)
11268 {
11269 /* Get tls info. */
11270 unsigned char *tls_mask;
11271
11272 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11273 irela - 1, input_bfd))
11274 goto error_ret_free_internal;
11275 if (*tls_mask != 0)
11276 continue;
11277 }
11278
11279 if (stub_type == ppc_stub_plt_call
11280 && irela + 1 < irelaend
11281 && irela[1].r_offset == irela->r_offset + 4
11282 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE
11283 && !tocsave_find (htab, INSERT,
11284 &local_syms, irela + 1, input_bfd))
11285 goto error_ret_free_internal;
11286
11287 /* Support for grouping stub sections. */
11288 id_sec = htab->stub_group[section->id].link_sec;
11289
11290 /* Get the name of this stub. */
11291 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11292 if (!stub_name)
11293 goto error_ret_free_internal;
11294
11295 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11296 stub_name, FALSE, FALSE);
11297 if (stub_entry != NULL)
11298 {
11299 /* The proper stub has already been created. */
11300 free (stub_name);
11301 continue;
11302 }
11303
11304 stub_entry = ppc_add_stub (stub_name, section, info);
11305 if (stub_entry == NULL)
11306 {
11307 free (stub_name);
11308 error_ret_free_internal:
11309 if (elf_section_data (section)->relocs == NULL)
11310 free (internal_relocs);
11311 error_ret_free_local:
11312 if (local_syms != NULL
11313 && (symtab_hdr->contents
11314 != (unsigned char *) local_syms))
11315 free (local_syms);
11316 return FALSE;
11317 }
11318
11319 stub_entry->stub_type = stub_type;
11320 if (stub_type != ppc_stub_plt_call)
11321 {
11322 stub_entry->target_value = code_value;
11323 stub_entry->target_section = code_sec;
11324 }
11325 else
11326 {
11327 stub_entry->target_value = sym_value;
11328 stub_entry->target_section = sym_sec;
11329 }
11330 stub_entry->h = hash;
11331 stub_entry->plt_ent = plt_ent;
11332 stub_entry->addend = irela->r_addend;
11333
11334 if (stub_entry->h != NULL)
11335 htab->stub_globals += 1;
11336 }
11337
11338 /* We're done with the internal relocs, free them. */
11339 if (elf_section_data (section)->relocs != internal_relocs)
11340 free (internal_relocs);
11341 }
11342
11343 if (local_syms != NULL
11344 && symtab_hdr->contents != (unsigned char *) local_syms)
11345 {
11346 if (!info->keep_memory)
11347 free (local_syms);
11348 else
11349 symtab_hdr->contents = (unsigned char *) local_syms;
11350 }
11351 }
11352
11353 /* We may have added some stubs. Find out the new size of the
11354 stub sections. */
11355 for (stub_sec = htab->stub_bfd->sections;
11356 stub_sec != NULL;
11357 stub_sec = stub_sec->next)
11358 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11359 {
11360 stub_sec->rawsize = stub_sec->size;
11361 stub_sec->size = 0;
11362 stub_sec->reloc_count = 0;
11363 stub_sec->flags &= ~SEC_RELOC;
11364 }
11365
11366 htab->brlt->size = 0;
11367 htab->brlt->reloc_count = 0;
11368 htab->brlt->flags &= ~SEC_RELOC;
11369 if (htab->relbrlt != NULL)
11370 htab->relbrlt->size = 0;
11371
11372 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11373
11374 if (info->emitrelocations
11375 && htab->glink != NULL && htab->glink->size != 0)
11376 {
11377 htab->glink->reloc_count = 1;
11378 htab->glink->flags |= SEC_RELOC;
11379 }
11380
11381 if (htab->glink_eh_frame != NULL
11382 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11383 && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11384 {
11385 bfd_size_type size = 0;
11386
11387 for (stub_sec = htab->stub_bfd->sections;
11388 stub_sec != NULL;
11389 stub_sec = stub_sec->next)
11390 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11391 size += 20;
11392 if (htab->glink != NULL && htab->glink->size != 0)
11393 size += 24;
11394 if (size != 0)
11395 size += sizeof (glink_eh_frame_cie);
11396 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11397 htab->glink_eh_frame->size = size;
11398 }
11399
11400 for (stub_sec = htab->stub_bfd->sections;
11401 stub_sec != NULL;
11402 stub_sec = stub_sec->next)
11403 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11404 && stub_sec->rawsize != stub_sec->size)
11405 break;
11406
11407 /* Exit from this loop when no stubs have been added, and no stubs
11408 have changed size. */
11409 if (stub_sec == NULL
11410 && (htab->glink_eh_frame == NULL
11411 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
11412 break;
11413
11414 /* Ask the linker to do its stuff. */
11415 (*htab->layout_sections_again) ();
11416 }
11417
11418 maybe_strip_output (info, htab->brlt);
11419 if (htab->glink_eh_frame != NULL)
11420 maybe_strip_output (info, htab->glink_eh_frame);
11421
11422 return TRUE;
11423 }
11424
11425 /* Called after we have determined section placement. If sections
11426 move, we'll be called again. Provide a value for TOCstart. */
11427
11428 bfd_vma
11429 ppc64_elf_toc (bfd *obfd)
11430 {
11431 asection *s;
11432 bfd_vma TOCstart;
11433
11434 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11435 order. The TOC starts where the first of these sections starts. */
11436 s = bfd_get_section_by_name (obfd, ".got");
11437 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11438 s = bfd_get_section_by_name (obfd, ".toc");
11439 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11440 s = bfd_get_section_by_name (obfd, ".tocbss");
11441 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11442 s = bfd_get_section_by_name (obfd, ".plt");
11443 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11444 {
11445 /* This may happen for
11446 o references to TOC base (SYM@toc / TOC[tc0]) without a
11447 .toc directive
11448 o bad linker script
11449 o --gc-sections and empty TOC sections
11450
11451 FIXME: Warn user? */
11452
11453 /* Look for a likely section. We probably won't even be
11454 using TOCstart. */
11455 for (s = obfd->sections; s != NULL; s = s->next)
11456 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11457 | SEC_EXCLUDE))
11458 == (SEC_ALLOC | SEC_SMALL_DATA))
11459 break;
11460 if (s == NULL)
11461 for (s = obfd->sections; s != NULL; s = s->next)
11462 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11463 == (SEC_ALLOC | SEC_SMALL_DATA))
11464 break;
11465 if (s == NULL)
11466 for (s = obfd->sections; s != NULL; s = s->next)
11467 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11468 == SEC_ALLOC)
11469 break;
11470 if (s == NULL)
11471 for (s = obfd->sections; s != NULL; s = s->next)
11472 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11473 break;
11474 }
11475
11476 TOCstart = 0;
11477 if (s != NULL)
11478 TOCstart = s->output_section->vma + s->output_offset;
11479
11480 return TOCstart;
11481 }
11482
11483 /* Build all the stubs associated with the current output file.
11484 The stubs are kept in a hash table attached to the main linker
11485 hash table. This function is called via gldelf64ppc_finish. */
11486
11487 bfd_boolean
11488 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11489 struct bfd_link_info *info,
11490 char **stats)
11491 {
11492 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11493 asection *stub_sec;
11494 bfd_byte *p;
11495 int stub_sec_count = 0;
11496
11497 if (htab == NULL)
11498 return FALSE;
11499
11500 htab->emit_stub_syms = emit_stub_syms;
11501
11502 /* Allocate memory to hold the linker stubs. */
11503 for (stub_sec = htab->stub_bfd->sections;
11504 stub_sec != NULL;
11505 stub_sec = stub_sec->next)
11506 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11507 && stub_sec->size != 0)
11508 {
11509 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11510 if (stub_sec->contents == NULL)
11511 return FALSE;
11512 /* We want to check that built size is the same as calculated
11513 size. rawsize is a convenient location to use. */
11514 stub_sec->rawsize = stub_sec->size;
11515 stub_sec->size = 0;
11516 }
11517
11518 if (htab->glink != NULL && htab->glink->size != 0)
11519 {
11520 unsigned int indx;
11521 bfd_vma plt0;
11522
11523 /* Build the .glink plt call stub. */
11524 if (htab->emit_stub_syms)
11525 {
11526 struct elf_link_hash_entry *h;
11527 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11528 TRUE, FALSE, FALSE);
11529 if (h == NULL)
11530 return FALSE;
11531 if (h->root.type == bfd_link_hash_new)
11532 {
11533 h->root.type = bfd_link_hash_defined;
11534 h->root.u.def.section = htab->glink;
11535 h->root.u.def.value = 8;
11536 h->ref_regular = 1;
11537 h->def_regular = 1;
11538 h->ref_regular_nonweak = 1;
11539 h->forced_local = 1;
11540 h->non_elf = 0;
11541 }
11542 }
11543 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11544 if (info->emitrelocations)
11545 {
11546 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11547 if (r == NULL)
11548 return FALSE;
11549 r->r_offset = (htab->glink->output_offset
11550 + htab->glink->output_section->vma);
11551 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11552 r->r_addend = plt0;
11553 }
11554 p = htab->glink->contents;
11555 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11556 bfd_put_64 (htab->glink->owner, plt0, p);
11557 p += 8;
11558 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11559 p += 4;
11560 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11561 p += 4;
11562 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11563 p += 4;
11564 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11565 p += 4;
11566 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11567 p += 4;
11568 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11569 p += 4;
11570 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11571 p += 4;
11572 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11573 p += 4;
11574 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11575 p += 4;
11576 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11577 p += 4;
11578 bfd_put_32 (htab->glink->owner, BCTR, p);
11579 p += 4;
11580 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11581 {
11582 bfd_put_32 (htab->glink->owner, NOP, p);
11583 p += 4;
11584 }
11585
11586 /* Build the .glink lazy link call stubs. */
11587 indx = 0;
11588 while (p < htab->glink->contents + htab->glink->size)
11589 {
11590 if (indx < 0x8000)
11591 {
11592 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11593 p += 4;
11594 }
11595 else
11596 {
11597 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11598 p += 4;
11599 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11600 p += 4;
11601 }
11602 bfd_put_32 (htab->glink->owner,
11603 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11604 indx++;
11605 p += 4;
11606 }
11607 htab->glink->rawsize = p - htab->glink->contents;
11608 }
11609
11610 if (htab->brlt->size != 0)
11611 {
11612 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11613 htab->brlt->size);
11614 if (htab->brlt->contents == NULL)
11615 return FALSE;
11616 }
11617 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11618 {
11619 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11620 htab->relbrlt->size);
11621 if (htab->relbrlt->contents == NULL)
11622 return FALSE;
11623 }
11624
11625 if (htab->glink_eh_frame != NULL
11626 && htab->glink_eh_frame->size != 0)
11627 {
11628 bfd_vma val;
11629
11630 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11631 if (p == NULL)
11632 return FALSE;
11633 htab->glink_eh_frame->contents = p;
11634
11635 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11636
11637 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11638 /* CIE length (rewrite in case little-endian). */
11639 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11640 p += sizeof (glink_eh_frame_cie);
11641
11642 for (stub_sec = htab->stub_bfd->sections;
11643 stub_sec != NULL;
11644 stub_sec = stub_sec->next)
11645 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11646 {
11647 /* FDE length. */
11648 bfd_put_32 (htab->elf.dynobj, 16, p);
11649 p += 4;
11650 /* CIE pointer. */
11651 val = p - htab->glink_eh_frame->contents;
11652 bfd_put_32 (htab->elf.dynobj, val, p);
11653 p += 4;
11654 /* Offset to stub section. */
11655 val = (stub_sec->output_section->vma
11656 + stub_sec->output_offset);
11657 val -= (htab->glink_eh_frame->output_section->vma
11658 + htab->glink_eh_frame->output_offset);
11659 val -= p - htab->glink_eh_frame->contents;
11660 if (val + 0x80000000 > 0xffffffff)
11661 {
11662 info->callbacks->einfo
11663 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11664 stub_sec->name);
11665 return FALSE;
11666 }
11667 bfd_put_32 (htab->elf.dynobj, val, p);
11668 p += 4;
11669 /* stub section size. */
11670 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11671 p += 4;
11672 /* Augmentation. */
11673 p += 1;
11674 /* Pad. */
11675 p += 3;
11676 }
11677 if (htab->glink != NULL && htab->glink->size != 0)
11678 {
11679 /* FDE length. */
11680 bfd_put_32 (htab->elf.dynobj, 20, p);
11681 p += 4;
11682 /* CIE pointer. */
11683 val = p - htab->glink_eh_frame->contents;
11684 bfd_put_32 (htab->elf.dynobj, val, p);
11685 p += 4;
11686 /* Offset to .glink. */
11687 val = (htab->glink->output_section->vma
11688 + htab->glink->output_offset
11689 + 8);
11690 val -= (htab->glink_eh_frame->output_section->vma
11691 + htab->glink_eh_frame->output_offset);
11692 val -= p - htab->glink_eh_frame->contents;
11693 if (val + 0x80000000 > 0xffffffff)
11694 {
11695 info->callbacks->einfo
11696 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
11697 htab->glink->name);
11698 return FALSE;
11699 }
11700 bfd_put_32 (htab->elf.dynobj, val, p);
11701 p += 4;
11702 /* .glink size. */
11703 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11704 p += 4;
11705 /* Augmentation. */
11706 p += 1;
11707
11708 *p++ = DW_CFA_advance_loc + 1;
11709 *p++ = DW_CFA_register;
11710 *p++ = 65;
11711 *p++ = 12;
11712 *p++ = DW_CFA_advance_loc + 4;
11713 *p++ = DW_CFA_restore_extended;
11714 *p++ = 65;
11715 }
11716 htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11717 }
11718
11719 /* Build the stubs as directed by the stub hash table. */
11720 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11721
11722 if (htab->relbrlt != NULL)
11723 htab->relbrlt->reloc_count = 0;
11724
11725 for (stub_sec = htab->stub_bfd->sections;
11726 stub_sec != NULL;
11727 stub_sec = stub_sec->next)
11728 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11729 {
11730 stub_sec_count += 1;
11731 if (stub_sec->rawsize != stub_sec->size)
11732 break;
11733 }
11734
11735 if (stub_sec != NULL
11736 || htab->glink->rawsize != htab->glink->size
11737 || (htab->glink_eh_frame != NULL
11738 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
11739 {
11740 htab->stub_error = TRUE;
11741 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
11742 }
11743
11744 if (htab->stub_error)
11745 return FALSE;
11746
11747 if (stats != NULL)
11748 {
11749 *stats = bfd_malloc (500);
11750 if (*stats == NULL)
11751 return FALSE;
11752
11753 sprintf (*stats, _("linker stubs in %u group%s\n"
11754 " branch %lu\n"
11755 " toc adjust %lu\n"
11756 " long branch %lu\n"
11757 " long toc adj %lu\n"
11758 " plt call %lu"),
11759 stub_sec_count,
11760 stub_sec_count == 1 ? "" : "s",
11761 htab->stub_count[ppc_stub_long_branch - 1],
11762 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11763 htab->stub_count[ppc_stub_plt_branch - 1],
11764 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11765 htab->stub_count[ppc_stub_plt_call - 1]);
11766 }
11767 return TRUE;
11768 }
11769
11770 /* This function undoes the changes made by add_symbol_adjust. */
11771
11772 static bfd_boolean
11773 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11774 {
11775 struct ppc_link_hash_entry *eh;
11776
11777 if (h->root.type == bfd_link_hash_indirect)
11778 return TRUE;
11779
11780 eh = (struct ppc_link_hash_entry *) h;
11781 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11782 return TRUE;
11783
11784 eh->elf.root.type = bfd_link_hash_undefined;
11785 return TRUE;
11786 }
11787
11788 void
11789 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11790 {
11791 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11792
11793 if (htab != NULL)
11794 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11795 }
11796
11797 /* What to do when ld finds relocations against symbols defined in
11798 discarded sections. */
11799
11800 static unsigned int
11801 ppc64_elf_action_discarded (asection *sec)
11802 {
11803 if (strcmp (".opd", sec->name) == 0)
11804 return 0;
11805
11806 if (strcmp (".toc", sec->name) == 0)
11807 return 0;
11808
11809 if (strcmp (".toc1", sec->name) == 0)
11810 return 0;
11811
11812 return _bfd_elf_default_action_discarded (sec);
11813 }
11814
11815 /* The RELOCATE_SECTION function is called by the ELF backend linker
11816 to handle the relocations for a section.
11817
11818 The relocs are always passed as Rela structures; if the section
11819 actually uses Rel structures, the r_addend field will always be
11820 zero.
11821
11822 This function is responsible for adjust the section contents as
11823 necessary, and (if using Rela relocs and generating a
11824 relocatable output file) adjusting the reloc addend as
11825 necessary.
11826
11827 This function does not have to worry about setting the reloc
11828 address or the reloc symbol index.
11829
11830 LOCAL_SYMS is a pointer to the swapped in local symbols.
11831
11832 LOCAL_SECTIONS is an array giving the section in the input file
11833 corresponding to the st_shndx field of each local symbol.
11834
11835 The global hash table entry for the global symbols can be found
11836 via elf_sym_hashes (input_bfd).
11837
11838 When generating relocatable output, this function must handle
11839 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11840 going to be the section symbol corresponding to the output
11841 section, which means that the addend must be adjusted
11842 accordingly. */
11843
11844 static bfd_boolean
11845 ppc64_elf_relocate_section (bfd *output_bfd,
11846 struct bfd_link_info *info,
11847 bfd *input_bfd,
11848 asection *input_section,
11849 bfd_byte *contents,
11850 Elf_Internal_Rela *relocs,
11851 Elf_Internal_Sym *local_syms,
11852 asection **local_sections)
11853 {
11854 struct ppc_link_hash_table *htab;
11855 Elf_Internal_Shdr *symtab_hdr;
11856 struct elf_link_hash_entry **sym_hashes;
11857 Elf_Internal_Rela *rel;
11858 Elf_Internal_Rela *relend;
11859 Elf_Internal_Rela outrel;
11860 bfd_byte *loc;
11861 struct got_entry **local_got_ents;
11862 bfd_vma TOCstart;
11863 bfd_boolean ret = TRUE;
11864 bfd_boolean is_opd;
11865 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
11866 bfd_boolean is_power4 = FALSE;
11867 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11868
11869 /* Initialize howto table if needed. */
11870 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11871 ppc_howto_init ();
11872
11873 htab = ppc_hash_table (info);
11874 if (htab == NULL)
11875 return FALSE;
11876
11877 /* Don't relocate stub sections. */
11878 if (input_section->owner == htab->stub_bfd)
11879 return TRUE;
11880
11881 BFD_ASSERT (is_ppc64_elf (input_bfd));
11882
11883 local_got_ents = elf_local_got_ents (input_bfd);
11884 TOCstart = elf_gp (output_bfd);
11885 symtab_hdr = &elf_symtab_hdr (input_bfd);
11886 sym_hashes = elf_sym_hashes (input_bfd);
11887 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11888
11889 rel = relocs;
11890 relend = relocs + input_section->reloc_count;
11891 for (; rel < relend; rel++)
11892 {
11893 enum elf_ppc64_reloc_type r_type;
11894 bfd_vma addend, orig_addend;
11895 bfd_reloc_status_type r;
11896 Elf_Internal_Sym *sym;
11897 asection *sec;
11898 struct elf_link_hash_entry *h_elf;
11899 struct ppc_link_hash_entry *h;
11900 struct ppc_link_hash_entry *fdh;
11901 const char *sym_name;
11902 unsigned long r_symndx, toc_symndx;
11903 bfd_vma toc_addend;
11904 unsigned char tls_mask, tls_gd, tls_type;
11905 unsigned char sym_type;
11906 bfd_vma relocation;
11907 bfd_boolean unresolved_reloc;
11908 bfd_boolean warned;
11909 unsigned int insn;
11910 unsigned int mask;
11911 struct ppc_stub_hash_entry *stub_entry;
11912 bfd_vma max_br_offset;
11913 bfd_vma from;
11914
11915 r_type = ELF64_R_TYPE (rel->r_info);
11916 r_symndx = ELF64_R_SYM (rel->r_info);
11917
11918 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11919 symbol of the previous ADDR64 reloc. The symbol gives us the
11920 proper TOC base to use. */
11921 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11922 && rel != relocs
11923 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11924 && is_opd)
11925 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11926
11927 sym = NULL;
11928 sec = NULL;
11929 h_elf = NULL;
11930 sym_name = NULL;
11931 unresolved_reloc = FALSE;
11932 warned = FALSE;
11933 orig_addend = rel->r_addend;
11934
11935 if (r_symndx < symtab_hdr->sh_info)
11936 {
11937 /* It's a local symbol. */
11938 struct _opd_sec_data *opd;
11939
11940 sym = local_syms + r_symndx;
11941 sec = local_sections[r_symndx];
11942 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11943 sym_type = ELF64_ST_TYPE (sym->st_info);
11944 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11945 opd = get_opd_info (sec);
11946 if (opd != NULL && opd->adjust != NULL)
11947 {
11948 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11949 if (adjust == -1)
11950 relocation = 0;
11951 else
11952 {
11953 /* If this is a relocation against the opd section sym
11954 and we have edited .opd, adjust the reloc addend so
11955 that ld -r and ld --emit-relocs output is correct.
11956 If it is a reloc against some other .opd symbol,
11957 then the symbol value will be adjusted later. */
11958 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11959 rel->r_addend += adjust;
11960 else
11961 relocation += adjust;
11962 }
11963 }
11964 }
11965 else
11966 {
11967 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11968 r_symndx, symtab_hdr, sym_hashes,
11969 h_elf, sec, relocation,
11970 unresolved_reloc, warned);
11971 sym_name = h_elf->root.root.string;
11972 sym_type = h_elf->type;
11973 }
11974 h = (struct ppc_link_hash_entry *) h_elf;
11975
11976 if (sec != NULL && elf_discarded_section (sec))
11977 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11978 rel, relend,
11979 ppc64_elf_howto_table[r_type],
11980 contents);
11981
11982 if (info->relocatable)
11983 continue;
11984
11985 /* TLS optimizations. Replace instruction sequences and relocs
11986 based on information we collected in tls_optimize. We edit
11987 RELOCS so that --emit-relocs will output something sensible
11988 for the final instruction stream. */
11989 tls_mask = 0;
11990 tls_gd = 0;
11991 toc_symndx = 0;
11992 if (h != NULL)
11993 tls_mask = h->tls_mask;
11994 else if (local_got_ents != NULL)
11995 {
11996 struct plt_entry **local_plt = (struct plt_entry **)
11997 (local_got_ents + symtab_hdr->sh_info);
11998 unsigned char *lgot_masks = (unsigned char *)
11999 (local_plt + symtab_hdr->sh_info);
12000 tls_mask = lgot_masks[r_symndx];
12001 }
12002 if (tls_mask == 0
12003 && (r_type == R_PPC64_TLS
12004 || r_type == R_PPC64_TLSGD
12005 || r_type == R_PPC64_TLSLD))
12006 {
12007 /* Check for toc tls entries. */
12008 unsigned char *toc_tls;
12009
12010 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12011 &local_syms, rel, input_bfd))
12012 return FALSE;
12013
12014 if (toc_tls)
12015 tls_mask = *toc_tls;
12016 }
12017
12018 /* Check that tls relocs are used with tls syms, and non-tls
12019 relocs are used with non-tls syms. */
12020 if (r_symndx != STN_UNDEF
12021 && r_type != R_PPC64_NONE
12022 && (h == NULL
12023 || h->elf.root.type == bfd_link_hash_defined
12024 || h->elf.root.type == bfd_link_hash_defweak)
12025 && (IS_PPC64_TLS_RELOC (r_type)
12026 != (sym_type == STT_TLS
12027 || (sym_type == STT_SECTION
12028 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12029 {
12030 if (tls_mask != 0
12031 && (r_type == R_PPC64_TLS
12032 || r_type == R_PPC64_TLSGD
12033 || r_type == R_PPC64_TLSLD))
12034 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12035 ;
12036 else
12037 info->callbacks->einfo
12038 (!IS_PPC64_TLS_RELOC (r_type)
12039 ? _("%P: %H: %s used with TLS symbol %s\n")
12040 : _("%P: %H: %s used with non-TLS symbol %s\n"),
12041 input_bfd, input_section, rel->r_offset,
12042 ppc64_elf_howto_table[r_type]->name,
12043 sym_name);
12044 }
12045
12046 /* Ensure reloc mapping code below stays sane. */
12047 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12048 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12049 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12050 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12051 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12052 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12053 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12054 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12055 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12056 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12057 abort ();
12058
12059 switch (r_type)
12060 {
12061 default:
12062 break;
12063
12064 case R_PPC64_LO_DS_OPT:
12065 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12066 if ((insn & (0x3f << 26)) != 58u << 26)
12067 abort ();
12068 insn += (14u << 26) - (58u << 26);
12069 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12070 r_type = R_PPC64_TOC16_LO;
12071 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12072 break;
12073
12074 case R_PPC64_TOC16:
12075 case R_PPC64_TOC16_LO:
12076 case R_PPC64_TOC16_DS:
12077 case R_PPC64_TOC16_LO_DS:
12078 {
12079 /* Check for toc tls entries. */
12080 unsigned char *toc_tls;
12081 int retval;
12082
12083 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12084 &local_syms, rel, input_bfd);
12085 if (retval == 0)
12086 return FALSE;
12087
12088 if (toc_tls)
12089 {
12090 tls_mask = *toc_tls;
12091 if (r_type == R_PPC64_TOC16_DS
12092 || r_type == R_PPC64_TOC16_LO_DS)
12093 {
12094 if (tls_mask != 0
12095 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12096 goto toctprel;
12097 }
12098 else
12099 {
12100 /* If we found a GD reloc pair, then we might be
12101 doing a GD->IE transition. */
12102 if (retval == 2)
12103 {
12104 tls_gd = TLS_TPRELGD;
12105 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12106 goto tls_ldgd_opt;
12107 }
12108 else if (retval == 3)
12109 {
12110 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12111 goto tls_ldgd_opt;
12112 }
12113 }
12114 }
12115 }
12116 break;
12117
12118 case R_PPC64_GOT_TPREL16_HI:
12119 case R_PPC64_GOT_TPREL16_HA:
12120 if (tls_mask != 0
12121 && (tls_mask & TLS_TPREL) == 0)
12122 {
12123 rel->r_offset -= d_offset;
12124 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12125 r_type = R_PPC64_NONE;
12126 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12127 }
12128 break;
12129
12130 case R_PPC64_GOT_TPREL16_DS:
12131 case R_PPC64_GOT_TPREL16_LO_DS:
12132 if (tls_mask != 0
12133 && (tls_mask & TLS_TPREL) == 0)
12134 {
12135 toctprel:
12136 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12137 insn &= 31 << 21;
12138 insn |= 0x3c0d0000; /* addis 0,13,0 */
12139 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12140 r_type = R_PPC64_TPREL16_HA;
12141 if (toc_symndx != 0)
12142 {
12143 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12144 rel->r_addend = toc_addend;
12145 /* We changed the symbol. Start over in order to
12146 get h, sym, sec etc. right. */
12147 rel--;
12148 continue;
12149 }
12150 else
12151 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12152 }
12153 break;
12154
12155 case R_PPC64_TLS:
12156 if (tls_mask != 0
12157 && (tls_mask & TLS_TPREL) == 0)
12158 {
12159 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12160 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12161 if (insn == 0)
12162 abort ();
12163 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12164 /* Was PPC64_TLS which sits on insn boundary, now
12165 PPC64_TPREL16_LO which is at low-order half-word. */
12166 rel->r_offset += d_offset;
12167 r_type = R_PPC64_TPREL16_LO;
12168 if (toc_symndx != 0)
12169 {
12170 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12171 rel->r_addend = toc_addend;
12172 /* We changed the symbol. Start over in order to
12173 get h, sym, sec etc. right. */
12174 rel--;
12175 continue;
12176 }
12177 else
12178 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12179 }
12180 break;
12181
12182 case R_PPC64_GOT_TLSGD16_HI:
12183 case R_PPC64_GOT_TLSGD16_HA:
12184 tls_gd = TLS_TPRELGD;
12185 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12186 goto tls_gdld_hi;
12187 break;
12188
12189 case R_PPC64_GOT_TLSLD16_HI:
12190 case R_PPC64_GOT_TLSLD16_HA:
12191 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12192 {
12193 tls_gdld_hi:
12194 if ((tls_mask & tls_gd) != 0)
12195 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12196 + R_PPC64_GOT_TPREL16_DS);
12197 else
12198 {
12199 rel->r_offset -= d_offset;
12200 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12201 r_type = R_PPC64_NONE;
12202 }
12203 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12204 }
12205 break;
12206
12207 case R_PPC64_GOT_TLSGD16:
12208 case R_PPC64_GOT_TLSGD16_LO:
12209 tls_gd = TLS_TPRELGD;
12210 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12211 goto tls_ldgd_opt;
12212 break;
12213
12214 case R_PPC64_GOT_TLSLD16:
12215 case R_PPC64_GOT_TLSLD16_LO:
12216 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12217 {
12218 unsigned int insn1, insn2, insn3;
12219 bfd_vma offset;
12220
12221 tls_ldgd_opt:
12222 offset = (bfd_vma) -1;
12223 /* If not using the newer R_PPC64_TLSGD/LD to mark
12224 __tls_get_addr calls, we must trust that the call
12225 stays with its arg setup insns, ie. that the next
12226 reloc is the __tls_get_addr call associated with
12227 the current reloc. Edit both insns. */
12228 if (input_section->has_tls_get_addr_call
12229 && rel + 1 < relend
12230 && branch_reloc_hash_match (input_bfd, rel + 1,
12231 htab->tls_get_addr,
12232 htab->tls_get_addr_fd))
12233 offset = rel[1].r_offset;
12234 if ((tls_mask & tls_gd) != 0)
12235 {
12236 /* IE */
12237 insn1 = bfd_get_32 (output_bfd,
12238 contents + rel->r_offset - d_offset);
12239 insn1 &= (1 << 26) - (1 << 2);
12240 insn1 |= 58 << 26; /* ld */
12241 insn2 = 0x7c636a14; /* add 3,3,13 */
12242 if (offset != (bfd_vma) -1)
12243 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12244 if ((tls_mask & TLS_EXPLICIT) == 0)
12245 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12246 + R_PPC64_GOT_TPREL16_DS);
12247 else
12248 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12249 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12250 }
12251 else
12252 {
12253 /* LE */
12254 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12255 insn2 = 0x38630000; /* addi 3,3,0 */
12256 if (tls_gd == 0)
12257 {
12258 /* Was an LD reloc. */
12259 if (toc_symndx)
12260 sec = local_sections[toc_symndx];
12261 for (r_symndx = 0;
12262 r_symndx < symtab_hdr->sh_info;
12263 r_symndx++)
12264 if (local_sections[r_symndx] == sec)
12265 break;
12266 if (r_symndx >= symtab_hdr->sh_info)
12267 r_symndx = STN_UNDEF;
12268 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12269 if (r_symndx != STN_UNDEF)
12270 rel->r_addend -= (local_syms[r_symndx].st_value
12271 + sec->output_offset
12272 + sec->output_section->vma);
12273 }
12274 else if (toc_symndx != 0)
12275 {
12276 r_symndx = toc_symndx;
12277 rel->r_addend = toc_addend;
12278 }
12279 r_type = R_PPC64_TPREL16_HA;
12280 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12281 if (offset != (bfd_vma) -1)
12282 {
12283 rel[1].r_info = ELF64_R_INFO (r_symndx,
12284 R_PPC64_TPREL16_LO);
12285 rel[1].r_offset = offset + d_offset;
12286 rel[1].r_addend = rel->r_addend;
12287 }
12288 }
12289 bfd_put_32 (output_bfd, insn1,
12290 contents + rel->r_offset - d_offset);
12291 if (offset != (bfd_vma) -1)
12292 {
12293 insn3 = bfd_get_32 (output_bfd,
12294 contents + offset + 4);
12295 if (insn3 == NOP
12296 || insn3 == CROR_151515 || insn3 == CROR_313131)
12297 {
12298 rel[1].r_offset += 4;
12299 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12300 insn2 = NOP;
12301 }
12302 bfd_put_32 (output_bfd, insn2, contents + offset);
12303 }
12304 if ((tls_mask & tls_gd) == 0
12305 && (tls_gd == 0 || toc_symndx != 0))
12306 {
12307 /* We changed the symbol. Start over in order
12308 to get h, sym, sec etc. right. */
12309 rel--;
12310 continue;
12311 }
12312 }
12313 break;
12314
12315 case R_PPC64_TLSGD:
12316 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12317 {
12318 unsigned int insn2, insn3;
12319 bfd_vma offset = rel->r_offset;
12320
12321 if ((tls_mask & TLS_TPRELGD) != 0)
12322 {
12323 /* IE */
12324 r_type = R_PPC64_NONE;
12325 insn2 = 0x7c636a14; /* add 3,3,13 */
12326 }
12327 else
12328 {
12329 /* LE */
12330 if (toc_symndx != 0)
12331 {
12332 r_symndx = toc_symndx;
12333 rel->r_addend = toc_addend;
12334 }
12335 r_type = R_PPC64_TPREL16_LO;
12336 rel->r_offset = offset + d_offset;
12337 insn2 = 0x38630000; /* addi 3,3,0 */
12338 }
12339 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12340 /* Zap the reloc on the _tls_get_addr call too. */
12341 BFD_ASSERT (offset == rel[1].r_offset);
12342 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12343 insn3 = bfd_get_32 (output_bfd,
12344 contents + offset + 4);
12345 if (insn3 == NOP
12346 || insn3 == CROR_151515 || insn3 == CROR_313131)
12347 {
12348 rel->r_offset += 4;
12349 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12350 insn2 = NOP;
12351 }
12352 bfd_put_32 (output_bfd, insn2, contents + offset);
12353 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12354 {
12355 rel--;
12356 continue;
12357 }
12358 }
12359 break;
12360
12361 case R_PPC64_TLSLD:
12362 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12363 {
12364 unsigned int insn2, insn3;
12365 bfd_vma offset = rel->r_offset;
12366
12367 if (toc_symndx)
12368 sec = local_sections[toc_symndx];
12369 for (r_symndx = 0;
12370 r_symndx < symtab_hdr->sh_info;
12371 r_symndx++)
12372 if (local_sections[r_symndx] == sec)
12373 break;
12374 if (r_symndx >= symtab_hdr->sh_info)
12375 r_symndx = STN_UNDEF;
12376 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12377 if (r_symndx != STN_UNDEF)
12378 rel->r_addend -= (local_syms[r_symndx].st_value
12379 + sec->output_offset
12380 + sec->output_section->vma);
12381
12382 r_type = R_PPC64_TPREL16_LO;
12383 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12384 rel->r_offset = offset + d_offset;
12385 /* Zap the reloc on the _tls_get_addr call too. */
12386 BFD_ASSERT (offset == rel[1].r_offset);
12387 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12388 insn2 = 0x38630000; /* addi 3,3,0 */
12389 insn3 = bfd_get_32 (output_bfd,
12390 contents + offset + 4);
12391 if (insn3 == NOP
12392 || insn3 == CROR_151515 || insn3 == CROR_313131)
12393 {
12394 rel->r_offset += 4;
12395 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12396 insn2 = NOP;
12397 }
12398 bfd_put_32 (output_bfd, insn2, contents + offset);
12399 rel--;
12400 continue;
12401 }
12402 break;
12403
12404 case R_PPC64_DTPMOD64:
12405 if (rel + 1 < relend
12406 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12407 && rel[1].r_offset == rel->r_offset + 8)
12408 {
12409 if ((tls_mask & TLS_GD) == 0)
12410 {
12411 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12412 if ((tls_mask & TLS_TPRELGD) != 0)
12413 r_type = R_PPC64_TPREL64;
12414 else
12415 {
12416 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12417 r_type = R_PPC64_NONE;
12418 }
12419 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12420 }
12421 }
12422 else
12423 {
12424 if ((tls_mask & TLS_LD) == 0)
12425 {
12426 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12427 r_type = R_PPC64_NONE;
12428 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12429 }
12430 }
12431 break;
12432
12433 case R_PPC64_TPREL64:
12434 if ((tls_mask & TLS_TPREL) == 0)
12435 {
12436 r_type = R_PPC64_NONE;
12437 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12438 }
12439 break;
12440 }
12441
12442 /* Handle other relocations that tweak non-addend part of insn. */
12443 insn = 0;
12444 max_br_offset = 1 << 25;
12445 addend = rel->r_addend;
12446 switch (r_type)
12447 {
12448 default:
12449 break;
12450
12451 case R_PPC64_TOCSAVE:
12452 if (relocation + addend == (rel->r_offset
12453 + input_section->output_offset
12454 + input_section->output_section->vma)
12455 && tocsave_find (htab, NO_INSERT,
12456 &local_syms, rel, input_bfd))
12457 {
12458 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12459 if (insn == NOP
12460 || insn == CROR_151515 || insn == CROR_313131)
12461 bfd_put_32 (input_bfd, STD_R2_40R1,
12462 contents + rel->r_offset);
12463 }
12464 break;
12465
12466 /* Branch taken prediction relocations. */
12467 case R_PPC64_ADDR14_BRTAKEN:
12468 case R_PPC64_REL14_BRTAKEN:
12469 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12470 /* Fall thru. */
12471
12472 /* Branch not taken prediction relocations. */
12473 case R_PPC64_ADDR14_BRNTAKEN:
12474 case R_PPC64_REL14_BRNTAKEN:
12475 insn |= bfd_get_32 (output_bfd,
12476 contents + rel->r_offset) & ~(0x01 << 21);
12477 /* Fall thru. */
12478
12479 case R_PPC64_REL14:
12480 max_br_offset = 1 << 15;
12481 /* Fall thru. */
12482
12483 case R_PPC64_REL24:
12484 /* Calls to functions with a different TOC, such as calls to
12485 shared objects, need to alter the TOC pointer. This is
12486 done using a linkage stub. A REL24 branching to these
12487 linkage stubs needs to be followed by a nop, as the nop
12488 will be replaced with an instruction to restore the TOC
12489 base pointer. */
12490 fdh = h;
12491 if (h != NULL
12492 && h->oh != NULL
12493 && h->oh->is_func_descriptor)
12494 fdh = ppc_follow_link (h->oh);
12495 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12496 if (stub_entry != NULL
12497 && (stub_entry->stub_type == ppc_stub_plt_call
12498 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12499 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12500 {
12501 bfd_boolean can_plt_call = FALSE;
12502
12503 if (rel->r_offset + 8 <= input_section->size)
12504 {
12505 unsigned long nop;
12506 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12507 if (nop == NOP
12508 || nop == CROR_151515 || nop == CROR_313131)
12509 {
12510 if (h != NULL
12511 && (h == htab->tls_get_addr_fd
12512 || h == htab->tls_get_addr)
12513 && !htab->no_tls_get_addr_opt)
12514 {
12515 /* Special stub used, leave nop alone. */
12516 }
12517 else
12518 bfd_put_32 (input_bfd, LD_R2_40R1,
12519 contents + rel->r_offset + 4);
12520 can_plt_call = TRUE;
12521 }
12522 }
12523
12524 if (!can_plt_call)
12525 {
12526 if (stub_entry->stub_type == ppc_stub_plt_call)
12527 {
12528 /* If this is a plain branch rather than a branch
12529 and link, don't require a nop. However, don't
12530 allow tail calls in a shared library as they
12531 will result in r2 being corrupted. */
12532 unsigned long br;
12533 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12534 if (info->executable && (br & 1) == 0)
12535 can_plt_call = TRUE;
12536 else
12537 stub_entry = NULL;
12538 }
12539 else if (h != NULL
12540 && strcmp (h->elf.root.root.string,
12541 ".__libc_start_main") == 0)
12542 {
12543 /* Allow crt1 branch to go via a toc adjusting stub. */
12544 can_plt_call = TRUE;
12545 }
12546 else
12547 {
12548 if (strcmp (input_section->output_section->name,
12549 ".init") == 0
12550 || strcmp (input_section->output_section->name,
12551 ".fini") == 0)
12552 info->callbacks->einfo
12553 (_("%P: %H: automatic multiple TOCs "
12554 "not supported using your crt files; "
12555 "recompile with -mminimal-toc or upgrade gcc\n"),
12556 input_bfd, input_section, rel->r_offset);
12557 else
12558 info->callbacks->einfo
12559 (_("%P: %H: sibling call optimization to `%s' "
12560 "does not allow automatic multiple TOCs; "
12561 "recompile with -mminimal-toc or "
12562 "-fno-optimize-sibling-calls, "
12563 "or make `%s' extern\n"),
12564 input_bfd, input_section, rel->r_offset,
12565 sym_name,
12566 sym_name);
12567 bfd_set_error (bfd_error_bad_value);
12568 ret = FALSE;
12569 }
12570 }
12571
12572 if (can_plt_call
12573 && stub_entry->stub_type == ppc_stub_plt_call)
12574 unresolved_reloc = FALSE;
12575 }
12576
12577 if ((stub_entry == NULL
12578 || stub_entry->stub_type == ppc_stub_long_branch
12579 || stub_entry->stub_type == ppc_stub_plt_branch)
12580 && get_opd_info (sec) != NULL)
12581 {
12582 /* The branch destination is the value of the opd entry. */
12583 bfd_vma off = (relocation + addend
12584 - sec->output_section->vma
12585 - sec->output_offset);
12586 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12587 if (dest != (bfd_vma) -1)
12588 {
12589 relocation = dest;
12590 addend = 0;
12591 }
12592 }
12593
12594 /* If the branch is out of reach we ought to have a long
12595 branch stub. */
12596 from = (rel->r_offset
12597 + input_section->output_offset
12598 + input_section->output_section->vma);
12599
12600 if (stub_entry != NULL
12601 && (stub_entry->stub_type == ppc_stub_long_branch
12602 || stub_entry->stub_type == ppc_stub_plt_branch)
12603 && (r_type == R_PPC64_ADDR14_BRTAKEN
12604 || r_type == R_PPC64_ADDR14_BRNTAKEN
12605 || (relocation + addend - from + max_br_offset
12606 < 2 * max_br_offset)))
12607 /* Don't use the stub if this branch is in range. */
12608 stub_entry = NULL;
12609
12610 if (stub_entry != NULL)
12611 {
12612 /* Munge up the value and addend so that we call the stub
12613 rather than the procedure directly. */
12614 relocation = (stub_entry->stub_offset
12615 + stub_entry->stub_sec->output_offset
12616 + stub_entry->stub_sec->output_section->vma);
12617 addend = 0;
12618
12619 if (stub_entry->stub_type == ppc_stub_plt_call
12620 && rel + 1 < relend
12621 && rel[1].r_offset == rel->r_offset + 4
12622 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
12623 relocation += 4;
12624 }
12625
12626 if (insn != 0)
12627 {
12628 if (is_power4)
12629 {
12630 /* Set 'a' bit. This is 0b00010 in BO field for branch
12631 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12632 for branch on CTR insns (BO == 1a00t or 1a01t). */
12633 if ((insn & (0x14 << 21)) == (0x04 << 21))
12634 insn |= 0x02 << 21;
12635 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12636 insn |= 0x08 << 21;
12637 else
12638 break;
12639 }
12640 else
12641 {
12642 /* Invert 'y' bit if not the default. */
12643 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12644 insn ^= 0x01 << 21;
12645 }
12646
12647 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12648 }
12649
12650 /* NOP out calls to undefined weak functions.
12651 We can thus call a weak function without first
12652 checking whether the function is defined. */
12653 else if (h != NULL
12654 && h->elf.root.type == bfd_link_hash_undefweak
12655 && h->elf.dynindx == -1
12656 && r_type == R_PPC64_REL24
12657 && relocation == 0
12658 && addend == 0)
12659 {
12660 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12661 continue;
12662 }
12663 break;
12664 }
12665
12666 /* Set `addend'. */
12667 tls_type = 0;
12668 switch (r_type)
12669 {
12670 default:
12671 info->callbacks->einfo
12672 (_("%P: %B: unknown relocation type %d for symbol %s\n"),
12673 input_bfd, (int) r_type, sym_name);
12674
12675 bfd_set_error (bfd_error_bad_value);
12676 ret = FALSE;
12677 continue;
12678
12679 case R_PPC64_NONE:
12680 case R_PPC64_TLS:
12681 case R_PPC64_TLSGD:
12682 case R_PPC64_TLSLD:
12683 case R_PPC64_TOCSAVE:
12684 case R_PPC64_GNU_VTINHERIT:
12685 case R_PPC64_GNU_VTENTRY:
12686 continue;
12687
12688 /* GOT16 relocations. Like an ADDR16 using the symbol's
12689 address in the GOT as relocation value instead of the
12690 symbol's value itself. Also, create a GOT entry for the
12691 symbol and put the symbol value there. */
12692 case R_PPC64_GOT_TLSGD16:
12693 case R_PPC64_GOT_TLSGD16_LO:
12694 case R_PPC64_GOT_TLSGD16_HI:
12695 case R_PPC64_GOT_TLSGD16_HA:
12696 tls_type = TLS_TLS | TLS_GD;
12697 goto dogot;
12698
12699 case R_PPC64_GOT_TLSLD16:
12700 case R_PPC64_GOT_TLSLD16_LO:
12701 case R_PPC64_GOT_TLSLD16_HI:
12702 case R_PPC64_GOT_TLSLD16_HA:
12703 tls_type = TLS_TLS | TLS_LD;
12704 goto dogot;
12705
12706 case R_PPC64_GOT_TPREL16_DS:
12707 case R_PPC64_GOT_TPREL16_LO_DS:
12708 case R_PPC64_GOT_TPREL16_HI:
12709 case R_PPC64_GOT_TPREL16_HA:
12710 tls_type = TLS_TLS | TLS_TPREL;
12711 goto dogot;
12712
12713 case R_PPC64_GOT_DTPREL16_DS:
12714 case R_PPC64_GOT_DTPREL16_LO_DS:
12715 case R_PPC64_GOT_DTPREL16_HI:
12716 case R_PPC64_GOT_DTPREL16_HA:
12717 tls_type = TLS_TLS | TLS_DTPREL;
12718 goto dogot;
12719
12720 case R_PPC64_GOT16:
12721 case R_PPC64_GOT16_LO:
12722 case R_PPC64_GOT16_HI:
12723 case R_PPC64_GOT16_HA:
12724 case R_PPC64_GOT16_DS:
12725 case R_PPC64_GOT16_LO_DS:
12726 dogot:
12727 {
12728 /* Relocation is to the entry for this symbol in the global
12729 offset table. */
12730 asection *got;
12731 bfd_vma *offp;
12732 bfd_vma off;
12733 unsigned long indx = 0;
12734 struct got_entry *ent;
12735
12736 if (tls_type == (TLS_TLS | TLS_LD)
12737 && (h == NULL
12738 || !h->elf.def_dynamic))
12739 ent = ppc64_tlsld_got (input_bfd);
12740 else
12741 {
12742
12743 if (h != NULL)
12744 {
12745 bfd_boolean dyn = htab->elf.dynamic_sections_created;
12746 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12747 &h->elf)
12748 || (info->shared
12749 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
12750 /* This is actually a static link, or it is a
12751 -Bsymbolic link and the symbol is defined
12752 locally, or the symbol was forced to be local
12753 because of a version file. */
12754 ;
12755 else
12756 {
12757 indx = h->elf.dynindx;
12758 unresolved_reloc = FALSE;
12759 }
12760 ent = h->elf.got.glist;
12761 }
12762 else
12763 {
12764 if (local_got_ents == NULL)
12765 abort ();
12766 ent = local_got_ents[r_symndx];
12767 }
12768
12769 for (; ent != NULL; ent = ent->next)
12770 if (ent->addend == orig_addend
12771 && ent->owner == input_bfd
12772 && ent->tls_type == tls_type)
12773 break;
12774 }
12775
12776 if (ent == NULL)
12777 abort ();
12778 if (ent->is_indirect)
12779 ent = ent->got.ent;
12780 offp = &ent->got.offset;
12781 got = ppc64_elf_tdata (ent->owner)->got;
12782 if (got == NULL)
12783 abort ();
12784
12785 /* The offset must always be a multiple of 8. We use the
12786 least significant bit to record whether we have already
12787 processed this entry. */
12788 off = *offp;
12789 if ((off & 1) != 0)
12790 off &= ~1;
12791 else
12792 {
12793 /* Generate relocs for the dynamic linker, except in
12794 the case of TLSLD where we'll use one entry per
12795 module. */
12796 asection *relgot;
12797 bfd_boolean ifunc;
12798
12799 *offp = off | 1;
12800 relgot = NULL;
12801 ifunc = (h != NULL
12802 ? h->elf.type == STT_GNU_IFUNC
12803 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12804 if ((info->shared || indx != 0)
12805 && (h == NULL
12806 || (tls_type == (TLS_TLS | TLS_LD)
12807 && !h->elf.def_dynamic)
12808 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12809 || h->elf.root.type != bfd_link_hash_undefweak))
12810 relgot = ppc64_elf_tdata (ent->owner)->relgot;
12811 else if (ifunc)
12812 relgot = htab->reliplt;
12813 if (relgot != NULL)
12814 {
12815 outrel.r_offset = (got->output_section->vma
12816 + got->output_offset
12817 + off);
12818 outrel.r_addend = addend;
12819 if (tls_type & (TLS_LD | TLS_GD))
12820 {
12821 outrel.r_addend = 0;
12822 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12823 if (tls_type == (TLS_TLS | TLS_GD))
12824 {
12825 loc = relgot->contents;
12826 loc += (relgot->reloc_count++
12827 * sizeof (Elf64_External_Rela));
12828 bfd_elf64_swap_reloca_out (output_bfd,
12829 &outrel, loc);
12830 outrel.r_offset += 8;
12831 outrel.r_addend = addend;
12832 outrel.r_info
12833 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12834 }
12835 }
12836 else if (tls_type == (TLS_TLS | TLS_DTPREL))
12837 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12838 else if (tls_type == (TLS_TLS | TLS_TPREL))
12839 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12840 else if (indx != 0)
12841 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12842 else
12843 {
12844 if (ifunc)
12845 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12846 else
12847 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12848
12849 /* Write the .got section contents for the sake
12850 of prelink. */
12851 loc = got->contents + off;
12852 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12853 loc);
12854 }
12855
12856 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12857 {
12858 outrel.r_addend += relocation;
12859 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12860 outrel.r_addend -= htab->elf.tls_sec->vma;
12861 }
12862 loc = relgot->contents;
12863 loc += (relgot->reloc_count++
12864 * sizeof (Elf64_External_Rela));
12865 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12866 }
12867
12868 /* Init the .got section contents here if we're not
12869 emitting a reloc. */
12870 else
12871 {
12872 relocation += addend;
12873 if (tls_type == (TLS_TLS | TLS_LD))
12874 relocation = 1;
12875 else if (tls_type != 0)
12876 {
12877 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12878 if (tls_type == (TLS_TLS | TLS_TPREL))
12879 relocation += DTP_OFFSET - TP_OFFSET;
12880
12881 if (tls_type == (TLS_TLS | TLS_GD))
12882 {
12883 bfd_put_64 (output_bfd, relocation,
12884 got->contents + off + 8);
12885 relocation = 1;
12886 }
12887 }
12888
12889 bfd_put_64 (output_bfd, relocation,
12890 got->contents + off);
12891 }
12892 }
12893
12894 if (off >= (bfd_vma) -2)
12895 abort ();
12896
12897 relocation = got->output_section->vma + got->output_offset + off;
12898 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12899 }
12900 break;
12901
12902 case R_PPC64_PLT16_HA:
12903 case R_PPC64_PLT16_HI:
12904 case R_PPC64_PLT16_LO:
12905 case R_PPC64_PLT32:
12906 case R_PPC64_PLT64:
12907 /* Relocation is to the entry for this symbol in the
12908 procedure linkage table. */
12909
12910 /* Resolve a PLT reloc against a local symbol directly,
12911 without using the procedure linkage table. */
12912 if (h == NULL)
12913 break;
12914
12915 /* It's possible that we didn't make a PLT entry for this
12916 symbol. This happens when statically linking PIC code,
12917 or when using -Bsymbolic. Go find a match if there is a
12918 PLT entry. */
12919 if (htab->plt != NULL)
12920 {
12921 struct plt_entry *ent;
12922 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12923 if (ent->addend == orig_addend
12924 && ent->plt.offset != (bfd_vma) -1)
12925 {
12926 relocation = (htab->plt->output_section->vma
12927 + htab->plt->output_offset
12928 + ent->plt.offset);
12929 unresolved_reloc = FALSE;
12930 }
12931 }
12932 break;
12933
12934 case R_PPC64_TOC:
12935 /* Relocation value is TOC base. */
12936 relocation = TOCstart;
12937 if (r_symndx == STN_UNDEF)
12938 relocation += htab->stub_group[input_section->id].toc_off;
12939 else if (unresolved_reloc)
12940 ;
12941 else if (sec != NULL && sec->id <= htab->top_id)
12942 relocation += htab->stub_group[sec->id].toc_off;
12943 else
12944 unresolved_reloc = TRUE;
12945 goto dodyn;
12946
12947 /* TOC16 relocs. We want the offset relative to the TOC base,
12948 which is the address of the start of the TOC plus 0x8000.
12949 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12950 in this order. */
12951 case R_PPC64_TOC16:
12952 case R_PPC64_TOC16_LO:
12953 case R_PPC64_TOC16_HI:
12954 case R_PPC64_TOC16_DS:
12955 case R_PPC64_TOC16_LO_DS:
12956 case R_PPC64_TOC16_HA:
12957 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12958 break;
12959
12960 /* Relocate against the beginning of the section. */
12961 case R_PPC64_SECTOFF:
12962 case R_PPC64_SECTOFF_LO:
12963 case R_PPC64_SECTOFF_HI:
12964 case R_PPC64_SECTOFF_DS:
12965 case R_PPC64_SECTOFF_LO_DS:
12966 case R_PPC64_SECTOFF_HA:
12967 if (sec != NULL)
12968 addend -= sec->output_section->vma;
12969 break;
12970
12971 case R_PPC64_REL16:
12972 case R_PPC64_REL16_LO:
12973 case R_PPC64_REL16_HI:
12974 case R_PPC64_REL16_HA:
12975 break;
12976
12977 case R_PPC64_REL14:
12978 case R_PPC64_REL14_BRNTAKEN:
12979 case R_PPC64_REL14_BRTAKEN:
12980 case R_PPC64_REL24:
12981 break;
12982
12983 case R_PPC64_TPREL16:
12984 case R_PPC64_TPREL16_LO:
12985 case R_PPC64_TPREL16_HI:
12986 case R_PPC64_TPREL16_HA:
12987 case R_PPC64_TPREL16_DS:
12988 case R_PPC64_TPREL16_LO_DS:
12989 case R_PPC64_TPREL16_HIGHER:
12990 case R_PPC64_TPREL16_HIGHERA:
12991 case R_PPC64_TPREL16_HIGHEST:
12992 case R_PPC64_TPREL16_HIGHESTA:
12993 if (h != NULL
12994 && h->elf.root.type == bfd_link_hash_undefweak
12995 && h->elf.dynindx == -1)
12996 {
12997 /* Make this relocation against an undefined weak symbol
12998 resolve to zero. This is really just a tweak, since
12999 code using weak externs ought to check that they are
13000 defined before using them. */
13001 bfd_byte *p = contents + rel->r_offset - d_offset;
13002
13003 insn = bfd_get_32 (output_bfd, p);
13004 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13005 if (insn != 0)
13006 bfd_put_32 (output_bfd, insn, p);
13007 break;
13008 }
13009 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13010 if (info->shared)
13011 /* The TPREL16 relocs shouldn't really be used in shared
13012 libs as they will result in DT_TEXTREL being set, but
13013 support them anyway. */
13014 goto dodyn;
13015 break;
13016
13017 case R_PPC64_DTPREL16:
13018 case R_PPC64_DTPREL16_LO:
13019 case R_PPC64_DTPREL16_HI:
13020 case R_PPC64_DTPREL16_HA:
13021 case R_PPC64_DTPREL16_DS:
13022 case R_PPC64_DTPREL16_LO_DS:
13023 case R_PPC64_DTPREL16_HIGHER:
13024 case R_PPC64_DTPREL16_HIGHERA:
13025 case R_PPC64_DTPREL16_HIGHEST:
13026 case R_PPC64_DTPREL16_HIGHESTA:
13027 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13028 break;
13029
13030 case R_PPC64_DTPMOD64:
13031 relocation = 1;
13032 addend = 0;
13033 goto dodyn;
13034
13035 case R_PPC64_TPREL64:
13036 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13037 goto dodyn;
13038
13039 case R_PPC64_DTPREL64:
13040 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13041 /* Fall thru */
13042
13043 /* Relocations that may need to be propagated if this is a
13044 dynamic object. */
13045 case R_PPC64_REL30:
13046 case R_PPC64_REL32:
13047 case R_PPC64_REL64:
13048 case R_PPC64_ADDR14:
13049 case R_PPC64_ADDR14_BRNTAKEN:
13050 case R_PPC64_ADDR14_BRTAKEN:
13051 case R_PPC64_ADDR16:
13052 case R_PPC64_ADDR16_DS:
13053 case R_PPC64_ADDR16_HA:
13054 case R_PPC64_ADDR16_HI:
13055 case R_PPC64_ADDR16_HIGHER:
13056 case R_PPC64_ADDR16_HIGHERA:
13057 case R_PPC64_ADDR16_HIGHEST:
13058 case R_PPC64_ADDR16_HIGHESTA:
13059 case R_PPC64_ADDR16_LO:
13060 case R_PPC64_ADDR16_LO_DS:
13061 case R_PPC64_ADDR24:
13062 case R_PPC64_ADDR32:
13063 case R_PPC64_ADDR64:
13064 case R_PPC64_UADDR16:
13065 case R_PPC64_UADDR32:
13066 case R_PPC64_UADDR64:
13067 dodyn:
13068 if ((input_section->flags & SEC_ALLOC) == 0)
13069 break;
13070
13071 if (NO_OPD_RELOCS && is_opd)
13072 break;
13073
13074 if ((info->shared
13075 && (h == NULL
13076 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13077 || h->elf.root.type != bfd_link_hash_undefweak)
13078 && (must_be_dyn_reloc (info, r_type)
13079 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13080 || (ELIMINATE_COPY_RELOCS
13081 && !info->shared
13082 && h != NULL
13083 && h->elf.dynindx != -1
13084 && !h->elf.non_got_ref
13085 && !h->elf.def_regular)
13086 || (!info->shared
13087 && (h != NULL
13088 ? h->elf.type == STT_GNU_IFUNC
13089 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
13090 {
13091 bfd_boolean skip, relocate;
13092 asection *sreloc;
13093 bfd_vma out_off;
13094
13095 /* When generating a dynamic object, these relocations
13096 are copied into the output file to be resolved at run
13097 time. */
13098
13099 skip = FALSE;
13100 relocate = FALSE;
13101
13102 out_off = _bfd_elf_section_offset (output_bfd, info,
13103 input_section, rel->r_offset);
13104 if (out_off == (bfd_vma) -1)
13105 skip = TRUE;
13106 else if (out_off == (bfd_vma) -2)
13107 skip = TRUE, relocate = TRUE;
13108 out_off += (input_section->output_section->vma
13109 + input_section->output_offset);
13110 outrel.r_offset = out_off;
13111 outrel.r_addend = rel->r_addend;
13112
13113 /* Optimize unaligned reloc use. */
13114 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13115 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13116 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13117 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13118 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13119 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13120 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13121 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13122 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13123
13124 if (skip)
13125 memset (&outrel, 0, sizeof outrel);
13126 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13127 && !is_opd
13128 && r_type != R_PPC64_TOC)
13129 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13130 else
13131 {
13132 /* This symbol is local, or marked to become local,
13133 or this is an opd section reloc which must point
13134 at a local function. */
13135 outrel.r_addend += relocation;
13136 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13137 {
13138 if (is_opd && h != NULL)
13139 {
13140 /* Lie about opd entries. This case occurs
13141 when building shared libraries and we
13142 reference a function in another shared
13143 lib. The same thing happens for a weak
13144 definition in an application that's
13145 overridden by a strong definition in a
13146 shared lib. (I believe this is a generic
13147 bug in binutils handling of weak syms.)
13148 In these cases we won't use the opd
13149 entry in this lib. */
13150 unresolved_reloc = FALSE;
13151 }
13152 if (!is_opd
13153 && r_type == R_PPC64_ADDR64
13154 && (h != NULL
13155 ? h->elf.type == STT_GNU_IFUNC
13156 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13157 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13158 else
13159 {
13160 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13161
13162 /* We need to relocate .opd contents for ld.so.
13163 Prelink also wants simple and consistent rules
13164 for relocs. This make all RELATIVE relocs have
13165 *r_offset equal to r_addend. */
13166 relocate = TRUE;
13167 }
13168 }
13169 else
13170 {
13171 long indx = 0;
13172
13173 if (h != NULL
13174 ? h->elf.type == STT_GNU_IFUNC
13175 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13176 {
13177 info->callbacks->einfo
13178 (_("%P: %H: relocation %s for indirect "
13179 "function %s unsupported\n"),
13180 input_bfd, input_section, rel->r_offset,
13181 ppc64_elf_howto_table[r_type]->name,
13182 sym_name);
13183 ret = FALSE;
13184 }
13185 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
13186 ;
13187 else if (sec == NULL || sec->owner == NULL)
13188 {
13189 bfd_set_error (bfd_error_bad_value);
13190 return FALSE;
13191 }
13192 else
13193 {
13194 asection *osec;
13195
13196 osec = sec->output_section;
13197 indx = elf_section_data (osec)->dynindx;
13198
13199 if (indx == 0)
13200 {
13201 if ((osec->flags & SEC_READONLY) == 0
13202 && htab->elf.data_index_section != NULL)
13203 osec = htab->elf.data_index_section;
13204 else
13205 osec = htab->elf.text_index_section;
13206 indx = elf_section_data (osec)->dynindx;
13207 }
13208 BFD_ASSERT (indx != 0);
13209
13210 /* We are turning this relocation into one
13211 against a section symbol, so subtract out
13212 the output section's address but not the
13213 offset of the input section in the output
13214 section. */
13215 outrel.r_addend -= osec->vma;
13216 }
13217
13218 outrel.r_info = ELF64_R_INFO (indx, r_type);
13219 }
13220 }
13221
13222 sreloc = elf_section_data (input_section)->sreloc;
13223 if (!htab->elf.dynamic_sections_created)
13224 sreloc = htab->reliplt;
13225 if (sreloc == NULL)
13226 abort ();
13227
13228 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13229 >= sreloc->size)
13230 abort ();
13231 loc = sreloc->contents;
13232 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
13233 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13234
13235 /* If this reloc is against an external symbol, it will
13236 be computed at runtime, so there's no need to do
13237 anything now. However, for the sake of prelink ensure
13238 that the section contents are a known value. */
13239 if (! relocate)
13240 {
13241 unresolved_reloc = FALSE;
13242 /* The value chosen here is quite arbitrary as ld.so
13243 ignores section contents except for the special
13244 case of .opd where the contents might be accessed
13245 before relocation. Choose zero, as that won't
13246 cause reloc overflow. */
13247 relocation = 0;
13248 addend = 0;
13249 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13250 to improve backward compatibility with older
13251 versions of ld. */
13252 if (r_type == R_PPC64_ADDR64)
13253 addend = outrel.r_addend;
13254 /* Adjust pc_relative relocs to have zero in *r_offset. */
13255 else if (ppc64_elf_howto_table[r_type]->pc_relative)
13256 addend = (input_section->output_section->vma
13257 + input_section->output_offset
13258 + rel->r_offset);
13259 }
13260 }
13261 break;
13262
13263 case R_PPC64_COPY:
13264 case R_PPC64_GLOB_DAT:
13265 case R_PPC64_JMP_SLOT:
13266 case R_PPC64_JMP_IREL:
13267 case R_PPC64_RELATIVE:
13268 /* We shouldn't ever see these dynamic relocs in relocatable
13269 files. */
13270 /* Fall through. */
13271
13272 case R_PPC64_PLTGOT16:
13273 case R_PPC64_PLTGOT16_DS:
13274 case R_PPC64_PLTGOT16_HA:
13275 case R_PPC64_PLTGOT16_HI:
13276 case R_PPC64_PLTGOT16_LO:
13277 case R_PPC64_PLTGOT16_LO_DS:
13278 case R_PPC64_PLTREL32:
13279 case R_PPC64_PLTREL64:
13280 /* These ones haven't been implemented yet. */
13281
13282 info->callbacks->einfo
13283 (_("%P: %B: relocation %s is not supported for symbol %s\n"),
13284 input_bfd,
13285 ppc64_elf_howto_table[r_type]->name, sym_name);
13286
13287 bfd_set_error (bfd_error_invalid_operation);
13288 ret = FALSE;
13289 continue;
13290 }
13291
13292 /* Multi-instruction sequences that access the TOC can be
13293 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13294 to nop; addi rb,r2,x; */
13295 switch (r_type)
13296 {
13297 default:
13298 break;
13299
13300 case R_PPC64_GOT_TLSLD16_HI:
13301 case R_PPC64_GOT_TLSGD16_HI:
13302 case R_PPC64_GOT_TPREL16_HI:
13303 case R_PPC64_GOT_DTPREL16_HI:
13304 case R_PPC64_GOT16_HI:
13305 case R_PPC64_TOC16_HI:
13306 /* These relocs would only be useful if building up an
13307 offset to later add to r2, perhaps in an indexed
13308 addressing mode instruction. Don't try to optimize.
13309 Unfortunately, the possibility of someone building up an
13310 offset like this or even with the HA relocs, means that
13311 we need to check the high insn when optimizing the low
13312 insn. */
13313 break;
13314
13315 case R_PPC64_GOT_TLSLD16_HA:
13316 case R_PPC64_GOT_TLSGD16_HA:
13317 case R_PPC64_GOT_TPREL16_HA:
13318 case R_PPC64_GOT_DTPREL16_HA:
13319 case R_PPC64_GOT16_HA:
13320 case R_PPC64_TOC16_HA:
13321 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13322 && !ppc64_elf_tdata (input_bfd)->ha_relocs_not_using_r2)
13323 {
13324 bfd_byte *p = contents + (rel->r_offset & ~3);
13325 bfd_put_32 (input_bfd, NOP, p);
13326 }
13327 break;
13328
13329 case R_PPC64_GOT_TLSLD16_LO:
13330 case R_PPC64_GOT_TLSGD16_LO:
13331 case R_PPC64_GOT_TPREL16_LO_DS:
13332 case R_PPC64_GOT_DTPREL16_LO_DS:
13333 case R_PPC64_GOT16_LO:
13334 case R_PPC64_GOT16_LO_DS:
13335 case R_PPC64_TOC16_LO:
13336 case R_PPC64_TOC16_LO_DS:
13337 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13338 && !ppc64_elf_tdata (input_bfd)->ha_relocs_not_using_r2)
13339 {
13340 bfd_byte *p = contents + (rel->r_offset & ~3);
13341 insn = bfd_get_32 (input_bfd, p);
13342 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13343 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13344 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13345 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13346 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13347 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13348 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13349 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13350 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13351 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13352 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13353 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13354 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13355 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13356 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13357 && (insn & 3) != 1)
13358 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13359 && ((insn & 3) == 0 || (insn & 3) == 3)))
13360 {
13361 insn &= ~(0x1f << 16);
13362 insn |= 2 << 16;
13363 bfd_put_32 (input_bfd, insn, p);
13364 }
13365 }
13366 break;
13367 }
13368
13369 /* Do any further special processing. */
13370 switch (r_type)
13371 {
13372 default:
13373 break;
13374
13375 case R_PPC64_ADDR16_HA:
13376 case R_PPC64_REL16_HA:
13377 case R_PPC64_ADDR16_HIGHERA:
13378 case R_PPC64_ADDR16_HIGHESTA:
13379 case R_PPC64_TOC16_HA:
13380 case R_PPC64_SECTOFF_HA:
13381 case R_PPC64_TPREL16_HA:
13382 case R_PPC64_DTPREL16_HA:
13383 case R_PPC64_TPREL16_HIGHER:
13384 case R_PPC64_TPREL16_HIGHERA:
13385 case R_PPC64_TPREL16_HIGHEST:
13386 case R_PPC64_TPREL16_HIGHESTA:
13387 case R_PPC64_DTPREL16_HIGHER:
13388 case R_PPC64_DTPREL16_HIGHERA:
13389 case R_PPC64_DTPREL16_HIGHEST:
13390 case R_PPC64_DTPREL16_HIGHESTA:
13391 /* It's just possible that this symbol is a weak symbol
13392 that's not actually defined anywhere. In that case,
13393 'sec' would be NULL, and we should leave the symbol
13394 alone (it will be set to zero elsewhere in the link). */
13395 if (sec == NULL)
13396 break;
13397 /* Fall thru */
13398
13399 case R_PPC64_GOT16_HA:
13400 case R_PPC64_PLTGOT16_HA:
13401 case R_PPC64_PLT16_HA:
13402 case R_PPC64_GOT_TLSGD16_HA:
13403 case R_PPC64_GOT_TLSLD16_HA:
13404 case R_PPC64_GOT_TPREL16_HA:
13405 case R_PPC64_GOT_DTPREL16_HA:
13406 /* Add 0x10000 if sign bit in 0:15 is set.
13407 Bits 0:15 are not used. */
13408 addend += 0x8000;
13409 break;
13410
13411 case R_PPC64_ADDR16_DS:
13412 case R_PPC64_ADDR16_LO_DS:
13413 case R_PPC64_GOT16_DS:
13414 case R_PPC64_GOT16_LO_DS:
13415 case R_PPC64_PLT16_LO_DS:
13416 case R_PPC64_SECTOFF_DS:
13417 case R_PPC64_SECTOFF_LO_DS:
13418 case R_PPC64_TOC16_DS:
13419 case R_PPC64_TOC16_LO_DS:
13420 case R_PPC64_PLTGOT16_DS:
13421 case R_PPC64_PLTGOT16_LO_DS:
13422 case R_PPC64_GOT_TPREL16_DS:
13423 case R_PPC64_GOT_TPREL16_LO_DS:
13424 case R_PPC64_GOT_DTPREL16_DS:
13425 case R_PPC64_GOT_DTPREL16_LO_DS:
13426 case R_PPC64_TPREL16_DS:
13427 case R_PPC64_TPREL16_LO_DS:
13428 case R_PPC64_DTPREL16_DS:
13429 case R_PPC64_DTPREL16_LO_DS:
13430 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13431 mask = 3;
13432 /* If this reloc is against an lq insn, then the value must be
13433 a multiple of 16. This is somewhat of a hack, but the
13434 "correct" way to do this by defining _DQ forms of all the
13435 _DS relocs bloats all reloc switches in this file. It
13436 doesn't seem to make much sense to use any of these relocs
13437 in data, so testing the insn should be safe. */
13438 if ((insn & (0x3f << 26)) == (56u << 26))
13439 mask = 15;
13440 if (((relocation + addend) & mask) != 0)
13441 {
13442 info->callbacks->einfo
13443 (_("%P: %H: error: %s not a multiple of %u\n"),
13444 input_bfd, input_section, rel->r_offset,
13445 ppc64_elf_howto_table[r_type]->name,
13446 mask + 1);
13447 bfd_set_error (bfd_error_bad_value);
13448 ret = FALSE;
13449 continue;
13450 }
13451 break;
13452 }
13453
13454 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13455 because such sections are not SEC_ALLOC and thus ld.so will
13456 not process them. */
13457 if (unresolved_reloc
13458 && !((input_section->flags & SEC_DEBUGGING) != 0
13459 && h->elf.def_dynamic))
13460 {
13461 info->callbacks->einfo
13462 (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
13463 input_bfd, input_section, rel->r_offset,
13464 ppc64_elf_howto_table[(int) r_type]->name,
13465 h->elf.root.root.string);
13466 ret = FALSE;
13467 }
13468
13469 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13470 input_bfd,
13471 input_section,
13472 contents,
13473 rel->r_offset,
13474 relocation,
13475 addend);
13476
13477 if (r != bfd_reloc_ok)
13478 {
13479 if (sym_name == NULL)
13480 sym_name = "(null)";
13481 if (r == bfd_reloc_overflow)
13482 {
13483 if (warned)
13484 continue;
13485 if (h != NULL
13486 && h->elf.root.type == bfd_link_hash_undefweak
13487 && ppc64_elf_howto_table[r_type]->pc_relative)
13488 {
13489 /* Assume this is a call protected by other code that
13490 detects the symbol is undefined. If this is the case,
13491 we can safely ignore the overflow. If not, the
13492 program is hosed anyway, and a little warning isn't
13493 going to help. */
13494
13495 continue;
13496 }
13497
13498 if (!((*info->callbacks->reloc_overflow)
13499 (info, (h ? &h->elf.root : NULL), sym_name,
13500 ppc64_elf_howto_table[r_type]->name,
13501 orig_addend, input_bfd, input_section, rel->r_offset)))
13502 return FALSE;
13503 }
13504 else
13505 {
13506 info->callbacks->einfo
13507 (_("%P: %H: %s reloc against `%s': error %d\n"),
13508 input_bfd, input_section, rel->r_offset,
13509 ppc64_elf_howto_table[r_type]->name,
13510 sym_name,
13511 (int) r);
13512 ret = FALSE;
13513 }
13514 }
13515 }
13516
13517 /* If we're emitting relocations, then shortly after this function
13518 returns, reloc offsets and addends for this section will be
13519 adjusted. Worse, reloc symbol indices will be for the output
13520 file rather than the input. Save a copy of the relocs for
13521 opd_entry_value. */
13522 if (is_opd && (info->emitrelocations || info->relocatable))
13523 {
13524 bfd_size_type amt;
13525 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13526 rel = bfd_alloc (input_bfd, amt);
13527 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13528 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13529 if (rel == NULL)
13530 return FALSE;
13531 memcpy (rel, relocs, amt);
13532 }
13533 return ret;
13534 }
13535
13536 /* Adjust the value of any local symbols in opd sections. */
13537
13538 static int
13539 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13540 const char *name ATTRIBUTE_UNUSED,
13541 Elf_Internal_Sym *elfsym,
13542 asection *input_sec,
13543 struct elf_link_hash_entry *h)
13544 {
13545 struct _opd_sec_data *opd;
13546 long adjust;
13547 bfd_vma value;
13548
13549 if (h != NULL)
13550 return 1;
13551
13552 opd = get_opd_info (input_sec);
13553 if (opd == NULL || opd->adjust == NULL)
13554 return 1;
13555
13556 value = elfsym->st_value - input_sec->output_offset;
13557 if (!info->relocatable)
13558 value -= input_sec->output_section->vma;
13559
13560 adjust = opd->adjust[value / 8];
13561 if (adjust == -1)
13562 return 2;
13563
13564 elfsym->st_value += adjust;
13565 return 1;
13566 }
13567
13568 /* Finish up dynamic symbol handling. We set the contents of various
13569 dynamic sections here. */
13570
13571 static bfd_boolean
13572 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13573 struct bfd_link_info *info,
13574 struct elf_link_hash_entry *h,
13575 Elf_Internal_Sym *sym)
13576 {
13577 struct ppc_link_hash_table *htab;
13578 struct plt_entry *ent;
13579 Elf_Internal_Rela rela;
13580 bfd_byte *loc;
13581
13582 htab = ppc_hash_table (info);
13583 if (htab == NULL)
13584 return FALSE;
13585
13586 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13587 if (ent->plt.offset != (bfd_vma) -1)
13588 {
13589 /* This symbol has an entry in the procedure linkage
13590 table. Set it up. */
13591 if (!htab->elf.dynamic_sections_created
13592 || h->dynindx == -1)
13593 {
13594 BFD_ASSERT (h->type == STT_GNU_IFUNC
13595 && h->def_regular
13596 && (h->root.type == bfd_link_hash_defined
13597 || h->root.type == bfd_link_hash_defweak));
13598 rela.r_offset = (htab->iplt->output_section->vma
13599 + htab->iplt->output_offset
13600 + ent->plt.offset);
13601 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13602 rela.r_addend = (h->root.u.def.value
13603 + h->root.u.def.section->output_offset
13604 + h->root.u.def.section->output_section->vma
13605 + ent->addend);
13606 loc = (htab->reliplt->contents
13607 + (htab->reliplt->reloc_count++
13608 * sizeof (Elf64_External_Rela)));
13609 }
13610 else
13611 {
13612 rela.r_offset = (htab->plt->output_section->vma
13613 + htab->plt->output_offset
13614 + ent->plt.offset);
13615 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13616 rela.r_addend = ent->addend;
13617 loc = (htab->relplt->contents
13618 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13619 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13620 }
13621 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13622 }
13623
13624 if (h->needs_copy)
13625 {
13626 /* This symbol needs a copy reloc. Set it up. */
13627
13628 if (h->dynindx == -1
13629 || (h->root.type != bfd_link_hash_defined
13630 && h->root.type != bfd_link_hash_defweak)
13631 || htab->relbss == NULL)
13632 abort ();
13633
13634 rela.r_offset = (h->root.u.def.value
13635 + h->root.u.def.section->output_section->vma
13636 + h->root.u.def.section->output_offset);
13637 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13638 rela.r_addend = 0;
13639 loc = htab->relbss->contents;
13640 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13641 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13642 }
13643
13644 /* Mark some specially defined symbols as absolute. */
13645 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13646 sym->st_shndx = SHN_ABS;
13647
13648 return TRUE;
13649 }
13650
13651 /* Used to decide how to sort relocs in an optimal manner for the
13652 dynamic linker, before writing them out. */
13653
13654 static enum elf_reloc_type_class
13655 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13656 {
13657 enum elf_ppc64_reloc_type r_type;
13658
13659 r_type = ELF64_R_TYPE (rela->r_info);
13660 switch (r_type)
13661 {
13662 case R_PPC64_RELATIVE:
13663 return reloc_class_relative;
13664 case R_PPC64_JMP_SLOT:
13665 return reloc_class_plt;
13666 case R_PPC64_COPY:
13667 return reloc_class_copy;
13668 default:
13669 return reloc_class_normal;
13670 }
13671 }
13672
13673 /* Finish up the dynamic sections. */
13674
13675 static bfd_boolean
13676 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13677 struct bfd_link_info *info)
13678 {
13679 struct ppc_link_hash_table *htab;
13680 bfd *dynobj;
13681 asection *sdyn;
13682
13683 htab = ppc_hash_table (info);
13684 if (htab == NULL)
13685 return FALSE;
13686
13687 dynobj = htab->elf.dynobj;
13688 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13689
13690 if (htab->elf.dynamic_sections_created)
13691 {
13692 Elf64_External_Dyn *dyncon, *dynconend;
13693
13694 if (sdyn == NULL || htab->got == NULL)
13695 abort ();
13696
13697 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13698 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13699 for (; dyncon < dynconend; dyncon++)
13700 {
13701 Elf_Internal_Dyn dyn;
13702 asection *s;
13703
13704 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13705
13706 switch (dyn.d_tag)
13707 {
13708 default:
13709 continue;
13710
13711 case DT_PPC64_GLINK:
13712 s = htab->glink;
13713 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13714 /* We stupidly defined DT_PPC64_GLINK to be the start
13715 of glink rather than the first entry point, which is
13716 what ld.so needs, and now have a bigger stub to
13717 support automatic multiple TOCs. */
13718 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13719 break;
13720
13721 case DT_PPC64_OPD:
13722 s = bfd_get_section_by_name (output_bfd, ".opd");
13723 if (s == NULL)
13724 continue;
13725 dyn.d_un.d_ptr = s->vma;
13726 break;
13727
13728 case DT_PPC64_OPDSZ:
13729 s = bfd_get_section_by_name (output_bfd, ".opd");
13730 if (s == NULL)
13731 continue;
13732 dyn.d_un.d_val = s->size;
13733 break;
13734
13735 case DT_PLTGOT:
13736 s = htab->plt;
13737 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13738 break;
13739
13740 case DT_JMPREL:
13741 s = htab->relplt;
13742 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13743 break;
13744
13745 case DT_PLTRELSZ:
13746 dyn.d_un.d_val = htab->relplt->size;
13747 break;
13748
13749 case DT_RELASZ:
13750 /* Don't count procedure linkage table relocs in the
13751 overall reloc count. */
13752 s = htab->relplt;
13753 if (s == NULL)
13754 continue;
13755 dyn.d_un.d_val -= s->size;
13756 break;
13757
13758 case DT_RELA:
13759 /* We may not be using the standard ELF linker script.
13760 If .rela.plt is the first .rela section, we adjust
13761 DT_RELA to not include it. */
13762 s = htab->relplt;
13763 if (s == NULL)
13764 continue;
13765 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13766 continue;
13767 dyn.d_un.d_ptr += s->size;
13768 break;
13769 }
13770
13771 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13772 }
13773 }
13774
13775 if (htab->got != NULL && htab->got->size != 0)
13776 {
13777 /* Fill in the first entry in the global offset table.
13778 We use it to hold the link-time TOCbase. */
13779 bfd_put_64 (output_bfd,
13780 elf_gp (output_bfd) + TOC_BASE_OFF,
13781 htab->got->contents);
13782
13783 /* Set .got entry size. */
13784 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13785 }
13786
13787 if (htab->plt != NULL && htab->plt->size != 0)
13788 {
13789 /* Set .plt entry size. */
13790 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13791 = PLT_ENTRY_SIZE;
13792 }
13793
13794 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13795 brlt ourselves if emitrelocations. */
13796 if (htab->brlt != NULL
13797 && htab->brlt->reloc_count != 0
13798 && !_bfd_elf_link_output_relocs (output_bfd,
13799 htab->brlt,
13800 elf_section_data (htab->brlt)->rela.hdr,
13801 elf_section_data (htab->brlt)->relocs,
13802 NULL))
13803 return FALSE;
13804
13805 if (htab->glink != NULL
13806 && htab->glink->reloc_count != 0
13807 && !_bfd_elf_link_output_relocs (output_bfd,
13808 htab->glink,
13809 elf_section_data (htab->glink)->rela.hdr,
13810 elf_section_data (htab->glink)->relocs,
13811 NULL))
13812 return FALSE;
13813
13814
13815 if (htab->glink_eh_frame != NULL
13816 && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
13817 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
13818 htab->glink_eh_frame,
13819 htab->glink_eh_frame->contents))
13820 return FALSE;
13821
13822 /* We need to handle writing out multiple GOT sections ourselves,
13823 since we didn't add them to DYNOBJ. We know dynobj is the first
13824 bfd. */
13825 while ((dynobj = dynobj->link_next) != NULL)
13826 {
13827 asection *s;
13828
13829 if (!is_ppc64_elf (dynobj))
13830 continue;
13831
13832 s = ppc64_elf_tdata (dynobj)->got;
13833 if (s != NULL
13834 && s->size != 0
13835 && s->output_section != bfd_abs_section_ptr
13836 && !bfd_set_section_contents (output_bfd, s->output_section,
13837 s->contents, s->output_offset,
13838 s->size))
13839 return FALSE;
13840 s = ppc64_elf_tdata (dynobj)->relgot;
13841 if (s != NULL
13842 && s->size != 0
13843 && s->output_section != bfd_abs_section_ptr
13844 && !bfd_set_section_contents (output_bfd, s->output_section,
13845 s->contents, s->output_offset,
13846 s->size))
13847 return FALSE;
13848 }
13849
13850 return TRUE;
13851 }
13852
13853 #include "elf64-target.h"
This page took 0.371543 seconds and 4 git commands to generate.