bfd/
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23
24
25 /* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *);
58
59 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
79
80 #define bfd_elf64_mkobject ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
84 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
85 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
86 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
87 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
88 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
89
90 #define elf_backend_object_p ppc64_elf_object_p
91 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
92 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
93 #define elf_backend_write_core_note ppc64_elf_write_core_note
94 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
95 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
96 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
97 #define elf_backend_check_directives ppc64_elf_process_dot_syms
98 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
99 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
100 #define elf_backend_check_relocs ppc64_elf_check_relocs
101 #define elf_backend_gc_keep ppc64_elf_gc_keep
102 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
103 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
104 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
105 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
106 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
107 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
108 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
109 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
110 #define elf_backend_action_discarded ppc64_elf_action_discarded
111 #define elf_backend_relocate_section ppc64_elf_relocate_section
112 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
113 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
114 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
115 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
116 #define elf_backend_special_sections ppc64_elf_special_sections
117 #define elf_backend_post_process_headers _bfd_elf_set_osabi
118
119 /* The name of the dynamic interpreter. This is put in the .interp
120 section. */
121 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
122
123 /* The size in bytes of an entry in the procedure linkage table. */
124 #define PLT_ENTRY_SIZE 24
125
126 /* The initial size of the plt reserved for the dynamic linker. */
127 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
128
129 /* TOC base pointers offset from start of TOC. */
130 #define TOC_BASE_OFF 0x8000
131
132 /* Offset of tp and dtp pointers from start of TLS block. */
133 #define TP_OFFSET 0x7000
134 #define DTP_OFFSET 0x8000
135
136 /* .plt call stub instructions. The normal stub is like this, but
137 sometimes the .plt entry crosses a 64k boundary and we need to
138 insert an addi to adjust r12. */
139 #define PLT_CALL_STUB_SIZE (7*4)
140 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
141 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
142 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
143 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
144 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
145 /* ld %r11,xxx+16@l(%r12) */
146 #define BCTR 0x4e800420 /* bctr */
147
148
149 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
150 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
151 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
152 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
153
154 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
155 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
156
157 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
158
159 /* glink call stub instructions. We enter with the index in R0. */
160 #define GLINK_CALL_STUB_SIZE (16*4)
161 /* 0: */
162 /* .quad plt0-1f */
163 /* __glink: */
164 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
165 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
166 /* 1: */
167 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
168 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
169 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
170 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
171 /* ld %11,0(%12) */
172 /* ld %2,8(%12) */
173 /* mtctr %11 */
174 /* ld %11,16(%12) */
175 /* bctr */
176
177 /* Pad with this. */
178 #define NOP 0x60000000
179
180 /* Some other nops. */
181 #define CROR_151515 0x4def7b82
182 #define CROR_313131 0x4ffffb82
183
184 /* .glink entries for the first 32k functions are two instructions. */
185 #define LI_R0_0 0x38000000 /* li %r0,0 */
186 #define B_DOT 0x48000000 /* b . */
187
188 /* After that, we need two instructions to load the index, followed by
189 a branch. */
190 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
191 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
192
193 /* Instructions used by the save and restore reg functions. */
194 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
195 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
196 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
197 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
198 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
199 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
200 #define LI_R12_0 0x39800000 /* li %r12,0 */
201 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
202 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
203 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
204 #define BLR 0x4e800020 /* blr */
205
206 /* Since .opd is an array of descriptors and each entry will end up
207 with identical R_PPC64_RELATIVE relocs, there is really no need to
208 propagate .opd relocs; The dynamic linker should be taught to
209 relocate .opd without reloc entries. */
210 #ifndef NO_OPD_RELOCS
211 #define NO_OPD_RELOCS 0
212 #endif
213 \f
214 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
215
216 /* Relocation HOWTO's. */
217 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
218
219 static reloc_howto_type ppc64_elf_howto_raw[] = {
220 /* This reloc does nothing. */
221 HOWTO (R_PPC64_NONE, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 FALSE, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_dont, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_NONE", /* name */
230 FALSE, /* partial_inplace */
231 0, /* src_mask */
232 0, /* dst_mask */
233 FALSE), /* pcrel_offset */
234
235 /* A standard 32 bit relocation. */
236 HOWTO (R_PPC64_ADDR32, /* type */
237 0, /* rightshift */
238 2, /* size (0 = byte, 1 = short, 2 = long) */
239 32, /* bitsize */
240 FALSE, /* pc_relative */
241 0, /* bitpos */
242 complain_overflow_bitfield, /* complain_on_overflow */
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_ADDR32", /* name */
245 FALSE, /* partial_inplace */
246 0, /* src_mask */
247 0xffffffff, /* dst_mask */
248 FALSE), /* pcrel_offset */
249
250 /* An absolute 26 bit branch; the lower two bits must be zero.
251 FIXME: we don't check that, we just clear them. */
252 HOWTO (R_PPC64_ADDR24, /* type */
253 0, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 26, /* bitsize */
256 FALSE, /* pc_relative */
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_PPC64_ADDR24", /* name */
261 FALSE, /* partial_inplace */
262 0, /* src_mask */
263 0x03fffffc, /* dst_mask */
264 FALSE), /* pcrel_offset */
265
266 /* A standard 16 bit relocation. */
267 HOWTO (R_PPC64_ADDR16, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 FALSE, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16", /* name */
276 FALSE, /* partial_inplace */
277 0, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
280
281 /* A 16 bit relocation without overflow. */
282 HOWTO (R_PPC64_ADDR16_LO, /* type */
283 0, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 FALSE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_dont,/* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16_LO", /* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xffff, /* dst_mask */
294 FALSE), /* pcrel_offset */
295
296 /* Bits 16-31 of an address. */
297 HOWTO (R_PPC64_ADDR16_HI, /* type */
298 16, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
301 FALSE, /* pc_relative */
302 0, /* bitpos */
303 complain_overflow_dont, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_ADDR16_HI", /* name */
306 FALSE, /* partial_inplace */
307 0, /* src_mask */
308 0xffff, /* dst_mask */
309 FALSE), /* pcrel_offset */
310
311 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
312 bits, treated as a signed number, is negative. */
313 HOWTO (R_PPC64_ADDR16_HA, /* type */
314 16, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
317 FALSE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
320 ppc64_elf_ha_reloc, /* special_function */
321 "R_PPC64_ADDR16_HA", /* name */
322 FALSE, /* partial_inplace */
323 0, /* src_mask */
324 0xffff, /* dst_mask */
325 FALSE), /* pcrel_offset */
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOWTO (R_PPC64_ADDR14, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
333 FALSE, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
336 ppc64_elf_branch_reloc, /* special_function */
337 "R_PPC64_ADDR14", /* name */
338 FALSE, /* partial_inplace */
339 0, /* src_mask */
340 0x0000fffc, /* dst_mask */
341 FALSE), /* pcrel_offset */
342
343 /* An absolute 16 bit branch, for which bit 10 should be set to
344 indicate that the branch is expected to be taken. The lower two
345 bits must be zero. */
346 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
353 ppc64_elf_brtaken_reloc, /* special_function */
354 "R_PPC64_ADDR14_BRTAKEN",/* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0x0000fffc, /* dst_mask */
358 FALSE), /* pcrel_offset */
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
370 ppc64_elf_brtaken_reloc, /* special_function */
371 "R_PPC64_ADDR14_BRNTAKEN",/* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0x0000fffc, /* dst_mask */
375 FALSE), /* pcrel_offset */
376
377 /* A relative 26 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL24, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 26, /* bitsize */
382 TRUE, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
385 ppc64_elf_branch_reloc, /* special_function */
386 "R_PPC64_REL24", /* name */
387 FALSE, /* partial_inplace */
388 0, /* src_mask */
389 0x03fffffc, /* dst_mask */
390 TRUE), /* pcrel_offset */
391
392 /* A relative 16 bit branch; the lower two bits must be zero. */
393 HOWTO (R_PPC64_REL14, /* type */
394 0, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
397 TRUE, /* pc_relative */
398 0, /* bitpos */
399 complain_overflow_signed, /* complain_on_overflow */
400 ppc64_elf_branch_reloc, /* special_function */
401 "R_PPC64_REL14", /* name */
402 FALSE, /* partial_inplace */
403 0, /* src_mask */
404 0x0000fffc, /* dst_mask */
405 TRUE), /* pcrel_offset */
406
407 /* A relative 16 bit branch. Bit 10 should be set to indicate that
408 the branch is expected to be taken. The lower two bits must be
409 zero. */
410 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
411 0, /* rightshift */
412 2, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
414 TRUE, /* pc_relative */
415 0, /* bitpos */
416 complain_overflow_signed, /* complain_on_overflow */
417 ppc64_elf_brtaken_reloc, /* special_function */
418 "R_PPC64_REL14_BRTAKEN", /* name */
419 FALSE, /* partial_inplace */
420 0, /* src_mask */
421 0x0000fffc, /* dst_mask */
422 TRUE), /* pcrel_offset */
423
424 /* A relative 16 bit branch. Bit 10 should be set to indicate that
425 the branch is not expected to be taken. The lower two bits must
426 be zero. */
427 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 TRUE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
434 ppc64_elf_brtaken_reloc, /* special_function */
435 "R_PPC64_REL14_BRNTAKEN",/* name */
436 FALSE, /* partial_inplace */
437 0, /* src_mask */
438 0x0000fffc, /* dst_mask */
439 TRUE), /* pcrel_offset */
440
441 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
442 symbol. */
443 HOWTO (R_PPC64_GOT16, /* type */
444 0, /* rightshift */
445 1, /* size (0 = byte, 1 = short, 2 = long) */
446 16, /* bitsize */
447 FALSE, /* pc_relative */
448 0, /* bitpos */
449 complain_overflow_signed, /* complain_on_overflow */
450 ppc64_elf_unhandled_reloc, /* special_function */
451 "R_PPC64_GOT16", /* name */
452 FALSE, /* partial_inplace */
453 0, /* src_mask */
454 0xffff, /* dst_mask */
455 FALSE), /* pcrel_offset */
456
457 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
458 the symbol. */
459 HOWTO (R_PPC64_GOT16_LO, /* type */
460 0, /* rightshift */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
463 FALSE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_dont, /* complain_on_overflow */
466 ppc64_elf_unhandled_reloc, /* special_function */
467 "R_PPC64_GOT16_LO", /* name */
468 FALSE, /* partial_inplace */
469 0, /* src_mask */
470 0xffff, /* dst_mask */
471 FALSE), /* pcrel_offset */
472
473 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
474 the symbol. */
475 HOWTO (R_PPC64_GOT16_HI, /* type */
476 16, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont,/* complain_on_overflow */
482 ppc64_elf_unhandled_reloc, /* special_function */
483 "R_PPC64_GOT16_HI", /* name */
484 FALSE, /* partial_inplace */
485 0, /* src_mask */
486 0xffff, /* dst_mask */
487 FALSE), /* pcrel_offset */
488
489 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
490 the symbol. */
491 HOWTO (R_PPC64_GOT16_HA, /* type */
492 16, /* rightshift */
493 1, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
495 FALSE, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_dont,/* complain_on_overflow */
498 ppc64_elf_unhandled_reloc, /* special_function */
499 "R_PPC64_GOT16_HA", /* name */
500 FALSE, /* partial_inplace */
501 0, /* src_mask */
502 0xffff, /* dst_mask */
503 FALSE), /* pcrel_offset */
504
505 /* This is used only by the dynamic linker. The symbol should exist
506 both in the object being run and in some shared library. The
507 dynamic linker copies the data addressed by the symbol from the
508 shared library into the object, because the object being
509 run has to have the data at some particular address. */
510 HOWTO (R_PPC64_COPY, /* type */
511 0, /* rightshift */
512 0, /* this one is variable size */
513 0, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_dont, /* complain_on_overflow */
517 ppc64_elf_unhandled_reloc, /* special_function */
518 "R_PPC64_COPY", /* name */
519 FALSE, /* partial_inplace */
520 0, /* src_mask */
521 0, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 /* Like R_PPC64_ADDR64, but used when setting global offset table
525 entries. */
526 HOWTO (R_PPC64_GLOB_DAT, /* type */
527 0, /* rightshift */
528 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
529 64, /* bitsize */
530 FALSE, /* pc_relative */
531 0, /* bitpos */
532 complain_overflow_dont, /* complain_on_overflow */
533 ppc64_elf_unhandled_reloc, /* special_function */
534 "R_PPC64_GLOB_DAT", /* name */
535 FALSE, /* partial_inplace */
536 0, /* src_mask */
537 ONES (64), /* dst_mask */
538 FALSE), /* pcrel_offset */
539
540 /* Created by the link editor. Marks a procedure linkage table
541 entry for a symbol. */
542 HOWTO (R_PPC64_JMP_SLOT, /* type */
543 0, /* rightshift */
544 0, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
549 ppc64_elf_unhandled_reloc, /* special_function */
550 "R_PPC64_JMP_SLOT", /* name */
551 FALSE, /* partial_inplace */
552 0, /* src_mask */
553 0, /* dst_mask */
554 FALSE), /* pcrel_offset */
555
556 /* Used only by the dynamic linker. When the object is run, this
557 doubleword64 is set to the load address of the object, plus the
558 addend. */
559 HOWTO (R_PPC64_RELATIVE, /* type */
560 0, /* rightshift */
561 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
562 64, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_dont, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_RELATIVE", /* name */
568 FALSE, /* partial_inplace */
569 0, /* src_mask */
570 ONES (64), /* dst_mask */
571 FALSE), /* pcrel_offset */
572
573 /* Like R_PPC64_ADDR32, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR32, /* type */
575 0, /* rightshift */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
577 32, /* bitsize */
578 FALSE, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_UADDR32", /* name */
583 FALSE, /* partial_inplace */
584 0, /* src_mask */
585 0xffffffff, /* dst_mask */
586 FALSE), /* pcrel_offset */
587
588 /* Like R_PPC64_ADDR16, but may be unaligned. */
589 HOWTO (R_PPC64_UADDR16, /* type */
590 0, /* rightshift */
591 1, /* size (0 = byte, 1 = short, 2 = long) */
592 16, /* bitsize */
593 FALSE, /* pc_relative */
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 bfd_elf_generic_reloc, /* special_function */
597 "R_PPC64_UADDR16", /* name */
598 FALSE, /* partial_inplace */
599 0, /* src_mask */
600 0xffff, /* dst_mask */
601 FALSE), /* pcrel_offset */
602
603 /* 32-bit PC relative. */
604 HOWTO (R_PPC64_REL32, /* type */
605 0, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 32, /* bitsize */
608 TRUE, /* pc_relative */
609 0, /* bitpos */
610 /* FIXME: Verify. Was complain_overflow_bitfield. */
611 complain_overflow_signed, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_PPC64_REL32", /* name */
614 FALSE, /* partial_inplace */
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
617 TRUE), /* pcrel_offset */
618
619 /* 32-bit relocation to the symbol's procedure linkage table. */
620 HOWTO (R_PPC64_PLT32, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 32, /* bitsize */
624 FALSE, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
627 ppc64_elf_unhandled_reloc, /* special_function */
628 "R_PPC64_PLT32", /* name */
629 FALSE, /* partial_inplace */
630 0, /* src_mask */
631 0xffffffff, /* dst_mask */
632 FALSE), /* pcrel_offset */
633
634 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
635 FIXME: R_PPC64_PLTREL32 not supported. */
636 HOWTO (R_PPC64_PLTREL32, /* type */
637 0, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 32, /* bitsize */
640 TRUE, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 "R_PPC64_PLTREL32", /* name */
645 FALSE, /* partial_inplace */
646 0, /* src_mask */
647 0xffffffff, /* dst_mask */
648 TRUE), /* pcrel_offset */
649
650 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
651 the symbol. */
652 HOWTO (R_PPC64_PLT16_LO, /* type */
653 0, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
656 FALSE, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_dont, /* complain_on_overflow */
659 ppc64_elf_unhandled_reloc, /* special_function */
660 "R_PPC64_PLT16_LO", /* name */
661 FALSE, /* partial_inplace */
662 0, /* src_mask */
663 0xffff, /* dst_mask */
664 FALSE), /* pcrel_offset */
665
666 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
667 the symbol. */
668 HOWTO (R_PPC64_PLT16_HI, /* type */
669 16, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
675 ppc64_elf_unhandled_reloc, /* special_function */
676 "R_PPC64_PLT16_HI", /* name */
677 FALSE, /* partial_inplace */
678 0, /* src_mask */
679 0xffff, /* dst_mask */
680 FALSE), /* pcrel_offset */
681
682 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
683 the symbol. */
684 HOWTO (R_PPC64_PLT16_HA, /* type */
685 16, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
691 ppc64_elf_unhandled_reloc, /* special_function */
692 "R_PPC64_PLT16_HA", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0xffff, /* dst_mask */
696 FALSE), /* pcrel_offset */
697
698 /* 16-bit section relative relocation. */
699 HOWTO (R_PPC64_SECTOFF, /* type */
700 0, /* rightshift */
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
703 FALSE, /* pc_relative */
704 0, /* bitpos */
705 complain_overflow_bitfield, /* complain_on_overflow */
706 ppc64_elf_sectoff_reloc, /* special_function */
707 "R_PPC64_SECTOFF", /* name */
708 FALSE, /* partial_inplace */
709 0, /* src_mask */
710 0xffff, /* dst_mask */
711 FALSE), /* pcrel_offset */
712
713 /* Like R_PPC64_SECTOFF, but no overflow warning. */
714 HOWTO (R_PPC64_SECTOFF_LO, /* type */
715 0, /* rightshift */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
718 FALSE, /* pc_relative */
719 0, /* bitpos */
720 complain_overflow_dont, /* complain_on_overflow */
721 ppc64_elf_sectoff_reloc, /* special_function */
722 "R_PPC64_SECTOFF_LO", /* name */
723 FALSE, /* partial_inplace */
724 0, /* src_mask */
725 0xffff, /* dst_mask */
726 FALSE), /* pcrel_offset */
727
728 /* 16-bit upper half section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HI, /* type */
730 16, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
733 FALSE, /* pc_relative */
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
736 ppc64_elf_sectoff_reloc, /* special_function */
737 "R_PPC64_SECTOFF_HI", /* name */
738 FALSE, /* partial_inplace */
739 0, /* src_mask */
740 0xffff, /* dst_mask */
741 FALSE), /* pcrel_offset */
742
743 /* 16-bit upper half adjusted section relative relocation. */
744 HOWTO (R_PPC64_SECTOFF_HA, /* type */
745 16, /* rightshift */
746 1, /* size (0 = byte, 1 = short, 2 = long) */
747 16, /* bitsize */
748 FALSE, /* pc_relative */
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
751 ppc64_elf_sectoff_ha_reloc, /* special_function */
752 "R_PPC64_SECTOFF_HA", /* name */
753 FALSE, /* partial_inplace */
754 0, /* src_mask */
755 0xffff, /* dst_mask */
756 FALSE), /* pcrel_offset */
757
758 /* Like R_PPC64_REL24 without touching the two least significant bits. */
759 HOWTO (R_PPC64_REL30, /* type */
760 2, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 30, /* bitsize */
763 TRUE, /* pc_relative */
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
767 "R_PPC64_REL30", /* name */
768 FALSE, /* partial_inplace */
769 0, /* src_mask */
770 0xfffffffc, /* dst_mask */
771 TRUE), /* pcrel_offset */
772
773 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
774
775 /* A standard 64-bit relocation. */
776 HOWTO (R_PPC64_ADDR64, /* type */
777 0, /* rightshift */
778 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
779 64, /* bitsize */
780 FALSE, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC64_ADDR64", /* name */
785 FALSE, /* partial_inplace */
786 0, /* src_mask */
787 ONES (64), /* dst_mask */
788 FALSE), /* pcrel_offset */
789
790 /* The bits 32-47 of an address. */
791 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
792 32, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR16_HIGHER", /* name */
800 FALSE, /* partial_inplace */
801 0, /* src_mask */
802 0xffff, /* dst_mask */
803 FALSE), /* pcrel_offset */
804
805 /* The bits 32-47 of an address, plus 1 if the contents of the low
806 16 bits, treated as a signed number, is negative. */
807 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
808 32, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 ppc64_elf_ha_reloc, /* special_function */
815 "R_PPC64_ADDR16_HIGHERA", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* The bits 48-63 of an address. */
822 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
823 48, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC64_ADDR16_HIGHEST", /* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0xffff, /* dst_mask */
834 FALSE), /* pcrel_offset */
835
836 /* The bits 48-63 of an address, plus 1 if the contents of the low
837 16 bits, treated as a signed number, is negative. */
838 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
839 48, /* rightshift */
840 1, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 ppc64_elf_ha_reloc, /* special_function */
846 "R_PPC64_ADDR16_HIGHESTA", /* name */
847 FALSE, /* partial_inplace */
848 0, /* src_mask */
849 0xffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
851
852 /* Like ADDR64, but may be unaligned. */
853 HOWTO (R_PPC64_UADDR64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
857 FALSE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_UADDR64", /* name */
862 FALSE, /* partial_inplace */
863 0, /* src_mask */
864 ONES (64), /* dst_mask */
865 FALSE), /* pcrel_offset */
866
867 /* 64-bit relative relocation. */
868 HOWTO (R_PPC64_REL64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
872 TRUE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 "R_PPC64_REL64", /* name */
877 FALSE, /* partial_inplace */
878 0, /* src_mask */
879 ONES (64), /* dst_mask */
880 TRUE), /* pcrel_offset */
881
882 /* 64-bit relocation to the symbol's procedure linkage table. */
883 HOWTO (R_PPC64_PLT64, /* type */
884 0, /* rightshift */
885 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 64, /* bitsize */
887 FALSE, /* pc_relative */
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
890 ppc64_elf_unhandled_reloc, /* special_function */
891 "R_PPC64_PLT64", /* name */
892 FALSE, /* partial_inplace */
893 0, /* src_mask */
894 ONES (64), /* dst_mask */
895 FALSE), /* pcrel_offset */
896
897 /* 64-bit PC relative relocation to the symbol's procedure linkage
898 table. */
899 /* FIXME: R_PPC64_PLTREL64 not supported. */
900 HOWTO (R_PPC64_PLTREL64, /* type */
901 0, /* rightshift */
902 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903 64, /* bitsize */
904 TRUE, /* pc_relative */
905 0, /* bitpos */
906 complain_overflow_dont, /* complain_on_overflow */
907 ppc64_elf_unhandled_reloc, /* special_function */
908 "R_PPC64_PLTREL64", /* name */
909 FALSE, /* partial_inplace */
910 0, /* src_mask */
911 ONES (64), /* dst_mask */
912 TRUE), /* pcrel_offset */
913
914 /* 16 bit TOC-relative relocation. */
915
916 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
917 HOWTO (R_PPC64_TOC16, /* type */
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
921 FALSE, /* pc_relative */
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
924 ppc64_elf_toc_reloc, /* special_function */
925 "R_PPC64_TOC16", /* name */
926 FALSE, /* partial_inplace */
927 0, /* src_mask */
928 0xffff, /* dst_mask */
929 FALSE), /* pcrel_offset */
930
931 /* 16 bit TOC-relative relocation without overflow. */
932
933 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
934 HOWTO (R_PPC64_TOC16_LO, /* type */
935 0, /* rightshift */
936 1, /* size (0 = byte, 1 = short, 2 = long) */
937 16, /* bitsize */
938 FALSE, /* pc_relative */
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
941 ppc64_elf_toc_reloc, /* special_function */
942 "R_PPC64_TOC16_LO", /* name */
943 FALSE, /* partial_inplace */
944 0, /* src_mask */
945 0xffff, /* dst_mask */
946 FALSE), /* pcrel_offset */
947
948 /* 16 bit TOC-relative relocation, high 16 bits. */
949
950 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
951 HOWTO (R_PPC64_TOC16_HI, /* type */
952 16, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
955 FALSE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc64_elf_toc_reloc, /* special_function */
959 "R_PPC64_TOC16_HI", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 0xffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
964
965 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
966 contents of the low 16 bits, treated as a signed number, is
967 negative. */
968
969 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
970 HOWTO (R_PPC64_TOC16_HA, /* type */
971 16, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
974 FALSE, /* pc_relative */
975 0, /* bitpos */
976 complain_overflow_dont, /* complain_on_overflow */
977 ppc64_elf_toc_ha_reloc, /* special_function */
978 "R_PPC64_TOC16_HA", /* name */
979 FALSE, /* partial_inplace */
980 0, /* src_mask */
981 0xffff, /* dst_mask */
982 FALSE), /* pcrel_offset */
983
984 /* 64-bit relocation; insert value of TOC base (.TOC.). */
985
986 /* R_PPC64_TOC 51 doubleword64 .TOC. */
987 HOWTO (R_PPC64_TOC, /* type */
988 0, /* rightshift */
989 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
990 64, /* bitsize */
991 FALSE, /* pc_relative */
992 0, /* bitpos */
993 complain_overflow_bitfield, /* complain_on_overflow */
994 ppc64_elf_toc64_reloc, /* special_function */
995 "R_PPC64_TOC", /* name */
996 FALSE, /* partial_inplace */
997 0, /* src_mask */
998 ONES (64), /* dst_mask */
999 FALSE), /* pcrel_offset */
1000
1001 /* Like R_PPC64_GOT16, but also informs the link editor that the
1002 value to relocate may (!) refer to a PLT entry which the link
1003 editor (a) may replace with the symbol value. If the link editor
1004 is unable to fully resolve the symbol, it may (b) create a PLT
1005 entry and store the address to the new PLT entry in the GOT.
1006 This permits lazy resolution of function symbols at run time.
1007 The link editor may also skip all of this and just (c) emit a
1008 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1009 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1010 HOWTO (R_PPC64_PLTGOT16, /* type */
1011 0, /* rightshift */
1012 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 16, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_signed, /* complain_on_overflow */
1017 ppc64_elf_unhandled_reloc, /* special_function */
1018 "R_PPC64_PLTGOT16", /* name */
1019 FALSE, /* partial_inplace */
1020 0, /* src_mask */
1021 0xffff, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1023
1024 /* Like R_PPC64_PLTGOT16, but without overflow. */
1025 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1026 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont, /* complain_on_overflow */
1033 ppc64_elf_unhandled_reloc, /* special_function */
1034 "R_PPC64_PLTGOT16_LO", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1039
1040 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1041 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1042 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1043 16, /* rightshift */
1044 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 16, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_dont, /* complain_on_overflow */
1049 ppc64_elf_unhandled_reloc, /* special_function */
1050 "R_PPC64_PLTGOT16_HI", /* name */
1051 FALSE, /* partial_inplace */
1052 0, /* src_mask */
1053 0xffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1055
1056 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1057 1 if the contents of the low 16 bits, treated as a signed number,
1058 is negative. */
1059 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1060 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1061 16, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_dont,/* complain_on_overflow */
1067 ppc64_elf_unhandled_reloc, /* special_function */
1068 "R_PPC64_PLTGOT16_HA", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xffff, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1073
1074 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_DS, /* type */
1076 0, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_bitfield, /* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_DS", /* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1088
1089 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont,/* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 "R_PPC64_ADDR16_LO_DS",/* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_signed, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1118
1119 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_GOT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1133
1134 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc64_elf_unhandled_reloc, /* special_function */
1143 "R_PPC64_PLT16_LO_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1148
1149 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_bitfield, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_DS", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1163
1164 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 ppc64_elf_sectoff_reloc, /* special_function */
1173 "R_PPC64_SECTOFF_LO_DS",/* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_signed, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1195 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1196 0, /* rightshift */
1197 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 16, /* bitsize */
1199 FALSE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_dont, /* complain_on_overflow */
1202 ppc64_elf_toc_reloc, /* special_function */
1203 "R_PPC64_TOC16_LO_DS", /* name */
1204 FALSE, /* partial_inplace */
1205 0, /* src_mask */
1206 0xfffc, /* dst_mask */
1207 FALSE), /* pcrel_offset */
1208
1209 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1210 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1211 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1212 0, /* rightshift */
1213 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 16, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_signed, /* complain_on_overflow */
1218 ppc64_elf_unhandled_reloc, /* special_function */
1219 "R_PPC64_PLTGOT16_DS", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0xfffc, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1224
1225 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1226 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1227 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1228 0, /* rightshift */
1229 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 16, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc64_elf_unhandled_reloc, /* special_function */
1235 "R_PPC64_PLTGOT16_LO_DS",/* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0xfffc, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1240
1241 /* Marker relocs for TLS. */
1242 HOWTO (R_PPC64_TLS,
1243 0, /* rightshift */
1244 2, /* size (0 = byte, 1 = short, 2 = long) */
1245 32, /* bitsize */
1246 FALSE, /* pc_relative */
1247 0, /* bitpos */
1248 complain_overflow_dont, /* complain_on_overflow */
1249 bfd_elf_generic_reloc, /* special_function */
1250 "R_PPC64_TLS", /* name */
1251 FALSE, /* partial_inplace */
1252 0, /* src_mask */
1253 0, /* dst_mask */
1254 FALSE), /* pcrel_offset */
1255
1256 HOWTO (R_PPC64_TLSGD,
1257 0, /* rightshift */
1258 2, /* size (0 = byte, 1 = short, 2 = long) */
1259 32, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont, /* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC64_TLSGD", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1269
1270 HOWTO (R_PPC64_TLSLD,
1271 0, /* rightshift */
1272 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 32, /* bitsize */
1274 FALSE, /* pc_relative */
1275 0, /* bitpos */
1276 complain_overflow_dont, /* complain_on_overflow */
1277 bfd_elf_generic_reloc, /* special_function */
1278 "R_PPC64_TLSLD", /* name */
1279 FALSE, /* partial_inplace */
1280 0, /* src_mask */
1281 0, /* dst_mask */
1282 FALSE), /* pcrel_offset */
1283
1284 /* Computes the load module index of the load module that contains the
1285 definition of its TLS sym. */
1286 HOWTO (R_PPC64_DTPMOD64,
1287 0, /* rightshift */
1288 4, /* size (0 = byte, 1 = short, 2 = long) */
1289 64, /* bitsize */
1290 FALSE, /* pc_relative */
1291 0, /* bitpos */
1292 complain_overflow_dont, /* complain_on_overflow */
1293 ppc64_elf_unhandled_reloc, /* special_function */
1294 "R_PPC64_DTPMOD64", /* name */
1295 FALSE, /* partial_inplace */
1296 0, /* src_mask */
1297 ONES (64), /* dst_mask */
1298 FALSE), /* pcrel_offset */
1299
1300 /* Computes a dtv-relative displacement, the difference between the value
1301 of sym+add and the base address of the thread-local storage block that
1302 contains the definition of sym, minus 0x8000. */
1303 HOWTO (R_PPC64_DTPREL64,
1304 0, /* rightshift */
1305 4, /* size (0 = byte, 1 = short, 2 = long) */
1306 64, /* bitsize */
1307 FALSE, /* pc_relative */
1308 0, /* bitpos */
1309 complain_overflow_dont, /* complain_on_overflow */
1310 ppc64_elf_unhandled_reloc, /* special_function */
1311 "R_PPC64_DTPREL64", /* name */
1312 FALSE, /* partial_inplace */
1313 0, /* src_mask */
1314 ONES (64), /* dst_mask */
1315 FALSE), /* pcrel_offset */
1316
1317 /* A 16 bit dtprel reloc. */
1318 HOWTO (R_PPC64_DTPREL16,
1319 0, /* rightshift */
1320 1, /* size (0 = byte, 1 = short, 2 = long) */
1321 16, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_signed, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL16", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0xffff, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* Like DTPREL16, but no overflow. */
1333 HOWTO (R_PPC64_DTPREL16_LO,
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_dont, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16_LO", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1348 HOWTO (R_PPC64_DTPREL16_HI,
1349 16, /* 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_HI", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HA,
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_HA", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HIGHER,
1379 32, /* 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_HIGHER", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHERA,
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_HIGHERA", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1409 48, /* 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_HIGHEST", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
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_HIGHESTA", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16, but for insns with a DS field. */
1438 HOWTO (R_PPC64_DTPREL16_DS,
1439 0, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_signed, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_DS", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xfffc, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16_DS, but no overflow. */
1453 HOWTO (R_PPC64_DTPREL16_LO_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_dont, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_LO_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Computes a tp-relative displacement, the difference between the value of
1468 sym+add and the value of the thread pointer (r13). */
1469 HOWTO (R_PPC64_TPREL64,
1470 0, /* rightshift */
1471 4, /* size (0 = byte, 1 = short, 2 = long) */
1472 64, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 ppc64_elf_unhandled_reloc, /* special_function */
1477 "R_PPC64_TPREL64", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 ONES (64), /* dst_mask */
1481 FALSE), /* pcrel_offset */
1482
1483 /* A 16 bit tprel reloc. */
1484 HOWTO (R_PPC64_TPREL16,
1485 0, /* rightshift */
1486 1, /* size (0 = byte, 1 = short, 2 = long) */
1487 16, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_signed, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL16", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0xffff, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* Like TPREL16, but no overflow. */
1499 HOWTO (R_PPC64_TPREL16_LO,
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_dont, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16_LO", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16_LO, but next higher group of 16 bits. */
1514 HOWTO (R_PPC64_TPREL16_HI,
1515 16, /* 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_HI", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_HI, but adjust for low 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HA,
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_HA", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but next higher group of 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HIGHER,
1545 32, /* 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_HIGHER", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHERA,
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_HIGHERA", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHEST,
1575 48, /* 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_HIGHEST", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHESTA,
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_HIGHESTA", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16, but for insns with a DS field. */
1604 HOWTO (R_PPC64_TPREL16_DS,
1605 0, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_signed, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_DS", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xfffc, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16_DS, but no overflow. */
1619 HOWTO (R_PPC64_TPREL16_LO_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_dont, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_LO_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1634 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1635 to the first entry relative to the TOC base (r2). */
1636 HOWTO (R_PPC64_GOT_TLSGD16,
1637 0, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_signed, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_GOT_TLSGD16", /* name */
1645 FALSE, /* partial_inplace */
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
1648 FALSE), /* pcrel_offset */
1649
1650 /* Like GOT_TLSGD16, but no overflow. */
1651 HOWTO (R_PPC64_GOT_TLSGD16_LO,
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_dont, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_GOT_TLSGD16_LO", /* name */
1660 FALSE, /* partial_inplace */
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
1663 FALSE), /* pcrel_offset */
1664
1665 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1667 16, /* 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_HI", /* name */
1675 FALSE, /* partial_inplace */
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
1678 FALSE), /* pcrel_offset */
1679
1680 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HA,
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_HA", /* name */
1690 FALSE, /* partial_inplace */
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
1693 FALSE), /* pcrel_offset */
1694
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and zero, and computes the offset to the
1697 first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSLD16,
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
1702 FALSE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSLD16", /* name */
1707 FALSE, /* partial_inplace */
1708 0, /* src_mask */
1709 0xffff, /* dst_mask */
1710 FALSE), /* pcrel_offset */
1711
1712 /* Like GOT_TLSLD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSLD16_LO,
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_dont, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16_LO", /* name */
1722 FALSE, /* partial_inplace */
1723 0, /* src_mask */
1724 0xffff, /* dst_mask */
1725 FALSE), /* pcrel_offset */
1726
1727 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1729 16, /* 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_HI", /* name */
1737 FALSE, /* partial_inplace */
1738 0, /* src_mask */
1739 0xffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1741
1742 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HA,
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_HA", /* name */
1752 FALSE, /* partial_inplace */
1753 0, /* src_mask */
1754 0xffff, /* dst_mask */
1755 FALSE), /* pcrel_offset */
1756
1757 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1758 the offset to the entry relative to the TOC base (r2). */
1759 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1760 0, /* rightshift */
1761 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 16, /* bitsize */
1763 FALSE, /* pc_relative */
1764 0, /* bitpos */
1765 complain_overflow_signed, /* complain_on_overflow */
1766 ppc64_elf_unhandled_reloc, /* special_function */
1767 "R_PPC64_GOT_DTPREL16_DS", /* name */
1768 FALSE, /* partial_inplace */
1769 0, /* src_mask */
1770 0xfffc, /* dst_mask */
1771 FALSE), /* pcrel_offset */
1772
1773 /* Like GOT_DTPREL16_DS, but no overflow. */
1774 HOWTO (R_PPC64_GOT_DTPREL16_LO_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_dont, /* complain_on_overflow */
1781 ppc64_elf_unhandled_reloc, /* special_function */
1782 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1783 FALSE, /* partial_inplace */
1784 0, /* src_mask */
1785 0xfffc, /* dst_mask */
1786 FALSE), /* pcrel_offset */
1787
1788 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1790 16, /* 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_HI", /* name */
1798 FALSE, /* partial_inplace */
1799 0, /* src_mask */
1800 0xffff, /* dst_mask */
1801 FALSE), /* pcrel_offset */
1802
1803 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HA,
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_HA", /* name */
1813 FALSE, /* partial_inplace */
1814 0, /* src_mask */
1815 0xffff, /* dst_mask */
1816 FALSE), /* pcrel_offset */
1817
1818 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1819 offset to the entry relative to the TOC base (r2). */
1820 HOWTO (R_PPC64_GOT_TPREL16_DS,
1821 0, /* rightshift */
1822 1, /* size (0 = byte, 1 = short, 2 = long) */
1823 16, /* bitsize */
1824 FALSE, /* pc_relative */
1825 0, /* bitpos */
1826 complain_overflow_signed, /* complain_on_overflow */
1827 ppc64_elf_unhandled_reloc, /* special_function */
1828 "R_PPC64_GOT_TPREL16_DS", /* name */
1829 FALSE, /* partial_inplace */
1830 0, /* src_mask */
1831 0xfffc, /* dst_mask */
1832 FALSE), /* pcrel_offset */
1833
1834 /* Like GOT_TPREL16_DS, but no overflow. */
1835 HOWTO (R_PPC64_GOT_TPREL16_LO_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_dont, /* complain_on_overflow */
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1844 FALSE, /* partial_inplace */
1845 0, /* src_mask */
1846 0xfffc, /* dst_mask */
1847 FALSE), /* pcrel_offset */
1848
1849 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1850 HOWTO (R_PPC64_GOT_TPREL16_HI,
1851 16, /* 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_HI", /* name */
1859 FALSE, /* partial_inplace */
1860 0, /* src_mask */
1861 0xffff, /* dst_mask */
1862 FALSE), /* pcrel_offset */
1863
1864 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HA,
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_HA", /* name */
1874 FALSE, /* partial_inplace */
1875 0, /* src_mask */
1876 0xffff, /* dst_mask */
1877 FALSE), /* pcrel_offset */
1878
1879 HOWTO (R_PPC64_JMP_IREL, /* type */
1880 0, /* rightshift */
1881 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1882 0, /* bitsize */
1883 FALSE, /* pc_relative */
1884 0, /* bitpos */
1885 complain_overflow_dont, /* complain_on_overflow */
1886 ppc64_elf_unhandled_reloc, /* special_function */
1887 "R_PPC64_JMP_IREL", /* name */
1888 FALSE, /* partial_inplace */
1889 0, /* src_mask */
1890 0, /* dst_mask */
1891 FALSE), /* pcrel_offset */
1892
1893 HOWTO (R_PPC64_IRELATIVE, /* type */
1894 0, /* rightshift */
1895 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1896 64, /* bitsize */
1897 FALSE, /* pc_relative */
1898 0, /* bitpos */
1899 complain_overflow_dont, /* complain_on_overflow */
1900 bfd_elf_generic_reloc, /* special_function */
1901 "R_PPC64_IRELATIVE", /* name */
1902 FALSE, /* partial_inplace */
1903 0, /* src_mask */
1904 ONES (64), /* dst_mask */
1905 FALSE), /* pcrel_offset */
1906
1907 /* A 16 bit relative relocation. */
1908 HOWTO (R_PPC64_REL16, /* type */
1909 0, /* rightshift */
1910 1, /* size (0 = byte, 1 = short, 2 = long) */
1911 16, /* bitsize */
1912 TRUE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_bitfield, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_REL16", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 0xffff, /* dst_mask */
1920 TRUE), /* pcrel_offset */
1921
1922 /* A 16 bit relative relocation without overflow. */
1923 HOWTO (R_PPC64_REL16_LO, /* 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_dont,/* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16_LO", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* The high order 16 bits of a relative address. */
1938 HOWTO (R_PPC64_REL16_HI, /* type */
1939 16, /* 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_HI", /* 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, plus 1 if the contents of
1953 the low 16 bits, treated as a signed number, is negative. */
1954 HOWTO (R_PPC64_REL16_HA, /* type */
1955 16, /* rightshift */
1956 1, /* size (0 = byte, 1 = short, 2 = long) */
1957 16, /* bitsize */
1958 TRUE, /* pc_relative */
1959 0, /* bitpos */
1960 complain_overflow_dont, /* complain_on_overflow */
1961 ppc64_elf_ha_reloc, /* special_function */
1962 "R_PPC64_REL16_HA", /* name */
1963 FALSE, /* partial_inplace */
1964 0, /* src_mask */
1965 0xffff, /* dst_mask */
1966 TRUE), /* pcrel_offset */
1967
1968 /* GNU extension to record C++ vtable hierarchy. */
1969 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1970 0, /* rightshift */
1971 0, /* size (0 = byte, 1 = short, 2 = long) */
1972 0, /* bitsize */
1973 FALSE, /* pc_relative */
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 NULL, /* special_function */
1977 "R_PPC64_GNU_VTINHERIT", /* name */
1978 FALSE, /* partial_inplace */
1979 0, /* src_mask */
1980 0, /* dst_mask */
1981 FALSE), /* pcrel_offset */
1982
1983 /* GNU extension to record C++ vtable member usage. */
1984 HOWTO (R_PPC64_GNU_VTENTRY, /* 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_VTENTRY", /* name */
1993 FALSE, /* partial_inplace */
1994 0, /* src_mask */
1995 0, /* dst_mask */
1996 FALSE), /* pcrel_offset */
1997 };
1998
1999 \f
2000 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2001 be done. */
2002
2003 static void
2004 ppc_howto_init (void)
2005 {
2006 unsigned int i, type;
2007
2008 for (i = 0;
2009 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2010 i++)
2011 {
2012 type = ppc64_elf_howto_raw[i].type;
2013 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2014 / sizeof (ppc64_elf_howto_table[0])));
2015 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2016 }
2017 }
2018
2019 static reloc_howto_type *
2020 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2021 bfd_reloc_code_real_type code)
2022 {
2023 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2024
2025 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2026 /* Initialize howto table if needed. */
2027 ppc_howto_init ();
2028
2029 switch (code)
2030 {
2031 default:
2032 return NULL;
2033
2034 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2035 break;
2036 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2037 break;
2038 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2039 break;
2040 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2041 break;
2042 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2043 break;
2044 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2045 break;
2046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2047 break;
2048 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2049 break;
2050 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2051 break;
2052 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2053 break;
2054 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2055 break;
2056 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2057 break;
2058 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2059 break;
2060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2061 break;
2062 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2063 break;
2064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2065 break;
2066 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2067 break;
2068 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2069 break;
2070 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2071 break;
2072 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2073 break;
2074 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2075 break;
2076 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2077 break;
2078 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2079 break;
2080 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2081 break;
2082 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2083 break;
2084 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2085 break;
2086 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2087 break;
2088 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2089 break;
2090 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2091 break;
2092 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2093 break;
2094 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2095 break;
2096 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2097 break;
2098 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2099 break;
2100 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2101 break;
2102 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2103 break;
2104 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2105 break;
2106 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2107 break;
2108 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2109 break;
2110 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2111 break;
2112 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2113 break;
2114 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2115 break;
2116 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2117 break;
2118 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2119 break;
2120 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2121 break;
2122 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2123 break;
2124 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2125 break;
2126 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2127 break;
2128 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2129 break;
2130 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2131 break;
2132 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2133 break;
2134 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2135 break;
2136 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2137 break;
2138 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2139 break;
2140 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2141 break;
2142 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2143 break;
2144 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2145 break;
2146 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2147 break;
2148 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2149 break;
2150 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2151 break;
2152 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2153 break;
2154 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2155 break;
2156 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2157 break;
2158 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2159 break;
2160 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2161 break;
2162 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2163 break;
2164 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2165 break;
2166 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2167 break;
2168 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2169 break;
2170 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2171 break;
2172 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2173 break;
2174 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2175 break;
2176 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2177 break;
2178 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2179 break;
2180 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2181 break;
2182 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2183 break;
2184 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2185 break;
2186 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2187 break;
2188 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2189 break;
2190 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2191 break;
2192 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2193 break;
2194 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2195 break;
2196 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2197 break;
2198 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2199 break;
2200 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2201 break;
2202 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2203 break;
2204 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2205 break;
2206 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2207 break;
2208 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2209 break;
2210 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2211 break;
2212 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2213 break;
2214 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2215 break;
2216 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2217 break;
2218 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2219 break;
2220 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2221 break;
2222 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2223 break;
2224 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2225 break;
2226 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2227 break;
2228 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2229 break;
2230 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2231 break;
2232 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2233 break;
2234 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2235 break;
2236 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2237 break;
2238 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2239 break;
2240 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2241 break;
2242 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2243 break;
2244 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2245 break;
2246 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2247 break;
2248 }
2249
2250 return ppc64_elf_howto_table[r];
2251 };
2252
2253 static reloc_howto_type *
2254 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2255 const char *r_name)
2256 {
2257 unsigned int i;
2258
2259 for (i = 0;
2260 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2261 i++)
2262 if (ppc64_elf_howto_raw[i].name != NULL
2263 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2264 return &ppc64_elf_howto_raw[i];
2265
2266 return NULL;
2267 }
2268
2269 /* Set the howto pointer for a PowerPC ELF reloc. */
2270
2271 static void
2272 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2273 Elf_Internal_Rela *dst)
2274 {
2275 unsigned int type;
2276
2277 /* Initialize howto table if needed. */
2278 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2279 ppc_howto_init ();
2280
2281 type = ELF64_R_TYPE (dst->r_info);
2282 if (type >= (sizeof (ppc64_elf_howto_table)
2283 / sizeof (ppc64_elf_howto_table[0])))
2284 {
2285 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2286 abfd, (int) type);
2287 type = R_PPC64_NONE;
2288 }
2289 cache_ptr->howto = ppc64_elf_howto_table[type];
2290 }
2291
2292 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2293
2294 static bfd_reloc_status_type
2295 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2296 void *data, asection *input_section,
2297 bfd *output_bfd, char **error_message)
2298 {
2299 /* If this is a relocatable link (output_bfd test tells us), just
2300 call the generic function. Any adjustment will be done at final
2301 link time. */
2302 if (output_bfd != NULL)
2303 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2304 input_section, output_bfd, error_message);
2305
2306 /* Adjust the addend for sign extension of the low 16 bits.
2307 We won't actually be using the low 16 bits, so trashing them
2308 doesn't matter. */
2309 reloc_entry->addend += 0x8000;
2310 return bfd_reloc_continue;
2311 }
2312
2313 static bfd_reloc_status_type
2314 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2315 void *data, asection *input_section,
2316 bfd *output_bfd, char **error_message)
2317 {
2318 if (output_bfd != NULL)
2319 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2320 input_section, output_bfd, error_message);
2321
2322 if (strcmp (symbol->section->name, ".opd") == 0
2323 && (symbol->section->owner->flags & DYNAMIC) == 0)
2324 {
2325 bfd_vma dest = opd_entry_value (symbol->section,
2326 symbol->value + reloc_entry->addend,
2327 NULL, NULL);
2328 if (dest != (bfd_vma) -1)
2329 reloc_entry->addend = dest - (symbol->value
2330 + symbol->section->output_section->vma
2331 + symbol->section->output_offset);
2332 }
2333 return bfd_reloc_continue;
2334 }
2335
2336 static bfd_reloc_status_type
2337 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2338 void *data, asection *input_section,
2339 bfd *output_bfd, char **error_message)
2340 {
2341 long insn;
2342 enum elf_ppc64_reloc_type r_type;
2343 bfd_size_type octets;
2344 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2345 bfd_boolean is_power4 = FALSE;
2346
2347 /* If this is a relocatable link (output_bfd test tells us), just
2348 call the generic function. Any adjustment will be done at final
2349 link time. */
2350 if (output_bfd != NULL)
2351 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2352 input_section, output_bfd, error_message);
2353
2354 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2355 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2356 insn &= ~(0x01 << 21);
2357 r_type = reloc_entry->howto->type;
2358 if (r_type == R_PPC64_ADDR14_BRTAKEN
2359 || r_type == R_PPC64_REL14_BRTAKEN)
2360 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2361
2362 if (is_power4)
2363 {
2364 /* Set 'a' bit. This is 0b00010 in BO field for branch
2365 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2366 for branch on CTR insns (BO == 1a00t or 1a01t). */
2367 if ((insn & (0x14 << 21)) == (0x04 << 21))
2368 insn |= 0x02 << 21;
2369 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2370 insn |= 0x08 << 21;
2371 else
2372 goto out;
2373 }
2374 else
2375 {
2376 bfd_vma target = 0;
2377 bfd_vma from;
2378
2379 if (!bfd_is_com_section (symbol->section))
2380 target = symbol->value;
2381 target += symbol->section->output_section->vma;
2382 target += symbol->section->output_offset;
2383 target += reloc_entry->addend;
2384
2385 from = (reloc_entry->address
2386 + input_section->output_offset
2387 + input_section->output_section->vma);
2388
2389 /* Invert 'y' bit if not the default. */
2390 if ((bfd_signed_vma) (target - from) < 0)
2391 insn ^= 0x01 << 21;
2392 }
2393 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2394 out:
2395 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd, error_message);
2397 }
2398
2399 static bfd_reloc_status_type
2400 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2401 void *data, asection *input_section,
2402 bfd *output_bfd, char **error_message)
2403 {
2404 /* If this is a relocatable link (output_bfd test tells us), just
2405 call the generic function. Any adjustment will be done at final
2406 link time. */
2407 if (output_bfd != NULL)
2408 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2409 input_section, output_bfd, error_message);
2410
2411 /* Subtract the symbol section base address. */
2412 reloc_entry->addend -= symbol->section->output_section->vma;
2413 return bfd_reloc_continue;
2414 }
2415
2416 static bfd_reloc_status_type
2417 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2418 void *data, asection *input_section,
2419 bfd *output_bfd, char **error_message)
2420 {
2421 /* If this is a relocatable link (output_bfd test tells us), just
2422 call the generic function. Any adjustment will be done at final
2423 link time. */
2424 if (output_bfd != NULL)
2425 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2426 input_section, output_bfd, error_message);
2427
2428 /* Subtract the symbol section base address. */
2429 reloc_entry->addend -= symbol->section->output_section->vma;
2430
2431 /* Adjust the addend for sign extension of the low 16 bits. */
2432 reloc_entry->addend += 0x8000;
2433 return bfd_reloc_continue;
2434 }
2435
2436 static bfd_reloc_status_type
2437 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2438 void *data, asection *input_section,
2439 bfd *output_bfd, char **error_message)
2440 {
2441 bfd_vma TOCstart;
2442
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2445 link time. */
2446 if (output_bfd != NULL)
2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2448 input_section, output_bfd, error_message);
2449
2450 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2451 if (TOCstart == 0)
2452 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2453
2454 /* Subtract the TOC base address. */
2455 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2456 return bfd_reloc_continue;
2457 }
2458
2459 static bfd_reloc_status_type
2460 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2461 void *data, asection *input_section,
2462 bfd *output_bfd, char **error_message)
2463 {
2464 bfd_vma TOCstart;
2465
2466 /* If this is a relocatable link (output_bfd test tells us), just
2467 call the generic function. Any adjustment will be done at final
2468 link time. */
2469 if (output_bfd != NULL)
2470 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2471 input_section, output_bfd, error_message);
2472
2473 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474 if (TOCstart == 0)
2475 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2476
2477 /* Subtract the TOC base address. */
2478 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2479
2480 /* Adjust the addend for sign extension of the low 16 bits. */
2481 reloc_entry->addend += 0x8000;
2482 return bfd_reloc_continue;
2483 }
2484
2485 static bfd_reloc_status_type
2486 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
2489 {
2490 bfd_vma TOCstart;
2491 bfd_size_type octets;
2492
2493 /* If this is a relocatable link (output_bfd test tells us), just
2494 call the generic function. Any adjustment will be done at final
2495 link time. */
2496 if (output_bfd != NULL)
2497 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2498 input_section, output_bfd, error_message);
2499
2500 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2501 if (TOCstart == 0)
2502 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2503
2504 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2505 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2506 return bfd_reloc_ok;
2507 }
2508
2509 static bfd_reloc_status_type
2510 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2511 void *data, asection *input_section,
2512 bfd *output_bfd, char **error_message)
2513 {
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2519 input_section, output_bfd, error_message);
2520
2521 if (error_message != NULL)
2522 {
2523 static char buf[60];
2524 sprintf (buf, "generic linker can't handle %s",
2525 reloc_entry->howto->name);
2526 *error_message = buf;
2527 }
2528 return bfd_reloc_dangerous;
2529 }
2530
2531 /* Track GOT entries needed for a given symbol. We might need more
2532 than one got entry per symbol. */
2533 struct got_entry
2534 {
2535 struct got_entry *next;
2536
2537 /* The symbol addend that we'll be placing in the GOT. */
2538 bfd_vma addend;
2539
2540 /* Unlike other ELF targets, we use separate GOT entries for the same
2541 symbol referenced from different input files. This is to support
2542 automatic multiple TOC/GOT sections, where the TOC base can vary
2543 from one input file to another. After partitioning into TOC groups
2544 we merge entries within the group.
2545
2546 Point to the BFD owning this GOT entry. */
2547 bfd *owner;
2548
2549 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2550 TLS_TPREL or TLS_DTPREL for tls entries. */
2551 unsigned char tls_type;
2552
2553 /* Non-zero if got.ent points to real entry. */
2554 unsigned char is_indirect;
2555
2556 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2557 union
2558 {
2559 bfd_signed_vma refcount;
2560 bfd_vma offset;
2561 struct got_entry *ent;
2562 } got;
2563 };
2564
2565 /* The same for PLT. */
2566 struct plt_entry
2567 {
2568 struct plt_entry *next;
2569
2570 bfd_vma addend;
2571
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 } plt;
2577 };
2578
2579 struct ppc64_elf_obj_tdata
2580 {
2581 struct elf_obj_tdata elf;
2582
2583 /* Shortcuts to dynamic linker sections. */
2584 asection *got;
2585 asection *relgot;
2586
2587 /* Used during garbage collection. We attach global symbols defined
2588 on removed .opd entries to this section so that the sym is removed. */
2589 asection *deleted_section;
2590
2591 /* TLS local dynamic got entry handling. Support for multiple GOT
2592 sections means we potentially need one of these for each input bfd. */
2593 struct got_entry tlsld_got;
2594
2595 /* A copy of relocs before they are modified for --emit-relocs. */
2596 Elf_Internal_Rela *opd_relocs;
2597
2598 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2599 the reloc to be in the range -32768 to 32767. */
2600 unsigned int has_small_toc_reloc;
2601 };
2602
2603 #define ppc64_elf_tdata(bfd) \
2604 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2605
2606 #define ppc64_tlsld_got(bfd) \
2607 (&ppc64_elf_tdata (bfd)->tlsld_got)
2608
2609 #define is_ppc64_elf(bfd) \
2610 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2611 && elf_object_id (bfd) == PPC64_ELF_DATA)
2612
2613 /* Override the generic function because we store some extras. */
2614
2615 static bfd_boolean
2616 ppc64_elf_mkobject (bfd *abfd)
2617 {
2618 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2619 PPC64_ELF_DATA);
2620 }
2621
2622 /* Fix bad default arch selected for a 64 bit input bfd when the
2623 default is 32 bit. */
2624
2625 static bfd_boolean
2626 ppc64_elf_object_p (bfd *abfd)
2627 {
2628 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2629 {
2630 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2631
2632 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2633 {
2634 /* Relies on arch after 32 bit default being 64 bit default. */
2635 abfd->arch_info = abfd->arch_info->next;
2636 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2637 }
2638 }
2639 return TRUE;
2640 }
2641
2642 /* Support for core dump NOTE sections. */
2643
2644 static bfd_boolean
2645 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2646 {
2647 size_t offset, size;
2648
2649 if (note->descsz != 504)
2650 return FALSE;
2651
2652 /* pr_cursig */
2653 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2654
2655 /* pr_pid */
2656 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2657
2658 /* pr_reg */
2659 offset = 112;
2660 size = 384;
2661
2662 /* Make a ".reg/999" section. */
2663 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2664 size, note->descpos + offset);
2665 }
2666
2667 static bfd_boolean
2668 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2669 {
2670 if (note->descsz != 136)
2671 return FALSE;
2672
2673 elf_tdata (abfd)->core_pid
2674 = bfd_get_32 (abfd, note->descdata + 24);
2675 elf_tdata (abfd)->core_program
2676 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2677 elf_tdata (abfd)->core_command
2678 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2679
2680 return TRUE;
2681 }
2682
2683 static char *
2684 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2685 ...)
2686 {
2687 switch (note_type)
2688 {
2689 default:
2690 return NULL;
2691
2692 case NT_PRPSINFO:
2693 {
2694 char data[136];
2695 va_list ap;
2696
2697 va_start (ap, note_type);
2698 memset (data, 0, 40);
2699 strncpy (data + 40, va_arg (ap, const char *), 16);
2700 strncpy (data + 56, va_arg (ap, const char *), 80);
2701 va_end (ap);
2702 return elfcore_write_note (abfd, buf, bufsiz,
2703 "CORE", note_type, data, sizeof (data));
2704 }
2705
2706 case NT_PRSTATUS:
2707 {
2708 char data[504];
2709 va_list ap;
2710 long pid;
2711 int cursig;
2712 const void *greg;
2713
2714 va_start (ap, note_type);
2715 memset (data, 0, 112);
2716 pid = va_arg (ap, long);
2717 bfd_put_32 (abfd, pid, data + 32);
2718 cursig = va_arg (ap, int);
2719 bfd_put_16 (abfd, cursig, data + 12);
2720 greg = va_arg (ap, const void *);
2721 memcpy (data + 112, greg, 384);
2722 memset (data + 496, 0, 8);
2723 va_end (ap);
2724 return elfcore_write_note (abfd, buf, bufsiz,
2725 "CORE", note_type, data, sizeof (data));
2726 }
2727 }
2728 }
2729
2730 /* Add extra PPC sections. */
2731
2732 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2733 {
2734 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2735 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2736 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2737 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2738 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2739 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2740 { NULL, 0, 0, 0, 0 }
2741 };
2742
2743 enum _ppc64_sec_type {
2744 sec_normal = 0,
2745 sec_opd = 1,
2746 sec_toc = 2
2747 };
2748
2749 struct _ppc64_elf_section_data
2750 {
2751 struct bfd_elf_section_data elf;
2752
2753 union
2754 {
2755 /* An array with one entry for each opd function descriptor. */
2756 struct _opd_sec_data
2757 {
2758 /* Points to the function code section for local opd entries. */
2759 asection **func_sec;
2760
2761 /* After editing .opd, adjust references to opd local syms. */
2762 long *adjust;
2763 } opd;
2764
2765 /* An array for toc sections, indexed by offset/8. */
2766 struct _toc_sec_data
2767 {
2768 /* Specifies the relocation symbol index used at a given toc offset. */
2769 unsigned *symndx;
2770
2771 /* And the relocation addend. */
2772 bfd_vma *add;
2773 } toc;
2774 } u;
2775
2776 enum _ppc64_sec_type sec_type:2;
2777
2778 /* Flag set when small branches are detected. Used to
2779 select suitable defaults for the stub group size. */
2780 unsigned int has_14bit_branch:1;
2781 };
2782
2783 #define ppc64_elf_section_data(sec) \
2784 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2785
2786 static bfd_boolean
2787 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2788 {
2789 if (!sec->used_by_bfd)
2790 {
2791 struct _ppc64_elf_section_data *sdata;
2792 bfd_size_type amt = sizeof (*sdata);
2793
2794 sdata = bfd_zalloc (abfd, amt);
2795 if (sdata == NULL)
2796 return FALSE;
2797 sec->used_by_bfd = sdata;
2798 }
2799
2800 return _bfd_elf_new_section_hook (abfd, sec);
2801 }
2802
2803 static struct _opd_sec_data *
2804 get_opd_info (asection * sec)
2805 {
2806 if (sec != NULL
2807 && ppc64_elf_section_data (sec) != NULL
2808 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2809 return &ppc64_elf_section_data (sec)->u.opd;
2810 return NULL;
2811 }
2812 \f
2813 /* Parameters for the qsort hook. */
2814 static bfd_boolean synthetic_relocatable;
2815
2816 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2817
2818 static int
2819 compare_symbols (const void *ap, const void *bp)
2820 {
2821 const asymbol *a = * (const asymbol **) ap;
2822 const asymbol *b = * (const asymbol **) bp;
2823
2824 /* Section symbols first. */
2825 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2826 return -1;
2827 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2828 return 1;
2829
2830 /* then .opd symbols. */
2831 if (strcmp (a->section->name, ".opd") == 0
2832 && strcmp (b->section->name, ".opd") != 0)
2833 return -1;
2834 if (strcmp (a->section->name, ".opd") != 0
2835 && strcmp (b->section->name, ".opd") == 0)
2836 return 1;
2837
2838 /* then other code symbols. */
2839 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2840 == (SEC_CODE | SEC_ALLOC)
2841 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2842 != (SEC_CODE | SEC_ALLOC))
2843 return -1;
2844
2845 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2846 != (SEC_CODE | SEC_ALLOC)
2847 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2848 == (SEC_CODE | SEC_ALLOC))
2849 return 1;
2850
2851 if (synthetic_relocatable)
2852 {
2853 if (a->section->id < b->section->id)
2854 return -1;
2855
2856 if (a->section->id > b->section->id)
2857 return 1;
2858 }
2859
2860 if (a->value + a->section->vma < b->value + b->section->vma)
2861 return -1;
2862
2863 if (a->value + a->section->vma > b->value + b->section->vma)
2864 return 1;
2865
2866 /* For syms with the same value, prefer strong dynamic global function
2867 syms over other syms. */
2868 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2869 return -1;
2870
2871 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2872 return 1;
2873
2874 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2875 return -1;
2876
2877 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2878 return 1;
2879
2880 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2881 return -1;
2882
2883 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2884 return 1;
2885
2886 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2887 return -1;
2888
2889 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2890 return 1;
2891
2892 return 0;
2893 }
2894
2895 /* Search SYMS for a symbol of the given VALUE. */
2896
2897 static asymbol *
2898 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2899 {
2900 long mid;
2901
2902 if (id == -1)
2903 {
2904 while (lo < hi)
2905 {
2906 mid = (lo + hi) >> 1;
2907 if (syms[mid]->value + syms[mid]->section->vma < value)
2908 lo = mid + 1;
2909 else if (syms[mid]->value + syms[mid]->section->vma > value)
2910 hi = mid;
2911 else
2912 return syms[mid];
2913 }
2914 }
2915 else
2916 {
2917 while (lo < hi)
2918 {
2919 mid = (lo + hi) >> 1;
2920 if (syms[mid]->section->id < id)
2921 lo = mid + 1;
2922 else if (syms[mid]->section->id > id)
2923 hi = mid;
2924 else if (syms[mid]->value < value)
2925 lo = mid + 1;
2926 else if (syms[mid]->value > value)
2927 hi = mid;
2928 else
2929 return syms[mid];
2930 }
2931 }
2932 return NULL;
2933 }
2934
2935 static bfd_boolean
2936 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2937 {
2938 bfd_vma vma = *(bfd_vma *) ptr;
2939 return ((section->flags & SEC_ALLOC) != 0
2940 && section->vma <= vma
2941 && vma < section->vma + section->size);
2942 }
2943
2944 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2945 entry syms. Also generate @plt symbols for the glink branch table. */
2946
2947 static long
2948 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2949 long static_count, asymbol **static_syms,
2950 long dyn_count, asymbol **dyn_syms,
2951 asymbol **ret)
2952 {
2953 asymbol *s;
2954 long i;
2955 long count;
2956 char *names;
2957 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2958 asection *opd;
2959 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2960 asymbol **syms;
2961
2962 *ret = NULL;
2963
2964 opd = bfd_get_section_by_name (abfd, ".opd");
2965 if (opd == NULL)
2966 return 0;
2967
2968 symcount = static_count;
2969 if (!relocatable)
2970 symcount += dyn_count;
2971 if (symcount == 0)
2972 return 0;
2973
2974 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2975 if (syms == NULL)
2976 return -1;
2977
2978 if (!relocatable && static_count != 0 && dyn_count != 0)
2979 {
2980 /* Use both symbol tables. */
2981 memcpy (syms, static_syms, static_count * sizeof (*syms));
2982 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2983 }
2984 else if (!relocatable && static_count == 0)
2985 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2986 else
2987 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2988
2989 synthetic_relocatable = relocatable;
2990 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2991
2992 if (!relocatable && symcount > 1)
2993 {
2994 long j;
2995 /* Trim duplicate syms, since we may have merged the normal and
2996 dynamic symbols. Actually, we only care about syms that have
2997 different values, so trim any with the same value. */
2998 for (i = 1, j = 1; i < symcount; ++i)
2999 if (syms[i - 1]->value + syms[i - 1]->section->vma
3000 != syms[i]->value + syms[i]->section->vma)
3001 syms[j++] = syms[i];
3002 symcount = j;
3003 }
3004
3005 i = 0;
3006 if (strcmp (syms[i]->section->name, ".opd") == 0)
3007 ++i;
3008 codesecsym = i;
3009
3010 for (; i < symcount; ++i)
3011 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3012 != (SEC_CODE | SEC_ALLOC))
3013 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3014 break;
3015 codesecsymend = i;
3016
3017 for (; i < symcount; ++i)
3018 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3019 break;
3020 secsymend = i;
3021
3022 for (; i < symcount; ++i)
3023 if (strcmp (syms[i]->section->name, ".opd") != 0)
3024 break;
3025 opdsymend = i;
3026
3027 for (; i < symcount; ++i)
3028 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3029 != (SEC_CODE | SEC_ALLOC))
3030 break;
3031 symcount = i;
3032
3033 count = 0;
3034
3035 if (relocatable)
3036 {
3037 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3038 arelent *r;
3039 size_t size;
3040 long relcount;
3041
3042 if (opdsymend == secsymend)
3043 goto done;
3044
3045 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3046 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3047 if (relcount == 0)
3048 goto done;
3049
3050 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3051 {
3052 count = -1;
3053 goto done;
3054 }
3055
3056 size = 0;
3057 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3058 {
3059 asymbol *sym;
3060
3061 while (r < opd->relocation + relcount
3062 && r->address < syms[i]->value + opd->vma)
3063 ++r;
3064
3065 if (r == opd->relocation + relcount)
3066 break;
3067
3068 if (r->address != syms[i]->value + opd->vma)
3069 continue;
3070
3071 if (r->howto->type != R_PPC64_ADDR64)
3072 continue;
3073
3074 sym = *r->sym_ptr_ptr;
3075 if (!sym_exists_at (syms, opdsymend, symcount,
3076 sym->section->id, sym->value + r->addend))
3077 {
3078 ++count;
3079 size += sizeof (asymbol);
3080 size += strlen (syms[i]->name) + 2;
3081 }
3082 }
3083
3084 s = *ret = bfd_malloc (size);
3085 if (s == NULL)
3086 {
3087 count = -1;
3088 goto done;
3089 }
3090
3091 names = (char *) (s + count);
3092
3093 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3094 {
3095 asymbol *sym;
3096
3097 while (r < opd->relocation + relcount
3098 && r->address < syms[i]->value + opd->vma)
3099 ++r;
3100
3101 if (r == opd->relocation + relcount)
3102 break;
3103
3104 if (r->address != syms[i]->value + opd->vma)
3105 continue;
3106
3107 if (r->howto->type != R_PPC64_ADDR64)
3108 continue;
3109
3110 sym = *r->sym_ptr_ptr;
3111 if (!sym_exists_at (syms, opdsymend, symcount,
3112 sym->section->id, sym->value + r->addend))
3113 {
3114 size_t len;
3115
3116 *s = *syms[i];
3117 s->flags |= BSF_SYNTHETIC;
3118 s->section = sym->section;
3119 s->value = sym->value + r->addend;
3120 s->name = names;
3121 *names++ = '.';
3122 len = strlen (syms[i]->name);
3123 memcpy (names, syms[i]->name, len + 1);
3124 names += len + 1;
3125 /* Have udata.p point back to the original symbol this
3126 synthetic symbol was derived from. */
3127 s->udata.p = syms[i];
3128 s++;
3129 }
3130 }
3131 }
3132 else
3133 {
3134 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3135 bfd_byte *contents;
3136 size_t size;
3137 long plt_count = 0;
3138 bfd_vma glink_vma = 0, resolv_vma = 0;
3139 asection *dynamic, *glink = NULL, *relplt = NULL;
3140 arelent *p;
3141
3142 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3143 {
3144 if (contents)
3145 {
3146 free_contents_and_exit:
3147 free (contents);
3148 }
3149 count = -1;
3150 goto done;
3151 }
3152
3153 size = 0;
3154 for (i = secsymend; i < opdsymend; ++i)
3155 {
3156 bfd_vma ent;
3157
3158 /* Ignore bogus symbols. */
3159 if (syms[i]->value > opd->size - 8)
3160 continue;
3161
3162 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3163 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3164 {
3165 ++count;
3166 size += sizeof (asymbol);
3167 size += strlen (syms[i]->name) + 2;
3168 }
3169 }
3170
3171 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3172 if (dyn_count != 0
3173 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3174 {
3175 bfd_byte *dynbuf, *extdyn, *extdynend;
3176 size_t extdynsize;
3177 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3178
3179 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3180 goto free_contents_and_exit;
3181
3182 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3183 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3184
3185 extdyn = dynbuf;
3186 extdynend = extdyn + dynamic->size;
3187 for (; extdyn < extdynend; extdyn += extdynsize)
3188 {
3189 Elf_Internal_Dyn dyn;
3190 (*swap_dyn_in) (abfd, extdyn, &dyn);
3191
3192 if (dyn.d_tag == DT_NULL)
3193 break;
3194
3195 if (dyn.d_tag == DT_PPC64_GLINK)
3196 {
3197 /* The first glink stub starts at offset 32; see comment in
3198 ppc64_elf_finish_dynamic_sections. */
3199 glink_vma = dyn.d_un.d_val + 32;
3200 /* The .glink section usually does not survive the final
3201 link; search for the section (usually .text) where the
3202 glink stubs now reside. */
3203 glink = bfd_sections_find_if (abfd, section_covers_vma,
3204 &glink_vma);
3205 break;
3206 }
3207 }
3208
3209 free (dynbuf);
3210 }
3211
3212 if (glink != NULL)
3213 {
3214 /* Determine __glink trampoline by reading the relative branch
3215 from the first glink stub. */
3216 bfd_byte buf[4];
3217 if (bfd_get_section_contents (abfd, glink, buf,
3218 glink_vma + 4 - glink->vma, 4))
3219 {
3220 unsigned int insn = bfd_get_32 (abfd, buf);
3221 insn ^= B_DOT;
3222 if ((insn & ~0x3fffffc) == 0)
3223 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3224 }
3225
3226 if (resolv_vma)
3227 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3228
3229 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3230 if (relplt != NULL)
3231 {
3232 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3233 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3234 goto free_contents_and_exit;
3235
3236 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3237 size += plt_count * sizeof (asymbol);
3238
3239 p = relplt->relocation;
3240 for (i = 0; i < plt_count; i++, p++)
3241 {
3242 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3243 if (p->addend != 0)
3244 size += sizeof ("+0x") - 1 + 16;
3245 }
3246 }
3247 }
3248
3249 s = *ret = bfd_malloc (size);
3250 if (s == NULL)
3251 goto free_contents_and_exit;
3252
3253 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3254
3255 for (i = secsymend; i < opdsymend; ++i)
3256 {
3257 bfd_vma ent;
3258
3259 if (syms[i]->value > opd->size - 8)
3260 continue;
3261
3262 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3263 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3264 {
3265 long lo, hi;
3266 size_t len;
3267 asection *sec = abfd->sections;
3268
3269 *s = *syms[i];
3270 lo = codesecsym;
3271 hi = codesecsymend;
3272 while (lo < hi)
3273 {
3274 long mid = (lo + hi) >> 1;
3275 if (syms[mid]->section->vma < ent)
3276 lo = mid + 1;
3277 else if (syms[mid]->section->vma > ent)
3278 hi = mid;
3279 else
3280 {
3281 sec = syms[mid]->section;
3282 break;
3283 }
3284 }
3285
3286 if (lo >= hi && lo > codesecsym)
3287 sec = syms[lo - 1]->section;
3288
3289 for (; sec != NULL; sec = sec->next)
3290 {
3291 if (sec->vma > ent)
3292 break;
3293 /* SEC_LOAD may not be set if SEC is from a separate debug
3294 info file. */
3295 if ((sec->flags & SEC_ALLOC) == 0)
3296 break;
3297 if ((sec->flags & SEC_CODE) != 0)
3298 s->section = sec;
3299 }
3300 s->flags |= BSF_SYNTHETIC;
3301 s->value = ent - s->section->vma;
3302 s->name = names;
3303 *names++ = '.';
3304 len = strlen (syms[i]->name);
3305 memcpy (names, syms[i]->name, len + 1);
3306 names += len + 1;
3307 /* Have udata.p point back to the original symbol this
3308 synthetic symbol was derived from. */
3309 s->udata.p = syms[i];
3310 s++;
3311 }
3312 }
3313 free (contents);
3314
3315 if (glink != NULL && relplt != NULL)
3316 {
3317 if (resolv_vma)
3318 {
3319 /* Add a symbol for the main glink trampoline. */
3320 memset (s, 0, sizeof *s);
3321 s->the_bfd = abfd;
3322 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3323 s->section = glink;
3324 s->value = resolv_vma - glink->vma;
3325 s->name = names;
3326 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3327 names += sizeof ("__glink_PLTresolve");
3328 s++;
3329 count++;
3330 }
3331
3332 /* FIXME: It would be very much nicer to put sym@plt on the
3333 stub rather than on the glink branch table entry. The
3334 objdump disassembler would then use a sensible symbol
3335 name on plt calls. The difficulty in doing so is
3336 a) finding the stubs, and,
3337 b) matching stubs against plt entries, and,
3338 c) there can be multiple stubs for a given plt entry.
3339
3340 Solving (a) could be done by code scanning, but older
3341 ppc64 binaries used different stubs to current code.
3342 (b) is the tricky one since you need to known the toc
3343 pointer for at least one function that uses a pic stub to
3344 be able to calculate the plt address referenced.
3345 (c) means gdb would need to set multiple breakpoints (or
3346 find the glink branch itself) when setting breakpoints
3347 for pending shared library loads. */
3348 p = relplt->relocation;
3349 for (i = 0; i < plt_count; i++, p++)
3350 {
3351 size_t len;
3352
3353 *s = **p->sym_ptr_ptr;
3354 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3355 we are defining a symbol, ensure one of them is set. */
3356 if ((s->flags & BSF_LOCAL) == 0)
3357 s->flags |= BSF_GLOBAL;
3358 s->flags |= BSF_SYNTHETIC;
3359 s->section = glink;
3360 s->value = glink_vma - glink->vma;
3361 s->name = names;
3362 s->udata.p = NULL;
3363 len = strlen ((*p->sym_ptr_ptr)->name);
3364 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3365 names += len;
3366 if (p->addend != 0)
3367 {
3368 memcpy (names, "+0x", sizeof ("+0x") - 1);
3369 names += sizeof ("+0x") - 1;
3370 bfd_sprintf_vma (abfd, names, p->addend);
3371 names += strlen (names);
3372 }
3373 memcpy (names, "@plt", sizeof ("@plt"));
3374 names += sizeof ("@plt");
3375 s++;
3376 glink_vma += 8;
3377 if (i >= 0x8000)
3378 glink_vma += 4;
3379 }
3380 count += plt_count;
3381 }
3382 }
3383
3384 done:
3385 free (syms);
3386 return count;
3387 }
3388 \f
3389 /* The following functions are specific to the ELF linker, while
3390 functions above are used generally. Those named ppc64_elf_* are
3391 called by the main ELF linker code. They appear in this file more
3392 or less in the order in which they are called. eg.
3393 ppc64_elf_check_relocs is called early in the link process,
3394 ppc64_elf_finish_dynamic_sections is one of the last functions
3395 called.
3396
3397 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3398 functions have both a function code symbol and a function descriptor
3399 symbol. A call to foo in a relocatable object file looks like:
3400
3401 . .text
3402 . x:
3403 . bl .foo
3404 . nop
3405
3406 The function definition in another object file might be:
3407
3408 . .section .opd
3409 . foo: .quad .foo
3410 . .quad .TOC.@tocbase
3411 . .quad 0
3412 .
3413 . .text
3414 . .foo: blr
3415
3416 When the linker resolves the call during a static link, the branch
3417 unsurprisingly just goes to .foo and the .opd information is unused.
3418 If the function definition is in a shared library, things are a little
3419 different: The call goes via a plt call stub, the opd information gets
3420 copied to the plt, and the linker patches the nop.
3421
3422 . x:
3423 . bl .foo_stub
3424 . ld 2,40(1)
3425 .
3426 .
3427 . .foo_stub:
3428 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3429 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3430 . std 2,40(1) # this is the general idea
3431 . ld 11,0(12)
3432 . ld 2,8(12)
3433 . mtctr 11
3434 . ld 11,16(12)
3435 . bctr
3436 .
3437 . .section .plt
3438 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3439
3440 The "reloc ()" notation is supposed to indicate that the linker emits
3441 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3442 copying.
3443
3444 What are the difficulties here? Well, firstly, the relocations
3445 examined by the linker in check_relocs are against the function code
3446 sym .foo, while the dynamic relocation in the plt is emitted against
3447 the function descriptor symbol, foo. Somewhere along the line, we need
3448 to carefully copy dynamic link information from one symbol to the other.
3449 Secondly, the generic part of the elf linker will make .foo a dynamic
3450 symbol as is normal for most other backends. We need foo dynamic
3451 instead, at least for an application final link. However, when
3452 creating a shared library containing foo, we need to have both symbols
3453 dynamic so that references to .foo are satisfied during the early
3454 stages of linking. Otherwise the linker might decide to pull in a
3455 definition from some other object, eg. a static library.
3456
3457 Update: As of August 2004, we support a new convention. Function
3458 calls may use the function descriptor symbol, ie. "bl foo". This
3459 behaves exactly as "bl .foo". */
3460
3461 /* Of those relocs that might be copied as dynamic relocs, this function
3462 selects those that must be copied when linking a shared library,
3463 even when the symbol is local. */
3464
3465 static int
3466 must_be_dyn_reloc (struct bfd_link_info *info,
3467 enum elf_ppc64_reloc_type r_type)
3468 {
3469 switch (r_type)
3470 {
3471 default:
3472 return 1;
3473
3474 case R_PPC64_REL32:
3475 case R_PPC64_REL64:
3476 case R_PPC64_REL30:
3477 return 0;
3478
3479 case R_PPC64_TPREL16:
3480 case R_PPC64_TPREL16_LO:
3481 case R_PPC64_TPREL16_HI:
3482 case R_PPC64_TPREL16_HA:
3483 case R_PPC64_TPREL16_DS:
3484 case R_PPC64_TPREL16_LO_DS:
3485 case R_PPC64_TPREL16_HIGHER:
3486 case R_PPC64_TPREL16_HIGHERA:
3487 case R_PPC64_TPREL16_HIGHEST:
3488 case R_PPC64_TPREL16_HIGHESTA:
3489 case R_PPC64_TPREL64:
3490 return !info->executable;
3491 }
3492 }
3493
3494 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3495 copying dynamic variables from a shared lib into an app's dynbss
3496 section, and instead use a dynamic relocation to point into the
3497 shared lib. With code that gcc generates, it's vital that this be
3498 enabled; In the PowerPC64 ABI, the address of a function is actually
3499 the address of a function descriptor, which resides in the .opd
3500 section. gcc uses the descriptor directly rather than going via the
3501 GOT as some other ABI's do, which means that initialized function
3502 pointers must reference the descriptor. Thus, a function pointer
3503 initialized to the address of a function in a shared library will
3504 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3505 redefines the function descriptor symbol to point to the copy. This
3506 presents a problem as a plt entry for that function is also
3507 initialized from the function descriptor symbol and the copy reloc
3508 may not be initialized first. */
3509 #define ELIMINATE_COPY_RELOCS 1
3510
3511 /* Section name for stubs is the associated section name plus this
3512 string. */
3513 #define STUB_SUFFIX ".stub"
3514
3515 /* Linker stubs.
3516 ppc_stub_long_branch:
3517 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3518 destination, but a 24 bit branch in a stub section will reach.
3519 . b dest
3520
3521 ppc_stub_plt_branch:
3522 Similar to the above, but a 24 bit branch in the stub section won't
3523 reach its destination.
3524 . addis %r12,%r2,xxx@toc@ha
3525 . ld %r11,xxx@toc@l(%r12)
3526 . mtctr %r11
3527 . bctr
3528
3529 ppc_stub_plt_call:
3530 Used to call a function in a shared library. If it so happens that
3531 the plt entry referenced crosses a 64k boundary, then an extra
3532 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3533 . addis %r12,%r2,xxx@toc@ha
3534 . std %r2,40(%r1)
3535 . ld %r11,xxx+0@toc@l(%r12)
3536 . mtctr %r11
3537 . ld %r2,xxx+8@toc@l(%r12)
3538 . ld %r11,xxx+16@toc@l(%r12)
3539 . bctr
3540
3541 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3542 code to adjust the value and save r2 to support multiple toc sections.
3543 A ppc_stub_long_branch with an r2 offset looks like:
3544 . std %r2,40(%r1)
3545 . addis %r2,%r2,off@ha
3546 . addi %r2,%r2,off@l
3547 . b dest
3548
3549 A ppc_stub_plt_branch with an r2 offset looks like:
3550 . std %r2,40(%r1)
3551 . addis %r12,%r2,xxx@toc@ha
3552 . ld %r11,xxx@toc@l(%r12)
3553 . addis %r2,%r2,off@ha
3554 . addi %r2,%r2,off@l
3555 . mtctr %r11
3556 . bctr
3557
3558 In cases where the "addis" instruction would add zero, the "addis" is
3559 omitted and following instructions modified slightly in some cases.
3560 */
3561
3562 enum ppc_stub_type {
3563 ppc_stub_none,
3564 ppc_stub_long_branch,
3565 ppc_stub_long_branch_r2off,
3566 ppc_stub_plt_branch,
3567 ppc_stub_plt_branch_r2off,
3568 ppc_stub_plt_call
3569 };
3570
3571 struct ppc_stub_hash_entry {
3572
3573 /* Base hash table entry structure. */
3574 struct bfd_hash_entry root;
3575
3576 enum ppc_stub_type stub_type;
3577
3578 /* The stub section. */
3579 asection *stub_sec;
3580
3581 /* Offset within stub_sec of the beginning of this stub. */
3582 bfd_vma stub_offset;
3583
3584 /* Given the symbol's value and its section we can determine its final
3585 value when building the stubs (so the stub knows where to jump. */
3586 bfd_vma target_value;
3587 asection *target_section;
3588
3589 /* The symbol table entry, if any, that this was derived from. */
3590 struct ppc_link_hash_entry *h;
3591 struct plt_entry *plt_ent;
3592
3593 /* And the reloc addend that this was derived from. */
3594 bfd_vma addend;
3595
3596 /* Where this stub is being called from, or, in the case of combined
3597 stub sections, the first input section in the group. */
3598 asection *id_sec;
3599 };
3600
3601 struct ppc_branch_hash_entry {
3602
3603 /* Base hash table entry structure. */
3604 struct bfd_hash_entry root;
3605
3606 /* Offset within branch lookup table. */
3607 unsigned int offset;
3608
3609 /* Generation marker. */
3610 unsigned int iter;
3611 };
3612
3613 struct ppc_link_hash_entry
3614 {
3615 struct elf_link_hash_entry elf;
3616
3617 union {
3618 /* A pointer to the most recently used stub hash entry against this
3619 symbol. */
3620 struct ppc_stub_hash_entry *stub_cache;
3621
3622 /* A pointer to the next symbol starting with a '.' */
3623 struct ppc_link_hash_entry *next_dot_sym;
3624 } u;
3625
3626 /* Track dynamic relocs copied for this symbol. */
3627 struct elf_dyn_relocs *dyn_relocs;
3628
3629 /* Link between function code and descriptor symbols. */
3630 struct ppc_link_hash_entry *oh;
3631
3632 /* Flag function code and descriptor symbols. */
3633 unsigned int is_func:1;
3634 unsigned int is_func_descriptor:1;
3635 unsigned int fake:1;
3636
3637 /* Whether global opd/toc sym has been adjusted or not.
3638 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3639 should be set for all globals defined in any opd/toc section. */
3640 unsigned int adjust_done:1;
3641
3642 /* Set if we twiddled this symbol to weak at some stage. */
3643 unsigned int was_undefined:1;
3644
3645 /* Contexts in which symbol is used in the GOT (or TOC).
3646 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3647 corresponding relocs are encountered during check_relocs.
3648 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3649 indicate the corresponding GOT entry type is not needed.
3650 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3651 a TPREL one. We use a separate flag rather than setting TPREL
3652 just for convenience in distinguishing the two cases. */
3653 #define TLS_GD 1 /* GD reloc. */
3654 #define TLS_LD 2 /* LD reloc. */
3655 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3656 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3657 #define TLS_TLS 16 /* Any TLS reloc. */
3658 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3659 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3660 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3661 unsigned char tls_mask;
3662 };
3663
3664 /* ppc64 ELF linker hash table. */
3665
3666 struct ppc_link_hash_table
3667 {
3668 struct elf_link_hash_table elf;
3669
3670 /* The stub hash table. */
3671 struct bfd_hash_table stub_hash_table;
3672
3673 /* Another hash table for plt_branch stubs. */
3674 struct bfd_hash_table branch_hash_table;
3675
3676 /* Linker stub bfd. */
3677 bfd *stub_bfd;
3678
3679 /* Linker call-backs. */
3680 asection * (*add_stub_section) (const char *, asection *);
3681 void (*layout_sections_again) (void);
3682
3683 /* Array to keep track of which stub sections have been created, and
3684 information on stub grouping. */
3685 struct map_stub {
3686 /* This is the section to which stubs in the group will be attached. */
3687 asection *link_sec;
3688 /* The stub section. */
3689 asection *stub_sec;
3690 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3691 bfd_vma toc_off;
3692 } *stub_group;
3693
3694 /* Temp used when calculating TOC pointers. */
3695 bfd_vma toc_curr;
3696 bfd *toc_bfd;
3697 asection *toc_first_sec;
3698
3699 /* Highest input section id. */
3700 int top_id;
3701
3702 /* Highest output section index. */
3703 int top_index;
3704
3705 /* Used when adding symbols. */
3706 struct ppc_link_hash_entry *dot_syms;
3707
3708 /* List of input sections for each output section. */
3709 asection **input_list;
3710
3711 /* Short-cuts to get to dynamic linker sections. */
3712 asection *got;
3713 asection *plt;
3714 asection *relplt;
3715 asection *iplt;
3716 asection *reliplt;
3717 asection *dynbss;
3718 asection *relbss;
3719 asection *glink;
3720 asection *sfpr;
3721 asection *brlt;
3722 asection *relbrlt;
3723
3724 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3725 struct ppc_link_hash_entry *tls_get_addr;
3726 struct ppc_link_hash_entry *tls_get_addr_fd;
3727
3728 /* The size of reliplt used by got entry relocs. */
3729 bfd_size_type got_reli_size;
3730
3731 /* Statistics. */
3732 unsigned long stub_count[ppc_stub_plt_call];
3733
3734 /* Number of stubs against global syms. */
3735 unsigned long stub_globals;
3736
3737 /* Set if PLT call stubs should load r11. */
3738 unsigned int plt_static_chain:1;
3739
3740 /* Set if we should emit symbols for stubs. */
3741 unsigned int emit_stub_syms:1;
3742
3743 /* Set if __tls_get_addr optimization should not be done. */
3744 unsigned int no_tls_get_addr_opt:1;
3745
3746 /* Support for multiple toc sections. */
3747 unsigned int do_multi_toc:1;
3748 unsigned int multi_toc_needed:1;
3749 unsigned int second_toc_pass:1;
3750 unsigned int do_toc_opt:1;
3751
3752 /* Set on error. */
3753 unsigned int stub_error:1;
3754
3755 /* Temp used by ppc64_elf_process_dot_syms. */
3756 unsigned int twiddled_syms:1;
3757
3758 /* Incremented every time we size stubs. */
3759 unsigned int stub_iteration;
3760
3761 /* Small local sym cache. */
3762 struct sym_cache sym_cache;
3763 };
3764
3765 /* Rename some of the generic section flags to better document how they
3766 are used here. */
3767
3768 /* Nonzero if this section has TLS related relocations. */
3769 #define has_tls_reloc sec_flg0
3770
3771 /* Nonzero if this section has a call to __tls_get_addr. */
3772 #define has_tls_get_addr_call sec_flg1
3773
3774 /* Nonzero if this section has any toc or got relocs. */
3775 #define has_toc_reloc sec_flg2
3776
3777 /* Nonzero if this section has a call to another section that uses
3778 the toc or got. */
3779 #define makes_toc_func_call sec_flg3
3780
3781 /* Recursion protection when determining above flag. */
3782 #define call_check_in_progress sec_flg4
3783 #define call_check_done sec_flg5
3784
3785 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3786
3787 #define ppc_hash_table(p) \
3788 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3789 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3790
3791 #define ppc_stub_hash_lookup(table, string, create, copy) \
3792 ((struct ppc_stub_hash_entry *) \
3793 bfd_hash_lookup ((table), (string), (create), (copy)))
3794
3795 #define ppc_branch_hash_lookup(table, string, create, copy) \
3796 ((struct ppc_branch_hash_entry *) \
3797 bfd_hash_lookup ((table), (string), (create), (copy)))
3798
3799 /* Create an entry in the stub hash table. */
3800
3801 static struct bfd_hash_entry *
3802 stub_hash_newfunc (struct bfd_hash_entry *entry,
3803 struct bfd_hash_table *table,
3804 const char *string)
3805 {
3806 /* Allocate the structure if it has not already been allocated by a
3807 subclass. */
3808 if (entry == NULL)
3809 {
3810 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3811 if (entry == NULL)
3812 return entry;
3813 }
3814
3815 /* Call the allocation method of the superclass. */
3816 entry = bfd_hash_newfunc (entry, table, string);
3817 if (entry != NULL)
3818 {
3819 struct ppc_stub_hash_entry *eh;
3820
3821 /* Initialize the local fields. */
3822 eh = (struct ppc_stub_hash_entry *) entry;
3823 eh->stub_type = ppc_stub_none;
3824 eh->stub_sec = NULL;
3825 eh->stub_offset = 0;
3826 eh->target_value = 0;
3827 eh->target_section = NULL;
3828 eh->h = NULL;
3829 eh->id_sec = NULL;
3830 }
3831
3832 return entry;
3833 }
3834
3835 /* Create an entry in the branch hash table. */
3836
3837 static struct bfd_hash_entry *
3838 branch_hash_newfunc (struct bfd_hash_entry *entry,
3839 struct bfd_hash_table *table,
3840 const char *string)
3841 {
3842 /* Allocate the structure if it has not already been allocated by a
3843 subclass. */
3844 if (entry == NULL)
3845 {
3846 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3847 if (entry == NULL)
3848 return entry;
3849 }
3850
3851 /* Call the allocation method of the superclass. */
3852 entry = bfd_hash_newfunc (entry, table, string);
3853 if (entry != NULL)
3854 {
3855 struct ppc_branch_hash_entry *eh;
3856
3857 /* Initialize the local fields. */
3858 eh = (struct ppc_branch_hash_entry *) entry;
3859 eh->offset = 0;
3860 eh->iter = 0;
3861 }
3862
3863 return entry;
3864 }
3865
3866 /* Create an entry in a ppc64 ELF linker hash table. */
3867
3868 static struct bfd_hash_entry *
3869 link_hash_newfunc (struct bfd_hash_entry *entry,
3870 struct bfd_hash_table *table,
3871 const char *string)
3872 {
3873 /* Allocate the structure if it has not already been allocated by a
3874 subclass. */
3875 if (entry == NULL)
3876 {
3877 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3878 if (entry == NULL)
3879 return entry;
3880 }
3881
3882 /* Call the allocation method of the superclass. */
3883 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3884 if (entry != NULL)
3885 {
3886 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3887
3888 memset (&eh->u.stub_cache, 0,
3889 (sizeof (struct ppc_link_hash_entry)
3890 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3891
3892 /* When making function calls, old ABI code references function entry
3893 points (dot symbols), while new ABI code references the function
3894 descriptor symbol. We need to make any combination of reference and
3895 definition work together, without breaking archive linking.
3896
3897 For a defined function "foo" and an undefined call to "bar":
3898 An old object defines "foo" and ".foo", references ".bar" (possibly
3899 "bar" too).
3900 A new object defines "foo" and references "bar".
3901
3902 A new object thus has no problem with its undefined symbols being
3903 satisfied by definitions in an old object. On the other hand, the
3904 old object won't have ".bar" satisfied by a new object.
3905
3906 Keep a list of newly added dot-symbols. */
3907
3908 if (string[0] == '.')
3909 {
3910 struct ppc_link_hash_table *htab;
3911
3912 htab = (struct ppc_link_hash_table *) table;
3913 eh->u.next_dot_sym = htab->dot_syms;
3914 htab->dot_syms = eh;
3915 }
3916 }
3917
3918 return entry;
3919 }
3920
3921 /* Create a ppc64 ELF linker hash table. */
3922
3923 static struct bfd_link_hash_table *
3924 ppc64_elf_link_hash_table_create (bfd *abfd)
3925 {
3926 struct ppc_link_hash_table *htab;
3927 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3928
3929 htab = bfd_zmalloc (amt);
3930 if (htab == NULL)
3931 return NULL;
3932
3933 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3934 sizeof (struct ppc_link_hash_entry),
3935 PPC64_ELF_DATA))
3936 {
3937 free (htab);
3938 return NULL;
3939 }
3940
3941 /* Init the stub hash table too. */
3942 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3943 sizeof (struct ppc_stub_hash_entry)))
3944 return NULL;
3945
3946 /* And the branch hash table. */
3947 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3948 sizeof (struct ppc_branch_hash_entry)))
3949 return NULL;
3950
3951 /* Initializing two fields of the union is just cosmetic. We really
3952 only care about glist, but when compiled on a 32-bit host the
3953 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3954 debugger inspection of these fields look nicer. */
3955 htab->elf.init_got_refcount.refcount = 0;
3956 htab->elf.init_got_refcount.glist = NULL;
3957 htab->elf.init_plt_refcount.refcount = 0;
3958 htab->elf.init_plt_refcount.glist = NULL;
3959 htab->elf.init_got_offset.offset = 0;
3960 htab->elf.init_got_offset.glist = NULL;
3961 htab->elf.init_plt_offset.offset = 0;
3962 htab->elf.init_plt_offset.glist = NULL;
3963
3964 return &htab->elf.root;
3965 }
3966
3967 /* Free the derived linker hash table. */
3968
3969 static void
3970 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3971 {
3972 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3973
3974 bfd_hash_table_free (&ret->stub_hash_table);
3975 bfd_hash_table_free (&ret->branch_hash_table);
3976 _bfd_generic_link_hash_table_free (hash);
3977 }
3978
3979 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3980
3981 void
3982 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3983 {
3984 struct ppc_link_hash_table *htab;
3985
3986 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3987
3988 /* Always hook our dynamic sections into the first bfd, which is the
3989 linker created stub bfd. This ensures that the GOT header is at
3990 the start of the output TOC section. */
3991 htab = ppc_hash_table (info);
3992 if (htab == NULL)
3993 return;
3994 htab->stub_bfd = abfd;
3995 htab->elf.dynobj = abfd;
3996 }
3997
3998 /* Build a name for an entry in the stub hash table. */
3999
4000 static char *
4001 ppc_stub_name (const asection *input_section,
4002 const asection *sym_sec,
4003 const struct ppc_link_hash_entry *h,
4004 const Elf_Internal_Rela *rel)
4005 {
4006 char *stub_name;
4007 bfd_size_type len;
4008
4009 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4010 offsets from a sym as a branch target? In fact, we could
4011 probably assume the addend is always zero. */
4012 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4013
4014 if (h)
4015 {
4016 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4017 stub_name = bfd_malloc (len);
4018 if (stub_name == NULL)
4019 return stub_name;
4020
4021 sprintf (stub_name, "%08x.%s+%x",
4022 input_section->id & 0xffffffff,
4023 h->elf.root.root.string,
4024 (int) rel->r_addend & 0xffffffff);
4025 }
4026 else
4027 {
4028 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4029 stub_name = bfd_malloc (len);
4030 if (stub_name == NULL)
4031 return stub_name;
4032
4033 sprintf (stub_name, "%08x.%x:%x+%x",
4034 input_section->id & 0xffffffff,
4035 sym_sec->id & 0xffffffff,
4036 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4037 (int) rel->r_addend & 0xffffffff);
4038 }
4039 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4040 stub_name[len - 2] = 0;
4041 return stub_name;
4042 }
4043
4044 /* Look up an entry in the stub hash. Stub entries are cached because
4045 creating the stub name takes a bit of time. */
4046
4047 static struct ppc_stub_hash_entry *
4048 ppc_get_stub_entry (const asection *input_section,
4049 const asection *sym_sec,
4050 struct ppc_link_hash_entry *h,
4051 const Elf_Internal_Rela *rel,
4052 struct ppc_link_hash_table *htab)
4053 {
4054 struct ppc_stub_hash_entry *stub_entry;
4055 const asection *id_sec;
4056
4057 /* If this input section is part of a group of sections sharing one
4058 stub section, then use the id of the first section in the group.
4059 Stub names need to include a section id, as there may well be
4060 more than one stub used to reach say, printf, and we need to
4061 distinguish between them. */
4062 id_sec = htab->stub_group[input_section->id].link_sec;
4063
4064 if (h != NULL && h->u.stub_cache != NULL
4065 && h->u.stub_cache->h == h
4066 && h->u.stub_cache->id_sec == id_sec)
4067 {
4068 stub_entry = h->u.stub_cache;
4069 }
4070 else
4071 {
4072 char *stub_name;
4073
4074 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4075 if (stub_name == NULL)
4076 return NULL;
4077
4078 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4079 stub_name, FALSE, FALSE);
4080 if (h != NULL)
4081 h->u.stub_cache = stub_entry;
4082
4083 free (stub_name);
4084 }
4085
4086 return stub_entry;
4087 }
4088
4089 /* Add a new stub entry to the stub hash. Not all fields of the new
4090 stub entry are initialised. */
4091
4092 static struct ppc_stub_hash_entry *
4093 ppc_add_stub (const char *stub_name,
4094 asection *section,
4095 struct bfd_link_info *info)
4096 {
4097 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4098 asection *link_sec;
4099 asection *stub_sec;
4100 struct ppc_stub_hash_entry *stub_entry;
4101
4102 link_sec = htab->stub_group[section->id].link_sec;
4103 stub_sec = htab->stub_group[section->id].stub_sec;
4104 if (stub_sec == NULL)
4105 {
4106 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4107 if (stub_sec == NULL)
4108 {
4109 size_t namelen;
4110 bfd_size_type len;
4111 char *s_name;
4112
4113 namelen = strlen (link_sec->name);
4114 len = namelen + sizeof (STUB_SUFFIX);
4115 s_name = bfd_alloc (htab->stub_bfd, len);
4116 if (s_name == NULL)
4117 return NULL;
4118
4119 memcpy (s_name, link_sec->name, namelen);
4120 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4121 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4122 if (stub_sec == NULL)
4123 return NULL;
4124 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4125 }
4126 htab->stub_group[section->id].stub_sec = stub_sec;
4127 }
4128
4129 /* Enter this entry into the linker stub hash table. */
4130 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4131 TRUE, FALSE);
4132 if (stub_entry == NULL)
4133 {
4134 info->callbacks->einfo (_("%B: cannot create stub entry %s\n"),
4135 section->owner, stub_name);
4136 return NULL;
4137 }
4138
4139 stub_entry->stub_sec = stub_sec;
4140 stub_entry->stub_offset = 0;
4141 stub_entry->id_sec = link_sec;
4142 return stub_entry;
4143 }
4144
4145 /* Create sections for linker generated code. */
4146
4147 static bfd_boolean
4148 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4149 {
4150 struct ppc_link_hash_table *htab;
4151 flagword flags;
4152
4153 htab = ppc_hash_table (info);
4154 if (htab == NULL)
4155 return FALSE;
4156
4157 /* Create .sfpr for code to save and restore fp regs. */
4158 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4159 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4160 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4161 flags);
4162 if (htab->sfpr == NULL
4163 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4164 return FALSE;
4165
4166 /* Create .glink for lazy dynamic linking support. */
4167 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4168 flags);
4169 if (htab->glink == NULL
4170 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4171 return FALSE;
4172
4173 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4174 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4175 if (htab->iplt == NULL
4176 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4177 return FALSE;
4178
4179 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4180 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4181 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4182 ".rela.iplt",
4183 flags);
4184 if (htab->reliplt == NULL
4185 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4186 return FALSE;
4187
4188 /* Create branch lookup table for plt_branch stubs. */
4189 flags = (SEC_ALLOC | SEC_LOAD
4190 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4191 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4192 flags);
4193 if (htab->brlt == NULL
4194 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4195 return FALSE;
4196
4197 if (!info->shared)
4198 return TRUE;
4199
4200 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4201 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4202 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4203 ".rela.branch_lt",
4204 flags);
4205 if (htab->relbrlt == NULL
4206 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4207 return FALSE;
4208
4209 return TRUE;
4210 }
4211
4212 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4213 not already done. */
4214
4215 static bfd_boolean
4216 create_got_section (bfd *abfd, struct bfd_link_info *info)
4217 {
4218 asection *got, *relgot;
4219 flagword flags;
4220 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4221
4222 if (!is_ppc64_elf (abfd))
4223 return FALSE;
4224 if (htab == NULL)
4225 return FALSE;
4226
4227 if (!htab->got)
4228 {
4229 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4230 return FALSE;
4231
4232 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4233 if (!htab->got)
4234 abort ();
4235 }
4236
4237 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4238 | SEC_LINKER_CREATED);
4239
4240 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4241 if (!got
4242 || !bfd_set_section_alignment (abfd, got, 3))
4243 return FALSE;
4244
4245 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4246 flags | SEC_READONLY);
4247 if (!relgot
4248 || ! bfd_set_section_alignment (abfd, relgot, 3))
4249 return FALSE;
4250
4251 ppc64_elf_tdata (abfd)->got = got;
4252 ppc64_elf_tdata (abfd)->relgot = relgot;
4253 return TRUE;
4254 }
4255
4256 /* Create the dynamic sections, and set up shortcuts. */
4257
4258 static bfd_boolean
4259 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4260 {
4261 struct ppc_link_hash_table *htab;
4262
4263 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4264 return FALSE;
4265
4266 htab = ppc_hash_table (info);
4267 if (htab == NULL)
4268 return FALSE;
4269
4270 if (!htab->got)
4271 htab->got = bfd_get_section_by_name (dynobj, ".got");
4272 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4273 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4274 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4275 if (!info->shared)
4276 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4277
4278 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4279 || (!info->shared && !htab->relbss))
4280 abort ();
4281
4282 return TRUE;
4283 }
4284
4285 /* Follow indirect and warning symbol links. */
4286
4287 static inline struct bfd_link_hash_entry *
4288 follow_link (struct bfd_link_hash_entry *h)
4289 {
4290 while (h->type == bfd_link_hash_indirect
4291 || h->type == bfd_link_hash_warning)
4292 h = h->u.i.link;
4293 return h;
4294 }
4295
4296 static inline struct elf_link_hash_entry *
4297 elf_follow_link (struct elf_link_hash_entry *h)
4298 {
4299 return (struct elf_link_hash_entry *) follow_link (&h->root);
4300 }
4301
4302 static inline struct ppc_link_hash_entry *
4303 ppc_follow_link (struct ppc_link_hash_entry *h)
4304 {
4305 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4306 }
4307
4308 /* Merge PLT info on FROM with that on TO. */
4309
4310 static void
4311 move_plt_plist (struct ppc_link_hash_entry *from,
4312 struct ppc_link_hash_entry *to)
4313 {
4314 if (from->elf.plt.plist != NULL)
4315 {
4316 if (to->elf.plt.plist != NULL)
4317 {
4318 struct plt_entry **entp;
4319 struct plt_entry *ent;
4320
4321 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4322 {
4323 struct plt_entry *dent;
4324
4325 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4326 if (dent->addend == ent->addend)
4327 {
4328 dent->plt.refcount += ent->plt.refcount;
4329 *entp = ent->next;
4330 break;
4331 }
4332 if (dent == NULL)
4333 entp = &ent->next;
4334 }
4335 *entp = to->elf.plt.plist;
4336 }
4337
4338 to->elf.plt.plist = from->elf.plt.plist;
4339 from->elf.plt.plist = NULL;
4340 }
4341 }
4342
4343 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4344
4345 static void
4346 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4347 struct elf_link_hash_entry *dir,
4348 struct elf_link_hash_entry *ind)
4349 {
4350 struct ppc_link_hash_entry *edir, *eind;
4351
4352 edir = (struct ppc_link_hash_entry *) dir;
4353 eind = (struct ppc_link_hash_entry *) ind;
4354
4355 edir->is_func |= eind->is_func;
4356 edir->is_func_descriptor |= eind->is_func_descriptor;
4357 edir->tls_mask |= eind->tls_mask;
4358 if (eind->oh != NULL)
4359 edir->oh = ppc_follow_link (eind->oh);
4360
4361 /* If called to transfer flags for a weakdef during processing
4362 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4363 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4364 if (!(ELIMINATE_COPY_RELOCS
4365 && eind->elf.root.type != bfd_link_hash_indirect
4366 && edir->elf.dynamic_adjusted))
4367 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4368
4369 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4370 edir->elf.ref_regular |= eind->elf.ref_regular;
4371 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4372 edir->elf.needs_plt |= eind->elf.needs_plt;
4373
4374 /* If we were called to copy over info for a weak sym, that's all. */
4375 if (eind->elf.root.type != bfd_link_hash_indirect)
4376 return;
4377
4378 /* Copy over any dynamic relocs we may have on the indirect sym. */
4379 if (eind->dyn_relocs != NULL)
4380 {
4381 if (edir->dyn_relocs != NULL)
4382 {
4383 struct elf_dyn_relocs **pp;
4384 struct elf_dyn_relocs *p;
4385
4386 /* Add reloc counts against the indirect sym to the direct sym
4387 list. Merge any entries against the same section. */
4388 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4389 {
4390 struct elf_dyn_relocs *q;
4391
4392 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4393 if (q->sec == p->sec)
4394 {
4395 q->pc_count += p->pc_count;
4396 q->count += p->count;
4397 *pp = p->next;
4398 break;
4399 }
4400 if (q == NULL)
4401 pp = &p->next;
4402 }
4403 *pp = edir->dyn_relocs;
4404 }
4405
4406 edir->dyn_relocs = eind->dyn_relocs;
4407 eind->dyn_relocs = NULL;
4408 }
4409
4410 /* Copy over got entries that we may have already seen to the
4411 symbol which just became indirect. */
4412 if (eind->elf.got.glist != NULL)
4413 {
4414 if (edir->elf.got.glist != NULL)
4415 {
4416 struct got_entry **entp;
4417 struct got_entry *ent;
4418
4419 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4420 {
4421 struct got_entry *dent;
4422
4423 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4424 if (dent->addend == ent->addend
4425 && dent->owner == ent->owner
4426 && dent->tls_type == ent->tls_type)
4427 {
4428 dent->got.refcount += ent->got.refcount;
4429 *entp = ent->next;
4430 break;
4431 }
4432 if (dent == NULL)
4433 entp = &ent->next;
4434 }
4435 *entp = edir->elf.got.glist;
4436 }
4437
4438 edir->elf.got.glist = eind->elf.got.glist;
4439 eind->elf.got.glist = NULL;
4440 }
4441
4442 /* And plt entries. */
4443 move_plt_plist (eind, edir);
4444
4445 if (eind->elf.dynindx != -1)
4446 {
4447 if (edir->elf.dynindx != -1)
4448 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4449 edir->elf.dynstr_index);
4450 edir->elf.dynindx = eind->elf.dynindx;
4451 edir->elf.dynstr_index = eind->elf.dynstr_index;
4452 eind->elf.dynindx = -1;
4453 eind->elf.dynstr_index = 0;
4454 }
4455 }
4456
4457 /* Find the function descriptor hash entry from the given function code
4458 hash entry FH. Link the entries via their OH fields. */
4459
4460 static struct ppc_link_hash_entry *
4461 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4462 {
4463 struct ppc_link_hash_entry *fdh = fh->oh;
4464
4465 if (fdh == NULL)
4466 {
4467 const char *fd_name = fh->elf.root.root.string + 1;
4468
4469 fdh = (struct ppc_link_hash_entry *)
4470 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4471 if (fdh == NULL)
4472 return fdh;
4473
4474 fdh->is_func_descriptor = 1;
4475 fdh->oh = fh;
4476 fh->is_func = 1;
4477 fh->oh = fdh;
4478 }
4479
4480 return ppc_follow_link (fdh);
4481 }
4482
4483 /* Make a fake function descriptor sym for the code sym FH. */
4484
4485 static struct ppc_link_hash_entry *
4486 make_fdh (struct bfd_link_info *info,
4487 struct ppc_link_hash_entry *fh)
4488 {
4489 bfd *abfd;
4490 asymbol *newsym;
4491 struct bfd_link_hash_entry *bh;
4492 struct ppc_link_hash_entry *fdh;
4493
4494 abfd = fh->elf.root.u.undef.abfd;
4495 newsym = bfd_make_empty_symbol (abfd);
4496 newsym->name = fh->elf.root.root.string + 1;
4497 newsym->section = bfd_und_section_ptr;
4498 newsym->value = 0;
4499 newsym->flags = BSF_WEAK;
4500
4501 bh = NULL;
4502 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4503 newsym->flags, newsym->section,
4504 newsym->value, NULL, FALSE, FALSE,
4505 &bh))
4506 return NULL;
4507
4508 fdh = (struct ppc_link_hash_entry *) bh;
4509 fdh->elf.non_elf = 0;
4510 fdh->fake = 1;
4511 fdh->is_func_descriptor = 1;
4512 fdh->oh = fh;
4513 fh->is_func = 1;
4514 fh->oh = fdh;
4515 return fdh;
4516 }
4517
4518 /* Fix function descriptor symbols defined in .opd sections to be
4519 function type. */
4520
4521 static bfd_boolean
4522 ppc64_elf_add_symbol_hook (bfd *ibfd,
4523 struct bfd_link_info *info,
4524 Elf_Internal_Sym *isym,
4525 const char **name ATTRIBUTE_UNUSED,
4526 flagword *flags ATTRIBUTE_UNUSED,
4527 asection **sec,
4528 bfd_vma *value ATTRIBUTE_UNUSED)
4529 {
4530 if ((ibfd->flags & DYNAMIC) == 0
4531 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4532 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4533
4534 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4535 {
4536 if ((ibfd->flags & DYNAMIC) == 0)
4537 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4538 }
4539 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4540 ;
4541 else if (*sec != NULL
4542 && strcmp ((*sec)->name, ".opd") == 0)
4543 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4544
4545 return TRUE;
4546 }
4547
4548 /* This function makes an old ABI object reference to ".bar" cause the
4549 inclusion of a new ABI object archive that defines "bar".
4550 NAME is a symbol defined in an archive. Return a symbol in the hash
4551 table that might be satisfied by the archive symbols. */
4552
4553 static struct elf_link_hash_entry *
4554 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4555 struct bfd_link_info *info,
4556 const char *name)
4557 {
4558 struct elf_link_hash_entry *h;
4559 char *dot_name;
4560 size_t len;
4561
4562 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4563 if (h != NULL
4564 /* Don't return this sym if it is a fake function descriptor
4565 created by add_symbol_adjust. */
4566 && !(h->root.type == bfd_link_hash_undefweak
4567 && ((struct ppc_link_hash_entry *) h)->fake))
4568 return h;
4569
4570 if (name[0] == '.')
4571 return h;
4572
4573 len = strlen (name);
4574 dot_name = bfd_alloc (abfd, len + 2);
4575 if (dot_name == NULL)
4576 return (struct elf_link_hash_entry *) 0 - 1;
4577 dot_name[0] = '.';
4578 memcpy (dot_name + 1, name, len + 1);
4579 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4580 bfd_release (abfd, dot_name);
4581 return h;
4582 }
4583
4584 /* This function satisfies all old ABI object references to ".bar" if a
4585 new ABI object defines "bar". Well, at least, undefined dot symbols
4586 are made weak. This stops later archive searches from including an
4587 object if we already have a function descriptor definition. It also
4588 prevents the linker complaining about undefined symbols.
4589 We also check and correct mismatched symbol visibility here. The
4590 most restrictive visibility of the function descriptor and the
4591 function entry symbol is used. */
4592
4593 static bfd_boolean
4594 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4595 {
4596 struct ppc_link_hash_table *htab;
4597 struct ppc_link_hash_entry *fdh;
4598
4599 if (eh->elf.root.type == bfd_link_hash_indirect)
4600 return TRUE;
4601
4602 if (eh->elf.root.type == bfd_link_hash_warning)
4603 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4604
4605 if (eh->elf.root.root.string[0] != '.')
4606 abort ();
4607
4608 htab = ppc_hash_table (info);
4609 if (htab == NULL)
4610 return FALSE;
4611
4612 fdh = lookup_fdh (eh, htab);
4613 if (fdh == NULL)
4614 {
4615 if (!info->relocatable
4616 && (eh->elf.root.type == bfd_link_hash_undefined
4617 || eh->elf.root.type == bfd_link_hash_undefweak)
4618 && eh->elf.ref_regular)
4619 {
4620 /* Make an undefweak function descriptor sym, which is enough to
4621 pull in an --as-needed shared lib, but won't cause link
4622 errors. Archives are handled elsewhere. */
4623 fdh = make_fdh (info, eh);
4624 if (fdh == NULL)
4625 return FALSE;
4626 fdh->elf.ref_regular = 1;
4627 }
4628 }
4629 else
4630 {
4631 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4632 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4633 if (entry_vis < descr_vis)
4634 fdh->elf.other += entry_vis - descr_vis;
4635 else if (entry_vis > descr_vis)
4636 eh->elf.other += descr_vis - entry_vis;
4637
4638 if ((fdh->elf.root.type == bfd_link_hash_defined
4639 || fdh->elf.root.type == bfd_link_hash_defweak)
4640 && eh->elf.root.type == bfd_link_hash_undefined)
4641 {
4642 eh->elf.root.type = bfd_link_hash_undefweak;
4643 eh->was_undefined = 1;
4644 htab->twiddled_syms = 1;
4645 }
4646 }
4647
4648 return TRUE;
4649 }
4650
4651 /* Process list of dot-symbols we made in link_hash_newfunc. */
4652
4653 static bfd_boolean
4654 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4655 {
4656 struct ppc_link_hash_table *htab;
4657 struct ppc_link_hash_entry **p, *eh;
4658
4659 if (!is_ppc64_elf (info->output_bfd))
4660 return TRUE;
4661 htab = ppc_hash_table (info);
4662 if (htab == NULL)
4663 return FALSE;
4664
4665 if (is_ppc64_elf (ibfd))
4666 {
4667 p = &htab->dot_syms;
4668 while ((eh = *p) != NULL)
4669 {
4670 *p = NULL;
4671 if (!add_symbol_adjust (eh, info))
4672 return FALSE;
4673 p = &eh->u.next_dot_sym;
4674 }
4675 }
4676
4677 /* Clear the list for non-ppc64 input files. */
4678 p = &htab->dot_syms;
4679 while ((eh = *p) != NULL)
4680 {
4681 *p = NULL;
4682 p = &eh->u.next_dot_sym;
4683 }
4684
4685 /* We need to fix the undefs list for any syms we have twiddled to
4686 undef_weak. */
4687 if (htab->twiddled_syms)
4688 {
4689 bfd_link_repair_undef_list (&htab->elf.root);
4690 htab->twiddled_syms = 0;
4691 }
4692 return TRUE;
4693 }
4694
4695 /* Undo hash table changes when an --as-needed input file is determined
4696 not to be needed. */
4697
4698 static bfd_boolean
4699 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4700 struct bfd_link_info *info)
4701 {
4702 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4703
4704 if (htab == NULL)
4705 return FALSE;
4706
4707 htab->dot_syms = NULL;
4708 return TRUE;
4709 }
4710
4711 /* If --just-symbols against a final linked binary, then assume we need
4712 toc adjusting stubs when calling functions defined there. */
4713
4714 static void
4715 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4716 {
4717 if ((sec->flags & SEC_CODE) != 0
4718 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4719 && is_ppc64_elf (sec->owner))
4720 {
4721 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4722 if (got != NULL
4723 && got->size >= elf_backend_got_header_size
4724 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4725 sec->has_toc_reloc = 1;
4726 }
4727 _bfd_elf_link_just_syms (sec, info);
4728 }
4729
4730 static struct plt_entry **
4731 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4732 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4733 {
4734 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4735 struct plt_entry **local_plt;
4736 unsigned char *local_got_tls_masks;
4737
4738 if (local_got_ents == NULL)
4739 {
4740 bfd_size_type size = symtab_hdr->sh_info;
4741
4742 size *= (sizeof (*local_got_ents)
4743 + sizeof (*local_plt)
4744 + sizeof (*local_got_tls_masks));
4745 local_got_ents = bfd_zalloc (abfd, size);
4746 if (local_got_ents == NULL)
4747 return NULL;
4748 elf_local_got_ents (abfd) = local_got_ents;
4749 }
4750
4751 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4752 {
4753 struct got_entry *ent;
4754
4755 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4756 if (ent->addend == r_addend
4757 && ent->owner == abfd
4758 && ent->tls_type == tls_type)
4759 break;
4760 if (ent == NULL)
4761 {
4762 bfd_size_type amt = sizeof (*ent);
4763 ent = bfd_alloc (abfd, amt);
4764 if (ent == NULL)
4765 return FALSE;
4766 ent->next = local_got_ents[r_symndx];
4767 ent->addend = r_addend;
4768 ent->owner = abfd;
4769 ent->tls_type = tls_type;
4770 ent->is_indirect = FALSE;
4771 ent->got.refcount = 0;
4772 local_got_ents[r_symndx] = ent;
4773 }
4774 ent->got.refcount += 1;
4775 }
4776
4777 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4778 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4779 local_got_tls_masks[r_symndx] |= tls_type;
4780
4781 return local_plt + r_symndx;
4782 }
4783
4784 static bfd_boolean
4785 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4786 {
4787 struct plt_entry *ent;
4788
4789 for (ent = *plist; ent != NULL; ent = ent->next)
4790 if (ent->addend == addend)
4791 break;
4792 if (ent == NULL)
4793 {
4794 bfd_size_type amt = sizeof (*ent);
4795 ent = bfd_alloc (abfd, amt);
4796 if (ent == NULL)
4797 return FALSE;
4798 ent->next = *plist;
4799 ent->addend = addend;
4800 ent->plt.refcount = 0;
4801 *plist = ent;
4802 }
4803 ent->plt.refcount += 1;
4804 return TRUE;
4805 }
4806
4807 static bfd_boolean
4808 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4809 {
4810 return (r_type == R_PPC64_REL24
4811 || r_type == R_PPC64_REL14
4812 || r_type == R_PPC64_REL14_BRTAKEN
4813 || r_type == R_PPC64_REL14_BRNTAKEN
4814 || r_type == R_PPC64_ADDR24
4815 || r_type == R_PPC64_ADDR14
4816 || r_type == R_PPC64_ADDR14_BRTAKEN
4817 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4818 }
4819
4820 /* Look through the relocs for a section during the first phase, and
4821 calculate needed space in the global offset table, procedure
4822 linkage table, and dynamic reloc sections. */
4823
4824 static bfd_boolean
4825 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4826 asection *sec, const Elf_Internal_Rela *relocs)
4827 {
4828 struct ppc_link_hash_table *htab;
4829 Elf_Internal_Shdr *symtab_hdr;
4830 struct elf_link_hash_entry **sym_hashes;
4831 const Elf_Internal_Rela *rel;
4832 const Elf_Internal_Rela *rel_end;
4833 asection *sreloc;
4834 asection **opd_sym_map;
4835 struct elf_link_hash_entry *tga, *dottga;
4836
4837 if (info->relocatable)
4838 return TRUE;
4839
4840 /* Don't do anything special with non-loaded, non-alloced sections.
4841 In particular, any relocs in such sections should not affect GOT
4842 and PLT reference counting (ie. we don't allow them to create GOT
4843 or PLT entries), there's no possibility or desire to optimize TLS
4844 relocs, and there's not much point in propagating relocs to shared
4845 libs that the dynamic linker won't relocate. */
4846 if ((sec->flags & SEC_ALLOC) == 0)
4847 return TRUE;
4848
4849 BFD_ASSERT (is_ppc64_elf (abfd));
4850
4851 htab = ppc_hash_table (info);
4852 if (htab == NULL)
4853 return FALSE;
4854
4855 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4856 FALSE, FALSE, TRUE);
4857 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4858 FALSE, FALSE, TRUE);
4859 symtab_hdr = &elf_symtab_hdr (abfd);
4860 sym_hashes = elf_sym_hashes (abfd);
4861 sreloc = NULL;
4862 opd_sym_map = NULL;
4863 if (strcmp (sec->name, ".opd") == 0)
4864 {
4865 /* Garbage collection needs some extra help with .opd sections.
4866 We don't want to necessarily keep everything referenced by
4867 relocs in .opd, as that would keep all functions. Instead,
4868 if we reference an .opd symbol (a function descriptor), we
4869 want to keep the function code symbol's section. This is
4870 easy for global symbols, but for local syms we need to keep
4871 information about the associated function section. */
4872 bfd_size_type amt;
4873
4874 amt = sec->size * sizeof (*opd_sym_map) / 8;
4875 opd_sym_map = bfd_zalloc (abfd, amt);
4876 if (opd_sym_map == NULL)
4877 return FALSE;
4878 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4879 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4880 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4881 }
4882
4883 if (htab->sfpr == NULL
4884 && !create_linkage_sections (htab->elf.dynobj, info))
4885 return FALSE;
4886
4887 rel_end = relocs + sec->reloc_count;
4888 for (rel = relocs; rel < rel_end; rel++)
4889 {
4890 unsigned long r_symndx;
4891 struct elf_link_hash_entry *h;
4892 enum elf_ppc64_reloc_type r_type;
4893 int tls_type;
4894 struct _ppc64_elf_section_data *ppc64_sec;
4895 struct plt_entry **ifunc;
4896
4897 r_symndx = ELF64_R_SYM (rel->r_info);
4898 if (r_symndx < symtab_hdr->sh_info)
4899 h = NULL;
4900 else
4901 {
4902 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4903 h = elf_follow_link (h);
4904 }
4905
4906 tls_type = 0;
4907 ifunc = NULL;
4908 if (h != NULL)
4909 {
4910 if (h->type == STT_GNU_IFUNC)
4911 {
4912 h->needs_plt = 1;
4913 ifunc = &h->plt.plist;
4914 }
4915 }
4916 else
4917 {
4918 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4919 abfd, r_symndx);
4920 if (isym == NULL)
4921 return FALSE;
4922
4923 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4924 {
4925 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4926 rel->r_addend, PLT_IFUNC);
4927 if (ifunc == NULL)
4928 return FALSE;
4929 }
4930 }
4931 r_type = ELF64_R_TYPE (rel->r_info);
4932 if (is_branch_reloc (r_type))
4933 {
4934 if (h != NULL && (h == tga || h == dottga))
4935 {
4936 if (rel != relocs
4937 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4938 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4939 /* We have a new-style __tls_get_addr call with a marker
4940 reloc. */
4941 ;
4942 else
4943 /* Mark this section as having an old-style call. */
4944 sec->has_tls_get_addr_call = 1;
4945 }
4946
4947 /* STT_GNU_IFUNC symbols must have a PLT entry. */
4948 if (ifunc != NULL
4949 && !update_plt_info (abfd, ifunc, rel->r_addend))
4950 return FALSE;
4951 }
4952
4953 switch (r_type)
4954 {
4955 case R_PPC64_TLSGD:
4956 case R_PPC64_TLSLD:
4957 /* These special tls relocs tie a call to __tls_get_addr with
4958 its parameter symbol. */
4959 break;
4960
4961 case R_PPC64_GOT_TLSLD16:
4962 case R_PPC64_GOT_TLSLD16_LO:
4963 case R_PPC64_GOT_TLSLD16_HI:
4964 case R_PPC64_GOT_TLSLD16_HA:
4965 tls_type = TLS_TLS | TLS_LD;
4966 goto dogottls;
4967
4968 case R_PPC64_GOT_TLSGD16:
4969 case R_PPC64_GOT_TLSGD16_LO:
4970 case R_PPC64_GOT_TLSGD16_HI:
4971 case R_PPC64_GOT_TLSGD16_HA:
4972 tls_type = TLS_TLS | TLS_GD;
4973 goto dogottls;
4974
4975 case R_PPC64_GOT_TPREL16_DS:
4976 case R_PPC64_GOT_TPREL16_LO_DS:
4977 case R_PPC64_GOT_TPREL16_HI:
4978 case R_PPC64_GOT_TPREL16_HA:
4979 if (!info->executable)
4980 info->flags |= DF_STATIC_TLS;
4981 tls_type = TLS_TLS | TLS_TPREL;
4982 goto dogottls;
4983
4984 case R_PPC64_GOT_DTPREL16_DS:
4985 case R_PPC64_GOT_DTPREL16_LO_DS:
4986 case R_PPC64_GOT_DTPREL16_HI:
4987 case R_PPC64_GOT_DTPREL16_HA:
4988 tls_type = TLS_TLS | TLS_DTPREL;
4989 dogottls:
4990 sec->has_tls_reloc = 1;
4991 /* Fall thru */
4992
4993 case R_PPC64_GOT16:
4994 case R_PPC64_GOT16_DS:
4995 case R_PPC64_GOT16_HA:
4996 case R_PPC64_GOT16_HI:
4997 case R_PPC64_GOT16_LO:
4998 case R_PPC64_GOT16_LO_DS:
4999 /* This symbol requires a global offset table entry. */
5000 sec->has_toc_reloc = 1;
5001 if (r_type == R_PPC64_GOT_TLSLD16
5002 || r_type == R_PPC64_GOT_TLSGD16
5003 || r_type == R_PPC64_GOT_TPREL16_DS
5004 || r_type == R_PPC64_GOT_DTPREL16_DS
5005 || r_type == R_PPC64_GOT16
5006 || r_type == R_PPC64_GOT16_DS)
5007 {
5008 htab->do_multi_toc = 1;
5009 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5010 }
5011
5012 if (ppc64_elf_tdata (abfd)->got == NULL
5013 && !create_got_section (abfd, info))
5014 return FALSE;
5015
5016 if (h != NULL)
5017 {
5018 struct ppc_link_hash_entry *eh;
5019 struct got_entry *ent;
5020
5021 eh = (struct ppc_link_hash_entry *) h;
5022 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5023 if (ent->addend == rel->r_addend
5024 && ent->owner == abfd
5025 && ent->tls_type == tls_type)
5026 break;
5027 if (ent == NULL)
5028 {
5029 bfd_size_type amt = sizeof (*ent);
5030 ent = bfd_alloc (abfd, amt);
5031 if (ent == NULL)
5032 return FALSE;
5033 ent->next = eh->elf.got.glist;
5034 ent->addend = rel->r_addend;
5035 ent->owner = abfd;
5036 ent->tls_type = tls_type;
5037 ent->is_indirect = FALSE;
5038 ent->got.refcount = 0;
5039 eh->elf.got.glist = ent;
5040 }
5041 ent->got.refcount += 1;
5042 eh->tls_mask |= tls_type;
5043 }
5044 else
5045 /* This is a global offset table entry for a local symbol. */
5046 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5047 rel->r_addend, tls_type))
5048 return FALSE;
5049 break;
5050
5051 case R_PPC64_PLT16_HA:
5052 case R_PPC64_PLT16_HI:
5053 case R_PPC64_PLT16_LO:
5054 case R_PPC64_PLT32:
5055 case R_PPC64_PLT64:
5056 /* This symbol requires a procedure linkage table entry. We
5057 actually build the entry in adjust_dynamic_symbol,
5058 because this might be a case of linking PIC code without
5059 linking in any dynamic objects, in which case we don't
5060 need to generate a procedure linkage table after all. */
5061 if (h == NULL)
5062 {
5063 /* It does not make sense to have a procedure linkage
5064 table entry for a local symbol. */
5065 bfd_set_error (bfd_error_bad_value);
5066 return FALSE;
5067 }
5068 else
5069 {
5070 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5071 return FALSE;
5072 h->needs_plt = 1;
5073 if (h->root.root.string[0] == '.'
5074 && h->root.root.string[1] != '\0')
5075 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5076 }
5077 break;
5078
5079 /* The following relocations don't need to propagate the
5080 relocation if linking a shared object since they are
5081 section relative. */
5082 case R_PPC64_SECTOFF:
5083 case R_PPC64_SECTOFF_LO:
5084 case R_PPC64_SECTOFF_HI:
5085 case R_PPC64_SECTOFF_HA:
5086 case R_PPC64_SECTOFF_DS:
5087 case R_PPC64_SECTOFF_LO_DS:
5088 case R_PPC64_DTPREL16:
5089 case R_PPC64_DTPREL16_LO:
5090 case R_PPC64_DTPREL16_HI:
5091 case R_PPC64_DTPREL16_HA:
5092 case R_PPC64_DTPREL16_DS:
5093 case R_PPC64_DTPREL16_LO_DS:
5094 case R_PPC64_DTPREL16_HIGHER:
5095 case R_PPC64_DTPREL16_HIGHERA:
5096 case R_PPC64_DTPREL16_HIGHEST:
5097 case R_PPC64_DTPREL16_HIGHESTA:
5098 break;
5099
5100 /* Nor do these. */
5101 case R_PPC64_REL16:
5102 case R_PPC64_REL16_LO:
5103 case R_PPC64_REL16_HI:
5104 case R_PPC64_REL16_HA:
5105 break;
5106
5107 case R_PPC64_TOC16:
5108 case R_PPC64_TOC16_DS:
5109 htab->do_multi_toc = 1;
5110 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5111 case R_PPC64_TOC16_LO:
5112 case R_PPC64_TOC16_HI:
5113 case R_PPC64_TOC16_HA:
5114 case R_PPC64_TOC16_LO_DS:
5115 sec->has_toc_reloc = 1;
5116 break;
5117
5118 /* This relocation describes the C++ object vtable hierarchy.
5119 Reconstruct it for later use during GC. */
5120 case R_PPC64_GNU_VTINHERIT:
5121 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5122 return FALSE;
5123 break;
5124
5125 /* This relocation describes which C++ vtable entries are actually
5126 used. Record for later use during GC. */
5127 case R_PPC64_GNU_VTENTRY:
5128 BFD_ASSERT (h != NULL);
5129 if (h != NULL
5130 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5131 return FALSE;
5132 break;
5133
5134 case R_PPC64_REL14:
5135 case R_PPC64_REL14_BRTAKEN:
5136 case R_PPC64_REL14_BRNTAKEN:
5137 {
5138 asection *dest = NULL;
5139
5140 /* Heuristic: If jumping outside our section, chances are
5141 we are going to need a stub. */
5142 if (h != NULL)
5143 {
5144 /* If the sym is weak it may be overridden later, so
5145 don't assume we know where a weak sym lives. */
5146 if (h->root.type == bfd_link_hash_defined)
5147 dest = h->root.u.def.section;
5148 }
5149 else
5150 {
5151 Elf_Internal_Sym *isym;
5152
5153 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5154 abfd, r_symndx);
5155 if (isym == NULL)
5156 return FALSE;
5157
5158 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5159 }
5160
5161 if (dest != sec)
5162 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5163 }
5164 /* Fall through. */
5165
5166 case R_PPC64_REL24:
5167 if (h != NULL && ifunc == NULL)
5168 {
5169 /* We may need a .plt entry if the function this reloc
5170 refers to is in a shared lib. */
5171 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5172 return FALSE;
5173 h->needs_plt = 1;
5174 if (h->root.root.string[0] == '.'
5175 && h->root.root.string[1] != '\0')
5176 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5177 if (h == tga || h == dottga)
5178 sec->has_tls_reloc = 1;
5179 }
5180 break;
5181
5182 case R_PPC64_TPREL64:
5183 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5184 if (!info->executable)
5185 info->flags |= DF_STATIC_TLS;
5186 goto dotlstoc;
5187
5188 case R_PPC64_DTPMOD64:
5189 if (rel + 1 < rel_end
5190 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5191 && rel[1].r_offset == rel->r_offset + 8)
5192 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5193 else
5194 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5195 goto dotlstoc;
5196
5197 case R_PPC64_DTPREL64:
5198 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5199 if (rel != relocs
5200 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5201 && rel[-1].r_offset == rel->r_offset - 8)
5202 /* This is the second reloc of a dtpmod, dtprel pair.
5203 Don't mark with TLS_DTPREL. */
5204 goto dodyn;
5205
5206 dotlstoc:
5207 sec->has_tls_reloc = 1;
5208 if (h != NULL)
5209 {
5210 struct ppc_link_hash_entry *eh;
5211 eh = (struct ppc_link_hash_entry *) h;
5212 eh->tls_mask |= tls_type;
5213 }
5214 else
5215 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5216 rel->r_addend, tls_type))
5217 return FALSE;
5218
5219 ppc64_sec = ppc64_elf_section_data (sec);
5220 if (ppc64_sec->sec_type != sec_toc)
5221 {
5222 bfd_size_type amt;
5223
5224 /* One extra to simplify get_tls_mask. */
5225 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5226 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5227 if (ppc64_sec->u.toc.symndx == NULL)
5228 return FALSE;
5229 amt = sec->size * sizeof (bfd_vma) / 8;
5230 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5231 if (ppc64_sec->u.toc.add == NULL)
5232 return FALSE;
5233 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5234 ppc64_sec->sec_type = sec_toc;
5235 }
5236 BFD_ASSERT (rel->r_offset % 8 == 0);
5237 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5238 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5239
5240 /* Mark the second slot of a GD or LD entry.
5241 -1 to indicate GD and -2 to indicate LD. */
5242 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5243 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5244 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5245 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5246 goto dodyn;
5247
5248 case R_PPC64_TPREL16:
5249 case R_PPC64_TPREL16_LO:
5250 case R_PPC64_TPREL16_HI:
5251 case R_PPC64_TPREL16_HA:
5252 case R_PPC64_TPREL16_DS:
5253 case R_PPC64_TPREL16_LO_DS:
5254 case R_PPC64_TPREL16_HIGHER:
5255 case R_PPC64_TPREL16_HIGHERA:
5256 case R_PPC64_TPREL16_HIGHEST:
5257 case R_PPC64_TPREL16_HIGHESTA:
5258 if (info->shared)
5259 {
5260 if (!info->executable)
5261 info->flags |= DF_STATIC_TLS;
5262 goto dodyn;
5263 }
5264 break;
5265
5266 case R_PPC64_ADDR64:
5267 if (opd_sym_map != NULL
5268 && rel + 1 < rel_end
5269 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5270 {
5271 if (h != NULL)
5272 {
5273 if (h->root.root.string[0] == '.'
5274 && h->root.root.string[1] != 0
5275 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5276 ;
5277 else
5278 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5279 }
5280 else
5281 {
5282 asection *s;
5283 Elf_Internal_Sym *isym;
5284
5285 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5286 abfd, r_symndx);
5287 if (isym == NULL)
5288 return FALSE;
5289
5290 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5291 if (s != NULL && s != sec)
5292 opd_sym_map[rel->r_offset / 8] = s;
5293 }
5294 }
5295 /* Fall through. */
5296
5297 case R_PPC64_REL30:
5298 case R_PPC64_REL32:
5299 case R_PPC64_REL64:
5300 case R_PPC64_ADDR14:
5301 case R_PPC64_ADDR14_BRNTAKEN:
5302 case R_PPC64_ADDR14_BRTAKEN:
5303 case R_PPC64_ADDR16:
5304 case R_PPC64_ADDR16_DS:
5305 case R_PPC64_ADDR16_HA:
5306 case R_PPC64_ADDR16_HI:
5307 case R_PPC64_ADDR16_HIGHER:
5308 case R_PPC64_ADDR16_HIGHERA:
5309 case R_PPC64_ADDR16_HIGHEST:
5310 case R_PPC64_ADDR16_HIGHESTA:
5311 case R_PPC64_ADDR16_LO:
5312 case R_PPC64_ADDR16_LO_DS:
5313 case R_PPC64_ADDR24:
5314 case R_PPC64_ADDR32:
5315 case R_PPC64_UADDR16:
5316 case R_PPC64_UADDR32:
5317 case R_PPC64_UADDR64:
5318 case R_PPC64_TOC:
5319 if (h != NULL && !info->shared)
5320 /* We may need a copy reloc. */
5321 h->non_got_ref = 1;
5322
5323 /* Don't propagate .opd relocs. */
5324 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5325 break;
5326
5327 /* If we are creating a shared library, and this is a reloc
5328 against a global symbol, or a non PC relative reloc
5329 against a local symbol, then we need to copy the reloc
5330 into the shared library. However, if we are linking with
5331 -Bsymbolic, we do not need to copy a reloc against a
5332 global symbol which is defined in an object we are
5333 including in the link (i.e., DEF_REGULAR is set). At
5334 this point we have not seen all the input files, so it is
5335 possible that DEF_REGULAR is not set now but will be set
5336 later (it is never cleared). In case of a weak definition,
5337 DEF_REGULAR may be cleared later by a strong definition in
5338 a shared library. We account for that possibility below by
5339 storing information in the dyn_relocs field of the hash
5340 table entry. A similar situation occurs when creating
5341 shared libraries and symbol visibility changes render the
5342 symbol local.
5343
5344 If on the other hand, we are creating an executable, we
5345 may need to keep relocations for symbols satisfied by a
5346 dynamic library if we manage to avoid copy relocs for the
5347 symbol. */
5348 dodyn:
5349 if ((info->shared
5350 && (must_be_dyn_reloc (info, r_type)
5351 || (h != NULL
5352 && (! info->symbolic
5353 || h->root.type == bfd_link_hash_defweak
5354 || !h->def_regular))))
5355 || (ELIMINATE_COPY_RELOCS
5356 && !info->shared
5357 && h != NULL
5358 && (h->root.type == bfd_link_hash_defweak
5359 || !h->def_regular))
5360 || (!info->shared
5361 && ifunc != NULL))
5362 {
5363 struct elf_dyn_relocs *p;
5364 struct elf_dyn_relocs **head;
5365
5366 /* We must copy these reloc types into the output file.
5367 Create a reloc section in dynobj and make room for
5368 this reloc. */
5369 if (sreloc == NULL)
5370 {
5371 sreloc = _bfd_elf_make_dynamic_reloc_section
5372 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5373
5374 if (sreloc == NULL)
5375 return FALSE;
5376 }
5377
5378 /* If this is a global symbol, we count the number of
5379 relocations we need for this symbol. */
5380 if (h != NULL)
5381 {
5382 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5383 }
5384 else
5385 {
5386 /* Track dynamic relocs needed for local syms too.
5387 We really need local syms available to do this
5388 easily. Oh well. */
5389 asection *s;
5390 void *vpp;
5391 Elf_Internal_Sym *isym;
5392
5393 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5394 abfd, r_symndx);
5395 if (isym == NULL)
5396 return FALSE;
5397
5398 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5399 if (s == NULL)
5400 s = sec;
5401
5402 vpp = &elf_section_data (s)->local_dynrel;
5403 head = (struct elf_dyn_relocs **) vpp;
5404 }
5405
5406 p = *head;
5407 if (p == NULL || p->sec != sec)
5408 {
5409 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5410 if (p == NULL)
5411 return FALSE;
5412 p->next = *head;
5413 *head = p;
5414 p->sec = sec;
5415 p->count = 0;
5416 p->pc_count = 0;
5417 }
5418
5419 p->count += 1;
5420 if (!must_be_dyn_reloc (info, r_type))
5421 p->pc_count += 1;
5422 }
5423 break;
5424
5425 default:
5426 break;
5427 }
5428 }
5429
5430 return TRUE;
5431 }
5432
5433 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5434 of the code entry point, and its section. */
5435
5436 static bfd_vma
5437 opd_entry_value (asection *opd_sec,
5438 bfd_vma offset,
5439 asection **code_sec,
5440 bfd_vma *code_off)
5441 {
5442 bfd *opd_bfd = opd_sec->owner;
5443 Elf_Internal_Rela *relocs;
5444 Elf_Internal_Rela *lo, *hi, *look;
5445 bfd_vma val;
5446
5447 /* No relocs implies we are linking a --just-symbols object. */
5448 if (opd_sec->reloc_count == 0)
5449 {
5450 char buf[8];
5451
5452 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5453 return (bfd_vma) -1;
5454
5455 val = bfd_get_64 (opd_bfd, buf);
5456 if (code_sec != NULL)
5457 {
5458 asection *sec, *likely = NULL;
5459 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5460 if (sec->vma <= val
5461 && (sec->flags & SEC_LOAD) != 0
5462 && (sec->flags & SEC_ALLOC) != 0)
5463 likely = sec;
5464 if (likely != NULL)
5465 {
5466 *code_sec = likely;
5467 if (code_off != NULL)
5468 *code_off = val - likely->vma;
5469 }
5470 }
5471 return val;
5472 }
5473
5474 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5475
5476 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5477 if (relocs == NULL)
5478 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5479
5480 /* Go find the opd reloc at the sym address. */
5481 lo = relocs;
5482 BFD_ASSERT (lo != NULL);
5483 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5484 val = (bfd_vma) -1;
5485 while (lo < hi)
5486 {
5487 look = lo + (hi - lo) / 2;
5488 if (look->r_offset < offset)
5489 lo = look + 1;
5490 else if (look->r_offset > offset)
5491 hi = look;
5492 else
5493 {
5494 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5495
5496 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5497 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5498 {
5499 unsigned long symndx = ELF64_R_SYM (look->r_info);
5500 asection *sec;
5501
5502 if (symndx < symtab_hdr->sh_info)
5503 {
5504 Elf_Internal_Sym *sym;
5505
5506 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5507 if (sym == NULL)
5508 {
5509 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5510 symtab_hdr->sh_info,
5511 0, NULL, NULL, NULL);
5512 if (sym == NULL)
5513 break;
5514 symtab_hdr->contents = (bfd_byte *) sym;
5515 }
5516
5517 sym += symndx;
5518 val = sym->st_value;
5519 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5520 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5521 }
5522 else
5523 {
5524 struct elf_link_hash_entry **sym_hashes;
5525 struct elf_link_hash_entry *rh;
5526
5527 sym_hashes = elf_sym_hashes (opd_bfd);
5528 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5529 rh = elf_follow_link (rh);
5530 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5531 || rh->root.type == bfd_link_hash_defweak);
5532 val = rh->root.u.def.value;
5533 sec = rh->root.u.def.section;
5534 }
5535 val += look->r_addend;
5536 if (code_off != NULL)
5537 *code_off = val;
5538 if (code_sec != NULL)
5539 *code_sec = sec;
5540 if (sec != NULL && sec->output_section != NULL)
5541 val += sec->output_section->vma + sec->output_offset;
5542 }
5543 break;
5544 }
5545 }
5546
5547 return val;
5548 }
5549
5550 /* Return true if symbol is defined in a regular object file. */
5551
5552 static bfd_boolean
5553 is_static_defined (struct elf_link_hash_entry *h)
5554 {
5555 return ((h->root.type == bfd_link_hash_defined
5556 || h->root.type == bfd_link_hash_defweak)
5557 && h->root.u.def.section != NULL
5558 && h->root.u.def.section->output_section != NULL);
5559 }
5560
5561 /* If FDH is a function descriptor symbol, return the associated code
5562 entry symbol if it is defined. Return NULL otherwise. */
5563
5564 static struct ppc_link_hash_entry *
5565 defined_code_entry (struct ppc_link_hash_entry *fdh)
5566 {
5567 if (fdh->is_func_descriptor)
5568 {
5569 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5570 if (fh->elf.root.type == bfd_link_hash_defined
5571 || fh->elf.root.type == bfd_link_hash_defweak)
5572 return fh;
5573 }
5574 return NULL;
5575 }
5576
5577 /* If FH is a function code entry symbol, return the associated
5578 function descriptor symbol if it is defined. Return NULL otherwise. */
5579
5580 static struct ppc_link_hash_entry *
5581 defined_func_desc (struct ppc_link_hash_entry *fh)
5582 {
5583 if (fh->oh != NULL
5584 && fh->oh->is_func_descriptor)
5585 {
5586 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5587 if (fdh->elf.root.type == bfd_link_hash_defined
5588 || fdh->elf.root.type == bfd_link_hash_defweak)
5589 return fdh;
5590 }
5591 return NULL;
5592 }
5593
5594 /* Mark all our entry sym sections, both opd and code section. */
5595
5596 static void
5597 ppc64_elf_gc_keep (struct bfd_link_info *info)
5598 {
5599 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5600 struct bfd_sym_chain *sym;
5601
5602 if (htab == NULL)
5603 return;
5604
5605 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5606 {
5607 struct ppc_link_hash_entry *eh, *fh;
5608 asection *sec;
5609
5610 eh = (struct ppc_link_hash_entry *)
5611 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5612 if (eh == NULL)
5613 continue;
5614 if (eh->elf.root.type != bfd_link_hash_defined
5615 && eh->elf.root.type != bfd_link_hash_defweak)
5616 continue;
5617
5618 fh = defined_code_entry (eh);
5619 if (fh != NULL)
5620 {
5621 sec = fh->elf.root.u.def.section;
5622 sec->flags |= SEC_KEEP;
5623 }
5624 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5625 && opd_entry_value (eh->elf.root.u.def.section,
5626 eh->elf.root.u.def.value,
5627 &sec, NULL) != (bfd_vma) -1)
5628 sec->flags |= SEC_KEEP;
5629
5630 sec = eh->elf.root.u.def.section;
5631 sec->flags |= SEC_KEEP;
5632 }
5633 }
5634
5635 /* Mark sections containing dynamically referenced symbols. When
5636 building shared libraries, we must assume that any visible symbol is
5637 referenced. */
5638
5639 static bfd_boolean
5640 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5641 {
5642 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5643 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5644 struct ppc_link_hash_entry *fdh;
5645
5646 /* Dynamic linking info is on the func descriptor sym. */
5647 fdh = defined_func_desc (eh);
5648 if (fdh != NULL)
5649 eh = fdh;
5650
5651 if ((eh->elf.root.type == bfd_link_hash_defined
5652 || eh->elf.root.type == bfd_link_hash_defweak)
5653 && (eh->elf.ref_dynamic
5654 || (!info->executable
5655 && eh->elf.def_regular
5656 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5657 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5658 {
5659 asection *code_sec;
5660 struct ppc_link_hash_entry *fh;
5661
5662 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5663
5664 /* Function descriptor syms cause the associated
5665 function code sym section to be marked. */
5666 fh = defined_code_entry (eh);
5667 if (fh != NULL)
5668 {
5669 code_sec = fh->elf.root.u.def.section;
5670 code_sec->flags |= SEC_KEEP;
5671 }
5672 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5673 && opd_entry_value (eh->elf.root.u.def.section,
5674 eh->elf.root.u.def.value,
5675 &code_sec, NULL) != (bfd_vma) -1)
5676 code_sec->flags |= SEC_KEEP;
5677 }
5678
5679 return TRUE;
5680 }
5681
5682 /* Return the section that should be marked against GC for a given
5683 relocation. */
5684
5685 static asection *
5686 ppc64_elf_gc_mark_hook (asection *sec,
5687 struct bfd_link_info *info,
5688 Elf_Internal_Rela *rel,
5689 struct elf_link_hash_entry *h,
5690 Elf_Internal_Sym *sym)
5691 {
5692 asection *rsec;
5693
5694 /* Syms return NULL if we're marking .opd, so we avoid marking all
5695 function sections, as all functions are referenced in .opd. */
5696 rsec = NULL;
5697 if (get_opd_info (sec) != NULL)
5698 return rsec;
5699
5700 if (h != NULL)
5701 {
5702 enum elf_ppc64_reloc_type r_type;
5703 struct ppc_link_hash_entry *eh, *fh, *fdh;
5704
5705 r_type = ELF64_R_TYPE (rel->r_info);
5706 switch (r_type)
5707 {
5708 case R_PPC64_GNU_VTINHERIT:
5709 case R_PPC64_GNU_VTENTRY:
5710 break;
5711
5712 default:
5713 switch (h->root.type)
5714 {
5715 case bfd_link_hash_defined:
5716 case bfd_link_hash_defweak:
5717 eh = (struct ppc_link_hash_entry *) h;
5718 fdh = defined_func_desc (eh);
5719 if (fdh != NULL)
5720 eh = fdh;
5721
5722 /* Function descriptor syms cause the associated
5723 function code sym section to be marked. */
5724 fh = defined_code_entry (eh);
5725 if (fh != NULL)
5726 {
5727 /* They also mark their opd section. */
5728 eh->elf.root.u.def.section->gc_mark = 1;
5729
5730 rsec = fh->elf.root.u.def.section;
5731 }
5732 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5733 && opd_entry_value (eh->elf.root.u.def.section,
5734 eh->elf.root.u.def.value,
5735 &rsec, NULL) != (bfd_vma) -1)
5736 eh->elf.root.u.def.section->gc_mark = 1;
5737 else
5738 rsec = h->root.u.def.section;
5739 break;
5740
5741 case bfd_link_hash_common:
5742 rsec = h->root.u.c.p->section;
5743 break;
5744
5745 default:
5746 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5747 }
5748 }
5749 }
5750 else
5751 {
5752 struct _opd_sec_data *opd;
5753
5754 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5755 opd = get_opd_info (rsec);
5756 if (opd != NULL && opd->func_sec != NULL)
5757 {
5758 rsec->gc_mark = 1;
5759
5760 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5761 }
5762 }
5763
5764 return rsec;
5765 }
5766
5767 /* Update the .got, .plt. and dynamic reloc reference counts for the
5768 section being removed. */
5769
5770 static bfd_boolean
5771 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5772 asection *sec, const Elf_Internal_Rela *relocs)
5773 {
5774 struct ppc_link_hash_table *htab;
5775 Elf_Internal_Shdr *symtab_hdr;
5776 struct elf_link_hash_entry **sym_hashes;
5777 struct got_entry **local_got_ents;
5778 const Elf_Internal_Rela *rel, *relend;
5779
5780 if (info->relocatable)
5781 return TRUE;
5782
5783 if ((sec->flags & SEC_ALLOC) == 0)
5784 return TRUE;
5785
5786 elf_section_data (sec)->local_dynrel = NULL;
5787
5788 htab = ppc_hash_table (info);
5789 if (htab == NULL)
5790 return FALSE;
5791
5792 symtab_hdr = &elf_symtab_hdr (abfd);
5793 sym_hashes = elf_sym_hashes (abfd);
5794 local_got_ents = elf_local_got_ents (abfd);
5795
5796 relend = relocs + sec->reloc_count;
5797 for (rel = relocs; rel < relend; rel++)
5798 {
5799 unsigned long r_symndx;
5800 enum elf_ppc64_reloc_type r_type;
5801 struct elf_link_hash_entry *h = NULL;
5802 unsigned char tls_type = 0;
5803
5804 r_symndx = ELF64_R_SYM (rel->r_info);
5805 r_type = ELF64_R_TYPE (rel->r_info);
5806 if (r_symndx >= symtab_hdr->sh_info)
5807 {
5808 struct ppc_link_hash_entry *eh;
5809 struct elf_dyn_relocs **pp;
5810 struct elf_dyn_relocs *p;
5811
5812 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5813 h = elf_follow_link (h);
5814 eh = (struct ppc_link_hash_entry *) h;
5815
5816 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5817 if (p->sec == sec)
5818 {
5819 /* Everything must go for SEC. */
5820 *pp = p->next;
5821 break;
5822 }
5823 }
5824
5825 if (is_branch_reloc (r_type))
5826 {
5827 struct plt_entry **ifunc = NULL;
5828 if (h != NULL)
5829 {
5830 if (h->type == STT_GNU_IFUNC)
5831 ifunc = &h->plt.plist;
5832 }
5833 else if (local_got_ents != NULL)
5834 {
5835 struct plt_entry **local_plt = (struct plt_entry **)
5836 (local_got_ents + symtab_hdr->sh_info);
5837 unsigned char *local_got_tls_masks = (unsigned char *)
5838 (local_plt + symtab_hdr->sh_info);
5839 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5840 ifunc = local_plt + r_symndx;
5841 }
5842 if (ifunc != NULL)
5843 {
5844 struct plt_entry *ent;
5845
5846 for (ent = *ifunc; ent != NULL; ent = ent->next)
5847 if (ent->addend == rel->r_addend)
5848 break;
5849 if (ent == NULL)
5850 abort ();
5851 if (ent->plt.refcount > 0)
5852 ent->plt.refcount -= 1;
5853 continue;
5854 }
5855 }
5856
5857 switch (r_type)
5858 {
5859 case R_PPC64_GOT_TLSLD16:
5860 case R_PPC64_GOT_TLSLD16_LO:
5861 case R_PPC64_GOT_TLSLD16_HI:
5862 case R_PPC64_GOT_TLSLD16_HA:
5863 tls_type = TLS_TLS | TLS_LD;
5864 goto dogot;
5865
5866 case R_PPC64_GOT_TLSGD16:
5867 case R_PPC64_GOT_TLSGD16_LO:
5868 case R_PPC64_GOT_TLSGD16_HI:
5869 case R_PPC64_GOT_TLSGD16_HA:
5870 tls_type = TLS_TLS | TLS_GD;
5871 goto dogot;
5872
5873 case R_PPC64_GOT_TPREL16_DS:
5874 case R_PPC64_GOT_TPREL16_LO_DS:
5875 case R_PPC64_GOT_TPREL16_HI:
5876 case R_PPC64_GOT_TPREL16_HA:
5877 tls_type = TLS_TLS | TLS_TPREL;
5878 goto dogot;
5879
5880 case R_PPC64_GOT_DTPREL16_DS:
5881 case R_PPC64_GOT_DTPREL16_LO_DS:
5882 case R_PPC64_GOT_DTPREL16_HI:
5883 case R_PPC64_GOT_DTPREL16_HA:
5884 tls_type = TLS_TLS | TLS_DTPREL;
5885 goto dogot;
5886
5887 case R_PPC64_GOT16:
5888 case R_PPC64_GOT16_DS:
5889 case R_PPC64_GOT16_HA:
5890 case R_PPC64_GOT16_HI:
5891 case R_PPC64_GOT16_LO:
5892 case R_PPC64_GOT16_LO_DS:
5893 dogot:
5894 {
5895 struct got_entry *ent;
5896
5897 if (h != NULL)
5898 ent = h->got.glist;
5899 else
5900 ent = local_got_ents[r_symndx];
5901
5902 for (; ent != NULL; ent = ent->next)
5903 if (ent->addend == rel->r_addend
5904 && ent->owner == abfd
5905 && ent->tls_type == tls_type)
5906 break;
5907 if (ent == NULL)
5908 abort ();
5909 if (ent->got.refcount > 0)
5910 ent->got.refcount -= 1;
5911 }
5912 break;
5913
5914 case R_PPC64_PLT16_HA:
5915 case R_PPC64_PLT16_HI:
5916 case R_PPC64_PLT16_LO:
5917 case R_PPC64_PLT32:
5918 case R_PPC64_PLT64:
5919 case R_PPC64_REL14:
5920 case R_PPC64_REL14_BRNTAKEN:
5921 case R_PPC64_REL14_BRTAKEN:
5922 case R_PPC64_REL24:
5923 if (h != NULL)
5924 {
5925 struct plt_entry *ent;
5926
5927 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5928 if (ent->addend == rel->r_addend)
5929 break;
5930 if (ent != NULL && ent->plt.refcount > 0)
5931 ent->plt.refcount -= 1;
5932 }
5933 break;
5934
5935 default:
5936 break;
5937 }
5938 }
5939 return TRUE;
5940 }
5941
5942 /* The maximum size of .sfpr. */
5943 #define SFPR_MAX (218*4)
5944
5945 struct sfpr_def_parms
5946 {
5947 const char name[12];
5948 unsigned char lo, hi;
5949 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5950 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5951 };
5952
5953 /* Auto-generate _save*, _rest* functions in .sfpr. */
5954
5955 static bfd_boolean
5956 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5957 {
5958 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5959 unsigned int i;
5960 size_t len = strlen (parm->name);
5961 bfd_boolean writing = FALSE;
5962 char sym[16];
5963
5964 if (htab == NULL)
5965 return FALSE;
5966
5967 memcpy (sym, parm->name, len);
5968 sym[len + 2] = 0;
5969
5970 for (i = parm->lo; i <= parm->hi; i++)
5971 {
5972 struct elf_link_hash_entry *h;
5973
5974 sym[len + 0] = i / 10 + '0';
5975 sym[len + 1] = i % 10 + '0';
5976 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5977 if (h != NULL
5978 && !h->def_regular)
5979 {
5980 h->root.type = bfd_link_hash_defined;
5981 h->root.u.def.section = htab->sfpr;
5982 h->root.u.def.value = htab->sfpr->size;
5983 h->type = STT_FUNC;
5984 h->def_regular = 1;
5985 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5986 writing = TRUE;
5987 if (htab->sfpr->contents == NULL)
5988 {
5989 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5990 if (htab->sfpr->contents == NULL)
5991 return FALSE;
5992 }
5993 }
5994 if (writing)
5995 {
5996 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5997 if (i != parm->hi)
5998 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5999 else
6000 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6001 htab->sfpr->size = p - htab->sfpr->contents;
6002 }
6003 }
6004
6005 return TRUE;
6006 }
6007
6008 static bfd_byte *
6009 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6010 {
6011 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6012 return p + 4;
6013 }
6014
6015 static bfd_byte *
6016 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6017 {
6018 p = savegpr0 (abfd, p, r);
6019 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6020 p = p + 4;
6021 bfd_put_32 (abfd, BLR, p);
6022 return p + 4;
6023 }
6024
6025 static bfd_byte *
6026 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6027 {
6028 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6029 return p + 4;
6030 }
6031
6032 static bfd_byte *
6033 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6034 {
6035 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6036 p = p + 4;
6037 p = restgpr0 (abfd, p, r);
6038 bfd_put_32 (abfd, MTLR_R0, p);
6039 p = p + 4;
6040 if (r == 29)
6041 {
6042 p = restgpr0 (abfd, p, 30);
6043 p = restgpr0 (abfd, p, 31);
6044 }
6045 bfd_put_32 (abfd, BLR, p);
6046 return p + 4;
6047 }
6048
6049 static bfd_byte *
6050 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6051 {
6052 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6053 return p + 4;
6054 }
6055
6056 static bfd_byte *
6057 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6058 {
6059 p = savegpr1 (abfd, p, r);
6060 bfd_put_32 (abfd, BLR, p);
6061 return p + 4;
6062 }
6063
6064 static bfd_byte *
6065 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6066 {
6067 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6068 return p + 4;
6069 }
6070
6071 static bfd_byte *
6072 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6073 {
6074 p = restgpr1 (abfd, p, r);
6075 bfd_put_32 (abfd, BLR, p);
6076 return p + 4;
6077 }
6078
6079 static bfd_byte *
6080 savefpr (bfd *abfd, bfd_byte *p, int r)
6081 {
6082 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6083 return p + 4;
6084 }
6085
6086 static bfd_byte *
6087 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6088 {
6089 p = savefpr (abfd, p, r);
6090 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6091 p = p + 4;
6092 bfd_put_32 (abfd, BLR, p);
6093 return p + 4;
6094 }
6095
6096 static bfd_byte *
6097 restfpr (bfd *abfd, bfd_byte *p, int r)
6098 {
6099 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6100 return p + 4;
6101 }
6102
6103 static bfd_byte *
6104 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6105 {
6106 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6107 p = p + 4;
6108 p = restfpr (abfd, p, r);
6109 bfd_put_32 (abfd, MTLR_R0, p);
6110 p = p + 4;
6111 if (r == 29)
6112 {
6113 p = restfpr (abfd, p, 30);
6114 p = restfpr (abfd, p, 31);
6115 }
6116 bfd_put_32 (abfd, BLR, p);
6117 return p + 4;
6118 }
6119
6120 static bfd_byte *
6121 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6122 {
6123 p = savefpr (abfd, p, r);
6124 bfd_put_32 (abfd, BLR, p);
6125 return p + 4;
6126 }
6127
6128 static bfd_byte *
6129 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6130 {
6131 p = restfpr (abfd, p, r);
6132 bfd_put_32 (abfd, BLR, p);
6133 return p + 4;
6134 }
6135
6136 static bfd_byte *
6137 savevr (bfd *abfd, bfd_byte *p, int r)
6138 {
6139 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6140 p = p + 4;
6141 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6142 return p + 4;
6143 }
6144
6145 static bfd_byte *
6146 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6147 {
6148 p = savevr (abfd, p, r);
6149 bfd_put_32 (abfd, BLR, p);
6150 return p + 4;
6151 }
6152
6153 static bfd_byte *
6154 restvr (bfd *abfd, bfd_byte *p, int r)
6155 {
6156 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6157 p = p + 4;
6158 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6159 return p + 4;
6160 }
6161
6162 static bfd_byte *
6163 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6164 {
6165 p = restvr (abfd, p, r);
6166 bfd_put_32 (abfd, BLR, p);
6167 return p + 4;
6168 }
6169
6170 /* Called via elf_link_hash_traverse to transfer dynamic linking
6171 information on function code symbol entries to their corresponding
6172 function descriptor symbol entries. */
6173
6174 static bfd_boolean
6175 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6176 {
6177 struct bfd_link_info *info;
6178 struct ppc_link_hash_table *htab;
6179 struct plt_entry *ent;
6180 struct ppc_link_hash_entry *fh;
6181 struct ppc_link_hash_entry *fdh;
6182 bfd_boolean force_local;
6183
6184 fh = (struct ppc_link_hash_entry *) h;
6185 if (fh->elf.root.type == bfd_link_hash_indirect)
6186 return TRUE;
6187
6188 info = inf;
6189 htab = ppc_hash_table (info);
6190 if (htab == NULL)
6191 return FALSE;
6192
6193 /* Resolve undefined references to dot-symbols as the value
6194 in the function descriptor, if we have one in a regular object.
6195 This is to satisfy cases like ".quad .foo". Calls to functions
6196 in dynamic objects are handled elsewhere. */
6197 if (fh->elf.root.type == bfd_link_hash_undefweak
6198 && fh->was_undefined
6199 && (fdh = defined_func_desc (fh)) != NULL
6200 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6201 && opd_entry_value (fdh->elf.root.u.def.section,
6202 fdh->elf.root.u.def.value,
6203 &fh->elf.root.u.def.section,
6204 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6205 {
6206 fh->elf.root.type = fdh->elf.root.type;
6207 fh->elf.forced_local = 1;
6208 fh->elf.def_regular = fdh->elf.def_regular;
6209 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6210 }
6211
6212 /* If this is a function code symbol, transfer dynamic linking
6213 information to the function descriptor symbol. */
6214 if (!fh->is_func)
6215 return TRUE;
6216
6217 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6218 if (ent->plt.refcount > 0)
6219 break;
6220 if (ent == NULL
6221 || fh->elf.root.root.string[0] != '.'
6222 || fh->elf.root.root.string[1] == '\0')
6223 return TRUE;
6224
6225 /* Find the corresponding function descriptor symbol. Create it
6226 as undefined if necessary. */
6227
6228 fdh = lookup_fdh (fh, htab);
6229 if (fdh == NULL
6230 && !info->executable
6231 && (fh->elf.root.type == bfd_link_hash_undefined
6232 || fh->elf.root.type == bfd_link_hash_undefweak))
6233 {
6234 fdh = make_fdh (info, fh);
6235 if (fdh == NULL)
6236 return FALSE;
6237 }
6238
6239 /* Fake function descriptors are made undefweak. If the function
6240 code symbol is strong undefined, make the fake sym the same.
6241 If the function code symbol is defined, then force the fake
6242 descriptor local; We can't support overriding of symbols in a
6243 shared library on a fake descriptor. */
6244
6245 if (fdh != NULL
6246 && fdh->fake
6247 && fdh->elf.root.type == bfd_link_hash_undefweak)
6248 {
6249 if (fh->elf.root.type == bfd_link_hash_undefined)
6250 {
6251 fdh->elf.root.type = bfd_link_hash_undefined;
6252 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6253 }
6254 else if (fh->elf.root.type == bfd_link_hash_defined
6255 || fh->elf.root.type == bfd_link_hash_defweak)
6256 {
6257 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6258 }
6259 }
6260
6261 if (fdh != NULL
6262 && !fdh->elf.forced_local
6263 && (!info->executable
6264 || fdh->elf.def_dynamic
6265 || fdh->elf.ref_dynamic
6266 || (fdh->elf.root.type == bfd_link_hash_undefweak
6267 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6268 {
6269 if (fdh->elf.dynindx == -1)
6270 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6271 return FALSE;
6272 fdh->elf.ref_regular |= fh->elf.ref_regular;
6273 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6274 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6275 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6276 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6277 {
6278 move_plt_plist (fh, fdh);
6279 fdh->elf.needs_plt = 1;
6280 }
6281 fdh->is_func_descriptor = 1;
6282 fdh->oh = fh;
6283 fh->oh = fdh;
6284 }
6285
6286 /* Now that the info is on the function descriptor, clear the
6287 function code sym info. Any function code syms for which we
6288 don't have a definition in a regular file, we force local.
6289 This prevents a shared library from exporting syms that have
6290 been imported from another library. Function code syms that
6291 are really in the library we must leave global to prevent the
6292 linker dragging in a definition from a static library. */
6293 force_local = (!fh->elf.def_regular
6294 || fdh == NULL
6295 || !fdh->elf.def_regular
6296 || fdh->elf.forced_local);
6297 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6298
6299 return TRUE;
6300 }
6301
6302 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6303 this hook to a) provide some gcc support functions, and b) transfer
6304 dynamic linking information gathered so far on function code symbol
6305 entries, to their corresponding function descriptor symbol entries. */
6306
6307 static bfd_boolean
6308 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6309 struct bfd_link_info *info)
6310 {
6311 struct ppc_link_hash_table *htab;
6312 unsigned int i;
6313 const struct sfpr_def_parms funcs[] =
6314 {
6315 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6316 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6317 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6318 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6319 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6320 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6321 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6322 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6323 { "._savef", 14, 31, savefpr, savefpr1_tail },
6324 { "._restf", 14, 31, restfpr, restfpr1_tail },
6325 { "_savevr_", 20, 31, savevr, savevr_tail },
6326 { "_restvr_", 20, 31, restvr, restvr_tail }
6327 };
6328
6329 htab = ppc_hash_table (info);
6330 if (htab == NULL)
6331 return FALSE;
6332
6333 if (htab->sfpr == NULL)
6334 /* We don't have any relocs. */
6335 return TRUE;
6336
6337 /* Provide any missing _save* and _rest* functions. */
6338 htab->sfpr->size = 0;
6339 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6340 if (!sfpr_define (info, &funcs[i]))
6341 return FALSE;
6342
6343 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6344
6345 if (htab->sfpr->size == 0)
6346 htab->sfpr->flags |= SEC_EXCLUDE;
6347
6348 return TRUE;
6349 }
6350
6351 /* Adjust a symbol defined by a dynamic object and referenced by a
6352 regular object. The current definition is in some section of the
6353 dynamic object, but we're not including those sections. We have to
6354 change the definition to something the rest of the link can
6355 understand. */
6356
6357 static bfd_boolean
6358 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6359 struct elf_link_hash_entry *h)
6360 {
6361 struct ppc_link_hash_table *htab;
6362 asection *s;
6363
6364 htab = ppc_hash_table (info);
6365 if (htab == NULL)
6366 return FALSE;
6367
6368 /* Deal with function syms. */
6369 if (h->type == STT_FUNC
6370 || h->type == STT_GNU_IFUNC
6371 || h->needs_plt)
6372 {
6373 /* Clear procedure linkage table information for any symbol that
6374 won't need a .plt entry. */
6375 struct plt_entry *ent;
6376 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6377 if (ent->plt.refcount > 0)
6378 break;
6379 if (ent == NULL
6380 || (h->type != STT_GNU_IFUNC
6381 && (SYMBOL_CALLS_LOCAL (info, h)
6382 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6383 && h->root.type == bfd_link_hash_undefweak))))
6384 {
6385 h->plt.plist = NULL;
6386 h->needs_plt = 0;
6387 }
6388 }
6389 else
6390 h->plt.plist = NULL;
6391
6392 /* If this is a weak symbol, and there is a real definition, the
6393 processor independent code will have arranged for us to see the
6394 real definition first, and we can just use the same value. */
6395 if (h->u.weakdef != NULL)
6396 {
6397 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6398 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6399 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6400 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6401 if (ELIMINATE_COPY_RELOCS)
6402 h->non_got_ref = h->u.weakdef->non_got_ref;
6403 return TRUE;
6404 }
6405
6406 /* If we are creating a shared library, we must presume that the
6407 only references to the symbol are via the global offset table.
6408 For such cases we need not do anything here; the relocations will
6409 be handled correctly by relocate_section. */
6410 if (info->shared)
6411 return TRUE;
6412
6413 /* If there are no references to this symbol that do not use the
6414 GOT, we don't need to generate a copy reloc. */
6415 if (!h->non_got_ref)
6416 return TRUE;
6417
6418 /* Don't generate a copy reloc for symbols defined in the executable. */
6419 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6420 return TRUE;
6421
6422 if (ELIMINATE_COPY_RELOCS)
6423 {
6424 struct ppc_link_hash_entry * eh;
6425 struct elf_dyn_relocs *p;
6426
6427 eh = (struct ppc_link_hash_entry *) h;
6428 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6429 {
6430 s = p->sec->output_section;
6431 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6432 break;
6433 }
6434
6435 /* If we didn't find any dynamic relocs in read-only sections, then
6436 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6437 if (p == NULL)
6438 {
6439 h->non_got_ref = 0;
6440 return TRUE;
6441 }
6442 }
6443
6444 if (h->plt.plist != NULL)
6445 {
6446 /* We should never get here, but unfortunately there are versions
6447 of gcc out there that improperly (for this ABI) put initialized
6448 function pointers, vtable refs and suchlike in read-only
6449 sections. Allow them to proceed, but warn that this might
6450 break at runtime. */
6451 info->callbacks->einfo
6452 (_("copy reloc against `%s' requires lazy plt linking; "
6453 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6454 h->root.root.string);
6455 }
6456
6457 /* This is a reference to a symbol defined by a dynamic object which
6458 is not a function. */
6459
6460 if (h->size == 0)
6461 {
6462 info->callbacks->einfo (_("dynamic variable `%s' is zero size\n"),
6463 h->root.root.string);
6464 return TRUE;
6465 }
6466
6467 /* We must allocate the symbol in our .dynbss section, which will
6468 become part of the .bss section of the executable. There will be
6469 an entry for this symbol in the .dynsym section. The dynamic
6470 object will contain position independent code, so all references
6471 from the dynamic object to this symbol will go through the global
6472 offset table. The dynamic linker will use the .dynsym entry to
6473 determine the address it must put in the global offset table, so
6474 both the dynamic object and the regular object will refer to the
6475 same memory location for the variable. */
6476
6477 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6478 to copy the initial value out of the dynamic object and into the
6479 runtime process image. We need to remember the offset into the
6480 .rela.bss section we are going to use. */
6481 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6482 {
6483 htab->relbss->size += sizeof (Elf64_External_Rela);
6484 h->needs_copy = 1;
6485 }
6486
6487 s = htab->dynbss;
6488
6489 return _bfd_elf_adjust_dynamic_copy (h, s);
6490 }
6491
6492 /* If given a function descriptor symbol, hide both the function code
6493 sym and the descriptor. */
6494 static void
6495 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6496 struct elf_link_hash_entry *h,
6497 bfd_boolean force_local)
6498 {
6499 struct ppc_link_hash_entry *eh;
6500 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6501
6502 eh = (struct ppc_link_hash_entry *) h;
6503 if (eh->is_func_descriptor)
6504 {
6505 struct ppc_link_hash_entry *fh = eh->oh;
6506
6507 if (fh == NULL)
6508 {
6509 const char *p, *q;
6510 struct ppc_link_hash_table *htab;
6511 char save;
6512
6513 /* We aren't supposed to use alloca in BFD because on
6514 systems which do not have alloca the version in libiberty
6515 calls xmalloc, which might cause the program to crash
6516 when it runs out of memory. This function doesn't have a
6517 return status, so there's no way to gracefully return an
6518 error. So cheat. We know that string[-1] can be safely
6519 accessed; It's either a string in an ELF string table,
6520 or allocated in an objalloc structure. */
6521
6522 p = eh->elf.root.root.string - 1;
6523 save = *p;
6524 *(char *) p = '.';
6525 htab = ppc_hash_table (info);
6526 if (htab == NULL)
6527 return;
6528
6529 fh = (struct ppc_link_hash_entry *)
6530 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6531 *(char *) p = save;
6532
6533 /* Unfortunately, if it so happens that the string we were
6534 looking for was allocated immediately before this string,
6535 then we overwrote the string terminator. That's the only
6536 reason the lookup should fail. */
6537 if (fh == NULL)
6538 {
6539 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6540 while (q >= eh->elf.root.root.string && *q == *p)
6541 --q, --p;
6542 if (q < eh->elf.root.root.string && *p == '.')
6543 fh = (struct ppc_link_hash_entry *)
6544 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6545 }
6546 if (fh != NULL)
6547 {
6548 eh->oh = fh;
6549 fh->oh = eh;
6550 }
6551 }
6552 if (fh != NULL)
6553 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6554 }
6555 }
6556
6557 static bfd_boolean
6558 get_sym_h (struct elf_link_hash_entry **hp,
6559 Elf_Internal_Sym **symp,
6560 asection **symsecp,
6561 unsigned char **tls_maskp,
6562 Elf_Internal_Sym **locsymsp,
6563 unsigned long r_symndx,
6564 bfd *ibfd)
6565 {
6566 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6567
6568 if (r_symndx >= symtab_hdr->sh_info)
6569 {
6570 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6571 struct elf_link_hash_entry *h;
6572
6573 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6574 h = elf_follow_link (h);
6575
6576 if (hp != NULL)
6577 *hp = h;
6578
6579 if (symp != NULL)
6580 *symp = NULL;
6581
6582 if (symsecp != NULL)
6583 {
6584 asection *symsec = NULL;
6585 if (h->root.type == bfd_link_hash_defined
6586 || h->root.type == bfd_link_hash_defweak)
6587 symsec = h->root.u.def.section;
6588 *symsecp = symsec;
6589 }
6590
6591 if (tls_maskp != NULL)
6592 {
6593 struct ppc_link_hash_entry *eh;
6594
6595 eh = (struct ppc_link_hash_entry *) h;
6596 *tls_maskp = &eh->tls_mask;
6597 }
6598 }
6599 else
6600 {
6601 Elf_Internal_Sym *sym;
6602 Elf_Internal_Sym *locsyms = *locsymsp;
6603
6604 if (locsyms == NULL)
6605 {
6606 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6607 if (locsyms == NULL)
6608 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6609 symtab_hdr->sh_info,
6610 0, NULL, NULL, NULL);
6611 if (locsyms == NULL)
6612 return FALSE;
6613 *locsymsp = locsyms;
6614 }
6615 sym = locsyms + r_symndx;
6616
6617 if (hp != NULL)
6618 *hp = NULL;
6619
6620 if (symp != NULL)
6621 *symp = sym;
6622
6623 if (symsecp != NULL)
6624 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6625
6626 if (tls_maskp != NULL)
6627 {
6628 struct got_entry **lgot_ents;
6629 unsigned char *tls_mask;
6630
6631 tls_mask = NULL;
6632 lgot_ents = elf_local_got_ents (ibfd);
6633 if (lgot_ents != NULL)
6634 {
6635 struct plt_entry **local_plt = (struct plt_entry **)
6636 (lgot_ents + symtab_hdr->sh_info);
6637 unsigned char *lgot_masks = (unsigned char *)
6638 (local_plt + symtab_hdr->sh_info);
6639 tls_mask = &lgot_masks[r_symndx];
6640 }
6641 *tls_maskp = tls_mask;
6642 }
6643 }
6644 return TRUE;
6645 }
6646
6647 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6648 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6649 type suitable for optimization, and 1 otherwise. */
6650
6651 static int
6652 get_tls_mask (unsigned char **tls_maskp,
6653 unsigned long *toc_symndx,
6654 bfd_vma *toc_addend,
6655 Elf_Internal_Sym **locsymsp,
6656 const Elf_Internal_Rela *rel,
6657 bfd *ibfd)
6658 {
6659 unsigned long r_symndx;
6660 int next_r;
6661 struct elf_link_hash_entry *h;
6662 Elf_Internal_Sym *sym;
6663 asection *sec;
6664 bfd_vma off;
6665
6666 r_symndx = ELF64_R_SYM (rel->r_info);
6667 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6668 return 0;
6669
6670 if ((*tls_maskp != NULL && **tls_maskp != 0)
6671 || sec == NULL
6672 || ppc64_elf_section_data (sec) == NULL
6673 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6674 return 1;
6675
6676 /* Look inside a TOC section too. */
6677 if (h != NULL)
6678 {
6679 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6680 off = h->root.u.def.value;
6681 }
6682 else
6683 off = sym->st_value;
6684 off += rel->r_addend;
6685 BFD_ASSERT (off % 8 == 0);
6686 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6687 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6688 if (toc_symndx != NULL)
6689 *toc_symndx = r_symndx;
6690 if (toc_addend != NULL)
6691 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6692 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6693 return 0;
6694 if ((h == NULL || is_static_defined (h))
6695 && (next_r == -1 || next_r == -2))
6696 return 1 - next_r;
6697 return 1;
6698 }
6699
6700 /* Adjust all global syms defined in opd sections. In gcc generated
6701 code for the old ABI, these will already have been done. */
6702
6703 static bfd_boolean
6704 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6705 {
6706 struct ppc_link_hash_entry *eh;
6707 asection *sym_sec;
6708 struct _opd_sec_data *opd;
6709
6710 if (h->root.type == bfd_link_hash_indirect)
6711 return TRUE;
6712
6713 if (h->root.type != bfd_link_hash_defined
6714 && h->root.type != bfd_link_hash_defweak)
6715 return TRUE;
6716
6717 eh = (struct ppc_link_hash_entry *) h;
6718 if (eh->adjust_done)
6719 return TRUE;
6720
6721 sym_sec = eh->elf.root.u.def.section;
6722 opd = get_opd_info (sym_sec);
6723 if (opd != NULL && opd->adjust != NULL)
6724 {
6725 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6726 if (adjust == -1)
6727 {
6728 /* This entry has been deleted. */
6729 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6730 if (dsec == NULL)
6731 {
6732 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6733 if (elf_discarded_section (dsec))
6734 {
6735 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6736 break;
6737 }
6738 }
6739 eh->elf.root.u.def.value = 0;
6740 eh->elf.root.u.def.section = dsec;
6741 }
6742 else
6743 eh->elf.root.u.def.value += adjust;
6744 eh->adjust_done = 1;
6745 }
6746 return TRUE;
6747 }
6748
6749 /* Handles decrementing dynamic reloc counts for the reloc specified by
6750 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6751 have already been determined. */
6752
6753 static bfd_boolean
6754 dec_dynrel_count (bfd_vma r_info,
6755 asection *sec,
6756 struct bfd_link_info *info,
6757 Elf_Internal_Sym **local_syms,
6758 struct elf_link_hash_entry *h,
6759 asection *sym_sec)
6760 {
6761 enum elf_ppc64_reloc_type r_type;
6762 struct elf_dyn_relocs *p;
6763 struct elf_dyn_relocs **pp;
6764
6765 /* Can this reloc be dynamic? This switch, and later tests here
6766 should be kept in sync with the code in check_relocs. */
6767 r_type = ELF64_R_TYPE (r_info);
6768 switch (r_type)
6769 {
6770 default:
6771 return TRUE;
6772
6773 case R_PPC64_TPREL16:
6774 case R_PPC64_TPREL16_LO:
6775 case R_PPC64_TPREL16_HI:
6776 case R_PPC64_TPREL16_HA:
6777 case R_PPC64_TPREL16_DS:
6778 case R_PPC64_TPREL16_LO_DS:
6779 case R_PPC64_TPREL16_HIGHER:
6780 case R_PPC64_TPREL16_HIGHERA:
6781 case R_PPC64_TPREL16_HIGHEST:
6782 case R_PPC64_TPREL16_HIGHESTA:
6783 if (!info->shared)
6784 return TRUE;
6785
6786 case R_PPC64_TPREL64:
6787 case R_PPC64_DTPMOD64:
6788 case R_PPC64_DTPREL64:
6789 case R_PPC64_ADDR64:
6790 case R_PPC64_REL30:
6791 case R_PPC64_REL32:
6792 case R_PPC64_REL64:
6793 case R_PPC64_ADDR14:
6794 case R_PPC64_ADDR14_BRNTAKEN:
6795 case R_PPC64_ADDR14_BRTAKEN:
6796 case R_PPC64_ADDR16:
6797 case R_PPC64_ADDR16_DS:
6798 case R_PPC64_ADDR16_HA:
6799 case R_PPC64_ADDR16_HI:
6800 case R_PPC64_ADDR16_HIGHER:
6801 case R_PPC64_ADDR16_HIGHERA:
6802 case R_PPC64_ADDR16_HIGHEST:
6803 case R_PPC64_ADDR16_HIGHESTA:
6804 case R_PPC64_ADDR16_LO:
6805 case R_PPC64_ADDR16_LO_DS:
6806 case R_PPC64_ADDR24:
6807 case R_PPC64_ADDR32:
6808 case R_PPC64_UADDR16:
6809 case R_PPC64_UADDR32:
6810 case R_PPC64_UADDR64:
6811 case R_PPC64_TOC:
6812 break;
6813 }
6814
6815 if (local_syms != NULL)
6816 {
6817 unsigned long r_symndx;
6818 Elf_Internal_Sym *sym;
6819 bfd *ibfd = sec->owner;
6820
6821 r_symndx = ELF64_R_SYM (r_info);
6822 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6823 return FALSE;
6824 }
6825
6826 if ((info->shared
6827 && (must_be_dyn_reloc (info, r_type)
6828 || (h != NULL
6829 && (!info->symbolic
6830 || h->root.type == bfd_link_hash_defweak
6831 || !h->def_regular))))
6832 || (ELIMINATE_COPY_RELOCS
6833 && !info->shared
6834 && h != NULL
6835 && (h->root.type == bfd_link_hash_defweak
6836 || !h->def_regular)))
6837 ;
6838 else
6839 return TRUE;
6840
6841 if (h != NULL)
6842 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6843 else
6844 {
6845 if (sym_sec != NULL)
6846 {
6847 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6848 pp = (struct elf_dyn_relocs **) vpp;
6849 }
6850 else
6851 {
6852 void *vpp = &elf_section_data (sec)->local_dynrel;
6853 pp = (struct elf_dyn_relocs **) vpp;
6854 }
6855
6856 /* elf_gc_sweep may have already removed all dyn relocs associated
6857 with local syms for a given section. Don't report a dynreloc
6858 miscount. */
6859 if (*pp == NULL)
6860 return TRUE;
6861 }
6862
6863 while ((p = *pp) != NULL)
6864 {
6865 if (p->sec == sec)
6866 {
6867 if (!must_be_dyn_reloc (info, r_type))
6868 p->pc_count -= 1;
6869 p->count -= 1;
6870 if (p->count == 0)
6871 *pp = p->next;
6872 return TRUE;
6873 }
6874 pp = &p->next;
6875 }
6876
6877 info->callbacks->einfo (_("dynreloc miscount for %B, section %A\n"),
6878 sec->owner, sec);
6879 bfd_set_error (bfd_error_bad_value);
6880 return FALSE;
6881 }
6882
6883 /* Remove unused Official Procedure Descriptor entries. Currently we
6884 only remove those associated with functions in discarded link-once
6885 sections, or weakly defined functions that have been overridden. It
6886 would be possible to remove many more entries for statically linked
6887 applications. */
6888
6889 bfd_boolean
6890 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
6891 {
6892 bfd *ibfd;
6893 bfd_boolean some_edited = FALSE;
6894 asection *need_pad = NULL;
6895
6896 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6897 {
6898 asection *sec;
6899 Elf_Internal_Rela *relstart, *rel, *relend;
6900 Elf_Internal_Shdr *symtab_hdr;
6901 Elf_Internal_Sym *local_syms;
6902 bfd_vma offset;
6903 struct _opd_sec_data *opd;
6904 bfd_boolean need_edit, add_aux_fields;
6905 bfd_size_type cnt_16b = 0;
6906
6907 if (!is_ppc64_elf (ibfd))
6908 continue;
6909
6910 sec = bfd_get_section_by_name (ibfd, ".opd");
6911 if (sec == NULL || sec->size == 0)
6912 continue;
6913
6914 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6915 continue;
6916
6917 if (sec->output_section == bfd_abs_section_ptr)
6918 continue;
6919
6920 /* Look through the section relocs. */
6921 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6922 continue;
6923
6924 local_syms = NULL;
6925 symtab_hdr = &elf_symtab_hdr (ibfd);
6926
6927 /* Read the relocations. */
6928 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6929 info->keep_memory);
6930 if (relstart == NULL)
6931 return FALSE;
6932
6933 /* First run through the relocs to check they are sane, and to
6934 determine whether we need to edit this opd section. */
6935 need_edit = FALSE;
6936 need_pad = sec;
6937 offset = 0;
6938 relend = relstart + sec->reloc_count;
6939 for (rel = relstart; rel < relend; )
6940 {
6941 enum elf_ppc64_reloc_type r_type;
6942 unsigned long r_symndx;
6943 asection *sym_sec;
6944 struct elf_link_hash_entry *h;
6945 Elf_Internal_Sym *sym;
6946
6947 /* .opd contains a regular array of 16 or 24 byte entries. We're
6948 only interested in the reloc pointing to a function entry
6949 point. */
6950 if (rel->r_offset != offset
6951 || rel + 1 >= relend
6952 || (rel + 1)->r_offset != offset + 8)
6953 {
6954 /* If someone messes with .opd alignment then after a
6955 "ld -r" we might have padding in the middle of .opd.
6956 Also, there's nothing to prevent someone putting
6957 something silly in .opd with the assembler. No .opd
6958 optimization for them! */
6959 broken_opd:
6960 (*_bfd_error_handler)
6961 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6962 need_edit = FALSE;
6963 break;
6964 }
6965
6966 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6967 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6968 {
6969 (*_bfd_error_handler)
6970 (_("%B: unexpected reloc type %u in .opd section"),
6971 ibfd, r_type);
6972 need_edit = FALSE;
6973 break;
6974 }
6975
6976 r_symndx = ELF64_R_SYM (rel->r_info);
6977 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6978 r_symndx, ibfd))
6979 goto error_ret;
6980
6981 if (sym_sec == NULL || sym_sec->owner == NULL)
6982 {
6983 const char *sym_name;
6984 if (h != NULL)
6985 sym_name = h->root.root.string;
6986 else
6987 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6988 sym_sec);
6989
6990 (*_bfd_error_handler)
6991 (_("%B: undefined sym `%s' in .opd section"),
6992 ibfd, sym_name);
6993 need_edit = FALSE;
6994 break;
6995 }
6996
6997 /* opd entries are always for functions defined in the
6998 current input bfd. If the symbol isn't defined in the
6999 input bfd, then we won't be using the function in this
7000 bfd; It must be defined in a linkonce section in another
7001 bfd, or is weak. It's also possible that we are
7002 discarding the function due to a linker script /DISCARD/,
7003 which we test for via the output_section. */
7004 if (sym_sec->owner != ibfd
7005 || sym_sec->output_section == bfd_abs_section_ptr)
7006 need_edit = TRUE;
7007
7008 rel += 2;
7009 if (rel == relend
7010 || (rel + 1 == relend && rel->r_offset == offset + 16))
7011 {
7012 if (sec->size == offset + 24)
7013 {
7014 need_pad = NULL;
7015 break;
7016 }
7017 if (rel == relend && sec->size == offset + 16)
7018 {
7019 cnt_16b++;
7020 break;
7021 }
7022 goto broken_opd;
7023 }
7024
7025 if (rel->r_offset == offset + 24)
7026 offset += 24;
7027 else if (rel->r_offset != offset + 16)
7028 goto broken_opd;
7029 else if (rel + 1 < relend
7030 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7031 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7032 {
7033 offset += 16;
7034 cnt_16b++;
7035 }
7036 else if (rel + 2 < relend
7037 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7038 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7039 {
7040 offset += 24;
7041 rel += 1;
7042 }
7043 else
7044 goto broken_opd;
7045 }
7046
7047 add_aux_fields = non_overlapping && cnt_16b > 0;
7048
7049 if (need_edit || add_aux_fields)
7050 {
7051 Elf_Internal_Rela *write_rel;
7052 Elf_Internal_Shdr *rel_hdr;
7053 bfd_byte *rptr, *wptr;
7054 bfd_byte *new_contents;
7055 bfd_boolean skip;
7056 long opd_ent_size;
7057 bfd_size_type amt;
7058
7059 new_contents = NULL;
7060 amt = sec->size * sizeof (long) / 8;
7061 opd = &ppc64_elf_section_data (sec)->u.opd;
7062 opd->adjust = bfd_zalloc (sec->owner, amt);
7063 if (opd->adjust == NULL)
7064 return FALSE;
7065 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7066
7067 /* This seems a waste of time as input .opd sections are all
7068 zeros as generated by gcc, but I suppose there's no reason
7069 this will always be so. We might start putting something in
7070 the third word of .opd entries. */
7071 if ((sec->flags & SEC_IN_MEMORY) == 0)
7072 {
7073 bfd_byte *loc;
7074 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7075 {
7076 if (loc != NULL)
7077 free (loc);
7078 error_ret:
7079 if (local_syms != NULL
7080 && symtab_hdr->contents != (unsigned char *) local_syms)
7081 free (local_syms);
7082 if (elf_section_data (sec)->relocs != relstart)
7083 free (relstart);
7084 return FALSE;
7085 }
7086 sec->contents = loc;
7087 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7088 }
7089
7090 elf_section_data (sec)->relocs = relstart;
7091
7092 new_contents = sec->contents;
7093 if (add_aux_fields)
7094 {
7095 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7096 if (new_contents == NULL)
7097 return FALSE;
7098 need_pad = FALSE;
7099 }
7100 wptr = new_contents;
7101 rptr = sec->contents;
7102
7103 write_rel = relstart;
7104 skip = FALSE;
7105 offset = 0;
7106 opd_ent_size = 0;
7107 for (rel = relstart; rel < relend; rel++)
7108 {
7109 unsigned long r_symndx;
7110 asection *sym_sec;
7111 struct elf_link_hash_entry *h;
7112 Elf_Internal_Sym *sym;
7113
7114 r_symndx = ELF64_R_SYM (rel->r_info);
7115 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7116 r_symndx, ibfd))
7117 goto error_ret;
7118
7119 if (rel->r_offset == offset)
7120 {
7121 struct ppc_link_hash_entry *fdh = NULL;
7122
7123 /* See if the .opd entry is full 24 byte or
7124 16 byte (with fd_aux entry overlapped with next
7125 fd_func). */
7126 opd_ent_size = 24;
7127 if ((rel + 2 == relend && sec->size == offset + 16)
7128 || (rel + 3 < relend
7129 && rel[2].r_offset == offset + 16
7130 && rel[3].r_offset == offset + 24
7131 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7132 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7133 opd_ent_size = 16;
7134
7135 if (h != NULL
7136 && h->root.root.string[0] == '.')
7137 {
7138 struct ppc_link_hash_table *htab;
7139
7140 htab = ppc_hash_table (info);
7141 if (htab != NULL)
7142 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7143 htab);
7144 if (fdh != NULL
7145 && fdh->elf.root.type != bfd_link_hash_defined
7146 && fdh->elf.root.type != bfd_link_hash_defweak)
7147 fdh = NULL;
7148 }
7149
7150 skip = (sym_sec->owner != ibfd
7151 || sym_sec->output_section == bfd_abs_section_ptr);
7152 if (skip)
7153 {
7154 if (fdh != NULL && sym_sec->owner == ibfd)
7155 {
7156 /* Arrange for the function descriptor sym
7157 to be dropped. */
7158 fdh->elf.root.u.def.value = 0;
7159 fdh->elf.root.u.def.section = sym_sec;
7160 }
7161 opd->adjust[rel->r_offset / 8] = -1;
7162 }
7163 else
7164 {
7165 /* We'll be keeping this opd entry. */
7166
7167 if (fdh != NULL)
7168 {
7169 /* Redefine the function descriptor symbol to
7170 this location in the opd section. It is
7171 necessary to update the value here rather
7172 than using an array of adjustments as we do
7173 for local symbols, because various places
7174 in the generic ELF code use the value
7175 stored in u.def.value. */
7176 fdh->elf.root.u.def.value = wptr - new_contents;
7177 fdh->adjust_done = 1;
7178 }
7179
7180 /* Local syms are a bit tricky. We could
7181 tweak them as they can be cached, but
7182 we'd need to look through the local syms
7183 for the function descriptor sym which we
7184 don't have at the moment. So keep an
7185 array of adjustments. */
7186 opd->adjust[rel->r_offset / 8]
7187 = (wptr - new_contents) - (rptr - sec->contents);
7188
7189 if (wptr != rptr)
7190 memcpy (wptr, rptr, opd_ent_size);
7191 wptr += opd_ent_size;
7192 if (add_aux_fields && opd_ent_size == 16)
7193 {
7194 memset (wptr, '\0', 8);
7195 wptr += 8;
7196 }
7197 }
7198 rptr += opd_ent_size;
7199 offset += opd_ent_size;
7200 }
7201
7202 if (skip)
7203 {
7204 if (!NO_OPD_RELOCS
7205 && !info->relocatable
7206 && !dec_dynrel_count (rel->r_info, sec, info,
7207 NULL, h, sym_sec))
7208 goto error_ret;
7209 }
7210 else
7211 {
7212 /* We need to adjust any reloc offsets to point to the
7213 new opd entries. While we're at it, we may as well
7214 remove redundant relocs. */
7215 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7216 if (write_rel != rel)
7217 memcpy (write_rel, rel, sizeof (*rel));
7218 ++write_rel;
7219 }
7220 }
7221
7222 sec->size = wptr - new_contents;
7223 sec->reloc_count = write_rel - relstart;
7224 if (add_aux_fields)
7225 {
7226 free (sec->contents);
7227 sec->contents = new_contents;
7228 }
7229
7230 /* Fudge the header size too, as this is used later in
7231 elf_bfd_final_link if we are emitting relocs. */
7232 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7233 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7234 some_edited = TRUE;
7235 }
7236 else if (elf_section_data (sec)->relocs != relstart)
7237 free (relstart);
7238
7239 if (local_syms != NULL
7240 && symtab_hdr->contents != (unsigned char *) local_syms)
7241 {
7242 if (!info->keep_memory)
7243 free (local_syms);
7244 else
7245 symtab_hdr->contents = (unsigned char *) local_syms;
7246 }
7247 }
7248
7249 if (some_edited)
7250 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7251
7252 /* If we are doing a final link and the last .opd entry is just 16 byte
7253 long, add a 8 byte padding after it. */
7254 if (need_pad != NULL && !info->relocatable)
7255 {
7256 bfd_byte *p;
7257
7258 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7259 {
7260 BFD_ASSERT (need_pad->size > 0);
7261
7262 p = bfd_malloc (need_pad->size + 8);
7263 if (p == NULL)
7264 return FALSE;
7265
7266 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7267 p, 0, need_pad->size))
7268 return FALSE;
7269
7270 need_pad->contents = p;
7271 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7272 }
7273 else
7274 {
7275 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7276 if (p == NULL)
7277 return FALSE;
7278
7279 need_pad->contents = p;
7280 }
7281
7282 memset (need_pad->contents + need_pad->size, 0, 8);
7283 need_pad->size += 8;
7284 }
7285
7286 return TRUE;
7287 }
7288
7289 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7290
7291 asection *
7292 ppc64_elf_tls_setup (struct bfd_link_info *info,
7293 int no_tls_get_addr_opt,
7294 int *no_multi_toc)
7295 {
7296 struct ppc_link_hash_table *htab;
7297
7298 htab = ppc_hash_table (info);
7299 if (htab == NULL)
7300 return NULL;
7301
7302 if (*no_multi_toc)
7303 htab->do_multi_toc = 0;
7304 else if (!htab->do_multi_toc)
7305 *no_multi_toc = 1;
7306
7307 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7308 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7309 FALSE, FALSE, TRUE));
7310 /* Move dynamic linking info to the function descriptor sym. */
7311 if (htab->tls_get_addr != NULL)
7312 func_desc_adjust (&htab->tls_get_addr->elf, info);
7313 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7314 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7315 FALSE, FALSE, TRUE));
7316 if (!no_tls_get_addr_opt)
7317 {
7318 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7319
7320 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7321 FALSE, FALSE, TRUE);
7322 if (opt != NULL)
7323 func_desc_adjust (opt, info);
7324 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7325 FALSE, FALSE, TRUE);
7326 if (opt_fd != NULL
7327 && (opt_fd->root.type == bfd_link_hash_defined
7328 || opt_fd->root.type == bfd_link_hash_defweak))
7329 {
7330 /* If glibc supports an optimized __tls_get_addr call stub,
7331 signalled by the presence of __tls_get_addr_opt, and we'll
7332 be calling __tls_get_addr via a plt call stub, then
7333 make __tls_get_addr point to __tls_get_addr_opt. */
7334 tga_fd = &htab->tls_get_addr_fd->elf;
7335 if (htab->elf.dynamic_sections_created
7336 && tga_fd != NULL
7337 && (tga_fd->type == STT_FUNC
7338 || tga_fd->needs_plt)
7339 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7340 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7341 && tga_fd->root.type == bfd_link_hash_undefweak)))
7342 {
7343 struct plt_entry *ent;
7344
7345 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7346 if (ent->plt.refcount > 0)
7347 break;
7348 if (ent != NULL)
7349 {
7350 tga_fd->root.type = bfd_link_hash_indirect;
7351 tga_fd->root.u.i.link = &opt_fd->root;
7352 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7353 if (opt_fd->dynindx != -1)
7354 {
7355 /* Use __tls_get_addr_opt in dynamic relocations. */
7356 opt_fd->dynindx = -1;
7357 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7358 opt_fd->dynstr_index);
7359 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7360 return NULL;
7361 }
7362 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7363 tga = &htab->tls_get_addr->elf;
7364 if (opt != NULL && tga != NULL)
7365 {
7366 tga->root.type = bfd_link_hash_indirect;
7367 tga->root.u.i.link = &opt->root;
7368 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7369 _bfd_elf_link_hash_hide_symbol (info, opt,
7370 tga->forced_local);
7371 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7372 }
7373 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7374 htab->tls_get_addr_fd->is_func_descriptor = 1;
7375 if (htab->tls_get_addr != NULL)
7376 {
7377 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7378 htab->tls_get_addr->is_func = 1;
7379 }
7380 }
7381 }
7382 }
7383 else
7384 no_tls_get_addr_opt = TRUE;
7385 }
7386 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7387 return _bfd_elf_tls_setup (info->output_bfd, info);
7388 }
7389
7390 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7391 HASH1 or HASH2. */
7392
7393 static bfd_boolean
7394 branch_reloc_hash_match (const bfd *ibfd,
7395 const Elf_Internal_Rela *rel,
7396 const struct ppc_link_hash_entry *hash1,
7397 const struct ppc_link_hash_entry *hash2)
7398 {
7399 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7400 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7401 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7402
7403 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7404 {
7405 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7406 struct elf_link_hash_entry *h;
7407
7408 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7409 h = elf_follow_link (h);
7410 if (h == &hash1->elf || h == &hash2->elf)
7411 return TRUE;
7412 }
7413 return FALSE;
7414 }
7415
7416 /* Run through all the TLS relocs looking for optimization
7417 opportunities. The linker has been hacked (see ppc64elf.em) to do
7418 a preliminary section layout so that we know the TLS segment
7419 offsets. We can't optimize earlier because some optimizations need
7420 to know the tp offset, and we need to optimize before allocating
7421 dynamic relocations. */
7422
7423 bfd_boolean
7424 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7425 {
7426 bfd *ibfd;
7427 asection *sec;
7428 struct ppc_link_hash_table *htab;
7429 unsigned char *toc_ref;
7430 int pass;
7431
7432 if (info->relocatable || !info->executable)
7433 return TRUE;
7434
7435 htab = ppc_hash_table (info);
7436 if (htab == NULL)
7437 return FALSE;
7438
7439 /* Make two passes over the relocs. On the first pass, mark toc
7440 entries involved with tls relocs, and check that tls relocs
7441 involved in setting up a tls_get_addr call are indeed followed by
7442 such a call. If they are not, we can't do any tls optimization.
7443 On the second pass twiddle tls_mask flags to notify
7444 relocate_section that optimization can be done, and adjust got
7445 and plt refcounts. */
7446 toc_ref = NULL;
7447 for (pass = 0; pass < 2; ++pass)
7448 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7449 {
7450 Elf_Internal_Sym *locsyms = NULL;
7451 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7452
7453 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7454 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7455 {
7456 Elf_Internal_Rela *relstart, *rel, *relend;
7457 bfd_boolean found_tls_get_addr_arg = 0;
7458
7459 /* Read the relocations. */
7460 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7461 info->keep_memory);
7462 if (relstart == NULL)
7463 return FALSE;
7464
7465 relend = relstart + sec->reloc_count;
7466 for (rel = relstart; rel < relend; rel++)
7467 {
7468 enum elf_ppc64_reloc_type r_type;
7469 unsigned long r_symndx;
7470 struct elf_link_hash_entry *h;
7471 Elf_Internal_Sym *sym;
7472 asection *sym_sec;
7473 unsigned char *tls_mask;
7474 unsigned char tls_set, tls_clear, tls_type = 0;
7475 bfd_vma value;
7476 bfd_boolean ok_tprel, is_local;
7477 long toc_ref_index = 0;
7478 int expecting_tls_get_addr = 0;
7479 bfd_boolean ret = FALSE;
7480
7481 r_symndx = ELF64_R_SYM (rel->r_info);
7482 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7483 r_symndx, ibfd))
7484 {
7485 err_free_rel:
7486 if (elf_section_data (sec)->relocs != relstart)
7487 free (relstart);
7488 if (toc_ref != NULL)
7489 free (toc_ref);
7490 if (locsyms != NULL
7491 && (elf_symtab_hdr (ibfd).contents
7492 != (unsigned char *) locsyms))
7493 free (locsyms);
7494 return ret;
7495 }
7496
7497 if (h != NULL)
7498 {
7499 if (h->root.type == bfd_link_hash_defined
7500 || h->root.type == bfd_link_hash_defweak)
7501 value = h->root.u.def.value;
7502 else if (h->root.type == bfd_link_hash_undefweak)
7503 value = 0;
7504 else
7505 {
7506 found_tls_get_addr_arg = 0;
7507 continue;
7508 }
7509 }
7510 else
7511 /* Symbols referenced by TLS relocs must be of type
7512 STT_TLS. So no need for .opd local sym adjust. */
7513 value = sym->st_value;
7514
7515 ok_tprel = FALSE;
7516 is_local = FALSE;
7517 if (h == NULL
7518 || !h->def_dynamic)
7519 {
7520 is_local = TRUE;
7521 if (h != NULL
7522 && h->root.type == bfd_link_hash_undefweak)
7523 ok_tprel = TRUE;
7524 else
7525 {
7526 value += sym_sec->output_offset;
7527 value += sym_sec->output_section->vma;
7528 value -= htab->elf.tls_sec->vma;
7529 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7530 < (bfd_vma) 1 << 32);
7531 }
7532 }
7533
7534 r_type = ELF64_R_TYPE (rel->r_info);
7535 /* If this section has old-style __tls_get_addr calls
7536 without marker relocs, then check that each
7537 __tls_get_addr call reloc is preceded by a reloc
7538 that conceivably belongs to the __tls_get_addr arg
7539 setup insn. If we don't find matching arg setup
7540 relocs, don't do any tls optimization. */
7541 if (pass == 0
7542 && sec->has_tls_get_addr_call
7543 && h != NULL
7544 && (h == &htab->tls_get_addr->elf
7545 || h == &htab->tls_get_addr_fd->elf)
7546 && !found_tls_get_addr_arg
7547 && is_branch_reloc (r_type))
7548 {
7549 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7550 "TLS optimization disabled\n"),
7551 ibfd, sec, rel->r_offset);
7552 ret = TRUE;
7553 goto err_free_rel;
7554 }
7555
7556 found_tls_get_addr_arg = 0;
7557 switch (r_type)
7558 {
7559 case R_PPC64_GOT_TLSLD16:
7560 case R_PPC64_GOT_TLSLD16_LO:
7561 expecting_tls_get_addr = 1;
7562 found_tls_get_addr_arg = 1;
7563 /* Fall thru */
7564
7565 case R_PPC64_GOT_TLSLD16_HI:
7566 case R_PPC64_GOT_TLSLD16_HA:
7567 /* These relocs should never be against a symbol
7568 defined in a shared lib. Leave them alone if
7569 that turns out to be the case. */
7570 if (!is_local)
7571 continue;
7572
7573 /* LD -> LE */
7574 tls_set = 0;
7575 tls_clear = TLS_LD;
7576 tls_type = TLS_TLS | TLS_LD;
7577 break;
7578
7579 case R_PPC64_GOT_TLSGD16:
7580 case R_PPC64_GOT_TLSGD16_LO:
7581 expecting_tls_get_addr = 1;
7582 found_tls_get_addr_arg = 1;
7583 /* Fall thru */
7584
7585 case R_PPC64_GOT_TLSGD16_HI:
7586 case R_PPC64_GOT_TLSGD16_HA:
7587 if (ok_tprel)
7588 /* GD -> LE */
7589 tls_set = 0;
7590 else
7591 /* GD -> IE */
7592 tls_set = TLS_TLS | TLS_TPRELGD;
7593 tls_clear = TLS_GD;
7594 tls_type = TLS_TLS | TLS_GD;
7595 break;
7596
7597 case R_PPC64_GOT_TPREL16_DS:
7598 case R_PPC64_GOT_TPREL16_LO_DS:
7599 case R_PPC64_GOT_TPREL16_HI:
7600 case R_PPC64_GOT_TPREL16_HA:
7601 if (ok_tprel)
7602 {
7603 /* IE -> LE */
7604 tls_set = 0;
7605 tls_clear = TLS_TPREL;
7606 tls_type = TLS_TLS | TLS_TPREL;
7607 break;
7608 }
7609 continue;
7610
7611 case R_PPC64_TLSGD:
7612 case R_PPC64_TLSLD:
7613 found_tls_get_addr_arg = 1;
7614 /* Fall thru */
7615
7616 case R_PPC64_TLS:
7617 case R_PPC64_TOC16:
7618 case R_PPC64_TOC16_LO:
7619 if (sym_sec == NULL || sym_sec != toc)
7620 continue;
7621
7622 /* Mark this toc entry as referenced by a TLS
7623 code sequence. We can do that now in the
7624 case of R_PPC64_TLS, and after checking for
7625 tls_get_addr for the TOC16 relocs. */
7626 if (toc_ref == NULL)
7627 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7628 if (toc_ref == NULL)
7629 goto err_free_rel;
7630
7631 if (h != NULL)
7632 value = h->root.u.def.value;
7633 else
7634 value = sym->st_value;
7635 value += rel->r_addend;
7636 BFD_ASSERT (value < toc->size && value % 8 == 0);
7637 toc_ref_index = (value + toc->output_offset) / 8;
7638 if (r_type == R_PPC64_TLS
7639 || r_type == R_PPC64_TLSGD
7640 || r_type == R_PPC64_TLSLD)
7641 {
7642 toc_ref[toc_ref_index] = 1;
7643 continue;
7644 }
7645
7646 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7647 continue;
7648
7649 tls_set = 0;
7650 tls_clear = 0;
7651 expecting_tls_get_addr = 2;
7652 break;
7653
7654 case R_PPC64_TPREL64:
7655 if (pass == 0
7656 || sec != toc
7657 || toc_ref == NULL
7658 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7659 continue;
7660 if (ok_tprel)
7661 {
7662 /* IE -> LE */
7663 tls_set = TLS_EXPLICIT;
7664 tls_clear = TLS_TPREL;
7665 break;
7666 }
7667 continue;
7668
7669 case R_PPC64_DTPMOD64:
7670 if (pass == 0
7671 || sec != toc
7672 || toc_ref == NULL
7673 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7674 continue;
7675 if (rel + 1 < relend
7676 && (rel[1].r_info
7677 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7678 && rel[1].r_offset == rel->r_offset + 8)
7679 {
7680 if (ok_tprel)
7681 /* GD -> LE */
7682 tls_set = TLS_EXPLICIT | TLS_GD;
7683 else
7684 /* GD -> IE */
7685 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7686 tls_clear = TLS_GD;
7687 }
7688 else
7689 {
7690 if (!is_local)
7691 continue;
7692
7693 /* LD -> LE */
7694 tls_set = TLS_EXPLICIT;
7695 tls_clear = TLS_LD;
7696 }
7697 break;
7698
7699 default:
7700 continue;
7701 }
7702
7703 if (pass == 0)
7704 {
7705 if (!expecting_tls_get_addr
7706 || !sec->has_tls_get_addr_call)
7707 continue;
7708
7709 if (rel + 1 < relend
7710 && branch_reloc_hash_match (ibfd, rel + 1,
7711 htab->tls_get_addr,
7712 htab->tls_get_addr_fd))
7713 {
7714 if (expecting_tls_get_addr == 2)
7715 {
7716 /* Check for toc tls entries. */
7717 unsigned char *toc_tls;
7718 int retval;
7719
7720 retval = get_tls_mask (&toc_tls, NULL, NULL,
7721 &locsyms,
7722 rel, ibfd);
7723 if (retval == 0)
7724 goto err_free_rel;
7725 if (toc_tls != NULL)
7726 {
7727 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7728 found_tls_get_addr_arg = 1;
7729 if (retval > 1)
7730 toc_ref[toc_ref_index] = 1;
7731 }
7732 }
7733 continue;
7734 }
7735
7736 if (expecting_tls_get_addr != 1)
7737 continue;
7738
7739 /* Uh oh, we didn't find the expected call. We
7740 could just mark this symbol to exclude it
7741 from tls optimization but it's safer to skip
7742 the entire optimization. */
7743 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7744 "TLS optimization disabled\n"),
7745 ibfd, sec, rel->r_offset);
7746 ret = TRUE;
7747 goto err_free_rel;
7748 }
7749
7750 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7751 {
7752 struct plt_entry *ent;
7753 for (ent = htab->tls_get_addr->elf.plt.plist;
7754 ent != NULL;
7755 ent = ent->next)
7756 if (ent->addend == 0)
7757 {
7758 if (ent->plt.refcount > 0)
7759 {
7760 ent->plt.refcount -= 1;
7761 expecting_tls_get_addr = 0;
7762 }
7763 break;
7764 }
7765 }
7766
7767 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7768 {
7769 struct plt_entry *ent;
7770 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7771 ent != NULL;
7772 ent = ent->next)
7773 if (ent->addend == 0)
7774 {
7775 if (ent->plt.refcount > 0)
7776 ent->plt.refcount -= 1;
7777 break;
7778 }
7779 }
7780
7781 if (tls_clear == 0)
7782 continue;
7783
7784 if ((tls_set & TLS_EXPLICIT) == 0)
7785 {
7786 struct got_entry *ent;
7787
7788 /* Adjust got entry for this reloc. */
7789 if (h != NULL)
7790 ent = h->got.glist;
7791 else
7792 ent = elf_local_got_ents (ibfd)[r_symndx];
7793
7794 for (; ent != NULL; ent = ent->next)
7795 if (ent->addend == rel->r_addend
7796 && ent->owner == ibfd
7797 && ent->tls_type == tls_type)
7798 break;
7799 if (ent == NULL)
7800 abort ();
7801
7802 if (tls_set == 0)
7803 {
7804 /* We managed to get rid of a got entry. */
7805 if (ent->got.refcount > 0)
7806 ent->got.refcount -= 1;
7807 }
7808 }
7809 else
7810 {
7811 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7812 we'll lose one or two dyn relocs. */
7813 if (!dec_dynrel_count (rel->r_info, sec, info,
7814 NULL, h, sym_sec))
7815 return FALSE;
7816
7817 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7818 {
7819 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7820 NULL, h, sym_sec))
7821 return FALSE;
7822 }
7823 }
7824
7825 *tls_mask |= tls_set;
7826 *tls_mask &= ~tls_clear;
7827 }
7828
7829 if (elf_section_data (sec)->relocs != relstart)
7830 free (relstart);
7831 }
7832
7833 if (locsyms != NULL
7834 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7835 {
7836 if (!info->keep_memory)
7837 free (locsyms);
7838 else
7839 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7840 }
7841 }
7842
7843 if (toc_ref != NULL)
7844 free (toc_ref);
7845 return TRUE;
7846 }
7847
7848 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7849 the values of any global symbols in a toc section that has been
7850 edited. Globals in toc sections should be a rarity, so this function
7851 sets a flag if any are found in toc sections other than the one just
7852 edited, so that futher hash table traversals can be avoided. */
7853
7854 struct adjust_toc_info
7855 {
7856 asection *toc;
7857 unsigned long *skip;
7858 bfd_boolean global_toc_syms;
7859 };
7860
7861 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7862
7863 static bfd_boolean
7864 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7865 {
7866 struct ppc_link_hash_entry *eh;
7867 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7868 unsigned long i;
7869
7870 if (h->root.type == bfd_link_hash_indirect)
7871 return TRUE;
7872
7873 if (h->root.type != bfd_link_hash_defined
7874 && h->root.type != bfd_link_hash_defweak)
7875 return TRUE;
7876
7877 eh = (struct ppc_link_hash_entry *) h;
7878 if (eh->adjust_done)
7879 return TRUE;
7880
7881 if (eh->elf.root.u.def.section == toc_inf->toc)
7882 {
7883 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7884 i = toc_inf->toc->rawsize >> 3;
7885 else
7886 i = eh->elf.root.u.def.value >> 3;
7887
7888 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7889 {
7890 (*_bfd_error_handler)
7891 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7892 do
7893 ++i;
7894 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7895 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7896 }
7897
7898 eh->elf.root.u.def.value -= toc_inf->skip[i];
7899 eh->adjust_done = 1;
7900 }
7901 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7902 toc_inf->global_toc_syms = TRUE;
7903
7904 return TRUE;
7905 }
7906
7907 /* Examine all relocs referencing .toc sections in order to remove
7908 unused .toc entries. */
7909
7910 bfd_boolean
7911 ppc64_elf_edit_toc (struct bfd_link_info *info)
7912 {
7913 bfd *ibfd;
7914 struct adjust_toc_info toc_inf;
7915 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7916
7917 htab->do_toc_opt = 1;
7918 toc_inf.global_toc_syms = TRUE;
7919 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7920 {
7921 asection *toc, *sec;
7922 Elf_Internal_Shdr *symtab_hdr;
7923 Elf_Internal_Sym *local_syms;
7924 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7925 unsigned long *skip, *drop;
7926 unsigned char *used;
7927 unsigned char *keep, last, some_unused;
7928
7929 if (!is_ppc64_elf (ibfd))
7930 continue;
7931
7932 toc = bfd_get_section_by_name (ibfd, ".toc");
7933 if (toc == NULL
7934 || toc->size == 0
7935 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7936 || elf_discarded_section (toc))
7937 continue;
7938
7939 toc_relocs = NULL;
7940 local_syms = NULL;
7941 symtab_hdr = &elf_symtab_hdr (ibfd);
7942
7943 /* Look at sections dropped from the final link. */
7944 skip = NULL;
7945 relstart = NULL;
7946 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7947 {
7948 if (sec->reloc_count == 0
7949 || !elf_discarded_section (sec)
7950 || get_opd_info (sec)
7951 || (sec->flags & SEC_ALLOC) == 0
7952 || (sec->flags & SEC_DEBUGGING) != 0)
7953 continue;
7954
7955 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7956 if (relstart == NULL)
7957 goto error_ret;
7958
7959 /* Run through the relocs to see which toc entries might be
7960 unused. */
7961 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7962 {
7963 enum elf_ppc64_reloc_type r_type;
7964 unsigned long r_symndx;
7965 asection *sym_sec;
7966 struct elf_link_hash_entry *h;
7967 Elf_Internal_Sym *sym;
7968 bfd_vma val;
7969
7970 r_type = ELF64_R_TYPE (rel->r_info);
7971 switch (r_type)
7972 {
7973 default:
7974 continue;
7975
7976 case R_PPC64_TOC16:
7977 case R_PPC64_TOC16_LO:
7978 case R_PPC64_TOC16_HI:
7979 case R_PPC64_TOC16_HA:
7980 case R_PPC64_TOC16_DS:
7981 case R_PPC64_TOC16_LO_DS:
7982 break;
7983 }
7984
7985 r_symndx = ELF64_R_SYM (rel->r_info);
7986 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7987 r_symndx, ibfd))
7988 goto error_ret;
7989
7990 if (sym_sec != toc)
7991 continue;
7992
7993 if (h != NULL)
7994 val = h->root.u.def.value;
7995 else
7996 val = sym->st_value;
7997 val += rel->r_addend;
7998
7999 if (val >= toc->size)
8000 continue;
8001
8002 /* Anything in the toc ought to be aligned to 8 bytes.
8003 If not, don't mark as unused. */
8004 if (val & 7)
8005 continue;
8006
8007 if (skip == NULL)
8008 {
8009 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8010 if (skip == NULL)
8011 goto error_ret;
8012 }
8013
8014 skip[val >> 3] = ref_from_discarded;
8015 }
8016
8017 if (elf_section_data (sec)->relocs != relstart)
8018 free (relstart);
8019 }
8020
8021 /* For largetoc loads of address constants, we can convert
8022 . addis rx,2,addr@got@ha
8023 . ld ry,addr@got@l(rx)
8024 to
8025 . addis rx,2,addr@toc@ha
8026 . addi ry,rx,addr@toc@l
8027 when addr is within 2G of the toc pointer. This then means
8028 that the word storing "addr" in the toc is no longer needed. */
8029
8030 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8031 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8032 && toc->reloc_count != 0)
8033 {
8034 /* Read toc relocs. */
8035 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8036 info->keep_memory);
8037 if (toc_relocs == NULL)
8038 goto error_ret;
8039
8040 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8041 {
8042 enum elf_ppc64_reloc_type r_type;
8043 unsigned long r_symndx;
8044 asection *sym_sec;
8045 struct elf_link_hash_entry *h;
8046 Elf_Internal_Sym *sym;
8047 bfd_vma val, addr;
8048
8049 r_type = ELF64_R_TYPE (rel->r_info);
8050 if (r_type != R_PPC64_ADDR64)
8051 continue;
8052
8053 r_symndx = ELF64_R_SYM (rel->r_info);
8054 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8055 r_symndx, ibfd))
8056 goto error_ret;
8057
8058 if (sym_sec == NULL
8059 || elf_discarded_section (sym_sec))
8060 continue;
8061
8062 if (!SYMBOL_CALLS_LOCAL (info, h))
8063 continue;
8064
8065 if (h != NULL)
8066 {
8067 if (h->type == STT_GNU_IFUNC)
8068 continue;
8069 val = h->root.u.def.value;
8070 }
8071 else
8072 {
8073 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8074 continue;
8075 val = sym->st_value;
8076 }
8077 val += rel->r_addend;
8078 val += sym_sec->output_section->vma + sym_sec->output_offset;
8079
8080 /* We don't yet know the exact toc pointer value, but we
8081 know it will be somewhere in the toc section. Don't
8082 optimize if the difference from any possible toc
8083 pointer is outside [ff..f80008000, 7fff7fff]. */
8084 addr = toc->output_section->vma + TOC_BASE_OFF;
8085 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8086 continue;
8087
8088 addr = toc->output_section->vma + toc->output_section->rawsize;
8089 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8090 continue;
8091
8092 if (skip == NULL)
8093 {
8094 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8095 if (skip == NULL)
8096 goto error_ret;
8097 }
8098
8099 skip[rel->r_offset >> 3]
8100 |= can_optimize | ((rel - toc_relocs) << 2);
8101 }
8102 }
8103
8104 if (skip == NULL)
8105 continue;
8106
8107 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8108 if (used == NULL)
8109 {
8110 error_ret:
8111 if (local_syms != NULL
8112 && symtab_hdr->contents != (unsigned char *) local_syms)
8113 free (local_syms);
8114 if (sec != NULL
8115 && relstart != NULL
8116 && elf_section_data (sec)->relocs != relstart)
8117 free (relstart);
8118 if (toc_relocs != NULL
8119 && elf_section_data (toc)->relocs != toc_relocs)
8120 free (toc_relocs);
8121 if (skip != NULL)
8122 free (skip);
8123 return FALSE;
8124 }
8125
8126 /* Now check all kept sections that might reference the toc.
8127 Check the toc itself last. */
8128 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8129 : ibfd->sections);
8130 sec != NULL;
8131 sec = (sec == toc ? NULL
8132 : sec->next == NULL ? toc
8133 : sec->next == toc && toc->next ? toc->next
8134 : sec->next))
8135 {
8136 int repeat;
8137
8138 if (sec->reloc_count == 0
8139 || elf_discarded_section (sec)
8140 || get_opd_info (sec)
8141 || (sec->flags & SEC_ALLOC) == 0
8142 || (sec->flags & SEC_DEBUGGING) != 0)
8143 continue;
8144
8145 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8146 info->keep_memory);
8147 if (relstart == NULL)
8148 goto error_ret;
8149
8150 /* Mark toc entries referenced as used. */
8151 repeat = 0;
8152 do
8153 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8154 {
8155 enum elf_ppc64_reloc_type r_type;
8156 unsigned long r_symndx;
8157 asection *sym_sec;
8158 struct elf_link_hash_entry *h;
8159 Elf_Internal_Sym *sym;
8160 bfd_vma val;
8161
8162 r_type = ELF64_R_TYPE (rel->r_info);
8163 switch (r_type)
8164 {
8165 case R_PPC64_TOC16:
8166 case R_PPC64_TOC16_LO:
8167 case R_PPC64_TOC16_HI:
8168 case R_PPC64_TOC16_HA:
8169 case R_PPC64_TOC16_DS:
8170 case R_PPC64_TOC16_LO_DS:
8171 /* In case we're taking addresses of toc entries. */
8172 case R_PPC64_ADDR64:
8173 break;
8174
8175 default:
8176 continue;
8177 }
8178
8179 r_symndx = ELF64_R_SYM (rel->r_info);
8180 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8181 r_symndx, ibfd))
8182 {
8183 free (used);
8184 goto error_ret;
8185 }
8186
8187 if (sym_sec != toc)
8188 continue;
8189
8190 if (h != NULL)
8191 val = h->root.u.def.value;
8192 else
8193 val = sym->st_value;
8194 val += rel->r_addend;
8195
8196 if (val >= toc->size)
8197 continue;
8198
8199 if ((skip[val >> 3] & can_optimize) != 0)
8200 {
8201 bfd_vma off;
8202 unsigned char opc;
8203
8204 switch (r_type)
8205 {
8206 case R_PPC64_TOC16_HA:
8207 break;
8208
8209 case R_PPC64_TOC16_LO_DS:
8210 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8211 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8212 return FALSE;
8213 if ((opc & (0x3f << 2)) == (58u << 2))
8214 break;
8215 /* Fall thru */
8216
8217 default:
8218 /* Wrong sort of reloc, or not a ld. We may
8219 as well clear ref_from_discarded too. */
8220 skip[val >> 3] = 0;
8221 }
8222 }
8223
8224 /* For the toc section, we only mark as used if
8225 this entry itself isn't unused. */
8226 if (sec == toc
8227 && !used[val >> 3]
8228 && (used[rel->r_offset >> 3]
8229 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8230 /* Do all the relocs again, to catch reference
8231 chains. */
8232 repeat = 1;
8233
8234 used[val >> 3] = 1;
8235 }
8236 while (repeat);
8237
8238 if (elf_section_data (sec)->relocs != relstart)
8239 free (relstart);
8240 }
8241
8242 /* Merge the used and skip arrays. Assume that TOC
8243 doublewords not appearing as either used or unused belong
8244 to to an entry more than one doubleword in size. */
8245 for (drop = skip, keep = used, last = 0, some_unused = 0;
8246 drop < skip + (toc->size + 7) / 8;
8247 ++drop, ++keep)
8248 {
8249 if (*keep)
8250 {
8251 *drop &= ~ref_from_discarded;
8252 if ((*drop & can_optimize) != 0)
8253 some_unused = 1;
8254 last = 0;
8255 }
8256 else if (*drop)
8257 {
8258 some_unused = 1;
8259 last = ref_from_discarded;
8260 }
8261 else
8262 *drop = last;
8263 }
8264
8265 free (used);
8266
8267 if (some_unused)
8268 {
8269 bfd_byte *contents, *src;
8270 unsigned long off;
8271 Elf_Internal_Sym *sym;
8272 bfd_boolean local_toc_syms = FALSE;
8273
8274 /* Shuffle the toc contents, and at the same time convert the
8275 skip array from booleans into offsets. */
8276 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8277 goto error_ret;
8278
8279 elf_section_data (toc)->this_hdr.contents = contents;
8280
8281 for (src = contents, off = 0, drop = skip;
8282 src < contents + toc->size;
8283 src += 8, ++drop)
8284 {
8285 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8286 off += 8;
8287 else if (off != 0)
8288 {
8289 *drop = off;
8290 memcpy (src - off, src, 8);
8291 }
8292 }
8293 *drop = off;
8294 toc->rawsize = toc->size;
8295 toc->size = src - contents - off;
8296
8297 /* Adjust addends for relocs against the toc section sym,
8298 and optimize any accesses we can. */
8299 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8300 {
8301 if (sec->reloc_count == 0
8302 || elf_discarded_section (sec))
8303 continue;
8304
8305 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8306 info->keep_memory);
8307 if (relstart == NULL)
8308 goto error_ret;
8309
8310 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8311 {
8312 enum elf_ppc64_reloc_type r_type;
8313 unsigned long r_symndx;
8314 asection *sym_sec;
8315 struct elf_link_hash_entry *h;
8316 bfd_vma val;
8317
8318 r_type = ELF64_R_TYPE (rel->r_info);
8319 switch (r_type)
8320 {
8321 default:
8322 continue;
8323
8324 case R_PPC64_TOC16:
8325 case R_PPC64_TOC16_LO:
8326 case R_PPC64_TOC16_HI:
8327 case R_PPC64_TOC16_HA:
8328 case R_PPC64_TOC16_DS:
8329 case R_PPC64_TOC16_LO_DS:
8330 case R_PPC64_ADDR64:
8331 break;
8332 }
8333
8334 r_symndx = ELF64_R_SYM (rel->r_info);
8335 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8336 r_symndx, ibfd))
8337 goto error_ret;
8338
8339 if (sym_sec != toc)
8340 continue;
8341
8342 if (h != NULL)
8343 val = h->root.u.def.value;
8344 else
8345 {
8346 val = sym->st_value;
8347 if (val != 0)
8348 local_toc_syms = TRUE;
8349 }
8350
8351 val += rel->r_addend;
8352
8353 if (val > toc->rawsize)
8354 val = toc->rawsize;
8355 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8356 continue;
8357 else if ((skip[val >> 3] & can_optimize) != 0)
8358 {
8359 Elf_Internal_Rela *tocrel
8360 = toc_relocs + (skip[val >> 3] >> 2);
8361 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8362
8363 switch (r_type)
8364 {
8365 case R_PPC64_TOC16_HA:
8366 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8367 break;
8368
8369 case R_PPC64_TOC16_LO_DS:
8370 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8371 break;
8372
8373 default:
8374 abort ();
8375 }
8376 rel->r_addend = tocrel->r_addend;
8377 elf_section_data (sec)->relocs = relstart;
8378 continue;
8379 }
8380
8381 if (h != NULL || sym->st_value != 0)
8382 continue;
8383
8384 rel->r_addend -= skip[val >> 3];
8385 elf_section_data (sec)->relocs = relstart;
8386 }
8387
8388 if (elf_section_data (sec)->relocs != relstart)
8389 free (relstart);
8390 }
8391
8392 /* We shouldn't have local or global symbols defined in the TOC,
8393 but handle them anyway. */
8394 if (local_syms != NULL)
8395 for (sym = local_syms;
8396 sym < local_syms + symtab_hdr->sh_info;
8397 ++sym)
8398 if (sym->st_value != 0
8399 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8400 {
8401 unsigned long i;
8402
8403 if (sym->st_value > toc->rawsize)
8404 i = toc->rawsize >> 3;
8405 else
8406 i = sym->st_value >> 3;
8407
8408 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8409 {
8410 if (local_toc_syms)
8411 (*_bfd_error_handler)
8412 (_("%s defined on removed toc entry"),
8413 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8414 do
8415 ++i;
8416 while ((skip[i] & (ref_from_discarded | can_optimize)));
8417 sym->st_value = (bfd_vma) i << 3;
8418 }
8419
8420 sym->st_value -= skip[i];
8421 symtab_hdr->contents = (unsigned char *) local_syms;
8422 }
8423
8424 /* Adjust any global syms defined in this toc input section. */
8425 if (toc_inf.global_toc_syms)
8426 {
8427 toc_inf.toc = toc;
8428 toc_inf.skip = skip;
8429 toc_inf.global_toc_syms = FALSE;
8430 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8431 &toc_inf);
8432 }
8433
8434 if (toc->reloc_count != 0)
8435 {
8436 Elf_Internal_Shdr *rel_hdr;
8437 Elf_Internal_Rela *wrel;
8438 bfd_size_type sz;
8439
8440 /* Remove unused toc relocs, and adjust those we keep. */
8441 if (toc_relocs == NULL)
8442 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8443 info->keep_memory);
8444 if (toc_relocs == NULL)
8445 goto error_ret;
8446
8447 wrel = toc_relocs;
8448 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8449 if ((skip[rel->r_offset >> 3]
8450 & (ref_from_discarded | can_optimize)) == 0)
8451 {
8452 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8453 wrel->r_info = rel->r_info;
8454 wrel->r_addend = rel->r_addend;
8455 ++wrel;
8456 }
8457 else if (!dec_dynrel_count (rel->r_info, toc, info,
8458 &local_syms, NULL, NULL))
8459 goto error_ret;
8460
8461 elf_section_data (toc)->relocs = toc_relocs;
8462 toc->reloc_count = wrel - toc_relocs;
8463 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8464 sz = rel_hdr->sh_entsize;
8465 rel_hdr->sh_size = toc->reloc_count * sz;
8466 }
8467 }
8468 else if (toc_relocs != NULL
8469 && elf_section_data (toc)->relocs != toc_relocs)
8470 free (toc_relocs);
8471
8472 if (local_syms != NULL
8473 && symtab_hdr->contents != (unsigned char *) local_syms)
8474 {
8475 if (!info->keep_memory)
8476 free (local_syms);
8477 else
8478 symtab_hdr->contents = (unsigned char *) local_syms;
8479 }
8480 free (skip);
8481 }
8482
8483 return TRUE;
8484 }
8485
8486 /* Return true iff input section I references the TOC using
8487 instructions limited to +/-32k offsets. */
8488
8489 bfd_boolean
8490 ppc64_elf_has_small_toc_reloc (asection *i)
8491 {
8492 return (is_ppc64_elf (i->owner)
8493 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8494 }
8495
8496 /* Allocate space for one GOT entry. */
8497
8498 static void
8499 allocate_got (struct elf_link_hash_entry *h,
8500 struct bfd_link_info *info,
8501 struct got_entry *gent)
8502 {
8503 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8504 bfd_boolean dyn;
8505 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8506 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8507 ? 16 : 8);
8508 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8509 ? 2 : 1) * sizeof (Elf64_External_Rela);
8510 asection *got = ppc64_elf_tdata (gent->owner)->got;
8511
8512 gent->got.offset = got->size;
8513 got->size += entsize;
8514
8515 dyn = htab->elf.dynamic_sections_created;
8516 if ((info->shared
8517 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8518 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8519 || h->root.type != bfd_link_hash_undefweak))
8520 {
8521 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8522 relgot->size += rentsize;
8523 }
8524 else if (h->type == STT_GNU_IFUNC)
8525 {
8526 asection *relgot = htab->reliplt;
8527 relgot->size += rentsize;
8528 htab->got_reli_size += rentsize;
8529 }
8530 }
8531
8532 /* This function merges got entries in the same toc group. */
8533
8534 static void
8535 merge_got_entries (struct got_entry **pent)
8536 {
8537 struct got_entry *ent, *ent2;
8538
8539 for (ent = *pent; ent != NULL; ent = ent->next)
8540 if (!ent->is_indirect)
8541 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8542 if (!ent2->is_indirect
8543 && ent2->addend == ent->addend
8544 && ent2->tls_type == ent->tls_type
8545 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8546 {
8547 ent2->is_indirect = TRUE;
8548 ent2->got.ent = ent;
8549 }
8550 }
8551
8552 /* Allocate space in .plt, .got and associated reloc sections for
8553 dynamic relocs. */
8554
8555 static bfd_boolean
8556 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8557 {
8558 struct bfd_link_info *info;
8559 struct ppc_link_hash_table *htab;
8560 asection *s;
8561 struct ppc_link_hash_entry *eh;
8562 struct elf_dyn_relocs *p;
8563 struct got_entry **pgent, *gent;
8564
8565 if (h->root.type == bfd_link_hash_indirect)
8566 return TRUE;
8567
8568 info = (struct bfd_link_info *) inf;
8569 htab = ppc_hash_table (info);
8570 if (htab == NULL)
8571 return FALSE;
8572
8573 if ((htab->elf.dynamic_sections_created
8574 && h->dynindx != -1
8575 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8576 || h->type == STT_GNU_IFUNC)
8577 {
8578 struct plt_entry *pent;
8579 bfd_boolean doneone = FALSE;
8580 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8581 if (pent->plt.refcount > 0)
8582 {
8583 if (!htab->elf.dynamic_sections_created
8584 || h->dynindx == -1)
8585 {
8586 s = htab->iplt;
8587 pent->plt.offset = s->size;
8588 s->size += PLT_ENTRY_SIZE;
8589 s = htab->reliplt;
8590 }
8591 else
8592 {
8593 /* If this is the first .plt entry, make room for the special
8594 first entry. */
8595 s = htab->plt;
8596 if (s->size == 0)
8597 s->size += PLT_INITIAL_ENTRY_SIZE;
8598
8599 pent->plt.offset = s->size;
8600
8601 /* Make room for this entry. */
8602 s->size += PLT_ENTRY_SIZE;
8603
8604 /* Make room for the .glink code. */
8605 s = htab->glink;
8606 if (s->size == 0)
8607 s->size += GLINK_CALL_STUB_SIZE;
8608 /* We need bigger stubs past index 32767. */
8609 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8610 s->size += 4;
8611 s->size += 2*4;
8612
8613 /* We also need to make an entry in the .rela.plt section. */
8614 s = htab->relplt;
8615 }
8616 s->size += sizeof (Elf64_External_Rela);
8617 doneone = TRUE;
8618 }
8619 else
8620 pent->plt.offset = (bfd_vma) -1;
8621 if (!doneone)
8622 {
8623 h->plt.plist = NULL;
8624 h->needs_plt = 0;
8625 }
8626 }
8627 else
8628 {
8629 h->plt.plist = NULL;
8630 h->needs_plt = 0;
8631 }
8632
8633 eh = (struct ppc_link_hash_entry *) h;
8634 /* Run through the TLS GD got entries first if we're changing them
8635 to TPREL. */
8636 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8637 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8638 if (gent->got.refcount > 0
8639 && (gent->tls_type & TLS_GD) != 0)
8640 {
8641 /* This was a GD entry that has been converted to TPREL. If
8642 there happens to be a TPREL entry we can use that one. */
8643 struct got_entry *ent;
8644 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8645 if (ent->got.refcount > 0
8646 && (ent->tls_type & TLS_TPREL) != 0
8647 && ent->addend == gent->addend
8648 && ent->owner == gent->owner)
8649 {
8650 gent->got.refcount = 0;
8651 break;
8652 }
8653
8654 /* If not, then we'll be using our own TPREL entry. */
8655 if (gent->got.refcount != 0)
8656 gent->tls_type = TLS_TLS | TLS_TPREL;
8657 }
8658
8659 /* Remove any list entry that won't generate a word in the GOT before
8660 we call merge_got_entries. Otherwise we risk merging to empty
8661 entries. */
8662 pgent = &h->got.glist;
8663 while ((gent = *pgent) != NULL)
8664 if (gent->got.refcount > 0)
8665 {
8666 if ((gent->tls_type & TLS_LD) != 0
8667 && !h->def_dynamic)
8668 {
8669 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8670 *pgent = gent->next;
8671 }
8672 else
8673 pgent = &gent->next;
8674 }
8675 else
8676 *pgent = gent->next;
8677
8678 if (!htab->do_multi_toc)
8679 merge_got_entries (&h->got.glist);
8680
8681 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8682 if (!gent->is_indirect)
8683 {
8684 /* Make sure this symbol is output as a dynamic symbol.
8685 Undefined weak syms won't yet be marked as dynamic,
8686 nor will all TLS symbols. */
8687 if (h->dynindx == -1
8688 && !h->forced_local
8689 && h->type != STT_GNU_IFUNC
8690 && htab->elf.dynamic_sections_created)
8691 {
8692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8693 return FALSE;
8694 }
8695
8696 if (!is_ppc64_elf (gent->owner))
8697 abort ();
8698
8699 allocate_got (h, info, gent);
8700 }
8701
8702 if (eh->dyn_relocs == NULL
8703 || (!htab->elf.dynamic_sections_created
8704 && h->type != STT_GNU_IFUNC))
8705 return TRUE;
8706
8707 /* In the shared -Bsymbolic case, discard space allocated for
8708 dynamic pc-relative relocs against symbols which turn out to be
8709 defined in regular objects. For the normal shared case, discard
8710 space for relocs that have become local due to symbol visibility
8711 changes. */
8712
8713 if (info->shared)
8714 {
8715 /* Relocs that use pc_count are those that appear on a call insn,
8716 or certain REL relocs (see must_be_dyn_reloc) that can be
8717 generated via assembly. We want calls to protected symbols to
8718 resolve directly to the function rather than going via the plt.
8719 If people want function pointer comparisons to work as expected
8720 then they should avoid writing weird assembly. */
8721 if (SYMBOL_CALLS_LOCAL (info, h))
8722 {
8723 struct elf_dyn_relocs **pp;
8724
8725 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8726 {
8727 p->count -= p->pc_count;
8728 p->pc_count = 0;
8729 if (p->count == 0)
8730 *pp = p->next;
8731 else
8732 pp = &p->next;
8733 }
8734 }
8735
8736 /* Also discard relocs on undefined weak syms with non-default
8737 visibility. */
8738 if (eh->dyn_relocs != NULL
8739 && h->root.type == bfd_link_hash_undefweak)
8740 {
8741 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8742 eh->dyn_relocs = NULL;
8743
8744 /* Make sure this symbol is output as a dynamic symbol.
8745 Undefined weak syms won't yet be marked as dynamic. */
8746 else if (h->dynindx == -1
8747 && !h->forced_local)
8748 {
8749 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8750 return FALSE;
8751 }
8752 }
8753 }
8754 else if (h->type == STT_GNU_IFUNC)
8755 {
8756 if (!h->non_got_ref)
8757 eh->dyn_relocs = NULL;
8758 }
8759 else if (ELIMINATE_COPY_RELOCS)
8760 {
8761 /* For the non-shared case, discard space for relocs against
8762 symbols which turn out to need copy relocs or are not
8763 dynamic. */
8764
8765 if (!h->non_got_ref
8766 && !h->def_regular)
8767 {
8768 /* Make sure this symbol is output as a dynamic symbol.
8769 Undefined weak syms won't yet be marked as dynamic. */
8770 if (h->dynindx == -1
8771 && !h->forced_local)
8772 {
8773 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8774 return FALSE;
8775 }
8776
8777 /* If that succeeded, we know we'll be keeping all the
8778 relocs. */
8779 if (h->dynindx != -1)
8780 goto keep;
8781 }
8782
8783 eh->dyn_relocs = NULL;
8784
8785 keep: ;
8786 }
8787
8788 /* Finally, allocate space. */
8789 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8790 {
8791 asection *sreloc = elf_section_data (p->sec)->sreloc;
8792 if (!htab->elf.dynamic_sections_created)
8793 sreloc = htab->reliplt;
8794 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8795 }
8796
8797 return TRUE;
8798 }
8799
8800 /* Find any dynamic relocs that apply to read-only sections. */
8801
8802 static bfd_boolean
8803 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8804 {
8805 struct ppc_link_hash_entry *eh;
8806 struct elf_dyn_relocs *p;
8807
8808 eh = (struct ppc_link_hash_entry *) h;
8809 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8810 {
8811 asection *s = p->sec->output_section;
8812
8813 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8814 {
8815 struct bfd_link_info *info = inf;
8816
8817 info->flags |= DF_TEXTREL;
8818
8819 /* Not an error, just cut short the traversal. */
8820 return FALSE;
8821 }
8822 }
8823 return TRUE;
8824 }
8825
8826 /* Set the sizes of the dynamic sections. */
8827
8828 static bfd_boolean
8829 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8830 struct bfd_link_info *info)
8831 {
8832 struct ppc_link_hash_table *htab;
8833 bfd *dynobj;
8834 asection *s;
8835 bfd_boolean relocs;
8836 bfd *ibfd;
8837 struct got_entry *first_tlsld;
8838
8839 htab = ppc_hash_table (info);
8840 if (htab == NULL)
8841 return FALSE;
8842
8843 dynobj = htab->elf.dynobj;
8844 if (dynobj == NULL)
8845 abort ();
8846
8847 if (htab->elf.dynamic_sections_created)
8848 {
8849 /* Set the contents of the .interp section to the interpreter. */
8850 if (info->executable)
8851 {
8852 s = bfd_get_section_by_name (dynobj, ".interp");
8853 if (s == NULL)
8854 abort ();
8855 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8856 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8857 }
8858 }
8859
8860 /* Set up .got offsets for local syms, and space for local dynamic
8861 relocs. */
8862 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8863 {
8864 struct got_entry **lgot_ents;
8865 struct got_entry **end_lgot_ents;
8866 struct plt_entry **local_plt;
8867 struct plt_entry **end_local_plt;
8868 unsigned char *lgot_masks;
8869 bfd_size_type locsymcount;
8870 Elf_Internal_Shdr *symtab_hdr;
8871 asection *srel;
8872
8873 if (!is_ppc64_elf (ibfd))
8874 continue;
8875
8876 for (s = ibfd->sections; s != NULL; s = s->next)
8877 {
8878 struct elf_dyn_relocs *p;
8879
8880 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
8881 {
8882 if (!bfd_is_abs_section (p->sec)
8883 && bfd_is_abs_section (p->sec->output_section))
8884 {
8885 /* Input section has been discarded, either because
8886 it is a copy of a linkonce section or due to
8887 linker script /DISCARD/, so we'll be discarding
8888 the relocs too. */
8889 }
8890 else if (p->count != 0)
8891 {
8892 srel = elf_section_data (p->sec)->sreloc;
8893 if (!htab->elf.dynamic_sections_created)
8894 srel = htab->reliplt;
8895 srel->size += p->count * sizeof (Elf64_External_Rela);
8896 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8897 info->flags |= DF_TEXTREL;
8898 }
8899 }
8900 }
8901
8902 lgot_ents = elf_local_got_ents (ibfd);
8903 if (!lgot_ents)
8904 continue;
8905
8906 symtab_hdr = &elf_symtab_hdr (ibfd);
8907 locsymcount = symtab_hdr->sh_info;
8908 end_lgot_ents = lgot_ents + locsymcount;
8909 local_plt = (struct plt_entry **) end_lgot_ents;
8910 end_local_plt = local_plt + locsymcount;
8911 lgot_masks = (unsigned char *) end_local_plt;
8912 s = ppc64_elf_tdata (ibfd)->got;
8913 srel = ppc64_elf_tdata (ibfd)->relgot;
8914 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
8915 {
8916 struct got_entry **pent, *ent;
8917
8918 pent = lgot_ents;
8919 while ((ent = *pent) != NULL)
8920 if (ent->got.refcount > 0)
8921 {
8922 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
8923 {
8924 ppc64_tlsld_got (ibfd)->got.refcount += 1;
8925 *pent = ent->next;
8926 }
8927 else
8928 {
8929 unsigned int num = 1;
8930 ent->got.offset = s->size;
8931 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
8932 num = 2;
8933 s->size += num * 8;
8934 if (info->shared)
8935 srel->size += num * sizeof (Elf64_External_Rela);
8936 else if ((*lgot_masks & PLT_IFUNC) != 0)
8937 {
8938 htab->reliplt->size
8939 += num * sizeof (Elf64_External_Rela);
8940 htab->got_reli_size
8941 += num * sizeof (Elf64_External_Rela);
8942 }
8943 pent = &ent->next;
8944 }
8945 }
8946 else
8947 *pent = ent->next;
8948 }
8949
8950 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8951 for (; local_plt < end_local_plt; ++local_plt)
8952 {
8953 struct plt_entry *ent;
8954
8955 for (ent = *local_plt; ent != NULL; ent = ent->next)
8956 if (ent->plt.refcount > 0)
8957 {
8958 s = htab->iplt;
8959 ent->plt.offset = s->size;
8960 s->size += PLT_ENTRY_SIZE;
8961
8962 htab->reliplt->size += sizeof (Elf64_External_Rela);
8963 }
8964 else
8965 ent->plt.offset = (bfd_vma) -1;
8966 }
8967 }
8968
8969 /* Allocate global sym .plt and .got entries, and space for global
8970 sym dynamic relocs. */
8971 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
8972
8973 first_tlsld = NULL;
8974 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8975 {
8976 struct got_entry *ent;
8977
8978 if (!is_ppc64_elf (ibfd))
8979 continue;
8980
8981 ent = ppc64_tlsld_got (ibfd);
8982 if (ent->got.refcount > 0)
8983 {
8984 if (!htab->do_multi_toc && first_tlsld != NULL)
8985 {
8986 ent->is_indirect = TRUE;
8987 ent->got.ent = first_tlsld;
8988 }
8989 else
8990 {
8991 if (first_tlsld == NULL)
8992 first_tlsld = ent;
8993 s = ppc64_elf_tdata (ibfd)->got;
8994 ent->got.offset = s->size;
8995 ent->owner = ibfd;
8996 s->size += 16;
8997 if (info->shared)
8998 {
8999 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9000 srel->size += sizeof (Elf64_External_Rela);
9001 }
9002 }
9003 }
9004 else
9005 ent->got.offset = (bfd_vma) -1;
9006 }
9007
9008 /* We now have determined the sizes of the various dynamic sections.
9009 Allocate memory for them. */
9010 relocs = FALSE;
9011 for (s = dynobj->sections; s != NULL; s = s->next)
9012 {
9013 if ((s->flags & SEC_LINKER_CREATED) == 0)
9014 continue;
9015
9016 if (s == htab->brlt || s == htab->relbrlt)
9017 /* These haven't been allocated yet; don't strip. */
9018 continue;
9019 else if (s == htab->got
9020 || s == htab->plt
9021 || s == htab->iplt
9022 || s == htab->glink
9023 || s == htab->dynbss)
9024 {
9025 /* Strip this section if we don't need it; see the
9026 comment below. */
9027 }
9028 else if (CONST_STRNEQ (s->name, ".rela"))
9029 {
9030 if (s->size != 0)
9031 {
9032 if (s != htab->relplt)
9033 relocs = TRUE;
9034
9035 /* We use the reloc_count field as a counter if we need
9036 to copy relocs into the output file. */
9037 s->reloc_count = 0;
9038 }
9039 }
9040 else
9041 {
9042 /* It's not one of our sections, so don't allocate space. */
9043 continue;
9044 }
9045
9046 if (s->size == 0)
9047 {
9048 /* If we don't need this section, strip it from the
9049 output file. This is mostly to handle .rela.bss and
9050 .rela.plt. We must create both sections in
9051 create_dynamic_sections, because they must be created
9052 before the linker maps input sections to output
9053 sections. The linker does that before
9054 adjust_dynamic_symbol is called, and it is that
9055 function which decides whether anything needs to go
9056 into these sections. */
9057 s->flags |= SEC_EXCLUDE;
9058 continue;
9059 }
9060
9061 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9062 continue;
9063
9064 /* Allocate memory for the section contents. We use bfd_zalloc
9065 here in case unused entries are not reclaimed before the
9066 section's contents are written out. This should not happen,
9067 but this way if it does we get a R_PPC64_NONE reloc in .rela
9068 sections instead of garbage.
9069 We also rely on the section contents being zero when writing
9070 the GOT. */
9071 s->contents = bfd_zalloc (dynobj, s->size);
9072 if (s->contents == NULL)
9073 return FALSE;
9074 }
9075
9076 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9077 {
9078 if (!is_ppc64_elf (ibfd))
9079 continue;
9080
9081 s = ppc64_elf_tdata (ibfd)->got;
9082 if (s != NULL && s != htab->got)
9083 {
9084 if (s->size == 0)
9085 s->flags |= SEC_EXCLUDE;
9086 else
9087 {
9088 s->contents = bfd_zalloc (ibfd, s->size);
9089 if (s->contents == NULL)
9090 return FALSE;
9091 }
9092 }
9093 s = ppc64_elf_tdata (ibfd)->relgot;
9094 if (s != NULL)
9095 {
9096 if (s->size == 0)
9097 s->flags |= SEC_EXCLUDE;
9098 else
9099 {
9100 s->contents = bfd_zalloc (ibfd, s->size);
9101 if (s->contents == NULL)
9102 return FALSE;
9103 relocs = TRUE;
9104 s->reloc_count = 0;
9105 }
9106 }
9107 }
9108
9109 if (htab->elf.dynamic_sections_created)
9110 {
9111 /* Add some entries to the .dynamic section. We fill in the
9112 values later, in ppc64_elf_finish_dynamic_sections, but we
9113 must add the entries now so that we get the correct size for
9114 the .dynamic section. The DT_DEBUG entry is filled in by the
9115 dynamic linker and used by the debugger. */
9116 #define add_dynamic_entry(TAG, VAL) \
9117 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9118
9119 if (info->executable)
9120 {
9121 if (!add_dynamic_entry (DT_DEBUG, 0))
9122 return FALSE;
9123 }
9124
9125 if (htab->plt != NULL && htab->plt->size != 0)
9126 {
9127 if (!add_dynamic_entry (DT_PLTGOT, 0)
9128 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9129 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9130 || !add_dynamic_entry (DT_JMPREL, 0)
9131 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9132 return FALSE;
9133 }
9134
9135 if (NO_OPD_RELOCS)
9136 {
9137 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9138 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9139 return FALSE;
9140 }
9141
9142 if (!htab->no_tls_get_addr_opt
9143 && htab->tls_get_addr_fd != NULL
9144 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9145 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9146 return FALSE;
9147
9148 if (relocs)
9149 {
9150 if (!add_dynamic_entry (DT_RELA, 0)
9151 || !add_dynamic_entry (DT_RELASZ, 0)
9152 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9153 return FALSE;
9154
9155 /* If any dynamic relocs apply to a read-only section,
9156 then we need a DT_TEXTREL entry. */
9157 if ((info->flags & DF_TEXTREL) == 0)
9158 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9159
9160 if ((info->flags & DF_TEXTREL) != 0)
9161 {
9162 if (!add_dynamic_entry (DT_TEXTREL, 0))
9163 return FALSE;
9164 }
9165 }
9166 }
9167 #undef add_dynamic_entry
9168
9169 return TRUE;
9170 }
9171
9172 /* Determine the type of stub needed, if any, for a call. */
9173
9174 static inline enum ppc_stub_type
9175 ppc_type_of_stub (asection *input_sec,
9176 const Elf_Internal_Rela *rel,
9177 struct ppc_link_hash_entry **hash,
9178 struct plt_entry **plt_ent,
9179 bfd_vma destination)
9180 {
9181 struct ppc_link_hash_entry *h = *hash;
9182 bfd_vma location;
9183 bfd_vma branch_offset;
9184 bfd_vma max_branch_offset;
9185 enum elf_ppc64_reloc_type r_type;
9186
9187 if (h != NULL)
9188 {
9189 struct plt_entry *ent;
9190 struct ppc_link_hash_entry *fdh = h;
9191 if (h->oh != NULL
9192 && h->oh->is_func_descriptor)
9193 {
9194 fdh = ppc_follow_link (h->oh);
9195 *hash = fdh;
9196 }
9197
9198 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9199 if (ent->addend == rel->r_addend
9200 && ent->plt.offset != (bfd_vma) -1)
9201 {
9202 *plt_ent = ent;
9203 return ppc_stub_plt_call;
9204 }
9205
9206 /* Here, we know we don't have a plt entry. If we don't have a
9207 either a defined function descriptor or a defined entry symbol
9208 in a regular object file, then it is pointless trying to make
9209 any other type of stub. */
9210 if (!is_static_defined (&fdh->elf)
9211 && !is_static_defined (&h->elf))
9212 return ppc_stub_none;
9213 }
9214 else if (elf_local_got_ents (input_sec->owner) != NULL)
9215 {
9216 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9217 struct plt_entry **local_plt = (struct plt_entry **)
9218 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9219 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9220
9221 if (local_plt[r_symndx] != NULL)
9222 {
9223 struct plt_entry *ent;
9224
9225 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9226 if (ent->addend == rel->r_addend
9227 && ent->plt.offset != (bfd_vma) -1)
9228 {
9229 *plt_ent = ent;
9230 return ppc_stub_plt_call;
9231 }
9232 }
9233 }
9234
9235 /* Determine where the call point is. */
9236 location = (input_sec->output_offset
9237 + input_sec->output_section->vma
9238 + rel->r_offset);
9239
9240 branch_offset = destination - location;
9241 r_type = ELF64_R_TYPE (rel->r_info);
9242
9243 /* Determine if a long branch stub is needed. */
9244 max_branch_offset = 1 << 25;
9245 if (r_type != R_PPC64_REL24)
9246 max_branch_offset = 1 << 15;
9247
9248 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9249 /* We need a stub. Figure out whether a long_branch or plt_branch
9250 is needed later. */
9251 return ppc_stub_long_branch;
9252
9253 return ppc_stub_none;
9254 }
9255
9256 /* Build a .plt call stub. */
9257
9258 static inline bfd_byte *
9259 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r,
9260 bfd_boolean plt_static_chain)
9261 {
9262 #define PPC_LO(v) ((v) & 0xffff)
9263 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9264 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9265
9266 if (PPC_HA (offset) != 0)
9267 {
9268 if (r != NULL)
9269 {
9270 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9271 r[1].r_offset = r[0].r_offset + 8;
9272 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9273 r[1].r_addend = r[0].r_addend;
9274 if (PPC_HA (offset + 16) != PPC_HA (offset))
9275 {
9276 r[2].r_offset = r[1].r_offset + 4;
9277 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9278 r[2].r_addend = r[0].r_addend;
9279 }
9280 else
9281 {
9282 r[2].r_offset = r[1].r_offset + 8;
9283 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9284 r[2].r_addend = r[0].r_addend + 8;
9285 r[3].r_offset = r[2].r_offset + 4;
9286 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9287 r[3].r_addend = r[0].r_addend + 16;
9288 }
9289 }
9290 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9291 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9292 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9293 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9294 {
9295 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9296 offset = 0;
9297 }
9298 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9299 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9300 if (plt_static_chain)
9301 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9302 bfd_put_32 (obfd, BCTR, p), p += 4;
9303 }
9304 else
9305 {
9306 if (r != NULL)
9307 {
9308 r[0].r_offset += 4;
9309 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9310 if (PPC_HA (offset + 16) != PPC_HA (offset))
9311 {
9312 r[1].r_offset = r[0].r_offset + 4;
9313 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9314 r[1].r_addend = r[0].r_addend;
9315 }
9316 else
9317 {
9318 r[1].r_offset = r[0].r_offset + 8;
9319 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9320 r[1].r_addend = r[0].r_addend + 16;
9321 r[2].r_offset = r[1].r_offset + 4;
9322 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9323 r[2].r_addend = r[0].r_addend + 8;
9324 }
9325 }
9326 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9327 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9328 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
9329 {
9330 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9331 offset = 0;
9332 }
9333 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9334 if (plt_static_chain)
9335 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9336 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9337 bfd_put_32 (obfd, BCTR, p), p += 4;
9338 }
9339 return p;
9340 }
9341
9342 /* Build a special .plt call stub for __tls_get_addr. */
9343
9344 #define LD_R11_0R3 0xe9630000
9345 #define LD_R12_0R3 0xe9830000
9346 #define MR_R0_R3 0x7c601b78
9347 #define CMPDI_R11_0 0x2c2b0000
9348 #define ADD_R3_R12_R13 0x7c6c6a14
9349 #define BEQLR 0x4d820020
9350 #define MR_R3_R0 0x7c030378
9351 #define MFLR_R11 0x7d6802a6
9352 #define STD_R11_0R1 0xf9610000
9353 #define BCTRL 0x4e800421
9354 #define LD_R11_0R1 0xe9610000
9355 #define LD_R2_0R1 0xe8410000
9356 #define MTLR_R11 0x7d6803a6
9357
9358 static inline bfd_byte *
9359 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9360 Elf_Internal_Rela *r, bfd_boolean plt_static_chain)
9361 {
9362 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9363 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9364 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9365 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9366 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9367 bfd_put_32 (obfd, BEQLR, p), p += 4;
9368 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9369 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9370 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9371
9372 if (r != NULL)
9373 r[0].r_offset += 9 * 4;
9374 p = build_plt_stub (obfd, p, offset, r, plt_static_chain);
9375 bfd_put_32 (obfd, BCTRL, p - 4);
9376
9377 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9378 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9379 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9380 bfd_put_32 (obfd, BLR, p), p += 4;
9381
9382 return p;
9383 }
9384
9385 static Elf_Internal_Rela *
9386 get_relocs (asection *sec, int count)
9387 {
9388 Elf_Internal_Rela *relocs;
9389 struct bfd_elf_section_data *elfsec_data;
9390
9391 elfsec_data = elf_section_data (sec);
9392 relocs = elfsec_data->relocs;
9393 if (relocs == NULL)
9394 {
9395 bfd_size_type relsize;
9396 relsize = sec->reloc_count * sizeof (*relocs);
9397 relocs = bfd_alloc (sec->owner, relsize);
9398 if (relocs == NULL)
9399 return NULL;
9400 elfsec_data->relocs = relocs;
9401 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9402 sizeof (Elf_Internal_Shdr));
9403 if (elfsec_data->rela.hdr == NULL)
9404 return NULL;
9405 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9406 * sizeof (Elf64_External_Rela));
9407 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9408 sec->reloc_count = 0;
9409 }
9410 relocs += sec->reloc_count;
9411 sec->reloc_count += count;
9412 return relocs;
9413 }
9414
9415 static bfd_vma
9416 get_r2off (struct bfd_link_info *info,
9417 struct ppc_stub_hash_entry *stub_entry)
9418 {
9419 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9420 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9421
9422 if (r2off == 0)
9423 {
9424 /* Support linking -R objects. Get the toc pointer from the
9425 opd entry. */
9426 char buf[8];
9427 asection *opd = stub_entry->h->elf.root.u.def.section;
9428 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9429
9430 if (strcmp (opd->name, ".opd") != 0
9431 || opd->reloc_count != 0)
9432 {
9433 info->callbacks->einfo (_("cannot find opd entry toc for %s\n"),
9434 stub_entry->h->elf.root.root.string);
9435 bfd_set_error (bfd_error_bad_value);
9436 return 0;
9437 }
9438 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9439 return 0;
9440 r2off = bfd_get_64 (opd->owner, buf);
9441 r2off -= elf_gp (info->output_bfd);
9442 }
9443 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9444 return r2off;
9445 }
9446
9447 static bfd_boolean
9448 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9449 {
9450 struct ppc_stub_hash_entry *stub_entry;
9451 struct ppc_branch_hash_entry *br_entry;
9452 struct bfd_link_info *info;
9453 struct ppc_link_hash_table *htab;
9454 bfd_byte *loc;
9455 bfd_byte *p;
9456 bfd_vma dest, off;
9457 int size;
9458 Elf_Internal_Rela *r;
9459 asection *plt;
9460
9461 /* Massage our args to the form they really have. */
9462 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9463 info = in_arg;
9464
9465 htab = ppc_hash_table (info);
9466 if (htab == NULL)
9467 return FALSE;
9468
9469 /* Make a note of the offset within the stubs for this entry. */
9470 stub_entry->stub_offset = stub_entry->stub_sec->size;
9471 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9472
9473 htab->stub_count[stub_entry->stub_type - 1] += 1;
9474 switch (stub_entry->stub_type)
9475 {
9476 case ppc_stub_long_branch:
9477 case ppc_stub_long_branch_r2off:
9478 /* Branches are relative. This is where we are going to. */
9479 off = dest = (stub_entry->target_value
9480 + stub_entry->target_section->output_offset
9481 + stub_entry->target_section->output_section->vma);
9482
9483 /* And this is where we are coming from. */
9484 off -= (stub_entry->stub_offset
9485 + stub_entry->stub_sec->output_offset
9486 + stub_entry->stub_sec->output_section->vma);
9487
9488 size = 4;
9489 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9490 {
9491 bfd_vma r2off = get_r2off (info, stub_entry);
9492
9493 if (r2off == 0)
9494 {
9495 htab->stub_error = TRUE;
9496 return FALSE;
9497 }
9498 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9499 loc += 4;
9500 size = 12;
9501 if (PPC_HA (r2off) != 0)
9502 {
9503 size = 16;
9504 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9505 loc += 4;
9506 }
9507 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9508 loc += 4;
9509 off -= size - 4;
9510 }
9511 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9512
9513 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9514 {
9515 info->callbacks->einfo (_("long branch stub `%s' offset overflow\n"),
9516 stub_entry->root.string);
9517 htab->stub_error = TRUE;
9518 return FALSE;
9519 }
9520
9521 if (info->emitrelocations)
9522 {
9523 r = get_relocs (stub_entry->stub_sec, 1);
9524 if (r == NULL)
9525 return FALSE;
9526 r->r_offset = loc - stub_entry->stub_sec->contents;
9527 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9528 r->r_addend = dest;
9529 if (stub_entry->h != NULL)
9530 {
9531 struct elf_link_hash_entry **hashes;
9532 unsigned long symndx;
9533 struct ppc_link_hash_entry *h;
9534
9535 hashes = elf_sym_hashes (htab->stub_bfd);
9536 if (hashes == NULL)
9537 {
9538 bfd_size_type hsize;
9539
9540 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9541 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9542 if (hashes == NULL)
9543 return FALSE;
9544 elf_sym_hashes (htab->stub_bfd) = hashes;
9545 htab->stub_globals = 1;
9546 }
9547 symndx = htab->stub_globals++;
9548 h = stub_entry->h;
9549 hashes[symndx] = &h->elf;
9550 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9551 if (h->oh != NULL && h->oh->is_func)
9552 h = ppc_follow_link (h->oh);
9553 if (h->elf.root.u.def.section != stub_entry->target_section)
9554 /* H is an opd symbol. The addend must be zero. */
9555 r->r_addend = 0;
9556 else
9557 {
9558 off = (h->elf.root.u.def.value
9559 + h->elf.root.u.def.section->output_offset
9560 + h->elf.root.u.def.section->output_section->vma);
9561 r->r_addend -= off;
9562 }
9563 }
9564 }
9565 break;
9566
9567 case ppc_stub_plt_branch:
9568 case ppc_stub_plt_branch_r2off:
9569 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9570 stub_entry->root.string + 9,
9571 FALSE, FALSE);
9572 if (br_entry == NULL)
9573 {
9574 info->callbacks->einfo (_("can't find branch stub `%s'\n"),
9575 stub_entry->root.string);
9576 htab->stub_error = TRUE;
9577 return FALSE;
9578 }
9579
9580 dest = (stub_entry->target_value
9581 + stub_entry->target_section->output_offset
9582 + stub_entry->target_section->output_section->vma);
9583
9584 bfd_put_64 (htab->brlt->owner, dest,
9585 htab->brlt->contents + br_entry->offset);
9586
9587 if (br_entry->iter == htab->stub_iteration)
9588 {
9589 br_entry->iter = 0;
9590
9591 if (htab->relbrlt != NULL)
9592 {
9593 /* Create a reloc for the branch lookup table entry. */
9594 Elf_Internal_Rela rela;
9595 bfd_byte *rl;
9596
9597 rela.r_offset = (br_entry->offset
9598 + htab->brlt->output_offset
9599 + htab->brlt->output_section->vma);
9600 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9601 rela.r_addend = dest;
9602
9603 rl = htab->relbrlt->contents;
9604 rl += (htab->relbrlt->reloc_count++
9605 * sizeof (Elf64_External_Rela));
9606 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9607 }
9608 else if (info->emitrelocations)
9609 {
9610 r = get_relocs (htab->brlt, 1);
9611 if (r == NULL)
9612 return FALSE;
9613 /* brlt, being SEC_LINKER_CREATED does not go through the
9614 normal reloc processing. Symbols and offsets are not
9615 translated from input file to output file form, so
9616 set up the offset per the output file. */
9617 r->r_offset = (br_entry->offset
9618 + htab->brlt->output_offset
9619 + htab->brlt->output_section->vma);
9620 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9621 r->r_addend = dest;
9622 }
9623 }
9624
9625 dest = (br_entry->offset
9626 + htab->brlt->output_offset
9627 + htab->brlt->output_section->vma);
9628
9629 off = (dest
9630 - elf_gp (htab->brlt->output_section->owner)
9631 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9632
9633 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9634 {
9635 info->callbacks->einfo
9636 (_("linkage table error against `%s'\n"),
9637 stub_entry->root.string);
9638 bfd_set_error (bfd_error_bad_value);
9639 htab->stub_error = TRUE;
9640 return FALSE;
9641 }
9642
9643 if (info->emitrelocations)
9644 {
9645 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9646 if (r == NULL)
9647 return FALSE;
9648 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9649 if (bfd_big_endian (info->output_bfd))
9650 r[0].r_offset += 2;
9651 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9652 r[0].r_offset += 4;
9653 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9654 r[0].r_addend = dest;
9655 if (PPC_HA (off) != 0)
9656 {
9657 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9658 r[1].r_offset = r[0].r_offset + 4;
9659 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9660 r[1].r_addend = r[0].r_addend;
9661 }
9662 }
9663
9664 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9665 {
9666 if (PPC_HA (off) != 0)
9667 {
9668 size = 16;
9669 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9670 loc += 4;
9671 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9672 }
9673 else
9674 {
9675 size = 12;
9676 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9677 }
9678 }
9679 else
9680 {
9681 bfd_vma r2off = get_r2off (info, stub_entry);
9682
9683 if (r2off == 0)
9684 {
9685 htab->stub_error = TRUE;
9686 return FALSE;
9687 }
9688
9689 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9690 loc += 4;
9691 size = 20;
9692 if (PPC_HA (off) != 0)
9693 {
9694 size += 4;
9695 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9696 loc += 4;
9697 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9698 loc += 4;
9699 }
9700 else
9701 {
9702 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9703 loc += 4;
9704 }
9705
9706 if (PPC_HA (r2off) != 0)
9707 {
9708 size += 4;
9709 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9710 loc += 4;
9711 }
9712 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9713 }
9714 loc += 4;
9715 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9716 loc += 4;
9717 bfd_put_32 (htab->stub_bfd, BCTR, loc);
9718 break;
9719
9720 case ppc_stub_plt_call:
9721 if (stub_entry->h != NULL
9722 && stub_entry->h->is_func_descriptor
9723 && stub_entry->h->oh != NULL)
9724 {
9725 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9726
9727 /* If the old-ABI "dot-symbol" is undefined make it weak so
9728 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9729 FIXME: We used to define the symbol on one of the call
9730 stubs instead, which is why we test symbol section id
9731 against htab->top_id in various places. Likely all
9732 these checks could now disappear. */
9733 if (fh->elf.root.type == bfd_link_hash_undefined)
9734 fh->elf.root.type = bfd_link_hash_undefweak;
9735 /* Stop undo_symbol_twiddle changing it back to undefined. */
9736 fh->was_undefined = 0;
9737 }
9738
9739 /* Now build the stub. */
9740 dest = stub_entry->plt_ent->plt.offset & ~1;
9741 if (dest >= (bfd_vma) -2)
9742 abort ();
9743
9744 plt = htab->plt;
9745 if (!htab->elf.dynamic_sections_created
9746 || stub_entry->h == NULL
9747 || stub_entry->h->elf.dynindx == -1)
9748 plt = htab->iplt;
9749
9750 dest += plt->output_offset + plt->output_section->vma;
9751
9752 if (stub_entry->h == NULL
9753 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9754 {
9755 Elf_Internal_Rela rela;
9756 bfd_byte *rl;
9757
9758 rela.r_offset = dest;
9759 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9760 rela.r_addend = (stub_entry->target_value
9761 + stub_entry->target_section->output_offset
9762 + stub_entry->target_section->output_section->vma);
9763
9764 rl = (htab->reliplt->contents
9765 + (htab->reliplt->reloc_count++
9766 * sizeof (Elf64_External_Rela)));
9767 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9768 stub_entry->plt_ent->plt.offset |= 1;
9769 }
9770
9771 off = (dest
9772 - elf_gp (plt->output_section->owner)
9773 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9774
9775 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9776 {
9777 info->callbacks->einfo
9778 (_("linkage table error against `%s'\n"),
9779 stub_entry->h != NULL
9780 ? stub_entry->h->elf.root.root.string
9781 : "<local sym>");
9782 bfd_set_error (bfd_error_bad_value);
9783 htab->stub_error = TRUE;
9784 return FALSE;
9785 }
9786
9787 r = NULL;
9788 if (info->emitrelocations)
9789 {
9790 r = get_relocs (stub_entry->stub_sec,
9791 (2 + (PPC_HA (off) != 0)
9792 + (PPC_HA (off + 16) == PPC_HA (off))));
9793 if (r == NULL)
9794 return FALSE;
9795 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9796 if (bfd_big_endian (info->output_bfd))
9797 r[0].r_offset += 2;
9798 r[0].r_addend = dest;
9799 }
9800 if (stub_entry->h != NULL
9801 && (stub_entry->h == htab->tls_get_addr_fd
9802 || stub_entry->h == htab->tls_get_addr)
9803 && !htab->no_tls_get_addr_opt)
9804 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r,
9805 htab->plt_static_chain);
9806 else
9807 p = build_plt_stub (htab->stub_bfd, loc, off, r,
9808 htab->plt_static_chain);
9809 size = p - loc;
9810 break;
9811
9812 default:
9813 BFD_FAIL ();
9814 return FALSE;
9815 }
9816
9817 stub_entry->stub_sec->size += size;
9818
9819 if (htab->emit_stub_syms)
9820 {
9821 struct elf_link_hash_entry *h;
9822 size_t len1, len2;
9823 char *name;
9824 const char *const stub_str[] = { "long_branch",
9825 "long_branch_r2off",
9826 "plt_branch",
9827 "plt_branch_r2off",
9828 "plt_call" };
9829
9830 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9831 len2 = strlen (stub_entry->root.string);
9832 name = bfd_malloc (len1 + len2 + 2);
9833 if (name == NULL)
9834 return FALSE;
9835 memcpy (name, stub_entry->root.string, 9);
9836 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9837 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9838 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
9839 if (h == NULL)
9840 return FALSE;
9841 if (h->root.type == bfd_link_hash_new)
9842 {
9843 h->root.type = bfd_link_hash_defined;
9844 h->root.u.def.section = stub_entry->stub_sec;
9845 h->root.u.def.value = stub_entry->stub_offset;
9846 h->ref_regular = 1;
9847 h->def_regular = 1;
9848 h->ref_regular_nonweak = 1;
9849 h->forced_local = 1;
9850 h->non_elf = 0;
9851 }
9852 }
9853
9854 return TRUE;
9855 }
9856
9857 /* As above, but don't actually build the stub. Just bump offset so
9858 we know stub section sizes, and select plt_branch stubs where
9859 long_branch stubs won't do. */
9860
9861 static bfd_boolean
9862 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9863 {
9864 struct ppc_stub_hash_entry *stub_entry;
9865 struct bfd_link_info *info;
9866 struct ppc_link_hash_table *htab;
9867 bfd_vma off;
9868 int size;
9869
9870 /* Massage our args to the form they really have. */
9871 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9872 info = in_arg;
9873
9874 htab = ppc_hash_table (info);
9875 if (htab == NULL)
9876 return FALSE;
9877
9878 if (stub_entry->stub_type == ppc_stub_plt_call)
9879 {
9880 asection *plt;
9881 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
9882 if (off >= (bfd_vma) -2)
9883 abort ();
9884 plt = htab->plt;
9885 if (!htab->elf.dynamic_sections_created
9886 || stub_entry->h == NULL
9887 || stub_entry->h->elf.dynindx == -1)
9888 plt = htab->iplt;
9889 off += (plt->output_offset
9890 + plt->output_section->vma
9891 - elf_gp (plt->output_section->owner)
9892 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9893
9894 size = PLT_CALL_STUB_SIZE;
9895 if (!htab->plt_static_chain)
9896 size -= 4;
9897 if (PPC_HA (off) == 0)
9898 size -= 4;
9899 if (PPC_HA (off + 16) != PPC_HA (off))
9900 size += 4;
9901 if (stub_entry->h != NULL
9902 && (stub_entry->h == htab->tls_get_addr_fd
9903 || stub_entry->h == htab->tls_get_addr)
9904 && !htab->no_tls_get_addr_opt)
9905 size += 13 * 4;
9906 if (info->emitrelocations)
9907 {
9908 stub_entry->stub_sec->reloc_count
9909 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9910 stub_entry->stub_sec->flags |= SEC_RELOC;
9911 }
9912 }
9913 else
9914 {
9915 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9916 variants. */
9917 bfd_vma r2off = 0;
9918
9919 off = (stub_entry->target_value
9920 + stub_entry->target_section->output_offset
9921 + stub_entry->target_section->output_section->vma);
9922 off -= (stub_entry->stub_sec->size
9923 + stub_entry->stub_sec->output_offset
9924 + stub_entry->stub_sec->output_section->vma);
9925
9926 /* Reset the stub type from the plt variant in case we now
9927 can reach with a shorter stub. */
9928 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9929 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9930
9931 size = 4;
9932 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9933 {
9934 r2off = get_r2off (info, stub_entry);
9935 if (r2off == 0)
9936 {
9937 htab->stub_error = TRUE;
9938 return FALSE;
9939 }
9940 size = 12;
9941 if (PPC_HA (r2off) != 0)
9942 size = 16;
9943 off -= size - 4;
9944 }
9945
9946 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
9947 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9948 {
9949 struct ppc_branch_hash_entry *br_entry;
9950
9951 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9952 stub_entry->root.string + 9,
9953 TRUE, FALSE);
9954 if (br_entry == NULL)
9955 {
9956 info->callbacks->einfo (_("can't build branch stub `%s'\n"),
9957 stub_entry->root.string);
9958 htab->stub_error = TRUE;
9959 return FALSE;
9960 }
9961
9962 if (br_entry->iter != htab->stub_iteration)
9963 {
9964 br_entry->iter = htab->stub_iteration;
9965 br_entry->offset = htab->brlt->size;
9966 htab->brlt->size += 8;
9967
9968 if (htab->relbrlt != NULL)
9969 htab->relbrlt->size += sizeof (Elf64_External_Rela);
9970 else if (info->emitrelocations)
9971 {
9972 htab->brlt->reloc_count += 1;
9973 htab->brlt->flags |= SEC_RELOC;
9974 }
9975 }
9976
9977 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
9978 off = (br_entry->offset
9979 + htab->brlt->output_offset
9980 + htab->brlt->output_section->vma
9981 - elf_gp (htab->brlt->output_section->owner)
9982 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9983
9984 if (info->emitrelocations)
9985 {
9986 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9987 stub_entry->stub_sec->flags |= SEC_RELOC;
9988 }
9989
9990 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9991 {
9992 size = 12;
9993 if (PPC_HA (off) != 0)
9994 size = 16;
9995 }
9996 else
9997 {
9998 size = 20;
9999 if (PPC_HA (off) != 0)
10000 size += 4;
10001
10002 if (PPC_HA (r2off) != 0)
10003 size += 4;
10004 }
10005 }
10006 else if (info->emitrelocations)
10007 {
10008 stub_entry->stub_sec->reloc_count += 1;
10009 stub_entry->stub_sec->flags |= SEC_RELOC;
10010 }
10011 }
10012
10013 stub_entry->stub_sec->size += size;
10014 return TRUE;
10015 }
10016
10017 /* Set up various things so that we can make a list of input sections
10018 for each output section included in the link. Returns -1 on error,
10019 0 when no stubs will be needed, and 1 on success. */
10020
10021 int
10022 ppc64_elf_setup_section_lists
10023 (struct bfd_link_info *info,
10024 asection *(*add_stub_section) (const char *, asection *),
10025 void (*layout_sections_again) (void))
10026 {
10027 bfd *input_bfd;
10028 int top_id, top_index, id;
10029 asection *section;
10030 asection **input_list;
10031 bfd_size_type amt;
10032 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10033
10034 if (htab == NULL)
10035 return -1;
10036 /* Stash our params away. */
10037 htab->add_stub_section = add_stub_section;
10038 htab->layout_sections_again = layout_sections_again;
10039
10040 if (htab->brlt == NULL)
10041 return 0;
10042
10043 /* Find the top input section id. */
10044 for (input_bfd = info->input_bfds, top_id = 3;
10045 input_bfd != NULL;
10046 input_bfd = input_bfd->link_next)
10047 {
10048 for (section = input_bfd->sections;
10049 section != NULL;
10050 section = section->next)
10051 {
10052 if (top_id < section->id)
10053 top_id = section->id;
10054 }
10055 }
10056
10057 htab->top_id = top_id;
10058 amt = sizeof (struct map_stub) * (top_id + 1);
10059 htab->stub_group = bfd_zmalloc (amt);
10060 if (htab->stub_group == NULL)
10061 return -1;
10062
10063 /* Set toc_off for com, und, abs and ind sections. */
10064 for (id = 0; id < 3; id++)
10065 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10066
10067 /* We can't use output_bfd->section_count here to find the top output
10068 section index as some sections may have been removed, and
10069 strip_excluded_output_sections doesn't renumber the indices. */
10070 for (section = info->output_bfd->sections, top_index = 0;
10071 section != NULL;
10072 section = section->next)
10073 {
10074 if (top_index < section->index)
10075 top_index = section->index;
10076 }
10077
10078 htab->top_index = top_index;
10079 amt = sizeof (asection *) * (top_index + 1);
10080 input_list = bfd_zmalloc (amt);
10081 htab->input_list = input_list;
10082 if (input_list == NULL)
10083 return -1;
10084
10085 return 1;
10086 }
10087
10088 /* Set up for first pass at multitoc partitioning. */
10089
10090 void
10091 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10092 {
10093 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10094
10095 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10096 htab->toc_curr = elf_gp (info->output_bfd);
10097 htab->toc_bfd = NULL;
10098 htab->toc_first_sec = NULL;
10099 }
10100
10101 /* The linker repeatedly calls this function for each TOC input section
10102 and linker generated GOT section. Group input bfds such that the toc
10103 within a group is less than 64k in size. */
10104
10105 bfd_boolean
10106 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10107 {
10108 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10109 bfd_vma addr, off, limit;
10110
10111 if (htab == NULL)
10112 return FALSE;
10113
10114 if (!htab->second_toc_pass)
10115 {
10116 /* Keep track of the first .toc or .got section for this input bfd. */
10117 if (htab->toc_bfd != isec->owner)
10118 {
10119 htab->toc_bfd = isec->owner;
10120 htab->toc_first_sec = isec;
10121 }
10122
10123 addr = isec->output_offset + isec->output_section->vma;
10124 off = addr - htab->toc_curr;
10125 limit = 0x80008000;
10126 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10127 limit = 0x10000;
10128 if (off + isec->size > limit)
10129 {
10130 addr = (htab->toc_first_sec->output_offset
10131 + htab->toc_first_sec->output_section->vma);
10132 htab->toc_curr = addr;
10133 }
10134
10135 /* toc_curr is the base address of this toc group. Set elf_gp
10136 for the input section to be the offset relative to the
10137 output toc base plus 0x8000. Making the input elf_gp an
10138 offset allows us to move the toc as a whole without
10139 recalculating input elf_gp. */
10140 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10141 off += TOC_BASE_OFF;
10142
10143 /* Die if someone uses a linker script that doesn't keep input
10144 file .toc and .got together. */
10145 if (elf_gp (isec->owner) != 0
10146 && elf_gp (isec->owner) != off)
10147 return FALSE;
10148
10149 elf_gp (isec->owner) = off;
10150 return TRUE;
10151 }
10152
10153 /* During the second pass toc_first_sec points to the start of
10154 a toc group, and toc_curr is used to track the old elf_gp.
10155 We use toc_bfd to ensure we only look at each bfd once. */
10156 if (htab->toc_bfd == isec->owner)
10157 return TRUE;
10158 htab->toc_bfd = isec->owner;
10159
10160 if (htab->toc_first_sec == NULL
10161 || htab->toc_curr != elf_gp (isec->owner))
10162 {
10163 htab->toc_curr = elf_gp (isec->owner);
10164 htab->toc_first_sec = isec;
10165 }
10166 addr = (htab->toc_first_sec->output_offset
10167 + htab->toc_first_sec->output_section->vma);
10168 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10169 elf_gp (isec->owner) = off;
10170
10171 return TRUE;
10172 }
10173
10174 /* Called via elf_link_hash_traverse to merge GOT entries for global
10175 symbol H. */
10176
10177 static bfd_boolean
10178 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10179 {
10180 if (h->root.type == bfd_link_hash_indirect)
10181 return TRUE;
10182
10183 merge_got_entries (&h->got.glist);
10184
10185 return TRUE;
10186 }
10187
10188 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10189 symbol H. */
10190
10191 static bfd_boolean
10192 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10193 {
10194 struct got_entry *gent;
10195
10196 if (h->root.type == bfd_link_hash_indirect)
10197 return TRUE;
10198
10199 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10200 if (!gent->is_indirect)
10201 allocate_got (h, (struct bfd_link_info *) inf, gent);
10202 return TRUE;
10203 }
10204
10205 /* Called on the first multitoc pass after the last call to
10206 ppc64_elf_next_toc_section. This function removes duplicate GOT
10207 entries. */
10208
10209 bfd_boolean
10210 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10211 {
10212 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10213 struct bfd *ibfd, *ibfd2;
10214 bfd_boolean done_something;
10215
10216 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10217
10218 if (!htab->do_multi_toc)
10219 return FALSE;
10220
10221 /* Merge global sym got entries within a toc group. */
10222 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10223
10224 /* And tlsld_got. */
10225 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10226 {
10227 struct got_entry *ent, *ent2;
10228
10229 if (!is_ppc64_elf (ibfd))
10230 continue;
10231
10232 ent = ppc64_tlsld_got (ibfd);
10233 if (!ent->is_indirect
10234 && ent->got.offset != (bfd_vma) -1)
10235 {
10236 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10237 {
10238 if (!is_ppc64_elf (ibfd2))
10239 continue;
10240
10241 ent2 = ppc64_tlsld_got (ibfd2);
10242 if (!ent2->is_indirect
10243 && ent2->got.offset != (bfd_vma) -1
10244 && elf_gp (ibfd2) == elf_gp (ibfd))
10245 {
10246 ent2->is_indirect = TRUE;
10247 ent2->got.ent = ent;
10248 }
10249 }
10250 }
10251 }
10252
10253 /* Zap sizes of got sections. */
10254 htab->reliplt->rawsize = htab->reliplt->size;
10255 htab->reliplt->size -= htab->got_reli_size;
10256 htab->got_reli_size = 0;
10257
10258 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10259 {
10260 asection *got, *relgot;
10261
10262 if (!is_ppc64_elf (ibfd))
10263 continue;
10264
10265 got = ppc64_elf_tdata (ibfd)->got;
10266 if (got != NULL)
10267 {
10268 got->rawsize = got->size;
10269 got->size = 0;
10270 relgot = ppc64_elf_tdata (ibfd)->relgot;
10271 relgot->rawsize = relgot->size;
10272 relgot->size = 0;
10273 }
10274 }
10275
10276 /* Now reallocate the got, local syms first. We don't need to
10277 allocate section contents again since we never increase size. */
10278 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10279 {
10280 struct got_entry **lgot_ents;
10281 struct got_entry **end_lgot_ents;
10282 struct plt_entry **local_plt;
10283 struct plt_entry **end_local_plt;
10284 unsigned char *lgot_masks;
10285 bfd_size_type locsymcount;
10286 Elf_Internal_Shdr *symtab_hdr;
10287 asection *s, *srel;
10288
10289 if (!is_ppc64_elf (ibfd))
10290 continue;
10291
10292 lgot_ents = elf_local_got_ents (ibfd);
10293 if (!lgot_ents)
10294 continue;
10295
10296 symtab_hdr = &elf_symtab_hdr (ibfd);
10297 locsymcount = symtab_hdr->sh_info;
10298 end_lgot_ents = lgot_ents + locsymcount;
10299 local_plt = (struct plt_entry **) end_lgot_ents;
10300 end_local_plt = local_plt + locsymcount;
10301 lgot_masks = (unsigned char *) end_local_plt;
10302 s = ppc64_elf_tdata (ibfd)->got;
10303 srel = ppc64_elf_tdata (ibfd)->relgot;
10304 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10305 {
10306 struct got_entry *ent;
10307
10308 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10309 {
10310 unsigned int num = 1;
10311 ent->got.offset = s->size;
10312 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10313 num = 2;
10314 s->size += num * 8;
10315 if (info->shared)
10316 srel->size += num * sizeof (Elf64_External_Rela);
10317 else if ((*lgot_masks & PLT_IFUNC) != 0)
10318 {
10319 htab->reliplt->size
10320 += num * sizeof (Elf64_External_Rela);
10321 htab->got_reli_size
10322 += num * sizeof (Elf64_External_Rela);
10323 }
10324 }
10325 }
10326 }
10327
10328 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10329
10330 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10331 {
10332 struct got_entry *ent;
10333
10334 if (!is_ppc64_elf (ibfd))
10335 continue;
10336
10337 ent = ppc64_tlsld_got (ibfd);
10338 if (!ent->is_indirect
10339 && ent->got.offset != (bfd_vma) -1)
10340 {
10341 asection *s = ppc64_elf_tdata (ibfd)->got;
10342 ent->got.offset = s->size;
10343 s->size += 16;
10344 if (info->shared)
10345 {
10346 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10347 srel->size += sizeof (Elf64_External_Rela);
10348 }
10349 }
10350 }
10351
10352 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10353 if (!done_something)
10354 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10355 {
10356 asection *got;
10357
10358 if (!is_ppc64_elf (ibfd))
10359 continue;
10360
10361 got = ppc64_elf_tdata (ibfd)->got;
10362 if (got != NULL)
10363 {
10364 done_something = got->rawsize != got->size;
10365 if (done_something)
10366 break;
10367 }
10368 }
10369
10370 if (done_something)
10371 (*htab->layout_sections_again) ();
10372
10373 /* Set up for second pass over toc sections to recalculate elf_gp
10374 on input sections. */
10375 htab->toc_bfd = NULL;
10376 htab->toc_first_sec = NULL;
10377 htab->second_toc_pass = TRUE;
10378 return done_something;
10379 }
10380
10381 /* Called after second pass of multitoc partitioning. */
10382
10383 void
10384 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10385 {
10386 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10387
10388 /* After the second pass, toc_curr tracks the TOC offset used
10389 for code sections below in ppc64_elf_next_input_section. */
10390 htab->toc_curr = TOC_BASE_OFF;
10391 }
10392
10393 /* No toc references were found in ISEC. If the code in ISEC makes no
10394 calls, then there's no need to use toc adjusting stubs when branching
10395 into ISEC. Actually, indirect calls from ISEC are OK as they will
10396 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10397 needed, and 2 if a cyclical call-graph was found but no other reason
10398 for a stub was detected. If called from the top level, a return of
10399 2 means the same as a return of 0. */
10400
10401 static int
10402 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10403 {
10404 int ret;
10405
10406 /* Mark this section as checked. */
10407 isec->call_check_done = 1;
10408
10409 /* We know none of our code bearing sections will need toc stubs. */
10410 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10411 return 0;
10412
10413 if (isec->size == 0)
10414 return 0;
10415
10416 if (isec->output_section == NULL)
10417 return 0;
10418
10419 ret = 0;
10420 if (isec->reloc_count != 0)
10421 {
10422 Elf_Internal_Rela *relstart, *rel;
10423 Elf_Internal_Sym *local_syms;
10424 struct ppc_link_hash_table *htab;
10425
10426 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10427 info->keep_memory);
10428 if (relstart == NULL)
10429 return -1;
10430
10431 /* Look for branches to outside of this section. */
10432 local_syms = NULL;
10433 htab = ppc_hash_table (info);
10434 if (htab == NULL)
10435 return -1;
10436
10437 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10438 {
10439 enum elf_ppc64_reloc_type r_type;
10440 unsigned long r_symndx;
10441 struct elf_link_hash_entry *h;
10442 struct ppc_link_hash_entry *eh;
10443 Elf_Internal_Sym *sym;
10444 asection *sym_sec;
10445 struct _opd_sec_data *opd;
10446 bfd_vma sym_value;
10447 bfd_vma dest;
10448
10449 r_type = ELF64_R_TYPE (rel->r_info);
10450 if (r_type != R_PPC64_REL24
10451 && r_type != R_PPC64_REL14
10452 && r_type != R_PPC64_REL14_BRTAKEN
10453 && r_type != R_PPC64_REL14_BRNTAKEN)
10454 continue;
10455
10456 r_symndx = ELF64_R_SYM (rel->r_info);
10457 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10458 isec->owner))
10459 {
10460 ret = -1;
10461 break;
10462 }
10463
10464 /* Calls to dynamic lib functions go through a plt call stub
10465 that uses r2. */
10466 eh = (struct ppc_link_hash_entry *) h;
10467 if (eh != NULL
10468 && (eh->elf.plt.plist != NULL
10469 || (eh->oh != NULL
10470 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10471 {
10472 ret = 1;
10473 break;
10474 }
10475
10476 if (sym_sec == NULL)
10477 /* Ignore other undefined symbols. */
10478 continue;
10479
10480 /* Assume branches to other sections not included in the
10481 link need stubs too, to cover -R and absolute syms. */
10482 if (sym_sec->output_section == NULL)
10483 {
10484 ret = 1;
10485 break;
10486 }
10487
10488 if (h == NULL)
10489 sym_value = sym->st_value;
10490 else
10491 {
10492 if (h->root.type != bfd_link_hash_defined
10493 && h->root.type != bfd_link_hash_defweak)
10494 abort ();
10495 sym_value = h->root.u.def.value;
10496 }
10497 sym_value += rel->r_addend;
10498
10499 /* If this branch reloc uses an opd sym, find the code section. */
10500 opd = get_opd_info (sym_sec);
10501 if (opd != NULL)
10502 {
10503 if (h == NULL && opd->adjust != NULL)
10504 {
10505 long adjust;
10506
10507 adjust = opd->adjust[sym->st_value / 8];
10508 if (adjust == -1)
10509 /* Assume deleted functions won't ever be called. */
10510 continue;
10511 sym_value += adjust;
10512 }
10513
10514 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10515 if (dest == (bfd_vma) -1)
10516 continue;
10517 }
10518 else
10519 dest = (sym_value
10520 + sym_sec->output_offset
10521 + sym_sec->output_section->vma);
10522
10523 /* Ignore branch to self. */
10524 if (sym_sec == isec)
10525 continue;
10526
10527 /* If the called function uses the toc, we need a stub. */
10528 if (sym_sec->has_toc_reloc
10529 || sym_sec->makes_toc_func_call)
10530 {
10531 ret = 1;
10532 break;
10533 }
10534
10535 /* Assume any branch that needs a long branch stub might in fact
10536 need a plt_branch stub. A plt_branch stub uses r2. */
10537 else if (dest - (isec->output_offset
10538 + isec->output_section->vma
10539 + rel->r_offset) + (1 << 25) >= (2 << 25))
10540 {
10541 ret = 1;
10542 break;
10543 }
10544
10545 /* If calling back to a section in the process of being
10546 tested, we can't say for sure that no toc adjusting stubs
10547 are needed, so don't return zero. */
10548 else if (sym_sec->call_check_in_progress)
10549 ret = 2;
10550
10551 /* Branches to another section that itself doesn't have any TOC
10552 references are OK. Recursively call ourselves to check. */
10553 else if (!sym_sec->call_check_done)
10554 {
10555 int recur;
10556
10557 /* Mark current section as indeterminate, so that other
10558 sections that call back to current won't be marked as
10559 known. */
10560 isec->call_check_in_progress = 1;
10561 recur = toc_adjusting_stub_needed (info, sym_sec);
10562 isec->call_check_in_progress = 0;
10563
10564 if (recur != 0)
10565 {
10566 ret = recur;
10567 if (recur != 2)
10568 break;
10569 }
10570 }
10571 }
10572
10573 if (local_syms != NULL
10574 && (elf_symtab_hdr (isec->owner).contents
10575 != (unsigned char *) local_syms))
10576 free (local_syms);
10577 if (elf_section_data (isec)->relocs != relstart)
10578 free (relstart);
10579 }
10580
10581 if ((ret & 1) == 0
10582 && isec->map_head.s != NULL
10583 && (strcmp (isec->output_section->name, ".init") == 0
10584 || strcmp (isec->output_section->name, ".fini") == 0))
10585 {
10586 if (isec->map_head.s->has_toc_reloc
10587 || isec->map_head.s->makes_toc_func_call)
10588 ret = 1;
10589 else if (!isec->map_head.s->call_check_done)
10590 {
10591 int recur;
10592 isec->call_check_in_progress = 1;
10593 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10594 isec->call_check_in_progress = 0;
10595 if (recur != 0)
10596 ret = recur;
10597 }
10598 }
10599
10600 if (ret == 1)
10601 isec->makes_toc_func_call = 1;
10602
10603 return ret;
10604 }
10605
10606 /* The linker repeatedly calls this function for each input section,
10607 in the order that input sections are linked into output sections.
10608 Build lists of input sections to determine groupings between which
10609 we may insert linker stubs. */
10610
10611 bfd_boolean
10612 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10613 {
10614 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10615
10616 if (htab == NULL)
10617 return FALSE;
10618
10619 if ((isec->output_section->flags & SEC_CODE) != 0
10620 && isec->output_section->index <= htab->top_index)
10621 {
10622 asection **list = htab->input_list + isec->output_section->index;
10623 /* Steal the link_sec pointer for our list. */
10624 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10625 /* This happens to make the list in reverse order,
10626 which is what we want. */
10627 PREV_SEC (isec) = *list;
10628 *list = isec;
10629 }
10630
10631 if (htab->multi_toc_needed)
10632 {
10633 /* If a code section has a function that uses the TOC then we need
10634 to use the right TOC (obviously). Also, make sure that .opd gets
10635 the correct TOC value for R_PPC64_TOC relocs that don't have or
10636 can't find their function symbol (shouldn't ever happen now).
10637 Also specially treat .fixup for the linux kernel. .fixup
10638 contains branches, but only back to the function that hit an
10639 exception. */
10640 if (isec->has_toc_reloc
10641 || (isec->flags & SEC_CODE) == 0
10642 || strcmp (isec->name, ".fixup") == 0)
10643 {
10644 if (elf_gp (isec->owner) != 0)
10645 htab->toc_curr = elf_gp (isec->owner);
10646 }
10647 else
10648 {
10649 if (!isec->call_check_done
10650 && toc_adjusting_stub_needed (info, isec) < 0)
10651 return FALSE;
10652 /* If we make a local call from this section, ie. a branch
10653 without a following nop, then we have no place to put a
10654 toc restoring insn. We must use the same toc group as
10655 the callee.
10656 Testing makes_toc_func_call actually tests for *any*
10657 calls to functions that need a good toc pointer. A more
10658 precise test would be better, as this one will set
10659 incorrect values for pasted .init/.fini fragments.
10660 (Fixed later in check_pasted_section.) */
10661 if (isec->makes_toc_func_call
10662 && elf_gp (isec->owner) != 0)
10663 htab->toc_curr = elf_gp (isec->owner);
10664 }
10665 }
10666
10667 /* Functions that don't use the TOC can belong in any TOC group.
10668 Use the last TOC base. */
10669 htab->stub_group[isec->id].toc_off = htab->toc_curr;
10670 return TRUE;
10671 }
10672
10673 /* Check that all .init and .fini sections use the same toc, if they
10674 have toc relocs. */
10675
10676 static bfd_boolean
10677 check_pasted_section (struct bfd_link_info *info, const char *name)
10678 {
10679 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10680
10681 if (o != NULL)
10682 {
10683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10684 bfd_vma toc_off = 0;
10685 asection *i;
10686
10687 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10688 if (i->has_toc_reloc)
10689 {
10690 if (toc_off == 0)
10691 toc_off = htab->stub_group[i->id].toc_off;
10692 else if (toc_off != htab->stub_group[i->id].toc_off)
10693 return FALSE;
10694 }
10695
10696 if (toc_off == 0)
10697 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10698 if (i->makes_toc_func_call)
10699 {
10700 toc_off = htab->stub_group[i->id].toc_off;
10701 break;
10702 }
10703
10704 /* Make sure the whole pasted function uses the same toc offset. */
10705 if (toc_off != 0)
10706 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10707 htab->stub_group[i->id].toc_off = toc_off;
10708 }
10709 return TRUE;
10710 }
10711
10712 bfd_boolean
10713 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10714 {
10715 return (check_pasted_section (info, ".init")
10716 & check_pasted_section (info, ".fini"));
10717 }
10718
10719 /* See whether we can group stub sections together. Grouping stub
10720 sections may result in fewer stubs. More importantly, we need to
10721 put all .init* and .fini* stubs at the beginning of the .init or
10722 .fini output sections respectively, because glibc splits the
10723 _init and _fini functions into multiple parts. Putting a stub in
10724 the middle of a function is not a good idea. */
10725
10726 static void
10727 group_sections (struct ppc_link_hash_table *htab,
10728 bfd_size_type stub_group_size,
10729 bfd_boolean stubs_always_before_branch)
10730 {
10731 asection **list;
10732 bfd_size_type stub14_group_size;
10733 bfd_boolean suppress_size_errors;
10734
10735 suppress_size_errors = FALSE;
10736 stub14_group_size = stub_group_size;
10737 if (stub_group_size == 1)
10738 {
10739 /* Default values. */
10740 if (stubs_always_before_branch)
10741 {
10742 stub_group_size = 0x1e00000;
10743 stub14_group_size = 0x7800;
10744 }
10745 else
10746 {
10747 stub_group_size = 0x1c00000;
10748 stub14_group_size = 0x7000;
10749 }
10750 suppress_size_errors = TRUE;
10751 }
10752
10753 list = htab->input_list + htab->top_index;
10754 do
10755 {
10756 asection *tail = *list;
10757 while (tail != NULL)
10758 {
10759 asection *curr;
10760 asection *prev;
10761 bfd_size_type total;
10762 bfd_boolean big_sec;
10763 bfd_vma curr_toc;
10764
10765 curr = tail;
10766 total = tail->size;
10767 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10768 && ppc64_elf_section_data (tail)->has_14bit_branch
10769 ? stub14_group_size : stub_group_size);
10770 if (big_sec && !suppress_size_errors)
10771 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10772 tail->owner, tail);
10773 curr_toc = htab->stub_group[tail->id].toc_off;
10774
10775 while ((prev = PREV_SEC (curr)) != NULL
10776 && ((total += curr->output_offset - prev->output_offset)
10777 < (ppc64_elf_section_data (prev) != NULL
10778 && ppc64_elf_section_data (prev)->has_14bit_branch
10779 ? stub14_group_size : stub_group_size))
10780 && htab->stub_group[prev->id].toc_off == curr_toc)
10781 curr = prev;
10782
10783 /* OK, the size from the start of CURR to the end is less
10784 than stub_group_size and thus can be handled by one stub
10785 section. (or the tail section is itself larger than
10786 stub_group_size, in which case we may be toast.) We
10787 should really be keeping track of the total size of stubs
10788 added here, as stubs contribute to the final output
10789 section size. That's a little tricky, and this way will
10790 only break if stubs added make the total size more than
10791 2^25, ie. for the default stub_group_size, if stubs total
10792 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10793 do
10794 {
10795 prev = PREV_SEC (tail);
10796 /* Set up this stub group. */
10797 htab->stub_group[tail->id].link_sec = curr;
10798 }
10799 while (tail != curr && (tail = prev) != NULL);
10800
10801 /* But wait, there's more! Input sections up to stub_group_size
10802 bytes before the stub section can be handled by it too.
10803 Don't do this if we have a really large section after the
10804 stubs, as adding more stubs increases the chance that
10805 branches may not reach into the stub section. */
10806 if (!stubs_always_before_branch && !big_sec)
10807 {
10808 total = 0;
10809 while (prev != NULL
10810 && ((total += tail->output_offset - prev->output_offset)
10811 < (ppc64_elf_section_data (prev) != NULL
10812 && ppc64_elf_section_data (prev)->has_14bit_branch
10813 ? stub14_group_size : stub_group_size))
10814 && htab->stub_group[prev->id].toc_off == curr_toc)
10815 {
10816 tail = prev;
10817 prev = PREV_SEC (tail);
10818 htab->stub_group[tail->id].link_sec = curr;
10819 }
10820 }
10821 tail = prev;
10822 }
10823 }
10824 while (list-- != htab->input_list);
10825 free (htab->input_list);
10826 #undef PREV_SEC
10827 }
10828
10829 /* Determine and set the size of the stub section for a final link.
10830
10831 The basic idea here is to examine all the relocations looking for
10832 PC-relative calls to a target that is unreachable with a "bl"
10833 instruction. */
10834
10835 bfd_boolean
10836 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
10837 bfd_boolean plt_static_chain)
10838 {
10839 bfd_size_type stub_group_size;
10840 bfd_boolean stubs_always_before_branch;
10841 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10842
10843 if (htab == NULL)
10844 return FALSE;
10845
10846 htab->plt_static_chain = plt_static_chain;
10847 stubs_always_before_branch = group_size < 0;
10848 if (group_size < 0)
10849 stub_group_size = -group_size;
10850 else
10851 stub_group_size = group_size;
10852
10853 group_sections (htab, stub_group_size, stubs_always_before_branch);
10854
10855 while (1)
10856 {
10857 bfd *input_bfd;
10858 unsigned int bfd_indx;
10859 asection *stub_sec;
10860
10861 htab->stub_iteration += 1;
10862
10863 for (input_bfd = info->input_bfds, bfd_indx = 0;
10864 input_bfd != NULL;
10865 input_bfd = input_bfd->link_next, bfd_indx++)
10866 {
10867 Elf_Internal_Shdr *symtab_hdr;
10868 asection *section;
10869 Elf_Internal_Sym *local_syms = NULL;
10870
10871 if (!is_ppc64_elf (input_bfd))
10872 continue;
10873
10874 /* We'll need the symbol table in a second. */
10875 symtab_hdr = &elf_symtab_hdr (input_bfd);
10876 if (symtab_hdr->sh_info == 0)
10877 continue;
10878
10879 /* Walk over each section attached to the input bfd. */
10880 for (section = input_bfd->sections;
10881 section != NULL;
10882 section = section->next)
10883 {
10884 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
10885
10886 /* If there aren't any relocs, then there's nothing more
10887 to do. */
10888 if ((section->flags & SEC_RELOC) == 0
10889 || (section->flags & SEC_ALLOC) == 0
10890 || (section->flags & SEC_LOAD) == 0
10891 || (section->flags & SEC_CODE) == 0
10892 || section->reloc_count == 0)
10893 continue;
10894
10895 /* If this section is a link-once section that will be
10896 discarded, then don't create any stubs. */
10897 if (section->output_section == NULL
10898 || section->output_section->owner != info->output_bfd)
10899 continue;
10900
10901 /* Get the relocs. */
10902 internal_relocs
10903 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
10904 info->keep_memory);
10905 if (internal_relocs == NULL)
10906 goto error_ret_free_local;
10907
10908 /* Now examine each relocation. */
10909 irela = internal_relocs;
10910 irelaend = irela + section->reloc_count;
10911 for (; irela < irelaend; irela++)
10912 {
10913 enum elf_ppc64_reloc_type r_type;
10914 unsigned int r_indx;
10915 enum ppc_stub_type stub_type;
10916 struct ppc_stub_hash_entry *stub_entry;
10917 asection *sym_sec, *code_sec;
10918 bfd_vma sym_value, code_value;
10919 bfd_vma destination;
10920 bfd_boolean ok_dest;
10921 struct ppc_link_hash_entry *hash;
10922 struct ppc_link_hash_entry *fdh;
10923 struct elf_link_hash_entry *h;
10924 Elf_Internal_Sym *sym;
10925 char *stub_name;
10926 const asection *id_sec;
10927 struct _opd_sec_data *opd;
10928 struct plt_entry *plt_ent;
10929
10930 r_type = ELF64_R_TYPE (irela->r_info);
10931 r_indx = ELF64_R_SYM (irela->r_info);
10932
10933 if (r_type >= R_PPC64_max)
10934 {
10935 bfd_set_error (bfd_error_bad_value);
10936 goto error_ret_free_internal;
10937 }
10938
10939 /* Only look for stubs on branch instructions. */
10940 if (r_type != R_PPC64_REL24
10941 && r_type != R_PPC64_REL14
10942 && r_type != R_PPC64_REL14_BRTAKEN
10943 && r_type != R_PPC64_REL14_BRNTAKEN)
10944 continue;
10945
10946 /* Now determine the call target, its name, value,
10947 section. */
10948 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10949 r_indx, input_bfd))
10950 goto error_ret_free_internal;
10951 hash = (struct ppc_link_hash_entry *) h;
10952
10953 ok_dest = FALSE;
10954 fdh = NULL;
10955 sym_value = 0;
10956 if (hash == NULL)
10957 {
10958 sym_value = sym->st_value;
10959 ok_dest = TRUE;
10960 }
10961 else if (hash->elf.root.type == bfd_link_hash_defined
10962 || hash->elf.root.type == bfd_link_hash_defweak)
10963 {
10964 sym_value = hash->elf.root.u.def.value;
10965 if (sym_sec->output_section != NULL)
10966 ok_dest = TRUE;
10967 }
10968 else if (hash->elf.root.type == bfd_link_hash_undefweak
10969 || hash->elf.root.type == bfd_link_hash_undefined)
10970 {
10971 /* Recognise an old ABI func code entry sym, and
10972 use the func descriptor sym instead if it is
10973 defined. */
10974 if (hash->elf.root.root.string[0] == '.'
10975 && (fdh = lookup_fdh (hash, htab)) != NULL)
10976 {
10977 if (fdh->elf.root.type == bfd_link_hash_defined
10978 || fdh->elf.root.type == bfd_link_hash_defweak)
10979 {
10980 sym_sec = fdh->elf.root.u.def.section;
10981 sym_value = fdh->elf.root.u.def.value;
10982 if (sym_sec->output_section != NULL)
10983 ok_dest = TRUE;
10984 }
10985 else
10986 fdh = NULL;
10987 }
10988 }
10989 else
10990 {
10991 bfd_set_error (bfd_error_bad_value);
10992 goto error_ret_free_internal;
10993 }
10994
10995 destination = 0;
10996 if (ok_dest)
10997 {
10998 sym_value += irela->r_addend;
10999 destination = (sym_value
11000 + sym_sec->output_offset
11001 + sym_sec->output_section->vma);
11002 }
11003
11004 code_sec = sym_sec;
11005 code_value = sym_value;
11006 opd = get_opd_info (sym_sec);
11007 if (opd != NULL)
11008 {
11009 bfd_vma dest;
11010
11011 if (hash == NULL && opd->adjust != NULL)
11012 {
11013 long adjust = opd->adjust[sym_value / 8];
11014 if (adjust == -1)
11015 continue;
11016 code_value += adjust;
11017 sym_value += adjust;
11018 }
11019 dest = opd_entry_value (sym_sec, sym_value,
11020 &code_sec, &code_value);
11021 if (dest != (bfd_vma) -1)
11022 {
11023 destination = dest;
11024 if (fdh != NULL)
11025 {
11026 /* Fixup old ABI sym to point at code
11027 entry. */
11028 hash->elf.root.type = bfd_link_hash_defweak;
11029 hash->elf.root.u.def.section = code_sec;
11030 hash->elf.root.u.def.value = code_value;
11031 }
11032 }
11033 }
11034
11035 /* Determine what (if any) linker stub is needed. */
11036 plt_ent = NULL;
11037 stub_type = ppc_type_of_stub (section, irela, &hash,
11038 &plt_ent, destination);
11039
11040 if (stub_type != ppc_stub_plt_call)
11041 {
11042 /* Check whether we need a TOC adjusting stub.
11043 Since the linker pastes together pieces from
11044 different object files when creating the
11045 _init and _fini functions, it may be that a
11046 call to what looks like a local sym is in
11047 fact a call needing a TOC adjustment. */
11048 if (code_sec != NULL
11049 && code_sec->output_section != NULL
11050 && (htab->stub_group[code_sec->id].toc_off
11051 != htab->stub_group[section->id].toc_off)
11052 && (code_sec->has_toc_reloc
11053 || code_sec->makes_toc_func_call))
11054 stub_type = ppc_stub_long_branch_r2off;
11055 }
11056
11057 if (stub_type == ppc_stub_none)
11058 continue;
11059
11060 /* __tls_get_addr calls might be eliminated. */
11061 if (stub_type != ppc_stub_plt_call
11062 && hash != NULL
11063 && (hash == htab->tls_get_addr
11064 || hash == htab->tls_get_addr_fd)
11065 && section->has_tls_reloc
11066 && irela != internal_relocs)
11067 {
11068 /* Get tls info. */
11069 unsigned char *tls_mask;
11070
11071 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11072 irela - 1, input_bfd))
11073 goto error_ret_free_internal;
11074 if (*tls_mask != 0)
11075 continue;
11076 }
11077
11078 /* Support for grouping stub sections. */
11079 id_sec = htab->stub_group[section->id].link_sec;
11080
11081 /* Get the name of this stub. */
11082 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11083 if (!stub_name)
11084 goto error_ret_free_internal;
11085
11086 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11087 stub_name, FALSE, FALSE);
11088 if (stub_entry != NULL)
11089 {
11090 /* The proper stub has already been created. */
11091 free (stub_name);
11092 continue;
11093 }
11094
11095 stub_entry = ppc_add_stub (stub_name, section, info);
11096 if (stub_entry == NULL)
11097 {
11098 free (stub_name);
11099 error_ret_free_internal:
11100 if (elf_section_data (section)->relocs == NULL)
11101 free (internal_relocs);
11102 error_ret_free_local:
11103 if (local_syms != NULL
11104 && (symtab_hdr->contents
11105 != (unsigned char *) local_syms))
11106 free (local_syms);
11107 return FALSE;
11108 }
11109
11110 stub_entry->stub_type = stub_type;
11111 if (stub_type != ppc_stub_plt_call)
11112 {
11113 stub_entry->target_value = code_value;
11114 stub_entry->target_section = code_sec;
11115 }
11116 else
11117 {
11118 stub_entry->target_value = sym_value;
11119 stub_entry->target_section = sym_sec;
11120 }
11121 stub_entry->h = hash;
11122 stub_entry->plt_ent = plt_ent;
11123 stub_entry->addend = irela->r_addend;
11124
11125 if (stub_entry->h != NULL)
11126 htab->stub_globals += 1;
11127 }
11128
11129 /* We're done with the internal relocs, free them. */
11130 if (elf_section_data (section)->relocs != internal_relocs)
11131 free (internal_relocs);
11132 }
11133
11134 if (local_syms != NULL
11135 && symtab_hdr->contents != (unsigned char *) local_syms)
11136 {
11137 if (!info->keep_memory)
11138 free (local_syms);
11139 else
11140 symtab_hdr->contents = (unsigned char *) local_syms;
11141 }
11142 }
11143
11144 /* We may have added some stubs. Find out the new size of the
11145 stub sections. */
11146 for (stub_sec = htab->stub_bfd->sections;
11147 stub_sec != NULL;
11148 stub_sec = stub_sec->next)
11149 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11150 {
11151 stub_sec->rawsize = stub_sec->size;
11152 stub_sec->size = 0;
11153 stub_sec->reloc_count = 0;
11154 stub_sec->flags &= ~SEC_RELOC;
11155 }
11156
11157 htab->brlt->size = 0;
11158 htab->brlt->reloc_count = 0;
11159 htab->brlt->flags &= ~SEC_RELOC;
11160 if (htab->relbrlt != NULL)
11161 htab->relbrlt->size = 0;
11162
11163 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11164
11165 if (info->emitrelocations
11166 && htab->glink != NULL && htab->glink->size != 0)
11167 {
11168 htab->glink->reloc_count = 1;
11169 htab->glink->flags |= SEC_RELOC;
11170 }
11171
11172 for (stub_sec = htab->stub_bfd->sections;
11173 stub_sec != NULL;
11174 stub_sec = stub_sec->next)
11175 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11176 && stub_sec->rawsize != stub_sec->size)
11177 break;
11178
11179 /* Exit from this loop when no stubs have been added, and no stubs
11180 have changed size. */
11181 if (stub_sec == NULL)
11182 break;
11183
11184 /* Ask the linker to do its stuff. */
11185 (*htab->layout_sections_again) ();
11186 }
11187
11188 /* It would be nice to strip htab->brlt from the output if the
11189 section is empty, but it's too late. If we strip sections here,
11190 the dynamic symbol table is corrupted since the section symbol
11191 for the stripped section isn't written. */
11192
11193 return TRUE;
11194 }
11195
11196 /* Called after we have determined section placement. If sections
11197 move, we'll be called again. Provide a value for TOCstart. */
11198
11199 bfd_vma
11200 ppc64_elf_toc (bfd *obfd)
11201 {
11202 asection *s;
11203 bfd_vma TOCstart;
11204
11205 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11206 order. The TOC starts where the first of these sections starts. */
11207 s = bfd_get_section_by_name (obfd, ".got");
11208 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11209 s = bfd_get_section_by_name (obfd, ".toc");
11210 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11211 s = bfd_get_section_by_name (obfd, ".tocbss");
11212 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11213 s = bfd_get_section_by_name (obfd, ".plt");
11214 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11215 {
11216 /* This may happen for
11217 o references to TOC base (SYM@toc / TOC[tc0]) without a
11218 .toc directive
11219 o bad linker script
11220 o --gc-sections and empty TOC sections
11221
11222 FIXME: Warn user? */
11223
11224 /* Look for a likely section. We probably won't even be
11225 using TOCstart. */
11226 for (s = obfd->sections; s != NULL; s = s->next)
11227 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11228 | SEC_EXCLUDE))
11229 == (SEC_ALLOC | SEC_SMALL_DATA))
11230 break;
11231 if (s == NULL)
11232 for (s = obfd->sections; s != NULL; s = s->next)
11233 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11234 == (SEC_ALLOC | SEC_SMALL_DATA))
11235 break;
11236 if (s == NULL)
11237 for (s = obfd->sections; s != NULL; s = s->next)
11238 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11239 == SEC_ALLOC)
11240 break;
11241 if (s == NULL)
11242 for (s = obfd->sections; s != NULL; s = s->next)
11243 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11244 break;
11245 }
11246
11247 TOCstart = 0;
11248 if (s != NULL)
11249 TOCstart = s->output_section->vma + s->output_offset;
11250
11251 return TOCstart;
11252 }
11253
11254 /* Build all the stubs associated with the current output file.
11255 The stubs are kept in a hash table attached to the main linker
11256 hash table. This function is called via gldelf64ppc_finish. */
11257
11258 bfd_boolean
11259 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11260 struct bfd_link_info *info,
11261 char **stats)
11262 {
11263 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11264 asection *stub_sec;
11265 bfd_byte *p;
11266 int stub_sec_count = 0;
11267
11268 if (htab == NULL)
11269 return FALSE;
11270
11271 htab->emit_stub_syms = emit_stub_syms;
11272
11273 /* Allocate memory to hold the linker stubs. */
11274 for (stub_sec = htab->stub_bfd->sections;
11275 stub_sec != NULL;
11276 stub_sec = stub_sec->next)
11277 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11278 && stub_sec->size != 0)
11279 {
11280 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11281 if (stub_sec->contents == NULL)
11282 return FALSE;
11283 /* We want to check that built size is the same as calculated
11284 size. rawsize is a convenient location to use. */
11285 stub_sec->rawsize = stub_sec->size;
11286 stub_sec->size = 0;
11287 }
11288
11289 if (htab->glink != NULL && htab->glink->size != 0)
11290 {
11291 unsigned int indx;
11292 bfd_vma plt0;
11293
11294 /* Build the .glink plt call stub. */
11295 if (htab->emit_stub_syms)
11296 {
11297 struct elf_link_hash_entry *h;
11298 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11299 TRUE, FALSE, FALSE);
11300 if (h == NULL)
11301 return FALSE;
11302 if (h->root.type == bfd_link_hash_new)
11303 {
11304 h->root.type = bfd_link_hash_defined;
11305 h->root.u.def.section = htab->glink;
11306 h->root.u.def.value = 8;
11307 h->ref_regular = 1;
11308 h->def_regular = 1;
11309 h->ref_regular_nonweak = 1;
11310 h->forced_local = 1;
11311 h->non_elf = 0;
11312 }
11313 }
11314 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11315 if (info->emitrelocations)
11316 {
11317 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11318 if (r == NULL)
11319 return FALSE;
11320 r->r_offset = (htab->glink->output_offset
11321 + htab->glink->output_section->vma);
11322 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11323 r->r_addend = plt0;
11324 }
11325 p = htab->glink->contents;
11326 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11327 bfd_put_64 (htab->glink->owner, plt0, p);
11328 p += 8;
11329 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11330 p += 4;
11331 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11332 p += 4;
11333 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11334 p += 4;
11335 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11336 p += 4;
11337 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11338 p += 4;
11339 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11340 p += 4;
11341 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11342 p += 4;
11343 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11344 p += 4;
11345 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11346 p += 4;
11347 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11348 p += 4;
11349 bfd_put_32 (htab->glink->owner, BCTR, p);
11350 p += 4;
11351 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11352 {
11353 bfd_put_32 (htab->glink->owner, NOP, p);
11354 p += 4;
11355 }
11356
11357 /* Build the .glink lazy link call stubs. */
11358 indx = 0;
11359 while (p < htab->glink->contents + htab->glink->size)
11360 {
11361 if (indx < 0x8000)
11362 {
11363 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11364 p += 4;
11365 }
11366 else
11367 {
11368 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11369 p += 4;
11370 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11371 p += 4;
11372 }
11373 bfd_put_32 (htab->glink->owner,
11374 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11375 indx++;
11376 p += 4;
11377 }
11378 htab->glink->rawsize = p - htab->glink->contents;
11379 }
11380
11381 if (htab->brlt->size != 0)
11382 {
11383 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11384 htab->brlt->size);
11385 if (htab->brlt->contents == NULL)
11386 return FALSE;
11387 }
11388 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11389 {
11390 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11391 htab->relbrlt->size);
11392 if (htab->relbrlt->contents == NULL)
11393 return FALSE;
11394 }
11395
11396 /* Build the stubs as directed by the stub hash table. */
11397 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11398
11399 if (htab->relbrlt != NULL)
11400 htab->relbrlt->reloc_count = 0;
11401
11402 for (stub_sec = htab->stub_bfd->sections;
11403 stub_sec != NULL;
11404 stub_sec = stub_sec->next)
11405 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11406 {
11407 stub_sec_count += 1;
11408 if (stub_sec->rawsize != stub_sec->size)
11409 break;
11410 }
11411
11412 if (stub_sec != NULL
11413 || htab->glink->rawsize != htab->glink->size)
11414 {
11415 htab->stub_error = TRUE;
11416 info->callbacks->einfo (_("stubs don't match calculated size\n"));
11417 }
11418
11419 if (htab->stub_error)
11420 return FALSE;
11421
11422 if (stats != NULL)
11423 {
11424 *stats = bfd_malloc (500);
11425 if (*stats == NULL)
11426 return FALSE;
11427
11428 sprintf (*stats, _("linker stubs in %u group%s\n"
11429 " branch %lu\n"
11430 " toc adjust %lu\n"
11431 " long branch %lu\n"
11432 " long toc adj %lu\n"
11433 " plt call %lu"),
11434 stub_sec_count,
11435 stub_sec_count == 1 ? "" : "s",
11436 htab->stub_count[ppc_stub_long_branch - 1],
11437 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11438 htab->stub_count[ppc_stub_plt_branch - 1],
11439 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11440 htab->stub_count[ppc_stub_plt_call - 1]);
11441 }
11442 return TRUE;
11443 }
11444
11445 /* This function undoes the changes made by add_symbol_adjust. */
11446
11447 static bfd_boolean
11448 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11449 {
11450 struct ppc_link_hash_entry *eh;
11451
11452 if (h->root.type == bfd_link_hash_indirect)
11453 return TRUE;
11454
11455 eh = (struct ppc_link_hash_entry *) h;
11456 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11457 return TRUE;
11458
11459 eh->elf.root.type = bfd_link_hash_undefined;
11460 return TRUE;
11461 }
11462
11463 void
11464 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11465 {
11466 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11467
11468 if (htab != NULL)
11469 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11470 }
11471
11472 /* What to do when ld finds relocations against symbols defined in
11473 discarded sections. */
11474
11475 static unsigned int
11476 ppc64_elf_action_discarded (asection *sec)
11477 {
11478 if (strcmp (".opd", sec->name) == 0)
11479 return 0;
11480
11481 if (strcmp (".toc", sec->name) == 0)
11482 return 0;
11483
11484 if (strcmp (".toc1", sec->name) == 0)
11485 return 0;
11486
11487 return _bfd_elf_default_action_discarded (sec);
11488 }
11489
11490 /* REL points to a low-part reloc on a largetoc instruction sequence.
11491 Find the matching high-part reloc instruction and verify that it
11492 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11493 the high-part reloc. */
11494
11495 static const Elf_Internal_Rela *
11496 ha_reloc_match (const Elf_Internal_Rela *relocs,
11497 const Elf_Internal_Rela *rel,
11498 unsigned int *reg,
11499 bfd_boolean match_addend,
11500 const bfd *input_bfd,
11501 const bfd_byte *contents)
11502 {
11503 enum elf_ppc64_reloc_type r_type, r_type_ha;
11504 bfd_vma r_info_ha, r_addend;
11505
11506 r_type = ELF64_R_TYPE (rel->r_info);
11507 switch (r_type)
11508 {
11509 case R_PPC64_GOT_TLSLD16_LO:
11510 case R_PPC64_GOT_TLSGD16_LO:
11511 case R_PPC64_GOT_TPREL16_LO_DS:
11512 case R_PPC64_GOT_DTPREL16_LO_DS:
11513 case R_PPC64_GOT16_LO:
11514 case R_PPC64_TOC16_LO:
11515 r_type_ha = r_type + 2;
11516 break;
11517 case R_PPC64_GOT16_LO_DS:
11518 r_type_ha = R_PPC64_GOT16_HA;
11519 break;
11520 case R_PPC64_TOC16_LO_DS:
11521 r_type_ha = R_PPC64_TOC16_HA;
11522 break;
11523 default:
11524 abort ();
11525 }
11526 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11527 r_addend = rel->r_addend;
11528
11529 while (--rel >= relocs)
11530 if (rel->r_info == r_info_ha
11531 && (!match_addend
11532 || rel->r_addend == r_addend))
11533 {
11534 const bfd_byte *p = contents + (rel->r_offset & ~3);
11535 unsigned int insn = bfd_get_32 (input_bfd, p);
11536 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11537 && (insn & (0x1f << 21)) == (*reg << 21))
11538 {
11539 *reg = (insn >> 16) & 0x1f;
11540 return rel;
11541 }
11542 break;
11543 }
11544 return NULL;
11545 }
11546
11547 /* The RELOCATE_SECTION function is called by the ELF backend linker
11548 to handle the relocations for a section.
11549
11550 The relocs are always passed as Rela structures; if the section
11551 actually uses Rel structures, the r_addend field will always be
11552 zero.
11553
11554 This function is responsible for adjust the section contents as
11555 necessary, and (if using Rela relocs and generating a
11556 relocatable output file) adjusting the reloc addend as
11557 necessary.
11558
11559 This function does not have to worry about setting the reloc
11560 address or the reloc symbol index.
11561
11562 LOCAL_SYMS is a pointer to the swapped in local symbols.
11563
11564 LOCAL_SECTIONS is an array giving the section in the input file
11565 corresponding to the st_shndx field of each local symbol.
11566
11567 The global hash table entry for the global symbols can be found
11568 via elf_sym_hashes (input_bfd).
11569
11570 When generating relocatable output, this function must handle
11571 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11572 going to be the section symbol corresponding to the output
11573 section, which means that the addend must be adjusted
11574 accordingly. */
11575
11576 static bfd_boolean
11577 ppc64_elf_relocate_section (bfd *output_bfd,
11578 struct bfd_link_info *info,
11579 bfd *input_bfd,
11580 asection *input_section,
11581 bfd_byte *contents,
11582 Elf_Internal_Rela *relocs,
11583 Elf_Internal_Sym *local_syms,
11584 asection **local_sections)
11585 {
11586 struct ppc_link_hash_table *htab;
11587 Elf_Internal_Shdr *symtab_hdr;
11588 struct elf_link_hash_entry **sym_hashes;
11589 Elf_Internal_Rela *rel;
11590 Elf_Internal_Rela *relend;
11591 Elf_Internal_Rela outrel;
11592 bfd_byte *loc;
11593 struct got_entry **local_got_ents;
11594 unsigned char *ha_opt;
11595 bfd_vma TOCstart;
11596 bfd_boolean no_ha_opt;
11597 bfd_boolean ret = TRUE;
11598 bfd_boolean is_opd;
11599 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
11600 bfd_boolean is_power4 = FALSE;
11601 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11602
11603 /* Initialize howto table if needed. */
11604 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11605 ppc_howto_init ();
11606
11607 htab = ppc_hash_table (info);
11608 if (htab == NULL)
11609 return FALSE;
11610
11611 /* Don't relocate stub sections. */
11612 if (input_section->owner == htab->stub_bfd)
11613 return TRUE;
11614
11615 BFD_ASSERT (is_ppc64_elf (input_bfd));
11616
11617 local_got_ents = elf_local_got_ents (input_bfd);
11618 TOCstart = elf_gp (output_bfd);
11619 symtab_hdr = &elf_symtab_hdr (input_bfd);
11620 sym_hashes = elf_sym_hashes (input_bfd);
11621 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11622 ha_opt = NULL;
11623 no_ha_opt = FALSE;
11624
11625 rel = relocs;
11626 relend = relocs + input_section->reloc_count;
11627 for (; rel < relend; rel++)
11628 {
11629 enum elf_ppc64_reloc_type r_type;
11630 bfd_vma addend, orig_addend;
11631 bfd_reloc_status_type r;
11632 Elf_Internal_Sym *sym;
11633 asection *sec;
11634 struct elf_link_hash_entry *h_elf;
11635 struct ppc_link_hash_entry *h;
11636 struct ppc_link_hash_entry *fdh;
11637 const char *sym_name;
11638 unsigned long r_symndx, toc_symndx;
11639 bfd_vma toc_addend;
11640 unsigned char tls_mask, tls_gd, tls_type;
11641 unsigned char sym_type;
11642 bfd_vma relocation;
11643 bfd_boolean unresolved_reloc;
11644 bfd_boolean warned;
11645 unsigned int insn;
11646 unsigned int mask;
11647 struct ppc_stub_hash_entry *stub_entry;
11648 bfd_vma max_br_offset;
11649 bfd_vma from;
11650
11651 r_type = ELF64_R_TYPE (rel->r_info);
11652 r_symndx = ELF64_R_SYM (rel->r_info);
11653
11654 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11655 symbol of the previous ADDR64 reloc. The symbol gives us the
11656 proper TOC base to use. */
11657 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11658 && rel != relocs
11659 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11660 && is_opd)
11661 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11662
11663 sym = NULL;
11664 sec = NULL;
11665 h_elf = NULL;
11666 sym_name = NULL;
11667 unresolved_reloc = FALSE;
11668 warned = FALSE;
11669 orig_addend = rel->r_addend;
11670
11671 if (r_symndx < symtab_hdr->sh_info)
11672 {
11673 /* It's a local symbol. */
11674 struct _opd_sec_data *opd;
11675
11676 sym = local_syms + r_symndx;
11677 sec = local_sections[r_symndx];
11678 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11679 sym_type = ELF64_ST_TYPE (sym->st_info);
11680 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11681 opd = get_opd_info (sec);
11682 if (opd != NULL && opd->adjust != NULL)
11683 {
11684 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11685 if (adjust == -1)
11686 relocation = 0;
11687 else
11688 {
11689 /* If this is a relocation against the opd section sym
11690 and we have edited .opd, adjust the reloc addend so
11691 that ld -r and ld --emit-relocs output is correct.
11692 If it is a reloc against some other .opd symbol,
11693 then the symbol value will be adjusted later. */
11694 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11695 rel->r_addend += adjust;
11696 else
11697 relocation += adjust;
11698 }
11699 }
11700 }
11701 else
11702 {
11703 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11704 r_symndx, symtab_hdr, sym_hashes,
11705 h_elf, sec, relocation,
11706 unresolved_reloc, warned);
11707 sym_name = h_elf->root.root.string;
11708 sym_type = h_elf->type;
11709 }
11710 h = (struct ppc_link_hash_entry *) h_elf;
11711
11712 if (sec != NULL && elf_discarded_section (sec))
11713 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11714 rel, relend,
11715 ppc64_elf_howto_table[r_type],
11716 contents);
11717
11718 if (info->relocatable)
11719 continue;
11720
11721 /* TLS optimizations. Replace instruction sequences and relocs
11722 based on information we collected in tls_optimize. We edit
11723 RELOCS so that --emit-relocs will output something sensible
11724 for the final instruction stream. */
11725 tls_mask = 0;
11726 tls_gd = 0;
11727 toc_symndx = 0;
11728 if (h != NULL)
11729 tls_mask = h->tls_mask;
11730 else if (local_got_ents != NULL)
11731 {
11732 struct plt_entry **local_plt = (struct plt_entry **)
11733 (local_got_ents + symtab_hdr->sh_info);
11734 unsigned char *lgot_masks = (unsigned char *)
11735 (local_plt + symtab_hdr->sh_info);
11736 tls_mask = lgot_masks[r_symndx];
11737 }
11738 if (tls_mask == 0
11739 && (r_type == R_PPC64_TLS
11740 || r_type == R_PPC64_TLSGD
11741 || r_type == R_PPC64_TLSLD))
11742 {
11743 /* Check for toc tls entries. */
11744 unsigned char *toc_tls;
11745
11746 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11747 &local_syms, rel, input_bfd))
11748 return FALSE;
11749
11750 if (toc_tls)
11751 tls_mask = *toc_tls;
11752 }
11753
11754 /* Check that tls relocs are used with tls syms, and non-tls
11755 relocs are used with non-tls syms. */
11756 if (r_symndx != STN_UNDEF
11757 && r_type != R_PPC64_NONE
11758 && (h == NULL
11759 || h->elf.root.type == bfd_link_hash_defined
11760 || h->elf.root.type == bfd_link_hash_defweak)
11761 && (IS_PPC64_TLS_RELOC (r_type)
11762 != (sym_type == STT_TLS
11763 || (sym_type == STT_SECTION
11764 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
11765 {
11766 if (tls_mask != 0
11767 && (r_type == R_PPC64_TLS
11768 || r_type == R_PPC64_TLSGD
11769 || r_type == R_PPC64_TLSLD))
11770 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11771 ;
11772 else
11773 info->callbacks->einfo
11774 (!IS_PPC64_TLS_RELOC (r_type)
11775 ? _("%H: %s used with TLS symbol %s\n")
11776 : _("%H: %s used with non-TLS symbol %s\n"),
11777 input_bfd, input_section, rel->r_offset,
11778 ppc64_elf_howto_table[r_type]->name,
11779 sym_name);
11780 }
11781
11782 /* Ensure reloc mapping code below stays sane. */
11783 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11784 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11785 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11786 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11787 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11788 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11789 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11790 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11791 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11792 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11793 abort ();
11794
11795 switch (r_type)
11796 {
11797 default:
11798 break;
11799
11800 case R_PPC64_LO_DS_OPT:
11801 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11802 if ((insn & (0x3f << 26)) != 58u << 26)
11803 abort ();
11804 insn += (14u << 26) - (58u << 26);
11805 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11806 r_type = R_PPC64_TOC16_LO;
11807 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11808 break;
11809
11810 case R_PPC64_TOC16:
11811 case R_PPC64_TOC16_LO:
11812 case R_PPC64_TOC16_DS:
11813 case R_PPC64_TOC16_LO_DS:
11814 {
11815 /* Check for toc tls entries. */
11816 unsigned char *toc_tls;
11817 int retval;
11818
11819 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11820 &local_syms, rel, input_bfd);
11821 if (retval == 0)
11822 return FALSE;
11823
11824 if (toc_tls)
11825 {
11826 tls_mask = *toc_tls;
11827 if (r_type == R_PPC64_TOC16_DS
11828 || r_type == R_PPC64_TOC16_LO_DS)
11829 {
11830 if (tls_mask != 0
11831 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11832 goto toctprel;
11833 }
11834 else
11835 {
11836 /* If we found a GD reloc pair, then we might be
11837 doing a GD->IE transition. */
11838 if (retval == 2)
11839 {
11840 tls_gd = TLS_TPRELGD;
11841 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11842 goto tls_ldgd_opt;
11843 }
11844 else if (retval == 3)
11845 {
11846 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11847 goto tls_ldgd_opt;
11848 }
11849 }
11850 }
11851 }
11852 break;
11853
11854 case R_PPC64_GOT_TPREL16_HI:
11855 case R_PPC64_GOT_TPREL16_HA:
11856 if (tls_mask != 0
11857 && (tls_mask & TLS_TPREL) == 0)
11858 {
11859 rel->r_offset -= d_offset;
11860 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11861 r_type = R_PPC64_NONE;
11862 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11863 }
11864 break;
11865
11866 case R_PPC64_GOT_TPREL16_DS:
11867 case R_PPC64_GOT_TPREL16_LO_DS:
11868 if (tls_mask != 0
11869 && (tls_mask & TLS_TPREL) == 0)
11870 {
11871 toctprel:
11872 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11873 insn &= 31 << 21;
11874 insn |= 0x3c0d0000; /* addis 0,13,0 */
11875 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11876 r_type = R_PPC64_TPREL16_HA;
11877 if (toc_symndx != 0)
11878 {
11879 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11880 rel->r_addend = toc_addend;
11881 /* We changed the symbol. Start over in order to
11882 get h, sym, sec etc. right. */
11883 rel--;
11884 continue;
11885 }
11886 else
11887 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11888 }
11889 break;
11890
11891 case R_PPC64_TLS:
11892 if (tls_mask != 0
11893 && (tls_mask & TLS_TPREL) == 0)
11894 {
11895 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
11896 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11897 if (insn == 0)
11898 abort ();
11899 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
11900 /* Was PPC64_TLS which sits on insn boundary, now
11901 PPC64_TPREL16_LO which is at low-order half-word. */
11902 rel->r_offset += d_offset;
11903 r_type = R_PPC64_TPREL16_LO;
11904 if (toc_symndx != 0)
11905 {
11906 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11907 rel->r_addend = toc_addend;
11908 /* We changed the symbol. Start over in order to
11909 get h, sym, sec etc. right. */
11910 rel--;
11911 continue;
11912 }
11913 else
11914 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11915 }
11916 break;
11917
11918 case R_PPC64_GOT_TLSGD16_HI:
11919 case R_PPC64_GOT_TLSGD16_HA:
11920 tls_gd = TLS_TPRELGD;
11921 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11922 goto tls_gdld_hi;
11923 break;
11924
11925 case R_PPC64_GOT_TLSLD16_HI:
11926 case R_PPC64_GOT_TLSLD16_HA:
11927 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11928 {
11929 tls_gdld_hi:
11930 if ((tls_mask & tls_gd) != 0)
11931 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11932 + R_PPC64_GOT_TPREL16_DS);
11933 else
11934 {
11935 rel->r_offset -= d_offset;
11936 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11937 r_type = R_PPC64_NONE;
11938 }
11939 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11940 }
11941 break;
11942
11943 case R_PPC64_GOT_TLSGD16:
11944 case R_PPC64_GOT_TLSGD16_LO:
11945 tls_gd = TLS_TPRELGD;
11946 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11947 goto tls_ldgd_opt;
11948 break;
11949
11950 case R_PPC64_GOT_TLSLD16:
11951 case R_PPC64_GOT_TLSLD16_LO:
11952 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11953 {
11954 unsigned int insn1, insn2, insn3;
11955 bfd_vma offset;
11956
11957 tls_ldgd_opt:
11958 offset = (bfd_vma) -1;
11959 /* If not using the newer R_PPC64_TLSGD/LD to mark
11960 __tls_get_addr calls, we must trust that the call
11961 stays with its arg setup insns, ie. that the next
11962 reloc is the __tls_get_addr call associated with
11963 the current reloc. Edit both insns. */
11964 if (input_section->has_tls_get_addr_call
11965 && rel + 1 < relend
11966 && branch_reloc_hash_match (input_bfd, rel + 1,
11967 htab->tls_get_addr,
11968 htab->tls_get_addr_fd))
11969 offset = rel[1].r_offset;
11970 if ((tls_mask & tls_gd) != 0)
11971 {
11972 /* IE */
11973 insn1 = bfd_get_32 (output_bfd,
11974 contents + rel->r_offset - d_offset);
11975 insn1 &= (1 << 26) - (1 << 2);
11976 insn1 |= 58 << 26; /* ld */
11977 insn2 = 0x7c636a14; /* add 3,3,13 */
11978 if (offset != (bfd_vma) -1)
11979 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
11980 if ((tls_mask & TLS_EXPLICIT) == 0)
11981 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11982 + R_PPC64_GOT_TPREL16_DS);
11983 else
11984 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11985 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11986 }
11987 else
11988 {
11989 /* LE */
11990 insn1 = 0x3c6d0000; /* addis 3,13,0 */
11991 insn2 = 0x38630000; /* addi 3,3,0 */
11992 if (tls_gd == 0)
11993 {
11994 /* Was an LD reloc. */
11995 if (toc_symndx)
11996 sec = local_sections[toc_symndx];
11997 for (r_symndx = 0;
11998 r_symndx < symtab_hdr->sh_info;
11999 r_symndx++)
12000 if (local_sections[r_symndx] == sec)
12001 break;
12002 if (r_symndx >= symtab_hdr->sh_info)
12003 r_symndx = STN_UNDEF;
12004 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12005 if (r_symndx != STN_UNDEF)
12006 rel->r_addend -= (local_syms[r_symndx].st_value
12007 + sec->output_offset
12008 + sec->output_section->vma);
12009 }
12010 else if (toc_symndx != 0)
12011 {
12012 r_symndx = toc_symndx;
12013 rel->r_addend = toc_addend;
12014 }
12015 r_type = R_PPC64_TPREL16_HA;
12016 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12017 if (offset != (bfd_vma) -1)
12018 {
12019 rel[1].r_info = ELF64_R_INFO (r_symndx,
12020 R_PPC64_TPREL16_LO);
12021 rel[1].r_offset = offset + d_offset;
12022 rel[1].r_addend = rel->r_addend;
12023 }
12024 }
12025 bfd_put_32 (output_bfd, insn1,
12026 contents + rel->r_offset - d_offset);
12027 if (offset != (bfd_vma) -1)
12028 {
12029 insn3 = bfd_get_32 (output_bfd,
12030 contents + offset + 4);
12031 if (insn3 == NOP
12032 || insn3 == CROR_151515 || insn3 == CROR_313131)
12033 {
12034 rel[1].r_offset += 4;
12035 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12036 insn2 = NOP;
12037 }
12038 bfd_put_32 (output_bfd, insn2, contents + offset);
12039 }
12040 if ((tls_mask & tls_gd) == 0
12041 && (tls_gd == 0 || toc_symndx != 0))
12042 {
12043 /* We changed the symbol. Start over in order
12044 to get h, sym, sec etc. right. */
12045 rel--;
12046 continue;
12047 }
12048 }
12049 break;
12050
12051 case R_PPC64_TLSGD:
12052 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12053 {
12054 unsigned int insn2, insn3;
12055 bfd_vma offset = rel->r_offset;
12056
12057 if ((tls_mask & TLS_TPRELGD) != 0)
12058 {
12059 /* IE */
12060 r_type = R_PPC64_NONE;
12061 insn2 = 0x7c636a14; /* add 3,3,13 */
12062 }
12063 else
12064 {
12065 /* LE */
12066 if (toc_symndx != 0)
12067 {
12068 r_symndx = toc_symndx;
12069 rel->r_addend = toc_addend;
12070 }
12071 r_type = R_PPC64_TPREL16_LO;
12072 rel->r_offset = offset + d_offset;
12073 insn2 = 0x38630000; /* addi 3,3,0 */
12074 }
12075 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12076 /* Zap the reloc on the _tls_get_addr call too. */
12077 BFD_ASSERT (offset == rel[1].r_offset);
12078 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12079 insn3 = bfd_get_32 (output_bfd,
12080 contents + offset + 4);
12081 if (insn3 == NOP
12082 || insn3 == CROR_151515 || insn3 == CROR_313131)
12083 {
12084 rel->r_offset += 4;
12085 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12086 insn2 = NOP;
12087 }
12088 bfd_put_32 (output_bfd, insn2, contents + offset);
12089 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12090 {
12091 rel--;
12092 continue;
12093 }
12094 }
12095 break;
12096
12097 case R_PPC64_TLSLD:
12098 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12099 {
12100 unsigned int insn2, insn3;
12101 bfd_vma offset = rel->r_offset;
12102
12103 if (toc_symndx)
12104 sec = local_sections[toc_symndx];
12105 for (r_symndx = 0;
12106 r_symndx < symtab_hdr->sh_info;
12107 r_symndx++)
12108 if (local_sections[r_symndx] == sec)
12109 break;
12110 if (r_symndx >= symtab_hdr->sh_info)
12111 r_symndx = STN_UNDEF;
12112 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12113 if (r_symndx != STN_UNDEF)
12114 rel->r_addend -= (local_syms[r_symndx].st_value
12115 + sec->output_offset
12116 + sec->output_section->vma);
12117
12118 r_type = R_PPC64_TPREL16_LO;
12119 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12120 rel->r_offset = offset + d_offset;
12121 /* Zap the reloc on the _tls_get_addr call too. */
12122 BFD_ASSERT (offset == rel[1].r_offset);
12123 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12124 insn2 = 0x38630000; /* addi 3,3,0 */
12125 insn3 = bfd_get_32 (output_bfd,
12126 contents + offset + 4);
12127 if (insn3 == NOP
12128 || insn3 == CROR_151515 || insn3 == CROR_313131)
12129 {
12130 rel->r_offset += 4;
12131 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12132 insn2 = NOP;
12133 }
12134 bfd_put_32 (output_bfd, insn2, contents + offset);
12135 rel--;
12136 continue;
12137 }
12138 break;
12139
12140 case R_PPC64_DTPMOD64:
12141 if (rel + 1 < relend
12142 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12143 && rel[1].r_offset == rel->r_offset + 8)
12144 {
12145 if ((tls_mask & TLS_GD) == 0)
12146 {
12147 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12148 if ((tls_mask & TLS_TPRELGD) != 0)
12149 r_type = R_PPC64_TPREL64;
12150 else
12151 {
12152 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12153 r_type = R_PPC64_NONE;
12154 }
12155 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12156 }
12157 }
12158 else
12159 {
12160 if ((tls_mask & TLS_LD) == 0)
12161 {
12162 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12163 r_type = R_PPC64_NONE;
12164 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12165 }
12166 }
12167 break;
12168
12169 case R_PPC64_TPREL64:
12170 if ((tls_mask & TLS_TPREL) == 0)
12171 {
12172 r_type = R_PPC64_NONE;
12173 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12174 }
12175 break;
12176 }
12177
12178 /* Handle other relocations that tweak non-addend part of insn. */
12179 insn = 0;
12180 max_br_offset = 1 << 25;
12181 addend = rel->r_addend;
12182 switch (r_type)
12183 {
12184 default:
12185 break;
12186
12187 /* Branch taken prediction relocations. */
12188 case R_PPC64_ADDR14_BRTAKEN:
12189 case R_PPC64_REL14_BRTAKEN:
12190 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12191 /* Fall thru. */
12192
12193 /* Branch not taken prediction relocations. */
12194 case R_PPC64_ADDR14_BRNTAKEN:
12195 case R_PPC64_REL14_BRNTAKEN:
12196 insn |= bfd_get_32 (output_bfd,
12197 contents + rel->r_offset) & ~(0x01 << 21);
12198 /* Fall thru. */
12199
12200 case R_PPC64_REL14:
12201 max_br_offset = 1 << 15;
12202 /* Fall thru. */
12203
12204 case R_PPC64_REL24:
12205 /* Calls to functions with a different TOC, such as calls to
12206 shared objects, need to alter the TOC pointer. This is
12207 done using a linkage stub. A REL24 branching to these
12208 linkage stubs needs to be followed by a nop, as the nop
12209 will be replaced with an instruction to restore the TOC
12210 base pointer. */
12211 fdh = h;
12212 if (h != NULL
12213 && h->oh != NULL
12214 && h->oh->is_func_descriptor)
12215 fdh = ppc_follow_link (h->oh);
12216 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12217 if (stub_entry != NULL
12218 && (stub_entry->stub_type == ppc_stub_plt_call
12219 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12220 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12221 {
12222 bfd_boolean can_plt_call = FALSE;
12223
12224 if (rel->r_offset + 8 <= input_section->size)
12225 {
12226 unsigned long nop;
12227 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12228 if (nop == NOP
12229 || nop == CROR_151515 || nop == CROR_313131)
12230 {
12231 if (h != NULL
12232 && (h == htab->tls_get_addr_fd
12233 || h == htab->tls_get_addr)
12234 && !htab->no_tls_get_addr_opt)
12235 {
12236 /* Special stub used, leave nop alone. */
12237 }
12238 else
12239 bfd_put_32 (input_bfd, LD_R2_40R1,
12240 contents + rel->r_offset + 4);
12241 can_plt_call = TRUE;
12242 }
12243 }
12244
12245 if (!can_plt_call)
12246 {
12247 if (stub_entry->stub_type == ppc_stub_plt_call)
12248 {
12249 /* If this is a plain branch rather than a branch
12250 and link, don't require a nop. However, don't
12251 allow tail calls in a shared library as they
12252 will result in r2 being corrupted. */
12253 unsigned long br;
12254 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12255 if (info->executable && (br & 1) == 0)
12256 can_plt_call = TRUE;
12257 else
12258 stub_entry = NULL;
12259 }
12260 else if (h != NULL
12261 && strcmp (h->elf.root.root.string,
12262 ".__libc_start_main") == 0)
12263 {
12264 /* Allow crt1 branch to go via a toc adjusting stub. */
12265 can_plt_call = TRUE;
12266 }
12267 else
12268 {
12269 if (strcmp (input_section->output_section->name,
12270 ".init") == 0
12271 || strcmp (input_section->output_section->name,
12272 ".fini") == 0)
12273 info->callbacks->einfo
12274 (_("%H: automatic multiple TOCs "
12275 "not supported using your crt files; "
12276 "recompile with -mminimal-toc or upgrade gcc\n"),
12277 input_bfd, input_section, rel->r_offset);
12278 else
12279 info->callbacks->einfo
12280 (_("%H: sibling call optimization to `%s' "
12281 "does not allow automatic multiple TOCs; "
12282 "recompile with -mminimal-toc or "
12283 "-fno-optimize-sibling-calls, "
12284 "or make `%s' extern\n"),
12285 input_bfd, input_section, rel->r_offset,
12286 sym_name,
12287 sym_name);
12288 bfd_set_error (bfd_error_bad_value);
12289 ret = FALSE;
12290 }
12291 }
12292
12293 if (can_plt_call
12294 && stub_entry->stub_type == ppc_stub_plt_call)
12295 unresolved_reloc = FALSE;
12296 }
12297
12298 if ((stub_entry == NULL
12299 || stub_entry->stub_type == ppc_stub_long_branch
12300 || stub_entry->stub_type == ppc_stub_plt_branch)
12301 && get_opd_info (sec) != NULL)
12302 {
12303 /* The branch destination is the value of the opd entry. */
12304 bfd_vma off = (relocation + addend
12305 - sec->output_section->vma
12306 - sec->output_offset);
12307 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12308 if (dest != (bfd_vma) -1)
12309 {
12310 relocation = dest;
12311 addend = 0;
12312 }
12313 }
12314
12315 /* If the branch is out of reach we ought to have a long
12316 branch stub. */
12317 from = (rel->r_offset
12318 + input_section->output_offset
12319 + input_section->output_section->vma);
12320
12321 if (stub_entry != NULL
12322 && (stub_entry->stub_type == ppc_stub_long_branch
12323 || stub_entry->stub_type == ppc_stub_plt_branch)
12324 && (r_type == R_PPC64_ADDR14_BRTAKEN
12325 || r_type == R_PPC64_ADDR14_BRNTAKEN
12326 || (relocation + addend - from + max_br_offset
12327 < 2 * max_br_offset)))
12328 /* Don't use the stub if this branch is in range. */
12329 stub_entry = NULL;
12330
12331 if (stub_entry != NULL)
12332 {
12333 /* Munge up the value and addend so that we call the stub
12334 rather than the procedure directly. */
12335 relocation = (stub_entry->stub_offset
12336 + stub_entry->stub_sec->output_offset
12337 + stub_entry->stub_sec->output_section->vma);
12338 addend = 0;
12339 }
12340
12341 if (insn != 0)
12342 {
12343 if (is_power4)
12344 {
12345 /* Set 'a' bit. This is 0b00010 in BO field for branch
12346 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12347 for branch on CTR insns (BO == 1a00t or 1a01t). */
12348 if ((insn & (0x14 << 21)) == (0x04 << 21))
12349 insn |= 0x02 << 21;
12350 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12351 insn |= 0x08 << 21;
12352 else
12353 break;
12354 }
12355 else
12356 {
12357 /* Invert 'y' bit if not the default. */
12358 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12359 insn ^= 0x01 << 21;
12360 }
12361
12362 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12363 }
12364
12365 /* NOP out calls to undefined weak functions.
12366 We can thus call a weak function without first
12367 checking whether the function is defined. */
12368 else if (h != NULL
12369 && h->elf.root.type == bfd_link_hash_undefweak
12370 && h->elf.dynindx == -1
12371 && r_type == R_PPC64_REL24
12372 && relocation == 0
12373 && addend == 0)
12374 {
12375 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12376 continue;
12377 }
12378 break;
12379 }
12380
12381 /* Set `addend'. */
12382 tls_type = 0;
12383 switch (r_type)
12384 {
12385 default:
12386 info->callbacks->einfo
12387 (_("%B: unknown relocation type %d for symbol %s\n"),
12388 input_bfd, (int) r_type, sym_name);
12389
12390 bfd_set_error (bfd_error_bad_value);
12391 ret = FALSE;
12392 continue;
12393
12394 case R_PPC64_NONE:
12395 case R_PPC64_TLS:
12396 case R_PPC64_TLSGD:
12397 case R_PPC64_TLSLD:
12398 case R_PPC64_GNU_VTINHERIT:
12399 case R_PPC64_GNU_VTENTRY:
12400 continue;
12401
12402 /* GOT16 relocations. Like an ADDR16 using the symbol's
12403 address in the GOT as relocation value instead of the
12404 symbol's value itself. Also, create a GOT entry for the
12405 symbol and put the symbol value there. */
12406 case R_PPC64_GOT_TLSGD16:
12407 case R_PPC64_GOT_TLSGD16_LO:
12408 case R_PPC64_GOT_TLSGD16_HI:
12409 case R_PPC64_GOT_TLSGD16_HA:
12410 tls_type = TLS_TLS | TLS_GD;
12411 goto dogot;
12412
12413 case R_PPC64_GOT_TLSLD16:
12414 case R_PPC64_GOT_TLSLD16_LO:
12415 case R_PPC64_GOT_TLSLD16_HI:
12416 case R_PPC64_GOT_TLSLD16_HA:
12417 tls_type = TLS_TLS | TLS_LD;
12418 goto dogot;
12419
12420 case R_PPC64_GOT_TPREL16_DS:
12421 case R_PPC64_GOT_TPREL16_LO_DS:
12422 case R_PPC64_GOT_TPREL16_HI:
12423 case R_PPC64_GOT_TPREL16_HA:
12424 tls_type = TLS_TLS | TLS_TPREL;
12425 goto dogot;
12426
12427 case R_PPC64_GOT_DTPREL16_DS:
12428 case R_PPC64_GOT_DTPREL16_LO_DS:
12429 case R_PPC64_GOT_DTPREL16_HI:
12430 case R_PPC64_GOT_DTPREL16_HA:
12431 tls_type = TLS_TLS | TLS_DTPREL;
12432 goto dogot;
12433
12434 case R_PPC64_GOT16:
12435 case R_PPC64_GOT16_LO:
12436 case R_PPC64_GOT16_HI:
12437 case R_PPC64_GOT16_HA:
12438 case R_PPC64_GOT16_DS:
12439 case R_PPC64_GOT16_LO_DS:
12440 dogot:
12441 {
12442 /* Relocation is to the entry for this symbol in the global
12443 offset table. */
12444 asection *got;
12445 bfd_vma *offp;
12446 bfd_vma off;
12447 unsigned long indx = 0;
12448 struct got_entry *ent;
12449
12450 if (tls_type == (TLS_TLS | TLS_LD)
12451 && (h == NULL
12452 || !h->elf.def_dynamic))
12453 ent = ppc64_tlsld_got (input_bfd);
12454 else
12455 {
12456
12457 if (h != NULL)
12458 {
12459 bfd_boolean dyn = htab->elf.dynamic_sections_created;
12460 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12461 &h->elf)
12462 || (info->shared
12463 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
12464 /* This is actually a static link, or it is a
12465 -Bsymbolic link and the symbol is defined
12466 locally, or the symbol was forced to be local
12467 because of a version file. */
12468 ;
12469 else
12470 {
12471 indx = h->elf.dynindx;
12472 unresolved_reloc = FALSE;
12473 }
12474 ent = h->elf.got.glist;
12475 }
12476 else
12477 {
12478 if (local_got_ents == NULL)
12479 abort ();
12480 ent = local_got_ents[r_symndx];
12481 }
12482
12483 for (; ent != NULL; ent = ent->next)
12484 if (ent->addend == orig_addend
12485 && ent->owner == input_bfd
12486 && ent->tls_type == tls_type)
12487 break;
12488 }
12489
12490 if (ent == NULL)
12491 abort ();
12492 if (ent->is_indirect)
12493 ent = ent->got.ent;
12494 offp = &ent->got.offset;
12495 got = ppc64_elf_tdata (ent->owner)->got;
12496 if (got == NULL)
12497 abort ();
12498
12499 /* The offset must always be a multiple of 8. We use the
12500 least significant bit to record whether we have already
12501 processed this entry. */
12502 off = *offp;
12503 if ((off & 1) != 0)
12504 off &= ~1;
12505 else
12506 {
12507 /* Generate relocs for the dynamic linker, except in
12508 the case of TLSLD where we'll use one entry per
12509 module. */
12510 asection *relgot;
12511 bfd_boolean ifunc;
12512
12513 *offp = off | 1;
12514 relgot = NULL;
12515 ifunc = (h != NULL
12516 ? h->elf.type == STT_GNU_IFUNC
12517 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12518 if ((info->shared || indx != 0)
12519 && (h == NULL
12520 || (tls_type == (TLS_TLS | TLS_LD)
12521 && !h->elf.def_dynamic)
12522 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12523 || h->elf.root.type != bfd_link_hash_undefweak))
12524 relgot = ppc64_elf_tdata (ent->owner)->relgot;
12525 else if (ifunc)
12526 relgot = htab->reliplt;
12527 if (relgot != NULL)
12528 {
12529 outrel.r_offset = (got->output_section->vma
12530 + got->output_offset
12531 + off);
12532 outrel.r_addend = addend;
12533 if (tls_type & (TLS_LD | TLS_GD))
12534 {
12535 outrel.r_addend = 0;
12536 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12537 if (tls_type == (TLS_TLS | TLS_GD))
12538 {
12539 loc = relgot->contents;
12540 loc += (relgot->reloc_count++
12541 * sizeof (Elf64_External_Rela));
12542 bfd_elf64_swap_reloca_out (output_bfd,
12543 &outrel, loc);
12544 outrel.r_offset += 8;
12545 outrel.r_addend = addend;
12546 outrel.r_info
12547 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12548 }
12549 }
12550 else if (tls_type == (TLS_TLS | TLS_DTPREL))
12551 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12552 else if (tls_type == (TLS_TLS | TLS_TPREL))
12553 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12554 else if (indx != 0)
12555 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12556 else
12557 {
12558 if (ifunc)
12559 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12560 else
12561 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12562
12563 /* Write the .got section contents for the sake
12564 of prelink. */
12565 loc = got->contents + off;
12566 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12567 loc);
12568 }
12569
12570 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12571 {
12572 outrel.r_addend += relocation;
12573 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12574 outrel.r_addend -= htab->elf.tls_sec->vma;
12575 }
12576 loc = relgot->contents;
12577 loc += (relgot->reloc_count++
12578 * sizeof (Elf64_External_Rela));
12579 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12580 }
12581
12582 /* Init the .got section contents here if we're not
12583 emitting a reloc. */
12584 else
12585 {
12586 relocation += addend;
12587 if (tls_type == (TLS_TLS | TLS_LD))
12588 relocation = 1;
12589 else if (tls_type != 0)
12590 {
12591 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12592 if (tls_type == (TLS_TLS | TLS_TPREL))
12593 relocation += DTP_OFFSET - TP_OFFSET;
12594
12595 if (tls_type == (TLS_TLS | TLS_GD))
12596 {
12597 bfd_put_64 (output_bfd, relocation,
12598 got->contents + off + 8);
12599 relocation = 1;
12600 }
12601 }
12602
12603 bfd_put_64 (output_bfd, relocation,
12604 got->contents + off);
12605 }
12606 }
12607
12608 if (off >= (bfd_vma) -2)
12609 abort ();
12610
12611 relocation = got->output_section->vma + got->output_offset + off;
12612 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12613 }
12614 break;
12615
12616 case R_PPC64_PLT16_HA:
12617 case R_PPC64_PLT16_HI:
12618 case R_PPC64_PLT16_LO:
12619 case R_PPC64_PLT32:
12620 case R_PPC64_PLT64:
12621 /* Relocation is to the entry for this symbol in the
12622 procedure linkage table. */
12623
12624 /* Resolve a PLT reloc against a local symbol directly,
12625 without using the procedure linkage table. */
12626 if (h == NULL)
12627 break;
12628
12629 /* It's possible that we didn't make a PLT entry for this
12630 symbol. This happens when statically linking PIC code,
12631 or when using -Bsymbolic. Go find a match if there is a
12632 PLT entry. */
12633 if (htab->plt != NULL)
12634 {
12635 struct plt_entry *ent;
12636 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12637 if (ent->addend == orig_addend
12638 && ent->plt.offset != (bfd_vma) -1)
12639 {
12640 relocation = (htab->plt->output_section->vma
12641 + htab->plt->output_offset
12642 + ent->plt.offset);
12643 unresolved_reloc = FALSE;
12644 }
12645 }
12646 break;
12647
12648 case R_PPC64_TOC:
12649 /* Relocation value is TOC base. */
12650 relocation = TOCstart;
12651 if (r_symndx == STN_UNDEF)
12652 relocation += htab->stub_group[input_section->id].toc_off;
12653 else if (unresolved_reloc)
12654 ;
12655 else if (sec != NULL && sec->id <= htab->top_id)
12656 relocation += htab->stub_group[sec->id].toc_off;
12657 else
12658 unresolved_reloc = TRUE;
12659 goto dodyn;
12660
12661 /* TOC16 relocs. We want the offset relative to the TOC base,
12662 which is the address of the start of the TOC plus 0x8000.
12663 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12664 in this order. */
12665 case R_PPC64_TOC16:
12666 case R_PPC64_TOC16_LO:
12667 case R_PPC64_TOC16_HI:
12668 case R_PPC64_TOC16_DS:
12669 case R_PPC64_TOC16_LO_DS:
12670 case R_PPC64_TOC16_HA:
12671 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12672 break;
12673
12674 /* Relocate against the beginning of the section. */
12675 case R_PPC64_SECTOFF:
12676 case R_PPC64_SECTOFF_LO:
12677 case R_PPC64_SECTOFF_HI:
12678 case R_PPC64_SECTOFF_DS:
12679 case R_PPC64_SECTOFF_LO_DS:
12680 case R_PPC64_SECTOFF_HA:
12681 if (sec != NULL)
12682 addend -= sec->output_section->vma;
12683 break;
12684
12685 case R_PPC64_REL16:
12686 case R_PPC64_REL16_LO:
12687 case R_PPC64_REL16_HI:
12688 case R_PPC64_REL16_HA:
12689 break;
12690
12691 case R_PPC64_REL14:
12692 case R_PPC64_REL14_BRNTAKEN:
12693 case R_PPC64_REL14_BRTAKEN:
12694 case R_PPC64_REL24:
12695 break;
12696
12697 case R_PPC64_TPREL16:
12698 case R_PPC64_TPREL16_LO:
12699 case R_PPC64_TPREL16_HI:
12700 case R_PPC64_TPREL16_HA:
12701 case R_PPC64_TPREL16_DS:
12702 case R_PPC64_TPREL16_LO_DS:
12703 case R_PPC64_TPREL16_HIGHER:
12704 case R_PPC64_TPREL16_HIGHERA:
12705 case R_PPC64_TPREL16_HIGHEST:
12706 case R_PPC64_TPREL16_HIGHESTA:
12707 if (h != NULL
12708 && h->elf.root.type == bfd_link_hash_undefweak
12709 && h->elf.dynindx == -1)
12710 {
12711 /* Make this relocation against an undefined weak symbol
12712 resolve to zero. This is really just a tweak, since
12713 code using weak externs ought to check that they are
12714 defined before using them. */
12715 bfd_byte *p = contents + rel->r_offset - d_offset;
12716
12717 insn = bfd_get_32 (output_bfd, p);
12718 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12719 if (insn != 0)
12720 bfd_put_32 (output_bfd, insn, p);
12721 break;
12722 }
12723 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12724 if (info->shared)
12725 /* The TPREL16 relocs shouldn't really be used in shared
12726 libs as they will result in DT_TEXTREL being set, but
12727 support them anyway. */
12728 goto dodyn;
12729 break;
12730
12731 case R_PPC64_DTPREL16:
12732 case R_PPC64_DTPREL16_LO:
12733 case R_PPC64_DTPREL16_HI:
12734 case R_PPC64_DTPREL16_HA:
12735 case R_PPC64_DTPREL16_DS:
12736 case R_PPC64_DTPREL16_LO_DS:
12737 case R_PPC64_DTPREL16_HIGHER:
12738 case R_PPC64_DTPREL16_HIGHERA:
12739 case R_PPC64_DTPREL16_HIGHEST:
12740 case R_PPC64_DTPREL16_HIGHESTA:
12741 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12742 break;
12743
12744 case R_PPC64_DTPMOD64:
12745 relocation = 1;
12746 addend = 0;
12747 goto dodyn;
12748
12749 case R_PPC64_TPREL64:
12750 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12751 goto dodyn;
12752
12753 case R_PPC64_DTPREL64:
12754 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12755 /* Fall thru */
12756
12757 /* Relocations that may need to be propagated if this is a
12758 dynamic object. */
12759 case R_PPC64_REL30:
12760 case R_PPC64_REL32:
12761 case R_PPC64_REL64:
12762 case R_PPC64_ADDR14:
12763 case R_PPC64_ADDR14_BRNTAKEN:
12764 case R_PPC64_ADDR14_BRTAKEN:
12765 case R_PPC64_ADDR16:
12766 case R_PPC64_ADDR16_DS:
12767 case R_PPC64_ADDR16_HA:
12768 case R_PPC64_ADDR16_HI:
12769 case R_PPC64_ADDR16_HIGHER:
12770 case R_PPC64_ADDR16_HIGHERA:
12771 case R_PPC64_ADDR16_HIGHEST:
12772 case R_PPC64_ADDR16_HIGHESTA:
12773 case R_PPC64_ADDR16_LO:
12774 case R_PPC64_ADDR16_LO_DS:
12775 case R_PPC64_ADDR24:
12776 case R_PPC64_ADDR32:
12777 case R_PPC64_ADDR64:
12778 case R_PPC64_UADDR16:
12779 case R_PPC64_UADDR32:
12780 case R_PPC64_UADDR64:
12781 dodyn:
12782 if ((input_section->flags & SEC_ALLOC) == 0)
12783 break;
12784
12785 if (NO_OPD_RELOCS && is_opd)
12786 break;
12787
12788 if ((info->shared
12789 && (h == NULL
12790 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12791 || h->elf.root.type != bfd_link_hash_undefweak)
12792 && (must_be_dyn_reloc (info, r_type)
12793 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
12794 || (ELIMINATE_COPY_RELOCS
12795 && !info->shared
12796 && h != NULL
12797 && h->elf.dynindx != -1
12798 && !h->elf.non_got_ref
12799 && !h->elf.def_regular)
12800 || (!info->shared
12801 && (h != NULL
12802 ? h->elf.type == STT_GNU_IFUNC
12803 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
12804 {
12805 bfd_boolean skip, relocate;
12806 asection *sreloc;
12807 bfd_vma out_off;
12808
12809 /* When generating a dynamic object, these relocations
12810 are copied into the output file to be resolved at run
12811 time. */
12812
12813 skip = FALSE;
12814 relocate = FALSE;
12815
12816 out_off = _bfd_elf_section_offset (output_bfd, info,
12817 input_section, rel->r_offset);
12818 if (out_off == (bfd_vma) -1)
12819 skip = TRUE;
12820 else if (out_off == (bfd_vma) -2)
12821 skip = TRUE, relocate = TRUE;
12822 out_off += (input_section->output_section->vma
12823 + input_section->output_offset);
12824 outrel.r_offset = out_off;
12825 outrel.r_addend = rel->r_addend;
12826
12827 /* Optimize unaligned reloc use. */
12828 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12829 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12830 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12831 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12832 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12833 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12834 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12835 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12836 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12837
12838 if (skip)
12839 memset (&outrel, 0, sizeof outrel);
12840 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
12841 && !is_opd
12842 && r_type != R_PPC64_TOC)
12843 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
12844 else
12845 {
12846 /* This symbol is local, or marked to become local,
12847 or this is an opd section reloc which must point
12848 at a local function. */
12849 outrel.r_addend += relocation;
12850 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
12851 {
12852 if (is_opd && h != NULL)
12853 {
12854 /* Lie about opd entries. This case occurs
12855 when building shared libraries and we
12856 reference a function in another shared
12857 lib. The same thing happens for a weak
12858 definition in an application that's
12859 overridden by a strong definition in a
12860 shared lib. (I believe this is a generic
12861 bug in binutils handling of weak syms.)
12862 In these cases we won't use the opd
12863 entry in this lib. */
12864 unresolved_reloc = FALSE;
12865 }
12866 if (!is_opd
12867 && r_type == R_PPC64_ADDR64
12868 && (h != NULL
12869 ? h->elf.type == STT_GNU_IFUNC
12870 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12871 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12872 else
12873 {
12874 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12875
12876 /* We need to relocate .opd contents for ld.so.
12877 Prelink also wants simple and consistent rules
12878 for relocs. This make all RELATIVE relocs have
12879 *r_offset equal to r_addend. */
12880 relocate = TRUE;
12881 }
12882 }
12883 else
12884 {
12885 long indx = 0;
12886
12887 if (h != NULL
12888 ? h->elf.type == STT_GNU_IFUNC
12889 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12890 {
12891 info->callbacks->einfo
12892 (_("%H: relocation %s for indirect "
12893 "function %s unsupported\n"),
12894 input_bfd, input_section, rel->r_offset,
12895 ppc64_elf_howto_table[r_type]->name,
12896 sym_name);
12897 ret = FALSE;
12898 }
12899 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
12900 ;
12901 else if (sec == NULL || sec->owner == NULL)
12902 {
12903 bfd_set_error (bfd_error_bad_value);
12904 return FALSE;
12905 }
12906 else
12907 {
12908 asection *osec;
12909
12910 osec = sec->output_section;
12911 indx = elf_section_data (osec)->dynindx;
12912
12913 if (indx == 0)
12914 {
12915 if ((osec->flags & SEC_READONLY) == 0
12916 && htab->elf.data_index_section != NULL)
12917 osec = htab->elf.data_index_section;
12918 else
12919 osec = htab->elf.text_index_section;
12920 indx = elf_section_data (osec)->dynindx;
12921 }
12922 BFD_ASSERT (indx != 0);
12923
12924 /* We are turning this relocation into one
12925 against a section symbol, so subtract out
12926 the output section's address but not the
12927 offset of the input section in the output
12928 section. */
12929 outrel.r_addend -= osec->vma;
12930 }
12931
12932 outrel.r_info = ELF64_R_INFO (indx, r_type);
12933 }
12934 }
12935
12936 sreloc = elf_section_data (input_section)->sreloc;
12937 if (!htab->elf.dynamic_sections_created)
12938 sreloc = htab->reliplt;
12939 if (sreloc == NULL)
12940 abort ();
12941
12942 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12943 >= sreloc->size)
12944 abort ();
12945 loc = sreloc->contents;
12946 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
12947 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12948
12949 /* If this reloc is against an external symbol, it will
12950 be computed at runtime, so there's no need to do
12951 anything now. However, for the sake of prelink ensure
12952 that the section contents are a known value. */
12953 if (! relocate)
12954 {
12955 unresolved_reloc = FALSE;
12956 /* The value chosen here is quite arbitrary as ld.so
12957 ignores section contents except for the special
12958 case of .opd where the contents might be accessed
12959 before relocation. Choose zero, as that won't
12960 cause reloc overflow. */
12961 relocation = 0;
12962 addend = 0;
12963 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12964 to improve backward compatibility with older
12965 versions of ld. */
12966 if (r_type == R_PPC64_ADDR64)
12967 addend = outrel.r_addend;
12968 /* Adjust pc_relative relocs to have zero in *r_offset. */
12969 else if (ppc64_elf_howto_table[r_type]->pc_relative)
12970 addend = (input_section->output_section->vma
12971 + input_section->output_offset
12972 + rel->r_offset);
12973 }
12974 }
12975 break;
12976
12977 case R_PPC64_COPY:
12978 case R_PPC64_GLOB_DAT:
12979 case R_PPC64_JMP_SLOT:
12980 case R_PPC64_JMP_IREL:
12981 case R_PPC64_RELATIVE:
12982 /* We shouldn't ever see these dynamic relocs in relocatable
12983 files. */
12984 /* Fall through. */
12985
12986 case R_PPC64_PLTGOT16:
12987 case R_PPC64_PLTGOT16_DS:
12988 case R_PPC64_PLTGOT16_HA:
12989 case R_PPC64_PLTGOT16_HI:
12990 case R_PPC64_PLTGOT16_LO:
12991 case R_PPC64_PLTGOT16_LO_DS:
12992 case R_PPC64_PLTREL32:
12993 case R_PPC64_PLTREL64:
12994 /* These ones haven't been implemented yet. */
12995
12996 info->callbacks->einfo
12997 (_("%B: relocation %s is not supported for symbol %s\n"),
12998 input_bfd,
12999 ppc64_elf_howto_table[r_type]->name, sym_name);
13000
13001 bfd_set_error (bfd_error_invalid_operation);
13002 ret = FALSE;
13003 continue;
13004 }
13005
13006 /* Multi-instruction sequences that access the TOC can be
13007 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13008 to nop; addi rb,r2,x; */
13009 switch (r_type)
13010 {
13011 default:
13012 break;
13013
13014 case R_PPC64_GOT_TLSLD16_HI:
13015 case R_PPC64_GOT_TLSGD16_HI:
13016 case R_PPC64_GOT_TPREL16_HI:
13017 case R_PPC64_GOT_DTPREL16_HI:
13018 case R_PPC64_GOT16_HI:
13019 case R_PPC64_TOC16_HI:
13020 /* These relocs would only be useful if building up an
13021 offset to later add to r2, perhaps in an indexed
13022 addressing mode instruction. Don't try to optimize.
13023 Unfortunately, the possibility of someone building up an
13024 offset like this or even with the HA relocs, means that
13025 we need to check the high insn when optimizing the low
13026 insn. */
13027 break;
13028
13029 case R_PPC64_GOT_TLSLD16_HA:
13030 case R_PPC64_GOT_TLSGD16_HA:
13031 case R_PPC64_GOT_TPREL16_HA:
13032 case R_PPC64_GOT_DTPREL16_HA:
13033 case R_PPC64_GOT16_HA:
13034 case R_PPC64_TOC16_HA:
13035 /* nop is done later. */
13036 break;
13037
13038 case R_PPC64_GOT_TLSLD16_LO:
13039 case R_PPC64_GOT_TLSGD16_LO:
13040 case R_PPC64_GOT_TPREL16_LO_DS:
13041 case R_PPC64_GOT_DTPREL16_LO_DS:
13042 case R_PPC64_GOT16_LO:
13043 case R_PPC64_GOT16_LO_DS:
13044 case R_PPC64_TOC16_LO:
13045 case R_PPC64_TOC16_LO_DS:
13046 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13047 {
13048 bfd_byte *p = contents + (rel->r_offset & ~3);
13049 insn = bfd_get_32 (input_bfd, p);
13050 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13051 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13052 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13053 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13054 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13055 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13056 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13057 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13058 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13059 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13060 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13061 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13062 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13063 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13064 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13065 && (insn & 3) != 1)
13066 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13067 && ((insn & 3) == 0 || (insn & 3) == 3)))
13068 {
13069 unsigned int reg = (insn >> 16) & 0x1f;
13070 const Elf_Internal_Rela *ha;
13071 bfd_boolean match_addend;
13072
13073 match_addend = (sym != NULL
13074 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13075 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13076 input_bfd, contents);
13077 if (ha != NULL)
13078 {
13079 insn &= ~(0x1f << 16);
13080 insn |= reg << 16;
13081 bfd_put_32 (input_bfd, insn, p);
13082 if (ha_opt == NULL)
13083 {
13084 ha_opt = bfd_zmalloc (input_section->reloc_count);
13085 if (ha_opt == NULL)
13086 return FALSE;
13087 }
13088 ha_opt[ha - relocs] = 1;
13089 }
13090 else
13091 /* If we don't find a matching high part insn,
13092 something is fishy. Refuse to nop any high
13093 part insn in this section. */
13094 no_ha_opt = TRUE;
13095 }
13096 }
13097 break;
13098 }
13099
13100 /* Do any further special processing. */
13101 switch (r_type)
13102 {
13103 default:
13104 break;
13105
13106 case R_PPC64_ADDR16_HA:
13107 case R_PPC64_REL16_HA:
13108 case R_PPC64_ADDR16_HIGHERA:
13109 case R_PPC64_ADDR16_HIGHESTA:
13110 case R_PPC64_TOC16_HA:
13111 case R_PPC64_SECTOFF_HA:
13112 case R_PPC64_TPREL16_HA:
13113 case R_PPC64_DTPREL16_HA:
13114 case R_PPC64_TPREL16_HIGHER:
13115 case R_PPC64_TPREL16_HIGHERA:
13116 case R_PPC64_TPREL16_HIGHEST:
13117 case R_PPC64_TPREL16_HIGHESTA:
13118 case R_PPC64_DTPREL16_HIGHER:
13119 case R_PPC64_DTPREL16_HIGHERA:
13120 case R_PPC64_DTPREL16_HIGHEST:
13121 case R_PPC64_DTPREL16_HIGHESTA:
13122 /* It's just possible that this symbol is a weak symbol
13123 that's not actually defined anywhere. In that case,
13124 'sec' would be NULL, and we should leave the symbol
13125 alone (it will be set to zero elsewhere in the link). */
13126 if (sec == NULL)
13127 break;
13128 /* Fall thru */
13129
13130 case R_PPC64_GOT16_HA:
13131 case R_PPC64_PLTGOT16_HA:
13132 case R_PPC64_PLT16_HA:
13133 case R_PPC64_GOT_TLSGD16_HA:
13134 case R_PPC64_GOT_TLSLD16_HA:
13135 case R_PPC64_GOT_TPREL16_HA:
13136 case R_PPC64_GOT_DTPREL16_HA:
13137 /* Add 0x10000 if sign bit in 0:15 is set.
13138 Bits 0:15 are not used. */
13139 addend += 0x8000;
13140 break;
13141
13142 case R_PPC64_ADDR16_DS:
13143 case R_PPC64_ADDR16_LO_DS:
13144 case R_PPC64_GOT16_DS:
13145 case R_PPC64_GOT16_LO_DS:
13146 case R_PPC64_PLT16_LO_DS:
13147 case R_PPC64_SECTOFF_DS:
13148 case R_PPC64_SECTOFF_LO_DS:
13149 case R_PPC64_TOC16_DS:
13150 case R_PPC64_TOC16_LO_DS:
13151 case R_PPC64_PLTGOT16_DS:
13152 case R_PPC64_PLTGOT16_LO_DS:
13153 case R_PPC64_GOT_TPREL16_DS:
13154 case R_PPC64_GOT_TPREL16_LO_DS:
13155 case R_PPC64_GOT_DTPREL16_DS:
13156 case R_PPC64_GOT_DTPREL16_LO_DS:
13157 case R_PPC64_TPREL16_DS:
13158 case R_PPC64_TPREL16_LO_DS:
13159 case R_PPC64_DTPREL16_DS:
13160 case R_PPC64_DTPREL16_LO_DS:
13161 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13162 mask = 3;
13163 /* If this reloc is against an lq insn, then the value must be
13164 a multiple of 16. This is somewhat of a hack, but the
13165 "correct" way to do this by defining _DQ forms of all the
13166 _DS relocs bloats all reloc switches in this file. It
13167 doesn't seem to make much sense to use any of these relocs
13168 in data, so testing the insn should be safe. */
13169 if ((insn & (0x3f << 26)) == (56u << 26))
13170 mask = 15;
13171 if (((relocation + addend) & mask) != 0)
13172 {
13173 info->callbacks->einfo
13174 (_("%H: error: %s not a multiple of %u\n"),
13175 input_bfd, input_section, rel->r_offset,
13176 ppc64_elf_howto_table[r_type]->name,
13177 mask + 1);
13178 bfd_set_error (bfd_error_bad_value);
13179 ret = FALSE;
13180 continue;
13181 }
13182 break;
13183 }
13184
13185 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13186 because such sections are not SEC_ALLOC and thus ld.so will
13187 not process them. */
13188 if (unresolved_reloc
13189 && !((input_section->flags & SEC_DEBUGGING) != 0
13190 && h->elf.def_dynamic))
13191 {
13192 info->callbacks->einfo
13193 (_("%H: unresolvable %s relocation against symbol `%s'\n"),
13194 input_bfd, input_section, rel->r_offset,
13195 ppc64_elf_howto_table[(int) r_type]->name,
13196 h->elf.root.root.string);
13197 ret = FALSE;
13198 }
13199
13200 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13201 input_bfd,
13202 input_section,
13203 contents,
13204 rel->r_offset,
13205 relocation,
13206 addend);
13207
13208 if (r != bfd_reloc_ok)
13209 {
13210 if (sym_name == NULL)
13211 sym_name = "(null)";
13212 if (r == bfd_reloc_overflow)
13213 {
13214 if (warned)
13215 continue;
13216 if (h != NULL
13217 && h->elf.root.type == bfd_link_hash_undefweak
13218 && ppc64_elf_howto_table[r_type]->pc_relative)
13219 {
13220 /* Assume this is a call protected by other code that
13221 detects the symbol is undefined. If this is the case,
13222 we can safely ignore the overflow. If not, the
13223 program is hosed anyway, and a little warning isn't
13224 going to help. */
13225
13226 continue;
13227 }
13228
13229 if (!((*info->callbacks->reloc_overflow)
13230 (info, (h ? &h->elf.root : NULL), sym_name,
13231 ppc64_elf_howto_table[r_type]->name,
13232 orig_addend, input_bfd, input_section, rel->r_offset)))
13233 return FALSE;
13234 }
13235 else
13236 {
13237 info->callbacks->einfo
13238 (_("%H: %s reloc against `%s': error %d\n"),
13239 input_bfd, input_section, rel->r_offset,
13240 ppc64_elf_howto_table[r_type]->name,
13241 sym_name,
13242 (int) r);
13243 ret = FALSE;
13244 }
13245 }
13246 }
13247
13248 if (ha_opt != NULL)
13249 {
13250 if (!no_ha_opt)
13251 {
13252 unsigned char *opt = ha_opt;
13253 rel = relocs;
13254 relend = relocs + input_section->reloc_count;
13255 for (; rel < relend; opt++, rel++)
13256 if (*opt != 0)
13257 {
13258 bfd_byte *p = contents + (rel->r_offset & ~3);
13259 bfd_put_32 (input_bfd, NOP, p);
13260 }
13261 }
13262 free (ha_opt);
13263 }
13264
13265 /* If we're emitting relocations, then shortly after this function
13266 returns, reloc offsets and addends for this section will be
13267 adjusted. Worse, reloc symbol indices will be for the output
13268 file rather than the input. Save a copy of the relocs for
13269 opd_entry_value. */
13270 if (is_opd && (info->emitrelocations || info->relocatable))
13271 {
13272 bfd_size_type amt;
13273 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13274 rel = bfd_alloc (input_bfd, amt);
13275 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13276 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13277 if (rel == NULL)
13278 return FALSE;
13279 memcpy (rel, relocs, amt);
13280 }
13281 return ret;
13282 }
13283
13284 /* Adjust the value of any local symbols in opd sections. */
13285
13286 static int
13287 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13288 const char *name ATTRIBUTE_UNUSED,
13289 Elf_Internal_Sym *elfsym,
13290 asection *input_sec,
13291 struct elf_link_hash_entry *h)
13292 {
13293 struct _opd_sec_data *opd;
13294 long adjust;
13295 bfd_vma value;
13296
13297 if (h != NULL)
13298 return 1;
13299
13300 opd = get_opd_info (input_sec);
13301 if (opd == NULL || opd->adjust == NULL)
13302 return 1;
13303
13304 value = elfsym->st_value - input_sec->output_offset;
13305 if (!info->relocatable)
13306 value -= input_sec->output_section->vma;
13307
13308 adjust = opd->adjust[value / 8];
13309 if (adjust == -1)
13310 return 2;
13311
13312 elfsym->st_value += adjust;
13313 return 1;
13314 }
13315
13316 /* Finish up dynamic symbol handling. We set the contents of various
13317 dynamic sections here. */
13318
13319 static bfd_boolean
13320 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13321 struct bfd_link_info *info,
13322 struct elf_link_hash_entry *h,
13323 Elf_Internal_Sym *sym)
13324 {
13325 struct ppc_link_hash_table *htab;
13326 struct plt_entry *ent;
13327 Elf_Internal_Rela rela;
13328 bfd_byte *loc;
13329
13330 htab = ppc_hash_table (info);
13331 if (htab == NULL)
13332 return FALSE;
13333
13334 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13335 if (ent->plt.offset != (bfd_vma) -1)
13336 {
13337 /* This symbol has an entry in the procedure linkage
13338 table. Set it up. */
13339 if (!htab->elf.dynamic_sections_created
13340 || h->dynindx == -1)
13341 {
13342 BFD_ASSERT (h->type == STT_GNU_IFUNC
13343 && h->def_regular
13344 && (h->root.type == bfd_link_hash_defined
13345 || h->root.type == bfd_link_hash_defweak));
13346 rela.r_offset = (htab->iplt->output_section->vma
13347 + htab->iplt->output_offset
13348 + ent->plt.offset);
13349 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13350 rela.r_addend = (h->root.u.def.value
13351 + h->root.u.def.section->output_offset
13352 + h->root.u.def.section->output_section->vma
13353 + ent->addend);
13354 loc = (htab->reliplt->contents
13355 + (htab->reliplt->reloc_count++
13356 * sizeof (Elf64_External_Rela)));
13357 }
13358 else
13359 {
13360 rela.r_offset = (htab->plt->output_section->vma
13361 + htab->plt->output_offset
13362 + ent->plt.offset);
13363 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13364 rela.r_addend = ent->addend;
13365 loc = (htab->relplt->contents
13366 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13367 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13368 }
13369 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13370 }
13371
13372 if (h->needs_copy)
13373 {
13374 /* This symbol needs a copy reloc. Set it up. */
13375
13376 if (h->dynindx == -1
13377 || (h->root.type != bfd_link_hash_defined
13378 && h->root.type != bfd_link_hash_defweak)
13379 || htab->relbss == NULL)
13380 abort ();
13381
13382 rela.r_offset = (h->root.u.def.value
13383 + h->root.u.def.section->output_section->vma
13384 + h->root.u.def.section->output_offset);
13385 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13386 rela.r_addend = 0;
13387 loc = htab->relbss->contents;
13388 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13389 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13390 }
13391
13392 /* Mark some specially defined symbols as absolute. */
13393 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13394 sym->st_shndx = SHN_ABS;
13395
13396 return TRUE;
13397 }
13398
13399 /* Used to decide how to sort relocs in an optimal manner for the
13400 dynamic linker, before writing them out. */
13401
13402 static enum elf_reloc_type_class
13403 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13404 {
13405 enum elf_ppc64_reloc_type r_type;
13406
13407 r_type = ELF64_R_TYPE (rela->r_info);
13408 switch (r_type)
13409 {
13410 case R_PPC64_RELATIVE:
13411 return reloc_class_relative;
13412 case R_PPC64_JMP_SLOT:
13413 return reloc_class_plt;
13414 case R_PPC64_COPY:
13415 return reloc_class_copy;
13416 default:
13417 return reloc_class_normal;
13418 }
13419 }
13420
13421 /* Finish up the dynamic sections. */
13422
13423 static bfd_boolean
13424 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13425 struct bfd_link_info *info)
13426 {
13427 struct ppc_link_hash_table *htab;
13428 bfd *dynobj;
13429 asection *sdyn;
13430
13431 htab = ppc_hash_table (info);
13432 if (htab == NULL)
13433 return FALSE;
13434
13435 dynobj = htab->elf.dynobj;
13436 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13437
13438 if (htab->elf.dynamic_sections_created)
13439 {
13440 Elf64_External_Dyn *dyncon, *dynconend;
13441
13442 if (sdyn == NULL || htab->got == NULL)
13443 abort ();
13444
13445 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13446 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13447 for (; dyncon < dynconend; dyncon++)
13448 {
13449 Elf_Internal_Dyn dyn;
13450 asection *s;
13451
13452 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13453
13454 switch (dyn.d_tag)
13455 {
13456 default:
13457 continue;
13458
13459 case DT_PPC64_GLINK:
13460 s = htab->glink;
13461 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13462 /* We stupidly defined DT_PPC64_GLINK to be the start
13463 of glink rather than the first entry point, which is
13464 what ld.so needs, and now have a bigger stub to
13465 support automatic multiple TOCs. */
13466 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13467 break;
13468
13469 case DT_PPC64_OPD:
13470 s = bfd_get_section_by_name (output_bfd, ".opd");
13471 if (s == NULL)
13472 continue;
13473 dyn.d_un.d_ptr = s->vma;
13474 break;
13475
13476 case DT_PPC64_OPDSZ:
13477 s = bfd_get_section_by_name (output_bfd, ".opd");
13478 if (s == NULL)
13479 continue;
13480 dyn.d_un.d_val = s->size;
13481 break;
13482
13483 case DT_PLTGOT:
13484 s = htab->plt;
13485 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13486 break;
13487
13488 case DT_JMPREL:
13489 s = htab->relplt;
13490 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13491 break;
13492
13493 case DT_PLTRELSZ:
13494 dyn.d_un.d_val = htab->relplt->size;
13495 break;
13496
13497 case DT_RELASZ:
13498 /* Don't count procedure linkage table relocs in the
13499 overall reloc count. */
13500 s = htab->relplt;
13501 if (s == NULL)
13502 continue;
13503 dyn.d_un.d_val -= s->size;
13504 break;
13505
13506 case DT_RELA:
13507 /* We may not be using the standard ELF linker script.
13508 If .rela.plt is the first .rela section, we adjust
13509 DT_RELA to not include it. */
13510 s = htab->relplt;
13511 if (s == NULL)
13512 continue;
13513 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13514 continue;
13515 dyn.d_un.d_ptr += s->size;
13516 break;
13517 }
13518
13519 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13520 }
13521 }
13522
13523 if (htab->got != NULL && htab->got->size != 0)
13524 {
13525 /* Fill in the first entry in the global offset table.
13526 We use it to hold the link-time TOCbase. */
13527 bfd_put_64 (output_bfd,
13528 elf_gp (output_bfd) + TOC_BASE_OFF,
13529 htab->got->contents);
13530
13531 /* Set .got entry size. */
13532 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13533 }
13534
13535 if (htab->plt != NULL && htab->plt->size != 0)
13536 {
13537 /* Set .plt entry size. */
13538 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13539 = PLT_ENTRY_SIZE;
13540 }
13541
13542 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13543 brlt ourselves if emitrelocations. */
13544 if (htab->brlt != NULL
13545 && htab->brlt->reloc_count != 0
13546 && !_bfd_elf_link_output_relocs (output_bfd,
13547 htab->brlt,
13548 elf_section_data (htab->brlt)->rela.hdr,
13549 elf_section_data (htab->brlt)->relocs,
13550 NULL))
13551 return FALSE;
13552
13553 if (htab->glink != NULL
13554 && htab->glink->reloc_count != 0
13555 && !_bfd_elf_link_output_relocs (output_bfd,
13556 htab->glink,
13557 elf_section_data (htab->glink)->rela.hdr,
13558 elf_section_data (htab->glink)->relocs,
13559 NULL))
13560 return FALSE;
13561
13562 /* We need to handle writing out multiple GOT sections ourselves,
13563 since we didn't add them to DYNOBJ. We know dynobj is the first
13564 bfd. */
13565 while ((dynobj = dynobj->link_next) != NULL)
13566 {
13567 asection *s;
13568
13569 if (!is_ppc64_elf (dynobj))
13570 continue;
13571
13572 s = ppc64_elf_tdata (dynobj)->got;
13573 if (s != NULL
13574 && s->size != 0
13575 && s->output_section != bfd_abs_section_ptr
13576 && !bfd_set_section_contents (output_bfd, s->output_section,
13577 s->contents, s->output_offset,
13578 s->size))
13579 return FALSE;
13580 s = ppc64_elf_tdata (dynobj)->relgot;
13581 if (s != NULL
13582 && s->size != 0
13583 && s->output_section != bfd_abs_section_ptr
13584 && !bfd_set_section_contents (output_bfd, s->output_section,
13585 s->contents, s->output_offset,
13586 s->size))
13587 return FALSE;
13588 }
13589
13590 return TRUE;
13591 }
13592
13593 #include "elf64-target.h"
This page took 0.507709 seconds and 4 git commands to generate.