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