try_copy_symbol_type node_class check
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
... / ...
CommitLineData
1/* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
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 "sysdep.h"
29#include <stdarg.h>
30#include "bfd.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#include "dwarf2.h"
37
38static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59#define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60#define TARGET_LITTLE_NAME "elf64-powerpcle"
61#define TARGET_BIG_SYM powerpc_elf64_vec
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
64#define ELF_TARGET_ID PPC64_ELF_DATA
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
67#define ELF_COMMONPAGESIZE 0x10000
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
74#define elf_backend_got_header_size 8
75#define elf_backend_can_gc_sections 1
76#define elf_backend_can_refcount 1
77#define elf_backend_rela_normal 1
78#define elf_backend_dtrel_excludes_plt 1
79#define elf_backend_default_execstack 0
80
81#define bfd_elf64_mkobject ppc64_elf_mkobject
82#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
83#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
84#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
85#define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
86#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
87#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
88#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
89#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
90
91#define elf_backend_object_p ppc64_elf_object_p
92#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
94#define elf_backend_write_core_note ppc64_elf_write_core_note
95#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
97#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
98#define elf_backend_check_directives ppc64_elf_before_check_relocs
99#define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
100#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
101#define elf_backend_check_relocs ppc64_elf_check_relocs
102#define elf_backend_gc_keep ppc64_elf_gc_keep
103#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
104#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107#define elf_backend_hide_symbol ppc64_elf_hide_symbol
108#define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
109#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
110#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
111#define elf_backend_hash_symbol ppc64_elf_hash_symbol
112#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
113#define elf_backend_action_discarded ppc64_elf_action_discarded
114#define elf_backend_relocate_section ppc64_elf_relocate_section
115#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
116#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
117#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
118#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
119#define elf_backend_special_sections ppc64_elf_special_sections
120#define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
121
122/* The name of the dynamic interpreter. This is put in the .interp
123 section. */
124#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
125
126/* The size in bytes of an entry in the procedure linkage table. */
127#define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
128
129/* The initial size of the plt reserved for the dynamic linker. */
130#define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
131
132/* Offsets to some stack save slots. */
133#define STK_LR 16
134#define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
135/* This one is dodgy. ELFv2 does not have a linker word, so use the
136 CR save slot. Used only by optimised __tls_get_addr call stub,
137 relying on __tls_get_addr_opt not saving CR.. */
138#define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
139
140/* TOC base pointers offset from start of TOC. */
141#define TOC_BASE_OFF 0x8000
142/* TOC base alignment. */
143#define TOC_BASE_ALIGN 256
144
145/* Offset of tp and dtp pointers from start of TLS block. */
146#define TP_OFFSET 0x7000
147#define DTP_OFFSET 0x8000
148
149/* .plt call stub instructions. The normal stub is like this, but
150 sometimes the .plt entry crosses a 64k boundary and we need to
151 insert an addi to adjust r11. */
152#define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
153#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
154#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
155#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
156#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
157#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
158#define BCTR 0x4e800420 /* bctr */
159
160#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
161#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
162#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
163
164#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
165#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
166#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
167#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
168#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
169#define BNECTR 0x4ca20420 /* bnectr+ */
170#define BNECTR_P4 0x4ce20420 /* bnectr+ */
171
172#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
173#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
174#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
175
176#define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
177#define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
178#define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
179
180#define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
181#define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
182#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
183#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
184#define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
185
186/* glink call stub instructions. We enter with the index in R0. */
187#define GLINK_CALL_STUB_SIZE (16*4)
188 /* 0: */
189 /* .quad plt0-1f */
190 /* __glink: */
191#define MFLR_R12 0x7d8802a6 /* mflr %12 */
192#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
193 /* 1: */
194#define MFLR_R11 0x7d6802a6 /* mflr %11 */
195 /* ld %2,(0b-1b)(%11) */
196#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
197#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
198 /* ld %12,0(%11) */
199 /* ld %2,8(%11) */
200 /* mtctr %12 */
201 /* ld %11,16(%11) */
202 /* bctr */
203#define MFLR_R0 0x7c0802a6 /* mflr %r0 */
204#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
205#define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
206#define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
207#define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
208
209/* Pad with this. */
210#define NOP 0x60000000
211
212/* Some other nops. */
213#define CROR_151515 0x4def7b82
214#define CROR_313131 0x4ffffb82
215
216/* .glink entries for the first 32k functions are two instructions. */
217#define LI_R0_0 0x38000000 /* li %r0,0 */
218#define B_DOT 0x48000000 /* b . */
219
220/* After that, we need two instructions to load the index, followed by
221 a branch. */
222#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
223#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
224
225/* Instructions used by the save and restore reg functions. */
226#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
227#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
228#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
229#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
230#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
231#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
232#define LI_R12_0 0x39800000 /* li %r12,0 */
233#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
234#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
235#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
236#define BLR 0x4e800020 /* blr */
237
238/* Since .opd is an array of descriptors and each entry will end up
239 with identical R_PPC64_RELATIVE relocs, there is really no need to
240 propagate .opd relocs; The dynamic linker should be taught to
241 relocate .opd without reloc entries. */
242#ifndef NO_OPD_RELOCS
243#define NO_OPD_RELOCS 0
244#endif
245
246#ifndef ARRAY_SIZE
247#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
248#endif
249
250static inline int
251abiversion (bfd *abfd)
252{
253 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
254}
255
256static inline void
257set_abiversion (bfd *abfd, int ver)
258{
259 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
260 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
261}
262\f
263#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
264
265/* Relocation HOWTO's. */
266static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
267
268static reloc_howto_type ppc64_elf_howto_raw[] = {
269 /* This reloc does nothing. */
270 HOWTO (R_PPC64_NONE, /* type */
271 0, /* rightshift */
272 3, /* size (0 = byte, 1 = short, 2 = long) */
273 0, /* bitsize */
274 FALSE, /* pc_relative */
275 0, /* bitpos */
276 complain_overflow_dont, /* complain_on_overflow */
277 bfd_elf_generic_reloc, /* special_function */
278 "R_PPC64_NONE", /* name */
279 FALSE, /* partial_inplace */
280 0, /* src_mask */
281 0, /* dst_mask */
282 FALSE), /* pcrel_offset */
283
284 /* A standard 32 bit relocation. */
285 HOWTO (R_PPC64_ADDR32, /* type */
286 0, /* rightshift */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
288 32, /* bitsize */
289 FALSE, /* pc_relative */
290 0, /* bitpos */
291 complain_overflow_bitfield, /* complain_on_overflow */
292 bfd_elf_generic_reloc, /* special_function */
293 "R_PPC64_ADDR32", /* name */
294 FALSE, /* partial_inplace */
295 0, /* src_mask */
296 0xffffffff, /* dst_mask */
297 FALSE), /* pcrel_offset */
298
299 /* An absolute 26 bit branch; the lower two bits must be zero.
300 FIXME: we don't check that, we just clear them. */
301 HOWTO (R_PPC64_ADDR24, /* type */
302 0, /* rightshift */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
304 26, /* bitsize */
305 FALSE, /* pc_relative */
306 0, /* bitpos */
307 complain_overflow_bitfield, /* complain_on_overflow */
308 bfd_elf_generic_reloc, /* special_function */
309 "R_PPC64_ADDR24", /* name */
310 FALSE, /* partial_inplace */
311 0, /* src_mask */
312 0x03fffffc, /* dst_mask */
313 FALSE), /* pcrel_offset */
314
315 /* A standard 16 bit relocation. */
316 HOWTO (R_PPC64_ADDR16, /* type */
317 0, /* rightshift */
318 1, /* size (0 = byte, 1 = short, 2 = long) */
319 16, /* bitsize */
320 FALSE, /* pc_relative */
321 0, /* bitpos */
322 complain_overflow_bitfield, /* complain_on_overflow */
323 bfd_elf_generic_reloc, /* special_function */
324 "R_PPC64_ADDR16", /* name */
325 FALSE, /* partial_inplace */
326 0, /* src_mask */
327 0xffff, /* dst_mask */
328 FALSE), /* pcrel_offset */
329
330 /* A 16 bit relocation without overflow. */
331 HOWTO (R_PPC64_ADDR16_LO, /* type */
332 0, /* rightshift */
333 1, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 FALSE, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_dont,/* complain_on_overflow */
338 bfd_elf_generic_reloc, /* special_function */
339 "R_PPC64_ADDR16_LO", /* name */
340 FALSE, /* partial_inplace */
341 0, /* src_mask */
342 0xffff, /* dst_mask */
343 FALSE), /* pcrel_offset */
344
345 /* Bits 16-31 of an address. */
346 HOWTO (R_PPC64_ADDR16_HI, /* type */
347 16, /* rightshift */
348 1, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_signed, /* complain_on_overflow */
353 bfd_elf_generic_reloc, /* special_function */
354 "R_PPC64_ADDR16_HI", /* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0xffff, /* dst_mask */
358 FALSE), /* pcrel_offset */
359
360 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
361 bits, treated as a signed number, is negative. */
362 HOWTO (R_PPC64_ADDR16_HA, /* type */
363 16, /* rightshift */
364 1, /* size (0 = byte, 1 = short, 2 = long) */
365 16, /* bitsize */
366 FALSE, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_signed, /* complain_on_overflow */
369 ppc64_elf_ha_reloc, /* special_function */
370 "R_PPC64_ADDR16_HA", /* name */
371 FALSE, /* partial_inplace */
372 0, /* src_mask */
373 0xffff, /* dst_mask */
374 FALSE), /* pcrel_offset */
375
376 /* An absolute 16 bit branch; the lower two bits must be zero.
377 FIXME: we don't check that, we just clear them. */
378 HOWTO (R_PPC64_ADDR14, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 16, /* bitsize */
382 FALSE, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
385 ppc64_elf_branch_reloc, /* special_function */
386 "R_PPC64_ADDR14", /* name */
387 FALSE, /* partial_inplace */
388 0, /* src_mask */
389 0x0000fffc, /* dst_mask */
390 FALSE), /* pcrel_offset */
391
392 /* An absolute 16 bit branch, for which bit 10 should be set to
393 indicate that the branch is expected to be taken. The lower two
394 bits must be zero. */
395 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_signed, /* complain_on_overflow */
402 ppc64_elf_brtaken_reloc, /* special_function */
403 "R_PPC64_ADDR14_BRTAKEN",/* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0x0000fffc, /* dst_mask */
407 FALSE), /* pcrel_offset */
408
409 /* An absolute 16 bit branch, for which bit 10 should be set to
410 indicate that the branch is not expected to be taken. The lower
411 two bits must be zero. */
412 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 FALSE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 ppc64_elf_brtaken_reloc, /* special_function */
420 "R_PPC64_ADDR14_BRNTAKEN",/* name */
421 FALSE, /* partial_inplace */
422 0, /* src_mask */
423 0x0000fffc, /* dst_mask */
424 FALSE), /* pcrel_offset */
425
426 /* A relative 26 bit branch; the lower two bits must be zero. */
427 HOWTO (R_PPC64_REL24, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 26, /* bitsize */
431 TRUE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
434 ppc64_elf_branch_reloc, /* special_function */
435 "R_PPC64_REL24", /* name */
436 FALSE, /* partial_inplace */
437 0, /* src_mask */
438 0x03fffffc, /* dst_mask */
439 TRUE), /* pcrel_offset */
440
441 /* A relative 16 bit branch; the lower two bits must be zero. */
442 HOWTO (R_PPC64_REL14, /* type */
443 0, /* rightshift */
444 2, /* size (0 = byte, 1 = short, 2 = long) */
445 16, /* bitsize */
446 TRUE, /* pc_relative */
447 0, /* bitpos */
448 complain_overflow_signed, /* complain_on_overflow */
449 ppc64_elf_branch_reloc, /* special_function */
450 "R_PPC64_REL14", /* name */
451 FALSE, /* partial_inplace */
452 0, /* src_mask */
453 0x0000fffc, /* dst_mask */
454 TRUE), /* pcrel_offset */
455
456 /* A relative 16 bit branch. Bit 10 should be set to indicate that
457 the branch is expected to be taken. The lower two bits must be
458 zero. */
459 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
460 0, /* rightshift */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
463 TRUE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_signed, /* complain_on_overflow */
466 ppc64_elf_brtaken_reloc, /* special_function */
467 "R_PPC64_REL14_BRTAKEN", /* name */
468 FALSE, /* partial_inplace */
469 0, /* src_mask */
470 0x0000fffc, /* dst_mask */
471 TRUE), /* pcrel_offset */
472
473 /* A relative 16 bit branch. Bit 10 should be set to indicate that
474 the branch is not expected to be taken. The lower two bits must
475 be zero. */
476 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
477 0, /* rightshift */
478 2, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
480 TRUE, /* pc_relative */
481 0, /* bitpos */
482 complain_overflow_signed, /* complain_on_overflow */
483 ppc64_elf_brtaken_reloc, /* special_function */
484 "R_PPC64_REL14_BRNTAKEN",/* name */
485 FALSE, /* partial_inplace */
486 0, /* src_mask */
487 0x0000fffc, /* dst_mask */
488 TRUE), /* pcrel_offset */
489
490 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
491 symbol. */
492 HOWTO (R_PPC64_GOT16, /* type */
493 0, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
496 FALSE, /* pc_relative */
497 0, /* bitpos */
498 complain_overflow_signed, /* complain_on_overflow */
499 ppc64_elf_unhandled_reloc, /* special_function */
500 "R_PPC64_GOT16", /* name */
501 FALSE, /* partial_inplace */
502 0, /* src_mask */
503 0xffff, /* dst_mask */
504 FALSE), /* pcrel_offset */
505
506 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
507 the symbol. */
508 HOWTO (R_PPC64_GOT16_LO, /* type */
509 0, /* rightshift */
510 1, /* size (0 = byte, 1 = short, 2 = long) */
511 16, /* bitsize */
512 FALSE, /* pc_relative */
513 0, /* bitpos */
514 complain_overflow_dont, /* complain_on_overflow */
515 ppc64_elf_unhandled_reloc, /* special_function */
516 "R_PPC64_GOT16_LO", /* name */
517 FALSE, /* partial_inplace */
518 0, /* src_mask */
519 0xffff, /* dst_mask */
520 FALSE), /* pcrel_offset */
521
522 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
523 the symbol. */
524 HOWTO (R_PPC64_GOT16_HI, /* type */
525 16, /* rightshift */
526 1, /* size (0 = byte, 1 = short, 2 = long) */
527 16, /* bitsize */
528 FALSE, /* pc_relative */
529 0, /* bitpos */
530 complain_overflow_signed,/* complain_on_overflow */
531 ppc64_elf_unhandled_reloc, /* special_function */
532 "R_PPC64_GOT16_HI", /* name */
533 FALSE, /* partial_inplace */
534 0, /* src_mask */
535 0xffff, /* dst_mask */
536 FALSE), /* pcrel_offset */
537
538 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
539 the symbol. */
540 HOWTO (R_PPC64_GOT16_HA, /* type */
541 16, /* rightshift */
542 1, /* size (0 = byte, 1 = short, 2 = long) */
543 16, /* bitsize */
544 FALSE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_signed,/* complain_on_overflow */
547 ppc64_elf_unhandled_reloc, /* special_function */
548 "R_PPC64_GOT16_HA", /* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0xffff, /* dst_mask */
552 FALSE), /* pcrel_offset */
553
554 /* This is used only by the dynamic linker. The symbol should exist
555 both in the object being run and in some shared library. The
556 dynamic linker copies the data addressed by the symbol from the
557 shared library into the object, because the object being
558 run has to have the data at some particular address. */
559 HOWTO (R_PPC64_COPY, /* type */
560 0, /* rightshift */
561 0, /* this one is variable size */
562 0, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_dont, /* complain_on_overflow */
566 ppc64_elf_unhandled_reloc, /* special_function */
567 "R_PPC64_COPY", /* name */
568 FALSE, /* partial_inplace */
569 0, /* src_mask */
570 0, /* dst_mask */
571 FALSE), /* pcrel_offset */
572
573 /* Like R_PPC64_ADDR64, but used when setting global offset table
574 entries. */
575 HOWTO (R_PPC64_GLOB_DAT, /* type */
576 0, /* rightshift */
577 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
578 64, /* bitsize */
579 FALSE, /* pc_relative */
580 0, /* bitpos */
581 complain_overflow_dont, /* complain_on_overflow */
582 ppc64_elf_unhandled_reloc, /* special_function */
583 "R_PPC64_GLOB_DAT", /* name */
584 FALSE, /* partial_inplace */
585 0, /* src_mask */
586 ONES (64), /* dst_mask */
587 FALSE), /* pcrel_offset */
588
589 /* Created by the link editor. Marks a procedure linkage table
590 entry for a symbol. */
591 HOWTO (R_PPC64_JMP_SLOT, /* type */
592 0, /* rightshift */
593 0, /* size (0 = byte, 1 = short, 2 = long) */
594 0, /* bitsize */
595 FALSE, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_dont, /* complain_on_overflow */
598 ppc64_elf_unhandled_reloc, /* special_function */
599 "R_PPC64_JMP_SLOT", /* name */
600 FALSE, /* partial_inplace */
601 0, /* src_mask */
602 0, /* dst_mask */
603 FALSE), /* pcrel_offset */
604
605 /* Used only by the dynamic linker. When the object is run, this
606 doubleword64 is set to the load address of the object, plus the
607 addend. */
608 HOWTO (R_PPC64_RELATIVE, /* type */
609 0, /* rightshift */
610 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
611 64, /* bitsize */
612 FALSE, /* pc_relative */
613 0, /* bitpos */
614 complain_overflow_dont, /* complain_on_overflow */
615 bfd_elf_generic_reloc, /* special_function */
616 "R_PPC64_RELATIVE", /* name */
617 FALSE, /* partial_inplace */
618 0, /* src_mask */
619 ONES (64), /* dst_mask */
620 FALSE), /* pcrel_offset */
621
622 /* Like R_PPC64_ADDR32, but may be unaligned. */
623 HOWTO (R_PPC64_UADDR32, /* type */
624 0, /* rightshift */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
626 32, /* bitsize */
627 FALSE, /* pc_relative */
628 0, /* bitpos */
629 complain_overflow_bitfield, /* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_PPC64_UADDR32", /* name */
632 FALSE, /* partial_inplace */
633 0, /* src_mask */
634 0xffffffff, /* dst_mask */
635 FALSE), /* pcrel_offset */
636
637 /* Like R_PPC64_ADDR16, but may be unaligned. */
638 HOWTO (R_PPC64_UADDR16, /* type */
639 0, /* rightshift */
640 1, /* size (0 = byte, 1 = short, 2 = long) */
641 16, /* bitsize */
642 FALSE, /* pc_relative */
643 0, /* bitpos */
644 complain_overflow_bitfield, /* complain_on_overflow */
645 bfd_elf_generic_reloc, /* special_function */
646 "R_PPC64_UADDR16", /* name */
647 FALSE, /* partial_inplace */
648 0, /* src_mask */
649 0xffff, /* dst_mask */
650 FALSE), /* pcrel_offset */
651
652 /* 32-bit PC relative. */
653 HOWTO (R_PPC64_REL32, /* type */
654 0, /* rightshift */
655 2, /* size (0 = byte, 1 = short, 2 = long) */
656 32, /* bitsize */
657 TRUE, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_signed, /* complain_on_overflow */
660 bfd_elf_generic_reloc, /* special_function */
661 "R_PPC64_REL32", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0xffffffff, /* dst_mask */
665 TRUE), /* pcrel_offset */
666
667 /* 32-bit relocation to the symbol's procedure linkage table. */
668 HOWTO (R_PPC64_PLT32, /* type */
669 0, /* rightshift */
670 2, /* size (0 = byte, 1 = short, 2 = long) */
671 32, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_bitfield, /* complain_on_overflow */
675 ppc64_elf_unhandled_reloc, /* special_function */
676 "R_PPC64_PLT32", /* name */
677 FALSE, /* partial_inplace */
678 0, /* src_mask */
679 0xffffffff, /* dst_mask */
680 FALSE), /* pcrel_offset */
681
682 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
683 FIXME: R_PPC64_PLTREL32 not supported. */
684 HOWTO (R_PPC64_PLTREL32, /* type */
685 0, /* rightshift */
686 2, /* size (0 = byte, 1 = short, 2 = long) */
687 32, /* bitsize */
688 TRUE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_signed, /* complain_on_overflow */
691 ppc64_elf_unhandled_reloc, /* special_function */
692 "R_PPC64_PLTREL32", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0xffffffff, /* dst_mask */
696 TRUE), /* pcrel_offset */
697
698 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
699 the symbol. */
700 HOWTO (R_PPC64_PLT16_LO, /* type */
701 0, /* rightshift */
702 1, /* size (0 = byte, 1 = short, 2 = long) */
703 16, /* bitsize */
704 FALSE, /* pc_relative */
705 0, /* bitpos */
706 complain_overflow_dont, /* complain_on_overflow */
707 ppc64_elf_unhandled_reloc, /* special_function */
708 "R_PPC64_PLT16_LO", /* name */
709 FALSE, /* partial_inplace */
710 0, /* src_mask */
711 0xffff, /* dst_mask */
712 FALSE), /* pcrel_offset */
713
714 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
715 the symbol. */
716 HOWTO (R_PPC64_PLT16_HI, /* type */
717 16, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
720 FALSE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
723 ppc64_elf_unhandled_reloc, /* special_function */
724 "R_PPC64_PLT16_HI", /* name */
725 FALSE, /* partial_inplace */
726 0, /* src_mask */
727 0xffff, /* dst_mask */
728 FALSE), /* pcrel_offset */
729
730 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
731 the symbol. */
732 HOWTO (R_PPC64_PLT16_HA, /* type */
733 16, /* rightshift */
734 1, /* size (0 = byte, 1 = short, 2 = long) */
735 16, /* bitsize */
736 FALSE, /* pc_relative */
737 0, /* bitpos */
738 complain_overflow_signed, /* complain_on_overflow */
739 ppc64_elf_unhandled_reloc, /* special_function */
740 "R_PPC64_PLT16_HA", /* name */
741 FALSE, /* partial_inplace */
742 0, /* src_mask */
743 0xffff, /* dst_mask */
744 FALSE), /* pcrel_offset */
745
746 /* 16-bit section relative relocation. */
747 HOWTO (R_PPC64_SECTOFF, /* type */
748 0, /* rightshift */
749 1, /* size (0 = byte, 1 = short, 2 = long) */
750 16, /* bitsize */
751 FALSE, /* pc_relative */
752 0, /* bitpos */
753 complain_overflow_signed, /* complain_on_overflow */
754 ppc64_elf_sectoff_reloc, /* special_function */
755 "R_PPC64_SECTOFF", /* name */
756 FALSE, /* partial_inplace */
757 0, /* src_mask */
758 0xffff, /* dst_mask */
759 FALSE), /* pcrel_offset */
760
761 /* Like R_PPC64_SECTOFF, but no overflow warning. */
762 HOWTO (R_PPC64_SECTOFF_LO, /* type */
763 0, /* rightshift */
764 1, /* size (0 = byte, 1 = short, 2 = long) */
765 16, /* bitsize */
766 FALSE, /* pc_relative */
767 0, /* bitpos */
768 complain_overflow_dont, /* complain_on_overflow */
769 ppc64_elf_sectoff_reloc, /* special_function */
770 "R_PPC64_SECTOFF_LO", /* name */
771 FALSE, /* partial_inplace */
772 0, /* src_mask */
773 0xffff, /* dst_mask */
774 FALSE), /* pcrel_offset */
775
776 /* 16-bit upper half section relative relocation. */
777 HOWTO (R_PPC64_SECTOFF_HI, /* type */
778 16, /* rightshift */
779 1, /* size (0 = byte, 1 = short, 2 = long) */
780 16, /* bitsize */
781 FALSE, /* pc_relative */
782 0, /* bitpos */
783 complain_overflow_signed, /* complain_on_overflow */
784 ppc64_elf_sectoff_reloc, /* special_function */
785 "R_PPC64_SECTOFF_HI", /* name */
786 FALSE, /* partial_inplace */
787 0, /* src_mask */
788 0xffff, /* dst_mask */
789 FALSE), /* pcrel_offset */
790
791 /* 16-bit upper half adjusted section relative relocation. */
792 HOWTO (R_PPC64_SECTOFF_HA, /* type */
793 16, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_signed, /* complain_on_overflow */
799 ppc64_elf_sectoff_ha_reloc, /* special_function */
800 "R_PPC64_SECTOFF_HA", /* name */
801 FALSE, /* partial_inplace */
802 0, /* src_mask */
803 0xffff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* Like R_PPC64_REL24 without touching the two least significant bits. */
807 HOWTO (R_PPC64_REL30, /* type */
808 2, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 30, /* bitsize */
811 TRUE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 bfd_elf_generic_reloc, /* special_function */
815 "R_PPC64_REL30", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xfffffffc, /* dst_mask */
819 TRUE), /* pcrel_offset */
820
821 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
822
823 /* A standard 64-bit relocation. */
824 HOWTO (R_PPC64_ADDR64, /* type */
825 0, /* rightshift */
826 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
827 64, /* bitsize */
828 FALSE, /* pc_relative */
829 0, /* bitpos */
830 complain_overflow_dont, /* complain_on_overflow */
831 bfd_elf_generic_reloc, /* special_function */
832 "R_PPC64_ADDR64", /* name */
833 FALSE, /* partial_inplace */
834 0, /* src_mask */
835 ONES (64), /* dst_mask */
836 FALSE), /* pcrel_offset */
837
838 /* The bits 32-47 of an address. */
839 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
840 32, /* rightshift */
841 1, /* size (0 = byte, 1 = short, 2 = long) */
842 16, /* bitsize */
843 FALSE, /* pc_relative */
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
846 bfd_elf_generic_reloc, /* special_function */
847 "R_PPC64_ADDR16_HIGHER", /* name */
848 FALSE, /* partial_inplace */
849 0, /* src_mask */
850 0xffff, /* dst_mask */
851 FALSE), /* pcrel_offset */
852
853 /* The bits 32-47 of an address, plus 1 if the contents of the low
854 16 bits, treated as a signed number, is negative. */
855 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
856 32, /* rightshift */
857 1, /* size (0 = byte, 1 = short, 2 = long) */
858 16, /* bitsize */
859 FALSE, /* pc_relative */
860 0, /* bitpos */
861 complain_overflow_dont, /* complain_on_overflow */
862 ppc64_elf_ha_reloc, /* special_function */
863 "R_PPC64_ADDR16_HIGHERA", /* name */
864 FALSE, /* partial_inplace */
865 0, /* src_mask */
866 0xffff, /* dst_mask */
867 FALSE), /* pcrel_offset */
868
869 /* The bits 48-63 of an address. */
870 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
871 48, /* rightshift */
872 1, /* size (0 = byte, 1 = short, 2 = long) */
873 16, /* bitsize */
874 FALSE, /* pc_relative */
875 0, /* bitpos */
876 complain_overflow_dont, /* complain_on_overflow */
877 bfd_elf_generic_reloc, /* special_function */
878 "R_PPC64_ADDR16_HIGHEST", /* name */
879 FALSE, /* partial_inplace */
880 0, /* src_mask */
881 0xffff, /* dst_mask */
882 FALSE), /* pcrel_offset */
883
884 /* The bits 48-63 of an address, plus 1 if the contents of the low
885 16 bits, treated as a signed number, is negative. */
886 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
887 48, /* rightshift */
888 1, /* size (0 = byte, 1 = short, 2 = long) */
889 16, /* bitsize */
890 FALSE, /* pc_relative */
891 0, /* bitpos */
892 complain_overflow_dont, /* complain_on_overflow */
893 ppc64_elf_ha_reloc, /* special_function */
894 "R_PPC64_ADDR16_HIGHESTA", /* name */
895 FALSE, /* partial_inplace */
896 0, /* src_mask */
897 0xffff, /* dst_mask */
898 FALSE), /* pcrel_offset */
899
900 /* Like ADDR64, but may be unaligned. */
901 HOWTO (R_PPC64_UADDR64, /* type */
902 0, /* rightshift */
903 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
904 64, /* bitsize */
905 FALSE, /* pc_relative */
906 0, /* bitpos */
907 complain_overflow_dont, /* complain_on_overflow */
908 bfd_elf_generic_reloc, /* special_function */
909 "R_PPC64_UADDR64", /* name */
910 FALSE, /* partial_inplace */
911 0, /* src_mask */
912 ONES (64), /* dst_mask */
913 FALSE), /* pcrel_offset */
914
915 /* 64-bit relative relocation. */
916 HOWTO (R_PPC64_REL64, /* type */
917 0, /* rightshift */
918 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
919 64, /* bitsize */
920 TRUE, /* pc_relative */
921 0, /* bitpos */
922 complain_overflow_dont, /* complain_on_overflow */
923 bfd_elf_generic_reloc, /* special_function */
924 "R_PPC64_REL64", /* name */
925 FALSE, /* partial_inplace */
926 0, /* src_mask */
927 ONES (64), /* dst_mask */
928 TRUE), /* pcrel_offset */
929
930 /* 64-bit relocation to the symbol's procedure linkage table. */
931 HOWTO (R_PPC64_PLT64, /* type */
932 0, /* rightshift */
933 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
934 64, /* bitsize */
935 FALSE, /* pc_relative */
936 0, /* bitpos */
937 complain_overflow_dont, /* complain_on_overflow */
938 ppc64_elf_unhandled_reloc, /* special_function */
939 "R_PPC64_PLT64", /* name */
940 FALSE, /* partial_inplace */
941 0, /* src_mask */
942 ONES (64), /* dst_mask */
943 FALSE), /* pcrel_offset */
944
945 /* 64-bit PC relative relocation to the symbol's procedure linkage
946 table. */
947 /* FIXME: R_PPC64_PLTREL64 not supported. */
948 HOWTO (R_PPC64_PLTREL64, /* type */
949 0, /* rightshift */
950 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
951 64, /* bitsize */
952 TRUE, /* pc_relative */
953 0, /* bitpos */
954 complain_overflow_dont, /* complain_on_overflow */
955 ppc64_elf_unhandled_reloc, /* special_function */
956 "R_PPC64_PLTREL64", /* name */
957 FALSE, /* partial_inplace */
958 0, /* src_mask */
959 ONES (64), /* dst_mask */
960 TRUE), /* pcrel_offset */
961
962 /* 16 bit TOC-relative relocation. */
963
964 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
965 HOWTO (R_PPC64_TOC16, /* type */
966 0, /* rightshift */
967 1, /* size (0 = byte, 1 = short, 2 = long) */
968 16, /* bitsize */
969 FALSE, /* pc_relative */
970 0, /* bitpos */
971 complain_overflow_signed, /* complain_on_overflow */
972 ppc64_elf_toc_reloc, /* special_function */
973 "R_PPC64_TOC16", /* name */
974 FALSE, /* partial_inplace */
975 0, /* src_mask */
976 0xffff, /* dst_mask */
977 FALSE), /* pcrel_offset */
978
979 /* 16 bit TOC-relative relocation without overflow. */
980
981 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
982 HOWTO (R_PPC64_TOC16_LO, /* type */
983 0, /* rightshift */
984 1, /* size (0 = byte, 1 = short, 2 = long) */
985 16, /* bitsize */
986 FALSE, /* pc_relative */
987 0, /* bitpos */
988 complain_overflow_dont, /* complain_on_overflow */
989 ppc64_elf_toc_reloc, /* special_function */
990 "R_PPC64_TOC16_LO", /* name */
991 FALSE, /* partial_inplace */
992 0, /* src_mask */
993 0xffff, /* dst_mask */
994 FALSE), /* pcrel_offset */
995
996 /* 16 bit TOC-relative relocation, high 16 bits. */
997
998 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
999 HOWTO (R_PPC64_TOC16_HI, /* type */
1000 16, /* 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_toc_reloc, /* special_function */
1007 "R_PPC64_TOC16_HI", /* name */
1008 FALSE, /* partial_inplace */
1009 0, /* src_mask */
1010 0xffff, /* dst_mask */
1011 FALSE), /* pcrel_offset */
1012
1013 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1014 contents of the low 16 bits, treated as a signed number, is
1015 negative. */
1016
1017 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1018 HOWTO (R_PPC64_TOC16_HA, /* type */
1019 16, /* rightshift */
1020 1, /* size (0 = byte, 1 = short, 2 = long) */
1021 16, /* bitsize */
1022 FALSE, /* pc_relative */
1023 0, /* bitpos */
1024 complain_overflow_signed, /* complain_on_overflow */
1025 ppc64_elf_toc_ha_reloc, /* special_function */
1026 "R_PPC64_TOC16_HA", /* name */
1027 FALSE, /* partial_inplace */
1028 0, /* src_mask */
1029 0xffff, /* dst_mask */
1030 FALSE), /* pcrel_offset */
1031
1032 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1033
1034 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1035 HOWTO (R_PPC64_TOC, /* type */
1036 0, /* rightshift */
1037 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1038 64, /* bitsize */
1039 FALSE, /* pc_relative */
1040 0, /* bitpos */
1041 complain_overflow_dont, /* complain_on_overflow */
1042 ppc64_elf_toc64_reloc, /* special_function */
1043 "R_PPC64_TOC", /* name */
1044 FALSE, /* partial_inplace */
1045 0, /* src_mask */
1046 ONES (64), /* dst_mask */
1047 FALSE), /* pcrel_offset */
1048
1049 /* Like R_PPC64_GOT16, but also informs the link editor that the
1050 value to relocate may (!) refer to a PLT entry which the link
1051 editor (a) may replace with the symbol value. If the link editor
1052 is unable to fully resolve the symbol, it may (b) create a PLT
1053 entry and store the address to the new PLT entry in the GOT.
1054 This permits lazy resolution of function symbols at run time.
1055 The link editor may also skip all of this and just (c) emit a
1056 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1057 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1058 HOWTO (R_PPC64_PLTGOT16, /* type */
1059 0, /* rightshift */
1060 1, /* size (0 = byte, 1 = short, 2 = long) */
1061 16, /* bitsize */
1062 FALSE, /* pc_relative */
1063 0, /* bitpos */
1064 complain_overflow_signed, /* complain_on_overflow */
1065 ppc64_elf_unhandled_reloc, /* special_function */
1066 "R_PPC64_PLTGOT16", /* name */
1067 FALSE, /* partial_inplace */
1068 0, /* src_mask */
1069 0xffff, /* dst_mask */
1070 FALSE), /* pcrel_offset */
1071
1072 /* Like R_PPC64_PLTGOT16, but without overflow. */
1073 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1074 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1075 0, /* rightshift */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1077 16, /* bitsize */
1078 FALSE, /* pc_relative */
1079 0, /* bitpos */
1080 complain_overflow_dont, /* complain_on_overflow */
1081 ppc64_elf_unhandled_reloc, /* special_function */
1082 "R_PPC64_PLTGOT16_LO", /* name */
1083 FALSE, /* partial_inplace */
1084 0, /* src_mask */
1085 0xffff, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1087
1088 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1089 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1090 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1091 16, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_signed, /* complain_on_overflow */
1097 ppc64_elf_unhandled_reloc, /* special_function */
1098 "R_PPC64_PLTGOT16_HI", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xffff, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1105 1 if the contents of the low 16 bits, treated as a signed number,
1106 is negative. */
1107 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1108 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1109 16, /* rightshift */
1110 1, /* size (0 = byte, 1 = short, 2 = long) */
1111 16, /* bitsize */
1112 FALSE, /* pc_relative */
1113 0, /* bitpos */
1114 complain_overflow_signed, /* complain_on_overflow */
1115 ppc64_elf_unhandled_reloc, /* special_function */
1116 "R_PPC64_PLTGOT16_HA", /* name */
1117 FALSE, /* partial_inplace */
1118 0, /* src_mask */
1119 0xffff, /* dst_mask */
1120 FALSE), /* pcrel_offset */
1121
1122 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1123 HOWTO (R_PPC64_ADDR16_DS, /* type */
1124 0, /* rightshift */
1125 1, /* size (0 = byte, 1 = short, 2 = long) */
1126 16, /* bitsize */
1127 FALSE, /* pc_relative */
1128 0, /* bitpos */
1129 complain_overflow_signed, /* complain_on_overflow */
1130 bfd_elf_generic_reloc, /* special_function */
1131 "R_PPC64_ADDR16_DS", /* name */
1132 FALSE, /* partial_inplace */
1133 0, /* src_mask */
1134 0xfffc, /* dst_mask */
1135 FALSE), /* pcrel_offset */
1136
1137 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1138 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1139 0, /* rightshift */
1140 1, /* size (0 = byte, 1 = short, 2 = long) */
1141 16, /* bitsize */
1142 FALSE, /* pc_relative */
1143 0, /* bitpos */
1144 complain_overflow_dont,/* complain_on_overflow */
1145 bfd_elf_generic_reloc, /* special_function */
1146 "R_PPC64_ADDR16_LO_DS",/* name */
1147 FALSE, /* partial_inplace */
1148 0, /* src_mask */
1149 0xfffc, /* dst_mask */
1150 FALSE), /* pcrel_offset */
1151
1152 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1153 HOWTO (R_PPC64_GOT16_DS, /* type */
1154 0, /* rightshift */
1155 1, /* size (0 = byte, 1 = short, 2 = long) */
1156 16, /* bitsize */
1157 FALSE, /* pc_relative */
1158 0, /* bitpos */
1159 complain_overflow_signed, /* complain_on_overflow */
1160 ppc64_elf_unhandled_reloc, /* special_function */
1161 "R_PPC64_GOT16_DS", /* name */
1162 FALSE, /* partial_inplace */
1163 0, /* src_mask */
1164 0xfffc, /* dst_mask */
1165 FALSE), /* pcrel_offset */
1166
1167 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1168 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1169 0, /* rightshift */
1170 1, /* size (0 = byte, 1 = short, 2 = long) */
1171 16, /* bitsize */
1172 FALSE, /* pc_relative */
1173 0, /* bitpos */
1174 complain_overflow_dont, /* complain_on_overflow */
1175 ppc64_elf_unhandled_reloc, /* special_function */
1176 "R_PPC64_GOT16_LO_DS", /* name */
1177 FALSE, /* partial_inplace */
1178 0, /* src_mask */
1179 0xfffc, /* dst_mask */
1180 FALSE), /* pcrel_offset */
1181
1182 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1183 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1184 0, /* rightshift */
1185 1, /* size (0 = byte, 1 = short, 2 = long) */
1186 16, /* bitsize */
1187 FALSE, /* pc_relative */
1188 0, /* bitpos */
1189 complain_overflow_dont, /* complain_on_overflow */
1190 ppc64_elf_unhandled_reloc, /* special_function */
1191 "R_PPC64_PLT16_LO_DS", /* name */
1192 FALSE, /* partial_inplace */
1193 0, /* src_mask */
1194 0xfffc, /* dst_mask */
1195 FALSE), /* pcrel_offset */
1196
1197 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1198 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1199 0, /* rightshift */
1200 1, /* size (0 = byte, 1 = short, 2 = long) */
1201 16, /* bitsize */
1202 FALSE, /* pc_relative */
1203 0, /* bitpos */
1204 complain_overflow_signed, /* complain_on_overflow */
1205 ppc64_elf_sectoff_reloc, /* special_function */
1206 "R_PPC64_SECTOFF_DS", /* name */
1207 FALSE, /* partial_inplace */
1208 0, /* src_mask */
1209 0xfffc, /* dst_mask */
1210 FALSE), /* pcrel_offset */
1211
1212 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1213 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1214 0, /* rightshift */
1215 1, /* size (0 = byte, 1 = short, 2 = long) */
1216 16, /* bitsize */
1217 FALSE, /* pc_relative */
1218 0, /* bitpos */
1219 complain_overflow_dont, /* complain_on_overflow */
1220 ppc64_elf_sectoff_reloc, /* special_function */
1221 "R_PPC64_SECTOFF_LO_DS",/* name */
1222 FALSE, /* partial_inplace */
1223 0, /* src_mask */
1224 0xfffc, /* dst_mask */
1225 FALSE), /* pcrel_offset */
1226
1227 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1228 HOWTO (R_PPC64_TOC16_DS, /* type */
1229 0, /* rightshift */
1230 1, /* size (0 = byte, 1 = short, 2 = long) */
1231 16, /* bitsize */
1232 FALSE, /* pc_relative */
1233 0, /* bitpos */
1234 complain_overflow_signed, /* complain_on_overflow */
1235 ppc64_elf_toc_reloc, /* special_function */
1236 "R_PPC64_TOC16_DS", /* name */
1237 FALSE, /* partial_inplace */
1238 0, /* src_mask */
1239 0xfffc, /* dst_mask */
1240 FALSE), /* pcrel_offset */
1241
1242 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1243 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1244 0, /* rightshift */
1245 1, /* size (0 = byte, 1 = short, 2 = long) */
1246 16, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 ppc64_elf_toc_reloc, /* special_function */
1251 "R_PPC64_TOC16_LO_DS", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 0xfffc, /* dst_mask */
1255 FALSE), /* pcrel_offset */
1256
1257 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1258 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1259 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1260 0, /* rightshift */
1261 1, /* size (0 = byte, 1 = short, 2 = long) */
1262 16, /* bitsize */
1263 FALSE, /* pc_relative */
1264 0, /* bitpos */
1265 complain_overflow_signed, /* complain_on_overflow */
1266 ppc64_elf_unhandled_reloc, /* special_function */
1267 "R_PPC64_PLTGOT16_DS", /* name */
1268 FALSE, /* partial_inplace */
1269 0, /* src_mask */
1270 0xfffc, /* dst_mask */
1271 FALSE), /* pcrel_offset */
1272
1273 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1274 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1275 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1276 0, /* rightshift */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1278 16, /* bitsize */
1279 FALSE, /* pc_relative */
1280 0, /* bitpos */
1281 complain_overflow_dont, /* complain_on_overflow */
1282 ppc64_elf_unhandled_reloc, /* special_function */
1283 "R_PPC64_PLTGOT16_LO_DS",/* name */
1284 FALSE, /* partial_inplace */
1285 0, /* src_mask */
1286 0xfffc, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1288
1289 /* Marker relocs for TLS. */
1290 HOWTO (R_PPC64_TLS,
1291 0, /* rightshift */
1292 2, /* size (0 = byte, 1 = short, 2 = long) */
1293 32, /* bitsize */
1294 FALSE, /* pc_relative */
1295 0, /* bitpos */
1296 complain_overflow_dont, /* complain_on_overflow */
1297 bfd_elf_generic_reloc, /* special_function */
1298 "R_PPC64_TLS", /* name */
1299 FALSE, /* partial_inplace */
1300 0, /* src_mask */
1301 0, /* dst_mask */
1302 FALSE), /* pcrel_offset */
1303
1304 HOWTO (R_PPC64_TLSGD,
1305 0, /* rightshift */
1306 2, /* size (0 = byte, 1 = short, 2 = long) */
1307 32, /* bitsize */
1308 FALSE, /* pc_relative */
1309 0, /* bitpos */
1310 complain_overflow_dont, /* complain_on_overflow */
1311 bfd_elf_generic_reloc, /* special_function */
1312 "R_PPC64_TLSGD", /* name */
1313 FALSE, /* partial_inplace */
1314 0, /* src_mask */
1315 0, /* dst_mask */
1316 FALSE), /* pcrel_offset */
1317
1318 HOWTO (R_PPC64_TLSLD,
1319 0, /* rightshift */
1320 2, /* size (0 = byte, 1 = short, 2 = long) */
1321 32, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_dont, /* complain_on_overflow */
1325 bfd_elf_generic_reloc, /* special_function */
1326 "R_PPC64_TLSLD", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 HOWTO (R_PPC64_TOCSAVE,
1333 0, /* rightshift */
1334 2, /* size (0 = byte, 1 = short, 2 = long) */
1335 32, /* bitsize */
1336 FALSE, /* pc_relative */
1337 0, /* bitpos */
1338 complain_overflow_dont, /* complain_on_overflow */
1339 bfd_elf_generic_reloc, /* special_function */
1340 "R_PPC64_TOCSAVE", /* name */
1341 FALSE, /* partial_inplace */
1342 0, /* src_mask */
1343 0, /* dst_mask */
1344 FALSE), /* pcrel_offset */
1345
1346 /* Computes the load module index of the load module that contains the
1347 definition of its TLS sym. */
1348 HOWTO (R_PPC64_DTPMOD64,
1349 0, /* rightshift */
1350 4, /* size (0 = byte, 1 = short, 2 = long) */
1351 64, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPMOD64", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 ONES (64), /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Computes a dtv-relative displacement, the difference between the value
1363 of sym+add and the base address of the thread-local storage block that
1364 contains the definition of sym, minus 0x8000. */
1365 HOWTO (R_PPC64_DTPREL64,
1366 0, /* rightshift */
1367 4, /* size (0 = byte, 1 = short, 2 = long) */
1368 64, /* bitsize */
1369 FALSE, /* pc_relative */
1370 0, /* bitpos */
1371 complain_overflow_dont, /* complain_on_overflow */
1372 ppc64_elf_unhandled_reloc, /* special_function */
1373 "R_PPC64_DTPREL64", /* name */
1374 FALSE, /* partial_inplace */
1375 0, /* src_mask */
1376 ONES (64), /* dst_mask */
1377 FALSE), /* pcrel_offset */
1378
1379 /* A 16 bit dtprel reloc. */
1380 HOWTO (R_PPC64_DTPREL16,
1381 0, /* rightshift */
1382 1, /* size (0 = byte, 1 = short, 2 = long) */
1383 16, /* bitsize */
1384 FALSE, /* pc_relative */
1385 0, /* bitpos */
1386 complain_overflow_signed, /* complain_on_overflow */
1387 ppc64_elf_unhandled_reloc, /* special_function */
1388 "R_PPC64_DTPREL16", /* name */
1389 FALSE, /* partial_inplace */
1390 0, /* src_mask */
1391 0xffff, /* dst_mask */
1392 FALSE), /* pcrel_offset */
1393
1394 /* Like DTPREL16, but no overflow. */
1395 HOWTO (R_PPC64_DTPREL16_LO,
1396 0, /* rightshift */
1397 1, /* size (0 = byte, 1 = short, 2 = long) */
1398 16, /* bitsize */
1399 FALSE, /* pc_relative */
1400 0, /* bitpos */
1401 complain_overflow_dont, /* complain_on_overflow */
1402 ppc64_elf_unhandled_reloc, /* special_function */
1403 "R_PPC64_DTPREL16_LO", /* name */
1404 FALSE, /* partial_inplace */
1405 0, /* src_mask */
1406 0xffff, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1408
1409 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1410 HOWTO (R_PPC64_DTPREL16_HI,
1411 16, /* rightshift */
1412 1, /* size (0 = byte, 1 = short, 2 = long) */
1413 16, /* bitsize */
1414 FALSE, /* pc_relative */
1415 0, /* bitpos */
1416 complain_overflow_signed, /* complain_on_overflow */
1417 ppc64_elf_unhandled_reloc, /* special_function */
1418 "R_PPC64_DTPREL16_HI", /* name */
1419 FALSE, /* partial_inplace */
1420 0, /* src_mask */
1421 0xffff, /* dst_mask */
1422 FALSE), /* pcrel_offset */
1423
1424 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1425 HOWTO (R_PPC64_DTPREL16_HA,
1426 16, /* rightshift */
1427 1, /* size (0 = byte, 1 = short, 2 = long) */
1428 16, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
1431 complain_overflow_signed, /* complain_on_overflow */
1432 ppc64_elf_unhandled_reloc, /* special_function */
1433 "R_PPC64_DTPREL16_HA", /* name */
1434 FALSE, /* partial_inplace */
1435 0, /* src_mask */
1436 0xffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438
1439 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1440 HOWTO (R_PPC64_DTPREL16_HIGHER,
1441 32, /* rightshift */
1442 1, /* size (0 = byte, 1 = short, 2 = long) */
1443 16, /* bitsize */
1444 FALSE, /* pc_relative */
1445 0, /* bitpos */
1446 complain_overflow_dont, /* complain_on_overflow */
1447 ppc64_elf_unhandled_reloc, /* special_function */
1448 "R_PPC64_DTPREL16_HIGHER", /* name */
1449 FALSE, /* partial_inplace */
1450 0, /* src_mask */
1451 0xffff, /* dst_mask */
1452 FALSE), /* pcrel_offset */
1453
1454 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1455 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1456 32, /* rightshift */
1457 1, /* size (0 = byte, 1 = short, 2 = long) */
1458 16, /* bitsize */
1459 FALSE, /* pc_relative */
1460 0, /* bitpos */
1461 complain_overflow_dont, /* complain_on_overflow */
1462 ppc64_elf_unhandled_reloc, /* special_function */
1463 "R_PPC64_DTPREL16_HIGHERA", /* name */
1464 FALSE, /* partial_inplace */
1465 0, /* src_mask */
1466 0xffff, /* dst_mask */
1467 FALSE), /* pcrel_offset */
1468
1469 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1470 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1471 48, /* rightshift */
1472 1, /* size (0 = byte, 1 = short, 2 = long) */
1473 16, /* bitsize */
1474 FALSE, /* pc_relative */
1475 0, /* bitpos */
1476 complain_overflow_dont, /* complain_on_overflow */
1477 ppc64_elf_unhandled_reloc, /* special_function */
1478 "R_PPC64_DTPREL16_HIGHEST", /* name */
1479 FALSE, /* partial_inplace */
1480 0, /* src_mask */
1481 0xffff, /* dst_mask */
1482 FALSE), /* pcrel_offset */
1483
1484 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1485 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1486 48, /* rightshift */
1487 1, /* size (0 = byte, 1 = short, 2 = long) */
1488 16, /* bitsize */
1489 FALSE, /* pc_relative */
1490 0, /* bitpos */
1491 complain_overflow_dont, /* complain_on_overflow */
1492 ppc64_elf_unhandled_reloc, /* special_function */
1493 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1494 FALSE, /* partial_inplace */
1495 0, /* src_mask */
1496 0xffff, /* dst_mask */
1497 FALSE), /* pcrel_offset */
1498
1499 /* Like DTPREL16, but for insns with a DS field. */
1500 HOWTO (R_PPC64_DTPREL16_DS,
1501 0, /* rightshift */
1502 1, /* size (0 = byte, 1 = short, 2 = long) */
1503 16, /* bitsize */
1504 FALSE, /* pc_relative */
1505 0, /* bitpos */
1506 complain_overflow_signed, /* complain_on_overflow */
1507 ppc64_elf_unhandled_reloc, /* special_function */
1508 "R_PPC64_DTPREL16_DS", /* name */
1509 FALSE, /* partial_inplace */
1510 0, /* src_mask */
1511 0xfffc, /* dst_mask */
1512 FALSE), /* pcrel_offset */
1513
1514 /* Like DTPREL16_DS, but no overflow. */
1515 HOWTO (R_PPC64_DTPREL16_LO_DS,
1516 0, /* rightshift */
1517 1, /* size (0 = byte, 1 = short, 2 = long) */
1518 16, /* bitsize */
1519 FALSE, /* pc_relative */
1520 0, /* bitpos */
1521 complain_overflow_dont, /* complain_on_overflow */
1522 ppc64_elf_unhandled_reloc, /* special_function */
1523 "R_PPC64_DTPREL16_LO_DS", /* name */
1524 FALSE, /* partial_inplace */
1525 0, /* src_mask */
1526 0xfffc, /* dst_mask */
1527 FALSE), /* pcrel_offset */
1528
1529 /* Computes a tp-relative displacement, the difference between the value of
1530 sym+add and the value of the thread pointer (r13). */
1531 HOWTO (R_PPC64_TPREL64,
1532 0, /* rightshift */
1533 4, /* size (0 = byte, 1 = short, 2 = long) */
1534 64, /* bitsize */
1535 FALSE, /* pc_relative */
1536 0, /* bitpos */
1537 complain_overflow_dont, /* complain_on_overflow */
1538 ppc64_elf_unhandled_reloc, /* special_function */
1539 "R_PPC64_TPREL64", /* name */
1540 FALSE, /* partial_inplace */
1541 0, /* src_mask */
1542 ONES (64), /* dst_mask */
1543 FALSE), /* pcrel_offset */
1544
1545 /* A 16 bit tprel reloc. */
1546 HOWTO (R_PPC64_TPREL16,
1547 0, /* rightshift */
1548 1, /* size (0 = byte, 1 = short, 2 = long) */
1549 16, /* bitsize */
1550 FALSE, /* pc_relative */
1551 0, /* bitpos */
1552 complain_overflow_signed, /* complain_on_overflow */
1553 ppc64_elf_unhandled_reloc, /* special_function */
1554 "R_PPC64_TPREL16", /* name */
1555 FALSE, /* partial_inplace */
1556 0, /* src_mask */
1557 0xffff, /* dst_mask */
1558 FALSE), /* pcrel_offset */
1559
1560 /* Like TPREL16, but no overflow. */
1561 HOWTO (R_PPC64_TPREL16_LO,
1562 0, /* rightshift */
1563 1, /* size (0 = byte, 1 = short, 2 = long) */
1564 16, /* bitsize */
1565 FALSE, /* pc_relative */
1566 0, /* bitpos */
1567 complain_overflow_dont, /* complain_on_overflow */
1568 ppc64_elf_unhandled_reloc, /* special_function */
1569 "R_PPC64_TPREL16_LO", /* name */
1570 FALSE, /* partial_inplace */
1571 0, /* src_mask */
1572 0xffff, /* dst_mask */
1573 FALSE), /* pcrel_offset */
1574
1575 /* Like TPREL16_LO, but next higher group of 16 bits. */
1576 HOWTO (R_PPC64_TPREL16_HI,
1577 16, /* rightshift */
1578 1, /* size (0 = byte, 1 = short, 2 = long) */
1579 16, /* bitsize */
1580 FALSE, /* pc_relative */
1581 0, /* bitpos */
1582 complain_overflow_signed, /* complain_on_overflow */
1583 ppc64_elf_unhandled_reloc, /* special_function */
1584 "R_PPC64_TPREL16_HI", /* name */
1585 FALSE, /* partial_inplace */
1586 0, /* src_mask */
1587 0xffff, /* dst_mask */
1588 FALSE), /* pcrel_offset */
1589
1590 /* Like TPREL16_HI, but adjust for low 16 bits. */
1591 HOWTO (R_PPC64_TPREL16_HA,
1592 16, /* rightshift */
1593 1, /* size (0 = byte, 1 = short, 2 = long) */
1594 16, /* bitsize */
1595 FALSE, /* pc_relative */
1596 0, /* bitpos */
1597 complain_overflow_signed, /* complain_on_overflow */
1598 ppc64_elf_unhandled_reloc, /* special_function */
1599 "R_PPC64_TPREL16_HA", /* name */
1600 FALSE, /* partial_inplace */
1601 0, /* src_mask */
1602 0xffff, /* dst_mask */
1603 FALSE), /* pcrel_offset */
1604
1605 /* Like TPREL16_HI, but next higher group of 16 bits. */
1606 HOWTO (R_PPC64_TPREL16_HIGHER,
1607 32, /* rightshift */
1608 1, /* size (0 = byte, 1 = short, 2 = long) */
1609 16, /* bitsize */
1610 FALSE, /* pc_relative */
1611 0, /* bitpos */
1612 complain_overflow_dont, /* complain_on_overflow */
1613 ppc64_elf_unhandled_reloc, /* special_function */
1614 "R_PPC64_TPREL16_HIGHER", /* name */
1615 FALSE, /* partial_inplace */
1616 0, /* src_mask */
1617 0xffff, /* dst_mask */
1618 FALSE), /* pcrel_offset */
1619
1620 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1621 HOWTO (R_PPC64_TPREL16_HIGHERA,
1622 32, /* rightshift */
1623 1, /* size (0 = byte, 1 = short, 2 = long) */
1624 16, /* bitsize */
1625 FALSE, /* pc_relative */
1626 0, /* bitpos */
1627 complain_overflow_dont, /* complain_on_overflow */
1628 ppc64_elf_unhandled_reloc, /* special_function */
1629 "R_PPC64_TPREL16_HIGHERA", /* name */
1630 FALSE, /* partial_inplace */
1631 0, /* src_mask */
1632 0xffff, /* dst_mask */
1633 FALSE), /* pcrel_offset */
1634
1635 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1636 HOWTO (R_PPC64_TPREL16_HIGHEST,
1637 48, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_dont, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_TPREL16_HIGHEST", /* name */
1645 FALSE, /* partial_inplace */
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
1648 FALSE), /* pcrel_offset */
1649
1650 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1651 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1652 48, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_dont, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_TPREL16_HIGHESTA", /* name */
1660 FALSE, /* partial_inplace */
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
1663 FALSE), /* pcrel_offset */
1664
1665 /* Like TPREL16, but for insns with a DS field. */
1666 HOWTO (R_PPC64_TPREL16_DS,
1667 0, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
1670 FALSE, /* pc_relative */
1671 0, /* bitpos */
1672 complain_overflow_signed, /* complain_on_overflow */
1673 ppc64_elf_unhandled_reloc, /* special_function */
1674 "R_PPC64_TPREL16_DS", /* name */
1675 FALSE, /* partial_inplace */
1676 0, /* src_mask */
1677 0xfffc, /* dst_mask */
1678 FALSE), /* pcrel_offset */
1679
1680 /* Like TPREL16_DS, but no overflow. */
1681 HOWTO (R_PPC64_TPREL16_LO_DS,
1682 0, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
1685 FALSE, /* pc_relative */
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
1688 ppc64_elf_unhandled_reloc, /* special_function */
1689 "R_PPC64_TPREL16_LO_DS", /* name */
1690 FALSE, /* partial_inplace */
1691 0, /* src_mask */
1692 0xfffc, /* dst_mask */
1693 FALSE), /* pcrel_offset */
1694
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1697 to the first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSGD16,
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
1702 FALSE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSGD16", /* name */
1707 FALSE, /* partial_inplace */
1708 0, /* src_mask */
1709 0xffff, /* dst_mask */
1710 FALSE), /* pcrel_offset */
1711
1712 /* Like GOT_TLSGD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
1717 FALSE, /* pc_relative */
1718 0, /* bitpos */
1719 complain_overflow_dont, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSGD16_LO", /* name */
1722 FALSE, /* partial_inplace */
1723 0, /* src_mask */
1724 0xffff, /* dst_mask */
1725 FALSE), /* pcrel_offset */
1726
1727 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1729 16, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
1732 FALSE, /* pc_relative */
1733 0, /* bitpos */
1734 complain_overflow_signed, /* complain_on_overflow */
1735 ppc64_elf_unhandled_reloc, /* special_function */
1736 "R_PPC64_GOT_TLSGD16_HI", /* name */
1737 FALSE, /* partial_inplace */
1738 0, /* src_mask */
1739 0xffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1741
1742 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1744 16, /* rightshift */
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
1747 FALSE, /* pc_relative */
1748 0, /* bitpos */
1749 complain_overflow_signed, /* complain_on_overflow */
1750 ppc64_elf_unhandled_reloc, /* special_function */
1751 "R_PPC64_GOT_TLSGD16_HA", /* name */
1752 FALSE, /* partial_inplace */
1753 0, /* src_mask */
1754 0xffff, /* dst_mask */
1755 FALSE), /* pcrel_offset */
1756
1757 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1758 with values (sym+add)@dtpmod and zero, and computes the offset to the
1759 first entry relative to the TOC base (r2). */
1760 HOWTO (R_PPC64_GOT_TLSLD16,
1761 0, /* rightshift */
1762 1, /* size (0 = byte, 1 = short, 2 = long) */
1763 16, /* bitsize */
1764 FALSE, /* pc_relative */
1765 0, /* bitpos */
1766 complain_overflow_signed, /* complain_on_overflow */
1767 ppc64_elf_unhandled_reloc, /* special_function */
1768 "R_PPC64_GOT_TLSLD16", /* name */
1769 FALSE, /* partial_inplace */
1770 0, /* src_mask */
1771 0xffff, /* dst_mask */
1772 FALSE), /* pcrel_offset */
1773
1774 /* Like GOT_TLSLD16, but no overflow. */
1775 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1776 0, /* rightshift */
1777 1, /* size (0 = byte, 1 = short, 2 = long) */
1778 16, /* bitsize */
1779 FALSE, /* pc_relative */
1780 0, /* bitpos */
1781 complain_overflow_dont, /* complain_on_overflow */
1782 ppc64_elf_unhandled_reloc, /* special_function */
1783 "R_PPC64_GOT_TLSLD16_LO", /* name */
1784 FALSE, /* partial_inplace */
1785 0, /* src_mask */
1786 0xffff, /* dst_mask */
1787 FALSE), /* pcrel_offset */
1788
1789 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1790 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1791 16, /* rightshift */
1792 1, /* size (0 = byte, 1 = short, 2 = long) */
1793 16, /* bitsize */
1794 FALSE, /* pc_relative */
1795 0, /* bitpos */
1796 complain_overflow_signed, /* complain_on_overflow */
1797 ppc64_elf_unhandled_reloc, /* special_function */
1798 "R_PPC64_GOT_TLSLD16_HI", /* name */
1799 FALSE, /* partial_inplace */
1800 0, /* src_mask */
1801 0xffff, /* dst_mask */
1802 FALSE), /* pcrel_offset */
1803
1804 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1805 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1806 16, /* rightshift */
1807 1, /* size (0 = byte, 1 = short, 2 = long) */
1808 16, /* bitsize */
1809 FALSE, /* pc_relative */
1810 0, /* bitpos */
1811 complain_overflow_signed, /* complain_on_overflow */
1812 ppc64_elf_unhandled_reloc, /* special_function */
1813 "R_PPC64_GOT_TLSLD16_HA", /* name */
1814 FALSE, /* partial_inplace */
1815 0, /* src_mask */
1816 0xffff, /* dst_mask */
1817 FALSE), /* pcrel_offset */
1818
1819 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1820 the offset to the entry relative to the TOC base (r2). */
1821 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1822 0, /* rightshift */
1823 1, /* size (0 = byte, 1 = short, 2 = long) */
1824 16, /* bitsize */
1825 FALSE, /* pc_relative */
1826 0, /* bitpos */
1827 complain_overflow_signed, /* complain_on_overflow */
1828 ppc64_elf_unhandled_reloc, /* special_function */
1829 "R_PPC64_GOT_DTPREL16_DS", /* name */
1830 FALSE, /* partial_inplace */
1831 0, /* src_mask */
1832 0xfffc, /* dst_mask */
1833 FALSE), /* pcrel_offset */
1834
1835 /* Like GOT_DTPREL16_DS, but no overflow. */
1836 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1837 0, /* rightshift */
1838 1, /* size (0 = byte, 1 = short, 2 = long) */
1839 16, /* bitsize */
1840 FALSE, /* pc_relative */
1841 0, /* bitpos */
1842 complain_overflow_dont, /* complain_on_overflow */
1843 ppc64_elf_unhandled_reloc, /* special_function */
1844 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1845 FALSE, /* partial_inplace */
1846 0, /* src_mask */
1847 0xfffc, /* dst_mask */
1848 FALSE), /* pcrel_offset */
1849
1850 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1851 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1852 16, /* rightshift */
1853 1, /* size (0 = byte, 1 = short, 2 = long) */
1854 16, /* bitsize */
1855 FALSE, /* pc_relative */
1856 0, /* bitpos */
1857 complain_overflow_signed, /* complain_on_overflow */
1858 ppc64_elf_unhandled_reloc, /* special_function */
1859 "R_PPC64_GOT_DTPREL16_HI", /* name */
1860 FALSE, /* partial_inplace */
1861 0, /* src_mask */
1862 0xffff, /* dst_mask */
1863 FALSE), /* pcrel_offset */
1864
1865 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1866 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1867 16, /* rightshift */
1868 1, /* size (0 = byte, 1 = short, 2 = long) */
1869 16, /* bitsize */
1870 FALSE, /* pc_relative */
1871 0, /* bitpos */
1872 complain_overflow_signed, /* complain_on_overflow */
1873 ppc64_elf_unhandled_reloc, /* special_function */
1874 "R_PPC64_GOT_DTPREL16_HA", /* name */
1875 FALSE, /* partial_inplace */
1876 0, /* src_mask */
1877 0xffff, /* dst_mask */
1878 FALSE), /* pcrel_offset */
1879
1880 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1881 offset to the entry relative to the TOC base (r2). */
1882 HOWTO (R_PPC64_GOT_TPREL16_DS,
1883 0, /* rightshift */
1884 1, /* size (0 = byte, 1 = short, 2 = long) */
1885 16, /* bitsize */
1886 FALSE, /* pc_relative */
1887 0, /* bitpos */
1888 complain_overflow_signed, /* complain_on_overflow */
1889 ppc64_elf_unhandled_reloc, /* special_function */
1890 "R_PPC64_GOT_TPREL16_DS", /* name */
1891 FALSE, /* partial_inplace */
1892 0, /* src_mask */
1893 0xfffc, /* dst_mask */
1894 FALSE), /* pcrel_offset */
1895
1896 /* Like GOT_TPREL16_DS, but no overflow. */
1897 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1898 0, /* rightshift */
1899 1, /* size (0 = byte, 1 = short, 2 = long) */
1900 16, /* bitsize */
1901 FALSE, /* pc_relative */
1902 0, /* bitpos */
1903 complain_overflow_dont, /* complain_on_overflow */
1904 ppc64_elf_unhandled_reloc, /* special_function */
1905 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1906 FALSE, /* partial_inplace */
1907 0, /* src_mask */
1908 0xfffc, /* dst_mask */
1909 FALSE), /* pcrel_offset */
1910
1911 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1912 HOWTO (R_PPC64_GOT_TPREL16_HI,
1913 16, /* rightshift */
1914 1, /* size (0 = byte, 1 = short, 2 = long) */
1915 16, /* bitsize */
1916 FALSE, /* pc_relative */
1917 0, /* bitpos */
1918 complain_overflow_signed, /* complain_on_overflow */
1919 ppc64_elf_unhandled_reloc, /* special_function */
1920 "R_PPC64_GOT_TPREL16_HI", /* name */
1921 FALSE, /* partial_inplace */
1922 0, /* src_mask */
1923 0xffff, /* dst_mask */
1924 FALSE), /* pcrel_offset */
1925
1926 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1927 HOWTO (R_PPC64_GOT_TPREL16_HA,
1928 16, /* rightshift */
1929 1, /* size (0 = byte, 1 = short, 2 = long) */
1930 16, /* bitsize */
1931 FALSE, /* pc_relative */
1932 0, /* bitpos */
1933 complain_overflow_signed, /* complain_on_overflow */
1934 ppc64_elf_unhandled_reloc, /* special_function */
1935 "R_PPC64_GOT_TPREL16_HA", /* name */
1936 FALSE, /* partial_inplace */
1937 0, /* src_mask */
1938 0xffff, /* dst_mask */
1939 FALSE), /* pcrel_offset */
1940
1941 HOWTO (R_PPC64_JMP_IREL, /* type */
1942 0, /* rightshift */
1943 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1944 0, /* bitsize */
1945 FALSE, /* pc_relative */
1946 0, /* bitpos */
1947 complain_overflow_dont, /* complain_on_overflow */
1948 ppc64_elf_unhandled_reloc, /* special_function */
1949 "R_PPC64_JMP_IREL", /* name */
1950 FALSE, /* partial_inplace */
1951 0, /* src_mask */
1952 0, /* dst_mask */
1953 FALSE), /* pcrel_offset */
1954
1955 HOWTO (R_PPC64_IRELATIVE, /* type */
1956 0, /* rightshift */
1957 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1958 64, /* bitsize */
1959 FALSE, /* pc_relative */
1960 0, /* bitpos */
1961 complain_overflow_dont, /* complain_on_overflow */
1962 bfd_elf_generic_reloc, /* special_function */
1963 "R_PPC64_IRELATIVE", /* name */
1964 FALSE, /* partial_inplace */
1965 0, /* src_mask */
1966 ONES (64), /* dst_mask */
1967 FALSE), /* pcrel_offset */
1968
1969 /* A 16 bit relative relocation. */
1970 HOWTO (R_PPC64_REL16, /* type */
1971 0, /* rightshift */
1972 1, /* size (0 = byte, 1 = short, 2 = long) */
1973 16, /* bitsize */
1974 TRUE, /* pc_relative */
1975 0, /* bitpos */
1976 complain_overflow_signed, /* complain_on_overflow */
1977 bfd_elf_generic_reloc, /* special_function */
1978 "R_PPC64_REL16", /* name */
1979 FALSE, /* partial_inplace */
1980 0, /* src_mask */
1981 0xffff, /* dst_mask */
1982 TRUE), /* pcrel_offset */
1983
1984 /* A 16 bit relative relocation without overflow. */
1985 HOWTO (R_PPC64_REL16_LO, /* type */
1986 0, /* rightshift */
1987 1, /* size (0 = byte, 1 = short, 2 = long) */
1988 16, /* bitsize */
1989 TRUE, /* pc_relative */
1990 0, /* bitpos */
1991 complain_overflow_dont,/* complain_on_overflow */
1992 bfd_elf_generic_reloc, /* special_function */
1993 "R_PPC64_REL16_LO", /* name */
1994 FALSE, /* partial_inplace */
1995 0, /* src_mask */
1996 0xffff, /* dst_mask */
1997 TRUE), /* pcrel_offset */
1998
1999 /* The high order 16 bits of a relative address. */
2000 HOWTO (R_PPC64_REL16_HI, /* type */
2001 16, /* rightshift */
2002 1, /* size (0 = byte, 1 = short, 2 = long) */
2003 16, /* bitsize */
2004 TRUE, /* pc_relative */
2005 0, /* bitpos */
2006 complain_overflow_signed, /* complain_on_overflow */
2007 bfd_elf_generic_reloc, /* special_function */
2008 "R_PPC64_REL16_HI", /* name */
2009 FALSE, /* partial_inplace */
2010 0, /* src_mask */
2011 0xffff, /* dst_mask */
2012 TRUE), /* pcrel_offset */
2013
2014 /* The high order 16 bits of a relative address, plus 1 if the contents of
2015 the low 16 bits, treated as a signed number, is negative. */
2016 HOWTO (R_PPC64_REL16_HA, /* type */
2017 16, /* rightshift */
2018 1, /* size (0 = byte, 1 = short, 2 = long) */
2019 16, /* bitsize */
2020 TRUE, /* pc_relative */
2021 0, /* bitpos */
2022 complain_overflow_signed, /* complain_on_overflow */
2023 ppc64_elf_ha_reloc, /* special_function */
2024 "R_PPC64_REL16_HA", /* name */
2025 FALSE, /* partial_inplace */
2026 0, /* src_mask */
2027 0xffff, /* dst_mask */
2028 TRUE), /* pcrel_offset */
2029
2030 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2031 HOWTO (R_PPC64_REL16DX_HA, /* type */
2032 16, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 16, /* bitsize */
2035 TRUE, /* pc_relative */
2036 0, /* bitpos */
2037 complain_overflow_signed, /* complain_on_overflow */
2038 ppc64_elf_ha_reloc, /* special_function */
2039 "R_PPC64_REL16DX_HA", /* name */
2040 FALSE, /* partial_inplace */
2041 0, /* src_mask */
2042 0x1fffc1, /* dst_mask */
2043 TRUE), /* pcrel_offset */
2044
2045 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2046 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2047 16, /* rightshift */
2048 1, /* size (0 = byte, 1 = short, 2 = long) */
2049 16, /* bitsize */
2050 FALSE, /* pc_relative */
2051 0, /* bitpos */
2052 complain_overflow_dont, /* complain_on_overflow */
2053 bfd_elf_generic_reloc, /* special_function */
2054 "R_PPC64_ADDR16_HIGH", /* name */
2055 FALSE, /* partial_inplace */
2056 0, /* src_mask */
2057 0xffff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059
2060 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2061 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2062 16, /* rightshift */
2063 1, /* size (0 = byte, 1 = short, 2 = long) */
2064 16, /* bitsize */
2065 FALSE, /* pc_relative */
2066 0, /* bitpos */
2067 complain_overflow_dont, /* complain_on_overflow */
2068 ppc64_elf_ha_reloc, /* special_function */
2069 "R_PPC64_ADDR16_HIGHA", /* name */
2070 FALSE, /* partial_inplace */
2071 0, /* src_mask */
2072 0xffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2074
2075 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2076 HOWTO (R_PPC64_DTPREL16_HIGH,
2077 16, /* rightshift */
2078 1, /* size (0 = byte, 1 = short, 2 = long) */
2079 16, /* bitsize */
2080 FALSE, /* pc_relative */
2081 0, /* bitpos */
2082 complain_overflow_dont, /* complain_on_overflow */
2083 ppc64_elf_unhandled_reloc, /* special_function */
2084 "R_PPC64_DTPREL16_HIGH", /* name */
2085 FALSE, /* partial_inplace */
2086 0, /* src_mask */
2087 0xffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
2089
2090 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2091 HOWTO (R_PPC64_DTPREL16_HIGHA,
2092 16, /* rightshift */
2093 1, /* size (0 = byte, 1 = short, 2 = long) */
2094 16, /* bitsize */
2095 FALSE, /* pc_relative */
2096 0, /* bitpos */
2097 complain_overflow_dont, /* complain_on_overflow */
2098 ppc64_elf_unhandled_reloc, /* special_function */
2099 "R_PPC64_DTPREL16_HIGHA", /* name */
2100 FALSE, /* partial_inplace */
2101 0, /* src_mask */
2102 0xffff, /* dst_mask */
2103 FALSE), /* pcrel_offset */
2104
2105 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2106 HOWTO (R_PPC64_TPREL16_HIGH,
2107 16, /* rightshift */
2108 1, /* size (0 = byte, 1 = short, 2 = long) */
2109 16, /* bitsize */
2110 FALSE, /* pc_relative */
2111 0, /* bitpos */
2112 complain_overflow_dont, /* complain_on_overflow */
2113 ppc64_elf_unhandled_reloc, /* special_function */
2114 "R_PPC64_TPREL16_HIGH", /* name */
2115 FALSE, /* partial_inplace */
2116 0, /* src_mask */
2117 0xffff, /* dst_mask */
2118 FALSE), /* pcrel_offset */
2119
2120 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2121 HOWTO (R_PPC64_TPREL16_HIGHA,
2122 16, /* rightshift */
2123 1, /* size (0 = byte, 1 = short, 2 = long) */
2124 16, /* bitsize */
2125 FALSE, /* pc_relative */
2126 0, /* bitpos */
2127 complain_overflow_dont, /* complain_on_overflow */
2128 ppc64_elf_unhandled_reloc, /* special_function */
2129 "R_PPC64_TPREL16_HIGHA", /* name */
2130 FALSE, /* partial_inplace */
2131 0, /* src_mask */
2132 0xffff, /* dst_mask */
2133 FALSE), /* pcrel_offset */
2134
2135 /* Marker reloc on ELFv2 large-model function entry. */
2136 HOWTO (R_PPC64_ENTRY,
2137 0, /* rightshift */
2138 2, /* size (0 = byte, 1 = short, 2 = long) */
2139 32, /* bitsize */
2140 FALSE, /* pc_relative */
2141 0, /* bitpos */
2142 complain_overflow_dont, /* complain_on_overflow */
2143 bfd_elf_generic_reloc, /* special_function */
2144 "R_PPC64_ENTRY", /* name */
2145 FALSE, /* partial_inplace */
2146 0, /* src_mask */
2147 0, /* dst_mask */
2148 FALSE), /* pcrel_offset */
2149
2150 /* Like ADDR64, but use local entry point of function. */
2151 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2152 0, /* rightshift */
2153 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2154 64, /* bitsize */
2155 FALSE, /* pc_relative */
2156 0, /* bitpos */
2157 complain_overflow_dont, /* complain_on_overflow */
2158 bfd_elf_generic_reloc, /* special_function */
2159 "R_PPC64_ADDR64_LOCAL", /* name */
2160 FALSE, /* partial_inplace */
2161 0, /* src_mask */
2162 ONES (64), /* dst_mask */
2163 FALSE), /* pcrel_offset */
2164
2165 /* GNU extension to record C++ vtable hierarchy. */
2166 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2167 0, /* rightshift */
2168 0, /* size (0 = byte, 1 = short, 2 = long) */
2169 0, /* bitsize */
2170 FALSE, /* pc_relative */
2171 0, /* bitpos */
2172 complain_overflow_dont, /* complain_on_overflow */
2173 NULL, /* special_function */
2174 "R_PPC64_GNU_VTINHERIT", /* name */
2175 FALSE, /* partial_inplace */
2176 0, /* src_mask */
2177 0, /* dst_mask */
2178 FALSE), /* pcrel_offset */
2179
2180 /* GNU extension to record C++ vtable member usage. */
2181 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2182 0, /* rightshift */
2183 0, /* size (0 = byte, 1 = short, 2 = long) */
2184 0, /* bitsize */
2185 FALSE, /* pc_relative */
2186 0, /* bitpos */
2187 complain_overflow_dont, /* complain_on_overflow */
2188 NULL, /* special_function */
2189 "R_PPC64_GNU_VTENTRY", /* name */
2190 FALSE, /* partial_inplace */
2191 0, /* src_mask */
2192 0, /* dst_mask */
2193 FALSE), /* pcrel_offset */
2194};
2195
2196\f
2197/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2198 be done. */
2199
2200static void
2201ppc_howto_init (void)
2202{
2203 unsigned int i, type;
2204
2205 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2206 {
2207 type = ppc64_elf_howto_raw[i].type;
2208 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2209 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2210 }
2211}
2212
2213static reloc_howto_type *
2214ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2215 bfd_reloc_code_real_type code)
2216{
2217 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2218
2219 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2220 /* Initialize howto table if needed. */
2221 ppc_howto_init ();
2222
2223 switch (code)
2224 {
2225 default:
2226 return NULL;
2227
2228 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2229 break;
2230 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2231 break;
2232 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2233 break;
2234 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2235 break;
2236 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2237 break;
2238 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2239 break;
2240 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2241 break;
2242 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2243 break;
2244 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2245 break;
2246 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2247 break;
2248 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2249 break;
2250 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2251 break;
2252 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2253 break;
2254 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2255 break;
2256 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2257 break;
2258 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2259 break;
2260 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2261 break;
2262 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2263 break;
2264 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2265 break;
2266 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2267 break;
2268 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2269 break;
2270 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2271 break;
2272 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2273 break;
2274 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2275 break;
2276 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2277 break;
2278 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2279 break;
2280 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2281 break;
2282 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2283 break;
2284 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2285 break;
2286 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2287 break;
2288 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2289 break;
2290 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2291 break;
2292 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2293 break;
2294 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2295 break;
2296 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2297 break;
2298 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2299 break;
2300 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2301 break;
2302 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2303 break;
2304 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2305 break;
2306 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2307 break;
2308 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2309 break;
2310 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2311 break;
2312 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2313 break;
2314 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2315 break;
2316 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2317 break;
2318 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2319 break;
2320 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2321 break;
2322 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2323 break;
2324 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2325 break;
2326 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2327 break;
2328 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2329 break;
2330 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2331 break;
2332 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2333 break;
2334 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2335 break;
2336 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2337 break;
2338 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2339 break;
2340 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2341 break;
2342 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2343 break;
2344 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2345 break;
2346 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2347 break;
2348 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2349 break;
2350 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2351 break;
2352 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2353 break;
2354 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2355 break;
2356 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2357 break;
2358 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2359 break;
2360 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2361 break;
2362 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2363 break;
2364 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2365 break;
2366 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2367 break;
2368 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2369 break;
2370 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2371 break;
2372 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2373 break;
2374 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2375 break;
2376 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2377 break;
2378 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2379 break;
2380 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2381 break;
2382 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2383 break;
2384 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2385 break;
2386 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2387 break;
2388 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2389 break;
2390 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2391 break;
2392 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2393 break;
2394 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2395 break;
2396 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2397 break;
2398 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2399 break;
2400 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2401 break;
2402 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2403 break;
2404 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2409 break;
2410 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2411 break;
2412 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2413 break;
2414 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2415 break;
2416 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2417 break;
2418 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2419 break;
2420 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2421 break;
2422 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2423 break;
2424 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2425 break;
2426 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2427 break;
2428 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2429 break;
2430 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2431 break;
2432 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2433 break;
2434 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2435 break;
2436 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2437 break;
2438 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2439 break;
2440 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2441 break;
2442 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2443 break;
2444 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2445 break;
2446 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2447 break;
2448 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2449 break;
2450 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2451 break;
2452 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2453 break;
2454 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2455 break;
2456 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2457 break;
2458 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2459 break;
2460 }
2461
2462 return ppc64_elf_howto_table[r];
2463};
2464
2465static reloc_howto_type *
2466ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2467 const char *r_name)
2468{
2469 unsigned int i;
2470
2471 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2472 if (ppc64_elf_howto_raw[i].name != NULL
2473 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2474 return &ppc64_elf_howto_raw[i];
2475
2476 return NULL;
2477}
2478
2479/* Set the howto pointer for a PowerPC ELF reloc. */
2480
2481static void
2482ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2483 Elf_Internal_Rela *dst)
2484{
2485 unsigned int type;
2486
2487 /* Initialize howto table if needed. */
2488 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2489 ppc_howto_init ();
2490
2491 type = ELF64_R_TYPE (dst->r_info);
2492 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2493 {
2494 /* xgettext:c-format */
2495 _bfd_error_handler (_("%B: invalid relocation type %d"),
2496 abfd, (int) type);
2497 type = R_PPC64_NONE;
2498 }
2499 cache_ptr->howto = ppc64_elf_howto_table[type];
2500}
2501
2502/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2503
2504static bfd_reloc_status_type
2505ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2506 void *data, asection *input_section,
2507 bfd *output_bfd, char **error_message)
2508{
2509 enum elf_ppc64_reloc_type r_type;
2510 long insn;
2511 bfd_size_type octets;
2512 bfd_vma value;
2513
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2519 input_section, output_bfd, error_message);
2520
2521 /* Adjust the addend for sign extension of the low 16 bits.
2522 We won't actually be using the low 16 bits, so trashing them
2523 doesn't matter. */
2524 reloc_entry->addend += 0x8000;
2525 r_type = reloc_entry->howto->type;
2526 if (r_type != R_PPC64_REL16DX_HA)
2527 return bfd_reloc_continue;
2528
2529 value = 0;
2530 if (!bfd_is_com_section (symbol->section))
2531 value = symbol->value;
2532 value += (reloc_entry->addend
2533 + symbol->section->output_offset
2534 + symbol->section->output_section->vma);
2535 value -= (reloc_entry->address
2536 + input_section->output_offset
2537 + input_section->output_section->vma);
2538 value = (bfd_signed_vma) value >> 16;
2539
2540 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2541 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2542 insn &= ~0x1fffc1;
2543 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2544 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2545 if (value + 0x8000 > 0xffff)
2546 return bfd_reloc_overflow;
2547 return bfd_reloc_ok;
2548}
2549
2550static bfd_reloc_status_type
2551ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2552 void *data, asection *input_section,
2553 bfd *output_bfd, char **error_message)
2554{
2555 if (output_bfd != NULL)
2556 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2557 input_section, output_bfd, error_message);
2558
2559 if (strcmp (symbol->section->name, ".opd") == 0
2560 && (symbol->section->owner->flags & DYNAMIC) == 0)
2561 {
2562 bfd_vma dest = opd_entry_value (symbol->section,
2563 symbol->value + reloc_entry->addend,
2564 NULL, NULL, FALSE);
2565 if (dest != (bfd_vma) -1)
2566 reloc_entry->addend = dest - (symbol->value
2567 + symbol->section->output_section->vma
2568 + symbol->section->output_offset);
2569 }
2570 else
2571 {
2572 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2573
2574 if (symbol->section->owner != abfd
2575 && symbol->section->owner != NULL
2576 && abiversion (symbol->section->owner) >= 2)
2577 {
2578 unsigned int i;
2579
2580 for (i = 0; i < symbol->section->owner->symcount; ++i)
2581 {
2582 asymbol *symdef = symbol->section->owner->outsymbols[i];
2583
2584 if (strcmp (symdef->name, symbol->name) == 0)
2585 {
2586 elfsym = (elf_symbol_type *) symdef;
2587 break;
2588 }
2589 }
2590 }
2591 reloc_entry->addend
2592 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2593 }
2594 return bfd_reloc_continue;
2595}
2596
2597static bfd_reloc_status_type
2598ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2599 void *data, asection *input_section,
2600 bfd *output_bfd, char **error_message)
2601{
2602 long insn;
2603 enum elf_ppc64_reloc_type r_type;
2604 bfd_size_type octets;
2605 /* Assume 'at' branch hints. */
2606 bfd_boolean is_isa_v2 = TRUE;
2607
2608 /* If this is a relocatable link (output_bfd test tells us), just
2609 call the generic function. Any adjustment will be done at final
2610 link time. */
2611 if (output_bfd != NULL)
2612 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2613 input_section, output_bfd, error_message);
2614
2615 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2616 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2617 insn &= ~(0x01 << 21);
2618 r_type = reloc_entry->howto->type;
2619 if (r_type == R_PPC64_ADDR14_BRTAKEN
2620 || r_type == R_PPC64_REL14_BRTAKEN)
2621 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2622
2623 if (is_isa_v2)
2624 {
2625 /* Set 'a' bit. This is 0b00010 in BO field for branch
2626 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2627 for branch on CTR insns (BO == 1a00t or 1a01t). */
2628 if ((insn & (0x14 << 21)) == (0x04 << 21))
2629 insn |= 0x02 << 21;
2630 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2631 insn |= 0x08 << 21;
2632 else
2633 goto out;
2634 }
2635 else
2636 {
2637 bfd_vma target = 0;
2638 bfd_vma from;
2639
2640 if (!bfd_is_com_section (symbol->section))
2641 target = symbol->value;
2642 target += symbol->section->output_section->vma;
2643 target += symbol->section->output_offset;
2644 target += reloc_entry->addend;
2645
2646 from = (reloc_entry->address
2647 + input_section->output_offset
2648 + input_section->output_section->vma);
2649
2650 /* Invert 'y' bit if not the default. */
2651 if ((bfd_signed_vma) (target - from) < 0)
2652 insn ^= 0x01 << 21;
2653 }
2654 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2655 out:
2656 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2657 input_section, output_bfd, error_message);
2658}
2659
2660static bfd_reloc_status_type
2661ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2662 void *data, asection *input_section,
2663 bfd *output_bfd, char **error_message)
2664{
2665 /* If this is a relocatable link (output_bfd test tells us), just
2666 call the generic function. Any adjustment will be done at final
2667 link time. */
2668 if (output_bfd != NULL)
2669 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2670 input_section, output_bfd, error_message);
2671
2672 /* Subtract the symbol section base address. */
2673 reloc_entry->addend -= symbol->section->output_section->vma;
2674 return bfd_reloc_continue;
2675}
2676
2677static bfd_reloc_status_type
2678ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2679 void *data, asection *input_section,
2680 bfd *output_bfd, char **error_message)
2681{
2682 /* If this is a relocatable link (output_bfd test tells us), just
2683 call the generic function. Any adjustment will be done at final
2684 link time. */
2685 if (output_bfd != NULL)
2686 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2687 input_section, output_bfd, error_message);
2688
2689 /* Subtract the symbol section base address. */
2690 reloc_entry->addend -= symbol->section->output_section->vma;
2691
2692 /* Adjust the addend for sign extension of the low 16 bits. */
2693 reloc_entry->addend += 0x8000;
2694 return bfd_reloc_continue;
2695}
2696
2697static bfd_reloc_status_type
2698ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2699 void *data, asection *input_section,
2700 bfd *output_bfd, char **error_message)
2701{
2702 bfd_vma TOCstart;
2703
2704 /* If this is a relocatable link (output_bfd test tells us), just
2705 call the generic function. Any adjustment will be done at final
2706 link time. */
2707 if (output_bfd != NULL)
2708 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2709 input_section, output_bfd, error_message);
2710
2711 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2712 if (TOCstart == 0)
2713 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2714
2715 /* Subtract the TOC base address. */
2716 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2717 return bfd_reloc_continue;
2718}
2719
2720static bfd_reloc_status_type
2721ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2722 void *data, asection *input_section,
2723 bfd *output_bfd, char **error_message)
2724{
2725 bfd_vma TOCstart;
2726
2727 /* If this is a relocatable link (output_bfd test tells us), just
2728 call the generic function. Any adjustment will be done at final
2729 link time. */
2730 if (output_bfd != NULL)
2731 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2732 input_section, output_bfd, error_message);
2733
2734 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2735 if (TOCstart == 0)
2736 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2737
2738 /* Subtract the TOC base address. */
2739 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2740
2741 /* Adjust the addend for sign extension of the low 16 bits. */
2742 reloc_entry->addend += 0x8000;
2743 return bfd_reloc_continue;
2744}
2745
2746static bfd_reloc_status_type
2747ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2748 void *data, asection *input_section,
2749 bfd *output_bfd, char **error_message)
2750{
2751 bfd_vma TOCstart;
2752 bfd_size_type octets;
2753
2754 /* If this is a relocatable link (output_bfd test tells us), just
2755 call the generic function. Any adjustment will be done at final
2756 link time. */
2757 if (output_bfd != NULL)
2758 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2759 input_section, output_bfd, error_message);
2760
2761 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2762 if (TOCstart == 0)
2763 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2764
2765 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2766 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2767 return bfd_reloc_ok;
2768}
2769
2770static bfd_reloc_status_type
2771ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2772 void *data, asection *input_section,
2773 bfd *output_bfd, char **error_message)
2774{
2775 /* If this is a relocatable link (output_bfd test tells us), just
2776 call the generic function. Any adjustment will be done at final
2777 link time. */
2778 if (output_bfd != NULL)
2779 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2780 input_section, output_bfd, error_message);
2781
2782 if (error_message != NULL)
2783 {
2784 static char buf[60];
2785 sprintf (buf, "generic linker can't handle %s",
2786 reloc_entry->howto->name);
2787 *error_message = buf;
2788 }
2789 return bfd_reloc_dangerous;
2790}
2791
2792/* Track GOT entries needed for a given symbol. We might need more
2793 than one got entry per symbol. */
2794struct got_entry
2795{
2796 struct got_entry *next;
2797
2798 /* The symbol addend that we'll be placing in the GOT. */
2799 bfd_vma addend;
2800
2801 /* Unlike other ELF targets, we use separate GOT entries for the same
2802 symbol referenced from different input files. This is to support
2803 automatic multiple TOC/GOT sections, where the TOC base can vary
2804 from one input file to another. After partitioning into TOC groups
2805 we merge entries within the group.
2806
2807 Point to the BFD owning this GOT entry. */
2808 bfd *owner;
2809
2810 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2811 TLS_TPREL or TLS_DTPREL for tls entries. */
2812 unsigned char tls_type;
2813
2814 /* Non-zero if got.ent points to real entry. */
2815 unsigned char is_indirect;
2816
2817 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2818 union
2819 {
2820 bfd_signed_vma refcount;
2821 bfd_vma offset;
2822 struct got_entry *ent;
2823 } got;
2824};
2825
2826/* The same for PLT. */
2827struct plt_entry
2828{
2829 struct plt_entry *next;
2830
2831 bfd_vma addend;
2832
2833 union
2834 {
2835 bfd_signed_vma refcount;
2836 bfd_vma offset;
2837 } plt;
2838};
2839
2840struct ppc64_elf_obj_tdata
2841{
2842 struct elf_obj_tdata elf;
2843
2844 /* Shortcuts to dynamic linker sections. */
2845 asection *got;
2846 asection *relgot;
2847
2848 /* Used during garbage collection. We attach global symbols defined
2849 on removed .opd entries to this section so that the sym is removed. */
2850 asection *deleted_section;
2851
2852 /* TLS local dynamic got entry handling. Support for multiple GOT
2853 sections means we potentially need one of these for each input bfd. */
2854 struct got_entry tlsld_got;
2855
2856 union {
2857 /* A copy of relocs before they are modified for --emit-relocs. */
2858 Elf_Internal_Rela *relocs;
2859
2860 /* Section contents. */
2861 bfd_byte *contents;
2862 } opd;
2863
2864 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2865 the reloc to be in the range -32768 to 32767. */
2866 unsigned int has_small_toc_reloc : 1;
2867
2868 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2869 instruction not one we handle. */
2870 unsigned int unexpected_toc_insn : 1;
2871};
2872
2873#define ppc64_elf_tdata(bfd) \
2874 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2875
2876#define ppc64_tlsld_got(bfd) \
2877 (&ppc64_elf_tdata (bfd)->tlsld_got)
2878
2879#define is_ppc64_elf(bfd) \
2880 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2881 && elf_object_id (bfd) == PPC64_ELF_DATA)
2882
2883/* Override the generic function because we store some extras. */
2884
2885static bfd_boolean
2886ppc64_elf_mkobject (bfd *abfd)
2887{
2888 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2889 PPC64_ELF_DATA);
2890}
2891
2892/* Fix bad default arch selected for a 64 bit input bfd when the
2893 default is 32 bit. Also select arch based on apuinfo. */
2894
2895static bfd_boolean
2896ppc64_elf_object_p (bfd *abfd)
2897{
2898 if (!abfd->arch_info->the_default)
2899 return TRUE;
2900
2901 if (abfd->arch_info->bits_per_word == 32)
2902 {
2903 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2904
2905 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2906 {
2907 /* Relies on arch after 32 bit default being 64 bit default. */
2908 abfd->arch_info = abfd->arch_info->next;
2909 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2910 }
2911 }
2912 return _bfd_elf_ppc_set_arch (abfd);
2913}
2914
2915/* Support for core dump NOTE sections. */
2916
2917static bfd_boolean
2918ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2919{
2920 size_t offset, size;
2921
2922 if (note->descsz != 504)
2923 return FALSE;
2924
2925 /* pr_cursig */
2926 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2927
2928 /* pr_pid */
2929 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2930
2931 /* pr_reg */
2932 offset = 112;
2933 size = 384;
2934
2935 /* Make a ".reg/999" section. */
2936 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2937 size, note->descpos + offset);
2938}
2939
2940static bfd_boolean
2941ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2942{
2943 if (note->descsz != 136)
2944 return FALSE;
2945
2946 elf_tdata (abfd)->core->pid
2947 = bfd_get_32 (abfd, note->descdata + 24);
2948 elf_tdata (abfd)->core->program
2949 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2950 elf_tdata (abfd)->core->command
2951 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2952
2953 return TRUE;
2954}
2955
2956static char *
2957ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2958 ...)
2959{
2960 switch (note_type)
2961 {
2962 default:
2963 return NULL;
2964
2965 case NT_PRPSINFO:
2966 {
2967 char data[136];
2968 va_list ap;
2969
2970 va_start (ap, note_type);
2971 memset (data, 0, sizeof (data));
2972 strncpy (data + 40, va_arg (ap, const char *), 16);
2973 strncpy (data + 56, va_arg (ap, const char *), 80);
2974 va_end (ap);
2975 return elfcore_write_note (abfd, buf, bufsiz,
2976 "CORE", note_type, data, sizeof (data));
2977 }
2978
2979 case NT_PRSTATUS:
2980 {
2981 char data[504];
2982 va_list ap;
2983 long pid;
2984 int cursig;
2985 const void *greg;
2986
2987 va_start (ap, note_type);
2988 memset (data, 0, 112);
2989 pid = va_arg (ap, long);
2990 bfd_put_32 (abfd, pid, data + 32);
2991 cursig = va_arg (ap, int);
2992 bfd_put_16 (abfd, cursig, data + 12);
2993 greg = va_arg (ap, const void *);
2994 memcpy (data + 112, greg, 384);
2995 memset (data + 496, 0, 8);
2996 va_end (ap);
2997 return elfcore_write_note (abfd, buf, bufsiz,
2998 "CORE", note_type, data, sizeof (data));
2999 }
3000 }
3001}
3002
3003/* Add extra PPC sections. */
3004
3005static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3006{
3007 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3008 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3009 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3010 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3011 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3012 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3013 { NULL, 0, 0, 0, 0 }
3014};
3015
3016enum _ppc64_sec_type {
3017 sec_normal = 0,
3018 sec_opd = 1,
3019 sec_toc = 2
3020};
3021
3022struct _ppc64_elf_section_data
3023{
3024 struct bfd_elf_section_data elf;
3025
3026 union
3027 {
3028 /* An array with one entry for each opd function descriptor,
3029 and some spares since opd entries may be either 16 or 24 bytes. */
3030#define OPD_NDX(OFF) ((OFF) >> 4)
3031 struct _opd_sec_data
3032 {
3033 /* Points to the function code section for local opd entries. */
3034 asection **func_sec;
3035
3036 /* After editing .opd, adjust references to opd local syms. */
3037 long *adjust;
3038 } opd;
3039
3040 /* An array for toc sections, indexed by offset/8. */
3041 struct _toc_sec_data
3042 {
3043 /* Specifies the relocation symbol index used at a given toc offset. */
3044 unsigned *symndx;
3045
3046 /* And the relocation addend. */
3047 bfd_vma *add;
3048 } toc;
3049 } u;
3050
3051 enum _ppc64_sec_type sec_type:2;
3052
3053 /* Flag set when small branches are detected. Used to
3054 select suitable defaults for the stub group size. */
3055 unsigned int has_14bit_branch:1;
3056};
3057
3058#define ppc64_elf_section_data(sec) \
3059 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3060
3061static bfd_boolean
3062ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3063{
3064 if (!sec->used_by_bfd)
3065 {
3066 struct _ppc64_elf_section_data *sdata;
3067 bfd_size_type amt = sizeof (*sdata);
3068
3069 sdata = bfd_zalloc (abfd, amt);
3070 if (sdata == NULL)
3071 return FALSE;
3072 sec->used_by_bfd = sdata;
3073 }
3074
3075 return _bfd_elf_new_section_hook (abfd, sec);
3076}
3077
3078static struct _opd_sec_data *
3079get_opd_info (asection * sec)
3080{
3081 if (sec != NULL
3082 && ppc64_elf_section_data (sec) != NULL
3083 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3084 return &ppc64_elf_section_data (sec)->u.opd;
3085 return NULL;
3086}
3087\f
3088/* Parameters for the qsort hook. */
3089static bfd_boolean synthetic_relocatable;
3090static asection *synthetic_opd;
3091
3092/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3093
3094static int
3095compare_symbols (const void *ap, const void *bp)
3096{
3097 const asymbol *a = * (const asymbol **) ap;
3098 const asymbol *b = * (const asymbol **) bp;
3099
3100 /* Section symbols first. */
3101 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3102 return -1;
3103 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3104 return 1;
3105
3106 /* then .opd symbols. */
3107 if (synthetic_opd != NULL)
3108 {
3109 if (strcmp (a->section->name, ".opd") == 0
3110 && strcmp (b->section->name, ".opd") != 0)
3111 return -1;
3112 if (strcmp (a->section->name, ".opd") != 0
3113 && strcmp (b->section->name, ".opd") == 0)
3114 return 1;
3115 }
3116
3117 /* then other code symbols. */
3118 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3119 == (SEC_CODE | SEC_ALLOC)
3120 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3121 != (SEC_CODE | SEC_ALLOC))
3122 return -1;
3123
3124 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3125 != (SEC_CODE | SEC_ALLOC)
3126 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3127 == (SEC_CODE | SEC_ALLOC))
3128 return 1;
3129
3130 if (synthetic_relocatable)
3131 {
3132 if (a->section->id < b->section->id)
3133 return -1;
3134
3135 if (a->section->id > b->section->id)
3136 return 1;
3137 }
3138
3139 if (a->value + a->section->vma < b->value + b->section->vma)
3140 return -1;
3141
3142 if (a->value + a->section->vma > b->value + b->section->vma)
3143 return 1;
3144
3145 /* For syms with the same value, prefer strong dynamic global function
3146 syms over other syms. */
3147 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3148 return -1;
3149
3150 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3151 return 1;
3152
3153 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3154 return -1;
3155
3156 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3157 return 1;
3158
3159 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3160 return -1;
3161
3162 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3163 return 1;
3164
3165 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3166 return -1;
3167
3168 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3169 return 1;
3170
3171 return 0;
3172}
3173
3174/* Search SYMS for a symbol of the given VALUE. */
3175
3176static asymbol *
3177sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3178{
3179 long mid;
3180
3181 if (id == (unsigned) -1)
3182 {
3183 while (lo < hi)
3184 {
3185 mid = (lo + hi) >> 1;
3186 if (syms[mid]->value + syms[mid]->section->vma < value)
3187 lo = mid + 1;
3188 else if (syms[mid]->value + syms[mid]->section->vma > value)
3189 hi = mid;
3190 else
3191 return syms[mid];
3192 }
3193 }
3194 else
3195 {
3196 while (lo < hi)
3197 {
3198 mid = (lo + hi) >> 1;
3199 if (syms[mid]->section->id < id)
3200 lo = mid + 1;
3201 else if (syms[mid]->section->id > id)
3202 hi = mid;
3203 else if (syms[mid]->value < value)
3204 lo = mid + 1;
3205 else if (syms[mid]->value > value)
3206 hi = mid;
3207 else
3208 return syms[mid];
3209 }
3210 }
3211 return NULL;
3212}
3213
3214static bfd_boolean
3215section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3216{
3217 bfd_vma vma = *(bfd_vma *) ptr;
3218 return ((section->flags & SEC_ALLOC) != 0
3219 && section->vma <= vma
3220 && vma < section->vma + section->size);
3221}
3222
3223/* Create synthetic symbols, effectively restoring "dot-symbol" function
3224 entry syms. Also generate @plt symbols for the glink branch table.
3225 Returns count of synthetic symbols in RET or -1 on error. */
3226
3227static long
3228ppc64_elf_get_synthetic_symtab (bfd *abfd,
3229 long static_count, asymbol **static_syms,
3230 long dyn_count, asymbol **dyn_syms,
3231 asymbol **ret)
3232{
3233 asymbol *s;
3234 long i;
3235 long count;
3236 char *names;
3237 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3238 asection *opd = NULL;
3239 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3240 asymbol **syms;
3241 int abi = abiversion (abfd);
3242
3243 *ret = NULL;
3244
3245 if (abi < 2)
3246 {
3247 opd = bfd_get_section_by_name (abfd, ".opd");
3248 if (opd == NULL && abi == 1)
3249 return 0;
3250 }
3251
3252 symcount = static_count;
3253 if (!relocatable)
3254 symcount += dyn_count;
3255 if (symcount == 0)
3256 return 0;
3257
3258 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3259 if (syms == NULL)
3260 return -1;
3261
3262 if (!relocatable && static_count != 0 && dyn_count != 0)
3263 {
3264 /* Use both symbol tables. */
3265 memcpy (syms, static_syms, static_count * sizeof (*syms));
3266 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3267 }
3268 else if (!relocatable && static_count == 0)
3269 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3270 else
3271 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3272
3273 synthetic_relocatable = relocatable;
3274 synthetic_opd = opd;
3275 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3276
3277 if (!relocatable && symcount > 1)
3278 {
3279 long j;
3280 /* Trim duplicate syms, since we may have merged the normal and
3281 dynamic symbols. Actually, we only care about syms that have
3282 different values, so trim any with the same value. */
3283 for (i = 1, j = 1; i < symcount; ++i)
3284 if (syms[i - 1]->value + syms[i - 1]->section->vma
3285 != syms[i]->value + syms[i]->section->vma)
3286 syms[j++] = syms[i];
3287 symcount = j;
3288 }
3289
3290 i = 0;
3291 /* Note that here and in compare_symbols we can't compare opd and
3292 sym->section directly. With separate debug info files, the
3293 symbols will be extracted from the debug file while abfd passed
3294 to this function is the real binary. */
3295 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3296 ++i;
3297 codesecsym = i;
3298
3299 for (; i < symcount; ++i)
3300 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3301 != (SEC_CODE | SEC_ALLOC))
3302 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3303 break;
3304 codesecsymend = i;
3305
3306 for (; i < symcount; ++i)
3307 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3308 break;
3309 secsymend = i;
3310
3311 if (opd != NULL)
3312 for (; i < symcount; ++i)
3313 if (strcmp (syms[i]->section->name, ".opd") != 0)
3314 break;
3315 opdsymend = i;
3316
3317 for (; i < symcount; ++i)
3318 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3319 != (SEC_CODE | SEC_ALLOC))
3320 break;
3321 symcount = i;
3322
3323 count = 0;
3324
3325 if (relocatable)
3326 {
3327 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3328 arelent *r;
3329 size_t size;
3330 long relcount;
3331
3332 if (opdsymend == secsymend)
3333 goto done;
3334
3335 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3336 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3337 if (relcount == 0)
3338 goto done;
3339
3340 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3341 {
3342 count = -1;
3343 goto done;
3344 }
3345
3346 size = 0;
3347 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3348 {
3349 asymbol *sym;
3350
3351 while (r < opd->relocation + relcount
3352 && r->address < syms[i]->value + opd->vma)
3353 ++r;
3354
3355 if (r == opd->relocation + relcount)
3356 break;
3357
3358 if (r->address != syms[i]->value + opd->vma)
3359 continue;
3360
3361 if (r->howto->type != R_PPC64_ADDR64)
3362 continue;
3363
3364 sym = *r->sym_ptr_ptr;
3365 if (!sym_exists_at (syms, opdsymend, symcount,
3366 sym->section->id, sym->value + r->addend))
3367 {
3368 ++count;
3369 size += sizeof (asymbol);
3370 size += strlen (syms[i]->name) + 2;
3371 }
3372 }
3373
3374 if (size == 0)
3375 goto done;
3376 s = *ret = bfd_malloc (size);
3377 if (s == NULL)
3378 {
3379 count = -1;
3380 goto done;
3381 }
3382
3383 names = (char *) (s + count);
3384
3385 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3386 {
3387 asymbol *sym;
3388
3389 while (r < opd->relocation + relcount
3390 && r->address < syms[i]->value + opd->vma)
3391 ++r;
3392
3393 if (r == opd->relocation + relcount)
3394 break;
3395
3396 if (r->address != syms[i]->value + opd->vma)
3397 continue;
3398
3399 if (r->howto->type != R_PPC64_ADDR64)
3400 continue;
3401
3402 sym = *r->sym_ptr_ptr;
3403 if (!sym_exists_at (syms, opdsymend, symcount,
3404 sym->section->id, sym->value + r->addend))
3405 {
3406 size_t len;
3407
3408 *s = *syms[i];
3409 s->flags |= BSF_SYNTHETIC;
3410 s->section = sym->section;
3411 s->value = sym->value + r->addend;
3412 s->name = names;
3413 *names++ = '.';
3414 len = strlen (syms[i]->name);
3415 memcpy (names, syms[i]->name, len + 1);
3416 names += len + 1;
3417 /* Have udata.p point back to the original symbol this
3418 synthetic symbol was derived from. */
3419 s->udata.p = syms[i];
3420 s++;
3421 }
3422 }
3423 }
3424 else
3425 {
3426 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3427 bfd_byte *contents = NULL;
3428 size_t size;
3429 long plt_count = 0;
3430 bfd_vma glink_vma = 0, resolv_vma = 0;
3431 asection *dynamic, *glink = NULL, *relplt = NULL;
3432 arelent *p;
3433
3434 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3435 {
3436 free_contents_and_exit_err:
3437 count = -1;
3438 free_contents_and_exit:
3439 if (contents)
3440 free (contents);
3441 goto done;
3442 }
3443
3444 size = 0;
3445 for (i = secsymend; i < opdsymend; ++i)
3446 {
3447 bfd_vma ent;
3448
3449 /* Ignore bogus symbols. */
3450 if (syms[i]->value > opd->size - 8)
3451 continue;
3452
3453 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3454 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3455 {
3456 ++count;
3457 size += sizeof (asymbol);
3458 size += strlen (syms[i]->name) + 2;
3459 }
3460 }
3461
3462 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3463 if (dyn_count != 0
3464 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3465 {
3466 bfd_byte *dynbuf, *extdyn, *extdynend;
3467 size_t extdynsize;
3468 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3469
3470 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3471 goto free_contents_and_exit_err;
3472
3473 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3474 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3475
3476 extdyn = dynbuf;
3477 extdynend = extdyn + dynamic->size;
3478 for (; extdyn < extdynend; extdyn += extdynsize)
3479 {
3480 Elf_Internal_Dyn dyn;
3481 (*swap_dyn_in) (abfd, extdyn, &dyn);
3482
3483 if (dyn.d_tag == DT_NULL)
3484 break;
3485
3486 if (dyn.d_tag == DT_PPC64_GLINK)
3487 {
3488 /* The first glink stub starts at offset 32; see
3489 comment in ppc64_elf_finish_dynamic_sections. */
3490 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3491 /* The .glink section usually does not survive the final
3492 link; search for the section (usually .text) where the
3493 glink stubs now reside. */
3494 glink = bfd_sections_find_if (abfd, section_covers_vma,
3495 &glink_vma);
3496 break;
3497 }
3498 }
3499
3500 free (dynbuf);
3501 }
3502
3503 if (glink != NULL)
3504 {
3505 /* Determine __glink trampoline by reading the relative branch
3506 from the first glink stub. */
3507 bfd_byte buf[4];
3508 unsigned int off = 0;
3509
3510 while (bfd_get_section_contents (abfd, glink, buf,
3511 glink_vma + off - glink->vma, 4))
3512 {
3513 unsigned int insn = bfd_get_32 (abfd, buf);
3514 insn ^= B_DOT;
3515 if ((insn & ~0x3fffffc) == 0)
3516 {
3517 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3518 break;
3519 }
3520 off += 4;
3521 if (off > 4)
3522 break;
3523 }
3524
3525 if (resolv_vma)
3526 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3527
3528 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3529 if (relplt != NULL)
3530 {
3531 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3532 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3533 goto free_contents_and_exit_err;
3534
3535 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3536 size += plt_count * sizeof (asymbol);
3537
3538 p = relplt->relocation;
3539 for (i = 0; i < plt_count; i++, p++)
3540 {
3541 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3542 if (p->addend != 0)
3543 size += sizeof ("+0x") - 1 + 16;
3544 }
3545 }
3546 }
3547
3548 if (size == 0)
3549 goto free_contents_and_exit;
3550 s = *ret = bfd_malloc (size);
3551 if (s == NULL)
3552 goto free_contents_and_exit_err;
3553
3554 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3555
3556 for (i = secsymend; i < opdsymend; ++i)
3557 {
3558 bfd_vma ent;
3559
3560 if (syms[i]->value > opd->size - 8)
3561 continue;
3562
3563 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3564 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3565 {
3566 long lo, hi;
3567 size_t len;
3568 asection *sec = abfd->sections;
3569
3570 *s = *syms[i];
3571 lo = codesecsym;
3572 hi = codesecsymend;
3573 while (lo < hi)
3574 {
3575 long mid = (lo + hi) >> 1;
3576 if (syms[mid]->section->vma < ent)
3577 lo = mid + 1;
3578 else if (syms[mid]->section->vma > ent)
3579 hi = mid;
3580 else
3581 {
3582 sec = syms[mid]->section;
3583 break;
3584 }
3585 }
3586
3587 if (lo >= hi && lo > codesecsym)
3588 sec = syms[lo - 1]->section;
3589
3590 for (; sec != NULL; sec = sec->next)
3591 {
3592 if (sec->vma > ent)
3593 break;
3594 /* SEC_LOAD may not be set if SEC is from a separate debug
3595 info file. */
3596 if ((sec->flags & SEC_ALLOC) == 0)
3597 break;
3598 if ((sec->flags & SEC_CODE) != 0)
3599 s->section = sec;
3600 }
3601 s->flags |= BSF_SYNTHETIC;
3602 s->value = ent - s->section->vma;
3603 s->name = names;
3604 *names++ = '.';
3605 len = strlen (syms[i]->name);
3606 memcpy (names, syms[i]->name, len + 1);
3607 names += len + 1;
3608 /* Have udata.p point back to the original symbol this
3609 synthetic symbol was derived from. */
3610 s->udata.p = syms[i];
3611 s++;
3612 }
3613 }
3614 free (contents);
3615
3616 if (glink != NULL && relplt != NULL)
3617 {
3618 if (resolv_vma)
3619 {
3620 /* Add a symbol for the main glink trampoline. */
3621 memset (s, 0, sizeof *s);
3622 s->the_bfd = abfd;
3623 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3624 s->section = glink;
3625 s->value = resolv_vma - glink->vma;
3626 s->name = names;
3627 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3628 names += sizeof ("__glink_PLTresolve");
3629 s++;
3630 count++;
3631 }
3632
3633 /* FIXME: It would be very much nicer to put sym@plt on the
3634 stub rather than on the glink branch table entry. The
3635 objdump disassembler would then use a sensible symbol
3636 name on plt calls. The difficulty in doing so is
3637 a) finding the stubs, and,
3638 b) matching stubs against plt entries, and,
3639 c) there can be multiple stubs for a given plt entry.
3640
3641 Solving (a) could be done by code scanning, but older
3642 ppc64 binaries used different stubs to current code.
3643 (b) is the tricky one since you need to known the toc
3644 pointer for at least one function that uses a pic stub to
3645 be able to calculate the plt address referenced.
3646 (c) means gdb would need to set multiple breakpoints (or
3647 find the glink branch itself) when setting breakpoints
3648 for pending shared library loads. */
3649 p = relplt->relocation;
3650 for (i = 0; i < plt_count; i++, p++)
3651 {
3652 size_t len;
3653
3654 *s = **p->sym_ptr_ptr;
3655 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3656 we are defining a symbol, ensure one of them is set. */
3657 if ((s->flags & BSF_LOCAL) == 0)
3658 s->flags |= BSF_GLOBAL;
3659 s->flags |= BSF_SYNTHETIC;
3660 s->section = glink;
3661 s->value = glink_vma - glink->vma;
3662 s->name = names;
3663 s->udata.p = NULL;
3664 len = strlen ((*p->sym_ptr_ptr)->name);
3665 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3666 names += len;
3667 if (p->addend != 0)
3668 {
3669 memcpy (names, "+0x", sizeof ("+0x") - 1);
3670 names += sizeof ("+0x") - 1;
3671 bfd_sprintf_vma (abfd, names, p->addend);
3672 names += strlen (names);
3673 }
3674 memcpy (names, "@plt", sizeof ("@plt"));
3675 names += sizeof ("@plt");
3676 s++;
3677 if (abi < 2)
3678 {
3679 glink_vma += 8;
3680 if (i >= 0x8000)
3681 glink_vma += 4;
3682 }
3683 else
3684 glink_vma += 4;
3685 }
3686 count += plt_count;
3687 }
3688 }
3689
3690 done:
3691 free (syms);
3692 return count;
3693}
3694\f
3695/* The following functions are specific to the ELF linker, while
3696 functions above are used generally. Those named ppc64_elf_* are
3697 called by the main ELF linker code. They appear in this file more
3698 or less in the order in which they are called. eg.
3699 ppc64_elf_check_relocs is called early in the link process,
3700 ppc64_elf_finish_dynamic_sections is one of the last functions
3701 called.
3702
3703 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3704 functions have both a function code symbol and a function descriptor
3705 symbol. A call to foo in a relocatable object file looks like:
3706
3707 . .text
3708 . x:
3709 . bl .foo
3710 . nop
3711
3712 The function definition in another object file might be:
3713
3714 . .section .opd
3715 . foo: .quad .foo
3716 . .quad .TOC.@tocbase
3717 . .quad 0
3718 .
3719 . .text
3720 . .foo: blr
3721
3722 When the linker resolves the call during a static link, the branch
3723 unsurprisingly just goes to .foo and the .opd information is unused.
3724 If the function definition is in a shared library, things are a little
3725 different: The call goes via a plt call stub, the opd information gets
3726 copied to the plt, and the linker patches the nop.
3727
3728 . x:
3729 . bl .foo_stub
3730 . ld 2,40(1)
3731 .
3732 .
3733 . .foo_stub:
3734 . std 2,40(1) # in practice, the call stub
3735 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3736 . addi 11,11,Lfoo@toc@l # this is the general idea
3737 . ld 12,0(11)
3738 . ld 2,8(11)
3739 . mtctr 12
3740 . ld 11,16(11)
3741 . bctr
3742 .
3743 . .section .plt
3744 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3745
3746 The "reloc ()" notation is supposed to indicate that the linker emits
3747 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3748 copying.
3749
3750 What are the difficulties here? Well, firstly, the relocations
3751 examined by the linker in check_relocs are against the function code
3752 sym .foo, while the dynamic relocation in the plt is emitted against
3753 the function descriptor symbol, foo. Somewhere along the line, we need
3754 to carefully copy dynamic link information from one symbol to the other.
3755 Secondly, the generic part of the elf linker will make .foo a dynamic
3756 symbol as is normal for most other backends. We need foo dynamic
3757 instead, at least for an application final link. However, when
3758 creating a shared library containing foo, we need to have both symbols
3759 dynamic so that references to .foo are satisfied during the early
3760 stages of linking. Otherwise the linker might decide to pull in a
3761 definition from some other object, eg. a static library.
3762
3763 Update: As of August 2004, we support a new convention. Function
3764 calls may use the function descriptor symbol, ie. "bl foo". This
3765 behaves exactly as "bl .foo". */
3766
3767/* Of those relocs that might be copied as dynamic relocs, this function
3768 selects those that must be copied when linking a shared library,
3769 even when the symbol is local. */
3770
3771static int
3772must_be_dyn_reloc (struct bfd_link_info *info,
3773 enum elf_ppc64_reloc_type r_type)
3774{
3775 switch (r_type)
3776 {
3777 default:
3778 return 1;
3779
3780 case R_PPC64_REL32:
3781 case R_PPC64_REL64:
3782 case R_PPC64_REL30:
3783 return 0;
3784
3785 case R_PPC64_TPREL16:
3786 case R_PPC64_TPREL16_LO:
3787 case R_PPC64_TPREL16_HI:
3788 case R_PPC64_TPREL16_HA:
3789 case R_PPC64_TPREL16_DS:
3790 case R_PPC64_TPREL16_LO_DS:
3791 case R_PPC64_TPREL16_HIGH:
3792 case R_PPC64_TPREL16_HIGHA:
3793 case R_PPC64_TPREL16_HIGHER:
3794 case R_PPC64_TPREL16_HIGHERA:
3795 case R_PPC64_TPREL16_HIGHEST:
3796 case R_PPC64_TPREL16_HIGHESTA:
3797 case R_PPC64_TPREL64:
3798 return !bfd_link_executable (info);
3799 }
3800}
3801
3802/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3803 copying dynamic variables from a shared lib into an app's dynbss
3804 section, and instead use a dynamic relocation to point into the
3805 shared lib. With code that gcc generates, it's vital that this be
3806 enabled; In the PowerPC64 ABI, the address of a function is actually
3807 the address of a function descriptor, which resides in the .opd
3808 section. gcc uses the descriptor directly rather than going via the
3809 GOT as some other ABI's do, which means that initialized function
3810 pointers must reference the descriptor. Thus, a function pointer
3811 initialized to the address of a function in a shared library will
3812 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3813 redefines the function descriptor symbol to point to the copy. This
3814 presents a problem as a plt entry for that function is also
3815 initialized from the function descriptor symbol and the copy reloc
3816 may not be initialized first. */
3817#define ELIMINATE_COPY_RELOCS 1
3818
3819/* Section name for stubs is the associated section name plus this
3820 string. */
3821#define STUB_SUFFIX ".stub"
3822
3823/* Linker stubs.
3824 ppc_stub_long_branch:
3825 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3826 destination, but a 24 bit branch in a stub section will reach.
3827 . b dest
3828
3829 ppc_stub_plt_branch:
3830 Similar to the above, but a 24 bit branch in the stub section won't
3831 reach its destination.
3832 . addis %r11,%r2,xxx@toc@ha
3833 . ld %r12,xxx@toc@l(%r11)
3834 . mtctr %r12
3835 . bctr
3836
3837 ppc_stub_plt_call:
3838 Used to call a function in a shared library. If it so happens that
3839 the plt entry referenced crosses a 64k boundary, then an extra
3840 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3841 . std %r2,40(%r1)
3842 . addis %r11,%r2,xxx@toc@ha
3843 . ld %r12,xxx+0@toc@l(%r11)
3844 . mtctr %r12
3845 . ld %r2,xxx+8@toc@l(%r11)
3846 . ld %r11,xxx+16@toc@l(%r11)
3847 . bctr
3848
3849 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3850 code to adjust the value and save r2 to support multiple toc sections.
3851 A ppc_stub_long_branch with an r2 offset looks like:
3852 . std %r2,40(%r1)
3853 . addis %r2,%r2,off@ha
3854 . addi %r2,%r2,off@l
3855 . b dest
3856
3857 A ppc_stub_plt_branch with an r2 offset looks like:
3858 . std %r2,40(%r1)
3859 . addis %r11,%r2,xxx@toc@ha
3860 . ld %r12,xxx@toc@l(%r11)
3861 . addis %r2,%r2,off@ha
3862 . addi %r2,%r2,off@l
3863 . mtctr %r12
3864 . bctr
3865
3866 In cases where the "addis" instruction would add zero, the "addis" is
3867 omitted and following instructions modified slightly in some cases.
3868*/
3869
3870enum ppc_stub_type {
3871 ppc_stub_none,
3872 ppc_stub_long_branch,
3873 ppc_stub_long_branch_r2off,
3874 ppc_stub_plt_branch,
3875 ppc_stub_plt_branch_r2off,
3876 ppc_stub_plt_call,
3877 ppc_stub_plt_call_r2save,
3878 ppc_stub_global_entry,
3879 ppc_stub_save_res
3880};
3881
3882/* Information on stub grouping. */
3883struct map_stub
3884{
3885 /* The stub section. */
3886 asection *stub_sec;
3887 /* This is the section to which stubs in the group will be attached. */
3888 asection *link_sec;
3889 /* Next group. */
3890 struct map_stub *next;
3891 /* Whether to emit a copy of register save/restore functions in this
3892 group. */
3893 int needs_save_res;
3894};
3895
3896struct ppc_stub_hash_entry {
3897
3898 /* Base hash table entry structure. */
3899 struct bfd_hash_entry root;
3900
3901 enum ppc_stub_type stub_type;
3902
3903 /* Group information. */
3904 struct map_stub *group;
3905
3906 /* Offset within stub_sec of the beginning of this stub. */
3907 bfd_vma stub_offset;
3908
3909 /* Given the symbol's value and its section we can determine its final
3910 value when building the stubs (so the stub knows where to jump. */
3911 bfd_vma target_value;
3912 asection *target_section;
3913
3914 /* The symbol table entry, if any, that this was derived from. */
3915 struct ppc_link_hash_entry *h;
3916 struct plt_entry *plt_ent;
3917
3918 /* Symbol st_other. */
3919 unsigned char other;
3920};
3921
3922struct ppc_branch_hash_entry {
3923
3924 /* Base hash table entry structure. */
3925 struct bfd_hash_entry root;
3926
3927 /* Offset within branch lookup table. */
3928 unsigned int offset;
3929
3930 /* Generation marker. */
3931 unsigned int iter;
3932};
3933
3934/* Used to track dynamic relocations for local symbols. */
3935struct ppc_dyn_relocs
3936{
3937 struct ppc_dyn_relocs *next;
3938
3939 /* The input section of the reloc. */
3940 asection *sec;
3941
3942 /* Total number of relocs copied for the input section. */
3943 unsigned int count : 31;
3944
3945 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3946 unsigned int ifunc : 1;
3947};
3948
3949struct ppc_link_hash_entry
3950{
3951 struct elf_link_hash_entry elf;
3952
3953 union {
3954 /* A pointer to the most recently used stub hash entry against this
3955 symbol. */
3956 struct ppc_stub_hash_entry *stub_cache;
3957
3958 /* A pointer to the next symbol starting with a '.' */
3959 struct ppc_link_hash_entry *next_dot_sym;
3960 } u;
3961
3962 /* Track dynamic relocs copied for this symbol. */
3963 struct elf_dyn_relocs *dyn_relocs;
3964
3965 /* Chain of aliases referring to a weakdef. */
3966 struct ppc_link_hash_entry *weakref;
3967
3968 /* Link between function code and descriptor symbols. */
3969 struct ppc_link_hash_entry *oh;
3970
3971 /* Flag function code and descriptor symbols. */
3972 unsigned int is_func:1;
3973 unsigned int is_func_descriptor:1;
3974 unsigned int fake:1;
3975
3976 /* Whether global opd/toc sym has been adjusted or not.
3977 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3978 should be set for all globals defined in any opd/toc section. */
3979 unsigned int adjust_done:1;
3980
3981 /* Set if we twiddled this symbol to weak at some stage. */
3982 unsigned int was_undefined:1;
3983
3984 /* Set if this is an out-of-line register save/restore function,
3985 with non-standard calling convention. */
3986 unsigned int save_res:1;
3987
3988 /* Contexts in which symbol is used in the GOT (or TOC).
3989 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3990 corresponding relocs are encountered during check_relocs.
3991 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3992 indicate the corresponding GOT entry type is not needed.
3993 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3994 a TPREL one. We use a separate flag rather than setting TPREL
3995 just for convenience in distinguishing the two cases. */
3996#define TLS_GD 1 /* GD reloc. */
3997#define TLS_LD 2 /* LD reloc. */
3998#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3999#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4000#define TLS_TLS 16 /* Any TLS reloc. */
4001#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4002#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4003#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
4004 unsigned char tls_mask;
4005};
4006
4007/* ppc64 ELF linker hash table. */
4008
4009struct ppc_link_hash_table
4010{
4011 struct elf_link_hash_table elf;
4012
4013 /* The stub hash table. */
4014 struct bfd_hash_table stub_hash_table;
4015
4016 /* Another hash table for plt_branch stubs. */
4017 struct bfd_hash_table branch_hash_table;
4018
4019 /* Hash table for function prologue tocsave. */
4020 htab_t tocsave_htab;
4021
4022 /* Various options and other info passed from the linker. */
4023 struct ppc64_elf_params *params;
4024
4025 /* The size of sec_info below. */
4026 unsigned int sec_info_arr_size;
4027
4028 /* Per-section array of extra section info. Done this way rather
4029 than as part of ppc64_elf_section_data so we have the info for
4030 non-ppc64 sections. */
4031 struct
4032 {
4033 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4034 bfd_vma toc_off;
4035
4036 union
4037 {
4038 /* The section group that this section belongs to. */
4039 struct map_stub *group;
4040 /* A temp section list pointer. */
4041 asection *list;
4042 } u;
4043 } *sec_info;
4044
4045 /* Linked list of groups. */
4046 struct map_stub *group;
4047
4048 /* Temp used when calculating TOC pointers. */
4049 bfd_vma toc_curr;
4050 bfd *toc_bfd;
4051 asection *toc_first_sec;
4052
4053 /* Used when adding symbols. */
4054 struct ppc_link_hash_entry *dot_syms;
4055
4056 /* Shortcuts to get to dynamic linker sections. */
4057 asection *dynbss;
4058 asection *relbss;
4059 asection *glink;
4060 asection *sfpr;
4061 asection *brlt;
4062 asection *relbrlt;
4063 asection *glink_eh_frame;
4064
4065 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4066 struct ppc_link_hash_entry *tls_get_addr;
4067 struct ppc_link_hash_entry *tls_get_addr_fd;
4068
4069 /* The size of reliplt used by got entry relocs. */
4070 bfd_size_type got_reli_size;
4071
4072 /* Statistics. */
4073 unsigned long stub_count[ppc_stub_global_entry];
4074
4075 /* Number of stubs against global syms. */
4076 unsigned long stub_globals;
4077
4078 /* Set if we're linking code with function descriptors. */
4079 unsigned int opd_abi:1;
4080
4081 /* Support for multiple toc sections. */
4082 unsigned int do_multi_toc:1;
4083 unsigned int multi_toc_needed:1;
4084 unsigned int second_toc_pass:1;
4085 unsigned int do_toc_opt:1;
4086
4087 /* Set on error. */
4088 unsigned int stub_error:1;
4089
4090 /* Temp used by ppc64_elf_before_check_relocs. */
4091 unsigned int twiddled_syms:1;
4092
4093 /* Incremented every time we size stubs. */
4094 unsigned int stub_iteration;
4095
4096 /* Small local sym cache. */
4097 struct sym_cache sym_cache;
4098};
4099
4100/* Rename some of the generic section flags to better document how they
4101 are used here. */
4102
4103/* Nonzero if this section has TLS related relocations. */
4104#define has_tls_reloc sec_flg0
4105
4106/* Nonzero if this section has a call to __tls_get_addr. */
4107#define has_tls_get_addr_call sec_flg1
4108
4109/* Nonzero if this section has any toc or got relocs. */
4110#define has_toc_reloc sec_flg2
4111
4112/* Nonzero if this section has a call to another section that uses
4113 the toc or got. */
4114#define makes_toc_func_call sec_flg3
4115
4116/* Recursion protection when determining above flag. */
4117#define call_check_in_progress sec_flg4
4118#define call_check_done sec_flg5
4119
4120/* Get the ppc64 ELF linker hash table from a link_info structure. */
4121
4122#define ppc_hash_table(p) \
4123 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4124 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4125
4126#define ppc_stub_hash_lookup(table, string, create, copy) \
4127 ((struct ppc_stub_hash_entry *) \
4128 bfd_hash_lookup ((table), (string), (create), (copy)))
4129
4130#define ppc_branch_hash_lookup(table, string, create, copy) \
4131 ((struct ppc_branch_hash_entry *) \
4132 bfd_hash_lookup ((table), (string), (create), (copy)))
4133
4134/* Create an entry in the stub hash table. */
4135
4136static struct bfd_hash_entry *
4137stub_hash_newfunc (struct bfd_hash_entry *entry,
4138 struct bfd_hash_table *table,
4139 const char *string)
4140{
4141 /* Allocate the structure if it has not already been allocated by a
4142 subclass. */
4143 if (entry == NULL)
4144 {
4145 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4146 if (entry == NULL)
4147 return entry;
4148 }
4149
4150 /* Call the allocation method of the superclass. */
4151 entry = bfd_hash_newfunc (entry, table, string);
4152 if (entry != NULL)
4153 {
4154 struct ppc_stub_hash_entry *eh;
4155
4156 /* Initialize the local fields. */
4157 eh = (struct ppc_stub_hash_entry *) entry;
4158 eh->stub_type = ppc_stub_none;
4159 eh->group = NULL;
4160 eh->stub_offset = 0;
4161 eh->target_value = 0;
4162 eh->target_section = NULL;
4163 eh->h = NULL;
4164 eh->plt_ent = NULL;
4165 eh->other = 0;
4166 }
4167
4168 return entry;
4169}
4170
4171/* Create an entry in the branch hash table. */
4172
4173static struct bfd_hash_entry *
4174branch_hash_newfunc (struct bfd_hash_entry *entry,
4175 struct bfd_hash_table *table,
4176 const char *string)
4177{
4178 /* Allocate the structure if it has not already been allocated by a
4179 subclass. */
4180 if (entry == NULL)
4181 {
4182 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4183 if (entry == NULL)
4184 return entry;
4185 }
4186
4187 /* Call the allocation method of the superclass. */
4188 entry = bfd_hash_newfunc (entry, table, string);
4189 if (entry != NULL)
4190 {
4191 struct ppc_branch_hash_entry *eh;
4192
4193 /* Initialize the local fields. */
4194 eh = (struct ppc_branch_hash_entry *) entry;
4195 eh->offset = 0;
4196 eh->iter = 0;
4197 }
4198
4199 return entry;
4200}
4201
4202/* Create an entry in a ppc64 ELF linker hash table. */
4203
4204static struct bfd_hash_entry *
4205link_hash_newfunc (struct bfd_hash_entry *entry,
4206 struct bfd_hash_table *table,
4207 const char *string)
4208{
4209 /* Allocate the structure if it has not already been allocated by a
4210 subclass. */
4211 if (entry == NULL)
4212 {
4213 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4214 if (entry == NULL)
4215 return entry;
4216 }
4217
4218 /* Call the allocation method of the superclass. */
4219 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4220 if (entry != NULL)
4221 {
4222 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4223
4224 memset (&eh->u.stub_cache, 0,
4225 (sizeof (struct ppc_link_hash_entry)
4226 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4227
4228 /* When making function calls, old ABI code references function entry
4229 points (dot symbols), while new ABI code references the function
4230 descriptor symbol. We need to make any combination of reference and
4231 definition work together, without breaking archive linking.
4232
4233 For a defined function "foo" and an undefined call to "bar":
4234 An old object defines "foo" and ".foo", references ".bar" (possibly
4235 "bar" too).
4236 A new object defines "foo" and references "bar".
4237
4238 A new object thus has no problem with its undefined symbols being
4239 satisfied by definitions in an old object. On the other hand, the
4240 old object won't have ".bar" satisfied by a new object.
4241
4242 Keep a list of newly added dot-symbols. */
4243
4244 if (string[0] == '.')
4245 {
4246 struct ppc_link_hash_table *htab;
4247
4248 htab = (struct ppc_link_hash_table *) table;
4249 eh->u.next_dot_sym = htab->dot_syms;
4250 htab->dot_syms = eh;
4251 }
4252 }
4253
4254 return entry;
4255}
4256
4257struct tocsave_entry {
4258 asection *sec;
4259 bfd_vma offset;
4260};
4261
4262static hashval_t
4263tocsave_htab_hash (const void *p)
4264{
4265 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4266 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4267}
4268
4269static int
4270tocsave_htab_eq (const void *p1, const void *p2)
4271{
4272 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4273 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4274 return e1->sec == e2->sec && e1->offset == e2->offset;
4275}
4276
4277/* Destroy a ppc64 ELF linker hash table. */
4278
4279static void
4280ppc64_elf_link_hash_table_free (bfd *obfd)
4281{
4282 struct ppc_link_hash_table *htab;
4283
4284 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4285 if (htab->tocsave_htab)
4286 htab_delete (htab->tocsave_htab);
4287 bfd_hash_table_free (&htab->branch_hash_table);
4288 bfd_hash_table_free (&htab->stub_hash_table);
4289 _bfd_elf_link_hash_table_free (obfd);
4290}
4291
4292/* Create a ppc64 ELF linker hash table. */
4293
4294static struct bfd_link_hash_table *
4295ppc64_elf_link_hash_table_create (bfd *abfd)
4296{
4297 struct ppc_link_hash_table *htab;
4298 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4299
4300 htab = bfd_zmalloc (amt);
4301 if (htab == NULL)
4302 return NULL;
4303
4304 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4305 sizeof (struct ppc_link_hash_entry),
4306 PPC64_ELF_DATA))
4307 {
4308 free (htab);
4309 return NULL;
4310 }
4311
4312 /* Init the stub hash table too. */
4313 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4314 sizeof (struct ppc_stub_hash_entry)))
4315 {
4316 _bfd_elf_link_hash_table_free (abfd);
4317 return NULL;
4318 }
4319
4320 /* And the branch hash table. */
4321 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4322 sizeof (struct ppc_branch_hash_entry)))
4323 {
4324 bfd_hash_table_free (&htab->stub_hash_table);
4325 _bfd_elf_link_hash_table_free (abfd);
4326 return NULL;
4327 }
4328
4329 htab->tocsave_htab = htab_try_create (1024,
4330 tocsave_htab_hash,
4331 tocsave_htab_eq,
4332 NULL);
4333 if (htab->tocsave_htab == NULL)
4334 {
4335 ppc64_elf_link_hash_table_free (abfd);
4336 return NULL;
4337 }
4338 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4339
4340 /* Initializing two fields of the union is just cosmetic. We really
4341 only care about glist, but when compiled on a 32-bit host the
4342 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4343 debugger inspection of these fields look nicer. */
4344 htab->elf.init_got_refcount.refcount = 0;
4345 htab->elf.init_got_refcount.glist = NULL;
4346 htab->elf.init_plt_refcount.refcount = 0;
4347 htab->elf.init_plt_refcount.glist = NULL;
4348 htab->elf.init_got_offset.offset = 0;
4349 htab->elf.init_got_offset.glist = NULL;
4350 htab->elf.init_plt_offset.offset = 0;
4351 htab->elf.init_plt_offset.glist = NULL;
4352
4353 return &htab->elf.root;
4354}
4355
4356/* Create sections for linker generated code. */
4357
4358static bfd_boolean
4359create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4360{
4361 struct ppc_link_hash_table *htab;
4362 flagword flags;
4363
4364 htab = ppc_hash_table (info);
4365
4366 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4367 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4368 if (htab->params->save_restore_funcs)
4369 {
4370 /* Create .sfpr for code to save and restore fp regs. */
4371 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4372 flags);
4373 if (htab->sfpr == NULL
4374 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4375 return FALSE;
4376 }
4377
4378 if (bfd_link_relocatable (info))
4379 return TRUE;
4380
4381 /* Create .glink for lazy dynamic linking support. */
4382 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4383 flags);
4384 if (htab->glink == NULL
4385 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4386 return FALSE;
4387
4388 if (!info->no_ld_generated_unwind_info)
4389 {
4390 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4391 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4392 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4393 ".eh_frame",
4394 flags);
4395 if (htab->glink_eh_frame == NULL
4396 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4397 return FALSE;
4398 }
4399
4400 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4401 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4402 if (htab->elf.iplt == NULL
4403 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4404 return FALSE;
4405
4406 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4407 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4408 htab->elf.irelplt
4409 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4410 if (htab->elf.irelplt == NULL
4411 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4412 return FALSE;
4413
4414 /* Create branch lookup table for plt_branch stubs. */
4415 flags = (SEC_ALLOC | SEC_LOAD
4416 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4417 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4418 flags);
4419 if (htab->brlt == NULL
4420 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4421 return FALSE;
4422
4423 if (!bfd_link_pic (info))
4424 return TRUE;
4425
4426 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4427 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4428 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4429 ".rela.branch_lt",
4430 flags);
4431 if (htab->relbrlt == NULL
4432 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4433 return FALSE;
4434
4435 return TRUE;
4436}
4437
4438/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4439
4440bfd_boolean
4441ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4442 struct ppc64_elf_params *params)
4443{
4444 struct ppc_link_hash_table *htab;
4445
4446 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4447
4448/* Always hook our dynamic sections into the first bfd, which is the
4449 linker created stub bfd. This ensures that the GOT header is at
4450 the start of the output TOC section. */
4451 htab = ppc_hash_table (info);
4452 htab->elf.dynobj = params->stub_bfd;
4453 htab->params = params;
4454
4455 return create_linkage_sections (htab->elf.dynobj, info);
4456}
4457
4458/* Build a name for an entry in the stub hash table. */
4459
4460static char *
4461ppc_stub_name (const asection *input_section,
4462 const asection *sym_sec,
4463 const struct ppc_link_hash_entry *h,
4464 const Elf_Internal_Rela *rel)
4465{
4466 char *stub_name;
4467 ssize_t len;
4468
4469 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4470 offsets from a sym as a branch target? In fact, we could
4471 probably assume the addend is always zero. */
4472 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4473
4474 if (h)
4475 {
4476 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4477 stub_name = bfd_malloc (len);
4478 if (stub_name == NULL)
4479 return stub_name;
4480
4481 len = sprintf (stub_name, "%08x.%s+%x",
4482 input_section->id & 0xffffffff,
4483 h->elf.root.root.string,
4484 (int) rel->r_addend & 0xffffffff);
4485 }
4486 else
4487 {
4488 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4489 stub_name = bfd_malloc (len);
4490 if (stub_name == NULL)
4491 return stub_name;
4492
4493 len = sprintf (stub_name, "%08x.%x:%x+%x",
4494 input_section->id & 0xffffffff,
4495 sym_sec->id & 0xffffffff,
4496 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4497 (int) rel->r_addend & 0xffffffff);
4498 }
4499 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4500 stub_name[len - 2] = 0;
4501 return stub_name;
4502}
4503
4504/* Look up an entry in the stub hash. Stub entries are cached because
4505 creating the stub name takes a bit of time. */
4506
4507static struct ppc_stub_hash_entry *
4508ppc_get_stub_entry (const asection *input_section,
4509 const asection *sym_sec,
4510 struct ppc_link_hash_entry *h,
4511 const Elf_Internal_Rela *rel,
4512 struct ppc_link_hash_table *htab)
4513{
4514 struct ppc_stub_hash_entry *stub_entry;
4515 struct map_stub *group;
4516
4517 /* If this input section is part of a group of sections sharing one
4518 stub section, then use the id of the first section in the group.
4519 Stub names need to include a section id, as there may well be
4520 more than one stub used to reach say, printf, and we need to
4521 distinguish between them. */
4522 group = htab->sec_info[input_section->id].u.group;
4523 if (group == NULL)
4524 return NULL;
4525
4526 if (h != NULL && h->u.stub_cache != NULL
4527 && h->u.stub_cache->h == h
4528 && h->u.stub_cache->group == group)
4529 {
4530 stub_entry = h->u.stub_cache;
4531 }
4532 else
4533 {
4534 char *stub_name;
4535
4536 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4537 if (stub_name == NULL)
4538 return NULL;
4539
4540 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4541 stub_name, FALSE, FALSE);
4542 if (h != NULL)
4543 h->u.stub_cache = stub_entry;
4544
4545 free (stub_name);
4546 }
4547
4548 return stub_entry;
4549}
4550
4551/* Add a new stub entry to the stub hash. Not all fields of the new
4552 stub entry are initialised. */
4553
4554static struct ppc_stub_hash_entry *
4555ppc_add_stub (const char *stub_name,
4556 asection *section,
4557 struct bfd_link_info *info)
4558{
4559 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4560 struct map_stub *group;
4561 asection *link_sec;
4562 asection *stub_sec;
4563 struct ppc_stub_hash_entry *stub_entry;
4564
4565 group = htab->sec_info[section->id].u.group;
4566 link_sec = group->link_sec;
4567 stub_sec = group->stub_sec;
4568 if (stub_sec == NULL)
4569 {
4570 size_t namelen;
4571 bfd_size_type len;
4572 char *s_name;
4573
4574 namelen = strlen (link_sec->name);
4575 len = namelen + sizeof (STUB_SUFFIX);
4576 s_name = bfd_alloc (htab->params->stub_bfd, len);
4577 if (s_name == NULL)
4578 return NULL;
4579
4580 memcpy (s_name, link_sec->name, namelen);
4581 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4582 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4583 if (stub_sec == NULL)
4584 return NULL;
4585 group->stub_sec = stub_sec;
4586 }
4587
4588 /* Enter this entry into the linker stub hash table. */
4589 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4590 TRUE, FALSE);
4591 if (stub_entry == NULL)
4592 {
4593 /* xgettext:c-format */
4594 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4595 section->owner, stub_name);
4596 return NULL;
4597 }
4598
4599 stub_entry->group = group;
4600 stub_entry->stub_offset = 0;
4601 return stub_entry;
4602}
4603
4604/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4605 not already done. */
4606
4607static bfd_boolean
4608create_got_section (bfd *abfd, struct bfd_link_info *info)
4609{
4610 asection *got, *relgot;
4611 flagword flags;
4612 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4613
4614 if (!is_ppc64_elf (abfd))
4615 return FALSE;
4616 if (htab == NULL)
4617 return FALSE;
4618
4619 if (!htab->elf.sgot
4620 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4621 return FALSE;
4622
4623 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4624 | SEC_LINKER_CREATED);
4625
4626 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4627 if (!got
4628 || !bfd_set_section_alignment (abfd, got, 3))
4629 return FALSE;
4630
4631 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4632 flags | SEC_READONLY);
4633 if (!relgot
4634 || ! bfd_set_section_alignment (abfd, relgot, 3))
4635 return FALSE;
4636
4637 ppc64_elf_tdata (abfd)->got = got;
4638 ppc64_elf_tdata (abfd)->relgot = relgot;
4639 return TRUE;
4640}
4641
4642/* Create the dynamic sections, and set up shortcuts. */
4643
4644static bfd_boolean
4645ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4646{
4647 struct ppc_link_hash_table *htab;
4648
4649 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4650 return FALSE;
4651
4652 htab = ppc_hash_table (info);
4653 if (htab == NULL)
4654 return FALSE;
4655
4656 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4657 if (!bfd_link_pic (info))
4658 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4659
4660 if (!htab->elf.sgot || !htab->elf.splt || !htab->elf.srelplt || !htab->dynbss
4661 || (!bfd_link_pic (info) && !htab->relbss))
4662 abort ();
4663
4664 return TRUE;
4665}
4666
4667/* Follow indirect and warning symbol links. */
4668
4669static inline struct bfd_link_hash_entry *
4670follow_link (struct bfd_link_hash_entry *h)
4671{
4672 while (h->type == bfd_link_hash_indirect
4673 || h->type == bfd_link_hash_warning)
4674 h = h->u.i.link;
4675 return h;
4676}
4677
4678static inline struct elf_link_hash_entry *
4679elf_follow_link (struct elf_link_hash_entry *h)
4680{
4681 return (struct elf_link_hash_entry *) follow_link (&h->root);
4682}
4683
4684static inline struct ppc_link_hash_entry *
4685ppc_follow_link (struct ppc_link_hash_entry *h)
4686{
4687 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4688}
4689
4690/* Merge PLT info on FROM with that on TO. */
4691
4692static void
4693move_plt_plist (struct ppc_link_hash_entry *from,
4694 struct ppc_link_hash_entry *to)
4695{
4696 if (from->elf.plt.plist != NULL)
4697 {
4698 if (to->elf.plt.plist != NULL)
4699 {
4700 struct plt_entry **entp;
4701 struct plt_entry *ent;
4702
4703 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4704 {
4705 struct plt_entry *dent;
4706
4707 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4708 if (dent->addend == ent->addend)
4709 {
4710 dent->plt.refcount += ent->plt.refcount;
4711 *entp = ent->next;
4712 break;
4713 }
4714 if (dent == NULL)
4715 entp = &ent->next;
4716 }
4717 *entp = to->elf.plt.plist;
4718 }
4719
4720 to->elf.plt.plist = from->elf.plt.plist;
4721 from->elf.plt.plist = NULL;
4722 }
4723}
4724
4725/* Copy the extra info we tack onto an elf_link_hash_entry. */
4726
4727static void
4728ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4729 struct elf_link_hash_entry *dir,
4730 struct elf_link_hash_entry *ind)
4731{
4732 struct ppc_link_hash_entry *edir, *eind;
4733
4734 edir = (struct ppc_link_hash_entry *) dir;
4735 eind = (struct ppc_link_hash_entry *) ind;
4736
4737 edir->is_func |= eind->is_func;
4738 edir->is_func_descriptor |= eind->is_func_descriptor;
4739 edir->tls_mask |= eind->tls_mask;
4740 if (eind->oh != NULL)
4741 edir->oh = ppc_follow_link (eind->oh);
4742
4743 /* If called to transfer flags for a weakdef during processing
4744 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4745 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4746 if (!(ELIMINATE_COPY_RELOCS
4747 && eind->elf.root.type != bfd_link_hash_indirect
4748 && edir->elf.dynamic_adjusted))
4749 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4750
4751 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4752 edir->elf.ref_regular |= eind->elf.ref_regular;
4753 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4754 edir->elf.needs_plt |= eind->elf.needs_plt;
4755 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4756
4757 /* If we were called to copy over info for a weak sym, don't copy
4758 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4759 in order to simplify readonly_dynrelocs and save a field in the
4760 symbol hash entry, but that means dyn_relocs can't be used in any
4761 tests about a specific symbol, or affect other symbol flags which
4762 are then tested.
4763 Chain weakdefs so we can get from the weakdef back to an alias.
4764 The list is circular so that we don't need to use u.weakdef as
4765 well as this list to look at all aliases. */
4766 if (eind->elf.root.type != bfd_link_hash_indirect)
4767 {
4768 struct ppc_link_hash_entry *cur, *add, *next;
4769
4770 add = eind;
4771 do
4772 {
4773 cur = edir->weakref;
4774 if (cur != NULL)
4775 {
4776 do
4777 {
4778 /* We can be called twice for the same symbols.
4779 Don't make multiple loops. */
4780 if (cur == add)
4781 return;
4782 cur = cur->weakref;
4783 } while (cur != edir);
4784 }
4785 next = add->weakref;
4786 if (cur != add)
4787 {
4788 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4789 edir->weakref = add;
4790 }
4791 add = next;
4792 } while (add != NULL && add != eind);
4793 return;
4794 }
4795
4796 /* Copy over any dynamic relocs we may have on the indirect sym. */
4797 if (eind->dyn_relocs != NULL)
4798 {
4799 if (edir->dyn_relocs != NULL)
4800 {
4801 struct elf_dyn_relocs **pp;
4802 struct elf_dyn_relocs *p;
4803
4804 /* Add reloc counts against the indirect sym to the direct sym
4805 list. Merge any entries against the same section. */
4806 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4807 {
4808 struct elf_dyn_relocs *q;
4809
4810 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4811 if (q->sec == p->sec)
4812 {
4813 q->pc_count += p->pc_count;
4814 q->count += p->count;
4815 *pp = p->next;
4816 break;
4817 }
4818 if (q == NULL)
4819 pp = &p->next;
4820 }
4821 *pp = edir->dyn_relocs;
4822 }
4823
4824 edir->dyn_relocs = eind->dyn_relocs;
4825 eind->dyn_relocs = NULL;
4826 }
4827
4828 /* Copy over got entries that we may have already seen to the
4829 symbol which just became indirect. */
4830 if (eind->elf.got.glist != NULL)
4831 {
4832 if (edir->elf.got.glist != NULL)
4833 {
4834 struct got_entry **entp;
4835 struct got_entry *ent;
4836
4837 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4838 {
4839 struct got_entry *dent;
4840
4841 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4842 if (dent->addend == ent->addend
4843 && dent->owner == ent->owner
4844 && dent->tls_type == ent->tls_type)
4845 {
4846 dent->got.refcount += ent->got.refcount;
4847 *entp = ent->next;
4848 break;
4849 }
4850 if (dent == NULL)
4851 entp = &ent->next;
4852 }
4853 *entp = edir->elf.got.glist;
4854 }
4855
4856 edir->elf.got.glist = eind->elf.got.glist;
4857 eind->elf.got.glist = NULL;
4858 }
4859
4860 /* And plt entries. */
4861 move_plt_plist (eind, edir);
4862
4863 if (eind->elf.dynindx != -1)
4864 {
4865 if (edir->elf.dynindx != -1)
4866 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4867 edir->elf.dynstr_index);
4868 edir->elf.dynindx = eind->elf.dynindx;
4869 edir->elf.dynstr_index = eind->elf.dynstr_index;
4870 eind->elf.dynindx = -1;
4871 eind->elf.dynstr_index = 0;
4872 }
4873}
4874
4875/* Find the function descriptor hash entry from the given function code
4876 hash entry FH. Link the entries via their OH fields. */
4877
4878static struct ppc_link_hash_entry *
4879lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4880{
4881 struct ppc_link_hash_entry *fdh = fh->oh;
4882
4883 if (fdh == NULL)
4884 {
4885 const char *fd_name = fh->elf.root.root.string + 1;
4886
4887 fdh = (struct ppc_link_hash_entry *)
4888 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4889 if (fdh == NULL)
4890 return fdh;
4891
4892 fdh->is_func_descriptor = 1;
4893 fdh->oh = fh;
4894 fh->is_func = 1;
4895 fh->oh = fdh;
4896 }
4897
4898 return ppc_follow_link (fdh);
4899}
4900
4901/* Make a fake function descriptor sym for the code sym FH. */
4902
4903static struct ppc_link_hash_entry *
4904make_fdh (struct bfd_link_info *info,
4905 struct ppc_link_hash_entry *fh)
4906{
4907 bfd *abfd;
4908 asymbol *newsym;
4909 struct bfd_link_hash_entry *bh;
4910 struct ppc_link_hash_entry *fdh;
4911
4912 abfd = fh->elf.root.u.undef.abfd;
4913 newsym = bfd_make_empty_symbol (abfd);
4914 newsym->name = fh->elf.root.root.string + 1;
4915 newsym->section = bfd_und_section_ptr;
4916 newsym->value = 0;
4917 newsym->flags = BSF_WEAK;
4918
4919 bh = NULL;
4920 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4921 newsym->flags, newsym->section,
4922 newsym->value, NULL, FALSE, FALSE,
4923 &bh))
4924 return NULL;
4925
4926 fdh = (struct ppc_link_hash_entry *) bh;
4927 fdh->elf.non_elf = 0;
4928 fdh->fake = 1;
4929 fdh->is_func_descriptor = 1;
4930 fdh->oh = fh;
4931 fh->is_func = 1;
4932 fh->oh = fdh;
4933 return fdh;
4934}
4935
4936/* Fix function descriptor symbols defined in .opd sections to be
4937 function type. */
4938
4939static bfd_boolean
4940ppc64_elf_add_symbol_hook (bfd *ibfd,
4941 struct bfd_link_info *info,
4942 Elf_Internal_Sym *isym,
4943 const char **name,
4944 flagword *flags ATTRIBUTE_UNUSED,
4945 asection **sec,
4946 bfd_vma *value)
4947{
4948 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4949 && (ibfd->flags & DYNAMIC) == 0
4950 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4951 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4952
4953 if (*sec != NULL
4954 && strcmp ((*sec)->name, ".opd") == 0)
4955 {
4956 asection *code_sec;
4957
4958 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4959 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4960 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4961
4962 /* If the symbol is a function defined in .opd, and the function
4963 code is in a discarded group, let it appear to be undefined. */
4964 if (!bfd_link_relocatable (info)
4965 && (*sec)->reloc_count != 0
4966 && opd_entry_value (*sec, *value, &code_sec, NULL,
4967 FALSE) != (bfd_vma) -1
4968 && discarded_section (code_sec))
4969 {
4970 *sec = bfd_und_section_ptr;
4971 isym->st_shndx = SHN_UNDEF;
4972 }
4973 }
4974 else if (*sec != NULL
4975 && strcmp ((*sec)->name, ".toc") == 0
4976 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4977 {
4978 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4979 if (htab != NULL)
4980 htab->params->object_in_toc = 1;
4981 }
4982
4983 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4984 {
4985 if (abiversion (ibfd) == 0)
4986 set_abiversion (ibfd, 2);
4987 else if (abiversion (ibfd) == 1)
4988 {
4989 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4990 " for ABI version 1\n"), name);
4991 bfd_set_error (bfd_error_bad_value);
4992 return FALSE;
4993 }
4994 }
4995
4996 return TRUE;
4997}
4998
4999/* Merge non-visibility st_other attributes: local entry point. */
5000
5001static void
5002ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5003 const Elf_Internal_Sym *isym,
5004 bfd_boolean definition,
5005 bfd_boolean dynamic)
5006{
5007 if (definition && !dynamic)
5008 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5009 | ELF_ST_VISIBILITY (h->other));
5010}
5011
5012/* This function makes an old ABI object reference to ".bar" cause the
5013 inclusion of a new ABI object archive that defines "bar".
5014 NAME is a symbol defined in an archive. Return a symbol in the hash
5015 table that might be satisfied by the archive symbols. */
5016
5017static struct elf_link_hash_entry *
5018ppc64_elf_archive_symbol_lookup (bfd *abfd,
5019 struct bfd_link_info *info,
5020 const char *name)
5021{
5022 struct elf_link_hash_entry *h;
5023 char *dot_name;
5024 size_t len;
5025
5026 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5027 if (h != NULL
5028 /* Don't return this sym if it is a fake function descriptor
5029 created by add_symbol_adjust. */
5030 && !(h->root.type == bfd_link_hash_undefweak
5031 && ((struct ppc_link_hash_entry *) h)->fake))
5032 return h;
5033
5034 if (name[0] == '.')
5035 return h;
5036
5037 len = strlen (name);
5038 dot_name = bfd_alloc (abfd, len + 2);
5039 if (dot_name == NULL)
5040 return (struct elf_link_hash_entry *) 0 - 1;
5041 dot_name[0] = '.';
5042 memcpy (dot_name + 1, name, len + 1);
5043 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5044 bfd_release (abfd, dot_name);
5045 return h;
5046}
5047
5048/* This function satisfies all old ABI object references to ".bar" if a
5049 new ABI object defines "bar". Well, at least, undefined dot symbols
5050 are made weak. This stops later archive searches from including an
5051 object if we already have a function descriptor definition. It also
5052 prevents the linker complaining about undefined symbols.
5053 We also check and correct mismatched symbol visibility here. The
5054 most restrictive visibility of the function descriptor and the
5055 function entry symbol is used. */
5056
5057static bfd_boolean
5058add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5059{
5060 struct ppc_link_hash_table *htab;
5061 struct ppc_link_hash_entry *fdh;
5062
5063 if (eh->elf.root.type == bfd_link_hash_indirect)
5064 return TRUE;
5065
5066 if (eh->elf.root.type == bfd_link_hash_warning)
5067 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5068
5069 if (eh->elf.root.root.string[0] != '.')
5070 abort ();
5071
5072 htab = ppc_hash_table (info);
5073 if (htab == NULL)
5074 return FALSE;
5075
5076 fdh = lookup_fdh (eh, htab);
5077 if (fdh == NULL)
5078 {
5079 if (!bfd_link_relocatable (info)
5080 && (eh->elf.root.type == bfd_link_hash_undefined
5081 || eh->elf.root.type == bfd_link_hash_undefweak)
5082 && eh->elf.ref_regular)
5083 {
5084 /* Make an undefweak function descriptor sym, which is enough to
5085 pull in an --as-needed shared lib, but won't cause link
5086 errors. Archives are handled elsewhere. */
5087 fdh = make_fdh (info, eh);
5088 if (fdh == NULL)
5089 return FALSE;
5090 fdh->elf.ref_regular = 1;
5091 }
5092 }
5093 else
5094 {
5095 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5096 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5097 if (entry_vis < descr_vis)
5098 fdh->elf.other += entry_vis - descr_vis;
5099 else if (entry_vis > descr_vis)
5100 eh->elf.other += descr_vis - entry_vis;
5101
5102 if ((fdh->elf.root.type == bfd_link_hash_defined
5103 || fdh->elf.root.type == bfd_link_hash_defweak)
5104 && eh->elf.root.type == bfd_link_hash_undefined)
5105 {
5106 eh->elf.root.type = bfd_link_hash_undefweak;
5107 eh->was_undefined = 1;
5108 htab->twiddled_syms = 1;
5109 }
5110 }
5111
5112 return TRUE;
5113}
5114
5115/* Set up opd section info and abiversion for IBFD, and process list
5116 of dot-symbols we made in link_hash_newfunc. */
5117
5118static bfd_boolean
5119ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5120{
5121 struct ppc_link_hash_table *htab;
5122 struct ppc_link_hash_entry **p, *eh;
5123 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5124
5125 if (opd != NULL && opd->size != 0)
5126 {
5127 if (abiversion (ibfd) == 0)
5128 set_abiversion (ibfd, 1);
5129 else if (abiversion (ibfd) >= 2)
5130 {
5131 /* xgettext:c-format */
5132 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5133 " version %d\n"),
5134 ibfd, abiversion (ibfd));
5135 bfd_set_error (bfd_error_bad_value);
5136 return FALSE;
5137 }
5138
5139 if ((ibfd->flags & DYNAMIC) == 0
5140 && (opd->flags & SEC_RELOC) != 0
5141 && opd->reloc_count != 0
5142 && !bfd_is_abs_section (opd->output_section))
5143 {
5144 /* Garbage collection needs some extra help with .opd sections.
5145 We don't want to necessarily keep everything referenced by
5146 relocs in .opd, as that would keep all functions. Instead,
5147 if we reference an .opd symbol (a function descriptor), we
5148 want to keep the function code symbol's section. This is
5149 easy for global symbols, but for local syms we need to keep
5150 information about the associated function section. */
5151 bfd_size_type amt;
5152 asection **opd_sym_map;
5153
5154 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5155 opd_sym_map = bfd_zalloc (ibfd, amt);
5156 if (opd_sym_map == NULL)
5157 return FALSE;
5158 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5159 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5160 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5161 }
5162 }
5163
5164 if (!is_ppc64_elf (info->output_bfd))
5165 return TRUE;
5166 htab = ppc_hash_table (info);
5167 if (htab == NULL)
5168 return FALSE;
5169
5170 /* For input files without an explicit abiversion in e_flags
5171 we should have flagged any with symbol st_other bits set
5172 as ELFv1 and above flagged those with .opd as ELFv2.
5173 Set the output abiversion if not yet set, and for any input
5174 still ambiguous, take its abiversion from the output.
5175 Differences in ABI are reported later. */
5176 if (abiversion (info->output_bfd) == 0)
5177 set_abiversion (info->output_bfd, abiversion (ibfd));
5178 else if (abiversion (ibfd) == 0)
5179 set_abiversion (ibfd, abiversion (info->output_bfd));
5180
5181 p = &htab->dot_syms;
5182 while ((eh = *p) != NULL)
5183 {
5184 *p = NULL;
5185 if (&eh->elf == htab->elf.hgot)
5186 ;
5187 else if (htab->elf.hgot == NULL
5188 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5189 htab->elf.hgot = &eh->elf;
5190 else if (!add_symbol_adjust (eh, info))
5191 return FALSE;
5192 p = &eh->u.next_dot_sym;
5193 }
5194
5195 /* Clear the list for non-ppc64 input files. */
5196 p = &htab->dot_syms;
5197 while ((eh = *p) != NULL)
5198 {
5199 *p = NULL;
5200 p = &eh->u.next_dot_sym;
5201 }
5202
5203 /* We need to fix the undefs list for any syms we have twiddled to
5204 undefweak. */
5205 if (htab->twiddled_syms)
5206 {
5207 bfd_link_repair_undef_list (&htab->elf.root);
5208 htab->twiddled_syms = 0;
5209 }
5210 return TRUE;
5211}
5212
5213/* Undo hash table changes when an --as-needed input file is determined
5214 not to be needed. */
5215
5216static bfd_boolean
5217ppc64_elf_notice_as_needed (bfd *ibfd,
5218 struct bfd_link_info *info,
5219 enum notice_asneeded_action act)
5220{
5221 if (act == notice_not_needed)
5222 {
5223 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5224
5225 if (htab == NULL)
5226 return FALSE;
5227
5228 htab->dot_syms = NULL;
5229 }
5230 return _bfd_elf_notice_as_needed (ibfd, info, act);
5231}
5232
5233/* If --just-symbols against a final linked binary, then assume we need
5234 toc adjusting stubs when calling functions defined there. */
5235
5236static void
5237ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5238{
5239 if ((sec->flags & SEC_CODE) != 0
5240 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5241 && is_ppc64_elf (sec->owner))
5242 {
5243 if (abiversion (sec->owner) >= 2
5244 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5245 sec->has_toc_reloc = 1;
5246 }
5247 _bfd_elf_link_just_syms (sec, info);
5248}
5249
5250static struct plt_entry **
5251update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5252 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5253{
5254 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5255 struct plt_entry **local_plt;
5256 unsigned char *local_got_tls_masks;
5257
5258 if (local_got_ents == NULL)
5259 {
5260 bfd_size_type size = symtab_hdr->sh_info;
5261
5262 size *= (sizeof (*local_got_ents)
5263 + sizeof (*local_plt)
5264 + sizeof (*local_got_tls_masks));
5265 local_got_ents = bfd_zalloc (abfd, size);
5266 if (local_got_ents == NULL)
5267 return NULL;
5268 elf_local_got_ents (abfd) = local_got_ents;
5269 }
5270
5271 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5272 {
5273 struct got_entry *ent;
5274
5275 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5276 if (ent->addend == r_addend
5277 && ent->owner == abfd
5278 && ent->tls_type == tls_type)
5279 break;
5280 if (ent == NULL)
5281 {
5282 bfd_size_type amt = sizeof (*ent);
5283 ent = bfd_alloc (abfd, amt);
5284 if (ent == NULL)
5285 return FALSE;
5286 ent->next = local_got_ents[r_symndx];
5287 ent->addend = r_addend;
5288 ent->owner = abfd;
5289 ent->tls_type = tls_type;
5290 ent->is_indirect = FALSE;
5291 ent->got.refcount = 0;
5292 local_got_ents[r_symndx] = ent;
5293 }
5294 ent->got.refcount += 1;
5295 }
5296
5297 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5298 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5299 local_got_tls_masks[r_symndx] |= tls_type;
5300
5301 return local_plt + r_symndx;
5302}
5303
5304static bfd_boolean
5305update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5306{
5307 struct plt_entry *ent;
5308
5309 for (ent = *plist; ent != NULL; ent = ent->next)
5310 if (ent->addend == addend)
5311 break;
5312 if (ent == NULL)
5313 {
5314 bfd_size_type amt = sizeof (*ent);
5315 ent = bfd_alloc (abfd, amt);
5316 if (ent == NULL)
5317 return FALSE;
5318 ent->next = *plist;
5319 ent->addend = addend;
5320 ent->plt.refcount = 0;
5321 *plist = ent;
5322 }
5323 ent->plt.refcount += 1;
5324 return TRUE;
5325}
5326
5327static bfd_boolean
5328is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5329{
5330 return (r_type == R_PPC64_REL24
5331 || r_type == R_PPC64_REL14
5332 || r_type == R_PPC64_REL14_BRTAKEN
5333 || r_type == R_PPC64_REL14_BRNTAKEN
5334 || r_type == R_PPC64_ADDR24
5335 || r_type == R_PPC64_ADDR14
5336 || r_type == R_PPC64_ADDR14_BRTAKEN
5337 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5338}
5339
5340/* Look through the relocs for a section during the first phase, and
5341 calculate needed space in the global offset table, procedure
5342 linkage table, and dynamic reloc sections. */
5343
5344static bfd_boolean
5345ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5346 asection *sec, const Elf_Internal_Rela *relocs)
5347{
5348 struct ppc_link_hash_table *htab;
5349 Elf_Internal_Shdr *symtab_hdr;
5350 struct elf_link_hash_entry **sym_hashes;
5351 const Elf_Internal_Rela *rel;
5352 const Elf_Internal_Rela *rel_end;
5353 asection *sreloc;
5354 asection **opd_sym_map;
5355 struct elf_link_hash_entry *tga, *dottga;
5356
5357 if (bfd_link_relocatable (info))
5358 return TRUE;
5359
5360 /* Don't do anything special with non-loaded, non-alloced sections.
5361 In particular, any relocs in such sections should not affect GOT
5362 and PLT reference counting (ie. we don't allow them to create GOT
5363 or PLT entries), there's no possibility or desire to optimize TLS
5364 relocs, and there's not much point in propagating relocs to shared
5365 libs that the dynamic linker won't relocate. */
5366 if ((sec->flags & SEC_ALLOC) == 0)
5367 return TRUE;
5368
5369 BFD_ASSERT (is_ppc64_elf (abfd));
5370
5371 htab = ppc_hash_table (info);
5372 if (htab == NULL)
5373 return FALSE;
5374
5375 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5376 FALSE, FALSE, TRUE);
5377 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5378 FALSE, FALSE, TRUE);
5379 symtab_hdr = &elf_symtab_hdr (abfd);
5380 sym_hashes = elf_sym_hashes (abfd);
5381 sreloc = NULL;
5382 opd_sym_map = NULL;
5383 if (ppc64_elf_section_data (sec) != NULL
5384 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5385 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5386
5387 rel_end = relocs + sec->reloc_count;
5388 for (rel = relocs; rel < rel_end; rel++)
5389 {
5390 unsigned long r_symndx;
5391 struct elf_link_hash_entry *h;
5392 enum elf_ppc64_reloc_type r_type;
5393 int tls_type;
5394 struct _ppc64_elf_section_data *ppc64_sec;
5395 struct plt_entry **ifunc, **plt_list;
5396
5397 r_symndx = ELF64_R_SYM (rel->r_info);
5398 if (r_symndx < symtab_hdr->sh_info)
5399 h = NULL;
5400 else
5401 {
5402 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5403 h = elf_follow_link (h);
5404
5405 /* PR15323, ref flags aren't set for references in the same
5406 object. */
5407 h->root.non_ir_ref = 1;
5408
5409 if (h == htab->elf.hgot)
5410 sec->has_toc_reloc = 1;
5411 }
5412
5413 tls_type = 0;
5414 ifunc = NULL;
5415 if (h != NULL)
5416 {
5417 if (h->type == STT_GNU_IFUNC)
5418 {
5419 h->needs_plt = 1;
5420 ifunc = &h->plt.plist;
5421 }
5422 }
5423 else
5424 {
5425 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5426 abfd, r_symndx);
5427 if (isym == NULL)
5428 return FALSE;
5429
5430 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5431 {
5432 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5433 rel->r_addend, PLT_IFUNC);
5434 if (ifunc == NULL)
5435 return FALSE;
5436 }
5437 }
5438
5439 r_type = ELF64_R_TYPE (rel->r_info);
5440 switch (r_type)
5441 {
5442 case R_PPC64_TLSGD:
5443 case R_PPC64_TLSLD:
5444 /* These special tls relocs tie a call to __tls_get_addr with
5445 its parameter symbol. */
5446 break;
5447
5448 case R_PPC64_GOT_TLSLD16:
5449 case R_PPC64_GOT_TLSLD16_LO:
5450 case R_PPC64_GOT_TLSLD16_HI:
5451 case R_PPC64_GOT_TLSLD16_HA:
5452 tls_type = TLS_TLS | TLS_LD;
5453 goto dogottls;
5454
5455 case R_PPC64_GOT_TLSGD16:
5456 case R_PPC64_GOT_TLSGD16_LO:
5457 case R_PPC64_GOT_TLSGD16_HI:
5458 case R_PPC64_GOT_TLSGD16_HA:
5459 tls_type = TLS_TLS | TLS_GD;
5460 goto dogottls;
5461
5462 case R_PPC64_GOT_TPREL16_DS:
5463 case R_PPC64_GOT_TPREL16_LO_DS:
5464 case R_PPC64_GOT_TPREL16_HI:
5465 case R_PPC64_GOT_TPREL16_HA:
5466 if (bfd_link_pic (info))
5467 info->flags |= DF_STATIC_TLS;
5468 tls_type = TLS_TLS | TLS_TPREL;
5469 goto dogottls;
5470
5471 case R_PPC64_GOT_DTPREL16_DS:
5472 case R_PPC64_GOT_DTPREL16_LO_DS:
5473 case R_PPC64_GOT_DTPREL16_HI:
5474 case R_PPC64_GOT_DTPREL16_HA:
5475 tls_type = TLS_TLS | TLS_DTPREL;
5476 dogottls:
5477 sec->has_tls_reloc = 1;
5478 /* Fall through */
5479
5480 case R_PPC64_GOT16:
5481 case R_PPC64_GOT16_DS:
5482 case R_PPC64_GOT16_HA:
5483 case R_PPC64_GOT16_HI:
5484 case R_PPC64_GOT16_LO:
5485 case R_PPC64_GOT16_LO_DS:
5486 /* This symbol requires a global offset table entry. */
5487 sec->has_toc_reloc = 1;
5488 if (r_type == R_PPC64_GOT_TLSLD16
5489 || r_type == R_PPC64_GOT_TLSGD16
5490 || r_type == R_PPC64_GOT_TPREL16_DS
5491 || r_type == R_PPC64_GOT_DTPREL16_DS
5492 || r_type == R_PPC64_GOT16
5493 || r_type == R_PPC64_GOT16_DS)
5494 {
5495 htab->do_multi_toc = 1;
5496 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5497 }
5498
5499 if (ppc64_elf_tdata (abfd)->got == NULL
5500 && !create_got_section (abfd, info))
5501 return FALSE;
5502
5503 if (h != NULL)
5504 {
5505 struct ppc_link_hash_entry *eh;
5506 struct got_entry *ent;
5507
5508 eh = (struct ppc_link_hash_entry *) h;
5509 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5510 if (ent->addend == rel->r_addend
5511 && ent->owner == abfd
5512 && ent->tls_type == tls_type)
5513 break;
5514 if (ent == NULL)
5515 {
5516 bfd_size_type amt = sizeof (*ent);
5517 ent = bfd_alloc (abfd, amt);
5518 if (ent == NULL)
5519 return FALSE;
5520 ent->next = eh->elf.got.glist;
5521 ent->addend = rel->r_addend;
5522 ent->owner = abfd;
5523 ent->tls_type = tls_type;
5524 ent->is_indirect = FALSE;
5525 ent->got.refcount = 0;
5526 eh->elf.got.glist = ent;
5527 }
5528 ent->got.refcount += 1;
5529 eh->tls_mask |= tls_type;
5530 }
5531 else
5532 /* This is a global offset table entry for a local symbol. */
5533 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5534 rel->r_addend, tls_type))
5535 return FALSE;
5536
5537 /* We may also need a plt entry if the symbol turns out to be
5538 an ifunc. */
5539 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5540 {
5541 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5542 return FALSE;
5543 }
5544 break;
5545
5546 case R_PPC64_PLT16_HA:
5547 case R_PPC64_PLT16_HI:
5548 case R_PPC64_PLT16_LO:
5549 case R_PPC64_PLT32:
5550 case R_PPC64_PLT64:
5551 /* This symbol requires a procedure linkage table entry. */
5552 plt_list = ifunc;
5553 if (h != NULL)
5554 {
5555 h->needs_plt = 1;
5556 if (h->root.root.string[0] == '.'
5557 && h->root.root.string[1] != '\0')
5558 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5559 plt_list = &h->plt.plist;
5560 }
5561 if (plt_list == NULL)
5562 {
5563 /* It does not make sense to have a procedure linkage
5564 table entry for a non-ifunc local symbol. */
5565 info->callbacks->einfo
5566 /* xgettext:c-format */
5567 (_("%P: %H: %s reloc against local symbol\n"),
5568 abfd, sec, rel->r_offset,
5569 ppc64_elf_howto_table[r_type]->name);
5570 bfd_set_error (bfd_error_bad_value);
5571 return FALSE;
5572 }
5573 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5574 return FALSE;
5575 break;
5576
5577 /* The following relocations don't need to propagate the
5578 relocation if linking a shared object since they are
5579 section relative. */
5580 case R_PPC64_SECTOFF:
5581 case R_PPC64_SECTOFF_LO:
5582 case R_PPC64_SECTOFF_HI:
5583 case R_PPC64_SECTOFF_HA:
5584 case R_PPC64_SECTOFF_DS:
5585 case R_PPC64_SECTOFF_LO_DS:
5586 case R_PPC64_DTPREL16:
5587 case R_PPC64_DTPREL16_LO:
5588 case R_PPC64_DTPREL16_HI:
5589 case R_PPC64_DTPREL16_HA:
5590 case R_PPC64_DTPREL16_DS:
5591 case R_PPC64_DTPREL16_LO_DS:
5592 case R_PPC64_DTPREL16_HIGH:
5593 case R_PPC64_DTPREL16_HIGHA:
5594 case R_PPC64_DTPREL16_HIGHER:
5595 case R_PPC64_DTPREL16_HIGHERA:
5596 case R_PPC64_DTPREL16_HIGHEST:
5597 case R_PPC64_DTPREL16_HIGHESTA:
5598 break;
5599
5600 /* Nor do these. */
5601 case R_PPC64_REL16:
5602 case R_PPC64_REL16_LO:
5603 case R_PPC64_REL16_HI:
5604 case R_PPC64_REL16_HA:
5605 case R_PPC64_REL16DX_HA:
5606 break;
5607
5608 /* Not supported as a dynamic relocation. */
5609 case R_PPC64_ADDR64_LOCAL:
5610 if (bfd_link_pic (info))
5611 {
5612 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5613 ppc_howto_init ();
5614 /* xgettext:c-format */
5615 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5616 "in shared libraries and PIEs.\n"),
5617 abfd, sec, rel->r_offset,
5618 ppc64_elf_howto_table[r_type]->name);
5619 bfd_set_error (bfd_error_bad_value);
5620 return FALSE;
5621 }
5622 break;
5623
5624 case R_PPC64_TOC16:
5625 case R_PPC64_TOC16_DS:
5626 htab->do_multi_toc = 1;
5627 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5628 /* Fall through. */
5629 case R_PPC64_TOC16_LO:
5630 case R_PPC64_TOC16_HI:
5631 case R_PPC64_TOC16_HA:
5632 case R_PPC64_TOC16_LO_DS:
5633 sec->has_toc_reloc = 1;
5634 break;
5635
5636 /* Marker reloc. */
5637 case R_PPC64_ENTRY:
5638 break;
5639
5640 /* This relocation describes the C++ object vtable hierarchy.
5641 Reconstruct it for later use during GC. */
5642 case R_PPC64_GNU_VTINHERIT:
5643 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5644 return FALSE;
5645 break;
5646
5647 /* This relocation describes which C++ vtable entries are actually
5648 used. Record for later use during GC. */
5649 case R_PPC64_GNU_VTENTRY:
5650 BFD_ASSERT (h != NULL);
5651 if (h != NULL
5652 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5653 return FALSE;
5654 break;
5655
5656 case R_PPC64_REL14:
5657 case R_PPC64_REL14_BRTAKEN:
5658 case R_PPC64_REL14_BRNTAKEN:
5659 {
5660 asection *dest = NULL;
5661
5662 /* Heuristic: If jumping outside our section, chances are
5663 we are going to need a stub. */
5664 if (h != NULL)
5665 {
5666 /* If the sym is weak it may be overridden later, so
5667 don't assume we know where a weak sym lives. */
5668 if (h->root.type == bfd_link_hash_defined)
5669 dest = h->root.u.def.section;
5670 }
5671 else
5672 {
5673 Elf_Internal_Sym *isym;
5674
5675 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5676 abfd, r_symndx);
5677 if (isym == NULL)
5678 return FALSE;
5679
5680 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5681 }
5682
5683 if (dest != sec)
5684 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5685 }
5686 /* Fall through. */
5687
5688 case R_PPC64_REL24:
5689 plt_list = ifunc;
5690 if (h != NULL)
5691 {
5692 h->needs_plt = 1;
5693 if (h->root.root.string[0] == '.'
5694 && h->root.root.string[1] != '\0')
5695 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5696
5697 if (h == tga || h == dottga)
5698 {
5699 sec->has_tls_reloc = 1;
5700 if (rel != relocs
5701 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5702 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5703 /* We have a new-style __tls_get_addr call with
5704 a marker reloc. */
5705 ;
5706 else
5707 /* Mark this section as having an old-style call. */
5708 sec->has_tls_get_addr_call = 1;
5709 }
5710 plt_list = &h->plt.plist;
5711 }
5712
5713 /* We may need a .plt entry if the function this reloc
5714 refers to is in a shared lib. */
5715 if (plt_list
5716 && !update_plt_info (abfd, plt_list, rel->r_addend))
5717 return FALSE;
5718 break;
5719
5720 case R_PPC64_ADDR14:
5721 case R_PPC64_ADDR14_BRNTAKEN:
5722 case R_PPC64_ADDR14_BRTAKEN:
5723 case R_PPC64_ADDR24:
5724 goto dodyn;
5725
5726 case R_PPC64_TPREL64:
5727 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5728 if (bfd_link_pic (info))
5729 info->flags |= DF_STATIC_TLS;
5730 goto dotlstoc;
5731
5732 case R_PPC64_DTPMOD64:
5733 if (rel + 1 < rel_end
5734 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5735 && rel[1].r_offset == rel->r_offset + 8)
5736 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5737 else
5738 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5739 goto dotlstoc;
5740
5741 case R_PPC64_DTPREL64:
5742 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5743 if (rel != relocs
5744 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5745 && rel[-1].r_offset == rel->r_offset - 8)
5746 /* This is the second reloc of a dtpmod, dtprel pair.
5747 Don't mark with TLS_DTPREL. */
5748 goto dodyn;
5749
5750 dotlstoc:
5751 sec->has_tls_reloc = 1;
5752 if (h != NULL)
5753 {
5754 struct ppc_link_hash_entry *eh;
5755 eh = (struct ppc_link_hash_entry *) h;
5756 eh->tls_mask |= tls_type;
5757 }
5758 else
5759 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5760 rel->r_addend, tls_type))
5761 return FALSE;
5762
5763 ppc64_sec = ppc64_elf_section_data (sec);
5764 if (ppc64_sec->sec_type != sec_toc)
5765 {
5766 bfd_size_type amt;
5767
5768 /* One extra to simplify get_tls_mask. */
5769 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5770 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5771 if (ppc64_sec->u.toc.symndx == NULL)
5772 return FALSE;
5773 amt = sec->size * sizeof (bfd_vma) / 8;
5774 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5775 if (ppc64_sec->u.toc.add == NULL)
5776 return FALSE;
5777 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5778 ppc64_sec->sec_type = sec_toc;
5779 }
5780 BFD_ASSERT (rel->r_offset % 8 == 0);
5781 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5782 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5783
5784 /* Mark the second slot of a GD or LD entry.
5785 -1 to indicate GD and -2 to indicate LD. */
5786 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5787 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5788 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5789 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5790 goto dodyn;
5791
5792 case R_PPC64_TPREL16:
5793 case R_PPC64_TPREL16_LO:
5794 case R_PPC64_TPREL16_HI:
5795 case R_PPC64_TPREL16_HA:
5796 case R_PPC64_TPREL16_DS:
5797 case R_PPC64_TPREL16_LO_DS:
5798 case R_PPC64_TPREL16_HIGH:
5799 case R_PPC64_TPREL16_HIGHA:
5800 case R_PPC64_TPREL16_HIGHER:
5801 case R_PPC64_TPREL16_HIGHERA:
5802 case R_PPC64_TPREL16_HIGHEST:
5803 case R_PPC64_TPREL16_HIGHESTA:
5804 if (bfd_link_pic (info))
5805 {
5806 info->flags |= DF_STATIC_TLS;
5807 goto dodyn;
5808 }
5809 break;
5810
5811 case R_PPC64_ADDR64:
5812 if (opd_sym_map != NULL
5813 && rel + 1 < rel_end
5814 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5815 {
5816 if (h != NULL)
5817 {
5818 if (h->root.root.string[0] == '.'
5819 && h->root.root.string[1] != 0
5820 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5821 ;
5822 else
5823 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5824 }
5825 else
5826 {
5827 asection *s;
5828 Elf_Internal_Sym *isym;
5829
5830 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5831 abfd, r_symndx);
5832 if (isym == NULL)
5833 return FALSE;
5834
5835 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5836 if (s != NULL && s != sec)
5837 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5838 }
5839 }
5840 /* Fall through. */
5841
5842 case R_PPC64_ADDR16:
5843 case R_PPC64_ADDR16_DS:
5844 case R_PPC64_ADDR16_HA:
5845 case R_PPC64_ADDR16_HI:
5846 case R_PPC64_ADDR16_HIGH:
5847 case R_PPC64_ADDR16_HIGHA:
5848 case R_PPC64_ADDR16_HIGHER:
5849 case R_PPC64_ADDR16_HIGHERA:
5850 case R_PPC64_ADDR16_HIGHEST:
5851 case R_PPC64_ADDR16_HIGHESTA:
5852 case R_PPC64_ADDR16_LO:
5853 case R_PPC64_ADDR16_LO_DS:
5854 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5855 && rel->r_addend == 0)
5856 {
5857 /* We may need a .plt entry if this reloc refers to a
5858 function in a shared lib. */
5859 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5860 return FALSE;
5861 h->pointer_equality_needed = 1;
5862 }
5863 /* Fall through. */
5864
5865 case R_PPC64_REL30:
5866 case R_PPC64_REL32:
5867 case R_PPC64_REL64:
5868 case R_PPC64_ADDR32:
5869 case R_PPC64_UADDR16:
5870 case R_PPC64_UADDR32:
5871 case R_PPC64_UADDR64:
5872 case R_PPC64_TOC:
5873 if (h != NULL && !bfd_link_pic (info))
5874 /* We may need a copy reloc. */
5875 h->non_got_ref = 1;
5876
5877 /* Don't propagate .opd relocs. */
5878 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5879 break;
5880
5881 /* If we are creating a shared library, and this is a reloc
5882 against a global symbol, or a non PC relative reloc
5883 against a local symbol, then we need to copy the reloc
5884 into the shared library. However, if we are linking with
5885 -Bsymbolic, we do not need to copy a reloc against a
5886 global symbol which is defined in an object we are
5887 including in the link (i.e., DEF_REGULAR is set). At
5888 this point we have not seen all the input files, so it is
5889 possible that DEF_REGULAR is not set now but will be set
5890 later (it is never cleared). In case of a weak definition,
5891 DEF_REGULAR may be cleared later by a strong definition in
5892 a shared library. We account for that possibility below by
5893 storing information in the dyn_relocs field of the hash
5894 table entry. A similar situation occurs when creating
5895 shared libraries and symbol visibility changes render the
5896 symbol local.
5897
5898 If on the other hand, we are creating an executable, we
5899 may need to keep relocations for symbols satisfied by a
5900 dynamic library if we manage to avoid copy relocs for the
5901 symbol. */
5902 dodyn:
5903 if ((bfd_link_pic (info)
5904 && (must_be_dyn_reloc (info, r_type)
5905 || (h != NULL
5906 && (!SYMBOLIC_BIND (info, h)
5907 || h->root.type == bfd_link_hash_defweak
5908 || !h->def_regular))))
5909 || (ELIMINATE_COPY_RELOCS
5910 && !bfd_link_pic (info)
5911 && h != NULL
5912 && (h->root.type == bfd_link_hash_defweak
5913 || !h->def_regular))
5914 || (!bfd_link_pic (info)
5915 && ifunc != NULL))
5916 {
5917 /* We must copy these reloc types into the output file.
5918 Create a reloc section in dynobj and make room for
5919 this reloc. */
5920 if (sreloc == NULL)
5921 {
5922 sreloc = _bfd_elf_make_dynamic_reloc_section
5923 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5924
5925 if (sreloc == NULL)
5926 return FALSE;
5927 }
5928
5929 /* If this is a global symbol, we count the number of
5930 relocations we need for this symbol. */
5931 if (h != NULL)
5932 {
5933 struct elf_dyn_relocs *p;
5934 struct elf_dyn_relocs **head;
5935
5936 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5937 p = *head;
5938 if (p == NULL || p->sec != sec)
5939 {
5940 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5941 if (p == NULL)
5942 return FALSE;
5943 p->next = *head;
5944 *head = p;
5945 p->sec = sec;
5946 p->count = 0;
5947 p->pc_count = 0;
5948 }
5949 p->count += 1;
5950 if (!must_be_dyn_reloc (info, r_type))
5951 p->pc_count += 1;
5952 }
5953 else
5954 {
5955 /* Track dynamic relocs needed for local syms too.
5956 We really need local syms available to do this
5957 easily. Oh well. */
5958 struct ppc_dyn_relocs *p;
5959 struct ppc_dyn_relocs **head;
5960 bfd_boolean is_ifunc;
5961 asection *s;
5962 void *vpp;
5963 Elf_Internal_Sym *isym;
5964
5965 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5966 abfd, r_symndx);
5967 if (isym == NULL)
5968 return FALSE;
5969
5970 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5971 if (s == NULL)
5972 s = sec;
5973
5974 vpp = &elf_section_data (s)->local_dynrel;
5975 head = (struct ppc_dyn_relocs **) vpp;
5976 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5977 p = *head;
5978 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5979 p = p->next;
5980 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5981 {
5982 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5983 if (p == NULL)
5984 return FALSE;
5985 p->next = *head;
5986 *head = p;
5987 p->sec = sec;
5988 p->ifunc = is_ifunc;
5989 p->count = 0;
5990 }
5991 p->count += 1;
5992 }
5993 }
5994 break;
5995
5996 default:
5997 break;
5998 }
5999 }
6000
6001 return TRUE;
6002}
6003
6004/* Merge backend specific data from an object file to the output
6005 object file when linking. */
6006
6007static bfd_boolean
6008ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6009{
6010 bfd *obfd = info->output_bfd;
6011 unsigned long iflags, oflags;
6012
6013 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6014 return TRUE;
6015
6016 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6017 return TRUE;
6018
6019 if (!_bfd_generic_verify_endian_match (ibfd, info))
6020 return FALSE;
6021
6022 iflags = elf_elfheader (ibfd)->e_flags;
6023 oflags = elf_elfheader (obfd)->e_flags;
6024
6025 if (iflags & ~EF_PPC64_ABI)
6026 {
6027 _bfd_error_handler
6028 /* xgettext:c-format */
6029 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6030 bfd_set_error (bfd_error_bad_value);
6031 return FALSE;
6032 }
6033 else if (iflags != oflags && iflags != 0)
6034 {
6035 _bfd_error_handler
6036 /* xgettext:c-format */
6037 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6038 ibfd, iflags, oflags);
6039 bfd_set_error (bfd_error_bad_value);
6040 return FALSE;
6041 }
6042
6043 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6044
6045 /* Merge Tag_compatibility attributes and any common GNU ones. */
6046 _bfd_elf_merge_object_attributes (ibfd, info);
6047
6048 return TRUE;
6049}
6050
6051static bfd_boolean
6052ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6053{
6054 /* Print normal ELF private data. */
6055 _bfd_elf_print_private_bfd_data (abfd, ptr);
6056
6057 if (elf_elfheader (abfd)->e_flags != 0)
6058 {
6059 FILE *file = ptr;
6060
6061 fprintf (file, _("private flags = 0x%lx:"),
6062 elf_elfheader (abfd)->e_flags);
6063
6064 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6065 fprintf (file, _(" [abiv%ld]"),
6066 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6067 fputc ('\n', file);
6068 }
6069
6070 return TRUE;
6071}
6072
6073/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6074 of the code entry point, and its section, which must be in the same
6075 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6076
6077static bfd_vma
6078opd_entry_value (asection *opd_sec,
6079 bfd_vma offset,
6080 asection **code_sec,
6081 bfd_vma *code_off,
6082 bfd_boolean in_code_sec)
6083{
6084 bfd *opd_bfd = opd_sec->owner;
6085 Elf_Internal_Rela *relocs;
6086 Elf_Internal_Rela *lo, *hi, *look;
6087 bfd_vma val;
6088
6089 /* No relocs implies we are linking a --just-symbols object, or looking
6090 at a final linked executable with addr2line or somesuch. */
6091 if (opd_sec->reloc_count == 0)
6092 {
6093 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6094
6095 if (contents == NULL)
6096 {
6097 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6098 return (bfd_vma) -1;
6099 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6100 }
6101
6102 /* PR 17512: file: 64b9dfbb. */
6103 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6104 return (bfd_vma) -1;
6105
6106 val = bfd_get_64 (opd_bfd, contents + offset);
6107 if (code_sec != NULL)
6108 {
6109 asection *sec, *likely = NULL;
6110
6111 if (in_code_sec)
6112 {
6113 sec = *code_sec;
6114 if (sec->vma <= val
6115 && val < sec->vma + sec->size)
6116 likely = sec;
6117 else
6118 val = -1;
6119 }
6120 else
6121 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6122 if (sec->vma <= val
6123 && (sec->flags & SEC_LOAD) != 0
6124 && (sec->flags & SEC_ALLOC) != 0)
6125 likely = sec;
6126 if (likely != NULL)
6127 {
6128 *code_sec = likely;
6129 if (code_off != NULL)
6130 *code_off = val - likely->vma;
6131 }
6132 }
6133 return val;
6134 }
6135
6136 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6137
6138 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6139 if (relocs == NULL)
6140 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6141 /* PR 17512: file: df8e1fd6. */
6142 if (relocs == NULL)
6143 return (bfd_vma) -1;
6144
6145 /* Go find the opd reloc at the sym address. */
6146 lo = relocs;
6147 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6148 val = (bfd_vma) -1;
6149 while (lo < hi)
6150 {
6151 look = lo + (hi - lo) / 2;
6152 if (look->r_offset < offset)
6153 lo = look + 1;
6154 else if (look->r_offset > offset)
6155 hi = look;
6156 else
6157 {
6158 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6159
6160 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6161 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6162 {
6163 unsigned long symndx = ELF64_R_SYM (look->r_info);
6164 asection *sec = NULL;
6165
6166 if (symndx >= symtab_hdr->sh_info
6167 && elf_sym_hashes (opd_bfd) != NULL)
6168 {
6169 struct elf_link_hash_entry **sym_hashes;
6170 struct elf_link_hash_entry *rh;
6171
6172 sym_hashes = elf_sym_hashes (opd_bfd);
6173 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6174 if (rh != NULL)
6175 {
6176 rh = elf_follow_link (rh);
6177 if (rh->root.type != bfd_link_hash_defined
6178 && rh->root.type != bfd_link_hash_defweak)
6179 break;
6180 if (rh->root.u.def.section->owner == opd_bfd)
6181 {
6182 val = rh->root.u.def.value;
6183 sec = rh->root.u.def.section;
6184 }
6185 }
6186 }
6187
6188 if (sec == NULL)
6189 {
6190 Elf_Internal_Sym *sym;
6191
6192 if (symndx < symtab_hdr->sh_info)
6193 {
6194 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6195 if (sym == NULL)
6196 {
6197 size_t symcnt = symtab_hdr->sh_info;
6198 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6199 symcnt, 0,
6200 NULL, NULL, NULL);
6201 if (sym == NULL)
6202 break;
6203 symtab_hdr->contents = (bfd_byte *) sym;
6204 }
6205 sym += symndx;
6206 }
6207 else
6208 {
6209 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6210 1, symndx,
6211 NULL, NULL, NULL);
6212 if (sym == NULL)
6213 break;
6214 }
6215 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6216 if (sec == NULL)
6217 break;
6218 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6219 val = sym->st_value;
6220 }
6221
6222 val += look->r_addend;
6223 if (code_off != NULL)
6224 *code_off = val;
6225 if (code_sec != NULL)
6226 {
6227 if (in_code_sec && *code_sec != sec)
6228 return -1;
6229 else
6230 *code_sec = sec;
6231 }
6232 if (sec->output_section != NULL)
6233 val += sec->output_section->vma + sec->output_offset;
6234 }
6235 break;
6236 }
6237 }
6238
6239 return val;
6240}
6241
6242/* If the ELF symbol SYM might be a function in SEC, return the
6243 function size and set *CODE_OFF to the function's entry point,
6244 otherwise return zero. */
6245
6246static bfd_size_type
6247ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6248 bfd_vma *code_off)
6249{
6250 bfd_size_type size;
6251
6252 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6253 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6254 return 0;
6255
6256 size = 0;
6257 if (!(sym->flags & BSF_SYNTHETIC))
6258 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6259
6260 if (strcmp (sym->section->name, ".opd") == 0)
6261 {
6262 struct _opd_sec_data *opd = get_opd_info (sym->section);
6263 bfd_vma symval = sym->value;
6264
6265 if (opd != NULL
6266 && opd->adjust != NULL
6267 && elf_section_data (sym->section)->relocs != NULL)
6268 {
6269 /* opd_entry_value will use cached relocs that have been
6270 adjusted, but with raw symbols. That means both local
6271 and global symbols need adjusting. */
6272 long adjust = opd->adjust[OPD_NDX (symval)];
6273 if (adjust == -1)
6274 return 0;
6275 symval += adjust;
6276 }
6277
6278 if (opd_entry_value (sym->section, symval,
6279 &sec, code_off, TRUE) == (bfd_vma) -1)
6280 return 0;
6281 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6282 symbol. This size has nothing to do with the code size of the
6283 function, which is what we're supposed to return, but the
6284 code size isn't available without looking up the dot-sym.
6285 However, doing that would be a waste of time particularly
6286 since elf_find_function will look at the dot-sym anyway.
6287 Now, elf_find_function will keep the largest size of any
6288 function sym found at the code address of interest, so return
6289 1 here to avoid it incorrectly caching a larger function size
6290 for a small function. This does mean we return the wrong
6291 size for a new-ABI function of size 24, but all that does is
6292 disable caching for such functions. */
6293 if (size == 24)
6294 size = 1;
6295 }
6296 else
6297 {
6298 if (sym->section != sec)
6299 return 0;
6300 *code_off = sym->value;
6301 }
6302 if (size == 0)
6303 size = 1;
6304 return size;
6305}
6306
6307/* Return true if symbol is defined in a regular object file. */
6308
6309static bfd_boolean
6310is_static_defined (struct elf_link_hash_entry *h)
6311{
6312 return ((h->root.type == bfd_link_hash_defined
6313 || h->root.type == bfd_link_hash_defweak)
6314 && h->root.u.def.section != NULL
6315 && h->root.u.def.section->output_section != NULL);
6316}
6317
6318/* If FDH is a function descriptor symbol, return the associated code
6319 entry symbol if it is defined. Return NULL otherwise. */
6320
6321static struct ppc_link_hash_entry *
6322defined_code_entry (struct ppc_link_hash_entry *fdh)
6323{
6324 if (fdh->is_func_descriptor)
6325 {
6326 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6327 if (fh->elf.root.type == bfd_link_hash_defined
6328 || fh->elf.root.type == bfd_link_hash_defweak)
6329 return fh;
6330 }
6331 return NULL;
6332}
6333
6334/* If FH is a function code entry symbol, return the associated
6335 function descriptor symbol if it is defined. Return NULL otherwise. */
6336
6337static struct ppc_link_hash_entry *
6338defined_func_desc (struct ppc_link_hash_entry *fh)
6339{
6340 if (fh->oh != NULL
6341 && fh->oh->is_func_descriptor)
6342 {
6343 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6344 if (fdh->elf.root.type == bfd_link_hash_defined
6345 || fdh->elf.root.type == bfd_link_hash_defweak)
6346 return fdh;
6347 }
6348 return NULL;
6349}
6350
6351/* Mark all our entry sym sections, both opd and code section. */
6352
6353static void
6354ppc64_elf_gc_keep (struct bfd_link_info *info)
6355{
6356 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6357 struct bfd_sym_chain *sym;
6358
6359 if (htab == NULL)
6360 return;
6361
6362 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6363 {
6364 struct ppc_link_hash_entry *eh, *fh;
6365 asection *sec;
6366
6367 eh = (struct ppc_link_hash_entry *)
6368 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6369 if (eh == NULL)
6370 continue;
6371 if (eh->elf.root.type != bfd_link_hash_defined
6372 && eh->elf.root.type != bfd_link_hash_defweak)
6373 continue;
6374
6375 fh = defined_code_entry (eh);
6376 if (fh != NULL)
6377 {
6378 sec = fh->elf.root.u.def.section;
6379 sec->flags |= SEC_KEEP;
6380 }
6381 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6382 && opd_entry_value (eh->elf.root.u.def.section,
6383 eh->elf.root.u.def.value,
6384 &sec, NULL, FALSE) != (bfd_vma) -1)
6385 sec->flags |= SEC_KEEP;
6386
6387 sec = eh->elf.root.u.def.section;
6388 sec->flags |= SEC_KEEP;
6389 }
6390}
6391
6392/* Mark sections containing dynamically referenced symbols. When
6393 building shared libraries, we must assume that any visible symbol is
6394 referenced. */
6395
6396static bfd_boolean
6397ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6398{
6399 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6400 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6401 struct ppc_link_hash_entry *fdh;
6402 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6403
6404 /* Dynamic linking info is on the func descriptor sym. */
6405 fdh = defined_func_desc (eh);
6406 if (fdh != NULL)
6407 eh = fdh;
6408
6409 if ((eh->elf.root.type == bfd_link_hash_defined
6410 || eh->elf.root.type == bfd_link_hash_defweak)
6411 && (eh->elf.ref_dynamic
6412 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6413 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6414 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6415 && (!bfd_link_executable (info)
6416 || info->export_dynamic
6417 || (eh->elf.dynamic
6418 && d != NULL
6419 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6420 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6421 || !bfd_hide_sym_by_version (info->version_info,
6422 eh->elf.root.root.string)))))
6423 {
6424 asection *code_sec;
6425 struct ppc_link_hash_entry *fh;
6426
6427 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6428
6429 /* Function descriptor syms cause the associated
6430 function code sym section to be marked. */
6431 fh = defined_code_entry (eh);
6432 if (fh != NULL)
6433 {
6434 code_sec = fh->elf.root.u.def.section;
6435 code_sec->flags |= SEC_KEEP;
6436 }
6437 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6438 && opd_entry_value (eh->elf.root.u.def.section,
6439 eh->elf.root.u.def.value,
6440 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6441 code_sec->flags |= SEC_KEEP;
6442 }
6443
6444 return TRUE;
6445}
6446
6447/* Return the section that should be marked against GC for a given
6448 relocation. */
6449
6450static asection *
6451ppc64_elf_gc_mark_hook (asection *sec,
6452 struct bfd_link_info *info,
6453 Elf_Internal_Rela *rel,
6454 struct elf_link_hash_entry *h,
6455 Elf_Internal_Sym *sym)
6456{
6457 asection *rsec;
6458
6459 /* Syms return NULL if we're marking .opd, so we avoid marking all
6460 function sections, as all functions are referenced in .opd. */
6461 rsec = NULL;
6462 if (get_opd_info (sec) != NULL)
6463 return rsec;
6464
6465 if (h != NULL)
6466 {
6467 enum elf_ppc64_reloc_type r_type;
6468 struct ppc_link_hash_entry *eh, *fh, *fdh;
6469
6470 r_type = ELF64_R_TYPE (rel->r_info);
6471 switch (r_type)
6472 {
6473 case R_PPC64_GNU_VTINHERIT:
6474 case R_PPC64_GNU_VTENTRY:
6475 break;
6476
6477 default:
6478 switch (h->root.type)
6479 {
6480 case bfd_link_hash_defined:
6481 case bfd_link_hash_defweak:
6482 eh = (struct ppc_link_hash_entry *) h;
6483 fdh = defined_func_desc (eh);
6484 if (fdh != NULL)
6485 eh = fdh;
6486
6487 /* Function descriptor syms cause the associated
6488 function code sym section to be marked. */
6489 fh = defined_code_entry (eh);
6490 if (fh != NULL)
6491 {
6492 /* They also mark their opd section. */
6493 eh->elf.root.u.def.section->gc_mark = 1;
6494
6495 rsec = fh->elf.root.u.def.section;
6496 }
6497 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6498 && opd_entry_value (eh->elf.root.u.def.section,
6499 eh->elf.root.u.def.value,
6500 &rsec, NULL, FALSE) != (bfd_vma) -1)
6501 eh->elf.root.u.def.section->gc_mark = 1;
6502 else
6503 rsec = h->root.u.def.section;
6504 break;
6505
6506 case bfd_link_hash_common:
6507 rsec = h->root.u.c.p->section;
6508 break;
6509
6510 default:
6511 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6512 }
6513 }
6514 }
6515 else
6516 {
6517 struct _opd_sec_data *opd;
6518
6519 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6520 opd = get_opd_info (rsec);
6521 if (opd != NULL && opd->func_sec != NULL)
6522 {
6523 rsec->gc_mark = 1;
6524
6525 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6526 }
6527 }
6528
6529 return rsec;
6530}
6531
6532/* Update the .got, .plt. and dynamic reloc reference counts for the
6533 section being removed. */
6534
6535static bfd_boolean
6536ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6537 asection *sec, const Elf_Internal_Rela *relocs)
6538{
6539 struct ppc_link_hash_table *htab;
6540 Elf_Internal_Shdr *symtab_hdr;
6541 struct elf_link_hash_entry **sym_hashes;
6542 struct got_entry **local_got_ents;
6543 const Elf_Internal_Rela *rel, *relend;
6544
6545 if (bfd_link_relocatable (info))
6546 return TRUE;
6547
6548 if ((sec->flags & SEC_ALLOC) == 0)
6549 return TRUE;
6550
6551 elf_section_data (sec)->local_dynrel = NULL;
6552
6553 htab = ppc_hash_table (info);
6554 if (htab == NULL)
6555 return FALSE;
6556
6557 symtab_hdr = &elf_symtab_hdr (abfd);
6558 sym_hashes = elf_sym_hashes (abfd);
6559 local_got_ents = elf_local_got_ents (abfd);
6560
6561 relend = relocs + sec->reloc_count;
6562 for (rel = relocs; rel < relend; rel++)
6563 {
6564 unsigned long r_symndx;
6565 enum elf_ppc64_reloc_type r_type;
6566 struct elf_link_hash_entry *h = NULL;
6567 struct plt_entry **plt_list;
6568 unsigned char tls_type = 0;
6569
6570 r_symndx = ELF64_R_SYM (rel->r_info);
6571 r_type = ELF64_R_TYPE (rel->r_info);
6572 if (r_symndx >= symtab_hdr->sh_info)
6573 {
6574 struct ppc_link_hash_entry *eh;
6575 struct elf_dyn_relocs **pp;
6576 struct elf_dyn_relocs *p;
6577
6578 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6579 h = elf_follow_link (h);
6580 eh = (struct ppc_link_hash_entry *) h;
6581
6582 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6583 if (p->sec == sec)
6584 {
6585 /* Everything must go for SEC. */
6586 *pp = p->next;
6587 break;
6588 }
6589 }
6590
6591 switch (r_type)
6592 {
6593 case R_PPC64_GOT_TLSLD16:
6594 case R_PPC64_GOT_TLSLD16_LO:
6595 case R_PPC64_GOT_TLSLD16_HI:
6596 case R_PPC64_GOT_TLSLD16_HA:
6597 tls_type = TLS_TLS | TLS_LD;
6598 goto dogot;
6599
6600 case R_PPC64_GOT_TLSGD16:
6601 case R_PPC64_GOT_TLSGD16_LO:
6602 case R_PPC64_GOT_TLSGD16_HI:
6603 case R_PPC64_GOT_TLSGD16_HA:
6604 tls_type = TLS_TLS | TLS_GD;
6605 goto dogot;
6606
6607 case R_PPC64_GOT_TPREL16_DS:
6608 case R_PPC64_GOT_TPREL16_LO_DS:
6609 case R_PPC64_GOT_TPREL16_HI:
6610 case R_PPC64_GOT_TPREL16_HA:
6611 tls_type = TLS_TLS | TLS_TPREL;
6612 goto dogot;
6613
6614 case R_PPC64_GOT_DTPREL16_DS:
6615 case R_PPC64_GOT_DTPREL16_LO_DS:
6616 case R_PPC64_GOT_DTPREL16_HI:
6617 case R_PPC64_GOT_DTPREL16_HA:
6618 tls_type = TLS_TLS | TLS_DTPREL;
6619 goto dogot;
6620
6621 case R_PPC64_GOT16:
6622 case R_PPC64_GOT16_DS:
6623 case R_PPC64_GOT16_HA:
6624 case R_PPC64_GOT16_HI:
6625 case R_PPC64_GOT16_LO:
6626 case R_PPC64_GOT16_LO_DS:
6627 dogot:
6628 {
6629 struct got_entry *ent;
6630
6631 if (h != NULL)
6632 ent = h->got.glist;
6633 else
6634 ent = local_got_ents[r_symndx];
6635
6636 for (; ent != NULL; ent = ent->next)
6637 if (ent->addend == rel->r_addend
6638 && ent->owner == abfd
6639 && ent->tls_type == tls_type)
6640 break;
6641 if (ent == NULL)
6642 abort ();
6643 if (ent->got.refcount > 0)
6644 ent->got.refcount -= 1;
6645 }
6646 break;
6647
6648 case R_PPC64_PLT16_HA:
6649 case R_PPC64_PLT16_HI:
6650 case R_PPC64_PLT16_LO:
6651 case R_PPC64_PLT32:
6652 case R_PPC64_PLT64:
6653 case R_PPC64_REL14:
6654 case R_PPC64_REL14_BRNTAKEN:
6655 case R_PPC64_REL14_BRTAKEN:
6656 case R_PPC64_REL24:
6657 plt_list = NULL;
6658 if (h != NULL)
6659 plt_list = &h->plt.plist;
6660 else if (local_got_ents != NULL)
6661 {
6662 struct plt_entry **local_plt = (struct plt_entry **)
6663 (local_got_ents + symtab_hdr->sh_info);
6664 unsigned char *local_got_tls_masks = (unsigned char *)
6665 (local_plt + symtab_hdr->sh_info);
6666 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6667 plt_list = local_plt + r_symndx;
6668 }
6669 if (plt_list)
6670 {
6671 struct plt_entry *ent;
6672
6673 for (ent = *plt_list; ent != NULL; ent = ent->next)
6674 if (ent->addend == rel->r_addend)
6675 break;
6676 if (ent != NULL && ent->plt.refcount > 0)
6677 ent->plt.refcount -= 1;
6678 }
6679 break;
6680
6681 default:
6682 break;
6683 }
6684 }
6685 return TRUE;
6686}
6687
6688/* The maximum size of .sfpr. */
6689#define SFPR_MAX (218*4)
6690
6691struct sfpr_def_parms
6692{
6693 const char name[12];
6694 unsigned char lo, hi;
6695 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6696 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6697};
6698
6699/* Auto-generate _save*, _rest* functions in .sfpr.
6700 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6701 instead. */
6702
6703static bfd_boolean
6704sfpr_define (struct bfd_link_info *info,
6705 const struct sfpr_def_parms *parm,
6706 asection *stub_sec)
6707{
6708 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6709 unsigned int i;
6710 size_t len = strlen (parm->name);
6711 bfd_boolean writing = FALSE;
6712 char sym[16];
6713
6714 if (htab == NULL)
6715 return FALSE;
6716
6717 memcpy (sym, parm->name, len);
6718 sym[len + 2] = 0;
6719
6720 for (i = parm->lo; i <= parm->hi; i++)
6721 {
6722 struct ppc_link_hash_entry *h;
6723
6724 sym[len + 0] = i / 10 + '0';
6725 sym[len + 1] = i % 10 + '0';
6726 h = (struct ppc_link_hash_entry *)
6727 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6728 if (stub_sec != NULL)
6729 {
6730 if (h != NULL
6731 && h->elf.root.type == bfd_link_hash_defined
6732 && h->elf.root.u.def.section == htab->sfpr)
6733 {
6734 struct elf_link_hash_entry *s;
6735 char buf[32];
6736 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6737 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6738 if (s == NULL)
6739 return FALSE;
6740 if (s->root.type == bfd_link_hash_new
6741 || (s->root.type = bfd_link_hash_defined
6742 && s->root.u.def.section == stub_sec))
6743 {
6744 s->root.type = bfd_link_hash_defined;
6745 s->root.u.def.section = stub_sec;
6746 s->root.u.def.value = (stub_sec->size
6747 + h->elf.root.u.def.value);
6748 s->ref_regular = 1;
6749 s->def_regular = 1;
6750 s->ref_regular_nonweak = 1;
6751 s->forced_local = 1;
6752 s->non_elf = 0;
6753 s->root.linker_def = 1;
6754 }
6755 }
6756 continue;
6757 }
6758 if (h != NULL)
6759 {
6760 h->save_res = 1;
6761 if (!h->elf.def_regular)
6762 {
6763 h->elf.root.type = bfd_link_hash_defined;
6764 h->elf.root.u.def.section = htab->sfpr;
6765 h->elf.root.u.def.value = htab->sfpr->size;
6766 h->elf.type = STT_FUNC;
6767 h->elf.def_regular = 1;
6768 h->elf.non_elf = 0;
6769 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6770 writing = TRUE;
6771 if (htab->sfpr->contents == NULL)
6772 {
6773 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6774 if (htab->sfpr->contents == NULL)
6775 return FALSE;
6776 }
6777 }
6778 }
6779 if (writing)
6780 {
6781 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6782 if (i != parm->hi)
6783 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6784 else
6785 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6786 htab->sfpr->size = p - htab->sfpr->contents;
6787 }
6788 }
6789
6790 return TRUE;
6791}
6792
6793static bfd_byte *
6794savegpr0 (bfd *abfd, bfd_byte *p, int r)
6795{
6796 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6797 return p + 4;
6798}
6799
6800static bfd_byte *
6801savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6802{
6803 p = savegpr0 (abfd, p, r);
6804 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6805 p = p + 4;
6806 bfd_put_32 (abfd, BLR, p);
6807 return p + 4;
6808}
6809
6810static bfd_byte *
6811restgpr0 (bfd *abfd, bfd_byte *p, int r)
6812{
6813 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6814 return p + 4;
6815}
6816
6817static bfd_byte *
6818restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6819{
6820 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6821 p = p + 4;
6822 p = restgpr0 (abfd, p, r);
6823 bfd_put_32 (abfd, MTLR_R0, p);
6824 p = p + 4;
6825 if (r == 29)
6826 {
6827 p = restgpr0 (abfd, p, 30);
6828 p = restgpr0 (abfd, p, 31);
6829 }
6830 bfd_put_32 (abfd, BLR, p);
6831 return p + 4;
6832}
6833
6834static bfd_byte *
6835savegpr1 (bfd *abfd, bfd_byte *p, int r)
6836{
6837 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6838 return p + 4;
6839}
6840
6841static bfd_byte *
6842savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6843{
6844 p = savegpr1 (abfd, p, r);
6845 bfd_put_32 (abfd, BLR, p);
6846 return p + 4;
6847}
6848
6849static bfd_byte *
6850restgpr1 (bfd *abfd, bfd_byte *p, int r)
6851{
6852 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6853 return p + 4;
6854}
6855
6856static bfd_byte *
6857restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6858{
6859 p = restgpr1 (abfd, p, r);
6860 bfd_put_32 (abfd, BLR, p);
6861 return p + 4;
6862}
6863
6864static bfd_byte *
6865savefpr (bfd *abfd, bfd_byte *p, int r)
6866{
6867 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6868 return p + 4;
6869}
6870
6871static bfd_byte *
6872savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6873{
6874 p = savefpr (abfd, p, r);
6875 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6876 p = p + 4;
6877 bfd_put_32 (abfd, BLR, p);
6878 return p + 4;
6879}
6880
6881static bfd_byte *
6882restfpr (bfd *abfd, bfd_byte *p, int r)
6883{
6884 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6885 return p + 4;
6886}
6887
6888static bfd_byte *
6889restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6890{
6891 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6892 p = p + 4;
6893 p = restfpr (abfd, p, r);
6894 bfd_put_32 (abfd, MTLR_R0, p);
6895 p = p + 4;
6896 if (r == 29)
6897 {
6898 p = restfpr (abfd, p, 30);
6899 p = restfpr (abfd, p, 31);
6900 }
6901 bfd_put_32 (abfd, BLR, p);
6902 return p + 4;
6903}
6904
6905static bfd_byte *
6906savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6907{
6908 p = savefpr (abfd, p, r);
6909 bfd_put_32 (abfd, BLR, p);
6910 return p + 4;
6911}
6912
6913static bfd_byte *
6914restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6915{
6916 p = restfpr (abfd, p, r);
6917 bfd_put_32 (abfd, BLR, p);
6918 return p + 4;
6919}
6920
6921static bfd_byte *
6922savevr (bfd *abfd, bfd_byte *p, int r)
6923{
6924 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6925 p = p + 4;
6926 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6927 return p + 4;
6928}
6929
6930static bfd_byte *
6931savevr_tail (bfd *abfd, bfd_byte *p, int r)
6932{
6933 p = savevr (abfd, p, r);
6934 bfd_put_32 (abfd, BLR, p);
6935 return p + 4;
6936}
6937
6938static bfd_byte *
6939restvr (bfd *abfd, bfd_byte *p, int r)
6940{
6941 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6942 p = p + 4;
6943 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6944 return p + 4;
6945}
6946
6947static bfd_byte *
6948restvr_tail (bfd *abfd, bfd_byte *p, int r)
6949{
6950 p = restvr (abfd, p, r);
6951 bfd_put_32 (abfd, BLR, p);
6952 return p + 4;
6953}
6954
6955/* Called via elf_link_hash_traverse to transfer dynamic linking
6956 information on function code symbol entries to their corresponding
6957 function descriptor symbol entries. */
6958
6959static bfd_boolean
6960func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6961{
6962 struct bfd_link_info *info;
6963 struct ppc_link_hash_table *htab;
6964 struct plt_entry *ent;
6965 struct ppc_link_hash_entry *fh;
6966 struct ppc_link_hash_entry *fdh;
6967 bfd_boolean force_local;
6968
6969 fh = (struct ppc_link_hash_entry *) h;
6970 if (fh->elf.root.type == bfd_link_hash_indirect)
6971 return TRUE;
6972
6973 info = inf;
6974 htab = ppc_hash_table (info);
6975 if (htab == NULL)
6976 return FALSE;
6977
6978 /* Resolve undefined references to dot-symbols as the value
6979 in the function descriptor, if we have one in a regular object.
6980 This is to satisfy cases like ".quad .foo". Calls to functions
6981 in dynamic objects are handled elsewhere. */
6982 if (fh->elf.root.type == bfd_link_hash_undefweak
6983 && fh->was_undefined
6984 && (fdh = defined_func_desc (fh)) != NULL
6985 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6986 && opd_entry_value (fdh->elf.root.u.def.section,
6987 fdh->elf.root.u.def.value,
6988 &fh->elf.root.u.def.section,
6989 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6990 {
6991 fh->elf.root.type = fdh->elf.root.type;
6992 fh->elf.forced_local = 1;
6993 fh->elf.def_regular = fdh->elf.def_regular;
6994 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6995 }
6996
6997 /* If this is a function code symbol, transfer dynamic linking
6998 information to the function descriptor symbol. */
6999 if (!fh->is_func)
7000 return TRUE;
7001
7002 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7003 if (ent->plt.refcount > 0)
7004 break;
7005 if (ent == NULL
7006 || fh->elf.root.root.string[0] != '.'
7007 || fh->elf.root.root.string[1] == '\0')
7008 return TRUE;
7009
7010 /* Find the corresponding function descriptor symbol. Create it
7011 as undefined if necessary. */
7012
7013 fdh = lookup_fdh (fh, htab);
7014 if (fdh == NULL
7015 && !bfd_link_executable (info)
7016 && (fh->elf.root.type == bfd_link_hash_undefined
7017 || fh->elf.root.type == bfd_link_hash_undefweak))
7018 {
7019 fdh = make_fdh (info, fh);
7020 if (fdh == NULL)
7021 return FALSE;
7022 }
7023
7024 /* Fake function descriptors are made undefweak. If the function
7025 code symbol is strong undefined, make the fake sym the same.
7026 If the function code symbol is defined, then force the fake
7027 descriptor local; We can't support overriding of symbols in a
7028 shared library on a fake descriptor. */
7029
7030 if (fdh != NULL
7031 && fdh->fake
7032 && fdh->elf.root.type == bfd_link_hash_undefweak)
7033 {
7034 if (fh->elf.root.type == bfd_link_hash_undefined)
7035 {
7036 fdh->elf.root.type = bfd_link_hash_undefined;
7037 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
7038 }
7039 else if (fh->elf.root.type == bfd_link_hash_defined
7040 || fh->elf.root.type == bfd_link_hash_defweak)
7041 {
7042 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7043 }
7044 }
7045
7046 if (fdh != NULL
7047 && !fdh->elf.forced_local
7048 && (!bfd_link_executable (info)
7049 || fdh->elf.def_dynamic
7050 || fdh->elf.ref_dynamic
7051 || (fdh->elf.root.type == bfd_link_hash_undefweak
7052 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
7053 {
7054 if (fdh->elf.dynindx == -1)
7055 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7056 return FALSE;
7057 fdh->elf.ref_regular |= fh->elf.ref_regular;
7058 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7059 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7060 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7061 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
7062 {
7063 move_plt_plist (fh, fdh);
7064 fdh->elf.needs_plt = 1;
7065 }
7066 fdh->is_func_descriptor = 1;
7067 fdh->oh = fh;
7068 fh->oh = fdh;
7069 }
7070
7071 /* Now that the info is on the function descriptor, clear the
7072 function code sym info. Any function code syms for which we
7073 don't have a definition in a regular file, we force local.
7074 This prevents a shared library from exporting syms that have
7075 been imported from another library. Function code syms that
7076 are really in the library we must leave global to prevent the
7077 linker dragging in a definition from a static library. */
7078 force_local = (!fh->elf.def_regular
7079 || fdh == NULL
7080 || !fdh->elf.def_regular
7081 || fdh->elf.forced_local);
7082 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7083
7084 return TRUE;
7085}
7086
7087static const struct sfpr_def_parms save_res_funcs[] =
7088 {
7089 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7090 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7091 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7092 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7093 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7094 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7095 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7096 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7097 { "._savef", 14, 31, savefpr, savefpr1_tail },
7098 { "._restf", 14, 31, restfpr, restfpr1_tail },
7099 { "_savevr_", 20, 31, savevr, savevr_tail },
7100 { "_restvr_", 20, 31, restvr, restvr_tail }
7101 };
7102
7103/* Called near the start of bfd_elf_size_dynamic_sections. We use
7104 this hook to a) provide some gcc support functions, and b) transfer
7105 dynamic linking information gathered so far on function code symbol
7106 entries, to their corresponding function descriptor symbol entries. */
7107
7108static bfd_boolean
7109ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7110 struct bfd_link_info *info)
7111{
7112 struct ppc_link_hash_table *htab;
7113
7114 htab = ppc_hash_table (info);
7115 if (htab == NULL)
7116 return FALSE;
7117
7118 /* Provide any missing _save* and _rest* functions. */
7119 if (htab->sfpr != NULL)
7120 {
7121 unsigned int i;
7122
7123 htab->sfpr->size = 0;
7124 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7125 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7126 return FALSE;
7127 if (htab->sfpr->size == 0)
7128 htab->sfpr->flags |= SEC_EXCLUDE;
7129 }
7130
7131 if (bfd_link_relocatable (info))
7132 return TRUE;
7133
7134 if (htab->elf.hgot != NULL)
7135 {
7136 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7137 /* Make .TOC. defined so as to prevent it being made dynamic.
7138 The wrong value here is fixed later in ppc64_elf_set_toc. */
7139 if (!htab->elf.hgot->def_regular
7140 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7141 {
7142 htab->elf.hgot->root.type = bfd_link_hash_defined;
7143 htab->elf.hgot->root.u.def.value = 0;
7144 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7145 htab->elf.hgot->def_regular = 1;
7146 htab->elf.hgot->root.linker_def = 1;
7147 }
7148 htab->elf.hgot->type = STT_OBJECT;
7149 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7150 | STV_HIDDEN);
7151 }
7152
7153 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7154
7155 return TRUE;
7156}
7157
7158/* Return true if we have dynamic relocs against H that apply to
7159 read-only sections. */
7160
7161static bfd_boolean
7162readonly_dynrelocs (struct elf_link_hash_entry *h)
7163{
7164 struct ppc_link_hash_entry *eh;
7165 struct elf_dyn_relocs *p;
7166
7167 eh = (struct ppc_link_hash_entry *) h;
7168 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7169 {
7170 asection *s = p->sec->output_section;
7171
7172 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7173 return TRUE;
7174 }
7175 return FALSE;
7176}
7177
7178/* Return true if we have dynamic relocs against H or any of its weak
7179 aliases, that apply to read-only sections. */
7180
7181static bfd_boolean
7182alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7183{
7184 struct ppc_link_hash_entry *eh;
7185
7186 eh = (struct ppc_link_hash_entry *) h;
7187 do
7188 {
7189 if (readonly_dynrelocs (&eh->elf))
7190 return TRUE;
7191 eh = eh->weakref;
7192 } while (eh != NULL && &eh->elf != h);
7193
7194 return FALSE;
7195}
7196
7197/* Return whether EH has pc-relative dynamic relocs. */
7198
7199static bfd_boolean
7200pc_dynrelocs (struct ppc_link_hash_entry *eh)
7201{
7202 struct elf_dyn_relocs *p;
7203
7204 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7205 if (p->pc_count != 0)
7206 return TRUE;
7207 return FALSE;
7208}
7209
7210/* Return true if a global entry stub will be created for H. Valid
7211 for ELFv2 before plt entries have been allocated. */
7212
7213static bfd_boolean
7214global_entry_stub (struct elf_link_hash_entry *h)
7215{
7216 struct plt_entry *pent;
7217
7218 if (!h->pointer_equality_needed
7219 || h->def_regular)
7220 return FALSE;
7221
7222 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7223 if (pent->plt.refcount > 0
7224 && pent->addend == 0)
7225 return TRUE;
7226
7227 return FALSE;
7228}
7229
7230/* Adjust a symbol defined by a dynamic object and referenced by a
7231 regular object. The current definition is in some section of the
7232 dynamic object, but we're not including those sections. We have to
7233 change the definition to something the rest of the link can
7234 understand. */
7235
7236static bfd_boolean
7237ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7238 struct elf_link_hash_entry *h)
7239{
7240 struct ppc_link_hash_table *htab;
7241 asection *s;
7242
7243 htab = ppc_hash_table (info);
7244 if (htab == NULL)
7245 return FALSE;
7246
7247 /* Deal with function syms. */
7248 if (h->type == STT_FUNC
7249 || h->type == STT_GNU_IFUNC
7250 || h->needs_plt)
7251 {
7252 /* Clear procedure linkage table information for any symbol that
7253 won't need a .plt entry. */
7254 struct plt_entry *ent;
7255 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7256 if (ent->plt.refcount > 0)
7257 break;
7258 if (ent == NULL
7259 || (h->type != STT_GNU_IFUNC
7260 && (SYMBOL_CALLS_LOCAL (info, h)
7261 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7262 && h->root.type == bfd_link_hash_undefweak)))
7263 || ((struct ppc_link_hash_entry *) h)->save_res)
7264 {
7265 h->plt.plist = NULL;
7266 h->needs_plt = 0;
7267 h->pointer_equality_needed = 0;
7268 }
7269 else if (abiversion (info->output_bfd) >= 2)
7270 {
7271 /* Taking a function's address in a read/write section
7272 doesn't require us to define the function symbol in the
7273 executable on a global entry stub. A dynamic reloc can
7274 be used instead. The reason we prefer a few more dynamic
7275 relocs is that calling via a global entry stub costs a
7276 few more instructions, and pointer_equality_needed causes
7277 extra work in ld.so when resolving these symbols. */
7278 if (global_entry_stub (h)
7279 && !alias_readonly_dynrelocs (h))
7280 {
7281 h->pointer_equality_needed = 0;
7282 /* After adjust_dynamic_symbol, non_got_ref set in
7283 the non-pic case means that dyn_relocs for this
7284 symbol should be discarded. */
7285 h->non_got_ref = 0;
7286 }
7287
7288 /* If making a plt entry, then we don't need copy relocs. */
7289 return TRUE;
7290 }
7291 }
7292 else
7293 h->plt.plist = NULL;
7294
7295 /* If this is a weak symbol, and there is a real definition, the
7296 processor independent code will have arranged for us to see the
7297 real definition first, and we can just use the same value. */
7298 if (h->u.weakdef != NULL)
7299 {
7300 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7301 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7302 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7303 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7304 if (ELIMINATE_COPY_RELOCS)
7305 h->non_got_ref = h->u.weakdef->non_got_ref;
7306 return TRUE;
7307 }
7308
7309 /* If we are creating a shared library, we must presume that the
7310 only references to the symbol are via the global offset table.
7311 For such cases we need not do anything here; the relocations will
7312 be handled correctly by relocate_section. */
7313 if (bfd_link_pic (info))
7314 return TRUE;
7315
7316 /* If there are no references to this symbol that do not use the
7317 GOT, we don't need to generate a copy reloc. */
7318 if (!h->non_got_ref)
7319 return TRUE;
7320
7321 /* Don't generate a copy reloc for symbols defined in the executable. */
7322 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7323
7324 /* If -z nocopyreloc was given, don't generate them either. */
7325 || info->nocopyreloc
7326
7327 /* If we didn't find any dynamic relocs in read-only sections, then
7328 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7329 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7330
7331 /* Protected variables do not work with .dynbss. The copy in
7332 .dynbss won't be used by the shared library with the protected
7333 definition for the variable. Text relocations are preferable
7334 to an incorrect program. */
7335 || h->protected_def)
7336 {
7337 h->non_got_ref = 0;
7338 return TRUE;
7339 }
7340
7341 if (h->plt.plist != NULL)
7342 {
7343 /* We should never get here, but unfortunately there are versions
7344 of gcc out there that improperly (for this ABI) put initialized
7345 function pointers, vtable refs and suchlike in read-only
7346 sections. Allow them to proceed, but warn that this might
7347 break at runtime. */
7348 info->callbacks->einfo
7349 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7350 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7351 h->root.root.string);
7352 }
7353
7354 /* This is a reference to a symbol defined by a dynamic object which
7355 is not a function. */
7356
7357 /* We must allocate the symbol in our .dynbss section, which will
7358 become part of the .bss section of the executable. There will be
7359 an entry for this symbol in the .dynsym section. The dynamic
7360 object will contain position independent code, so all references
7361 from the dynamic object to this symbol will go through the global
7362 offset table. The dynamic linker will use the .dynsym entry to
7363 determine the address it must put in the global offset table, so
7364 both the dynamic object and the regular object will refer to the
7365 same memory location for the variable. */
7366
7367 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7368 to copy the initial value out of the dynamic object and into the
7369 runtime process image. We need to remember the offset into the
7370 .rela.bss section we are going to use. */
7371 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7372 {
7373 htab->relbss->size += sizeof (Elf64_External_Rela);
7374 h->needs_copy = 1;
7375 }
7376
7377 s = htab->dynbss;
7378
7379 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7380}
7381
7382/* If given a function descriptor symbol, hide both the function code
7383 sym and the descriptor. */
7384static void
7385ppc64_elf_hide_symbol (struct bfd_link_info *info,
7386 struct elf_link_hash_entry *h,
7387 bfd_boolean force_local)
7388{
7389 struct ppc_link_hash_entry *eh;
7390 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7391
7392 eh = (struct ppc_link_hash_entry *) h;
7393 if (eh->is_func_descriptor)
7394 {
7395 struct ppc_link_hash_entry *fh = eh->oh;
7396
7397 if (fh == NULL)
7398 {
7399 const char *p, *q;
7400 struct ppc_link_hash_table *htab;
7401 char save;
7402
7403 /* We aren't supposed to use alloca in BFD because on
7404 systems which do not have alloca the version in libiberty
7405 calls xmalloc, which might cause the program to crash
7406 when it runs out of memory. This function doesn't have a
7407 return status, so there's no way to gracefully return an
7408 error. So cheat. We know that string[-1] can be safely
7409 accessed; It's either a string in an ELF string table,
7410 or allocated in an objalloc structure. */
7411
7412 p = eh->elf.root.root.string - 1;
7413 save = *p;
7414 *(char *) p = '.';
7415 htab = ppc_hash_table (info);
7416 if (htab == NULL)
7417 return;
7418
7419 fh = (struct ppc_link_hash_entry *)
7420 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7421 *(char *) p = save;
7422
7423 /* Unfortunately, if it so happens that the string we were
7424 looking for was allocated immediately before this string,
7425 then we overwrote the string terminator. That's the only
7426 reason the lookup should fail. */
7427 if (fh == NULL)
7428 {
7429 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7430 while (q >= eh->elf.root.root.string && *q == *p)
7431 --q, --p;
7432 if (q < eh->elf.root.root.string && *p == '.')
7433 fh = (struct ppc_link_hash_entry *)
7434 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7435 }
7436 if (fh != NULL)
7437 {
7438 eh->oh = fh;
7439 fh->oh = eh;
7440 }
7441 }
7442 if (fh != NULL)
7443 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7444 }
7445}
7446
7447static bfd_boolean
7448get_sym_h (struct elf_link_hash_entry **hp,
7449 Elf_Internal_Sym **symp,
7450 asection **symsecp,
7451 unsigned char **tls_maskp,
7452 Elf_Internal_Sym **locsymsp,
7453 unsigned long r_symndx,
7454 bfd *ibfd)
7455{
7456 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7457
7458 if (r_symndx >= symtab_hdr->sh_info)
7459 {
7460 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7461 struct elf_link_hash_entry *h;
7462
7463 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7464 h = elf_follow_link (h);
7465
7466 if (hp != NULL)
7467 *hp = h;
7468
7469 if (symp != NULL)
7470 *symp = NULL;
7471
7472 if (symsecp != NULL)
7473 {
7474 asection *symsec = NULL;
7475 if (h->root.type == bfd_link_hash_defined
7476 || h->root.type == bfd_link_hash_defweak)
7477 symsec = h->root.u.def.section;
7478 *symsecp = symsec;
7479 }
7480
7481 if (tls_maskp != NULL)
7482 {
7483 struct ppc_link_hash_entry *eh;
7484
7485 eh = (struct ppc_link_hash_entry *) h;
7486 *tls_maskp = &eh->tls_mask;
7487 }
7488 }
7489 else
7490 {
7491 Elf_Internal_Sym *sym;
7492 Elf_Internal_Sym *locsyms = *locsymsp;
7493
7494 if (locsyms == NULL)
7495 {
7496 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7497 if (locsyms == NULL)
7498 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7499 symtab_hdr->sh_info,
7500 0, NULL, NULL, NULL);
7501 if (locsyms == NULL)
7502 return FALSE;
7503 *locsymsp = locsyms;
7504 }
7505 sym = locsyms + r_symndx;
7506
7507 if (hp != NULL)
7508 *hp = NULL;
7509
7510 if (symp != NULL)
7511 *symp = sym;
7512
7513 if (symsecp != NULL)
7514 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7515
7516 if (tls_maskp != NULL)
7517 {
7518 struct got_entry **lgot_ents;
7519 unsigned char *tls_mask;
7520
7521 tls_mask = NULL;
7522 lgot_ents = elf_local_got_ents (ibfd);
7523 if (lgot_ents != NULL)
7524 {
7525 struct plt_entry **local_plt = (struct plt_entry **)
7526 (lgot_ents + symtab_hdr->sh_info);
7527 unsigned char *lgot_masks = (unsigned char *)
7528 (local_plt + symtab_hdr->sh_info);
7529 tls_mask = &lgot_masks[r_symndx];
7530 }
7531 *tls_maskp = tls_mask;
7532 }
7533 }
7534 return TRUE;
7535}
7536
7537/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7538 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7539 type suitable for optimization, and 1 otherwise. */
7540
7541static int
7542get_tls_mask (unsigned char **tls_maskp,
7543 unsigned long *toc_symndx,
7544 bfd_vma *toc_addend,
7545 Elf_Internal_Sym **locsymsp,
7546 const Elf_Internal_Rela *rel,
7547 bfd *ibfd)
7548{
7549 unsigned long r_symndx;
7550 int next_r;
7551 struct elf_link_hash_entry *h;
7552 Elf_Internal_Sym *sym;
7553 asection *sec;
7554 bfd_vma off;
7555
7556 r_symndx = ELF64_R_SYM (rel->r_info);
7557 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7558 return 0;
7559
7560 if ((*tls_maskp != NULL && **tls_maskp != 0)
7561 || sec == NULL
7562 || ppc64_elf_section_data (sec) == NULL
7563 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7564 return 1;
7565
7566 /* Look inside a TOC section too. */
7567 if (h != NULL)
7568 {
7569 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7570 off = h->root.u.def.value;
7571 }
7572 else
7573 off = sym->st_value;
7574 off += rel->r_addend;
7575 BFD_ASSERT (off % 8 == 0);
7576 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7577 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7578 if (toc_symndx != NULL)
7579 *toc_symndx = r_symndx;
7580 if (toc_addend != NULL)
7581 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7582 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7583 return 0;
7584 if ((h == NULL || is_static_defined (h))
7585 && (next_r == -1 || next_r == -2))
7586 return 1 - next_r;
7587 return 1;
7588}
7589
7590/* Find (or create) an entry in the tocsave hash table. */
7591
7592static struct tocsave_entry *
7593tocsave_find (struct ppc_link_hash_table *htab,
7594 enum insert_option insert,
7595 Elf_Internal_Sym **local_syms,
7596 const Elf_Internal_Rela *irela,
7597 bfd *ibfd)
7598{
7599 unsigned long r_indx;
7600 struct elf_link_hash_entry *h;
7601 Elf_Internal_Sym *sym;
7602 struct tocsave_entry ent, *p;
7603 hashval_t hash;
7604 struct tocsave_entry **slot;
7605
7606 r_indx = ELF64_R_SYM (irela->r_info);
7607 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7608 return NULL;
7609 if (ent.sec == NULL || ent.sec->output_section == NULL)
7610 {
7611 _bfd_error_handler
7612 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7613 return NULL;
7614 }
7615
7616 if (h != NULL)
7617 ent.offset = h->root.u.def.value;
7618 else
7619 ent.offset = sym->st_value;
7620 ent.offset += irela->r_addend;
7621
7622 hash = tocsave_htab_hash (&ent);
7623 slot = ((struct tocsave_entry **)
7624 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7625 if (slot == NULL)
7626 return NULL;
7627
7628 if (*slot == NULL)
7629 {
7630 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7631 if (p == NULL)
7632 return NULL;
7633 *p = ent;
7634 *slot = p;
7635 }
7636 return *slot;
7637}
7638
7639/* Adjust all global syms defined in opd sections. In gcc generated
7640 code for the old ABI, these will already have been done. */
7641
7642static bfd_boolean
7643adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7644{
7645 struct ppc_link_hash_entry *eh;
7646 asection *sym_sec;
7647 struct _opd_sec_data *opd;
7648
7649 if (h->root.type == bfd_link_hash_indirect)
7650 return TRUE;
7651
7652 if (h->root.type != bfd_link_hash_defined
7653 && h->root.type != bfd_link_hash_defweak)
7654 return TRUE;
7655
7656 eh = (struct ppc_link_hash_entry *) h;
7657 if (eh->adjust_done)
7658 return TRUE;
7659
7660 sym_sec = eh->elf.root.u.def.section;
7661 opd = get_opd_info (sym_sec);
7662 if (opd != NULL && opd->adjust != NULL)
7663 {
7664 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7665 if (adjust == -1)
7666 {
7667 /* This entry has been deleted. */
7668 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7669 if (dsec == NULL)
7670 {
7671 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7672 if (discarded_section (dsec))
7673 {
7674 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7675 break;
7676 }
7677 }
7678 eh->elf.root.u.def.value = 0;
7679 eh->elf.root.u.def.section = dsec;
7680 }
7681 else
7682 eh->elf.root.u.def.value += adjust;
7683 eh->adjust_done = 1;
7684 }
7685 return TRUE;
7686}
7687
7688/* Handles decrementing dynamic reloc counts for the reloc specified by
7689 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7690 have already been determined. */
7691
7692static bfd_boolean
7693dec_dynrel_count (bfd_vma r_info,
7694 asection *sec,
7695 struct bfd_link_info *info,
7696 Elf_Internal_Sym **local_syms,
7697 struct elf_link_hash_entry *h,
7698 Elf_Internal_Sym *sym)
7699{
7700 enum elf_ppc64_reloc_type r_type;
7701 asection *sym_sec = NULL;
7702
7703 /* Can this reloc be dynamic? This switch, and later tests here
7704 should be kept in sync with the code in check_relocs. */
7705 r_type = ELF64_R_TYPE (r_info);
7706 switch (r_type)
7707 {
7708 default:
7709 return TRUE;
7710
7711 case R_PPC64_TPREL16:
7712 case R_PPC64_TPREL16_LO:
7713 case R_PPC64_TPREL16_HI:
7714 case R_PPC64_TPREL16_HA:
7715 case R_PPC64_TPREL16_DS:
7716 case R_PPC64_TPREL16_LO_DS:
7717 case R_PPC64_TPREL16_HIGH:
7718 case R_PPC64_TPREL16_HIGHA:
7719 case R_PPC64_TPREL16_HIGHER:
7720 case R_PPC64_TPREL16_HIGHERA:
7721 case R_PPC64_TPREL16_HIGHEST:
7722 case R_PPC64_TPREL16_HIGHESTA:
7723 if (!bfd_link_pic (info))
7724 return TRUE;
7725
7726 case R_PPC64_TPREL64:
7727 case R_PPC64_DTPMOD64:
7728 case R_PPC64_DTPREL64:
7729 case R_PPC64_ADDR64:
7730 case R_PPC64_REL30:
7731 case R_PPC64_REL32:
7732 case R_PPC64_REL64:
7733 case R_PPC64_ADDR14:
7734 case R_PPC64_ADDR14_BRNTAKEN:
7735 case R_PPC64_ADDR14_BRTAKEN:
7736 case R_PPC64_ADDR16:
7737 case R_PPC64_ADDR16_DS:
7738 case R_PPC64_ADDR16_HA:
7739 case R_PPC64_ADDR16_HI:
7740 case R_PPC64_ADDR16_HIGH:
7741 case R_PPC64_ADDR16_HIGHA:
7742 case R_PPC64_ADDR16_HIGHER:
7743 case R_PPC64_ADDR16_HIGHERA:
7744 case R_PPC64_ADDR16_HIGHEST:
7745 case R_PPC64_ADDR16_HIGHESTA:
7746 case R_PPC64_ADDR16_LO:
7747 case R_PPC64_ADDR16_LO_DS:
7748 case R_PPC64_ADDR24:
7749 case R_PPC64_ADDR32:
7750 case R_PPC64_UADDR16:
7751 case R_PPC64_UADDR32:
7752 case R_PPC64_UADDR64:
7753 case R_PPC64_TOC:
7754 break;
7755 }
7756
7757 if (local_syms != NULL)
7758 {
7759 unsigned long r_symndx;
7760 bfd *ibfd = sec->owner;
7761
7762 r_symndx = ELF64_R_SYM (r_info);
7763 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7764 return FALSE;
7765 }
7766
7767 if ((bfd_link_pic (info)
7768 && (must_be_dyn_reloc (info, r_type)
7769 || (h != NULL
7770 && (!SYMBOLIC_BIND (info, h)
7771 || h->root.type == bfd_link_hash_defweak
7772 || !h->def_regular))))
7773 || (ELIMINATE_COPY_RELOCS
7774 && !bfd_link_pic (info)
7775 && h != NULL
7776 && (h->root.type == bfd_link_hash_defweak
7777 || !h->def_regular)))
7778 ;
7779 else
7780 return TRUE;
7781
7782 if (h != NULL)
7783 {
7784 struct elf_dyn_relocs *p;
7785 struct elf_dyn_relocs **pp;
7786 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7787
7788 /* elf_gc_sweep may have already removed all dyn relocs associated
7789 with local syms for a given section. Also, symbol flags are
7790 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7791 report a dynreloc miscount. */
7792 if (*pp == NULL && info->gc_sections)
7793 return TRUE;
7794
7795 while ((p = *pp) != NULL)
7796 {
7797 if (p->sec == sec)
7798 {
7799 if (!must_be_dyn_reloc (info, r_type))
7800 p->pc_count -= 1;
7801 p->count -= 1;
7802 if (p->count == 0)
7803 *pp = p->next;
7804 return TRUE;
7805 }
7806 pp = &p->next;
7807 }
7808 }
7809 else
7810 {
7811 struct ppc_dyn_relocs *p;
7812 struct ppc_dyn_relocs **pp;
7813 void *vpp;
7814 bfd_boolean is_ifunc;
7815
7816 if (local_syms == NULL)
7817 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7818 if (sym_sec == NULL)
7819 sym_sec = sec;
7820
7821 vpp = &elf_section_data (sym_sec)->local_dynrel;
7822 pp = (struct ppc_dyn_relocs **) vpp;
7823
7824 if (*pp == NULL && info->gc_sections)
7825 return TRUE;
7826
7827 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7828 while ((p = *pp) != NULL)
7829 {
7830 if (p->sec == sec && p->ifunc == is_ifunc)
7831 {
7832 p->count -= 1;
7833 if (p->count == 0)
7834 *pp = p->next;
7835 return TRUE;
7836 }
7837 pp = &p->next;
7838 }
7839 }
7840
7841 /* xgettext:c-format */
7842 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7843 sec->owner, sec);
7844 bfd_set_error (bfd_error_bad_value);
7845 return FALSE;
7846}
7847
7848/* Remove unused Official Procedure Descriptor entries. Currently we
7849 only remove those associated with functions in discarded link-once
7850 sections, or weakly defined functions that have been overridden. It
7851 would be possible to remove many more entries for statically linked
7852 applications. */
7853
7854bfd_boolean
7855ppc64_elf_edit_opd (struct bfd_link_info *info)
7856{
7857 bfd *ibfd;
7858 bfd_boolean some_edited = FALSE;
7859 asection *need_pad = NULL;
7860 struct ppc_link_hash_table *htab;
7861
7862 htab = ppc_hash_table (info);
7863 if (htab == NULL)
7864 return FALSE;
7865
7866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7867 {
7868 asection *sec;
7869 Elf_Internal_Rela *relstart, *rel, *relend;
7870 Elf_Internal_Shdr *symtab_hdr;
7871 Elf_Internal_Sym *local_syms;
7872 struct _opd_sec_data *opd;
7873 bfd_boolean need_edit, add_aux_fields, broken;
7874 bfd_size_type cnt_16b = 0;
7875
7876 if (!is_ppc64_elf (ibfd))
7877 continue;
7878
7879 sec = bfd_get_section_by_name (ibfd, ".opd");
7880 if (sec == NULL || sec->size == 0)
7881 continue;
7882
7883 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7884 continue;
7885
7886 if (sec->output_section == bfd_abs_section_ptr)
7887 continue;
7888
7889 /* Look through the section relocs. */
7890 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7891 continue;
7892
7893 local_syms = NULL;
7894 symtab_hdr = &elf_symtab_hdr (ibfd);
7895
7896 /* Read the relocations. */
7897 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7898 info->keep_memory);
7899 if (relstart == NULL)
7900 return FALSE;
7901
7902 /* First run through the relocs to check they are sane, and to
7903 determine whether we need to edit this opd section. */
7904 need_edit = FALSE;
7905 broken = FALSE;
7906 need_pad = sec;
7907 relend = relstart + sec->reloc_count;
7908 for (rel = relstart; rel < relend; )
7909 {
7910 enum elf_ppc64_reloc_type r_type;
7911 unsigned long r_symndx;
7912 asection *sym_sec;
7913 struct elf_link_hash_entry *h;
7914 Elf_Internal_Sym *sym;
7915 bfd_vma offset;
7916
7917 /* .opd contains an array of 16 or 24 byte entries. We're
7918 only interested in the reloc pointing to a function entry
7919 point. */
7920 offset = rel->r_offset;
7921 if (rel + 1 == relend
7922 || rel[1].r_offset != offset + 8)
7923 {
7924 /* If someone messes with .opd alignment then after a
7925 "ld -r" we might have padding in the middle of .opd.
7926 Also, there's nothing to prevent someone putting
7927 something silly in .opd with the assembler. No .opd
7928 optimization for them! */
7929 broken_opd:
7930 _bfd_error_handler
7931 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7932 broken = TRUE;
7933 break;
7934 }
7935
7936 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7937 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7938 {
7939 _bfd_error_handler
7940 /* xgettext:c-format */
7941 (_("%B: unexpected reloc type %u in .opd section"),
7942 ibfd, r_type);
7943 broken = TRUE;
7944 break;
7945 }
7946
7947 r_symndx = ELF64_R_SYM (rel->r_info);
7948 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7949 r_symndx, ibfd))
7950 goto error_ret;
7951
7952 if (sym_sec == NULL || sym_sec->owner == NULL)
7953 {
7954 const char *sym_name;
7955 if (h != NULL)
7956 sym_name = h->root.root.string;
7957 else
7958 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7959 sym_sec);
7960
7961 _bfd_error_handler
7962 /* xgettext:c-format */
7963 (_("%B: undefined sym `%s' in .opd section"),
7964 ibfd, sym_name);
7965 broken = TRUE;
7966 break;
7967 }
7968
7969 /* opd entries are always for functions defined in the
7970 current input bfd. If the symbol isn't defined in the
7971 input bfd, then we won't be using the function in this
7972 bfd; It must be defined in a linkonce section in another
7973 bfd, or is weak. It's also possible that we are
7974 discarding the function due to a linker script /DISCARD/,
7975 which we test for via the output_section. */
7976 if (sym_sec->owner != ibfd
7977 || sym_sec->output_section == bfd_abs_section_ptr)
7978 need_edit = TRUE;
7979
7980 rel += 2;
7981 if (rel + 1 == relend
7982 || (rel + 2 < relend
7983 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7984 ++rel;
7985
7986 if (rel == relend)
7987 {
7988 if (sec->size == offset + 24)
7989 {
7990 need_pad = NULL;
7991 break;
7992 }
7993 if (sec->size == offset + 16)
7994 {
7995 cnt_16b++;
7996 break;
7997 }
7998 goto broken_opd;
7999 }
8000 else if (rel + 1 < relend
8001 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8002 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8003 {
8004 if (rel[0].r_offset == offset + 16)
8005 cnt_16b++;
8006 else if (rel[0].r_offset != offset + 24)
8007 goto broken_opd;
8008 }
8009 else
8010 goto broken_opd;
8011 }
8012
8013 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8014
8015 if (!broken && (need_edit || add_aux_fields))
8016 {
8017 Elf_Internal_Rela *write_rel;
8018 Elf_Internal_Shdr *rel_hdr;
8019 bfd_byte *rptr, *wptr;
8020 bfd_byte *new_contents;
8021 bfd_size_type amt;
8022
8023 new_contents = NULL;
8024 amt = OPD_NDX (sec->size) * sizeof (long);
8025 opd = &ppc64_elf_section_data (sec)->u.opd;
8026 opd->adjust = bfd_zalloc (sec->owner, amt);
8027 if (opd->adjust == NULL)
8028 return FALSE;
8029 ppc64_elf_section_data (sec)->sec_type = sec_opd;
8030
8031 /* This seems a waste of time as input .opd sections are all
8032 zeros as generated by gcc, but I suppose there's no reason
8033 this will always be so. We might start putting something in
8034 the third word of .opd entries. */
8035 if ((sec->flags & SEC_IN_MEMORY) == 0)
8036 {
8037 bfd_byte *loc;
8038 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8039 {
8040 if (loc != NULL)
8041 free (loc);
8042 error_ret:
8043 if (local_syms != NULL
8044 && symtab_hdr->contents != (unsigned char *) local_syms)
8045 free (local_syms);
8046 if (elf_section_data (sec)->relocs != relstart)
8047 free (relstart);
8048 return FALSE;
8049 }
8050 sec->contents = loc;
8051 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8052 }
8053
8054 elf_section_data (sec)->relocs = relstart;
8055
8056 new_contents = sec->contents;
8057 if (add_aux_fields)
8058 {
8059 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8060 if (new_contents == NULL)
8061 return FALSE;
8062 need_pad = NULL;
8063 }
8064 wptr = new_contents;
8065 rptr = sec->contents;
8066 write_rel = relstart;
8067 for (rel = relstart; rel < relend; )
8068 {
8069 unsigned long r_symndx;
8070 asection *sym_sec;
8071 struct elf_link_hash_entry *h;
8072 struct ppc_link_hash_entry *fdh = NULL;
8073 Elf_Internal_Sym *sym;
8074 long opd_ent_size;
8075 Elf_Internal_Rela *next_rel;
8076 bfd_boolean skip;
8077
8078 r_symndx = ELF64_R_SYM (rel->r_info);
8079 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8080 r_symndx, ibfd))
8081 goto error_ret;
8082
8083 next_rel = rel + 2;
8084 if (next_rel + 1 == relend
8085 || (next_rel + 2 < relend
8086 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8087 ++next_rel;
8088
8089 /* See if the .opd entry is full 24 byte or
8090 16 byte (with fd_aux entry overlapped with next
8091 fd_func). */
8092 opd_ent_size = 24;
8093 if (next_rel == relend)
8094 {
8095 if (sec->size == rel->r_offset + 16)
8096 opd_ent_size = 16;
8097 }
8098 else if (next_rel->r_offset == rel->r_offset + 16)
8099 opd_ent_size = 16;
8100
8101 if (h != NULL
8102 && h->root.root.string[0] == '.')
8103 {
8104 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
8105 if (fdh != NULL
8106 && fdh->elf.root.type != bfd_link_hash_defined
8107 && fdh->elf.root.type != bfd_link_hash_defweak)
8108 fdh = NULL;
8109 }
8110
8111 skip = (sym_sec->owner != ibfd
8112 || sym_sec->output_section == bfd_abs_section_ptr);
8113 if (skip)
8114 {
8115 if (fdh != NULL && sym_sec->owner == ibfd)
8116 {
8117 /* Arrange for the function descriptor sym
8118 to be dropped. */
8119 fdh->elf.root.u.def.value = 0;
8120 fdh->elf.root.u.def.section = sym_sec;
8121 }
8122 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8123
8124 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8125 rel = next_rel;
8126 else
8127 while (1)
8128 {
8129 if (!dec_dynrel_count (rel->r_info, sec, info,
8130 NULL, h, sym))
8131 goto error_ret;
8132
8133 if (++rel == next_rel)
8134 break;
8135
8136 r_symndx = ELF64_R_SYM (rel->r_info);
8137 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8138 r_symndx, ibfd))
8139 goto error_ret;
8140 }
8141 }
8142 else
8143 {
8144 /* We'll be keeping this opd entry. */
8145 long adjust;
8146
8147 if (fdh != NULL)
8148 {
8149 /* Redefine the function descriptor symbol to
8150 this location in the opd section. It is
8151 necessary to update the value here rather
8152 than using an array of adjustments as we do
8153 for local symbols, because various places
8154 in the generic ELF code use the value
8155 stored in u.def.value. */
8156 fdh->elf.root.u.def.value = wptr - new_contents;
8157 fdh->adjust_done = 1;
8158 }
8159
8160 /* Local syms are a bit tricky. We could
8161 tweak them as they can be cached, but
8162 we'd need to look through the local syms
8163 for the function descriptor sym which we
8164 don't have at the moment. So keep an
8165 array of adjustments. */
8166 adjust = (wptr - new_contents) - (rptr - sec->contents);
8167 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8168
8169 if (wptr != rptr)
8170 memcpy (wptr, rptr, opd_ent_size);
8171 wptr += opd_ent_size;
8172 if (add_aux_fields && opd_ent_size == 16)
8173 {
8174 memset (wptr, '\0', 8);
8175 wptr += 8;
8176 }
8177
8178 /* We need to adjust any reloc offsets to point to the
8179 new opd entries. */
8180 for ( ; rel != next_rel; ++rel)
8181 {
8182 rel->r_offset += adjust;
8183 if (write_rel != rel)
8184 memcpy (write_rel, rel, sizeof (*rel));
8185 ++write_rel;
8186 }
8187 }
8188
8189 rptr += opd_ent_size;
8190 }
8191
8192 sec->size = wptr - new_contents;
8193 sec->reloc_count = write_rel - relstart;
8194 if (add_aux_fields)
8195 {
8196 free (sec->contents);
8197 sec->contents = new_contents;
8198 }
8199
8200 /* Fudge the header size too, as this is used later in
8201 elf_bfd_final_link if we are emitting relocs. */
8202 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8203 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8204 some_edited = TRUE;
8205 }
8206 else if (elf_section_data (sec)->relocs != relstart)
8207 free (relstart);
8208
8209 if (local_syms != NULL
8210 && symtab_hdr->contents != (unsigned char *) local_syms)
8211 {
8212 if (!info->keep_memory)
8213 free (local_syms);
8214 else
8215 symtab_hdr->contents = (unsigned char *) local_syms;
8216 }
8217 }
8218
8219 if (some_edited)
8220 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8221
8222 /* If we are doing a final link and the last .opd entry is just 16 byte
8223 long, add a 8 byte padding after it. */
8224 if (need_pad != NULL && !bfd_link_relocatable (info))
8225 {
8226 bfd_byte *p;
8227
8228 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8229 {
8230 BFD_ASSERT (need_pad->size > 0);
8231
8232 p = bfd_malloc (need_pad->size + 8);
8233 if (p == NULL)
8234 return FALSE;
8235
8236 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8237 p, 0, need_pad->size))
8238 return FALSE;
8239
8240 need_pad->contents = p;
8241 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8242 }
8243 else
8244 {
8245 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8246 if (p == NULL)
8247 return FALSE;
8248
8249 need_pad->contents = p;
8250 }
8251
8252 memset (need_pad->contents + need_pad->size, 0, 8);
8253 need_pad->size += 8;
8254 }
8255
8256 return TRUE;
8257}
8258
8259/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8260
8261asection *
8262ppc64_elf_tls_setup (struct bfd_link_info *info)
8263{
8264 struct ppc_link_hash_table *htab;
8265
8266 htab = ppc_hash_table (info);
8267 if (htab == NULL)
8268 return NULL;
8269
8270 if (abiversion (info->output_bfd) == 1)
8271 htab->opd_abi = 1;
8272
8273 if (htab->params->no_multi_toc)
8274 htab->do_multi_toc = 0;
8275 else if (!htab->do_multi_toc)
8276 htab->params->no_multi_toc = 1;
8277
8278 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8279 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8280 FALSE, FALSE, TRUE));
8281 /* Move dynamic linking info to the function descriptor sym. */
8282 if (htab->tls_get_addr != NULL)
8283 func_desc_adjust (&htab->tls_get_addr->elf, info);
8284 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8285 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8286 FALSE, FALSE, TRUE));
8287 if (htab->params->tls_get_addr_opt)
8288 {
8289 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8290
8291 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8292 FALSE, FALSE, TRUE);
8293 if (opt != NULL)
8294 func_desc_adjust (opt, info);
8295 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8296 FALSE, FALSE, TRUE);
8297 if (opt_fd != NULL
8298 && (opt_fd->root.type == bfd_link_hash_defined
8299 || opt_fd->root.type == bfd_link_hash_defweak))
8300 {
8301 /* If glibc supports an optimized __tls_get_addr call stub,
8302 signalled by the presence of __tls_get_addr_opt, and we'll
8303 be calling __tls_get_addr via a plt call stub, then
8304 make __tls_get_addr point to __tls_get_addr_opt. */
8305 tga_fd = &htab->tls_get_addr_fd->elf;
8306 if (htab->elf.dynamic_sections_created
8307 && tga_fd != NULL
8308 && (tga_fd->type == STT_FUNC
8309 || tga_fd->needs_plt)
8310 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8311 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8312 && tga_fd->root.type == bfd_link_hash_undefweak)))
8313 {
8314 struct plt_entry *ent;
8315
8316 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8317 if (ent->plt.refcount > 0)
8318 break;
8319 if (ent != NULL)
8320 {
8321 tga_fd->root.type = bfd_link_hash_indirect;
8322 tga_fd->root.u.i.link = &opt_fd->root;
8323 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8324 opt_fd->forced_local = 0;
8325 if (opt_fd->dynindx != -1)
8326 {
8327 /* Use __tls_get_addr_opt in dynamic relocations. */
8328 opt_fd->dynindx = -1;
8329 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8330 opt_fd->dynstr_index);
8331 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8332 return NULL;
8333 }
8334 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8335 tga = &htab->tls_get_addr->elf;
8336 if (opt != NULL && tga != NULL)
8337 {
8338 tga->root.type = bfd_link_hash_indirect;
8339 tga->root.u.i.link = &opt->root;
8340 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8341 opt->forced_local = 0;
8342 _bfd_elf_link_hash_hide_symbol (info, opt,
8343 tga->forced_local);
8344 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8345 }
8346 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8347 htab->tls_get_addr_fd->is_func_descriptor = 1;
8348 if (htab->tls_get_addr != NULL)
8349 {
8350 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8351 htab->tls_get_addr->is_func = 1;
8352 }
8353 }
8354 }
8355 }
8356 else if (htab->params->tls_get_addr_opt < 0)
8357 htab->params->tls_get_addr_opt = 0;
8358 }
8359 return _bfd_elf_tls_setup (info->output_bfd, info);
8360}
8361
8362/* Return TRUE iff REL is a branch reloc with a global symbol matching
8363 HASH1 or HASH2. */
8364
8365static bfd_boolean
8366branch_reloc_hash_match (const bfd *ibfd,
8367 const Elf_Internal_Rela *rel,
8368 const struct ppc_link_hash_entry *hash1,
8369 const struct ppc_link_hash_entry *hash2)
8370{
8371 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8372 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8373 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8374
8375 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8376 {
8377 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8378 struct elf_link_hash_entry *h;
8379
8380 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8381 h = elf_follow_link (h);
8382 if (h == &hash1->elf || h == &hash2->elf)
8383 return TRUE;
8384 }
8385 return FALSE;
8386}
8387
8388/* Run through all the TLS relocs looking for optimization
8389 opportunities. The linker has been hacked (see ppc64elf.em) to do
8390 a preliminary section layout so that we know the TLS segment
8391 offsets. We can't optimize earlier because some optimizations need
8392 to know the tp offset, and we need to optimize before allocating
8393 dynamic relocations. */
8394
8395bfd_boolean
8396ppc64_elf_tls_optimize (struct bfd_link_info *info)
8397{
8398 bfd *ibfd;
8399 asection *sec;
8400 struct ppc_link_hash_table *htab;
8401 unsigned char *toc_ref;
8402 int pass;
8403
8404 if (!bfd_link_executable (info))
8405 return TRUE;
8406
8407 htab = ppc_hash_table (info);
8408 if (htab == NULL)
8409 return FALSE;
8410
8411 /* Make two passes over the relocs. On the first pass, mark toc
8412 entries involved with tls relocs, and check that tls relocs
8413 involved in setting up a tls_get_addr call are indeed followed by
8414 such a call. If they are not, we can't do any tls optimization.
8415 On the second pass twiddle tls_mask flags to notify
8416 relocate_section that optimization can be done, and adjust got
8417 and plt refcounts. */
8418 toc_ref = NULL;
8419 for (pass = 0; pass < 2; ++pass)
8420 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8421 {
8422 Elf_Internal_Sym *locsyms = NULL;
8423 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8424
8425 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8426 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8427 {
8428 Elf_Internal_Rela *relstart, *rel, *relend;
8429 bfd_boolean found_tls_get_addr_arg = 0;
8430
8431 /* Read the relocations. */
8432 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8433 info->keep_memory);
8434 if (relstart == NULL)
8435 {
8436 free (toc_ref);
8437 return FALSE;
8438 }
8439
8440 relend = relstart + sec->reloc_count;
8441 for (rel = relstart; rel < relend; rel++)
8442 {
8443 enum elf_ppc64_reloc_type r_type;
8444 unsigned long r_symndx;
8445 struct elf_link_hash_entry *h;
8446 Elf_Internal_Sym *sym;
8447 asection *sym_sec;
8448 unsigned char *tls_mask;
8449 unsigned char tls_set, tls_clear, tls_type = 0;
8450 bfd_vma value;
8451 bfd_boolean ok_tprel, is_local;
8452 long toc_ref_index = 0;
8453 int expecting_tls_get_addr = 0;
8454 bfd_boolean ret = FALSE;
8455
8456 r_symndx = ELF64_R_SYM (rel->r_info);
8457 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8458 r_symndx, ibfd))
8459 {
8460 err_free_rel:
8461 if (elf_section_data (sec)->relocs != relstart)
8462 free (relstart);
8463 if (toc_ref != NULL)
8464 free (toc_ref);
8465 if (locsyms != NULL
8466 && (elf_symtab_hdr (ibfd).contents
8467 != (unsigned char *) locsyms))
8468 free (locsyms);
8469 return ret;
8470 }
8471
8472 if (h != NULL)
8473 {
8474 if (h->root.type == bfd_link_hash_defined
8475 || h->root.type == bfd_link_hash_defweak)
8476 value = h->root.u.def.value;
8477 else if (h->root.type == bfd_link_hash_undefweak)
8478 value = 0;
8479 else
8480 {
8481 found_tls_get_addr_arg = 0;
8482 continue;
8483 }
8484 }
8485 else
8486 /* Symbols referenced by TLS relocs must be of type
8487 STT_TLS. So no need for .opd local sym adjust. */
8488 value = sym->st_value;
8489
8490 ok_tprel = FALSE;
8491 is_local = FALSE;
8492 if (h == NULL
8493 || !h->def_dynamic)
8494 {
8495 is_local = TRUE;
8496 if (h != NULL
8497 && h->root.type == bfd_link_hash_undefweak)
8498 ok_tprel = TRUE;
8499 else if (sym_sec != NULL
8500 && sym_sec->output_section != NULL)
8501 {
8502 value += sym_sec->output_offset;
8503 value += sym_sec->output_section->vma;
8504 value -= htab->elf.tls_sec->vma;
8505 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8506 < (bfd_vma) 1 << 32);
8507 }
8508 }
8509
8510 r_type = ELF64_R_TYPE (rel->r_info);
8511 /* If this section has old-style __tls_get_addr calls
8512 without marker relocs, then check that each
8513 __tls_get_addr call reloc is preceded by a reloc
8514 that conceivably belongs to the __tls_get_addr arg
8515 setup insn. If we don't find matching arg setup
8516 relocs, don't do any tls optimization. */
8517 if (pass == 0
8518 && sec->has_tls_get_addr_call
8519 && h != NULL
8520 && (h == &htab->tls_get_addr->elf
8521 || h == &htab->tls_get_addr_fd->elf)
8522 && !found_tls_get_addr_arg
8523 && is_branch_reloc (r_type))
8524 {
8525 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8526 "TLS optimization disabled\n"),
8527 ibfd, sec, rel->r_offset);
8528 ret = TRUE;
8529 goto err_free_rel;
8530 }
8531
8532 found_tls_get_addr_arg = 0;
8533 switch (r_type)
8534 {
8535 case R_PPC64_GOT_TLSLD16:
8536 case R_PPC64_GOT_TLSLD16_LO:
8537 expecting_tls_get_addr = 1;
8538 found_tls_get_addr_arg = 1;
8539 /* Fall through. */
8540
8541 case R_PPC64_GOT_TLSLD16_HI:
8542 case R_PPC64_GOT_TLSLD16_HA:
8543 /* These relocs should never be against a symbol
8544 defined in a shared lib. Leave them alone if
8545 that turns out to be the case. */
8546 if (!is_local)
8547 continue;
8548
8549 /* LD -> LE */
8550 tls_set = 0;
8551 tls_clear = TLS_LD;
8552 tls_type = TLS_TLS | TLS_LD;
8553 break;
8554
8555 case R_PPC64_GOT_TLSGD16:
8556 case R_PPC64_GOT_TLSGD16_LO:
8557 expecting_tls_get_addr = 1;
8558 found_tls_get_addr_arg = 1;
8559 /* Fall through. */
8560
8561 case R_PPC64_GOT_TLSGD16_HI:
8562 case R_PPC64_GOT_TLSGD16_HA:
8563 if (ok_tprel)
8564 /* GD -> LE */
8565 tls_set = 0;
8566 else
8567 /* GD -> IE */
8568 tls_set = TLS_TLS | TLS_TPRELGD;
8569 tls_clear = TLS_GD;
8570 tls_type = TLS_TLS | TLS_GD;
8571 break;
8572
8573 case R_PPC64_GOT_TPREL16_DS:
8574 case R_PPC64_GOT_TPREL16_LO_DS:
8575 case R_PPC64_GOT_TPREL16_HI:
8576 case R_PPC64_GOT_TPREL16_HA:
8577 if (ok_tprel)
8578 {
8579 /* IE -> LE */
8580 tls_set = 0;
8581 tls_clear = TLS_TPREL;
8582 tls_type = TLS_TLS | TLS_TPREL;
8583 break;
8584 }
8585 continue;
8586
8587 case R_PPC64_TLSGD:
8588 case R_PPC64_TLSLD:
8589 found_tls_get_addr_arg = 1;
8590 /* Fall through. */
8591
8592 case R_PPC64_TLS:
8593 case R_PPC64_TOC16:
8594 case R_PPC64_TOC16_LO:
8595 if (sym_sec == NULL || sym_sec != toc)
8596 continue;
8597
8598 /* Mark this toc entry as referenced by a TLS
8599 code sequence. We can do that now in the
8600 case of R_PPC64_TLS, and after checking for
8601 tls_get_addr for the TOC16 relocs. */
8602 if (toc_ref == NULL)
8603 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8604 if (toc_ref == NULL)
8605 goto err_free_rel;
8606
8607 if (h != NULL)
8608 value = h->root.u.def.value;
8609 else
8610 value = sym->st_value;
8611 value += rel->r_addend;
8612 if (value % 8 != 0)
8613 continue;
8614 BFD_ASSERT (value < toc->size
8615 && toc->output_offset % 8 == 0);
8616 toc_ref_index = (value + toc->output_offset) / 8;
8617 if (r_type == R_PPC64_TLS
8618 || r_type == R_PPC64_TLSGD
8619 || r_type == R_PPC64_TLSLD)
8620 {
8621 toc_ref[toc_ref_index] = 1;
8622 continue;
8623 }
8624
8625 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8626 continue;
8627
8628 tls_set = 0;
8629 tls_clear = 0;
8630 expecting_tls_get_addr = 2;
8631 break;
8632
8633 case R_PPC64_TPREL64:
8634 if (pass == 0
8635 || sec != toc
8636 || toc_ref == NULL
8637 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8638 continue;
8639 if (ok_tprel)
8640 {
8641 /* IE -> LE */
8642 tls_set = TLS_EXPLICIT;
8643 tls_clear = TLS_TPREL;
8644 break;
8645 }
8646 continue;
8647
8648 case R_PPC64_DTPMOD64:
8649 if (pass == 0
8650 || sec != toc
8651 || toc_ref == NULL
8652 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8653 continue;
8654 if (rel + 1 < relend
8655 && (rel[1].r_info
8656 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8657 && rel[1].r_offset == rel->r_offset + 8)
8658 {
8659 if (ok_tprel)
8660 /* GD -> LE */
8661 tls_set = TLS_EXPLICIT | TLS_GD;
8662 else
8663 /* GD -> IE */
8664 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8665 tls_clear = TLS_GD;
8666 }
8667 else
8668 {
8669 if (!is_local)
8670 continue;
8671
8672 /* LD -> LE */
8673 tls_set = TLS_EXPLICIT;
8674 tls_clear = TLS_LD;
8675 }
8676 break;
8677
8678 default:
8679 continue;
8680 }
8681
8682 if (pass == 0)
8683 {
8684 if (!expecting_tls_get_addr
8685 || !sec->has_tls_get_addr_call)
8686 continue;
8687
8688 if (rel + 1 < relend
8689 && branch_reloc_hash_match (ibfd, rel + 1,
8690 htab->tls_get_addr,
8691 htab->tls_get_addr_fd))
8692 {
8693 if (expecting_tls_get_addr == 2)
8694 {
8695 /* Check for toc tls entries. */
8696 unsigned char *toc_tls;
8697 int retval;
8698
8699 retval = get_tls_mask (&toc_tls, NULL, NULL,
8700 &locsyms,
8701 rel, ibfd);
8702 if (retval == 0)
8703 goto err_free_rel;
8704 if (toc_tls != NULL)
8705 {
8706 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8707 found_tls_get_addr_arg = 1;
8708 if (retval > 1)
8709 toc_ref[toc_ref_index] = 1;
8710 }
8711 }
8712 continue;
8713 }
8714
8715 if (expecting_tls_get_addr != 1)
8716 continue;
8717
8718 /* Uh oh, we didn't find the expected call. We
8719 could just mark this symbol to exclude it
8720 from tls optimization but it's safer to skip
8721 the entire optimization. */
8722 /* xgettext:c-format */
8723 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8724 "TLS optimization disabled\n"),
8725 ibfd, sec, rel->r_offset);
8726 ret = TRUE;
8727 goto err_free_rel;
8728 }
8729
8730 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8731 {
8732 struct plt_entry *ent;
8733 for (ent = htab->tls_get_addr->elf.plt.plist;
8734 ent != NULL;
8735 ent = ent->next)
8736 if (ent->addend == 0)
8737 {
8738 if (ent->plt.refcount > 0)
8739 {
8740 ent->plt.refcount -= 1;
8741 expecting_tls_get_addr = 0;
8742 }
8743 break;
8744 }
8745 }
8746
8747 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8748 {
8749 struct plt_entry *ent;
8750 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8751 ent != NULL;
8752 ent = ent->next)
8753 if (ent->addend == 0)
8754 {
8755 if (ent->plt.refcount > 0)
8756 ent->plt.refcount -= 1;
8757 break;
8758 }
8759 }
8760
8761 if (tls_clear == 0)
8762 continue;
8763
8764 if ((tls_set & TLS_EXPLICIT) == 0)
8765 {
8766 struct got_entry *ent;
8767
8768 /* Adjust got entry for this reloc. */
8769 if (h != NULL)
8770 ent = h->got.glist;
8771 else
8772 ent = elf_local_got_ents (ibfd)[r_symndx];
8773
8774 for (; ent != NULL; ent = ent->next)
8775 if (ent->addend == rel->r_addend
8776 && ent->owner == ibfd
8777 && ent->tls_type == tls_type)
8778 break;
8779 if (ent == NULL)
8780 abort ();
8781
8782 if (tls_set == 0)
8783 {
8784 /* We managed to get rid of a got entry. */
8785 if (ent->got.refcount > 0)
8786 ent->got.refcount -= 1;
8787 }
8788 }
8789 else
8790 {
8791 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8792 we'll lose one or two dyn relocs. */
8793 if (!dec_dynrel_count (rel->r_info, sec, info,
8794 NULL, h, sym))
8795 return FALSE;
8796
8797 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8798 {
8799 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8800 NULL, h, sym))
8801 return FALSE;
8802 }
8803 }
8804
8805 *tls_mask |= tls_set;
8806 *tls_mask &= ~tls_clear;
8807 }
8808
8809 if (elf_section_data (sec)->relocs != relstart)
8810 free (relstart);
8811 }
8812
8813 if (locsyms != NULL
8814 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8815 {
8816 if (!info->keep_memory)
8817 free (locsyms);
8818 else
8819 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8820 }
8821 }
8822
8823 if (toc_ref != NULL)
8824 free (toc_ref);
8825 return TRUE;
8826}
8827
8828/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8829 the values of any global symbols in a toc section that has been
8830 edited. Globals in toc sections should be a rarity, so this function
8831 sets a flag if any are found in toc sections other than the one just
8832 edited, so that futher hash table traversals can be avoided. */
8833
8834struct adjust_toc_info
8835{
8836 asection *toc;
8837 unsigned long *skip;
8838 bfd_boolean global_toc_syms;
8839};
8840
8841enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8842
8843static bfd_boolean
8844adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8845{
8846 struct ppc_link_hash_entry *eh;
8847 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8848 unsigned long i;
8849
8850 if (h->root.type != bfd_link_hash_defined
8851 && h->root.type != bfd_link_hash_defweak)
8852 return TRUE;
8853
8854 eh = (struct ppc_link_hash_entry *) h;
8855 if (eh->adjust_done)
8856 return TRUE;
8857
8858 if (eh->elf.root.u.def.section == toc_inf->toc)
8859 {
8860 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8861 i = toc_inf->toc->rawsize >> 3;
8862 else
8863 i = eh->elf.root.u.def.value >> 3;
8864
8865 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8866 {
8867 _bfd_error_handler
8868 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8869 do
8870 ++i;
8871 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8872 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8873 }
8874
8875 eh->elf.root.u.def.value -= toc_inf->skip[i];
8876 eh->adjust_done = 1;
8877 }
8878 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8879 toc_inf->global_toc_syms = TRUE;
8880
8881 return TRUE;
8882}
8883
8884/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8885
8886static bfd_boolean
8887ok_lo_toc_insn (unsigned int insn)
8888{
8889 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8890 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8891 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8892 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8893 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8894 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8895 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8896 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8897 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8898 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8899 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8900 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8901 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8902 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8903 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8904 && (insn & 3) != 1)
8905 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8906 && ((insn & 3) == 0 || (insn & 3) == 3))
8907 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8908}
8909
8910/* Examine all relocs referencing .toc sections in order to remove
8911 unused .toc entries. */
8912
8913bfd_boolean
8914ppc64_elf_edit_toc (struct bfd_link_info *info)
8915{
8916 bfd *ibfd;
8917 struct adjust_toc_info toc_inf;
8918 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8919
8920 htab->do_toc_opt = 1;
8921 toc_inf.global_toc_syms = TRUE;
8922 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8923 {
8924 asection *toc, *sec;
8925 Elf_Internal_Shdr *symtab_hdr;
8926 Elf_Internal_Sym *local_syms;
8927 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8928 unsigned long *skip, *drop;
8929 unsigned char *used;
8930 unsigned char *keep, last, some_unused;
8931
8932 if (!is_ppc64_elf (ibfd))
8933 continue;
8934
8935 toc = bfd_get_section_by_name (ibfd, ".toc");
8936 if (toc == NULL
8937 || toc->size == 0
8938 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8939 || discarded_section (toc))
8940 continue;
8941
8942 toc_relocs = NULL;
8943 local_syms = NULL;
8944 symtab_hdr = &elf_symtab_hdr (ibfd);
8945
8946 /* Look at sections dropped from the final link. */
8947 skip = NULL;
8948 relstart = NULL;
8949 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8950 {
8951 if (sec->reloc_count == 0
8952 || !discarded_section (sec)
8953 || get_opd_info (sec)
8954 || (sec->flags & SEC_ALLOC) == 0
8955 || (sec->flags & SEC_DEBUGGING) != 0)
8956 continue;
8957
8958 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8959 if (relstart == NULL)
8960 goto error_ret;
8961
8962 /* Run through the relocs to see which toc entries might be
8963 unused. */
8964 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8965 {
8966 enum elf_ppc64_reloc_type r_type;
8967 unsigned long r_symndx;
8968 asection *sym_sec;
8969 struct elf_link_hash_entry *h;
8970 Elf_Internal_Sym *sym;
8971 bfd_vma val;
8972
8973 r_type = ELF64_R_TYPE (rel->r_info);
8974 switch (r_type)
8975 {
8976 default:
8977 continue;
8978
8979 case R_PPC64_TOC16:
8980 case R_PPC64_TOC16_LO:
8981 case R_PPC64_TOC16_HI:
8982 case R_PPC64_TOC16_HA:
8983 case R_PPC64_TOC16_DS:
8984 case R_PPC64_TOC16_LO_DS:
8985 break;
8986 }
8987
8988 r_symndx = ELF64_R_SYM (rel->r_info);
8989 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8990 r_symndx, ibfd))
8991 goto error_ret;
8992
8993 if (sym_sec != toc)
8994 continue;
8995
8996 if (h != NULL)
8997 val = h->root.u.def.value;
8998 else
8999 val = sym->st_value;
9000 val += rel->r_addend;
9001
9002 if (val >= toc->size)
9003 continue;
9004
9005 /* Anything in the toc ought to be aligned to 8 bytes.
9006 If not, don't mark as unused. */
9007 if (val & 7)
9008 continue;
9009
9010 if (skip == NULL)
9011 {
9012 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9013 if (skip == NULL)
9014 goto error_ret;
9015 }
9016
9017 skip[val >> 3] = ref_from_discarded;
9018 }
9019
9020 if (elf_section_data (sec)->relocs != relstart)
9021 free (relstart);
9022 }
9023
9024 /* For largetoc loads of address constants, we can convert
9025 . addis rx,2,addr@got@ha
9026 . ld ry,addr@got@l(rx)
9027 to
9028 . addis rx,2,addr@toc@ha
9029 . addi ry,rx,addr@toc@l
9030 when addr is within 2G of the toc pointer. This then means
9031 that the word storing "addr" in the toc is no longer needed. */
9032
9033 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9034 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9035 && toc->reloc_count != 0)
9036 {
9037 /* Read toc relocs. */
9038 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9039 info->keep_memory);
9040 if (toc_relocs == NULL)
9041 goto error_ret;
9042
9043 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9044 {
9045 enum elf_ppc64_reloc_type r_type;
9046 unsigned long r_symndx;
9047 asection *sym_sec;
9048 struct elf_link_hash_entry *h;
9049 Elf_Internal_Sym *sym;
9050 bfd_vma val, addr;
9051
9052 r_type = ELF64_R_TYPE (rel->r_info);
9053 if (r_type != R_PPC64_ADDR64)
9054 continue;
9055
9056 r_symndx = ELF64_R_SYM (rel->r_info);
9057 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9058 r_symndx, ibfd))
9059 goto error_ret;
9060
9061 if (sym_sec == NULL
9062 || sym_sec->output_section == NULL
9063 || discarded_section (sym_sec))
9064 continue;
9065
9066 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9067 continue;
9068
9069 if (h != NULL)
9070 {
9071 if (h->type == STT_GNU_IFUNC)
9072 continue;
9073 val = h->root.u.def.value;
9074 }
9075 else
9076 {
9077 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9078 continue;
9079 val = sym->st_value;
9080 }
9081 val += rel->r_addend;
9082 val += sym_sec->output_section->vma + sym_sec->output_offset;
9083
9084 /* We don't yet know the exact toc pointer value, but we
9085 know it will be somewhere in the toc section. Don't
9086 optimize if the difference from any possible toc
9087 pointer is outside [ff..f80008000, 7fff7fff]. */
9088 addr = toc->output_section->vma + TOC_BASE_OFF;
9089 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9090 continue;
9091
9092 addr = toc->output_section->vma + toc->output_section->rawsize;
9093 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9094 continue;
9095
9096 if (skip == NULL)
9097 {
9098 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9099 if (skip == NULL)
9100 goto error_ret;
9101 }
9102
9103 skip[rel->r_offset >> 3]
9104 |= can_optimize | ((rel - toc_relocs) << 2);
9105 }
9106 }
9107
9108 if (skip == NULL)
9109 continue;
9110
9111 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9112 if (used == NULL)
9113 {
9114 error_ret:
9115 if (local_syms != NULL
9116 && symtab_hdr->contents != (unsigned char *) local_syms)
9117 free (local_syms);
9118 if (sec != NULL
9119 && relstart != NULL
9120 && elf_section_data (sec)->relocs != relstart)
9121 free (relstart);
9122 if (toc_relocs != NULL
9123 && elf_section_data (toc)->relocs != toc_relocs)
9124 free (toc_relocs);
9125 if (skip != NULL)
9126 free (skip);
9127 return FALSE;
9128 }
9129
9130 /* Now check all kept sections that might reference the toc.
9131 Check the toc itself last. */
9132 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9133 : ibfd->sections);
9134 sec != NULL;
9135 sec = (sec == toc ? NULL
9136 : sec->next == NULL ? toc
9137 : sec->next == toc && toc->next ? toc->next
9138 : sec->next))
9139 {
9140 int repeat;
9141
9142 if (sec->reloc_count == 0
9143 || discarded_section (sec)
9144 || get_opd_info (sec)
9145 || (sec->flags & SEC_ALLOC) == 0
9146 || (sec->flags & SEC_DEBUGGING) != 0)
9147 continue;
9148
9149 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9150 info->keep_memory);
9151 if (relstart == NULL)
9152 {
9153 free (used);
9154 goto error_ret;
9155 }
9156
9157 /* Mark toc entries referenced as used. */
9158 do
9159 {
9160 repeat = 0;
9161 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9162 {
9163 enum elf_ppc64_reloc_type r_type;
9164 unsigned long r_symndx;
9165 asection *sym_sec;
9166 struct elf_link_hash_entry *h;
9167 Elf_Internal_Sym *sym;
9168 bfd_vma val;
9169 enum {no_check, check_lo, check_ha} insn_check;
9170
9171 r_type = ELF64_R_TYPE (rel->r_info);
9172 switch (r_type)
9173 {
9174 default:
9175 insn_check = no_check;
9176 break;
9177
9178 case R_PPC64_GOT_TLSLD16_HA:
9179 case R_PPC64_GOT_TLSGD16_HA:
9180 case R_PPC64_GOT_TPREL16_HA:
9181 case R_PPC64_GOT_DTPREL16_HA:
9182 case R_PPC64_GOT16_HA:
9183 case R_PPC64_TOC16_HA:
9184 insn_check = check_ha;
9185 break;
9186
9187 case R_PPC64_GOT_TLSLD16_LO:
9188 case R_PPC64_GOT_TLSGD16_LO:
9189 case R_PPC64_GOT_TPREL16_LO_DS:
9190 case R_PPC64_GOT_DTPREL16_LO_DS:
9191 case R_PPC64_GOT16_LO:
9192 case R_PPC64_GOT16_LO_DS:
9193 case R_PPC64_TOC16_LO:
9194 case R_PPC64_TOC16_LO_DS:
9195 insn_check = check_lo;
9196 break;
9197 }
9198
9199 if (insn_check != no_check)
9200 {
9201 bfd_vma off = rel->r_offset & ~3;
9202 unsigned char buf[4];
9203 unsigned int insn;
9204
9205 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9206 {
9207 free (used);
9208 goto error_ret;
9209 }
9210 insn = bfd_get_32 (ibfd, buf);
9211 if (insn_check == check_lo
9212 ? !ok_lo_toc_insn (insn)
9213 : ((insn & ((0x3f << 26) | 0x1f << 16))
9214 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9215 {
9216 char str[12];
9217
9218 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9219 sprintf (str, "%#08x", insn);
9220 info->callbacks->einfo
9221 /* xgettext:c-format */
9222 (_("%P: %H: toc optimization is not supported for"
9223 " %s instruction.\n"),
9224 ibfd, sec, rel->r_offset & ~3, str);
9225 }
9226 }
9227
9228 switch (r_type)
9229 {
9230 case R_PPC64_TOC16:
9231 case R_PPC64_TOC16_LO:
9232 case R_PPC64_TOC16_HI:
9233 case R_PPC64_TOC16_HA:
9234 case R_PPC64_TOC16_DS:
9235 case R_PPC64_TOC16_LO_DS:
9236 /* In case we're taking addresses of toc entries. */
9237 case R_PPC64_ADDR64:
9238 break;
9239
9240 default:
9241 continue;
9242 }
9243
9244 r_symndx = ELF64_R_SYM (rel->r_info);
9245 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9246 r_symndx, ibfd))
9247 {
9248 free (used);
9249 goto error_ret;
9250 }
9251
9252 if (sym_sec != toc)
9253 continue;
9254
9255 if (h != NULL)
9256 val = h->root.u.def.value;
9257 else
9258 val = sym->st_value;
9259 val += rel->r_addend;
9260
9261 if (val >= toc->size)
9262 continue;
9263
9264 if ((skip[val >> 3] & can_optimize) != 0)
9265 {
9266 bfd_vma off;
9267 unsigned char opc;
9268
9269 switch (r_type)
9270 {
9271 case R_PPC64_TOC16_HA:
9272 break;
9273
9274 case R_PPC64_TOC16_LO_DS:
9275 off = rel->r_offset;
9276 off += (bfd_big_endian (ibfd) ? -2 : 3);
9277 if (!bfd_get_section_contents (ibfd, sec, &opc,
9278 off, 1))
9279 {
9280 free (used);
9281 goto error_ret;
9282 }
9283 if ((opc & (0x3f << 2)) == (58u << 2))
9284 break;
9285 /* Fall through. */
9286
9287 default:
9288 /* Wrong sort of reloc, or not a ld. We may
9289 as well clear ref_from_discarded too. */
9290 skip[val >> 3] = 0;
9291 }
9292 }
9293
9294 if (sec != toc)
9295 used[val >> 3] = 1;
9296 /* For the toc section, we only mark as used if this
9297 entry itself isn't unused. */
9298 else if ((used[rel->r_offset >> 3]
9299 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9300 && !used[val >> 3])
9301 {
9302 /* Do all the relocs again, to catch reference
9303 chains. */
9304 repeat = 1;
9305 used[val >> 3] = 1;
9306 }
9307 }
9308 }
9309 while (repeat);
9310
9311 if (elf_section_data (sec)->relocs != relstart)
9312 free (relstart);
9313 }
9314
9315 /* Merge the used and skip arrays. Assume that TOC
9316 doublewords not appearing as either used or unused belong
9317 to to an entry more than one doubleword in size. */
9318 for (drop = skip, keep = used, last = 0, some_unused = 0;
9319 drop < skip + (toc->size + 7) / 8;
9320 ++drop, ++keep)
9321 {
9322 if (*keep)
9323 {
9324 *drop &= ~ref_from_discarded;
9325 if ((*drop & can_optimize) != 0)
9326 some_unused = 1;
9327 last = 0;
9328 }
9329 else if ((*drop & ref_from_discarded) != 0)
9330 {
9331 some_unused = 1;
9332 last = ref_from_discarded;
9333 }
9334 else
9335 *drop = last;
9336 }
9337
9338 free (used);
9339
9340 if (some_unused)
9341 {
9342 bfd_byte *contents, *src;
9343 unsigned long off;
9344 Elf_Internal_Sym *sym;
9345 bfd_boolean local_toc_syms = FALSE;
9346
9347 /* Shuffle the toc contents, and at the same time convert the
9348 skip array from booleans into offsets. */
9349 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9350 goto error_ret;
9351
9352 elf_section_data (toc)->this_hdr.contents = contents;
9353
9354 for (src = contents, off = 0, drop = skip;
9355 src < contents + toc->size;
9356 src += 8, ++drop)
9357 {
9358 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9359 off += 8;
9360 else if (off != 0)
9361 {
9362 *drop = off;
9363 memcpy (src - off, src, 8);
9364 }
9365 }
9366 *drop = off;
9367 toc->rawsize = toc->size;
9368 toc->size = src - contents - off;
9369
9370 /* Adjust addends for relocs against the toc section sym,
9371 and optimize any accesses we can. */
9372 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9373 {
9374 if (sec->reloc_count == 0
9375 || discarded_section (sec))
9376 continue;
9377
9378 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9379 info->keep_memory);
9380 if (relstart == NULL)
9381 goto error_ret;
9382
9383 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9384 {
9385 enum elf_ppc64_reloc_type r_type;
9386 unsigned long r_symndx;
9387 asection *sym_sec;
9388 struct elf_link_hash_entry *h;
9389 bfd_vma val;
9390
9391 r_type = ELF64_R_TYPE (rel->r_info);
9392 switch (r_type)
9393 {
9394 default:
9395 continue;
9396
9397 case R_PPC64_TOC16:
9398 case R_PPC64_TOC16_LO:
9399 case R_PPC64_TOC16_HI:
9400 case R_PPC64_TOC16_HA:
9401 case R_PPC64_TOC16_DS:
9402 case R_PPC64_TOC16_LO_DS:
9403 case R_PPC64_ADDR64:
9404 break;
9405 }
9406
9407 r_symndx = ELF64_R_SYM (rel->r_info);
9408 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9409 r_symndx, ibfd))
9410 goto error_ret;
9411
9412 if (sym_sec != toc)
9413 continue;
9414
9415 if (h != NULL)
9416 val = h->root.u.def.value;
9417 else
9418 {
9419 val = sym->st_value;
9420 if (val != 0)
9421 local_toc_syms = TRUE;
9422 }
9423
9424 val += rel->r_addend;
9425
9426 if (val > toc->rawsize)
9427 val = toc->rawsize;
9428 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9429 continue;
9430 else if ((skip[val >> 3] & can_optimize) != 0)
9431 {
9432 Elf_Internal_Rela *tocrel
9433 = toc_relocs + (skip[val >> 3] >> 2);
9434 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9435
9436 switch (r_type)
9437 {
9438 case R_PPC64_TOC16_HA:
9439 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9440 break;
9441
9442 case R_PPC64_TOC16_LO_DS:
9443 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9444 break;
9445
9446 default:
9447 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9448 ppc_howto_init ();
9449 info->callbacks->einfo
9450 /* xgettext:c-format */
9451 (_("%P: %H: %s references "
9452 "optimized away TOC entry\n"),
9453 ibfd, sec, rel->r_offset,
9454 ppc64_elf_howto_table[r_type]->name);
9455 bfd_set_error (bfd_error_bad_value);
9456 goto error_ret;
9457 }
9458 rel->r_addend = tocrel->r_addend;
9459 elf_section_data (sec)->relocs = relstart;
9460 continue;
9461 }
9462
9463 if (h != NULL || sym->st_value != 0)
9464 continue;
9465
9466 rel->r_addend -= skip[val >> 3];
9467 elf_section_data (sec)->relocs = relstart;
9468 }
9469
9470 if (elf_section_data (sec)->relocs != relstart)
9471 free (relstart);
9472 }
9473
9474 /* We shouldn't have local or global symbols defined in the TOC,
9475 but handle them anyway. */
9476 if (local_syms != NULL)
9477 for (sym = local_syms;
9478 sym < local_syms + symtab_hdr->sh_info;
9479 ++sym)
9480 if (sym->st_value != 0
9481 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9482 {
9483 unsigned long i;
9484
9485 if (sym->st_value > toc->rawsize)
9486 i = toc->rawsize >> 3;
9487 else
9488 i = sym->st_value >> 3;
9489
9490 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9491 {
9492 if (local_toc_syms)
9493 _bfd_error_handler
9494 (_("%s defined on removed toc entry"),
9495 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9496 do
9497 ++i;
9498 while ((skip[i] & (ref_from_discarded | can_optimize)));
9499 sym->st_value = (bfd_vma) i << 3;
9500 }
9501
9502 sym->st_value -= skip[i];
9503 symtab_hdr->contents = (unsigned char *) local_syms;
9504 }
9505
9506 /* Adjust any global syms defined in this toc input section. */
9507 if (toc_inf.global_toc_syms)
9508 {
9509 toc_inf.toc = toc;
9510 toc_inf.skip = skip;
9511 toc_inf.global_toc_syms = FALSE;
9512 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9513 &toc_inf);
9514 }
9515
9516 if (toc->reloc_count != 0)
9517 {
9518 Elf_Internal_Shdr *rel_hdr;
9519 Elf_Internal_Rela *wrel;
9520 bfd_size_type sz;
9521
9522 /* Remove unused toc relocs, and adjust those we keep. */
9523 if (toc_relocs == NULL)
9524 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9525 info->keep_memory);
9526 if (toc_relocs == NULL)
9527 goto error_ret;
9528
9529 wrel = toc_relocs;
9530 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9531 if ((skip[rel->r_offset >> 3]
9532 & (ref_from_discarded | can_optimize)) == 0)
9533 {
9534 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9535 wrel->r_info = rel->r_info;
9536 wrel->r_addend = rel->r_addend;
9537 ++wrel;
9538 }
9539 else if (!dec_dynrel_count (rel->r_info, toc, info,
9540 &local_syms, NULL, NULL))
9541 goto error_ret;
9542
9543 elf_section_data (toc)->relocs = toc_relocs;
9544 toc->reloc_count = wrel - toc_relocs;
9545 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9546 sz = rel_hdr->sh_entsize;
9547 rel_hdr->sh_size = toc->reloc_count * sz;
9548 }
9549 }
9550 else if (toc_relocs != NULL
9551 && elf_section_data (toc)->relocs != toc_relocs)
9552 free (toc_relocs);
9553
9554 if (local_syms != NULL
9555 && symtab_hdr->contents != (unsigned char *) local_syms)
9556 {
9557 if (!info->keep_memory)
9558 free (local_syms);
9559 else
9560 symtab_hdr->contents = (unsigned char *) local_syms;
9561 }
9562 free (skip);
9563 }
9564
9565 return TRUE;
9566}
9567
9568/* Return true iff input section I references the TOC using
9569 instructions limited to +/-32k offsets. */
9570
9571bfd_boolean
9572ppc64_elf_has_small_toc_reloc (asection *i)
9573{
9574 return (is_ppc64_elf (i->owner)
9575 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9576}
9577
9578/* Allocate space for one GOT entry. */
9579
9580static void
9581allocate_got (struct elf_link_hash_entry *h,
9582 struct bfd_link_info *info,
9583 struct got_entry *gent)
9584{
9585 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9586 bfd_boolean dyn;
9587 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9588 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9589 ? 16 : 8);
9590 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9591 ? 2 : 1) * sizeof (Elf64_External_Rela);
9592 asection *got = ppc64_elf_tdata (gent->owner)->got;
9593
9594 gent->got.offset = got->size;
9595 got->size += entsize;
9596
9597 dyn = htab->elf.dynamic_sections_created;
9598 if (h->type == STT_GNU_IFUNC)
9599 {
9600 htab->elf.irelplt->size += rentsize;
9601 htab->got_reli_size += rentsize;
9602 }
9603 else if ((bfd_link_pic (info)
9604 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9605 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9606 || h->root.type != bfd_link_hash_undefweak))
9607 {
9608 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9609 relgot->size += rentsize;
9610 }
9611}
9612
9613/* This function merges got entries in the same toc group. */
9614
9615static void
9616merge_got_entries (struct got_entry **pent)
9617{
9618 struct got_entry *ent, *ent2;
9619
9620 for (ent = *pent; ent != NULL; ent = ent->next)
9621 if (!ent->is_indirect)
9622 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9623 if (!ent2->is_indirect
9624 && ent2->addend == ent->addend
9625 && ent2->tls_type == ent->tls_type
9626 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9627 {
9628 ent2->is_indirect = TRUE;
9629 ent2->got.ent = ent;
9630 }
9631}
9632
9633/* Allocate space in .plt, .got and associated reloc sections for
9634 dynamic relocs. */
9635
9636static bfd_boolean
9637allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9638{
9639 struct bfd_link_info *info;
9640 struct ppc_link_hash_table *htab;
9641 asection *s;
9642 struct ppc_link_hash_entry *eh;
9643 struct got_entry **pgent, *gent;
9644
9645 if (h->root.type == bfd_link_hash_indirect)
9646 return TRUE;
9647
9648 info = (struct bfd_link_info *) inf;
9649 htab = ppc_hash_table (info);
9650 if (htab == NULL)
9651 return FALSE;
9652
9653 eh = (struct ppc_link_hash_entry *) h;
9654 /* Run through the TLS GD got entries first if we're changing them
9655 to TPREL. */
9656 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9657 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9658 if (gent->got.refcount > 0
9659 && (gent->tls_type & TLS_GD) != 0)
9660 {
9661 /* This was a GD entry that has been converted to TPREL. If
9662 there happens to be a TPREL entry we can use that one. */
9663 struct got_entry *ent;
9664 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9665 if (ent->got.refcount > 0
9666 && (ent->tls_type & TLS_TPREL) != 0
9667 && ent->addend == gent->addend
9668 && ent->owner == gent->owner)
9669 {
9670 gent->got.refcount = 0;
9671 break;
9672 }
9673
9674 /* If not, then we'll be using our own TPREL entry. */
9675 if (gent->got.refcount != 0)
9676 gent->tls_type = TLS_TLS | TLS_TPREL;
9677 }
9678
9679 /* Remove any list entry that won't generate a word in the GOT before
9680 we call merge_got_entries. Otherwise we risk merging to empty
9681 entries. */
9682 pgent = &h->got.glist;
9683 while ((gent = *pgent) != NULL)
9684 if (gent->got.refcount > 0)
9685 {
9686 if ((gent->tls_type & TLS_LD) != 0
9687 && !h->def_dynamic)
9688 {
9689 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9690 *pgent = gent->next;
9691 }
9692 else
9693 pgent = &gent->next;
9694 }
9695 else
9696 *pgent = gent->next;
9697
9698 if (!htab->do_multi_toc)
9699 merge_got_entries (&h->got.glist);
9700
9701 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9702 if (!gent->is_indirect)
9703 {
9704 /* Make sure this symbol is output as a dynamic symbol.
9705 Undefined weak syms won't yet be marked as dynamic,
9706 nor will all TLS symbols. */
9707 if (h->dynindx == -1
9708 && !h->forced_local
9709 && h->type != STT_GNU_IFUNC
9710 && htab->elf.dynamic_sections_created)
9711 {
9712 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9713 return FALSE;
9714 }
9715
9716 if (!is_ppc64_elf (gent->owner))
9717 abort ();
9718
9719 allocate_got (h, info, gent);
9720 }
9721
9722 if (!htab->elf.dynamic_sections_created
9723 && h->type != STT_GNU_IFUNC)
9724 eh->dyn_relocs = NULL;
9725
9726 if (eh->dyn_relocs != NULL)
9727 {
9728 struct elf_dyn_relocs *p, **pp;
9729
9730 /* In the shared -Bsymbolic case, discard space allocated for
9731 dynamic pc-relative relocs against symbols which turn out to
9732 be defined in regular objects. For the normal shared case,
9733 discard space for relocs that have become local due to symbol
9734 visibility changes. */
9735
9736 if (bfd_link_pic (info))
9737 {
9738 /* Relocs that use pc_count are those that appear on a call
9739 insn, or certain REL relocs (see must_be_dyn_reloc) that
9740 can be generated via assembly. We want calls to
9741 protected symbols to resolve directly to the function
9742 rather than going via the plt. If people want function
9743 pointer comparisons to work as expected then they should
9744 avoid writing weird assembly. */
9745 if (SYMBOL_CALLS_LOCAL (info, h))
9746 {
9747 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9748 {
9749 p->count -= p->pc_count;
9750 p->pc_count = 0;
9751 if (p->count == 0)
9752 *pp = p->next;
9753 else
9754 pp = &p->next;
9755 }
9756 }
9757
9758 /* Also discard relocs on undefined weak syms with
9759 non-default visibility. */
9760 if (eh->dyn_relocs != NULL
9761 && h->root.type == bfd_link_hash_undefweak)
9762 {
9763 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9764 eh->dyn_relocs = NULL;
9765
9766 /* Make sure this symbol is output as a dynamic symbol.
9767 Undefined weak syms won't yet be marked as dynamic. */
9768 else if (h->dynindx == -1
9769 && !h->forced_local)
9770 {
9771 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9772 return FALSE;
9773 }
9774 }
9775 }
9776 else if (h->type == STT_GNU_IFUNC)
9777 {
9778 /* A plt entry is always created when making direct calls to
9779 an ifunc, even when building a static executable, but
9780 that doesn't cover all cases. We may have only an ifunc
9781 initialised function pointer for a given ifunc symbol.
9782
9783 For ELFv2, dynamic relocations are not required when
9784 generating a global entry PLT stub. */
9785 if (abiversion (info->output_bfd) >= 2)
9786 {
9787 if (global_entry_stub (h))
9788 eh->dyn_relocs = NULL;
9789 }
9790
9791 /* For ELFv1 we have function descriptors. Descriptors need
9792 to be treated like PLT entries and thus have dynamic
9793 relocations. One exception is when the function
9794 descriptor is copied into .dynbss (which should only
9795 happen with ancient versions of gcc). */
9796 else if (h->needs_copy)
9797 eh->dyn_relocs = NULL;
9798 }
9799 else if (ELIMINATE_COPY_RELOCS)
9800 {
9801 /* For the non-pic case, discard space for relocs against
9802 symbols which turn out to need copy relocs or are not
9803 dynamic. */
9804
9805 /* First make sure this symbol is output as a dynamic symbol.
9806 Undefined weak syms won't yet be marked as dynamic. */
9807 if (h->root.type == bfd_link_hash_undefweak
9808 && !h->non_got_ref
9809 && !h->def_regular
9810 && h->dynindx == -1
9811 && !h->forced_local
9812 && !bfd_elf_link_record_dynamic_symbol (info, h))
9813 return FALSE;
9814
9815 if (h->non_got_ref
9816 || h->def_regular
9817 || h->dynindx == -1)
9818 eh->dyn_relocs = NULL;
9819 }
9820
9821 /* Finally, allocate space. */
9822 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9823 {
9824 asection *sreloc = elf_section_data (p->sec)->sreloc;
9825 if (eh->elf.type == STT_GNU_IFUNC)
9826 sreloc = htab->elf.irelplt;
9827 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9828 }
9829 }
9830
9831 if ((htab->elf.dynamic_sections_created
9832 && h->dynindx != -1
9833 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9834 || h->type == STT_GNU_IFUNC)
9835 {
9836 struct plt_entry *pent;
9837 bfd_boolean doneone = FALSE;
9838 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9839 if (pent->plt.refcount > 0)
9840 {
9841 if (!htab->elf.dynamic_sections_created
9842 || h->dynindx == -1)
9843 {
9844 s = htab->elf.iplt;
9845 pent->plt.offset = s->size;
9846 s->size += PLT_ENTRY_SIZE (htab);
9847 s = htab->elf.irelplt;
9848 }
9849 else
9850 {
9851 /* If this is the first .plt entry, make room for the special
9852 first entry. */
9853 s = htab->elf.splt;
9854 if (s->size == 0)
9855 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9856
9857 pent->plt.offset = s->size;
9858
9859 /* Make room for this entry. */
9860 s->size += PLT_ENTRY_SIZE (htab);
9861
9862 /* Make room for the .glink code. */
9863 s = htab->glink;
9864 if (s->size == 0)
9865 s->size += GLINK_CALL_STUB_SIZE;
9866 if (htab->opd_abi)
9867 {
9868 /* We need bigger stubs past index 32767. */
9869 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9870 s->size += 4;
9871 s->size += 2*4;
9872 }
9873 else
9874 s->size += 4;
9875
9876 /* We also need to make an entry in the .rela.plt section. */
9877 s = htab->elf.srelplt;
9878 }
9879 s->size += sizeof (Elf64_External_Rela);
9880 doneone = TRUE;
9881 }
9882 else
9883 pent->plt.offset = (bfd_vma) -1;
9884 if (!doneone)
9885 {
9886 h->plt.plist = NULL;
9887 h->needs_plt = 0;
9888 }
9889 }
9890 else
9891 {
9892 h->plt.plist = NULL;
9893 h->needs_plt = 0;
9894 }
9895
9896 return TRUE;
9897}
9898
9899/* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9900 to set up space for global entry stubs. These are put in glink,
9901 after the branch table. */
9902
9903static bfd_boolean
9904size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9905{
9906 struct bfd_link_info *info;
9907 struct ppc_link_hash_table *htab;
9908 struct plt_entry *pent;
9909 asection *s;
9910
9911 if (h->root.type == bfd_link_hash_indirect)
9912 return TRUE;
9913
9914 if (!h->pointer_equality_needed)
9915 return TRUE;
9916
9917 if (h->def_regular)
9918 return TRUE;
9919
9920 info = inf;
9921 htab = ppc_hash_table (info);
9922 if (htab == NULL)
9923 return FALSE;
9924
9925 s = htab->glink;
9926 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9927 if (pent->plt.offset != (bfd_vma) -1
9928 && pent->addend == 0)
9929 {
9930 /* For ELFv2, if this symbol is not defined in a regular file
9931 and we are not generating a shared library or pie, then we
9932 need to define the symbol in the executable on a call stub.
9933 This is to avoid text relocations. */
9934 s->size = (s->size + 15) & -16;
9935 h->root.type = bfd_link_hash_defined;
9936 h->root.u.def.section = s;
9937 h->root.u.def.value = s->size;
9938 s->size += 16;
9939 break;
9940 }
9941 return TRUE;
9942}
9943
9944/* Set DF_TEXTREL if we find any dynamic relocs that apply to
9945 read-only sections. */
9946
9947static bfd_boolean
9948maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9949{
9950 if (h->root.type == bfd_link_hash_indirect)
9951 return TRUE;
9952
9953 if (readonly_dynrelocs (h))
9954 {
9955 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9956
9957 /* Not an error, just cut short the traversal. */
9958 return FALSE;
9959 }
9960 return TRUE;
9961}
9962
9963/* Set the sizes of the dynamic sections. */
9964
9965static bfd_boolean
9966ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9967 struct bfd_link_info *info)
9968{
9969 struct ppc_link_hash_table *htab;
9970 bfd *dynobj;
9971 asection *s;
9972 bfd_boolean relocs;
9973 bfd *ibfd;
9974 struct got_entry *first_tlsld;
9975
9976 htab = ppc_hash_table (info);
9977 if (htab == NULL)
9978 return FALSE;
9979
9980 dynobj = htab->elf.dynobj;
9981 if (dynobj == NULL)
9982 abort ();
9983
9984 if (htab->elf.dynamic_sections_created)
9985 {
9986 /* Set the contents of the .interp section to the interpreter. */
9987 if (bfd_link_executable (info) && !info->nointerp)
9988 {
9989 s = bfd_get_linker_section (dynobj, ".interp");
9990 if (s == NULL)
9991 abort ();
9992 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9993 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9994 }
9995 }
9996
9997 /* Set up .got offsets for local syms, and space for local dynamic
9998 relocs. */
9999 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10000 {
10001 struct got_entry **lgot_ents;
10002 struct got_entry **end_lgot_ents;
10003 struct plt_entry **local_plt;
10004 struct plt_entry **end_local_plt;
10005 unsigned char *lgot_masks;
10006 bfd_size_type locsymcount;
10007 Elf_Internal_Shdr *symtab_hdr;
10008
10009 if (!is_ppc64_elf (ibfd))
10010 continue;
10011
10012 for (s = ibfd->sections; s != NULL; s = s->next)
10013 {
10014 struct ppc_dyn_relocs *p;
10015
10016 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10017 {
10018 if (!bfd_is_abs_section (p->sec)
10019 && bfd_is_abs_section (p->sec->output_section))
10020 {
10021 /* Input section has been discarded, either because
10022 it is a copy of a linkonce section or due to
10023 linker script /DISCARD/, so we'll be discarding
10024 the relocs too. */
10025 }
10026 else if (p->count != 0)
10027 {
10028 asection *srel = elf_section_data (p->sec)->sreloc;
10029 if (p->ifunc)
10030 srel = htab->elf.irelplt;
10031 srel->size += p->count * sizeof (Elf64_External_Rela);
10032 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10033 info->flags |= DF_TEXTREL;
10034 }
10035 }
10036 }
10037
10038 lgot_ents = elf_local_got_ents (ibfd);
10039 if (!lgot_ents)
10040 continue;
10041
10042 symtab_hdr = &elf_symtab_hdr (ibfd);
10043 locsymcount = symtab_hdr->sh_info;
10044 end_lgot_ents = lgot_ents + locsymcount;
10045 local_plt = (struct plt_entry **) end_lgot_ents;
10046 end_local_plt = local_plt + locsymcount;
10047 lgot_masks = (unsigned char *) end_local_plt;
10048 s = ppc64_elf_tdata (ibfd)->got;
10049 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10050 {
10051 struct got_entry **pent, *ent;
10052
10053 pent = lgot_ents;
10054 while ((ent = *pent) != NULL)
10055 if (ent->got.refcount > 0)
10056 {
10057 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10058 {
10059 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10060 *pent = ent->next;
10061 }
10062 else
10063 {
10064 unsigned int ent_size = 8;
10065 unsigned int rel_size = sizeof (Elf64_External_Rela);
10066
10067 ent->got.offset = s->size;
10068 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10069 {
10070 ent_size *= 2;
10071 rel_size *= 2;
10072 }
10073 s->size += ent_size;
10074 if ((*lgot_masks & PLT_IFUNC) != 0)
10075 {
10076 htab->elf.irelplt->size += rel_size;
10077 htab->got_reli_size += rel_size;
10078 }
10079 else if (bfd_link_pic (info))
10080 {
10081 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10082 srel->size += rel_size;
10083 }
10084 pent = &ent->next;
10085 }
10086 }
10087 else
10088 *pent = ent->next;
10089 }
10090
10091 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10092 for (; local_plt < end_local_plt; ++local_plt)
10093 {
10094 struct plt_entry *ent;
10095
10096 for (ent = *local_plt; ent != NULL; ent = ent->next)
10097 if (ent->plt.refcount > 0)
10098 {
10099 s = htab->elf.iplt;
10100 ent->plt.offset = s->size;
10101 s->size += PLT_ENTRY_SIZE (htab);
10102
10103 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10104 }
10105 else
10106 ent->plt.offset = (bfd_vma) -1;
10107 }
10108 }
10109
10110 /* Allocate global sym .plt and .got entries, and space for global
10111 sym dynamic relocs. */
10112 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10113 /* Stash the end of glink branch table. */
10114 if (htab->glink != NULL)
10115 htab->glink->rawsize = htab->glink->size;
10116
10117 if (!htab->opd_abi && !bfd_link_pic (info))
10118 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10119
10120 first_tlsld = NULL;
10121 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10122 {
10123 struct got_entry *ent;
10124
10125 if (!is_ppc64_elf (ibfd))
10126 continue;
10127
10128 ent = ppc64_tlsld_got (ibfd);
10129 if (ent->got.refcount > 0)
10130 {
10131 if (!htab->do_multi_toc && first_tlsld != NULL)
10132 {
10133 ent->is_indirect = TRUE;
10134 ent->got.ent = first_tlsld;
10135 }
10136 else
10137 {
10138 if (first_tlsld == NULL)
10139 first_tlsld = ent;
10140 s = ppc64_elf_tdata (ibfd)->got;
10141 ent->got.offset = s->size;
10142 ent->owner = ibfd;
10143 s->size += 16;
10144 if (bfd_link_pic (info))
10145 {
10146 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10147 srel->size += sizeof (Elf64_External_Rela);
10148 }
10149 }
10150 }
10151 else
10152 ent->got.offset = (bfd_vma) -1;
10153 }
10154
10155 /* We now have determined the sizes of the various dynamic sections.
10156 Allocate memory for them. */
10157 relocs = FALSE;
10158 for (s = dynobj->sections; s != NULL; s = s->next)
10159 {
10160 if ((s->flags & SEC_LINKER_CREATED) == 0)
10161 continue;
10162
10163 if (s == htab->brlt || s == htab->relbrlt)
10164 /* These haven't been allocated yet; don't strip. */
10165 continue;
10166 else if (s == htab->elf.sgot
10167 || s == htab->elf.splt
10168 || s == htab->elf.iplt
10169 || s == htab->glink
10170 || s == htab->dynbss)
10171 {
10172 /* Strip this section if we don't need it; see the
10173 comment below. */
10174 }
10175 else if (s == htab->glink_eh_frame)
10176 {
10177 if (!bfd_is_abs_section (s->output_section))
10178 /* Not sized yet. */
10179 continue;
10180 }
10181 else if (CONST_STRNEQ (s->name, ".rela"))
10182 {
10183 if (s->size != 0)
10184 {
10185 if (s != htab->elf.srelplt)
10186 relocs = TRUE;
10187
10188 /* We use the reloc_count field as a counter if we need
10189 to copy relocs into the output file. */
10190 s->reloc_count = 0;
10191 }
10192 }
10193 else
10194 {
10195 /* It's not one of our sections, so don't allocate space. */
10196 continue;
10197 }
10198
10199 if (s->size == 0)
10200 {
10201 /* If we don't need this section, strip it from the
10202 output file. This is mostly to handle .rela.bss and
10203 .rela.plt. We must create both sections in
10204 create_dynamic_sections, because they must be created
10205 before the linker maps input sections to output
10206 sections. The linker does that before
10207 adjust_dynamic_symbol is called, and it is that
10208 function which decides whether anything needs to go
10209 into these sections. */
10210 s->flags |= SEC_EXCLUDE;
10211 continue;
10212 }
10213
10214 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10215 continue;
10216
10217 /* Allocate memory for the section contents. We use bfd_zalloc
10218 here in case unused entries are not reclaimed before the
10219 section's contents are written out. This should not happen,
10220 but this way if it does we get a R_PPC64_NONE reloc in .rela
10221 sections instead of garbage.
10222 We also rely on the section contents being zero when writing
10223 the GOT. */
10224 s->contents = bfd_zalloc (dynobj, s->size);
10225 if (s->contents == NULL)
10226 return FALSE;
10227 }
10228
10229 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10230 {
10231 if (!is_ppc64_elf (ibfd))
10232 continue;
10233
10234 s = ppc64_elf_tdata (ibfd)->got;
10235 if (s != NULL && s != htab->elf.sgot)
10236 {
10237 if (s->size == 0)
10238 s->flags |= SEC_EXCLUDE;
10239 else
10240 {
10241 s->contents = bfd_zalloc (ibfd, s->size);
10242 if (s->contents == NULL)
10243 return FALSE;
10244 }
10245 }
10246 s = ppc64_elf_tdata (ibfd)->relgot;
10247 if (s != NULL)
10248 {
10249 if (s->size == 0)
10250 s->flags |= SEC_EXCLUDE;
10251 else
10252 {
10253 s->contents = bfd_zalloc (ibfd, s->size);
10254 if (s->contents == NULL)
10255 return FALSE;
10256 relocs = TRUE;
10257 s->reloc_count = 0;
10258 }
10259 }
10260 }
10261
10262 if (htab->elf.dynamic_sections_created)
10263 {
10264 bfd_boolean tls_opt;
10265
10266 /* Add some entries to the .dynamic section. We fill in the
10267 values later, in ppc64_elf_finish_dynamic_sections, but we
10268 must add the entries now so that we get the correct size for
10269 the .dynamic section. The DT_DEBUG entry is filled in by the
10270 dynamic linker and used by the debugger. */
10271#define add_dynamic_entry(TAG, VAL) \
10272 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10273
10274 if (bfd_link_executable (info))
10275 {
10276 if (!add_dynamic_entry (DT_DEBUG, 0))
10277 return FALSE;
10278 }
10279
10280 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10281 {
10282 if (!add_dynamic_entry (DT_PLTGOT, 0)
10283 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10284 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10285 || !add_dynamic_entry (DT_JMPREL, 0)
10286 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10287 return FALSE;
10288 }
10289
10290 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10291 {
10292 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10293 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10294 return FALSE;
10295 }
10296
10297 tls_opt = (htab->params->tls_get_addr_opt
10298 && htab->tls_get_addr_fd != NULL
10299 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10300 if (tls_opt || !htab->opd_abi)
10301 {
10302 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10303 return FALSE;
10304 }
10305
10306 if (relocs)
10307 {
10308 if (!add_dynamic_entry (DT_RELA, 0)
10309 || !add_dynamic_entry (DT_RELASZ, 0)
10310 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10311 return FALSE;
10312
10313 /* If any dynamic relocs apply to a read-only section,
10314 then we need a DT_TEXTREL entry. */
10315 if ((info->flags & DF_TEXTREL) == 0)
10316 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10317
10318 if ((info->flags & DF_TEXTREL) != 0)
10319 {
10320 if (!add_dynamic_entry (DT_TEXTREL, 0))
10321 return FALSE;
10322 }
10323 }
10324 }
10325#undef add_dynamic_entry
10326
10327 return TRUE;
10328}
10329
10330/* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10331
10332static bfd_boolean
10333ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10334{
10335 if (h->plt.plist != NULL
10336 && !h->def_regular
10337 && !h->pointer_equality_needed)
10338 return FALSE;
10339
10340 return _bfd_elf_hash_symbol (h);
10341}
10342
10343/* Determine the type of stub needed, if any, for a call. */
10344
10345static inline enum ppc_stub_type
10346ppc_type_of_stub (asection *input_sec,
10347 const Elf_Internal_Rela *rel,
10348 struct ppc_link_hash_entry **hash,
10349 struct plt_entry **plt_ent,
10350 bfd_vma destination,
10351 unsigned long local_off)
10352{
10353 struct ppc_link_hash_entry *h = *hash;
10354 bfd_vma location;
10355 bfd_vma branch_offset;
10356 bfd_vma max_branch_offset;
10357 enum elf_ppc64_reloc_type r_type;
10358
10359 if (h != NULL)
10360 {
10361 struct plt_entry *ent;
10362 struct ppc_link_hash_entry *fdh = h;
10363 if (h->oh != NULL
10364 && h->oh->is_func_descriptor)
10365 {
10366 fdh = ppc_follow_link (h->oh);
10367 *hash = fdh;
10368 }
10369
10370 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10371 if (ent->addend == rel->r_addend
10372 && ent->plt.offset != (bfd_vma) -1)
10373 {
10374 *plt_ent = ent;
10375 return ppc_stub_plt_call;
10376 }
10377
10378 /* Here, we know we don't have a plt entry. If we don't have a
10379 either a defined function descriptor or a defined entry symbol
10380 in a regular object file, then it is pointless trying to make
10381 any other type of stub. */
10382 if (!is_static_defined (&fdh->elf)
10383 && !is_static_defined (&h->elf))
10384 return ppc_stub_none;
10385 }
10386 else if (elf_local_got_ents (input_sec->owner) != NULL)
10387 {
10388 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10389 struct plt_entry **local_plt = (struct plt_entry **)
10390 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10391 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10392
10393 if (local_plt[r_symndx] != NULL)
10394 {
10395 struct plt_entry *ent;
10396
10397 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10398 if (ent->addend == rel->r_addend
10399 && ent->plt.offset != (bfd_vma) -1)
10400 {
10401 *plt_ent = ent;
10402 return ppc_stub_plt_call;
10403 }
10404 }
10405 }
10406
10407 /* Determine where the call point is. */
10408 location = (input_sec->output_offset
10409 + input_sec->output_section->vma
10410 + rel->r_offset);
10411
10412 branch_offset = destination - location;
10413 r_type = ELF64_R_TYPE (rel->r_info);
10414
10415 /* Determine if a long branch stub is needed. */
10416 max_branch_offset = 1 << 25;
10417 if (r_type != R_PPC64_REL24)
10418 max_branch_offset = 1 << 15;
10419
10420 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10421 /* We need a stub. Figure out whether a long_branch or plt_branch
10422 is needed later. */
10423 return ppc_stub_long_branch;
10424
10425 return ppc_stub_none;
10426}
10427
10428/* With power7 weakly ordered memory model, it is possible for ld.so
10429 to update a plt entry in one thread and have another thread see a
10430 stale zero toc entry. To avoid this we need some sort of acquire
10431 barrier in the call stub. One solution is to make the load of the
10432 toc word seem to appear to depend on the load of the function entry
10433 word. Another solution is to test for r2 being zero, and branch to
10434 the appropriate glink entry if so.
10435
10436 . fake dep barrier compare
10437 . ld 12,xxx(2) ld 12,xxx(2)
10438 . mtctr 12 mtctr 12
10439 . xor 11,12,12 ld 2,xxx+8(2)
10440 . add 2,2,11 cmpldi 2,0
10441 . ld 2,xxx+8(2) bnectr+
10442 . bctr b <glink_entry>
10443
10444 The solution involving the compare turns out to be faster, so
10445 that's what we use unless the branch won't reach. */
10446
10447#define ALWAYS_USE_FAKE_DEP 0
10448#define ALWAYS_EMIT_R2SAVE 0
10449
10450#define PPC_LO(v) ((v) & 0xffff)
10451#define PPC_HI(v) (((v) >> 16) & 0xffff)
10452#define PPC_HA(v) PPC_HI ((v) + 0x8000)
10453
10454static inline unsigned int
10455plt_stub_size (struct ppc_link_hash_table *htab,
10456 struct ppc_stub_hash_entry *stub_entry,
10457 bfd_vma off)
10458{
10459 unsigned size = 12;
10460
10461 if (ALWAYS_EMIT_R2SAVE
10462 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10463 size += 4;
10464 if (PPC_HA (off) != 0)
10465 size += 4;
10466 if (htab->opd_abi)
10467 {
10468 size += 4;
10469 if (htab->params->plt_static_chain)
10470 size += 4;
10471 if (htab->params->plt_thread_safe
10472 && htab->elf.dynamic_sections_created
10473 && stub_entry->h != NULL
10474 && stub_entry->h->elf.dynindx != -1)
10475 size += 8;
10476 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10477 size += 4;
10478 }
10479 if (stub_entry->h != NULL
10480 && (stub_entry->h == htab->tls_get_addr_fd
10481 || stub_entry->h == htab->tls_get_addr)
10482 && htab->params->tls_get_addr_opt)
10483 size += 13 * 4;
10484 return size;
10485}
10486
10487/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10488 then return the padding needed to do so. */
10489static inline unsigned int
10490plt_stub_pad (struct ppc_link_hash_table *htab,
10491 struct ppc_stub_hash_entry *stub_entry,
10492 bfd_vma plt_off)
10493{
10494 int stub_align = 1 << htab->params->plt_stub_align;
10495 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10496 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10497
10498 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10499 > ((stub_size - 1) & -stub_align))
10500 return stub_align - (stub_off & (stub_align - 1));
10501 return 0;
10502}
10503
10504/* Build a .plt call stub. */
10505
10506static inline bfd_byte *
10507build_plt_stub (struct ppc_link_hash_table *htab,
10508 struct ppc_stub_hash_entry *stub_entry,
10509 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10510{
10511 bfd *obfd = htab->params->stub_bfd;
10512 bfd_boolean plt_load_toc = htab->opd_abi;
10513 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10514 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10515 && htab->elf.dynamic_sections_created
10516 && stub_entry->h != NULL
10517 && stub_entry->h->elf.dynindx != -1);
10518 bfd_boolean use_fake_dep = plt_thread_safe;
10519 bfd_vma cmp_branch_off = 0;
10520
10521 if (!ALWAYS_USE_FAKE_DEP
10522 && plt_load_toc
10523 && plt_thread_safe
10524 && !((stub_entry->h == htab->tls_get_addr_fd
10525 || stub_entry->h == htab->tls_get_addr)
10526 && htab->params->tls_get_addr_opt))
10527 {
10528 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10529 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10530 / PLT_ENTRY_SIZE (htab));
10531 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10532 bfd_vma to, from;
10533
10534 if (pltindex > 32768)
10535 glinkoff += (pltindex - 32768) * 4;
10536 to = (glinkoff
10537 + htab->glink->output_offset
10538 + htab->glink->output_section->vma);
10539 from = (p - stub_entry->group->stub_sec->contents
10540 + 4 * (ALWAYS_EMIT_R2SAVE
10541 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10542 + 4 * (PPC_HA (offset) != 0)
10543 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10544 != PPC_HA (offset))
10545 + 4 * (plt_static_chain != 0)
10546 + 20
10547 + stub_entry->group->stub_sec->output_offset
10548 + stub_entry->group->stub_sec->output_section->vma);
10549 cmp_branch_off = to - from;
10550 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10551 }
10552
10553 if (PPC_HA (offset) != 0)
10554 {
10555 if (r != NULL)
10556 {
10557 if (ALWAYS_EMIT_R2SAVE
10558 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10559 r[0].r_offset += 4;
10560 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10561 r[1].r_offset = r[0].r_offset + 4;
10562 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10563 r[1].r_addend = r[0].r_addend;
10564 if (plt_load_toc)
10565 {
10566 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10567 {
10568 r[2].r_offset = r[1].r_offset + 4;
10569 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10570 r[2].r_addend = r[0].r_addend;
10571 }
10572 else
10573 {
10574 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10575 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10576 r[2].r_addend = r[0].r_addend + 8;
10577 if (plt_static_chain)
10578 {
10579 r[3].r_offset = r[2].r_offset + 4;
10580 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10581 r[3].r_addend = r[0].r_addend + 16;
10582 }
10583 }
10584 }
10585 }
10586 if (ALWAYS_EMIT_R2SAVE
10587 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10588 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10589 if (plt_load_toc)
10590 {
10591 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10592 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10593 }
10594 else
10595 {
10596 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10597 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10598 }
10599 if (plt_load_toc
10600 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10601 {
10602 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10603 offset = 0;
10604 }
10605 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10606 if (plt_load_toc)
10607 {
10608 if (use_fake_dep)
10609 {
10610 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10611 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10612 }
10613 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10614 if (plt_static_chain)
10615 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10616 }
10617 }
10618 else
10619 {
10620 if (r != NULL)
10621 {
10622 if (ALWAYS_EMIT_R2SAVE
10623 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10624 r[0].r_offset += 4;
10625 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10626 if (plt_load_toc)
10627 {
10628 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10629 {
10630 r[1].r_offset = r[0].r_offset + 4;
10631 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10632 r[1].r_addend = r[0].r_addend;
10633 }
10634 else
10635 {
10636 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10637 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10638 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10639 if (plt_static_chain)
10640 {
10641 r[2].r_offset = r[1].r_offset + 4;
10642 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10643 r[2].r_addend = r[0].r_addend + 8;
10644 }
10645 }
10646 }
10647 }
10648 if (ALWAYS_EMIT_R2SAVE
10649 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10650 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10651 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10652 if (plt_load_toc
10653 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10654 {
10655 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10656 offset = 0;
10657 }
10658 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10659 if (plt_load_toc)
10660 {
10661 if (use_fake_dep)
10662 {
10663 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10664 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10665 }
10666 if (plt_static_chain)
10667 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10668 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10669 }
10670 }
10671 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10672 {
10673 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10674 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10675 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10676 }
10677 else
10678 bfd_put_32 (obfd, BCTR, p), p += 4;
10679 return p;
10680}
10681
10682/* Build a special .plt call stub for __tls_get_addr. */
10683
10684#define LD_R11_0R3 0xe9630000
10685#define LD_R12_0R3 0xe9830000
10686#define MR_R0_R3 0x7c601b78
10687#define CMPDI_R11_0 0x2c2b0000
10688#define ADD_R3_R12_R13 0x7c6c6a14
10689#define BEQLR 0x4d820020
10690#define MR_R3_R0 0x7c030378
10691#define STD_R11_0R1 0xf9610000
10692#define BCTRL 0x4e800421
10693#define LD_R11_0R1 0xe9610000
10694#define MTLR_R11 0x7d6803a6
10695
10696static inline bfd_byte *
10697build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10698 struct ppc_stub_hash_entry *stub_entry,
10699 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10700{
10701 bfd *obfd = htab->params->stub_bfd;
10702
10703 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10704 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10705 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10706 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10707 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10708 bfd_put_32 (obfd, BEQLR, p), p += 4;
10709 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10710 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10711 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10712
10713 if (r != NULL)
10714 r[0].r_offset += 9 * 4;
10715 p = build_plt_stub (htab, stub_entry, p, offset, r);
10716 bfd_put_32 (obfd, BCTRL, p - 4);
10717
10718 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10719 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10720 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10721 bfd_put_32 (obfd, BLR, p), p += 4;
10722
10723 return p;
10724}
10725
10726static Elf_Internal_Rela *
10727get_relocs (asection *sec, int count)
10728{
10729 Elf_Internal_Rela *relocs;
10730 struct bfd_elf_section_data *elfsec_data;
10731
10732 elfsec_data = elf_section_data (sec);
10733 relocs = elfsec_data->relocs;
10734 if (relocs == NULL)
10735 {
10736 bfd_size_type relsize;
10737 relsize = sec->reloc_count * sizeof (*relocs);
10738 relocs = bfd_alloc (sec->owner, relsize);
10739 if (relocs == NULL)
10740 return NULL;
10741 elfsec_data->relocs = relocs;
10742 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10743 sizeof (Elf_Internal_Shdr));
10744 if (elfsec_data->rela.hdr == NULL)
10745 return NULL;
10746 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10747 * sizeof (Elf64_External_Rela));
10748 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10749 sec->reloc_count = 0;
10750 }
10751 relocs += sec->reloc_count;
10752 sec->reloc_count += count;
10753 return relocs;
10754}
10755
10756static bfd_vma
10757get_r2off (struct bfd_link_info *info,
10758 struct ppc_stub_hash_entry *stub_entry)
10759{
10760 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10761 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10762
10763 if (r2off == 0)
10764 {
10765 /* Support linking -R objects. Get the toc pointer from the
10766 opd entry. */
10767 char buf[8];
10768 if (!htab->opd_abi)
10769 return r2off;
10770 asection *opd = stub_entry->h->elf.root.u.def.section;
10771 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10772
10773 if (strcmp (opd->name, ".opd") != 0
10774 || opd->reloc_count != 0)
10775 {
10776 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10777 stub_entry->h->elf.root.root.string);
10778 bfd_set_error (bfd_error_bad_value);
10779 return (bfd_vma) -1;
10780 }
10781 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10782 return (bfd_vma) -1;
10783 r2off = bfd_get_64 (opd->owner, buf);
10784 r2off -= elf_gp (info->output_bfd);
10785 }
10786 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10787 return r2off;
10788}
10789
10790static bfd_boolean
10791ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10792{
10793 struct ppc_stub_hash_entry *stub_entry;
10794 struct ppc_branch_hash_entry *br_entry;
10795 struct bfd_link_info *info;
10796 struct ppc_link_hash_table *htab;
10797 bfd_byte *loc;
10798 bfd_byte *p;
10799 bfd_vma dest, off;
10800 int size;
10801 Elf_Internal_Rela *r;
10802 asection *plt;
10803
10804 /* Massage our args to the form they really have. */
10805 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10806 info = in_arg;
10807
10808 htab = ppc_hash_table (info);
10809 if (htab == NULL)
10810 return FALSE;
10811
10812 /* Make a note of the offset within the stubs for this entry. */
10813 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10814 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10815
10816 htab->stub_count[stub_entry->stub_type - 1] += 1;
10817 switch (stub_entry->stub_type)
10818 {
10819 case ppc_stub_long_branch:
10820 case ppc_stub_long_branch_r2off:
10821 /* Branches are relative. This is where we are going to. */
10822 dest = (stub_entry->target_value
10823 + stub_entry->target_section->output_offset
10824 + stub_entry->target_section->output_section->vma);
10825 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10826 off = dest;
10827
10828 /* And this is where we are coming from. */
10829 off -= (stub_entry->stub_offset
10830 + stub_entry->group->stub_sec->output_offset
10831 + stub_entry->group->stub_sec->output_section->vma);
10832
10833 size = 4;
10834 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10835 {
10836 bfd_vma r2off = get_r2off (info, stub_entry);
10837
10838 if (r2off == (bfd_vma) -1)
10839 {
10840 htab->stub_error = TRUE;
10841 return FALSE;
10842 }
10843 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10844 loc += 4;
10845 size = 8;
10846 if (PPC_HA (r2off) != 0)
10847 {
10848 bfd_put_32 (htab->params->stub_bfd,
10849 ADDIS_R2_R2 | PPC_HA (r2off), loc);
10850 loc += 4;
10851 size += 4;
10852 }
10853 if (PPC_LO (r2off) != 0)
10854 {
10855 bfd_put_32 (htab->params->stub_bfd,
10856 ADDI_R2_R2 | PPC_LO (r2off), loc);
10857 loc += 4;
10858 size += 4;
10859 }
10860 off -= size - 4;
10861 }
10862 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10863
10864 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10865 {
10866 info->callbacks->einfo
10867 (_("%P: long branch stub `%s' offset overflow\n"),
10868 stub_entry->root.string);
10869 htab->stub_error = TRUE;
10870 return FALSE;
10871 }
10872
10873 if (info->emitrelocations)
10874 {
10875 r = get_relocs (stub_entry->group->stub_sec, 1);
10876 if (r == NULL)
10877 return FALSE;
10878 r->r_offset = loc - stub_entry->group->stub_sec->contents;
10879 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10880 r->r_addend = dest;
10881 if (stub_entry->h != NULL)
10882 {
10883 struct elf_link_hash_entry **hashes;
10884 unsigned long symndx;
10885 struct ppc_link_hash_entry *h;
10886
10887 hashes = elf_sym_hashes (htab->params->stub_bfd);
10888 if (hashes == NULL)
10889 {
10890 bfd_size_type hsize;
10891
10892 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10893 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10894 if (hashes == NULL)
10895 return FALSE;
10896 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10897 htab->stub_globals = 1;
10898 }
10899 symndx = htab->stub_globals++;
10900 h = stub_entry->h;
10901 hashes[symndx] = &h->elf;
10902 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10903 if (h->oh != NULL && h->oh->is_func)
10904 h = ppc_follow_link (h->oh);
10905 if (h->elf.root.u.def.section != stub_entry->target_section)
10906 /* H is an opd symbol. The addend must be zero. */
10907 r->r_addend = 0;
10908 else
10909 {
10910 off = (h->elf.root.u.def.value
10911 + h->elf.root.u.def.section->output_offset
10912 + h->elf.root.u.def.section->output_section->vma);
10913 r->r_addend -= off;
10914 }
10915 }
10916 }
10917 break;
10918
10919 case ppc_stub_plt_branch:
10920 case ppc_stub_plt_branch_r2off:
10921 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10922 stub_entry->root.string + 9,
10923 FALSE, FALSE);
10924 if (br_entry == NULL)
10925 {
10926 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10927 stub_entry->root.string);
10928 htab->stub_error = TRUE;
10929 return FALSE;
10930 }
10931
10932 dest = (stub_entry->target_value
10933 + stub_entry->target_section->output_offset
10934 + stub_entry->target_section->output_section->vma);
10935 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10936 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10937
10938 bfd_put_64 (htab->brlt->owner, dest,
10939 htab->brlt->contents + br_entry->offset);
10940
10941 if (br_entry->iter == htab->stub_iteration)
10942 {
10943 br_entry->iter = 0;
10944
10945 if (htab->relbrlt != NULL)
10946 {
10947 /* Create a reloc for the branch lookup table entry. */
10948 Elf_Internal_Rela rela;
10949 bfd_byte *rl;
10950
10951 rela.r_offset = (br_entry->offset
10952 + htab->brlt->output_offset
10953 + htab->brlt->output_section->vma);
10954 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10955 rela.r_addend = dest;
10956
10957 rl = htab->relbrlt->contents;
10958 rl += (htab->relbrlt->reloc_count++
10959 * sizeof (Elf64_External_Rela));
10960 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10961 }
10962 else if (info->emitrelocations)
10963 {
10964 r = get_relocs (htab->brlt, 1);
10965 if (r == NULL)
10966 return FALSE;
10967 /* brlt, being SEC_LINKER_CREATED does not go through the
10968 normal reloc processing. Symbols and offsets are not
10969 translated from input file to output file form, so
10970 set up the offset per the output file. */
10971 r->r_offset = (br_entry->offset
10972 + htab->brlt->output_offset
10973 + htab->brlt->output_section->vma);
10974 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10975 r->r_addend = dest;
10976 }
10977 }
10978
10979 dest = (br_entry->offset
10980 + htab->brlt->output_offset
10981 + htab->brlt->output_section->vma);
10982
10983 off = (dest
10984 - elf_gp (htab->brlt->output_section->owner)
10985 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10986
10987 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10988 {
10989 info->callbacks->einfo
10990 (_("%P: linkage table error against `%T'\n"),
10991 stub_entry->root.string);
10992 bfd_set_error (bfd_error_bad_value);
10993 htab->stub_error = TRUE;
10994 return FALSE;
10995 }
10996
10997 if (info->emitrelocations)
10998 {
10999 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11000 if (r == NULL)
11001 return FALSE;
11002 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11003 if (bfd_big_endian (info->output_bfd))
11004 r[0].r_offset += 2;
11005 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11006 r[0].r_offset += 4;
11007 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11008 r[0].r_addend = dest;
11009 if (PPC_HA (off) != 0)
11010 {
11011 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11012 r[1].r_offset = r[0].r_offset + 4;
11013 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11014 r[1].r_addend = r[0].r_addend;
11015 }
11016 }
11017
11018 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11019 {
11020 if (PPC_HA (off) != 0)
11021 {
11022 size = 16;
11023 bfd_put_32 (htab->params->stub_bfd,
11024 ADDIS_R12_R2 | PPC_HA (off), loc);
11025 loc += 4;
11026 bfd_put_32 (htab->params->stub_bfd,
11027 LD_R12_0R12 | PPC_LO (off), loc);
11028 }
11029 else
11030 {
11031 size = 12;
11032 bfd_put_32 (htab->params->stub_bfd,
11033 LD_R12_0R2 | PPC_LO (off), loc);
11034 }
11035 }
11036 else
11037 {
11038 bfd_vma r2off = get_r2off (info, stub_entry);
11039
11040 if (r2off == (bfd_vma) -1)
11041 {
11042 htab->stub_error = TRUE;
11043 return FALSE;
11044 }
11045
11046 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11047 loc += 4;
11048 size = 16;
11049 if (PPC_HA (off) != 0)
11050 {
11051 size += 4;
11052 bfd_put_32 (htab->params->stub_bfd,
11053 ADDIS_R12_R2 | PPC_HA (off), loc);
11054 loc += 4;
11055 bfd_put_32 (htab->params->stub_bfd,
11056 LD_R12_0R12 | PPC_LO (off), loc);
11057 }
11058 else
11059 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11060
11061 if (PPC_HA (r2off) != 0)
11062 {
11063 size += 4;
11064 loc += 4;
11065 bfd_put_32 (htab->params->stub_bfd,
11066 ADDIS_R2_R2 | PPC_HA (r2off), loc);
11067 }
11068 if (PPC_LO (r2off) != 0)
11069 {
11070 size += 4;
11071 loc += 4;
11072 bfd_put_32 (htab->params->stub_bfd,
11073 ADDI_R2_R2 | PPC_LO (r2off), loc);
11074 }
11075 }
11076 loc += 4;
11077 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11078 loc += 4;
11079 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11080 break;
11081
11082 case ppc_stub_plt_call:
11083 case ppc_stub_plt_call_r2save:
11084 if (stub_entry->h != NULL
11085 && stub_entry->h->is_func_descriptor
11086 && stub_entry->h->oh != NULL)
11087 {
11088 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11089
11090 /* If the old-ABI "dot-symbol" is undefined make it weak so
11091 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11092 if (fh->elf.root.type == bfd_link_hash_undefined)
11093 fh->elf.root.type = bfd_link_hash_undefweak;
11094 /* Stop undo_symbol_twiddle changing it back to undefined. */
11095 fh->was_undefined = 0;
11096 }
11097
11098 /* Now build the stub. */
11099 dest = stub_entry->plt_ent->plt.offset & ~1;
11100 if (dest >= (bfd_vma) -2)
11101 abort ();
11102
11103 plt = htab->elf.splt;
11104 if (!htab->elf.dynamic_sections_created
11105 || stub_entry->h == NULL
11106 || stub_entry->h->elf.dynindx == -1)
11107 plt = htab->elf.iplt;
11108
11109 dest += plt->output_offset + plt->output_section->vma;
11110
11111 if (stub_entry->h == NULL
11112 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11113 {
11114 Elf_Internal_Rela rela;
11115 bfd_byte *rl;
11116
11117 rela.r_offset = dest;
11118 if (htab->opd_abi)
11119 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11120 else
11121 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11122 rela.r_addend = (stub_entry->target_value
11123 + stub_entry->target_section->output_offset
11124 + stub_entry->target_section->output_section->vma);
11125
11126 rl = (htab->elf.irelplt->contents
11127 + (htab->elf.irelplt->reloc_count++
11128 * sizeof (Elf64_External_Rela)));
11129 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11130 stub_entry->plt_ent->plt.offset |= 1;
11131 }
11132
11133 off = (dest
11134 - elf_gp (plt->output_section->owner)
11135 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11136
11137 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11138 {
11139 info->callbacks->einfo
11140 /* xgettext:c-format */
11141 (_("%P: linkage table error against `%T'\n"),
11142 stub_entry->h != NULL
11143 ? stub_entry->h->elf.root.root.string
11144 : "<local sym>");
11145 bfd_set_error (bfd_error_bad_value);
11146 htab->stub_error = TRUE;
11147 return FALSE;
11148 }
11149
11150 if (htab->params->plt_stub_align != 0)
11151 {
11152 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11153
11154 stub_entry->group->stub_sec->size += pad;
11155 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11156 loc += pad;
11157 }
11158
11159 r = NULL;
11160 if (info->emitrelocations)
11161 {
11162 r = get_relocs (stub_entry->group->stub_sec,
11163 ((PPC_HA (off) != 0)
11164 + (htab->opd_abi
11165 ? 2 + (htab->params->plt_static_chain
11166 && PPC_HA (off + 16) == PPC_HA (off))
11167 : 1)));
11168 if (r == NULL)
11169 return FALSE;
11170 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11171 if (bfd_big_endian (info->output_bfd))
11172 r[0].r_offset += 2;
11173 r[0].r_addend = dest;
11174 }
11175 if (stub_entry->h != NULL
11176 && (stub_entry->h == htab->tls_get_addr_fd
11177 || stub_entry->h == htab->tls_get_addr)
11178 && htab->params->tls_get_addr_opt)
11179 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11180 else
11181 p = build_plt_stub (htab, stub_entry, loc, off, r);
11182 size = p - loc;
11183 break;
11184
11185 case ppc_stub_save_res:
11186 return TRUE;
11187
11188 default:
11189 BFD_FAIL ();
11190 return FALSE;
11191 }
11192
11193 stub_entry->group->stub_sec->size += size;
11194
11195 if (htab->params->emit_stub_syms)
11196 {
11197 struct elf_link_hash_entry *h;
11198 size_t len1, len2;
11199 char *name;
11200 const char *const stub_str[] = { "long_branch",
11201 "long_branch_r2off",
11202 "plt_branch",
11203 "plt_branch_r2off",
11204 "plt_call",
11205 "plt_call" };
11206
11207 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11208 len2 = strlen (stub_entry->root.string);
11209 name = bfd_malloc (len1 + len2 + 2);
11210 if (name == NULL)
11211 return FALSE;
11212 memcpy (name, stub_entry->root.string, 9);
11213 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11214 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11215 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11216 if (h == NULL)
11217 return FALSE;
11218 if (h->root.type == bfd_link_hash_new)
11219 {
11220 h->root.type = bfd_link_hash_defined;
11221 h->root.u.def.section = stub_entry->group->stub_sec;
11222 h->root.u.def.value = stub_entry->stub_offset;
11223 h->ref_regular = 1;
11224 h->def_regular = 1;
11225 h->ref_regular_nonweak = 1;
11226 h->forced_local = 1;
11227 h->non_elf = 0;
11228 h->root.linker_def = 1;
11229 }
11230 }
11231
11232 return TRUE;
11233}
11234
11235/* As above, but don't actually build the stub. Just bump offset so
11236 we know stub section sizes, and select plt_branch stubs where
11237 long_branch stubs won't do. */
11238
11239static bfd_boolean
11240ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11241{
11242 struct ppc_stub_hash_entry *stub_entry;
11243 struct bfd_link_info *info;
11244 struct ppc_link_hash_table *htab;
11245 bfd_vma off;
11246 int size;
11247
11248 /* Massage our args to the form they really have. */
11249 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11250 info = in_arg;
11251
11252 htab = ppc_hash_table (info);
11253 if (htab == NULL)
11254 return FALSE;
11255
11256 if (stub_entry->h != NULL
11257 && stub_entry->h->save_res
11258 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11259 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11260 {
11261 /* Don't make stubs to out-of-line register save/restore
11262 functions. Instead, emit copies of the functions. */
11263 stub_entry->group->needs_save_res = 1;
11264 stub_entry->stub_type = ppc_stub_save_res;
11265 return TRUE;
11266 }
11267
11268 if (stub_entry->stub_type == ppc_stub_plt_call
11269 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11270 {
11271 asection *plt;
11272 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11273 if (off >= (bfd_vma) -2)
11274 abort ();
11275 plt = htab->elf.splt;
11276 if (!htab->elf.dynamic_sections_created
11277 || stub_entry->h == NULL
11278 || stub_entry->h->elf.dynindx == -1)
11279 plt = htab->elf.iplt;
11280 off += (plt->output_offset
11281 + plt->output_section->vma
11282 - elf_gp (plt->output_section->owner)
11283 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11284
11285 size = plt_stub_size (htab, stub_entry, off);
11286 if (htab->params->plt_stub_align)
11287 size += plt_stub_pad (htab, stub_entry, off);
11288 if (info->emitrelocations)
11289 {
11290 stub_entry->group->stub_sec->reloc_count
11291 += ((PPC_HA (off) != 0)
11292 + (htab->opd_abi
11293 ? 2 + (htab->params->plt_static_chain
11294 && PPC_HA (off + 16) == PPC_HA (off))
11295 : 1));
11296 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11297 }
11298 }
11299 else
11300 {
11301 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11302 variants. */
11303 bfd_vma r2off = 0;
11304 bfd_vma local_off = 0;
11305
11306 off = (stub_entry->target_value
11307 + stub_entry->target_section->output_offset
11308 + stub_entry->target_section->output_section->vma);
11309 off -= (stub_entry->group->stub_sec->size
11310 + stub_entry->group->stub_sec->output_offset
11311 + stub_entry->group->stub_sec->output_section->vma);
11312
11313 /* Reset the stub type from the plt variant in case we now
11314 can reach with a shorter stub. */
11315 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11316 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11317
11318 size = 4;
11319 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11320 {
11321 r2off = get_r2off (info, stub_entry);
11322 if (r2off == (bfd_vma) -1)
11323 {
11324 htab->stub_error = TRUE;
11325 return FALSE;
11326 }
11327 size = 8;
11328 if (PPC_HA (r2off) != 0)
11329 size += 4;
11330 if (PPC_LO (r2off) != 0)
11331 size += 4;
11332 off -= size - 4;
11333 }
11334
11335 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11336
11337 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11338 Do the same for -R objects without function descriptors. */
11339 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11340 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11341 && r2off == 0
11342 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11343 {
11344 struct ppc_branch_hash_entry *br_entry;
11345
11346 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11347 stub_entry->root.string + 9,
11348 TRUE, FALSE);
11349 if (br_entry == NULL)
11350 {
11351 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11352 stub_entry->root.string);
11353 htab->stub_error = TRUE;
11354 return FALSE;
11355 }
11356
11357 if (br_entry->iter != htab->stub_iteration)
11358 {
11359 br_entry->iter = htab->stub_iteration;
11360 br_entry->offset = htab->brlt->size;
11361 htab->brlt->size += 8;
11362
11363 if (htab->relbrlt != NULL)
11364 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11365 else if (info->emitrelocations)
11366 {
11367 htab->brlt->reloc_count += 1;
11368 htab->brlt->flags |= SEC_RELOC;
11369 }
11370 }
11371
11372 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11373 off = (br_entry->offset
11374 + htab->brlt->output_offset
11375 + htab->brlt->output_section->vma
11376 - elf_gp (htab->brlt->output_section->owner)
11377 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11378
11379 if (info->emitrelocations)
11380 {
11381 stub_entry->group->stub_sec->reloc_count
11382 += 1 + (PPC_HA (off) != 0);
11383 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11384 }
11385
11386 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11387 {
11388 size = 12;
11389 if (PPC_HA (off) != 0)
11390 size = 16;
11391 }
11392 else
11393 {
11394 size = 16;
11395 if (PPC_HA (off) != 0)
11396 size += 4;
11397
11398 if (PPC_HA (r2off) != 0)
11399 size += 4;
11400 if (PPC_LO (r2off) != 0)
11401 size += 4;
11402 }
11403 }
11404 else if (info->emitrelocations)
11405 {
11406 stub_entry->group->stub_sec->reloc_count += 1;
11407 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11408 }
11409 }
11410
11411 stub_entry->group->stub_sec->size += size;
11412 return TRUE;
11413}
11414
11415/* Set up various things so that we can make a list of input sections
11416 for each output section included in the link. Returns -1 on error,
11417 0 when no stubs will be needed, and 1 on success. */
11418
11419int
11420ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11421{
11422 unsigned int id;
11423 bfd_size_type amt;
11424 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11425
11426 if (htab == NULL)
11427 return -1;
11428
11429 htab->sec_info_arr_size = bfd_get_next_section_id ();
11430 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11431 htab->sec_info = bfd_zmalloc (amt);
11432 if (htab->sec_info == NULL)
11433 return -1;
11434
11435 /* Set toc_off for com, und, abs and ind sections. */
11436 for (id = 0; id < 3; id++)
11437 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11438
11439 return 1;
11440}
11441
11442/* Set up for first pass at multitoc partitioning. */
11443
11444void
11445ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11446{
11447 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11448
11449 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11450 htab->toc_bfd = NULL;
11451 htab->toc_first_sec = NULL;
11452}
11453
11454/* The linker repeatedly calls this function for each TOC input section
11455 and linker generated GOT section. Group input bfds such that the toc
11456 within a group is less than 64k in size. */
11457
11458bfd_boolean
11459ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11460{
11461 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11462 bfd_vma addr, off, limit;
11463
11464 if (htab == NULL)
11465 return FALSE;
11466
11467 if (!htab->second_toc_pass)
11468 {
11469 /* Keep track of the first .toc or .got section for this input bfd. */
11470 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11471
11472 if (new_bfd)
11473 {
11474 htab->toc_bfd = isec->owner;
11475 htab->toc_first_sec = isec;
11476 }
11477
11478 addr = isec->output_offset + isec->output_section->vma;
11479 off = addr - htab->toc_curr;
11480 limit = 0x80008000;
11481 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11482 limit = 0x10000;
11483 if (off + isec->size > limit)
11484 {
11485 addr = (htab->toc_first_sec->output_offset
11486 + htab->toc_first_sec->output_section->vma);
11487 htab->toc_curr = addr;
11488 htab->toc_curr &= -TOC_BASE_ALIGN;
11489 }
11490
11491 /* toc_curr is the base address of this toc group. Set elf_gp
11492 for the input section to be the offset relative to the
11493 output toc base plus 0x8000. Making the input elf_gp an
11494 offset allows us to move the toc as a whole without
11495 recalculating input elf_gp. */
11496 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11497 off += TOC_BASE_OFF;
11498
11499 /* Die if someone uses a linker script that doesn't keep input
11500 file .toc and .got together. */
11501 if (new_bfd
11502 && elf_gp (isec->owner) != 0
11503 && elf_gp (isec->owner) != off)
11504 return FALSE;
11505
11506 elf_gp (isec->owner) = off;
11507 return TRUE;
11508 }
11509
11510 /* During the second pass toc_first_sec points to the start of
11511 a toc group, and toc_curr is used to track the old elf_gp.
11512 We use toc_bfd to ensure we only look at each bfd once. */
11513 if (htab->toc_bfd == isec->owner)
11514 return TRUE;
11515 htab->toc_bfd = isec->owner;
11516
11517 if (htab->toc_first_sec == NULL
11518 || htab->toc_curr != elf_gp (isec->owner))
11519 {
11520 htab->toc_curr = elf_gp (isec->owner);
11521 htab->toc_first_sec = isec;
11522 }
11523 addr = (htab->toc_first_sec->output_offset
11524 + htab->toc_first_sec->output_section->vma);
11525 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11526 elf_gp (isec->owner) = off;
11527
11528 return TRUE;
11529}
11530
11531/* Called via elf_link_hash_traverse to merge GOT entries for global
11532 symbol H. */
11533
11534static bfd_boolean
11535merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11536{
11537 if (h->root.type == bfd_link_hash_indirect)
11538 return TRUE;
11539
11540 merge_got_entries (&h->got.glist);
11541
11542 return TRUE;
11543}
11544
11545/* Called via elf_link_hash_traverse to allocate GOT entries for global
11546 symbol H. */
11547
11548static bfd_boolean
11549reallocate_got (struct elf_link_hash_entry *h, void *inf)
11550{
11551 struct got_entry *gent;
11552
11553 if (h->root.type == bfd_link_hash_indirect)
11554 return TRUE;
11555
11556 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11557 if (!gent->is_indirect)
11558 allocate_got (h, (struct bfd_link_info *) inf, gent);
11559 return TRUE;
11560}
11561
11562/* Called on the first multitoc pass after the last call to
11563 ppc64_elf_next_toc_section. This function removes duplicate GOT
11564 entries. */
11565
11566bfd_boolean
11567ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11568{
11569 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11570 struct bfd *ibfd, *ibfd2;
11571 bfd_boolean done_something;
11572
11573 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11574
11575 if (!htab->do_multi_toc)
11576 return FALSE;
11577
11578 /* Merge global sym got entries within a toc group. */
11579 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11580
11581 /* And tlsld_got. */
11582 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11583 {
11584 struct got_entry *ent, *ent2;
11585
11586 if (!is_ppc64_elf (ibfd))
11587 continue;
11588
11589 ent = ppc64_tlsld_got (ibfd);
11590 if (!ent->is_indirect
11591 && ent->got.offset != (bfd_vma) -1)
11592 {
11593 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11594 {
11595 if (!is_ppc64_elf (ibfd2))
11596 continue;
11597
11598 ent2 = ppc64_tlsld_got (ibfd2);
11599 if (!ent2->is_indirect
11600 && ent2->got.offset != (bfd_vma) -1
11601 && elf_gp (ibfd2) == elf_gp (ibfd))
11602 {
11603 ent2->is_indirect = TRUE;
11604 ent2->got.ent = ent;
11605 }
11606 }
11607 }
11608 }
11609
11610 /* Zap sizes of got sections. */
11611 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11612 htab->elf.irelplt->size -= htab->got_reli_size;
11613 htab->got_reli_size = 0;
11614
11615 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11616 {
11617 asection *got, *relgot;
11618
11619 if (!is_ppc64_elf (ibfd))
11620 continue;
11621
11622 got = ppc64_elf_tdata (ibfd)->got;
11623 if (got != NULL)
11624 {
11625 got->rawsize = got->size;
11626 got->size = 0;
11627 relgot = ppc64_elf_tdata (ibfd)->relgot;
11628 relgot->rawsize = relgot->size;
11629 relgot->size = 0;
11630 }
11631 }
11632
11633 /* Now reallocate the got, local syms first. We don't need to
11634 allocate section contents again since we never increase size. */
11635 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11636 {
11637 struct got_entry **lgot_ents;
11638 struct got_entry **end_lgot_ents;
11639 struct plt_entry **local_plt;
11640 struct plt_entry **end_local_plt;
11641 unsigned char *lgot_masks;
11642 bfd_size_type locsymcount;
11643 Elf_Internal_Shdr *symtab_hdr;
11644 asection *s;
11645
11646 if (!is_ppc64_elf (ibfd))
11647 continue;
11648
11649 lgot_ents = elf_local_got_ents (ibfd);
11650 if (!lgot_ents)
11651 continue;
11652
11653 symtab_hdr = &elf_symtab_hdr (ibfd);
11654 locsymcount = symtab_hdr->sh_info;
11655 end_lgot_ents = lgot_ents + locsymcount;
11656 local_plt = (struct plt_entry **) end_lgot_ents;
11657 end_local_plt = local_plt + locsymcount;
11658 lgot_masks = (unsigned char *) end_local_plt;
11659 s = ppc64_elf_tdata (ibfd)->got;
11660 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11661 {
11662 struct got_entry *ent;
11663
11664 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11665 {
11666 unsigned int ent_size = 8;
11667 unsigned int rel_size = sizeof (Elf64_External_Rela);
11668
11669 ent->got.offset = s->size;
11670 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11671 {
11672 ent_size *= 2;
11673 rel_size *= 2;
11674 }
11675 s->size += ent_size;
11676 if ((*lgot_masks & PLT_IFUNC) != 0)
11677 {
11678 htab->elf.irelplt->size += rel_size;
11679 htab->got_reli_size += rel_size;
11680 }
11681 else if (bfd_link_pic (info))
11682 {
11683 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11684 srel->size += rel_size;
11685 }
11686 }
11687 }
11688 }
11689
11690 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11691
11692 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11693 {
11694 struct got_entry *ent;
11695
11696 if (!is_ppc64_elf (ibfd))
11697 continue;
11698
11699 ent = ppc64_tlsld_got (ibfd);
11700 if (!ent->is_indirect
11701 && ent->got.offset != (bfd_vma) -1)
11702 {
11703 asection *s = ppc64_elf_tdata (ibfd)->got;
11704 ent->got.offset = s->size;
11705 s->size += 16;
11706 if (bfd_link_pic (info))
11707 {
11708 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11709 srel->size += sizeof (Elf64_External_Rela);
11710 }
11711 }
11712 }
11713
11714 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11715 if (!done_something)
11716 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11717 {
11718 asection *got;
11719
11720 if (!is_ppc64_elf (ibfd))
11721 continue;
11722
11723 got = ppc64_elf_tdata (ibfd)->got;
11724 if (got != NULL)
11725 {
11726 done_something = got->rawsize != got->size;
11727 if (done_something)
11728 break;
11729 }
11730 }
11731
11732 if (done_something)
11733 (*htab->params->layout_sections_again) ();
11734
11735 /* Set up for second pass over toc sections to recalculate elf_gp
11736 on input sections. */
11737 htab->toc_bfd = NULL;
11738 htab->toc_first_sec = NULL;
11739 htab->second_toc_pass = TRUE;
11740 return done_something;
11741}
11742
11743/* Called after second pass of multitoc partitioning. */
11744
11745void
11746ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11747{
11748 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11749
11750 /* After the second pass, toc_curr tracks the TOC offset used
11751 for code sections below in ppc64_elf_next_input_section. */
11752 htab->toc_curr = TOC_BASE_OFF;
11753}
11754
11755/* No toc references were found in ISEC. If the code in ISEC makes no
11756 calls, then there's no need to use toc adjusting stubs when branching
11757 into ISEC. Actually, indirect calls from ISEC are OK as they will
11758 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11759 needed, and 2 if a cyclical call-graph was found but no other reason
11760 for a stub was detected. If called from the top level, a return of
11761 2 means the same as a return of 0. */
11762
11763static int
11764toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11765{
11766 int ret;
11767
11768 /* Mark this section as checked. */
11769 isec->call_check_done = 1;
11770
11771 /* We know none of our code bearing sections will need toc stubs. */
11772 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11773 return 0;
11774
11775 if (isec->size == 0)
11776 return 0;
11777
11778 if (isec->output_section == NULL)
11779 return 0;
11780
11781 ret = 0;
11782 if (isec->reloc_count != 0)
11783 {
11784 Elf_Internal_Rela *relstart, *rel;
11785 Elf_Internal_Sym *local_syms;
11786 struct ppc_link_hash_table *htab;
11787
11788 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11789 info->keep_memory);
11790 if (relstart == NULL)
11791 return -1;
11792
11793 /* Look for branches to outside of this section. */
11794 local_syms = NULL;
11795 htab = ppc_hash_table (info);
11796 if (htab == NULL)
11797 return -1;
11798
11799 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11800 {
11801 enum elf_ppc64_reloc_type r_type;
11802 unsigned long r_symndx;
11803 struct elf_link_hash_entry *h;
11804 struct ppc_link_hash_entry *eh;
11805 Elf_Internal_Sym *sym;
11806 asection *sym_sec;
11807 struct _opd_sec_data *opd;
11808 bfd_vma sym_value;
11809 bfd_vma dest;
11810
11811 r_type = ELF64_R_TYPE (rel->r_info);
11812 if (r_type != R_PPC64_REL24
11813 && r_type != R_PPC64_REL14
11814 && r_type != R_PPC64_REL14_BRTAKEN
11815 && r_type != R_PPC64_REL14_BRNTAKEN)
11816 continue;
11817
11818 r_symndx = ELF64_R_SYM (rel->r_info);
11819 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11820 isec->owner))
11821 {
11822 ret = -1;
11823 break;
11824 }
11825
11826 /* Calls to dynamic lib functions go through a plt call stub
11827 that uses r2. */
11828 eh = (struct ppc_link_hash_entry *) h;
11829 if (eh != NULL
11830 && (eh->elf.plt.plist != NULL
11831 || (eh->oh != NULL
11832 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11833 {
11834 ret = 1;
11835 break;
11836 }
11837
11838 if (sym_sec == NULL)
11839 /* Ignore other undefined symbols. */
11840 continue;
11841
11842 /* Assume branches to other sections not included in the
11843 link need stubs too, to cover -R and absolute syms. */
11844 if (sym_sec->output_section == NULL)
11845 {
11846 ret = 1;
11847 break;
11848 }
11849
11850 if (h == NULL)
11851 sym_value = sym->st_value;
11852 else
11853 {
11854 if (h->root.type != bfd_link_hash_defined
11855 && h->root.type != bfd_link_hash_defweak)
11856 abort ();
11857 sym_value = h->root.u.def.value;
11858 }
11859 sym_value += rel->r_addend;
11860
11861 /* If this branch reloc uses an opd sym, find the code section. */
11862 opd = get_opd_info (sym_sec);
11863 if (opd != NULL)
11864 {
11865 if (h == NULL && opd->adjust != NULL)
11866 {
11867 long adjust;
11868
11869 adjust = opd->adjust[OPD_NDX (sym_value)];
11870 if (adjust == -1)
11871 /* Assume deleted functions won't ever be called. */
11872 continue;
11873 sym_value += adjust;
11874 }
11875
11876 dest = opd_entry_value (sym_sec, sym_value,
11877 &sym_sec, NULL, FALSE);
11878 if (dest == (bfd_vma) -1)
11879 continue;
11880 }
11881 else
11882 dest = (sym_value
11883 + sym_sec->output_offset
11884 + sym_sec->output_section->vma);
11885
11886 /* Ignore branch to self. */
11887 if (sym_sec == isec)
11888 continue;
11889
11890 /* If the called function uses the toc, we need a stub. */
11891 if (sym_sec->has_toc_reloc
11892 || sym_sec->makes_toc_func_call)
11893 {
11894 ret = 1;
11895 break;
11896 }
11897
11898 /* Assume any branch that needs a long branch stub might in fact
11899 need a plt_branch stub. A plt_branch stub uses r2. */
11900 else if (dest - (isec->output_offset
11901 + isec->output_section->vma
11902 + rel->r_offset) + (1 << 25)
11903 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11904 ? h->other
11905 : sym->st_other))
11906 {
11907 ret = 1;
11908 break;
11909 }
11910
11911 /* If calling back to a section in the process of being
11912 tested, we can't say for sure that no toc adjusting stubs
11913 are needed, so don't return zero. */
11914 else if (sym_sec->call_check_in_progress)
11915 ret = 2;
11916
11917 /* Branches to another section that itself doesn't have any TOC
11918 references are OK. Recursively call ourselves to check. */
11919 else if (!sym_sec->call_check_done)
11920 {
11921 int recur;
11922
11923 /* Mark current section as indeterminate, so that other
11924 sections that call back to current won't be marked as
11925 known. */
11926 isec->call_check_in_progress = 1;
11927 recur = toc_adjusting_stub_needed (info, sym_sec);
11928 isec->call_check_in_progress = 0;
11929
11930 if (recur != 0)
11931 {
11932 ret = recur;
11933 if (recur != 2)
11934 break;
11935 }
11936 }
11937 }
11938
11939 if (local_syms != NULL
11940 && (elf_symtab_hdr (isec->owner).contents
11941 != (unsigned char *) local_syms))
11942 free (local_syms);
11943 if (elf_section_data (isec)->relocs != relstart)
11944 free (relstart);
11945 }
11946
11947 if ((ret & 1) == 0
11948 && isec->map_head.s != NULL
11949 && (strcmp (isec->output_section->name, ".init") == 0
11950 || strcmp (isec->output_section->name, ".fini") == 0))
11951 {
11952 if (isec->map_head.s->has_toc_reloc
11953 || isec->map_head.s->makes_toc_func_call)
11954 ret = 1;
11955 else if (!isec->map_head.s->call_check_done)
11956 {
11957 int recur;
11958 isec->call_check_in_progress = 1;
11959 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11960 isec->call_check_in_progress = 0;
11961 if (recur != 0)
11962 ret = recur;
11963 }
11964 }
11965
11966 if (ret == 1)
11967 isec->makes_toc_func_call = 1;
11968
11969 return ret;
11970}
11971
11972/* The linker repeatedly calls this function for each input section,
11973 in the order that input sections are linked into output sections.
11974 Build lists of input sections to determine groupings between which
11975 we may insert linker stubs. */
11976
11977bfd_boolean
11978ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11979{
11980 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11981
11982 if (htab == NULL)
11983 return FALSE;
11984
11985 if ((isec->output_section->flags & SEC_CODE) != 0
11986 && isec->output_section->id < htab->sec_info_arr_size)
11987 {
11988 /* This happens to make the list in reverse order,
11989 which is what we want. */
11990 htab->sec_info[isec->id].u.list
11991 = htab->sec_info[isec->output_section->id].u.list;
11992 htab->sec_info[isec->output_section->id].u.list = isec;
11993 }
11994
11995 if (htab->multi_toc_needed)
11996 {
11997 /* Analyse sections that aren't already flagged as needing a
11998 valid toc pointer. Exclude .fixup for the linux kernel.
11999 .fixup contains branches, but only back to the function that
12000 hit an exception. */
12001 if (!(isec->has_toc_reloc
12002 || (isec->flags & SEC_CODE) == 0
12003 || strcmp (isec->name, ".fixup") == 0
12004 || isec->call_check_done))
12005 {
12006 if (toc_adjusting_stub_needed (info, isec) < 0)
12007 return FALSE;
12008 }
12009 /* Make all sections use the TOC assigned for this object file.
12010 This will be wrong for pasted sections; We fix that in
12011 check_pasted_section(). */
12012 if (elf_gp (isec->owner) != 0)
12013 htab->toc_curr = elf_gp (isec->owner);
12014 }
12015
12016 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12017 return TRUE;
12018}
12019
12020/* Check that all .init and .fini sections use the same toc, if they
12021 have toc relocs. */
12022
12023static bfd_boolean
12024check_pasted_section (struct bfd_link_info *info, const char *name)
12025{
12026 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12027
12028 if (o != NULL)
12029 {
12030 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12031 bfd_vma toc_off = 0;
12032 asection *i;
12033
12034 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12035 if (i->has_toc_reloc)
12036 {
12037 if (toc_off == 0)
12038 toc_off = htab->sec_info[i->id].toc_off;
12039 else if (toc_off != htab->sec_info[i->id].toc_off)
12040 return FALSE;
12041 }
12042
12043 if (toc_off == 0)
12044 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12045 if (i->makes_toc_func_call)
12046 {
12047 toc_off = htab->sec_info[i->id].toc_off;
12048 break;
12049 }
12050
12051 /* Make sure the whole pasted function uses the same toc offset. */
12052 if (toc_off != 0)
12053 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12054 htab->sec_info[i->id].toc_off = toc_off;
12055 }
12056 return TRUE;
12057}
12058
12059bfd_boolean
12060ppc64_elf_check_init_fini (struct bfd_link_info *info)
12061{
12062 return (check_pasted_section (info, ".init")
12063 & check_pasted_section (info, ".fini"));
12064}
12065
12066/* See whether we can group stub sections together. Grouping stub
12067 sections may result in fewer stubs. More importantly, we need to
12068 put all .init* and .fini* stubs at the beginning of the .init or
12069 .fini output sections respectively, because glibc splits the
12070 _init and _fini functions into multiple parts. Putting a stub in
12071 the middle of a function is not a good idea. */
12072
12073static bfd_boolean
12074group_sections (struct bfd_link_info *info,
12075 bfd_size_type stub_group_size,
12076 bfd_boolean stubs_always_before_branch)
12077{
12078 struct ppc_link_hash_table *htab;
12079 asection *osec;
12080 bfd_boolean suppress_size_errors;
12081
12082 htab = ppc_hash_table (info);
12083 if (htab == NULL)
12084 return FALSE;
12085
12086 suppress_size_errors = FALSE;
12087 if (stub_group_size == 1)
12088 {
12089 /* Default values. */
12090 if (stubs_always_before_branch)
12091 stub_group_size = 0x1e00000;
12092 else
12093 stub_group_size = 0x1c00000;
12094 suppress_size_errors = TRUE;
12095 }
12096
12097 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12098 {
12099 asection *tail;
12100
12101 if (osec->id >= htab->sec_info_arr_size)
12102 continue;
12103
12104 tail = htab->sec_info[osec->id].u.list;
12105 while (tail != NULL)
12106 {
12107 asection *curr;
12108 asection *prev;
12109 bfd_size_type total;
12110 bfd_boolean big_sec;
12111 bfd_vma curr_toc;
12112 struct map_stub *group;
12113 bfd_size_type group_size;
12114
12115 curr = tail;
12116 total = tail->size;
12117 group_size = (ppc64_elf_section_data (tail) != NULL
12118 && ppc64_elf_section_data (tail)->has_14bit_branch
12119 ? stub_group_size >> 10 : stub_group_size);
12120
12121 big_sec = total > group_size;
12122 if (big_sec && !suppress_size_errors)
12123 /* xgettext:c-format */
12124 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12125 tail->owner, tail);
12126 curr_toc = htab->sec_info[tail->id].toc_off;
12127
12128 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12129 && ((total += curr->output_offset - prev->output_offset)
12130 < (ppc64_elf_section_data (prev) != NULL
12131 && ppc64_elf_section_data (prev)->has_14bit_branch
12132 ? (group_size = stub_group_size >> 10) : group_size))
12133 && htab->sec_info[prev->id].toc_off == curr_toc)
12134 curr = prev;
12135
12136 /* OK, the size from the start of CURR to the end is less
12137 than group_size and thus can be handled by one stub
12138 section. (or the tail section is itself larger than
12139 group_size, in which case we may be toast.) We should
12140 really be keeping track of the total size of stubs added
12141 here, as stubs contribute to the final output section
12142 size. That's a little tricky, and this way will only
12143 break if stubs added make the total size more than 2^25,
12144 ie. for the default stub_group_size, if stubs total more
12145 than 2097152 bytes, or nearly 75000 plt call stubs. */
12146 group = bfd_alloc (curr->owner, sizeof (*group));
12147 if (group == NULL)
12148 return FALSE;
12149 group->link_sec = curr;
12150 group->stub_sec = NULL;
12151 group->needs_save_res = 0;
12152 group->next = htab->group;
12153 htab->group = group;
12154 do
12155 {
12156 prev = htab->sec_info[tail->id].u.list;
12157 /* Set up this stub group. */
12158 htab->sec_info[tail->id].u.group = group;
12159 }
12160 while (tail != curr && (tail = prev) != NULL);
12161
12162 /* But wait, there's more! Input sections up to group_size
12163 bytes before the stub section can be handled by it too.
12164 Don't do this if we have a really large section after the
12165 stubs, as adding more stubs increases the chance that
12166 branches may not reach into the stub section. */
12167 if (!stubs_always_before_branch && !big_sec)
12168 {
12169 total = 0;
12170 while (prev != NULL
12171 && ((total += tail->output_offset - prev->output_offset)
12172 < (ppc64_elf_section_data (prev) != NULL
12173 && ppc64_elf_section_data (prev)->has_14bit_branch
12174 ? (group_size = stub_group_size >> 10) : group_size))
12175 && htab->sec_info[prev->id].toc_off == curr_toc)
12176 {
12177 tail = prev;
12178 prev = htab->sec_info[tail->id].u.list;
12179 htab->sec_info[tail->id].u.group = group;
12180 }
12181 }
12182 tail = prev;
12183 }
12184 }
12185 return TRUE;
12186}
12187
12188static const unsigned char glink_eh_frame_cie[] =
12189{
12190 0, 0, 0, 16, /* length. */
12191 0, 0, 0, 0, /* id. */
12192 1, /* CIE version. */
12193 'z', 'R', 0, /* Augmentation string. */
12194 4, /* Code alignment. */
12195 0x78, /* Data alignment. */
12196 65, /* RA reg. */
12197 1, /* Augmentation size. */
12198 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12199 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12200 0, 0, 0, 0
12201};
12202
12203/* Stripping output sections is normally done before dynamic section
12204 symbols have been allocated. This function is called later, and
12205 handles cases like htab->brlt which is mapped to its own output
12206 section. */
12207
12208static void
12209maybe_strip_output (struct bfd_link_info *info, asection *isec)
12210{
12211 if (isec->size == 0
12212 && isec->output_section->size == 0
12213 && !(isec->output_section->flags & SEC_KEEP)
12214 && !bfd_section_removed_from_list (info->output_bfd,
12215 isec->output_section)
12216 && elf_section_data (isec->output_section)->dynindx == 0)
12217 {
12218 isec->output_section->flags |= SEC_EXCLUDE;
12219 bfd_section_list_remove (info->output_bfd, isec->output_section);
12220 info->output_bfd->section_count--;
12221 }
12222}
12223
12224/* Determine and set the size of the stub section for a final link.
12225
12226 The basic idea here is to examine all the relocations looking for
12227 PC-relative calls to a target that is unreachable with a "bl"
12228 instruction. */
12229
12230bfd_boolean
12231ppc64_elf_size_stubs (struct bfd_link_info *info)
12232{
12233 bfd_size_type stub_group_size;
12234 bfd_boolean stubs_always_before_branch;
12235 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12236
12237 if (htab == NULL)
12238 return FALSE;
12239
12240 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12241 htab->params->plt_thread_safe = 1;
12242 if (!htab->opd_abi)
12243 htab->params->plt_thread_safe = 0;
12244 else if (htab->params->plt_thread_safe == -1)
12245 {
12246 static const char *const thread_starter[] =
12247 {
12248 "pthread_create",
12249 /* libstdc++ */
12250 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12251 /* librt */
12252 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12253 "mq_notify", "create_timer",
12254 /* libanl */
12255 "getaddrinfo_a",
12256 /* libgomp */
12257 "GOMP_parallel",
12258 "GOMP_parallel_start",
12259 "GOMP_parallel_loop_static",
12260 "GOMP_parallel_loop_static_start",
12261 "GOMP_parallel_loop_dynamic",
12262 "GOMP_parallel_loop_dynamic_start",
12263 "GOMP_parallel_loop_guided",
12264 "GOMP_parallel_loop_guided_start",
12265 "GOMP_parallel_loop_runtime",
12266 "GOMP_parallel_loop_runtime_start",
12267 "GOMP_parallel_sections",
12268 "GOMP_parallel_sections_start",
12269 /* libgo */
12270 "__go_go",
12271 };
12272 unsigned i;
12273
12274 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12275 {
12276 struct elf_link_hash_entry *h;
12277 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12278 FALSE, FALSE, TRUE);
12279 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12280 if (htab->params->plt_thread_safe)
12281 break;
12282 }
12283 }
12284 stubs_always_before_branch = htab->params->group_size < 0;
12285 if (htab->params->group_size < 0)
12286 stub_group_size = -htab->params->group_size;
12287 else
12288 stub_group_size = htab->params->group_size;
12289
12290 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12291 return FALSE;
12292
12293#define STUB_SHRINK_ITER 20
12294 /* Loop until no stubs added. After iteration 20 of this loop we may
12295 exit on a stub section shrinking. This is to break out of a
12296 pathological case where adding stubs on one iteration decreases
12297 section gaps (perhaps due to alignment), which then requires
12298 fewer or smaller stubs on the next iteration. */
12299
12300 while (1)
12301 {
12302 bfd *input_bfd;
12303 unsigned int bfd_indx;
12304 struct map_stub *group;
12305 asection *stub_sec;
12306
12307 htab->stub_iteration += 1;
12308
12309 for (input_bfd = info->input_bfds, bfd_indx = 0;
12310 input_bfd != NULL;
12311 input_bfd = input_bfd->link.next, bfd_indx++)
12312 {
12313 Elf_Internal_Shdr *symtab_hdr;
12314 asection *section;
12315 Elf_Internal_Sym *local_syms = NULL;
12316
12317 if (!is_ppc64_elf (input_bfd))
12318 continue;
12319
12320 /* We'll need the symbol table in a second. */
12321 symtab_hdr = &elf_symtab_hdr (input_bfd);
12322 if (symtab_hdr->sh_info == 0)
12323 continue;
12324
12325 /* Walk over each section attached to the input bfd. */
12326 for (section = input_bfd->sections;
12327 section != NULL;
12328 section = section->next)
12329 {
12330 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12331
12332 /* If there aren't any relocs, then there's nothing more
12333 to do. */
12334 if ((section->flags & SEC_RELOC) == 0
12335 || (section->flags & SEC_ALLOC) == 0
12336 || (section->flags & SEC_LOAD) == 0
12337 || (section->flags & SEC_CODE) == 0
12338 || section->reloc_count == 0)
12339 continue;
12340
12341 /* If this section is a link-once section that will be
12342 discarded, then don't create any stubs. */
12343 if (section->output_section == NULL
12344 || section->output_section->owner != info->output_bfd)
12345 continue;
12346
12347 /* Get the relocs. */
12348 internal_relocs
12349 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12350 info->keep_memory);
12351 if (internal_relocs == NULL)
12352 goto error_ret_free_local;
12353
12354 /* Now examine each relocation. */
12355 irela = internal_relocs;
12356 irelaend = irela + section->reloc_count;
12357 for (; irela < irelaend; irela++)
12358 {
12359 enum elf_ppc64_reloc_type r_type;
12360 unsigned int r_indx;
12361 enum ppc_stub_type stub_type;
12362 struct ppc_stub_hash_entry *stub_entry;
12363 asection *sym_sec, *code_sec;
12364 bfd_vma sym_value, code_value;
12365 bfd_vma destination;
12366 unsigned long local_off;
12367 bfd_boolean ok_dest;
12368 struct ppc_link_hash_entry *hash;
12369 struct ppc_link_hash_entry *fdh;
12370 struct elf_link_hash_entry *h;
12371 Elf_Internal_Sym *sym;
12372 char *stub_name;
12373 const asection *id_sec;
12374 struct _opd_sec_data *opd;
12375 struct plt_entry *plt_ent;
12376
12377 r_type = ELF64_R_TYPE (irela->r_info);
12378 r_indx = ELF64_R_SYM (irela->r_info);
12379
12380 if (r_type >= R_PPC64_max)
12381 {
12382 bfd_set_error (bfd_error_bad_value);
12383 goto error_ret_free_internal;
12384 }
12385
12386 /* Only look for stubs on branch instructions. */
12387 if (r_type != R_PPC64_REL24
12388 && r_type != R_PPC64_REL14
12389 && r_type != R_PPC64_REL14_BRTAKEN
12390 && r_type != R_PPC64_REL14_BRNTAKEN)
12391 continue;
12392
12393 /* Now determine the call target, its name, value,
12394 section. */
12395 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12396 r_indx, input_bfd))
12397 goto error_ret_free_internal;
12398 hash = (struct ppc_link_hash_entry *) h;
12399
12400 ok_dest = FALSE;
12401 fdh = NULL;
12402 sym_value = 0;
12403 if (hash == NULL)
12404 {
12405 sym_value = sym->st_value;
12406 if (sym_sec != NULL
12407 && sym_sec->output_section != NULL)
12408 ok_dest = TRUE;
12409 }
12410 else if (hash->elf.root.type == bfd_link_hash_defined
12411 || hash->elf.root.type == bfd_link_hash_defweak)
12412 {
12413 sym_value = hash->elf.root.u.def.value;
12414 if (sym_sec->output_section != NULL)
12415 ok_dest = TRUE;
12416 }
12417 else if (hash->elf.root.type == bfd_link_hash_undefweak
12418 || hash->elf.root.type == bfd_link_hash_undefined)
12419 {
12420 /* Recognise an old ABI func code entry sym, and
12421 use the func descriptor sym instead if it is
12422 defined. */
12423 if (hash->elf.root.root.string[0] == '.'
12424 && (fdh = lookup_fdh (hash, htab)) != NULL)
12425 {
12426 if (fdh->elf.root.type == bfd_link_hash_defined
12427 || fdh->elf.root.type == bfd_link_hash_defweak)
12428 {
12429 sym_sec = fdh->elf.root.u.def.section;
12430 sym_value = fdh->elf.root.u.def.value;
12431 if (sym_sec->output_section != NULL)
12432 ok_dest = TRUE;
12433 }
12434 else
12435 fdh = NULL;
12436 }
12437 }
12438 else
12439 {
12440 bfd_set_error (bfd_error_bad_value);
12441 goto error_ret_free_internal;
12442 }
12443
12444 destination = 0;
12445 local_off = 0;
12446 if (ok_dest)
12447 {
12448 sym_value += irela->r_addend;
12449 destination = (sym_value
12450 + sym_sec->output_offset
12451 + sym_sec->output_section->vma);
12452 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12453 ? hash->elf.other
12454 : sym->st_other);
12455 }
12456
12457 code_sec = sym_sec;
12458 code_value = sym_value;
12459 opd = get_opd_info (sym_sec);
12460 if (opd != NULL)
12461 {
12462 bfd_vma dest;
12463
12464 if (hash == NULL && opd->adjust != NULL)
12465 {
12466 long adjust = opd->adjust[OPD_NDX (sym_value)];
12467 if (adjust == -1)
12468 continue;
12469 code_value += adjust;
12470 sym_value += adjust;
12471 }
12472 dest = opd_entry_value (sym_sec, sym_value,
12473 &code_sec, &code_value, FALSE);
12474 if (dest != (bfd_vma) -1)
12475 {
12476 destination = dest;
12477 if (fdh != NULL)
12478 {
12479 /* Fixup old ABI sym to point at code
12480 entry. */
12481 hash->elf.root.type = bfd_link_hash_defweak;
12482 hash->elf.root.u.def.section = code_sec;
12483 hash->elf.root.u.def.value = code_value;
12484 }
12485 }
12486 }
12487
12488 /* Determine what (if any) linker stub is needed. */
12489 plt_ent = NULL;
12490 stub_type = ppc_type_of_stub (section, irela, &hash,
12491 &plt_ent, destination,
12492 local_off);
12493
12494 if (stub_type != ppc_stub_plt_call)
12495 {
12496 /* Check whether we need a TOC adjusting stub.
12497 Since the linker pastes together pieces from
12498 different object files when creating the
12499 _init and _fini functions, it may be that a
12500 call to what looks like a local sym is in
12501 fact a call needing a TOC adjustment. */
12502 if (code_sec != NULL
12503 && code_sec->output_section != NULL
12504 && (htab->sec_info[code_sec->id].toc_off
12505 != htab->sec_info[section->id].toc_off)
12506 && (code_sec->has_toc_reloc
12507 || code_sec->makes_toc_func_call))
12508 stub_type = ppc_stub_long_branch_r2off;
12509 }
12510
12511 if (stub_type == ppc_stub_none)
12512 continue;
12513
12514 /* __tls_get_addr calls might be eliminated. */
12515 if (stub_type != ppc_stub_plt_call
12516 && hash != NULL
12517 && (hash == htab->tls_get_addr
12518 || hash == htab->tls_get_addr_fd)
12519 && section->has_tls_reloc
12520 && irela != internal_relocs)
12521 {
12522 /* Get tls info. */
12523 unsigned char *tls_mask;
12524
12525 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12526 irela - 1, input_bfd))
12527 goto error_ret_free_internal;
12528 if (*tls_mask != 0)
12529 continue;
12530 }
12531
12532 if (stub_type == ppc_stub_plt_call
12533 && irela + 1 < irelaend
12534 && irela[1].r_offset == irela->r_offset + 4
12535 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12536 {
12537 if (!tocsave_find (htab, INSERT,
12538 &local_syms, irela + 1, input_bfd))
12539 goto error_ret_free_internal;
12540 }
12541 else if (stub_type == ppc_stub_plt_call)
12542 stub_type = ppc_stub_plt_call_r2save;
12543
12544 /* Support for grouping stub sections. */
12545 id_sec = htab->sec_info[section->id].u.group->link_sec;
12546
12547 /* Get the name of this stub. */
12548 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12549 if (!stub_name)
12550 goto error_ret_free_internal;
12551
12552 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12553 stub_name, FALSE, FALSE);
12554 if (stub_entry != NULL)
12555 {
12556 /* The proper stub has already been created. */
12557 free (stub_name);
12558 if (stub_type == ppc_stub_plt_call_r2save)
12559 stub_entry->stub_type = stub_type;
12560 continue;
12561 }
12562
12563 stub_entry = ppc_add_stub (stub_name, section, info);
12564 if (stub_entry == NULL)
12565 {
12566 free (stub_name);
12567 error_ret_free_internal:
12568 if (elf_section_data (section)->relocs == NULL)
12569 free (internal_relocs);
12570 error_ret_free_local:
12571 if (local_syms != NULL
12572 && (symtab_hdr->contents
12573 != (unsigned char *) local_syms))
12574 free (local_syms);
12575 return FALSE;
12576 }
12577
12578 stub_entry->stub_type = stub_type;
12579 if (stub_type != ppc_stub_plt_call
12580 && stub_type != ppc_stub_plt_call_r2save)
12581 {
12582 stub_entry->target_value = code_value;
12583 stub_entry->target_section = code_sec;
12584 }
12585 else
12586 {
12587 stub_entry->target_value = sym_value;
12588 stub_entry->target_section = sym_sec;
12589 }
12590 stub_entry->h = hash;
12591 stub_entry->plt_ent = plt_ent;
12592 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12593
12594 if (stub_entry->h != NULL)
12595 htab->stub_globals += 1;
12596 }
12597
12598 /* We're done with the internal relocs, free them. */
12599 if (elf_section_data (section)->relocs != internal_relocs)
12600 free (internal_relocs);
12601 }
12602
12603 if (local_syms != NULL
12604 && symtab_hdr->contents != (unsigned char *) local_syms)
12605 {
12606 if (!info->keep_memory)
12607 free (local_syms);
12608 else
12609 symtab_hdr->contents = (unsigned char *) local_syms;
12610 }
12611 }
12612
12613 /* We may have added some stubs. Find out the new size of the
12614 stub sections. */
12615 for (stub_sec = htab->params->stub_bfd->sections;
12616 stub_sec != NULL;
12617 stub_sec = stub_sec->next)
12618 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12619 {
12620 if (htab->stub_iteration <= STUB_SHRINK_ITER
12621 || stub_sec->rawsize < stub_sec->size)
12622 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12623 stub_sec->rawsize = stub_sec->size;
12624 stub_sec->size = 0;
12625 stub_sec->reloc_count = 0;
12626 stub_sec->flags &= ~SEC_RELOC;
12627 }
12628
12629 htab->brlt->size = 0;
12630 htab->brlt->reloc_count = 0;
12631 htab->brlt->flags &= ~SEC_RELOC;
12632 if (htab->relbrlt != NULL)
12633 htab->relbrlt->size = 0;
12634
12635 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12636
12637 for (group = htab->group; group != NULL; group = group->next)
12638 if (group->needs_save_res)
12639 group->stub_sec->size += htab->sfpr->size;
12640
12641 if (info->emitrelocations
12642 && htab->glink != NULL && htab->glink->size != 0)
12643 {
12644 htab->glink->reloc_count = 1;
12645 htab->glink->flags |= SEC_RELOC;
12646 }
12647
12648 if (htab->glink_eh_frame != NULL
12649 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12650 && htab->glink_eh_frame->output_section->size != 0)
12651 {
12652 size_t size = 0, align;
12653
12654 for (stub_sec = htab->params->stub_bfd->sections;
12655 stub_sec != NULL;
12656 stub_sec = stub_sec->next)
12657 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12658 size += 24;
12659 if (htab->glink != NULL && htab->glink->size != 0)
12660 size += 24;
12661 if (size != 0)
12662 size += sizeof (glink_eh_frame_cie);
12663 align = 1;
12664 align <<= htab->glink_eh_frame->output_section->alignment_power;
12665 align -= 1;
12666 size = (size + align) & ~align;
12667 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12668 htab->glink_eh_frame->size = size;
12669 }
12670
12671 if (htab->params->plt_stub_align != 0)
12672 for (stub_sec = htab->params->stub_bfd->sections;
12673 stub_sec != NULL;
12674 stub_sec = stub_sec->next)
12675 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12676 stub_sec->size = ((stub_sec->size
12677 + (1 << htab->params->plt_stub_align) - 1)
12678 & -(1 << htab->params->plt_stub_align));
12679
12680 for (stub_sec = htab->params->stub_bfd->sections;
12681 stub_sec != NULL;
12682 stub_sec = stub_sec->next)
12683 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12684 && stub_sec->rawsize != stub_sec->size
12685 && (htab->stub_iteration <= STUB_SHRINK_ITER
12686 || stub_sec->rawsize < stub_sec->size))
12687 break;
12688
12689 if (stub_sec == NULL
12690 && (htab->glink_eh_frame == NULL
12691 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12692 break;
12693
12694 /* Ask the linker to do its stuff. */
12695 (*htab->params->layout_sections_again) ();
12696 }
12697
12698 if (htab->glink_eh_frame != NULL
12699 && htab->glink_eh_frame->size != 0)
12700 {
12701 bfd_vma val;
12702 bfd_byte *p, *last_fde;
12703 size_t last_fde_len, size, align, pad;
12704 asection *stub_sec;
12705
12706 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12707 if (p == NULL)
12708 return FALSE;
12709 htab->glink_eh_frame->contents = p;
12710 last_fde = p;
12711
12712 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12713 /* CIE length (rewrite in case little-endian). */
12714 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12715 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12716 p += sizeof (glink_eh_frame_cie);
12717
12718 for (stub_sec = htab->params->stub_bfd->sections;
12719 stub_sec != NULL;
12720 stub_sec = stub_sec->next)
12721 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12722 {
12723 last_fde = p;
12724 last_fde_len = 20;
12725 /* FDE length. */
12726 bfd_put_32 (htab->elf.dynobj, 20, p);
12727 p += 4;
12728 /* CIE pointer. */
12729 val = p - htab->glink_eh_frame->contents;
12730 bfd_put_32 (htab->elf.dynobj, val, p);
12731 p += 4;
12732 /* Offset to stub section, written later. */
12733 p += 4;
12734 /* stub section size. */
12735 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12736 p += 4;
12737 /* Augmentation. */
12738 p += 1;
12739 /* Pad. */
12740 p += 7;
12741 }
12742 if (htab->glink != NULL && htab->glink->size != 0)
12743 {
12744 last_fde = p;
12745 last_fde_len = 20;
12746 /* FDE length. */
12747 bfd_put_32 (htab->elf.dynobj, 20, p);
12748 p += 4;
12749 /* CIE pointer. */
12750 val = p - htab->glink_eh_frame->contents;
12751 bfd_put_32 (htab->elf.dynobj, val, p);
12752 p += 4;
12753 /* Offset to .glink, written later. */
12754 p += 4;
12755 /* .glink size. */
12756 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12757 p += 4;
12758 /* Augmentation. */
12759 p += 1;
12760
12761 *p++ = DW_CFA_advance_loc + 1;
12762 *p++ = DW_CFA_register;
12763 *p++ = 65;
12764 *p++ = htab->opd_abi ? 12 : 0;
12765 *p++ = DW_CFA_advance_loc + 4;
12766 *p++ = DW_CFA_restore_extended;
12767 *p++ = 65;
12768 }
12769 /* Subsume any padding into the last FDE if user .eh_frame
12770 sections are aligned more than glink_eh_frame. Otherwise any
12771 zero padding will be seen as a terminator. */
12772 size = p - htab->glink_eh_frame->contents;
12773 align = 1;
12774 align <<= htab->glink_eh_frame->output_section->alignment_power;
12775 align -= 1;
12776 pad = ((size + align) & ~align) - size;
12777 htab->glink_eh_frame->size = size + pad;
12778 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12779 }
12780
12781 maybe_strip_output (info, htab->brlt);
12782 if (htab->glink_eh_frame != NULL)
12783 maybe_strip_output (info, htab->glink_eh_frame);
12784
12785 return TRUE;
12786}
12787
12788/* Called after we have determined section placement. If sections
12789 move, we'll be called again. Provide a value for TOCstart. */
12790
12791bfd_vma
12792ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12793{
12794 asection *s;
12795 bfd_vma TOCstart, adjust;
12796
12797 if (info != NULL)
12798 {
12799 struct elf_link_hash_entry *h;
12800 struct elf_link_hash_table *htab = elf_hash_table (info);
12801
12802 if (is_elf_hash_table (htab)
12803 && htab->hgot != NULL)
12804 h = htab->hgot;
12805 else
12806 {
12807 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12808 if (is_elf_hash_table (htab))
12809 htab->hgot = h;
12810 }
12811 if (h != NULL
12812 && h->root.type == bfd_link_hash_defined
12813 && !h->root.linker_def
12814 && (!is_elf_hash_table (htab)
12815 || h->def_regular))
12816 {
12817 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12818 + h->root.u.def.section->output_offset
12819 + h->root.u.def.section->output_section->vma);
12820 _bfd_set_gp_value (obfd, TOCstart);
12821 return TOCstart;
12822 }
12823 }
12824
12825 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12826 order. The TOC starts where the first of these sections starts. */
12827 s = bfd_get_section_by_name (obfd, ".got");
12828 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12829 s = bfd_get_section_by_name (obfd, ".toc");
12830 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12831 s = bfd_get_section_by_name (obfd, ".tocbss");
12832 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12833 s = bfd_get_section_by_name (obfd, ".plt");
12834 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12835 {
12836 /* This may happen for
12837 o references to TOC base (SYM@toc / TOC[tc0]) without a
12838 .toc directive
12839 o bad linker script
12840 o --gc-sections and empty TOC sections
12841
12842 FIXME: Warn user? */
12843
12844 /* Look for a likely section. We probably won't even be
12845 using TOCstart. */
12846 for (s = obfd->sections; s != NULL; s = s->next)
12847 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12848 | SEC_EXCLUDE))
12849 == (SEC_ALLOC | SEC_SMALL_DATA))
12850 break;
12851 if (s == NULL)
12852 for (s = obfd->sections; s != NULL; s = s->next)
12853 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12854 == (SEC_ALLOC | SEC_SMALL_DATA))
12855 break;
12856 if (s == NULL)
12857 for (s = obfd->sections; s != NULL; s = s->next)
12858 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12859 == SEC_ALLOC)
12860 break;
12861 if (s == NULL)
12862 for (s = obfd->sections; s != NULL; s = s->next)
12863 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12864 break;
12865 }
12866
12867 TOCstart = 0;
12868 if (s != NULL)
12869 TOCstart = s->output_section->vma + s->output_offset;
12870
12871 /* Force alignment. */
12872 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12873 TOCstart -= adjust;
12874 _bfd_set_gp_value (obfd, TOCstart);
12875
12876 if (info != NULL && s != NULL)
12877 {
12878 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12879
12880 if (htab != NULL)
12881 {
12882 if (htab->elf.hgot != NULL)
12883 {
12884 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12885 htab->elf.hgot->root.u.def.section = s;
12886 }
12887 }
12888 else
12889 {
12890 struct bfd_link_hash_entry *bh = NULL;
12891 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12892 s, TOC_BASE_OFF - adjust,
12893 NULL, FALSE, FALSE, &bh);
12894 }
12895 }
12896 return TOCstart;
12897}
12898
12899/* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12900 write out any global entry stubs. */
12901
12902static bfd_boolean
12903build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12904{
12905 struct bfd_link_info *info;
12906 struct ppc_link_hash_table *htab;
12907 struct plt_entry *pent;
12908 asection *s;
12909
12910 if (h->root.type == bfd_link_hash_indirect)
12911 return TRUE;
12912
12913 if (!h->pointer_equality_needed)
12914 return TRUE;
12915
12916 if (h->def_regular)
12917 return TRUE;
12918
12919 info = inf;
12920 htab = ppc_hash_table (info);
12921 if (htab == NULL)
12922 return FALSE;
12923
12924 s = htab->glink;
12925 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12926 if (pent->plt.offset != (bfd_vma) -1
12927 && pent->addend == 0)
12928 {
12929 bfd_byte *p;
12930 asection *plt;
12931 bfd_vma off;
12932
12933 p = s->contents + h->root.u.def.value;
12934 plt = htab->elf.splt;
12935 if (!htab->elf.dynamic_sections_created
12936 || h->dynindx == -1)
12937 plt = htab->elf.iplt;
12938 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12939 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12940
12941 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12942 {
12943 info->callbacks->einfo
12944 (_("%P: linkage table error against `%T'\n"),
12945 h->root.root.string);
12946 bfd_set_error (bfd_error_bad_value);
12947 htab->stub_error = TRUE;
12948 }
12949
12950 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12951 if (htab->params->emit_stub_syms)
12952 {
12953 size_t len = strlen (h->root.root.string);
12954 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12955
12956 if (name == NULL)
12957 return FALSE;
12958
12959 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12960 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12961 if (h == NULL)
12962 return FALSE;
12963 if (h->root.type == bfd_link_hash_new)
12964 {
12965 h->root.type = bfd_link_hash_defined;
12966 h->root.u.def.section = s;
12967 h->root.u.def.value = p - s->contents;
12968 h->ref_regular = 1;
12969 h->def_regular = 1;
12970 h->ref_regular_nonweak = 1;
12971 h->forced_local = 1;
12972 h->non_elf = 0;
12973 h->root.linker_def = 1;
12974 }
12975 }
12976
12977 if (PPC_HA (off) != 0)
12978 {
12979 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12980 p += 4;
12981 }
12982 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12983 p += 4;
12984 bfd_put_32 (s->owner, MTCTR_R12, p);
12985 p += 4;
12986 bfd_put_32 (s->owner, BCTR, p);
12987 break;
12988 }
12989 return TRUE;
12990}
12991
12992/* Build all the stubs associated with the current output file.
12993 The stubs are kept in a hash table attached to the main linker
12994 hash table. This function is called via gldelf64ppc_finish. */
12995
12996bfd_boolean
12997ppc64_elf_build_stubs (struct bfd_link_info *info,
12998 char **stats)
12999{
13000 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13001 struct map_stub *group;
13002 asection *stub_sec;
13003 bfd_byte *p;
13004 int stub_sec_count = 0;
13005
13006 if (htab == NULL)
13007 return FALSE;
13008
13009 /* Allocate memory to hold the linker stubs. */
13010 for (stub_sec = htab->params->stub_bfd->sections;
13011 stub_sec != NULL;
13012 stub_sec = stub_sec->next)
13013 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13014 && stub_sec->size != 0)
13015 {
13016 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13017 if (stub_sec->contents == NULL)
13018 return FALSE;
13019 stub_sec->size = 0;
13020 }
13021
13022 if (htab->glink != NULL && htab->glink->size != 0)
13023 {
13024 unsigned int indx;
13025 bfd_vma plt0;
13026
13027 /* Build the .glink plt call stub. */
13028 if (htab->params->emit_stub_syms)
13029 {
13030 struct elf_link_hash_entry *h;
13031 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13032 TRUE, FALSE, FALSE);
13033 if (h == NULL)
13034 return FALSE;
13035 if (h->root.type == bfd_link_hash_new)
13036 {
13037 h->root.type = bfd_link_hash_defined;
13038 h->root.u.def.section = htab->glink;
13039 h->root.u.def.value = 8;
13040 h->ref_regular = 1;
13041 h->def_regular = 1;
13042 h->ref_regular_nonweak = 1;
13043 h->forced_local = 1;
13044 h->non_elf = 0;
13045 h->root.linker_def = 1;
13046 }
13047 }
13048 plt0 = (htab->elf.splt->output_section->vma
13049 + htab->elf.splt->output_offset
13050 - 16);
13051 if (info->emitrelocations)
13052 {
13053 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13054 if (r == NULL)
13055 return FALSE;
13056 r->r_offset = (htab->glink->output_offset
13057 + htab->glink->output_section->vma);
13058 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13059 r->r_addend = plt0;
13060 }
13061 p = htab->glink->contents;
13062 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13063 bfd_put_64 (htab->glink->owner, plt0, p);
13064 p += 8;
13065 if (htab->opd_abi)
13066 {
13067 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13068 p += 4;
13069 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13070 p += 4;
13071 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13072 p += 4;
13073 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13074 p += 4;
13075 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13076 p += 4;
13077 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13078 p += 4;
13079 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13080 p += 4;
13081 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13082 p += 4;
13083 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13084 p += 4;
13085 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13086 p += 4;
13087 }
13088 else
13089 {
13090 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13091 p += 4;
13092 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13093 p += 4;
13094 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13095 p += 4;
13096 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13097 p += 4;
13098 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13099 p += 4;
13100 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13101 p += 4;
13102 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13103 p += 4;
13104 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13105 p += 4;
13106 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13107 p += 4;
13108 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13109 p += 4;
13110 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13111 p += 4;
13112 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13113 p += 4;
13114 }
13115 bfd_put_32 (htab->glink->owner, BCTR, p);
13116 p += 4;
13117 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13118 {
13119 bfd_put_32 (htab->glink->owner, NOP, p);
13120 p += 4;
13121 }
13122
13123 /* Build the .glink lazy link call stubs. */
13124 indx = 0;
13125 while (p < htab->glink->contents + htab->glink->rawsize)
13126 {
13127 if (htab->opd_abi)
13128 {
13129 if (indx < 0x8000)
13130 {
13131 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13132 p += 4;
13133 }
13134 else
13135 {
13136 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13137 p += 4;
13138 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13139 p);
13140 p += 4;
13141 }
13142 }
13143 bfd_put_32 (htab->glink->owner,
13144 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13145 indx++;
13146 p += 4;
13147 }
13148
13149 /* Build .glink global entry stubs. */
13150 if (htab->glink->size > htab->glink->rawsize)
13151 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13152 }
13153
13154 if (htab->brlt != NULL && htab->brlt->size != 0)
13155 {
13156 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13157 htab->brlt->size);
13158 if (htab->brlt->contents == NULL)
13159 return FALSE;
13160 }
13161 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13162 {
13163 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13164 htab->relbrlt->size);
13165 if (htab->relbrlt->contents == NULL)
13166 return FALSE;
13167 }
13168
13169 /* Build the stubs as directed by the stub hash table. */
13170 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13171
13172 for (group = htab->group; group != NULL; group = group->next)
13173 if (group->needs_save_res)
13174 {
13175 stub_sec = group->stub_sec;
13176 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13177 htab->sfpr->size);
13178 if (htab->params->emit_stub_syms)
13179 {
13180 unsigned int i;
13181
13182 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13183 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13184 return FALSE;
13185 }
13186 stub_sec->size += htab->sfpr->size;
13187 }
13188
13189 if (htab->relbrlt != NULL)
13190 htab->relbrlt->reloc_count = 0;
13191
13192 if (htab->params->plt_stub_align != 0)
13193 for (stub_sec = htab->params->stub_bfd->sections;
13194 stub_sec != NULL;
13195 stub_sec = stub_sec->next)
13196 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13197 stub_sec->size = ((stub_sec->size
13198 + (1 << htab->params->plt_stub_align) - 1)
13199 & -(1 << htab->params->plt_stub_align));
13200
13201 for (stub_sec = htab->params->stub_bfd->sections;
13202 stub_sec != NULL;
13203 stub_sec = stub_sec->next)
13204 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13205 {
13206 stub_sec_count += 1;
13207 if (stub_sec->rawsize != stub_sec->size
13208 && (htab->stub_iteration <= STUB_SHRINK_ITER
13209 || stub_sec->rawsize < stub_sec->size))
13210 break;
13211 }
13212
13213 /* Note that the glink_eh_frame check here is not only testing that
13214 the generated size matched the calculated size but also that
13215 bfd_elf_discard_info didn't make any changes to the section. */
13216 if (stub_sec != NULL
13217 || (htab->glink_eh_frame != NULL
13218 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13219 {
13220 htab->stub_error = TRUE;
13221 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13222 }
13223
13224 if (htab->stub_error)
13225 return FALSE;
13226
13227 if (stats != NULL)
13228 {
13229 *stats = bfd_malloc (500);
13230 if (*stats == NULL)
13231 return FALSE;
13232
13233 sprintf (*stats, _("linker stubs in %u group%s\n"
13234 " branch %lu\n"
13235 " toc adjust %lu\n"
13236 " long branch %lu\n"
13237 " long toc adj %lu\n"
13238 " plt call %lu\n"
13239 " plt call toc %lu\n"
13240 " global entry %lu"),
13241 stub_sec_count,
13242 stub_sec_count == 1 ? "" : "s",
13243 htab->stub_count[ppc_stub_long_branch - 1],
13244 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13245 htab->stub_count[ppc_stub_plt_branch - 1],
13246 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13247 htab->stub_count[ppc_stub_plt_call - 1],
13248 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13249 htab->stub_count[ppc_stub_global_entry - 1]);
13250 }
13251 return TRUE;
13252}
13253
13254/* This function undoes the changes made by add_symbol_adjust. */
13255
13256static bfd_boolean
13257undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13258{
13259 struct ppc_link_hash_entry *eh;
13260
13261 if (h->root.type == bfd_link_hash_indirect)
13262 return TRUE;
13263
13264 eh = (struct ppc_link_hash_entry *) h;
13265 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13266 return TRUE;
13267
13268 eh->elf.root.type = bfd_link_hash_undefined;
13269 return TRUE;
13270}
13271
13272void
13273ppc64_elf_restore_symbols (struct bfd_link_info *info)
13274{
13275 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13276
13277 if (htab != NULL)
13278 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
13279}
13280
13281/* What to do when ld finds relocations against symbols defined in
13282 discarded sections. */
13283
13284static unsigned int
13285ppc64_elf_action_discarded (asection *sec)
13286{
13287 if (strcmp (".opd", sec->name) == 0)
13288 return 0;
13289
13290 if (strcmp (".toc", sec->name) == 0)
13291 return 0;
13292
13293 if (strcmp (".toc1", sec->name) == 0)
13294 return 0;
13295
13296 return _bfd_elf_default_action_discarded (sec);
13297}
13298
13299/* The RELOCATE_SECTION function is called by the ELF backend linker
13300 to handle the relocations for a section.
13301
13302 The relocs are always passed as Rela structures; if the section
13303 actually uses Rel structures, the r_addend field will always be
13304 zero.
13305
13306 This function is responsible for adjust the section contents as
13307 necessary, and (if using Rela relocs and generating a
13308 relocatable output file) adjusting the reloc addend as
13309 necessary.
13310
13311 This function does not have to worry about setting the reloc
13312 address or the reloc symbol index.
13313
13314 LOCAL_SYMS is a pointer to the swapped in local symbols.
13315
13316 LOCAL_SECTIONS is an array giving the section in the input file
13317 corresponding to the st_shndx field of each local symbol.
13318
13319 The global hash table entry for the global symbols can be found
13320 via elf_sym_hashes (input_bfd).
13321
13322 When generating relocatable output, this function must handle
13323 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13324 going to be the section symbol corresponding to the output
13325 section, which means that the addend must be adjusted
13326 accordingly. */
13327
13328static bfd_boolean
13329ppc64_elf_relocate_section (bfd *output_bfd,
13330 struct bfd_link_info *info,
13331 bfd *input_bfd,
13332 asection *input_section,
13333 bfd_byte *contents,
13334 Elf_Internal_Rela *relocs,
13335 Elf_Internal_Sym *local_syms,
13336 asection **local_sections)
13337{
13338 struct ppc_link_hash_table *htab;
13339 Elf_Internal_Shdr *symtab_hdr;
13340 struct elf_link_hash_entry **sym_hashes;
13341 Elf_Internal_Rela *rel;
13342 Elf_Internal_Rela *wrel;
13343 Elf_Internal_Rela *relend;
13344 Elf_Internal_Rela outrel;
13345 bfd_byte *loc;
13346 struct got_entry **local_got_ents;
13347 bfd_vma TOCstart;
13348 bfd_boolean ret = TRUE;
13349 bfd_boolean is_opd;
13350 /* Assume 'at' branch hints. */
13351 bfd_boolean is_isa_v2 = TRUE;
13352 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13353
13354 /* Initialize howto table if needed. */
13355 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13356 ppc_howto_init ();
13357
13358 htab = ppc_hash_table (info);
13359 if (htab == NULL)
13360 return FALSE;
13361
13362 /* Don't relocate stub sections. */
13363 if (input_section->owner == htab->params->stub_bfd)
13364 return TRUE;
13365
13366 BFD_ASSERT (is_ppc64_elf (input_bfd));
13367
13368 local_got_ents = elf_local_got_ents (input_bfd);
13369 TOCstart = elf_gp (output_bfd);
13370 symtab_hdr = &elf_symtab_hdr (input_bfd);
13371 sym_hashes = elf_sym_hashes (input_bfd);
13372 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13373
13374 rel = wrel = relocs;
13375 relend = relocs + input_section->reloc_count;
13376 for (; rel < relend; wrel++, rel++)
13377 {
13378 enum elf_ppc64_reloc_type r_type;
13379 bfd_vma addend;
13380 bfd_reloc_status_type r;
13381 Elf_Internal_Sym *sym;
13382 asection *sec;
13383 struct elf_link_hash_entry *h_elf;
13384 struct ppc_link_hash_entry *h;
13385 struct ppc_link_hash_entry *fdh;
13386 const char *sym_name;
13387 unsigned long r_symndx, toc_symndx;
13388 bfd_vma toc_addend;
13389 unsigned char tls_mask, tls_gd, tls_type;
13390 unsigned char sym_type;
13391 bfd_vma relocation;
13392 bfd_boolean unresolved_reloc;
13393 bfd_boolean warned;
13394 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13395 unsigned int insn;
13396 unsigned int mask;
13397 struct ppc_stub_hash_entry *stub_entry;
13398 bfd_vma max_br_offset;
13399 bfd_vma from;
13400 Elf_Internal_Rela orig_rel;
13401 reloc_howto_type *howto;
13402 struct reloc_howto_struct alt_howto;
13403
13404 again:
13405 orig_rel = *rel;
13406
13407 r_type = ELF64_R_TYPE (rel->r_info);
13408 r_symndx = ELF64_R_SYM (rel->r_info);
13409
13410 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13411 symbol of the previous ADDR64 reloc. The symbol gives us the
13412 proper TOC base to use. */
13413 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13414 && wrel != relocs
13415 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13416 && is_opd)
13417 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13418
13419 sym = NULL;
13420 sec = NULL;
13421 h_elf = NULL;
13422 sym_name = NULL;
13423 unresolved_reloc = FALSE;
13424 warned = FALSE;
13425
13426 if (r_symndx < symtab_hdr->sh_info)
13427 {
13428 /* It's a local symbol. */
13429 struct _opd_sec_data *opd;
13430
13431 sym = local_syms + r_symndx;
13432 sec = local_sections[r_symndx];
13433 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13434 sym_type = ELF64_ST_TYPE (sym->st_info);
13435 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13436 opd = get_opd_info (sec);
13437 if (opd != NULL && opd->adjust != NULL)
13438 {
13439 long adjust = opd->adjust[OPD_NDX (sym->st_value
13440 + rel->r_addend)];
13441 if (adjust == -1)
13442 relocation = 0;
13443 else
13444 {
13445 /* If this is a relocation against the opd section sym
13446 and we have edited .opd, adjust the reloc addend so
13447 that ld -r and ld --emit-relocs output is correct.
13448 If it is a reloc against some other .opd symbol,
13449 then the symbol value will be adjusted later. */
13450 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13451 rel->r_addend += adjust;
13452 else
13453 relocation += adjust;
13454 }
13455 }
13456 }
13457 else
13458 {
13459 bfd_boolean ignored;
13460
13461 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13462 r_symndx, symtab_hdr, sym_hashes,
13463 h_elf, sec, relocation,
13464 unresolved_reloc, warned, ignored);
13465 sym_name = h_elf->root.root.string;
13466 sym_type = h_elf->type;
13467 if (sec != NULL
13468 && sec->owner == output_bfd
13469 && strcmp (sec->name, ".opd") == 0)
13470 {
13471 /* This is a symbol defined in a linker script. All
13472 such are defined in output sections, even those
13473 defined by simple assignment from a symbol defined in
13474 an input section. Transfer the symbol to an
13475 appropriate input .opd section, so that a branch to
13476 this symbol will be mapped to the location specified
13477 by the opd entry. */
13478 struct bfd_link_order *lo;
13479 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13480 if (lo->type == bfd_indirect_link_order)
13481 {
13482 asection *isec = lo->u.indirect.section;
13483 if (h_elf->root.u.def.value >= isec->output_offset
13484 && h_elf->root.u.def.value < (isec->output_offset
13485 + isec->size))
13486 {
13487 h_elf->root.u.def.value -= isec->output_offset;
13488 h_elf->root.u.def.section = isec;
13489 sec = isec;
13490 break;
13491 }
13492 }
13493 }
13494 }
13495 h = (struct ppc_link_hash_entry *) h_elf;
13496
13497 if (sec != NULL && discarded_section (sec))
13498 {
13499 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13500 input_bfd, input_section,
13501 contents + rel->r_offset);
13502 wrel->r_offset = rel->r_offset;
13503 wrel->r_info = 0;
13504 wrel->r_addend = 0;
13505
13506 /* For ld -r, remove relocations in debug sections against
13507 sections defined in discarded sections. Not done for
13508 non-debug to preserve relocs in .eh_frame which the
13509 eh_frame editing code expects to be present. */
13510 if (bfd_link_relocatable (info)
13511 && (input_section->flags & SEC_DEBUGGING))
13512 wrel--;
13513
13514 continue;
13515 }
13516
13517 if (bfd_link_relocatable (info))
13518 goto copy_reloc;
13519
13520 if (h != NULL && &h->elf == htab->elf.hgot)
13521 {
13522 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13523 sec = bfd_abs_section_ptr;
13524 unresolved_reloc = FALSE;
13525 }
13526
13527 /* TLS optimizations. Replace instruction sequences and relocs
13528 based on information we collected in tls_optimize. We edit
13529 RELOCS so that --emit-relocs will output something sensible
13530 for the final instruction stream. */
13531 tls_mask = 0;
13532 tls_gd = 0;
13533 toc_symndx = 0;
13534 if (h != NULL)
13535 tls_mask = h->tls_mask;
13536 else if (local_got_ents != NULL)
13537 {
13538 struct plt_entry **local_plt = (struct plt_entry **)
13539 (local_got_ents + symtab_hdr->sh_info);
13540 unsigned char *lgot_masks = (unsigned char *)
13541 (local_plt + symtab_hdr->sh_info);
13542 tls_mask = lgot_masks[r_symndx];
13543 }
13544 if (tls_mask == 0
13545 && (r_type == R_PPC64_TLS
13546 || r_type == R_PPC64_TLSGD
13547 || r_type == R_PPC64_TLSLD))
13548 {
13549 /* Check for toc tls entries. */
13550 unsigned char *toc_tls;
13551
13552 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13553 &local_syms, rel, input_bfd))
13554 return FALSE;
13555
13556 if (toc_tls)
13557 tls_mask = *toc_tls;
13558 }
13559
13560 /* Check that tls relocs are used with tls syms, and non-tls
13561 relocs are used with non-tls syms. */
13562 if (r_symndx != STN_UNDEF
13563 && r_type != R_PPC64_NONE
13564 && (h == NULL
13565 || h->elf.root.type == bfd_link_hash_defined
13566 || h->elf.root.type == bfd_link_hash_defweak)
13567 && (IS_PPC64_TLS_RELOC (r_type)
13568 != (sym_type == STT_TLS
13569 || (sym_type == STT_SECTION
13570 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13571 {
13572 if (tls_mask != 0
13573 && (r_type == R_PPC64_TLS
13574 || r_type == R_PPC64_TLSGD
13575 || r_type == R_PPC64_TLSLD))
13576 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13577 ;
13578 else
13579 info->callbacks->einfo
13580 (!IS_PPC64_TLS_RELOC (r_type)
13581 /* xgettext:c-format */
13582 ? _("%P: %H: %s used with TLS symbol `%T'\n")
13583 /* xgettext:c-format */
13584 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13585 input_bfd, input_section, rel->r_offset,
13586 ppc64_elf_howto_table[r_type]->name,
13587 sym_name);
13588 }
13589
13590 /* Ensure reloc mapping code below stays sane. */
13591 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13592 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13593 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13594 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13595 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13596 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13597 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13598 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13599 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13600 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13601 abort ();
13602
13603 switch (r_type)
13604 {
13605 default:
13606 break;
13607
13608 case R_PPC64_LO_DS_OPT:
13609 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13610 if ((insn & (0x3f << 26)) != 58u << 26)
13611 abort ();
13612 insn += (14u << 26) - (58u << 26);
13613 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13614 r_type = R_PPC64_TOC16_LO;
13615 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13616 break;
13617
13618 case R_PPC64_TOC16:
13619 case R_PPC64_TOC16_LO:
13620 case R_PPC64_TOC16_DS:
13621 case R_PPC64_TOC16_LO_DS:
13622 {
13623 /* Check for toc tls entries. */
13624 unsigned char *toc_tls;
13625 int retval;
13626
13627 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13628 &local_syms, rel, input_bfd);
13629 if (retval == 0)
13630 return FALSE;
13631
13632 if (toc_tls)
13633 {
13634 tls_mask = *toc_tls;
13635 if (r_type == R_PPC64_TOC16_DS
13636 || r_type == R_PPC64_TOC16_LO_DS)
13637 {
13638 if (tls_mask != 0
13639 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13640 goto toctprel;
13641 }
13642 else
13643 {
13644 /* If we found a GD reloc pair, then we might be
13645 doing a GD->IE transition. */
13646 if (retval == 2)
13647 {
13648 tls_gd = TLS_TPRELGD;
13649 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13650 goto tls_ldgd_opt;
13651 }
13652 else if (retval == 3)
13653 {
13654 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13655 goto tls_ldgd_opt;
13656 }
13657 }
13658 }
13659 }
13660 break;
13661
13662 case R_PPC64_GOT_TPREL16_HI:
13663 case R_PPC64_GOT_TPREL16_HA:
13664 if (tls_mask != 0
13665 && (tls_mask & TLS_TPREL) == 0)
13666 {
13667 rel->r_offset -= d_offset;
13668 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13669 r_type = R_PPC64_NONE;
13670 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13671 }
13672 break;
13673
13674 case R_PPC64_GOT_TPREL16_DS:
13675 case R_PPC64_GOT_TPREL16_LO_DS:
13676 if (tls_mask != 0
13677 && (tls_mask & TLS_TPREL) == 0)
13678 {
13679 toctprel:
13680 insn = bfd_get_32 (input_bfd,
13681 contents + rel->r_offset - d_offset);
13682 insn &= 31 << 21;
13683 insn |= 0x3c0d0000; /* addis 0,13,0 */
13684 bfd_put_32 (input_bfd, insn,
13685 contents + rel->r_offset - d_offset);
13686 r_type = R_PPC64_TPREL16_HA;
13687 if (toc_symndx != 0)
13688 {
13689 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13690 rel->r_addend = toc_addend;
13691 /* We changed the symbol. Start over in order to
13692 get h, sym, sec etc. right. */
13693 goto again;
13694 }
13695 else
13696 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13697 }
13698 break;
13699
13700 case R_PPC64_TLS:
13701 if (tls_mask != 0
13702 && (tls_mask & TLS_TPREL) == 0)
13703 {
13704 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13705 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13706 if (insn == 0)
13707 abort ();
13708 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13709 /* Was PPC64_TLS which sits on insn boundary, now
13710 PPC64_TPREL16_LO which is at low-order half-word. */
13711 rel->r_offset += d_offset;
13712 r_type = R_PPC64_TPREL16_LO;
13713 if (toc_symndx != 0)
13714 {
13715 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13716 rel->r_addend = toc_addend;
13717 /* We changed the symbol. Start over in order to
13718 get h, sym, sec etc. right. */
13719 goto again;
13720 }
13721 else
13722 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13723 }
13724 break;
13725
13726 case R_PPC64_GOT_TLSGD16_HI:
13727 case R_PPC64_GOT_TLSGD16_HA:
13728 tls_gd = TLS_TPRELGD;
13729 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13730 goto tls_gdld_hi;
13731 break;
13732
13733 case R_PPC64_GOT_TLSLD16_HI:
13734 case R_PPC64_GOT_TLSLD16_HA:
13735 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13736 {
13737 tls_gdld_hi:
13738 if ((tls_mask & tls_gd) != 0)
13739 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13740 + R_PPC64_GOT_TPREL16_DS);
13741 else
13742 {
13743 rel->r_offset -= d_offset;
13744 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13745 r_type = R_PPC64_NONE;
13746 }
13747 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13748 }
13749 break;
13750
13751 case R_PPC64_GOT_TLSGD16:
13752 case R_PPC64_GOT_TLSGD16_LO:
13753 tls_gd = TLS_TPRELGD;
13754 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13755 goto tls_ldgd_opt;
13756 break;
13757
13758 case R_PPC64_GOT_TLSLD16:
13759 case R_PPC64_GOT_TLSLD16_LO:
13760 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13761 {
13762 unsigned int insn1, insn2, insn3;
13763 bfd_vma offset;
13764
13765 tls_ldgd_opt:
13766 offset = (bfd_vma) -1;
13767 /* If not using the newer R_PPC64_TLSGD/LD to mark
13768 __tls_get_addr calls, we must trust that the call
13769 stays with its arg setup insns, ie. that the next
13770 reloc is the __tls_get_addr call associated with
13771 the current reloc. Edit both insns. */
13772 if (input_section->has_tls_get_addr_call
13773 && rel + 1 < relend
13774 && branch_reloc_hash_match (input_bfd, rel + 1,
13775 htab->tls_get_addr,
13776 htab->tls_get_addr_fd))
13777 offset = rel[1].r_offset;
13778 /* We read the low GOT_TLS (or TOC16) insn because we
13779 need to keep the destination reg. It may be
13780 something other than the usual r3, and moved to r3
13781 before the call by intervening code. */
13782 insn1 = bfd_get_32 (input_bfd,
13783 contents + rel->r_offset - d_offset);
13784 if ((tls_mask & tls_gd) != 0)
13785 {
13786 /* IE */
13787 insn1 &= (0x1f << 21) | (0x1f << 16);
13788 insn1 |= 58 << 26; /* ld */
13789 insn2 = 0x7c636a14; /* add 3,3,13 */
13790 if (offset != (bfd_vma) -1)
13791 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13792 if ((tls_mask & TLS_EXPLICIT) == 0)
13793 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13794 + R_PPC64_GOT_TPREL16_DS);
13795 else
13796 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13797 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13798 }
13799 else
13800 {
13801 /* LE */
13802 insn1 &= 0x1f << 21;
13803 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13804 insn2 = 0x38630000; /* addi 3,3,0 */
13805 if (tls_gd == 0)
13806 {
13807 /* Was an LD reloc. */
13808 if (toc_symndx)
13809 sec = local_sections[toc_symndx];
13810 for (r_symndx = 0;
13811 r_symndx < symtab_hdr->sh_info;
13812 r_symndx++)
13813 if (local_sections[r_symndx] == sec)
13814 break;
13815 if (r_symndx >= symtab_hdr->sh_info)
13816 r_symndx = STN_UNDEF;
13817 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13818 if (r_symndx != STN_UNDEF)
13819 rel->r_addend -= (local_syms[r_symndx].st_value
13820 + sec->output_offset
13821 + sec->output_section->vma);
13822 }
13823 else if (toc_symndx != 0)
13824 {
13825 r_symndx = toc_symndx;
13826 rel->r_addend = toc_addend;
13827 }
13828 r_type = R_PPC64_TPREL16_HA;
13829 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13830 if (offset != (bfd_vma) -1)
13831 {
13832 rel[1].r_info = ELF64_R_INFO (r_symndx,
13833 R_PPC64_TPREL16_LO);
13834 rel[1].r_offset = offset + d_offset;
13835 rel[1].r_addend = rel->r_addend;
13836 }
13837 }
13838 bfd_put_32 (input_bfd, insn1,
13839 contents + rel->r_offset - d_offset);
13840 if (offset != (bfd_vma) -1)
13841 {
13842 insn3 = bfd_get_32 (input_bfd,
13843 contents + offset + 4);
13844 if (insn3 == NOP
13845 || insn3 == CROR_151515 || insn3 == CROR_313131)
13846 {
13847 rel[1].r_offset += 4;
13848 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13849 insn2 = NOP;
13850 }
13851 bfd_put_32 (input_bfd, insn2, contents + offset);
13852 }
13853 if ((tls_mask & tls_gd) == 0
13854 && (tls_gd == 0 || toc_symndx != 0))
13855 {
13856 /* We changed the symbol. Start over in order
13857 to get h, sym, sec etc. right. */
13858 goto again;
13859 }
13860 }
13861 break;
13862
13863 case R_PPC64_TLSGD:
13864 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13865 {
13866 unsigned int insn2, insn3;
13867 bfd_vma offset = rel->r_offset;
13868
13869 if ((tls_mask & TLS_TPRELGD) != 0)
13870 {
13871 /* IE */
13872 r_type = R_PPC64_NONE;
13873 insn2 = 0x7c636a14; /* add 3,3,13 */
13874 }
13875 else
13876 {
13877 /* LE */
13878 if (toc_symndx != 0)
13879 {
13880 r_symndx = toc_symndx;
13881 rel->r_addend = toc_addend;
13882 }
13883 r_type = R_PPC64_TPREL16_LO;
13884 rel->r_offset = offset + d_offset;
13885 insn2 = 0x38630000; /* addi 3,3,0 */
13886 }
13887 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13888 /* Zap the reloc on the _tls_get_addr call too. */
13889 BFD_ASSERT (offset == rel[1].r_offset);
13890 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13891 insn3 = bfd_get_32 (input_bfd,
13892 contents + offset + 4);
13893 if (insn3 == NOP
13894 || insn3 == CROR_151515 || insn3 == CROR_313131)
13895 {
13896 rel->r_offset += 4;
13897 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13898 insn2 = NOP;
13899 }
13900 bfd_put_32 (input_bfd, insn2, contents + offset);
13901 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13902 goto again;
13903 }
13904 break;
13905
13906 case R_PPC64_TLSLD:
13907 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13908 {
13909 unsigned int insn2, insn3;
13910 bfd_vma offset = rel->r_offset;
13911
13912 if (toc_symndx)
13913 sec = local_sections[toc_symndx];
13914 for (r_symndx = 0;
13915 r_symndx < symtab_hdr->sh_info;
13916 r_symndx++)
13917 if (local_sections[r_symndx] == sec)
13918 break;
13919 if (r_symndx >= symtab_hdr->sh_info)
13920 r_symndx = STN_UNDEF;
13921 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13922 if (r_symndx != STN_UNDEF)
13923 rel->r_addend -= (local_syms[r_symndx].st_value
13924 + sec->output_offset
13925 + sec->output_section->vma);
13926
13927 r_type = R_PPC64_TPREL16_LO;
13928 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13929 rel->r_offset = offset + d_offset;
13930 /* Zap the reloc on the _tls_get_addr call too. */
13931 BFD_ASSERT (offset == rel[1].r_offset);
13932 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13933 insn2 = 0x38630000; /* addi 3,3,0 */
13934 insn3 = bfd_get_32 (input_bfd,
13935 contents + offset + 4);
13936 if (insn3 == NOP
13937 || insn3 == CROR_151515 || insn3 == CROR_313131)
13938 {
13939 rel->r_offset += 4;
13940 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13941 insn2 = NOP;
13942 }
13943 bfd_put_32 (input_bfd, insn2, contents + offset);
13944 goto again;
13945 }
13946 break;
13947
13948 case R_PPC64_DTPMOD64:
13949 if (rel + 1 < relend
13950 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13951 && rel[1].r_offset == rel->r_offset + 8)
13952 {
13953 if ((tls_mask & TLS_GD) == 0)
13954 {
13955 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13956 if ((tls_mask & TLS_TPRELGD) != 0)
13957 r_type = R_PPC64_TPREL64;
13958 else
13959 {
13960 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13961 r_type = R_PPC64_NONE;
13962 }
13963 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13964 }
13965 }
13966 else
13967 {
13968 if ((tls_mask & TLS_LD) == 0)
13969 {
13970 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13971 r_type = R_PPC64_NONE;
13972 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13973 }
13974 }
13975 break;
13976
13977 case R_PPC64_TPREL64:
13978 if ((tls_mask & TLS_TPREL) == 0)
13979 {
13980 r_type = R_PPC64_NONE;
13981 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13982 }
13983 break;
13984
13985 case R_PPC64_ENTRY:
13986 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13987 if (!bfd_link_pic (info)
13988 && !info->traditional_format
13989 && relocation + 0x80008000 <= 0xffffffff)
13990 {
13991 unsigned int insn1, insn2;
13992
13993 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13994 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13995 if ((insn1 & ~0xfffc) == LD_R2_0R12
13996 && insn2 == ADD_R2_R2_R12)
13997 {
13998 bfd_put_32 (input_bfd,
13999 LIS_R2 + PPC_HA (relocation),
14000 contents + rel->r_offset);
14001 bfd_put_32 (input_bfd,
14002 ADDI_R2_R2 + PPC_LO (relocation),
14003 contents + rel->r_offset + 4);
14004 }
14005 }
14006 else
14007 {
14008 relocation -= (rel->r_offset
14009 + input_section->output_offset
14010 + input_section->output_section->vma);
14011 if (relocation + 0x80008000 <= 0xffffffff)
14012 {
14013 unsigned int insn1, insn2;
14014
14015 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14016 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14017 if ((insn1 & ~0xfffc) == LD_R2_0R12
14018 && insn2 == ADD_R2_R2_R12)
14019 {
14020 bfd_put_32 (input_bfd,
14021 ADDIS_R2_R12 + PPC_HA (relocation),
14022 contents + rel->r_offset);
14023 bfd_put_32 (input_bfd,
14024 ADDI_R2_R2 + PPC_LO (relocation),
14025 contents + rel->r_offset + 4);
14026 }
14027 }
14028 }
14029 break;
14030
14031 case R_PPC64_REL16_HA:
14032 /* If we are generating a non-PIC executable, edit
14033 . 0: addis 2,12,.TOC.-0b@ha
14034 . addi 2,2,.TOC.-0b@l
14035 used by ELFv2 global entry points to set up r2, to
14036 . lis 2,.TOC.@ha
14037 . addi 2,2,.TOC.@l
14038 if .TOC. is in range. */
14039 if (!bfd_link_pic (info)
14040 && !info->traditional_format
14041 && !htab->opd_abi
14042 && rel->r_addend == d_offset
14043 && h != NULL && &h->elf == htab->elf.hgot
14044 && rel + 1 < relend
14045 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14046 && rel[1].r_offset == rel->r_offset + 4
14047 && rel[1].r_addend == rel->r_addend + 4
14048 && relocation + 0x80008000 <= 0xffffffff)
14049 {
14050 unsigned int insn1, insn2;
14051 bfd_vma offset = rel->r_offset - d_offset;
14052 insn1 = bfd_get_32 (input_bfd, contents + offset);
14053 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14054 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14055 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14056 {
14057 r_type = R_PPC64_ADDR16_HA;
14058 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14059 rel->r_addend -= d_offset;
14060 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14061 rel[1].r_addend -= d_offset + 4;
14062 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14063 }
14064 }
14065 break;
14066 }
14067
14068 /* Handle other relocations that tweak non-addend part of insn. */
14069 insn = 0;
14070 max_br_offset = 1 << 25;
14071 addend = rel->r_addend;
14072 reloc_dest = DEST_NORMAL;
14073 switch (r_type)
14074 {
14075 default:
14076 break;
14077
14078 case R_PPC64_TOCSAVE:
14079 if (relocation + addend == (rel->r_offset
14080 + input_section->output_offset
14081 + input_section->output_section->vma)
14082 && tocsave_find (htab, NO_INSERT,
14083 &local_syms, rel, input_bfd))
14084 {
14085 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14086 if (insn == NOP
14087 || insn == CROR_151515 || insn == CROR_313131)
14088 bfd_put_32 (input_bfd,
14089 STD_R2_0R1 + STK_TOC (htab),
14090 contents + rel->r_offset);
14091 }
14092 break;
14093
14094 /* Branch taken prediction relocations. */
14095 case R_PPC64_ADDR14_BRTAKEN:
14096 case R_PPC64_REL14_BRTAKEN:
14097 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14098 /* Fall through. */
14099
14100 /* Branch not taken prediction relocations. */
14101 case R_PPC64_ADDR14_BRNTAKEN:
14102 case R_PPC64_REL14_BRNTAKEN:
14103 insn |= bfd_get_32 (input_bfd,
14104 contents + rel->r_offset) & ~(0x01 << 21);
14105 /* Fall through. */
14106
14107 case R_PPC64_REL14:
14108 max_br_offset = 1 << 15;
14109 /* Fall through. */
14110
14111 case R_PPC64_REL24:
14112 /* Calls to functions with a different TOC, such as calls to
14113 shared objects, need to alter the TOC pointer. This is
14114 done using a linkage stub. A REL24 branching to these
14115 linkage stubs needs to be followed by a nop, as the nop
14116 will be replaced with an instruction to restore the TOC
14117 base pointer. */
14118 fdh = h;
14119 if (h != NULL
14120 && h->oh != NULL
14121 && h->oh->is_func_descriptor)
14122 fdh = ppc_follow_link (h->oh);
14123 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14124 htab);
14125 if (stub_entry != NULL
14126 && (stub_entry->stub_type == ppc_stub_plt_call
14127 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14128 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14129 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14130 {
14131 bfd_boolean can_plt_call = FALSE;
14132
14133 /* All of these stubs will modify r2, so there must be a
14134 branch and link followed by a nop. The nop is
14135 replaced by an insn to restore r2. */
14136 if (rel->r_offset + 8 <= input_section->size)
14137 {
14138 unsigned long br;
14139
14140 br = bfd_get_32 (input_bfd,
14141 contents + rel->r_offset);
14142 if ((br & 1) != 0)
14143 {
14144 unsigned long nop;
14145
14146 nop = bfd_get_32 (input_bfd,
14147 contents + rel->r_offset + 4);
14148 if (nop == NOP
14149 || nop == CROR_151515 || nop == CROR_313131)
14150 {
14151 if (h != NULL
14152 && (h == htab->tls_get_addr_fd
14153 || h == htab->tls_get_addr)
14154 && htab->params->tls_get_addr_opt)
14155 {
14156 /* Special stub used, leave nop alone. */
14157 }
14158 else
14159 bfd_put_32 (input_bfd,
14160 LD_R2_0R1 + STK_TOC (htab),
14161 contents + rel->r_offset + 4);
14162 can_plt_call = TRUE;
14163 }
14164 }
14165 }
14166
14167 if (!can_plt_call && h != NULL)
14168 {
14169 const char *name = h->elf.root.root.string;
14170
14171 if (*name == '.')
14172 ++name;
14173
14174 if (strncmp (name, "__libc_start_main", 17) == 0
14175 && (name[17] == 0 || name[17] == '@'))
14176 {
14177 /* Allow crt1 branch to go via a toc adjusting
14178 stub. Other calls that never return could do
14179 the same, if we could detect such. */
14180 can_plt_call = TRUE;
14181 }
14182 }
14183
14184 if (!can_plt_call)
14185 {
14186 /* g++ as of 20130507 emits self-calls without a
14187 following nop. This is arguably wrong since we
14188 have conflicting information. On the one hand a
14189 global symbol and on the other a local call
14190 sequence, but don't error for this special case.
14191 It isn't possible to cheaply verify we have
14192 exactly such a call. Allow all calls to the same
14193 section. */
14194 asection *code_sec = sec;
14195
14196 if (get_opd_info (sec) != NULL)
14197 {
14198 bfd_vma off = (relocation + addend
14199 - sec->output_section->vma
14200 - sec->output_offset);
14201
14202 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14203 }
14204 if (code_sec == input_section)
14205 can_plt_call = TRUE;
14206 }
14207
14208 if (!can_plt_call)
14209 {
14210 if (stub_entry->stub_type == ppc_stub_plt_call
14211 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14212 info->callbacks->einfo
14213 /* xgettext:c-format */
14214 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14215 "recompile with -fPIC\n"),
14216 input_bfd, input_section, rel->r_offset, sym_name);
14217 else
14218 info->callbacks->einfo
14219 /* xgettext:c-format */
14220 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14221 "(-mcmodel=small toc adjust stub)\n"),
14222 input_bfd, input_section, rel->r_offset, sym_name);
14223
14224 bfd_set_error (bfd_error_bad_value);
14225 ret = FALSE;
14226 }
14227
14228 if (can_plt_call
14229 && (stub_entry->stub_type == ppc_stub_plt_call
14230 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14231 unresolved_reloc = FALSE;
14232 }
14233
14234 if ((stub_entry == NULL
14235 || stub_entry->stub_type == ppc_stub_long_branch
14236 || stub_entry->stub_type == ppc_stub_plt_branch)
14237 && get_opd_info (sec) != NULL)
14238 {
14239 /* The branch destination is the value of the opd entry. */
14240 bfd_vma off = (relocation + addend
14241 - sec->output_section->vma
14242 - sec->output_offset);
14243 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14244 if (dest != (bfd_vma) -1)
14245 {
14246 relocation = dest;
14247 addend = 0;
14248 reloc_dest = DEST_OPD;
14249 }
14250 }
14251
14252 /* If the branch is out of reach we ought to have a long
14253 branch stub. */
14254 from = (rel->r_offset
14255 + input_section->output_offset
14256 + input_section->output_section->vma);
14257
14258 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14259 ? fdh->elf.other
14260 : sym->st_other);
14261
14262 if (stub_entry != NULL
14263 && (stub_entry->stub_type == ppc_stub_long_branch
14264 || stub_entry->stub_type == ppc_stub_plt_branch)
14265 && (r_type == R_PPC64_ADDR14_BRTAKEN
14266 || r_type == R_PPC64_ADDR14_BRNTAKEN
14267 || (relocation + addend - from + max_br_offset
14268 < 2 * max_br_offset)))
14269 /* Don't use the stub if this branch is in range. */
14270 stub_entry = NULL;
14271
14272 if (stub_entry != NULL)
14273 {
14274 /* Munge up the value and addend so that we call the stub
14275 rather than the procedure directly. */
14276 asection *stub_sec = stub_entry->group->stub_sec;
14277
14278 if (stub_entry->stub_type == ppc_stub_save_res)
14279 relocation += (stub_sec->output_offset
14280 + stub_sec->output_section->vma
14281 + stub_sec->size - htab->sfpr->size
14282 - htab->sfpr->output_offset
14283 - htab->sfpr->output_section->vma);
14284 else
14285 relocation = (stub_entry->stub_offset
14286 + stub_sec->output_offset
14287 + stub_sec->output_section->vma);
14288 addend = 0;
14289 reloc_dest = DEST_STUB;
14290
14291 if ((stub_entry->stub_type == ppc_stub_plt_call
14292 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14293 && (ALWAYS_EMIT_R2SAVE
14294 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14295 && rel + 1 < relend
14296 && rel[1].r_offset == rel->r_offset + 4
14297 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14298 relocation += 4;
14299 }
14300
14301 if (insn != 0)
14302 {
14303 if (is_isa_v2)
14304 {
14305 /* Set 'a' bit. This is 0b00010 in BO field for branch
14306 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14307 for branch on CTR insns (BO == 1a00t or 1a01t). */
14308 if ((insn & (0x14 << 21)) == (0x04 << 21))
14309 insn |= 0x02 << 21;
14310 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14311 insn |= 0x08 << 21;
14312 else
14313 break;
14314 }
14315 else
14316 {
14317 /* Invert 'y' bit if not the default. */
14318 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14319 insn ^= 0x01 << 21;
14320 }
14321
14322 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14323 }
14324
14325 /* NOP out calls to undefined weak functions.
14326 We can thus call a weak function without first
14327 checking whether the function is defined. */
14328 else if (h != NULL
14329 && h->elf.root.type == bfd_link_hash_undefweak
14330 && h->elf.dynindx == -1
14331 && r_type == R_PPC64_REL24
14332 && relocation == 0
14333 && addend == 0)
14334 {
14335 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14336 goto copy_reloc;
14337 }
14338 break;
14339 }
14340
14341 /* Set `addend'. */
14342 tls_type = 0;
14343 switch (r_type)
14344 {
14345 default:
14346 info->callbacks->einfo
14347 /* xgettext:c-format */
14348 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14349 input_bfd, (int) r_type, sym_name);
14350
14351 bfd_set_error (bfd_error_bad_value);
14352 ret = FALSE;
14353 goto copy_reloc;
14354
14355 case R_PPC64_NONE:
14356 case R_PPC64_TLS:
14357 case R_PPC64_TLSGD:
14358 case R_PPC64_TLSLD:
14359 case R_PPC64_TOCSAVE:
14360 case R_PPC64_GNU_VTINHERIT:
14361 case R_PPC64_GNU_VTENTRY:
14362 case R_PPC64_ENTRY:
14363 goto copy_reloc;
14364
14365 /* GOT16 relocations. Like an ADDR16 using the symbol's
14366 address in the GOT as relocation value instead of the
14367 symbol's value itself. Also, create a GOT entry for the
14368 symbol and put the symbol value there. */
14369 case R_PPC64_GOT_TLSGD16:
14370 case R_PPC64_GOT_TLSGD16_LO:
14371 case R_PPC64_GOT_TLSGD16_HI:
14372 case R_PPC64_GOT_TLSGD16_HA:
14373 tls_type = TLS_TLS | TLS_GD;
14374 goto dogot;
14375
14376 case R_PPC64_GOT_TLSLD16:
14377 case R_PPC64_GOT_TLSLD16_LO:
14378 case R_PPC64_GOT_TLSLD16_HI:
14379 case R_PPC64_GOT_TLSLD16_HA:
14380 tls_type = TLS_TLS | TLS_LD;
14381 goto dogot;
14382
14383 case R_PPC64_GOT_TPREL16_DS:
14384 case R_PPC64_GOT_TPREL16_LO_DS:
14385 case R_PPC64_GOT_TPREL16_HI:
14386 case R_PPC64_GOT_TPREL16_HA:
14387 tls_type = TLS_TLS | TLS_TPREL;
14388 goto dogot;
14389
14390 case R_PPC64_GOT_DTPREL16_DS:
14391 case R_PPC64_GOT_DTPREL16_LO_DS:
14392 case R_PPC64_GOT_DTPREL16_HI:
14393 case R_PPC64_GOT_DTPREL16_HA:
14394 tls_type = TLS_TLS | TLS_DTPREL;
14395 goto dogot;
14396
14397 case R_PPC64_GOT16:
14398 case R_PPC64_GOT16_LO:
14399 case R_PPC64_GOT16_HI:
14400 case R_PPC64_GOT16_HA:
14401 case R_PPC64_GOT16_DS:
14402 case R_PPC64_GOT16_LO_DS:
14403 dogot:
14404 {
14405 /* Relocation is to the entry for this symbol in the global
14406 offset table. */
14407 asection *got;
14408 bfd_vma *offp;
14409 bfd_vma off;
14410 unsigned long indx = 0;
14411 struct got_entry *ent;
14412
14413 if (tls_type == (TLS_TLS | TLS_LD)
14414 && (h == NULL
14415 || !h->elf.def_dynamic))
14416 ent = ppc64_tlsld_got (input_bfd);
14417 else
14418 {
14419
14420 if (h != NULL)
14421 {
14422 bfd_boolean dyn = htab->elf.dynamic_sections_created;
14423 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
14424 &h->elf)
14425 || (bfd_link_pic (info)
14426 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14427 /* This is actually a static link, or it is a
14428 -Bsymbolic link and the symbol is defined
14429 locally, or the symbol was forced to be local
14430 because of a version file. */
14431 ;
14432 else
14433 {
14434 BFD_ASSERT (h->elf.dynindx != -1);
14435 indx = h->elf.dynindx;
14436 unresolved_reloc = FALSE;
14437 }
14438 ent = h->elf.got.glist;
14439 }
14440 else
14441 {
14442 if (local_got_ents == NULL)
14443 abort ();
14444 ent = local_got_ents[r_symndx];
14445 }
14446
14447 for (; ent != NULL; ent = ent->next)
14448 if (ent->addend == orig_rel.r_addend
14449 && ent->owner == input_bfd
14450 && ent->tls_type == tls_type)
14451 break;
14452 }
14453
14454 if (ent == NULL)
14455 abort ();
14456 if (ent->is_indirect)
14457 ent = ent->got.ent;
14458 offp = &ent->got.offset;
14459 got = ppc64_elf_tdata (ent->owner)->got;
14460 if (got == NULL)
14461 abort ();
14462
14463 /* The offset must always be a multiple of 8. We use the
14464 least significant bit to record whether we have already
14465 processed this entry. */
14466 off = *offp;
14467 if ((off & 1) != 0)
14468 off &= ~1;
14469 else
14470 {
14471 /* Generate relocs for the dynamic linker, except in
14472 the case of TLSLD where we'll use one entry per
14473 module. */
14474 asection *relgot;
14475 bfd_boolean ifunc;
14476
14477 *offp = off | 1;
14478 relgot = NULL;
14479 ifunc = (h != NULL
14480 ? h->elf.type == STT_GNU_IFUNC
14481 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14482 if (ifunc)
14483 relgot = htab->elf.irelplt;
14484 else if ((bfd_link_pic (info) || indx != 0)
14485 && (h == NULL
14486 || (tls_type == (TLS_TLS | TLS_LD)
14487 && !h->elf.def_dynamic)
14488 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14489 || h->elf.root.type != bfd_link_hash_undefweak))
14490 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14491 if (relgot != NULL)
14492 {
14493 outrel.r_offset = (got->output_section->vma
14494 + got->output_offset
14495 + off);
14496 outrel.r_addend = addend;
14497 if (tls_type & (TLS_LD | TLS_GD))
14498 {
14499 outrel.r_addend = 0;
14500 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14501 if (tls_type == (TLS_TLS | TLS_GD))
14502 {
14503 loc = relgot->contents;
14504 loc += (relgot->reloc_count++
14505 * sizeof (Elf64_External_Rela));
14506 bfd_elf64_swap_reloca_out (output_bfd,
14507 &outrel, loc);
14508 outrel.r_offset += 8;
14509 outrel.r_addend = addend;
14510 outrel.r_info
14511 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14512 }
14513 }
14514 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14515 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14516 else if (tls_type == (TLS_TLS | TLS_TPREL))
14517 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14518 else if (indx != 0)
14519 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14520 else
14521 {
14522 if (ifunc)
14523 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14524 else
14525 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14526
14527 /* Write the .got section contents for the sake
14528 of prelink. */
14529 loc = got->contents + off;
14530 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14531 loc);
14532 }
14533
14534 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14535 {
14536 outrel.r_addend += relocation;
14537 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14538 {
14539 if (htab->elf.tls_sec == NULL)
14540 outrel.r_addend = 0;
14541 else
14542 outrel.r_addend -= htab->elf.tls_sec->vma;
14543 }
14544 }
14545 loc = relgot->contents;
14546 loc += (relgot->reloc_count++
14547 * sizeof (Elf64_External_Rela));
14548 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14549 }
14550
14551 /* Init the .got section contents here if we're not
14552 emitting a reloc. */
14553 else
14554 {
14555 relocation += addend;
14556 if (tls_type == (TLS_TLS | TLS_LD))
14557 relocation = 1;
14558 else if (tls_type != 0)
14559 {
14560 if (htab->elf.tls_sec == NULL)
14561 relocation = 0;
14562 else
14563 {
14564 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14565 if (tls_type == (TLS_TLS | TLS_TPREL))
14566 relocation += DTP_OFFSET - TP_OFFSET;
14567 }
14568
14569 if (tls_type == (TLS_TLS | TLS_GD))
14570 {
14571 bfd_put_64 (output_bfd, relocation,
14572 got->contents + off + 8);
14573 relocation = 1;
14574 }
14575 }
14576
14577 bfd_put_64 (output_bfd, relocation,
14578 got->contents + off);
14579 }
14580 }
14581
14582 if (off >= (bfd_vma) -2)
14583 abort ();
14584
14585 relocation = got->output_section->vma + got->output_offset + off;
14586 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14587 }
14588 break;
14589
14590 case R_PPC64_PLT16_HA:
14591 case R_PPC64_PLT16_HI:
14592 case R_PPC64_PLT16_LO:
14593 case R_PPC64_PLT32:
14594 case R_PPC64_PLT64:
14595 /* Relocation is to the entry for this symbol in the
14596 procedure linkage table. */
14597 {
14598 struct plt_entry **plt_list = NULL;
14599 if (h != NULL)
14600 plt_list = &h->elf.plt.plist;
14601 else if (local_got_ents != NULL)
14602 {
14603 struct plt_entry **local_plt = (struct plt_entry **)
14604 (local_got_ents + symtab_hdr->sh_info);
14605 unsigned char *local_got_tls_masks = (unsigned char *)
14606 (local_plt + symtab_hdr->sh_info);
14607 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14608 plt_list = local_plt + r_symndx;
14609 }
14610 if (plt_list)
14611 {
14612 struct plt_entry *ent;
14613
14614 for (ent = *plt_list; ent != NULL; ent = ent->next)
14615 if (ent->plt.offset != (bfd_vma) -1
14616 && ent->addend == orig_rel.r_addend)
14617 {
14618 asection *plt;
14619
14620 plt = htab->elf.splt;
14621 if (!htab->elf.dynamic_sections_created
14622 || h == NULL
14623 || h->elf.dynindx == -1)
14624 plt = htab->elf.iplt;
14625 relocation = (plt->output_section->vma
14626 + plt->output_offset
14627 + ent->plt.offset);
14628 addend = 0;
14629 unresolved_reloc = FALSE;
14630 break;
14631 }
14632 }
14633 }
14634 break;
14635
14636 case R_PPC64_TOC:
14637 /* Relocation value is TOC base. */
14638 relocation = TOCstart;
14639 if (r_symndx == STN_UNDEF)
14640 relocation += htab->sec_info[input_section->id].toc_off;
14641 else if (unresolved_reloc)
14642 ;
14643 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14644 relocation += htab->sec_info[sec->id].toc_off;
14645 else
14646 unresolved_reloc = TRUE;
14647 goto dodyn;
14648
14649 /* TOC16 relocs. We want the offset relative to the TOC base,
14650 which is the address of the start of the TOC plus 0x8000.
14651 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14652 in this order. */
14653 case R_PPC64_TOC16:
14654 case R_PPC64_TOC16_LO:
14655 case R_PPC64_TOC16_HI:
14656 case R_PPC64_TOC16_DS:
14657 case R_PPC64_TOC16_LO_DS:
14658 case R_PPC64_TOC16_HA:
14659 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14660 break;
14661
14662 /* Relocate against the beginning of the section. */
14663 case R_PPC64_SECTOFF:
14664 case R_PPC64_SECTOFF_LO:
14665 case R_PPC64_SECTOFF_HI:
14666 case R_PPC64_SECTOFF_DS:
14667 case R_PPC64_SECTOFF_LO_DS:
14668 case R_PPC64_SECTOFF_HA:
14669 if (sec != NULL)
14670 addend -= sec->output_section->vma;
14671 break;
14672
14673 case R_PPC64_REL16:
14674 case R_PPC64_REL16_LO:
14675 case R_PPC64_REL16_HI:
14676 case R_PPC64_REL16_HA:
14677 case R_PPC64_REL16DX_HA:
14678 break;
14679
14680 case R_PPC64_REL14:
14681 case R_PPC64_REL14_BRNTAKEN:
14682 case R_PPC64_REL14_BRTAKEN:
14683 case R_PPC64_REL24:
14684 break;
14685
14686 case R_PPC64_TPREL16:
14687 case R_PPC64_TPREL16_LO:
14688 case R_PPC64_TPREL16_HI:
14689 case R_PPC64_TPREL16_HA:
14690 case R_PPC64_TPREL16_DS:
14691 case R_PPC64_TPREL16_LO_DS:
14692 case R_PPC64_TPREL16_HIGH:
14693 case R_PPC64_TPREL16_HIGHA:
14694 case R_PPC64_TPREL16_HIGHER:
14695 case R_PPC64_TPREL16_HIGHERA:
14696 case R_PPC64_TPREL16_HIGHEST:
14697 case R_PPC64_TPREL16_HIGHESTA:
14698 if (h != NULL
14699 && h->elf.root.type == bfd_link_hash_undefweak
14700 && h->elf.dynindx == -1)
14701 {
14702 /* Make this relocation against an undefined weak symbol
14703 resolve to zero. This is really just a tweak, since
14704 code using weak externs ought to check that they are
14705 defined before using them. */
14706 bfd_byte *p = contents + rel->r_offset - d_offset;
14707
14708 insn = bfd_get_32 (input_bfd, p);
14709 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14710 if (insn != 0)
14711 bfd_put_32 (input_bfd, insn, p);
14712 break;
14713 }
14714 if (htab->elf.tls_sec != NULL)
14715 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14716 if (bfd_link_pic (info))
14717 /* The TPREL16 relocs shouldn't really be used in shared
14718 libs as they will result in DT_TEXTREL being set, but
14719 support them anyway. */
14720 goto dodyn;
14721 break;
14722
14723 case R_PPC64_DTPREL16:
14724 case R_PPC64_DTPREL16_LO:
14725 case R_PPC64_DTPREL16_HI:
14726 case R_PPC64_DTPREL16_HA:
14727 case R_PPC64_DTPREL16_DS:
14728 case R_PPC64_DTPREL16_LO_DS:
14729 case R_PPC64_DTPREL16_HIGH:
14730 case R_PPC64_DTPREL16_HIGHA:
14731 case R_PPC64_DTPREL16_HIGHER:
14732 case R_PPC64_DTPREL16_HIGHERA:
14733 case R_PPC64_DTPREL16_HIGHEST:
14734 case R_PPC64_DTPREL16_HIGHESTA:
14735 if (htab->elf.tls_sec != NULL)
14736 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14737 break;
14738
14739 case R_PPC64_ADDR64_LOCAL:
14740 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14741 ? h->elf.other
14742 : sym->st_other);
14743 break;
14744
14745 case R_PPC64_DTPMOD64:
14746 relocation = 1;
14747 addend = 0;
14748 goto dodyn;
14749
14750 case R_PPC64_TPREL64:
14751 if (htab->elf.tls_sec != NULL)
14752 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14753 goto dodyn;
14754
14755 case R_PPC64_DTPREL64:
14756 if (htab->elf.tls_sec != NULL)
14757 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14758 /* Fall through. */
14759
14760 /* Relocations that may need to be propagated if this is a
14761 dynamic object. */
14762 case R_PPC64_REL30:
14763 case R_PPC64_REL32:
14764 case R_PPC64_REL64:
14765 case R_PPC64_ADDR14:
14766 case R_PPC64_ADDR14_BRNTAKEN:
14767 case R_PPC64_ADDR14_BRTAKEN:
14768 case R_PPC64_ADDR16:
14769 case R_PPC64_ADDR16_DS:
14770 case R_PPC64_ADDR16_HA:
14771 case R_PPC64_ADDR16_HI:
14772 case R_PPC64_ADDR16_HIGH:
14773 case R_PPC64_ADDR16_HIGHA:
14774 case R_PPC64_ADDR16_HIGHER:
14775 case R_PPC64_ADDR16_HIGHERA:
14776 case R_PPC64_ADDR16_HIGHEST:
14777 case R_PPC64_ADDR16_HIGHESTA:
14778 case R_PPC64_ADDR16_LO:
14779 case R_PPC64_ADDR16_LO_DS:
14780 case R_PPC64_ADDR24:
14781 case R_PPC64_ADDR32:
14782 case R_PPC64_ADDR64:
14783 case R_PPC64_UADDR16:
14784 case R_PPC64_UADDR32:
14785 case R_PPC64_UADDR64:
14786 dodyn:
14787 if ((input_section->flags & SEC_ALLOC) == 0)
14788 break;
14789
14790 if (NO_OPD_RELOCS && is_opd)
14791 break;
14792
14793 if (bfd_link_pic (info)
14794 ? ((h != NULL && pc_dynrelocs (h))
14795 || must_be_dyn_reloc (info, r_type))
14796 : (h != NULL
14797 ? h->dyn_relocs != NULL
14798 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14799 {
14800 bfd_boolean skip, relocate;
14801 asection *sreloc;
14802 bfd_vma out_off;
14803
14804 /* When generating a dynamic object, these relocations
14805 are copied into the output file to be resolved at run
14806 time. */
14807
14808 skip = FALSE;
14809 relocate = FALSE;
14810
14811 out_off = _bfd_elf_section_offset (output_bfd, info,
14812 input_section, rel->r_offset);
14813 if (out_off == (bfd_vma) -1)
14814 skip = TRUE;
14815 else if (out_off == (bfd_vma) -2)
14816 skip = TRUE, relocate = TRUE;
14817 out_off += (input_section->output_section->vma
14818 + input_section->output_offset);
14819 outrel.r_offset = out_off;
14820 outrel.r_addend = rel->r_addend;
14821
14822 /* Optimize unaligned reloc use. */
14823 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14824 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14825 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14826 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14827 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14828 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14829 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14830 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14831 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14832
14833 if (skip)
14834 memset (&outrel, 0, sizeof outrel);
14835 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14836 && !is_opd
14837 && r_type != R_PPC64_TOC)
14838 {
14839 BFD_ASSERT (h->elf.dynindx != -1);
14840 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14841 }
14842 else
14843 {
14844 /* This symbol is local, or marked to become local,
14845 or this is an opd section reloc which must point
14846 at a local function. */
14847 outrel.r_addend += relocation;
14848 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14849 {
14850 if (is_opd && h != NULL)
14851 {
14852 /* Lie about opd entries. This case occurs
14853 when building shared libraries and we
14854 reference a function in another shared
14855 lib. The same thing happens for a weak
14856 definition in an application that's
14857 overridden by a strong definition in a
14858 shared lib. (I believe this is a generic
14859 bug in binutils handling of weak syms.)
14860 In these cases we won't use the opd
14861 entry in this lib. */
14862 unresolved_reloc = FALSE;
14863 }
14864 if (!is_opd
14865 && r_type == R_PPC64_ADDR64
14866 && (h != NULL
14867 ? h->elf.type == STT_GNU_IFUNC
14868 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14869 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14870 else
14871 {
14872 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14873
14874 /* We need to relocate .opd contents for ld.so.
14875 Prelink also wants simple and consistent rules
14876 for relocs. This make all RELATIVE relocs have
14877 *r_offset equal to r_addend. */
14878 relocate = TRUE;
14879 }
14880 }
14881 else
14882 {
14883 long indx = 0;
14884
14885 if (h != NULL
14886 ? h->elf.type == STT_GNU_IFUNC
14887 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14888 {
14889 info->callbacks->einfo
14890 /* xgettext:c-format */
14891 (_("%P: %H: %s for indirect "
14892 "function `%T' unsupported\n"),
14893 input_bfd, input_section, rel->r_offset,
14894 ppc64_elf_howto_table[r_type]->name,
14895 sym_name);
14896 ret = FALSE;
14897 }
14898 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14899 ;
14900 else if (sec == NULL || sec->owner == NULL)
14901 {
14902 bfd_set_error (bfd_error_bad_value);
14903 return FALSE;
14904 }
14905 else
14906 {
14907 asection *osec;
14908
14909 osec = sec->output_section;
14910 indx = elf_section_data (osec)->dynindx;
14911
14912 if (indx == 0)
14913 {
14914 if ((osec->flags & SEC_READONLY) == 0
14915 && htab->elf.data_index_section != NULL)
14916 osec = htab->elf.data_index_section;
14917 else
14918 osec = htab->elf.text_index_section;
14919 indx = elf_section_data (osec)->dynindx;
14920 }
14921 BFD_ASSERT (indx != 0);
14922
14923 /* We are turning this relocation into one
14924 against a section symbol, so subtract out
14925 the output section's address but not the
14926 offset of the input section in the output
14927 section. */
14928 outrel.r_addend -= osec->vma;
14929 }
14930
14931 outrel.r_info = ELF64_R_INFO (indx, r_type);
14932 }
14933 }
14934
14935 sreloc = elf_section_data (input_section)->sreloc;
14936 if (h != NULL
14937 ? h->elf.type == STT_GNU_IFUNC
14938 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14939 sreloc = htab->elf.irelplt;
14940 if (sreloc == NULL)
14941 abort ();
14942
14943 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14944 >= sreloc->size)
14945 abort ();
14946 loc = sreloc->contents;
14947 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14948 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14949
14950 /* If this reloc is against an external symbol, it will
14951 be computed at runtime, so there's no need to do
14952 anything now. However, for the sake of prelink ensure
14953 that the section contents are a known value. */
14954 if (! relocate)
14955 {
14956 unresolved_reloc = FALSE;
14957 /* The value chosen here is quite arbitrary as ld.so
14958 ignores section contents except for the special
14959 case of .opd where the contents might be accessed
14960 before relocation. Choose zero, as that won't
14961 cause reloc overflow. */
14962 relocation = 0;
14963 addend = 0;
14964 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14965 to improve backward compatibility with older
14966 versions of ld. */
14967 if (r_type == R_PPC64_ADDR64)
14968 addend = outrel.r_addend;
14969 /* Adjust pc_relative relocs to have zero in *r_offset. */
14970 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14971 addend = (input_section->output_section->vma
14972 + input_section->output_offset
14973 + rel->r_offset);
14974 }
14975 }
14976 break;
14977
14978 case R_PPC64_COPY:
14979 case R_PPC64_GLOB_DAT:
14980 case R_PPC64_JMP_SLOT:
14981 case R_PPC64_JMP_IREL:
14982 case R_PPC64_RELATIVE:
14983 /* We shouldn't ever see these dynamic relocs in relocatable
14984 files. */
14985 /* Fall through. */
14986
14987 case R_PPC64_PLTGOT16:
14988 case R_PPC64_PLTGOT16_DS:
14989 case R_PPC64_PLTGOT16_HA:
14990 case R_PPC64_PLTGOT16_HI:
14991 case R_PPC64_PLTGOT16_LO:
14992 case R_PPC64_PLTGOT16_LO_DS:
14993 case R_PPC64_PLTREL32:
14994 case R_PPC64_PLTREL64:
14995 /* These ones haven't been implemented yet. */
14996
14997 info->callbacks->einfo
14998 /* xgettext:c-format */
14999 (_("%P: %B: %s is not supported for `%T'\n"),
15000 input_bfd,
15001 ppc64_elf_howto_table[r_type]->name, sym_name);
15002
15003 bfd_set_error (bfd_error_invalid_operation);
15004 ret = FALSE;
15005 goto copy_reloc;
15006 }
15007
15008 /* Multi-instruction sequences that access the TOC can be
15009 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15010 to nop; addi rb,r2,x; */
15011 switch (r_type)
15012 {
15013 default:
15014 break;
15015
15016 case R_PPC64_GOT_TLSLD16_HI:
15017 case R_PPC64_GOT_TLSGD16_HI:
15018 case R_PPC64_GOT_TPREL16_HI:
15019 case R_PPC64_GOT_DTPREL16_HI:
15020 case R_PPC64_GOT16_HI:
15021 case R_PPC64_TOC16_HI:
15022 /* These relocs would only be useful if building up an
15023 offset to later add to r2, perhaps in an indexed
15024 addressing mode instruction. Don't try to optimize.
15025 Unfortunately, the possibility of someone building up an
15026 offset like this or even with the HA relocs, means that
15027 we need to check the high insn when optimizing the low
15028 insn. */
15029 break;
15030
15031 case R_PPC64_GOT_TLSLD16_HA:
15032 case R_PPC64_GOT_TLSGD16_HA:
15033 case R_PPC64_GOT_TPREL16_HA:
15034 case R_PPC64_GOT_DTPREL16_HA:
15035 case R_PPC64_GOT16_HA:
15036 case R_PPC64_TOC16_HA:
15037 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15038 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15039 {
15040 bfd_byte *p = contents + (rel->r_offset & ~3);
15041 bfd_put_32 (input_bfd, NOP, p);
15042 }
15043 break;
15044
15045 case R_PPC64_GOT_TLSLD16_LO:
15046 case R_PPC64_GOT_TLSGD16_LO:
15047 case R_PPC64_GOT_TPREL16_LO_DS:
15048 case R_PPC64_GOT_DTPREL16_LO_DS:
15049 case R_PPC64_GOT16_LO:
15050 case R_PPC64_GOT16_LO_DS:
15051 case R_PPC64_TOC16_LO:
15052 case R_PPC64_TOC16_LO_DS:
15053 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15054 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15055 {
15056 bfd_byte *p = contents + (rel->r_offset & ~3);
15057 insn = bfd_get_32 (input_bfd, p);
15058 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15059 {
15060 /* Transform addic to addi when we change reg. */
15061 insn &= ~((0x3f << 26) | (0x1f << 16));
15062 insn |= (14u << 26) | (2 << 16);
15063 }
15064 else
15065 {
15066 insn &= ~(0x1f << 16);
15067 insn |= 2 << 16;
15068 }
15069 bfd_put_32 (input_bfd, insn, p);
15070 }
15071 break;
15072 }
15073
15074 /* Do any further special processing. */
15075 howto = ppc64_elf_howto_table[(int) r_type];
15076 switch (r_type)
15077 {
15078 default:
15079 break;
15080
15081 case R_PPC64_REL16_HA:
15082 case R_PPC64_REL16DX_HA:
15083 case R_PPC64_ADDR16_HA:
15084 case R_PPC64_ADDR16_HIGHA:
15085 case R_PPC64_ADDR16_HIGHERA:
15086 case R_PPC64_ADDR16_HIGHESTA:
15087 case R_PPC64_TOC16_HA:
15088 case R_PPC64_SECTOFF_HA:
15089 case R_PPC64_TPREL16_HA:
15090 case R_PPC64_TPREL16_HIGHA:
15091 case R_PPC64_TPREL16_HIGHERA:
15092 case R_PPC64_TPREL16_HIGHESTA:
15093 case R_PPC64_DTPREL16_HA:
15094 case R_PPC64_DTPREL16_HIGHA:
15095 case R_PPC64_DTPREL16_HIGHERA:
15096 case R_PPC64_DTPREL16_HIGHESTA:
15097 /* It's just possible that this symbol is a weak symbol
15098 that's not actually defined anywhere. In that case,
15099 'sec' would be NULL, and we should leave the symbol
15100 alone (it will be set to zero elsewhere in the link). */
15101 if (sec == NULL)
15102 break;
15103 /* Fall through. */
15104
15105 case R_PPC64_GOT16_HA:
15106 case R_PPC64_PLTGOT16_HA:
15107 case R_PPC64_PLT16_HA:
15108 case R_PPC64_GOT_TLSGD16_HA:
15109 case R_PPC64_GOT_TLSLD16_HA:
15110 case R_PPC64_GOT_TPREL16_HA:
15111 case R_PPC64_GOT_DTPREL16_HA:
15112 /* Add 0x10000 if sign bit in 0:15 is set.
15113 Bits 0:15 are not used. */
15114 addend += 0x8000;
15115 break;
15116
15117 case R_PPC64_ADDR16_DS:
15118 case R_PPC64_ADDR16_LO_DS:
15119 case R_PPC64_GOT16_DS:
15120 case R_PPC64_GOT16_LO_DS:
15121 case R_PPC64_PLT16_LO_DS:
15122 case R_PPC64_SECTOFF_DS:
15123 case R_PPC64_SECTOFF_LO_DS:
15124 case R_PPC64_TOC16_DS:
15125 case R_PPC64_TOC16_LO_DS:
15126 case R_PPC64_PLTGOT16_DS:
15127 case R_PPC64_PLTGOT16_LO_DS:
15128 case R_PPC64_GOT_TPREL16_DS:
15129 case R_PPC64_GOT_TPREL16_LO_DS:
15130 case R_PPC64_GOT_DTPREL16_DS:
15131 case R_PPC64_GOT_DTPREL16_LO_DS:
15132 case R_PPC64_TPREL16_DS:
15133 case R_PPC64_TPREL16_LO_DS:
15134 case R_PPC64_DTPREL16_DS:
15135 case R_PPC64_DTPREL16_LO_DS:
15136 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15137 mask = 3;
15138 /* If this reloc is against an lq, lxv, or stxv insn, then
15139 the value must be a multiple of 16. This is somewhat of
15140 a hack, but the "correct" way to do this by defining _DQ
15141 forms of all the _DS relocs bloats all reloc switches in
15142 this file. It doesn't make much sense to use these
15143 relocs in data, so testing the insn should be safe. */
15144 if ((insn & (0x3f << 26)) == (56u << 26)
15145 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15146 mask = 15;
15147 relocation += addend;
15148 addend = insn & (mask ^ 3);
15149 if ((relocation & mask) != 0)
15150 {
15151 relocation ^= relocation & mask;
15152 info->callbacks->einfo
15153 /* xgettext:c-format */
15154 (_("%P: %H: error: %s not a multiple of %u\n"),
15155 input_bfd, input_section, rel->r_offset,
15156 howto->name,
15157 mask + 1);
15158 bfd_set_error (bfd_error_bad_value);
15159 ret = FALSE;
15160 goto copy_reloc;
15161 }
15162 break;
15163 }
15164
15165 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15166 because such sections are not SEC_ALLOC and thus ld.so will
15167 not process them. */
15168 if (unresolved_reloc
15169 && !((input_section->flags & SEC_DEBUGGING) != 0
15170 && h->elf.def_dynamic)
15171 && _bfd_elf_section_offset (output_bfd, info, input_section,
15172 rel->r_offset) != (bfd_vma) -1)
15173 {
15174 info->callbacks->einfo
15175 /* xgettext:c-format */
15176 (_("%P: %H: unresolvable %s against `%T'\n"),
15177 input_bfd, input_section, rel->r_offset,
15178 howto->name,
15179 h->elf.root.root.string);
15180 ret = FALSE;
15181 }
15182
15183 /* 16-bit fields in insns mostly have signed values, but a
15184 few insns have 16-bit unsigned values. Really, we should
15185 have different reloc types. */
15186 if (howto->complain_on_overflow != complain_overflow_dont
15187 && howto->dst_mask == 0xffff
15188 && (input_section->flags & SEC_CODE) != 0)
15189 {
15190 enum complain_overflow complain = complain_overflow_signed;
15191
15192 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15193 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15194 complain = complain_overflow_bitfield;
15195 else if (howto->rightshift == 0
15196 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15197 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15198 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15199 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15200 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15201 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15202 complain = complain_overflow_unsigned;
15203 if (howto->complain_on_overflow != complain)
15204 {
15205 alt_howto = *howto;
15206 alt_howto.complain_on_overflow = complain;
15207 howto = &alt_howto;
15208 }
15209 }
15210
15211 if (r_type == R_PPC64_REL16DX_HA)
15212 {
15213 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15214 if (rel->r_offset + 4 > input_section->size)
15215 r = bfd_reloc_outofrange;
15216 else
15217 {
15218 relocation += addend;
15219 relocation -= (rel->r_offset
15220 + input_section->output_offset
15221 + input_section->output_section->vma);
15222 relocation = (bfd_signed_vma) relocation >> 16;
15223 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15224 insn &= ~0x1fffc1;
15225 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15226 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15227 r = bfd_reloc_ok;
15228 if (relocation + 0x8000 > 0xffff)
15229 r = bfd_reloc_overflow;
15230 }
15231 }
15232 else
15233 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15234 rel->r_offset, relocation, addend);
15235
15236 if (r != bfd_reloc_ok)
15237 {
15238 char *more_info = NULL;
15239 const char *reloc_name = howto->name;
15240
15241 if (reloc_dest != DEST_NORMAL)
15242 {
15243 more_info = bfd_malloc (strlen (reloc_name) + 8);
15244 if (more_info != NULL)
15245 {
15246 strcpy (more_info, reloc_name);
15247 strcat (more_info, (reloc_dest == DEST_OPD
15248 ? " (OPD)" : " (stub)"));
15249 reloc_name = more_info;
15250 }
15251 }
15252
15253 if (r == bfd_reloc_overflow)
15254 {
15255 /* On code like "if (foo) foo();" don't report overflow
15256 on a branch to zero when foo is undefined. */
15257 if (!warned
15258 && (reloc_dest == DEST_STUB
15259 || !(h != NULL
15260 && (h->elf.root.type == bfd_link_hash_undefweak
15261 || h->elf.root.type == bfd_link_hash_undefined)
15262 && is_branch_reloc (r_type))))
15263 info->callbacks->reloc_overflow (info, &h->elf.root,
15264 sym_name, reloc_name,
15265 orig_rel.r_addend,
15266 input_bfd, input_section,
15267 rel->r_offset);
15268 }
15269 else
15270 {
15271 info->callbacks->einfo
15272 /* xgettext:c-format */
15273 (_("%P: %H: %s against `%T': error %d\n"),
15274 input_bfd, input_section, rel->r_offset,
15275 reloc_name, sym_name, (int) r);
15276 ret = FALSE;
15277 }
15278 if (more_info != NULL)
15279 free (more_info);
15280 }
15281 copy_reloc:
15282 if (wrel != rel)
15283 *wrel = *rel;
15284 }
15285
15286 if (wrel != rel)
15287 {
15288 Elf_Internal_Shdr *rel_hdr;
15289 size_t deleted = rel - wrel;
15290
15291 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15292 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15293 if (rel_hdr->sh_size == 0)
15294 {
15295 /* It is too late to remove an empty reloc section. Leave
15296 one NONE reloc.
15297 ??? What is wrong with an empty section??? */
15298 rel_hdr->sh_size = rel_hdr->sh_entsize;
15299 deleted -= 1;
15300 }
15301 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15302 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15303 input_section->reloc_count -= deleted;
15304 }
15305
15306 /* If we're emitting relocations, then shortly after this function
15307 returns, reloc offsets and addends for this section will be
15308 adjusted. Worse, reloc symbol indices will be for the output
15309 file rather than the input. Save a copy of the relocs for
15310 opd_entry_value. */
15311 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15312 {
15313 bfd_size_type amt;
15314 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15315 rel = bfd_alloc (input_bfd, amt);
15316 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15317 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15318 if (rel == NULL)
15319 return FALSE;
15320 memcpy (rel, relocs, amt);
15321 }
15322 return ret;
15323}
15324
15325/* Adjust the value of any local symbols in opd sections. */
15326
15327static int
15328ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15329 const char *name ATTRIBUTE_UNUSED,
15330 Elf_Internal_Sym *elfsym,
15331 asection *input_sec,
15332 struct elf_link_hash_entry *h)
15333{
15334 struct _opd_sec_data *opd;
15335 long adjust;
15336 bfd_vma value;
15337
15338 if (h != NULL)
15339 return 1;
15340
15341 opd = get_opd_info (input_sec);
15342 if (opd == NULL || opd->adjust == NULL)
15343 return 1;
15344
15345 value = elfsym->st_value - input_sec->output_offset;
15346 if (!bfd_link_relocatable (info))
15347 value -= input_sec->output_section->vma;
15348
15349 adjust = opd->adjust[OPD_NDX (value)];
15350 if (adjust == -1)
15351 return 2;
15352
15353 elfsym->st_value += adjust;
15354 return 1;
15355}
15356
15357/* Finish up dynamic symbol handling. We set the contents of various
15358 dynamic sections here. */
15359
15360static bfd_boolean
15361ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15362 struct bfd_link_info *info,
15363 struct elf_link_hash_entry *h,
15364 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
15365{
15366 struct ppc_link_hash_table *htab;
15367 struct plt_entry *ent;
15368 Elf_Internal_Rela rela;
15369 bfd_byte *loc;
15370
15371 htab = ppc_hash_table (info);
15372 if (htab == NULL)
15373 return FALSE;
15374
15375 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15376 if (ent->plt.offset != (bfd_vma) -1)
15377 {
15378 /* This symbol has an entry in the procedure linkage
15379 table. Set it up. */
15380 if (!htab->elf.dynamic_sections_created
15381 || h->dynindx == -1)
15382 {
15383 BFD_ASSERT (h->type == STT_GNU_IFUNC
15384 && h->def_regular
15385 && (h->root.type == bfd_link_hash_defined
15386 || h->root.type == bfd_link_hash_defweak));
15387 rela.r_offset = (htab->elf.iplt->output_section->vma
15388 + htab->elf.iplt->output_offset
15389 + ent->plt.offset);
15390 if (htab->opd_abi)
15391 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15392 else
15393 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15394 rela.r_addend = (h->root.u.def.value
15395 + h->root.u.def.section->output_offset
15396 + h->root.u.def.section->output_section->vma
15397 + ent->addend);
15398 loc = (htab->elf.irelplt->contents
15399 + (htab->elf.irelplt->reloc_count++
15400 * sizeof (Elf64_External_Rela)));
15401 }
15402 else
15403 {
15404 rela.r_offset = (htab->elf.splt->output_section->vma
15405 + htab->elf.splt->output_offset
15406 + ent->plt.offset);
15407 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15408 rela.r_addend = ent->addend;
15409 loc = (htab->elf.srelplt->contents
15410 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15411 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15412 }
15413 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15414
15415 if (!htab->opd_abi)
15416 {
15417 if (!h->def_regular)
15418 {
15419 /* Mark the symbol as undefined, rather than as
15420 defined in glink. Leave the value if there were
15421 any relocations where pointer equality matters
15422 (this is a clue for the dynamic linker, to make
15423 function pointer comparisons work between an
15424 application and shared library), otherwise set it
15425 to zero. */
15426 sym->st_shndx = SHN_UNDEF;
15427 if (!h->pointer_equality_needed)
15428 sym->st_value = 0;
15429 else if (!h->ref_regular_nonweak)
15430 {
15431 /* This breaks function pointer comparisons, but
15432 that is better than breaking tests for a NULL
15433 function pointer. */
15434 sym->st_value = 0;
15435 }
15436 }
15437 }
15438 }
15439
15440 if (h->needs_copy)
15441 {
15442 /* This symbol needs a copy reloc. Set it up. */
15443
15444 if (h->dynindx == -1
15445 || (h->root.type != bfd_link_hash_defined
15446 && h->root.type != bfd_link_hash_defweak)
15447 || htab->relbss == NULL)
15448 abort ();
15449
15450 rela.r_offset = (h->root.u.def.value
15451 + h->root.u.def.section->output_section->vma
15452 + h->root.u.def.section->output_offset);
15453 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15454 rela.r_addend = 0;
15455 loc = htab->relbss->contents;
15456 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
15457 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15458 }
15459
15460 return TRUE;
15461}
15462
15463/* Used to decide how to sort relocs in an optimal manner for the
15464 dynamic linker, before writing them out. */
15465
15466static enum elf_reloc_type_class
15467ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15468 const asection *rel_sec,
15469 const Elf_Internal_Rela *rela)
15470{
15471 enum elf_ppc64_reloc_type r_type;
15472 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15473
15474 if (rel_sec == htab->elf.irelplt)
15475 return reloc_class_ifunc;
15476
15477 r_type = ELF64_R_TYPE (rela->r_info);
15478 switch (r_type)
15479 {
15480 case R_PPC64_RELATIVE:
15481 return reloc_class_relative;
15482 case R_PPC64_JMP_SLOT:
15483 return reloc_class_plt;
15484 case R_PPC64_COPY:
15485 return reloc_class_copy;
15486 default:
15487 return reloc_class_normal;
15488 }
15489}
15490
15491/* Finish up the dynamic sections. */
15492
15493static bfd_boolean
15494ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15495 struct bfd_link_info *info)
15496{
15497 struct ppc_link_hash_table *htab;
15498 bfd *dynobj;
15499 asection *sdyn;
15500
15501 htab = ppc_hash_table (info);
15502 if (htab == NULL)
15503 return FALSE;
15504
15505 dynobj = htab->elf.dynobj;
15506 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15507
15508 if (htab->elf.dynamic_sections_created)
15509 {
15510 Elf64_External_Dyn *dyncon, *dynconend;
15511
15512 if (sdyn == NULL || htab->elf.sgot == NULL)
15513 abort ();
15514
15515 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15516 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15517 for (; dyncon < dynconend; dyncon++)
15518 {
15519 Elf_Internal_Dyn dyn;
15520 asection *s;
15521
15522 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15523
15524 switch (dyn.d_tag)
15525 {
15526 default:
15527 continue;
15528
15529 case DT_PPC64_GLINK:
15530 s = htab->glink;
15531 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15532 /* We stupidly defined DT_PPC64_GLINK to be the start
15533 of glink rather than the first entry point, which is
15534 what ld.so needs, and now have a bigger stub to
15535 support automatic multiple TOCs. */
15536 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15537 break;
15538
15539 case DT_PPC64_OPD:
15540 s = bfd_get_section_by_name (output_bfd, ".opd");
15541 if (s == NULL)
15542 continue;
15543 dyn.d_un.d_ptr = s->vma;
15544 break;
15545
15546 case DT_PPC64_OPT:
15547 if (htab->do_multi_toc && htab->multi_toc_needed)
15548 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15549 break;
15550
15551 case DT_PPC64_OPDSZ:
15552 s = bfd_get_section_by_name (output_bfd, ".opd");
15553 if (s == NULL)
15554 continue;
15555 dyn.d_un.d_val = s->size;
15556 break;
15557
15558 case DT_PLTGOT:
15559 s = htab->elf.splt;
15560 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15561 break;
15562
15563 case DT_JMPREL:
15564 s = htab->elf.srelplt;
15565 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15566 break;
15567
15568 case DT_PLTRELSZ:
15569 dyn.d_un.d_val = htab->elf.srelplt->size;
15570 break;
15571 }
15572
15573 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15574 }
15575 }
15576
15577 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15578 {
15579 /* Fill in the first entry in the global offset table.
15580 We use it to hold the link-time TOCbase. */
15581 bfd_put_64 (output_bfd,
15582 elf_gp (output_bfd) + TOC_BASE_OFF,
15583 htab->elf.sgot->contents);
15584
15585 /* Set .got entry size. */
15586 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15587 }
15588
15589 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15590 {
15591 /* Set .plt entry size. */
15592 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15593 = PLT_ENTRY_SIZE (htab);
15594 }
15595
15596 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15597 brlt ourselves if emitrelocations. */
15598 if (htab->brlt != NULL
15599 && htab->brlt->reloc_count != 0
15600 && !_bfd_elf_link_output_relocs (output_bfd,
15601 htab->brlt,
15602 elf_section_data (htab->brlt)->rela.hdr,
15603 elf_section_data (htab->brlt)->relocs,
15604 NULL))
15605 return FALSE;
15606
15607 if (htab->glink != NULL
15608 && htab->glink->reloc_count != 0
15609 && !_bfd_elf_link_output_relocs (output_bfd,
15610 htab->glink,
15611 elf_section_data (htab->glink)->rela.hdr,
15612 elf_section_data (htab->glink)->relocs,
15613 NULL))
15614 return FALSE;
15615
15616 if (htab->glink_eh_frame != NULL
15617 && htab->glink_eh_frame->size != 0)
15618 {
15619 bfd_vma val;
15620 bfd_byte *p;
15621 asection *stub_sec;
15622
15623 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15624 for (stub_sec = htab->params->stub_bfd->sections;
15625 stub_sec != NULL;
15626 stub_sec = stub_sec->next)
15627 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15628 {
15629 /* FDE length. */
15630 p += 4;
15631 /* CIE pointer. */
15632 p += 4;
15633 /* Offset to stub section. */
15634 val = (stub_sec->output_section->vma
15635 + stub_sec->output_offset);
15636 val -= (htab->glink_eh_frame->output_section->vma
15637 + htab->glink_eh_frame->output_offset
15638 + (p - htab->glink_eh_frame->contents));
15639 if (val + 0x80000000 > 0xffffffff)
15640 {
15641 info->callbacks->einfo
15642 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15643 stub_sec->name);
15644 return FALSE;
15645 }
15646 bfd_put_32 (dynobj, val, p);
15647 p += 4;
15648 /* stub section size. */
15649 p += 4;
15650 /* Augmentation. */
15651 p += 1;
15652 /* Pad. */
15653 p += 7;
15654 }
15655 if (htab->glink != NULL && htab->glink->size != 0)
15656 {
15657 /* FDE length. */
15658 p += 4;
15659 /* CIE pointer. */
15660 p += 4;
15661 /* Offset to .glink. */
15662 val = (htab->glink->output_section->vma
15663 + htab->glink->output_offset
15664 + 8);
15665 val -= (htab->glink_eh_frame->output_section->vma
15666 + htab->glink_eh_frame->output_offset
15667 + (p - htab->glink_eh_frame->contents));
15668 if (val + 0x80000000 > 0xffffffff)
15669 {
15670 info->callbacks->einfo
15671 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15672 htab->glink->name);
15673 return FALSE;
15674 }
15675 bfd_put_32 (dynobj, val, p);
15676 p += 4;
15677 /* .glink size. */
15678 p += 4;
15679 /* Augmentation. */
15680 p += 1;
15681 /* Ops. */
15682 p += 7;
15683 }
15684
15685 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15686 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15687 htab->glink_eh_frame,
15688 htab->glink_eh_frame->contents))
15689 return FALSE;
15690 }
15691
15692 /* We need to handle writing out multiple GOT sections ourselves,
15693 since we didn't add them to DYNOBJ. We know dynobj is the first
15694 bfd. */
15695 while ((dynobj = dynobj->link.next) != NULL)
15696 {
15697 asection *s;
15698
15699 if (!is_ppc64_elf (dynobj))
15700 continue;
15701
15702 s = ppc64_elf_tdata (dynobj)->got;
15703 if (s != NULL
15704 && s->size != 0
15705 && s->output_section != bfd_abs_section_ptr
15706 && !bfd_set_section_contents (output_bfd, s->output_section,
15707 s->contents, s->output_offset,
15708 s->size))
15709 return FALSE;
15710 s = ppc64_elf_tdata (dynobj)->relgot;
15711 if (s != NULL
15712 && s->size != 0
15713 && s->output_section != bfd_abs_section_ptr
15714 && !bfd_set_section_contents (output_bfd, s->output_section,
15715 s->contents, s->output_offset,
15716 s->size))
15717 return FALSE;
15718 }
15719
15720 return TRUE;
15721}
15722
15723#include "elf64-target.h"
15724
15725/* FreeBSD support */
15726
15727#undef TARGET_LITTLE_SYM
15728#undef TARGET_LITTLE_NAME
15729
15730#undef TARGET_BIG_SYM
15731#define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15732#undef TARGET_BIG_NAME
15733#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15734
15735#undef ELF_OSABI
15736#define ELF_OSABI ELFOSABI_FREEBSD
15737
15738#undef elf64_bed
15739#define elf64_bed elf64_powerpc_fbsd_bed
15740
15741#include "elf64-target.h"
15742
This page took 0.077882 seconds and 4 git commands to generate.