alpha-bsd-nat: Use ptid from regcache instead of inferior_ptid
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2017 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 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2049 HOWTO (R_PPC64_16DX_HA, /* type */
2050 16, /* rightshift */
2051 2, /* size (0 = byte, 1 = short, 2 = long) */
2052 16, /* bitsize */
2053 FALSE, /* pc_relative */
2054 0, /* bitpos */
2055 complain_overflow_signed, /* complain_on_overflow */
2056 ppc64_elf_ha_reloc, /* special_function */
2057 "R_PPC64_16DX_HA", /* name */
2058 FALSE, /* partial_inplace */
2059 0, /* src_mask */
2060 0x1fffc1, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2062
2063 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2064 HOWTO (R_PPC64_ADDR16_HIGH, /* 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 bfd_elf_generic_reloc, /* special_function */
2072 "R_PPC64_ADDR16_HIGH", /* name */
2073 FALSE, /* partial_inplace */
2074 0, /* src_mask */
2075 0xffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2077
2078 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2079 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
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_ha_reloc, /* special_function */
2087 "R_PPC64_ADDR16_HIGHA", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
2092
2093 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2094 HOWTO (R_PPC64_DTPREL16_HIGH,
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_HIGH", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0xffff, /* dst_mask */
2106 FALSE), /* pcrel_offset */
2107
2108 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2109 HOWTO (R_PPC64_DTPREL16_HIGHA,
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_DTPREL16_HIGHA", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0xffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
2123 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2124 HOWTO (R_PPC64_TPREL16_HIGH,
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_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
2138 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2139 HOWTO (R_PPC64_TPREL16_HIGHA,
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2142 16, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_unhandled_reloc, /* special_function */
2147 "R_PPC64_TPREL16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
2153 /* Marker reloc on ELFv2 large-model function entry. */
2154 HOWTO (R_PPC64_ENTRY,
2155 0, /* rightshift */
2156 2, /* size (0 = byte, 1 = short, 2 = long) */
2157 32, /* 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_ENTRY", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 0, /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
2168 /* Like ADDR64, but use local entry point of function. */
2169 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2170 0, /* rightshift */
2171 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2172 64, /* bitsize */
2173 FALSE, /* pc_relative */
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 bfd_elf_generic_reloc, /* special_function */
2177 "R_PPC64_ADDR64_LOCAL", /* name */
2178 FALSE, /* partial_inplace */
2179 0, /* src_mask */
2180 ONES (64), /* dst_mask */
2181 FALSE), /* pcrel_offset */
2182
2183 /* GNU extension to record C++ vtable hierarchy. */
2184 HOWTO (R_PPC64_GNU_VTINHERIT, /* 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_VTINHERIT", /* name */
2193 FALSE, /* partial_inplace */
2194 0, /* src_mask */
2195 0, /* dst_mask */
2196 FALSE), /* pcrel_offset */
2197
2198 /* GNU extension to record C++ vtable member usage. */
2199 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2200 0, /* rightshift */
2201 0, /* size (0 = byte, 1 = short, 2 = long) */
2202 0, /* bitsize */
2203 FALSE, /* pc_relative */
2204 0, /* bitpos */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 NULL, /* special_function */
2207 "R_PPC64_GNU_VTENTRY", /* name */
2208 FALSE, /* partial_inplace */
2209 0, /* src_mask */
2210 0, /* dst_mask */
2211 FALSE), /* pcrel_offset */
2212 };
2213
2214 \f
2215 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2216 be done. */
2217
2218 static void
2219 ppc_howto_init (void)
2220 {
2221 unsigned int i, type;
2222
2223 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2224 {
2225 type = ppc64_elf_howto_raw[i].type;
2226 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2227 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2228 }
2229 }
2230
2231 static reloc_howto_type *
2232 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2233 bfd_reloc_code_real_type code)
2234 {
2235 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2236
2237 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2238 /* Initialize howto table if needed. */
2239 ppc_howto_init ();
2240
2241 switch (code)
2242 {
2243 default:
2244 return NULL;
2245
2246 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2247 break;
2248 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2249 break;
2250 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2251 break;
2252 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2253 break;
2254 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2255 break;
2256 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2257 break;
2258 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2259 break;
2260 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2261 break;
2262 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2263 break;
2264 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2265 break;
2266 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2267 break;
2268 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2269 break;
2270 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2271 break;
2272 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2273 break;
2274 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2275 break;
2276 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2277 break;
2278 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2279 break;
2280 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2281 break;
2282 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2283 break;
2284 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2285 break;
2286 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2287 break;
2288 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2289 break;
2290 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2291 break;
2292 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2293 break;
2294 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2295 break;
2296 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2297 break;
2298 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2299 break;
2300 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2301 break;
2302 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2303 break;
2304 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2305 break;
2306 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2307 break;
2308 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2309 break;
2310 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2311 break;
2312 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2313 break;
2314 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2315 break;
2316 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2317 break;
2318 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2319 break;
2320 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2321 break;
2322 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2323 break;
2324 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2325 break;
2326 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2327 break;
2328 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2329 break;
2330 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2331 break;
2332 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2333 break;
2334 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2335 break;
2336 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2337 break;
2338 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2339 break;
2340 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2341 break;
2342 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2343 break;
2344 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2345 break;
2346 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2347 break;
2348 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2349 break;
2350 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2351 break;
2352 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2353 break;
2354 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2355 break;
2356 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2357 break;
2358 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2359 break;
2360 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2361 break;
2362 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2363 break;
2364 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2365 break;
2366 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2367 break;
2368 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2369 break;
2370 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2371 break;
2372 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2373 break;
2374 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2375 break;
2376 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2377 break;
2378 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2379 break;
2380 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2381 break;
2382 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2383 break;
2384 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2385 break;
2386 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2387 break;
2388 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2389 break;
2390 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2391 break;
2392 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2393 break;
2394 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2395 break;
2396 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2397 break;
2398 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2399 break;
2400 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2401 break;
2402 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2403 break;
2404 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2411 break;
2412 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2413 break;
2414 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2415 break;
2416 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2417 break;
2418 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2419 break;
2420 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2421 break;
2422 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2423 break;
2424 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2425 break;
2426 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2427 break;
2428 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2429 break;
2430 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2431 break;
2432 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2433 break;
2434 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2435 break;
2436 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2437 break;
2438 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2439 break;
2440 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2441 break;
2442 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2443 break;
2444 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2445 break;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2447 break;
2448 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2449 break;
2450 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2451 break;
2452 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2453 break;
2454 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2455 break;
2456 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2457 break;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2459 break;
2460 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2461 break;
2462 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2463 break;
2464 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2465 break;
2466 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2467 break;
2468 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2469 break;
2470 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2471 break;
2472 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2473 break;
2474 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2475 break;
2476 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2477 break;
2478 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2479 break;
2480 }
2481
2482 return ppc64_elf_howto_table[r];
2483 };
2484
2485 static reloc_howto_type *
2486 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2487 const char *r_name)
2488 {
2489 unsigned int i;
2490
2491 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2492 if (ppc64_elf_howto_raw[i].name != NULL
2493 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2494 return &ppc64_elf_howto_raw[i];
2495
2496 return NULL;
2497 }
2498
2499 /* Set the howto pointer for a PowerPC ELF reloc. */
2500
2501 static void
2502 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2503 Elf_Internal_Rela *dst)
2504 {
2505 unsigned int type;
2506
2507 /* Initialize howto table if needed. */
2508 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2509 ppc_howto_init ();
2510
2511 type = ELF64_R_TYPE (dst->r_info);
2512 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2513 {
2514 /* xgettext:c-format */
2515 _bfd_error_handler (_("%B: invalid relocation type %d"),
2516 abfd, (int) type);
2517 type = R_PPC64_NONE;
2518 }
2519 cache_ptr->howto = ppc64_elf_howto_table[type];
2520 }
2521
2522 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2523
2524 static bfd_reloc_status_type
2525 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
2528 {
2529 enum elf_ppc64_reloc_type r_type;
2530 long insn;
2531 bfd_size_type octets;
2532 bfd_vma value;
2533
2534 /* If this is a relocatable link (output_bfd test tells us), just
2535 call the generic function. Any adjustment will be done at final
2536 link time. */
2537 if (output_bfd != NULL)
2538 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2539 input_section, output_bfd, error_message);
2540
2541 /* Adjust the addend for sign extension of the low 16 bits.
2542 We won't actually be using the low 16 bits, so trashing them
2543 doesn't matter. */
2544 reloc_entry->addend += 0x8000;
2545 r_type = reloc_entry->howto->type;
2546 if (r_type != R_PPC64_REL16DX_HA)
2547 return bfd_reloc_continue;
2548
2549 value = 0;
2550 if (!bfd_is_com_section (symbol->section))
2551 value = symbol->value;
2552 value += (reloc_entry->addend
2553 + symbol->section->output_offset
2554 + symbol->section->output_section->vma);
2555 value -= (reloc_entry->address
2556 + input_section->output_offset
2557 + input_section->output_section->vma);
2558 value = (bfd_signed_vma) value >> 16;
2559
2560 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2561 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2562 insn &= ~0x1fffc1;
2563 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2565 if (value + 0x8000 > 0xffff)
2566 return bfd_reloc_overflow;
2567 return bfd_reloc_ok;
2568 }
2569
2570 static bfd_reloc_status_type
2571 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2572 void *data, asection *input_section,
2573 bfd *output_bfd, char **error_message)
2574 {
2575 if (output_bfd != NULL)
2576 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2577 input_section, output_bfd, error_message);
2578
2579 if (strcmp (symbol->section->name, ".opd") == 0
2580 && (symbol->section->owner->flags & DYNAMIC) == 0)
2581 {
2582 bfd_vma dest = opd_entry_value (symbol->section,
2583 symbol->value + reloc_entry->addend,
2584 NULL, NULL, FALSE);
2585 if (dest != (bfd_vma) -1)
2586 reloc_entry->addend = dest - (symbol->value
2587 + symbol->section->output_section->vma
2588 + symbol->section->output_offset);
2589 }
2590 else
2591 {
2592 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2593
2594 if (symbol->section->owner != abfd
2595 && symbol->section->owner != NULL
2596 && abiversion (symbol->section->owner) >= 2)
2597 {
2598 unsigned int i;
2599
2600 for (i = 0; i < symbol->section->owner->symcount; ++i)
2601 {
2602 asymbol *symdef = symbol->section->owner->outsymbols[i];
2603
2604 if (strcmp (symdef->name, symbol->name) == 0)
2605 {
2606 elfsym = (elf_symbol_type *) symdef;
2607 break;
2608 }
2609 }
2610 }
2611 reloc_entry->addend
2612 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2613 }
2614 return bfd_reloc_continue;
2615 }
2616
2617 static bfd_reloc_status_type
2618 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2619 void *data, asection *input_section,
2620 bfd *output_bfd, char **error_message)
2621 {
2622 long insn;
2623 enum elf_ppc64_reloc_type r_type;
2624 bfd_size_type octets;
2625 /* Assume 'at' branch hints. */
2626 bfd_boolean is_isa_v2 = TRUE;
2627
2628 /* If this is a relocatable link (output_bfd test tells us), just
2629 call the generic function. Any adjustment will be done at final
2630 link time. */
2631 if (output_bfd != NULL)
2632 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2633 input_section, output_bfd, error_message);
2634
2635 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2636 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2637 insn &= ~(0x01 << 21);
2638 r_type = reloc_entry->howto->type;
2639 if (r_type == R_PPC64_ADDR14_BRTAKEN
2640 || r_type == R_PPC64_REL14_BRTAKEN)
2641 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2642
2643 if (is_isa_v2)
2644 {
2645 /* Set 'a' bit. This is 0b00010 in BO field for branch
2646 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2647 for branch on CTR insns (BO == 1a00t or 1a01t). */
2648 if ((insn & (0x14 << 21)) == (0x04 << 21))
2649 insn |= 0x02 << 21;
2650 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2651 insn |= 0x08 << 21;
2652 else
2653 goto out;
2654 }
2655 else
2656 {
2657 bfd_vma target = 0;
2658 bfd_vma from;
2659
2660 if (!bfd_is_com_section (symbol->section))
2661 target = symbol->value;
2662 target += symbol->section->output_section->vma;
2663 target += symbol->section->output_offset;
2664 target += reloc_entry->addend;
2665
2666 from = (reloc_entry->address
2667 + input_section->output_offset
2668 + input_section->output_section->vma);
2669
2670 /* Invert 'y' bit if not the default. */
2671 if ((bfd_signed_vma) (target - from) < 0)
2672 insn ^= 0x01 << 21;
2673 }
2674 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2675 out:
2676 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2677 input_section, output_bfd, error_message);
2678 }
2679
2680 static bfd_reloc_status_type
2681 ppc64_elf_sectoff_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 return bfd_reloc_continue;
2695 }
2696
2697 static bfd_reloc_status_type
2698 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2699 void *data, asection *input_section,
2700 bfd *output_bfd, char **error_message)
2701 {
2702 /* If this is a relocatable link (output_bfd test tells us), just
2703 call the generic function. Any adjustment will be done at final
2704 link time. */
2705 if (output_bfd != NULL)
2706 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2707 input_section, output_bfd, error_message);
2708
2709 /* Subtract the symbol section base address. */
2710 reloc_entry->addend -= symbol->section->output_section->vma;
2711
2712 /* Adjust the addend for sign extension of the low 16 bits. */
2713 reloc_entry->addend += 0x8000;
2714 return bfd_reloc_continue;
2715 }
2716
2717 static bfd_reloc_status_type
2718 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2719 void *data, asection *input_section,
2720 bfd *output_bfd, char **error_message)
2721 {
2722 bfd_vma TOCstart;
2723
2724 /* If this is a relocatable link (output_bfd test tells us), just
2725 call the generic function. Any adjustment will be done at final
2726 link time. */
2727 if (output_bfd != NULL)
2728 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2729 input_section, output_bfd, error_message);
2730
2731 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2732 if (TOCstart == 0)
2733 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2734
2735 /* Subtract the TOC base address. */
2736 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2737 return bfd_reloc_continue;
2738 }
2739
2740 static bfd_reloc_status_type
2741 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2742 void *data, asection *input_section,
2743 bfd *output_bfd, char **error_message)
2744 {
2745 bfd_vma TOCstart;
2746
2747 /* If this is a relocatable link (output_bfd test tells us), just
2748 call the generic function. Any adjustment will be done at final
2749 link time. */
2750 if (output_bfd != NULL)
2751 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2752 input_section, output_bfd, error_message);
2753
2754 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2755 if (TOCstart == 0)
2756 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2757
2758 /* Subtract the TOC base address. */
2759 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2760
2761 /* Adjust the addend for sign extension of the low 16 bits. */
2762 reloc_entry->addend += 0x8000;
2763 return bfd_reloc_continue;
2764 }
2765
2766 static bfd_reloc_status_type
2767 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2768 void *data, asection *input_section,
2769 bfd *output_bfd, char **error_message)
2770 {
2771 bfd_vma TOCstart;
2772 bfd_size_type octets;
2773
2774 /* If this is a relocatable link (output_bfd test tells us), just
2775 call the generic function. Any adjustment will be done at final
2776 link time. */
2777 if (output_bfd != NULL)
2778 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2779 input_section, output_bfd, error_message);
2780
2781 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2782 if (TOCstart == 0)
2783 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2784
2785 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2786 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2787 return bfd_reloc_ok;
2788 }
2789
2790 static bfd_reloc_status_type
2791 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2792 void *data, asection *input_section,
2793 bfd *output_bfd, char **error_message)
2794 {
2795 /* If this is a relocatable link (output_bfd test tells us), just
2796 call the generic function. Any adjustment will be done at final
2797 link time. */
2798 if (output_bfd != NULL)
2799 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2800 input_section, output_bfd, error_message);
2801
2802 if (error_message != NULL)
2803 {
2804 static char buf[60];
2805 sprintf (buf, "generic linker can't handle %s",
2806 reloc_entry->howto->name);
2807 *error_message = buf;
2808 }
2809 return bfd_reloc_dangerous;
2810 }
2811
2812 /* Track GOT entries needed for a given symbol. We might need more
2813 than one got entry per symbol. */
2814 struct got_entry
2815 {
2816 struct got_entry *next;
2817
2818 /* The symbol addend that we'll be placing in the GOT. */
2819 bfd_vma addend;
2820
2821 /* Unlike other ELF targets, we use separate GOT entries for the same
2822 symbol referenced from different input files. This is to support
2823 automatic multiple TOC/GOT sections, where the TOC base can vary
2824 from one input file to another. After partitioning into TOC groups
2825 we merge entries within the group.
2826
2827 Point to the BFD owning this GOT entry. */
2828 bfd *owner;
2829
2830 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2831 TLS_TPREL or TLS_DTPREL for tls entries. */
2832 unsigned char tls_type;
2833
2834 /* Non-zero if got.ent points to real entry. */
2835 unsigned char is_indirect;
2836
2837 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2838 union
2839 {
2840 bfd_signed_vma refcount;
2841 bfd_vma offset;
2842 struct got_entry *ent;
2843 } got;
2844 };
2845
2846 /* The same for PLT. */
2847 struct plt_entry
2848 {
2849 struct plt_entry *next;
2850
2851 bfd_vma addend;
2852
2853 union
2854 {
2855 bfd_signed_vma refcount;
2856 bfd_vma offset;
2857 } plt;
2858 };
2859
2860 struct ppc64_elf_obj_tdata
2861 {
2862 struct elf_obj_tdata elf;
2863
2864 /* Shortcuts to dynamic linker sections. */
2865 asection *got;
2866 asection *relgot;
2867
2868 /* Used during garbage collection. We attach global symbols defined
2869 on removed .opd entries to this section so that the sym is removed. */
2870 asection *deleted_section;
2871
2872 /* TLS local dynamic got entry handling. Support for multiple GOT
2873 sections means we potentially need one of these for each input bfd. */
2874 struct got_entry tlsld_got;
2875
2876 union {
2877 /* A copy of relocs before they are modified for --emit-relocs. */
2878 Elf_Internal_Rela *relocs;
2879
2880 /* Section contents. */
2881 bfd_byte *contents;
2882 } opd;
2883
2884 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2885 the reloc to be in the range -32768 to 32767. */
2886 unsigned int has_small_toc_reloc : 1;
2887
2888 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2889 instruction not one we handle. */
2890 unsigned int unexpected_toc_insn : 1;
2891 };
2892
2893 #define ppc64_elf_tdata(bfd) \
2894 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2895
2896 #define ppc64_tlsld_got(bfd) \
2897 (&ppc64_elf_tdata (bfd)->tlsld_got)
2898
2899 #define is_ppc64_elf(bfd) \
2900 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2901 && elf_object_id (bfd) == PPC64_ELF_DATA)
2902
2903 /* Override the generic function because we store some extras. */
2904
2905 static bfd_boolean
2906 ppc64_elf_mkobject (bfd *abfd)
2907 {
2908 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2909 PPC64_ELF_DATA);
2910 }
2911
2912 /* Fix bad default arch selected for a 64 bit input bfd when the
2913 default is 32 bit. Also select arch based on apuinfo. */
2914
2915 static bfd_boolean
2916 ppc64_elf_object_p (bfd *abfd)
2917 {
2918 if (!abfd->arch_info->the_default)
2919 return TRUE;
2920
2921 if (abfd->arch_info->bits_per_word == 32)
2922 {
2923 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2924
2925 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2926 {
2927 /* Relies on arch after 32 bit default being 64 bit default. */
2928 abfd->arch_info = abfd->arch_info->next;
2929 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2930 }
2931 }
2932 return _bfd_elf_ppc_set_arch (abfd);
2933 }
2934
2935 /* Support for core dump NOTE sections. */
2936
2937 static bfd_boolean
2938 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2939 {
2940 size_t offset, size;
2941
2942 if (note->descsz != 504)
2943 return FALSE;
2944
2945 /* pr_cursig */
2946 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2947
2948 /* pr_pid */
2949 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2950
2951 /* pr_reg */
2952 offset = 112;
2953 size = 384;
2954
2955 /* Make a ".reg/999" section. */
2956 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2957 size, note->descpos + offset);
2958 }
2959
2960 static bfd_boolean
2961 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2962 {
2963 if (note->descsz != 136)
2964 return FALSE;
2965
2966 elf_tdata (abfd)->core->pid
2967 = bfd_get_32 (abfd, note->descdata + 24);
2968 elf_tdata (abfd)->core->program
2969 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2970 elf_tdata (abfd)->core->command
2971 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2972
2973 return TRUE;
2974 }
2975
2976 static char *
2977 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2978 ...)
2979 {
2980 switch (note_type)
2981 {
2982 default:
2983 return NULL;
2984
2985 case NT_PRPSINFO:
2986 {
2987 char data[136];
2988 va_list ap;
2989
2990 va_start (ap, note_type);
2991 memset (data, 0, sizeof (data));
2992 strncpy (data + 40, va_arg (ap, const char *), 16);
2993 strncpy (data + 56, va_arg (ap, const char *), 80);
2994 va_end (ap);
2995 return elfcore_write_note (abfd, buf, bufsiz,
2996 "CORE", note_type, data, sizeof (data));
2997 }
2998
2999 case NT_PRSTATUS:
3000 {
3001 char data[504];
3002 va_list ap;
3003 long pid;
3004 int cursig;
3005 const void *greg;
3006
3007 va_start (ap, note_type);
3008 memset (data, 0, 112);
3009 pid = va_arg (ap, long);
3010 bfd_put_32 (abfd, pid, data + 32);
3011 cursig = va_arg (ap, int);
3012 bfd_put_16 (abfd, cursig, data + 12);
3013 greg = va_arg (ap, const void *);
3014 memcpy (data + 112, greg, 384);
3015 memset (data + 496, 0, 8);
3016 va_end (ap);
3017 return elfcore_write_note (abfd, buf, bufsiz,
3018 "CORE", note_type, data, sizeof (data));
3019 }
3020 }
3021 }
3022
3023 /* Add extra PPC sections. */
3024
3025 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3026 {
3027 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3028 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3029 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3030 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3031 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3032 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3033 { NULL, 0, 0, 0, 0 }
3034 };
3035
3036 enum _ppc64_sec_type {
3037 sec_normal = 0,
3038 sec_opd = 1,
3039 sec_toc = 2
3040 };
3041
3042 struct _ppc64_elf_section_data
3043 {
3044 struct bfd_elf_section_data elf;
3045
3046 union
3047 {
3048 /* An array with one entry for each opd function descriptor,
3049 and some spares since opd entries may be either 16 or 24 bytes. */
3050 #define OPD_NDX(OFF) ((OFF) >> 4)
3051 struct _opd_sec_data
3052 {
3053 /* Points to the function code section for local opd entries. */
3054 asection **func_sec;
3055
3056 /* After editing .opd, adjust references to opd local syms. */
3057 long *adjust;
3058 } opd;
3059
3060 /* An array for toc sections, indexed by offset/8. */
3061 struct _toc_sec_data
3062 {
3063 /* Specifies the relocation symbol index used at a given toc offset. */
3064 unsigned *symndx;
3065
3066 /* And the relocation addend. */
3067 bfd_vma *add;
3068 } toc;
3069 } u;
3070
3071 enum _ppc64_sec_type sec_type:2;
3072
3073 /* Flag set when small branches are detected. Used to
3074 select suitable defaults for the stub group size. */
3075 unsigned int has_14bit_branch:1;
3076 };
3077
3078 #define ppc64_elf_section_data(sec) \
3079 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3080
3081 static bfd_boolean
3082 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3083 {
3084 if (!sec->used_by_bfd)
3085 {
3086 struct _ppc64_elf_section_data *sdata;
3087 bfd_size_type amt = sizeof (*sdata);
3088
3089 sdata = bfd_zalloc (abfd, amt);
3090 if (sdata == NULL)
3091 return FALSE;
3092 sec->used_by_bfd = sdata;
3093 }
3094
3095 return _bfd_elf_new_section_hook (abfd, sec);
3096 }
3097
3098 static struct _opd_sec_data *
3099 get_opd_info (asection * sec)
3100 {
3101 if (sec != NULL
3102 && ppc64_elf_section_data (sec) != NULL
3103 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3104 return &ppc64_elf_section_data (sec)->u.opd;
3105 return NULL;
3106 }
3107 \f
3108 /* Parameters for the qsort hook. */
3109 static bfd_boolean synthetic_relocatable;
3110 static asection *synthetic_opd;
3111
3112 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3113
3114 static int
3115 compare_symbols (const void *ap, const void *bp)
3116 {
3117 const asymbol *a = * (const asymbol **) ap;
3118 const asymbol *b = * (const asymbol **) bp;
3119
3120 /* Section symbols first. */
3121 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3122 return -1;
3123 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3124 return 1;
3125
3126 /* then .opd symbols. */
3127 if (synthetic_opd != NULL)
3128 {
3129 if (strcmp (a->section->name, ".opd") == 0
3130 && strcmp (b->section->name, ".opd") != 0)
3131 return -1;
3132 if (strcmp (a->section->name, ".opd") != 0
3133 && strcmp (b->section->name, ".opd") == 0)
3134 return 1;
3135 }
3136
3137 /* then other code symbols. */
3138 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3139 == (SEC_CODE | SEC_ALLOC)
3140 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3141 != (SEC_CODE | SEC_ALLOC))
3142 return -1;
3143
3144 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3145 != (SEC_CODE | SEC_ALLOC)
3146 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3147 == (SEC_CODE | SEC_ALLOC))
3148 return 1;
3149
3150 if (synthetic_relocatable)
3151 {
3152 if (a->section->id < b->section->id)
3153 return -1;
3154
3155 if (a->section->id > b->section->id)
3156 return 1;
3157 }
3158
3159 if (a->value + a->section->vma < b->value + b->section->vma)
3160 return -1;
3161
3162 if (a->value + a->section->vma > b->value + b->section->vma)
3163 return 1;
3164
3165 /* For syms with the same value, prefer strong dynamic global function
3166 syms over other syms. */
3167 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3168 return -1;
3169
3170 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3171 return 1;
3172
3173 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3174 return -1;
3175
3176 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3177 return 1;
3178
3179 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3180 return -1;
3181
3182 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3183 return 1;
3184
3185 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3186 return -1;
3187
3188 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3189 return 1;
3190
3191 return 0;
3192 }
3193
3194 /* Search SYMS for a symbol of the given VALUE. */
3195
3196 static asymbol *
3197 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3198 {
3199 long mid;
3200
3201 if (id == (unsigned) -1)
3202 {
3203 while (lo < hi)
3204 {
3205 mid = (lo + hi) >> 1;
3206 if (syms[mid]->value + syms[mid]->section->vma < value)
3207 lo = mid + 1;
3208 else if (syms[mid]->value + syms[mid]->section->vma > value)
3209 hi = mid;
3210 else
3211 return syms[mid];
3212 }
3213 }
3214 else
3215 {
3216 while (lo < hi)
3217 {
3218 mid = (lo + hi) >> 1;
3219 if (syms[mid]->section->id < id)
3220 lo = mid + 1;
3221 else if (syms[mid]->section->id > id)
3222 hi = mid;
3223 else if (syms[mid]->value < value)
3224 lo = mid + 1;
3225 else if (syms[mid]->value > value)
3226 hi = mid;
3227 else
3228 return syms[mid];
3229 }
3230 }
3231 return NULL;
3232 }
3233
3234 static bfd_boolean
3235 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3236 {
3237 bfd_vma vma = *(bfd_vma *) ptr;
3238 return ((section->flags & SEC_ALLOC) != 0
3239 && section->vma <= vma
3240 && vma < section->vma + section->size);
3241 }
3242
3243 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3244 entry syms. Also generate @plt symbols for the glink branch table.
3245 Returns count of synthetic symbols in RET or -1 on error. */
3246
3247 static long
3248 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3249 long static_count, asymbol **static_syms,
3250 long dyn_count, asymbol **dyn_syms,
3251 asymbol **ret)
3252 {
3253 asymbol *s;
3254 long i;
3255 long count;
3256 char *names;
3257 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3258 asection *opd = NULL;
3259 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3260 asymbol **syms;
3261 int abi = abiversion (abfd);
3262
3263 *ret = NULL;
3264
3265 if (abi < 2)
3266 {
3267 opd = bfd_get_section_by_name (abfd, ".opd");
3268 if (opd == NULL && abi == 1)
3269 return 0;
3270 }
3271
3272 symcount = static_count;
3273 if (!relocatable)
3274 symcount += dyn_count;
3275 if (symcount == 0)
3276 return 0;
3277
3278 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3279 if (syms == NULL)
3280 return -1;
3281
3282 if (!relocatable && static_count != 0 && dyn_count != 0)
3283 {
3284 /* Use both symbol tables. */
3285 memcpy (syms, static_syms, static_count * sizeof (*syms));
3286 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3287 }
3288 else if (!relocatable && static_count == 0)
3289 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3290 else
3291 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3292
3293 synthetic_relocatable = relocatable;
3294 synthetic_opd = opd;
3295 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3296
3297 if (!relocatable && symcount > 1)
3298 {
3299 long j;
3300 /* Trim duplicate syms, since we may have merged the normal and
3301 dynamic symbols. Actually, we only care about syms that have
3302 different values, so trim any with the same value. */
3303 for (i = 1, j = 1; i < symcount; ++i)
3304 if (syms[i - 1]->value + syms[i - 1]->section->vma
3305 != syms[i]->value + syms[i]->section->vma)
3306 syms[j++] = syms[i];
3307 symcount = j;
3308 }
3309
3310 i = 0;
3311 /* Note that here and in compare_symbols we can't compare opd and
3312 sym->section directly. With separate debug info files, the
3313 symbols will be extracted from the debug file while abfd passed
3314 to this function is the real binary. */
3315 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3316 ++i;
3317 codesecsym = i;
3318
3319 for (; i < symcount; ++i)
3320 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3321 != (SEC_CODE | SEC_ALLOC))
3322 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3323 break;
3324 codesecsymend = i;
3325
3326 for (; i < symcount; ++i)
3327 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3328 break;
3329 secsymend = i;
3330
3331 if (opd != NULL)
3332 for (; i < symcount; ++i)
3333 if (strcmp (syms[i]->section->name, ".opd") != 0)
3334 break;
3335 opdsymend = i;
3336
3337 for (; i < symcount; ++i)
3338 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3339 != (SEC_CODE | SEC_ALLOC))
3340 break;
3341 symcount = i;
3342
3343 count = 0;
3344
3345 if (relocatable)
3346 {
3347 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3348 arelent *r;
3349 size_t size;
3350 long relcount;
3351
3352 if (opdsymend == secsymend)
3353 goto done;
3354
3355 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3356 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3357 if (relcount == 0)
3358 goto done;
3359
3360 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3361 {
3362 count = -1;
3363 goto done;
3364 }
3365
3366 size = 0;
3367 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3368 {
3369 asymbol *sym;
3370
3371 while (r < opd->relocation + relcount
3372 && r->address < syms[i]->value + opd->vma)
3373 ++r;
3374
3375 if (r == opd->relocation + relcount)
3376 break;
3377
3378 if (r->address != syms[i]->value + opd->vma)
3379 continue;
3380
3381 if (r->howto->type != R_PPC64_ADDR64)
3382 continue;
3383
3384 sym = *r->sym_ptr_ptr;
3385 if (!sym_exists_at (syms, opdsymend, symcount,
3386 sym->section->id, sym->value + r->addend))
3387 {
3388 ++count;
3389 size += sizeof (asymbol);
3390 size += strlen (syms[i]->name) + 2;
3391 }
3392 }
3393
3394 if (size == 0)
3395 goto done;
3396 s = *ret = bfd_malloc (size);
3397 if (s == NULL)
3398 {
3399 count = -1;
3400 goto done;
3401 }
3402
3403 names = (char *) (s + count);
3404
3405 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3406 {
3407 asymbol *sym;
3408
3409 while (r < opd->relocation + relcount
3410 && r->address < syms[i]->value + opd->vma)
3411 ++r;
3412
3413 if (r == opd->relocation + relcount)
3414 break;
3415
3416 if (r->address != syms[i]->value + opd->vma)
3417 continue;
3418
3419 if (r->howto->type != R_PPC64_ADDR64)
3420 continue;
3421
3422 sym = *r->sym_ptr_ptr;
3423 if (!sym_exists_at (syms, opdsymend, symcount,
3424 sym->section->id, sym->value + r->addend))
3425 {
3426 size_t len;
3427
3428 *s = *syms[i];
3429 s->flags |= BSF_SYNTHETIC;
3430 s->section = sym->section;
3431 s->value = sym->value + r->addend;
3432 s->name = names;
3433 *names++ = '.';
3434 len = strlen (syms[i]->name);
3435 memcpy (names, syms[i]->name, len + 1);
3436 names += len + 1;
3437 /* Have udata.p point back to the original symbol this
3438 synthetic symbol was derived from. */
3439 s->udata.p = syms[i];
3440 s++;
3441 }
3442 }
3443 }
3444 else
3445 {
3446 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3447 bfd_byte *contents = NULL;
3448 size_t size;
3449 long plt_count = 0;
3450 bfd_vma glink_vma = 0, resolv_vma = 0;
3451 asection *dynamic, *glink = NULL, *relplt = NULL;
3452 arelent *p;
3453
3454 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3455 {
3456 free_contents_and_exit_err:
3457 count = -1;
3458 free_contents_and_exit:
3459 if (contents)
3460 free (contents);
3461 goto done;
3462 }
3463
3464 size = 0;
3465 for (i = secsymend; i < opdsymend; ++i)
3466 {
3467 bfd_vma ent;
3468
3469 /* Ignore bogus symbols. */
3470 if (syms[i]->value > opd->size - 8)
3471 continue;
3472
3473 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3474 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3475 {
3476 ++count;
3477 size += sizeof (asymbol);
3478 size += strlen (syms[i]->name) + 2;
3479 }
3480 }
3481
3482 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3483 if (dyn_count != 0
3484 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3485 {
3486 bfd_byte *dynbuf, *extdyn, *extdynend;
3487 size_t extdynsize;
3488 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3489
3490 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3491 goto free_contents_and_exit_err;
3492
3493 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3494 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3495
3496 extdyn = dynbuf;
3497 extdynend = extdyn + dynamic->size;
3498 for (; extdyn < extdynend; extdyn += extdynsize)
3499 {
3500 Elf_Internal_Dyn dyn;
3501 (*swap_dyn_in) (abfd, extdyn, &dyn);
3502
3503 if (dyn.d_tag == DT_NULL)
3504 break;
3505
3506 if (dyn.d_tag == DT_PPC64_GLINK)
3507 {
3508 /* The first glink stub starts at offset 32; see
3509 comment in ppc64_elf_finish_dynamic_sections. */
3510 glink_vma = dyn.d_un.d_val + GLINK_CALL_STUB_SIZE - 8 * 4;
3511 /* The .glink section usually does not survive the final
3512 link; search for the section (usually .text) where the
3513 glink stubs now reside. */
3514 glink = bfd_sections_find_if (abfd, section_covers_vma,
3515 &glink_vma);
3516 break;
3517 }
3518 }
3519
3520 free (dynbuf);
3521 }
3522
3523 if (glink != NULL)
3524 {
3525 /* Determine __glink trampoline by reading the relative branch
3526 from the first glink stub. */
3527 bfd_byte buf[4];
3528 unsigned int off = 0;
3529
3530 while (bfd_get_section_contents (abfd, glink, buf,
3531 glink_vma + off - glink->vma, 4))
3532 {
3533 unsigned int insn = bfd_get_32 (abfd, buf);
3534 insn ^= B_DOT;
3535 if ((insn & ~0x3fffffc) == 0)
3536 {
3537 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3538 break;
3539 }
3540 off += 4;
3541 if (off > 4)
3542 break;
3543 }
3544
3545 if (resolv_vma)
3546 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3547
3548 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3549 if (relplt != NULL)
3550 {
3551 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3552 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3553 goto free_contents_and_exit_err;
3554
3555 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3556 size += plt_count * sizeof (asymbol);
3557
3558 p = relplt->relocation;
3559 for (i = 0; i < plt_count; i++, p++)
3560 {
3561 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3562 if (p->addend != 0)
3563 size += sizeof ("+0x") - 1 + 16;
3564 }
3565 }
3566 }
3567
3568 if (size == 0)
3569 goto free_contents_and_exit;
3570 s = *ret = bfd_malloc (size);
3571 if (s == NULL)
3572 goto free_contents_and_exit_err;
3573
3574 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3575
3576 for (i = secsymend; i < opdsymend; ++i)
3577 {
3578 bfd_vma ent;
3579
3580 if (syms[i]->value > opd->size - 8)
3581 continue;
3582
3583 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3584 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3585 {
3586 long lo, hi;
3587 size_t len;
3588 asection *sec = abfd->sections;
3589
3590 *s = *syms[i];
3591 lo = codesecsym;
3592 hi = codesecsymend;
3593 while (lo < hi)
3594 {
3595 long mid = (lo + hi) >> 1;
3596 if (syms[mid]->section->vma < ent)
3597 lo = mid + 1;
3598 else if (syms[mid]->section->vma > ent)
3599 hi = mid;
3600 else
3601 {
3602 sec = syms[mid]->section;
3603 break;
3604 }
3605 }
3606
3607 if (lo >= hi && lo > codesecsym)
3608 sec = syms[lo - 1]->section;
3609
3610 for (; sec != NULL; sec = sec->next)
3611 {
3612 if (sec->vma > ent)
3613 break;
3614 /* SEC_LOAD may not be set if SEC is from a separate debug
3615 info file. */
3616 if ((sec->flags & SEC_ALLOC) == 0)
3617 break;
3618 if ((sec->flags & SEC_CODE) != 0)
3619 s->section = sec;
3620 }
3621 s->flags |= BSF_SYNTHETIC;
3622 s->value = ent - s->section->vma;
3623 s->name = names;
3624 *names++ = '.';
3625 len = strlen (syms[i]->name);
3626 memcpy (names, syms[i]->name, len + 1);
3627 names += len + 1;
3628 /* Have udata.p point back to the original symbol this
3629 synthetic symbol was derived from. */
3630 s->udata.p = syms[i];
3631 s++;
3632 }
3633 }
3634 free (contents);
3635
3636 if (glink != NULL && relplt != NULL)
3637 {
3638 if (resolv_vma)
3639 {
3640 /* Add a symbol for the main glink trampoline. */
3641 memset (s, 0, sizeof *s);
3642 s->the_bfd = abfd;
3643 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3644 s->section = glink;
3645 s->value = resolv_vma - glink->vma;
3646 s->name = names;
3647 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3648 names += sizeof ("__glink_PLTresolve");
3649 s++;
3650 count++;
3651 }
3652
3653 /* FIXME: It would be very much nicer to put sym@plt on the
3654 stub rather than on the glink branch table entry. The
3655 objdump disassembler would then use a sensible symbol
3656 name on plt calls. The difficulty in doing so is
3657 a) finding the stubs, and,
3658 b) matching stubs against plt entries, and,
3659 c) there can be multiple stubs for a given plt entry.
3660
3661 Solving (a) could be done by code scanning, but older
3662 ppc64 binaries used different stubs to current code.
3663 (b) is the tricky one since you need to known the toc
3664 pointer for at least one function that uses a pic stub to
3665 be able to calculate the plt address referenced.
3666 (c) means gdb would need to set multiple breakpoints (or
3667 find the glink branch itself) when setting breakpoints
3668 for pending shared library loads. */
3669 p = relplt->relocation;
3670 for (i = 0; i < plt_count; i++, p++)
3671 {
3672 size_t len;
3673
3674 *s = **p->sym_ptr_ptr;
3675 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3676 we are defining a symbol, ensure one of them is set. */
3677 if ((s->flags & BSF_LOCAL) == 0)
3678 s->flags |= BSF_GLOBAL;
3679 s->flags |= BSF_SYNTHETIC;
3680 s->section = glink;
3681 s->value = glink_vma - glink->vma;
3682 s->name = names;
3683 s->udata.p = NULL;
3684 len = strlen ((*p->sym_ptr_ptr)->name);
3685 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3686 names += len;
3687 if (p->addend != 0)
3688 {
3689 memcpy (names, "+0x", sizeof ("+0x") - 1);
3690 names += sizeof ("+0x") - 1;
3691 bfd_sprintf_vma (abfd, names, p->addend);
3692 names += strlen (names);
3693 }
3694 memcpy (names, "@plt", sizeof ("@plt"));
3695 names += sizeof ("@plt");
3696 s++;
3697 if (abi < 2)
3698 {
3699 glink_vma += 8;
3700 if (i >= 0x8000)
3701 glink_vma += 4;
3702 }
3703 else
3704 glink_vma += 4;
3705 }
3706 count += plt_count;
3707 }
3708 }
3709
3710 done:
3711 free (syms);
3712 return count;
3713 }
3714 \f
3715 /* The following functions are specific to the ELF linker, while
3716 functions above are used generally. Those named ppc64_elf_* are
3717 called by the main ELF linker code. They appear in this file more
3718 or less in the order in which they are called. eg.
3719 ppc64_elf_check_relocs is called early in the link process,
3720 ppc64_elf_finish_dynamic_sections is one of the last functions
3721 called.
3722
3723 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3724 functions have both a function code symbol and a function descriptor
3725 symbol. A call to foo in a relocatable object file looks like:
3726
3727 . .text
3728 . x:
3729 . bl .foo
3730 . nop
3731
3732 The function definition in another object file might be:
3733
3734 . .section .opd
3735 . foo: .quad .foo
3736 . .quad .TOC.@tocbase
3737 . .quad 0
3738 .
3739 . .text
3740 . .foo: blr
3741
3742 When the linker resolves the call during a static link, the branch
3743 unsurprisingly just goes to .foo and the .opd information is unused.
3744 If the function definition is in a shared library, things are a little
3745 different: The call goes via a plt call stub, the opd information gets
3746 copied to the plt, and the linker patches the nop.
3747
3748 . x:
3749 . bl .foo_stub
3750 . ld 2,40(1)
3751 .
3752 .
3753 . .foo_stub:
3754 . std 2,40(1) # in practice, the call stub
3755 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3756 . addi 11,11,Lfoo@toc@l # this is the general idea
3757 . ld 12,0(11)
3758 . ld 2,8(11)
3759 . mtctr 12
3760 . ld 11,16(11)
3761 . bctr
3762 .
3763 . .section .plt
3764 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3765
3766 The "reloc ()" notation is supposed to indicate that the linker emits
3767 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3768 copying.
3769
3770 What are the difficulties here? Well, firstly, the relocations
3771 examined by the linker in check_relocs are against the function code
3772 sym .foo, while the dynamic relocation in the plt is emitted against
3773 the function descriptor symbol, foo. Somewhere along the line, we need
3774 to carefully copy dynamic link information from one symbol to the other.
3775 Secondly, the generic part of the elf linker will make .foo a dynamic
3776 symbol as is normal for most other backends. We need foo dynamic
3777 instead, at least for an application final link. However, when
3778 creating a shared library containing foo, we need to have both symbols
3779 dynamic so that references to .foo are satisfied during the early
3780 stages of linking. Otherwise the linker might decide to pull in a
3781 definition from some other object, eg. a static library.
3782
3783 Update: As of August 2004, we support a new convention. Function
3784 calls may use the function descriptor symbol, ie. "bl foo". This
3785 behaves exactly as "bl .foo". */
3786
3787 /* Of those relocs that might be copied as dynamic relocs, this function
3788 selects those that must be copied when linking a shared library,
3789 even when the symbol is local. */
3790
3791 static int
3792 must_be_dyn_reloc (struct bfd_link_info *info,
3793 enum elf_ppc64_reloc_type r_type)
3794 {
3795 switch (r_type)
3796 {
3797 default:
3798 return 1;
3799
3800 case R_PPC64_REL32:
3801 case R_PPC64_REL64:
3802 case R_PPC64_REL30:
3803 return 0;
3804
3805 case R_PPC64_TPREL16:
3806 case R_PPC64_TPREL16_LO:
3807 case R_PPC64_TPREL16_HI:
3808 case R_PPC64_TPREL16_HA:
3809 case R_PPC64_TPREL16_DS:
3810 case R_PPC64_TPREL16_LO_DS:
3811 case R_PPC64_TPREL16_HIGH:
3812 case R_PPC64_TPREL16_HIGHA:
3813 case R_PPC64_TPREL16_HIGHER:
3814 case R_PPC64_TPREL16_HIGHERA:
3815 case R_PPC64_TPREL16_HIGHEST:
3816 case R_PPC64_TPREL16_HIGHESTA:
3817 case R_PPC64_TPREL64:
3818 return !bfd_link_executable (info);
3819 }
3820 }
3821
3822 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3823 copying dynamic variables from a shared lib into an app's dynbss
3824 section, and instead use a dynamic relocation to point into the
3825 shared lib. With code that gcc generates, it's vital that this be
3826 enabled; In the PowerPC64 ABI, the address of a function is actually
3827 the address of a function descriptor, which resides in the .opd
3828 section. gcc uses the descriptor directly rather than going via the
3829 GOT as some other ABI's do, which means that initialized function
3830 pointers must reference the descriptor. Thus, a function pointer
3831 initialized to the address of a function in a shared library will
3832 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3833 redefines the function descriptor symbol to point to the copy. This
3834 presents a problem as a plt entry for that function is also
3835 initialized from the function descriptor symbol and the copy reloc
3836 may not be initialized first. */
3837 #define ELIMINATE_COPY_RELOCS 1
3838
3839 /* Section name for stubs is the associated section name plus this
3840 string. */
3841 #define STUB_SUFFIX ".stub"
3842
3843 /* Linker stubs.
3844 ppc_stub_long_branch:
3845 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3846 destination, but a 24 bit branch in a stub section will reach.
3847 . b dest
3848
3849 ppc_stub_plt_branch:
3850 Similar to the above, but a 24 bit branch in the stub section won't
3851 reach its destination.
3852 . addis %r11,%r2,xxx@toc@ha
3853 . ld %r12,xxx@toc@l(%r11)
3854 . mtctr %r12
3855 . bctr
3856
3857 ppc_stub_plt_call:
3858 Used to call a function in a shared library. If it so happens that
3859 the plt entry referenced crosses a 64k boundary, then an extra
3860 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3861 . std %r2,40(%r1)
3862 . addis %r11,%r2,xxx@toc@ha
3863 . ld %r12,xxx+0@toc@l(%r11)
3864 . mtctr %r12
3865 . ld %r2,xxx+8@toc@l(%r11)
3866 . ld %r11,xxx+16@toc@l(%r11)
3867 . bctr
3868
3869 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3870 code to adjust the value and save r2 to support multiple toc sections.
3871 A ppc_stub_long_branch with an r2 offset looks like:
3872 . std %r2,40(%r1)
3873 . addis %r2,%r2,off@ha
3874 . addi %r2,%r2,off@l
3875 . b dest
3876
3877 A ppc_stub_plt_branch with an r2 offset looks like:
3878 . std %r2,40(%r1)
3879 . addis %r11,%r2,xxx@toc@ha
3880 . ld %r12,xxx@toc@l(%r11)
3881 . addis %r2,%r2,off@ha
3882 . addi %r2,%r2,off@l
3883 . mtctr %r12
3884 . bctr
3885
3886 In cases where the "addis" instruction would add zero, the "addis" is
3887 omitted and following instructions modified slightly in some cases.
3888 */
3889
3890 enum ppc_stub_type {
3891 ppc_stub_none,
3892 ppc_stub_long_branch,
3893 ppc_stub_long_branch_r2off,
3894 ppc_stub_plt_branch,
3895 ppc_stub_plt_branch_r2off,
3896 ppc_stub_plt_call,
3897 ppc_stub_plt_call_r2save,
3898 ppc_stub_global_entry,
3899 ppc_stub_save_res
3900 };
3901
3902 /* Information on stub grouping. */
3903 struct map_stub
3904 {
3905 /* The stub section. */
3906 asection *stub_sec;
3907 /* This is the section to which stubs in the group will be attached. */
3908 asection *link_sec;
3909 /* Next group. */
3910 struct map_stub *next;
3911 /* Whether to emit a copy of register save/restore functions in this
3912 group. */
3913 int needs_save_res;
3914 };
3915
3916 struct ppc_stub_hash_entry {
3917
3918 /* Base hash table entry structure. */
3919 struct bfd_hash_entry root;
3920
3921 enum ppc_stub_type stub_type;
3922
3923 /* Group information. */
3924 struct map_stub *group;
3925
3926 /* Offset within stub_sec of the beginning of this stub. */
3927 bfd_vma stub_offset;
3928
3929 /* Given the symbol's value and its section we can determine its final
3930 value when building the stubs (so the stub knows where to jump. */
3931 bfd_vma target_value;
3932 asection *target_section;
3933
3934 /* The symbol table entry, if any, that this was derived from. */
3935 struct ppc_link_hash_entry *h;
3936 struct plt_entry *plt_ent;
3937
3938 /* Symbol st_other. */
3939 unsigned char other;
3940 };
3941
3942 struct ppc_branch_hash_entry {
3943
3944 /* Base hash table entry structure. */
3945 struct bfd_hash_entry root;
3946
3947 /* Offset within branch lookup table. */
3948 unsigned int offset;
3949
3950 /* Generation marker. */
3951 unsigned int iter;
3952 };
3953
3954 /* Used to track dynamic relocations for local symbols. */
3955 struct ppc_dyn_relocs
3956 {
3957 struct ppc_dyn_relocs *next;
3958
3959 /* The input section of the reloc. */
3960 asection *sec;
3961
3962 /* Total number of relocs copied for the input section. */
3963 unsigned int count : 31;
3964
3965 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3966 unsigned int ifunc : 1;
3967 };
3968
3969 struct ppc_link_hash_entry
3970 {
3971 struct elf_link_hash_entry elf;
3972
3973 union {
3974 /* A pointer to the most recently used stub hash entry against this
3975 symbol. */
3976 struct ppc_stub_hash_entry *stub_cache;
3977
3978 /* A pointer to the next symbol starting with a '.' */
3979 struct ppc_link_hash_entry *next_dot_sym;
3980 } u;
3981
3982 /* Track dynamic relocs copied for this symbol. */
3983 struct elf_dyn_relocs *dyn_relocs;
3984
3985 /* Chain of aliases referring to a weakdef. */
3986 struct ppc_link_hash_entry *weakref;
3987
3988 /* Link between function code and descriptor symbols. */
3989 struct ppc_link_hash_entry *oh;
3990
3991 /* Flag function code and descriptor symbols. */
3992 unsigned int is_func:1;
3993 unsigned int is_func_descriptor:1;
3994 unsigned int fake:1;
3995
3996 /* Whether global opd/toc sym has been adjusted or not.
3997 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3998 should be set for all globals defined in any opd/toc section. */
3999 unsigned int adjust_done:1;
4000
4001 /* Set if this is an out-of-line register save/restore function,
4002 with non-standard calling convention. */
4003 unsigned int save_res:1;
4004
4005 /* Contexts in which symbol is used in the GOT (or TOC).
4006 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4007 corresponding relocs are encountered during check_relocs.
4008 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4009 indicate the corresponding GOT entry type is not needed.
4010 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4011 a TPREL one. We use a separate flag rather than setting TPREL
4012 just for convenience in distinguishing the two cases. */
4013 #define TLS_GD 1 /* GD reloc. */
4014 #define TLS_LD 2 /* LD reloc. */
4015 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
4016 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4017 #define TLS_TLS 16 /* Any TLS reloc. */
4018 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4019 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4020 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
4021 unsigned char tls_mask;
4022 };
4023
4024 /* ppc64 ELF linker hash table. */
4025
4026 struct ppc_link_hash_table
4027 {
4028 struct elf_link_hash_table elf;
4029
4030 /* The stub hash table. */
4031 struct bfd_hash_table stub_hash_table;
4032
4033 /* Another hash table for plt_branch stubs. */
4034 struct bfd_hash_table branch_hash_table;
4035
4036 /* Hash table for function prologue tocsave. */
4037 htab_t tocsave_htab;
4038
4039 /* Various options and other info passed from the linker. */
4040 struct ppc64_elf_params *params;
4041
4042 /* The size of sec_info below. */
4043 unsigned int sec_info_arr_size;
4044
4045 /* Per-section array of extra section info. Done this way rather
4046 than as part of ppc64_elf_section_data so we have the info for
4047 non-ppc64 sections. */
4048 struct
4049 {
4050 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4051 bfd_vma toc_off;
4052
4053 union
4054 {
4055 /* The section group that this section belongs to. */
4056 struct map_stub *group;
4057 /* A temp section list pointer. */
4058 asection *list;
4059 } u;
4060 } *sec_info;
4061
4062 /* Linked list of groups. */
4063 struct map_stub *group;
4064
4065 /* Temp used when calculating TOC pointers. */
4066 bfd_vma toc_curr;
4067 bfd *toc_bfd;
4068 asection *toc_first_sec;
4069
4070 /* Used when adding symbols. */
4071 struct ppc_link_hash_entry *dot_syms;
4072
4073 /* Shortcuts to get to dynamic linker sections. */
4074 asection *glink;
4075 asection *sfpr;
4076 asection *brlt;
4077 asection *relbrlt;
4078 asection *glink_eh_frame;
4079
4080 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4081 struct ppc_link_hash_entry *tls_get_addr;
4082 struct ppc_link_hash_entry *tls_get_addr_fd;
4083
4084 /* The size of reliplt used by got entry relocs. */
4085 bfd_size_type got_reli_size;
4086
4087 /* Statistics. */
4088 unsigned long stub_count[ppc_stub_global_entry];
4089
4090 /* Number of stubs against global syms. */
4091 unsigned long stub_globals;
4092
4093 /* Set if we're linking code with function descriptors. */
4094 unsigned int opd_abi:1;
4095
4096 /* Support for multiple toc sections. */
4097 unsigned int do_multi_toc:1;
4098 unsigned int multi_toc_needed:1;
4099 unsigned int second_toc_pass:1;
4100 unsigned int do_toc_opt:1;
4101
4102 /* Set on error. */
4103 unsigned int stub_error:1;
4104
4105 /* Whether func_desc_adjust needs to be run over symbols. */
4106 unsigned int need_func_desc_adj:1;
4107
4108 /* Incremented every time we size stubs. */
4109 unsigned int stub_iteration;
4110
4111 /* Small local sym cache. */
4112 struct sym_cache sym_cache;
4113 };
4114
4115 /* Rename some of the generic section flags to better document how they
4116 are used here. */
4117
4118 /* Nonzero if this section has TLS related relocations. */
4119 #define has_tls_reloc sec_flg0
4120
4121 /* Nonzero if this section has a call to __tls_get_addr. */
4122 #define has_tls_get_addr_call sec_flg1
4123
4124 /* Nonzero if this section has any toc or got relocs. */
4125 #define has_toc_reloc sec_flg2
4126
4127 /* Nonzero if this section has a call to another section that uses
4128 the toc or got. */
4129 #define makes_toc_func_call sec_flg3
4130
4131 /* Recursion protection when determining above flag. */
4132 #define call_check_in_progress sec_flg4
4133 #define call_check_done sec_flg5
4134
4135 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4136
4137 #define ppc_hash_table(p) \
4138 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4139 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4140
4141 #define ppc_stub_hash_lookup(table, string, create, copy) \
4142 ((struct ppc_stub_hash_entry *) \
4143 bfd_hash_lookup ((table), (string), (create), (copy)))
4144
4145 #define ppc_branch_hash_lookup(table, string, create, copy) \
4146 ((struct ppc_branch_hash_entry *) \
4147 bfd_hash_lookup ((table), (string), (create), (copy)))
4148
4149 /* Create an entry in the stub hash table. */
4150
4151 static struct bfd_hash_entry *
4152 stub_hash_newfunc (struct bfd_hash_entry *entry,
4153 struct bfd_hash_table *table,
4154 const char *string)
4155 {
4156 /* Allocate the structure if it has not already been allocated by a
4157 subclass. */
4158 if (entry == NULL)
4159 {
4160 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4161 if (entry == NULL)
4162 return entry;
4163 }
4164
4165 /* Call the allocation method of the superclass. */
4166 entry = bfd_hash_newfunc (entry, table, string);
4167 if (entry != NULL)
4168 {
4169 struct ppc_stub_hash_entry *eh;
4170
4171 /* Initialize the local fields. */
4172 eh = (struct ppc_stub_hash_entry *) entry;
4173 eh->stub_type = ppc_stub_none;
4174 eh->group = NULL;
4175 eh->stub_offset = 0;
4176 eh->target_value = 0;
4177 eh->target_section = NULL;
4178 eh->h = NULL;
4179 eh->plt_ent = NULL;
4180 eh->other = 0;
4181 }
4182
4183 return entry;
4184 }
4185
4186 /* Create an entry in the branch hash table. */
4187
4188 static struct bfd_hash_entry *
4189 branch_hash_newfunc (struct bfd_hash_entry *entry,
4190 struct bfd_hash_table *table,
4191 const char *string)
4192 {
4193 /* Allocate the structure if it has not already been allocated by a
4194 subclass. */
4195 if (entry == NULL)
4196 {
4197 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4198 if (entry == NULL)
4199 return entry;
4200 }
4201
4202 /* Call the allocation method of the superclass. */
4203 entry = bfd_hash_newfunc (entry, table, string);
4204 if (entry != NULL)
4205 {
4206 struct ppc_branch_hash_entry *eh;
4207
4208 /* Initialize the local fields. */
4209 eh = (struct ppc_branch_hash_entry *) entry;
4210 eh->offset = 0;
4211 eh->iter = 0;
4212 }
4213
4214 return entry;
4215 }
4216
4217 /* Create an entry in a ppc64 ELF linker hash table. */
4218
4219 static struct bfd_hash_entry *
4220 link_hash_newfunc (struct bfd_hash_entry *entry,
4221 struct bfd_hash_table *table,
4222 const char *string)
4223 {
4224 /* Allocate the structure if it has not already been allocated by a
4225 subclass. */
4226 if (entry == NULL)
4227 {
4228 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4229 if (entry == NULL)
4230 return entry;
4231 }
4232
4233 /* Call the allocation method of the superclass. */
4234 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4235 if (entry != NULL)
4236 {
4237 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4238
4239 memset (&eh->u.stub_cache, 0,
4240 (sizeof (struct ppc_link_hash_entry)
4241 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4242
4243 /* When making function calls, old ABI code references function entry
4244 points (dot symbols), while new ABI code references the function
4245 descriptor symbol. We need to make any combination of reference and
4246 definition work together, without breaking archive linking.
4247
4248 For a defined function "foo" and an undefined call to "bar":
4249 An old object defines "foo" and ".foo", references ".bar" (possibly
4250 "bar" too).
4251 A new object defines "foo" and references "bar".
4252
4253 A new object thus has no problem with its undefined symbols being
4254 satisfied by definitions in an old object. On the other hand, the
4255 old object won't have ".bar" satisfied by a new object.
4256
4257 Keep a list of newly added dot-symbols. */
4258
4259 if (string[0] == '.')
4260 {
4261 struct ppc_link_hash_table *htab;
4262
4263 htab = (struct ppc_link_hash_table *) table;
4264 eh->u.next_dot_sym = htab->dot_syms;
4265 htab->dot_syms = eh;
4266 }
4267 }
4268
4269 return entry;
4270 }
4271
4272 struct tocsave_entry {
4273 asection *sec;
4274 bfd_vma offset;
4275 };
4276
4277 static hashval_t
4278 tocsave_htab_hash (const void *p)
4279 {
4280 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4281 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4282 }
4283
4284 static int
4285 tocsave_htab_eq (const void *p1, const void *p2)
4286 {
4287 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4288 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4289 return e1->sec == e2->sec && e1->offset == e2->offset;
4290 }
4291
4292 /* Destroy a ppc64 ELF linker hash table. */
4293
4294 static void
4295 ppc64_elf_link_hash_table_free (bfd *obfd)
4296 {
4297 struct ppc_link_hash_table *htab;
4298
4299 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4300 if (htab->tocsave_htab)
4301 htab_delete (htab->tocsave_htab);
4302 bfd_hash_table_free (&htab->branch_hash_table);
4303 bfd_hash_table_free (&htab->stub_hash_table);
4304 _bfd_elf_link_hash_table_free (obfd);
4305 }
4306
4307 /* Create a ppc64 ELF linker hash table. */
4308
4309 static struct bfd_link_hash_table *
4310 ppc64_elf_link_hash_table_create (bfd *abfd)
4311 {
4312 struct ppc_link_hash_table *htab;
4313 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4314
4315 htab = bfd_zmalloc (amt);
4316 if (htab == NULL)
4317 return NULL;
4318
4319 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4320 sizeof (struct ppc_link_hash_entry),
4321 PPC64_ELF_DATA))
4322 {
4323 free (htab);
4324 return NULL;
4325 }
4326
4327 /* Init the stub hash table too. */
4328 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4329 sizeof (struct ppc_stub_hash_entry)))
4330 {
4331 _bfd_elf_link_hash_table_free (abfd);
4332 return NULL;
4333 }
4334
4335 /* And the branch hash table. */
4336 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4337 sizeof (struct ppc_branch_hash_entry)))
4338 {
4339 bfd_hash_table_free (&htab->stub_hash_table);
4340 _bfd_elf_link_hash_table_free (abfd);
4341 return NULL;
4342 }
4343
4344 htab->tocsave_htab = htab_try_create (1024,
4345 tocsave_htab_hash,
4346 tocsave_htab_eq,
4347 NULL);
4348 if (htab->tocsave_htab == NULL)
4349 {
4350 ppc64_elf_link_hash_table_free (abfd);
4351 return NULL;
4352 }
4353 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4354
4355 /* Initializing two fields of the union is just cosmetic. We really
4356 only care about glist, but when compiled on a 32-bit host the
4357 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4358 debugger inspection of these fields look nicer. */
4359 htab->elf.init_got_refcount.refcount = 0;
4360 htab->elf.init_got_refcount.glist = NULL;
4361 htab->elf.init_plt_refcount.refcount = 0;
4362 htab->elf.init_plt_refcount.glist = NULL;
4363 htab->elf.init_got_offset.offset = 0;
4364 htab->elf.init_got_offset.glist = NULL;
4365 htab->elf.init_plt_offset.offset = 0;
4366 htab->elf.init_plt_offset.glist = NULL;
4367
4368 return &htab->elf.root;
4369 }
4370
4371 /* Create sections for linker generated code. */
4372
4373 static bfd_boolean
4374 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4375 {
4376 struct ppc_link_hash_table *htab;
4377 flagword flags;
4378
4379 htab = ppc_hash_table (info);
4380
4381 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4382 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4383 if (htab->params->save_restore_funcs)
4384 {
4385 /* Create .sfpr for code to save and restore fp regs. */
4386 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4387 flags);
4388 if (htab->sfpr == NULL
4389 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4390 return FALSE;
4391 }
4392
4393 if (bfd_link_relocatable (info))
4394 return TRUE;
4395
4396 /* Create .glink for lazy dynamic linking support. */
4397 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4398 flags);
4399 if (htab->glink == NULL
4400 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4401 return FALSE;
4402
4403 if (!info->no_ld_generated_unwind_info)
4404 {
4405 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4406 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4407 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4408 ".eh_frame",
4409 flags);
4410 if (htab->glink_eh_frame == NULL
4411 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4412 return FALSE;
4413 }
4414
4415 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4416 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4417 if (htab->elf.iplt == NULL
4418 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4419 return FALSE;
4420
4421 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4422 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4423 htab->elf.irelplt
4424 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4425 if (htab->elf.irelplt == NULL
4426 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4427 return FALSE;
4428
4429 /* Create branch lookup table for plt_branch stubs. */
4430 flags = (SEC_ALLOC | SEC_LOAD
4431 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4432 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4433 flags);
4434 if (htab->brlt == NULL
4435 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4436 return FALSE;
4437
4438 if (!bfd_link_pic (info))
4439 return TRUE;
4440
4441 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4442 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4443 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4444 ".rela.branch_lt",
4445 flags);
4446 if (htab->relbrlt == NULL
4447 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4448 return FALSE;
4449
4450 return TRUE;
4451 }
4452
4453 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4454
4455 bfd_boolean
4456 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4457 struct ppc64_elf_params *params)
4458 {
4459 struct ppc_link_hash_table *htab;
4460
4461 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4462
4463 /* Always hook our dynamic sections into the first bfd, which is the
4464 linker created stub bfd. This ensures that the GOT header is at
4465 the start of the output TOC section. */
4466 htab = ppc_hash_table (info);
4467 htab->elf.dynobj = params->stub_bfd;
4468 htab->params = params;
4469
4470 return create_linkage_sections (htab->elf.dynobj, info);
4471 }
4472
4473 /* Build a name for an entry in the stub hash table. */
4474
4475 static char *
4476 ppc_stub_name (const asection *input_section,
4477 const asection *sym_sec,
4478 const struct ppc_link_hash_entry *h,
4479 const Elf_Internal_Rela *rel)
4480 {
4481 char *stub_name;
4482 ssize_t len;
4483
4484 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4485 offsets from a sym as a branch target? In fact, we could
4486 probably assume the addend is always zero. */
4487 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4488
4489 if (h)
4490 {
4491 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4492 stub_name = bfd_malloc (len);
4493 if (stub_name == NULL)
4494 return stub_name;
4495
4496 len = sprintf (stub_name, "%08x.%s+%x",
4497 input_section->id & 0xffffffff,
4498 h->elf.root.root.string,
4499 (int) rel->r_addend & 0xffffffff);
4500 }
4501 else
4502 {
4503 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4504 stub_name = bfd_malloc (len);
4505 if (stub_name == NULL)
4506 return stub_name;
4507
4508 len = sprintf (stub_name, "%08x.%x:%x+%x",
4509 input_section->id & 0xffffffff,
4510 sym_sec->id & 0xffffffff,
4511 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4512 (int) rel->r_addend & 0xffffffff);
4513 }
4514 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4515 stub_name[len - 2] = 0;
4516 return stub_name;
4517 }
4518
4519 /* Look up an entry in the stub hash. Stub entries are cached because
4520 creating the stub name takes a bit of time. */
4521
4522 static struct ppc_stub_hash_entry *
4523 ppc_get_stub_entry (const asection *input_section,
4524 const asection *sym_sec,
4525 struct ppc_link_hash_entry *h,
4526 const Elf_Internal_Rela *rel,
4527 struct ppc_link_hash_table *htab)
4528 {
4529 struct ppc_stub_hash_entry *stub_entry;
4530 struct map_stub *group;
4531
4532 /* If this input section is part of a group of sections sharing one
4533 stub section, then use the id of the first section in the group.
4534 Stub names need to include a section id, as there may well be
4535 more than one stub used to reach say, printf, and we need to
4536 distinguish between them. */
4537 group = htab->sec_info[input_section->id].u.group;
4538 if (group == NULL)
4539 return NULL;
4540
4541 if (h != NULL && h->u.stub_cache != NULL
4542 && h->u.stub_cache->h == h
4543 && h->u.stub_cache->group == group)
4544 {
4545 stub_entry = h->u.stub_cache;
4546 }
4547 else
4548 {
4549 char *stub_name;
4550
4551 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4552 if (stub_name == NULL)
4553 return NULL;
4554
4555 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4556 stub_name, FALSE, FALSE);
4557 if (h != NULL)
4558 h->u.stub_cache = stub_entry;
4559
4560 free (stub_name);
4561 }
4562
4563 return stub_entry;
4564 }
4565
4566 /* Add a new stub entry to the stub hash. Not all fields of the new
4567 stub entry are initialised. */
4568
4569 static struct ppc_stub_hash_entry *
4570 ppc_add_stub (const char *stub_name,
4571 asection *section,
4572 struct bfd_link_info *info)
4573 {
4574 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4575 struct map_stub *group;
4576 asection *link_sec;
4577 asection *stub_sec;
4578 struct ppc_stub_hash_entry *stub_entry;
4579
4580 group = htab->sec_info[section->id].u.group;
4581 link_sec = group->link_sec;
4582 stub_sec = group->stub_sec;
4583 if (stub_sec == NULL)
4584 {
4585 size_t namelen;
4586 bfd_size_type len;
4587 char *s_name;
4588
4589 namelen = strlen (link_sec->name);
4590 len = namelen + sizeof (STUB_SUFFIX);
4591 s_name = bfd_alloc (htab->params->stub_bfd, len);
4592 if (s_name == NULL)
4593 return NULL;
4594
4595 memcpy (s_name, link_sec->name, namelen);
4596 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4597 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4598 if (stub_sec == NULL)
4599 return NULL;
4600 group->stub_sec = stub_sec;
4601 }
4602
4603 /* Enter this entry into the linker stub hash table. */
4604 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4605 TRUE, FALSE);
4606 if (stub_entry == NULL)
4607 {
4608 /* xgettext:c-format */
4609 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4610 section->owner, stub_name);
4611 return NULL;
4612 }
4613
4614 stub_entry->group = group;
4615 stub_entry->stub_offset = 0;
4616 return stub_entry;
4617 }
4618
4619 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4620 not already done. */
4621
4622 static bfd_boolean
4623 create_got_section (bfd *abfd, struct bfd_link_info *info)
4624 {
4625 asection *got, *relgot;
4626 flagword flags;
4627 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4628
4629 if (!is_ppc64_elf (abfd))
4630 return FALSE;
4631 if (htab == NULL)
4632 return FALSE;
4633
4634 if (!htab->elf.sgot
4635 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4636 return FALSE;
4637
4638 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4639 | SEC_LINKER_CREATED);
4640
4641 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4642 if (!got
4643 || !bfd_set_section_alignment (abfd, got, 3))
4644 return FALSE;
4645
4646 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4647 flags | SEC_READONLY);
4648 if (!relgot
4649 || ! bfd_set_section_alignment (abfd, relgot, 3))
4650 return FALSE;
4651
4652 ppc64_elf_tdata (abfd)->got = got;
4653 ppc64_elf_tdata (abfd)->relgot = relgot;
4654 return TRUE;
4655 }
4656
4657 /* Follow indirect and warning symbol links. */
4658
4659 static inline struct bfd_link_hash_entry *
4660 follow_link (struct bfd_link_hash_entry *h)
4661 {
4662 while (h->type == bfd_link_hash_indirect
4663 || h->type == bfd_link_hash_warning)
4664 h = h->u.i.link;
4665 return h;
4666 }
4667
4668 static inline struct elf_link_hash_entry *
4669 elf_follow_link (struct elf_link_hash_entry *h)
4670 {
4671 return (struct elf_link_hash_entry *) follow_link (&h->root);
4672 }
4673
4674 static inline struct ppc_link_hash_entry *
4675 ppc_follow_link (struct ppc_link_hash_entry *h)
4676 {
4677 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4678 }
4679
4680 /* Merge PLT info on FROM with that on TO. */
4681
4682 static void
4683 move_plt_plist (struct ppc_link_hash_entry *from,
4684 struct ppc_link_hash_entry *to)
4685 {
4686 if (from->elf.plt.plist != NULL)
4687 {
4688 if (to->elf.plt.plist != NULL)
4689 {
4690 struct plt_entry **entp;
4691 struct plt_entry *ent;
4692
4693 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4694 {
4695 struct plt_entry *dent;
4696
4697 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4698 if (dent->addend == ent->addend)
4699 {
4700 dent->plt.refcount += ent->plt.refcount;
4701 *entp = ent->next;
4702 break;
4703 }
4704 if (dent == NULL)
4705 entp = &ent->next;
4706 }
4707 *entp = to->elf.plt.plist;
4708 }
4709
4710 to->elf.plt.plist = from->elf.plt.plist;
4711 from->elf.plt.plist = NULL;
4712 }
4713 }
4714
4715 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4716
4717 static void
4718 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4719 struct elf_link_hash_entry *dir,
4720 struct elf_link_hash_entry *ind)
4721 {
4722 struct ppc_link_hash_entry *edir, *eind;
4723
4724 edir = (struct ppc_link_hash_entry *) dir;
4725 eind = (struct ppc_link_hash_entry *) ind;
4726
4727 edir->is_func |= eind->is_func;
4728 edir->is_func_descriptor |= eind->is_func_descriptor;
4729 edir->tls_mask |= eind->tls_mask;
4730 if (eind->oh != NULL)
4731 edir->oh = ppc_follow_link (eind->oh);
4732
4733 /* If called to transfer flags for a weakdef during processing
4734 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4735 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4736 if (!(ELIMINATE_COPY_RELOCS
4737 && eind->elf.root.type != bfd_link_hash_indirect
4738 && edir->elf.dynamic_adjusted))
4739 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4740
4741 if (edir->elf.versioned != versioned_hidden)
4742 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4743 edir->elf.ref_regular |= eind->elf.ref_regular;
4744 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4745 edir->elf.needs_plt |= eind->elf.needs_plt;
4746 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4747
4748 /* If we were called to copy over info for a weak sym, don't copy
4749 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4750 in order to simplify readonly_dynrelocs and save a field in the
4751 symbol hash entry, but that means dyn_relocs can't be used in any
4752 tests about a specific symbol, or affect other symbol flags which
4753 are then tested.
4754 Chain weakdefs so we can get from the weakdef back to an alias.
4755 The list is circular so that we don't need to use u.weakdef as
4756 well as this list to look at all aliases. */
4757 if (eind->elf.root.type != bfd_link_hash_indirect)
4758 {
4759 struct ppc_link_hash_entry *cur, *add, *next;
4760
4761 add = eind;
4762 do
4763 {
4764 cur = edir->weakref;
4765 if (cur != NULL)
4766 {
4767 do
4768 {
4769 /* We can be called twice for the same symbols.
4770 Don't make multiple loops. */
4771 if (cur == add)
4772 return;
4773 cur = cur->weakref;
4774 } while (cur != edir);
4775 }
4776 next = add->weakref;
4777 if (cur != add)
4778 {
4779 add->weakref = edir->weakref != NULL ? edir->weakref : edir;
4780 edir->weakref = add;
4781 }
4782 add = next;
4783 } while (add != NULL && add != eind);
4784 return;
4785 }
4786
4787 /* Copy over any dynamic relocs we may have on the indirect sym. */
4788 if (eind->dyn_relocs != NULL)
4789 {
4790 if (edir->dyn_relocs != NULL)
4791 {
4792 struct elf_dyn_relocs **pp;
4793 struct elf_dyn_relocs *p;
4794
4795 /* Add reloc counts against the indirect sym to the direct sym
4796 list. Merge any entries against the same section. */
4797 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4798 {
4799 struct elf_dyn_relocs *q;
4800
4801 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4802 if (q->sec == p->sec)
4803 {
4804 q->pc_count += p->pc_count;
4805 q->count += p->count;
4806 *pp = p->next;
4807 break;
4808 }
4809 if (q == NULL)
4810 pp = &p->next;
4811 }
4812 *pp = edir->dyn_relocs;
4813 }
4814
4815 edir->dyn_relocs = eind->dyn_relocs;
4816 eind->dyn_relocs = NULL;
4817 }
4818
4819 /* Copy over got entries that we may have already seen to the
4820 symbol which just became indirect. */
4821 if (eind->elf.got.glist != NULL)
4822 {
4823 if (edir->elf.got.glist != NULL)
4824 {
4825 struct got_entry **entp;
4826 struct got_entry *ent;
4827
4828 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4829 {
4830 struct got_entry *dent;
4831
4832 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4833 if (dent->addend == ent->addend
4834 && dent->owner == ent->owner
4835 && dent->tls_type == ent->tls_type)
4836 {
4837 dent->got.refcount += ent->got.refcount;
4838 *entp = ent->next;
4839 break;
4840 }
4841 if (dent == NULL)
4842 entp = &ent->next;
4843 }
4844 *entp = edir->elf.got.glist;
4845 }
4846
4847 edir->elf.got.glist = eind->elf.got.glist;
4848 eind->elf.got.glist = NULL;
4849 }
4850
4851 /* And plt entries. */
4852 move_plt_plist (eind, edir);
4853
4854 if (eind->elf.dynindx != -1)
4855 {
4856 if (edir->elf.dynindx != -1)
4857 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4858 edir->elf.dynstr_index);
4859 edir->elf.dynindx = eind->elf.dynindx;
4860 edir->elf.dynstr_index = eind->elf.dynstr_index;
4861 eind->elf.dynindx = -1;
4862 eind->elf.dynstr_index = 0;
4863 }
4864 }
4865
4866 /* Find the function descriptor hash entry from the given function code
4867 hash entry FH. Link the entries via their OH fields. */
4868
4869 static struct ppc_link_hash_entry *
4870 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4871 {
4872 struct ppc_link_hash_entry *fdh = fh->oh;
4873
4874 if (fdh == NULL)
4875 {
4876 const char *fd_name = fh->elf.root.root.string + 1;
4877
4878 fdh = (struct ppc_link_hash_entry *)
4879 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4880 if (fdh == NULL)
4881 return fdh;
4882
4883 fdh->is_func_descriptor = 1;
4884 fdh->oh = fh;
4885 fh->is_func = 1;
4886 fh->oh = fdh;
4887 }
4888
4889 fdh = ppc_follow_link (fdh);
4890 fdh->is_func_descriptor = 1;
4891 fdh->oh = fh;
4892 return fdh;
4893 }
4894
4895 /* Make a fake function descriptor sym for the undefined code sym FH. */
4896
4897 static struct ppc_link_hash_entry *
4898 make_fdh (struct bfd_link_info *info,
4899 struct ppc_link_hash_entry *fh)
4900 {
4901 bfd *abfd = fh->elf.root.u.undef.abfd;
4902 struct bfd_link_hash_entry *bh = NULL;
4903 struct ppc_link_hash_entry *fdh;
4904 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4905 ? BSF_WEAK
4906 : BSF_GLOBAL);
4907
4908 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4909 fh->elf.root.root.string + 1,
4910 flags, bfd_und_section_ptr, 0,
4911 NULL, FALSE, FALSE, &bh))
4912 return NULL;
4913
4914 fdh = (struct ppc_link_hash_entry *) bh;
4915 fdh->elf.non_elf = 0;
4916 fdh->fake = 1;
4917 fdh->is_func_descriptor = 1;
4918 fdh->oh = fh;
4919 fh->is_func = 1;
4920 fh->oh = fdh;
4921 return fdh;
4922 }
4923
4924 /* Fix function descriptor symbols defined in .opd sections to be
4925 function type. */
4926
4927 static bfd_boolean
4928 ppc64_elf_add_symbol_hook (bfd *ibfd,
4929 struct bfd_link_info *info,
4930 Elf_Internal_Sym *isym,
4931 const char **name,
4932 flagword *flags ATTRIBUTE_UNUSED,
4933 asection **sec,
4934 bfd_vma *value)
4935 {
4936 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4937 && (ibfd->flags & DYNAMIC) == 0
4938 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4939 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4940
4941 if (*sec != NULL
4942 && strcmp ((*sec)->name, ".opd") == 0)
4943 {
4944 asection *code_sec;
4945
4946 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4947 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4948 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4949
4950 /* If the symbol is a function defined in .opd, and the function
4951 code is in a discarded group, let it appear to be undefined. */
4952 if (!bfd_link_relocatable (info)
4953 && (*sec)->reloc_count != 0
4954 && opd_entry_value (*sec, *value, &code_sec, NULL,
4955 FALSE) != (bfd_vma) -1
4956 && discarded_section (code_sec))
4957 {
4958 *sec = bfd_und_section_ptr;
4959 isym->st_shndx = SHN_UNDEF;
4960 }
4961 }
4962 else if (*sec != NULL
4963 && strcmp ((*sec)->name, ".toc") == 0
4964 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4965 {
4966 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4967 if (htab != NULL)
4968 htab->params->object_in_toc = 1;
4969 }
4970
4971 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4972 {
4973 if (abiversion (ibfd) == 0)
4974 set_abiversion (ibfd, 2);
4975 else if (abiversion (ibfd) == 1)
4976 {
4977 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4978 " for ABI version 1\n"), name);
4979 bfd_set_error (bfd_error_bad_value);
4980 return FALSE;
4981 }
4982 }
4983
4984 return TRUE;
4985 }
4986
4987 /* Merge non-visibility st_other attributes: local entry point. */
4988
4989 static void
4990 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4991 const Elf_Internal_Sym *isym,
4992 bfd_boolean definition,
4993 bfd_boolean dynamic)
4994 {
4995 if (definition && !dynamic)
4996 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4997 | ELF_ST_VISIBILITY (h->other));
4998 }
4999
5000 /* Hook called on merging a symbol. We use this to clear "fake" since
5001 we now have a real symbol. */
5002
5003 static bfd_boolean
5004 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5005 const Elf_Internal_Sym *isym ATTRIBUTE_UNUSED,
5006 asection **psec ATTRIBUTE_UNUSED,
5007 bfd_boolean newdef ATTRIBUTE_UNUSED,
5008 bfd_boolean olddef ATTRIBUTE_UNUSED,
5009 bfd *oldbfd ATTRIBUTE_UNUSED,
5010 const asection *oldsec ATTRIBUTE_UNUSED)
5011 {
5012 ((struct ppc_link_hash_entry *) h)->fake = 0;
5013 return TRUE;
5014 }
5015
5016 /* This function makes an old ABI object reference to ".bar" cause the
5017 inclusion of a new ABI object archive that defines "bar".
5018 NAME is a symbol defined in an archive. Return a symbol in the hash
5019 table that might be satisfied by the archive symbols. */
5020
5021 static struct elf_link_hash_entry *
5022 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5023 struct bfd_link_info *info,
5024 const char *name)
5025 {
5026 struct elf_link_hash_entry *h;
5027 char *dot_name;
5028 size_t len;
5029
5030 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5031 if (h != NULL
5032 /* Don't return this sym if it is a fake function descriptor
5033 created by add_symbol_adjust. */
5034 && !((struct ppc_link_hash_entry *) h)->fake)
5035 return h;
5036
5037 if (name[0] == '.')
5038 return h;
5039
5040 len = strlen (name);
5041 dot_name = bfd_alloc (abfd, len + 2);
5042 if (dot_name == NULL)
5043 return (struct elf_link_hash_entry *) 0 - 1;
5044 dot_name[0] = '.';
5045 memcpy (dot_name + 1, name, len + 1);
5046 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5047 bfd_release (abfd, dot_name);
5048 return h;
5049 }
5050
5051 /* This function satisfies all old ABI object references to ".bar" if a
5052 new ABI object defines "bar". Well, at least, undefined dot symbols
5053 are made weak. This stops later archive searches from including an
5054 object if we already have a function descriptor definition. It also
5055 prevents the linker complaining about undefined symbols.
5056 We also check and correct mismatched symbol visibility here. The
5057 most restrictive visibility of the function descriptor and the
5058 function entry symbol is used. */
5059
5060 static bfd_boolean
5061 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5062 {
5063 struct ppc_link_hash_table *htab;
5064 struct ppc_link_hash_entry *fdh;
5065
5066 if (eh->elf.root.type == bfd_link_hash_warning)
5067 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5068
5069 if (eh->elf.root.type == bfd_link_hash_indirect)
5070 return TRUE;
5071
5072 if (eh->elf.root.root.string[0] != '.')
5073 abort ();
5074
5075 htab = ppc_hash_table (info);
5076 if (htab == NULL)
5077 return FALSE;
5078
5079 fdh = lookup_fdh (eh, htab);
5080 if (fdh == NULL
5081 && !bfd_link_relocatable (info)
5082 && (eh->elf.root.type == bfd_link_hash_undefined
5083 || eh->elf.root.type == bfd_link_hash_undefweak)
5084 && eh->elf.ref_regular)
5085 {
5086 /* Make an undefined function descriptor sym, in order to
5087 pull in an --as-needed shared lib. Archives are handled
5088 elsewhere. */
5089 fdh = make_fdh (info, eh);
5090 if (fdh == NULL)
5091 return FALSE;
5092 }
5093
5094 if (fdh != NULL)
5095 {
5096 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5097 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5098
5099 /* Make both descriptor and entry symbol have the most
5100 constraining visibility of either symbol. */
5101 if (entry_vis < descr_vis)
5102 fdh->elf.other += entry_vis - descr_vis;
5103 else if (entry_vis > descr_vis)
5104 eh->elf.other += descr_vis - entry_vis;
5105
5106 /* Propagate reference flags from entry symbol to function
5107 descriptor symbol. */
5108 fdh->elf.root.non_ir_ref |= eh->elf.root.non_ir_ref;
5109 fdh->elf.ref_regular |= eh->elf.ref_regular;
5110 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5111
5112 if (!fdh->elf.forced_local
5113 && fdh->elf.dynindx == -1
5114 && fdh->elf.versioned != versioned_hidden
5115 && (bfd_link_dll (info)
5116 || fdh->elf.def_dynamic
5117 || fdh->elf.ref_dynamic)
5118 && (eh->elf.ref_regular
5119 || eh->elf.def_regular))
5120 {
5121 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5122 return FALSE;
5123 }
5124 }
5125
5126 return TRUE;
5127 }
5128
5129 /* Set up opd section info and abiversion for IBFD, and process list
5130 of dot-symbols we made in link_hash_newfunc. */
5131
5132 static bfd_boolean
5133 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5134 {
5135 struct ppc_link_hash_table *htab;
5136 struct ppc_link_hash_entry **p, *eh;
5137 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5138
5139 if (opd != NULL && opd->size != 0)
5140 {
5141 if (abiversion (ibfd) == 0)
5142 set_abiversion (ibfd, 1);
5143 else if (abiversion (ibfd) >= 2)
5144 {
5145 /* xgettext:c-format */
5146 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5147 " version %d\n"),
5148 ibfd, abiversion (ibfd));
5149 bfd_set_error (bfd_error_bad_value);
5150 return FALSE;
5151 }
5152
5153 if ((ibfd->flags & DYNAMIC) == 0
5154 && (opd->flags & SEC_RELOC) != 0
5155 && opd->reloc_count != 0
5156 && !bfd_is_abs_section (opd->output_section))
5157 {
5158 /* Garbage collection needs some extra help with .opd sections.
5159 We don't want to necessarily keep everything referenced by
5160 relocs in .opd, as that would keep all functions. Instead,
5161 if we reference an .opd symbol (a function descriptor), we
5162 want to keep the function code symbol's section. This is
5163 easy for global symbols, but for local syms we need to keep
5164 information about the associated function section. */
5165 bfd_size_type amt;
5166 asection **opd_sym_map;
5167
5168 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5169 opd_sym_map = bfd_zalloc (ibfd, amt);
5170 if (opd_sym_map == NULL)
5171 return FALSE;
5172 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5173 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5174 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5175 }
5176 }
5177
5178 if (!is_ppc64_elf (info->output_bfd))
5179 return TRUE;
5180 htab = ppc_hash_table (info);
5181 if (htab == NULL)
5182 return FALSE;
5183
5184 /* For input files without an explicit abiversion in e_flags
5185 we should have flagged any with symbol st_other bits set
5186 as ELFv1 and above flagged those with .opd as ELFv2.
5187 Set the output abiversion if not yet set, and for any input
5188 still ambiguous, take its abiversion from the output.
5189 Differences in ABI are reported later. */
5190 if (abiversion (info->output_bfd) == 0)
5191 set_abiversion (info->output_bfd, abiversion (ibfd));
5192 else if (abiversion (ibfd) == 0)
5193 set_abiversion (ibfd, abiversion (info->output_bfd));
5194
5195 p = &htab->dot_syms;
5196 while ((eh = *p) != NULL)
5197 {
5198 *p = NULL;
5199 if (&eh->elf == htab->elf.hgot)
5200 ;
5201 else if (htab->elf.hgot == NULL
5202 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5203 htab->elf.hgot = &eh->elf;
5204 else if (abiversion (ibfd) <= 1)
5205 {
5206 htab->need_func_desc_adj = 1;
5207 if (!add_symbol_adjust (eh, info))
5208 return FALSE;
5209 }
5210 p = &eh->u.next_dot_sym;
5211 }
5212 return TRUE;
5213 }
5214
5215 /* Undo hash table changes when an --as-needed input file is determined
5216 not to be needed. */
5217
5218 static bfd_boolean
5219 ppc64_elf_notice_as_needed (bfd *ibfd,
5220 struct bfd_link_info *info,
5221 enum notice_asneeded_action act)
5222 {
5223 if (act == notice_not_needed)
5224 {
5225 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5226
5227 if (htab == NULL)
5228 return FALSE;
5229
5230 htab->dot_syms = NULL;
5231 }
5232 return _bfd_elf_notice_as_needed (ibfd, info, act);
5233 }
5234
5235 /* If --just-symbols against a final linked binary, then assume we need
5236 toc adjusting stubs when calling functions defined there. */
5237
5238 static void
5239 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5240 {
5241 if ((sec->flags & SEC_CODE) != 0
5242 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5243 && is_ppc64_elf (sec->owner))
5244 {
5245 if (abiversion (sec->owner) >= 2
5246 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5247 sec->has_toc_reloc = 1;
5248 }
5249 _bfd_elf_link_just_syms (sec, info);
5250 }
5251
5252 static struct plt_entry **
5253 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5254 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5255 {
5256 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5257 struct plt_entry **local_plt;
5258 unsigned char *local_got_tls_masks;
5259
5260 if (local_got_ents == NULL)
5261 {
5262 bfd_size_type size = symtab_hdr->sh_info;
5263
5264 size *= (sizeof (*local_got_ents)
5265 + sizeof (*local_plt)
5266 + sizeof (*local_got_tls_masks));
5267 local_got_ents = bfd_zalloc (abfd, size);
5268 if (local_got_ents == NULL)
5269 return NULL;
5270 elf_local_got_ents (abfd) = local_got_ents;
5271 }
5272
5273 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5274 {
5275 struct got_entry *ent;
5276
5277 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5278 if (ent->addend == r_addend
5279 && ent->owner == abfd
5280 && ent->tls_type == tls_type)
5281 break;
5282 if (ent == NULL)
5283 {
5284 bfd_size_type amt = sizeof (*ent);
5285 ent = bfd_alloc (abfd, amt);
5286 if (ent == NULL)
5287 return FALSE;
5288 ent->next = local_got_ents[r_symndx];
5289 ent->addend = r_addend;
5290 ent->owner = abfd;
5291 ent->tls_type = tls_type;
5292 ent->is_indirect = FALSE;
5293 ent->got.refcount = 0;
5294 local_got_ents[r_symndx] = ent;
5295 }
5296 ent->got.refcount += 1;
5297 }
5298
5299 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5300 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5301 local_got_tls_masks[r_symndx] |= tls_type;
5302
5303 return local_plt + r_symndx;
5304 }
5305
5306 static bfd_boolean
5307 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5308 {
5309 struct plt_entry *ent;
5310
5311 for (ent = *plist; ent != NULL; ent = ent->next)
5312 if (ent->addend == addend)
5313 break;
5314 if (ent == NULL)
5315 {
5316 bfd_size_type amt = sizeof (*ent);
5317 ent = bfd_alloc (abfd, amt);
5318 if (ent == NULL)
5319 return FALSE;
5320 ent->next = *plist;
5321 ent->addend = addend;
5322 ent->plt.refcount = 0;
5323 *plist = ent;
5324 }
5325 ent->plt.refcount += 1;
5326 return TRUE;
5327 }
5328
5329 static bfd_boolean
5330 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5331 {
5332 return (r_type == R_PPC64_REL24
5333 || r_type == R_PPC64_REL14
5334 || r_type == R_PPC64_REL14_BRTAKEN
5335 || r_type == R_PPC64_REL14_BRNTAKEN
5336 || r_type == R_PPC64_ADDR24
5337 || r_type == R_PPC64_ADDR14
5338 || r_type == R_PPC64_ADDR14_BRTAKEN
5339 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5340 }
5341
5342 /* Look through the relocs for a section during the first phase, and
5343 calculate needed space in the global offset table, procedure
5344 linkage table, and dynamic reloc sections. */
5345
5346 static bfd_boolean
5347 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5348 asection *sec, const Elf_Internal_Rela *relocs)
5349 {
5350 struct ppc_link_hash_table *htab;
5351 Elf_Internal_Shdr *symtab_hdr;
5352 struct elf_link_hash_entry **sym_hashes;
5353 const Elf_Internal_Rela *rel;
5354 const Elf_Internal_Rela *rel_end;
5355 asection *sreloc;
5356 asection **opd_sym_map;
5357 struct elf_link_hash_entry *tga, *dottga;
5358
5359 if (bfd_link_relocatable (info))
5360 return TRUE;
5361
5362 /* Don't do anything special with non-loaded, non-alloced sections.
5363 In particular, any relocs in such sections should not affect GOT
5364 and PLT reference counting (ie. we don't allow them to create GOT
5365 or PLT entries), there's no possibility or desire to optimize TLS
5366 relocs, and there's not much point in propagating relocs to shared
5367 libs that the dynamic linker won't relocate. */
5368 if ((sec->flags & SEC_ALLOC) == 0)
5369 return TRUE;
5370
5371 BFD_ASSERT (is_ppc64_elf (abfd));
5372
5373 htab = ppc_hash_table (info);
5374 if (htab == NULL)
5375 return FALSE;
5376
5377 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5378 FALSE, FALSE, TRUE);
5379 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5380 FALSE, FALSE, TRUE);
5381 symtab_hdr = &elf_symtab_hdr (abfd);
5382 sym_hashes = elf_sym_hashes (abfd);
5383 sreloc = NULL;
5384 opd_sym_map = NULL;
5385 if (ppc64_elf_section_data (sec) != NULL
5386 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
5387 opd_sym_map = ppc64_elf_section_data (sec)->u.opd.func_sec;
5388
5389 rel_end = relocs + sec->reloc_count;
5390 for (rel = relocs; rel < rel_end; rel++)
5391 {
5392 unsigned long r_symndx;
5393 struct elf_link_hash_entry *h;
5394 enum elf_ppc64_reloc_type r_type;
5395 int tls_type;
5396 struct _ppc64_elf_section_data *ppc64_sec;
5397 struct plt_entry **ifunc, **plt_list;
5398
5399 r_symndx = ELF64_R_SYM (rel->r_info);
5400 if (r_symndx < symtab_hdr->sh_info)
5401 h = NULL;
5402 else
5403 {
5404 struct ppc_link_hash_entry *eh;
5405
5406 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5407 h = elf_follow_link (h);
5408 eh = (struct ppc_link_hash_entry *) h;
5409
5410 /* PR15323, ref flags aren't set for references in the same
5411 object. */
5412 h->root.non_ir_ref = 1;
5413 if (eh->is_func && eh->oh != NULL)
5414 eh->oh->elf.root.non_ir_ref = 1;
5415
5416 if (h == htab->elf.hgot)
5417 sec->has_toc_reloc = 1;
5418 }
5419
5420 tls_type = 0;
5421 ifunc = NULL;
5422 if (h != NULL)
5423 {
5424 if (h->type == STT_GNU_IFUNC)
5425 {
5426 h->needs_plt = 1;
5427 ifunc = &h->plt.plist;
5428 }
5429 }
5430 else
5431 {
5432 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5433 abfd, r_symndx);
5434 if (isym == NULL)
5435 return FALSE;
5436
5437 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5438 {
5439 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5440 rel->r_addend, PLT_IFUNC);
5441 if (ifunc == NULL)
5442 return FALSE;
5443 }
5444 }
5445
5446 r_type = ELF64_R_TYPE (rel->r_info);
5447 switch (r_type)
5448 {
5449 case R_PPC64_TLSGD:
5450 case R_PPC64_TLSLD:
5451 /* These special tls relocs tie a call to __tls_get_addr with
5452 its parameter symbol. */
5453 break;
5454
5455 case R_PPC64_GOT_TLSLD16:
5456 case R_PPC64_GOT_TLSLD16_LO:
5457 case R_PPC64_GOT_TLSLD16_HI:
5458 case R_PPC64_GOT_TLSLD16_HA:
5459 tls_type = TLS_TLS | TLS_LD;
5460 goto dogottls;
5461
5462 case R_PPC64_GOT_TLSGD16:
5463 case R_PPC64_GOT_TLSGD16_LO:
5464 case R_PPC64_GOT_TLSGD16_HI:
5465 case R_PPC64_GOT_TLSGD16_HA:
5466 tls_type = TLS_TLS | TLS_GD;
5467 goto dogottls;
5468
5469 case R_PPC64_GOT_TPREL16_DS:
5470 case R_PPC64_GOT_TPREL16_LO_DS:
5471 case R_PPC64_GOT_TPREL16_HI:
5472 case R_PPC64_GOT_TPREL16_HA:
5473 if (bfd_link_pic (info))
5474 info->flags |= DF_STATIC_TLS;
5475 tls_type = TLS_TLS | TLS_TPREL;
5476 goto dogottls;
5477
5478 case R_PPC64_GOT_DTPREL16_DS:
5479 case R_PPC64_GOT_DTPREL16_LO_DS:
5480 case R_PPC64_GOT_DTPREL16_HI:
5481 case R_PPC64_GOT_DTPREL16_HA:
5482 tls_type = TLS_TLS | TLS_DTPREL;
5483 dogottls:
5484 sec->has_tls_reloc = 1;
5485 /* Fall through */
5486
5487 case R_PPC64_GOT16:
5488 case R_PPC64_GOT16_DS:
5489 case R_PPC64_GOT16_HA:
5490 case R_PPC64_GOT16_HI:
5491 case R_PPC64_GOT16_LO:
5492 case R_PPC64_GOT16_LO_DS:
5493 /* This symbol requires a global offset table entry. */
5494 sec->has_toc_reloc = 1;
5495 if (r_type == R_PPC64_GOT_TLSLD16
5496 || r_type == R_PPC64_GOT_TLSGD16
5497 || r_type == R_PPC64_GOT_TPREL16_DS
5498 || r_type == R_PPC64_GOT_DTPREL16_DS
5499 || r_type == R_PPC64_GOT16
5500 || r_type == R_PPC64_GOT16_DS)
5501 {
5502 htab->do_multi_toc = 1;
5503 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5504 }
5505
5506 if (ppc64_elf_tdata (abfd)->got == NULL
5507 && !create_got_section (abfd, info))
5508 return FALSE;
5509
5510 if (h != NULL)
5511 {
5512 struct ppc_link_hash_entry *eh;
5513 struct got_entry *ent;
5514
5515 eh = (struct ppc_link_hash_entry *) h;
5516 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5517 if (ent->addend == rel->r_addend
5518 && ent->owner == abfd
5519 && ent->tls_type == tls_type)
5520 break;
5521 if (ent == NULL)
5522 {
5523 bfd_size_type amt = sizeof (*ent);
5524 ent = bfd_alloc (abfd, amt);
5525 if (ent == NULL)
5526 return FALSE;
5527 ent->next = eh->elf.got.glist;
5528 ent->addend = rel->r_addend;
5529 ent->owner = abfd;
5530 ent->tls_type = tls_type;
5531 ent->is_indirect = FALSE;
5532 ent->got.refcount = 0;
5533 eh->elf.got.glist = ent;
5534 }
5535 ent->got.refcount += 1;
5536 eh->tls_mask |= tls_type;
5537 }
5538 else
5539 /* This is a global offset table entry for a local symbol. */
5540 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5541 rel->r_addend, tls_type))
5542 return FALSE;
5543
5544 /* We may also need a plt entry if the symbol turns out to be
5545 an ifunc. */
5546 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5547 {
5548 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5549 return FALSE;
5550 }
5551 break;
5552
5553 case R_PPC64_PLT16_HA:
5554 case R_PPC64_PLT16_HI:
5555 case R_PPC64_PLT16_LO:
5556 case R_PPC64_PLT32:
5557 case R_PPC64_PLT64:
5558 /* This symbol requires a procedure linkage table entry. */
5559 plt_list = ifunc;
5560 if (h != NULL)
5561 {
5562 h->needs_plt = 1;
5563 if (h->root.root.string[0] == '.'
5564 && h->root.root.string[1] != '\0')
5565 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5566 plt_list = &h->plt.plist;
5567 }
5568 if (plt_list == NULL)
5569 {
5570 /* It does not make sense to have a procedure linkage
5571 table entry for a non-ifunc local symbol. */
5572 info->callbacks->einfo
5573 /* xgettext:c-format */
5574 (_("%H: %s reloc against local symbol\n"),
5575 abfd, sec, rel->r_offset,
5576 ppc64_elf_howto_table[r_type]->name);
5577 bfd_set_error (bfd_error_bad_value);
5578 return FALSE;
5579 }
5580 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5581 return FALSE;
5582 break;
5583
5584 /* The following relocations don't need to propagate the
5585 relocation if linking a shared object since they are
5586 section relative. */
5587 case R_PPC64_SECTOFF:
5588 case R_PPC64_SECTOFF_LO:
5589 case R_PPC64_SECTOFF_HI:
5590 case R_PPC64_SECTOFF_HA:
5591 case R_PPC64_SECTOFF_DS:
5592 case R_PPC64_SECTOFF_LO_DS:
5593 case R_PPC64_DTPREL16:
5594 case R_PPC64_DTPREL16_LO:
5595 case R_PPC64_DTPREL16_HI:
5596 case R_PPC64_DTPREL16_HA:
5597 case R_PPC64_DTPREL16_DS:
5598 case R_PPC64_DTPREL16_LO_DS:
5599 case R_PPC64_DTPREL16_HIGH:
5600 case R_PPC64_DTPREL16_HIGHA:
5601 case R_PPC64_DTPREL16_HIGHER:
5602 case R_PPC64_DTPREL16_HIGHERA:
5603 case R_PPC64_DTPREL16_HIGHEST:
5604 case R_PPC64_DTPREL16_HIGHESTA:
5605 break;
5606
5607 /* Nor do these. */
5608 case R_PPC64_REL16:
5609 case R_PPC64_REL16_LO:
5610 case R_PPC64_REL16_HI:
5611 case R_PPC64_REL16_HA:
5612 case R_PPC64_REL16DX_HA:
5613 break;
5614
5615 /* Not supported as a dynamic relocation. */
5616 case R_PPC64_ADDR64_LOCAL:
5617 if (bfd_link_pic (info))
5618 {
5619 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5620 ppc_howto_init ();
5621 /* xgettext:c-format */
5622 info->callbacks->einfo (_("%H: %s reloc unsupported "
5623 "in shared libraries and PIEs.\n"),
5624 abfd, sec, rel->r_offset,
5625 ppc64_elf_howto_table[r_type]->name);
5626 bfd_set_error (bfd_error_bad_value);
5627 return FALSE;
5628 }
5629 break;
5630
5631 case R_PPC64_TOC16:
5632 case R_PPC64_TOC16_DS:
5633 htab->do_multi_toc = 1;
5634 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5635 /* Fall through. */
5636 case R_PPC64_TOC16_LO:
5637 case R_PPC64_TOC16_HI:
5638 case R_PPC64_TOC16_HA:
5639 case R_PPC64_TOC16_LO_DS:
5640 sec->has_toc_reloc = 1;
5641 break;
5642
5643 /* Marker reloc. */
5644 case R_PPC64_ENTRY:
5645 break;
5646
5647 /* This relocation describes the C++ object vtable hierarchy.
5648 Reconstruct it for later use during GC. */
5649 case R_PPC64_GNU_VTINHERIT:
5650 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5651 return FALSE;
5652 break;
5653
5654 /* This relocation describes which C++ vtable entries are actually
5655 used. Record for later use during GC. */
5656 case R_PPC64_GNU_VTENTRY:
5657 BFD_ASSERT (h != NULL);
5658 if (h != NULL
5659 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5660 return FALSE;
5661 break;
5662
5663 case R_PPC64_REL14:
5664 case R_PPC64_REL14_BRTAKEN:
5665 case R_PPC64_REL14_BRNTAKEN:
5666 {
5667 asection *dest = NULL;
5668
5669 /* Heuristic: If jumping outside our section, chances are
5670 we are going to need a stub. */
5671 if (h != NULL)
5672 {
5673 /* If the sym is weak it may be overridden later, so
5674 don't assume we know where a weak sym lives. */
5675 if (h->root.type == bfd_link_hash_defined)
5676 dest = h->root.u.def.section;
5677 }
5678 else
5679 {
5680 Elf_Internal_Sym *isym;
5681
5682 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5683 abfd, r_symndx);
5684 if (isym == NULL)
5685 return FALSE;
5686
5687 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5688 }
5689
5690 if (dest != sec)
5691 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5692 }
5693 /* Fall through. */
5694
5695 case R_PPC64_REL24:
5696 plt_list = ifunc;
5697 if (h != NULL)
5698 {
5699 h->needs_plt = 1;
5700 if (h->root.root.string[0] == '.'
5701 && h->root.root.string[1] != '\0')
5702 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5703
5704 if (h == tga || h == dottga)
5705 {
5706 sec->has_tls_reloc = 1;
5707 if (rel != relocs
5708 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5709 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5710 /* We have a new-style __tls_get_addr call with
5711 a marker reloc. */
5712 ;
5713 else
5714 /* Mark this section as having an old-style call. */
5715 sec->has_tls_get_addr_call = 1;
5716 }
5717 plt_list = &h->plt.plist;
5718 }
5719
5720 /* We may need a .plt entry if the function this reloc
5721 refers to is in a shared lib. */
5722 if (plt_list
5723 && !update_plt_info (abfd, plt_list, rel->r_addend))
5724 return FALSE;
5725 break;
5726
5727 case R_PPC64_ADDR14:
5728 case R_PPC64_ADDR14_BRNTAKEN:
5729 case R_PPC64_ADDR14_BRTAKEN:
5730 case R_PPC64_ADDR24:
5731 goto dodyn;
5732
5733 case R_PPC64_TPREL64:
5734 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5735 if (bfd_link_pic (info))
5736 info->flags |= DF_STATIC_TLS;
5737 goto dotlstoc;
5738
5739 case R_PPC64_DTPMOD64:
5740 if (rel + 1 < rel_end
5741 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5742 && rel[1].r_offset == rel->r_offset + 8)
5743 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5744 else
5745 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5746 goto dotlstoc;
5747
5748 case R_PPC64_DTPREL64:
5749 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5750 if (rel != relocs
5751 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5752 && rel[-1].r_offset == rel->r_offset - 8)
5753 /* This is the second reloc of a dtpmod, dtprel pair.
5754 Don't mark with TLS_DTPREL. */
5755 goto dodyn;
5756
5757 dotlstoc:
5758 sec->has_tls_reloc = 1;
5759 if (h != NULL)
5760 {
5761 struct ppc_link_hash_entry *eh;
5762 eh = (struct ppc_link_hash_entry *) h;
5763 eh->tls_mask |= tls_type;
5764 }
5765 else
5766 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5767 rel->r_addend, tls_type))
5768 return FALSE;
5769
5770 ppc64_sec = ppc64_elf_section_data (sec);
5771 if (ppc64_sec->sec_type != sec_toc)
5772 {
5773 bfd_size_type amt;
5774
5775 /* One extra to simplify get_tls_mask. */
5776 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5777 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5778 if (ppc64_sec->u.toc.symndx == NULL)
5779 return FALSE;
5780 amt = sec->size * sizeof (bfd_vma) / 8;
5781 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5782 if (ppc64_sec->u.toc.add == NULL)
5783 return FALSE;
5784 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5785 ppc64_sec->sec_type = sec_toc;
5786 }
5787 BFD_ASSERT (rel->r_offset % 8 == 0);
5788 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5789 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5790
5791 /* Mark the second slot of a GD or LD entry.
5792 -1 to indicate GD and -2 to indicate LD. */
5793 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5794 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5795 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5796 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5797 goto dodyn;
5798
5799 case R_PPC64_TPREL16:
5800 case R_PPC64_TPREL16_LO:
5801 case R_PPC64_TPREL16_HI:
5802 case R_PPC64_TPREL16_HA:
5803 case R_PPC64_TPREL16_DS:
5804 case R_PPC64_TPREL16_LO_DS:
5805 case R_PPC64_TPREL16_HIGH:
5806 case R_PPC64_TPREL16_HIGHA:
5807 case R_PPC64_TPREL16_HIGHER:
5808 case R_PPC64_TPREL16_HIGHERA:
5809 case R_PPC64_TPREL16_HIGHEST:
5810 case R_PPC64_TPREL16_HIGHESTA:
5811 if (bfd_link_pic (info))
5812 {
5813 info->flags |= DF_STATIC_TLS;
5814 goto dodyn;
5815 }
5816 break;
5817
5818 case R_PPC64_ADDR64:
5819 if (opd_sym_map != NULL
5820 && rel + 1 < rel_end
5821 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5822 {
5823 if (h != NULL)
5824 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5825 else
5826 {
5827 asection *s;
5828 Elf_Internal_Sym *isym;
5829
5830 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5831 abfd, r_symndx);
5832 if (isym == NULL)
5833 return FALSE;
5834
5835 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5836 if (s != NULL && s != sec)
5837 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5838 }
5839 }
5840 /* Fall through. */
5841
5842 case R_PPC64_ADDR16:
5843 case R_PPC64_ADDR16_DS:
5844 case R_PPC64_ADDR16_HA:
5845 case R_PPC64_ADDR16_HI:
5846 case R_PPC64_ADDR16_HIGH:
5847 case R_PPC64_ADDR16_HIGHA:
5848 case R_PPC64_ADDR16_HIGHER:
5849 case R_PPC64_ADDR16_HIGHERA:
5850 case R_PPC64_ADDR16_HIGHEST:
5851 case R_PPC64_ADDR16_HIGHESTA:
5852 case R_PPC64_ADDR16_LO:
5853 case R_PPC64_ADDR16_LO_DS:
5854 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5855 && rel->r_addend == 0)
5856 {
5857 /* We may need a .plt entry if this reloc refers to a
5858 function in a shared lib. */
5859 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5860 return FALSE;
5861 h->pointer_equality_needed = 1;
5862 }
5863 /* Fall through. */
5864
5865 case R_PPC64_REL30:
5866 case R_PPC64_REL32:
5867 case R_PPC64_REL64:
5868 case R_PPC64_ADDR32:
5869 case R_PPC64_UADDR16:
5870 case R_PPC64_UADDR32:
5871 case R_PPC64_UADDR64:
5872 case R_PPC64_TOC:
5873 if (h != NULL && !bfd_link_pic (info))
5874 /* We may need a copy reloc. */
5875 h->non_got_ref = 1;
5876
5877 /* Don't propagate .opd relocs. */
5878 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5879 break;
5880
5881 /* If we are creating a shared library, and this is a reloc
5882 against a global symbol, or a non PC relative reloc
5883 against a local symbol, then we need to copy the reloc
5884 into the shared library. However, if we are linking with
5885 -Bsymbolic, we do not need to copy a reloc against a
5886 global symbol which is defined in an object we are
5887 including in the link (i.e., DEF_REGULAR is set). At
5888 this point we have not seen all the input files, so it is
5889 possible that DEF_REGULAR is not set now but will be set
5890 later (it is never cleared). In case of a weak definition,
5891 DEF_REGULAR may be cleared later by a strong definition in
5892 a shared library. We account for that possibility below by
5893 storing information in the dyn_relocs field of the hash
5894 table entry. A similar situation occurs when creating
5895 shared libraries and symbol visibility changes render the
5896 symbol local.
5897
5898 If on the other hand, we are creating an executable, we
5899 may need to keep relocations for symbols satisfied by a
5900 dynamic library if we manage to avoid copy relocs for the
5901 symbol. */
5902 dodyn:
5903 if ((bfd_link_pic (info)
5904 && (must_be_dyn_reloc (info, r_type)
5905 || (h != NULL
5906 && (!SYMBOLIC_BIND (info, h)
5907 || h->root.type == bfd_link_hash_defweak
5908 || !h->def_regular))))
5909 || (ELIMINATE_COPY_RELOCS
5910 && !bfd_link_pic (info)
5911 && h != NULL
5912 && (h->root.type == bfd_link_hash_defweak
5913 || !h->def_regular))
5914 || (!bfd_link_pic (info)
5915 && ifunc != NULL))
5916 {
5917 /* We must copy these reloc types into the output file.
5918 Create a reloc section in dynobj and make room for
5919 this reloc. */
5920 if (sreloc == NULL)
5921 {
5922 sreloc = _bfd_elf_make_dynamic_reloc_section
5923 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5924
5925 if (sreloc == NULL)
5926 return FALSE;
5927 }
5928
5929 /* If this is a global symbol, we count the number of
5930 relocations we need for this symbol. */
5931 if (h != NULL)
5932 {
5933 struct elf_dyn_relocs *p;
5934 struct elf_dyn_relocs **head;
5935
5936 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5937 p = *head;
5938 if (p == NULL || p->sec != sec)
5939 {
5940 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5941 if (p == NULL)
5942 return FALSE;
5943 p->next = *head;
5944 *head = p;
5945 p->sec = sec;
5946 p->count = 0;
5947 p->pc_count = 0;
5948 }
5949 p->count += 1;
5950 if (!must_be_dyn_reloc (info, r_type))
5951 p->pc_count += 1;
5952 }
5953 else
5954 {
5955 /* Track dynamic relocs needed for local syms too.
5956 We really need local syms available to do this
5957 easily. Oh well. */
5958 struct ppc_dyn_relocs *p;
5959 struct ppc_dyn_relocs **head;
5960 bfd_boolean is_ifunc;
5961 asection *s;
5962 void *vpp;
5963 Elf_Internal_Sym *isym;
5964
5965 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5966 abfd, r_symndx);
5967 if (isym == NULL)
5968 return FALSE;
5969
5970 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5971 if (s == NULL)
5972 s = sec;
5973
5974 vpp = &elf_section_data (s)->local_dynrel;
5975 head = (struct ppc_dyn_relocs **) vpp;
5976 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5977 p = *head;
5978 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5979 p = p->next;
5980 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5981 {
5982 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5983 if (p == NULL)
5984 return FALSE;
5985 p->next = *head;
5986 *head = p;
5987 p->sec = sec;
5988 p->ifunc = is_ifunc;
5989 p->count = 0;
5990 }
5991 p->count += 1;
5992 }
5993 }
5994 break;
5995
5996 default:
5997 break;
5998 }
5999 }
6000
6001 return TRUE;
6002 }
6003
6004 /* Merge backend specific data from an object file to the output
6005 object file when linking. */
6006
6007 static bfd_boolean
6008 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6009 {
6010 bfd *obfd = info->output_bfd;
6011 unsigned long iflags, oflags;
6012
6013 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6014 return TRUE;
6015
6016 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6017 return TRUE;
6018
6019 if (!_bfd_generic_verify_endian_match (ibfd, info))
6020 return FALSE;
6021
6022 iflags = elf_elfheader (ibfd)->e_flags;
6023 oflags = elf_elfheader (obfd)->e_flags;
6024
6025 if (iflags & ~EF_PPC64_ABI)
6026 {
6027 _bfd_error_handler
6028 /* xgettext:c-format */
6029 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6030 bfd_set_error (bfd_error_bad_value);
6031 return FALSE;
6032 }
6033 else if (iflags != oflags && iflags != 0)
6034 {
6035 _bfd_error_handler
6036 /* xgettext:c-format */
6037 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6038 ibfd, iflags, oflags);
6039 bfd_set_error (bfd_error_bad_value);
6040 return FALSE;
6041 }
6042
6043 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6044
6045 /* Merge Tag_compatibility attributes and any common GNU ones. */
6046 _bfd_elf_merge_object_attributes (ibfd, info);
6047
6048 return TRUE;
6049 }
6050
6051 static bfd_boolean
6052 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6053 {
6054 /* Print normal ELF private data. */
6055 _bfd_elf_print_private_bfd_data (abfd, ptr);
6056
6057 if (elf_elfheader (abfd)->e_flags != 0)
6058 {
6059 FILE *file = ptr;
6060
6061 fprintf (file, _("private flags = 0x%lx:"),
6062 elf_elfheader (abfd)->e_flags);
6063
6064 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6065 fprintf (file, _(" [abiv%ld]"),
6066 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6067 fputc ('\n', file);
6068 }
6069
6070 return TRUE;
6071 }
6072
6073 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6074 of the code entry point, and its section, which must be in the same
6075 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6076
6077 static bfd_vma
6078 opd_entry_value (asection *opd_sec,
6079 bfd_vma offset,
6080 asection **code_sec,
6081 bfd_vma *code_off,
6082 bfd_boolean in_code_sec)
6083 {
6084 bfd *opd_bfd = opd_sec->owner;
6085 Elf_Internal_Rela *relocs;
6086 Elf_Internal_Rela *lo, *hi, *look;
6087 bfd_vma val;
6088
6089 /* No relocs implies we are linking a --just-symbols object, or looking
6090 at a final linked executable with addr2line or somesuch. */
6091 if (opd_sec->reloc_count == 0)
6092 {
6093 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6094
6095 if (contents == NULL)
6096 {
6097 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6098 return (bfd_vma) -1;
6099 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6100 }
6101
6102 /* PR 17512: file: 64b9dfbb. */
6103 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6104 return (bfd_vma) -1;
6105
6106 val = bfd_get_64 (opd_bfd, contents + offset);
6107 if (code_sec != NULL)
6108 {
6109 asection *sec, *likely = NULL;
6110
6111 if (in_code_sec)
6112 {
6113 sec = *code_sec;
6114 if (sec->vma <= val
6115 && val < sec->vma + sec->size)
6116 likely = sec;
6117 else
6118 val = -1;
6119 }
6120 else
6121 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6122 if (sec->vma <= val
6123 && (sec->flags & SEC_LOAD) != 0
6124 && (sec->flags & SEC_ALLOC) != 0)
6125 likely = sec;
6126 if (likely != NULL)
6127 {
6128 *code_sec = likely;
6129 if (code_off != NULL)
6130 *code_off = val - likely->vma;
6131 }
6132 }
6133 return val;
6134 }
6135
6136 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6137
6138 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6139 if (relocs == NULL)
6140 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6141 /* PR 17512: file: df8e1fd6. */
6142 if (relocs == NULL)
6143 return (bfd_vma) -1;
6144
6145 /* Go find the opd reloc at the sym address. */
6146 lo = relocs;
6147 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6148 val = (bfd_vma) -1;
6149 while (lo < hi)
6150 {
6151 look = lo + (hi - lo) / 2;
6152 if (look->r_offset < offset)
6153 lo = look + 1;
6154 else if (look->r_offset > offset)
6155 hi = look;
6156 else
6157 {
6158 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6159
6160 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6161 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6162 {
6163 unsigned long symndx = ELF64_R_SYM (look->r_info);
6164 asection *sec = NULL;
6165
6166 if (symndx >= symtab_hdr->sh_info
6167 && elf_sym_hashes (opd_bfd) != NULL)
6168 {
6169 struct elf_link_hash_entry **sym_hashes;
6170 struct elf_link_hash_entry *rh;
6171
6172 sym_hashes = elf_sym_hashes (opd_bfd);
6173 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6174 if (rh != NULL)
6175 {
6176 rh = elf_follow_link (rh);
6177 if (rh->root.type != bfd_link_hash_defined
6178 && rh->root.type != bfd_link_hash_defweak)
6179 break;
6180 if (rh->root.u.def.section->owner == opd_bfd)
6181 {
6182 val = rh->root.u.def.value;
6183 sec = rh->root.u.def.section;
6184 }
6185 }
6186 }
6187
6188 if (sec == NULL)
6189 {
6190 Elf_Internal_Sym *sym;
6191
6192 if (symndx < symtab_hdr->sh_info)
6193 {
6194 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6195 if (sym == NULL)
6196 {
6197 size_t symcnt = symtab_hdr->sh_info;
6198 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6199 symcnt, 0,
6200 NULL, NULL, NULL);
6201 if (sym == NULL)
6202 break;
6203 symtab_hdr->contents = (bfd_byte *) sym;
6204 }
6205 sym += symndx;
6206 }
6207 else
6208 {
6209 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6210 1, symndx,
6211 NULL, NULL, NULL);
6212 if (sym == NULL)
6213 break;
6214 }
6215 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6216 if (sec == NULL)
6217 break;
6218 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6219 val = sym->st_value;
6220 }
6221
6222 val += look->r_addend;
6223 if (code_off != NULL)
6224 *code_off = val;
6225 if (code_sec != NULL)
6226 {
6227 if (in_code_sec && *code_sec != sec)
6228 return -1;
6229 else
6230 *code_sec = sec;
6231 }
6232 if (sec->output_section != NULL)
6233 val += sec->output_section->vma + sec->output_offset;
6234 }
6235 break;
6236 }
6237 }
6238
6239 return val;
6240 }
6241
6242 /* If the ELF symbol SYM might be a function in SEC, return the
6243 function size and set *CODE_OFF to the function's entry point,
6244 otherwise return zero. */
6245
6246 static bfd_size_type
6247 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6248 bfd_vma *code_off)
6249 {
6250 bfd_size_type size;
6251
6252 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6253 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6254 return 0;
6255
6256 size = 0;
6257 if (!(sym->flags & BSF_SYNTHETIC))
6258 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6259
6260 if (strcmp (sym->section->name, ".opd") == 0)
6261 {
6262 struct _opd_sec_data *opd = get_opd_info (sym->section);
6263 bfd_vma symval = sym->value;
6264
6265 if (opd != NULL
6266 && opd->adjust != NULL
6267 && elf_section_data (sym->section)->relocs != NULL)
6268 {
6269 /* opd_entry_value will use cached relocs that have been
6270 adjusted, but with raw symbols. That means both local
6271 and global symbols need adjusting. */
6272 long adjust = opd->adjust[OPD_NDX (symval)];
6273 if (adjust == -1)
6274 return 0;
6275 symval += adjust;
6276 }
6277
6278 if (opd_entry_value (sym->section, symval,
6279 &sec, code_off, TRUE) == (bfd_vma) -1)
6280 return 0;
6281 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6282 symbol. This size has nothing to do with the code size of the
6283 function, which is what we're supposed to return, but the
6284 code size isn't available without looking up the dot-sym.
6285 However, doing that would be a waste of time particularly
6286 since elf_find_function will look at the dot-sym anyway.
6287 Now, elf_find_function will keep the largest size of any
6288 function sym found at the code address of interest, so return
6289 1 here to avoid it incorrectly caching a larger function size
6290 for a small function. This does mean we return the wrong
6291 size for a new-ABI function of size 24, but all that does is
6292 disable caching for such functions. */
6293 if (size == 24)
6294 size = 1;
6295 }
6296 else
6297 {
6298 if (sym->section != sec)
6299 return 0;
6300 *code_off = sym->value;
6301 }
6302 if (size == 0)
6303 size = 1;
6304 return size;
6305 }
6306
6307 /* Return true if symbol is defined in a regular object file. */
6308
6309 static bfd_boolean
6310 is_static_defined (struct elf_link_hash_entry *h)
6311 {
6312 return ((h->root.type == bfd_link_hash_defined
6313 || h->root.type == bfd_link_hash_defweak)
6314 && h->root.u.def.section != NULL
6315 && h->root.u.def.section->output_section != NULL);
6316 }
6317
6318 /* If FDH is a function descriptor symbol, return the associated code
6319 entry symbol if it is defined. Return NULL otherwise. */
6320
6321 static struct ppc_link_hash_entry *
6322 defined_code_entry (struct ppc_link_hash_entry *fdh)
6323 {
6324 if (fdh->is_func_descriptor)
6325 {
6326 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6327 if (fh->elf.root.type == bfd_link_hash_defined
6328 || fh->elf.root.type == bfd_link_hash_defweak)
6329 return fh;
6330 }
6331 return NULL;
6332 }
6333
6334 /* If FH is a function code entry symbol, return the associated
6335 function descriptor symbol if it is defined. Return NULL otherwise. */
6336
6337 static struct ppc_link_hash_entry *
6338 defined_func_desc (struct ppc_link_hash_entry *fh)
6339 {
6340 if (fh->oh != NULL
6341 && fh->oh->is_func_descriptor)
6342 {
6343 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6344 if (fdh->elf.root.type == bfd_link_hash_defined
6345 || fdh->elf.root.type == bfd_link_hash_defweak)
6346 return fdh;
6347 }
6348 return NULL;
6349 }
6350
6351 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6352
6353 /* Garbage collect sections, after first dealing with dot-symbols. */
6354
6355 static bfd_boolean
6356 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6357 {
6358 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6359
6360 if (htab != NULL && htab->need_func_desc_adj)
6361 {
6362 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6363 htab->need_func_desc_adj = 0;
6364 }
6365 return bfd_elf_gc_sections (abfd, info);
6366 }
6367
6368 /* Mark all our entry sym sections, both opd and code section. */
6369
6370 static void
6371 ppc64_elf_gc_keep (struct bfd_link_info *info)
6372 {
6373 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6374 struct bfd_sym_chain *sym;
6375
6376 if (htab == NULL)
6377 return;
6378
6379 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6380 {
6381 struct ppc_link_hash_entry *eh, *fh;
6382 asection *sec;
6383
6384 eh = (struct ppc_link_hash_entry *)
6385 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6386 if (eh == NULL)
6387 continue;
6388 if (eh->elf.root.type != bfd_link_hash_defined
6389 && eh->elf.root.type != bfd_link_hash_defweak)
6390 continue;
6391
6392 fh = defined_code_entry (eh);
6393 if (fh != NULL)
6394 {
6395 sec = fh->elf.root.u.def.section;
6396 sec->flags |= SEC_KEEP;
6397 }
6398 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6399 && opd_entry_value (eh->elf.root.u.def.section,
6400 eh->elf.root.u.def.value,
6401 &sec, NULL, FALSE) != (bfd_vma) -1)
6402 sec->flags |= SEC_KEEP;
6403
6404 sec = eh->elf.root.u.def.section;
6405 sec->flags |= SEC_KEEP;
6406 }
6407 }
6408
6409 /* Mark sections containing dynamically referenced symbols. When
6410 building shared libraries, we must assume that any visible symbol is
6411 referenced. */
6412
6413 static bfd_boolean
6414 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6415 {
6416 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6417 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6418 struct ppc_link_hash_entry *fdh;
6419 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6420
6421 /* Dynamic linking info is on the func descriptor sym. */
6422 fdh = defined_func_desc (eh);
6423 if (fdh != NULL)
6424 eh = fdh;
6425
6426 if ((eh->elf.root.type == bfd_link_hash_defined
6427 || eh->elf.root.type == bfd_link_hash_defweak)
6428 && (eh->elf.ref_dynamic
6429 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6430 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6431 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6432 && (!bfd_link_executable (info)
6433 || info->gc_keep_exported
6434 || info->export_dynamic
6435 || (eh->elf.dynamic
6436 && d != NULL
6437 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6438 && (eh->elf.versioned >= versioned
6439 || !bfd_hide_sym_by_version (info->version_info,
6440 eh->elf.root.root.string)))))
6441 {
6442 asection *code_sec;
6443 struct ppc_link_hash_entry *fh;
6444
6445 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6446
6447 /* Function descriptor syms cause the associated
6448 function code sym section to be marked. */
6449 fh = defined_code_entry (eh);
6450 if (fh != NULL)
6451 {
6452 code_sec = fh->elf.root.u.def.section;
6453 code_sec->flags |= SEC_KEEP;
6454 }
6455 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6456 && opd_entry_value (eh->elf.root.u.def.section,
6457 eh->elf.root.u.def.value,
6458 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6459 code_sec->flags |= SEC_KEEP;
6460 }
6461
6462 return TRUE;
6463 }
6464
6465 /* Return the section that should be marked against GC for a given
6466 relocation. */
6467
6468 static asection *
6469 ppc64_elf_gc_mark_hook (asection *sec,
6470 struct bfd_link_info *info,
6471 Elf_Internal_Rela *rel,
6472 struct elf_link_hash_entry *h,
6473 Elf_Internal_Sym *sym)
6474 {
6475 asection *rsec;
6476
6477 /* Syms return NULL if we're marking .opd, so we avoid marking all
6478 function sections, as all functions are referenced in .opd. */
6479 rsec = NULL;
6480 if (get_opd_info (sec) != NULL)
6481 return rsec;
6482
6483 if (h != NULL)
6484 {
6485 enum elf_ppc64_reloc_type r_type;
6486 struct ppc_link_hash_entry *eh, *fh, *fdh;
6487
6488 r_type = ELF64_R_TYPE (rel->r_info);
6489 switch (r_type)
6490 {
6491 case R_PPC64_GNU_VTINHERIT:
6492 case R_PPC64_GNU_VTENTRY:
6493 break;
6494
6495 default:
6496 switch (h->root.type)
6497 {
6498 case bfd_link_hash_defined:
6499 case bfd_link_hash_defweak:
6500 eh = (struct ppc_link_hash_entry *) h;
6501 fdh = defined_func_desc (eh);
6502 if (fdh != NULL)
6503 {
6504 /* -mcall-aixdesc code references the dot-symbol on
6505 a call reloc. Mark the function descriptor too
6506 against garbage collection. */
6507 fdh->elf.mark = 1;
6508 if (fdh->elf.u.weakdef != NULL)
6509 fdh->elf.u.weakdef->mark = 1;
6510 eh = fdh;
6511 }
6512
6513 /* Function descriptor syms cause the associated
6514 function code sym section to be marked. */
6515 fh = defined_code_entry (eh);
6516 if (fh != NULL)
6517 {
6518 /* They also mark their opd section. */
6519 eh->elf.root.u.def.section->gc_mark = 1;
6520
6521 rsec = fh->elf.root.u.def.section;
6522 }
6523 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6524 && opd_entry_value (eh->elf.root.u.def.section,
6525 eh->elf.root.u.def.value,
6526 &rsec, NULL, FALSE) != (bfd_vma) -1)
6527 eh->elf.root.u.def.section->gc_mark = 1;
6528 else
6529 rsec = h->root.u.def.section;
6530 break;
6531
6532 case bfd_link_hash_common:
6533 rsec = h->root.u.c.p->section;
6534 break;
6535
6536 default:
6537 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6538 }
6539 }
6540 }
6541 else
6542 {
6543 struct _opd_sec_data *opd;
6544
6545 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6546 opd = get_opd_info (rsec);
6547 if (opd != NULL && opd->func_sec != NULL)
6548 {
6549 rsec->gc_mark = 1;
6550
6551 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6552 }
6553 }
6554
6555 return rsec;
6556 }
6557
6558 /* Update the .got, .plt. and dynamic reloc reference counts for the
6559 section being removed. */
6560
6561 static bfd_boolean
6562 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6563 asection *sec, const Elf_Internal_Rela *relocs)
6564 {
6565 struct ppc_link_hash_table *htab;
6566 Elf_Internal_Shdr *symtab_hdr;
6567 struct elf_link_hash_entry **sym_hashes;
6568 struct got_entry **local_got_ents;
6569 const Elf_Internal_Rela *rel, *relend;
6570
6571 if (bfd_link_relocatable (info))
6572 return TRUE;
6573
6574 if ((sec->flags & SEC_ALLOC) == 0)
6575 return TRUE;
6576
6577 elf_section_data (sec)->local_dynrel = NULL;
6578
6579 htab = ppc_hash_table (info);
6580 if (htab == NULL)
6581 return FALSE;
6582
6583 symtab_hdr = &elf_symtab_hdr (abfd);
6584 sym_hashes = elf_sym_hashes (abfd);
6585 local_got_ents = elf_local_got_ents (abfd);
6586
6587 relend = relocs + sec->reloc_count;
6588 for (rel = relocs; rel < relend; rel++)
6589 {
6590 unsigned long r_symndx;
6591 enum elf_ppc64_reloc_type r_type;
6592 struct elf_link_hash_entry *h = NULL;
6593 struct plt_entry **plt_list;
6594 unsigned char tls_type = 0;
6595
6596 r_symndx = ELF64_R_SYM (rel->r_info);
6597 r_type = ELF64_R_TYPE (rel->r_info);
6598 if (r_symndx >= symtab_hdr->sh_info)
6599 {
6600 struct ppc_link_hash_entry *eh;
6601 struct elf_dyn_relocs **pp;
6602 struct elf_dyn_relocs *p;
6603
6604 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6605 h = elf_follow_link (h);
6606 eh = (struct ppc_link_hash_entry *) h;
6607
6608 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6609 if (p->sec == sec)
6610 {
6611 /* Everything must go for SEC. */
6612 *pp = p->next;
6613 break;
6614 }
6615 }
6616
6617 switch (r_type)
6618 {
6619 case R_PPC64_GOT_TLSLD16:
6620 case R_PPC64_GOT_TLSLD16_LO:
6621 case R_PPC64_GOT_TLSLD16_HI:
6622 case R_PPC64_GOT_TLSLD16_HA:
6623 tls_type = TLS_TLS | TLS_LD;
6624 goto dogot;
6625
6626 case R_PPC64_GOT_TLSGD16:
6627 case R_PPC64_GOT_TLSGD16_LO:
6628 case R_PPC64_GOT_TLSGD16_HI:
6629 case R_PPC64_GOT_TLSGD16_HA:
6630 tls_type = TLS_TLS | TLS_GD;
6631 goto dogot;
6632
6633 case R_PPC64_GOT_TPREL16_DS:
6634 case R_PPC64_GOT_TPREL16_LO_DS:
6635 case R_PPC64_GOT_TPREL16_HI:
6636 case R_PPC64_GOT_TPREL16_HA:
6637 tls_type = TLS_TLS | TLS_TPREL;
6638 goto dogot;
6639
6640 case R_PPC64_GOT_DTPREL16_DS:
6641 case R_PPC64_GOT_DTPREL16_LO_DS:
6642 case R_PPC64_GOT_DTPREL16_HI:
6643 case R_PPC64_GOT_DTPREL16_HA:
6644 tls_type = TLS_TLS | TLS_DTPREL;
6645 goto dogot;
6646
6647 case R_PPC64_GOT16:
6648 case R_PPC64_GOT16_DS:
6649 case R_PPC64_GOT16_HA:
6650 case R_PPC64_GOT16_HI:
6651 case R_PPC64_GOT16_LO:
6652 case R_PPC64_GOT16_LO_DS:
6653 dogot:
6654 {
6655 struct got_entry *ent;
6656
6657 if (h != NULL)
6658 ent = h->got.glist;
6659 else
6660 ent = local_got_ents[r_symndx];
6661
6662 for (; ent != NULL; ent = ent->next)
6663 if (ent->addend == rel->r_addend
6664 && ent->owner == abfd
6665 && ent->tls_type == tls_type)
6666 break;
6667 if (ent == NULL)
6668 abort ();
6669 if (ent->got.refcount > 0)
6670 ent->got.refcount -= 1;
6671 }
6672 break;
6673
6674 case R_PPC64_PLT16_HA:
6675 case R_PPC64_PLT16_HI:
6676 case R_PPC64_PLT16_LO:
6677 case R_PPC64_PLT32:
6678 case R_PPC64_PLT64:
6679 case R_PPC64_REL14:
6680 case R_PPC64_REL14_BRNTAKEN:
6681 case R_PPC64_REL14_BRTAKEN:
6682 case R_PPC64_REL24:
6683 plt_list = NULL;
6684 if (h != NULL)
6685 plt_list = &h->plt.plist;
6686 else if (local_got_ents != NULL)
6687 {
6688 struct plt_entry **local_plt = (struct plt_entry **)
6689 (local_got_ents + symtab_hdr->sh_info);
6690 unsigned char *local_got_tls_masks = (unsigned char *)
6691 (local_plt + symtab_hdr->sh_info);
6692 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6693 plt_list = local_plt + r_symndx;
6694 }
6695 if (plt_list)
6696 {
6697 struct plt_entry *ent;
6698
6699 for (ent = *plt_list; ent != NULL; ent = ent->next)
6700 if (ent->addend == rel->r_addend)
6701 break;
6702 if (ent != NULL && ent->plt.refcount > 0)
6703 ent->plt.refcount -= 1;
6704 }
6705 break;
6706
6707 default:
6708 break;
6709 }
6710 }
6711 return TRUE;
6712 }
6713
6714 /* The maximum size of .sfpr. */
6715 #define SFPR_MAX (218*4)
6716
6717 struct sfpr_def_parms
6718 {
6719 const char name[12];
6720 unsigned char lo, hi;
6721 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6722 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6723 };
6724
6725 /* Auto-generate _save*, _rest* functions in .sfpr.
6726 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6727 instead. */
6728
6729 static bfd_boolean
6730 sfpr_define (struct bfd_link_info *info,
6731 const struct sfpr_def_parms *parm,
6732 asection *stub_sec)
6733 {
6734 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6735 unsigned int i;
6736 size_t len = strlen (parm->name);
6737 bfd_boolean writing = FALSE;
6738 char sym[16];
6739
6740 if (htab == NULL)
6741 return FALSE;
6742
6743 memcpy (sym, parm->name, len);
6744 sym[len + 2] = 0;
6745
6746 for (i = parm->lo; i <= parm->hi; i++)
6747 {
6748 struct ppc_link_hash_entry *h;
6749
6750 sym[len + 0] = i / 10 + '0';
6751 sym[len + 1] = i % 10 + '0';
6752 h = (struct ppc_link_hash_entry *)
6753 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6754 if (stub_sec != NULL)
6755 {
6756 if (h != NULL
6757 && h->elf.root.type == bfd_link_hash_defined
6758 && h->elf.root.u.def.section == htab->sfpr)
6759 {
6760 struct elf_link_hash_entry *s;
6761 char buf[32];
6762 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6763 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6764 if (s == NULL)
6765 return FALSE;
6766 if (s->root.type == bfd_link_hash_new
6767 || (s->root.type = bfd_link_hash_defined
6768 && s->root.u.def.section == stub_sec))
6769 {
6770 s->root.type = bfd_link_hash_defined;
6771 s->root.u.def.section = stub_sec;
6772 s->root.u.def.value = (stub_sec->size
6773 + h->elf.root.u.def.value);
6774 s->ref_regular = 1;
6775 s->def_regular = 1;
6776 s->ref_regular_nonweak = 1;
6777 s->forced_local = 1;
6778 s->non_elf = 0;
6779 s->root.linker_def = 1;
6780 }
6781 }
6782 continue;
6783 }
6784 if (h != NULL)
6785 {
6786 h->save_res = 1;
6787 if (!h->elf.def_regular)
6788 {
6789 h->elf.root.type = bfd_link_hash_defined;
6790 h->elf.root.u.def.section = htab->sfpr;
6791 h->elf.root.u.def.value = htab->sfpr->size;
6792 h->elf.type = STT_FUNC;
6793 h->elf.def_regular = 1;
6794 h->elf.non_elf = 0;
6795 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6796 writing = TRUE;
6797 if (htab->sfpr->contents == NULL)
6798 {
6799 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6800 if (htab->sfpr->contents == NULL)
6801 return FALSE;
6802 }
6803 }
6804 }
6805 if (writing)
6806 {
6807 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6808 if (i != parm->hi)
6809 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6810 else
6811 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6812 htab->sfpr->size = p - htab->sfpr->contents;
6813 }
6814 }
6815
6816 return TRUE;
6817 }
6818
6819 static bfd_byte *
6820 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6821 {
6822 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6823 return p + 4;
6824 }
6825
6826 static bfd_byte *
6827 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6828 {
6829 p = savegpr0 (abfd, p, r);
6830 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6831 p = p + 4;
6832 bfd_put_32 (abfd, BLR, p);
6833 return p + 4;
6834 }
6835
6836 static bfd_byte *
6837 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6838 {
6839 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6840 return p + 4;
6841 }
6842
6843 static bfd_byte *
6844 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6845 {
6846 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6847 p = p + 4;
6848 p = restgpr0 (abfd, p, r);
6849 bfd_put_32 (abfd, MTLR_R0, p);
6850 p = p + 4;
6851 if (r == 29)
6852 {
6853 p = restgpr0 (abfd, p, 30);
6854 p = restgpr0 (abfd, p, 31);
6855 }
6856 bfd_put_32 (abfd, BLR, p);
6857 return p + 4;
6858 }
6859
6860 static bfd_byte *
6861 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6862 {
6863 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6864 return p + 4;
6865 }
6866
6867 static bfd_byte *
6868 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6869 {
6870 p = savegpr1 (abfd, p, r);
6871 bfd_put_32 (abfd, BLR, p);
6872 return p + 4;
6873 }
6874
6875 static bfd_byte *
6876 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6877 {
6878 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6879 return p + 4;
6880 }
6881
6882 static bfd_byte *
6883 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6884 {
6885 p = restgpr1 (abfd, p, r);
6886 bfd_put_32 (abfd, BLR, p);
6887 return p + 4;
6888 }
6889
6890 static bfd_byte *
6891 savefpr (bfd *abfd, bfd_byte *p, int r)
6892 {
6893 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6894 return p + 4;
6895 }
6896
6897 static bfd_byte *
6898 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6899 {
6900 p = savefpr (abfd, p, r);
6901 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6902 p = p + 4;
6903 bfd_put_32 (abfd, BLR, p);
6904 return p + 4;
6905 }
6906
6907 static bfd_byte *
6908 restfpr (bfd *abfd, bfd_byte *p, int r)
6909 {
6910 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6911 return p + 4;
6912 }
6913
6914 static bfd_byte *
6915 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6916 {
6917 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6918 p = p + 4;
6919 p = restfpr (abfd, p, r);
6920 bfd_put_32 (abfd, MTLR_R0, p);
6921 p = p + 4;
6922 if (r == 29)
6923 {
6924 p = restfpr (abfd, p, 30);
6925 p = restfpr (abfd, p, 31);
6926 }
6927 bfd_put_32 (abfd, BLR, p);
6928 return p + 4;
6929 }
6930
6931 static bfd_byte *
6932 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6933 {
6934 p = savefpr (abfd, p, r);
6935 bfd_put_32 (abfd, BLR, p);
6936 return p + 4;
6937 }
6938
6939 static bfd_byte *
6940 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6941 {
6942 p = restfpr (abfd, p, r);
6943 bfd_put_32 (abfd, BLR, p);
6944 return p + 4;
6945 }
6946
6947 static bfd_byte *
6948 savevr (bfd *abfd, bfd_byte *p, int r)
6949 {
6950 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6951 p = p + 4;
6952 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6953 return p + 4;
6954 }
6955
6956 static bfd_byte *
6957 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6958 {
6959 p = savevr (abfd, p, r);
6960 bfd_put_32 (abfd, BLR, p);
6961 return p + 4;
6962 }
6963
6964 static bfd_byte *
6965 restvr (bfd *abfd, bfd_byte *p, int r)
6966 {
6967 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6968 p = p + 4;
6969 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6970 return p + 4;
6971 }
6972
6973 static bfd_byte *
6974 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6975 {
6976 p = restvr (abfd, p, r);
6977 bfd_put_32 (abfd, BLR, p);
6978 return p + 4;
6979 }
6980
6981 /* Called via elf_link_hash_traverse to transfer dynamic linking
6982 information on function code symbol entries to their corresponding
6983 function descriptor symbol entries. */
6984
6985 static bfd_boolean
6986 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6987 {
6988 struct bfd_link_info *info;
6989 struct ppc_link_hash_table *htab;
6990 struct ppc_link_hash_entry *fh;
6991 struct ppc_link_hash_entry *fdh;
6992 bfd_boolean force_local;
6993
6994 fh = (struct ppc_link_hash_entry *) h;
6995 if (fh->elf.root.type == bfd_link_hash_indirect)
6996 return TRUE;
6997
6998 if (!fh->is_func)
6999 return TRUE;
7000
7001 if (fh->elf.root.root.string[0] != '.'
7002 || fh->elf.root.root.string[1] == '\0')
7003 return TRUE;
7004
7005 info = inf;
7006 htab = ppc_hash_table (info);
7007 if (htab == NULL)
7008 return FALSE;
7009
7010 /* Find the corresponding function descriptor symbol. */
7011 fdh = lookup_fdh (fh, htab);
7012
7013 /* Resolve undefined references to dot-symbols as the value
7014 in the function descriptor, if we have one in a regular object.
7015 This is to satisfy cases like ".quad .foo". Calls to functions
7016 in dynamic objects are handled elsewhere. */
7017 if ((fh->elf.root.type == bfd_link_hash_undefined
7018 || fh->elf.root.type == bfd_link_hash_undefweak)
7019 && (fdh->elf.root.type == bfd_link_hash_defined
7020 || fdh->elf.root.type == bfd_link_hash_defweak)
7021 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7022 && opd_entry_value (fdh->elf.root.u.def.section,
7023 fdh->elf.root.u.def.value,
7024 &fh->elf.root.u.def.section,
7025 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7026 {
7027 fh->elf.root.type = fdh->elf.root.type;
7028 fh->elf.forced_local = 1;
7029 fh->elf.def_regular = fdh->elf.def_regular;
7030 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7031 }
7032
7033 if (!fh->elf.dynamic)
7034 {
7035 struct plt_entry *ent;
7036
7037 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7038 if (ent->plt.refcount > 0)
7039 break;
7040 if (ent == NULL)
7041 return TRUE;
7042 }
7043
7044 /* Create a descriptor as undefined if necessary. */
7045 if (fdh == NULL
7046 && !bfd_link_executable (info)
7047 && (fh->elf.root.type == bfd_link_hash_undefined
7048 || fh->elf.root.type == bfd_link_hash_undefweak))
7049 {
7050 fdh = make_fdh (info, fh);
7051 if (fdh == NULL)
7052 return FALSE;
7053 }
7054
7055 /* We can't support overriding of symbols on a fake descriptor. */
7056 if (fdh != NULL
7057 && fdh->fake
7058 && (fh->elf.root.type == bfd_link_hash_defined
7059 || fh->elf.root.type == bfd_link_hash_defweak))
7060 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7061
7062 /* Transfer dynamic linking information to the function descriptor. */
7063 if (fdh != NULL)
7064 {
7065 fdh->elf.ref_regular |= fh->elf.ref_regular;
7066 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7067 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7068 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7069 fdh->elf.dynamic |= fh->elf.dynamic;
7070 fdh->elf.needs_plt |= (fh->elf.needs_plt
7071 || fh->elf.type == STT_FUNC
7072 || fh->elf.type == STT_GNU_IFUNC);
7073 move_plt_plist (fh, fdh);
7074
7075 if (!fdh->elf.forced_local
7076 && fh->elf.dynindx != -1)
7077 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7078 return FALSE;
7079 }
7080
7081 /* Now that the info is on the function descriptor, clear the
7082 function code sym info. Any function code syms for which we
7083 don't have a definition in a regular file, we force local.
7084 This prevents a shared library from exporting syms that have
7085 been imported from another library. Function code syms that
7086 are really in the library we must leave global to prevent the
7087 linker dragging in a definition from a static library. */
7088 force_local = (!fh->elf.def_regular
7089 || fdh == NULL
7090 || !fdh->elf.def_regular
7091 || fdh->elf.forced_local);
7092 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7093
7094 return TRUE;
7095 }
7096
7097 static const struct sfpr_def_parms save_res_funcs[] =
7098 {
7099 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7100 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7101 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7102 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7103 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7104 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7105 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7106 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7107 { "._savef", 14, 31, savefpr, savefpr1_tail },
7108 { "._restf", 14, 31, restfpr, restfpr1_tail },
7109 { "_savevr_", 20, 31, savevr, savevr_tail },
7110 { "_restvr_", 20, 31, restvr, restvr_tail }
7111 };
7112
7113 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7114 this hook to a) provide some gcc support functions, and b) transfer
7115 dynamic linking information gathered so far on function code symbol
7116 entries, to their corresponding function descriptor symbol entries. */
7117
7118 static bfd_boolean
7119 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7120 struct bfd_link_info *info)
7121 {
7122 struct ppc_link_hash_table *htab;
7123
7124 htab = ppc_hash_table (info);
7125 if (htab == NULL)
7126 return FALSE;
7127
7128 /* Provide any missing _save* and _rest* functions. */
7129 if (htab->sfpr != NULL)
7130 {
7131 unsigned int i;
7132
7133 htab->sfpr->size = 0;
7134 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7135 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7136 return FALSE;
7137 if (htab->sfpr->size == 0)
7138 htab->sfpr->flags |= SEC_EXCLUDE;
7139 }
7140
7141 if (bfd_link_relocatable (info))
7142 return TRUE;
7143
7144 if (htab->elf.hgot != NULL)
7145 {
7146 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7147 /* Make .TOC. defined so as to prevent it being made dynamic.
7148 The wrong value here is fixed later in ppc64_elf_set_toc. */
7149 if (!htab->elf.hgot->def_regular
7150 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7151 {
7152 htab->elf.hgot->root.type = bfd_link_hash_defined;
7153 htab->elf.hgot->root.u.def.value = 0;
7154 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7155 htab->elf.hgot->def_regular = 1;
7156 htab->elf.hgot->root.linker_def = 1;
7157 }
7158 htab->elf.hgot->type = STT_OBJECT;
7159 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7160 | STV_HIDDEN);
7161 }
7162
7163 if (htab->need_func_desc_adj)
7164 {
7165 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7166 htab->need_func_desc_adj = 0;
7167 }
7168
7169 return TRUE;
7170 }
7171
7172 /* Return true if we have dynamic relocs against H that apply to
7173 read-only sections. */
7174
7175 static bfd_boolean
7176 readonly_dynrelocs (struct elf_link_hash_entry *h)
7177 {
7178 struct ppc_link_hash_entry *eh;
7179 struct elf_dyn_relocs *p;
7180
7181 eh = (struct ppc_link_hash_entry *) h;
7182 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7183 {
7184 asection *s = p->sec->output_section;
7185
7186 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7187 return TRUE;
7188 }
7189 return FALSE;
7190 }
7191
7192 /* Return true if we have dynamic relocs against H or any of its weak
7193 aliases, that apply to read-only sections. */
7194
7195 static bfd_boolean
7196 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7197 {
7198 struct ppc_link_hash_entry *eh;
7199
7200 eh = (struct ppc_link_hash_entry *) h;
7201 do
7202 {
7203 if (readonly_dynrelocs (&eh->elf))
7204 return TRUE;
7205 eh = eh->weakref;
7206 } while (eh != NULL && &eh->elf != h);
7207
7208 return FALSE;
7209 }
7210
7211 /* Return whether EH has pc-relative dynamic relocs. */
7212
7213 static bfd_boolean
7214 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7215 {
7216 struct elf_dyn_relocs *p;
7217
7218 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7219 if (p->pc_count != 0)
7220 return TRUE;
7221 return FALSE;
7222 }
7223
7224 /* Return true if a global entry stub will be created for H. Valid
7225 for ELFv2 before plt entries have been allocated. */
7226
7227 static bfd_boolean
7228 global_entry_stub (struct elf_link_hash_entry *h)
7229 {
7230 struct plt_entry *pent;
7231
7232 if (!h->pointer_equality_needed
7233 || h->def_regular)
7234 return FALSE;
7235
7236 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7237 if (pent->plt.refcount > 0
7238 && pent->addend == 0)
7239 return TRUE;
7240
7241 return FALSE;
7242 }
7243
7244 /* Adjust a symbol defined by a dynamic object and referenced by a
7245 regular object. The current definition is in some section of the
7246 dynamic object, but we're not including those sections. We have to
7247 change the definition to something the rest of the link can
7248 understand. */
7249
7250 static bfd_boolean
7251 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7252 struct elf_link_hash_entry *h)
7253 {
7254 struct ppc_link_hash_table *htab;
7255 asection *s, *srel;
7256
7257 htab = ppc_hash_table (info);
7258 if (htab == NULL)
7259 return FALSE;
7260
7261 /* Deal with function syms. */
7262 if (h->type == STT_FUNC
7263 || h->type == STT_GNU_IFUNC
7264 || h->needs_plt)
7265 {
7266 /* Clear procedure linkage table information for any symbol that
7267 won't need a .plt entry. */
7268 struct plt_entry *ent;
7269 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7270 if (ent->plt.refcount > 0)
7271 break;
7272 if (ent == NULL
7273 || (h->type != STT_GNU_IFUNC
7274 && (SYMBOL_CALLS_LOCAL (info, h)
7275 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
7276 && h->root.type == bfd_link_hash_undefweak)))
7277 || ((struct ppc_link_hash_entry *) h)->save_res)
7278 {
7279 h->plt.plist = NULL;
7280 h->needs_plt = 0;
7281 h->pointer_equality_needed = 0;
7282 }
7283 else if (abiversion (info->output_bfd) >= 2)
7284 {
7285 /* Taking a function's address in a read/write section
7286 doesn't require us to define the function symbol in the
7287 executable on a global entry stub. A dynamic reloc can
7288 be used instead. The reason we prefer a few more dynamic
7289 relocs is that calling via a global entry stub costs a
7290 few more instructions, and pointer_equality_needed causes
7291 extra work in ld.so when resolving these symbols. */
7292 if (global_entry_stub (h)
7293 && !alias_readonly_dynrelocs (h))
7294 {
7295 h->pointer_equality_needed = 0;
7296 /* After adjust_dynamic_symbol, non_got_ref set in
7297 the non-pic case means that dyn_relocs for this
7298 symbol should be discarded. */
7299 h->non_got_ref = 0;
7300 }
7301
7302 /* If making a plt entry, then we don't need copy relocs. */
7303 return TRUE;
7304 }
7305 }
7306 else
7307 h->plt.plist = NULL;
7308
7309 /* If this is a weak symbol, and there is a real definition, the
7310 processor independent code will have arranged for us to see the
7311 real definition first, and we can just use the same value. */
7312 if (h->u.weakdef != NULL)
7313 {
7314 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
7315 || h->u.weakdef->root.type == bfd_link_hash_defweak);
7316 h->root.u.def.section = h->u.weakdef->root.u.def.section;
7317 h->root.u.def.value = h->u.weakdef->root.u.def.value;
7318 if (ELIMINATE_COPY_RELOCS)
7319 h->non_got_ref = h->u.weakdef->non_got_ref;
7320 return TRUE;
7321 }
7322
7323 /* If we are creating a shared library, we must presume that the
7324 only references to the symbol are via the global offset table.
7325 For such cases we need not do anything here; the relocations will
7326 be handled correctly by relocate_section. */
7327 if (bfd_link_pic (info))
7328 return TRUE;
7329
7330 /* If there are no references to this symbol that do not use the
7331 GOT, we don't need to generate a copy reloc. */
7332 if (!h->non_got_ref)
7333 return TRUE;
7334
7335 /* Don't generate a copy reloc for symbols defined in the executable. */
7336 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7337
7338 /* If -z nocopyreloc was given, don't generate them either. */
7339 || info->nocopyreloc
7340
7341 /* If we didn't find any dynamic relocs in read-only sections, then
7342 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7343 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7344
7345 /* Protected variables do not work with .dynbss. The copy in
7346 .dynbss won't be used by the shared library with the protected
7347 definition for the variable. Text relocations are preferable
7348 to an incorrect program. */
7349 || h->protected_def)
7350 {
7351 h->non_got_ref = 0;
7352 return TRUE;
7353 }
7354
7355 if (h->plt.plist != NULL)
7356 {
7357 /* We should never get here, but unfortunately there are versions
7358 of gcc out there that improperly (for this ABI) put initialized
7359 function pointers, vtable refs and suchlike in read-only
7360 sections. Allow them to proceed, but warn that this might
7361 break at runtime. */
7362 info->callbacks->einfo
7363 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7364 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7365 h->root.root.string);
7366 }
7367
7368 /* This is a reference to a symbol defined by a dynamic object which
7369 is not a function. */
7370
7371 /* We must allocate the symbol in our .dynbss section, which will
7372 become part of the .bss section of the executable. There will be
7373 an entry for this symbol in the .dynsym section. The dynamic
7374 object will contain position independent code, so all references
7375 from the dynamic object to this symbol will go through the global
7376 offset table. The dynamic linker will use the .dynsym entry to
7377 determine the address it must put in the global offset table, so
7378 both the dynamic object and the regular object will refer to the
7379 same memory location for the variable. */
7380
7381 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
7382 to copy the initial value out of the dynamic object and into the
7383 runtime process image. We need to remember the offset into the
7384 .rela.bss section we are going to use. */
7385 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7386 {
7387 s = htab->elf.sdynrelro;
7388 srel = htab->elf.sreldynrelro;
7389 }
7390 else
7391 {
7392 s = htab->elf.sdynbss;
7393 srel = htab->elf.srelbss;
7394 }
7395 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7396 {
7397 srel->size += sizeof (Elf64_External_Rela);
7398 h->needs_copy = 1;
7399 }
7400
7401 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7402 }
7403
7404 /* If given a function descriptor symbol, hide both the function code
7405 sym and the descriptor. */
7406 static void
7407 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7408 struct elf_link_hash_entry *h,
7409 bfd_boolean force_local)
7410 {
7411 struct ppc_link_hash_entry *eh;
7412 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7413
7414 eh = (struct ppc_link_hash_entry *) h;
7415 if (eh->is_func_descriptor)
7416 {
7417 struct ppc_link_hash_entry *fh = eh->oh;
7418
7419 if (fh == NULL)
7420 {
7421 const char *p, *q;
7422 struct elf_link_hash_table *htab = elf_hash_table (info);
7423 char save;
7424
7425 /* We aren't supposed to use alloca in BFD because on
7426 systems which do not have alloca the version in libiberty
7427 calls xmalloc, which might cause the program to crash
7428 when it runs out of memory. This function doesn't have a
7429 return status, so there's no way to gracefully return an
7430 error. So cheat. We know that string[-1] can be safely
7431 accessed; It's either a string in an ELF string table,
7432 or allocated in an objalloc structure. */
7433
7434 p = eh->elf.root.root.string - 1;
7435 save = *p;
7436 *(char *) p = '.';
7437 fh = (struct ppc_link_hash_entry *)
7438 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7439 *(char *) p = save;
7440
7441 /* Unfortunately, if it so happens that the string we were
7442 looking for was allocated immediately before this string,
7443 then we overwrote the string terminator. That's the only
7444 reason the lookup should fail. */
7445 if (fh == NULL)
7446 {
7447 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7448 while (q >= eh->elf.root.root.string && *q == *p)
7449 --q, --p;
7450 if (q < eh->elf.root.root.string && *p == '.')
7451 fh = (struct ppc_link_hash_entry *)
7452 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7453 }
7454 if (fh != NULL)
7455 {
7456 eh->oh = fh;
7457 fh->oh = eh;
7458 }
7459 }
7460 if (fh != NULL)
7461 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7462 }
7463 }
7464
7465 static bfd_boolean
7466 get_sym_h (struct elf_link_hash_entry **hp,
7467 Elf_Internal_Sym **symp,
7468 asection **symsecp,
7469 unsigned char **tls_maskp,
7470 Elf_Internal_Sym **locsymsp,
7471 unsigned long r_symndx,
7472 bfd *ibfd)
7473 {
7474 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7475
7476 if (r_symndx >= symtab_hdr->sh_info)
7477 {
7478 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7479 struct elf_link_hash_entry *h;
7480
7481 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7482 h = elf_follow_link (h);
7483
7484 if (hp != NULL)
7485 *hp = h;
7486
7487 if (symp != NULL)
7488 *symp = NULL;
7489
7490 if (symsecp != NULL)
7491 {
7492 asection *symsec = NULL;
7493 if (h->root.type == bfd_link_hash_defined
7494 || h->root.type == bfd_link_hash_defweak)
7495 symsec = h->root.u.def.section;
7496 *symsecp = symsec;
7497 }
7498
7499 if (tls_maskp != NULL)
7500 {
7501 struct ppc_link_hash_entry *eh;
7502
7503 eh = (struct ppc_link_hash_entry *) h;
7504 *tls_maskp = &eh->tls_mask;
7505 }
7506 }
7507 else
7508 {
7509 Elf_Internal_Sym *sym;
7510 Elf_Internal_Sym *locsyms = *locsymsp;
7511
7512 if (locsyms == NULL)
7513 {
7514 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7515 if (locsyms == NULL)
7516 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7517 symtab_hdr->sh_info,
7518 0, NULL, NULL, NULL);
7519 if (locsyms == NULL)
7520 return FALSE;
7521 *locsymsp = locsyms;
7522 }
7523 sym = locsyms + r_symndx;
7524
7525 if (hp != NULL)
7526 *hp = NULL;
7527
7528 if (symp != NULL)
7529 *symp = sym;
7530
7531 if (symsecp != NULL)
7532 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7533
7534 if (tls_maskp != NULL)
7535 {
7536 struct got_entry **lgot_ents;
7537 unsigned char *tls_mask;
7538
7539 tls_mask = NULL;
7540 lgot_ents = elf_local_got_ents (ibfd);
7541 if (lgot_ents != NULL)
7542 {
7543 struct plt_entry **local_plt = (struct plt_entry **)
7544 (lgot_ents + symtab_hdr->sh_info);
7545 unsigned char *lgot_masks = (unsigned char *)
7546 (local_plt + symtab_hdr->sh_info);
7547 tls_mask = &lgot_masks[r_symndx];
7548 }
7549 *tls_maskp = tls_mask;
7550 }
7551 }
7552 return TRUE;
7553 }
7554
7555 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7556 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7557 type suitable for optimization, and 1 otherwise. */
7558
7559 static int
7560 get_tls_mask (unsigned char **tls_maskp,
7561 unsigned long *toc_symndx,
7562 bfd_vma *toc_addend,
7563 Elf_Internal_Sym **locsymsp,
7564 const Elf_Internal_Rela *rel,
7565 bfd *ibfd)
7566 {
7567 unsigned long r_symndx;
7568 int next_r;
7569 struct elf_link_hash_entry *h;
7570 Elf_Internal_Sym *sym;
7571 asection *sec;
7572 bfd_vma off;
7573
7574 r_symndx = ELF64_R_SYM (rel->r_info);
7575 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7576 return 0;
7577
7578 if ((*tls_maskp != NULL && **tls_maskp != 0)
7579 || sec == NULL
7580 || ppc64_elf_section_data (sec) == NULL
7581 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7582 return 1;
7583
7584 /* Look inside a TOC section too. */
7585 if (h != NULL)
7586 {
7587 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7588 off = h->root.u.def.value;
7589 }
7590 else
7591 off = sym->st_value;
7592 off += rel->r_addend;
7593 BFD_ASSERT (off % 8 == 0);
7594 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7595 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7596 if (toc_symndx != NULL)
7597 *toc_symndx = r_symndx;
7598 if (toc_addend != NULL)
7599 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7600 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7601 return 0;
7602 if ((h == NULL || is_static_defined (h))
7603 && (next_r == -1 || next_r == -2))
7604 return 1 - next_r;
7605 return 1;
7606 }
7607
7608 /* Find (or create) an entry in the tocsave hash table. */
7609
7610 static struct tocsave_entry *
7611 tocsave_find (struct ppc_link_hash_table *htab,
7612 enum insert_option insert,
7613 Elf_Internal_Sym **local_syms,
7614 const Elf_Internal_Rela *irela,
7615 bfd *ibfd)
7616 {
7617 unsigned long r_indx;
7618 struct elf_link_hash_entry *h;
7619 Elf_Internal_Sym *sym;
7620 struct tocsave_entry ent, *p;
7621 hashval_t hash;
7622 struct tocsave_entry **slot;
7623
7624 r_indx = ELF64_R_SYM (irela->r_info);
7625 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7626 return NULL;
7627 if (ent.sec == NULL || ent.sec->output_section == NULL)
7628 {
7629 _bfd_error_handler
7630 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7631 return NULL;
7632 }
7633
7634 if (h != NULL)
7635 ent.offset = h->root.u.def.value;
7636 else
7637 ent.offset = sym->st_value;
7638 ent.offset += irela->r_addend;
7639
7640 hash = tocsave_htab_hash (&ent);
7641 slot = ((struct tocsave_entry **)
7642 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7643 if (slot == NULL)
7644 return NULL;
7645
7646 if (*slot == NULL)
7647 {
7648 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7649 if (p == NULL)
7650 return NULL;
7651 *p = ent;
7652 *slot = p;
7653 }
7654 return *slot;
7655 }
7656
7657 /* Adjust all global syms defined in opd sections. In gcc generated
7658 code for the old ABI, these will already have been done. */
7659
7660 static bfd_boolean
7661 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7662 {
7663 struct ppc_link_hash_entry *eh;
7664 asection *sym_sec;
7665 struct _opd_sec_data *opd;
7666
7667 if (h->root.type == bfd_link_hash_indirect)
7668 return TRUE;
7669
7670 if (h->root.type != bfd_link_hash_defined
7671 && h->root.type != bfd_link_hash_defweak)
7672 return TRUE;
7673
7674 eh = (struct ppc_link_hash_entry *) h;
7675 if (eh->adjust_done)
7676 return TRUE;
7677
7678 sym_sec = eh->elf.root.u.def.section;
7679 opd = get_opd_info (sym_sec);
7680 if (opd != NULL && opd->adjust != NULL)
7681 {
7682 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7683 if (adjust == -1)
7684 {
7685 /* This entry has been deleted. */
7686 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7687 if (dsec == NULL)
7688 {
7689 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7690 if (discarded_section (dsec))
7691 {
7692 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7693 break;
7694 }
7695 }
7696 eh->elf.root.u.def.value = 0;
7697 eh->elf.root.u.def.section = dsec;
7698 }
7699 else
7700 eh->elf.root.u.def.value += adjust;
7701 eh->adjust_done = 1;
7702 }
7703 return TRUE;
7704 }
7705
7706 /* Handles decrementing dynamic reloc counts for the reloc specified by
7707 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7708 have already been determined. */
7709
7710 static bfd_boolean
7711 dec_dynrel_count (bfd_vma r_info,
7712 asection *sec,
7713 struct bfd_link_info *info,
7714 Elf_Internal_Sym **local_syms,
7715 struct elf_link_hash_entry *h,
7716 Elf_Internal_Sym *sym)
7717 {
7718 enum elf_ppc64_reloc_type r_type;
7719 asection *sym_sec = NULL;
7720
7721 /* Can this reloc be dynamic? This switch, and later tests here
7722 should be kept in sync with the code in check_relocs. */
7723 r_type = ELF64_R_TYPE (r_info);
7724 switch (r_type)
7725 {
7726 default:
7727 return TRUE;
7728
7729 case R_PPC64_TPREL16:
7730 case R_PPC64_TPREL16_LO:
7731 case R_PPC64_TPREL16_HI:
7732 case R_PPC64_TPREL16_HA:
7733 case R_PPC64_TPREL16_DS:
7734 case R_PPC64_TPREL16_LO_DS:
7735 case R_PPC64_TPREL16_HIGH:
7736 case R_PPC64_TPREL16_HIGHA:
7737 case R_PPC64_TPREL16_HIGHER:
7738 case R_PPC64_TPREL16_HIGHERA:
7739 case R_PPC64_TPREL16_HIGHEST:
7740 case R_PPC64_TPREL16_HIGHESTA:
7741 if (!bfd_link_pic (info))
7742 return TRUE;
7743
7744 case R_PPC64_TPREL64:
7745 case R_PPC64_DTPMOD64:
7746 case R_PPC64_DTPREL64:
7747 case R_PPC64_ADDR64:
7748 case R_PPC64_REL30:
7749 case R_PPC64_REL32:
7750 case R_PPC64_REL64:
7751 case R_PPC64_ADDR14:
7752 case R_PPC64_ADDR14_BRNTAKEN:
7753 case R_PPC64_ADDR14_BRTAKEN:
7754 case R_PPC64_ADDR16:
7755 case R_PPC64_ADDR16_DS:
7756 case R_PPC64_ADDR16_HA:
7757 case R_PPC64_ADDR16_HI:
7758 case R_PPC64_ADDR16_HIGH:
7759 case R_PPC64_ADDR16_HIGHA:
7760 case R_PPC64_ADDR16_HIGHER:
7761 case R_PPC64_ADDR16_HIGHERA:
7762 case R_PPC64_ADDR16_HIGHEST:
7763 case R_PPC64_ADDR16_HIGHESTA:
7764 case R_PPC64_ADDR16_LO:
7765 case R_PPC64_ADDR16_LO_DS:
7766 case R_PPC64_ADDR24:
7767 case R_PPC64_ADDR32:
7768 case R_PPC64_UADDR16:
7769 case R_PPC64_UADDR32:
7770 case R_PPC64_UADDR64:
7771 case R_PPC64_TOC:
7772 break;
7773 }
7774
7775 if (local_syms != NULL)
7776 {
7777 unsigned long r_symndx;
7778 bfd *ibfd = sec->owner;
7779
7780 r_symndx = ELF64_R_SYM (r_info);
7781 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7782 return FALSE;
7783 }
7784
7785 if ((bfd_link_pic (info)
7786 && (must_be_dyn_reloc (info, r_type)
7787 || (h != NULL
7788 && (!SYMBOLIC_BIND (info, h)
7789 || h->root.type == bfd_link_hash_defweak
7790 || !h->def_regular))))
7791 || (ELIMINATE_COPY_RELOCS
7792 && !bfd_link_pic (info)
7793 && h != NULL
7794 && (h->root.type == bfd_link_hash_defweak
7795 || !h->def_regular)))
7796 ;
7797 else
7798 return TRUE;
7799
7800 if (h != NULL)
7801 {
7802 struct elf_dyn_relocs *p;
7803 struct elf_dyn_relocs **pp;
7804 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7805
7806 /* elf_gc_sweep may have already removed all dyn relocs associated
7807 with local syms for a given section. Also, symbol flags are
7808 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7809 report a dynreloc miscount. */
7810 if (*pp == NULL && info->gc_sections)
7811 return TRUE;
7812
7813 while ((p = *pp) != NULL)
7814 {
7815 if (p->sec == sec)
7816 {
7817 if (!must_be_dyn_reloc (info, r_type))
7818 p->pc_count -= 1;
7819 p->count -= 1;
7820 if (p->count == 0)
7821 *pp = p->next;
7822 return TRUE;
7823 }
7824 pp = &p->next;
7825 }
7826 }
7827 else
7828 {
7829 struct ppc_dyn_relocs *p;
7830 struct ppc_dyn_relocs **pp;
7831 void *vpp;
7832 bfd_boolean is_ifunc;
7833
7834 if (local_syms == NULL)
7835 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7836 if (sym_sec == NULL)
7837 sym_sec = sec;
7838
7839 vpp = &elf_section_data (sym_sec)->local_dynrel;
7840 pp = (struct ppc_dyn_relocs **) vpp;
7841
7842 if (*pp == NULL && info->gc_sections)
7843 return TRUE;
7844
7845 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7846 while ((p = *pp) != NULL)
7847 {
7848 if (p->sec == sec && p->ifunc == is_ifunc)
7849 {
7850 p->count -= 1;
7851 if (p->count == 0)
7852 *pp = p->next;
7853 return TRUE;
7854 }
7855 pp = &p->next;
7856 }
7857 }
7858
7859 /* xgettext:c-format */
7860 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7861 sec->owner, sec);
7862 bfd_set_error (bfd_error_bad_value);
7863 return FALSE;
7864 }
7865
7866 /* Remove unused Official Procedure Descriptor entries. Currently we
7867 only remove those associated with functions in discarded link-once
7868 sections, or weakly defined functions that have been overridden. It
7869 would be possible to remove many more entries for statically linked
7870 applications. */
7871
7872 bfd_boolean
7873 ppc64_elf_edit_opd (struct bfd_link_info *info)
7874 {
7875 bfd *ibfd;
7876 bfd_boolean some_edited = FALSE;
7877 asection *need_pad = NULL;
7878 struct ppc_link_hash_table *htab;
7879
7880 htab = ppc_hash_table (info);
7881 if (htab == NULL)
7882 return FALSE;
7883
7884 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7885 {
7886 asection *sec;
7887 Elf_Internal_Rela *relstart, *rel, *relend;
7888 Elf_Internal_Shdr *symtab_hdr;
7889 Elf_Internal_Sym *local_syms;
7890 struct _opd_sec_data *opd;
7891 bfd_boolean need_edit, add_aux_fields, broken;
7892 bfd_size_type cnt_16b = 0;
7893
7894 if (!is_ppc64_elf (ibfd))
7895 continue;
7896
7897 sec = bfd_get_section_by_name (ibfd, ".opd");
7898 if (sec == NULL || sec->size == 0)
7899 continue;
7900
7901 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7902 continue;
7903
7904 if (sec->output_section == bfd_abs_section_ptr)
7905 continue;
7906
7907 /* Look through the section relocs. */
7908 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7909 continue;
7910
7911 local_syms = NULL;
7912 symtab_hdr = &elf_symtab_hdr (ibfd);
7913
7914 /* Read the relocations. */
7915 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7916 info->keep_memory);
7917 if (relstart == NULL)
7918 return FALSE;
7919
7920 /* First run through the relocs to check they are sane, and to
7921 determine whether we need to edit this opd section. */
7922 need_edit = FALSE;
7923 broken = FALSE;
7924 need_pad = sec;
7925 relend = relstart + sec->reloc_count;
7926 for (rel = relstart; rel < relend; )
7927 {
7928 enum elf_ppc64_reloc_type r_type;
7929 unsigned long r_symndx;
7930 asection *sym_sec;
7931 struct elf_link_hash_entry *h;
7932 Elf_Internal_Sym *sym;
7933 bfd_vma offset;
7934
7935 /* .opd contains an array of 16 or 24 byte entries. We're
7936 only interested in the reloc pointing to a function entry
7937 point. */
7938 offset = rel->r_offset;
7939 if (rel + 1 == relend
7940 || rel[1].r_offset != offset + 8)
7941 {
7942 /* If someone messes with .opd alignment then after a
7943 "ld -r" we might have padding in the middle of .opd.
7944 Also, there's nothing to prevent someone putting
7945 something silly in .opd with the assembler. No .opd
7946 optimization for them! */
7947 broken_opd:
7948 _bfd_error_handler
7949 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7950 broken = TRUE;
7951 break;
7952 }
7953
7954 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7955 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7956 {
7957 _bfd_error_handler
7958 /* xgettext:c-format */
7959 (_("%B: unexpected reloc type %u in .opd section"),
7960 ibfd, r_type);
7961 broken = TRUE;
7962 break;
7963 }
7964
7965 r_symndx = ELF64_R_SYM (rel->r_info);
7966 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7967 r_symndx, ibfd))
7968 goto error_ret;
7969
7970 if (sym_sec == NULL || sym_sec->owner == NULL)
7971 {
7972 const char *sym_name;
7973 if (h != NULL)
7974 sym_name = h->root.root.string;
7975 else
7976 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7977 sym_sec);
7978
7979 _bfd_error_handler
7980 /* xgettext:c-format */
7981 (_("%B: undefined sym `%s' in .opd section"),
7982 ibfd, sym_name);
7983 broken = TRUE;
7984 break;
7985 }
7986
7987 /* opd entries are always for functions defined in the
7988 current input bfd. If the symbol isn't defined in the
7989 input bfd, then we won't be using the function in this
7990 bfd; It must be defined in a linkonce section in another
7991 bfd, or is weak. It's also possible that we are
7992 discarding the function due to a linker script /DISCARD/,
7993 which we test for via the output_section. */
7994 if (sym_sec->owner != ibfd
7995 || sym_sec->output_section == bfd_abs_section_ptr)
7996 need_edit = TRUE;
7997
7998 rel += 2;
7999 if (rel + 1 == relend
8000 || (rel + 2 < relend
8001 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8002 ++rel;
8003
8004 if (rel == relend)
8005 {
8006 if (sec->size == offset + 24)
8007 {
8008 need_pad = NULL;
8009 break;
8010 }
8011 if (sec->size == offset + 16)
8012 {
8013 cnt_16b++;
8014 break;
8015 }
8016 goto broken_opd;
8017 }
8018 else if (rel + 1 < relend
8019 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8020 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8021 {
8022 if (rel[0].r_offset == offset + 16)
8023 cnt_16b++;
8024 else if (rel[0].r_offset != offset + 24)
8025 goto broken_opd;
8026 }
8027 else
8028 goto broken_opd;
8029 }
8030
8031 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8032
8033 if (!broken && (need_edit || add_aux_fields))
8034 {
8035 Elf_Internal_Rela *write_rel;
8036 Elf_Internal_Shdr *rel_hdr;
8037 bfd_byte *rptr, *wptr;
8038 bfd_byte *new_contents;
8039 bfd_size_type amt;
8040
8041 new_contents = NULL;
8042 amt = OPD_NDX (sec->size) * sizeof (long);
8043 opd = &ppc64_elf_section_data (sec)->u.opd;
8044 opd->adjust = bfd_zalloc (sec->owner, amt);
8045 if (opd->adjust == NULL)
8046 return FALSE;
8047 ppc64_elf_section_data (sec)->sec_type = sec_opd;
8048
8049 /* This seems a waste of time as input .opd sections are all
8050 zeros as generated by gcc, but I suppose there's no reason
8051 this will always be so. We might start putting something in
8052 the third word of .opd entries. */
8053 if ((sec->flags & SEC_IN_MEMORY) == 0)
8054 {
8055 bfd_byte *loc;
8056 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8057 {
8058 if (loc != NULL)
8059 free (loc);
8060 error_ret:
8061 if (local_syms != NULL
8062 && symtab_hdr->contents != (unsigned char *) local_syms)
8063 free (local_syms);
8064 if (elf_section_data (sec)->relocs != relstart)
8065 free (relstart);
8066 return FALSE;
8067 }
8068 sec->contents = loc;
8069 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8070 }
8071
8072 elf_section_data (sec)->relocs = relstart;
8073
8074 new_contents = sec->contents;
8075 if (add_aux_fields)
8076 {
8077 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8078 if (new_contents == NULL)
8079 return FALSE;
8080 need_pad = NULL;
8081 }
8082 wptr = new_contents;
8083 rptr = sec->contents;
8084 write_rel = relstart;
8085 for (rel = relstart; rel < relend; )
8086 {
8087 unsigned long r_symndx;
8088 asection *sym_sec;
8089 struct elf_link_hash_entry *h;
8090 struct ppc_link_hash_entry *fdh = NULL;
8091 Elf_Internal_Sym *sym;
8092 long opd_ent_size;
8093 Elf_Internal_Rela *next_rel;
8094 bfd_boolean skip;
8095
8096 r_symndx = ELF64_R_SYM (rel->r_info);
8097 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8098 r_symndx, ibfd))
8099 goto error_ret;
8100
8101 next_rel = rel + 2;
8102 if (next_rel + 1 == relend
8103 || (next_rel + 2 < relend
8104 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8105 ++next_rel;
8106
8107 /* See if the .opd entry is full 24 byte or
8108 16 byte (with fd_aux entry overlapped with next
8109 fd_func). */
8110 opd_ent_size = 24;
8111 if (next_rel == relend)
8112 {
8113 if (sec->size == rel->r_offset + 16)
8114 opd_ent_size = 16;
8115 }
8116 else if (next_rel->r_offset == rel->r_offset + 16)
8117 opd_ent_size = 16;
8118
8119 if (h != NULL
8120 && h->root.root.string[0] == '.')
8121 {
8122 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8123 if (fdh != NULL)
8124 {
8125 fdh = ppc_follow_link (fdh);
8126 if (fdh->elf.root.type != bfd_link_hash_defined
8127 && fdh->elf.root.type != bfd_link_hash_defweak)
8128 fdh = NULL;
8129 }
8130 }
8131
8132 skip = (sym_sec->owner != ibfd
8133 || sym_sec->output_section == bfd_abs_section_ptr);
8134 if (skip)
8135 {
8136 if (fdh != NULL && sym_sec->owner == ibfd)
8137 {
8138 /* Arrange for the function descriptor sym
8139 to be dropped. */
8140 fdh->elf.root.u.def.value = 0;
8141 fdh->elf.root.u.def.section = sym_sec;
8142 }
8143 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8144
8145 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8146 rel = next_rel;
8147 else
8148 while (1)
8149 {
8150 if (!dec_dynrel_count (rel->r_info, sec, info,
8151 NULL, h, sym))
8152 goto error_ret;
8153
8154 if (++rel == next_rel)
8155 break;
8156
8157 r_symndx = ELF64_R_SYM (rel->r_info);
8158 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8159 r_symndx, ibfd))
8160 goto error_ret;
8161 }
8162 }
8163 else
8164 {
8165 /* We'll be keeping this opd entry. */
8166 long adjust;
8167
8168 if (fdh != NULL)
8169 {
8170 /* Redefine the function descriptor symbol to
8171 this location in the opd section. It is
8172 necessary to update the value here rather
8173 than using an array of adjustments as we do
8174 for local symbols, because various places
8175 in the generic ELF code use the value
8176 stored in u.def.value. */
8177 fdh->elf.root.u.def.value = wptr - new_contents;
8178 fdh->adjust_done = 1;
8179 }
8180
8181 /* Local syms are a bit tricky. We could
8182 tweak them as they can be cached, but
8183 we'd need to look through the local syms
8184 for the function descriptor sym which we
8185 don't have at the moment. So keep an
8186 array of adjustments. */
8187 adjust = (wptr - new_contents) - (rptr - sec->contents);
8188 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8189
8190 if (wptr != rptr)
8191 memcpy (wptr, rptr, opd_ent_size);
8192 wptr += opd_ent_size;
8193 if (add_aux_fields && opd_ent_size == 16)
8194 {
8195 memset (wptr, '\0', 8);
8196 wptr += 8;
8197 }
8198
8199 /* We need to adjust any reloc offsets to point to the
8200 new opd entries. */
8201 for ( ; rel != next_rel; ++rel)
8202 {
8203 rel->r_offset += adjust;
8204 if (write_rel != rel)
8205 memcpy (write_rel, rel, sizeof (*rel));
8206 ++write_rel;
8207 }
8208 }
8209
8210 rptr += opd_ent_size;
8211 }
8212
8213 sec->size = wptr - new_contents;
8214 sec->reloc_count = write_rel - relstart;
8215 if (add_aux_fields)
8216 {
8217 free (sec->contents);
8218 sec->contents = new_contents;
8219 }
8220
8221 /* Fudge the header size too, as this is used later in
8222 elf_bfd_final_link if we are emitting relocs. */
8223 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8224 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8225 some_edited = TRUE;
8226 }
8227 else if (elf_section_data (sec)->relocs != relstart)
8228 free (relstart);
8229
8230 if (local_syms != NULL
8231 && symtab_hdr->contents != (unsigned char *) local_syms)
8232 {
8233 if (!info->keep_memory)
8234 free (local_syms);
8235 else
8236 symtab_hdr->contents = (unsigned char *) local_syms;
8237 }
8238 }
8239
8240 if (some_edited)
8241 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8242
8243 /* If we are doing a final link and the last .opd entry is just 16 byte
8244 long, add a 8 byte padding after it. */
8245 if (need_pad != NULL && !bfd_link_relocatable (info))
8246 {
8247 bfd_byte *p;
8248
8249 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8250 {
8251 BFD_ASSERT (need_pad->size > 0);
8252
8253 p = bfd_malloc (need_pad->size + 8);
8254 if (p == NULL)
8255 return FALSE;
8256
8257 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8258 p, 0, need_pad->size))
8259 return FALSE;
8260
8261 need_pad->contents = p;
8262 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8263 }
8264 else
8265 {
8266 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8267 if (p == NULL)
8268 return FALSE;
8269
8270 need_pad->contents = p;
8271 }
8272
8273 memset (need_pad->contents + need_pad->size, 0, 8);
8274 need_pad->size += 8;
8275 }
8276
8277 return TRUE;
8278 }
8279
8280 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8281
8282 asection *
8283 ppc64_elf_tls_setup (struct bfd_link_info *info)
8284 {
8285 struct ppc_link_hash_table *htab;
8286
8287 htab = ppc_hash_table (info);
8288 if (htab == NULL)
8289 return NULL;
8290
8291 if (abiversion (info->output_bfd) == 1)
8292 htab->opd_abi = 1;
8293
8294 if (htab->params->no_multi_toc)
8295 htab->do_multi_toc = 0;
8296 else if (!htab->do_multi_toc)
8297 htab->params->no_multi_toc = 1;
8298
8299 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8300 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8301 FALSE, FALSE, TRUE));
8302 /* Move dynamic linking info to the function descriptor sym. */
8303 if (htab->tls_get_addr != NULL)
8304 func_desc_adjust (&htab->tls_get_addr->elf, info);
8305 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8306 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8307 FALSE, FALSE, TRUE));
8308 if (htab->params->tls_get_addr_opt)
8309 {
8310 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8311
8312 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8313 FALSE, FALSE, TRUE);
8314 if (opt != NULL)
8315 func_desc_adjust (opt, info);
8316 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8317 FALSE, FALSE, TRUE);
8318 if (opt_fd != NULL
8319 && (opt_fd->root.type == bfd_link_hash_defined
8320 || opt_fd->root.type == bfd_link_hash_defweak))
8321 {
8322 /* If glibc supports an optimized __tls_get_addr call stub,
8323 signalled by the presence of __tls_get_addr_opt, and we'll
8324 be calling __tls_get_addr via a plt call stub, then
8325 make __tls_get_addr point to __tls_get_addr_opt. */
8326 tga_fd = &htab->tls_get_addr_fd->elf;
8327 if (htab->elf.dynamic_sections_created
8328 && tga_fd != NULL
8329 && (tga_fd->type == STT_FUNC
8330 || tga_fd->needs_plt)
8331 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8332 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
8333 && tga_fd->root.type == bfd_link_hash_undefweak)))
8334 {
8335 struct plt_entry *ent;
8336
8337 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8338 if (ent->plt.refcount > 0)
8339 break;
8340 if (ent != NULL)
8341 {
8342 tga_fd->root.type = bfd_link_hash_indirect;
8343 tga_fd->root.u.i.link = &opt_fd->root;
8344 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8345 opt_fd->mark = 1;
8346 if (opt_fd->dynindx != -1)
8347 {
8348 /* Use __tls_get_addr_opt in dynamic relocations. */
8349 opt_fd->dynindx = -1;
8350 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8351 opt_fd->dynstr_index);
8352 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8353 return NULL;
8354 }
8355 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8356 tga = &htab->tls_get_addr->elf;
8357 if (opt != NULL && tga != NULL)
8358 {
8359 tga->root.type = bfd_link_hash_indirect;
8360 tga->root.u.i.link = &opt->root;
8361 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8362 opt->mark = 1;
8363 _bfd_elf_link_hash_hide_symbol (info, opt,
8364 tga->forced_local);
8365 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8366 }
8367 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8368 htab->tls_get_addr_fd->is_func_descriptor = 1;
8369 if (htab->tls_get_addr != NULL)
8370 {
8371 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8372 htab->tls_get_addr->is_func = 1;
8373 }
8374 }
8375 }
8376 }
8377 else if (htab->params->tls_get_addr_opt < 0)
8378 htab->params->tls_get_addr_opt = 0;
8379 }
8380 return _bfd_elf_tls_setup (info->output_bfd, info);
8381 }
8382
8383 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8384 HASH1 or HASH2. */
8385
8386 static bfd_boolean
8387 branch_reloc_hash_match (const bfd *ibfd,
8388 const Elf_Internal_Rela *rel,
8389 const struct ppc_link_hash_entry *hash1,
8390 const struct ppc_link_hash_entry *hash2)
8391 {
8392 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8393 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8394 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8395
8396 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8397 {
8398 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8399 struct elf_link_hash_entry *h;
8400
8401 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8402 h = elf_follow_link (h);
8403 if (h == &hash1->elf || h == &hash2->elf)
8404 return TRUE;
8405 }
8406 return FALSE;
8407 }
8408
8409 /* Run through all the TLS relocs looking for optimization
8410 opportunities. The linker has been hacked (see ppc64elf.em) to do
8411 a preliminary section layout so that we know the TLS segment
8412 offsets. We can't optimize earlier because some optimizations need
8413 to know the tp offset, and we need to optimize before allocating
8414 dynamic relocations. */
8415
8416 bfd_boolean
8417 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8418 {
8419 bfd *ibfd;
8420 asection *sec;
8421 struct ppc_link_hash_table *htab;
8422 unsigned char *toc_ref;
8423 int pass;
8424
8425 if (!bfd_link_executable (info))
8426 return TRUE;
8427
8428 htab = ppc_hash_table (info);
8429 if (htab == NULL)
8430 return FALSE;
8431
8432 /* Make two passes over the relocs. On the first pass, mark toc
8433 entries involved with tls relocs, and check that tls relocs
8434 involved in setting up a tls_get_addr call are indeed followed by
8435 such a call. If they are not, we can't do any tls optimization.
8436 On the second pass twiddle tls_mask flags to notify
8437 relocate_section that optimization can be done, and adjust got
8438 and plt refcounts. */
8439 toc_ref = NULL;
8440 for (pass = 0; pass < 2; ++pass)
8441 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8442 {
8443 Elf_Internal_Sym *locsyms = NULL;
8444 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8445
8446 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8447 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8448 {
8449 Elf_Internal_Rela *relstart, *rel, *relend;
8450 bfd_boolean found_tls_get_addr_arg = 0;
8451
8452 /* Read the relocations. */
8453 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8454 info->keep_memory);
8455 if (relstart == NULL)
8456 {
8457 free (toc_ref);
8458 return FALSE;
8459 }
8460
8461 relend = relstart + sec->reloc_count;
8462 for (rel = relstart; rel < relend; rel++)
8463 {
8464 enum elf_ppc64_reloc_type r_type;
8465 unsigned long r_symndx;
8466 struct elf_link_hash_entry *h;
8467 Elf_Internal_Sym *sym;
8468 asection *sym_sec;
8469 unsigned char *tls_mask;
8470 unsigned char tls_set, tls_clear, tls_type = 0;
8471 bfd_vma value;
8472 bfd_boolean ok_tprel, is_local;
8473 long toc_ref_index = 0;
8474 int expecting_tls_get_addr = 0;
8475 bfd_boolean ret = FALSE;
8476
8477 r_symndx = ELF64_R_SYM (rel->r_info);
8478 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8479 r_symndx, ibfd))
8480 {
8481 err_free_rel:
8482 if (elf_section_data (sec)->relocs != relstart)
8483 free (relstart);
8484 if (toc_ref != NULL)
8485 free (toc_ref);
8486 if (locsyms != NULL
8487 && (elf_symtab_hdr (ibfd).contents
8488 != (unsigned char *) locsyms))
8489 free (locsyms);
8490 return ret;
8491 }
8492
8493 if (h != NULL)
8494 {
8495 if (h->root.type == bfd_link_hash_defined
8496 || h->root.type == bfd_link_hash_defweak)
8497 value = h->root.u.def.value;
8498 else if (h->root.type == bfd_link_hash_undefweak)
8499 value = 0;
8500 else
8501 {
8502 found_tls_get_addr_arg = 0;
8503 continue;
8504 }
8505 }
8506 else
8507 /* Symbols referenced by TLS relocs must be of type
8508 STT_TLS. So no need for .opd local sym adjust. */
8509 value = sym->st_value;
8510
8511 ok_tprel = FALSE;
8512 is_local = FALSE;
8513 if (h == NULL
8514 || !h->def_dynamic)
8515 {
8516 is_local = TRUE;
8517 if (h != NULL
8518 && h->root.type == bfd_link_hash_undefweak)
8519 ok_tprel = TRUE;
8520 else if (sym_sec != NULL
8521 && sym_sec->output_section != NULL)
8522 {
8523 value += sym_sec->output_offset;
8524 value += sym_sec->output_section->vma;
8525 value -= htab->elf.tls_sec->vma;
8526 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8527 < (bfd_vma) 1 << 32);
8528 }
8529 }
8530
8531 r_type = ELF64_R_TYPE (rel->r_info);
8532 /* If this section has old-style __tls_get_addr calls
8533 without marker relocs, then check that each
8534 __tls_get_addr call reloc is preceded by a reloc
8535 that conceivably belongs to the __tls_get_addr arg
8536 setup insn. If we don't find matching arg setup
8537 relocs, don't do any tls optimization. */
8538 if (pass == 0
8539 && sec->has_tls_get_addr_call
8540 && h != NULL
8541 && (h == &htab->tls_get_addr->elf
8542 || h == &htab->tls_get_addr_fd->elf)
8543 && !found_tls_get_addr_arg
8544 && is_branch_reloc (r_type))
8545 {
8546 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8547 "TLS optimization disabled\n"),
8548 ibfd, sec, rel->r_offset);
8549 ret = TRUE;
8550 goto err_free_rel;
8551 }
8552
8553 found_tls_get_addr_arg = 0;
8554 switch (r_type)
8555 {
8556 case R_PPC64_GOT_TLSLD16:
8557 case R_PPC64_GOT_TLSLD16_LO:
8558 expecting_tls_get_addr = 1;
8559 found_tls_get_addr_arg = 1;
8560 /* Fall through. */
8561
8562 case R_PPC64_GOT_TLSLD16_HI:
8563 case R_PPC64_GOT_TLSLD16_HA:
8564 /* These relocs should never be against a symbol
8565 defined in a shared lib. Leave them alone if
8566 that turns out to be the case. */
8567 if (!is_local)
8568 continue;
8569
8570 /* LD -> LE */
8571 tls_set = 0;
8572 tls_clear = TLS_LD;
8573 tls_type = TLS_TLS | TLS_LD;
8574 break;
8575
8576 case R_PPC64_GOT_TLSGD16:
8577 case R_PPC64_GOT_TLSGD16_LO:
8578 expecting_tls_get_addr = 1;
8579 found_tls_get_addr_arg = 1;
8580 /* Fall through. */
8581
8582 case R_PPC64_GOT_TLSGD16_HI:
8583 case R_PPC64_GOT_TLSGD16_HA:
8584 if (ok_tprel)
8585 /* GD -> LE */
8586 tls_set = 0;
8587 else
8588 /* GD -> IE */
8589 tls_set = TLS_TLS | TLS_TPRELGD;
8590 tls_clear = TLS_GD;
8591 tls_type = TLS_TLS | TLS_GD;
8592 break;
8593
8594 case R_PPC64_GOT_TPREL16_DS:
8595 case R_PPC64_GOT_TPREL16_LO_DS:
8596 case R_PPC64_GOT_TPREL16_HI:
8597 case R_PPC64_GOT_TPREL16_HA:
8598 if (ok_tprel)
8599 {
8600 /* IE -> LE */
8601 tls_set = 0;
8602 tls_clear = TLS_TPREL;
8603 tls_type = TLS_TLS | TLS_TPREL;
8604 break;
8605 }
8606 continue;
8607
8608 case R_PPC64_TLSGD:
8609 case R_PPC64_TLSLD:
8610 found_tls_get_addr_arg = 1;
8611 /* Fall through. */
8612
8613 case R_PPC64_TLS:
8614 case R_PPC64_TOC16:
8615 case R_PPC64_TOC16_LO:
8616 if (sym_sec == NULL || sym_sec != toc)
8617 continue;
8618
8619 /* Mark this toc entry as referenced by a TLS
8620 code sequence. We can do that now in the
8621 case of R_PPC64_TLS, and after checking for
8622 tls_get_addr for the TOC16 relocs. */
8623 if (toc_ref == NULL)
8624 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8625 if (toc_ref == NULL)
8626 goto err_free_rel;
8627
8628 if (h != NULL)
8629 value = h->root.u.def.value;
8630 else
8631 value = sym->st_value;
8632 value += rel->r_addend;
8633 if (value % 8 != 0)
8634 continue;
8635 BFD_ASSERT (value < toc->size
8636 && toc->output_offset % 8 == 0);
8637 toc_ref_index = (value + toc->output_offset) / 8;
8638 if (r_type == R_PPC64_TLS
8639 || r_type == R_PPC64_TLSGD
8640 || r_type == R_PPC64_TLSLD)
8641 {
8642 toc_ref[toc_ref_index] = 1;
8643 continue;
8644 }
8645
8646 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8647 continue;
8648
8649 tls_set = 0;
8650 tls_clear = 0;
8651 expecting_tls_get_addr = 2;
8652 break;
8653
8654 case R_PPC64_TPREL64:
8655 if (pass == 0
8656 || sec != toc
8657 || toc_ref == NULL
8658 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8659 continue;
8660 if (ok_tprel)
8661 {
8662 /* IE -> LE */
8663 tls_set = TLS_EXPLICIT;
8664 tls_clear = TLS_TPREL;
8665 break;
8666 }
8667 continue;
8668
8669 case R_PPC64_DTPMOD64:
8670 if (pass == 0
8671 || sec != toc
8672 || toc_ref == NULL
8673 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8674 continue;
8675 if (rel + 1 < relend
8676 && (rel[1].r_info
8677 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8678 && rel[1].r_offset == rel->r_offset + 8)
8679 {
8680 if (ok_tprel)
8681 /* GD -> LE */
8682 tls_set = TLS_EXPLICIT | TLS_GD;
8683 else
8684 /* GD -> IE */
8685 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8686 tls_clear = TLS_GD;
8687 }
8688 else
8689 {
8690 if (!is_local)
8691 continue;
8692
8693 /* LD -> LE */
8694 tls_set = TLS_EXPLICIT;
8695 tls_clear = TLS_LD;
8696 }
8697 break;
8698
8699 default:
8700 continue;
8701 }
8702
8703 if (pass == 0)
8704 {
8705 if (!expecting_tls_get_addr
8706 || !sec->has_tls_get_addr_call)
8707 continue;
8708
8709 if (rel + 1 < relend
8710 && branch_reloc_hash_match (ibfd, rel + 1,
8711 htab->tls_get_addr,
8712 htab->tls_get_addr_fd))
8713 {
8714 if (expecting_tls_get_addr == 2)
8715 {
8716 /* Check for toc tls entries. */
8717 unsigned char *toc_tls;
8718 int retval;
8719
8720 retval = get_tls_mask (&toc_tls, NULL, NULL,
8721 &locsyms,
8722 rel, ibfd);
8723 if (retval == 0)
8724 goto err_free_rel;
8725 if (toc_tls != NULL)
8726 {
8727 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8728 found_tls_get_addr_arg = 1;
8729 if (retval > 1)
8730 toc_ref[toc_ref_index] = 1;
8731 }
8732 }
8733 continue;
8734 }
8735
8736 if (expecting_tls_get_addr != 1)
8737 continue;
8738
8739 /* Uh oh, we didn't find the expected call. We
8740 could just mark this symbol to exclude it
8741 from tls optimization but it's safer to skip
8742 the entire optimization. */
8743 /* xgettext:c-format */
8744 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8745 "TLS optimization disabled\n"),
8746 ibfd, sec, rel->r_offset);
8747 ret = TRUE;
8748 goto err_free_rel;
8749 }
8750
8751 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8752 {
8753 struct plt_entry *ent;
8754 for (ent = htab->tls_get_addr->elf.plt.plist;
8755 ent != NULL;
8756 ent = ent->next)
8757 if (ent->addend == 0)
8758 {
8759 if (ent->plt.refcount > 0)
8760 {
8761 ent->plt.refcount -= 1;
8762 expecting_tls_get_addr = 0;
8763 }
8764 break;
8765 }
8766 }
8767
8768 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8769 {
8770 struct plt_entry *ent;
8771 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8772 ent != NULL;
8773 ent = ent->next)
8774 if (ent->addend == 0)
8775 {
8776 if (ent->plt.refcount > 0)
8777 ent->plt.refcount -= 1;
8778 break;
8779 }
8780 }
8781
8782 if (tls_clear == 0)
8783 continue;
8784
8785 if ((tls_set & TLS_EXPLICIT) == 0)
8786 {
8787 struct got_entry *ent;
8788
8789 /* Adjust got entry for this reloc. */
8790 if (h != NULL)
8791 ent = h->got.glist;
8792 else
8793 ent = elf_local_got_ents (ibfd)[r_symndx];
8794
8795 for (; ent != NULL; ent = ent->next)
8796 if (ent->addend == rel->r_addend
8797 && ent->owner == ibfd
8798 && ent->tls_type == tls_type)
8799 break;
8800 if (ent == NULL)
8801 abort ();
8802
8803 if (tls_set == 0)
8804 {
8805 /* We managed to get rid of a got entry. */
8806 if (ent->got.refcount > 0)
8807 ent->got.refcount -= 1;
8808 }
8809 }
8810 else
8811 {
8812 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8813 we'll lose one or two dyn relocs. */
8814 if (!dec_dynrel_count (rel->r_info, sec, info,
8815 NULL, h, sym))
8816 return FALSE;
8817
8818 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8819 {
8820 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8821 NULL, h, sym))
8822 return FALSE;
8823 }
8824 }
8825
8826 *tls_mask |= tls_set;
8827 *tls_mask &= ~tls_clear;
8828 }
8829
8830 if (elf_section_data (sec)->relocs != relstart)
8831 free (relstart);
8832 }
8833
8834 if (locsyms != NULL
8835 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8836 {
8837 if (!info->keep_memory)
8838 free (locsyms);
8839 else
8840 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8841 }
8842 }
8843
8844 if (toc_ref != NULL)
8845 free (toc_ref);
8846 return TRUE;
8847 }
8848
8849 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8850 the values of any global symbols in a toc section that has been
8851 edited. Globals in toc sections should be a rarity, so this function
8852 sets a flag if any are found in toc sections other than the one just
8853 edited, so that futher hash table traversals can be avoided. */
8854
8855 struct adjust_toc_info
8856 {
8857 asection *toc;
8858 unsigned long *skip;
8859 bfd_boolean global_toc_syms;
8860 };
8861
8862 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8863
8864 static bfd_boolean
8865 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8866 {
8867 struct ppc_link_hash_entry *eh;
8868 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8869 unsigned long i;
8870
8871 if (h->root.type != bfd_link_hash_defined
8872 && h->root.type != bfd_link_hash_defweak)
8873 return TRUE;
8874
8875 eh = (struct ppc_link_hash_entry *) h;
8876 if (eh->adjust_done)
8877 return TRUE;
8878
8879 if (eh->elf.root.u.def.section == toc_inf->toc)
8880 {
8881 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8882 i = toc_inf->toc->rawsize >> 3;
8883 else
8884 i = eh->elf.root.u.def.value >> 3;
8885
8886 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8887 {
8888 _bfd_error_handler
8889 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8890 do
8891 ++i;
8892 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8893 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8894 }
8895
8896 eh->elf.root.u.def.value -= toc_inf->skip[i];
8897 eh->adjust_done = 1;
8898 }
8899 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8900 toc_inf->global_toc_syms = TRUE;
8901
8902 return TRUE;
8903 }
8904
8905 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8906 on a _LO variety toc/got reloc. */
8907
8908 static bfd_boolean
8909 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8910 {
8911 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8912 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8913 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8914 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8915 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8916 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8917 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8918 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8919 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8920 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8921 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8922 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8923 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8924 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8925 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8926 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8927 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8928 /* Exclude lfqu by testing reloc. If relocs are ever
8929 defined for the reduced D field in psq_lu then those
8930 will need testing too. */
8931 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8932 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8933 && (insn & 1) == 0)
8934 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8935 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8936 /* Exclude stfqu. psq_stu as above for psq_lu. */
8937 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8938 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8939 && (insn & 1) == 0));
8940 }
8941
8942 /* Examine all relocs referencing .toc sections in order to remove
8943 unused .toc entries. */
8944
8945 bfd_boolean
8946 ppc64_elf_edit_toc (struct bfd_link_info *info)
8947 {
8948 bfd *ibfd;
8949 struct adjust_toc_info toc_inf;
8950 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8951
8952 htab->do_toc_opt = 1;
8953 toc_inf.global_toc_syms = TRUE;
8954 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8955 {
8956 asection *toc, *sec;
8957 Elf_Internal_Shdr *symtab_hdr;
8958 Elf_Internal_Sym *local_syms;
8959 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8960 unsigned long *skip, *drop;
8961 unsigned char *used;
8962 unsigned char *keep, last, some_unused;
8963
8964 if (!is_ppc64_elf (ibfd))
8965 continue;
8966
8967 toc = bfd_get_section_by_name (ibfd, ".toc");
8968 if (toc == NULL
8969 || toc->size == 0
8970 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8971 || discarded_section (toc))
8972 continue;
8973
8974 toc_relocs = NULL;
8975 local_syms = NULL;
8976 symtab_hdr = &elf_symtab_hdr (ibfd);
8977
8978 /* Look at sections dropped from the final link. */
8979 skip = NULL;
8980 relstart = NULL;
8981 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8982 {
8983 if (sec->reloc_count == 0
8984 || !discarded_section (sec)
8985 || get_opd_info (sec)
8986 || (sec->flags & SEC_ALLOC) == 0
8987 || (sec->flags & SEC_DEBUGGING) != 0)
8988 continue;
8989
8990 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8991 if (relstart == NULL)
8992 goto error_ret;
8993
8994 /* Run through the relocs to see which toc entries might be
8995 unused. */
8996 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8997 {
8998 enum elf_ppc64_reloc_type r_type;
8999 unsigned long r_symndx;
9000 asection *sym_sec;
9001 struct elf_link_hash_entry *h;
9002 Elf_Internal_Sym *sym;
9003 bfd_vma val;
9004
9005 r_type = ELF64_R_TYPE (rel->r_info);
9006 switch (r_type)
9007 {
9008 default:
9009 continue;
9010
9011 case R_PPC64_TOC16:
9012 case R_PPC64_TOC16_LO:
9013 case R_PPC64_TOC16_HI:
9014 case R_PPC64_TOC16_HA:
9015 case R_PPC64_TOC16_DS:
9016 case R_PPC64_TOC16_LO_DS:
9017 break;
9018 }
9019
9020 r_symndx = ELF64_R_SYM (rel->r_info);
9021 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9022 r_symndx, ibfd))
9023 goto error_ret;
9024
9025 if (sym_sec != toc)
9026 continue;
9027
9028 if (h != NULL)
9029 val = h->root.u.def.value;
9030 else
9031 val = sym->st_value;
9032 val += rel->r_addend;
9033
9034 if (val >= toc->size)
9035 continue;
9036
9037 /* Anything in the toc ought to be aligned to 8 bytes.
9038 If not, don't mark as unused. */
9039 if (val & 7)
9040 continue;
9041
9042 if (skip == NULL)
9043 {
9044 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9045 if (skip == NULL)
9046 goto error_ret;
9047 }
9048
9049 skip[val >> 3] = ref_from_discarded;
9050 }
9051
9052 if (elf_section_data (sec)->relocs != relstart)
9053 free (relstart);
9054 }
9055
9056 /* For largetoc loads of address constants, we can convert
9057 . addis rx,2,addr@got@ha
9058 . ld ry,addr@got@l(rx)
9059 to
9060 . addis rx,2,addr@toc@ha
9061 . addi ry,rx,addr@toc@l
9062 when addr is within 2G of the toc pointer. This then means
9063 that the word storing "addr" in the toc is no longer needed. */
9064
9065 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9066 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9067 && toc->reloc_count != 0)
9068 {
9069 /* Read toc relocs. */
9070 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9071 info->keep_memory);
9072 if (toc_relocs == NULL)
9073 goto error_ret;
9074
9075 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9076 {
9077 enum elf_ppc64_reloc_type r_type;
9078 unsigned long r_symndx;
9079 asection *sym_sec;
9080 struct elf_link_hash_entry *h;
9081 Elf_Internal_Sym *sym;
9082 bfd_vma val, addr;
9083
9084 r_type = ELF64_R_TYPE (rel->r_info);
9085 if (r_type != R_PPC64_ADDR64)
9086 continue;
9087
9088 r_symndx = ELF64_R_SYM (rel->r_info);
9089 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9090 r_symndx, ibfd))
9091 goto error_ret;
9092
9093 if (sym_sec == NULL
9094 || sym_sec->output_section == NULL
9095 || discarded_section (sym_sec))
9096 continue;
9097
9098 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9099 continue;
9100
9101 if (h != NULL)
9102 {
9103 if (h->type == STT_GNU_IFUNC)
9104 continue;
9105 val = h->root.u.def.value;
9106 }
9107 else
9108 {
9109 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9110 continue;
9111 val = sym->st_value;
9112 }
9113 val += rel->r_addend;
9114 val += sym_sec->output_section->vma + sym_sec->output_offset;
9115
9116 /* We don't yet know the exact toc pointer value, but we
9117 know it will be somewhere in the toc section. Don't
9118 optimize if the difference from any possible toc
9119 pointer is outside [ff..f80008000, 7fff7fff]. */
9120 addr = toc->output_section->vma + TOC_BASE_OFF;
9121 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9122 continue;
9123
9124 addr = toc->output_section->vma + toc->output_section->rawsize;
9125 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9126 continue;
9127
9128 if (skip == NULL)
9129 {
9130 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9131 if (skip == NULL)
9132 goto error_ret;
9133 }
9134
9135 skip[rel->r_offset >> 3]
9136 |= can_optimize | ((rel - toc_relocs) << 2);
9137 }
9138 }
9139
9140 if (skip == NULL)
9141 continue;
9142
9143 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9144 if (used == NULL)
9145 {
9146 error_ret:
9147 if (local_syms != NULL
9148 && symtab_hdr->contents != (unsigned char *) local_syms)
9149 free (local_syms);
9150 if (sec != NULL
9151 && relstart != NULL
9152 && elf_section_data (sec)->relocs != relstart)
9153 free (relstart);
9154 if (toc_relocs != NULL
9155 && elf_section_data (toc)->relocs != toc_relocs)
9156 free (toc_relocs);
9157 if (skip != NULL)
9158 free (skip);
9159 return FALSE;
9160 }
9161
9162 /* Now check all kept sections that might reference the toc.
9163 Check the toc itself last. */
9164 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9165 : ibfd->sections);
9166 sec != NULL;
9167 sec = (sec == toc ? NULL
9168 : sec->next == NULL ? toc
9169 : sec->next == toc && toc->next ? toc->next
9170 : sec->next))
9171 {
9172 int repeat;
9173
9174 if (sec->reloc_count == 0
9175 || discarded_section (sec)
9176 || get_opd_info (sec)
9177 || (sec->flags & SEC_ALLOC) == 0
9178 || (sec->flags & SEC_DEBUGGING) != 0)
9179 continue;
9180
9181 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9182 info->keep_memory);
9183 if (relstart == NULL)
9184 {
9185 free (used);
9186 goto error_ret;
9187 }
9188
9189 /* Mark toc entries referenced as used. */
9190 do
9191 {
9192 repeat = 0;
9193 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9194 {
9195 enum elf_ppc64_reloc_type r_type;
9196 unsigned long r_symndx;
9197 asection *sym_sec;
9198 struct elf_link_hash_entry *h;
9199 Elf_Internal_Sym *sym;
9200 bfd_vma val;
9201 enum {no_check, check_lo, check_ha} insn_check;
9202
9203 r_type = ELF64_R_TYPE (rel->r_info);
9204 switch (r_type)
9205 {
9206 default:
9207 insn_check = no_check;
9208 break;
9209
9210 case R_PPC64_GOT_TLSLD16_HA:
9211 case R_PPC64_GOT_TLSGD16_HA:
9212 case R_PPC64_GOT_TPREL16_HA:
9213 case R_PPC64_GOT_DTPREL16_HA:
9214 case R_PPC64_GOT16_HA:
9215 case R_PPC64_TOC16_HA:
9216 insn_check = check_ha;
9217 break;
9218
9219 case R_PPC64_GOT_TLSLD16_LO:
9220 case R_PPC64_GOT_TLSGD16_LO:
9221 case R_PPC64_GOT_TPREL16_LO_DS:
9222 case R_PPC64_GOT_DTPREL16_LO_DS:
9223 case R_PPC64_GOT16_LO:
9224 case R_PPC64_GOT16_LO_DS:
9225 case R_PPC64_TOC16_LO:
9226 case R_PPC64_TOC16_LO_DS:
9227 insn_check = check_lo;
9228 break;
9229 }
9230
9231 if (insn_check != no_check)
9232 {
9233 bfd_vma off = rel->r_offset & ~3;
9234 unsigned char buf[4];
9235 unsigned int insn;
9236
9237 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9238 {
9239 free (used);
9240 goto error_ret;
9241 }
9242 insn = bfd_get_32 (ibfd, buf);
9243 if (insn_check == check_lo
9244 ? !ok_lo_toc_insn (insn, r_type)
9245 : ((insn & ((0x3f << 26) | 0x1f << 16))
9246 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9247 {
9248 char str[12];
9249
9250 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9251 sprintf (str, "%#08x", insn);
9252 info->callbacks->einfo
9253 /* xgettext:c-format */
9254 (_("%H: toc optimization is not supported for"
9255 " %s instruction.\n"),
9256 ibfd, sec, rel->r_offset & ~3, str);
9257 }
9258 }
9259
9260 switch (r_type)
9261 {
9262 case R_PPC64_TOC16:
9263 case R_PPC64_TOC16_LO:
9264 case R_PPC64_TOC16_HI:
9265 case R_PPC64_TOC16_HA:
9266 case R_PPC64_TOC16_DS:
9267 case R_PPC64_TOC16_LO_DS:
9268 /* In case we're taking addresses of toc entries. */
9269 case R_PPC64_ADDR64:
9270 break;
9271
9272 default:
9273 continue;
9274 }
9275
9276 r_symndx = ELF64_R_SYM (rel->r_info);
9277 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9278 r_symndx, ibfd))
9279 {
9280 free (used);
9281 goto error_ret;
9282 }
9283
9284 if (sym_sec != toc)
9285 continue;
9286
9287 if (h != NULL)
9288 val = h->root.u.def.value;
9289 else
9290 val = sym->st_value;
9291 val += rel->r_addend;
9292
9293 if (val >= toc->size)
9294 continue;
9295
9296 if ((skip[val >> 3] & can_optimize) != 0)
9297 {
9298 bfd_vma off;
9299 unsigned char opc;
9300
9301 switch (r_type)
9302 {
9303 case R_PPC64_TOC16_HA:
9304 break;
9305
9306 case R_PPC64_TOC16_LO_DS:
9307 off = rel->r_offset;
9308 off += (bfd_big_endian (ibfd) ? -2 : 3);
9309 if (!bfd_get_section_contents (ibfd, sec, &opc,
9310 off, 1))
9311 {
9312 free (used);
9313 goto error_ret;
9314 }
9315 if ((opc & (0x3f << 2)) == (58u << 2))
9316 break;
9317 /* Fall through. */
9318
9319 default:
9320 /* Wrong sort of reloc, or not a ld. We may
9321 as well clear ref_from_discarded too. */
9322 skip[val >> 3] = 0;
9323 }
9324 }
9325
9326 if (sec != toc)
9327 used[val >> 3] = 1;
9328 /* For the toc section, we only mark as used if this
9329 entry itself isn't unused. */
9330 else if ((used[rel->r_offset >> 3]
9331 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9332 && !used[val >> 3])
9333 {
9334 /* Do all the relocs again, to catch reference
9335 chains. */
9336 repeat = 1;
9337 used[val >> 3] = 1;
9338 }
9339 }
9340 }
9341 while (repeat);
9342
9343 if (elf_section_data (sec)->relocs != relstart)
9344 free (relstart);
9345 }
9346
9347 /* Merge the used and skip arrays. Assume that TOC
9348 doublewords not appearing as either used or unused belong
9349 to to an entry more than one doubleword in size. */
9350 for (drop = skip, keep = used, last = 0, some_unused = 0;
9351 drop < skip + (toc->size + 7) / 8;
9352 ++drop, ++keep)
9353 {
9354 if (*keep)
9355 {
9356 *drop &= ~ref_from_discarded;
9357 if ((*drop & can_optimize) != 0)
9358 some_unused = 1;
9359 last = 0;
9360 }
9361 else if ((*drop & ref_from_discarded) != 0)
9362 {
9363 some_unused = 1;
9364 last = ref_from_discarded;
9365 }
9366 else
9367 *drop = last;
9368 }
9369
9370 free (used);
9371
9372 if (some_unused)
9373 {
9374 bfd_byte *contents, *src;
9375 unsigned long off;
9376 Elf_Internal_Sym *sym;
9377 bfd_boolean local_toc_syms = FALSE;
9378
9379 /* Shuffle the toc contents, and at the same time convert the
9380 skip array from booleans into offsets. */
9381 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9382 goto error_ret;
9383
9384 elf_section_data (toc)->this_hdr.contents = contents;
9385
9386 for (src = contents, off = 0, drop = skip;
9387 src < contents + toc->size;
9388 src += 8, ++drop)
9389 {
9390 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9391 off += 8;
9392 else if (off != 0)
9393 {
9394 *drop = off;
9395 memcpy (src - off, src, 8);
9396 }
9397 }
9398 *drop = off;
9399 toc->rawsize = toc->size;
9400 toc->size = src - contents - off;
9401
9402 /* Adjust addends for relocs against the toc section sym,
9403 and optimize any accesses we can. */
9404 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9405 {
9406 if (sec->reloc_count == 0
9407 || discarded_section (sec))
9408 continue;
9409
9410 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9411 info->keep_memory);
9412 if (relstart == NULL)
9413 goto error_ret;
9414
9415 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9416 {
9417 enum elf_ppc64_reloc_type r_type;
9418 unsigned long r_symndx;
9419 asection *sym_sec;
9420 struct elf_link_hash_entry *h;
9421 bfd_vma val;
9422
9423 r_type = ELF64_R_TYPE (rel->r_info);
9424 switch (r_type)
9425 {
9426 default:
9427 continue;
9428
9429 case R_PPC64_TOC16:
9430 case R_PPC64_TOC16_LO:
9431 case R_PPC64_TOC16_HI:
9432 case R_PPC64_TOC16_HA:
9433 case R_PPC64_TOC16_DS:
9434 case R_PPC64_TOC16_LO_DS:
9435 case R_PPC64_ADDR64:
9436 break;
9437 }
9438
9439 r_symndx = ELF64_R_SYM (rel->r_info);
9440 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9441 r_symndx, ibfd))
9442 goto error_ret;
9443
9444 if (sym_sec != toc)
9445 continue;
9446
9447 if (h != NULL)
9448 val = h->root.u.def.value;
9449 else
9450 {
9451 val = sym->st_value;
9452 if (val != 0)
9453 local_toc_syms = TRUE;
9454 }
9455
9456 val += rel->r_addend;
9457
9458 if (val > toc->rawsize)
9459 val = toc->rawsize;
9460 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9461 continue;
9462 else if ((skip[val >> 3] & can_optimize) != 0)
9463 {
9464 Elf_Internal_Rela *tocrel
9465 = toc_relocs + (skip[val >> 3] >> 2);
9466 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9467
9468 switch (r_type)
9469 {
9470 case R_PPC64_TOC16_HA:
9471 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9472 break;
9473
9474 case R_PPC64_TOC16_LO_DS:
9475 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9476 break;
9477
9478 default:
9479 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9480 ppc_howto_init ();
9481 info->callbacks->einfo
9482 /* xgettext:c-format */
9483 (_("%H: %s references "
9484 "optimized away TOC entry\n"),
9485 ibfd, sec, rel->r_offset,
9486 ppc64_elf_howto_table[r_type]->name);
9487 bfd_set_error (bfd_error_bad_value);
9488 goto error_ret;
9489 }
9490 rel->r_addend = tocrel->r_addend;
9491 elf_section_data (sec)->relocs = relstart;
9492 continue;
9493 }
9494
9495 if (h != NULL || sym->st_value != 0)
9496 continue;
9497
9498 rel->r_addend -= skip[val >> 3];
9499 elf_section_data (sec)->relocs = relstart;
9500 }
9501
9502 if (elf_section_data (sec)->relocs != relstart)
9503 free (relstart);
9504 }
9505
9506 /* We shouldn't have local or global symbols defined in the TOC,
9507 but handle them anyway. */
9508 if (local_syms != NULL)
9509 for (sym = local_syms;
9510 sym < local_syms + symtab_hdr->sh_info;
9511 ++sym)
9512 if (sym->st_value != 0
9513 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9514 {
9515 unsigned long i;
9516
9517 if (sym->st_value > toc->rawsize)
9518 i = toc->rawsize >> 3;
9519 else
9520 i = sym->st_value >> 3;
9521
9522 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9523 {
9524 if (local_toc_syms)
9525 _bfd_error_handler
9526 (_("%s defined on removed toc entry"),
9527 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9528 do
9529 ++i;
9530 while ((skip[i] & (ref_from_discarded | can_optimize)));
9531 sym->st_value = (bfd_vma) i << 3;
9532 }
9533
9534 sym->st_value -= skip[i];
9535 symtab_hdr->contents = (unsigned char *) local_syms;
9536 }
9537
9538 /* Adjust any global syms defined in this toc input section. */
9539 if (toc_inf.global_toc_syms)
9540 {
9541 toc_inf.toc = toc;
9542 toc_inf.skip = skip;
9543 toc_inf.global_toc_syms = FALSE;
9544 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9545 &toc_inf);
9546 }
9547
9548 if (toc->reloc_count != 0)
9549 {
9550 Elf_Internal_Shdr *rel_hdr;
9551 Elf_Internal_Rela *wrel;
9552 bfd_size_type sz;
9553
9554 /* Remove unused toc relocs, and adjust those we keep. */
9555 if (toc_relocs == NULL)
9556 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9557 info->keep_memory);
9558 if (toc_relocs == NULL)
9559 goto error_ret;
9560
9561 wrel = toc_relocs;
9562 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9563 if ((skip[rel->r_offset >> 3]
9564 & (ref_from_discarded | can_optimize)) == 0)
9565 {
9566 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9567 wrel->r_info = rel->r_info;
9568 wrel->r_addend = rel->r_addend;
9569 ++wrel;
9570 }
9571 else if (!dec_dynrel_count (rel->r_info, toc, info,
9572 &local_syms, NULL, NULL))
9573 goto error_ret;
9574
9575 elf_section_data (toc)->relocs = toc_relocs;
9576 toc->reloc_count = wrel - toc_relocs;
9577 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9578 sz = rel_hdr->sh_entsize;
9579 rel_hdr->sh_size = toc->reloc_count * sz;
9580 }
9581 }
9582 else if (toc_relocs != NULL
9583 && elf_section_data (toc)->relocs != toc_relocs)
9584 free (toc_relocs);
9585
9586 if (local_syms != NULL
9587 && symtab_hdr->contents != (unsigned char *) local_syms)
9588 {
9589 if (!info->keep_memory)
9590 free (local_syms);
9591 else
9592 symtab_hdr->contents = (unsigned char *) local_syms;
9593 }
9594 free (skip);
9595 }
9596
9597 return TRUE;
9598 }
9599
9600 /* Return true iff input section I references the TOC using
9601 instructions limited to +/-32k offsets. */
9602
9603 bfd_boolean
9604 ppc64_elf_has_small_toc_reloc (asection *i)
9605 {
9606 return (is_ppc64_elf (i->owner)
9607 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9608 }
9609
9610 /* Allocate space for one GOT entry. */
9611
9612 static void
9613 allocate_got (struct elf_link_hash_entry *h,
9614 struct bfd_link_info *info,
9615 struct got_entry *gent)
9616 {
9617 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9618 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9619 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9620 ? 16 : 8);
9621 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9622 ? 2 : 1) * sizeof (Elf64_External_Rela);
9623 asection *got = ppc64_elf_tdata (gent->owner)->got;
9624
9625 gent->got.offset = got->size;
9626 got->size += entsize;
9627
9628 if (h->type == STT_GNU_IFUNC)
9629 {
9630 htab->elf.irelplt->size += rentsize;
9631 htab->got_reli_size += rentsize;
9632 }
9633 else if ((bfd_link_pic (info)
9634 || (htab->elf.dynamic_sections_created
9635 && h->dynindx != -1
9636 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9637 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9638 || h->root.type != bfd_link_hash_undefweak))
9639 {
9640 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9641 relgot->size += rentsize;
9642 }
9643 }
9644
9645 /* This function merges got entries in the same toc group. */
9646
9647 static void
9648 merge_got_entries (struct got_entry **pent)
9649 {
9650 struct got_entry *ent, *ent2;
9651
9652 for (ent = *pent; ent != NULL; ent = ent->next)
9653 if (!ent->is_indirect)
9654 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9655 if (!ent2->is_indirect
9656 && ent2->addend == ent->addend
9657 && ent2->tls_type == ent->tls_type
9658 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9659 {
9660 ent2->is_indirect = TRUE;
9661 ent2->got.ent = ent;
9662 }
9663 }
9664
9665 /* If H is undefined weak, make it dynamic if that makes sense. */
9666
9667 static bfd_boolean
9668 ensure_undefweak_dynamic (struct bfd_link_info *info,
9669 struct elf_link_hash_entry *h)
9670 {
9671 struct elf_link_hash_table *htab = elf_hash_table (info);
9672
9673 if (htab->dynamic_sections_created
9674 && h->root.type == bfd_link_hash_undefweak
9675 && h->dynindx == -1
9676 && !h->forced_local
9677 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9678 return bfd_elf_link_record_dynamic_symbol (info, h);
9679 return TRUE;
9680 }
9681
9682 /* Allocate space in .plt, .got and associated reloc sections for
9683 dynamic relocs. */
9684
9685 static bfd_boolean
9686 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9687 {
9688 struct bfd_link_info *info;
9689 struct ppc_link_hash_table *htab;
9690 asection *s;
9691 struct ppc_link_hash_entry *eh;
9692 struct got_entry **pgent, *gent;
9693
9694 if (h->root.type == bfd_link_hash_indirect)
9695 return TRUE;
9696
9697 info = (struct bfd_link_info *) inf;
9698 htab = ppc_hash_table (info);
9699 if (htab == NULL)
9700 return FALSE;
9701
9702 eh = (struct ppc_link_hash_entry *) h;
9703 /* Run through the TLS GD got entries first if we're changing them
9704 to TPREL. */
9705 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9706 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9707 if (gent->got.refcount > 0
9708 && (gent->tls_type & TLS_GD) != 0)
9709 {
9710 /* This was a GD entry that has been converted to TPREL. If
9711 there happens to be a TPREL entry we can use that one. */
9712 struct got_entry *ent;
9713 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9714 if (ent->got.refcount > 0
9715 && (ent->tls_type & TLS_TPREL) != 0
9716 && ent->addend == gent->addend
9717 && ent->owner == gent->owner)
9718 {
9719 gent->got.refcount = 0;
9720 break;
9721 }
9722
9723 /* If not, then we'll be using our own TPREL entry. */
9724 if (gent->got.refcount != 0)
9725 gent->tls_type = TLS_TLS | TLS_TPREL;
9726 }
9727
9728 /* Remove any list entry that won't generate a word in the GOT before
9729 we call merge_got_entries. Otherwise we risk merging to empty
9730 entries. */
9731 pgent = &h->got.glist;
9732 while ((gent = *pgent) != NULL)
9733 if (gent->got.refcount > 0)
9734 {
9735 if ((gent->tls_type & TLS_LD) != 0
9736 && !h->def_dynamic)
9737 {
9738 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9739 *pgent = gent->next;
9740 }
9741 else
9742 pgent = &gent->next;
9743 }
9744 else
9745 *pgent = gent->next;
9746
9747 if (!htab->do_multi_toc)
9748 merge_got_entries (&h->got.glist);
9749
9750 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9751 if (!gent->is_indirect)
9752 {
9753 /* Make sure this symbol is output as a dynamic symbol.
9754 Undefined weak syms won't yet be marked as dynamic. */
9755 if (!ensure_undefweak_dynamic (info, h))
9756 return FALSE;
9757
9758 if (!is_ppc64_elf (gent->owner))
9759 abort ();
9760
9761 allocate_got (h, info, gent);
9762 }
9763
9764 if (!htab->elf.dynamic_sections_created
9765 && h->type != STT_GNU_IFUNC)
9766 eh->dyn_relocs = NULL;
9767
9768 if (eh->dyn_relocs != NULL)
9769 {
9770 struct elf_dyn_relocs *p, **pp;
9771
9772 /* In the shared -Bsymbolic case, discard space allocated for
9773 dynamic pc-relative relocs against symbols which turn out to
9774 be defined in regular objects. For the normal shared case,
9775 discard space for relocs that have become local due to symbol
9776 visibility changes. */
9777
9778 if (bfd_link_pic (info))
9779 {
9780 /* Relocs that use pc_count are those that appear on a call
9781 insn, or certain REL relocs (see must_be_dyn_reloc) that
9782 can be generated via assembly. We want calls to
9783 protected symbols to resolve directly to the function
9784 rather than going via the plt. If people want function
9785 pointer comparisons to work as expected then they should
9786 avoid writing weird assembly. */
9787 if (SYMBOL_CALLS_LOCAL (info, h))
9788 {
9789 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9790 {
9791 p->count -= p->pc_count;
9792 p->pc_count = 0;
9793 if (p->count == 0)
9794 *pp = p->next;
9795 else
9796 pp = &p->next;
9797 }
9798 }
9799
9800 /* Also discard relocs on undefined weak syms with
9801 non-default visibility. */
9802 if (eh->dyn_relocs != NULL
9803 && h->root.type == bfd_link_hash_undefweak)
9804 {
9805 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9806 eh->dyn_relocs = NULL;
9807
9808 /* Make sure this symbol is output as a dynamic symbol.
9809 Undefined weak syms won't yet be marked as dynamic. */
9810 else if (!ensure_undefweak_dynamic (info, h))
9811 return FALSE;
9812 }
9813 }
9814 else if (h->type == STT_GNU_IFUNC)
9815 {
9816 /* A plt entry is always created when making direct calls to
9817 an ifunc, even when building a static executable, but
9818 that doesn't cover all cases. We may have only an ifunc
9819 initialised function pointer for a given ifunc symbol.
9820
9821 For ELFv2, dynamic relocations are not required when
9822 generating a global entry PLT stub. */
9823 if (abiversion (info->output_bfd) >= 2)
9824 {
9825 if (global_entry_stub (h))
9826 eh->dyn_relocs = NULL;
9827 }
9828
9829 /* For ELFv1 we have function descriptors. Descriptors need
9830 to be treated like PLT entries and thus have dynamic
9831 relocations. One exception is when the function
9832 descriptor is copied into .dynbss (which should only
9833 happen with ancient versions of gcc). */
9834 else if (h->needs_copy)
9835 eh->dyn_relocs = NULL;
9836 }
9837 else if (ELIMINATE_COPY_RELOCS)
9838 {
9839 /* For the non-pic case, discard space for relocs against
9840 symbols which turn out to need copy relocs or are not
9841 dynamic. */
9842 if (!h->non_got_ref
9843 && !h->def_regular)
9844 {
9845 /* Make sure this symbol is output as a dynamic symbol.
9846 Undefined weak syms won't yet be marked as dynamic. */
9847 if (!ensure_undefweak_dynamic (info, h))
9848 return FALSE;
9849
9850 if (h->dynindx == -1)
9851 eh->dyn_relocs = NULL;
9852 }
9853 else
9854 eh->dyn_relocs = NULL;
9855 }
9856
9857 /* Finally, allocate space. */
9858 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9859 {
9860 asection *sreloc = elf_section_data (p->sec)->sreloc;
9861 if (eh->elf.type == STT_GNU_IFUNC)
9862 sreloc = htab->elf.irelplt;
9863 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9864 }
9865 }
9866
9867 if ((htab->elf.dynamic_sections_created
9868 && h->dynindx != -1)
9869 || h->type == STT_GNU_IFUNC)
9870 {
9871 struct plt_entry *pent;
9872 bfd_boolean doneone = FALSE;
9873 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9874 if (pent->plt.refcount > 0)
9875 {
9876 if (!htab->elf.dynamic_sections_created
9877 || h->dynindx == -1)
9878 {
9879 s = htab->elf.iplt;
9880 pent->plt.offset = s->size;
9881 s->size += PLT_ENTRY_SIZE (htab);
9882 s = htab->elf.irelplt;
9883 }
9884 else
9885 {
9886 /* If this is the first .plt entry, make room for the special
9887 first entry. */
9888 s = htab->elf.splt;
9889 if (s->size == 0)
9890 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9891
9892 pent->plt.offset = s->size;
9893
9894 /* Make room for this entry. */
9895 s->size += PLT_ENTRY_SIZE (htab);
9896
9897 /* Make room for the .glink code. */
9898 s = htab->glink;
9899 if (s->size == 0)
9900 s->size += GLINK_CALL_STUB_SIZE;
9901 if (htab->opd_abi)
9902 {
9903 /* We need bigger stubs past index 32767. */
9904 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9905 s->size += 4;
9906 s->size += 2*4;
9907 }
9908 else
9909 s->size += 4;
9910
9911 /* We also need to make an entry in the .rela.plt section. */
9912 s = htab->elf.srelplt;
9913 }
9914 s->size += sizeof (Elf64_External_Rela);
9915 doneone = TRUE;
9916 }
9917 else
9918 pent->plt.offset = (bfd_vma) -1;
9919 if (!doneone)
9920 {
9921 h->plt.plist = NULL;
9922 h->needs_plt = 0;
9923 }
9924 }
9925 else
9926 {
9927 h->plt.plist = NULL;
9928 h->needs_plt = 0;
9929 }
9930
9931 return TRUE;
9932 }
9933
9934 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9935 to set up space for global entry stubs. These are put in glink,
9936 after the branch table. */
9937
9938 static bfd_boolean
9939 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9940 {
9941 struct bfd_link_info *info;
9942 struct ppc_link_hash_table *htab;
9943 struct plt_entry *pent;
9944 asection *s;
9945
9946 if (h->root.type == bfd_link_hash_indirect)
9947 return TRUE;
9948
9949 if (!h->pointer_equality_needed)
9950 return TRUE;
9951
9952 if (h->def_regular)
9953 return TRUE;
9954
9955 info = inf;
9956 htab = ppc_hash_table (info);
9957 if (htab == NULL)
9958 return FALSE;
9959
9960 s = htab->glink;
9961 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9962 if (pent->plt.offset != (bfd_vma) -1
9963 && pent->addend == 0)
9964 {
9965 /* For ELFv2, if this symbol is not defined in a regular file
9966 and we are not generating a shared library or pie, then we
9967 need to define the symbol in the executable on a call stub.
9968 This is to avoid text relocations. */
9969 s->size = (s->size + 15) & -16;
9970 h->root.type = bfd_link_hash_defined;
9971 h->root.u.def.section = s;
9972 h->root.u.def.value = s->size;
9973 s->size += 16;
9974 break;
9975 }
9976 return TRUE;
9977 }
9978
9979 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9980 read-only sections. */
9981
9982 static bfd_boolean
9983 maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
9984 {
9985 if (h->root.type == bfd_link_hash_indirect)
9986 return TRUE;
9987
9988 if (readonly_dynrelocs (h))
9989 {
9990 ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
9991
9992 /* Not an error, just cut short the traversal. */
9993 return FALSE;
9994 }
9995 return TRUE;
9996 }
9997
9998 /* Set the sizes of the dynamic sections. */
9999
10000 static bfd_boolean
10001 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10002 struct bfd_link_info *info)
10003 {
10004 struct ppc_link_hash_table *htab;
10005 bfd *dynobj;
10006 asection *s;
10007 bfd_boolean relocs;
10008 bfd *ibfd;
10009 struct got_entry *first_tlsld;
10010
10011 htab = ppc_hash_table (info);
10012 if (htab == NULL)
10013 return FALSE;
10014
10015 dynobj = htab->elf.dynobj;
10016 if (dynobj == NULL)
10017 abort ();
10018
10019 if (htab->elf.dynamic_sections_created)
10020 {
10021 /* Set the contents of the .interp section to the interpreter. */
10022 if (bfd_link_executable (info) && !info->nointerp)
10023 {
10024 s = bfd_get_linker_section (dynobj, ".interp");
10025 if (s == NULL)
10026 abort ();
10027 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10028 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10029 }
10030 }
10031
10032 /* Set up .got offsets for local syms, and space for local dynamic
10033 relocs. */
10034 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10035 {
10036 struct got_entry **lgot_ents;
10037 struct got_entry **end_lgot_ents;
10038 struct plt_entry **local_plt;
10039 struct plt_entry **end_local_plt;
10040 unsigned char *lgot_masks;
10041 bfd_size_type locsymcount;
10042 Elf_Internal_Shdr *symtab_hdr;
10043
10044 if (!is_ppc64_elf (ibfd))
10045 continue;
10046
10047 for (s = ibfd->sections; s != NULL; s = s->next)
10048 {
10049 struct ppc_dyn_relocs *p;
10050
10051 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10052 {
10053 if (!bfd_is_abs_section (p->sec)
10054 && bfd_is_abs_section (p->sec->output_section))
10055 {
10056 /* Input section has been discarded, either because
10057 it is a copy of a linkonce section or due to
10058 linker script /DISCARD/, so we'll be discarding
10059 the relocs too. */
10060 }
10061 else if (p->count != 0)
10062 {
10063 asection *srel = elf_section_data (p->sec)->sreloc;
10064 if (p->ifunc)
10065 srel = htab->elf.irelplt;
10066 srel->size += p->count * sizeof (Elf64_External_Rela);
10067 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10068 info->flags |= DF_TEXTREL;
10069 }
10070 }
10071 }
10072
10073 lgot_ents = elf_local_got_ents (ibfd);
10074 if (!lgot_ents)
10075 continue;
10076
10077 symtab_hdr = &elf_symtab_hdr (ibfd);
10078 locsymcount = symtab_hdr->sh_info;
10079 end_lgot_ents = lgot_ents + locsymcount;
10080 local_plt = (struct plt_entry **) end_lgot_ents;
10081 end_local_plt = local_plt + locsymcount;
10082 lgot_masks = (unsigned char *) end_local_plt;
10083 s = ppc64_elf_tdata (ibfd)->got;
10084 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10085 {
10086 struct got_entry **pent, *ent;
10087
10088 pent = lgot_ents;
10089 while ((ent = *pent) != NULL)
10090 if (ent->got.refcount > 0)
10091 {
10092 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10093 {
10094 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10095 *pent = ent->next;
10096 }
10097 else
10098 {
10099 unsigned int ent_size = 8;
10100 unsigned int rel_size = sizeof (Elf64_External_Rela);
10101
10102 ent->got.offset = s->size;
10103 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10104 {
10105 ent_size *= 2;
10106 rel_size *= 2;
10107 }
10108 s->size += ent_size;
10109 if ((*lgot_masks & PLT_IFUNC) != 0)
10110 {
10111 htab->elf.irelplt->size += rel_size;
10112 htab->got_reli_size += rel_size;
10113 }
10114 else if (bfd_link_pic (info))
10115 {
10116 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10117 srel->size += rel_size;
10118 }
10119 pent = &ent->next;
10120 }
10121 }
10122 else
10123 *pent = ent->next;
10124 }
10125
10126 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10127 for (; local_plt < end_local_plt; ++local_plt)
10128 {
10129 struct plt_entry *ent;
10130
10131 for (ent = *local_plt; ent != NULL; ent = ent->next)
10132 if (ent->plt.refcount > 0)
10133 {
10134 s = htab->elf.iplt;
10135 ent->plt.offset = s->size;
10136 s->size += PLT_ENTRY_SIZE (htab);
10137
10138 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10139 }
10140 else
10141 ent->plt.offset = (bfd_vma) -1;
10142 }
10143 }
10144
10145 /* Allocate global sym .plt and .got entries, and space for global
10146 sym dynamic relocs. */
10147 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10148 /* Stash the end of glink branch table. */
10149 if (htab->glink != NULL)
10150 htab->glink->rawsize = htab->glink->size;
10151
10152 if (!htab->opd_abi && !bfd_link_pic (info))
10153 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10154
10155 first_tlsld = NULL;
10156 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10157 {
10158 struct got_entry *ent;
10159
10160 if (!is_ppc64_elf (ibfd))
10161 continue;
10162
10163 ent = ppc64_tlsld_got (ibfd);
10164 if (ent->got.refcount > 0)
10165 {
10166 if (!htab->do_multi_toc && first_tlsld != NULL)
10167 {
10168 ent->is_indirect = TRUE;
10169 ent->got.ent = first_tlsld;
10170 }
10171 else
10172 {
10173 if (first_tlsld == NULL)
10174 first_tlsld = ent;
10175 s = ppc64_elf_tdata (ibfd)->got;
10176 ent->got.offset = s->size;
10177 ent->owner = ibfd;
10178 s->size += 16;
10179 if (bfd_link_pic (info))
10180 {
10181 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10182 srel->size += sizeof (Elf64_External_Rela);
10183 }
10184 }
10185 }
10186 else
10187 ent->got.offset = (bfd_vma) -1;
10188 }
10189
10190 /* We now have determined the sizes of the various dynamic sections.
10191 Allocate memory for them. */
10192 relocs = FALSE;
10193 for (s = dynobj->sections; s != NULL; s = s->next)
10194 {
10195 if ((s->flags & SEC_LINKER_CREATED) == 0)
10196 continue;
10197
10198 if (s == htab->brlt || s == htab->relbrlt)
10199 /* These haven't been allocated yet; don't strip. */
10200 continue;
10201 else if (s == htab->elf.sgot
10202 || s == htab->elf.splt
10203 || s == htab->elf.iplt
10204 || s == htab->glink
10205 || s == htab->elf.sdynbss
10206 || s == htab->elf.sdynrelro)
10207 {
10208 /* Strip this section if we don't need it; see the
10209 comment below. */
10210 }
10211 else if (s == htab->glink_eh_frame)
10212 {
10213 if (!bfd_is_abs_section (s->output_section))
10214 /* Not sized yet. */
10215 continue;
10216 }
10217 else if (CONST_STRNEQ (s->name, ".rela"))
10218 {
10219 if (s->size != 0)
10220 {
10221 if (s != htab->elf.srelplt)
10222 relocs = TRUE;
10223
10224 /* We use the reloc_count field as a counter if we need
10225 to copy relocs into the output file. */
10226 s->reloc_count = 0;
10227 }
10228 }
10229 else
10230 {
10231 /* It's not one of our sections, so don't allocate space. */
10232 continue;
10233 }
10234
10235 if (s->size == 0)
10236 {
10237 /* If we don't need this section, strip it from the
10238 output file. This is mostly to handle .rela.bss and
10239 .rela.plt. We must create both sections in
10240 create_dynamic_sections, because they must be created
10241 before the linker maps input sections to output
10242 sections. The linker does that before
10243 adjust_dynamic_symbol is called, and it is that
10244 function which decides whether anything needs to go
10245 into these sections. */
10246 s->flags |= SEC_EXCLUDE;
10247 continue;
10248 }
10249
10250 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10251 continue;
10252
10253 /* Allocate memory for the section contents. We use bfd_zalloc
10254 here in case unused entries are not reclaimed before the
10255 section's contents are written out. This should not happen,
10256 but this way if it does we get a R_PPC64_NONE reloc in .rela
10257 sections instead of garbage.
10258 We also rely on the section contents being zero when writing
10259 the GOT and .dynrelro. */
10260 s->contents = bfd_zalloc (dynobj, s->size);
10261 if (s->contents == NULL)
10262 return FALSE;
10263 }
10264
10265 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10266 {
10267 if (!is_ppc64_elf (ibfd))
10268 continue;
10269
10270 s = ppc64_elf_tdata (ibfd)->got;
10271 if (s != NULL && s != htab->elf.sgot)
10272 {
10273 if (s->size == 0)
10274 s->flags |= SEC_EXCLUDE;
10275 else
10276 {
10277 s->contents = bfd_zalloc (ibfd, s->size);
10278 if (s->contents == NULL)
10279 return FALSE;
10280 }
10281 }
10282 s = ppc64_elf_tdata (ibfd)->relgot;
10283 if (s != NULL)
10284 {
10285 if (s->size == 0)
10286 s->flags |= SEC_EXCLUDE;
10287 else
10288 {
10289 s->contents = bfd_zalloc (ibfd, s->size);
10290 if (s->contents == NULL)
10291 return FALSE;
10292 relocs = TRUE;
10293 s->reloc_count = 0;
10294 }
10295 }
10296 }
10297
10298 if (htab->elf.dynamic_sections_created)
10299 {
10300 bfd_boolean tls_opt;
10301
10302 /* Add some entries to the .dynamic section. We fill in the
10303 values later, in ppc64_elf_finish_dynamic_sections, but we
10304 must add the entries now so that we get the correct size for
10305 the .dynamic section. The DT_DEBUG entry is filled in by the
10306 dynamic linker and used by the debugger. */
10307 #define add_dynamic_entry(TAG, VAL) \
10308 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10309
10310 if (bfd_link_executable (info))
10311 {
10312 if (!add_dynamic_entry (DT_DEBUG, 0))
10313 return FALSE;
10314 }
10315
10316 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10317 {
10318 if (!add_dynamic_entry (DT_PLTGOT, 0)
10319 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10320 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10321 || !add_dynamic_entry (DT_JMPREL, 0)
10322 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10323 return FALSE;
10324 }
10325
10326 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10327 {
10328 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10329 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10330 return FALSE;
10331 }
10332
10333 tls_opt = (htab->params->tls_get_addr_opt
10334 && htab->tls_get_addr_fd != NULL
10335 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10336 if (tls_opt || !htab->opd_abi)
10337 {
10338 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10339 return FALSE;
10340 }
10341
10342 if (relocs)
10343 {
10344 if (!add_dynamic_entry (DT_RELA, 0)
10345 || !add_dynamic_entry (DT_RELASZ, 0)
10346 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10347 return FALSE;
10348
10349 /* If any dynamic relocs apply to a read-only section,
10350 then we need a DT_TEXTREL entry. */
10351 if ((info->flags & DF_TEXTREL) == 0)
10352 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10353
10354 if ((info->flags & DF_TEXTREL) != 0)
10355 {
10356 if (!add_dynamic_entry (DT_TEXTREL, 0))
10357 return FALSE;
10358 }
10359 }
10360 }
10361 #undef add_dynamic_entry
10362
10363 return TRUE;
10364 }
10365
10366 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10367
10368 static bfd_boolean
10369 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10370 {
10371 if (h->plt.plist != NULL
10372 && !h->def_regular
10373 && !h->pointer_equality_needed)
10374 return FALSE;
10375
10376 return _bfd_elf_hash_symbol (h);
10377 }
10378
10379 /* Determine the type of stub needed, if any, for a call. */
10380
10381 static inline enum ppc_stub_type
10382 ppc_type_of_stub (asection *input_sec,
10383 const Elf_Internal_Rela *rel,
10384 struct ppc_link_hash_entry **hash,
10385 struct plt_entry **plt_ent,
10386 bfd_vma destination,
10387 unsigned long local_off)
10388 {
10389 struct ppc_link_hash_entry *h = *hash;
10390 bfd_vma location;
10391 bfd_vma branch_offset;
10392 bfd_vma max_branch_offset;
10393 enum elf_ppc64_reloc_type r_type;
10394
10395 if (h != NULL)
10396 {
10397 struct plt_entry *ent;
10398 struct ppc_link_hash_entry *fdh = h;
10399 if (h->oh != NULL
10400 && h->oh->is_func_descriptor)
10401 {
10402 fdh = ppc_follow_link (h->oh);
10403 *hash = fdh;
10404 }
10405
10406 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10407 if (ent->addend == rel->r_addend
10408 && ent->plt.offset != (bfd_vma) -1)
10409 {
10410 *plt_ent = ent;
10411 return ppc_stub_plt_call;
10412 }
10413
10414 /* Here, we know we don't have a plt entry. If we don't have a
10415 either a defined function descriptor or a defined entry symbol
10416 in a regular object file, then it is pointless trying to make
10417 any other type of stub. */
10418 if (!is_static_defined (&fdh->elf)
10419 && !is_static_defined (&h->elf))
10420 return ppc_stub_none;
10421 }
10422 else if (elf_local_got_ents (input_sec->owner) != NULL)
10423 {
10424 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10425 struct plt_entry **local_plt = (struct plt_entry **)
10426 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10427 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10428
10429 if (local_plt[r_symndx] != NULL)
10430 {
10431 struct plt_entry *ent;
10432
10433 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10434 if (ent->addend == rel->r_addend
10435 && ent->plt.offset != (bfd_vma) -1)
10436 {
10437 *plt_ent = ent;
10438 return ppc_stub_plt_call;
10439 }
10440 }
10441 }
10442
10443 /* Determine where the call point is. */
10444 location = (input_sec->output_offset
10445 + input_sec->output_section->vma
10446 + rel->r_offset);
10447
10448 branch_offset = destination - location;
10449 r_type = ELF64_R_TYPE (rel->r_info);
10450
10451 /* Determine if a long branch stub is needed. */
10452 max_branch_offset = 1 << 25;
10453 if (r_type != R_PPC64_REL24)
10454 max_branch_offset = 1 << 15;
10455
10456 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10457 /* We need a stub. Figure out whether a long_branch or plt_branch
10458 is needed later. */
10459 return ppc_stub_long_branch;
10460
10461 return ppc_stub_none;
10462 }
10463
10464 /* With power7 weakly ordered memory model, it is possible for ld.so
10465 to update a plt entry in one thread and have another thread see a
10466 stale zero toc entry. To avoid this we need some sort of acquire
10467 barrier in the call stub. One solution is to make the load of the
10468 toc word seem to appear to depend on the load of the function entry
10469 word. Another solution is to test for r2 being zero, and branch to
10470 the appropriate glink entry if so.
10471
10472 . fake dep barrier compare
10473 . ld 12,xxx(2) ld 12,xxx(2)
10474 . mtctr 12 mtctr 12
10475 . xor 11,12,12 ld 2,xxx+8(2)
10476 . add 2,2,11 cmpldi 2,0
10477 . ld 2,xxx+8(2) bnectr+
10478 . bctr b <glink_entry>
10479
10480 The solution involving the compare turns out to be faster, so
10481 that's what we use unless the branch won't reach. */
10482
10483 #define ALWAYS_USE_FAKE_DEP 0
10484 #define ALWAYS_EMIT_R2SAVE 0
10485
10486 #define PPC_LO(v) ((v) & 0xffff)
10487 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10488 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10489
10490 static inline unsigned int
10491 plt_stub_size (struct ppc_link_hash_table *htab,
10492 struct ppc_stub_hash_entry *stub_entry,
10493 bfd_vma off)
10494 {
10495 unsigned size = 12;
10496
10497 if (ALWAYS_EMIT_R2SAVE
10498 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10499 size += 4;
10500 if (PPC_HA (off) != 0)
10501 size += 4;
10502 if (htab->opd_abi)
10503 {
10504 size += 4;
10505 if (htab->params->plt_static_chain)
10506 size += 4;
10507 if (htab->params->plt_thread_safe
10508 && htab->elf.dynamic_sections_created
10509 && stub_entry->h != NULL
10510 && stub_entry->h->elf.dynindx != -1)
10511 size += 8;
10512 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10513 size += 4;
10514 }
10515 if (stub_entry->h != NULL
10516 && (stub_entry->h == htab->tls_get_addr_fd
10517 || stub_entry->h == htab->tls_get_addr)
10518 && htab->params->tls_get_addr_opt)
10519 size += 13 * 4;
10520 return size;
10521 }
10522
10523 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
10524 then return the padding needed to do so. */
10525 static inline unsigned int
10526 plt_stub_pad (struct ppc_link_hash_table *htab,
10527 struct ppc_stub_hash_entry *stub_entry,
10528 bfd_vma plt_off)
10529 {
10530 int stub_align = 1 << htab->params->plt_stub_align;
10531 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10532 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10533
10534 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10535 > ((stub_size - 1) & -stub_align))
10536 return stub_align - (stub_off & (stub_align - 1));
10537 return 0;
10538 }
10539
10540 /* Build a .plt call stub. */
10541
10542 static inline bfd_byte *
10543 build_plt_stub (struct ppc_link_hash_table *htab,
10544 struct ppc_stub_hash_entry *stub_entry,
10545 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10546 {
10547 bfd *obfd = htab->params->stub_bfd;
10548 bfd_boolean plt_load_toc = htab->opd_abi;
10549 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10550 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10551 && htab->elf.dynamic_sections_created
10552 && stub_entry->h != NULL
10553 && stub_entry->h->elf.dynindx != -1);
10554 bfd_boolean use_fake_dep = plt_thread_safe;
10555 bfd_vma cmp_branch_off = 0;
10556
10557 if (!ALWAYS_USE_FAKE_DEP
10558 && plt_load_toc
10559 && plt_thread_safe
10560 && !((stub_entry->h == htab->tls_get_addr_fd
10561 || stub_entry->h == htab->tls_get_addr)
10562 && htab->params->tls_get_addr_opt))
10563 {
10564 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10565 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10566 / PLT_ENTRY_SIZE (htab));
10567 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10568 bfd_vma to, from;
10569
10570 if (pltindex > 32768)
10571 glinkoff += (pltindex - 32768) * 4;
10572 to = (glinkoff
10573 + htab->glink->output_offset
10574 + htab->glink->output_section->vma);
10575 from = (p - stub_entry->group->stub_sec->contents
10576 + 4 * (ALWAYS_EMIT_R2SAVE
10577 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10578 + 4 * (PPC_HA (offset) != 0)
10579 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10580 != PPC_HA (offset))
10581 + 4 * (plt_static_chain != 0)
10582 + 20
10583 + stub_entry->group->stub_sec->output_offset
10584 + stub_entry->group->stub_sec->output_section->vma);
10585 cmp_branch_off = to - from;
10586 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10587 }
10588
10589 if (PPC_HA (offset) != 0)
10590 {
10591 if (r != NULL)
10592 {
10593 if (ALWAYS_EMIT_R2SAVE
10594 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10595 r[0].r_offset += 4;
10596 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10597 r[1].r_offset = r[0].r_offset + 4;
10598 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10599 r[1].r_addend = r[0].r_addend;
10600 if (plt_load_toc)
10601 {
10602 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10603 {
10604 r[2].r_offset = r[1].r_offset + 4;
10605 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10606 r[2].r_addend = r[0].r_addend;
10607 }
10608 else
10609 {
10610 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10611 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10612 r[2].r_addend = r[0].r_addend + 8;
10613 if (plt_static_chain)
10614 {
10615 r[3].r_offset = r[2].r_offset + 4;
10616 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10617 r[3].r_addend = r[0].r_addend + 16;
10618 }
10619 }
10620 }
10621 }
10622 if (ALWAYS_EMIT_R2SAVE
10623 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10624 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10625 if (plt_load_toc)
10626 {
10627 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10628 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10629 }
10630 else
10631 {
10632 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10633 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10634 }
10635 if (plt_load_toc
10636 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10637 {
10638 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10639 offset = 0;
10640 }
10641 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10642 if (plt_load_toc)
10643 {
10644 if (use_fake_dep)
10645 {
10646 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10647 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10648 }
10649 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10650 if (plt_static_chain)
10651 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10652 }
10653 }
10654 else
10655 {
10656 if (r != NULL)
10657 {
10658 if (ALWAYS_EMIT_R2SAVE
10659 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10660 r[0].r_offset += 4;
10661 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10662 if (plt_load_toc)
10663 {
10664 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10665 {
10666 r[1].r_offset = r[0].r_offset + 4;
10667 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10668 r[1].r_addend = r[0].r_addend;
10669 }
10670 else
10671 {
10672 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10673 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10674 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10675 if (plt_static_chain)
10676 {
10677 r[2].r_offset = r[1].r_offset + 4;
10678 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10679 r[2].r_addend = r[0].r_addend + 8;
10680 }
10681 }
10682 }
10683 }
10684 if (ALWAYS_EMIT_R2SAVE
10685 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10686 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10687 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10688 if (plt_load_toc
10689 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10690 {
10691 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10692 offset = 0;
10693 }
10694 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10695 if (plt_load_toc)
10696 {
10697 if (use_fake_dep)
10698 {
10699 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10700 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10701 }
10702 if (plt_static_chain)
10703 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10704 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10705 }
10706 }
10707 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10708 {
10709 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10710 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10711 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10712 }
10713 else
10714 bfd_put_32 (obfd, BCTR, p), p += 4;
10715 return p;
10716 }
10717
10718 /* Build a special .plt call stub for __tls_get_addr. */
10719
10720 #define LD_R11_0R3 0xe9630000
10721 #define LD_R12_0R3 0xe9830000
10722 #define MR_R0_R3 0x7c601b78
10723 #define CMPDI_R11_0 0x2c2b0000
10724 #define ADD_R3_R12_R13 0x7c6c6a14
10725 #define BEQLR 0x4d820020
10726 #define MR_R3_R0 0x7c030378
10727 #define STD_R11_0R1 0xf9610000
10728 #define BCTRL 0x4e800421
10729 #define LD_R11_0R1 0xe9610000
10730 #define MTLR_R11 0x7d6803a6
10731
10732 static inline bfd_byte *
10733 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10734 struct ppc_stub_hash_entry *stub_entry,
10735 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10736 {
10737 bfd *obfd = htab->params->stub_bfd;
10738
10739 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10740 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10741 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10742 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10743 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10744 bfd_put_32 (obfd, BEQLR, p), p += 4;
10745 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10746 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10747 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10748
10749 if (r != NULL)
10750 r[0].r_offset += 9 * 4;
10751 p = build_plt_stub (htab, stub_entry, p, offset, r);
10752 bfd_put_32 (obfd, BCTRL, p - 4);
10753
10754 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10755 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10756 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10757 bfd_put_32 (obfd, BLR, p), p += 4;
10758
10759 return p;
10760 }
10761
10762 static Elf_Internal_Rela *
10763 get_relocs (asection *sec, int count)
10764 {
10765 Elf_Internal_Rela *relocs;
10766 struct bfd_elf_section_data *elfsec_data;
10767
10768 elfsec_data = elf_section_data (sec);
10769 relocs = elfsec_data->relocs;
10770 if (relocs == NULL)
10771 {
10772 bfd_size_type relsize;
10773 relsize = sec->reloc_count * sizeof (*relocs);
10774 relocs = bfd_alloc (sec->owner, relsize);
10775 if (relocs == NULL)
10776 return NULL;
10777 elfsec_data->relocs = relocs;
10778 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10779 sizeof (Elf_Internal_Shdr));
10780 if (elfsec_data->rela.hdr == NULL)
10781 return NULL;
10782 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10783 * sizeof (Elf64_External_Rela));
10784 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10785 sec->reloc_count = 0;
10786 }
10787 relocs += sec->reloc_count;
10788 sec->reloc_count += count;
10789 return relocs;
10790 }
10791
10792 static bfd_vma
10793 get_r2off (struct bfd_link_info *info,
10794 struct ppc_stub_hash_entry *stub_entry)
10795 {
10796 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10797 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10798
10799 if (r2off == 0)
10800 {
10801 /* Support linking -R objects. Get the toc pointer from the
10802 opd entry. */
10803 char buf[8];
10804 if (!htab->opd_abi)
10805 return r2off;
10806 asection *opd = stub_entry->h->elf.root.u.def.section;
10807 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10808
10809 if (strcmp (opd->name, ".opd") != 0
10810 || opd->reloc_count != 0)
10811 {
10812 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10813 stub_entry->h->elf.root.root.string);
10814 bfd_set_error (bfd_error_bad_value);
10815 return (bfd_vma) -1;
10816 }
10817 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10818 return (bfd_vma) -1;
10819 r2off = bfd_get_64 (opd->owner, buf);
10820 r2off -= elf_gp (info->output_bfd);
10821 }
10822 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10823 return r2off;
10824 }
10825
10826 static bfd_boolean
10827 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10828 {
10829 struct ppc_stub_hash_entry *stub_entry;
10830 struct ppc_branch_hash_entry *br_entry;
10831 struct bfd_link_info *info;
10832 struct ppc_link_hash_table *htab;
10833 bfd_byte *loc;
10834 bfd_byte *p;
10835 bfd_vma dest, off;
10836 int size;
10837 Elf_Internal_Rela *r;
10838 asection *plt;
10839
10840 /* Massage our args to the form they really have. */
10841 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10842 info = in_arg;
10843
10844 htab = ppc_hash_table (info);
10845 if (htab == NULL)
10846 return FALSE;
10847
10848 /* Make a note of the offset within the stubs for this entry. */
10849 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10850 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10851
10852 htab->stub_count[stub_entry->stub_type - 1] += 1;
10853 switch (stub_entry->stub_type)
10854 {
10855 case ppc_stub_long_branch:
10856 case ppc_stub_long_branch_r2off:
10857 /* Branches are relative. This is where we are going to. */
10858 dest = (stub_entry->target_value
10859 + stub_entry->target_section->output_offset
10860 + stub_entry->target_section->output_section->vma);
10861 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10862 off = dest;
10863
10864 /* And this is where we are coming from. */
10865 off -= (stub_entry->stub_offset
10866 + stub_entry->group->stub_sec->output_offset
10867 + stub_entry->group->stub_sec->output_section->vma);
10868
10869 size = 4;
10870 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10871 {
10872 bfd_vma r2off = get_r2off (info, stub_entry);
10873
10874 if (r2off == (bfd_vma) -1)
10875 {
10876 htab->stub_error = TRUE;
10877 return FALSE;
10878 }
10879 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
10880 loc += 4;
10881 size = 8;
10882 if (PPC_HA (r2off) != 0)
10883 {
10884 bfd_put_32 (htab->params->stub_bfd,
10885 ADDIS_R2_R2 | PPC_HA (r2off), loc);
10886 loc += 4;
10887 size += 4;
10888 }
10889 if (PPC_LO (r2off) != 0)
10890 {
10891 bfd_put_32 (htab->params->stub_bfd,
10892 ADDI_R2_R2 | PPC_LO (r2off), loc);
10893 loc += 4;
10894 size += 4;
10895 }
10896 off -= size - 4;
10897 }
10898 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10899
10900 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10901 {
10902 info->callbacks->einfo
10903 (_("%P: long branch stub `%s' offset overflow\n"),
10904 stub_entry->root.string);
10905 htab->stub_error = TRUE;
10906 return FALSE;
10907 }
10908
10909 if (info->emitrelocations)
10910 {
10911 r = get_relocs (stub_entry->group->stub_sec, 1);
10912 if (r == NULL)
10913 return FALSE;
10914 r->r_offset = loc - stub_entry->group->stub_sec->contents;
10915 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10916 r->r_addend = dest;
10917 if (stub_entry->h != NULL)
10918 {
10919 struct elf_link_hash_entry **hashes;
10920 unsigned long symndx;
10921 struct ppc_link_hash_entry *h;
10922
10923 hashes = elf_sym_hashes (htab->params->stub_bfd);
10924 if (hashes == NULL)
10925 {
10926 bfd_size_type hsize;
10927
10928 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10929 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10930 if (hashes == NULL)
10931 return FALSE;
10932 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10933 htab->stub_globals = 1;
10934 }
10935 symndx = htab->stub_globals++;
10936 h = stub_entry->h;
10937 hashes[symndx] = &h->elf;
10938 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10939 if (h->oh != NULL && h->oh->is_func)
10940 h = ppc_follow_link (h->oh);
10941 if (h->elf.root.u.def.section != stub_entry->target_section)
10942 /* H is an opd symbol. The addend must be zero. */
10943 r->r_addend = 0;
10944 else
10945 {
10946 off = (h->elf.root.u.def.value
10947 + h->elf.root.u.def.section->output_offset
10948 + h->elf.root.u.def.section->output_section->vma);
10949 r->r_addend -= off;
10950 }
10951 }
10952 }
10953 break;
10954
10955 case ppc_stub_plt_branch:
10956 case ppc_stub_plt_branch_r2off:
10957 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10958 stub_entry->root.string + 9,
10959 FALSE, FALSE);
10960 if (br_entry == NULL)
10961 {
10962 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10963 stub_entry->root.string);
10964 htab->stub_error = TRUE;
10965 return FALSE;
10966 }
10967
10968 dest = (stub_entry->target_value
10969 + stub_entry->target_section->output_offset
10970 + stub_entry->target_section->output_section->vma);
10971 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10972 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10973
10974 bfd_put_64 (htab->brlt->owner, dest,
10975 htab->brlt->contents + br_entry->offset);
10976
10977 if (br_entry->iter == htab->stub_iteration)
10978 {
10979 br_entry->iter = 0;
10980
10981 if (htab->relbrlt != NULL)
10982 {
10983 /* Create a reloc for the branch lookup table entry. */
10984 Elf_Internal_Rela rela;
10985 bfd_byte *rl;
10986
10987 rela.r_offset = (br_entry->offset
10988 + htab->brlt->output_offset
10989 + htab->brlt->output_section->vma);
10990 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10991 rela.r_addend = dest;
10992
10993 rl = htab->relbrlt->contents;
10994 rl += (htab->relbrlt->reloc_count++
10995 * sizeof (Elf64_External_Rela));
10996 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10997 }
10998 else if (info->emitrelocations)
10999 {
11000 r = get_relocs (htab->brlt, 1);
11001 if (r == NULL)
11002 return FALSE;
11003 /* brlt, being SEC_LINKER_CREATED does not go through the
11004 normal reloc processing. Symbols and offsets are not
11005 translated from input file to output file form, so
11006 set up the offset per the output file. */
11007 r->r_offset = (br_entry->offset
11008 + htab->brlt->output_offset
11009 + htab->brlt->output_section->vma);
11010 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11011 r->r_addend = dest;
11012 }
11013 }
11014
11015 dest = (br_entry->offset
11016 + htab->brlt->output_offset
11017 + htab->brlt->output_section->vma);
11018
11019 off = (dest
11020 - elf_gp (htab->brlt->output_section->owner)
11021 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11022
11023 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11024 {
11025 info->callbacks->einfo
11026 (_("%P: linkage table error against `%T'\n"),
11027 stub_entry->root.string);
11028 bfd_set_error (bfd_error_bad_value);
11029 htab->stub_error = TRUE;
11030 return FALSE;
11031 }
11032
11033 if (info->emitrelocations)
11034 {
11035 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11036 if (r == NULL)
11037 return FALSE;
11038 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11039 if (bfd_big_endian (info->output_bfd))
11040 r[0].r_offset += 2;
11041 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11042 r[0].r_offset += 4;
11043 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11044 r[0].r_addend = dest;
11045 if (PPC_HA (off) != 0)
11046 {
11047 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11048 r[1].r_offset = r[0].r_offset + 4;
11049 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11050 r[1].r_addend = r[0].r_addend;
11051 }
11052 }
11053
11054 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11055 {
11056 if (PPC_HA (off) != 0)
11057 {
11058 size = 16;
11059 bfd_put_32 (htab->params->stub_bfd,
11060 ADDIS_R12_R2 | PPC_HA (off), loc);
11061 loc += 4;
11062 bfd_put_32 (htab->params->stub_bfd,
11063 LD_R12_0R12 | PPC_LO (off), loc);
11064 }
11065 else
11066 {
11067 size = 12;
11068 bfd_put_32 (htab->params->stub_bfd,
11069 LD_R12_0R2 | PPC_LO (off), loc);
11070 }
11071 }
11072 else
11073 {
11074 bfd_vma r2off = get_r2off (info, stub_entry);
11075
11076 if (r2off == (bfd_vma) -1)
11077 {
11078 htab->stub_error = TRUE;
11079 return FALSE;
11080 }
11081
11082 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), loc);
11083 loc += 4;
11084 size = 16;
11085 if (PPC_HA (off) != 0)
11086 {
11087 size += 4;
11088 bfd_put_32 (htab->params->stub_bfd,
11089 ADDIS_R12_R2 | PPC_HA (off), loc);
11090 loc += 4;
11091 bfd_put_32 (htab->params->stub_bfd,
11092 LD_R12_0R12 | PPC_LO (off), loc);
11093 }
11094 else
11095 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
11096
11097 if (PPC_HA (r2off) != 0)
11098 {
11099 size += 4;
11100 loc += 4;
11101 bfd_put_32 (htab->params->stub_bfd,
11102 ADDIS_R2_R2 | PPC_HA (r2off), loc);
11103 }
11104 if (PPC_LO (r2off) != 0)
11105 {
11106 size += 4;
11107 loc += 4;
11108 bfd_put_32 (htab->params->stub_bfd,
11109 ADDI_R2_R2 | PPC_LO (r2off), loc);
11110 }
11111 }
11112 loc += 4;
11113 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, loc);
11114 loc += 4;
11115 bfd_put_32 (htab->params->stub_bfd, BCTR, loc);
11116 break;
11117
11118 case ppc_stub_plt_call:
11119 case ppc_stub_plt_call_r2save:
11120 if (stub_entry->h != NULL
11121 && stub_entry->h->is_func_descriptor
11122 && stub_entry->h->oh != NULL)
11123 {
11124 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11125
11126 /* If the old-ABI "dot-symbol" is undefined make it weak so
11127 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11128 if (fh->elf.root.type == bfd_link_hash_undefined
11129 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11130 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11131 fh->elf.root.type = bfd_link_hash_undefweak;
11132 }
11133
11134 /* Now build the stub. */
11135 dest = stub_entry->plt_ent->plt.offset & ~1;
11136 if (dest >= (bfd_vma) -2)
11137 abort ();
11138
11139 plt = htab->elf.splt;
11140 if (!htab->elf.dynamic_sections_created
11141 || stub_entry->h == NULL
11142 || stub_entry->h->elf.dynindx == -1)
11143 plt = htab->elf.iplt;
11144
11145 dest += plt->output_offset + plt->output_section->vma;
11146
11147 if (stub_entry->h == NULL
11148 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11149 {
11150 Elf_Internal_Rela rela;
11151 bfd_byte *rl;
11152
11153 rela.r_offset = dest;
11154 if (htab->opd_abi)
11155 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11156 else
11157 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11158 rela.r_addend = (stub_entry->target_value
11159 + stub_entry->target_section->output_offset
11160 + stub_entry->target_section->output_section->vma);
11161
11162 rl = (htab->elf.irelplt->contents
11163 + (htab->elf.irelplt->reloc_count++
11164 * sizeof (Elf64_External_Rela)));
11165 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11166 stub_entry->plt_ent->plt.offset |= 1;
11167 }
11168
11169 off = (dest
11170 - elf_gp (plt->output_section->owner)
11171 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11172
11173 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11174 {
11175 info->callbacks->einfo
11176 /* xgettext:c-format */
11177 (_("%P: linkage table error against `%T'\n"),
11178 stub_entry->h != NULL
11179 ? stub_entry->h->elf.root.root.string
11180 : "<local sym>");
11181 bfd_set_error (bfd_error_bad_value);
11182 htab->stub_error = TRUE;
11183 return FALSE;
11184 }
11185
11186 if (htab->params->plt_stub_align != 0)
11187 {
11188 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11189
11190 stub_entry->group->stub_sec->size += pad;
11191 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11192 loc += pad;
11193 }
11194
11195 r = NULL;
11196 if (info->emitrelocations)
11197 {
11198 r = get_relocs (stub_entry->group->stub_sec,
11199 ((PPC_HA (off) != 0)
11200 + (htab->opd_abi
11201 ? 2 + (htab->params->plt_static_chain
11202 && PPC_HA (off + 16) == PPC_HA (off))
11203 : 1)));
11204 if (r == NULL)
11205 return FALSE;
11206 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11207 if (bfd_big_endian (info->output_bfd))
11208 r[0].r_offset += 2;
11209 r[0].r_addend = dest;
11210 }
11211 if (stub_entry->h != NULL
11212 && (stub_entry->h == htab->tls_get_addr_fd
11213 || stub_entry->h == htab->tls_get_addr)
11214 && htab->params->tls_get_addr_opt)
11215 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11216 else
11217 p = build_plt_stub (htab, stub_entry, loc, off, r);
11218 size = p - loc;
11219 break;
11220
11221 case ppc_stub_save_res:
11222 return TRUE;
11223
11224 default:
11225 BFD_FAIL ();
11226 return FALSE;
11227 }
11228
11229 stub_entry->group->stub_sec->size += size;
11230
11231 if (htab->params->emit_stub_syms)
11232 {
11233 struct elf_link_hash_entry *h;
11234 size_t len1, len2;
11235 char *name;
11236 const char *const stub_str[] = { "long_branch",
11237 "long_branch_r2off",
11238 "plt_branch",
11239 "plt_branch_r2off",
11240 "plt_call",
11241 "plt_call" };
11242
11243 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11244 len2 = strlen (stub_entry->root.string);
11245 name = bfd_malloc (len1 + len2 + 2);
11246 if (name == NULL)
11247 return FALSE;
11248 memcpy (name, stub_entry->root.string, 9);
11249 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11250 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11251 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11252 if (h == NULL)
11253 return FALSE;
11254 if (h->root.type == bfd_link_hash_new)
11255 {
11256 h->root.type = bfd_link_hash_defined;
11257 h->root.u.def.section = stub_entry->group->stub_sec;
11258 h->root.u.def.value = stub_entry->stub_offset;
11259 h->ref_regular = 1;
11260 h->def_regular = 1;
11261 h->ref_regular_nonweak = 1;
11262 h->forced_local = 1;
11263 h->non_elf = 0;
11264 h->root.linker_def = 1;
11265 }
11266 }
11267
11268 return TRUE;
11269 }
11270
11271 /* As above, but don't actually build the stub. Just bump offset so
11272 we know stub section sizes, and select plt_branch stubs where
11273 long_branch stubs won't do. */
11274
11275 static bfd_boolean
11276 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11277 {
11278 struct ppc_stub_hash_entry *stub_entry;
11279 struct bfd_link_info *info;
11280 struct ppc_link_hash_table *htab;
11281 bfd_vma off;
11282 int size;
11283
11284 /* Massage our args to the form they really have. */
11285 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11286 info = in_arg;
11287
11288 htab = ppc_hash_table (info);
11289 if (htab == NULL)
11290 return FALSE;
11291
11292 if (stub_entry->h != NULL
11293 && stub_entry->h->save_res
11294 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11295 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11296 {
11297 /* Don't make stubs to out-of-line register save/restore
11298 functions. Instead, emit copies of the functions. */
11299 stub_entry->group->needs_save_res = 1;
11300 stub_entry->stub_type = ppc_stub_save_res;
11301 return TRUE;
11302 }
11303
11304 if (stub_entry->stub_type == ppc_stub_plt_call
11305 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11306 {
11307 asection *plt;
11308 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11309 if (off >= (bfd_vma) -2)
11310 abort ();
11311 plt = htab->elf.splt;
11312 if (!htab->elf.dynamic_sections_created
11313 || stub_entry->h == NULL
11314 || stub_entry->h->elf.dynindx == -1)
11315 plt = htab->elf.iplt;
11316 off += (plt->output_offset
11317 + plt->output_section->vma
11318 - elf_gp (plt->output_section->owner)
11319 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11320
11321 size = plt_stub_size (htab, stub_entry, off);
11322 if (htab->params->plt_stub_align)
11323 size += plt_stub_pad (htab, stub_entry, off);
11324 if (info->emitrelocations)
11325 {
11326 stub_entry->group->stub_sec->reloc_count
11327 += ((PPC_HA (off) != 0)
11328 + (htab->opd_abi
11329 ? 2 + (htab->params->plt_static_chain
11330 && PPC_HA (off + 16) == PPC_HA (off))
11331 : 1));
11332 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11333 }
11334 }
11335 else
11336 {
11337 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11338 variants. */
11339 bfd_vma r2off = 0;
11340 bfd_vma local_off = 0;
11341
11342 off = (stub_entry->target_value
11343 + stub_entry->target_section->output_offset
11344 + stub_entry->target_section->output_section->vma);
11345 off -= (stub_entry->group->stub_sec->size
11346 + stub_entry->group->stub_sec->output_offset
11347 + stub_entry->group->stub_sec->output_section->vma);
11348
11349 /* Reset the stub type from the plt variant in case we now
11350 can reach with a shorter stub. */
11351 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11352 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11353
11354 size = 4;
11355 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11356 {
11357 r2off = get_r2off (info, stub_entry);
11358 if (r2off == (bfd_vma) -1)
11359 {
11360 htab->stub_error = TRUE;
11361 return FALSE;
11362 }
11363 size = 8;
11364 if (PPC_HA (r2off) != 0)
11365 size += 4;
11366 if (PPC_LO (r2off) != 0)
11367 size += 4;
11368 off -= size - 4;
11369 }
11370
11371 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11372
11373 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11374 Do the same for -R objects without function descriptors. */
11375 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11376 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11377 && r2off == 0
11378 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11379 {
11380 struct ppc_branch_hash_entry *br_entry;
11381
11382 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11383 stub_entry->root.string + 9,
11384 TRUE, FALSE);
11385 if (br_entry == NULL)
11386 {
11387 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11388 stub_entry->root.string);
11389 htab->stub_error = TRUE;
11390 return FALSE;
11391 }
11392
11393 if (br_entry->iter != htab->stub_iteration)
11394 {
11395 br_entry->iter = htab->stub_iteration;
11396 br_entry->offset = htab->brlt->size;
11397 htab->brlt->size += 8;
11398
11399 if (htab->relbrlt != NULL)
11400 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11401 else if (info->emitrelocations)
11402 {
11403 htab->brlt->reloc_count += 1;
11404 htab->brlt->flags |= SEC_RELOC;
11405 }
11406 }
11407
11408 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11409 off = (br_entry->offset
11410 + htab->brlt->output_offset
11411 + htab->brlt->output_section->vma
11412 - elf_gp (htab->brlt->output_section->owner)
11413 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11414
11415 if (info->emitrelocations)
11416 {
11417 stub_entry->group->stub_sec->reloc_count
11418 += 1 + (PPC_HA (off) != 0);
11419 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11420 }
11421
11422 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11423 {
11424 size = 12;
11425 if (PPC_HA (off) != 0)
11426 size = 16;
11427 }
11428 else
11429 {
11430 size = 16;
11431 if (PPC_HA (off) != 0)
11432 size += 4;
11433
11434 if (PPC_HA (r2off) != 0)
11435 size += 4;
11436 if (PPC_LO (r2off) != 0)
11437 size += 4;
11438 }
11439 }
11440 else if (info->emitrelocations)
11441 {
11442 stub_entry->group->stub_sec->reloc_count += 1;
11443 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11444 }
11445 }
11446
11447 stub_entry->group->stub_sec->size += size;
11448 return TRUE;
11449 }
11450
11451 /* Set up various things so that we can make a list of input sections
11452 for each output section included in the link. Returns -1 on error,
11453 0 when no stubs will be needed, and 1 on success. */
11454
11455 int
11456 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11457 {
11458 unsigned int id;
11459 bfd_size_type amt;
11460 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11461
11462 if (htab == NULL)
11463 return -1;
11464
11465 htab->sec_info_arr_size = bfd_get_next_section_id ();
11466 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11467 htab->sec_info = bfd_zmalloc (amt);
11468 if (htab->sec_info == NULL)
11469 return -1;
11470
11471 /* Set toc_off for com, und, abs and ind sections. */
11472 for (id = 0; id < 3; id++)
11473 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11474
11475 return 1;
11476 }
11477
11478 /* Set up for first pass at multitoc partitioning. */
11479
11480 void
11481 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11482 {
11483 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11484
11485 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11486 htab->toc_bfd = NULL;
11487 htab->toc_first_sec = NULL;
11488 }
11489
11490 /* The linker repeatedly calls this function for each TOC input section
11491 and linker generated GOT section. Group input bfds such that the toc
11492 within a group is less than 64k in size. */
11493
11494 bfd_boolean
11495 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11496 {
11497 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11498 bfd_vma addr, off, limit;
11499
11500 if (htab == NULL)
11501 return FALSE;
11502
11503 if (!htab->second_toc_pass)
11504 {
11505 /* Keep track of the first .toc or .got section for this input bfd. */
11506 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11507
11508 if (new_bfd)
11509 {
11510 htab->toc_bfd = isec->owner;
11511 htab->toc_first_sec = isec;
11512 }
11513
11514 addr = isec->output_offset + isec->output_section->vma;
11515 off = addr - htab->toc_curr;
11516 limit = 0x80008000;
11517 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11518 limit = 0x10000;
11519 if (off + isec->size > limit)
11520 {
11521 addr = (htab->toc_first_sec->output_offset
11522 + htab->toc_first_sec->output_section->vma);
11523 htab->toc_curr = addr;
11524 htab->toc_curr &= -TOC_BASE_ALIGN;
11525 }
11526
11527 /* toc_curr is the base address of this toc group. Set elf_gp
11528 for the input section to be the offset relative to the
11529 output toc base plus 0x8000. Making the input elf_gp an
11530 offset allows us to move the toc as a whole without
11531 recalculating input elf_gp. */
11532 off = htab->toc_curr - elf_gp (isec->output_section->owner);
11533 off += TOC_BASE_OFF;
11534
11535 /* Die if someone uses a linker script that doesn't keep input
11536 file .toc and .got together. */
11537 if (new_bfd
11538 && elf_gp (isec->owner) != 0
11539 && elf_gp (isec->owner) != off)
11540 return FALSE;
11541
11542 elf_gp (isec->owner) = off;
11543 return TRUE;
11544 }
11545
11546 /* During the second pass toc_first_sec points to the start of
11547 a toc group, and toc_curr is used to track the old elf_gp.
11548 We use toc_bfd to ensure we only look at each bfd once. */
11549 if (htab->toc_bfd == isec->owner)
11550 return TRUE;
11551 htab->toc_bfd = isec->owner;
11552
11553 if (htab->toc_first_sec == NULL
11554 || htab->toc_curr != elf_gp (isec->owner))
11555 {
11556 htab->toc_curr = elf_gp (isec->owner);
11557 htab->toc_first_sec = isec;
11558 }
11559 addr = (htab->toc_first_sec->output_offset
11560 + htab->toc_first_sec->output_section->vma);
11561 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
11562 elf_gp (isec->owner) = off;
11563
11564 return TRUE;
11565 }
11566
11567 /* Called via elf_link_hash_traverse to merge GOT entries for global
11568 symbol H. */
11569
11570 static bfd_boolean
11571 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11572 {
11573 if (h->root.type == bfd_link_hash_indirect)
11574 return TRUE;
11575
11576 merge_got_entries (&h->got.glist);
11577
11578 return TRUE;
11579 }
11580
11581 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11582 symbol H. */
11583
11584 static bfd_boolean
11585 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11586 {
11587 struct got_entry *gent;
11588
11589 if (h->root.type == bfd_link_hash_indirect)
11590 return TRUE;
11591
11592 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11593 if (!gent->is_indirect)
11594 allocate_got (h, (struct bfd_link_info *) inf, gent);
11595 return TRUE;
11596 }
11597
11598 /* Called on the first multitoc pass after the last call to
11599 ppc64_elf_next_toc_section. This function removes duplicate GOT
11600 entries. */
11601
11602 bfd_boolean
11603 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11604 {
11605 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11606 struct bfd *ibfd, *ibfd2;
11607 bfd_boolean done_something;
11608
11609 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11610
11611 if (!htab->do_multi_toc)
11612 return FALSE;
11613
11614 /* Merge global sym got entries within a toc group. */
11615 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11616
11617 /* And tlsld_got. */
11618 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11619 {
11620 struct got_entry *ent, *ent2;
11621
11622 if (!is_ppc64_elf (ibfd))
11623 continue;
11624
11625 ent = ppc64_tlsld_got (ibfd);
11626 if (!ent->is_indirect
11627 && ent->got.offset != (bfd_vma) -1)
11628 {
11629 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11630 {
11631 if (!is_ppc64_elf (ibfd2))
11632 continue;
11633
11634 ent2 = ppc64_tlsld_got (ibfd2);
11635 if (!ent2->is_indirect
11636 && ent2->got.offset != (bfd_vma) -1
11637 && elf_gp (ibfd2) == elf_gp (ibfd))
11638 {
11639 ent2->is_indirect = TRUE;
11640 ent2->got.ent = ent;
11641 }
11642 }
11643 }
11644 }
11645
11646 /* Zap sizes of got sections. */
11647 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11648 htab->elf.irelplt->size -= htab->got_reli_size;
11649 htab->got_reli_size = 0;
11650
11651 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11652 {
11653 asection *got, *relgot;
11654
11655 if (!is_ppc64_elf (ibfd))
11656 continue;
11657
11658 got = ppc64_elf_tdata (ibfd)->got;
11659 if (got != NULL)
11660 {
11661 got->rawsize = got->size;
11662 got->size = 0;
11663 relgot = ppc64_elf_tdata (ibfd)->relgot;
11664 relgot->rawsize = relgot->size;
11665 relgot->size = 0;
11666 }
11667 }
11668
11669 /* Now reallocate the got, local syms first. We don't need to
11670 allocate section contents again since we never increase size. */
11671 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11672 {
11673 struct got_entry **lgot_ents;
11674 struct got_entry **end_lgot_ents;
11675 struct plt_entry **local_plt;
11676 struct plt_entry **end_local_plt;
11677 unsigned char *lgot_masks;
11678 bfd_size_type locsymcount;
11679 Elf_Internal_Shdr *symtab_hdr;
11680 asection *s;
11681
11682 if (!is_ppc64_elf (ibfd))
11683 continue;
11684
11685 lgot_ents = elf_local_got_ents (ibfd);
11686 if (!lgot_ents)
11687 continue;
11688
11689 symtab_hdr = &elf_symtab_hdr (ibfd);
11690 locsymcount = symtab_hdr->sh_info;
11691 end_lgot_ents = lgot_ents + locsymcount;
11692 local_plt = (struct plt_entry **) end_lgot_ents;
11693 end_local_plt = local_plt + locsymcount;
11694 lgot_masks = (unsigned char *) end_local_plt;
11695 s = ppc64_elf_tdata (ibfd)->got;
11696 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11697 {
11698 struct got_entry *ent;
11699
11700 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11701 {
11702 unsigned int ent_size = 8;
11703 unsigned int rel_size = sizeof (Elf64_External_Rela);
11704
11705 ent->got.offset = s->size;
11706 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11707 {
11708 ent_size *= 2;
11709 rel_size *= 2;
11710 }
11711 s->size += ent_size;
11712 if ((*lgot_masks & PLT_IFUNC) != 0)
11713 {
11714 htab->elf.irelplt->size += rel_size;
11715 htab->got_reli_size += rel_size;
11716 }
11717 else if (bfd_link_pic (info))
11718 {
11719 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11720 srel->size += rel_size;
11721 }
11722 }
11723 }
11724 }
11725
11726 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11727
11728 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11729 {
11730 struct got_entry *ent;
11731
11732 if (!is_ppc64_elf (ibfd))
11733 continue;
11734
11735 ent = ppc64_tlsld_got (ibfd);
11736 if (!ent->is_indirect
11737 && ent->got.offset != (bfd_vma) -1)
11738 {
11739 asection *s = ppc64_elf_tdata (ibfd)->got;
11740 ent->got.offset = s->size;
11741 s->size += 16;
11742 if (bfd_link_pic (info))
11743 {
11744 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11745 srel->size += sizeof (Elf64_External_Rela);
11746 }
11747 }
11748 }
11749
11750 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11751 if (!done_something)
11752 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11753 {
11754 asection *got;
11755
11756 if (!is_ppc64_elf (ibfd))
11757 continue;
11758
11759 got = ppc64_elf_tdata (ibfd)->got;
11760 if (got != NULL)
11761 {
11762 done_something = got->rawsize != got->size;
11763 if (done_something)
11764 break;
11765 }
11766 }
11767
11768 if (done_something)
11769 (*htab->params->layout_sections_again) ();
11770
11771 /* Set up for second pass over toc sections to recalculate elf_gp
11772 on input sections. */
11773 htab->toc_bfd = NULL;
11774 htab->toc_first_sec = NULL;
11775 htab->second_toc_pass = TRUE;
11776 return done_something;
11777 }
11778
11779 /* Called after second pass of multitoc partitioning. */
11780
11781 void
11782 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11783 {
11784 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11785
11786 /* After the second pass, toc_curr tracks the TOC offset used
11787 for code sections below in ppc64_elf_next_input_section. */
11788 htab->toc_curr = TOC_BASE_OFF;
11789 }
11790
11791 /* No toc references were found in ISEC. If the code in ISEC makes no
11792 calls, then there's no need to use toc adjusting stubs when branching
11793 into ISEC. Actually, indirect calls from ISEC are OK as they will
11794 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11795 needed, and 2 if a cyclical call-graph was found but no other reason
11796 for a stub was detected. If called from the top level, a return of
11797 2 means the same as a return of 0. */
11798
11799 static int
11800 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11801 {
11802 int ret;
11803
11804 /* Mark this section as checked. */
11805 isec->call_check_done = 1;
11806
11807 /* We know none of our code bearing sections will need toc stubs. */
11808 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11809 return 0;
11810
11811 if (isec->size == 0)
11812 return 0;
11813
11814 if (isec->output_section == NULL)
11815 return 0;
11816
11817 ret = 0;
11818 if (isec->reloc_count != 0)
11819 {
11820 Elf_Internal_Rela *relstart, *rel;
11821 Elf_Internal_Sym *local_syms;
11822 struct ppc_link_hash_table *htab;
11823
11824 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11825 info->keep_memory);
11826 if (relstart == NULL)
11827 return -1;
11828
11829 /* Look for branches to outside of this section. */
11830 local_syms = NULL;
11831 htab = ppc_hash_table (info);
11832 if (htab == NULL)
11833 return -1;
11834
11835 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11836 {
11837 enum elf_ppc64_reloc_type r_type;
11838 unsigned long r_symndx;
11839 struct elf_link_hash_entry *h;
11840 struct ppc_link_hash_entry *eh;
11841 Elf_Internal_Sym *sym;
11842 asection *sym_sec;
11843 struct _opd_sec_data *opd;
11844 bfd_vma sym_value;
11845 bfd_vma dest;
11846
11847 r_type = ELF64_R_TYPE (rel->r_info);
11848 if (r_type != R_PPC64_REL24
11849 && r_type != R_PPC64_REL14
11850 && r_type != R_PPC64_REL14_BRTAKEN
11851 && r_type != R_PPC64_REL14_BRNTAKEN)
11852 continue;
11853
11854 r_symndx = ELF64_R_SYM (rel->r_info);
11855 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11856 isec->owner))
11857 {
11858 ret = -1;
11859 break;
11860 }
11861
11862 /* Calls to dynamic lib functions go through a plt call stub
11863 that uses r2. */
11864 eh = (struct ppc_link_hash_entry *) h;
11865 if (eh != NULL
11866 && (eh->elf.plt.plist != NULL
11867 || (eh->oh != NULL
11868 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11869 {
11870 ret = 1;
11871 break;
11872 }
11873
11874 if (sym_sec == NULL)
11875 /* Ignore other undefined symbols. */
11876 continue;
11877
11878 /* Assume branches to other sections not included in the
11879 link need stubs too, to cover -R and absolute syms. */
11880 if (sym_sec->output_section == NULL)
11881 {
11882 ret = 1;
11883 break;
11884 }
11885
11886 if (h == NULL)
11887 sym_value = sym->st_value;
11888 else
11889 {
11890 if (h->root.type != bfd_link_hash_defined
11891 && h->root.type != bfd_link_hash_defweak)
11892 abort ();
11893 sym_value = h->root.u.def.value;
11894 }
11895 sym_value += rel->r_addend;
11896
11897 /* If this branch reloc uses an opd sym, find the code section. */
11898 opd = get_opd_info (sym_sec);
11899 if (opd != NULL)
11900 {
11901 if (h == NULL && opd->adjust != NULL)
11902 {
11903 long adjust;
11904
11905 adjust = opd->adjust[OPD_NDX (sym_value)];
11906 if (adjust == -1)
11907 /* Assume deleted functions won't ever be called. */
11908 continue;
11909 sym_value += adjust;
11910 }
11911
11912 dest = opd_entry_value (sym_sec, sym_value,
11913 &sym_sec, NULL, FALSE);
11914 if (dest == (bfd_vma) -1)
11915 continue;
11916 }
11917 else
11918 dest = (sym_value
11919 + sym_sec->output_offset
11920 + sym_sec->output_section->vma);
11921
11922 /* Ignore branch to self. */
11923 if (sym_sec == isec)
11924 continue;
11925
11926 /* If the called function uses the toc, we need a stub. */
11927 if (sym_sec->has_toc_reloc
11928 || sym_sec->makes_toc_func_call)
11929 {
11930 ret = 1;
11931 break;
11932 }
11933
11934 /* Assume any branch that needs a long branch stub might in fact
11935 need a plt_branch stub. A plt_branch stub uses r2. */
11936 else if (dest - (isec->output_offset
11937 + isec->output_section->vma
11938 + rel->r_offset) + (1 << 25)
11939 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11940 ? h->other
11941 : sym->st_other))
11942 {
11943 ret = 1;
11944 break;
11945 }
11946
11947 /* If calling back to a section in the process of being
11948 tested, we can't say for sure that no toc adjusting stubs
11949 are needed, so don't return zero. */
11950 else if (sym_sec->call_check_in_progress)
11951 ret = 2;
11952
11953 /* Branches to another section that itself doesn't have any TOC
11954 references are OK. Recursively call ourselves to check. */
11955 else if (!sym_sec->call_check_done)
11956 {
11957 int recur;
11958
11959 /* Mark current section as indeterminate, so that other
11960 sections that call back to current won't be marked as
11961 known. */
11962 isec->call_check_in_progress = 1;
11963 recur = toc_adjusting_stub_needed (info, sym_sec);
11964 isec->call_check_in_progress = 0;
11965
11966 if (recur != 0)
11967 {
11968 ret = recur;
11969 if (recur != 2)
11970 break;
11971 }
11972 }
11973 }
11974
11975 if (local_syms != NULL
11976 && (elf_symtab_hdr (isec->owner).contents
11977 != (unsigned char *) local_syms))
11978 free (local_syms);
11979 if (elf_section_data (isec)->relocs != relstart)
11980 free (relstart);
11981 }
11982
11983 if ((ret & 1) == 0
11984 && isec->map_head.s != NULL
11985 && (strcmp (isec->output_section->name, ".init") == 0
11986 || strcmp (isec->output_section->name, ".fini") == 0))
11987 {
11988 if (isec->map_head.s->has_toc_reloc
11989 || isec->map_head.s->makes_toc_func_call)
11990 ret = 1;
11991 else if (!isec->map_head.s->call_check_done)
11992 {
11993 int recur;
11994 isec->call_check_in_progress = 1;
11995 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11996 isec->call_check_in_progress = 0;
11997 if (recur != 0)
11998 ret = recur;
11999 }
12000 }
12001
12002 if (ret == 1)
12003 isec->makes_toc_func_call = 1;
12004
12005 return ret;
12006 }
12007
12008 /* The linker repeatedly calls this function for each input section,
12009 in the order that input sections are linked into output sections.
12010 Build lists of input sections to determine groupings between which
12011 we may insert linker stubs. */
12012
12013 bfd_boolean
12014 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12015 {
12016 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12017
12018 if (htab == NULL)
12019 return FALSE;
12020
12021 if ((isec->output_section->flags & SEC_CODE) != 0
12022 && isec->output_section->id < htab->sec_info_arr_size)
12023 {
12024 /* This happens to make the list in reverse order,
12025 which is what we want. */
12026 htab->sec_info[isec->id].u.list
12027 = htab->sec_info[isec->output_section->id].u.list;
12028 htab->sec_info[isec->output_section->id].u.list = isec;
12029 }
12030
12031 if (htab->multi_toc_needed)
12032 {
12033 /* Analyse sections that aren't already flagged as needing a
12034 valid toc pointer. Exclude .fixup for the linux kernel.
12035 .fixup contains branches, but only back to the function that
12036 hit an exception. */
12037 if (!(isec->has_toc_reloc
12038 || (isec->flags & SEC_CODE) == 0
12039 || strcmp (isec->name, ".fixup") == 0
12040 || isec->call_check_done))
12041 {
12042 if (toc_adjusting_stub_needed (info, isec) < 0)
12043 return FALSE;
12044 }
12045 /* Make all sections use the TOC assigned for this object file.
12046 This will be wrong for pasted sections; We fix that in
12047 check_pasted_section(). */
12048 if (elf_gp (isec->owner) != 0)
12049 htab->toc_curr = elf_gp (isec->owner);
12050 }
12051
12052 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12053 return TRUE;
12054 }
12055
12056 /* Check that all .init and .fini sections use the same toc, if they
12057 have toc relocs. */
12058
12059 static bfd_boolean
12060 check_pasted_section (struct bfd_link_info *info, const char *name)
12061 {
12062 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12063
12064 if (o != NULL)
12065 {
12066 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12067 bfd_vma toc_off = 0;
12068 asection *i;
12069
12070 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12071 if (i->has_toc_reloc)
12072 {
12073 if (toc_off == 0)
12074 toc_off = htab->sec_info[i->id].toc_off;
12075 else if (toc_off != htab->sec_info[i->id].toc_off)
12076 return FALSE;
12077 }
12078
12079 if (toc_off == 0)
12080 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12081 if (i->makes_toc_func_call)
12082 {
12083 toc_off = htab->sec_info[i->id].toc_off;
12084 break;
12085 }
12086
12087 /* Make sure the whole pasted function uses the same toc offset. */
12088 if (toc_off != 0)
12089 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12090 htab->sec_info[i->id].toc_off = toc_off;
12091 }
12092 return TRUE;
12093 }
12094
12095 bfd_boolean
12096 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12097 {
12098 return (check_pasted_section (info, ".init")
12099 & check_pasted_section (info, ".fini"));
12100 }
12101
12102 /* See whether we can group stub sections together. Grouping stub
12103 sections may result in fewer stubs. More importantly, we need to
12104 put all .init* and .fini* stubs at the beginning of the .init or
12105 .fini output sections respectively, because glibc splits the
12106 _init and _fini functions into multiple parts. Putting a stub in
12107 the middle of a function is not a good idea. */
12108
12109 static bfd_boolean
12110 group_sections (struct bfd_link_info *info,
12111 bfd_size_type stub_group_size,
12112 bfd_boolean stubs_always_before_branch)
12113 {
12114 struct ppc_link_hash_table *htab;
12115 asection *osec;
12116 bfd_boolean suppress_size_errors;
12117
12118 htab = ppc_hash_table (info);
12119 if (htab == NULL)
12120 return FALSE;
12121
12122 suppress_size_errors = FALSE;
12123 if (stub_group_size == 1)
12124 {
12125 /* Default values. */
12126 if (stubs_always_before_branch)
12127 stub_group_size = 0x1e00000;
12128 else
12129 stub_group_size = 0x1c00000;
12130 suppress_size_errors = TRUE;
12131 }
12132
12133 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12134 {
12135 asection *tail;
12136
12137 if (osec->id >= htab->sec_info_arr_size)
12138 continue;
12139
12140 tail = htab->sec_info[osec->id].u.list;
12141 while (tail != NULL)
12142 {
12143 asection *curr;
12144 asection *prev;
12145 bfd_size_type total;
12146 bfd_boolean big_sec;
12147 bfd_vma curr_toc;
12148 struct map_stub *group;
12149 bfd_size_type group_size;
12150
12151 curr = tail;
12152 total = tail->size;
12153 group_size = (ppc64_elf_section_data (tail) != NULL
12154 && ppc64_elf_section_data (tail)->has_14bit_branch
12155 ? stub_group_size >> 10 : stub_group_size);
12156
12157 big_sec = total > group_size;
12158 if (big_sec && !suppress_size_errors)
12159 /* xgettext:c-format */
12160 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12161 tail->owner, tail);
12162 curr_toc = htab->sec_info[tail->id].toc_off;
12163
12164 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12165 && ((total += curr->output_offset - prev->output_offset)
12166 < (ppc64_elf_section_data (prev) != NULL
12167 && ppc64_elf_section_data (prev)->has_14bit_branch
12168 ? (group_size = stub_group_size >> 10) : group_size))
12169 && htab->sec_info[prev->id].toc_off == curr_toc)
12170 curr = prev;
12171
12172 /* OK, the size from the start of CURR to the end is less
12173 than group_size and thus can be handled by one stub
12174 section. (or the tail section is itself larger than
12175 group_size, in which case we may be toast.) We should
12176 really be keeping track of the total size of stubs added
12177 here, as stubs contribute to the final output section
12178 size. That's a little tricky, and this way will only
12179 break if stubs added make the total size more than 2^25,
12180 ie. for the default stub_group_size, if stubs total more
12181 than 2097152 bytes, or nearly 75000 plt call stubs. */
12182 group = bfd_alloc (curr->owner, sizeof (*group));
12183 if (group == NULL)
12184 return FALSE;
12185 group->link_sec = curr;
12186 group->stub_sec = NULL;
12187 group->needs_save_res = 0;
12188 group->next = htab->group;
12189 htab->group = group;
12190 do
12191 {
12192 prev = htab->sec_info[tail->id].u.list;
12193 /* Set up this stub group. */
12194 htab->sec_info[tail->id].u.group = group;
12195 }
12196 while (tail != curr && (tail = prev) != NULL);
12197
12198 /* But wait, there's more! Input sections up to group_size
12199 bytes before the stub section can be handled by it too.
12200 Don't do this if we have a really large section after the
12201 stubs, as adding more stubs increases the chance that
12202 branches may not reach into the stub section. */
12203 if (!stubs_always_before_branch && !big_sec)
12204 {
12205 total = 0;
12206 while (prev != NULL
12207 && ((total += tail->output_offset - prev->output_offset)
12208 < (ppc64_elf_section_data (prev) != NULL
12209 && ppc64_elf_section_data (prev)->has_14bit_branch
12210 ? (group_size = stub_group_size >> 10) : group_size))
12211 && htab->sec_info[prev->id].toc_off == curr_toc)
12212 {
12213 tail = prev;
12214 prev = htab->sec_info[tail->id].u.list;
12215 htab->sec_info[tail->id].u.group = group;
12216 }
12217 }
12218 tail = prev;
12219 }
12220 }
12221 return TRUE;
12222 }
12223
12224 static const unsigned char glink_eh_frame_cie[] =
12225 {
12226 0, 0, 0, 16, /* length. */
12227 0, 0, 0, 0, /* id. */
12228 1, /* CIE version. */
12229 'z', 'R', 0, /* Augmentation string. */
12230 4, /* Code alignment. */
12231 0x78, /* Data alignment. */
12232 65, /* RA reg. */
12233 1, /* Augmentation size. */
12234 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12235 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12236 };
12237
12238 /* Stripping output sections is normally done before dynamic section
12239 symbols have been allocated. This function is called later, and
12240 handles cases like htab->brlt which is mapped to its own output
12241 section. */
12242
12243 static void
12244 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12245 {
12246 if (isec->size == 0
12247 && isec->output_section->size == 0
12248 && !(isec->output_section->flags & SEC_KEEP)
12249 && !bfd_section_removed_from_list (info->output_bfd,
12250 isec->output_section)
12251 && elf_section_data (isec->output_section)->dynindx == 0)
12252 {
12253 isec->output_section->flags |= SEC_EXCLUDE;
12254 bfd_section_list_remove (info->output_bfd, isec->output_section);
12255 info->output_bfd->section_count--;
12256 }
12257 }
12258
12259 /* Determine and set the size of the stub section for a final link.
12260
12261 The basic idea here is to examine all the relocations looking for
12262 PC-relative calls to a target that is unreachable with a "bl"
12263 instruction. */
12264
12265 bfd_boolean
12266 ppc64_elf_size_stubs (struct bfd_link_info *info)
12267 {
12268 bfd_size_type stub_group_size;
12269 bfd_boolean stubs_always_before_branch;
12270 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12271
12272 if (htab == NULL)
12273 return FALSE;
12274
12275 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12276 htab->params->plt_thread_safe = 1;
12277 if (!htab->opd_abi)
12278 htab->params->plt_thread_safe = 0;
12279 else if (htab->params->plt_thread_safe == -1)
12280 {
12281 static const char *const thread_starter[] =
12282 {
12283 "pthread_create",
12284 /* libstdc++ */
12285 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12286 /* librt */
12287 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12288 "mq_notify", "create_timer",
12289 /* libanl */
12290 "getaddrinfo_a",
12291 /* libgomp */
12292 "GOMP_parallel",
12293 "GOMP_parallel_start",
12294 "GOMP_parallel_loop_static",
12295 "GOMP_parallel_loop_static_start",
12296 "GOMP_parallel_loop_dynamic",
12297 "GOMP_parallel_loop_dynamic_start",
12298 "GOMP_parallel_loop_guided",
12299 "GOMP_parallel_loop_guided_start",
12300 "GOMP_parallel_loop_runtime",
12301 "GOMP_parallel_loop_runtime_start",
12302 "GOMP_parallel_sections",
12303 "GOMP_parallel_sections_start",
12304 /* libgo */
12305 "__go_go",
12306 };
12307 unsigned i;
12308
12309 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12310 {
12311 struct elf_link_hash_entry *h;
12312 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12313 FALSE, FALSE, TRUE);
12314 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12315 if (htab->params->plt_thread_safe)
12316 break;
12317 }
12318 }
12319 stubs_always_before_branch = htab->params->group_size < 0;
12320 if (htab->params->group_size < 0)
12321 stub_group_size = -htab->params->group_size;
12322 else
12323 stub_group_size = htab->params->group_size;
12324
12325 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12326 return FALSE;
12327
12328 #define STUB_SHRINK_ITER 20
12329 /* Loop until no stubs added. After iteration 20 of this loop we may
12330 exit on a stub section shrinking. This is to break out of a
12331 pathological case where adding stubs on one iteration decreases
12332 section gaps (perhaps due to alignment), which then requires
12333 fewer or smaller stubs on the next iteration. */
12334
12335 while (1)
12336 {
12337 bfd *input_bfd;
12338 unsigned int bfd_indx;
12339 struct map_stub *group;
12340 asection *stub_sec;
12341
12342 htab->stub_iteration += 1;
12343
12344 for (input_bfd = info->input_bfds, bfd_indx = 0;
12345 input_bfd != NULL;
12346 input_bfd = input_bfd->link.next, bfd_indx++)
12347 {
12348 Elf_Internal_Shdr *symtab_hdr;
12349 asection *section;
12350 Elf_Internal_Sym *local_syms = NULL;
12351
12352 if (!is_ppc64_elf (input_bfd))
12353 continue;
12354
12355 /* We'll need the symbol table in a second. */
12356 symtab_hdr = &elf_symtab_hdr (input_bfd);
12357 if (symtab_hdr->sh_info == 0)
12358 continue;
12359
12360 /* Walk over each section attached to the input bfd. */
12361 for (section = input_bfd->sections;
12362 section != NULL;
12363 section = section->next)
12364 {
12365 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12366
12367 /* If there aren't any relocs, then there's nothing more
12368 to do. */
12369 if ((section->flags & SEC_RELOC) == 0
12370 || (section->flags & SEC_ALLOC) == 0
12371 || (section->flags & SEC_LOAD) == 0
12372 || (section->flags & SEC_CODE) == 0
12373 || section->reloc_count == 0)
12374 continue;
12375
12376 /* If this section is a link-once section that will be
12377 discarded, then don't create any stubs. */
12378 if (section->output_section == NULL
12379 || section->output_section->owner != info->output_bfd)
12380 continue;
12381
12382 /* Get the relocs. */
12383 internal_relocs
12384 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12385 info->keep_memory);
12386 if (internal_relocs == NULL)
12387 goto error_ret_free_local;
12388
12389 /* Now examine each relocation. */
12390 irela = internal_relocs;
12391 irelaend = irela + section->reloc_count;
12392 for (; irela < irelaend; irela++)
12393 {
12394 enum elf_ppc64_reloc_type r_type;
12395 unsigned int r_indx;
12396 enum ppc_stub_type stub_type;
12397 struct ppc_stub_hash_entry *stub_entry;
12398 asection *sym_sec, *code_sec;
12399 bfd_vma sym_value, code_value;
12400 bfd_vma destination;
12401 unsigned long local_off;
12402 bfd_boolean ok_dest;
12403 struct ppc_link_hash_entry *hash;
12404 struct ppc_link_hash_entry *fdh;
12405 struct elf_link_hash_entry *h;
12406 Elf_Internal_Sym *sym;
12407 char *stub_name;
12408 const asection *id_sec;
12409 struct _opd_sec_data *opd;
12410 struct plt_entry *plt_ent;
12411
12412 r_type = ELF64_R_TYPE (irela->r_info);
12413 r_indx = ELF64_R_SYM (irela->r_info);
12414
12415 if (r_type >= R_PPC64_max)
12416 {
12417 bfd_set_error (bfd_error_bad_value);
12418 goto error_ret_free_internal;
12419 }
12420
12421 /* Only look for stubs on branch instructions. */
12422 if (r_type != R_PPC64_REL24
12423 && r_type != R_PPC64_REL14
12424 && r_type != R_PPC64_REL14_BRTAKEN
12425 && r_type != R_PPC64_REL14_BRNTAKEN)
12426 continue;
12427
12428 /* Now determine the call target, its name, value,
12429 section. */
12430 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12431 r_indx, input_bfd))
12432 goto error_ret_free_internal;
12433 hash = (struct ppc_link_hash_entry *) h;
12434
12435 ok_dest = FALSE;
12436 fdh = NULL;
12437 sym_value = 0;
12438 if (hash == NULL)
12439 {
12440 sym_value = sym->st_value;
12441 if (sym_sec != NULL
12442 && sym_sec->output_section != NULL)
12443 ok_dest = TRUE;
12444 }
12445 else if (hash->elf.root.type == bfd_link_hash_defined
12446 || hash->elf.root.type == bfd_link_hash_defweak)
12447 {
12448 sym_value = hash->elf.root.u.def.value;
12449 if (sym_sec->output_section != NULL)
12450 ok_dest = TRUE;
12451 }
12452 else if (hash->elf.root.type == bfd_link_hash_undefweak
12453 || hash->elf.root.type == bfd_link_hash_undefined)
12454 {
12455 /* Recognise an old ABI func code entry sym, and
12456 use the func descriptor sym instead if it is
12457 defined. */
12458 if (hash->elf.root.root.string[0] == '.'
12459 && hash->oh != NULL)
12460 {
12461 fdh = ppc_follow_link (hash->oh);
12462 if (fdh->elf.root.type == bfd_link_hash_defined
12463 || fdh->elf.root.type == bfd_link_hash_defweak)
12464 {
12465 sym_sec = fdh->elf.root.u.def.section;
12466 sym_value = fdh->elf.root.u.def.value;
12467 if (sym_sec->output_section != NULL)
12468 ok_dest = TRUE;
12469 }
12470 else
12471 fdh = NULL;
12472 }
12473 }
12474 else
12475 {
12476 bfd_set_error (bfd_error_bad_value);
12477 goto error_ret_free_internal;
12478 }
12479
12480 destination = 0;
12481 local_off = 0;
12482 if (ok_dest)
12483 {
12484 sym_value += irela->r_addend;
12485 destination = (sym_value
12486 + sym_sec->output_offset
12487 + sym_sec->output_section->vma);
12488 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12489 ? hash->elf.other
12490 : sym->st_other);
12491 }
12492
12493 code_sec = sym_sec;
12494 code_value = sym_value;
12495 opd = get_opd_info (sym_sec);
12496 if (opd != NULL)
12497 {
12498 bfd_vma dest;
12499
12500 if (hash == NULL && opd->adjust != NULL)
12501 {
12502 long adjust = opd->adjust[OPD_NDX (sym_value)];
12503 if (adjust == -1)
12504 continue;
12505 code_value += adjust;
12506 sym_value += adjust;
12507 }
12508 dest = opd_entry_value (sym_sec, sym_value,
12509 &code_sec, &code_value, FALSE);
12510 if (dest != (bfd_vma) -1)
12511 {
12512 destination = dest;
12513 if (fdh != NULL)
12514 {
12515 /* Fixup old ABI sym to point at code
12516 entry. */
12517 hash->elf.root.type = bfd_link_hash_defweak;
12518 hash->elf.root.u.def.section = code_sec;
12519 hash->elf.root.u.def.value = code_value;
12520 }
12521 }
12522 }
12523
12524 /* Determine what (if any) linker stub is needed. */
12525 plt_ent = NULL;
12526 stub_type = ppc_type_of_stub (section, irela, &hash,
12527 &plt_ent, destination,
12528 local_off);
12529
12530 if (stub_type != ppc_stub_plt_call)
12531 {
12532 /* Check whether we need a TOC adjusting stub.
12533 Since the linker pastes together pieces from
12534 different object files when creating the
12535 _init and _fini functions, it may be that a
12536 call to what looks like a local sym is in
12537 fact a call needing a TOC adjustment. */
12538 if (code_sec != NULL
12539 && code_sec->output_section != NULL
12540 && (htab->sec_info[code_sec->id].toc_off
12541 != htab->sec_info[section->id].toc_off)
12542 && (code_sec->has_toc_reloc
12543 || code_sec->makes_toc_func_call))
12544 stub_type = ppc_stub_long_branch_r2off;
12545 }
12546
12547 if (stub_type == ppc_stub_none)
12548 continue;
12549
12550 /* __tls_get_addr calls might be eliminated. */
12551 if (stub_type != ppc_stub_plt_call
12552 && hash != NULL
12553 && (hash == htab->tls_get_addr
12554 || hash == htab->tls_get_addr_fd)
12555 && section->has_tls_reloc
12556 && irela != internal_relocs)
12557 {
12558 /* Get tls info. */
12559 unsigned char *tls_mask;
12560
12561 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12562 irela - 1, input_bfd))
12563 goto error_ret_free_internal;
12564 if (*tls_mask != 0)
12565 continue;
12566 }
12567
12568 if (stub_type == ppc_stub_plt_call
12569 && irela + 1 < irelaend
12570 && irela[1].r_offset == irela->r_offset + 4
12571 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
12572 {
12573 if (!tocsave_find (htab, INSERT,
12574 &local_syms, irela + 1, input_bfd))
12575 goto error_ret_free_internal;
12576 }
12577 else if (stub_type == ppc_stub_plt_call)
12578 stub_type = ppc_stub_plt_call_r2save;
12579
12580 /* Support for grouping stub sections. */
12581 id_sec = htab->sec_info[section->id].u.group->link_sec;
12582
12583 /* Get the name of this stub. */
12584 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12585 if (!stub_name)
12586 goto error_ret_free_internal;
12587
12588 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12589 stub_name, FALSE, FALSE);
12590 if (stub_entry != NULL)
12591 {
12592 /* The proper stub has already been created. */
12593 free (stub_name);
12594 if (stub_type == ppc_stub_plt_call_r2save)
12595 stub_entry->stub_type = stub_type;
12596 continue;
12597 }
12598
12599 stub_entry = ppc_add_stub (stub_name, section, info);
12600 if (stub_entry == NULL)
12601 {
12602 free (stub_name);
12603 error_ret_free_internal:
12604 if (elf_section_data (section)->relocs == NULL)
12605 free (internal_relocs);
12606 error_ret_free_local:
12607 if (local_syms != NULL
12608 && (symtab_hdr->contents
12609 != (unsigned char *) local_syms))
12610 free (local_syms);
12611 return FALSE;
12612 }
12613
12614 stub_entry->stub_type = stub_type;
12615 if (stub_type != ppc_stub_plt_call
12616 && stub_type != ppc_stub_plt_call_r2save)
12617 {
12618 stub_entry->target_value = code_value;
12619 stub_entry->target_section = code_sec;
12620 }
12621 else
12622 {
12623 stub_entry->target_value = sym_value;
12624 stub_entry->target_section = sym_sec;
12625 }
12626 stub_entry->h = hash;
12627 stub_entry->plt_ent = plt_ent;
12628 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12629
12630 if (stub_entry->h != NULL)
12631 htab->stub_globals += 1;
12632 }
12633
12634 /* We're done with the internal relocs, free them. */
12635 if (elf_section_data (section)->relocs != internal_relocs)
12636 free (internal_relocs);
12637 }
12638
12639 if (local_syms != NULL
12640 && symtab_hdr->contents != (unsigned char *) local_syms)
12641 {
12642 if (!info->keep_memory)
12643 free (local_syms);
12644 else
12645 symtab_hdr->contents = (unsigned char *) local_syms;
12646 }
12647 }
12648
12649 /* We may have added some stubs. Find out the new size of the
12650 stub sections. */
12651 for (stub_sec = htab->params->stub_bfd->sections;
12652 stub_sec != NULL;
12653 stub_sec = stub_sec->next)
12654 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12655 {
12656 if (htab->stub_iteration <= STUB_SHRINK_ITER
12657 || stub_sec->rawsize < stub_sec->size)
12658 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12659 stub_sec->rawsize = stub_sec->size;
12660 stub_sec->size = 0;
12661 stub_sec->reloc_count = 0;
12662 stub_sec->flags &= ~SEC_RELOC;
12663 }
12664
12665 htab->brlt->size = 0;
12666 htab->brlt->reloc_count = 0;
12667 htab->brlt->flags &= ~SEC_RELOC;
12668 if (htab->relbrlt != NULL)
12669 htab->relbrlt->size = 0;
12670
12671 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12672
12673 for (group = htab->group; group != NULL; group = group->next)
12674 if (group->needs_save_res)
12675 group->stub_sec->size += htab->sfpr->size;
12676
12677 if (info->emitrelocations
12678 && htab->glink != NULL && htab->glink->size != 0)
12679 {
12680 htab->glink->reloc_count = 1;
12681 htab->glink->flags |= SEC_RELOC;
12682 }
12683
12684 if (htab->glink_eh_frame != NULL
12685 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12686 && htab->glink_eh_frame->output_section->size != 0)
12687 {
12688 size_t size = 0, align = 4;
12689
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 size += (17 + align - 1) & -align;
12695 if (htab->glink != NULL && htab->glink->size != 0)
12696 size += (24 + align - 1) & -align;
12697 if (size != 0)
12698 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12699 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12700 size = (size + align - 1) & -align;
12701 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12702 htab->glink_eh_frame->size = size;
12703 }
12704
12705 if (htab->params->plt_stub_align != 0)
12706 for (stub_sec = htab->params->stub_bfd->sections;
12707 stub_sec != NULL;
12708 stub_sec = stub_sec->next)
12709 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12710 stub_sec->size = ((stub_sec->size
12711 + (1 << htab->params->plt_stub_align) - 1)
12712 & -(1 << htab->params->plt_stub_align));
12713
12714 for (stub_sec = htab->params->stub_bfd->sections;
12715 stub_sec != NULL;
12716 stub_sec = stub_sec->next)
12717 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12718 && stub_sec->rawsize != stub_sec->size
12719 && (htab->stub_iteration <= STUB_SHRINK_ITER
12720 || stub_sec->rawsize < stub_sec->size))
12721 break;
12722
12723 if (stub_sec == NULL
12724 && (htab->glink_eh_frame == NULL
12725 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12726 break;
12727
12728 /* Ask the linker to do its stuff. */
12729 (*htab->params->layout_sections_again) ();
12730 }
12731
12732 if (htab->glink_eh_frame != NULL
12733 && htab->glink_eh_frame->size != 0)
12734 {
12735 bfd_vma val;
12736 bfd_byte *p, *last_fde;
12737 size_t last_fde_len, size, align, pad;
12738 asection *stub_sec;
12739
12740 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12741 if (p == NULL)
12742 return FALSE;
12743 htab->glink_eh_frame->contents = p;
12744 last_fde = p;
12745 align = 4;
12746
12747 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12748 /* CIE length (rewrite in case little-endian). */
12749 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12750 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12751 p += last_fde_len + 4;
12752
12753 for (stub_sec = htab->params->stub_bfd->sections;
12754 stub_sec != NULL;
12755 stub_sec = stub_sec->next)
12756 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12757 {
12758 last_fde = p;
12759 last_fde_len = ((17 + align - 1) & -align) - 4;
12760 /* FDE length. */
12761 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12762 p += 4;
12763 /* CIE pointer. */
12764 val = p - htab->glink_eh_frame->contents;
12765 bfd_put_32 (htab->elf.dynobj, val, p);
12766 p += 4;
12767 /* Offset to stub section, written later. */
12768 p += 4;
12769 /* stub section size. */
12770 bfd_put_32 (htab->elf.dynobj, stub_sec->size, p);
12771 p += 4;
12772 /* Augmentation. */
12773 p += 1;
12774 /* Pad. */
12775 p += ((17 + align - 1) & -align) - 17;
12776 }
12777 if (htab->glink != NULL && htab->glink->size != 0)
12778 {
12779 last_fde = p;
12780 last_fde_len = ((24 + align - 1) & -align) - 4;
12781 /* FDE length. */
12782 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12783 p += 4;
12784 /* CIE pointer. */
12785 val = p - htab->glink_eh_frame->contents;
12786 bfd_put_32 (htab->elf.dynobj, val, p);
12787 p += 4;
12788 /* Offset to .glink, written later. */
12789 p += 4;
12790 /* .glink size. */
12791 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12792 p += 4;
12793 /* Augmentation. */
12794 p += 1;
12795
12796 *p++ = DW_CFA_advance_loc + 1;
12797 *p++ = DW_CFA_register;
12798 *p++ = 65;
12799 *p++ = htab->opd_abi ? 12 : 0;
12800 *p++ = DW_CFA_advance_loc + 4;
12801 *p++ = DW_CFA_restore_extended;
12802 *p++ = 65;
12803 p += ((24 + align - 1) & -align) - 24;
12804 }
12805 /* Subsume any padding into the last FDE if user .eh_frame
12806 sections are aligned more than glink_eh_frame. Otherwise any
12807 zero padding will be seen as a terminator. */
12808 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12809 size = p - htab->glink_eh_frame->contents;
12810 pad = ((size + align - 1) & -align) - size;
12811 htab->glink_eh_frame->size = size + pad;
12812 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12813 }
12814
12815 maybe_strip_output (info, htab->brlt);
12816 if (htab->glink_eh_frame != NULL)
12817 maybe_strip_output (info, htab->glink_eh_frame);
12818
12819 return TRUE;
12820 }
12821
12822 /* Called after we have determined section placement. If sections
12823 move, we'll be called again. Provide a value for TOCstart. */
12824
12825 bfd_vma
12826 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12827 {
12828 asection *s;
12829 bfd_vma TOCstart, adjust;
12830
12831 if (info != NULL)
12832 {
12833 struct elf_link_hash_entry *h;
12834 struct elf_link_hash_table *htab = elf_hash_table (info);
12835
12836 if (is_elf_hash_table (htab)
12837 && htab->hgot != NULL)
12838 h = htab->hgot;
12839 else
12840 {
12841 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12842 if (is_elf_hash_table (htab))
12843 htab->hgot = h;
12844 }
12845 if (h != NULL
12846 && h->root.type == bfd_link_hash_defined
12847 && !h->root.linker_def
12848 && (!is_elf_hash_table (htab)
12849 || h->def_regular))
12850 {
12851 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12852 + h->root.u.def.section->output_offset
12853 + h->root.u.def.section->output_section->vma);
12854 _bfd_set_gp_value (obfd, TOCstart);
12855 return TOCstart;
12856 }
12857 }
12858
12859 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12860 order. The TOC starts where the first of these sections starts. */
12861 s = bfd_get_section_by_name (obfd, ".got");
12862 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12863 s = bfd_get_section_by_name (obfd, ".toc");
12864 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12865 s = bfd_get_section_by_name (obfd, ".tocbss");
12866 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12867 s = bfd_get_section_by_name (obfd, ".plt");
12868 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12869 {
12870 /* This may happen for
12871 o references to TOC base (SYM@toc / TOC[tc0]) without a
12872 .toc directive
12873 o bad linker script
12874 o --gc-sections and empty TOC sections
12875
12876 FIXME: Warn user? */
12877
12878 /* Look for a likely section. We probably won't even be
12879 using TOCstart. */
12880 for (s = obfd->sections; s != NULL; s = s->next)
12881 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12882 | SEC_EXCLUDE))
12883 == (SEC_ALLOC | SEC_SMALL_DATA))
12884 break;
12885 if (s == NULL)
12886 for (s = obfd->sections; s != NULL; s = s->next)
12887 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12888 == (SEC_ALLOC | SEC_SMALL_DATA))
12889 break;
12890 if (s == NULL)
12891 for (s = obfd->sections; s != NULL; s = s->next)
12892 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12893 == SEC_ALLOC)
12894 break;
12895 if (s == NULL)
12896 for (s = obfd->sections; s != NULL; s = s->next)
12897 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12898 break;
12899 }
12900
12901 TOCstart = 0;
12902 if (s != NULL)
12903 TOCstart = s->output_section->vma + s->output_offset;
12904
12905 /* Force alignment. */
12906 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12907 TOCstart -= adjust;
12908 _bfd_set_gp_value (obfd, TOCstart);
12909
12910 if (info != NULL && s != NULL)
12911 {
12912 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12913
12914 if (htab != NULL)
12915 {
12916 if (htab->elf.hgot != NULL)
12917 {
12918 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12919 htab->elf.hgot->root.u.def.section = s;
12920 }
12921 }
12922 else
12923 {
12924 struct bfd_link_hash_entry *bh = NULL;
12925 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12926 s, TOC_BASE_OFF - adjust,
12927 NULL, FALSE, FALSE, &bh);
12928 }
12929 }
12930 return TOCstart;
12931 }
12932
12933 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12934 write out any global entry stubs. */
12935
12936 static bfd_boolean
12937 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12938 {
12939 struct bfd_link_info *info;
12940 struct ppc_link_hash_table *htab;
12941 struct plt_entry *pent;
12942 asection *s;
12943
12944 if (h->root.type == bfd_link_hash_indirect)
12945 return TRUE;
12946
12947 if (!h->pointer_equality_needed)
12948 return TRUE;
12949
12950 if (h->def_regular)
12951 return TRUE;
12952
12953 info = inf;
12954 htab = ppc_hash_table (info);
12955 if (htab == NULL)
12956 return FALSE;
12957
12958 s = htab->glink;
12959 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12960 if (pent->plt.offset != (bfd_vma) -1
12961 && pent->addend == 0)
12962 {
12963 bfd_byte *p;
12964 asection *plt;
12965 bfd_vma off;
12966
12967 p = s->contents + h->root.u.def.value;
12968 plt = htab->elf.splt;
12969 if (!htab->elf.dynamic_sections_created
12970 || h->dynindx == -1)
12971 plt = htab->elf.iplt;
12972 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
12973 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
12974
12975 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
12976 {
12977 info->callbacks->einfo
12978 (_("%P: linkage table error against `%T'\n"),
12979 h->root.root.string);
12980 bfd_set_error (bfd_error_bad_value);
12981 htab->stub_error = TRUE;
12982 }
12983
12984 htab->stub_count[ppc_stub_global_entry - 1] += 1;
12985 if (htab->params->emit_stub_syms)
12986 {
12987 size_t len = strlen (h->root.root.string);
12988 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
12989
12990 if (name == NULL)
12991 return FALSE;
12992
12993 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
12994 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
12995 if (h == NULL)
12996 return FALSE;
12997 if (h->root.type == bfd_link_hash_new)
12998 {
12999 h->root.type = bfd_link_hash_defined;
13000 h->root.u.def.section = s;
13001 h->root.u.def.value = p - s->contents;
13002 h->ref_regular = 1;
13003 h->def_regular = 1;
13004 h->ref_regular_nonweak = 1;
13005 h->forced_local = 1;
13006 h->non_elf = 0;
13007 h->root.linker_def = 1;
13008 }
13009 }
13010
13011 if (PPC_HA (off) != 0)
13012 {
13013 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13014 p += 4;
13015 }
13016 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13017 p += 4;
13018 bfd_put_32 (s->owner, MTCTR_R12, p);
13019 p += 4;
13020 bfd_put_32 (s->owner, BCTR, p);
13021 break;
13022 }
13023 return TRUE;
13024 }
13025
13026 /* Build all the stubs associated with the current output file.
13027 The stubs are kept in a hash table attached to the main linker
13028 hash table. This function is called via gldelf64ppc_finish. */
13029
13030 bfd_boolean
13031 ppc64_elf_build_stubs (struct bfd_link_info *info,
13032 char **stats)
13033 {
13034 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13035 struct map_stub *group;
13036 asection *stub_sec;
13037 bfd_byte *p;
13038 int stub_sec_count = 0;
13039
13040 if (htab == NULL)
13041 return FALSE;
13042
13043 /* Allocate memory to hold the linker stubs. */
13044 for (stub_sec = htab->params->stub_bfd->sections;
13045 stub_sec != NULL;
13046 stub_sec = stub_sec->next)
13047 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
13048 && stub_sec->size != 0)
13049 {
13050 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13051 if (stub_sec->contents == NULL)
13052 return FALSE;
13053 stub_sec->size = 0;
13054 }
13055
13056 if (htab->glink != NULL && htab->glink->size != 0)
13057 {
13058 unsigned int indx;
13059 bfd_vma plt0;
13060
13061 /* Build the .glink plt call stub. */
13062 if (htab->params->emit_stub_syms)
13063 {
13064 struct elf_link_hash_entry *h;
13065 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13066 TRUE, FALSE, FALSE);
13067 if (h == NULL)
13068 return FALSE;
13069 if (h->root.type == bfd_link_hash_new)
13070 {
13071 h->root.type = bfd_link_hash_defined;
13072 h->root.u.def.section = htab->glink;
13073 h->root.u.def.value = 8;
13074 h->ref_regular = 1;
13075 h->def_regular = 1;
13076 h->ref_regular_nonweak = 1;
13077 h->forced_local = 1;
13078 h->non_elf = 0;
13079 h->root.linker_def = 1;
13080 }
13081 }
13082 plt0 = (htab->elf.splt->output_section->vma
13083 + htab->elf.splt->output_offset
13084 - 16);
13085 if (info->emitrelocations)
13086 {
13087 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13088 if (r == NULL)
13089 return FALSE;
13090 r->r_offset = (htab->glink->output_offset
13091 + htab->glink->output_section->vma);
13092 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13093 r->r_addend = plt0;
13094 }
13095 p = htab->glink->contents;
13096 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13097 bfd_put_64 (htab->glink->owner, plt0, p);
13098 p += 8;
13099 if (htab->opd_abi)
13100 {
13101 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13102 p += 4;
13103 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13104 p += 4;
13105 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13106 p += 4;
13107 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13108 p += 4;
13109 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13110 p += 4;
13111 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13112 p += 4;
13113 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13114 p += 4;
13115 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13116 p += 4;
13117 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13118 p += 4;
13119 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13120 p += 4;
13121 }
13122 else
13123 {
13124 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13125 p += 4;
13126 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13127 p += 4;
13128 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13129 p += 4;
13130 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13131 p += 4;
13132 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13133 p += 4;
13134 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13135 p += 4;
13136 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13137 p += 4;
13138 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13139 p += 4;
13140 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13141 p += 4;
13142 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13143 p += 4;
13144 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13145 p += 4;
13146 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13147 p += 4;
13148 }
13149 bfd_put_32 (htab->glink->owner, BCTR, p);
13150 p += 4;
13151 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
13152 {
13153 bfd_put_32 (htab->glink->owner, NOP, p);
13154 p += 4;
13155 }
13156
13157 /* Build the .glink lazy link call stubs. */
13158 indx = 0;
13159 while (p < htab->glink->contents + htab->glink->rawsize)
13160 {
13161 if (htab->opd_abi)
13162 {
13163 if (indx < 0x8000)
13164 {
13165 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13166 p += 4;
13167 }
13168 else
13169 {
13170 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13171 p += 4;
13172 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13173 p);
13174 p += 4;
13175 }
13176 }
13177 bfd_put_32 (htab->glink->owner,
13178 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13179 indx++;
13180 p += 4;
13181 }
13182
13183 /* Build .glink global entry stubs. */
13184 if (htab->glink->size > htab->glink->rawsize)
13185 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13186 }
13187
13188 if (htab->brlt != NULL && htab->brlt->size != 0)
13189 {
13190 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13191 htab->brlt->size);
13192 if (htab->brlt->contents == NULL)
13193 return FALSE;
13194 }
13195 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13196 {
13197 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13198 htab->relbrlt->size);
13199 if (htab->relbrlt->contents == NULL)
13200 return FALSE;
13201 }
13202
13203 /* Build the stubs as directed by the stub hash table. */
13204 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13205
13206 for (group = htab->group; group != NULL; group = group->next)
13207 if (group->needs_save_res)
13208 {
13209 stub_sec = group->stub_sec;
13210 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13211 htab->sfpr->size);
13212 if (htab->params->emit_stub_syms)
13213 {
13214 unsigned int i;
13215
13216 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13217 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13218 return FALSE;
13219 }
13220 stub_sec->size += htab->sfpr->size;
13221 }
13222
13223 if (htab->relbrlt != NULL)
13224 htab->relbrlt->reloc_count = 0;
13225
13226 if (htab->params->plt_stub_align != 0)
13227 for (stub_sec = htab->params->stub_bfd->sections;
13228 stub_sec != NULL;
13229 stub_sec = stub_sec->next)
13230 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13231 stub_sec->size = ((stub_sec->size
13232 + (1 << htab->params->plt_stub_align) - 1)
13233 & -(1 << htab->params->plt_stub_align));
13234
13235 for (stub_sec = htab->params->stub_bfd->sections;
13236 stub_sec != NULL;
13237 stub_sec = stub_sec->next)
13238 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
13239 {
13240 stub_sec_count += 1;
13241 if (stub_sec->rawsize != stub_sec->size
13242 && (htab->stub_iteration <= STUB_SHRINK_ITER
13243 || stub_sec->rawsize < stub_sec->size))
13244 break;
13245 }
13246
13247 /* Note that the glink_eh_frame check here is not only testing that
13248 the generated size matched the calculated size but also that
13249 bfd_elf_discard_info didn't make any changes to the section. */
13250 if (stub_sec != NULL
13251 || (htab->glink_eh_frame != NULL
13252 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
13253 {
13254 htab->stub_error = TRUE;
13255 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13256 }
13257
13258 if (htab->stub_error)
13259 return FALSE;
13260
13261 if (stats != NULL)
13262 {
13263 *stats = bfd_malloc (500);
13264 if (*stats == NULL)
13265 return FALSE;
13266
13267 sprintf (*stats, _("linker stubs in %u group%s\n"
13268 " branch %lu\n"
13269 " toc adjust %lu\n"
13270 " long branch %lu\n"
13271 " long toc adj %lu\n"
13272 " plt call %lu\n"
13273 " plt call toc %lu\n"
13274 " global entry %lu"),
13275 stub_sec_count,
13276 stub_sec_count == 1 ? "" : "s",
13277 htab->stub_count[ppc_stub_long_branch - 1],
13278 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13279 htab->stub_count[ppc_stub_plt_branch - 1],
13280 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13281 htab->stub_count[ppc_stub_plt_call - 1],
13282 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13283 htab->stub_count[ppc_stub_global_entry - 1]);
13284 }
13285 return TRUE;
13286 }
13287
13288 /* What to do when ld finds relocations against symbols defined in
13289 discarded sections. */
13290
13291 static unsigned int
13292 ppc64_elf_action_discarded (asection *sec)
13293 {
13294 if (strcmp (".opd", sec->name) == 0)
13295 return 0;
13296
13297 if (strcmp (".toc", sec->name) == 0)
13298 return 0;
13299
13300 if (strcmp (".toc1", sec->name) == 0)
13301 return 0;
13302
13303 return _bfd_elf_default_action_discarded (sec);
13304 }
13305
13306 /* The RELOCATE_SECTION function is called by the ELF backend linker
13307 to handle the relocations for a section.
13308
13309 The relocs are always passed as Rela structures; if the section
13310 actually uses Rel structures, the r_addend field will always be
13311 zero.
13312
13313 This function is responsible for adjust the section contents as
13314 necessary, and (if using Rela relocs and generating a
13315 relocatable output file) adjusting the reloc addend as
13316 necessary.
13317
13318 This function does not have to worry about setting the reloc
13319 address or the reloc symbol index.
13320
13321 LOCAL_SYMS is a pointer to the swapped in local symbols.
13322
13323 LOCAL_SECTIONS is an array giving the section in the input file
13324 corresponding to the st_shndx field of each local symbol.
13325
13326 The global hash table entry for the global symbols can be found
13327 via elf_sym_hashes (input_bfd).
13328
13329 When generating relocatable output, this function must handle
13330 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13331 going to be the section symbol corresponding to the output
13332 section, which means that the addend must be adjusted
13333 accordingly. */
13334
13335 static bfd_boolean
13336 ppc64_elf_relocate_section (bfd *output_bfd,
13337 struct bfd_link_info *info,
13338 bfd *input_bfd,
13339 asection *input_section,
13340 bfd_byte *contents,
13341 Elf_Internal_Rela *relocs,
13342 Elf_Internal_Sym *local_syms,
13343 asection **local_sections)
13344 {
13345 struct ppc_link_hash_table *htab;
13346 Elf_Internal_Shdr *symtab_hdr;
13347 struct elf_link_hash_entry **sym_hashes;
13348 Elf_Internal_Rela *rel;
13349 Elf_Internal_Rela *wrel;
13350 Elf_Internal_Rela *relend;
13351 Elf_Internal_Rela outrel;
13352 bfd_byte *loc;
13353 struct got_entry **local_got_ents;
13354 bfd_vma TOCstart;
13355 bfd_boolean ret = TRUE;
13356 bfd_boolean is_opd;
13357 /* Assume 'at' branch hints. */
13358 bfd_boolean is_isa_v2 = TRUE;
13359 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13360
13361 /* Initialize howto table if needed. */
13362 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13363 ppc_howto_init ();
13364
13365 htab = ppc_hash_table (info);
13366 if (htab == NULL)
13367 return FALSE;
13368
13369 /* Don't relocate stub sections. */
13370 if (input_section->owner == htab->params->stub_bfd)
13371 return TRUE;
13372
13373 BFD_ASSERT (is_ppc64_elf (input_bfd));
13374
13375 local_got_ents = elf_local_got_ents (input_bfd);
13376 TOCstart = elf_gp (output_bfd);
13377 symtab_hdr = &elf_symtab_hdr (input_bfd);
13378 sym_hashes = elf_sym_hashes (input_bfd);
13379 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13380
13381 rel = wrel = relocs;
13382 relend = relocs + input_section->reloc_count;
13383 for (; rel < relend; wrel++, rel++)
13384 {
13385 enum elf_ppc64_reloc_type r_type;
13386 bfd_vma addend;
13387 bfd_reloc_status_type r;
13388 Elf_Internal_Sym *sym;
13389 asection *sec;
13390 struct elf_link_hash_entry *h_elf;
13391 struct ppc_link_hash_entry *h;
13392 struct ppc_link_hash_entry *fdh;
13393 const char *sym_name;
13394 unsigned long r_symndx, toc_symndx;
13395 bfd_vma toc_addend;
13396 unsigned char tls_mask, tls_gd, tls_type;
13397 unsigned char sym_type;
13398 bfd_vma relocation;
13399 bfd_boolean unresolved_reloc;
13400 bfd_boolean warned;
13401 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13402 unsigned int insn;
13403 unsigned int mask;
13404 struct ppc_stub_hash_entry *stub_entry;
13405 bfd_vma max_br_offset;
13406 bfd_vma from;
13407 Elf_Internal_Rela orig_rel;
13408 reloc_howto_type *howto;
13409 struct reloc_howto_struct alt_howto;
13410
13411 again:
13412 orig_rel = *rel;
13413
13414 r_type = ELF64_R_TYPE (rel->r_info);
13415 r_symndx = ELF64_R_SYM (rel->r_info);
13416
13417 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13418 symbol of the previous ADDR64 reloc. The symbol gives us the
13419 proper TOC base to use. */
13420 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13421 && wrel != relocs
13422 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13423 && is_opd)
13424 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13425
13426 sym = NULL;
13427 sec = NULL;
13428 h_elf = NULL;
13429 sym_name = NULL;
13430 unresolved_reloc = FALSE;
13431 warned = FALSE;
13432
13433 if (r_symndx < symtab_hdr->sh_info)
13434 {
13435 /* It's a local symbol. */
13436 struct _opd_sec_data *opd;
13437
13438 sym = local_syms + r_symndx;
13439 sec = local_sections[r_symndx];
13440 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13441 sym_type = ELF64_ST_TYPE (sym->st_info);
13442 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13443 opd = get_opd_info (sec);
13444 if (opd != NULL && opd->adjust != NULL)
13445 {
13446 long adjust = opd->adjust[OPD_NDX (sym->st_value
13447 + rel->r_addend)];
13448 if (adjust == -1)
13449 relocation = 0;
13450 else
13451 {
13452 /* If this is a relocation against the opd section sym
13453 and we have edited .opd, adjust the reloc addend so
13454 that ld -r and ld --emit-relocs output is correct.
13455 If it is a reloc against some other .opd symbol,
13456 then the symbol value will be adjusted later. */
13457 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13458 rel->r_addend += adjust;
13459 else
13460 relocation += adjust;
13461 }
13462 }
13463 }
13464 else
13465 {
13466 bfd_boolean ignored;
13467
13468 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13469 r_symndx, symtab_hdr, sym_hashes,
13470 h_elf, sec, relocation,
13471 unresolved_reloc, warned, ignored);
13472 sym_name = h_elf->root.root.string;
13473 sym_type = h_elf->type;
13474 if (sec != NULL
13475 && sec->owner == output_bfd
13476 && strcmp (sec->name, ".opd") == 0)
13477 {
13478 /* This is a symbol defined in a linker script. All
13479 such are defined in output sections, even those
13480 defined by simple assignment from a symbol defined in
13481 an input section. Transfer the symbol to an
13482 appropriate input .opd section, so that a branch to
13483 this symbol will be mapped to the location specified
13484 by the opd entry. */
13485 struct bfd_link_order *lo;
13486 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13487 if (lo->type == bfd_indirect_link_order)
13488 {
13489 asection *isec = lo->u.indirect.section;
13490 if (h_elf->root.u.def.value >= isec->output_offset
13491 && h_elf->root.u.def.value < (isec->output_offset
13492 + isec->size))
13493 {
13494 h_elf->root.u.def.value -= isec->output_offset;
13495 h_elf->root.u.def.section = isec;
13496 sec = isec;
13497 break;
13498 }
13499 }
13500 }
13501 }
13502 h = (struct ppc_link_hash_entry *) h_elf;
13503
13504 if (sec != NULL && discarded_section (sec))
13505 {
13506 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13507 input_bfd, input_section,
13508 contents + rel->r_offset);
13509 wrel->r_offset = rel->r_offset;
13510 wrel->r_info = 0;
13511 wrel->r_addend = 0;
13512
13513 /* For ld -r, remove relocations in debug sections against
13514 sections defined in discarded sections. Not done for
13515 non-debug to preserve relocs in .eh_frame which the
13516 eh_frame editing code expects to be present. */
13517 if (bfd_link_relocatable (info)
13518 && (input_section->flags & SEC_DEBUGGING))
13519 wrel--;
13520
13521 continue;
13522 }
13523
13524 if (bfd_link_relocatable (info))
13525 goto copy_reloc;
13526
13527 if (h != NULL && &h->elf == htab->elf.hgot)
13528 {
13529 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13530 sec = bfd_abs_section_ptr;
13531 unresolved_reloc = FALSE;
13532 }
13533
13534 /* TLS optimizations. Replace instruction sequences and relocs
13535 based on information we collected in tls_optimize. We edit
13536 RELOCS so that --emit-relocs will output something sensible
13537 for the final instruction stream. */
13538 tls_mask = 0;
13539 tls_gd = 0;
13540 toc_symndx = 0;
13541 if (h != NULL)
13542 tls_mask = h->tls_mask;
13543 else if (local_got_ents != NULL)
13544 {
13545 struct plt_entry **local_plt = (struct plt_entry **)
13546 (local_got_ents + symtab_hdr->sh_info);
13547 unsigned char *lgot_masks = (unsigned char *)
13548 (local_plt + symtab_hdr->sh_info);
13549 tls_mask = lgot_masks[r_symndx];
13550 }
13551 if (tls_mask == 0
13552 && (r_type == R_PPC64_TLS
13553 || r_type == R_PPC64_TLSGD
13554 || r_type == R_PPC64_TLSLD))
13555 {
13556 /* Check for toc tls entries. */
13557 unsigned char *toc_tls;
13558
13559 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13560 &local_syms, rel, input_bfd))
13561 return FALSE;
13562
13563 if (toc_tls)
13564 tls_mask = *toc_tls;
13565 }
13566
13567 /* Check that tls relocs are used with tls syms, and non-tls
13568 relocs are used with non-tls syms. */
13569 if (r_symndx != STN_UNDEF
13570 && r_type != R_PPC64_NONE
13571 && (h == NULL
13572 || h->elf.root.type == bfd_link_hash_defined
13573 || h->elf.root.type == bfd_link_hash_defweak)
13574 && (IS_PPC64_TLS_RELOC (r_type)
13575 != (sym_type == STT_TLS
13576 || (sym_type == STT_SECTION
13577 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13578 {
13579 if (tls_mask != 0
13580 && (r_type == R_PPC64_TLS
13581 || r_type == R_PPC64_TLSGD
13582 || r_type == R_PPC64_TLSLD))
13583 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13584 ;
13585 else
13586 info->callbacks->einfo
13587 (!IS_PPC64_TLS_RELOC (r_type)
13588 /* xgettext:c-format */
13589 ? _("%H: %s used with TLS symbol `%T'\n")
13590 /* xgettext:c-format */
13591 : _("%H: %s used with non-TLS symbol `%T'\n"),
13592 input_bfd, input_section, rel->r_offset,
13593 ppc64_elf_howto_table[r_type]->name,
13594 sym_name);
13595 }
13596
13597 /* Ensure reloc mapping code below stays sane. */
13598 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13599 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13600 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13601 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13602 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13603 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13604 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13605 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13606 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13607 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13608 abort ();
13609
13610 switch (r_type)
13611 {
13612 default:
13613 break;
13614
13615 case R_PPC64_LO_DS_OPT:
13616 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13617 if ((insn & (0x3f << 26)) != 58u << 26)
13618 abort ();
13619 insn += (14u << 26) - (58u << 26);
13620 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13621 r_type = R_PPC64_TOC16_LO;
13622 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13623 break;
13624
13625 case R_PPC64_TOC16:
13626 case R_PPC64_TOC16_LO:
13627 case R_PPC64_TOC16_DS:
13628 case R_PPC64_TOC16_LO_DS:
13629 {
13630 /* Check for toc tls entries. */
13631 unsigned char *toc_tls;
13632 int retval;
13633
13634 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13635 &local_syms, rel, input_bfd);
13636 if (retval == 0)
13637 return FALSE;
13638
13639 if (toc_tls)
13640 {
13641 tls_mask = *toc_tls;
13642 if (r_type == R_PPC64_TOC16_DS
13643 || r_type == R_PPC64_TOC16_LO_DS)
13644 {
13645 if (tls_mask != 0
13646 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13647 goto toctprel;
13648 }
13649 else
13650 {
13651 /* If we found a GD reloc pair, then we might be
13652 doing a GD->IE transition. */
13653 if (retval == 2)
13654 {
13655 tls_gd = TLS_TPRELGD;
13656 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13657 goto tls_ldgd_opt;
13658 }
13659 else if (retval == 3)
13660 {
13661 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13662 goto tls_ldgd_opt;
13663 }
13664 }
13665 }
13666 }
13667 break;
13668
13669 case R_PPC64_GOT_TPREL16_HI:
13670 case R_PPC64_GOT_TPREL16_HA:
13671 if (tls_mask != 0
13672 && (tls_mask & TLS_TPREL) == 0)
13673 {
13674 rel->r_offset -= d_offset;
13675 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13676 r_type = R_PPC64_NONE;
13677 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13678 }
13679 break;
13680
13681 case R_PPC64_GOT_TPREL16_DS:
13682 case R_PPC64_GOT_TPREL16_LO_DS:
13683 if (tls_mask != 0
13684 && (tls_mask & TLS_TPREL) == 0)
13685 {
13686 toctprel:
13687 insn = bfd_get_32 (input_bfd,
13688 contents + rel->r_offset - d_offset);
13689 insn &= 31 << 21;
13690 insn |= 0x3c0d0000; /* addis 0,13,0 */
13691 bfd_put_32 (input_bfd, insn,
13692 contents + rel->r_offset - d_offset);
13693 r_type = R_PPC64_TPREL16_HA;
13694 if (toc_symndx != 0)
13695 {
13696 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13697 rel->r_addend = toc_addend;
13698 /* We changed the symbol. Start over in order to
13699 get h, sym, sec etc. right. */
13700 goto again;
13701 }
13702 else
13703 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13704 }
13705 break;
13706
13707 case R_PPC64_TLS:
13708 if (tls_mask != 0
13709 && (tls_mask & TLS_TPREL) == 0)
13710 {
13711 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13712 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13713 if (insn == 0)
13714 abort ();
13715 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13716 /* Was PPC64_TLS which sits on insn boundary, now
13717 PPC64_TPREL16_LO which is at low-order half-word. */
13718 rel->r_offset += d_offset;
13719 r_type = R_PPC64_TPREL16_LO;
13720 if (toc_symndx != 0)
13721 {
13722 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13723 rel->r_addend = toc_addend;
13724 /* We changed the symbol. Start over in order to
13725 get h, sym, sec etc. right. */
13726 goto again;
13727 }
13728 else
13729 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13730 }
13731 break;
13732
13733 case R_PPC64_GOT_TLSGD16_HI:
13734 case R_PPC64_GOT_TLSGD16_HA:
13735 tls_gd = TLS_TPRELGD;
13736 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13737 goto tls_gdld_hi;
13738 break;
13739
13740 case R_PPC64_GOT_TLSLD16_HI:
13741 case R_PPC64_GOT_TLSLD16_HA:
13742 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13743 {
13744 tls_gdld_hi:
13745 if ((tls_mask & tls_gd) != 0)
13746 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13747 + R_PPC64_GOT_TPREL16_DS);
13748 else
13749 {
13750 rel->r_offset -= d_offset;
13751 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13752 r_type = R_PPC64_NONE;
13753 }
13754 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13755 }
13756 break;
13757
13758 case R_PPC64_GOT_TLSGD16:
13759 case R_PPC64_GOT_TLSGD16_LO:
13760 tls_gd = TLS_TPRELGD;
13761 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13762 goto tls_ldgd_opt;
13763 break;
13764
13765 case R_PPC64_GOT_TLSLD16:
13766 case R_PPC64_GOT_TLSLD16_LO:
13767 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13768 {
13769 unsigned int insn1, insn2, insn3;
13770 bfd_vma offset;
13771
13772 tls_ldgd_opt:
13773 offset = (bfd_vma) -1;
13774 /* If not using the newer R_PPC64_TLSGD/LD to mark
13775 __tls_get_addr calls, we must trust that the call
13776 stays with its arg setup insns, ie. that the next
13777 reloc is the __tls_get_addr call associated with
13778 the current reloc. Edit both insns. */
13779 if (input_section->has_tls_get_addr_call
13780 && rel + 1 < relend
13781 && branch_reloc_hash_match (input_bfd, rel + 1,
13782 htab->tls_get_addr,
13783 htab->tls_get_addr_fd))
13784 offset = rel[1].r_offset;
13785 /* We read the low GOT_TLS (or TOC16) insn because we
13786 need to keep the destination reg. It may be
13787 something other than the usual r3, and moved to r3
13788 before the call by intervening code. */
13789 insn1 = bfd_get_32 (input_bfd,
13790 contents + rel->r_offset - d_offset);
13791 if ((tls_mask & tls_gd) != 0)
13792 {
13793 /* IE */
13794 insn1 &= (0x1f << 21) | (0x1f << 16);
13795 insn1 |= 58 << 26; /* ld */
13796 insn2 = 0x7c636a14; /* add 3,3,13 */
13797 if (offset != (bfd_vma) -1)
13798 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13799 if ((tls_mask & TLS_EXPLICIT) == 0)
13800 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13801 + R_PPC64_GOT_TPREL16_DS);
13802 else
13803 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13804 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13805 }
13806 else
13807 {
13808 /* LE */
13809 insn1 &= 0x1f << 21;
13810 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13811 insn2 = 0x38630000; /* addi 3,3,0 */
13812 if (tls_gd == 0)
13813 {
13814 /* Was an LD reloc. */
13815 if (toc_symndx)
13816 sec = local_sections[toc_symndx];
13817 for (r_symndx = 0;
13818 r_symndx < symtab_hdr->sh_info;
13819 r_symndx++)
13820 if (local_sections[r_symndx] == sec)
13821 break;
13822 if (r_symndx >= symtab_hdr->sh_info)
13823 r_symndx = STN_UNDEF;
13824 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13825 if (r_symndx != STN_UNDEF)
13826 rel->r_addend -= (local_syms[r_symndx].st_value
13827 + sec->output_offset
13828 + sec->output_section->vma);
13829 }
13830 else if (toc_symndx != 0)
13831 {
13832 r_symndx = toc_symndx;
13833 rel->r_addend = toc_addend;
13834 }
13835 r_type = R_PPC64_TPREL16_HA;
13836 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13837 if (offset != (bfd_vma) -1)
13838 {
13839 rel[1].r_info = ELF64_R_INFO (r_symndx,
13840 R_PPC64_TPREL16_LO);
13841 rel[1].r_offset = offset + d_offset;
13842 rel[1].r_addend = rel->r_addend;
13843 }
13844 }
13845 bfd_put_32 (input_bfd, insn1,
13846 contents + rel->r_offset - d_offset);
13847 if (offset != (bfd_vma) -1)
13848 {
13849 insn3 = bfd_get_32 (input_bfd,
13850 contents + offset + 4);
13851 if (insn3 == NOP
13852 || insn3 == CROR_151515 || insn3 == CROR_313131)
13853 {
13854 rel[1].r_offset += 4;
13855 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13856 insn2 = NOP;
13857 }
13858 bfd_put_32 (input_bfd, insn2, contents + offset);
13859 }
13860 if ((tls_mask & tls_gd) == 0
13861 && (tls_gd == 0 || toc_symndx != 0))
13862 {
13863 /* We changed the symbol. Start over in order
13864 to get h, sym, sec etc. right. */
13865 goto again;
13866 }
13867 }
13868 break;
13869
13870 case R_PPC64_TLSGD:
13871 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13872 {
13873 unsigned int insn2, insn3;
13874 bfd_vma offset = rel->r_offset;
13875
13876 if ((tls_mask & TLS_TPRELGD) != 0)
13877 {
13878 /* IE */
13879 r_type = R_PPC64_NONE;
13880 insn2 = 0x7c636a14; /* add 3,3,13 */
13881 }
13882 else
13883 {
13884 /* LE */
13885 if (toc_symndx != 0)
13886 {
13887 r_symndx = toc_symndx;
13888 rel->r_addend = toc_addend;
13889 }
13890 r_type = R_PPC64_TPREL16_LO;
13891 rel->r_offset = offset + d_offset;
13892 insn2 = 0x38630000; /* addi 3,3,0 */
13893 }
13894 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13895 /* Zap the reloc on the _tls_get_addr call too. */
13896 BFD_ASSERT (offset == rel[1].r_offset);
13897 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13898 insn3 = bfd_get_32 (input_bfd,
13899 contents + offset + 4);
13900 if (insn3 == NOP
13901 || insn3 == CROR_151515 || insn3 == CROR_313131)
13902 {
13903 rel->r_offset += 4;
13904 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13905 insn2 = NOP;
13906 }
13907 bfd_put_32 (input_bfd, insn2, contents + offset);
13908 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13909 goto again;
13910 }
13911 break;
13912
13913 case R_PPC64_TLSLD:
13914 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13915 {
13916 unsigned int insn2, insn3;
13917 bfd_vma offset = rel->r_offset;
13918
13919 if (toc_symndx)
13920 sec = local_sections[toc_symndx];
13921 for (r_symndx = 0;
13922 r_symndx < symtab_hdr->sh_info;
13923 r_symndx++)
13924 if (local_sections[r_symndx] == sec)
13925 break;
13926 if (r_symndx >= symtab_hdr->sh_info)
13927 r_symndx = STN_UNDEF;
13928 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13929 if (r_symndx != STN_UNDEF)
13930 rel->r_addend -= (local_syms[r_symndx].st_value
13931 + sec->output_offset
13932 + sec->output_section->vma);
13933
13934 r_type = R_PPC64_TPREL16_LO;
13935 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13936 rel->r_offset = offset + d_offset;
13937 /* Zap the reloc on the _tls_get_addr call too. */
13938 BFD_ASSERT (offset == rel[1].r_offset);
13939 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13940 insn2 = 0x38630000; /* addi 3,3,0 */
13941 insn3 = bfd_get_32 (input_bfd,
13942 contents + offset + 4);
13943 if (insn3 == NOP
13944 || insn3 == CROR_151515 || insn3 == CROR_313131)
13945 {
13946 rel->r_offset += 4;
13947 bfd_put_32 (input_bfd, insn2, contents + offset + 4);
13948 insn2 = NOP;
13949 }
13950 bfd_put_32 (input_bfd, insn2, contents + offset);
13951 goto again;
13952 }
13953 break;
13954
13955 case R_PPC64_DTPMOD64:
13956 if (rel + 1 < relend
13957 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13958 && rel[1].r_offset == rel->r_offset + 8)
13959 {
13960 if ((tls_mask & TLS_GD) == 0)
13961 {
13962 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13963 if ((tls_mask & TLS_TPRELGD) != 0)
13964 r_type = R_PPC64_TPREL64;
13965 else
13966 {
13967 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13968 r_type = R_PPC64_NONE;
13969 }
13970 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13971 }
13972 }
13973 else
13974 {
13975 if ((tls_mask & TLS_LD) == 0)
13976 {
13977 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13978 r_type = R_PPC64_NONE;
13979 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13980 }
13981 }
13982 break;
13983
13984 case R_PPC64_TPREL64:
13985 if ((tls_mask & TLS_TPREL) == 0)
13986 {
13987 r_type = R_PPC64_NONE;
13988 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13989 }
13990 break;
13991
13992 case R_PPC64_ENTRY:
13993 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13994 if (!bfd_link_pic (info)
13995 && !info->traditional_format
13996 && relocation + 0x80008000 <= 0xffffffff)
13997 {
13998 unsigned int insn1, insn2;
13999
14000 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14001 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14002 if ((insn1 & ~0xfffc) == LD_R2_0R12
14003 && insn2 == ADD_R2_R2_R12)
14004 {
14005 bfd_put_32 (input_bfd,
14006 LIS_R2 + PPC_HA (relocation),
14007 contents + rel->r_offset);
14008 bfd_put_32 (input_bfd,
14009 ADDI_R2_R2 + PPC_LO (relocation),
14010 contents + rel->r_offset + 4);
14011 }
14012 }
14013 else
14014 {
14015 relocation -= (rel->r_offset
14016 + input_section->output_offset
14017 + input_section->output_section->vma);
14018 if (relocation + 0x80008000 <= 0xffffffff)
14019 {
14020 unsigned int insn1, insn2;
14021
14022 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14023 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14024 if ((insn1 & ~0xfffc) == LD_R2_0R12
14025 && insn2 == ADD_R2_R2_R12)
14026 {
14027 bfd_put_32 (input_bfd,
14028 ADDIS_R2_R12 + PPC_HA (relocation),
14029 contents + rel->r_offset);
14030 bfd_put_32 (input_bfd,
14031 ADDI_R2_R2 + PPC_LO (relocation),
14032 contents + rel->r_offset + 4);
14033 }
14034 }
14035 }
14036 break;
14037
14038 case R_PPC64_REL16_HA:
14039 /* If we are generating a non-PIC executable, edit
14040 . 0: addis 2,12,.TOC.-0b@ha
14041 . addi 2,2,.TOC.-0b@l
14042 used by ELFv2 global entry points to set up r2, to
14043 . lis 2,.TOC.@ha
14044 . addi 2,2,.TOC.@l
14045 if .TOC. is in range. */
14046 if (!bfd_link_pic (info)
14047 && !info->traditional_format
14048 && !htab->opd_abi
14049 && rel->r_addend == d_offset
14050 && h != NULL && &h->elf == htab->elf.hgot
14051 && rel + 1 < relend
14052 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14053 && rel[1].r_offset == rel->r_offset + 4
14054 && rel[1].r_addend == rel->r_addend + 4
14055 && relocation + 0x80008000 <= 0xffffffff)
14056 {
14057 unsigned int insn1, insn2;
14058 bfd_vma offset = rel->r_offset - d_offset;
14059 insn1 = bfd_get_32 (input_bfd, contents + offset);
14060 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14061 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14062 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14063 {
14064 r_type = R_PPC64_ADDR16_HA;
14065 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14066 rel->r_addend -= d_offset;
14067 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14068 rel[1].r_addend -= d_offset + 4;
14069 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14070 }
14071 }
14072 break;
14073 }
14074
14075 /* Handle other relocations that tweak non-addend part of insn. */
14076 insn = 0;
14077 max_br_offset = 1 << 25;
14078 addend = rel->r_addend;
14079 reloc_dest = DEST_NORMAL;
14080 switch (r_type)
14081 {
14082 default:
14083 break;
14084
14085 case R_PPC64_TOCSAVE:
14086 if (relocation + addend == (rel->r_offset
14087 + input_section->output_offset
14088 + input_section->output_section->vma)
14089 && tocsave_find (htab, NO_INSERT,
14090 &local_syms, rel, input_bfd))
14091 {
14092 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14093 if (insn == NOP
14094 || insn == CROR_151515 || insn == CROR_313131)
14095 bfd_put_32 (input_bfd,
14096 STD_R2_0R1 + STK_TOC (htab),
14097 contents + rel->r_offset);
14098 }
14099 break;
14100
14101 /* Branch taken prediction relocations. */
14102 case R_PPC64_ADDR14_BRTAKEN:
14103 case R_PPC64_REL14_BRTAKEN:
14104 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14105 /* Fall through. */
14106
14107 /* Branch not taken prediction relocations. */
14108 case R_PPC64_ADDR14_BRNTAKEN:
14109 case R_PPC64_REL14_BRNTAKEN:
14110 insn |= bfd_get_32 (input_bfd,
14111 contents + rel->r_offset) & ~(0x01 << 21);
14112 /* Fall through. */
14113
14114 case R_PPC64_REL14:
14115 max_br_offset = 1 << 15;
14116 /* Fall through. */
14117
14118 case R_PPC64_REL24:
14119 /* Calls to functions with a different TOC, such as calls to
14120 shared objects, need to alter the TOC pointer. This is
14121 done using a linkage stub. A REL24 branching to these
14122 linkage stubs needs to be followed by a nop, as the nop
14123 will be replaced with an instruction to restore the TOC
14124 base pointer. */
14125 fdh = h;
14126 if (h != NULL
14127 && h->oh != NULL
14128 && h->oh->is_func_descriptor)
14129 fdh = ppc_follow_link (h->oh);
14130 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14131 htab);
14132 if (stub_entry != NULL
14133 && (stub_entry->stub_type == ppc_stub_plt_call
14134 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14135 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14136 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14137 {
14138 bfd_boolean can_plt_call = FALSE;
14139
14140 /* All of these stubs will modify r2, so there must be a
14141 branch and link followed by a nop. The nop is
14142 replaced by an insn to restore r2. */
14143 if (rel->r_offset + 8 <= input_section->size)
14144 {
14145 unsigned long br;
14146
14147 br = bfd_get_32 (input_bfd,
14148 contents + rel->r_offset);
14149 if ((br & 1) != 0)
14150 {
14151 unsigned long nop;
14152
14153 nop = bfd_get_32 (input_bfd,
14154 contents + rel->r_offset + 4);
14155 if (nop == NOP
14156 || nop == CROR_151515 || nop == CROR_313131)
14157 {
14158 if (h != NULL
14159 && (h == htab->tls_get_addr_fd
14160 || h == htab->tls_get_addr)
14161 && htab->params->tls_get_addr_opt)
14162 {
14163 /* Special stub used, leave nop alone. */
14164 }
14165 else
14166 bfd_put_32 (input_bfd,
14167 LD_R2_0R1 + STK_TOC (htab),
14168 contents + rel->r_offset + 4);
14169 can_plt_call = TRUE;
14170 }
14171 }
14172 }
14173
14174 if (!can_plt_call && h != NULL)
14175 {
14176 const char *name = h->elf.root.root.string;
14177
14178 if (*name == '.')
14179 ++name;
14180
14181 if (strncmp (name, "__libc_start_main", 17) == 0
14182 && (name[17] == 0 || name[17] == '@'))
14183 {
14184 /* Allow crt1 branch to go via a toc adjusting
14185 stub. Other calls that never return could do
14186 the same, if we could detect such. */
14187 can_plt_call = TRUE;
14188 }
14189 }
14190
14191 if (!can_plt_call)
14192 {
14193 /* g++ as of 20130507 emits self-calls without a
14194 following nop. This is arguably wrong since we
14195 have conflicting information. On the one hand a
14196 global symbol and on the other a local call
14197 sequence, but don't error for this special case.
14198 It isn't possible to cheaply verify we have
14199 exactly such a call. Allow all calls to the same
14200 section. */
14201 asection *code_sec = sec;
14202
14203 if (get_opd_info (sec) != NULL)
14204 {
14205 bfd_vma off = (relocation + addend
14206 - sec->output_section->vma
14207 - sec->output_offset);
14208
14209 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14210 }
14211 if (code_sec == input_section)
14212 can_plt_call = TRUE;
14213 }
14214
14215 if (!can_plt_call)
14216 {
14217 if (stub_entry->stub_type == ppc_stub_plt_call
14218 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14219 info->callbacks->einfo
14220 /* xgettext:c-format */
14221 (_("%H: call to `%T' lacks nop, can't restore toc; "
14222 "recompile with -fPIC\n"),
14223 input_bfd, input_section, rel->r_offset, sym_name);
14224 else
14225 info->callbacks->einfo
14226 /* xgettext:c-format */
14227 (_("%H: call to `%T' lacks nop, can't restore toc; "
14228 "(-mcmodel=small toc adjust stub)\n"),
14229 input_bfd, input_section, rel->r_offset, sym_name);
14230
14231 bfd_set_error (bfd_error_bad_value);
14232 ret = FALSE;
14233 }
14234
14235 if (can_plt_call
14236 && (stub_entry->stub_type == ppc_stub_plt_call
14237 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14238 unresolved_reloc = FALSE;
14239 }
14240
14241 if ((stub_entry == NULL
14242 || stub_entry->stub_type == ppc_stub_long_branch
14243 || stub_entry->stub_type == ppc_stub_plt_branch)
14244 && get_opd_info (sec) != NULL)
14245 {
14246 /* The branch destination is the value of the opd entry. */
14247 bfd_vma off = (relocation + addend
14248 - sec->output_section->vma
14249 - sec->output_offset);
14250 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14251 if (dest != (bfd_vma) -1)
14252 {
14253 relocation = dest;
14254 addend = 0;
14255 reloc_dest = DEST_OPD;
14256 }
14257 }
14258
14259 /* If the branch is out of reach we ought to have a long
14260 branch stub. */
14261 from = (rel->r_offset
14262 + input_section->output_offset
14263 + input_section->output_section->vma);
14264
14265 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14266 ? fdh->elf.other
14267 : sym->st_other);
14268
14269 if (stub_entry != NULL
14270 && (stub_entry->stub_type == ppc_stub_long_branch
14271 || stub_entry->stub_type == ppc_stub_plt_branch)
14272 && (r_type == R_PPC64_ADDR14_BRTAKEN
14273 || r_type == R_PPC64_ADDR14_BRNTAKEN
14274 || (relocation + addend - from + max_br_offset
14275 < 2 * max_br_offset)))
14276 /* Don't use the stub if this branch is in range. */
14277 stub_entry = NULL;
14278
14279 if (stub_entry != NULL)
14280 {
14281 /* Munge up the value and addend so that we call the stub
14282 rather than the procedure directly. */
14283 asection *stub_sec = stub_entry->group->stub_sec;
14284
14285 if (stub_entry->stub_type == ppc_stub_save_res)
14286 relocation += (stub_sec->output_offset
14287 + stub_sec->output_section->vma
14288 + stub_sec->size - htab->sfpr->size
14289 - htab->sfpr->output_offset
14290 - htab->sfpr->output_section->vma);
14291 else
14292 relocation = (stub_entry->stub_offset
14293 + stub_sec->output_offset
14294 + stub_sec->output_section->vma);
14295 addend = 0;
14296 reloc_dest = DEST_STUB;
14297
14298 if ((stub_entry->stub_type == ppc_stub_plt_call
14299 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14300 && (ALWAYS_EMIT_R2SAVE
14301 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14302 && rel + 1 < relend
14303 && rel[1].r_offset == rel->r_offset + 4
14304 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14305 relocation += 4;
14306 }
14307
14308 if (insn != 0)
14309 {
14310 if (is_isa_v2)
14311 {
14312 /* Set 'a' bit. This is 0b00010 in BO field for branch
14313 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14314 for branch on CTR insns (BO == 1a00t or 1a01t). */
14315 if ((insn & (0x14 << 21)) == (0x04 << 21))
14316 insn |= 0x02 << 21;
14317 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14318 insn |= 0x08 << 21;
14319 else
14320 break;
14321 }
14322 else
14323 {
14324 /* Invert 'y' bit if not the default. */
14325 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14326 insn ^= 0x01 << 21;
14327 }
14328
14329 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14330 }
14331
14332 /* NOP out calls to undefined weak functions.
14333 We can thus call a weak function without first
14334 checking whether the function is defined. */
14335 else if (h != NULL
14336 && h->elf.root.type == bfd_link_hash_undefweak
14337 && h->elf.dynindx == -1
14338 && r_type == R_PPC64_REL24
14339 && relocation == 0
14340 && addend == 0)
14341 {
14342 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14343 goto copy_reloc;
14344 }
14345 break;
14346 }
14347
14348 /* Set `addend'. */
14349 tls_type = 0;
14350 switch (r_type)
14351 {
14352 default:
14353 info->callbacks->einfo
14354 /* xgettext:c-format */
14355 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14356 input_bfd, (int) r_type, sym_name);
14357
14358 bfd_set_error (bfd_error_bad_value);
14359 ret = FALSE;
14360 goto copy_reloc;
14361
14362 case R_PPC64_NONE:
14363 case R_PPC64_TLS:
14364 case R_PPC64_TLSGD:
14365 case R_PPC64_TLSLD:
14366 case R_PPC64_TOCSAVE:
14367 case R_PPC64_GNU_VTINHERIT:
14368 case R_PPC64_GNU_VTENTRY:
14369 case R_PPC64_ENTRY:
14370 goto copy_reloc;
14371
14372 /* GOT16 relocations. Like an ADDR16 using the symbol's
14373 address in the GOT as relocation value instead of the
14374 symbol's value itself. Also, create a GOT entry for the
14375 symbol and put the symbol value there. */
14376 case R_PPC64_GOT_TLSGD16:
14377 case R_PPC64_GOT_TLSGD16_LO:
14378 case R_PPC64_GOT_TLSGD16_HI:
14379 case R_PPC64_GOT_TLSGD16_HA:
14380 tls_type = TLS_TLS | TLS_GD;
14381 goto dogot;
14382
14383 case R_PPC64_GOT_TLSLD16:
14384 case R_PPC64_GOT_TLSLD16_LO:
14385 case R_PPC64_GOT_TLSLD16_HI:
14386 case R_PPC64_GOT_TLSLD16_HA:
14387 tls_type = TLS_TLS | TLS_LD;
14388 goto dogot;
14389
14390 case R_PPC64_GOT_TPREL16_DS:
14391 case R_PPC64_GOT_TPREL16_LO_DS:
14392 case R_PPC64_GOT_TPREL16_HI:
14393 case R_PPC64_GOT_TPREL16_HA:
14394 tls_type = TLS_TLS | TLS_TPREL;
14395 goto dogot;
14396
14397 case R_PPC64_GOT_DTPREL16_DS:
14398 case R_PPC64_GOT_DTPREL16_LO_DS:
14399 case R_PPC64_GOT_DTPREL16_HI:
14400 case R_PPC64_GOT_DTPREL16_HA:
14401 tls_type = TLS_TLS | TLS_DTPREL;
14402 goto dogot;
14403
14404 case R_PPC64_GOT16:
14405 case R_PPC64_GOT16_LO:
14406 case R_PPC64_GOT16_HI:
14407 case R_PPC64_GOT16_HA:
14408 case R_PPC64_GOT16_DS:
14409 case R_PPC64_GOT16_LO_DS:
14410 dogot:
14411 {
14412 /* Relocation is to the entry for this symbol in the global
14413 offset table. */
14414 asection *got;
14415 bfd_vma *offp;
14416 bfd_vma off;
14417 unsigned long indx = 0;
14418 struct got_entry *ent;
14419
14420 if (tls_type == (TLS_TLS | TLS_LD)
14421 && (h == NULL
14422 || !h->elf.def_dynamic))
14423 ent = ppc64_tlsld_got (input_bfd);
14424 else
14425 {
14426 if (h != NULL)
14427 {
14428 if (!htab->elf.dynamic_sections_created
14429 || h->elf.dynindx == -1
14430 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14431 || (ELF_ST_VISIBILITY (h->elf.other) != STV_DEFAULT
14432 && h->elf.root.type == bfd_link_hash_undefweak))
14433 /* This is actually a static link, or it is a
14434 -Bsymbolic link and the symbol is defined
14435 locally, or the symbol was forced to be local
14436 because of a version file. */
14437 ;
14438 else
14439 {
14440 indx = h->elf.dynindx;
14441 unresolved_reloc = FALSE;
14442 }
14443 ent = h->elf.got.glist;
14444 }
14445 else
14446 {
14447 if (local_got_ents == NULL)
14448 abort ();
14449 ent = local_got_ents[r_symndx];
14450 }
14451
14452 for (; ent != NULL; ent = ent->next)
14453 if (ent->addend == orig_rel.r_addend
14454 && ent->owner == input_bfd
14455 && ent->tls_type == tls_type)
14456 break;
14457 }
14458
14459 if (ent == NULL)
14460 abort ();
14461 if (ent->is_indirect)
14462 ent = ent->got.ent;
14463 offp = &ent->got.offset;
14464 got = ppc64_elf_tdata (ent->owner)->got;
14465 if (got == NULL)
14466 abort ();
14467
14468 /* The offset must always be a multiple of 8. We use the
14469 least significant bit to record whether we have already
14470 processed this entry. */
14471 off = *offp;
14472 if ((off & 1) != 0)
14473 off &= ~1;
14474 else
14475 {
14476 /* Generate relocs for the dynamic linker, except in
14477 the case of TLSLD where we'll use one entry per
14478 module. */
14479 asection *relgot;
14480 bfd_boolean ifunc;
14481
14482 *offp = off | 1;
14483 relgot = NULL;
14484 ifunc = (h != NULL
14485 ? h->elf.type == STT_GNU_IFUNC
14486 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14487 if (ifunc)
14488 relgot = htab->elf.irelplt;
14489 else if (indx != 0
14490 || (bfd_link_pic (info)
14491 && (h == NULL
14492 || (ELF_ST_VISIBILITY (h->elf.other)
14493 == STV_DEFAULT)
14494 || h->elf.root.type != bfd_link_hash_undefweak
14495 || (tls_type == (TLS_TLS | TLS_LD)
14496 && !h->elf.def_dynamic))))
14497 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14498 if (relgot != NULL)
14499 {
14500 outrel.r_offset = (got->output_section->vma
14501 + got->output_offset
14502 + off);
14503 outrel.r_addend = addend;
14504 if (tls_type & (TLS_LD | TLS_GD))
14505 {
14506 outrel.r_addend = 0;
14507 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14508 if (tls_type == (TLS_TLS | TLS_GD))
14509 {
14510 loc = relgot->contents;
14511 loc += (relgot->reloc_count++
14512 * sizeof (Elf64_External_Rela));
14513 bfd_elf64_swap_reloca_out (output_bfd,
14514 &outrel, loc);
14515 outrel.r_offset += 8;
14516 outrel.r_addend = addend;
14517 outrel.r_info
14518 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14519 }
14520 }
14521 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14522 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14523 else if (tls_type == (TLS_TLS | TLS_TPREL))
14524 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14525 else if (indx != 0)
14526 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14527 else
14528 {
14529 if (ifunc)
14530 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14531 else
14532 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14533
14534 /* Write the .got section contents for the sake
14535 of prelink. */
14536 loc = got->contents + off;
14537 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14538 loc);
14539 }
14540
14541 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14542 {
14543 outrel.r_addend += relocation;
14544 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14545 {
14546 if (htab->elf.tls_sec == NULL)
14547 outrel.r_addend = 0;
14548 else
14549 outrel.r_addend -= htab->elf.tls_sec->vma;
14550 }
14551 }
14552 loc = relgot->contents;
14553 loc += (relgot->reloc_count++
14554 * sizeof (Elf64_External_Rela));
14555 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14556 }
14557
14558 /* Init the .got section contents here if we're not
14559 emitting a reloc. */
14560 else
14561 {
14562 int tlsopt
14563 = (htab->params->tls_get_addr_opt
14564 && htab->tls_get_addr_fd != NULL
14565 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
14566
14567 relocation += addend;
14568 if (tls_type != 0)
14569 {
14570 if (htab->elf.tls_sec == NULL)
14571 relocation = 0;
14572 else
14573 {
14574 if (tls_type & TLS_LD)
14575 relocation = 0;
14576 else
14577 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14578 if ((tls_type & TLS_TPREL)
14579 || (tlsopt && !(tls_type & TLS_DTPREL)))
14580 relocation += DTP_OFFSET - TP_OFFSET;
14581 }
14582
14583 if (tls_type & (TLS_GD | TLS_LD))
14584 {
14585 bfd_put_64 (output_bfd, relocation,
14586 got->contents + off + 8);
14587 relocation = !tlsopt;
14588 }
14589 }
14590 bfd_put_64 (output_bfd, relocation,
14591 got->contents + off);
14592 }
14593 }
14594
14595 if (off >= (bfd_vma) -2)
14596 abort ();
14597
14598 relocation = got->output_section->vma + got->output_offset + off;
14599 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14600 }
14601 break;
14602
14603 case R_PPC64_PLT16_HA:
14604 case R_PPC64_PLT16_HI:
14605 case R_PPC64_PLT16_LO:
14606 case R_PPC64_PLT32:
14607 case R_PPC64_PLT64:
14608 /* Relocation is to the entry for this symbol in the
14609 procedure linkage table. */
14610 {
14611 struct plt_entry **plt_list = NULL;
14612 if (h != NULL)
14613 plt_list = &h->elf.plt.plist;
14614 else if (local_got_ents != NULL)
14615 {
14616 struct plt_entry **local_plt = (struct plt_entry **)
14617 (local_got_ents + symtab_hdr->sh_info);
14618 unsigned char *local_got_tls_masks = (unsigned char *)
14619 (local_plt + symtab_hdr->sh_info);
14620 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14621 plt_list = local_plt + r_symndx;
14622 }
14623 if (plt_list)
14624 {
14625 struct plt_entry *ent;
14626
14627 for (ent = *plt_list; ent != NULL; ent = ent->next)
14628 if (ent->plt.offset != (bfd_vma) -1
14629 && ent->addend == orig_rel.r_addend)
14630 {
14631 asection *plt;
14632
14633 plt = htab->elf.splt;
14634 if (!htab->elf.dynamic_sections_created
14635 || h == NULL
14636 || h->elf.dynindx == -1)
14637 plt = htab->elf.iplt;
14638 relocation = (plt->output_section->vma
14639 + plt->output_offset
14640 + ent->plt.offset);
14641 addend = 0;
14642 unresolved_reloc = FALSE;
14643 break;
14644 }
14645 }
14646 }
14647 break;
14648
14649 case R_PPC64_TOC:
14650 /* Relocation value is TOC base. */
14651 relocation = TOCstart;
14652 if (r_symndx == STN_UNDEF)
14653 relocation += htab->sec_info[input_section->id].toc_off;
14654 else if (unresolved_reloc)
14655 ;
14656 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14657 relocation += htab->sec_info[sec->id].toc_off;
14658 else
14659 unresolved_reloc = TRUE;
14660 goto dodyn;
14661
14662 /* TOC16 relocs. We want the offset relative to the TOC base,
14663 which is the address of the start of the TOC plus 0x8000.
14664 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14665 in this order. */
14666 case R_PPC64_TOC16:
14667 case R_PPC64_TOC16_LO:
14668 case R_PPC64_TOC16_HI:
14669 case R_PPC64_TOC16_DS:
14670 case R_PPC64_TOC16_LO_DS:
14671 case R_PPC64_TOC16_HA:
14672 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14673 break;
14674
14675 /* Relocate against the beginning of the section. */
14676 case R_PPC64_SECTOFF:
14677 case R_PPC64_SECTOFF_LO:
14678 case R_PPC64_SECTOFF_HI:
14679 case R_PPC64_SECTOFF_DS:
14680 case R_PPC64_SECTOFF_LO_DS:
14681 case R_PPC64_SECTOFF_HA:
14682 if (sec != NULL)
14683 addend -= sec->output_section->vma;
14684 break;
14685
14686 case R_PPC64_REL16:
14687 case R_PPC64_REL16_LO:
14688 case R_PPC64_REL16_HI:
14689 case R_PPC64_REL16_HA:
14690 case R_PPC64_REL16DX_HA:
14691 break;
14692
14693 case R_PPC64_REL14:
14694 case R_PPC64_REL14_BRNTAKEN:
14695 case R_PPC64_REL14_BRTAKEN:
14696 case R_PPC64_REL24:
14697 break;
14698
14699 case R_PPC64_TPREL16:
14700 case R_PPC64_TPREL16_LO:
14701 case R_PPC64_TPREL16_HI:
14702 case R_PPC64_TPREL16_HA:
14703 case R_PPC64_TPREL16_DS:
14704 case R_PPC64_TPREL16_LO_DS:
14705 case R_PPC64_TPREL16_HIGH:
14706 case R_PPC64_TPREL16_HIGHA:
14707 case R_PPC64_TPREL16_HIGHER:
14708 case R_PPC64_TPREL16_HIGHERA:
14709 case R_PPC64_TPREL16_HIGHEST:
14710 case R_PPC64_TPREL16_HIGHESTA:
14711 if (h != NULL
14712 && h->elf.root.type == bfd_link_hash_undefweak
14713 && h->elf.dynindx == -1)
14714 {
14715 /* Make this relocation against an undefined weak symbol
14716 resolve to zero. This is really just a tweak, since
14717 code using weak externs ought to check that they are
14718 defined before using them. */
14719 bfd_byte *p = contents + rel->r_offset - d_offset;
14720
14721 insn = bfd_get_32 (input_bfd, p);
14722 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14723 if (insn != 0)
14724 bfd_put_32 (input_bfd, insn, p);
14725 break;
14726 }
14727 if (htab->elf.tls_sec != NULL)
14728 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14729 if (bfd_link_pic (info))
14730 /* The TPREL16 relocs shouldn't really be used in shared
14731 libs as they will result in DT_TEXTREL being set, but
14732 support them anyway. */
14733 goto dodyn;
14734 break;
14735
14736 case R_PPC64_DTPREL16:
14737 case R_PPC64_DTPREL16_LO:
14738 case R_PPC64_DTPREL16_HI:
14739 case R_PPC64_DTPREL16_HA:
14740 case R_PPC64_DTPREL16_DS:
14741 case R_PPC64_DTPREL16_LO_DS:
14742 case R_PPC64_DTPREL16_HIGH:
14743 case R_PPC64_DTPREL16_HIGHA:
14744 case R_PPC64_DTPREL16_HIGHER:
14745 case R_PPC64_DTPREL16_HIGHERA:
14746 case R_PPC64_DTPREL16_HIGHEST:
14747 case R_PPC64_DTPREL16_HIGHESTA:
14748 if (htab->elf.tls_sec != NULL)
14749 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14750 break;
14751
14752 case R_PPC64_ADDR64_LOCAL:
14753 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14754 ? h->elf.other
14755 : sym->st_other);
14756 break;
14757
14758 case R_PPC64_DTPMOD64:
14759 relocation = 1;
14760 addend = 0;
14761 goto dodyn;
14762
14763 case R_PPC64_TPREL64:
14764 if (htab->elf.tls_sec != NULL)
14765 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14766 goto dodyn;
14767
14768 case R_PPC64_DTPREL64:
14769 if (htab->elf.tls_sec != NULL)
14770 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14771 /* Fall through. */
14772
14773 /* Relocations that may need to be propagated if this is a
14774 dynamic object. */
14775 case R_PPC64_REL30:
14776 case R_PPC64_REL32:
14777 case R_PPC64_REL64:
14778 case R_PPC64_ADDR14:
14779 case R_PPC64_ADDR14_BRNTAKEN:
14780 case R_PPC64_ADDR14_BRTAKEN:
14781 case R_PPC64_ADDR16:
14782 case R_PPC64_ADDR16_DS:
14783 case R_PPC64_ADDR16_HA:
14784 case R_PPC64_ADDR16_HI:
14785 case R_PPC64_ADDR16_HIGH:
14786 case R_PPC64_ADDR16_HIGHA:
14787 case R_PPC64_ADDR16_HIGHER:
14788 case R_PPC64_ADDR16_HIGHERA:
14789 case R_PPC64_ADDR16_HIGHEST:
14790 case R_PPC64_ADDR16_HIGHESTA:
14791 case R_PPC64_ADDR16_LO:
14792 case R_PPC64_ADDR16_LO_DS:
14793 case R_PPC64_ADDR24:
14794 case R_PPC64_ADDR32:
14795 case R_PPC64_ADDR64:
14796 case R_PPC64_UADDR16:
14797 case R_PPC64_UADDR32:
14798 case R_PPC64_UADDR64:
14799 dodyn:
14800 if ((input_section->flags & SEC_ALLOC) == 0)
14801 break;
14802
14803 if (NO_OPD_RELOCS && is_opd)
14804 break;
14805
14806 if (bfd_link_pic (info)
14807 ? ((h == NULL
14808 || h->dyn_relocs != NULL)
14809 && ((h != NULL && pc_dynrelocs (h))
14810 || must_be_dyn_reloc (info, r_type)))
14811 : (h != NULL
14812 ? h->dyn_relocs != NULL
14813 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14814 {
14815 bfd_boolean skip, relocate;
14816 asection *sreloc;
14817 bfd_vma out_off;
14818
14819 /* When generating a dynamic object, these relocations
14820 are copied into the output file to be resolved at run
14821 time. */
14822
14823 skip = FALSE;
14824 relocate = FALSE;
14825
14826 out_off = _bfd_elf_section_offset (output_bfd, info,
14827 input_section, rel->r_offset);
14828 if (out_off == (bfd_vma) -1)
14829 skip = TRUE;
14830 else if (out_off == (bfd_vma) -2)
14831 skip = TRUE, relocate = TRUE;
14832 out_off += (input_section->output_section->vma
14833 + input_section->output_offset);
14834 outrel.r_offset = out_off;
14835 outrel.r_addend = rel->r_addend;
14836
14837 /* Optimize unaligned reloc use. */
14838 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14839 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14840 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14841 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14842 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14843 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14844 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14845 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14846 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14847
14848 if (skip)
14849 memset (&outrel, 0, sizeof outrel);
14850 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14851 && !is_opd
14852 && r_type != R_PPC64_TOC)
14853 {
14854 BFD_ASSERT (h->elf.dynindx != -1);
14855 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
14856 }
14857 else
14858 {
14859 /* This symbol is local, or marked to become local,
14860 or this is an opd section reloc which must point
14861 at a local function. */
14862 outrel.r_addend += relocation;
14863 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14864 {
14865 if (is_opd && h != NULL)
14866 {
14867 /* Lie about opd entries. This case occurs
14868 when building shared libraries and we
14869 reference a function in another shared
14870 lib. The same thing happens for a weak
14871 definition in an application that's
14872 overridden by a strong definition in a
14873 shared lib. (I believe this is a generic
14874 bug in binutils handling of weak syms.)
14875 In these cases we won't use the opd
14876 entry in this lib. */
14877 unresolved_reloc = FALSE;
14878 }
14879 if (!is_opd
14880 && r_type == R_PPC64_ADDR64
14881 && (h != NULL
14882 ? h->elf.type == STT_GNU_IFUNC
14883 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14884 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14885 else
14886 {
14887 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14888
14889 /* We need to relocate .opd contents for ld.so.
14890 Prelink also wants simple and consistent rules
14891 for relocs. This make all RELATIVE relocs have
14892 *r_offset equal to r_addend. */
14893 relocate = TRUE;
14894 }
14895 }
14896 else
14897 {
14898 long indx = 0;
14899
14900 if (h != NULL
14901 ? h->elf.type == STT_GNU_IFUNC
14902 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14903 {
14904 info->callbacks->einfo
14905 /* xgettext:c-format */
14906 (_("%H: %s for indirect "
14907 "function `%T' unsupported\n"),
14908 input_bfd, input_section, rel->r_offset,
14909 ppc64_elf_howto_table[r_type]->name,
14910 sym_name);
14911 ret = FALSE;
14912 }
14913 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14914 ;
14915 else if (sec == NULL || sec->owner == NULL)
14916 {
14917 bfd_set_error (bfd_error_bad_value);
14918 return FALSE;
14919 }
14920 else
14921 {
14922 asection *osec;
14923
14924 osec = sec->output_section;
14925 indx = elf_section_data (osec)->dynindx;
14926
14927 if (indx == 0)
14928 {
14929 if ((osec->flags & SEC_READONLY) == 0
14930 && htab->elf.data_index_section != NULL)
14931 osec = htab->elf.data_index_section;
14932 else
14933 osec = htab->elf.text_index_section;
14934 indx = elf_section_data (osec)->dynindx;
14935 }
14936 BFD_ASSERT (indx != 0);
14937
14938 /* We are turning this relocation into one
14939 against a section symbol, so subtract out
14940 the output section's address but not the
14941 offset of the input section in the output
14942 section. */
14943 outrel.r_addend -= osec->vma;
14944 }
14945
14946 outrel.r_info = ELF64_R_INFO (indx, r_type);
14947 }
14948 }
14949
14950 sreloc = elf_section_data (input_section)->sreloc;
14951 if (h != NULL
14952 ? h->elf.type == STT_GNU_IFUNC
14953 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14954 sreloc = htab->elf.irelplt;
14955 if (sreloc == NULL)
14956 abort ();
14957
14958 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14959 >= sreloc->size)
14960 abort ();
14961 loc = sreloc->contents;
14962 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14963 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14964
14965 /* If this reloc is against an external symbol, it will
14966 be computed at runtime, so there's no need to do
14967 anything now. However, for the sake of prelink ensure
14968 that the section contents are a known value. */
14969 if (! relocate)
14970 {
14971 unresolved_reloc = FALSE;
14972 /* The value chosen here is quite arbitrary as ld.so
14973 ignores section contents except for the special
14974 case of .opd where the contents might be accessed
14975 before relocation. Choose zero, as that won't
14976 cause reloc overflow. */
14977 relocation = 0;
14978 addend = 0;
14979 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14980 to improve backward compatibility with older
14981 versions of ld. */
14982 if (r_type == R_PPC64_ADDR64)
14983 addend = outrel.r_addend;
14984 /* Adjust pc_relative relocs to have zero in *r_offset. */
14985 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14986 addend = outrel.r_offset;
14987 }
14988 }
14989 else if (r_type == R_PPC64_DTPMOD64
14990 && htab->params->tls_get_addr_opt
14991 && htab->tls_get_addr_fd != NULL
14992 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
14993 {
14994 /* Set up for __tls_get_addr_opt stub, when this entry
14995 does not have dynamic relocs. */
14996 relocation = 0;
14997 /* Set up the next word for local dynamic. If it turns
14998 out to be global dynamic, the reloc will overwrite
14999 this value. */
15000 if (rel->r_offset + 16 <= input_section->size)
15001 bfd_put_64 (input_bfd, DTP_OFFSET - TP_OFFSET,
15002 contents + rel->r_offset + 8);
15003 }
15004 else if (r_type == R_PPC64_DTPREL64
15005 && htab->params->tls_get_addr_opt
15006 && htab->tls_get_addr_fd != NULL
15007 && htab->tls_get_addr_fd->elf.plt.plist != NULL
15008 && rel > relocs
15009 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
15010 && rel[-1].r_offset + 8 == rel->r_offset)
15011 {
15012 /* __tls_get_addr_opt stub value. */
15013 addend += DTP_OFFSET - TP_OFFSET;
15014 }
15015 break;
15016
15017 case R_PPC64_COPY:
15018 case R_PPC64_GLOB_DAT:
15019 case R_PPC64_JMP_SLOT:
15020 case R_PPC64_JMP_IREL:
15021 case R_PPC64_RELATIVE:
15022 /* We shouldn't ever see these dynamic relocs in relocatable
15023 files. */
15024 /* Fall through. */
15025
15026 case R_PPC64_PLTGOT16:
15027 case R_PPC64_PLTGOT16_DS:
15028 case R_PPC64_PLTGOT16_HA:
15029 case R_PPC64_PLTGOT16_HI:
15030 case R_PPC64_PLTGOT16_LO:
15031 case R_PPC64_PLTGOT16_LO_DS:
15032 case R_PPC64_PLTREL32:
15033 case R_PPC64_PLTREL64:
15034 /* These ones haven't been implemented yet. */
15035
15036 info->callbacks->einfo
15037 /* xgettext:c-format */
15038 (_("%P: %B: %s is not supported for `%T'\n"),
15039 input_bfd,
15040 ppc64_elf_howto_table[r_type]->name, sym_name);
15041
15042 bfd_set_error (bfd_error_invalid_operation);
15043 ret = FALSE;
15044 goto copy_reloc;
15045 }
15046
15047 /* Multi-instruction sequences that access the TOC can be
15048 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15049 to nop; addi rb,r2,x; */
15050 switch (r_type)
15051 {
15052 default:
15053 break;
15054
15055 case R_PPC64_GOT_TLSLD16_HI:
15056 case R_PPC64_GOT_TLSGD16_HI:
15057 case R_PPC64_GOT_TPREL16_HI:
15058 case R_PPC64_GOT_DTPREL16_HI:
15059 case R_PPC64_GOT16_HI:
15060 case R_PPC64_TOC16_HI:
15061 /* These relocs would only be useful if building up an
15062 offset to later add to r2, perhaps in an indexed
15063 addressing mode instruction. Don't try to optimize.
15064 Unfortunately, the possibility of someone building up an
15065 offset like this or even with the HA relocs, means that
15066 we need to check the high insn when optimizing the low
15067 insn. */
15068 break;
15069
15070 case R_PPC64_GOT_TLSLD16_HA:
15071 case R_PPC64_GOT_TLSGD16_HA:
15072 case R_PPC64_GOT_TPREL16_HA:
15073 case R_PPC64_GOT_DTPREL16_HA:
15074 case R_PPC64_GOT16_HA:
15075 case R_PPC64_TOC16_HA:
15076 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15077 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15078 {
15079 bfd_byte *p = contents + (rel->r_offset & ~3);
15080 bfd_put_32 (input_bfd, NOP, p);
15081 }
15082 break;
15083
15084 case R_PPC64_GOT_TLSLD16_LO:
15085 case R_PPC64_GOT_TLSGD16_LO:
15086 case R_PPC64_GOT_TPREL16_LO_DS:
15087 case R_PPC64_GOT_DTPREL16_LO_DS:
15088 case R_PPC64_GOT16_LO:
15089 case R_PPC64_GOT16_LO_DS:
15090 case R_PPC64_TOC16_LO:
15091 case R_PPC64_TOC16_LO_DS:
15092 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15093 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15094 {
15095 bfd_byte *p = contents + (rel->r_offset & ~3);
15096 insn = bfd_get_32 (input_bfd, p);
15097 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15098 {
15099 /* Transform addic to addi when we change reg. */
15100 insn &= ~((0x3f << 26) | (0x1f << 16));
15101 insn |= (14u << 26) | (2 << 16);
15102 }
15103 else
15104 {
15105 insn &= ~(0x1f << 16);
15106 insn |= 2 << 16;
15107 }
15108 bfd_put_32 (input_bfd, insn, p);
15109 }
15110 break;
15111 }
15112
15113 /* Do any further special processing. */
15114 howto = ppc64_elf_howto_table[(int) r_type];
15115 switch (r_type)
15116 {
15117 default:
15118 break;
15119
15120 case R_PPC64_REL16_HA:
15121 case R_PPC64_REL16DX_HA:
15122 case R_PPC64_ADDR16_HA:
15123 case R_PPC64_ADDR16_HIGHA:
15124 case R_PPC64_ADDR16_HIGHERA:
15125 case R_PPC64_ADDR16_HIGHESTA:
15126 case R_PPC64_TOC16_HA:
15127 case R_PPC64_SECTOFF_HA:
15128 case R_PPC64_TPREL16_HA:
15129 case R_PPC64_TPREL16_HIGHA:
15130 case R_PPC64_TPREL16_HIGHERA:
15131 case R_PPC64_TPREL16_HIGHESTA:
15132 case R_PPC64_DTPREL16_HA:
15133 case R_PPC64_DTPREL16_HIGHA:
15134 case R_PPC64_DTPREL16_HIGHERA:
15135 case R_PPC64_DTPREL16_HIGHESTA:
15136 /* It's just possible that this symbol is a weak symbol
15137 that's not actually defined anywhere. In that case,
15138 'sec' would be NULL, and we should leave the symbol
15139 alone (it will be set to zero elsewhere in the link). */
15140 if (sec == NULL)
15141 break;
15142 /* Fall through. */
15143
15144 case R_PPC64_GOT16_HA:
15145 case R_PPC64_PLTGOT16_HA:
15146 case R_PPC64_PLT16_HA:
15147 case R_PPC64_GOT_TLSGD16_HA:
15148 case R_PPC64_GOT_TLSLD16_HA:
15149 case R_PPC64_GOT_TPREL16_HA:
15150 case R_PPC64_GOT_DTPREL16_HA:
15151 /* Add 0x10000 if sign bit in 0:15 is set.
15152 Bits 0:15 are not used. */
15153 addend += 0x8000;
15154 break;
15155
15156 case R_PPC64_ADDR16_DS:
15157 case R_PPC64_ADDR16_LO_DS:
15158 case R_PPC64_GOT16_DS:
15159 case R_PPC64_GOT16_LO_DS:
15160 case R_PPC64_PLT16_LO_DS:
15161 case R_PPC64_SECTOFF_DS:
15162 case R_PPC64_SECTOFF_LO_DS:
15163 case R_PPC64_TOC16_DS:
15164 case R_PPC64_TOC16_LO_DS:
15165 case R_PPC64_PLTGOT16_DS:
15166 case R_PPC64_PLTGOT16_LO_DS:
15167 case R_PPC64_GOT_TPREL16_DS:
15168 case R_PPC64_GOT_TPREL16_LO_DS:
15169 case R_PPC64_GOT_DTPREL16_DS:
15170 case R_PPC64_GOT_DTPREL16_LO_DS:
15171 case R_PPC64_TPREL16_DS:
15172 case R_PPC64_TPREL16_LO_DS:
15173 case R_PPC64_DTPREL16_DS:
15174 case R_PPC64_DTPREL16_LO_DS:
15175 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15176 mask = 3;
15177 /* If this reloc is against an lq, lxv, or stxv insn, then
15178 the value must be a multiple of 16. This is somewhat of
15179 a hack, but the "correct" way to do this by defining _DQ
15180 forms of all the _DS relocs bloats all reloc switches in
15181 this file. It doesn't make much sense to use these
15182 relocs in data, so testing the insn should be safe. */
15183 if ((insn & (0x3f << 26)) == (56u << 26)
15184 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15185 mask = 15;
15186 relocation += addend;
15187 addend = insn & (mask ^ 3);
15188 if ((relocation & mask) != 0)
15189 {
15190 relocation ^= relocation & mask;
15191 info->callbacks->einfo
15192 /* xgettext:c-format */
15193 (_("%H: error: %s not a multiple of %u\n"),
15194 input_bfd, input_section, rel->r_offset,
15195 howto->name,
15196 mask + 1);
15197 bfd_set_error (bfd_error_bad_value);
15198 ret = FALSE;
15199 goto copy_reloc;
15200 }
15201 break;
15202 }
15203
15204 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15205 because such sections are not SEC_ALLOC and thus ld.so will
15206 not process them. */
15207 if (unresolved_reloc
15208 && !((input_section->flags & SEC_DEBUGGING) != 0
15209 && h->elf.def_dynamic)
15210 && _bfd_elf_section_offset (output_bfd, info, input_section,
15211 rel->r_offset) != (bfd_vma) -1)
15212 {
15213 info->callbacks->einfo
15214 /* xgettext:c-format */
15215 (_("%H: unresolvable %s against `%T'\n"),
15216 input_bfd, input_section, rel->r_offset,
15217 howto->name,
15218 h->elf.root.root.string);
15219 ret = FALSE;
15220 }
15221
15222 /* 16-bit fields in insns mostly have signed values, but a
15223 few insns have 16-bit unsigned values. Really, we should
15224 have different reloc types. */
15225 if (howto->complain_on_overflow != complain_overflow_dont
15226 && howto->dst_mask == 0xffff
15227 && (input_section->flags & SEC_CODE) != 0)
15228 {
15229 enum complain_overflow complain = complain_overflow_signed;
15230
15231 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15232 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15233 complain = complain_overflow_bitfield;
15234 else if (howto->rightshift == 0
15235 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15236 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15237 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15238 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15239 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15240 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15241 complain = complain_overflow_unsigned;
15242 if (howto->complain_on_overflow != complain)
15243 {
15244 alt_howto = *howto;
15245 alt_howto.complain_on_overflow = complain;
15246 howto = &alt_howto;
15247 }
15248 }
15249
15250 if (r_type == R_PPC64_REL16DX_HA)
15251 {
15252 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15253 if (rel->r_offset + 4 > input_section->size)
15254 r = bfd_reloc_outofrange;
15255 else
15256 {
15257 relocation += addend;
15258 relocation -= (rel->r_offset
15259 + input_section->output_offset
15260 + input_section->output_section->vma);
15261 relocation = (bfd_signed_vma) relocation >> 16;
15262 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15263 insn &= ~0x1fffc1;
15264 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15265 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15266 r = bfd_reloc_ok;
15267 if (relocation + 0x8000 > 0xffff)
15268 r = bfd_reloc_overflow;
15269 }
15270 }
15271 else
15272 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15273 rel->r_offset, relocation, addend);
15274
15275 if (r != bfd_reloc_ok)
15276 {
15277 char *more_info = NULL;
15278 const char *reloc_name = howto->name;
15279
15280 if (reloc_dest != DEST_NORMAL)
15281 {
15282 more_info = bfd_malloc (strlen (reloc_name) + 8);
15283 if (more_info != NULL)
15284 {
15285 strcpy (more_info, reloc_name);
15286 strcat (more_info, (reloc_dest == DEST_OPD
15287 ? " (OPD)" : " (stub)"));
15288 reloc_name = more_info;
15289 }
15290 }
15291
15292 if (r == bfd_reloc_overflow)
15293 {
15294 /* On code like "if (foo) foo();" don't report overflow
15295 on a branch to zero when foo is undefined. */
15296 if (!warned
15297 && (reloc_dest == DEST_STUB
15298 || !(h != NULL
15299 && (h->elf.root.type == bfd_link_hash_undefweak
15300 || h->elf.root.type == bfd_link_hash_undefined)
15301 && is_branch_reloc (r_type))))
15302 info->callbacks->reloc_overflow (info, &h->elf.root,
15303 sym_name, reloc_name,
15304 orig_rel.r_addend,
15305 input_bfd, input_section,
15306 rel->r_offset);
15307 }
15308 else
15309 {
15310 info->callbacks->einfo
15311 /* xgettext:c-format */
15312 (_("%H: %s against `%T': error %d\n"),
15313 input_bfd, input_section, rel->r_offset,
15314 reloc_name, sym_name, (int) r);
15315 ret = FALSE;
15316 }
15317 if (more_info != NULL)
15318 free (more_info);
15319 }
15320 copy_reloc:
15321 if (wrel != rel)
15322 *wrel = *rel;
15323 }
15324
15325 if (wrel != rel)
15326 {
15327 Elf_Internal_Shdr *rel_hdr;
15328 size_t deleted = rel - wrel;
15329
15330 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15331 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15332 if (rel_hdr->sh_size == 0)
15333 {
15334 /* It is too late to remove an empty reloc section. Leave
15335 one NONE reloc.
15336 ??? What is wrong with an empty section??? */
15337 rel_hdr->sh_size = rel_hdr->sh_entsize;
15338 deleted -= 1;
15339 }
15340 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15341 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15342 input_section->reloc_count -= deleted;
15343 }
15344
15345 /* If we're emitting relocations, then shortly after this function
15346 returns, reloc offsets and addends for this section will be
15347 adjusted. Worse, reloc symbol indices will be for the output
15348 file rather than the input. Save a copy of the relocs for
15349 opd_entry_value. */
15350 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15351 {
15352 bfd_size_type amt;
15353 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15354 rel = bfd_alloc (input_bfd, amt);
15355 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15356 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15357 if (rel == NULL)
15358 return FALSE;
15359 memcpy (rel, relocs, amt);
15360 }
15361 return ret;
15362 }
15363
15364 /* Adjust the value of any local symbols in opd sections. */
15365
15366 static int
15367 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15368 const char *name ATTRIBUTE_UNUSED,
15369 Elf_Internal_Sym *elfsym,
15370 asection *input_sec,
15371 struct elf_link_hash_entry *h)
15372 {
15373 struct _opd_sec_data *opd;
15374 long adjust;
15375 bfd_vma value;
15376
15377 if (h != NULL)
15378 return 1;
15379
15380 opd = get_opd_info (input_sec);
15381 if (opd == NULL || opd->adjust == NULL)
15382 return 1;
15383
15384 value = elfsym->st_value - input_sec->output_offset;
15385 if (!bfd_link_relocatable (info))
15386 value -= input_sec->output_section->vma;
15387
15388 adjust = opd->adjust[OPD_NDX (value)];
15389 if (adjust == -1)
15390 return 2;
15391
15392 elfsym->st_value += adjust;
15393 return 1;
15394 }
15395
15396 /* Finish up dynamic symbol handling. We set the contents of various
15397 dynamic sections here. */
15398
15399 static bfd_boolean
15400 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15401 struct bfd_link_info *info,
15402 struct elf_link_hash_entry *h,
15403 Elf_Internal_Sym *sym)
15404 {
15405 struct ppc_link_hash_table *htab;
15406 struct plt_entry *ent;
15407 Elf_Internal_Rela rela;
15408 bfd_byte *loc;
15409
15410 htab = ppc_hash_table (info);
15411 if (htab == NULL)
15412 return FALSE;
15413
15414 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15415 if (ent->plt.offset != (bfd_vma) -1)
15416 {
15417 /* This symbol has an entry in the procedure linkage
15418 table. Set it up. */
15419 if (!htab->elf.dynamic_sections_created
15420 || h->dynindx == -1)
15421 {
15422 BFD_ASSERT (h->type == STT_GNU_IFUNC
15423 && h->def_regular
15424 && (h->root.type == bfd_link_hash_defined
15425 || h->root.type == bfd_link_hash_defweak));
15426 rela.r_offset = (htab->elf.iplt->output_section->vma
15427 + htab->elf.iplt->output_offset
15428 + ent->plt.offset);
15429 if (htab->opd_abi)
15430 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15431 else
15432 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15433 rela.r_addend = (h->root.u.def.value
15434 + h->root.u.def.section->output_offset
15435 + h->root.u.def.section->output_section->vma
15436 + ent->addend);
15437 loc = (htab->elf.irelplt->contents
15438 + (htab->elf.irelplt->reloc_count++
15439 * sizeof (Elf64_External_Rela)));
15440 }
15441 else
15442 {
15443 rela.r_offset = (htab->elf.splt->output_section->vma
15444 + htab->elf.splt->output_offset
15445 + ent->plt.offset);
15446 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15447 rela.r_addend = ent->addend;
15448 loc = (htab->elf.srelplt->contents
15449 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15450 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15451 }
15452 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15453
15454 if (!htab->opd_abi)
15455 {
15456 if (!h->def_regular)
15457 {
15458 /* Mark the symbol as undefined, rather than as
15459 defined in glink. Leave the value if there were
15460 any relocations where pointer equality matters
15461 (this is a clue for the dynamic linker, to make
15462 function pointer comparisons work between an
15463 application and shared library), otherwise set it
15464 to zero. */
15465 sym->st_shndx = SHN_UNDEF;
15466 if (!h->pointer_equality_needed)
15467 sym->st_value = 0;
15468 else if (!h->ref_regular_nonweak)
15469 {
15470 /* This breaks function pointer comparisons, but
15471 that is better than breaking tests for a NULL
15472 function pointer. */
15473 sym->st_value = 0;
15474 }
15475 }
15476 }
15477 }
15478
15479 if (h->needs_copy)
15480 {
15481 /* This symbol needs a copy reloc. Set it up. */
15482 asection *srel;
15483
15484 if (h->dynindx == -1
15485 || (h->root.type != bfd_link_hash_defined
15486 && h->root.type != bfd_link_hash_defweak)
15487 || htab->elf.srelbss == NULL
15488 || htab->elf.sreldynrelro == NULL)
15489 abort ();
15490
15491 rela.r_offset = (h->root.u.def.value
15492 + h->root.u.def.section->output_section->vma
15493 + h->root.u.def.section->output_offset);
15494 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15495 rela.r_addend = 0;
15496 if (h->root.u.def.section == htab->elf.sdynrelro)
15497 srel = htab->elf.sreldynrelro;
15498 else
15499 srel = htab->elf.srelbss;
15500 loc = srel->contents;
15501 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15502 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15503 }
15504
15505 return TRUE;
15506 }
15507
15508 /* Used to decide how to sort relocs in an optimal manner for the
15509 dynamic linker, before writing them out. */
15510
15511 static enum elf_reloc_type_class
15512 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15513 const asection *rel_sec,
15514 const Elf_Internal_Rela *rela)
15515 {
15516 enum elf_ppc64_reloc_type r_type;
15517 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15518
15519 if (rel_sec == htab->elf.irelplt)
15520 return reloc_class_ifunc;
15521
15522 r_type = ELF64_R_TYPE (rela->r_info);
15523 switch (r_type)
15524 {
15525 case R_PPC64_RELATIVE:
15526 return reloc_class_relative;
15527 case R_PPC64_JMP_SLOT:
15528 return reloc_class_plt;
15529 case R_PPC64_COPY:
15530 return reloc_class_copy;
15531 default:
15532 return reloc_class_normal;
15533 }
15534 }
15535
15536 /* Finish up the dynamic sections. */
15537
15538 static bfd_boolean
15539 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15540 struct bfd_link_info *info)
15541 {
15542 struct ppc_link_hash_table *htab;
15543 bfd *dynobj;
15544 asection *sdyn;
15545
15546 htab = ppc_hash_table (info);
15547 if (htab == NULL)
15548 return FALSE;
15549
15550 dynobj = htab->elf.dynobj;
15551 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15552
15553 if (htab->elf.dynamic_sections_created)
15554 {
15555 Elf64_External_Dyn *dyncon, *dynconend;
15556
15557 if (sdyn == NULL || htab->elf.sgot == NULL)
15558 abort ();
15559
15560 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15561 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15562 for (; dyncon < dynconend; dyncon++)
15563 {
15564 Elf_Internal_Dyn dyn;
15565 asection *s;
15566
15567 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15568
15569 switch (dyn.d_tag)
15570 {
15571 default:
15572 continue;
15573
15574 case DT_PPC64_GLINK:
15575 s = htab->glink;
15576 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15577 /* We stupidly defined DT_PPC64_GLINK to be the start
15578 of glink rather than the first entry point, which is
15579 what ld.so needs, and now have a bigger stub to
15580 support automatic multiple TOCs. */
15581 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 8 * 4;
15582 break;
15583
15584 case DT_PPC64_OPD:
15585 s = bfd_get_section_by_name (output_bfd, ".opd");
15586 if (s == NULL)
15587 continue;
15588 dyn.d_un.d_ptr = s->vma;
15589 break;
15590
15591 case DT_PPC64_OPT:
15592 if (htab->do_multi_toc && htab->multi_toc_needed)
15593 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15594 break;
15595
15596 case DT_PPC64_OPDSZ:
15597 s = bfd_get_section_by_name (output_bfd, ".opd");
15598 if (s == NULL)
15599 continue;
15600 dyn.d_un.d_val = s->size;
15601 break;
15602
15603 case DT_PLTGOT:
15604 s = htab->elf.splt;
15605 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15606 break;
15607
15608 case DT_JMPREL:
15609 s = htab->elf.srelplt;
15610 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15611 break;
15612
15613 case DT_PLTRELSZ:
15614 dyn.d_un.d_val = htab->elf.srelplt->size;
15615 break;
15616 }
15617
15618 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15619 }
15620 }
15621
15622 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15623 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15624 {
15625 /* Fill in the first entry in the global offset table.
15626 We use it to hold the link-time TOCbase. */
15627 bfd_put_64 (output_bfd,
15628 elf_gp (output_bfd) + TOC_BASE_OFF,
15629 htab->elf.sgot->contents);
15630
15631 /* Set .got entry size. */
15632 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15633 }
15634
15635 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15636 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15637 {
15638 /* Set .plt entry size. */
15639 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15640 = PLT_ENTRY_SIZE (htab);
15641 }
15642
15643 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15644 brlt ourselves if emitrelocations. */
15645 if (htab->brlt != NULL
15646 && htab->brlt->reloc_count != 0
15647 && !_bfd_elf_link_output_relocs (output_bfd,
15648 htab->brlt,
15649 elf_section_data (htab->brlt)->rela.hdr,
15650 elf_section_data (htab->brlt)->relocs,
15651 NULL))
15652 return FALSE;
15653
15654 if (htab->glink != NULL
15655 && htab->glink->reloc_count != 0
15656 && !_bfd_elf_link_output_relocs (output_bfd,
15657 htab->glink,
15658 elf_section_data (htab->glink)->rela.hdr,
15659 elf_section_data (htab->glink)->relocs,
15660 NULL))
15661 return FALSE;
15662
15663 if (htab->glink_eh_frame != NULL
15664 && htab->glink_eh_frame->size != 0)
15665 {
15666 bfd_vma val;
15667 bfd_byte *p;
15668 asection *stub_sec;
15669 size_t align = 4;
15670
15671 p = htab->glink_eh_frame->contents;
15672 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15673 for (stub_sec = htab->params->stub_bfd->sections;
15674 stub_sec != NULL;
15675 stub_sec = stub_sec->next)
15676 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
15677 {
15678 /* FDE length. */
15679 p += 4;
15680 /* CIE pointer. */
15681 p += 4;
15682 /* Offset to stub section. */
15683 val = (stub_sec->output_section->vma
15684 + stub_sec->output_offset);
15685 val -= (htab->glink_eh_frame->output_section->vma
15686 + htab->glink_eh_frame->output_offset
15687 + (p - htab->glink_eh_frame->contents));
15688 if (val + 0x80000000 > 0xffffffff)
15689 {
15690 info->callbacks->einfo
15691 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15692 stub_sec->name);
15693 return FALSE;
15694 }
15695 bfd_put_32 (dynobj, val, p);
15696 p += 4;
15697 /* stub section size. */
15698 p += 4;
15699 /* Augmentation. */
15700 p += 1;
15701 /* Pad. */
15702 p += ((17 + align - 1) & -align) - 17;
15703 }
15704 if (htab->glink != NULL && htab->glink->size != 0)
15705 {
15706 /* FDE length. */
15707 p += 4;
15708 /* CIE pointer. */
15709 p += 4;
15710 /* Offset to .glink. */
15711 val = (htab->glink->output_section->vma
15712 + htab->glink->output_offset
15713 + 8);
15714 val -= (htab->glink_eh_frame->output_section->vma
15715 + htab->glink_eh_frame->output_offset
15716 + (p - htab->glink_eh_frame->contents));
15717 if (val + 0x80000000 > 0xffffffff)
15718 {
15719 info->callbacks->einfo
15720 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15721 htab->glink->name);
15722 return FALSE;
15723 }
15724 bfd_put_32 (dynobj, val, p);
15725 p += 4;
15726 /* .glink size. */
15727 p += 4;
15728 /* Augmentation. */
15729 p += 1;
15730 /* Ops. */
15731 p += 7;
15732 p += ((24 + align - 1) & -align) - 24;
15733 }
15734
15735 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15736 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15737 htab->glink_eh_frame,
15738 htab->glink_eh_frame->contents))
15739 return FALSE;
15740 }
15741
15742 /* We need to handle writing out multiple GOT sections ourselves,
15743 since we didn't add them to DYNOBJ. We know dynobj is the first
15744 bfd. */
15745 while ((dynobj = dynobj->link.next) != NULL)
15746 {
15747 asection *s;
15748
15749 if (!is_ppc64_elf (dynobj))
15750 continue;
15751
15752 s = ppc64_elf_tdata (dynobj)->got;
15753 if (s != NULL
15754 && s->size != 0
15755 && s->output_section != bfd_abs_section_ptr
15756 && !bfd_set_section_contents (output_bfd, s->output_section,
15757 s->contents, s->output_offset,
15758 s->size))
15759 return FALSE;
15760 s = ppc64_elf_tdata (dynobj)->relgot;
15761 if (s != NULL
15762 && s->size != 0
15763 && s->output_section != bfd_abs_section_ptr
15764 && !bfd_set_section_contents (output_bfd, s->output_section,
15765 s->contents, s->output_offset,
15766 s->size))
15767 return FALSE;
15768 }
15769
15770 return TRUE;
15771 }
15772
15773 #include "elf64-target.h"
15774
15775 /* FreeBSD support */
15776
15777 #undef TARGET_LITTLE_SYM
15778 #undef TARGET_LITTLE_NAME
15779
15780 #undef TARGET_BIG_SYM
15781 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15782 #undef TARGET_BIG_NAME
15783 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15784
15785 #undef ELF_OSABI
15786 #define ELF_OSABI ELFOSABI_FREEBSD
15787
15788 #undef elf64_bed
15789 #define elf64_bed elf64_powerpc_fbsd_bed
15790
15791 #include "elf64-target.h"
15792
This page took 0.358468 seconds and 4 git commands to generate.