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