]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf64-ppc.c
Switch sources over to use the GPL version 3
[thirdparty/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
3 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra <amodra@bigpond.net.au>
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_MACHINE_CODE EM_PPC64
65 #define ELF_MAXPAGESIZE 0x10000
66 #define ELF_COMMONPAGESIZE 0x1000
67 #define elf_info_to_howto ppc64_elf_info_to_howto
68
69 #define elf_backend_want_got_sym 0
70 #define elf_backend_want_plt_sym 0
71 #define elf_backend_plt_alignment 3
72 #define elf_backend_plt_not_loaded 1
73 #define elf_backend_got_header_size 8
74 #define elf_backend_can_gc_sections 1
75 #define elf_backend_can_refcount 1
76 #define elf_backend_rela_normal 1
77 #define elf_backend_default_execstack 0
78
79 #define bfd_elf64_mkobject ppc64_elf_mkobject
80 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
81 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
82 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
83 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
84 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
85 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
86 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
87
88 #define elf_backend_object_p ppc64_elf_object_p
89 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
90 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
91 #define elf_backend_write_core_note ppc64_elf_write_core_note
92 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
93 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
94 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
95 #define elf_backend_check_directives ppc64_elf_check_directives
96 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
97 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
98 #define elf_backend_check_relocs ppc64_elf_check_relocs
99 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
100 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
101 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
102 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
103 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
104 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
105 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
106 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
107 #define elf_backend_action_discarded ppc64_elf_action_discarded
108 #define elf_backend_relocate_section ppc64_elf_relocate_section
109 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
110 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
111 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
112 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
113 #define elf_backend_special_sections ppc64_elf_special_sections
114
115 /* The name of the dynamic interpreter. This is put in the .interp
116 section. */
117 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
118
119 /* The size in bytes of an entry in the procedure linkage table. */
120 #define PLT_ENTRY_SIZE 24
121
122 /* The initial size of the plt reserved for the dynamic linker. */
123 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
124
125 /* TOC base pointers offset from start of TOC. */
126 #define TOC_BASE_OFF 0x8000
127
128 /* Offset of tp and dtp pointers from start of TLS block. */
129 #define TP_OFFSET 0x7000
130 #define DTP_OFFSET 0x8000
131
132 /* .plt call stub instructions. The normal stub is like this, but
133 sometimes the .plt entry crosses a 64k boundary and we need to
134 insert an addis to adjust r12. */
135 #define PLT_CALL_STUB_SIZE (7*4)
136 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
137 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
138 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
139 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
140 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
141 /* ld %r11,xxx+16@l(%r12) */
142 #define BCTR 0x4e800420 /* bctr */
143
144
145 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
146 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
147 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
148
149 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
150
151 /* glink call stub instructions. We enter with the index in R0. */
152 #define GLINK_CALL_STUB_SIZE (16*4)
153 /* 0: */
154 /* .quad plt0-1f */
155 /* __glink: */
156 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
157 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
158 /* 1: */
159 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
160 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
161 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
162 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
163 /* ld %11,0(%12) */
164 /* ld %2,8(%12) */
165 /* mtctr %11 */
166 /* ld %11,16(%12) */
167 /* bctr */
168
169 /* Pad with this. */
170 #define NOP 0x60000000
171
172 /* Some other nops. */
173 #define CROR_151515 0x4def7b82
174 #define CROR_313131 0x4ffffb82
175
176 /* .glink entries for the first 32k functions are two instructions. */
177 #define LI_R0_0 0x38000000 /* li %r0,0 */
178 #define B_DOT 0x48000000 /* b . */
179
180 /* After that, we need two instructions to load the index, followed by
181 a branch. */
182 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
183 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
184
185 /* Instructions used by the save and restore reg functions. */
186 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
187 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
188 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
189 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
190 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
191 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
192 #define LI_R12_0 0x39800000 /* li %r12,0 */
193 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
194 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
195 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
196 #define BLR 0x4e800020 /* blr */
197
198 /* Since .opd is an array of descriptors and each entry will end up
199 with identical R_PPC64_RELATIVE relocs, there is really no need to
200 propagate .opd relocs; The dynamic linker should be taught to
201 relocate .opd without reloc entries. */
202 #ifndef NO_OPD_RELOCS
203 #define NO_OPD_RELOCS 0
204 #endif
205 \f
206 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
207
208 /* Relocation HOWTO's. */
209 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
210
211 static reloc_howto_type ppc64_elf_howto_raw[] = {
212 /* This reloc does nothing. */
213 HOWTO (R_PPC64_NONE, /* type */
214 0, /* rightshift */
215 2, /* size (0 = byte, 1 = short, 2 = long) */
216 32, /* bitsize */
217 FALSE, /* pc_relative */
218 0, /* bitpos */
219 complain_overflow_dont, /* complain_on_overflow */
220 bfd_elf_generic_reloc, /* special_function */
221 "R_PPC64_NONE", /* name */
222 FALSE, /* partial_inplace */
223 0, /* src_mask */
224 0, /* dst_mask */
225 FALSE), /* pcrel_offset */
226
227 /* A standard 32 bit relocation. */
228 HOWTO (R_PPC64_ADDR32, /* type */
229 0, /* rightshift */
230 2, /* size (0 = byte, 1 = short, 2 = long) */
231 32, /* bitsize */
232 FALSE, /* pc_relative */
233 0, /* bitpos */
234 complain_overflow_bitfield, /* complain_on_overflow */
235 bfd_elf_generic_reloc, /* special_function */
236 "R_PPC64_ADDR32", /* name */
237 FALSE, /* partial_inplace */
238 0, /* src_mask */
239 0xffffffff, /* dst_mask */
240 FALSE), /* pcrel_offset */
241
242 /* An absolute 26 bit branch; the lower two bits must be zero.
243 FIXME: we don't check that, we just clear them. */
244 HOWTO (R_PPC64_ADDR24, /* type */
245 0, /* rightshift */
246 2, /* size (0 = byte, 1 = short, 2 = long) */
247 26, /* bitsize */
248 FALSE, /* pc_relative */
249 0, /* bitpos */
250 complain_overflow_bitfield, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_PPC64_ADDR24", /* name */
253 FALSE, /* partial_inplace */
254 0, /* src_mask */
255 0x03fffffc, /* dst_mask */
256 FALSE), /* pcrel_offset */
257
258 /* A standard 16 bit relocation. */
259 HOWTO (R_PPC64_ADDR16, /* type */
260 0, /* rightshift */
261 1, /* size (0 = byte, 1 = short, 2 = long) */
262 16, /* bitsize */
263 FALSE, /* pc_relative */
264 0, /* bitpos */
265 complain_overflow_bitfield, /* complain_on_overflow */
266 bfd_elf_generic_reloc, /* special_function */
267 "R_PPC64_ADDR16", /* name */
268 FALSE, /* partial_inplace */
269 0, /* src_mask */
270 0xffff, /* dst_mask */
271 FALSE), /* pcrel_offset */
272
273 /* A 16 bit relocation without overflow. */
274 HOWTO (R_PPC64_ADDR16_LO, /* type */
275 0, /* rightshift */
276 1, /* size (0 = byte, 1 = short, 2 = long) */
277 16, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_dont,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_PPC64_ADDR16_LO", /* name */
283 FALSE, /* partial_inplace */
284 0, /* src_mask */
285 0xffff, /* dst_mask */
286 FALSE), /* pcrel_offset */
287
288 /* Bits 16-31 of an address. */
289 HOWTO (R_PPC64_ADDR16_HI, /* type */
290 16, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 FALSE, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_dont, /* complain_on_overflow */
296 bfd_elf_generic_reloc, /* special_function */
297 "R_PPC64_ADDR16_HI", /* name */
298 FALSE, /* partial_inplace */
299 0, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
302
303 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
304 bits, treated as a signed number, is negative. */
305 HOWTO (R_PPC64_ADDR16_HA, /* type */
306 16, /* rightshift */
307 1, /* size (0 = byte, 1 = short, 2 = long) */
308 16, /* bitsize */
309 FALSE, /* pc_relative */
310 0, /* bitpos */
311 complain_overflow_dont, /* complain_on_overflow */
312 ppc64_elf_ha_reloc, /* special_function */
313 "R_PPC64_ADDR16_HA", /* name */
314 FALSE, /* partial_inplace */
315 0, /* src_mask */
316 0xffff, /* dst_mask */
317 FALSE), /* pcrel_offset */
318
319 /* An absolute 16 bit branch; the lower two bits must be zero.
320 FIXME: we don't check that, we just clear them. */
321 HOWTO (R_PPC64_ADDR14, /* type */
322 0, /* rightshift */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
324 16, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 ppc64_elf_branch_reloc, /* special_function */
329 "R_PPC64_ADDR14", /* name */
330 FALSE, /* partial_inplace */
331 0, /* src_mask */
332 0x0000fffc, /* dst_mask */
333 FALSE), /* pcrel_offset */
334
335 /* An absolute 16 bit branch, for which bit 10 should be set to
336 indicate that the branch is expected to be taken. The lower two
337 bits must be zero. */
338 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
339 0, /* rightshift */
340 2, /* size (0 = byte, 1 = short, 2 = long) */
341 16, /* bitsize */
342 FALSE, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_bitfield, /* complain_on_overflow */
345 ppc64_elf_brtaken_reloc, /* special_function */
346 "R_PPC64_ADDR14_BRTAKEN",/* name */
347 FALSE, /* partial_inplace */
348 0, /* src_mask */
349 0x0000fffc, /* dst_mask */
350 FALSE), /* pcrel_offset */
351
352 /* An absolute 16 bit branch, for which bit 10 should be set to
353 indicate that the branch is not expected to be taken. The lower
354 two bits must be zero. */
355 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
356 0, /* rightshift */
357 2, /* size (0 = byte, 1 = short, 2 = long) */
358 16, /* bitsize */
359 FALSE, /* pc_relative */
360 0, /* bitpos */
361 complain_overflow_bitfield, /* complain_on_overflow */
362 ppc64_elf_brtaken_reloc, /* special_function */
363 "R_PPC64_ADDR14_BRNTAKEN",/* name */
364 FALSE, /* partial_inplace */
365 0, /* src_mask */
366 0x0000fffc, /* dst_mask */
367 FALSE), /* pcrel_offset */
368
369 /* A relative 26 bit branch; the lower two bits must be zero. */
370 HOWTO (R_PPC64_REL24, /* type */
371 0, /* rightshift */
372 2, /* size (0 = byte, 1 = short, 2 = long) */
373 26, /* bitsize */
374 TRUE, /* pc_relative */
375 0, /* bitpos */
376 complain_overflow_signed, /* complain_on_overflow */
377 ppc64_elf_branch_reloc, /* special_function */
378 "R_PPC64_REL24", /* name */
379 FALSE, /* partial_inplace */
380 0, /* src_mask */
381 0x03fffffc, /* dst_mask */
382 TRUE), /* pcrel_offset */
383
384 /* A relative 16 bit branch; the lower two bits must be zero. */
385 HOWTO (R_PPC64_REL14, /* type */
386 0, /* rightshift */
387 2, /* size (0 = byte, 1 = short, 2 = long) */
388 16, /* bitsize */
389 TRUE, /* pc_relative */
390 0, /* bitpos */
391 complain_overflow_signed, /* complain_on_overflow */
392 ppc64_elf_branch_reloc, /* special_function */
393 "R_PPC64_REL14", /* name */
394 FALSE, /* partial_inplace */
395 0, /* src_mask */
396 0x0000fffc, /* dst_mask */
397 TRUE), /* pcrel_offset */
398
399 /* A relative 16 bit branch. Bit 10 should be set to indicate that
400 the branch is expected to be taken. The lower two bits must be
401 zero. */
402 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
403 0, /* rightshift */
404 2, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 TRUE, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_signed, /* complain_on_overflow */
409 ppc64_elf_brtaken_reloc, /* special_function */
410 "R_PPC64_REL14_BRTAKEN", /* name */
411 FALSE, /* partial_inplace */
412 0, /* src_mask */
413 0x0000fffc, /* dst_mask */
414 TRUE), /* pcrel_offset */
415
416 /* A relative 16 bit branch. Bit 10 should be set to indicate that
417 the branch is not expected to be taken. The lower two bits must
418 be zero. */
419 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
420 0, /* rightshift */
421 2, /* size (0 = byte, 1 = short, 2 = long) */
422 16, /* bitsize */
423 TRUE, /* pc_relative */
424 0, /* bitpos */
425 complain_overflow_signed, /* complain_on_overflow */
426 ppc64_elf_brtaken_reloc, /* special_function */
427 "R_PPC64_REL14_BRNTAKEN",/* name */
428 FALSE, /* partial_inplace */
429 0, /* src_mask */
430 0x0000fffc, /* dst_mask */
431 TRUE), /* pcrel_offset */
432
433 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
434 symbol. */
435 HOWTO (R_PPC64_GOT16, /* type */
436 0, /* rightshift */
437 1, /* size (0 = byte, 1 = short, 2 = long) */
438 16, /* bitsize */
439 FALSE, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_signed, /* complain_on_overflow */
442 ppc64_elf_unhandled_reloc, /* special_function */
443 "R_PPC64_GOT16", /* name */
444 FALSE, /* partial_inplace */
445 0, /* src_mask */
446 0xffff, /* dst_mask */
447 FALSE), /* pcrel_offset */
448
449 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
450 the symbol. */
451 HOWTO (R_PPC64_GOT16_LO, /* type */
452 0, /* rightshift */
453 1, /* size (0 = byte, 1 = short, 2 = long) */
454 16, /* bitsize */
455 FALSE, /* pc_relative */
456 0, /* bitpos */
457 complain_overflow_dont, /* complain_on_overflow */
458 ppc64_elf_unhandled_reloc, /* special_function */
459 "R_PPC64_GOT16_LO", /* name */
460 FALSE, /* partial_inplace */
461 0, /* src_mask */
462 0xffff, /* dst_mask */
463 FALSE), /* pcrel_offset */
464
465 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
466 the symbol. */
467 HOWTO (R_PPC64_GOT16_HI, /* type */
468 16, /* rightshift */
469 1, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
471 FALSE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_dont,/* complain_on_overflow */
474 ppc64_elf_unhandled_reloc, /* special_function */
475 "R_PPC64_GOT16_HI", /* name */
476 FALSE, /* partial_inplace */
477 0, /* src_mask */
478 0xffff, /* dst_mask */
479 FALSE), /* pcrel_offset */
480
481 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
482 the symbol. */
483 HOWTO (R_PPC64_GOT16_HA, /* type */
484 16, /* rightshift */
485 1, /* size (0 = byte, 1 = short, 2 = long) */
486 16, /* bitsize */
487 FALSE, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_dont,/* complain_on_overflow */
490 ppc64_elf_unhandled_reloc, /* special_function */
491 "R_PPC64_GOT16_HA", /* name */
492 FALSE, /* partial_inplace */
493 0, /* src_mask */
494 0xffff, /* dst_mask */
495 FALSE), /* pcrel_offset */
496
497 /* This is used only by the dynamic linker. The symbol should exist
498 both in the object being run and in some shared library. The
499 dynamic linker copies the data addressed by the symbol from the
500 shared library into the object, because the object being
501 run has to have the data at some particular address. */
502 HOWTO (R_PPC64_COPY, /* type */
503 0, /* rightshift */
504 0, /* this one is variable size */
505 0, /* bitsize */
506 FALSE, /* pc_relative */
507 0, /* bitpos */
508 complain_overflow_dont, /* complain_on_overflow */
509 ppc64_elf_unhandled_reloc, /* special_function */
510 "R_PPC64_COPY", /* name */
511 FALSE, /* partial_inplace */
512 0, /* src_mask */
513 0, /* dst_mask */
514 FALSE), /* pcrel_offset */
515
516 /* Like R_PPC64_ADDR64, but used when setting global offset table
517 entries. */
518 HOWTO (R_PPC64_GLOB_DAT, /* type */
519 0, /* rightshift */
520 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
521 64, /* bitsize */
522 FALSE, /* pc_relative */
523 0, /* bitpos */
524 complain_overflow_dont, /* complain_on_overflow */
525 ppc64_elf_unhandled_reloc, /* special_function */
526 "R_PPC64_GLOB_DAT", /* name */
527 FALSE, /* partial_inplace */
528 0, /* src_mask */
529 ONES (64), /* dst_mask */
530 FALSE), /* pcrel_offset */
531
532 /* Created by the link editor. Marks a procedure linkage table
533 entry for a symbol. */
534 HOWTO (R_PPC64_JMP_SLOT, /* type */
535 0, /* rightshift */
536 0, /* size (0 = byte, 1 = short, 2 = long) */
537 0, /* bitsize */
538 FALSE, /* pc_relative */
539 0, /* bitpos */
540 complain_overflow_dont, /* complain_on_overflow */
541 ppc64_elf_unhandled_reloc, /* special_function */
542 "R_PPC64_JMP_SLOT", /* name */
543 FALSE, /* partial_inplace */
544 0, /* src_mask */
545 0, /* dst_mask */
546 FALSE), /* pcrel_offset */
547
548 /* Used only by the dynamic linker. When the object is run, this
549 doubleword64 is set to the load address of the object, plus the
550 addend. */
551 HOWTO (R_PPC64_RELATIVE, /* type */
552 0, /* rightshift */
553 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
554 64, /* bitsize */
555 FALSE, /* pc_relative */
556 0, /* bitpos */
557 complain_overflow_dont, /* complain_on_overflow */
558 bfd_elf_generic_reloc, /* special_function */
559 "R_PPC64_RELATIVE", /* name */
560 FALSE, /* partial_inplace */
561 0, /* src_mask */
562 ONES (64), /* dst_mask */
563 FALSE), /* pcrel_offset */
564
565 /* Like R_PPC64_ADDR32, but may be unaligned. */
566 HOWTO (R_PPC64_UADDR32, /* type */
567 0, /* rightshift */
568 2, /* size (0 = byte, 1 = short, 2 = long) */
569 32, /* bitsize */
570 FALSE, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_bitfield, /* complain_on_overflow */
573 bfd_elf_generic_reloc, /* special_function */
574 "R_PPC64_UADDR32", /* name */
575 FALSE, /* partial_inplace */
576 0, /* src_mask */
577 0xffffffff, /* dst_mask */
578 FALSE), /* pcrel_offset */
579
580 /* Like R_PPC64_ADDR16, but may be unaligned. */
581 HOWTO (R_PPC64_UADDR16, /* type */
582 0, /* rightshift */
583 1, /* size (0 = byte, 1 = short, 2 = long) */
584 16, /* bitsize */
585 FALSE, /* pc_relative */
586 0, /* bitpos */
587 complain_overflow_bitfield, /* complain_on_overflow */
588 bfd_elf_generic_reloc, /* special_function */
589 "R_PPC64_UADDR16", /* name */
590 FALSE, /* partial_inplace */
591 0, /* src_mask */
592 0xffff, /* dst_mask */
593 FALSE), /* pcrel_offset */
594
595 /* 32-bit PC relative. */
596 HOWTO (R_PPC64_REL32, /* type */
597 0, /* rightshift */
598 2, /* size (0 = byte, 1 = short, 2 = long) */
599 32, /* bitsize */
600 TRUE, /* pc_relative */
601 0, /* bitpos */
602 /* FIXME: Verify. Was complain_overflow_bitfield. */
603 complain_overflow_signed, /* complain_on_overflow */
604 bfd_elf_generic_reloc, /* special_function */
605 "R_PPC64_REL32", /* name */
606 FALSE, /* partial_inplace */
607 0, /* src_mask */
608 0xffffffff, /* dst_mask */
609 TRUE), /* pcrel_offset */
610
611 /* 32-bit relocation to the symbol's procedure linkage table. */
612 HOWTO (R_PPC64_PLT32, /* type */
613 0, /* rightshift */
614 2, /* size (0 = byte, 1 = short, 2 = long) */
615 32, /* bitsize */
616 FALSE, /* pc_relative */
617 0, /* bitpos */
618 complain_overflow_bitfield, /* complain_on_overflow */
619 ppc64_elf_unhandled_reloc, /* special_function */
620 "R_PPC64_PLT32", /* name */
621 FALSE, /* partial_inplace */
622 0, /* src_mask */
623 0xffffffff, /* dst_mask */
624 FALSE), /* pcrel_offset */
625
626 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
627 FIXME: R_PPC64_PLTREL32 not supported. */
628 HOWTO (R_PPC64_PLTREL32, /* type */
629 0, /* rightshift */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
631 32, /* bitsize */
632 TRUE, /* pc_relative */
633 0, /* bitpos */
634 complain_overflow_signed, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_PPC64_PLTREL32", /* name */
637 FALSE, /* partial_inplace */
638 0, /* src_mask */
639 0xffffffff, /* dst_mask */
640 TRUE), /* pcrel_offset */
641
642 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
643 the symbol. */
644 HOWTO (R_PPC64_PLT16_LO, /* type */
645 0, /* rightshift */
646 1, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
648 FALSE, /* pc_relative */
649 0, /* bitpos */
650 complain_overflow_dont, /* complain_on_overflow */
651 ppc64_elf_unhandled_reloc, /* special_function */
652 "R_PPC64_PLT16_LO", /* name */
653 FALSE, /* partial_inplace */
654 0, /* src_mask */
655 0xffff, /* dst_mask */
656 FALSE), /* pcrel_offset */
657
658 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
659 the symbol. */
660 HOWTO (R_PPC64_PLT16_HI, /* type */
661 16, /* rightshift */
662 1, /* size (0 = byte, 1 = short, 2 = long) */
663 16, /* bitsize */
664 FALSE, /* pc_relative */
665 0, /* bitpos */
666 complain_overflow_dont, /* complain_on_overflow */
667 ppc64_elf_unhandled_reloc, /* special_function */
668 "R_PPC64_PLT16_HI", /* name */
669 FALSE, /* partial_inplace */
670 0, /* src_mask */
671 0xffff, /* dst_mask */
672 FALSE), /* pcrel_offset */
673
674 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
675 the symbol. */
676 HOWTO (R_PPC64_PLT16_HA, /* type */
677 16, /* rightshift */
678 1, /* size (0 = byte, 1 = short, 2 = long) */
679 16, /* bitsize */
680 FALSE, /* pc_relative */
681 0, /* bitpos */
682 complain_overflow_dont, /* complain_on_overflow */
683 ppc64_elf_unhandled_reloc, /* special_function */
684 "R_PPC64_PLT16_HA", /* name */
685 FALSE, /* partial_inplace */
686 0, /* src_mask */
687 0xffff, /* dst_mask */
688 FALSE), /* pcrel_offset */
689
690 /* 16-bit section relative relocation. */
691 HOWTO (R_PPC64_SECTOFF, /* type */
692 0, /* rightshift */
693 1, /* size (0 = byte, 1 = short, 2 = long) */
694 16, /* bitsize */
695 FALSE, /* pc_relative */
696 0, /* bitpos */
697 complain_overflow_bitfield, /* complain_on_overflow */
698 ppc64_elf_sectoff_reloc, /* special_function */
699 "R_PPC64_SECTOFF", /* name */
700 FALSE, /* partial_inplace */
701 0, /* src_mask */
702 0xffff, /* dst_mask */
703 FALSE), /* pcrel_offset */
704
705 /* Like R_PPC64_SECTOFF, but no overflow warning. */
706 HOWTO (R_PPC64_SECTOFF_LO, /* type */
707 0, /* rightshift */
708 1, /* size (0 = byte, 1 = short, 2 = long) */
709 16, /* bitsize */
710 FALSE, /* pc_relative */
711 0, /* bitpos */
712 complain_overflow_dont, /* complain_on_overflow */
713 ppc64_elf_sectoff_reloc, /* special_function */
714 "R_PPC64_SECTOFF_LO", /* name */
715 FALSE, /* partial_inplace */
716 0, /* src_mask */
717 0xffff, /* dst_mask */
718 FALSE), /* pcrel_offset */
719
720 /* 16-bit upper half section relative relocation. */
721 HOWTO (R_PPC64_SECTOFF_HI, /* type */
722 16, /* rightshift */
723 1, /* size (0 = byte, 1 = short, 2 = long) */
724 16, /* bitsize */
725 FALSE, /* pc_relative */
726 0, /* bitpos */
727 complain_overflow_dont, /* complain_on_overflow */
728 ppc64_elf_sectoff_reloc, /* special_function */
729 "R_PPC64_SECTOFF_HI", /* name */
730 FALSE, /* partial_inplace */
731 0, /* src_mask */
732 0xffff, /* dst_mask */
733 FALSE), /* pcrel_offset */
734
735 /* 16-bit upper half adjusted section relative relocation. */
736 HOWTO (R_PPC64_SECTOFF_HA, /* type */
737 16, /* rightshift */
738 1, /* size (0 = byte, 1 = short, 2 = long) */
739 16, /* bitsize */
740 FALSE, /* pc_relative */
741 0, /* bitpos */
742 complain_overflow_dont, /* complain_on_overflow */
743 ppc64_elf_sectoff_ha_reloc, /* special_function */
744 "R_PPC64_SECTOFF_HA", /* name */
745 FALSE, /* partial_inplace */
746 0, /* src_mask */
747 0xffff, /* dst_mask */
748 FALSE), /* pcrel_offset */
749
750 /* Like R_PPC64_REL24 without touching the two least significant bits. */
751 HOWTO (R_PPC64_REL30, /* type */
752 2, /* rightshift */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
754 30, /* bitsize */
755 TRUE, /* pc_relative */
756 0, /* bitpos */
757 complain_overflow_dont, /* complain_on_overflow */
758 bfd_elf_generic_reloc, /* special_function */
759 "R_PPC64_REL30", /* name */
760 FALSE, /* partial_inplace */
761 0, /* src_mask */
762 0xfffffffc, /* dst_mask */
763 TRUE), /* pcrel_offset */
764
765 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
766
767 /* A standard 64-bit relocation. */
768 HOWTO (R_PPC64_ADDR64, /* type */
769 0, /* rightshift */
770 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
771 64, /* bitsize */
772 FALSE, /* pc_relative */
773 0, /* bitpos */
774 complain_overflow_dont, /* complain_on_overflow */
775 bfd_elf_generic_reloc, /* special_function */
776 "R_PPC64_ADDR64", /* name */
777 FALSE, /* partial_inplace */
778 0, /* src_mask */
779 ONES (64), /* dst_mask */
780 FALSE), /* pcrel_offset */
781
782 /* The bits 32-47 of an address. */
783 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
784 32, /* rightshift */
785 1, /* size (0 = byte, 1 = short, 2 = long) */
786 16, /* bitsize */
787 FALSE, /* pc_relative */
788 0, /* bitpos */
789 complain_overflow_dont, /* complain_on_overflow */
790 bfd_elf_generic_reloc, /* special_function */
791 "R_PPC64_ADDR16_HIGHER", /* name */
792 FALSE, /* partial_inplace */
793 0, /* src_mask */
794 0xffff, /* dst_mask */
795 FALSE), /* pcrel_offset */
796
797 /* The bits 32-47 of an address, plus 1 if the contents of the low
798 16 bits, treated as a signed number, is negative. */
799 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
800 32, /* rightshift */
801 1, /* size (0 = byte, 1 = short, 2 = long) */
802 16, /* bitsize */
803 FALSE, /* pc_relative */
804 0, /* bitpos */
805 complain_overflow_dont, /* complain_on_overflow */
806 ppc64_elf_ha_reloc, /* special_function */
807 "R_PPC64_ADDR16_HIGHERA", /* name */
808 FALSE, /* partial_inplace */
809 0, /* src_mask */
810 0xffff, /* dst_mask */
811 FALSE), /* pcrel_offset */
812
813 /* The bits 48-63 of an address. */
814 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
815 48, /* rightshift */
816 1, /* size (0 = byte, 1 = short, 2 = long) */
817 16, /* bitsize */
818 FALSE, /* pc_relative */
819 0, /* bitpos */
820 complain_overflow_dont, /* complain_on_overflow */
821 bfd_elf_generic_reloc, /* special_function */
822 "R_PPC64_ADDR16_HIGHEST", /* name */
823 FALSE, /* partial_inplace */
824 0, /* src_mask */
825 0xffff, /* dst_mask */
826 FALSE), /* pcrel_offset */
827
828 /* The bits 48-63 of an address, plus 1 if the contents of the low
829 16 bits, treated as a signed number, is negative. */
830 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
831 48, /* rightshift */
832 1, /* size (0 = byte, 1 = short, 2 = long) */
833 16, /* bitsize */
834 FALSE, /* pc_relative */
835 0, /* bitpos */
836 complain_overflow_dont, /* complain_on_overflow */
837 ppc64_elf_ha_reloc, /* special_function */
838 "R_PPC64_ADDR16_HIGHESTA", /* name */
839 FALSE, /* partial_inplace */
840 0, /* src_mask */
841 0xffff, /* dst_mask */
842 FALSE), /* pcrel_offset */
843
844 /* Like ADDR64, but may be unaligned. */
845 HOWTO (R_PPC64_UADDR64, /* type */
846 0, /* rightshift */
847 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
848 64, /* bitsize */
849 FALSE, /* pc_relative */
850 0, /* bitpos */
851 complain_overflow_dont, /* complain_on_overflow */
852 bfd_elf_generic_reloc, /* special_function */
853 "R_PPC64_UADDR64", /* name */
854 FALSE, /* partial_inplace */
855 0, /* src_mask */
856 ONES (64), /* dst_mask */
857 FALSE), /* pcrel_offset */
858
859 /* 64-bit relative relocation. */
860 HOWTO (R_PPC64_REL64, /* type */
861 0, /* rightshift */
862 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
863 64, /* bitsize */
864 TRUE, /* pc_relative */
865 0, /* bitpos */
866 complain_overflow_dont, /* complain_on_overflow */
867 bfd_elf_generic_reloc, /* special_function */
868 "R_PPC64_REL64", /* name */
869 FALSE, /* partial_inplace */
870 0, /* src_mask */
871 ONES (64), /* dst_mask */
872 TRUE), /* pcrel_offset */
873
874 /* 64-bit relocation to the symbol's procedure linkage table. */
875 HOWTO (R_PPC64_PLT64, /* type */
876 0, /* rightshift */
877 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
878 64, /* bitsize */
879 FALSE, /* pc_relative */
880 0, /* bitpos */
881 complain_overflow_dont, /* complain_on_overflow */
882 ppc64_elf_unhandled_reloc, /* special_function */
883 "R_PPC64_PLT64", /* name */
884 FALSE, /* partial_inplace */
885 0, /* src_mask */
886 ONES (64), /* dst_mask */
887 FALSE), /* pcrel_offset */
888
889 /* 64-bit PC relative relocation to the symbol's procedure linkage
890 table. */
891 /* FIXME: R_PPC64_PLTREL64 not supported. */
892 HOWTO (R_PPC64_PLTREL64, /* type */
893 0, /* rightshift */
894 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
895 64, /* bitsize */
896 TRUE, /* pc_relative */
897 0, /* bitpos */
898 complain_overflow_dont, /* complain_on_overflow */
899 ppc64_elf_unhandled_reloc, /* special_function */
900 "R_PPC64_PLTREL64", /* name */
901 FALSE, /* partial_inplace */
902 0, /* src_mask */
903 ONES (64), /* dst_mask */
904 TRUE), /* pcrel_offset */
905
906 /* 16 bit TOC-relative relocation. */
907
908 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
909 HOWTO (R_PPC64_TOC16, /* type */
910 0, /* rightshift */
911 1, /* size (0 = byte, 1 = short, 2 = long) */
912 16, /* bitsize */
913 FALSE, /* pc_relative */
914 0, /* bitpos */
915 complain_overflow_signed, /* complain_on_overflow */
916 ppc64_elf_toc_reloc, /* special_function */
917 "R_PPC64_TOC16", /* name */
918 FALSE, /* partial_inplace */
919 0, /* src_mask */
920 0xffff, /* dst_mask */
921 FALSE), /* pcrel_offset */
922
923 /* 16 bit TOC-relative relocation without overflow. */
924
925 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
926 HOWTO (R_PPC64_TOC16_LO, /* type */
927 0, /* rightshift */
928 1, /* size (0 = byte, 1 = short, 2 = long) */
929 16, /* bitsize */
930 FALSE, /* pc_relative */
931 0, /* bitpos */
932 complain_overflow_dont, /* complain_on_overflow */
933 ppc64_elf_toc_reloc, /* special_function */
934 "R_PPC64_TOC16_LO", /* name */
935 FALSE, /* partial_inplace */
936 0, /* src_mask */
937 0xffff, /* dst_mask */
938 FALSE), /* pcrel_offset */
939
940 /* 16 bit TOC-relative relocation, high 16 bits. */
941
942 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
943 HOWTO (R_PPC64_TOC16_HI, /* type */
944 16, /* rightshift */
945 1, /* size (0 = byte, 1 = short, 2 = long) */
946 16, /* bitsize */
947 FALSE, /* pc_relative */
948 0, /* bitpos */
949 complain_overflow_dont, /* complain_on_overflow */
950 ppc64_elf_toc_reloc, /* special_function */
951 "R_PPC64_TOC16_HI", /* name */
952 FALSE, /* partial_inplace */
953 0, /* src_mask */
954 0xffff, /* dst_mask */
955 FALSE), /* pcrel_offset */
956
957 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
958 contents of the low 16 bits, treated as a signed number, is
959 negative. */
960
961 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
962 HOWTO (R_PPC64_TOC16_HA, /* type */
963 16, /* rightshift */
964 1, /* size (0 = byte, 1 = short, 2 = long) */
965 16, /* bitsize */
966 FALSE, /* pc_relative */
967 0, /* bitpos */
968 complain_overflow_dont, /* complain_on_overflow */
969 ppc64_elf_toc_ha_reloc, /* special_function */
970 "R_PPC64_TOC16_HA", /* name */
971 FALSE, /* partial_inplace */
972 0, /* src_mask */
973 0xffff, /* dst_mask */
974 FALSE), /* pcrel_offset */
975
976 /* 64-bit relocation; insert value of TOC base (.TOC.). */
977
978 /* R_PPC64_TOC 51 doubleword64 .TOC. */
979 HOWTO (R_PPC64_TOC, /* type */
980 0, /* rightshift */
981 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
982 64, /* bitsize */
983 FALSE, /* pc_relative */
984 0, /* bitpos */
985 complain_overflow_bitfield, /* complain_on_overflow */
986 ppc64_elf_toc64_reloc, /* special_function */
987 "R_PPC64_TOC", /* name */
988 FALSE, /* partial_inplace */
989 0, /* src_mask */
990 ONES (64), /* dst_mask */
991 FALSE), /* pcrel_offset */
992
993 /* Like R_PPC64_GOT16, but also informs the link editor that the
994 value to relocate may (!) refer to a PLT entry which the link
995 editor (a) may replace with the symbol value. If the link editor
996 is unable to fully resolve the symbol, it may (b) create a PLT
997 entry and store the address to the new PLT entry in the GOT.
998 This permits lazy resolution of function symbols at run time.
999 The link editor may also skip all of this and just (c) emit a
1000 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1001 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1002 HOWTO (R_PPC64_PLTGOT16, /* type */
1003 0, /* rightshift */
1004 1, /* size (0 = byte, 1 = short, 2 = long) */
1005 16, /* bitsize */
1006 FALSE, /* pc_relative */
1007 0, /* bitpos */
1008 complain_overflow_signed, /* complain_on_overflow */
1009 ppc64_elf_unhandled_reloc, /* special_function */
1010 "R_PPC64_PLTGOT16", /* name */
1011 FALSE, /* partial_inplace */
1012 0, /* src_mask */
1013 0xffff, /* dst_mask */
1014 FALSE), /* pcrel_offset */
1015
1016 /* Like R_PPC64_PLTGOT16, but without overflow. */
1017 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1018 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1019 0, /* rightshift */
1020 1, /* size (0 = byte, 1 = short, 2 = long) */
1021 16, /* bitsize */
1022 FALSE, /* pc_relative */
1023 0, /* bitpos */
1024 complain_overflow_dont, /* complain_on_overflow */
1025 ppc64_elf_unhandled_reloc, /* special_function */
1026 "R_PPC64_PLTGOT16_LO", /* name */
1027 FALSE, /* partial_inplace */
1028 0, /* src_mask */
1029 0xffff, /* dst_mask */
1030 FALSE), /* pcrel_offset */
1031
1032 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1033 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1034 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1035 16, /* rightshift */
1036 1, /* size (0 = byte, 1 = short, 2 = long) */
1037 16, /* bitsize */
1038 FALSE, /* pc_relative */
1039 0, /* bitpos */
1040 complain_overflow_dont, /* complain_on_overflow */
1041 ppc64_elf_unhandled_reloc, /* special_function */
1042 "R_PPC64_PLTGOT16_HI", /* name */
1043 FALSE, /* partial_inplace */
1044 0, /* src_mask */
1045 0xffff, /* dst_mask */
1046 FALSE), /* pcrel_offset */
1047
1048 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1049 1 if the contents of the low 16 bits, treated as a signed number,
1050 is negative. */
1051 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1052 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1053 16, /* rightshift */
1054 1, /* size (0 = byte, 1 = short, 2 = long) */
1055 16, /* bitsize */
1056 FALSE, /* pc_relative */
1057 0, /* bitpos */
1058 complain_overflow_dont,/* complain_on_overflow */
1059 ppc64_elf_unhandled_reloc, /* special_function */
1060 "R_PPC64_PLTGOT16_HA", /* name */
1061 FALSE, /* partial_inplace */
1062 0, /* src_mask */
1063 0xffff, /* dst_mask */
1064 FALSE), /* pcrel_offset */
1065
1066 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1067 HOWTO (R_PPC64_ADDR16_DS, /* type */
1068 0, /* rightshift */
1069 1, /* size (0 = byte, 1 = short, 2 = long) */
1070 16, /* bitsize */
1071 FALSE, /* pc_relative */
1072 0, /* bitpos */
1073 complain_overflow_bitfield, /* complain_on_overflow */
1074 bfd_elf_generic_reloc, /* special_function */
1075 "R_PPC64_ADDR16_DS", /* name */
1076 FALSE, /* partial_inplace */
1077 0, /* src_mask */
1078 0xfffc, /* dst_mask */
1079 FALSE), /* pcrel_offset */
1080
1081 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1082 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1083 0, /* rightshift */
1084 1, /* size (0 = byte, 1 = short, 2 = long) */
1085 16, /* bitsize */
1086 FALSE, /* pc_relative */
1087 0, /* bitpos */
1088 complain_overflow_dont,/* complain_on_overflow */
1089 bfd_elf_generic_reloc, /* special_function */
1090 "R_PPC64_ADDR16_LO_DS",/* name */
1091 FALSE, /* partial_inplace */
1092 0, /* src_mask */
1093 0xfffc, /* dst_mask */
1094 FALSE), /* pcrel_offset */
1095
1096 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1097 HOWTO (R_PPC64_GOT16_DS, /* type */
1098 0, /* rightshift */
1099 1, /* size (0 = byte, 1 = short, 2 = long) */
1100 16, /* bitsize */
1101 FALSE, /* pc_relative */
1102 0, /* bitpos */
1103 complain_overflow_signed, /* complain_on_overflow */
1104 ppc64_elf_unhandled_reloc, /* special_function */
1105 "R_PPC64_GOT16_DS", /* name */
1106 FALSE, /* partial_inplace */
1107 0, /* src_mask */
1108 0xfffc, /* dst_mask */
1109 FALSE), /* pcrel_offset */
1110
1111 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1112 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1113 0, /* rightshift */
1114 1, /* size (0 = byte, 1 = short, 2 = long) */
1115 16, /* bitsize */
1116 FALSE, /* pc_relative */
1117 0, /* bitpos */
1118 complain_overflow_dont, /* complain_on_overflow */
1119 ppc64_elf_unhandled_reloc, /* special_function */
1120 "R_PPC64_GOT16_LO_DS", /* name */
1121 FALSE, /* partial_inplace */
1122 0, /* src_mask */
1123 0xfffc, /* dst_mask */
1124 FALSE), /* pcrel_offset */
1125
1126 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1127 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1128 0, /* rightshift */
1129 1, /* size (0 = byte, 1 = short, 2 = long) */
1130 16, /* bitsize */
1131 FALSE, /* pc_relative */
1132 0, /* bitpos */
1133 complain_overflow_dont, /* complain_on_overflow */
1134 ppc64_elf_unhandled_reloc, /* special_function */
1135 "R_PPC64_PLT16_LO_DS", /* name */
1136 FALSE, /* partial_inplace */
1137 0, /* src_mask */
1138 0xfffc, /* dst_mask */
1139 FALSE), /* pcrel_offset */
1140
1141 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1142 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1143 0, /* rightshift */
1144 1, /* size (0 = byte, 1 = short, 2 = long) */
1145 16, /* bitsize */
1146 FALSE, /* pc_relative */
1147 0, /* bitpos */
1148 complain_overflow_bitfield, /* complain_on_overflow */
1149 ppc64_elf_sectoff_reloc, /* special_function */
1150 "R_PPC64_SECTOFF_DS", /* name */
1151 FALSE, /* partial_inplace */
1152 0, /* src_mask */
1153 0xfffc, /* dst_mask */
1154 FALSE), /* pcrel_offset */
1155
1156 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1157 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1158 0, /* rightshift */
1159 1, /* size (0 = byte, 1 = short, 2 = long) */
1160 16, /* bitsize */
1161 FALSE, /* pc_relative */
1162 0, /* bitpos */
1163 complain_overflow_dont, /* complain_on_overflow */
1164 ppc64_elf_sectoff_reloc, /* special_function */
1165 "R_PPC64_SECTOFF_LO_DS",/* name */
1166 FALSE, /* partial_inplace */
1167 0, /* src_mask */
1168 0xfffc, /* dst_mask */
1169 FALSE), /* pcrel_offset */
1170
1171 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1172 HOWTO (R_PPC64_TOC16_DS, /* type */
1173 0, /* rightshift */
1174 1, /* size (0 = byte, 1 = short, 2 = long) */
1175 16, /* bitsize */
1176 FALSE, /* pc_relative */
1177 0, /* bitpos */
1178 complain_overflow_signed, /* complain_on_overflow */
1179 ppc64_elf_toc_reloc, /* special_function */
1180 "R_PPC64_TOC16_DS", /* name */
1181 FALSE, /* partial_inplace */
1182 0, /* src_mask */
1183 0xfffc, /* dst_mask */
1184 FALSE), /* pcrel_offset */
1185
1186 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1187 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1188 0, /* rightshift */
1189 1, /* size (0 = byte, 1 = short, 2 = long) */
1190 16, /* bitsize */
1191 FALSE, /* pc_relative */
1192 0, /* bitpos */
1193 complain_overflow_dont, /* complain_on_overflow */
1194 ppc64_elf_toc_reloc, /* special_function */
1195 "R_PPC64_TOC16_LO_DS", /* name */
1196 FALSE, /* partial_inplace */
1197 0, /* src_mask */
1198 0xfffc, /* dst_mask */
1199 FALSE), /* pcrel_offset */
1200
1201 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1202 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1203 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1204 0, /* rightshift */
1205 1, /* size (0 = byte, 1 = short, 2 = long) */
1206 16, /* bitsize */
1207 FALSE, /* pc_relative */
1208 0, /* bitpos */
1209 complain_overflow_signed, /* complain_on_overflow */
1210 ppc64_elf_unhandled_reloc, /* special_function */
1211 "R_PPC64_PLTGOT16_DS", /* name */
1212 FALSE, /* partial_inplace */
1213 0, /* src_mask */
1214 0xfffc, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1216
1217 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1218 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1219 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1220 0, /* rightshift */
1221 1, /* size (0 = byte, 1 = short, 2 = long) */
1222 16, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_dont, /* complain_on_overflow */
1226 ppc64_elf_unhandled_reloc, /* special_function */
1227 "R_PPC64_PLTGOT16_LO_DS",/* name */
1228 FALSE, /* partial_inplace */
1229 0, /* src_mask */
1230 0xfffc, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Marker reloc for TLS. */
1234 HOWTO (R_PPC64_TLS,
1235 0, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 32, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_dont, /* complain_on_overflow */
1241 bfd_elf_generic_reloc, /* special_function */
1242 "R_PPC64_TLS", /* name */
1243 FALSE, /* partial_inplace */
1244 0, /* src_mask */
1245 0, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* Computes the load module index of the load module that contains the
1249 definition of its TLS sym. */
1250 HOWTO (R_PPC64_DTPMOD64,
1251 0, /* rightshift */
1252 4, /* size (0 = byte, 1 = short, 2 = long) */
1253 64, /* bitsize */
1254 FALSE, /* pc_relative */
1255 0, /* bitpos */
1256 complain_overflow_dont, /* complain_on_overflow */
1257 ppc64_elf_unhandled_reloc, /* special_function */
1258 "R_PPC64_DTPMOD64", /* name */
1259 FALSE, /* partial_inplace */
1260 0, /* src_mask */
1261 ONES (64), /* dst_mask */
1262 FALSE), /* pcrel_offset */
1263
1264 /* Computes a dtv-relative displacement, the difference between the value
1265 of sym+add and the base address of the thread-local storage block that
1266 contains the definition of sym, minus 0x8000. */
1267 HOWTO (R_PPC64_DTPREL64,
1268 0, /* rightshift */
1269 4, /* size (0 = byte, 1 = short, 2 = long) */
1270 64, /* bitsize */
1271 FALSE, /* pc_relative */
1272 0, /* bitpos */
1273 complain_overflow_dont, /* complain_on_overflow */
1274 ppc64_elf_unhandled_reloc, /* special_function */
1275 "R_PPC64_DTPREL64", /* name */
1276 FALSE, /* partial_inplace */
1277 0, /* src_mask */
1278 ONES (64), /* dst_mask */
1279 FALSE), /* pcrel_offset */
1280
1281 /* A 16 bit dtprel reloc. */
1282 HOWTO (R_PPC64_DTPREL16,
1283 0, /* rightshift */
1284 1, /* size (0 = byte, 1 = short, 2 = long) */
1285 16, /* bitsize */
1286 FALSE, /* pc_relative */
1287 0, /* bitpos */
1288 complain_overflow_signed, /* complain_on_overflow */
1289 ppc64_elf_unhandled_reloc, /* special_function */
1290 "R_PPC64_DTPREL16", /* name */
1291 FALSE, /* partial_inplace */
1292 0, /* src_mask */
1293 0xffff, /* dst_mask */
1294 FALSE), /* pcrel_offset */
1295
1296 /* Like DTPREL16, but no overflow. */
1297 HOWTO (R_PPC64_DTPREL16_LO,
1298 0, /* rightshift */
1299 1, /* size (0 = byte, 1 = short, 2 = long) */
1300 16, /* bitsize */
1301 FALSE, /* pc_relative */
1302 0, /* bitpos */
1303 complain_overflow_dont, /* complain_on_overflow */
1304 ppc64_elf_unhandled_reloc, /* special_function */
1305 "R_PPC64_DTPREL16_LO", /* name */
1306 FALSE, /* partial_inplace */
1307 0, /* src_mask */
1308 0xffff, /* dst_mask */
1309 FALSE), /* pcrel_offset */
1310
1311 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1312 HOWTO (R_PPC64_DTPREL16_HI,
1313 16, /* rightshift */
1314 1, /* size (0 = byte, 1 = short, 2 = long) */
1315 16, /* bitsize */
1316 FALSE, /* pc_relative */
1317 0, /* bitpos */
1318 complain_overflow_dont, /* complain_on_overflow */
1319 ppc64_elf_unhandled_reloc, /* special_function */
1320 "R_PPC64_DTPREL16_HI", /* name */
1321 FALSE, /* partial_inplace */
1322 0, /* src_mask */
1323 0xffff, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1325
1326 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1327 HOWTO (R_PPC64_DTPREL16_HA,
1328 16, /* rightshift */
1329 1, /* size (0 = byte, 1 = short, 2 = long) */
1330 16, /* bitsize */
1331 FALSE, /* pc_relative */
1332 0, /* bitpos */
1333 complain_overflow_dont, /* complain_on_overflow */
1334 ppc64_elf_unhandled_reloc, /* special_function */
1335 "R_PPC64_DTPREL16_HA", /* name */
1336 FALSE, /* partial_inplace */
1337 0, /* src_mask */
1338 0xffff, /* dst_mask */
1339 FALSE), /* pcrel_offset */
1340
1341 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1342 HOWTO (R_PPC64_DTPREL16_HIGHER,
1343 32, /* rightshift */
1344 1, /* size (0 = byte, 1 = short, 2 = long) */
1345 16, /* bitsize */
1346 FALSE, /* pc_relative */
1347 0, /* bitpos */
1348 complain_overflow_dont, /* complain_on_overflow */
1349 ppc64_elf_unhandled_reloc, /* special_function */
1350 "R_PPC64_DTPREL16_HIGHER", /* name */
1351 FALSE, /* partial_inplace */
1352 0, /* src_mask */
1353 0xffff, /* dst_mask */
1354 FALSE), /* pcrel_offset */
1355
1356 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1357 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1358 32, /* rightshift */
1359 1, /* size (0 = byte, 1 = short, 2 = long) */
1360 16, /* bitsize */
1361 FALSE, /* pc_relative */
1362 0, /* bitpos */
1363 complain_overflow_dont, /* complain_on_overflow */
1364 ppc64_elf_unhandled_reloc, /* special_function */
1365 "R_PPC64_DTPREL16_HIGHERA", /* name */
1366 FALSE, /* partial_inplace */
1367 0, /* src_mask */
1368 0xffff, /* dst_mask */
1369 FALSE), /* pcrel_offset */
1370
1371 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1372 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1373 48, /* rightshift */
1374 1, /* size (0 = byte, 1 = short, 2 = long) */
1375 16, /* bitsize */
1376 FALSE, /* pc_relative */
1377 0, /* bitpos */
1378 complain_overflow_dont, /* complain_on_overflow */
1379 ppc64_elf_unhandled_reloc, /* special_function */
1380 "R_PPC64_DTPREL16_HIGHEST", /* name */
1381 FALSE, /* partial_inplace */
1382 0, /* src_mask */
1383 0xffff, /* dst_mask */
1384 FALSE), /* pcrel_offset */
1385
1386 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1387 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1388 48, /* rightshift */
1389 1, /* size (0 = byte, 1 = short, 2 = long) */
1390 16, /* bitsize */
1391 FALSE, /* pc_relative */
1392 0, /* bitpos */
1393 complain_overflow_dont, /* complain_on_overflow */
1394 ppc64_elf_unhandled_reloc, /* special_function */
1395 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1396 FALSE, /* partial_inplace */
1397 0, /* src_mask */
1398 0xffff, /* dst_mask */
1399 FALSE), /* pcrel_offset */
1400
1401 /* Like DTPREL16, but for insns with a DS field. */
1402 HOWTO (R_PPC64_DTPREL16_DS,
1403 0, /* rightshift */
1404 1, /* size (0 = byte, 1 = short, 2 = long) */
1405 16, /* bitsize */
1406 FALSE, /* pc_relative */
1407 0, /* bitpos */
1408 complain_overflow_signed, /* complain_on_overflow */
1409 ppc64_elf_unhandled_reloc, /* special_function */
1410 "R_PPC64_DTPREL16_DS", /* name */
1411 FALSE, /* partial_inplace */
1412 0, /* src_mask */
1413 0xfffc, /* dst_mask */
1414 FALSE), /* pcrel_offset */
1415
1416 /* Like DTPREL16_DS, but no overflow. */
1417 HOWTO (R_PPC64_DTPREL16_LO_DS,
1418 0, /* rightshift */
1419 1, /* size (0 = byte, 1 = short, 2 = long) */
1420 16, /* bitsize */
1421 FALSE, /* pc_relative */
1422 0, /* bitpos */
1423 complain_overflow_dont, /* complain_on_overflow */
1424 ppc64_elf_unhandled_reloc, /* special_function */
1425 "R_PPC64_DTPREL16_LO_DS", /* name */
1426 FALSE, /* partial_inplace */
1427 0, /* src_mask */
1428 0xfffc, /* dst_mask */
1429 FALSE), /* pcrel_offset */
1430
1431 /* Computes a tp-relative displacement, the difference between the value of
1432 sym+add and the value of the thread pointer (r13). */
1433 HOWTO (R_PPC64_TPREL64,
1434 0, /* rightshift */
1435 4, /* size (0 = byte, 1 = short, 2 = long) */
1436 64, /* bitsize */
1437 FALSE, /* pc_relative */
1438 0, /* bitpos */
1439 complain_overflow_dont, /* complain_on_overflow */
1440 ppc64_elf_unhandled_reloc, /* special_function */
1441 "R_PPC64_TPREL64", /* name */
1442 FALSE, /* partial_inplace */
1443 0, /* src_mask */
1444 ONES (64), /* dst_mask */
1445 FALSE), /* pcrel_offset */
1446
1447 /* A 16 bit tprel reloc. */
1448 HOWTO (R_PPC64_TPREL16,
1449 0, /* rightshift */
1450 1, /* size (0 = byte, 1 = short, 2 = long) */
1451 16, /* bitsize */
1452 FALSE, /* pc_relative */
1453 0, /* bitpos */
1454 complain_overflow_signed, /* complain_on_overflow */
1455 ppc64_elf_unhandled_reloc, /* special_function */
1456 "R_PPC64_TPREL16", /* name */
1457 FALSE, /* partial_inplace */
1458 0, /* src_mask */
1459 0xffff, /* dst_mask */
1460 FALSE), /* pcrel_offset */
1461
1462 /* Like TPREL16, but no overflow. */
1463 HOWTO (R_PPC64_TPREL16_LO,
1464 0, /* rightshift */
1465 1, /* size (0 = byte, 1 = short, 2 = long) */
1466 16, /* bitsize */
1467 FALSE, /* pc_relative */
1468 0, /* bitpos */
1469 complain_overflow_dont, /* complain_on_overflow */
1470 ppc64_elf_unhandled_reloc, /* special_function */
1471 "R_PPC64_TPREL16_LO", /* name */
1472 FALSE, /* partial_inplace */
1473 0, /* src_mask */
1474 0xffff, /* dst_mask */
1475 FALSE), /* pcrel_offset */
1476
1477 /* Like TPREL16_LO, but next higher group of 16 bits. */
1478 HOWTO (R_PPC64_TPREL16_HI,
1479 16, /* rightshift */
1480 1, /* size (0 = byte, 1 = short, 2 = long) */
1481 16, /* bitsize */
1482 FALSE, /* pc_relative */
1483 0, /* bitpos */
1484 complain_overflow_dont, /* complain_on_overflow */
1485 ppc64_elf_unhandled_reloc, /* special_function */
1486 "R_PPC64_TPREL16_HI", /* name */
1487 FALSE, /* partial_inplace */
1488 0, /* src_mask */
1489 0xffff, /* dst_mask */
1490 FALSE), /* pcrel_offset */
1491
1492 /* Like TPREL16_HI, but adjust for low 16 bits. */
1493 HOWTO (R_PPC64_TPREL16_HA,
1494 16, /* rightshift */
1495 1, /* size (0 = byte, 1 = short, 2 = long) */
1496 16, /* bitsize */
1497 FALSE, /* pc_relative */
1498 0, /* bitpos */
1499 complain_overflow_dont, /* complain_on_overflow */
1500 ppc64_elf_unhandled_reloc, /* special_function */
1501 "R_PPC64_TPREL16_HA", /* name */
1502 FALSE, /* partial_inplace */
1503 0, /* src_mask */
1504 0xffff, /* dst_mask */
1505 FALSE), /* pcrel_offset */
1506
1507 /* Like TPREL16_HI, but next higher group of 16 bits. */
1508 HOWTO (R_PPC64_TPREL16_HIGHER,
1509 32, /* rightshift */
1510 1, /* size (0 = byte, 1 = short, 2 = long) */
1511 16, /* bitsize */
1512 FALSE, /* pc_relative */
1513 0, /* bitpos */
1514 complain_overflow_dont, /* complain_on_overflow */
1515 ppc64_elf_unhandled_reloc, /* special_function */
1516 "R_PPC64_TPREL16_HIGHER", /* name */
1517 FALSE, /* partial_inplace */
1518 0, /* src_mask */
1519 0xffff, /* dst_mask */
1520 FALSE), /* pcrel_offset */
1521
1522 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1523 HOWTO (R_PPC64_TPREL16_HIGHERA,
1524 32, /* rightshift */
1525 1, /* size (0 = byte, 1 = short, 2 = long) */
1526 16, /* bitsize */
1527 FALSE, /* pc_relative */
1528 0, /* bitpos */
1529 complain_overflow_dont, /* complain_on_overflow */
1530 ppc64_elf_unhandled_reloc, /* special_function */
1531 "R_PPC64_TPREL16_HIGHERA", /* name */
1532 FALSE, /* partial_inplace */
1533 0, /* src_mask */
1534 0xffff, /* dst_mask */
1535 FALSE), /* pcrel_offset */
1536
1537 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1538 HOWTO (R_PPC64_TPREL16_HIGHEST,
1539 48, /* rightshift */
1540 1, /* size (0 = byte, 1 = short, 2 = long) */
1541 16, /* bitsize */
1542 FALSE, /* pc_relative */
1543 0, /* bitpos */
1544 complain_overflow_dont, /* complain_on_overflow */
1545 ppc64_elf_unhandled_reloc, /* special_function */
1546 "R_PPC64_TPREL16_HIGHEST", /* name */
1547 FALSE, /* partial_inplace */
1548 0, /* src_mask */
1549 0xffff, /* dst_mask */
1550 FALSE), /* pcrel_offset */
1551
1552 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1553 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1554 48, /* rightshift */
1555 1, /* size (0 = byte, 1 = short, 2 = long) */
1556 16, /* bitsize */
1557 FALSE, /* pc_relative */
1558 0, /* bitpos */
1559 complain_overflow_dont, /* complain_on_overflow */
1560 ppc64_elf_unhandled_reloc, /* special_function */
1561 "R_PPC64_TPREL16_HIGHESTA", /* name */
1562 FALSE, /* partial_inplace */
1563 0, /* src_mask */
1564 0xffff, /* dst_mask */
1565 FALSE), /* pcrel_offset */
1566
1567 /* Like TPREL16, but for insns with a DS field. */
1568 HOWTO (R_PPC64_TPREL16_DS,
1569 0, /* rightshift */
1570 1, /* size (0 = byte, 1 = short, 2 = long) */
1571 16, /* bitsize */
1572 FALSE, /* pc_relative */
1573 0, /* bitpos */
1574 complain_overflow_signed, /* complain_on_overflow */
1575 ppc64_elf_unhandled_reloc, /* special_function */
1576 "R_PPC64_TPREL16_DS", /* name */
1577 FALSE, /* partial_inplace */
1578 0, /* src_mask */
1579 0xfffc, /* dst_mask */
1580 FALSE), /* pcrel_offset */
1581
1582 /* Like TPREL16_DS, but no overflow. */
1583 HOWTO (R_PPC64_TPREL16_LO_DS,
1584 0, /* rightshift */
1585 1, /* size (0 = byte, 1 = short, 2 = long) */
1586 16, /* bitsize */
1587 FALSE, /* pc_relative */
1588 0, /* bitpos */
1589 complain_overflow_dont, /* complain_on_overflow */
1590 ppc64_elf_unhandled_reloc, /* special_function */
1591 "R_PPC64_TPREL16_LO_DS", /* name */
1592 FALSE, /* partial_inplace */
1593 0, /* src_mask */
1594 0xfffc, /* dst_mask */
1595 FALSE), /* pcrel_offset */
1596
1597 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1598 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1599 to the first entry relative to the TOC base (r2). */
1600 HOWTO (R_PPC64_GOT_TLSGD16,
1601 0, /* rightshift */
1602 1, /* size (0 = byte, 1 = short, 2 = long) */
1603 16, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_signed, /* complain_on_overflow */
1607 ppc64_elf_unhandled_reloc, /* special_function */
1608 "R_PPC64_GOT_TLSGD16", /* name */
1609 FALSE, /* partial_inplace */
1610 0, /* src_mask */
1611 0xffff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613
1614 /* Like GOT_TLSGD16, but no overflow. */
1615 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1616 0, /* rightshift */
1617 1, /* size (0 = byte, 1 = short, 2 = long) */
1618 16, /* bitsize */
1619 FALSE, /* pc_relative */
1620 0, /* bitpos */
1621 complain_overflow_dont, /* complain_on_overflow */
1622 ppc64_elf_unhandled_reloc, /* special_function */
1623 "R_PPC64_GOT_TLSGD16_LO", /* name */
1624 FALSE, /* partial_inplace */
1625 0, /* src_mask */
1626 0xffff, /* dst_mask */
1627 FALSE), /* pcrel_offset */
1628
1629 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1630 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1631 16, /* rightshift */
1632 1, /* size (0 = byte, 1 = short, 2 = long) */
1633 16, /* bitsize */
1634 FALSE, /* pc_relative */
1635 0, /* bitpos */
1636 complain_overflow_dont, /* complain_on_overflow */
1637 ppc64_elf_unhandled_reloc, /* special_function */
1638 "R_PPC64_GOT_TLSGD16_HI", /* name */
1639 FALSE, /* partial_inplace */
1640 0, /* src_mask */
1641 0xffff, /* dst_mask */
1642 FALSE), /* pcrel_offset */
1643
1644 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1645 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1646 16, /* rightshift */
1647 1, /* size (0 = byte, 1 = short, 2 = long) */
1648 16, /* bitsize */
1649 FALSE, /* pc_relative */
1650 0, /* bitpos */
1651 complain_overflow_dont, /* complain_on_overflow */
1652 ppc64_elf_unhandled_reloc, /* special_function */
1653 "R_PPC64_GOT_TLSGD16_HA", /* name */
1654 FALSE, /* partial_inplace */
1655 0, /* src_mask */
1656 0xffff, /* dst_mask */
1657 FALSE), /* pcrel_offset */
1658
1659 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1660 with values (sym+add)@dtpmod and zero, and computes the offset to the
1661 first entry relative to the TOC base (r2). */
1662 HOWTO (R_PPC64_GOT_TLSLD16,
1663 0, /* rightshift */
1664 1, /* size (0 = byte, 1 = short, 2 = long) */
1665 16, /* bitsize */
1666 FALSE, /* pc_relative */
1667 0, /* bitpos */
1668 complain_overflow_signed, /* complain_on_overflow */
1669 ppc64_elf_unhandled_reloc, /* special_function */
1670 "R_PPC64_GOT_TLSLD16", /* name */
1671 FALSE, /* partial_inplace */
1672 0, /* src_mask */
1673 0xffff, /* dst_mask */
1674 FALSE), /* pcrel_offset */
1675
1676 /* Like GOT_TLSLD16, but no overflow. */
1677 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1678 0, /* rightshift */
1679 1, /* size (0 = byte, 1 = short, 2 = long) */
1680 16, /* bitsize */
1681 FALSE, /* pc_relative */
1682 0, /* bitpos */
1683 complain_overflow_dont, /* complain_on_overflow */
1684 ppc64_elf_unhandled_reloc, /* special_function */
1685 "R_PPC64_GOT_TLSLD16_LO", /* name */
1686 FALSE, /* partial_inplace */
1687 0, /* src_mask */
1688 0xffff, /* dst_mask */
1689 FALSE), /* pcrel_offset */
1690
1691 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1692 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1693 16, /* rightshift */
1694 1, /* size (0 = byte, 1 = short, 2 = long) */
1695 16, /* bitsize */
1696 FALSE, /* pc_relative */
1697 0, /* bitpos */
1698 complain_overflow_dont, /* complain_on_overflow */
1699 ppc64_elf_unhandled_reloc, /* special_function */
1700 "R_PPC64_GOT_TLSLD16_HI", /* name */
1701 FALSE, /* partial_inplace */
1702 0, /* src_mask */
1703 0xffff, /* dst_mask */
1704 FALSE), /* pcrel_offset */
1705
1706 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1707 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1708 16, /* rightshift */
1709 1, /* size (0 = byte, 1 = short, 2 = long) */
1710 16, /* bitsize */
1711 FALSE, /* pc_relative */
1712 0, /* bitpos */
1713 complain_overflow_dont, /* complain_on_overflow */
1714 ppc64_elf_unhandled_reloc, /* special_function */
1715 "R_PPC64_GOT_TLSLD16_HA", /* name */
1716 FALSE, /* partial_inplace */
1717 0, /* src_mask */
1718 0xffff, /* dst_mask */
1719 FALSE), /* pcrel_offset */
1720
1721 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1722 the offset to the entry relative to the TOC base (r2). */
1723 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1724 0, /* rightshift */
1725 1, /* size (0 = byte, 1 = short, 2 = long) */
1726 16, /* bitsize */
1727 FALSE, /* pc_relative */
1728 0, /* bitpos */
1729 complain_overflow_signed, /* complain_on_overflow */
1730 ppc64_elf_unhandled_reloc, /* special_function */
1731 "R_PPC64_GOT_DTPREL16_DS", /* name */
1732 FALSE, /* partial_inplace */
1733 0, /* src_mask */
1734 0xfffc, /* dst_mask */
1735 FALSE), /* pcrel_offset */
1736
1737 /* Like GOT_DTPREL16_DS, but no overflow. */
1738 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1739 0, /* rightshift */
1740 1, /* size (0 = byte, 1 = short, 2 = long) */
1741 16, /* bitsize */
1742 FALSE, /* pc_relative */
1743 0, /* bitpos */
1744 complain_overflow_dont, /* complain_on_overflow */
1745 ppc64_elf_unhandled_reloc, /* special_function */
1746 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1747 FALSE, /* partial_inplace */
1748 0, /* src_mask */
1749 0xfffc, /* dst_mask */
1750 FALSE), /* pcrel_offset */
1751
1752 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1753 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1754 16, /* rightshift */
1755 1, /* size (0 = byte, 1 = short, 2 = long) */
1756 16, /* bitsize */
1757 FALSE, /* pc_relative */
1758 0, /* bitpos */
1759 complain_overflow_dont, /* complain_on_overflow */
1760 ppc64_elf_unhandled_reloc, /* special_function */
1761 "R_PPC64_GOT_DTPREL16_HI", /* name */
1762 FALSE, /* partial_inplace */
1763 0, /* src_mask */
1764 0xffff, /* dst_mask */
1765 FALSE), /* pcrel_offset */
1766
1767 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1768 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1769 16, /* rightshift */
1770 1, /* size (0 = byte, 1 = short, 2 = long) */
1771 16, /* bitsize */
1772 FALSE, /* pc_relative */
1773 0, /* bitpos */
1774 complain_overflow_dont, /* complain_on_overflow */
1775 ppc64_elf_unhandled_reloc, /* special_function */
1776 "R_PPC64_GOT_DTPREL16_HA", /* name */
1777 FALSE, /* partial_inplace */
1778 0, /* src_mask */
1779 0xffff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1781
1782 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1783 offset to the entry relative to the TOC base (r2). */
1784 HOWTO (R_PPC64_GOT_TPREL16_DS,
1785 0, /* rightshift */
1786 1, /* size (0 = byte, 1 = short, 2 = long) */
1787 16, /* bitsize */
1788 FALSE, /* pc_relative */
1789 0, /* bitpos */
1790 complain_overflow_signed, /* complain_on_overflow */
1791 ppc64_elf_unhandled_reloc, /* special_function */
1792 "R_PPC64_GOT_TPREL16_DS", /* name */
1793 FALSE, /* partial_inplace */
1794 0, /* src_mask */
1795 0xfffc, /* dst_mask */
1796 FALSE), /* pcrel_offset */
1797
1798 /* Like GOT_TPREL16_DS, but no overflow. */
1799 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1800 0, /* rightshift */
1801 1, /* size (0 = byte, 1 = short, 2 = long) */
1802 16, /* bitsize */
1803 FALSE, /* pc_relative */
1804 0, /* bitpos */
1805 complain_overflow_dont, /* complain_on_overflow */
1806 ppc64_elf_unhandled_reloc, /* special_function */
1807 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1808 FALSE, /* partial_inplace */
1809 0, /* src_mask */
1810 0xfffc, /* dst_mask */
1811 FALSE), /* pcrel_offset */
1812
1813 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1814 HOWTO (R_PPC64_GOT_TPREL16_HI,
1815 16, /* rightshift */
1816 1, /* size (0 = byte, 1 = short, 2 = long) */
1817 16, /* bitsize */
1818 FALSE, /* pc_relative */
1819 0, /* bitpos */
1820 complain_overflow_dont, /* complain_on_overflow */
1821 ppc64_elf_unhandled_reloc, /* special_function */
1822 "R_PPC64_GOT_TPREL16_HI", /* name */
1823 FALSE, /* partial_inplace */
1824 0, /* src_mask */
1825 0xffff, /* dst_mask */
1826 FALSE), /* pcrel_offset */
1827
1828 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1829 HOWTO (R_PPC64_GOT_TPREL16_HA,
1830 16, /* rightshift */
1831 1, /* size (0 = byte, 1 = short, 2 = long) */
1832 16, /* bitsize */
1833 FALSE, /* pc_relative */
1834 0, /* bitpos */
1835 complain_overflow_dont, /* complain_on_overflow */
1836 ppc64_elf_unhandled_reloc, /* special_function */
1837 "R_PPC64_GOT_TPREL16_HA", /* name */
1838 FALSE, /* partial_inplace */
1839 0, /* src_mask */
1840 0xffff, /* dst_mask */
1841 FALSE), /* pcrel_offset */
1842
1843 /* GNU extension to record C++ vtable hierarchy. */
1844 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1845 0, /* rightshift */
1846 0, /* size (0 = byte, 1 = short, 2 = long) */
1847 0, /* bitsize */
1848 FALSE, /* pc_relative */
1849 0, /* bitpos */
1850 complain_overflow_dont, /* complain_on_overflow */
1851 NULL, /* special_function */
1852 "R_PPC64_GNU_VTINHERIT", /* name */
1853 FALSE, /* partial_inplace */
1854 0, /* src_mask */
1855 0, /* dst_mask */
1856 FALSE), /* pcrel_offset */
1857
1858 /* GNU extension to record C++ vtable member usage. */
1859 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1860 0, /* rightshift */
1861 0, /* size (0 = byte, 1 = short, 2 = long) */
1862 0, /* bitsize */
1863 FALSE, /* pc_relative */
1864 0, /* bitpos */
1865 complain_overflow_dont, /* complain_on_overflow */
1866 NULL, /* special_function */
1867 "R_PPC64_GNU_VTENTRY", /* name */
1868 FALSE, /* partial_inplace */
1869 0, /* src_mask */
1870 0, /* dst_mask */
1871 FALSE), /* pcrel_offset */
1872 };
1873
1874 \f
1875 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1876 be done. */
1877
1878 static void
1879 ppc_howto_init (void)
1880 {
1881 unsigned int i, type;
1882
1883 for (i = 0;
1884 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1885 i++)
1886 {
1887 type = ppc64_elf_howto_raw[i].type;
1888 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1889 / sizeof (ppc64_elf_howto_table[0])));
1890 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1891 }
1892 }
1893
1894 static reloc_howto_type *
1895 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1896 bfd_reloc_code_real_type code)
1897 {
1898 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1899
1900 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1901 /* Initialize howto table if needed. */
1902 ppc_howto_init ();
1903
1904 switch (code)
1905 {
1906 default:
1907 return NULL;
1908
1909 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1910 break;
1911 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1912 break;
1913 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1914 break;
1915 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1916 break;
1917 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1918 break;
1919 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1920 break;
1921 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1922 break;
1923 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1924 break;
1925 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1926 break;
1927 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1928 break;
1929 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1930 break;
1931 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1932 break;
1933 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1934 break;
1935 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1936 break;
1937 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1938 break;
1939 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1940 break;
1941 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1942 break;
1943 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1944 break;
1945 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1946 break;
1947 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1948 break;
1949 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1950 break;
1951 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1952 break;
1953 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1954 break;
1955 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1956 break;
1957 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1958 break;
1959 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1960 break;
1961 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1962 break;
1963 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1964 break;
1965 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1966 break;
1967 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1968 break;
1969 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1970 break;
1971 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1972 break;
1973 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1974 break;
1975 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1976 break;
1977 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1978 break;
1979 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1980 break;
1981 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1982 break;
1983 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1984 break;
1985 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1986 break;
1987 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1988 break;
1989 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1990 break;
1991 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1992 break;
1993 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1994 break;
1995 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1996 break;
1997 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1998 break;
1999 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2000 break;
2001 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2002 break;
2003 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2004 break;
2005 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2006 break;
2007 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2008 break;
2009 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2010 break;
2011 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2012 break;
2013 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2014 break;
2015 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2016 break;
2017 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2018 break;
2019 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2020 break;
2021 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2022 break;
2023 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2024 break;
2025 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2026 break;
2027 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2028 break;
2029 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2030 break;
2031 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2032 break;
2033 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2034 break;
2035 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2036 break;
2037 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2038 break;
2039 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2040 break;
2041 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2042 break;
2043 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2044 break;
2045 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2046 break;
2047 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2048 break;
2049 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2050 break;
2051 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2052 break;
2053 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2054 break;
2055 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2056 break;
2057 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2058 break;
2059 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2060 break;
2061 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2062 break;
2063 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2064 break;
2065 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2066 break;
2067 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2068 break;
2069 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2070 break;
2071 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2072 break;
2073 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2074 break;
2075 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2076 break;
2077 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2078 break;
2079 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2080 break;
2081 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2082 break;
2083 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2084 break;
2085 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2086 break;
2087 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2088 break;
2089 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2090 break;
2091 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2092 break;
2093 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2094 break;
2095 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2096 break;
2097 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2098 break;
2099 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2100 break;
2101 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2102 break;
2103 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2104 break;
2105 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2106 break;
2107 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2108 break;
2109 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2110 break;
2111 }
2112
2113 return ppc64_elf_howto_table[r];
2114 };
2115
2116 static reloc_howto_type *
2117 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2118 const char *r_name)
2119 {
2120 unsigned int i;
2121
2122 for (i = 0;
2123 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2124 i++)
2125 if (ppc64_elf_howto_raw[i].name != NULL
2126 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2127 return &ppc64_elf_howto_raw[i];
2128
2129 return NULL;
2130 }
2131
2132 /* Set the howto pointer for a PowerPC ELF reloc. */
2133
2134 static void
2135 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2136 Elf_Internal_Rela *dst)
2137 {
2138 unsigned int type;
2139
2140 /* Initialize howto table if needed. */
2141 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2142 ppc_howto_init ();
2143
2144 type = ELF64_R_TYPE (dst->r_info);
2145 if (type >= (sizeof (ppc64_elf_howto_table)
2146 / sizeof (ppc64_elf_howto_table[0])))
2147 {
2148 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2149 abfd, (int) type);
2150 type = R_PPC64_NONE;
2151 }
2152 cache_ptr->howto = ppc64_elf_howto_table[type];
2153 }
2154
2155 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2156
2157 static bfd_reloc_status_type
2158 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2159 void *data, asection *input_section,
2160 bfd *output_bfd, char **error_message)
2161 {
2162 /* If this is a relocatable link (output_bfd test tells us), just
2163 call the generic function. Any adjustment will be done at final
2164 link time. */
2165 if (output_bfd != NULL)
2166 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2167 input_section, output_bfd, error_message);
2168
2169 /* Adjust the addend for sign extension of the low 16 bits.
2170 We won't actually be using the low 16 bits, so trashing them
2171 doesn't matter. */
2172 reloc_entry->addend += 0x8000;
2173 return bfd_reloc_continue;
2174 }
2175
2176 static bfd_reloc_status_type
2177 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2178 void *data, asection *input_section,
2179 bfd *output_bfd, char **error_message)
2180 {
2181 if (output_bfd != NULL)
2182 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2183 input_section, output_bfd, error_message);
2184
2185 if (strcmp (symbol->section->name, ".opd") == 0
2186 && (symbol->section->owner->flags & DYNAMIC) == 0)
2187 {
2188 bfd_vma dest = opd_entry_value (symbol->section,
2189 symbol->value + reloc_entry->addend,
2190 NULL, NULL);
2191 if (dest != (bfd_vma) -1)
2192 reloc_entry->addend = dest - (symbol->value
2193 + symbol->section->output_section->vma
2194 + symbol->section->output_offset);
2195 }
2196 return bfd_reloc_continue;
2197 }
2198
2199 static bfd_reloc_status_type
2200 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2201 void *data, asection *input_section,
2202 bfd *output_bfd, char **error_message)
2203 {
2204 long insn;
2205 enum elf_ppc64_reloc_type r_type;
2206 bfd_size_type octets;
2207 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2208 bfd_boolean is_power4 = FALSE;
2209
2210 /* If this is a relocatable link (output_bfd test tells us), just
2211 call the generic function. Any adjustment will be done at final
2212 link time. */
2213 if (output_bfd != NULL)
2214 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2215 input_section, output_bfd, error_message);
2216
2217 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2218 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2219 insn &= ~(0x01 << 21);
2220 r_type = reloc_entry->howto->type;
2221 if (r_type == R_PPC64_ADDR14_BRTAKEN
2222 || r_type == R_PPC64_REL14_BRTAKEN)
2223 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2224
2225 if (is_power4)
2226 {
2227 /* Set 'a' bit. This is 0b00010 in BO field for branch
2228 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2229 for branch on CTR insns (BO == 1a00t or 1a01t). */
2230 if ((insn & (0x14 << 21)) == (0x04 << 21))
2231 insn |= 0x02 << 21;
2232 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2233 insn |= 0x08 << 21;
2234 else
2235 goto out;
2236 }
2237 else
2238 {
2239 bfd_vma target = 0;
2240 bfd_vma from;
2241
2242 if (!bfd_is_com_section (symbol->section))
2243 target = symbol->value;
2244 target += symbol->section->output_section->vma;
2245 target += symbol->section->output_offset;
2246 target += reloc_entry->addend;
2247
2248 from = (reloc_entry->address
2249 + input_section->output_offset
2250 + input_section->output_section->vma);
2251
2252 /* Invert 'y' bit if not the default. */
2253 if ((bfd_signed_vma) (target - from) < 0)
2254 insn ^= 0x01 << 21;
2255 }
2256 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2257 out:
2258 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2259 input_section, output_bfd, error_message);
2260 }
2261
2262 static bfd_reloc_status_type
2263 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2264 void *data, asection *input_section,
2265 bfd *output_bfd, char **error_message)
2266 {
2267 /* If this is a relocatable link (output_bfd test tells us), just
2268 call the generic function. Any adjustment will be done at final
2269 link time. */
2270 if (output_bfd != NULL)
2271 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2272 input_section, output_bfd, error_message);
2273
2274 /* Subtract the symbol section base address. */
2275 reloc_entry->addend -= symbol->section->output_section->vma;
2276 return bfd_reloc_continue;
2277 }
2278
2279 static bfd_reloc_status_type
2280 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2281 void *data, asection *input_section,
2282 bfd *output_bfd, char **error_message)
2283 {
2284 /* If this is a relocatable link (output_bfd test tells us), just
2285 call the generic function. Any adjustment will be done at final
2286 link time. */
2287 if (output_bfd != NULL)
2288 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2289 input_section, output_bfd, error_message);
2290
2291 /* Subtract the symbol section base address. */
2292 reloc_entry->addend -= symbol->section->output_section->vma;
2293
2294 /* Adjust the addend for sign extension of the low 16 bits. */
2295 reloc_entry->addend += 0x8000;
2296 return bfd_reloc_continue;
2297 }
2298
2299 static bfd_reloc_status_type
2300 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2301 void *data, asection *input_section,
2302 bfd *output_bfd, char **error_message)
2303 {
2304 bfd_vma TOCstart;
2305
2306 /* If this is a relocatable link (output_bfd test tells us), just
2307 call the generic function. Any adjustment will be done at final
2308 link time. */
2309 if (output_bfd != NULL)
2310 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2311 input_section, output_bfd, error_message);
2312
2313 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2314 if (TOCstart == 0)
2315 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2316
2317 /* Subtract the TOC base address. */
2318 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2319 return bfd_reloc_continue;
2320 }
2321
2322 static bfd_reloc_status_type
2323 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2324 void *data, asection *input_section,
2325 bfd *output_bfd, char **error_message)
2326 {
2327 bfd_vma TOCstart;
2328
2329 /* If this is a relocatable link (output_bfd test tells us), just
2330 call the generic function. Any adjustment will be done at final
2331 link time. */
2332 if (output_bfd != NULL)
2333 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2334 input_section, output_bfd, error_message);
2335
2336 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2337 if (TOCstart == 0)
2338 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2339
2340 /* Subtract the TOC base address. */
2341 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2342
2343 /* Adjust the addend for sign extension of the low 16 bits. */
2344 reloc_entry->addend += 0x8000;
2345 return bfd_reloc_continue;
2346 }
2347
2348 static bfd_reloc_status_type
2349 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2350 void *data, asection *input_section,
2351 bfd *output_bfd, char **error_message)
2352 {
2353 bfd_vma TOCstart;
2354 bfd_size_type octets;
2355
2356 /* If this is a relocatable link (output_bfd test tells us), just
2357 call the generic function. Any adjustment will be done at final
2358 link time. */
2359 if (output_bfd != NULL)
2360 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2361 input_section, output_bfd, error_message);
2362
2363 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2364 if (TOCstart == 0)
2365 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2366
2367 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2368 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2369 return bfd_reloc_ok;
2370 }
2371
2372 static bfd_reloc_status_type
2373 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2374 void *data, asection *input_section,
2375 bfd *output_bfd, char **error_message)
2376 {
2377 /* If this is a relocatable link (output_bfd test tells us), just
2378 call the generic function. Any adjustment will be done at final
2379 link time. */
2380 if (output_bfd != NULL)
2381 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2382 input_section, output_bfd, error_message);
2383
2384 if (error_message != NULL)
2385 {
2386 static char buf[60];
2387 sprintf (buf, "generic linker can't handle %s",
2388 reloc_entry->howto->name);
2389 *error_message = buf;
2390 }
2391 return bfd_reloc_dangerous;
2392 }
2393
2394 struct ppc64_elf_obj_tdata
2395 {
2396 struct elf_obj_tdata elf;
2397
2398 /* Shortcuts to dynamic linker sections. */
2399 asection *got;
2400 asection *relgot;
2401
2402 /* Used during garbage collection. We attach global symbols defined
2403 on removed .opd entries to this section so that the sym is removed. */
2404 asection *deleted_section;
2405
2406 /* TLS local dynamic got entry handling. Suppose for multiple GOT
2407 sections means we potentially need one of these for each input bfd. */
2408 union {
2409 bfd_signed_vma refcount;
2410 bfd_vma offset;
2411 } tlsld_got;
2412
2413 /* A copy of relocs before they are modified for --emit-relocs. */
2414 Elf_Internal_Rela *opd_relocs;
2415 };
2416
2417 #define ppc64_elf_tdata(bfd) \
2418 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2419
2420 #define ppc64_tlsld_got(bfd) \
2421 (&ppc64_elf_tdata (bfd)->tlsld_got)
2422
2423 /* Override the generic function because we store some extras. */
2424
2425 static bfd_boolean
2426 ppc64_elf_mkobject (bfd *abfd)
2427 {
2428 if (abfd->tdata.any == NULL)
2429 {
2430 bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2431 abfd->tdata.any = bfd_zalloc (abfd, amt);
2432 if (abfd->tdata.any == NULL)
2433 return FALSE;
2434 }
2435 return bfd_elf_mkobject (abfd);
2436 }
2437
2438 /* Return 1 if target is one of ours. */
2439
2440 static bfd_boolean
2441 is_ppc64_elf_target (const struct bfd_target *targ)
2442 {
2443 extern const bfd_target bfd_elf64_powerpc_vec;
2444 extern const bfd_target bfd_elf64_powerpcle_vec;
2445
2446 return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
2447 }
2448
2449 /* Fix bad default arch selected for a 64 bit input bfd when the
2450 default is 32 bit. */
2451
2452 static bfd_boolean
2453 ppc64_elf_object_p (bfd *abfd)
2454 {
2455 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2456 {
2457 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2458
2459 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2460 {
2461 /* Relies on arch after 32 bit default being 64 bit default. */
2462 abfd->arch_info = abfd->arch_info->next;
2463 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2464 }
2465 }
2466 return TRUE;
2467 }
2468
2469 /* Support for core dump NOTE sections. */
2470
2471 static bfd_boolean
2472 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2473 {
2474 size_t offset, size;
2475
2476 if (note->descsz != 504)
2477 return FALSE;
2478
2479 /* pr_cursig */
2480 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2481
2482 /* pr_pid */
2483 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2484
2485 /* pr_reg */
2486 offset = 112;
2487 size = 384;
2488
2489 /* Make a ".reg/999" section. */
2490 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2491 size, note->descpos + offset);
2492 }
2493
2494 static bfd_boolean
2495 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2496 {
2497 if (note->descsz != 136)
2498 return FALSE;
2499
2500 elf_tdata (abfd)->core_program
2501 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2502 elf_tdata (abfd)->core_command
2503 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2504
2505 return TRUE;
2506 }
2507
2508 static char *
2509 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2510 ...)
2511 {
2512 switch (note_type)
2513 {
2514 default:
2515 return NULL;
2516
2517 case NT_PRPSINFO:
2518 {
2519 char data[136];
2520 va_list ap;
2521
2522 va_start (ap, note_type);
2523 memset (data, 0, 40);
2524 strncpy (data + 40, va_arg (ap, const char *), 16);
2525 strncpy (data + 56, va_arg (ap, const char *), 80);
2526 va_end (ap);
2527 return elfcore_write_note (abfd, buf, bufsiz,
2528 "CORE", note_type, data, sizeof (data));
2529 }
2530
2531 case NT_PRSTATUS:
2532 {
2533 char data[504];
2534 va_list ap;
2535 long pid;
2536 int cursig;
2537 const void *greg;
2538
2539 va_start (ap, note_type);
2540 memset (data, 0, 112);
2541 pid = va_arg (ap, long);
2542 bfd_put_32 (abfd, pid, data + 32);
2543 cursig = va_arg (ap, int);
2544 bfd_put_16 (abfd, cursig, data + 12);
2545 greg = va_arg (ap, const void *);
2546 memcpy (data + 112, greg, 384);
2547 memset (data + 496, 0, 8);
2548 va_end (ap);
2549 return elfcore_write_note (abfd, buf, bufsiz,
2550 "CORE", note_type, data, sizeof (data));
2551 }
2552 }
2553 }
2554
2555 /* Merge backend specific data from an object file to the output
2556 object file when linking. */
2557
2558 static bfd_boolean
2559 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2560 {
2561 /* Check if we have the same endianess. */
2562 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2563 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2564 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2565 {
2566 const char *msg;
2567
2568 if (bfd_big_endian (ibfd))
2569 msg = _("%B: compiled for a big endian system "
2570 "and target is little endian");
2571 else
2572 msg = _("%B: compiled for a little endian system "
2573 "and target is big endian");
2574
2575 (*_bfd_error_handler) (msg, ibfd);
2576
2577 bfd_set_error (bfd_error_wrong_format);
2578 return FALSE;
2579 }
2580
2581 return TRUE;
2582 }
2583
2584 /* Add extra PPC sections. */
2585
2586 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2587 {
2588 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2589 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2590 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2591 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2592 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2593 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2594 { NULL, 0, 0, 0, 0 }
2595 };
2596
2597 enum _ppc64_sec_type {
2598 sec_normal = 0,
2599 sec_opd = 1,
2600 sec_toc = 2
2601 };
2602
2603 struct _ppc64_elf_section_data
2604 {
2605 struct bfd_elf_section_data elf;
2606
2607 /* An array with one entry for each opd function descriptor. */
2608 union
2609 {
2610 /* Points to the function code section for local opd entries. */
2611 asection **opd_func_sec;
2612 /* After editing .opd, adjust references to opd local syms. */
2613 long *opd_adjust;
2614
2615 /* An array for toc sections, indexed by offset/8.
2616 Specifies the relocation symbol index used at a given toc offset. */
2617 unsigned *t_symndx;
2618 } u;
2619
2620 enum _ppc64_sec_type sec_type:2;
2621
2622 /* Flag set when small branches are detected. Used to
2623 select suitable defaults for the stub group size. */
2624 unsigned int has_14bit_branch:1;
2625 };
2626
2627 #define ppc64_elf_section_data(sec) \
2628 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2629
2630 static bfd_boolean
2631 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2632 {
2633 if (!sec->used_by_bfd)
2634 {
2635 struct _ppc64_elf_section_data *sdata;
2636 bfd_size_type amt = sizeof (*sdata);
2637
2638 sdata = bfd_zalloc (abfd, amt);
2639 if (sdata == NULL)
2640 return FALSE;
2641 sec->used_by_bfd = sdata;
2642 }
2643
2644 return _bfd_elf_new_section_hook (abfd, sec);
2645 }
2646
2647 static void *
2648 get_opd_info (asection * sec)
2649 {
2650 if (sec != NULL
2651 && ppc64_elf_section_data (sec) != NULL
2652 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2653 return ppc64_elf_section_data (sec)->u.opd_adjust;
2654 return NULL;
2655 }
2656 \f
2657 /* Parameters for the qsort hook. */
2658 static asection *synthetic_opd;
2659 static bfd_boolean synthetic_relocatable;
2660
2661 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2662
2663 static int
2664 compare_symbols (const void *ap, const void *bp)
2665 {
2666 const asymbol *a = * (const asymbol **) ap;
2667 const asymbol *b = * (const asymbol **) bp;
2668
2669 /* Section symbols first. */
2670 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2671 return -1;
2672 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2673 return 1;
2674
2675 /* then .opd symbols. */
2676 if (a->section == synthetic_opd && b->section != synthetic_opd)
2677 return -1;
2678 if (a->section != synthetic_opd && b->section == synthetic_opd)
2679 return 1;
2680
2681 /* then other code symbols. */
2682 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2683 == (SEC_CODE | SEC_ALLOC)
2684 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2685 != (SEC_CODE | SEC_ALLOC))
2686 return -1;
2687
2688 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2689 != (SEC_CODE | SEC_ALLOC)
2690 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2691 == (SEC_CODE | SEC_ALLOC))
2692 return 1;
2693
2694 if (synthetic_relocatable)
2695 {
2696 if (a->section->id < b->section->id)
2697 return -1;
2698
2699 if (a->section->id > b->section->id)
2700 return 1;
2701 }
2702
2703 if (a->value + a->section->vma < b->value + b->section->vma)
2704 return -1;
2705
2706 if (a->value + a->section->vma > b->value + b->section->vma)
2707 return 1;
2708
2709 /* For syms with the same value, prefer strong dynamic global function
2710 syms over other syms. */
2711 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2712 return -1;
2713
2714 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2715 return 1;
2716
2717 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2718 return -1;
2719
2720 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2721 return 1;
2722
2723 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2724 return -1;
2725
2726 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2727 return 1;
2728
2729 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2730 return -1;
2731
2732 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2733 return 1;
2734
2735 return 0;
2736 }
2737
2738 /* Search SYMS for a symbol of the given VALUE. */
2739
2740 static asymbol *
2741 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2742 {
2743 long mid;
2744
2745 if (id == -1)
2746 {
2747 while (lo < hi)
2748 {
2749 mid = (lo + hi) >> 1;
2750 if (syms[mid]->value + syms[mid]->section->vma < value)
2751 lo = mid + 1;
2752 else if (syms[mid]->value + syms[mid]->section->vma > value)
2753 hi = mid;
2754 else
2755 return syms[mid];
2756 }
2757 }
2758 else
2759 {
2760 while (lo < hi)
2761 {
2762 mid = (lo + hi) >> 1;
2763 if (syms[mid]->section->id < id)
2764 lo = mid + 1;
2765 else if (syms[mid]->section->id > id)
2766 hi = mid;
2767 else if (syms[mid]->value < value)
2768 lo = mid + 1;
2769 else if (syms[mid]->value > value)
2770 hi = mid;
2771 else
2772 return syms[mid];
2773 }
2774 }
2775 return NULL;
2776 }
2777
2778 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2779 entry syms. */
2780
2781 static long
2782 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2783 long static_count, asymbol **static_syms,
2784 long dyn_count, asymbol **dyn_syms,
2785 asymbol **ret)
2786 {
2787 asymbol *s;
2788 long i;
2789 long count;
2790 char *names;
2791 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2792 asection *opd;
2793 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2794 asymbol **syms;
2795
2796 *ret = NULL;
2797
2798 opd = bfd_get_section_by_name (abfd, ".opd");
2799 if (opd == NULL)
2800 return 0;
2801
2802 symcount = static_count;
2803 if (!relocatable)
2804 symcount += dyn_count;
2805 if (symcount == 0)
2806 return 0;
2807
2808 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2809 if (syms == NULL)
2810 return -1;
2811
2812 if (!relocatable && static_count != 0 && dyn_count != 0)
2813 {
2814 /* Use both symbol tables. */
2815 memcpy (syms, static_syms, static_count * sizeof (*syms));
2816 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2817 }
2818 else if (!relocatable && static_count == 0)
2819 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2820 else
2821 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2822
2823 synthetic_opd = opd;
2824 synthetic_relocatable = relocatable;
2825 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2826
2827 if (!relocatable && symcount > 1)
2828 {
2829 long j;
2830 /* Trim duplicate syms, since we may have merged the normal and
2831 dynamic symbols. Actually, we only care about syms that have
2832 different values, so trim any with the same value. */
2833 for (i = 1, j = 1; i < symcount; ++i)
2834 if (syms[i - 1]->value + syms[i - 1]->section->vma
2835 != syms[i]->value + syms[i]->section->vma)
2836 syms[j++] = syms[i];
2837 symcount = j;
2838 }
2839
2840 i = 0;
2841 if (syms[i]->section == opd)
2842 ++i;
2843 codesecsym = i;
2844
2845 for (; i < symcount; ++i)
2846 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2847 != (SEC_CODE | SEC_ALLOC))
2848 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2849 break;
2850 codesecsymend = i;
2851
2852 for (; i < symcount; ++i)
2853 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2854 break;
2855 secsymend = i;
2856
2857 for (; i < symcount; ++i)
2858 if (syms[i]->section != opd)
2859 break;
2860 opdsymend = i;
2861
2862 for (; i < symcount; ++i)
2863 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2864 != (SEC_CODE | SEC_ALLOC))
2865 break;
2866 symcount = i;
2867
2868 count = 0;
2869 if (opdsymend == secsymend)
2870 goto done;
2871
2872 if (relocatable)
2873 {
2874 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2875 arelent *r;
2876 size_t size;
2877 long relcount;
2878
2879 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2880 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2881 if (relcount == 0)
2882 goto done;
2883
2884 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2885 {
2886 count = -1;
2887 goto done;
2888 }
2889
2890 size = 0;
2891 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2892 {
2893 asymbol *sym;
2894
2895 while (r < opd->relocation + relcount
2896 && r->address < syms[i]->value + opd->vma)
2897 ++r;
2898
2899 if (r == opd->relocation + relcount)
2900 break;
2901
2902 if (r->address != syms[i]->value + opd->vma)
2903 continue;
2904
2905 if (r->howto->type != R_PPC64_ADDR64)
2906 continue;
2907
2908 sym = *r->sym_ptr_ptr;
2909 if (!sym_exists_at (syms, opdsymend, symcount,
2910 sym->section->id, sym->value + r->addend))
2911 {
2912 ++count;
2913 size += sizeof (asymbol);
2914 size += strlen (syms[i]->name) + 2;
2915 }
2916 }
2917
2918 s = *ret = bfd_malloc (size);
2919 if (s == NULL)
2920 {
2921 count = -1;
2922 goto done;
2923 }
2924
2925 names = (char *) (s + count);
2926
2927 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2928 {
2929 asymbol *sym;
2930
2931 while (r < opd->relocation + relcount
2932 && r->address < syms[i]->value + opd->vma)
2933 ++r;
2934
2935 if (r == opd->relocation + relcount)
2936 break;
2937
2938 if (r->address != syms[i]->value + opd->vma)
2939 continue;
2940
2941 if (r->howto->type != R_PPC64_ADDR64)
2942 continue;
2943
2944 sym = *r->sym_ptr_ptr;
2945 if (!sym_exists_at (syms, opdsymend, symcount,
2946 sym->section->id, sym->value + r->addend))
2947 {
2948 size_t len;
2949
2950 *s = *syms[i];
2951 s->section = sym->section;
2952 s->value = sym->value + r->addend;
2953 s->name = names;
2954 *names++ = '.';
2955 len = strlen (syms[i]->name);
2956 memcpy (names, syms[i]->name, len + 1);
2957 names += len + 1;
2958 s++;
2959 }
2960 }
2961 }
2962 else
2963 {
2964 bfd_byte *contents;
2965 size_t size;
2966
2967 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2968 {
2969 if (contents)
2970 {
2971 free_contents_and_exit:
2972 free (contents);
2973 }
2974 count = -1;
2975 goto done;
2976 }
2977
2978 size = 0;
2979 for (i = secsymend; i < opdsymend; ++i)
2980 {
2981 bfd_vma ent;
2982
2983 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2984 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2985 {
2986 ++count;
2987 size += sizeof (asymbol);
2988 size += strlen (syms[i]->name) + 2;
2989 }
2990 }
2991
2992 s = *ret = bfd_malloc (size);
2993 if (s == NULL)
2994 goto free_contents_and_exit;
2995
2996 names = (char *) (s + count);
2997
2998 for (i = secsymend; i < opdsymend; ++i)
2999 {
3000 bfd_vma ent;
3001
3002 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3003 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3004 {
3005 long lo, hi;
3006 size_t len;
3007 asection *sec = abfd->sections;
3008
3009 *s = *syms[i];
3010 lo = codesecsym;
3011 hi = codesecsymend;
3012 while (lo < hi)
3013 {
3014 long mid = (lo + hi) >> 1;
3015 if (syms[mid]->section->vma < ent)
3016 lo = mid + 1;
3017 else if (syms[mid]->section->vma > ent)
3018 hi = mid;
3019 else
3020 {
3021 sec = syms[mid]->section;
3022 break;
3023 }
3024 }
3025
3026 if (lo >= hi && lo > codesecsym)
3027 sec = syms[lo - 1]->section;
3028
3029 for (; sec != NULL; sec = sec->next)
3030 {
3031 if (sec->vma > ent)
3032 break;
3033 if ((sec->flags & SEC_ALLOC) == 0
3034 || (sec->flags & SEC_LOAD) == 0)
3035 break;
3036 if ((sec->flags & SEC_CODE) != 0)
3037 s->section = sec;
3038 }
3039 s->value = ent - s->section->vma;
3040 s->name = names;
3041 *names++ = '.';
3042 len = strlen (syms[i]->name);
3043 memcpy (names, syms[i]->name, len + 1);
3044 names += len + 1;
3045 s++;
3046 }
3047 }
3048 free (contents);
3049 }
3050
3051 done:
3052 free (syms);
3053 return count;
3054 }
3055 \f
3056 /* The following functions are specific to the ELF linker, while
3057 functions above are used generally. Those named ppc64_elf_* are
3058 called by the main ELF linker code. They appear in this file more
3059 or less in the order in which they are called. eg.
3060 ppc64_elf_check_relocs is called early in the link process,
3061 ppc64_elf_finish_dynamic_sections is one of the last functions
3062 called.
3063
3064 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3065 functions have both a function code symbol and a function descriptor
3066 symbol. A call to foo in a relocatable object file looks like:
3067
3068 . .text
3069 . x:
3070 . bl .foo
3071 . nop
3072
3073 The function definition in another object file might be:
3074
3075 . .section .opd
3076 . foo: .quad .foo
3077 . .quad .TOC.@tocbase
3078 . .quad 0
3079 .
3080 . .text
3081 . .foo: blr
3082
3083 When the linker resolves the call during a static link, the branch
3084 unsurprisingly just goes to .foo and the .opd information is unused.
3085 If the function definition is in a shared library, things are a little
3086 different: The call goes via a plt call stub, the opd information gets
3087 copied to the plt, and the linker patches the nop.
3088
3089 . x:
3090 . bl .foo_stub
3091 . ld 2,40(1)
3092 .
3093 .
3094 . .foo_stub:
3095 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3096 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3097 . std 2,40(1) # this is the general idea
3098 . ld 11,0(12)
3099 . ld 2,8(12)
3100 . mtctr 11
3101 . ld 11,16(12)
3102 . bctr
3103 .
3104 . .section .plt
3105 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3106
3107 The "reloc ()" notation is supposed to indicate that the linker emits
3108 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3109 copying.
3110
3111 What are the difficulties here? Well, firstly, the relocations
3112 examined by the linker in check_relocs are against the function code
3113 sym .foo, while the dynamic relocation in the plt is emitted against
3114 the function descriptor symbol, foo. Somewhere along the line, we need
3115 to carefully copy dynamic link information from one symbol to the other.
3116 Secondly, the generic part of the elf linker will make .foo a dynamic
3117 symbol as is normal for most other backends. We need foo dynamic
3118 instead, at least for an application final link. However, when
3119 creating a shared library containing foo, we need to have both symbols
3120 dynamic so that references to .foo are satisfied during the early
3121 stages of linking. Otherwise the linker might decide to pull in a
3122 definition from some other object, eg. a static library.
3123
3124 Update: As of August 2004, we support a new convention. Function
3125 calls may use the function descriptor symbol, ie. "bl foo". This
3126 behaves exactly as "bl .foo". */
3127
3128 /* The linker needs to keep track of the number of relocs that it
3129 decides to copy as dynamic relocs in check_relocs for each symbol.
3130 This is so that it can later discard them if they are found to be
3131 unnecessary. We store the information in a field extending the
3132 regular ELF linker hash table. */
3133
3134 struct ppc_dyn_relocs
3135 {
3136 struct ppc_dyn_relocs *next;
3137
3138 /* The input section of the reloc. */
3139 asection *sec;
3140
3141 /* Total number of relocs copied for the input section. */
3142 bfd_size_type count;
3143
3144 /* Number of pc-relative relocs copied for the input section. */
3145 bfd_size_type pc_count;
3146 };
3147
3148 /* Track GOT entries needed for a given symbol. We might need more
3149 than one got entry per symbol. */
3150 struct got_entry
3151 {
3152 struct got_entry *next;
3153
3154 /* The symbol addend that we'll be placing in the GOT. */
3155 bfd_vma addend;
3156
3157 /* Unlike other ELF targets, we use separate GOT entries for the same
3158 symbol referenced from different input files. This is to support
3159 automatic multiple TOC/GOT sections, where the TOC base can vary
3160 from one input file to another.
3161
3162 Point to the BFD owning this GOT entry. */
3163 bfd *owner;
3164
3165 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3166 TLS_TPREL or TLS_DTPREL for tls entries. */
3167 char tls_type;
3168
3169 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
3170 union
3171 {
3172 bfd_signed_vma refcount;
3173 bfd_vma offset;
3174 } got;
3175 };
3176
3177 /* The same for PLT. */
3178 struct plt_entry
3179 {
3180 struct plt_entry *next;
3181
3182 bfd_vma addend;
3183
3184 union
3185 {
3186 bfd_signed_vma refcount;
3187 bfd_vma offset;
3188 } plt;
3189 };
3190
3191 /* Of those relocs that might be copied as dynamic relocs, this macro
3192 selects those that must be copied when linking a shared library,
3193 even when the symbol is local. */
3194
3195 #define MUST_BE_DYN_RELOC(RTYPE) \
3196 ((RTYPE) != R_PPC64_REL32 \
3197 && (RTYPE) != R_PPC64_REL64 \
3198 && (RTYPE) != R_PPC64_REL30)
3199
3200 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3201 copying dynamic variables from a shared lib into an app's dynbss
3202 section, and instead use a dynamic relocation to point into the
3203 shared lib. With code that gcc generates, it's vital that this be
3204 enabled; In the PowerPC64 ABI, the address of a function is actually
3205 the address of a function descriptor, which resides in the .opd
3206 section. gcc uses the descriptor directly rather than going via the
3207 GOT as some other ABI's do, which means that initialized function
3208 pointers must reference the descriptor. Thus, a function pointer
3209 initialized to the address of a function in a shared library will
3210 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3211 redefines the function descriptor symbol to point to the copy. This
3212 presents a problem as a plt entry for that function is also
3213 initialized from the function descriptor symbol and the copy reloc
3214 may not be initialized first. */
3215 #define ELIMINATE_COPY_RELOCS 1
3216
3217 /* Section name for stubs is the associated section name plus this
3218 string. */
3219 #define STUB_SUFFIX ".stub"
3220
3221 /* Linker stubs.
3222 ppc_stub_long_branch:
3223 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3224 destination, but a 24 bit branch in a stub section will reach.
3225 . b dest
3226
3227 ppc_stub_plt_branch:
3228 Similar to the above, but a 24 bit branch in the stub section won't
3229 reach its destination.
3230 . addis %r12,%r2,xxx@toc@ha
3231 . ld %r11,xxx@toc@l(%r12)
3232 . mtctr %r11
3233 . bctr
3234
3235 ppc_stub_plt_call:
3236 Used to call a function in a shared library. If it so happens that
3237 the plt entry referenced crosses a 64k boundary, then an extra
3238 "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3239 xxx+16 as appropriate.
3240 . addis %r12,%r2,xxx@toc@ha
3241 . std %r2,40(%r1)
3242 . ld %r11,xxx+0@toc@l(%r12)
3243 . ld %r2,xxx+8@toc@l(%r12)
3244 . mtctr %r11
3245 . ld %r11,xxx+16@toc@l(%r12)
3246 . bctr
3247
3248 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3249 code to adjust the value and save r2 to support multiple toc sections.
3250 A ppc_stub_long_branch with an r2 offset looks like:
3251 . std %r2,40(%r1)
3252 . addis %r2,%r2,off@ha
3253 . addi %r2,%r2,off@l
3254 . b dest
3255
3256 A ppc_stub_plt_branch with an r2 offset looks like:
3257 . std %r2,40(%r1)
3258 . addis %r12,%r2,xxx@toc@ha
3259 . ld %r11,xxx@toc@l(%r12)
3260 . addis %r2,%r2,off@ha
3261 . addi %r2,%r2,off@l
3262 . mtctr %r11
3263 . bctr
3264 */
3265
3266 enum ppc_stub_type {
3267 ppc_stub_none,
3268 ppc_stub_long_branch,
3269 ppc_stub_long_branch_r2off,
3270 ppc_stub_plt_branch,
3271 ppc_stub_plt_branch_r2off,
3272 ppc_stub_plt_call
3273 };
3274
3275 struct ppc_stub_hash_entry {
3276
3277 /* Base hash table entry structure. */
3278 struct bfd_hash_entry root;
3279
3280 enum ppc_stub_type stub_type;
3281
3282 /* The stub section. */
3283 asection *stub_sec;
3284
3285 /* Offset within stub_sec of the beginning of this stub. */
3286 bfd_vma stub_offset;
3287
3288 /* Given the symbol's value and its section we can determine its final
3289 value when building the stubs (so the stub knows where to jump. */
3290 bfd_vma target_value;
3291 asection *target_section;
3292
3293 /* The symbol table entry, if any, that this was derived from. */
3294 struct ppc_link_hash_entry *h;
3295
3296 /* And the reloc addend that this was derived from. */
3297 bfd_vma addend;
3298
3299 /* Where this stub is being called from, or, in the case of combined
3300 stub sections, the first input section in the group. */
3301 asection *id_sec;
3302 };
3303
3304 struct ppc_branch_hash_entry {
3305
3306 /* Base hash table entry structure. */
3307 struct bfd_hash_entry root;
3308
3309 /* Offset within branch lookup table. */
3310 unsigned int offset;
3311
3312 /* Generation marker. */
3313 unsigned int iter;
3314 };
3315
3316 struct ppc_link_hash_entry
3317 {
3318 struct elf_link_hash_entry elf;
3319
3320 union {
3321 /* A pointer to the most recently used stub hash entry against this
3322 symbol. */
3323 struct ppc_stub_hash_entry *stub_cache;
3324
3325 /* A pointer to the next symbol starting with a '.' */
3326 struct ppc_link_hash_entry *next_dot_sym;
3327 } u;
3328
3329 /* Track dynamic relocs copied for this symbol. */
3330 struct ppc_dyn_relocs *dyn_relocs;
3331
3332 /* Link between function code and descriptor symbols. */
3333 struct ppc_link_hash_entry *oh;
3334
3335 /* Flag function code and descriptor symbols. */
3336 unsigned int is_func:1;
3337 unsigned int is_func_descriptor:1;
3338 unsigned int fake:1;
3339
3340 /* Whether global opd/toc sym has been adjusted or not.
3341 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3342 should be set for all globals defined in any opd/toc section. */
3343 unsigned int adjust_done:1;
3344
3345 /* Set if we twiddled this symbol to weak at some stage. */
3346 unsigned int was_undefined:1;
3347
3348 /* Contexts in which symbol is used in the GOT (or TOC).
3349 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3350 corresponding relocs are encountered during check_relocs.
3351 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3352 indicate the corresponding GOT entry type is not needed.
3353 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3354 a TPREL one. We use a separate flag rather than setting TPREL
3355 just for convenience in distinguishing the two cases. */
3356 #define TLS_GD 1 /* GD reloc. */
3357 #define TLS_LD 2 /* LD reloc. */
3358 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3359 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3360 #define TLS_TLS 16 /* Any TLS reloc. */
3361 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3362 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3363 char tls_mask;
3364 };
3365
3366 /* ppc64 ELF linker hash table. */
3367
3368 struct ppc_link_hash_table
3369 {
3370 struct elf_link_hash_table elf;
3371
3372 /* The stub hash table. */
3373 struct bfd_hash_table stub_hash_table;
3374
3375 /* Another hash table for plt_branch stubs. */
3376 struct bfd_hash_table branch_hash_table;
3377
3378 /* Linker stub bfd. */
3379 bfd *stub_bfd;
3380
3381 /* Linker call-backs. */
3382 asection * (*add_stub_section) (const char *, asection *);
3383 void (*layout_sections_again) (void);
3384
3385 /* Array to keep track of which stub sections have been created, and
3386 information on stub grouping. */
3387 struct map_stub {
3388 /* This is the section to which stubs in the group will be attached. */
3389 asection *link_sec;
3390 /* The stub section. */
3391 asection *stub_sec;
3392 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3393 bfd_vma toc_off;
3394 } *stub_group;
3395
3396 /* Temp used when calculating TOC pointers. */
3397 bfd_vma toc_curr;
3398
3399 /* Highest input section id. */
3400 int top_id;
3401
3402 /* Highest output section index. */
3403 int top_index;
3404
3405 /* Used when adding symbols. */
3406 struct ppc_link_hash_entry *dot_syms;
3407
3408 /* List of input sections for each output section. */
3409 asection **input_list;
3410
3411 /* Short-cuts to get to dynamic linker sections. */
3412 asection *got;
3413 asection *plt;
3414 asection *relplt;
3415 asection *dynbss;
3416 asection *relbss;
3417 asection *glink;
3418 asection *sfpr;
3419 asection *brlt;
3420 asection *relbrlt;
3421
3422 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3423 struct ppc_link_hash_entry *tls_get_addr;
3424 struct ppc_link_hash_entry *tls_get_addr_fd;
3425
3426 /* Statistics. */
3427 unsigned long stub_count[ppc_stub_plt_call];
3428
3429 /* Number of stubs against global syms. */
3430 unsigned long stub_globals;
3431
3432 /* Set if we should emit symbols for stubs. */
3433 unsigned int emit_stub_syms:1;
3434
3435 /* Support for multiple toc sections. */
3436 unsigned int no_multi_toc:1;
3437 unsigned int multi_toc_needed:1;
3438
3439 /* Set on error. */
3440 unsigned int stub_error:1;
3441
3442 /* Temp used by ppc64_elf_check_directives. */
3443 unsigned int twiddled_syms:1;
3444
3445 /* Incremented every time we size stubs. */
3446 unsigned int stub_iteration;
3447
3448 /* Small local sym to section mapping cache. */
3449 struct sym_sec_cache sym_sec;
3450 };
3451
3452 /* Rename some of the generic section flags to better document how they
3453 are used here. */
3454 #define has_toc_reloc has_gp_reloc
3455 #define makes_toc_func_call need_finalize_relax
3456 #define call_check_in_progress reloc_done
3457
3458 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3459
3460 #define ppc_hash_table(p) \
3461 ((struct ppc_link_hash_table *) ((p)->hash))
3462
3463 #define ppc_stub_hash_lookup(table, string, create, copy) \
3464 ((struct ppc_stub_hash_entry *) \
3465 bfd_hash_lookup ((table), (string), (create), (copy)))
3466
3467 #define ppc_branch_hash_lookup(table, string, create, copy) \
3468 ((struct ppc_branch_hash_entry *) \
3469 bfd_hash_lookup ((table), (string), (create), (copy)))
3470
3471 /* Create an entry in the stub hash table. */
3472
3473 static struct bfd_hash_entry *
3474 stub_hash_newfunc (struct bfd_hash_entry *entry,
3475 struct bfd_hash_table *table,
3476 const char *string)
3477 {
3478 /* Allocate the structure if it has not already been allocated by a
3479 subclass. */
3480 if (entry == NULL)
3481 {
3482 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3483 if (entry == NULL)
3484 return entry;
3485 }
3486
3487 /* Call the allocation method of the superclass. */
3488 entry = bfd_hash_newfunc (entry, table, string);
3489 if (entry != NULL)
3490 {
3491 struct ppc_stub_hash_entry *eh;
3492
3493 /* Initialize the local fields. */
3494 eh = (struct ppc_stub_hash_entry *) entry;
3495 eh->stub_type = ppc_stub_none;
3496 eh->stub_sec = NULL;
3497 eh->stub_offset = 0;
3498 eh->target_value = 0;
3499 eh->target_section = NULL;
3500 eh->h = NULL;
3501 eh->id_sec = NULL;
3502 }
3503
3504 return entry;
3505 }
3506
3507 /* Create an entry in the branch hash table. */
3508
3509 static struct bfd_hash_entry *
3510 branch_hash_newfunc (struct bfd_hash_entry *entry,
3511 struct bfd_hash_table *table,
3512 const char *string)
3513 {
3514 /* Allocate the structure if it has not already been allocated by a
3515 subclass. */
3516 if (entry == NULL)
3517 {
3518 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3519 if (entry == NULL)
3520 return entry;
3521 }
3522
3523 /* Call the allocation method of the superclass. */
3524 entry = bfd_hash_newfunc (entry, table, string);
3525 if (entry != NULL)
3526 {
3527 struct ppc_branch_hash_entry *eh;
3528
3529 /* Initialize the local fields. */
3530 eh = (struct ppc_branch_hash_entry *) entry;
3531 eh->offset = 0;
3532 eh->iter = 0;
3533 }
3534
3535 return entry;
3536 }
3537
3538 /* Create an entry in a ppc64 ELF linker hash table. */
3539
3540 static struct bfd_hash_entry *
3541 link_hash_newfunc (struct bfd_hash_entry *entry,
3542 struct bfd_hash_table *table,
3543 const char *string)
3544 {
3545 /* Allocate the structure if it has not already been allocated by a
3546 subclass. */
3547 if (entry == NULL)
3548 {
3549 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3550 if (entry == NULL)
3551 return entry;
3552 }
3553
3554 /* Call the allocation method of the superclass. */
3555 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3556 if (entry != NULL)
3557 {
3558 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3559
3560 memset (&eh->u.stub_cache, 0,
3561 (sizeof (struct ppc_link_hash_entry)
3562 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3563
3564 /* When making function calls, old ABI code references function entry
3565 points (dot symbols), while new ABI code references the function
3566 descriptor symbol. We need to make any combination of reference and
3567 definition work together, without breaking archive linking.
3568
3569 For a defined function "foo" and an undefined call to "bar":
3570 An old object defines "foo" and ".foo", references ".bar" (possibly
3571 "bar" too).
3572 A new object defines "foo" and references "bar".
3573
3574 A new object thus has no problem with its undefined symbols being
3575 satisfied by definitions in an old object. On the other hand, the
3576 old object won't have ".bar" satisfied by a new object.
3577
3578 Keep a list of newly added dot-symbols. */
3579
3580 if (string[0] == '.')
3581 {
3582 struct ppc_link_hash_table *htab;
3583
3584 htab = (struct ppc_link_hash_table *) table;
3585 eh->u.next_dot_sym = htab->dot_syms;
3586 htab->dot_syms = eh;
3587 }
3588 }
3589
3590 return entry;
3591 }
3592
3593 /* Create a ppc64 ELF linker hash table. */
3594
3595 static struct bfd_link_hash_table *
3596 ppc64_elf_link_hash_table_create (bfd *abfd)
3597 {
3598 struct ppc_link_hash_table *htab;
3599 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3600
3601 htab = bfd_zmalloc (amt);
3602 if (htab == NULL)
3603 return NULL;
3604
3605 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3606 sizeof (struct ppc_link_hash_entry)))
3607 {
3608 free (htab);
3609 return NULL;
3610 }
3611
3612 /* Init the stub hash table too. */
3613 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3614 sizeof (struct ppc_stub_hash_entry)))
3615 return NULL;
3616
3617 /* And the branch hash table. */
3618 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3619 sizeof (struct ppc_branch_hash_entry)))
3620 return NULL;
3621
3622 /* Initializing two fields of the union is just cosmetic. We really
3623 only care about glist, but when compiled on a 32-bit host the
3624 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3625 debugger inspection of these fields look nicer. */
3626 htab->elf.init_got_refcount.refcount = 0;
3627 htab->elf.init_got_refcount.glist = NULL;
3628 htab->elf.init_plt_refcount.refcount = 0;
3629 htab->elf.init_plt_refcount.glist = NULL;
3630 htab->elf.init_got_offset.offset = 0;
3631 htab->elf.init_got_offset.glist = NULL;
3632 htab->elf.init_plt_offset.offset = 0;
3633 htab->elf.init_plt_offset.glist = NULL;
3634
3635 return &htab->elf.root;
3636 }
3637
3638 /* Free the derived linker hash table. */
3639
3640 static void
3641 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3642 {
3643 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3644
3645 bfd_hash_table_free (&ret->stub_hash_table);
3646 bfd_hash_table_free (&ret->branch_hash_table);
3647 _bfd_generic_link_hash_table_free (hash);
3648 }
3649
3650 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3651
3652 void
3653 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3654 {
3655 struct ppc_link_hash_table *htab;
3656
3657 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3658
3659 /* Always hook our dynamic sections into the first bfd, which is the
3660 linker created stub bfd. This ensures that the GOT header is at
3661 the start of the output TOC section. */
3662 htab = ppc_hash_table (info);
3663 htab->stub_bfd = abfd;
3664 htab->elf.dynobj = abfd;
3665 }
3666
3667 /* Build a name for an entry in the stub hash table. */
3668
3669 static char *
3670 ppc_stub_name (const asection *input_section,
3671 const asection *sym_sec,
3672 const struct ppc_link_hash_entry *h,
3673 const Elf_Internal_Rela *rel)
3674 {
3675 char *stub_name;
3676 bfd_size_type len;
3677
3678 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3679 offsets from a sym as a branch target? In fact, we could
3680 probably assume the addend is always zero. */
3681 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3682
3683 if (h)
3684 {
3685 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3686 stub_name = bfd_malloc (len);
3687 if (stub_name == NULL)
3688 return stub_name;
3689
3690 sprintf (stub_name, "%08x.%s+%x",
3691 input_section->id & 0xffffffff,
3692 h->elf.root.root.string,
3693 (int) rel->r_addend & 0xffffffff);
3694 }
3695 else
3696 {
3697 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3698 stub_name = bfd_malloc (len);
3699 if (stub_name == NULL)
3700 return stub_name;
3701
3702 sprintf (stub_name, "%08x.%x:%x+%x",
3703 input_section->id & 0xffffffff,
3704 sym_sec->id & 0xffffffff,
3705 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3706 (int) rel->r_addend & 0xffffffff);
3707 }
3708 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3709 stub_name[len - 2] = 0;
3710 return stub_name;
3711 }
3712
3713 /* Look up an entry in the stub hash. Stub entries are cached because
3714 creating the stub name takes a bit of time. */
3715
3716 static struct ppc_stub_hash_entry *
3717 ppc_get_stub_entry (const asection *input_section,
3718 const asection *sym_sec,
3719 struct ppc_link_hash_entry *h,
3720 const Elf_Internal_Rela *rel,
3721 struct ppc_link_hash_table *htab)
3722 {
3723 struct ppc_stub_hash_entry *stub_entry;
3724 const asection *id_sec;
3725
3726 /* If this input section is part of a group of sections sharing one
3727 stub section, then use the id of the first section in the group.
3728 Stub names need to include a section id, as there may well be
3729 more than one stub used to reach say, printf, and we need to
3730 distinguish between them. */
3731 id_sec = htab->stub_group[input_section->id].link_sec;
3732
3733 if (h != NULL && h->u.stub_cache != NULL
3734 && h->u.stub_cache->h == h
3735 && h->u.stub_cache->id_sec == id_sec)
3736 {
3737 stub_entry = h->u.stub_cache;
3738 }
3739 else
3740 {
3741 char *stub_name;
3742
3743 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3744 if (stub_name == NULL)
3745 return NULL;
3746
3747 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3748 stub_name, FALSE, FALSE);
3749 if (h != NULL)
3750 h->u.stub_cache = stub_entry;
3751
3752 free (stub_name);
3753 }
3754
3755 return stub_entry;
3756 }
3757
3758 /* Add a new stub entry to the stub hash. Not all fields of the new
3759 stub entry are initialised. */
3760
3761 static struct ppc_stub_hash_entry *
3762 ppc_add_stub (const char *stub_name,
3763 asection *section,
3764 struct ppc_link_hash_table *htab)
3765 {
3766 asection *link_sec;
3767 asection *stub_sec;
3768 struct ppc_stub_hash_entry *stub_entry;
3769
3770 link_sec = htab->stub_group[section->id].link_sec;
3771 stub_sec = htab->stub_group[section->id].stub_sec;
3772 if (stub_sec == NULL)
3773 {
3774 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3775 if (stub_sec == NULL)
3776 {
3777 size_t namelen;
3778 bfd_size_type len;
3779 char *s_name;
3780
3781 namelen = strlen (link_sec->name);
3782 len = namelen + sizeof (STUB_SUFFIX);
3783 s_name = bfd_alloc (htab->stub_bfd, len);
3784 if (s_name == NULL)
3785 return NULL;
3786
3787 memcpy (s_name, link_sec->name, namelen);
3788 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3789 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3790 if (stub_sec == NULL)
3791 return NULL;
3792 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3793 }
3794 htab->stub_group[section->id].stub_sec = stub_sec;
3795 }
3796
3797 /* Enter this entry into the linker stub hash table. */
3798 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3799 TRUE, FALSE);
3800 if (stub_entry == NULL)
3801 {
3802 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3803 section->owner, stub_name);
3804 return NULL;
3805 }
3806
3807 stub_entry->stub_sec = stub_sec;
3808 stub_entry->stub_offset = 0;
3809 stub_entry->id_sec = link_sec;
3810 return stub_entry;
3811 }
3812
3813 /* Create sections for linker generated code. */
3814
3815 static bfd_boolean
3816 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3817 {
3818 struct ppc_link_hash_table *htab;
3819 flagword flags;
3820
3821 htab = ppc_hash_table (info);
3822
3823 /* Create .sfpr for code to save and restore fp regs. */
3824 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3825 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3826 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3827 flags);
3828 if (htab->sfpr == NULL
3829 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3830 return FALSE;
3831
3832 /* Create .glink for lazy dynamic linking support. */
3833 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3834 flags);
3835 if (htab->glink == NULL
3836 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
3837 return FALSE;
3838
3839 /* Create branch lookup table for plt_branch stubs. */
3840 flags = (SEC_ALLOC | SEC_LOAD
3841 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3842 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3843 flags);
3844 if (htab->brlt == NULL
3845 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3846 return FALSE;
3847
3848 if (!info->shared)
3849 return TRUE;
3850
3851 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3852 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3853 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
3854 ".rela.branch_lt",
3855 flags);
3856 if (!htab->relbrlt
3857 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3858 return FALSE;
3859
3860 return TRUE;
3861 }
3862
3863 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3864 not already done. */
3865
3866 static bfd_boolean
3867 create_got_section (bfd *abfd, struct bfd_link_info *info)
3868 {
3869 asection *got, *relgot;
3870 flagword flags;
3871 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3872
3873 if (!htab->got)
3874 {
3875 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3876 return FALSE;
3877
3878 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3879 if (!htab->got)
3880 abort ();
3881 }
3882
3883 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3884 | SEC_LINKER_CREATED);
3885
3886 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3887 if (!got
3888 || !bfd_set_section_alignment (abfd, got, 3))
3889 return FALSE;
3890
3891 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3892 flags | SEC_READONLY);
3893 if (!relgot
3894 || ! bfd_set_section_alignment (abfd, relgot, 3))
3895 return FALSE;
3896
3897 ppc64_elf_tdata (abfd)->got = got;
3898 ppc64_elf_tdata (abfd)->relgot = relgot;
3899 return TRUE;
3900 }
3901
3902 /* Create the dynamic sections, and set up shortcuts. */
3903
3904 static bfd_boolean
3905 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3906 {
3907 struct ppc_link_hash_table *htab;
3908
3909 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3910 return FALSE;
3911
3912 htab = ppc_hash_table (info);
3913 if (!htab->got)
3914 htab->got = bfd_get_section_by_name (dynobj, ".got");
3915 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3916 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3917 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3918 if (!info->shared)
3919 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3920
3921 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3922 || (!info->shared && !htab->relbss))
3923 abort ();
3924
3925 return TRUE;
3926 }
3927
3928 /* Merge PLT info on FROM with that on TO. */
3929
3930 static void
3931 move_plt_plist (struct ppc_link_hash_entry *from,
3932 struct ppc_link_hash_entry *to)
3933 {
3934 if (from->elf.plt.plist != NULL)
3935 {
3936 if (to->elf.plt.plist != NULL)
3937 {
3938 struct plt_entry **entp;
3939 struct plt_entry *ent;
3940
3941 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3942 {
3943 struct plt_entry *dent;
3944
3945 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3946 if (dent->addend == ent->addend)
3947 {
3948 dent->plt.refcount += ent->plt.refcount;
3949 *entp = ent->next;
3950 break;
3951 }
3952 if (dent == NULL)
3953 entp = &ent->next;
3954 }
3955 *entp = to->elf.plt.plist;
3956 }
3957
3958 to->elf.plt.plist = from->elf.plt.plist;
3959 from->elf.plt.plist = NULL;
3960 }
3961 }
3962
3963 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3964
3965 static void
3966 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3967 struct elf_link_hash_entry *dir,
3968 struct elf_link_hash_entry *ind)
3969 {
3970 struct ppc_link_hash_entry *edir, *eind;
3971
3972 edir = (struct ppc_link_hash_entry *) dir;
3973 eind = (struct ppc_link_hash_entry *) ind;
3974
3975 /* Copy over any dynamic relocs we may have on the indirect sym. */
3976 if (eind->dyn_relocs != NULL)
3977 {
3978 if (edir->dyn_relocs != NULL)
3979 {
3980 struct ppc_dyn_relocs **pp;
3981 struct ppc_dyn_relocs *p;
3982
3983 /* Add reloc counts against the indirect sym to the direct sym
3984 list. Merge any entries against the same section. */
3985 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3986 {
3987 struct ppc_dyn_relocs *q;
3988
3989 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3990 if (q->sec == p->sec)
3991 {
3992 q->pc_count += p->pc_count;
3993 q->count += p->count;
3994 *pp = p->next;
3995 break;
3996 }
3997 if (q == NULL)
3998 pp = &p->next;
3999 }
4000 *pp = edir->dyn_relocs;
4001 }
4002
4003 edir->dyn_relocs = eind->dyn_relocs;
4004 eind->dyn_relocs = NULL;
4005 }
4006
4007 edir->is_func |= eind->is_func;
4008 edir->is_func_descriptor |= eind->is_func_descriptor;
4009 edir->tls_mask |= eind->tls_mask;
4010
4011 /* If called to transfer flags for a weakdef during processing
4012 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4013 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4014 if (!(ELIMINATE_COPY_RELOCS
4015 && eind->elf.root.type != bfd_link_hash_indirect
4016 && edir->elf.dynamic_adjusted))
4017 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4018
4019 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4020 edir->elf.ref_regular |= eind->elf.ref_regular;
4021 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4022 edir->elf.needs_plt |= eind->elf.needs_plt;
4023
4024 /* If we were called to copy over info for a weak sym, that's all. */
4025 if (eind->elf.root.type != bfd_link_hash_indirect)
4026 return;
4027
4028 /* Copy over got entries that we may have already seen to the
4029 symbol which just became indirect. */
4030 if (eind->elf.got.glist != NULL)
4031 {
4032 if (edir->elf.got.glist != NULL)
4033 {
4034 struct got_entry **entp;
4035 struct got_entry *ent;
4036
4037 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4038 {
4039 struct got_entry *dent;
4040
4041 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4042 if (dent->addend == ent->addend
4043 && dent->owner == ent->owner
4044 && dent->tls_type == ent->tls_type)
4045 {
4046 dent->got.refcount += ent->got.refcount;
4047 *entp = ent->next;
4048 break;
4049 }
4050 if (dent == NULL)
4051 entp = &ent->next;
4052 }
4053 *entp = edir->elf.got.glist;
4054 }
4055
4056 edir->elf.got.glist = eind->elf.got.glist;
4057 eind->elf.got.glist = NULL;
4058 }
4059
4060 /* And plt entries. */
4061 move_plt_plist (eind, edir);
4062
4063 if (eind->elf.dynindx != -1)
4064 {
4065 if (edir->elf.dynindx != -1)
4066 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4067 edir->elf.dynstr_index);
4068 edir->elf.dynindx = eind->elf.dynindx;
4069 edir->elf.dynstr_index = eind->elf.dynstr_index;
4070 eind->elf.dynindx = -1;
4071 eind->elf.dynstr_index = 0;
4072 }
4073 }
4074
4075 /* Find the function descriptor hash entry from the given function code
4076 hash entry FH. Link the entries via their OH fields. */
4077
4078 static struct ppc_link_hash_entry *
4079 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4080 {
4081 struct ppc_link_hash_entry *fdh = fh->oh;
4082
4083 if (fdh == NULL)
4084 {
4085 const char *fd_name = fh->elf.root.root.string + 1;
4086
4087 fdh = (struct ppc_link_hash_entry *)
4088 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4089 if (fdh != NULL)
4090 {
4091 fdh->is_func_descriptor = 1;
4092 fdh->oh = fh;
4093 fh->is_func = 1;
4094 fh->oh = fdh;
4095 }
4096 }
4097
4098 return fdh;
4099 }
4100
4101 /* Make a fake function descriptor sym for the code sym FH. */
4102
4103 static struct ppc_link_hash_entry *
4104 make_fdh (struct bfd_link_info *info,
4105 struct ppc_link_hash_entry *fh)
4106 {
4107 bfd *abfd;
4108 asymbol *newsym;
4109 struct bfd_link_hash_entry *bh;
4110 struct ppc_link_hash_entry *fdh;
4111
4112 abfd = fh->elf.root.u.undef.abfd;
4113 newsym = bfd_make_empty_symbol (abfd);
4114 newsym->name = fh->elf.root.root.string + 1;
4115 newsym->section = bfd_und_section_ptr;
4116 newsym->value = 0;
4117 newsym->flags = BSF_WEAK;
4118
4119 bh = NULL;
4120 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4121 newsym->flags, newsym->section,
4122 newsym->value, NULL, FALSE, FALSE,
4123 &bh))
4124 return NULL;
4125
4126 fdh = (struct ppc_link_hash_entry *) bh;
4127 fdh->elf.non_elf = 0;
4128 fdh->fake = 1;
4129 fdh->is_func_descriptor = 1;
4130 fdh->oh = fh;
4131 fh->is_func = 1;
4132 fh->oh = fdh;
4133 return fdh;
4134 }
4135
4136 /* Fix function descriptor symbols defined in .opd sections to be
4137 function type. */
4138
4139 static bfd_boolean
4140 ppc64_elf_add_symbol_hook (bfd *ibfd ATTRIBUTE_UNUSED,
4141 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4142 Elf_Internal_Sym *isym,
4143 const char **name ATTRIBUTE_UNUSED,
4144 flagword *flags ATTRIBUTE_UNUSED,
4145 asection **sec,
4146 bfd_vma *value ATTRIBUTE_UNUSED)
4147 {
4148 if (*sec != NULL
4149 && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
4150 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4151
4152 return TRUE;
4153 }
4154
4155 /* This function makes an old ABI object reference to ".bar" cause the
4156 inclusion of a new ABI object archive that defines "bar".
4157 NAME is a symbol defined in an archive. Return a symbol in the hash
4158 table that might be satisfied by the archive symbols. */
4159
4160 static struct elf_link_hash_entry *
4161 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4162 struct bfd_link_info *info,
4163 const char *name)
4164 {
4165 struct elf_link_hash_entry *h;
4166 char *dot_name;
4167 size_t len;
4168
4169 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4170 if (h != NULL
4171 /* Don't return this sym if it is a fake function descriptor
4172 created by add_symbol_adjust. */
4173 && !(h->root.type == bfd_link_hash_undefweak
4174 && ((struct ppc_link_hash_entry *) h)->fake))
4175 return h;
4176
4177 if (name[0] == '.')
4178 return h;
4179
4180 len = strlen (name);
4181 dot_name = bfd_alloc (abfd, len + 2);
4182 if (dot_name == NULL)
4183 return (struct elf_link_hash_entry *) 0 - 1;
4184 dot_name[0] = '.';
4185 memcpy (dot_name + 1, name, len + 1);
4186 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4187 bfd_release (abfd, dot_name);
4188 return h;
4189 }
4190
4191 /* This function satisfies all old ABI object references to ".bar" if a
4192 new ABI object defines "bar". Well, at least, undefined dot symbols
4193 are made weak. This stops later archive searches from including an
4194 object if we already have a function descriptor definition. It also
4195 prevents the linker complaining about undefined symbols.
4196 We also check and correct mismatched symbol visibility here. The
4197 most restrictive visibility of the function descriptor and the
4198 function entry symbol is used. */
4199
4200 static bfd_boolean
4201 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4202 {
4203 struct ppc_link_hash_table *htab;
4204 struct ppc_link_hash_entry *fdh;
4205
4206 if (eh->elf.root.type == bfd_link_hash_indirect)
4207 return TRUE;
4208
4209 if (eh->elf.root.type == bfd_link_hash_warning)
4210 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4211
4212 if (eh->elf.root.root.string[0] != '.')
4213 abort ();
4214
4215 htab = ppc_hash_table (info);
4216 fdh = get_fdh (eh, htab);
4217 if (fdh == NULL
4218 && !info->relocatable
4219 && (eh->elf.root.type == bfd_link_hash_undefined
4220 || eh->elf.root.type == bfd_link_hash_undefweak)
4221 && eh->elf.ref_regular)
4222 {
4223 /* Make an undefweak function descriptor sym, which is enough to
4224 pull in an --as-needed shared lib, but won't cause link
4225 errors. Archives are handled elsewhere. */
4226 fdh = make_fdh (info, eh);
4227 if (fdh == NULL)
4228 return FALSE;
4229 else
4230 fdh->elf.ref_regular = 1;
4231 }
4232 else if (fdh != NULL)
4233 {
4234 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4235 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4236 if (entry_vis < descr_vis)
4237 fdh->elf.other += entry_vis - descr_vis;
4238 else if (entry_vis > descr_vis)
4239 eh->elf.other += descr_vis - entry_vis;
4240
4241 if ((fdh->elf.root.type == bfd_link_hash_defined
4242 || fdh->elf.root.type == bfd_link_hash_defweak)
4243 && eh->elf.root.type == bfd_link_hash_undefined)
4244 {
4245 eh->elf.root.type = bfd_link_hash_undefweak;
4246 eh->was_undefined = 1;
4247 htab->twiddled_syms = 1;
4248 }
4249 }
4250
4251 return TRUE;
4252 }
4253
4254 /* Process list of dot-symbols we made in link_hash_newfunc. */
4255
4256 static bfd_boolean
4257 ppc64_elf_check_directives (bfd *ibfd, struct bfd_link_info *info)
4258 {
4259 struct ppc_link_hash_table *htab;
4260 struct ppc_link_hash_entry **p, *eh;
4261
4262 htab = ppc_hash_table (info);
4263 if (!is_ppc64_elf_target (htab->elf.root.creator))
4264 return TRUE;
4265
4266 if (is_ppc64_elf_target (ibfd->xvec))
4267 {
4268 p = &htab->dot_syms;
4269 while ((eh = *p) != NULL)
4270 {
4271 *p = NULL;
4272 if (!add_symbol_adjust (eh, info))
4273 return FALSE;
4274 p = &eh->u.next_dot_sym;
4275 }
4276 }
4277
4278 /* Clear the list for non-ppc64 input files. */
4279 p = &htab->dot_syms;
4280 while ((eh = *p) != NULL)
4281 {
4282 *p = NULL;
4283 p = &eh->u.next_dot_sym;
4284 }
4285
4286 /* We need to fix the undefs list for any syms we have twiddled to
4287 undef_weak. */
4288 if (htab->twiddled_syms)
4289 {
4290 bfd_link_repair_undef_list (&htab->elf.root);
4291 htab->twiddled_syms = 0;
4292 }
4293 return TRUE;
4294 }
4295
4296 /* Undo hash table changes when an --as-needed input file is determined
4297 not to be needed. */
4298
4299 static bfd_boolean
4300 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4301 struct bfd_link_info *info)
4302 {
4303 ppc_hash_table (info)->dot_syms = NULL;
4304 return TRUE;
4305 }
4306
4307 static bfd_boolean
4308 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4309 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4310 {
4311 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4312 char *local_got_tls_masks;
4313
4314 if (local_got_ents == NULL)
4315 {
4316 bfd_size_type size = symtab_hdr->sh_info;
4317
4318 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4319 local_got_ents = bfd_zalloc (abfd, size);
4320 if (local_got_ents == NULL)
4321 return FALSE;
4322 elf_local_got_ents (abfd) = local_got_ents;
4323 }
4324
4325 if ((tls_type & TLS_EXPLICIT) == 0)
4326 {
4327 struct got_entry *ent;
4328
4329 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4330 if (ent->addend == r_addend
4331 && ent->owner == abfd
4332 && ent->tls_type == tls_type)
4333 break;
4334 if (ent == NULL)
4335 {
4336 bfd_size_type amt = sizeof (*ent);
4337 ent = bfd_alloc (abfd, amt);
4338 if (ent == NULL)
4339 return FALSE;
4340 ent->next = local_got_ents[r_symndx];
4341 ent->addend = r_addend;
4342 ent->owner = abfd;
4343 ent->tls_type = tls_type;
4344 ent->got.refcount = 0;
4345 local_got_ents[r_symndx] = ent;
4346 }
4347 ent->got.refcount += 1;
4348 }
4349
4350 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4351 local_got_tls_masks[r_symndx] |= tls_type;
4352 return TRUE;
4353 }
4354
4355 static bfd_boolean
4356 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
4357 {
4358 struct plt_entry *ent;
4359
4360 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4361 if (ent->addend == addend)
4362 break;
4363 if (ent == NULL)
4364 {
4365 bfd_size_type amt = sizeof (*ent);
4366 ent = bfd_alloc (abfd, amt);
4367 if (ent == NULL)
4368 return FALSE;
4369 ent->next = eh->elf.plt.plist;
4370 ent->addend = addend;
4371 ent->plt.refcount = 0;
4372 eh->elf.plt.plist = ent;
4373 }
4374 ent->plt.refcount += 1;
4375 eh->elf.needs_plt = 1;
4376 if (eh->elf.root.root.string[0] == '.'
4377 && eh->elf.root.root.string[1] != '\0')
4378 eh->is_func = 1;
4379 return TRUE;
4380 }
4381
4382 /* Look through the relocs for a section during the first phase, and
4383 calculate needed space in the global offset table, procedure
4384 linkage table, and dynamic reloc sections. */
4385
4386 static bfd_boolean
4387 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4388 asection *sec, const Elf_Internal_Rela *relocs)
4389 {
4390 struct ppc_link_hash_table *htab;
4391 Elf_Internal_Shdr *symtab_hdr;
4392 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4393 const Elf_Internal_Rela *rel;
4394 const Elf_Internal_Rela *rel_end;
4395 asection *sreloc;
4396 asection **opd_sym_map;
4397
4398 if (info->relocatable)
4399 return TRUE;
4400
4401 /* Don't do anything special with non-loaded, non-alloced sections.
4402 In particular, any relocs in such sections should not affect GOT
4403 and PLT reference counting (ie. we don't allow them to create GOT
4404 or PLT entries), there's no possibility or desire to optimize TLS
4405 relocs, and there's not much point in propagating relocs to shared
4406 libs that the dynamic linker won't relocate. */
4407 if ((sec->flags & SEC_ALLOC) == 0)
4408 return TRUE;
4409
4410 htab = ppc_hash_table (info);
4411 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4412
4413 sym_hashes = elf_sym_hashes (abfd);
4414 sym_hashes_end = (sym_hashes
4415 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4416 - symtab_hdr->sh_info);
4417
4418 sreloc = NULL;
4419 opd_sym_map = NULL;
4420 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4421 {
4422 /* Garbage collection needs some extra help with .opd sections.
4423 We don't want to necessarily keep everything referenced by
4424 relocs in .opd, as that would keep all functions. Instead,
4425 if we reference an .opd symbol (a function descriptor), we
4426 want to keep the function code symbol's section. This is
4427 easy for global symbols, but for local syms we need to keep
4428 information about the associated function section. Later, if
4429 edit_opd deletes entries, we'll use this array to adjust
4430 local syms in .opd. */
4431 union opd_info {
4432 asection *func_section;
4433 long entry_adjust;
4434 };
4435 bfd_size_type amt;
4436
4437 amt = sec->size * sizeof (union opd_info) / 8;
4438 opd_sym_map = bfd_zalloc (abfd, amt);
4439 if (opd_sym_map == NULL)
4440 return FALSE;
4441 ppc64_elf_section_data (sec)->u.opd_func_sec = opd_sym_map;
4442 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4443 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4444 }
4445
4446 if (htab->sfpr == NULL
4447 && !create_linkage_sections (htab->elf.dynobj, info))
4448 return FALSE;
4449
4450 rel_end = relocs + sec->reloc_count;
4451 for (rel = relocs; rel < rel_end; rel++)
4452 {
4453 unsigned long r_symndx;
4454 struct elf_link_hash_entry *h;
4455 enum elf_ppc64_reloc_type r_type;
4456 int tls_type = 0;
4457 struct _ppc64_elf_section_data *ppc64_sec;
4458
4459 r_symndx = ELF64_R_SYM (rel->r_info);
4460 if (r_symndx < symtab_hdr->sh_info)
4461 h = NULL;
4462 else
4463 {
4464 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4465 while (h->root.type == bfd_link_hash_indirect
4466 || h->root.type == bfd_link_hash_warning)
4467 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4468 }
4469
4470 r_type = ELF64_R_TYPE (rel->r_info);
4471 switch (r_type)
4472 {
4473 case R_PPC64_GOT_TLSLD16:
4474 case R_PPC64_GOT_TLSLD16_LO:
4475 case R_PPC64_GOT_TLSLD16_HI:
4476 case R_PPC64_GOT_TLSLD16_HA:
4477 ppc64_tlsld_got (abfd)->refcount += 1;
4478 tls_type = TLS_TLS | TLS_LD;
4479 goto dogottls;
4480
4481 case R_PPC64_GOT_TLSGD16:
4482 case R_PPC64_GOT_TLSGD16_LO:
4483 case R_PPC64_GOT_TLSGD16_HI:
4484 case R_PPC64_GOT_TLSGD16_HA:
4485 tls_type = TLS_TLS | TLS_GD;
4486 goto dogottls;
4487
4488 case R_PPC64_GOT_TPREL16_DS:
4489 case R_PPC64_GOT_TPREL16_LO_DS:
4490 case R_PPC64_GOT_TPREL16_HI:
4491 case R_PPC64_GOT_TPREL16_HA:
4492 if (info->shared)
4493 info->flags |= DF_STATIC_TLS;
4494 tls_type = TLS_TLS | TLS_TPREL;
4495 goto dogottls;
4496
4497 case R_PPC64_GOT_DTPREL16_DS:
4498 case R_PPC64_GOT_DTPREL16_LO_DS:
4499 case R_PPC64_GOT_DTPREL16_HI:
4500 case R_PPC64_GOT_DTPREL16_HA:
4501 tls_type = TLS_TLS | TLS_DTPREL;
4502 dogottls:
4503 sec->has_tls_reloc = 1;
4504 /* Fall thru */
4505
4506 case R_PPC64_GOT16:
4507 case R_PPC64_GOT16_DS:
4508 case R_PPC64_GOT16_HA:
4509 case R_PPC64_GOT16_HI:
4510 case R_PPC64_GOT16_LO:
4511 case R_PPC64_GOT16_LO_DS:
4512 /* This symbol requires a global offset table entry. */
4513 sec->has_toc_reloc = 1;
4514 if (ppc64_elf_tdata (abfd)->got == NULL
4515 && !create_got_section (abfd, info))
4516 return FALSE;
4517
4518 if (h != NULL)
4519 {
4520 struct ppc_link_hash_entry *eh;
4521 struct got_entry *ent;
4522
4523 eh = (struct ppc_link_hash_entry *) h;
4524 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4525 if (ent->addend == rel->r_addend
4526 && ent->owner == abfd
4527 && ent->tls_type == tls_type)
4528 break;
4529 if (ent == NULL)
4530 {
4531 bfd_size_type amt = sizeof (*ent);
4532 ent = bfd_alloc (abfd, amt);
4533 if (ent == NULL)
4534 return FALSE;
4535 ent->next = eh->elf.got.glist;
4536 ent->addend = rel->r_addend;
4537 ent->owner = abfd;
4538 ent->tls_type = tls_type;
4539 ent->got.refcount = 0;
4540 eh->elf.got.glist = ent;
4541 }
4542 ent->got.refcount += 1;
4543 eh->tls_mask |= tls_type;
4544 }
4545 else
4546 /* This is a global offset table entry for a local symbol. */
4547 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4548 rel->r_addend, tls_type))
4549 return FALSE;
4550 break;
4551
4552 case R_PPC64_PLT16_HA:
4553 case R_PPC64_PLT16_HI:
4554 case R_PPC64_PLT16_LO:
4555 case R_PPC64_PLT32:
4556 case R_PPC64_PLT64:
4557 /* This symbol requires a procedure linkage table entry. We
4558 actually build the entry in adjust_dynamic_symbol,
4559 because this might be a case of linking PIC code without
4560 linking in any dynamic objects, in which case we don't
4561 need to generate a procedure linkage table after all. */
4562 if (h == NULL)
4563 {
4564 /* It does not make sense to have a procedure linkage
4565 table entry for a local symbol. */
4566 bfd_set_error (bfd_error_bad_value);
4567 return FALSE;
4568 }
4569 else
4570 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4571 rel->r_addend))
4572 return FALSE;
4573 break;
4574
4575 /* The following relocations don't need to propagate the
4576 relocation if linking a shared object since they are
4577 section relative. */
4578 case R_PPC64_SECTOFF:
4579 case R_PPC64_SECTOFF_LO:
4580 case R_PPC64_SECTOFF_HI:
4581 case R_PPC64_SECTOFF_HA:
4582 case R_PPC64_SECTOFF_DS:
4583 case R_PPC64_SECTOFF_LO_DS:
4584 case R_PPC64_DTPREL16:
4585 case R_PPC64_DTPREL16_LO:
4586 case R_PPC64_DTPREL16_HI:
4587 case R_PPC64_DTPREL16_HA:
4588 case R_PPC64_DTPREL16_DS:
4589 case R_PPC64_DTPREL16_LO_DS:
4590 case R_PPC64_DTPREL16_HIGHER:
4591 case R_PPC64_DTPREL16_HIGHERA:
4592 case R_PPC64_DTPREL16_HIGHEST:
4593 case R_PPC64_DTPREL16_HIGHESTA:
4594 break;
4595
4596 /* Nor do these. */
4597 case R_PPC64_TOC16:
4598 case R_PPC64_TOC16_LO:
4599 case R_PPC64_TOC16_HI:
4600 case R_PPC64_TOC16_HA:
4601 case R_PPC64_TOC16_DS:
4602 case R_PPC64_TOC16_LO_DS:
4603 sec->has_toc_reloc = 1;
4604 break;
4605
4606 /* This relocation describes the C++ object vtable hierarchy.
4607 Reconstruct it for later use during GC. */
4608 case R_PPC64_GNU_VTINHERIT:
4609 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4610 return FALSE;
4611 break;
4612
4613 /* This relocation describes which C++ vtable entries are actually
4614 used. Record for later use during GC. */
4615 case R_PPC64_GNU_VTENTRY:
4616 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4617 return FALSE;
4618 break;
4619
4620 case R_PPC64_REL14:
4621 case R_PPC64_REL14_BRTAKEN:
4622 case R_PPC64_REL14_BRNTAKEN:
4623 {
4624 asection *dest = NULL;
4625
4626 /* Heuristic: If jumping outside our section, chances are
4627 we are going to need a stub. */
4628 if (h != NULL)
4629 {
4630 /* If the sym is weak it may be overridden later, so
4631 don't assume we know where a weak sym lives. */
4632 if (h->root.type == bfd_link_hash_defined)
4633 dest = h->root.u.def.section;
4634 }
4635 else
4636 dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4637 sec, r_symndx);
4638 if (dest != sec)
4639 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4640 }
4641 /* Fall through. */
4642
4643 case R_PPC64_REL24:
4644 if (h != NULL)
4645 {
4646 /* We may need a .plt entry if the function this reloc
4647 refers to is in a shared lib. */
4648 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4649 rel->r_addend))
4650 return FALSE;
4651 if (h == &htab->tls_get_addr->elf
4652 || h == &htab->tls_get_addr_fd->elf)
4653 sec->has_tls_reloc = 1;
4654 else if (htab->tls_get_addr == NULL
4655 && CONST_STRNEQ (h->root.root.string, ".__tls_get_addr")
4656 && (h->root.root.string[15] == 0
4657 || h->root.root.string[15] == '@'))
4658 {
4659 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4660 sec->has_tls_reloc = 1;
4661 }
4662 else if (htab->tls_get_addr_fd == NULL
4663 && CONST_STRNEQ (h->root.root.string, "__tls_get_addr")
4664 && (h->root.root.string[14] == 0
4665 || h->root.root.string[14] == '@'))
4666 {
4667 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
4668 sec->has_tls_reloc = 1;
4669 }
4670 }
4671 break;
4672
4673 case R_PPC64_TPREL64:
4674 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4675 if (info->shared)
4676 info->flags |= DF_STATIC_TLS;
4677 goto dotlstoc;
4678
4679 case R_PPC64_DTPMOD64:
4680 if (rel + 1 < rel_end
4681 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4682 && rel[1].r_offset == rel->r_offset + 8)
4683 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4684 else
4685 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4686 goto dotlstoc;
4687
4688 case R_PPC64_DTPREL64:
4689 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4690 if (rel != relocs
4691 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4692 && rel[-1].r_offset == rel->r_offset - 8)
4693 /* This is the second reloc of a dtpmod, dtprel pair.
4694 Don't mark with TLS_DTPREL. */
4695 goto dodyn;
4696
4697 dotlstoc:
4698 sec->has_tls_reloc = 1;
4699 if (h != NULL)
4700 {
4701 struct ppc_link_hash_entry *eh;
4702 eh = (struct ppc_link_hash_entry *) h;
4703 eh->tls_mask |= tls_type;
4704 }
4705 else
4706 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4707 rel->r_addend, tls_type))
4708 return FALSE;
4709
4710 ppc64_sec = ppc64_elf_section_data (sec);
4711 if (ppc64_sec->sec_type != sec_toc)
4712 {
4713 /* One extra to simplify get_tls_mask. */
4714 bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4715 ppc64_sec->u.t_symndx = bfd_zalloc (abfd, amt);
4716 if (ppc64_sec->u.t_symndx == NULL)
4717 return FALSE;
4718 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4719 ppc64_sec->sec_type = sec_toc;
4720 }
4721 BFD_ASSERT (rel->r_offset % 8 == 0);
4722 ppc64_sec->u.t_symndx[rel->r_offset / 8] = r_symndx;
4723
4724 /* Mark the second slot of a GD or LD entry.
4725 -1 to indicate GD and -2 to indicate LD. */
4726 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4727 ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -1;
4728 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4729 ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -2;
4730 goto dodyn;
4731
4732 case R_PPC64_TPREL16:
4733 case R_PPC64_TPREL16_LO:
4734 case R_PPC64_TPREL16_HI:
4735 case R_PPC64_TPREL16_HA:
4736 case R_PPC64_TPREL16_DS:
4737 case R_PPC64_TPREL16_LO_DS:
4738 case R_PPC64_TPREL16_HIGHER:
4739 case R_PPC64_TPREL16_HIGHERA:
4740 case R_PPC64_TPREL16_HIGHEST:
4741 case R_PPC64_TPREL16_HIGHESTA:
4742 if (info->shared)
4743 {
4744 info->flags |= DF_STATIC_TLS;
4745 goto dodyn;
4746 }
4747 break;
4748
4749 case R_PPC64_ADDR64:
4750 if (opd_sym_map != NULL
4751 && rel + 1 < rel_end
4752 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4753 {
4754 if (h != NULL)
4755 {
4756 if (h->root.root.string[0] == '.'
4757 && h->root.root.string[1] != 0
4758 && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4759 ;
4760 else
4761 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4762 }
4763 else
4764 {
4765 asection *s;
4766
4767 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4768 r_symndx);
4769 if (s == NULL)
4770 return FALSE;
4771 else if (s != sec)
4772 opd_sym_map[rel->r_offset / 8] = s;
4773 }
4774 }
4775 /* Fall through. */
4776
4777 case R_PPC64_REL30:
4778 case R_PPC64_REL32:
4779 case R_PPC64_REL64:
4780 case R_PPC64_ADDR14:
4781 case R_PPC64_ADDR14_BRNTAKEN:
4782 case R_PPC64_ADDR14_BRTAKEN:
4783 case R_PPC64_ADDR16:
4784 case R_PPC64_ADDR16_DS:
4785 case R_PPC64_ADDR16_HA:
4786 case R_PPC64_ADDR16_HI:
4787 case R_PPC64_ADDR16_HIGHER:
4788 case R_PPC64_ADDR16_HIGHERA:
4789 case R_PPC64_ADDR16_HIGHEST:
4790 case R_PPC64_ADDR16_HIGHESTA:
4791 case R_PPC64_ADDR16_LO:
4792 case R_PPC64_ADDR16_LO_DS:
4793 case R_PPC64_ADDR24:
4794 case R_PPC64_ADDR32:
4795 case R_PPC64_UADDR16:
4796 case R_PPC64_UADDR32:
4797 case R_PPC64_UADDR64:
4798 case R_PPC64_TOC:
4799 if (h != NULL && !info->shared)
4800 /* We may need a copy reloc. */
4801 h->non_got_ref = 1;
4802
4803 /* Don't propagate .opd relocs. */
4804 if (NO_OPD_RELOCS && opd_sym_map != NULL)
4805 break;
4806
4807 /* If we are creating a shared library, and this is a reloc
4808 against a global symbol, or a non PC relative reloc
4809 against a local symbol, then we need to copy the reloc
4810 into the shared library. However, if we are linking with
4811 -Bsymbolic, we do not need to copy a reloc against a
4812 global symbol which is defined in an object we are
4813 including in the link (i.e., DEF_REGULAR is set). At
4814 this point we have not seen all the input files, so it is
4815 possible that DEF_REGULAR is not set now but will be set
4816 later (it is never cleared). In case of a weak definition,
4817 DEF_REGULAR may be cleared later by a strong definition in
4818 a shared library. We account for that possibility below by
4819 storing information in the dyn_relocs field of the hash
4820 table entry. A similar situation occurs when creating
4821 shared libraries and symbol visibility changes render the
4822 symbol local.
4823
4824 If on the other hand, we are creating an executable, we
4825 may need to keep relocations for symbols satisfied by a
4826 dynamic library if we manage to avoid copy relocs for the
4827 symbol. */
4828 dodyn:
4829 if ((info->shared
4830 && (MUST_BE_DYN_RELOC (r_type)
4831 || (h != NULL
4832 && (! info->symbolic
4833 || h->root.type == bfd_link_hash_defweak
4834 || !h->def_regular))))
4835 || (ELIMINATE_COPY_RELOCS
4836 && !info->shared
4837 && h != NULL
4838 && (h->root.type == bfd_link_hash_defweak
4839 || !h->def_regular)))
4840 {
4841 struct ppc_dyn_relocs *p;
4842 struct ppc_dyn_relocs **head;
4843
4844 /* We must copy these reloc types into the output file.
4845 Create a reloc section in dynobj and make room for
4846 this reloc. */
4847 if (sreloc == NULL)
4848 {
4849 const char *name;
4850 bfd *dynobj;
4851
4852 name = (bfd_elf_string_from_elf_section
4853 (abfd,
4854 elf_elfheader (abfd)->e_shstrndx,
4855 elf_section_data (sec)->rel_hdr.sh_name));
4856 if (name == NULL)
4857 return FALSE;
4858
4859 if (! CONST_STRNEQ (name, ".rela")
4860 || strcmp (bfd_get_section_name (abfd, sec),
4861 name + 5) != 0)
4862 {
4863 (*_bfd_error_handler)
4864 (_("%B: bad relocation section name `%s\'"),
4865 abfd, name);
4866 bfd_set_error (bfd_error_bad_value);
4867 }
4868
4869 dynobj = htab->elf.dynobj;
4870 sreloc = bfd_get_section_by_name (dynobj, name);
4871 if (sreloc == NULL)
4872 {
4873 flagword flags;
4874
4875 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4876 | SEC_IN_MEMORY | SEC_LINKER_CREATED
4877 | SEC_ALLOC | SEC_LOAD);
4878 sreloc = bfd_make_section_with_flags (dynobj,
4879 name,
4880 flags);
4881 if (sreloc == NULL
4882 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4883 return FALSE;
4884 }
4885 elf_section_data (sec)->sreloc = sreloc;
4886 }
4887
4888 /* If this is a global symbol, we count the number of
4889 relocations we need for this symbol. */
4890 if (h != NULL)
4891 {
4892 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4893 }
4894 else
4895 {
4896 /* Track dynamic relocs needed for local syms too.
4897 We really need local syms available to do this
4898 easily. Oh well. */
4899
4900 asection *s;
4901 void *vpp;
4902
4903 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4904 sec, r_symndx);
4905 if (s == NULL)
4906 return FALSE;
4907
4908 vpp = &elf_section_data (s)->local_dynrel;
4909 head = (struct ppc_dyn_relocs **) vpp;
4910 }
4911
4912 p = *head;
4913 if (p == NULL || p->sec != sec)
4914 {
4915 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4916 if (p == NULL)
4917 return FALSE;
4918 p->next = *head;
4919 *head = p;
4920 p->sec = sec;
4921 p->count = 0;
4922 p->pc_count = 0;
4923 }
4924
4925 p->count += 1;
4926 if (!MUST_BE_DYN_RELOC (r_type))
4927 p->pc_count += 1;
4928 }
4929 break;
4930
4931 default:
4932 break;
4933 }
4934 }
4935
4936 return TRUE;
4937 }
4938
4939 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4940 of the code entry point, and its section. */
4941
4942 static bfd_vma
4943 opd_entry_value (asection *opd_sec,
4944 bfd_vma offset,
4945 asection **code_sec,
4946 bfd_vma *code_off)
4947 {
4948 bfd *opd_bfd = opd_sec->owner;
4949 Elf_Internal_Rela *relocs;
4950 Elf_Internal_Rela *lo, *hi, *look;
4951 bfd_vma val;
4952
4953 /* No relocs implies we are linking a --just-symbols object. */
4954 if (opd_sec->reloc_count == 0)
4955 {
4956 bfd_vma val;
4957
4958 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4959 return (bfd_vma) -1;
4960
4961 if (code_sec != NULL)
4962 {
4963 asection *sec, *likely = NULL;
4964 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4965 if (sec->vma <= val
4966 && (sec->flags & SEC_LOAD) != 0
4967 && (sec->flags & SEC_ALLOC) != 0)
4968 likely = sec;
4969 if (likely != NULL)
4970 {
4971 *code_sec = likely;
4972 if (code_off != NULL)
4973 *code_off = val - likely->vma;
4974 }
4975 }
4976 return val;
4977 }
4978
4979 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4980 if (relocs == NULL)
4981 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4982
4983 /* Go find the opd reloc at the sym address. */
4984 lo = relocs;
4985 BFD_ASSERT (lo != NULL);
4986 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
4987 val = (bfd_vma) -1;
4988 while (lo < hi)
4989 {
4990 look = lo + (hi - lo) / 2;
4991 if (look->r_offset < offset)
4992 lo = look + 1;
4993 else if (look->r_offset > offset)
4994 hi = look;
4995 else
4996 {
4997 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4998 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4999 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5000 {
5001 unsigned long symndx = ELF64_R_SYM (look->r_info);
5002 asection *sec;
5003
5004 if (symndx < symtab_hdr->sh_info)
5005 {
5006 Elf_Internal_Sym *sym;
5007
5008 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5009 if (sym == NULL)
5010 {
5011 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5012 symtab_hdr->sh_info,
5013 0, NULL, NULL, NULL);
5014 if (sym == NULL)
5015 break;
5016 symtab_hdr->contents = (bfd_byte *) sym;
5017 }
5018
5019 sym += symndx;
5020 val = sym->st_value;
5021 sec = NULL;
5022 if ((sym->st_shndx != SHN_UNDEF
5023 && sym->st_shndx < SHN_LORESERVE)
5024 || sym->st_shndx > SHN_HIRESERVE)
5025 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5026 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5027 }
5028 else
5029 {
5030 struct elf_link_hash_entry **sym_hashes;
5031 struct elf_link_hash_entry *rh;
5032
5033 sym_hashes = elf_sym_hashes (opd_bfd);
5034 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5035 while (rh->root.type == bfd_link_hash_indirect
5036 || rh->root.type == bfd_link_hash_warning)
5037 rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
5038 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5039 || rh->root.type == bfd_link_hash_defweak);
5040 val = rh->root.u.def.value;
5041 sec = rh->root.u.def.section;
5042 }
5043 val += look->r_addend;
5044 if (code_off != NULL)
5045 *code_off = val;
5046 if (code_sec != NULL)
5047 *code_sec = sec;
5048 if (sec != NULL && sec->output_section != NULL)
5049 val += sec->output_section->vma + sec->output_offset;
5050 }
5051 break;
5052 }
5053 }
5054
5055 return val;
5056 }
5057
5058 /* Mark sections containing dynamically referenced symbols. When
5059 building shared libraries, we must assume that any visible symbol is
5060 referenced. */
5061
5062 static bfd_boolean
5063 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5064 {
5065 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5066 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5067
5068 if (eh->elf.root.type == bfd_link_hash_warning)
5069 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5070
5071 /* Dynamic linking info is on the func descriptor sym. */
5072 if (eh->oh != NULL
5073 && eh->oh->is_func_descriptor
5074 && (eh->oh->elf.root.type == bfd_link_hash_defined
5075 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5076 eh = eh->oh;
5077
5078 if ((eh->elf.root.type == bfd_link_hash_defined
5079 || eh->elf.root.type == bfd_link_hash_defweak)
5080 && (eh->elf.ref_dynamic
5081 || (!info->executable
5082 && eh->elf.def_regular
5083 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5084 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5085 {
5086 asection *code_sec;
5087
5088 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5089
5090 /* Function descriptor syms cause the associated
5091 function code sym section to be marked. */
5092 if (eh->is_func_descriptor
5093 && (eh->oh->elf.root.type == bfd_link_hash_defined
5094 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5095 eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
5096 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5097 && opd_entry_value (eh->elf.root.u.def.section,
5098 eh->elf.root.u.def.value,
5099 &code_sec, NULL) != (bfd_vma) -1)
5100 code_sec->flags |= SEC_KEEP;
5101 }
5102
5103 return TRUE;
5104 }
5105
5106 /* Return the section that should be marked against GC for a given
5107 relocation. */
5108
5109 static asection *
5110 ppc64_elf_gc_mark_hook (asection *sec,
5111 struct bfd_link_info *info,
5112 Elf_Internal_Rela *rel,
5113 struct elf_link_hash_entry *h,
5114 Elf_Internal_Sym *sym)
5115 {
5116 asection *rsec;
5117
5118 /* First mark all our entry sym sections. */
5119 if (info->gc_sym_list != NULL)
5120 {
5121 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5122 struct bfd_sym_chain *sym = info->gc_sym_list;
5123
5124 info->gc_sym_list = NULL;
5125 for (; sym != NULL; sym = sym->next)
5126 {
5127 struct ppc_link_hash_entry *eh;
5128
5129 eh = (struct ppc_link_hash_entry *)
5130 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5131 if (eh == NULL)
5132 continue;
5133 if (eh->elf.root.type != bfd_link_hash_defined
5134 && eh->elf.root.type != bfd_link_hash_defweak)
5135 continue;
5136
5137 if (eh->is_func_descriptor
5138 && (eh->oh->elf.root.type == bfd_link_hash_defined
5139 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5140 rsec = eh->oh->elf.root.u.def.section;
5141 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5142 && opd_entry_value (eh->elf.root.u.def.section,
5143 eh->elf.root.u.def.value,
5144 &rsec, NULL) != (bfd_vma) -1)
5145 ;
5146 else
5147 continue;
5148
5149 if (!rsec->gc_mark)
5150 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5151
5152 rsec = eh->elf.root.u.def.section;
5153 if (!rsec->gc_mark)
5154 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5155 }
5156 }
5157
5158 /* Syms return NULL if we're marking .opd, so we avoid marking all
5159 function sections, as all functions are referenced in .opd. */
5160 rsec = NULL;
5161 if (get_opd_info (sec) != NULL)
5162 return rsec;
5163
5164 if (h != NULL)
5165 {
5166 enum elf_ppc64_reloc_type r_type;
5167 struct ppc_link_hash_entry *eh;
5168
5169 r_type = ELF64_R_TYPE (rel->r_info);
5170 switch (r_type)
5171 {
5172 case R_PPC64_GNU_VTINHERIT:
5173 case R_PPC64_GNU_VTENTRY:
5174 break;
5175
5176 default:
5177 switch (h->root.type)
5178 {
5179 case bfd_link_hash_defined:
5180 case bfd_link_hash_defweak:
5181 eh = (struct ppc_link_hash_entry *) h;
5182 if (eh->oh != NULL
5183 && eh->oh->is_func_descriptor
5184 && (eh->oh->elf.root.type == bfd_link_hash_defined
5185 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5186 eh = eh->oh;
5187
5188 /* Function descriptor syms cause the associated
5189 function code sym section to be marked. */
5190 if (eh->is_func_descriptor
5191 && (eh->oh->elf.root.type == bfd_link_hash_defined
5192 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5193 {
5194 /* They also mark their opd section. */
5195 if (!eh->elf.root.u.def.section->gc_mark)
5196 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5197 ppc64_elf_gc_mark_hook);
5198
5199 rsec = eh->oh->elf.root.u.def.section;
5200 }
5201 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5202 && opd_entry_value (eh->elf.root.u.def.section,
5203 eh->elf.root.u.def.value,
5204 &rsec, NULL) != (bfd_vma) -1)
5205 {
5206 if (!eh->elf.root.u.def.section->gc_mark)
5207 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5208 ppc64_elf_gc_mark_hook);
5209 }
5210 else
5211 rsec = h->root.u.def.section;
5212 break;
5213
5214 case bfd_link_hash_common:
5215 rsec = h->root.u.c.p->section;
5216 break;
5217
5218 default:
5219 break;
5220 }
5221 }
5222 }
5223 else
5224 {
5225 asection **opd_sym_section;
5226
5227 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5228 opd_sym_section = get_opd_info (rsec);
5229 if (opd_sym_section != NULL)
5230 {
5231 if (!rsec->gc_mark)
5232 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5233
5234 rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
5235 }
5236 }
5237
5238 return rsec;
5239 }
5240
5241 /* Update the .got, .plt. and dynamic reloc reference counts for the
5242 section being removed. */
5243
5244 static bfd_boolean
5245 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5246 asection *sec, const Elf_Internal_Rela *relocs)
5247 {
5248 struct ppc_link_hash_table *htab;
5249 Elf_Internal_Shdr *symtab_hdr;
5250 struct elf_link_hash_entry **sym_hashes;
5251 struct got_entry **local_got_ents;
5252 const Elf_Internal_Rela *rel, *relend;
5253
5254 if ((sec->flags & SEC_ALLOC) == 0)
5255 return TRUE;
5256
5257 elf_section_data (sec)->local_dynrel = NULL;
5258
5259 htab = ppc_hash_table (info);
5260 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5261 sym_hashes = elf_sym_hashes (abfd);
5262 local_got_ents = elf_local_got_ents (abfd);
5263
5264 relend = relocs + sec->reloc_count;
5265 for (rel = relocs; rel < relend; rel++)
5266 {
5267 unsigned long r_symndx;
5268 enum elf_ppc64_reloc_type r_type;
5269 struct elf_link_hash_entry *h = NULL;
5270 char tls_type = 0;
5271
5272 r_symndx = ELF64_R_SYM (rel->r_info);
5273 r_type = ELF64_R_TYPE (rel->r_info);
5274 if (r_symndx >= symtab_hdr->sh_info)
5275 {
5276 struct ppc_link_hash_entry *eh;
5277 struct ppc_dyn_relocs **pp;
5278 struct ppc_dyn_relocs *p;
5279
5280 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5281 while (h->root.type == bfd_link_hash_indirect
5282 || h->root.type == bfd_link_hash_warning)
5283 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5284 eh = (struct ppc_link_hash_entry *) h;
5285
5286 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5287 if (p->sec == sec)
5288 {
5289 /* Everything must go for SEC. */
5290 *pp = p->next;
5291 break;
5292 }
5293 }
5294
5295 switch (r_type)
5296 {
5297 case R_PPC64_GOT_TLSLD16:
5298 case R_PPC64_GOT_TLSLD16_LO:
5299 case R_PPC64_GOT_TLSLD16_HI:
5300 case R_PPC64_GOT_TLSLD16_HA:
5301 ppc64_tlsld_got (abfd)->refcount -= 1;
5302 tls_type = TLS_TLS | TLS_LD;
5303 goto dogot;
5304
5305 case R_PPC64_GOT_TLSGD16:
5306 case R_PPC64_GOT_TLSGD16_LO:
5307 case R_PPC64_GOT_TLSGD16_HI:
5308 case R_PPC64_GOT_TLSGD16_HA:
5309 tls_type = TLS_TLS | TLS_GD;
5310 goto dogot;
5311
5312 case R_PPC64_GOT_TPREL16_DS:
5313 case R_PPC64_GOT_TPREL16_LO_DS:
5314 case R_PPC64_GOT_TPREL16_HI:
5315 case R_PPC64_GOT_TPREL16_HA:
5316 tls_type = TLS_TLS | TLS_TPREL;
5317 goto dogot;
5318
5319 case R_PPC64_GOT_DTPREL16_DS:
5320 case R_PPC64_GOT_DTPREL16_LO_DS:
5321 case R_PPC64_GOT_DTPREL16_HI:
5322 case R_PPC64_GOT_DTPREL16_HA:
5323 tls_type = TLS_TLS | TLS_DTPREL;
5324 goto dogot;
5325
5326 case R_PPC64_GOT16:
5327 case R_PPC64_GOT16_DS:
5328 case R_PPC64_GOT16_HA:
5329 case R_PPC64_GOT16_HI:
5330 case R_PPC64_GOT16_LO:
5331 case R_PPC64_GOT16_LO_DS:
5332 dogot:
5333 {
5334 struct got_entry *ent;
5335
5336 if (h != NULL)
5337 ent = h->got.glist;
5338 else
5339 ent = local_got_ents[r_symndx];
5340
5341 for (; ent != NULL; ent = ent->next)
5342 if (ent->addend == rel->r_addend
5343 && ent->owner == abfd
5344 && ent->tls_type == tls_type)
5345 break;
5346 if (ent == NULL)
5347 abort ();
5348 if (ent->got.refcount > 0)
5349 ent->got.refcount -= 1;
5350 }
5351 break;
5352
5353 case R_PPC64_PLT16_HA:
5354 case R_PPC64_PLT16_HI:
5355 case R_PPC64_PLT16_LO:
5356 case R_PPC64_PLT32:
5357 case R_PPC64_PLT64:
5358 case R_PPC64_REL14:
5359 case R_PPC64_REL14_BRNTAKEN:
5360 case R_PPC64_REL14_BRTAKEN:
5361 case R_PPC64_REL24:
5362 if (h != NULL)
5363 {
5364 struct plt_entry *ent;
5365
5366 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5367 if (ent->addend == rel->r_addend)
5368 break;
5369 if (ent == NULL)
5370 abort ();
5371 if (ent->plt.refcount > 0)
5372 ent->plt.refcount -= 1;
5373 }
5374 break;
5375
5376 default:
5377 break;
5378 }
5379 }
5380 return TRUE;
5381 }
5382
5383 /* The maximum size of .sfpr. */
5384 #define SFPR_MAX (218*4)
5385
5386 struct sfpr_def_parms
5387 {
5388 const char name[12];
5389 unsigned char lo, hi;
5390 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5391 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5392 };
5393
5394 /* Auto-generate _save*, _rest* functions in .sfpr. */
5395
5396 static unsigned int
5397 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5398 {
5399 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5400 unsigned int i;
5401 size_t len = strlen (parm->name);
5402 bfd_boolean writing = FALSE;
5403 char sym[16];
5404
5405 memcpy (sym, parm->name, len);
5406 sym[len + 2] = 0;
5407
5408 for (i = parm->lo; i <= parm->hi; i++)
5409 {
5410 struct elf_link_hash_entry *h;
5411
5412 sym[len + 0] = i / 10 + '0';
5413 sym[len + 1] = i % 10 + '0';
5414 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5415 if (h != NULL
5416 && !h->def_regular)
5417 {
5418 h->root.type = bfd_link_hash_defined;
5419 h->root.u.def.section = htab->sfpr;
5420 h->root.u.def.value = htab->sfpr->size;
5421 h->type = STT_FUNC;
5422 h->def_regular = 1;
5423 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5424 writing = TRUE;
5425 if (htab->sfpr->contents == NULL)
5426 {
5427 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5428 if (htab->sfpr->contents == NULL)
5429 return FALSE;
5430 }
5431 }
5432 if (writing)
5433 {
5434 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5435 if (i != parm->hi)
5436 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5437 else
5438 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5439 htab->sfpr->size = p - htab->sfpr->contents;
5440 }
5441 }
5442
5443 return TRUE;
5444 }
5445
5446 static bfd_byte *
5447 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5448 {
5449 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5450 return p + 4;
5451 }
5452
5453 static bfd_byte *
5454 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5455 {
5456 p = savegpr0 (abfd, p, r);
5457 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5458 p = p + 4;
5459 bfd_put_32 (abfd, BLR, p);
5460 return p + 4;
5461 }
5462
5463 static bfd_byte *
5464 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5465 {
5466 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5467 return p + 4;
5468 }
5469
5470 static bfd_byte *
5471 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5472 {
5473 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5474 p = p + 4;
5475 p = restgpr0 (abfd, p, r);
5476 bfd_put_32 (abfd, MTLR_R0, p);
5477 p = p + 4;
5478 if (r == 29)
5479 {
5480 p = restgpr0 (abfd, p, 30);
5481 p = restgpr0 (abfd, p, 31);
5482 }
5483 bfd_put_32 (abfd, BLR, p);
5484 return p + 4;
5485 }
5486
5487 static bfd_byte *
5488 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5489 {
5490 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5491 return p + 4;
5492 }
5493
5494 static bfd_byte *
5495 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5496 {
5497 p = savegpr1 (abfd, p, r);
5498 bfd_put_32 (abfd, BLR, p);
5499 return p + 4;
5500 }
5501
5502 static bfd_byte *
5503 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5504 {
5505 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5506 return p + 4;
5507 }
5508
5509 static bfd_byte *
5510 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5511 {
5512 p = restgpr1 (abfd, p, r);
5513 bfd_put_32 (abfd, BLR, p);
5514 return p + 4;
5515 }
5516
5517 static bfd_byte *
5518 savefpr (bfd *abfd, bfd_byte *p, int r)
5519 {
5520 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5521 return p + 4;
5522 }
5523
5524 static bfd_byte *
5525 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5526 {
5527 p = savefpr (abfd, p, r);
5528 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5529 p = p + 4;
5530 bfd_put_32 (abfd, BLR, p);
5531 return p + 4;
5532 }
5533
5534 static bfd_byte *
5535 restfpr (bfd *abfd, bfd_byte *p, int r)
5536 {
5537 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5538 return p + 4;
5539 }
5540
5541 static bfd_byte *
5542 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5543 {
5544 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5545 p = p + 4;
5546 p = restfpr (abfd, p, r);
5547 bfd_put_32 (abfd, MTLR_R0, p);
5548 p = p + 4;
5549 if (r == 29)
5550 {
5551 p = restfpr (abfd, p, 30);
5552 p = restfpr (abfd, p, 31);
5553 }
5554 bfd_put_32 (abfd, BLR, p);
5555 return p + 4;
5556 }
5557
5558 static bfd_byte *
5559 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5560 {
5561 p = savefpr (abfd, p, r);
5562 bfd_put_32 (abfd, BLR, p);
5563 return p + 4;
5564 }
5565
5566 static bfd_byte *
5567 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5568 {
5569 p = restfpr (abfd, p, r);
5570 bfd_put_32 (abfd, BLR, p);
5571 return p + 4;
5572 }
5573
5574 static bfd_byte *
5575 savevr (bfd *abfd, bfd_byte *p, int r)
5576 {
5577 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5578 p = p + 4;
5579 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5580 return p + 4;
5581 }
5582
5583 static bfd_byte *
5584 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5585 {
5586 p = savevr (abfd, p, r);
5587 bfd_put_32 (abfd, BLR, p);
5588 return p + 4;
5589 }
5590
5591 static bfd_byte *
5592 restvr (bfd *abfd, bfd_byte *p, int r)
5593 {
5594 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5595 p = p + 4;
5596 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5597 return p + 4;
5598 }
5599
5600 static bfd_byte *
5601 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5602 {
5603 p = restvr (abfd, p, r);
5604 bfd_put_32 (abfd, BLR, p);
5605 return p + 4;
5606 }
5607
5608 /* Called via elf_link_hash_traverse to transfer dynamic linking
5609 information on function code symbol entries to their corresponding
5610 function descriptor symbol entries. */
5611
5612 static bfd_boolean
5613 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5614 {
5615 struct bfd_link_info *info;
5616 struct ppc_link_hash_table *htab;
5617 struct plt_entry *ent;
5618 struct ppc_link_hash_entry *fh;
5619 struct ppc_link_hash_entry *fdh;
5620 bfd_boolean force_local;
5621
5622 fh = (struct ppc_link_hash_entry *) h;
5623 if (fh->elf.root.type == bfd_link_hash_indirect)
5624 return TRUE;
5625
5626 if (fh->elf.root.type == bfd_link_hash_warning)
5627 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
5628
5629 info = inf;
5630 htab = ppc_hash_table (info);
5631
5632 /* Resolve undefined references to dot-symbols as the value
5633 in the function descriptor, if we have one in a regular object.
5634 This is to satisfy cases like ".quad .foo". Calls to functions
5635 in dynamic objects are handled elsewhere. */
5636 if (fh->elf.root.type == bfd_link_hash_undefweak
5637 && fh->was_undefined
5638 && (fh->oh->elf.root.type == bfd_link_hash_defined
5639 || fh->oh->elf.root.type == bfd_link_hash_defweak)
5640 && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5641 && opd_entry_value (fh->oh->elf.root.u.def.section,
5642 fh->oh->elf.root.u.def.value,
5643 &fh->elf.root.u.def.section,
5644 &fh->elf.root.u.def.value) != (bfd_vma) -1)
5645 {
5646 fh->elf.root.type = fh->oh->elf.root.type;
5647 fh->elf.forced_local = 1;
5648 }
5649
5650 /* If this is a function code symbol, transfer dynamic linking
5651 information to the function descriptor symbol. */
5652 if (!fh->is_func)
5653 return TRUE;
5654
5655 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5656 if (ent->plt.refcount > 0)
5657 break;
5658 if (ent == NULL
5659 || fh->elf.root.root.string[0] != '.'
5660 || fh->elf.root.root.string[1] == '\0')
5661 return TRUE;
5662
5663 /* Find the corresponding function descriptor symbol. Create it
5664 as undefined if necessary. */
5665
5666 fdh = get_fdh (fh, htab);
5667 if (fdh != NULL)
5668 while (fdh->elf.root.type == bfd_link_hash_indirect
5669 || fdh->elf.root.type == bfd_link_hash_warning)
5670 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5671
5672 if (fdh == NULL
5673 && info->shared
5674 && (fh->elf.root.type == bfd_link_hash_undefined
5675 || fh->elf.root.type == bfd_link_hash_undefweak))
5676 {
5677 fdh = make_fdh (info, fh);
5678 if (fdh == NULL)
5679 return FALSE;
5680 }
5681
5682 /* Fake function descriptors are made undefweak. If the function
5683 code symbol is strong undefined, make the fake sym the same.
5684 If the function code symbol is defined, then force the fake
5685 descriptor local; We can't support overriding of symbols in a
5686 shared library on a fake descriptor. */
5687
5688 if (fdh != NULL
5689 && fdh->fake
5690 && fdh->elf.root.type == bfd_link_hash_undefweak)
5691 {
5692 if (fh->elf.root.type == bfd_link_hash_undefined)
5693 {
5694 fdh->elf.root.type = bfd_link_hash_undefined;
5695 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5696 }
5697 else if (fh->elf.root.type == bfd_link_hash_defined
5698 || fh->elf.root.type == bfd_link_hash_defweak)
5699 {
5700 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5701 }
5702 }
5703
5704 if (fdh != NULL
5705 && !fdh->elf.forced_local
5706 && (info->shared
5707 || fdh->elf.def_dynamic
5708 || fdh->elf.ref_dynamic
5709 || (fdh->elf.root.type == bfd_link_hash_undefweak
5710 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5711 {
5712 if (fdh->elf.dynindx == -1)
5713 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5714 return FALSE;
5715 fdh->elf.ref_regular |= fh->elf.ref_regular;
5716 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5717 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5718 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5719 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
5720 {
5721 move_plt_plist (fh, fdh);
5722 fdh->elf.needs_plt = 1;
5723 }
5724 fdh->is_func_descriptor = 1;
5725 fdh->oh = fh;
5726 fh->oh = fdh;
5727 }
5728
5729 /* Now that the info is on the function descriptor, clear the
5730 function code sym info. Any function code syms for which we
5731 don't have a definition in a regular file, we force local.
5732 This prevents a shared library from exporting syms that have
5733 been imported from another library. Function code syms that
5734 are really in the library we must leave global to prevent the
5735 linker dragging in a definition from a static library. */
5736 force_local = (!fh->elf.def_regular
5737 || fdh == NULL
5738 || !fdh->elf.def_regular
5739 || fdh->elf.forced_local);
5740 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5741
5742 return TRUE;
5743 }
5744
5745 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5746 this hook to a) provide some gcc support functions, and b) transfer
5747 dynamic linking information gathered so far on function code symbol
5748 entries, to their corresponding function descriptor symbol entries. */
5749
5750 static bfd_boolean
5751 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5752 struct bfd_link_info *info)
5753 {
5754 struct ppc_link_hash_table *htab;
5755 unsigned int i;
5756 const struct sfpr_def_parms funcs[] =
5757 {
5758 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5759 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5760 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5761 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5762 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5763 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5764 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5765 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5766 { "._savef", 14, 31, savefpr, savefpr1_tail },
5767 { "._restf", 14, 31, restfpr, restfpr1_tail },
5768 { "_savevr_", 20, 31, savevr, savevr_tail },
5769 { "_restvr_", 20, 31, restvr, restvr_tail }
5770 };
5771
5772 htab = ppc_hash_table (info);
5773 if (htab->sfpr == NULL)
5774 /* We don't have any relocs. */
5775 return TRUE;
5776
5777 /* Provide any missing _save* and _rest* functions. */
5778 htab->sfpr->size = 0;
5779 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5780 if (!sfpr_define (info, &funcs[i]))
5781 return FALSE;
5782
5783 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5784
5785 if (htab->sfpr->size == 0)
5786 htab->sfpr->flags |= SEC_EXCLUDE;
5787
5788 return TRUE;
5789 }
5790
5791 /* Adjust a symbol defined by a dynamic object and referenced by a
5792 regular object. The current definition is in some section of the
5793 dynamic object, but we're not including those sections. We have to
5794 change the definition to something the rest of the link can
5795 understand. */
5796
5797 static bfd_boolean
5798 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5799 struct elf_link_hash_entry *h)
5800 {
5801 struct ppc_link_hash_table *htab;
5802 asection *s;
5803
5804 htab = ppc_hash_table (info);
5805
5806 /* Deal with function syms. */
5807 if (h->type == STT_FUNC
5808 || h->needs_plt)
5809 {
5810 /* Clear procedure linkage table information for any symbol that
5811 won't need a .plt entry. */
5812 struct plt_entry *ent;
5813 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5814 if (ent->plt.refcount > 0)
5815 break;
5816 if (ent == NULL
5817 || SYMBOL_CALLS_LOCAL (info, h)
5818 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5819 && h->root.type == bfd_link_hash_undefweak))
5820 {
5821 h->plt.plist = NULL;
5822 h->needs_plt = 0;
5823 }
5824 }
5825 else
5826 h->plt.plist = NULL;
5827
5828 /* If this is a weak symbol, and there is a real definition, the
5829 processor independent code will have arranged for us to see the
5830 real definition first, and we can just use the same value. */
5831 if (h->u.weakdef != NULL)
5832 {
5833 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5834 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5835 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5836 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5837 if (ELIMINATE_COPY_RELOCS)
5838 h->non_got_ref = h->u.weakdef->non_got_ref;
5839 return TRUE;
5840 }
5841
5842 /* If we are creating a shared library, we must presume that the
5843 only references to the symbol are via the global offset table.
5844 For such cases we need not do anything here; the relocations will
5845 be handled correctly by relocate_section. */
5846 if (info->shared)
5847 return TRUE;
5848
5849 /* If there are no references to this symbol that do not use the
5850 GOT, we don't need to generate a copy reloc. */
5851 if (!h->non_got_ref)
5852 return TRUE;
5853
5854 /* Don't generate a copy reloc for symbols defined in the executable. */
5855 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
5856 return TRUE;
5857
5858 if (ELIMINATE_COPY_RELOCS)
5859 {
5860 struct ppc_link_hash_entry * eh;
5861 struct ppc_dyn_relocs *p;
5862
5863 eh = (struct ppc_link_hash_entry *) h;
5864 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5865 {
5866 s = p->sec->output_section;
5867 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5868 break;
5869 }
5870
5871 /* If we didn't find any dynamic relocs in read-only sections, then
5872 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5873 if (p == NULL)
5874 {
5875 h->non_got_ref = 0;
5876 return TRUE;
5877 }
5878 }
5879
5880 if (h->plt.plist != NULL)
5881 {
5882 /* We should never get here, but unfortunately there are versions
5883 of gcc out there that improperly (for this ABI) put initialized
5884 function pointers, vtable refs and suchlike in read-only
5885 sections. Allow them to proceed, but warn that this might
5886 break at runtime. */
5887 (*_bfd_error_handler)
5888 (_("copy reloc against `%s' requires lazy plt linking; "
5889 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5890 h->root.root.string);
5891 }
5892
5893 /* This is a reference to a symbol defined by a dynamic object which
5894 is not a function. */
5895
5896 if (h->size == 0)
5897 {
5898 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5899 h->root.root.string);
5900 return TRUE;
5901 }
5902
5903 /* We must allocate the symbol in our .dynbss section, which will
5904 become part of the .bss section of the executable. There will be
5905 an entry for this symbol in the .dynsym section. The dynamic
5906 object will contain position independent code, so all references
5907 from the dynamic object to this symbol will go through the global
5908 offset table. The dynamic linker will use the .dynsym entry to
5909 determine the address it must put in the global offset table, so
5910 both the dynamic object and the regular object will refer to the
5911 same memory location for the variable. */
5912
5913 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5914 to copy the initial value out of the dynamic object and into the
5915 runtime process image. We need to remember the offset into the
5916 .rela.bss section we are going to use. */
5917 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5918 {
5919 htab->relbss->size += sizeof (Elf64_External_Rela);
5920 h->needs_copy = 1;
5921 }
5922
5923 s = htab->dynbss;
5924
5925 return _bfd_elf_adjust_dynamic_copy (h, s);
5926 }
5927
5928 /* If given a function descriptor symbol, hide both the function code
5929 sym and the descriptor. */
5930 static void
5931 ppc64_elf_hide_symbol (struct bfd_link_info *info,
5932 struct elf_link_hash_entry *h,
5933 bfd_boolean force_local)
5934 {
5935 struct ppc_link_hash_entry *eh;
5936 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5937
5938 eh = (struct ppc_link_hash_entry *) h;
5939 if (eh->is_func_descriptor)
5940 {
5941 struct ppc_link_hash_entry *fh = eh->oh;
5942
5943 if (fh == NULL)
5944 {
5945 const char *p, *q;
5946 struct ppc_link_hash_table *htab;
5947 char save;
5948
5949 /* We aren't supposed to use alloca in BFD because on
5950 systems which do not have alloca the version in libiberty
5951 calls xmalloc, which might cause the program to crash
5952 when it runs out of memory. This function doesn't have a
5953 return status, so there's no way to gracefully return an
5954 error. So cheat. We know that string[-1] can be safely
5955 accessed; It's either a string in an ELF string table,
5956 or allocated in an objalloc structure. */
5957
5958 p = eh->elf.root.root.string - 1;
5959 save = *p;
5960 *(char *) p = '.';
5961 htab = ppc_hash_table (info);
5962 fh = (struct ppc_link_hash_entry *)
5963 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5964 *(char *) p = save;
5965
5966 /* Unfortunately, if it so happens that the string we were
5967 looking for was allocated immediately before this string,
5968 then we overwrote the string terminator. That's the only
5969 reason the lookup should fail. */
5970 if (fh == NULL)
5971 {
5972 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5973 while (q >= eh->elf.root.root.string && *q == *p)
5974 --q, --p;
5975 if (q < eh->elf.root.root.string && *p == '.')
5976 fh = (struct ppc_link_hash_entry *)
5977 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5978 }
5979 if (fh != NULL)
5980 {
5981 eh->oh = fh;
5982 fh->oh = eh;
5983 }
5984 }
5985 if (fh != NULL)
5986 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5987 }
5988 }
5989
5990 static bfd_boolean
5991 get_sym_h (struct elf_link_hash_entry **hp,
5992 Elf_Internal_Sym **symp,
5993 asection **symsecp,
5994 char **tls_maskp,
5995 Elf_Internal_Sym **locsymsp,
5996 unsigned long r_symndx,
5997 bfd *ibfd)
5998 {
5999 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6000
6001 if (r_symndx >= symtab_hdr->sh_info)
6002 {
6003 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6004 struct elf_link_hash_entry *h;
6005
6006 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6007 while (h->root.type == bfd_link_hash_indirect
6008 || h->root.type == bfd_link_hash_warning)
6009 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6010
6011 if (hp != NULL)
6012 *hp = h;
6013
6014 if (symp != NULL)
6015 *symp = NULL;
6016
6017 if (symsecp != NULL)
6018 {
6019 asection *symsec = NULL;
6020 if (h->root.type == bfd_link_hash_defined
6021 || h->root.type == bfd_link_hash_defweak)
6022 symsec = h->root.u.def.section;
6023 *symsecp = symsec;
6024 }
6025
6026 if (tls_maskp != NULL)
6027 {
6028 struct ppc_link_hash_entry *eh;
6029
6030 eh = (struct ppc_link_hash_entry *) h;
6031 *tls_maskp = &eh->tls_mask;
6032 }
6033 }
6034 else
6035 {
6036 Elf_Internal_Sym *sym;
6037 Elf_Internal_Sym *locsyms = *locsymsp;
6038
6039 if (locsyms == NULL)
6040 {
6041 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6042 if (locsyms == NULL)
6043 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6044 symtab_hdr->sh_info,
6045 0, NULL, NULL, NULL);
6046 if (locsyms == NULL)
6047 return FALSE;
6048 *locsymsp = locsyms;
6049 }
6050 sym = locsyms + r_symndx;
6051
6052 if (hp != NULL)
6053 *hp = NULL;
6054
6055 if (symp != NULL)
6056 *symp = sym;
6057
6058 if (symsecp != NULL)
6059 {
6060 asection *symsec = NULL;
6061 if ((sym->st_shndx != SHN_UNDEF
6062 && sym->st_shndx < SHN_LORESERVE)
6063 || sym->st_shndx > SHN_HIRESERVE)
6064 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6065 *symsecp = symsec;
6066 }
6067
6068 if (tls_maskp != NULL)
6069 {
6070 struct got_entry **lgot_ents;
6071 char *tls_mask;
6072
6073 tls_mask = NULL;
6074 lgot_ents = elf_local_got_ents (ibfd);
6075 if (lgot_ents != NULL)
6076 {
6077 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
6078 tls_mask = &lgot_masks[r_symndx];
6079 }
6080 *tls_maskp = tls_mask;
6081 }
6082 }
6083 return TRUE;
6084 }
6085
6086 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6087 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6088 type suitable for optimization, and 1 otherwise. */
6089
6090 static int
6091 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
6092 Elf_Internal_Sym **locsymsp,
6093 const Elf_Internal_Rela *rel, bfd *ibfd)
6094 {
6095 unsigned long r_symndx;
6096 int next_r;
6097 struct elf_link_hash_entry *h;
6098 Elf_Internal_Sym *sym;
6099 asection *sec;
6100 bfd_vma off;
6101
6102 r_symndx = ELF64_R_SYM (rel->r_info);
6103 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6104 return 0;
6105
6106 if ((*tls_maskp != NULL && **tls_maskp != 0)
6107 || sec == NULL
6108 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6109 return 1;
6110
6111 /* Look inside a TOC section too. */
6112 if (h != NULL)
6113 {
6114 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6115 off = h->root.u.def.value;
6116 }
6117 else
6118 off = sym->st_value;
6119 off += rel->r_addend;
6120 BFD_ASSERT (off % 8 == 0);
6121 r_symndx = ppc64_elf_section_data (sec)->u.t_symndx[off / 8];
6122 next_r = ppc64_elf_section_data (sec)->u.t_symndx[off / 8 + 1];
6123 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6124 return 0;
6125 if (toc_symndx != NULL)
6126 *toc_symndx = r_symndx;
6127 if ((h == NULL
6128 || ((h->root.type == bfd_link_hash_defined
6129 || h->root.type == bfd_link_hash_defweak)
6130 && !h->def_dynamic))
6131 && (next_r == -1 || next_r == -2))
6132 return 1 - next_r;
6133 return 1;
6134 }
6135
6136 /* Adjust all global syms defined in opd sections. In gcc generated
6137 code for the old ABI, these will already have been done. */
6138
6139 static bfd_boolean
6140 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6141 {
6142 struct ppc_link_hash_entry *eh;
6143 asection *sym_sec;
6144 long *opd_adjust;
6145
6146 if (h->root.type == bfd_link_hash_indirect)
6147 return TRUE;
6148
6149 if (h->root.type == bfd_link_hash_warning)
6150 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6151
6152 if (h->root.type != bfd_link_hash_defined
6153 && h->root.type != bfd_link_hash_defweak)
6154 return TRUE;
6155
6156 eh = (struct ppc_link_hash_entry *) h;
6157 if (eh->adjust_done)
6158 return TRUE;
6159
6160 sym_sec = eh->elf.root.u.def.section;
6161 opd_adjust = get_opd_info (sym_sec);
6162 if (opd_adjust != NULL)
6163 {
6164 long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
6165 if (adjust == -1)
6166 {
6167 /* This entry has been deleted. */
6168 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6169 if (dsec == NULL)
6170 {
6171 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6172 if (elf_discarded_section (dsec))
6173 {
6174 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6175 break;
6176 }
6177 }
6178 eh->elf.root.u.def.value = 0;
6179 eh->elf.root.u.def.section = dsec;
6180 }
6181 else
6182 eh->elf.root.u.def.value += adjust;
6183 eh->adjust_done = 1;
6184 }
6185 return TRUE;
6186 }
6187
6188 /* Handles decrementing dynamic reloc counts for the reloc specified by
6189 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6190 have already been determined. */
6191
6192 static bfd_boolean
6193 dec_dynrel_count (bfd_vma r_info,
6194 asection *sec,
6195 struct bfd_link_info *info,
6196 Elf_Internal_Sym **local_syms,
6197 struct elf_link_hash_entry *h,
6198 asection *sym_sec)
6199 {
6200 enum elf_ppc64_reloc_type r_type;
6201 struct ppc_dyn_relocs *p;
6202 struct ppc_dyn_relocs **pp;
6203
6204 /* Can this reloc be dynamic? This switch, and later tests here
6205 should be kept in sync with the code in check_relocs. */
6206 r_type = ELF64_R_TYPE (r_info);
6207 switch (r_type)
6208 {
6209 default:
6210 return TRUE;
6211
6212 case R_PPC64_TPREL16:
6213 case R_PPC64_TPREL16_LO:
6214 case R_PPC64_TPREL16_HI:
6215 case R_PPC64_TPREL16_HA:
6216 case R_PPC64_TPREL16_DS:
6217 case R_PPC64_TPREL16_LO_DS:
6218 case R_PPC64_TPREL16_HIGHER:
6219 case R_PPC64_TPREL16_HIGHERA:
6220 case R_PPC64_TPREL16_HIGHEST:
6221 case R_PPC64_TPREL16_HIGHESTA:
6222 if (!info->shared)
6223 return TRUE;
6224
6225 case R_PPC64_TPREL64:
6226 case R_PPC64_DTPMOD64:
6227 case R_PPC64_DTPREL64:
6228 case R_PPC64_ADDR64:
6229 case R_PPC64_REL30:
6230 case R_PPC64_REL32:
6231 case R_PPC64_REL64:
6232 case R_PPC64_ADDR14:
6233 case R_PPC64_ADDR14_BRNTAKEN:
6234 case R_PPC64_ADDR14_BRTAKEN:
6235 case R_PPC64_ADDR16:
6236 case R_PPC64_ADDR16_DS:
6237 case R_PPC64_ADDR16_HA:
6238 case R_PPC64_ADDR16_HI:
6239 case R_PPC64_ADDR16_HIGHER:
6240 case R_PPC64_ADDR16_HIGHERA:
6241 case R_PPC64_ADDR16_HIGHEST:
6242 case R_PPC64_ADDR16_HIGHESTA:
6243 case R_PPC64_ADDR16_LO:
6244 case R_PPC64_ADDR16_LO_DS:
6245 case R_PPC64_ADDR24:
6246 case R_PPC64_ADDR32:
6247 case R_PPC64_UADDR16:
6248 case R_PPC64_UADDR32:
6249 case R_PPC64_UADDR64:
6250 case R_PPC64_TOC:
6251 break;
6252 }
6253
6254 if (local_syms != NULL)
6255 {
6256 unsigned long r_symndx;
6257 Elf_Internal_Sym *sym;
6258 bfd *ibfd = sec->owner;
6259
6260 r_symndx = ELF64_R_SYM (r_info);
6261 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6262 return FALSE;
6263 }
6264
6265 if ((info->shared
6266 && (MUST_BE_DYN_RELOC (r_type)
6267 || (h != NULL
6268 && (!info->symbolic
6269 || h->root.type == bfd_link_hash_defweak
6270 || !h->def_regular))))
6271 || (ELIMINATE_COPY_RELOCS
6272 && !info->shared
6273 && h != NULL
6274 && (h->root.type == bfd_link_hash_defweak
6275 || !h->def_regular)))
6276 ;
6277 else
6278 return TRUE;
6279
6280 if (h != NULL)
6281 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6282 else
6283 {
6284 if (sym_sec != NULL)
6285 {
6286 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6287 pp = (struct ppc_dyn_relocs **) vpp;
6288 }
6289 else
6290 {
6291 void *vpp = &elf_section_data (sec)->local_dynrel;
6292 pp = (struct ppc_dyn_relocs **) vpp;
6293 }
6294
6295 /* elf_gc_sweep may have already removed all dyn relocs associated
6296 with local syms for a given section. Don't report a dynreloc
6297 miscount. */
6298 if (*pp == NULL)
6299 return TRUE;
6300 }
6301
6302 while ((p = *pp) != NULL)
6303 {
6304 if (p->sec == sec)
6305 {
6306 if (!MUST_BE_DYN_RELOC (r_type))
6307 p->pc_count -= 1;
6308 p->count -= 1;
6309 if (p->count == 0)
6310 *pp = p->next;
6311 return TRUE;
6312 }
6313 pp = &p->next;
6314 }
6315
6316 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6317 sec->owner, sec);
6318 bfd_set_error (bfd_error_bad_value);
6319 return FALSE;
6320 }
6321
6322 /* Remove unused Official Procedure Descriptor entries. Currently we
6323 only remove those associated with functions in discarded link-once
6324 sections, or weakly defined functions that have been overridden. It
6325 would be possible to remove many more entries for statically linked
6326 applications. */
6327
6328 bfd_boolean
6329 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
6330 bfd_boolean no_opd_opt,
6331 bfd_boolean non_overlapping)
6332 {
6333 bfd *ibfd;
6334 bfd_boolean some_edited = FALSE;
6335 asection *need_pad = NULL;
6336
6337 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6338 {
6339 asection *sec;
6340 Elf_Internal_Rela *relstart, *rel, *relend;
6341 Elf_Internal_Shdr *symtab_hdr;
6342 Elf_Internal_Sym *local_syms;
6343 struct elf_link_hash_entry **sym_hashes;
6344 bfd_vma offset;
6345 bfd_size_type amt;
6346 long *opd_adjust;
6347 bfd_boolean need_edit, add_aux_fields;
6348 bfd_size_type cnt_16b = 0;
6349
6350 sec = bfd_get_section_by_name (ibfd, ".opd");
6351 if (sec == NULL || sec->size == 0)
6352 continue;
6353
6354 amt = sec->size * sizeof (long) / 8;
6355 opd_adjust = get_opd_info (sec);
6356 if (opd_adjust == NULL)
6357 {
6358 /* check_relocs hasn't been called. Must be a ld -r link
6359 or --just-symbols object. */
6360 opd_adjust = bfd_alloc (obfd, amt);
6361 if (opd_adjust == NULL)
6362 return FALSE;
6363 ppc64_elf_section_data (sec)->u.opd_adjust = opd_adjust;
6364 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
6365 ppc64_elf_section_data (sec)->sec_type = sec_opd;
6366 }
6367 memset (opd_adjust, 0, amt);
6368
6369 if (no_opd_opt)
6370 continue;
6371
6372 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6373 continue;
6374
6375 if (sec->output_section == bfd_abs_section_ptr)
6376 continue;
6377
6378 /* Look through the section relocs. */
6379 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6380 continue;
6381
6382 local_syms = NULL;
6383 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6384 sym_hashes = elf_sym_hashes (ibfd);
6385
6386 /* Read the relocations. */
6387 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6388 info->keep_memory);
6389 if (relstart == NULL)
6390 return FALSE;
6391
6392 /* First run through the relocs to check they are sane, and to
6393 determine whether we need to edit this opd section. */
6394 need_edit = FALSE;
6395 need_pad = sec;
6396 offset = 0;
6397 relend = relstart + sec->reloc_count;
6398 for (rel = relstart; rel < relend; )
6399 {
6400 enum elf_ppc64_reloc_type r_type;
6401 unsigned long r_symndx;
6402 asection *sym_sec;
6403 struct elf_link_hash_entry *h;
6404 Elf_Internal_Sym *sym;
6405
6406 /* .opd contains a regular array of 16 or 24 byte entries. We're
6407 only interested in the reloc pointing to a function entry
6408 point. */
6409 if (rel->r_offset != offset
6410 || rel + 1 >= relend
6411 || (rel + 1)->r_offset != offset + 8)
6412 {
6413 /* If someone messes with .opd alignment then after a
6414 "ld -r" we might have padding in the middle of .opd.
6415 Also, there's nothing to prevent someone putting
6416 something silly in .opd with the assembler. No .opd
6417 optimization for them! */
6418 broken_opd:
6419 (*_bfd_error_handler)
6420 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6421 need_edit = FALSE;
6422 break;
6423 }
6424
6425 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6426 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6427 {
6428 (*_bfd_error_handler)
6429 (_("%B: unexpected reloc type %u in .opd section"),
6430 ibfd, r_type);
6431 need_edit = FALSE;
6432 break;
6433 }
6434
6435 r_symndx = ELF64_R_SYM (rel->r_info);
6436 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6437 r_symndx, ibfd))
6438 goto error_ret;
6439
6440 if (sym_sec == NULL || sym_sec->owner == NULL)
6441 {
6442 const char *sym_name;
6443 if (h != NULL)
6444 sym_name = h->root.root.string;
6445 else
6446 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6447 sym_sec);
6448
6449 (*_bfd_error_handler)
6450 (_("%B: undefined sym `%s' in .opd section"),
6451 ibfd, sym_name);
6452 need_edit = FALSE;
6453 break;
6454 }
6455
6456 /* opd entries are always for functions defined in the
6457 current input bfd. If the symbol isn't defined in the
6458 input bfd, then we won't be using the function in this
6459 bfd; It must be defined in a linkonce section in another
6460 bfd, or is weak. It's also possible that we are
6461 discarding the function due to a linker script /DISCARD/,
6462 which we test for via the output_section. */
6463 if (sym_sec->owner != ibfd
6464 || sym_sec->output_section == bfd_abs_section_ptr)
6465 need_edit = TRUE;
6466
6467 rel += 2;
6468 if (rel == relend
6469 || (rel + 1 == relend && rel->r_offset == offset + 16))
6470 {
6471 if (sec->size == offset + 24)
6472 {
6473 need_pad = NULL;
6474 break;
6475 }
6476 if (rel == relend && sec->size == offset + 16)
6477 {
6478 cnt_16b++;
6479 break;
6480 }
6481 goto broken_opd;
6482 }
6483
6484 if (rel->r_offset == offset + 24)
6485 offset += 24;
6486 else if (rel->r_offset != offset + 16)
6487 goto broken_opd;
6488 else if (rel + 1 < relend
6489 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6490 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6491 {
6492 offset += 16;
6493 cnt_16b++;
6494 }
6495 else if (rel + 2 < relend
6496 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6497 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6498 {
6499 offset += 24;
6500 rel += 1;
6501 }
6502 else
6503 goto broken_opd;
6504 }
6505
6506 add_aux_fields = non_overlapping && cnt_16b > 0;
6507
6508 if (need_edit || add_aux_fields)
6509 {
6510 Elf_Internal_Rela *write_rel;
6511 bfd_byte *rptr, *wptr;
6512 bfd_byte *new_contents = NULL;
6513 bfd_boolean skip;
6514 long opd_ent_size;
6515
6516 /* This seems a waste of time as input .opd sections are all
6517 zeros as generated by gcc, but I suppose there's no reason
6518 this will always be so. We might start putting something in
6519 the third word of .opd entries. */
6520 if ((sec->flags & SEC_IN_MEMORY) == 0)
6521 {
6522 bfd_byte *loc;
6523 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6524 {
6525 if (loc != NULL)
6526 free (loc);
6527 error_ret:
6528 if (local_syms != NULL
6529 && symtab_hdr->contents != (unsigned char *) local_syms)
6530 free (local_syms);
6531 if (elf_section_data (sec)->relocs != relstart)
6532 free (relstart);
6533 return FALSE;
6534 }
6535 sec->contents = loc;
6536 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6537 }
6538
6539 elf_section_data (sec)->relocs = relstart;
6540
6541 new_contents = sec->contents;
6542 if (add_aux_fields)
6543 {
6544 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6545 if (new_contents == NULL)
6546 return FALSE;
6547 need_pad = FALSE;
6548 }
6549 wptr = new_contents;
6550 rptr = sec->contents;
6551
6552 write_rel = relstart;
6553 skip = FALSE;
6554 offset = 0;
6555 opd_ent_size = 0;
6556 for (rel = relstart; rel < relend; rel++)
6557 {
6558 unsigned long r_symndx;
6559 asection *sym_sec;
6560 struct elf_link_hash_entry *h;
6561 Elf_Internal_Sym *sym;
6562
6563 r_symndx = ELF64_R_SYM (rel->r_info);
6564 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6565 r_symndx, ibfd))
6566 goto error_ret;
6567
6568 if (rel->r_offset == offset)
6569 {
6570 struct ppc_link_hash_entry *fdh = NULL;
6571
6572 /* See if the .opd entry is full 24 byte or
6573 16 byte (with fd_aux entry overlapped with next
6574 fd_func). */
6575 opd_ent_size = 24;
6576 if ((rel + 2 == relend && sec->size == offset + 16)
6577 || (rel + 3 < relend
6578 && rel[2].r_offset == offset + 16
6579 && rel[3].r_offset == offset + 24
6580 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6581 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6582 opd_ent_size = 16;
6583
6584 if (h != NULL
6585 && h->root.root.string[0] == '.')
6586 {
6587 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6588 ppc_hash_table (info));
6589 if (fdh != NULL
6590 && fdh->elf.root.type != bfd_link_hash_defined
6591 && fdh->elf.root.type != bfd_link_hash_defweak)
6592 fdh = NULL;
6593 }
6594
6595 skip = (sym_sec->owner != ibfd
6596 || sym_sec->output_section == bfd_abs_section_ptr);
6597 if (skip)
6598 {
6599 if (fdh != NULL && sym_sec->owner == ibfd)
6600 {
6601 /* Arrange for the function descriptor sym
6602 to be dropped. */
6603 fdh->elf.root.u.def.value = 0;
6604 fdh->elf.root.u.def.section = sym_sec;
6605 }
6606 opd_adjust[rel->r_offset / 8] = -1;
6607 }
6608 else
6609 {
6610 /* We'll be keeping this opd entry. */
6611
6612 if (fdh != NULL)
6613 {
6614 /* Redefine the function descriptor symbol to
6615 this location in the opd section. It is
6616 necessary to update the value here rather
6617 than using an array of adjustments as we do
6618 for local symbols, because various places
6619 in the generic ELF code use the value
6620 stored in u.def.value. */
6621 fdh->elf.root.u.def.value = wptr - new_contents;
6622 fdh->adjust_done = 1;
6623 }
6624
6625 /* Local syms are a bit tricky. We could
6626 tweak them as they can be cached, but
6627 we'd need to look through the local syms
6628 for the function descriptor sym which we
6629 don't have at the moment. So keep an
6630 array of adjustments. */
6631 opd_adjust[rel->r_offset / 8]
6632 = (wptr - new_contents) - (rptr - sec->contents);
6633
6634 if (wptr != rptr)
6635 memcpy (wptr, rptr, opd_ent_size);
6636 wptr += opd_ent_size;
6637 if (add_aux_fields && opd_ent_size == 16)
6638 {
6639 memset (wptr, '\0', 8);
6640 wptr += 8;
6641 }
6642 }
6643 rptr += opd_ent_size;
6644 offset += opd_ent_size;
6645 }
6646
6647 if (skip)
6648 {
6649 if (!NO_OPD_RELOCS
6650 && !info->relocatable
6651 && !dec_dynrel_count (rel->r_info, sec, info,
6652 NULL, h, sym_sec))
6653 goto error_ret;
6654 }
6655 else
6656 {
6657 /* We need to adjust any reloc offsets to point to the
6658 new opd entries. While we're at it, we may as well
6659 remove redundant relocs. */
6660 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
6661 if (write_rel != rel)
6662 memcpy (write_rel, rel, sizeof (*rel));
6663 ++write_rel;
6664 }
6665 }
6666
6667 sec->size = wptr - new_contents;
6668 sec->reloc_count = write_rel - relstart;
6669 if (add_aux_fields)
6670 {
6671 free (sec->contents);
6672 sec->contents = new_contents;
6673 }
6674
6675 /* Fudge the header size too, as this is used later in
6676 elf_bfd_final_link if we are emitting relocs. */
6677 elf_section_data (sec)->rel_hdr.sh_size
6678 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6679 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
6680 some_edited = TRUE;
6681 }
6682 else if (elf_section_data (sec)->relocs != relstart)
6683 free (relstart);
6684
6685 if (local_syms != NULL
6686 && symtab_hdr->contents != (unsigned char *) local_syms)
6687 {
6688 if (!info->keep_memory)
6689 free (local_syms);
6690 else
6691 symtab_hdr->contents = (unsigned char *) local_syms;
6692 }
6693 }
6694
6695 if (some_edited)
6696 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6697
6698 /* If we are doing a final link and the last .opd entry is just 16 byte
6699 long, add a 8 byte padding after it. */
6700 if (need_pad != NULL && !info->relocatable)
6701 {
6702 bfd_byte *p;
6703
6704 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6705 {
6706 BFD_ASSERT (need_pad->size > 0);
6707
6708 p = bfd_malloc (need_pad->size + 8);
6709 if (p == NULL)
6710 return FALSE;
6711
6712 if (! bfd_get_section_contents (need_pad->owner, need_pad,
6713 p, 0, need_pad->size))
6714 return FALSE;
6715
6716 need_pad->contents = p;
6717 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6718 }
6719 else
6720 {
6721 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6722 if (p == NULL)
6723 return FALSE;
6724
6725 need_pad->contents = p;
6726 }
6727
6728 memset (need_pad->contents + need_pad->size, 0, 8);
6729 need_pad->size += 8;
6730 }
6731
6732 return TRUE;
6733 }
6734
6735 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
6736
6737 asection *
6738 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
6739 {
6740 struct ppc_link_hash_table *htab;
6741
6742 htab = ppc_hash_table (info);
6743 if (htab->tls_get_addr != NULL)
6744 {
6745 struct ppc_link_hash_entry *h = htab->tls_get_addr;
6746
6747 while (h->elf.root.type == bfd_link_hash_indirect
6748 || h->elf.root.type == bfd_link_hash_warning)
6749 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6750
6751 htab->tls_get_addr = h;
6752
6753 if (htab->tls_get_addr_fd == NULL
6754 && h->oh != NULL
6755 && h->oh->is_func_descriptor
6756 && (h->oh->elf.root.type == bfd_link_hash_defined
6757 || h->oh->elf.root.type == bfd_link_hash_defweak))
6758 htab->tls_get_addr_fd = h->oh;
6759 }
6760
6761 if (htab->tls_get_addr_fd != NULL)
6762 {
6763 struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6764
6765 while (h->elf.root.type == bfd_link_hash_indirect
6766 || h->elf.root.type == bfd_link_hash_warning)
6767 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6768
6769 htab->tls_get_addr_fd = h;
6770 }
6771
6772 return _bfd_elf_tls_setup (obfd, info);
6773 }
6774
6775 /* Run through all the TLS relocs looking for optimization
6776 opportunities. The linker has been hacked (see ppc64elf.em) to do
6777 a preliminary section layout so that we know the TLS segment
6778 offsets. We can't optimize earlier because some optimizations need
6779 to know the tp offset, and we need to optimize before allocating
6780 dynamic relocations. */
6781
6782 bfd_boolean
6783 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6784 {
6785 bfd *ibfd;
6786 asection *sec;
6787 struct ppc_link_hash_table *htab;
6788
6789 if (info->relocatable || info->shared)
6790 return TRUE;
6791
6792 htab = ppc_hash_table (info);
6793 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6794 {
6795 Elf_Internal_Sym *locsyms = NULL;
6796 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
6797 unsigned char *toc_ref = NULL;
6798
6799 /* Look at all the sections for this file, with TOC last. */
6800 for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
6801 : ibfd->sections);
6802 sec != NULL;
6803 sec = (sec == toc ? NULL
6804 : sec->next == NULL ? toc
6805 : sec->next == toc && toc->next ? toc->next
6806 : sec->next))
6807 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6808 {
6809 Elf_Internal_Rela *relstart, *rel, *relend;
6810 int expecting_tls_get_addr;
6811 long toc_ref_index = 0;
6812
6813 /* Read the relocations. */
6814 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6815 info->keep_memory);
6816 if (relstart == NULL)
6817 return FALSE;
6818
6819 expecting_tls_get_addr = 0;
6820 relend = relstart + sec->reloc_count;
6821 for (rel = relstart; rel < relend; rel++)
6822 {
6823 enum elf_ppc64_reloc_type r_type;
6824 unsigned long r_symndx;
6825 struct elf_link_hash_entry *h;
6826 Elf_Internal_Sym *sym;
6827 asection *sym_sec;
6828 char *tls_mask;
6829 char tls_set, tls_clear, tls_type = 0;
6830 bfd_vma value;
6831 bfd_boolean ok_tprel, is_local;
6832
6833 r_symndx = ELF64_R_SYM (rel->r_info);
6834 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
6835 r_symndx, ibfd))
6836 {
6837 err_free_rel:
6838 if (elf_section_data (sec)->relocs != relstart)
6839 free (relstart);
6840 if (toc_ref != NULL)
6841 free (toc_ref);
6842 if (locsyms != NULL
6843 && (elf_tdata (ibfd)->symtab_hdr.contents
6844 != (unsigned char *) locsyms))
6845 free (locsyms);
6846 return FALSE;
6847 }
6848
6849 if (h != NULL)
6850 {
6851 if (h->root.type != bfd_link_hash_defined
6852 && h->root.type != bfd_link_hash_defweak)
6853 continue;
6854 value = h->root.u.def.value;
6855 }
6856 else
6857 /* Symbols referenced by TLS relocs must be of type
6858 STT_TLS. So no need for .opd local sym adjust. */
6859 value = sym->st_value;
6860
6861 ok_tprel = FALSE;
6862 is_local = FALSE;
6863 if (h == NULL
6864 || !h->def_dynamic)
6865 {
6866 is_local = TRUE;
6867 value += sym_sec->output_offset;
6868 value += sym_sec->output_section->vma;
6869 value -= htab->elf.tls_sec->vma;
6870 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6871 < (bfd_vma) 1 << 32);
6872 }
6873
6874 r_type = ELF64_R_TYPE (rel->r_info);
6875 switch (r_type)
6876 {
6877 case R_PPC64_GOT_TLSLD16:
6878 case R_PPC64_GOT_TLSLD16_LO:
6879 case R_PPC64_GOT_TLSLD16_HI:
6880 case R_PPC64_GOT_TLSLD16_HA:
6881 /* These relocs should never be against a symbol
6882 defined in a shared lib. Leave them alone if
6883 that turns out to be the case. */
6884 ppc64_tlsld_got (ibfd)->refcount -= 1;
6885 if (!is_local)
6886 continue;
6887
6888 /* LD -> LE */
6889 tls_set = 0;
6890 tls_clear = TLS_LD;
6891 tls_type = TLS_TLS | TLS_LD;
6892 expecting_tls_get_addr = 1;
6893 break;
6894
6895 case R_PPC64_GOT_TLSGD16:
6896 case R_PPC64_GOT_TLSGD16_LO:
6897 case R_PPC64_GOT_TLSGD16_HI:
6898 case R_PPC64_GOT_TLSGD16_HA:
6899 if (ok_tprel)
6900 /* GD -> LE */
6901 tls_set = 0;
6902 else
6903 /* GD -> IE */
6904 tls_set = TLS_TLS | TLS_TPRELGD;
6905 tls_clear = TLS_GD;
6906 tls_type = TLS_TLS | TLS_GD;
6907 expecting_tls_get_addr = 1;
6908 break;
6909
6910 case R_PPC64_GOT_TPREL16_DS:
6911 case R_PPC64_GOT_TPREL16_LO_DS:
6912 case R_PPC64_GOT_TPREL16_HI:
6913 case R_PPC64_GOT_TPREL16_HA:
6914 expecting_tls_get_addr = 0;
6915 if (ok_tprel)
6916 {
6917 /* IE -> LE */
6918 tls_set = 0;
6919 tls_clear = TLS_TPREL;
6920 tls_type = TLS_TLS | TLS_TPREL;
6921 break;
6922 }
6923 else
6924 continue;
6925
6926 case R_PPC64_REL14:
6927 case R_PPC64_REL14_BRTAKEN:
6928 case R_PPC64_REL14_BRNTAKEN:
6929 case R_PPC64_REL24:
6930 if (h != NULL
6931 && (h == &htab->tls_get_addr->elf
6932 || h == &htab->tls_get_addr_fd->elf))
6933 {
6934 if (!expecting_tls_get_addr
6935 && rel != relstart
6936 && ((ELF64_R_TYPE (rel[-1].r_info)
6937 == R_PPC64_TOC16)
6938 || (ELF64_R_TYPE (rel[-1].r_info)
6939 == R_PPC64_TOC16_LO)))
6940 {
6941 /* Check for toc tls entries. */
6942 char *toc_tls;
6943 int retval;
6944
6945 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
6946 rel - 1, ibfd);
6947 if (retval == 0)
6948 goto err_free_rel;
6949 if (retval > 1 && toc_tls != NULL)
6950 {
6951 expecting_tls_get_addr = 1;
6952 if (toc_ref != NULL)
6953 toc_ref[toc_ref_index] = 1;
6954 }
6955 }
6956
6957 if (expecting_tls_get_addr)
6958 {
6959 struct plt_entry *ent;
6960 for (ent = h->plt.plist; ent; ent = ent->next)
6961 if (ent->addend == 0)
6962 {
6963 if (ent->plt.refcount > 0)
6964 ent->plt.refcount -= 1;
6965 break;
6966 }
6967 }
6968 }
6969 expecting_tls_get_addr = 0;
6970 continue;
6971
6972 case R_PPC64_TOC16:
6973 case R_PPC64_TOC16_LO:
6974 case R_PPC64_TLS:
6975 expecting_tls_get_addr = 0;
6976 if (sym_sec == toc && toc != NULL)
6977 {
6978 /* Mark this toc entry as referenced by a TLS
6979 code sequence. We can do that now in the
6980 case of R_PPC64_TLS, and after checking for
6981 tls_get_addr for the TOC16 relocs. */
6982 if (toc_ref == NULL)
6983 {
6984 toc_ref = bfd_zmalloc (toc->size / 8);
6985 if (toc_ref == NULL)
6986 goto err_free_rel;
6987 }
6988 if (h != NULL)
6989 value = h->root.u.def.value;
6990 else
6991 value = sym->st_value;
6992 value += rel->r_addend;
6993 BFD_ASSERT (value < toc->size && value % 8 == 0);
6994 toc_ref_index = value / 8;
6995 if (r_type == R_PPC64_TLS)
6996 toc_ref[toc_ref_index] = 1;
6997 }
6998 continue;
6999
7000 case R_PPC64_TPREL64:
7001 expecting_tls_get_addr = 0;
7002 if (sec != toc
7003 || toc_ref == NULL
7004 || !toc_ref[rel->r_offset / 8])
7005 continue;
7006 if (ok_tprel)
7007 {
7008 /* IE -> LE */
7009 tls_set = TLS_EXPLICIT;
7010 tls_clear = TLS_TPREL;
7011 break;
7012 }
7013 else
7014 continue;
7015
7016 case R_PPC64_DTPMOD64:
7017 expecting_tls_get_addr = 0;
7018 if (sec != toc
7019 || toc_ref == NULL
7020 || !toc_ref[rel->r_offset / 8])
7021 continue;
7022 if (rel + 1 < relend
7023 && (rel[1].r_info
7024 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7025 && rel[1].r_offset == rel->r_offset + 8)
7026 {
7027 if (ok_tprel)
7028 /* GD -> LE */
7029 tls_set = TLS_EXPLICIT | TLS_GD;
7030 else
7031 /* GD -> IE */
7032 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7033 tls_clear = TLS_GD;
7034 }
7035 else
7036 {
7037 if (!is_local)
7038 continue;
7039
7040 /* LD -> LE */
7041 tls_set = TLS_EXPLICIT;
7042 tls_clear = TLS_LD;
7043 }
7044 break;
7045
7046 default:
7047 expecting_tls_get_addr = 0;
7048 continue;
7049 }
7050
7051 if ((tls_set & TLS_EXPLICIT) == 0)
7052 {
7053 struct got_entry *ent;
7054
7055 /* Adjust got entry for this reloc. */
7056 if (h != NULL)
7057 ent = h->got.glist;
7058 else
7059 ent = elf_local_got_ents (ibfd)[r_symndx];
7060
7061 for (; ent != NULL; ent = ent->next)
7062 if (ent->addend == rel->r_addend
7063 && ent->owner == ibfd
7064 && ent->tls_type == tls_type)
7065 break;
7066 if (ent == NULL)
7067 abort ();
7068
7069 if (tls_set == 0)
7070 {
7071 /* We managed to get rid of a got entry. */
7072 if (ent->got.refcount > 0)
7073 ent->got.refcount -= 1;
7074 }
7075 }
7076 else
7077 {
7078 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7079 we'll lose one or two dyn relocs. */
7080 if (!dec_dynrel_count (rel->r_info, sec, info,
7081 NULL, h, sym_sec))
7082 return FALSE;
7083
7084 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7085 {
7086 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7087 NULL, h, sym_sec))
7088 return FALSE;
7089 }
7090 }
7091
7092 *tls_mask |= tls_set;
7093 *tls_mask &= ~tls_clear;
7094 }
7095
7096 if (elf_section_data (sec)->relocs != relstart)
7097 free (relstart);
7098 }
7099
7100 if (toc_ref != NULL)
7101 free (toc_ref);
7102
7103 if (locsyms != NULL
7104 && (elf_tdata (ibfd)->symtab_hdr.contents
7105 != (unsigned char *) locsyms))
7106 {
7107 if (!info->keep_memory)
7108 free (locsyms);
7109 else
7110 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
7111 }
7112 }
7113 return TRUE;
7114 }
7115
7116 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7117 the values of any global symbols in a toc section that has been
7118 edited. Globals in toc sections should be a rarity, so this function
7119 sets a flag if any are found in toc sections other than the one just
7120 edited, so that futher hash table traversals can be avoided. */
7121
7122 struct adjust_toc_info
7123 {
7124 asection *toc;
7125 unsigned long *skip;
7126 bfd_boolean global_toc_syms;
7127 };
7128
7129 static bfd_boolean
7130 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7131 {
7132 struct ppc_link_hash_entry *eh;
7133 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7134
7135 if (h->root.type == bfd_link_hash_indirect)
7136 return TRUE;
7137
7138 if (h->root.type == bfd_link_hash_warning)
7139 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7140
7141 if (h->root.type != bfd_link_hash_defined
7142 && h->root.type != bfd_link_hash_defweak)
7143 return TRUE;
7144
7145 eh = (struct ppc_link_hash_entry *) h;
7146 if (eh->adjust_done)
7147 return TRUE;
7148
7149 if (eh->elf.root.u.def.section == toc_inf->toc)
7150 {
7151 unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7152 if (skip != (unsigned long) -1)
7153 eh->elf.root.u.def.value -= skip;
7154 else
7155 {
7156 (*_bfd_error_handler)
7157 (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7158 eh->elf.root.u.def.section = &bfd_abs_section;
7159 eh->elf.root.u.def.value = 0;
7160 }
7161 eh->adjust_done = 1;
7162 }
7163 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7164 toc_inf->global_toc_syms = TRUE;
7165
7166 return TRUE;
7167 }
7168
7169 /* Examine all relocs referencing .toc sections in order to remove
7170 unused .toc entries. */
7171
7172 bfd_boolean
7173 ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7174 {
7175 bfd *ibfd;
7176 struct adjust_toc_info toc_inf;
7177
7178 toc_inf.global_toc_syms = TRUE;
7179 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7180 {
7181 asection *toc, *sec;
7182 Elf_Internal_Shdr *symtab_hdr;
7183 Elf_Internal_Sym *local_syms;
7184 struct elf_link_hash_entry **sym_hashes;
7185 Elf_Internal_Rela *relstart, *rel;
7186 unsigned long *skip, *drop;
7187 unsigned char *used;
7188 unsigned char *keep, last, some_unused;
7189
7190 toc = bfd_get_section_by_name (ibfd, ".toc");
7191 if (toc == NULL
7192 || toc->size == 0
7193 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7194 || elf_discarded_section (toc))
7195 continue;
7196
7197 local_syms = NULL;
7198 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7199 sym_hashes = elf_sym_hashes (ibfd);
7200
7201 /* Look at sections dropped from the final link. */
7202 skip = NULL;
7203 relstart = NULL;
7204 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7205 {
7206 if (sec->reloc_count == 0
7207 || !elf_discarded_section (sec)
7208 || get_opd_info (sec)
7209 || (sec->flags & SEC_ALLOC) == 0
7210 || (sec->flags & SEC_DEBUGGING) != 0)
7211 continue;
7212
7213 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7214 if (relstart == NULL)
7215 goto error_ret;
7216
7217 /* Run through the relocs to see which toc entries might be
7218 unused. */
7219 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7220 {
7221 enum elf_ppc64_reloc_type r_type;
7222 unsigned long r_symndx;
7223 asection *sym_sec;
7224 struct elf_link_hash_entry *h;
7225 Elf_Internal_Sym *sym;
7226 bfd_vma val;
7227
7228 r_type = ELF64_R_TYPE (rel->r_info);
7229 switch (r_type)
7230 {
7231 default:
7232 continue;
7233
7234 case R_PPC64_TOC16:
7235 case R_PPC64_TOC16_LO:
7236 case R_PPC64_TOC16_HI:
7237 case R_PPC64_TOC16_HA:
7238 case R_PPC64_TOC16_DS:
7239 case R_PPC64_TOC16_LO_DS:
7240 break;
7241 }
7242
7243 r_symndx = ELF64_R_SYM (rel->r_info);
7244 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7245 r_symndx, ibfd))
7246 goto error_ret;
7247
7248 if (sym_sec != toc)
7249 continue;
7250
7251 if (h != NULL)
7252 val = h->root.u.def.value;
7253 else
7254 val = sym->st_value;
7255 val += rel->r_addend;
7256
7257 if (val >= toc->size)
7258 continue;
7259
7260 /* Anything in the toc ought to be aligned to 8 bytes.
7261 If not, don't mark as unused. */
7262 if (val & 7)
7263 continue;
7264
7265 if (skip == NULL)
7266 {
7267 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7268 if (skip == NULL)
7269 goto error_ret;
7270 }
7271
7272 skip[val >> 3] = 1;
7273 }
7274
7275 if (elf_section_data (sec)->relocs != relstart)
7276 free (relstart);
7277 }
7278
7279 if (skip == NULL)
7280 continue;
7281
7282 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7283 if (used == NULL)
7284 {
7285 error_ret:
7286 if (local_syms != NULL
7287 && symtab_hdr->contents != (unsigned char *) local_syms)
7288 free (local_syms);
7289 if (sec != NULL
7290 && relstart != NULL
7291 && elf_section_data (sec)->relocs != relstart)
7292 free (relstart);
7293 if (skip != NULL)
7294 free (skip);
7295 return FALSE;
7296 }
7297
7298 /* Now check all kept sections that might reference the toc.
7299 Check the toc itself last. */
7300 for (sec = (ibfd->sections == toc && toc->next ? toc->next
7301 : ibfd->sections);
7302 sec != NULL;
7303 sec = (sec == toc ? NULL
7304 : sec->next == NULL ? toc
7305 : sec->next == toc && toc->next ? toc->next
7306 : sec->next))
7307 {
7308 int repeat;
7309
7310 if (sec->reloc_count == 0
7311 || elf_discarded_section (sec)
7312 || get_opd_info (sec)
7313 || (sec->flags & SEC_ALLOC) == 0
7314 || (sec->flags & SEC_DEBUGGING) != 0)
7315 continue;
7316
7317 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7318 if (relstart == NULL)
7319 goto error_ret;
7320
7321 /* Mark toc entries referenced as used. */
7322 repeat = 0;
7323 do
7324 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7325 {
7326 enum elf_ppc64_reloc_type r_type;
7327 unsigned long r_symndx;
7328 asection *sym_sec;
7329 struct elf_link_hash_entry *h;
7330 Elf_Internal_Sym *sym;
7331 bfd_vma val;
7332
7333 r_type = ELF64_R_TYPE (rel->r_info);
7334 switch (r_type)
7335 {
7336 case R_PPC64_TOC16:
7337 case R_PPC64_TOC16_LO:
7338 case R_PPC64_TOC16_HI:
7339 case R_PPC64_TOC16_HA:
7340 case R_PPC64_TOC16_DS:
7341 case R_PPC64_TOC16_LO_DS:
7342 /* In case we're taking addresses of toc entries. */
7343 case R_PPC64_ADDR64:
7344 break;
7345
7346 default:
7347 continue;
7348 }
7349
7350 r_symndx = ELF64_R_SYM (rel->r_info);
7351 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7352 r_symndx, ibfd))
7353 {
7354 free (used);
7355 goto error_ret;
7356 }
7357
7358 if (sym_sec != toc)
7359 continue;
7360
7361 if (h != NULL)
7362 val = h->root.u.def.value;
7363 else
7364 val = sym->st_value;
7365 val += rel->r_addend;
7366
7367 if (val >= toc->size)
7368 continue;
7369
7370 /* For the toc section, we only mark as used if
7371 this entry itself isn't unused. */
7372 if (sec == toc
7373 && !used[val >> 3]
7374 && (used[rel->r_offset >> 3]
7375 || !skip[rel->r_offset >> 3]))
7376 /* Do all the relocs again, to catch reference
7377 chains. */
7378 repeat = 1;
7379
7380 used[val >> 3] = 1;
7381 }
7382 while (repeat);
7383 }
7384
7385 /* Merge the used and skip arrays. Assume that TOC
7386 doublewords not appearing as either used or unused belong
7387 to to an entry more than one doubleword in size. */
7388 for (drop = skip, keep = used, last = 0, some_unused = 0;
7389 drop < skip + (toc->size + 7) / 8;
7390 ++drop, ++keep)
7391 {
7392 if (*keep)
7393 {
7394 *drop = 0;
7395 last = 0;
7396 }
7397 else if (*drop)
7398 {
7399 some_unused = 1;
7400 last = 1;
7401 }
7402 else
7403 *drop = last;
7404 }
7405
7406 free (used);
7407
7408 if (some_unused)
7409 {
7410 bfd_byte *contents, *src;
7411 unsigned long off;
7412
7413 /* Shuffle the toc contents, and at the same time convert the
7414 skip array from booleans into offsets. */
7415 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7416 goto error_ret;
7417
7418 elf_section_data (toc)->this_hdr.contents = contents;
7419
7420 for (src = contents, off = 0, drop = skip;
7421 src < contents + toc->size;
7422 src += 8, ++drop)
7423 {
7424 if (*drop)
7425 {
7426 *drop = (unsigned long) -1;
7427 off += 8;
7428 }
7429 else if (off != 0)
7430 {
7431 *drop = off;
7432 memcpy (src - off, src, 8);
7433 }
7434 }
7435 toc->rawsize = toc->size;
7436 toc->size = src - contents - off;
7437
7438 if (toc->reloc_count != 0)
7439 {
7440 Elf_Internal_Rela *wrel;
7441 bfd_size_type sz;
7442
7443 /* Read toc relocs. */
7444 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7445 TRUE);
7446 if (relstart == NULL)
7447 goto error_ret;
7448
7449 /* Remove unused toc relocs, and adjust those we keep. */
7450 wrel = relstart;
7451 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7452 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7453 {
7454 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7455 wrel->r_info = rel->r_info;
7456 wrel->r_addend = rel->r_addend;
7457 ++wrel;
7458 }
7459 else if (!dec_dynrel_count (rel->r_info, toc, info,
7460 &local_syms, NULL, NULL))
7461 goto error_ret;
7462
7463 toc->reloc_count = wrel - relstart;
7464 sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7465 elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7466 BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7467 }
7468
7469 /* Adjust addends for relocs against the toc section sym. */
7470 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7471 {
7472 if (sec->reloc_count == 0
7473 || elf_discarded_section (sec))
7474 continue;
7475
7476 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7477 TRUE);
7478 if (relstart == NULL)
7479 goto error_ret;
7480
7481 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7482 {
7483 enum elf_ppc64_reloc_type r_type;
7484 unsigned long r_symndx;
7485 asection *sym_sec;
7486 struct elf_link_hash_entry *h;
7487 Elf_Internal_Sym *sym;
7488
7489 r_type = ELF64_R_TYPE (rel->r_info);
7490 switch (r_type)
7491 {
7492 default:
7493 continue;
7494
7495 case R_PPC64_TOC16:
7496 case R_PPC64_TOC16_LO:
7497 case R_PPC64_TOC16_HI:
7498 case R_PPC64_TOC16_HA:
7499 case R_PPC64_TOC16_DS:
7500 case R_PPC64_TOC16_LO_DS:
7501 case R_PPC64_ADDR64:
7502 break;
7503 }
7504
7505 r_symndx = ELF64_R_SYM (rel->r_info);
7506 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7507 r_symndx, ibfd))
7508 goto error_ret;
7509
7510 if (sym_sec != toc || h != NULL || sym->st_value != 0)
7511 continue;
7512
7513 rel->r_addend -= skip[rel->r_addend >> 3];
7514 }
7515 }
7516
7517 /* We shouldn't have local or global symbols defined in the TOC,
7518 but handle them anyway. */
7519 if (local_syms != NULL)
7520 {
7521 Elf_Internal_Sym *sym;
7522
7523 for (sym = local_syms;
7524 sym < local_syms + symtab_hdr->sh_info;
7525 ++sym)
7526 if (sym->st_shndx != SHN_UNDEF
7527 && (sym->st_shndx < SHN_LORESERVE
7528 || sym->st_shndx > SHN_HIRESERVE)
7529 && sym->st_value != 0
7530 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7531 {
7532 if (skip[sym->st_value >> 3] != (unsigned long) -1)
7533 sym->st_value -= skip[sym->st_value >> 3];
7534 else
7535 {
7536 (*_bfd_error_handler)
7537 (_("%s defined in removed toc entry"),
7538 bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7539 NULL));
7540 sym->st_value = 0;
7541 sym->st_shndx = SHN_ABS;
7542 }
7543 symtab_hdr->contents = (unsigned char *) local_syms;
7544 }
7545 }
7546
7547 /* Finally, adjust any global syms defined in the toc. */
7548 if (toc_inf.global_toc_syms)
7549 {
7550 toc_inf.toc = toc;
7551 toc_inf.skip = skip;
7552 toc_inf.global_toc_syms = FALSE;
7553 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7554 &toc_inf);
7555 }
7556 }
7557
7558 if (local_syms != NULL
7559 && symtab_hdr->contents != (unsigned char *) local_syms)
7560 {
7561 if (!info->keep_memory)
7562 free (local_syms);
7563 else
7564 symtab_hdr->contents = (unsigned char *) local_syms;
7565 }
7566 free (skip);
7567 }
7568
7569 return TRUE;
7570 }
7571
7572 /* Allocate space in .plt, .got and associated reloc sections for
7573 dynamic relocs. */
7574
7575 static bfd_boolean
7576 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7577 {
7578 struct bfd_link_info *info;
7579 struct ppc_link_hash_table *htab;
7580 asection *s;
7581 struct ppc_link_hash_entry *eh;
7582 struct ppc_dyn_relocs *p;
7583 struct got_entry *gent;
7584
7585 if (h->root.type == bfd_link_hash_indirect)
7586 return TRUE;
7587
7588 if (h->root.type == bfd_link_hash_warning)
7589 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7590
7591 info = (struct bfd_link_info *) inf;
7592 htab = ppc_hash_table (info);
7593
7594 if (htab->elf.dynamic_sections_created
7595 && h->dynindx != -1
7596 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
7597 {
7598 struct plt_entry *pent;
7599 bfd_boolean doneone = FALSE;
7600 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7601 if (pent->plt.refcount > 0)
7602 {
7603 /* If this is the first .plt entry, make room for the special
7604 first entry. */
7605 s = htab->plt;
7606 if (s->size == 0)
7607 s->size += PLT_INITIAL_ENTRY_SIZE;
7608
7609 pent->plt.offset = s->size;
7610
7611 /* Make room for this entry. */
7612 s->size += PLT_ENTRY_SIZE;
7613
7614 /* Make room for the .glink code. */
7615 s = htab->glink;
7616 if (s->size == 0)
7617 s->size += GLINK_CALL_STUB_SIZE;
7618 /* We need bigger stubs past index 32767. */
7619 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7620 s->size += 4;
7621 s->size += 2*4;
7622
7623 /* We also need to make an entry in the .rela.plt section. */
7624 s = htab->relplt;
7625 s->size += sizeof (Elf64_External_Rela);
7626 doneone = TRUE;
7627 }
7628 else
7629 pent->plt.offset = (bfd_vma) -1;
7630 if (!doneone)
7631 {
7632 h->plt.plist = NULL;
7633 h->needs_plt = 0;
7634 }
7635 }
7636 else
7637 {
7638 h->plt.plist = NULL;
7639 h->needs_plt = 0;
7640 }
7641
7642 eh = (struct ppc_link_hash_entry *) h;
7643 /* Run through the TLS GD got entries first if we're changing them
7644 to TPREL. */
7645 if ((eh->tls_mask & TLS_TPRELGD) != 0)
7646 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7647 if (gent->got.refcount > 0
7648 && (gent->tls_type & TLS_GD) != 0)
7649 {
7650 /* This was a GD entry that has been converted to TPREL. If
7651 there happens to be a TPREL entry we can use that one. */
7652 struct got_entry *ent;
7653 for (ent = h->got.glist; ent != NULL; ent = ent->next)
7654 if (ent->got.refcount > 0
7655 && (ent->tls_type & TLS_TPREL) != 0
7656 && ent->addend == gent->addend
7657 && ent->owner == gent->owner)
7658 {
7659 gent->got.refcount = 0;
7660 break;
7661 }
7662
7663 /* If not, then we'll be using our own TPREL entry. */
7664 if (gent->got.refcount != 0)
7665 gent->tls_type = TLS_TLS | TLS_TPREL;
7666 }
7667
7668 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7669 if (gent->got.refcount > 0)
7670 {
7671 bfd_boolean dyn;
7672
7673 /* Make sure this symbol is output as a dynamic symbol.
7674 Undefined weak syms won't yet be marked as dynamic,
7675 nor will all TLS symbols. */
7676 if (h->dynindx == -1
7677 && !h->forced_local)
7678 {
7679 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7680 return FALSE;
7681 }
7682
7683 if ((gent->tls_type & TLS_LD) != 0
7684 && !h->def_dynamic)
7685 {
7686 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
7687 continue;
7688 }
7689
7690 s = ppc64_elf_tdata (gent->owner)->got;
7691 gent->got.offset = s->size;
7692 s->size
7693 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
7694 dyn = htab->elf.dynamic_sections_created;
7695 if ((info->shared
7696 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7697 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7698 || h->root.type != bfd_link_hash_undefweak))
7699 ppc64_elf_tdata (gent->owner)->relgot->size
7700 += (gent->tls_type & eh->tls_mask & TLS_GD
7701 ? 2 * sizeof (Elf64_External_Rela)
7702 : sizeof (Elf64_External_Rela));
7703 }
7704 else
7705 gent->got.offset = (bfd_vma) -1;
7706
7707 if (eh->dyn_relocs == NULL)
7708 return TRUE;
7709
7710 /* In the shared -Bsymbolic case, discard space allocated for
7711 dynamic pc-relative relocs against symbols which turn out to be
7712 defined in regular objects. For the normal shared case, discard
7713 space for relocs that have become local due to symbol visibility
7714 changes. */
7715
7716 if (info->shared)
7717 {
7718 /* Relocs that use pc_count are those that appear on a call insn,
7719 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7720 generated via assembly. We want calls to protected symbols to
7721 resolve directly to the function rather than going via the plt.
7722 If people want function pointer comparisons to work as expected
7723 then they should avoid writing weird assembly. */
7724 if (SYMBOL_CALLS_LOCAL (info, h))
7725 {
7726 struct ppc_dyn_relocs **pp;
7727
7728 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
7729 {
7730 p->count -= p->pc_count;
7731 p->pc_count = 0;
7732 if (p->count == 0)
7733 *pp = p->next;
7734 else
7735 pp = &p->next;
7736 }
7737 }
7738
7739 /* Also discard relocs on undefined weak syms with non-default
7740 visibility. */
7741 if (eh->dyn_relocs != NULL
7742 && h->root.type == bfd_link_hash_undefweak)
7743 {
7744 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7745 eh->dyn_relocs = NULL;
7746
7747 /* Make sure this symbol is output as a dynamic symbol.
7748 Undefined weak syms won't yet be marked as dynamic. */
7749 else if (h->dynindx == -1
7750 && !h->forced_local)
7751 {
7752 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7753 return FALSE;
7754 }
7755 }
7756 }
7757 else if (ELIMINATE_COPY_RELOCS)
7758 {
7759 /* For the non-shared case, discard space for relocs against
7760 symbols which turn out to need copy relocs or are not
7761 dynamic. */
7762
7763 if (!h->non_got_ref
7764 && h->def_dynamic
7765 && !h->def_regular)
7766 {
7767 /* Make sure this symbol is output as a dynamic symbol.
7768 Undefined weak syms won't yet be marked as dynamic. */
7769 if (h->dynindx == -1
7770 && !h->forced_local)
7771 {
7772 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7773 return FALSE;
7774 }
7775
7776 /* If that succeeded, we know we'll be keeping all the
7777 relocs. */
7778 if (h->dynindx != -1)
7779 goto keep;
7780 }
7781
7782 eh->dyn_relocs = NULL;
7783
7784 keep: ;
7785 }
7786
7787 /* Finally, allocate space. */
7788 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7789 {
7790 asection *sreloc = elf_section_data (p->sec)->sreloc;
7791 sreloc->size += p->count * sizeof (Elf64_External_Rela);
7792 }
7793
7794 return TRUE;
7795 }
7796
7797 /* Find any dynamic relocs that apply to read-only sections. */
7798
7799 static bfd_boolean
7800 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7801 {
7802 struct ppc_link_hash_entry *eh;
7803 struct ppc_dyn_relocs *p;
7804
7805 if (h->root.type == bfd_link_hash_warning)
7806 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7807
7808 eh = (struct ppc_link_hash_entry *) h;
7809 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7810 {
7811 asection *s = p->sec->output_section;
7812
7813 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7814 {
7815 struct bfd_link_info *info = inf;
7816
7817 info->flags |= DF_TEXTREL;
7818
7819 /* Not an error, just cut short the traversal. */
7820 return FALSE;
7821 }
7822 }
7823 return TRUE;
7824 }
7825
7826 /* Set the sizes of the dynamic sections. */
7827
7828 static bfd_boolean
7829 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7830 struct bfd_link_info *info)
7831 {
7832 struct ppc_link_hash_table *htab;
7833 bfd *dynobj;
7834 asection *s;
7835 bfd_boolean relocs;
7836 bfd *ibfd;
7837
7838 htab = ppc_hash_table (info);
7839 dynobj = htab->elf.dynobj;
7840 if (dynobj == NULL)
7841 abort ();
7842
7843 if (htab->elf.dynamic_sections_created)
7844 {
7845 /* Set the contents of the .interp section to the interpreter. */
7846 if (info->executable)
7847 {
7848 s = bfd_get_section_by_name (dynobj, ".interp");
7849 if (s == NULL)
7850 abort ();
7851 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7852 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7853 }
7854 }
7855
7856 /* Set up .got offsets for local syms, and space for local dynamic
7857 relocs. */
7858 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7859 {
7860 struct got_entry **lgot_ents;
7861 struct got_entry **end_lgot_ents;
7862 char *lgot_masks;
7863 bfd_size_type locsymcount;
7864 Elf_Internal_Shdr *symtab_hdr;
7865 asection *srel;
7866
7867 if (!is_ppc64_elf_target (ibfd->xvec))
7868 continue;
7869
7870 if (ppc64_tlsld_got (ibfd)->refcount > 0)
7871 {
7872 s = ppc64_elf_tdata (ibfd)->got;
7873 ppc64_tlsld_got (ibfd)->offset = s->size;
7874 s->size += 16;
7875 if (info->shared)
7876 {
7877 srel = ppc64_elf_tdata (ibfd)->relgot;
7878 srel->size += sizeof (Elf64_External_Rela);
7879 }
7880 }
7881 else
7882 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7883
7884 for (s = ibfd->sections; s != NULL; s = s->next)
7885 {
7886 struct ppc_dyn_relocs *p;
7887
7888 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7889 {
7890 if (!bfd_is_abs_section (p->sec)
7891 && bfd_is_abs_section (p->sec->output_section))
7892 {
7893 /* Input section has been discarded, either because
7894 it is a copy of a linkonce section or due to
7895 linker script /DISCARD/, so we'll be discarding
7896 the relocs too. */
7897 }
7898 else if (p->count != 0)
7899 {
7900 srel = elf_section_data (p->sec)->sreloc;
7901 srel->size += p->count * sizeof (Elf64_External_Rela);
7902 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7903 info->flags |= DF_TEXTREL;
7904 }
7905 }
7906 }
7907
7908 lgot_ents = elf_local_got_ents (ibfd);
7909 if (!lgot_ents)
7910 continue;
7911
7912 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7913 locsymcount = symtab_hdr->sh_info;
7914 end_lgot_ents = lgot_ents + locsymcount;
7915 lgot_masks = (char *) end_lgot_ents;
7916 s = ppc64_elf_tdata (ibfd)->got;
7917 srel = ppc64_elf_tdata (ibfd)->relgot;
7918 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
7919 {
7920 struct got_entry *ent;
7921
7922 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7923 if (ent->got.refcount > 0)
7924 {
7925 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
7926 {
7927 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
7928 {
7929 ppc64_tlsld_got (ibfd)->offset = s->size;
7930 s->size += 16;
7931 if (info->shared)
7932 srel->size += sizeof (Elf64_External_Rela);
7933 }
7934 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
7935 }
7936 else
7937 {
7938 ent->got.offset = s->size;
7939 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
7940 {
7941 s->size += 16;
7942 if (info->shared)
7943 srel->size += 2 * sizeof (Elf64_External_Rela);
7944 }
7945 else
7946 {
7947 s->size += 8;
7948 if (info->shared)
7949 srel->size += sizeof (Elf64_External_Rela);
7950 }
7951 }
7952 }
7953 else
7954 ent->got.offset = (bfd_vma) -1;
7955 }
7956 }
7957
7958 /* Allocate global sym .plt and .got entries, and space for global
7959 sym dynamic relocs. */
7960 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
7961
7962 /* We now have determined the sizes of the various dynamic sections.
7963 Allocate memory for them. */
7964 relocs = FALSE;
7965 for (s = dynobj->sections; s != NULL; s = s->next)
7966 {
7967 if ((s->flags & SEC_LINKER_CREATED) == 0)
7968 continue;
7969
7970 if (s == htab->brlt || s == htab->relbrlt)
7971 /* These haven't been allocated yet; don't strip. */
7972 continue;
7973 else if (s == htab->got
7974 || s == htab->plt
7975 || s == htab->glink
7976 || s == htab->dynbss)
7977 {
7978 /* Strip this section if we don't need it; see the
7979 comment below. */
7980 }
7981 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
7982 {
7983 if (s->size != 0)
7984 {
7985 if (s != htab->relplt)
7986 relocs = TRUE;
7987
7988 /* We use the reloc_count field as a counter if we need
7989 to copy relocs into the output file. */
7990 s->reloc_count = 0;
7991 }
7992 }
7993 else
7994 {
7995 /* It's not one of our sections, so don't allocate space. */
7996 continue;
7997 }
7998
7999 if (s->size == 0)
8000 {
8001 /* If we don't need this section, strip it from the
8002 output file. This is mostly to handle .rela.bss and
8003 .rela.plt. We must create both sections in
8004 create_dynamic_sections, because they must be created
8005 before the linker maps input sections to output
8006 sections. The linker does that before
8007 adjust_dynamic_symbol is called, and it is that
8008 function which decides whether anything needs to go
8009 into these sections. */
8010 s->flags |= SEC_EXCLUDE;
8011 continue;
8012 }
8013
8014 if ((s->flags & SEC_HAS_CONTENTS) == 0)
8015 continue;
8016
8017 /* Allocate memory for the section contents. We use bfd_zalloc
8018 here in case unused entries are not reclaimed before the
8019 section's contents are written out. This should not happen,
8020 but this way if it does we get a R_PPC64_NONE reloc in .rela
8021 sections instead of garbage.
8022 We also rely on the section contents being zero when writing
8023 the GOT. */
8024 s->contents = bfd_zalloc (dynobj, s->size);
8025 if (s->contents == NULL)
8026 return FALSE;
8027 }
8028
8029 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8030 {
8031 if (!is_ppc64_elf_target (ibfd->xvec))
8032 continue;
8033
8034 s = ppc64_elf_tdata (ibfd)->got;
8035 if (s != NULL && s != htab->got)
8036 {
8037 if (s->size == 0)
8038 s->flags |= SEC_EXCLUDE;
8039 else
8040 {
8041 s->contents = bfd_zalloc (ibfd, s->size);
8042 if (s->contents == NULL)
8043 return FALSE;
8044 }
8045 }
8046 s = ppc64_elf_tdata (ibfd)->relgot;
8047 if (s != NULL)
8048 {
8049 if (s->size == 0)
8050 s->flags |= SEC_EXCLUDE;
8051 else
8052 {
8053 s->contents = bfd_zalloc (ibfd, s->size);
8054 if (s->contents == NULL)
8055 return FALSE;
8056 relocs = TRUE;
8057 s->reloc_count = 0;
8058 }
8059 }
8060 }
8061
8062 if (htab->elf.dynamic_sections_created)
8063 {
8064 /* Add some entries to the .dynamic section. We fill in the
8065 values later, in ppc64_elf_finish_dynamic_sections, but we
8066 must add the entries now so that we get the correct size for
8067 the .dynamic section. The DT_DEBUG entry is filled in by the
8068 dynamic linker and used by the debugger. */
8069 #define add_dynamic_entry(TAG, VAL) \
8070 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
8071
8072 if (info->executable)
8073 {
8074 if (!add_dynamic_entry (DT_DEBUG, 0))
8075 return FALSE;
8076 }
8077
8078 if (htab->plt != NULL && htab->plt->size != 0)
8079 {
8080 if (!add_dynamic_entry (DT_PLTGOT, 0)
8081 || !add_dynamic_entry (DT_PLTRELSZ, 0)
8082 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
8083 || !add_dynamic_entry (DT_JMPREL, 0)
8084 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
8085 return FALSE;
8086 }
8087
8088 if (NO_OPD_RELOCS)
8089 {
8090 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
8091 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
8092 return FALSE;
8093 }
8094
8095 if (relocs)
8096 {
8097 if (!add_dynamic_entry (DT_RELA, 0)
8098 || !add_dynamic_entry (DT_RELASZ, 0)
8099 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
8100 return FALSE;
8101
8102 /* If any dynamic relocs apply to a read-only section,
8103 then we need a DT_TEXTREL entry. */
8104 if ((info->flags & DF_TEXTREL) == 0)
8105 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
8106
8107 if ((info->flags & DF_TEXTREL) != 0)
8108 {
8109 if (!add_dynamic_entry (DT_TEXTREL, 0))
8110 return FALSE;
8111 }
8112 }
8113 }
8114 #undef add_dynamic_entry
8115
8116 return TRUE;
8117 }
8118
8119 /* Determine the type of stub needed, if any, for a call. */
8120
8121 static inline enum ppc_stub_type
8122 ppc_type_of_stub (asection *input_sec,
8123 const Elf_Internal_Rela *rel,
8124 struct ppc_link_hash_entry **hash,
8125 bfd_vma destination)
8126 {
8127 struct ppc_link_hash_entry *h = *hash;
8128 bfd_vma location;
8129 bfd_vma branch_offset;
8130 bfd_vma max_branch_offset;
8131 enum elf_ppc64_reloc_type r_type;
8132
8133 if (h != NULL)
8134 {
8135 struct ppc_link_hash_entry *fdh = h;
8136 if (fdh->oh != NULL
8137 && fdh->oh->is_func_descriptor)
8138 fdh = fdh->oh;
8139
8140 if (fdh->elf.dynindx != -1)
8141 {
8142 struct plt_entry *ent;
8143
8144 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
8145 if (ent->addend == rel->r_addend
8146 && ent->plt.offset != (bfd_vma) -1)
8147 {
8148 *hash = fdh;
8149 return ppc_stub_plt_call;
8150 }
8151 }
8152
8153 /* Here, we know we don't have a plt entry. If we don't have a
8154 either a defined function descriptor or a defined entry symbol
8155 in a regular object file, then it is pointless trying to make
8156 any other type of stub. */
8157 if (!((fdh->elf.root.type == bfd_link_hash_defined
8158 || fdh->elf.root.type == bfd_link_hash_defweak)
8159 && fdh->elf.root.u.def.section->output_section != NULL)
8160 && !((h->elf.root.type == bfd_link_hash_defined
8161 || h->elf.root.type == bfd_link_hash_defweak)
8162 && h->elf.root.u.def.section->output_section != NULL))
8163 return ppc_stub_none;
8164 }
8165
8166 /* Determine where the call point is. */
8167 location = (input_sec->output_offset
8168 + input_sec->output_section->vma
8169 + rel->r_offset);
8170
8171 branch_offset = destination - location;
8172 r_type = ELF64_R_TYPE (rel->r_info);
8173
8174 /* Determine if a long branch stub is needed. */
8175 max_branch_offset = 1 << 25;
8176 if (r_type != R_PPC64_REL24)
8177 max_branch_offset = 1 << 15;
8178
8179 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8180 /* We need a stub. Figure out whether a long_branch or plt_branch
8181 is needed later. */
8182 return ppc_stub_long_branch;
8183
8184 return ppc_stub_none;
8185 }
8186
8187 /* Build a .plt call stub. */
8188
8189 static inline bfd_byte *
8190 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
8191 {
8192 #define PPC_LO(v) ((v) & 0xffff)
8193 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8194 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8195
8196 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
8197 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
8198 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8199 if (PPC_HA (offset + 8) != PPC_HA (offset))
8200 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8201 offset += 8;
8202 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
8203 if (PPC_HA (offset + 8) != PPC_HA (offset))
8204 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8205 offset += 8;
8206 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
8207 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8208 bfd_put_32 (obfd, BCTR, p), p += 4;
8209 return p;
8210 }
8211
8212 static bfd_boolean
8213 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8214 {
8215 struct ppc_stub_hash_entry *stub_entry;
8216 struct ppc_branch_hash_entry *br_entry;
8217 struct bfd_link_info *info;
8218 struct ppc_link_hash_table *htab;
8219 bfd_byte *loc;
8220 bfd_byte *p;
8221 unsigned int indx;
8222 struct plt_entry *ent;
8223 bfd_vma dest, off;
8224 int size;
8225
8226 /* Massage our args to the form they really have. */
8227 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8228 info = in_arg;
8229
8230 htab = ppc_hash_table (info);
8231
8232 /* Make a note of the offset within the stubs for this entry. */
8233 stub_entry->stub_offset = stub_entry->stub_sec->size;
8234 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
8235
8236 htab->stub_count[stub_entry->stub_type - 1] += 1;
8237 switch (stub_entry->stub_type)
8238 {
8239 case ppc_stub_long_branch:
8240 case ppc_stub_long_branch_r2off:
8241 /* Branches are relative. This is where we are going to. */
8242 off = dest = (stub_entry->target_value
8243 + stub_entry->target_section->output_offset
8244 + stub_entry->target_section->output_section->vma);
8245
8246 /* And this is where we are coming from. */
8247 off -= (stub_entry->stub_offset
8248 + stub_entry->stub_sec->output_offset
8249 + stub_entry->stub_sec->output_section->vma);
8250
8251 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8252 size = 4;
8253 else
8254 {
8255 bfd_vma r2off;
8256
8257 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8258 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8259 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8260 loc += 4;
8261 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8262 loc += 4;
8263 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8264 loc += 4;
8265 off -= 12;
8266 size = 16;
8267 }
8268 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
8269
8270 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8271 {
8272 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8273 stub_entry->root.string);
8274 htab->stub_error = TRUE;
8275 return FALSE;
8276 }
8277
8278 if (info->emitrelocations)
8279 {
8280 Elf_Internal_Rela *relocs, *r;
8281 struct bfd_elf_section_data *elfsec_data;
8282
8283 elfsec_data = elf_section_data (stub_entry->stub_sec);
8284 relocs = elfsec_data->relocs;
8285 if (relocs == NULL)
8286 {
8287 bfd_size_type relsize;
8288 relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8289 relocs = bfd_alloc (htab->stub_bfd, relsize);
8290 if (relocs == NULL)
8291 return FALSE;
8292 elfsec_data->relocs = relocs;
8293 elfsec_data->rel_hdr.sh_size = relsize;
8294 elfsec_data->rel_hdr.sh_entsize = 24;
8295 stub_entry->stub_sec->reloc_count = 0;
8296 }
8297 r = relocs + stub_entry->stub_sec->reloc_count;
8298 stub_entry->stub_sec->reloc_count += 1;
8299 r->r_offset = loc - stub_entry->stub_sec->contents;
8300 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8301 r->r_addend = dest;
8302 if (stub_entry->h != NULL)
8303 {
8304 struct elf_link_hash_entry **hashes;
8305 unsigned long symndx;
8306 struct ppc_link_hash_entry *h;
8307
8308 hashes = elf_sym_hashes (htab->stub_bfd);
8309 if (hashes == NULL)
8310 {
8311 bfd_size_type hsize;
8312
8313 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8314 hashes = bfd_zalloc (htab->stub_bfd, hsize);
8315 if (hashes == NULL)
8316 return FALSE;
8317 elf_sym_hashes (htab->stub_bfd) = hashes;
8318 htab->stub_globals = 1;
8319 }
8320 symndx = htab->stub_globals++;
8321 h = stub_entry->h;
8322 hashes[symndx] = &h->elf;
8323 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8324 if (h->oh != NULL && h->oh->is_func)
8325 h = h->oh;
8326 if (h->elf.root.u.def.section != stub_entry->target_section)
8327 /* H is an opd symbol. The addend must be zero. */
8328 r->r_addend = 0;
8329 else
8330 {
8331 off = (h->elf.root.u.def.value
8332 + h->elf.root.u.def.section->output_offset
8333 + h->elf.root.u.def.section->output_section->vma);
8334 r->r_addend -= off;
8335 }
8336 }
8337 }
8338 break;
8339
8340 case ppc_stub_plt_branch:
8341 case ppc_stub_plt_branch_r2off:
8342 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8343 stub_entry->root.string + 9,
8344 FALSE, FALSE);
8345 if (br_entry == NULL)
8346 {
8347 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
8348 stub_entry->root.string);
8349 htab->stub_error = TRUE;
8350 return FALSE;
8351 }
8352
8353 off = (stub_entry->target_value
8354 + stub_entry->target_section->output_offset
8355 + stub_entry->target_section->output_section->vma);
8356
8357 bfd_put_64 (htab->brlt->owner, off,
8358 htab->brlt->contents + br_entry->offset);
8359
8360 if (htab->relbrlt != NULL)
8361 {
8362 /* Create a reloc for the branch lookup table entry. */
8363 Elf_Internal_Rela rela;
8364 bfd_byte *rl;
8365
8366 rela.r_offset = (br_entry->offset
8367 + htab->brlt->output_offset
8368 + htab->brlt->output_section->vma);
8369 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8370 rela.r_addend = off;
8371
8372 rl = htab->relbrlt->contents;
8373 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8374 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
8375 }
8376 else if (info->emitrelocations)
8377 {
8378 Elf_Internal_Rela *relocs, *r;
8379 struct bfd_elf_section_data *elfsec_data;
8380
8381 elfsec_data = elf_section_data (htab->brlt);
8382 relocs = elfsec_data->relocs;
8383 if (relocs == NULL)
8384 {
8385 bfd_size_type relsize;
8386 relsize = htab->brlt->reloc_count * sizeof (*relocs);
8387 relocs = bfd_alloc (htab->brlt->owner, relsize);
8388 if (relocs == NULL)
8389 return FALSE;
8390 elfsec_data->relocs = relocs;
8391 elfsec_data->rel_hdr.sh_size = relsize;
8392 elfsec_data->rel_hdr.sh_entsize = 24;
8393 htab->brlt->reloc_count = 0;
8394 }
8395 r = relocs + htab->brlt->reloc_count;
8396 htab->brlt->reloc_count += 1;
8397 r->r_offset = (br_entry->offset
8398 + htab->brlt->output_offset
8399 + htab->brlt->output_section->vma);
8400 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8401 r->r_addend = off;
8402 }
8403
8404 off = (br_entry->offset
8405 + htab->brlt->output_offset
8406 + htab->brlt->output_section->vma
8407 - elf_gp (htab->brlt->output_section->owner)
8408 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8409
8410 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8411 {
8412 (*_bfd_error_handler)
8413 (_("linkage table error against `%s'"),
8414 stub_entry->root.string);
8415 bfd_set_error (bfd_error_bad_value);
8416 htab->stub_error = TRUE;
8417 return FALSE;
8418 }
8419
8420 indx = off;
8421 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8422 {
8423 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8424 loc += 4;
8425 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8426 size = 16;
8427 }
8428 else
8429 {
8430 bfd_vma r2off;
8431
8432 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8433 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8434 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8435 loc += 4;
8436 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8437 loc += 4;
8438 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8439 loc += 4;
8440 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8441 loc += 4;
8442 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8443 size = 28;
8444 }
8445 loc += 4;
8446 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
8447 loc += 4;
8448 bfd_put_32 (htab->stub_bfd, BCTR, loc);
8449 break;
8450
8451 case ppc_stub_plt_call:
8452 /* Do the best we can for shared libraries built without
8453 exporting ".foo" for each "foo". This can happen when symbol
8454 versioning scripts strip all bar a subset of symbols. */
8455 if (stub_entry->h->oh != NULL
8456 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
8457 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
8458 {
8459 /* Point the symbol at the stub. There may be multiple stubs,
8460 we don't really care; The main thing is to make this sym
8461 defined somewhere. Maybe defining the symbol in the stub
8462 section is a silly idea. If we didn't do this, htab->top_id
8463 could disappear. */
8464 stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8465 stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8466 stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
8467 }
8468
8469 /* Now build the stub. */
8470 off = (bfd_vma) -1;
8471 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8472 if (ent->addend == stub_entry->addend)
8473 {
8474 off = ent->plt.offset;
8475 break;
8476 }
8477 if (off >= (bfd_vma) -2)
8478 abort ();
8479
8480 off &= ~ (bfd_vma) 1;
8481 off += (htab->plt->output_offset
8482 + htab->plt->output_section->vma
8483 - elf_gp (htab->plt->output_section->owner)
8484 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8485
8486 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8487 {
8488 (*_bfd_error_handler)
8489 (_("linkage table error against `%s'"),
8490 stub_entry->h->elf.root.root.string);
8491 bfd_set_error (bfd_error_bad_value);
8492 htab->stub_error = TRUE;
8493 return FALSE;
8494 }
8495
8496 p = build_plt_stub (htab->stub_bfd, loc, off);
8497 size = p - loc;
8498 break;
8499
8500 default:
8501 BFD_FAIL ();
8502 return FALSE;
8503 }
8504
8505 stub_entry->stub_sec->size += size;
8506
8507 if (htab->emit_stub_syms)
8508 {
8509 struct elf_link_hash_entry *h;
8510 size_t len1, len2;
8511 char *name;
8512 const char *const stub_str[] = { "long_branch",
8513 "long_branch_r2off",
8514 "plt_branch",
8515 "plt_branch_r2off",
8516 "plt_call" };
8517
8518 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8519 len2 = strlen (stub_entry->root.string);
8520 name = bfd_malloc (len1 + len2 + 2);
8521 if (name == NULL)
8522 return FALSE;
8523 memcpy (name, stub_entry->root.string, 9);
8524 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8525 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8526 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
8527 if (h == NULL)
8528 return FALSE;
8529 if (h->root.type == bfd_link_hash_new)
8530 {
8531 h->root.type = bfd_link_hash_defined;
8532 h->root.u.def.section = stub_entry->stub_sec;
8533 h->root.u.def.value = stub_entry->stub_offset;
8534 h->ref_regular = 1;
8535 h->def_regular = 1;
8536 h->ref_regular_nonweak = 1;
8537 h->forced_local = 1;
8538 h->non_elf = 0;
8539 }
8540 }
8541
8542 return TRUE;
8543 }
8544
8545 /* As above, but don't actually build the stub. Just bump offset so
8546 we know stub section sizes, and select plt_branch stubs where
8547 long_branch stubs won't do. */
8548
8549 static bfd_boolean
8550 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8551 {
8552 struct ppc_stub_hash_entry *stub_entry;
8553 struct bfd_link_info *info;
8554 struct ppc_link_hash_table *htab;
8555 bfd_vma off;
8556 int size;
8557
8558 /* Massage our args to the form they really have. */
8559 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8560 info = in_arg;
8561
8562 htab = ppc_hash_table (info);
8563
8564 if (stub_entry->stub_type == ppc_stub_plt_call)
8565 {
8566 struct plt_entry *ent;
8567 off = (bfd_vma) -1;
8568 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8569 if (ent->addend == stub_entry->addend)
8570 {
8571 off = ent->plt.offset & ~(bfd_vma) 1;
8572 break;
8573 }
8574 if (off >= (bfd_vma) -2)
8575 abort ();
8576 off += (htab->plt->output_offset
8577 + htab->plt->output_section->vma
8578 - elf_gp (htab->plt->output_section->owner)
8579 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8580
8581 size = PLT_CALL_STUB_SIZE;
8582 if (PPC_HA (off + 16) != PPC_HA (off))
8583 size += 4;
8584 }
8585 else
8586 {
8587 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8588 variants. */
8589 off = (stub_entry->target_value
8590 + stub_entry->target_section->output_offset
8591 + stub_entry->target_section->output_section->vma);
8592 off -= (stub_entry->stub_sec->size
8593 + stub_entry->stub_sec->output_offset
8594 + stub_entry->stub_sec->output_section->vma);
8595
8596 /* Reset the stub type from the plt variant in case we now
8597 can reach with a shorter stub. */
8598 if (stub_entry->stub_type >= ppc_stub_plt_branch)
8599 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8600
8601 size = 4;
8602 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8603 {
8604 off -= 12;
8605 size = 16;
8606 }
8607
8608 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
8609 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8610 {
8611 struct ppc_branch_hash_entry *br_entry;
8612
8613 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8614 stub_entry->root.string + 9,
8615 TRUE, FALSE);
8616 if (br_entry == NULL)
8617 {
8618 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
8619 stub_entry->root.string);
8620 htab->stub_error = TRUE;
8621 return FALSE;
8622 }
8623
8624 if (br_entry->iter != htab->stub_iteration)
8625 {
8626 br_entry->iter = htab->stub_iteration;
8627 br_entry->offset = htab->brlt->size;
8628 htab->brlt->size += 8;
8629
8630 if (htab->relbrlt != NULL)
8631 htab->relbrlt->size += sizeof (Elf64_External_Rela);
8632 else if (info->emitrelocations)
8633 {
8634 htab->brlt->reloc_count += 1;
8635 htab->brlt->flags |= SEC_RELOC;
8636 }
8637 }
8638
8639 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
8640 size = 16;
8641 if (stub_entry->stub_type != ppc_stub_plt_branch)
8642 size = 28;
8643 }
8644 else if (info->emitrelocations)
8645 {
8646 stub_entry->stub_sec->reloc_count += 1;
8647 stub_entry->stub_sec->flags |= SEC_RELOC;
8648 }
8649 }
8650
8651 stub_entry->stub_sec->size += size;
8652 return TRUE;
8653 }
8654
8655 /* Set up various things so that we can make a list of input sections
8656 for each output section included in the link. Returns -1 on error,
8657 0 when no stubs will be needed, and 1 on success. */
8658
8659 int
8660 ppc64_elf_setup_section_lists (bfd *output_bfd,
8661 struct bfd_link_info *info,
8662 int no_multi_toc)
8663 {
8664 bfd *input_bfd;
8665 int top_id, top_index, id;
8666 asection *section;
8667 asection **input_list;
8668 bfd_size_type amt;
8669 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8670
8671 htab->no_multi_toc = no_multi_toc;
8672
8673 if (htab->brlt == NULL)
8674 return 0;
8675
8676 /* Find the top input section id. */
8677 for (input_bfd = info->input_bfds, top_id = 3;
8678 input_bfd != NULL;
8679 input_bfd = input_bfd->link_next)
8680 {
8681 for (section = input_bfd->sections;
8682 section != NULL;
8683 section = section->next)
8684 {
8685 if (top_id < section->id)
8686 top_id = section->id;
8687 }
8688 }
8689
8690 htab->top_id = top_id;
8691 amt = sizeof (struct map_stub) * (top_id + 1);
8692 htab->stub_group = bfd_zmalloc (amt);
8693 if (htab->stub_group == NULL)
8694 return -1;
8695
8696 /* Set toc_off for com, und, abs and ind sections. */
8697 for (id = 0; id < 3; id++)
8698 htab->stub_group[id].toc_off = TOC_BASE_OFF;
8699
8700 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
8701
8702 /* We can't use output_bfd->section_count here to find the top output
8703 section index as some sections may have been removed, and
8704 strip_excluded_output_sections doesn't renumber the indices. */
8705 for (section = output_bfd->sections, top_index = 0;
8706 section != NULL;
8707 section = section->next)
8708 {
8709 if (top_index < section->index)
8710 top_index = section->index;
8711 }
8712
8713 htab->top_index = top_index;
8714 amt = sizeof (asection *) * (top_index + 1);
8715 input_list = bfd_zmalloc (amt);
8716 htab->input_list = input_list;
8717 if (input_list == NULL)
8718 return -1;
8719
8720 return 1;
8721 }
8722
8723 /* The linker repeatedly calls this function for each TOC input section
8724 and linker generated GOT section. Group input bfds such that the toc
8725 within a group is less than 64k in size. Will break with cute linker
8726 scripts that play games with dot in the output toc section. */
8727
8728 void
8729 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
8730 {
8731 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8732
8733 if (!htab->no_multi_toc)
8734 {
8735 bfd_vma addr = isec->output_offset + isec->output_section->vma;
8736 bfd_vma off = addr - htab->toc_curr;
8737
8738 if (off + isec->size > 0x10000)
8739 htab->toc_curr = addr;
8740
8741 elf_gp (isec->owner) = (htab->toc_curr
8742 - elf_gp (isec->output_section->owner)
8743 + TOC_BASE_OFF);
8744 }
8745 }
8746
8747 /* Called after the last call to the above function. */
8748
8749 void
8750 ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
8751 {
8752 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8753
8754 htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8755
8756 /* toc_curr tracks the TOC offset used for code sections below in
8757 ppc64_elf_next_input_section. Start off at 0x8000. */
8758 htab->toc_curr = TOC_BASE_OFF;
8759 }
8760
8761 /* No toc references were found in ISEC. If the code in ISEC makes no
8762 calls, then there's no need to use toc adjusting stubs when branching
8763 into ISEC. Actually, indirect calls from ISEC are OK as they will
8764 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
8765 needed, and 2 if a cyclical call-graph was found but no other reason
8766 for a stub was detected. If called from the top level, a return of
8767 2 means the same as a return of 0. */
8768
8769 static int
8770 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
8771 {
8772 Elf_Internal_Rela *relstart, *rel;
8773 Elf_Internal_Sym *local_syms;
8774 int ret;
8775 struct ppc_link_hash_table *htab;
8776
8777 /* We know none of our code bearing sections will need toc stubs. */
8778 if ((isec->flags & SEC_LINKER_CREATED) != 0)
8779 return 0;
8780
8781 if (isec->size == 0)
8782 return 0;
8783
8784 if (isec->output_section == NULL)
8785 return 0;
8786
8787 /* Hack for linux kernel. .fixup contains branches, but only back to
8788 the function that hit an exception. */
8789 if (strcmp (isec->name, ".fixup") == 0)
8790 return 0;
8791
8792 if (isec->reloc_count == 0)
8793 return 0;
8794
8795 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8796 info->keep_memory);
8797 if (relstart == NULL)
8798 return -1;
8799
8800 /* Look for branches to outside of this section. */
8801 local_syms = NULL;
8802 ret = 0;
8803 htab = ppc_hash_table (info);
8804 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
8805 {
8806 enum elf_ppc64_reloc_type r_type;
8807 unsigned long r_symndx;
8808 struct elf_link_hash_entry *h;
8809 Elf_Internal_Sym *sym;
8810 asection *sym_sec;
8811 long *opd_adjust;
8812 bfd_vma sym_value;
8813 bfd_vma dest;
8814
8815 r_type = ELF64_R_TYPE (rel->r_info);
8816 if (r_type != R_PPC64_REL24
8817 && r_type != R_PPC64_REL14
8818 && r_type != R_PPC64_REL14_BRTAKEN
8819 && r_type != R_PPC64_REL14_BRNTAKEN)
8820 continue;
8821
8822 r_symndx = ELF64_R_SYM (rel->r_info);
8823 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8824 isec->owner))
8825 {
8826 ret = -1;
8827 break;
8828 }
8829
8830 /* Calls to dynamic lib functions go through a plt call stub
8831 that uses r2. Branches to undefined symbols might be a call
8832 using old-style dot symbols that can be satisfied by a plt
8833 call into a new-style dynamic library. */
8834 if (sym_sec == NULL)
8835 {
8836 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8837 if (eh != NULL
8838 && eh->oh != NULL
8839 && eh->oh->elf.plt.plist != NULL)
8840 {
8841 ret = 1;
8842 break;
8843 }
8844
8845 /* Ignore other undefined symbols. */
8846 continue;
8847 }
8848
8849 /* Assume branches to other sections not included in the link need
8850 stubs too, to cover -R and absolute syms. */
8851 if (sym_sec->output_section == NULL)
8852 {
8853 ret = 1;
8854 break;
8855 }
8856
8857 if (h == NULL)
8858 sym_value = sym->st_value;
8859 else
8860 {
8861 if (h->root.type != bfd_link_hash_defined
8862 && h->root.type != bfd_link_hash_defweak)
8863 abort ();
8864 sym_value = h->root.u.def.value;
8865 }
8866 sym_value += rel->r_addend;
8867
8868 /* If this branch reloc uses an opd sym, find the code section. */
8869 opd_adjust = get_opd_info (sym_sec);
8870 if (opd_adjust != NULL)
8871 {
8872 if (h == NULL)
8873 {
8874 long adjust;
8875
8876 adjust = opd_adjust[sym->st_value / 8];
8877 if (adjust == -1)
8878 /* Assume deleted functions won't ever be called. */
8879 continue;
8880 sym_value += adjust;
8881 }
8882
8883 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8884 if (dest == (bfd_vma) -1)
8885 continue;
8886 }
8887 else
8888 dest = (sym_value
8889 + sym_sec->output_offset
8890 + sym_sec->output_section->vma);
8891
8892 /* Ignore branch to self. */
8893 if (sym_sec == isec)
8894 continue;
8895
8896 /* If the called function uses the toc, we need a stub. */
8897 if (sym_sec->has_toc_reloc
8898 || sym_sec->makes_toc_func_call)
8899 {
8900 ret = 1;
8901 break;
8902 }
8903
8904 /* Assume any branch that needs a long branch stub might in fact
8905 need a plt_branch stub. A plt_branch stub uses r2. */
8906 else if (dest - (isec->output_offset
8907 + isec->output_section->vma
8908 + rel->r_offset) + (1 << 25) >= (2 << 25))
8909 {
8910 ret = 1;
8911 break;
8912 }
8913
8914 /* If calling back to a section in the process of being tested, we
8915 can't say for sure that no toc adjusting stubs are needed, so
8916 don't return zero. */
8917 else if (sym_sec->call_check_in_progress)
8918 ret = 2;
8919
8920 /* Branches to another section that itself doesn't have any TOC
8921 references are OK. Recursively call ourselves to check. */
8922 else if (sym_sec->id <= htab->top_id
8923 && htab->stub_group[sym_sec->id].toc_off == 0)
8924 {
8925 int recur;
8926
8927 /* Mark current section as indeterminate, so that other
8928 sections that call back to current won't be marked as
8929 known. */
8930 isec->call_check_in_progress = 1;
8931 recur = toc_adjusting_stub_needed (info, sym_sec);
8932 isec->call_check_in_progress = 0;
8933
8934 if (recur < 0)
8935 {
8936 /* An error. Exit. */
8937 ret = -1;
8938 break;
8939 }
8940 else if (recur <= 1)
8941 {
8942 /* Known result. Mark as checked and set section flag. */
8943 htab->stub_group[sym_sec->id].toc_off = 1;
8944 if (recur != 0)
8945 {
8946 sym_sec->makes_toc_func_call = 1;
8947 ret = 1;
8948 break;
8949 }
8950 }
8951 else
8952 {
8953 /* Unknown result. Continue checking. */
8954 ret = 2;
8955 }
8956 }
8957 }
8958
8959 if (local_syms != NULL
8960 && (elf_tdata (isec->owner)->symtab_hdr.contents
8961 != (unsigned char *) local_syms))
8962 free (local_syms);
8963 if (elf_section_data (isec)->relocs != relstart)
8964 free (relstart);
8965
8966 return ret;
8967 }
8968
8969 /* The linker repeatedly calls this function for each input section,
8970 in the order that input sections are linked into output sections.
8971 Build lists of input sections to determine groupings between which
8972 we may insert linker stubs. */
8973
8974 bfd_boolean
8975 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
8976 {
8977 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8978
8979 if ((isec->output_section->flags & SEC_CODE) != 0
8980 && isec->output_section->index <= htab->top_index)
8981 {
8982 asection **list = htab->input_list + isec->output_section->index;
8983 /* Steal the link_sec pointer for our list. */
8984 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
8985 /* This happens to make the list in reverse order,
8986 which is what we want. */
8987 PREV_SEC (isec) = *list;
8988 *list = isec;
8989 }
8990
8991 if (htab->multi_toc_needed)
8992 {
8993 /* If a code section has a function that uses the TOC then we need
8994 to use the right TOC (obviously). Also, make sure that .opd gets
8995 the correct TOC value for R_PPC64_TOC relocs that don't have or
8996 can't find their function symbol (shouldn't ever happen now). */
8997 if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8998 {
8999 if (elf_gp (isec->owner) != 0)
9000 htab->toc_curr = elf_gp (isec->owner);
9001 }
9002 else if (htab->stub_group[isec->id].toc_off == 0)
9003 {
9004 int ret = toc_adjusting_stub_needed (info, isec);
9005 if (ret < 0)
9006 return FALSE;
9007 else
9008 isec->makes_toc_func_call = ret & 1;
9009 }
9010 }
9011
9012 /* Functions that don't use the TOC can belong in any TOC group.
9013 Use the last TOC base. This happens to make _init and _fini
9014 pasting work. */
9015 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9016 return TRUE;
9017 }
9018
9019 /* See whether we can group stub sections together. Grouping stub
9020 sections may result in fewer stubs. More importantly, we need to
9021 put all .init* and .fini* stubs at the beginning of the .init or
9022 .fini output sections respectively, because glibc splits the
9023 _init and _fini functions into multiple parts. Putting a stub in
9024 the middle of a function is not a good idea. */
9025
9026 static void
9027 group_sections (struct ppc_link_hash_table *htab,
9028 bfd_size_type stub_group_size,
9029 bfd_boolean stubs_always_before_branch)
9030 {
9031 asection **list;
9032 bfd_size_type stub14_group_size;
9033 bfd_boolean suppress_size_errors;
9034
9035 suppress_size_errors = FALSE;
9036 stub14_group_size = stub_group_size;
9037 if (stub_group_size == 1)
9038 {
9039 /* Default values. */
9040 if (stubs_always_before_branch)
9041 {
9042 stub_group_size = 0x1e00000;
9043 stub14_group_size = 0x7800;
9044 }
9045 else
9046 {
9047 stub_group_size = 0x1c00000;
9048 stub14_group_size = 0x7000;
9049 }
9050 suppress_size_errors = TRUE;
9051 }
9052
9053 list = htab->input_list + htab->top_index;
9054 do
9055 {
9056 asection *tail = *list;
9057 while (tail != NULL)
9058 {
9059 asection *curr;
9060 asection *prev;
9061 bfd_size_type total;
9062 bfd_boolean big_sec;
9063 bfd_vma curr_toc;
9064
9065 curr = tail;
9066 total = tail->size;
9067 big_sec = total > (ppc64_elf_section_data (tail)->has_14bit_branch
9068 ? stub14_group_size : stub_group_size);
9069 if (big_sec && !suppress_size_errors)
9070 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
9071 tail->owner, tail);
9072 curr_toc = htab->stub_group[tail->id].toc_off;
9073
9074 while ((prev = PREV_SEC (curr)) != NULL
9075 && ((total += curr->output_offset - prev->output_offset)
9076 < (ppc64_elf_section_data (prev)->has_14bit_branch
9077 ? stub14_group_size : stub_group_size))
9078 && htab->stub_group[prev->id].toc_off == curr_toc)
9079 curr = prev;
9080
9081 /* OK, the size from the start of CURR to the end is less
9082 than stub_group_size and thus can be handled by one stub
9083 section. (or the tail section is itself larger than
9084 stub_group_size, in which case we may be toast.) We
9085 should really be keeping track of the total size of stubs
9086 added here, as stubs contribute to the final output
9087 section size. That's a little tricky, and this way will
9088 only break if stubs added make the total size more than
9089 2^25, ie. for the default stub_group_size, if stubs total
9090 more than 2097152 bytes, or nearly 75000 plt call stubs. */
9091 do
9092 {
9093 prev = PREV_SEC (tail);
9094 /* Set up this stub group. */
9095 htab->stub_group[tail->id].link_sec = curr;
9096 }
9097 while (tail != curr && (tail = prev) != NULL);
9098
9099 /* But wait, there's more! Input sections up to stub_group_size
9100 bytes before the stub section can be handled by it too.
9101 Don't do this if we have a really large section after the
9102 stubs, as adding more stubs increases the chance that
9103 branches may not reach into the stub section. */
9104 if (!stubs_always_before_branch && !big_sec)
9105 {
9106 total = 0;
9107 while (prev != NULL
9108 && ((total += tail->output_offset - prev->output_offset)
9109 < (ppc64_elf_section_data (prev)->has_14bit_branch
9110 ? stub14_group_size : stub_group_size))
9111 && htab->stub_group[prev->id].toc_off == curr_toc)
9112 {
9113 tail = prev;
9114 prev = PREV_SEC (tail);
9115 htab->stub_group[tail->id].link_sec = curr;
9116 }
9117 }
9118 tail = prev;
9119 }
9120 }
9121 while (list-- != htab->input_list);
9122 free (htab->input_list);
9123 #undef PREV_SEC
9124 }
9125
9126 /* Determine and set the size of the stub section for a final link.
9127
9128 The basic idea here is to examine all the relocations looking for
9129 PC-relative calls to a target that is unreachable with a "bl"
9130 instruction. */
9131
9132 bfd_boolean
9133 ppc64_elf_size_stubs (bfd *output_bfd,
9134 struct bfd_link_info *info,
9135 bfd_signed_vma group_size,
9136 asection *(*add_stub_section) (const char *, asection *),
9137 void (*layout_sections_again) (void))
9138 {
9139 bfd_size_type stub_group_size;
9140 bfd_boolean stubs_always_before_branch;
9141 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9142
9143 /* Stash our params away. */
9144 htab->add_stub_section = add_stub_section;
9145 htab->layout_sections_again = layout_sections_again;
9146 stubs_always_before_branch = group_size < 0;
9147 if (group_size < 0)
9148 stub_group_size = -group_size;
9149 else
9150 stub_group_size = group_size;
9151
9152 group_sections (htab, stub_group_size, stubs_always_before_branch);
9153
9154 while (1)
9155 {
9156 bfd *input_bfd;
9157 unsigned int bfd_indx;
9158 asection *stub_sec;
9159
9160 htab->stub_iteration += 1;
9161
9162 for (input_bfd = info->input_bfds, bfd_indx = 0;
9163 input_bfd != NULL;
9164 input_bfd = input_bfd->link_next, bfd_indx++)
9165 {
9166 Elf_Internal_Shdr *symtab_hdr;
9167 asection *section;
9168 Elf_Internal_Sym *local_syms = NULL;
9169
9170 if (!is_ppc64_elf_target (input_bfd->xvec))
9171 continue;
9172
9173 /* We'll need the symbol table in a second. */
9174 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9175 if (symtab_hdr->sh_info == 0)
9176 continue;
9177
9178 /* Walk over each section attached to the input bfd. */
9179 for (section = input_bfd->sections;
9180 section != NULL;
9181 section = section->next)
9182 {
9183 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
9184
9185 /* If there aren't any relocs, then there's nothing more
9186 to do. */
9187 if ((section->flags & SEC_RELOC) == 0
9188 || (section->flags & SEC_ALLOC) == 0
9189 || (section->flags & SEC_LOAD) == 0
9190 || (section->flags & SEC_CODE) == 0
9191 || section->reloc_count == 0)
9192 continue;
9193
9194 /* If this section is a link-once section that will be
9195 discarded, then don't create any stubs. */
9196 if (section->output_section == NULL
9197 || section->output_section->owner != output_bfd)
9198 continue;
9199
9200 /* Get the relocs. */
9201 internal_relocs
9202 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
9203 info->keep_memory);
9204 if (internal_relocs == NULL)
9205 goto error_ret_free_local;
9206
9207 /* Now examine each relocation. */
9208 irela = internal_relocs;
9209 irelaend = irela + section->reloc_count;
9210 for (; irela < irelaend; irela++)
9211 {
9212 enum elf_ppc64_reloc_type r_type;
9213 unsigned int r_indx;
9214 enum ppc_stub_type stub_type;
9215 struct ppc_stub_hash_entry *stub_entry;
9216 asection *sym_sec, *code_sec;
9217 bfd_vma sym_value;
9218 bfd_vma destination;
9219 bfd_boolean ok_dest;
9220 struct ppc_link_hash_entry *hash;
9221 struct ppc_link_hash_entry *fdh;
9222 struct elf_link_hash_entry *h;
9223 Elf_Internal_Sym *sym;
9224 char *stub_name;
9225 const asection *id_sec;
9226 long *opd_adjust;
9227
9228 r_type = ELF64_R_TYPE (irela->r_info);
9229 r_indx = ELF64_R_SYM (irela->r_info);
9230
9231 if (r_type >= R_PPC64_max)
9232 {
9233 bfd_set_error (bfd_error_bad_value);
9234 goto error_ret_free_internal;
9235 }
9236
9237 /* Only look for stubs on branch instructions. */
9238 if (r_type != R_PPC64_REL24
9239 && r_type != R_PPC64_REL14
9240 && r_type != R_PPC64_REL14_BRTAKEN
9241 && r_type != R_PPC64_REL14_BRNTAKEN)
9242 continue;
9243
9244 /* Now determine the call target, its name, value,
9245 section. */
9246 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9247 r_indx, input_bfd))
9248 goto error_ret_free_internal;
9249 hash = (struct ppc_link_hash_entry *) h;
9250
9251 ok_dest = FALSE;
9252 fdh = NULL;
9253 sym_value = 0;
9254 if (hash == NULL)
9255 {
9256 sym_value = sym->st_value;
9257 ok_dest = TRUE;
9258 }
9259 else if (hash->elf.root.type == bfd_link_hash_defined
9260 || hash->elf.root.type == bfd_link_hash_defweak)
9261 {
9262 sym_value = hash->elf.root.u.def.value;
9263 if (sym_sec->output_section != NULL)
9264 ok_dest = TRUE;
9265 }
9266 else if (hash->elf.root.type == bfd_link_hash_undefweak
9267 || hash->elf.root.type == bfd_link_hash_undefined)
9268 {
9269 /* Recognise an old ABI func code entry sym, and
9270 use the func descriptor sym instead if it is
9271 defined. */
9272 if (hash->elf.root.root.string[0] == '.'
9273 && (fdh = get_fdh (hash, htab)) != NULL)
9274 {
9275 if (fdh->elf.root.type == bfd_link_hash_defined
9276 || fdh->elf.root.type == bfd_link_hash_defweak)
9277 {
9278 sym_sec = fdh->elf.root.u.def.section;
9279 sym_value = fdh->elf.root.u.def.value;
9280 if (sym_sec->output_section != NULL)
9281 ok_dest = TRUE;
9282 }
9283 else
9284 fdh = NULL;
9285 }
9286 }
9287 else
9288 {
9289 bfd_set_error (bfd_error_bad_value);
9290 goto error_ret_free_internal;
9291 }
9292
9293 destination = 0;
9294 if (ok_dest)
9295 {
9296 sym_value += irela->r_addend;
9297 destination = (sym_value
9298 + sym_sec->output_offset
9299 + sym_sec->output_section->vma);
9300 }
9301
9302 code_sec = sym_sec;
9303 opd_adjust = get_opd_info (sym_sec);
9304 if (opd_adjust != NULL)
9305 {
9306 bfd_vma dest;
9307
9308 if (hash == NULL)
9309 {
9310 long adjust = opd_adjust[sym_value / 8];
9311 if (adjust == -1)
9312 continue;
9313 sym_value += adjust;
9314 }
9315 dest = opd_entry_value (sym_sec, sym_value,
9316 &code_sec, &sym_value);
9317 if (dest != (bfd_vma) -1)
9318 {
9319 destination = dest;
9320 if (fdh != NULL)
9321 {
9322 /* Fixup old ABI sym to point at code
9323 entry. */
9324 hash->elf.root.type = bfd_link_hash_defweak;
9325 hash->elf.root.u.def.section = code_sec;
9326 hash->elf.root.u.def.value = sym_value;
9327 }
9328 }
9329 }
9330
9331 /* Determine what (if any) linker stub is needed. */
9332 stub_type = ppc_type_of_stub (section, irela, &hash,
9333 destination);
9334
9335 if (stub_type != ppc_stub_plt_call)
9336 {
9337 /* Check whether we need a TOC adjusting stub.
9338 Since the linker pastes together pieces from
9339 different object files when creating the
9340 _init and _fini functions, it may be that a
9341 call to what looks like a local sym is in
9342 fact a call needing a TOC adjustment. */
9343 if (code_sec != NULL
9344 && code_sec->output_section != NULL
9345 && (htab->stub_group[code_sec->id].toc_off
9346 != htab->stub_group[section->id].toc_off)
9347 && (code_sec->has_toc_reloc
9348 || code_sec->makes_toc_func_call))
9349 stub_type = ppc_stub_long_branch_r2off;
9350 }
9351
9352 if (stub_type == ppc_stub_none)
9353 continue;
9354
9355 /* __tls_get_addr calls might be eliminated. */
9356 if (stub_type != ppc_stub_plt_call
9357 && hash != NULL
9358 && (hash == htab->tls_get_addr
9359 || hash == htab->tls_get_addr_fd)
9360 && section->has_tls_reloc
9361 && irela != internal_relocs)
9362 {
9363 /* Get tls info. */
9364 char *tls_mask;
9365
9366 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
9367 irela - 1, input_bfd))
9368 goto error_ret_free_internal;
9369 if (*tls_mask != 0)
9370 continue;
9371 }
9372
9373 /* Support for grouping stub sections. */
9374 id_sec = htab->stub_group[section->id].link_sec;
9375
9376 /* Get the name of this stub. */
9377 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9378 if (!stub_name)
9379 goto error_ret_free_internal;
9380
9381 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
9382 stub_name, FALSE, FALSE);
9383 if (stub_entry != NULL)
9384 {
9385 /* The proper stub has already been created. */
9386 free (stub_name);
9387 continue;
9388 }
9389
9390 stub_entry = ppc_add_stub (stub_name, section, htab);
9391 if (stub_entry == NULL)
9392 {
9393 free (stub_name);
9394 error_ret_free_internal:
9395 if (elf_section_data (section)->relocs == NULL)
9396 free (internal_relocs);
9397 error_ret_free_local:
9398 if (local_syms != NULL
9399 && (symtab_hdr->contents
9400 != (unsigned char *) local_syms))
9401 free (local_syms);
9402 return FALSE;
9403 }
9404
9405 stub_entry->stub_type = stub_type;
9406 stub_entry->target_value = sym_value;
9407 stub_entry->target_section = code_sec;
9408 stub_entry->h = hash;
9409 stub_entry->addend = irela->r_addend;
9410
9411 if (stub_entry->h != NULL)
9412 htab->stub_globals += 1;
9413 }
9414
9415 /* We're done with the internal relocs, free them. */
9416 if (elf_section_data (section)->relocs != internal_relocs)
9417 free (internal_relocs);
9418 }
9419
9420 if (local_syms != NULL
9421 && symtab_hdr->contents != (unsigned char *) local_syms)
9422 {
9423 if (!info->keep_memory)
9424 free (local_syms);
9425 else
9426 symtab_hdr->contents = (unsigned char *) local_syms;
9427 }
9428 }
9429
9430 /* We may have added some stubs. Find out the new size of the
9431 stub sections. */
9432 for (stub_sec = htab->stub_bfd->sections;
9433 stub_sec != NULL;
9434 stub_sec = stub_sec->next)
9435 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9436 {
9437 stub_sec->rawsize = stub_sec->size;
9438 stub_sec->size = 0;
9439 stub_sec->reloc_count = 0;
9440 stub_sec->flags &= ~SEC_RELOC;
9441 }
9442
9443 htab->brlt->size = 0;
9444 htab->brlt->reloc_count = 0;
9445 htab->brlt->flags &= ~SEC_RELOC;
9446 if (htab->relbrlt != NULL)
9447 htab->relbrlt->size = 0;
9448
9449 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
9450
9451 for (stub_sec = htab->stub_bfd->sections;
9452 stub_sec != NULL;
9453 stub_sec = stub_sec->next)
9454 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9455 && stub_sec->rawsize != stub_sec->size)
9456 break;
9457
9458 /* Exit from this loop when no stubs have been added, and no stubs
9459 have changed size. */
9460 if (stub_sec == NULL)
9461 break;
9462
9463 /* Ask the linker to do its stuff. */
9464 (*htab->layout_sections_again) ();
9465 }
9466
9467 /* It would be nice to strip htab->brlt from the output if the
9468 section is empty, but it's too late. If we strip sections here,
9469 the dynamic symbol table is corrupted since the section symbol
9470 for the stripped section isn't written. */
9471
9472 return TRUE;
9473 }
9474
9475 /* Called after we have determined section placement. If sections
9476 move, we'll be called again. Provide a value for TOCstart. */
9477
9478 bfd_vma
9479 ppc64_elf_toc (bfd *obfd)
9480 {
9481 asection *s;
9482 bfd_vma TOCstart;
9483
9484 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9485 order. The TOC starts where the first of these sections starts. */
9486 s = bfd_get_section_by_name (obfd, ".got");
9487 if (s == NULL)
9488 s = bfd_get_section_by_name (obfd, ".toc");
9489 if (s == NULL)
9490 s = bfd_get_section_by_name (obfd, ".tocbss");
9491 if (s == NULL)
9492 s = bfd_get_section_by_name (obfd, ".plt");
9493 if (s == NULL)
9494 {
9495 /* This may happen for
9496 o references to TOC base (SYM@toc / TOC[tc0]) without a
9497 .toc directive
9498 o bad linker script
9499 o --gc-sections and empty TOC sections
9500
9501 FIXME: Warn user? */
9502
9503 /* Look for a likely section. We probably won't even be
9504 using TOCstart. */
9505 for (s = obfd->sections; s != NULL; s = s->next)
9506 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9507 == (SEC_ALLOC | SEC_SMALL_DATA))
9508 break;
9509 if (s == NULL)
9510 for (s = obfd->sections; s != NULL; s = s->next)
9511 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9512 == (SEC_ALLOC | SEC_SMALL_DATA))
9513 break;
9514 if (s == NULL)
9515 for (s = obfd->sections; s != NULL; s = s->next)
9516 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9517 break;
9518 if (s == NULL)
9519 for (s = obfd->sections; s != NULL; s = s->next)
9520 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9521 break;
9522 }
9523
9524 TOCstart = 0;
9525 if (s != NULL)
9526 TOCstart = s->output_section->vma + s->output_offset;
9527
9528 return TOCstart;
9529 }
9530
9531 /* Build all the stubs associated with the current output file.
9532 The stubs are kept in a hash table attached to the main linker
9533 hash table. This function is called via gldelf64ppc_finish. */
9534
9535 bfd_boolean
9536 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9537 struct bfd_link_info *info,
9538 char **stats)
9539 {
9540 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9541 asection *stub_sec;
9542 bfd_byte *p;
9543 int stub_sec_count = 0;
9544
9545 htab->emit_stub_syms = emit_stub_syms;
9546
9547 /* Allocate memory to hold the linker stubs. */
9548 for (stub_sec = htab->stub_bfd->sections;
9549 stub_sec != NULL;
9550 stub_sec = stub_sec->next)
9551 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9552 && stub_sec->size != 0)
9553 {
9554 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9555 if (stub_sec->contents == NULL)
9556 return FALSE;
9557 /* We want to check that built size is the same as calculated
9558 size. rawsize is a convenient location to use. */
9559 stub_sec->rawsize = stub_sec->size;
9560 stub_sec->size = 0;
9561 }
9562
9563 if (htab->glink != NULL && htab->glink->size != 0)
9564 {
9565 unsigned int indx;
9566 bfd_vma plt0;
9567
9568 /* Build the .glink plt call stub. */
9569 if (htab->emit_stub_syms)
9570 {
9571 struct elf_link_hash_entry *h;
9572 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9573 if (h == NULL)
9574 return FALSE;
9575 if (h->root.type == bfd_link_hash_new)
9576 {
9577 h->root.type = bfd_link_hash_defined;
9578 h->root.u.def.section = htab->glink;
9579 h->root.u.def.value = 8;
9580 h->ref_regular = 1;
9581 h->def_regular = 1;
9582 h->ref_regular_nonweak = 1;
9583 h->forced_local = 1;
9584 h->non_elf = 0;
9585 }
9586 }
9587 p = htab->glink->contents;
9588 plt0 = (htab->plt->output_section->vma
9589 + htab->plt->output_offset
9590 - (htab->glink->output_section->vma
9591 + htab->glink->output_offset
9592 + 16));
9593 bfd_put_64 (htab->glink->owner, plt0, p);
9594 p += 8;
9595 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
9596 p += 4;
9597 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
9598 p += 4;
9599 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
9600 p += 4;
9601 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
9602 p += 4;
9603 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
9604 p += 4;
9605 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
9606 p += 4;
9607 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
9608 p += 4;
9609 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
9610 p += 4;
9611 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
9612 p += 4;
9613 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
9614 p += 4;
9615 bfd_put_32 (htab->glink->owner, BCTR, p);
9616 p += 4;
9617 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
9618 {
9619 bfd_put_32 (htab->glink->owner, NOP, p);
9620 p += 4;
9621 }
9622
9623 /* Build the .glink lazy link call stubs. */
9624 indx = 0;
9625 while (p < htab->glink->contents + htab->glink->size)
9626 {
9627 if (indx < 0x8000)
9628 {
9629 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9630 p += 4;
9631 }
9632 else
9633 {
9634 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9635 p += 4;
9636 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9637 p += 4;
9638 }
9639 bfd_put_32 (htab->glink->owner,
9640 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
9641 indx++;
9642 p += 4;
9643 }
9644 htab->glink->rawsize = p - htab->glink->contents;
9645 }
9646
9647 if (htab->brlt->size != 0)
9648 {
9649 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
9650 htab->brlt->size);
9651 if (htab->brlt->contents == NULL)
9652 return FALSE;
9653 }
9654 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
9655 {
9656 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
9657 htab->relbrlt->size);
9658 if (htab->relbrlt->contents == NULL)
9659 return FALSE;
9660 }
9661
9662 /* Build the stubs as directed by the stub hash table. */
9663 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
9664
9665 if (htab->relbrlt != NULL)
9666 htab->relbrlt->reloc_count = 0;
9667
9668 for (stub_sec = htab->stub_bfd->sections;
9669 stub_sec != NULL;
9670 stub_sec = stub_sec->next)
9671 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9672 {
9673 stub_sec_count += 1;
9674 if (stub_sec->rawsize != stub_sec->size)
9675 break;
9676 }
9677
9678 if (stub_sec != NULL
9679 || htab->glink->rawsize != htab->glink->size)
9680 {
9681 htab->stub_error = TRUE;
9682 (*_bfd_error_handler) (_("stubs don't match calculated size"));
9683 }
9684
9685 if (htab->stub_error)
9686 return FALSE;
9687
9688 if (stats != NULL)
9689 {
9690 *stats = bfd_malloc (500);
9691 if (*stats == NULL)
9692 return FALSE;
9693
9694 sprintf (*stats, _("linker stubs in %u group%s\n"
9695 " branch %lu\n"
9696 " toc adjust %lu\n"
9697 " long branch %lu\n"
9698 " long toc adj %lu\n"
9699 " plt call %lu"),
9700 stub_sec_count,
9701 stub_sec_count == 1 ? "" : "s",
9702 htab->stub_count[ppc_stub_long_branch - 1],
9703 htab->stub_count[ppc_stub_long_branch_r2off - 1],
9704 htab->stub_count[ppc_stub_plt_branch - 1],
9705 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9706 htab->stub_count[ppc_stub_plt_call - 1]);
9707 }
9708 return TRUE;
9709 }
9710
9711 /* This function undoes the changes made by add_symbol_adjust. */
9712
9713 static bfd_boolean
9714 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9715 {
9716 struct ppc_link_hash_entry *eh;
9717
9718 if (h->root.type == bfd_link_hash_indirect)
9719 return TRUE;
9720
9721 if (h->root.type == bfd_link_hash_warning)
9722 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9723
9724 eh = (struct ppc_link_hash_entry *) h;
9725 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9726 return TRUE;
9727
9728 eh->elf.root.type = bfd_link_hash_undefined;
9729 return TRUE;
9730 }
9731
9732 void
9733 ppc64_elf_restore_symbols (struct bfd_link_info *info)
9734 {
9735 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9736 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9737 }
9738
9739 /* What to do when ld finds relocations against symbols defined in
9740 discarded sections. */
9741
9742 static unsigned int
9743 ppc64_elf_action_discarded (asection *sec)
9744 {
9745 if (strcmp (".opd", sec->name) == 0)
9746 return 0;
9747
9748 if (strcmp (".toc", sec->name) == 0)
9749 return 0;
9750
9751 if (strcmp (".toc1", sec->name) == 0)
9752 return 0;
9753
9754 return _bfd_elf_default_action_discarded (sec);
9755 }
9756
9757 /* The RELOCATE_SECTION function is called by the ELF backend linker
9758 to handle the relocations for a section.
9759
9760 The relocs are always passed as Rela structures; if the section
9761 actually uses Rel structures, the r_addend field will always be
9762 zero.
9763
9764 This function is responsible for adjust the section contents as
9765 necessary, and (if using Rela relocs and generating a
9766 relocatable output file) adjusting the reloc addend as
9767 necessary.
9768
9769 This function does not have to worry about setting the reloc
9770 address or the reloc symbol index.
9771
9772 LOCAL_SYMS is a pointer to the swapped in local symbols.
9773
9774 LOCAL_SECTIONS is an array giving the section in the input file
9775 corresponding to the st_shndx field of each local symbol.
9776
9777 The global hash table entry for the global symbols can be found
9778 via elf_sym_hashes (input_bfd).
9779
9780 When generating relocatable output, this function must handle
9781 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
9782 going to be the section symbol corresponding to the output
9783 section, which means that the addend must be adjusted
9784 accordingly. */
9785
9786 static bfd_boolean
9787 ppc64_elf_relocate_section (bfd *output_bfd,
9788 struct bfd_link_info *info,
9789 bfd *input_bfd,
9790 asection *input_section,
9791 bfd_byte *contents,
9792 Elf_Internal_Rela *relocs,
9793 Elf_Internal_Sym *local_syms,
9794 asection **local_sections)
9795 {
9796 struct ppc_link_hash_table *htab;
9797 Elf_Internal_Shdr *symtab_hdr;
9798 struct elf_link_hash_entry **sym_hashes;
9799 Elf_Internal_Rela *rel;
9800 Elf_Internal_Rela *relend;
9801 Elf_Internal_Rela outrel;
9802 bfd_byte *loc;
9803 struct got_entry **local_got_ents;
9804 bfd_vma TOCstart;
9805 bfd_boolean ret = TRUE;
9806 bfd_boolean is_opd;
9807 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
9808 bfd_boolean is_power4 = FALSE;
9809 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
9810
9811 /* Initialize howto table if needed. */
9812 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9813 ppc_howto_init ();
9814
9815 htab = ppc_hash_table (info);
9816
9817 /* Don't relocate stub sections. */
9818 if (input_section->owner == htab->stub_bfd)
9819 return TRUE;
9820
9821 local_got_ents = elf_local_got_ents (input_bfd);
9822 TOCstart = elf_gp (output_bfd);
9823 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9824 sym_hashes = elf_sym_hashes (input_bfd);
9825 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
9826
9827 rel = relocs;
9828 relend = relocs + input_section->reloc_count;
9829 for (; rel < relend; rel++)
9830 {
9831 enum elf_ppc64_reloc_type r_type;
9832 bfd_vma addend, orig_addend;
9833 bfd_reloc_status_type r;
9834 Elf_Internal_Sym *sym;
9835 asection *sec;
9836 struct elf_link_hash_entry *h_elf;
9837 struct ppc_link_hash_entry *h;
9838 struct ppc_link_hash_entry *fdh;
9839 const char *sym_name;
9840 unsigned long r_symndx, toc_symndx;
9841 char tls_mask, tls_gd, tls_type;
9842 char sym_type;
9843 bfd_vma relocation;
9844 bfd_boolean unresolved_reloc;
9845 bfd_boolean warned;
9846 unsigned long insn, mask;
9847 struct ppc_stub_hash_entry *stub_entry;
9848 bfd_vma max_br_offset;
9849 bfd_vma from;
9850
9851 r_type = ELF64_R_TYPE (rel->r_info);
9852 r_symndx = ELF64_R_SYM (rel->r_info);
9853
9854 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9855 symbol of the previous ADDR64 reloc. The symbol gives us the
9856 proper TOC base to use. */
9857 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9858 && rel != relocs
9859 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9860 && is_opd)
9861 r_symndx = ELF64_R_SYM (rel[-1].r_info);
9862
9863 sym = NULL;
9864 sec = NULL;
9865 h_elf = NULL;
9866 sym_name = NULL;
9867 unresolved_reloc = FALSE;
9868 warned = FALSE;
9869 orig_addend = rel->r_addend;
9870
9871 if (r_symndx < symtab_hdr->sh_info)
9872 {
9873 /* It's a local symbol. */
9874 long *opd_adjust;
9875
9876 sym = local_syms + r_symndx;
9877 sec = local_sections[r_symndx];
9878 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
9879 sym_type = ELF64_ST_TYPE (sym->st_info);
9880 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9881 opd_adjust = get_opd_info (sec);
9882 if (opd_adjust != NULL)
9883 {
9884 long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
9885 if (adjust == -1)
9886 relocation = 0;
9887 else
9888 {
9889 /* If this is a relocation against the opd section sym
9890 and we have edited .opd, adjust the reloc addend so
9891 that ld -r and ld --emit-relocs output is correct.
9892 If it is a reloc against some other .opd symbol,
9893 then the symbol value will be adjusted later. */
9894 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9895 rel->r_addend += adjust;
9896 else
9897 relocation += adjust;
9898 }
9899 }
9900 }
9901 else
9902 {
9903 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9904 r_symndx, symtab_hdr, sym_hashes,
9905 h_elf, sec, relocation,
9906 unresolved_reloc, warned);
9907 sym_name = h_elf->root.root.string;
9908 sym_type = h_elf->type;
9909 }
9910 h = (struct ppc_link_hash_entry *) h_elf;
9911
9912 if (sec != NULL && elf_discarded_section (sec))
9913 {
9914 /* For relocs against symbols from removed linkonce sections,
9915 or sections discarded by a linker script, we just want the
9916 section contents zeroed. Avoid any special processing. */
9917 _bfd_clear_contents (ppc64_elf_howto_table[r_type], input_bfd,
9918 contents + rel->r_offset);
9919 rel->r_info = 0;
9920 rel->r_addend = 0;
9921 continue;
9922 }
9923
9924 if (info->relocatable)
9925 continue;
9926
9927 /* TLS optimizations. Replace instruction sequences and relocs
9928 based on information we collected in tls_optimize. We edit
9929 RELOCS so that --emit-relocs will output something sensible
9930 for the final instruction stream. */
9931 tls_mask = 0;
9932 tls_gd = 0;
9933 toc_symndx = 0;
9934 if (IS_PPC64_TLS_RELOC (r_type))
9935 {
9936 if (h != NULL)
9937 tls_mask = h->tls_mask;
9938 else if (local_got_ents != NULL)
9939 {
9940 char *lgot_masks;
9941 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9942 tls_mask = lgot_masks[r_symndx];
9943 }
9944 if (tls_mask == 0 && r_type == R_PPC64_TLS)
9945 {
9946 /* Check for toc tls entries. */
9947 char *toc_tls;
9948
9949 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9950 rel, input_bfd))
9951 return FALSE;
9952
9953 if (toc_tls)
9954 tls_mask = *toc_tls;
9955 }
9956 }
9957
9958 /* Check that tls relocs are used with tls syms, and non-tls
9959 relocs are used with non-tls syms. */
9960 if (r_symndx != 0
9961 && r_type != R_PPC64_NONE
9962 && (h == NULL
9963 || h->elf.root.type == bfd_link_hash_defined
9964 || h->elf.root.type == bfd_link_hash_defweak)
9965 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9966 {
9967 if (r_type == R_PPC64_TLS && tls_mask != 0)
9968 /* R_PPC64_TLS is OK against a symbol in the TOC. */
9969 ;
9970 else
9971 (*_bfd_error_handler)
9972 (sym_type == STT_TLS
9973 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9974 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9975 input_bfd,
9976 input_section,
9977 (long) rel->r_offset,
9978 ppc64_elf_howto_table[r_type]->name,
9979 sym_name);
9980 }
9981
9982 /* Ensure reloc mapping code below stays sane. */
9983 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9984 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9985 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
9986 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9987 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9988 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9989 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
9990 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9991 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9992 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9993 abort ();
9994
9995 switch (r_type)
9996 {
9997 default:
9998 break;
9999
10000 case R_PPC64_TOC16:
10001 case R_PPC64_TOC16_LO:
10002 case R_PPC64_TOC16_DS:
10003 case R_PPC64_TOC16_LO_DS:
10004 {
10005 /* Check for toc tls entries. */
10006 char *toc_tls;
10007 int retval;
10008
10009 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
10010 rel, input_bfd);
10011 if (retval == 0)
10012 return FALSE;
10013
10014 if (toc_tls)
10015 {
10016 tls_mask = *toc_tls;
10017 if (r_type == R_PPC64_TOC16_DS
10018 || r_type == R_PPC64_TOC16_LO_DS)
10019 {
10020 if (tls_mask != 0
10021 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
10022 goto toctprel;
10023 }
10024 else
10025 {
10026 /* If we found a GD reloc pair, then we might be
10027 doing a GD->IE transition. */
10028 if (retval == 2)
10029 {
10030 tls_gd = TLS_TPRELGD;
10031 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10032 goto tls_get_addr_check;
10033 }
10034 else if (retval == 3)
10035 {
10036 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10037 goto tls_get_addr_check;
10038 }
10039 }
10040 }
10041 }
10042 break;
10043
10044 case R_PPC64_GOT_TPREL16_DS:
10045 case R_PPC64_GOT_TPREL16_LO_DS:
10046 if (tls_mask != 0
10047 && (tls_mask & TLS_TPREL) == 0)
10048 {
10049 toctprel:
10050 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
10051 insn &= 31 << 21;
10052 insn |= 0x3c0d0000; /* addis 0,13,0 */
10053 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
10054 r_type = R_PPC64_TPREL16_HA;
10055 if (toc_symndx != 0)
10056 {
10057 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
10058 /* We changed the symbol. Start over in order to
10059 get h, sym, sec etc. right. */
10060 rel--;
10061 continue;
10062 }
10063 else
10064 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10065 }
10066 break;
10067
10068 case R_PPC64_TLS:
10069 if (tls_mask != 0
10070 && (tls_mask & TLS_TPREL) == 0)
10071 {
10072 bfd_vma rtra;
10073 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
10074 if ((insn & ((0x3f << 26) | (31 << 11)))
10075 == ((31 << 26) | (13 << 11)))
10076 rtra = insn & ((1 << 26) - (1 << 16));
10077 else if ((insn & ((0x3f << 26) | (31 << 16)))
10078 == ((31 << 26) | (13 << 16)))
10079 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
10080 else
10081 abort ();
10082 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
10083 /* add -> addi. */
10084 insn = 14 << 26;
10085 else if ((insn & (31 << 1)) == 23 << 1
10086 && ((insn & (31 << 6)) < 14 << 6
10087 || ((insn & (31 << 6)) >= 16 << 6
10088 && (insn & (31 << 6)) < 24 << 6)))
10089 /* load and store indexed -> dform. */
10090 insn = (32 | ((insn >> 6) & 31)) << 26;
10091 else if ((insn & (31 << 1)) == 21 << 1
10092 && (insn & (0x1a << 6)) == 0)
10093 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
10094 insn = (((58 | ((insn >> 6) & 4)) << 26)
10095 | ((insn >> 6) & 1));
10096 else if ((insn & (31 << 1)) == 21 << 1
10097 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
10098 /* lwax -> lwa. */
10099 insn = (58 << 26) | 2;
10100 else
10101 abort ();
10102 insn |= rtra;
10103 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10104 /* Was PPC64_TLS which sits on insn boundary, now
10105 PPC64_TPREL16_LO which is at low-order half-word. */
10106 rel->r_offset += d_offset;
10107 r_type = R_PPC64_TPREL16_LO;
10108 if (toc_symndx != 0)
10109 {
10110 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
10111 /* We changed the symbol. Start over in order to
10112 get h, sym, sec etc. right. */
10113 rel--;
10114 continue;
10115 }
10116 else
10117 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10118 }
10119 break;
10120
10121 case R_PPC64_GOT_TLSGD16_HI:
10122 case R_PPC64_GOT_TLSGD16_HA:
10123 tls_gd = TLS_TPRELGD;
10124 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10125 goto tls_gdld_hi;
10126 break;
10127
10128 case R_PPC64_GOT_TLSLD16_HI:
10129 case R_PPC64_GOT_TLSLD16_HA:
10130 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10131 {
10132 tls_gdld_hi:
10133 if ((tls_mask & tls_gd) != 0)
10134 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10135 + R_PPC64_GOT_TPREL16_DS);
10136 else
10137 {
10138 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10139 rel->r_offset -= d_offset;
10140 r_type = R_PPC64_NONE;
10141 }
10142 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10143 }
10144 break;
10145
10146 case R_PPC64_GOT_TLSGD16:
10147 case R_PPC64_GOT_TLSGD16_LO:
10148 tls_gd = TLS_TPRELGD;
10149 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10150 goto tls_get_addr_check;
10151 break;
10152
10153 case R_PPC64_GOT_TLSLD16:
10154 case R_PPC64_GOT_TLSLD16_LO:
10155 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10156 {
10157 tls_get_addr_check:
10158 if (rel + 1 < relend)
10159 {
10160 enum elf_ppc64_reloc_type r_type2;
10161 unsigned long r_symndx2;
10162 struct elf_link_hash_entry *h2;
10163 bfd_vma insn1, insn2, insn3;
10164 bfd_vma offset;
10165
10166 /* The next instruction should be a call to
10167 __tls_get_addr. Peek at the reloc to be sure. */
10168 r_type2 = ELF64_R_TYPE (rel[1].r_info);
10169 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
10170 if (r_symndx2 < symtab_hdr->sh_info
10171 || (r_type2 != R_PPC64_REL14
10172 && r_type2 != R_PPC64_REL14_BRTAKEN
10173 && r_type2 != R_PPC64_REL14_BRNTAKEN
10174 && r_type2 != R_PPC64_REL24))
10175 break;
10176
10177 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
10178 while (h2->root.type == bfd_link_hash_indirect
10179 || h2->root.type == bfd_link_hash_warning)
10180 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
10181 if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
10182 && h2 != &htab->tls_get_addr_fd->elf))
10183 break;
10184
10185 /* OK, it checks out. Replace the call. */
10186 offset = rel[1].r_offset;
10187 insn1 = bfd_get_32 (output_bfd,
10188 contents + rel->r_offset - d_offset);
10189 insn3 = bfd_get_32 (output_bfd,
10190 contents + offset + 4);
10191 if ((tls_mask & tls_gd) != 0)
10192 {
10193 /* IE */
10194 insn1 &= (1 << 26) - (1 << 2);
10195 insn1 |= 58 << 26; /* ld */
10196 insn2 = 0x7c636a14; /* add 3,3,13 */
10197 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
10198 if ((tls_mask & TLS_EXPLICIT) == 0)
10199 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10200 + R_PPC64_GOT_TPREL16_DS);
10201 else
10202 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
10203 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10204 }
10205 else
10206 {
10207 /* LE */
10208 insn1 = 0x3c6d0000; /* addis 3,13,0 */
10209 insn2 = 0x38630000; /* addi 3,3,0 */
10210 if (tls_gd == 0)
10211 {
10212 /* Was an LD reloc. */
10213 r_symndx = 0;
10214 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10215 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10216 }
10217 else if (toc_symndx != 0)
10218 r_symndx = toc_symndx;
10219 r_type = R_PPC64_TPREL16_HA;
10220 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10221 rel[1].r_info = ELF64_R_INFO (r_symndx,
10222 R_PPC64_TPREL16_LO);
10223 rel[1].r_offset += d_offset;
10224 }
10225 if (insn3 == NOP
10226 || insn3 == CROR_151515 || insn3 == CROR_313131)
10227 {
10228 insn3 = insn2;
10229 insn2 = NOP;
10230 rel[1].r_offset += 4;
10231 }
10232 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - d_offset);
10233 bfd_put_32 (output_bfd, insn2, contents + offset);
10234 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
10235 if (tls_gd == 0 || toc_symndx != 0)
10236 {
10237 /* We changed the symbol. Start over in order
10238 to get h, sym, sec etc. right. */
10239 rel--;
10240 continue;
10241 }
10242 }
10243 }
10244 break;
10245
10246 case R_PPC64_DTPMOD64:
10247 if (rel + 1 < relend
10248 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10249 && rel[1].r_offset == rel->r_offset + 8)
10250 {
10251 if ((tls_mask & TLS_GD) == 0)
10252 {
10253 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10254 if ((tls_mask & TLS_TPRELGD) != 0)
10255 r_type = R_PPC64_TPREL64;
10256 else
10257 {
10258 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10259 r_type = R_PPC64_NONE;
10260 }
10261 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10262 }
10263 }
10264 else
10265 {
10266 if ((tls_mask & TLS_LD) == 0)
10267 {
10268 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10269 r_type = R_PPC64_NONE;
10270 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10271 }
10272 }
10273 break;
10274
10275 case R_PPC64_TPREL64:
10276 if ((tls_mask & TLS_TPREL) == 0)
10277 {
10278 r_type = R_PPC64_NONE;
10279 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10280 }
10281 break;
10282 }
10283
10284 /* Handle other relocations that tweak non-addend part of insn. */
10285 insn = 0;
10286 max_br_offset = 1 << 25;
10287 addend = rel->r_addend;
10288 switch (r_type)
10289 {
10290 default:
10291 break;
10292
10293 /* Branch taken prediction relocations. */
10294 case R_PPC64_ADDR14_BRTAKEN:
10295 case R_PPC64_REL14_BRTAKEN:
10296 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
10297 /* Fall thru. */
10298
10299 /* Branch not taken prediction relocations. */
10300 case R_PPC64_ADDR14_BRNTAKEN:
10301 case R_PPC64_REL14_BRNTAKEN:
10302 insn |= bfd_get_32 (output_bfd,
10303 contents + rel->r_offset) & ~(0x01 << 21);
10304 /* Fall thru. */
10305
10306 case R_PPC64_REL14:
10307 max_br_offset = 1 << 15;
10308 /* Fall thru. */
10309
10310 case R_PPC64_REL24:
10311 /* Calls to functions with a different TOC, such as calls to
10312 shared objects, need to alter the TOC pointer. This is
10313 done using a linkage stub. A REL24 branching to these
10314 linkage stubs needs to be followed by a nop, as the nop
10315 will be replaced with an instruction to restore the TOC
10316 base pointer. */
10317 stub_entry = NULL;
10318 fdh = h;
10319 if (((h != NULL
10320 && (((fdh = h->oh) != NULL
10321 && fdh->elf.plt.plist != NULL)
10322 || (fdh = h)->elf.plt.plist != NULL))
10323 || (sec != NULL
10324 && sec->output_section != NULL
10325 && sec->id <= htab->top_id
10326 && (htab->stub_group[sec->id].toc_off
10327 != htab->stub_group[input_section->id].toc_off)))
10328 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
10329 rel, htab)) != NULL
10330 && (stub_entry->stub_type == ppc_stub_plt_call
10331 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10332 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
10333 {
10334 bfd_boolean can_plt_call = FALSE;
10335
10336 if (rel->r_offset + 8 <= input_section->size)
10337 {
10338 unsigned long nop;
10339 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10340 if (nop == NOP
10341 || nop == CROR_151515 || nop == CROR_313131)
10342 {
10343 bfd_put_32 (input_bfd, LD_R2_40R1,
10344 contents + rel->r_offset + 4);
10345 can_plt_call = TRUE;
10346 }
10347 }
10348
10349 if (!can_plt_call)
10350 {
10351 if (stub_entry->stub_type == ppc_stub_plt_call)
10352 {
10353 /* If this is a plain branch rather than a branch
10354 and link, don't require a nop. However, don't
10355 allow tail calls in a shared library as they
10356 will result in r2 being corrupted. */
10357 unsigned long br;
10358 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
10359 if (info->executable && (br & 1) == 0)
10360 can_plt_call = TRUE;
10361 else
10362 stub_entry = NULL;
10363 }
10364 else if (h != NULL
10365 && strcmp (h->elf.root.root.string,
10366 ".__libc_start_main") == 0)
10367 {
10368 /* Allow crt1 branch to go via a toc adjusting stub. */
10369 can_plt_call = TRUE;
10370 }
10371 else
10372 {
10373 if (strcmp (input_section->output_section->name,
10374 ".init") == 0
10375 || strcmp (input_section->output_section->name,
10376 ".fini") == 0)
10377 (*_bfd_error_handler)
10378 (_("%B(%A+0x%lx): automatic multiple TOCs "
10379 "not supported using your crt files; "
10380 "recompile with -mminimal-toc or upgrade gcc"),
10381 input_bfd,
10382 input_section,
10383 (long) rel->r_offset);
10384 else
10385 (*_bfd_error_handler)
10386 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
10387 "does not allow automatic multiple TOCs; "
10388 "recompile with -mminimal-toc or "
10389 "-fno-optimize-sibling-calls, "
10390 "or make `%s' extern"),
10391 input_bfd,
10392 input_section,
10393 (long) rel->r_offset,
10394 sym_name,
10395 sym_name);
10396 bfd_set_error (bfd_error_bad_value);
10397 ret = FALSE;
10398 }
10399 }
10400
10401 if (can_plt_call
10402 && stub_entry->stub_type == ppc_stub_plt_call)
10403 unresolved_reloc = FALSE;
10404 }
10405
10406 if (stub_entry == NULL
10407 && get_opd_info (sec) != NULL)
10408 {
10409 /* The branch destination is the value of the opd entry. */
10410 bfd_vma off = (relocation + addend
10411 - sec->output_section->vma
10412 - sec->output_offset);
10413 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10414 if (dest != (bfd_vma) -1)
10415 {
10416 relocation = dest;
10417 addend = 0;
10418 }
10419 }
10420
10421 /* If the branch is out of reach we ought to have a long
10422 branch stub. */
10423 from = (rel->r_offset
10424 + input_section->output_offset
10425 + input_section->output_section->vma);
10426
10427 if (stub_entry == NULL
10428 && (relocation + addend - from + max_br_offset
10429 >= 2 * max_br_offset)
10430 && r_type != R_PPC64_ADDR14_BRTAKEN
10431 && r_type != R_PPC64_ADDR14_BRNTAKEN)
10432 stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10433 htab);
10434
10435 if (stub_entry != NULL)
10436 {
10437 /* Munge up the value and addend so that we call the stub
10438 rather than the procedure directly. */
10439 relocation = (stub_entry->stub_offset
10440 + stub_entry->stub_sec->output_offset
10441 + stub_entry->stub_sec->output_section->vma);
10442 addend = 0;
10443 }
10444
10445 if (insn != 0)
10446 {
10447 if (is_power4)
10448 {
10449 /* Set 'a' bit. This is 0b00010 in BO field for branch
10450 on CR(BI) insns (BO == 001at or 011at), and 0b01000
10451 for branch on CTR insns (BO == 1a00t or 1a01t). */
10452 if ((insn & (0x14 << 21)) == (0x04 << 21))
10453 insn |= 0x02 << 21;
10454 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10455 insn |= 0x08 << 21;
10456 else
10457 break;
10458 }
10459 else
10460 {
10461 /* Invert 'y' bit if not the default. */
10462 if ((bfd_signed_vma) (relocation + addend - from) < 0)
10463 insn ^= 0x01 << 21;
10464 }
10465
10466 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10467 }
10468
10469 /* NOP out calls to undefined weak functions.
10470 We can thus call a weak function without first
10471 checking whether the function is defined. */
10472 else if (h != NULL
10473 && h->elf.root.type == bfd_link_hash_undefweak
10474 && r_type == R_PPC64_REL24
10475 && relocation == 0
10476 && addend == 0)
10477 {
10478 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10479 continue;
10480 }
10481 break;
10482 }
10483
10484 /* Set `addend'. */
10485 tls_type = 0;
10486 switch (r_type)
10487 {
10488 default:
10489 (*_bfd_error_handler)
10490 (_("%B: unknown relocation type %d for symbol %s"),
10491 input_bfd, (int) r_type, sym_name);
10492
10493 bfd_set_error (bfd_error_bad_value);
10494 ret = FALSE;
10495 continue;
10496
10497 case R_PPC64_NONE:
10498 case R_PPC64_TLS:
10499 case R_PPC64_GNU_VTINHERIT:
10500 case R_PPC64_GNU_VTENTRY:
10501 continue;
10502
10503 /* GOT16 relocations. Like an ADDR16 using the symbol's
10504 address in the GOT as relocation value instead of the
10505 symbol's value itself. Also, create a GOT entry for the
10506 symbol and put the symbol value there. */
10507 case R_PPC64_GOT_TLSGD16:
10508 case R_PPC64_GOT_TLSGD16_LO:
10509 case R_PPC64_GOT_TLSGD16_HI:
10510 case R_PPC64_GOT_TLSGD16_HA:
10511 tls_type = TLS_TLS | TLS_GD;
10512 goto dogot;
10513
10514 case R_PPC64_GOT_TLSLD16:
10515 case R_PPC64_GOT_TLSLD16_LO:
10516 case R_PPC64_GOT_TLSLD16_HI:
10517 case R_PPC64_GOT_TLSLD16_HA:
10518 tls_type = TLS_TLS | TLS_LD;
10519 goto dogot;
10520
10521 case R_PPC64_GOT_TPREL16_DS:
10522 case R_PPC64_GOT_TPREL16_LO_DS:
10523 case R_PPC64_GOT_TPREL16_HI:
10524 case R_PPC64_GOT_TPREL16_HA:
10525 tls_type = TLS_TLS | TLS_TPREL;
10526 goto dogot;
10527
10528 case R_PPC64_GOT_DTPREL16_DS:
10529 case R_PPC64_GOT_DTPREL16_LO_DS:
10530 case R_PPC64_GOT_DTPREL16_HI:
10531 case R_PPC64_GOT_DTPREL16_HA:
10532 tls_type = TLS_TLS | TLS_DTPREL;
10533 goto dogot;
10534
10535 case R_PPC64_GOT16:
10536 case R_PPC64_GOT16_LO:
10537 case R_PPC64_GOT16_HI:
10538 case R_PPC64_GOT16_HA:
10539 case R_PPC64_GOT16_DS:
10540 case R_PPC64_GOT16_LO_DS:
10541 dogot:
10542 {
10543 /* Relocation is to the entry for this symbol in the global
10544 offset table. */
10545 asection *got;
10546 bfd_vma *offp;
10547 bfd_vma off;
10548 unsigned long indx = 0;
10549
10550 if (tls_type == (TLS_TLS | TLS_LD)
10551 && (h == NULL
10552 || !h->elf.def_dynamic))
10553 offp = &ppc64_tlsld_got (input_bfd)->offset;
10554 else
10555 {
10556 struct got_entry *ent;
10557
10558 if (h != NULL)
10559 {
10560 bfd_boolean dyn = htab->elf.dynamic_sections_created;
10561 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10562 &h->elf)
10563 || (info->shared
10564 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
10565 /* This is actually a static link, or it is a
10566 -Bsymbolic link and the symbol is defined
10567 locally, or the symbol was forced to be local
10568 because of a version file. */
10569 ;
10570 else
10571 {
10572 indx = h->elf.dynindx;
10573 unresolved_reloc = FALSE;
10574 }
10575 ent = h->elf.got.glist;
10576 }
10577 else
10578 {
10579 if (local_got_ents == NULL)
10580 abort ();
10581 ent = local_got_ents[r_symndx];
10582 }
10583
10584 for (; ent != NULL; ent = ent->next)
10585 if (ent->addend == orig_addend
10586 && ent->owner == input_bfd
10587 && ent->tls_type == tls_type)
10588 break;
10589 if (ent == NULL)
10590 abort ();
10591 offp = &ent->got.offset;
10592 }
10593
10594 got = ppc64_elf_tdata (input_bfd)->got;
10595 if (got == NULL)
10596 abort ();
10597
10598 /* The offset must always be a multiple of 8. We use the
10599 least significant bit to record whether we have already
10600 processed this entry. */
10601 off = *offp;
10602 if ((off & 1) != 0)
10603 off &= ~1;
10604 else
10605 {
10606 /* Generate relocs for the dynamic linker, except in
10607 the case of TLSLD where we'll use one entry per
10608 module. */
10609 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10610
10611 *offp = off | 1;
10612 if ((info->shared || indx != 0)
10613 && (h == NULL
10614 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10615 || h->elf.root.type != bfd_link_hash_undefweak))
10616 {
10617 outrel.r_offset = (got->output_section->vma
10618 + got->output_offset
10619 + off);
10620 outrel.r_addend = addend;
10621 if (tls_type & (TLS_LD | TLS_GD))
10622 {
10623 outrel.r_addend = 0;
10624 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
10625 if (tls_type == (TLS_TLS | TLS_GD))
10626 {
10627 loc = relgot->contents;
10628 loc += (relgot->reloc_count++
10629 * sizeof (Elf64_External_Rela));
10630 bfd_elf64_swap_reloca_out (output_bfd,
10631 &outrel, loc);
10632 outrel.r_offset += 8;
10633 outrel.r_addend = addend;
10634 outrel.r_info
10635 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10636 }
10637 }
10638 else if (tls_type == (TLS_TLS | TLS_DTPREL))
10639 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10640 else if (tls_type == (TLS_TLS | TLS_TPREL))
10641 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10642 else if (indx == 0)
10643 {
10644 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10645
10646 /* Write the .got section contents for the sake
10647 of prelink. */
10648 loc = got->contents + off;
10649 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10650 loc);
10651 }
10652 else
10653 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
10654
10655 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
10656 {
10657 outrel.r_addend += relocation;
10658 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
10659 outrel.r_addend -= htab->elf.tls_sec->vma;
10660 }
10661 loc = relgot->contents;
10662 loc += (relgot->reloc_count++
10663 * sizeof (Elf64_External_Rela));
10664 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10665 }
10666
10667 /* Init the .got section contents here if we're not
10668 emitting a reloc. */
10669 else
10670 {
10671 relocation += addend;
10672 if (tls_type == (TLS_TLS | TLS_LD))
10673 relocation = 1;
10674 else if (tls_type != 0)
10675 {
10676 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
10677 if (tls_type == (TLS_TLS | TLS_TPREL))
10678 relocation += DTP_OFFSET - TP_OFFSET;
10679
10680 if (tls_type == (TLS_TLS | TLS_GD))
10681 {
10682 bfd_put_64 (output_bfd, relocation,
10683 got->contents + off + 8);
10684 relocation = 1;
10685 }
10686 }
10687
10688 bfd_put_64 (output_bfd, relocation,
10689 got->contents + off);
10690 }
10691 }
10692
10693 if (off >= (bfd_vma) -2)
10694 abort ();
10695
10696 relocation = got->output_offset + off;
10697
10698 /* TOC base (r2) is TOC start plus 0x8000. */
10699 addend = -TOC_BASE_OFF;
10700 }
10701 break;
10702
10703 case R_PPC64_PLT16_HA:
10704 case R_PPC64_PLT16_HI:
10705 case R_PPC64_PLT16_LO:
10706 case R_PPC64_PLT32:
10707 case R_PPC64_PLT64:
10708 /* Relocation is to the entry for this symbol in the
10709 procedure linkage table. */
10710
10711 /* Resolve a PLT reloc against a local symbol directly,
10712 without using the procedure linkage table. */
10713 if (h == NULL)
10714 break;
10715
10716 /* It's possible that we didn't make a PLT entry for this
10717 symbol. This happens when statically linking PIC code,
10718 or when using -Bsymbolic. Go find a match if there is a
10719 PLT entry. */
10720 if (htab->plt != NULL)
10721 {
10722 struct plt_entry *ent;
10723 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
10724 if (ent->addend == orig_addend
10725 && ent->plt.offset != (bfd_vma) -1)
10726 {
10727 relocation = (htab->plt->output_section->vma
10728 + htab->plt->output_offset
10729 + ent->plt.offset);
10730 unresolved_reloc = FALSE;
10731 }
10732 }
10733 break;
10734
10735 case R_PPC64_TOC:
10736 /* Relocation value is TOC base. */
10737 relocation = TOCstart;
10738 if (r_symndx == 0)
10739 relocation += htab->stub_group[input_section->id].toc_off;
10740 else if (unresolved_reloc)
10741 ;
10742 else if (sec != NULL && sec->id <= htab->top_id)
10743 relocation += htab->stub_group[sec->id].toc_off;
10744 else
10745 unresolved_reloc = TRUE;
10746 goto dodyn;
10747
10748 /* TOC16 relocs. We want the offset relative to the TOC base,
10749 which is the address of the start of the TOC plus 0x8000.
10750 The TOC consists of sections .got, .toc, .tocbss, and .plt,
10751 in this order. */
10752 case R_PPC64_TOC16:
10753 case R_PPC64_TOC16_LO:
10754 case R_PPC64_TOC16_HI:
10755 case R_PPC64_TOC16_DS:
10756 case R_PPC64_TOC16_LO_DS:
10757 case R_PPC64_TOC16_HA:
10758 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
10759 break;
10760
10761 /* Relocate against the beginning of the section. */
10762 case R_PPC64_SECTOFF:
10763 case R_PPC64_SECTOFF_LO:
10764 case R_PPC64_SECTOFF_HI:
10765 case R_PPC64_SECTOFF_DS:
10766 case R_PPC64_SECTOFF_LO_DS:
10767 case R_PPC64_SECTOFF_HA:
10768 if (sec != NULL)
10769 addend -= sec->output_section->vma;
10770 break;
10771
10772 case R_PPC64_REL14:
10773 case R_PPC64_REL14_BRNTAKEN:
10774 case R_PPC64_REL14_BRTAKEN:
10775 case R_PPC64_REL24:
10776 break;
10777
10778 case R_PPC64_TPREL16:
10779 case R_PPC64_TPREL16_LO:
10780 case R_PPC64_TPREL16_HI:
10781 case R_PPC64_TPREL16_HA:
10782 case R_PPC64_TPREL16_DS:
10783 case R_PPC64_TPREL16_LO_DS:
10784 case R_PPC64_TPREL16_HIGHER:
10785 case R_PPC64_TPREL16_HIGHERA:
10786 case R_PPC64_TPREL16_HIGHEST:
10787 case R_PPC64_TPREL16_HIGHESTA:
10788 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10789 if (info->shared)
10790 /* The TPREL16 relocs shouldn't really be used in shared
10791 libs as they will result in DT_TEXTREL being set, but
10792 support them anyway. */
10793 goto dodyn;
10794 break;
10795
10796 case R_PPC64_DTPREL16:
10797 case R_PPC64_DTPREL16_LO:
10798 case R_PPC64_DTPREL16_HI:
10799 case R_PPC64_DTPREL16_HA:
10800 case R_PPC64_DTPREL16_DS:
10801 case R_PPC64_DTPREL16_LO_DS:
10802 case R_PPC64_DTPREL16_HIGHER:
10803 case R_PPC64_DTPREL16_HIGHERA:
10804 case R_PPC64_DTPREL16_HIGHEST:
10805 case R_PPC64_DTPREL16_HIGHESTA:
10806 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10807 break;
10808
10809 case R_PPC64_DTPMOD64:
10810 relocation = 1;
10811 addend = 0;
10812 goto dodyn;
10813
10814 case R_PPC64_TPREL64:
10815 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10816 goto dodyn;
10817
10818 case R_PPC64_DTPREL64:
10819 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10820 /* Fall thru */
10821
10822 /* Relocations that may need to be propagated if this is a
10823 dynamic object. */
10824 case R_PPC64_REL30:
10825 case R_PPC64_REL32:
10826 case R_PPC64_REL64:
10827 case R_PPC64_ADDR14:
10828 case R_PPC64_ADDR14_BRNTAKEN:
10829 case R_PPC64_ADDR14_BRTAKEN:
10830 case R_PPC64_ADDR16:
10831 case R_PPC64_ADDR16_DS:
10832 case R_PPC64_ADDR16_HA:
10833 case R_PPC64_ADDR16_HI:
10834 case R_PPC64_ADDR16_HIGHER:
10835 case R_PPC64_ADDR16_HIGHERA:
10836 case R_PPC64_ADDR16_HIGHEST:
10837 case R_PPC64_ADDR16_HIGHESTA:
10838 case R_PPC64_ADDR16_LO:
10839 case R_PPC64_ADDR16_LO_DS:
10840 case R_PPC64_ADDR24:
10841 case R_PPC64_ADDR32:
10842 case R_PPC64_ADDR64:
10843 case R_PPC64_UADDR16:
10844 case R_PPC64_UADDR32:
10845 case R_PPC64_UADDR64:
10846 dodyn:
10847 if ((input_section->flags & SEC_ALLOC) == 0)
10848 break;
10849
10850 if (NO_OPD_RELOCS && is_opd)
10851 break;
10852
10853 if ((info->shared
10854 && (h == NULL
10855 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10856 || h->elf.root.type != bfd_link_hash_undefweak)
10857 && (MUST_BE_DYN_RELOC (r_type)
10858 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
10859 || (ELIMINATE_COPY_RELOCS
10860 && !info->shared
10861 && h != NULL
10862 && h->elf.dynindx != -1
10863 && !h->elf.non_got_ref
10864 && h->elf.def_dynamic
10865 && !h->elf.def_regular))
10866 {
10867 Elf_Internal_Rela outrel;
10868 bfd_boolean skip, relocate;
10869 asection *sreloc;
10870 bfd_byte *loc;
10871 bfd_vma out_off;
10872
10873 /* When generating a dynamic object, these relocations
10874 are copied into the output file to be resolved at run
10875 time. */
10876
10877 skip = FALSE;
10878 relocate = FALSE;
10879
10880 out_off = _bfd_elf_section_offset (output_bfd, info,
10881 input_section, rel->r_offset);
10882 if (out_off == (bfd_vma) -1)
10883 skip = TRUE;
10884 else if (out_off == (bfd_vma) -2)
10885 skip = TRUE, relocate = TRUE;
10886 out_off += (input_section->output_section->vma
10887 + input_section->output_offset);
10888 outrel.r_offset = out_off;
10889 outrel.r_addend = rel->r_addend;
10890
10891 /* Optimize unaligned reloc use. */
10892 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10893 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10894 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10895 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10896 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10897 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10898 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10899 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10900 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10901
10902 if (skip)
10903 memset (&outrel, 0, sizeof outrel);
10904 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
10905 && !is_opd
10906 && r_type != R_PPC64_TOC)
10907 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
10908 else
10909 {
10910 /* This symbol is local, or marked to become local,
10911 or this is an opd section reloc which must point
10912 at a local function. */
10913 outrel.r_addend += relocation;
10914 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
10915 {
10916 if (is_opd && h != NULL)
10917 {
10918 /* Lie about opd entries. This case occurs
10919 when building shared libraries and we
10920 reference a function in another shared
10921 lib. The same thing happens for a weak
10922 definition in an application that's
10923 overridden by a strong definition in a
10924 shared lib. (I believe this is a generic
10925 bug in binutils handling of weak syms.)
10926 In these cases we won't use the opd
10927 entry in this lib. */
10928 unresolved_reloc = FALSE;
10929 }
10930 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10931
10932 /* We need to relocate .opd contents for ld.so.
10933 Prelink also wants simple and consistent rules
10934 for relocs. This make all RELATIVE relocs have
10935 *r_offset equal to r_addend. */
10936 relocate = TRUE;
10937 }
10938 else
10939 {
10940 long indx = 0;
10941
10942 if (bfd_is_abs_section (sec))
10943 ;
10944 else if (sec == NULL || sec->owner == NULL)
10945 {
10946 bfd_set_error (bfd_error_bad_value);
10947 return FALSE;
10948 }
10949 else
10950 {
10951 asection *osec;
10952
10953 osec = sec->output_section;
10954 indx = elf_section_data (osec)->dynindx;
10955
10956 if (indx == 0)
10957 {
10958 if ((osec->flags & SEC_READONLY) == 0
10959 && htab->elf.data_index_section != NULL)
10960 osec = htab->elf.data_index_section;
10961 else
10962 osec = htab->elf.text_index_section;
10963 indx = elf_section_data (osec)->dynindx;
10964 }
10965 BFD_ASSERT (indx != 0);
10966
10967 /* We are turning this relocation into one
10968 against a section symbol, so subtract out
10969 the output section's address but not the
10970 offset of the input section in the output
10971 section. */
10972 outrel.r_addend -= osec->vma;
10973 }
10974
10975 outrel.r_info = ELF64_R_INFO (indx, r_type);
10976 }
10977 }
10978
10979 sreloc = elf_section_data (input_section)->sreloc;
10980 if (sreloc == NULL)
10981 abort ();
10982
10983 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10984 >= sreloc->size)
10985 abort ();
10986 loc = sreloc->contents;
10987 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
10988 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10989
10990 /* If this reloc is against an external symbol, it will
10991 be computed at runtime, so there's no need to do
10992 anything now. However, for the sake of prelink ensure
10993 that the section contents are a known value. */
10994 if (! relocate)
10995 {
10996 unresolved_reloc = FALSE;
10997 /* The value chosen here is quite arbitrary as ld.so
10998 ignores section contents except for the special
10999 case of .opd where the contents might be accessed
11000 before relocation. Choose zero, as that won't
11001 cause reloc overflow. */
11002 relocation = 0;
11003 addend = 0;
11004 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
11005 to improve backward compatibility with older
11006 versions of ld. */
11007 if (r_type == R_PPC64_ADDR64)
11008 addend = outrel.r_addend;
11009 /* Adjust pc_relative relocs to have zero in *r_offset. */
11010 else if (ppc64_elf_howto_table[r_type]->pc_relative)
11011 addend = (input_section->output_section->vma
11012 + input_section->output_offset
11013 + rel->r_offset);
11014 }
11015 }
11016 break;
11017
11018 case R_PPC64_COPY:
11019 case R_PPC64_GLOB_DAT:
11020 case R_PPC64_JMP_SLOT:
11021 case R_PPC64_RELATIVE:
11022 /* We shouldn't ever see these dynamic relocs in relocatable
11023 files. */
11024 /* Fall through. */
11025
11026 case R_PPC64_PLTGOT16:
11027 case R_PPC64_PLTGOT16_DS:
11028 case R_PPC64_PLTGOT16_HA:
11029 case R_PPC64_PLTGOT16_HI:
11030 case R_PPC64_PLTGOT16_LO:
11031 case R_PPC64_PLTGOT16_LO_DS:
11032 case R_PPC64_PLTREL32:
11033 case R_PPC64_PLTREL64:
11034 /* These ones haven't been implemented yet. */
11035
11036 (*_bfd_error_handler)
11037 (_("%B: relocation %s is not supported for symbol %s."),
11038 input_bfd,
11039 ppc64_elf_howto_table[r_type]->name, sym_name);
11040
11041 bfd_set_error (bfd_error_invalid_operation);
11042 ret = FALSE;
11043 continue;
11044 }
11045
11046 /* Do any further special processing. */
11047 switch (r_type)
11048 {
11049 default:
11050 break;
11051
11052 case R_PPC64_ADDR16_HA:
11053 case R_PPC64_ADDR16_HIGHERA:
11054 case R_PPC64_ADDR16_HIGHESTA:
11055 case R_PPC64_TOC16_HA:
11056 case R_PPC64_SECTOFF_HA:
11057 case R_PPC64_TPREL16_HA:
11058 case R_PPC64_DTPREL16_HA:
11059 case R_PPC64_TPREL16_HIGHER:
11060 case R_PPC64_TPREL16_HIGHERA:
11061 case R_PPC64_TPREL16_HIGHEST:
11062 case R_PPC64_TPREL16_HIGHESTA:
11063 case R_PPC64_DTPREL16_HIGHER:
11064 case R_PPC64_DTPREL16_HIGHERA:
11065 case R_PPC64_DTPREL16_HIGHEST:
11066 case R_PPC64_DTPREL16_HIGHESTA:
11067 /* It's just possible that this symbol is a weak symbol
11068 that's not actually defined anywhere. In that case,
11069 'sec' would be NULL, and we should leave the symbol
11070 alone (it will be set to zero elsewhere in the link). */
11071 if (sec == NULL)
11072 break;
11073 /* Fall thru */
11074
11075 case R_PPC64_GOT16_HA:
11076 case R_PPC64_PLTGOT16_HA:
11077 case R_PPC64_PLT16_HA:
11078 case R_PPC64_GOT_TLSGD16_HA:
11079 case R_PPC64_GOT_TLSLD16_HA:
11080 case R_PPC64_GOT_TPREL16_HA:
11081 case R_PPC64_GOT_DTPREL16_HA:
11082 /* Add 0x10000 if sign bit in 0:15 is set.
11083 Bits 0:15 are not used. */
11084 addend += 0x8000;
11085 break;
11086
11087 case R_PPC64_ADDR16_DS:
11088 case R_PPC64_ADDR16_LO_DS:
11089 case R_PPC64_GOT16_DS:
11090 case R_PPC64_GOT16_LO_DS:
11091 case R_PPC64_PLT16_LO_DS:
11092 case R_PPC64_SECTOFF_DS:
11093 case R_PPC64_SECTOFF_LO_DS:
11094 case R_PPC64_TOC16_DS:
11095 case R_PPC64_TOC16_LO_DS:
11096 case R_PPC64_PLTGOT16_DS:
11097 case R_PPC64_PLTGOT16_LO_DS:
11098 case R_PPC64_GOT_TPREL16_DS:
11099 case R_PPC64_GOT_TPREL16_LO_DS:
11100 case R_PPC64_GOT_DTPREL16_DS:
11101 case R_PPC64_GOT_DTPREL16_LO_DS:
11102 case R_PPC64_TPREL16_DS:
11103 case R_PPC64_TPREL16_LO_DS:
11104 case R_PPC64_DTPREL16_DS:
11105 case R_PPC64_DTPREL16_LO_DS:
11106 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
11107 mask = 3;
11108 /* If this reloc is against an lq insn, then the value must be
11109 a multiple of 16. This is somewhat of a hack, but the
11110 "correct" way to do this by defining _DQ forms of all the
11111 _DS relocs bloats all reloc switches in this file. It
11112 doesn't seem to make much sense to use any of these relocs
11113 in data, so testing the insn should be safe. */
11114 if ((insn & (0x3f << 26)) == (56u << 26))
11115 mask = 15;
11116 if (((relocation + addend) & mask) != 0)
11117 {
11118 (*_bfd_error_handler)
11119 (_("%B: error: relocation %s not a multiple of %d"),
11120 input_bfd,
11121 ppc64_elf_howto_table[r_type]->name,
11122 mask + 1);
11123 bfd_set_error (bfd_error_bad_value);
11124 ret = FALSE;
11125 continue;
11126 }
11127 break;
11128 }
11129
11130 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11131 because such sections are not SEC_ALLOC and thus ld.so will
11132 not process them. */
11133 if (unresolved_reloc
11134 && !((input_section->flags & SEC_DEBUGGING) != 0
11135 && h->elf.def_dynamic))
11136 {
11137 (*_bfd_error_handler)
11138 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11139 input_bfd,
11140 input_section,
11141 (long) rel->r_offset,
11142 ppc64_elf_howto_table[(int) r_type]->name,
11143 h->elf.root.root.string);
11144 ret = FALSE;
11145 }
11146
11147 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
11148 input_bfd,
11149 input_section,
11150 contents,
11151 rel->r_offset,
11152 relocation,
11153 addend);
11154
11155 if (r != bfd_reloc_ok)
11156 {
11157 if (sym_name == NULL)
11158 sym_name = "(null)";
11159 if (r == bfd_reloc_overflow)
11160 {
11161 if (warned)
11162 continue;
11163 if (h != NULL
11164 && h->elf.root.type == bfd_link_hash_undefweak
11165 && ppc64_elf_howto_table[r_type]->pc_relative)
11166 {
11167 /* Assume this is a call protected by other code that
11168 detects the symbol is undefined. If this is the case,
11169 we can safely ignore the overflow. If not, the
11170 program is hosed anyway, and a little warning isn't
11171 going to help. */
11172
11173 continue;
11174 }
11175
11176 if (!((*info->callbacks->reloc_overflow)
11177 (info, (h ? &h->elf.root : NULL), sym_name,
11178 ppc64_elf_howto_table[r_type]->name,
11179 orig_addend, input_bfd, input_section, rel->r_offset)))
11180 return FALSE;
11181 }
11182 else
11183 {
11184 (*_bfd_error_handler)
11185 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
11186 input_bfd,
11187 input_section,
11188 (long) rel->r_offset,
11189 ppc64_elf_howto_table[r_type]->name,
11190 sym_name,
11191 (int) r);
11192 ret = FALSE;
11193 }
11194 }
11195 }
11196
11197 /* If we're emitting relocations, then shortly after this function
11198 returns, reloc offsets and addends for this section will be
11199 adjusted. Worse, reloc symbol indices will be for the output
11200 file rather than the input. Save a copy of the relocs for
11201 opd_entry_value. */
11202 if (is_opd && (info->emitrelocations || info->relocatable))
11203 {
11204 bfd_size_type amt;
11205 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
11206 rel = bfd_alloc (input_bfd, amt);
11207 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
11208 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
11209 if (rel == NULL)
11210 return FALSE;
11211 memcpy (rel, relocs, amt);
11212 }
11213 return ret;
11214 }
11215
11216 /* Adjust the value of any local symbols in opd sections. */
11217
11218 static bfd_boolean
11219 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11220 const char *name ATTRIBUTE_UNUSED,
11221 Elf_Internal_Sym *elfsym,
11222 asection *input_sec,
11223 struct elf_link_hash_entry *h)
11224 {
11225 long *opd_adjust, adjust;
11226 bfd_vma value;
11227
11228 if (h != NULL)
11229 return TRUE;
11230
11231 opd_adjust = get_opd_info (input_sec);
11232 if (opd_adjust == NULL)
11233 return TRUE;
11234
11235 value = elfsym->st_value - input_sec->output_offset;
11236 if (!info->relocatable)
11237 value -= input_sec->output_section->vma;
11238
11239 adjust = opd_adjust[value / 8];
11240 if (adjust == -1)
11241 elfsym->st_value = 0;
11242 else
11243 elfsym->st_value += adjust;
11244 return TRUE;
11245 }
11246
11247 /* Finish up dynamic symbol handling. We set the contents of various
11248 dynamic sections here. */
11249
11250 static bfd_boolean
11251 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11252 struct bfd_link_info *info,
11253 struct elf_link_hash_entry *h,
11254 Elf_Internal_Sym *sym)
11255 {
11256 struct ppc_link_hash_table *htab;
11257 struct plt_entry *ent;
11258 Elf_Internal_Rela rela;
11259 bfd_byte *loc;
11260
11261 htab = ppc_hash_table (info);
11262
11263 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11264 if (ent->plt.offset != (bfd_vma) -1)
11265 {
11266 /* This symbol has an entry in the procedure linkage
11267 table. Set it up. */
11268
11269 if (htab->plt == NULL
11270 || htab->relplt == NULL
11271 || htab->glink == NULL)
11272 abort ();
11273
11274 /* Create a JMP_SLOT reloc to inform the dynamic linker to
11275 fill in the PLT entry. */
11276 rela.r_offset = (htab->plt->output_section->vma
11277 + htab->plt->output_offset
11278 + ent->plt.offset);
11279 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11280 rela.r_addend = ent->addend;
11281
11282 loc = htab->relplt->contents;
11283 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11284 * sizeof (Elf64_External_Rela));
11285 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11286 }
11287
11288 if (h->needs_copy)
11289 {
11290 Elf_Internal_Rela rela;
11291 bfd_byte *loc;
11292
11293 /* This symbol needs a copy reloc. Set it up. */
11294
11295 if (h->dynindx == -1
11296 || (h->root.type != bfd_link_hash_defined
11297 && h->root.type != bfd_link_hash_defweak)
11298 || htab->relbss == NULL)
11299 abort ();
11300
11301 rela.r_offset = (h->root.u.def.value
11302 + h->root.u.def.section->output_section->vma
11303 + h->root.u.def.section->output_offset);
11304 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11305 rela.r_addend = 0;
11306 loc = htab->relbss->contents;
11307 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
11308 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11309 }
11310
11311 /* Mark some specially defined symbols as absolute. */
11312 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11313 sym->st_shndx = SHN_ABS;
11314
11315 return TRUE;
11316 }
11317
11318 /* Used to decide how to sort relocs in an optimal manner for the
11319 dynamic linker, before writing them out. */
11320
11321 static enum elf_reloc_type_class
11322 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
11323 {
11324 enum elf_ppc64_reloc_type r_type;
11325
11326 r_type = ELF64_R_TYPE (rela->r_info);
11327 switch (r_type)
11328 {
11329 case R_PPC64_RELATIVE:
11330 return reloc_class_relative;
11331 case R_PPC64_JMP_SLOT:
11332 return reloc_class_plt;
11333 case R_PPC64_COPY:
11334 return reloc_class_copy;
11335 default:
11336 return reloc_class_normal;
11337 }
11338 }
11339
11340 /* Finish up the dynamic sections. */
11341
11342 static bfd_boolean
11343 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11344 struct bfd_link_info *info)
11345 {
11346 struct ppc_link_hash_table *htab;
11347 bfd *dynobj;
11348 asection *sdyn;
11349
11350 htab = ppc_hash_table (info);
11351 dynobj = htab->elf.dynobj;
11352 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11353
11354 if (htab->elf.dynamic_sections_created)
11355 {
11356 Elf64_External_Dyn *dyncon, *dynconend;
11357
11358 if (sdyn == NULL || htab->got == NULL)
11359 abort ();
11360
11361 dyncon = (Elf64_External_Dyn *) sdyn->contents;
11362 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
11363 for (; dyncon < dynconend; dyncon++)
11364 {
11365 Elf_Internal_Dyn dyn;
11366 asection *s;
11367
11368 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11369
11370 switch (dyn.d_tag)
11371 {
11372 default:
11373 continue;
11374
11375 case DT_PPC64_GLINK:
11376 s = htab->glink;
11377 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11378 /* We stupidly defined DT_PPC64_GLINK to be the start
11379 of glink rather than the first entry point, which is
11380 what ld.so needs, and now have a bigger stub to
11381 support automatic multiple TOCs. */
11382 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
11383 break;
11384
11385 case DT_PPC64_OPD:
11386 s = bfd_get_section_by_name (output_bfd, ".opd");
11387 if (s == NULL)
11388 continue;
11389 dyn.d_un.d_ptr = s->vma;
11390 break;
11391
11392 case DT_PPC64_OPDSZ:
11393 s = bfd_get_section_by_name (output_bfd, ".opd");
11394 if (s == NULL)
11395 continue;
11396 dyn.d_un.d_val = s->size;
11397 break;
11398
11399 case DT_PLTGOT:
11400 s = htab->plt;
11401 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11402 break;
11403
11404 case DT_JMPREL:
11405 s = htab->relplt;
11406 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11407 break;
11408
11409 case DT_PLTRELSZ:
11410 dyn.d_un.d_val = htab->relplt->size;
11411 break;
11412
11413 case DT_RELASZ:
11414 /* Don't count procedure linkage table relocs in the
11415 overall reloc count. */
11416 s = htab->relplt;
11417 if (s == NULL)
11418 continue;
11419 dyn.d_un.d_val -= s->size;
11420 break;
11421
11422 case DT_RELA:
11423 /* We may not be using the standard ELF linker script.
11424 If .rela.plt is the first .rela section, we adjust
11425 DT_RELA to not include it. */
11426 s = htab->relplt;
11427 if (s == NULL)
11428 continue;
11429 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11430 continue;
11431 dyn.d_un.d_ptr += s->size;
11432 break;
11433 }
11434
11435 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
11436 }
11437 }
11438
11439 if (htab->got != NULL && htab->got->size != 0)
11440 {
11441 /* Fill in the first entry in the global offset table.
11442 We use it to hold the link-time TOCbase. */
11443 bfd_put_64 (output_bfd,
11444 elf_gp (output_bfd) + TOC_BASE_OFF,
11445 htab->got->contents);
11446
11447 /* Set .got entry size. */
11448 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
11449 }
11450
11451 if (htab->plt != NULL && htab->plt->size != 0)
11452 {
11453 /* Set .plt entry size. */
11454 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
11455 = PLT_ENTRY_SIZE;
11456 }
11457
11458 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
11459 brlt ourselves if emitrelocations. */
11460 if (htab->brlt != NULL
11461 && htab->brlt->reloc_count != 0
11462 && !_bfd_elf_link_output_relocs (output_bfd,
11463 htab->brlt,
11464 &elf_section_data (htab->brlt)->rel_hdr,
11465 elf_section_data (htab->brlt)->relocs,
11466 NULL))
11467 return FALSE;
11468
11469 /* We need to handle writing out multiple GOT sections ourselves,
11470 since we didn't add them to DYNOBJ. We know dynobj is the first
11471 bfd. */
11472 while ((dynobj = dynobj->link_next) != NULL)
11473 {
11474 asection *s;
11475
11476 if (!is_ppc64_elf_target (dynobj->xvec))
11477 continue;
11478
11479 s = ppc64_elf_tdata (dynobj)->got;
11480 if (s != NULL
11481 && s->size != 0
11482 && s->output_section != bfd_abs_section_ptr
11483 && !bfd_set_section_contents (output_bfd, s->output_section,
11484 s->contents, s->output_offset,
11485 s->size))
11486 return FALSE;
11487 s = ppc64_elf_tdata (dynobj)->relgot;
11488 if (s != NULL
11489 && s->size != 0
11490 && s->output_section != bfd_abs_section_ptr
11491 && !bfd_set_section_contents (output_bfd, s->output_section,
11492 s->contents, s->output_offset,
11493 s->size))
11494 return FALSE;
11495 }
11496
11497 return TRUE;
11498 }
11499
11500 #include "elf64-target.h"