dynrelro section for read-only dynamic symbols copied into executable
[binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2016 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x10000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_want_dynrelro 1
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_dtrel_excludes_plt 1
80 #define elf_backend_default_execstack 0
81
82 #define bfd_elf64_mkobject ppc64_elf_mkobject
83 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
84 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
85 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
87 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
89 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
91 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
92
93 #define elf_backend_object_p ppc64_elf_object_p
94 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
96 #define elf_backend_write_core_note ppc64_elf_write_core_note
97 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
98 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
99 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
100 #define elf_backend_check_directives ppc64_elf_before_check_relocs
101 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
102 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
103 #define elf_backend_check_relocs ppc64_elf_check_relocs
104 #define elf_backend_gc_keep ppc64_elf_gc_keep
105 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
106 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
107 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
108 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
110 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
111 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
113 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
114 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
115 #define elf_backend_action_discarded ppc64_elf_action_discarded
116 #define elf_backend_relocate_section ppc64_elf_relocate_section
117 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
120 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
121 #define elf_backend_special_sections ppc64_elf_special_sections
122 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
123 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
124
125 /* The name of the dynamic interpreter. This is put in the .interp
126 section. */
127 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
128
129 /* The size in bytes of an entry in the procedure linkage table. */
130 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
131
132 /* The initial size of the plt reserved for the dynamic linker. */
133 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
134
135 /* Offsets to some stack save slots. */
136 #define STK_LR 16
137 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
138 /* This one is dodgy. ELFv2 does not have a linker word, so use the
139 CR save slot. Used only by optimised __tls_get_addr call stub,
140 relying on __tls_get_addr_opt not saving CR.. */
141 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
142
143 /* TOC base pointers offset from start of TOC. */
144 #define TOC_BASE_OFF 0x8000
145 /* TOC base alignment. */
146 #define TOC_BASE_ALIGN 256
147
148 /* Offset of tp and dtp pointers from start of TLS block. */
149 #define TP_OFFSET 0x7000
150 #define DTP_OFFSET 0x8000
151
152 /* .plt call stub instructions. The normal stub is like this, but
153 sometimes the .plt entry crosses a 64k boundary and we need to
154 insert an addi to adjust r11. */
155 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
156 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
157 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
158 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
159 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
160 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
161 #define BCTR 0x4e800420 /* bctr */
162
163 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
164 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
165 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
166
167 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
168 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
169 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
170 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
171 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
172 #define BNECTR 0x4ca20420 /* bnectr+ */
173 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
174
175 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
176 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
177 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
178
179 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
180 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
181 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
182
183 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
184 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
185 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
186 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
187 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
188
189 /* glink call stub instructions. We enter with the index in R0. */
190 #define GLINK_CALL_STUB_SIZE (16*4)
191 /* 0: */
192 /* .quad plt0-1f */
193 /* __glink: */
194 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
195 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
196 /* 1: */
197 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
198 /* ld %2,(0b-1b)(%11) */
199 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
200 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
201 /* ld %12,0(%11) */
202 /* ld %2,8(%11) */
203 /* mtctr %12 */
204 /* ld %11,16(%11) */
205 /* bctr */
206 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
207 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
208 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
209 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
210 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
211
212 /* Pad with this. */
213 #define NOP 0x60000000
214
215 /* Some other nops. */
216 #define CROR_151515 0x4def7b82
217 #define CROR_313131 0x4ffffb82
218
219 /* .glink entries for the first 32k functions are two instructions. */
220 #define LI_R0_0 0x38000000 /* li %r0,0 */
221 #define B_DOT 0x48000000 /* b . */
222
223 /* After that, we need two instructions to load the index, followed by
224 a branch. */
225 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
226 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
227
228 /* Instructions used by the save and restore reg functions. */
229 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
230 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
231 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
232 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
233 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
234 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
235 #define LI_R12_0 0x39800000 /* li %r12,0 */
236 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
237 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
238 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
239 #define BLR 0x4e800020 /* blr */
240
241 /* Since .opd is an array of descriptors and each entry will end up
242 with identical R_PPC64_RELATIVE relocs, there is really no need to
243 propagate .opd relocs; The dynamic linker should be taught to
244 relocate .opd without reloc entries. */
245 #ifndef NO_OPD_RELOCS
246 #define NO_OPD_RELOCS 0
247 #endif
248
249 #ifndef ARRAY_SIZE
250 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
251 #endif
252
253 static inline int
254 abiversion (bfd *abfd)
255 {
256 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
257 }
258
259 static inline void
260 set_abiversion (bfd *abfd, int ver)
261 {
262 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
263 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
264 }
265 \f
266 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
267
268 /* Relocation HOWTO's. */
269 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
270
271 static reloc_howto_type ppc64_elf_howto_raw[] = {
272 /* This reloc does nothing. */
273 HOWTO (R_PPC64_NONE, /* type */
274 0, /* rightshift */
275 3, /* size (0 = byte, 1 = short, 2 = long) */
276 0, /* bitsize */
277 FALSE, /* pc_relative */
278 0, /* bitpos */
279 complain_overflow_dont, /* complain_on_overflow */
280 bfd_elf_generic_reloc, /* special_function */
281 "R_PPC64_NONE", /* name */
282 FALSE, /* partial_inplace */
283 0, /* src_mask */
284 0, /* dst_mask */
285 FALSE), /* pcrel_offset */
286
287 /* A standard 32 bit relocation. */
288 HOWTO (R_PPC64_ADDR32, /* type */
289 0, /* rightshift */
290 2, /* size (0 = byte, 1 = short, 2 = long) */
291 32, /* bitsize */
292 FALSE, /* pc_relative */
293 0, /* bitpos */
294 complain_overflow_bitfield, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_PPC64_ADDR32", /* name */
297 FALSE, /* partial_inplace */
298 0, /* src_mask */
299 0xffffffff, /* dst_mask */
300 FALSE), /* pcrel_offset */
301
302 /* An absolute 26 bit branch; the lower two bits must be zero.
303 FIXME: we don't check that, we just clear them. */
304 HOWTO (R_PPC64_ADDR24, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 26, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield, /* complain_on_overflow */
311 bfd_elf_generic_reloc, /* special_function */
312 "R_PPC64_ADDR24", /* name */
313 FALSE, /* partial_inplace */
314 0, /* src_mask */
315 0x03fffffc, /* dst_mask */
316 FALSE), /* pcrel_offset */
317
318 /* A standard 16 bit relocation. */
319 HOWTO (R_PPC64_ADDR16, /* type */
320 0, /* rightshift */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
322 16, /* bitsize */
323 FALSE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* special_function */
327 "R_PPC64_ADDR16", /* name */
328 FALSE, /* partial_inplace */
329 0, /* src_mask */
330 0xffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
332
333 /* A 16 bit relocation without overflow. */
334 HOWTO (R_PPC64_ADDR16_LO, /* type */
335 0, /* rightshift */
336 1, /* size (0 = byte, 1 = short, 2 = long) */
337 16, /* bitsize */
338 FALSE, /* pc_relative */
339 0, /* bitpos */
340 complain_overflow_dont,/* complain_on_overflow */
341 bfd_elf_generic_reloc, /* special_function */
342 "R_PPC64_ADDR16_LO", /* name */
343 FALSE, /* partial_inplace */
344 0, /* src_mask */
345 0xffff, /* dst_mask */
346 FALSE), /* pcrel_offset */
347
348 /* Bits 16-31 of an address. */
349 HOWTO (R_PPC64_ADDR16_HI, /* type */
350 16, /* rightshift */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
352 16, /* bitsize */
353 FALSE, /* pc_relative */
354 0, /* bitpos */
355 complain_overflow_signed, /* complain_on_overflow */
356 bfd_elf_generic_reloc, /* special_function */
357 "R_PPC64_ADDR16_HI", /* name */
358 FALSE, /* partial_inplace */
359 0, /* src_mask */
360 0xffff, /* dst_mask */
361 FALSE), /* pcrel_offset */
362
363 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
364 bits, treated as a signed number, is negative. */
365 HOWTO (R_PPC64_ADDR16_HA, /* type */
366 16, /* rightshift */
367 1, /* size (0 = byte, 1 = short, 2 = long) */
368 16, /* bitsize */
369 FALSE, /* pc_relative */
370 0, /* bitpos */
371 complain_overflow_signed, /* complain_on_overflow */
372 ppc64_elf_ha_reloc, /* special_function */
373 "R_PPC64_ADDR16_HA", /* name */
374 FALSE, /* partial_inplace */
375 0, /* src_mask */
376 0xffff, /* dst_mask */
377 FALSE), /* pcrel_offset */
378
379 /* An absolute 16 bit branch; the lower two bits must be zero.
380 FIXME: we don't check that, we just clear them. */
381 HOWTO (R_PPC64_ADDR14, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
385 FALSE, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_signed, /* complain_on_overflow */
388 ppc64_elf_branch_reloc, /* special_function */
389 "R_PPC64_ADDR14", /* name */
390 FALSE, /* partial_inplace */
391 0, /* src_mask */
392 0x0000fffc, /* dst_mask */
393 FALSE), /* pcrel_offset */
394
395 /* An absolute 16 bit branch, for which bit 10 should be set to
396 indicate that the branch is expected to be taken. The lower two
397 bits must be zero. */
398 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
399 0, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 16, /* bitsize */
402 FALSE, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_signed, /* complain_on_overflow */
405 ppc64_elf_brtaken_reloc, /* special_function */
406 "R_PPC64_ADDR14_BRTAKEN",/* name */
407 FALSE, /* partial_inplace */
408 0, /* src_mask */
409 0x0000fffc, /* dst_mask */
410 FALSE), /* pcrel_offset */
411
412 /* An absolute 16 bit branch, for which bit 10 should be set to
413 indicate that the branch is not expected to be taken. The lower
414 two bits must be zero. */
415 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
416 0, /* rightshift */
417 2, /* size (0 = byte, 1 = short, 2 = long) */
418 16, /* bitsize */
419 FALSE, /* pc_relative */
420 0, /* bitpos */
421 complain_overflow_signed, /* complain_on_overflow */
422 ppc64_elf_brtaken_reloc, /* special_function */
423 "R_PPC64_ADDR14_BRNTAKEN",/* name */
424 FALSE, /* partial_inplace */
425 0, /* src_mask */
426 0x0000fffc, /* dst_mask */
427 FALSE), /* pcrel_offset */
428
429 /* A relative 26 bit branch; the lower two bits must be zero. */
430 HOWTO (R_PPC64_REL24, /* type */
431 0, /* rightshift */
432 2, /* size (0 = byte, 1 = short, 2 = long) */
433 26, /* bitsize */
434 TRUE, /* pc_relative */
435 0, /* bitpos */
436 complain_overflow_signed, /* complain_on_overflow */
437 ppc64_elf_branch_reloc, /* special_function */
438 "R_PPC64_REL24", /* name */
439 FALSE, /* partial_inplace */
440 0, /* src_mask */
441 0x03fffffc, /* dst_mask */
442 TRUE), /* pcrel_offset */
443
444 /* A relative 16 bit branch; the lower two bits must be zero. */
445 HOWTO (R_PPC64_REL14, /* type */
446 0, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 16, /* bitsize */
449 TRUE, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_signed, /* complain_on_overflow */
452 ppc64_elf_branch_reloc, /* special_function */
453 "R_PPC64_REL14", /* name */
454 FALSE, /* partial_inplace */
455 0, /* src_mask */
456 0x0000fffc, /* dst_mask */
457 TRUE), /* pcrel_offset */
458
459 /* A relative 16 bit branch. Bit 10 should be set to indicate that
460 the branch is expected to be taken. The lower two bits must be
461 zero. */
462 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
463 0, /* rightshift */
464 2, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
466 TRUE, /* pc_relative */
467 0, /* bitpos */
468 complain_overflow_signed, /* complain_on_overflow */
469 ppc64_elf_brtaken_reloc, /* special_function */
470 "R_PPC64_REL14_BRTAKEN", /* name */
471 FALSE, /* partial_inplace */
472 0, /* src_mask */
473 0x0000fffc, /* dst_mask */
474 TRUE), /* pcrel_offset */
475
476 /* A relative 16 bit branch. Bit 10 should be set to indicate that
477 the branch is not expected to be taken. The lower two bits must
478 be zero. */
479 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
480 0, /* rightshift */
481 2, /* size (0 = byte, 1 = short, 2 = long) */
482 16, /* bitsize */
483 TRUE, /* pc_relative */
484 0, /* bitpos */
485 complain_overflow_signed, /* complain_on_overflow */
486 ppc64_elf_brtaken_reloc, /* special_function */
487 "R_PPC64_REL14_BRNTAKEN",/* name */
488 FALSE, /* partial_inplace */
489 0, /* src_mask */
490 0x0000fffc, /* dst_mask */
491 TRUE), /* pcrel_offset */
492
493 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
494 symbol. */
495 HOWTO (R_PPC64_GOT16, /* type */
496 0, /* rightshift */
497 1, /* size (0 = byte, 1 = short, 2 = long) */
498 16, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_signed, /* complain_on_overflow */
502 ppc64_elf_unhandled_reloc, /* special_function */
503 "R_PPC64_GOT16", /* name */
504 FALSE, /* partial_inplace */
505 0, /* src_mask */
506 0xffff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
510 the symbol. */
511 HOWTO (R_PPC64_GOT16_LO, /* type */
512 0, /* rightshift */
513 1, /* size (0 = byte, 1 = short, 2 = long) */
514 16, /* bitsize */
515 FALSE, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
519 "R_PPC64_GOT16_LO", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 0xffff, /* dst_mask */
523 FALSE), /* pcrel_offset */
524
525 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
526 the symbol. */
527 HOWTO (R_PPC64_GOT16_HI, /* type */
528 16, /* rightshift */
529 1, /* size (0 = byte, 1 = short, 2 = long) */
530 16, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_signed,/* complain_on_overflow */
534 ppc64_elf_unhandled_reloc, /* special_function */
535 "R_PPC64_GOT16_HI", /* name */
536 FALSE, /* partial_inplace */
537 0, /* src_mask */
538 0xffff, /* dst_mask */
539 FALSE), /* pcrel_offset */
540
541 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
542 the symbol. */
543 HOWTO (R_PPC64_GOT16_HA, /* type */
544 16, /* rightshift */
545 1, /* size (0 = byte, 1 = short, 2 = long) */
546 16, /* bitsize */
547 FALSE, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_signed,/* complain_on_overflow */
550 ppc64_elf_unhandled_reloc, /* special_function */
551 "R_PPC64_GOT16_HA", /* name */
552 FALSE, /* partial_inplace */
553 0, /* src_mask */
554 0xffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
556
557 /* This is used only by the dynamic linker. The symbol should exist
558 both in the object being run and in some shared library. The
559 dynamic linker copies the data addressed by the symbol from the
560 shared library into the object, because the object being
561 run has to have the data at some particular address. */
562 HOWTO (R_PPC64_COPY, /* type */
563 0, /* rightshift */
564 0, /* this one is variable size */
565 0, /* bitsize */
566 FALSE, /* pc_relative */
567 0, /* bitpos */
568 complain_overflow_dont, /* complain_on_overflow */
569 ppc64_elf_unhandled_reloc, /* special_function */
570 "R_PPC64_COPY", /* name */
571 FALSE, /* partial_inplace */
572 0, /* src_mask */
573 0, /* dst_mask */
574 FALSE), /* pcrel_offset */
575
576 /* Like R_PPC64_ADDR64, but used when setting global offset table
577 entries. */
578 HOWTO (R_PPC64_GLOB_DAT, /* type */
579 0, /* rightshift */
580 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
581 64, /* bitsize */
582 FALSE, /* pc_relative */
583 0, /* bitpos */
584 complain_overflow_dont, /* complain_on_overflow */
585 ppc64_elf_unhandled_reloc, /* special_function */
586 "R_PPC64_GLOB_DAT", /* name */
587 FALSE, /* partial_inplace */
588 0, /* src_mask */
589 ONES (64), /* dst_mask */
590 FALSE), /* pcrel_offset */
591
592 /* Created by the link editor. Marks a procedure linkage table
593 entry for a symbol. */
594 HOWTO (R_PPC64_JMP_SLOT, /* type */
595 0, /* rightshift */
596 0, /* size (0 = byte, 1 = short, 2 = long) */
597 0, /* bitsize */
598 FALSE, /* pc_relative */
599 0, /* bitpos */
600 complain_overflow_dont, /* complain_on_overflow */
601 ppc64_elf_unhandled_reloc, /* special_function */
602 "R_PPC64_JMP_SLOT", /* name */
603 FALSE, /* partial_inplace */
604 0, /* src_mask */
605 0, /* dst_mask */
606 FALSE), /* pcrel_offset */
607
608 /* Used only by the dynamic linker. When the object is run, this
609 doubleword64 is set to the load address of the object, plus the
610 addend. */
611 HOWTO (R_PPC64_RELATIVE, /* type */
612 0, /* rightshift */
613 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
614 64, /* bitsize */
615 FALSE, /* pc_relative */
616 0, /* bitpos */
617 complain_overflow_dont, /* complain_on_overflow */
618 bfd_elf_generic_reloc, /* special_function */
619 "R_PPC64_RELATIVE", /* name */
620 FALSE, /* partial_inplace */
621 0, /* src_mask */
622 ONES (64), /* dst_mask */
623 FALSE), /* pcrel_offset */
624
625 /* Like R_PPC64_ADDR32, but may be unaligned. */
626 HOWTO (R_PPC64_UADDR32, /* type */
627 0, /* rightshift */
628 2, /* size (0 = byte, 1 = short, 2 = long) */
629 32, /* bitsize */
630 FALSE, /* pc_relative */
631 0, /* bitpos */
632 complain_overflow_bitfield, /* complain_on_overflow */
633 bfd_elf_generic_reloc, /* special_function */
634 "R_PPC64_UADDR32", /* name */
635 FALSE, /* partial_inplace */
636 0, /* src_mask */
637 0xffffffff, /* dst_mask */
638 FALSE), /* pcrel_offset */
639
640 /* Like R_PPC64_ADDR16, but may be unaligned. */
641 HOWTO (R_PPC64_UADDR16, /* type */
642 0, /* rightshift */
643 1, /* size (0 = byte, 1 = short, 2 = long) */
644 16, /* bitsize */
645 FALSE, /* pc_relative */
646 0, /* bitpos */
647 complain_overflow_bitfield, /* complain_on_overflow */
648 bfd_elf_generic_reloc, /* special_function */
649 "R_PPC64_UADDR16", /* name */
650 FALSE, /* partial_inplace */
651 0, /* src_mask */
652 0xffff, /* dst_mask */
653 FALSE), /* pcrel_offset */
654
655 /* 32-bit PC relative. */
656 HOWTO (R_PPC64_REL32, /* type */
657 0, /* rightshift */
658 2, /* size (0 = byte, 1 = short, 2 = long) */
659 32, /* bitsize */
660 TRUE, /* pc_relative */
661 0, /* bitpos */
662 complain_overflow_signed, /* complain_on_overflow */
663 bfd_elf_generic_reloc, /* special_function */
664 "R_PPC64_REL32", /* name */
665 FALSE, /* partial_inplace */
666 0, /* src_mask */
667 0xffffffff, /* dst_mask */
668 TRUE), /* pcrel_offset */
669
670 /* 32-bit relocation to the symbol's procedure linkage table. */
671 HOWTO (R_PPC64_PLT32, /* type */
672 0, /* rightshift */
673 2, /* size (0 = byte, 1 = short, 2 = long) */
674 32, /* bitsize */
675 FALSE, /* pc_relative */
676 0, /* bitpos */
677 complain_overflow_bitfield, /* complain_on_overflow */
678 ppc64_elf_unhandled_reloc, /* special_function */
679 "R_PPC64_PLT32", /* name */
680 FALSE, /* partial_inplace */
681 0, /* src_mask */
682 0xffffffff, /* dst_mask */
683 FALSE), /* pcrel_offset */
684
685 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
686 FIXME: R_PPC64_PLTREL32 not supported. */
687 HOWTO (R_PPC64_PLTREL32, /* type */
688 0, /* rightshift */
689 2, /* size (0 = byte, 1 = short, 2 = long) */
690 32, /* bitsize */
691 TRUE, /* pc_relative */
692 0, /* bitpos */
693 complain_overflow_signed, /* complain_on_overflow */
694 ppc64_elf_unhandled_reloc, /* special_function */
695 "R_PPC64_PLTREL32", /* name */
696 FALSE, /* partial_inplace */
697 0, /* src_mask */
698 0xffffffff, /* dst_mask */
699 TRUE), /* pcrel_offset */
700
701 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
702 the symbol. */
703 HOWTO (R_PPC64_PLT16_LO, /* type */
704 0, /* rightshift */
705 1, /* size (0 = byte, 1 = short, 2 = long) */
706 16, /* bitsize */
707 FALSE, /* pc_relative */
708 0, /* bitpos */
709 complain_overflow_dont, /* complain_on_overflow */
710 ppc64_elf_unhandled_reloc, /* special_function */
711 "R_PPC64_PLT16_LO", /* name */
712 FALSE, /* partial_inplace */
713 0, /* src_mask */
714 0xffff, /* dst_mask */
715 FALSE), /* pcrel_offset */
716
717 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
718 the symbol. */
719 HOWTO (R_PPC64_PLT16_HI, /* type */
720 16, /* rightshift */
721 1, /* size (0 = byte, 1 = short, 2 = long) */
722 16, /* bitsize */
723 FALSE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_signed, /* complain_on_overflow */
726 ppc64_elf_unhandled_reloc, /* special_function */
727 "R_PPC64_PLT16_HI", /* name */
728 FALSE, /* partial_inplace */
729 0, /* src_mask */
730 0xffff, /* dst_mask */
731 FALSE), /* pcrel_offset */
732
733 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
734 the symbol. */
735 HOWTO (R_PPC64_PLT16_HA, /* type */
736 16, /* rightshift */
737 1, /* size (0 = byte, 1 = short, 2 = long) */
738 16, /* bitsize */
739 FALSE, /* pc_relative */
740 0, /* bitpos */
741 complain_overflow_signed, /* complain_on_overflow */
742 ppc64_elf_unhandled_reloc, /* special_function */
743 "R_PPC64_PLT16_HA", /* name */
744 FALSE, /* partial_inplace */
745 0, /* src_mask */
746 0xffff, /* dst_mask */
747 FALSE), /* pcrel_offset */
748
749 /* 16-bit section relative relocation. */
750 HOWTO (R_PPC64_SECTOFF, /* type */
751 0, /* rightshift */
752 1, /* size (0 = byte, 1 = short, 2 = long) */
753 16, /* bitsize */
754 FALSE, /* pc_relative */
755 0, /* bitpos */
756 complain_overflow_signed, /* complain_on_overflow */
757 ppc64_elf_sectoff_reloc, /* special_function */
758 "R_PPC64_SECTOFF", /* name */
759 FALSE, /* partial_inplace */
760 0, /* src_mask */
761 0xffff, /* dst_mask */
762 FALSE), /* pcrel_offset */
763
764 /* Like R_PPC64_SECTOFF, but no overflow warning. */
765 HOWTO (R_PPC64_SECTOFF_LO, /* type */
766 0, /* rightshift */
767 1, /* size (0 = byte, 1 = short, 2 = long) */
768 16, /* bitsize */
769 FALSE, /* pc_relative */
770 0, /* bitpos */
771 complain_overflow_dont, /* complain_on_overflow */
772 ppc64_elf_sectoff_reloc, /* special_function */
773 "R_PPC64_SECTOFF_LO", /* name */
774 FALSE, /* partial_inplace */
775 0, /* src_mask */
776 0xffff, /* dst_mask */
777 FALSE), /* pcrel_offset */
778
779 /* 16-bit upper half section relative relocation. */
780 HOWTO (R_PPC64_SECTOFF_HI, /* type */
781 16, /* rightshift */
782 1, /* size (0 = byte, 1 = short, 2 = long) */
783 16, /* bitsize */
784 FALSE, /* pc_relative */
785 0, /* bitpos */
786 complain_overflow_signed, /* complain_on_overflow */
787 ppc64_elf_sectoff_reloc, /* special_function */
788 "R_PPC64_SECTOFF_HI", /* name */
789 FALSE, /* partial_inplace */
790 0, /* src_mask */
791 0xffff, /* dst_mask */
792 FALSE), /* pcrel_offset */
793
794 /* 16-bit upper half adjusted section relative relocation. */
795 HOWTO (R_PPC64_SECTOFF_HA, /* type */
796 16, /* rightshift */
797 1, /* size (0 = byte, 1 = short, 2 = long) */
798 16, /* bitsize */
799 FALSE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_signed, /* complain_on_overflow */
802 ppc64_elf_sectoff_ha_reloc, /* special_function */
803 "R_PPC64_SECTOFF_HA", /* name */
804 FALSE, /* partial_inplace */
805 0, /* src_mask */
806 0xffff, /* dst_mask */
807 FALSE), /* pcrel_offset */
808
809 /* Like R_PPC64_REL24 without touching the two least significant bits. */
810 HOWTO (R_PPC64_REL30, /* type */
811 2, /* rightshift */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
813 30, /* bitsize */
814 TRUE, /* pc_relative */
815 0, /* bitpos */
816 complain_overflow_dont, /* complain_on_overflow */
817 bfd_elf_generic_reloc, /* special_function */
818 "R_PPC64_REL30", /* name */
819 FALSE, /* partial_inplace */
820 0, /* src_mask */
821 0xfffffffc, /* dst_mask */
822 TRUE), /* pcrel_offset */
823
824 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
825
826 /* A standard 64-bit relocation. */
827 HOWTO (R_PPC64_ADDR64, /* type */
828 0, /* rightshift */
829 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
830 64, /* bitsize */
831 FALSE, /* pc_relative */
832 0, /* bitpos */
833 complain_overflow_dont, /* complain_on_overflow */
834 bfd_elf_generic_reloc, /* special_function */
835 "R_PPC64_ADDR64", /* name */
836 FALSE, /* partial_inplace */
837 0, /* src_mask */
838 ONES (64), /* dst_mask */
839 FALSE), /* pcrel_offset */
840
841 /* The bits 32-47 of an address. */
842 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
843 32, /* rightshift */
844 1, /* size (0 = byte, 1 = short, 2 = long) */
845 16, /* bitsize */
846 FALSE, /* pc_relative */
847 0, /* bitpos */
848 complain_overflow_dont, /* complain_on_overflow */
849 bfd_elf_generic_reloc, /* special_function */
850 "R_PPC64_ADDR16_HIGHER", /* name */
851 FALSE, /* partial_inplace */
852 0, /* src_mask */
853 0xffff, /* dst_mask */
854 FALSE), /* pcrel_offset */
855
856 /* The bits 32-47 of an address, plus 1 if the contents of the low
857 16 bits, treated as a signed number, is negative. */
858 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
859 32, /* rightshift */
860 1, /* size (0 = byte, 1 = short, 2 = long) */
861 16, /* bitsize */
862 FALSE, /* pc_relative */
863 0, /* bitpos */
864 complain_overflow_dont, /* complain_on_overflow */
865 ppc64_elf_ha_reloc, /* special_function */
866 "R_PPC64_ADDR16_HIGHERA", /* name */
867 FALSE, /* partial_inplace */
868 0, /* src_mask */
869 0xffff, /* dst_mask */
870 FALSE), /* pcrel_offset */
871
872 /* The bits 48-63 of an address. */
873 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
874 48, /* rightshift */
875 1, /* size (0 = byte, 1 = short, 2 = long) */
876 16, /* bitsize */
877 FALSE, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_dont, /* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_PPC64_ADDR16_HIGHEST", /* name */
882 FALSE, /* partial_inplace */
883 0, /* src_mask */
884 0xffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
886
887 /* The bits 48-63 of an address, plus 1 if the contents of the low
888 16 bits, treated as a signed number, is negative. */
889 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
890 48, /* rightshift */
891 1, /* size (0 = byte, 1 = short, 2 = long) */
892 16, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_dont, /* complain_on_overflow */
896 ppc64_elf_ha_reloc, /* special_function */
897 "R_PPC64_ADDR16_HIGHESTA", /* name */
898 FALSE, /* partial_inplace */
899 0, /* src_mask */
900 0xffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
902
903 /* Like ADDR64, but may be unaligned. */
904 HOWTO (R_PPC64_UADDR64, /* type */
905 0, /* rightshift */
906 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
907 64, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_dont, /* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_PPC64_UADDR64", /* name */
913 FALSE, /* partial_inplace */
914 0, /* src_mask */
915 ONES (64), /* dst_mask */
916 FALSE), /* pcrel_offset */
917
918 /* 64-bit relative relocation. */
919 HOWTO (R_PPC64_REL64, /* type */
920 0, /* rightshift */
921 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
922 64, /* bitsize */
923 TRUE, /* pc_relative */
924 0, /* bitpos */
925 complain_overflow_dont, /* complain_on_overflow */
926 bfd_elf_generic_reloc, /* special_function */
927 "R_PPC64_REL64", /* name */
928 FALSE, /* partial_inplace */
929 0, /* src_mask */
930 ONES (64), /* dst_mask */
931 TRUE), /* pcrel_offset */
932
933 /* 64-bit relocation to the symbol's procedure linkage table. */
934 HOWTO (R_PPC64_PLT64, /* type */
935 0, /* rightshift */
936 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
937 64, /* bitsize */
938 FALSE, /* pc_relative */
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
941 ppc64_elf_unhandled_reloc, /* special_function */
942 "R_PPC64_PLT64", /* name */
943 FALSE, /* partial_inplace */
944 0, /* src_mask */
945 ONES (64), /* dst_mask */
946 FALSE), /* pcrel_offset */
947
948 /* 64-bit PC relative relocation to the symbol's procedure linkage
949 table. */
950 /* FIXME: R_PPC64_PLTREL64 not supported. */
951 HOWTO (R_PPC64_PLTREL64, /* type */
952 0, /* rightshift */
953 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
954 64, /* bitsize */
955 TRUE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc64_elf_unhandled_reloc, /* special_function */
959 "R_PPC64_PLTREL64", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 ONES (64), /* dst_mask */
963 TRUE), /* pcrel_offset */
964
965 /* 16 bit TOC-relative relocation. */
966
967 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
968 HOWTO (R_PPC64_TOC16, /* type */
969 0, /* rightshift */
970 1, /* size (0 = byte, 1 = short, 2 = long) */
971 16, /* bitsize */
972 FALSE, /* pc_relative */
973 0, /* bitpos */
974 complain_overflow_signed, /* complain_on_overflow */
975 ppc64_elf_toc_reloc, /* special_function */
976 "R_PPC64_TOC16", /* name */
977 FALSE, /* partial_inplace */
978 0, /* src_mask */
979 0xffff, /* dst_mask */
980 FALSE), /* pcrel_offset */
981
982 /* 16 bit TOC-relative relocation without overflow. */
983
984 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
985 HOWTO (R_PPC64_TOC16_LO, /* type */
986 0, /* rightshift */
987 1, /* size (0 = byte, 1 = short, 2 = long) */
988 16, /* bitsize */
989 FALSE, /* pc_relative */
990 0, /* bitpos */
991 complain_overflow_dont, /* complain_on_overflow */
992 ppc64_elf_toc_reloc, /* special_function */
993 "R_PPC64_TOC16_LO", /* name */
994 FALSE, /* partial_inplace */
995 0, /* src_mask */
996 0xffff, /* dst_mask */
997 FALSE), /* pcrel_offset */
998
999 /* 16 bit TOC-relative relocation, high 16 bits. */
1000
1001 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1002 HOWTO (R_PPC64_TOC16_HI, /* type */
1003 16, /* rightshift */
1004 1, /* size (0 = byte, 1 = short, 2 = long) */
1005 16, /* bitsize */
1006 FALSE, /* pc_relative */
1007 0, /* bitpos */
1008 complain_overflow_signed, /* complain_on_overflow */
1009 ppc64_elf_toc_reloc, /* special_function */
1010 "R_PPC64_TOC16_HI", /* name */
1011 FALSE, /* partial_inplace */
1012 0, /* src_mask */
1013 0xffff, /* dst_mask */
1014 FALSE), /* pcrel_offset */
1015
1016 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1017 contents of the low 16 bits, treated as a signed number, is
1018 negative. */
1019
1020 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1021 HOWTO (R_PPC64_TOC16_HA, /* type */
1022 16, /* rightshift */
1023 1, /* size (0 = byte, 1 = short, 2 = long) */
1024 16, /* bitsize */
1025 FALSE, /* pc_relative */
1026 0, /* bitpos */
1027 complain_overflow_signed, /* complain_on_overflow */
1028 ppc64_elf_toc_ha_reloc, /* special_function */
1029 "R_PPC64_TOC16_HA", /* name */
1030 FALSE, /* partial_inplace */
1031 0, /* src_mask */
1032 0xffff, /* dst_mask */
1033 FALSE), /* pcrel_offset */
1034
1035 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1036
1037 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1038 HOWTO (R_PPC64_TOC, /* type */
1039 0, /* rightshift */
1040 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1041 64, /* bitsize */
1042 FALSE, /* pc_relative */
1043 0, /* bitpos */
1044 complain_overflow_dont, /* complain_on_overflow */
1045 ppc64_elf_toc64_reloc, /* special_function */
1046 "R_PPC64_TOC", /* name */
1047 FALSE, /* partial_inplace */
1048 0, /* src_mask */
1049 ONES (64), /* dst_mask */
1050 FALSE), /* pcrel_offset */
1051
1052 /* Like R_PPC64_GOT16, but also informs the link editor that the
1053 value to relocate may (!) refer to a PLT entry which the link
1054 editor (a) may replace with the symbol value. If the link editor
1055 is unable to fully resolve the symbol, it may (b) create a PLT
1056 entry and store the address to the new PLT entry in the GOT.
1057 This permits lazy resolution of function symbols at run time.
1058 The link editor may also skip all of this and just (c) emit a
1059 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1060 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16, /* type */
1062 0, /* rightshift */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1064 16, /* bitsize */
1065 FALSE, /* pc_relative */
1066 0, /* bitpos */
1067 complain_overflow_signed, /* complain_on_overflow */
1068 ppc64_elf_unhandled_reloc, /* special_function */
1069 "R_PPC64_PLTGOT16", /* name */
1070 FALSE, /* partial_inplace */
1071 0, /* src_mask */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1074
1075 /* Like R_PPC64_PLTGOT16, but without overflow. */
1076 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1077 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1078 0, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont, /* complain_on_overflow */
1084 ppc64_elf_unhandled_reloc, /* special_function */
1085 "R_PPC64_PLTGOT16_LO", /* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xffff, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1090
1091 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1092 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1093 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1094 16, /* rightshift */
1095 1, /* size (0 = byte, 1 = short, 2 = long) */
1096 16, /* bitsize */
1097 FALSE, /* pc_relative */
1098 0, /* bitpos */
1099 complain_overflow_signed, /* complain_on_overflow */
1100 ppc64_elf_unhandled_reloc, /* special_function */
1101 "R_PPC64_PLTGOT16_HI", /* name */
1102 FALSE, /* partial_inplace */
1103 0, /* src_mask */
1104 0xffff, /* dst_mask */
1105 FALSE), /* pcrel_offset */
1106
1107 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1108 1 if the contents of the low 16 bits, treated as a signed number,
1109 is negative. */
1110 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1111 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1112 16, /* rightshift */
1113 1, /* size (0 = byte, 1 = short, 2 = long) */
1114 16, /* bitsize */
1115 FALSE, /* pc_relative */
1116 0, /* bitpos */
1117 complain_overflow_signed, /* complain_on_overflow */
1118 ppc64_elf_unhandled_reloc, /* special_function */
1119 "R_PPC64_PLTGOT16_HA", /* name */
1120 FALSE, /* partial_inplace */
1121 0, /* src_mask */
1122 0xffff, /* dst_mask */
1123 FALSE), /* pcrel_offset */
1124
1125 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1126 HOWTO (R_PPC64_ADDR16_DS, /* type */
1127 0, /* rightshift */
1128 1, /* size (0 = byte, 1 = short, 2 = long) */
1129 16, /* bitsize */
1130 FALSE, /* pc_relative */
1131 0, /* bitpos */
1132 complain_overflow_signed, /* complain_on_overflow */
1133 bfd_elf_generic_reloc, /* special_function */
1134 "R_PPC64_ADDR16_DS", /* name */
1135 FALSE, /* partial_inplace */
1136 0, /* src_mask */
1137 0xfffc, /* dst_mask */
1138 FALSE), /* pcrel_offset */
1139
1140 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1141 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1142 0, /* rightshift */
1143 1, /* size (0 = byte, 1 = short, 2 = long) */
1144 16, /* bitsize */
1145 FALSE, /* pc_relative */
1146 0, /* bitpos */
1147 complain_overflow_dont,/* complain_on_overflow */
1148 bfd_elf_generic_reloc, /* special_function */
1149 "R_PPC64_ADDR16_LO_DS",/* name */
1150 FALSE, /* partial_inplace */
1151 0, /* src_mask */
1152 0xfffc, /* dst_mask */
1153 FALSE), /* pcrel_offset */
1154
1155 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1156 HOWTO (R_PPC64_GOT16_DS, /* type */
1157 0, /* rightshift */
1158 1, /* size (0 = byte, 1 = short, 2 = long) */
1159 16, /* bitsize */
1160 FALSE, /* pc_relative */
1161 0, /* bitpos */
1162 complain_overflow_signed, /* complain_on_overflow */
1163 ppc64_elf_unhandled_reloc, /* special_function */
1164 "R_PPC64_GOT16_DS", /* name */
1165 FALSE, /* partial_inplace */
1166 0, /* src_mask */
1167 0xfffc, /* dst_mask */
1168 FALSE), /* pcrel_offset */
1169
1170 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1171 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1172 0, /* rightshift */
1173 1, /* size (0 = byte, 1 = short, 2 = long) */
1174 16, /* bitsize */
1175 FALSE, /* pc_relative */
1176 0, /* bitpos */
1177 complain_overflow_dont, /* complain_on_overflow */
1178 ppc64_elf_unhandled_reloc, /* special_function */
1179 "R_PPC64_GOT16_LO_DS", /* name */
1180 FALSE, /* partial_inplace */
1181 0, /* src_mask */
1182 0xfffc, /* dst_mask */
1183 FALSE), /* pcrel_offset */
1184
1185 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1186 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1187 0, /* rightshift */
1188 1, /* size (0 = byte, 1 = short, 2 = long) */
1189 16, /* bitsize */
1190 FALSE, /* pc_relative */
1191 0, /* bitpos */
1192 complain_overflow_dont, /* complain_on_overflow */
1193 ppc64_elf_unhandled_reloc, /* special_function */
1194 "R_PPC64_PLT16_LO_DS", /* name */
1195 FALSE, /* partial_inplace */
1196 0, /* src_mask */
1197 0xfffc, /* dst_mask */
1198 FALSE), /* pcrel_offset */
1199
1200 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1201 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1202 0, /* rightshift */
1203 1, /* size (0 = byte, 1 = short, 2 = long) */
1204 16, /* bitsize */
1205 FALSE, /* pc_relative */
1206 0, /* bitpos */
1207 complain_overflow_signed, /* complain_on_overflow */
1208 ppc64_elf_sectoff_reloc, /* special_function */
1209 "R_PPC64_SECTOFF_DS", /* name */
1210 FALSE, /* partial_inplace */
1211 0, /* src_mask */
1212 0xfffc, /* dst_mask */
1213 FALSE), /* pcrel_offset */
1214
1215 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1216 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1217 0, /* rightshift */
1218 1, /* size (0 = byte, 1 = short, 2 = long) */
1219 16, /* bitsize */
1220 FALSE, /* pc_relative */
1221 0, /* bitpos */
1222 complain_overflow_dont, /* complain_on_overflow */
1223 ppc64_elf_sectoff_reloc, /* special_function */
1224 "R_PPC64_SECTOFF_LO_DS",/* name */
1225 FALSE, /* partial_inplace */
1226 0, /* src_mask */
1227 0xfffc, /* dst_mask */
1228 FALSE), /* pcrel_offset */
1229
1230 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1231 HOWTO (R_PPC64_TOC16_DS, /* type */
1232 0, /* rightshift */
1233 1, /* size (0 = byte, 1 = short, 2 = long) */
1234 16, /* bitsize */
1235 FALSE, /* pc_relative */
1236 0, /* bitpos */
1237 complain_overflow_signed, /* complain_on_overflow */
1238 ppc64_elf_toc_reloc, /* special_function */
1239 "R_PPC64_TOC16_DS", /* name */
1240 FALSE, /* partial_inplace */
1241 0, /* src_mask */
1242 0xfffc, /* dst_mask */
1243 FALSE), /* pcrel_offset */
1244
1245 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1246 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1247 0, /* rightshift */
1248 1, /* size (0 = byte, 1 = short, 2 = long) */
1249 16, /* bitsize */
1250 FALSE, /* pc_relative */
1251 0, /* bitpos */
1252 complain_overflow_dont, /* complain_on_overflow */
1253 ppc64_elf_toc_reloc, /* special_function */
1254 "R_PPC64_TOC16_LO_DS", /* name */
1255 FALSE, /* partial_inplace */
1256 0, /* src_mask */
1257 0xfffc, /* dst_mask */
1258 FALSE), /* pcrel_offset */
1259
1260 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1261 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1262 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1263 0, /* rightshift */
1264 1, /* size (0 = byte, 1 = short, 2 = long) */
1265 16, /* bitsize */
1266 FALSE, /* pc_relative */
1267 0, /* bitpos */
1268 complain_overflow_signed, /* complain_on_overflow */
1269 ppc64_elf_unhandled_reloc, /* special_function */
1270 "R_PPC64_PLTGOT16_DS", /* name */
1271 FALSE, /* partial_inplace */
1272 0, /* src_mask */
1273 0xfffc, /* dst_mask */
1274 FALSE), /* pcrel_offset */
1275
1276 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1277 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1278 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1279 0, /* rightshift */
1280 1, /* size (0 = byte, 1 = short, 2 = long) */
1281 16, /* bitsize */
1282 FALSE, /* pc_relative */
1283 0, /* bitpos */
1284 complain_overflow_dont, /* complain_on_overflow */
1285 ppc64_elf_unhandled_reloc, /* special_function */
1286 "R_PPC64_PLTGOT16_LO_DS",/* name */
1287 FALSE, /* partial_inplace */
1288 0, /* src_mask */
1289 0xfffc, /* dst_mask */
1290 FALSE), /* pcrel_offset */
1291
1292 /* Marker relocs for TLS. */
1293 HOWTO (R_PPC64_TLS,
1294 0, /* rightshift */
1295 2, /* size (0 = byte, 1 = short, 2 = long) */
1296 32, /* bitsize */
1297 FALSE, /* pc_relative */
1298 0, /* bitpos */
1299 complain_overflow_dont, /* complain_on_overflow */
1300 bfd_elf_generic_reloc, /* special_function */
1301 "R_PPC64_TLS", /* name */
1302 FALSE, /* partial_inplace */
1303 0, /* src_mask */
1304 0, /* dst_mask */
1305 FALSE), /* pcrel_offset */
1306
1307 HOWTO (R_PPC64_TLSGD,
1308 0, /* rightshift */
1309 2, /* size (0 = byte, 1 = short, 2 = long) */
1310 32, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 bfd_elf_generic_reloc, /* special_function */
1315 "R_PPC64_TLSGD", /* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
1321 HOWTO (R_PPC64_TLSLD,
1322 0, /* rightshift */
1323 2, /* size (0 = byte, 1 = short, 2 = long) */
1324 32, /* bitsize */
1325 FALSE, /* pc_relative */
1326 0, /* bitpos */
1327 complain_overflow_dont, /* complain_on_overflow */
1328 bfd_elf_generic_reloc, /* special_function */
1329 "R_PPC64_TLSLD", /* name */
1330 FALSE, /* partial_inplace */
1331 0, /* src_mask */
1332 0, /* dst_mask */
1333 FALSE), /* pcrel_offset */
1334
1335 HOWTO (R_PPC64_TOCSAVE,
1336 0, /* rightshift */
1337 2, /* size (0 = byte, 1 = short, 2 = long) */
1338 32, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 bfd_elf_generic_reloc, /* special_function */
1343 "R_PPC64_TOCSAVE", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1348
1349 /* Computes the load module index of the load module that contains the
1350 definition of its TLS sym. */
1351 HOWTO (R_PPC64_DTPMOD64,
1352 0, /* rightshift */
1353 4, /* size (0 = byte, 1 = short, 2 = long) */
1354 64, /* bitsize */
1355 FALSE, /* pc_relative */
1356 0, /* bitpos */
1357 complain_overflow_dont, /* complain_on_overflow */
1358 ppc64_elf_unhandled_reloc, /* special_function */
1359 "R_PPC64_DTPMOD64", /* name */
1360 FALSE, /* partial_inplace */
1361 0, /* src_mask */
1362 ONES (64), /* dst_mask */
1363 FALSE), /* pcrel_offset */
1364
1365 /* Computes a dtv-relative displacement, the difference between the value
1366 of sym+add and the base address of the thread-local storage block that
1367 contains the definition of sym, minus 0x8000. */
1368 HOWTO (R_PPC64_DTPREL64,
1369 0, /* rightshift */
1370 4, /* size (0 = byte, 1 = short, 2 = long) */
1371 64, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont, /* complain_on_overflow */
1375 ppc64_elf_unhandled_reloc, /* special_function */
1376 "R_PPC64_DTPREL64", /* name */
1377 FALSE, /* partial_inplace */
1378 0, /* src_mask */
1379 ONES (64), /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381
1382 /* A 16 bit dtprel reloc. */
1383 HOWTO (R_PPC64_DTPREL16,
1384 0, /* rightshift */
1385 1, /* size (0 = byte, 1 = short, 2 = long) */
1386 16, /* bitsize */
1387 FALSE, /* pc_relative */
1388 0, /* bitpos */
1389 complain_overflow_signed, /* complain_on_overflow */
1390 ppc64_elf_unhandled_reloc, /* special_function */
1391 "R_PPC64_DTPREL16", /* name */
1392 FALSE, /* partial_inplace */
1393 0, /* src_mask */
1394 0xffff, /* dst_mask */
1395 FALSE), /* pcrel_offset */
1396
1397 /* Like DTPREL16, but no overflow. */
1398 HOWTO (R_PPC64_DTPREL16_LO,
1399 0, /* rightshift */
1400 1, /* size (0 = byte, 1 = short, 2 = long) */
1401 16, /* bitsize */
1402 FALSE, /* pc_relative */
1403 0, /* bitpos */
1404 complain_overflow_dont, /* complain_on_overflow */
1405 ppc64_elf_unhandled_reloc, /* special_function */
1406 "R_PPC64_DTPREL16_LO", /* name */
1407 FALSE, /* partial_inplace */
1408 0, /* src_mask */
1409 0xffff, /* dst_mask */
1410 FALSE), /* pcrel_offset */
1411
1412 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1413 HOWTO (R_PPC64_DTPREL16_HI,
1414 16, /* rightshift */
1415 1, /* size (0 = byte, 1 = short, 2 = long) */
1416 16, /* bitsize */
1417 FALSE, /* pc_relative */
1418 0, /* bitpos */
1419 complain_overflow_signed, /* complain_on_overflow */
1420 ppc64_elf_unhandled_reloc, /* special_function */
1421 "R_PPC64_DTPREL16_HI", /* name */
1422 FALSE, /* partial_inplace */
1423 0, /* src_mask */
1424 0xffff, /* dst_mask */
1425 FALSE), /* pcrel_offset */
1426
1427 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1428 HOWTO (R_PPC64_DTPREL16_HA,
1429 16, /* rightshift */
1430 1, /* size (0 = byte, 1 = short, 2 = long) */
1431 16, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_signed, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL16_HA", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 0xffff, /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441
1442 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1443 HOWTO (R_PPC64_DTPREL16_HIGHER,
1444 32, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_dont, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16_HIGHER", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1456
1457 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1458 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1459 32, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_HIGHERA", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1471
1472 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1474 48, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
1479 complain_overflow_dont, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HIGHEST", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486
1487 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1489 48, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501
1502 /* Like DTPREL16, but for insns with a DS field. */
1503 HOWTO (R_PPC64_DTPREL16_DS,
1504 0, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_signed, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_DS", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xfffc, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1516
1517 /* Like DTPREL16_DS, but no overflow. */
1518 HOWTO (R_PPC64_DTPREL16_LO_DS,
1519 0, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_LO_DS", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xfffc, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531
1532 /* Computes a tp-relative displacement, the difference between the value of
1533 sym+add and the value of the thread pointer (r13). */
1534 HOWTO (R_PPC64_TPREL64,
1535 0, /* rightshift */
1536 4, /* size (0 = byte, 1 = short, 2 = long) */
1537 64, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont, /* complain_on_overflow */
1541 ppc64_elf_unhandled_reloc, /* special_function */
1542 "R_PPC64_TPREL64", /* name */
1543 FALSE, /* partial_inplace */
1544 0, /* src_mask */
1545 ONES (64), /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547
1548 /* A 16 bit tprel reloc. */
1549 HOWTO (R_PPC64_TPREL16,
1550 0, /* rightshift */
1551 1, /* size (0 = byte, 1 = short, 2 = long) */
1552 16, /* bitsize */
1553 FALSE, /* pc_relative */
1554 0, /* bitpos */
1555 complain_overflow_signed, /* complain_on_overflow */
1556 ppc64_elf_unhandled_reloc, /* special_function */
1557 "R_PPC64_TPREL16", /* name */
1558 FALSE, /* partial_inplace */
1559 0, /* src_mask */
1560 0xffff, /* dst_mask */
1561 FALSE), /* pcrel_offset */
1562
1563 /* Like TPREL16, but no overflow. */
1564 HOWTO (R_PPC64_TPREL16_LO,
1565 0, /* rightshift */
1566 1, /* size (0 = byte, 1 = short, 2 = long) */
1567 16, /* bitsize */
1568 FALSE, /* pc_relative */
1569 0, /* bitpos */
1570 complain_overflow_dont, /* complain_on_overflow */
1571 ppc64_elf_unhandled_reloc, /* special_function */
1572 "R_PPC64_TPREL16_LO", /* name */
1573 FALSE, /* partial_inplace */
1574 0, /* src_mask */
1575 0xffff, /* dst_mask */
1576 FALSE), /* pcrel_offset */
1577
1578 /* Like TPREL16_LO, but next higher group of 16 bits. */
1579 HOWTO (R_PPC64_TPREL16_HI,
1580 16, /* rightshift */
1581 1, /* size (0 = byte, 1 = short, 2 = long) */
1582 16, /* bitsize */
1583 FALSE, /* pc_relative */
1584 0, /* bitpos */
1585 complain_overflow_signed, /* complain_on_overflow */
1586 ppc64_elf_unhandled_reloc, /* special_function */
1587 "R_PPC64_TPREL16_HI", /* name */
1588 FALSE, /* partial_inplace */
1589 0, /* src_mask */
1590 0xffff, /* dst_mask */
1591 FALSE), /* pcrel_offset */
1592
1593 /* Like TPREL16_HI, but adjust for low 16 bits. */
1594 HOWTO (R_PPC64_TPREL16_HA,
1595 16, /* rightshift */
1596 1, /* size (0 = byte, 1 = short, 2 = long) */
1597 16, /* bitsize */
1598 FALSE, /* pc_relative */
1599 0, /* bitpos */
1600 complain_overflow_signed, /* complain_on_overflow */
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL16_HA", /* name */
1603 FALSE, /* partial_inplace */
1604 0, /* src_mask */
1605 0xffff, /* dst_mask */
1606 FALSE), /* pcrel_offset */
1607
1608 /* Like TPREL16_HI, but next higher group of 16 bits. */
1609 HOWTO (R_PPC64_TPREL16_HIGHER,
1610 32, /* rightshift */
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1612 16, /* bitsize */
1613 FALSE, /* pc_relative */
1614 0, /* bitpos */
1615 complain_overflow_dont, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16_HIGHER", /* name */
1618 FALSE, /* partial_inplace */
1619 0, /* src_mask */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1622
1623 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1624 HOWTO (R_PPC64_TPREL16_HIGHERA,
1625 32, /* rightshift */
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1627 16, /* bitsize */
1628 FALSE, /* pc_relative */
1629 0, /* bitpos */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_HIGHERA", /* name */
1633 FALSE, /* partial_inplace */
1634 0, /* src_mask */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1637
1638 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HIGHEST,
1640 48, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 16, /* bitsize */
1643 FALSE, /* pc_relative */
1644 0, /* bitpos */
1645 complain_overflow_dont, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HIGHEST", /* name */
1648 FALSE, /* partial_inplace */
1649 0, /* src_mask */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1652
1653 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1655 48, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_dont, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HIGHESTA", /* name */
1663 FALSE, /* partial_inplace */
1664 0, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 /* Like TPREL16, but for insns with a DS field. */
1669 HOWTO (R_PPC64_TPREL16_DS,
1670 0, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1672 16, /* bitsize */
1673 FALSE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_signed, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_DS", /* name */
1678 FALSE, /* partial_inplace */
1679 0, /* src_mask */
1680 0xfffc, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1682
1683 /* Like TPREL16_DS, but no overflow. */
1684 HOWTO (R_PPC64_TPREL16_LO_DS,
1685 0, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 16, /* bitsize */
1688 FALSE, /* pc_relative */
1689 0, /* bitpos */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_LO_DS", /* name */
1693 FALSE, /* partial_inplace */
1694 0, /* src_mask */
1695 0xfffc, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1697
1698 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1699 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1700 to the first entry relative to the TOC base (r2). */
1701 HOWTO (R_PPC64_GOT_TLSGD16,
1702 0, /* rightshift */
1703 1, /* size (0 = byte, 1 = short, 2 = long) */
1704 16, /* bitsize */
1705 FALSE, /* pc_relative */
1706 0, /* bitpos */
1707 complain_overflow_signed, /* complain_on_overflow */
1708 ppc64_elf_unhandled_reloc, /* special_function */
1709 "R_PPC64_GOT_TLSGD16", /* name */
1710 FALSE, /* partial_inplace */
1711 0, /* src_mask */
1712 0xffff, /* dst_mask */
1713 FALSE), /* pcrel_offset */
1714
1715 /* Like GOT_TLSGD16, but no overflow. */
1716 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1717 0, /* rightshift */
1718 1, /* size (0 = byte, 1 = short, 2 = long) */
1719 16, /* bitsize */
1720 FALSE, /* pc_relative */
1721 0, /* bitpos */
1722 complain_overflow_dont, /* complain_on_overflow */
1723 ppc64_elf_unhandled_reloc, /* special_function */
1724 "R_PPC64_GOT_TLSGD16_LO", /* name */
1725 FALSE, /* partial_inplace */
1726 0, /* src_mask */
1727 0xffff, /* dst_mask */
1728 FALSE), /* pcrel_offset */
1729
1730 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1731 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1732 16, /* rightshift */
1733 1, /* size (0 = byte, 1 = short, 2 = long) */
1734 16, /* bitsize */
1735 FALSE, /* pc_relative */
1736 0, /* bitpos */
1737 complain_overflow_signed, /* complain_on_overflow */
1738 ppc64_elf_unhandled_reloc, /* special_function */
1739 "R_PPC64_GOT_TLSGD16_HI", /* name */
1740 FALSE, /* partial_inplace */
1741 0, /* src_mask */
1742 0xffff, /* dst_mask */
1743 FALSE), /* pcrel_offset */
1744
1745 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1746 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1747 16, /* rightshift */
1748 1, /* size (0 = byte, 1 = short, 2 = long) */
1749 16, /* bitsize */
1750 FALSE, /* pc_relative */
1751 0, /* bitpos */
1752 complain_overflow_signed, /* complain_on_overflow */
1753 ppc64_elf_unhandled_reloc, /* special_function */
1754 "R_PPC64_GOT_TLSGD16_HA", /* name */
1755 FALSE, /* partial_inplace */
1756 0, /* src_mask */
1757 0xffff, /* dst_mask */
1758 FALSE), /* pcrel_offset */
1759
1760 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1761 with values (sym+add)@dtpmod and zero, and computes the offset to the
1762 first entry relative to the TOC base (r2). */
1763 HOWTO (R_PPC64_GOT_TLSLD16,
1764 0, /* rightshift */
1765 1, /* size (0 = byte, 1 = short, 2 = long) */
1766 16, /* bitsize */
1767 FALSE, /* pc_relative */
1768 0, /* bitpos */
1769 complain_overflow_signed, /* complain_on_overflow */
1770 ppc64_elf_unhandled_reloc, /* special_function */
1771 "R_PPC64_GOT_TLSLD16", /* name */
1772 FALSE, /* partial_inplace */
1773 0, /* src_mask */
1774 0xffff, /* dst_mask */
1775 FALSE), /* pcrel_offset */
1776
1777 /* Like GOT_TLSLD16, but no overflow. */
1778 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1779 0, /* rightshift */
1780 1, /* size (0 = byte, 1 = short, 2 = long) */
1781 16, /* bitsize */
1782 FALSE, /* pc_relative */
1783 0, /* bitpos */
1784 complain_overflow_dont, /* complain_on_overflow */
1785 ppc64_elf_unhandled_reloc, /* special_function */
1786 "R_PPC64_GOT_TLSLD16_LO", /* name */
1787 FALSE, /* partial_inplace */
1788 0, /* src_mask */
1789 0xffff, /* dst_mask */
1790 FALSE), /* pcrel_offset */
1791
1792 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1793 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1794 16, /* rightshift */
1795 1, /* size (0 = byte, 1 = short, 2 = long) */
1796 16, /* bitsize */
1797 FALSE, /* pc_relative */
1798 0, /* bitpos */
1799 complain_overflow_signed, /* complain_on_overflow */
1800 ppc64_elf_unhandled_reloc, /* special_function */
1801 "R_PPC64_GOT_TLSLD16_HI", /* name */
1802 FALSE, /* partial_inplace */
1803 0, /* src_mask */
1804 0xffff, /* dst_mask */
1805 FALSE), /* pcrel_offset */
1806
1807 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1808 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1809 16, /* rightshift */
1810 1, /* size (0 = byte, 1 = short, 2 = long) */
1811 16, /* bitsize */
1812 FALSE, /* pc_relative */
1813 0, /* bitpos */
1814 complain_overflow_signed, /* complain_on_overflow */
1815 ppc64_elf_unhandled_reloc, /* special_function */
1816 "R_PPC64_GOT_TLSLD16_HA", /* name */
1817 FALSE, /* partial_inplace */
1818 0, /* src_mask */
1819 0xffff, /* dst_mask */
1820 FALSE), /* pcrel_offset */
1821
1822 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1823 the offset to the entry relative to the TOC base (r2). */
1824 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1825 0, /* rightshift */
1826 1, /* size (0 = byte, 1 = short, 2 = long) */
1827 16, /* bitsize */
1828 FALSE, /* pc_relative */
1829 0, /* bitpos */
1830 complain_overflow_signed, /* complain_on_overflow */
1831 ppc64_elf_unhandled_reloc, /* special_function */
1832 "R_PPC64_GOT_DTPREL16_DS", /* name */
1833 FALSE, /* partial_inplace */
1834 0, /* src_mask */
1835 0xfffc, /* dst_mask */
1836 FALSE), /* pcrel_offset */
1837
1838 /* Like GOT_DTPREL16_DS, but no overflow. */
1839 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1840 0, /* rightshift */
1841 1, /* size (0 = byte, 1 = short, 2 = long) */
1842 16, /* bitsize */
1843 FALSE, /* pc_relative */
1844 0, /* bitpos */
1845 complain_overflow_dont, /* complain_on_overflow */
1846 ppc64_elf_unhandled_reloc, /* special_function */
1847 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1848 FALSE, /* partial_inplace */
1849 0, /* src_mask */
1850 0xfffc, /* dst_mask */
1851 FALSE), /* pcrel_offset */
1852
1853 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1854 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1855 16, /* rightshift */
1856 1, /* size (0 = byte, 1 = short, 2 = long) */
1857 16, /* bitsize */
1858 FALSE, /* pc_relative */
1859 0, /* bitpos */
1860 complain_overflow_signed, /* complain_on_overflow */
1861 ppc64_elf_unhandled_reloc, /* special_function */
1862 "R_PPC64_GOT_DTPREL16_HI", /* name */
1863 FALSE, /* partial_inplace */
1864 0, /* src_mask */
1865 0xffff, /* dst_mask */
1866 FALSE), /* pcrel_offset */
1867
1868 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1869 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1870 16, /* rightshift */
1871 1, /* size (0 = byte, 1 = short, 2 = long) */
1872 16, /* bitsize */
1873 FALSE, /* pc_relative */
1874 0, /* bitpos */
1875 complain_overflow_signed, /* complain_on_overflow */
1876 ppc64_elf_unhandled_reloc, /* special_function */
1877 "R_PPC64_GOT_DTPREL16_HA", /* name */
1878 FALSE, /* partial_inplace */
1879 0, /* src_mask */
1880 0xffff, /* dst_mask */
1881 FALSE), /* pcrel_offset */
1882
1883 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1884 offset to the entry relative to the TOC base (r2). */
1885 HOWTO (R_PPC64_GOT_TPREL16_DS,
1886 0, /* rightshift */
1887 1, /* size (0 = byte, 1 = short, 2 = long) */
1888 16, /* bitsize */
1889 FALSE, /* pc_relative */
1890 0, /* bitpos */
1891 complain_overflow_signed, /* complain_on_overflow */
1892 ppc64_elf_unhandled_reloc, /* special_function */
1893 "R_PPC64_GOT_TPREL16_DS", /* name */
1894 FALSE, /* partial_inplace */
1895 0, /* src_mask */
1896 0xfffc, /* dst_mask */
1897 FALSE), /* pcrel_offset */
1898
1899 /* Like GOT_TPREL16_DS, but no overflow. */
1900 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1901 0, /* rightshift */
1902 1, /* size (0 = byte, 1 = short, 2 = long) */
1903 16, /* bitsize */
1904 FALSE, /* pc_relative */
1905 0, /* bitpos */
1906 complain_overflow_dont, /* complain_on_overflow */
1907 ppc64_elf_unhandled_reloc, /* special_function */
1908 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1909 FALSE, /* partial_inplace */
1910 0, /* src_mask */
1911 0xfffc, /* dst_mask */
1912 FALSE), /* pcrel_offset */
1913
1914 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1915 HOWTO (R_PPC64_GOT_TPREL16_HI,
1916 16, /* rightshift */
1917 1, /* size (0 = byte, 1 = short, 2 = long) */
1918 16, /* bitsize */
1919 FALSE, /* pc_relative */
1920 0, /* bitpos */
1921 complain_overflow_signed, /* complain_on_overflow */
1922 ppc64_elf_unhandled_reloc, /* special_function */
1923 "R_PPC64_GOT_TPREL16_HI", /* name */
1924 FALSE, /* partial_inplace */
1925 0, /* src_mask */
1926 0xffff, /* dst_mask */
1927 FALSE), /* pcrel_offset */
1928
1929 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1930 HOWTO (R_PPC64_GOT_TPREL16_HA,
1931 16, /* rightshift */
1932 1, /* size (0 = byte, 1 = short, 2 = long) */
1933 16, /* bitsize */
1934 FALSE, /* pc_relative */
1935 0, /* bitpos */
1936 complain_overflow_signed, /* complain_on_overflow */
1937 ppc64_elf_unhandled_reloc, /* special_function */
1938 "R_PPC64_GOT_TPREL16_HA", /* name */
1939 FALSE, /* partial_inplace */
1940 0, /* src_mask */
1941 0xffff, /* dst_mask */
1942 FALSE), /* pcrel_offset */
1943
1944 HOWTO (R_PPC64_JMP_IREL, /* type */
1945 0, /* rightshift */
1946 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1947 0, /* bitsize */
1948 FALSE, /* pc_relative */
1949 0, /* bitpos */
1950 complain_overflow_dont, /* complain_on_overflow */
1951 ppc64_elf_unhandled_reloc, /* special_function */
1952 "R_PPC64_JMP_IREL", /* name */
1953 FALSE, /* partial_inplace */
1954 0, /* src_mask */
1955 0, /* dst_mask */
1956 FALSE), /* pcrel_offset */
1957
1958 HOWTO (R_PPC64_IRELATIVE, /* type */
1959 0, /* rightshift */
1960 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1961 64, /* bitsize */
1962 FALSE, /* pc_relative */
1963 0, /* bitpos */
1964 complain_overflow_dont, /* complain_on_overflow */
1965 bfd_elf_generic_reloc, /* special_function */
1966 "R_PPC64_IRELATIVE", /* name */
1967 FALSE, /* partial_inplace */
1968 0, /* src_mask */
1969 ONES (64), /* dst_mask */
1970 FALSE), /* pcrel_offset */
1971
1972 /* A 16 bit relative relocation. */
1973 HOWTO (R_PPC64_REL16, /* type */
1974 0, /* rightshift */
1975 1, /* size (0 = byte, 1 = short, 2 = long) */
1976 16, /* bitsize */
1977 TRUE, /* pc_relative */
1978 0, /* bitpos */
1979 complain_overflow_signed, /* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_PPC64_REL16", /* name */
1982 FALSE, /* partial_inplace */
1983 0, /* src_mask */
1984 0xffff, /* dst_mask */
1985 TRUE), /* pcrel_offset */
1986
1987 /* A 16 bit relative relocation without overflow. */
1988 HOWTO (R_PPC64_REL16_LO, /* type */
1989 0, /* rightshift */
1990 1, /* size (0 = byte, 1 = short, 2 = long) */
1991 16, /* bitsize */
1992 TRUE, /* pc_relative */
1993 0, /* bitpos */
1994 complain_overflow_dont,/* complain_on_overflow */
1995 bfd_elf_generic_reloc, /* special_function */
1996 "R_PPC64_REL16_LO", /* name */
1997 FALSE, /* partial_inplace */
1998 0, /* src_mask */
1999 0xffff, /* dst_mask */
2000 TRUE), /* pcrel_offset */
2001
2002 /* The high order 16 bits of a relative address. */
2003 HOWTO (R_PPC64_REL16_HI, /* type */
2004 16, /* rightshift */
2005 1, /* size (0 = byte, 1 = short, 2 = long) */
2006 16, /* bitsize */
2007 TRUE, /* pc_relative */
2008 0, /* bitpos */
2009 complain_overflow_signed, /* complain_on_overflow */
2010 bfd_elf_generic_reloc, /* special_function */
2011 "R_PPC64_REL16_HI", /* name */
2012 FALSE, /* partial_inplace */
2013 0, /* src_mask */
2014 0xffff, /* dst_mask */
2015 TRUE), /* pcrel_offset */
2016
2017 /* The high order 16 bits of a relative address, plus 1 if the contents of
2018 the low 16 bits, treated as a signed number, is negative. */
2019 HOWTO (R_PPC64_REL16_HA, /* type */
2020 16, /* rightshift */
2021 1, /* size (0 = byte, 1 = short, 2 = long) */
2022 16, /* bitsize */
2023 TRUE, /* pc_relative */
2024 0, /* bitpos */
2025 complain_overflow_signed, /* complain_on_overflow */
2026 ppc64_elf_ha_reloc, /* special_function */
2027 "R_PPC64_REL16_HA", /* name */
2028 FALSE, /* partial_inplace */
2029 0, /* src_mask */
2030 0xffff, /* dst_mask */
2031 TRUE), /* pcrel_offset */
2032
2033 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2034 HOWTO (R_PPC64_REL16DX_HA, /* type */
2035 16, /* rightshift */
2036 2, /* size (0 = byte, 1 = short, 2 = long) */
2037 16, /* bitsize */
2038 TRUE, /* pc_relative */
2039 0, /* bitpos */
2040 complain_overflow_signed, /* complain_on_overflow */
2041 ppc64_elf_ha_reloc, /* special_function */
2042 "R_PPC64_REL16DX_HA", /* name */
2043 FALSE, /* partial_inplace */
2044 0, /* src_mask */
2045 0x1fffc1, /* dst_mask */
2046 TRUE), /* pcrel_offset */
2047
2048 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2049 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2050 16, /* rightshift */
2051 1, /* size (0 = byte, 1 = short, 2 = long) */
2052 16, /* bitsize */
2053 FALSE, /* pc_relative */
2054 0, /* bitpos */
2055 complain_overflow_dont, /* complain_on_overflow */
2056 bfd_elf_generic_reloc, /* special_function */
2057 "R_PPC64_ADDR16_HIGH", /* name */
2058 FALSE, /* partial_inplace */
2059 0, /* src_mask */
2060 0xffff, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2062
2063 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2064 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2065 16, /* rightshift */
2066 1, /* size (0 = byte, 1 = short, 2 = long) */
2067 16, /* bitsize */
2068 FALSE, /* pc_relative */
2069 0, /* bitpos */
2070 complain_overflow_dont, /* complain_on_overflow */
2071 ppc64_elf_ha_reloc, /* special_function */
2072 "R_PPC64_ADDR16_HIGHA", /* name */
2073 FALSE, /* partial_inplace */
2074 0, /* src_mask */
2075 0xffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2077
2078 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2079 HOWTO (R_PPC64_DTPREL16_HIGH,
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2082 16, /* bitsize */
2083 FALSE, /* pc_relative */
2084 0, /* bitpos */
2085 complain_overflow_dont, /* complain_on_overflow */
2086 ppc64_elf_unhandled_reloc, /* special_function */
2087 "R_PPC64_DTPREL16_HIGH", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
2092
2093 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2094 HOWTO (R_PPC64_DTPREL16_HIGHA,
2095 16, /* rightshift */
2096 1, /* size (0 = byte, 1 = short, 2 = long) */
2097 16, /* bitsize */
2098 FALSE, /* pc_relative */
2099 0, /* bitpos */
2100 complain_overflow_dont, /* complain_on_overflow */
2101 ppc64_elf_unhandled_reloc, /* special_function */
2102 "R_PPC64_DTPREL16_HIGHA", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0xffff, /* dst_mask */
2106 FALSE), /* pcrel_offset */
2107
2108 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2109 HOWTO (R_PPC64_TPREL16_HIGH,
2110 16, /* rightshift */
2111 1, /* size (0 = byte, 1 = short, 2 = long) */
2112 16, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont, /* complain_on_overflow */
2116 ppc64_elf_unhandled_reloc, /* special_function */
2117 "R_PPC64_TPREL16_HIGH", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0xffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
2123 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2124 HOWTO (R_PPC64_TPREL16_HIGHA,
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2127 16, /* bitsize */
2128 FALSE, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 ppc64_elf_unhandled_reloc, /* special_function */
2132 "R_PPC64_TPREL16_HIGHA", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
2138 /* Marker reloc on ELFv2 large-model function entry. */
2139 HOWTO (R_PPC64_ENTRY,
2140 0, /* rightshift */
2141 2, /* size (0 = byte, 1 = short, 2 = long) */
2142 32, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 bfd_elf_generic_reloc, /* special_function */
2147 "R_PPC64_ENTRY", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
2153 /* Like ADDR64, but use local entry point of function. */
2154 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2155 0, /* rightshift */
2156 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2157 64, /* bitsize */
2158 FALSE, /* pc_relative */
2159 0, /* bitpos */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 bfd_elf_generic_reloc, /* special_function */
2162 "R_PPC64_ADDR64_LOCAL", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 ONES (64), /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
2168 /* GNU extension to record C++ vtable hierarchy. */
2169 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2170 0, /* rightshift */
2171 0, /* size (0 = byte, 1 = short, 2 = long) */
2172 0, /* bitsize */
2173 FALSE, /* pc_relative */
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 NULL, /* special_function */
2177 "R_PPC64_GNU_VTINHERIT", /* name */
2178 FALSE, /* partial_inplace */
2179 0, /* src_mask */
2180 0, /* dst_mask */
2181 FALSE), /* pcrel_offset */
2182
2183 /* GNU extension to record C++ vtable member usage. */
2184 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2185 0, /* rightshift */
2186 0, /* size (0 = byte, 1 = short, 2 = long) */
2187 0, /* bitsize */
2188 FALSE, /* pc_relative */
2189 0, /* bitpos */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 NULL, /* special_function */
2192 "R_PPC64_GNU_VTENTRY", /* name */
2193 FALSE, /* partial_inplace */
2194 0, /* src_mask */
2195 0, /* dst_mask */
2196 FALSE), /* pcrel_offset */
2197 };
2198
2199 \f
2200 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2201 be done. */
2202
2203 static void
2204 ppc_howto_init (void)
2205 {
2206 unsigned int i, type;
2207
2208 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2209 {
2210 type = ppc64_elf_howto_raw[i].type;
2211 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2212 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2213 }
2214 }
2215
2216 static reloc_howto_type *
2217 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2218 bfd_reloc_code_real_type code)
2219 {
2220 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2221
2222 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2223 /* Initialize howto table if needed. */
2224 ppc_howto_init ();
2225
2226 switch (code)
2227 {
2228 default:
2229 return NULL;
2230
2231 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2232 break;
2233 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2234 break;
2235 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2236 break;
2237 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2238 break;
2239 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2240 break;
2241 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2242 break;
2243 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2244 break;
2245 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2246 break;
2247 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2248 break;
2249 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2250 break;
2251 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2252 break;
2253 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2254 break;
2255 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2256 break;
2257 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2258 break;
2259 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2260 break;
2261 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2262 break;
2263 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2264 break;
2265 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2266 break;
2267 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2268 break;
2269 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2270 break;
2271 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2272 break;
2273 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2274 break;
2275 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2276 break;
2277 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2278 break;
2279 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2280 break;
2281 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2282 break;
2283 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2284 break;
2285 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2286 break;
2287 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2288 break;
2289 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2290 break;
2291 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2292 break;
2293 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2294 break;
2295 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2296 break;
2297 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2298 break;
2299 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2300 break;
2301 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2302 break;
2303 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2304 break;
2305 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2306 break;
2307 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2308 break;
2309 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2310 break;
2311 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2312 break;
2313 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2314 break;
2315 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2316 break;
2317 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2318 break;
2319 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2320 break;
2321 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2322 break;
2323 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2324 break;
2325 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2326 break;
2327 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2328 break;
2329 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2330 break;
2331 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2332 break;
2333 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2334 break;
2335 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2336 break;
2337 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2338 break;
2339 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2340 break;
2341 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2342 break;
2343 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2344 break;
2345 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2346 break;
2347 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2348 break;
2349 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2350 break;
2351 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2352 break;
2353 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2354 break;
2355 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2356 break;
2357 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2358 break;
2359 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2360 break;
2361 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2362 break;
2363 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2364 break;
2365 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2366 break;
2367 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2368 break;
2369 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2370 break;
2371 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2372 break;
2373 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2374 break;
2375 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2376 break;
2377 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2378 break;
2379 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2380 break;
2381 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2382 break;
2383 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2384 break;
2385 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2386 break;
2387 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2388 break;
2389 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2390 break;
2391 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2392 break;
2393 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2394 break;
2395 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2396 break;
2397 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2398 break;
2399 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2400 break;
2401 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2402 break;
2403 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2404 break;
2405 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2406 break;
2407 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2408 break;
2409 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2410 break;
2411 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2412 break;
2413 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2414 break;
2415 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2416 break;
2417 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2418 break;
2419 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2420 break;
2421 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2422 break;
2423 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2424 break;
2425 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2426 break;
2427 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2428 break;
2429 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2430 break;
2431 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2432 break;
2433 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2434 break;
2435 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2436 break;
2437 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2438 break;
2439 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2440 break;
2441 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2442 break;
2443 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2444 break;
2445 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2446 break;
2447 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2448 break;
2449 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2450 break;
2451 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2452 break;
2453 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2454 break;
2455 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2456 break;
2457 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2458 break;
2459 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2460 break;
2461 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2462 break;
2463 }
2464
2465 return ppc64_elf_howto_table[r];
2466 };
2467
2468 static reloc_howto_type *
2469 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2470 const char *r_name)
2471 {
2472 unsigned int i;
2473
2474 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2475 if (ppc64_elf_howto_raw[i].name != NULL
2476 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2477 return &ppc64_elf_howto_raw[i];
2478
2479 return NULL;
2480 }
2481
2482 /* Set the howto pointer for a PowerPC ELF reloc. */
2483
2484 static void
2485 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2486 Elf_Internal_Rela *dst)
2487 {
2488 unsigned int type;
2489
2490 /* Initialize howto table if needed. */
2491 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2492 ppc_howto_init ();
2493
2494 type = ELF64_R_TYPE (dst->r_info);
2495 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2496 {
2497 /* xgettext:c-format */
2498 _bfd_error_handler (_("%B: invalid relocation type %d"),
2499 abfd, (int) type);
2500 type = R_PPC64_NONE;
2501 }
2502 cache_ptr->howto = ppc64_elf_howto_table[type];
2503 }
2504
2505 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2506
2507 static bfd_reloc_status_type
2508 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2509 void *data, asection *input_section,
2510 bfd *output_bfd, char **error_message)
2511 {
2512 enum elf_ppc64_reloc_type r_type;
2513 long insn;
2514 bfd_size_type octets;
2515 bfd_vma value;
2516
2517 /* If this is a relocatable link (output_bfd test tells us), just
2518 call the generic function. Any adjustment will be done at final
2519 link time. */
2520 if (output_bfd != NULL)
2521 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2522 input_section, output_bfd, error_message);
2523
2524 /* Adjust the addend for sign extension of the low 16 bits.
2525 We won't actually be using the low 16 bits, so trashing them
2526 doesn't matter. */
2527 reloc_entry->addend += 0x8000;
2528 r_type = reloc_entry->howto->type;
2529 if (r_type != R_PPC64_REL16DX_HA)
2530 return bfd_reloc_continue;
2531
2532 value = 0;
2533 if (!bfd_is_com_section (symbol->section))
2534 value = symbol->value;
2535 value += (reloc_entry->addend
2536 + symbol->section->output_offset
2537 + symbol->section->output_section->vma);
2538 value -= (reloc_entry->address
2539 + input_section->output_offset
2540 + input_section->output_section->vma);
2541 value = (bfd_signed_vma) value >> 16;
2542
2543 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2544 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2545 insn &= ~0x1fffc1;
2546 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2547 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2548 if (value + 0x8000 > 0xffff)
2549 return bfd_reloc_overflow;
2550 return bfd_reloc_ok;
2551 }
2552
2553 static bfd_reloc_status_type
2554 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2555 void *data, asection *input_section,
2556 bfd *output_bfd, char **error_message)
2557 {
2558 if (output_bfd != NULL)
2559 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2560 input_section, output_bfd, error_message);
2561
2562 if (strcmp (symbol->section->name, ".opd") == 0
2563 && (symbol->section->owner->flags & DYNAMIC) == 0)
2564 {
2565 bfd_vma dest = opd_entry_value (symbol->section,
2566 symbol->value + reloc_entry->addend,
2567 NULL, NULL, FALSE);
2568 if (dest != (bfd_vma) -1)
2569 reloc_entry->addend = dest - (symbol->value
2570 + symbol->section->output_section->vma
2571 + symbol->section->output_offset);
2572 }
2573 else
2574 {
2575 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2576
2577 if (symbol->section->owner != abfd
2578 && symbol->section->owner != NULL
2579 && abiversion (symbol->section->owner) >= 2)
2580 {
2581 unsigned int i;
2582
2583 for (i = 0; i < symbol->section->owner->symcount; ++i)
2584 {
2585 asymbol *symdef = symbol->section->owner->outsymbols[i];
2586
2587 if (strcmp (symdef->name, symbol->name) == 0)
2588 {
2589 elfsym = (elf_symbol_type *) symdef;
2590 break;
2591 }
2592 }
2593 }
2594 reloc_entry->addend
2595 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2596 }
2597 return bfd_reloc_continue;
2598 }
2599
2600 static bfd_reloc_status_type
2601 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2602 void *data, asection *input_section,
2603 bfd *output_bfd, char **error_message)
2604 {
2605 long insn;
2606 enum elf_ppc64_reloc_type r_type;
2607 bfd_size_type octets;
2608 /* Assume 'at' branch hints. */
2609 bfd_boolean is_isa_v2 = TRUE;
2610
2611 /* If this is a relocatable link (output_bfd test tells us), just
2612 call the generic function. Any adjustment will be done at final
2613 link time. */
2614 if (output_bfd != NULL)
2615 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2616 input_section, output_bfd, error_message);
2617
2618 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2619 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2620 insn &= ~(0x01 << 21);
2621 r_type = reloc_entry->howto->type;
2622 if (r_type == R_PPC64_ADDR14_BRTAKEN
2623 || r_type == R_PPC64_REL14_BRTAKEN)
2624 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2625
2626 if (is_isa_v2)
2627 {
2628 /* Set 'a' bit. This is 0b00010 in BO field for branch
2629 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2630 for branch on CTR insns (BO == 1a00t or 1a01t). */
2631 if ((insn & (0x14 << 21)) == (0x04 << 21))
2632 insn |= 0x02 << 21;
2633 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2634 insn |= 0x08 << 21;
2635 else
2636 goto out;
2637 }
2638 else
2639 {
2640 bfd_vma target = 0;
2641 bfd_vma from;
2642
2643 if (!bfd_is_com_section (symbol->section))
2644 target = symbol->value;
2645 target += symbol->section->output_section->vma;
2646 target += symbol->section->output_offset;
2647 target += reloc_entry->addend;
2648
2649 from = (reloc_entry->address
2650 + input_section->output_offset
2651 + input_section->output_section->vma);
2652
2653 /* Invert 'y' bit if not the default. */
2654 if ((bfd_signed_vma) (target - from) < 0)
2655 insn ^= 0x01 << 21;
2656 }
2657 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2658 out:
2659 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2660 input_section, output_bfd, error_message);
2661 }
2662
2663 static bfd_reloc_status_type
2664 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2665 void *data, asection *input_section,
2666 bfd *output_bfd, char **error_message)
2667 {
2668 /* If this is a relocatable link (output_bfd test tells us), just
2669 call the generic function. Any adjustment will be done at final
2670 link time. */
2671 if (output_bfd != NULL)
2672 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2673 input_section, output_bfd, error_message);
2674
2675 /* Subtract the symbol section base address. */
2676 reloc_entry->addend -= symbol->section->output_section->vma;
2677 return bfd_reloc_continue;
2678 }
2679
2680 static bfd_reloc_status_type
2681 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2682 void *data, asection *input_section,
2683 bfd *output_bfd, char **error_message)
2684 {
2685 /* If this is a relocatable link (output_bfd test tells us), just
2686 call the generic function. Any adjustment will be done at final
2687 link time. */
2688 if (output_bfd != NULL)
2689 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2690 input_section, output_bfd, error_message);
2691
2692 /* Subtract the symbol section base address. */
2693 reloc_entry->addend -= symbol->section->output_section->vma;
2694
2695 /* Adjust the addend for sign extension of the low 16 bits. */
2696 reloc_entry->addend += 0x8000;
2697 return bfd_reloc_continue;
2698 }
2699
2700 static bfd_reloc_status_type
2701 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2702 void *data, asection *input_section,
2703 bfd *output_bfd, char **error_message)
2704 {
2705 bfd_vma TOCstart;
2706
2707 /* If this is a relocatable link (output_bfd test tells us), just
2708 call the generic function. Any adjustment will be done at final
2709 link time. */
2710 if (output_bfd != NULL)
2711 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2712 input_section, output_bfd, error_message);
2713
2714 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2715 if (TOCstart == 0)
2716 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2717
2718 /* Subtract the TOC base address. */
2719 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2720 return bfd_reloc_continue;
2721 }
2722
2723 static bfd_reloc_status_type
2724 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2725 void *data, asection *input_section,
2726 bfd *output_bfd, char **error_message)
2727 {
2728 bfd_vma TOCstart;
2729
2730 /* If this is a relocatable link (output_bfd test tells us), just
2731 call the generic function. Any adjustment will be done at final
2732 link time. */
2733 if (output_bfd != NULL)
2734 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2735 input_section, output_bfd, error_message);
2736
2737 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2738 if (TOCstart == 0)
2739 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2740
2741 /* Subtract the TOC base address. */
2742 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2743
2744 /* Adjust the addend for sign extension of the low 16 bits. */
2745 reloc_entry->addend += 0x8000;
2746 return bfd_reloc_continue;
2747 }
2748
2749 static bfd_reloc_status_type
2750 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2751 void *data, asection *input_section,
2752 bfd *output_bfd, char **error_message)
2753 {
2754 bfd_vma TOCstart;
2755 bfd_size_type octets;
2756
2757 /* If this is a relocatable link (output_bfd test tells us), just
2758 call the generic function. Any adjustment will be done at final
2759 link time. */
2760 if (output_bfd != NULL)
2761 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2762 input_section, output_bfd, error_message);
2763
2764 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2765 if (TOCstart == 0)
2766 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2767
2768 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2769 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2770 return bfd_reloc_ok;
2771 }
2772
2773 static bfd_reloc_status_type
2774 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2775 void *data, asection *input_section,
2776 bfd *output_bfd, char **error_message)
2777 {
2778 /* If this is a relocatable link (output_bfd test tells us), just
2779 call the generic function. Any adjustment will be done at final
2780 link time. */
2781 if (output_bfd != NULL)
2782 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2783 input_section, output_bfd, error_message);
2784
2785 if (error_message != NULL)
2786 {
2787 static char buf[60];
2788 sprintf (buf, "generic linker can't handle %s",
2789 reloc_entry->howto->name);
2790 *error_message = buf;
2791 }
2792 return bfd_reloc_dangerous;
2793 }
2794
2795 /* Track GOT entries needed for a given symbol. We might need more
2796 than one got entry per symbol. */
2797 struct got_entry
2798 {
2799 struct got_entry *next;
2800
2801 /* The symbol addend that we'll be placing in the GOT. */
2802 bfd_vma addend;
2803
2804 /* Unlike other ELF targets, we use separate GOT entries for the same
2805 symbol referenced from different input files. This is to support
2806 automatic multiple TOC/GOT sections, where the TOC base can vary
2807 from one input file to another. After partitioning into TOC groups
2808 we merge entries within the group.
2809
2810 Point to the BFD owning this GOT entry. */
2811 bfd *owner;
2812
2813 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2814 TLS_TPREL or TLS_DTPREL for tls entries. */
2815 unsigned char tls_type;
2816
2817 /* Non-zero if got.ent points to real entry. */
2818 unsigned char is_indirect;
2819
2820 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2821 union
2822 {
2823 bfd_signed_vma refcount;
2824 bfd_vma offset;
2825 struct got_entry *ent;
2826 } got;
2827 };
2828
2829 /* The same for PLT. */
2830 struct plt_entry
2831 {
2832 struct plt_entry *next;
2833
2834 bfd_vma addend;
2835
2836 union
2837 {
2838 bfd_signed_vma refcount;
2839 bfd_vma offset;
2840 } plt;
2841 };
2842
2843 struct ppc64_elf_obj_tdata
2844 {
2845 struct elf_obj_tdata elf;
2846
2847 /* Shortcuts to dynamic linker sections. */
2848 asection *got;
2849 asection *relgot;
2850
2851 /* Used during garbage collection. We attach global symbols defined
2852 on removed .opd entries to this section so that the sym is removed. */
2853 asection *deleted_section;
2854
2855 /* TLS local dynamic got entry handling. Support for multiple GOT
2856 sections means we potentially need one of these for each input bfd. */
2857 struct got_entry tlsld_got;
2858
2859 union {
2860 /* A copy of relocs before they are modified for --emit-relocs. */
2861 Elf_Internal_Rela *relocs;
2862
2863 /* Section contents. */
2864 bfd_byte *contents;
2865 } opd;
2866
2867 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2868 the reloc to be in the range -32768 to 32767. */
2869 unsigned int has_small_toc_reloc : 1;
2870
2871 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2872 instruction not one we handle. */
2873 unsigned int unexpected_toc_insn : 1;
2874 };
2875
2876 #define ppc64_elf_tdata(bfd) \
2877 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2878
2879 #define ppc64_tlsld_got(bfd) \
2880 (&ppc64_elf_tdata (bfd)->tlsld_got)
2881
2882 #define is_ppc64_elf(bfd) \
2883 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2884 && elf_object_id (bfd) == PPC64_ELF_DATA)
2885
2886 /* Override the generic function because we store some extras. */
2887
2888 static bfd_boolean
2889 ppc64_elf_mkobject (bfd *abfd)
2890 {
2891 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2892 PPC64_ELF_DATA);
2893 }
2894
2895 /* Fix bad default arch selected for a 64 bit input bfd when the
2896 default is 32 bit. Also select arch based on apuinfo. */
2897
2898 static bfd_boolean
2899 ppc64_elf_object_p (bfd *abfd)
2900 {
2901 if (!abfd->arch_info->the_default)
2902 return TRUE;
2903
2904 if (abfd->arch_info->bits_per_word == 32)
2905 {
2906 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2907
2908 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2909 {
2910 /* Relies on arch after 32 bit default being 64 bit default. */
2911 abfd->arch_info = abfd->arch_info->next;
2912 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2913 }
2914 }
2915 return _bfd_elf_ppc_set_arch (abfd);
2916 }
2917
2918 /* Support for core dump NOTE sections. */
2919
2920 static bfd_boolean
2921 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2922 {
2923 size_t offset, size;
2924
2925 if (note->descsz != 504)
2926 return FALSE;
2927
2928 /* pr_cursig */
2929 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2930
2931 /* pr_pid */
2932 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2933
2934 /* pr_reg */
2935 offset = 112;
2936 size = 384;
2937
2938 /* Make a ".reg/999" section. */
2939 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2940 size, note->descpos + offset);
2941 }
2942
2943 static bfd_boolean
2944 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2945 {
2946 if (note->descsz != 136)
2947 return FALSE;
2948
2949 elf_tdata (abfd)->core->pid
2950 = bfd_get_32 (abfd, note->descdata + 24);
2951 elf_tdata (abfd)->core->program
2952 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2953 elf_tdata (abfd)->core->command
2954 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2955
2956 return TRUE;
2957 }
2958
2959 static char *
2960 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2961 ...)
2962 {
2963 switch (note_type)
2964 {
2965 default:
2966 return NULL;
2967
2968 case NT_PRPSINFO:
2969 {
2970 char data[136];
2971 va_list ap;
2972
2973 va_start (ap, note_type);
2974 memset (data, 0, sizeof (data));
2975 strncpy (data + 40, va_arg (ap, const char *), 16);
2976 strncpy (data + 56, va_arg (ap, const char *), 80);
2977 va_end (ap);
2978 return elfcore_write_note (abfd, buf, bufsiz,
2979 "CORE", note_type, data, sizeof (data));
2980 }
2981
2982 case NT_PRSTATUS:
2983 {
2984 char data[504];
2985 va_list ap;
2986 long pid;
2987 int cursig;
2988 const void *greg;
2989
2990 va_start (ap, note_type);
2991 memset (data, 0, 112);
2992 pid = va_arg (ap, long);
2993 bfd_put_32 (abfd, pid, data + 32);
2994 cursig = va_arg (ap, int);
2995 bfd_put_16 (abfd, cursig, data + 12);
2996 greg = va_arg (ap, const void *);
2997 memcpy (data + 112, greg, 384);
2998 memset (data + 496, 0, 8);
2999 va_end (ap);
3000 return elfcore_write_note (abfd, buf, bufsiz,
3001 "CORE", note_type, data, sizeof (data));
3002 }
3003 }
3004 }
3005
3006 /* Add extra PPC sections. */
3007
3008 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3009 {
3010 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3011 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3012 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3013 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3014 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3015 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3016 { NULL, 0, 0, 0, 0 }
3017 };
3018
3019 enum _ppc64_sec_type {
3020 sec_normal = 0,
3021 sec_opd = 1,
3022 sec_toc = 2
3023 };
3024
3025 struct _ppc64_elf_section_data
3026 {
3027 struct bfd_elf_section_data elf;
3028
3029 union
3030 {
3031 /* An array with one entry for each opd function descriptor,
3032 and some spares since opd entries may be either 16 or 24 bytes. */
3033 #define OPD_NDX(OFF) ((OFF) >> 4)
3034 struct _opd_sec_data
3035 {
3036 /* Points to the function code section for local opd entries. */
3037 asection **func_sec;
3038
3039 /* After editing .opd, adjust references to opd local syms. */
3040 long *adjust;
3041 } opd;
3042
3043 /* An array for toc sections, indexed by offset/8. */
3044 struct _toc_sec_data
3045 {
3046 /* Specifies the relocation symbol index used at a given toc offset. */
3047 unsigned *symndx;
3048
3049 /* And the relocation addend. */
3050 bfd_vma *add;
3051 } toc;
3052 } u;
3053
3054 enum _ppc64_sec_type sec_type:2;
3055
3056 /* Flag set when small branches are detected. Used to
3057 select suitable defaults for the stub group size. */
3058 unsigned int has_14bit_branch:1;
3059 };
3060
3061 #define ppc64_elf_section_data(sec) \
3062 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3063
3064 static bfd_boolean
3065 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3066 {
3067 if (!sec->used_by_bfd)
3068 {
3069 struct _ppc64_elf_section_data *sdata;
3070 bfd_size_type amt = sizeof (*sdata);
3071
3072 sdata = bfd_zalloc (abfd, amt);
3073 if (sdata == NULL)
3074 return FALSE;
3075 sec->used_by_bfd = sdata;
3076 }
3077
3078 return _bfd_elf_new_section_hook (abfd, sec);
3079 }
3080
3081 static struct _opd_sec_data *
3082 get_opd_info (asection * sec)
3083 {
3084 if (sec != NULL
3085 && ppc64_elf_section_data (sec) != NULL
3086 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3087 return &ppc64_elf_section_data (sec)->u.opd;
3088 return NULL;
3089 }
3090 \f
3091 /* Parameters for the qsort hook. */
3092 static bfd_boolean synthetic_relocatable;
3093 static asection *synthetic_opd;
3094
3095 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3096
3097 static int
3098 compare_symbols (const void *ap, const void *bp)
3099 {
3100 const asymbol *a = * (const asymbol **) ap;
3101 const asymbol *b = * (const asymbol **) bp;
3102
3103 /* Section symbols first. */
3104 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3105 return -1;
3106 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3107 return 1;
3108
3109 /* then .opd symbols. */
3110 if (synthetic_opd != NULL)
3111 {
3112 if (strcmp (a->section->name, ".opd") == 0
3113 && strcmp (b->section->name, ".opd") != 0)
3114 return -1;
3115 if (strcmp (a->section->name, ".opd") != 0
3116 && strcmp (b->section->name, ".opd") == 0)
3117 return 1;
3118 }
3119
3120 /* then other code symbols. */
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 ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3128 != (SEC_CODE | SEC_ALLOC)
3129 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3130 == (SEC_CODE | SEC_ALLOC))
3131 return 1;
3132
3133 if (synthetic_relocatable)
3134 {
3135 if (a->section->id < b->section->id)
3136 return -1;
3137
3138 if (a->section->id > b->section->id)
3139 return 1;
3140 }
3141
3142 if (a->value + a->section->vma < b->value + b->section->vma)
3143 return -1;
3144
3145 if (a->value + a->section->vma > b->value + b->section->vma)
3146 return 1;
3147
3148 /* For syms with the same value, prefer strong dynamic global function
3149 syms over other syms. */
3150 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3151 return -1;
3152
3153 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3154 return 1;
3155
3156 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3157 return -1;
3158
3159 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3160 return 1;
3161
3162 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3163 return -1;
3164
3165 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3166 return 1;
3167
3168 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3169 return -1;
3170
3171 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3172 return 1;
3173
3174 return 0;
3175 }
3176
3177 /* Search SYMS for a symbol of the given VALUE. */
3178
3179 static asymbol *
3180 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3181 {
3182 long mid;
3183
3184 if (id == (unsigned) -1)
3185 {
3186 while (lo < hi)
3187 {
3188 mid = (lo + hi) >> 1;
3189 if (syms[mid]->value + syms[mid]->section->vma < value)
3190 lo = mid + 1;
3191 else if (syms[mid]->value + syms[mid]->section->vma > value)
3192 hi = mid;
3193 else
3194 return syms[mid];
3195 }
3196 }
3197 else
3198 {
3199 while (lo < hi)
3200 {
3201 mid = (lo + hi) >> 1;
3202 if (syms[mid]->section->id < id)
3203 lo = mid + 1;
3204 else if (syms[mid]->section->id > id)
3205 hi = mid;
3206 else if (syms[mid]->value < value)
3207 lo = mid + 1;
3208 else if (syms[mid]->value > value)
3209 hi = mid;
3210 else
3211 return syms[mid];
3212 }
3213 }
3214 return NULL;
3215 }
3216
3217 static bfd_boolean
3218 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3219 {
3220 bfd_vma vma = *(bfd_vma *) ptr;
3221 return ((section->flags & SEC_ALLOC) != 0
3222 && section->vma <= vma
3223 && vma < section->vma + section->size);
3224 }
3225
3226 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3227 entry syms. Also generate @plt symbols for the glink branch table.
3228 Returns count of synthetic symbols in RET or -1 on error. */
3229
3230 static long
3231 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3232 long static_count, asymbol **static_syms,
3233 long dyn_count, asymbol **dyn_syms,
3234 asymbol **ret)
3235 {
3236 asymbol *s;
3237 long i;
3238 long count;
3239 char *names;
3240 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3241 asection *opd = NULL;
3242 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3243 asymbol **syms;
3244 int abi = abiversion (abfd);
3245
3246 *ret = NULL;
3247
3248 if (abi < 2)
3249 {
3250 opd = bfd_get_section_by_name (abfd, ".opd");
3251 if (opd == NULL && abi == 1)
3252 return 0;
3253 }
3254
3255 symcount = static_count;
3256 if (!relocatable)
3257 symcount += dyn_count;
3258 if (symcount == 0)
3259 return 0;
3260
3261 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3262 if (syms == NULL)
3263 return -1;
3264
3265 if (!relocatable && static_count != 0 && dyn_count != 0)
3266 {
3267 /* Use both symbol tables. */
3268 memcpy (syms, static_syms, static_count * sizeof (*syms));
3269 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3270 }
3271 else if (!relocatable && static_count == 0)
3272 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3273 else
3274 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3275
3276 synthetic_relocatable = relocatable;
3277 synthetic_opd = opd;
3278 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3279
3280 if (!relocatable && symcount > 1)
3281 {
3282 long j;
3283 /* Trim duplicate syms, since we may have merged the normal and
3284 dynamic symbols. Actually, we only care about syms that have
3285 different values, so trim any with the same value. */
3286 for (i = 1, j = 1; i < symcount; ++i)
3287 if (syms[i - 1]->value + syms[i - 1]->section->vma
3288 != syms[i]->value + syms[i]->section->vma)
3289 syms[j++] = syms[i];
3290 symcount = j;
3291 }
3292
3293 i = 0;
3294 /* Note that here and in compare_symbols we can't compare opd and
3295 sym->section directly. With separate debug info files, the
3296 symbols will be extracted from the debug file while abfd passed
3297 to this function is the real binary. */
3298 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3299 ++i;
3300 codesecsym = i;
3301
3302 for (; i < symcount; ++i)
3303 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3304 != (SEC_CODE | SEC_ALLOC))
3305 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3306 break;
3307 codesecsymend = i;
3308
3309 for (; i < symcount; ++i)
3310 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3311 break;
3312 secsymend = i;
3313
3314 if (opd != NULL)
3315 for (; i < symcount; ++i)
3316 if (strcmp (syms[i]->section->name, ".opd") != 0)
3317 break;
3318 opdsymend = i;
3319
3320 for (; i < symcount; ++i)
3321 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3322 != (SEC_CODE | SEC_ALLOC))
3323 break;
3324 symcount = i;
3325
3326 count = 0;
3327
3328 if (relocatable)
3329 {
3330 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3331 arelent *r;
3332 size_t size;
3333 long relcount;
3334
3335 if (opdsymend == secsymend)
3336 goto done;
3337
3338 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3339 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3340 if (relcount == 0)
3341 goto done;
3342
3343 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3344 {
3345 count = -1;
3346 goto done;
3347 }
3348
3349 size = 0;
3350 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3351 {
3352 asymbol *sym;
3353
3354 while (r < opd->relocation + relcount
3355 && r->address < syms[i]->value + opd->vma)
3356 ++r;
3357
3358 if (r == opd->relocation + relcount)
3359 break;
3360
3361 if (r->address != syms[i]->value + opd->vma)
3362 continue;
3363
3364 if (r->howto->type != R_PPC64_ADDR64)
3365 continue;
3366
3367 sym = *r->sym_ptr_ptr;
3368 if (!sym_exists_at (syms, opdsymend, symcount,
3369 sym->section->id, sym->value + r->addend))
3370 {
3371 ++count;
3372 size += sizeof (asymbol);
3373 size += strlen (syms[i]->name) + 2;
3374 }
3375 }
3376
3377 if (size == 0)
3378 goto done;
3379 s = *ret = bfd_malloc (size);
3380 if (s == NULL)
3381 {
3382 count = -1;
3383 goto done;
3384 }
3385
3386 names = (char *) (s + count);
3387
3388 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3389 {
3390 asymbol *sym;
3391
3392 while (r < opd->relocation + relcount
3393 && r->address < syms[i]->value + opd->vma)
3394 ++r;
3395
3396 if (r == opd->relocation + relcount)
3397 break;
3398
3399 if (r->address != syms[i]->value + opd->vma)
3400 continue;
3401
3402 if (r->howto->type != R_PPC64_ADDR64)
3403 continue;
3404
3405 sym = *r->sym_ptr_ptr;
3406 if (!sym_exists_at (syms, opdsymend, symcount,
3407 sym->section->id, sym->value + r->addend))
3408 {
3409 size_t len;
3410
3411 *s = *syms[i];
3412 s->flags |= BSF_SYNTHETIC;
3413 s->section = sym->section;
3414 s->value = sym->value + r->addend;
3415 s->name = names;
3416 *names++ = '.';
3417 len = strlen (syms[i]->name);
3418 memcpy (names, syms[i]->name, len + 1);
3419 names += len + 1;
3420 /* Have udata.p point back to the original symbol this
3421 synthetic symbol was derived from. */
3422 s->udata.p = syms[i];
3423 s++;
3424 }
3425 }
3426 }
3427 else
3428 {
3429 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3430 bfd_byte *contents = NULL;
3431 size_t size;
3432 long plt_count = 0;
3433 bfd_vma glink_vma = 0, resolv_vma = 0;
3434 asection *dynamic, *glink = NULL, *relplt = NULL;
3435 arelent *p;
3436
3437 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3438 {
3439 free_contents_and_exit_err:
3440 count = -1;
3441 free_contents_and_exit:
3442 if (contents)
3443 free (contents);
3444 goto done;
3445 }
3446
3447 size = 0;
3448 for (i = secsymend; i < opdsymend; ++i)
3449 {
3450 bfd_vma ent;
3451
3452 /* Ignore bogus symbols. */
3453 if (syms[i]->value > opd->size - 8)
3454 continue;
3455
3456 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3457 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3458 {
3459 ++count;
3460 size += sizeof (asymbol);
3461 size += strlen (syms[i]->name) + 2;
3462 }
3463 }
3464
3465 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3466 if (dyn_count != 0
3467 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3468 {
3469 bfd_byte *dynbuf, *extdyn, *extdynend;
3470 size_t extdynsize;
3471 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3472
3473 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3474 goto free_contents_and_exit_err;
3475
3476 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3477 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3478
3479 extdyn = dynbuf;
3480 extdynend = extdyn + dynamic->size;
3481 for (; extdyn < extdynend; extdyn += extdynsize)
3482 {
3483 Elf_Internal_Dyn dyn;
3484 (*swap_dyn_in) (abfd, extdyn, &dyn);
3485
3486 if (dyn.d_tag == DT_NULL)
3487 break;
3488
3489 if (dyn.d_tag == DT_PPC64_GLINK)
3490 {
3491 /* The first glink stub starts at offset 32; see
3492 comment in ppc64_elf_finish_dynamic_sections. */
3493 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3494 /* The .glink section usually does not survive the final
3495 link; search for the section (usually .text) where the
3496 glink stubs now reside. */
3497 glink = bfd_sections_find_if (abfd, section_covers_vma,
3498 &glink_vma);
3499 break;
3500 }
3501 }
3502
3503 free (dynbuf);
3504 }
3505
3506 if (glink != NULL)
3507 {
3508 /* Determine __glink trampoline by reading the relative branch
3509 from the first glink stub. */
3510 bfd_byte buf[4];
3511 unsigned int off = 0;
3512
3513 while (bfd_get_section_contents (abfd, glink, buf,
3514 glink_vma + off - glink->vma, 4))
3515 {
3516 unsigned int insn = bfd_get_32 (abfd, buf);
3517 insn ^= B_DOT;
3518 if ((insn & ~0x3fffffc) == 0)
3519 {
3520 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3521 break;
3522 }
3523 off += 4;
3524 if (off > 4)
3525 break;
3526 }
3527
3528 if (resolv_vma)
3529 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3530
3531 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3532 if (relplt != NULL)
3533 {
3534 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3535 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3536 goto free_contents_and_exit_err;
3537
3538 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3539 size += plt_count * sizeof (asymbol);
3540
3541 p = relplt->relocation;
3542 for (i = 0; i < plt_count; i++, p++)
3543 {
3544 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3545 if (p->addend != 0)
3546 size += sizeof ("+0x") - 1 + 16;
3547 }
3548 }
3549 }
3550
3551 if (size == 0)
3552 goto free_contents_and_exit;
3553 s = *ret = bfd_malloc (size);
3554 if (s == NULL)
3555 goto free_contents_and_exit_err;
3556
3557 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3558
3559 for (i = secsymend; i < opdsymend; ++i)
3560 {
3561 bfd_vma ent;
3562
3563 if (syms[i]->value > opd->size - 8)
3564 continue;
3565
3566 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3567 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3568 {
3569 long lo, hi;
3570 size_t len;
3571 asection *sec = abfd->sections;
3572
3573 *s = *syms[i];
3574 lo = codesecsym;
3575 hi = codesecsymend;
3576 while (lo < hi)
3577 {
3578 long mid = (lo + hi) >> 1;
3579 if (syms[mid]->section->vma < ent)
3580 lo = mid + 1;
3581 else if (syms[mid]->section->vma > ent)
3582 hi = mid;
3583 else
3584 {
3585 sec = syms[mid]->section;
3586 break;
3587 }
3588 }
3589
3590 if (lo >= hi && lo > codesecsym)
3591 sec = syms[lo - 1]->section;
3592
3593 for (; sec != NULL; sec = sec->next)
3594 {
3595 if (sec->vma > ent)
3596 break;
3597 /* SEC_LOAD may not be set if SEC is from a separate debug
3598 info file. */
3599 if ((sec->flags & SEC_ALLOC) == 0)
3600 break;
3601 if ((sec->flags & SEC_CODE) != 0)
3602 s->section = sec;
3603 }
3604 s->flags |= BSF_SYNTHETIC;
3605 s->value = ent - s->section->vma;
3606 s->name = names;
3607 *names++ = '.';
3608 len = strlen (syms[i]->name);
3609 memcpy (names, syms[i]->name, len + 1);
3610 names += len + 1;
3611 /* Have udata.p point back to the original symbol this
3612 synthetic symbol was derived from. */
3613 s->udata.p = syms[i];
3614 s++;
3615 }
3616 }
3617 free (contents);
3618
3619 if (glink != NULL && relplt != NULL)
3620 {
3621 if (resolv_vma)
3622 {
3623 /* Add a symbol for the main glink trampoline. */
3624 memset (s, 0, sizeof *s);
3625 s->the_bfd = abfd;
3626 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3627 s->section = glink;
3628 s->value = resolv_vma - glink->vma;
3629 s->name = names;
3630 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3631 names += sizeof ("__glink_PLTresolve");
3632 s++;
3633 count++;
3634 }
3635
3636 /* FIXME: It would be very much nicer to put sym@plt on the
3637 stub rather than on the glink branch table entry. The
3638 objdump disassembler would then use a sensible symbol
3639 name on plt calls. The difficulty in doing so is
3640 a) finding the stubs, and,
3641 b) matching stubs against plt entries, and,
3642 c) there can be multiple stubs for a given plt entry.
3643
3644 Solving (a) could be done by code scanning, but older
3645 ppc64 binaries used different stubs to current code.
3646 (b) is the tricky one since you need to known the toc
3647 pointer for at least one function that uses a pic stub to
3648 be able to calculate the plt address referenced.
3649 (c) means gdb would need to set multiple breakpoints (or
3650 find the glink branch itself) when setting breakpoints
3651 for pending shared library loads. */
3652 p = relplt->relocation;
3653 for (i = 0; i < plt_count; i++, p++)
3654 {
3655 size_t len;
3656
3657 *s = **p->sym_ptr_ptr;
3658 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3659 we are defining a symbol, ensure one of them is set. */
3660 if ((s->flags & BSF_LOCAL) == 0)
3661 s->flags |= BSF_GLOBAL;
3662 s->flags |= BSF_SYNTHETIC;
3663 s->section = glink;
3664 s->value = glink_vma - glink->vma;
3665 s->name = names;
3666 s->udata.p = NULL;
3667 len = strlen ((*p->sym_ptr_ptr)->name);
3668 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3669 names += len;
3670 if (p->addend != 0)
3671 {
3672 memcpy (names, "+0x", sizeof ("+0x") - 1);
3673 names += sizeof ("+0x") - 1;
3674 bfd_sprintf_vma (abfd, names, p->addend);
3675 names += strlen (names);
3676 }
3677 memcpy (names, "@plt", sizeof ("@plt"));
3678 names += sizeof ("@plt");
3679 s++;
3680 if (abi < 2)
3681 {
3682 glink_vma += 8;
3683 if (i >= 0x8000)
3684 glink_vma += 4;
3685 }
3686 else
3687 glink_vma += 4;
3688 }
3689 count += plt_count;
3690 }
3691 }
3692
3693 done:
3694 free (syms);
3695 return count;
3696 }
3697 \f
3698 /* The following functions are specific to the ELF linker, while
3699 functions above are used generally. Those named ppc64_elf_* are
3700 called by the main ELF linker code. They appear in this file more
3701 or less in the order in which they are called. eg.
3702 ppc64_elf_check_relocs is called early in the link process,
3703 ppc64_elf_finish_dynamic_sections is one of the last functions
3704 called.
3705
3706 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3707 functions have both a function code symbol and a function descriptor
3708 symbol. A call to foo in a relocatable object file looks like:
3709
3710 . .text
3711 . x:
3712 . bl .foo
3713 . nop
3714
3715 The function definition in another object file might be:
3716
3717 . .section .opd
3718 . foo: .quad .foo
3719 . .quad .TOC.@tocbase
3720 . .quad 0
3721 .
3722 . .text
3723 . .foo: blr
3724
3725 When the linker resolves the call during a static link, the branch
3726 unsurprisingly just goes to .foo and the .opd information is unused.
3727 If the function definition is in a shared library, things are a little
3728 different: The call goes via a plt call stub, the opd information gets
3729 copied to the plt, and the linker patches the nop.
3730
3731 . x:
3732 . bl .foo_stub
3733 . ld 2,40(1)
3734 .
3735 .
3736 . .foo_stub:
3737 . std 2,40(1) # in practice, the call stub
3738 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3739 . addi 11,11,Lfoo@toc@l # this is the general idea
3740 . ld 12,0(11)
3741 . ld 2,8(11)
3742 . mtctr 12
3743 . ld 11,16(11)
3744 . bctr
3745 .
3746 . .section .plt
3747 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3748
3749 The "reloc ()" notation is supposed to indicate that the linker emits
3750 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3751 copying.
3752
3753 What are the difficulties here? Well, firstly, the relocations
3754 examined by the linker in check_relocs are against the function code
3755 sym .foo, while the dynamic relocation in the plt is emitted against
3756 the function descriptor symbol, foo. Somewhere along the line, we need
3757 to carefully copy dynamic link information from one symbol to the other.
3758 Secondly, the generic part of the elf linker will make .foo a dynamic
3759 symbol as is normal for most other backends. We need foo dynamic
3760 instead, at least for an application final link. However, when
3761 creating a shared library containing foo, we need to have both symbols
3762 dynamic so that references to .foo are satisfied during the early
3763 stages of linking. Otherwise the linker might decide to pull in a
3764 definition from some other object, eg. a static library.
3765
3766 Update: As of August 2004, we support a new convention. Function
3767 calls may use the function descriptor symbol, ie. "bl foo". This
3768 behaves exactly as "bl .foo". */
3769
3770 /* Of those relocs that might be copied as dynamic relocs, this function
3771 selects those that must be copied when linking a shared library,
3772 even when the symbol is local. */
3773
3774 static int
3775 must_be_dyn_reloc (struct bfd_link_info *info,
3776 enum elf_ppc64_reloc_type r_type)
3777 {
3778 switch (r_type)
3779 {
3780 default:
3781 return 1;
3782
3783 case R_PPC64_REL32:
3784 case R_PPC64_REL64:
3785 case R_PPC64_REL30:
3786 return 0;
3787
3788 case R_PPC64_TPREL16:
3789 case R_PPC64_TPREL16_LO:
3790 case R_PPC64_TPREL16_HI:
3791 case R_PPC64_TPREL16_HA:
3792 case R_PPC64_TPREL16_DS:
3793 case R_PPC64_TPREL16_LO_DS:
3794 case R_PPC64_TPREL16_HIGH:
3795 case R_PPC64_TPREL16_HIGHA:
3796 case R_PPC64_TPREL16_HIGHER:
3797 case R_PPC64_TPREL16_HIGHERA:
3798 case R_PPC64_TPREL16_HIGHEST:
3799 case R_PPC64_TPREL16_HIGHESTA:
3800 case R_PPC64_TPREL64:
3801 return !bfd_link_executable (info);
3802 }
3803 }
3804
3805 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3806 copying dynamic variables from a shared lib into an app's dynbss
3807 section, and instead use a dynamic relocation to point into the
3808 shared lib. With code that gcc generates, it's vital that this be
3809 enabled; In the PowerPC64 ABI, the address of a function is actually
3810 the address of a function descriptor, which resides in the .opd
3811 section. gcc uses the descriptor directly rather than going via the
3812 GOT as some other ABI's do, which means that initialized function
3813 pointers must reference the descriptor. Thus, a function pointer
3814 initialized to the address of a function in a shared library will
3815 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3816 redefines the function descriptor symbol to point to the copy. This
3817 presents a problem as a plt entry for that function is also
3818 initialized from the function descriptor symbol and the copy reloc
3819 may not be initialized first. */
3820 #define ELIMINATE_COPY_RELOCS 1
3821
3822 /* Section name for stubs is the associated section name plus this
3823 string. */
3824 #define STUB_SUFFIX ".stub"
3825
3826 /* Linker stubs.
3827 ppc_stub_long_branch:
3828 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3829 destination, but a 24 bit branch in a stub section will reach.
3830 . b dest
3831
3832 ppc_stub_plt_branch:
3833 Similar to the above, but a 24 bit branch in the stub section won't
3834 reach its destination.
3835 . addis %r11,%r2,xxx@toc@ha
3836 . ld %r12,xxx@toc@l(%r11)
3837 . mtctr %r12
3838 . bctr
3839
3840 ppc_stub_plt_call:
3841 Used to call a function in a shared library. If it so happens that
3842 the plt entry referenced crosses a 64k boundary, then an extra
3843 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3844 . std %r2,40(%r1)
3845 . addis %r11,%r2,xxx@toc@ha
3846 . ld %r12,xxx+0@toc@l(%r11)
3847 . mtctr %r12
3848 . ld %r2,xxx+8@toc@l(%r11)
3849 . ld %r11,xxx+16@toc@l(%r11)
3850 . bctr
3851
3852 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3853 code to adjust the value and save r2 to support multiple toc sections.
3854 A ppc_stub_long_branch with an r2 offset looks like:
3855 . std %r2,40(%r1)
3856 . addis %r2,%r2,off@ha
3857 . addi %r2,%r2,off@l
3858 . b dest
3859
3860 A ppc_stub_plt_branch with an r2 offset looks like:
3861 . std %r2,40(%r1)
3862 . addis %r11,%r2,xxx@toc@ha
3863 . ld %r12,xxx@toc@l(%r11)
3864 . addis %r2,%r2,off@ha
3865 . addi %r2,%r2,off@l
3866 . mtctr %r12
3867 . bctr
3868
3869 In cases where the "addis" instruction would add zero, the "addis" is
3870 omitted and following instructions modified slightly in some cases.
3871 */
3872
3873 enum ppc_stub_type {
3874 ppc_stub_none,
3875 ppc_stub_long_branch,
3876 ppc_stub_long_branch_r2off,
3877 ppc_stub_plt_branch,
3878 ppc_stub_plt_branch_r2off,
3879 ppc_stub_plt_call,
3880 ppc_stub_plt_call_r2save,
3881 ppc_stub_global_entry,
3882 ppc_stub_save_res
3883 };
3884
3885 /* Information on stub grouping. */
3886 struct map_stub
3887 {
3888 /* The stub section. */
3889 asection *stub_sec;
3890 /* This is the section to which stubs in the group will be attached. */
3891 asection *link_sec;
3892 /* Next group. */
3893 struct map_stub *next;
3894 /* Whether to emit a copy of register save/restore functions in this
3895 group. */
3896 int needs_save_res;
3897 };
3898
3899 struct ppc_stub_hash_entry {
3900
3901 /* Base hash table entry structure. */
3902 struct bfd_hash_entry root;
3903
3904 enum ppc_stub_type stub_type;
3905
3906 /* Group information. */
3907 struct map_stub *group;
3908
3909 /* Offset within stub_sec of the beginning of this stub. */
3910 bfd_vma stub_offset;
3911
3912 /* Given the symbol's value and its section we can determine its final
3913 value when building the stubs (so the stub knows where to jump. */
3914 bfd_vma target_value;
3915 asection *target_section;
3916
3917 /* The symbol table entry, if any, that this was derived from. */
3918 struct ppc_link_hash_entry *h;
3919 struct plt_entry *plt_ent;
3920
3921 /* Symbol st_other. */
3922 unsigned char other;
3923 };
3924
3925 struct ppc_branch_hash_entry {
3926
3927 /* Base hash table entry structure. */
3928 struct bfd_hash_entry root;
3929
3930 /* Offset within branch lookup table. */
3931 unsigned int offset;
3932
3933 /* Generation marker. */
3934 unsigned int iter;
3935 };
3936
3937 /* Used to track dynamic relocations for local symbols. */
3938 struct ppc_dyn_relocs
3939 {
3940 struct ppc_dyn_relocs *next;
3941
3942 /* The input section of the reloc. */
3943 asection *sec;
3944
3945 /* Total number of relocs copied for the input section. */
3946 unsigned int count : 31;
3947
3948 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3949 unsigned int ifunc : 1;
3950 };
3951
3952 struct ppc_link_hash_entry
3953 {
3954 struct elf_link_hash_entry elf;
3955
3956 union {
3957 /* A pointer to the most recently used stub hash entry against this
3958 symbol. */
3959 struct ppc_stub_hash_entry *stub_cache;
3960
3961 /* A pointer to the next symbol starting with a '.' */
3962 struct ppc_link_hash_entry *next_dot_sym;
3963 } u;
3964
3965 /* Track dynamic relocs copied for this symbol. */
3966 struct elf_dyn_relocs *dyn_relocs;
3967
3968 /* Chain of aliases referring to a weakdef. */
3969 struct ppc_link_hash_entry *weakref;
3970
3971 /* Link between function code and descriptor symbols. */
3972 struct ppc_link_hash_entry *oh;
3973
3974 /* Flag function code and descriptor symbols. */
3975 unsigned int is_func:1;
3976 unsigned int is_func_descriptor:1;
3977 unsigned int fake:1;
3978
3979 /* Whether global opd/toc sym has been adjusted or not.
3980 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3981 should be set for all globals defined in any opd/toc section. */
3982 unsigned int adjust_done:1;
3983
3984 /* Set if this is an out-of-line register save/restore function,
3985 with non-standard calling convention. */
3986 unsigned int save_res:1;
3987
3988 /* Contexts in which symbol is used in the GOT (or TOC).
3989 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3990 corresponding relocs are encountered during check_relocs.
3991 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3992 indicate the corresponding GOT entry type is not needed.
3993 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3994 a TPREL one. We use a separate flag rather than setting TPREL
3995 just for convenience in distinguishing the two cases. */
3996 #define TLS_GD 1 /* GD reloc. */
3997 #define TLS_LD 2 /* LD reloc. */
3998 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3999 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4000 #define TLS_TLS 16 /* Any TLS reloc. */
4001 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4002 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4003 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
4004 unsigned char tls_mask;
4005 };
4006
4007 /* ppc64 ELF linker hash table. */
4008
4009 struct ppc_link_hash_table
4010 {
4011 struct elf_link_hash_table elf;
4012
4013 /* The stub hash table. */
4014 struct bfd_hash_table stub_hash_table;
4015
4016 /* Another hash table for plt_branch stubs. */
4017 struct bfd_hash_table branch_hash_table;
4018
4019 /* Hash table for function prologue tocsave. */
4020 htab_t tocsave_htab;
4021
4022 /* Various options and other info passed from the linker. */
4023 struct ppc64_elf_params *params;
4024
4025 /* The size of sec_info below. */
4026 unsigned int sec_info_arr_size;
4027
4028 /* Per-section array of extra section info. Done this way rather
4029 than as part of ppc64_elf_section_data so we have the info for
4030 non-ppc64 sections. */
4031 struct
4032 {
4033 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4034 bfd_vma toc_off;
4035
4036 union
4037 {
4038 /* The section group that this section belongs to. */
4039 struct map_stub *group;
4040 /* A temp section list pointer. */
4041 asection *list;
4042 } u;
4043 } *sec_info;
4044
4045 /* Linked list of groups. */
4046 struct map_stub *group;
4047
4048 /* Temp used when calculating TOC pointers. */
4049 bfd_vma toc_curr;
4050 bfd *toc_bfd;
4051 asection *toc_first_sec;
4052
4053 /* Used when adding symbols. */
4054 struct ppc_link_hash_entry *dot_syms;
4055
4056 /* Shortcuts to get to dynamic linker sections. */
4057 asection *glink;
4058 asection *sfpr;
4059 asection *brlt;
4060 asection *relbrlt;
4061 asection *glink_eh_frame;
4062
4063 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4064 struct ppc_link_hash_entry *tls_get_addr;
4065 struct ppc_link_hash_entry *tls_get_addr_fd;
4066
4067 /* The size of reliplt used by got entry relocs. */
4068 bfd_size_type got_reli_size;
4069
4070 /* Statistics. */
4071 unsigned long stub_count[ppc_stub_global_entry];
4072
4073 /* Number of stubs against global syms. */
4074 unsigned long stub_globals;
4075
4076 /* Set if we're linking code with function descriptors. */
4077 unsigned int opd_abi:1;
4078
4079 /* Support for multiple toc sections. */
4080 unsigned int do_multi_toc:1;
4081 unsigned int multi_toc_needed:1;
4082 unsigned int second_toc_pass:1;
4083 unsigned int do_toc_opt:1;
4084
4085 /* Set on error. */
4086 unsigned int stub_error:1;
4087
4088 /* Whether func_desc_adjust needs to be run over symbols. */
4089 unsigned int need_func_desc_adj:1;
4090
4091 /* Incremented every time we size stubs. */
4092 unsigned int stub_iteration;
4093
4094 /* Small local sym cache. */
4095 struct sym_cache sym_cache;
4096 };
4097
4098 /* Rename some of the generic section flags to better document how they
4099 are used here. */
4100
4101 /* Nonzero if this section has TLS related relocations. */
4102 #define has_tls_reloc sec_flg0
4103
4104 /* Nonzero if this section has a call to __tls_get_addr. */
4105 #define has_tls_get_addr_call sec_flg1
4106
4107 /* Nonzero if this section has any toc or got relocs. */
4108 #define has_toc_reloc sec_flg2
4109
4110 /* Nonzero if this section has a call to another section that uses
4111 the toc or got. */
4112 #define makes_toc_func_call sec_flg3
4113
4114 /* Recursion protection when determining above flag. */
4115 #define call_check_in_progress sec_flg4
4116 #define call_check_done sec_flg5
4117
4118 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4119
4120 #define ppc_hash_table(p) \
4121 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4122 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4123
4124 #define ppc_stub_hash_lookup(table, string, create, copy) \
4125 ((struct ppc_stub_hash_entry *) \
4126 bfd_hash_lookup ((table), (string), (create), (copy)))
4127
4128 #define ppc_branch_hash_lookup(table, string, create, copy) \
4129 ((struct ppc_branch_hash_entry *) \
4130 bfd_hash_lookup ((table), (string), (create), (copy)))
4131
4132 /* Create an entry in the stub hash table. */
4133
4134 static struct bfd_hash_entry *
4135 stub_hash_newfunc (struct bfd_hash_entry *entry,
4136 struct bfd_hash_table *table,
4137 const char *string)
4138 {
4139 /* Allocate the structure if it has not already been allocated by a
4140 subclass. */
4141 if (entry == NULL)
4142 {
4143 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4144 if (entry == NULL)
4145 return entry;
4146 }
4147
4148 /* Call the allocation method of the superclass. */
4149 entry = bfd_hash_newfunc (entry, table, string);
4150 if (entry != NULL)
4151 {
4152 struct ppc_stub_hash_entry *eh;
4153
4154 /* Initialize the local fields. */
4155 eh = (struct ppc_stub_hash_entry *) entry;
4156 eh->stub_type = ppc_stub_none;
4157 eh->group = NULL;
4158 eh->stub_offset = 0;
4159 eh->target_value = 0;
4160 eh->target_section = NULL;
4161 eh->h = NULL;
4162 eh->plt_ent = NULL;
4163 eh->other = 0;
4164 }
4165
4166 return entry;
4167 }
4168
4169 /* Create an entry in the branch hash table. */
4170
4171 static struct bfd_hash_entry *
4172 branch_hash_newfunc (struct bfd_hash_entry *entry,
4173 struct bfd_hash_table *table,
4174 const char *string)
4175 {
4176 /* Allocate the structure if it has not already been allocated by a
4177 subclass. */
4178 if (entry == NULL)
4179 {
4180 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4181 if (entry == NULL)
4182 return entry;
4183 }
4184
4185 /* Call the allocation method of the superclass. */
4186 entry = bfd_hash_newfunc (entry, table, string);
4187 if (entry != NULL)
4188 {
4189 struct ppc_branch_hash_entry *eh;
4190
4191 /* Initialize the local fields. */
4192 eh = (struct ppc_branch_hash_entry *) entry;
4193 eh->offset = 0;
4194 eh->iter = 0;
4195 }
4196
4197 return entry;
4198 }
4199
4200 /* Create an entry in a ppc64 ELF linker hash table. */
4201
4202 static struct bfd_hash_entry *
4203 link_hash_newfunc (struct bfd_hash_entry *entry,
4204 struct bfd_hash_table *table,
4205 const char *string)
4206 {
4207 /* Allocate the structure if it has not already been allocated by a
4208 subclass. */
4209 if (entry == NULL)
4210 {
4211 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4212 if (entry == NULL)
4213 return entry;
4214 }
4215
4216 /* Call the allocation method of the superclass. */
4217 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4218 if (entry != NULL)
4219 {
4220 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4221
4222 memset (&eh->u.stub_cache, 0,
4223 (sizeof (struct ppc_link_hash_entry)
4224 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4225
4226 /* When making function calls, old ABI code references function entry
4227 points (dot symbols), while new ABI code references the function
4228 descriptor symbol. We need to make any combination of reference and
4229 definition work together, without breaking archive linking.
4230
4231 For a defined function "foo" and an undefined call to "bar":
4232 An old object defines "foo" and ".foo", references ".bar" (possibly
4233 "bar" too).
4234 A new object defines "foo" and references "bar".
4235
4236 A new object thus has no problem with its undefined symbols being
4237 satisfied by definitions in an old object. On the other hand, the
4238 old object won't have ".bar" satisfied by a new object.
4239
4240 Keep a list of newly added dot-symbols. */
4241
4242 if (string[0] == '.')
4243 {
4244 struct ppc_link_hash_table *htab;
4245
4246 htab = (struct ppc_link_hash_table *) table;
4247 eh->u.next_dot_sym = htab->dot_syms;
4248 htab->dot_syms = eh;
4249 }
4250 }
4251
4252 return entry;
4253 }
4254
4255 struct tocsave_entry {
4256 asection *sec;
4257 bfd_vma offset;
4258 };
4259
4260 static hashval_t
4261 tocsave_htab_hash (const void *p)
4262 {
4263 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4264 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4265 }
4266
4267 static int
4268 tocsave_htab_eq (const void *p1, const void *p2)
4269 {
4270 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4271 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4272 return e1->sec == e2->sec && e1->offset == e2->offset;
4273 }
4274
4275 /* Destroy a ppc64 ELF linker hash table. */
4276
4277 static void
4278 ppc64_elf_link_hash_table_free (bfd *obfd)
4279 {
4280 struct ppc_link_hash_table *htab;
4281
4282 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4283 if (htab->tocsave_htab)
4284 htab_delete (htab->tocsave_htab);
4285 bfd_hash_table_free (&htab->branch_hash_table);
4286 bfd_hash_table_free (&htab->stub_hash_table);
4287 _bfd_elf_link_hash_table_free (obfd);
4288 }
4289
4290 /* Create a ppc64 ELF linker hash table. */
4291
4292 static struct bfd_link_hash_table *
4293 ppc64_elf_link_hash_table_create (bfd *abfd)
4294 {
4295 struct ppc_link_hash_table *htab;
4296 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4297
4298 htab = bfd_zmalloc (amt);
4299 if (htab == NULL)
4300 return NULL;
4301
4302 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4303 sizeof (struct ppc_link_hash_entry),
4304 PPC64_ELF_DATA))
4305 {
4306 free (htab);
4307 return NULL;
4308 }
4309
4310 /* Init the stub hash table too. */
4311 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4312 sizeof (struct ppc_stub_hash_entry)))
4313 {
4314 _bfd_elf_link_hash_table_free (abfd);
4315 return NULL;
4316 }
4317
4318 /* And the branch hash table. */
4319 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4320 sizeof (struct ppc_branch_hash_entry)))
4321 {
4322 bfd_hash_table_free (&htab->stub_hash_table);
4323 _bfd_elf_link_hash_table_free (abfd);
4324 return NULL;
4325 }
4326
4327 htab->tocsave_htab = htab_try_create (1024,
4328 tocsave_htab_hash,
4329 tocsave_htab_eq,
4330 NULL);
4331 if (htab->tocsave_htab == NULL)
4332 {
4333 ppc64_elf_link_hash_table_free (abfd);
4334 return NULL;
4335 }
4336 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4337
4338 /* Initializing two fields of the union is just cosmetic. We really
4339 only care about glist, but when compiled on a 32-bit host the
4340 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4341 debugger inspection of these fields look nicer. */
4342 htab->elf.init_got_refcount.refcount = 0;
4343 htab->elf.init_got_refcount.glist = NULL;
4344 htab->elf.init_plt_refcount.refcount = 0;
4345 htab->elf.init_plt_refcount.glist = NULL;
4346 htab->elf.init_got_offset.offset = 0;
4347 htab->elf.init_got_offset.glist = NULL;
4348 htab->elf.init_plt_offset.offset = 0;
4349 htab->elf.init_plt_offset.glist = NULL;
4350
4351 return &htab->elf.root;
4352 }
4353
4354 /* Create sections for linker generated code. */
4355
4356 static bfd_boolean
4357 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4358 {
4359 struct ppc_link_hash_table *htab;
4360 flagword flags;
4361
4362 htab = ppc_hash_table (info);
4363
4364 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4365 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4366 if (htab->params->save_restore_funcs)
4367 {
4368 /* Create .sfpr for code to save and restore fp regs. */
4369 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4370 flags);
4371 if (htab->sfpr == NULL
4372 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4373 return FALSE;
4374 }
4375
4376 if (bfd_link_relocatable (info))
4377 return TRUE;
4378
4379 /* Create .glink for lazy dynamic linking support. */
4380 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4381 flags);
4382 if (htab->glink == NULL
4383 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4384 return FALSE;
4385
4386 if (!info->no_ld_generated_unwind_info)
4387 {
4388 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4389 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4390 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4391 ".eh_frame",
4392 flags);
4393 if (htab->glink_eh_frame == NULL
4394 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4395 return FALSE;
4396 }
4397
4398 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4399 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4400 if (htab->elf.iplt == NULL
4401 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4402 return FALSE;
4403
4404 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4405 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4406 htab->elf.irelplt
4407 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4408 if (htab->elf.irelplt == NULL
4409 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4410 return FALSE;
4411
4412 /* Create branch lookup table for plt_branch stubs. */
4413 flags = (SEC_ALLOC | SEC_LOAD
4414 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4415 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4416 flags);
4417 if (htab->brlt == NULL
4418 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4419 return FALSE;
4420
4421 if (!bfd_link_pic (info))
4422 return TRUE;
4423
4424 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4425 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4426 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4427 ".rela.branch_lt",
4428 flags);
4429 if (htab->relbrlt == NULL
4430 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4431 return FALSE;
4432
4433 return TRUE;
4434 }
4435
4436 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4437
4438 bfd_boolean
4439 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4440 struct ppc64_elf_params *params)
4441 {
4442 struct ppc_link_hash_table *htab;
4443
4444 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4445
4446 /* Always hook our dynamic sections into the first bfd, which is the
4447 linker created stub bfd. This ensures that the GOT header is at
4448 the start of the output TOC section. */
4449 htab = ppc_hash_table (info);
4450 htab->elf.dynobj = params->stub_bfd;
4451 htab->params = params;
4452
4453 return create_linkage_sections (htab->elf.dynobj, info);
4454 }
4455
4456 /* Build a name for an entry in the stub hash table. */
4457
4458 static char *
4459 ppc_stub_name (const asection *input_section,
4460 const asection *sym_sec,
4461 const struct ppc_link_hash_entry *h,
4462 const Elf_Internal_Rela *rel)
4463 {
4464 char *stub_name;
4465 ssize_t len;
4466
4467 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4468 offsets from a sym as a branch target? In fact, we could
4469 probably assume the addend is always zero. */
4470 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4471
4472 if (h)
4473 {
4474 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4475 stub_name = bfd_malloc (len);
4476 if (stub_name == NULL)
4477 return stub_name;
4478
4479 len = sprintf (stub_name, "%08x.%s+%x",
4480 input_section->id & 0xffffffff,
4481 h->elf.root.root.string,
4482 (int) rel->r_addend & 0xffffffff);
4483 }
4484 else
4485 {
4486 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4487 stub_name = bfd_malloc (len);
4488 if (stub_name == NULL)
4489 return stub_name;
4490
4491 len = sprintf (stub_name, "%08x.%x:%x+%x",
4492 input_section->id & 0xffffffff,
4493 sym_sec->id & 0xffffffff,
4494 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4495 (int) rel->r_addend & 0xffffffff);
4496 }
4497 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4498 stub_name[len - 2] = 0;
4499 return stub_name;
4500 }
4501
4502 /* Look up an entry in the stub hash. Stub entries are cached because
4503 creating the stub name takes a bit of time. */
4504
4505 static struct ppc_stub_hash_entry *
4506 ppc_get_stub_entry (const asection *input_section,
4507 const asection *sym_sec,
4508 struct ppc_link_hash_entry *h,
4509 const Elf_Internal_Rela *rel,
4510 struct ppc_link_hash_table *htab)
4511 {
4512 struct ppc_stub_hash_entry *stub_entry;
4513 struct map_stub *group;
4514
4515 /* If this input section is part of a group of sections sharing one
4516 stub section, then use the id of the first section in the group.
4517 Stub names need to include a section id, as there may well be
4518 more than one stub used to reach say, printf, and we need to
4519 distinguish between them. */
4520 group = htab->sec_info[input_section->id].u.group;
4521 if (group == NULL)
4522 return NULL;
4523
4524 if (h != NULL && h->u.stub_cache != NULL
4525 && h->u.stub_cache->h == h
4526 && h->u.stub_cache->group == group)
4527 {
4528 stub_entry = h->u.stub_cache;
4529 }
4530 else
4531 {
4532 char *stub_name;
4533
4534 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4535 if (stub_name == NULL)
4536 return NULL;
4537
4538 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4539 stub_name, FALSE, FALSE);
4540 if (h != NULL)
4541 h->u.stub_cache = stub_entry;
4542
4543 free (stub_name);
4544 }
4545
4546 return stub_entry;
4547 }
4548
4549 /* Add a new stub entry to the stub hash. Not all fields of the new
4550 stub entry are initialised. */
4551
4552 static struct ppc_stub_hash_entry *
4553 ppc_add_stub (const char *stub_name,
4554 asection *section,
4555 struct bfd_link_info *info)
4556 {
4557 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4558 struct map_stub *group;
4559 asection *link_sec;
4560 asection *stub_sec;
4561 struct ppc_stub_hash_entry *stub_entry;
4562
4563 group = htab->sec_info[section->id].u.group;
4564 link_sec = group->link_sec;
4565 stub_sec = group->stub_sec;
4566 if (stub_sec == NULL)
4567 {
4568 size_t namelen;
4569 bfd_size_type len;
4570 char *s_name;
4571
4572 namelen = strlen (link_sec->name);
4573 len = namelen + sizeof (STUB_SUFFIX);
4574 s_name = bfd_alloc (htab->params->stub_bfd, len);
4575 if (s_name == NULL)
4576 return NULL;
4577
4578 memcpy (s_name, link_sec->name, namelen);
4579 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4580 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4581 if (stub_sec == NULL)
4582 return NULL;
4583 group->stub_sec = stub_sec;
4584 }
4585
4586 /* Enter this entry into the linker stub hash table. */
4587 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4588 TRUE, FALSE);
4589 if (stub_entry == NULL)
4590 {
4591 /* xgettext:c-format */
4592 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4593 section->owner, stub_name);
4594 return NULL;
4595 }
4596
4597 stub_entry->group = group;
4598 stub_entry->stub_offset = 0;
4599 return stub_entry;
4600 }
4601
4602 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4603 not already done. */
4604
4605 static bfd_boolean
4606 create_got_section (bfd *abfd, struct bfd_link_info *info)
4607 {
4608 asection *got, *relgot;
4609 flagword flags;
4610 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4611
4612 if (!is_ppc64_elf (abfd))
4613 return FALSE;
4614 if (htab == NULL)
4615 return FALSE;
4616
4617 if (!htab->elf.sgot
4618 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4619 return FALSE;
4620
4621 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4622 | SEC_LINKER_CREATED);
4623
4624 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4625 if (!got
4626 || !bfd_set_section_alignment (abfd, got, 3))
4627 return FALSE;
4628
4629 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4630 flags | SEC_READONLY);
4631 if (!relgot
4632 || ! bfd_set_section_alignment (abfd, relgot, 3))
4633 return FALSE;
4634
4635 ppc64_elf_tdata (abfd)->got = got;
4636 ppc64_elf_tdata (abfd)->relgot = relgot;
4637 return TRUE;
4638 }
4639
4640 /* Follow indirect and warning symbol links. */
4641
4642 static inline struct bfd_link_hash_entry *
4643 follow_link (struct bfd_link_hash_entry *h)
4644 {
4645 while (h->type == bfd_link_hash_indirect
4646 || h->type == bfd_link_hash_warning)
4647 h = h->u.i.link;
4648 return h;
4649 }
4650
4651 static inline struct elf_link_hash_entry *
4652 elf_follow_link (struct elf_link_hash_entry *h)
4653 {
4654 return (struct elf_link_hash_entry *) follow_link (&h->root);
4655 }
4656
4657 static inline struct ppc_link_hash_entry *
4658 ppc_follow_link (struct ppc_link_hash_entry *h)
4659 {
4660 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4661 }
4662
4663 /* Merge PLT info on FROM with that on TO. */
4664
4665 static void
4666 move_plt_plist (struct ppc_link_hash_entry *from,
4667 struct ppc_link_hash_entry *to)
4668 {
4669 if (from->elf.plt.plist != NULL)
4670 {
4671 if (to->elf.plt.plist != NULL)
4672 {
4673 struct plt_entry **entp;
4674 struct plt_entry *ent;
4675
4676 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4677 {
4678 struct plt_entry *dent;
4679
4680 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4681 if (dent->addend == ent->addend)
4682 {
4683 dent->plt.refcount += ent->plt.refcount;
4684 *entp = ent->next;
4685 break;
4686 }
4687 if (dent == NULL)
4688 entp = &ent->next;
4689 }
4690 *entp = to->elf.plt.plist;
4691 }
4692
4693 to->elf.plt.plist = from->elf.plt.plist;
4694 from->elf.plt.plist = NULL;
4695 }
4696 }
4697
4698 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4699
4700 static void
4701 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4702 struct elf_link_hash_entry *dir,
4703 struct elf_link_hash_entry *ind)
4704 {
4705 struct ppc_link_hash_entry *edir, *eind;
4706
4707 edir = (struct ppc_link_hash_entry *) dir;
4708 eind = (struct ppc_link_hash_entry *) ind;
4709
4710 edir->is_func |= eind->is_func;
4711 edir->is_func_descriptor |= eind->is_func_descriptor;
4712 edir->tls_mask |= eind->tls_mask;
4713 if (eind->oh != NULL)
4714 edir->oh = ppc_follow_link (eind->oh);
4715
4716 if (edir->elf.versioned != versioned_hidden)
4717 {
4718 /* If called to transfer flags for a weakdef during processing
4719 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4720 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4721 if (!(ELIMINATE_COPY_RELOCS
4722 && eind->elf.root.type != bfd_link_hash_indirect
4723 && edir->elf.dynamic_adjusted))
4724 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4725
4726 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4727 edir->elf.ref_regular |= eind->elf.ref_regular;
4728 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4729 edir->elf.needs_plt |= eind->elf.needs_plt;
4730 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4731 }
4732
4733 /* If we were called to copy over info for a weak sym, don't copy
4734 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4735 in order to simplify readonly_dynrelocs and save a field in the
4736 symbol hash entry, but that means dyn_relocs can't be used in any
4737 tests about a specific symbol, or affect other symbol flags which
4738 are then tested.
4739 Chain weakdefs so we can get from the weakdef back to an alias.
4740 The list is circular so that we don't need to use u.weakdef as
4741 well as this list to look at all aliases. */
4742 if (eind->elf.root.type != bfd_link_hash_indirect)
4743 {
4744 struct ppc_link_hash_entry *cur, *add, *next;
4745
4746 add = eind;
4747 do
4748 {
4749 cur = edir->weakref;
4750 if (cur != NULL)
4751 {
4752 do
4753 {
4754 /* We can be called twice for the same symbols.
4755 Don't make multiple loops. */
4756 if (cur == add)
4757 return;
4758 cur = cur->weakref;
4759 } while (cur != edir);
4760 }
4761 next = add->weakref;
4762 if (cur != add)
4763 {
4764 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4765 edir->weakref = add;
4766 }
4767 add = next;
4768 } while (add != NULL && add != eind);
4769 return;
4770 }
4771
4772 /* Copy over any dynamic relocs we may have on the indirect sym. */
4773 if (eind->dyn_relocs != NULL)
4774 {
4775 if (edir->dyn_relocs != NULL)
4776 {
4777 struct elf_dyn_relocs **pp;
4778 struct elf_dyn_relocs *p;
4779
4780 /* Add reloc counts against the indirect sym to the direct sym
4781 list. Merge any entries against the same section. */
4782 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4783 {
4784 struct elf_dyn_relocs *q;
4785
4786 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4787 if (q->sec == p->sec)
4788 {
4789 q->pc_count += p->pc_count;
4790 q->count += p->count;
4791 *pp = p->next;
4792 break;
4793 }
4794 if (q == NULL)
4795 pp = &p->next;
4796 }
4797 *pp = edir->dyn_relocs;
4798 }
4799
4800 edir->dyn_relocs = eind->dyn_relocs;
4801 eind->dyn_relocs = NULL;
4802 }
4803
4804 /* Copy over got entries that we may have already seen to the
4805 symbol which just became indirect. */
4806 if (eind->elf.got.glist != NULL)
4807 {
4808 if (edir->elf.got.glist != NULL)
4809 {
4810 struct got_entry **entp;
4811 struct got_entry *ent;
4812
4813 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4814 {
4815 struct got_entry *dent;
4816
4817 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4818 if (dent->addend == ent->addend
4819 && dent->owner == ent->owner
4820 && dent->tls_type == ent->tls_type)
4821 {
4822 dent->got.refcount += ent->got.refcount;
4823 *entp = ent->next;
4824 break;
4825 }
4826 if (dent == NULL)
4827 entp = &ent->next;
4828 }
4829 *entp = edir->elf.got.glist;
4830 }
4831
4832 edir->elf.got.glist = eind->elf.got.glist;
4833 eind->elf.got.glist = NULL;
4834 }
4835
4836 /* And plt entries. */
4837 move_plt_plist (eind, edir);
4838
4839 if (eind->elf.dynindx != -1)
4840 {
4841 if (edir->elf.dynindx != -1)
4842 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4843 edir->elf.dynstr_index);
4844 edir->elf.dynindx = eind->elf.dynindx;
4845 edir->elf.dynstr_index = eind->elf.dynstr_index;
4846 eind->elf.dynindx = -1;
4847 eind->elf.dynstr_index = 0;
4848 }
4849 }
4850
4851 /* Find the function descriptor hash entry from the given function code
4852 hash entry FH. Link the entries via their OH fields. */
4853
4854 static struct ppc_link_hash_entry *
4855 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4856 {
4857 struct ppc_link_hash_entry *fdh = fh->oh;
4858
4859 if (fdh == NULL)
4860 {
4861 const char *fd_name = fh->elf.root.root.string + 1;
4862
4863 fdh = (struct ppc_link_hash_entry *)
4864 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4865 if (fdh == NULL)
4866 return fdh;
4867
4868 fdh->is_func_descriptor = 1;
4869 fdh->oh = fh;
4870 fh->is_func = 1;
4871 fh->oh = fdh;
4872 }
4873
4874 fdh = ppc_follow_link (fdh);
4875 fdh->is_func_descriptor = 1;
4876 fdh->oh = fh;
4877 return fdh;
4878 }
4879
4880 /* Make a fake function descriptor sym for the undefined code sym FH. */
4881
4882 static struct ppc_link_hash_entry *
4883 make_fdh (struct bfd_link_info *info,
4884 struct ppc_link_hash_entry *fh)
4885 {
4886 bfd *abfd = fh->elf.root.u.undef.abfd;
4887 struct bfd_link_hash_entry *bh = NULL;
4888 struct ppc_link_hash_entry *fdh;
4889 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4890 ? BSF_WEAK
4891 : BSF_GLOBAL);
4892
4893 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4894 fh->elf.root.root.string + 1,
4895 flags, bfd_und_section_ptr, 0,
4896 NULL, FALSE, FALSE, &bh))
4897 return NULL;
4898
4899 fdh = (struct ppc_link_hash_entry *) bh;
4900 fdh->elf.non_elf = 0;
4901 fdh->fake = 1;
4902 fdh->is_func_descriptor = 1;
4903 fdh->oh = fh;
4904 fh->is_func = 1;
4905 fh->oh = fdh;
4906 return fdh;
4907 }
4908
4909 /* Fix function descriptor symbols defined in .opd sections to be
4910 function type. */
4911
4912 static bfd_boolean
4913 ppc64_elf_add_symbol_hook (bfd *ibfd,
4914 struct bfd_link_info *info,
4915 Elf_Internal_Sym *isym,
4916 const char **name,
4917 flagword *flags ATTRIBUTE_UNUSED,
4918 asection **sec,
4919 bfd_vma *value)
4920 {
4921 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4922 && (ibfd->flags & DYNAMIC) == 0
4923 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4924 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4925
4926 if (*sec != NULL
4927 && strcmp ((*sec)->name, ".opd") == 0)
4928 {
4929 asection *code_sec;
4930
4931 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4932 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4933 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4934
4935 /* If the symbol is a function defined in .opd, and the function
4936 code is in a discarded group, let it appear to be undefined. */
4937 if (!bfd_link_relocatable (info)
4938 && (*sec)->reloc_count != 0
4939 && opd_entry_value (*sec, *value, &code_sec, NULL,
4940 FALSE) != (bfd_vma) -1
4941 && discarded_section (code_sec))
4942 {
4943 *sec = bfd_und_section_ptr;
4944 isym->st_shndx = SHN_UNDEF;
4945 }
4946 }
4947 else if (*sec != NULL
4948 && strcmp ((*sec)->name, ".toc") == 0
4949 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4950 {
4951 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4952 if (htab != NULL)
4953 htab->params->object_in_toc = 1;
4954 }
4955
4956 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4957 {
4958 if (abiversion (ibfd) == 0)
4959 set_abiversion (ibfd, 2);
4960 else if (abiversion (ibfd) == 1)
4961 {
4962 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4963 " for ABI version 1\n"), name);
4964 bfd_set_error (bfd_error_bad_value);
4965 return FALSE;
4966 }
4967 }
4968
4969 return TRUE;
4970 }
4971
4972 /* Merge non-visibility st_other attributes: local entry point. */
4973
4974 static void
4975 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4976 const Elf_Internal_Sym *isym,
4977 bfd_boolean definition,
4978 bfd_boolean dynamic)
4979 {
4980 if (definition && !dynamic)
4981 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4982 | ELF_ST_VISIBILITY (h->other));
4983 }
4984
4985 /* Hook called on merging a symbol. We use this to clear "fake" since
4986 we now have a real symbol. */
4987
4988 static bfd_boolean
4989 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4990 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
4991 asection **psec ATTRIBUTE_UNUSED,
4992 bfd_boolean newdef ATTRIBUTE_UNUSED,
4993 bfd_boolean olddef ATTRIBUTE_UNUSED,
4994 bfd *oldbfd ATTRIBUTE_UNUSED,
4995 const asection *oldsec ATTRIBUTE_UNUSED)
4996 {
4997 ((struct ppc_link_hash_entry *) h)->fake = 0;
4998 return TRUE;
4999 }
5000
5001 /* This function makes an old ABI object reference to ".bar" cause the
5002 inclusion of a new ABI object archive that defines "bar".
5003 NAME is a symbol defined in an archive. Return a symbol in the hash
5004 table that might be satisfied by the archive symbols. */
5005
5006 static struct elf_link_hash_entry *
5007 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5008 struct bfd_link_info *info,
5009 const char *name)
5010 {
5011 struct elf_link_hash_entry *h;
5012 char *dot_name;
5013 size_t len;
5014
5015 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5016 if (h != NULL
5017 /* Don't return this sym if it is a fake function descriptor
5018 created by add_symbol_adjust. */
5019 && !((struct ppc_link_hash_entry *) h)->fake)
5020 return h;
5021
5022 if (name[0] == '.')
5023 return h;
5024
5025 len = strlen (name);
5026 dot_name = bfd_alloc (abfd, len + 2);
5027 if (dot_name == NULL)
5028 return (struct elf_link_hash_entry *) 0 - 1;
5029 dot_name[0] = '.';
5030 memcpy (dot_name + 1, name, len + 1);
5031 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5032 bfd_release (abfd, dot_name);
5033 return h;
5034 }
5035
5036 /* This function satisfies all old ABI object references to ".bar" if a
5037 new ABI object defines "bar". Well, at least, undefined dot symbols
5038 are made weak. This stops later archive searches from including an
5039 object if we already have a function descriptor definition. It also
5040 prevents the linker complaining about undefined symbols.
5041 We also check and correct mismatched symbol visibility here. The
5042 most restrictive visibility of the function descriptor and the
5043 function entry symbol is used. */
5044
5045 static bfd_boolean
5046 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5047 {
5048 struct ppc_link_hash_table *htab;
5049 struct ppc_link_hash_entry *fdh;
5050
5051 if (eh->elf.root.type == bfd_link_hash_warning)
5052 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5053
5054 if (eh->elf.root.type == bfd_link_hash_indirect)
5055 return TRUE;
5056
5057 if (eh->elf.root.root.string[0] != '.')
5058 abort ();
5059
5060 htab = ppc_hash_table (info);
5061 if (htab == NULL)
5062 return FALSE;
5063
5064 fdh = lookup_fdh (eh, htab);
5065 if (fdh == NULL
5066 && !bfd_link_relocatable (info)
5067 && (eh->elf.root.type == bfd_link_hash_undefined
5068 || eh->elf.root.type == bfd_link_hash_undefweak)
5069 && eh->elf.ref_regular)
5070 {
5071 /* Make an undefined function descriptor sym, in order to
5072 pull in an --as-needed shared lib. Archives are handled
5073 elsewhere. */
5074 fdh = make_fdh (info, eh);
5075 if (fdh == NULL)
5076 return FALSE;
5077 }
5078
5079 if (fdh != NULL)
5080 {
5081 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5082 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5083
5084 /* Make both descriptor and entry symbol have the most
5085 constraining visibility of either symbol. */
5086 if (entry_vis < descr_vis)
5087 fdh->elf.other += entry_vis - descr_vis;
5088 else if (entry_vis > descr_vis)
5089 eh->elf.other += descr_vis - entry_vis;
5090
5091 /* Propagate reference flags from entry symbol to function
5092 descriptor symbol. */
5093 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5094 fdh->elf.ref_regular |= eh->elf.ref_regular;
5095 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5096
5097 if (!fdh->elf.forced_local
5098 && fdh->elf.dynindx == -1
5099 && fdh->elf.versioned != versioned_hidden
5100 && (bfd_link_dll (info)
5101 || fdh->elf.def_dynamic
5102 || fdh->elf.ref_dynamic)
5103 && (eh->elf.ref_regular
5104 || eh->elf.def_regular))
5105 {
5106 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5107 return FALSE;
5108 }
5109 }
5110
5111 return TRUE;
5112 }
5113
5114 /* Set up opd section info and abiversion for IBFD, and process list
5115 of dot-symbols we made in link_hash_newfunc. */
5116
5117 static bfd_boolean
5118 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5119 {
5120 struct ppc_link_hash_table *htab;
5121 struct ppc_link_hash_entry **p, *eh;
5122 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5123
5124 if (opd != NULL && opd->size != 0)
5125 {
5126 if (abiversion (ibfd) == 0)
5127 set_abiversion (ibfd, 1);
5128 else if (abiversion (ibfd) >= 2)
5129 {
5130 /* xgettext:c-format */
5131 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5132 " version %d\n"),
5133 ibfd, abiversion (ibfd));
5134 bfd_set_error (bfd_error_bad_value);
5135 return FALSE;
5136 }
5137
5138 if ((ibfd->flags & DYNAMIC) == 0
5139 && (opd->flags & SEC_RELOC) != 0
5140 && opd->reloc_count != 0
5141 && !bfd_is_abs_section (opd->output_section))
5142 {
5143 /* Garbage collection needs some extra help with .opd sections.
5144 We don't want to necessarily keep everything referenced by
5145 relocs in .opd, as that would keep all functions. Instead,
5146 if we reference an .opd symbol (a function descriptor), we
5147 want to keep the function code symbol's section. This is
5148 easy for global symbols, but for local syms we need to keep
5149 information about the associated function section. */
5150 bfd_size_type amt;
5151 asection **opd_sym_map;
5152
5153 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5154 opd_sym_map = bfd_zalloc (ibfd, amt);
5155 if (opd_sym_map == NULL)
5156 return FALSE;
5157 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5158 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5159 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5160 }
5161 }
5162
5163 if (!is_ppc64_elf (info->output_bfd))
5164 return TRUE;
5165 htab = ppc_hash_table (info);
5166 if (htab == NULL)
5167 return FALSE;
5168
5169 /* For input files without an explicit abiversion in e_flags
5170 we should have flagged any with symbol st_other bits set
5171 as ELFv1 and above flagged those with .opd as ELFv2.
5172 Set the output abiversion if not yet set, and for any input
5173 still ambiguous, take its abiversion from the output.
5174 Differences in ABI are reported later. */
5175 if (abiversion (info->output_bfd) == 0)
5176 set_abiversion (info->output_bfd, abiversion (ibfd));
5177 else if (abiversion (ibfd) == 0)
5178 set_abiversion (ibfd, abiversion (info->output_bfd));
5179
5180 p = &htab->dot_syms;
5181 while ((eh = *p) != NULL)
5182 {
5183 *p = NULL;
5184 if (&eh->elf == htab->elf.hgot)
5185 ;
5186 else if (htab->elf.hgot == NULL
5187 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5188 htab->elf.hgot = &eh->elf;
5189 else if (abiversion (ibfd) <= 1)
5190 {
5191 htab->need_func_desc_adj = 1;
5192 if (!add_symbol_adjust (eh, info))
5193 return FALSE;
5194 }
5195 p = &eh->u.next_dot_sym;
5196 }
5197 return TRUE;
5198 }
5199
5200 /* Undo hash table changes when an --as-needed input file is determined
5201 not to be needed. */
5202
5203 static bfd_boolean
5204 ppc64_elf_notice_as_needed (bfd *ibfd,
5205 struct bfd_link_info *info,
5206 enum notice_asneeded_action act)
5207 {
5208 if (act == notice_not_needed)
5209 {
5210 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5211
5212 if (htab == NULL)
5213 return FALSE;
5214
5215 htab->dot_syms = NULL;
5216 }
5217 return _bfd_elf_notice_as_needed (ibfd, info, act);
5218 }
5219
5220 /* If --just-symbols against a final linked binary, then assume we need
5221 toc adjusting stubs when calling functions defined there. */
5222
5223 static void
5224 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5225 {
5226 if ((sec->flags & SEC_CODE) != 0
5227 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5228 && is_ppc64_elf (sec->owner))
5229 {
5230 if (abiversion (sec->owner) >= 2
5231 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5232 sec->has_toc_reloc = 1;
5233 }
5234 _bfd_elf_link_just_syms (sec, info);
5235 }
5236
5237 static struct plt_entry **
5238 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5239 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5240 {
5241 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5242 struct plt_entry **local_plt;
5243 unsigned char *local_got_tls_masks;
5244
5245 if (local_got_ents == NULL)
5246 {
5247 bfd_size_type size = symtab_hdr->sh_info;
5248
5249 size *= (sizeof (*local_got_ents)
5250 + sizeof (*local_plt)
5251 + sizeof (*local_got_tls_masks));
5252 local_got_ents = bfd_zalloc (abfd, size);
5253 if (local_got_ents == NULL)
5254 return NULL;
5255 elf_local_got_ents (abfd) = local_got_ents;
5256 }
5257
5258 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5259 {
5260 struct got_entry *ent;
5261
5262 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5263 if (ent->addend == r_addend
5264 && ent->owner == abfd
5265 && ent->tls_type == tls_type)
5266 break;
5267 if (ent == NULL)
5268 {
5269 bfd_size_type amt = sizeof (*ent);
5270 ent = bfd_alloc (abfd, amt);
5271 if (ent == NULL)
5272 return FALSE;
5273 ent->next = local_got_ents[r_symndx];
5274 ent->addend = r_addend;
5275 ent->owner = abfd;
5276 ent->tls_type = tls_type;
5277 ent->is_indirect = FALSE;
5278 ent->got.refcount = 0;
5279 local_got_ents[r_symndx] = ent;
5280 }
5281 ent->got.refcount += 1;
5282 }
5283
5284 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5285 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5286 local_got_tls_masks[r_symndx] |= tls_type;
5287
5288 return local_plt + r_symndx;
5289 }
5290
5291 static bfd_boolean
5292 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5293 {
5294 struct plt_entry *ent;
5295
5296 for (ent = *plist; ent != NULL; ent = ent->next)
5297 if (ent->addend == addend)
5298 break;
5299 if (ent == NULL)
5300 {
5301 bfd_size_type amt = sizeof (*ent);
5302 ent = bfd_alloc (abfd, amt);
5303 if (ent == NULL)
5304 return FALSE;
5305 ent->next = *plist;
5306 ent->addend = addend;
5307 ent->plt.refcount = 0;
5308 *plist = ent;
5309 }
5310 ent->plt.refcount += 1;
5311 return TRUE;
5312 }
5313
5314 static bfd_boolean
5315 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5316 {
5317 return (r_type == R_PPC64_REL24
5318 || r_type == R_PPC64_REL14
5319 || r_type == R_PPC64_REL14_BRTAKEN
5320 || r_type == R_PPC64_REL14_BRNTAKEN
5321 || r_type == R_PPC64_ADDR24
5322 || r_type == R_PPC64_ADDR14
5323 || r_type == R_PPC64_ADDR14_BRTAKEN
5324 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5325 }
5326
5327 /* Look through the relocs for a section during the first phase, and
5328 calculate needed space in the global offset table, procedure
5329 linkage table, and dynamic reloc sections. */
5330
5331 static bfd_boolean
5332 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5333 asection *sec, const Elf_Internal_Rela *relocs)
5334 {
5335 struct ppc_link_hash_table *htab;
5336 Elf_Internal_Shdr *symtab_hdr;
5337 struct elf_link_hash_entry **sym_hashes;
5338 const Elf_Internal_Rela *rel;
5339 const Elf_Internal_Rela *rel_end;
5340 asection *sreloc;
5341 asection **opd_sym_map;
5342 struct elf_link_hash_entry *tga, *dottga;
5343
5344 if (bfd_link_relocatable (info))
5345 return TRUE;
5346
5347 /* Don't do anything special with non-loaded, non-alloced sections.
5348 In particular, any relocs in such sections should not affect GOT
5349 and PLT reference counting (ie. we don't allow them to create GOT
5350 or PLT entries), there's no possibility or desire to optimize TLS
5351 relocs, and there's not much point in propagating relocs to shared
5352 libs that the dynamic linker won't relocate. */
5353 if ((sec->flags & SEC_ALLOC) == 0)
5354 return TRUE;
5355
5356 BFD_ASSERT (is_ppc64_elf (abfd));
5357
5358 htab = ppc_hash_table (info);
5359 if (htab == NULL)
5360 return FALSE;
5361
5362 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5363 FALSE, FALSE, TRUE);
5364 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5365 FALSE, FALSE, TRUE);
5366 symtab_hdr = &elf_symtab_hdr (abfd);
5367 sym_hashes = elf_sym_hashes (abfd);
5368 sreloc = NULL;
5369 opd_sym_map = NULL;
5370 if (ppc64_elf_section_data (sec) != NULL
5371 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5372 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5373
5374 rel_end = relocs + sec->reloc_count;
5375 for (rel = relocs; rel < rel_end; rel++)
5376 {
5377 unsigned long r_symndx;
5378 struct elf_link_hash_entry *h;
5379 enum elf_ppc64_reloc_type r_type;
5380 int tls_type;
5381 struct _ppc64_elf_section_data *ppc64_sec;
5382 struct plt_entry **ifunc, **plt_list;
5383
5384 r_symndx = ELF64_R_SYM (rel->r_info);
5385 if (r_symndx < symtab_hdr->sh_info)
5386 h = NULL;
5387 else
5388 {
5389 struct ppc_link_hash_entry *eh;
5390
5391 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5392 h = elf_follow_link (h);
5393 eh = (struct ppc_link_hash_entry *) h;
5394
5395 /* PR15323, ref flags aren't set for references in the same
5396 object. */
5397 h->root.non_ir_ref = 1;
5398 if (eh->is_func && eh->oh != NULL)
5399 eh->oh->elf.root.non_ir_ref = 1;
5400
5401 if (h == htab->elf.hgot)
5402 sec->has_toc_reloc = 1;
5403 }
5404
5405 tls_type = 0;
5406 ifunc = NULL;
5407 if (h != NULL)
5408 {
5409 if (h->type == STT_GNU_IFUNC)
5410 {
5411 h->needs_plt = 1;
5412 ifunc = &h->plt.plist;
5413 }
5414 }
5415 else
5416 {
5417 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5418 abfd, r_symndx);
5419 if (isym == NULL)
5420 return FALSE;
5421
5422 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5423 {
5424 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5425 rel->r_addend, PLT_IFUNC);
5426 if (ifunc == NULL)
5427 return FALSE;
5428 }
5429 }
5430
5431 r_type = ELF64_R_TYPE (rel->r_info);
5432 switch (r_type)
5433 {
5434 case R_PPC64_TLSGD:
5435 case R_PPC64_TLSLD:
5436 /* These special tls relocs tie a call to __tls_get_addr with
5437 its parameter symbol. */
5438 break;
5439
5440 case R_PPC64_GOT_TLSLD16:
5441 case R_PPC64_GOT_TLSLD16_LO:
5442 case R_PPC64_GOT_TLSLD16_HI:
5443 case R_PPC64_GOT_TLSLD16_HA:
5444 tls_type = TLS_TLS | TLS_LD;
5445 goto dogottls;
5446
5447 case R_PPC64_GOT_TLSGD16:
5448 case R_PPC64_GOT_TLSGD16_LO:
5449 case R_PPC64_GOT_TLSGD16_HI:
5450 case R_PPC64_GOT_TLSGD16_HA:
5451 tls_type = TLS_TLS | TLS_GD;
5452 goto dogottls;
5453
5454 case R_PPC64_GOT_TPREL16_DS:
5455 case R_PPC64_GOT_TPREL16_LO_DS:
5456 case R_PPC64_GOT_TPREL16_HI:
5457 case R_PPC64_GOT_TPREL16_HA:
5458 if (bfd_link_pic (info))
5459 info->flags |= DF_STATIC_TLS;
5460 tls_type = TLS_TLS | TLS_TPREL;
5461 goto dogottls;
5462
5463 case R_PPC64_GOT_DTPREL16_DS:
5464 case R_PPC64_GOT_DTPREL16_LO_DS:
5465 case R_PPC64_GOT_DTPREL16_HI:
5466 case R_PPC64_GOT_DTPREL16_HA:
5467 tls_type = TLS_TLS | TLS_DTPREL;
5468 dogottls:
5469 sec->has_tls_reloc = 1;
5470 /* Fall through */
5471
5472 case R_PPC64_GOT16:
5473 case R_PPC64_GOT16_DS:
5474 case R_PPC64_GOT16_HA:
5475 case R_PPC64_GOT16_HI:
5476 case R_PPC64_GOT16_LO:
5477 case R_PPC64_GOT16_LO_DS:
5478 /* This symbol requires a global offset table entry. */
5479 sec->has_toc_reloc = 1;
5480 if (r_type == R_PPC64_GOT_TLSLD16
5481 || r_type == R_PPC64_GOT_TLSGD16
5482 || r_type == R_PPC64_GOT_TPREL16_DS
5483 || r_type == R_PPC64_GOT_DTPREL16_DS
5484 || r_type == R_PPC64_GOT16
5485 || r_type == R_PPC64_GOT16_DS)
5486 {
5487 htab->do_multi_toc = 1;
5488 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5489 }
5490
5491 if (ppc64_elf_tdata (abfd)->got == NULL
5492 && !create_got_section (abfd, info))
5493 return FALSE;
5494
5495 if (h != NULL)
5496 {
5497 struct ppc_link_hash_entry *eh;
5498 struct got_entry *ent;
5499
5500 eh = (struct ppc_link_hash_entry *) h;
5501 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5502 if (ent->addend == rel->r_addend
5503 && ent->owner == abfd
5504 && ent->tls_type == tls_type)
5505 break;
5506 if (ent == NULL)
5507 {
5508 bfd_size_type amt = sizeof (*ent);
5509 ent = bfd_alloc (abfd, amt);
5510 if (ent == NULL)
5511 return FALSE;
5512 ent->next = eh->elf.got.glist;
5513 ent->addend = rel->r_addend;
5514 ent->owner = abfd;
5515 ent->tls_type = tls_type;
5516 ent->is_indirect = FALSE;
5517 ent->got.refcount = 0;
5518 eh->elf.got.glist = ent;
5519 }
5520 ent->got.refcount += 1;
5521 eh->tls_mask |= tls_type;
5522 }
5523 else
5524 /* This is a global offset table entry for a local symbol. */
5525 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5526 rel->r_addend, tls_type))
5527 return FALSE;
5528
5529 /* We may also need a plt entry if the symbol turns out to be
5530 an ifunc. */
5531 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5532 {
5533 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5534 return FALSE;
5535 }
5536 break;
5537
5538 case R_PPC64_PLT16_HA:
5539 case R_PPC64_PLT16_HI:
5540 case R_PPC64_PLT16_LO:
5541 case R_PPC64_PLT32:
5542 case R_PPC64_PLT64:
5543 /* This symbol requires a procedure linkage table entry. */
5544 plt_list = ifunc;
5545 if (h != NULL)
5546 {
5547 h->needs_plt = 1;
5548 if (h->root.root.string[0] == '.'
5549 && h->root.root.string[1] != '\0')
5550 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5551 plt_list = &h->plt.plist;
5552 }
5553 if (plt_list == NULL)
5554 {
5555 /* It does not make sense to have a procedure linkage
5556 table entry for a non-ifunc local symbol. */
5557 info->callbacks->einfo
5558 /* xgettext:c-format */
5559 (_("%P: %H: %s reloc against local symbol\n"),
5560 abfd, sec, rel->r_offset,
5561 ppc64_elf_howto_table[r_type]->name);
5562 bfd_set_error (bfd_error_bad_value);
5563 return FALSE;
5564 }
5565 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5566 return FALSE;
5567 break;
5568
5569 /* The following relocations don't need to propagate the
5570 relocation if linking a shared object since they are
5571 section relative. */
5572 case R_PPC64_SECTOFF:
5573 case R_PPC64_SECTOFF_LO:
5574 case R_PPC64_SECTOFF_HI:
5575 case R_PPC64_SECTOFF_HA:
5576 case R_PPC64_SECTOFF_DS:
5577 case R_PPC64_SECTOFF_LO_DS:
5578 case R_PPC64_DTPREL16:
5579 case R_PPC64_DTPREL16_LO:
5580 case R_PPC64_DTPREL16_HI:
5581 case R_PPC64_DTPREL16_HA:
5582 case R_PPC64_DTPREL16_DS:
5583 case R_PPC64_DTPREL16_LO_DS:
5584 case R_PPC64_DTPREL16_HIGH:
5585 case R_PPC64_DTPREL16_HIGHA:
5586 case R_PPC64_DTPREL16_HIGHER:
5587 case R_PPC64_DTPREL16_HIGHERA:
5588 case R_PPC64_DTPREL16_HIGHEST:
5589 case R_PPC64_DTPREL16_HIGHESTA:
5590 break;
5591
5592 /* Nor do these. */
5593 case R_PPC64_REL16:
5594 case R_PPC64_REL16_LO:
5595 case R_PPC64_REL16_HI:
5596 case R_PPC64_REL16_HA:
5597 case R_PPC64_REL16DX_HA:
5598 break;
5599
5600 /* Not supported as a dynamic relocation. */
5601 case R_PPC64_ADDR64_LOCAL:
5602 if (bfd_link_pic (info))
5603 {
5604 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5605 ppc_howto_init ();
5606 /* xgettext:c-format */
5607 info->callbacks->einfo (_("%P: %H: %s reloc unsupported "
5608 "in shared libraries and PIEs.\n"),
5609 abfd, sec, rel->r_offset,
5610 ppc64_elf_howto_table[r_type]->name);
5611 bfd_set_error (bfd_error_bad_value);
5612 return FALSE;
5613 }
5614 break;
5615
5616 case R_PPC64_TOC16:
5617 case R_PPC64_TOC16_DS:
5618 htab->do_multi_toc = 1;
5619 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5620 /* Fall through. */
5621 case R_PPC64_TOC16_LO:
5622 case R_PPC64_TOC16_HI:
5623 case R_PPC64_TOC16_HA:
5624 case R_PPC64_TOC16_LO_DS:
5625 sec->has_toc_reloc = 1;
5626 break;
5627
5628 /* Marker reloc. */
5629 case R_PPC64_ENTRY:
5630 break;
5631
5632 /* This relocation describes the C++ object vtable hierarchy.
5633 Reconstruct it for later use during GC. */
5634 case R_PPC64_GNU_VTINHERIT:
5635 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5636 return FALSE;
5637 break;
5638
5639 /* This relocation describes which C++ vtable entries are actually
5640 used. Record for later use during GC. */
5641 case R_PPC64_GNU_VTENTRY:
5642 BFD_ASSERT (h != NULL);
5643 if (h != NULL
5644 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5645 return FALSE;
5646 break;
5647
5648 case R_PPC64_REL14:
5649 case R_PPC64_REL14_BRTAKEN:
5650 case R_PPC64_REL14_BRNTAKEN:
5651 {
5652 asection *dest = NULL;
5653
5654 /* Heuristic: If jumping outside our section, chances are
5655 we are going to need a stub. */
5656 if (h != NULL)
5657 {
5658 /* If the sym is weak it may be overridden later, so
5659 don't assume we know where a weak sym lives. */
5660 if (h->root.type == bfd_link_hash_defined)
5661 dest = h->root.u.def.section;
5662 }
5663 else
5664 {
5665 Elf_Internal_Sym *isym;
5666
5667 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5668 abfd, r_symndx);
5669 if (isym == NULL)
5670 return FALSE;
5671
5672 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5673 }
5674
5675 if (dest != sec)
5676 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5677 }
5678 /* Fall through. */
5679
5680 case R_PPC64_REL24:
5681 plt_list = ifunc;
5682 if (h != NULL)
5683 {
5684 h->needs_plt = 1;
5685 if (h->root.root.string[0] == '.'
5686 && h->root.root.string[1] != '\0')
5687 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5688
5689 if (h == tga || h == dottga)
5690 {
5691 sec->has_tls_reloc = 1;
5692 if (rel != relocs
5693 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5694 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5695 /* We have a new-style __tls_get_addr call with
5696 a marker reloc. */
5697 ;
5698 else
5699 /* Mark this section as having an old-style call. */
5700 sec->has_tls_get_addr_call = 1;
5701 }
5702 plt_list = &h->plt.plist;
5703 }
5704
5705 /* We may need a .plt entry if the function this reloc
5706 refers to is in a shared lib. */
5707 if (plt_list
5708 && !update_plt_info (abfd, plt_list, rel->r_addend))
5709 return FALSE;
5710 break;
5711
5712 case R_PPC64_ADDR14:
5713 case R_PPC64_ADDR14_BRNTAKEN:
5714 case R_PPC64_ADDR14_BRTAKEN:
5715 case R_PPC64_ADDR24:
5716 goto dodyn;
5717
5718 case R_PPC64_TPREL64:
5719 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5720 if (bfd_link_pic (info))
5721 info->flags |= DF_STATIC_TLS;
5722 goto dotlstoc;
5723
5724 case R_PPC64_DTPMOD64:
5725 if (rel + 1 < rel_end
5726 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5727 && rel[1].r_offset == rel->r_offset + 8)
5728 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5729 else
5730 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5731 goto dotlstoc;
5732
5733 case R_PPC64_DTPREL64:
5734 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5735 if (rel != relocs
5736 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5737 && rel[-1].r_offset == rel->r_offset - 8)
5738 /* This is the second reloc of a dtpmod, dtprel pair.
5739 Don't mark with TLS_DTPREL. */
5740 goto dodyn;
5741
5742 dotlstoc:
5743 sec->has_tls_reloc = 1;
5744 if (h != NULL)
5745 {
5746 struct ppc_link_hash_entry *eh;
5747 eh = (struct ppc_link_hash_entry *) h;
5748 eh->tls_mask |= tls_type;
5749 }
5750 else
5751 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5752 rel->r_addend, tls_type))
5753 return FALSE;
5754
5755 ppc64_sec = ppc64_elf_section_data (sec);
5756 if (ppc64_sec->sec_type != sec_toc)
5757 {
5758 bfd_size_type amt;
5759
5760 /* One extra to simplify get_tls_mask. */
5761 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5762 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5763 if (ppc64_sec->u.toc.symndx == NULL)
5764 return FALSE;
5765 amt = sec->size * sizeof (bfd_vma) / 8;
5766 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5767 if (ppc64_sec->u.toc.add == NULL)
5768 return FALSE;
5769 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5770 ppc64_sec->sec_type = sec_toc;
5771 }
5772 BFD_ASSERT (rel->r_offset % 8 == 0);
5773 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5774 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5775
5776 /* Mark the second slot of a GD or LD entry.
5777 -1 to indicate GD and -2 to indicate LD. */
5778 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5779 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5780 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5781 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5782 goto dodyn;
5783
5784 case R_PPC64_TPREL16:
5785 case R_PPC64_TPREL16_LO:
5786 case R_PPC64_TPREL16_HI:
5787 case R_PPC64_TPREL16_HA:
5788 case R_PPC64_TPREL16_DS:
5789 case R_PPC64_TPREL16_LO_DS:
5790 case R_PPC64_TPREL16_HIGH:
5791 case R_PPC64_TPREL16_HIGHA:
5792 case R_PPC64_TPREL16_HIGHER:
5793 case R_PPC64_TPREL16_HIGHERA:
5794 case R_PPC64_TPREL16_HIGHEST:
5795 case R_PPC64_TPREL16_HIGHESTA:
5796 if (bfd_link_pic (info))
5797 {
5798 info->flags |= DF_STATIC_TLS;
5799 goto dodyn;
5800 }
5801 break;
5802
5803 case R_PPC64_ADDR64:
5804 if (opd_sym_map != NULL
5805 && rel + 1 < rel_end
5806 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5807 {
5808 if (h != NULL)
5809 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5810 else
5811 {
5812 asection *s;
5813 Elf_Internal_Sym *isym;
5814
5815 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5816 abfd, r_symndx);
5817 if (isym == NULL)
5818 return FALSE;
5819
5820 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5821 if (s != NULL && s != sec)
5822 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5823 }
5824 }
5825 /* Fall through. */
5826
5827 case R_PPC64_ADDR16:
5828 case R_PPC64_ADDR16_DS:
5829 case R_PPC64_ADDR16_HA:
5830 case R_PPC64_ADDR16_HI:
5831 case R_PPC64_ADDR16_HIGH:
5832 case R_PPC64_ADDR16_HIGHA:
5833 case R_PPC64_ADDR16_HIGHER:
5834 case R_PPC64_ADDR16_HIGHERA:
5835 case R_PPC64_ADDR16_HIGHEST:
5836 case R_PPC64_ADDR16_HIGHESTA:
5837 case R_PPC64_ADDR16_LO:
5838 case R_PPC64_ADDR16_LO_DS:
5839 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5840 && rel->r_addend == 0)
5841 {
5842 /* We may need a .plt entry if this reloc refers to a
5843 function in a shared lib. */
5844 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5845 return FALSE;
5846 h->pointer_equality_needed = 1;
5847 }
5848 /* Fall through. */
5849
5850 case R_PPC64_REL30:
5851 case R_PPC64_REL32:
5852 case R_PPC64_REL64:
5853 case R_PPC64_ADDR32:
5854 case R_PPC64_UADDR16:
5855 case R_PPC64_UADDR32:
5856 case R_PPC64_UADDR64:
5857 case R_PPC64_TOC:
5858 if (h != NULL && !bfd_link_pic (info))
5859 /* We may need a copy reloc. */
5860 h->non_got_ref = 1;
5861
5862 /* Don't propagate .opd relocs. */
5863 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5864 break;
5865
5866 /* If we are creating a shared library, and this is a reloc
5867 against a global symbol, or a non PC relative reloc
5868 against a local symbol, then we need to copy the reloc
5869 into the shared library. However, if we are linking with
5870 -Bsymbolic, we do not need to copy a reloc against a
5871 global symbol which is defined in an object we are
5872 including in the link (i.e., DEF_REGULAR is set). At
5873 this point we have not seen all the input files, so it is
5874 possible that DEF_REGULAR is not set now but will be set
5875 later (it is never cleared). In case of a weak definition,
5876 DEF_REGULAR may be cleared later by a strong definition in
5877 a shared library. We account for that possibility below by
5878 storing information in the dyn_relocs field of the hash
5879 table entry. A similar situation occurs when creating
5880 shared libraries and symbol visibility changes render the
5881 symbol local.
5882
5883 If on the other hand, we are creating an executable, we
5884 may need to keep relocations for symbols satisfied by a
5885 dynamic library if we manage to avoid copy relocs for the
5886 symbol. */
5887 dodyn:
5888 if ((bfd_link_pic (info)
5889 && (must_be_dyn_reloc (info, r_type)
5890 || (h != NULL
5891 && (!SYMBOLIC_BIND (info, h)
5892 || h->root.type == bfd_link_hash_defweak
5893 || !h->def_regular))))
5894 || (ELIMINATE_COPY_RELOCS
5895 && !bfd_link_pic (info)
5896 && h != NULL
5897 && (h->root.type == bfd_link_hash_defweak
5898 || !h->def_regular))
5899 || (!bfd_link_pic (info)
5900 && ifunc != NULL))
5901 {
5902 /* We must copy these reloc types into the output file.
5903 Create a reloc section in dynobj and make room for
5904 this reloc. */
5905 if (sreloc == NULL)
5906 {
5907 sreloc = _bfd_elf_make_dynamic_reloc_section
5908 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5909
5910 if (sreloc == NULL)
5911 return FALSE;
5912 }
5913
5914 /* If this is a global symbol, we count the number of
5915 relocations we need for this symbol. */
5916 if (h != NULL)
5917 {
5918 struct elf_dyn_relocs *p;
5919 struct elf_dyn_relocs **head;
5920
5921 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5922 p = *head;
5923 if (p == NULL || p->sec != sec)
5924 {
5925 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5926 if (p == NULL)
5927 return FALSE;
5928 p->next = *head;
5929 *head = p;
5930 p->sec = sec;
5931 p->count = 0;
5932 p->pc_count = 0;
5933 }
5934 p->count += 1;
5935 if (!must_be_dyn_reloc (info, r_type))
5936 p->pc_count += 1;
5937 }
5938 else
5939 {
5940 /* Track dynamic relocs needed for local syms too.
5941 We really need local syms available to do this
5942 easily. Oh well. */
5943 struct ppc_dyn_relocs *p;
5944 struct ppc_dyn_relocs **head;
5945 bfd_boolean is_ifunc;
5946 asection *s;
5947 void *vpp;
5948 Elf_Internal_Sym *isym;
5949
5950 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5951 abfd, r_symndx);
5952 if (isym == NULL)
5953 return FALSE;
5954
5955 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5956 if (s == NULL)
5957 s = sec;
5958
5959 vpp = &elf_section_data (s)->local_dynrel;
5960 head = (struct ppc_dyn_relocs **) vpp;
5961 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5962 p = *head;
5963 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5964 p = p->next;
5965 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5966 {
5967 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5968 if (p == NULL)
5969 return FALSE;
5970 p->next = *head;
5971 *head = p;
5972 p->sec = sec;
5973 p->ifunc = is_ifunc;
5974 p->count = 0;
5975 }
5976 p->count += 1;
5977 }
5978 }
5979 break;
5980
5981 default:
5982 break;
5983 }
5984 }
5985
5986 return TRUE;
5987 }
5988
5989 /* Merge backend specific data from an object file to the output
5990 object file when linking. */
5991
5992 static bfd_boolean
5993 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5994 {
5995 bfd *obfd = info->output_bfd;
5996 unsigned long iflags, oflags;
5997
5998 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5999 return TRUE;
6000
6001 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6002 return TRUE;
6003
6004 if (!_bfd_generic_verify_endian_match (ibfd, info))
6005 return FALSE;
6006
6007 iflags = elf_elfheader (ibfd)->e_flags;
6008 oflags = elf_elfheader (obfd)->e_flags;
6009
6010 if (iflags & ~EF_PPC64_ABI)
6011 {
6012 _bfd_error_handler
6013 /* xgettext:c-format */
6014 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6015 bfd_set_error (bfd_error_bad_value);
6016 return FALSE;
6017 }
6018 else if (iflags != oflags && iflags != 0)
6019 {
6020 _bfd_error_handler
6021 /* xgettext:c-format */
6022 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6023 ibfd, iflags, oflags);
6024 bfd_set_error (bfd_error_bad_value);
6025 return FALSE;
6026 }
6027
6028 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6029
6030 /* Merge Tag_compatibility attributes and any common GNU ones. */
6031 _bfd_elf_merge_object_attributes (ibfd, info);
6032
6033 return TRUE;
6034 }
6035
6036 static bfd_boolean
6037 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6038 {
6039 /* Print normal ELF private data. */
6040 _bfd_elf_print_private_bfd_data (abfd, ptr);
6041
6042 if (elf_elfheader (abfd)->e_flags != 0)
6043 {
6044 FILE *file = ptr;
6045
6046 fprintf (file, _("private flags = 0x%lx:"),
6047 elf_elfheader (abfd)->e_flags);
6048
6049 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6050 fprintf (file, _(" [abiv%ld]"),
6051 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6052 fputc ('\n', file);
6053 }
6054
6055 return TRUE;
6056 }
6057
6058 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6059 of the code entry point, and its section, which must be in the same
6060 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6061
6062 static bfd_vma
6063 opd_entry_value (asection *opd_sec,
6064 bfd_vma offset,
6065 asection **code_sec,
6066 bfd_vma *code_off,
6067 bfd_boolean in_code_sec)
6068 {
6069 bfd *opd_bfd = opd_sec->owner;
6070 Elf_Internal_Rela *relocs;
6071 Elf_Internal_Rela *lo, *hi, *look;
6072 bfd_vma val;
6073
6074 /* No relocs implies we are linking a --just-symbols object, or looking
6075 at a final linked executable with addr2line or somesuch. */
6076 if (opd_sec->reloc_count == 0)
6077 {
6078 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6079
6080 if (contents == NULL)
6081 {
6082 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6083 return (bfd_vma) -1;
6084 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6085 }
6086
6087 /* PR 17512: file: 64b9dfbb. */
6088 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6089 return (bfd_vma) -1;
6090
6091 val = bfd_get_64 (opd_bfd, contents + offset);
6092 if (code_sec != NULL)
6093 {
6094 asection *sec, *likely = NULL;
6095
6096 if (in_code_sec)
6097 {
6098 sec = *code_sec;
6099 if (sec->vma <= val
6100 && val < sec->vma + sec->size)
6101 likely = sec;
6102 else
6103 val = -1;
6104 }
6105 else
6106 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6107 if (sec->vma <= val
6108 && (sec->flags & SEC_LOAD) != 0
6109 && (sec->flags & SEC_ALLOC) != 0)
6110 likely = sec;
6111 if (likely != NULL)
6112 {
6113 *code_sec = likely;
6114 if (code_off != NULL)
6115 *code_off = val - likely->vma;
6116 }
6117 }
6118 return val;
6119 }
6120
6121 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6122
6123 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6124 if (relocs == NULL)
6125 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6126 /* PR 17512: file: df8e1fd6. */
6127 if (relocs == NULL)
6128 return (bfd_vma) -1;
6129
6130 /* Go find the opd reloc at the sym address. */
6131 lo = relocs;
6132 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6133 val = (bfd_vma) -1;
6134 while (lo < hi)
6135 {
6136 look = lo + (hi - lo) / 2;
6137 if (look->r_offset < offset)
6138 lo = look + 1;
6139 else if (look->r_offset > offset)
6140 hi = look;
6141 else
6142 {
6143 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6144
6145 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6146 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6147 {
6148 unsigned long symndx = ELF64_R_SYM (look->r_info);
6149 asection *sec = NULL;
6150
6151 if (symndx >= symtab_hdr->sh_info
6152 && elf_sym_hashes (opd_bfd) != NULL)
6153 {
6154 struct elf_link_hash_entry **sym_hashes;
6155 struct elf_link_hash_entry *rh;
6156
6157 sym_hashes = elf_sym_hashes (opd_bfd);
6158 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6159 if (rh != NULL)
6160 {
6161 rh = elf_follow_link (rh);
6162 if (rh->root.type != bfd_link_hash_defined
6163 && rh->root.type != bfd_link_hash_defweak)
6164 break;
6165 if (rh->root.u.def.section->owner == opd_bfd)
6166 {
6167 val = rh->root.u.def.value;
6168 sec = rh->root.u.def.section;
6169 }
6170 }
6171 }
6172
6173 if (sec == NULL)
6174 {
6175 Elf_Internal_Sym *sym;
6176
6177 if (symndx < symtab_hdr->sh_info)
6178 {
6179 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6180 if (sym == NULL)
6181 {
6182 size_t symcnt = symtab_hdr->sh_info;
6183 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6184 symcnt, 0,
6185 NULL, NULL, NULL);
6186 if (sym == NULL)
6187 break;
6188 symtab_hdr->contents = (bfd_byte *) sym;
6189 }
6190 sym += symndx;
6191 }
6192 else
6193 {
6194 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6195 1, symndx,
6196 NULL, NULL, NULL);
6197 if (sym == NULL)
6198 break;
6199 }
6200 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6201 if (sec == NULL)
6202 break;
6203 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6204 val = sym->st_value;
6205 }
6206
6207 val += look->r_addend;
6208 if (code_off != NULL)
6209 *code_off = val;
6210 if (code_sec != NULL)
6211 {
6212 if (in_code_sec && *code_sec != sec)
6213 return -1;
6214 else
6215 *code_sec = sec;
6216 }
6217 if (sec->output_section != NULL)
6218 val += sec->output_section->vma + sec->output_offset;
6219 }
6220 break;
6221 }
6222 }
6223
6224 return val;
6225 }
6226
6227 /* If the ELF symbol SYM might be a function in SEC, return the
6228 function size and set *CODE_OFF to the function's entry point,
6229 otherwise return zero. */
6230
6231 static bfd_size_type
6232 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6233 bfd_vma *code_off)
6234 {
6235 bfd_size_type size;
6236
6237 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6238 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6239 return 0;
6240
6241 size = 0;
6242 if (!(sym->flags & BSF_SYNTHETIC))
6243 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6244
6245 if (strcmp (sym->section->name, ".opd") == 0)
6246 {
6247 struct _opd_sec_data *opd = get_opd_info (sym->section);
6248 bfd_vma symval = sym->value;
6249
6250 if (opd != NULL
6251 && opd->adjust != NULL
6252 && elf_section_data (sym->section)->relocs != NULL)
6253 {
6254 /* opd_entry_value will use cached relocs that have been
6255 adjusted, but with raw symbols. That means both local
6256 and global symbols need adjusting. */
6257 long adjust = opd->adjust[OPD_NDX (symval)];
6258 if (adjust == -1)
6259 return 0;
6260 symval += adjust;
6261 }
6262
6263 if (opd_entry_value (sym->section, symval,
6264 &sec, code_off, TRUE) == (bfd_vma) -1)
6265 return 0;
6266 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6267 symbol. This size has nothing to do with the code size of the
6268 function, which is what we're supposed to return, but the
6269 code size isn't available without looking up the dot-sym.
6270 However, doing that would be a waste of time particularly
6271 since elf_find_function will look at the dot-sym anyway.
6272 Now, elf_find_function will keep the largest size of any
6273 function sym found at the code address of interest, so return
6274 1 here to avoid it incorrectly caching a larger function size
6275 for a small function. This does mean we return the wrong
6276 size for a new-ABI function of size 24, but all that does is
6277 disable caching for such functions. */
6278 if (size == 24)
6279 size = 1;
6280 }
6281 else
6282 {
6283 if (sym->section != sec)
6284 return 0;
6285 *code_off = sym->value;
6286 }
6287 if (size == 0)
6288 size = 1;
6289 return size;
6290 }
6291
6292 /* Return true if symbol is defined in a regular object file. */
6293
6294 static bfd_boolean
6295 is_static_defined (struct elf_link_hash_entry *h)
6296 {
6297 return ((h->root.type == bfd_link_hash_defined
6298 || h->root.type == bfd_link_hash_defweak)
6299 && h->root.u.def.section != NULL
6300 && h->root.u.def.section->output_section != NULL);
6301 }
6302
6303 /* If FDH is a function descriptor symbol, return the associated code
6304 entry symbol if it is defined. Return NULL otherwise. */
6305
6306 static struct ppc_link_hash_entry *
6307 defined_code_entry (struct ppc_link_hash_entry *fdh)
6308 {
6309 if (fdh->is_func_descriptor)
6310 {
6311 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6312 if (fh->elf.root.type == bfd_link_hash_defined
6313 || fh->elf.root.type == bfd_link_hash_defweak)
6314 return fh;
6315 }
6316 return NULL;
6317 }
6318
6319 /* If FH is a function code entry symbol, return the associated
6320 function descriptor symbol if it is defined. Return NULL otherwise. */
6321
6322 static struct ppc_link_hash_entry *
6323 defined_func_desc (struct ppc_link_hash_entry *fh)
6324 {
6325 if (fh->oh != NULL
6326 && fh->oh->is_func_descriptor)
6327 {
6328 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6329 if (fdh->elf.root.type == bfd_link_hash_defined
6330 || fdh->elf.root.type == bfd_link_hash_defweak)
6331 return fdh;
6332 }
6333 return NULL;
6334 }
6335
6336 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6337
6338 /* Garbage collect sections, after first dealing with dot-symbols. */
6339
6340 static bfd_boolean
6341 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6342 {
6343 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6344
6345 if (htab != NULL && htab->need_func_desc_adj)
6346 {
6347 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6348 htab->need_func_desc_adj = 0;
6349 }
6350 return bfd_elf_gc_sections (abfd, info);
6351 }
6352
6353 /* Mark all our entry sym sections, both opd and code section. */
6354
6355 static void
6356 ppc64_elf_gc_keep (struct bfd_link_info *info)
6357 {
6358 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6359 struct bfd_sym_chain *sym;
6360
6361 if (htab == NULL)
6362 return;
6363
6364 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6365 {
6366 struct ppc_link_hash_entry *eh, *fh;
6367 asection *sec;
6368
6369 eh = (struct ppc_link_hash_entry *)
6370 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6371 if (eh == NULL)
6372 continue;
6373 if (eh->elf.root.type != bfd_link_hash_defined
6374 && eh->elf.root.type != bfd_link_hash_defweak)
6375 continue;
6376
6377 fh = defined_code_entry (eh);
6378 if (fh != NULL)
6379 {
6380 sec = fh->elf.root.u.def.section;
6381 sec->flags |= SEC_KEEP;
6382 }
6383 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6384 && opd_entry_value (eh->elf.root.u.def.section,
6385 eh->elf.root.u.def.value,
6386 &sec, NULL, FALSE) != (bfd_vma) -1)
6387 sec->flags |= SEC_KEEP;
6388
6389 sec = eh->elf.root.u.def.section;
6390 sec->flags |= SEC_KEEP;
6391 }
6392 }
6393
6394 /* Mark sections containing dynamically referenced symbols. When
6395 building shared libraries, we must assume that any visible symbol is
6396 referenced. */
6397
6398 static bfd_boolean
6399 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6400 {
6401 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6402 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6403 struct ppc_link_hash_entry *fdh;
6404 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6405
6406 /* Dynamic linking info is on the func descriptor sym. */
6407 fdh = defined_func_desc (eh);
6408 if (fdh != NULL)
6409 eh = fdh;
6410
6411 if ((eh->elf.root.type == bfd_link_hash_defined
6412 || eh->elf.root.type == bfd_link_hash_defweak)
6413 && (eh->elf.ref_dynamic
6414 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6415 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6416 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6417 && (!bfd_link_executable (info)
6418 || info->export_dynamic
6419 || (eh->elf.dynamic
6420 && d != NULL
6421 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6422 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6423 || !bfd_hide_sym_by_version (info->version_info,
6424 eh->elf.root.root.string)))))
6425 {
6426 asection *code_sec;
6427 struct ppc_link_hash_entry *fh;
6428
6429 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6430
6431 /* Function descriptor syms cause the associated
6432 function code sym section to be marked. */
6433 fh = defined_code_entry (eh);
6434 if (fh != NULL)
6435 {
6436 code_sec = fh->elf.root.u.def.section;
6437 code_sec->flags |= SEC_KEEP;
6438 }
6439 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6440 && opd_entry_value (eh->elf.root.u.def.section,
6441 eh->elf.root.u.def.value,
6442 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6443 code_sec->flags |= SEC_KEEP;
6444 }
6445
6446 return TRUE;
6447 }
6448
6449 /* Return the section that should be marked against GC for a given
6450 relocation. */
6451
6452 static asection *
6453 ppc64_elf_gc_mark_hook (asection *sec,
6454 struct bfd_link_info *info,
6455 Elf_Internal_Rela *rel,
6456 struct elf_link_hash_entry *h,
6457 Elf_Internal_Sym *sym)
6458 {
6459 asection *rsec;
6460
6461 /* Syms return NULL if we're marking .opd, so we avoid marking all
6462 function sections, as all functions are referenced in .opd. */
6463 rsec = NULL;
6464 if (get_opd_info (sec) != NULL)
6465 return rsec;
6466
6467 if (h != NULL)
6468 {
6469 enum elf_ppc64_reloc_type r_type;
6470 struct ppc_link_hash_entry *eh, *fh, *fdh;
6471
6472 r_type = ELF64_R_TYPE (rel->r_info);
6473 switch (r_type)
6474 {
6475 case R_PPC64_GNU_VTINHERIT:
6476 case R_PPC64_GNU_VTENTRY:
6477 break;
6478
6479 default:
6480 switch (h->root.type)
6481 {
6482 case bfd_link_hash_defined:
6483 case bfd_link_hash_defweak:
6484 eh = (struct ppc_link_hash_entry *) h;
6485 fdh = defined_func_desc (eh);
6486 if (fdh != NULL)
6487 {
6488 /* -mcall-aixdesc code references the dot-symbol on
6489 a call reloc. Mark the function descriptor too
6490 against garbage collection. */
6491 fdh->elf.mark = 1;
6492 if (fdh->elf.u.weakdef != NULL)
6493 fdh->elf.u.weakdef->mark = 1;
6494 eh = fdh;
6495 }
6496
6497 /* Function descriptor syms cause the associated
6498 function code sym section to be marked. */
6499 fh = defined_code_entry (eh);
6500 if (fh != NULL)
6501 {
6502 /* They also mark their opd section. */
6503 eh->elf.root.u.def.section->gc_mark = 1;
6504
6505 rsec = fh->elf.root.u.def.section;
6506 }
6507 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6508 && opd_entry_value (eh->elf.root.u.def.section,
6509 eh->elf.root.u.def.value,
6510 &rsec, NULL, FALSE) != (bfd_vma) -1)
6511 eh->elf.root.u.def.section->gc_mark = 1;
6512 else
6513 rsec = h->root.u.def.section;
6514 break;
6515
6516 case bfd_link_hash_common:
6517 rsec = h->root.u.c.p->section;
6518 break;
6519
6520 default:
6521 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6522 }
6523 }
6524 }
6525 else
6526 {
6527 struct _opd_sec_data *opd;
6528
6529 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6530 opd = get_opd_info (rsec);
6531 if (opd != NULL && opd->func_sec != NULL)
6532 {
6533 rsec->gc_mark = 1;
6534
6535 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6536 }
6537 }
6538
6539 return rsec;
6540 }
6541
6542 /* Update the .got, .plt. and dynamic reloc reference counts for the
6543 section being removed. */
6544
6545 static bfd_boolean
6546 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6547 asection *sec, const Elf_Internal_Rela *relocs)
6548 {
6549 struct ppc_link_hash_table *htab;
6550 Elf_Internal_Shdr *symtab_hdr;
6551 struct elf_link_hash_entry **sym_hashes;
6552 struct got_entry **local_got_ents;
6553 const Elf_Internal_Rela *rel, *relend;
6554
6555 if (bfd_link_relocatable (info))
6556 return TRUE;
6557
6558 if ((sec->flags & SEC_ALLOC) == 0)
6559 return TRUE;
6560
6561 elf_section_data (sec)->local_dynrel = NULL;
6562
6563 htab = ppc_hash_table (info);
6564 if (htab == NULL)
6565 return FALSE;
6566
6567 symtab_hdr = &elf_symtab_hdr (abfd);
6568 sym_hashes = elf_sym_hashes (abfd);
6569 local_got_ents = elf_local_got_ents (abfd);
6570
6571 relend = relocs + sec->reloc_count;
6572 for (rel = relocs; rel < relend; rel++)
6573 {
6574 unsigned long r_symndx;
6575 enum elf_ppc64_reloc_type r_type;
6576 struct elf_link_hash_entry *h = NULL;
6577 struct plt_entry **plt_list;
6578 unsigned char tls_type = 0;
6579
6580 r_symndx = ELF64_R_SYM (rel->r_info);
6581 r_type = ELF64_R_TYPE (rel->r_info);
6582 if (r_symndx >= symtab_hdr->sh_info)
6583 {
6584 struct ppc_link_hash_entry *eh;
6585 struct elf_dyn_relocs **pp;
6586 struct elf_dyn_relocs *p;
6587
6588 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6589 h = elf_follow_link (h);
6590 eh = (struct ppc_link_hash_entry *) h;
6591
6592 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6593 if (p->sec == sec)
6594 {
6595 /* Everything must go for SEC. */
6596 *pp = p->next;
6597 break;
6598 }
6599 }
6600
6601 switch (r_type)
6602 {
6603 case R_PPC64_GOT_TLSLD16:
6604 case R_PPC64_GOT_TLSLD16_LO:
6605 case R_PPC64_GOT_TLSLD16_HI:
6606 case R_PPC64_GOT_TLSLD16_HA:
6607 tls_type = TLS_TLS | TLS_LD;
6608 goto dogot;
6609
6610 case R_PPC64_GOT_TLSGD16:
6611 case R_PPC64_GOT_TLSGD16_LO:
6612 case R_PPC64_GOT_TLSGD16_HI:
6613 case R_PPC64_GOT_TLSGD16_HA:
6614 tls_type = TLS_TLS | TLS_GD;
6615 goto dogot;
6616
6617 case R_PPC64_GOT_TPREL16_DS:
6618 case R_PPC64_GOT_TPREL16_LO_DS:
6619 case R_PPC64_GOT_TPREL16_HI:
6620 case R_PPC64_GOT_TPREL16_HA:
6621 tls_type = TLS_TLS | TLS_TPREL;
6622 goto dogot;
6623
6624 case R_PPC64_GOT_DTPREL16_DS:
6625 case R_PPC64_GOT_DTPREL16_LO_DS:
6626 case R_PPC64_GOT_DTPREL16_HI:
6627 case R_PPC64_GOT_DTPREL16_HA:
6628 tls_type = TLS_TLS | TLS_DTPREL;
6629 goto dogot;
6630
6631 case R_PPC64_GOT16:
6632 case R_PPC64_GOT16_DS:
6633 case R_PPC64_GOT16_HA:
6634 case R_PPC64_GOT16_HI:
6635 case R_PPC64_GOT16_LO:
6636 case R_PPC64_GOT16_LO_DS:
6637 dogot:
6638 {
6639 struct got_entry *ent;
6640
6641 if (h != NULL)
6642 ent = h->got.glist;
6643 else
6644 ent = local_got_ents[r_symndx];
6645
6646 for (; ent != NULL; ent = ent->next)
6647 if (ent->addend == rel->r_addend
6648 && ent->owner == abfd
6649 && ent->tls_type == tls_type)
6650 break;
6651 if (ent == NULL)
6652 abort ();
6653 if (ent->got.refcount > 0)
6654 ent->got.refcount -= 1;
6655 }
6656 break;
6657
6658 case R_PPC64_PLT16_HA:
6659 case R_PPC64_PLT16_HI:
6660 case R_PPC64_PLT16_LO:
6661 case R_PPC64_PLT32:
6662 case R_PPC64_PLT64:
6663 case R_PPC64_REL14:
6664 case R_PPC64_REL14_BRNTAKEN:
6665 case R_PPC64_REL14_BRTAKEN:
6666 case R_PPC64_REL24:
6667 plt_list = NULL;
6668 if (h != NULL)
6669 plt_list = &h->plt.plist;
6670 else if (local_got_ents != NULL)
6671 {
6672 struct plt_entry **local_plt = (struct plt_entry **)
6673 (local_got_ents + symtab_hdr->sh_info);
6674 unsigned char *local_got_tls_masks = (unsigned char *)
6675 (local_plt + symtab_hdr->sh_info);
6676 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6677 plt_list = local_plt + r_symndx;
6678 }
6679 if (plt_list)
6680 {
6681 struct plt_entry *ent;
6682
6683 for (ent = *plt_list; ent != NULL; ent = ent->next)
6684 if (ent->addend == rel->r_addend)
6685 break;
6686 if (ent != NULL && ent->plt.refcount > 0)
6687 ent->plt.refcount -= 1;
6688 }
6689 break;
6690
6691 default:
6692 break;
6693 }
6694 }
6695 return TRUE;
6696 }
6697
6698 /* The maximum size of .sfpr. */
6699 #define SFPR_MAX (218*4)
6700
6701 struct sfpr_def_parms
6702 {
6703 const char name[12];
6704 unsigned char lo, hi;
6705 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6706 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6707 };
6708
6709 /* Auto-generate _save*, _rest* functions in .sfpr.
6710 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6711 instead. */
6712
6713 static bfd_boolean
6714 sfpr_define (struct bfd_link_info *info,
6715 const struct sfpr_def_parms *parm,
6716 asection *stub_sec)
6717 {
6718 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6719 unsigned int i;
6720 size_t len = strlen (parm->name);
6721 bfd_boolean writing = FALSE;
6722 char sym[16];
6723
6724 if (htab == NULL)
6725 return FALSE;
6726
6727 memcpy (sym, parm->name, len);
6728 sym[len + 2] = 0;
6729
6730 for (i = parm->lo; i <= parm->hi; i++)
6731 {
6732 struct ppc_link_hash_entry *h;
6733
6734 sym[len + 0] = i / 10 + '0';
6735 sym[len + 1] = i % 10 + '0';
6736 h = (struct ppc_link_hash_entry *)
6737 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6738 if (stub_sec != NULL)
6739 {
6740 if (h != NULL
6741 && h->elf.root.type == bfd_link_hash_defined
6742 && h->elf.root.u.def.section == htab->sfpr)
6743 {
6744 struct elf_link_hash_entry *s;
6745 char buf[32];
6746 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6747 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6748 if (s == NULL)
6749 return FALSE;
6750 if (s->root.type == bfd_link_hash_new
6751 || (s->root.type = bfd_link_hash_defined
6752 && s->root.u.def.section == stub_sec))
6753 {
6754 s->root.type = bfd_link_hash_defined;
6755 s->root.u.def.section = stub_sec;
6756 s->root.u.def.value = (stub_sec->size
6757 + h->elf.root.u.def.value);
6758 s->ref_regular = 1;
6759 s->def_regular = 1;
6760 s->ref_regular_nonweak = 1;
6761 s->forced_local = 1;
6762 s->non_elf = 0;
6763 s->root.linker_def = 1;
6764 }
6765 }
6766 continue;
6767 }
6768 if (h != NULL)
6769 {
6770 h->save_res = 1;
6771 if (!h->elf.def_regular)
6772 {
6773 h->elf.root.type = bfd_link_hash_defined;
6774 h->elf.root.u.def.section = htab->sfpr;
6775 h->elf.root.u.def.value = htab->sfpr->size;
6776 h->elf.type = STT_FUNC;
6777 h->elf.def_regular = 1;
6778 h->elf.non_elf = 0;
6779 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6780 writing = TRUE;
6781 if (htab->sfpr->contents == NULL)
6782 {
6783 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6784 if (htab->sfpr->contents == NULL)
6785 return FALSE;
6786 }
6787 }
6788 }
6789 if (writing)
6790 {
6791 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6792 if (i != parm->hi)
6793 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6794 else
6795 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6796 htab->sfpr->size = p - htab->sfpr->contents;
6797 }
6798 }
6799
6800 return TRUE;
6801 }
6802
6803 static bfd_byte *
6804 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6805 {
6806 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6807 return p + 4;
6808 }
6809
6810 static bfd_byte *
6811 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6812 {
6813 p = savegpr0 (abfd, p, r);
6814 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6815 p = p + 4;
6816 bfd_put_32 (abfd, BLR, p);
6817 return p + 4;
6818 }
6819
6820 static bfd_byte *
6821 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6822 {
6823 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6824 return p + 4;
6825 }
6826
6827 static bfd_byte *
6828 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6829 {
6830 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6831 p = p + 4;
6832 p = restgpr0 (abfd, p, r);
6833 bfd_put_32 (abfd, MTLR_R0, p);
6834 p = p + 4;
6835 if (r == 29)
6836 {
6837 p = restgpr0 (abfd, p, 30);
6838 p = restgpr0 (abfd, p, 31);
6839 }
6840 bfd_put_32 (abfd, BLR, p);
6841 return p + 4;
6842 }
6843
6844 static bfd_byte *
6845 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6846 {
6847 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6848 return p + 4;
6849 }
6850
6851 static bfd_byte *
6852 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6853 {
6854 p = savegpr1 (abfd, p, r);
6855 bfd_put_32 (abfd, BLR, p);
6856 return p + 4;
6857 }
6858
6859 static bfd_byte *
6860 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6861 {
6862 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6863 return p + 4;
6864 }
6865
6866 static bfd_byte *
6867 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6868 {
6869 p = restgpr1 (abfd, p, r);
6870 bfd_put_32 (abfd, BLR, p);
6871 return p + 4;
6872 }
6873
6874 static bfd_byte *
6875 savefpr (bfd *abfd, bfd_byte *p, int r)
6876 {
6877 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6878 return p + 4;
6879 }
6880
6881 static bfd_byte *
6882 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6883 {
6884 p = savefpr (abfd, p, r);
6885 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6886 p = p + 4;
6887 bfd_put_32 (abfd, BLR, p);
6888 return p + 4;
6889 }
6890
6891 static bfd_byte *
6892 restfpr (bfd *abfd, bfd_byte *p, int r)
6893 {
6894 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6895 return p + 4;
6896 }
6897
6898 static bfd_byte *
6899 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6900 {
6901 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6902 p = p + 4;
6903 p = restfpr (abfd, p, r);
6904 bfd_put_32 (abfd, MTLR_R0, p);
6905 p = p + 4;
6906 if (r == 29)
6907 {
6908 p = restfpr (abfd, p, 30);
6909 p = restfpr (abfd, p, 31);
6910 }
6911 bfd_put_32 (abfd, BLR, p);
6912 return p + 4;
6913 }
6914
6915 static bfd_byte *
6916 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6917 {
6918 p = savefpr (abfd, p, r);
6919 bfd_put_32 (abfd, BLR, p);
6920 return p + 4;
6921 }
6922
6923 static bfd_byte *
6924 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6925 {
6926 p = restfpr (abfd, p, r);
6927 bfd_put_32 (abfd, BLR, p);
6928 return p + 4;
6929 }
6930
6931 static bfd_byte *
6932 savevr (bfd *abfd, bfd_byte *p, int r)
6933 {
6934 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6935 p = p + 4;
6936 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6937 return p + 4;
6938 }
6939
6940 static bfd_byte *
6941 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6942 {
6943 p = savevr (abfd, p, r);
6944 bfd_put_32 (abfd, BLR, p);
6945 return p + 4;
6946 }
6947
6948 static bfd_byte *
6949 restvr (bfd *abfd, bfd_byte *p, int r)
6950 {
6951 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6952 p = p + 4;
6953 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6954 return p + 4;
6955 }
6956
6957 static bfd_byte *
6958 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6959 {
6960 p = restvr (abfd, p, r);
6961 bfd_put_32 (abfd, BLR, p);
6962 return p + 4;
6963 }
6964
6965 /* Called via elf_link_hash_traverse to transfer dynamic linking
6966 information on function code symbol entries to their corresponding
6967 function descriptor symbol entries. */
6968
6969 static bfd_boolean
6970 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6971 {
6972 struct bfd_link_info *info;
6973 struct ppc_link_hash_table *htab;
6974 struct ppc_link_hash_entry *fh;
6975 struct ppc_link_hash_entry *fdh;
6976 bfd_boolean force_local;
6977
6978 fh = (struct ppc_link_hash_entry *) h;
6979 if (fh->elf.root.type == bfd_link_hash_indirect)
6980 return TRUE;
6981
6982 if (!fh->is_func)
6983 return TRUE;
6984
6985 if (fh->elf.root.root.string[0] != '.'
6986 || fh->elf.root.root.string[1] == '\0')
6987 return TRUE;
6988
6989 info = inf;
6990 htab = ppc_hash_table (info);
6991 if (htab == NULL)
6992 return FALSE;
6993
6994 /* Find the corresponding function descriptor symbol. */
6995 fdh = lookup_fdh (fh, htab);
6996
6997 /* Resolve undefined references to dot-symbols as the value
6998 in the function descriptor, if we have one in a regular object.
6999 This is to satisfy cases like ".quad .foo". Calls to functions
7000 in dynamic objects are handled elsewhere. */
7001 if ((fh->elf.root.type == bfd_link_hash_undefined
7002 || fh->elf.root.type == bfd_link_hash_undefweak)
7003 && (fdh->elf.root.type == bfd_link_hash_defined
7004 || fdh->elf.root.type == bfd_link_hash_defweak)
7005 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7006 && opd_entry_value (fdh->elf.root.u.def.section,
7007 fdh->elf.root.u.def.value,
7008 &fh->elf.root.u.def.section,
7009 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7010 {
7011 fh->elf.root.type = fdh->elf.root.type;
7012 fh->elf.forced_local = 1;
7013 fh->elf.def_regular = fdh->elf.def_regular;
7014 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7015 }
7016
7017 if (!fh->elf.dynamic)
7018 {
7019 struct plt_entry *ent;
7020
7021 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7022 if (ent->plt.refcount > 0)
7023 break;
7024 if (ent == NULL)
7025 return TRUE;
7026 }
7027
7028 /* Create a descriptor as undefined if necessary. */
7029 if (fdh == NULL
7030 && !bfd_link_executable (info)
7031 && (fh->elf.root.type == bfd_link_hash_undefined
7032 || fh->elf.root.type == bfd_link_hash_undefweak))
7033 {
7034 fdh = make_fdh (info, fh);
7035 if (fdh == NULL)
7036 return FALSE;
7037 }
7038
7039 /* We can't support overriding of symbols on a fake descriptor. */
7040 if (fdh != NULL
7041 && fdh->fake
7042 && (fh->elf.root.type == bfd_link_hash_defined
7043 || fh->elf.root.type == bfd_link_hash_defweak))
7044 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7045
7046 /* Transfer dynamic linking information to the function descriptor. */
7047 if (fdh != NULL)
7048 {
7049 fdh->elf.ref_regular |= fh->elf.ref_regular;
7050 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7051 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7052 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7053 fdh->elf.dynamic |= fh->elf.dynamic;
7054 fdh->elf.needs_plt |= (fh->elf.needs_plt
7055 || fh->elf.type == STT_FUNC
7056 || fh->elf.type == STT_GNU_IFUNC);
7057 move_plt_plist (fh, fdh);
7058
7059 if (!fdh->elf.forced_local
7060 && fh->elf.dynindx != -1)
7061 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7062 return FALSE;
7063 }
7064
7065 /* Now that the info is on the function descriptor, clear the
7066 function code sym info. Any function code syms for which we
7067 don't have a definition in a regular file, we force local.
7068 This prevents a shared library from exporting syms that have
7069 been imported from another library. Function code syms that
7070 are really in the library we must leave global to prevent the
7071 linker dragging in a definition from a static library. */
7072 force_local = (!fh->elf.def_regular
7073 || fdh == NULL
7074 || !fdh->elf.def_regular
7075 || fdh->elf.forced_local);
7076 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7077
7078 return TRUE;
7079 }
7080
7081 static const struct sfpr_def_parms save_res_funcs[] =
7082 {
7083 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7084 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7085 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7086 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7087 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7088 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7089 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7090 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7091 { "._savef", 14, 31, savefpr, savefpr1_tail },
7092 { "._restf", 14, 31, restfpr, restfpr1_tail },
7093 { "_savevr_", 20, 31, savevr, savevr_tail },
7094 { "_restvr_", 20, 31, restvr, restvr_tail }
7095 };
7096
7097 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7098 this hook to a) provide some gcc support functions, and b) transfer
7099 dynamic linking information gathered so far on function code symbol
7100 entries, to their corresponding function descriptor symbol entries. */
7101
7102 static bfd_boolean
7103 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7104 struct bfd_link_info *info)
7105 {
7106 struct ppc_link_hash_table *htab;
7107
7108 htab = ppc_hash_table (info);
7109 if (htab == NULL)
7110 return FALSE;
7111
7112 /* Provide any missing _save* and _rest* functions. */
7113 if (htab->sfpr != NULL)
7114 {
7115 unsigned int i;
7116
7117 htab->sfpr->size = 0;
7118 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7119 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7120 return FALSE;
7121 if (htab->sfpr->size == 0)
7122 htab->sfpr->flags |= SEC_EXCLUDE;
7123 }
7124
7125 if (bfd_link_relocatable (info))
7126 return TRUE;
7127
7128 if (htab->elf.hgot != NULL)
7129 {
7130 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7131 /* Make .TOC. defined so as to prevent it being made dynamic.
7132 The wrong value here is fixed later in ppc64_elf_set_toc. */
7133 if (!htab->elf.hgot->def_regular
7134 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7135 {
7136 htab->elf.hgot->root.type = bfd_link_hash_defined;
7137 htab->elf.hgot->root.u.def.value = 0;
7138 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7139 htab->elf.hgot->def_regular = 1;
7140 htab->elf.hgot->root.linker_def = 1;
7141 }
7142 htab->elf.hgot->type = STT_OBJECT;
7143 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7144 | STV_HIDDEN);
7145 }
7146
7147 if (htab->need_func_desc_adj)
7148 {
7149 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7150 htab->need_func_desc_adj = 0;
7151 }
7152
7153 return TRUE;
7154 }
7155
7156 /* Return true if we have dynamic relocs against H that apply to
7157 read-only sections. */
7158
7159 static bfd_boolean
7160 readonly_dynrelocs (struct elf_link_hash_entry *h)
7161 {
7162 struct ppc_link_hash_entry *eh;
7163 struct elf_dyn_relocs *p;
7164
7165 eh = (struct ppc_link_hash_entry *) h;
7166 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7167 {
7168 asection *s = p->sec->output_section;
7169
7170 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7171 return TRUE;
7172 }
7173 return FALSE;
7174 }
7175
7176 /* Return true if we have dynamic relocs against H or any of its weak
7177 aliases, that apply to read-only sections. */
7178
7179 static bfd_boolean
7180 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7181 {
7182 struct ppc_link_hash_entry *eh;
7183
7184 eh = (struct ppc_link_hash_entry *) h;
7185 do
7186 {
7187 if (readonly_dynrelocs (&eh->elf))
7188 return TRUE;
7189 eh = eh->weakref;
7190 } while (eh != NULL && &eh->elf != h);
7191
7192 return FALSE;
7193 }
7194
7195 /* Return whether EH has pc-relative dynamic relocs. */
7196
7197 static bfd_boolean
7198 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7199 {
7200 struct elf_dyn_relocs *p;
7201
7202 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7203 if (p->pc_count != 0)
7204 return TRUE;
7205 return FALSE;
7206 }
7207
7208 /* Return true if a global entry stub will be created for H. Valid
7209 for ELFv2 before plt entries have been allocated. */
7210
7211 static bfd_boolean
7212 global_entry_stub (struct elf_link_hash_entry *h)
7213 {
7214 struct plt_entry *pent;
7215
7216 if (!h->pointer_equality_needed
7217 || h->def_regular)
7218 return FALSE;
7219
7220 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7221 if (pent->plt.refcount > 0
7222 && pent->addend == 0)
7223 return TRUE;
7224
7225 return FALSE;
7226 }
7227
7228 /* Adjust a symbol defined by a dynamic object and referenced by a
7229 regular object. The current definition is in some section of the
7230 dynamic object, but we're not including those sections. We have to
7231 change the definition to something the rest of the link can
7232 understand. */
7233
7234 static bfd_boolean
7235 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7236 struct elf_link_hash_entry *h)
7237 {
7238 struct ppc_link_hash_table *htab;
7239 asection *s, *srel;
7240
7241 htab = ppc_hash_table (info);
7242 if (htab == NULL)
7243 return FALSE;
7244
7245 /* Deal with function syms. */
7246 if (h->type == STT_FUNC
7247 || h->type == STT_GNU_IFUNC
7248 || h->needs_plt)
7249 {
7250 /* Clear procedure linkage table information for any symbol that
7251 won't need a .plt entry. */
7252 struct plt_entry *ent;
7253 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7254 if (ent->plt.refcount > 0)
7255 break;
7256 if (ent == NULL
7257 || (h->type != STT_GNU_IFUNC
7258 && (SYMBOL_CALLS_LOCAL (info, h)
7259 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7260 && h->root.type == bfd_link_hash_undefweak)))
7261 || ((struct ppc_link_hash_entry *) h)->save_res)
7262 {
7263 h->plt.plist = NULL;
7264 h->needs_plt = 0;
7265 h->pointer_equality_needed = 0;
7266 }
7267 else if (abiversion (info->output_bfd) >= 2)
7268 {
7269 /* Taking a function's address in a read/write section
7270 doesn't require us to define the function symbol in the
7271 executable on a global entry stub. A dynamic reloc can
7272 be used instead. The reason we prefer a few more dynamic
7273 relocs is that calling via a global entry stub costs a
7274 few more instructions, and pointer_equality_needed causes
7275 extra work in ld.so when resolving these symbols. */
7276 if (global_entry_stub (h)
7277 && !alias_readonly_dynrelocs (h))
7278 {
7279 h->pointer_equality_needed = 0;
7280 /* After adjust_dynamic_symbol, non_got_ref set in
7281 the non-pic case means that dyn_relocs for this
7282 symbol should be discarded. */
7283 h->non_got_ref = 0;
7284 }
7285
7286 /* If making a plt entry, then we don't need copy relocs. */
7287 return TRUE;
7288 }
7289 }
7290 else
7291 h->plt.plist = NULL;
7292
7293 /* If this is a weak symbol, and there is a real definition, the
7294 processor independent code will have arranged for us to see the
7295 real definition first, and we can just use the same value. */
7296 if (h->u.weakdef != NULL)
7297 {
7298 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7299 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7300 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7301 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7302 if (ELIMINATE_COPY_RELOCS)
7303 h->non_got_ref = h->u.weakdef->non_got_ref;
7304 return TRUE;
7305 }
7306
7307 /* If we are creating a shared library, we must presume that the
7308 only references to the symbol are via the global offset table.
7309 For such cases we need not do anything here; the relocations will
7310 be handled correctly by relocate_section. */
7311 if (bfd_link_pic (info))
7312 return TRUE;
7313
7314 /* If there are no references to this symbol that do not use the
7315 GOT, we don't need to generate a copy reloc. */
7316 if (!h->non_got_ref)
7317 return TRUE;
7318
7319 /* Don't generate a copy reloc for symbols defined in the executable. */
7320 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7321
7322 /* If -z nocopyreloc was given, don't generate them either. */
7323 || info->nocopyreloc
7324
7325 /* If we didn't find any dynamic relocs in read-only sections, then
7326 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7327 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7328
7329 /* Protected variables do not work with .dynbss. The copy in
7330 .dynbss won't be used by the shared library with the protected
7331 definition for the variable. Text relocations are preferable
7332 to an incorrect program. */
7333 || h->protected_def)
7334 {
7335 h->non_got_ref = 0;
7336 return TRUE;
7337 }
7338
7339 if (h->plt.plist != NULL)
7340 {
7341 /* We should never get here, but unfortunately there are versions
7342 of gcc out there that improperly (for this ABI) put initialized
7343 function pointers, vtable refs and suchlike in read-only
7344 sections. Allow them to proceed, but warn that this might
7345 break at runtime. */
7346 info->callbacks->einfo
7347 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7348 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7349 h->root.root.string);
7350 }
7351
7352 /* This is a reference to a symbol defined by a dynamic object which
7353 is not a function. */
7354
7355 /* We must allocate the symbol in our .dynbss section, which will
7356 become part of the .bss section of the executable. There will be
7357 an entry for this symbol in the .dynsym section. The dynamic
7358 object will contain position independent code, so all references
7359 from the dynamic object to this symbol will go through the global
7360 offset table. The dynamic linker will use the .dynsym entry to
7361 determine the address it must put in the global offset table, so
7362 both the dynamic object and the regular object will refer to the
7363 same memory location for the variable. */
7364
7365 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7366 to copy the initial value out of the dynamic object and into the
7367 runtime process image. We need to remember the offset into the
7368 .rela.bss section we are going to use. */
7369 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7370 {
7371 s = htab->elf.sdynrelro;
7372 srel = htab->elf.sreldynrelro;
7373 }
7374 else
7375 {
7376 s = htab->elf.sdynbss;
7377 srel = htab->elf.srelbss;
7378 }
7379 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7380 {
7381 srel->size += sizeof (Elf64_External_Rela);
7382 h->needs_copy = 1;
7383 }
7384
7385 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7386 }
7387
7388 /* If given a function descriptor symbol, hide both the function code
7389 sym and the descriptor. */
7390 static void
7391 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7392 struct elf_link_hash_entry *h,
7393 bfd_boolean force_local)
7394 {
7395 struct ppc_link_hash_entry *eh;
7396 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7397
7398 eh = (struct ppc_link_hash_entry *) h;
7399 if (eh->is_func_descriptor)
7400 {
7401 struct ppc_link_hash_entry *fh = eh->oh;
7402
7403 if (fh == NULL)
7404 {
7405 const char *p, *q;
7406 struct elf_link_hash_table *htab = elf_hash_table (info);
7407 char save;
7408
7409 /* We aren't supposed to use alloca in BFD because on
7410 systems which do not have alloca the version in libiberty
7411 calls xmalloc, which might cause the program to crash
7412 when it runs out of memory. This function doesn't have a
7413 return status, so there's no way to gracefully return an
7414 error. So cheat. We know that string[-1] can be safely
7415 accessed; It's either a string in an ELF string table,
7416 or allocated in an objalloc structure. */
7417
7418 p = eh->elf.root.root.string - 1;
7419 save = *p;
7420 *(char *) p = '.';
7421 fh = (struct ppc_link_hash_entry *)
7422 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7423 *(char *) p = save;
7424
7425 /* Unfortunately, if it so happens that the string we were
7426 looking for was allocated immediately before this string,
7427 then we overwrote the string terminator. That's the only
7428 reason the lookup should fail. */
7429 if (fh == NULL)
7430 {
7431 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7432 while (q >= eh->elf.root.root.string && *q == *p)
7433 --q, --p;
7434 if (q < eh->elf.root.root.string && *p == '.')
7435 fh = (struct ppc_link_hash_entry *)
7436 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7437 }
7438 if (fh != NULL)
7439 {
7440 eh->oh = fh;
7441 fh->oh = eh;
7442 }
7443 }
7444 if (fh != NULL)
7445 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7446 }
7447 }
7448
7449 static bfd_boolean
7450 get_sym_h (struct elf_link_hash_entry **hp,
7451 Elf_Internal_Sym **symp,
7452 asection **symsecp,
7453 unsigned char **tls_maskp,
7454 Elf_Internal_Sym **locsymsp,
7455 unsigned long r_symndx,
7456 bfd *ibfd)
7457 {
7458 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7459
7460 if (r_symndx >= symtab_hdr->sh_info)
7461 {
7462 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7463 struct elf_link_hash_entry *h;
7464
7465 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7466 h = elf_follow_link (h);
7467
7468 if (hp != NULL)
7469 *hp = h;
7470
7471 if (symp != NULL)
7472 *symp = NULL;
7473
7474 if (symsecp != NULL)
7475 {
7476 asection *symsec = NULL;
7477 if (h->root.type == bfd_link_hash_defined
7478 || h->root.type == bfd_link_hash_defweak)
7479 symsec = h->root.u.def.section;
7480 *symsecp = symsec;
7481 }
7482
7483 if (tls_maskp != NULL)
7484 {
7485 struct ppc_link_hash_entry *eh;
7486
7487 eh = (struct ppc_link_hash_entry *) h;
7488 *tls_maskp = &eh->tls_mask;
7489 }
7490 }
7491 else
7492 {
7493 Elf_Internal_Sym *sym;
7494 Elf_Internal_Sym *locsyms = *locsymsp;
7495
7496 if (locsyms == NULL)
7497 {
7498 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7499 if (locsyms == NULL)
7500 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7501 symtab_hdr->sh_info,
7502 0, NULL, NULL, NULL);
7503 if (locsyms == NULL)
7504 return FALSE;
7505 *locsymsp = locsyms;
7506 }
7507 sym = locsyms + r_symndx;
7508
7509 if (hp != NULL)
7510 *hp = NULL;
7511
7512 if (symp != NULL)
7513 *symp = sym;
7514
7515 if (symsecp != NULL)
7516 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7517
7518 if (tls_maskp != NULL)
7519 {
7520 struct got_entry **lgot_ents;
7521 unsigned char *tls_mask;
7522
7523 tls_mask = NULL;
7524 lgot_ents = elf_local_got_ents (ibfd);
7525 if (lgot_ents != NULL)
7526 {
7527 struct plt_entry **local_plt = (struct plt_entry **)
7528 (lgot_ents + symtab_hdr->sh_info);
7529 unsigned char *lgot_masks = (unsigned char *)
7530 (local_plt + symtab_hdr->sh_info);
7531 tls_mask = &lgot_masks[r_symndx];
7532 }
7533 *tls_maskp = tls_mask;
7534 }
7535 }
7536 return TRUE;
7537 }
7538
7539 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7540 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7541 type suitable for optimization, and 1 otherwise. */
7542
7543 static int
7544 get_tls_mask (unsigned char **tls_maskp,
7545 unsigned long *toc_symndx,
7546 bfd_vma *toc_addend,
7547 Elf_Internal_Sym **locsymsp,
7548 const Elf_Internal_Rela *rel,
7549 bfd *ibfd)
7550 {
7551 unsigned long r_symndx;
7552 int next_r;
7553 struct elf_link_hash_entry *h;
7554 Elf_Internal_Sym *sym;
7555 asection *sec;
7556 bfd_vma off;
7557
7558 r_symndx = ELF64_R_SYM (rel->r_info);
7559 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7560 return 0;
7561
7562 if ((*tls_maskp != NULL && **tls_maskp != 0)
7563 || sec == NULL
7564 || ppc64_elf_section_data (sec) == NULL
7565 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7566 return 1;
7567
7568 /* Look inside a TOC section too. */
7569 if (h != NULL)
7570 {
7571 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7572 off = h->root.u.def.value;
7573 }
7574 else
7575 off = sym->st_value;
7576 off += rel->r_addend;
7577 BFD_ASSERT (off % 8 == 0);
7578 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7579 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7580 if (toc_symndx != NULL)
7581 *toc_symndx = r_symndx;
7582 if (toc_addend != NULL)
7583 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7584 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7585 return 0;
7586 if ((h == NULL || is_static_defined (h))
7587 && (next_r == -1 || next_r == -2))
7588 return 1 - next_r;
7589 return 1;
7590 }
7591
7592 /* Find (or create) an entry in the tocsave hash table. */
7593
7594 static struct tocsave_entry *
7595 tocsave_find (struct ppc_link_hash_table *htab,
7596 enum insert_option insert,
7597 Elf_Internal_Sym **local_syms,
7598 const Elf_Internal_Rela *irela,
7599 bfd *ibfd)
7600 {
7601 unsigned long r_indx;
7602 struct elf_link_hash_entry *h;
7603 Elf_Internal_Sym *sym;
7604 struct tocsave_entry ent, *p;
7605 hashval_t hash;
7606 struct tocsave_entry **slot;
7607
7608 r_indx = ELF64_R_SYM (irela->r_info);
7609 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7610 return NULL;
7611 if (ent.sec == NULL || ent.sec->output_section == NULL)
7612 {
7613 _bfd_error_handler
7614 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7615 return NULL;
7616 }
7617
7618 if (h != NULL)
7619 ent.offset = h->root.u.def.value;
7620 else
7621 ent.offset = sym->st_value;
7622 ent.offset += irela->r_addend;
7623
7624 hash = tocsave_htab_hash (&ent);
7625 slot = ((struct tocsave_entry **)
7626 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7627 if (slot == NULL)
7628 return NULL;
7629
7630 if (*slot == NULL)
7631 {
7632 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7633 if (p == NULL)
7634 return NULL;
7635 *p = ent;
7636 *slot = p;
7637 }
7638 return *slot;
7639 }
7640
7641 /* Adjust all global syms defined in opd sections. In gcc generated
7642 code for the old ABI, these will already have been done. */
7643
7644 static bfd_boolean
7645 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7646 {
7647 struct ppc_link_hash_entry *eh;
7648 asection *sym_sec;
7649 struct _opd_sec_data *opd;
7650
7651 if (h->root.type == bfd_link_hash_indirect)
7652 return TRUE;
7653
7654 if (h->root.type != bfd_link_hash_defined
7655 && h->root.type != bfd_link_hash_defweak)
7656 return TRUE;
7657
7658 eh = (struct ppc_link_hash_entry *) h;
7659 if (eh->adjust_done)
7660 return TRUE;
7661
7662 sym_sec = eh->elf.root.u.def.section;
7663 opd = get_opd_info (sym_sec);
7664 if (opd != NULL && opd->adjust != NULL)
7665 {
7666 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7667 if (adjust == -1)
7668 {
7669 /* This entry has been deleted. */
7670 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7671 if (dsec == NULL)
7672 {
7673 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7674 if (discarded_section (dsec))
7675 {
7676 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7677 break;
7678 }
7679 }
7680 eh->elf.root.u.def.value = 0;
7681 eh->elf.root.u.def.section = dsec;
7682 }
7683 else
7684 eh->elf.root.u.def.value += adjust;
7685 eh->adjust_done = 1;
7686 }
7687 return TRUE;
7688 }
7689
7690 /* Handles decrementing dynamic reloc counts for the reloc specified by
7691 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7692 have already been determined. */
7693
7694 static bfd_boolean
7695 dec_dynrel_count (bfd_vma r_info,
7696 asection *sec,
7697 struct bfd_link_info *info,
7698 Elf_Internal_Sym **local_syms,
7699 struct elf_link_hash_entry *h,
7700 Elf_Internal_Sym *sym)
7701 {
7702 enum elf_ppc64_reloc_type r_type;
7703 asection *sym_sec = NULL;
7704
7705 /* Can this reloc be dynamic? This switch, and later tests here
7706 should be kept in sync with the code in check_relocs. */
7707 r_type = ELF64_R_TYPE (r_info);
7708 switch (r_type)
7709 {
7710 default:
7711 return TRUE;
7712
7713 case R_PPC64_TPREL16:
7714 case R_PPC64_TPREL16_LO:
7715 case R_PPC64_TPREL16_HI:
7716 case R_PPC64_TPREL16_HA:
7717 case R_PPC64_TPREL16_DS:
7718 case R_PPC64_TPREL16_LO_DS:
7719 case R_PPC64_TPREL16_HIGH:
7720 case R_PPC64_TPREL16_HIGHA:
7721 case R_PPC64_TPREL16_HIGHER:
7722 case R_PPC64_TPREL16_HIGHERA:
7723 case R_PPC64_TPREL16_HIGHEST:
7724 case R_PPC64_TPREL16_HIGHESTA:
7725 if (!bfd_link_pic (info))
7726 return TRUE;
7727
7728 case R_PPC64_TPREL64:
7729 case R_PPC64_DTPMOD64:
7730 case R_PPC64_DTPREL64:
7731 case R_PPC64_ADDR64:
7732 case R_PPC64_REL30:
7733 case R_PPC64_REL32:
7734 case R_PPC64_REL64:
7735 case R_PPC64_ADDR14:
7736 case R_PPC64_ADDR14_BRNTAKEN:
7737 case R_PPC64_ADDR14_BRTAKEN:
7738 case R_PPC64_ADDR16:
7739 case R_PPC64_ADDR16_DS:
7740 case R_PPC64_ADDR16_HA:
7741 case R_PPC64_ADDR16_HI:
7742 case R_PPC64_ADDR16_HIGH:
7743 case R_PPC64_ADDR16_HIGHA:
7744 case R_PPC64_ADDR16_HIGHER:
7745 case R_PPC64_ADDR16_HIGHERA:
7746 case R_PPC64_ADDR16_HIGHEST:
7747 case R_PPC64_ADDR16_HIGHESTA:
7748 case R_PPC64_ADDR16_LO:
7749 case R_PPC64_ADDR16_LO_DS:
7750 case R_PPC64_ADDR24:
7751 case R_PPC64_ADDR32:
7752 case R_PPC64_UADDR16:
7753 case R_PPC64_UADDR32:
7754 case R_PPC64_UADDR64:
7755 case R_PPC64_TOC:
7756 break;
7757 }
7758
7759 if (local_syms != NULL)
7760 {
7761 unsigned long r_symndx;
7762 bfd *ibfd = sec->owner;
7763
7764 r_symndx = ELF64_R_SYM (r_info);
7765 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7766 return FALSE;
7767 }
7768
7769 if ((bfd_link_pic (info)
7770 && (must_be_dyn_reloc (info, r_type)
7771 || (h != NULL
7772 && (!SYMBOLIC_BIND (info, h)
7773 || h->root.type == bfd_link_hash_defweak
7774 || !h->def_regular))))
7775 || (ELIMINATE_COPY_RELOCS
7776 && !bfd_link_pic (info)
7777 && h != NULL
7778 && (h->root.type == bfd_link_hash_defweak
7779 || !h->def_regular)))
7780 ;
7781 else
7782 return TRUE;
7783
7784 if (h != NULL)
7785 {
7786 struct elf_dyn_relocs *p;
7787 struct elf_dyn_relocs **pp;
7788 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7789
7790 /* elf_gc_sweep may have already removed all dyn relocs associated
7791 with local syms for a given section. Also, symbol flags are
7792 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7793 report a dynreloc miscount. */
7794 if (*pp == NULL && info->gc_sections)
7795 return TRUE;
7796
7797 while ((p = *pp) != NULL)
7798 {
7799 if (p->sec == sec)
7800 {
7801 if (!must_be_dyn_reloc (info, r_type))
7802 p->pc_count -= 1;
7803 p->count -= 1;
7804 if (p->count == 0)
7805 *pp = p->next;
7806 return TRUE;
7807 }
7808 pp = &p->next;
7809 }
7810 }
7811 else
7812 {
7813 struct ppc_dyn_relocs *p;
7814 struct ppc_dyn_relocs **pp;
7815 void *vpp;
7816 bfd_boolean is_ifunc;
7817
7818 if (local_syms == NULL)
7819 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7820 if (sym_sec == NULL)
7821 sym_sec = sec;
7822
7823 vpp = &elf_section_data (sym_sec)->local_dynrel;
7824 pp = (struct ppc_dyn_relocs **) vpp;
7825
7826 if (*pp == NULL && info->gc_sections)
7827 return TRUE;
7828
7829 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7830 while ((p = *pp) != NULL)
7831 {
7832 if (p->sec == sec && p->ifunc == is_ifunc)
7833 {
7834 p->count -= 1;
7835 if (p->count == 0)
7836 *pp = p->next;
7837 return TRUE;
7838 }
7839 pp = &p->next;
7840 }
7841 }
7842
7843 /* xgettext:c-format */
7844 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7845 sec->owner, sec);
7846 bfd_set_error (bfd_error_bad_value);
7847 return FALSE;
7848 }
7849
7850 /* Remove unused Official Procedure Descriptor entries. Currently we
7851 only remove those associated with functions in discarded link-once
7852 sections, or weakly defined functions that have been overridden. It
7853 would be possible to remove many more entries for statically linked
7854 applications. */
7855
7856 bfd_boolean
7857 ppc64_elf_edit_opd (struct bfd_link_info *info)
7858 {
7859 bfd *ibfd;
7860 bfd_boolean some_edited = FALSE;
7861 asection *need_pad = NULL;
7862 struct ppc_link_hash_table *htab;
7863
7864 htab = ppc_hash_table (info);
7865 if (htab == NULL)
7866 return FALSE;
7867
7868 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7869 {
7870 asection *sec;
7871 Elf_Internal_Rela *relstart, *rel, *relend;
7872 Elf_Internal_Shdr *symtab_hdr;
7873 Elf_Internal_Sym *local_syms;
7874 struct _opd_sec_data *opd;
7875 bfd_boolean need_edit, add_aux_fields, broken;
7876 bfd_size_type cnt_16b = 0;
7877
7878 if (!is_ppc64_elf (ibfd))
7879 continue;
7880
7881 sec = bfd_get_section_by_name (ibfd, ".opd");
7882 if (sec == NULL || sec->size == 0)
7883 continue;
7884
7885 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7886 continue;
7887
7888 if (sec->output_section == bfd_abs_section_ptr)
7889 continue;
7890
7891 /* Look through the section relocs. */
7892 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7893 continue;
7894
7895 local_syms = NULL;
7896 symtab_hdr = &elf_symtab_hdr (ibfd);
7897
7898 /* Read the relocations. */
7899 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7900 info->keep_memory);
7901 if (relstart == NULL)
7902 return FALSE;
7903
7904 /* First run through the relocs to check they are sane, and to
7905 determine whether we need to edit this opd section. */
7906 need_edit = FALSE;
7907 broken = FALSE;
7908 need_pad = sec;
7909 relend = relstart + sec->reloc_count;
7910 for (rel = relstart; rel < relend; )
7911 {
7912 enum elf_ppc64_reloc_type r_type;
7913 unsigned long r_symndx;
7914 asection *sym_sec;
7915 struct elf_link_hash_entry *h;
7916 Elf_Internal_Sym *sym;
7917 bfd_vma offset;
7918
7919 /* .opd contains an array of 16 or 24 byte entries. We're
7920 only interested in the reloc pointing to a function entry
7921 point. */
7922 offset = rel->r_offset;
7923 if (rel + 1 == relend
7924 || rel[1].r_offset != offset + 8)
7925 {
7926 /* If someone messes with .opd alignment then after a
7927 "ld -r" we might have padding in the middle of .opd.
7928 Also, there's nothing to prevent someone putting
7929 something silly in .opd with the assembler. No .opd
7930 optimization for them! */
7931 broken_opd:
7932 _bfd_error_handler
7933 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7934 broken = TRUE;
7935 break;
7936 }
7937
7938 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7939 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7940 {
7941 _bfd_error_handler
7942 /* xgettext:c-format */
7943 (_("%B: unexpected reloc type %u in .opd section"),
7944 ibfd, r_type);
7945 broken = TRUE;
7946 break;
7947 }
7948
7949 r_symndx = ELF64_R_SYM (rel->r_info);
7950 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7951 r_symndx, ibfd))
7952 goto error_ret;
7953
7954 if (sym_sec == NULL || sym_sec->owner == NULL)
7955 {
7956 const char *sym_name;
7957 if (h != NULL)
7958 sym_name = h->root.root.string;
7959 else
7960 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7961 sym_sec);
7962
7963 _bfd_error_handler
7964 /* xgettext:c-format */
7965 (_("%B: undefined sym `%s' in .opd section"),
7966 ibfd, sym_name);
7967 broken = TRUE;
7968 break;
7969 }
7970
7971 /* opd entries are always for functions defined in the
7972 current input bfd. If the symbol isn't defined in the
7973 input bfd, then we won't be using the function in this
7974 bfd; It must be defined in a linkonce section in another
7975 bfd, or is weak. It's also possible that we are
7976 discarding the function due to a linker script /DISCARD/,
7977 which we test for via the output_section. */
7978 if (sym_sec->owner != ibfd
7979 || sym_sec->output_section == bfd_abs_section_ptr)
7980 need_edit = TRUE;
7981
7982 rel += 2;
7983 if (rel + 1 == relend
7984 || (rel + 2 < relend
7985 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7986 ++rel;
7987
7988 if (rel == relend)
7989 {
7990 if (sec->size == offset + 24)
7991 {
7992 need_pad = NULL;
7993 break;
7994 }
7995 if (sec->size == offset + 16)
7996 {
7997 cnt_16b++;
7998 break;
7999 }
8000 goto broken_opd;
8001 }
8002 else if (rel + 1 < relend
8003 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8004 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8005 {
8006 if (rel[0].r_offset == offset + 16)
8007 cnt_16b++;
8008 else if (rel[0].r_offset != offset + 24)
8009 goto broken_opd;
8010 }
8011 else
8012 goto broken_opd;
8013 }
8014
8015 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8016
8017 if (!broken && (need_edit || add_aux_fields))
8018 {
8019 Elf_Internal_Rela *write_rel;
8020 Elf_Internal_Shdr *rel_hdr;
8021 bfd_byte *rptr, *wptr;
8022 bfd_byte *new_contents;
8023 bfd_size_type amt;
8024
8025 new_contents = NULL;
8026 amt = OPD_NDX (sec->size) * sizeof (long);
8027 opd = &ppc64_elf_section_data (sec)->u.opd;
8028 opd->adjust = bfd_zalloc (sec->owner, amt);
8029 if (opd->adjust == NULL)
8030 return FALSE;
8031 ppc64_elf_section_data (sec)->sec_type = sec_opd;
8032
8033 /* This seems a waste of time as input .opd sections are all
8034 zeros as generated by gcc, but I suppose there's no reason
8035 this will always be so. We might start putting something in
8036 the third word of .opd entries. */
8037 if ((sec->flags & SEC_IN_MEMORY) == 0)
8038 {
8039 bfd_byte *loc;
8040 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8041 {
8042 if (loc != NULL)
8043 free (loc);
8044 error_ret:
8045 if (local_syms != NULL
8046 && symtab_hdr->contents != (unsigned char *) local_syms)
8047 free (local_syms);
8048 if (elf_section_data (sec)->relocs != relstart)
8049 free (relstart);
8050 return FALSE;
8051 }
8052 sec->contents = loc;
8053 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8054 }
8055
8056 elf_section_data (sec)->relocs = relstart;
8057
8058 new_contents = sec->contents;
8059 if (add_aux_fields)
8060 {
8061 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8062 if (new_contents == NULL)
8063 return FALSE;
8064 need_pad = NULL;
8065 }
8066 wptr = new_contents;
8067 rptr = sec->contents;
8068 write_rel = relstart;
8069 for (rel = relstart; rel < relend; )
8070 {
8071 unsigned long r_symndx;
8072 asection *sym_sec;
8073 struct elf_link_hash_entry *h;
8074 struct ppc_link_hash_entry *fdh = NULL;
8075 Elf_Internal_Sym *sym;
8076 long opd_ent_size;
8077 Elf_Internal_Rela *next_rel;
8078 bfd_boolean skip;
8079
8080 r_symndx = ELF64_R_SYM (rel->r_info);
8081 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8082 r_symndx, ibfd))
8083 goto error_ret;
8084
8085 next_rel = rel + 2;
8086 if (next_rel + 1 == relend
8087 || (next_rel + 2 < relend
8088 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8089 ++next_rel;
8090
8091 /* See if the .opd entry is full 24 byte or
8092 16 byte (with fd_aux entry overlapped with next
8093 fd_func). */
8094 opd_ent_size = 24;
8095 if (next_rel == relend)
8096 {
8097 if (sec->size == rel->r_offset + 16)
8098 opd_ent_size = 16;
8099 }
8100 else if (next_rel->r_offset == rel->r_offset + 16)
8101 opd_ent_size = 16;
8102
8103 if (h != NULL
8104 && h->root.root.string[0] == '.')
8105 {
8106 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8107 if (fdh != NULL)
8108 {
8109 fdh = ppc_follow_link (fdh);
8110 if (fdh->elf.root.type != bfd_link_hash_defined
8111 && fdh->elf.root.type != bfd_link_hash_defweak)
8112 fdh = NULL;
8113 }
8114 }
8115
8116 skip = (sym_sec->owner != ibfd
8117 || sym_sec->output_section == bfd_abs_section_ptr);
8118 if (skip)
8119 {
8120 if (fdh != NULL && sym_sec->owner == ibfd)
8121 {
8122 /* Arrange for the function descriptor sym
8123 to be dropped. */
8124 fdh->elf.root.u.def.value = 0;
8125 fdh->elf.root.u.def.section = sym_sec;
8126 }
8127 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8128
8129 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8130 rel = next_rel;
8131 else
8132 while (1)
8133 {
8134 if (!dec_dynrel_count (rel->r_info, sec, info,
8135 NULL, h, sym))
8136 goto error_ret;
8137
8138 if (++rel == next_rel)
8139 break;
8140
8141 r_symndx = ELF64_R_SYM (rel->r_info);
8142 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8143 r_symndx, ibfd))
8144 goto error_ret;
8145 }
8146 }
8147 else
8148 {
8149 /* We'll be keeping this opd entry. */
8150 long adjust;
8151
8152 if (fdh != NULL)
8153 {
8154 /* Redefine the function descriptor symbol to
8155 this location in the opd section. It is
8156 necessary to update the value here rather
8157 than using an array of adjustments as we do
8158 for local symbols, because various places
8159 in the generic ELF code use the value
8160 stored in u.def.value. */
8161 fdh->elf.root.u.def.value = wptr - new_contents;
8162 fdh->adjust_done = 1;
8163 }
8164
8165 /* Local syms are a bit tricky. We could
8166 tweak them as they can be cached, but
8167 we'd need to look through the local syms
8168 for the function descriptor sym which we
8169 don't have at the moment. So keep an
8170 array of adjustments. */
8171 adjust = (wptr - new_contents) - (rptr - sec->contents);
8172 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8173
8174 if (wptr != rptr)
8175 memcpy (wptr, rptr, opd_ent_size);
8176 wptr += opd_ent_size;
8177 if (add_aux_fields && opd_ent_size == 16)
8178 {
8179 memset (wptr, '\0', 8);
8180 wptr += 8;
8181 }
8182
8183 /* We need to adjust any reloc offsets to point to the
8184 new opd entries. */
8185 for ( ; rel != next_rel; ++rel)
8186 {
8187 rel->r_offset += adjust;
8188 if (write_rel != rel)
8189 memcpy (write_rel, rel, sizeof (*rel));
8190 ++write_rel;
8191 }
8192 }
8193
8194 rptr += opd_ent_size;
8195 }
8196
8197 sec->size = wptr - new_contents;
8198 sec->reloc_count = write_rel - relstart;
8199 if (add_aux_fields)
8200 {
8201 free (sec->contents);
8202 sec->contents = new_contents;
8203 }
8204
8205 /* Fudge the header size too, as this is used later in
8206 elf_bfd_final_link if we are emitting relocs. */
8207 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8208 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8209 some_edited = TRUE;
8210 }
8211 else if (elf_section_data (sec)->relocs != relstart)
8212 free (relstart);
8213
8214 if (local_syms != NULL
8215 && symtab_hdr->contents != (unsigned char *) local_syms)
8216 {
8217 if (!info->keep_memory)
8218 free (local_syms);
8219 else
8220 symtab_hdr->contents = (unsigned char *) local_syms;
8221 }
8222 }
8223
8224 if (some_edited)
8225 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8226
8227 /* If we are doing a final link and the last .opd entry is just 16 byte
8228 long, add a 8 byte padding after it. */
8229 if (need_pad != NULL && !bfd_link_relocatable (info))
8230 {
8231 bfd_byte *p;
8232
8233 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8234 {
8235 BFD_ASSERT (need_pad->size > 0);
8236
8237 p = bfd_malloc (need_pad->size + 8);
8238 if (p == NULL)
8239 return FALSE;
8240
8241 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8242 p, 0, need_pad->size))
8243 return FALSE;
8244
8245 need_pad->contents = p;
8246 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8247 }
8248 else
8249 {
8250 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8251 if (p == NULL)
8252 return FALSE;
8253
8254 need_pad->contents = p;
8255 }
8256
8257 memset (need_pad->contents + need_pad->size, 0, 8);
8258 need_pad->size += 8;
8259 }
8260
8261 return TRUE;
8262 }
8263
8264 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8265
8266 asection *
8267 ppc64_elf_tls_setup (struct bfd_link_info *info)
8268 {
8269 struct ppc_link_hash_table *htab;
8270
8271 htab = ppc_hash_table (info);
8272 if (htab == NULL)
8273 return NULL;
8274
8275 if (abiversion (info->output_bfd) == 1)
8276 htab->opd_abi = 1;
8277
8278 if (htab->params->no_multi_toc)
8279 htab->do_multi_toc = 0;
8280 else if (!htab->do_multi_toc)
8281 htab->params->no_multi_toc = 1;
8282
8283 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8284 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8285 FALSE, FALSE, TRUE));
8286 /* Move dynamic linking info to the function descriptor sym. */
8287 if (htab->tls_get_addr != NULL)
8288 func_desc_adjust (&htab->tls_get_addr->elf, info);
8289 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8290 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8291 FALSE, FALSE, TRUE));
8292 if (htab->params->tls_get_addr_opt)
8293 {
8294 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8295
8296 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8297 FALSE, FALSE, TRUE);
8298 if (opt != NULL)
8299 func_desc_adjust (opt, info);
8300 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8301 FALSE, FALSE, TRUE);
8302 if (opt_fd != NULL
8303 && (opt_fd->root.type == bfd_link_hash_defined
8304 || opt_fd->root.type == bfd_link_hash_defweak))
8305 {
8306 /* If glibc supports an optimized __tls_get_addr call stub,
8307 signalled by the presence of __tls_get_addr_opt, and we'll
8308 be calling __tls_get_addr via a plt call stub, then
8309 make __tls_get_addr point to __tls_get_addr_opt. */
8310 tga_fd = &htab->tls_get_addr_fd->elf;
8311 if (htab->elf.dynamic_sections_created
8312 && tga_fd != NULL
8313 && (tga_fd->type == STT_FUNC
8314 || tga_fd->needs_plt)
8315 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8316 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8317 && tga_fd->root.type == bfd_link_hash_undefweak)))
8318 {
8319 struct plt_entry *ent;
8320
8321 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8322 if (ent->plt.refcount > 0)
8323 break;
8324 if (ent != NULL)
8325 {
8326 tga_fd->root.type = bfd_link_hash_indirect;
8327 tga_fd->root.u.i.link = &opt_fd->root;
8328 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8329 opt_fd->forced_local = 0;
8330 if (opt_fd->dynindx != -1)
8331 {
8332 /* Use __tls_get_addr_opt in dynamic relocations. */
8333 opt_fd->dynindx = -1;
8334 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8335 opt_fd->dynstr_index);
8336 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8337 return NULL;
8338 }
8339 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8340 tga = &htab->tls_get_addr->elf;
8341 if (opt != NULL && tga != NULL)
8342 {
8343 tga->root.type = bfd_link_hash_indirect;
8344 tga->root.u.i.link = &opt->root;
8345 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8346 opt->forced_local = 0;
8347 _bfd_elf_link_hash_hide_symbol (info, opt,
8348 tga->forced_local);
8349 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8350 }
8351 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8352 htab->tls_get_addr_fd->is_func_descriptor = 1;
8353 if (htab->tls_get_addr != NULL)
8354 {
8355 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8356 htab->tls_get_addr->is_func = 1;
8357 }
8358 }
8359 }
8360 }
8361 else if (htab->params->tls_get_addr_opt < 0)
8362 htab->params->tls_get_addr_opt = 0;
8363 }
8364 return _bfd_elf_tls_setup (info->output_bfd, info);
8365 }
8366
8367 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8368 HASH1 or HASH2. */
8369
8370 static bfd_boolean
8371 branch_reloc_hash_match (const bfd *ibfd,
8372 const Elf_Internal_Rela *rel,
8373 const struct ppc_link_hash_entry *hash1,
8374 const struct ppc_link_hash_entry *hash2)
8375 {
8376 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8377 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8378 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8379
8380 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8381 {
8382 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8383 struct elf_link_hash_entry *h;
8384
8385 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8386 h = elf_follow_link (h);
8387 if (h == &hash1->elf || h == &hash2->elf)
8388 return TRUE;
8389 }
8390 return FALSE;
8391 }
8392
8393 /* Run through all the TLS relocs looking for optimization
8394 opportunities. The linker has been hacked (see ppc64elf.em) to do
8395 a preliminary section layout so that we know the TLS segment
8396 offsets. We can't optimize earlier because some optimizations need
8397 to know the tp offset, and we need to optimize before allocating
8398 dynamic relocations. */
8399
8400 bfd_boolean
8401 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8402 {
8403 bfd *ibfd;
8404 asection *sec;
8405 struct ppc_link_hash_table *htab;
8406 unsigned char *toc_ref;
8407 int pass;
8408
8409 if (!bfd_link_executable (info))
8410 return TRUE;
8411
8412 htab = ppc_hash_table (info);
8413 if (htab == NULL)
8414 return FALSE;
8415
8416 /* Make two passes over the relocs. On the first pass, mark toc
8417 entries involved with tls relocs, and check that tls relocs
8418 involved in setting up a tls_get_addr call are indeed followed by
8419 such a call. If they are not, we can't do any tls optimization.
8420 On the second pass twiddle tls_mask flags to notify
8421 relocate_section that optimization can be done, and adjust got
8422 and plt refcounts. */
8423 toc_ref = NULL;
8424 for (pass = 0; pass < 2; ++pass)
8425 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8426 {
8427 Elf_Internal_Sym *locsyms = NULL;
8428 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8429
8430 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8431 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8432 {
8433 Elf_Internal_Rela *relstart, *rel, *relend;
8434 bfd_boolean found_tls_get_addr_arg = 0;
8435
8436 /* Read the relocations. */
8437 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8438 info->keep_memory);
8439 if (relstart == NULL)
8440 {
8441 free (toc_ref);
8442 return FALSE;
8443 }
8444
8445 relend = relstart + sec->reloc_count;
8446 for (rel = relstart; rel < relend; rel++)
8447 {
8448 enum elf_ppc64_reloc_type r_type;
8449 unsigned long r_symndx;
8450 struct elf_link_hash_entry *h;
8451 Elf_Internal_Sym *sym;
8452 asection *sym_sec;
8453 unsigned char *tls_mask;
8454 unsigned char tls_set, tls_clear, tls_type = 0;
8455 bfd_vma value;
8456 bfd_boolean ok_tprel, is_local;
8457 long toc_ref_index = 0;
8458 int expecting_tls_get_addr = 0;
8459 bfd_boolean ret = FALSE;
8460
8461 r_symndx = ELF64_R_SYM (rel->r_info);
8462 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8463 r_symndx, ibfd))
8464 {
8465 err_free_rel:
8466 if (elf_section_data (sec)->relocs != relstart)
8467 free (relstart);
8468 if (toc_ref != NULL)
8469 free (toc_ref);
8470 if (locsyms != NULL
8471 && (elf_symtab_hdr (ibfd).contents
8472 != (unsigned char *) locsyms))
8473 free (locsyms);
8474 return ret;
8475 }
8476
8477 if (h != NULL)
8478 {
8479 if (h->root.type == bfd_link_hash_defined
8480 || h->root.type == bfd_link_hash_defweak)
8481 value = h->root.u.def.value;
8482 else if (h->root.type == bfd_link_hash_undefweak)
8483 value = 0;
8484 else
8485 {
8486 found_tls_get_addr_arg = 0;
8487 continue;
8488 }
8489 }
8490 else
8491 /* Symbols referenced by TLS relocs must be of type
8492 STT_TLS. So no need for .opd local sym adjust. */
8493 value = sym->st_value;
8494
8495 ok_tprel = FALSE;
8496 is_local = FALSE;
8497 if (h == NULL
8498 || !h->def_dynamic)
8499 {
8500 is_local = TRUE;
8501 if (h != NULL
8502 && h->root.type == bfd_link_hash_undefweak)
8503 ok_tprel = TRUE;
8504 else if (sym_sec != NULL
8505 && sym_sec->output_section != NULL)
8506 {
8507 value += sym_sec->output_offset;
8508 value += sym_sec->output_section->vma;
8509 value -= htab->elf.tls_sec->vma;
8510 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8511 < (bfd_vma) 1 << 32);
8512 }
8513 }
8514
8515 r_type = ELF64_R_TYPE (rel->r_info);
8516 /* If this section has old-style __tls_get_addr calls
8517 without marker relocs, then check that each
8518 __tls_get_addr call reloc is preceded by a reloc
8519 that conceivably belongs to the __tls_get_addr arg
8520 setup insn. If we don't find matching arg setup
8521 relocs, don't do any tls optimization. */
8522 if (pass == 0
8523 && sec->has_tls_get_addr_call
8524 && h != NULL
8525 && (h == &htab->tls_get_addr->elf
8526 || h == &htab->tls_get_addr_fd->elf)
8527 && !found_tls_get_addr_arg
8528 && is_branch_reloc (r_type))
8529 {
8530 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8531 "TLS optimization disabled\n"),
8532 ibfd, sec, rel->r_offset);
8533 ret = TRUE;
8534 goto err_free_rel;
8535 }
8536
8537 found_tls_get_addr_arg = 0;
8538 switch (r_type)
8539 {
8540 case R_PPC64_GOT_TLSLD16:
8541 case R_PPC64_GOT_TLSLD16_LO:
8542 expecting_tls_get_addr = 1;
8543 found_tls_get_addr_arg = 1;
8544 /* Fall through. */
8545
8546 case R_PPC64_GOT_TLSLD16_HI:
8547 case R_PPC64_GOT_TLSLD16_HA:
8548 /* These relocs should never be against a symbol
8549 defined in a shared lib. Leave them alone if
8550 that turns out to be the case. */
8551 if (!is_local)
8552 continue;
8553
8554 /* LD -> LE */
8555 tls_set = 0;
8556 tls_clear = TLS_LD;
8557 tls_type = TLS_TLS | TLS_LD;
8558 break;
8559
8560 case R_PPC64_GOT_TLSGD16:
8561 case R_PPC64_GOT_TLSGD16_LO:
8562 expecting_tls_get_addr = 1;
8563 found_tls_get_addr_arg = 1;
8564 /* Fall through. */
8565
8566 case R_PPC64_GOT_TLSGD16_HI:
8567 case R_PPC64_GOT_TLSGD16_HA:
8568 if (ok_tprel)
8569 /* GD -> LE */
8570 tls_set = 0;
8571 else
8572 /* GD -> IE */
8573 tls_set = TLS_TLS | TLS_TPRELGD;
8574 tls_clear = TLS_GD;
8575 tls_type = TLS_TLS | TLS_GD;
8576 break;
8577
8578 case R_PPC64_GOT_TPREL16_DS:
8579 case R_PPC64_GOT_TPREL16_LO_DS:
8580 case R_PPC64_GOT_TPREL16_HI:
8581 case R_PPC64_GOT_TPREL16_HA:
8582 if (ok_tprel)
8583 {
8584 /* IE -> LE */
8585 tls_set = 0;
8586 tls_clear = TLS_TPREL;
8587 tls_type = TLS_TLS | TLS_TPREL;
8588 break;
8589 }
8590 continue;
8591
8592 case R_PPC64_TLSGD:
8593 case R_PPC64_TLSLD:
8594 found_tls_get_addr_arg = 1;
8595 /* Fall through. */
8596
8597 case R_PPC64_TLS:
8598 case R_PPC64_TOC16:
8599 case R_PPC64_TOC16_LO:
8600 if (sym_sec == NULL || sym_sec != toc)
8601 continue;
8602
8603 /* Mark this toc entry as referenced by a TLS
8604 code sequence. We can do that now in the
8605 case of R_PPC64_TLS, and after checking for
8606 tls_get_addr for the TOC16 relocs. */
8607 if (toc_ref == NULL)
8608 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8609 if (toc_ref == NULL)
8610 goto err_free_rel;
8611
8612 if (h != NULL)
8613 value = h->root.u.def.value;
8614 else
8615 value = sym->st_value;
8616 value += rel->r_addend;
8617 if (value % 8 != 0)
8618 continue;
8619 BFD_ASSERT (value < toc->size
8620 && toc->output_offset % 8 == 0);
8621 toc_ref_index = (value + toc->output_offset) / 8;
8622 if (r_type == R_PPC64_TLS
8623 || r_type == R_PPC64_TLSGD
8624 || r_type == R_PPC64_TLSLD)
8625 {
8626 toc_ref[toc_ref_index] = 1;
8627 continue;
8628 }
8629
8630 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8631 continue;
8632
8633 tls_set = 0;
8634 tls_clear = 0;
8635 expecting_tls_get_addr = 2;
8636 break;
8637
8638 case R_PPC64_TPREL64:
8639 if (pass == 0
8640 || sec != toc
8641 || toc_ref == NULL
8642 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8643 continue;
8644 if (ok_tprel)
8645 {
8646 /* IE -> LE */
8647 tls_set = TLS_EXPLICIT;
8648 tls_clear = TLS_TPREL;
8649 break;
8650 }
8651 continue;
8652
8653 case R_PPC64_DTPMOD64:
8654 if (pass == 0
8655 || sec != toc
8656 || toc_ref == NULL
8657 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8658 continue;
8659 if (rel + 1 < relend
8660 && (rel[1].r_info
8661 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8662 && rel[1].r_offset == rel->r_offset + 8)
8663 {
8664 if (ok_tprel)
8665 /* GD -> LE */
8666 tls_set = TLS_EXPLICIT | TLS_GD;
8667 else
8668 /* GD -> IE */
8669 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8670 tls_clear = TLS_GD;
8671 }
8672 else
8673 {
8674 if (!is_local)
8675 continue;
8676
8677 /* LD -> LE */
8678 tls_set = TLS_EXPLICIT;
8679 tls_clear = TLS_LD;
8680 }
8681 break;
8682
8683 default:
8684 continue;
8685 }
8686
8687 if (pass == 0)
8688 {
8689 if (!expecting_tls_get_addr
8690 || !sec->has_tls_get_addr_call)
8691 continue;
8692
8693 if (rel + 1 < relend
8694 && branch_reloc_hash_match (ibfd, rel + 1,
8695 htab->tls_get_addr,
8696 htab->tls_get_addr_fd))
8697 {
8698 if (expecting_tls_get_addr == 2)
8699 {
8700 /* Check for toc tls entries. */
8701 unsigned char *toc_tls;
8702 int retval;
8703
8704 retval = get_tls_mask (&toc_tls, NULL, NULL,
8705 &locsyms,
8706 rel, ibfd);
8707 if (retval == 0)
8708 goto err_free_rel;
8709 if (toc_tls != NULL)
8710 {
8711 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8712 found_tls_get_addr_arg = 1;
8713 if (retval > 1)
8714 toc_ref[toc_ref_index] = 1;
8715 }
8716 }
8717 continue;
8718 }
8719
8720 if (expecting_tls_get_addr != 1)
8721 continue;
8722
8723 /* Uh oh, we didn't find the expected call. We
8724 could just mark this symbol to exclude it
8725 from tls optimization but it's safer to skip
8726 the entire optimization. */
8727 /* xgettext:c-format */
8728 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8729 "TLS optimization disabled\n"),
8730 ibfd, sec, rel->r_offset);
8731 ret = TRUE;
8732 goto err_free_rel;
8733 }
8734
8735 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8736 {
8737 struct plt_entry *ent;
8738 for (ent = htab->tls_get_addr->elf.plt.plist;
8739 ent != NULL;
8740 ent = ent->next)
8741 if (ent->addend == 0)
8742 {
8743 if (ent->plt.refcount > 0)
8744 {
8745 ent->plt.refcount -= 1;
8746 expecting_tls_get_addr = 0;
8747 }
8748 break;
8749 }
8750 }
8751
8752 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8753 {
8754 struct plt_entry *ent;
8755 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8756 ent != NULL;
8757 ent = ent->next)
8758 if (ent->addend == 0)
8759 {
8760 if (ent->plt.refcount > 0)
8761 ent->plt.refcount -= 1;
8762 break;
8763 }
8764 }
8765
8766 if (tls_clear == 0)
8767 continue;
8768
8769 if ((tls_set & TLS_EXPLICIT) == 0)
8770 {
8771 struct got_entry *ent;
8772
8773 /* Adjust got entry for this reloc. */
8774 if (h != NULL)
8775 ent = h->got.glist;
8776 else
8777 ent = elf_local_got_ents (ibfd)[r_symndx];
8778
8779 for (; ent != NULL; ent = ent->next)
8780 if (ent->addend == rel->r_addend
8781 && ent->owner == ibfd
8782 && ent->tls_type == tls_type)
8783 break;
8784 if (ent == NULL)
8785 abort ();
8786
8787 if (tls_set == 0)
8788 {
8789 /* We managed to get rid of a got entry. */
8790 if (ent->got.refcount > 0)
8791 ent->got.refcount -= 1;
8792 }
8793 }
8794 else
8795 {
8796 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8797 we'll lose one or two dyn relocs. */
8798 if (!dec_dynrel_count (rel->r_info, sec, info,
8799 NULL, h, sym))
8800 return FALSE;
8801
8802 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8803 {
8804 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8805 NULL, h, sym))
8806 return FALSE;
8807 }
8808 }
8809
8810 *tls_mask |= tls_set;
8811 *tls_mask &= ~tls_clear;
8812 }
8813
8814 if (elf_section_data (sec)->relocs != relstart)
8815 free (relstart);
8816 }
8817
8818 if (locsyms != NULL
8819 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8820 {
8821 if (!info->keep_memory)
8822 free (locsyms);
8823 else
8824 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8825 }
8826 }
8827
8828 if (toc_ref != NULL)
8829 free (toc_ref);
8830 return TRUE;
8831 }
8832
8833 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8834 the values of any global symbols in a toc section that has been
8835 edited. Globals in toc sections should be a rarity, so this function
8836 sets a flag if any are found in toc sections other than the one just
8837 edited, so that futher hash table traversals can be avoided. */
8838
8839 struct adjust_toc_info
8840 {
8841 asection *toc;
8842 unsigned long *skip;
8843 bfd_boolean global_toc_syms;
8844 };
8845
8846 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8847
8848 static bfd_boolean
8849 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8850 {
8851 struct ppc_link_hash_entry *eh;
8852 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8853 unsigned long i;
8854
8855 if (h->root.type != bfd_link_hash_defined
8856 && h->root.type != bfd_link_hash_defweak)
8857 return TRUE;
8858
8859 eh = (struct ppc_link_hash_entry *) h;
8860 if (eh->adjust_done)
8861 return TRUE;
8862
8863 if (eh->elf.root.u.def.section == toc_inf->toc)
8864 {
8865 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8866 i = toc_inf->toc->rawsize >> 3;
8867 else
8868 i = eh->elf.root.u.def.value >> 3;
8869
8870 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8871 {
8872 _bfd_error_handler
8873 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8874 do
8875 ++i;
8876 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8877 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8878 }
8879
8880 eh->elf.root.u.def.value -= toc_inf->skip[i];
8881 eh->adjust_done = 1;
8882 }
8883 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8884 toc_inf->global_toc_syms = TRUE;
8885
8886 return TRUE;
8887 }
8888
8889 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8890 on a _LO variety toc/got reloc. */
8891
8892 static bfd_boolean
8893 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8894 {
8895 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8896 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8897 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8898 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8899 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8900 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8901 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8902 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8903 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8904 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8905 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8906 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8907 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8908 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8909 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8910 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8911 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8912 /* Exclude lfqu by testing reloc. If relocs are ever
8913 defined for the reduced D field in psq_lu then those
8914 will need testing too. */
8915 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8916 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8917 && (insn & 1) == 0)
8918 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8919 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8920 /* Exclude stfqu. psq_stu as above for psq_lu. */
8921 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8922 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8923 && (insn & 1) == 0));
8924 }
8925
8926 /* Examine all relocs referencing .toc sections in order to remove
8927 unused .toc entries. */
8928
8929 bfd_boolean
8930 ppc64_elf_edit_toc (struct bfd_link_info *info)
8931 {
8932 bfd *ibfd;
8933 struct adjust_toc_info toc_inf;
8934 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8935
8936 htab->do_toc_opt = 1;
8937 toc_inf.global_toc_syms = TRUE;
8938 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8939 {
8940 asection *toc, *sec;
8941 Elf_Internal_Shdr *symtab_hdr;
8942 Elf_Internal_Sym *local_syms;
8943 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8944 unsigned long *skip, *drop;
8945 unsigned char *used;
8946 unsigned char *keep, last, some_unused;
8947
8948 if (!is_ppc64_elf (ibfd))
8949 continue;
8950
8951 toc = bfd_get_section_by_name (ibfd, ".toc");
8952 if (toc == NULL
8953 || toc->size == 0
8954 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8955 || discarded_section (toc))
8956 continue;
8957
8958 toc_relocs = NULL;
8959 local_syms = NULL;
8960 symtab_hdr = &elf_symtab_hdr (ibfd);
8961
8962 /* Look at sections dropped from the final link. */
8963 skip = NULL;
8964 relstart = NULL;
8965 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8966 {
8967 if (sec->reloc_count == 0
8968 || !discarded_section (sec)
8969 || get_opd_info (sec)
8970 || (sec->flags & SEC_ALLOC) == 0
8971 || (sec->flags & SEC_DEBUGGING) != 0)
8972 continue;
8973
8974 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8975 if (relstart == NULL)
8976 goto error_ret;
8977
8978 /* Run through the relocs to see which toc entries might be
8979 unused. */
8980 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8981 {
8982 enum elf_ppc64_reloc_type r_type;
8983 unsigned long r_symndx;
8984 asection *sym_sec;
8985 struct elf_link_hash_entry *h;
8986 Elf_Internal_Sym *sym;
8987 bfd_vma val;
8988
8989 r_type = ELF64_R_TYPE (rel->r_info);
8990 switch (r_type)
8991 {
8992 default:
8993 continue;
8994
8995 case R_PPC64_TOC16:
8996 case R_PPC64_TOC16_LO:
8997 case R_PPC64_TOC16_HI:
8998 case R_PPC64_TOC16_HA:
8999 case R_PPC64_TOC16_DS:
9000 case R_PPC64_TOC16_LO_DS:
9001 break;
9002 }
9003
9004 r_symndx = ELF64_R_SYM (rel->r_info);
9005 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9006 r_symndx, ibfd))
9007 goto error_ret;
9008
9009 if (sym_sec != toc)
9010 continue;
9011
9012 if (h != NULL)
9013 val = h->root.u.def.value;
9014 else
9015 val = sym->st_value;
9016 val += rel->r_addend;
9017
9018 if (val >= toc->size)
9019 continue;
9020
9021 /* Anything in the toc ought to be aligned to 8 bytes.
9022 If not, don't mark as unused. */
9023 if (val & 7)
9024 continue;
9025
9026 if (skip == NULL)
9027 {
9028 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9029 if (skip == NULL)
9030 goto error_ret;
9031 }
9032
9033 skip[val >> 3] = ref_from_discarded;
9034 }
9035
9036 if (elf_section_data (sec)->relocs != relstart)
9037 free (relstart);
9038 }
9039
9040 /* For largetoc loads of address constants, we can convert
9041 . addis rx,2,addr@got@ha
9042 . ld ry,addr@got@l(rx)
9043 to
9044 . addis rx,2,addr@toc@ha
9045 . addi ry,rx,addr@toc@l
9046 when addr is within 2G of the toc pointer. This then means
9047 that the word storing "addr" in the toc is no longer needed. */
9048
9049 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9050 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9051 && toc->reloc_count != 0)
9052 {
9053 /* Read toc relocs. */
9054 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9055 info->keep_memory);
9056 if (toc_relocs == NULL)
9057 goto error_ret;
9058
9059 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9060 {
9061 enum elf_ppc64_reloc_type r_type;
9062 unsigned long r_symndx;
9063 asection *sym_sec;
9064 struct elf_link_hash_entry *h;
9065 Elf_Internal_Sym *sym;
9066 bfd_vma val, addr;
9067
9068 r_type = ELF64_R_TYPE (rel->r_info);
9069 if (r_type != R_PPC64_ADDR64)
9070 continue;
9071
9072 r_symndx = ELF64_R_SYM (rel->r_info);
9073 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9074 r_symndx, ibfd))
9075 goto error_ret;
9076
9077 if (sym_sec == NULL
9078 || sym_sec->output_section == NULL
9079 || discarded_section (sym_sec))
9080 continue;
9081
9082 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9083 continue;
9084
9085 if (h != NULL)
9086 {
9087 if (h->type == STT_GNU_IFUNC)
9088 continue;
9089 val = h->root.u.def.value;
9090 }
9091 else
9092 {
9093 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9094 continue;
9095 val = sym->st_value;
9096 }
9097 val += rel->r_addend;
9098 val += sym_sec->output_section->vma + sym_sec->output_offset;
9099
9100 /* We don't yet know the exact toc pointer value, but we
9101 know it will be somewhere in the toc section. Don't
9102 optimize if the difference from any possible toc
9103 pointer is outside [ff..f80008000, 7fff7fff]. */
9104 addr = toc->output_section->vma + TOC_BASE_OFF;
9105 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9106 continue;
9107
9108 addr = toc->output_section->vma + toc->output_section->rawsize;
9109 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9110 continue;
9111
9112 if (skip == NULL)
9113 {
9114 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9115 if (skip == NULL)
9116 goto error_ret;
9117 }
9118
9119 skip[rel->r_offset >> 3]
9120 |= can_optimize | ((rel - toc_relocs) << 2);
9121 }
9122 }
9123
9124 if (skip == NULL)
9125 continue;
9126
9127 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9128 if (used == NULL)
9129 {
9130 error_ret:
9131 if (local_syms != NULL
9132 && symtab_hdr->contents != (unsigned char *) local_syms)
9133 free (local_syms);
9134 if (sec != NULL
9135 && relstart != NULL
9136 && elf_section_data (sec)->relocs != relstart)
9137 free (relstart);
9138 if (toc_relocs != NULL
9139 && elf_section_data (toc)->relocs != toc_relocs)
9140 free (toc_relocs);
9141 if (skip != NULL)
9142 free (skip);
9143 return FALSE;
9144 }
9145
9146 /* Now check all kept sections that might reference the toc.
9147 Check the toc itself last. */
9148 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9149 : ibfd->sections);
9150 sec != NULL;
9151 sec = (sec == toc ? NULL
9152 : sec->next == NULL ? toc
9153 : sec->next == toc && toc->next ? toc->next
9154 : sec->next))
9155 {
9156 int repeat;
9157
9158 if (sec->reloc_count == 0
9159 || discarded_section (sec)
9160 || get_opd_info (sec)
9161 || (sec->flags & SEC_ALLOC) == 0
9162 || (sec->flags & SEC_DEBUGGING) != 0)
9163 continue;
9164
9165 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9166 info->keep_memory);
9167 if (relstart == NULL)
9168 {
9169 free (used);
9170 goto error_ret;
9171 }
9172
9173 /* Mark toc entries referenced as used. */
9174 do
9175 {
9176 repeat = 0;
9177 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9178 {
9179 enum elf_ppc64_reloc_type r_type;
9180 unsigned long r_symndx;
9181 asection *sym_sec;
9182 struct elf_link_hash_entry *h;
9183 Elf_Internal_Sym *sym;
9184 bfd_vma val;
9185 enum {no_check, check_lo, check_ha} insn_check;
9186
9187 r_type = ELF64_R_TYPE (rel->r_info);
9188 switch (r_type)
9189 {
9190 default:
9191 insn_check = no_check;
9192 break;
9193
9194 case R_PPC64_GOT_TLSLD16_HA:
9195 case R_PPC64_GOT_TLSGD16_HA:
9196 case R_PPC64_GOT_TPREL16_HA:
9197 case R_PPC64_GOT_DTPREL16_HA:
9198 case R_PPC64_GOT16_HA:
9199 case R_PPC64_TOC16_HA:
9200 insn_check = check_ha;
9201 break;
9202
9203 case R_PPC64_GOT_TLSLD16_LO:
9204 case R_PPC64_GOT_TLSGD16_LO:
9205 case R_PPC64_GOT_TPREL16_LO_DS:
9206 case R_PPC64_GOT_DTPREL16_LO_DS:
9207 case R_PPC64_GOT16_LO:
9208 case R_PPC64_GOT16_LO_DS:
9209 case R_PPC64_TOC16_LO:
9210 case R_PPC64_TOC16_LO_DS:
9211 insn_check = check_lo;
9212 break;
9213 }
9214
9215 if (insn_check != no_check)
9216 {
9217 bfd_vma off = rel->r_offset & ~3;
9218 unsigned char buf[4];
9219 unsigned int insn;
9220
9221 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9222 {
9223 free (used);
9224 goto error_ret;
9225 }
9226 insn = bfd_get_32 (ibfd, buf);
9227 if (insn_check == check_lo
9228 ? !ok_lo_toc_insn (insn, r_type)
9229 : ((insn & ((0x3f << 26) | 0x1f << 16))
9230 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9231 {
9232 char str[12];
9233
9234 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9235 sprintf (str, "%#08x", insn);
9236 info->callbacks->einfo
9237 /* xgettext:c-format */
9238 (_("%P: %H: toc optimization is not supported for"
9239 " %s instruction.\n"),
9240 ibfd, sec, rel->r_offset & ~3, str);
9241 }
9242 }
9243
9244 switch (r_type)
9245 {
9246 case R_PPC64_TOC16:
9247 case R_PPC64_TOC16_LO:
9248 case R_PPC64_TOC16_HI:
9249 case R_PPC64_TOC16_HA:
9250 case R_PPC64_TOC16_DS:
9251 case R_PPC64_TOC16_LO_DS:
9252 /* In case we're taking addresses of toc entries. */
9253 case R_PPC64_ADDR64:
9254 break;
9255
9256 default:
9257 continue;
9258 }
9259
9260 r_symndx = ELF64_R_SYM (rel->r_info);
9261 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9262 r_symndx, ibfd))
9263 {
9264 free (used);
9265 goto error_ret;
9266 }
9267
9268 if (sym_sec != toc)
9269 continue;
9270
9271 if (h != NULL)
9272 val = h->root.u.def.value;
9273 else
9274 val = sym->st_value;
9275 val += rel->r_addend;
9276
9277 if (val >= toc->size)
9278 continue;
9279
9280 if ((skip[val >> 3] & can_optimize) != 0)
9281 {
9282 bfd_vma off;
9283 unsigned char opc;
9284
9285 switch (r_type)
9286 {
9287 case R_PPC64_TOC16_HA:
9288 break;
9289
9290 case R_PPC64_TOC16_LO_DS:
9291 off = rel->r_offset;
9292 off += (bfd_big_endian (ibfd) ? -2 : 3);
9293 if (!bfd_get_section_contents (ibfd, sec, &opc,
9294 off, 1))
9295 {
9296 free (used);
9297 goto error_ret;
9298 }
9299 if ((opc & (0x3f << 2)) == (58u << 2))
9300 break;
9301 /* Fall through. */
9302
9303 default:
9304 /* Wrong sort of reloc, or not a ld. We may
9305 as well clear ref_from_discarded too. */
9306 skip[val >> 3] = 0;
9307 }
9308 }
9309
9310 if (sec != toc)
9311 used[val >> 3] = 1;
9312 /* For the toc section, we only mark as used if this
9313 entry itself isn't unused. */
9314 else if ((used[rel->r_offset >> 3]
9315 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9316 && !used[val >> 3])
9317 {
9318 /* Do all the relocs again, to catch reference
9319 chains. */
9320 repeat = 1;
9321 used[val >> 3] = 1;
9322 }
9323 }
9324 }
9325 while (repeat);
9326
9327 if (elf_section_data (sec)->relocs != relstart)
9328 free (relstart);
9329 }
9330
9331 /* Merge the used and skip arrays. Assume that TOC
9332 doublewords not appearing as either used or unused belong
9333 to to an entry more than one doubleword in size. */
9334 for (drop = skip, keep = used, last = 0, some_unused = 0;
9335 drop < skip + (toc->size + 7) / 8;
9336 ++drop, ++keep)
9337 {
9338 if (*keep)
9339 {
9340 *drop &= ~ref_from_discarded;
9341 if ((*drop & can_optimize) != 0)
9342 some_unused = 1;
9343 last = 0;
9344 }
9345 else if ((*drop & ref_from_discarded) != 0)
9346 {
9347 some_unused = 1;
9348 last = ref_from_discarded;
9349 }
9350 else
9351 *drop = last;
9352 }
9353
9354 free (used);
9355
9356 if (some_unused)
9357 {
9358 bfd_byte *contents, *src;
9359 unsigned long off;
9360 Elf_Internal_Sym *sym;
9361 bfd_boolean local_toc_syms = FALSE;
9362
9363 /* Shuffle the toc contents, and at the same time convert the
9364 skip array from booleans into offsets. */
9365 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9366 goto error_ret;
9367
9368 elf_section_data (toc)->this_hdr.contents = contents;
9369
9370 for (src = contents, off = 0, drop = skip;
9371 src < contents + toc->size;
9372 src += 8, ++drop)
9373 {
9374 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9375 off += 8;
9376 else if (off != 0)
9377 {
9378 *drop = off;
9379 memcpy (src - off, src, 8);
9380 }
9381 }
9382 *drop = off;
9383 toc->rawsize = toc->size;
9384 toc->size = src - contents - off;
9385
9386 /* Adjust addends for relocs against the toc section sym,
9387 and optimize any accesses we can. */
9388 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9389 {
9390 if (sec->reloc_count == 0
9391 || discarded_section (sec))
9392 continue;
9393
9394 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9395 info->keep_memory);
9396 if (relstart == NULL)
9397 goto error_ret;
9398
9399 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9400 {
9401 enum elf_ppc64_reloc_type r_type;
9402 unsigned long r_symndx;
9403 asection *sym_sec;
9404 struct elf_link_hash_entry *h;
9405 bfd_vma val;
9406
9407 r_type = ELF64_R_TYPE (rel->r_info);
9408 switch (r_type)
9409 {
9410 default:
9411 continue;
9412
9413 case R_PPC64_TOC16:
9414 case R_PPC64_TOC16_LO:
9415 case R_PPC64_TOC16_HI:
9416 case R_PPC64_TOC16_HA:
9417 case R_PPC64_TOC16_DS:
9418 case R_PPC64_TOC16_LO_DS:
9419 case R_PPC64_ADDR64:
9420 break;
9421 }
9422
9423 r_symndx = ELF64_R_SYM (rel->r_info);
9424 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9425 r_symndx, ibfd))
9426 goto error_ret;
9427
9428 if (sym_sec != toc)
9429 continue;
9430
9431 if (h != NULL)
9432 val = h->root.u.def.value;
9433 else
9434 {
9435 val = sym->st_value;
9436 if (val != 0)
9437 local_toc_syms = TRUE;
9438 }
9439
9440 val += rel->r_addend;
9441
9442 if (val > toc->rawsize)
9443 val = toc->rawsize;
9444 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9445 continue;
9446 else if ((skip[val >> 3] & can_optimize) != 0)
9447 {
9448 Elf_Internal_Rela *tocrel
9449 = toc_relocs + (skip[val >> 3] >> 2);
9450 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9451
9452 switch (r_type)
9453 {
9454 case R_PPC64_TOC16_HA:
9455 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9456 break;
9457
9458 case R_PPC64_TOC16_LO_DS:
9459 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9460 break;
9461
9462 default:
9463 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9464 ppc_howto_init ();
9465 info->callbacks->einfo
9466 /* xgettext:c-format */
9467 (_("%P: %H: %s references "
9468 "optimized away TOC entry\n"),
9469 ibfd, sec, rel->r_offset,
9470 ppc64_elf_howto_table[r_type]->name);
9471 bfd_set_error (bfd_error_bad_value);
9472 goto error_ret;
9473 }
9474 rel->r_addend = tocrel->r_addend;
9475 elf_section_data (sec)->relocs = relstart;
9476 continue;
9477 }
9478
9479 if (h != NULL || sym->st_value != 0)
9480 continue;
9481
9482 rel->r_addend -= skip[val >> 3];
9483 elf_section_data (sec)->relocs = relstart;
9484 }
9485
9486 if (elf_section_data (sec)->relocs != relstart)
9487 free (relstart);
9488 }
9489
9490 /* We shouldn't have local or global symbols defined in the TOC,
9491 but handle them anyway. */
9492 if (local_syms != NULL)
9493 for (sym = local_syms;
9494 sym < local_syms + symtab_hdr->sh_info;
9495 ++sym)
9496 if (sym->st_value != 0
9497 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9498 {
9499 unsigned long i;
9500
9501 if (sym->st_value > toc->rawsize)
9502 i = toc->rawsize >> 3;
9503 else
9504 i = sym->st_value >> 3;
9505
9506 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9507 {
9508 if (local_toc_syms)
9509 _bfd_error_handler
9510 (_("%s defined on removed toc entry"),
9511 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9512 do
9513 ++i;
9514 while ((skip[i] & (ref_from_discarded | can_optimize)));
9515 sym->st_value = (bfd_vma) i << 3;
9516 }
9517
9518 sym->st_value -= skip[i];
9519 symtab_hdr->contents = (unsigned char *) local_syms;
9520 }
9521
9522 /* Adjust any global syms defined in this toc input section. */
9523 if (toc_inf.global_toc_syms)
9524 {
9525 toc_inf.toc = toc;
9526 toc_inf.skip = skip;
9527 toc_inf.global_toc_syms = FALSE;
9528 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9529 &toc_inf);
9530 }
9531
9532 if (toc->reloc_count != 0)
9533 {
9534 Elf_Internal_Shdr *rel_hdr;
9535 Elf_Internal_Rela *wrel;
9536 bfd_size_type sz;
9537
9538 /* Remove unused toc relocs, and adjust those we keep. */
9539 if (toc_relocs == NULL)
9540 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9541 info->keep_memory);
9542 if (toc_relocs == NULL)
9543 goto error_ret;
9544
9545 wrel = toc_relocs;
9546 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9547 if ((skip[rel->r_offset >> 3]
9548 & (ref_from_discarded | can_optimize)) == 0)
9549 {
9550 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9551 wrel->r_info = rel->r_info;
9552 wrel->r_addend = rel->r_addend;
9553 ++wrel;
9554 }
9555 else if (!dec_dynrel_count (rel->r_info, toc, info,
9556 &local_syms, NULL, NULL))
9557 goto error_ret;
9558
9559 elf_section_data (toc)->relocs = toc_relocs;
9560 toc->reloc_count = wrel - toc_relocs;
9561 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9562 sz = rel_hdr->sh_entsize;
9563 rel_hdr->sh_size = toc->reloc_count * sz;
9564 }
9565 }
9566 else if (toc_relocs != NULL
9567 && elf_section_data (toc)->relocs != toc_relocs)
9568 free (toc_relocs);
9569
9570 if (local_syms != NULL
9571 && symtab_hdr->contents != (unsigned char *) local_syms)
9572 {
9573 if (!info->keep_memory)
9574 free (local_syms);
9575 else
9576 symtab_hdr->contents = (unsigned char *) local_syms;
9577 }
9578 free (skip);
9579 }
9580
9581 return TRUE;
9582 }
9583
9584 /* Return true iff input section I references the TOC using
9585 instructions limited to +/-32k offsets. */
9586
9587 bfd_boolean
9588 ppc64_elf_has_small_toc_reloc (asection *i)
9589 {
9590 return (is_ppc64_elf (i->owner)
9591 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9592 }
9593
9594 /* Allocate space for one GOT entry. */
9595
9596 static void
9597 allocate_got (struct elf_link_hash_entry *h,
9598 struct bfd_link_info *info,
9599 struct got_entry *gent)
9600 {
9601 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9602 bfd_boolean dyn;
9603 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9604 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9605 ? 16 : 8);
9606 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9607 ? 2 : 1) * sizeof (Elf64_External_Rela);
9608 asection *got = ppc64_elf_tdata (gent->owner)->got;
9609
9610 gent->got.offset = got->size;
9611 got->size += entsize;
9612
9613 dyn = htab->elf.dynamic_sections_created;
9614 if (h->type == STT_GNU_IFUNC)
9615 {
9616 htab->elf.irelplt->size += rentsize;
9617 htab->got_reli_size += rentsize;
9618 }
9619 else if ((bfd_link_pic (info)
9620 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9621 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9622 || h->root.type != bfd_link_hash_undefweak))
9623 {
9624 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9625 relgot->size += rentsize;
9626 }
9627 }
9628
9629 /* This function merges got entries in the same toc group. */
9630
9631 static void
9632 merge_got_entries (struct got_entry **pent)
9633 {
9634 struct got_entry *ent, *ent2;
9635
9636 for (ent = *pent; ent != NULL; ent = ent->next)
9637 if (!ent->is_indirect)
9638 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9639 if (!ent2->is_indirect
9640 && ent2->addend == ent->addend
9641 && ent2->tls_type == ent->tls_type
9642 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9643 {
9644 ent2->is_indirect = TRUE;
9645 ent2->got.ent = ent;
9646 }
9647 }
9648
9649 /* Allocate space in .plt, .got and associated reloc sections for
9650 dynamic relocs. */
9651
9652 static bfd_boolean
9653 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9654 {
9655 struct bfd_link_info *info;
9656 struct ppc_link_hash_table *htab;
9657 asection *s;
9658 struct ppc_link_hash_entry *eh;
9659 struct got_entry **pgent, *gent;
9660
9661 if (h->root.type == bfd_link_hash_indirect)
9662 return TRUE;
9663
9664 info = (struct bfd_link_info *) inf;
9665 htab = ppc_hash_table (info);
9666 if (htab == NULL)
9667 return FALSE;
9668
9669 eh = (struct ppc_link_hash_entry *) h;
9670 /* Run through the TLS GD got entries first if we're changing them
9671 to TPREL. */
9672 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9673 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9674 if (gent->got.refcount > 0
9675 && (gent->tls_type & TLS_GD) != 0)
9676 {
9677 /* This was a GD entry that has been converted to TPREL. If
9678 there happens to be a TPREL entry we can use that one. */
9679 struct got_entry *ent;
9680 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9681 if (ent->got.refcount > 0
9682 && (ent->tls_type & TLS_TPREL) != 0
9683 && ent->addend == gent->addend
9684 && ent->owner == gent->owner)
9685 {
9686 gent->got.refcount = 0;
9687 break;
9688 }
9689
9690 /* If not, then we'll be using our own TPREL entry. */
9691 if (gent->got.refcount != 0)
9692 gent->tls_type = TLS_TLS | TLS_TPREL;
9693 }
9694
9695 /* Remove any list entry that won't generate a word in the GOT before
9696 we call merge_got_entries. Otherwise we risk merging to empty
9697 entries. */
9698 pgent = &h->got.glist;
9699 while ((gent = *pgent) != NULL)
9700 if (gent->got.refcount > 0)
9701 {
9702 if ((gent->tls_type & TLS_LD) != 0
9703 && !h->def_dynamic)
9704 {
9705 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9706 *pgent = gent->next;
9707 }
9708 else
9709 pgent = &gent->next;
9710 }
9711 else
9712 *pgent = gent->next;
9713
9714 if (!htab->do_multi_toc)
9715 merge_got_entries (&h->got.glist);
9716
9717 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9718 if (!gent->is_indirect)
9719 {
9720 /* Make sure this symbol is output as a dynamic symbol.
9721 Undefined weak syms won't yet be marked as dynamic,
9722 nor will all TLS symbols. */
9723 if (h->dynindx == -1
9724 && !h->forced_local
9725 && h->type != STT_GNU_IFUNC
9726 && htab->elf.dynamic_sections_created)
9727 {
9728 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9729 return FALSE;
9730 }
9731
9732 if (!is_ppc64_elf (gent->owner))
9733 abort ();
9734
9735 allocate_got (h, info, gent);
9736 }
9737
9738 if (!htab->elf.dynamic_sections_created
9739 && h->type != STT_GNU_IFUNC)
9740 eh->dyn_relocs = NULL;
9741
9742 if (eh->dyn_relocs != NULL)
9743 {
9744 struct elf_dyn_relocs *p, **pp;
9745
9746 /* In the shared -Bsymbolic case, discard space allocated for
9747 dynamic pc-relative relocs against symbols which turn out to
9748 be defined in regular objects. For the normal shared case,
9749 discard space for relocs that have become local due to symbol
9750 visibility changes. */
9751
9752 if (bfd_link_pic (info))
9753 {
9754 /* Relocs that use pc_count are those that appear on a call
9755 insn, or certain REL relocs (see must_be_dyn_reloc) that
9756 can be generated via assembly. We want calls to
9757 protected symbols to resolve directly to the function
9758 rather than going via the plt. If people want function
9759 pointer comparisons to work as expected then they should
9760 avoid writing weird assembly. */
9761 if (SYMBOL_CALLS_LOCAL (info, h))
9762 {
9763 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9764 {
9765 p->count -= p->pc_count;
9766 p->pc_count = 0;
9767 if (p->count == 0)
9768 *pp = p->next;
9769 else
9770 pp = &p->next;
9771 }
9772 }
9773
9774 /* Also discard relocs on undefined weak syms with
9775 non-default visibility. */
9776 if (eh->dyn_relocs != NULL
9777 && h->root.type == bfd_link_hash_undefweak)
9778 {
9779 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9780 eh->dyn_relocs = NULL;
9781
9782 /* Make sure this symbol is output as a dynamic symbol.
9783 Undefined weak syms won't yet be marked as dynamic. */
9784 else if (h->dynindx == -1
9785 && !h->forced_local)
9786 {
9787 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9788 return FALSE;
9789 }
9790 }
9791 }
9792 else if (h->type == STT_GNU_IFUNC)
9793 {
9794 /* A plt entry is always created when making direct calls to
9795 an ifunc, even when building a static executable, but
9796 that doesn't cover all cases. We may have only an ifunc
9797 initialised function pointer for a given ifunc symbol.
9798
9799 For ELFv2, dynamic relocations are not required when
9800 generating a global entry PLT stub. */
9801 if (abiversion (info->output_bfd) >= 2)
9802 {
9803 if (global_entry_stub (h))
9804 eh->dyn_relocs = NULL;
9805 }
9806
9807 /* For ELFv1 we have function descriptors. Descriptors need
9808 to be treated like PLT entries and thus have dynamic
9809 relocations. One exception is when the function
9810 descriptor is copied into .dynbss (which should only
9811 happen with ancient versions of gcc). */
9812 else if (h->needs_copy)
9813 eh->dyn_relocs = NULL;
9814 }
9815 else if (ELIMINATE_COPY_RELOCS)
9816 {
9817 /* For the non-pic case, discard space for relocs against
9818 symbols which turn out to need copy relocs or are not
9819 dynamic. */
9820
9821 /* First make sure this symbol is output as a dynamic symbol.
9822 Undefined weak syms won't yet be marked as dynamic. */
9823 if (h->root.type == bfd_link_hash_undefweak
9824 && !h->non_got_ref
9825 && !h->def_regular
9826 && h->dynindx == -1
9827 && !h->forced_local
9828 && !bfd_elf_link_record_dynamic_symbol (info, h))
9829 return FALSE;
9830
9831 if (h->non_got_ref
9832 || h->def_regular
9833 || h->dynindx == -1)
9834 eh->dyn_relocs = NULL;
9835 }
9836
9837 /* Finally, allocate space. */
9838 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9839 {
9840 asection *sreloc = elf_section_data (p->sec)->sreloc;
9841 if (eh->elf.type == STT_GNU_IFUNC)
9842 sreloc = htab->elf.irelplt;
9843 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9844 }
9845 }
9846
9847 if ((htab->elf.dynamic_sections_created
9848 && h->dynindx != -1
9849 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
9850 || h->type == STT_GNU_IFUNC)
9851 {
9852 struct plt_entry *pent;
9853 bfd_boolean doneone = FALSE;
9854 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9855 if (pent->plt.refcount > 0)
9856 {
9857 if (!htab->elf.dynamic_sections_created
9858 || h->dynindx == -1)
9859 {
9860 s = htab->elf.iplt;
9861 pent->plt.offset = s->size;
9862 s->size += PLT_ENTRY_SIZE (htab);
9863 s = htab->elf.irelplt;
9864 }
9865 else
9866 {
9867 /* If this is the first .plt entry, make room for the special
9868 first entry. */
9869 s = htab->elf.splt;
9870 if (s->size == 0)
9871 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9872
9873 pent->plt.offset = s->size;
9874
9875 /* Make room for this entry. */
9876 s->size += PLT_ENTRY_SIZE (htab);
9877
9878 /* Make room for the .glink code. */
9879 s = htab->glink;
9880 if (s->size == 0)
9881 s->size += GLINK_CALL_STUB_SIZE;
9882 if (htab->opd_abi)
9883 {
9884 /* We need bigger stubs past index 32767. */
9885 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9886 s->size += 4;
9887 s->size += 2*4;
9888 }
9889 else
9890 s->size += 4;
9891
9892 /* We also need to make an entry in the .rela.plt section. */
9893 s = htab->elf.srelplt;
9894 }
9895 s->size += sizeof (Elf64_External_Rela);
9896 doneone = TRUE;
9897 }
9898 else
9899 pent->plt.offset = (bfd_vma) -1;
9900 if (!doneone)
9901 {
9902 h->plt.plist = NULL;
9903 h->needs_plt = 0;
9904 }
9905 }
9906 else
9907 {
9908 h->plt.plist = NULL;
9909 h->needs_plt = 0;
9910 }
9911
9912 return TRUE;
9913 }
9914
9915 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9916 to set up space for global entry stubs. These are put in glink,
9917 after the branch table. */
9918
9919 static bfd_boolean
9920 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9921 {
9922 struct bfd_link_info *info;
9923 struct ppc_link_hash_table *htab;
9924 struct plt_entry *pent;
9925 asection *s;
9926
9927 if (h->root.type == bfd_link_hash_indirect)
9928 return TRUE;
9929
9930 if (!h->pointer_equality_needed)
9931 return TRUE;
9932
9933 if (h->def_regular)
9934 return TRUE;
9935
9936 info = inf;
9937 htab = ppc_hash_table (info);
9938 if (htab == NULL)
9939 return FALSE;
9940
9941 s = htab->glink;
9942 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9943 if (pent->plt.offset != (bfd_vma) -1
9944 && pent->addend == 0)
9945 {
9946 /* For ELFv2, if this symbol is not defined in a regular file
9947 and we are not generating a shared library or pie, then we
9948 need to define the symbol in the executable on a call stub.
9949 This is to avoid text relocations. */
9950 s->size = (s->size + 15) & -16;
9951 h->root.type = bfd_link_hash_defined;
9952 h->root.u.def.section = s;
9953 h->root.u.def.value = s->size;
9954 s->size += 16;
9955 break;
9956 }
9957 return TRUE;
9958 }
9959
9960 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9961 read-only sections. */
9962
9963 static bfd_boolean
9964 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9965 {
9966 if (h->root.type == bfd_link_hash_indirect)
9967 return TRUE;
9968
9969 if (readonly_dynrelocs (h))
9970 {
9971 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9972
9973 /* Not an error, just cut short the traversal. */
9974 return FALSE;
9975 }
9976 return TRUE;
9977 }
9978
9979 /* Set the sizes of the dynamic sections. */
9980
9981 static bfd_boolean
9982 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9983 struct bfd_link_info *info)
9984 {
9985 struct ppc_link_hash_table *htab;
9986 bfd *dynobj;
9987 asection *s;
9988 bfd_boolean relocs;
9989 bfd *ibfd;
9990 struct got_entry *first_tlsld;
9991
9992 htab = ppc_hash_table (info);
9993 if (htab == NULL)
9994 return FALSE;
9995
9996 dynobj = htab->elf.dynobj;
9997 if (dynobj == NULL)
9998 abort ();
9999
10000 if (htab->elf.dynamic_sections_created)
10001 {
10002 /* Set the contents of the .interp section to the interpreter. */
10003 if (bfd_link_executable (info) && !info->nointerp)
10004 {
10005 s = bfd_get_linker_section (dynobj, ".interp");
10006 if (s == NULL)
10007 abort ();
10008 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10009 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10010 }
10011 }
10012
10013 /* Set up .got offsets for local syms, and space for local dynamic
10014 relocs. */
10015 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10016 {
10017 struct got_entry **lgot_ents;
10018 struct got_entry **end_lgot_ents;
10019 struct plt_entry **local_plt;
10020 struct plt_entry **end_local_plt;
10021 unsigned char *lgot_masks;
10022 bfd_size_type locsymcount;
10023 Elf_Internal_Shdr *symtab_hdr;
10024
10025 if (!is_ppc64_elf (ibfd))
10026 continue;
10027
10028 for (s = ibfd->sections; s != NULL; s = s->next)
10029 {
10030 struct ppc_dyn_relocs *p;
10031
10032 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10033 {
10034 if (!bfd_is_abs_section (p->sec)
10035 && bfd_is_abs_section (p->sec->output_section))
10036 {
10037 /* Input section has been discarded, either because
10038 it is a copy of a linkonce section or due to
10039 linker script /DISCARD/, so we'll be discarding
10040 the relocs too. */
10041 }
10042 else if (p->count != 0)
10043 {
10044 asection *srel = elf_section_data (p->sec)->sreloc;
10045 if (p->ifunc)
10046 srel = htab->elf.irelplt;
10047 srel->size += p->count * sizeof (Elf64_External_Rela);
10048 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10049 info->flags |= DF_TEXTREL;
10050 }
10051 }
10052 }
10053
10054 lgot_ents = elf_local_got_ents (ibfd);
10055 if (!lgot_ents)
10056 continue;
10057
10058 symtab_hdr = &elf_symtab_hdr (ibfd);
10059 locsymcount = symtab_hdr->sh_info;
10060 end_lgot_ents = lgot_ents + locsymcount;
10061 local_plt = (struct plt_entry **) end_lgot_ents;
10062 end_local_plt = local_plt + locsymcount;
10063 lgot_masks = (unsigned char *) end_local_plt;
10064 s = ppc64_elf_tdata (ibfd)->got;
10065 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10066 {
10067 struct got_entry **pent, *ent;
10068
10069 pent = lgot_ents;
10070 while ((ent = *pent) != NULL)
10071 if (ent->got.refcount > 0)
10072 {
10073 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10074 {
10075 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10076 *pent = ent->next;
10077 }
10078 else
10079 {
10080 unsigned int ent_size = 8;
10081 unsigned int rel_size = sizeof (Elf64_External_Rela);
10082
10083 ent->got.offset = s->size;
10084 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10085 {
10086 ent_size *= 2;
10087 rel_size *= 2;
10088 }
10089 s->size += ent_size;
10090 if ((*lgot_masks & PLT_IFUNC) != 0)
10091 {
10092 htab->elf.irelplt->size += rel_size;
10093 htab->got_reli_size += rel_size;
10094 }
10095 else if (bfd_link_pic (info))
10096 {
10097 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10098 srel->size += rel_size;
10099 }
10100 pent = &ent->next;
10101 }
10102 }
10103 else
10104 *pent = ent->next;
10105 }
10106
10107 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10108 for (; local_plt < end_local_plt; ++local_plt)
10109 {
10110 struct plt_entry *ent;
10111
10112 for (ent = *local_plt; ent != NULL; ent = ent->next)
10113 if (ent->plt.refcount > 0)
10114 {
10115 s = htab->elf.iplt;
10116 ent->plt.offset = s->size;
10117 s->size += PLT_ENTRY_SIZE (htab);
10118
10119 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10120 }
10121 else
10122 ent->plt.offset = (bfd_vma) -1;
10123 }
10124 }
10125
10126 /* Allocate global sym .plt and .got entries, and space for global
10127 sym dynamic relocs. */
10128 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10129 /* Stash the end of glink branch table. */
10130 if (htab->glink != NULL)
10131 htab->glink->rawsize = htab->glink->size;
10132
10133 if (!htab->opd_abi && !bfd_link_pic (info))
10134 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10135
10136 first_tlsld = NULL;
10137 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10138 {
10139 struct got_entry *ent;
10140
10141 if (!is_ppc64_elf (ibfd))
10142 continue;
10143
10144 ent = ppc64_tlsld_got (ibfd);
10145 if (ent->got.refcount > 0)
10146 {
10147 if (!htab->do_multi_toc && first_tlsld != NULL)
10148 {
10149 ent->is_indirect = TRUE;
10150 ent->got.ent = first_tlsld;
10151 }
10152 else
10153 {
10154 if (first_tlsld == NULL)
10155 first_tlsld = ent;
10156 s = ppc64_elf_tdata (ibfd)->got;
10157 ent->got.offset = s->size;
10158 ent->owner = ibfd;
10159 s->size += 16;
10160 if (bfd_link_pic (info))
10161 {
10162 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10163 srel->size += sizeof (Elf64_External_Rela);
10164 }
10165 }
10166 }
10167 else
10168 ent->got.offset = (bfd_vma) -1;
10169 }
10170
10171 /* We now have determined the sizes of the various dynamic sections.
10172 Allocate memory for them. */
10173 relocs = FALSE;
10174 for (s = dynobj->sections; s != NULL; s = s->next)
10175 {
10176 if ((s->flags & SEC_LINKER_CREATED) == 0)
10177 continue;
10178
10179 if (s == htab->brlt || s == htab->relbrlt)
10180 /* These haven't been allocated yet; don't strip. */
10181 continue;
10182 else if (s == htab->elf.sgot
10183 || s == htab->elf.splt
10184 || s == htab->elf.iplt
10185 || s == htab->glink
10186 || s == htab->elf.sdynbss
10187 || s == htab->elf.sdynrelro)
10188 {
10189 /* Strip this section if we don't need it; see the
10190 comment below. */
10191 }
10192 else if (s == htab->glink_eh_frame)
10193 {
10194 if (!bfd_is_abs_section (s->output_section))
10195 /* Not sized yet. */
10196 continue;
10197 }
10198 else if (CONST_STRNEQ (s->name, ".rela"))
10199 {
10200 if (s->size != 0)
10201 {
10202 if (s != htab->elf.srelplt)
10203 relocs = TRUE;
10204
10205 /* We use the reloc_count field as a counter if we need
10206 to copy relocs into the output file. */
10207 s->reloc_count = 0;
10208 }
10209 }
10210 else
10211 {
10212 /* It's not one of our sections, so don't allocate space. */
10213 continue;
10214 }
10215
10216 if (s->size == 0)
10217 {
10218 /* If we don't need this section, strip it from the
10219 output file. This is mostly to handle .rela.bss and
10220 .rela.plt. We must create both sections in
10221 create_dynamic_sections, because they must be created
10222 before the linker maps input sections to output
10223 sections. The linker does that before
10224 adjust_dynamic_symbol is called, and it is that
10225 function which decides whether anything needs to go
10226 into these sections. */
10227 s->flags |= SEC_EXCLUDE;
10228 continue;
10229 }
10230
10231 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10232 continue;
10233
10234 /* Allocate memory for the section contents. We use bfd_zalloc
10235 here in case unused entries are not reclaimed before the
10236 section's contents are written out. This should not happen,
10237 but this way if it does we get a R_PPC64_NONE reloc in .rela
10238 sections instead of garbage.
10239 We also rely on the section contents being zero when writing
10240 the GOT and .dynrelro. */
10241 s->contents = bfd_zalloc (dynobj, s->size);
10242 if (s->contents == NULL)
10243 return FALSE;
10244 }
10245
10246 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10247 {
10248 if (!is_ppc64_elf (ibfd))
10249 continue;
10250
10251 s = ppc64_elf_tdata (ibfd)->got;
10252 if (s != NULL && s != htab->elf.sgot)
10253 {
10254 if (s->size == 0)
10255 s->flags |= SEC_EXCLUDE;
10256 else
10257 {
10258 s->contents = bfd_zalloc (ibfd, s->size);
10259 if (s->contents == NULL)
10260 return FALSE;
10261 }
10262 }
10263 s = ppc64_elf_tdata (ibfd)->relgot;
10264 if (s != NULL)
10265 {
10266 if (s->size == 0)
10267 s->flags |= SEC_EXCLUDE;
10268 else
10269 {
10270 s->contents = bfd_zalloc (ibfd, s->size);
10271 if (s->contents == NULL)
10272 return FALSE;
10273 relocs = TRUE;
10274 s->reloc_count = 0;
10275 }
10276 }
10277 }
10278
10279 if (htab->elf.dynamic_sections_created)
10280 {
10281 bfd_boolean tls_opt;
10282
10283 /* Add some entries to the .dynamic section. We fill in the
10284 values later, in ppc64_elf_finish_dynamic_sections, but we
10285 must add the entries now so that we get the correct size for
10286 the .dynamic section. The DT_DEBUG entry is filled in by the
10287 dynamic linker and used by the debugger. */
10288 #define add_dynamic_entry(TAG, VAL) \
10289 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10290
10291 if (bfd_link_executable (info))
10292 {
10293 if (!add_dynamic_entry (DT_DEBUG, 0))
10294 return FALSE;
10295 }
10296
10297 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10298 {
10299 if (!add_dynamic_entry (DT_PLTGOT, 0)
10300 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10301 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10302 || !add_dynamic_entry (DT_JMPREL, 0)
10303 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10304 return FALSE;
10305 }
10306
10307 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10308 {
10309 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10310 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10311 return FALSE;
10312 }
10313
10314 tls_opt = (htab->params->tls_get_addr_opt
10315 && htab->tls_get_addr_fd != NULL
10316 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10317 if (tls_opt || !htab->opd_abi)
10318 {
10319 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10320 return FALSE;
10321 }
10322
10323 if (relocs)
10324 {
10325 if (!add_dynamic_entry (DT_RELA, 0)
10326 || !add_dynamic_entry (DT_RELASZ, 0)
10327 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10328 return FALSE;
10329
10330 /* If any dynamic relocs apply to a read-only section,
10331 then we need a DT_TEXTREL entry. */
10332 if ((info->flags & DF_TEXTREL) == 0)
10333 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10334
10335 if ((info->flags & DF_TEXTREL) != 0)
10336 {
10337 if (!add_dynamic_entry (DT_TEXTREL, 0))
10338 return FALSE;
10339 }
10340 }
10341 }
10342 #undef add_dynamic_entry
10343
10344 return TRUE;
10345 }
10346
10347 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10348
10349 static bfd_boolean
10350 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10351 {
10352 if (h->plt.plist != NULL
10353 && !h->def_regular
10354 && !h->pointer_equality_needed)
10355 return FALSE;
10356
10357 return _bfd_elf_hash_symbol (h);
10358 }
10359
10360 /* Determine the type of stub needed, if any, for a call. */
10361
10362 static inline enum ppc_stub_type
10363 ppc_type_of_stub (asection *input_sec,
10364 const Elf_Internal_Rela *rel,
10365 struct ppc_link_hash_entry **hash,
10366 struct plt_entry **plt_ent,
10367 bfd_vma destination,
10368 unsigned long local_off)
10369 {
10370 struct ppc_link_hash_entry *h = *hash;
10371 bfd_vma location;
10372 bfd_vma branch_offset;
10373 bfd_vma max_branch_offset;
10374 enum elf_ppc64_reloc_type r_type;
10375
10376 if (h != NULL)
10377 {
10378 struct plt_entry *ent;
10379 struct ppc_link_hash_entry *fdh = h;
10380 if (h->oh != NULL
10381 && h->oh->is_func_descriptor)
10382 {
10383 fdh = ppc_follow_link (h->oh);
10384 *hash = fdh;
10385 }
10386
10387 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10388 if (ent->addend == rel->r_addend
10389 && ent->plt.offset != (bfd_vma) -1)
10390 {
10391 *plt_ent = ent;
10392 return ppc_stub_plt_call;
10393 }
10394
10395 /* Here, we know we don't have a plt entry. If we don't have a
10396 either a defined function descriptor or a defined entry symbol
10397 in a regular object file, then it is pointless trying to make
10398 any other type of stub. */
10399 if (!is_static_defined (&fdh->elf)
10400 && !is_static_defined (&h->elf))
10401 return ppc_stub_none;
10402 }
10403 else if (elf_local_got_ents (input_sec->owner) != NULL)
10404 {
10405 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10406 struct plt_entry **local_plt = (struct plt_entry **)
10407 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10408 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10409
10410 if (local_plt[r_symndx] != NULL)
10411 {
10412 struct plt_entry *ent;
10413
10414 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10415 if (ent->addend == rel->r_addend
10416 && ent->plt.offset != (bfd_vma) -1)
10417 {
10418 *plt_ent = ent;
10419 return ppc_stub_plt_call;
10420 }
10421 }
10422 }
10423
10424 /* Determine where the call point is. */
10425 location = (input_sec->output_offset
10426 + input_sec->output_section->vma
10427 + rel->r_offset);
10428
10429 branch_offset = destination - location;
10430 r_type = ELF64_R_TYPE (rel->r_info);
10431
10432 /* Determine if a long branch stub is needed. */
10433 max_branch_offset = 1 << 25;
10434 if (r_type != R_PPC64_REL24)
10435 max_branch_offset = 1 << 15;
10436
10437 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10438 /* We need a stub. Figure out whether a long_branch or plt_branch
10439 is needed later. */
10440 return ppc_stub_long_branch;
10441
10442 return ppc_stub_none;
10443 }
10444
10445 /* With power7 weakly ordered memory model, it is possible for ld.so
10446 to update a plt entry in one thread and have another thread see a
10447 stale zero toc entry. To avoid this we need some sort of acquire
10448 barrier in the call stub. One solution is to make the load of the
10449 toc word seem to appear to depend on the load of the function entry
10450 word. Another solution is to test for r2 being zero, and branch to
10451 the appropriate glink entry if so.
10452
10453 . fake dep barrier compare
10454 . ld 12,xxx(2) ld 12,xxx(2)
10455 . mtctr 12 mtctr 12
10456 . xor 11,12,12 ld 2,xxx+8(2)
10457 . add 2,2,11 cmpldi 2,0
10458 . ld 2,xxx+8(2) bnectr+
10459 . bctr b <glink_entry>
10460
10461 The solution involving the compare turns out to be faster, so
10462 that's what we use unless the branch won't reach. */
10463
10464 #define ALWAYS_USE_FAKE_DEP 0
10465 #define ALWAYS_EMIT_R2SAVE 0
10466
10467 #define PPC_LO(v) ((v) & 0xffff)
10468 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10469 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10470
10471 static inline unsigned int
10472 plt_stub_size (struct ppc_link_hash_table *htab,
10473 struct ppc_stub_hash_entry *stub_entry,
10474 bfd_vma off)
10475 {
10476 unsigned size = 12;
10477
10478 if (ALWAYS_EMIT_R2SAVE
10479 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10480 size += 4;
10481 if (PPC_HA (off) != 0)
10482 size += 4;
10483 if (htab->opd_abi)
10484 {
10485 size += 4;
10486 if (htab->params->plt_static_chain)
10487 size += 4;
10488 if (htab->params->plt_thread_safe
10489 && htab->elf.dynamic_sections_created
10490 && stub_entry->h != NULL
10491 && stub_entry->h->elf.dynindx != -1)
10492 size += 8;
10493 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10494 size += 4;
10495 }
10496 if (stub_entry->h != NULL
10497 && (stub_entry->h == htab->tls_get_addr_fd
10498 || stub_entry->h == htab->tls_get_addr)
10499 && htab->params->tls_get_addr_opt)
10500 size += 13 * 4;
10501 return size;
10502 }
10503
10504 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10505 then return the padding needed to do so. */
10506 static inline unsigned int
10507 plt_stub_pad (struct ppc_link_hash_table *htab,
10508 struct ppc_stub_hash_entry *stub_entry,
10509 bfd_vma plt_off)
10510 {
10511 int stub_align = 1 << htab->params->plt_stub_align;
10512 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10513 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10514
10515 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10516 > ((stub_size - 1) & -stub_align))
10517 return stub_align - (stub_off & (stub_align - 1));
10518 return 0;
10519 }
10520
10521 /* Build a .plt call stub. */
10522
10523 static inline bfd_byte *
10524 build_plt_stub (struct ppc_link_hash_table *htab,
10525 struct ppc_stub_hash_entry *stub_entry,
10526 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10527 {
10528 bfd *obfd = htab->params->stub_bfd;
10529 bfd_boolean plt_load_toc = htab->opd_abi;
10530 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10531 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10532 && htab->elf.dynamic_sections_created
10533 && stub_entry->h != NULL
10534 && stub_entry->h->elf.dynindx != -1);
10535 bfd_boolean use_fake_dep = plt_thread_safe;
10536 bfd_vma cmp_branch_off = 0;
10537
10538 if (!ALWAYS_USE_FAKE_DEP
10539 && plt_load_toc
10540 && plt_thread_safe
10541 && !((stub_entry->h == htab->tls_get_addr_fd
10542 || stub_entry->h == htab->tls_get_addr)
10543 && htab->params->tls_get_addr_opt))
10544 {
10545 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10546 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10547 / PLT_ENTRY_SIZE (htab));
10548 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10549 bfd_vma to, from;
10550
10551 if (pltindex > 32768)
10552 glinkoff += (pltindex - 32768) * 4;
10553 to = (glinkoff
10554 + htab->glink->output_offset
10555 + htab->glink->output_section->vma);
10556 from = (p - stub_entry->group->stub_sec->contents
10557 + 4 * (ALWAYS_EMIT_R2SAVE
10558 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10559 + 4 * (PPC_HA (offset) != 0)
10560 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10561 != PPC_HA (offset))
10562 + 4 * (plt_static_chain != 0)
10563 + 20
10564 + stub_entry->group->stub_sec->output_offset
10565 + stub_entry->group->stub_sec->output_section->vma);
10566 cmp_branch_off = to - from;
10567 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10568 }
10569
10570 if (PPC_HA (offset) != 0)
10571 {
10572 if (r != NULL)
10573 {
10574 if (ALWAYS_EMIT_R2SAVE
10575 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10576 r[0].r_offset += 4;
10577 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10578 r[1].r_offset = r[0].r_offset + 4;
10579 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10580 r[1].r_addend = r[0].r_addend;
10581 if (plt_load_toc)
10582 {
10583 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10584 {
10585 r[2].r_offset = r[1].r_offset + 4;
10586 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10587 r[2].r_addend = r[0].r_addend;
10588 }
10589 else
10590 {
10591 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10592 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10593 r[2].r_addend = r[0].r_addend + 8;
10594 if (plt_static_chain)
10595 {
10596 r[3].r_offset = r[2].r_offset + 4;
10597 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10598 r[3].r_addend = r[0].r_addend + 16;
10599 }
10600 }
10601 }
10602 }
10603 if (ALWAYS_EMIT_R2SAVE
10604 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10605 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10606 if (plt_load_toc)
10607 {
10608 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10609 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10610 }
10611 else
10612 {
10613 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10614 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10615 }
10616 if (plt_load_toc
10617 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10618 {
10619 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10620 offset = 0;
10621 }
10622 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10623 if (plt_load_toc)
10624 {
10625 if (use_fake_dep)
10626 {
10627 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10628 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10629 }
10630 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10631 if (plt_static_chain)
10632 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10633 }
10634 }
10635 else
10636 {
10637 if (r != NULL)
10638 {
10639 if (ALWAYS_EMIT_R2SAVE
10640 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10641 r[0].r_offset += 4;
10642 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10643 if (plt_load_toc)
10644 {
10645 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10646 {
10647 r[1].r_offset = r[0].r_offset + 4;
10648 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10649 r[1].r_addend = r[0].r_addend;
10650 }
10651 else
10652 {
10653 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10654 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10655 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10656 if (plt_static_chain)
10657 {
10658 r[2].r_offset = r[1].r_offset + 4;
10659 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10660 r[2].r_addend = r[0].r_addend + 8;
10661 }
10662 }
10663 }
10664 }
10665 if (ALWAYS_EMIT_R2SAVE
10666 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10667 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10668 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10669 if (plt_load_toc
10670 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10671 {
10672 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10673 offset = 0;
10674 }
10675 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10676 if (plt_load_toc)
10677 {
10678 if (use_fake_dep)
10679 {
10680 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10681 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10682 }
10683 if (plt_static_chain)
10684 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10685 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10686 }
10687 }
10688 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10689 {
10690 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10691 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10692 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10693 }
10694 else
10695 bfd_put_32 (obfd, BCTR, p), p += 4;
10696 return p;
10697 }
10698
10699 /* Build a special .plt call stub for __tls_get_addr. */
10700
10701 #define LD_R11_0R3 0xe9630000
10702 #define LD_R12_0R3 0xe9830000
10703 #define MR_R0_R3 0x7c601b78
10704 #define CMPDI_R11_0 0x2c2b0000
10705 #define ADD_R3_R12_R13 0x7c6c6a14
10706 #define BEQLR 0x4d820020
10707 #define MR_R3_R0 0x7c030378
10708 #define STD_R11_0R1 0xf9610000
10709 #define BCTRL 0x4e800421
10710 #define LD_R11_0R1 0xe9610000
10711 #define MTLR_R11 0x7d6803a6
10712
10713 static inline bfd_byte *
10714 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10715 struct ppc_stub_hash_entry *stub_entry,
10716 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10717 {
10718 bfd *obfd = htab->params->stub_bfd;
10719
10720 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10721 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10722 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10723 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10724 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10725 bfd_put_32 (obfd, BEQLR, p), p += 4;
10726 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10727 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10728 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10729
10730 if (r != NULL)
10731 r[0].r_offset += 9 * 4;
10732 p = build_plt_stub (htab, stub_entry, p, offset, r);
10733 bfd_put_32 (obfd, BCTRL, p - 4);
10734
10735 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10736 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10737 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10738 bfd_put_32 (obfd, BLR, p), p += 4;
10739
10740 return p;
10741 }
10742
10743 static Elf_Internal_Rela *
10744 get_relocs (asection *sec, int count)
10745 {
10746 Elf_Internal_Rela *relocs;
10747 struct bfd_elf_section_data *elfsec_data;
10748
10749 elfsec_data = elf_section_data (sec);
10750 relocs = elfsec_data->relocs;
10751 if (relocs == NULL)
10752 {
10753 bfd_size_type relsize;
10754 relsize = sec->reloc_count * sizeof (*relocs);
10755 relocs = bfd_alloc (sec->owner, relsize);
10756 if (relocs == NULL)
10757 return NULL;
10758 elfsec_data->relocs = relocs;
10759 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10760 sizeof (Elf_Internal_Shdr));
10761 if (elfsec_data->rela.hdr == NULL)
10762 return NULL;
10763 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10764 * sizeof (Elf64_External_Rela));
10765 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10766 sec->reloc_count = 0;
10767 }
10768 relocs += sec->reloc_count;
10769 sec->reloc_count += count;
10770 return relocs;
10771 }
10772
10773 static bfd_vma
10774 get_r2off (struct bfd_link_info *info,
10775 struct ppc_stub_hash_entry *stub_entry)
10776 {
10777 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10778 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10779
10780 if (r2off == 0)
10781 {
10782 /* Support linking -R objects. Get the toc pointer from the
10783 opd entry. */
10784 char buf[8];
10785 if (!htab->opd_abi)
10786 return r2off;
10787 asection *opd = stub_entry->h->elf.root.u.def.section;
10788 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10789
10790 if (strcmp (opd->name, ".opd") != 0
10791 || opd->reloc_count != 0)
10792 {
10793 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10794 stub_entry->h->elf.root.root.string);
10795 bfd_set_error (bfd_error_bad_value);
10796 return (bfd_vma) -1;
10797 }
10798 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10799 return (bfd_vma) -1;
10800 r2off = bfd_get_64 (opd->owner, buf);
10801 r2off -= elf_gp (info->output_bfd);
10802 }
10803 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10804 return r2off;
10805 }
10806
10807 static bfd_boolean
10808 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10809 {
10810 struct ppc_stub_hash_entry *stub_entry;
10811 struct ppc_branch_hash_entry *br_entry;
10812 struct bfd_link_info *info;
10813 struct ppc_link_hash_table *htab;
10814 bfd_byte *loc;
10815 bfd_byte *p;
10816 bfd_vma dest, off;
10817 int size;
10818 Elf_Internal_Rela *r;
10819 asection *plt;
10820
10821 /* Massage our args to the form they really have. */
10822 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10823 info = in_arg;
10824
10825 htab = ppc_hash_table (info);
10826 if (htab == NULL)
10827 return FALSE;
10828
10829 /* Make a note of the offset within the stubs for this entry. */
10830 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10831 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10832
10833 htab->stub_count[stub_entry->stub_type - 1] += 1;
10834 switch (stub_entry->stub_type)
10835 {
10836 case ppc_stub_long_branch:
10837 case ppc_stub_long_branch_r2off:
10838 /* Branches are relative. This is where we are going to. */
10839 dest = (stub_entry->target_value
10840 + stub_entry->target_section->output_offset
10841 + stub_entry->target_section->output_section->vma);
10842 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10843 off = dest;
10844
10845 /* And this is where we are coming from. */
10846 off -= (stub_entry->stub_offset
10847 + stub_entry->group->stub_sec->output_offset
10848 + stub_entry->group->stub_sec->output_section->vma);
10849
10850 size = 4;
10851 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10852 {
10853 bfd_vma r2off = get_r2off (info, stub_entry);
10854
10855 if (r2off == (bfd_vma) -1)
10856 {
10857 htab->stub_error = TRUE;
10858 return FALSE;
10859 }
10860 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10861 loc += 4;
10862 size = 8;
10863 if (PPC_HA (r2off) != 0)
10864 {
10865 bfd_put_32 (htab->params->stub_bfd,
10866 ADDIS_R2_R2 | PPC_HA (r2off), loc);
10867 loc += 4;
10868 size += 4;
10869 }
10870 if (PPC_LO (r2off) != 0)
10871 {
10872 bfd_put_32 (htab->params->stub_bfd,
10873 ADDI_R2_R2 | PPC_LO (r2off), loc);
10874 loc += 4;
10875 size += 4;
10876 }
10877 off -= size - 4;
10878 }
10879 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10880
10881 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10882 {
10883 info->callbacks->einfo
10884 (_("%P: long branch stub `%s' offset overflow\n"),
10885 stub_entry->root.string);
10886 htab->stub_error = TRUE;
10887 return FALSE;
10888 }
10889
10890 if (info->emitrelocations)
10891 {
10892 r = get_relocs (stub_entry->group->stub_sec, 1);
10893 if (r == NULL)
10894 return FALSE;
10895 r->r_offset = loc - stub_entry->group->stub_sec->contents;
10896 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10897 r->r_addend = dest;
10898 if (stub_entry->h != NULL)
10899 {
10900 struct elf_link_hash_entry **hashes;
10901 unsigned long symndx;
10902 struct ppc_link_hash_entry *h;
10903
10904 hashes = elf_sym_hashes (htab->params->stub_bfd);
10905 if (hashes == NULL)
10906 {
10907 bfd_size_type hsize;
10908
10909 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10910 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10911 if (hashes == NULL)
10912 return FALSE;
10913 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10914 htab->stub_globals = 1;
10915 }
10916 symndx = htab->stub_globals++;
10917 h = stub_entry->h;
10918 hashes[symndx] = &h->elf;
10919 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10920 if (h->oh != NULL && h->oh->is_func)
10921 h = ppc_follow_link (h->oh);
10922 if (h->elf.root.u.def.section != stub_entry->target_section)
10923 /* H is an opd symbol. The addend must be zero. */
10924 r->r_addend = 0;
10925 else
10926 {
10927 off = (h->elf.root.u.def.value
10928 + h->elf.root.u.def.section->output_offset
10929 + h->elf.root.u.def.section->output_section->vma);
10930 r->r_addend -= off;
10931 }
10932 }
10933 }
10934 break;
10935
10936 case ppc_stub_plt_branch:
10937 case ppc_stub_plt_branch_r2off:
10938 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10939 stub_entry->root.string + 9,
10940 FALSE, FALSE);
10941 if (br_entry == NULL)
10942 {
10943 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10944 stub_entry->root.string);
10945 htab->stub_error = TRUE;
10946 return FALSE;
10947 }
10948
10949 dest = (stub_entry->target_value
10950 + stub_entry->target_section->output_offset
10951 + stub_entry->target_section->output_section->vma);
10952 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10953 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10954
10955 bfd_put_64 (htab->brlt->owner, dest,
10956 htab->brlt->contents + br_entry->offset);
10957
10958 if (br_entry->iter == htab->stub_iteration)
10959 {
10960 br_entry->iter = 0;
10961
10962 if (htab->relbrlt != NULL)
10963 {
10964 /* Create a reloc for the branch lookup table entry. */
10965 Elf_Internal_Rela rela;
10966 bfd_byte *rl;
10967
10968 rela.r_offset = (br_entry->offset
10969 + htab->brlt->output_offset
10970 + htab->brlt->output_section->vma);
10971 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10972 rela.r_addend = dest;
10973
10974 rl = htab->relbrlt->contents;
10975 rl += (htab->relbrlt->reloc_count++
10976 * sizeof (Elf64_External_Rela));
10977 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10978 }
10979 else if (info->emitrelocations)
10980 {
10981 r = get_relocs (htab->brlt, 1);
10982 if (r == NULL)
10983 return FALSE;
10984 /* brlt, being SEC_LINKER_CREATED does not go through the
10985 normal reloc processing. Symbols and offsets are not
10986 translated from input file to output file form, so
10987 set up the offset per the output file. */
10988 r->r_offset = (br_entry->offset
10989 + htab->brlt->output_offset
10990 + htab->brlt->output_section->vma);
10991 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10992 r->r_addend = dest;
10993 }
10994 }
10995
10996 dest = (br_entry->offset
10997 + htab->brlt->output_offset
10998 + htab->brlt->output_section->vma);
10999
11000 off = (dest
11001 - elf_gp (htab->brlt->output_section->owner)
11002 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11003
11004 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11005 {
11006 info->callbacks->einfo
11007 (_("%P: linkage table error against `%T'\n"),
11008 stub_entry->root.string);
11009 bfd_set_error (bfd_error_bad_value);
11010 htab->stub_error = TRUE;
11011 return FALSE;
11012 }
11013
11014 if (info->emitrelocations)
11015 {
11016 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11017 if (r == NULL)
11018 return FALSE;
11019 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11020 if (bfd_big_endian (info->output_bfd))
11021 r[0].r_offset += 2;
11022 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11023 r[0].r_offset += 4;
11024 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11025 r[0].r_addend = dest;
11026 if (PPC_HA (off) != 0)
11027 {
11028 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11029 r[1].r_offset = r[0].r_offset + 4;
11030 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11031 r[1].r_addend = r[0].r_addend;
11032 }
11033 }
11034
11035 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11036 {
11037 if (PPC_HA (off) != 0)
11038 {
11039 size = 16;
11040 bfd_put_32 (htab->params->stub_bfd,
11041 ADDIS_R12_R2 | PPC_HA (off), loc);
11042 loc += 4;
11043 bfd_put_32 (htab->params->stub_bfd,
11044 LD_R12_0R12 | PPC_LO (off), loc);
11045 }
11046 else
11047 {
11048 size = 12;
11049 bfd_put_32 (htab->params->stub_bfd,
11050 LD_R12_0R2 | PPC_LO (off), loc);
11051 }
11052 }
11053 else
11054 {
11055 bfd_vma r2off = get_r2off (info, stub_entry);
11056
11057 if (r2off == (bfd_vma) -1)
11058 {
11059 htab->stub_error = TRUE;
11060 return FALSE;
11061 }
11062
11063 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11064 loc += 4;
11065 size = 16;
11066 if (PPC_HA (off) != 0)
11067 {
11068 size += 4;
11069 bfd_put_32 (htab->params->stub_bfd,
11070 ADDIS_R12_R2 | PPC_HA (off), loc);
11071 loc += 4;
11072 bfd_put_32 (htab->params->stub_bfd,
11073 LD_R12_0R12 | PPC_LO (off), loc);
11074 }
11075 else
11076 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11077
11078 if (PPC_HA (r2off) != 0)
11079 {
11080 size += 4;
11081 loc += 4;
11082 bfd_put_32 (htab->params->stub_bfd,
11083 ADDIS_R2_R2 | PPC_HA (r2off), loc);
11084 }
11085 if (PPC_LO (r2off) != 0)
11086 {
11087 size += 4;
11088 loc += 4;
11089 bfd_put_32 (htab->params->stub_bfd,
11090 ADDI_R2_R2 | PPC_LO (r2off), loc);
11091 }
11092 }
11093 loc += 4;
11094 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11095 loc += 4;
11096 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11097 break;
11098
11099 case ppc_stub_plt_call:
11100 case ppc_stub_plt_call_r2save:
11101 if (stub_entry->h != NULL
11102 && stub_entry->h->is_func_descriptor
11103 && stub_entry->h->oh != NULL)
11104 {
11105 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11106
11107 /* If the old-ABI "dot-symbol" is undefined make it weak so
11108 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11109 if (fh->elf.root.type == bfd_link_hash_undefined
11110 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11111 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11112 fh->elf.root.type = bfd_link_hash_undefweak;
11113 }
11114
11115 /* Now build the stub. */
11116 dest = stub_entry->plt_ent->plt.offset & ~1;
11117 if (dest >= (bfd_vma) -2)
11118 abort ();
11119
11120 plt = htab->elf.splt;
11121 if (!htab->elf.dynamic_sections_created
11122 || stub_entry->h == NULL
11123 || stub_entry->h->elf.dynindx == -1)
11124 plt = htab->elf.iplt;
11125
11126 dest += plt->output_offset + plt->output_section->vma;
11127
11128 if (stub_entry->h == NULL
11129 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11130 {
11131 Elf_Internal_Rela rela;
11132 bfd_byte *rl;
11133
11134 rela.r_offset = dest;
11135 if (htab->opd_abi)
11136 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11137 else
11138 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11139 rela.r_addend = (stub_entry->target_value
11140 + stub_entry->target_section->output_offset
11141 + stub_entry->target_section->output_section->vma);
11142
11143 rl = (htab->elf.irelplt->contents
11144 + (htab->elf.irelplt->reloc_count++
11145 * sizeof (Elf64_External_Rela)));
11146 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11147 stub_entry->plt_ent->plt.offset |= 1;
11148 }
11149
11150 off = (dest
11151 - elf_gp (plt->output_section->owner)
11152 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11153
11154 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11155 {
11156 info->callbacks->einfo
11157 /* xgettext:c-format */
11158 (_("%P: linkage table error against `%T'\n"),
11159 stub_entry->h != NULL
11160 ? stub_entry->h->elf.root.root.string
11161 : "<local sym>");
11162 bfd_set_error (bfd_error_bad_value);
11163 htab->stub_error = TRUE;
11164 return FALSE;
11165 }
11166
11167 if (htab->params->plt_stub_align != 0)
11168 {
11169 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11170
11171 stub_entry->group->stub_sec->size += pad;
11172 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11173 loc += pad;
11174 }
11175
11176 r = NULL;
11177 if (info->emitrelocations)
11178 {
11179 r = get_relocs (stub_entry->group->stub_sec,
11180 ((PPC_HA (off) != 0)
11181 + (htab->opd_abi
11182 ? 2 + (htab->params->plt_static_chain
11183 && PPC_HA (off + 16) == PPC_HA (off))
11184 : 1)));
11185 if (r == NULL)
11186 return FALSE;
11187 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11188 if (bfd_big_endian (info->output_bfd))
11189 r[0].r_offset += 2;
11190 r[0].r_addend = dest;
11191 }
11192 if (stub_entry->h != NULL
11193 && (stub_entry->h == htab->tls_get_addr_fd
11194 || stub_entry->h == htab->tls_get_addr)
11195 && htab->params->tls_get_addr_opt)
11196 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11197 else
11198 p = build_plt_stub (htab, stub_entry, loc, off, r);
11199 size = p - loc;
11200 break;
11201
11202 case ppc_stub_save_res:
11203 return TRUE;
11204
11205 default:
11206 BFD_FAIL ();
11207 return FALSE;
11208 }
11209
11210 stub_entry->group->stub_sec->size += size;
11211
11212 if (htab->params->emit_stub_syms)
11213 {
11214 struct elf_link_hash_entry *h;
11215 size_t len1, len2;
11216 char *name;
11217 const char *const stub_str[] = { "long_branch",
11218 "long_branch_r2off",
11219 "plt_branch",
11220 "plt_branch_r2off",
11221 "plt_call",
11222 "plt_call" };
11223
11224 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11225 len2 = strlen (stub_entry->root.string);
11226 name = bfd_malloc (len1 + len2 + 2);
11227 if (name == NULL)
11228 return FALSE;
11229 memcpy (name, stub_entry->root.string, 9);
11230 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11231 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11232 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11233 if (h == NULL)
11234 return FALSE;
11235 if (h->root.type == bfd_link_hash_new)
11236 {
11237 h->root.type = bfd_link_hash_defined;
11238 h->root.u.def.section = stub_entry->group->stub_sec;
11239 h->root.u.def.value = stub_entry->stub_offset;
11240 h->ref_regular = 1;
11241 h->def_regular = 1;
11242 h->ref_regular_nonweak = 1;
11243 h->forced_local = 1;
11244 h->non_elf = 0;
11245 h->root.linker_def = 1;
11246 }
11247 }
11248
11249 return TRUE;
11250 }
11251
11252 /* As above, but don't actually build the stub. Just bump offset so
11253 we know stub section sizes, and select plt_branch stubs where
11254 long_branch stubs won't do. */
11255
11256 static bfd_boolean
11257 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11258 {
11259 struct ppc_stub_hash_entry *stub_entry;
11260 struct bfd_link_info *info;
11261 struct ppc_link_hash_table *htab;
11262 bfd_vma off;
11263 int size;
11264
11265 /* Massage our args to the form they really have. */
11266 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11267 info = in_arg;
11268
11269 htab = ppc_hash_table (info);
11270 if (htab == NULL)
11271 return FALSE;
11272
11273 if (stub_entry->h != NULL
11274 && stub_entry->h->save_res
11275 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11276 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11277 {
11278 /* Don't make stubs to out-of-line register save/restore
11279 functions. Instead, emit copies of the functions. */
11280 stub_entry->group->needs_save_res = 1;
11281 stub_entry->stub_type = ppc_stub_save_res;
11282 return TRUE;
11283 }
11284
11285 if (stub_entry->stub_type == ppc_stub_plt_call
11286 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11287 {
11288 asection *plt;
11289 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11290 if (off >= (bfd_vma) -2)
11291 abort ();
11292 plt = htab->elf.splt;
11293 if (!htab->elf.dynamic_sections_created
11294 || stub_entry->h == NULL
11295 || stub_entry->h->elf.dynindx == -1)
11296 plt = htab->elf.iplt;
11297 off += (plt->output_offset
11298 + plt->output_section->vma
11299 - elf_gp (plt->output_section->owner)
11300 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11301
11302 size = plt_stub_size (htab, stub_entry, off);
11303 if (htab->params->plt_stub_align)
11304 size += plt_stub_pad (htab, stub_entry, off);
11305 if (info->emitrelocations)
11306 {
11307 stub_entry->group->stub_sec->reloc_count
11308 += ((PPC_HA (off) != 0)
11309 + (htab->opd_abi
11310 ? 2 + (htab->params->plt_static_chain
11311 && PPC_HA (off + 16) == PPC_HA (off))
11312 : 1));
11313 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11314 }
11315 }
11316 else
11317 {
11318 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11319 variants. */
11320 bfd_vma r2off = 0;
11321 bfd_vma local_off = 0;
11322
11323 off = (stub_entry->target_value
11324 + stub_entry->target_section->output_offset
11325 + stub_entry->target_section->output_section->vma);
11326 off -= (stub_entry->group->stub_sec->size
11327 + stub_entry->group->stub_sec->output_offset
11328 + stub_entry->group->stub_sec->output_section->vma);
11329
11330 /* Reset the stub type from the plt variant in case we now
11331 can reach with a shorter stub. */
11332 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11333 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11334
11335 size = 4;
11336 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11337 {
11338 r2off = get_r2off (info, stub_entry);
11339 if (r2off == (bfd_vma) -1)
11340 {
11341 htab->stub_error = TRUE;
11342 return FALSE;
11343 }
11344 size = 8;
11345 if (PPC_HA (r2off) != 0)
11346 size += 4;
11347 if (PPC_LO (r2off) != 0)
11348 size += 4;
11349 off -= size - 4;
11350 }
11351
11352 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11353
11354 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11355 Do the same for -R objects without function descriptors. */
11356 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11357 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11358 && r2off == 0
11359 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11360 {
11361 struct ppc_branch_hash_entry *br_entry;
11362
11363 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11364 stub_entry->root.string + 9,
11365 TRUE, FALSE);
11366 if (br_entry == NULL)
11367 {
11368 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11369 stub_entry->root.string);
11370 htab->stub_error = TRUE;
11371 return FALSE;
11372 }
11373
11374 if (br_entry->iter != htab->stub_iteration)
11375 {
11376 br_entry->iter = htab->stub_iteration;
11377 br_entry->offset = htab->brlt->size;
11378 htab->brlt->size += 8;
11379
11380 if (htab->relbrlt != NULL)
11381 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11382 else if (info->emitrelocations)
11383 {
11384 htab->brlt->reloc_count += 1;
11385 htab->brlt->flags |= SEC_RELOC;
11386 }
11387 }
11388
11389 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11390 off = (br_entry->offset
11391 + htab->brlt->output_offset
11392 + htab->brlt->output_section->vma
11393 - elf_gp (htab->brlt->output_section->owner)
11394 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11395
11396 if (info->emitrelocations)
11397 {
11398 stub_entry->group->stub_sec->reloc_count
11399 += 1 + (PPC_HA (off) != 0);
11400 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11401 }
11402
11403 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11404 {
11405 size = 12;
11406 if (PPC_HA (off) != 0)
11407 size = 16;
11408 }
11409 else
11410 {
11411 size = 16;
11412 if (PPC_HA (off) != 0)
11413 size += 4;
11414
11415 if (PPC_HA (r2off) != 0)
11416 size += 4;
11417 if (PPC_LO (r2off) != 0)
11418 size += 4;
11419 }
11420 }
11421 else if (info->emitrelocations)
11422 {
11423 stub_entry->group->stub_sec->reloc_count += 1;
11424 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11425 }
11426 }
11427
11428 stub_entry->group->stub_sec->size += size;
11429 return TRUE;
11430 }
11431
11432 /* Set up various things so that we can make a list of input sections
11433 for each output section included in the link. Returns -1 on error,
11434 0 when no stubs will be needed, and 1 on success. */
11435
11436 int
11437 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11438 {
11439 unsigned int id;
11440 bfd_size_type amt;
11441 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11442
11443 if (htab == NULL)
11444 return -1;
11445
11446 htab->sec_info_arr_size = bfd_get_next_section_id ();
11447 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11448 htab->sec_info = bfd_zmalloc (amt);
11449 if (htab->sec_info == NULL)
11450 return -1;
11451
11452 /* Set toc_off for com, und, abs and ind sections. */
11453 for (id = 0; id < 3; id++)
11454 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11455
11456 return 1;
11457 }
11458
11459 /* Set up for first pass at multitoc partitioning. */
11460
11461 void
11462 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11463 {
11464 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11465
11466 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11467 htab->toc_bfd = NULL;
11468 htab->toc_first_sec = NULL;
11469 }
11470
11471 /* The linker repeatedly calls this function for each TOC input section
11472 and linker generated GOT section. Group input bfds such that the toc
11473 within a group is less than 64k in size. */
11474
11475 bfd_boolean
11476 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11477 {
11478 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11479 bfd_vma addr, off, limit;
11480
11481 if (htab == NULL)
11482 return FALSE;
11483
11484 if (!htab->second_toc_pass)
11485 {
11486 /* Keep track of the first .toc or .got section for this input bfd. */
11487 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11488
11489 if (new_bfd)
11490 {
11491 htab->toc_bfd = isec->owner;
11492 htab->toc_first_sec = isec;
11493 }
11494
11495 addr = isec->output_offset + isec->output_section->vma;
11496 off = addr - htab->toc_curr;
11497 limit = 0x80008000;
11498 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11499 limit = 0x10000;
11500 if (off + isec->size > limit)
11501 {
11502 addr = (htab->toc_first_sec->output_offset
11503 + htab->toc_first_sec->output_section->vma);
11504 htab->toc_curr = addr;
11505 htab->toc_curr &= -TOC_BASE_ALIGN;
11506 }
11507
11508 /* toc_curr is the base address of this toc group. Set elf_gp
11509 for the input section to be the offset relative to the
11510 output toc base plus 0x8000. Making the input elf_gp an
11511 offset allows us to move the toc as a whole without
11512 recalculating input elf_gp. */
11513 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11514 off += TOC_BASE_OFF;
11515
11516 /* Die if someone uses a linker script that doesn't keep input
11517 file .toc and .got together. */
11518 if (new_bfd
11519 && elf_gp (isec->owner) != 0
11520 && elf_gp (isec->owner) != off)
11521 return FALSE;
11522
11523 elf_gp (isec->owner) = off;
11524 return TRUE;
11525 }
11526
11527 /* During the second pass toc_first_sec points to the start of
11528 a toc group, and toc_curr is used to track the old elf_gp.
11529 We use toc_bfd to ensure we only look at each bfd once. */
11530 if (htab->toc_bfd == isec->owner)
11531 return TRUE;
11532 htab->toc_bfd = isec->owner;
11533
11534 if (htab->toc_first_sec == NULL
11535 || htab->toc_curr != elf_gp (isec->owner))
11536 {
11537 htab->toc_curr = elf_gp (isec->owner);
11538 htab->toc_first_sec = isec;
11539 }
11540 addr = (htab->toc_first_sec->output_offset
11541 + htab->toc_first_sec->output_section->vma);
11542 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11543 elf_gp (isec->owner) = off;
11544
11545 return TRUE;
11546 }
11547
11548 /* Called via elf_link_hash_traverse to merge GOT entries for global
11549 symbol H. */
11550
11551 static bfd_boolean
11552 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11553 {
11554 if (h->root.type == bfd_link_hash_indirect)
11555 return TRUE;
11556
11557 merge_got_entries (&h->got.glist);
11558
11559 return TRUE;
11560 }
11561
11562 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11563 symbol H. */
11564
11565 static bfd_boolean
11566 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11567 {
11568 struct got_entry *gent;
11569
11570 if (h->root.type == bfd_link_hash_indirect)
11571 return TRUE;
11572
11573 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11574 if (!gent->is_indirect)
11575 allocate_got (h, (struct bfd_link_info *) inf, gent);
11576 return TRUE;
11577 }
11578
11579 /* Called on the first multitoc pass after the last call to
11580 ppc64_elf_next_toc_section. This function removes duplicate GOT
11581 entries. */
11582
11583 bfd_boolean
11584 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11585 {
11586 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11587 struct bfd *ibfd, *ibfd2;
11588 bfd_boolean done_something;
11589
11590 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11591
11592 if (!htab->do_multi_toc)
11593 return FALSE;
11594
11595 /* Merge global sym got entries within a toc group. */
11596 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11597
11598 /* And tlsld_got. */
11599 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11600 {
11601 struct got_entry *ent, *ent2;
11602
11603 if (!is_ppc64_elf (ibfd))
11604 continue;
11605
11606 ent = ppc64_tlsld_got (ibfd);
11607 if (!ent->is_indirect
11608 && ent->got.offset != (bfd_vma) -1)
11609 {
11610 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11611 {
11612 if (!is_ppc64_elf (ibfd2))
11613 continue;
11614
11615 ent2 = ppc64_tlsld_got (ibfd2);
11616 if (!ent2->is_indirect
11617 && ent2->got.offset != (bfd_vma) -1
11618 && elf_gp (ibfd2) == elf_gp (ibfd))
11619 {
11620 ent2->is_indirect = TRUE;
11621 ent2->got.ent = ent;
11622 }
11623 }
11624 }
11625 }
11626
11627 /* Zap sizes of got sections. */
11628 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11629 htab->elf.irelplt->size -= htab->got_reli_size;
11630 htab->got_reli_size = 0;
11631
11632 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11633 {
11634 asection *got, *relgot;
11635
11636 if (!is_ppc64_elf (ibfd))
11637 continue;
11638
11639 got = ppc64_elf_tdata (ibfd)->got;
11640 if (got != NULL)
11641 {
11642 got->rawsize = got->size;
11643 got->size = 0;
11644 relgot = ppc64_elf_tdata (ibfd)->relgot;
11645 relgot->rawsize = relgot->size;
11646 relgot->size = 0;
11647 }
11648 }
11649
11650 /* Now reallocate the got, local syms first. We don't need to
11651 allocate section contents again since we never increase size. */
11652 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11653 {
11654 struct got_entry **lgot_ents;
11655 struct got_entry **end_lgot_ents;
11656 struct plt_entry **local_plt;
11657 struct plt_entry **end_local_plt;
11658 unsigned char *lgot_masks;
11659 bfd_size_type locsymcount;
11660 Elf_Internal_Shdr *symtab_hdr;
11661 asection *s;
11662
11663 if (!is_ppc64_elf (ibfd))
11664 continue;
11665
11666 lgot_ents = elf_local_got_ents (ibfd);
11667 if (!lgot_ents)
11668 continue;
11669
11670 symtab_hdr = &elf_symtab_hdr (ibfd);
11671 locsymcount = symtab_hdr->sh_info;
11672 end_lgot_ents = lgot_ents + locsymcount;
11673 local_plt = (struct plt_entry **) end_lgot_ents;
11674 end_local_plt = local_plt + locsymcount;
11675 lgot_masks = (unsigned char *) end_local_plt;
11676 s = ppc64_elf_tdata (ibfd)->got;
11677 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11678 {
11679 struct got_entry *ent;
11680
11681 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11682 {
11683 unsigned int ent_size = 8;
11684 unsigned int rel_size = sizeof (Elf64_External_Rela);
11685
11686 ent->got.offset = s->size;
11687 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11688 {
11689 ent_size *= 2;
11690 rel_size *= 2;
11691 }
11692 s->size += ent_size;
11693 if ((*lgot_masks & PLT_IFUNC) != 0)
11694 {
11695 htab->elf.irelplt->size += rel_size;
11696 htab->got_reli_size += rel_size;
11697 }
11698 else if (bfd_link_pic (info))
11699 {
11700 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11701 srel->size += rel_size;
11702 }
11703 }
11704 }
11705 }
11706
11707 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11708
11709 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11710 {
11711 struct got_entry *ent;
11712
11713 if (!is_ppc64_elf (ibfd))
11714 continue;
11715
11716 ent = ppc64_tlsld_got (ibfd);
11717 if (!ent->is_indirect
11718 && ent->got.offset != (bfd_vma) -1)
11719 {
11720 asection *s = ppc64_elf_tdata (ibfd)->got;
11721 ent->got.offset = s->size;
11722 s->size += 16;
11723 if (bfd_link_pic (info))
11724 {
11725 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11726 srel->size += sizeof (Elf64_External_Rela);
11727 }
11728 }
11729 }
11730
11731 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11732 if (!done_something)
11733 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11734 {
11735 asection *got;
11736
11737 if (!is_ppc64_elf (ibfd))
11738 continue;
11739
11740 got = ppc64_elf_tdata (ibfd)->got;
11741 if (got != NULL)
11742 {
11743 done_something = got->rawsize != got->size;
11744 if (done_something)
11745 break;
11746 }
11747 }
11748
11749 if (done_something)
11750 (*htab->params->layout_sections_again) ();
11751
11752 /* Set up for second pass over toc sections to recalculate elf_gp
11753 on input sections. */
11754 htab->toc_bfd = NULL;
11755 htab->toc_first_sec = NULL;
11756 htab->second_toc_pass = TRUE;
11757 return done_something;
11758 }
11759
11760 /* Called after second pass of multitoc partitioning. */
11761
11762 void
11763 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11764 {
11765 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11766
11767 /* After the second pass, toc_curr tracks the TOC offset used
11768 for code sections below in ppc64_elf_next_input_section. */
11769 htab->toc_curr = TOC_BASE_OFF;
11770 }
11771
11772 /* No toc references were found in ISEC. If the code in ISEC makes no
11773 calls, then there's no need to use toc adjusting stubs when branching
11774 into ISEC. Actually, indirect calls from ISEC are OK as they will
11775 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11776 needed, and 2 if a cyclical call-graph was found but no other reason
11777 for a stub was detected. If called from the top level, a return of
11778 2 means the same as a return of 0. */
11779
11780 static int
11781 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11782 {
11783 int ret;
11784
11785 /* Mark this section as checked. */
11786 isec->call_check_done = 1;
11787
11788 /* We know none of our code bearing sections will need toc stubs. */
11789 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11790 return 0;
11791
11792 if (isec->size == 0)
11793 return 0;
11794
11795 if (isec->output_section == NULL)
11796 return 0;
11797
11798 ret = 0;
11799 if (isec->reloc_count != 0)
11800 {
11801 Elf_Internal_Rela *relstart, *rel;
11802 Elf_Internal_Sym *local_syms;
11803 struct ppc_link_hash_table *htab;
11804
11805 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11806 info->keep_memory);
11807 if (relstart == NULL)
11808 return -1;
11809
11810 /* Look for branches to outside of this section. */
11811 local_syms = NULL;
11812 htab = ppc_hash_table (info);
11813 if (htab == NULL)
11814 return -1;
11815
11816 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11817 {
11818 enum elf_ppc64_reloc_type r_type;
11819 unsigned long r_symndx;
11820 struct elf_link_hash_entry *h;
11821 struct ppc_link_hash_entry *eh;
11822 Elf_Internal_Sym *sym;
11823 asection *sym_sec;
11824 struct _opd_sec_data *opd;
11825 bfd_vma sym_value;
11826 bfd_vma dest;
11827
11828 r_type = ELF64_R_TYPE (rel->r_info);
11829 if (r_type != R_PPC64_REL24
11830 && r_type != R_PPC64_REL14
11831 && r_type != R_PPC64_REL14_BRTAKEN
11832 && r_type != R_PPC64_REL14_BRNTAKEN)
11833 continue;
11834
11835 r_symndx = ELF64_R_SYM (rel->r_info);
11836 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11837 isec->owner))
11838 {
11839 ret = -1;
11840 break;
11841 }
11842
11843 /* Calls to dynamic lib functions go through a plt call stub
11844 that uses r2. */
11845 eh = (struct ppc_link_hash_entry *) h;
11846 if (eh != NULL
11847 && (eh->elf.plt.plist != NULL
11848 || (eh->oh != NULL
11849 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11850 {
11851 ret = 1;
11852 break;
11853 }
11854
11855 if (sym_sec == NULL)
11856 /* Ignore other undefined symbols. */
11857 continue;
11858
11859 /* Assume branches to other sections not included in the
11860 link need stubs too, to cover -R and absolute syms. */
11861 if (sym_sec->output_section == NULL)
11862 {
11863 ret = 1;
11864 break;
11865 }
11866
11867 if (h == NULL)
11868 sym_value = sym->st_value;
11869 else
11870 {
11871 if (h->root.type != bfd_link_hash_defined
11872 && h->root.type != bfd_link_hash_defweak)
11873 abort ();
11874 sym_value = h->root.u.def.value;
11875 }
11876 sym_value += rel->r_addend;
11877
11878 /* If this branch reloc uses an opd sym, find the code section. */
11879 opd = get_opd_info (sym_sec);
11880 if (opd != NULL)
11881 {
11882 if (h == NULL && opd->adjust != NULL)
11883 {
11884 long adjust;
11885
11886 adjust = opd->adjust[OPD_NDX (sym_value)];
11887 if (adjust == -1)
11888 /* Assume deleted functions won't ever be called. */
11889 continue;
11890 sym_value += adjust;
11891 }
11892
11893 dest = opd_entry_value (sym_sec, sym_value,
11894 &sym_sec, NULL, FALSE);
11895 if (dest == (bfd_vma) -1)
11896 continue;
11897 }
11898 else
11899 dest = (sym_value
11900 + sym_sec->output_offset
11901 + sym_sec->output_section->vma);
11902
11903 /* Ignore branch to self. */
11904 if (sym_sec == isec)
11905 continue;
11906
11907 /* If the called function uses the toc, we need a stub. */
11908 if (sym_sec->has_toc_reloc
11909 || sym_sec->makes_toc_func_call)
11910 {
11911 ret = 1;
11912 break;
11913 }
11914
11915 /* Assume any branch that needs a long branch stub might in fact
11916 need a plt_branch stub. A plt_branch stub uses r2. */
11917 else if (dest - (isec->output_offset
11918 + isec->output_section->vma
11919 + rel->r_offset) + (1 << 25)
11920 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11921 ? h->other
11922 : sym->st_other))
11923 {
11924 ret = 1;
11925 break;
11926 }
11927
11928 /* If calling back to a section in the process of being
11929 tested, we can't say for sure that no toc adjusting stubs
11930 are needed, so don't return zero. */
11931 else if (sym_sec->call_check_in_progress)
11932 ret = 2;
11933
11934 /* Branches to another section that itself doesn't have any TOC
11935 references are OK. Recursively call ourselves to check. */
11936 else if (!sym_sec->call_check_done)
11937 {
11938 int recur;
11939
11940 /* Mark current section as indeterminate, so that other
11941 sections that call back to current won't be marked as
11942 known. */
11943 isec->call_check_in_progress = 1;
11944 recur = toc_adjusting_stub_needed (info, sym_sec);
11945 isec->call_check_in_progress = 0;
11946
11947 if (recur != 0)
11948 {
11949 ret = recur;
11950 if (recur != 2)
11951 break;
11952 }
11953 }
11954 }
11955
11956 if (local_syms != NULL
11957 && (elf_symtab_hdr (isec->owner).contents
11958 != (unsigned char *) local_syms))
11959 free (local_syms);
11960 if (elf_section_data (isec)->relocs != relstart)
11961 free (relstart);
11962 }
11963
11964 if ((ret & 1) == 0
11965 && isec->map_head.s != NULL
11966 && (strcmp (isec->output_section->name, ".init") == 0
11967 || strcmp (isec->output_section->name, ".fini") == 0))
11968 {
11969 if (isec->map_head.s->has_toc_reloc
11970 || isec->map_head.s->makes_toc_func_call)
11971 ret = 1;
11972 else if (!isec->map_head.s->call_check_done)
11973 {
11974 int recur;
11975 isec->call_check_in_progress = 1;
11976 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11977 isec->call_check_in_progress = 0;
11978 if (recur != 0)
11979 ret = recur;
11980 }
11981 }
11982
11983 if (ret == 1)
11984 isec->makes_toc_func_call = 1;
11985
11986 return ret;
11987 }
11988
11989 /* The linker repeatedly calls this function for each input section,
11990 in the order that input sections are linked into output sections.
11991 Build lists of input sections to determine groupings between which
11992 we may insert linker stubs. */
11993
11994 bfd_boolean
11995 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11996 {
11997 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11998
11999 if (htab == NULL)
12000 return FALSE;
12001
12002 if ((isec->output_section->flags & SEC_CODE) != 0
12003 && isec->output_section->id < htab->sec_info_arr_size)
12004 {
12005 /* This happens to make the list in reverse order,
12006 which is what we want. */
12007 htab->sec_info[isec->id].u.list
12008 = htab->sec_info[isec->output_section->id].u.list;
12009 htab->sec_info[isec->output_section->id].u.list = isec;
12010 }
12011
12012 if (htab->multi_toc_needed)
12013 {
12014 /* Analyse sections that aren't already flagged as needing a
12015 valid toc pointer. Exclude .fixup for the linux kernel.
12016 .fixup contains branches, but only back to the function that
12017 hit an exception. */
12018 if (!(isec->has_toc_reloc
12019 || (isec->flags & SEC_CODE) == 0
12020 || strcmp (isec->name, ".fixup") == 0
12021 || isec->call_check_done))
12022 {
12023 if (toc_adjusting_stub_needed (info, isec) < 0)
12024 return FALSE;
12025 }
12026 /* Make all sections use the TOC assigned for this object file.
12027 This will be wrong for pasted sections; We fix that in
12028 check_pasted_section(). */
12029 if (elf_gp (isec->owner) != 0)
12030 htab->toc_curr = elf_gp (isec->owner);
12031 }
12032
12033 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12034 return TRUE;
12035 }
12036
12037 /* Check that all .init and .fini sections use the same toc, if they
12038 have toc relocs. */
12039
12040 static bfd_boolean
12041 check_pasted_section (struct bfd_link_info *info, const char *name)
12042 {
12043 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12044
12045 if (o != NULL)
12046 {
12047 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12048 bfd_vma toc_off = 0;
12049 asection *i;
12050
12051 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12052 if (i->has_toc_reloc)
12053 {
12054 if (toc_off == 0)
12055 toc_off = htab->sec_info[i->id].toc_off;
12056 else if (toc_off != htab->sec_info[i->id].toc_off)
12057 return FALSE;
12058 }
12059
12060 if (toc_off == 0)
12061 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12062 if (i->makes_toc_func_call)
12063 {
12064 toc_off = htab->sec_info[i->id].toc_off;
12065 break;
12066 }
12067
12068 /* Make sure the whole pasted function uses the same toc offset. */
12069 if (toc_off != 0)
12070 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12071 htab->sec_info[i->id].toc_off = toc_off;
12072 }
12073 return TRUE;
12074 }
12075
12076 bfd_boolean
12077 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12078 {
12079 return (check_pasted_section (info, ".init")
12080 & check_pasted_section (info, ".fini"));
12081 }
12082
12083 /* See whether we can group stub sections together. Grouping stub
12084 sections may result in fewer stubs. More importantly, we need to
12085 put all .init* and .fini* stubs at the beginning of the .init or
12086 .fini output sections respectively, because glibc splits the
12087 _init and _fini functions into multiple parts. Putting a stub in
12088 the middle of a function is not a good idea. */
12089
12090 static bfd_boolean
12091 group_sections (struct bfd_link_info *info,
12092 bfd_size_type stub_group_size,
12093 bfd_boolean stubs_always_before_branch)
12094 {
12095 struct ppc_link_hash_table *htab;
12096 asection *osec;
12097 bfd_boolean suppress_size_errors;
12098
12099 htab = ppc_hash_table (info);
12100 if (htab == NULL)
12101 return FALSE;
12102
12103 suppress_size_errors = FALSE;
12104 if (stub_group_size == 1)
12105 {
12106 /* Default values. */
12107 if (stubs_always_before_branch)
12108 stub_group_size = 0x1e00000;
12109 else
12110 stub_group_size = 0x1c00000;
12111 suppress_size_errors = TRUE;
12112 }
12113
12114 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12115 {
12116 asection *tail;
12117
12118 if (osec->id >= htab->sec_info_arr_size)
12119 continue;
12120
12121 tail = htab->sec_info[osec->id].u.list;
12122 while (tail != NULL)
12123 {
12124 asection *curr;
12125 asection *prev;
12126 bfd_size_type total;
12127 bfd_boolean big_sec;
12128 bfd_vma curr_toc;
12129 struct map_stub *group;
12130 bfd_size_type group_size;
12131
12132 curr = tail;
12133 total = tail->size;
12134 group_size = (ppc64_elf_section_data (tail) != NULL
12135 && ppc64_elf_section_data (tail)->has_14bit_branch
12136 ? stub_group_size >> 10 : stub_group_size);
12137
12138 big_sec = total > group_size;
12139 if (big_sec && !suppress_size_errors)
12140 /* xgettext:c-format */
12141 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12142 tail->owner, tail);
12143 curr_toc = htab->sec_info[tail->id].toc_off;
12144
12145 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12146 && ((total += curr->output_offset - prev->output_offset)
12147 < (ppc64_elf_section_data (prev) != NULL
12148 && ppc64_elf_section_data (prev)->has_14bit_branch
12149 ? (group_size = stub_group_size >> 10) : group_size))
12150 && htab->sec_info[prev->id].toc_off == curr_toc)
12151 curr = prev;
12152
12153 /* OK, the size from the start of CURR to the end is less
12154 than group_size and thus can be handled by one stub
12155 section. (or the tail section is itself larger than
12156 group_size, in which case we may be toast.) We should
12157 really be keeping track of the total size of stubs added
12158 here, as stubs contribute to the final output section
12159 size. That's a little tricky, and this way will only
12160 break if stubs added make the total size more than 2^25,
12161 ie. for the default stub_group_size, if stubs total more
12162 than 2097152 bytes, or nearly 75000 plt call stubs. */
12163 group = bfd_alloc (curr->owner, sizeof (*group));
12164 if (group == NULL)
12165 return FALSE;
12166 group->link_sec = curr;
12167 group->stub_sec = NULL;
12168 group->needs_save_res = 0;
12169 group->next = htab->group;
12170 htab->group = group;
12171 do
12172 {
12173 prev = htab->sec_info[tail->id].u.list;
12174 /* Set up this stub group. */
12175 htab->sec_info[tail->id].u.group = group;
12176 }
12177 while (tail != curr && (tail = prev) != NULL);
12178
12179 /* But wait, there's more! Input sections up to group_size
12180 bytes before the stub section can be handled by it too.
12181 Don't do this if we have a really large section after the
12182 stubs, as adding more stubs increases the chance that
12183 branches may not reach into the stub section. */
12184 if (!stubs_always_before_branch && !big_sec)
12185 {
12186 total = 0;
12187 while (prev != NULL
12188 && ((total += tail->output_offset - prev->output_offset)
12189 < (ppc64_elf_section_data (prev) != NULL
12190 && ppc64_elf_section_data (prev)->has_14bit_branch
12191 ? (group_size = stub_group_size >> 10) : group_size))
12192 && htab->sec_info[prev->id].toc_off == curr_toc)
12193 {
12194 tail = prev;
12195 prev = htab->sec_info[tail->id].u.list;
12196 htab->sec_info[tail->id].u.group = group;
12197 }
12198 }
12199 tail = prev;
12200 }
12201 }
12202 return TRUE;
12203 }
12204
12205 static const unsigned char glink_eh_frame_cie[] =
12206 {
12207 0, 0, 0, 16, /* length. */
12208 0, 0, 0, 0, /* id. */
12209 1, /* CIE version. */
12210 'z', 'R', 0, /* Augmentation string. */
12211 4, /* Code alignment. */
12212 0x78, /* Data alignment. */
12213 65, /* RA reg. */
12214 1, /* Augmentation size. */
12215 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12216 DW_CFA_def_cfa, 1, 0, /* def_cfa: r1 offset 0. */
12217 0, 0, 0, 0
12218 };
12219
12220 /* Stripping output sections is normally done before dynamic section
12221 symbols have been allocated. This function is called later, and
12222 handles cases like htab->brlt which is mapped to its own output
12223 section. */
12224
12225 static void
12226 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12227 {
12228 if (isec->size == 0
12229 && isec->output_section->size == 0
12230 && !(isec->output_section->flags & SEC_KEEP)
12231 && !bfd_section_removed_from_list (info->output_bfd,
12232 isec->output_section)
12233 && elf_section_data (isec->output_section)->dynindx == 0)
12234 {
12235 isec->output_section->flags |= SEC_EXCLUDE;
12236 bfd_section_list_remove (info->output_bfd, isec->output_section);
12237 info->output_bfd->section_count--;
12238 }
12239 }
12240
12241 /* Determine and set the size of the stub section for a final link.
12242
12243 The basic idea here is to examine all the relocations looking for
12244 PC-relative calls to a target that is unreachable with a "bl"
12245 instruction. */
12246
12247 bfd_boolean
12248 ppc64_elf_size_stubs (struct bfd_link_info *info)
12249 {
12250 bfd_size_type stub_group_size;
12251 bfd_boolean stubs_always_before_branch;
12252 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12253
12254 if (htab == NULL)
12255 return FALSE;
12256
12257 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12258 htab->params->plt_thread_safe = 1;
12259 if (!htab->opd_abi)
12260 htab->params->plt_thread_safe = 0;
12261 else if (htab->params->plt_thread_safe == -1)
12262 {
12263 static const char *const thread_starter[] =
12264 {
12265 "pthread_create",
12266 /* libstdc++ */
12267 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12268 /* librt */
12269 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12270 "mq_notify", "create_timer",
12271 /* libanl */
12272 "getaddrinfo_a",
12273 /* libgomp */
12274 "GOMP_parallel",
12275 "GOMP_parallel_start",
12276 "GOMP_parallel_loop_static",
12277 "GOMP_parallel_loop_static_start",
12278 "GOMP_parallel_loop_dynamic",
12279 "GOMP_parallel_loop_dynamic_start",
12280 "GOMP_parallel_loop_guided",
12281 "GOMP_parallel_loop_guided_start",
12282 "GOMP_parallel_loop_runtime",
12283 "GOMP_parallel_loop_runtime_start",
12284 "GOMP_parallel_sections",
12285 "GOMP_parallel_sections_start",
12286 /* libgo */
12287 "__go_go",
12288 };
12289 unsigned i;
12290
12291 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12292 {
12293 struct elf_link_hash_entry *h;
12294 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12295 FALSE, FALSE, TRUE);
12296 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12297 if (htab->params->plt_thread_safe)
12298 break;
12299 }
12300 }
12301 stubs_always_before_branch = htab->params->group_size < 0;
12302 if (htab->params->group_size < 0)
12303 stub_group_size = -htab->params->group_size;
12304 else
12305 stub_group_size = htab->params->group_size;
12306
12307 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12308 return FALSE;
12309
12310 #define STUB_SHRINK_ITER 20
12311 /* Loop until no stubs added. After iteration 20 of this loop we may
12312 exit on a stub section shrinking. This is to break out of a
12313 pathological case where adding stubs on one iteration decreases
12314 section gaps (perhaps due to alignment), which then requires
12315 fewer or smaller stubs on the next iteration. */
12316
12317 while (1)
12318 {
12319 bfd *input_bfd;
12320 unsigned int bfd_indx;
12321 struct map_stub *group;
12322 asection *stub_sec;
12323
12324 htab->stub_iteration += 1;
12325
12326 for (input_bfd = info->input_bfds, bfd_indx = 0;
12327 input_bfd != NULL;
12328 input_bfd = input_bfd->link.next, bfd_indx++)
12329 {
12330 Elf_Internal_Shdr *symtab_hdr;
12331 asection *section;
12332 Elf_Internal_Sym *local_syms = NULL;
12333
12334 if (!is_ppc64_elf (input_bfd))
12335 continue;
12336
12337 /* We'll need the symbol table in a second. */
12338 symtab_hdr = &elf_symtab_hdr (input_bfd);
12339 if (symtab_hdr->sh_info == 0)
12340 continue;
12341
12342 /* Walk over each section attached to the input bfd. */
12343 for (section = input_bfd->sections;
12344 section != NULL;
12345 section = section->next)
12346 {
12347 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12348
12349 /* If there aren't any relocs, then there's nothing more
12350 to do. */
12351 if ((section->flags & SEC_RELOC) == 0
12352 || (section->flags & SEC_ALLOC) == 0
12353 || (section->flags & SEC_LOAD) == 0
12354 || (section->flags & SEC_CODE) == 0
12355 || section->reloc_count == 0)
12356 continue;
12357
12358 /* If this section is a link-once section that will be
12359 discarded, then don't create any stubs. */
12360 if (section->output_section == NULL
12361 || section->output_section->owner != info->output_bfd)
12362 continue;
12363
12364 /* Get the relocs. */
12365 internal_relocs
12366 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12367 info->keep_memory);
12368 if (internal_relocs == NULL)
12369 goto error_ret_free_local;
12370
12371 /* Now examine each relocation. */
12372 irela = internal_relocs;
12373 irelaend = irela + section->reloc_count;
12374 for (; irela < irelaend; irela++)
12375 {
12376 enum elf_ppc64_reloc_type r_type;
12377 unsigned int r_indx;
12378 enum ppc_stub_type stub_type;
12379 struct ppc_stub_hash_entry *stub_entry;
12380 asection *sym_sec, *code_sec;
12381 bfd_vma sym_value, code_value;
12382 bfd_vma destination;
12383 unsigned long local_off;
12384 bfd_boolean ok_dest;
12385 struct ppc_link_hash_entry *hash;
12386 struct ppc_link_hash_entry *fdh;
12387 struct elf_link_hash_entry *h;
12388 Elf_Internal_Sym *sym;
12389 char *stub_name;
12390 const asection *id_sec;
12391 struct _opd_sec_data *opd;
12392 struct plt_entry *plt_ent;
12393
12394 r_type = ELF64_R_TYPE (irela->r_info);
12395 r_indx = ELF64_R_SYM (irela->r_info);
12396
12397 if (r_type >= R_PPC64_max)
12398 {
12399 bfd_set_error (bfd_error_bad_value);
12400 goto error_ret_free_internal;
12401 }
12402
12403 /* Only look for stubs on branch instructions. */
12404 if (r_type != R_PPC64_REL24
12405 && r_type != R_PPC64_REL14
12406 && r_type != R_PPC64_REL14_BRTAKEN
12407 && r_type != R_PPC64_REL14_BRNTAKEN)
12408 continue;
12409
12410 /* Now determine the call target, its name, value,
12411 section. */
12412 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12413 r_indx, input_bfd))
12414 goto error_ret_free_internal;
12415 hash = (struct ppc_link_hash_entry *) h;
12416
12417 ok_dest = FALSE;
12418 fdh = NULL;
12419 sym_value = 0;
12420 if (hash == NULL)
12421 {
12422 sym_value = sym->st_value;
12423 if (sym_sec != NULL
12424 && sym_sec->output_section != NULL)
12425 ok_dest = TRUE;
12426 }
12427 else if (hash->elf.root.type == bfd_link_hash_defined
12428 || hash->elf.root.type == bfd_link_hash_defweak)
12429 {
12430 sym_value = hash->elf.root.u.def.value;
12431 if (sym_sec->output_section != NULL)
12432 ok_dest = TRUE;
12433 }
12434 else if (hash->elf.root.type == bfd_link_hash_undefweak
12435 || hash->elf.root.type == bfd_link_hash_undefined)
12436 {
12437 /* Recognise an old ABI func code entry sym, and
12438 use the func descriptor sym instead if it is
12439 defined. */
12440 if (hash->elf.root.root.string[0] == '.'
12441 && hash->oh != NULL)
12442 {
12443 fdh = ppc_follow_link (hash->oh);
12444 if (fdh->elf.root.type == bfd_link_hash_defined
12445 || fdh->elf.root.type == bfd_link_hash_defweak)
12446 {
12447 sym_sec = fdh->elf.root.u.def.section;
12448 sym_value = fdh->elf.root.u.def.value;
12449 if (sym_sec->output_section != NULL)
12450 ok_dest = TRUE;
12451 }
12452 else
12453 fdh = NULL;
12454 }
12455 }
12456 else
12457 {
12458 bfd_set_error (bfd_error_bad_value);
12459 goto error_ret_free_internal;
12460 }
12461
12462 destination = 0;
12463 local_off = 0;
12464 if (ok_dest)
12465 {
12466 sym_value += irela->r_addend;
12467 destination = (sym_value
12468 + sym_sec->output_offset
12469 + sym_sec->output_section->vma);
12470 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12471 ? hash->elf.other
12472 : sym->st_other);
12473 }
12474
12475 code_sec = sym_sec;
12476 code_value = sym_value;
12477 opd = get_opd_info (sym_sec);
12478 if (opd != NULL)
12479 {
12480 bfd_vma dest;
12481
12482 if (hash == NULL && opd->adjust != NULL)
12483 {
12484 long adjust = opd->adjust[OPD_NDX (sym_value)];
12485 if (adjust == -1)
12486 continue;
12487 code_value += adjust;
12488 sym_value += adjust;
12489 }
12490 dest = opd_entry_value (sym_sec, sym_value,
12491 &code_sec, &code_value, FALSE);
12492 if (dest != (bfd_vma) -1)
12493 {
12494 destination = dest;
12495 if (fdh != NULL)
12496 {
12497 /* Fixup old ABI sym to point at code
12498 entry. */
12499 hash->elf.root.type = bfd_link_hash_defweak;
12500 hash->elf.root.u.def.section = code_sec;
12501 hash->elf.root.u.def.value = code_value;
12502 }
12503 }
12504 }
12505
12506 /* Determine what (if any) linker stub is needed. */
12507 plt_ent = NULL;
12508 stub_type = ppc_type_of_stub (section, irela, &hash,
12509 &plt_ent, destination,
12510 local_off);
12511
12512 if (stub_type != ppc_stub_plt_call)
12513 {
12514 /* Check whether we need a TOC adjusting stub.
12515 Since the linker pastes together pieces from
12516 different object files when creating the
12517 _init and _fini functions, it may be that a
12518 call to what looks like a local sym is in
12519 fact a call needing a TOC adjustment. */
12520 if (code_sec != NULL
12521 && code_sec->output_section != NULL
12522 && (htab->sec_info[code_sec->id].toc_off
12523 != htab->sec_info[section->id].toc_off)
12524 && (code_sec->has_toc_reloc
12525 || code_sec->makes_toc_func_call))
12526 stub_type = ppc_stub_long_branch_r2off;
12527 }
12528
12529 if (stub_type == ppc_stub_none)
12530 continue;
12531
12532 /* __tls_get_addr calls might be eliminated. */
12533 if (stub_type != ppc_stub_plt_call
12534 && hash != NULL
12535 && (hash == htab->tls_get_addr
12536 || hash == htab->tls_get_addr_fd)
12537 && section->has_tls_reloc
12538 && irela != internal_relocs)
12539 {
12540 /* Get tls info. */
12541 unsigned char *tls_mask;
12542
12543 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12544 irela - 1, input_bfd))
12545 goto error_ret_free_internal;
12546 if (*tls_mask != 0)
12547 continue;
12548 }
12549
12550 if (stub_type == ppc_stub_plt_call
12551 && irela + 1 < irelaend
12552 && irela[1].r_offset == irela->r_offset + 4
12553 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12554 {
12555 if (!tocsave_find (htab, INSERT,
12556 &local_syms, irela + 1, input_bfd))
12557 goto error_ret_free_internal;
12558 }
12559 else if (stub_type == ppc_stub_plt_call)
12560 stub_type = ppc_stub_plt_call_r2save;
12561
12562 /* Support for grouping stub sections. */
12563 id_sec = htab->sec_info[section->id].u.group->link_sec;
12564
12565 /* Get the name of this stub. */
12566 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12567 if (!stub_name)
12568 goto error_ret_free_internal;
12569
12570 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12571 stub_name, FALSE, FALSE);
12572 if (stub_entry != NULL)
12573 {
12574 /* The proper stub has already been created. */
12575 free (stub_name);
12576 if (stub_type == ppc_stub_plt_call_r2save)
12577 stub_entry->stub_type = stub_type;
12578 continue;
12579 }
12580
12581 stub_entry = ppc_add_stub (stub_name, section, info);
12582 if (stub_entry == NULL)
12583 {
12584 free (stub_name);
12585 error_ret_free_internal:
12586 if (elf_section_data (section)->relocs == NULL)
12587 free (internal_relocs);
12588 error_ret_free_local:
12589 if (local_syms != NULL
12590 && (symtab_hdr->contents
12591 != (unsigned char *) local_syms))
12592 free (local_syms);
12593 return FALSE;
12594 }
12595
12596 stub_entry->stub_type = stub_type;
12597 if (stub_type != ppc_stub_plt_call
12598 && stub_type != ppc_stub_plt_call_r2save)
12599 {
12600 stub_entry->target_value = code_value;
12601 stub_entry->target_section = code_sec;
12602 }
12603 else
12604 {
12605 stub_entry->target_value = sym_value;
12606 stub_entry->target_section = sym_sec;
12607 }
12608 stub_entry->h = hash;
12609 stub_entry->plt_ent = plt_ent;
12610 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12611
12612 if (stub_entry->h != NULL)
12613 htab->stub_globals += 1;
12614 }
12615
12616 /* We're done with the internal relocs, free them. */
12617 if (elf_section_data (section)->relocs != internal_relocs)
12618 free (internal_relocs);
12619 }
12620
12621 if (local_syms != NULL
12622 && symtab_hdr->contents != (unsigned char *) local_syms)
12623 {
12624 if (!info->keep_memory)
12625 free (local_syms);
12626 else
12627 symtab_hdr->contents = (unsigned char *) local_syms;
12628 }
12629 }
12630
12631 /* We may have added some stubs. Find out the new size of the
12632 stub sections. */
12633 for (stub_sec = htab->params->stub_bfd->sections;
12634 stub_sec != NULL;
12635 stub_sec = stub_sec->next)
12636 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12637 {
12638 if (htab->stub_iteration <= STUB_SHRINK_ITER
12639 || stub_sec->rawsize < stub_sec->size)
12640 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12641 stub_sec->rawsize = stub_sec->size;
12642 stub_sec->size = 0;
12643 stub_sec->reloc_count = 0;
12644 stub_sec->flags &= ~SEC_RELOC;
12645 }
12646
12647 htab->brlt->size = 0;
12648 htab->brlt->reloc_count = 0;
12649 htab->brlt->flags &= ~SEC_RELOC;
12650 if (htab->relbrlt != NULL)
12651 htab->relbrlt->size = 0;
12652
12653 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12654
12655 for (group = htab->group; group != NULL; group = group->next)
12656 if (group->needs_save_res)
12657 group->stub_sec->size += htab->sfpr->size;
12658
12659 if (info->emitrelocations
12660 && htab->glink != NULL && htab->glink->size != 0)
12661 {
12662 htab->glink->reloc_count = 1;
12663 htab->glink->flags |= SEC_RELOC;
12664 }
12665
12666 if (htab->glink_eh_frame != NULL
12667 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12668 && htab->glink_eh_frame->output_section->size != 0)
12669 {
12670 size_t size = 0, align;
12671
12672 for (stub_sec = htab->params->stub_bfd->sections;
12673 stub_sec != NULL;
12674 stub_sec = stub_sec->next)
12675 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12676 size += 24;
12677 if (htab->glink != NULL && htab->glink->size != 0)
12678 size += 24;
12679 if (size != 0)
12680 size += sizeof (glink_eh_frame_cie);
12681 align = 1;
12682 align <<= htab->glink_eh_frame->output_section->alignment_power;
12683 align -= 1;
12684 size = (size + align) & ~align;
12685 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12686 htab->glink_eh_frame->size = size;
12687 }
12688
12689 if (htab->params->plt_stub_align != 0)
12690 for (stub_sec = htab->params->stub_bfd->sections;
12691 stub_sec != NULL;
12692 stub_sec = stub_sec->next)
12693 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12694 stub_sec->size = ((stub_sec->size
12695 + (1 << htab->params->plt_stub_align) - 1)
12696 & -(1 << htab->params->plt_stub_align));
12697
12698 for (stub_sec = htab->params->stub_bfd->sections;
12699 stub_sec != NULL;
12700 stub_sec = stub_sec->next)
12701 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12702 && stub_sec->rawsize != stub_sec->size
12703 && (htab->stub_iteration <= STUB_SHRINK_ITER
12704 || stub_sec->rawsize < stub_sec->size))
12705 break;
12706
12707 if (stub_sec == NULL
12708 && (htab->glink_eh_frame == NULL
12709 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12710 break;
12711
12712 /* Ask the linker to do its stuff. */
12713 (*htab->params->layout_sections_again) ();
12714 }
12715
12716 if (htab->glink_eh_frame != NULL
12717 && htab->glink_eh_frame->size != 0)
12718 {
12719 bfd_vma val;
12720 bfd_byte *p, *last_fde;
12721 size_t last_fde_len, size, align, pad;
12722 asection *stub_sec;
12723
12724 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12725 if (p == NULL)
12726 return FALSE;
12727 htab->glink_eh_frame->contents = p;
12728 last_fde = p;
12729
12730 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12731 /* CIE length (rewrite in case little-endian). */
12732 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12733 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12734 p += sizeof (glink_eh_frame_cie);
12735
12736 for (stub_sec = htab->params->stub_bfd->sections;
12737 stub_sec != NULL;
12738 stub_sec = stub_sec->next)
12739 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12740 {
12741 last_fde = p;
12742 last_fde_len = 20;
12743 /* FDE length. */
12744 bfd_put_32 (htab->elf.dynobj, 20, p);
12745 p += 4;
12746 /* CIE pointer. */
12747 val = p - htab->glink_eh_frame->contents;
12748 bfd_put_32 (htab->elf.dynobj, val, p);
12749 p += 4;
12750 /* Offset to stub section, written later. */
12751 p += 4;
12752 /* stub section size. */
12753 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12754 p += 4;
12755 /* Augmentation. */
12756 p += 1;
12757 /* Pad. */
12758 p += 7;
12759 }
12760 if (htab->glink != NULL && htab->glink->size != 0)
12761 {
12762 last_fde = p;
12763 last_fde_len = 20;
12764 /* FDE length. */
12765 bfd_put_32 (htab->elf.dynobj, 20, p);
12766 p += 4;
12767 /* CIE pointer. */
12768 val = p - htab->glink_eh_frame->contents;
12769 bfd_put_32 (htab->elf.dynobj, val, p);
12770 p += 4;
12771 /* Offset to .glink, written later. */
12772 p += 4;
12773 /* .glink size. */
12774 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12775 p += 4;
12776 /* Augmentation. */
12777 p += 1;
12778
12779 *p++ = DW_CFA_advance_loc + 1;
12780 *p++ = DW_CFA_register;
12781 *p++ = 65;
12782 *p++ = htab->opd_abi ? 12 : 0;
12783 *p++ = DW_CFA_advance_loc + 4;
12784 *p++ = DW_CFA_restore_extended;
12785 *p++ = 65;
12786 }
12787 /* Subsume any padding into the last FDE if user .eh_frame
12788 sections are aligned more than glink_eh_frame. Otherwise any
12789 zero padding will be seen as a terminator. */
12790 size = p - htab->glink_eh_frame->contents;
12791 align = 1;
12792 align <<= htab->glink_eh_frame->output_section->alignment_power;
12793 align -= 1;
12794 pad = ((size + align) & ~align) - size;
12795 htab->glink_eh_frame->size = size + pad;
12796 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12797 }
12798
12799 maybe_strip_output (info, htab->brlt);
12800 if (htab->glink_eh_frame != NULL)
12801 maybe_strip_output (info, htab->glink_eh_frame);
12802
12803 return TRUE;
12804 }
12805
12806 /* Called after we have determined section placement. If sections
12807 move, we'll be called again. Provide a value for TOCstart. */
12808
12809 bfd_vma
12810 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12811 {
12812 asection *s;
12813 bfd_vma TOCstart, adjust;
12814
12815 if (info != NULL)
12816 {
12817 struct elf_link_hash_entry *h;
12818 struct elf_link_hash_table *htab = elf_hash_table (info);
12819
12820 if (is_elf_hash_table (htab)
12821 && htab->hgot != NULL)
12822 h = htab->hgot;
12823 else
12824 {
12825 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12826 if (is_elf_hash_table (htab))
12827 htab->hgot = h;
12828 }
12829 if (h != NULL
12830 && h->root.type == bfd_link_hash_defined
12831 && !h->root.linker_def
12832 && (!is_elf_hash_table (htab)
12833 || h->def_regular))
12834 {
12835 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12836 + h->root.u.def.section->output_offset
12837 + h->root.u.def.section->output_section->vma);
12838 _bfd_set_gp_value (obfd, TOCstart);
12839 return TOCstart;
12840 }
12841 }
12842
12843 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12844 order. The TOC starts where the first of these sections starts. */
12845 s = bfd_get_section_by_name (obfd, ".got");
12846 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12847 s = bfd_get_section_by_name (obfd, ".toc");
12848 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12849 s = bfd_get_section_by_name (obfd, ".tocbss");
12850 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12851 s = bfd_get_section_by_name (obfd, ".plt");
12852 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12853 {
12854 /* This may happen for
12855 o references to TOC base (SYM@toc / TOC[tc0]) without a
12856 .toc directive
12857 o bad linker script
12858 o --gc-sections and empty TOC sections
12859
12860 FIXME: Warn user? */
12861
12862 /* Look for a likely section. We probably won't even be
12863 using TOCstart. */
12864 for (s = obfd->sections; s != NULL; s = s->next)
12865 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12866 | SEC_EXCLUDE))
12867 == (SEC_ALLOC | SEC_SMALL_DATA))
12868 break;
12869 if (s == NULL)
12870 for (s = obfd->sections; s != NULL; s = s->next)
12871 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12872 == (SEC_ALLOC | SEC_SMALL_DATA))
12873 break;
12874 if (s == NULL)
12875 for (s = obfd->sections; s != NULL; s = s->next)
12876 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12877 == SEC_ALLOC)
12878 break;
12879 if (s == NULL)
12880 for (s = obfd->sections; s != NULL; s = s->next)
12881 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12882 break;
12883 }
12884
12885 TOCstart = 0;
12886 if (s != NULL)
12887 TOCstart = s->output_section->vma + s->output_offset;
12888
12889 /* Force alignment. */
12890 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12891 TOCstart -= adjust;
12892 _bfd_set_gp_value (obfd, TOCstart);
12893
12894 if (info != NULL && s != NULL)
12895 {
12896 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12897
12898 if (htab != NULL)
12899 {
12900 if (htab->elf.hgot != NULL)
12901 {
12902 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12903 htab->elf.hgot->root.u.def.section = s;
12904 }
12905 }
12906 else
12907 {
12908 struct bfd_link_hash_entry *bh = NULL;
12909 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12910 s, TOC_BASE_OFF - adjust,
12911 NULL, FALSE, FALSE, &bh);
12912 }
12913 }
12914 return TOCstart;
12915 }
12916
12917 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12918 write out any global entry stubs. */
12919
12920 static bfd_boolean
12921 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12922 {
12923 struct bfd_link_info *info;
12924 struct ppc_link_hash_table *htab;
12925 struct plt_entry *pent;
12926 asection *s;
12927
12928 if (h->root.type == bfd_link_hash_indirect)
12929 return TRUE;
12930
12931 if (!h->pointer_equality_needed)
12932 return TRUE;
12933
12934 if (h->def_regular)
12935 return TRUE;
12936
12937 info = inf;
12938 htab = ppc_hash_table (info);
12939 if (htab == NULL)
12940 return FALSE;
12941
12942 s = htab->glink;
12943 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12944 if (pent->plt.offset != (bfd_vma) -1
12945 && pent->addend == 0)
12946 {
12947 bfd_byte *p;
12948 asection *plt;
12949 bfd_vma off;
12950
12951 p = s->contents + h->root.u.def.value;
12952 plt = htab->elf.splt;
12953 if (!htab->elf.dynamic_sections_created
12954 || h->dynindx == -1)
12955 plt = htab->elf.iplt;
12956 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12957 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12958
12959 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12960 {
12961 info->callbacks->einfo
12962 (_("%P: linkage table error against `%T'\n"),
12963 h->root.root.string);
12964 bfd_set_error (bfd_error_bad_value);
12965 htab->stub_error = TRUE;
12966 }
12967
12968 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12969 if (htab->params->emit_stub_syms)
12970 {
12971 size_t len = strlen (h->root.root.string);
12972 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12973
12974 if (name == NULL)
12975 return FALSE;
12976
12977 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12978 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12979 if (h == NULL)
12980 return FALSE;
12981 if (h->root.type == bfd_link_hash_new)
12982 {
12983 h->root.type = bfd_link_hash_defined;
12984 h->root.u.def.section = s;
12985 h->root.u.def.value = p - s->contents;
12986 h->ref_regular = 1;
12987 h->def_regular = 1;
12988 h->ref_regular_nonweak = 1;
12989 h->forced_local = 1;
12990 h->non_elf = 0;
12991 h->root.linker_def = 1;
12992 }
12993 }
12994
12995 if (PPC_HA (off) != 0)
12996 {
12997 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
12998 p += 4;
12999 }
13000 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13001 p += 4;
13002 bfd_put_32 (s->owner, MTCTR_R12, p);
13003 p += 4;
13004 bfd_put_32 (s->owner, BCTR, p);
13005 break;
13006 }
13007 return TRUE;
13008 }
13009
13010 /* Build all the stubs associated with the current output file.
13011 The stubs are kept in a hash table attached to the main linker
13012 hash table. This function is called via gldelf64ppc_finish. */
13013
13014 bfd_boolean
13015 ppc64_elf_build_stubs (struct bfd_link_info *info,
13016 char **stats)
13017 {
13018 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13019 struct map_stub *group;
13020 asection *stub_sec;
13021 bfd_byte *p;
13022 int stub_sec_count = 0;
13023
13024 if (htab == NULL)
13025 return FALSE;
13026
13027 /* Allocate memory to hold the linker stubs. */
13028 for (stub_sec = htab->params->stub_bfd->sections;
13029 stub_sec != NULL;
13030 stub_sec = stub_sec->next)
13031 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13032 && stub_sec->size != 0)
13033 {
13034 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13035 if (stub_sec->contents == NULL)
13036 return FALSE;
13037 stub_sec->size = 0;
13038 }
13039
13040 if (htab->glink != NULL && htab->glink->size != 0)
13041 {
13042 unsigned int indx;
13043 bfd_vma plt0;
13044
13045 /* Build the .glink plt call stub. */
13046 if (htab->params->emit_stub_syms)
13047 {
13048 struct elf_link_hash_entry *h;
13049 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13050 TRUE, FALSE, FALSE);
13051 if (h == NULL)
13052 return FALSE;
13053 if (h->root.type == bfd_link_hash_new)
13054 {
13055 h->root.type = bfd_link_hash_defined;
13056 h->root.u.def.section = htab->glink;
13057 h->root.u.def.value = 8;
13058 h->ref_regular = 1;
13059 h->def_regular = 1;
13060 h->ref_regular_nonweak = 1;
13061 h->forced_local = 1;
13062 h->non_elf = 0;
13063 h->root.linker_def = 1;
13064 }
13065 }
13066 plt0 = (htab->elf.splt->output_section->vma
13067 + htab->elf.splt->output_offset
13068 - 16);
13069 if (info->emitrelocations)
13070 {
13071 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13072 if (r == NULL)
13073 return FALSE;
13074 r->r_offset = (htab->glink->output_offset
13075 + htab->glink->output_section->vma);
13076 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13077 r->r_addend = plt0;
13078 }
13079 p = htab->glink->contents;
13080 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13081 bfd_put_64 (htab->glink->owner, plt0, p);
13082 p += 8;
13083 if (htab->opd_abi)
13084 {
13085 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13086 p += 4;
13087 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13088 p += 4;
13089 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13090 p += 4;
13091 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13092 p += 4;
13093 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13094 p += 4;
13095 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13096 p += 4;
13097 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13098 p += 4;
13099 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13100 p += 4;
13101 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13102 p += 4;
13103 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13104 p += 4;
13105 }
13106 else
13107 {
13108 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13109 p += 4;
13110 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13111 p += 4;
13112 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13113 p += 4;
13114 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13115 p += 4;
13116 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13117 p += 4;
13118 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13119 p += 4;
13120 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13121 p += 4;
13122 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13123 p += 4;
13124 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13125 p += 4;
13126 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13127 p += 4;
13128 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13129 p += 4;
13130 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13131 p += 4;
13132 }
13133 bfd_put_32 (htab->glink->owner, BCTR, p);
13134 p += 4;
13135 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13136 {
13137 bfd_put_32 (htab->glink->owner, NOP, p);
13138 p += 4;
13139 }
13140
13141 /* Build the .glink lazy link call stubs. */
13142 indx = 0;
13143 while (p < htab->glink->contents + htab->glink->rawsize)
13144 {
13145 if (htab->opd_abi)
13146 {
13147 if (indx < 0x8000)
13148 {
13149 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13150 p += 4;
13151 }
13152 else
13153 {
13154 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13155 p += 4;
13156 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13157 p);
13158 p += 4;
13159 }
13160 }
13161 bfd_put_32 (htab->glink->owner,
13162 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13163 indx++;
13164 p += 4;
13165 }
13166
13167 /* Build .glink global entry stubs. */
13168 if (htab->glink->size > htab->glink->rawsize)
13169 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13170 }
13171
13172 if (htab->brlt != NULL && htab->brlt->size != 0)
13173 {
13174 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13175 htab->brlt->size);
13176 if (htab->brlt->contents == NULL)
13177 return FALSE;
13178 }
13179 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13180 {
13181 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13182 htab->relbrlt->size);
13183 if (htab->relbrlt->contents == NULL)
13184 return FALSE;
13185 }
13186
13187 /* Build the stubs as directed by the stub hash table. */
13188 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13189
13190 for (group = htab->group; group != NULL; group = group->next)
13191 if (group->needs_save_res)
13192 {
13193 stub_sec = group->stub_sec;
13194 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13195 htab->sfpr->size);
13196 if (htab->params->emit_stub_syms)
13197 {
13198 unsigned int i;
13199
13200 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13201 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13202 return FALSE;
13203 }
13204 stub_sec->size += htab->sfpr->size;
13205 }
13206
13207 if (htab->relbrlt != NULL)
13208 htab->relbrlt->reloc_count = 0;
13209
13210 if (htab->params->plt_stub_align != 0)
13211 for (stub_sec = htab->params->stub_bfd->sections;
13212 stub_sec != NULL;
13213 stub_sec = stub_sec->next)
13214 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13215 stub_sec->size = ((stub_sec->size
13216 + (1 << htab->params->plt_stub_align) - 1)
13217 & -(1 << htab->params->plt_stub_align));
13218
13219 for (stub_sec = htab->params->stub_bfd->sections;
13220 stub_sec != NULL;
13221 stub_sec = stub_sec->next)
13222 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13223 {
13224 stub_sec_count += 1;
13225 if (stub_sec->rawsize != stub_sec->size
13226 && (htab->stub_iteration <= STUB_SHRINK_ITER
13227 || stub_sec->rawsize < stub_sec->size))
13228 break;
13229 }
13230
13231 /* Note that the glink_eh_frame check here is not only testing that
13232 the generated size matched the calculated size but also that
13233 bfd_elf_discard_info didn't make any changes to the section. */
13234 if (stub_sec != NULL
13235 || (htab->glink_eh_frame != NULL
13236 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13237 {
13238 htab->stub_error = TRUE;
13239 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13240 }
13241
13242 if (htab->stub_error)
13243 return FALSE;
13244
13245 if (stats != NULL)
13246 {
13247 *stats = bfd_malloc (500);
13248 if (*stats == NULL)
13249 return FALSE;
13250
13251 sprintf (*stats, _("linker stubs in %u group%s\n"
13252 " branch %lu\n"
13253 " toc adjust %lu\n"
13254 " long branch %lu\n"
13255 " long toc adj %lu\n"
13256 " plt call %lu\n"
13257 " plt call toc %lu\n"
13258 " global entry %lu"),
13259 stub_sec_count,
13260 stub_sec_count == 1 ? "" : "s",
13261 htab->stub_count[ppc_stub_long_branch - 1],
13262 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13263 htab->stub_count[ppc_stub_plt_branch - 1],
13264 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13265 htab->stub_count[ppc_stub_plt_call - 1],
13266 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13267 htab->stub_count[ppc_stub_global_entry - 1]);
13268 }
13269 return TRUE;
13270 }
13271
13272 /* What to do when ld finds relocations against symbols defined in
13273 discarded sections. */
13274
13275 static unsigned int
13276 ppc64_elf_action_discarded (asection *sec)
13277 {
13278 if (strcmp (".opd", sec->name) == 0)
13279 return 0;
13280
13281 if (strcmp (".toc", sec->name) == 0)
13282 return 0;
13283
13284 if (strcmp (".toc1", sec->name) == 0)
13285 return 0;
13286
13287 return _bfd_elf_default_action_discarded (sec);
13288 }
13289
13290 /* The RELOCATE_SECTION function is called by the ELF backend linker
13291 to handle the relocations for a section.
13292
13293 The relocs are always passed as Rela structures; if the section
13294 actually uses Rel structures, the r_addend field will always be
13295 zero.
13296
13297 This function is responsible for adjust the section contents as
13298 necessary, and (if using Rela relocs and generating a
13299 relocatable output file) adjusting the reloc addend as
13300 necessary.
13301
13302 This function does not have to worry about setting the reloc
13303 address or the reloc symbol index.
13304
13305 LOCAL_SYMS is a pointer to the swapped in local symbols.
13306
13307 LOCAL_SECTIONS is an array giving the section in the input file
13308 corresponding to the st_shndx field of each local symbol.
13309
13310 The global hash table entry for the global symbols can be found
13311 via elf_sym_hashes (input_bfd).
13312
13313 When generating relocatable output, this function must handle
13314 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13315 going to be the section symbol corresponding to the output
13316 section, which means that the addend must be adjusted
13317 accordingly. */
13318
13319 static bfd_boolean
13320 ppc64_elf_relocate_section (bfd *output_bfd,
13321 struct bfd_link_info *info,
13322 bfd *input_bfd,
13323 asection *input_section,
13324 bfd_byte *contents,
13325 Elf_Internal_Rela *relocs,
13326 Elf_Internal_Sym *local_syms,
13327 asection **local_sections)
13328 {
13329 struct ppc_link_hash_table *htab;
13330 Elf_Internal_Shdr *symtab_hdr;
13331 struct elf_link_hash_entry **sym_hashes;
13332 Elf_Internal_Rela *rel;
13333 Elf_Internal_Rela *wrel;
13334 Elf_Internal_Rela *relend;
13335 Elf_Internal_Rela outrel;
13336 bfd_byte *loc;
13337 struct got_entry **local_got_ents;
13338 bfd_vma TOCstart;
13339 bfd_boolean ret = TRUE;
13340 bfd_boolean is_opd;
13341 /* Assume 'at' branch hints. */
13342 bfd_boolean is_isa_v2 = TRUE;
13343 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13344
13345 /* Initialize howto table if needed. */
13346 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13347 ppc_howto_init ();
13348
13349 htab = ppc_hash_table (info);
13350 if (htab == NULL)
13351 return FALSE;
13352
13353 /* Don't relocate stub sections. */
13354 if (input_section->owner == htab->params->stub_bfd)
13355 return TRUE;
13356
13357 BFD_ASSERT (is_ppc64_elf (input_bfd));
13358
13359 local_got_ents = elf_local_got_ents (input_bfd);
13360 TOCstart = elf_gp (output_bfd);
13361 symtab_hdr = &elf_symtab_hdr (input_bfd);
13362 sym_hashes = elf_sym_hashes (input_bfd);
13363 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13364
13365 rel = wrel = relocs;
13366 relend = relocs + input_section->reloc_count;
13367 for (; rel < relend; wrel++, rel++)
13368 {
13369 enum elf_ppc64_reloc_type r_type;
13370 bfd_vma addend;
13371 bfd_reloc_status_type r;
13372 Elf_Internal_Sym *sym;
13373 asection *sec;
13374 struct elf_link_hash_entry *h_elf;
13375 struct ppc_link_hash_entry *h;
13376 struct ppc_link_hash_entry *fdh;
13377 const char *sym_name;
13378 unsigned long r_symndx, toc_symndx;
13379 bfd_vma toc_addend;
13380 unsigned char tls_mask, tls_gd, tls_type;
13381 unsigned char sym_type;
13382 bfd_vma relocation;
13383 bfd_boolean unresolved_reloc;
13384 bfd_boolean warned;
13385 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13386 unsigned int insn;
13387 unsigned int mask;
13388 struct ppc_stub_hash_entry *stub_entry;
13389 bfd_vma max_br_offset;
13390 bfd_vma from;
13391 Elf_Internal_Rela orig_rel;
13392 reloc_howto_type *howto;
13393 struct reloc_howto_struct alt_howto;
13394
13395 again:
13396 orig_rel = *rel;
13397
13398 r_type = ELF64_R_TYPE (rel->r_info);
13399 r_symndx = ELF64_R_SYM (rel->r_info);
13400
13401 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13402 symbol of the previous ADDR64 reloc. The symbol gives us the
13403 proper TOC base to use. */
13404 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13405 && wrel != relocs
13406 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13407 && is_opd)
13408 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13409
13410 sym = NULL;
13411 sec = NULL;
13412 h_elf = NULL;
13413 sym_name = NULL;
13414 unresolved_reloc = FALSE;
13415 warned = FALSE;
13416
13417 if (r_symndx < symtab_hdr->sh_info)
13418 {
13419 /* It's a local symbol. */
13420 struct _opd_sec_data *opd;
13421
13422 sym = local_syms + r_symndx;
13423 sec = local_sections[r_symndx];
13424 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13425 sym_type = ELF64_ST_TYPE (sym->st_info);
13426 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13427 opd = get_opd_info (sec);
13428 if (opd != NULL && opd->adjust != NULL)
13429 {
13430 long adjust = opd->adjust[OPD_NDX (sym->st_value
13431 + rel->r_addend)];
13432 if (adjust == -1)
13433 relocation = 0;
13434 else
13435 {
13436 /* If this is a relocation against the opd section sym
13437 and we have edited .opd, adjust the reloc addend so
13438 that ld -r and ld --emit-relocs output is correct.
13439 If it is a reloc against some other .opd symbol,
13440 then the symbol value will be adjusted later. */
13441 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13442 rel->r_addend += adjust;
13443 else
13444 relocation += adjust;
13445 }
13446 }
13447 }
13448 else
13449 {
13450 bfd_boolean ignored;
13451
13452 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13453 r_symndx, symtab_hdr, sym_hashes,
13454 h_elf, sec, relocation,
13455 unresolved_reloc, warned, ignored);
13456 sym_name = h_elf->root.root.string;
13457 sym_type = h_elf->type;
13458 if (sec != NULL
13459 && sec->owner == output_bfd
13460 && strcmp (sec->name, ".opd") == 0)
13461 {
13462 /* This is a symbol defined in a linker script. All
13463 such are defined in output sections, even those
13464 defined by simple assignment from a symbol defined in
13465 an input section. Transfer the symbol to an
13466 appropriate input .opd section, so that a branch to
13467 this symbol will be mapped to the location specified
13468 by the opd entry. */
13469 struct bfd_link_order *lo;
13470 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13471 if (lo->type == bfd_indirect_link_order)
13472 {
13473 asection *isec = lo->u.indirect.section;
13474 if (h_elf->root.u.def.value >= isec->output_offset
13475 && h_elf->root.u.def.value < (isec->output_offset
13476 + isec->size))
13477 {
13478 h_elf->root.u.def.value -= isec->output_offset;
13479 h_elf->root.u.def.section = isec;
13480 sec = isec;
13481 break;
13482 }
13483 }
13484 }
13485 }
13486 h = (struct ppc_link_hash_entry *) h_elf;
13487
13488 if (sec != NULL && discarded_section (sec))
13489 {
13490 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13491 input_bfd, input_section,
13492 contents + rel->r_offset);
13493 wrel->r_offset = rel->r_offset;
13494 wrel->r_info = 0;
13495 wrel->r_addend = 0;
13496
13497 /* For ld -r, remove relocations in debug sections against
13498 sections defined in discarded sections. Not done for
13499 non-debug to preserve relocs in .eh_frame which the
13500 eh_frame editing code expects to be present. */
13501 if (bfd_link_relocatable (info)
13502 && (input_section->flags & SEC_DEBUGGING))
13503 wrel--;
13504
13505 continue;
13506 }
13507
13508 if (bfd_link_relocatable (info))
13509 goto copy_reloc;
13510
13511 if (h != NULL && &h->elf == htab->elf.hgot)
13512 {
13513 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13514 sec = bfd_abs_section_ptr;
13515 unresolved_reloc = FALSE;
13516 }
13517
13518 /* TLS optimizations. Replace instruction sequences and relocs
13519 based on information we collected in tls_optimize. We edit
13520 RELOCS so that --emit-relocs will output something sensible
13521 for the final instruction stream. */
13522 tls_mask = 0;
13523 tls_gd = 0;
13524 toc_symndx = 0;
13525 if (h != NULL)
13526 tls_mask = h->tls_mask;
13527 else if (local_got_ents != NULL)
13528 {
13529 struct plt_entry **local_plt = (struct plt_entry **)
13530 (local_got_ents + symtab_hdr->sh_info);
13531 unsigned char *lgot_masks = (unsigned char *)
13532 (local_plt + symtab_hdr->sh_info);
13533 tls_mask = lgot_masks[r_symndx];
13534 }
13535 if (tls_mask == 0
13536 && (r_type == R_PPC64_TLS
13537 || r_type == R_PPC64_TLSGD
13538 || r_type == R_PPC64_TLSLD))
13539 {
13540 /* Check for toc tls entries. */
13541 unsigned char *toc_tls;
13542
13543 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13544 &local_syms, rel, input_bfd))
13545 return FALSE;
13546
13547 if (toc_tls)
13548 tls_mask = *toc_tls;
13549 }
13550
13551 /* Check that tls relocs are used with tls syms, and non-tls
13552 relocs are used with non-tls syms. */
13553 if (r_symndx != STN_UNDEF
13554 && r_type != R_PPC64_NONE
13555 && (h == NULL
13556 || h->elf.root.type == bfd_link_hash_defined
13557 || h->elf.root.type == bfd_link_hash_defweak)
13558 && (IS_PPC64_TLS_RELOC (r_type)
13559 != (sym_type == STT_TLS
13560 || (sym_type == STT_SECTION
13561 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13562 {
13563 if (tls_mask != 0
13564 && (r_type == R_PPC64_TLS
13565 || r_type == R_PPC64_TLSGD
13566 || r_type == R_PPC64_TLSLD))
13567 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13568 ;
13569 else
13570 info->callbacks->einfo
13571 (!IS_PPC64_TLS_RELOC (r_type)
13572 /* xgettext:c-format */
13573 ? _("%P: %H: %s used with TLS symbol `%T'\n")
13574 /* xgettext:c-format */
13575 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
13576 input_bfd, input_section, rel->r_offset,
13577 ppc64_elf_howto_table[r_type]->name,
13578 sym_name);
13579 }
13580
13581 /* Ensure reloc mapping code below stays sane. */
13582 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13583 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13584 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13585 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13586 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13587 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13588 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13589 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13590 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13591 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13592 abort ();
13593
13594 switch (r_type)
13595 {
13596 default:
13597 break;
13598
13599 case R_PPC64_LO_DS_OPT:
13600 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13601 if ((insn & (0x3f << 26)) != 58u << 26)
13602 abort ();
13603 insn += (14u << 26) - (58u << 26);
13604 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13605 r_type = R_PPC64_TOC16_LO;
13606 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13607 break;
13608
13609 case R_PPC64_TOC16:
13610 case R_PPC64_TOC16_LO:
13611 case R_PPC64_TOC16_DS:
13612 case R_PPC64_TOC16_LO_DS:
13613 {
13614 /* Check for toc tls entries. */
13615 unsigned char *toc_tls;
13616 int retval;
13617
13618 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13619 &local_syms, rel, input_bfd);
13620 if (retval == 0)
13621 return FALSE;
13622
13623 if (toc_tls)
13624 {
13625 tls_mask = *toc_tls;
13626 if (r_type == R_PPC64_TOC16_DS
13627 || r_type == R_PPC64_TOC16_LO_DS)
13628 {
13629 if (tls_mask != 0
13630 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13631 goto toctprel;
13632 }
13633 else
13634 {
13635 /* If we found a GD reloc pair, then we might be
13636 doing a GD->IE transition. */
13637 if (retval == 2)
13638 {
13639 tls_gd = TLS_TPRELGD;
13640 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13641 goto tls_ldgd_opt;
13642 }
13643 else if (retval == 3)
13644 {
13645 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13646 goto tls_ldgd_opt;
13647 }
13648 }
13649 }
13650 }
13651 break;
13652
13653 case R_PPC64_GOT_TPREL16_HI:
13654 case R_PPC64_GOT_TPREL16_HA:
13655 if (tls_mask != 0
13656 && (tls_mask & TLS_TPREL) == 0)
13657 {
13658 rel->r_offset -= d_offset;
13659 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13660 r_type = R_PPC64_NONE;
13661 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13662 }
13663 break;
13664
13665 case R_PPC64_GOT_TPREL16_DS:
13666 case R_PPC64_GOT_TPREL16_LO_DS:
13667 if (tls_mask != 0
13668 && (tls_mask & TLS_TPREL) == 0)
13669 {
13670 toctprel:
13671 insn = bfd_get_32 (input_bfd,
13672 contents + rel->r_offset - d_offset);
13673 insn &= 31 << 21;
13674 insn |= 0x3c0d0000; /* addis 0,13,0 */
13675 bfd_put_32 (input_bfd, insn,
13676 contents + rel->r_offset - d_offset);
13677 r_type = R_PPC64_TPREL16_HA;
13678 if (toc_symndx != 0)
13679 {
13680 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13681 rel->r_addend = toc_addend;
13682 /* We changed the symbol. Start over in order to
13683 get h, sym, sec etc. right. */
13684 goto again;
13685 }
13686 else
13687 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13688 }
13689 break;
13690
13691 case R_PPC64_TLS:
13692 if (tls_mask != 0
13693 && (tls_mask & TLS_TPREL) == 0)
13694 {
13695 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13696 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13697 if (insn == 0)
13698 abort ();
13699 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13700 /* Was PPC64_TLS which sits on insn boundary, now
13701 PPC64_TPREL16_LO which is at low-order half-word. */
13702 rel->r_offset += d_offset;
13703 r_type = R_PPC64_TPREL16_LO;
13704 if (toc_symndx != 0)
13705 {
13706 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13707 rel->r_addend = toc_addend;
13708 /* We changed the symbol. Start over in order to
13709 get h, sym, sec etc. right. */
13710 goto again;
13711 }
13712 else
13713 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13714 }
13715 break;
13716
13717 case R_PPC64_GOT_TLSGD16_HI:
13718 case R_PPC64_GOT_TLSGD16_HA:
13719 tls_gd = TLS_TPRELGD;
13720 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13721 goto tls_gdld_hi;
13722 break;
13723
13724 case R_PPC64_GOT_TLSLD16_HI:
13725 case R_PPC64_GOT_TLSLD16_HA:
13726 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13727 {
13728 tls_gdld_hi:
13729 if ((tls_mask & tls_gd) != 0)
13730 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13731 + R_PPC64_GOT_TPREL16_DS);
13732 else
13733 {
13734 rel->r_offset -= d_offset;
13735 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13736 r_type = R_PPC64_NONE;
13737 }
13738 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13739 }
13740 break;
13741
13742 case R_PPC64_GOT_TLSGD16:
13743 case R_PPC64_GOT_TLSGD16_LO:
13744 tls_gd = TLS_TPRELGD;
13745 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13746 goto tls_ldgd_opt;
13747 break;
13748
13749 case R_PPC64_GOT_TLSLD16:
13750 case R_PPC64_GOT_TLSLD16_LO:
13751 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13752 {
13753 unsigned int insn1, insn2, insn3;
13754 bfd_vma offset;
13755
13756 tls_ldgd_opt:
13757 offset = (bfd_vma) -1;
13758 /* If not using the newer R_PPC64_TLSGD/LD to mark
13759 __tls_get_addr calls, we must trust that the call
13760 stays with its arg setup insns, ie. that the next
13761 reloc is the __tls_get_addr call associated with
13762 the current reloc. Edit both insns. */
13763 if (input_section->has_tls_get_addr_call
13764 && rel + 1 < relend
13765 && branch_reloc_hash_match (input_bfd, rel + 1,
13766 htab->tls_get_addr,
13767 htab->tls_get_addr_fd))
13768 offset = rel[1].r_offset;
13769 /* We read the low GOT_TLS (or TOC16) insn because we
13770 need to keep the destination reg. It may be
13771 something other than the usual r3, and moved to r3
13772 before the call by intervening code. */
13773 insn1 = bfd_get_32 (input_bfd,
13774 contents + rel->r_offset - d_offset);
13775 if ((tls_mask & tls_gd) != 0)
13776 {
13777 /* IE */
13778 insn1 &= (0x1f << 21) | (0x1f << 16);
13779 insn1 |= 58 << 26; /* ld */
13780 insn2 = 0x7c636a14; /* add 3,3,13 */
13781 if (offset != (bfd_vma) -1)
13782 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13783 if ((tls_mask & TLS_EXPLICIT) == 0)
13784 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13785 + R_PPC64_GOT_TPREL16_DS);
13786 else
13787 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13788 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13789 }
13790 else
13791 {
13792 /* LE */
13793 insn1 &= 0x1f << 21;
13794 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13795 insn2 = 0x38630000; /* addi 3,3,0 */
13796 if (tls_gd == 0)
13797 {
13798 /* Was an LD reloc. */
13799 if (toc_symndx)
13800 sec = local_sections[toc_symndx];
13801 for (r_symndx = 0;
13802 r_symndx < symtab_hdr->sh_info;
13803 r_symndx++)
13804 if (local_sections[r_symndx] == sec)
13805 break;
13806 if (r_symndx >= symtab_hdr->sh_info)
13807 r_symndx = STN_UNDEF;
13808 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13809 if (r_symndx != STN_UNDEF)
13810 rel->r_addend -= (local_syms[r_symndx].st_value
13811 + sec->output_offset
13812 + sec->output_section->vma);
13813 }
13814 else if (toc_symndx != 0)
13815 {
13816 r_symndx = toc_symndx;
13817 rel->r_addend = toc_addend;
13818 }
13819 r_type = R_PPC64_TPREL16_HA;
13820 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13821 if (offset != (bfd_vma) -1)
13822 {
13823 rel[1].r_info = ELF64_R_INFO (r_symndx,
13824 R_PPC64_TPREL16_LO);
13825 rel[1].r_offset = offset + d_offset;
13826 rel[1].r_addend = rel->r_addend;
13827 }
13828 }
13829 bfd_put_32 (input_bfd, insn1,
13830 contents + rel->r_offset - d_offset);
13831 if (offset != (bfd_vma) -1)
13832 {
13833 insn3 = bfd_get_32 (input_bfd,
13834 contents + offset + 4);
13835 if (insn3 == NOP
13836 || insn3 == CROR_151515 || insn3 == CROR_313131)
13837 {
13838 rel[1].r_offset += 4;
13839 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13840 insn2 = NOP;
13841 }
13842 bfd_put_32 (input_bfd, insn2, contents + offset);
13843 }
13844 if ((tls_mask & tls_gd) == 0
13845 && (tls_gd == 0 || toc_symndx != 0))
13846 {
13847 /* We changed the symbol. Start over in order
13848 to get h, sym, sec etc. right. */
13849 goto again;
13850 }
13851 }
13852 break;
13853
13854 case R_PPC64_TLSGD:
13855 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13856 {
13857 unsigned int insn2, insn3;
13858 bfd_vma offset = rel->r_offset;
13859
13860 if ((tls_mask & TLS_TPRELGD) != 0)
13861 {
13862 /* IE */
13863 r_type = R_PPC64_NONE;
13864 insn2 = 0x7c636a14; /* add 3,3,13 */
13865 }
13866 else
13867 {
13868 /* LE */
13869 if (toc_symndx != 0)
13870 {
13871 r_symndx = toc_symndx;
13872 rel->r_addend = toc_addend;
13873 }
13874 r_type = R_PPC64_TPREL16_LO;
13875 rel->r_offset = offset + d_offset;
13876 insn2 = 0x38630000; /* addi 3,3,0 */
13877 }
13878 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13879 /* Zap the reloc on the _tls_get_addr call too. */
13880 BFD_ASSERT (offset == rel[1].r_offset);
13881 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13882 insn3 = bfd_get_32 (input_bfd,
13883 contents + offset + 4);
13884 if (insn3 == NOP
13885 || insn3 == CROR_151515 || insn3 == CROR_313131)
13886 {
13887 rel->r_offset += 4;
13888 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13889 insn2 = NOP;
13890 }
13891 bfd_put_32 (input_bfd, insn2, contents + offset);
13892 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13893 goto again;
13894 }
13895 break;
13896
13897 case R_PPC64_TLSLD:
13898 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13899 {
13900 unsigned int insn2, insn3;
13901 bfd_vma offset = rel->r_offset;
13902
13903 if (toc_symndx)
13904 sec = local_sections[toc_symndx];
13905 for (r_symndx = 0;
13906 r_symndx < symtab_hdr->sh_info;
13907 r_symndx++)
13908 if (local_sections[r_symndx] == sec)
13909 break;
13910 if (r_symndx >= symtab_hdr->sh_info)
13911 r_symndx = STN_UNDEF;
13912 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13913 if (r_symndx != STN_UNDEF)
13914 rel->r_addend -= (local_syms[r_symndx].st_value
13915 + sec->output_offset
13916 + sec->output_section->vma);
13917
13918 r_type = R_PPC64_TPREL16_LO;
13919 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13920 rel->r_offset = offset + d_offset;
13921 /* Zap the reloc on the _tls_get_addr call too. */
13922 BFD_ASSERT (offset == rel[1].r_offset);
13923 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13924 insn2 = 0x38630000; /* addi 3,3,0 */
13925 insn3 = bfd_get_32 (input_bfd,
13926 contents + offset + 4);
13927 if (insn3 == NOP
13928 || insn3 == CROR_151515 || insn3 == CROR_313131)
13929 {
13930 rel->r_offset += 4;
13931 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13932 insn2 = NOP;
13933 }
13934 bfd_put_32 (input_bfd, insn2, contents + offset);
13935 goto again;
13936 }
13937 break;
13938
13939 case R_PPC64_DTPMOD64:
13940 if (rel + 1 < relend
13941 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13942 && rel[1].r_offset == rel->r_offset + 8)
13943 {
13944 if ((tls_mask & TLS_GD) == 0)
13945 {
13946 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13947 if ((tls_mask & TLS_TPRELGD) != 0)
13948 r_type = R_PPC64_TPREL64;
13949 else
13950 {
13951 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13952 r_type = R_PPC64_NONE;
13953 }
13954 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13955 }
13956 }
13957 else
13958 {
13959 if ((tls_mask & TLS_LD) == 0)
13960 {
13961 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13962 r_type = R_PPC64_NONE;
13963 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13964 }
13965 }
13966 break;
13967
13968 case R_PPC64_TPREL64:
13969 if ((tls_mask & TLS_TPREL) == 0)
13970 {
13971 r_type = R_PPC64_NONE;
13972 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13973 }
13974 break;
13975
13976 case R_PPC64_ENTRY:
13977 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13978 if (!bfd_link_pic (info)
13979 && !info->traditional_format
13980 && relocation + 0x80008000 <= 0xffffffff)
13981 {
13982 unsigned int insn1, insn2;
13983
13984 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13985 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13986 if ((insn1 & ~0xfffc) == LD_R2_0R12
13987 && insn2 == ADD_R2_R2_R12)
13988 {
13989 bfd_put_32 (input_bfd,
13990 LIS_R2 + PPC_HA (relocation),
13991 contents + rel->r_offset);
13992 bfd_put_32 (input_bfd,
13993 ADDI_R2_R2 + PPC_LO (relocation),
13994 contents + rel->r_offset + 4);
13995 }
13996 }
13997 else
13998 {
13999 relocation -= (rel->r_offset
14000 + input_section->output_offset
14001 + input_section->output_section->vma);
14002 if (relocation + 0x80008000 <= 0xffffffff)
14003 {
14004 unsigned int insn1, insn2;
14005
14006 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14007 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14008 if ((insn1 & ~0xfffc) == LD_R2_0R12
14009 && insn2 == ADD_R2_R2_R12)
14010 {
14011 bfd_put_32 (input_bfd,
14012 ADDIS_R2_R12 + PPC_HA (relocation),
14013 contents + rel->r_offset);
14014 bfd_put_32 (input_bfd,
14015 ADDI_R2_R2 + PPC_LO (relocation),
14016 contents + rel->r_offset + 4);
14017 }
14018 }
14019 }
14020 break;
14021
14022 case R_PPC64_REL16_HA:
14023 /* If we are generating a non-PIC executable, edit
14024 . 0: addis 2,12,.TOC.-0b@ha
14025 . addi 2,2,.TOC.-0b@l
14026 used by ELFv2 global entry points to set up r2, to
14027 . lis 2,.TOC.@ha
14028 . addi 2,2,.TOC.@l
14029 if .TOC. is in range. */
14030 if (!bfd_link_pic (info)
14031 && !info->traditional_format
14032 && !htab->opd_abi
14033 && rel->r_addend == d_offset
14034 && h != NULL && &h->elf == htab->elf.hgot
14035 && rel + 1 < relend
14036 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14037 && rel[1].r_offset == rel->r_offset + 4
14038 && rel[1].r_addend == rel->r_addend + 4
14039 && relocation + 0x80008000 <= 0xffffffff)
14040 {
14041 unsigned int insn1, insn2;
14042 bfd_vma offset = rel->r_offset - d_offset;
14043 insn1 = bfd_get_32 (input_bfd, contents + offset);
14044 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14045 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14046 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14047 {
14048 r_type = R_PPC64_ADDR16_HA;
14049 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14050 rel->r_addend -= d_offset;
14051 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14052 rel[1].r_addend -= d_offset + 4;
14053 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14054 }
14055 }
14056 break;
14057 }
14058
14059 /* Handle other relocations that tweak non-addend part of insn. */
14060 insn = 0;
14061 max_br_offset = 1 << 25;
14062 addend = rel->r_addend;
14063 reloc_dest = DEST_NORMAL;
14064 switch (r_type)
14065 {
14066 default:
14067 break;
14068
14069 case R_PPC64_TOCSAVE:
14070 if (relocation + addend == (rel->r_offset
14071 + input_section->output_offset
14072 + input_section->output_section->vma)
14073 && tocsave_find (htab, NO_INSERT,
14074 &local_syms, rel, input_bfd))
14075 {
14076 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14077 if (insn == NOP
14078 || insn == CROR_151515 || insn == CROR_313131)
14079 bfd_put_32 (input_bfd,
14080 STD_R2_0R1 + STK_TOC (htab),
14081 contents + rel->r_offset);
14082 }
14083 break;
14084
14085 /* Branch taken prediction relocations. */
14086 case R_PPC64_ADDR14_BRTAKEN:
14087 case R_PPC64_REL14_BRTAKEN:
14088 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14089 /* Fall through. */
14090
14091 /* Branch not taken prediction relocations. */
14092 case R_PPC64_ADDR14_BRNTAKEN:
14093 case R_PPC64_REL14_BRNTAKEN:
14094 insn |= bfd_get_32 (input_bfd,
14095 contents + rel->r_offset) & ~(0x01 << 21);
14096 /* Fall through. */
14097
14098 case R_PPC64_REL14:
14099 max_br_offset = 1 << 15;
14100 /* Fall through. */
14101
14102 case R_PPC64_REL24:
14103 /* Calls to functions with a different TOC, such as calls to
14104 shared objects, need to alter the TOC pointer. This is
14105 done using a linkage stub. A REL24 branching to these
14106 linkage stubs needs to be followed by a nop, as the nop
14107 will be replaced with an instruction to restore the TOC
14108 base pointer. */
14109 fdh = h;
14110 if (h != NULL
14111 && h->oh != NULL
14112 && h->oh->is_func_descriptor)
14113 fdh = ppc_follow_link (h->oh);
14114 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14115 htab);
14116 if (stub_entry != NULL
14117 && (stub_entry->stub_type == ppc_stub_plt_call
14118 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14119 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14120 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14121 {
14122 bfd_boolean can_plt_call = FALSE;
14123
14124 /* All of these stubs will modify r2, so there must be a
14125 branch and link followed by a nop. The nop is
14126 replaced by an insn to restore r2. */
14127 if (rel->r_offset + 8 <= input_section->size)
14128 {
14129 unsigned long br;
14130
14131 br = bfd_get_32 (input_bfd,
14132 contents + rel->r_offset);
14133 if ((br & 1) != 0)
14134 {
14135 unsigned long nop;
14136
14137 nop = bfd_get_32 (input_bfd,
14138 contents + rel->r_offset + 4);
14139 if (nop == NOP
14140 || nop == CROR_151515 || nop == CROR_313131)
14141 {
14142 if (h != NULL
14143 && (h == htab->tls_get_addr_fd
14144 || h == htab->tls_get_addr)
14145 && htab->params->tls_get_addr_opt)
14146 {
14147 /* Special stub used, leave nop alone. */
14148 }
14149 else
14150 bfd_put_32 (input_bfd,
14151 LD_R2_0R1 + STK_TOC (htab),
14152 contents + rel->r_offset + 4);
14153 can_plt_call = TRUE;
14154 }
14155 }
14156 }
14157
14158 if (!can_plt_call && h != NULL)
14159 {
14160 const char *name = h->elf.root.root.string;
14161
14162 if (*name == '.')
14163 ++name;
14164
14165 if (strncmp (name, "__libc_start_main", 17) == 0
14166 && (name[17] == 0 || name[17] == '@'))
14167 {
14168 /* Allow crt1 branch to go via a toc adjusting
14169 stub. Other calls that never return could do
14170 the same, if we could detect such. */
14171 can_plt_call = TRUE;
14172 }
14173 }
14174
14175 if (!can_plt_call)
14176 {
14177 /* g++ as of 20130507 emits self-calls without a
14178 following nop. This is arguably wrong since we
14179 have conflicting information. On the one hand a
14180 global symbol and on the other a local call
14181 sequence, but don't error for this special case.
14182 It isn't possible to cheaply verify we have
14183 exactly such a call. Allow all calls to the same
14184 section. */
14185 asection *code_sec = sec;
14186
14187 if (get_opd_info (sec) != NULL)
14188 {
14189 bfd_vma off = (relocation + addend
14190 - sec->output_section->vma
14191 - sec->output_offset);
14192
14193 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14194 }
14195 if (code_sec == input_section)
14196 can_plt_call = TRUE;
14197 }
14198
14199 if (!can_plt_call)
14200 {
14201 if (stub_entry->stub_type == ppc_stub_plt_call
14202 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14203 info->callbacks->einfo
14204 /* xgettext:c-format */
14205 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14206 "recompile with -fPIC\n"),
14207 input_bfd, input_section, rel->r_offset, sym_name);
14208 else
14209 info->callbacks->einfo
14210 /* xgettext:c-format */
14211 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
14212 "(-mcmodel=small toc adjust stub)\n"),
14213 input_bfd, input_section, rel->r_offset, sym_name);
14214
14215 bfd_set_error (bfd_error_bad_value);
14216 ret = FALSE;
14217 }
14218
14219 if (can_plt_call
14220 && (stub_entry->stub_type == ppc_stub_plt_call
14221 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14222 unresolved_reloc = FALSE;
14223 }
14224
14225 if ((stub_entry == NULL
14226 || stub_entry->stub_type == ppc_stub_long_branch
14227 || stub_entry->stub_type == ppc_stub_plt_branch)
14228 && get_opd_info (sec) != NULL)
14229 {
14230 /* The branch destination is the value of the opd entry. */
14231 bfd_vma off = (relocation + addend
14232 - sec->output_section->vma
14233 - sec->output_offset);
14234 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14235 if (dest != (bfd_vma) -1)
14236 {
14237 relocation = dest;
14238 addend = 0;
14239 reloc_dest = DEST_OPD;
14240 }
14241 }
14242
14243 /* If the branch is out of reach we ought to have a long
14244 branch stub. */
14245 from = (rel->r_offset
14246 + input_section->output_offset
14247 + input_section->output_section->vma);
14248
14249 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14250 ? fdh->elf.other
14251 : sym->st_other);
14252
14253 if (stub_entry != NULL
14254 && (stub_entry->stub_type == ppc_stub_long_branch
14255 || stub_entry->stub_type == ppc_stub_plt_branch)
14256 && (r_type == R_PPC64_ADDR14_BRTAKEN
14257 || r_type == R_PPC64_ADDR14_BRNTAKEN
14258 || (relocation + addend - from + max_br_offset
14259 < 2 * max_br_offset)))
14260 /* Don't use the stub if this branch is in range. */
14261 stub_entry = NULL;
14262
14263 if (stub_entry != NULL)
14264 {
14265 /* Munge up the value and addend so that we call the stub
14266 rather than the procedure directly. */
14267 asection *stub_sec = stub_entry->group->stub_sec;
14268
14269 if (stub_entry->stub_type == ppc_stub_save_res)
14270 relocation += (stub_sec->output_offset
14271 + stub_sec->output_section->vma
14272 + stub_sec->size - htab->sfpr->size
14273 - htab->sfpr->output_offset
14274 - htab->sfpr->output_section->vma);
14275 else
14276 relocation = (stub_entry->stub_offset
14277 + stub_sec->output_offset
14278 + stub_sec->output_section->vma);
14279 addend = 0;
14280 reloc_dest = DEST_STUB;
14281
14282 if ((stub_entry->stub_type == ppc_stub_plt_call
14283 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14284 && (ALWAYS_EMIT_R2SAVE
14285 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14286 && rel + 1 < relend
14287 && rel[1].r_offset == rel->r_offset + 4
14288 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14289 relocation += 4;
14290 }
14291
14292 if (insn != 0)
14293 {
14294 if (is_isa_v2)
14295 {
14296 /* Set 'a' bit. This is 0b00010 in BO field for branch
14297 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14298 for branch on CTR insns (BO == 1a00t or 1a01t). */
14299 if ((insn & (0x14 << 21)) == (0x04 << 21))
14300 insn |= 0x02 << 21;
14301 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14302 insn |= 0x08 << 21;
14303 else
14304 break;
14305 }
14306 else
14307 {
14308 /* Invert 'y' bit if not the default. */
14309 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14310 insn ^= 0x01 << 21;
14311 }
14312
14313 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14314 }
14315
14316 /* NOP out calls to undefined weak functions.
14317 We can thus call a weak function without first
14318 checking whether the function is defined. */
14319 else if (h != NULL
14320 && h->elf.root.type == bfd_link_hash_undefweak
14321 && h->elf.dynindx == -1
14322 && r_type == R_PPC64_REL24
14323 && relocation == 0
14324 && addend == 0)
14325 {
14326 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14327 goto copy_reloc;
14328 }
14329 break;
14330 }
14331
14332 /* Set `addend'. */
14333 tls_type = 0;
14334 switch (r_type)
14335 {
14336 default:
14337 info->callbacks->einfo
14338 /* xgettext:c-format */
14339 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14340 input_bfd, (int) r_type, sym_name);
14341
14342 bfd_set_error (bfd_error_bad_value);
14343 ret = FALSE;
14344 goto copy_reloc;
14345
14346 case R_PPC64_NONE:
14347 case R_PPC64_TLS:
14348 case R_PPC64_TLSGD:
14349 case R_PPC64_TLSLD:
14350 case R_PPC64_TOCSAVE:
14351 case R_PPC64_GNU_VTINHERIT:
14352 case R_PPC64_GNU_VTENTRY:
14353 case R_PPC64_ENTRY:
14354 goto copy_reloc;
14355
14356 /* GOT16 relocations. Like an ADDR16 using the symbol's
14357 address in the GOT as relocation value instead of the
14358 symbol's value itself. Also, create a GOT entry for the
14359 symbol and put the symbol value there. */
14360 case R_PPC64_GOT_TLSGD16:
14361 case R_PPC64_GOT_TLSGD16_LO:
14362 case R_PPC64_GOT_TLSGD16_HI:
14363 case R_PPC64_GOT_TLSGD16_HA:
14364 tls_type = TLS_TLS | TLS_GD;
14365 goto dogot;
14366
14367 case R_PPC64_GOT_TLSLD16:
14368 case R_PPC64_GOT_TLSLD16_LO:
14369 case R_PPC64_GOT_TLSLD16_HI:
14370 case R_PPC64_GOT_TLSLD16_HA:
14371 tls_type = TLS_TLS | TLS_LD;
14372 goto dogot;
14373
14374 case R_PPC64_GOT_TPREL16_DS:
14375 case R_PPC64_GOT_TPREL16_LO_DS:
14376 case R_PPC64_GOT_TPREL16_HI:
14377 case R_PPC64_GOT_TPREL16_HA:
14378 tls_type = TLS_TLS | TLS_TPREL;
14379 goto dogot;
14380
14381 case R_PPC64_GOT_DTPREL16_DS:
14382 case R_PPC64_GOT_DTPREL16_LO_DS:
14383 case R_PPC64_GOT_DTPREL16_HI:
14384 case R_PPC64_GOT_DTPREL16_HA:
14385 tls_type = TLS_TLS | TLS_DTPREL;
14386 goto dogot;
14387
14388 case R_PPC64_GOT16:
14389 case R_PPC64_GOT16_LO:
14390 case R_PPC64_GOT16_HI:
14391 case R_PPC64_GOT16_HA:
14392 case R_PPC64_GOT16_DS:
14393 case R_PPC64_GOT16_LO_DS:
14394 dogot:
14395 {
14396 /* Relocation is to the entry for this symbol in the global
14397 offset table. */
14398 asection *got;
14399 bfd_vma *offp;
14400 bfd_vma off;
14401 unsigned long indx = 0;
14402 struct got_entry *ent;
14403
14404 if (tls_type == (TLS_TLS | TLS_LD)
14405 && (h == NULL
14406 || !h->elf.def_dynamic))
14407 ent = ppc64_tlsld_got (input_bfd);
14408 else
14409 {
14410
14411 if (h != NULL)
14412 {
14413 bfd_boolean dyn = htab->elf.dynamic_sections_created;
14414 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info),
14415 &h->elf)
14416 || (bfd_link_pic (info)
14417 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
14418 /* This is actually a static link, or it is a
14419 -Bsymbolic link and the symbol is defined
14420 locally, or the symbol was forced to be local
14421 because of a version file. */
14422 ;
14423 else
14424 {
14425 BFD_ASSERT (h->elf.dynindx != -1);
14426 indx = h->elf.dynindx;
14427 unresolved_reloc = FALSE;
14428 }
14429 ent = h->elf.got.glist;
14430 }
14431 else
14432 {
14433 if (local_got_ents == NULL)
14434 abort ();
14435 ent = local_got_ents[r_symndx];
14436 }
14437
14438 for (; ent != NULL; ent = ent->next)
14439 if (ent->addend == orig_rel.r_addend
14440 && ent->owner == input_bfd
14441 && ent->tls_type == tls_type)
14442 break;
14443 }
14444
14445 if (ent == NULL)
14446 abort ();
14447 if (ent->is_indirect)
14448 ent = ent->got.ent;
14449 offp = &ent->got.offset;
14450 got = ppc64_elf_tdata (ent->owner)->got;
14451 if (got == NULL)
14452 abort ();
14453
14454 /* The offset must always be a multiple of 8. We use the
14455 least significant bit to record whether we have already
14456 processed this entry. */
14457 off = *offp;
14458 if ((off & 1) != 0)
14459 off &= ~1;
14460 else
14461 {
14462 /* Generate relocs for the dynamic linker, except in
14463 the case of TLSLD where we'll use one entry per
14464 module. */
14465 asection *relgot;
14466 bfd_boolean ifunc;
14467
14468 *offp = off | 1;
14469 relgot = NULL;
14470 ifunc = (h != NULL
14471 ? h->elf.type == STT_GNU_IFUNC
14472 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14473 if (ifunc)
14474 relgot = htab->elf.irelplt;
14475 else if ((bfd_link_pic (info) || indx != 0)
14476 && (h == NULL
14477 || (tls_type == (TLS_TLS | TLS_LD)
14478 && !h->elf.def_dynamic)
14479 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
14480 || h->elf.root.type != bfd_link_hash_undefweak))
14481 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14482 if (relgot != NULL)
14483 {
14484 outrel.r_offset = (got->output_section->vma
14485 + got->output_offset
14486 + off);
14487 outrel.r_addend = addend;
14488 if (tls_type & (TLS_LD | TLS_GD))
14489 {
14490 outrel.r_addend = 0;
14491 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14492 if (tls_type == (TLS_TLS | TLS_GD))
14493 {
14494 loc = relgot->contents;
14495 loc += (relgot->reloc_count++
14496 * sizeof (Elf64_External_Rela));
14497 bfd_elf64_swap_reloca_out (output_bfd,
14498 &outrel, loc);
14499 outrel.r_offset += 8;
14500 outrel.r_addend = addend;
14501 outrel.r_info
14502 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14503 }
14504 }
14505 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14506 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14507 else if (tls_type == (TLS_TLS | TLS_TPREL))
14508 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14509 else if (indx != 0)
14510 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14511 else
14512 {
14513 if (ifunc)
14514 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14515 else
14516 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14517
14518 /* Write the .got section contents for the sake
14519 of prelink. */
14520 loc = got->contents + off;
14521 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14522 loc);
14523 }
14524
14525 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14526 {
14527 outrel.r_addend += relocation;
14528 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14529 {
14530 if (htab->elf.tls_sec == NULL)
14531 outrel.r_addend = 0;
14532 else
14533 outrel.r_addend -= htab->elf.tls_sec->vma;
14534 }
14535 }
14536 loc = relgot->contents;
14537 loc += (relgot->reloc_count++
14538 * sizeof (Elf64_External_Rela));
14539 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14540 }
14541
14542 /* Init the .got section contents here if we're not
14543 emitting a reloc. */
14544 else
14545 {
14546 relocation += addend;
14547 if (tls_type == (TLS_TLS | TLS_LD))
14548 relocation = 1;
14549 else if (tls_type != 0)
14550 {
14551 if (htab->elf.tls_sec == NULL)
14552 relocation = 0;
14553 else
14554 {
14555 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14556 if (tls_type == (TLS_TLS | TLS_TPREL))
14557 relocation += DTP_OFFSET - TP_OFFSET;
14558 }
14559
14560 if (tls_type == (TLS_TLS | TLS_GD))
14561 {
14562 bfd_put_64 (output_bfd, relocation,
14563 got->contents + off + 8);
14564 relocation = 1;
14565 }
14566 }
14567
14568 bfd_put_64 (output_bfd, relocation,
14569 got->contents + off);
14570 }
14571 }
14572
14573 if (off >= (bfd_vma) -2)
14574 abort ();
14575
14576 relocation = got->output_section->vma + got->output_offset + off;
14577 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14578 }
14579 break;
14580
14581 case R_PPC64_PLT16_HA:
14582 case R_PPC64_PLT16_HI:
14583 case R_PPC64_PLT16_LO:
14584 case R_PPC64_PLT32:
14585 case R_PPC64_PLT64:
14586 /* Relocation is to the entry for this symbol in the
14587 procedure linkage table. */
14588 {
14589 struct plt_entry **plt_list = NULL;
14590 if (h != NULL)
14591 plt_list = &h->elf.plt.plist;
14592 else if (local_got_ents != NULL)
14593 {
14594 struct plt_entry **local_plt = (struct plt_entry **)
14595 (local_got_ents + symtab_hdr->sh_info);
14596 unsigned char *local_got_tls_masks = (unsigned char *)
14597 (local_plt + symtab_hdr->sh_info);
14598 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14599 plt_list = local_plt + r_symndx;
14600 }
14601 if (plt_list)
14602 {
14603 struct plt_entry *ent;
14604
14605 for (ent = *plt_list; ent != NULL; ent = ent->next)
14606 if (ent->plt.offset != (bfd_vma) -1
14607 && ent->addend == orig_rel.r_addend)
14608 {
14609 asection *plt;
14610
14611 plt = htab->elf.splt;
14612 if (!htab->elf.dynamic_sections_created
14613 || h == NULL
14614 || h->elf.dynindx == -1)
14615 plt = htab->elf.iplt;
14616 relocation = (plt->output_section->vma
14617 + plt->output_offset
14618 + ent->plt.offset);
14619 addend = 0;
14620 unresolved_reloc = FALSE;
14621 break;
14622 }
14623 }
14624 }
14625 break;
14626
14627 case R_PPC64_TOC:
14628 /* Relocation value is TOC base. */
14629 relocation = TOCstart;
14630 if (r_symndx == STN_UNDEF)
14631 relocation += htab->sec_info[input_section->id].toc_off;
14632 else if (unresolved_reloc)
14633 ;
14634 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14635 relocation += htab->sec_info[sec->id].toc_off;
14636 else
14637 unresolved_reloc = TRUE;
14638 goto dodyn;
14639
14640 /* TOC16 relocs. We want the offset relative to the TOC base,
14641 which is the address of the start of the TOC plus 0x8000.
14642 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14643 in this order. */
14644 case R_PPC64_TOC16:
14645 case R_PPC64_TOC16_LO:
14646 case R_PPC64_TOC16_HI:
14647 case R_PPC64_TOC16_DS:
14648 case R_PPC64_TOC16_LO_DS:
14649 case R_PPC64_TOC16_HA:
14650 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14651 break;
14652
14653 /* Relocate against the beginning of the section. */
14654 case R_PPC64_SECTOFF:
14655 case R_PPC64_SECTOFF_LO:
14656 case R_PPC64_SECTOFF_HI:
14657 case R_PPC64_SECTOFF_DS:
14658 case R_PPC64_SECTOFF_LO_DS:
14659 case R_PPC64_SECTOFF_HA:
14660 if (sec != NULL)
14661 addend -= sec->output_section->vma;
14662 break;
14663
14664 case R_PPC64_REL16:
14665 case R_PPC64_REL16_LO:
14666 case R_PPC64_REL16_HI:
14667 case R_PPC64_REL16_HA:
14668 case R_PPC64_REL16DX_HA:
14669 break;
14670
14671 case R_PPC64_REL14:
14672 case R_PPC64_REL14_BRNTAKEN:
14673 case R_PPC64_REL14_BRTAKEN:
14674 case R_PPC64_REL24:
14675 break;
14676
14677 case R_PPC64_TPREL16:
14678 case R_PPC64_TPREL16_LO:
14679 case R_PPC64_TPREL16_HI:
14680 case R_PPC64_TPREL16_HA:
14681 case R_PPC64_TPREL16_DS:
14682 case R_PPC64_TPREL16_LO_DS:
14683 case R_PPC64_TPREL16_HIGH:
14684 case R_PPC64_TPREL16_HIGHA:
14685 case R_PPC64_TPREL16_HIGHER:
14686 case R_PPC64_TPREL16_HIGHERA:
14687 case R_PPC64_TPREL16_HIGHEST:
14688 case R_PPC64_TPREL16_HIGHESTA:
14689 if (h != NULL
14690 && h->elf.root.type == bfd_link_hash_undefweak
14691 && h->elf.dynindx == -1)
14692 {
14693 /* Make this relocation against an undefined weak symbol
14694 resolve to zero. This is really just a tweak, since
14695 code using weak externs ought to check that they are
14696 defined before using them. */
14697 bfd_byte *p = contents + rel->r_offset - d_offset;
14698
14699 insn = bfd_get_32 (input_bfd, p);
14700 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14701 if (insn != 0)
14702 bfd_put_32 (input_bfd, insn, p);
14703 break;
14704 }
14705 if (htab->elf.tls_sec != NULL)
14706 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14707 if (bfd_link_pic (info))
14708 /* The TPREL16 relocs shouldn't really be used in shared
14709 libs as they will result in DT_TEXTREL being set, but
14710 support them anyway. */
14711 goto dodyn;
14712 break;
14713
14714 case R_PPC64_DTPREL16:
14715 case R_PPC64_DTPREL16_LO:
14716 case R_PPC64_DTPREL16_HI:
14717 case R_PPC64_DTPREL16_HA:
14718 case R_PPC64_DTPREL16_DS:
14719 case R_PPC64_DTPREL16_LO_DS:
14720 case R_PPC64_DTPREL16_HIGH:
14721 case R_PPC64_DTPREL16_HIGHA:
14722 case R_PPC64_DTPREL16_HIGHER:
14723 case R_PPC64_DTPREL16_HIGHERA:
14724 case R_PPC64_DTPREL16_HIGHEST:
14725 case R_PPC64_DTPREL16_HIGHESTA:
14726 if (htab->elf.tls_sec != NULL)
14727 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14728 break;
14729
14730 case R_PPC64_ADDR64_LOCAL:
14731 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14732 ? h->elf.other
14733 : sym->st_other);
14734 break;
14735
14736 case R_PPC64_DTPMOD64:
14737 relocation = 1;
14738 addend = 0;
14739 goto dodyn;
14740
14741 case R_PPC64_TPREL64:
14742 if (htab->elf.tls_sec != NULL)
14743 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14744 goto dodyn;
14745
14746 case R_PPC64_DTPREL64:
14747 if (htab->elf.tls_sec != NULL)
14748 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14749 /* Fall through. */
14750
14751 /* Relocations that may need to be propagated if this is a
14752 dynamic object. */
14753 case R_PPC64_REL30:
14754 case R_PPC64_REL32:
14755 case R_PPC64_REL64:
14756 case R_PPC64_ADDR14:
14757 case R_PPC64_ADDR14_BRNTAKEN:
14758 case R_PPC64_ADDR14_BRTAKEN:
14759 case R_PPC64_ADDR16:
14760 case R_PPC64_ADDR16_DS:
14761 case R_PPC64_ADDR16_HA:
14762 case R_PPC64_ADDR16_HI:
14763 case R_PPC64_ADDR16_HIGH:
14764 case R_PPC64_ADDR16_HIGHA:
14765 case R_PPC64_ADDR16_HIGHER:
14766 case R_PPC64_ADDR16_HIGHERA:
14767 case R_PPC64_ADDR16_HIGHEST:
14768 case R_PPC64_ADDR16_HIGHESTA:
14769 case R_PPC64_ADDR16_LO:
14770 case R_PPC64_ADDR16_LO_DS:
14771 case R_PPC64_ADDR24:
14772 case R_PPC64_ADDR32:
14773 case R_PPC64_ADDR64:
14774 case R_PPC64_UADDR16:
14775 case R_PPC64_UADDR32:
14776 case R_PPC64_UADDR64:
14777 dodyn:
14778 if ((input_section->flags & SEC_ALLOC) == 0)
14779 break;
14780
14781 if (NO_OPD_RELOCS && is_opd)
14782 break;
14783
14784 if (bfd_link_pic (info)
14785 ? ((h != NULL && pc_dynrelocs (h))
14786 || must_be_dyn_reloc (info, r_type))
14787 : (h != NULL
14788 ? h->dyn_relocs != NULL
14789 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14790 {
14791 bfd_boolean skip, relocate;
14792 asection *sreloc;
14793 bfd_vma out_off;
14794
14795 /* When generating a dynamic object, these relocations
14796 are copied into the output file to be resolved at run
14797 time. */
14798
14799 skip = FALSE;
14800 relocate = FALSE;
14801
14802 out_off = _bfd_elf_section_offset (output_bfd, info,
14803 input_section, rel->r_offset);
14804 if (out_off == (bfd_vma) -1)
14805 skip = TRUE;
14806 else if (out_off == (bfd_vma) -2)
14807 skip = TRUE, relocate = TRUE;
14808 out_off += (input_section->output_section->vma
14809 + input_section->output_offset);
14810 outrel.r_offset = out_off;
14811 outrel.r_addend = rel->r_addend;
14812
14813 /* Optimize unaligned reloc use. */
14814 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14815 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14816 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14817 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14818 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14819 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14820 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14821 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14822 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14823
14824 if (skip)
14825 memset (&outrel, 0, sizeof outrel);
14826 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14827 && !is_opd
14828 && r_type != R_PPC64_TOC)
14829 {
14830 BFD_ASSERT (h->elf.dynindx != -1);
14831 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14832 }
14833 else
14834 {
14835 /* This symbol is local, or marked to become local,
14836 or this is an opd section reloc which must point
14837 at a local function. */
14838 outrel.r_addend += relocation;
14839 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14840 {
14841 if (is_opd && h != NULL)
14842 {
14843 /* Lie about opd entries. This case occurs
14844 when building shared libraries and we
14845 reference a function in another shared
14846 lib. The same thing happens for a weak
14847 definition in an application that's
14848 overridden by a strong definition in a
14849 shared lib. (I believe this is a generic
14850 bug in binutils handling of weak syms.)
14851 In these cases we won't use the opd
14852 entry in this lib. */
14853 unresolved_reloc = FALSE;
14854 }
14855 if (!is_opd
14856 && r_type == R_PPC64_ADDR64
14857 && (h != NULL
14858 ? h->elf.type == STT_GNU_IFUNC
14859 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14860 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14861 else
14862 {
14863 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14864
14865 /* We need to relocate .opd contents for ld.so.
14866 Prelink also wants simple and consistent rules
14867 for relocs. This make all RELATIVE relocs have
14868 *r_offset equal to r_addend. */
14869 relocate = TRUE;
14870 }
14871 }
14872 else
14873 {
14874 long indx = 0;
14875
14876 if (h != NULL
14877 ? h->elf.type == STT_GNU_IFUNC
14878 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14879 {
14880 info->callbacks->einfo
14881 /* xgettext:c-format */
14882 (_("%P: %H: %s for indirect "
14883 "function `%T' unsupported\n"),
14884 input_bfd, input_section, rel->r_offset,
14885 ppc64_elf_howto_table[r_type]->name,
14886 sym_name);
14887 ret = FALSE;
14888 }
14889 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14890 ;
14891 else if (sec == NULL || sec->owner == NULL)
14892 {
14893 bfd_set_error (bfd_error_bad_value);
14894 return FALSE;
14895 }
14896 else
14897 {
14898 asection *osec;
14899
14900 osec = sec->output_section;
14901 indx = elf_section_data (osec)->dynindx;
14902
14903 if (indx == 0)
14904 {
14905 if ((osec->flags & SEC_READONLY) == 0
14906 && htab->elf.data_index_section != NULL)
14907 osec = htab->elf.data_index_section;
14908 else
14909 osec = htab->elf.text_index_section;
14910 indx = elf_section_data (osec)->dynindx;
14911 }
14912 BFD_ASSERT (indx != 0);
14913
14914 /* We are turning this relocation into one
14915 against a section symbol, so subtract out
14916 the output section's address but not the
14917 offset of the input section in the output
14918 section. */
14919 outrel.r_addend -= osec->vma;
14920 }
14921
14922 outrel.r_info = ELF64_R_INFO (indx, r_type);
14923 }
14924 }
14925
14926 sreloc = elf_section_data (input_section)->sreloc;
14927 if (h != NULL
14928 ? h->elf.type == STT_GNU_IFUNC
14929 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14930 sreloc = htab->elf.irelplt;
14931 if (sreloc == NULL)
14932 abort ();
14933
14934 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14935 >= sreloc->size)
14936 abort ();
14937 loc = sreloc->contents;
14938 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14939 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14940
14941 /* If this reloc is against an external symbol, it will
14942 be computed at runtime, so there's no need to do
14943 anything now. However, for the sake of prelink ensure
14944 that the section contents are a known value. */
14945 if (! relocate)
14946 {
14947 unresolved_reloc = FALSE;
14948 /* The value chosen here is quite arbitrary as ld.so
14949 ignores section contents except for the special
14950 case of .opd where the contents might be accessed
14951 before relocation. Choose zero, as that won't
14952 cause reloc overflow. */
14953 relocation = 0;
14954 addend = 0;
14955 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14956 to improve backward compatibility with older
14957 versions of ld. */
14958 if (r_type == R_PPC64_ADDR64)
14959 addend = outrel.r_addend;
14960 /* Adjust pc_relative relocs to have zero in *r_offset. */
14961 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14962 addend = (input_section->output_section->vma
14963 + input_section->output_offset
14964 + rel->r_offset);
14965 }
14966 }
14967 break;
14968
14969 case R_PPC64_COPY:
14970 case R_PPC64_GLOB_DAT:
14971 case R_PPC64_JMP_SLOT:
14972 case R_PPC64_JMP_IREL:
14973 case R_PPC64_RELATIVE:
14974 /* We shouldn't ever see these dynamic relocs in relocatable
14975 files. */
14976 /* Fall through. */
14977
14978 case R_PPC64_PLTGOT16:
14979 case R_PPC64_PLTGOT16_DS:
14980 case R_PPC64_PLTGOT16_HA:
14981 case R_PPC64_PLTGOT16_HI:
14982 case R_PPC64_PLTGOT16_LO:
14983 case R_PPC64_PLTGOT16_LO_DS:
14984 case R_PPC64_PLTREL32:
14985 case R_PPC64_PLTREL64:
14986 /* These ones haven't been implemented yet. */
14987
14988 info->callbacks->einfo
14989 /* xgettext:c-format */
14990 (_("%P: %B: %s is not supported for `%T'\n"),
14991 input_bfd,
14992 ppc64_elf_howto_table[r_type]->name, sym_name);
14993
14994 bfd_set_error (bfd_error_invalid_operation);
14995 ret = FALSE;
14996 goto copy_reloc;
14997 }
14998
14999 /* Multi-instruction sequences that access the TOC can be
15000 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15001 to nop; addi rb,r2,x; */
15002 switch (r_type)
15003 {
15004 default:
15005 break;
15006
15007 case R_PPC64_GOT_TLSLD16_HI:
15008 case R_PPC64_GOT_TLSGD16_HI:
15009 case R_PPC64_GOT_TPREL16_HI:
15010 case R_PPC64_GOT_DTPREL16_HI:
15011 case R_PPC64_GOT16_HI:
15012 case R_PPC64_TOC16_HI:
15013 /* These relocs would only be useful if building up an
15014 offset to later add to r2, perhaps in an indexed
15015 addressing mode instruction. Don't try to optimize.
15016 Unfortunately, the possibility of someone building up an
15017 offset like this or even with the HA relocs, means that
15018 we need to check the high insn when optimizing the low
15019 insn. */
15020 break;
15021
15022 case R_PPC64_GOT_TLSLD16_HA:
15023 case R_PPC64_GOT_TLSGD16_HA:
15024 case R_PPC64_GOT_TPREL16_HA:
15025 case R_PPC64_GOT_DTPREL16_HA:
15026 case R_PPC64_GOT16_HA:
15027 case R_PPC64_TOC16_HA:
15028 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15029 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15030 {
15031 bfd_byte *p = contents + (rel->r_offset & ~3);
15032 bfd_put_32 (input_bfd, NOP, p);
15033 }
15034 break;
15035
15036 case R_PPC64_GOT_TLSLD16_LO:
15037 case R_PPC64_GOT_TLSGD16_LO:
15038 case R_PPC64_GOT_TPREL16_LO_DS:
15039 case R_PPC64_GOT_DTPREL16_LO_DS:
15040 case R_PPC64_GOT16_LO:
15041 case R_PPC64_GOT16_LO_DS:
15042 case R_PPC64_TOC16_LO:
15043 case R_PPC64_TOC16_LO_DS:
15044 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15045 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15046 {
15047 bfd_byte *p = contents + (rel->r_offset & ~3);
15048 insn = bfd_get_32 (input_bfd, p);
15049 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15050 {
15051 /* Transform addic to addi when we change reg. */
15052 insn &= ~((0x3f << 26) | (0x1f << 16));
15053 insn |= (14u << 26) | (2 << 16);
15054 }
15055 else
15056 {
15057 insn &= ~(0x1f << 16);
15058 insn |= 2 << 16;
15059 }
15060 bfd_put_32 (input_bfd, insn, p);
15061 }
15062 break;
15063 }
15064
15065 /* Do any further special processing. */
15066 howto = ppc64_elf_howto_table[(int) r_type];
15067 switch (r_type)
15068 {
15069 default:
15070 break;
15071
15072 case R_PPC64_REL16_HA:
15073 case R_PPC64_REL16DX_HA:
15074 case R_PPC64_ADDR16_HA:
15075 case R_PPC64_ADDR16_HIGHA:
15076 case R_PPC64_ADDR16_HIGHERA:
15077 case R_PPC64_ADDR16_HIGHESTA:
15078 case R_PPC64_TOC16_HA:
15079 case R_PPC64_SECTOFF_HA:
15080 case R_PPC64_TPREL16_HA:
15081 case R_PPC64_TPREL16_HIGHA:
15082 case R_PPC64_TPREL16_HIGHERA:
15083 case R_PPC64_TPREL16_HIGHESTA:
15084 case R_PPC64_DTPREL16_HA:
15085 case R_PPC64_DTPREL16_HIGHA:
15086 case R_PPC64_DTPREL16_HIGHERA:
15087 case R_PPC64_DTPREL16_HIGHESTA:
15088 /* It's just possible that this symbol is a weak symbol
15089 that's not actually defined anywhere. In that case,
15090 'sec' would be NULL, and we should leave the symbol
15091 alone (it will be set to zero elsewhere in the link). */
15092 if (sec == NULL)
15093 break;
15094 /* Fall through. */
15095
15096 case R_PPC64_GOT16_HA:
15097 case R_PPC64_PLTGOT16_HA:
15098 case R_PPC64_PLT16_HA:
15099 case R_PPC64_GOT_TLSGD16_HA:
15100 case R_PPC64_GOT_TLSLD16_HA:
15101 case R_PPC64_GOT_TPREL16_HA:
15102 case R_PPC64_GOT_DTPREL16_HA:
15103 /* Add 0x10000 if sign bit in 0:15 is set.
15104 Bits 0:15 are not used. */
15105 addend += 0x8000;
15106 break;
15107
15108 case R_PPC64_ADDR16_DS:
15109 case R_PPC64_ADDR16_LO_DS:
15110 case R_PPC64_GOT16_DS:
15111 case R_PPC64_GOT16_LO_DS:
15112 case R_PPC64_PLT16_LO_DS:
15113 case R_PPC64_SECTOFF_DS:
15114 case R_PPC64_SECTOFF_LO_DS:
15115 case R_PPC64_TOC16_DS:
15116 case R_PPC64_TOC16_LO_DS:
15117 case R_PPC64_PLTGOT16_DS:
15118 case R_PPC64_PLTGOT16_LO_DS:
15119 case R_PPC64_GOT_TPREL16_DS:
15120 case R_PPC64_GOT_TPREL16_LO_DS:
15121 case R_PPC64_GOT_DTPREL16_DS:
15122 case R_PPC64_GOT_DTPREL16_LO_DS:
15123 case R_PPC64_TPREL16_DS:
15124 case R_PPC64_TPREL16_LO_DS:
15125 case R_PPC64_DTPREL16_DS:
15126 case R_PPC64_DTPREL16_LO_DS:
15127 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15128 mask = 3;
15129 /* If this reloc is against an lq, lxv, or stxv insn, then
15130 the value must be a multiple of 16. This is somewhat of
15131 a hack, but the "correct" way to do this by defining _DQ
15132 forms of all the _DS relocs bloats all reloc switches in
15133 this file. It doesn't make much sense to use these
15134 relocs in data, so testing the insn should be safe. */
15135 if ((insn & (0x3f << 26)) == (56u << 26)
15136 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15137 mask = 15;
15138 relocation += addend;
15139 addend = insn & (mask ^ 3);
15140 if ((relocation & mask) != 0)
15141 {
15142 relocation ^= relocation & mask;
15143 info->callbacks->einfo
15144 /* xgettext:c-format */
15145 (_("%P: %H: error: %s not a multiple of %u\n"),
15146 input_bfd, input_section, rel->r_offset,
15147 howto->name,
15148 mask + 1);
15149 bfd_set_error (bfd_error_bad_value);
15150 ret = FALSE;
15151 goto copy_reloc;
15152 }
15153 break;
15154 }
15155
15156 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15157 because such sections are not SEC_ALLOC and thus ld.so will
15158 not process them. */
15159 if (unresolved_reloc
15160 && !((input_section->flags & SEC_DEBUGGING) != 0
15161 && h->elf.def_dynamic)
15162 && _bfd_elf_section_offset (output_bfd, info, input_section,
15163 rel->r_offset) != (bfd_vma) -1)
15164 {
15165 info->callbacks->einfo
15166 /* xgettext:c-format */
15167 (_("%P: %H: unresolvable %s against `%T'\n"),
15168 input_bfd, input_section, rel->r_offset,
15169 howto->name,
15170 h->elf.root.root.string);
15171 ret = FALSE;
15172 }
15173
15174 /* 16-bit fields in insns mostly have signed values, but a
15175 few insns have 16-bit unsigned values. Really, we should
15176 have different reloc types. */
15177 if (howto->complain_on_overflow != complain_overflow_dont
15178 && howto->dst_mask == 0xffff
15179 && (input_section->flags & SEC_CODE) != 0)
15180 {
15181 enum complain_overflow complain = complain_overflow_signed;
15182
15183 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15184 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15185 complain = complain_overflow_bitfield;
15186 else if (howto->rightshift == 0
15187 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15188 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15189 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15190 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15191 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15192 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15193 complain = complain_overflow_unsigned;
15194 if (howto->complain_on_overflow != complain)
15195 {
15196 alt_howto = *howto;
15197 alt_howto.complain_on_overflow = complain;
15198 howto = &alt_howto;
15199 }
15200 }
15201
15202 if (r_type == R_PPC64_REL16DX_HA)
15203 {
15204 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15205 if (rel->r_offset + 4 > input_section->size)
15206 r = bfd_reloc_outofrange;
15207 else
15208 {
15209 relocation += addend;
15210 relocation -= (rel->r_offset
15211 + input_section->output_offset
15212 + input_section->output_section->vma);
15213 relocation = (bfd_signed_vma) relocation >> 16;
15214 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15215 insn &= ~0x1fffc1;
15216 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15217 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15218 r = bfd_reloc_ok;
15219 if (relocation + 0x8000 > 0xffff)
15220 r = bfd_reloc_overflow;
15221 }
15222 }
15223 else
15224 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15225 rel->r_offset, relocation, addend);
15226
15227 if (r != bfd_reloc_ok)
15228 {
15229 char *more_info = NULL;
15230 const char *reloc_name = howto->name;
15231
15232 if (reloc_dest != DEST_NORMAL)
15233 {
15234 more_info = bfd_malloc (strlen (reloc_name) + 8);
15235 if (more_info != NULL)
15236 {
15237 strcpy (more_info, reloc_name);
15238 strcat (more_info, (reloc_dest == DEST_OPD
15239 ? " (OPD)" : " (stub)"));
15240 reloc_name = more_info;
15241 }
15242 }
15243
15244 if (r == bfd_reloc_overflow)
15245 {
15246 /* On code like "if (foo) foo();" don't report overflow
15247 on a branch to zero when foo is undefined. */
15248 if (!warned
15249 && (reloc_dest == DEST_STUB
15250 || !(h != NULL
15251 && (h->elf.root.type == bfd_link_hash_undefweak
15252 || h->elf.root.type == bfd_link_hash_undefined)
15253 && is_branch_reloc (r_type))))
15254 info->callbacks->reloc_overflow (info, &h->elf.root,
15255 sym_name, reloc_name,
15256 orig_rel.r_addend,
15257 input_bfd, input_section,
15258 rel->r_offset);
15259 }
15260 else
15261 {
15262 info->callbacks->einfo
15263 /* xgettext:c-format */
15264 (_("%P: %H: %s against `%T': error %d\n"),
15265 input_bfd, input_section, rel->r_offset,
15266 reloc_name, sym_name, (int) r);
15267 ret = FALSE;
15268 }
15269 if (more_info != NULL)
15270 free (more_info);
15271 }
15272 copy_reloc:
15273 if (wrel != rel)
15274 *wrel = *rel;
15275 }
15276
15277 if (wrel != rel)
15278 {
15279 Elf_Internal_Shdr *rel_hdr;
15280 size_t deleted = rel - wrel;
15281
15282 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15283 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15284 if (rel_hdr->sh_size == 0)
15285 {
15286 /* It is too late to remove an empty reloc section. Leave
15287 one NONE reloc.
15288 ??? What is wrong with an empty section??? */
15289 rel_hdr->sh_size = rel_hdr->sh_entsize;
15290 deleted -= 1;
15291 }
15292 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15293 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15294 input_section->reloc_count -= deleted;
15295 }
15296
15297 /* If we're emitting relocations, then shortly after this function
15298 returns, reloc offsets and addends for this section will be
15299 adjusted. Worse, reloc symbol indices will be for the output
15300 file rather than the input. Save a copy of the relocs for
15301 opd_entry_value. */
15302 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15303 {
15304 bfd_size_type amt;
15305 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15306 rel = bfd_alloc (input_bfd, amt);
15307 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15308 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15309 if (rel == NULL)
15310 return FALSE;
15311 memcpy (rel, relocs, amt);
15312 }
15313 return ret;
15314 }
15315
15316 /* Adjust the value of any local symbols in opd sections. */
15317
15318 static int
15319 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15320 const char *name ATTRIBUTE_UNUSED,
15321 Elf_Internal_Sym *elfsym,
15322 asection *input_sec,
15323 struct elf_link_hash_entry *h)
15324 {
15325 struct _opd_sec_data *opd;
15326 long adjust;
15327 bfd_vma value;
15328
15329 if (h != NULL)
15330 return 1;
15331
15332 opd = get_opd_info (input_sec);
15333 if (opd == NULL || opd->adjust == NULL)
15334 return 1;
15335
15336 value = elfsym->st_value - input_sec->output_offset;
15337 if (!bfd_link_relocatable (info))
15338 value -= input_sec->output_section->vma;
15339
15340 adjust = opd->adjust[OPD_NDX (value)];
15341 if (adjust == -1)
15342 return 2;
15343
15344 elfsym->st_value += adjust;
15345 return 1;
15346 }
15347
15348 /* Finish up dynamic symbol handling. We set the contents of various
15349 dynamic sections here. */
15350
15351 static bfd_boolean
15352 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15353 struct bfd_link_info *info,
15354 struct elf_link_hash_entry *h,
15355 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
15356 {
15357 struct ppc_link_hash_table *htab;
15358 struct plt_entry *ent;
15359 Elf_Internal_Rela rela;
15360 bfd_byte *loc;
15361
15362 htab = ppc_hash_table (info);
15363 if (htab == NULL)
15364 return FALSE;
15365
15366 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15367 if (ent->plt.offset != (bfd_vma) -1)
15368 {
15369 /* This symbol has an entry in the procedure linkage
15370 table. Set it up. */
15371 if (!htab->elf.dynamic_sections_created
15372 || h->dynindx == -1)
15373 {
15374 BFD_ASSERT (h->type == STT_GNU_IFUNC
15375 && h->def_regular
15376 && (h->root.type == bfd_link_hash_defined
15377 || h->root.type == bfd_link_hash_defweak));
15378 rela.r_offset = (htab->elf.iplt->output_section->vma
15379 + htab->elf.iplt->output_offset
15380 + ent->plt.offset);
15381 if (htab->opd_abi)
15382 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15383 else
15384 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15385 rela.r_addend = (h->root.u.def.value
15386 + h->root.u.def.section->output_offset
15387 + h->root.u.def.section->output_section->vma
15388 + ent->addend);
15389 loc = (htab->elf.irelplt->contents
15390 + (htab->elf.irelplt->reloc_count++
15391 * sizeof (Elf64_External_Rela)));
15392 }
15393 else
15394 {
15395 rela.r_offset = (htab->elf.splt->output_section->vma
15396 + htab->elf.splt->output_offset
15397 + ent->plt.offset);
15398 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15399 rela.r_addend = ent->addend;
15400 loc = (htab->elf.srelplt->contents
15401 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15402 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15403 }
15404 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15405
15406 if (!htab->opd_abi)
15407 {
15408 if (!h->def_regular)
15409 {
15410 /* Mark the symbol as undefined, rather than as
15411 defined in glink. Leave the value if there were
15412 any relocations where pointer equality matters
15413 (this is a clue for the dynamic linker, to make
15414 function pointer comparisons work between an
15415 application and shared library), otherwise set it
15416 to zero. */
15417 sym->st_shndx = SHN_UNDEF;
15418 if (!h->pointer_equality_needed)
15419 sym->st_value = 0;
15420 else if (!h->ref_regular_nonweak)
15421 {
15422 /* This breaks function pointer comparisons, but
15423 that is better than breaking tests for a NULL
15424 function pointer. */
15425 sym->st_value = 0;
15426 }
15427 }
15428 }
15429 }
15430
15431 if (h->needs_copy)
15432 {
15433 /* This symbol needs a copy reloc. Set it up. */
15434 asection *srel;
15435
15436 if (h->dynindx == -1
15437 || (h->root.type != bfd_link_hash_defined
15438 && h->root.type != bfd_link_hash_defweak)
15439 || htab->elf.srelbss == NULL
15440 || htab->elf.sreldynrelro == NULL)
15441 abort ();
15442
15443 rela.r_offset = (h->root.u.def.value
15444 + h->root.u.def.section->output_section->vma
15445 + h->root.u.def.section->output_offset);
15446 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15447 rela.r_addend = 0;
15448 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
15449 srel = htab->elf.sreldynrelro;
15450 else
15451 srel = htab->elf.srelbss;
15452 loc = srel->contents;
15453 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15454 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15455 }
15456
15457 return TRUE;
15458 }
15459
15460 /* Used to decide how to sort relocs in an optimal manner for the
15461 dynamic linker, before writing them out. */
15462
15463 static enum elf_reloc_type_class
15464 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15465 const asection *rel_sec,
15466 const Elf_Internal_Rela *rela)
15467 {
15468 enum elf_ppc64_reloc_type r_type;
15469 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15470
15471 if (rel_sec == htab->elf.irelplt)
15472 return reloc_class_ifunc;
15473
15474 r_type = ELF64_R_TYPE (rela->r_info);
15475 switch (r_type)
15476 {
15477 case R_PPC64_RELATIVE:
15478 return reloc_class_relative;
15479 case R_PPC64_JMP_SLOT:
15480 return reloc_class_plt;
15481 case R_PPC64_COPY:
15482 return reloc_class_copy;
15483 default:
15484 return reloc_class_normal;
15485 }
15486 }
15487
15488 /* Finish up the dynamic sections. */
15489
15490 static bfd_boolean
15491 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15492 struct bfd_link_info *info)
15493 {
15494 struct ppc_link_hash_table *htab;
15495 bfd *dynobj;
15496 asection *sdyn;
15497
15498 htab = ppc_hash_table (info);
15499 if (htab == NULL)
15500 return FALSE;
15501
15502 dynobj = htab->elf.dynobj;
15503 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15504
15505 if (htab->elf.dynamic_sections_created)
15506 {
15507 Elf64_External_Dyn *dyncon, *dynconend;
15508
15509 if (sdyn == NULL || htab->elf.sgot == NULL)
15510 abort ();
15511
15512 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15513 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15514 for (; dyncon < dynconend; dyncon++)
15515 {
15516 Elf_Internal_Dyn dyn;
15517 asection *s;
15518
15519 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15520
15521 switch (dyn.d_tag)
15522 {
15523 default:
15524 continue;
15525
15526 case DT_PPC64_GLINK:
15527 s = htab->glink;
15528 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15529 /* We stupidly defined DT_PPC64_GLINK to be the start
15530 of glink rather than the first entry point, which is
15531 what ld.so needs, and now have a bigger stub to
15532 support automatic multiple TOCs. */
15533 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15534 break;
15535
15536 case DT_PPC64_OPD:
15537 s = bfd_get_section_by_name (output_bfd, ".opd");
15538 if (s == NULL)
15539 continue;
15540 dyn.d_un.d_ptr = s->vma;
15541 break;
15542
15543 case DT_PPC64_OPT:
15544 if (htab->do_multi_toc && htab->multi_toc_needed)
15545 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15546 break;
15547
15548 case DT_PPC64_OPDSZ:
15549 s = bfd_get_section_by_name (output_bfd, ".opd");
15550 if (s == NULL)
15551 continue;
15552 dyn.d_un.d_val = s->size;
15553 break;
15554
15555 case DT_PLTGOT:
15556 s = htab->elf.splt;
15557 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15558 break;
15559
15560 case DT_JMPREL:
15561 s = htab->elf.srelplt;
15562 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15563 break;
15564
15565 case DT_PLTRELSZ:
15566 dyn.d_un.d_val = htab->elf.srelplt->size;
15567 break;
15568 }
15569
15570 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15571 }
15572 }
15573
15574 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0)
15575 {
15576 /* Fill in the first entry in the global offset table.
15577 We use it to hold the link-time TOCbase. */
15578 bfd_put_64 (output_bfd,
15579 elf_gp (output_bfd) + TOC_BASE_OFF,
15580 htab->elf.sgot->contents);
15581
15582 /* Set .got entry size. */
15583 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15584 }
15585
15586 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
15587 {
15588 /* Set .plt entry size. */
15589 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15590 = PLT_ENTRY_SIZE (htab);
15591 }
15592
15593 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15594 brlt ourselves if emitrelocations. */
15595 if (htab->brlt != NULL
15596 && htab->brlt->reloc_count != 0
15597 && !_bfd_elf_link_output_relocs (output_bfd,
15598 htab->brlt,
15599 elf_section_data (htab->brlt)->rela.hdr,
15600 elf_section_data (htab->brlt)->relocs,
15601 NULL))
15602 return FALSE;
15603
15604 if (htab->glink != NULL
15605 && htab->glink->reloc_count != 0
15606 && !_bfd_elf_link_output_relocs (output_bfd,
15607 htab->glink,
15608 elf_section_data (htab->glink)->rela.hdr,
15609 elf_section_data (htab->glink)->relocs,
15610 NULL))
15611 return FALSE;
15612
15613 if (htab->glink_eh_frame != NULL
15614 && htab->glink_eh_frame->size != 0)
15615 {
15616 bfd_vma val;
15617 bfd_byte *p;
15618 asection *stub_sec;
15619
15620 p = htab->glink_eh_frame->contents + sizeof (glink_eh_frame_cie);
15621 for (stub_sec = htab->params->stub_bfd->sections;
15622 stub_sec != NULL;
15623 stub_sec = stub_sec->next)
15624 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15625 {
15626 /* FDE length. */
15627 p += 4;
15628 /* CIE pointer. */
15629 p += 4;
15630 /* Offset to stub section. */
15631 val = (stub_sec->output_section->vma
15632 + stub_sec->output_offset);
15633 val -= (htab->glink_eh_frame->output_section->vma
15634 + htab->glink_eh_frame->output_offset
15635 + (p - htab->glink_eh_frame->contents));
15636 if (val + 0x80000000 > 0xffffffff)
15637 {
15638 info->callbacks->einfo
15639 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15640 stub_sec->name);
15641 return FALSE;
15642 }
15643 bfd_put_32 (dynobj, val, p);
15644 p += 4;
15645 /* stub section size. */
15646 p += 4;
15647 /* Augmentation. */
15648 p += 1;
15649 /* Pad. */
15650 p += 7;
15651 }
15652 if (htab->glink != NULL && htab->glink->size != 0)
15653 {
15654 /* FDE length. */
15655 p += 4;
15656 /* CIE pointer. */
15657 p += 4;
15658 /* Offset to .glink. */
15659 val = (htab->glink->output_section->vma
15660 + htab->glink->output_offset
15661 + 8);
15662 val -= (htab->glink_eh_frame->output_section->vma
15663 + htab->glink_eh_frame->output_offset
15664 + (p - htab->glink_eh_frame->contents));
15665 if (val + 0x80000000 > 0xffffffff)
15666 {
15667 info->callbacks->einfo
15668 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15669 htab->glink->name);
15670 return FALSE;
15671 }
15672 bfd_put_32 (dynobj, val, p);
15673 p += 4;
15674 /* .glink size. */
15675 p += 4;
15676 /* Augmentation. */
15677 p += 1;
15678 /* Ops. */
15679 p += 7;
15680 }
15681
15682 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15683 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15684 htab->glink_eh_frame,
15685 htab->glink_eh_frame->contents))
15686 return FALSE;
15687 }
15688
15689 /* We need to handle writing out multiple GOT sections ourselves,
15690 since we didn't add them to DYNOBJ. We know dynobj is the first
15691 bfd. */
15692 while ((dynobj = dynobj->link.next) != NULL)
15693 {
15694 asection *s;
15695
15696 if (!is_ppc64_elf (dynobj))
15697 continue;
15698
15699 s = ppc64_elf_tdata (dynobj)->got;
15700 if (s != NULL
15701 && s->size != 0
15702 && s->output_section != bfd_abs_section_ptr
15703 && !bfd_set_section_contents (output_bfd, s->output_section,
15704 s->contents, s->output_offset,
15705 s->size))
15706 return FALSE;
15707 s = ppc64_elf_tdata (dynobj)->relgot;
15708 if (s != NULL
15709 && s->size != 0
15710 && s->output_section != bfd_abs_section_ptr
15711 && !bfd_set_section_contents (output_bfd, s->output_section,
15712 s->contents, s->output_offset,
15713 s->size))
15714 return FALSE;
15715 }
15716
15717 return TRUE;
15718 }
15719
15720 #include "elf64-target.h"
15721
15722 /* FreeBSD support */
15723
15724 #undef TARGET_LITTLE_SYM
15725 #undef TARGET_LITTLE_NAME
15726
15727 #undef TARGET_BIG_SYM
15728 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15729 #undef TARGET_BIG_NAME
15730 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15731
15732 #undef ELF_OSABI
15733 #define ELF_OSABI ELFOSABI_FREEBSD
15734
15735 #undef elf64_bed
15736 #define elf64_bed elf64_powerpc_fbsd_bed
15737
15738 #include "elf64-target.h"
15739