Indirect and warning symbols
[binutils-gdb.git] / bfd / elf64-ppc.c
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
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, 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
250 static inline int
251 abiversion (bfd *abfd)
252 {
253 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
254 }
255
256 static inline void
257 set_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. */
266 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
267
268 static 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
2200 static void
2201 ppc_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
2213 static reloc_howto_type *
2214 ppc64_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
2465 static reloc_howto_type *
2466 ppc64_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
2481 static void
2482 ppc64_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
2504 static bfd_reloc_status_type
2505 ppc64_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
2550 static bfd_reloc_status_type
2551 ppc64_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
2597 static bfd_reloc_status_type
2598 ppc64_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
2660 static bfd_reloc_status_type
2661 ppc64_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
2677 static bfd_reloc_status_type
2678 ppc64_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
2697 static bfd_reloc_status_type
2698 ppc64_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
2720 static bfd_reloc_status_type
2721 ppc64_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
2746 static bfd_reloc_status_type
2747 ppc64_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
2770 static bfd_reloc_status_type
2771 ppc64_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. */
2794 struct 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. */
2827 struct 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
2840 struct 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
2885 static bfd_boolean
2886 ppc64_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
2895 static bfd_boolean
2896 ppc64_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
2917 static bfd_boolean
2918 ppc64_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
2940 static bfd_boolean
2941 ppc64_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
2956 static char *
2957 ppc64_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
3005 static 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
3016 enum _ppc64_sec_type {
3017 sec_normal = 0,
3018 sec_opd = 1,
3019 sec_toc = 2
3020 };
3021
3022 struct _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
3061 static bfd_boolean
3062 ppc64_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
3078 static struct _opd_sec_data *
3079 get_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. */
3089 static bfd_boolean synthetic_relocatable;
3090 static asection *synthetic_opd;
3091
3092 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3093
3094 static int
3095 compare_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
3176 static asymbol *
3177 sym_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
3214 static bfd_boolean
3215 section_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
3227 static long
3228 ppc64_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
3771 static int
3772 must_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
3870 enum 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. */
3883 struct 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
3896 struct 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
3922 struct 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. */
3935 struct 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
3949 struct 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
4009 struct 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
4136 static struct bfd_hash_entry *
4137 stub_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
4173 static struct bfd_hash_entry *
4174 branch_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
4204 static struct bfd_hash_entry *
4205 link_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
4257 struct tocsave_entry {
4258 asection *sec;
4259 bfd_vma offset;
4260 };
4261
4262 static hashval_t
4263 tocsave_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
4269 static int
4270 tocsave_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
4279 static void
4280 ppc64_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
4294 static struct bfd_link_hash_table *
4295 ppc64_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
4358 static bfd_boolean
4359 create_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
4440 bfd_boolean
4441 ppc64_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
4460 static char *
4461 ppc_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
4507 static struct ppc_stub_hash_entry *
4508 ppc_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
4554 static struct ppc_stub_hash_entry *
4555 ppc_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
4607 static bfd_boolean
4608 create_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
4644 static bfd_boolean
4645 ppc64_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
4669 static inline struct bfd_link_hash_entry *
4670 follow_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
4678 static inline struct elf_link_hash_entry *
4679 elf_follow_link (struct elf_link_hash_entry *h)
4680 {
4681 return (struct elf_link_hash_entry *) follow_link (&h->root);
4682 }
4683
4684 static inline struct ppc_link_hash_entry *
4685 ppc_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
4692 static void
4693 move_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
4727 static void
4728 ppc64_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 (edir->elf.versioned != versioned_hidden)
4744 {
4745 /* If called to transfer flags for a weakdef during processing
4746 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4747 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4748 if (!(ELIMINATE_COPY_RELOCS
4749 && eind->elf.root.type != bfd_link_hash_indirect
4750 && edir->elf.dynamic_adjusted))
4751 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4752
4753 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4754 edir->elf.ref_regular |= eind->elf.ref_regular;
4755 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4756 edir->elf.needs_plt |= eind->elf.needs_plt;
4757 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4758 }
4759
4760 /* If we were called to copy over info for a weak sym, don't copy
4761 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4762 in order to simplify readonly_dynrelocs and save a field in the
4763 symbol hash entry, but that means dyn_relocs can't be used in any
4764 tests about a specific symbol, or affect other symbol flags which
4765 are then tested.
4766 Chain weakdefs so we can get from the weakdef back to an alias.
4767 The list is circular so that we don't need to use u.weakdef as
4768 well as this list to look at all aliases. */
4769 if (eind->elf.root.type != bfd_link_hash_indirect)
4770 {
4771 struct ppc_link_hash_entry *cur, *add, *next;
4772
4773 add = eind;
4774 do
4775 {
4776 cur = edir->weakref;
4777 if (cur != NULL)
4778 {
4779 do
4780 {
4781 /* We can be called twice for the same symbols.
4782 Don't make multiple loops. */
4783 if (cur == add)
4784 return;
4785 cur = cur->weakref;
4786 } while (cur != edir);
4787 }
4788 next = add->weakref;
4789 if (cur != add)
4790 {
4791 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4792 edir->weakref = add;
4793 }
4794 add = next;
4795 } while (add != NULL && add != eind);
4796 return;
4797 }
4798
4799 /* Copy over any dynamic relocs we may have on the indirect sym. */
4800 if (eind->dyn_relocs != NULL)
4801 {
4802 if (edir->dyn_relocs != NULL)
4803 {
4804 struct elf_dyn_relocs **pp;
4805 struct elf_dyn_relocs *p;
4806
4807 /* Add reloc counts against the indirect sym to the direct sym
4808 list. Merge any entries against the same section. */
4809 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4810 {
4811 struct elf_dyn_relocs *q;
4812
4813 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4814 if (q->sec == p->sec)
4815 {
4816 q->pc_count += p->pc_count;
4817 q->count += p->count;
4818 *pp = p->next;
4819 break;
4820 }
4821 if (q == NULL)
4822 pp = &p->next;
4823 }
4824 *pp = edir->dyn_relocs;
4825 }
4826
4827 edir->dyn_relocs = eind->dyn_relocs;
4828 eind->dyn_relocs = NULL;
4829 }
4830
4831 /* Copy over got entries that we may have already seen to the
4832 symbol which just became indirect. */
4833 if (eind->elf.got.glist != NULL)
4834 {
4835 if (edir->elf.got.glist != NULL)
4836 {
4837 struct got_entry **entp;
4838 struct got_entry *ent;
4839
4840 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4841 {
4842 struct got_entry *dent;
4843
4844 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4845 if (dent->addend == ent->addend
4846 && dent->owner == ent->owner
4847 && dent->tls_type == ent->tls_type)
4848 {
4849 dent->got.refcount += ent->got.refcount;
4850 *entp = ent->next;
4851 break;
4852 }
4853 if (dent == NULL)
4854 entp = &ent->next;
4855 }
4856 *entp = edir->elf.got.glist;
4857 }
4858
4859 edir->elf.got.glist = eind->elf.got.glist;
4860 eind->elf.got.glist = NULL;
4861 }
4862
4863 /* And plt entries. */
4864 move_plt_plist (eind, edir);
4865
4866 if (eind->elf.dynindx != -1)
4867 {
4868 if (edir->elf.dynindx != -1)
4869 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4870 edir->elf.dynstr_index);
4871 edir->elf.dynindx = eind->elf.dynindx;
4872 edir->elf.dynstr_index = eind->elf.dynstr_index;
4873 eind->elf.dynindx = -1;
4874 eind->elf.dynstr_index = 0;
4875 }
4876 }
4877
4878 /* Find the function descriptor hash entry from the given function code
4879 hash entry FH. Link the entries via their OH fields. */
4880
4881 static struct ppc_link_hash_entry *
4882 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4883 {
4884 struct ppc_link_hash_entry *fdh = fh->oh;
4885
4886 if (fdh == NULL)
4887 {
4888 const char *fd_name = fh->elf.root.root.string + 1;
4889
4890 fdh = (struct ppc_link_hash_entry *)
4891 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4892 if (fdh == NULL)
4893 return fdh;
4894
4895 fdh->is_func_descriptor = 1;
4896 fdh->oh = fh;
4897 fh->is_func = 1;
4898 fh->oh = fdh;
4899 }
4900
4901 return ppc_follow_link (fdh);
4902 }
4903
4904 /* Make a fake function descriptor sym for the code sym FH. */
4905
4906 static struct ppc_link_hash_entry *
4907 make_fdh (struct bfd_link_info *info,
4908 struct ppc_link_hash_entry *fh)
4909 {
4910 bfd *abfd;
4911 asymbol *newsym;
4912 struct bfd_link_hash_entry *bh;
4913 struct ppc_link_hash_entry *fdh;
4914
4915 abfd = fh->elf.root.u.undef.abfd;
4916 newsym = bfd_make_empty_symbol (abfd);
4917 newsym->name = fh->elf.root.root.string + 1;
4918 newsym->section = bfd_und_section_ptr;
4919 newsym->value = 0;
4920 newsym->flags = BSF_WEAK;
4921
4922 bh = NULL;
4923 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4924 newsym->flags, newsym->section,
4925 newsym->value, NULL, FALSE, FALSE,
4926 &bh))
4927 return NULL;
4928
4929 fdh = (struct ppc_link_hash_entry *) bh;
4930 fdh->elf.non_elf = 0;
4931 fdh->fake = 1;
4932 fdh->is_func_descriptor = 1;
4933 fdh->oh = fh;
4934 fh->is_func = 1;
4935 fh->oh = fdh;
4936 return fdh;
4937 }
4938
4939 /* Fix function descriptor symbols defined in .opd sections to be
4940 function type. */
4941
4942 static bfd_boolean
4943 ppc64_elf_add_symbol_hook (bfd *ibfd,
4944 struct bfd_link_info *info,
4945 Elf_Internal_Sym *isym,
4946 const char **name,
4947 flagword *flags ATTRIBUTE_UNUSED,
4948 asection **sec,
4949 bfd_vma *value)
4950 {
4951 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4952 && (ibfd->flags & DYNAMIC) == 0
4953 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4954 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4955
4956 if (*sec != NULL
4957 && strcmp ((*sec)->name, ".opd") == 0)
4958 {
4959 asection *code_sec;
4960
4961 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4962 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4963 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4964
4965 /* If the symbol is a function defined in .opd, and the function
4966 code is in a discarded group, let it appear to be undefined. */
4967 if (!bfd_link_relocatable (info)
4968 && (*sec)->reloc_count != 0
4969 && opd_entry_value (*sec, *value, &code_sec, NULL,
4970 FALSE) != (bfd_vma) -1
4971 && discarded_section (code_sec))
4972 {
4973 *sec = bfd_und_section_ptr;
4974 isym->st_shndx = SHN_UNDEF;
4975 }
4976 }
4977 else if (*sec != NULL
4978 && strcmp ((*sec)->name, ".toc") == 0
4979 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4980 {
4981 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4982 if (htab != NULL)
4983 htab->params->object_in_toc = 1;
4984 }
4985
4986 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4987 {
4988 if (abiversion (ibfd) == 0)
4989 set_abiversion (ibfd, 2);
4990 else if (abiversion (ibfd) == 1)
4991 {
4992 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4993 " for ABI version 1\n"), name);
4994 bfd_set_error (bfd_error_bad_value);
4995 return FALSE;
4996 }
4997 }
4998
4999 return TRUE;
5000 }
5001
5002 /* Merge non-visibility st_other attributes: local entry point. */
5003
5004 static void
5005 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5006 const Elf_Internal_Sym *isym,
5007 bfd_boolean definition,
5008 bfd_boolean dynamic)
5009 {
5010 if (definition && !dynamic)
5011 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5012 | ELF_ST_VISIBILITY (h->other));
5013 }
5014
5015 /* This function makes an old ABI object reference to ".bar" cause the
5016 inclusion of a new ABI object archive that defines "bar".
5017 NAME is a symbol defined in an archive. Return a symbol in the hash
5018 table that might be satisfied by the archive symbols. */
5019
5020 static struct elf_link_hash_entry *
5021 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5022 struct bfd_link_info *info,
5023 const char *name)
5024 {
5025 struct elf_link_hash_entry *h;
5026 char *dot_name;
5027 size_t len;
5028
5029 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5030 if (h != NULL
5031 /* Don't return this sym if it is a fake function descriptor
5032 created by add_symbol_adjust. */
5033 && !(h->root.type == bfd_link_hash_undefweak
5034 && ((struct ppc_link_hash_entry *) h)->fake))
5035 return h;
5036
5037 if (name[0] == '.')
5038 return h;
5039
5040 len = strlen (name);
5041 dot_name = bfd_alloc (abfd, len + 2);
5042 if (dot_name == NULL)
5043 return (struct elf_link_hash_entry *) 0 - 1;
5044 dot_name[0] = '.';
5045 memcpy (dot_name + 1, name, len + 1);
5046 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5047 bfd_release (abfd, dot_name);
5048 return h;
5049 }
5050
5051 /* This function satisfies all old ABI object references to ".bar" if a
5052 new ABI object defines "bar". Well, at least, undefined dot symbols
5053 are made weak. This stops later archive searches from including an
5054 object if we already have a function descriptor definition. It also
5055 prevents the linker complaining about undefined symbols.
5056 We also check and correct mismatched symbol visibility here. The
5057 most restrictive visibility of the function descriptor and the
5058 function entry symbol is used. */
5059
5060 static bfd_boolean
5061 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5062 {
5063 struct ppc_link_hash_table *htab;
5064 struct ppc_link_hash_entry *fdh;
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.type == bfd_link_hash_indirect)
5070 return TRUE;
5071
5072 if (eh->elf.root.root.string[0] != '.')
5073 abort ();
5074
5075 htab = ppc_hash_table (info);
5076 if (htab == NULL)
5077 return FALSE;
5078
5079 fdh = lookup_fdh (eh, htab);
5080 if (fdh == NULL)
5081 {
5082 if (!bfd_link_relocatable (info)
5083 && (eh->elf.root.type == bfd_link_hash_undefined
5084 || eh->elf.root.type == bfd_link_hash_undefweak)
5085 && eh->elf.ref_regular)
5086 {
5087 /* Make an undefweak function descriptor sym, which is enough to
5088 pull in an --as-needed shared lib, but won't cause link
5089 errors. Archives are handled elsewhere. */
5090 fdh = make_fdh (info, eh);
5091 if (fdh == NULL)
5092 return FALSE;
5093 fdh->elf.ref_regular = 1;
5094 }
5095 }
5096 else
5097 {
5098 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5099 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5100 if (entry_vis < descr_vis)
5101 fdh->elf.other += entry_vis - descr_vis;
5102 else if (entry_vis > descr_vis)
5103 eh->elf.other += descr_vis - entry_vis;
5104
5105 if ((fdh->elf.root.type == bfd_link_hash_defined
5106 || fdh->elf.root.type == bfd_link_hash_defweak)
5107 && eh->elf.root.type == bfd_link_hash_undefined)
5108 {
5109 eh->elf.root.type = bfd_link_hash_undefweak;
5110 eh->was_undefined = 1;
5111 htab->twiddled_syms = 1;
5112 }
5113 }
5114
5115 return TRUE;
5116 }
5117
5118 /* Set up opd section info and abiversion for IBFD, and process list
5119 of dot-symbols we made in link_hash_newfunc. */
5120
5121 static bfd_boolean
5122 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5123 {
5124 struct ppc_link_hash_table *htab;
5125 struct ppc_link_hash_entry **p, *eh;
5126 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5127
5128 if (opd != NULL && opd->size != 0)
5129 {
5130 if (abiversion (ibfd) == 0)
5131 set_abiversion (ibfd, 1);
5132 else if (abiversion (ibfd) >= 2)
5133 {
5134 /* xgettext:c-format */
5135 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5136 " version %d\n"),
5137 ibfd, abiversion (ibfd));
5138 bfd_set_error (bfd_error_bad_value);
5139 return FALSE;
5140 }
5141
5142 if ((ibfd->flags & DYNAMIC) == 0
5143 && (opd->flags & SEC_RELOC) != 0
5144 && opd->reloc_count != 0
5145 && !bfd_is_abs_section (opd->output_section))
5146 {
5147 /* Garbage collection needs some extra help with .opd sections.
5148 We don't want to necessarily keep everything referenced by
5149 relocs in .opd, as that would keep all functions. Instead,
5150 if we reference an .opd symbol (a function descriptor), we
5151 want to keep the function code symbol's section. This is
5152 easy for global symbols, but for local syms we need to keep
5153 information about the associated function section. */
5154 bfd_size_type amt;
5155 asection **opd_sym_map;
5156
5157 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5158 opd_sym_map = bfd_zalloc (ibfd, amt);
5159 if (opd_sym_map == NULL)
5160 return FALSE;
5161 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5162 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5163 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5164 }
5165 }
5166
5167 if (!is_ppc64_elf (info->output_bfd))
5168 return TRUE;
5169 htab = ppc_hash_table (info);
5170 if (htab == NULL)
5171 return FALSE;
5172
5173 /* For input files without an explicit abiversion in e_flags
5174 we should have flagged any with symbol st_other bits set
5175 as ELFv1 and above flagged those with .opd as ELFv2.
5176 Set the output abiversion if not yet set, and for any input
5177 still ambiguous, take its abiversion from the output.
5178 Differences in ABI are reported later. */
5179 if (abiversion (info->output_bfd) == 0)
5180 set_abiversion (info->output_bfd, abiversion (ibfd));
5181 else if (abiversion (ibfd) == 0)
5182 set_abiversion (ibfd, abiversion (info->output_bfd));
5183
5184 p = &htab->dot_syms;
5185 while ((eh = *p) != NULL)
5186 {
5187 *p = NULL;
5188 if (&eh->elf == htab->elf.hgot)
5189 ;
5190 else if (htab->elf.hgot == NULL
5191 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5192 htab->elf.hgot = &eh->elf;
5193 else if (!add_symbol_adjust (eh, info))
5194 return FALSE;
5195 p = &eh->u.next_dot_sym;
5196 }
5197
5198 /* Clear the list for non-ppc64 input files. */
5199 p = &htab->dot_syms;
5200 while ((eh = *p) != NULL)
5201 {
5202 *p = NULL;
5203 p = &eh->u.next_dot_sym;
5204 }
5205
5206 /* We need to fix the undefs list for any syms we have twiddled to
5207 undefweak. */
5208 if (htab->twiddled_syms)
5209 {
5210 bfd_link_repair_undef_list (&htab->elf.root);
5211 htab->twiddled_syms = 0;
5212 }
5213 return TRUE;
5214 }
5215
5216 /* Undo hash table changes when an --as-needed input file is determined
5217 not to be needed. */
5218
5219 static bfd_boolean
5220 ppc64_elf_notice_as_needed (bfd *ibfd,
5221 struct bfd_link_info *info,
5222 enum notice_asneeded_action act)
5223 {
5224 if (act == notice_not_needed)
5225 {
5226 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5227
5228 if (htab == NULL)
5229 return FALSE;
5230
5231 htab->dot_syms = NULL;
5232 }
5233 return _bfd_elf_notice_as_needed (ibfd, info, act);
5234 }
5235
5236 /* If --just-symbols against a final linked binary, then assume we need
5237 toc adjusting stubs when calling functions defined there. */
5238
5239 static void
5240 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5241 {
5242 if ((sec->flags & SEC_CODE) != 0
5243 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5244 && is_ppc64_elf (sec->owner))
5245 {
5246 if (abiversion (sec->owner) >= 2
5247 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5248 sec->has_toc_reloc = 1;
5249 }
5250 _bfd_elf_link_just_syms (sec, info);
5251 }
5252
5253 static struct plt_entry **
5254 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5255 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5256 {
5257 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5258 struct plt_entry **local_plt;
5259 unsigned char *local_got_tls_masks;
5260
5261 if (local_got_ents == NULL)
5262 {
5263 bfd_size_type size = symtab_hdr->sh_info;
5264
5265 size *= (sizeof (*local_got_ents)
5266 + sizeof (*local_plt)
5267 + sizeof (*local_got_tls_masks));
5268 local_got_ents = bfd_zalloc (abfd, size);
5269 if (local_got_ents == NULL)
5270 return NULL;
5271 elf_local_got_ents (abfd) = local_got_ents;
5272 }
5273
5274 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5275 {
5276 struct got_entry *ent;
5277
5278 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5279 if (ent->addend == r_addend
5280 && ent->owner == abfd
5281 && ent->tls_type == tls_type)
5282 break;
5283 if (ent == NULL)
5284 {
5285 bfd_size_type amt = sizeof (*ent);
5286 ent = bfd_alloc (abfd, amt);
5287 if (ent == NULL)
5288 return FALSE;
5289 ent->next = local_got_ents[r_symndx];
5290 ent->addend = r_addend;
5291 ent->owner = abfd;
5292 ent->tls_type = tls_type;
5293 ent->is_indirect = FALSE;
5294 ent->got.refcount = 0;
5295 local_got_ents[r_symndx] = ent;
5296 }
5297 ent->got.refcount += 1;
5298 }
5299
5300 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5301 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5302 local_got_tls_masks[r_symndx] |= tls_type;
5303
5304 return local_plt + r_symndx;
5305 }
5306
5307 static bfd_boolean
5308 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5309 {
5310 struct plt_entry *ent;
5311
5312 for (ent = *plist; ent != NULL; ent = ent->next)
5313 if (ent->addend == addend)
5314 break;
5315 if (ent == NULL)
5316 {
5317 bfd_size_type amt = sizeof (*ent);
5318 ent = bfd_alloc (abfd, amt);
5319 if (ent == NULL)
5320 return FALSE;
5321 ent->next = *plist;
5322 ent->addend = addend;
5323 ent->plt.refcount = 0;
5324 *plist = ent;
5325 }
5326 ent->plt.refcount += 1;
5327 return TRUE;
5328 }
5329
5330 static bfd_boolean
5331 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5332 {
5333 return (r_type == R_PPC64_REL24
5334 || r_type == R_PPC64_REL14
5335 || r_type == R_PPC64_REL14_BRTAKEN
5336 || r_type == R_PPC64_REL14_BRNTAKEN
5337 || r_type == R_PPC64_ADDR24
5338 || r_type == R_PPC64_ADDR14
5339 || r_type == R_PPC64_ADDR14_BRTAKEN
5340 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5341 }
5342
5343 /* Look through the relocs for a section during the first phase, and
5344 calculate needed space in the global offset table, procedure
5345 linkage table, and dynamic reloc sections. */
5346
5347 static bfd_boolean
5348 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5349 asection *sec, const Elf_Internal_Rela *relocs)
5350 {
5351 struct ppc_link_hash_table *htab;
5352 Elf_Internal_Shdr *symtab_hdr;
5353 struct elf_link_hash_entry **sym_hashes;
5354 const Elf_Internal_Rela *rel;
5355 const Elf_Internal_Rela *rel_end;
5356 asection *sreloc;
5357 asection **opd_sym_map;
5358 struct elf_link_hash_entry *tga, *dottga;
5359
5360 if (bfd_link_relocatable (info))
5361 return TRUE;
5362
5363 /* Don't do anything special with non-loaded, non-alloced sections.
5364 In particular, any relocs in such sections should not affect GOT
5365 and PLT reference counting (ie. we don't allow them to create GOT
5366 or PLT entries), there's no possibility or desire to optimize TLS
5367 relocs, and there's not much point in propagating relocs to shared
5368 libs that the dynamic linker won't relocate. */
5369 if ((sec->flags & SEC_ALLOC) == 0)
5370 return TRUE;
5371
5372 BFD_ASSERT (is_ppc64_elf (abfd));
5373
5374 htab = ppc_hash_table (info);
5375 if (htab == NULL)
5376 return FALSE;
5377
5378 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5379 FALSE, FALSE, TRUE);
5380 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5381 FALSE, FALSE, TRUE);
5382 symtab_hdr = &elf_symtab_hdr (abfd);
5383 sym_hashes = elf_sym_hashes (abfd);
5384 sreloc = NULL;
5385 opd_sym_map = NULL;
5386 if (ppc64_elf_section_data (sec) != NULL
5387 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5388 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5389
5390 rel_end = relocs + sec->reloc_count;
5391 for (rel = relocs; rel < rel_end; rel++)
5392 {
5393 unsigned long r_symndx;
5394 struct elf_link_hash_entry *h;
5395 enum elf_ppc64_reloc_type r_type;
5396 int tls_type;
5397 struct _ppc64_elf_section_data *ppc64_sec;
5398 struct plt_entry **ifunc, **plt_list;
5399
5400 r_symndx = ELF64_R_SYM (rel->r_info);
5401 if (r_symndx < symtab_hdr->sh_info)
5402 h = NULL;
5403 else
5404 {
5405 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5406 h = elf_follow_link (h);
5407
5408 /* PR15323, ref flags aren't set for references in the same
5409 object. */
5410 h->root.non_ir_ref = 1;
5411
5412 if (h == htab->elf.hgot)
5413 sec->has_toc_reloc = 1;
5414 }
5415
5416 tls_type = 0;
5417 ifunc = NULL;
5418 if (h != NULL)
5419 {
5420 if (h->type == STT_GNU_IFUNC)
5421 {
5422 h->needs_plt = 1;
5423 ifunc = &h->plt.plist;
5424 }
5425 }
5426 else
5427 {
5428 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5429 abfd, r_symndx);
5430 if (isym == NULL)
5431 return FALSE;
5432
5433 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5434 {
5435 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5436 rel->r_addend, PLT_IFUNC);
5437 if (ifunc == NULL)
5438 return FALSE;
5439 }
5440 }
5441
5442 r_type = ELF64_R_TYPE (rel->r_info);
5443 switch (r_type)
5444 {
5445 case R_PPC64_TLSGD:
5446 case R_PPC64_TLSLD:
5447 /* These special tls relocs tie a call to __tls_get_addr with
5448 its parameter symbol. */
5449 break;
5450
5451 case R_PPC64_GOT_TLSLD16:
5452 case R_PPC64_GOT_TLSLD16_LO:
5453 case R_PPC64_GOT_TLSLD16_HI:
5454 case R_PPC64_GOT_TLSLD16_HA:
5455 tls_type = TLS_TLS | TLS_LD;
5456 goto dogottls;
5457
5458 case R_PPC64_GOT_TLSGD16:
5459 case R_PPC64_GOT_TLSGD16_LO:
5460 case R_PPC64_GOT_TLSGD16_HI:
5461 case R_PPC64_GOT_TLSGD16_HA:
5462 tls_type = TLS_TLS | TLS_GD;
5463 goto dogottls;
5464
5465 case R_PPC64_GOT_TPREL16_DS:
5466 case R_PPC64_GOT_TPREL16_LO_DS:
5467 case R_PPC64_GOT_TPREL16_HI:
5468 case R_PPC64_GOT_TPREL16_HA:
5469 if (bfd_link_pic (info))
5470 info->flags |= DF_STATIC_TLS;
5471 tls_type = TLS_TLS | TLS_TPREL;
5472 goto dogottls;
5473
5474 case R_PPC64_GOT_DTPREL16_DS:
5475 case R_PPC64_GOT_DTPREL16_LO_DS:
5476 case R_PPC64_GOT_DTPREL16_HI:
5477 case R_PPC64_GOT_DTPREL16_HA:
5478 tls_type = TLS_TLS | TLS_DTPREL;
5479 dogottls:
5480 sec->has_tls_reloc = 1;
5481 /* Fall through */
5482
5483 case R_PPC64_GOT16:
5484 case R_PPC64_GOT16_DS:
5485 case R_PPC64_GOT16_HA:
5486 case R_PPC64_GOT16_HI:
5487 case R_PPC64_GOT16_LO:
5488 case R_PPC64_GOT16_LO_DS:
5489 /* This symbol requires a global offset table entry. */
5490 sec->has_toc_reloc = 1;
5491 if (r_type == R_PPC64_GOT_TLSLD16
5492 || r_type == R_PPC64_GOT_TLSGD16
5493 || r_type == R_PPC64_GOT_TPREL16_DS
5494 || r_type == R_PPC64_GOT_DTPREL16_DS
5495 || r_type == R_PPC64_GOT16
5496 || r_type == R_PPC64_GOT16_DS)
5497 {
5498 htab->do_multi_toc = 1;
5499 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5500 }
5501
5502 if (ppc64_elf_tdata (abfd)->got == NULL
5503 && !create_got_section (abfd, info))
5504 return FALSE;
5505
5506 if (h != NULL)
5507 {
5508 struct ppc_link_hash_entry *eh;
5509 struct got_entry *ent;
5510
5511 eh = (struct ppc_link_hash_entry *) h;
5512 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5513 if (ent->addend == rel->r_addend
5514 && ent->owner == abfd
5515 && ent->tls_type == tls_type)
5516 break;
5517 if (ent == NULL)
5518 {
5519 bfd_size_type amt = sizeof (*ent);
5520 ent = bfd_alloc (abfd, amt);
5521 if (ent == NULL)
5522 return FALSE;
5523 ent->next = eh->elf.got.glist;
5524 ent->addend = rel->r_addend;
5525 ent->owner = abfd;
5526 ent->tls_type = tls_type;
5527 ent->is_indirect = FALSE;
5528 ent->got.refcount = 0;
5529 eh->elf.got.glist = ent;
5530 }
5531 ent->got.refcount += 1;
5532 eh->tls_mask |= tls_type;
5533 }
5534 else
5535 /* This is a global offset table entry for a local symbol. */
5536 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5537 rel->r_addend, tls_type))
5538 return FALSE;
5539
5540 /* We may also need a plt entry if the symbol turns out to be
5541 an ifunc. */
5542 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5543 {
5544 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5545 return FALSE;
5546 }
5547 break;
5548
5549 case R_PPC64_PLT16_HA:
5550 case R_PPC64_PLT16_HI:
5551 case R_PPC64_PLT16_LO:
5552 case R_PPC64_PLT32:
5553 case R_PPC64_PLT64:
5554 /* This symbol requires a procedure linkage table entry. */
5555 plt_list = ifunc;
5556 if (h != NULL)
5557 {
5558 h->needs_plt = 1;
5559 if (h->root.root.string[0] == '.'
5560 && h->root.root.string[1] != '\0')
5561 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5562 plt_list = &h->plt.plist;
5563 }
5564 if (plt_list == NULL)
5565 {
5566 /* It does not make sense to have a procedure linkage
5567 table entry for a non-ifunc local symbol. */
5568 info->callbacks->einfo
5569 /* xgettext:c-format */
5570 (_("%P: %H: %s reloc against local symbol\n"),
5571 abfd, sec, rel->r_offset,
5572 ppc64_elf_howto_table[r_type]->name);
5573 bfd_set_error (bfd_error_bad_value);
5574 return FALSE;
5575 }
5576 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5577 return FALSE;
5578 break;
5579
5580 /* The following relocations don't need to propagate the
5581 relocation if linking a shared object since they are
5582 section relative. */
5583 case R_PPC64_SECTOFF:
5584 case R_PPC64_SECTOFF_LO:
5585 case R_PPC64_SECTOFF_HI:
5586 case R_PPC64_SECTOFF_HA:
5587 case R_PPC64_SECTOFF_DS:
5588 case R_PPC64_SECTOFF_LO_DS:
5589 case R_PPC64_DTPREL16:
5590 case R_PPC64_DTPREL16_LO:
5591 case R_PPC64_DTPREL16_HI:
5592 case R_PPC64_DTPREL16_HA:
5593 case R_PPC64_DTPREL16_DS:
5594 case R_PPC64_DTPREL16_LO_DS:
5595 case R_PPC64_DTPREL16_HIGH:
5596 case R_PPC64_DTPREL16_HIGHA:
5597 case R_PPC64_DTPREL16_HIGHER:
5598 case R_PPC64_DTPREL16_HIGHERA:
5599 case R_PPC64_DTPREL16_HIGHEST:
5600 case R_PPC64_DTPREL16_HIGHESTA:
5601 break;
5602
5603 /* Nor do these. */
5604 case R_PPC64_REL16:
5605 case R_PPC64_REL16_LO:
5606 case R_PPC64_REL16_HI:
5607 case R_PPC64_REL16_HA:
5608 case R_PPC64_REL16DX_HA:
5609 break;
5610
5611 /* Not supported as a dynamic relocation. */
5612 case R_PPC64_ADDR64_LOCAL:
5613 if (bfd_link_pic (info))
5614 {
5615 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5616 ppc_howto_init ();
5617 /* xgettext:c-format */
5618 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5619 "in shared libraries and PIEs.\n"),
5620 abfd, sec, rel->r_offset,
5621 ppc64_elf_howto_table[r_type]->name);
5622 bfd_set_error (bfd_error_bad_value);
5623 return FALSE;
5624 }
5625 break;
5626
5627 case R_PPC64_TOC16:
5628 case R_PPC64_TOC16_DS:
5629 htab->do_multi_toc = 1;
5630 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5631 /* Fall through. */
5632 case R_PPC64_TOC16_LO:
5633 case R_PPC64_TOC16_HI:
5634 case R_PPC64_TOC16_HA:
5635 case R_PPC64_TOC16_LO_DS:
5636 sec->has_toc_reloc = 1;
5637 break;
5638
5639 /* Marker reloc. */
5640 case R_PPC64_ENTRY:
5641 break;
5642
5643 /* This relocation describes the C++ object vtable hierarchy.
5644 Reconstruct it for later use during GC. */
5645 case R_PPC64_GNU_VTINHERIT:
5646 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5647 return FALSE;
5648 break;
5649
5650 /* This relocation describes which C++ vtable entries are actually
5651 used. Record for later use during GC. */
5652 case R_PPC64_GNU_VTENTRY:
5653 BFD_ASSERT (h != NULL);
5654 if (h != NULL
5655 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5656 return FALSE;
5657 break;
5658
5659 case R_PPC64_REL14:
5660 case R_PPC64_REL14_BRTAKEN:
5661 case R_PPC64_REL14_BRNTAKEN:
5662 {
5663 asection *dest = NULL;
5664
5665 /* Heuristic: If jumping outside our section, chances are
5666 we are going to need a stub. */
5667 if (h != NULL)
5668 {
5669 /* If the sym is weak it may be overridden later, so
5670 don't assume we know where a weak sym lives. */
5671 if (h->root.type == bfd_link_hash_defined)
5672 dest = h->root.u.def.section;
5673 }
5674 else
5675 {
5676 Elf_Internal_Sym *isym;
5677
5678 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5679 abfd, r_symndx);
5680 if (isym == NULL)
5681 return FALSE;
5682
5683 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5684 }
5685
5686 if (dest != sec)
5687 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5688 }
5689 /* Fall through. */
5690
5691 case R_PPC64_REL24:
5692 plt_list = ifunc;
5693 if (h != NULL)
5694 {
5695 h->needs_plt = 1;
5696 if (h->root.root.string[0] == '.'
5697 && h->root.root.string[1] != '\0')
5698 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5699
5700 if (h == tga || h == dottga)
5701 {
5702 sec->has_tls_reloc = 1;
5703 if (rel != relocs
5704 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5705 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5706 /* We have a new-style __tls_get_addr call with
5707 a marker reloc. */
5708 ;
5709 else
5710 /* Mark this section as having an old-style call. */
5711 sec->has_tls_get_addr_call = 1;
5712 }
5713 plt_list = &h->plt.plist;
5714 }
5715
5716 /* We may need a .plt entry if the function this reloc
5717 refers to is in a shared lib. */
5718 if (plt_list
5719 && !update_plt_info (abfd, plt_list, rel->r_addend))
5720 return FALSE;
5721 break;
5722
5723 case R_PPC64_ADDR14:
5724 case R_PPC64_ADDR14_BRNTAKEN:
5725 case R_PPC64_ADDR14_BRTAKEN:
5726 case R_PPC64_ADDR24:
5727 goto dodyn;
5728
5729 case R_PPC64_TPREL64:
5730 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5731 if (bfd_link_pic (info))
5732 info->flags |= DF_STATIC_TLS;
5733 goto dotlstoc;
5734
5735 case R_PPC64_DTPMOD64:
5736 if (rel + 1 < rel_end
5737 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5738 && rel[1].r_offset == rel->r_offset + 8)
5739 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5740 else
5741 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5742 goto dotlstoc;
5743
5744 case R_PPC64_DTPREL64:
5745 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5746 if (rel != relocs
5747 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5748 && rel[-1].r_offset == rel->r_offset - 8)
5749 /* This is the second reloc of a dtpmod, dtprel pair.
5750 Don't mark with TLS_DTPREL. */
5751 goto dodyn;
5752
5753 dotlstoc:
5754 sec->has_tls_reloc = 1;
5755 if (h != NULL)
5756 {
5757 struct ppc_link_hash_entry *eh;
5758 eh = (struct ppc_link_hash_entry *) h;
5759 eh->tls_mask |= tls_type;
5760 }
5761 else
5762 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5763 rel->r_addend, tls_type))
5764 return FALSE;
5765
5766 ppc64_sec = ppc64_elf_section_data (sec);
5767 if (ppc64_sec->sec_type != sec_toc)
5768 {
5769 bfd_size_type amt;
5770
5771 /* One extra to simplify get_tls_mask. */
5772 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5773 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5774 if (ppc64_sec->u.toc.symndx == NULL)
5775 return FALSE;
5776 amt = sec->size * sizeof (bfd_vma) / 8;
5777 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5778 if (ppc64_sec->u.toc.add == NULL)
5779 return FALSE;
5780 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5781 ppc64_sec->sec_type = sec_toc;
5782 }
5783 BFD_ASSERT (rel->r_offset % 8 == 0);
5784 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5785 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5786
5787 /* Mark the second slot of a GD or LD entry.
5788 -1 to indicate GD and -2 to indicate LD. */
5789 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5790 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5791 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5792 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5793 goto dodyn;
5794
5795 case R_PPC64_TPREL16:
5796 case R_PPC64_TPREL16_LO:
5797 case R_PPC64_TPREL16_HI:
5798 case R_PPC64_TPREL16_HA:
5799 case R_PPC64_TPREL16_DS:
5800 case R_PPC64_TPREL16_LO_DS:
5801 case R_PPC64_TPREL16_HIGH:
5802 case R_PPC64_TPREL16_HIGHA:
5803 case R_PPC64_TPREL16_HIGHER:
5804 case R_PPC64_TPREL16_HIGHERA:
5805 case R_PPC64_TPREL16_HIGHEST:
5806 case R_PPC64_TPREL16_HIGHESTA:
5807 if (bfd_link_pic (info))
5808 {
5809 info->flags |= DF_STATIC_TLS;
5810 goto dodyn;
5811 }
5812 break;
5813
5814 case R_PPC64_ADDR64:
5815 if (opd_sym_map != NULL
5816 && rel + 1 < rel_end
5817 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5818 {
5819 if (h != NULL)
5820 {
5821 if (h->root.root.string[0] == '.'
5822 && h->root.root.string[1] != 0
5823 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5824 ;
5825 else
5826 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5827 }
5828 else
5829 {
5830 asection *s;
5831 Elf_Internal_Sym *isym;
5832
5833 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5834 abfd, r_symndx);
5835 if (isym == NULL)
5836 return FALSE;
5837
5838 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5839 if (s != NULL && s != sec)
5840 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5841 }
5842 }
5843 /* Fall through. */
5844
5845 case R_PPC64_ADDR16:
5846 case R_PPC64_ADDR16_DS:
5847 case R_PPC64_ADDR16_HA:
5848 case R_PPC64_ADDR16_HI:
5849 case R_PPC64_ADDR16_HIGH:
5850 case R_PPC64_ADDR16_HIGHA:
5851 case R_PPC64_ADDR16_HIGHER:
5852 case R_PPC64_ADDR16_HIGHERA:
5853 case R_PPC64_ADDR16_HIGHEST:
5854 case R_PPC64_ADDR16_HIGHESTA:
5855 case R_PPC64_ADDR16_LO:
5856 case R_PPC64_ADDR16_LO_DS:
5857 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5858 && rel->r_addend == 0)
5859 {
5860 /* We may need a .plt entry if this reloc refers to a
5861 function in a shared lib. */
5862 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5863 return FALSE;
5864 h->pointer_equality_needed = 1;
5865 }
5866 /* Fall through. */
5867
5868 case R_PPC64_REL30:
5869 case R_PPC64_REL32:
5870 case R_PPC64_REL64:
5871 case R_PPC64_ADDR32:
5872 case R_PPC64_UADDR16:
5873 case R_PPC64_UADDR32:
5874 case R_PPC64_UADDR64:
5875 case R_PPC64_TOC:
5876 if (h != NULL && !bfd_link_pic (info))
5877 /* We may need a copy reloc. */
5878 h->non_got_ref = 1;
5879
5880 /* Don't propagate .opd relocs. */
5881 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5882 break;
5883
5884 /* If we are creating a shared library, and this is a reloc
5885 against a global symbol, or a non PC relative reloc
5886 against a local symbol, then we need to copy the reloc
5887 into the shared library. However, if we are linking with
5888 -Bsymbolic, we do not need to copy a reloc against a
5889 global symbol which is defined in an object we are
5890 including in the link (i.e., DEF_REGULAR is set). At
5891 this point we have not seen all the input files, so it is
5892 possible that DEF_REGULAR is not set now but will be set
5893 later (it is never cleared). In case of a weak definition,
5894 DEF_REGULAR may be cleared later by a strong definition in
5895 a shared library. We account for that possibility below by
5896 storing information in the dyn_relocs field of the hash
5897 table entry. A similar situation occurs when creating
5898 shared libraries and symbol visibility changes render the
5899 symbol local.
5900
5901 If on the other hand, we are creating an executable, we
5902 may need to keep relocations for symbols satisfied by a
5903 dynamic library if we manage to avoid copy relocs for the
5904 symbol. */
5905 dodyn:
5906 if ((bfd_link_pic (info)
5907 && (must_be_dyn_reloc (info, r_type)
5908 || (h != NULL
5909 && (!SYMBOLIC_BIND (info, h)
5910 || h->root.type == bfd_link_hash_defweak
5911 || !h->def_regular))))
5912 || (ELIMINATE_COPY_RELOCS
5913 && !bfd_link_pic (info)
5914 && h != NULL
5915 && (h->root.type == bfd_link_hash_defweak
5916 || !h->def_regular))
5917 || (!bfd_link_pic (info)
5918 && ifunc != NULL))
5919 {
5920 /* We must copy these reloc types into the output file.
5921 Create a reloc section in dynobj and make room for
5922 this reloc. */
5923 if (sreloc == NULL)
5924 {
5925 sreloc = _bfd_elf_make_dynamic_reloc_section
5926 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5927
5928 if (sreloc == NULL)
5929 return FALSE;
5930 }
5931
5932 /* If this is a global symbol, we count the number of
5933 relocations we need for this symbol. */
5934 if (h != NULL)
5935 {
5936 struct elf_dyn_relocs *p;
5937 struct elf_dyn_relocs **head;
5938
5939 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5940 p = *head;
5941 if (p == NULL || p->sec != sec)
5942 {
5943 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5944 if (p == NULL)
5945 return FALSE;
5946 p->next = *head;
5947 *head = p;
5948 p->sec = sec;
5949 p->count = 0;
5950 p->pc_count = 0;
5951 }
5952 p->count += 1;
5953 if (!must_be_dyn_reloc (info, r_type))
5954 p->pc_count += 1;
5955 }
5956 else
5957 {
5958 /* Track dynamic relocs needed for local syms too.
5959 We really need local syms available to do this
5960 easily. Oh well. */
5961 struct ppc_dyn_relocs *p;
5962 struct ppc_dyn_relocs **head;
5963 bfd_boolean is_ifunc;
5964 asection *s;
5965 void *vpp;
5966 Elf_Internal_Sym *isym;
5967
5968 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5969 abfd, r_symndx);
5970 if (isym == NULL)
5971 return FALSE;
5972
5973 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5974 if (s == NULL)
5975 s = sec;
5976
5977 vpp = &elf_section_data (s)->local_dynrel;
5978 head = (struct ppc_dyn_relocs **) vpp;
5979 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5980 p = *head;
5981 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5982 p = p->next;
5983 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5984 {
5985 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5986 if (p == NULL)
5987 return FALSE;
5988 p->next = *head;
5989 *head = p;
5990 p->sec = sec;
5991 p->ifunc = is_ifunc;
5992 p->count = 0;
5993 }
5994 p->count += 1;
5995 }
5996 }
5997 break;
5998
5999 default:
6000 break;
6001 }
6002 }
6003
6004 return TRUE;
6005 }
6006
6007 /* Merge backend specific data from an object file to the output
6008 object file when linking. */
6009
6010 static bfd_boolean
6011 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6012 {
6013 bfd *obfd = info->output_bfd;
6014 unsigned long iflags, oflags;
6015
6016 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6017 return TRUE;
6018
6019 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6020 return TRUE;
6021
6022 if (!_bfd_generic_verify_endian_match (ibfd, info))
6023 return FALSE;
6024
6025 iflags = elf_elfheader (ibfd)->e_flags;
6026 oflags = elf_elfheader (obfd)->e_flags;
6027
6028 if (iflags & ~EF_PPC64_ABI)
6029 {
6030 _bfd_error_handler
6031 /* xgettext:c-format */
6032 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6033 bfd_set_error (bfd_error_bad_value);
6034 return FALSE;
6035 }
6036 else if (iflags != oflags && iflags != 0)
6037 {
6038 _bfd_error_handler
6039 /* xgettext:c-format */
6040 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6041 ibfd, iflags, oflags);
6042 bfd_set_error (bfd_error_bad_value);
6043 return FALSE;
6044 }
6045
6046 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6047
6048 /* Merge Tag_compatibility attributes and any common GNU ones. */
6049 _bfd_elf_merge_object_attributes (ibfd, info);
6050
6051 return TRUE;
6052 }
6053
6054 static bfd_boolean
6055 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6056 {
6057 /* Print normal ELF private data. */
6058 _bfd_elf_print_private_bfd_data (abfd, ptr);
6059
6060 if (elf_elfheader (abfd)->e_flags != 0)
6061 {
6062 FILE *file = ptr;
6063
6064 fprintf (file, _("private flags = 0x%lx:"),
6065 elf_elfheader (abfd)->e_flags);
6066
6067 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6068 fprintf (file, _(" [abiv%ld]"),
6069 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6070 fputc ('\n', file);
6071 }
6072
6073 return TRUE;
6074 }
6075
6076 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6077 of the code entry point, and its section, which must be in the same
6078 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6079
6080 static bfd_vma
6081 opd_entry_value (asection *opd_sec,
6082 bfd_vma offset,
6083 asection **code_sec,
6084 bfd_vma *code_off,
6085 bfd_boolean in_code_sec)
6086 {
6087 bfd *opd_bfd = opd_sec->owner;
6088 Elf_Internal_Rela *relocs;
6089 Elf_Internal_Rela *lo, *hi, *look;
6090 bfd_vma val;
6091
6092 /* No relocs implies we are linking a --just-symbols object, or looking
6093 at a final linked executable with addr2line or somesuch. */
6094 if (opd_sec->reloc_count == 0)
6095 {
6096 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6097
6098 if (contents == NULL)
6099 {
6100 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6101 return (bfd_vma) -1;
6102 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6103 }
6104
6105 /* PR 17512: file: 64b9dfbb. */
6106 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6107 return (bfd_vma) -1;
6108
6109 val = bfd_get_64 (opd_bfd, contents + offset);
6110 if (code_sec != NULL)
6111 {
6112 asection *sec, *likely = NULL;
6113
6114 if (in_code_sec)
6115 {
6116 sec = *code_sec;
6117 if (sec->vma <= val
6118 && val < sec->vma + sec->size)
6119 likely = sec;
6120 else
6121 val = -1;
6122 }
6123 else
6124 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6125 if (sec->vma <= val
6126 && (sec->flags & SEC_LOAD) != 0
6127 && (sec->flags & SEC_ALLOC) != 0)
6128 likely = sec;
6129 if (likely != NULL)
6130 {
6131 *code_sec = likely;
6132 if (code_off != NULL)
6133 *code_off = val - likely->vma;
6134 }
6135 }
6136 return val;
6137 }
6138
6139 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6140
6141 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6142 if (relocs == NULL)
6143 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6144 /* PR 17512: file: df8e1fd6. */
6145 if (relocs == NULL)
6146 return (bfd_vma) -1;
6147
6148 /* Go find the opd reloc at the sym address. */
6149 lo = relocs;
6150 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6151 val = (bfd_vma) -1;
6152 while (lo < hi)
6153 {
6154 look = lo + (hi - lo) / 2;
6155 if (look->r_offset < offset)
6156 lo = look + 1;
6157 else if (look->r_offset > offset)
6158 hi = look;
6159 else
6160 {
6161 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6162
6163 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6164 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6165 {
6166 unsigned long symndx = ELF64_R_SYM (look->r_info);
6167 asection *sec = NULL;
6168
6169 if (symndx >= symtab_hdr->sh_info
6170 && elf_sym_hashes (opd_bfd) != NULL)
6171 {
6172 struct elf_link_hash_entry **sym_hashes;
6173 struct elf_link_hash_entry *rh;
6174
6175 sym_hashes = elf_sym_hashes (opd_bfd);
6176 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6177 if (rh != NULL)
6178 {
6179 rh = elf_follow_link (rh);
6180 if (rh->root.type != bfd_link_hash_defined
6181 && rh->root.type != bfd_link_hash_defweak)
6182 break;
6183 if (rh->root.u.def.section->owner == opd_bfd)
6184 {
6185 val = rh->root.u.def.value;
6186 sec = rh->root.u.def.section;
6187 }
6188 }
6189 }
6190
6191 if (sec == NULL)
6192 {
6193 Elf_Internal_Sym *sym;
6194
6195 if (symndx < symtab_hdr->sh_info)
6196 {
6197 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6198 if (sym == NULL)
6199 {
6200 size_t symcnt = symtab_hdr->sh_info;
6201 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6202 symcnt, 0,
6203 NULL, NULL, NULL);
6204 if (sym == NULL)
6205 break;
6206 symtab_hdr->contents = (bfd_byte *) sym;
6207 }
6208 sym += symndx;
6209 }
6210 else
6211 {
6212 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6213 1, symndx,
6214 NULL, NULL, NULL);
6215 if (sym == NULL)
6216 break;
6217 }
6218 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6219 if (sec == NULL)
6220 break;
6221 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6222 val = sym->st_value;
6223 }
6224
6225 val += look->r_addend;
6226 if (code_off != NULL)
6227 *code_off = val;
6228 if (code_sec != NULL)
6229 {
6230 if (in_code_sec && *code_sec != sec)
6231 return -1;
6232 else
6233 *code_sec = sec;
6234 }
6235 if (sec->output_section != NULL)
6236 val += sec->output_section->vma + sec->output_offset;
6237 }
6238 break;
6239 }
6240 }
6241
6242 return val;
6243 }
6244
6245 /* If the ELF symbol SYM might be a function in SEC, return the
6246 function size and set *CODE_OFF to the function's entry point,
6247 otherwise return zero. */
6248
6249 static bfd_size_type
6250 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6251 bfd_vma *code_off)
6252 {
6253 bfd_size_type size;
6254
6255 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6256 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6257 return 0;
6258
6259 size = 0;
6260 if (!(sym->flags & BSF_SYNTHETIC))
6261 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6262
6263 if (strcmp (sym->section->name, ".opd") == 0)
6264 {
6265 struct _opd_sec_data *opd = get_opd_info (sym->section);
6266 bfd_vma symval = sym->value;
6267
6268 if (opd != NULL
6269 && opd->adjust != NULL
6270 && elf_section_data (sym->section)->relocs != NULL)
6271 {
6272 /* opd_entry_value will use cached relocs that have been
6273 adjusted, but with raw symbols. That means both local
6274 and global symbols need adjusting. */
6275 long adjust = opd->adjust[OPD_NDX (symval)];
6276 if (adjust == -1)
6277 return 0;
6278 symval += adjust;
6279 }
6280
6281 if (opd_entry_value (sym->section, symval,
6282 &sec, code_off, TRUE) == (bfd_vma) -1)
6283 return 0;
6284 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6285 symbol. This size has nothing to do with the code size of the
6286 function, which is what we're supposed to return, but the
6287 code size isn't available without looking up the dot-sym.
6288 However, doing that would be a waste of time particularly
6289 since elf_find_function will look at the dot-sym anyway.
6290 Now, elf_find_function will keep the largest size of any
6291 function sym found at the code address of interest, so return
6292 1 here to avoid it incorrectly caching a larger function size
6293 for a small function. This does mean we return the wrong
6294 size for a new-ABI function of size 24, but all that does is
6295 disable caching for such functions. */
6296 if (size == 24)
6297 size = 1;
6298 }
6299 else
6300 {
6301 if (sym->section != sec)
6302 return 0;
6303 *code_off = sym->value;
6304 }
6305 if (size == 0)
6306 size = 1;
6307 return size;
6308 }
6309
6310 /* Return true if symbol is defined in a regular object file. */
6311
6312 static bfd_boolean
6313 is_static_defined (struct elf_link_hash_entry *h)
6314 {
6315 return ((h->root.type == bfd_link_hash_defined
6316 || h->root.type == bfd_link_hash_defweak)
6317 && h->root.u.def.section != NULL
6318 && h->root.u.def.section->output_section != NULL);
6319 }
6320
6321 /* If FDH is a function descriptor symbol, return the associated code
6322 entry symbol if it is defined. Return NULL otherwise. */
6323
6324 static struct ppc_link_hash_entry *
6325 defined_code_entry (struct ppc_link_hash_entry *fdh)
6326 {
6327 if (fdh->is_func_descriptor)
6328 {
6329 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6330 if (fh->elf.root.type == bfd_link_hash_defined
6331 || fh->elf.root.type == bfd_link_hash_defweak)
6332 return fh;
6333 }
6334 return NULL;
6335 }
6336
6337 /* If FH is a function code entry symbol, return the associated
6338 function descriptor symbol if it is defined. Return NULL otherwise. */
6339
6340 static struct ppc_link_hash_entry *
6341 defined_func_desc (struct ppc_link_hash_entry *fh)
6342 {
6343 if (fh->oh != NULL
6344 && fh->oh->is_func_descriptor)
6345 {
6346 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6347 if (fdh->elf.root.type == bfd_link_hash_defined
6348 || fdh->elf.root.type == bfd_link_hash_defweak)
6349 return fdh;
6350 }
6351 return NULL;
6352 }
6353
6354 /* Mark all our entry sym sections, both opd and code section. */
6355
6356 static void
6357 ppc64_elf_gc_keep (struct bfd_link_info *info)
6358 {
6359 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6360 struct bfd_sym_chain *sym;
6361
6362 if (htab == NULL)
6363 return;
6364
6365 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6366 {
6367 struct ppc_link_hash_entry *eh, *fh;
6368 asection *sec;
6369
6370 eh = (struct ppc_link_hash_entry *)
6371 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6372 if (eh == NULL)
6373 continue;
6374 if (eh->elf.root.type != bfd_link_hash_defined
6375 && eh->elf.root.type != bfd_link_hash_defweak)
6376 continue;
6377
6378 fh = defined_code_entry (eh);
6379 if (fh != NULL)
6380 {
6381 sec = fh->elf.root.u.def.section;
6382 sec->flags |= SEC_KEEP;
6383 }
6384 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6385 && opd_entry_value (eh->elf.root.u.def.section,
6386 eh->elf.root.u.def.value,
6387 &sec, NULL, FALSE) != (bfd_vma) -1)
6388 sec->flags |= SEC_KEEP;
6389
6390 sec = eh->elf.root.u.def.section;
6391 sec->flags |= SEC_KEEP;
6392 }
6393 }
6394
6395 /* Mark sections containing dynamically referenced symbols. When
6396 building shared libraries, we must assume that any visible symbol is
6397 referenced. */
6398
6399 static bfd_boolean
6400 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6401 {
6402 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6403 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6404 struct ppc_link_hash_entry *fdh;
6405 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6406
6407 /* Dynamic linking info is on the func descriptor sym. */
6408 fdh = defined_func_desc (eh);
6409 if (fdh != NULL)
6410 eh = fdh;
6411
6412 if ((eh->elf.root.type == bfd_link_hash_defined
6413 || eh->elf.root.type == bfd_link_hash_defweak)
6414 && (eh->elf.ref_dynamic
6415 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6416 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6417 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6418 && (!bfd_link_executable (info)
6419 || info->export_dynamic
6420 || (eh->elf.dynamic
6421 && d != NULL
6422 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6423 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6424 || !bfd_hide_sym_by_version (info->version_info,
6425 eh->elf.root.root.string)))))
6426 {
6427 asection *code_sec;
6428 struct ppc_link_hash_entry *fh;
6429
6430 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6431
6432 /* Function descriptor syms cause the associated
6433 function code sym section to be marked. */
6434 fh = defined_code_entry (eh);
6435 if (fh != NULL)
6436 {
6437 code_sec = fh->elf.root.u.def.section;
6438 code_sec->flags |= SEC_KEEP;
6439 }
6440 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6441 && opd_entry_value (eh->elf.root.u.def.section,
6442 eh->elf.root.u.def.value,
6443 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6444 code_sec->flags |= SEC_KEEP;
6445 }
6446
6447 return TRUE;
6448 }
6449
6450 /* Return the section that should be marked against GC for a given
6451 relocation. */
6452
6453 static asection *
6454 ppc64_elf_gc_mark_hook (asection *sec,
6455 struct bfd_link_info *info,
6456 Elf_Internal_Rela *rel,
6457 struct elf_link_hash_entry *h,
6458 Elf_Internal_Sym *sym)
6459 {
6460 asection *rsec;
6461
6462 /* Syms return NULL if we're marking .opd, so we avoid marking all
6463 function sections, as all functions are referenced in .opd. */
6464 rsec = NULL;
6465 if (get_opd_info (sec) != NULL)
6466 return rsec;
6467
6468 if (h != NULL)
6469 {
6470 enum elf_ppc64_reloc_type r_type;
6471 struct ppc_link_hash_entry *eh, *fh, *fdh;
6472
6473 r_type = ELF64_R_TYPE (rel->r_info);
6474 switch (r_type)
6475 {
6476 case R_PPC64_GNU_VTINHERIT:
6477 case R_PPC64_GNU_VTENTRY:
6478 break;
6479
6480 default:
6481 switch (h->root.type)
6482 {
6483 case bfd_link_hash_defined:
6484 case bfd_link_hash_defweak:
6485 eh = (struct ppc_link_hash_entry *) h;
6486 fdh = defined_func_desc (eh);
6487 if (fdh != NULL)
6488 eh = fdh;
6489
6490 /* Function descriptor syms cause the associated
6491 function code sym section to be marked. */
6492 fh = defined_code_entry (eh);
6493 if (fh != NULL)
6494 {
6495 /* They also mark their opd section. */
6496 eh->elf.root.u.def.section->gc_mark = 1;
6497
6498 rsec = fh->elf.root.u.def.section;
6499 }
6500 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6501 && opd_entry_value (eh->elf.root.u.def.section,
6502 eh->elf.root.u.def.value,
6503 &rsec, NULL, FALSE) != (bfd_vma) -1)
6504 eh->elf.root.u.def.section->gc_mark = 1;
6505 else
6506 rsec = h->root.u.def.section;
6507 break;
6508
6509 case bfd_link_hash_common:
6510 rsec = h->root.u.c.p->section;
6511 break;
6512
6513 default:
6514 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6515 }
6516 }
6517 }
6518 else
6519 {
6520 struct _opd_sec_data *opd;
6521
6522 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6523 opd = get_opd_info (rsec);
6524 if (opd != NULL && opd->func_sec != NULL)
6525 {
6526 rsec->gc_mark = 1;
6527
6528 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6529 }
6530 }
6531
6532 return rsec;
6533 }
6534
6535 /* Update the .got, .plt. and dynamic reloc reference counts for the
6536 section being removed. */
6537
6538 static bfd_boolean
6539 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6540 asection *sec, const Elf_Internal_Rela *relocs)
6541 {
6542 struct ppc_link_hash_table *htab;
6543 Elf_Internal_Shdr *symtab_hdr;
6544 struct elf_link_hash_entry **sym_hashes;
6545 struct got_entry **local_got_ents;
6546 const Elf_Internal_Rela *rel, *relend;
6547
6548 if (bfd_link_relocatable (info))
6549 return TRUE;
6550
6551 if ((sec->flags & SEC_ALLOC) == 0)
6552 return TRUE;
6553
6554 elf_section_data (sec)->local_dynrel = NULL;
6555
6556 htab = ppc_hash_table (info);
6557 if (htab == NULL)
6558 return FALSE;
6559
6560 symtab_hdr = &elf_symtab_hdr (abfd);
6561 sym_hashes = elf_sym_hashes (abfd);
6562 local_got_ents = elf_local_got_ents (abfd);
6563
6564 relend = relocs + sec->reloc_count;
6565 for (rel = relocs; rel < relend; rel++)
6566 {
6567 unsigned long r_symndx;
6568 enum elf_ppc64_reloc_type r_type;
6569 struct elf_link_hash_entry *h = NULL;
6570 struct plt_entry **plt_list;
6571 unsigned char tls_type = 0;
6572
6573 r_symndx = ELF64_R_SYM (rel->r_info);
6574 r_type = ELF64_R_TYPE (rel->r_info);
6575 if (r_symndx >= symtab_hdr->sh_info)
6576 {
6577 struct ppc_link_hash_entry *eh;
6578 struct elf_dyn_relocs **pp;
6579 struct elf_dyn_relocs *p;
6580
6581 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6582 h = elf_follow_link (h);
6583 eh = (struct ppc_link_hash_entry *) h;
6584
6585 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6586 if (p->sec == sec)
6587 {
6588 /* Everything must go for SEC. */
6589 *pp = p->next;
6590 break;
6591 }
6592 }
6593
6594 switch (r_type)
6595 {
6596 case R_PPC64_GOT_TLSLD16:
6597 case R_PPC64_GOT_TLSLD16_LO:
6598 case R_PPC64_GOT_TLSLD16_HI:
6599 case R_PPC64_GOT_TLSLD16_HA:
6600 tls_type = TLS_TLS | TLS_LD;
6601 goto dogot;
6602
6603 case R_PPC64_GOT_TLSGD16:
6604 case R_PPC64_GOT_TLSGD16_LO:
6605 case R_PPC64_GOT_TLSGD16_HI:
6606 case R_PPC64_GOT_TLSGD16_HA:
6607 tls_type = TLS_TLS | TLS_GD;
6608 goto dogot;
6609
6610 case R_PPC64_GOT_TPREL16_DS:
6611 case R_PPC64_GOT_TPREL16_LO_DS:
6612 case R_PPC64_GOT_TPREL16_HI:
6613 case R_PPC64_GOT_TPREL16_HA:
6614 tls_type = TLS_TLS | TLS_TPREL;
6615 goto dogot;
6616
6617 case R_PPC64_GOT_DTPREL16_DS:
6618 case R_PPC64_GOT_DTPREL16_LO_DS:
6619 case R_PPC64_GOT_DTPREL16_HI:
6620 case R_PPC64_GOT_DTPREL16_HA:
6621 tls_type = TLS_TLS | TLS_DTPREL;
6622 goto dogot;
6623
6624 case R_PPC64_GOT16:
6625 case R_PPC64_GOT16_DS:
6626 case R_PPC64_GOT16_HA:
6627 case R_PPC64_GOT16_HI:
6628 case R_PPC64_GOT16_LO:
6629 case R_PPC64_GOT16_LO_DS:
6630 dogot:
6631 {
6632 struct got_entry *ent;
6633
6634 if (h != NULL)
6635 ent = h->got.glist;
6636 else
6637 ent = local_got_ents[r_symndx];
6638
6639 for (; ent != NULL; ent = ent->next)
6640 if (ent->addend == rel->r_addend
6641 && ent->owner == abfd
6642 && ent->tls_type == tls_type)
6643 break;
6644 if (ent == NULL)
6645 abort ();
6646 if (ent->got.refcount > 0)
6647 ent->got.refcount -= 1;
6648 }
6649 break;
6650
6651 case R_PPC64_PLT16_HA:
6652 case R_PPC64_PLT16_HI:
6653 case R_PPC64_PLT16_LO:
6654 case R_PPC64_PLT32:
6655 case R_PPC64_PLT64:
6656 case R_PPC64_REL14:
6657 case R_PPC64_REL14_BRNTAKEN:
6658 case R_PPC64_REL14_BRTAKEN:
6659 case R_PPC64_REL24:
6660 plt_list = NULL;
6661 if (h != NULL)
6662 plt_list = &h->plt.plist;
6663 else if (local_got_ents != NULL)
6664 {
6665 struct plt_entry **local_plt = (struct plt_entry **)
6666 (local_got_ents + symtab_hdr->sh_info);
6667 unsigned char *local_got_tls_masks = (unsigned char *)
6668 (local_plt + symtab_hdr->sh_info);
6669 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6670 plt_list = local_plt + r_symndx;
6671 }
6672 if (plt_list)
6673 {
6674 struct plt_entry *ent;
6675
6676 for (ent = *plt_list; ent != NULL; ent = ent->next)
6677 if (ent->addend == rel->r_addend)
6678 break;
6679 if (ent != NULL && ent->plt.refcount > 0)
6680 ent->plt.refcount -= 1;
6681 }
6682 break;
6683
6684 default:
6685 break;
6686 }
6687 }
6688 return TRUE;
6689 }
6690
6691 /* The maximum size of .sfpr. */
6692 #define SFPR_MAX (218*4)
6693
6694 struct sfpr_def_parms
6695 {
6696 const char name[12];
6697 unsigned char lo, hi;
6698 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6699 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6700 };
6701
6702 /* Auto-generate _save*, _rest* functions in .sfpr.
6703 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6704 instead. */
6705
6706 static bfd_boolean
6707 sfpr_define (struct bfd_link_info *info,
6708 const struct sfpr_def_parms *parm,
6709 asection *stub_sec)
6710 {
6711 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6712 unsigned int i;
6713 size_t len = strlen (parm->name);
6714 bfd_boolean writing = FALSE;
6715 char sym[16];
6716
6717 if (htab == NULL)
6718 return FALSE;
6719
6720 memcpy (sym, parm->name, len);
6721 sym[len + 2] = 0;
6722
6723 for (i = parm->lo; i <= parm->hi; i++)
6724 {
6725 struct ppc_link_hash_entry *h;
6726
6727 sym[len + 0] = i / 10 + '0';
6728 sym[len + 1] = i % 10 + '0';
6729 h = (struct ppc_link_hash_entry *)
6730 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6731 if (stub_sec != NULL)
6732 {
6733 if (h != NULL
6734 && h->elf.root.type == bfd_link_hash_defined
6735 && h->elf.root.u.def.section == htab->sfpr)
6736 {
6737 struct elf_link_hash_entry *s;
6738 char buf[32];
6739 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6740 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6741 if (s == NULL)
6742 return FALSE;
6743 if (s->root.type == bfd_link_hash_new
6744 || (s->root.type = bfd_link_hash_defined
6745 && s->root.u.def.section == stub_sec))
6746 {
6747 s->root.type = bfd_link_hash_defined;
6748 s->root.u.def.section = stub_sec;
6749 s->root.u.def.value = (stub_sec->size
6750 + h->elf.root.u.def.value);
6751 s->ref_regular = 1;
6752 s->def_regular = 1;
6753 s->ref_regular_nonweak = 1;
6754 s->forced_local = 1;
6755 s->non_elf = 0;
6756 s->root.linker_def = 1;
6757 }
6758 }
6759 continue;
6760 }
6761 if (h != NULL)
6762 {
6763 h->save_res = 1;
6764 if (!h->elf.def_regular)
6765 {
6766 h->elf.root.type = bfd_link_hash_defined;
6767 h->elf.root.u.def.section = htab->sfpr;
6768 h->elf.root.u.def.value = htab->sfpr->size;
6769 h->elf.type = STT_FUNC;
6770 h->elf.def_regular = 1;
6771 h->elf.non_elf = 0;
6772 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6773 writing = TRUE;
6774 if (htab->sfpr->contents == NULL)
6775 {
6776 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6777 if (htab->sfpr->contents == NULL)
6778 return FALSE;
6779 }
6780 }
6781 }
6782 if (writing)
6783 {
6784 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6785 if (i != parm->hi)
6786 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6787 else
6788 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6789 htab->sfpr->size = p - htab->sfpr->contents;
6790 }
6791 }
6792
6793 return TRUE;
6794 }
6795
6796 static bfd_byte *
6797 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6798 {
6799 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6800 return p + 4;
6801 }
6802
6803 static bfd_byte *
6804 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6805 {
6806 p = savegpr0 (abfd, p, r);
6807 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6808 p = p + 4;
6809 bfd_put_32 (abfd, BLR, p);
6810 return p + 4;
6811 }
6812
6813 static bfd_byte *
6814 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6815 {
6816 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6817 return p + 4;
6818 }
6819
6820 static bfd_byte *
6821 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6822 {
6823 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6824 p = p + 4;
6825 p = restgpr0 (abfd, p, r);
6826 bfd_put_32 (abfd, MTLR_R0, p);
6827 p = p + 4;
6828 if (r == 29)
6829 {
6830 p = restgpr0 (abfd, p, 30);
6831 p = restgpr0 (abfd, p, 31);
6832 }
6833 bfd_put_32 (abfd, BLR, p);
6834 return p + 4;
6835 }
6836
6837 static bfd_byte *
6838 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6839 {
6840 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6841 return p + 4;
6842 }
6843
6844 static bfd_byte *
6845 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6846 {
6847 p = savegpr1 (abfd, p, r);
6848 bfd_put_32 (abfd, BLR, p);
6849 return p + 4;
6850 }
6851
6852 static bfd_byte *
6853 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6854 {
6855 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6856 return p + 4;
6857 }
6858
6859 static bfd_byte *
6860 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6861 {
6862 p = restgpr1 (abfd, p, r);
6863 bfd_put_32 (abfd, BLR, p);
6864 return p + 4;
6865 }
6866
6867 static bfd_byte *
6868 savefpr (bfd *abfd, bfd_byte *p, int r)
6869 {
6870 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6871 return p + 4;
6872 }
6873
6874 static bfd_byte *
6875 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6876 {
6877 p = savefpr (abfd, p, r);
6878 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6879 p = p + 4;
6880 bfd_put_32 (abfd, BLR, p);
6881 return p + 4;
6882 }
6883
6884 static bfd_byte *
6885 restfpr (bfd *abfd, bfd_byte *p, int r)
6886 {
6887 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6888 return p + 4;
6889 }
6890
6891 static bfd_byte *
6892 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6893 {
6894 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6895 p = p + 4;
6896 p = restfpr (abfd, p, r);
6897 bfd_put_32 (abfd, MTLR_R0, p);
6898 p = p + 4;
6899 if (r == 29)
6900 {
6901 p = restfpr (abfd, p, 30);
6902 p = restfpr (abfd, p, 31);
6903 }
6904 bfd_put_32 (abfd, BLR, p);
6905 return p + 4;
6906 }
6907
6908 static bfd_byte *
6909 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6910 {
6911 p = savefpr (abfd, p, r);
6912 bfd_put_32 (abfd, BLR, p);
6913 return p + 4;
6914 }
6915
6916 static bfd_byte *
6917 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6918 {
6919 p = restfpr (abfd, p, r);
6920 bfd_put_32 (abfd, BLR, p);
6921 return p + 4;
6922 }
6923
6924 static bfd_byte *
6925 savevr (bfd *abfd, bfd_byte *p, int r)
6926 {
6927 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6928 p = p + 4;
6929 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6930 return p + 4;
6931 }
6932
6933 static bfd_byte *
6934 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6935 {
6936 p = savevr (abfd, p, r);
6937 bfd_put_32 (abfd, BLR, p);
6938 return p + 4;
6939 }
6940
6941 static bfd_byte *
6942 restvr (bfd *abfd, bfd_byte *p, int r)
6943 {
6944 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6945 p = p + 4;
6946 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6947 return p + 4;
6948 }
6949
6950 static bfd_byte *
6951 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6952 {
6953 p = restvr (abfd, p, r);
6954 bfd_put_32 (abfd, BLR, p);
6955 return p + 4;
6956 }
6957
6958 /* Called via elf_link_hash_traverse to transfer dynamic linking
6959 information on function code symbol entries to their corresponding
6960 function descriptor symbol entries. */
6961
6962 static bfd_boolean
6963 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6964 {
6965 struct bfd_link_info *info;
6966 struct ppc_link_hash_table *htab;
6967 struct plt_entry *ent;
6968 struct ppc_link_hash_entry *fh;
6969 struct ppc_link_hash_entry *fdh;
6970 bfd_boolean force_local;
6971
6972 fh = (struct ppc_link_hash_entry *) h;
6973 if (fh->elf.root.type == bfd_link_hash_indirect)
6974 return TRUE;
6975
6976 info = inf;
6977 htab = ppc_hash_table (info);
6978 if (htab == NULL)
6979 return FALSE;
6980
6981 /* Resolve undefined references to dot-symbols as the value
6982 in the function descriptor, if we have one in a regular object.
6983 This is to satisfy cases like ".quad .foo". Calls to functions
6984 in dynamic objects are handled elsewhere. */
6985 if (fh->elf.root.type == bfd_link_hash_undefweak
6986 && fh->was_undefined
6987 && (fdh = defined_func_desc (fh)) != NULL
6988 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6989 && opd_entry_value (fdh->elf.root.u.def.section,
6990 fdh->elf.root.u.def.value,
6991 &fh->elf.root.u.def.section,
6992 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6993 {
6994 fh->elf.root.type = fdh->elf.root.type;
6995 fh->elf.forced_local = 1;
6996 fh->elf.def_regular = fdh->elf.def_regular;
6997 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6998 }
6999
7000 /* If this is a function code symbol, transfer dynamic linking
7001 information to the function descriptor symbol. */
7002 if (!fh->is_func)
7003 return TRUE;
7004
7005 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7006 if (ent->plt.refcount > 0)
7007 break;
7008 if (ent == NULL
7009 || fh->elf.root.root.string[0] != '.'
7010 || fh->elf.root.root.string[1] == '\0')
7011 return TRUE;
7012
7013 /* Find the corresponding function descriptor symbol. Create it
7014 as undefined if necessary. */
7015
7016 fdh = lookup_fdh (fh, htab);
7017 if (fdh == NULL
7018 && !bfd_link_executable (info)
7019 && (fh->elf.root.type == bfd_link_hash_undefined
7020 || fh->elf.root.type == bfd_link_hash_undefweak))
7021 {
7022 fdh = make_fdh (info, fh);
7023 if (fdh == NULL)
7024 return FALSE;
7025 }
7026
7027 /* Fake function descriptors are made undefweak. If the function
7028 code symbol is strong undefined, make the fake sym the same.
7029 If the function code symbol is defined, then force the fake
7030 descriptor local; We can't support overriding of symbols in a
7031 shared library on a fake descriptor. */
7032
7033 if (fdh != NULL
7034 && fdh->fake
7035 && fdh->elf.root.type == bfd_link_hash_undefweak)
7036 {
7037 if (fh->elf.root.type == bfd_link_hash_undefined)
7038 {
7039 fdh->elf.root.type = bfd_link_hash_undefined;
7040 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
7041 }
7042 else if (fh->elf.root.type == bfd_link_hash_defined
7043 || fh->elf.root.type == bfd_link_hash_defweak)
7044 {
7045 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7046 }
7047 }
7048
7049 if (fdh != NULL
7050 && !fdh->elf.forced_local
7051 && (!bfd_link_executable (info)
7052 || fdh->elf.def_dynamic
7053 || fdh->elf.ref_dynamic
7054 || (fdh->elf.root.type == bfd_link_hash_undefweak
7055 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
7056 {
7057 if (fdh->elf.dynindx == -1)
7058 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7059 return FALSE;
7060 fdh->elf.ref_regular |= fh->elf.ref_regular;
7061 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7062 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7063 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7064 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
7065 {
7066 move_plt_plist (fh, fdh);
7067 fdh->elf.needs_plt = 1;
7068 }
7069 fdh->is_func_descriptor = 1;
7070 fdh->oh = fh;
7071 fh->oh = fdh;
7072 }
7073
7074 /* Now that the info is on the function descriptor, clear the
7075 function code sym info. Any function code syms for which we
7076 don't have a definition in a regular file, we force local.
7077 This prevents a shared library from exporting syms that have
7078 been imported from another library. Function code syms that
7079 are really in the library we must leave global to prevent the
7080 linker dragging in a definition from a static library. */
7081 force_local = (!fh->elf.def_regular
7082 || fdh == NULL
7083 || !fdh->elf.def_regular
7084 || fdh->elf.forced_local);
7085 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7086
7087 return TRUE;
7088 }
7089
7090 static const struct sfpr_def_parms save_res_funcs[] =
7091 {
7092 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7093 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7094 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7095 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7096 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7097 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7098 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7099 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7100 { "._savef", 14, 31, savefpr, savefpr1_tail },
7101 { "._restf", 14, 31, restfpr, restfpr1_tail },
7102 { "_savevr_", 20, 31, savevr, savevr_tail },
7103 { "_restvr_", 20, 31, restvr, restvr_tail }
7104 };
7105
7106 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7107 this hook to a) provide some gcc support functions, and b) transfer
7108 dynamic linking information gathered so far on function code symbol
7109 entries, to their corresponding function descriptor symbol entries. */
7110
7111 static bfd_boolean
7112 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7113 struct bfd_link_info *info)
7114 {
7115 struct ppc_link_hash_table *htab;
7116
7117 htab = ppc_hash_table (info);
7118 if (htab == NULL)
7119 return FALSE;
7120
7121 /* Provide any missing _save* and _rest* functions. */
7122 if (htab->sfpr != NULL)
7123 {
7124 unsigned int i;
7125
7126 htab->sfpr->size = 0;
7127 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7128 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7129 return FALSE;
7130 if (htab->sfpr->size == 0)
7131 htab->sfpr->flags |= SEC_EXCLUDE;
7132 }
7133
7134 if (bfd_link_relocatable (info))
7135 return TRUE;
7136
7137 if (htab->elf.hgot != NULL)
7138 {
7139 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7140 /* Make .TOC. defined so as to prevent it being made dynamic.
7141 The wrong value here is fixed later in ppc64_elf_set_toc. */
7142 if (!htab->elf.hgot->def_regular
7143 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7144 {
7145 htab->elf.hgot->root.type = bfd_link_hash_defined;
7146 htab->elf.hgot->root.u.def.value = 0;
7147 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7148 htab->elf.hgot->def_regular = 1;
7149 htab->elf.hgot->root.linker_def = 1;
7150 }
7151 htab->elf.hgot->type = STT_OBJECT;
7152 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7153 | STV_HIDDEN);
7154 }
7155
7156 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7157
7158 return TRUE;
7159 }
7160
7161 /* Return true if we have dynamic relocs against H that apply to
7162 read-only sections. */
7163
7164 static bfd_boolean
7165 readonly_dynrelocs (struct elf_link_hash_entry *h)
7166 {
7167 struct ppc_link_hash_entry *eh;
7168 struct elf_dyn_relocs *p;
7169
7170 eh = (struct ppc_link_hash_entry *) h;
7171 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7172 {
7173 asection *s = p->sec->output_section;
7174
7175 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7176 return TRUE;
7177 }
7178 return FALSE;
7179 }
7180
7181 /* Return true if we have dynamic relocs against H or any of its weak
7182 aliases, that apply to read-only sections. */
7183
7184 static bfd_boolean
7185 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7186 {
7187 struct ppc_link_hash_entry *eh;
7188
7189 eh = (struct ppc_link_hash_entry *) h;
7190 do
7191 {
7192 if (readonly_dynrelocs (&eh->elf))
7193 return TRUE;
7194 eh = eh->weakref;
7195 } while (eh != NULL && &eh->elf != h);
7196
7197 return FALSE;
7198 }
7199
7200 /* Return whether EH has pc-relative dynamic relocs. */
7201
7202 static bfd_boolean
7203 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7204 {
7205 struct elf_dyn_relocs *p;
7206
7207 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7208 if (p->pc_count != 0)
7209 return TRUE;
7210 return FALSE;
7211 }
7212
7213 /* Return true if a global entry stub will be created for H. Valid
7214 for ELFv2 before plt entries have been allocated. */
7215
7216 static bfd_boolean
7217 global_entry_stub (struct elf_link_hash_entry *h)
7218 {
7219 struct plt_entry *pent;
7220
7221 if (!h->pointer_equality_needed
7222 || h->def_regular)
7223 return FALSE;
7224
7225 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7226 if (pent->plt.refcount > 0
7227 && pent->addend == 0)
7228 return TRUE;
7229
7230 return FALSE;
7231 }
7232
7233 /* Adjust a symbol defined by a dynamic object and referenced by a
7234 regular object. The current definition is in some section of the
7235 dynamic object, but we're not including those sections. We have to
7236 change the definition to something the rest of the link can
7237 understand. */
7238
7239 static bfd_boolean
7240 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7241 struct elf_link_hash_entry *h)
7242 {
7243 struct ppc_link_hash_table *htab;
7244 asection *s;
7245
7246 htab = ppc_hash_table (info);
7247 if (htab == NULL)
7248 return FALSE;
7249
7250 /* Deal with function syms. */
7251 if (h->type == STT_FUNC
7252 || h->type == STT_GNU_IFUNC
7253 || h->needs_plt)
7254 {
7255 /* Clear procedure linkage table information for any symbol that
7256 won't need a .plt entry. */
7257 struct plt_entry *ent;
7258 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7259 if (ent->plt.refcount > 0)
7260 break;
7261 if (ent == NULL
7262 || (h->type != STT_GNU_IFUNC
7263 && (SYMBOL_CALLS_LOCAL (info, h)
7264 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7265 && h->root.type == bfd_link_hash_undefweak)))
7266 || ((struct ppc_link_hash_entry *) h)->save_res)
7267 {
7268 h->plt.plist = NULL;
7269 h->needs_plt = 0;
7270 h->pointer_equality_needed = 0;
7271 }
7272 else if (abiversion (info->output_bfd) >= 2)
7273 {
7274 /* Taking a function's address in a read/write section
7275 doesn't require us to define the function symbol in the
7276 executable on a global entry stub. A dynamic reloc can
7277 be used instead. The reason we prefer a few more dynamic
7278 relocs is that calling via a global entry stub costs a
7279 few more instructions, and pointer_equality_needed causes
7280 extra work in ld.so when resolving these symbols. */
7281 if (global_entry_stub (h)
7282 && !alias_readonly_dynrelocs (h))
7283 {
7284 h->pointer_equality_needed = 0;
7285 /* After adjust_dynamic_symbol, non_got_ref set in
7286 the non-pic case means that dyn_relocs for this
7287 symbol should be discarded. */
7288 h->non_got_ref = 0;
7289 }
7290
7291 /* If making a plt entry, then we don't need copy relocs. */
7292 return TRUE;
7293 }
7294 }
7295 else
7296 h->plt.plist = NULL;
7297
7298 /* If this is a weak symbol, and there is a real definition, the
7299 processor independent code will have arranged for us to see the
7300 real definition first, and we can just use the same value. */
7301 if (h->u.weakdef != NULL)
7302 {
7303 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7304 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7305 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7306 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7307 if (ELIMINATE_COPY_RELOCS)
7308 h->non_got_ref = h->u.weakdef->non_got_ref;
7309 return TRUE;
7310 }
7311
7312 /* If we are creating a shared library, we must presume that the
7313 only references to the symbol are via the global offset table.
7314 For such cases we need not do anything here; the relocations will
7315 be handled correctly by relocate_section. */
7316 if (bfd_link_pic (info))
7317 return TRUE;
7318
7319 /* If there are no references to this symbol that do not use the
7320 GOT, we don't need to generate a copy reloc. */
7321 if (!h->non_got_ref)
7322 return TRUE;
7323
7324 /* Don't generate a copy reloc for symbols defined in the executable. */
7325 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7326
7327 /* If -z nocopyreloc was given, don't generate them either. */
7328 || info->nocopyreloc
7329
7330 /* If we didn't find any dynamic relocs in read-only sections, then
7331 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7332 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7333
7334 /* Protected variables do not work with .dynbss. The copy in
7335 .dynbss won't be used by the shared library with the protected
7336 definition for the variable. Text relocations are preferable
7337 to an incorrect program. */
7338 || h->protected_def)
7339 {
7340 h->non_got_ref = 0;
7341 return TRUE;
7342 }
7343
7344 if (h->plt.plist != NULL)
7345 {
7346 /* We should never get here, but unfortunately there are versions
7347 of gcc out there that improperly (for this ABI) put initialized
7348 function pointers, vtable refs and suchlike in read-only
7349 sections. Allow them to proceed, but warn that this might
7350 break at runtime. */
7351 info->callbacks->einfo
7352 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7353 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7354 h->root.root.string);
7355 }
7356
7357 /* This is a reference to a symbol defined by a dynamic object which
7358 is not a function. */
7359
7360 /* We must allocate the symbol in our .dynbss section, which will
7361 become part of the .bss section of the executable. There will be
7362 an entry for this symbol in the .dynsym section. The dynamic
7363 object will contain position independent code, so all references
7364 from the dynamic object to this symbol will go through the global
7365 offset table. The dynamic linker will use the .dynsym entry to
7366 determine the address it must put in the global offset table, so
7367 both the dynamic object and the regular object will refer to the
7368 same memory location for the variable. */
7369
7370 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7371 to copy the initial value out of the dynamic object and into the
7372 runtime process image. We need to remember the offset into the
7373 .rela.bss section we are going to use. */
7374 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7375 {
7376 htab->relbss->size += sizeof (Elf64_External_Rela);
7377 h->needs_copy = 1;
7378 }
7379
7380 s = htab->dynbss;
7381
7382 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7383 }
7384
7385 /* If given a function descriptor symbol, hide both the function code
7386 sym and the descriptor. */
7387 static void
7388 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7389 struct elf_link_hash_entry *h,
7390 bfd_boolean force_local)
7391 {
7392 struct ppc_link_hash_entry *eh;
7393 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7394
7395 eh = (struct ppc_link_hash_entry *) h;
7396 if (eh->is_func_descriptor)
7397 {
7398 struct ppc_link_hash_entry *fh = eh->oh;
7399
7400 if (fh == NULL)
7401 {
7402 const char *p, *q;
7403 struct ppc_link_hash_table *htab;
7404 char save;
7405
7406 /* We aren't supposed to use alloca in BFD because on
7407 systems which do not have alloca the version in libiberty
7408 calls xmalloc, which might cause the program to crash
7409 when it runs out of memory. This function doesn't have a
7410 return status, so there's no way to gracefully return an
7411 error. So cheat. We know that string[-1] can be safely
7412 accessed; It's either a string in an ELF string table,
7413 or allocated in an objalloc structure. */
7414
7415 p = eh->elf.root.root.string - 1;
7416 save = *p;
7417 *(char *) p = '.';
7418 htab = ppc_hash_table (info);
7419 if (htab == NULL)
7420 return;
7421
7422 fh = (struct ppc_link_hash_entry *)
7423 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7424 *(char *) p = save;
7425
7426 /* Unfortunately, if it so happens that the string we were
7427 looking for was allocated immediately before this string,
7428 then we overwrote the string terminator. That's the only
7429 reason the lookup should fail. */
7430 if (fh == NULL)
7431 {
7432 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7433 while (q >= eh->elf.root.root.string && *q == *p)
7434 --q, --p;
7435 if (q < eh->elf.root.root.string && *p == '.')
7436 fh = (struct ppc_link_hash_entry *)
7437 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7438 }
7439 if (fh != NULL)
7440 {
7441 eh->oh = fh;
7442 fh->oh = eh;
7443 }
7444 }
7445 if (fh != NULL)
7446 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7447 }
7448 }
7449
7450 static bfd_boolean
7451 get_sym_h (struct elf_link_hash_entry **hp,
7452 Elf_Internal_Sym **symp,
7453 asection **symsecp,
7454 unsigned char **tls_maskp,
7455 Elf_Internal_Sym **locsymsp,
7456 unsigned long r_symndx,
7457 bfd *ibfd)
7458 {
7459 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7460
7461 if (r_symndx >= symtab_hdr->sh_info)
7462 {
7463 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7464 struct elf_link_hash_entry *h;
7465
7466 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7467 h = elf_follow_link (h);
7468
7469 if (hp != NULL)
7470 *hp = h;
7471
7472 if (symp != NULL)
7473 *symp = NULL;
7474
7475 if (symsecp != NULL)
7476 {
7477 asection *symsec = NULL;
7478 if (h->root.type == bfd_link_hash_defined
7479 || h->root.type == bfd_link_hash_defweak)
7480 symsec = h->root.u.def.section;
7481 *symsecp = symsec;
7482 }
7483
7484 if (tls_maskp != NULL)
7485 {
7486 struct ppc_link_hash_entry *eh;
7487
7488 eh = (struct ppc_link_hash_entry *) h;
7489 *tls_maskp = &eh->tls_mask;
7490 }
7491 }
7492 else
7493 {
7494 Elf_Internal_Sym *sym;
7495 Elf_Internal_Sym *locsyms = *locsymsp;
7496
7497 if (locsyms == NULL)
7498 {
7499 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7500 if (locsyms == NULL)
7501 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7502 symtab_hdr->sh_info,
7503 0, NULL, NULL, NULL);
7504 if (locsyms == NULL)
7505 return FALSE;
7506 *locsymsp = locsyms;
7507 }
7508 sym = locsyms + r_symndx;
7509
7510 if (hp != NULL)
7511 *hp = NULL;
7512
7513 if (symp != NULL)
7514 *symp = sym;
7515
7516 if (symsecp != NULL)
7517 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7518
7519 if (tls_maskp != NULL)
7520 {
7521 struct got_entry **lgot_ents;
7522 unsigned char *tls_mask;
7523
7524 tls_mask = NULL;
7525 lgot_ents = elf_local_got_ents (ibfd);
7526 if (lgot_ents != NULL)
7527 {
7528 struct plt_entry **local_plt = (struct plt_entry **)
7529 (lgot_ents + symtab_hdr->sh_info);
7530 unsigned char *lgot_masks = (unsigned char *)
7531 (local_plt + symtab_hdr->sh_info);
7532 tls_mask = &lgot_masks[r_symndx];
7533 }
7534 *tls_maskp = tls_mask;
7535 }
7536 }
7537 return TRUE;
7538 }
7539
7540 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7541 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7542 type suitable for optimization, and 1 otherwise. */
7543
7544 static int
7545 get_tls_mask (unsigned char **tls_maskp,
7546 unsigned long *toc_symndx,
7547 bfd_vma *toc_addend,
7548 Elf_Internal_Sym **locsymsp,
7549 const Elf_Internal_Rela *rel,
7550 bfd *ibfd)
7551 {
7552 unsigned long r_symndx;
7553 int next_r;
7554 struct elf_link_hash_entry *h;
7555 Elf_Internal_Sym *sym;
7556 asection *sec;
7557 bfd_vma off;
7558
7559 r_symndx = ELF64_R_SYM (rel->r_info);
7560 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7561 return 0;
7562
7563 if ((*tls_maskp != NULL && **tls_maskp != 0)
7564 || sec == NULL
7565 || ppc64_elf_section_data (sec) == NULL
7566 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7567 return 1;
7568
7569 /* Look inside a TOC section too. */
7570 if (h != NULL)
7571 {
7572 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7573 off = h->root.u.def.value;
7574 }
7575 else
7576 off = sym->st_value;
7577 off += rel->r_addend;
7578 BFD_ASSERT (off % 8 == 0);
7579 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7580 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7581 if (toc_symndx != NULL)
7582 *toc_symndx = r_symndx;
7583 if (toc_addend != NULL)
7584 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7585 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7586 return 0;
7587 if ((h == NULL || is_static_defined (h))
7588 && (next_r == -1 || next_r == -2))
7589 return 1 - next_r;
7590 return 1;
7591 }
7592
7593 /* Find (or create) an entry in the tocsave hash table. */
7594
7595 static struct tocsave_entry *
7596 tocsave_find (struct ppc_link_hash_table *htab,
7597 enum insert_option insert,
7598 Elf_Internal_Sym **local_syms,
7599 const Elf_Internal_Rela *irela,
7600 bfd *ibfd)
7601 {
7602 unsigned long r_indx;
7603 struct elf_link_hash_entry *h;
7604 Elf_Internal_Sym *sym;
7605 struct tocsave_entry ent, *p;
7606 hashval_t hash;
7607 struct tocsave_entry **slot;
7608
7609 r_indx = ELF64_R_SYM (irela->r_info);
7610 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7611 return NULL;
7612 if (ent.sec == NULL || ent.sec->output_section == NULL)
7613 {
7614 _bfd_error_handler
7615 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7616 return NULL;
7617 }
7618
7619 if (h != NULL)
7620 ent.offset = h->root.u.def.value;
7621 else
7622 ent.offset = sym->st_value;
7623 ent.offset += irela->r_addend;
7624
7625 hash = tocsave_htab_hash (&ent);
7626 slot = ((struct tocsave_entry **)
7627 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7628 if (slot == NULL)
7629 return NULL;
7630
7631 if (*slot == NULL)
7632 {
7633 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7634 if (p == NULL)
7635 return NULL;
7636 *p = ent;
7637 *slot = p;
7638 }
7639 return *slot;
7640 }
7641
7642 /* Adjust all global syms defined in opd sections. In gcc generated
7643 code for the old ABI, these will already have been done. */
7644
7645 static bfd_boolean
7646 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7647 {
7648 struct ppc_link_hash_entry *eh;
7649 asection *sym_sec;
7650 struct _opd_sec_data *opd;
7651
7652 if (h->root.type == bfd_link_hash_indirect)
7653 return TRUE;
7654
7655 if (h->root.type != bfd_link_hash_defined
7656 && h->root.type != bfd_link_hash_defweak)
7657 return TRUE;
7658
7659 eh = (struct ppc_link_hash_entry *) h;
7660 if (eh->adjust_done)
7661 return TRUE;
7662
7663 sym_sec = eh->elf.root.u.def.section;
7664 opd = get_opd_info (sym_sec);
7665 if (opd != NULL && opd->adjust != NULL)
7666 {
7667 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7668 if (adjust == -1)
7669 {
7670 /* This entry has been deleted. */
7671 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7672 if (dsec == NULL)
7673 {
7674 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7675 if (discarded_section (dsec))
7676 {
7677 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7678 break;
7679 }
7680 }
7681 eh->elf.root.u.def.value = 0;
7682 eh->elf.root.u.def.section = dsec;
7683 }
7684 else
7685 eh->elf.root.u.def.value += adjust;
7686 eh->adjust_done = 1;
7687 }
7688 return TRUE;
7689 }
7690
7691 /* Handles decrementing dynamic reloc counts for the reloc specified by
7692 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7693 have already been determined. */
7694
7695 static bfd_boolean
7696 dec_dynrel_count (bfd_vma r_info,
7697 asection *sec,
7698 struct bfd_link_info *info,
7699 Elf_Internal_Sym **local_syms,
7700 struct elf_link_hash_entry *h,
7701 Elf_Internal_Sym *sym)
7702 {
7703 enum elf_ppc64_reloc_type r_type;
7704 asection *sym_sec = NULL;
7705
7706 /* Can this reloc be dynamic? This switch, and later tests here
7707 should be kept in sync with the code in check_relocs. */
7708 r_type = ELF64_R_TYPE (r_info);
7709 switch (r_type)
7710 {
7711 default:
7712 return TRUE;
7713
7714 case R_PPC64_TPREL16:
7715 case R_PPC64_TPREL16_LO:
7716 case R_PPC64_TPREL16_HI:
7717 case R_PPC64_TPREL16_HA:
7718 case R_PPC64_TPREL16_DS:
7719 case R_PPC64_TPREL16_LO_DS:
7720 case R_PPC64_TPREL16_HIGH:
7721 case R_PPC64_TPREL16_HIGHA:
7722 case R_PPC64_TPREL16_HIGHER:
7723 case R_PPC64_TPREL16_HIGHERA:
7724 case R_PPC64_TPREL16_HIGHEST:
7725 case R_PPC64_TPREL16_HIGHESTA:
7726 if (!bfd_link_pic (info))
7727 return TRUE;
7728
7729 case R_PPC64_TPREL64:
7730 case R_PPC64_DTPMOD64:
7731 case R_PPC64_DTPREL64:
7732 case R_PPC64_ADDR64:
7733 case R_PPC64_REL30:
7734 case R_PPC64_REL32:
7735 case R_PPC64_REL64:
7736 case R_PPC64_ADDR14:
7737 case R_PPC64_ADDR14_BRNTAKEN:
7738 case R_PPC64_ADDR14_BRTAKEN:
7739 case R_PPC64_ADDR16:
7740 case R_PPC64_ADDR16_DS:
7741 case R_PPC64_ADDR16_HA:
7742 case R_PPC64_ADDR16_HI:
7743 case R_PPC64_ADDR16_HIGH:
7744 case R_PPC64_ADDR16_HIGHA:
7745 case R_PPC64_ADDR16_HIGHER:
7746 case R_PPC64_ADDR16_HIGHERA:
7747 case R_PPC64_ADDR16_HIGHEST:
7748 case R_PPC64_ADDR16_HIGHESTA:
7749 case R_PPC64_ADDR16_LO:
7750 case R_PPC64_ADDR16_LO_DS:
7751 case R_PPC64_ADDR24:
7752 case R_PPC64_ADDR32:
7753 case R_PPC64_UADDR16:
7754 case R_PPC64_UADDR32:
7755 case R_PPC64_UADDR64:
7756 case R_PPC64_TOC:
7757 break;
7758 }
7759
7760 if (local_syms != NULL)
7761 {
7762 unsigned long r_symndx;
7763 bfd *ibfd = sec->owner;
7764
7765 r_symndx = ELF64_R_SYM (r_info);
7766 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7767 return FALSE;
7768 }
7769
7770 if ((bfd_link_pic (info)
7771 && (must_be_dyn_reloc (info, r_type)
7772 || (h != NULL
7773 && (!SYMBOLIC_BIND (info, h)
7774 || h->root.type == bfd_link_hash_defweak
7775 || !h->def_regular))))
7776 || (ELIMINATE_COPY_RELOCS
7777 && !bfd_link_pic (info)
7778 && h != NULL
7779 && (h->root.type == bfd_link_hash_defweak
7780 || !h->def_regular)))
7781 ;
7782 else
7783 return TRUE;
7784
7785 if (h != NULL)
7786 {
7787 struct elf_dyn_relocs *p;
7788 struct elf_dyn_relocs **pp;
7789 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7790
7791 /* elf_gc_sweep may have already removed all dyn relocs associated
7792 with local syms for a given section. Also, symbol flags are
7793 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7794 report a dynreloc miscount. */
7795 if (*pp == NULL && info->gc_sections)
7796 return TRUE;
7797
7798 while ((p = *pp) != NULL)
7799 {
7800 if (p->sec == sec)
7801 {
7802 if (!must_be_dyn_reloc (info, r_type))
7803 p->pc_count -= 1;
7804 p->count -= 1;
7805 if (p->count == 0)
7806 *pp = p->next;
7807 return TRUE;
7808 }
7809 pp = &p->next;
7810 }
7811 }
7812 else
7813 {
7814 struct ppc_dyn_relocs *p;
7815 struct ppc_dyn_relocs **pp;
7816 void *vpp;
7817 bfd_boolean is_ifunc;
7818
7819 if (local_syms == NULL)
7820 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7821 if (sym_sec == NULL)
7822 sym_sec = sec;
7823
7824 vpp = &elf_section_data (sym_sec)->local_dynrel;
7825 pp = (struct ppc_dyn_relocs **) vpp;
7826
7827 if (*pp == NULL && info->gc_sections)
7828 return TRUE;
7829
7830 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7831 while ((p = *pp) != NULL)
7832 {
7833 if (p->sec == sec && p->ifunc == is_ifunc)
7834 {
7835 p->count -= 1;
7836 if (p->count == 0)
7837 *pp = p->next;
7838 return TRUE;
7839 }
7840 pp = &p->next;
7841 }
7842 }
7843
7844 /* xgettext:c-format */
7845 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7846 sec->owner, sec);
7847 bfd_set_error (bfd_error_bad_value);
7848 return FALSE;
7849 }
7850
7851 /* Remove unused Official Procedure Descriptor entries. Currently we
7852 only remove those associated with functions in discarded link-once
7853 sections, or weakly defined functions that have been overridden. It
7854 would be possible to remove many more entries for statically linked
7855 applications. */
7856
7857 bfd_boolean
7858 ppc64_elf_edit_opd (struct bfd_link_info *info)
7859 {
7860 bfd *ibfd;
7861 bfd_boolean some_edited = FALSE;
7862 asection *need_pad = NULL;
7863 struct ppc_link_hash_table *htab;
7864
7865 htab = ppc_hash_table (info);
7866 if (htab == NULL)
7867 return FALSE;
7868
7869 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7870 {
7871 asection *sec;
7872 Elf_Internal_Rela *relstart, *rel, *relend;
7873 Elf_Internal_Shdr *symtab_hdr;
7874 Elf_Internal_Sym *local_syms;
7875 struct _opd_sec_data *opd;
7876 bfd_boolean need_edit, add_aux_fields, broken;
7877 bfd_size_type cnt_16b = 0;
7878
7879 if (!is_ppc64_elf (ibfd))
7880 continue;
7881
7882 sec = bfd_get_section_by_name (ibfd, ".opd");
7883 if (sec == NULL || sec->size == 0)
7884 continue;
7885
7886 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7887 continue;
7888
7889 if (sec->output_section == bfd_abs_section_ptr)
7890 continue;
7891
7892 /* Look through the section relocs. */
7893 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7894 continue;
7895
7896 local_syms = NULL;
7897 symtab_hdr = &elf_symtab_hdr (ibfd);
7898
7899 /* Read the relocations. */
7900 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7901 info->keep_memory);
7902 if (relstart == NULL)
7903 return FALSE;
7904
7905 /* First run through the relocs to check they are sane, and to
7906 determine whether we need to edit this opd section. */
7907 need_edit = FALSE;
7908 broken = FALSE;
7909 need_pad = sec;
7910 relend = relstart + sec->reloc_count;
7911 for (rel = relstart; rel < relend; )
7912 {
7913 enum elf_ppc64_reloc_type r_type;
7914 unsigned long r_symndx;
7915 asection *sym_sec;
7916 struct elf_link_hash_entry *h;
7917 Elf_Internal_Sym *sym;
7918 bfd_vma offset;
7919
7920 /* .opd contains an array of 16 or 24 byte entries. We're
7921 only interested in the reloc pointing to a function entry
7922 point. */
7923 offset = rel->r_offset;
7924 if (rel + 1 == relend
7925 || rel[1].r_offset != offset + 8)
7926 {
7927 /* If someone messes with .opd alignment then after a
7928 "ld -r" we might have padding in the middle of .opd.
7929 Also, there's nothing to prevent someone putting
7930 something silly in .opd with the assembler. No .opd
7931 optimization for them! */
7932 broken_opd:
7933 _bfd_error_handler
7934 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7935 broken = TRUE;
7936 break;
7937 }
7938
7939 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7940 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7941 {
7942 _bfd_error_handler
7943 /* xgettext:c-format */
7944 (_("%B: unexpected reloc type %u in .opd section"),
7945 ibfd, r_type);
7946 broken = TRUE;
7947 break;
7948 }
7949
7950 r_symndx = ELF64_R_SYM (rel->r_info);
7951 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7952 r_symndx, ibfd))
7953 goto error_ret;
7954
7955 if (sym_sec == NULL || sym_sec->owner == NULL)
7956 {
7957 const char *sym_name;
7958 if (h != NULL)
7959 sym_name = h->root.root.string;
7960 else
7961 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7962 sym_sec);
7963
7964 _bfd_error_handler
7965 /* xgettext:c-format */
7966 (_("%B: undefined sym `%s' in .opd section"),
7967 ibfd, sym_name);
7968 broken = TRUE;
7969 break;
7970 }
7971
7972 /* opd entries are always for functions defined in the
7973 current input bfd. If the symbol isn't defined in the
7974 input bfd, then we won't be using the function in this
7975 bfd; It must be defined in a linkonce section in another
7976 bfd, or is weak. It's also possible that we are
7977 discarding the function due to a linker script /DISCARD/,
7978 which we test for via the output_section. */
7979 if (sym_sec->owner != ibfd
7980 || sym_sec->output_section == bfd_abs_section_ptr)
7981 need_edit = TRUE;
7982
7983 rel += 2;
7984 if (rel + 1 == relend
7985 || (rel + 2 < relend
7986 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7987 ++rel;
7988
7989 if (rel == relend)
7990 {
7991 if (sec->size == offset + 24)
7992 {
7993 need_pad = NULL;
7994 break;
7995 }
7996 if (sec->size == offset + 16)
7997 {
7998 cnt_16b++;
7999 break;
8000 }
8001 goto broken_opd;
8002 }
8003 else if (rel + 1 < relend
8004 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8005 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8006 {
8007 if (rel[0].r_offset == offset + 16)
8008 cnt_16b++;
8009 else if (rel[0].r_offset != offset + 24)
8010 goto broken_opd;
8011 }
8012 else
8013 goto broken_opd;
8014 }
8015
8016 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8017
8018 if (!broken && (need_edit || add_aux_fields))
8019 {
8020 Elf_Internal_Rela *write_rel;
8021 Elf_Internal_Shdr *rel_hdr;
8022 bfd_byte *rptr, *wptr;
8023 bfd_byte *new_contents;
8024 bfd_size_type amt;
8025
8026 new_contents = NULL;
8027 amt = OPD_NDX (sec->size) * sizeof (long);
8028 opd = &ppc64_elf_section_data (sec)->u.opd;
8029 opd->adjust = bfd_zalloc (sec->owner, amt);
8030 if (opd->adjust == NULL)
8031 return FALSE;
8032 ppc64_elf_section_data (sec)->sec_type = sec_opd;
8033
8034 /* This seems a waste of time as input .opd sections are all
8035 zeros as generated by gcc, but I suppose there's no reason
8036 this will always be so. We might start putting something in
8037 the third word of .opd entries. */
8038 if ((sec->flags & SEC_IN_MEMORY) == 0)
8039 {
8040 bfd_byte *loc;
8041 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8042 {
8043 if (loc != NULL)
8044 free (loc);
8045 error_ret:
8046 if (local_syms != NULL
8047 && symtab_hdr->contents != (unsigned char *) local_syms)
8048 free (local_syms);
8049 if (elf_section_data (sec)->relocs != relstart)
8050 free (relstart);
8051 return FALSE;
8052 }
8053 sec->contents = loc;
8054 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8055 }
8056
8057 elf_section_data (sec)->relocs = relstart;
8058
8059 new_contents = sec->contents;
8060 if (add_aux_fields)
8061 {
8062 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8063 if (new_contents == NULL)
8064 return FALSE;
8065 need_pad = NULL;
8066 }
8067 wptr = new_contents;
8068 rptr = sec->contents;
8069 write_rel = relstart;
8070 for (rel = relstart; rel < relend; )
8071 {
8072 unsigned long r_symndx;
8073 asection *sym_sec;
8074 struct elf_link_hash_entry *h;
8075 struct ppc_link_hash_entry *fdh = NULL;
8076 Elf_Internal_Sym *sym;
8077 long opd_ent_size;
8078 Elf_Internal_Rela *next_rel;
8079 bfd_boolean skip;
8080
8081 r_symndx = ELF64_R_SYM (rel->r_info);
8082 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8083 r_symndx, ibfd))
8084 goto error_ret;
8085
8086 next_rel = rel + 2;
8087 if (next_rel + 1 == relend
8088 || (next_rel + 2 < relend
8089 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8090 ++next_rel;
8091
8092 /* See if the .opd entry is full 24 byte or
8093 16 byte (with fd_aux entry overlapped with next
8094 fd_func). */
8095 opd_ent_size = 24;
8096 if (next_rel == relend)
8097 {
8098 if (sec->size == rel->r_offset + 16)
8099 opd_ent_size = 16;
8100 }
8101 else if (next_rel->r_offset == rel->r_offset + 16)
8102 opd_ent_size = 16;
8103
8104 if (h != NULL
8105 && h->root.root.string[0] == '.')
8106 {
8107 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h, htab);
8108 if (fdh != NULL
8109 && fdh->elf.root.type != bfd_link_hash_defined
8110 && fdh->elf.root.type != bfd_link_hash_defweak)
8111 fdh = NULL;
8112 }
8113
8114 skip = (sym_sec->owner != ibfd
8115 || sym_sec->output_section == bfd_abs_section_ptr);
8116 if (skip)
8117 {
8118 if (fdh != NULL && sym_sec->owner == ibfd)
8119 {
8120 /* Arrange for the function descriptor sym
8121 to be dropped. */
8122 fdh->elf.root.u.def.value = 0;
8123 fdh->elf.root.u.def.section = sym_sec;
8124 }
8125 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8126
8127 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8128 rel = next_rel;
8129 else
8130 while (1)
8131 {
8132 if (!dec_dynrel_count (rel->r_info, sec, info,
8133 NULL, h, sym))
8134 goto error_ret;
8135
8136 if (++rel == next_rel)
8137 break;
8138
8139 r_symndx = ELF64_R_SYM (rel->r_info);
8140 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8141 r_symndx, ibfd))
8142 goto error_ret;
8143 }
8144 }
8145 else
8146 {
8147 /* We'll be keeping this opd entry. */
8148 long adjust;
8149
8150 if (fdh != NULL)
8151 {
8152 /* Redefine the function descriptor symbol to
8153 this location in the opd section. It is
8154 necessary to update the value here rather
8155 than using an array of adjustments as we do
8156 for local symbols, because various places
8157 in the generic ELF code use the value
8158 stored in u.def.value. */
8159 fdh->elf.root.u.def.value = wptr - new_contents;
8160 fdh->adjust_done = 1;
8161 }
8162
8163 /* Local syms are a bit tricky. We could
8164 tweak them as they can be cached, but
8165 we'd need to look through the local syms
8166 for the function descriptor sym which we
8167 don't have at the moment. So keep an
8168 array of adjustments. */
8169 adjust = (wptr - new_contents) - (rptr - sec->contents);
8170 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8171
8172 if (wptr != rptr)
8173 memcpy (wptr, rptr, opd_ent_size);
8174 wptr += opd_ent_size;
8175 if (add_aux_fields && opd_ent_size == 16)
8176 {
8177 memset (wptr, '\0', 8);
8178 wptr += 8;
8179 }
8180
8181 /* We need to adjust any reloc offsets to point to the
8182 new opd entries. */
8183 for ( ; rel != next_rel; ++rel)
8184 {
8185 rel->r_offset += adjust;
8186 if (write_rel != rel)
8187 memcpy (write_rel, rel, sizeof (*rel));
8188 ++write_rel;
8189 }
8190 }
8191
8192 rptr += opd_ent_size;
8193 }
8194
8195 sec->size = wptr - new_contents;
8196 sec->reloc_count = write_rel - relstart;
8197 if (add_aux_fields)
8198 {
8199 free (sec->contents);
8200 sec->contents = new_contents;
8201 }
8202
8203 /* Fudge the header size too, as this is used later in
8204 elf_bfd_final_link if we are emitting relocs. */
8205 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8206 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8207 some_edited = TRUE;
8208 }
8209 else if (elf_section_data (sec)->relocs != relstart)
8210 free (relstart);
8211
8212 if (local_syms != NULL
8213 && symtab_hdr->contents != (unsigned char *) local_syms)
8214 {
8215 if (!info->keep_memory)
8216 free (local_syms);
8217 else
8218 symtab_hdr->contents = (unsigned char *) local_syms;
8219 }
8220 }
8221
8222 if (some_edited)
8223 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8224
8225 /* If we are doing a final link and the last .opd entry is just 16 byte
8226 long, add a 8 byte padding after it. */
8227 if (need_pad != NULL && !bfd_link_relocatable (info))
8228 {
8229 bfd_byte *p;
8230
8231 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8232 {
8233 BFD_ASSERT (need_pad->size > 0);
8234
8235 p = bfd_malloc (need_pad->size + 8);
8236 if (p == NULL)
8237 return FALSE;
8238
8239 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8240 p, 0, need_pad->size))
8241 return FALSE;
8242
8243 need_pad->contents = p;
8244 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8245 }
8246 else
8247 {
8248 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8249 if (p == NULL)
8250 return FALSE;
8251
8252 need_pad->contents = p;
8253 }
8254
8255 memset (need_pad->contents + need_pad->size, 0, 8);
8256 need_pad->size += 8;
8257 }
8258
8259 return TRUE;
8260 }
8261
8262 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8263
8264 asection *
8265 ppc64_elf_tls_setup (struct bfd_link_info *info)
8266 {
8267 struct ppc_link_hash_table *htab;
8268
8269 htab = ppc_hash_table (info);
8270 if (htab == NULL)
8271 return NULL;
8272
8273 if (abiversion (info->output_bfd) == 1)
8274 htab->opd_abi = 1;
8275
8276 if (htab->params->no_multi_toc)
8277 htab->do_multi_toc = 0;
8278 else if (!htab->do_multi_toc)
8279 htab->params->no_multi_toc = 1;
8280
8281 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8282 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8283 FALSE, FALSE, TRUE));
8284 /* Move dynamic linking info to the function descriptor sym. */
8285 if (htab->tls_get_addr != NULL)
8286 func_desc_adjust (&htab->tls_get_addr->elf, info);
8287 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8288 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8289 FALSE, FALSE, TRUE));
8290 if (htab->params->tls_get_addr_opt)
8291 {
8292 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8293
8294 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8295 FALSE, FALSE, TRUE);
8296 if (opt != NULL)
8297 func_desc_adjust (opt, info);
8298 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8299 FALSE, FALSE, TRUE);
8300 if (opt_fd != NULL
8301 && (opt_fd->root.type == bfd_link_hash_defined
8302 || opt_fd->root.type == bfd_link_hash_defweak))
8303 {
8304 /* If glibc supports an optimized __tls_get_addr call stub,
8305 signalled by the presence of __tls_get_addr_opt, and we'll
8306 be calling __tls_get_addr via a plt call stub, then
8307 make __tls_get_addr point to __tls_get_addr_opt. */
8308 tga_fd = &htab->tls_get_addr_fd->elf;
8309 if (htab->elf.dynamic_sections_created
8310 && tga_fd != NULL
8311 && (tga_fd->type == STT_FUNC
8312 || tga_fd->needs_plt)
8313 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8314 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8315 && tga_fd->root.type == bfd_link_hash_undefweak)))
8316 {
8317 struct plt_entry *ent;
8318
8319 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8320 if (ent->plt.refcount > 0)
8321 break;
8322 if (ent != NULL)
8323 {
8324 tga_fd->root.type = bfd_link_hash_indirect;
8325 tga_fd->root.u.i.link = &opt_fd->root;
8326 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8327 opt_fd->forced_local = 0;
8328 if (opt_fd->dynindx != -1)
8329 {
8330 /* Use __tls_get_addr_opt in dynamic relocations. */
8331 opt_fd->dynindx = -1;
8332 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8333 opt_fd->dynstr_index);
8334 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8335 return NULL;
8336 }
8337 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8338 tga = &htab->tls_get_addr->elf;
8339 if (opt != NULL && tga != NULL)
8340 {
8341 tga->root.type = bfd_link_hash_indirect;
8342 tga->root.u.i.link = &opt->root;
8343 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8344 opt->forced_local = 0;
8345 _bfd_elf_link_hash_hide_symbol (info, opt,
8346 tga->forced_local);
8347 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8348 }
8349 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8350 htab->tls_get_addr_fd->is_func_descriptor = 1;
8351 if (htab->tls_get_addr != NULL)
8352 {
8353 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8354 htab->tls_get_addr->is_func = 1;
8355 }
8356 }
8357 }
8358 }
8359 else if (htab->params->tls_get_addr_opt < 0)
8360 htab->params->tls_get_addr_opt = 0;
8361 }
8362 return _bfd_elf_tls_setup (info->output_bfd, info);
8363 }
8364
8365 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8366 HASH1 or HASH2. */
8367
8368 static bfd_boolean
8369 branch_reloc_hash_match (const bfd *ibfd,
8370 const Elf_Internal_Rela *rel,
8371 const struct ppc_link_hash_entry *hash1,
8372 const struct ppc_link_hash_entry *hash2)
8373 {
8374 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8375 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8376 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8377
8378 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8379 {
8380 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8381 struct elf_link_hash_entry *h;
8382
8383 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8384 h = elf_follow_link (h);
8385 if (h == &hash1->elf || h == &hash2->elf)
8386 return TRUE;
8387 }
8388 return FALSE;
8389 }
8390
8391 /* Run through all the TLS relocs looking for optimization
8392 opportunities. The linker has been hacked (see ppc64elf.em) to do
8393 a preliminary section layout so that we know the TLS segment
8394 offsets. We can't optimize earlier because some optimizations need
8395 to know the tp offset, and we need to optimize before allocating
8396 dynamic relocations. */
8397
8398 bfd_boolean
8399 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8400 {
8401 bfd *ibfd;
8402 asection *sec;
8403 struct ppc_link_hash_table *htab;
8404 unsigned char *toc_ref;
8405 int pass;
8406
8407 if (!bfd_link_executable (info))
8408 return TRUE;
8409
8410 htab = ppc_hash_table (info);
8411 if (htab == NULL)
8412 return FALSE;
8413
8414 /* Make two passes over the relocs. On the first pass, mark toc
8415 entries involved with tls relocs, and check that tls relocs
8416 involved in setting up a tls_get_addr call are indeed followed by
8417 such a call. If they are not, we can't do any tls optimization.
8418 On the second pass twiddle tls_mask flags to notify
8419 relocate_section that optimization can be done, and adjust got
8420 and plt refcounts. */
8421 toc_ref = NULL;
8422 for (pass = 0; pass < 2; ++pass)
8423 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8424 {
8425 Elf_Internal_Sym *locsyms = NULL;
8426 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8427
8428 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8429 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8430 {
8431 Elf_Internal_Rela *relstart, *rel, *relend;
8432 bfd_boolean found_tls_get_addr_arg = 0;
8433
8434 /* Read the relocations. */
8435 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8436 info->keep_memory);
8437 if (relstart == NULL)
8438 {
8439 free (toc_ref);
8440 return FALSE;
8441 }
8442
8443 relend = relstart + sec->reloc_count;
8444 for (rel = relstart; rel < relend; rel++)
8445 {
8446 enum elf_ppc64_reloc_type r_type;
8447 unsigned long r_symndx;
8448 struct elf_link_hash_entry *h;
8449 Elf_Internal_Sym *sym;
8450 asection *sym_sec;
8451 unsigned char *tls_mask;
8452 unsigned char tls_set, tls_clear, tls_type = 0;
8453 bfd_vma value;
8454 bfd_boolean ok_tprel, is_local;
8455 long toc_ref_index = 0;
8456 int expecting_tls_get_addr = 0;
8457 bfd_boolean ret = FALSE;
8458
8459 r_symndx = ELF64_R_SYM (rel->r_info);
8460 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8461 r_symndx, ibfd))
8462 {
8463 err_free_rel:
8464 if (elf_section_data (sec)->relocs != relstart)
8465 free (relstart);
8466 if (toc_ref != NULL)
8467 free (toc_ref);
8468 if (locsyms != NULL
8469 && (elf_symtab_hdr (ibfd).contents
8470 != (unsigned char *) locsyms))
8471 free (locsyms);
8472 return ret;
8473 }
8474
8475 if (h != NULL)
8476 {
8477 if (h->root.type == bfd_link_hash_defined
8478 || h->root.type == bfd_link_hash_defweak)
8479 value = h->root.u.def.value;
8480 else if (h->root.type == bfd_link_hash_undefweak)
8481 value = 0;
8482 else
8483 {
8484 found_tls_get_addr_arg = 0;
8485 continue;
8486 }
8487 }
8488 else
8489 /* Symbols referenced by TLS relocs must be of type
8490 STT_TLS. So no need for .opd local sym adjust. */
8491 value = sym->st_value;
8492
8493 ok_tprel = FALSE;
8494 is_local = FALSE;
8495 if (h == NULL
8496 || !h->def_dynamic)
8497 {
8498 is_local = TRUE;
8499 if (h != NULL
8500 && h->root.type == bfd_link_hash_undefweak)
8501 ok_tprel = TRUE;
8502 else if (sym_sec != NULL
8503 && sym_sec->output_section != NULL)
8504 {
8505 value += sym_sec->output_offset;
8506 value += sym_sec->output_section->vma;
8507 value -= htab->elf.tls_sec->vma;
8508 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8509 < (bfd_vma) 1 << 32);
8510 }
8511 }
8512
8513 r_type = ELF64_R_TYPE (rel->r_info);
8514 /* If this section has old-style __tls_get_addr calls
8515 without marker relocs, then check that each
8516 __tls_get_addr call reloc is preceded by a reloc
8517 that conceivably belongs to the __tls_get_addr arg
8518 setup insn. If we don't find matching arg setup
8519 relocs, don't do any tls optimization. */
8520 if (pass == 0
8521 && sec->has_tls_get_addr_call
8522 && h != NULL
8523 && (h == &htab->tls_get_addr->elf
8524 || h == &htab->tls_get_addr_fd->elf)
8525 && !found_tls_get_addr_arg
8526 && is_branch_reloc (r_type))
8527 {
8528 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8529 "TLS optimization disabled\n"),
8530 ibfd, sec, rel->r_offset);
8531 ret = TRUE;
8532 goto err_free_rel;
8533 }
8534
8535 found_tls_get_addr_arg = 0;
8536 switch (r_type)
8537 {
8538 case R_PPC64_GOT_TLSLD16:
8539 case R_PPC64_GOT_TLSLD16_LO:
8540 expecting_tls_get_addr = 1;
8541 found_tls_get_addr_arg = 1;
8542 /* Fall through. */
8543
8544 case R_PPC64_GOT_TLSLD16_HI:
8545 case R_PPC64_GOT_TLSLD16_HA:
8546 /* These relocs should never be against a symbol
8547 defined in a shared lib. Leave them alone if
8548 that turns out to be the case. */
8549 if (!is_local)
8550 continue;
8551
8552 /* LD -> LE */
8553 tls_set = 0;
8554 tls_clear = TLS_LD;
8555 tls_type = TLS_TLS | TLS_LD;
8556 break;
8557
8558 case R_PPC64_GOT_TLSGD16:
8559 case R_PPC64_GOT_TLSGD16_LO:
8560 expecting_tls_get_addr = 1;
8561 found_tls_get_addr_arg = 1;
8562 /* Fall through. */
8563
8564 case R_PPC64_GOT_TLSGD16_HI:
8565 case R_PPC64_GOT_TLSGD16_HA:
8566 if (ok_tprel)
8567 /* GD -> LE */
8568 tls_set = 0;
8569 else
8570 /* GD -> IE */
8571 tls_set = TLS_TLS | TLS_TPRELGD;
8572 tls_clear = TLS_GD;
8573 tls_type = TLS_TLS | TLS_GD;
8574 break;
8575
8576 case R_PPC64_GOT_TPREL16_DS:
8577 case R_PPC64_GOT_TPREL16_LO_DS:
8578 case R_PPC64_GOT_TPREL16_HI:
8579 case R_PPC64_GOT_TPREL16_HA:
8580 if (ok_tprel)
8581 {
8582 /* IE -> LE */
8583 tls_set = 0;
8584 tls_clear = TLS_TPREL;
8585 tls_type = TLS_TLS | TLS_TPREL;
8586 break;
8587 }
8588 continue;
8589
8590 case R_PPC64_TLSGD:
8591 case R_PPC64_TLSLD:
8592 found_tls_get_addr_arg = 1;
8593 /* Fall through. */
8594
8595 case R_PPC64_TLS:
8596 case R_PPC64_TOC16:
8597 case R_PPC64_TOC16_LO:
8598 if (sym_sec == NULL || sym_sec != toc)
8599 continue;
8600
8601 /* Mark this toc entry as referenced by a TLS
8602 code sequence. We can do that now in the
8603 case of R_PPC64_TLS, and after checking for
8604 tls_get_addr for the TOC16 relocs. */
8605 if (toc_ref == NULL)
8606 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8607 if (toc_ref == NULL)
8608 goto err_free_rel;
8609
8610 if (h != NULL)
8611 value = h->root.u.def.value;
8612 else
8613 value = sym->st_value;
8614 value += rel->r_addend;
8615 if (value % 8 != 0)
8616 continue;
8617 BFD_ASSERT (value < toc->size
8618 && toc->output_offset % 8 == 0);
8619 toc_ref_index = (value + toc->output_offset) / 8;
8620 if (r_type == R_PPC64_TLS
8621 || r_type == R_PPC64_TLSGD
8622 || r_type == R_PPC64_TLSLD)
8623 {
8624 toc_ref[toc_ref_index] = 1;
8625 continue;
8626 }
8627
8628 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8629 continue;
8630
8631 tls_set = 0;
8632 tls_clear = 0;
8633 expecting_tls_get_addr = 2;
8634 break;
8635
8636 case R_PPC64_TPREL64:
8637 if (pass == 0
8638 || sec != toc
8639 || toc_ref == NULL
8640 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8641 continue;
8642 if (ok_tprel)
8643 {
8644 /* IE -> LE */
8645 tls_set = TLS_EXPLICIT;
8646 tls_clear = TLS_TPREL;
8647 break;
8648 }
8649 continue;
8650
8651 case R_PPC64_DTPMOD64:
8652 if (pass == 0
8653 || sec != toc
8654 || toc_ref == NULL
8655 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8656 continue;
8657 if (rel + 1 < relend
8658 && (rel[1].r_info
8659 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8660 && rel[1].r_offset == rel->r_offset + 8)
8661 {
8662 if (ok_tprel)
8663 /* GD -> LE */
8664 tls_set = TLS_EXPLICIT | TLS_GD;
8665 else
8666 /* GD -> IE */
8667 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8668 tls_clear = TLS_GD;
8669 }
8670 else
8671 {
8672 if (!is_local)
8673 continue;
8674
8675 /* LD -> LE */
8676 tls_set = TLS_EXPLICIT;
8677 tls_clear = TLS_LD;
8678 }
8679 break;
8680
8681 default:
8682 continue;
8683 }
8684
8685 if (pass == 0)
8686 {
8687 if (!expecting_tls_get_addr
8688 || !sec->has_tls_get_addr_call)
8689 continue;
8690
8691 if (rel + 1 < relend
8692 && branch_reloc_hash_match (ibfd, rel + 1,
8693 htab->tls_get_addr,
8694 htab->tls_get_addr_fd))
8695 {
8696 if (expecting_tls_get_addr == 2)
8697 {
8698 /* Check for toc tls entries. */
8699 unsigned char *toc_tls;
8700 int retval;
8701
8702 retval = get_tls_mask (&toc_tls, NULL, NULL,
8703 &locsyms,
8704 rel, ibfd);
8705 if (retval == 0)
8706 goto err_free_rel;
8707 if (toc_tls != NULL)
8708 {
8709 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8710 found_tls_get_addr_arg = 1;
8711 if (retval > 1)
8712 toc_ref[toc_ref_index] = 1;
8713 }
8714 }
8715 continue;
8716 }
8717
8718 if (expecting_tls_get_addr != 1)
8719 continue;
8720
8721 /* Uh oh, we didn't find the expected call. We
8722 could just mark this symbol to exclude it
8723 from tls optimization but it's safer to skip
8724 the entire optimization. */
8725 /* xgettext:c-format */
8726 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8727 "TLS optimization disabled\n"),
8728 ibfd, sec, rel->r_offset);
8729 ret = TRUE;
8730 goto err_free_rel;
8731 }
8732
8733 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8734 {
8735 struct plt_entry *ent;
8736 for (ent = htab->tls_get_addr->elf.plt.plist;
8737 ent != NULL;
8738 ent = ent->next)
8739 if (ent->addend == 0)
8740 {
8741 if (ent->plt.refcount > 0)
8742 {
8743 ent->plt.refcount -= 1;
8744 expecting_tls_get_addr = 0;
8745 }
8746 break;
8747 }
8748 }
8749
8750 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8751 {
8752 struct plt_entry *ent;
8753 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8754 ent != NULL;
8755 ent = ent->next)
8756 if (ent->addend == 0)
8757 {
8758 if (ent->plt.refcount > 0)
8759 ent->plt.refcount -= 1;
8760 break;
8761 }
8762 }
8763
8764 if (tls_clear == 0)
8765 continue;
8766
8767 if ((tls_set & TLS_EXPLICIT) == 0)
8768 {
8769 struct got_entry *ent;
8770
8771 /* Adjust got entry for this reloc. */
8772 if (h != NULL)
8773 ent = h->got.glist;
8774 else
8775 ent = elf_local_got_ents (ibfd)[r_symndx];
8776
8777 for (; ent != NULL; ent = ent->next)
8778 if (ent->addend == rel->r_addend
8779 && ent->owner == ibfd
8780 && ent->tls_type == tls_type)
8781 break;
8782 if (ent == NULL)
8783 abort ();
8784
8785 if (tls_set == 0)
8786 {
8787 /* We managed to get rid of a got entry. */
8788 if (ent->got.refcount > 0)
8789 ent->got.refcount -= 1;
8790 }
8791 }
8792 else
8793 {
8794 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8795 we'll lose one or two dyn relocs. */
8796 if (!dec_dynrel_count (rel->r_info, sec, info,
8797 NULL, h, sym))
8798 return FALSE;
8799
8800 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8801 {
8802 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8803 NULL, h, sym))
8804 return FALSE;
8805 }
8806 }
8807
8808 *tls_mask |= tls_set;
8809 *tls_mask &= ~tls_clear;
8810 }
8811
8812 if (elf_section_data (sec)->relocs != relstart)
8813 free (relstart);
8814 }
8815
8816 if (locsyms != NULL
8817 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8818 {
8819 if (!info->keep_memory)
8820 free (locsyms);
8821 else
8822 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8823 }
8824 }
8825
8826 if (toc_ref != NULL)
8827 free (toc_ref);
8828 return TRUE;
8829 }
8830
8831 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8832 the values of any global symbols in a toc section that has been
8833 edited. Globals in toc sections should be a rarity, so this function
8834 sets a flag if any are found in toc sections other than the one just
8835 edited, so that futher hash table traversals can be avoided. */
8836
8837 struct adjust_toc_info
8838 {
8839 asection *toc;
8840 unsigned long *skip;
8841 bfd_boolean global_toc_syms;
8842 };
8843
8844 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8845
8846 static bfd_boolean
8847 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8848 {
8849 struct ppc_link_hash_entry *eh;
8850 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8851 unsigned long i;
8852
8853 if (h->root.type != bfd_link_hash_defined
8854 && h->root.type != bfd_link_hash_defweak)
8855 return TRUE;
8856
8857 eh = (struct ppc_link_hash_entry *) h;
8858 if (eh->adjust_done)
8859 return TRUE;
8860
8861 if (eh->elf.root.u.def.section == toc_inf->toc)
8862 {
8863 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8864 i = toc_inf->toc->rawsize >> 3;
8865 else
8866 i = eh->elf.root.u.def.value >> 3;
8867
8868 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8869 {
8870 _bfd_error_handler
8871 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8872 do
8873 ++i;
8874 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8875 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8876 }
8877
8878 eh->elf.root.u.def.value -= toc_inf->skip[i];
8879 eh->adjust_done = 1;
8880 }
8881 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8882 toc_inf->global_toc_syms = TRUE;
8883
8884 return TRUE;
8885 }
8886
8887 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8888
8889 static bfd_boolean
8890 ok_lo_toc_insn (unsigned int insn)
8891 {
8892 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8893 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8894 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8895 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8896 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8897 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8898 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8899 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8900 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8901 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8902 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8903 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8904 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8905 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8906 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8907 && (insn & 3) != 1)
8908 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8909 && ((insn & 3) == 0 || (insn & 3) == 3))
8910 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8911 }
8912
8913 /* Examine all relocs referencing .toc sections in order to remove
8914 unused .toc entries. */
8915
8916 bfd_boolean
8917 ppc64_elf_edit_toc (struct bfd_link_info *info)
8918 {
8919 bfd *ibfd;
8920 struct adjust_toc_info toc_inf;
8921 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8922
8923 htab->do_toc_opt = 1;
8924 toc_inf.global_toc_syms = TRUE;
8925 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8926 {
8927 asection *toc, *sec;
8928 Elf_Internal_Shdr *symtab_hdr;
8929 Elf_Internal_Sym *local_syms;
8930 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8931 unsigned long *skip, *drop;
8932 unsigned char *used;
8933 unsigned char *keep, last, some_unused;
8934
8935 if (!is_ppc64_elf (ibfd))
8936 continue;
8937
8938 toc = bfd_get_section_by_name (ibfd, ".toc");
8939 if (toc == NULL
8940 || toc->size == 0
8941 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8942 || discarded_section (toc))
8943 continue;
8944
8945 toc_relocs = NULL;
8946 local_syms = NULL;
8947 symtab_hdr = &elf_symtab_hdr (ibfd);
8948
8949 /* Look at sections dropped from the final link. */
8950 skip = NULL;
8951 relstart = NULL;
8952 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8953 {
8954 if (sec->reloc_count == 0
8955 || !discarded_section (sec)
8956 || get_opd_info (sec)
8957 || (sec->flags & SEC_ALLOC) == 0
8958 || (sec->flags & SEC_DEBUGGING) != 0)
8959 continue;
8960
8961 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8962 if (relstart == NULL)
8963 goto error_ret;
8964
8965 /* Run through the relocs to see which toc entries might be
8966 unused. */
8967 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8968 {
8969 enum elf_ppc64_reloc_type r_type;
8970 unsigned long r_symndx;
8971 asection *sym_sec;
8972 struct elf_link_hash_entry *h;
8973 Elf_Internal_Sym *sym;
8974 bfd_vma val;
8975
8976 r_type = ELF64_R_TYPE (rel->r_info);
8977 switch (r_type)
8978 {
8979 default:
8980 continue;
8981
8982 case R_PPC64_TOC16:
8983 case R_PPC64_TOC16_LO:
8984 case R_PPC64_TOC16_HI:
8985 case R_PPC64_TOC16_HA:
8986 case R_PPC64_TOC16_DS:
8987 case R_PPC64_TOC16_LO_DS:
8988 break;
8989 }
8990
8991 r_symndx = ELF64_R_SYM (rel->r_info);
8992 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8993 r_symndx, ibfd))
8994 goto error_ret;
8995
8996 if (sym_sec != toc)
8997 continue;
8998
8999 if (h != NULL)
9000 val = h->root.u.def.value;
9001 else
9002 val = sym->st_value;
9003 val += rel->r_addend;
9004
9005 if (val >= toc->size)
9006 continue;
9007
9008 /* Anything in the toc ought to be aligned to 8 bytes.
9009 If not, don't mark as unused. */
9010 if (val & 7)
9011 continue;
9012
9013 if (skip == NULL)
9014 {
9015 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9016 if (skip == NULL)
9017 goto error_ret;
9018 }
9019
9020 skip[val >> 3] = ref_from_discarded;
9021 }
9022
9023 if (elf_section_data (sec)->relocs != relstart)
9024 free (relstart);
9025 }
9026
9027 /* For largetoc loads of address constants, we can convert
9028 . addis rx,2,addr@got@ha
9029 . ld ry,addr@got@l(rx)
9030 to
9031 . addis rx,2,addr@toc@ha
9032 . addi ry,rx,addr@toc@l
9033 when addr is within 2G of the toc pointer. This then means
9034 that the word storing "addr" in the toc is no longer needed. */
9035
9036 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9037 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9038 && toc->reloc_count != 0)
9039 {
9040 /* Read toc relocs. */
9041 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9042 info->keep_memory);
9043 if (toc_relocs == NULL)
9044 goto error_ret;
9045
9046 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9047 {
9048 enum elf_ppc64_reloc_type r_type;
9049 unsigned long r_symndx;
9050 asection *sym_sec;
9051 struct elf_link_hash_entry *h;
9052 Elf_Internal_Sym *sym;
9053 bfd_vma val, addr;
9054
9055 r_type = ELF64_R_TYPE (rel->r_info);
9056 if (r_type != R_PPC64_ADDR64)
9057 continue;
9058
9059 r_symndx = ELF64_R_SYM (rel->r_info);
9060 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9061 r_symndx, ibfd))
9062 goto error_ret;
9063
9064 if (sym_sec == NULL
9065 || sym_sec->output_section == NULL
9066 || discarded_section (sym_sec))
9067 continue;
9068
9069 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9070 continue;
9071
9072 if (h != NULL)
9073 {
9074 if (h->type == STT_GNU_IFUNC)
9075 continue;
9076 val = h->root.u.def.value;
9077 }
9078 else
9079 {
9080 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9081 continue;
9082 val = sym->st_value;
9083 }
9084 val += rel->r_addend;
9085 val += sym_sec->output_section->vma + sym_sec->output_offset;
9086
9087 /* We don't yet know the exact toc pointer value, but we
9088 know it will be somewhere in the toc section. Don't
9089 optimize if the difference from any possible toc
9090 pointer is outside [ff..f80008000, 7fff7fff]. */
9091 addr = toc->output_section->vma + TOC_BASE_OFF;
9092 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9093 continue;
9094
9095 addr = toc->output_section->vma + toc->output_section->rawsize;
9096 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9097 continue;
9098
9099 if (skip == NULL)
9100 {
9101 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9102 if (skip == NULL)
9103 goto error_ret;
9104 }
9105
9106 skip[rel->r_offset >> 3]
9107 |= can_optimize | ((rel - toc_relocs) << 2);
9108 }
9109 }
9110
9111 if (skip == NULL)
9112 continue;
9113
9114 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9115 if (used == NULL)
9116 {
9117 error_ret:
9118 if (local_syms != NULL
9119 && symtab_hdr->contents != (unsigned char *) local_syms)
9120 free (local_syms);
9121 if (sec != NULL
9122 && relstart != NULL
9123 && elf_section_data (sec)->relocs != relstart)
9124 free (relstart);
9125 if (toc_relocs != NULL
9126 && elf_section_data (toc)->relocs != toc_relocs)
9127 free (toc_relocs);
9128 if (skip != NULL)
9129 free (skip);
9130 return FALSE;
9131 }
9132
9133 /* Now check all kept sections that might reference the toc.
9134 Check the toc itself last. */
9135 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9136 : ibfd->sections);
9137 sec != NULL;
9138 sec = (sec == toc ? NULL
9139 : sec->next == NULL ? toc
9140 : sec->next == toc && toc->next ? toc->next
9141 : sec->next))
9142 {
9143 int repeat;
9144
9145 if (sec->reloc_count == 0
9146 || discarded_section (sec)
9147 || get_opd_info (sec)
9148 || (sec->flags & SEC_ALLOC) == 0
9149 || (sec->flags & SEC_DEBUGGING) != 0)
9150 continue;
9151
9152 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9153 info->keep_memory);
9154 if (relstart == NULL)
9155 {
9156 free (used);
9157 goto error_ret;
9158 }
9159
9160 /* Mark toc entries referenced as used. */
9161 do
9162 {
9163 repeat = 0;
9164 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9165 {
9166 enum elf_ppc64_reloc_type r_type;
9167 unsigned long r_symndx;
9168 asection *sym_sec;
9169 struct elf_link_hash_entry *h;
9170 Elf_Internal_Sym *sym;
9171 bfd_vma val;
9172 enum {no_check, check_lo, check_ha} insn_check;
9173
9174 r_type = ELF64_R_TYPE (rel->r_info);
9175 switch (r_type)
9176 {
9177 default:
9178 insn_check = no_check;
9179 break;
9180
9181 case R_PPC64_GOT_TLSLD16_HA:
9182 case R_PPC64_GOT_TLSGD16_HA:
9183 case R_PPC64_GOT_TPREL16_HA:
9184 case R_PPC64_GOT_DTPREL16_HA:
9185 case R_PPC64_GOT16_HA:
9186 case R_PPC64_TOC16_HA:
9187 insn_check = check_ha;
9188 break;
9189
9190 case R_PPC64_GOT_TLSLD16_LO:
9191 case R_PPC64_GOT_TLSGD16_LO:
9192 case R_PPC64_GOT_TPREL16_LO_DS:
9193 case R_PPC64_GOT_DTPREL16_LO_DS:
9194 case R_PPC64_GOT16_LO:
9195 case R_PPC64_GOT16_LO_DS:
9196 case R_PPC64_TOC16_LO:
9197 case R_PPC64_TOC16_LO_DS:
9198 insn_check = check_lo;
9199 break;
9200 }
9201
9202 if (insn_check != no_check)
9203 {
9204 bfd_vma off = rel->r_offset & ~3;
9205 unsigned char buf[4];
9206 unsigned int insn;
9207
9208 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9209 {
9210 free (used);
9211 goto error_ret;
9212 }
9213 insn = bfd_get_32 (ibfd, buf);
9214 if (insn_check == check_lo
9215 ? !ok_lo_toc_insn (insn)
9216 : ((insn & ((0x3f << 26) | 0x1f << 16))
9217 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9218 {
9219 char str[12];
9220
9221 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9222 sprintf (str, "%#08x", insn);
9223 info->callbacks->einfo
9224 /* xgettext:c-format */
9225 (_("%P: %H: toc optimization is not supported for"
9226 " %s instruction.\n"),
9227 ibfd, sec, rel->r_offset & ~3, str);
9228 }
9229 }
9230
9231 switch (r_type)
9232 {
9233 case R_PPC64_TOC16:
9234 case R_PPC64_TOC16_LO:
9235 case R_PPC64_TOC16_HI:
9236 case R_PPC64_TOC16_HA:
9237 case R_PPC64_TOC16_DS:
9238 case R_PPC64_TOC16_LO_DS:
9239 /* In case we're taking addresses of toc entries. */
9240 case R_PPC64_ADDR64:
9241 break;
9242
9243 default:
9244 continue;
9245 }
9246
9247 r_symndx = ELF64_R_SYM (rel->r_info);
9248 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9249 r_symndx, ibfd))
9250 {
9251 free (used);
9252 goto error_ret;
9253 }
9254
9255 if (sym_sec != toc)
9256 continue;
9257
9258 if (h != NULL)
9259 val = h->root.u.def.value;
9260 else
9261 val = sym->st_value;
9262 val += rel->r_addend;
9263
9264 if (val >= toc->size)
9265 continue;
9266
9267 if ((skip[val >> 3] & can_optimize) != 0)
9268 {
9269 bfd_vma off;
9270 unsigned char opc;
9271
9272 switch (r_type)
9273 {
9274 case R_PPC64_TOC16_HA:
9275 break;
9276
9277 case R_PPC64_TOC16_LO_DS:
9278 off = rel->r_offset;
9279 off += (bfd_big_endian (ibfd) ? -2 : 3);
9280 if (!bfd_get_section_contents (ibfd, sec, &opc,
9281 off, 1))
9282 {
9283 free (used);
9284 goto error_ret;
9285 }
9286 if ((opc & (0x3f << 2)) == (58u << 2))
9287 break;
9288 /* Fall through. */
9289
9290 default:
9291 /* Wrong sort of reloc, or not a ld. We may
9292 as well clear ref_from_discarded too. */
9293 skip[val >> 3] = 0;
9294 }
9295 }
9296
9297 if (sec != toc)
9298 used[val >> 3] = 1;
9299 /* For the toc section, we only mark as used if this
9300 entry itself isn't unused. */
9301 else if ((used[rel->r_offset >> 3]
9302 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9303 && !used[val >> 3])
9304 {
9305 /* Do all the relocs again, to catch reference
9306 chains. */
9307 repeat = 1;
9308 used[val >> 3] = 1;
9309 }
9310 }
9311 }
9312 while (repeat);
9313
9314 if (elf_section_data (sec)->relocs != relstart)
9315 free (relstart);
9316 }
9317
9318 /* Merge the used and skip arrays. Assume that TOC
9319 doublewords not appearing as either used or unused belong
9320 to to an entry more than one doubleword in size. */
9321 for (drop = skip, keep = used, last = 0, some_unused = 0;
9322 drop < skip + (toc->size + 7) / 8;
9323 ++drop, ++keep)
9324 {
9325 if (*keep)
9326 {
9327 *drop &= ~ref_from_discarded;
9328 if ((*drop & can_optimize) != 0)
9329 some_unused = 1;
9330 last = 0;
9331 }
9332 else if ((*drop & ref_from_discarded) != 0)
9333 {
9334 some_unused = 1;
9335 last = ref_from_discarded;
9336 }
9337 else
9338 *drop = last;
9339 }
9340
9341 free (used);
9342
9343 if (some_unused)
9344 {
9345 bfd_byte *contents, *src;
9346 unsigned long off;
9347 Elf_Internal_Sym *sym;
9348 bfd_boolean local_toc_syms = FALSE;
9349
9350 /* Shuffle the toc contents, and at the same time convert the
9351 skip array from booleans into offsets. */
9352 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9353 goto error_ret;
9354
9355 elf_section_data (toc)->this_hdr.contents = contents;
9356
9357 for (src = contents, off = 0, drop = skip;
9358 src < contents + toc->size;
9359 src += 8, ++drop)
9360 {
9361 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9362 off += 8;
9363 else if (off != 0)
9364 {
9365 *drop = off;
9366 memcpy (src - off, src, 8);
9367 }
9368 }
9369 *drop = off;
9370 toc->rawsize = toc->size;
9371 toc->size = src - contents - off;
9372
9373 /* Adjust addends for relocs against the toc section sym,
9374 and optimize any accesses we can. */
9375 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9376 {
9377 if (sec->reloc_count == 0
9378 || discarded_section (sec))
9379 continue;
9380
9381 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9382 info->keep_memory);
9383 if (relstart == NULL)
9384 goto error_ret;
9385
9386 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9387 {
9388 enum elf_ppc64_reloc_type r_type;
9389 unsigned long r_symndx;
9390 asection *sym_sec;
9391 struct elf_link_hash_entry *h;
9392 bfd_vma val;
9393
9394 r_type = ELF64_R_TYPE (rel->r_info);
9395 switch (r_type)
9396 {
9397 default:
9398 continue;
9399
9400 case R_PPC64_TOC16:
9401 case R_PPC64_TOC16_LO:
9402 case R_PPC64_TOC16_HI:
9403 case R_PPC64_TOC16_HA:
9404 case R_PPC64_TOC16_DS:
9405 case R_PPC64_TOC16_LO_DS:
9406 case R_PPC64_ADDR64:
9407 break;
9408 }
9409
9410 r_symndx = ELF64_R_SYM (rel->r_info);
9411 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9412 r_symndx, ibfd))
9413 goto error_ret;
9414
9415 if (sym_sec != toc)
9416 continue;
9417
9418 if (h != NULL)
9419 val = h->root.u.def.value;
9420 else
9421 {
9422 val = sym->st_value;
9423 if (val != 0)
9424 local_toc_syms = TRUE;
9425 }
9426
9427 val += rel->r_addend;
9428
9429 if (val > toc->rawsize)
9430 val = toc->rawsize;
9431 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9432 continue;
9433 else if ((skip[val >> 3] & can_optimize) != 0)
9434 {
9435 Elf_Internal_Rela *tocrel
9436 = toc_relocs + (skip[val >> 3] >> 2);
9437 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9438
9439 switch (r_type)
9440 {
9441 case R_PPC64_TOC16_HA:
9442 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9443 break;
9444
9445 case R_PPC64_TOC16_LO_DS:
9446 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9447 break;
9448
9449 default:
9450 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9451 ppc_howto_init ();
9452 info->callbacks->einfo
9453 /* xgettext:c-format */
9454 (_("%P: %H: %s references "
9455 "optimized away TOC entry\n"),
9456 ibfd, sec, rel->r_offset,
9457 ppc64_elf_howto_table[r_type]->name);
9458 bfd_set_error (bfd_error_bad_value);
9459 goto error_ret;
9460 }
9461 rel->r_addend = tocrel->r_addend;
9462 elf_section_data (sec)->relocs = relstart;
9463 continue;
9464 }
9465
9466 if (h != NULL || sym->st_value != 0)
9467 continue;
9468
9469 rel->r_addend -= skip[val >> 3];
9470 elf_section_data (sec)->relocs = relstart;
9471 }
9472
9473 if (elf_section_data (sec)->relocs != relstart)
9474 free (relstart);
9475 }
9476
9477 /* We shouldn't have local or global symbols defined in the TOC,
9478 but handle them anyway. */
9479 if (local_syms != NULL)
9480 for (sym = local_syms;
9481 sym < local_syms + symtab_hdr->sh_info;
9482 ++sym)
9483 if (sym->st_value != 0
9484 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9485 {
9486 unsigned long i;
9487
9488 if (sym->st_value > toc->rawsize)
9489 i = toc->rawsize >> 3;
9490 else
9491 i = sym->st_value >> 3;
9492
9493 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9494 {
9495 if (local_toc_syms)
9496 _bfd_error_handler
9497 (_("%s defined on removed toc entry"),
9498 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9499 do
9500 ++i;
9501 while ((skip[i] & (ref_from_discarded | can_optimize)));
9502 sym->st_value = (bfd_vma) i << 3;
9503 }
9504
9505 sym->st_value -= skip[i];
9506 symtab_hdr->contents = (unsigned char *) local_syms;
9507 }
9508
9509 /* Adjust any global syms defined in this toc input section. */
9510 if (toc_inf.global_toc_syms)
9511 {
9512 toc_inf.toc = toc;
9513 toc_inf.skip = skip;
9514 toc_inf.global_toc_syms = FALSE;
9515 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9516 &toc_inf);
9517 }
9518
9519 if (toc->reloc_count != 0)
9520 {
9521 Elf_Internal_Shdr *rel_hdr;
9522 Elf_Internal_Rela *wrel;
9523 bfd_size_type sz;
9524
9525 /* Remove unused toc relocs, and adjust those we keep. */
9526 if (toc_relocs == NULL)
9527 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9528 info->keep_memory);
9529 if (toc_relocs == NULL)
9530 goto error_ret;
9531
9532 wrel = toc_relocs;
9533 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9534 if ((skip[rel->r_offset >> 3]
9535 & (ref_from_discarded | can_optimize)) == 0)
9536 {
9537 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9538 wrel->r_info = rel->r_info;
9539 wrel->r_addend = rel->r_addend;
9540 ++wrel;
9541 }
9542 else if (!dec_dynrel_count (rel->r_info, toc, info,
9543 &local_syms, NULL, NULL))
9544 goto error_ret;
9545
9546 elf_section_data (toc)->relocs = toc_relocs;
9547 toc->reloc_count = wrel - toc_relocs;
9548 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9549 sz = rel_hdr->sh_entsize;
9550 rel_hdr->sh_size = toc->reloc_count * sz;
9551 }
9552 }
9553 else if (toc_relocs != NULL
9554 && elf_section_data (toc)->relocs != toc_relocs)
9555 free (toc_relocs);
9556
9557 if (local_syms != NULL
9558 && symtab_hdr->contents != (unsigned char *) local_syms)
9559 {
9560 if (!info->keep_memory)
9561 free (local_syms);
9562 else
9563 symtab_hdr->contents = (unsigned char *) local_syms;
9564 }
9565 free (skip);
9566 }
9567
9568 return TRUE;
9569 }
9570
9571 /* Return true iff input section I references the TOC using
9572 instructions limited to +/-32k offsets. */
9573
9574 bfd_boolean
9575 ppc64_elf_has_small_toc_reloc (asection *i)
9576 {
9577 return (is_ppc64_elf (i->owner)
9578 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9579 }
9580
9581 /* Allocate space for one GOT entry. */
9582
9583 static void
9584 allocate_got (struct elf_link_hash_entry *h,
9585 struct bfd_link_info *info,
9586 struct got_entry *gent)
9587 {
9588 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9589 bfd_boolean dyn;
9590 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9591 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9592 ? 16 : 8);
9593 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9594 ? 2 : 1) * sizeof (Elf64_External_Rela);
9595 asection *got = ppc64_elf_tdata (gent->owner)->got;
9596
9597 gent->got.offset = got->size;
9598 got->size += entsize;
9599
9600 dyn = htab->elf.dynamic_sections_created;
9601 if (h->type == STT_GNU_IFUNC)
9602 {
9603 htab->elf.irelplt->size += rentsize;
9604 htab->got_reli_size += rentsize;
9605 }
9606 else if ((bfd_link_pic (info)
9607 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9608 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9609 || h->root.type != bfd_link_hash_undefweak))
9610 {
9611 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9612 relgot->size += rentsize;
9613 }
9614 }
9615
9616 /* This function merges got entries in the same toc group. */
9617
9618 static void
9619 merge_got_entries (struct got_entry **pent)
9620 {
9621 struct got_entry *ent, *ent2;
9622
9623 for (ent = *pent; ent != NULL; ent = ent->next)
9624 if (!ent->is_indirect)
9625 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9626 if (!ent2->is_indirect
9627 && ent2->addend == ent->addend
9628 && ent2->tls_type == ent->tls_type
9629 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9630 {
9631 ent2->is_indirect = TRUE;
9632 ent2->got.ent = ent;
9633 }
9634 }
9635
9636 /* Allocate space in .plt, .got and associated reloc sections for
9637 dynamic relocs. */
9638
9639 static bfd_boolean
9640 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9641 {
9642 struct bfd_link_info *info;
9643 struct ppc_link_hash_table *htab;
9644 asection *s;
9645 struct ppc_link_hash_entry *eh;
9646 struct got_entry **pgent, *gent;
9647
9648 if (h->root.type == bfd_link_hash_indirect)
9649 return TRUE;
9650
9651 info = (struct bfd_link_info *) inf;
9652 htab = ppc_hash_table (info);
9653 if (htab == NULL)
9654 return FALSE;
9655
9656 eh = (struct ppc_link_hash_entry *) h;
9657 /* Run through the TLS GD got entries first if we're changing them
9658 to TPREL. */
9659 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9660 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9661 if (gent->got.refcount > 0
9662 && (gent->tls_type & TLS_GD) != 0)
9663 {
9664 /* This was a GD entry that has been converted to TPREL. If
9665 there happens to be a TPREL entry we can use that one. */
9666 struct got_entry *ent;
9667 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9668 if (ent->got.refcount > 0
9669 && (ent->tls_type & TLS_TPREL) != 0
9670 && ent->addend == gent->addend
9671 && ent->owner == gent->owner)
9672 {
9673 gent->got.refcount = 0;
9674 break;
9675 }
9676
9677 /* If not, then we'll be using our own TPREL entry. */
9678 if (gent->got.refcount != 0)
9679 gent->tls_type = TLS_TLS | TLS_TPREL;
9680 }
9681
9682 /* Remove any list entry that won't generate a word in the GOT before
9683 we call merge_got_entries. Otherwise we risk merging to empty
9684 entries. */
9685 pgent = &h->got.glist;
9686 while ((gent = *pgent) != NULL)
9687 if (gent->got.refcount > 0)
9688 {
9689 if ((gent->tls_type & TLS_LD) != 0
9690 && !h->def_dynamic)
9691 {
9692 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9693 *pgent = gent->next;
9694 }
9695 else
9696 pgent = &gent->next;
9697 }
9698 else
9699 *pgent = gent->next;
9700
9701 if (!htab->do_multi_toc)
9702 merge_got_entries (&h->got.glist);
9703
9704 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9705 if (!gent->is_indirect)
9706 {
9707 /* Make sure this symbol is output as a dynamic symbol.
9708 Undefined weak syms won't yet be marked as dynamic,
9709 nor will all TLS symbols. */
9710 if (h->dynindx == -1
9711 && !h->forced_local
9712 && h->type != STT_GNU_IFUNC
9713 && htab->elf.dynamic_sections_created)
9714 {
9715 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9716 return FALSE;
9717 }
9718
9719 if (!is_ppc64_elf (gent->owner))
9720 abort ();
9721
9722 allocate_got (h, info, gent);
9723 }
9724
9725 if (!htab->elf.dynamic_sections_created
9726 && h->type != STT_GNU_IFUNC)
9727 eh->dyn_relocs = NULL;
9728
9729 if (eh->dyn_relocs != NULL)
9730 {
9731 struct elf_dyn_relocs *p, **pp;
9732
9733 /* In the shared -Bsymbolic case, discard space allocated for
9734 dynamic pc-relative relocs against symbols which turn out to
9735 be defined in regular objects. For the normal shared case,
9736 discard space for relocs that have become local due to symbol
9737 visibility changes. */
9738
9739 if (bfd_link_pic (info))
9740 {
9741 /* Relocs that use pc_count are those that appear on a call
9742 insn, or certain REL relocs (see must_be_dyn_reloc) that
9743 can be generated via assembly. We want calls to
9744 protected symbols to resolve directly to the function
9745 rather than going via the plt. If people want function
9746 pointer comparisons to work as expected then they should
9747 avoid writing weird assembly. */
9748 if (SYMBOL_CALLS_LOCAL (info, h))
9749 {
9750 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9751 {
9752 p->count -= p->pc_count;
9753 p->pc_count = 0;
9754 if (p->count == 0)
9755 *pp = p->next;
9756 else
9757 pp = &p->next;
9758 }
9759 }
9760
9761 /* Also discard relocs on undefined weak syms with
9762 non-default visibility. */
9763 if (eh->dyn_relocs != NULL
9764 && h->root.type == bfd_link_hash_undefweak)
9765 {
9766 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9767 eh->dyn_relocs = NULL;
9768
9769 /* Make sure this symbol is output as a dynamic symbol.
9770 Undefined weak syms won't yet be marked as dynamic. */
9771 else if (h->dynindx == -1
9772 && !h->forced_local)
9773 {
9774 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9775 return FALSE;
9776 }
9777 }
9778 }
9779 else if (h->type == STT_GNU_IFUNC)
9780 {
9781 /* A plt entry is always created when making direct calls to
9782 an ifunc, even when building a static executable, but
9783 that doesn't cover all cases. We may have only an ifunc
9784 initialised function pointer for a given ifunc symbol.
9785
9786 For ELFv2, dynamic relocations are not required when
9787 generating a global entry PLT stub. */
9788 if (abiversion (info->output_bfd) >= 2)
9789 {
9790 if (global_entry_stub (h))
9791 eh->dyn_relocs = NULL;
9792 }
9793
9794 /* For ELFv1 we have function descriptors. Descriptors need
9795 to be treated like PLT entries and thus have dynamic
9796 relocations. One exception is when the function
9797 descriptor is copied into .dynbss (which should only
9798 happen with ancient versions of gcc). */
9799 else if (h->needs_copy)
9800 eh->dyn_relocs = NULL;
9801 }
9802 else if (ELIMINATE_COPY_RELOCS)
9803 {
9804 /* For the non-pic case, discard space for relocs against
9805 symbols which turn out to need copy relocs or are not
9806 dynamic. */
9807
9808 /* First make sure this symbol is output as a dynamic symbol.
9809 Undefined weak syms won't yet be marked as dynamic. */
9810 if (h->root.type == bfd_link_hash_undefweak
9811 && !h->non_got_ref
9812 && !h->def_regular
9813 && h->dynindx == -1
9814 && !h->forced_local
9815 && !bfd_elf_link_record_dynamic_symbol (info, h))
9816 return FALSE;
9817
9818 if (h->non_got_ref
9819 || h->def_regular
9820 || h->dynindx == -1)
9821 eh->dyn_relocs = NULL;
9822 }
9823
9824 /* Finally, allocate space. */
9825 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9826 {
9827 asection *sreloc = elf_section_data (p->sec)->sreloc;
9828 if (eh->elf.type == STT_GNU_IFUNC)
9829 sreloc = htab->elf.irelplt;
9830 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9831 }
9832 }
9833
9834 if ((htab->elf.dynamic_sections_created
9835 && h->dynindx != -1
9836 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9837 || h->type == STT_GNU_IFUNC)
9838 {
9839 struct plt_entry *pent;
9840 bfd_boolean doneone = FALSE;
9841 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9842 if (pent->plt.refcount > 0)
9843 {
9844 if (!htab->elf.dynamic_sections_created
9845 || h->dynindx == -1)
9846 {
9847 s = htab->elf.iplt;
9848 pent->plt.offset = s->size;
9849 s->size += PLT_ENTRY_SIZE (htab);
9850 s = htab->elf.irelplt;
9851 }
9852 else
9853 {
9854 /* If this is the first .plt entry, make room for the special
9855 first entry. */
9856 s = htab->elf.splt;
9857 if (s->size == 0)
9858 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9859
9860 pent->plt.offset = s->size;
9861
9862 /* Make room for this entry. */
9863 s->size += PLT_ENTRY_SIZE (htab);
9864
9865 /* Make room for the .glink code. */
9866 s = htab->glink;
9867 if (s->size == 0)
9868 s->size += GLINK_CALL_STUB_SIZE;
9869 if (htab->opd_abi)
9870 {
9871 /* We need bigger stubs past index 32767. */
9872 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9873 s->size += 4;
9874 s->size += 2*4;
9875 }
9876 else
9877 s->size += 4;
9878
9879 /* We also need to make an entry in the .rela.plt section. */
9880 s = htab->elf.srelplt;
9881 }
9882 s->size += sizeof (Elf64_External_Rela);
9883 doneone = TRUE;
9884 }
9885 else
9886 pent->plt.offset = (bfd_vma) -1;
9887 if (!doneone)
9888 {
9889 h->plt.plist = NULL;
9890 h->needs_plt = 0;
9891 }
9892 }
9893 else
9894 {
9895 h->plt.plist = NULL;
9896 h->needs_plt = 0;
9897 }
9898
9899 return TRUE;
9900 }
9901
9902 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9903 to set up space for global entry stubs. These are put in glink,
9904 after the branch table. */
9905
9906 static bfd_boolean
9907 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9908 {
9909 struct bfd_link_info *info;
9910 struct ppc_link_hash_table *htab;
9911 struct plt_entry *pent;
9912 asection *s;
9913
9914 if (h->root.type == bfd_link_hash_indirect)
9915 return TRUE;
9916
9917 if (!h->pointer_equality_needed)
9918 return TRUE;
9919
9920 if (h->def_regular)
9921 return TRUE;
9922
9923 info = inf;
9924 htab = ppc_hash_table (info);
9925 if (htab == NULL)
9926 return FALSE;
9927
9928 s = htab->glink;
9929 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9930 if (pent->plt.offset != (bfd_vma) -1
9931 && pent->addend == 0)
9932 {
9933 /* For ELFv2, if this symbol is not defined in a regular file
9934 and we are not generating a shared library or pie, then we
9935 need to define the symbol in the executable on a call stub.
9936 This is to avoid text relocations. */
9937 s->size = (s->size + 15) & -16;
9938 h->root.type = bfd_link_hash_defined;
9939 h->root.u.def.section = s;
9940 h->root.u.def.value = s->size;
9941 s->size += 16;
9942 break;
9943 }
9944 return TRUE;
9945 }
9946
9947 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9948 read-only sections. */
9949
9950 static bfd_boolean
9951 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9952 {
9953 if (h->root.type == bfd_link_hash_indirect)
9954 return TRUE;
9955
9956 if (readonly_dynrelocs (h))
9957 {
9958 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9959
9960 /* Not an error, just cut short the traversal. */
9961 return FALSE;
9962 }
9963 return TRUE;
9964 }
9965
9966 /* Set the sizes of the dynamic sections. */
9967
9968 static bfd_boolean
9969 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9970 struct bfd_link_info *info)
9971 {
9972 struct ppc_link_hash_table *htab;
9973 bfd *dynobj;
9974 asection *s;
9975 bfd_boolean relocs;
9976 bfd *ibfd;
9977 struct got_entry *first_tlsld;
9978
9979 htab = ppc_hash_table (info);
9980 if (htab == NULL)
9981 return FALSE;
9982
9983 dynobj = htab->elf.dynobj;
9984 if (dynobj == NULL)
9985 abort ();
9986
9987 if (htab->elf.dynamic_sections_created)
9988 {
9989 /* Set the contents of the .interp section to the interpreter. */
9990 if (bfd_link_executable (info) && !info->nointerp)
9991 {
9992 s = bfd_get_linker_section (dynobj, ".interp");
9993 if (s == NULL)
9994 abort ();
9995 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9996 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9997 }
9998 }
9999
10000 /* Set up .got offsets for local syms, and space for local dynamic
10001 relocs. */
10002 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10003 {
10004 struct got_entry **lgot_ents;
10005 struct got_entry **end_lgot_ents;
10006 struct plt_entry **local_plt;
10007 struct plt_entry **end_local_plt;
10008 unsigned char *lgot_masks;
10009 bfd_size_type locsymcount;
10010 Elf_Internal_Shdr *symtab_hdr;
10011
10012 if (!is_ppc64_elf (ibfd))
10013 continue;
10014
10015 for (s = ibfd->sections; s != NULL; s = s->next)
10016 {
10017 struct ppc_dyn_relocs *p;
10018
10019 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10020 {
10021 if (!bfd_is_abs_section (p->sec)
10022 && bfd_is_abs_section (p->sec->output_section))
10023 {
10024 /* Input section has been discarded, either because
10025 it is a copy of a linkonce section or due to
10026 linker script /DISCARD/, so we'll be discarding
10027 the relocs too. */
10028 }
10029 else if (p->count != 0)
10030 {
10031 asection *srel = elf_section_data (p->sec)->sreloc;
10032 if (p->ifunc)
10033 srel = htab->elf.irelplt;
10034 srel->size += p->count * sizeof (Elf64_External_Rela);
10035 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10036 info->flags |= DF_TEXTREL;
10037 }
10038 }
10039 }
10040
10041 lgot_ents = elf_local_got_ents (ibfd);
10042 if (!lgot_ents)
10043 continue;
10044
10045 symtab_hdr = &elf_symtab_hdr (ibfd);
10046 locsymcount = symtab_hdr->sh_info;
10047 end_lgot_ents = lgot_ents + locsymcount;
10048 local_plt = (struct plt_entry **) end_lgot_ents;
10049 end_local_plt = local_plt + locsymcount;
10050 lgot_masks = (unsigned char *) end_local_plt;
10051 s = ppc64_elf_tdata (ibfd)->got;
10052 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10053 {
10054 struct got_entry **pent, *ent;
10055
10056 pent = lgot_ents;
10057 while ((ent = *pent) != NULL)
10058 if (ent->got.refcount > 0)
10059 {
10060 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10061 {
10062 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10063 *pent = ent->next;
10064 }
10065 else
10066 {
10067 unsigned int ent_size = 8;
10068 unsigned int rel_size = sizeof (Elf64_External_Rela);
10069
10070 ent->got.offset = s->size;
10071 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10072 {
10073 ent_size *= 2;
10074 rel_size *= 2;
10075 }
10076 s->size += ent_size;
10077 if ((*lgot_masks & PLT_IFUNC) != 0)
10078 {
10079 htab->elf.irelplt->size += rel_size;
10080 htab->got_reli_size += rel_size;
10081 }
10082 else if (bfd_link_pic (info))
10083 {
10084 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10085 srel->size += rel_size;
10086 }
10087 pent = &ent->next;
10088 }
10089 }
10090 else
10091 *pent = ent->next;
10092 }
10093
10094 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10095 for (; local_plt < end_local_plt; ++local_plt)
10096 {
10097 struct plt_entry *ent;
10098
10099 for (ent = *local_plt; ent != NULL; ent = ent->next)
10100 if (ent->plt.refcount > 0)
10101 {
10102 s = htab->elf.iplt;
10103 ent->plt.offset = s->size;
10104 s->size += PLT_ENTRY_SIZE (htab);
10105
10106 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10107 }
10108 else
10109 ent->plt.offset = (bfd_vma) -1;
10110 }
10111 }
10112
10113 /* Allocate global sym .plt and .got entries, and space for global
10114 sym dynamic relocs. */
10115 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10116 /* Stash the end of glink branch table. */
10117 if (htab->glink != NULL)
10118 htab->glink->rawsize = htab->glink->size;
10119
10120 if (!htab->opd_abi && !bfd_link_pic (info))
10121 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10122
10123 first_tlsld = NULL;
10124 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10125 {
10126 struct got_entry *ent;
10127
10128 if (!is_ppc64_elf (ibfd))
10129 continue;
10130
10131 ent = ppc64_tlsld_got (ibfd);
10132 if (ent->got.refcount > 0)
10133 {
10134 if (!htab->do_multi_toc && first_tlsld != NULL)
10135 {
10136 ent->is_indirect = TRUE;
10137 ent->got.ent = first_tlsld;
10138 }
10139 else
10140 {
10141 if (first_tlsld == NULL)
10142 first_tlsld = ent;
10143 s = ppc64_elf_tdata (ibfd)->got;
10144 ent->got.offset = s->size;
10145 ent->owner = ibfd;
10146 s->size += 16;
10147 if (bfd_link_pic (info))
10148 {
10149 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10150 srel->size += sizeof (Elf64_External_Rela);
10151 }
10152 }
10153 }
10154 else
10155 ent->got.offset = (bfd_vma) -1;
10156 }
10157
10158 /* We now have determined the sizes of the various dynamic sections.
10159 Allocate memory for them. */
10160 relocs = FALSE;
10161 for (s = dynobj->sections; s != NULL; s = s->next)
10162 {
10163 if ((s->flags & SEC_LINKER_CREATED) == 0)
10164 continue;
10165
10166 if (s == htab->brlt || s == htab->relbrlt)
10167 /* These haven't been allocated yet; don't strip. */
10168 continue;
10169 else if (s == htab->elf.sgot
10170 || s == htab->elf.splt
10171 || s == htab->elf.iplt
10172 || s == htab->glink
10173 || s == htab->dynbss)
10174 {
10175 /* Strip this section if we don't need it; see the
10176 comment below. */
10177 }
10178 else if (s == htab->glink_eh_frame)
10179 {
10180 if (!bfd_is_abs_section (s->output_section))
10181 /* Not sized yet. */
10182 continue;
10183 }
10184 else if (CONST_STRNEQ (s->name, ".rela"))
10185 {
10186 if (s->size != 0)
10187 {
10188 if (s != htab->elf.srelplt)
10189 relocs = TRUE;
10190
10191 /* We use the reloc_count field as a counter if we need
10192 to copy relocs into the output file. */
10193 s->reloc_count = 0;
10194 }
10195 }
10196 else
10197 {
10198 /* It's not one of our sections, so don't allocate space. */
10199 continue;
10200 }
10201
10202 if (s->size == 0)
10203 {
10204 /* If we don't need this section, strip it from the
10205 output file. This is mostly to handle .rela.bss and
10206 .rela.plt. We must create both sections in
10207 create_dynamic_sections, because they must be created
10208 before the linker maps input sections to output
10209 sections. The linker does that before
10210 adjust_dynamic_symbol is called, and it is that
10211 function which decides whether anything needs to go
10212 into these sections. */
10213 s->flags |= SEC_EXCLUDE;
10214 continue;
10215 }
10216
10217 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10218 continue;
10219
10220 /* Allocate memory for the section contents. We use bfd_zalloc
10221 here in case unused entries are not reclaimed before the
10222 section's contents are written out. This should not happen,
10223 but this way if it does we get a R_PPC64_NONE reloc in .rela
10224 sections instead of garbage.
10225 We also rely on the section contents being zero when writing
10226 the GOT. */
10227 s->contents = bfd_zalloc (dynobj, s->size);
10228 if (s->contents == NULL)
10229 return FALSE;
10230 }
10231
10232 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10233 {
10234 if (!is_ppc64_elf (ibfd))
10235 continue;
10236
10237 s = ppc64_elf_tdata (ibfd)->got;
10238 if (s != NULL && s != htab->elf.sgot)
10239 {
10240 if (s->size == 0)
10241 s->flags |= SEC_EXCLUDE;
10242 else
10243 {
10244 s->contents = bfd_zalloc (ibfd, s->size);
10245 if (s->contents == NULL)
10246 return FALSE;
10247 }
10248 }
10249 s = ppc64_elf_tdata (ibfd)->relgot;
10250 if (s != NULL)
10251 {
10252 if (s->size == 0)
10253 s->flags |= SEC_EXCLUDE;
10254 else
10255 {
10256 s->contents = bfd_zalloc (ibfd, s->size);
10257 if (s->contents == NULL)
10258 return FALSE;
10259 relocs = TRUE;
10260 s->reloc_count = 0;
10261 }
10262 }
10263 }
10264
10265 if (htab->elf.dynamic_sections_created)
10266 {
10267 bfd_boolean tls_opt;
10268
10269 /* Add some entries to the .dynamic section. We fill in the
10270 values later, in ppc64_elf_finish_dynamic_sections, but we
10271 must add the entries now so that we get the correct size for
10272 the .dynamic section. The DT_DEBUG entry is filled in by the
10273 dynamic linker and used by the debugger. */
10274 #define add_dynamic_entry(TAG, VAL) \
10275 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10276
10277 if (bfd_link_executable (info))
10278 {
10279 if (!add_dynamic_entry (DT_DEBUG, 0))
10280 return FALSE;
10281 }
10282
10283 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10284 {
10285 if (!add_dynamic_entry (DT_PLTGOT, 0)
10286 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10287 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10288 || !add_dynamic_entry (DT_JMPREL, 0)
10289 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10290 return FALSE;
10291 }
10292
10293 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10294 {
10295 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10296 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10297 return FALSE;
10298 }
10299
10300 tls_opt = (htab->params->tls_get_addr_opt
10301 && htab->tls_get_addr_fd != NULL
10302 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10303 if (tls_opt || !htab->opd_abi)
10304 {
10305 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10306 return FALSE;
10307 }
10308
10309 if (relocs)
10310 {
10311 if (!add_dynamic_entry (DT_RELA, 0)
10312 || !add_dynamic_entry (DT_RELASZ, 0)
10313 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10314 return FALSE;
10315
10316 /* If any dynamic relocs apply to a read-only section,
10317 then we need a DT_TEXTREL entry. */
10318 if ((info->flags & DF_TEXTREL) == 0)
10319 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10320
10321 if ((info->flags & DF_TEXTREL) != 0)
10322 {
10323 if (!add_dynamic_entry (DT_TEXTREL, 0))
10324 return FALSE;
10325 }
10326 }
10327 }
10328 #undef add_dynamic_entry
10329
10330 return TRUE;
10331 }
10332
10333 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10334
10335 static bfd_boolean
10336 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10337 {
10338 if (h->plt.plist != NULL
10339 && !h->def_regular
10340 && !h->pointer_equality_needed)
10341 return FALSE;
10342
10343 return _bfd_elf_hash_symbol (h);
10344 }
10345
10346 /* Determine the type of stub needed, if any, for a call. */
10347
10348 static inline enum ppc_stub_type
10349 ppc_type_of_stub (asection *input_sec,
10350 const Elf_Internal_Rela *rel,
10351 struct ppc_link_hash_entry **hash,
10352 struct plt_entry **plt_ent,
10353 bfd_vma destination,
10354 unsigned long local_off)
10355 {
10356 struct ppc_link_hash_entry *h = *hash;
10357 bfd_vma location;
10358 bfd_vma branch_offset;
10359 bfd_vma max_branch_offset;
10360 enum elf_ppc64_reloc_type r_type;
10361
10362 if (h != NULL)
10363 {
10364 struct plt_entry *ent;
10365 struct ppc_link_hash_entry *fdh = h;
10366 if (h->oh != NULL
10367 && h->oh->is_func_descriptor)
10368 {
10369 fdh = ppc_follow_link (h->oh);
10370 *hash = fdh;
10371 }
10372
10373 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10374 if (ent->addend == rel->r_addend
10375 && ent->plt.offset != (bfd_vma) -1)
10376 {
10377 *plt_ent = ent;
10378 return ppc_stub_plt_call;
10379 }
10380
10381 /* Here, we know we don't have a plt entry. If we don't have a
10382 either a defined function descriptor or a defined entry symbol
10383 in a regular object file, then it is pointless trying to make
10384 any other type of stub. */
10385 if (!is_static_defined (&fdh->elf)
10386 && !is_static_defined (&h->elf))
10387 return ppc_stub_none;
10388 }
10389 else if (elf_local_got_ents (input_sec->owner) != NULL)
10390 {
10391 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10392 struct plt_entry **local_plt = (struct plt_entry **)
10393 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10394 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10395
10396 if (local_plt[r_symndx] != NULL)
10397 {
10398 struct plt_entry *ent;
10399
10400 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10401 if (ent->addend == rel->r_addend
10402 && ent->plt.offset != (bfd_vma) -1)
10403 {
10404 *plt_ent = ent;
10405 return ppc_stub_plt_call;
10406 }
10407 }
10408 }
10409
10410 /* Determine where the call point is. */
10411 location = (input_sec->output_offset
10412 + input_sec->output_section->vma
10413 + rel->r_offset);
10414
10415 branch_offset = destination - location;
10416 r_type = ELF64_R_TYPE (rel->r_info);
10417
10418 /* Determine if a long branch stub is needed. */
10419 max_branch_offset = 1 << 25;
10420 if (r_type != R_PPC64_REL24)
10421 max_branch_offset = 1 << 15;
10422
10423 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10424 /* We need a stub. Figure out whether a long_branch or plt_branch
10425 is needed later. */
10426 return ppc_stub_long_branch;
10427
10428 return ppc_stub_none;
10429 }
10430
10431 /* With power7 weakly ordered memory model, it is possible for ld.so
10432 to update a plt entry in one thread and have another thread see a
10433 stale zero toc entry. To avoid this we need some sort of acquire
10434 barrier in the call stub. One solution is to make the load of the
10435 toc word seem to appear to depend on the load of the function entry
10436 word. Another solution is to test for r2 being zero, and branch to
10437 the appropriate glink entry if so.
10438
10439 . fake dep barrier compare
10440 . ld 12,xxx(2) ld 12,xxx(2)
10441 . mtctr 12 mtctr 12
10442 . xor 11,12,12 ld 2,xxx+8(2)
10443 . add 2,2,11 cmpldi 2,0
10444 . ld 2,xxx+8(2) bnectr+
10445 . bctr b <glink_entry>
10446
10447 The solution involving the compare turns out to be faster, so
10448 that's what we use unless the branch won't reach. */
10449
10450 #define ALWAYS_USE_FAKE_DEP 0
10451 #define ALWAYS_EMIT_R2SAVE 0
10452
10453 #define PPC_LO(v) ((v) & 0xffff)
10454 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10455 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10456
10457 static inline unsigned int
10458 plt_stub_size (struct ppc_link_hash_table *htab,
10459 struct ppc_stub_hash_entry *stub_entry,
10460 bfd_vma off)
10461 {
10462 unsigned size = 12;
10463
10464 if (ALWAYS_EMIT_R2SAVE
10465 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10466 size += 4;
10467 if (PPC_HA (off) != 0)
10468 size += 4;
10469 if (htab->opd_abi)
10470 {
10471 size += 4;
10472 if (htab->params->plt_static_chain)
10473 size += 4;
10474 if (htab->params->plt_thread_safe
10475 && htab->elf.dynamic_sections_created
10476 && stub_entry->h != NULL
10477 && stub_entry->h->elf.dynindx != -1)
10478 size += 8;
10479 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10480 size += 4;
10481 }
10482 if (stub_entry->h != NULL
10483 && (stub_entry->h == htab->tls_get_addr_fd
10484 || stub_entry->h == htab->tls_get_addr)
10485 && htab->params->tls_get_addr_opt)
10486 size += 13 * 4;
10487 return size;
10488 }
10489
10490 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10491 then return the padding needed to do so. */
10492 static inline unsigned int
10493 plt_stub_pad (struct ppc_link_hash_table *htab,
10494 struct ppc_stub_hash_entry *stub_entry,
10495 bfd_vma plt_off)
10496 {
10497 int stub_align = 1 << htab->params->plt_stub_align;
10498 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10499 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10500
10501 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10502 > ((stub_size - 1) & -stub_align))
10503 return stub_align - (stub_off & (stub_align - 1));
10504 return 0;
10505 }
10506
10507 /* Build a .plt call stub. */
10508
10509 static inline bfd_byte *
10510 build_plt_stub (struct ppc_link_hash_table *htab,
10511 struct ppc_stub_hash_entry *stub_entry,
10512 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10513 {
10514 bfd *obfd = htab->params->stub_bfd;
10515 bfd_boolean plt_load_toc = htab->opd_abi;
10516 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10517 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10518 && htab->elf.dynamic_sections_created
10519 && stub_entry->h != NULL
10520 && stub_entry->h->elf.dynindx != -1);
10521 bfd_boolean use_fake_dep = plt_thread_safe;
10522 bfd_vma cmp_branch_off = 0;
10523
10524 if (!ALWAYS_USE_FAKE_DEP
10525 && plt_load_toc
10526 && plt_thread_safe
10527 && !((stub_entry->h == htab->tls_get_addr_fd
10528 || stub_entry->h == htab->tls_get_addr)
10529 && htab->params->tls_get_addr_opt))
10530 {
10531 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10532 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10533 / PLT_ENTRY_SIZE (htab));
10534 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10535 bfd_vma to, from;
10536
10537 if (pltindex > 32768)
10538 glinkoff += (pltindex - 32768) * 4;
10539 to = (glinkoff
10540 + htab->glink->output_offset
10541 + htab->glink->output_section->vma);
10542 from = (p - stub_entry->group->stub_sec->contents
10543 + 4 * (ALWAYS_EMIT_R2SAVE
10544 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10545 + 4 * (PPC_HA (offset) != 0)
10546 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10547 != PPC_HA (offset))
10548 + 4 * (plt_static_chain != 0)
10549 + 20
10550 + stub_entry->group->stub_sec->output_offset
10551 + stub_entry->group->stub_sec->output_section->vma);
10552 cmp_branch_off = to - from;
10553 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10554 }
10555
10556 if (PPC_HA (offset) != 0)
10557 {
10558 if (r != NULL)
10559 {
10560 if (ALWAYS_EMIT_R2SAVE
10561 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10562 r[0].r_offset += 4;
10563 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10564 r[1].r_offset = r[0].r_offset + 4;
10565 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10566 r[1].r_addend = r[0].r_addend;
10567 if (plt_load_toc)
10568 {
10569 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10570 {
10571 r[2].r_offset = r[1].r_offset + 4;
10572 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10573 r[2].r_addend = r[0].r_addend;
10574 }
10575 else
10576 {
10577 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10578 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10579 r[2].r_addend = r[0].r_addend + 8;
10580 if (plt_static_chain)
10581 {
10582 r[3].r_offset = r[2].r_offset + 4;
10583 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10584 r[3].r_addend = r[0].r_addend + 16;
10585 }
10586 }
10587 }
10588 }
10589 if (ALWAYS_EMIT_R2SAVE
10590 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10591 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10592 if (plt_load_toc)
10593 {
10594 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10595 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10596 }
10597 else
10598 {
10599 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10600 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10601 }
10602 if (plt_load_toc
10603 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10604 {
10605 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10606 offset = 0;
10607 }
10608 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10609 if (plt_load_toc)
10610 {
10611 if (use_fake_dep)
10612 {
10613 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10614 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10615 }
10616 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10617 if (plt_static_chain)
10618 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10619 }
10620 }
10621 else
10622 {
10623 if (r != NULL)
10624 {
10625 if (ALWAYS_EMIT_R2SAVE
10626 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10627 r[0].r_offset += 4;
10628 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10629 if (plt_load_toc)
10630 {
10631 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10632 {
10633 r[1].r_offset = r[0].r_offset + 4;
10634 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10635 r[1].r_addend = r[0].r_addend;
10636 }
10637 else
10638 {
10639 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10640 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10641 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10642 if (plt_static_chain)
10643 {
10644 r[2].r_offset = r[1].r_offset + 4;
10645 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10646 r[2].r_addend = r[0].r_addend + 8;
10647 }
10648 }
10649 }
10650 }
10651 if (ALWAYS_EMIT_R2SAVE
10652 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10653 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10654 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10655 if (plt_load_toc
10656 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10657 {
10658 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10659 offset = 0;
10660 }
10661 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10662 if (plt_load_toc)
10663 {
10664 if (use_fake_dep)
10665 {
10666 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10667 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10668 }
10669 if (plt_static_chain)
10670 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10671 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10672 }
10673 }
10674 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10675 {
10676 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10677 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10678 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10679 }
10680 else
10681 bfd_put_32 (obfd, BCTR, p), p += 4;
10682 return p;
10683 }
10684
10685 /* Build a special .plt call stub for __tls_get_addr. */
10686
10687 #define LD_R11_0R3 0xe9630000
10688 #define LD_R12_0R3 0xe9830000
10689 #define MR_R0_R3 0x7c601b78
10690 #define CMPDI_R11_0 0x2c2b0000
10691 #define ADD_R3_R12_R13 0x7c6c6a14
10692 #define BEQLR 0x4d820020
10693 #define MR_R3_R0 0x7c030378
10694 #define STD_R11_0R1 0xf9610000
10695 #define BCTRL 0x4e800421
10696 #define LD_R11_0R1 0xe9610000
10697 #define MTLR_R11 0x7d6803a6
10698
10699 static inline bfd_byte *
10700 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10701 struct ppc_stub_hash_entry *stub_entry,
10702 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10703 {
10704 bfd *obfd = htab->params->stub_bfd;
10705
10706 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10707 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10708 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10709 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10710 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10711 bfd_put_32 (obfd, BEQLR, p), p += 4;
10712 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10713 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10714 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10715
10716 if (r != NULL)
10717 r[0].r_offset += 9 * 4;
10718 p = build_plt_stub (htab, stub_entry, p, offset, r);
10719 bfd_put_32 (obfd, BCTRL, p - 4);
10720
10721 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10722 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10723 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10724 bfd_put_32 (obfd, BLR, p), p += 4;
10725
10726 return p;
10727 }
10728
10729 static Elf_Internal_Rela *
10730 get_relocs (asection *sec, int count)
10731 {
10732 Elf_Internal_Rela *relocs;
10733 struct bfd_elf_section_data *elfsec_data;
10734
10735 elfsec_data = elf_section_data (sec);
10736 relocs = elfsec_data->relocs;
10737 if (relocs == NULL)
10738 {
10739 bfd_size_type relsize;
10740 relsize = sec->reloc_count * sizeof (*relocs);
10741 relocs = bfd_alloc (sec->owner, relsize);
10742 if (relocs == NULL)
10743 return NULL;
10744 elfsec_data->relocs = relocs;
10745 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10746 sizeof (Elf_Internal_Shdr));
10747 if (elfsec_data->rela.hdr == NULL)
10748 return NULL;
10749 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10750 * sizeof (Elf64_External_Rela));
10751 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10752 sec->reloc_count = 0;
10753 }
10754 relocs += sec->reloc_count;
10755 sec->reloc_count += count;
10756 return relocs;
10757 }
10758
10759 static bfd_vma
10760 get_r2off (struct bfd_link_info *info,
10761 struct ppc_stub_hash_entry *stub_entry)
10762 {
10763 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10764 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10765
10766 if (r2off == 0)
10767 {
10768 /* Support linking -R objects. Get the toc pointer from the
10769 opd entry. */
10770 char buf[8];
10771 if (!htab->opd_abi)
10772 return r2off;
10773 asection *opd = stub_entry->h->elf.root.u.def.section;
10774 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10775
10776 if (strcmp (opd->name, ".opd") != 0
10777 || opd->reloc_count != 0)
10778 {
10779 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10780 stub_entry->h->elf.root.root.string);
10781 bfd_set_error (bfd_error_bad_value);
10782 return (bfd_vma) -1;
10783 }
10784 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10785 return (bfd_vma) -1;
10786 r2off = bfd_get_64 (opd->owner, buf);
10787 r2off -= elf_gp (info->output_bfd);
10788 }
10789 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10790 return r2off;
10791 }
10792
10793 static bfd_boolean
10794 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10795 {
10796 struct ppc_stub_hash_entry *stub_entry;
10797 struct ppc_branch_hash_entry *br_entry;
10798 struct bfd_link_info *info;
10799 struct ppc_link_hash_table *htab;
10800 bfd_byte *loc;
10801 bfd_byte *p;
10802 bfd_vma dest, off;
10803 int size;
10804 Elf_Internal_Rela *r;
10805 asection *plt;
10806
10807 /* Massage our args to the form they really have. */
10808 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10809 info = in_arg;
10810
10811 htab = ppc_hash_table (info);
10812 if (htab == NULL)
10813 return FALSE;
10814
10815 /* Make a note of the offset within the stubs for this entry. */
10816 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10817 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10818
10819 htab->stub_count[stub_entry->stub_type - 1] += 1;
10820 switch (stub_entry->stub_type)
10821 {
10822 case ppc_stub_long_branch:
10823 case ppc_stub_long_branch_r2off:
10824 /* Branches are relative. This is where we are going to. */
10825 dest = (stub_entry->target_value
10826 + stub_entry->target_section->output_offset
10827 + stub_entry->target_section->output_section->vma);
10828 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10829 off = dest;
10830
10831 /* And this is where we are coming from. */
10832 off -= (stub_entry->stub_offset
10833 + stub_entry->group->stub_sec->output_offset
10834 + stub_entry->group->stub_sec->output_section->vma);
10835
10836 size = 4;
10837 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10838 {
10839 bfd_vma r2off = get_r2off (info, stub_entry);
10840
10841 if (r2off == (bfd_vma) -1)
10842 {
10843 htab->stub_error = TRUE;
10844 return FALSE;
10845 }
10846 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10847 loc += 4;
10848 size = 8;
10849 if (PPC_HA (r2off) != 0)
10850 {
10851 bfd_put_32 (htab->params->stub_bfd,
10852 ADDIS_R2_R2 | PPC_HA (r2off), loc);
10853 loc += 4;
10854 size += 4;
10855 }
10856 if (PPC_LO (r2off) != 0)
10857 {
10858 bfd_put_32 (htab->params->stub_bfd,
10859 ADDI_R2_R2 | PPC_LO (r2off), loc);
10860 loc += 4;
10861 size += 4;
10862 }
10863 off -= size - 4;
10864 }
10865 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10866
10867 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10868 {
10869 info->callbacks->einfo
10870 (_("%P: long branch stub `%s' offset overflow\n"),
10871 stub_entry->root.string);
10872 htab->stub_error = TRUE;
10873 return FALSE;
10874 }
10875
10876 if (info->emitrelocations)
10877 {
10878 r = get_relocs (stub_entry->group->stub_sec, 1);
10879 if (r == NULL)
10880 return FALSE;
10881 r->r_offset = loc - stub_entry->group->stub_sec->contents;
10882 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10883 r->r_addend = dest;
10884 if (stub_entry->h != NULL)
10885 {
10886 struct elf_link_hash_entry **hashes;
10887 unsigned long symndx;
10888 struct ppc_link_hash_entry *h;
10889
10890 hashes = elf_sym_hashes (htab->params->stub_bfd);
10891 if (hashes == NULL)
10892 {
10893 bfd_size_type hsize;
10894
10895 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10896 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10897 if (hashes == NULL)
10898 return FALSE;
10899 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10900 htab->stub_globals = 1;
10901 }
10902 symndx = htab->stub_globals++;
10903 h = stub_entry->h;
10904 hashes[symndx] = &h->elf;
10905 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10906 if (h->oh != NULL && h->oh->is_func)
10907 h = ppc_follow_link (h->oh);
10908 if (h->elf.root.u.def.section != stub_entry->target_section)
10909 /* H is an opd symbol. The addend must be zero. */
10910 r->r_addend = 0;
10911 else
10912 {
10913 off = (h->elf.root.u.def.value
10914 + h->elf.root.u.def.section->output_offset
10915 + h->elf.root.u.def.section->output_section->vma);
10916 r->r_addend -= off;
10917 }
10918 }
10919 }
10920 break;
10921
10922 case ppc_stub_plt_branch:
10923 case ppc_stub_plt_branch_r2off:
10924 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10925 stub_entry->root.string + 9,
10926 FALSE, FALSE);
10927 if (br_entry == NULL)
10928 {
10929 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10930 stub_entry->root.string);
10931 htab->stub_error = TRUE;
10932 return FALSE;
10933 }
10934
10935 dest = (stub_entry->target_value
10936 + stub_entry->target_section->output_offset
10937 + stub_entry->target_section->output_section->vma);
10938 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10939 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10940
10941 bfd_put_64 (htab->brlt->owner, dest,
10942 htab->brlt->contents + br_entry->offset);
10943
10944 if (br_entry->iter == htab->stub_iteration)
10945 {
10946 br_entry->iter = 0;
10947
10948 if (htab->relbrlt != NULL)
10949 {
10950 /* Create a reloc for the branch lookup table entry. */
10951 Elf_Internal_Rela rela;
10952 bfd_byte *rl;
10953
10954 rela.r_offset = (br_entry->offset
10955 + htab->brlt->output_offset
10956 + htab->brlt->output_section->vma);
10957 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10958 rela.r_addend = dest;
10959
10960 rl = htab->relbrlt->contents;
10961 rl += (htab->relbrlt->reloc_count++
10962 * sizeof (Elf64_External_Rela));
10963 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10964 }
10965 else if (info->emitrelocations)
10966 {
10967 r = get_relocs (htab->brlt, 1);
10968 if (r == NULL)
10969 return FALSE;
10970 /* brlt, being SEC_LINKER_CREATED does not go through the
10971 normal reloc processing. Symbols and offsets are not
10972 translated from input file to output file form, so
10973 set up the offset per the output file. */
10974 r->r_offset = (br_entry->offset
10975 + htab->brlt->output_offset
10976 + htab->brlt->output_section->vma);
10977 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10978 r->r_addend = dest;
10979 }
10980 }
10981
10982 dest = (br_entry->offset
10983 + htab->brlt->output_offset
10984 + htab->brlt->output_section->vma);
10985
10986 off = (dest
10987 - elf_gp (htab->brlt->output_section->owner)
10988 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10989
10990 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10991 {
10992 info->callbacks->einfo
10993 (_("%P: linkage table error against `%T'\n"),
10994 stub_entry->root.string);
10995 bfd_set_error (bfd_error_bad_value);
10996 htab->stub_error = TRUE;
10997 return FALSE;
10998 }
10999
11000 if (info->emitrelocations)
11001 {
11002 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11003 if (r == NULL)
11004 return FALSE;
11005 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11006 if (bfd_big_endian (info->output_bfd))
11007 r[0].r_offset += 2;
11008 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11009 r[0].r_offset += 4;
11010 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11011 r[0].r_addend = dest;
11012 if (PPC_HA (off) != 0)
11013 {
11014 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11015 r[1].r_offset = r[0].r_offset + 4;
11016 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11017 r[1].r_addend = r[0].r_addend;
11018 }
11019 }
11020
11021 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11022 {
11023 if (PPC_HA (off) != 0)
11024 {
11025 size = 16;
11026 bfd_put_32 (htab->params->stub_bfd,
11027 ADDIS_R12_R2 | PPC_HA (off), loc);
11028 loc += 4;
11029 bfd_put_32 (htab->params->stub_bfd,
11030 LD_R12_0R12 | PPC_LO (off), loc);
11031 }
11032 else
11033 {
11034 size = 12;
11035 bfd_put_32 (htab->params->stub_bfd,
11036 LD_R12_0R2 | PPC_LO (off), loc);
11037 }
11038 }
11039 else
11040 {
11041 bfd_vma r2off = get_r2off (info, stub_entry);
11042
11043 if (r2off == (bfd_vma) -1)
11044 {
11045 htab->stub_error = TRUE;
11046 return FALSE;
11047 }
11048
11049 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11050 loc += 4;
11051 size = 16;
11052 if (PPC_HA (off) != 0)
11053 {
11054 size += 4;
11055 bfd_put_32 (htab->params->stub_bfd,
11056 ADDIS_R12_R2 | PPC_HA (off), loc);
11057 loc += 4;
11058 bfd_put_32 (htab->params->stub_bfd,
11059 LD_R12_0R12 | PPC_LO (off), loc);
11060 }
11061 else
11062 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11063
11064 if (PPC_HA (r2off) != 0)
11065 {
11066 size += 4;
11067 loc += 4;
11068 bfd_put_32 (htab->params->stub_bfd,
11069 ADDIS_R2_R2 | PPC_HA (r2off), loc);
11070 }
11071 if (PPC_LO (r2off) != 0)
11072 {
11073 size += 4;
11074 loc += 4;
11075 bfd_put_32 (htab->params->stub_bfd,
11076 ADDI_R2_R2 | PPC_LO (r2off), loc);
11077 }
11078 }
11079 loc += 4;
11080 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11081 loc += 4;
11082 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11083 break;
11084
11085 case ppc_stub_plt_call:
11086 case ppc_stub_plt_call_r2save:
11087 if (stub_entry->h != NULL
11088 && stub_entry->h->is_func_descriptor
11089 && stub_entry->h->oh != NULL)
11090 {
11091 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11092
11093 /* If the old-ABI "dot-symbol" is undefined make it weak so
11094 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11095 if (fh->elf.root.type == bfd_link_hash_undefined)
11096 fh->elf.root.type = bfd_link_hash_undefweak;
11097 /* Stop undo_symbol_twiddle changing it back to undefined. */
11098 fh->was_undefined = 0;
11099 }
11100
11101 /* Now build the stub. */
11102 dest = stub_entry->plt_ent->plt.offset & ~1;
11103 if (dest >= (bfd_vma) -2)
11104 abort ();
11105
11106 plt = htab->elf.splt;
11107 if (!htab->elf.dynamic_sections_created
11108 || stub_entry->h == NULL
11109 || stub_entry->h->elf.dynindx == -1)
11110 plt = htab->elf.iplt;
11111
11112 dest += plt->output_offset + plt->output_section->vma;
11113
11114 if (stub_entry->h == NULL
11115 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11116 {
11117 Elf_Internal_Rela rela;
11118 bfd_byte *rl;
11119
11120 rela.r_offset = dest;
11121 if (htab->opd_abi)
11122 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11123 else
11124 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11125 rela.r_addend = (stub_entry->target_value
11126 + stub_entry->target_section->output_offset
11127 + stub_entry->target_section->output_section->vma);
11128
11129 rl = (htab->elf.irelplt->contents
11130 + (htab->elf.irelplt->reloc_count++
11131 * sizeof (Elf64_External_Rela)));
11132 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11133 stub_entry->plt_ent->plt.offset |= 1;
11134 }
11135
11136 off = (dest
11137 - elf_gp (plt->output_section->owner)
11138 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11139
11140 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11141 {
11142 info->callbacks->einfo
11143 /* xgettext:c-format */
11144 (_("%P: linkage table error against `%T'\n"),
11145 stub_entry->h != NULL
11146 ? stub_entry->h->elf.root.root.string
11147 : "<local sym>");
11148 bfd_set_error (bfd_error_bad_value);
11149 htab->stub_error = TRUE;
11150 return FALSE;
11151 }
11152
11153 if (htab->params->plt_stub_align != 0)
11154 {
11155 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11156
11157 stub_entry->group->stub_sec->size += pad;
11158 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11159 loc += pad;
11160 }
11161
11162 r = NULL;
11163 if (info->emitrelocations)
11164 {
11165 r = get_relocs (stub_entry->group->stub_sec,
11166 ((PPC_HA (off) != 0)
11167 + (htab->opd_abi
11168 ? 2 + (htab->params->plt_static_chain
11169 && PPC_HA (off + 16) == PPC_HA (off))
11170 : 1)));
11171 if (r == NULL)
11172 return FALSE;
11173 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11174 if (bfd_big_endian (info->output_bfd))
11175 r[0].r_offset += 2;
11176 r[0].r_addend = dest;
11177 }
11178 if (stub_entry->h != NULL
11179 && (stub_entry->h == htab->tls_get_addr_fd
11180 || stub_entry->h == htab->tls_get_addr)
11181 && htab->params->tls_get_addr_opt)
11182 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11183 else
11184 p = build_plt_stub (htab, stub_entry, loc, off, r);
11185 size = p - loc;
11186 break;
11187
11188 case ppc_stub_save_res:
11189 return TRUE;
11190
11191 default:
11192 BFD_FAIL ();
11193 return FALSE;
11194 }
11195
11196 stub_entry->group->stub_sec->size += size;
11197
11198 if (htab->params->emit_stub_syms)
11199 {
11200 struct elf_link_hash_entry *h;
11201 size_t len1, len2;
11202 char *name;
11203 const char *const stub_str[] = { "long_branch",
11204 "long_branch_r2off",
11205 "plt_branch",
11206 "plt_branch_r2off",
11207 "plt_call",
11208 "plt_call" };
11209
11210 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11211 len2 = strlen (stub_entry->root.string);
11212 name = bfd_malloc (len1 + len2 + 2);
11213 if (name == NULL)
11214 return FALSE;
11215 memcpy (name, stub_entry->root.string, 9);
11216 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11217 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11218 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11219 if (h == NULL)
11220 return FALSE;
11221 if (h->root.type == bfd_link_hash_new)
11222 {
11223 h->root.type = bfd_link_hash_defined;
11224 h->root.u.def.section = stub_entry->group->stub_sec;
11225 h->root.u.def.value = stub_entry->stub_offset;
11226 h->ref_regular = 1;
11227 h->def_regular = 1;
11228 h->ref_regular_nonweak = 1;
11229 h->forced_local = 1;
11230 h->non_elf = 0;
11231 h->root.linker_def = 1;
11232 }
11233 }
11234
11235 return TRUE;
11236 }
11237
11238 /* As above, but don't actually build the stub. Just bump offset so
11239 we know stub section sizes, and select plt_branch stubs where
11240 long_branch stubs won't do. */
11241
11242 static bfd_boolean
11243 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11244 {
11245 struct ppc_stub_hash_entry *stub_entry;
11246 struct bfd_link_info *info;
11247 struct ppc_link_hash_table *htab;
11248 bfd_vma off;
11249 int size;
11250
11251 /* Massage our args to the form they really have. */
11252 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11253 info = in_arg;
11254
11255 htab = ppc_hash_table (info);
11256 if (htab == NULL)
11257 return FALSE;
11258
11259 if (stub_entry->h != NULL
11260 && stub_entry->h->save_res
11261 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11262 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11263 {
11264 /* Don't make stubs to out-of-line register save/restore
11265 functions. Instead, emit copies of the functions. */
11266 stub_entry->group->needs_save_res = 1;
11267 stub_entry->stub_type = ppc_stub_save_res;
11268 return TRUE;
11269 }
11270
11271 if (stub_entry->stub_type == ppc_stub_plt_call
11272 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11273 {
11274 asection *plt;
11275 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11276 if (off >= (bfd_vma) -2)
11277 abort ();
11278 plt = htab->elf.splt;
11279 if (!htab->elf.dynamic_sections_created
11280 || stub_entry->h == NULL
11281 || stub_entry->h->elf.dynindx == -1)
11282 plt = htab->elf.iplt;
11283 off += (plt->output_offset
11284 + plt->output_section->vma
11285 - elf_gp (plt->output_section->owner)
11286 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11287
11288 size = plt_stub_size (htab, stub_entry, off);
11289 if (htab->params->plt_stub_align)
11290 size += plt_stub_pad (htab, stub_entry, off);
11291 if (info->emitrelocations)
11292 {
11293 stub_entry->group->stub_sec->reloc_count
11294 += ((PPC_HA (off) != 0)
11295 + (htab->opd_abi
11296 ? 2 + (htab->params->plt_static_chain
11297 && PPC_HA (off + 16) == PPC_HA (off))
11298 : 1));
11299 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11300 }
11301 }
11302 else
11303 {
11304 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11305 variants. */
11306 bfd_vma r2off = 0;
11307 bfd_vma local_off = 0;
11308
11309 off = (stub_entry->target_value
11310 + stub_entry->target_section->output_offset
11311 + stub_entry->target_section->output_section->vma);
11312 off -= (stub_entry->group->stub_sec->size
11313 + stub_entry->group->stub_sec->output_offset
11314 + stub_entry->group->stub_sec->output_section->vma);
11315
11316 /* Reset the stub type from the plt variant in case we now
11317 can reach with a shorter stub. */
11318 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11319 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11320
11321 size = 4;
11322 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11323 {
11324 r2off = get_r2off (info, stub_entry);
11325 if (r2off == (bfd_vma) -1)
11326 {
11327 htab->stub_error = TRUE;
11328 return FALSE;
11329 }
11330 size = 8;
11331 if (PPC_HA (r2off) != 0)
11332 size += 4;
11333 if (PPC_LO (r2off) != 0)
11334 size += 4;
11335 off -= size - 4;
11336 }
11337
11338 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11339
11340 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11341 Do the same for -R objects without function descriptors. */
11342 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11343 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11344 && r2off == 0
11345 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11346 {
11347 struct ppc_branch_hash_entry *br_entry;
11348
11349 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11350 stub_entry->root.string + 9,
11351 TRUE, FALSE);
11352 if (br_entry == NULL)
11353 {
11354 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11355 stub_entry->root.string);
11356 htab->stub_error = TRUE;
11357 return FALSE;
11358 }
11359
11360 if (br_entry->iter != htab->stub_iteration)
11361 {
11362 br_entry->iter = htab->stub_iteration;
11363 br_entry->offset = htab->brlt->size;
11364 htab->brlt->size += 8;
11365
11366 if (htab->relbrlt != NULL)
11367 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11368 else if (info->emitrelocations)
11369 {
11370 htab->brlt->reloc_count += 1;
11371 htab->brlt->flags |= SEC_RELOC;
11372 }
11373 }
11374
11375 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11376 off = (br_entry->offset
11377 + htab->brlt->output_offset
11378 + htab->brlt->output_section->vma
11379 - elf_gp (htab->brlt->output_section->owner)
11380 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11381
11382 if (info->emitrelocations)
11383 {
11384 stub_entry->group->stub_sec->reloc_count
11385 += 1 + (PPC_HA (off) != 0);
11386 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11387 }
11388
11389 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11390 {
11391 size = 12;
11392 if (PPC_HA (off) != 0)
11393 size = 16;
11394 }
11395 else
11396 {
11397 size = 16;
11398 if (PPC_HA (off) != 0)
11399 size += 4;
11400
11401 if (PPC_HA (r2off) != 0)
11402 size += 4;
11403 if (PPC_LO (r2off) != 0)
11404 size += 4;
11405 }
11406 }
11407 else if (info->emitrelocations)
11408 {
11409 stub_entry->group->stub_sec->reloc_count += 1;
11410 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11411 }
11412 }
11413
11414 stub_entry->group->stub_sec->size += size;
11415 return TRUE;
11416 }
11417
11418 /* Set up various things so that we can make a list of input sections
11419 for each output section included in the link. Returns -1 on error,
11420 0 when no stubs will be needed, and 1 on success. */
11421
11422 int
11423 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11424 {
11425 unsigned int id;
11426 bfd_size_type amt;
11427 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11428
11429 if (htab == NULL)
11430 return -1;
11431
11432 htab->sec_info_arr_size = bfd_get_next_section_id ();
11433 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11434 htab->sec_info = bfd_zmalloc (amt);
11435 if (htab->sec_info == NULL)
11436 return -1;
11437
11438 /* Set toc_off for com, und, abs and ind sections. */
11439 for (id = 0; id < 3; id++)
11440 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11441
11442 return 1;
11443 }
11444
11445 /* Set up for first pass at multitoc partitioning. */
11446
11447 void
11448 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11449 {
11450 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11451
11452 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11453 htab->toc_bfd = NULL;
11454 htab->toc_first_sec = NULL;
11455 }
11456
11457 /* The linker repeatedly calls this function for each TOC input section
11458 and linker generated GOT section. Group input bfds such that the toc
11459 within a group is less than 64k in size. */
11460
11461 bfd_boolean
11462 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11463 {
11464 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11465 bfd_vma addr, off, limit;
11466
11467 if (htab == NULL)
11468 return FALSE;
11469
11470 if (!htab->second_toc_pass)
11471 {
11472 /* Keep track of the first .toc or .got section for this input bfd. */
11473 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11474
11475 if (new_bfd)
11476 {
11477 htab->toc_bfd = isec->owner;
11478 htab->toc_first_sec = isec;
11479 }
11480
11481 addr = isec->output_offset + isec->output_section->vma;
11482 off = addr - htab->toc_curr;
11483 limit = 0x80008000;
11484 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11485 limit = 0x10000;
11486 if (off + isec->size > limit)
11487 {
11488 addr = (htab->toc_first_sec->output_offset
11489 + htab->toc_first_sec->output_section->vma);
11490 htab->toc_curr = addr;
11491 htab->toc_curr &= -TOC_BASE_ALIGN;
11492 }
11493
11494 /* toc_curr is the base address of this toc group. Set elf_gp
11495 for the input section to be the offset relative to the
11496 output toc base plus 0x8000. Making the input elf_gp an
11497 offset allows us to move the toc as a whole without
11498 recalculating input elf_gp. */
11499 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11500 off += TOC_BASE_OFF;
11501
11502 /* Die if someone uses a linker script that doesn't keep input
11503 file .toc and .got together. */
11504 if (new_bfd
11505 && elf_gp (isec->owner) != 0
11506 && elf_gp (isec->owner) != off)
11507 return FALSE;
11508
11509 elf_gp (isec->owner) = off;
11510 return TRUE;
11511 }
11512
11513 /* During the second pass toc_first_sec points to the start of
11514 a toc group, and toc_curr is used to track the old elf_gp.
11515 We use toc_bfd to ensure we only look at each bfd once. */
11516 if (htab->toc_bfd == isec->owner)
11517 return TRUE;
11518 htab->toc_bfd = isec->owner;
11519
11520 if (htab->toc_first_sec == NULL
11521 || htab->toc_curr != elf_gp (isec->owner))
11522 {
11523 htab->toc_curr = elf_gp (isec->owner);
11524 htab->toc_first_sec = isec;
11525 }
11526 addr = (htab->toc_first_sec->output_offset
11527 + htab->toc_first_sec->output_section->vma);
11528 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11529 elf_gp (isec->owner) = off;
11530
11531 return TRUE;
11532 }
11533
11534 /* Called via elf_link_hash_traverse to merge GOT entries for global
11535 symbol H. */
11536
11537 static bfd_boolean
11538 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11539 {
11540 if (h->root.type == bfd_link_hash_indirect)
11541 return TRUE;
11542
11543 merge_got_entries (&h->got.glist);
11544
11545 return TRUE;
11546 }
11547
11548 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11549 symbol H. */
11550
11551 static bfd_boolean
11552 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11553 {
11554 struct got_entry *gent;
11555
11556 if (h->root.type == bfd_link_hash_indirect)
11557 return TRUE;
11558
11559 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11560 if (!gent->is_indirect)
11561 allocate_got (h, (struct bfd_link_info *) inf, gent);
11562 return TRUE;
11563 }
11564
11565 /* Called on the first multitoc pass after the last call to
11566 ppc64_elf_next_toc_section. This function removes duplicate GOT
11567 entries. */
11568
11569 bfd_boolean
11570 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11571 {
11572 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11573 struct bfd *ibfd, *ibfd2;
11574 bfd_boolean done_something;
11575
11576 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11577
11578 if (!htab->do_multi_toc)
11579 return FALSE;
11580
11581 /* Merge global sym got entries within a toc group. */
11582 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11583
11584 /* And tlsld_got. */
11585 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11586 {
11587 struct got_entry *ent, *ent2;
11588
11589 if (!is_ppc64_elf (ibfd))
11590 continue;
11591
11592 ent = ppc64_tlsld_got (ibfd);
11593 if (!ent->is_indirect
11594 && ent->got.offset != (bfd_vma) -1)
11595 {
11596 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11597 {
11598 if (!is_ppc64_elf (ibfd2))
11599 continue;
11600
11601 ent2 = ppc64_tlsld_got (ibfd2);
11602 if (!ent2->is_indirect
11603 && ent2->got.offset != (bfd_vma) -1
11604 && elf_gp (ibfd2) == elf_gp (ibfd))
11605 {
11606 ent2->is_indirect = TRUE;
11607 ent2->got.ent = ent;
11608 }
11609 }
11610 }
11611 }
11612
11613 /* Zap sizes of got sections. */
11614 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11615 htab->elf.irelplt->size -= htab->got_reli_size;
11616 htab->got_reli_size = 0;
11617
11618 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11619 {
11620 asection *got, *relgot;
11621
11622 if (!is_ppc64_elf (ibfd))
11623 continue;
11624
11625 got = ppc64_elf_tdata (ibfd)->got;
11626 if (got != NULL)
11627 {
11628 got->rawsize = got->size;
11629 got->size = 0;
11630 relgot = ppc64_elf_tdata (ibfd)->relgot;
11631 relgot->rawsize = relgot->size;
11632 relgot->size = 0;
11633 }
11634 }
11635
11636 /* Now reallocate the got, local syms first. We don't need to
11637 allocate section contents again since we never increase size. */
11638 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11639 {
11640 struct got_entry **lgot_ents;
11641 struct got_entry **end_lgot_ents;
11642 struct plt_entry **local_plt;
11643 struct plt_entry **end_local_plt;
11644 unsigned char *lgot_masks;
11645 bfd_size_type locsymcount;
11646 Elf_Internal_Shdr *symtab_hdr;
11647 asection *s;
11648
11649 if (!is_ppc64_elf (ibfd))
11650 continue;
11651
11652 lgot_ents = elf_local_got_ents (ibfd);
11653 if (!lgot_ents)
11654 continue;
11655
11656 symtab_hdr = &elf_symtab_hdr (ibfd);
11657 locsymcount = symtab_hdr->sh_info;
11658 end_lgot_ents = lgot_ents + locsymcount;
11659 local_plt = (struct plt_entry **) end_lgot_ents;
11660 end_local_plt = local_plt + locsymcount;
11661 lgot_masks = (unsigned char *) end_local_plt;
11662 s = ppc64_elf_tdata (ibfd)->got;
11663 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11664 {
11665 struct got_entry *ent;
11666
11667 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11668 {
11669 unsigned int ent_size = 8;
11670 unsigned int rel_size = sizeof (Elf64_External_Rela);
11671
11672 ent->got.offset = s->size;
11673 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11674 {
11675 ent_size *= 2;
11676 rel_size *= 2;
11677 }
11678 s->size += ent_size;
11679 if ((*lgot_masks & PLT_IFUNC) != 0)
11680 {
11681 htab->elf.irelplt->size += rel_size;
11682 htab->got_reli_size += rel_size;
11683 }
11684 else if (bfd_link_pic (info))
11685 {
11686 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11687 srel->size += rel_size;
11688 }
11689 }
11690 }
11691 }
11692
11693 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11694
11695 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11696 {
11697 struct got_entry *ent;
11698
11699 if (!is_ppc64_elf (ibfd))
11700 continue;
11701
11702 ent = ppc64_tlsld_got (ibfd);
11703 if (!ent->is_indirect
11704 && ent->got.offset != (bfd_vma) -1)
11705 {
11706 asection *s = ppc64_elf_tdata (ibfd)->got;
11707 ent->got.offset = s->size;
11708 s->size += 16;
11709 if (bfd_link_pic (info))
11710 {
11711 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11712 srel->size += sizeof (Elf64_External_Rela);
11713 }
11714 }
11715 }
11716
11717 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11718 if (!done_something)
11719 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11720 {
11721 asection *got;
11722
11723 if (!is_ppc64_elf (ibfd))
11724 continue;
11725
11726 got = ppc64_elf_tdata (ibfd)->got;
11727 if (got != NULL)
11728 {
11729 done_something = got->rawsize != got->size;
11730 if (done_something)
11731 break;
11732 }
11733 }
11734
11735 if (done_something)
11736 (*htab->params->layout_sections_again) ();
11737
11738 /* Set up for second pass over toc sections to recalculate elf_gp
11739 on input sections. */
11740 htab->toc_bfd = NULL;
11741 htab->toc_first_sec = NULL;
11742 htab->second_toc_pass = TRUE;
11743 return done_something;
11744 }
11745
11746 /* Called after second pass of multitoc partitioning. */
11747
11748 void
11749 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11750 {
11751 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11752
11753 /* After the second pass, toc_curr tracks the TOC offset used
11754 for code sections below in ppc64_elf_next_input_section. */
11755 htab->toc_curr = TOC_BASE_OFF;
11756 }
11757
11758 /* No toc references were found in ISEC. If the code in ISEC makes no
11759 calls, then there's no need to use toc adjusting stubs when branching
11760 into ISEC. Actually, indirect calls from ISEC are OK as they will
11761 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11762 needed, and 2 if a cyclical call-graph was found but no other reason
11763 for a stub was detected. If called from the top level, a return of
11764 2 means the same as a return of 0. */
11765
11766 static int
11767 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11768 {
11769 int ret;
11770
11771 /* Mark this section as checked. */
11772 isec->call_check_done = 1;
11773
11774 /* We know none of our code bearing sections will need toc stubs. */
11775 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11776 return 0;
11777
11778 if (isec->size == 0)
11779 return 0;
11780
11781 if (isec->output_section == NULL)
11782 return 0;
11783
11784 ret = 0;
11785 if (isec->reloc_count != 0)
11786 {
11787 Elf_Internal_Rela *relstart, *rel;
11788 Elf_Internal_Sym *local_syms;
11789 struct ppc_link_hash_table *htab;
11790
11791 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11792 info->keep_memory);
11793 if (relstart == NULL)
11794 return -1;
11795
11796 /* Look for branches to outside of this section. */
11797 local_syms = NULL;
11798 htab = ppc_hash_table (info);
11799 if (htab == NULL)
11800 return -1;
11801
11802 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11803 {
11804 enum elf_ppc64_reloc_type r_type;
11805 unsigned long r_symndx;
11806 struct elf_link_hash_entry *h;
11807 struct ppc_link_hash_entry *eh;
11808 Elf_Internal_Sym *sym;
11809 asection *sym_sec;
11810 struct _opd_sec_data *opd;
11811 bfd_vma sym_value;
11812 bfd_vma dest;
11813
11814 r_type = ELF64_R_TYPE (rel->r_info);
11815 if (r_type != R_PPC64_REL24
11816 && r_type != R_PPC64_REL14
11817 && r_type != R_PPC64_REL14_BRTAKEN
11818 && r_type != R_PPC64_REL14_BRNTAKEN)
11819 continue;
11820
11821 r_symndx = ELF64_R_SYM (rel->r_info);
11822 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11823 isec->owner))
11824 {
11825 ret = -1;
11826 break;
11827 }
11828
11829 /* Calls to dynamic lib functions go through a plt call stub
11830 that uses r2. */
11831 eh = (struct ppc_link_hash_entry *) h;
11832 if (eh != NULL
11833 && (eh->elf.plt.plist != NULL
11834 || (eh->oh != NULL
11835 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11836 {
11837 ret = 1;
11838 break;
11839 }
11840
11841 if (sym_sec == NULL)
11842 /* Ignore other undefined symbols. */
11843 continue;
11844
11845 /* Assume branches to other sections not included in the
11846 link need stubs too, to cover -R and absolute syms. */
11847 if (sym_sec->output_section == NULL)
11848 {
11849 ret = 1;
11850 break;
11851 }
11852
11853 if (h == NULL)
11854 sym_value = sym->st_value;
11855 else
11856 {
11857 if (h->root.type != bfd_link_hash_defined
11858 && h->root.type != bfd_link_hash_defweak)
11859 abort ();
11860 sym_value = h->root.u.def.value;
11861 }
11862 sym_value += rel->r_addend;
11863
11864 /* If this branch reloc uses an opd sym, find the code section. */
11865 opd = get_opd_info (sym_sec);
11866 if (opd != NULL)
11867 {
11868 if (h == NULL && opd->adjust != NULL)
11869 {
11870 long adjust;
11871
11872 adjust = opd->adjust[OPD_NDX (sym_value)];
11873 if (adjust == -1)
11874 /* Assume deleted functions won't ever be called. */
11875 continue;
11876 sym_value += adjust;
11877 }
11878
11879 dest = opd_entry_value (sym_sec, sym_value,
11880 &sym_sec, NULL, FALSE);
11881 if (dest == (bfd_vma) -1)
11882 continue;
11883 }
11884 else
11885 dest = (sym_value
11886 + sym_sec->output_offset
11887 + sym_sec->output_section->vma);
11888
11889 /* Ignore branch to self. */
11890 if (sym_sec == isec)
11891 continue;
11892
11893 /* If the called function uses the toc, we need a stub. */
11894 if (sym_sec->has_toc_reloc
11895 || sym_sec->makes_toc_func_call)
11896 {
11897 ret = 1;
11898 break;
11899 }
11900
11901 /* Assume any branch that needs a long branch stub might in fact
11902 need a plt_branch stub. A plt_branch stub uses r2. */
11903 else if (dest - (isec->output_offset
11904 + isec->output_section->vma
11905 + rel->r_offset) + (1 << 25)
11906 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11907 ? h->other
11908 : sym->st_other))
11909 {
11910 ret = 1;
11911 break;
11912 }
11913
11914 /* If calling back to a section in the process of being
11915 tested, we can't say for sure that no toc adjusting stubs
11916 are needed, so don't return zero. */
11917 else if (sym_sec->call_check_in_progress)
11918 ret = 2;
11919
11920 /* Branches to another section that itself doesn't have any TOC
11921 references are OK. Recursively call ourselves to check. */
11922 else if (!sym_sec->call_check_done)
11923 {
11924 int recur;
11925
11926 /* Mark current section as indeterminate, so that other
11927 sections that call back to current won't be marked as
11928 known. */
11929 isec->call_check_in_progress = 1;
11930 recur = toc_adjusting_stub_needed (info, sym_sec);
11931 isec->call_check_in_progress = 0;
11932
11933 if (recur != 0)
11934 {
11935 ret = recur;
11936 if (recur != 2)
11937 break;
11938 }
11939 }
11940 }
11941
11942 if (local_syms != NULL
11943 && (elf_symtab_hdr (isec->owner).contents
11944 != (unsigned char *) local_syms))
11945 free (local_syms);
11946 if (elf_section_data (isec)->relocs != relstart)
11947 free (relstart);
11948 }
11949
11950 if ((ret & 1) == 0
11951 && isec->map_head.s != NULL
11952 && (strcmp (isec->output_section->name, ".init") == 0
11953 || strcmp (isec->output_section->name, ".fini") == 0))
11954 {
11955 if (isec->map_head.s->has_toc_reloc
11956 || isec->map_head.s->makes_toc_func_call)
11957 ret = 1;
11958 else if (!isec->map_head.s->call_check_done)
11959 {
11960 int recur;
11961 isec->call_check_in_progress = 1;
11962 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11963 isec->call_check_in_progress = 0;
11964 if (recur != 0)
11965 ret = recur;
11966 }
11967 }
11968
11969 if (ret == 1)
11970 isec->makes_toc_func_call = 1;
11971
11972 return ret;
11973 }
11974
11975 /* The linker repeatedly calls this function for each input section,
11976 in the order that input sections are linked into output sections.
11977 Build lists of input sections to determine groupings between which
11978 we may insert linker stubs. */
11979
11980 bfd_boolean
11981 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11982 {
11983 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11984
11985 if (htab == NULL)
11986 return FALSE;
11987
11988 if ((isec->output_section->flags & SEC_CODE) != 0
11989 && isec->output_section->id < htab->sec_info_arr_size)
11990 {
11991 /* This happens to make the list in reverse order,
11992 which is what we want. */
11993 htab->sec_info[isec->id].u.list
11994 = htab->sec_info[isec->output_section->id].u.list;
11995 htab->sec_info[isec->output_section->id].u.list = isec;
11996 }
11997
11998 if (htab->multi_toc_needed)
11999 {
12000 /* Analyse sections that aren't already flagged as needing a
12001 valid toc pointer. Exclude .fixup for the linux kernel.
12002 .fixup contains branches, but only back to the function that
12003 hit an exception. */
12004 if (!(isec->has_toc_reloc
12005 || (isec->flags & SEC_CODE) == 0
12006 || strcmp (isec->name, ".fixup") == 0
12007 || isec->call_check_done))
12008 {
12009 if (toc_adjusting_stub_needed (info, isec) < 0)
12010 return FALSE;
12011 }
12012 /* Make all sections use the TOC assigned for this object file.
12013 This will be wrong for pasted sections; We fix that in
12014 check_pasted_section(). */
12015 if (elf_gp (isec->owner) != 0)
12016 htab->toc_curr = elf_gp (isec->owner);
12017 }
12018
12019 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12020 return TRUE;
12021 }
12022
12023 /* Check that all .init and .fini sections use the same toc, if they
12024 have toc relocs. */
12025
12026 static bfd_boolean
12027 check_pasted_section (struct bfd_link_info *info, const char *name)
12028 {
12029 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12030
12031 if (o != NULL)
12032 {
12033 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12034 bfd_vma toc_off = 0;
12035 asection *i;
12036
12037 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12038 if (i->has_toc_reloc)
12039 {
12040 if (toc_off == 0)
12041 toc_off = htab->sec_info[i->id].toc_off;
12042 else if (toc_off != htab->sec_info[i->id].toc_off)
12043 return FALSE;
12044 }
12045
12046 if (toc_off == 0)
12047 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12048 if (i->makes_toc_func_call)
12049 {
12050 toc_off = htab->sec_info[i->id].toc_off;
12051 break;
12052 }
12053
12054 /* Make sure the whole pasted function uses the same toc offset. */
12055 if (toc_off != 0)
12056 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12057 htab->sec_info[i->id].toc_off = toc_off;
12058 }
12059 return TRUE;
12060 }
12061
12062 bfd_boolean
12063 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12064 {
12065 return (check_pasted_section (info, ".init")
12066 & check_pasted_section (info, ".fini"));
12067 }
12068
12069 /* See whether we can group stub sections together. Grouping stub
12070 sections may result in fewer stubs. More importantly, we need to
12071 put all .init* and .fini* stubs at the beginning of the .init or
12072 .fini output sections respectively, because glibc splits the
12073 _init and _fini functions into multiple parts. Putting a stub in
12074 the middle of a function is not a good idea. */
12075
12076 static bfd_boolean
12077 group_sections (struct bfd_link_info *info,
12078 bfd_size_type stub_group_size,
12079 bfd_boolean stubs_always_before_branch)
12080 {
12081 struct ppc_link_hash_table *htab;
12082 asection *osec;
12083 bfd_boolean suppress_size_errors;
12084
12085 htab = ppc_hash_table (info);
12086 if (htab == NULL)
12087 return FALSE;
12088
12089 suppress_size_errors = FALSE;
12090 if (stub_group_size == 1)
12091 {
12092 /* Default values. */
12093 if (stubs_always_before_branch)
12094 stub_group_size = 0x1e00000;
12095 else
12096 stub_group_size = 0x1c00000;
12097 suppress_size_errors = TRUE;
12098 }
12099
12100 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12101 {
12102 asection *tail;
12103
12104 if (osec->id >= htab->sec_info_arr_size)
12105 continue;
12106
12107 tail = htab->sec_info[osec->id].u.list;
12108 while (tail != NULL)
12109 {
12110 asection *curr;
12111 asection *prev;
12112 bfd_size_type total;
12113 bfd_boolean big_sec;
12114 bfd_vma curr_toc;
12115 struct map_stub *group;
12116 bfd_size_type group_size;
12117
12118 curr = tail;
12119 total = tail->size;
12120 group_size = (ppc64_elf_section_data (tail) != NULL
12121 && ppc64_elf_section_data (tail)->has_14bit_branch
12122 ? stub_group_size >> 10 : stub_group_size);
12123
12124 big_sec = total > group_size;
12125 if (big_sec && !suppress_size_errors)
12126 /* xgettext:c-format */
12127 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12128 tail->owner, tail);
12129 curr_toc = htab->sec_info[tail->id].toc_off;
12130
12131 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12132 && ((total += curr->output_offset - prev->output_offset)
12133 < (ppc64_elf_section_data (prev) != NULL
12134 && ppc64_elf_section_data (prev)->has_14bit_branch
12135 ? (group_size = stub_group_size >> 10) : group_size))
12136 && htab->sec_info[prev->id].toc_off == curr_toc)
12137 curr = prev;
12138
12139 /* OK, the size from the start of CURR to the end is less
12140 than group_size and thus can be handled by one stub
12141 section. (or the tail section is itself larger than
12142 group_size, in which case we may be toast.) We should
12143 really be keeping track of the total size of stubs added
12144 here, as stubs contribute to the final output section
12145 size. That's a little tricky, and this way will only
12146 break if stubs added make the total size more than 2^25,
12147 ie. for the default stub_group_size, if stubs total more
12148 than 2097152 bytes, or nearly 75000 plt call stubs. */
12149 group = bfd_alloc (curr->owner, sizeof (*group));
12150 if (group == NULL)
12151 return FALSE;
12152 group->link_sec = curr;
12153 group->stub_sec = NULL;
12154 group->needs_save_res = 0;
12155 group->next = htab->group;
12156 htab->group = group;
12157 do
12158 {
12159 prev = htab->sec_info[tail->id].u.list;
12160 /* Set up this stub group. */
12161 htab->sec_info[tail->id].u.group = group;
12162 }
12163 while (tail != curr && (tail = prev) != NULL);
12164
12165 /* But wait, there's more! Input sections up to group_size
12166 bytes before the stub section can be handled by it too.
12167 Don't do this if we have a really large section after the
12168 stubs, as adding more stubs increases the chance that
12169 branches may not reach into the stub section. */
12170 if (!stubs_always_before_branch && !big_sec)
12171 {
12172 total = 0;
12173 while (prev != NULL
12174 && ((total += tail->output_offset - prev->output_offset)
12175 < (ppc64_elf_section_data (prev) != NULL
12176 && ppc64_elf_section_data (prev)->has_14bit_branch
12177 ? (group_size = stub_group_size >> 10) : group_size))
12178 && htab->sec_info[prev->id].toc_off == curr_toc)
12179 {
12180 tail = prev;
12181 prev = htab->sec_info[tail->id].u.list;
12182 htab->sec_info[tail->id].u.group = group;
12183 }
12184 }
12185 tail = prev;
12186 }
12187 }
12188 return TRUE;
12189 }
12190
12191 static const unsigned char glink_eh_frame_cie[] =
12192 {
12193 0, 0, 0, 16, /* length. */
12194 0, 0, 0, 0, /* id. */
12195 1, /* CIE version. */
12196 'z', 'R', 0, /* Augmentation string. */
12197 4, /* Code alignment. */
12198 0x78, /* Data alignment. */
12199 65, /* RA reg. */
12200 1, /* Augmentation size. */
12201 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12202 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12203 0, 0, 0, 0
12204 };
12205
12206 /* Stripping output sections is normally done before dynamic section
12207 symbols have been allocated. This function is called later, and
12208 handles cases like htab->brlt which is mapped to its own output
12209 section. */
12210
12211 static void
12212 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12213 {
12214 if (isec->size == 0
12215 && isec->output_section->size == 0
12216 && !(isec->output_section->flags & SEC_KEEP)
12217 && !bfd_section_removed_from_list (info->output_bfd,
12218 isec->output_section)
12219 && elf_section_data (isec->output_section)->dynindx == 0)
12220 {
12221 isec->output_section->flags |= SEC_EXCLUDE;
12222 bfd_section_list_remove (info->output_bfd, isec->output_section);
12223 info->output_bfd->section_count--;
12224 }
12225 }
12226
12227 /* Determine and set the size of the stub section for a final link.
12228
12229 The basic idea here is to examine all the relocations looking for
12230 PC-relative calls to a target that is unreachable with a "bl"
12231 instruction. */
12232
12233 bfd_boolean
12234 ppc64_elf_size_stubs (struct bfd_link_info *info)
12235 {
12236 bfd_size_type stub_group_size;
12237 bfd_boolean stubs_always_before_branch;
12238 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12239
12240 if (htab == NULL)
12241 return FALSE;
12242
12243 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12244 htab->params->plt_thread_safe = 1;
12245 if (!htab->opd_abi)
12246 htab->params->plt_thread_safe = 0;
12247 else if (htab->params->plt_thread_safe == -1)
12248 {
12249 static const char *const thread_starter[] =
12250 {
12251 "pthread_create",
12252 /* libstdc++ */
12253 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12254 /* librt */
12255 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12256 "mq_notify", "create_timer",
12257 /* libanl */
12258 "getaddrinfo_a",
12259 /* libgomp */
12260 "GOMP_parallel",
12261 "GOMP_parallel_start",
12262 "GOMP_parallel_loop_static",
12263 "GOMP_parallel_loop_static_start",
12264 "GOMP_parallel_loop_dynamic",
12265 "GOMP_parallel_loop_dynamic_start",
12266 "GOMP_parallel_loop_guided",
12267 "GOMP_parallel_loop_guided_start",
12268 "GOMP_parallel_loop_runtime",
12269 "GOMP_parallel_loop_runtime_start",
12270 "GOMP_parallel_sections",
12271 "GOMP_parallel_sections_start",
12272 /* libgo */
12273 "__go_go",
12274 };
12275 unsigned i;
12276
12277 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12278 {
12279 struct elf_link_hash_entry *h;
12280 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12281 FALSE, FALSE, TRUE);
12282 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12283 if (htab->params->plt_thread_safe)
12284 break;
12285 }
12286 }
12287 stubs_always_before_branch = htab->params->group_size < 0;
12288 if (htab->params->group_size < 0)
12289 stub_group_size = -htab->params->group_size;
12290 else
12291 stub_group_size = htab->params->group_size;
12292
12293 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12294 return FALSE;
12295
12296 #define STUB_SHRINK_ITER 20
12297 /* Loop until no stubs added. After iteration 20 of this loop we may
12298 exit on a stub section shrinking. This is to break out of a
12299 pathological case where adding stubs on one iteration decreases
12300 section gaps (perhaps due to alignment), which then requires
12301 fewer or smaller stubs on the next iteration. */
12302
12303 while (1)
12304 {
12305 bfd *input_bfd;
12306 unsigned int bfd_indx;
12307 struct map_stub *group;
12308 asection *stub_sec;
12309
12310 htab->stub_iteration += 1;
12311
12312 for (input_bfd = info->input_bfds, bfd_indx = 0;
12313 input_bfd != NULL;
12314 input_bfd = input_bfd->link.next, bfd_indx++)
12315 {
12316 Elf_Internal_Shdr *symtab_hdr;
12317 asection *section;
12318 Elf_Internal_Sym *local_syms = NULL;
12319
12320 if (!is_ppc64_elf (input_bfd))
12321 continue;
12322
12323 /* We'll need the symbol table in a second. */
12324 symtab_hdr = &elf_symtab_hdr (input_bfd);
12325 if (symtab_hdr->sh_info == 0)
12326 continue;
12327
12328 /* Walk over each section attached to the input bfd. */
12329 for (section = input_bfd->sections;
12330 section != NULL;
12331 section = section->next)
12332 {
12333 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12334
12335 /* If there aren't any relocs, then there's nothing more
12336 to do. */
12337 if ((section->flags & SEC_RELOC) == 0
12338 || (section->flags & SEC_ALLOC) == 0
12339 || (section->flags & SEC_LOAD) == 0
12340 || (section->flags & SEC_CODE) == 0
12341 || section->reloc_count == 0)
12342 continue;
12343
12344 /* If this section is a link-once section that will be
12345 discarded, then don't create any stubs. */
12346 if (section->output_section == NULL
12347 || section->output_section->owner != info->output_bfd)
12348 continue;
12349
12350 /* Get the relocs. */
12351 internal_relocs
12352 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12353 info->keep_memory);
12354 if (internal_relocs == NULL)
12355 goto error_ret_free_local;
12356
12357 /* Now examine each relocation. */
12358 irela = internal_relocs;
12359 irelaend = irela + section->reloc_count;
12360 for (; irela < irelaend; irela++)
12361 {
12362 enum elf_ppc64_reloc_type r_type;
12363 unsigned int r_indx;
12364 enum ppc_stub_type stub_type;
12365 struct ppc_stub_hash_entry *stub_entry;
12366 asection *sym_sec, *code_sec;
12367 bfd_vma sym_value, code_value;
12368 bfd_vma destination;
12369 unsigned long local_off;
12370 bfd_boolean ok_dest;
12371 struct ppc_link_hash_entry *hash;
12372 struct ppc_link_hash_entry *fdh;
12373 struct elf_link_hash_entry *h;
12374 Elf_Internal_Sym *sym;
12375 char *stub_name;
12376 const asection *id_sec;
12377 struct _opd_sec_data *opd;
12378 struct plt_entry *plt_ent;
12379
12380 r_type = ELF64_R_TYPE (irela->r_info);
12381 r_indx = ELF64_R_SYM (irela->r_info);
12382
12383 if (r_type >= R_PPC64_max)
12384 {
12385 bfd_set_error (bfd_error_bad_value);
12386 goto error_ret_free_internal;
12387 }
12388
12389 /* Only look for stubs on branch instructions. */
12390 if (r_type != R_PPC64_REL24
12391 && r_type != R_PPC64_REL14
12392 && r_type != R_PPC64_REL14_BRTAKEN
12393 && r_type != R_PPC64_REL14_BRNTAKEN)
12394 continue;
12395
12396 /* Now determine the call target, its name, value,
12397 section. */
12398 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12399 r_indx, input_bfd))
12400 goto error_ret_free_internal;
12401 hash = (struct ppc_link_hash_entry *) h;
12402
12403 ok_dest = FALSE;
12404 fdh = NULL;
12405 sym_value = 0;
12406 if (hash == NULL)
12407 {
12408 sym_value = sym->st_value;
12409 if (sym_sec != NULL
12410 && sym_sec->output_section != NULL)
12411 ok_dest = TRUE;
12412 }
12413 else if (hash->elf.root.type == bfd_link_hash_defined
12414 || hash->elf.root.type == bfd_link_hash_defweak)
12415 {
12416 sym_value = hash->elf.root.u.def.value;
12417 if (sym_sec->output_section != NULL)
12418 ok_dest = TRUE;
12419 }
12420 else if (hash->elf.root.type == bfd_link_hash_undefweak
12421 || hash->elf.root.type == bfd_link_hash_undefined)
12422 {
12423 /* Recognise an old ABI func code entry sym, and
12424 use the func descriptor sym instead if it is
12425 defined. */
12426 if (hash->elf.root.root.string[0] == '.'
12427 && (fdh = lookup_fdh (hash, htab)) != NULL)
12428 {
12429 if (fdh->elf.root.type == bfd_link_hash_defined
12430 || fdh->elf.root.type == bfd_link_hash_defweak)
12431 {
12432 sym_sec = fdh->elf.root.u.def.section;
12433 sym_value = fdh->elf.root.u.def.value;
12434 if (sym_sec->output_section != NULL)
12435 ok_dest = TRUE;
12436 }
12437 else
12438 fdh = NULL;
12439 }
12440 }
12441 else
12442 {
12443 bfd_set_error (bfd_error_bad_value);
12444 goto error_ret_free_internal;
12445 }
12446
12447 destination = 0;
12448 local_off = 0;
12449 if (ok_dest)
12450 {
12451 sym_value += irela->r_addend;
12452 destination = (sym_value
12453 + sym_sec->output_offset
12454 + sym_sec->output_section->vma);
12455 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12456 ? hash->elf.other
12457 : sym->st_other);
12458 }
12459
12460 code_sec = sym_sec;
12461 code_value = sym_value;
12462 opd = get_opd_info (sym_sec);
12463 if (opd != NULL)
12464 {
12465 bfd_vma dest;
12466
12467 if (hash == NULL && opd->adjust != NULL)
12468 {
12469 long adjust = opd->adjust[OPD_NDX (sym_value)];
12470 if (adjust == -1)
12471 continue;
12472 code_value += adjust;
12473 sym_value += adjust;
12474 }
12475 dest = opd_entry_value (sym_sec, sym_value,
12476 &code_sec, &code_value, FALSE);
12477 if (dest != (bfd_vma) -1)
12478 {
12479 destination = dest;
12480 if (fdh != NULL)
12481 {
12482 /* Fixup old ABI sym to point at code
12483 entry. */
12484 hash->elf.root.type = bfd_link_hash_defweak;
12485 hash->elf.root.u.def.section = code_sec;
12486 hash->elf.root.u.def.value = code_value;
12487 }
12488 }
12489 }
12490
12491 /* Determine what (if any) linker stub is needed. */
12492 plt_ent = NULL;
12493 stub_type = ppc_type_of_stub (section, irela, &hash,
12494 &plt_ent, destination,
12495 local_off);
12496
12497 if (stub_type != ppc_stub_plt_call)
12498 {
12499 /* Check whether we need a TOC adjusting stub.
12500 Since the linker pastes together pieces from
12501 different object files when creating the
12502 _init and _fini functions, it may be that a
12503 call to what looks like a local sym is in
12504 fact a call needing a TOC adjustment. */
12505 if (code_sec != NULL
12506 && code_sec->output_section != NULL
12507 && (htab->sec_info[code_sec->id].toc_off
12508 != htab->sec_info[section->id].toc_off)
12509 && (code_sec->has_toc_reloc
12510 || code_sec->makes_toc_func_call))
12511 stub_type = ppc_stub_long_branch_r2off;
12512 }
12513
12514 if (stub_type == ppc_stub_none)
12515 continue;
12516
12517 /* __tls_get_addr calls might be eliminated. */
12518 if (stub_type != ppc_stub_plt_call
12519 && hash != NULL
12520 && (hash == htab->tls_get_addr
12521 || hash == htab->tls_get_addr_fd)
12522 && section->has_tls_reloc
12523 && irela != internal_relocs)
12524 {
12525 /* Get tls info. */
12526 unsigned char *tls_mask;
12527
12528 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12529 irela - 1, input_bfd))
12530 goto error_ret_free_internal;
12531 if (*tls_mask != 0)
12532 continue;
12533 }
12534
12535 if (stub_type == ppc_stub_plt_call
12536 && irela + 1 < irelaend
12537 && irela[1].r_offset == irela->r_offset + 4
12538 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12539 {
12540 if (!tocsave_find (htab, INSERT,
12541 &local_syms, irela + 1, input_bfd))
12542 goto error_ret_free_internal;
12543 }
12544 else if (stub_type == ppc_stub_plt_call)
12545 stub_type = ppc_stub_plt_call_r2save;
12546
12547 /* Support for grouping stub sections. */
12548 id_sec = htab->sec_info[section->id].u.group->link_sec;
12549
12550 /* Get the name of this stub. */
12551 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12552 if (!stub_name)
12553 goto error_ret_free_internal;
12554
12555 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12556 stub_name, FALSE, FALSE);
12557 if (stub_entry != NULL)
12558 {
12559 /* The proper stub has already been created. */
12560 free (stub_name);
12561 if (stub_type == ppc_stub_plt_call_r2save)
12562 stub_entry->stub_type = stub_type;
12563 continue;
12564 }
12565
12566 stub_entry = ppc_add_stub (stub_name, section, info);
12567 if (stub_entry == NULL)
12568 {
12569 free (stub_name);
12570 error_ret_free_internal:
12571 if (elf_section_data (section)->relocs == NULL)
12572 free (internal_relocs);
12573 error_ret_free_local:
12574 if (local_syms != NULL
12575 && (symtab_hdr->contents
12576 != (unsigned char *) local_syms))
12577 free (local_syms);
12578 return FALSE;
12579 }
12580
12581 stub_entry->stub_type = stub_type;
12582 if (stub_type != ppc_stub_plt_call
12583 && stub_type != ppc_stub_plt_call_r2save)
12584 {
12585 stub_entry->target_value = code_value;
12586 stub_entry->target_section = code_sec;
12587 }
12588 else
12589 {
12590 stub_entry->target_value = sym_value;
12591 stub_entry->target_section = sym_sec;
12592 }
12593 stub_entry->h = hash;
12594 stub_entry->plt_ent = plt_ent;
12595 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12596
12597 if (stub_entry->h != NULL)
12598 htab->stub_globals += 1;
12599 }
12600
12601 /* We're done with the internal relocs, free them. */
12602 if (elf_section_data (section)->relocs != internal_relocs)
12603 free (internal_relocs);
12604 }
12605
12606 if (local_syms != NULL
12607 && symtab_hdr->contents != (unsigned char *) local_syms)
12608 {
12609 if (!info->keep_memory)
12610 free (local_syms);
12611 else
12612 symtab_hdr->contents = (unsigned char *) local_syms;
12613 }
12614 }
12615
12616 /* We may have added some stubs. Find out the new size of the
12617 stub sections. */
12618 for (stub_sec = htab->params->stub_bfd->sections;
12619 stub_sec != NULL;
12620 stub_sec = stub_sec->next)
12621 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12622 {
12623 if (htab->stub_iteration <= STUB_SHRINK_ITER
12624 || stub_sec->rawsize < stub_sec->size)
12625 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12626 stub_sec->rawsize = stub_sec->size;
12627 stub_sec->size = 0;
12628 stub_sec->reloc_count = 0;
12629 stub_sec->flags &= ~SEC_RELOC;
12630 }
12631
12632 htab->brlt->size = 0;
12633 htab->brlt->reloc_count = 0;
12634 htab->brlt->flags &= ~SEC_RELOC;
12635 if (htab->relbrlt != NULL)
12636 htab->relbrlt->size = 0;
12637
12638 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12639
12640 for (group = htab->group; group != NULL; group = group->next)
12641 if (group->needs_save_res)
12642 group->stub_sec->size += htab->sfpr->size;
12643
12644 if (info->emitrelocations
12645 && htab->glink != NULL && htab->glink->size != 0)
12646 {
12647 htab->glink->reloc_count = 1;
12648 htab->glink->flags |= SEC_RELOC;
12649 }
12650
12651 if (htab->glink_eh_frame != NULL
12652 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12653 && htab->glink_eh_frame->output_section->size != 0)
12654 {
12655 size_t size = 0, align;
12656
12657 for (stub_sec = htab->params->stub_bfd->sections;
12658 stub_sec != NULL;
12659 stub_sec = stub_sec->next)
12660 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12661 size += 24;
12662 if (htab->glink != NULL && htab->glink->size != 0)
12663 size += 24;
12664 if (size != 0)
12665 size += sizeof (glink_eh_frame_cie);
12666 align = 1;
12667 align <<= htab->glink_eh_frame->output_section->alignment_power;
12668 align -= 1;
12669 size = (size + align) & ~align;
12670 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12671 htab->glink_eh_frame->size = size;
12672 }
12673
12674 if (htab->params->plt_stub_align != 0)
12675 for (stub_sec = htab->params->stub_bfd->sections;
12676 stub_sec != NULL;
12677 stub_sec = stub_sec->next)
12678 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12679 stub_sec->size = ((stub_sec->size
12680 + (1 << htab->params->plt_stub_align) - 1)
12681 & -(1 << htab->params->plt_stub_align));
12682
12683 for (stub_sec = htab->params->stub_bfd->sections;
12684 stub_sec != NULL;
12685 stub_sec = stub_sec->next)
12686 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12687 && stub_sec->rawsize != stub_sec->size
12688 && (htab->stub_iteration <= STUB_SHRINK_ITER
12689 || stub_sec->rawsize < stub_sec->size))
12690 break;
12691
12692 if (stub_sec == NULL
12693 && (htab->glink_eh_frame == NULL
12694 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12695 break;
12696
12697 /* Ask the linker to do its stuff. */
12698 (*htab->params->layout_sections_again) ();
12699 }
12700
12701 if (htab->glink_eh_frame != NULL
12702 && htab->glink_eh_frame->size != 0)
12703 {
12704 bfd_vma val;
12705 bfd_byte *p, *last_fde;
12706 size_t last_fde_len, size, align, pad;
12707 asection *stub_sec;
12708
12709 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12710 if (p == NULL)
12711 return FALSE;
12712 htab->glink_eh_frame->contents = p;
12713 last_fde = p;
12714
12715 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12716 /* CIE length (rewrite in case little-endian). */
12717 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12718 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12719 p += sizeof (glink_eh_frame_cie);
12720
12721 for (stub_sec = htab->params->stub_bfd->sections;
12722 stub_sec != NULL;
12723 stub_sec = stub_sec->next)
12724 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12725 {
12726 last_fde = p;
12727 last_fde_len = 20;
12728 /* FDE length. */
12729 bfd_put_32 (htab->elf.dynobj, 20, p);
12730 p += 4;
12731 /* CIE pointer. */
12732 val = p - htab->glink_eh_frame->contents;
12733 bfd_put_32 (htab->elf.dynobj, val, p);
12734 p += 4;
12735 /* Offset to stub section, written later. */
12736 p += 4;
12737 /* stub section size. */
12738 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12739 p += 4;
12740 /* Augmentation. */
12741 p += 1;
12742 /* Pad. */
12743 p += 7;
12744 }
12745 if (htab->glink != NULL && htab->glink->size != 0)
12746 {
12747 last_fde = p;
12748 last_fde_len = 20;
12749 /* FDE length. */
12750 bfd_put_32 (htab->elf.dynobj, 20, p);
12751 p += 4;
12752 /* CIE pointer. */
12753 val = p - htab->glink_eh_frame->contents;
12754 bfd_put_32 (htab->elf.dynobj, val, p);
12755 p += 4;
12756 /* Offset to .glink, written later. */
12757 p += 4;
12758 /* .glink size. */
12759 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12760 p += 4;
12761 /* Augmentation. */
12762 p += 1;
12763
12764 *p++ = DW_CFA_advance_loc + 1;
12765 *p++ = DW_CFA_register;
12766 *p++ = 65;
12767 *p++ = htab->opd_abi ? 12 : 0;
12768 *p++ = DW_CFA_advance_loc + 4;
12769 *p++ = DW_CFA_restore_extended;
12770 *p++ = 65;
12771 }
12772 /* Subsume any padding into the last FDE if user .eh_frame
12773 sections are aligned more than glink_eh_frame. Otherwise any
12774 zero padding will be seen as a terminator. */
12775 size = p - htab->glink_eh_frame->contents;
12776 align = 1;
12777 align <<= htab->glink_eh_frame->output_section->alignment_power;
12778 align -= 1;
12779 pad = ((size + align) & ~align) - size;
12780 htab->glink_eh_frame->size = size + pad;
12781 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12782 }
12783
12784 maybe_strip_output (info, htab->brlt);
12785 if (htab->glink_eh_frame != NULL)
12786 maybe_strip_output (info, htab->glink_eh_frame);
12787
12788 return TRUE;
12789 }
12790
12791 /* Called after we have determined section placement. If sections
12792 move, we'll be called again. Provide a value for TOCstart. */
12793
12794 bfd_vma
12795 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12796 {
12797 asection *s;
12798 bfd_vma TOCstart, adjust;
12799
12800 if (info != NULL)
12801 {
12802 struct elf_link_hash_entry *h;
12803 struct elf_link_hash_table *htab = elf_hash_table (info);
12804
12805 if (is_elf_hash_table (htab)
12806 && htab->hgot != NULL)
12807 h = htab->hgot;
12808 else
12809 {
12810 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12811 if (is_elf_hash_table (htab))
12812 htab->hgot = h;
12813 }
12814 if (h != NULL
12815 && h->root.type == bfd_link_hash_defined
12816 && !h->root.linker_def
12817 && (!is_elf_hash_table (htab)
12818 || h->def_regular))
12819 {
12820 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12821 + h->root.u.def.section->output_offset
12822 + h->root.u.def.section->output_section->vma);
12823 _bfd_set_gp_value (obfd, TOCstart);
12824 return TOCstart;
12825 }
12826 }
12827
12828 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12829 order. The TOC starts where the first of these sections starts. */
12830 s = bfd_get_section_by_name (obfd, ".got");
12831 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12832 s = bfd_get_section_by_name (obfd, ".toc");
12833 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12834 s = bfd_get_section_by_name (obfd, ".tocbss");
12835 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12836 s = bfd_get_section_by_name (obfd, ".plt");
12837 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12838 {
12839 /* This may happen for
12840 o references to TOC base (SYM@toc / TOC[tc0]) without a
12841 .toc directive
12842 o bad linker script
12843 o --gc-sections and empty TOC sections
12844
12845 FIXME: Warn user? */
12846
12847 /* Look for a likely section. We probably won't even be
12848 using TOCstart. */
12849 for (s = obfd->sections; s != NULL; s = s->next)
12850 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12851 | SEC_EXCLUDE))
12852 == (SEC_ALLOC | SEC_SMALL_DATA))
12853 break;
12854 if (s == NULL)
12855 for (s = obfd->sections; s != NULL; s = s->next)
12856 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12857 == (SEC_ALLOC | SEC_SMALL_DATA))
12858 break;
12859 if (s == NULL)
12860 for (s = obfd->sections; s != NULL; s = s->next)
12861 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12862 == SEC_ALLOC)
12863 break;
12864 if (s == NULL)
12865 for (s = obfd->sections; s != NULL; s = s->next)
12866 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12867 break;
12868 }
12869
12870 TOCstart = 0;
12871 if (s != NULL)
12872 TOCstart = s->output_section->vma + s->output_offset;
12873
12874 /* Force alignment. */
12875 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12876 TOCstart -= adjust;
12877 _bfd_set_gp_value (obfd, TOCstart);
12878
12879 if (info != NULL && s != NULL)
12880 {
12881 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12882
12883 if (htab != NULL)
12884 {
12885 if (htab->elf.hgot != NULL)
12886 {
12887 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12888 htab->elf.hgot->root.u.def.section = s;
12889 }
12890 }
12891 else
12892 {
12893 struct bfd_link_hash_entry *bh = NULL;
12894 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12895 s, TOC_BASE_OFF - adjust,
12896 NULL, FALSE, FALSE, &bh);
12897 }
12898 }
12899 return TOCstart;
12900 }
12901
12902 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12903 write out any global entry stubs. */
12904
12905 static bfd_boolean
12906 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12907 {
12908 struct bfd_link_info *info;
12909 struct ppc_link_hash_table *htab;
12910 struct plt_entry *pent;
12911 asection *s;
12912
12913 if (h->root.type == bfd_link_hash_indirect)
12914 return TRUE;
12915
12916 if (!h->pointer_equality_needed)
12917 return TRUE;
12918
12919 if (h->def_regular)
12920 return TRUE;
12921
12922 info = inf;
12923 htab = ppc_hash_table (info);
12924 if (htab == NULL)
12925 return FALSE;
12926
12927 s = htab->glink;
12928 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12929 if (pent->plt.offset != (bfd_vma) -1
12930 && pent->addend == 0)
12931 {
12932 bfd_byte *p;
12933 asection *plt;
12934 bfd_vma off;
12935
12936 p = s->contents + h->root.u.def.value;
12937 plt = htab->elf.splt;
12938 if (!htab->elf.dynamic_sections_created
12939 || h->dynindx == -1)
12940 plt = htab->elf.iplt;
12941 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12942 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12943
12944 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12945 {
12946 info->callbacks->einfo
12947 (_("%P: linkage table error against `%T'\n"),
12948 h->root.root.string);
12949 bfd_set_error (bfd_error_bad_value);
12950 htab->stub_error = TRUE;
12951 }
12952
12953 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12954 if (htab->params->emit_stub_syms)
12955 {
12956 size_t len = strlen (h->root.root.string);
12957 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12958
12959 if (name == NULL)
12960 return FALSE;
12961
12962 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12963 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12964 if (h == NULL)
12965 return FALSE;
12966 if (h->root.type == bfd_link_hash_new)
12967 {
12968 h->root.type = bfd_link_hash_defined;
12969 h->root.u.def.section = s;
12970 h->root.u.def.value = p - s->contents;
12971 h->ref_regular = 1;
12972 h->def_regular = 1;
12973 h->ref_regular_nonweak = 1;
12974 h->forced_local = 1;
12975 h->non_elf = 0;
12976 h->root.linker_def = 1;
12977 }
12978 }
12979
12980 if (PPC_HA (off) != 0)
12981 {
12982 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12983 p += 4;
12984 }
12985 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
12986 p += 4;
12987 bfd_put_32 (s->owner, MTCTR_R12, p);
12988 p += 4;
12989 bfd_put_32 (s->owner, BCTR, p);
12990 break;
12991 }
12992 return TRUE;
12993 }
12994
12995 /* Build all the stubs associated with the current output file.
12996 The stubs are kept in a hash table attached to the main linker
12997 hash table. This function is called via gldelf64ppc_finish. */
12998
12999 bfd_boolean
13000 ppc64_elf_build_stubs (struct bfd_link_info *info,
13001 char **stats)
13002 {
13003 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13004 struct map_stub *group;
13005 asection *stub_sec;
13006 bfd_byte *p;
13007 int stub_sec_count = 0;
13008
13009 if (htab == NULL)
13010 return FALSE;
13011
13012 /* Allocate memory to hold the linker stubs. */
13013 for (stub_sec = htab->params->stub_bfd->sections;
13014 stub_sec != NULL;
13015 stub_sec = stub_sec->next)
13016 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13017 && stub_sec->size != 0)
13018 {
13019 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13020 if (stub_sec->contents == NULL)
13021 return FALSE;
13022 stub_sec->size = 0;
13023 }
13024
13025 if (htab->glink != NULL && htab->glink->size != 0)
13026 {
13027 unsigned int indx;
13028 bfd_vma plt0;
13029
13030 /* Build the .glink plt call stub. */
13031 if (htab->params->emit_stub_syms)
13032 {
13033 struct elf_link_hash_entry *h;
13034 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13035 TRUE, FALSE, FALSE);
13036 if (h == NULL)
13037 return FALSE;
13038 if (h->root.type == bfd_link_hash_new)
13039 {
13040 h->root.type = bfd_link_hash_defined;
13041 h->root.u.def.section = htab->glink;
13042 h->root.u.def.value = 8;
13043 h->ref_regular = 1;
13044 h->def_regular = 1;
13045 h->ref_regular_nonweak = 1;
13046 h->forced_local = 1;
13047 h->non_elf = 0;
13048 h->root.linker_def = 1;
13049 }
13050 }
13051 plt0 = (htab->elf.splt->output_section->vma
13052 + htab->elf.splt->output_offset
13053 - 16);
13054 if (info->emitrelocations)
13055 {
13056 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13057 if (r == NULL)
13058 return FALSE;
13059 r->r_offset = (htab->glink->output_offset
13060 + htab->glink->output_section->vma);
13061 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13062 r->r_addend = plt0;
13063 }
13064 p = htab->glink->contents;
13065 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13066 bfd_put_64 (htab->glink->owner, plt0, p);
13067 p += 8;
13068 if (htab->opd_abi)
13069 {
13070 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13071 p += 4;
13072 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13073 p += 4;
13074 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13075 p += 4;
13076 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13077 p += 4;
13078 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13079 p += 4;
13080 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13081 p += 4;
13082 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13083 p += 4;
13084 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13085 p += 4;
13086 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13087 p += 4;
13088 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13089 p += 4;
13090 }
13091 else
13092 {
13093 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13094 p += 4;
13095 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13096 p += 4;
13097 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13098 p += 4;
13099 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13100 p += 4;
13101 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13102 p += 4;
13103 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13104 p += 4;
13105 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13106 p += 4;
13107 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13108 p += 4;
13109 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13110 p += 4;
13111 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13112 p += 4;
13113 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13114 p += 4;
13115 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13116 p += 4;
13117 }
13118 bfd_put_32 (htab->glink->owner, BCTR, p);
13119 p += 4;
13120 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13121 {
13122 bfd_put_32 (htab->glink->owner, NOP, p);
13123 p += 4;
13124 }
13125
13126 /* Build the .glink lazy link call stubs. */
13127 indx = 0;
13128 while (p < htab->glink->contents + htab->glink->rawsize)
13129 {
13130 if (htab->opd_abi)
13131 {
13132 if (indx < 0x8000)
13133 {
13134 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13135 p += 4;
13136 }
13137 else
13138 {
13139 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13140 p += 4;
13141 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13142 p);
13143 p += 4;
13144 }
13145 }
13146 bfd_put_32 (htab->glink->owner,
13147 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13148 indx++;
13149 p += 4;
13150 }
13151
13152 /* Build .glink global entry stubs. */
13153 if (htab->glink->size > htab->glink->rawsize)
13154 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13155 }
13156
13157 if (htab->brlt != NULL && htab->brlt->size != 0)
13158 {
13159 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13160 htab->brlt->size);
13161 if (htab->brlt->contents == NULL)
13162 return FALSE;
13163 }
13164 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13165 {
13166 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13167 htab->relbrlt->size);
13168 if (htab->relbrlt->contents == NULL)
13169 return FALSE;
13170 }
13171
13172 /* Build the stubs as directed by the stub hash table. */
13173 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13174
13175 for (group = htab->group; group != NULL; group = group->next)
13176 if (group->needs_save_res)
13177 {
13178 stub_sec = group->stub_sec;
13179 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13180 htab->sfpr->size);
13181 if (htab->params->emit_stub_syms)
13182 {
13183 unsigned int i;
13184
13185 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13186 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13187 return FALSE;
13188 }
13189 stub_sec->size += htab->sfpr->size;
13190 }
13191
13192 if (htab->relbrlt != NULL)
13193 htab->relbrlt->reloc_count = 0;
13194
13195 if (htab->params->plt_stub_align != 0)
13196 for (stub_sec = htab->params->stub_bfd->sections;
13197 stub_sec != NULL;
13198 stub_sec = stub_sec->next)
13199 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13200 stub_sec->size = ((stub_sec->size
13201 + (1 << htab->params->plt_stub_align) - 1)
13202 & -(1 << htab->params->plt_stub_align));
13203
13204 for (stub_sec = htab->params->stub_bfd->sections;
13205 stub_sec != NULL;
13206 stub_sec = stub_sec->next)
13207 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13208 {
13209 stub_sec_count += 1;
13210 if (stub_sec->rawsize != stub_sec->size
13211 && (htab->stub_iteration <= STUB_SHRINK_ITER
13212 || stub_sec->rawsize < stub_sec->size))
13213 break;
13214 }
13215
13216 /* Note that the glink_eh_frame check here is not only testing that
13217 the generated size matched the calculated size but also that
13218 bfd_elf_discard_info didn't make any changes to the section. */
13219 if (stub_sec != NULL
13220 || (htab->glink_eh_frame != NULL
13221 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13222 {
13223 htab->stub_error = TRUE;
13224 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13225 }
13226
13227 if (htab->stub_error)
13228 return FALSE;
13229
13230 if (stats != NULL)
13231 {
13232 *stats = bfd_malloc (500);
13233 if (*stats == NULL)
13234 return FALSE;
13235
13236 sprintf (*stats, _("linker stubs in %u group%s\n"
13237 " branch %lu\n"
13238 " toc adjust %lu\n"
13239 " long branch %lu\n"
13240 " long toc adj %lu\n"
13241 " plt call %lu\n"
13242 " plt call toc %lu\n"
13243 " global entry %lu"),
13244 stub_sec_count,
13245 stub_sec_count == 1 ? "" : "s",
13246 htab->stub_count[ppc_stub_long_branch - 1],
13247 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13248 htab->stub_count[ppc_stub_plt_branch - 1],
13249 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13250 htab->stub_count[ppc_stub_plt_call - 1],
13251 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13252 htab->stub_count[ppc_stub_global_entry - 1]);
13253 }
13254 return TRUE;
13255 }
13256
13257 /* This function undoes the changes made by add_symbol_adjust. */
13258
13259 static bfd_boolean
13260 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
13261 {
13262 struct ppc_link_hash_entry *eh;
13263
13264 if (h->root.type == bfd_link_hash_indirect)
13265 return TRUE;
13266
13267 eh = (struct ppc_link_hash_entry *) h;
13268 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
13269 return TRUE;
13270
13271 eh->elf.root.type = bfd_link_hash_undefined;
13272 return TRUE;
13273 }
13274
13275 void
13276 ppc64_elf_restore_symbols (struct bfd_link_info *info)
13277 {
13278 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13279
13280 if (htab != NULL)
13281 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
13282 }
13283
13284 /* What to do when ld finds relocations against symbols defined in
13285 discarded sections. */
13286
13287 static unsigned int
13288 ppc64_elf_action_discarded (asection *sec)
13289 {
13290 if (strcmp (".opd", sec->name) == 0)
13291 return 0;
13292
13293 if (strcmp (".toc", sec->name) == 0)
13294 return 0;
13295
13296 if (strcmp (".toc1", sec->name) == 0)
13297 return 0;
13298
13299 return _bfd_elf_default_action_discarded (sec);
13300 }
13301
13302 /* The RELOCATE_SECTION function is called by the ELF backend linker
13303 to handle the relocations for a section.
13304
13305 The relocs are always passed as Rela structures; if the section
13306 actually uses Rel structures, the r_addend field will always be
13307 zero.
13308
13309 This function is responsible for adjust the section contents as
13310 necessary, and (if using Rela relocs and generating a
13311 relocatable output file) adjusting the reloc addend as
13312 necessary.
13313
13314 This function does not have to worry about setting the reloc
13315 address or the reloc symbol index.
13316
13317 LOCAL_SYMS is a pointer to the swapped in local symbols.
13318
13319 LOCAL_SECTIONS is an array giving the section in the input file
13320 corresponding to the st_shndx field of each local symbol.
13321
13322 The global hash table entry for the global symbols can be found
13323 via elf_sym_hashes (input_bfd).
13324
13325 When generating relocatable output, this function must handle
13326 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13327 going to be the section symbol corresponding to the output
13328 section, which means that the addend must be adjusted
13329 accordingly. */
13330
13331 static bfd_boolean
13332 ppc64_elf_relocate_section (bfd *output_bfd,
13333 struct bfd_link_info *info,
13334 bfd *input_bfd,
13335 asection *input_section,
13336 bfd_byte *contents,
13337 Elf_Internal_Rela *relocs,
13338 Elf_Internal_Sym *local_syms,
13339 asection **local_sections)
13340 {
13341 struct ppc_link_hash_table *htab;
13342 Elf_Internal_Shdr *symtab_hdr;
13343 struct elf_link_hash_entry **sym_hashes;
13344 Elf_Internal_Rela *rel;
13345 Elf_Internal_Rela *wrel;
13346 Elf_Internal_Rela *relend;
13347 Elf_Internal_Rela outrel;
13348 bfd_byte *loc;
13349 struct got_entry **local_got_ents;
13350 bfd_vma TOCstart;
13351 bfd_boolean ret = TRUE;
13352 bfd_boolean is_opd;
13353 /* Assume 'at' branch hints. */
13354 bfd_boolean is_isa_v2 = TRUE;
13355 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13356
13357 /* Initialize howto table if needed. */
13358 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13359 ppc_howto_init ();
13360
13361 htab = ppc_hash_table (info);
13362 if (htab == NULL)
13363 return FALSE;
13364
13365 /* Don't relocate stub sections. */
13366 if (input_section->owner == htab->params->stub_bfd)
13367 return TRUE;
13368
13369 BFD_ASSERT (is_ppc64_elf (input_bfd));
13370
13371 local_got_ents = elf_local_got_ents (input_bfd);
13372 TOCstart = elf_gp (output_bfd);
13373 symtab_hdr = &elf_symtab_hdr (input_bfd);
13374 sym_hashes = elf_sym_hashes (input_bfd);
13375 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13376
13377 rel = wrel = relocs;
13378 relend = relocs + input_section->reloc_count;
13379 for (; rel < relend; wrel++, rel++)
13380 {
13381 enum elf_ppc64_reloc_type r_type;
13382 bfd_vma addend;
13383 bfd_reloc_status_type r;
13384 Elf_Internal_Sym *sym;
13385 asection *sec;
13386 struct elf_link_hash_entry *h_elf;
13387 struct ppc_link_hash_entry *h;
13388 struct ppc_link_hash_entry *fdh;
13389 const char *sym_name;
13390 unsigned long r_symndx, toc_symndx;
13391 bfd_vma toc_addend;
13392 unsigned char tls_mask, tls_gd, tls_type;
13393 unsigned char sym_type;
13394 bfd_vma relocation;
13395 bfd_boolean unresolved_reloc;
13396 bfd_boolean warned;
13397 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13398 unsigned int insn;
13399 unsigned int mask;
13400 struct ppc_stub_hash_entry *stub_entry;
13401 bfd_vma max_br_offset;
13402 bfd_vma from;
13403 Elf_Internal_Rela orig_rel;
13404 reloc_howto_type *howto;
13405 struct reloc_howto_struct alt_howto;
13406
13407 again:
13408 orig_rel = *rel;
13409
13410 r_type = ELF64_R_TYPE (rel->r_info);
13411 r_symndx = ELF64_R_SYM (rel->r_info);
13412
13413 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13414 symbol of the previous ADDR64 reloc. The symbol gives us the
13415 proper TOC base to use. */
13416 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13417 && wrel != relocs
13418 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13419 && is_opd)
13420 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13421
13422 sym = NULL;
13423 sec = NULL;
13424 h_elf = NULL;
13425 sym_name = NULL;
13426 unresolved_reloc = FALSE;
13427 warned = FALSE;
13428
13429 if (r_symndx < symtab_hdr->sh_info)
13430 {
13431 /* It's a local symbol. */
13432 struct _opd_sec_data *opd;
13433
13434 sym = local_syms + r_symndx;
13435 sec = local_sections[r_symndx];
13436 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13437 sym_type = ELF64_ST_TYPE (sym->st_info);
13438 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13439 opd = get_opd_info (sec);
13440 if (opd != NULL && opd->adjust != NULL)
13441 {
13442 long adjust = opd->adjust[OPD_NDX (sym->st_value
13443 + rel->r_addend)];
13444 if (adjust == -1)
13445 relocation = 0;
13446 else
13447 {
13448 /* If this is a relocation against the opd section sym
13449 and we have edited .opd, adjust the reloc addend so
13450 that ld -r and ld --emit-relocs output is correct.
13451 If it is a reloc against some other .opd symbol,
13452 then the symbol value will be adjusted later. */
13453 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13454 rel->r_addend += adjust;
13455 else
13456 relocation += adjust;
13457 }
13458 }
13459 }
13460 else
13461 {
13462 bfd_boolean ignored;
13463
13464 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13465 r_symndx, symtab_hdr, sym_hashes,
13466 h_elf, sec, relocation,
13467 unresolved_reloc, warned, ignored);
13468 sym_name = h_elf->root.root.string;
13469 sym_type = h_elf->type;
13470 if (sec != NULL
13471 && sec->owner == output_bfd
13472 && strcmp (sec->name, ".opd") == 0)
13473 {
13474 /* This is a symbol defined in a linker script. All
13475 such are defined in output sections, even those
13476 defined by simple assignment from a symbol defined in
13477 an input section. Transfer the symbol to an
13478 appropriate input .opd section, so that a branch to
13479 this symbol will be mapped to the location specified
13480 by the opd entry. */
13481 struct bfd_link_order *lo;
13482 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13483 if (lo->type == bfd_indirect_link_order)
13484 {
13485 asection *isec = lo->u.indirect.section;
13486 if (h_elf->root.u.def.value >= isec->output_offset
13487 && h_elf->root.u.def.value < (isec->output_offset
13488 + isec->size))
13489 {
13490 h_elf->root.u.def.value -= isec->output_offset;
13491 h_elf->root.u.def.section = isec;
13492 sec = isec;
13493 break;
13494 }
13495 }
13496 }
13497 }
13498 h = (struct ppc_link_hash_entry *) h_elf;
13499
13500 if (sec != NULL && discarded_section (sec))
13501 {
13502 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13503 input_bfd, input_section,
13504 contents + rel->r_offset);
13505 wrel->r_offset = rel->r_offset;
13506 wrel->r_info = 0;
13507 wrel->r_addend = 0;
13508
13509 /* For ld -r, remove relocations in debug sections against
13510 sections defined in discarded sections. Not done for
13511 non-debug to preserve relocs in .eh_frame which the
13512 eh_frame editing code expects to be present. */
13513 if (bfd_link_relocatable (info)
13514 && (input_section->flags & SEC_DEBUGGING))
13515 wrel--;
13516
13517 continue;
13518 }
13519
13520 if (bfd_link_relocatable (info))
13521 goto copy_reloc;
13522
13523 if (h != NULL && &h->elf == htab->elf.hgot)
13524 {
13525 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13526 sec = bfd_abs_section_ptr;
13527 unresolved_reloc = FALSE;
13528 }
13529
13530 /* TLS optimizations. Replace instruction sequences and relocs
13531 based on information we collected in tls_optimize. We edit
13532 RELOCS so that --emit-relocs will output something sensible
13533 for the final instruction stream. */
13534 tls_mask = 0;
13535 tls_gd = 0;
13536 toc_symndx = 0;
13537 if (h != NULL)
13538 tls_mask = h->tls_mask;
13539 else if (local_got_ents != NULL)
13540 {
13541 struct plt_entry **local_plt = (struct plt_entry **)
13542 (local_got_ents + symtab_hdr->sh_info);
13543 unsigned char *lgot_masks = (unsigned char *)
13544 (local_plt + symtab_hdr->sh_info);
13545 tls_mask = lgot_masks[r_symndx];
13546 }
13547 if (tls_mask == 0
13548 && (r_type == R_PPC64_TLS
13549 || r_type == R_PPC64_TLSGD
13550 || r_type == R_PPC64_TLSLD))
13551 {
13552 /* Check for toc tls entries. */
13553 unsigned char *toc_tls;
13554
13555 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13556 &local_syms, rel, input_bfd))
13557 return FALSE;
13558
13559 if (toc_tls)
13560 tls_mask = *toc_tls;
13561 }
13562
13563 /* Check that tls relocs are used with tls syms, and non-tls
13564 relocs are used with non-tls syms. */
13565 if (r_symndx != STN_UNDEF
13566 && r_type != R_PPC64_NONE
13567 && (h == NULL
13568 || h->elf.root.type == bfd_link_hash_defined
13569 || h->elf.root.type == bfd_link_hash_defweak)
13570 && (IS_PPC64_TLS_RELOC (r_type)
13571 != (sym_type == STT_TLS
13572 || (sym_type == STT_SECTION
13573 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13574 {
13575 if (tls_mask != 0
13576 && (r_type == R_PPC64_TLS
13577 || r_type == R_PPC64_TLSGD
13578 || r_type == R_PPC64_TLSLD))
13579 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13580 ;
13581 else
13582 info->callbacks->einfo
13583 (!IS_PPC64_TLS_RELOC (r_type)
13584 /* xgettext:c-format */
13585 ? _("%P: %H: %s used with TLS symbol `%T'\n")
13586 /* xgettext:c-format */
13587 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13588 input_bfd, input_section, rel->r_offset,
13589 ppc64_elf_howto_table[r_type]->name,
13590 sym_name);
13591 }
13592
13593 /* Ensure reloc mapping code below stays sane. */
13594 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13595 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13596 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13597 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13598 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13599 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13600 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13601 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13602 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13603 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13604 abort ();
13605
13606 switch (r_type)
13607 {
13608 default:
13609 break;
13610
13611 case R_PPC64_LO_DS_OPT:
13612 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13613 if ((insn & (0x3f << 26)) != 58u << 26)
13614 abort ();
13615 insn += (14u << 26) - (58u << 26);
13616 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13617 r_type = R_PPC64_TOC16_LO;
13618 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13619 break;
13620
13621 case R_PPC64_TOC16:
13622 case R_PPC64_TOC16_LO:
13623 case R_PPC64_TOC16_DS:
13624 case R_PPC64_TOC16_LO_DS:
13625 {
13626 /* Check for toc tls entries. */
13627 unsigned char *toc_tls;
13628 int retval;
13629
13630 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13631 &local_syms, rel, input_bfd);
13632 if (retval == 0)
13633 return FALSE;
13634
13635 if (toc_tls)
13636 {
13637 tls_mask = *toc_tls;
13638 if (r_type == R_PPC64_TOC16_DS
13639 || r_type == R_PPC64_TOC16_LO_DS)
13640 {
13641 if (tls_mask != 0
13642 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13643 goto toctprel;
13644 }
13645 else
13646 {
13647 /* If we found a GD reloc pair, then we might be
13648 doing a GD->IE transition. */
13649 if (retval == 2)
13650 {
13651 tls_gd = TLS_TPRELGD;
13652 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13653 goto tls_ldgd_opt;
13654 }
13655 else if (retval == 3)
13656 {
13657 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13658 goto tls_ldgd_opt;
13659 }
13660 }
13661 }
13662 }
13663 break;
13664
13665 case R_PPC64_GOT_TPREL16_HI:
13666 case R_PPC64_GOT_TPREL16_HA:
13667 if (tls_mask != 0
13668 && (tls_mask & TLS_TPREL) == 0)
13669 {
13670 rel->r_offset -= d_offset;
13671 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13672 r_type = R_PPC64_NONE;
13673 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13674 }
13675 break;
13676
13677 case R_PPC64_GOT_TPREL16_DS:
13678 case R_PPC64_GOT_TPREL16_LO_DS:
13679 if (tls_mask != 0
13680 && (tls_mask & TLS_TPREL) == 0)
13681 {
13682 toctprel:
13683 insn = bfd_get_32 (input_bfd,
13684 contents + rel->r_offset - d_offset);
13685 insn &= 31 << 21;
13686 insn |= 0x3c0d0000; /* addis 0,13,0 */
13687 bfd_put_32 (input_bfd, insn,
13688 contents + rel->r_offset - d_offset);
13689 r_type = R_PPC64_TPREL16_HA;
13690 if (toc_symndx != 0)
13691 {
13692 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13693 rel->r_addend = toc_addend;
13694 /* We changed the symbol. Start over in order to
13695 get h, sym, sec etc. right. */
13696 goto again;
13697 }
13698 else
13699 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13700 }
13701 break;
13702
13703 case R_PPC64_TLS:
13704 if (tls_mask != 0
13705 && (tls_mask & TLS_TPREL) == 0)
13706 {
13707 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13708 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13709 if (insn == 0)
13710 abort ();
13711 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13712 /* Was PPC64_TLS which sits on insn boundary, now
13713 PPC64_TPREL16_LO which is at low-order half-word. */
13714 rel->r_offset += d_offset;
13715 r_type = R_PPC64_TPREL16_LO;
13716 if (toc_symndx != 0)
13717 {
13718 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13719 rel->r_addend = toc_addend;
13720 /* We changed the symbol. Start over in order to
13721 get h, sym, sec etc. right. */
13722 goto again;
13723 }
13724 else
13725 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13726 }
13727 break;
13728
13729 case R_PPC64_GOT_TLSGD16_HI:
13730 case R_PPC64_GOT_TLSGD16_HA:
13731 tls_gd = TLS_TPRELGD;
13732 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13733 goto tls_gdld_hi;
13734 break;
13735
13736 case R_PPC64_GOT_TLSLD16_HI:
13737 case R_PPC64_GOT_TLSLD16_HA:
13738 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13739 {
13740 tls_gdld_hi:
13741 if ((tls_mask & tls_gd) != 0)
13742 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13743 + R_PPC64_GOT_TPREL16_DS);
13744 else
13745 {
13746 rel->r_offset -= d_offset;
13747 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13748 r_type = R_PPC64_NONE;
13749 }
13750 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13751 }
13752 break;
13753
13754 case R_PPC64_GOT_TLSGD16:
13755 case R_PPC64_GOT_TLSGD16_LO:
13756 tls_gd = TLS_TPRELGD;
13757 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13758 goto tls_ldgd_opt;
13759 break;
13760
13761 case R_PPC64_GOT_TLSLD16:
13762 case R_PPC64_GOT_TLSLD16_LO:
13763 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13764 {
13765 unsigned int insn1, insn2, insn3;
13766 bfd_vma offset;
13767
13768 tls_ldgd_opt:
13769 offset = (bfd_vma) -1;
13770 /* If not using the newer R_PPC64_TLSGD/LD to mark
13771 __tls_get_addr calls, we must trust that the call
13772 stays with its arg setup insns, ie. that the next
13773 reloc is the __tls_get_addr call associated with
13774 the current reloc. Edit both insns. */
13775 if (input_section->has_tls_get_addr_call
13776 && rel + 1 < relend
13777 && branch_reloc_hash_match (input_bfd, rel + 1,
13778 htab->tls_get_addr,
13779 htab->tls_get_addr_fd))
13780 offset = rel[1].r_offset;
13781 /* We read the low GOT_TLS (or TOC16) insn because we
13782 need to keep the destination reg. It may be
13783 something other than the usual r3, and moved to r3
13784 before the call by intervening code. */
13785 insn1 = bfd_get_32 (input_bfd,
13786 contents + rel->r_offset - d_offset);
13787 if ((tls_mask & tls_gd) != 0)
13788 {
13789 /* IE */
13790 insn1 &= (0x1f << 21) | (0x1f << 16);
13791 insn1 |= 58 << 26; /* ld */
13792 insn2 = 0x7c636a14; /* add 3,3,13 */
13793 if (offset != (bfd_vma) -1)
13794 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13795 if ((tls_mask & TLS_EXPLICIT) == 0)
13796 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13797 + R_PPC64_GOT_TPREL16_DS);
13798 else
13799 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13800 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13801 }
13802 else
13803 {
13804 /* LE */
13805 insn1 &= 0x1f << 21;
13806 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13807 insn2 = 0x38630000; /* addi 3,3,0 */
13808 if (tls_gd == 0)
13809 {
13810 /* Was an LD reloc. */
13811 if (toc_symndx)
13812 sec = local_sections[toc_symndx];
13813 for (r_symndx = 0;
13814 r_symndx < symtab_hdr->sh_info;
13815 r_symndx++)
13816 if (local_sections[r_symndx] == sec)
13817 break;
13818 if (r_symndx >= symtab_hdr->sh_info)
13819 r_symndx = STN_UNDEF;
13820 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13821 if (r_symndx != STN_UNDEF)
13822 rel->r_addend -= (local_syms[r_symndx].st_value
13823 + sec->output_offset
13824 + sec->output_section->vma);
13825 }
13826 else if (toc_symndx != 0)
13827 {
13828 r_symndx = toc_symndx;
13829 rel->r_addend = toc_addend;
13830 }
13831 r_type = R_PPC64_TPREL16_HA;
13832 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13833 if (offset != (bfd_vma) -1)
13834 {
13835 rel[1].r_info = ELF64_R_INFO (r_symndx,
13836 R_PPC64_TPREL16_LO);
13837 rel[1].r_offset = offset + d_offset;
13838 rel[1].r_addend = rel->r_addend;
13839 }
13840 }
13841 bfd_put_32 (input_bfd, insn1,
13842 contents + rel->r_offset - d_offset);
13843 if (offset != (bfd_vma) -1)
13844 {
13845 insn3 = bfd_get_32 (input_bfd,
13846 contents + offset + 4);
13847 if (insn3 == NOP
13848 || insn3 == CROR_151515 || insn3 == CROR_313131)
13849 {
13850 rel[1].r_offset += 4;
13851 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13852 insn2 = NOP;
13853 }
13854 bfd_put_32 (input_bfd, insn2, contents + offset);
13855 }
13856 if ((tls_mask & tls_gd) == 0
13857 && (tls_gd == 0 || toc_symndx != 0))
13858 {
13859 /* We changed the symbol. Start over in order
13860 to get h, sym, sec etc. right. */
13861 goto again;
13862 }
13863 }
13864 break;
13865
13866 case R_PPC64_TLSGD:
13867 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13868 {
13869 unsigned int insn2, insn3;
13870 bfd_vma offset = rel->r_offset;
13871
13872 if ((tls_mask & TLS_TPRELGD) != 0)
13873 {
13874 /* IE */
13875 r_type = R_PPC64_NONE;
13876 insn2 = 0x7c636a14; /* add 3,3,13 */
13877 }
13878 else
13879 {
13880 /* LE */
13881 if (toc_symndx != 0)
13882 {
13883 r_symndx = toc_symndx;
13884 rel->r_addend = toc_addend;
13885 }
13886 r_type = R_PPC64_TPREL16_LO;
13887 rel->r_offset = offset + d_offset;
13888 insn2 = 0x38630000; /* addi 3,3,0 */
13889 }
13890 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13891 /* Zap the reloc on the _tls_get_addr call too. */
13892 BFD_ASSERT (offset == rel[1].r_offset);
13893 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13894 insn3 = bfd_get_32 (input_bfd,
13895 contents + offset + 4);
13896 if (insn3 == NOP
13897 || insn3 == CROR_151515 || insn3 == CROR_313131)
13898 {
13899 rel->r_offset += 4;
13900 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13901 insn2 = NOP;
13902 }
13903 bfd_put_32 (input_bfd, insn2, contents + offset);
13904 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13905 goto again;
13906 }
13907 break;
13908
13909 case R_PPC64_TLSLD:
13910 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13911 {
13912 unsigned int insn2, insn3;
13913 bfd_vma offset = rel->r_offset;
13914
13915 if (toc_symndx)
13916 sec = local_sections[toc_symndx];
13917 for (r_symndx = 0;
13918 r_symndx < symtab_hdr->sh_info;
13919 r_symndx++)
13920 if (local_sections[r_symndx] == sec)
13921 break;
13922 if (r_symndx >= symtab_hdr->sh_info)
13923 r_symndx = STN_UNDEF;
13924 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13925 if (r_symndx != STN_UNDEF)
13926 rel->r_addend -= (local_syms[r_symndx].st_value
13927 + sec->output_offset
13928 + sec->output_section->vma);
13929
13930 r_type = R_PPC64_TPREL16_LO;
13931 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13932 rel->r_offset = offset + d_offset;
13933 /* Zap the reloc on the _tls_get_addr call too. */
13934 BFD_ASSERT (offset == rel[1].r_offset);
13935 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13936 insn2 = 0x38630000; /* addi 3,3,0 */
13937 insn3 = bfd_get_32 (input_bfd,
13938 contents + offset + 4);
13939 if (insn3 == NOP
13940 || insn3 == CROR_151515 || insn3 == CROR_313131)
13941 {
13942 rel->r_offset += 4;
13943 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13944 insn2 = NOP;
13945 }
13946 bfd_put_32 (input_bfd, insn2, contents + offset);
13947 goto again;
13948 }
13949 break;
13950
13951 case R_PPC64_DTPMOD64:
13952 if (rel + 1 < relend
13953 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13954 && rel[1].r_offset == rel->r_offset + 8)
13955 {
13956 if ((tls_mask & TLS_GD) == 0)
13957 {
13958 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13959 if ((tls_mask & TLS_TPRELGD) != 0)
13960 r_type = R_PPC64_TPREL64;
13961 else
13962 {
13963 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13964 r_type = R_PPC64_NONE;
13965 }
13966 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13967 }
13968 }
13969 else
13970 {
13971 if ((tls_mask & TLS_LD) == 0)
13972 {
13973 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13974 r_type = R_PPC64_NONE;
13975 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13976 }
13977 }
13978 break;
13979
13980 case R_PPC64_TPREL64:
13981 if ((tls_mask & TLS_TPREL) == 0)
13982 {
13983 r_type = R_PPC64_NONE;
13984 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13985 }
13986 break;
13987
13988 case R_PPC64_ENTRY:
13989 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13990 if (!bfd_link_pic (info)
13991 && !info->traditional_format
13992 && relocation + 0x80008000 <= 0xffffffff)
13993 {
13994 unsigned int insn1, insn2;
13995
13996 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13997 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13998 if ((insn1 & ~0xfffc) == LD_R2_0R12
13999 && insn2 == ADD_R2_R2_R12)
14000 {
14001 bfd_put_32 (input_bfd,
14002 LIS_R2 + PPC_HA (relocation),
14003 contents + rel->r_offset);
14004 bfd_put_32 (input_bfd,
14005 ADDI_R2_R2 + PPC_LO (relocation),
14006 contents + rel->r_offset + 4);
14007 }
14008 }
14009 else
14010 {
14011 relocation -= (rel->r_offset
14012 + input_section->output_offset
14013 + input_section->output_section->vma);
14014 if (relocation + 0x80008000 <= 0xffffffff)
14015 {
14016 unsigned int insn1, insn2;
14017
14018 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14019 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14020 if ((insn1 & ~0xfffc) == LD_R2_0R12
14021 && insn2 == ADD_R2_R2_R12)
14022 {
14023 bfd_put_32 (input_bfd,
14024 ADDIS_R2_R12 + PPC_HA (relocation),
14025 contents + rel->r_offset);
14026 bfd_put_32 (input_bfd,
14027 ADDI_R2_R2 + PPC_LO (relocation),
14028 contents + rel->r_offset + 4);
14029 }
14030 }
14031 }
14032 break;
14033
14034 case R_PPC64_REL16_HA:
14035 /* If we are generating a non-PIC executable, edit
14036 . 0: addis 2,12,.TOC.-0b@ha
14037 . addi 2,2,.TOC.-0b@l
14038 used by ELFv2 global entry points to set up r2, to
14039 . lis 2,.TOC.@ha
14040 . addi 2,2,.TOC.@l
14041 if .TOC. is in range. */
14042 if (!bfd_link_pic (info)
14043 && !info->traditional_format
14044 && !htab->opd_abi
14045 && rel->r_addend == d_offset
14046 && h != NULL && &h->elf == htab->elf.hgot
14047 && rel + 1 < relend
14048 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14049 && rel[1].r_offset == rel->r_offset + 4
14050 && rel[1].r_addend == rel->r_addend + 4
14051 && relocation + 0x80008000 <= 0xffffffff)
14052 {
14053 unsigned int insn1, insn2;
14054 bfd_vma offset = rel->r_offset - d_offset;
14055 insn1 = bfd_get_32 (input_bfd, contents + offset);
14056 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14057 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14058 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14059 {
14060 r_type = R_PPC64_ADDR16_HA;
14061 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14062 rel->r_addend -= d_offset;
14063 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14064 rel[1].r_addend -= d_offset + 4;
14065 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14066 }
14067 }
14068 break;
14069 }
14070
14071 /* Handle other relocations that tweak non-addend part of insn. */
14072 insn = 0;
14073 max_br_offset = 1 << 25;
14074 addend = rel->r_addend;
14075 reloc_dest = DEST_NORMAL;
14076 switch (r_type)
14077 {
14078 default:
14079 break;
14080
14081 case R_PPC64_TOCSAVE:
14082 if (relocation + addend == (rel->r_offset
14083 + input_section->output_offset
14084 + input_section->output_section->vma)
14085 && tocsave_find (htab, NO_INSERT,
14086 &local_syms, rel, input_bfd))
14087 {
14088 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14089 if (insn == NOP
14090 || insn == CROR_151515 || insn == CROR_313131)
14091 bfd_put_32 (input_bfd,
14092 STD_R2_0R1 + STK_TOC (htab),
14093 contents + rel->r_offset);
14094 }
14095 break;
14096
14097 /* Branch taken prediction relocations. */
14098 case R_PPC64_ADDR14_BRTAKEN:
14099 case R_PPC64_REL14_BRTAKEN:
14100 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14101 /* Fall through. */
14102
14103 /* Branch not taken prediction relocations. */
14104 case R_PPC64_ADDR14_BRNTAKEN:
14105 case R_PPC64_REL14_BRNTAKEN:
14106 insn |= bfd_get_32 (input_bfd,
14107 contents + rel->r_offset) & ~(0x01 << 21);
14108 /* Fall through. */
14109
14110 case R_PPC64_REL14:
14111 max_br_offset = 1 << 15;
14112 /* Fall through. */
14113
14114 case R_PPC64_REL24:
14115 /* Calls to functions with a different TOC, such as calls to
14116 shared objects, need to alter the TOC pointer. This is
14117 done using a linkage stub. A REL24 branching to these
14118 linkage stubs needs to be followed by a nop, as the nop
14119 will be replaced with an instruction to restore the TOC
14120 base pointer. */
14121 fdh = h;
14122 if (h != NULL
14123 && h->oh != NULL
14124 && h->oh->is_func_descriptor)
14125 fdh = ppc_follow_link (h->oh);
14126 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14127 htab);
14128 if (stub_entry != NULL
14129 && (stub_entry->stub_type == ppc_stub_plt_call
14130 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14131 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14132 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14133 {
14134 bfd_boolean can_plt_call = FALSE;
14135
14136 /* All of these stubs will modify r2, so there must be a
14137 branch and link followed by a nop. The nop is
14138 replaced by an insn to restore r2. */
14139 if (rel->r_offset + 8 <= input_section->size)
14140 {
14141 unsigned long br;
14142
14143 br = bfd_get_32 (input_bfd,
14144 contents + rel->r_offset);
14145 if ((br & 1) != 0)
14146 {
14147 unsigned long nop;
14148
14149 nop = bfd_get_32 (input_bfd,
14150 contents + rel->r_offset + 4);
14151 if (nop == NOP
14152 || nop == CROR_151515 || nop == CROR_313131)
14153 {
14154 if (h != NULL
14155 && (h == htab->tls_get_addr_fd
14156 || h == htab->tls_get_addr)
14157 && htab->params->tls_get_addr_opt)
14158 {
14159 /* Special stub used, leave nop alone. */
14160 }
14161 else
14162 bfd_put_32 (input_bfd,
14163 LD_R2_0R1 + STK_TOC (htab),
14164 contents + rel->r_offset + 4);
14165 can_plt_call = TRUE;
14166 }
14167 }
14168 }
14169
14170 if (!can_plt_call && h != NULL)
14171 {
14172 const char *name = h->elf.root.root.string;
14173
14174 if (*name == '.')
14175 ++name;
14176
14177 if (strncmp (name, "__libc_start_main", 17) == 0
14178 && (name[17] == 0 || name[17] == '@'))
14179 {
14180 /* Allow crt1 branch to go via a toc adjusting
14181 stub. Other calls that never return could do
14182 the same, if we could detect such. */
14183 can_plt_call = TRUE;
14184 }
14185 }
14186
14187 if (!can_plt_call)
14188 {
14189 /* g++ as of 20130507 emits self-calls without a
14190 following nop. This is arguably wrong since we
14191 have conflicting information. On the one hand a
14192 global symbol and on the other a local call
14193 sequence, but don't error for this special case.
14194 It isn't possible to cheaply verify we have
14195 exactly such a call. Allow all calls to the same
14196 section. */
14197 asection *code_sec = sec;
14198
14199 if (get_opd_info (sec) != NULL)
14200 {
14201 bfd_vma off = (relocation + addend
14202 - sec->output_section->vma
14203 - sec->output_offset);
14204
14205 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14206 }
14207 if (code_sec == input_section)
14208 can_plt_call = TRUE;
14209 }
14210
14211 if (!can_plt_call)
14212 {
14213 if (stub_entry->stub_type == ppc_stub_plt_call
14214 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14215 info->callbacks->einfo
14216 /* xgettext:c-format */
14217 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14218 "recompile with -fPIC\n"),
14219 input_bfd, input_section, rel->r_offset, sym_name);
14220 else
14221 info->callbacks->einfo
14222 /* xgettext:c-format */
14223 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14224 "(-mcmodel=small toc adjust stub)\n"),
14225 input_bfd, input_section, rel->r_offset, sym_name);
14226
14227 bfd_set_error (bfd_error_bad_value);
14228 ret = FALSE;
14229 }
14230
14231 if (can_plt_call
14232 && (stub_entry->stub_type == ppc_stub_plt_call
14233 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14234 unresolved_reloc = FALSE;
14235 }
14236
14237 if ((stub_entry == NULL
14238 || stub_entry->stub_type == ppc_stub_long_branch
14239 || stub_entry->stub_type == ppc_stub_plt_branch)
14240 && get_opd_info (sec) != NULL)
14241 {
14242 /* The branch destination is the value of the opd entry. */
14243 bfd_vma off = (relocation + addend
14244 - sec->output_section->vma
14245 - sec->output_offset);
14246 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14247 if (dest != (bfd_vma) -1)
14248 {
14249 relocation = dest;
14250 addend = 0;
14251 reloc_dest = DEST_OPD;
14252 }
14253 }
14254
14255 /* If the branch is out of reach we ought to have a long
14256 branch stub. */
14257 from = (rel->r_offset
14258 + input_section->output_offset
14259 + input_section->output_section->vma);
14260
14261 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14262 ? fdh->elf.other
14263 : sym->st_other);
14264
14265 if (stub_entry != NULL
14266 && (stub_entry->stub_type == ppc_stub_long_branch
14267 || stub_entry->stub_type == ppc_stub_plt_branch)
14268 && (r_type == R_PPC64_ADDR14_BRTAKEN
14269 || r_type == R_PPC64_ADDR14_BRNTAKEN
14270 || (relocation + addend - from + max_br_offset
14271 < 2 * max_br_offset)))
14272 /* Don't use the stub if this branch is in range. */
14273 stub_entry = NULL;
14274
14275 if (stub_entry != NULL)
14276 {
14277 /* Munge up the value and addend so that we call the stub
14278 rather than the procedure directly. */
14279 asection *stub_sec = stub_entry->group->stub_sec;
14280
14281 if (stub_entry->stub_type == ppc_stub_save_res)
14282 relocation += (stub_sec->output_offset
14283 + stub_sec->output_section->vma
14284 + stub_sec->size - htab->sfpr->size
14285 - htab->sfpr->output_offset
14286 - htab->sfpr->output_section->vma);
14287 else
14288 relocation = (stub_entry->stub_offset
14289 + stub_sec->output_offset
14290 + stub_sec->output_section->vma);
14291 addend = 0;
14292 reloc_dest = DEST_STUB;
14293
14294 if ((stub_entry->stub_type == ppc_stub_plt_call
14295 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14296 && (ALWAYS_EMIT_R2SAVE
14297 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14298 && rel + 1 < relend
14299 && rel[1].r_offset == rel->r_offset + 4
14300 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14301 relocation += 4;
14302 }
14303
14304 if (insn != 0)
14305 {
14306 if (is_isa_v2)
14307 {
14308 /* Set 'a' bit. This is 0b00010 in BO field for branch
14309 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14310 for branch on CTR insns (BO == 1a00t or 1a01t). */
14311 if ((insn & (0x14 << 21)) == (0x04 << 21))
14312 insn |= 0x02 << 21;
14313 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14314 insn |= 0x08 << 21;
14315 else
14316 break;
14317 }
14318 else
14319 {
14320 /* Invert 'y' bit if not the default. */
14321 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14322 insn ^= 0x01 << 21;
14323 }
14324
14325 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14326 }
14327
14328 /* NOP out calls to undefined weak functions.
14329 We can thus call a weak function without first
14330 checking whether the function is defined. */
14331 else if (h != NULL
14332 && h->elf.root.type == bfd_link_hash_undefweak
14333 && h->elf.dynindx == -1
14334 && r_type == R_PPC64_REL24
14335 && relocation == 0
14336 && addend == 0)
14337 {
14338 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14339 goto copy_reloc;
14340 }
14341 break;
14342 }
14343
14344 /* Set `addend'. */
14345 tls_type = 0;
14346 switch (r_type)
14347 {
14348 default:
14349 info->callbacks->einfo
14350 /* xgettext:c-format */
14351 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14352 input_bfd, (int) r_type, sym_name);
14353
14354 bfd_set_error (bfd_error_bad_value);
14355 ret = FALSE;
14356 goto copy_reloc;
14357
14358 case R_PPC64_NONE:
14359 case R_PPC64_TLS:
14360 case R_PPC64_TLSGD:
14361 case R_PPC64_TLSLD:
14362 case R_PPC64_TOCSAVE:
14363 case R_PPC64_GNU_VTINHERIT:
14364 case R_PPC64_GNU_VTENTRY:
14365 case R_PPC64_ENTRY:
14366 goto copy_reloc;
14367
14368 /* GOT16 relocations. Like an ADDR16 using the symbol's
14369 address in the GOT as relocation value instead of the
14370 symbol's value itself. Also, create a GOT entry for the
14371 symbol and put the symbol value there. */
14372 case R_PPC64_GOT_TLSGD16:
14373 case R_PPC64_GOT_TLSGD16_LO:
14374 case R_PPC64_GOT_TLSGD16_HI:
14375 case R_PPC64_GOT_TLSGD16_HA:
14376 tls_type = TLS_TLS | TLS_GD;
14377 goto dogot;
14378
14379 case R_PPC64_GOT_TLSLD16:
14380 case R_PPC64_GOT_TLSLD16_LO:
14381 case R_PPC64_GOT_TLSLD16_HI:
14382 case R_PPC64_GOT_TLSLD16_HA:
14383 tls_type = TLS_TLS | TLS_LD;
14384 goto dogot;
14385
14386 case R_PPC64_GOT_TPREL16_DS:
14387 case R_PPC64_GOT_TPREL16_LO_DS:
14388 case R_PPC64_GOT_TPREL16_HI:
14389 case R_PPC64_GOT_TPREL16_HA:
14390 tls_type = TLS_TLS | TLS_TPREL;
14391 goto dogot;
14392
14393 case R_PPC64_GOT_DTPREL16_DS:
14394 case R_PPC64_GOT_DTPREL16_LO_DS:
14395 case R_PPC64_GOT_DTPREL16_HI:
14396 case R_PPC64_GOT_DTPREL16_HA:
14397 tls_type = TLS_TLS | TLS_DTPREL;
14398 goto dogot;
14399
14400 case R_PPC64_GOT16:
14401 case R_PPC64_GOT16_LO:
14402 case R_PPC64_GOT16_HI:
14403 case R_PPC64_GOT16_HA:
14404 case R_PPC64_GOT16_DS:
14405 case R_PPC64_GOT16_LO_DS:
14406 dogot:
14407 {
14408 /* Relocation is to the entry for this symbol in the global
14409 offset table. */
14410 asection *got;
14411 bfd_vma *offp;
14412 bfd_vma off;
14413 unsigned long indx = 0;
14414 struct got_entry *ent;
14415
14416 if (tls_type == (TLS_TLS | TLS_LD)
14417 && (h == NULL
14418 || !h->elf.def_dynamic))
14419 ent = ppc64_tlsld_got (input_bfd);
14420 else
14421 {
14422
14423 if (h != NULL)
14424 {
14425 bfd_boolean dyn = htab->elf.dynamic_sections_created;
14426 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
14427 &h->elf)
14428 || (bfd_link_pic (info)
14429 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14430 /* This is actually a static link, or it is a
14431 -Bsymbolic link and the symbol is defined
14432 locally, or the symbol was forced to be local
14433 because of a version file. */
14434 ;
14435 else
14436 {
14437 BFD_ASSERT (h->elf.dynindx != -1);
14438 indx = h->elf.dynindx;
14439 unresolved_reloc = FALSE;
14440 }
14441 ent = h->elf.got.glist;
14442 }
14443 else
14444 {
14445 if (local_got_ents == NULL)
14446 abort ();
14447 ent = local_got_ents[r_symndx];
14448 }
14449
14450 for (; ent != NULL; ent = ent->next)
14451 if (ent->addend == orig_rel.r_addend
14452 && ent->owner == input_bfd
14453 && ent->tls_type == tls_type)
14454 break;
14455 }
14456
14457 if (ent == NULL)
14458 abort ();
14459 if (ent->is_indirect)
14460 ent = ent->got.ent;
14461 offp = &ent->got.offset;
14462 got = ppc64_elf_tdata (ent->owner)->got;
14463 if (got == NULL)
14464 abort ();
14465
14466 /* The offset must always be a multiple of 8. We use the
14467 least significant bit to record whether we have already
14468 processed this entry. */
14469 off = *offp;
14470 if ((off & 1) != 0)
14471 off &= ~1;
14472 else
14473 {
14474 /* Generate relocs for the dynamic linker, except in
14475 the case of TLSLD where we'll use one entry per
14476 module. */
14477 asection *relgot;
14478 bfd_boolean ifunc;
14479
14480 *offp = off | 1;
14481 relgot = NULL;
14482 ifunc = (h != NULL
14483 ? h->elf.type == STT_GNU_IFUNC
14484 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14485 if (ifunc)
14486 relgot = htab->elf.irelplt;
14487 else if ((bfd_link_pic (info) || indx != 0)
14488 && (h == NULL
14489 || (tls_type == (TLS_TLS | TLS_LD)
14490 && !h->elf.def_dynamic)
14491 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14492 || h->elf.root.type != bfd_link_hash_undefweak))
14493 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14494 if (relgot != NULL)
14495 {
14496 outrel.r_offset = (got->output_section->vma
14497 + got->output_offset
14498 + off);
14499 outrel.r_addend = addend;
14500 if (tls_type & (TLS_LD | TLS_GD))
14501 {
14502 outrel.r_addend = 0;
14503 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14504 if (tls_type == (TLS_TLS | TLS_GD))
14505 {
14506 loc = relgot->contents;
14507 loc += (relgot->reloc_count++
14508 * sizeof (Elf64_External_Rela));
14509 bfd_elf64_swap_reloca_out (output_bfd,
14510 &outrel, loc);
14511 outrel.r_offset += 8;
14512 outrel.r_addend = addend;
14513 outrel.r_info
14514 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14515 }
14516 }
14517 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14518 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14519 else if (tls_type == (TLS_TLS | TLS_TPREL))
14520 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14521 else if (indx != 0)
14522 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14523 else
14524 {
14525 if (ifunc)
14526 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14527 else
14528 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14529
14530 /* Write the .got section contents for the sake
14531 of prelink. */
14532 loc = got->contents + off;
14533 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14534 loc);
14535 }
14536
14537 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14538 {
14539 outrel.r_addend += relocation;
14540 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14541 {
14542 if (htab->elf.tls_sec == NULL)
14543 outrel.r_addend = 0;
14544 else
14545 outrel.r_addend -= htab->elf.tls_sec->vma;
14546 }
14547 }
14548 loc = relgot->contents;
14549 loc += (relgot->reloc_count++
14550 * sizeof (Elf64_External_Rela));
14551 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14552 }
14553
14554 /* Init the .got section contents here if we're not
14555 emitting a reloc. */
14556 else
14557 {
14558 relocation += addend;
14559 if (tls_type == (TLS_TLS | TLS_LD))
14560 relocation = 1;
14561 else if (tls_type != 0)
14562 {
14563 if (htab->elf.tls_sec == NULL)
14564 relocation = 0;
14565 else
14566 {
14567 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14568 if (tls_type == (TLS_TLS | TLS_TPREL))
14569 relocation += DTP_OFFSET - TP_OFFSET;
14570 }
14571
14572 if (tls_type == (TLS_TLS | TLS_GD))
14573 {
14574 bfd_put_64 (output_bfd, relocation,
14575 got->contents + off + 8);
14576 relocation = 1;
14577 }
14578 }
14579
14580 bfd_put_64 (output_bfd, relocation,
14581 got->contents + off);
14582 }
14583 }
14584
14585 if (off >= (bfd_vma) -2)
14586 abort ();
14587
14588 relocation = got->output_section->vma + got->output_offset + off;
14589 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14590 }
14591 break;
14592
14593 case R_PPC64_PLT16_HA:
14594 case R_PPC64_PLT16_HI:
14595 case R_PPC64_PLT16_LO:
14596 case R_PPC64_PLT32:
14597 case R_PPC64_PLT64:
14598 /* Relocation is to the entry for this symbol in the
14599 procedure linkage table. */
14600 {
14601 struct plt_entry **plt_list = NULL;
14602 if (h != NULL)
14603 plt_list = &h->elf.plt.plist;
14604 else if (local_got_ents != NULL)
14605 {
14606 struct plt_entry **local_plt = (struct plt_entry **)
14607 (local_got_ents + symtab_hdr->sh_info);
14608 unsigned char *local_got_tls_masks = (unsigned char *)
14609 (local_plt + symtab_hdr->sh_info);
14610 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14611 plt_list = local_plt + r_symndx;
14612 }
14613 if (plt_list)
14614 {
14615 struct plt_entry *ent;
14616
14617 for (ent = *plt_list; ent != NULL; ent = ent->next)
14618 if (ent->plt.offset != (bfd_vma) -1
14619 && ent->addend == orig_rel.r_addend)
14620 {
14621 asection *plt;
14622
14623 plt = htab->elf.splt;
14624 if (!htab->elf.dynamic_sections_created
14625 || h == NULL
14626 || h->elf.dynindx == -1)
14627 plt = htab->elf.iplt;
14628 relocation = (plt->output_section->vma
14629 + plt->output_offset
14630 + ent->plt.offset);
14631 addend = 0;
14632 unresolved_reloc = FALSE;
14633 break;
14634 }
14635 }
14636 }
14637 break;
14638
14639 case R_PPC64_TOC:
14640 /* Relocation value is TOC base. */
14641 relocation = TOCstart;
14642 if (r_symndx == STN_UNDEF)
14643 relocation += htab->sec_info[input_section->id].toc_off;
14644 else if (unresolved_reloc)
14645 ;
14646 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14647 relocation += htab->sec_info[sec->id].toc_off;
14648 else
14649 unresolved_reloc = TRUE;
14650 goto dodyn;
14651
14652 /* TOC16 relocs. We want the offset relative to the TOC base,
14653 which is the address of the start of the TOC plus 0x8000.
14654 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14655 in this order. */
14656 case R_PPC64_TOC16:
14657 case R_PPC64_TOC16_LO:
14658 case R_PPC64_TOC16_HI:
14659 case R_PPC64_TOC16_DS:
14660 case R_PPC64_TOC16_LO_DS:
14661 case R_PPC64_TOC16_HA:
14662 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14663 break;
14664
14665 /* Relocate against the beginning of the section. */
14666 case R_PPC64_SECTOFF:
14667 case R_PPC64_SECTOFF_LO:
14668 case R_PPC64_SECTOFF_HI:
14669 case R_PPC64_SECTOFF_DS:
14670 case R_PPC64_SECTOFF_LO_DS:
14671 case R_PPC64_SECTOFF_HA:
14672 if (sec != NULL)
14673 addend -= sec->output_section->vma;
14674 break;
14675
14676 case R_PPC64_REL16:
14677 case R_PPC64_REL16_LO:
14678 case R_PPC64_REL16_HI:
14679 case R_PPC64_REL16_HA:
14680 case R_PPC64_REL16DX_HA:
14681 break;
14682
14683 case R_PPC64_REL14:
14684 case R_PPC64_REL14_BRNTAKEN:
14685 case R_PPC64_REL14_BRTAKEN:
14686 case R_PPC64_REL24:
14687 break;
14688
14689 case R_PPC64_TPREL16:
14690 case R_PPC64_TPREL16_LO:
14691 case R_PPC64_TPREL16_HI:
14692 case R_PPC64_TPREL16_HA:
14693 case R_PPC64_TPREL16_DS:
14694 case R_PPC64_TPREL16_LO_DS:
14695 case R_PPC64_TPREL16_HIGH:
14696 case R_PPC64_TPREL16_HIGHA:
14697 case R_PPC64_TPREL16_HIGHER:
14698 case R_PPC64_TPREL16_HIGHERA:
14699 case R_PPC64_TPREL16_HIGHEST:
14700 case R_PPC64_TPREL16_HIGHESTA:
14701 if (h != NULL
14702 && h->elf.root.type == bfd_link_hash_undefweak
14703 && h->elf.dynindx == -1)
14704 {
14705 /* Make this relocation against an undefined weak symbol
14706 resolve to zero. This is really just a tweak, since
14707 code using weak externs ought to check that they are
14708 defined before using them. */
14709 bfd_byte *p = contents + rel->r_offset - d_offset;
14710
14711 insn = bfd_get_32 (input_bfd, p);
14712 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14713 if (insn != 0)
14714 bfd_put_32 (input_bfd, insn, p);
14715 break;
14716 }
14717 if (htab->elf.tls_sec != NULL)
14718 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14719 if (bfd_link_pic (info))
14720 /* The TPREL16 relocs shouldn't really be used in shared
14721 libs as they will result in DT_TEXTREL being set, but
14722 support them anyway. */
14723 goto dodyn;
14724 break;
14725
14726 case R_PPC64_DTPREL16:
14727 case R_PPC64_DTPREL16_LO:
14728 case R_PPC64_DTPREL16_HI:
14729 case R_PPC64_DTPREL16_HA:
14730 case R_PPC64_DTPREL16_DS:
14731 case R_PPC64_DTPREL16_LO_DS:
14732 case R_PPC64_DTPREL16_HIGH:
14733 case R_PPC64_DTPREL16_HIGHA:
14734 case R_PPC64_DTPREL16_HIGHER:
14735 case R_PPC64_DTPREL16_HIGHERA:
14736 case R_PPC64_DTPREL16_HIGHEST:
14737 case R_PPC64_DTPREL16_HIGHESTA:
14738 if (htab->elf.tls_sec != NULL)
14739 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14740 break;
14741
14742 case R_PPC64_ADDR64_LOCAL:
14743 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14744 ? h->elf.other
14745 : sym->st_other);
14746 break;
14747
14748 case R_PPC64_DTPMOD64:
14749 relocation = 1;
14750 addend = 0;
14751 goto dodyn;
14752
14753 case R_PPC64_TPREL64:
14754 if (htab->elf.tls_sec != NULL)
14755 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14756 goto dodyn;
14757
14758 case R_PPC64_DTPREL64:
14759 if (htab->elf.tls_sec != NULL)
14760 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14761 /* Fall through. */
14762
14763 /* Relocations that may need to be propagated if this is a
14764 dynamic object. */
14765 case R_PPC64_REL30:
14766 case R_PPC64_REL32:
14767 case R_PPC64_REL64:
14768 case R_PPC64_ADDR14:
14769 case R_PPC64_ADDR14_BRNTAKEN:
14770 case R_PPC64_ADDR14_BRTAKEN:
14771 case R_PPC64_ADDR16:
14772 case R_PPC64_ADDR16_DS:
14773 case R_PPC64_ADDR16_HA:
14774 case R_PPC64_ADDR16_HI:
14775 case R_PPC64_ADDR16_HIGH:
14776 case R_PPC64_ADDR16_HIGHA:
14777 case R_PPC64_ADDR16_HIGHER:
14778 case R_PPC64_ADDR16_HIGHERA:
14779 case R_PPC64_ADDR16_HIGHEST:
14780 case R_PPC64_ADDR16_HIGHESTA:
14781 case R_PPC64_ADDR16_LO:
14782 case R_PPC64_ADDR16_LO_DS:
14783 case R_PPC64_ADDR24:
14784 case R_PPC64_ADDR32:
14785 case R_PPC64_ADDR64:
14786 case R_PPC64_UADDR16:
14787 case R_PPC64_UADDR32:
14788 case R_PPC64_UADDR64:
14789 dodyn:
14790 if ((input_section->flags & SEC_ALLOC) == 0)
14791 break;
14792
14793 if (NO_OPD_RELOCS && is_opd)
14794 break;
14795
14796 if (bfd_link_pic (info)
14797 ? ((h != NULL && pc_dynrelocs (h))
14798 || must_be_dyn_reloc (info, r_type))
14799 : (h != NULL
14800 ? h->dyn_relocs != NULL
14801 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14802 {
14803 bfd_boolean skip, relocate;
14804 asection *sreloc;
14805 bfd_vma out_off;
14806
14807 /* When generating a dynamic object, these relocations
14808 are copied into the output file to be resolved at run
14809 time. */
14810
14811 skip = FALSE;
14812 relocate = FALSE;
14813
14814 out_off = _bfd_elf_section_offset (output_bfd, info,
14815 input_section, rel->r_offset);
14816 if (out_off == (bfd_vma) -1)
14817 skip = TRUE;
14818 else if (out_off == (bfd_vma) -2)
14819 skip = TRUE, relocate = TRUE;
14820 out_off += (input_section->output_section->vma
14821 + input_section->output_offset);
14822 outrel.r_offset = out_off;
14823 outrel.r_addend = rel->r_addend;
14824
14825 /* Optimize unaligned reloc use. */
14826 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14827 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14828 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14829 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14830 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14831 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14832 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14833 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14834 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14835
14836 if (skip)
14837 memset (&outrel, 0, sizeof outrel);
14838 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14839 && !is_opd
14840 && r_type != R_PPC64_TOC)
14841 {
14842 BFD_ASSERT (h->elf.dynindx != -1);
14843 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14844 }
14845 else
14846 {
14847 /* This symbol is local, or marked to become local,
14848 or this is an opd section reloc which must point
14849 at a local function. */
14850 outrel.r_addend += relocation;
14851 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14852 {
14853 if (is_opd && h != NULL)
14854 {
14855 /* Lie about opd entries. This case occurs
14856 when building shared libraries and we
14857 reference a function in another shared
14858 lib. The same thing happens for a weak
14859 definition in an application that's
14860 overridden by a strong definition in a
14861 shared lib. (I believe this is a generic
14862 bug in binutils handling of weak syms.)
14863 In these cases we won't use the opd
14864 entry in this lib. */
14865 unresolved_reloc = FALSE;
14866 }
14867 if (!is_opd
14868 && r_type == R_PPC64_ADDR64
14869 && (h != NULL
14870 ? h->elf.type == STT_GNU_IFUNC
14871 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14872 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14873 else
14874 {
14875 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14876
14877 /* We need to relocate .opd contents for ld.so.
14878 Prelink also wants simple and consistent rules
14879 for relocs. This make all RELATIVE relocs have
14880 *r_offset equal to r_addend. */
14881 relocate = TRUE;
14882 }
14883 }
14884 else
14885 {
14886 long indx = 0;
14887
14888 if (h != NULL
14889 ? h->elf.type == STT_GNU_IFUNC
14890 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14891 {
14892 info->callbacks->einfo
14893 /* xgettext:c-format */
14894 (_("%P: %H: %s for indirect "
14895 "function `%T' unsupported\n"),
14896 input_bfd, input_section, rel->r_offset,
14897 ppc64_elf_howto_table[r_type]->name,
14898 sym_name);
14899 ret = FALSE;
14900 }
14901 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14902 ;
14903 else if (sec == NULL || sec->owner == NULL)
14904 {
14905 bfd_set_error (bfd_error_bad_value);
14906 return FALSE;
14907 }
14908 else
14909 {
14910 asection *osec;
14911
14912 osec = sec->output_section;
14913 indx = elf_section_data (osec)->dynindx;
14914
14915 if (indx == 0)
14916 {
14917 if ((osec->flags & SEC_READONLY) == 0
14918 && htab->elf.data_index_section != NULL)
14919 osec = htab->elf.data_index_section;
14920 else
14921 osec = htab->elf.text_index_section;
14922 indx = elf_section_data (osec)->dynindx;
14923 }
14924 BFD_ASSERT (indx != 0);
14925
14926 /* We are turning this relocation into one
14927 against a section symbol, so subtract out
14928 the output section's address but not the
14929 offset of the input section in the output
14930 section. */
14931 outrel.r_addend -= osec->vma;
14932 }
14933
14934 outrel.r_info = ELF64_R_INFO (indx, r_type);
14935 }
14936 }
14937
14938 sreloc = elf_section_data (input_section)->sreloc;
14939 if (h != NULL
14940 ? h->elf.type == STT_GNU_IFUNC
14941 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14942 sreloc = htab->elf.irelplt;
14943 if (sreloc == NULL)
14944 abort ();
14945
14946 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14947 >= sreloc->size)
14948 abort ();
14949 loc = sreloc->contents;
14950 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14951 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14952
14953 /* If this reloc is against an external symbol, it will
14954 be computed at runtime, so there's no need to do
14955 anything now. However, for the sake of prelink ensure
14956 that the section contents are a known value. */
14957 if (! relocate)
14958 {
14959 unresolved_reloc = FALSE;
14960 /* The value chosen here is quite arbitrary as ld.so
14961 ignores section contents except for the special
14962 case of .opd where the contents might be accessed
14963 before relocation. Choose zero, as that won't
14964 cause reloc overflow. */
14965 relocation = 0;
14966 addend = 0;
14967 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14968 to improve backward compatibility with older
14969 versions of ld. */
14970 if (r_type == R_PPC64_ADDR64)
14971 addend = outrel.r_addend;
14972 /* Adjust pc_relative relocs to have zero in *r_offset. */
14973 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14974 addend = (input_section->output_section->vma
14975 + input_section->output_offset
14976 + rel->r_offset);
14977 }
14978 }
14979 break;
14980
14981 case R_PPC64_COPY:
14982 case R_PPC64_GLOB_DAT:
14983 case R_PPC64_JMP_SLOT:
14984 case R_PPC64_JMP_IREL:
14985 case R_PPC64_RELATIVE:
14986 /* We shouldn't ever see these dynamic relocs in relocatable
14987 files. */
14988 /* Fall through. */
14989
14990 case R_PPC64_PLTGOT16:
14991 case R_PPC64_PLTGOT16_DS:
14992 case R_PPC64_PLTGOT16_HA:
14993 case R_PPC64_PLTGOT16_HI:
14994 case R_PPC64_PLTGOT16_LO:
14995 case R_PPC64_PLTGOT16_LO_DS:
14996 case R_PPC64_PLTREL32:
14997 case R_PPC64_PLTREL64:
14998 /* These ones haven't been implemented yet. */
14999
15000 info->callbacks->einfo
15001 /* xgettext:c-format */
15002 (_("%P: %B: %s is not supported for `%T'\n"),
15003 input_bfd,
15004 ppc64_elf_howto_table[r_type]->name, sym_name);
15005
15006 bfd_set_error (bfd_error_invalid_operation);
15007 ret = FALSE;
15008 goto copy_reloc;
15009 }
15010
15011 /* Multi-instruction sequences that access the TOC can be
15012 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15013 to nop; addi rb,r2,x; */
15014 switch (r_type)
15015 {
15016 default:
15017 break;
15018
15019 case R_PPC64_GOT_TLSLD16_HI:
15020 case R_PPC64_GOT_TLSGD16_HI:
15021 case R_PPC64_GOT_TPREL16_HI:
15022 case R_PPC64_GOT_DTPREL16_HI:
15023 case R_PPC64_GOT16_HI:
15024 case R_PPC64_TOC16_HI:
15025 /* These relocs would only be useful if building up an
15026 offset to later add to r2, perhaps in an indexed
15027 addressing mode instruction. Don't try to optimize.
15028 Unfortunately, the possibility of someone building up an
15029 offset like this or even with the HA relocs, means that
15030 we need to check the high insn when optimizing the low
15031 insn. */
15032 break;
15033
15034 case R_PPC64_GOT_TLSLD16_HA:
15035 case R_PPC64_GOT_TLSGD16_HA:
15036 case R_PPC64_GOT_TPREL16_HA:
15037 case R_PPC64_GOT_DTPREL16_HA:
15038 case R_PPC64_GOT16_HA:
15039 case R_PPC64_TOC16_HA:
15040 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15041 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15042 {
15043 bfd_byte *p = contents + (rel->r_offset & ~3);
15044 bfd_put_32 (input_bfd, NOP, p);
15045 }
15046 break;
15047
15048 case R_PPC64_GOT_TLSLD16_LO:
15049 case R_PPC64_GOT_TLSGD16_LO:
15050 case R_PPC64_GOT_TPREL16_LO_DS:
15051 case R_PPC64_GOT_DTPREL16_LO_DS:
15052 case R_PPC64_GOT16_LO:
15053 case R_PPC64_GOT16_LO_DS:
15054 case R_PPC64_TOC16_LO:
15055 case R_PPC64_TOC16_LO_DS:
15056 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15057 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15058 {
15059 bfd_byte *p = contents + (rel->r_offset & ~3);
15060 insn = bfd_get_32 (input_bfd, p);
15061 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15062 {
15063 /* Transform addic to addi when we change reg. */
15064 insn &= ~((0x3f << 26) | (0x1f << 16));
15065 insn |= (14u << 26) | (2 << 16);
15066 }
15067 else
15068 {
15069 insn &= ~(0x1f << 16);
15070 insn |= 2 << 16;
15071 }
15072 bfd_put_32 (input_bfd, insn, p);
15073 }
15074 break;
15075 }
15076
15077 /* Do any further special processing. */
15078 howto = ppc64_elf_howto_table[(int) r_type];
15079 switch (r_type)
15080 {
15081 default:
15082 break;
15083
15084 case R_PPC64_REL16_HA:
15085 case R_PPC64_REL16DX_HA:
15086 case R_PPC64_ADDR16_HA:
15087 case R_PPC64_ADDR16_HIGHA:
15088 case R_PPC64_ADDR16_HIGHERA:
15089 case R_PPC64_ADDR16_HIGHESTA:
15090 case R_PPC64_TOC16_HA:
15091 case R_PPC64_SECTOFF_HA:
15092 case R_PPC64_TPREL16_HA:
15093 case R_PPC64_TPREL16_HIGHA:
15094 case R_PPC64_TPREL16_HIGHERA:
15095 case R_PPC64_TPREL16_HIGHESTA:
15096 case R_PPC64_DTPREL16_HA:
15097 case R_PPC64_DTPREL16_HIGHA:
15098 case R_PPC64_DTPREL16_HIGHERA:
15099 case R_PPC64_DTPREL16_HIGHESTA:
15100 /* It's just possible that this symbol is a weak symbol
15101 that's not actually defined anywhere. In that case,
15102 'sec' would be NULL, and we should leave the symbol
15103 alone (it will be set to zero elsewhere in the link). */
15104 if (sec == NULL)
15105 break;
15106 /* Fall through. */
15107
15108 case R_PPC64_GOT16_HA:
15109 case R_PPC64_PLTGOT16_HA:
15110 case R_PPC64_PLT16_HA:
15111 case R_PPC64_GOT_TLSGD16_HA:
15112 case R_PPC64_GOT_TLSLD16_HA:
15113 case R_PPC64_GOT_TPREL16_HA:
15114 case R_PPC64_GOT_DTPREL16_HA:
15115 /* Add 0x10000 if sign bit in 0:15 is set.
15116 Bits 0:15 are not used. */
15117 addend += 0x8000;
15118 break;
15119
15120 case R_PPC64_ADDR16_DS:
15121 case R_PPC64_ADDR16_LO_DS:
15122 case R_PPC64_GOT16_DS:
15123 case R_PPC64_GOT16_LO_DS:
15124 case R_PPC64_PLT16_LO_DS:
15125 case R_PPC64_SECTOFF_DS:
15126 case R_PPC64_SECTOFF_LO_DS:
15127 case R_PPC64_TOC16_DS:
15128 case R_PPC64_TOC16_LO_DS:
15129 case R_PPC64_PLTGOT16_DS:
15130 case R_PPC64_PLTGOT16_LO_DS:
15131 case R_PPC64_GOT_TPREL16_DS:
15132 case R_PPC64_GOT_TPREL16_LO_DS:
15133 case R_PPC64_GOT_DTPREL16_DS:
15134 case R_PPC64_GOT_DTPREL16_LO_DS:
15135 case R_PPC64_TPREL16_DS:
15136 case R_PPC64_TPREL16_LO_DS:
15137 case R_PPC64_DTPREL16_DS:
15138 case R_PPC64_DTPREL16_LO_DS:
15139 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15140 mask = 3;
15141 /* If this reloc is against an lq, lxv, or stxv insn, then
15142 the value must be a multiple of 16. This is somewhat of
15143 a hack, but the "correct" way to do this by defining _DQ
15144 forms of all the _DS relocs bloats all reloc switches in
15145 this file. It doesn't make much sense to use these
15146 relocs in data, so testing the insn should be safe. */
15147 if ((insn & (0x3f << 26)) == (56u << 26)
15148 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15149 mask = 15;
15150 relocation += addend;
15151 addend = insn & (mask ^ 3);
15152 if ((relocation & mask) != 0)
15153 {
15154 relocation ^= relocation & mask;
15155 info->callbacks->einfo
15156 /* xgettext:c-format */
15157 (_("%P: %H: error: %s not a multiple of %u\n"),
15158 input_bfd, input_section, rel->r_offset,
15159 howto->name,
15160 mask + 1);
15161 bfd_set_error (bfd_error_bad_value);
15162 ret = FALSE;
15163 goto copy_reloc;
15164 }
15165 break;
15166 }
15167
15168 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15169 because such sections are not SEC_ALLOC and thus ld.so will
15170 not process them. */
15171 if (unresolved_reloc
15172 && !((input_section->flags & SEC_DEBUGGING) != 0
15173 && h->elf.def_dynamic)
15174 && _bfd_elf_section_offset (output_bfd, info, input_section,
15175 rel->r_offset) != (bfd_vma) -1)
15176 {
15177 info->callbacks->einfo
15178 /* xgettext:c-format */
15179 (_("%P: %H: unresolvable %s against `%T'\n"),
15180 input_bfd, input_section, rel->r_offset,
15181 howto->name,
15182 h->elf.root.root.string);
15183 ret = FALSE;
15184 }
15185
15186 /* 16-bit fields in insns mostly have signed values, but a
15187 few insns have 16-bit unsigned values. Really, we should
15188 have different reloc types. */
15189 if (howto->complain_on_overflow != complain_overflow_dont
15190 && howto->dst_mask == 0xffff
15191 && (input_section->flags & SEC_CODE) != 0)
15192 {
15193 enum complain_overflow complain = complain_overflow_signed;
15194
15195 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15196 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15197 complain = complain_overflow_bitfield;
15198 else if (howto->rightshift == 0
15199 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15200 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15201 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15202 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15203 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15204 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15205 complain = complain_overflow_unsigned;
15206 if (howto->complain_on_overflow != complain)
15207 {
15208 alt_howto = *howto;
15209 alt_howto.complain_on_overflow = complain;
15210 howto = &alt_howto;
15211 }
15212 }
15213
15214 if (r_type == R_PPC64_REL16DX_HA)
15215 {
15216 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15217 if (rel->r_offset + 4 > input_section->size)
15218 r = bfd_reloc_outofrange;
15219 else
15220 {
15221 relocation += addend;
15222 relocation -= (rel->r_offset
15223 + input_section->output_offset
15224 + input_section->output_section->vma);
15225 relocation = (bfd_signed_vma) relocation >> 16;
15226 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15227 insn &= ~0x1fffc1;
15228 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15229 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15230 r = bfd_reloc_ok;
15231 if (relocation + 0x8000 > 0xffff)
15232 r = bfd_reloc_overflow;
15233 }
15234 }
15235 else
15236 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15237 rel->r_offset, relocation, addend);
15238
15239 if (r != bfd_reloc_ok)
15240 {
15241 char *more_info = NULL;
15242 const char *reloc_name = howto->name;
15243
15244 if (reloc_dest != DEST_NORMAL)
15245 {
15246 more_info = bfd_malloc (strlen (reloc_name) + 8);
15247 if (more_info != NULL)
15248 {
15249 strcpy (more_info, reloc_name);
15250 strcat (more_info, (reloc_dest == DEST_OPD
15251 ? " (OPD)" : " (stub)"));
15252 reloc_name = more_info;
15253 }
15254 }
15255
15256 if (r == bfd_reloc_overflow)
15257 {
15258 /* On code like "if (foo) foo();" don't report overflow
15259 on a branch to zero when foo is undefined. */
15260 if (!warned
15261 && (reloc_dest == DEST_STUB
15262 || !(h != NULL
15263 && (h->elf.root.type == bfd_link_hash_undefweak
15264 || h->elf.root.type == bfd_link_hash_undefined)
15265 && is_branch_reloc (r_type))))
15266 info->callbacks->reloc_overflow (info, &h->elf.root,
15267 sym_name, reloc_name,
15268 orig_rel.r_addend,
15269 input_bfd, input_section,
15270 rel->r_offset);
15271 }
15272 else
15273 {
15274 info->callbacks->einfo
15275 /* xgettext:c-format */
15276 (_("%P: %H: %s against `%T': error %d\n"),
15277 input_bfd, input_section, rel->r_offset,
15278 reloc_name, sym_name, (int) r);
15279 ret = FALSE;
15280 }
15281 if (more_info != NULL)
15282 free (more_info);
15283 }
15284 copy_reloc:
15285 if (wrel != rel)
15286 *wrel = *rel;
15287 }
15288
15289 if (wrel != rel)
15290 {
15291 Elf_Internal_Shdr *rel_hdr;
15292 size_t deleted = rel - wrel;
15293
15294 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15295 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15296 if (rel_hdr->sh_size == 0)
15297 {
15298 /* It is too late to remove an empty reloc section. Leave
15299 one NONE reloc.
15300 ??? What is wrong with an empty section??? */
15301 rel_hdr->sh_size = rel_hdr->sh_entsize;
15302 deleted -= 1;
15303 }
15304 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15305 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15306 input_section->reloc_count -= deleted;
15307 }
15308
15309 /* If we're emitting relocations, then shortly after this function
15310 returns, reloc offsets and addends for this section will be
15311 adjusted. Worse, reloc symbol indices will be for the output
15312 file rather than the input. Save a copy of the relocs for
15313 opd_entry_value. */
15314 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15315 {
15316 bfd_size_type amt;
15317 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15318 rel = bfd_alloc (input_bfd, amt);
15319 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15320 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15321 if (rel == NULL)
15322 return FALSE;
15323 memcpy (rel, relocs, amt);
15324 }
15325 return ret;
15326 }
15327
15328 /* Adjust the value of any local symbols in opd sections. */
15329
15330 static int
15331 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15332 const char *name ATTRIBUTE_UNUSED,
15333 Elf_Internal_Sym *elfsym,
15334 asection *input_sec,
15335 struct elf_link_hash_entry *h)
15336 {
15337 struct _opd_sec_data *opd;
15338 long adjust;
15339 bfd_vma value;
15340
15341 if (h != NULL)
15342 return 1;
15343
15344 opd = get_opd_info (input_sec);
15345 if (opd == NULL || opd->adjust == NULL)
15346 return 1;
15347
15348 value = elfsym->st_value - input_sec->output_offset;
15349 if (!bfd_link_relocatable (info))
15350 value -= input_sec->output_section->vma;
15351
15352 adjust = opd->adjust[OPD_NDX (value)];
15353 if (adjust == -1)
15354 return 2;
15355
15356 elfsym->st_value += adjust;
15357 return 1;
15358 }
15359
15360 /* Finish up dynamic symbol handling. We set the contents of various
15361 dynamic sections here. */
15362
15363 static bfd_boolean
15364 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15365 struct bfd_link_info *info,
15366 struct elf_link_hash_entry *h,
15367 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
15368 {
15369 struct ppc_link_hash_table *htab;
15370 struct plt_entry *ent;
15371 Elf_Internal_Rela rela;
15372 bfd_byte *loc;
15373
15374 htab = ppc_hash_table (info);
15375 if (htab == NULL)
15376 return FALSE;
15377
15378 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15379 if (ent->plt.offset != (bfd_vma) -1)
15380 {
15381 /* This symbol has an entry in the procedure linkage
15382 table. Set it up. */
15383 if (!htab->elf.dynamic_sections_created
15384 || h->dynindx == -1)
15385 {
15386 BFD_ASSERT (h->type == STT_GNU_IFUNC
15387 && h->def_regular
15388 && (h->root.type == bfd_link_hash_defined
15389 || h->root.type == bfd_link_hash_defweak));
15390 rela.r_offset = (htab->elf.iplt->output_section->vma
15391 + htab->elf.iplt->output_offset
15392 + ent->plt.offset);
15393 if (htab->opd_abi)
15394 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15395 else
15396 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15397 rela.r_addend = (h->root.u.def.value
15398 + h->root.u.def.section->output_offset
15399 + h->root.u.def.section->output_section->vma
15400 + ent->addend);
15401 loc = (htab->elf.irelplt->contents
15402 + (htab->elf.irelplt->reloc_count++
15403 * sizeof (Elf64_External_Rela)));
15404 }
15405 else
15406 {
15407 rela.r_offset = (htab->elf.splt->output_section->vma
15408 + htab->elf.splt->output_offset
15409 + ent->plt.offset);
15410 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15411 rela.r_addend = ent->addend;
15412 loc = (htab->elf.srelplt->contents
15413 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15414 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15415 }
15416 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15417
15418 if (!htab->opd_abi)
15419 {
15420 if (!h->def_regular)
15421 {
15422 /* Mark the symbol as undefined, rather than as
15423 defined in glink. Leave the value if there were
15424 any relocations where pointer equality matters
15425 (this is a clue for the dynamic linker, to make
15426 function pointer comparisons work between an
15427 application and shared library), otherwise set it
15428 to zero. */
15429 sym->st_shndx = SHN_UNDEF;
15430 if (!h->pointer_equality_needed)
15431 sym->st_value = 0;
15432 else if (!h->ref_regular_nonweak)
15433 {
15434 /* This breaks function pointer comparisons, but
15435 that is better than breaking tests for a NULL
15436 function pointer. */
15437 sym->st_value = 0;
15438 }
15439 }
15440 }
15441 }
15442
15443 if (h->needs_copy)
15444 {
15445 /* This symbol needs a copy reloc. Set it up. */
15446
15447 if (h->dynindx == -1
15448 || (h->root.type != bfd_link_hash_defined
15449 && h->root.type != bfd_link_hash_defweak)
15450 || htab->relbss == NULL)
15451 abort ();
15452
15453 rela.r_offset = (h->root.u.def.value
15454 + h->root.u.def.section->output_section->vma
15455 + h->root.u.def.section->output_offset);
15456 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15457 rela.r_addend = 0;
15458 loc = htab->relbss->contents;
15459 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
15460 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15461 }
15462
15463 return TRUE;
15464 }
15465
15466 /* Used to decide how to sort relocs in an optimal manner for the
15467 dynamic linker, before writing them out. */
15468
15469 static enum elf_reloc_type_class
15470 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15471 const asection *rel_sec,
15472 const Elf_Internal_Rela *rela)
15473 {
15474 enum elf_ppc64_reloc_type r_type;
15475 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15476
15477 if (rel_sec == htab->elf.irelplt)
15478 return reloc_class_ifunc;
15479
15480 r_type = ELF64_R_TYPE (rela->r_info);
15481 switch (r_type)
15482 {
15483 case R_PPC64_RELATIVE:
15484 return reloc_class_relative;
15485 case R_PPC64_JMP_SLOT:
15486 return reloc_class_plt;
15487 case R_PPC64_COPY:
15488 return reloc_class_copy;
15489 default:
15490 return reloc_class_normal;
15491 }
15492 }
15493
15494 /* Finish up the dynamic sections. */
15495
15496 static bfd_boolean
15497 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15498 struct bfd_link_info *info)
15499 {
15500 struct ppc_link_hash_table *htab;
15501 bfd *dynobj;
15502 asection *sdyn;
15503
15504 htab = ppc_hash_table (info);
15505 if (htab == NULL)
15506 return FALSE;
15507
15508 dynobj = htab->elf.dynobj;
15509 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15510
15511 if (htab->elf.dynamic_sections_created)
15512 {
15513 Elf64_External_Dyn *dyncon, *dynconend;
15514
15515 if (sdyn == NULL || htab->elf.sgot == NULL)
15516 abort ();
15517
15518 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15519 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15520 for (; dyncon < dynconend; dyncon++)
15521 {
15522 Elf_Internal_Dyn dyn;
15523 asection *s;
15524
15525 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15526
15527 switch (dyn.d_tag)
15528 {
15529 default:
15530 continue;
15531
15532 case DT_PPC64_GLINK:
15533 s = htab->glink;
15534 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15535 /* We stupidly defined DT_PPC64_GLINK to be the start
15536 of glink rather than the first entry point, which is
15537 what ld.so needs, and now have a bigger stub to
15538 support automatic multiple TOCs. */
15539 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15540 break;
15541
15542 case DT_PPC64_OPD:
15543 s = bfd_get_section_by_name (output_bfd, ".opd");
15544 if (s == NULL)
15545 continue;
15546 dyn.d_un.d_ptr = s->vma;
15547 break;
15548
15549 case DT_PPC64_OPT:
15550 if (htab->do_multi_toc && htab->multi_toc_needed)
15551 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15552 break;
15553
15554 case DT_PPC64_OPDSZ:
15555 s = bfd_get_section_by_name (output_bfd, ".opd");
15556 if (s == NULL)
15557 continue;
15558 dyn.d_un.d_val = s->size;
15559 break;
15560
15561 case DT_PLTGOT:
15562 s = htab->elf.splt;
15563 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15564 break;
15565
15566 case DT_JMPREL:
15567 s = htab->elf.srelplt;
15568 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15569 break;
15570
15571 case DT_PLTRELSZ:
15572 dyn.d_un.d_val = htab->elf.srelplt->size;
15573 break;
15574 }
15575
15576 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15577 }
15578 }
15579
15580 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15581 {
15582 /* Fill in the first entry in the global offset table.
15583 We use it to hold the link-time TOCbase. */
15584 bfd_put_64 (output_bfd,
15585 elf_gp (output_bfd) + TOC_BASE_OFF,
15586 htab->elf.sgot->contents);
15587
15588 /* Set .got entry size. */
15589 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15590 }
15591
15592 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15593 {
15594 /* Set .plt entry size. */
15595 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15596 = PLT_ENTRY_SIZE (htab);
15597 }
15598
15599 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15600 brlt ourselves if emitrelocations. */
15601 if (htab->brlt != NULL
15602 && htab->brlt->reloc_count != 0
15603 && !_bfd_elf_link_output_relocs (output_bfd,
15604 htab->brlt,
15605 elf_section_data (htab->brlt)->rela.hdr,
15606 elf_section_data (htab->brlt)->relocs,
15607 NULL))
15608 return FALSE;
15609
15610 if (htab->glink != NULL
15611 && htab->glink->reloc_count != 0
15612 && !_bfd_elf_link_output_relocs (output_bfd,
15613 htab->glink,
15614 elf_section_data (htab->glink)->rela.hdr,
15615 elf_section_data (htab->glink)->relocs,
15616 NULL))
15617 return FALSE;
15618
15619 if (htab->glink_eh_frame != NULL
15620 && htab->glink_eh_frame->size != 0)
15621 {
15622 bfd_vma val;
15623 bfd_byte *p;
15624 asection *stub_sec;
15625
15626 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15627 for (stub_sec = htab->params->stub_bfd->sections;
15628 stub_sec != NULL;
15629 stub_sec = stub_sec->next)
15630 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15631 {
15632 /* FDE length. */
15633 p += 4;
15634 /* CIE pointer. */
15635 p += 4;
15636 /* Offset to stub section. */
15637 val = (stub_sec->output_section->vma
15638 + stub_sec->output_offset);
15639 val -= (htab->glink_eh_frame->output_section->vma
15640 + htab->glink_eh_frame->output_offset
15641 + (p - htab->glink_eh_frame->contents));
15642 if (val + 0x80000000 > 0xffffffff)
15643 {
15644 info->callbacks->einfo
15645 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15646 stub_sec->name);
15647 return FALSE;
15648 }
15649 bfd_put_32 (dynobj, val, p);
15650 p += 4;
15651 /* stub section size. */
15652 p += 4;
15653 /* Augmentation. */
15654 p += 1;
15655 /* Pad. */
15656 p += 7;
15657 }
15658 if (htab->glink != NULL && htab->glink->size != 0)
15659 {
15660 /* FDE length. */
15661 p += 4;
15662 /* CIE pointer. */
15663 p += 4;
15664 /* Offset to .glink. */
15665 val = (htab->glink->output_section->vma
15666 + htab->glink->output_offset
15667 + 8);
15668 val -= (htab->glink_eh_frame->output_section->vma
15669 + htab->glink_eh_frame->output_offset
15670 + (p - htab->glink_eh_frame->contents));
15671 if (val + 0x80000000 > 0xffffffff)
15672 {
15673 info->callbacks->einfo
15674 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15675 htab->glink->name);
15676 return FALSE;
15677 }
15678 bfd_put_32 (dynobj, val, p);
15679 p += 4;
15680 /* .glink size. */
15681 p += 4;
15682 /* Augmentation. */
15683 p += 1;
15684 /* Ops. */
15685 p += 7;
15686 }
15687
15688 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15689 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15690 htab->glink_eh_frame,
15691 htab->glink_eh_frame->contents))
15692 return FALSE;
15693 }
15694
15695 /* We need to handle writing out multiple GOT sections ourselves,
15696 since we didn't add them to DYNOBJ. We know dynobj is the first
15697 bfd. */
15698 while ((dynobj = dynobj->link.next) != NULL)
15699 {
15700 asection *s;
15701
15702 if (!is_ppc64_elf (dynobj))
15703 continue;
15704
15705 s = ppc64_elf_tdata (dynobj)->got;
15706 if (s != NULL
15707 && s->size != 0
15708 && s->output_section != bfd_abs_section_ptr
15709 && !bfd_set_section_contents (output_bfd, s->output_section,
15710 s->contents, s->output_offset,
15711 s->size))
15712 return FALSE;
15713 s = ppc64_elf_tdata (dynobj)->relgot;
15714 if (s != NULL
15715 && s->size != 0
15716 && s->output_section != bfd_abs_section_ptr
15717 && !bfd_set_section_contents (output_bfd, s->output_section,
15718 s->contents, s->output_offset,
15719 s->size))
15720 return FALSE;
15721 }
15722
15723 return TRUE;
15724 }
15725
15726 #include "elf64-target.h"
15727
15728 /* FreeBSD support */
15729
15730 #undef TARGET_LITTLE_SYM
15731 #undef TARGET_LITTLE_NAME
15732
15733 #undef TARGET_BIG_SYM
15734 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15735 #undef TARGET_BIG_NAME
15736 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15737
15738 #undef ELF_OSABI
15739 #define ELF_OSABI ELFOSABI_FREEBSD
15740
15741 #undef elf64_bed
15742 #define elf64_bed elf64_powerpc_fbsd_bed
15743
15744 #include "elf64-target.h"
15745