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