PowerPC64 __tls_get_addr_opt stub .eh_frame fix
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2018 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 0x1000
68 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
69 #define elf_info_to_howto ppc64_elf_info_to_howto
70
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
82
83 #define bfd_elf64_mkobject ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
93
94 #define elf_backend_object_p ppc64_elf_object_p
95 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded ppc64_elf_action_discarded
117 #define elf_backend_relocate_section ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section bfd_get_section_by_name
126
127 /* The name of the dynamic interpreter. This is put in the .interp
128 section. */
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
130
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
134
135 /* The initial size of the plt reserved for the dynamic linker. */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
137
138 /* Offsets to some stack save slots. */
139 #define STK_LR 16
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy. ELFv2 does not have a linker word, so use the
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
145
146 /* TOC base pointers offset from start of TOC. */
147 #define TOC_BASE_OFF 0x8000
148 /* TOC base alignment. */
149 #define TOC_BASE_ALIGN 256
150
151 /* Offset of tp and dtp pointers from start of TLS block. */
152 #define TP_OFFSET 0x7000
153 #define DTP_OFFSET 0x8000
154
155 /* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
157 insert an addi to adjust r11. */
158 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
159 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
164 #define BCTR 0x4e800420 /* bctr */
165
166 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
167 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
168 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
169
170 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
171 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
172 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
173 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
174 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
175 #define BNECTR 0x4ca20420 /* bnectr+ */
176 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
177
178 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
179 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
180 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
181
182 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
183 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
184 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
185
186 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
187 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
188 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
189 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
190 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
191
192 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
193 #define GLINK_PLTRESOLVE_SIZE(htab) \
194 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
195 /* 0: */
196 /* .quad plt0-1f */
197 /* __glink: */
198 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
199 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
200 /* 1: */
201 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
202 /* ld %2,(0b-1b)(%11) */
203 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
204 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
205 /* ld %12,0(%11) */
206 /* ld %2,8(%11) */
207 /* mtctr %12 */
208 /* ld %11,16(%11) */
209 /* bctr */
210 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
211 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
212 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
213 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
214 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
215
216 /* Pad with this. */
217 #define NOP 0x60000000
218
219 /* Some other nops. */
220 #define CROR_151515 0x4def7b82
221 #define CROR_313131 0x4ffffb82
222
223 /* .glink entries for the first 32k functions are two instructions. */
224 #define LI_R0_0 0x38000000 /* li %r0,0 */
225 #define B_DOT 0x48000000 /* b . */
226
227 /* After that, we need two instructions to load the index, followed by
228 a branch. */
229 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
230 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
231
232 /* Instructions used by the save and restore reg functions. */
233 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
234 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
235 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
236 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
237 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
238 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
239 #define LI_R12_0 0x39800000 /* li %r12,0 */
240 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
241 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
242 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
243 #define BLR 0x4e800020 /* blr */
244
245 /* Since .opd is an array of descriptors and each entry will end up
246 with identical R_PPC64_RELATIVE relocs, there is really no need to
247 propagate .opd relocs; The dynamic linker should be taught to
248 relocate .opd without reloc entries. */
249 #ifndef NO_OPD_RELOCS
250 #define NO_OPD_RELOCS 0
251 #endif
252
253 #ifndef ARRAY_SIZE
254 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
255 #endif
256
257 static inline int
258 abiversion (bfd *abfd)
259 {
260 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
261 }
262
263 static inline void
264 set_abiversion (bfd *abfd, int ver)
265 {
266 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
267 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
268 }
269 \f
270 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
271
272 /* Relocation HOWTO's. */
273 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
274
275 static reloc_howto_type ppc64_elf_howto_raw[] =
276 {
277 /* This reloc does nothing. */
278 HOWTO (R_PPC64_NONE, /* type */
279 0, /* rightshift */
280 3, /* size (0 = byte, 1 = short, 2 = long) */
281 0, /* bitsize */
282 FALSE, /* pc_relative */
283 0, /* bitpos */
284 complain_overflow_dont, /* complain_on_overflow */
285 bfd_elf_generic_reloc, /* special_function */
286 "R_PPC64_NONE", /* name */
287 FALSE, /* partial_inplace */
288 0, /* src_mask */
289 0, /* dst_mask */
290 FALSE), /* pcrel_offset */
291
292 /* A standard 32 bit relocation. */
293 HOWTO (R_PPC64_ADDR32, /* type */
294 0, /* rightshift */
295 2, /* size (0 = byte, 1 = short, 2 = long) */
296 32, /* bitsize */
297 FALSE, /* pc_relative */
298 0, /* bitpos */
299 complain_overflow_bitfield, /* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_PPC64_ADDR32", /* name */
302 FALSE, /* partial_inplace */
303 0, /* src_mask */
304 0xffffffff, /* dst_mask */
305 FALSE), /* pcrel_offset */
306
307 /* An absolute 26 bit branch; the lower two bits must be zero.
308 FIXME: we don't check that, we just clear them. */
309 HOWTO (R_PPC64_ADDR24, /* type */
310 0, /* rightshift */
311 2, /* size (0 = byte, 1 = short, 2 = long) */
312 26, /* bitsize */
313 FALSE, /* pc_relative */
314 0, /* bitpos */
315 complain_overflow_bitfield, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_PPC64_ADDR24", /* name */
318 FALSE, /* partial_inplace */
319 0, /* src_mask */
320 0x03fffffc, /* dst_mask */
321 FALSE), /* pcrel_offset */
322
323 /* A standard 16 bit relocation. */
324 HOWTO (R_PPC64_ADDR16, /* type */
325 0, /* rightshift */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
328 FALSE, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_bitfield, /* complain_on_overflow */
331 bfd_elf_generic_reloc, /* special_function */
332 "R_PPC64_ADDR16", /* name */
333 FALSE, /* partial_inplace */
334 0, /* src_mask */
335 0xffff, /* dst_mask */
336 FALSE), /* pcrel_offset */
337
338 /* A 16 bit relocation without overflow. */
339 HOWTO (R_PPC64_ADDR16_LO, /* type */
340 0, /* rightshift */
341 1, /* size (0 = byte, 1 = short, 2 = long) */
342 16, /* bitsize */
343 FALSE, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_dont,/* complain_on_overflow */
346 bfd_elf_generic_reloc, /* special_function */
347 "R_PPC64_ADDR16_LO", /* name */
348 FALSE, /* partial_inplace */
349 0, /* src_mask */
350 0xffff, /* dst_mask */
351 FALSE), /* pcrel_offset */
352
353 /* Bits 16-31 of an address. */
354 HOWTO (R_PPC64_ADDR16_HI, /* type */
355 16, /* rightshift */
356 1, /* size (0 = byte, 1 = short, 2 = long) */
357 16, /* bitsize */
358 FALSE, /* pc_relative */
359 0, /* bitpos */
360 complain_overflow_signed, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_PPC64_ADDR16_HI", /* name */
363 FALSE, /* partial_inplace */
364 0, /* src_mask */
365 0xffff, /* dst_mask */
366 FALSE), /* pcrel_offset */
367
368 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
369 bits, treated as a signed number, is negative. */
370 HOWTO (R_PPC64_ADDR16_HA, /* type */
371 16, /* rightshift */
372 1, /* size (0 = byte, 1 = short, 2 = long) */
373 16, /* bitsize */
374 FALSE, /* pc_relative */
375 0, /* bitpos */
376 complain_overflow_signed, /* complain_on_overflow */
377 ppc64_elf_ha_reloc, /* special_function */
378 "R_PPC64_ADDR16_HA", /* name */
379 FALSE, /* partial_inplace */
380 0, /* src_mask */
381 0xffff, /* dst_mask */
382 FALSE), /* pcrel_offset */
383
384 /* An absolute 16 bit branch; the lower two bits must be zero.
385 FIXME: we don't check that, we just clear them. */
386 HOWTO (R_PPC64_ADDR14, /* type */
387 0, /* rightshift */
388 2, /* size (0 = byte, 1 = short, 2 = long) */
389 16, /* bitsize */
390 FALSE, /* pc_relative */
391 0, /* bitpos */
392 complain_overflow_signed, /* complain_on_overflow */
393 ppc64_elf_branch_reloc, /* special_function */
394 "R_PPC64_ADDR14", /* name */
395 FALSE, /* partial_inplace */
396 0, /* src_mask */
397 0x0000fffc, /* dst_mask */
398 FALSE), /* pcrel_offset */
399
400 /* An absolute 16 bit branch, for which bit 10 should be set to
401 indicate that the branch is expected to be taken. The lower two
402 bits must be zero. */
403 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
404 0, /* rightshift */
405 2, /* size (0 = byte, 1 = short, 2 = long) */
406 16, /* bitsize */
407 FALSE, /* pc_relative */
408 0, /* bitpos */
409 complain_overflow_signed, /* complain_on_overflow */
410 ppc64_elf_brtaken_reloc, /* special_function */
411 "R_PPC64_ADDR14_BRTAKEN",/* name */
412 FALSE, /* partial_inplace */
413 0, /* src_mask */
414 0x0000fffc, /* dst_mask */
415 FALSE), /* pcrel_offset */
416
417 /* An absolute 16 bit branch, for which bit 10 should be set to
418 indicate that the branch is not expected to be taken. The lower
419 two bits must be zero. */
420 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
421 0, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 16, /* bitsize */
424 FALSE, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_signed, /* complain_on_overflow */
427 ppc64_elf_brtaken_reloc, /* special_function */
428 "R_PPC64_ADDR14_BRNTAKEN",/* name */
429 FALSE, /* partial_inplace */
430 0, /* src_mask */
431 0x0000fffc, /* dst_mask */
432 FALSE), /* pcrel_offset */
433
434 /* A relative 26 bit branch; the lower two bits must be zero. */
435 HOWTO (R_PPC64_REL24, /* type */
436 0, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 26, /* bitsize */
439 TRUE, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_signed, /* complain_on_overflow */
442 ppc64_elf_branch_reloc, /* special_function */
443 "R_PPC64_REL24", /* name */
444 FALSE, /* partial_inplace */
445 0, /* src_mask */
446 0x03fffffc, /* dst_mask */
447 TRUE), /* pcrel_offset */
448
449 /* A relative 16 bit branch; the lower two bits must be zero. */
450 HOWTO (R_PPC64_REL14, /* type */
451 0, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 16, /* bitsize */
454 TRUE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_signed, /* complain_on_overflow */
457 ppc64_elf_branch_reloc, /* special_function */
458 "R_PPC64_REL14", /* name */
459 FALSE, /* partial_inplace */
460 0, /* src_mask */
461 0x0000fffc, /* dst_mask */
462 TRUE), /* pcrel_offset */
463
464 /* A relative 16 bit branch. Bit 10 should be set to indicate that
465 the branch is expected to be taken. The lower two bits must be
466 zero. */
467 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 16, /* bitsize */
471 TRUE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_signed, /* complain_on_overflow */
474 ppc64_elf_brtaken_reloc, /* special_function */
475 "R_PPC64_REL14_BRTAKEN", /* name */
476 FALSE, /* partial_inplace */
477 0, /* src_mask */
478 0x0000fffc, /* dst_mask */
479 TRUE), /* pcrel_offset */
480
481 /* A relative 16 bit branch. Bit 10 should be set to indicate that
482 the branch is not expected to be taken. The lower two bits must
483 be zero. */
484 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
485 0, /* rightshift */
486 2, /* size (0 = byte, 1 = short, 2 = long) */
487 16, /* bitsize */
488 TRUE, /* pc_relative */
489 0, /* bitpos */
490 complain_overflow_signed, /* complain_on_overflow */
491 ppc64_elf_brtaken_reloc, /* special_function */
492 "R_PPC64_REL14_BRNTAKEN",/* name */
493 FALSE, /* partial_inplace */
494 0, /* src_mask */
495 0x0000fffc, /* dst_mask */
496 TRUE), /* pcrel_offset */
497
498 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
499 symbol. */
500 HOWTO (R_PPC64_GOT16, /* type */
501 0, /* rightshift */
502 1, /* size (0 = byte, 1 = short, 2 = long) */
503 16, /* bitsize */
504 FALSE, /* pc_relative */
505 0, /* bitpos */
506 complain_overflow_signed, /* complain_on_overflow */
507 ppc64_elf_unhandled_reloc, /* special_function */
508 "R_PPC64_GOT16", /* name */
509 FALSE, /* partial_inplace */
510 0, /* src_mask */
511 0xffff, /* dst_mask */
512 FALSE), /* pcrel_offset */
513
514 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
515 the symbol. */
516 HOWTO (R_PPC64_GOT16_LO, /* type */
517 0, /* rightshift */
518 1, /* size (0 = byte, 1 = short, 2 = long) */
519 16, /* bitsize */
520 FALSE, /* pc_relative */
521 0, /* bitpos */
522 complain_overflow_dont, /* complain_on_overflow */
523 ppc64_elf_unhandled_reloc, /* special_function */
524 "R_PPC64_GOT16_LO", /* name */
525 FALSE, /* partial_inplace */
526 0, /* src_mask */
527 0xffff, /* dst_mask */
528 FALSE), /* pcrel_offset */
529
530 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
531 the symbol. */
532 HOWTO (R_PPC64_GOT16_HI, /* type */
533 16, /* rightshift */
534 1, /* size (0 = byte, 1 = short, 2 = long) */
535 16, /* bitsize */
536 FALSE, /* pc_relative */
537 0, /* bitpos */
538 complain_overflow_signed,/* complain_on_overflow */
539 ppc64_elf_unhandled_reloc, /* special_function */
540 "R_PPC64_GOT16_HI", /* name */
541 FALSE, /* partial_inplace */
542 0, /* src_mask */
543 0xffff, /* dst_mask */
544 FALSE), /* pcrel_offset */
545
546 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
547 the symbol. */
548 HOWTO (R_PPC64_GOT16_HA, /* type */
549 16, /* rightshift */
550 1, /* size (0 = byte, 1 = short, 2 = long) */
551 16, /* bitsize */
552 FALSE, /* pc_relative */
553 0, /* bitpos */
554 complain_overflow_signed,/* complain_on_overflow */
555 ppc64_elf_unhandled_reloc, /* special_function */
556 "R_PPC64_GOT16_HA", /* name */
557 FALSE, /* partial_inplace */
558 0, /* src_mask */
559 0xffff, /* dst_mask */
560 FALSE), /* pcrel_offset */
561
562 /* This is used only by the dynamic linker. The symbol should exist
563 both in the object being run and in some shared library. The
564 dynamic linker copies the data addressed by the symbol from the
565 shared library into the object, because the object being
566 run has to have the data at some particular address. */
567 HOWTO (R_PPC64_COPY, /* type */
568 0, /* rightshift */
569 0, /* this one is variable size */
570 0, /* bitsize */
571 FALSE, /* pc_relative */
572 0, /* bitpos */
573 complain_overflow_dont, /* complain_on_overflow */
574 ppc64_elf_unhandled_reloc, /* special_function */
575 "R_PPC64_COPY", /* name */
576 FALSE, /* partial_inplace */
577 0, /* src_mask */
578 0, /* dst_mask */
579 FALSE), /* pcrel_offset */
580
581 /* Like R_PPC64_ADDR64, but used when setting global offset table
582 entries. */
583 HOWTO (R_PPC64_GLOB_DAT, /* type */
584 0, /* rightshift */
585 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
586 64, /* bitsize */
587 FALSE, /* pc_relative */
588 0, /* bitpos */
589 complain_overflow_dont, /* complain_on_overflow */
590 ppc64_elf_unhandled_reloc, /* special_function */
591 "R_PPC64_GLOB_DAT", /* name */
592 FALSE, /* partial_inplace */
593 0, /* src_mask */
594 ONES (64), /* dst_mask */
595 FALSE), /* pcrel_offset */
596
597 /* Created by the link editor. Marks a procedure linkage table
598 entry for a symbol. */
599 HOWTO (R_PPC64_JMP_SLOT, /* type */
600 0, /* rightshift */
601 0, /* size (0 = byte, 1 = short, 2 = long) */
602 0, /* bitsize */
603 FALSE, /* pc_relative */
604 0, /* bitpos */
605 complain_overflow_dont, /* complain_on_overflow */
606 ppc64_elf_unhandled_reloc, /* special_function */
607 "R_PPC64_JMP_SLOT", /* name */
608 FALSE, /* partial_inplace */
609 0, /* src_mask */
610 0, /* dst_mask */
611 FALSE), /* pcrel_offset */
612
613 /* Used only by the dynamic linker. When the object is run, this
614 doubleword64 is set to the load address of the object, plus the
615 addend. */
616 HOWTO (R_PPC64_RELATIVE, /* type */
617 0, /* rightshift */
618 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
619 64, /* bitsize */
620 FALSE, /* pc_relative */
621 0, /* bitpos */
622 complain_overflow_dont, /* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_PPC64_RELATIVE", /* name */
625 FALSE, /* partial_inplace */
626 0, /* src_mask */
627 ONES (64), /* dst_mask */
628 FALSE), /* pcrel_offset */
629
630 /* Like R_PPC64_ADDR32, but may be unaligned. */
631 HOWTO (R_PPC64_UADDR32, /* type */
632 0, /* rightshift */
633 2, /* size (0 = byte, 1 = short, 2 = long) */
634 32, /* bitsize */
635 FALSE, /* pc_relative */
636 0, /* bitpos */
637 complain_overflow_bitfield, /* complain_on_overflow */
638 bfd_elf_generic_reloc, /* special_function */
639 "R_PPC64_UADDR32", /* name */
640 FALSE, /* partial_inplace */
641 0, /* src_mask */
642 0xffffffff, /* dst_mask */
643 FALSE), /* pcrel_offset */
644
645 /* Like R_PPC64_ADDR16, but may be unaligned. */
646 HOWTO (R_PPC64_UADDR16, /* type */
647 0, /* rightshift */
648 1, /* size (0 = byte, 1 = short, 2 = long) */
649 16, /* bitsize */
650 FALSE, /* pc_relative */
651 0, /* bitpos */
652 complain_overflow_bitfield, /* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_PPC64_UADDR16", /* name */
655 FALSE, /* partial_inplace */
656 0, /* src_mask */
657 0xffff, /* dst_mask */
658 FALSE), /* pcrel_offset */
659
660 /* 32-bit PC relative. */
661 HOWTO (R_PPC64_REL32, /* type */
662 0, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 32, /* bitsize */
665 TRUE, /* pc_relative */
666 0, /* bitpos */
667 complain_overflow_signed, /* complain_on_overflow */
668 bfd_elf_generic_reloc, /* special_function */
669 "R_PPC64_REL32", /* name */
670 FALSE, /* partial_inplace */
671 0, /* src_mask */
672 0xffffffff, /* dst_mask */
673 TRUE), /* pcrel_offset */
674
675 /* 32-bit relocation to the symbol's procedure linkage table. */
676 HOWTO (R_PPC64_PLT32, /* type */
677 0, /* rightshift */
678 2, /* size (0 = byte, 1 = short, 2 = long) */
679 32, /* bitsize */
680 FALSE, /* pc_relative */
681 0, /* bitpos */
682 complain_overflow_bitfield, /* complain_on_overflow */
683 ppc64_elf_unhandled_reloc, /* special_function */
684 "R_PPC64_PLT32", /* name */
685 FALSE, /* partial_inplace */
686 0, /* src_mask */
687 0xffffffff, /* dst_mask */
688 FALSE), /* pcrel_offset */
689
690 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
691 FIXME: R_PPC64_PLTREL32 not supported. */
692 HOWTO (R_PPC64_PLTREL32, /* type */
693 0, /* rightshift */
694 2, /* size (0 = byte, 1 = short, 2 = long) */
695 32, /* bitsize */
696 TRUE, /* pc_relative */
697 0, /* bitpos */
698 complain_overflow_signed, /* complain_on_overflow */
699 ppc64_elf_unhandled_reloc, /* special_function */
700 "R_PPC64_PLTREL32", /* name */
701 FALSE, /* partial_inplace */
702 0, /* src_mask */
703 0xffffffff, /* dst_mask */
704 TRUE), /* pcrel_offset */
705
706 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
707 the symbol. */
708 HOWTO (R_PPC64_PLT16_LO, /* type */
709 0, /* rightshift */
710 1, /* size (0 = byte, 1 = short, 2 = long) */
711 16, /* bitsize */
712 FALSE, /* pc_relative */
713 0, /* bitpos */
714 complain_overflow_dont, /* complain_on_overflow */
715 ppc64_elf_unhandled_reloc, /* special_function */
716 "R_PPC64_PLT16_LO", /* name */
717 FALSE, /* partial_inplace */
718 0, /* src_mask */
719 0xffff, /* dst_mask */
720 FALSE), /* pcrel_offset */
721
722 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
723 the symbol. */
724 HOWTO (R_PPC64_PLT16_HI, /* type */
725 16, /* rightshift */
726 1, /* size (0 = byte, 1 = short, 2 = long) */
727 16, /* bitsize */
728 FALSE, /* pc_relative */
729 0, /* bitpos */
730 complain_overflow_signed, /* complain_on_overflow */
731 ppc64_elf_unhandled_reloc, /* special_function */
732 "R_PPC64_PLT16_HI", /* name */
733 FALSE, /* partial_inplace */
734 0, /* src_mask */
735 0xffff, /* dst_mask */
736 FALSE), /* pcrel_offset */
737
738 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
739 the symbol. */
740 HOWTO (R_PPC64_PLT16_HA, /* type */
741 16, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
744 FALSE, /* pc_relative */
745 0, /* bitpos */
746 complain_overflow_signed, /* complain_on_overflow */
747 ppc64_elf_unhandled_reloc, /* special_function */
748 "R_PPC64_PLT16_HA", /* name */
749 FALSE, /* partial_inplace */
750 0, /* src_mask */
751 0xffff, /* dst_mask */
752 FALSE), /* pcrel_offset */
753
754 /* 16-bit section relative relocation. */
755 HOWTO (R_PPC64_SECTOFF, /* type */
756 0, /* rightshift */
757 1, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
759 FALSE, /* pc_relative */
760 0, /* bitpos */
761 complain_overflow_signed, /* complain_on_overflow */
762 ppc64_elf_sectoff_reloc, /* special_function */
763 "R_PPC64_SECTOFF", /* name */
764 FALSE, /* partial_inplace */
765 0, /* src_mask */
766 0xffff, /* dst_mask */
767 FALSE), /* pcrel_offset */
768
769 /* Like R_PPC64_SECTOFF, but no overflow warning. */
770 HOWTO (R_PPC64_SECTOFF_LO, /* type */
771 0, /* rightshift */
772 1, /* size (0 = byte, 1 = short, 2 = long) */
773 16, /* bitsize */
774 FALSE, /* pc_relative */
775 0, /* bitpos */
776 complain_overflow_dont, /* complain_on_overflow */
777 ppc64_elf_sectoff_reloc, /* special_function */
778 "R_PPC64_SECTOFF_LO", /* name */
779 FALSE, /* partial_inplace */
780 0, /* src_mask */
781 0xffff, /* dst_mask */
782 FALSE), /* pcrel_offset */
783
784 /* 16-bit upper half section relative relocation. */
785 HOWTO (R_PPC64_SECTOFF_HI, /* type */
786 16, /* rightshift */
787 1, /* size (0 = byte, 1 = short, 2 = long) */
788 16, /* bitsize */
789 FALSE, /* pc_relative */
790 0, /* bitpos */
791 complain_overflow_signed, /* complain_on_overflow */
792 ppc64_elf_sectoff_reloc, /* special_function */
793 "R_PPC64_SECTOFF_HI", /* name */
794 FALSE, /* partial_inplace */
795 0, /* src_mask */
796 0xffff, /* dst_mask */
797 FALSE), /* pcrel_offset */
798
799 /* 16-bit upper half adjusted section relative relocation. */
800 HOWTO (R_PPC64_SECTOFF_HA, /* type */
801 16, /* rightshift */
802 1, /* size (0 = byte, 1 = short, 2 = long) */
803 16, /* bitsize */
804 FALSE, /* pc_relative */
805 0, /* bitpos */
806 complain_overflow_signed, /* complain_on_overflow */
807 ppc64_elf_sectoff_ha_reloc, /* special_function */
808 "R_PPC64_SECTOFF_HA", /* name */
809 FALSE, /* partial_inplace */
810 0, /* src_mask */
811 0xffff, /* dst_mask */
812 FALSE), /* pcrel_offset */
813
814 /* Like R_PPC64_REL24 without touching the two least significant bits. */
815 HOWTO (R_PPC64_REL30, /* type */
816 2, /* rightshift */
817 2, /* size (0 = byte, 1 = short, 2 = long) */
818 30, /* bitsize */
819 TRUE, /* pc_relative */
820 0, /* bitpos */
821 complain_overflow_dont, /* complain_on_overflow */
822 bfd_elf_generic_reloc, /* special_function */
823 "R_PPC64_REL30", /* name */
824 FALSE, /* partial_inplace */
825 0, /* src_mask */
826 0xfffffffc, /* dst_mask */
827 TRUE), /* pcrel_offset */
828
829 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
830
831 /* A standard 64-bit relocation. */
832 HOWTO (R_PPC64_ADDR64, /* type */
833 0, /* rightshift */
834 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
835 64, /* bitsize */
836 FALSE, /* pc_relative */
837 0, /* bitpos */
838 complain_overflow_dont, /* complain_on_overflow */
839 bfd_elf_generic_reloc, /* special_function */
840 "R_PPC64_ADDR64", /* name */
841 FALSE, /* partial_inplace */
842 0, /* src_mask */
843 ONES (64), /* dst_mask */
844 FALSE), /* pcrel_offset */
845
846 /* The bits 32-47 of an address. */
847 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
848 32, /* rightshift */
849 1, /* size (0 = byte, 1 = short, 2 = long) */
850 16, /* bitsize */
851 FALSE, /* pc_relative */
852 0, /* bitpos */
853 complain_overflow_dont, /* complain_on_overflow */
854 bfd_elf_generic_reloc, /* special_function */
855 "R_PPC64_ADDR16_HIGHER", /* name */
856 FALSE, /* partial_inplace */
857 0, /* src_mask */
858 0xffff, /* dst_mask */
859 FALSE), /* pcrel_offset */
860
861 /* The bits 32-47 of an address, plus 1 if the contents of the low
862 16 bits, treated as a signed number, is negative. */
863 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
864 32, /* rightshift */
865 1, /* size (0 = byte, 1 = short, 2 = long) */
866 16, /* bitsize */
867 FALSE, /* pc_relative */
868 0, /* bitpos */
869 complain_overflow_dont, /* complain_on_overflow */
870 ppc64_elf_ha_reloc, /* special_function */
871 "R_PPC64_ADDR16_HIGHERA", /* name */
872 FALSE, /* partial_inplace */
873 0, /* src_mask */
874 0xffff, /* dst_mask */
875 FALSE), /* pcrel_offset */
876
877 /* The bits 48-63 of an address. */
878 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
879 48, /* rightshift */
880 1, /* size (0 = byte, 1 = short, 2 = long) */
881 16, /* bitsize */
882 FALSE, /* pc_relative */
883 0, /* bitpos */
884 complain_overflow_dont, /* complain_on_overflow */
885 bfd_elf_generic_reloc, /* special_function */
886 "R_PPC64_ADDR16_HIGHEST", /* name */
887 FALSE, /* partial_inplace */
888 0, /* src_mask */
889 0xffff, /* dst_mask */
890 FALSE), /* pcrel_offset */
891
892 /* The bits 48-63 of an address, plus 1 if the contents of the low
893 16 bits, treated as a signed number, is negative. */
894 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
895 48, /* rightshift */
896 1, /* size (0 = byte, 1 = short, 2 = long) */
897 16, /* bitsize */
898 FALSE, /* pc_relative */
899 0, /* bitpos */
900 complain_overflow_dont, /* complain_on_overflow */
901 ppc64_elf_ha_reloc, /* special_function */
902 "R_PPC64_ADDR16_HIGHESTA", /* name */
903 FALSE, /* partial_inplace */
904 0, /* src_mask */
905 0xffff, /* dst_mask */
906 FALSE), /* pcrel_offset */
907
908 /* Like ADDR64, but may be unaligned. */
909 HOWTO (R_PPC64_UADDR64, /* type */
910 0, /* rightshift */
911 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
912 64, /* bitsize */
913 FALSE, /* pc_relative */
914 0, /* bitpos */
915 complain_overflow_dont, /* complain_on_overflow */
916 bfd_elf_generic_reloc, /* special_function */
917 "R_PPC64_UADDR64", /* name */
918 FALSE, /* partial_inplace */
919 0, /* src_mask */
920 ONES (64), /* dst_mask */
921 FALSE), /* pcrel_offset */
922
923 /* 64-bit relative relocation. */
924 HOWTO (R_PPC64_REL64, /* type */
925 0, /* rightshift */
926 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
927 64, /* bitsize */
928 TRUE, /* pc_relative */
929 0, /* bitpos */
930 complain_overflow_dont, /* complain_on_overflow */
931 bfd_elf_generic_reloc, /* special_function */
932 "R_PPC64_REL64", /* name */
933 FALSE, /* partial_inplace */
934 0, /* src_mask */
935 ONES (64), /* dst_mask */
936 TRUE), /* pcrel_offset */
937
938 /* 64-bit relocation to the symbol's procedure linkage table. */
939 HOWTO (R_PPC64_PLT64, /* type */
940 0, /* rightshift */
941 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
942 64, /* bitsize */
943 FALSE, /* pc_relative */
944 0, /* bitpos */
945 complain_overflow_dont, /* complain_on_overflow */
946 ppc64_elf_unhandled_reloc, /* special_function */
947 "R_PPC64_PLT64", /* name */
948 FALSE, /* partial_inplace */
949 0, /* src_mask */
950 ONES (64), /* dst_mask */
951 FALSE), /* pcrel_offset */
952
953 /* 64-bit PC relative relocation to the symbol's procedure linkage
954 table. */
955 /* FIXME: R_PPC64_PLTREL64 not supported. */
956 HOWTO (R_PPC64_PLTREL64, /* type */
957 0, /* rightshift */
958 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
959 64, /* bitsize */
960 TRUE, /* pc_relative */
961 0, /* bitpos */
962 complain_overflow_dont, /* complain_on_overflow */
963 ppc64_elf_unhandled_reloc, /* special_function */
964 "R_PPC64_PLTREL64", /* name */
965 FALSE, /* partial_inplace */
966 0, /* src_mask */
967 ONES (64), /* dst_mask */
968 TRUE), /* pcrel_offset */
969
970 /* 16 bit TOC-relative relocation. */
971
972 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
973 HOWTO (R_PPC64_TOC16, /* type */
974 0, /* rightshift */
975 1, /* size (0 = byte, 1 = short, 2 = long) */
976 16, /* bitsize */
977 FALSE, /* pc_relative */
978 0, /* bitpos */
979 complain_overflow_signed, /* complain_on_overflow */
980 ppc64_elf_toc_reloc, /* special_function */
981 "R_PPC64_TOC16", /* name */
982 FALSE, /* partial_inplace */
983 0, /* src_mask */
984 0xffff, /* dst_mask */
985 FALSE), /* pcrel_offset */
986
987 /* 16 bit TOC-relative relocation without overflow. */
988
989 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
990 HOWTO (R_PPC64_TOC16_LO, /* type */
991 0, /* rightshift */
992 1, /* size (0 = byte, 1 = short, 2 = long) */
993 16, /* bitsize */
994 FALSE, /* pc_relative */
995 0, /* bitpos */
996 complain_overflow_dont, /* complain_on_overflow */
997 ppc64_elf_toc_reloc, /* special_function */
998 "R_PPC64_TOC16_LO", /* name */
999 FALSE, /* partial_inplace */
1000 0, /* src_mask */
1001 0xffff, /* dst_mask */
1002 FALSE), /* pcrel_offset */
1003
1004 /* 16 bit TOC-relative relocation, high 16 bits. */
1005
1006 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1007 HOWTO (R_PPC64_TOC16_HI, /* type */
1008 16, /* rightshift */
1009 1, /* size (0 = byte, 1 = short, 2 = long) */
1010 16, /* bitsize */
1011 FALSE, /* pc_relative */
1012 0, /* bitpos */
1013 complain_overflow_signed, /* complain_on_overflow */
1014 ppc64_elf_toc_reloc, /* special_function */
1015 "R_PPC64_TOC16_HI", /* name */
1016 FALSE, /* partial_inplace */
1017 0, /* src_mask */
1018 0xffff, /* dst_mask */
1019 FALSE), /* pcrel_offset */
1020
1021 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1022 contents of the low 16 bits, treated as a signed number, is
1023 negative. */
1024
1025 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1026 HOWTO (R_PPC64_TOC16_HA, /* type */
1027 16, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_signed, /* complain_on_overflow */
1033 ppc64_elf_toc_ha_reloc, /* special_function */
1034 "R_PPC64_TOC16_HA", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1039
1040 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1041
1042 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1043 HOWTO (R_PPC64_TOC, /* type */
1044 0, /* rightshift */
1045 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1046 64, /* bitsize */
1047 FALSE, /* pc_relative */
1048 0, /* bitpos */
1049 complain_overflow_dont, /* complain_on_overflow */
1050 ppc64_elf_toc64_reloc, /* special_function */
1051 "R_PPC64_TOC", /* name */
1052 FALSE, /* partial_inplace */
1053 0, /* src_mask */
1054 ONES (64), /* dst_mask */
1055 FALSE), /* pcrel_offset */
1056
1057 /* Like R_PPC64_GOT16, but also informs the link editor that the
1058 value to relocate may (!) refer to a PLT entry which the link
1059 editor (a) may replace with the symbol value. If the link editor
1060 is unable to fully resolve the symbol, it may (b) create a PLT
1061 entry and store the address to the new PLT entry in the GOT.
1062 This permits lazy resolution of function symbols at run time.
1063 The link editor may also skip all of this and just (c) emit a
1064 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1065 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1066 HOWTO (R_PPC64_PLTGOT16, /* type */
1067 0, /* rightshift */
1068 1, /* size (0 = byte, 1 = short, 2 = long) */
1069 16, /* bitsize */
1070 FALSE, /* pc_relative */
1071 0, /* bitpos */
1072 complain_overflow_signed, /* complain_on_overflow */
1073 ppc64_elf_unhandled_reloc, /* special_function */
1074 "R_PPC64_PLTGOT16", /* name */
1075 FALSE, /* partial_inplace */
1076 0, /* src_mask */
1077 0xffff, /* dst_mask */
1078 FALSE), /* pcrel_offset */
1079
1080 /* Like R_PPC64_PLTGOT16, but without overflow. */
1081 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1082 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1083 0, /* rightshift */
1084 1, /* size (0 = byte, 1 = short, 2 = long) */
1085 16, /* bitsize */
1086 FALSE, /* pc_relative */
1087 0, /* bitpos */
1088 complain_overflow_dont, /* complain_on_overflow */
1089 ppc64_elf_unhandled_reloc, /* special_function */
1090 "R_PPC64_PLTGOT16_LO", /* name */
1091 FALSE, /* partial_inplace */
1092 0, /* src_mask */
1093 0xffff, /* dst_mask */
1094 FALSE), /* pcrel_offset */
1095
1096 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1097 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1098 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1099 16, /* rightshift */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1101 16, /* bitsize */
1102 FALSE, /* pc_relative */
1103 0, /* bitpos */
1104 complain_overflow_signed, /* complain_on_overflow */
1105 ppc64_elf_unhandled_reloc, /* special_function */
1106 "R_PPC64_PLTGOT16_HI", /* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xffff, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1111
1112 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1113 1 if the contents of the low 16 bits, treated as a signed number,
1114 is negative. */
1115 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1116 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1117 16, /* rightshift */
1118 1, /* size (0 = byte, 1 = short, 2 = long) */
1119 16, /* bitsize */
1120 FALSE, /* pc_relative */
1121 0, /* bitpos */
1122 complain_overflow_signed, /* complain_on_overflow */
1123 ppc64_elf_unhandled_reloc, /* special_function */
1124 "R_PPC64_PLTGOT16_HA", /* name */
1125 FALSE, /* partial_inplace */
1126 0, /* src_mask */
1127 0xffff, /* dst_mask */
1128 FALSE), /* pcrel_offset */
1129
1130 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1131 HOWTO (R_PPC64_ADDR16_DS, /* type */
1132 0, /* rightshift */
1133 1, /* size (0 = byte, 1 = short, 2 = long) */
1134 16, /* bitsize */
1135 FALSE, /* pc_relative */
1136 0, /* bitpos */
1137 complain_overflow_signed, /* complain_on_overflow */
1138 bfd_elf_generic_reloc, /* special_function */
1139 "R_PPC64_ADDR16_DS", /* name */
1140 FALSE, /* partial_inplace */
1141 0, /* src_mask */
1142 0xfffc, /* dst_mask */
1143 FALSE), /* pcrel_offset */
1144
1145 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1146 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1147 0, /* rightshift */
1148 1, /* size (0 = byte, 1 = short, 2 = long) */
1149 16, /* bitsize */
1150 FALSE, /* pc_relative */
1151 0, /* bitpos */
1152 complain_overflow_dont,/* complain_on_overflow */
1153 bfd_elf_generic_reloc, /* special_function */
1154 "R_PPC64_ADDR16_LO_DS",/* name */
1155 FALSE, /* partial_inplace */
1156 0, /* src_mask */
1157 0xfffc, /* dst_mask */
1158 FALSE), /* pcrel_offset */
1159
1160 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1161 HOWTO (R_PPC64_GOT16_DS, /* type */
1162 0, /* rightshift */
1163 1, /* size (0 = byte, 1 = short, 2 = long) */
1164 16, /* bitsize */
1165 FALSE, /* pc_relative */
1166 0, /* bitpos */
1167 complain_overflow_signed, /* complain_on_overflow */
1168 ppc64_elf_unhandled_reloc, /* special_function */
1169 "R_PPC64_GOT16_DS", /* name */
1170 FALSE, /* partial_inplace */
1171 0, /* src_mask */
1172 0xfffc, /* dst_mask */
1173 FALSE), /* pcrel_offset */
1174
1175 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1176 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1177 0, /* rightshift */
1178 1, /* size (0 = byte, 1 = short, 2 = long) */
1179 16, /* bitsize */
1180 FALSE, /* pc_relative */
1181 0, /* bitpos */
1182 complain_overflow_dont, /* complain_on_overflow */
1183 ppc64_elf_unhandled_reloc, /* special_function */
1184 "R_PPC64_GOT16_LO_DS", /* name */
1185 FALSE, /* partial_inplace */
1186 0, /* src_mask */
1187 0xfffc, /* dst_mask */
1188 FALSE), /* pcrel_offset */
1189
1190 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1191 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_dont, /* complain_on_overflow */
1198 ppc64_elf_unhandled_reloc, /* special_function */
1199 "R_PPC64_PLT16_LO_DS", /* name */
1200 FALSE, /* partial_inplace */
1201 0, /* src_mask */
1202 0xfffc, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1204
1205 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1206 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1207 0, /* rightshift */
1208 1, /* size (0 = byte, 1 = short, 2 = long) */
1209 16, /* bitsize */
1210 FALSE, /* pc_relative */
1211 0, /* bitpos */
1212 complain_overflow_signed, /* complain_on_overflow */
1213 ppc64_elf_sectoff_reloc, /* special_function */
1214 "R_PPC64_SECTOFF_DS", /* name */
1215 FALSE, /* partial_inplace */
1216 0, /* src_mask */
1217 0xfffc, /* dst_mask */
1218 FALSE), /* pcrel_offset */
1219
1220 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1221 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1222 0, /* rightshift */
1223 1, /* size (0 = byte, 1 = short, 2 = long) */
1224 16, /* bitsize */
1225 FALSE, /* pc_relative */
1226 0, /* bitpos */
1227 complain_overflow_dont, /* complain_on_overflow */
1228 ppc64_elf_sectoff_reloc, /* special_function */
1229 "R_PPC64_SECTOFF_LO_DS",/* name */
1230 FALSE, /* partial_inplace */
1231 0, /* src_mask */
1232 0xfffc, /* dst_mask */
1233 FALSE), /* pcrel_offset */
1234
1235 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1236 HOWTO (R_PPC64_TOC16_DS, /* type */
1237 0, /* rightshift */
1238 1, /* size (0 = byte, 1 = short, 2 = long) */
1239 16, /* bitsize */
1240 FALSE, /* pc_relative */
1241 0, /* bitpos */
1242 complain_overflow_signed, /* complain_on_overflow */
1243 ppc64_elf_toc_reloc, /* special_function */
1244 "R_PPC64_TOC16_DS", /* name */
1245 FALSE, /* partial_inplace */
1246 0, /* src_mask */
1247 0xfffc, /* dst_mask */
1248 FALSE), /* pcrel_offset */
1249
1250 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1251 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1252 0, /* rightshift */
1253 1, /* size (0 = byte, 1 = short, 2 = long) */
1254 16, /* bitsize */
1255 FALSE, /* pc_relative */
1256 0, /* bitpos */
1257 complain_overflow_dont, /* complain_on_overflow */
1258 ppc64_elf_toc_reloc, /* special_function */
1259 "R_PPC64_TOC16_LO_DS", /* name */
1260 FALSE, /* partial_inplace */
1261 0, /* src_mask */
1262 0xfffc, /* dst_mask */
1263 FALSE), /* pcrel_offset */
1264
1265 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1266 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1267 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1268 0, /* rightshift */
1269 1, /* size (0 = byte, 1 = short, 2 = long) */
1270 16, /* bitsize */
1271 FALSE, /* pc_relative */
1272 0, /* bitpos */
1273 complain_overflow_signed, /* complain_on_overflow */
1274 ppc64_elf_unhandled_reloc, /* special_function */
1275 "R_PPC64_PLTGOT16_DS", /* name */
1276 FALSE, /* partial_inplace */
1277 0, /* src_mask */
1278 0xfffc, /* dst_mask */
1279 FALSE), /* pcrel_offset */
1280
1281 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1282 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1283 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1284 0, /* rightshift */
1285 1, /* size (0 = byte, 1 = short, 2 = long) */
1286 16, /* bitsize */
1287 FALSE, /* pc_relative */
1288 0, /* bitpos */
1289 complain_overflow_dont, /* complain_on_overflow */
1290 ppc64_elf_unhandled_reloc, /* special_function */
1291 "R_PPC64_PLTGOT16_LO_DS",/* name */
1292 FALSE, /* partial_inplace */
1293 0, /* src_mask */
1294 0xfffc, /* dst_mask */
1295 FALSE), /* pcrel_offset */
1296
1297 /* Marker relocs for TLS. */
1298 HOWTO (R_PPC64_TLS,
1299 0, /* rightshift */
1300 2, /* size (0 = byte, 1 = short, 2 = long) */
1301 32, /* bitsize */
1302 FALSE, /* pc_relative */
1303 0, /* bitpos */
1304 complain_overflow_dont, /* complain_on_overflow */
1305 bfd_elf_generic_reloc, /* special_function */
1306 "R_PPC64_TLS", /* name */
1307 FALSE, /* partial_inplace */
1308 0, /* src_mask */
1309 0, /* dst_mask */
1310 FALSE), /* pcrel_offset */
1311
1312 HOWTO (R_PPC64_TLSGD,
1313 0, /* rightshift */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1315 32, /* bitsize */
1316 FALSE, /* pc_relative */
1317 0, /* bitpos */
1318 complain_overflow_dont, /* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_PPC64_TLSGD", /* name */
1321 FALSE, /* partial_inplace */
1322 0, /* src_mask */
1323 0, /* dst_mask */
1324 FALSE), /* pcrel_offset */
1325
1326 HOWTO (R_PPC64_TLSLD,
1327 0, /* rightshift */
1328 2, /* size (0 = byte, 1 = short, 2 = long) */
1329 32, /* bitsize */
1330 FALSE, /* pc_relative */
1331 0, /* bitpos */
1332 complain_overflow_dont, /* complain_on_overflow */
1333 bfd_elf_generic_reloc, /* special_function */
1334 "R_PPC64_TLSLD", /* name */
1335 FALSE, /* partial_inplace */
1336 0, /* src_mask */
1337 0, /* dst_mask */
1338 FALSE), /* pcrel_offset */
1339
1340 /* Marker reloc for optimizing r2 save in prologue rather than on
1341 each plt call stub. */
1342 HOWTO (R_PPC64_TOCSAVE,
1343 0, /* rightshift */
1344 2, /* size (0 = byte, 1 = short, 2 = long) */
1345 32, /* bitsize */
1346 FALSE, /* pc_relative */
1347 0, /* bitpos */
1348 complain_overflow_dont, /* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_PPC64_TOCSAVE", /* name */
1351 FALSE, /* partial_inplace */
1352 0, /* src_mask */
1353 0, /* dst_mask */
1354 FALSE), /* pcrel_offset */
1355
1356 /* Marker relocs on inline plt call instructions. */
1357 HOWTO (R_PPC64_PLTSEQ,
1358 0, /* rightshift */
1359 2, /* size (0 = byte, 1 = short, 2 = long) */
1360 32, /* bitsize */
1361 FALSE, /* pc_relative */
1362 0, /* bitpos */
1363 complain_overflow_dont, /* complain_on_overflow */
1364 bfd_elf_generic_reloc, /* special_function */
1365 "R_PPC64_PLTSEQ", /* name */
1366 FALSE, /* partial_inplace */
1367 0, /* src_mask */
1368 0, /* dst_mask */
1369 FALSE), /* pcrel_offset */
1370
1371 HOWTO (R_PPC64_PLTCALL,
1372 0, /* rightshift */
1373 2, /* size (0 = byte, 1 = short, 2 = long) */
1374 32, /* bitsize */
1375 FALSE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_dont, /* complain_on_overflow */
1378 bfd_elf_generic_reloc, /* special_function */
1379 "R_PPC64_PLTCALL", /* name */
1380 FALSE, /* partial_inplace */
1381 0, /* src_mask */
1382 0, /* dst_mask */
1383 FALSE), /* pcrel_offset */
1384
1385 /* Computes the load module index of the load module that contains the
1386 definition of its TLS sym. */
1387 HOWTO (R_PPC64_DTPMOD64,
1388 0, /* rightshift */
1389 4, /* size (0 = byte, 1 = short, 2 = long) */
1390 64, /* bitsize */
1391 FALSE, /* pc_relative */
1392 0, /* bitpos */
1393 complain_overflow_dont, /* complain_on_overflow */
1394 ppc64_elf_unhandled_reloc, /* special_function */
1395 "R_PPC64_DTPMOD64", /* name */
1396 FALSE, /* partial_inplace */
1397 0, /* src_mask */
1398 ONES (64), /* dst_mask */
1399 FALSE), /* pcrel_offset */
1400
1401 /* Computes a dtv-relative displacement, the difference between the value
1402 of sym+add and the base address of the thread-local storage block that
1403 contains the definition of sym, minus 0x8000. */
1404 HOWTO (R_PPC64_DTPREL64,
1405 0, /* rightshift */
1406 4, /* size (0 = byte, 1 = short, 2 = long) */
1407 64, /* bitsize */
1408 FALSE, /* pc_relative */
1409 0, /* bitpos */
1410 complain_overflow_dont, /* complain_on_overflow */
1411 ppc64_elf_unhandled_reloc, /* special_function */
1412 "R_PPC64_DTPREL64", /* name */
1413 FALSE, /* partial_inplace */
1414 0, /* src_mask */
1415 ONES (64), /* dst_mask */
1416 FALSE), /* pcrel_offset */
1417
1418 /* A 16 bit dtprel reloc. */
1419 HOWTO (R_PPC64_DTPREL16,
1420 0, /* rightshift */
1421 1, /* size (0 = byte, 1 = short, 2 = long) */
1422 16, /* bitsize */
1423 FALSE, /* pc_relative */
1424 0, /* bitpos */
1425 complain_overflow_signed, /* complain_on_overflow */
1426 ppc64_elf_unhandled_reloc, /* special_function */
1427 "R_PPC64_DTPREL16", /* name */
1428 FALSE, /* partial_inplace */
1429 0, /* src_mask */
1430 0xffff, /* dst_mask */
1431 FALSE), /* pcrel_offset */
1432
1433 /* Like DTPREL16, but no overflow. */
1434 HOWTO (R_PPC64_DTPREL16_LO,
1435 0, /* rightshift */
1436 1, /* size (0 = byte, 1 = short, 2 = long) */
1437 16, /* bitsize */
1438 FALSE, /* pc_relative */
1439 0, /* bitpos */
1440 complain_overflow_dont, /* complain_on_overflow */
1441 ppc64_elf_unhandled_reloc, /* special_function */
1442 "R_PPC64_DTPREL16_LO", /* name */
1443 FALSE, /* partial_inplace */
1444 0, /* src_mask */
1445 0xffff, /* dst_mask */
1446 FALSE), /* pcrel_offset */
1447
1448 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1449 HOWTO (R_PPC64_DTPREL16_HI,
1450 16, /* rightshift */
1451 1, /* size (0 = byte, 1 = short, 2 = long) */
1452 16, /* bitsize */
1453 FALSE, /* pc_relative */
1454 0, /* bitpos */
1455 complain_overflow_signed, /* complain_on_overflow */
1456 ppc64_elf_unhandled_reloc, /* special_function */
1457 "R_PPC64_DTPREL16_HI", /* name */
1458 FALSE, /* partial_inplace */
1459 0, /* src_mask */
1460 0xffff, /* dst_mask */
1461 FALSE), /* pcrel_offset */
1462
1463 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1464 HOWTO (R_PPC64_DTPREL16_HA,
1465 16, /* rightshift */
1466 1, /* size (0 = byte, 1 = short, 2 = long) */
1467 16, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_signed, /* complain_on_overflow */
1471 ppc64_elf_unhandled_reloc, /* special_function */
1472 "R_PPC64_DTPREL16_HA", /* name */
1473 FALSE, /* partial_inplace */
1474 0, /* src_mask */
1475 0xffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477
1478 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1479 HOWTO (R_PPC64_DTPREL16_HIGHER,
1480 32, /* rightshift */
1481 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 16, /* bitsize */
1483 FALSE, /* pc_relative */
1484 0, /* bitpos */
1485 complain_overflow_dont, /* complain_on_overflow */
1486 ppc64_elf_unhandled_reloc, /* special_function */
1487 "R_PPC64_DTPREL16_HIGHER", /* name */
1488 FALSE, /* partial_inplace */
1489 0, /* src_mask */
1490 0xffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
1492
1493 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1494 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1495 32, /* rightshift */
1496 1, /* size (0 = byte, 1 = short, 2 = long) */
1497 16, /* bitsize */
1498 FALSE, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_dont, /* complain_on_overflow */
1501 ppc64_elf_unhandled_reloc, /* special_function */
1502 "R_PPC64_DTPREL16_HIGHERA", /* name */
1503 FALSE, /* partial_inplace */
1504 0, /* src_mask */
1505 0xffff, /* dst_mask */
1506 FALSE), /* pcrel_offset */
1507
1508 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1509 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1510 48, /* rightshift */
1511 1, /* size (0 = byte, 1 = short, 2 = long) */
1512 16, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_dont, /* complain_on_overflow */
1516 ppc64_elf_unhandled_reloc, /* special_function */
1517 "R_PPC64_DTPREL16_HIGHEST", /* name */
1518 FALSE, /* partial_inplace */
1519 0, /* src_mask */
1520 0xffff, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1522
1523 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1524 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1525 48, /* rightshift */
1526 1, /* size (0 = byte, 1 = short, 2 = long) */
1527 16, /* bitsize */
1528 FALSE, /* pc_relative */
1529 0, /* bitpos */
1530 complain_overflow_dont, /* complain_on_overflow */
1531 ppc64_elf_unhandled_reloc, /* special_function */
1532 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1533 FALSE, /* partial_inplace */
1534 0, /* src_mask */
1535 0xffff, /* dst_mask */
1536 FALSE), /* pcrel_offset */
1537
1538 /* Like DTPREL16, but for insns with a DS field. */
1539 HOWTO (R_PPC64_DTPREL16_DS,
1540 0, /* rightshift */
1541 1, /* size (0 = byte, 1 = short, 2 = long) */
1542 16, /* bitsize */
1543 FALSE, /* pc_relative */
1544 0, /* bitpos */
1545 complain_overflow_signed, /* complain_on_overflow */
1546 ppc64_elf_unhandled_reloc, /* special_function */
1547 "R_PPC64_DTPREL16_DS", /* name */
1548 FALSE, /* partial_inplace */
1549 0, /* src_mask */
1550 0xfffc, /* dst_mask */
1551 FALSE), /* pcrel_offset */
1552
1553 /* Like DTPREL16_DS, but no overflow. */
1554 HOWTO (R_PPC64_DTPREL16_LO_DS,
1555 0, /* rightshift */
1556 1, /* size (0 = byte, 1 = short, 2 = long) */
1557 16, /* bitsize */
1558 FALSE, /* pc_relative */
1559 0, /* bitpos */
1560 complain_overflow_dont, /* complain_on_overflow */
1561 ppc64_elf_unhandled_reloc, /* special_function */
1562 "R_PPC64_DTPREL16_LO_DS", /* name */
1563 FALSE, /* partial_inplace */
1564 0, /* src_mask */
1565 0xfffc, /* dst_mask */
1566 FALSE), /* pcrel_offset */
1567
1568 /* Computes a tp-relative displacement, the difference between the value of
1569 sym+add and the value of the thread pointer (r13). */
1570 HOWTO (R_PPC64_TPREL64,
1571 0, /* rightshift */
1572 4, /* size (0 = byte, 1 = short, 2 = long) */
1573 64, /* bitsize */
1574 FALSE, /* pc_relative */
1575 0, /* bitpos */
1576 complain_overflow_dont, /* complain_on_overflow */
1577 ppc64_elf_unhandled_reloc, /* special_function */
1578 "R_PPC64_TPREL64", /* name */
1579 FALSE, /* partial_inplace */
1580 0, /* src_mask */
1581 ONES (64), /* dst_mask */
1582 FALSE), /* pcrel_offset */
1583
1584 /* A 16 bit tprel reloc. */
1585 HOWTO (R_PPC64_TPREL16,
1586 0, /* rightshift */
1587 1, /* size (0 = byte, 1 = short, 2 = long) */
1588 16, /* bitsize */
1589 FALSE, /* pc_relative */
1590 0, /* bitpos */
1591 complain_overflow_signed, /* complain_on_overflow */
1592 ppc64_elf_unhandled_reloc, /* special_function */
1593 "R_PPC64_TPREL16", /* name */
1594 FALSE, /* partial_inplace */
1595 0, /* src_mask */
1596 0xffff, /* dst_mask */
1597 FALSE), /* pcrel_offset */
1598
1599 /* Like TPREL16, but no overflow. */
1600 HOWTO (R_PPC64_TPREL16_LO,
1601 0, /* rightshift */
1602 1, /* size (0 = byte, 1 = short, 2 = long) */
1603 16, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_dont, /* complain_on_overflow */
1607 ppc64_elf_unhandled_reloc, /* special_function */
1608 "R_PPC64_TPREL16_LO", /* name */
1609 FALSE, /* partial_inplace */
1610 0, /* src_mask */
1611 0xffff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613
1614 /* Like TPREL16_LO, but next higher group of 16 bits. */
1615 HOWTO (R_PPC64_TPREL16_HI,
1616 16, /* rightshift */
1617 1, /* size (0 = byte, 1 = short, 2 = long) */
1618 16, /* bitsize */
1619 FALSE, /* pc_relative */
1620 0, /* bitpos */
1621 complain_overflow_signed, /* complain_on_overflow */
1622 ppc64_elf_unhandled_reloc, /* special_function */
1623 "R_PPC64_TPREL16_HI", /* name */
1624 FALSE, /* partial_inplace */
1625 0, /* src_mask */
1626 0xffff, /* dst_mask */
1627 FALSE), /* pcrel_offset */
1628
1629 /* Like TPREL16_HI, but adjust for low 16 bits. */
1630 HOWTO (R_PPC64_TPREL16_HA,
1631 16, /* rightshift */
1632 1, /* size (0 = byte, 1 = short, 2 = long) */
1633 16, /* bitsize */
1634 FALSE, /* pc_relative */
1635 0, /* bitpos */
1636 complain_overflow_signed, /* complain_on_overflow */
1637 ppc64_elf_unhandled_reloc, /* special_function */
1638 "R_PPC64_TPREL16_HA", /* name */
1639 FALSE, /* partial_inplace */
1640 0, /* src_mask */
1641 0xffff, /* dst_mask */
1642 FALSE), /* pcrel_offset */
1643
1644 /* Like TPREL16_HI, but next higher group of 16 bits. */
1645 HOWTO (R_PPC64_TPREL16_HIGHER,
1646 32, /* rightshift */
1647 1, /* size (0 = byte, 1 = short, 2 = long) */
1648 16, /* bitsize */
1649 FALSE, /* pc_relative */
1650 0, /* bitpos */
1651 complain_overflow_dont, /* complain_on_overflow */
1652 ppc64_elf_unhandled_reloc, /* special_function */
1653 "R_PPC64_TPREL16_HIGHER", /* name */
1654 FALSE, /* partial_inplace */
1655 0, /* src_mask */
1656 0xffff, /* dst_mask */
1657 FALSE), /* pcrel_offset */
1658
1659 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1660 HOWTO (R_PPC64_TPREL16_HIGHERA,
1661 32, /* rightshift */
1662 1, /* size (0 = byte, 1 = short, 2 = long) */
1663 16, /* bitsize */
1664 FALSE, /* pc_relative */
1665 0, /* bitpos */
1666 complain_overflow_dont, /* complain_on_overflow */
1667 ppc64_elf_unhandled_reloc, /* special_function */
1668 "R_PPC64_TPREL16_HIGHERA", /* name */
1669 FALSE, /* partial_inplace */
1670 0, /* src_mask */
1671 0xffff, /* dst_mask */
1672 FALSE), /* pcrel_offset */
1673
1674 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1675 HOWTO (R_PPC64_TPREL16_HIGHEST,
1676 48, /* rightshift */
1677 1, /* size (0 = byte, 1 = short, 2 = long) */
1678 16, /* bitsize */
1679 FALSE, /* pc_relative */
1680 0, /* bitpos */
1681 complain_overflow_dont, /* complain_on_overflow */
1682 ppc64_elf_unhandled_reloc, /* special_function */
1683 "R_PPC64_TPREL16_HIGHEST", /* name */
1684 FALSE, /* partial_inplace */
1685 0, /* src_mask */
1686 0xffff, /* dst_mask */
1687 FALSE), /* pcrel_offset */
1688
1689 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1690 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1691 48, /* rightshift */
1692 1, /* size (0 = byte, 1 = short, 2 = long) */
1693 16, /* bitsize */
1694 FALSE, /* pc_relative */
1695 0, /* bitpos */
1696 complain_overflow_dont, /* complain_on_overflow */
1697 ppc64_elf_unhandled_reloc, /* special_function */
1698 "R_PPC64_TPREL16_HIGHESTA", /* name */
1699 FALSE, /* partial_inplace */
1700 0, /* src_mask */
1701 0xffff, /* dst_mask */
1702 FALSE), /* pcrel_offset */
1703
1704 /* Like TPREL16, but for insns with a DS field. */
1705 HOWTO (R_PPC64_TPREL16_DS,
1706 0, /* rightshift */
1707 1, /* size (0 = byte, 1 = short, 2 = long) */
1708 16, /* bitsize */
1709 FALSE, /* pc_relative */
1710 0, /* bitpos */
1711 complain_overflow_signed, /* complain_on_overflow */
1712 ppc64_elf_unhandled_reloc, /* special_function */
1713 "R_PPC64_TPREL16_DS", /* name */
1714 FALSE, /* partial_inplace */
1715 0, /* src_mask */
1716 0xfffc, /* dst_mask */
1717 FALSE), /* pcrel_offset */
1718
1719 /* Like TPREL16_DS, but no overflow. */
1720 HOWTO (R_PPC64_TPREL16_LO_DS,
1721 0, /* rightshift */
1722 1, /* size (0 = byte, 1 = short, 2 = long) */
1723 16, /* bitsize */
1724 FALSE, /* pc_relative */
1725 0, /* bitpos */
1726 complain_overflow_dont, /* complain_on_overflow */
1727 ppc64_elf_unhandled_reloc, /* special_function */
1728 "R_PPC64_TPREL16_LO_DS", /* name */
1729 FALSE, /* partial_inplace */
1730 0, /* src_mask */
1731 0xfffc, /* dst_mask */
1732 FALSE), /* pcrel_offset */
1733
1734 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1735 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1736 to the first entry relative to the TOC base (r2). */
1737 HOWTO (R_PPC64_GOT_TLSGD16,
1738 0, /* rightshift */
1739 1, /* size (0 = byte, 1 = short, 2 = long) */
1740 16, /* bitsize */
1741 FALSE, /* pc_relative */
1742 0, /* bitpos */
1743 complain_overflow_signed, /* complain_on_overflow */
1744 ppc64_elf_unhandled_reloc, /* special_function */
1745 "R_PPC64_GOT_TLSGD16", /* name */
1746 FALSE, /* partial_inplace */
1747 0, /* src_mask */
1748 0xffff, /* dst_mask */
1749 FALSE), /* pcrel_offset */
1750
1751 /* Like GOT_TLSGD16, but no overflow. */
1752 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1753 0, /* rightshift */
1754 1, /* size (0 = byte, 1 = short, 2 = long) */
1755 16, /* bitsize */
1756 FALSE, /* pc_relative */
1757 0, /* bitpos */
1758 complain_overflow_dont, /* complain_on_overflow */
1759 ppc64_elf_unhandled_reloc, /* special_function */
1760 "R_PPC64_GOT_TLSGD16_LO", /* name */
1761 FALSE, /* partial_inplace */
1762 0, /* src_mask */
1763 0xffff, /* dst_mask */
1764 FALSE), /* pcrel_offset */
1765
1766 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1767 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1768 16, /* rightshift */
1769 1, /* size (0 = byte, 1 = short, 2 = long) */
1770 16, /* bitsize */
1771 FALSE, /* pc_relative */
1772 0, /* bitpos */
1773 complain_overflow_signed, /* complain_on_overflow */
1774 ppc64_elf_unhandled_reloc, /* special_function */
1775 "R_PPC64_GOT_TLSGD16_HI", /* name */
1776 FALSE, /* partial_inplace */
1777 0, /* src_mask */
1778 0xffff, /* dst_mask */
1779 FALSE), /* pcrel_offset */
1780
1781 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1782 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1783 16, /* rightshift */
1784 1, /* size (0 = byte, 1 = short, 2 = long) */
1785 16, /* bitsize */
1786 FALSE, /* pc_relative */
1787 0, /* bitpos */
1788 complain_overflow_signed, /* complain_on_overflow */
1789 ppc64_elf_unhandled_reloc, /* special_function */
1790 "R_PPC64_GOT_TLSGD16_HA", /* name */
1791 FALSE, /* partial_inplace */
1792 0, /* src_mask */
1793 0xffff, /* dst_mask */
1794 FALSE), /* pcrel_offset */
1795
1796 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1797 with values (sym+add)@dtpmod and zero, and computes the offset to the
1798 first entry relative to the TOC base (r2). */
1799 HOWTO (R_PPC64_GOT_TLSLD16,
1800 0, /* rightshift */
1801 1, /* size (0 = byte, 1 = short, 2 = long) */
1802 16, /* bitsize */
1803 FALSE, /* pc_relative */
1804 0, /* bitpos */
1805 complain_overflow_signed, /* complain_on_overflow */
1806 ppc64_elf_unhandled_reloc, /* special_function */
1807 "R_PPC64_GOT_TLSLD16", /* name */
1808 FALSE, /* partial_inplace */
1809 0, /* src_mask */
1810 0xffff, /* dst_mask */
1811 FALSE), /* pcrel_offset */
1812
1813 /* Like GOT_TLSLD16, but no overflow. */
1814 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1815 0, /* rightshift */
1816 1, /* size (0 = byte, 1 = short, 2 = long) */
1817 16, /* bitsize */
1818 FALSE, /* pc_relative */
1819 0, /* bitpos */
1820 complain_overflow_dont, /* complain_on_overflow */
1821 ppc64_elf_unhandled_reloc, /* special_function */
1822 "R_PPC64_GOT_TLSLD16_LO", /* name */
1823 FALSE, /* partial_inplace */
1824 0, /* src_mask */
1825 0xffff, /* dst_mask */
1826 FALSE), /* pcrel_offset */
1827
1828 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1829 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1830 16, /* rightshift */
1831 1, /* size (0 = byte, 1 = short, 2 = long) */
1832 16, /* bitsize */
1833 FALSE, /* pc_relative */
1834 0, /* bitpos */
1835 complain_overflow_signed, /* complain_on_overflow */
1836 ppc64_elf_unhandled_reloc, /* special_function */
1837 "R_PPC64_GOT_TLSLD16_HI", /* name */
1838 FALSE, /* partial_inplace */
1839 0, /* src_mask */
1840 0xffff, /* dst_mask */
1841 FALSE), /* pcrel_offset */
1842
1843 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1844 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1845 16, /* rightshift */
1846 1, /* size (0 = byte, 1 = short, 2 = long) */
1847 16, /* bitsize */
1848 FALSE, /* pc_relative */
1849 0, /* bitpos */
1850 complain_overflow_signed, /* complain_on_overflow */
1851 ppc64_elf_unhandled_reloc, /* special_function */
1852 "R_PPC64_GOT_TLSLD16_HA", /* name */
1853 FALSE, /* partial_inplace */
1854 0, /* src_mask */
1855 0xffff, /* dst_mask */
1856 FALSE), /* pcrel_offset */
1857
1858 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1859 the offset to the entry relative to the TOC base (r2). */
1860 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1861 0, /* rightshift */
1862 1, /* size (0 = byte, 1 = short, 2 = long) */
1863 16, /* bitsize */
1864 FALSE, /* pc_relative */
1865 0, /* bitpos */
1866 complain_overflow_signed, /* complain_on_overflow */
1867 ppc64_elf_unhandled_reloc, /* special_function */
1868 "R_PPC64_GOT_DTPREL16_DS", /* name */
1869 FALSE, /* partial_inplace */
1870 0, /* src_mask */
1871 0xfffc, /* dst_mask */
1872 FALSE), /* pcrel_offset */
1873
1874 /* Like GOT_DTPREL16_DS, but no overflow. */
1875 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1876 0, /* rightshift */
1877 1, /* size (0 = byte, 1 = short, 2 = long) */
1878 16, /* bitsize */
1879 FALSE, /* pc_relative */
1880 0, /* bitpos */
1881 complain_overflow_dont, /* complain_on_overflow */
1882 ppc64_elf_unhandled_reloc, /* special_function */
1883 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1884 FALSE, /* partial_inplace */
1885 0, /* src_mask */
1886 0xfffc, /* dst_mask */
1887 FALSE), /* pcrel_offset */
1888
1889 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1890 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1891 16, /* rightshift */
1892 1, /* size (0 = byte, 1 = short, 2 = long) */
1893 16, /* bitsize */
1894 FALSE, /* pc_relative */
1895 0, /* bitpos */
1896 complain_overflow_signed, /* complain_on_overflow */
1897 ppc64_elf_unhandled_reloc, /* special_function */
1898 "R_PPC64_GOT_DTPREL16_HI", /* name */
1899 FALSE, /* partial_inplace */
1900 0, /* src_mask */
1901 0xffff, /* dst_mask */
1902 FALSE), /* pcrel_offset */
1903
1904 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1905 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1906 16, /* rightshift */
1907 1, /* size (0 = byte, 1 = short, 2 = long) */
1908 16, /* bitsize */
1909 FALSE, /* pc_relative */
1910 0, /* bitpos */
1911 complain_overflow_signed, /* complain_on_overflow */
1912 ppc64_elf_unhandled_reloc, /* special_function */
1913 "R_PPC64_GOT_DTPREL16_HA", /* name */
1914 FALSE, /* partial_inplace */
1915 0, /* src_mask */
1916 0xffff, /* dst_mask */
1917 FALSE), /* pcrel_offset */
1918
1919 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1920 offset to the entry relative to the TOC base (r2). */
1921 HOWTO (R_PPC64_GOT_TPREL16_DS,
1922 0, /* rightshift */
1923 1, /* size (0 = byte, 1 = short, 2 = long) */
1924 16, /* bitsize */
1925 FALSE, /* pc_relative */
1926 0, /* bitpos */
1927 complain_overflow_signed, /* complain_on_overflow */
1928 ppc64_elf_unhandled_reloc, /* special_function */
1929 "R_PPC64_GOT_TPREL16_DS", /* name */
1930 FALSE, /* partial_inplace */
1931 0, /* src_mask */
1932 0xfffc, /* dst_mask */
1933 FALSE), /* pcrel_offset */
1934
1935 /* Like GOT_TPREL16_DS, but no overflow. */
1936 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1937 0, /* rightshift */
1938 1, /* size (0 = byte, 1 = short, 2 = long) */
1939 16, /* bitsize */
1940 FALSE, /* pc_relative */
1941 0, /* bitpos */
1942 complain_overflow_dont, /* complain_on_overflow */
1943 ppc64_elf_unhandled_reloc, /* special_function */
1944 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1945 FALSE, /* partial_inplace */
1946 0, /* src_mask */
1947 0xfffc, /* dst_mask */
1948 FALSE), /* pcrel_offset */
1949
1950 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1951 HOWTO (R_PPC64_GOT_TPREL16_HI,
1952 16, /* rightshift */
1953 1, /* size (0 = byte, 1 = short, 2 = long) */
1954 16, /* bitsize */
1955 FALSE, /* pc_relative */
1956 0, /* bitpos */
1957 complain_overflow_signed, /* complain_on_overflow */
1958 ppc64_elf_unhandled_reloc, /* special_function */
1959 "R_PPC64_GOT_TPREL16_HI", /* name */
1960 FALSE, /* partial_inplace */
1961 0, /* src_mask */
1962 0xffff, /* dst_mask */
1963 FALSE), /* pcrel_offset */
1964
1965 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1966 HOWTO (R_PPC64_GOT_TPREL16_HA,
1967 16, /* rightshift */
1968 1, /* size (0 = byte, 1 = short, 2 = long) */
1969 16, /* bitsize */
1970 FALSE, /* pc_relative */
1971 0, /* bitpos */
1972 complain_overflow_signed, /* complain_on_overflow */
1973 ppc64_elf_unhandled_reloc, /* special_function */
1974 "R_PPC64_GOT_TPREL16_HA", /* name */
1975 FALSE, /* partial_inplace */
1976 0, /* src_mask */
1977 0xffff, /* dst_mask */
1978 FALSE), /* pcrel_offset */
1979
1980 HOWTO (R_PPC64_JMP_IREL, /* type */
1981 0, /* rightshift */
1982 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1983 0, /* bitsize */
1984 FALSE, /* pc_relative */
1985 0, /* bitpos */
1986 complain_overflow_dont, /* complain_on_overflow */
1987 ppc64_elf_unhandled_reloc, /* special_function */
1988 "R_PPC64_JMP_IREL", /* name */
1989 FALSE, /* partial_inplace */
1990 0, /* src_mask */
1991 0, /* dst_mask */
1992 FALSE), /* pcrel_offset */
1993
1994 HOWTO (R_PPC64_IRELATIVE, /* type */
1995 0, /* rightshift */
1996 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1997 64, /* bitsize */
1998 FALSE, /* pc_relative */
1999 0, /* bitpos */
2000 complain_overflow_dont, /* complain_on_overflow */
2001 bfd_elf_generic_reloc, /* special_function */
2002 "R_PPC64_IRELATIVE", /* name */
2003 FALSE, /* partial_inplace */
2004 0, /* src_mask */
2005 ONES (64), /* dst_mask */
2006 FALSE), /* pcrel_offset */
2007
2008 /* A 16 bit relative relocation. */
2009 HOWTO (R_PPC64_REL16, /* type */
2010 0, /* rightshift */
2011 1, /* size (0 = byte, 1 = short, 2 = long) */
2012 16, /* bitsize */
2013 TRUE, /* pc_relative */
2014 0, /* bitpos */
2015 complain_overflow_signed, /* complain_on_overflow */
2016 bfd_elf_generic_reloc, /* special_function */
2017 "R_PPC64_REL16", /* name */
2018 FALSE, /* partial_inplace */
2019 0, /* src_mask */
2020 0xffff, /* dst_mask */
2021 TRUE), /* pcrel_offset */
2022
2023 /* A 16 bit relative relocation without overflow. */
2024 HOWTO (R_PPC64_REL16_LO, /* type */
2025 0, /* rightshift */
2026 1, /* size (0 = byte, 1 = short, 2 = long) */
2027 16, /* bitsize */
2028 TRUE, /* pc_relative */
2029 0, /* bitpos */
2030 complain_overflow_dont,/* complain_on_overflow */
2031 bfd_elf_generic_reloc, /* special_function */
2032 "R_PPC64_REL16_LO", /* name */
2033 FALSE, /* partial_inplace */
2034 0, /* src_mask */
2035 0xffff, /* dst_mask */
2036 TRUE), /* pcrel_offset */
2037
2038 /* The high order 16 bits of a relative address. */
2039 HOWTO (R_PPC64_REL16_HI, /* type */
2040 16, /* rightshift */
2041 1, /* size (0 = byte, 1 = short, 2 = long) */
2042 16, /* bitsize */
2043 TRUE, /* pc_relative */
2044 0, /* bitpos */
2045 complain_overflow_signed, /* complain_on_overflow */
2046 bfd_elf_generic_reloc, /* special_function */
2047 "R_PPC64_REL16_HI", /* name */
2048 FALSE, /* partial_inplace */
2049 0, /* src_mask */
2050 0xffff, /* dst_mask */
2051 TRUE), /* pcrel_offset */
2052
2053 /* The high order 16 bits of a relative address, plus 1 if the contents of
2054 the low 16 bits, treated as a signed number, is negative. */
2055 HOWTO (R_PPC64_REL16_HA, /* type */
2056 16, /* rightshift */
2057 1, /* size (0 = byte, 1 = short, 2 = long) */
2058 16, /* bitsize */
2059 TRUE, /* pc_relative */
2060 0, /* bitpos */
2061 complain_overflow_signed, /* complain_on_overflow */
2062 ppc64_elf_ha_reloc, /* special_function */
2063 "R_PPC64_REL16_HA", /* name */
2064 FALSE, /* partial_inplace */
2065 0, /* src_mask */
2066 0xffff, /* dst_mask */
2067 TRUE), /* pcrel_offset */
2068
2069 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2070 HOWTO (R_PPC64_REL16DX_HA, /* type */
2071 16, /* rightshift */
2072 2, /* size (0 = byte, 1 = short, 2 = long) */
2073 16, /* bitsize */
2074 TRUE, /* pc_relative */
2075 0, /* bitpos */
2076 complain_overflow_signed, /* complain_on_overflow */
2077 ppc64_elf_ha_reloc, /* special_function */
2078 "R_PPC64_REL16DX_HA", /* name */
2079 FALSE, /* partial_inplace */
2080 0, /* src_mask */
2081 0x1fffc1, /* dst_mask */
2082 TRUE), /* pcrel_offset */
2083
2084 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2085 HOWTO (R_PPC64_16DX_HA, /* type */
2086 16, /* rightshift */
2087 2, /* size (0 = byte, 1 = short, 2 = long) */
2088 16, /* bitsize */
2089 FALSE, /* pc_relative */
2090 0, /* bitpos */
2091 complain_overflow_signed, /* complain_on_overflow */
2092 ppc64_elf_ha_reloc, /* special_function */
2093 "R_PPC64_16DX_HA", /* name */
2094 FALSE, /* partial_inplace */
2095 0, /* src_mask */
2096 0x1fffc1, /* dst_mask */
2097 FALSE), /* pcrel_offset */
2098
2099 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2100 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2101 16, /* rightshift */
2102 1, /* size (0 = byte, 1 = short, 2 = long) */
2103 16, /* bitsize */
2104 FALSE, /* pc_relative */
2105 0, /* bitpos */
2106 complain_overflow_dont, /* complain_on_overflow */
2107 bfd_elf_generic_reloc, /* special_function */
2108 "R_PPC64_ADDR16_HIGH", /* name */
2109 FALSE, /* partial_inplace */
2110 0, /* src_mask */
2111 0xffff, /* dst_mask */
2112 FALSE), /* pcrel_offset */
2113
2114 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2115 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2116 16, /* rightshift */
2117 1, /* size (0 = byte, 1 = short, 2 = long) */
2118 16, /* bitsize */
2119 FALSE, /* pc_relative */
2120 0, /* bitpos */
2121 complain_overflow_dont, /* complain_on_overflow */
2122 ppc64_elf_ha_reloc, /* special_function */
2123 "R_PPC64_ADDR16_HIGHA", /* name */
2124 FALSE, /* partial_inplace */
2125 0, /* src_mask */
2126 0xffff, /* dst_mask */
2127 FALSE), /* pcrel_offset */
2128
2129 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2130 HOWTO (R_PPC64_DTPREL16_HIGH,
2131 16, /* rightshift */
2132 1, /* size (0 = byte, 1 = short, 2 = long) */
2133 16, /* bitsize */
2134 FALSE, /* pc_relative */
2135 0, /* bitpos */
2136 complain_overflow_dont, /* complain_on_overflow */
2137 ppc64_elf_unhandled_reloc, /* special_function */
2138 "R_PPC64_DTPREL16_HIGH", /* name */
2139 FALSE, /* partial_inplace */
2140 0, /* src_mask */
2141 0xffff, /* dst_mask */
2142 FALSE), /* pcrel_offset */
2143
2144 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2145 HOWTO (R_PPC64_DTPREL16_HIGHA,
2146 16, /* rightshift */
2147 1, /* size (0 = byte, 1 = short, 2 = long) */
2148 16, /* bitsize */
2149 FALSE, /* pc_relative */
2150 0, /* bitpos */
2151 complain_overflow_dont, /* complain_on_overflow */
2152 ppc64_elf_unhandled_reloc, /* special_function */
2153 "R_PPC64_DTPREL16_HIGHA", /* name */
2154 FALSE, /* partial_inplace */
2155 0, /* src_mask */
2156 0xffff, /* dst_mask */
2157 FALSE), /* pcrel_offset */
2158
2159 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2160 HOWTO (R_PPC64_TPREL16_HIGH,
2161 16, /* rightshift */
2162 1, /* size (0 = byte, 1 = short, 2 = long) */
2163 16, /* bitsize */
2164 FALSE, /* pc_relative */
2165 0, /* bitpos */
2166 complain_overflow_dont, /* complain_on_overflow */
2167 ppc64_elf_unhandled_reloc, /* special_function */
2168 "R_PPC64_TPREL16_HIGH", /* name */
2169 FALSE, /* partial_inplace */
2170 0, /* src_mask */
2171 0xffff, /* dst_mask */
2172 FALSE), /* pcrel_offset */
2173
2174 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2175 HOWTO (R_PPC64_TPREL16_HIGHA,
2176 16, /* rightshift */
2177 1, /* size (0 = byte, 1 = short, 2 = long) */
2178 16, /* bitsize */
2179 FALSE, /* pc_relative */
2180 0, /* bitpos */
2181 complain_overflow_dont, /* complain_on_overflow */
2182 ppc64_elf_unhandled_reloc, /* special_function */
2183 "R_PPC64_TPREL16_HIGHA", /* name */
2184 FALSE, /* partial_inplace */
2185 0, /* src_mask */
2186 0xffff, /* dst_mask */
2187 FALSE), /* pcrel_offset */
2188
2189 /* Marker reloc on ELFv2 large-model function entry. */
2190 HOWTO (R_PPC64_ENTRY,
2191 0, /* rightshift */
2192 2, /* size (0 = byte, 1 = short, 2 = long) */
2193 32, /* bitsize */
2194 FALSE, /* pc_relative */
2195 0, /* bitpos */
2196 complain_overflow_dont, /* complain_on_overflow */
2197 bfd_elf_generic_reloc, /* special_function */
2198 "R_PPC64_ENTRY", /* name */
2199 FALSE, /* partial_inplace */
2200 0, /* src_mask */
2201 0, /* dst_mask */
2202 FALSE), /* pcrel_offset */
2203
2204 /* Like ADDR64, but use local entry point of function. */
2205 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2206 0, /* rightshift */
2207 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2208 64, /* bitsize */
2209 FALSE, /* pc_relative */
2210 0, /* bitpos */
2211 complain_overflow_dont, /* complain_on_overflow */
2212 bfd_elf_generic_reloc, /* special_function */
2213 "R_PPC64_ADDR64_LOCAL", /* name */
2214 FALSE, /* partial_inplace */
2215 0, /* src_mask */
2216 ONES (64), /* dst_mask */
2217 FALSE), /* pcrel_offset */
2218
2219 /* GNU extension to record C++ vtable hierarchy. */
2220 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2221 0, /* rightshift */
2222 0, /* size (0 = byte, 1 = short, 2 = long) */
2223 0, /* bitsize */
2224 FALSE, /* pc_relative */
2225 0, /* bitpos */
2226 complain_overflow_dont, /* complain_on_overflow */
2227 NULL, /* special_function */
2228 "R_PPC64_GNU_VTINHERIT", /* name */
2229 FALSE, /* partial_inplace */
2230 0, /* src_mask */
2231 0, /* dst_mask */
2232 FALSE), /* pcrel_offset */
2233
2234 /* GNU extension to record C++ vtable member usage. */
2235 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2236 0, /* rightshift */
2237 0, /* size (0 = byte, 1 = short, 2 = long) */
2238 0, /* bitsize */
2239 FALSE, /* pc_relative */
2240 0, /* bitpos */
2241 complain_overflow_dont, /* complain_on_overflow */
2242 NULL, /* special_function */
2243 "R_PPC64_GNU_VTENTRY", /* name */
2244 FALSE, /* partial_inplace */
2245 0, /* src_mask */
2246 0, /* dst_mask */
2247 FALSE), /* pcrel_offset */
2248 };
2249
2250 \f
2251 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2252 be done. */
2253
2254 static void
2255 ppc_howto_init (void)
2256 {
2257 unsigned int i, type;
2258
2259 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2260 {
2261 type = ppc64_elf_howto_raw[i].type;
2262 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2263 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2264 }
2265 }
2266
2267 static reloc_howto_type *
2268 ppc64_elf_reloc_type_lookup (bfd *abfd,
2269 bfd_reloc_code_real_type code)
2270 {
2271 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2272
2273 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2274 /* Initialize howto table if needed. */
2275 ppc_howto_init ();
2276
2277 switch (code)
2278 {
2279 default:
2280 /* xgettext:c-format */
2281 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2282 bfd_set_error (bfd_error_bad_value);
2283 return NULL;
2284
2285 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2286 break;
2287 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2288 break;
2289 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2290 break;
2291 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2292 break;
2293 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2294 break;
2295 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2296 break;
2297 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2298 break;
2299 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2300 break;
2301 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2302 break;
2303 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2304 break;
2305 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2306 break;
2307 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2308 break;
2309 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2310 break;
2311 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2312 break;
2313 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2314 break;
2315 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2316 break;
2317 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2318 break;
2319 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2320 break;
2321 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2322 break;
2323 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2324 break;
2325 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2326 break;
2327 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2328 break;
2329 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2330 break;
2331 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2332 break;
2333 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2334 break;
2335 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2336 break;
2337 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2338 break;
2339 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2340 break;
2341 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2342 break;
2343 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2344 break;
2345 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2346 break;
2347 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2348 break;
2349 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2350 break;
2351 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2352 break;
2353 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2354 break;
2355 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2356 break;
2357 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2358 break;
2359 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2360 break;
2361 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2362 break;
2363 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2364 break;
2365 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2366 break;
2367 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2368 break;
2369 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2370 break;
2371 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2372 break;
2373 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2374 break;
2375 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2376 break;
2377 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2378 break;
2379 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2380 break;
2381 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2382 break;
2383 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2384 break;
2385 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2386 break;
2387 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2388 break;
2389 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2390 break;
2391 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2392 break;
2393 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2394 break;
2395 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2396 break;
2397 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2398 break;
2399 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2400 break;
2401 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2402 break;
2403 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2404 break;
2405 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2406 break;
2407 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2408 break;
2409 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2410 break;
2411 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2412 break;
2413 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2414 break;
2415 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2416 break;
2417 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2418 break;
2419 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2420 break;
2421 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2422 break;
2423 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2424 break;
2425 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2426 break;
2427 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2428 break;
2429 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2430 break;
2431 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2432 break;
2433 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2434 break;
2435 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2436 break;
2437 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2438 break;
2439 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2440 break;
2441 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2442 break;
2443 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2444 break;
2445 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2446 break;
2447 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2448 break;
2449 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2450 break;
2451 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2452 break;
2453 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2454 break;
2455 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2456 break;
2457 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2458 break;
2459 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2460 break;
2461 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2462 break;
2463 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2464 break;
2465 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2466 break;
2467 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2468 break;
2469 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2470 break;
2471 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2472 break;
2473 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2474 break;
2475 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2476 break;
2477 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2478 break;
2479 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2480 break;
2481 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2482 break;
2483 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2484 break;
2485 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2486 break;
2487 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2488 break;
2489 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2490 break;
2491 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2492 break;
2493 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2494 break;
2495 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2496 break;
2497 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2498 break;
2499 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2500 break;
2501 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2502 break;
2503 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2504 break;
2505 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2506 break;
2507 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2508 break;
2509 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2510 break;
2511 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2512 break;
2513 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2514 break;
2515 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2516 break;
2517 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2518 break;
2519 }
2520
2521 return ppc64_elf_howto_table[r];
2522 };
2523
2524 static reloc_howto_type *
2525 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2526 const char *r_name)
2527 {
2528 unsigned int i;
2529
2530 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2531 if (ppc64_elf_howto_raw[i].name != NULL
2532 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2533 return &ppc64_elf_howto_raw[i];
2534
2535
2536 return NULL;
2537 }
2538
2539 /* Set the howto pointer for a PowerPC ELF reloc. */
2540
2541 static bfd_boolean
2542 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2543 Elf_Internal_Rela *dst)
2544 {
2545 unsigned int type;
2546
2547 /* Initialize howto table if needed. */
2548 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2549 ppc_howto_init ();
2550
2551 type = ELF64_R_TYPE (dst->r_info);
2552 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2553 {
2554 /* xgettext:c-format */
2555 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2556 abfd, type);
2557 bfd_set_error (bfd_error_bad_value);
2558 return FALSE;
2559 }
2560 cache_ptr->howto = ppc64_elf_howto_table[type];
2561 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2562 {
2563 /* xgettext:c-format */
2564 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2565 abfd, type);
2566 bfd_set_error (bfd_error_bad_value);
2567 return FALSE;
2568 }
2569
2570 return TRUE;
2571 }
2572
2573 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2574
2575 static bfd_reloc_status_type
2576 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2577 void *data, asection *input_section,
2578 bfd *output_bfd, char **error_message)
2579 {
2580 enum elf_ppc64_reloc_type r_type;
2581 long insn;
2582 bfd_size_type octets;
2583 bfd_vma value;
2584
2585 /* If this is a relocatable link (output_bfd test tells us), just
2586 call the generic function. Any adjustment will be done at final
2587 link time. */
2588 if (output_bfd != NULL)
2589 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2590 input_section, output_bfd, error_message);
2591
2592 /* Adjust the addend for sign extension of the low 16 bits.
2593 We won't actually be using the low 16 bits, so trashing them
2594 doesn't matter. */
2595 reloc_entry->addend += 0x8000;
2596 r_type = reloc_entry->howto->type;
2597 if (r_type != R_PPC64_REL16DX_HA)
2598 return bfd_reloc_continue;
2599
2600 value = 0;
2601 if (!bfd_is_com_section (symbol->section))
2602 value = symbol->value;
2603 value += (reloc_entry->addend
2604 + symbol->section->output_offset
2605 + symbol->section->output_section->vma);
2606 value -= (reloc_entry->address
2607 + input_section->output_offset
2608 + input_section->output_section->vma);
2609 value = (bfd_signed_vma) value >> 16;
2610
2611 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2612 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2613 insn &= ~0x1fffc1;
2614 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2615 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2616 if (value + 0x8000 > 0xffff)
2617 return bfd_reloc_overflow;
2618 return bfd_reloc_ok;
2619 }
2620
2621 static bfd_reloc_status_type
2622 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2623 void *data, asection *input_section,
2624 bfd *output_bfd, char **error_message)
2625 {
2626 if (output_bfd != NULL)
2627 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2628 input_section, output_bfd, error_message);
2629
2630 if (strcmp (symbol->section->name, ".opd") == 0
2631 && (symbol->section->owner->flags & DYNAMIC) == 0)
2632 {
2633 bfd_vma dest = opd_entry_value (symbol->section,
2634 symbol->value + reloc_entry->addend,
2635 NULL, NULL, FALSE);
2636 if (dest != (bfd_vma) -1)
2637 reloc_entry->addend = dest - (symbol->value
2638 + symbol->section->output_section->vma
2639 + symbol->section->output_offset);
2640 }
2641 else
2642 {
2643 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2644
2645 if (symbol->section->owner != abfd
2646 && symbol->section->owner != NULL
2647 && abiversion (symbol->section->owner) >= 2)
2648 {
2649 unsigned int i;
2650
2651 for (i = 0; i < symbol->section->owner->symcount; ++i)
2652 {
2653 asymbol *symdef = symbol->section->owner->outsymbols[i];
2654
2655 if (strcmp (symdef->name, symbol->name) == 0)
2656 {
2657 elfsym = (elf_symbol_type *) symdef;
2658 break;
2659 }
2660 }
2661 }
2662 reloc_entry->addend
2663 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2664 }
2665 return bfd_reloc_continue;
2666 }
2667
2668 static bfd_reloc_status_type
2669 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2670 void *data, asection *input_section,
2671 bfd *output_bfd, char **error_message)
2672 {
2673 long insn;
2674 enum elf_ppc64_reloc_type r_type;
2675 bfd_size_type octets;
2676 /* Assume 'at' branch hints. */
2677 bfd_boolean is_isa_v2 = TRUE;
2678
2679 /* If this is a relocatable link (output_bfd test tells us), just
2680 call the generic function. Any adjustment will be done at final
2681 link time. */
2682 if (output_bfd != NULL)
2683 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2684 input_section, output_bfd, error_message);
2685
2686 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2687 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2688 insn &= ~(0x01 << 21);
2689 r_type = reloc_entry->howto->type;
2690 if (r_type == R_PPC64_ADDR14_BRTAKEN
2691 || r_type == R_PPC64_REL14_BRTAKEN)
2692 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2693
2694 if (is_isa_v2)
2695 {
2696 /* Set 'a' bit. This is 0b00010 in BO field for branch
2697 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2698 for branch on CTR insns (BO == 1a00t or 1a01t). */
2699 if ((insn & (0x14 << 21)) == (0x04 << 21))
2700 insn |= 0x02 << 21;
2701 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2702 insn |= 0x08 << 21;
2703 else
2704 goto out;
2705 }
2706 else
2707 {
2708 bfd_vma target = 0;
2709 bfd_vma from;
2710
2711 if (!bfd_is_com_section (symbol->section))
2712 target = symbol->value;
2713 target += symbol->section->output_section->vma;
2714 target += symbol->section->output_offset;
2715 target += reloc_entry->addend;
2716
2717 from = (reloc_entry->address
2718 + input_section->output_offset
2719 + input_section->output_section->vma);
2720
2721 /* Invert 'y' bit if not the default. */
2722 if ((bfd_signed_vma) (target - from) < 0)
2723 insn ^= 0x01 << 21;
2724 }
2725 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2726 out:
2727 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2728 input_section, output_bfd, error_message);
2729 }
2730
2731 static bfd_reloc_status_type
2732 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2733 void *data, asection *input_section,
2734 bfd *output_bfd, char **error_message)
2735 {
2736 /* If this is a relocatable link (output_bfd test tells us), just
2737 call the generic function. Any adjustment will be done at final
2738 link time. */
2739 if (output_bfd != NULL)
2740 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2741 input_section, output_bfd, error_message);
2742
2743 /* Subtract the symbol section base address. */
2744 reloc_entry->addend -= symbol->section->output_section->vma;
2745 return bfd_reloc_continue;
2746 }
2747
2748 static bfd_reloc_status_type
2749 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2750 void *data, asection *input_section,
2751 bfd *output_bfd, char **error_message)
2752 {
2753 /* If this is a relocatable link (output_bfd test tells us), just
2754 call the generic function. Any adjustment will be done at final
2755 link time. */
2756 if (output_bfd != NULL)
2757 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2758 input_section, output_bfd, error_message);
2759
2760 /* Subtract the symbol section base address. */
2761 reloc_entry->addend -= symbol->section->output_section->vma;
2762
2763 /* Adjust the addend for sign extension of the low 16 bits. */
2764 reloc_entry->addend += 0x8000;
2765 return bfd_reloc_continue;
2766 }
2767
2768 static bfd_reloc_status_type
2769 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2770 void *data, asection *input_section,
2771 bfd *output_bfd, char **error_message)
2772 {
2773 bfd_vma TOCstart;
2774
2775 /* If this is a relocatable link (output_bfd test tells us), just
2776 call the generic function. Any adjustment will be done at final
2777 link time. */
2778 if (output_bfd != NULL)
2779 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2780 input_section, output_bfd, error_message);
2781
2782 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2783 if (TOCstart == 0)
2784 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2785
2786 /* Subtract the TOC base address. */
2787 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2788 return bfd_reloc_continue;
2789 }
2790
2791 static bfd_reloc_status_type
2792 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2793 void *data, asection *input_section,
2794 bfd *output_bfd, char **error_message)
2795 {
2796 bfd_vma TOCstart;
2797
2798 /* If this is a relocatable link (output_bfd test tells us), just
2799 call the generic function. Any adjustment will be done at final
2800 link time. */
2801 if (output_bfd != NULL)
2802 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2803 input_section, output_bfd, error_message);
2804
2805 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2806 if (TOCstart == 0)
2807 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2808
2809 /* Subtract the TOC base address. */
2810 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2811
2812 /* Adjust the addend for sign extension of the low 16 bits. */
2813 reloc_entry->addend += 0x8000;
2814 return bfd_reloc_continue;
2815 }
2816
2817 static bfd_reloc_status_type
2818 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2819 void *data, asection *input_section,
2820 bfd *output_bfd, char **error_message)
2821 {
2822 bfd_vma TOCstart;
2823 bfd_size_type octets;
2824
2825 /* If this is a relocatable link (output_bfd test tells us), just
2826 call the generic function. Any adjustment will be done at final
2827 link time. */
2828 if (output_bfd != NULL)
2829 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2830 input_section, output_bfd, error_message);
2831
2832 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2833 if (TOCstart == 0)
2834 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2835
2836 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2837 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2838 return bfd_reloc_ok;
2839 }
2840
2841 static bfd_reloc_status_type
2842 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2843 void *data, asection *input_section,
2844 bfd *output_bfd, char **error_message)
2845 {
2846 /* If this is a relocatable link (output_bfd test tells us), just
2847 call the generic function. Any adjustment will be done at final
2848 link time. */
2849 if (output_bfd != NULL)
2850 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2851 input_section, output_bfd, error_message);
2852
2853 if (error_message != NULL)
2854 {
2855 static char buf[60];
2856 sprintf (buf, "generic linker can't handle %s",
2857 reloc_entry->howto->name);
2858 *error_message = buf;
2859 }
2860 return bfd_reloc_dangerous;
2861 }
2862
2863 /* Track GOT entries needed for a given symbol. We might need more
2864 than one got entry per symbol. */
2865 struct got_entry
2866 {
2867 struct got_entry *next;
2868
2869 /* The symbol addend that we'll be placing in the GOT. */
2870 bfd_vma addend;
2871
2872 /* Unlike other ELF targets, we use separate GOT entries for the same
2873 symbol referenced from different input files. This is to support
2874 automatic multiple TOC/GOT sections, where the TOC base can vary
2875 from one input file to another. After partitioning into TOC groups
2876 we merge entries within the group.
2877
2878 Point to the BFD owning this GOT entry. */
2879 bfd *owner;
2880
2881 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2882 TLS_TPREL or TLS_DTPREL for tls entries. */
2883 unsigned char tls_type;
2884
2885 /* Non-zero if got.ent points to real entry. */
2886 unsigned char is_indirect;
2887
2888 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2889 union
2890 {
2891 bfd_signed_vma refcount;
2892 bfd_vma offset;
2893 struct got_entry *ent;
2894 } got;
2895 };
2896
2897 /* The same for PLT. */
2898 struct plt_entry
2899 {
2900 struct plt_entry *next;
2901
2902 bfd_vma addend;
2903
2904 union
2905 {
2906 bfd_signed_vma refcount;
2907 bfd_vma offset;
2908 } plt;
2909 };
2910
2911 struct ppc64_elf_obj_tdata
2912 {
2913 struct elf_obj_tdata elf;
2914
2915 /* Shortcuts to dynamic linker sections. */
2916 asection *got;
2917 asection *relgot;
2918
2919 /* Used during garbage collection. We attach global symbols defined
2920 on removed .opd entries to this section so that the sym is removed. */
2921 asection *deleted_section;
2922
2923 /* TLS local dynamic got entry handling. Support for multiple GOT
2924 sections means we potentially need one of these for each input bfd. */
2925 struct got_entry tlsld_got;
2926
2927 union {
2928 /* A copy of relocs before they are modified for --emit-relocs. */
2929 Elf_Internal_Rela *relocs;
2930
2931 /* Section contents. */
2932 bfd_byte *contents;
2933 } opd;
2934
2935 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2936 the reloc to be in the range -32768 to 32767. */
2937 unsigned int has_small_toc_reloc : 1;
2938
2939 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2940 instruction not one we handle. */
2941 unsigned int unexpected_toc_insn : 1;
2942 };
2943
2944 #define ppc64_elf_tdata(bfd) \
2945 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2946
2947 #define ppc64_tlsld_got(bfd) \
2948 (&ppc64_elf_tdata (bfd)->tlsld_got)
2949
2950 #define is_ppc64_elf(bfd) \
2951 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2952 && elf_object_id (bfd) == PPC64_ELF_DATA)
2953
2954 /* Override the generic function because we store some extras. */
2955
2956 static bfd_boolean
2957 ppc64_elf_mkobject (bfd *abfd)
2958 {
2959 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2960 PPC64_ELF_DATA);
2961 }
2962
2963 /* Fix bad default arch selected for a 64 bit input bfd when the
2964 default is 32 bit. Also select arch based on apuinfo. */
2965
2966 static bfd_boolean
2967 ppc64_elf_object_p (bfd *abfd)
2968 {
2969 if (!abfd->arch_info->the_default)
2970 return TRUE;
2971
2972 if (abfd->arch_info->bits_per_word == 32)
2973 {
2974 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2975
2976 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2977 {
2978 /* Relies on arch after 32 bit default being 64 bit default. */
2979 abfd->arch_info = abfd->arch_info->next;
2980 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2981 }
2982 }
2983 return _bfd_elf_ppc_set_arch (abfd);
2984 }
2985
2986 /* Support for core dump NOTE sections. */
2987
2988 static bfd_boolean
2989 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2990 {
2991 size_t offset, size;
2992
2993 if (note->descsz != 504)
2994 return FALSE;
2995
2996 /* pr_cursig */
2997 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2998
2999 /* pr_pid */
3000 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3001
3002 /* pr_reg */
3003 offset = 112;
3004 size = 384;
3005
3006 /* Make a ".reg/999" section. */
3007 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3008 size, note->descpos + offset);
3009 }
3010
3011 static bfd_boolean
3012 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3013 {
3014 if (note->descsz != 136)
3015 return FALSE;
3016
3017 elf_tdata (abfd)->core->pid
3018 = bfd_get_32 (abfd, note->descdata + 24);
3019 elf_tdata (abfd)->core->program
3020 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3021 elf_tdata (abfd)->core->command
3022 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3023
3024 return TRUE;
3025 }
3026
3027 static char *
3028 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3029 ...)
3030 {
3031 switch (note_type)
3032 {
3033 default:
3034 return NULL;
3035
3036 case NT_PRPSINFO:
3037 {
3038 char data[136] ATTRIBUTE_NONSTRING;
3039 va_list ap;
3040
3041 va_start (ap, note_type);
3042 memset (data, 0, sizeof (data));
3043 strncpy (data + 40, va_arg (ap, const char *), 16);
3044 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3045 DIAGNOSTIC_PUSH;
3046 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
3047 -Wstringop-truncation:
3048 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3049 */
3050 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3051 #endif
3052 strncpy (data + 56, va_arg (ap, const char *), 80);
3053 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3054 DIAGNOSTIC_POP;
3055 #endif
3056 va_end (ap);
3057 return elfcore_write_note (abfd, buf, bufsiz,
3058 "CORE", note_type, data, sizeof (data));
3059 }
3060
3061 case NT_PRSTATUS:
3062 {
3063 char data[504];
3064 va_list ap;
3065 long pid;
3066 int cursig;
3067 const void *greg;
3068
3069 va_start (ap, note_type);
3070 memset (data, 0, 112);
3071 pid = va_arg (ap, long);
3072 bfd_put_32 (abfd, pid, data + 32);
3073 cursig = va_arg (ap, int);
3074 bfd_put_16 (abfd, cursig, data + 12);
3075 greg = va_arg (ap, const void *);
3076 memcpy (data + 112, greg, 384);
3077 memset (data + 496, 0, 8);
3078 va_end (ap);
3079 return elfcore_write_note (abfd, buf, bufsiz,
3080 "CORE", note_type, data, sizeof (data));
3081 }
3082 }
3083 }
3084
3085 /* Add extra PPC sections. */
3086
3087 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3088 {
3089 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3090 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3091 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3092 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3093 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3094 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3095 { NULL, 0, 0, 0, 0 }
3096 };
3097
3098 enum _ppc64_sec_type {
3099 sec_normal = 0,
3100 sec_opd = 1,
3101 sec_toc = 2
3102 };
3103
3104 struct _ppc64_elf_section_data
3105 {
3106 struct bfd_elf_section_data elf;
3107
3108 union
3109 {
3110 /* An array with one entry for each opd function descriptor,
3111 and some spares since opd entries may be either 16 or 24 bytes. */
3112 #define OPD_NDX(OFF) ((OFF) >> 4)
3113 struct _opd_sec_data
3114 {
3115 /* Points to the function code section for local opd entries. */
3116 asection **func_sec;
3117
3118 /* After editing .opd, adjust references to opd local syms. */
3119 long *adjust;
3120 } opd;
3121
3122 /* An array for toc sections, indexed by offset/8. */
3123 struct _toc_sec_data
3124 {
3125 /* Specifies the relocation symbol index used at a given toc offset. */
3126 unsigned *symndx;
3127
3128 /* And the relocation addend. */
3129 bfd_vma *add;
3130 } toc;
3131 } u;
3132
3133 enum _ppc64_sec_type sec_type:2;
3134
3135 /* Flag set when small branches are detected. Used to
3136 select suitable defaults for the stub group size. */
3137 unsigned int has_14bit_branch:1;
3138
3139 /* Flag set when PLTCALL relocs are detected. */
3140 unsigned int has_pltcall:1;
3141 };
3142
3143 #define ppc64_elf_section_data(sec) \
3144 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3145
3146 static bfd_boolean
3147 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3148 {
3149 if (!sec->used_by_bfd)
3150 {
3151 struct _ppc64_elf_section_data *sdata;
3152 bfd_size_type amt = sizeof (*sdata);
3153
3154 sdata = bfd_zalloc (abfd, amt);
3155 if (sdata == NULL)
3156 return FALSE;
3157 sec->used_by_bfd = sdata;
3158 }
3159
3160 return _bfd_elf_new_section_hook (abfd, sec);
3161 }
3162
3163 static struct _opd_sec_data *
3164 get_opd_info (asection * sec)
3165 {
3166 if (sec != NULL
3167 && ppc64_elf_section_data (sec) != NULL
3168 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3169 return &ppc64_elf_section_data (sec)->u.opd;
3170 return NULL;
3171 }
3172 \f
3173 /* Parameters for the qsort hook. */
3174 static bfd_boolean synthetic_relocatable;
3175 static asection *synthetic_opd;
3176
3177 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3178
3179 static int
3180 compare_symbols (const void *ap, const void *bp)
3181 {
3182 const asymbol *a = * (const asymbol **) ap;
3183 const asymbol *b = * (const asymbol **) bp;
3184
3185 /* Section symbols first. */
3186 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3187 return -1;
3188 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3189 return 1;
3190
3191 /* then .opd symbols. */
3192 if (synthetic_opd != NULL)
3193 {
3194 if (strcmp (a->section->name, ".opd") == 0
3195 && strcmp (b->section->name, ".opd") != 0)
3196 return -1;
3197 if (strcmp (a->section->name, ".opd") != 0
3198 && strcmp (b->section->name, ".opd") == 0)
3199 return 1;
3200 }
3201
3202 /* then other code symbols. */
3203 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3204 == (SEC_CODE | SEC_ALLOC)
3205 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3206 != (SEC_CODE | SEC_ALLOC))
3207 return -1;
3208
3209 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3210 != (SEC_CODE | SEC_ALLOC)
3211 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3212 == (SEC_CODE | SEC_ALLOC))
3213 return 1;
3214
3215 if (synthetic_relocatable)
3216 {
3217 if (a->section->id < b->section->id)
3218 return -1;
3219
3220 if (a->section->id > b->section->id)
3221 return 1;
3222 }
3223
3224 if (a->value + a->section->vma < b->value + b->section->vma)
3225 return -1;
3226
3227 if (a->value + a->section->vma > b->value + b->section->vma)
3228 return 1;
3229
3230 /* For syms with the same value, prefer strong dynamic global function
3231 syms over other syms. */
3232 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3233 return -1;
3234
3235 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3236 return 1;
3237
3238 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3239 return -1;
3240
3241 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3242 return 1;
3243
3244 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3245 return -1;
3246
3247 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3248 return 1;
3249
3250 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3251 return -1;
3252
3253 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3254 return 1;
3255
3256 return a > b;
3257 }
3258
3259 /* Search SYMS for a symbol of the given VALUE. */
3260
3261 static asymbol *
3262 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3263 {
3264 long mid;
3265
3266 if (id == (unsigned) -1)
3267 {
3268 while (lo < hi)
3269 {
3270 mid = (lo + hi) >> 1;
3271 if (syms[mid]->value + syms[mid]->section->vma < value)
3272 lo = mid + 1;
3273 else if (syms[mid]->value + syms[mid]->section->vma > value)
3274 hi = mid;
3275 else
3276 return syms[mid];
3277 }
3278 }
3279 else
3280 {
3281 while (lo < hi)
3282 {
3283 mid = (lo + hi) >> 1;
3284 if (syms[mid]->section->id < id)
3285 lo = mid + 1;
3286 else if (syms[mid]->section->id > id)
3287 hi = mid;
3288 else if (syms[mid]->value < value)
3289 lo = mid + 1;
3290 else if (syms[mid]->value > value)
3291 hi = mid;
3292 else
3293 return syms[mid];
3294 }
3295 }
3296 return NULL;
3297 }
3298
3299 static bfd_boolean
3300 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3301 {
3302 bfd_vma vma = *(bfd_vma *) ptr;
3303 return ((section->flags & SEC_ALLOC) != 0
3304 && section->vma <= vma
3305 && vma < section->vma + section->size);
3306 }
3307
3308 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3309 entry syms. Also generate @plt symbols for the glink branch table.
3310 Returns count of synthetic symbols in RET or -1 on error. */
3311
3312 static long
3313 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3314 long static_count, asymbol **static_syms,
3315 long dyn_count, asymbol **dyn_syms,
3316 asymbol **ret)
3317 {
3318 asymbol *s;
3319 size_t i, j, count;
3320 char *names;
3321 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3322 asection *opd = NULL;
3323 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3324 asymbol **syms;
3325 int abi = abiversion (abfd);
3326
3327 *ret = NULL;
3328
3329 if (abi < 2)
3330 {
3331 opd = bfd_get_section_by_name (abfd, ".opd");
3332 if (opd == NULL && abi == 1)
3333 return 0;
3334 }
3335
3336 syms = NULL;
3337 codesecsym = 0;
3338 codesecsymend = 0;
3339 secsymend = 0;
3340 opdsymend = 0;
3341 symcount = 0;
3342 if (opd != NULL)
3343 {
3344 symcount = static_count;
3345 if (!relocatable)
3346 symcount += dyn_count;
3347 if (symcount == 0)
3348 return 0;
3349
3350 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3351 if (syms == NULL)
3352 return -1;
3353
3354 if (!relocatable && static_count != 0 && dyn_count != 0)
3355 {
3356 /* Use both symbol tables. */
3357 memcpy (syms, static_syms, static_count * sizeof (*syms));
3358 memcpy (syms + static_count, dyn_syms,
3359 (dyn_count + 1) * sizeof (*syms));
3360 }
3361 else if (!relocatable && static_count == 0)
3362 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3363 else
3364 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3365
3366 /* Trim uninteresting symbols. Interesting symbols are section,
3367 function, and notype symbols. */
3368 for (i = 0, j = 0; i < symcount; ++i)
3369 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3370 | BSF_RELC | BSF_SRELC)) == 0)
3371 syms[j++] = syms[i];
3372 symcount = j;
3373
3374 synthetic_relocatable = relocatable;
3375 synthetic_opd = opd;
3376 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3377
3378 if (!relocatable && symcount > 1)
3379 {
3380 /* Trim duplicate syms, since we may have merged the normal
3381 and dynamic symbols. Actually, we only care about syms
3382 that have different values, so trim any with the same
3383 value. Don't consider ifunc and ifunc resolver symbols
3384 duplicates however, because GDB wants to know whether a
3385 text symbol is an ifunc resolver. */
3386 for (i = 1, j = 1; i < symcount; ++i)
3387 {
3388 const asymbol *s0 = syms[i - 1];
3389 const asymbol *s1 = syms[i];
3390
3391 if ((s0->value + s0->section->vma
3392 != s1->value + s1->section->vma)
3393 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3394 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3395 syms[j++] = syms[i];
3396 }
3397 symcount = j;
3398 }
3399
3400 i = 0;
3401 /* Note that here and in compare_symbols we can't compare opd and
3402 sym->section directly. With separate debug info files, the
3403 symbols will be extracted from the debug file while abfd passed
3404 to this function is the real binary. */
3405 if (strcmp (syms[i]->section->name, ".opd") == 0)
3406 ++i;
3407 codesecsym = i;
3408
3409 for (; i < symcount; ++i)
3410 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3411 | SEC_THREAD_LOCAL))
3412 != (SEC_CODE | SEC_ALLOC))
3413 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3414 break;
3415 codesecsymend = i;
3416
3417 for (; i < symcount; ++i)
3418 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3419 break;
3420 secsymend = i;
3421
3422 for (; i < symcount; ++i)
3423 if (strcmp (syms[i]->section->name, ".opd") != 0)
3424 break;
3425 opdsymend = i;
3426
3427 for (; i < symcount; ++i)
3428 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3429 != (SEC_CODE | SEC_ALLOC))
3430 break;
3431 symcount = i;
3432 }
3433 count = 0;
3434
3435 if (relocatable)
3436 {
3437 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3438 arelent *r;
3439 size_t size;
3440 size_t relcount;
3441
3442 if (opdsymend == secsymend)
3443 goto done;
3444
3445 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3446 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3447 if (relcount == 0)
3448 goto done;
3449
3450 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3451 {
3452 count = -1;
3453 goto done;
3454 }
3455
3456 size = 0;
3457 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3458 {
3459 asymbol *sym;
3460
3461 while (r < opd->relocation + relcount
3462 && r->address < syms[i]->value + opd->vma)
3463 ++r;
3464
3465 if (r == opd->relocation + relcount)
3466 break;
3467
3468 if (r->address != syms[i]->value + opd->vma)
3469 continue;
3470
3471 if (r->howto->type != R_PPC64_ADDR64)
3472 continue;
3473
3474 sym = *r->sym_ptr_ptr;
3475 if (!sym_exists_at (syms, opdsymend, symcount,
3476 sym->section->id, sym->value + r->addend))
3477 {
3478 ++count;
3479 size += sizeof (asymbol);
3480 size += strlen (syms[i]->name) + 2;
3481 }
3482 }
3483
3484 if (size == 0)
3485 goto done;
3486 s = *ret = bfd_malloc (size);
3487 if (s == NULL)
3488 {
3489 count = -1;
3490 goto done;
3491 }
3492
3493 names = (char *) (s + count);
3494
3495 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3496 {
3497 asymbol *sym;
3498
3499 while (r < opd->relocation + relcount
3500 && r->address < syms[i]->value + opd->vma)
3501 ++r;
3502
3503 if (r == opd->relocation + relcount)
3504 break;
3505
3506 if (r->address != syms[i]->value + opd->vma)
3507 continue;
3508
3509 if (r->howto->type != R_PPC64_ADDR64)
3510 continue;
3511
3512 sym = *r->sym_ptr_ptr;
3513 if (!sym_exists_at (syms, opdsymend, symcount,
3514 sym->section->id, sym->value + r->addend))
3515 {
3516 size_t len;
3517
3518 *s = *syms[i];
3519 s->flags |= BSF_SYNTHETIC;
3520 s->section = sym->section;
3521 s->value = sym->value + r->addend;
3522 s->name = names;
3523 *names++ = '.';
3524 len = strlen (syms[i]->name);
3525 memcpy (names, syms[i]->name, len + 1);
3526 names += len + 1;
3527 /* Have udata.p point back to the original symbol this
3528 synthetic symbol was derived from. */
3529 s->udata.p = syms[i];
3530 s++;
3531 }
3532 }
3533 }
3534 else
3535 {
3536 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3537 bfd_byte *contents = NULL;
3538 size_t size;
3539 size_t plt_count = 0;
3540 bfd_vma glink_vma = 0, resolv_vma = 0;
3541 asection *dynamic, *glink = NULL, *relplt = NULL;
3542 arelent *p;
3543
3544 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3545 {
3546 free_contents_and_exit_err:
3547 count = -1;
3548 free_contents_and_exit:
3549 if (contents)
3550 free (contents);
3551 goto done;
3552 }
3553
3554 size = 0;
3555 for (i = secsymend; i < opdsymend; ++i)
3556 {
3557 bfd_vma ent;
3558
3559 /* Ignore bogus symbols. */
3560 if (syms[i]->value > opd->size - 8)
3561 continue;
3562
3563 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3564 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3565 {
3566 ++count;
3567 size += sizeof (asymbol);
3568 size += strlen (syms[i]->name) + 2;
3569 }
3570 }
3571
3572 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3573 if (dyn_count != 0
3574 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3575 {
3576 bfd_byte *dynbuf, *extdyn, *extdynend;
3577 size_t extdynsize;
3578 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3579
3580 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3581 goto free_contents_and_exit_err;
3582
3583 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3584 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3585
3586 extdyn = dynbuf;
3587 extdynend = extdyn + dynamic->size;
3588 for (; extdyn < extdynend; extdyn += extdynsize)
3589 {
3590 Elf_Internal_Dyn dyn;
3591 (*swap_dyn_in) (abfd, extdyn, &dyn);
3592
3593 if (dyn.d_tag == DT_NULL)
3594 break;
3595
3596 if (dyn.d_tag == DT_PPC64_GLINK)
3597 {
3598 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3599 See comment in ppc64_elf_finish_dynamic_sections. */
3600 glink_vma = dyn.d_un.d_val + 8 * 4;
3601 /* The .glink section usually does not survive the final
3602 link; search for the section (usually .text) where the
3603 glink stubs now reside. */
3604 glink = bfd_sections_find_if (abfd, section_covers_vma,
3605 &glink_vma);
3606 break;
3607 }
3608 }
3609
3610 free (dynbuf);
3611 }
3612
3613 if (glink != NULL)
3614 {
3615 /* Determine __glink trampoline by reading the relative branch
3616 from the first glink stub. */
3617 bfd_byte buf[4];
3618 unsigned int off = 0;
3619
3620 while (bfd_get_section_contents (abfd, glink, buf,
3621 glink_vma + off - glink->vma, 4))
3622 {
3623 unsigned int insn = bfd_get_32 (abfd, buf);
3624 insn ^= B_DOT;
3625 if ((insn & ~0x3fffffc) == 0)
3626 {
3627 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3628 break;
3629 }
3630 off += 4;
3631 if (off > 4)
3632 break;
3633 }
3634
3635 if (resolv_vma)
3636 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3637
3638 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3639 if (relplt != NULL)
3640 {
3641 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3642 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3643 goto free_contents_and_exit_err;
3644
3645 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3646 size += plt_count * sizeof (asymbol);
3647
3648 p = relplt->relocation;
3649 for (i = 0; i < plt_count; i++, p++)
3650 {
3651 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3652 if (p->addend != 0)
3653 size += sizeof ("+0x") - 1 + 16;
3654 }
3655 }
3656 }
3657
3658 if (size == 0)
3659 goto free_contents_and_exit;
3660 s = *ret = bfd_malloc (size);
3661 if (s == NULL)
3662 goto free_contents_and_exit_err;
3663
3664 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3665
3666 for (i = secsymend; i < opdsymend; ++i)
3667 {
3668 bfd_vma ent;
3669
3670 if (syms[i]->value > opd->size - 8)
3671 continue;
3672
3673 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3674 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3675 {
3676 size_t lo, hi;
3677 size_t len;
3678 asection *sec = abfd->sections;
3679
3680 *s = *syms[i];
3681 lo = codesecsym;
3682 hi = codesecsymend;
3683 while (lo < hi)
3684 {
3685 size_t mid = (lo + hi) >> 1;
3686 if (syms[mid]->section->vma < ent)
3687 lo = mid + 1;
3688 else if (syms[mid]->section->vma > ent)
3689 hi = mid;
3690 else
3691 {
3692 sec = syms[mid]->section;
3693 break;
3694 }
3695 }
3696
3697 if (lo >= hi && lo > codesecsym)
3698 sec = syms[lo - 1]->section;
3699
3700 for (; sec != NULL; sec = sec->next)
3701 {
3702 if (sec->vma > ent)
3703 break;
3704 /* SEC_LOAD may not be set if SEC is from a separate debug
3705 info file. */
3706 if ((sec->flags & SEC_ALLOC) == 0)
3707 break;
3708 if ((sec->flags & SEC_CODE) != 0)
3709 s->section = sec;
3710 }
3711 s->flags |= BSF_SYNTHETIC;
3712 s->value = ent - s->section->vma;
3713 s->name = names;
3714 *names++ = '.';
3715 len = strlen (syms[i]->name);
3716 memcpy (names, syms[i]->name, len + 1);
3717 names += len + 1;
3718 /* Have udata.p point back to the original symbol this
3719 synthetic symbol was derived from. */
3720 s->udata.p = syms[i];
3721 s++;
3722 }
3723 }
3724 free (contents);
3725
3726 if (glink != NULL && relplt != NULL)
3727 {
3728 if (resolv_vma)
3729 {
3730 /* Add a symbol for the main glink trampoline. */
3731 memset (s, 0, sizeof *s);
3732 s->the_bfd = abfd;
3733 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3734 s->section = glink;
3735 s->value = resolv_vma - glink->vma;
3736 s->name = names;
3737 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3738 names += sizeof ("__glink_PLTresolve");
3739 s++;
3740 count++;
3741 }
3742
3743 /* FIXME: It would be very much nicer to put sym@plt on the
3744 stub rather than on the glink branch table entry. The
3745 objdump disassembler would then use a sensible symbol
3746 name on plt calls. The difficulty in doing so is
3747 a) finding the stubs, and,
3748 b) matching stubs against plt entries, and,
3749 c) there can be multiple stubs for a given plt entry.
3750
3751 Solving (a) could be done by code scanning, but older
3752 ppc64 binaries used different stubs to current code.
3753 (b) is the tricky one since you need to known the toc
3754 pointer for at least one function that uses a pic stub to
3755 be able to calculate the plt address referenced.
3756 (c) means gdb would need to set multiple breakpoints (or
3757 find the glink branch itself) when setting breakpoints
3758 for pending shared library loads. */
3759 p = relplt->relocation;
3760 for (i = 0; i < plt_count; i++, p++)
3761 {
3762 size_t len;
3763
3764 *s = **p->sym_ptr_ptr;
3765 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3766 we are defining a symbol, ensure one of them is set. */
3767 if ((s->flags & BSF_LOCAL) == 0)
3768 s->flags |= BSF_GLOBAL;
3769 s->flags |= BSF_SYNTHETIC;
3770 s->section = glink;
3771 s->value = glink_vma - glink->vma;
3772 s->name = names;
3773 s->udata.p = NULL;
3774 len = strlen ((*p->sym_ptr_ptr)->name);
3775 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3776 names += len;
3777 if (p->addend != 0)
3778 {
3779 memcpy (names, "+0x", sizeof ("+0x") - 1);
3780 names += sizeof ("+0x") - 1;
3781 bfd_sprintf_vma (abfd, names, p->addend);
3782 names += strlen (names);
3783 }
3784 memcpy (names, "@plt", sizeof ("@plt"));
3785 names += sizeof ("@plt");
3786 s++;
3787 if (abi < 2)
3788 {
3789 glink_vma += 8;
3790 if (i >= 0x8000)
3791 glink_vma += 4;
3792 }
3793 else
3794 glink_vma += 4;
3795 }
3796 count += plt_count;
3797 }
3798 }
3799
3800 done:
3801 free (syms);
3802 return count;
3803 }
3804 \f
3805 /* The following functions are specific to the ELF linker, while
3806 functions above are used generally. Those named ppc64_elf_* are
3807 called by the main ELF linker code. They appear in this file more
3808 or less in the order in which they are called. eg.
3809 ppc64_elf_check_relocs is called early in the link process,
3810 ppc64_elf_finish_dynamic_sections is one of the last functions
3811 called.
3812
3813 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3814 functions have both a function code symbol and a function descriptor
3815 symbol. A call to foo in a relocatable object file looks like:
3816
3817 . .text
3818 . x:
3819 . bl .foo
3820 . nop
3821
3822 The function definition in another object file might be:
3823
3824 . .section .opd
3825 . foo: .quad .foo
3826 . .quad .TOC.@tocbase
3827 . .quad 0
3828 .
3829 . .text
3830 . .foo: blr
3831
3832 When the linker resolves the call during a static link, the branch
3833 unsurprisingly just goes to .foo and the .opd information is unused.
3834 If the function definition is in a shared library, things are a little
3835 different: The call goes via a plt call stub, the opd information gets
3836 copied to the plt, and the linker patches the nop.
3837
3838 . x:
3839 . bl .foo_stub
3840 . ld 2,40(1)
3841 .
3842 .
3843 . .foo_stub:
3844 . std 2,40(1) # in practice, the call stub
3845 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3846 . addi 11,11,Lfoo@toc@l # this is the general idea
3847 . ld 12,0(11)
3848 . ld 2,8(11)
3849 . mtctr 12
3850 . ld 11,16(11)
3851 . bctr
3852 .
3853 . .section .plt
3854 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3855
3856 The "reloc ()" notation is supposed to indicate that the linker emits
3857 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3858 copying.
3859
3860 What are the difficulties here? Well, firstly, the relocations
3861 examined by the linker in check_relocs are against the function code
3862 sym .foo, while the dynamic relocation in the plt is emitted against
3863 the function descriptor symbol, foo. Somewhere along the line, we need
3864 to carefully copy dynamic link information from one symbol to the other.
3865 Secondly, the generic part of the elf linker will make .foo a dynamic
3866 symbol as is normal for most other backends. We need foo dynamic
3867 instead, at least for an application final link. However, when
3868 creating a shared library containing foo, we need to have both symbols
3869 dynamic so that references to .foo are satisfied during the early
3870 stages of linking. Otherwise the linker might decide to pull in a
3871 definition from some other object, eg. a static library.
3872
3873 Update: As of August 2004, we support a new convention. Function
3874 calls may use the function descriptor symbol, ie. "bl foo". This
3875 behaves exactly as "bl .foo". */
3876
3877 /* Of those relocs that might be copied as dynamic relocs, this
3878 function selects those that must be copied when linking a shared
3879 library or PIE, even when the symbol is local. */
3880
3881 static int
3882 must_be_dyn_reloc (struct bfd_link_info *info,
3883 enum elf_ppc64_reloc_type r_type)
3884 {
3885 switch (r_type)
3886 {
3887 default:
3888 /* Only relative relocs can be resolved when the object load
3889 address isn't fixed. DTPREL64 is excluded because the
3890 dynamic linker needs to differentiate global dynamic from
3891 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3892 return 1;
3893
3894 case R_PPC64_REL32:
3895 case R_PPC64_REL64:
3896 case R_PPC64_REL30:
3897 return 0;
3898
3899 case R_PPC64_TPREL16:
3900 case R_PPC64_TPREL16_LO:
3901 case R_PPC64_TPREL16_HI:
3902 case R_PPC64_TPREL16_HA:
3903 case R_PPC64_TPREL16_DS:
3904 case R_PPC64_TPREL16_LO_DS:
3905 case R_PPC64_TPREL16_HIGH:
3906 case R_PPC64_TPREL16_HIGHA:
3907 case R_PPC64_TPREL16_HIGHER:
3908 case R_PPC64_TPREL16_HIGHERA:
3909 case R_PPC64_TPREL16_HIGHEST:
3910 case R_PPC64_TPREL16_HIGHESTA:
3911 case R_PPC64_TPREL64:
3912 /* These relocations are relative but in a shared library the
3913 linker doesn't know the thread pointer base. */
3914 return bfd_link_dll (info);
3915 }
3916 }
3917
3918 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3919 copying dynamic variables from a shared lib into an app's dynbss
3920 section, and instead use a dynamic relocation to point into the
3921 shared lib. With code that gcc generates, it's vital that this be
3922 enabled; In the PowerPC64 ABI, the address of a function is actually
3923 the address of a function descriptor, which resides in the .opd
3924 section. gcc uses the descriptor directly rather than going via the
3925 GOT as some other ABI's do, which means that initialized function
3926 pointers must reference the descriptor. Thus, a function pointer
3927 initialized to the address of a function in a shared library will
3928 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3929 redefines the function descriptor symbol to point to the copy. This
3930 presents a problem as a plt entry for that function is also
3931 initialized from the function descriptor symbol and the copy reloc
3932 may not be initialized first. */
3933 #define ELIMINATE_COPY_RELOCS 1
3934
3935 /* Section name for stubs is the associated section name plus this
3936 string. */
3937 #define STUB_SUFFIX ".stub"
3938
3939 /* Linker stubs.
3940 ppc_stub_long_branch:
3941 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3942 destination, but a 24 bit branch in a stub section will reach.
3943 . b dest
3944
3945 ppc_stub_plt_branch:
3946 Similar to the above, but a 24 bit branch in the stub section won't
3947 reach its destination.
3948 . addis %r11,%r2,xxx@toc@ha
3949 . ld %r12,xxx@toc@l(%r11)
3950 . mtctr %r12
3951 . bctr
3952
3953 ppc_stub_plt_call:
3954 Used to call a function in a shared library. If it so happens that
3955 the plt entry referenced crosses a 64k boundary, then an extra
3956 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3957 . std %r2,40(%r1)
3958 . addis %r11,%r2,xxx@toc@ha
3959 . ld %r12,xxx+0@toc@l(%r11)
3960 . mtctr %r12
3961 . ld %r2,xxx+8@toc@l(%r11)
3962 . ld %r11,xxx+16@toc@l(%r11)
3963 . bctr
3964
3965 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3966 code to adjust the value and save r2 to support multiple toc sections.
3967 A ppc_stub_long_branch with an r2 offset looks like:
3968 . std %r2,40(%r1)
3969 . addis %r2,%r2,off@ha
3970 . addi %r2,%r2,off@l
3971 . b dest
3972
3973 A ppc_stub_plt_branch with an r2 offset looks like:
3974 . std %r2,40(%r1)
3975 . addis %r11,%r2,xxx@toc@ha
3976 . ld %r12,xxx@toc@l(%r11)
3977 . addis %r2,%r2,off@ha
3978 . addi %r2,%r2,off@l
3979 . mtctr %r12
3980 . bctr
3981
3982 In cases where the "addis" instruction would add zero, the "addis" is
3983 omitted and following instructions modified slightly in some cases.
3984 */
3985
3986 enum ppc_stub_type {
3987 ppc_stub_none,
3988 ppc_stub_long_branch,
3989 ppc_stub_long_branch_r2off,
3990 ppc_stub_plt_branch,
3991 ppc_stub_plt_branch_r2off,
3992 ppc_stub_plt_call,
3993 ppc_stub_plt_call_r2save,
3994 ppc_stub_global_entry,
3995 ppc_stub_save_res
3996 };
3997
3998 /* Information on stub grouping. */
3999 struct map_stub
4000 {
4001 /* The stub section. */
4002 asection *stub_sec;
4003 /* This is the section to which stubs in the group will be attached. */
4004 asection *link_sec;
4005 /* Next group. */
4006 struct map_stub *next;
4007 /* Whether to emit a copy of register save/restore functions in this
4008 group. */
4009 int needs_save_res;
4010 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4011 or -1u if no such stub with bctrl exists. */
4012 unsigned int tls_get_addr_opt_bctrl;
4013 };
4014
4015 struct ppc_stub_hash_entry {
4016
4017 /* Base hash table entry structure. */
4018 struct bfd_hash_entry root;
4019
4020 enum ppc_stub_type stub_type;
4021
4022 /* Group information. */
4023 struct map_stub *group;
4024
4025 /* Offset within stub_sec of the beginning of this stub. */
4026 bfd_vma stub_offset;
4027
4028 /* Given the symbol's value and its section we can determine its final
4029 value when building the stubs (so the stub knows where to jump. */
4030 bfd_vma target_value;
4031 asection *target_section;
4032
4033 /* The symbol table entry, if any, that this was derived from. */
4034 struct ppc_link_hash_entry *h;
4035 struct plt_entry *plt_ent;
4036
4037 /* Symbol type. */
4038 unsigned char symtype;
4039
4040 /* Symbol st_other. */
4041 unsigned char other;
4042 };
4043
4044 struct ppc_branch_hash_entry {
4045
4046 /* Base hash table entry structure. */
4047 struct bfd_hash_entry root;
4048
4049 /* Offset within branch lookup table. */
4050 unsigned int offset;
4051
4052 /* Generation marker. */
4053 unsigned int iter;
4054 };
4055
4056 /* Used to track dynamic relocations for local symbols. */
4057 struct ppc_dyn_relocs
4058 {
4059 struct ppc_dyn_relocs *next;
4060
4061 /* The input section of the reloc. */
4062 asection *sec;
4063
4064 /* Total number of relocs copied for the input section. */
4065 unsigned int count : 31;
4066
4067 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4068 unsigned int ifunc : 1;
4069 };
4070
4071 struct ppc_link_hash_entry
4072 {
4073 struct elf_link_hash_entry elf;
4074
4075 union {
4076 /* A pointer to the most recently used stub hash entry against this
4077 symbol. */
4078 struct ppc_stub_hash_entry *stub_cache;
4079
4080 /* A pointer to the next symbol starting with a '.' */
4081 struct ppc_link_hash_entry *next_dot_sym;
4082 } u;
4083
4084 /* Track dynamic relocs copied for this symbol. */
4085 struct elf_dyn_relocs *dyn_relocs;
4086
4087 /* Link between function code and descriptor symbols. */
4088 struct ppc_link_hash_entry *oh;
4089
4090 /* Flag function code and descriptor symbols. */
4091 unsigned int is_func:1;
4092 unsigned int is_func_descriptor:1;
4093 unsigned int fake:1;
4094
4095 /* Whether global opd/toc sym has been adjusted or not.
4096 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4097 should be set for all globals defined in any opd/toc section. */
4098 unsigned int adjust_done:1;
4099
4100 /* Set if this is an out-of-line register save/restore function,
4101 with non-standard calling convention. */
4102 unsigned int save_res:1;
4103
4104 /* Set if a duplicate symbol with non-zero localentry is detected,
4105 even when the duplicate symbol does not provide a definition. */
4106 unsigned int non_zero_localentry:1;
4107
4108 /* Contexts in which symbol is used in the GOT (or TOC).
4109 Bits are or'd into the mask as the corresponding relocs are
4110 encountered during check_relocs, with TLS_TLS being set when any
4111 of the other TLS bits are set. tls_optimize clears bits when
4112 optimizing to indicate the corresponding GOT entry type is not
4113 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4114 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4115 separate flag rather than setting TPREL just for convenience in
4116 distinguishing the two cases.
4117 These flags are also kept for local symbols. */
4118 #define TLS_TLS 1 /* Any TLS reloc. */
4119 #define TLS_GD 2 /* GD reloc. */
4120 #define TLS_LD 4 /* LD reloc. */
4121 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4122 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4123 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4124 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4125 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4126 unsigned char tls_mask;
4127
4128 /* The above field is also used to mark function symbols. In which
4129 case TLS_TLS will be 0. */
4130 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4131 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4132 #define NON_GOT 256 /* local symbol plt, not stored. */
4133 };
4134
4135 /* ppc64 ELF linker hash table. */
4136
4137 struct ppc_link_hash_table
4138 {
4139 struct elf_link_hash_table elf;
4140
4141 /* The stub hash table. */
4142 struct bfd_hash_table stub_hash_table;
4143
4144 /* Another hash table for plt_branch stubs. */
4145 struct bfd_hash_table branch_hash_table;
4146
4147 /* Hash table for function prologue tocsave. */
4148 htab_t tocsave_htab;
4149
4150 /* Various options and other info passed from the linker. */
4151 struct ppc64_elf_params *params;
4152
4153 /* The size of sec_info below. */
4154 unsigned int sec_info_arr_size;
4155
4156 /* Per-section array of extra section info. Done this way rather
4157 than as part of ppc64_elf_section_data so we have the info for
4158 non-ppc64 sections. */
4159 struct
4160 {
4161 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4162 bfd_vma toc_off;
4163
4164 union
4165 {
4166 /* The section group that this section belongs to. */
4167 struct map_stub *group;
4168 /* A temp section list pointer. */
4169 asection *list;
4170 } u;
4171 } *sec_info;
4172
4173 /* Linked list of groups. */
4174 struct map_stub *group;
4175
4176 /* Temp used when calculating TOC pointers. */
4177 bfd_vma toc_curr;
4178 bfd *toc_bfd;
4179 asection *toc_first_sec;
4180
4181 /* Used when adding symbols. */
4182 struct ppc_link_hash_entry *dot_syms;
4183
4184 /* Shortcuts to get to dynamic linker sections. */
4185 asection *glink;
4186 asection *global_entry;
4187 asection *sfpr;
4188 asection *pltlocal;
4189 asection *relpltlocal;
4190 asection *brlt;
4191 asection *relbrlt;
4192 asection *glink_eh_frame;
4193
4194 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4195 struct ppc_link_hash_entry *tls_get_addr;
4196 struct ppc_link_hash_entry *tls_get_addr_fd;
4197
4198 /* The size of reliplt used by got entry relocs. */
4199 bfd_size_type got_reli_size;
4200
4201 /* Statistics. */
4202 unsigned long stub_count[ppc_stub_global_entry];
4203
4204 /* Number of stubs against global syms. */
4205 unsigned long stub_globals;
4206
4207 /* Set if we're linking code with function descriptors. */
4208 unsigned int opd_abi:1;
4209
4210 /* Support for multiple toc sections. */
4211 unsigned int do_multi_toc:1;
4212 unsigned int multi_toc_needed:1;
4213 unsigned int second_toc_pass:1;
4214 unsigned int do_toc_opt:1;
4215
4216 /* Set if tls optimization is enabled. */
4217 unsigned int do_tls_opt:1;
4218
4219 /* Set if inline plt calls should be converted to direct calls. */
4220 unsigned int can_convert_all_inline_plt:1;
4221
4222 /* Set on error. */
4223 unsigned int stub_error:1;
4224
4225 /* Whether func_desc_adjust needs to be run over symbols. */
4226 unsigned int need_func_desc_adj:1;
4227
4228 /* Whether there exist local gnu indirect function resolvers,
4229 referenced by dynamic relocations. */
4230 unsigned int local_ifunc_resolver:1;
4231 unsigned int maybe_local_ifunc_resolver:1;
4232
4233 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4234 unsigned int has_plt_localentry0:1;
4235
4236 /* Incremented every time we size stubs. */
4237 unsigned int stub_iteration;
4238
4239 /* Small local sym cache. */
4240 struct sym_cache sym_cache;
4241 };
4242
4243 /* Rename some of the generic section flags to better document how they
4244 are used here. */
4245
4246 /* Nonzero if this section has TLS related relocations. */
4247 #define has_tls_reloc sec_flg0
4248
4249 /* Nonzero if this section has an old-style call to __tls_get_addr. */
4250 #define has_tls_get_addr_call sec_flg1
4251
4252 /* Nonzero if this section has any toc or got relocs. */
4253 #define has_toc_reloc sec_flg2
4254
4255 /* Nonzero if this section has a call to another section that uses
4256 the toc or got. */
4257 #define makes_toc_func_call sec_flg3
4258
4259 /* Recursion protection when determining above flag. */
4260 #define call_check_in_progress sec_flg4
4261 #define call_check_done sec_flg5
4262
4263 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4264
4265 #define ppc_hash_table(p) \
4266 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4267 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4268
4269 #define ppc_stub_hash_lookup(table, string, create, copy) \
4270 ((struct ppc_stub_hash_entry *) \
4271 bfd_hash_lookup ((table), (string), (create), (copy)))
4272
4273 #define ppc_branch_hash_lookup(table, string, create, copy) \
4274 ((struct ppc_branch_hash_entry *) \
4275 bfd_hash_lookup ((table), (string), (create), (copy)))
4276
4277 /* Create an entry in the stub hash table. */
4278
4279 static struct bfd_hash_entry *
4280 stub_hash_newfunc (struct bfd_hash_entry *entry,
4281 struct bfd_hash_table *table,
4282 const char *string)
4283 {
4284 /* Allocate the structure if it has not already been allocated by a
4285 subclass. */
4286 if (entry == NULL)
4287 {
4288 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4289 if (entry == NULL)
4290 return entry;
4291 }
4292
4293 /* Call the allocation method of the superclass. */
4294 entry = bfd_hash_newfunc (entry, table, string);
4295 if (entry != NULL)
4296 {
4297 struct ppc_stub_hash_entry *eh;
4298
4299 /* Initialize the local fields. */
4300 eh = (struct ppc_stub_hash_entry *) entry;
4301 eh->stub_type = ppc_stub_none;
4302 eh->group = NULL;
4303 eh->stub_offset = 0;
4304 eh->target_value = 0;
4305 eh->target_section = NULL;
4306 eh->h = NULL;
4307 eh->plt_ent = NULL;
4308 eh->other = 0;
4309 }
4310
4311 return entry;
4312 }
4313
4314 /* Create an entry in the branch hash table. */
4315
4316 static struct bfd_hash_entry *
4317 branch_hash_newfunc (struct bfd_hash_entry *entry,
4318 struct bfd_hash_table *table,
4319 const char *string)
4320 {
4321 /* Allocate the structure if it has not already been allocated by a
4322 subclass. */
4323 if (entry == NULL)
4324 {
4325 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4326 if (entry == NULL)
4327 return entry;
4328 }
4329
4330 /* Call the allocation method of the superclass. */
4331 entry = bfd_hash_newfunc (entry, table, string);
4332 if (entry != NULL)
4333 {
4334 struct ppc_branch_hash_entry *eh;
4335
4336 /* Initialize the local fields. */
4337 eh = (struct ppc_branch_hash_entry *) entry;
4338 eh->offset = 0;
4339 eh->iter = 0;
4340 }
4341
4342 return entry;
4343 }
4344
4345 /* Create an entry in a ppc64 ELF linker hash table. */
4346
4347 static struct bfd_hash_entry *
4348 link_hash_newfunc (struct bfd_hash_entry *entry,
4349 struct bfd_hash_table *table,
4350 const char *string)
4351 {
4352 /* Allocate the structure if it has not already been allocated by a
4353 subclass. */
4354 if (entry == NULL)
4355 {
4356 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4357 if (entry == NULL)
4358 return entry;
4359 }
4360
4361 /* Call the allocation method of the superclass. */
4362 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4363 if (entry != NULL)
4364 {
4365 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4366
4367 memset (&eh->u.stub_cache, 0,
4368 (sizeof (struct ppc_link_hash_entry)
4369 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4370
4371 /* When making function calls, old ABI code references function entry
4372 points (dot symbols), while new ABI code references the function
4373 descriptor symbol. We need to make any combination of reference and
4374 definition work together, without breaking archive linking.
4375
4376 For a defined function "foo" and an undefined call to "bar":
4377 An old object defines "foo" and ".foo", references ".bar" (possibly
4378 "bar" too).
4379 A new object defines "foo" and references "bar".
4380
4381 A new object thus has no problem with its undefined symbols being
4382 satisfied by definitions in an old object. On the other hand, the
4383 old object won't have ".bar" satisfied by a new object.
4384
4385 Keep a list of newly added dot-symbols. */
4386
4387 if (string[0] == '.')
4388 {
4389 struct ppc_link_hash_table *htab;
4390
4391 htab = (struct ppc_link_hash_table *) table;
4392 eh->u.next_dot_sym = htab->dot_syms;
4393 htab->dot_syms = eh;
4394 }
4395 }
4396
4397 return entry;
4398 }
4399
4400 struct tocsave_entry {
4401 asection *sec;
4402 bfd_vma offset;
4403 };
4404
4405 static hashval_t
4406 tocsave_htab_hash (const void *p)
4407 {
4408 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4409 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4410 }
4411
4412 static int
4413 tocsave_htab_eq (const void *p1, const void *p2)
4414 {
4415 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4416 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4417 return e1->sec == e2->sec && e1->offset == e2->offset;
4418 }
4419
4420 /* Destroy a ppc64 ELF linker hash table. */
4421
4422 static void
4423 ppc64_elf_link_hash_table_free (bfd *obfd)
4424 {
4425 struct ppc_link_hash_table *htab;
4426
4427 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4428 if (htab->tocsave_htab)
4429 htab_delete (htab->tocsave_htab);
4430 bfd_hash_table_free (&htab->branch_hash_table);
4431 bfd_hash_table_free (&htab->stub_hash_table);
4432 _bfd_elf_link_hash_table_free (obfd);
4433 }
4434
4435 /* Create a ppc64 ELF linker hash table. */
4436
4437 static struct bfd_link_hash_table *
4438 ppc64_elf_link_hash_table_create (bfd *abfd)
4439 {
4440 struct ppc_link_hash_table *htab;
4441 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4442
4443 htab = bfd_zmalloc (amt);
4444 if (htab == NULL)
4445 return NULL;
4446
4447 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4448 sizeof (struct ppc_link_hash_entry),
4449 PPC64_ELF_DATA))
4450 {
4451 free (htab);
4452 return NULL;
4453 }
4454
4455 /* Init the stub hash table too. */
4456 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4457 sizeof (struct ppc_stub_hash_entry)))
4458 {
4459 _bfd_elf_link_hash_table_free (abfd);
4460 return NULL;
4461 }
4462
4463 /* And the branch hash table. */
4464 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4465 sizeof (struct ppc_branch_hash_entry)))
4466 {
4467 bfd_hash_table_free (&htab->stub_hash_table);
4468 _bfd_elf_link_hash_table_free (abfd);
4469 return NULL;
4470 }
4471
4472 htab->tocsave_htab = htab_try_create (1024,
4473 tocsave_htab_hash,
4474 tocsave_htab_eq,
4475 NULL);
4476 if (htab->tocsave_htab == NULL)
4477 {
4478 ppc64_elf_link_hash_table_free (abfd);
4479 return NULL;
4480 }
4481 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4482
4483 /* Initializing two fields of the union is just cosmetic. We really
4484 only care about glist, but when compiled on a 32-bit host the
4485 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4486 debugger inspection of these fields look nicer. */
4487 htab->elf.init_got_refcount.refcount = 0;
4488 htab->elf.init_got_refcount.glist = NULL;
4489 htab->elf.init_plt_refcount.refcount = 0;
4490 htab->elf.init_plt_refcount.glist = NULL;
4491 htab->elf.init_got_offset.offset = 0;
4492 htab->elf.init_got_offset.glist = NULL;
4493 htab->elf.init_plt_offset.offset = 0;
4494 htab->elf.init_plt_offset.glist = NULL;
4495
4496 return &htab->elf.root;
4497 }
4498
4499 /* Create sections for linker generated code. */
4500
4501 static bfd_boolean
4502 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4503 {
4504 struct ppc_link_hash_table *htab;
4505 flagword flags;
4506
4507 htab = ppc_hash_table (info);
4508
4509 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4510 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4511 if (htab->params->save_restore_funcs)
4512 {
4513 /* Create .sfpr for code to save and restore fp regs. */
4514 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4515 flags);
4516 if (htab->sfpr == NULL
4517 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4518 return FALSE;
4519 }
4520
4521 if (bfd_link_relocatable (info))
4522 return TRUE;
4523
4524 /* Create .glink for lazy dynamic linking support. */
4525 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4526 flags);
4527 if (htab->glink == NULL
4528 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4529 return FALSE;
4530
4531 /* The part of .glink used by global entry stubs, separate so that
4532 it can be aligned appropriately without affecting htab->glink. */
4533 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4534 flags);
4535 if (htab->global_entry == NULL
4536 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4537 return FALSE;
4538
4539 if (!info->no_ld_generated_unwind_info)
4540 {
4541 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4542 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4543 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4544 ".eh_frame",
4545 flags);
4546 if (htab->glink_eh_frame == NULL
4547 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4548 return FALSE;
4549 }
4550
4551 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4552 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4553 if (htab->elf.iplt == NULL
4554 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4555 return FALSE;
4556
4557 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4558 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4559 htab->elf.irelplt
4560 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4561 if (htab->elf.irelplt == NULL
4562 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4563 return FALSE;
4564
4565 /* Create branch lookup table for plt_branch stubs. */
4566 flags = (SEC_ALLOC | SEC_LOAD
4567 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4568 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4569 flags);
4570 if (htab->brlt == NULL
4571 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4572 return FALSE;
4573
4574 /* Local plt entries, put in .branch_lt but a separate section for
4575 convenience. */
4576 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4577 flags);
4578 if (htab->pltlocal == NULL
4579 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4580 return FALSE;
4581
4582 if (!bfd_link_pic (info))
4583 return TRUE;
4584
4585 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4586 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4587 htab->relbrlt
4588 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4589 if (htab->relbrlt == NULL
4590 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4591 return FALSE;
4592
4593 htab->relpltlocal
4594 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4595 if (htab->relpltlocal == NULL
4596 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4597 return FALSE;
4598
4599 return TRUE;
4600 }
4601
4602 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4603
4604 bfd_boolean
4605 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4606 struct ppc64_elf_params *params)
4607 {
4608 struct ppc_link_hash_table *htab;
4609
4610 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4611
4612 /* Always hook our dynamic sections into the first bfd, which is the
4613 linker created stub bfd. This ensures that the GOT header is at
4614 the start of the output TOC section. */
4615 htab = ppc_hash_table (info);
4616 htab->elf.dynobj = params->stub_bfd;
4617 htab->params = params;
4618
4619 return create_linkage_sections (htab->elf.dynobj, info);
4620 }
4621
4622 /* Build a name for an entry in the stub hash table. */
4623
4624 static char *
4625 ppc_stub_name (const asection *input_section,
4626 const asection *sym_sec,
4627 const struct ppc_link_hash_entry *h,
4628 const Elf_Internal_Rela *rel)
4629 {
4630 char *stub_name;
4631 ssize_t len;
4632
4633 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4634 offsets from a sym as a branch target? In fact, we could
4635 probably assume the addend is always zero. */
4636 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4637
4638 if (h)
4639 {
4640 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4641 stub_name = bfd_malloc (len);
4642 if (stub_name == NULL)
4643 return stub_name;
4644
4645 len = sprintf (stub_name, "%08x.%s+%x",
4646 input_section->id & 0xffffffff,
4647 h->elf.root.root.string,
4648 (int) rel->r_addend & 0xffffffff);
4649 }
4650 else
4651 {
4652 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4653 stub_name = bfd_malloc (len);
4654 if (stub_name == NULL)
4655 return stub_name;
4656
4657 len = sprintf (stub_name, "%08x.%x:%x+%x",
4658 input_section->id & 0xffffffff,
4659 sym_sec->id & 0xffffffff,
4660 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4661 (int) rel->r_addend & 0xffffffff);
4662 }
4663 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4664 stub_name[len - 2] = 0;
4665 return stub_name;
4666 }
4667
4668 /* Look up an entry in the stub hash. Stub entries are cached because
4669 creating the stub name takes a bit of time. */
4670
4671 static struct ppc_stub_hash_entry *
4672 ppc_get_stub_entry (const asection *input_section,
4673 const asection *sym_sec,
4674 struct ppc_link_hash_entry *h,
4675 const Elf_Internal_Rela *rel,
4676 struct ppc_link_hash_table *htab)
4677 {
4678 struct ppc_stub_hash_entry *stub_entry;
4679 struct map_stub *group;
4680
4681 /* If this input section is part of a group of sections sharing one
4682 stub section, then use the id of the first section in the group.
4683 Stub names need to include a section id, as there may well be
4684 more than one stub used to reach say, printf, and we need to
4685 distinguish between them. */
4686 group = htab->sec_info[input_section->id].u.group;
4687 if (group == NULL)
4688 return NULL;
4689
4690 if (h != NULL && h->u.stub_cache != NULL
4691 && h->u.stub_cache->h == h
4692 && h->u.stub_cache->group == group)
4693 {
4694 stub_entry = h->u.stub_cache;
4695 }
4696 else
4697 {
4698 char *stub_name;
4699
4700 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4701 if (stub_name == NULL)
4702 return NULL;
4703
4704 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4705 stub_name, FALSE, FALSE);
4706 if (h != NULL)
4707 h->u.stub_cache = stub_entry;
4708
4709 free (stub_name);
4710 }
4711
4712 return stub_entry;
4713 }
4714
4715 /* Add a new stub entry to the stub hash. Not all fields of the new
4716 stub entry are initialised. */
4717
4718 static struct ppc_stub_hash_entry *
4719 ppc_add_stub (const char *stub_name,
4720 asection *section,
4721 struct bfd_link_info *info)
4722 {
4723 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4724 struct map_stub *group;
4725 asection *link_sec;
4726 asection *stub_sec;
4727 struct ppc_stub_hash_entry *stub_entry;
4728
4729 group = htab->sec_info[section->id].u.group;
4730 link_sec = group->link_sec;
4731 stub_sec = group->stub_sec;
4732 if (stub_sec == NULL)
4733 {
4734 size_t namelen;
4735 bfd_size_type len;
4736 char *s_name;
4737
4738 namelen = strlen (link_sec->name);
4739 len = namelen + sizeof (STUB_SUFFIX);
4740 s_name = bfd_alloc (htab->params->stub_bfd, len);
4741 if (s_name == NULL)
4742 return NULL;
4743
4744 memcpy (s_name, link_sec->name, namelen);
4745 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4746 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4747 if (stub_sec == NULL)
4748 return NULL;
4749 group->stub_sec = stub_sec;
4750 }
4751
4752 /* Enter this entry into the linker stub hash table. */
4753 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4754 TRUE, FALSE);
4755 if (stub_entry == NULL)
4756 {
4757 /* xgettext:c-format */
4758 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4759 section->owner, stub_name);
4760 return NULL;
4761 }
4762
4763 stub_entry->group = group;
4764 stub_entry->stub_offset = 0;
4765 return stub_entry;
4766 }
4767
4768 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4769 not already done. */
4770
4771 static bfd_boolean
4772 create_got_section (bfd *abfd, struct bfd_link_info *info)
4773 {
4774 asection *got, *relgot;
4775 flagword flags;
4776 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4777
4778 if (!is_ppc64_elf (abfd))
4779 return FALSE;
4780 if (htab == NULL)
4781 return FALSE;
4782
4783 if (!htab->elf.sgot
4784 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4785 return FALSE;
4786
4787 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4788 | SEC_LINKER_CREATED);
4789
4790 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4791 if (!got
4792 || !bfd_set_section_alignment (abfd, got, 3))
4793 return FALSE;
4794
4795 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4796 flags | SEC_READONLY);
4797 if (!relgot
4798 || ! bfd_set_section_alignment (abfd, relgot, 3))
4799 return FALSE;
4800
4801 ppc64_elf_tdata (abfd)->got = got;
4802 ppc64_elf_tdata (abfd)->relgot = relgot;
4803 return TRUE;
4804 }
4805
4806 /* Follow indirect and warning symbol links. */
4807
4808 static inline struct bfd_link_hash_entry *
4809 follow_link (struct bfd_link_hash_entry *h)
4810 {
4811 while (h->type == bfd_link_hash_indirect
4812 || h->type == bfd_link_hash_warning)
4813 h = h->u.i.link;
4814 return h;
4815 }
4816
4817 static inline struct elf_link_hash_entry *
4818 elf_follow_link (struct elf_link_hash_entry *h)
4819 {
4820 return (struct elf_link_hash_entry *) follow_link (&h->root);
4821 }
4822
4823 static inline struct ppc_link_hash_entry *
4824 ppc_follow_link (struct ppc_link_hash_entry *h)
4825 {
4826 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4827 }
4828
4829 /* Merge PLT info on FROM with that on TO. */
4830
4831 static void
4832 move_plt_plist (struct ppc_link_hash_entry *from,
4833 struct ppc_link_hash_entry *to)
4834 {
4835 if (from->elf.plt.plist != NULL)
4836 {
4837 if (to->elf.plt.plist != NULL)
4838 {
4839 struct plt_entry **entp;
4840 struct plt_entry *ent;
4841
4842 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4843 {
4844 struct plt_entry *dent;
4845
4846 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4847 if (dent->addend == ent->addend)
4848 {
4849 dent->plt.refcount += ent->plt.refcount;
4850 *entp = ent->next;
4851 break;
4852 }
4853 if (dent == NULL)
4854 entp = &ent->next;
4855 }
4856 *entp = to->elf.plt.plist;
4857 }
4858
4859 to->elf.plt.plist = from->elf.plt.plist;
4860 from->elf.plt.plist = NULL;
4861 }
4862 }
4863
4864 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4865
4866 static void
4867 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4868 struct elf_link_hash_entry *dir,
4869 struct elf_link_hash_entry *ind)
4870 {
4871 struct ppc_link_hash_entry *edir, *eind;
4872
4873 edir = (struct ppc_link_hash_entry *) dir;
4874 eind = (struct ppc_link_hash_entry *) ind;
4875
4876 edir->is_func |= eind->is_func;
4877 edir->is_func_descriptor |= eind->is_func_descriptor;
4878 edir->tls_mask |= eind->tls_mask;
4879 if (eind->oh != NULL)
4880 edir->oh = ppc_follow_link (eind->oh);
4881
4882 if (edir->elf.versioned != versioned_hidden)
4883 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4884 edir->elf.ref_regular |= eind->elf.ref_regular;
4885 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4886 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4887 edir->elf.needs_plt |= eind->elf.needs_plt;
4888 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4889
4890 /* If we were called to copy over info for a weak sym, don't copy
4891 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4892 in order to simplify readonly_dynrelocs and save a field in the
4893 symbol hash entry, but that means dyn_relocs can't be used in any
4894 tests about a specific symbol, or affect other symbol flags which
4895 are then tested. */
4896 if (eind->elf.root.type != bfd_link_hash_indirect)
4897 return;
4898
4899 /* Copy over any dynamic relocs we may have on the indirect sym. */
4900 if (eind->dyn_relocs != NULL)
4901 {
4902 if (edir->dyn_relocs != NULL)
4903 {
4904 struct elf_dyn_relocs **pp;
4905 struct elf_dyn_relocs *p;
4906
4907 /* Add reloc counts against the indirect sym to the direct sym
4908 list. Merge any entries against the same section. */
4909 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4910 {
4911 struct elf_dyn_relocs *q;
4912
4913 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4914 if (q->sec == p->sec)
4915 {
4916 q->pc_count += p->pc_count;
4917 q->count += p->count;
4918 *pp = p->next;
4919 break;
4920 }
4921 if (q == NULL)
4922 pp = &p->next;
4923 }
4924 *pp = edir->dyn_relocs;
4925 }
4926
4927 edir->dyn_relocs = eind->dyn_relocs;
4928 eind->dyn_relocs = NULL;
4929 }
4930
4931 /* Copy over got entries that we may have already seen to the
4932 symbol which just became indirect. */
4933 if (eind->elf.got.glist != NULL)
4934 {
4935 if (edir->elf.got.glist != NULL)
4936 {
4937 struct got_entry **entp;
4938 struct got_entry *ent;
4939
4940 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4941 {
4942 struct got_entry *dent;
4943
4944 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4945 if (dent->addend == ent->addend
4946 && dent->owner == ent->owner
4947 && dent->tls_type == ent->tls_type)
4948 {
4949 dent->got.refcount += ent->got.refcount;
4950 *entp = ent->next;
4951 break;
4952 }
4953 if (dent == NULL)
4954 entp = &ent->next;
4955 }
4956 *entp = edir->elf.got.glist;
4957 }
4958
4959 edir->elf.got.glist = eind->elf.got.glist;
4960 eind->elf.got.glist = NULL;
4961 }
4962
4963 /* And plt entries. */
4964 move_plt_plist (eind, edir);
4965
4966 if (eind->elf.dynindx != -1)
4967 {
4968 if (edir->elf.dynindx != -1)
4969 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4970 edir->elf.dynstr_index);
4971 edir->elf.dynindx = eind->elf.dynindx;
4972 edir->elf.dynstr_index = eind->elf.dynstr_index;
4973 eind->elf.dynindx = -1;
4974 eind->elf.dynstr_index = 0;
4975 }
4976 }
4977
4978 /* Find the function descriptor hash entry from the given function code
4979 hash entry FH. Link the entries via their OH fields. */
4980
4981 static struct ppc_link_hash_entry *
4982 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4983 {
4984 struct ppc_link_hash_entry *fdh = fh->oh;
4985
4986 if (fdh == NULL)
4987 {
4988 const char *fd_name = fh->elf.root.root.string + 1;
4989
4990 fdh = (struct ppc_link_hash_entry *)
4991 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4992 if (fdh == NULL)
4993 return fdh;
4994
4995 fdh->is_func_descriptor = 1;
4996 fdh->oh = fh;
4997 fh->is_func = 1;
4998 fh->oh = fdh;
4999 }
5000
5001 fdh = ppc_follow_link (fdh);
5002 fdh->is_func_descriptor = 1;
5003 fdh->oh = fh;
5004 return fdh;
5005 }
5006
5007 /* Make a fake function descriptor sym for the undefined code sym FH. */
5008
5009 static struct ppc_link_hash_entry *
5010 make_fdh (struct bfd_link_info *info,
5011 struct ppc_link_hash_entry *fh)
5012 {
5013 bfd *abfd = fh->elf.root.u.undef.abfd;
5014 struct bfd_link_hash_entry *bh = NULL;
5015 struct ppc_link_hash_entry *fdh;
5016 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5017 ? BSF_WEAK
5018 : BSF_GLOBAL);
5019
5020 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5021 fh->elf.root.root.string + 1,
5022 flags, bfd_und_section_ptr, 0,
5023 NULL, FALSE, FALSE, &bh))
5024 return NULL;
5025
5026 fdh = (struct ppc_link_hash_entry *) bh;
5027 fdh->elf.non_elf = 0;
5028 fdh->fake = 1;
5029 fdh->is_func_descriptor = 1;
5030 fdh->oh = fh;
5031 fh->is_func = 1;
5032 fh->oh = fdh;
5033 return fdh;
5034 }
5035
5036 /* Fix function descriptor symbols defined in .opd sections to be
5037 function type. */
5038
5039 static bfd_boolean
5040 ppc64_elf_add_symbol_hook (bfd *ibfd,
5041 struct bfd_link_info *info,
5042 Elf_Internal_Sym *isym,
5043 const char **name,
5044 flagword *flags ATTRIBUTE_UNUSED,
5045 asection **sec,
5046 bfd_vma *value)
5047 {
5048 if (*sec != NULL
5049 && strcmp ((*sec)->name, ".opd") == 0)
5050 {
5051 asection *code_sec;
5052
5053 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5054 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5055 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5056
5057 /* If the symbol is a function defined in .opd, and the function
5058 code is in a discarded group, let it appear to be undefined. */
5059 if (!bfd_link_relocatable (info)
5060 && (*sec)->reloc_count != 0
5061 && opd_entry_value (*sec, *value, &code_sec, NULL,
5062 FALSE) != (bfd_vma) -1
5063 && discarded_section (code_sec))
5064 {
5065 *sec = bfd_und_section_ptr;
5066 isym->st_shndx = SHN_UNDEF;
5067 }
5068 }
5069 else if (*sec != NULL
5070 && strcmp ((*sec)->name, ".toc") == 0
5071 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5072 {
5073 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5074 if (htab != NULL)
5075 htab->params->object_in_toc = 1;
5076 }
5077
5078 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5079 {
5080 if (abiversion (ibfd) == 0)
5081 set_abiversion (ibfd, 2);
5082 else if (abiversion (ibfd) == 1)
5083 {
5084 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5085 " for ABI version 1"), *name);
5086 bfd_set_error (bfd_error_bad_value);
5087 return FALSE;
5088 }
5089 }
5090
5091 return TRUE;
5092 }
5093
5094 /* Merge non-visibility st_other attributes: local entry point. */
5095
5096 static void
5097 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5098 const Elf_Internal_Sym *isym,
5099 bfd_boolean definition,
5100 bfd_boolean dynamic)
5101 {
5102 if (definition && (!dynamic || !h->def_regular))
5103 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5104 | ELF_ST_VISIBILITY (h->other));
5105 }
5106
5107 /* Hook called on merging a symbol. We use this to clear "fake" since
5108 we now have a real symbol. */
5109
5110 static bfd_boolean
5111 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5112 const Elf_Internal_Sym *isym,
5113 asection **psec ATTRIBUTE_UNUSED,
5114 bfd_boolean newdef ATTRIBUTE_UNUSED,
5115 bfd_boolean olddef ATTRIBUTE_UNUSED,
5116 bfd *oldbfd ATTRIBUTE_UNUSED,
5117 const asection *oldsec ATTRIBUTE_UNUSED)
5118 {
5119 ((struct ppc_link_hash_entry *) h)->fake = 0;
5120 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5121 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5122 return TRUE;
5123 }
5124
5125 /* This function makes an old ABI object reference to ".bar" cause the
5126 inclusion of a new ABI object archive that defines "bar".
5127 NAME is a symbol defined in an archive. Return a symbol in the hash
5128 table that might be satisfied by the archive symbols. */
5129
5130 static struct elf_link_hash_entry *
5131 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5132 struct bfd_link_info *info,
5133 const char *name)
5134 {
5135 struct elf_link_hash_entry *h;
5136 char *dot_name;
5137 size_t len;
5138
5139 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5140 if (h != NULL
5141 /* Don't return this sym if it is a fake function descriptor
5142 created by add_symbol_adjust. */
5143 && !((struct ppc_link_hash_entry *) h)->fake)
5144 return h;
5145
5146 if (name[0] == '.')
5147 return h;
5148
5149 len = strlen (name);
5150 dot_name = bfd_alloc (abfd, len + 2);
5151 if (dot_name == NULL)
5152 return (struct elf_link_hash_entry *) -1;
5153 dot_name[0] = '.';
5154 memcpy (dot_name + 1, name, len + 1);
5155 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5156 bfd_release (abfd, dot_name);
5157 return h;
5158 }
5159
5160 /* This function satisfies all old ABI object references to ".bar" if a
5161 new ABI object defines "bar". Well, at least, undefined dot symbols
5162 are made weak. This stops later archive searches from including an
5163 object if we already have a function descriptor definition. It also
5164 prevents the linker complaining about undefined symbols.
5165 We also check and correct mismatched symbol visibility here. The
5166 most restrictive visibility of the function descriptor and the
5167 function entry symbol is used. */
5168
5169 static bfd_boolean
5170 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5171 {
5172 struct ppc_link_hash_table *htab;
5173 struct ppc_link_hash_entry *fdh;
5174
5175 if (eh->elf.root.type == bfd_link_hash_warning)
5176 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5177
5178 if (eh->elf.root.type == bfd_link_hash_indirect)
5179 return TRUE;
5180
5181 if (eh->elf.root.root.string[0] != '.')
5182 abort ();
5183
5184 htab = ppc_hash_table (info);
5185 if (htab == NULL)
5186 return FALSE;
5187
5188 fdh = lookup_fdh (eh, htab);
5189 if (fdh == NULL
5190 && !bfd_link_relocatable (info)
5191 && (eh->elf.root.type == bfd_link_hash_undefined
5192 || eh->elf.root.type == bfd_link_hash_undefweak)
5193 && eh->elf.ref_regular)
5194 {
5195 /* Make an undefined function descriptor sym, in order to
5196 pull in an --as-needed shared lib. Archives are handled
5197 elsewhere. */
5198 fdh = make_fdh (info, eh);
5199 if (fdh == NULL)
5200 return FALSE;
5201 }
5202
5203 if (fdh != NULL)
5204 {
5205 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5206 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5207
5208 /* Make both descriptor and entry symbol have the most
5209 constraining visibility of either symbol. */
5210 if (entry_vis < descr_vis)
5211 fdh->elf.other += entry_vis - descr_vis;
5212 else if (entry_vis > descr_vis)
5213 eh->elf.other += descr_vis - entry_vis;
5214
5215 /* Propagate reference flags from entry symbol to function
5216 descriptor symbol. */
5217 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5218 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5219 fdh->elf.ref_regular |= eh->elf.ref_regular;
5220 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5221
5222 if (!fdh->elf.forced_local
5223 && fdh->elf.dynindx == -1
5224 && fdh->elf.versioned != versioned_hidden
5225 && (bfd_link_dll (info)
5226 || fdh->elf.def_dynamic
5227 || fdh->elf.ref_dynamic)
5228 && (eh->elf.ref_regular
5229 || eh->elf.def_regular))
5230 {
5231 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5232 return FALSE;
5233 }
5234 }
5235
5236 return TRUE;
5237 }
5238
5239 /* Set up opd section info and abiversion for IBFD, and process list
5240 of dot-symbols we made in link_hash_newfunc. */
5241
5242 static bfd_boolean
5243 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5244 {
5245 struct ppc_link_hash_table *htab;
5246 struct ppc_link_hash_entry **p, *eh;
5247 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5248
5249 if (opd != NULL && opd->size != 0)
5250 {
5251 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5252 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5253
5254 if (abiversion (ibfd) == 0)
5255 set_abiversion (ibfd, 1);
5256 else if (abiversion (ibfd) >= 2)
5257 {
5258 /* xgettext:c-format */
5259 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5260 ibfd, abiversion (ibfd));
5261 bfd_set_error (bfd_error_bad_value);
5262 return FALSE;
5263 }
5264 }
5265
5266 if (is_ppc64_elf (info->output_bfd))
5267 {
5268 /* For input files without an explicit abiversion in e_flags
5269 we should have flagged any with symbol st_other bits set
5270 as ELFv1 and above flagged those with .opd as ELFv2.
5271 Set the output abiversion if not yet set, and for any input
5272 still ambiguous, take its abiversion from the output.
5273 Differences in ABI are reported later. */
5274 if (abiversion (info->output_bfd) == 0)
5275 set_abiversion (info->output_bfd, abiversion (ibfd));
5276 else if (abiversion (ibfd) == 0)
5277 set_abiversion (ibfd, abiversion (info->output_bfd));
5278 }
5279
5280 htab = ppc_hash_table (info);
5281 if (htab == NULL)
5282 return TRUE;
5283
5284 if (opd != NULL && opd->size != 0
5285 && (ibfd->flags & DYNAMIC) == 0
5286 && (opd->flags & SEC_RELOC) != 0
5287 && opd->reloc_count != 0
5288 && !bfd_is_abs_section (opd->output_section)
5289 && info->gc_sections)
5290 {
5291 /* Garbage collection needs some extra help with .opd sections.
5292 We don't want to necessarily keep everything referenced by
5293 relocs in .opd, as that would keep all functions. Instead,
5294 if we reference an .opd symbol (a function descriptor), we
5295 want to keep the function code symbol's section. This is
5296 easy for global symbols, but for local syms we need to keep
5297 information about the associated function section. */
5298 bfd_size_type amt;
5299 asection **opd_sym_map;
5300 Elf_Internal_Shdr *symtab_hdr;
5301 Elf_Internal_Rela *relocs, *rel_end, *rel;
5302
5303 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5304 opd_sym_map = bfd_zalloc (ibfd, amt);
5305 if (opd_sym_map == NULL)
5306 return FALSE;
5307 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5308 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5309 info->keep_memory);
5310 if (relocs == NULL)
5311 return FALSE;
5312 symtab_hdr = &elf_symtab_hdr (ibfd);
5313 rel_end = relocs + opd->reloc_count - 1;
5314 for (rel = relocs; rel < rel_end; rel++)
5315 {
5316 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5317 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5318
5319 if (r_type == R_PPC64_ADDR64
5320 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5321 && r_symndx < symtab_hdr->sh_info)
5322 {
5323 Elf_Internal_Sym *isym;
5324 asection *s;
5325
5326 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5327 if (isym == NULL)
5328 {
5329 if (elf_section_data (opd)->relocs != relocs)
5330 free (relocs);
5331 return FALSE;
5332 }
5333
5334 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5335 if (s != NULL && s != opd)
5336 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5337 }
5338 }
5339 if (elf_section_data (opd)->relocs != relocs)
5340 free (relocs);
5341 }
5342
5343 p = &htab->dot_syms;
5344 while ((eh = *p) != NULL)
5345 {
5346 *p = NULL;
5347 if (&eh->elf == htab->elf.hgot)
5348 ;
5349 else if (htab->elf.hgot == NULL
5350 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5351 htab->elf.hgot = &eh->elf;
5352 else if (abiversion (ibfd) <= 1)
5353 {
5354 htab->need_func_desc_adj = 1;
5355 if (!add_symbol_adjust (eh, info))
5356 return FALSE;
5357 }
5358 p = &eh->u.next_dot_sym;
5359 }
5360 return TRUE;
5361 }
5362
5363 /* Undo hash table changes when an --as-needed input file is determined
5364 not to be needed. */
5365
5366 static bfd_boolean
5367 ppc64_elf_notice_as_needed (bfd *ibfd,
5368 struct bfd_link_info *info,
5369 enum notice_asneeded_action act)
5370 {
5371 if (act == notice_not_needed)
5372 {
5373 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5374
5375 if (htab == NULL)
5376 return FALSE;
5377
5378 htab->dot_syms = NULL;
5379 }
5380 return _bfd_elf_notice_as_needed (ibfd, info, act);
5381 }
5382
5383 /* If --just-symbols against a final linked binary, then assume we need
5384 toc adjusting stubs when calling functions defined there. */
5385
5386 static void
5387 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5388 {
5389 if ((sec->flags & SEC_CODE) != 0
5390 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5391 && is_ppc64_elf (sec->owner))
5392 {
5393 if (abiversion (sec->owner) >= 2
5394 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5395 sec->has_toc_reloc = 1;
5396 }
5397 _bfd_elf_link_just_syms (sec, info);
5398 }
5399
5400 static struct plt_entry **
5401 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5402 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5403 {
5404 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5405 struct plt_entry **local_plt;
5406 unsigned char *local_got_tls_masks;
5407
5408 if (local_got_ents == NULL)
5409 {
5410 bfd_size_type size = symtab_hdr->sh_info;
5411
5412 size *= (sizeof (*local_got_ents)
5413 + sizeof (*local_plt)
5414 + sizeof (*local_got_tls_masks));
5415 local_got_ents = bfd_zalloc (abfd, size);
5416 if (local_got_ents == NULL)
5417 return NULL;
5418 elf_local_got_ents (abfd) = local_got_ents;
5419 }
5420
5421 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5422 {
5423 struct got_entry *ent;
5424
5425 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5426 if (ent->addend == r_addend
5427 && ent->owner == abfd
5428 && ent->tls_type == tls_type)
5429 break;
5430 if (ent == NULL)
5431 {
5432 bfd_size_type amt = sizeof (*ent);
5433 ent = bfd_alloc (abfd, amt);
5434 if (ent == NULL)
5435 return FALSE;
5436 ent->next = local_got_ents[r_symndx];
5437 ent->addend = r_addend;
5438 ent->owner = abfd;
5439 ent->tls_type = tls_type;
5440 ent->is_indirect = FALSE;
5441 ent->got.refcount = 0;
5442 local_got_ents[r_symndx] = ent;
5443 }
5444 ent->got.refcount += 1;
5445 }
5446
5447 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5448 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5449 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5450
5451 return local_plt + r_symndx;
5452 }
5453
5454 static bfd_boolean
5455 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5456 {
5457 struct plt_entry *ent;
5458
5459 for (ent = *plist; ent != NULL; ent = ent->next)
5460 if (ent->addend == addend)
5461 break;
5462 if (ent == NULL)
5463 {
5464 bfd_size_type amt = sizeof (*ent);
5465 ent = bfd_alloc (abfd, amt);
5466 if (ent == NULL)
5467 return FALSE;
5468 ent->next = *plist;
5469 ent->addend = addend;
5470 ent->plt.refcount = 0;
5471 *plist = ent;
5472 }
5473 ent->plt.refcount += 1;
5474 return TRUE;
5475 }
5476
5477 static bfd_boolean
5478 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5479 {
5480 return (r_type == R_PPC64_REL24
5481 || r_type == R_PPC64_REL14
5482 || r_type == R_PPC64_REL14_BRTAKEN
5483 || r_type == R_PPC64_REL14_BRNTAKEN
5484 || r_type == R_PPC64_ADDR24
5485 || r_type == R_PPC64_ADDR14
5486 || r_type == R_PPC64_ADDR14_BRTAKEN
5487 || r_type == R_PPC64_ADDR14_BRNTAKEN
5488 || r_type == R_PPC64_PLTCALL);
5489 }
5490
5491 /* Relocs on inline plt call sequence insns prior to the call. */
5492
5493 static bfd_boolean
5494 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5495 {
5496 return (r_type == R_PPC64_PLT16_HA
5497 || r_type == R_PPC64_PLT16_HI
5498 || r_type == R_PPC64_PLT16_LO
5499 || r_type == R_PPC64_PLT16_LO_DS
5500 || r_type == R_PPC64_PLTSEQ);
5501 }
5502
5503 /* Look through the relocs for a section during the first phase, and
5504 calculate needed space in the global offset table, procedure
5505 linkage table, and dynamic reloc sections. */
5506
5507 static bfd_boolean
5508 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5509 asection *sec, const Elf_Internal_Rela *relocs)
5510 {
5511 struct ppc_link_hash_table *htab;
5512 Elf_Internal_Shdr *symtab_hdr;
5513 struct elf_link_hash_entry **sym_hashes;
5514 const Elf_Internal_Rela *rel;
5515 const Elf_Internal_Rela *rel_end;
5516 asection *sreloc;
5517 struct elf_link_hash_entry *tga, *dottga;
5518 bfd_boolean is_opd;
5519
5520 if (bfd_link_relocatable (info))
5521 return TRUE;
5522
5523 /* Don't do anything special with non-loaded, non-alloced sections.
5524 In particular, any relocs in such sections should not affect GOT
5525 and PLT reference counting (ie. we don't allow them to create GOT
5526 or PLT entries), there's no possibility or desire to optimize TLS
5527 relocs, and there's not much point in propagating relocs to shared
5528 libs that the dynamic linker won't relocate. */
5529 if ((sec->flags & SEC_ALLOC) == 0)
5530 return TRUE;
5531
5532 BFD_ASSERT (is_ppc64_elf (abfd));
5533
5534 htab = ppc_hash_table (info);
5535 if (htab == NULL)
5536 return FALSE;
5537
5538 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5539 FALSE, FALSE, TRUE);
5540 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5541 FALSE, FALSE, TRUE);
5542 symtab_hdr = &elf_symtab_hdr (abfd);
5543 sym_hashes = elf_sym_hashes (abfd);
5544 sreloc = NULL;
5545 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5546 rel_end = relocs + sec->reloc_count;
5547 for (rel = relocs; rel < rel_end; rel++)
5548 {
5549 unsigned long r_symndx;
5550 struct elf_link_hash_entry *h;
5551 enum elf_ppc64_reloc_type r_type;
5552 int tls_type;
5553 struct _ppc64_elf_section_data *ppc64_sec;
5554 struct plt_entry **ifunc, **plt_list;
5555
5556 r_symndx = ELF64_R_SYM (rel->r_info);
5557 if (r_symndx < symtab_hdr->sh_info)
5558 h = NULL;
5559 else
5560 {
5561 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5562 h = elf_follow_link (h);
5563
5564 if (h == htab->elf.hgot)
5565 sec->has_toc_reloc = 1;
5566 }
5567
5568 tls_type = 0;
5569 ifunc = NULL;
5570 if (h != NULL)
5571 {
5572 if (h->type == STT_GNU_IFUNC)
5573 {
5574 h->needs_plt = 1;
5575 ifunc = &h->plt.plist;
5576 }
5577 }
5578 else
5579 {
5580 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5581 abfd, r_symndx);
5582 if (isym == NULL)
5583 return FALSE;
5584
5585 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5586 {
5587 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5588 rel->r_addend,
5589 NON_GOT | PLT_IFUNC);
5590 if (ifunc == NULL)
5591 return FALSE;
5592 }
5593 }
5594
5595 r_type = ELF64_R_TYPE (rel->r_info);
5596 switch (r_type)
5597 {
5598 case R_PPC64_TLSGD:
5599 case R_PPC64_TLSLD:
5600 /* These special tls relocs tie a call to __tls_get_addr with
5601 its parameter symbol. */
5602 if (h != NULL)
5603 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5604 else
5605 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5606 rel->r_addend,
5607 NON_GOT | TLS_TLS | TLS_MARK))
5608 return FALSE;
5609 sec->has_tls_reloc = 1;
5610 break;
5611
5612 case R_PPC64_GOT_TLSLD16:
5613 case R_PPC64_GOT_TLSLD16_LO:
5614 case R_PPC64_GOT_TLSLD16_HI:
5615 case R_PPC64_GOT_TLSLD16_HA:
5616 tls_type = TLS_TLS | TLS_LD;
5617 goto dogottls;
5618
5619 case R_PPC64_GOT_TLSGD16:
5620 case R_PPC64_GOT_TLSGD16_LO:
5621 case R_PPC64_GOT_TLSGD16_HI:
5622 case R_PPC64_GOT_TLSGD16_HA:
5623 tls_type = TLS_TLS | TLS_GD;
5624 goto dogottls;
5625
5626 case R_PPC64_GOT_TPREL16_DS:
5627 case R_PPC64_GOT_TPREL16_LO_DS:
5628 case R_PPC64_GOT_TPREL16_HI:
5629 case R_PPC64_GOT_TPREL16_HA:
5630 if (bfd_link_dll (info))
5631 info->flags |= DF_STATIC_TLS;
5632 tls_type = TLS_TLS | TLS_TPREL;
5633 goto dogottls;
5634
5635 case R_PPC64_GOT_DTPREL16_DS:
5636 case R_PPC64_GOT_DTPREL16_LO_DS:
5637 case R_PPC64_GOT_DTPREL16_HI:
5638 case R_PPC64_GOT_DTPREL16_HA:
5639 tls_type = TLS_TLS | TLS_DTPREL;
5640 dogottls:
5641 sec->has_tls_reloc = 1;
5642 /* Fall through */
5643
5644 case R_PPC64_GOT16:
5645 case R_PPC64_GOT16_DS:
5646 case R_PPC64_GOT16_HA:
5647 case R_PPC64_GOT16_HI:
5648 case R_PPC64_GOT16_LO:
5649 case R_PPC64_GOT16_LO_DS:
5650 /* This symbol requires a global offset table entry. */
5651 sec->has_toc_reloc = 1;
5652 if (r_type == R_PPC64_GOT_TLSLD16
5653 || r_type == R_PPC64_GOT_TLSGD16
5654 || r_type == R_PPC64_GOT_TPREL16_DS
5655 || r_type == R_PPC64_GOT_DTPREL16_DS
5656 || r_type == R_PPC64_GOT16
5657 || r_type == R_PPC64_GOT16_DS)
5658 {
5659 htab->do_multi_toc = 1;
5660 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5661 }
5662
5663 if (ppc64_elf_tdata (abfd)->got == NULL
5664 && !create_got_section (abfd, info))
5665 return FALSE;
5666
5667 if (h != NULL)
5668 {
5669 struct ppc_link_hash_entry *eh;
5670 struct got_entry *ent;
5671
5672 eh = (struct ppc_link_hash_entry *) h;
5673 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5674 if (ent->addend == rel->r_addend
5675 && ent->owner == abfd
5676 && ent->tls_type == tls_type)
5677 break;
5678 if (ent == NULL)
5679 {
5680 bfd_size_type amt = sizeof (*ent);
5681 ent = bfd_alloc (abfd, amt);
5682 if (ent == NULL)
5683 return FALSE;
5684 ent->next = eh->elf.got.glist;
5685 ent->addend = rel->r_addend;
5686 ent->owner = abfd;
5687 ent->tls_type = tls_type;
5688 ent->is_indirect = FALSE;
5689 ent->got.refcount = 0;
5690 eh->elf.got.glist = ent;
5691 }
5692 ent->got.refcount += 1;
5693 eh->tls_mask |= tls_type;
5694 }
5695 else
5696 /* This is a global offset table entry for a local symbol. */
5697 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5698 rel->r_addend, tls_type))
5699 return FALSE;
5700
5701 /* We may also need a plt entry if the symbol turns out to be
5702 an ifunc. */
5703 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5704 {
5705 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5706 return FALSE;
5707 }
5708 break;
5709
5710 case R_PPC64_PLT16_HA:
5711 case R_PPC64_PLT16_HI:
5712 case R_PPC64_PLT16_LO:
5713 case R_PPC64_PLT16_LO_DS:
5714 case R_PPC64_PLT32:
5715 case R_PPC64_PLT64:
5716 /* This symbol requires a procedure linkage table entry. */
5717 plt_list = ifunc;
5718 if (h != NULL)
5719 {
5720 h->needs_plt = 1;
5721 if (h->root.root.string[0] == '.'
5722 && h->root.root.string[1] != '\0')
5723 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5724 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5725 plt_list = &h->plt.plist;
5726 }
5727 if (plt_list == NULL)
5728 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5729 rel->r_addend,
5730 NON_GOT | PLT_KEEP);
5731 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5732 return FALSE;
5733 break;
5734
5735 /* The following relocations don't need to propagate the
5736 relocation if linking a shared object since they are
5737 section relative. */
5738 case R_PPC64_SECTOFF:
5739 case R_PPC64_SECTOFF_LO:
5740 case R_PPC64_SECTOFF_HI:
5741 case R_PPC64_SECTOFF_HA:
5742 case R_PPC64_SECTOFF_DS:
5743 case R_PPC64_SECTOFF_LO_DS:
5744 case R_PPC64_DTPREL16:
5745 case R_PPC64_DTPREL16_LO:
5746 case R_PPC64_DTPREL16_HI:
5747 case R_PPC64_DTPREL16_HA:
5748 case R_PPC64_DTPREL16_DS:
5749 case R_PPC64_DTPREL16_LO_DS:
5750 case R_PPC64_DTPREL16_HIGH:
5751 case R_PPC64_DTPREL16_HIGHA:
5752 case R_PPC64_DTPREL16_HIGHER:
5753 case R_PPC64_DTPREL16_HIGHERA:
5754 case R_PPC64_DTPREL16_HIGHEST:
5755 case R_PPC64_DTPREL16_HIGHESTA:
5756 break;
5757
5758 /* Nor do these. */
5759 case R_PPC64_REL16:
5760 case R_PPC64_REL16_LO:
5761 case R_PPC64_REL16_HI:
5762 case R_PPC64_REL16_HA:
5763 case R_PPC64_REL16DX_HA:
5764 break;
5765
5766 /* Not supported as a dynamic relocation. */
5767 case R_PPC64_ADDR64_LOCAL:
5768 if (bfd_link_pic (info))
5769 {
5770 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5771 ppc_howto_init ();
5772 /* xgettext:c-format */
5773 info->callbacks->einfo (_("%H: %s reloc unsupported "
5774 "in shared libraries and PIEs\n"),
5775 abfd, sec, rel->r_offset,
5776 ppc64_elf_howto_table[r_type]->name);
5777 bfd_set_error (bfd_error_bad_value);
5778 return FALSE;
5779 }
5780 break;
5781
5782 case R_PPC64_TOC16:
5783 case R_PPC64_TOC16_DS:
5784 htab->do_multi_toc = 1;
5785 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5786 /* Fall through. */
5787 case R_PPC64_TOC16_LO:
5788 case R_PPC64_TOC16_HI:
5789 case R_PPC64_TOC16_HA:
5790 case R_PPC64_TOC16_LO_DS:
5791 sec->has_toc_reloc = 1;
5792 break;
5793
5794 /* Marker reloc. */
5795 case R_PPC64_ENTRY:
5796 break;
5797
5798 /* This relocation describes the C++ object vtable hierarchy.
5799 Reconstruct it for later use during GC. */
5800 case R_PPC64_GNU_VTINHERIT:
5801 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5802 return FALSE;
5803 break;
5804
5805 /* This relocation describes which C++ vtable entries are actually
5806 used. Record for later use during GC. */
5807 case R_PPC64_GNU_VTENTRY:
5808 BFD_ASSERT (h != NULL);
5809 if (h != NULL
5810 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5811 return FALSE;
5812 break;
5813
5814 case R_PPC64_REL14:
5815 case R_PPC64_REL14_BRTAKEN:
5816 case R_PPC64_REL14_BRNTAKEN:
5817 {
5818 asection *dest = NULL;
5819
5820 /* Heuristic: If jumping outside our section, chances are
5821 we are going to need a stub. */
5822 if (h != NULL)
5823 {
5824 /* If the sym is weak it may be overridden later, so
5825 don't assume we know where a weak sym lives. */
5826 if (h->root.type == bfd_link_hash_defined)
5827 dest = h->root.u.def.section;
5828 }
5829 else
5830 {
5831 Elf_Internal_Sym *isym;
5832
5833 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5834 abfd, r_symndx);
5835 if (isym == NULL)
5836 return FALSE;
5837
5838 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5839 }
5840
5841 if (dest != sec)
5842 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5843 }
5844 goto rel24;
5845
5846 case R_PPC64_PLTCALL:
5847 ppc64_elf_section_data (sec)->has_pltcall = 1;
5848 /* Fall through. */
5849
5850 case R_PPC64_REL24:
5851 rel24:
5852 plt_list = ifunc;
5853 if (h != NULL)
5854 {
5855 h->needs_plt = 1;
5856 if (h->root.root.string[0] == '.'
5857 && h->root.root.string[1] != '\0')
5858 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5859
5860 if (h == tga || h == dottga)
5861 {
5862 sec->has_tls_reloc = 1;
5863 if (rel != relocs
5864 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5865 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5866 /* We have a new-style __tls_get_addr call with
5867 a marker reloc. */
5868 ;
5869 else
5870 /* Mark this section as having an old-style call. */
5871 sec->has_tls_get_addr_call = 1;
5872 }
5873 plt_list = &h->plt.plist;
5874 }
5875
5876 /* We may need a .plt entry if the function this reloc
5877 refers to is in a shared lib. */
5878 if (plt_list
5879 && !update_plt_info (abfd, plt_list, rel->r_addend))
5880 return FALSE;
5881 break;
5882
5883 case R_PPC64_ADDR14:
5884 case R_PPC64_ADDR14_BRNTAKEN:
5885 case R_PPC64_ADDR14_BRTAKEN:
5886 case R_PPC64_ADDR24:
5887 goto dodyn;
5888
5889 case R_PPC64_TPREL64:
5890 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5891 if (bfd_link_dll (info))
5892 info->flags |= DF_STATIC_TLS;
5893 goto dotlstoc;
5894
5895 case R_PPC64_DTPMOD64:
5896 if (rel + 1 < rel_end
5897 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5898 && rel[1].r_offset == rel->r_offset + 8)
5899 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5900 else
5901 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5902 goto dotlstoc;
5903
5904 case R_PPC64_DTPREL64:
5905 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5906 if (rel != relocs
5907 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5908 && rel[-1].r_offset == rel->r_offset - 8)
5909 /* This is the second reloc of a dtpmod, dtprel pair.
5910 Don't mark with TLS_DTPREL. */
5911 goto dodyn;
5912
5913 dotlstoc:
5914 sec->has_tls_reloc = 1;
5915 if (h != NULL)
5916 {
5917 struct ppc_link_hash_entry *eh;
5918 eh = (struct ppc_link_hash_entry *) h;
5919 eh->tls_mask |= tls_type;
5920 }
5921 else
5922 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5923 rel->r_addend, tls_type))
5924 return FALSE;
5925
5926 ppc64_sec = ppc64_elf_section_data (sec);
5927 if (ppc64_sec->sec_type != sec_toc)
5928 {
5929 bfd_size_type amt;
5930
5931 /* One extra to simplify get_tls_mask. */
5932 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5933 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5934 if (ppc64_sec->u.toc.symndx == NULL)
5935 return FALSE;
5936 amt = sec->size * sizeof (bfd_vma) / 8;
5937 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5938 if (ppc64_sec->u.toc.add == NULL)
5939 return FALSE;
5940 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5941 ppc64_sec->sec_type = sec_toc;
5942 }
5943 BFD_ASSERT (rel->r_offset % 8 == 0);
5944 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5945 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5946
5947 /* Mark the second slot of a GD or LD entry.
5948 -1 to indicate GD and -2 to indicate LD. */
5949 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5950 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5951 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5952 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5953 goto dodyn;
5954
5955 case R_PPC64_TPREL16:
5956 case R_PPC64_TPREL16_LO:
5957 case R_PPC64_TPREL16_HI:
5958 case R_PPC64_TPREL16_HA:
5959 case R_PPC64_TPREL16_DS:
5960 case R_PPC64_TPREL16_LO_DS:
5961 case R_PPC64_TPREL16_HIGH:
5962 case R_PPC64_TPREL16_HIGHA:
5963 case R_PPC64_TPREL16_HIGHER:
5964 case R_PPC64_TPREL16_HIGHERA:
5965 case R_PPC64_TPREL16_HIGHEST:
5966 case R_PPC64_TPREL16_HIGHESTA:
5967 if (bfd_link_dll (info))
5968 info->flags |= DF_STATIC_TLS;
5969 goto dodyn;
5970
5971 case R_PPC64_ADDR64:
5972 if (is_opd
5973 && rel + 1 < rel_end
5974 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5975 {
5976 if (h != NULL)
5977 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5978 }
5979 /* Fall through. */
5980
5981 case R_PPC64_ADDR16:
5982 case R_PPC64_ADDR16_DS:
5983 case R_PPC64_ADDR16_HA:
5984 case R_PPC64_ADDR16_HI:
5985 case R_PPC64_ADDR16_HIGH:
5986 case R_PPC64_ADDR16_HIGHA:
5987 case R_PPC64_ADDR16_HIGHER:
5988 case R_PPC64_ADDR16_HIGHERA:
5989 case R_PPC64_ADDR16_HIGHEST:
5990 case R_PPC64_ADDR16_HIGHESTA:
5991 case R_PPC64_ADDR16_LO:
5992 case R_PPC64_ADDR16_LO_DS:
5993 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5994 && rel->r_addend == 0)
5995 {
5996 /* We may need a .plt entry if this reloc refers to a
5997 function in a shared lib. */
5998 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5999 return FALSE;
6000 h->pointer_equality_needed = 1;
6001 }
6002 /* Fall through. */
6003
6004 case R_PPC64_REL30:
6005 case R_PPC64_REL32:
6006 case R_PPC64_REL64:
6007 case R_PPC64_ADDR32:
6008 case R_PPC64_UADDR16:
6009 case R_PPC64_UADDR32:
6010 case R_PPC64_UADDR64:
6011 case R_PPC64_TOC:
6012 if (h != NULL && !bfd_link_pic (info))
6013 /* We may need a copy reloc. */
6014 h->non_got_ref = 1;
6015
6016 /* Don't propagate .opd relocs. */
6017 if (NO_OPD_RELOCS && is_opd)
6018 break;
6019
6020 /* If we are creating a shared library, and this is a reloc
6021 against a global symbol, or a non PC relative reloc
6022 against a local symbol, then we need to copy the reloc
6023 into the shared library. However, if we are linking with
6024 -Bsymbolic, we do not need to copy a reloc against a
6025 global symbol which is defined in an object we are
6026 including in the link (i.e., DEF_REGULAR is set). At
6027 this point we have not seen all the input files, so it is
6028 possible that DEF_REGULAR is not set now but will be set
6029 later (it is never cleared). In case of a weak definition,
6030 DEF_REGULAR may be cleared later by a strong definition in
6031 a shared library. We account for that possibility below by
6032 storing information in the dyn_relocs field of the hash
6033 table entry. A similar situation occurs when creating
6034 shared libraries and symbol visibility changes render the
6035 symbol local.
6036
6037 If on the other hand, we are creating an executable, we
6038 may need to keep relocations for symbols satisfied by a
6039 dynamic library if we manage to avoid copy relocs for the
6040 symbol. */
6041 dodyn:
6042 if ((bfd_link_pic (info)
6043 && (must_be_dyn_reloc (info, r_type)
6044 || (h != NULL
6045 && (!SYMBOLIC_BIND (info, h)
6046 || h->root.type == bfd_link_hash_defweak
6047 || !h->def_regular))))
6048 || (ELIMINATE_COPY_RELOCS
6049 && !bfd_link_pic (info)
6050 && h != NULL
6051 && (h->root.type == bfd_link_hash_defweak
6052 || !h->def_regular))
6053 || (!bfd_link_pic (info)
6054 && ifunc != NULL))
6055 {
6056 /* We must copy these reloc types into the output file.
6057 Create a reloc section in dynobj and make room for
6058 this reloc. */
6059 if (sreloc == NULL)
6060 {
6061 sreloc = _bfd_elf_make_dynamic_reloc_section
6062 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6063
6064 if (sreloc == NULL)
6065 return FALSE;
6066 }
6067
6068 /* If this is a global symbol, we count the number of
6069 relocations we need for this symbol. */
6070 if (h != NULL)
6071 {
6072 struct elf_dyn_relocs *p;
6073 struct elf_dyn_relocs **head;
6074
6075 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6076 p = *head;
6077 if (p == NULL || p->sec != sec)
6078 {
6079 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6080 if (p == NULL)
6081 return FALSE;
6082 p->next = *head;
6083 *head = p;
6084 p->sec = sec;
6085 p->count = 0;
6086 p->pc_count = 0;
6087 }
6088 p->count += 1;
6089 if (!must_be_dyn_reloc (info, r_type))
6090 p->pc_count += 1;
6091 }
6092 else
6093 {
6094 /* Track dynamic relocs needed for local syms too.
6095 We really need local syms available to do this
6096 easily. Oh well. */
6097 struct ppc_dyn_relocs *p;
6098 struct ppc_dyn_relocs **head;
6099 bfd_boolean is_ifunc;
6100 asection *s;
6101 void *vpp;
6102 Elf_Internal_Sym *isym;
6103
6104 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6105 abfd, r_symndx);
6106 if (isym == NULL)
6107 return FALSE;
6108
6109 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6110 if (s == NULL)
6111 s = sec;
6112
6113 vpp = &elf_section_data (s)->local_dynrel;
6114 head = (struct ppc_dyn_relocs **) vpp;
6115 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6116 p = *head;
6117 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6118 p = p->next;
6119 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6120 {
6121 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6122 if (p == NULL)
6123 return FALSE;
6124 p->next = *head;
6125 *head = p;
6126 p->sec = sec;
6127 p->ifunc = is_ifunc;
6128 p->count = 0;
6129 }
6130 p->count += 1;
6131 }
6132 }
6133 break;
6134
6135 default:
6136 break;
6137 }
6138 }
6139
6140 return TRUE;
6141 }
6142
6143 /* Merge backend specific data from an object file to the output
6144 object file when linking. */
6145
6146 static bfd_boolean
6147 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6148 {
6149 bfd *obfd = info->output_bfd;
6150 unsigned long iflags, oflags;
6151
6152 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6153 return TRUE;
6154
6155 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6156 return TRUE;
6157
6158 if (!_bfd_generic_verify_endian_match (ibfd, info))
6159 return FALSE;
6160
6161 iflags = elf_elfheader (ibfd)->e_flags;
6162 oflags = elf_elfheader (obfd)->e_flags;
6163
6164 if (iflags & ~EF_PPC64_ABI)
6165 {
6166 _bfd_error_handler
6167 /* xgettext:c-format */
6168 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6169 bfd_set_error (bfd_error_bad_value);
6170 return FALSE;
6171 }
6172 else if (iflags != oflags && iflags != 0)
6173 {
6174 _bfd_error_handler
6175 /* xgettext:c-format */
6176 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6177 ibfd, iflags, oflags);
6178 bfd_set_error (bfd_error_bad_value);
6179 return FALSE;
6180 }
6181
6182 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6183 return FALSE;
6184
6185 /* Merge Tag_compatibility attributes and any common GNU ones. */
6186 return _bfd_elf_merge_object_attributes (ibfd, info);
6187 }
6188
6189 static bfd_boolean
6190 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6191 {
6192 /* Print normal ELF private data. */
6193 _bfd_elf_print_private_bfd_data (abfd, ptr);
6194
6195 if (elf_elfheader (abfd)->e_flags != 0)
6196 {
6197 FILE *file = ptr;
6198
6199 fprintf (file, _("private flags = 0x%lx:"),
6200 elf_elfheader (abfd)->e_flags);
6201
6202 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6203 fprintf (file, _(" [abiv%ld]"),
6204 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6205 fputc ('\n', file);
6206 }
6207
6208 return TRUE;
6209 }
6210
6211 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6212 of the code entry point, and its section, which must be in the same
6213 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6214
6215 static bfd_vma
6216 opd_entry_value (asection *opd_sec,
6217 bfd_vma offset,
6218 asection **code_sec,
6219 bfd_vma *code_off,
6220 bfd_boolean in_code_sec)
6221 {
6222 bfd *opd_bfd = opd_sec->owner;
6223 Elf_Internal_Rela *relocs;
6224 Elf_Internal_Rela *lo, *hi, *look;
6225 bfd_vma val;
6226
6227 /* No relocs implies we are linking a --just-symbols object, or looking
6228 at a final linked executable with addr2line or somesuch. */
6229 if (opd_sec->reloc_count == 0)
6230 {
6231 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6232
6233 if (contents == NULL)
6234 {
6235 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6236 return (bfd_vma) -1;
6237 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6238 }
6239
6240 /* PR 17512: file: 64b9dfbb. */
6241 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6242 return (bfd_vma) -1;
6243
6244 val = bfd_get_64 (opd_bfd, contents + offset);
6245 if (code_sec != NULL)
6246 {
6247 asection *sec, *likely = NULL;
6248
6249 if (in_code_sec)
6250 {
6251 sec = *code_sec;
6252 if (sec->vma <= val
6253 && val < sec->vma + sec->size)
6254 likely = sec;
6255 else
6256 val = -1;
6257 }
6258 else
6259 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6260 if (sec->vma <= val
6261 && (sec->flags & SEC_LOAD) != 0
6262 && (sec->flags & SEC_ALLOC) != 0)
6263 likely = sec;
6264 if (likely != NULL)
6265 {
6266 *code_sec = likely;
6267 if (code_off != NULL)
6268 *code_off = val - likely->vma;
6269 }
6270 }
6271 return val;
6272 }
6273
6274 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6275
6276 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6277 if (relocs == NULL)
6278 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6279 /* PR 17512: file: df8e1fd6. */
6280 if (relocs == NULL)
6281 return (bfd_vma) -1;
6282
6283 /* Go find the opd reloc at the sym address. */
6284 lo = relocs;
6285 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6286 val = (bfd_vma) -1;
6287 while (lo < hi)
6288 {
6289 look = lo + (hi - lo) / 2;
6290 if (look->r_offset < offset)
6291 lo = look + 1;
6292 else if (look->r_offset > offset)
6293 hi = look;
6294 else
6295 {
6296 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6297
6298 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6299 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6300 {
6301 unsigned long symndx = ELF64_R_SYM (look->r_info);
6302 asection *sec = NULL;
6303
6304 if (symndx >= symtab_hdr->sh_info
6305 && elf_sym_hashes (opd_bfd) != NULL)
6306 {
6307 struct elf_link_hash_entry **sym_hashes;
6308 struct elf_link_hash_entry *rh;
6309
6310 sym_hashes = elf_sym_hashes (opd_bfd);
6311 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6312 if (rh != NULL)
6313 {
6314 rh = elf_follow_link (rh);
6315 if (rh->root.type != bfd_link_hash_defined
6316 && rh->root.type != bfd_link_hash_defweak)
6317 break;
6318 if (rh->root.u.def.section->owner == opd_bfd)
6319 {
6320 val = rh->root.u.def.value;
6321 sec = rh->root.u.def.section;
6322 }
6323 }
6324 }
6325
6326 if (sec == NULL)
6327 {
6328 Elf_Internal_Sym *sym;
6329
6330 if (symndx < symtab_hdr->sh_info)
6331 {
6332 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6333 if (sym == NULL)
6334 {
6335 size_t symcnt = symtab_hdr->sh_info;
6336 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6337 symcnt, 0,
6338 NULL, NULL, NULL);
6339 if (sym == NULL)
6340 break;
6341 symtab_hdr->contents = (bfd_byte *) sym;
6342 }
6343 sym += symndx;
6344 }
6345 else
6346 {
6347 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6348 1, symndx,
6349 NULL, NULL, NULL);
6350 if (sym == NULL)
6351 break;
6352 }
6353 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6354 if (sec == NULL)
6355 break;
6356 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6357 val = sym->st_value;
6358 }
6359
6360 val += look->r_addend;
6361 if (code_off != NULL)
6362 *code_off = val;
6363 if (code_sec != NULL)
6364 {
6365 if (in_code_sec && *code_sec != sec)
6366 return -1;
6367 else
6368 *code_sec = sec;
6369 }
6370 if (sec->output_section != NULL)
6371 val += sec->output_section->vma + sec->output_offset;
6372 }
6373 break;
6374 }
6375 }
6376
6377 return val;
6378 }
6379
6380 /* If the ELF symbol SYM might be a function in SEC, return the
6381 function size and set *CODE_OFF to the function's entry point,
6382 otherwise return zero. */
6383
6384 static bfd_size_type
6385 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6386 bfd_vma *code_off)
6387 {
6388 bfd_size_type size;
6389
6390 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6391 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6392 return 0;
6393
6394 size = 0;
6395 if (!(sym->flags & BSF_SYNTHETIC))
6396 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6397
6398 if (strcmp (sym->section->name, ".opd") == 0)
6399 {
6400 struct _opd_sec_data *opd = get_opd_info (sym->section);
6401 bfd_vma symval = sym->value;
6402
6403 if (opd != NULL
6404 && opd->adjust != NULL
6405 && elf_section_data (sym->section)->relocs != NULL)
6406 {
6407 /* opd_entry_value will use cached relocs that have been
6408 adjusted, but with raw symbols. That means both local
6409 and global symbols need adjusting. */
6410 long adjust = opd->adjust[OPD_NDX (symval)];
6411 if (adjust == -1)
6412 return 0;
6413 symval += adjust;
6414 }
6415
6416 if (opd_entry_value (sym->section, symval,
6417 &sec, code_off, TRUE) == (bfd_vma) -1)
6418 return 0;
6419 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6420 symbol. This size has nothing to do with the code size of the
6421 function, which is what we're supposed to return, but the
6422 code size isn't available without looking up the dot-sym.
6423 However, doing that would be a waste of time particularly
6424 since elf_find_function will look at the dot-sym anyway.
6425 Now, elf_find_function will keep the largest size of any
6426 function sym found at the code address of interest, so return
6427 1 here to avoid it incorrectly caching a larger function size
6428 for a small function. This does mean we return the wrong
6429 size for a new-ABI function of size 24, but all that does is
6430 disable caching for such functions. */
6431 if (size == 24)
6432 size = 1;
6433 }
6434 else
6435 {
6436 if (sym->section != sec)
6437 return 0;
6438 *code_off = sym->value;
6439 }
6440 if (size == 0)
6441 size = 1;
6442 return size;
6443 }
6444
6445 /* Return true if symbol is a strong function defined in an ELFv2
6446 object with st_other localentry bits of zero, ie. its local entry
6447 point coincides with its global entry point. */
6448
6449 static bfd_boolean
6450 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6451 {
6452 return (h != NULL
6453 && h->type == STT_FUNC
6454 && h->root.type == bfd_link_hash_defined
6455 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6456 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6457 && is_ppc64_elf (h->root.u.def.section->owner)
6458 && abiversion (h->root.u.def.section->owner) >= 2);
6459 }
6460
6461 /* Return true if symbol is defined in a regular object file. */
6462
6463 static bfd_boolean
6464 is_static_defined (struct elf_link_hash_entry *h)
6465 {
6466 return ((h->root.type == bfd_link_hash_defined
6467 || h->root.type == bfd_link_hash_defweak)
6468 && h->root.u.def.section != NULL
6469 && h->root.u.def.section->output_section != NULL);
6470 }
6471
6472 /* If FDH is a function descriptor symbol, return the associated code
6473 entry symbol if it is defined. Return NULL otherwise. */
6474
6475 static struct ppc_link_hash_entry *
6476 defined_code_entry (struct ppc_link_hash_entry *fdh)
6477 {
6478 if (fdh->is_func_descriptor)
6479 {
6480 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6481 if (fh->elf.root.type == bfd_link_hash_defined
6482 || fh->elf.root.type == bfd_link_hash_defweak)
6483 return fh;
6484 }
6485 return NULL;
6486 }
6487
6488 /* If FH is a function code entry symbol, return the associated
6489 function descriptor symbol if it is defined. Return NULL otherwise. */
6490
6491 static struct ppc_link_hash_entry *
6492 defined_func_desc (struct ppc_link_hash_entry *fh)
6493 {
6494 if (fh->oh != NULL
6495 && fh->oh->is_func_descriptor)
6496 {
6497 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6498 if (fdh->elf.root.type == bfd_link_hash_defined
6499 || fdh->elf.root.type == bfd_link_hash_defweak)
6500 return fdh;
6501 }
6502 return NULL;
6503 }
6504
6505 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6506
6507 /* Garbage collect sections, after first dealing with dot-symbols. */
6508
6509 static bfd_boolean
6510 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6511 {
6512 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6513
6514 if (htab != NULL && htab->need_func_desc_adj)
6515 {
6516 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6517 htab->need_func_desc_adj = 0;
6518 }
6519 return bfd_elf_gc_sections (abfd, info);
6520 }
6521
6522 /* Mark all our entry sym sections, both opd and code section. */
6523
6524 static void
6525 ppc64_elf_gc_keep (struct bfd_link_info *info)
6526 {
6527 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6528 struct bfd_sym_chain *sym;
6529
6530 if (htab == NULL)
6531 return;
6532
6533 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6534 {
6535 struct ppc_link_hash_entry *eh, *fh;
6536 asection *sec;
6537
6538 eh = (struct ppc_link_hash_entry *)
6539 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6540 if (eh == NULL)
6541 continue;
6542 if (eh->elf.root.type != bfd_link_hash_defined
6543 && eh->elf.root.type != bfd_link_hash_defweak)
6544 continue;
6545
6546 fh = defined_code_entry (eh);
6547 if (fh != NULL)
6548 {
6549 sec = fh->elf.root.u.def.section;
6550 sec->flags |= SEC_KEEP;
6551 }
6552 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6553 && opd_entry_value (eh->elf.root.u.def.section,
6554 eh->elf.root.u.def.value,
6555 &sec, NULL, FALSE) != (bfd_vma) -1)
6556 sec->flags |= SEC_KEEP;
6557
6558 sec = eh->elf.root.u.def.section;
6559 sec->flags |= SEC_KEEP;
6560 }
6561 }
6562
6563 /* Mark sections containing dynamically referenced symbols. When
6564 building shared libraries, we must assume that any visible symbol is
6565 referenced. */
6566
6567 static bfd_boolean
6568 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6569 {
6570 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6571 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6572 struct ppc_link_hash_entry *fdh;
6573 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6574
6575 /* Dynamic linking info is on the func descriptor sym. */
6576 fdh = defined_func_desc (eh);
6577 if (fdh != NULL)
6578 eh = fdh;
6579
6580 if ((eh->elf.root.type == bfd_link_hash_defined
6581 || eh->elf.root.type == bfd_link_hash_defweak)
6582 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6583 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6584 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6585 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6586 && (!bfd_link_executable (info)
6587 || info->gc_keep_exported
6588 || info->export_dynamic
6589 || (eh->elf.dynamic
6590 && d != NULL
6591 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6592 && (eh->elf.versioned >= versioned
6593 || !bfd_hide_sym_by_version (info->version_info,
6594 eh->elf.root.root.string)))))
6595 {
6596 asection *code_sec;
6597 struct ppc_link_hash_entry *fh;
6598
6599 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6600
6601 /* Function descriptor syms cause the associated
6602 function code sym section to be marked. */
6603 fh = defined_code_entry (eh);
6604 if (fh != NULL)
6605 {
6606 code_sec = fh->elf.root.u.def.section;
6607 code_sec->flags |= SEC_KEEP;
6608 }
6609 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6610 && opd_entry_value (eh->elf.root.u.def.section,
6611 eh->elf.root.u.def.value,
6612 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6613 code_sec->flags |= SEC_KEEP;
6614 }
6615
6616 return TRUE;
6617 }
6618
6619 /* Return the section that should be marked against GC for a given
6620 relocation. */
6621
6622 static asection *
6623 ppc64_elf_gc_mark_hook (asection *sec,
6624 struct bfd_link_info *info,
6625 Elf_Internal_Rela *rel,
6626 struct elf_link_hash_entry *h,
6627 Elf_Internal_Sym *sym)
6628 {
6629 asection *rsec;
6630
6631 /* Syms return NULL if we're marking .opd, so we avoid marking all
6632 function sections, as all functions are referenced in .opd. */
6633 rsec = NULL;
6634 if (get_opd_info (sec) != NULL)
6635 return rsec;
6636
6637 if (h != NULL)
6638 {
6639 enum elf_ppc64_reloc_type r_type;
6640 struct ppc_link_hash_entry *eh, *fh, *fdh;
6641
6642 r_type = ELF64_R_TYPE (rel->r_info);
6643 switch (r_type)
6644 {
6645 case R_PPC64_GNU_VTINHERIT:
6646 case R_PPC64_GNU_VTENTRY:
6647 break;
6648
6649 default:
6650 switch (h->root.type)
6651 {
6652 case bfd_link_hash_defined:
6653 case bfd_link_hash_defweak:
6654 eh = (struct ppc_link_hash_entry *) h;
6655 fdh = defined_func_desc (eh);
6656 if (fdh != NULL)
6657 {
6658 /* -mcall-aixdesc code references the dot-symbol on
6659 a call reloc. Mark the function descriptor too
6660 against garbage collection. */
6661 fdh->elf.mark = 1;
6662 if (fdh->elf.is_weakalias)
6663 weakdef (&fdh->elf)->mark = 1;
6664 eh = fdh;
6665 }
6666
6667 /* Function descriptor syms cause the associated
6668 function code sym section to be marked. */
6669 fh = defined_code_entry (eh);
6670 if (fh != NULL)
6671 {
6672 /* They also mark their opd section. */
6673 eh->elf.root.u.def.section->gc_mark = 1;
6674
6675 rsec = fh->elf.root.u.def.section;
6676 }
6677 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6678 && opd_entry_value (eh->elf.root.u.def.section,
6679 eh->elf.root.u.def.value,
6680 &rsec, NULL, FALSE) != (bfd_vma) -1)
6681 eh->elf.root.u.def.section->gc_mark = 1;
6682 else
6683 rsec = h->root.u.def.section;
6684 break;
6685
6686 case bfd_link_hash_common:
6687 rsec = h->root.u.c.p->section;
6688 break;
6689
6690 default:
6691 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6692 }
6693 }
6694 }
6695 else
6696 {
6697 struct _opd_sec_data *opd;
6698
6699 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6700 opd = get_opd_info (rsec);
6701 if (opd != NULL && opd->func_sec != NULL)
6702 {
6703 rsec->gc_mark = 1;
6704
6705 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6706 }
6707 }
6708
6709 return rsec;
6710 }
6711
6712 /* The maximum size of .sfpr. */
6713 #define SFPR_MAX (218*4)
6714
6715 struct sfpr_def_parms
6716 {
6717 const char name[12];
6718 unsigned char lo, hi;
6719 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6720 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6721 };
6722
6723 /* Auto-generate _save*, _rest* functions in .sfpr.
6724 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6725 instead. */
6726
6727 static bfd_boolean
6728 sfpr_define (struct bfd_link_info *info,
6729 const struct sfpr_def_parms *parm,
6730 asection *stub_sec)
6731 {
6732 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6733 unsigned int i;
6734 size_t len = strlen (parm->name);
6735 bfd_boolean writing = FALSE;
6736 char sym[16];
6737
6738 if (htab == NULL)
6739 return FALSE;
6740
6741 memcpy (sym, parm->name, len);
6742 sym[len + 2] = 0;
6743
6744 for (i = parm->lo; i <= parm->hi; i++)
6745 {
6746 struct ppc_link_hash_entry *h;
6747
6748 sym[len + 0] = i / 10 + '0';
6749 sym[len + 1] = i % 10 + '0';
6750 h = (struct ppc_link_hash_entry *)
6751 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6752 if (stub_sec != NULL)
6753 {
6754 if (h != NULL
6755 && h->elf.root.type == bfd_link_hash_defined
6756 && h->elf.root.u.def.section == htab->sfpr)
6757 {
6758 struct elf_link_hash_entry *s;
6759 char buf[32];
6760 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6761 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6762 if (s == NULL)
6763 return FALSE;
6764 if (s->root.type == bfd_link_hash_new
6765 || (s->root.type = bfd_link_hash_defined
6766 && s->root.u.def.section == stub_sec))
6767 {
6768 s->root.type = bfd_link_hash_defined;
6769 s->root.u.def.section = stub_sec;
6770 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6771 + h->elf.root.u.def.value);
6772 s->ref_regular = 1;
6773 s->def_regular = 1;
6774 s->ref_regular_nonweak = 1;
6775 s->forced_local = 1;
6776 s->non_elf = 0;
6777 s->root.linker_def = 1;
6778 }
6779 }
6780 continue;
6781 }
6782 if (h != NULL)
6783 {
6784 h->save_res = 1;
6785 if (!h->elf.def_regular)
6786 {
6787 h->elf.root.type = bfd_link_hash_defined;
6788 h->elf.root.u.def.section = htab->sfpr;
6789 h->elf.root.u.def.value = htab->sfpr->size;
6790 h->elf.type = STT_FUNC;
6791 h->elf.def_regular = 1;
6792 h->elf.non_elf = 0;
6793 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6794 writing = TRUE;
6795 if (htab->sfpr->contents == NULL)
6796 {
6797 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6798 if (htab->sfpr->contents == NULL)
6799 return FALSE;
6800 }
6801 }
6802 }
6803 if (writing)
6804 {
6805 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6806 if (i != parm->hi)
6807 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6808 else
6809 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6810 htab->sfpr->size = p - htab->sfpr->contents;
6811 }
6812 }
6813
6814 return TRUE;
6815 }
6816
6817 static bfd_byte *
6818 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6819 {
6820 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6821 return p + 4;
6822 }
6823
6824 static bfd_byte *
6825 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6826 {
6827 p = savegpr0 (abfd, p, r);
6828 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6829 p = p + 4;
6830 bfd_put_32 (abfd, BLR, p);
6831 return p + 4;
6832 }
6833
6834 static bfd_byte *
6835 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6836 {
6837 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6838 return p + 4;
6839 }
6840
6841 static bfd_byte *
6842 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6843 {
6844 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6845 p = p + 4;
6846 p = restgpr0 (abfd, p, r);
6847 bfd_put_32 (abfd, MTLR_R0, p);
6848 p = p + 4;
6849 if (r == 29)
6850 {
6851 p = restgpr0 (abfd, p, 30);
6852 p = restgpr0 (abfd, p, 31);
6853 }
6854 bfd_put_32 (abfd, BLR, p);
6855 return p + 4;
6856 }
6857
6858 static bfd_byte *
6859 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6860 {
6861 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6862 return p + 4;
6863 }
6864
6865 static bfd_byte *
6866 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6867 {
6868 p = savegpr1 (abfd, p, r);
6869 bfd_put_32 (abfd, BLR, p);
6870 return p + 4;
6871 }
6872
6873 static bfd_byte *
6874 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6875 {
6876 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6877 return p + 4;
6878 }
6879
6880 static bfd_byte *
6881 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6882 {
6883 p = restgpr1 (abfd, p, r);
6884 bfd_put_32 (abfd, BLR, p);
6885 return p + 4;
6886 }
6887
6888 static bfd_byte *
6889 savefpr (bfd *abfd, bfd_byte *p, int r)
6890 {
6891 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6892 return p + 4;
6893 }
6894
6895 static bfd_byte *
6896 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6897 {
6898 p = savefpr (abfd, p, r);
6899 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6900 p = p + 4;
6901 bfd_put_32 (abfd, BLR, p);
6902 return p + 4;
6903 }
6904
6905 static bfd_byte *
6906 restfpr (bfd *abfd, bfd_byte *p, int r)
6907 {
6908 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6909 return p + 4;
6910 }
6911
6912 static bfd_byte *
6913 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6914 {
6915 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6916 p = p + 4;
6917 p = restfpr (abfd, p, r);
6918 bfd_put_32 (abfd, MTLR_R0, p);
6919 p = p + 4;
6920 if (r == 29)
6921 {
6922 p = restfpr (abfd, p, 30);
6923 p = restfpr (abfd, p, 31);
6924 }
6925 bfd_put_32 (abfd, BLR, p);
6926 return p + 4;
6927 }
6928
6929 static bfd_byte *
6930 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6931 {
6932 p = savefpr (abfd, p, r);
6933 bfd_put_32 (abfd, BLR, p);
6934 return p + 4;
6935 }
6936
6937 static bfd_byte *
6938 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6939 {
6940 p = restfpr (abfd, p, r);
6941 bfd_put_32 (abfd, BLR, p);
6942 return p + 4;
6943 }
6944
6945 static bfd_byte *
6946 savevr (bfd *abfd, bfd_byte *p, int r)
6947 {
6948 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6949 p = p + 4;
6950 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6951 return p + 4;
6952 }
6953
6954 static bfd_byte *
6955 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6956 {
6957 p = savevr (abfd, p, r);
6958 bfd_put_32 (abfd, BLR, p);
6959 return p + 4;
6960 }
6961
6962 static bfd_byte *
6963 restvr (bfd *abfd, bfd_byte *p, int r)
6964 {
6965 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6966 p = p + 4;
6967 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6968 return p + 4;
6969 }
6970
6971 static bfd_byte *
6972 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6973 {
6974 p = restvr (abfd, p, r);
6975 bfd_put_32 (abfd, BLR, p);
6976 return p + 4;
6977 }
6978
6979 /* Called via elf_link_hash_traverse to transfer dynamic linking
6980 information on function code symbol entries to their corresponding
6981 function descriptor symbol entries. */
6982
6983 static bfd_boolean
6984 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6985 {
6986 struct bfd_link_info *info;
6987 struct ppc_link_hash_table *htab;
6988 struct ppc_link_hash_entry *fh;
6989 struct ppc_link_hash_entry *fdh;
6990 bfd_boolean force_local;
6991
6992 fh = (struct ppc_link_hash_entry *) h;
6993 if (fh->elf.root.type == bfd_link_hash_indirect)
6994 return TRUE;
6995
6996 if (!fh->is_func)
6997 return TRUE;
6998
6999 if (fh->elf.root.root.string[0] != '.'
7000 || fh->elf.root.root.string[1] == '\0')
7001 return TRUE;
7002
7003 info = inf;
7004 htab = ppc_hash_table (info);
7005 if (htab == NULL)
7006 return FALSE;
7007
7008 /* Find the corresponding function descriptor symbol. */
7009 fdh = lookup_fdh (fh, htab);
7010
7011 /* Resolve undefined references to dot-symbols as the value
7012 in the function descriptor, if we have one in a regular object.
7013 This is to satisfy cases like ".quad .foo". Calls to functions
7014 in dynamic objects are handled elsewhere. */
7015 if ((fh->elf.root.type == bfd_link_hash_undefined
7016 || fh->elf.root.type == bfd_link_hash_undefweak)
7017 && (fdh->elf.root.type == bfd_link_hash_defined
7018 || fdh->elf.root.type == bfd_link_hash_defweak)
7019 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7020 && opd_entry_value (fdh->elf.root.u.def.section,
7021 fdh->elf.root.u.def.value,
7022 &fh->elf.root.u.def.section,
7023 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7024 {
7025 fh->elf.root.type = fdh->elf.root.type;
7026 fh->elf.forced_local = 1;
7027 fh->elf.def_regular = fdh->elf.def_regular;
7028 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7029 }
7030
7031 if (!fh->elf.dynamic)
7032 {
7033 struct plt_entry *ent;
7034
7035 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7036 if (ent->plt.refcount > 0)
7037 break;
7038 if (ent == NULL)
7039 return TRUE;
7040 }
7041
7042 /* Create a descriptor as undefined if necessary. */
7043 if (fdh == NULL
7044 && !bfd_link_executable (info)
7045 && (fh->elf.root.type == bfd_link_hash_undefined
7046 || fh->elf.root.type == bfd_link_hash_undefweak))
7047 {
7048 fdh = make_fdh (info, fh);
7049 if (fdh == NULL)
7050 return FALSE;
7051 }
7052
7053 /* We can't support overriding of symbols on a fake descriptor. */
7054 if (fdh != NULL
7055 && fdh->fake
7056 && (fh->elf.root.type == bfd_link_hash_defined
7057 || fh->elf.root.type == bfd_link_hash_defweak))
7058 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7059
7060 /* Transfer dynamic linking information to the function descriptor. */
7061 if (fdh != NULL)
7062 {
7063 fdh->elf.ref_regular |= fh->elf.ref_regular;
7064 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7065 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7066 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7067 fdh->elf.dynamic |= fh->elf.dynamic;
7068 fdh->elf.needs_plt |= (fh->elf.needs_plt
7069 || fh->elf.type == STT_FUNC
7070 || fh->elf.type == STT_GNU_IFUNC);
7071 move_plt_plist (fh, fdh);
7072
7073 if (!fdh->elf.forced_local
7074 && fh->elf.dynindx != -1)
7075 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7076 return FALSE;
7077 }
7078
7079 /* Now that the info is on the function descriptor, clear the
7080 function code sym info. Any function code syms for which we
7081 don't have a definition in a regular file, we force local.
7082 This prevents a shared library from exporting syms that have
7083 been imported from another library. Function code syms that
7084 are really in the library we must leave global to prevent the
7085 linker dragging in a definition from a static library. */
7086 force_local = (!fh->elf.def_regular
7087 || fdh == NULL
7088 || !fdh->elf.def_regular
7089 || fdh->elf.forced_local);
7090 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7091
7092 return TRUE;
7093 }
7094
7095 static const struct sfpr_def_parms save_res_funcs[] =
7096 {
7097 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7098 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7099 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7100 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7101 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7102 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7103 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7104 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7105 { "._savef", 14, 31, savefpr, savefpr1_tail },
7106 { "._restf", 14, 31, restfpr, restfpr1_tail },
7107 { "_savevr_", 20, 31, savevr, savevr_tail },
7108 { "_restvr_", 20, 31, restvr, restvr_tail }
7109 };
7110
7111 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7112 this hook to a) provide some gcc support functions, and b) transfer
7113 dynamic linking information gathered so far on function code symbol
7114 entries, to their corresponding function descriptor symbol entries. */
7115
7116 static bfd_boolean
7117 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7118 struct bfd_link_info *info)
7119 {
7120 struct ppc_link_hash_table *htab;
7121
7122 htab = ppc_hash_table (info);
7123 if (htab == NULL)
7124 return FALSE;
7125
7126 /* Provide any missing _save* and _rest* functions. */
7127 if (htab->sfpr != NULL)
7128 {
7129 unsigned int i;
7130
7131 htab->sfpr->size = 0;
7132 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7133 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7134 return FALSE;
7135 if (htab->sfpr->size == 0)
7136 htab->sfpr->flags |= SEC_EXCLUDE;
7137 }
7138
7139 if (bfd_link_relocatable (info))
7140 return TRUE;
7141
7142 if (htab->elf.hgot != NULL)
7143 {
7144 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7145 /* Make .TOC. defined so as to prevent it being made dynamic.
7146 The wrong value here is fixed later in ppc64_elf_set_toc. */
7147 if (!htab->elf.hgot->def_regular
7148 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7149 {
7150 htab->elf.hgot->root.type = bfd_link_hash_defined;
7151 htab->elf.hgot->root.u.def.value = 0;
7152 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7153 htab->elf.hgot->def_regular = 1;
7154 htab->elf.hgot->root.linker_def = 1;
7155 }
7156 htab->elf.hgot->type = STT_OBJECT;
7157 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7158 | STV_HIDDEN);
7159 }
7160
7161 if (htab->need_func_desc_adj)
7162 {
7163 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7164 htab->need_func_desc_adj = 0;
7165 }
7166
7167 return TRUE;
7168 }
7169
7170 /* Find dynamic relocs for H that apply to read-only sections. */
7171
7172 static asection *
7173 readonly_dynrelocs (struct elf_link_hash_entry *h)
7174 {
7175 struct ppc_link_hash_entry *eh;
7176 struct elf_dyn_relocs *p;
7177
7178 eh = (struct ppc_link_hash_entry *) h;
7179 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7180 {
7181 asection *s = p->sec->output_section;
7182
7183 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7184 return p->sec;
7185 }
7186 return NULL;
7187 }
7188
7189 /* Return true if we have dynamic relocs against H or any of its weak
7190 aliases, that apply to read-only sections. Cannot be used after
7191 size_dynamic_sections. */
7192
7193 static bfd_boolean
7194 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7195 {
7196 struct ppc_link_hash_entry *eh;
7197
7198 eh = (struct ppc_link_hash_entry *) h;
7199 do
7200 {
7201 if (readonly_dynrelocs (&eh->elf))
7202 return TRUE;
7203 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7204 } while (eh != NULL && &eh->elf != h);
7205
7206 return FALSE;
7207 }
7208
7209 /* Return whether EH has pc-relative dynamic relocs. */
7210
7211 static bfd_boolean
7212 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7213 {
7214 struct elf_dyn_relocs *p;
7215
7216 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7217 if (p->pc_count != 0)
7218 return TRUE;
7219 return FALSE;
7220 }
7221
7222 /* Return true if a global entry stub will be created for H. Valid
7223 for ELFv2 before plt entries have been allocated. */
7224
7225 static bfd_boolean
7226 global_entry_stub (struct elf_link_hash_entry *h)
7227 {
7228 struct plt_entry *pent;
7229
7230 if (!h->pointer_equality_needed
7231 || h->def_regular)
7232 return FALSE;
7233
7234 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7235 if (pent->plt.refcount > 0
7236 && pent->addend == 0)
7237 return TRUE;
7238
7239 return FALSE;
7240 }
7241
7242 /* Adjust a symbol defined by a dynamic object and referenced by a
7243 regular object. The current definition is in some section of the
7244 dynamic object, but we're not including those sections. We have to
7245 change the definition to something the rest of the link can
7246 understand. */
7247
7248 static bfd_boolean
7249 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7250 struct elf_link_hash_entry *h)
7251 {
7252 struct ppc_link_hash_table *htab;
7253 asection *s, *srel;
7254
7255 htab = ppc_hash_table (info);
7256 if (htab == NULL)
7257 return FALSE;
7258
7259 /* Deal with function syms. */
7260 if (h->type == STT_FUNC
7261 || h->type == STT_GNU_IFUNC
7262 || h->needs_plt)
7263 {
7264 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7265 || SYMBOL_CALLS_LOCAL (info, h)
7266 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7267 /* Discard dyn_relocs when non-pic if we've decided that a
7268 function symbol is local and not an ifunc. We keep dynamic
7269 relocs for ifuncs when local rather than always emitting a
7270 plt call stub for them and defining the symbol on the call
7271 stub. We can't do that for ELFv1 anyway (a function symbol
7272 is defined on a descriptor, not code) and it can be faster at
7273 run-time due to not needing to bounce through a stub. The
7274 dyn_relocs for ifuncs will be applied even in a static
7275 executable. */
7276 if (!bfd_link_pic (info)
7277 && h->type != STT_GNU_IFUNC
7278 && local)
7279 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7280
7281 /* Clear procedure linkage table information for any symbol that
7282 won't need a .plt entry. */
7283 struct plt_entry *ent;
7284 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7285 if (ent->plt.refcount > 0)
7286 break;
7287 if (ent == NULL
7288 || (h->type != STT_GNU_IFUNC
7289 && local
7290 && (htab->can_convert_all_inline_plt
7291 || (((struct ppc_link_hash_entry *) h)->tls_mask
7292 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7293 {
7294 h->plt.plist = NULL;
7295 h->needs_plt = 0;
7296 h->pointer_equality_needed = 0;
7297 }
7298 else if (abiversion (info->output_bfd) >= 2)
7299 {
7300 /* Taking a function's address in a read/write section
7301 doesn't require us to define the function symbol in the
7302 executable on a global entry stub. A dynamic reloc can
7303 be used instead. The reason we prefer a few more dynamic
7304 relocs is that calling via a global entry stub costs a
7305 few more instructions, and pointer_equality_needed causes
7306 extra work in ld.so when resolving these symbols. */
7307 if (global_entry_stub (h))
7308 {
7309 if (!readonly_dynrelocs (h))
7310 {
7311 h->pointer_equality_needed = 0;
7312 /* If we haven't seen a branch reloc and the symbol
7313 isn't an ifunc then we don't need a plt entry. */
7314 if (!h->needs_plt)
7315 h->plt.plist = NULL;
7316 }
7317 else if (!bfd_link_pic (info))
7318 /* We are going to be defining the function symbol on the
7319 plt stub, so no dyn_relocs needed when non-pic. */
7320 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7321 }
7322
7323 /* ELFv2 function symbols can't have copy relocs. */
7324 return TRUE;
7325 }
7326 else if (!h->needs_plt
7327 && !readonly_dynrelocs (h))
7328 {
7329 /* If we haven't seen a branch reloc and the symbol isn't an
7330 ifunc then we don't need a plt entry. */
7331 h->plt.plist = NULL;
7332 h->pointer_equality_needed = 0;
7333 return TRUE;
7334 }
7335 }
7336 else
7337 h->plt.plist = NULL;
7338
7339 /* If this is a weak symbol, and there is a real definition, the
7340 processor independent code will have arranged for us to see the
7341 real definition first, and we can just use the same value. */
7342 if (h->is_weakalias)
7343 {
7344 struct elf_link_hash_entry *def = weakdef (h);
7345 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7346 h->root.u.def.section = def->root.u.def.section;
7347 h->root.u.def.value = def->root.u.def.value;
7348 if (def->root.u.def.section == htab->elf.sdynbss
7349 || def->root.u.def.section == htab->elf.sdynrelro)
7350 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7351 return TRUE;
7352 }
7353
7354 /* If we are creating a shared library, we must presume that the
7355 only references to the symbol are via the global offset table.
7356 For such cases we need not do anything here; the relocations will
7357 be handled correctly by relocate_section. */
7358 if (bfd_link_pic (info))
7359 return TRUE;
7360
7361 /* If there are no references to this symbol that do not use the
7362 GOT, we don't need to generate a copy reloc. */
7363 if (!h->non_got_ref)
7364 return TRUE;
7365
7366 /* Don't generate a copy reloc for symbols defined in the executable. */
7367 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7368
7369 /* If -z nocopyreloc was given, don't generate them either. */
7370 || info->nocopyreloc
7371
7372 /* If we don't find any dynamic relocs in read-only sections, then
7373 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7374 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7375
7376 /* Protected variables do not work with .dynbss. The copy in
7377 .dynbss won't be used by the shared library with the protected
7378 definition for the variable. Text relocations are preferable
7379 to an incorrect program. */
7380 || h->protected_def)
7381 return TRUE;
7382
7383 if (h->plt.plist != NULL)
7384 {
7385 /* We should never get here, but unfortunately there are versions
7386 of gcc out there that improperly (for this ABI) put initialized
7387 function pointers, vtable refs and suchlike in read-only
7388 sections. Allow them to proceed, but warn that this might
7389 break at runtime. */
7390 info->callbacks->einfo
7391 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7392 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7393 h->root.root.string);
7394 }
7395
7396 /* This is a reference to a symbol defined by a dynamic object which
7397 is not a function. */
7398
7399 /* We must allocate the symbol in our .dynbss section, which will
7400 become part of the .bss section of the executable. There will be
7401 an entry for this symbol in the .dynsym section. The dynamic
7402 object will contain position independent code, so all references
7403 from the dynamic object to this symbol will go through the global
7404 offset table. The dynamic linker will use the .dynsym entry to
7405 determine the address it must put in the global offset table, so
7406 both the dynamic object and the regular object will refer to the
7407 same memory location for the variable. */
7408 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7409 {
7410 s = htab->elf.sdynrelro;
7411 srel = htab->elf.sreldynrelro;
7412 }
7413 else
7414 {
7415 s = htab->elf.sdynbss;
7416 srel = htab->elf.srelbss;
7417 }
7418 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7419 {
7420 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7421 linker to copy the initial value out of the dynamic object
7422 and into the runtime process image. */
7423 srel->size += sizeof (Elf64_External_Rela);
7424 h->needs_copy = 1;
7425 }
7426
7427 /* We no longer want dyn_relocs. */
7428 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7429 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7430 }
7431
7432 /* If given a function descriptor symbol, hide both the function code
7433 sym and the descriptor. */
7434 static void
7435 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7436 struct elf_link_hash_entry *h,
7437 bfd_boolean force_local)
7438 {
7439 struct ppc_link_hash_entry *eh;
7440 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7441
7442 eh = (struct ppc_link_hash_entry *) h;
7443 if (eh->is_func_descriptor)
7444 {
7445 struct ppc_link_hash_entry *fh = eh->oh;
7446
7447 if (fh == NULL)
7448 {
7449 const char *p, *q;
7450 struct elf_link_hash_table *htab = elf_hash_table (info);
7451 char save;
7452
7453 /* We aren't supposed to use alloca in BFD because on
7454 systems which do not have alloca the version in libiberty
7455 calls xmalloc, which might cause the program to crash
7456 when it runs out of memory. This function doesn't have a
7457 return status, so there's no way to gracefully return an
7458 error. So cheat. We know that string[-1] can be safely
7459 accessed; It's either a string in an ELF string table,
7460 or allocated in an objalloc structure. */
7461
7462 p = eh->elf.root.root.string - 1;
7463 save = *p;
7464 *(char *) p = '.';
7465 fh = (struct ppc_link_hash_entry *)
7466 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7467 *(char *) p = save;
7468
7469 /* Unfortunately, if it so happens that the string we were
7470 looking for was allocated immediately before this string,
7471 then we overwrote the string terminator. That's the only
7472 reason the lookup should fail. */
7473 if (fh == NULL)
7474 {
7475 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7476 while (q >= eh->elf.root.root.string && *q == *p)
7477 --q, --p;
7478 if (q < eh->elf.root.root.string && *p == '.')
7479 fh = (struct ppc_link_hash_entry *)
7480 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7481 }
7482 if (fh != NULL)
7483 {
7484 eh->oh = fh;
7485 fh->oh = eh;
7486 }
7487 }
7488 if (fh != NULL)
7489 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7490 }
7491 }
7492
7493 static bfd_boolean
7494 get_sym_h (struct elf_link_hash_entry **hp,
7495 Elf_Internal_Sym **symp,
7496 asection **symsecp,
7497 unsigned char **tls_maskp,
7498 Elf_Internal_Sym **locsymsp,
7499 unsigned long r_symndx,
7500 bfd *ibfd)
7501 {
7502 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7503
7504 if (r_symndx >= symtab_hdr->sh_info)
7505 {
7506 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7507 struct elf_link_hash_entry *h;
7508
7509 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7510 h = elf_follow_link (h);
7511
7512 if (hp != NULL)
7513 *hp = h;
7514
7515 if (symp != NULL)
7516 *symp = NULL;
7517
7518 if (symsecp != NULL)
7519 {
7520 asection *symsec = NULL;
7521 if (h->root.type == bfd_link_hash_defined
7522 || h->root.type == bfd_link_hash_defweak)
7523 symsec = h->root.u.def.section;
7524 *symsecp = symsec;
7525 }
7526
7527 if (tls_maskp != NULL)
7528 {
7529 struct ppc_link_hash_entry *eh;
7530
7531 eh = (struct ppc_link_hash_entry *) h;
7532 *tls_maskp = &eh->tls_mask;
7533 }
7534 }
7535 else
7536 {
7537 Elf_Internal_Sym *sym;
7538 Elf_Internal_Sym *locsyms = *locsymsp;
7539
7540 if (locsyms == NULL)
7541 {
7542 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7543 if (locsyms == NULL)
7544 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7545 symtab_hdr->sh_info,
7546 0, NULL, NULL, NULL);
7547 if (locsyms == NULL)
7548 return FALSE;
7549 *locsymsp = locsyms;
7550 }
7551 sym = locsyms + r_symndx;
7552
7553 if (hp != NULL)
7554 *hp = NULL;
7555
7556 if (symp != NULL)
7557 *symp = sym;
7558
7559 if (symsecp != NULL)
7560 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7561
7562 if (tls_maskp != NULL)
7563 {
7564 struct got_entry **lgot_ents;
7565 unsigned char *tls_mask;
7566
7567 tls_mask = NULL;
7568 lgot_ents = elf_local_got_ents (ibfd);
7569 if (lgot_ents != NULL)
7570 {
7571 struct plt_entry **local_plt = (struct plt_entry **)
7572 (lgot_ents + symtab_hdr->sh_info);
7573 unsigned char *lgot_masks = (unsigned char *)
7574 (local_plt + symtab_hdr->sh_info);
7575 tls_mask = &lgot_masks[r_symndx];
7576 }
7577 *tls_maskp = tls_mask;
7578 }
7579 }
7580 return TRUE;
7581 }
7582
7583 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7584 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7585 type suitable for optimization, and 1 otherwise. */
7586
7587 static int
7588 get_tls_mask (unsigned char **tls_maskp,
7589 unsigned long *toc_symndx,
7590 bfd_vma *toc_addend,
7591 Elf_Internal_Sym **locsymsp,
7592 const Elf_Internal_Rela *rel,
7593 bfd *ibfd)
7594 {
7595 unsigned long r_symndx;
7596 int next_r;
7597 struct elf_link_hash_entry *h;
7598 Elf_Internal_Sym *sym;
7599 asection *sec;
7600 bfd_vma off;
7601
7602 r_symndx = ELF64_R_SYM (rel->r_info);
7603 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7604 return 0;
7605
7606 if ((*tls_maskp != NULL
7607 && (**tls_maskp & TLS_TLS) != 0
7608 && **tls_maskp != (TLS_TLS | TLS_MARK))
7609 || sec == NULL
7610 || ppc64_elf_section_data (sec) == NULL
7611 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7612 return 1;
7613
7614 /* Look inside a TOC section too. */
7615 if (h != NULL)
7616 {
7617 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7618 off = h->root.u.def.value;
7619 }
7620 else
7621 off = sym->st_value;
7622 off += rel->r_addend;
7623 BFD_ASSERT (off % 8 == 0);
7624 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7625 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7626 if (toc_symndx != NULL)
7627 *toc_symndx = r_symndx;
7628 if (toc_addend != NULL)
7629 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7630 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7631 return 0;
7632 if ((h == NULL || is_static_defined (h))
7633 && (next_r == -1 || next_r == -2))
7634 return 1 - next_r;
7635 return 1;
7636 }
7637
7638 /* Find (or create) an entry in the tocsave hash table. */
7639
7640 static struct tocsave_entry *
7641 tocsave_find (struct ppc_link_hash_table *htab,
7642 enum insert_option insert,
7643 Elf_Internal_Sym **local_syms,
7644 const Elf_Internal_Rela *irela,
7645 bfd *ibfd)
7646 {
7647 unsigned long r_indx;
7648 struct elf_link_hash_entry *h;
7649 Elf_Internal_Sym *sym;
7650 struct tocsave_entry ent, *p;
7651 hashval_t hash;
7652 struct tocsave_entry **slot;
7653
7654 r_indx = ELF64_R_SYM (irela->r_info);
7655 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7656 return NULL;
7657 if (ent.sec == NULL || ent.sec->output_section == NULL)
7658 {
7659 _bfd_error_handler
7660 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7661 return NULL;
7662 }
7663
7664 if (h != NULL)
7665 ent.offset = h->root.u.def.value;
7666 else
7667 ent.offset = sym->st_value;
7668 ent.offset += irela->r_addend;
7669
7670 hash = tocsave_htab_hash (&ent);
7671 slot = ((struct tocsave_entry **)
7672 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7673 if (slot == NULL)
7674 return NULL;
7675
7676 if (*slot == NULL)
7677 {
7678 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7679 if (p == NULL)
7680 return NULL;
7681 *p = ent;
7682 *slot = p;
7683 }
7684 return *slot;
7685 }
7686
7687 /* Adjust all global syms defined in opd sections. In gcc generated
7688 code for the old ABI, these will already have been done. */
7689
7690 static bfd_boolean
7691 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7692 {
7693 struct ppc_link_hash_entry *eh;
7694 asection *sym_sec;
7695 struct _opd_sec_data *opd;
7696
7697 if (h->root.type == bfd_link_hash_indirect)
7698 return TRUE;
7699
7700 if (h->root.type != bfd_link_hash_defined
7701 && h->root.type != bfd_link_hash_defweak)
7702 return TRUE;
7703
7704 eh = (struct ppc_link_hash_entry *) h;
7705 if (eh->adjust_done)
7706 return TRUE;
7707
7708 sym_sec = eh->elf.root.u.def.section;
7709 opd = get_opd_info (sym_sec);
7710 if (opd != NULL && opd->adjust != NULL)
7711 {
7712 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7713 if (adjust == -1)
7714 {
7715 /* This entry has been deleted. */
7716 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7717 if (dsec == NULL)
7718 {
7719 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7720 if (discarded_section (dsec))
7721 {
7722 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7723 break;
7724 }
7725 }
7726 eh->elf.root.u.def.value = 0;
7727 eh->elf.root.u.def.section = dsec;
7728 }
7729 else
7730 eh->elf.root.u.def.value += adjust;
7731 eh->adjust_done = 1;
7732 }
7733 return TRUE;
7734 }
7735
7736 /* Handles decrementing dynamic reloc counts for the reloc specified by
7737 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7738 have already been determined. */
7739
7740 static bfd_boolean
7741 dec_dynrel_count (bfd_vma r_info,
7742 asection *sec,
7743 struct bfd_link_info *info,
7744 Elf_Internal_Sym **local_syms,
7745 struct elf_link_hash_entry *h,
7746 Elf_Internal_Sym *sym)
7747 {
7748 enum elf_ppc64_reloc_type r_type;
7749 asection *sym_sec = NULL;
7750
7751 /* Can this reloc be dynamic? This switch, and later tests here
7752 should be kept in sync with the code in check_relocs. */
7753 r_type = ELF64_R_TYPE (r_info);
7754 switch (r_type)
7755 {
7756 default:
7757 return TRUE;
7758
7759 case R_PPC64_TPREL16:
7760 case R_PPC64_TPREL16_LO:
7761 case R_PPC64_TPREL16_HI:
7762 case R_PPC64_TPREL16_HA:
7763 case R_PPC64_TPREL16_DS:
7764 case R_PPC64_TPREL16_LO_DS:
7765 case R_PPC64_TPREL16_HIGH:
7766 case R_PPC64_TPREL16_HIGHA:
7767 case R_PPC64_TPREL16_HIGHER:
7768 case R_PPC64_TPREL16_HIGHERA:
7769 case R_PPC64_TPREL16_HIGHEST:
7770 case R_PPC64_TPREL16_HIGHESTA:
7771 case R_PPC64_TPREL64:
7772 case R_PPC64_DTPMOD64:
7773 case R_PPC64_DTPREL64:
7774 case R_PPC64_ADDR64:
7775 case R_PPC64_REL30:
7776 case R_PPC64_REL32:
7777 case R_PPC64_REL64:
7778 case R_PPC64_ADDR14:
7779 case R_PPC64_ADDR14_BRNTAKEN:
7780 case R_PPC64_ADDR14_BRTAKEN:
7781 case R_PPC64_ADDR16:
7782 case R_PPC64_ADDR16_DS:
7783 case R_PPC64_ADDR16_HA:
7784 case R_PPC64_ADDR16_HI:
7785 case R_PPC64_ADDR16_HIGH:
7786 case R_PPC64_ADDR16_HIGHA:
7787 case R_PPC64_ADDR16_HIGHER:
7788 case R_PPC64_ADDR16_HIGHERA:
7789 case R_PPC64_ADDR16_HIGHEST:
7790 case R_PPC64_ADDR16_HIGHESTA:
7791 case R_PPC64_ADDR16_LO:
7792 case R_PPC64_ADDR16_LO_DS:
7793 case R_PPC64_ADDR24:
7794 case R_PPC64_ADDR32:
7795 case R_PPC64_UADDR16:
7796 case R_PPC64_UADDR32:
7797 case R_PPC64_UADDR64:
7798 case R_PPC64_TOC:
7799 break;
7800 }
7801
7802 if (local_syms != NULL)
7803 {
7804 unsigned long r_symndx;
7805 bfd *ibfd = sec->owner;
7806
7807 r_symndx = ELF64_R_SYM (r_info);
7808 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7809 return FALSE;
7810 }
7811
7812 if ((bfd_link_pic (info)
7813 && (must_be_dyn_reloc (info, r_type)
7814 || (h != NULL
7815 && (!SYMBOLIC_BIND (info, h)
7816 || h->root.type == bfd_link_hash_defweak
7817 || !h->def_regular))))
7818 || (ELIMINATE_COPY_RELOCS
7819 && !bfd_link_pic (info)
7820 && h != NULL
7821 && (h->root.type == bfd_link_hash_defweak
7822 || !h->def_regular)))
7823 ;
7824 else
7825 return TRUE;
7826
7827 if (h != NULL)
7828 {
7829 struct elf_dyn_relocs *p;
7830 struct elf_dyn_relocs **pp;
7831 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7832
7833 /* elf_gc_sweep may have already removed all dyn relocs associated
7834 with local syms for a given section. Also, symbol flags are
7835 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7836 report a dynreloc miscount. */
7837 if (*pp == NULL && info->gc_sections)
7838 return TRUE;
7839
7840 while ((p = *pp) != NULL)
7841 {
7842 if (p->sec == sec)
7843 {
7844 if (!must_be_dyn_reloc (info, r_type))
7845 p->pc_count -= 1;
7846 p->count -= 1;
7847 if (p->count == 0)
7848 *pp = p->next;
7849 return TRUE;
7850 }
7851 pp = &p->next;
7852 }
7853 }
7854 else
7855 {
7856 struct ppc_dyn_relocs *p;
7857 struct ppc_dyn_relocs **pp;
7858 void *vpp;
7859 bfd_boolean is_ifunc;
7860
7861 if (local_syms == NULL)
7862 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7863 if (sym_sec == NULL)
7864 sym_sec = sec;
7865
7866 vpp = &elf_section_data (sym_sec)->local_dynrel;
7867 pp = (struct ppc_dyn_relocs **) vpp;
7868
7869 if (*pp == NULL && info->gc_sections)
7870 return TRUE;
7871
7872 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7873 while ((p = *pp) != NULL)
7874 {
7875 if (p->sec == sec && p->ifunc == is_ifunc)
7876 {
7877 p->count -= 1;
7878 if (p->count == 0)
7879 *pp = p->next;
7880 return TRUE;
7881 }
7882 pp = &p->next;
7883 }
7884 }
7885
7886 /* xgettext:c-format */
7887 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7888 sec->owner, sec);
7889 bfd_set_error (bfd_error_bad_value);
7890 return FALSE;
7891 }
7892
7893 /* Remove unused Official Procedure Descriptor entries. Currently we
7894 only remove those associated with functions in discarded link-once
7895 sections, or weakly defined functions that have been overridden. It
7896 would be possible to remove many more entries for statically linked
7897 applications. */
7898
7899 bfd_boolean
7900 ppc64_elf_edit_opd (struct bfd_link_info *info)
7901 {
7902 bfd *ibfd;
7903 bfd_boolean some_edited = FALSE;
7904 asection *need_pad = NULL;
7905 struct ppc_link_hash_table *htab;
7906
7907 htab = ppc_hash_table (info);
7908 if (htab == NULL)
7909 return FALSE;
7910
7911 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7912 {
7913 asection *sec;
7914 Elf_Internal_Rela *relstart, *rel, *relend;
7915 Elf_Internal_Shdr *symtab_hdr;
7916 Elf_Internal_Sym *local_syms;
7917 struct _opd_sec_data *opd;
7918 bfd_boolean need_edit, add_aux_fields, broken;
7919 bfd_size_type cnt_16b = 0;
7920
7921 if (!is_ppc64_elf (ibfd))
7922 continue;
7923
7924 sec = bfd_get_section_by_name (ibfd, ".opd");
7925 if (sec == NULL || sec->size == 0)
7926 continue;
7927
7928 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7929 continue;
7930
7931 if (sec->output_section == bfd_abs_section_ptr)
7932 continue;
7933
7934 /* Look through the section relocs. */
7935 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7936 continue;
7937
7938 local_syms = NULL;
7939 symtab_hdr = &elf_symtab_hdr (ibfd);
7940
7941 /* Read the relocations. */
7942 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7943 info->keep_memory);
7944 if (relstart == NULL)
7945 return FALSE;
7946
7947 /* First run through the relocs to check they are sane, and to
7948 determine whether we need to edit this opd section. */
7949 need_edit = FALSE;
7950 broken = FALSE;
7951 need_pad = sec;
7952 relend = relstart + sec->reloc_count;
7953 for (rel = relstart; rel < relend; )
7954 {
7955 enum elf_ppc64_reloc_type r_type;
7956 unsigned long r_symndx;
7957 asection *sym_sec;
7958 struct elf_link_hash_entry *h;
7959 Elf_Internal_Sym *sym;
7960 bfd_vma offset;
7961
7962 /* .opd contains an array of 16 or 24 byte entries. We're
7963 only interested in the reloc pointing to a function entry
7964 point. */
7965 offset = rel->r_offset;
7966 if (rel + 1 == relend
7967 || rel[1].r_offset != offset + 8)
7968 {
7969 /* If someone messes with .opd alignment then after a
7970 "ld -r" we might have padding in the middle of .opd.
7971 Also, there's nothing to prevent someone putting
7972 something silly in .opd with the assembler. No .opd
7973 optimization for them! */
7974 broken_opd:
7975 _bfd_error_handler
7976 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7977 broken = TRUE;
7978 break;
7979 }
7980
7981 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7982 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7983 {
7984 _bfd_error_handler
7985 /* xgettext:c-format */
7986 (_("%pB: unexpected reloc type %u in .opd section"),
7987 ibfd, r_type);
7988 broken = TRUE;
7989 break;
7990 }
7991
7992 r_symndx = ELF64_R_SYM (rel->r_info);
7993 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7994 r_symndx, ibfd))
7995 goto error_ret;
7996
7997 if (sym_sec == NULL || sym_sec->owner == NULL)
7998 {
7999 const char *sym_name;
8000 if (h != NULL)
8001 sym_name = h->root.root.string;
8002 else
8003 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8004 sym_sec);
8005
8006 _bfd_error_handler
8007 /* xgettext:c-format */
8008 (_("%pB: undefined sym `%s' in .opd section"),
8009 ibfd, sym_name);
8010 broken = TRUE;
8011 break;
8012 }
8013
8014 /* opd entries are always for functions defined in the
8015 current input bfd. If the symbol isn't defined in the
8016 input bfd, then we won't be using the function in this
8017 bfd; It must be defined in a linkonce section in another
8018 bfd, or is weak. It's also possible that we are
8019 discarding the function due to a linker script /DISCARD/,
8020 which we test for via the output_section. */
8021 if (sym_sec->owner != ibfd
8022 || sym_sec->output_section == bfd_abs_section_ptr)
8023 need_edit = TRUE;
8024
8025 rel += 2;
8026 if (rel + 1 == relend
8027 || (rel + 2 < relend
8028 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8029 ++rel;
8030
8031 if (rel == relend)
8032 {
8033 if (sec->size == offset + 24)
8034 {
8035 need_pad = NULL;
8036 break;
8037 }
8038 if (sec->size == offset + 16)
8039 {
8040 cnt_16b++;
8041 break;
8042 }
8043 goto broken_opd;
8044 }
8045 else if (rel + 1 < relend
8046 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8047 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8048 {
8049 if (rel[0].r_offset == offset + 16)
8050 cnt_16b++;
8051 else if (rel[0].r_offset != offset + 24)
8052 goto broken_opd;
8053 }
8054 else
8055 goto broken_opd;
8056 }
8057
8058 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8059
8060 if (!broken && (need_edit || add_aux_fields))
8061 {
8062 Elf_Internal_Rela *write_rel;
8063 Elf_Internal_Shdr *rel_hdr;
8064 bfd_byte *rptr, *wptr;
8065 bfd_byte *new_contents;
8066 bfd_size_type amt;
8067
8068 new_contents = NULL;
8069 amt = OPD_NDX (sec->size) * sizeof (long);
8070 opd = &ppc64_elf_section_data (sec)->u.opd;
8071 opd->adjust = bfd_zalloc (sec->owner, amt);
8072 if (opd->adjust == NULL)
8073 return FALSE;
8074
8075 /* This seems a waste of time as input .opd sections are all
8076 zeros as generated by gcc, but I suppose there's no reason
8077 this will always be so. We might start putting something in
8078 the third word of .opd entries. */
8079 if ((sec->flags & SEC_IN_MEMORY) == 0)
8080 {
8081 bfd_byte *loc;
8082 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8083 {
8084 if (loc != NULL)
8085 free (loc);
8086 error_ret:
8087 if (local_syms != NULL
8088 && symtab_hdr->contents != (unsigned char *) local_syms)
8089 free (local_syms);
8090 if (elf_section_data (sec)->relocs != relstart)
8091 free (relstart);
8092 return FALSE;
8093 }
8094 sec->contents = loc;
8095 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8096 }
8097
8098 elf_section_data (sec)->relocs = relstart;
8099
8100 new_contents = sec->contents;
8101 if (add_aux_fields)
8102 {
8103 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8104 if (new_contents == NULL)
8105 return FALSE;
8106 need_pad = NULL;
8107 }
8108 wptr = new_contents;
8109 rptr = sec->contents;
8110 write_rel = relstart;
8111 for (rel = relstart; rel < relend; )
8112 {
8113 unsigned long r_symndx;
8114 asection *sym_sec;
8115 struct elf_link_hash_entry *h;
8116 struct ppc_link_hash_entry *fdh = NULL;
8117 Elf_Internal_Sym *sym;
8118 long opd_ent_size;
8119 Elf_Internal_Rela *next_rel;
8120 bfd_boolean skip;
8121
8122 r_symndx = ELF64_R_SYM (rel->r_info);
8123 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8124 r_symndx, ibfd))
8125 goto error_ret;
8126
8127 next_rel = rel + 2;
8128 if (next_rel + 1 == relend
8129 || (next_rel + 2 < relend
8130 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8131 ++next_rel;
8132
8133 /* See if the .opd entry is full 24 byte or
8134 16 byte (with fd_aux entry overlapped with next
8135 fd_func). */
8136 opd_ent_size = 24;
8137 if (next_rel == relend)
8138 {
8139 if (sec->size == rel->r_offset + 16)
8140 opd_ent_size = 16;
8141 }
8142 else if (next_rel->r_offset == rel->r_offset + 16)
8143 opd_ent_size = 16;
8144
8145 if (h != NULL
8146 && h->root.root.string[0] == '.')
8147 {
8148 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8149 if (fdh != NULL)
8150 {
8151 fdh = ppc_follow_link (fdh);
8152 if (fdh->elf.root.type != bfd_link_hash_defined
8153 && fdh->elf.root.type != bfd_link_hash_defweak)
8154 fdh = NULL;
8155 }
8156 }
8157
8158 skip = (sym_sec->owner != ibfd
8159 || sym_sec->output_section == bfd_abs_section_ptr);
8160 if (skip)
8161 {
8162 if (fdh != NULL && sym_sec->owner == ibfd)
8163 {
8164 /* Arrange for the function descriptor sym
8165 to be dropped. */
8166 fdh->elf.root.u.def.value = 0;
8167 fdh->elf.root.u.def.section = sym_sec;
8168 }
8169 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8170
8171 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8172 rel = next_rel;
8173 else
8174 while (1)
8175 {
8176 if (!dec_dynrel_count (rel->r_info, sec, info,
8177 NULL, h, sym))
8178 goto error_ret;
8179
8180 if (++rel == next_rel)
8181 break;
8182
8183 r_symndx = ELF64_R_SYM (rel->r_info);
8184 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8185 r_symndx, ibfd))
8186 goto error_ret;
8187 }
8188 }
8189 else
8190 {
8191 /* We'll be keeping this opd entry. */
8192 long adjust;
8193
8194 if (fdh != NULL)
8195 {
8196 /* Redefine the function descriptor symbol to
8197 this location in the opd section. It is
8198 necessary to update the value here rather
8199 than using an array of adjustments as we do
8200 for local symbols, because various places
8201 in the generic ELF code use the value
8202 stored in u.def.value. */
8203 fdh->elf.root.u.def.value = wptr - new_contents;
8204 fdh->adjust_done = 1;
8205 }
8206
8207 /* Local syms are a bit tricky. We could
8208 tweak them as they can be cached, but
8209 we'd need to look through the local syms
8210 for the function descriptor sym which we
8211 don't have at the moment. So keep an
8212 array of adjustments. */
8213 adjust = (wptr - new_contents) - (rptr - sec->contents);
8214 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8215
8216 if (wptr != rptr)
8217 memcpy (wptr, rptr, opd_ent_size);
8218 wptr += opd_ent_size;
8219 if (add_aux_fields && opd_ent_size == 16)
8220 {
8221 memset (wptr, '\0', 8);
8222 wptr += 8;
8223 }
8224
8225 /* We need to adjust any reloc offsets to point to the
8226 new opd entries. */
8227 for ( ; rel != next_rel; ++rel)
8228 {
8229 rel->r_offset += adjust;
8230 if (write_rel != rel)
8231 memcpy (write_rel, rel, sizeof (*rel));
8232 ++write_rel;
8233 }
8234 }
8235
8236 rptr += opd_ent_size;
8237 }
8238
8239 sec->size = wptr - new_contents;
8240 sec->reloc_count = write_rel - relstart;
8241 if (add_aux_fields)
8242 {
8243 free (sec->contents);
8244 sec->contents = new_contents;
8245 }
8246
8247 /* Fudge the header size too, as this is used later in
8248 elf_bfd_final_link if we are emitting relocs. */
8249 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8250 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8251 some_edited = TRUE;
8252 }
8253 else if (elf_section_data (sec)->relocs != relstart)
8254 free (relstart);
8255
8256 if (local_syms != NULL
8257 && symtab_hdr->contents != (unsigned char *) local_syms)
8258 {
8259 if (!info->keep_memory)
8260 free (local_syms);
8261 else
8262 symtab_hdr->contents = (unsigned char *) local_syms;
8263 }
8264 }
8265
8266 if (some_edited)
8267 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8268
8269 /* If we are doing a final link and the last .opd entry is just 16 byte
8270 long, add a 8 byte padding after it. */
8271 if (need_pad != NULL && !bfd_link_relocatable (info))
8272 {
8273 bfd_byte *p;
8274
8275 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8276 {
8277 BFD_ASSERT (need_pad->size > 0);
8278
8279 p = bfd_malloc (need_pad->size + 8);
8280 if (p == NULL)
8281 return FALSE;
8282
8283 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8284 p, 0, need_pad->size))
8285 return FALSE;
8286
8287 need_pad->contents = p;
8288 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8289 }
8290 else
8291 {
8292 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8293 if (p == NULL)
8294 return FALSE;
8295
8296 need_pad->contents = p;
8297 }
8298
8299 memset (need_pad->contents + need_pad->size, 0, 8);
8300 need_pad->size += 8;
8301 }
8302
8303 return TRUE;
8304 }
8305
8306 /* Analyze inline PLT call relocations to see whether calls to locally
8307 defined functions can be converted to direct calls. */
8308
8309 bfd_boolean
8310 ppc64_elf_inline_plt (struct bfd_link_info *info)
8311 {
8312 struct ppc_link_hash_table *htab;
8313 bfd *ibfd;
8314 asection *sec;
8315 bfd_vma low_vma, high_vma, limit;
8316
8317 htab = ppc_hash_table (info);
8318 if (htab == NULL)
8319 return FALSE;
8320
8321 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8322 reduced somewhat to cater for possible stubs that might be added
8323 between the call and its destination. */
8324 if (htab->params->group_size < 0)
8325 {
8326 limit = -htab->params->group_size;
8327 if (limit == 1)
8328 limit = 0x1e00000;
8329 }
8330 else
8331 {
8332 limit = htab->params->group_size;
8333 if (limit == 1)
8334 limit = 0x1c00000;
8335 }
8336
8337 low_vma = -1;
8338 high_vma = 0;
8339 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8340 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8341 {
8342 if (low_vma > sec->vma)
8343 low_vma = sec->vma;
8344 if (high_vma < sec->vma + sec->size)
8345 high_vma = sec->vma + sec->size;
8346 }
8347
8348 /* If a "bl" can reach anywhere in local code sections, then we can
8349 convert all inline PLT sequences to direct calls when the symbol
8350 is local. */
8351 if (high_vma - low_vma < limit)
8352 {
8353 htab->can_convert_all_inline_plt = 1;
8354 return TRUE;
8355 }
8356
8357 /* Otherwise, go looking through relocs for cases where a direct
8358 call won't reach. Mark the symbol on any such reloc to disable
8359 the optimization and keep the PLT entry as it seems likely that
8360 this will be better than creating trampolines. Note that this
8361 will disable the optimization for all inline PLT calls to a
8362 particular symbol, not just those that won't reach. The
8363 difficulty in doing a more precise optimization is that the
8364 linker needs to make a decision depending on whether a
8365 particular R_PPC64_PLTCALL insn can be turned into a direct
8366 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8367 the sequence, and there is nothing that ties those relocs
8368 together except their symbol. */
8369
8370 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8371 {
8372 Elf_Internal_Shdr *symtab_hdr;
8373 Elf_Internal_Sym *local_syms;
8374
8375 if (!is_ppc64_elf (ibfd))
8376 continue;
8377
8378 local_syms = NULL;
8379 symtab_hdr = &elf_symtab_hdr (ibfd);
8380
8381 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8382 if (ppc64_elf_section_data (sec)->has_pltcall
8383 && !bfd_is_abs_section (sec->output_section))
8384 {
8385 Elf_Internal_Rela *relstart, *rel, *relend;
8386
8387 /* Read the relocations. */
8388 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8389 info->keep_memory);
8390 if (relstart == NULL)
8391 return FALSE;
8392
8393 relend = relstart + sec->reloc_count;
8394 for (rel = relstart; rel < relend; )
8395 {
8396 enum elf_ppc64_reloc_type r_type;
8397 unsigned long r_symndx;
8398 asection *sym_sec;
8399 struct elf_link_hash_entry *h;
8400 Elf_Internal_Sym *sym;
8401 unsigned char *tls_maskp;
8402
8403 r_type = ELF64_R_TYPE (rel->r_info);
8404 if (r_type != R_PPC64_PLTCALL)
8405 continue;
8406
8407 r_symndx = ELF64_R_SYM (rel->r_info);
8408 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8409 r_symndx, ibfd))
8410 {
8411 if (elf_section_data (sec)->relocs != relstart)
8412 free (relstart);
8413 if (local_syms != NULL
8414 && symtab_hdr->contents != (unsigned char *) local_syms)
8415 free (local_syms);
8416 return FALSE;
8417 }
8418
8419 if (sym_sec != NULL && sym_sec->output_section != NULL)
8420 {
8421 bfd_vma from, to;
8422 if (h != NULL)
8423 to = h->root.u.def.value;
8424 else
8425 to = sym->st_value;
8426 to += (rel->r_addend
8427 + sym_sec->output_offset
8428 + sym_sec->output_section->vma);
8429 from = (rel->r_offset
8430 + sec->output_offset
8431 + sec->output_section->vma);
8432 if (to - from + limit < 2 * limit)
8433 *tls_maskp &= ~PLT_KEEP;
8434 }
8435 }
8436 if (elf_section_data (sec)->relocs != relstart)
8437 free (relstart);
8438 }
8439
8440 if (local_syms != NULL
8441 && symtab_hdr->contents != (unsigned char *) local_syms)
8442 {
8443 if (!info->keep_memory)
8444 free (local_syms);
8445 else
8446 symtab_hdr->contents = (unsigned char *) local_syms;
8447 }
8448 }
8449
8450 return TRUE;
8451 }
8452
8453 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8454
8455 asection *
8456 ppc64_elf_tls_setup (struct bfd_link_info *info)
8457 {
8458 struct ppc_link_hash_table *htab;
8459
8460 htab = ppc_hash_table (info);
8461 if (htab == NULL)
8462 return NULL;
8463
8464 if (abiversion (info->output_bfd) == 1)
8465 htab->opd_abi = 1;
8466
8467 if (htab->params->no_multi_toc)
8468 htab->do_multi_toc = 0;
8469 else if (!htab->do_multi_toc)
8470 htab->params->no_multi_toc = 1;
8471
8472 /* Default to --no-plt-localentry, as this option can cause problems
8473 with symbol interposition. For example, glibc libpthread.so and
8474 libc.so duplicate many pthread symbols, with a fallback
8475 implementation in libc.so. In some cases the fallback does more
8476 work than the pthread implementation. __pthread_condattr_destroy
8477 is one such symbol: the libpthread.so implementation is
8478 localentry:0 while the libc.so implementation is localentry:8.
8479 An app that "cleverly" uses dlopen to only load necessary
8480 libraries at runtime may omit loading libpthread.so when not
8481 running multi-threaded, which then results in the libc.so
8482 fallback symbols being used and ld.so complaining. Now there
8483 are workarounds in ld (see non_zero_localentry) to detect the
8484 pthread situation, but that may not be the only case where
8485 --plt-localentry can cause trouble. */
8486 if (htab->params->plt_localentry0 < 0)
8487 htab->params->plt_localentry0 = 0;
8488 if (htab->params->plt_localentry0
8489 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8490 FALSE, FALSE, FALSE) == NULL)
8491 _bfd_error_handler
8492 (_("warning: --plt-localentry is especially dangerous without "
8493 "ld.so support to detect ABI violations"));
8494
8495 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8496 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8497 FALSE, FALSE, TRUE));
8498 /* Move dynamic linking info to the function descriptor sym. */
8499 if (htab->tls_get_addr != NULL)
8500 func_desc_adjust (&htab->tls_get_addr->elf, info);
8501 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8502 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8503 FALSE, FALSE, TRUE));
8504 if (htab->params->tls_get_addr_opt)
8505 {
8506 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8507
8508 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8509 FALSE, FALSE, TRUE);
8510 if (opt != NULL)
8511 func_desc_adjust (opt, info);
8512 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8513 FALSE, FALSE, TRUE);
8514 if (opt_fd != NULL
8515 && (opt_fd->root.type == bfd_link_hash_defined
8516 || opt_fd->root.type == bfd_link_hash_defweak))
8517 {
8518 /* If glibc supports an optimized __tls_get_addr call stub,
8519 signalled by the presence of __tls_get_addr_opt, and we'll
8520 be calling __tls_get_addr via a plt call stub, then
8521 make __tls_get_addr point to __tls_get_addr_opt. */
8522 tga_fd = &htab->tls_get_addr_fd->elf;
8523 if (htab->elf.dynamic_sections_created
8524 && tga_fd != NULL
8525 && (tga_fd->type == STT_FUNC
8526 || tga_fd->needs_plt)
8527 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8528 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8529 {
8530 struct plt_entry *ent;
8531
8532 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8533 if (ent->plt.refcount > 0)
8534 break;
8535 if (ent != NULL)
8536 {
8537 tga_fd->root.type = bfd_link_hash_indirect;
8538 tga_fd->root.u.i.link = &opt_fd->root;
8539 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8540 opt_fd->mark = 1;
8541 if (opt_fd->dynindx != -1)
8542 {
8543 /* Use __tls_get_addr_opt in dynamic relocations. */
8544 opt_fd->dynindx = -1;
8545 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8546 opt_fd->dynstr_index);
8547 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8548 return NULL;
8549 }
8550 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8551 tga = &htab->tls_get_addr->elf;
8552 if (opt != NULL && tga != NULL)
8553 {
8554 tga->root.type = bfd_link_hash_indirect;
8555 tga->root.u.i.link = &opt->root;
8556 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8557 opt->mark = 1;
8558 _bfd_elf_link_hash_hide_symbol (info, opt,
8559 tga->forced_local);
8560 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8561 }
8562 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8563 htab->tls_get_addr_fd->is_func_descriptor = 1;
8564 if (htab->tls_get_addr != NULL)
8565 {
8566 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8567 htab->tls_get_addr->is_func = 1;
8568 }
8569 }
8570 }
8571 }
8572 else if (htab->params->tls_get_addr_opt < 0)
8573 htab->params->tls_get_addr_opt = 0;
8574 }
8575 return _bfd_elf_tls_setup (info->output_bfd, info);
8576 }
8577
8578 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8579 HASH1 or HASH2. */
8580
8581 static bfd_boolean
8582 branch_reloc_hash_match (const bfd *ibfd,
8583 const Elf_Internal_Rela *rel,
8584 const struct ppc_link_hash_entry *hash1,
8585 const struct ppc_link_hash_entry *hash2)
8586 {
8587 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8588 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8589 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8590
8591 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8592 {
8593 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8594 struct elf_link_hash_entry *h;
8595
8596 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8597 h = elf_follow_link (h);
8598 if (h == &hash1->elf || h == &hash2->elf)
8599 return TRUE;
8600 }
8601 return FALSE;
8602 }
8603
8604 /* Run through all the TLS relocs looking for optimization
8605 opportunities. The linker has been hacked (see ppc64elf.em) to do
8606 a preliminary section layout so that we know the TLS segment
8607 offsets. We can't optimize earlier because some optimizations need
8608 to know the tp offset, and we need to optimize before allocating
8609 dynamic relocations. */
8610
8611 bfd_boolean
8612 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8613 {
8614 bfd *ibfd;
8615 asection *sec;
8616 struct ppc_link_hash_table *htab;
8617 unsigned char *toc_ref;
8618 int pass;
8619
8620 if (!bfd_link_executable (info))
8621 return TRUE;
8622
8623 htab = ppc_hash_table (info);
8624 if (htab == NULL)
8625 return FALSE;
8626
8627 /* Make two passes over the relocs. On the first pass, mark toc
8628 entries involved with tls relocs, and check that tls relocs
8629 involved in setting up a tls_get_addr call are indeed followed by
8630 such a call. If they are not, we can't do any tls optimization.
8631 On the second pass twiddle tls_mask flags to notify
8632 relocate_section that optimization can be done, and adjust got
8633 and plt refcounts. */
8634 toc_ref = NULL;
8635 for (pass = 0; pass < 2; ++pass)
8636 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8637 {
8638 Elf_Internal_Sym *locsyms = NULL;
8639 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8640
8641 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8642 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8643 {
8644 Elf_Internal_Rela *relstart, *rel, *relend;
8645 bfd_boolean found_tls_get_addr_arg = 0;
8646
8647 /* Read the relocations. */
8648 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8649 info->keep_memory);
8650 if (relstart == NULL)
8651 {
8652 free (toc_ref);
8653 return FALSE;
8654 }
8655
8656 relend = relstart + sec->reloc_count;
8657 for (rel = relstart; rel < relend; rel++)
8658 {
8659 enum elf_ppc64_reloc_type r_type;
8660 unsigned long r_symndx;
8661 struct elf_link_hash_entry *h;
8662 Elf_Internal_Sym *sym;
8663 asection *sym_sec;
8664 unsigned char *tls_mask;
8665 unsigned char tls_set, tls_clear, tls_type = 0;
8666 bfd_vma value;
8667 bfd_boolean ok_tprel, is_local;
8668 long toc_ref_index = 0;
8669 int expecting_tls_get_addr = 0;
8670 bfd_boolean ret = FALSE;
8671
8672 r_symndx = ELF64_R_SYM (rel->r_info);
8673 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8674 r_symndx, ibfd))
8675 {
8676 err_free_rel:
8677 if (elf_section_data (sec)->relocs != relstart)
8678 free (relstart);
8679 if (toc_ref != NULL)
8680 free (toc_ref);
8681 if (locsyms != NULL
8682 && (elf_symtab_hdr (ibfd).contents
8683 != (unsigned char *) locsyms))
8684 free (locsyms);
8685 return ret;
8686 }
8687
8688 if (h != NULL)
8689 {
8690 if (h->root.type == bfd_link_hash_defined
8691 || h->root.type == bfd_link_hash_defweak)
8692 value = h->root.u.def.value;
8693 else if (h->root.type == bfd_link_hash_undefweak)
8694 value = 0;
8695 else
8696 {
8697 found_tls_get_addr_arg = 0;
8698 continue;
8699 }
8700 }
8701 else
8702 /* Symbols referenced by TLS relocs must be of type
8703 STT_TLS. So no need for .opd local sym adjust. */
8704 value = sym->st_value;
8705
8706 ok_tprel = FALSE;
8707 is_local = FALSE;
8708 if (h == NULL
8709 || !h->def_dynamic)
8710 {
8711 is_local = TRUE;
8712 if (h != NULL
8713 && h->root.type == bfd_link_hash_undefweak)
8714 ok_tprel = TRUE;
8715 else if (sym_sec != NULL
8716 && sym_sec->output_section != NULL)
8717 {
8718 value += sym_sec->output_offset;
8719 value += sym_sec->output_section->vma;
8720 value -= htab->elf.tls_sec->vma;
8721 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8722 < (bfd_vma) 1 << 32);
8723 }
8724 }
8725
8726 r_type = ELF64_R_TYPE (rel->r_info);
8727 /* If this section has old-style __tls_get_addr calls
8728 without marker relocs, then check that each
8729 __tls_get_addr call reloc is preceded by a reloc
8730 that conceivably belongs to the __tls_get_addr arg
8731 setup insn. If we don't find matching arg setup
8732 relocs, don't do any tls optimization. */
8733 if (pass == 0
8734 && sec->has_tls_get_addr_call
8735 && h != NULL
8736 && (h == &htab->tls_get_addr->elf
8737 || h == &htab->tls_get_addr_fd->elf)
8738 && !found_tls_get_addr_arg
8739 && is_branch_reloc (r_type))
8740 {
8741 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8742 "TLS optimization disabled\n"),
8743 ibfd, sec, rel->r_offset);
8744 ret = TRUE;
8745 goto err_free_rel;
8746 }
8747
8748 found_tls_get_addr_arg = 0;
8749 switch (r_type)
8750 {
8751 case R_PPC64_GOT_TLSLD16:
8752 case R_PPC64_GOT_TLSLD16_LO:
8753 expecting_tls_get_addr = 1;
8754 found_tls_get_addr_arg = 1;
8755 /* Fall through. */
8756
8757 case R_PPC64_GOT_TLSLD16_HI:
8758 case R_PPC64_GOT_TLSLD16_HA:
8759 /* These relocs should never be against a symbol
8760 defined in a shared lib. Leave them alone if
8761 that turns out to be the case. */
8762 if (!is_local)
8763 continue;
8764
8765 /* LD -> LE */
8766 tls_set = 0;
8767 tls_clear = TLS_LD;
8768 tls_type = TLS_TLS | TLS_LD;
8769 break;
8770
8771 case R_PPC64_GOT_TLSGD16:
8772 case R_PPC64_GOT_TLSGD16_LO:
8773 expecting_tls_get_addr = 1;
8774 found_tls_get_addr_arg = 1;
8775 /* Fall through. */
8776
8777 case R_PPC64_GOT_TLSGD16_HI:
8778 case R_PPC64_GOT_TLSGD16_HA:
8779 if (ok_tprel)
8780 /* GD -> LE */
8781 tls_set = 0;
8782 else
8783 /* GD -> IE */
8784 tls_set = TLS_TLS | TLS_TPRELGD;
8785 tls_clear = TLS_GD;
8786 tls_type = TLS_TLS | TLS_GD;
8787 break;
8788
8789 case R_PPC64_GOT_TPREL16_DS:
8790 case R_PPC64_GOT_TPREL16_LO_DS:
8791 case R_PPC64_GOT_TPREL16_HI:
8792 case R_PPC64_GOT_TPREL16_HA:
8793 if (ok_tprel)
8794 {
8795 /* IE -> LE */
8796 tls_set = 0;
8797 tls_clear = TLS_TPREL;
8798 tls_type = TLS_TLS | TLS_TPREL;
8799 break;
8800 }
8801 continue;
8802
8803 case R_PPC64_TLSGD:
8804 case R_PPC64_TLSLD:
8805 if (rel + 1 < relend
8806 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8807 {
8808 if (pass != 0
8809 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8810 {
8811 r_symndx = ELF64_R_SYM (rel[1].r_info);
8812 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8813 r_symndx, ibfd))
8814 goto err_free_rel;
8815 if (h != NULL)
8816 {
8817 struct plt_entry *ent = NULL;
8818
8819 for (ent = h->plt.plist;
8820 ent != NULL;
8821 ent = ent->next)
8822 if (ent->addend == rel[1].r_addend)
8823 break;
8824
8825 if (ent != NULL
8826 && ent->plt.refcount > 0)
8827 ent->plt.refcount -= 1;
8828 }
8829 }
8830 continue;
8831 }
8832 found_tls_get_addr_arg = 1;
8833 /* Fall through. */
8834
8835 case R_PPC64_TLS:
8836 case R_PPC64_TOC16:
8837 case R_PPC64_TOC16_LO:
8838 if (sym_sec == NULL || sym_sec != toc)
8839 continue;
8840
8841 /* Mark this toc entry as referenced by a TLS
8842 code sequence. We can do that now in the
8843 case of R_PPC64_TLS, and after checking for
8844 tls_get_addr for the TOC16 relocs. */
8845 if (toc_ref == NULL)
8846 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8847 if (toc_ref == NULL)
8848 goto err_free_rel;
8849
8850 if (h != NULL)
8851 value = h->root.u.def.value;
8852 else
8853 value = sym->st_value;
8854 value += rel->r_addend;
8855 if (value % 8 != 0)
8856 continue;
8857 BFD_ASSERT (value < toc->size
8858 && toc->output_offset % 8 == 0);
8859 toc_ref_index = (value + toc->output_offset) / 8;
8860 if (r_type == R_PPC64_TLS
8861 || r_type == R_PPC64_TLSGD
8862 || r_type == R_PPC64_TLSLD)
8863 {
8864 toc_ref[toc_ref_index] = 1;
8865 continue;
8866 }
8867
8868 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8869 continue;
8870
8871 tls_set = 0;
8872 tls_clear = 0;
8873 expecting_tls_get_addr = 2;
8874 break;
8875
8876 case R_PPC64_TPREL64:
8877 if (pass == 0
8878 || sec != toc
8879 || toc_ref == NULL
8880 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8881 continue;
8882 if (ok_tprel)
8883 {
8884 /* IE -> LE */
8885 tls_set = TLS_EXPLICIT;
8886 tls_clear = TLS_TPREL;
8887 break;
8888 }
8889 continue;
8890
8891 case R_PPC64_DTPMOD64:
8892 if (pass == 0
8893 || sec != toc
8894 || toc_ref == NULL
8895 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8896 continue;
8897 if (rel + 1 < relend
8898 && (rel[1].r_info
8899 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8900 && rel[1].r_offset == rel->r_offset + 8)
8901 {
8902 if (ok_tprel)
8903 /* GD -> LE */
8904 tls_set = TLS_EXPLICIT | TLS_GD;
8905 else
8906 /* GD -> IE */
8907 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8908 tls_clear = TLS_GD;
8909 }
8910 else
8911 {
8912 if (!is_local)
8913 continue;
8914
8915 /* LD -> LE */
8916 tls_set = TLS_EXPLICIT;
8917 tls_clear = TLS_LD;
8918 }
8919 break;
8920
8921 default:
8922 continue;
8923 }
8924
8925 if (pass == 0)
8926 {
8927 if (!expecting_tls_get_addr
8928 || !sec->has_tls_get_addr_call)
8929 continue;
8930
8931 if (rel + 1 < relend
8932 && branch_reloc_hash_match (ibfd, rel + 1,
8933 htab->tls_get_addr,
8934 htab->tls_get_addr_fd))
8935 {
8936 if (expecting_tls_get_addr == 2)
8937 {
8938 /* Check for toc tls entries. */
8939 unsigned char *toc_tls;
8940 int retval;
8941
8942 retval = get_tls_mask (&toc_tls, NULL, NULL,
8943 &locsyms,
8944 rel, ibfd);
8945 if (retval == 0)
8946 goto err_free_rel;
8947 if (toc_tls != NULL)
8948 {
8949 if ((*toc_tls & TLS_TLS) != 0
8950 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8951 found_tls_get_addr_arg = 1;
8952 if (retval > 1)
8953 toc_ref[toc_ref_index] = 1;
8954 }
8955 }
8956 continue;
8957 }
8958
8959 /* Uh oh, we didn't find the expected call. We
8960 could just mark this symbol to exclude it
8961 from tls optimization but it's safer to skip
8962 the entire optimization. */
8963 /* xgettext:c-format */
8964 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8965 "TLS optimization disabled\n"),
8966 ibfd, sec, rel->r_offset);
8967 ret = TRUE;
8968 goto err_free_rel;
8969 }
8970
8971 /* If we don't have old-style __tls_get_addr calls
8972 without TLSGD/TLSLD marker relocs, and we haven't
8973 found a new-style __tls_get_addr call with a
8974 marker for this symbol, then we either have a
8975 broken object file or an -mlongcall style
8976 indirect call to __tls_get_addr without a marker.
8977 Disable optimization in this case. */
8978 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8979 && (tls_set & TLS_EXPLICIT) == 0
8980 && !sec->has_tls_get_addr_call
8981 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8982 != (TLS_TLS | TLS_MARK)))
8983 continue;
8984
8985 if (expecting_tls_get_addr)
8986 {
8987 struct plt_entry *ent = NULL;
8988
8989 if (htab->tls_get_addr != NULL)
8990 for (ent = htab->tls_get_addr->elf.plt.plist;
8991 ent != NULL;
8992 ent = ent->next)
8993 if (ent->addend == 0)
8994 break;
8995
8996 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8997 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8998 ent != NULL;
8999 ent = ent->next)
9000 if (ent->addend == 0)
9001 break;
9002
9003 if (ent != NULL
9004 && ent->plt.refcount > 0)
9005 ent->plt.refcount -= 1;
9006 }
9007
9008 if (tls_clear == 0)
9009 continue;
9010
9011 if ((tls_set & TLS_EXPLICIT) == 0)
9012 {
9013 struct got_entry *ent;
9014
9015 /* Adjust got entry for this reloc. */
9016 if (h != NULL)
9017 ent = h->got.glist;
9018 else
9019 ent = elf_local_got_ents (ibfd)[r_symndx];
9020
9021 for (; ent != NULL; ent = ent->next)
9022 if (ent->addend == rel->r_addend
9023 && ent->owner == ibfd
9024 && ent->tls_type == tls_type)
9025 break;
9026 if (ent == NULL)
9027 abort ();
9028
9029 if (tls_set == 0)
9030 {
9031 /* We managed to get rid of a got entry. */
9032 if (ent->got.refcount > 0)
9033 ent->got.refcount -= 1;
9034 }
9035 }
9036 else
9037 {
9038 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9039 we'll lose one or two dyn relocs. */
9040 if (!dec_dynrel_count (rel->r_info, sec, info,
9041 NULL, h, sym))
9042 return FALSE;
9043
9044 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9045 {
9046 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9047 NULL, h, sym))
9048 return FALSE;
9049 }
9050 }
9051
9052 *tls_mask |= tls_set;
9053 *tls_mask &= ~tls_clear;
9054 }
9055
9056 if (elf_section_data (sec)->relocs != relstart)
9057 free (relstart);
9058 }
9059
9060 if (locsyms != NULL
9061 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9062 {
9063 if (!info->keep_memory)
9064 free (locsyms);
9065 else
9066 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9067 }
9068 }
9069
9070 if (toc_ref != NULL)
9071 free (toc_ref);
9072 htab->do_tls_opt = 1;
9073 return TRUE;
9074 }
9075
9076 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9077 the values of any global symbols in a toc section that has been
9078 edited. Globals in toc sections should be a rarity, so this function
9079 sets a flag if any are found in toc sections other than the one just
9080 edited, so that further hash table traversals can be avoided. */
9081
9082 struct adjust_toc_info
9083 {
9084 asection *toc;
9085 unsigned long *skip;
9086 bfd_boolean global_toc_syms;
9087 };
9088
9089 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9090
9091 static bfd_boolean
9092 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9093 {
9094 struct ppc_link_hash_entry *eh;
9095 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9096 unsigned long i;
9097
9098 if (h->root.type != bfd_link_hash_defined
9099 && h->root.type != bfd_link_hash_defweak)
9100 return TRUE;
9101
9102 eh = (struct ppc_link_hash_entry *) h;
9103 if (eh->adjust_done)
9104 return TRUE;
9105
9106 if (eh->elf.root.u.def.section == toc_inf->toc)
9107 {
9108 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9109 i = toc_inf->toc->rawsize >> 3;
9110 else
9111 i = eh->elf.root.u.def.value >> 3;
9112
9113 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9114 {
9115 _bfd_error_handler
9116 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9117 do
9118 ++i;
9119 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9120 eh->elf.root.u.def.value = (bfd_vma) i << 3;
9121 }
9122
9123 eh->elf.root.u.def.value -= toc_inf->skip[i];
9124 eh->adjust_done = 1;
9125 }
9126 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9127 toc_inf->global_toc_syms = TRUE;
9128
9129 return TRUE;
9130 }
9131
9132 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9133 on a _LO variety toc/got reloc. */
9134
9135 static bfd_boolean
9136 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9137 {
9138 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9139 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9140 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9141 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9142 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9143 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9144 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9145 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9146 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9147 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9148 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9149 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9150 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9151 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9152 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9153 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9154 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9155 /* Exclude lfqu by testing reloc. If relocs are ever
9156 defined for the reduced D field in psq_lu then those
9157 will need testing too. */
9158 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9159 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9160 && (insn & 1) == 0)
9161 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9162 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9163 /* Exclude stfqu. psq_stu as above for psq_lu. */
9164 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9165 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9166 && (insn & 1) == 0));
9167 }
9168
9169 /* Examine all relocs referencing .toc sections in order to remove
9170 unused .toc entries. */
9171
9172 bfd_boolean
9173 ppc64_elf_edit_toc (struct bfd_link_info *info)
9174 {
9175 bfd *ibfd;
9176 struct adjust_toc_info toc_inf;
9177 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9178
9179 htab->do_toc_opt = 1;
9180 toc_inf.global_toc_syms = TRUE;
9181 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9182 {
9183 asection *toc, *sec;
9184 Elf_Internal_Shdr *symtab_hdr;
9185 Elf_Internal_Sym *local_syms;
9186 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9187 unsigned long *skip, *drop;
9188 unsigned char *used;
9189 unsigned char *keep, last, some_unused;
9190
9191 if (!is_ppc64_elf (ibfd))
9192 continue;
9193
9194 toc = bfd_get_section_by_name (ibfd, ".toc");
9195 if (toc == NULL
9196 || toc->size == 0
9197 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9198 || discarded_section (toc))
9199 continue;
9200
9201 toc_relocs = NULL;
9202 local_syms = NULL;
9203 symtab_hdr = &elf_symtab_hdr (ibfd);
9204
9205 /* Look at sections dropped from the final link. */
9206 skip = NULL;
9207 relstart = NULL;
9208 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9209 {
9210 if (sec->reloc_count == 0
9211 || !discarded_section (sec)
9212 || get_opd_info (sec)
9213 || (sec->flags & SEC_ALLOC) == 0
9214 || (sec->flags & SEC_DEBUGGING) != 0)
9215 continue;
9216
9217 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9218 if (relstart == NULL)
9219 goto error_ret;
9220
9221 /* Run through the relocs to see which toc entries might be
9222 unused. */
9223 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9224 {
9225 enum elf_ppc64_reloc_type r_type;
9226 unsigned long r_symndx;
9227 asection *sym_sec;
9228 struct elf_link_hash_entry *h;
9229 Elf_Internal_Sym *sym;
9230 bfd_vma val;
9231
9232 r_type = ELF64_R_TYPE (rel->r_info);
9233 switch (r_type)
9234 {
9235 default:
9236 continue;
9237
9238 case R_PPC64_TOC16:
9239 case R_PPC64_TOC16_LO:
9240 case R_PPC64_TOC16_HI:
9241 case R_PPC64_TOC16_HA:
9242 case R_PPC64_TOC16_DS:
9243 case R_PPC64_TOC16_LO_DS:
9244 break;
9245 }
9246
9247 r_symndx = ELF64_R_SYM (rel->r_info);
9248 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9249 r_symndx, ibfd))
9250 goto error_ret;
9251
9252 if (sym_sec != toc)
9253 continue;
9254
9255 if (h != NULL)
9256 val = h->root.u.def.value;
9257 else
9258 val = sym->st_value;
9259 val += rel->r_addend;
9260
9261 if (val >= toc->size)
9262 continue;
9263
9264 /* Anything in the toc ought to be aligned to 8 bytes.
9265 If not, don't mark as unused. */
9266 if (val & 7)
9267 continue;
9268
9269 if (skip == NULL)
9270 {
9271 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9272 if (skip == NULL)
9273 goto error_ret;
9274 }
9275
9276 skip[val >> 3] = ref_from_discarded;
9277 }
9278
9279 if (elf_section_data (sec)->relocs != relstart)
9280 free (relstart);
9281 }
9282
9283 /* For largetoc loads of address constants, we can convert
9284 . addis rx,2,addr@got@ha
9285 . ld ry,addr@got@l(rx)
9286 to
9287 . addis rx,2,addr@toc@ha
9288 . addi ry,rx,addr@toc@l
9289 when addr is within 2G of the toc pointer. This then means
9290 that the word storing "addr" in the toc is no longer needed. */
9291
9292 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9293 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9294 && toc->reloc_count != 0)
9295 {
9296 /* Read toc relocs. */
9297 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9298 info->keep_memory);
9299 if (toc_relocs == NULL)
9300 goto error_ret;
9301
9302 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9303 {
9304 enum elf_ppc64_reloc_type r_type;
9305 unsigned long r_symndx;
9306 asection *sym_sec;
9307 struct elf_link_hash_entry *h;
9308 Elf_Internal_Sym *sym;
9309 bfd_vma val, addr;
9310
9311 r_type = ELF64_R_TYPE (rel->r_info);
9312 if (r_type != R_PPC64_ADDR64)
9313 continue;
9314
9315 r_symndx = ELF64_R_SYM (rel->r_info);
9316 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9317 r_symndx, ibfd))
9318 goto error_ret;
9319
9320 if (sym_sec == NULL
9321 || sym_sec->output_section == NULL
9322 || discarded_section (sym_sec))
9323 continue;
9324
9325 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9326 continue;
9327
9328 if (h != NULL)
9329 {
9330 if (h->type == STT_GNU_IFUNC)
9331 continue;
9332 val = h->root.u.def.value;
9333 }
9334 else
9335 {
9336 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9337 continue;
9338 val = sym->st_value;
9339 }
9340 val += rel->r_addend;
9341 val += sym_sec->output_section->vma + sym_sec->output_offset;
9342
9343 /* We don't yet know the exact toc pointer value, but we
9344 know it will be somewhere in the toc section. Don't
9345 optimize if the difference from any possible toc
9346 pointer is outside [ff..f80008000, 7fff7fff]. */
9347 addr = toc->output_section->vma + TOC_BASE_OFF;
9348 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9349 continue;
9350
9351 addr = toc->output_section->vma + toc->output_section->rawsize;
9352 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9353 continue;
9354
9355 if (skip == NULL)
9356 {
9357 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9358 if (skip == NULL)
9359 goto error_ret;
9360 }
9361
9362 skip[rel->r_offset >> 3]
9363 |= can_optimize | ((rel - toc_relocs) << 2);
9364 }
9365 }
9366
9367 if (skip == NULL)
9368 continue;
9369
9370 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9371 if (used == NULL)
9372 {
9373 error_ret:
9374 if (local_syms != NULL
9375 && symtab_hdr->contents != (unsigned char *) local_syms)
9376 free (local_syms);
9377 if (sec != NULL
9378 && relstart != NULL
9379 && elf_section_data (sec)->relocs != relstart)
9380 free (relstart);
9381 if (toc_relocs != NULL
9382 && elf_section_data (toc)->relocs != toc_relocs)
9383 free (toc_relocs);
9384 if (skip != NULL)
9385 free (skip);
9386 return FALSE;
9387 }
9388
9389 /* Now check all kept sections that might reference the toc.
9390 Check the toc itself last. */
9391 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9392 : ibfd->sections);
9393 sec != NULL;
9394 sec = (sec == toc ? NULL
9395 : sec->next == NULL ? toc
9396 : sec->next == toc && toc->next ? toc->next
9397 : sec->next))
9398 {
9399 int repeat;
9400
9401 if (sec->reloc_count == 0
9402 || discarded_section (sec)
9403 || get_opd_info (sec)
9404 || (sec->flags & SEC_ALLOC) == 0
9405 || (sec->flags & SEC_DEBUGGING) != 0)
9406 continue;
9407
9408 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9409 info->keep_memory);
9410 if (relstart == NULL)
9411 {
9412 free (used);
9413 goto error_ret;
9414 }
9415
9416 /* Mark toc entries referenced as used. */
9417 do
9418 {
9419 repeat = 0;
9420 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9421 {
9422 enum elf_ppc64_reloc_type r_type;
9423 unsigned long r_symndx;
9424 asection *sym_sec;
9425 struct elf_link_hash_entry *h;
9426 Elf_Internal_Sym *sym;
9427 bfd_vma val;
9428 enum {no_check, check_lo, check_ha} insn_check;
9429
9430 r_type = ELF64_R_TYPE (rel->r_info);
9431 switch (r_type)
9432 {
9433 default:
9434 insn_check = no_check;
9435 break;
9436
9437 case R_PPC64_GOT_TLSLD16_HA:
9438 case R_PPC64_GOT_TLSGD16_HA:
9439 case R_PPC64_GOT_TPREL16_HA:
9440 case R_PPC64_GOT_DTPREL16_HA:
9441 case R_PPC64_GOT16_HA:
9442 case R_PPC64_TOC16_HA:
9443 insn_check = check_ha;
9444 break;
9445
9446 case R_PPC64_GOT_TLSLD16_LO:
9447 case R_PPC64_GOT_TLSGD16_LO:
9448 case R_PPC64_GOT_TPREL16_LO_DS:
9449 case R_PPC64_GOT_DTPREL16_LO_DS:
9450 case R_PPC64_GOT16_LO:
9451 case R_PPC64_GOT16_LO_DS:
9452 case R_PPC64_TOC16_LO:
9453 case R_PPC64_TOC16_LO_DS:
9454 insn_check = check_lo;
9455 break;
9456 }
9457
9458 if (insn_check != no_check)
9459 {
9460 bfd_vma off = rel->r_offset & ~3;
9461 unsigned char buf[4];
9462 unsigned int insn;
9463
9464 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9465 {
9466 free (used);
9467 goto error_ret;
9468 }
9469 insn = bfd_get_32 (ibfd, buf);
9470 if (insn_check == check_lo
9471 ? !ok_lo_toc_insn (insn, r_type)
9472 : ((insn & ((0x3f << 26) | 0x1f << 16))
9473 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9474 {
9475 char str[12];
9476
9477 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9478 sprintf (str, "%#08x", insn);
9479 info->callbacks->einfo
9480 /* xgettext:c-format */
9481 (_("%H: toc optimization is not supported for"
9482 " %s instruction\n"),
9483 ibfd, sec, rel->r_offset & ~3, str);
9484 }
9485 }
9486
9487 switch (r_type)
9488 {
9489 case R_PPC64_TOC16:
9490 case R_PPC64_TOC16_LO:
9491 case R_PPC64_TOC16_HI:
9492 case R_PPC64_TOC16_HA:
9493 case R_PPC64_TOC16_DS:
9494 case R_PPC64_TOC16_LO_DS:
9495 /* In case we're taking addresses of toc entries. */
9496 case R_PPC64_ADDR64:
9497 break;
9498
9499 default:
9500 continue;
9501 }
9502
9503 r_symndx = ELF64_R_SYM (rel->r_info);
9504 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9505 r_symndx, ibfd))
9506 {
9507 free (used);
9508 goto error_ret;
9509 }
9510
9511 if (sym_sec != toc)
9512 continue;
9513
9514 if (h != NULL)
9515 val = h->root.u.def.value;
9516 else
9517 val = sym->st_value;
9518 val += rel->r_addend;
9519
9520 if (val >= toc->size)
9521 continue;
9522
9523 if ((skip[val >> 3] & can_optimize) != 0)
9524 {
9525 bfd_vma off;
9526 unsigned char opc;
9527
9528 switch (r_type)
9529 {
9530 case R_PPC64_TOC16_HA:
9531 break;
9532
9533 case R_PPC64_TOC16_LO_DS:
9534 off = rel->r_offset;
9535 off += (bfd_big_endian (ibfd) ? -2 : 3);
9536 if (!bfd_get_section_contents (ibfd, sec, &opc,
9537 off, 1))
9538 {
9539 free (used);
9540 goto error_ret;
9541 }
9542 if ((opc & (0x3f << 2)) == (58u << 2))
9543 break;
9544 /* Fall through. */
9545
9546 default:
9547 /* Wrong sort of reloc, or not a ld. We may
9548 as well clear ref_from_discarded too. */
9549 skip[val >> 3] = 0;
9550 }
9551 }
9552
9553 if (sec != toc)
9554 used[val >> 3] = 1;
9555 /* For the toc section, we only mark as used if this
9556 entry itself isn't unused. */
9557 else if ((used[rel->r_offset >> 3]
9558 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9559 && !used[val >> 3])
9560 {
9561 /* Do all the relocs again, to catch reference
9562 chains. */
9563 repeat = 1;
9564 used[val >> 3] = 1;
9565 }
9566 }
9567 }
9568 while (repeat);
9569
9570 if (elf_section_data (sec)->relocs != relstart)
9571 free (relstart);
9572 }
9573
9574 /* Merge the used and skip arrays. Assume that TOC
9575 doublewords not appearing as either used or unused belong
9576 to an entry more than one doubleword in size. */
9577 for (drop = skip, keep = used, last = 0, some_unused = 0;
9578 drop < skip + (toc->size + 7) / 8;
9579 ++drop, ++keep)
9580 {
9581 if (*keep)
9582 {
9583 *drop &= ~ref_from_discarded;
9584 if ((*drop & can_optimize) != 0)
9585 some_unused = 1;
9586 last = 0;
9587 }
9588 else if ((*drop & ref_from_discarded) != 0)
9589 {
9590 some_unused = 1;
9591 last = ref_from_discarded;
9592 }
9593 else
9594 *drop = last;
9595 }
9596
9597 free (used);
9598
9599 if (some_unused)
9600 {
9601 bfd_byte *contents, *src;
9602 unsigned long off;
9603 Elf_Internal_Sym *sym;
9604 bfd_boolean local_toc_syms = FALSE;
9605
9606 /* Shuffle the toc contents, and at the same time convert the
9607 skip array from booleans into offsets. */
9608 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9609 goto error_ret;
9610
9611 elf_section_data (toc)->this_hdr.contents = contents;
9612
9613 for (src = contents, off = 0, drop = skip;
9614 src < contents + toc->size;
9615 src += 8, ++drop)
9616 {
9617 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9618 off += 8;
9619 else if (off != 0)
9620 {
9621 *drop = off;
9622 memcpy (src - off, src, 8);
9623 }
9624 }
9625 *drop = off;
9626 toc->rawsize = toc->size;
9627 toc->size = src - contents - off;
9628
9629 /* Adjust addends for relocs against the toc section sym,
9630 and optimize any accesses we can. */
9631 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9632 {
9633 if (sec->reloc_count == 0
9634 || discarded_section (sec))
9635 continue;
9636
9637 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9638 info->keep_memory);
9639 if (relstart == NULL)
9640 goto error_ret;
9641
9642 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9643 {
9644 enum elf_ppc64_reloc_type r_type;
9645 unsigned long r_symndx;
9646 asection *sym_sec;
9647 struct elf_link_hash_entry *h;
9648 bfd_vma val;
9649
9650 r_type = ELF64_R_TYPE (rel->r_info);
9651 switch (r_type)
9652 {
9653 default:
9654 continue;
9655
9656 case R_PPC64_TOC16:
9657 case R_PPC64_TOC16_LO:
9658 case R_PPC64_TOC16_HI:
9659 case R_PPC64_TOC16_HA:
9660 case R_PPC64_TOC16_DS:
9661 case R_PPC64_TOC16_LO_DS:
9662 case R_PPC64_ADDR64:
9663 break;
9664 }
9665
9666 r_symndx = ELF64_R_SYM (rel->r_info);
9667 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9668 r_symndx, ibfd))
9669 goto error_ret;
9670
9671 if (sym_sec != toc)
9672 continue;
9673
9674 if (h != NULL)
9675 val = h->root.u.def.value;
9676 else
9677 {
9678 val = sym->st_value;
9679 if (val != 0)
9680 local_toc_syms = TRUE;
9681 }
9682
9683 val += rel->r_addend;
9684
9685 if (val > toc->rawsize)
9686 val = toc->rawsize;
9687 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9688 continue;
9689 else if ((skip[val >> 3] & can_optimize) != 0)
9690 {
9691 Elf_Internal_Rela *tocrel
9692 = toc_relocs + (skip[val >> 3] >> 2);
9693 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9694
9695 switch (r_type)
9696 {
9697 case R_PPC64_TOC16_HA:
9698 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9699 break;
9700
9701 case R_PPC64_TOC16_LO_DS:
9702 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9703 break;
9704
9705 default:
9706 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9707 ppc_howto_init ();
9708 info->callbacks->einfo
9709 /* xgettext:c-format */
9710 (_("%H: %s references "
9711 "optimized away TOC entry\n"),
9712 ibfd, sec, rel->r_offset,
9713 ppc64_elf_howto_table[r_type]->name);
9714 bfd_set_error (bfd_error_bad_value);
9715 goto error_ret;
9716 }
9717 rel->r_addend = tocrel->r_addend;
9718 elf_section_data (sec)->relocs = relstart;
9719 continue;
9720 }
9721
9722 if (h != NULL || sym->st_value != 0)
9723 continue;
9724
9725 rel->r_addend -= skip[val >> 3];
9726 elf_section_data (sec)->relocs = relstart;
9727 }
9728
9729 if (elf_section_data (sec)->relocs != relstart)
9730 free (relstart);
9731 }
9732
9733 /* We shouldn't have local or global symbols defined in the TOC,
9734 but handle them anyway. */
9735 if (local_syms != NULL)
9736 for (sym = local_syms;
9737 sym < local_syms + symtab_hdr->sh_info;
9738 ++sym)
9739 if (sym->st_value != 0
9740 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9741 {
9742 unsigned long i;
9743
9744 if (sym->st_value > toc->rawsize)
9745 i = toc->rawsize >> 3;
9746 else
9747 i = sym->st_value >> 3;
9748
9749 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9750 {
9751 if (local_toc_syms)
9752 _bfd_error_handler
9753 (_("%s defined on removed toc entry"),
9754 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9755 do
9756 ++i;
9757 while ((skip[i] & (ref_from_discarded | can_optimize)));
9758 sym->st_value = (bfd_vma) i << 3;
9759 }
9760
9761 sym->st_value -= skip[i];
9762 symtab_hdr->contents = (unsigned char *) local_syms;
9763 }
9764
9765 /* Adjust any global syms defined in this toc input section. */
9766 if (toc_inf.global_toc_syms)
9767 {
9768 toc_inf.toc = toc;
9769 toc_inf.skip = skip;
9770 toc_inf.global_toc_syms = FALSE;
9771 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9772 &toc_inf);
9773 }
9774
9775 if (toc->reloc_count != 0)
9776 {
9777 Elf_Internal_Shdr *rel_hdr;
9778 Elf_Internal_Rela *wrel;
9779 bfd_size_type sz;
9780
9781 /* Remove unused toc relocs, and adjust those we keep. */
9782 if (toc_relocs == NULL)
9783 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9784 info->keep_memory);
9785 if (toc_relocs == NULL)
9786 goto error_ret;
9787
9788 wrel = toc_relocs;
9789 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9790 if ((skip[rel->r_offset >> 3]
9791 & (ref_from_discarded | can_optimize)) == 0)
9792 {
9793 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9794 wrel->r_info = rel->r_info;
9795 wrel->r_addend = rel->r_addend;
9796 ++wrel;
9797 }
9798 else if (!dec_dynrel_count (rel->r_info, toc, info,
9799 &local_syms, NULL, NULL))
9800 goto error_ret;
9801
9802 elf_section_data (toc)->relocs = toc_relocs;
9803 toc->reloc_count = wrel - toc_relocs;
9804 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9805 sz = rel_hdr->sh_entsize;
9806 rel_hdr->sh_size = toc->reloc_count * sz;
9807 }
9808 }
9809 else if (toc_relocs != NULL
9810 && elf_section_data (toc)->relocs != toc_relocs)
9811 free (toc_relocs);
9812
9813 if (local_syms != NULL
9814 && symtab_hdr->contents != (unsigned char *) local_syms)
9815 {
9816 if (!info->keep_memory)
9817 free (local_syms);
9818 else
9819 symtab_hdr->contents = (unsigned char *) local_syms;
9820 }
9821 free (skip);
9822 }
9823
9824 return TRUE;
9825 }
9826
9827 /* Return true iff input section I references the TOC using
9828 instructions limited to +/-32k offsets. */
9829
9830 bfd_boolean
9831 ppc64_elf_has_small_toc_reloc (asection *i)
9832 {
9833 return (is_ppc64_elf (i->owner)
9834 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9835 }
9836
9837 /* Allocate space for one GOT entry. */
9838
9839 static void
9840 allocate_got (struct elf_link_hash_entry *h,
9841 struct bfd_link_info *info,
9842 struct got_entry *gent)
9843 {
9844 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9845 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9846 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9847 ? 16 : 8);
9848 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9849 ? 2 : 1) * sizeof (Elf64_External_Rela);
9850 asection *got = ppc64_elf_tdata (gent->owner)->got;
9851
9852 gent->got.offset = got->size;
9853 got->size += entsize;
9854
9855 if (h->type == STT_GNU_IFUNC)
9856 {
9857 htab->elf.irelplt->size += rentsize;
9858 htab->got_reli_size += rentsize;
9859 }
9860 else if (((bfd_link_pic (info)
9861 && !((gent->tls_type & TLS_TPREL) != 0
9862 && bfd_link_executable (info)
9863 && SYMBOL_REFERENCES_LOCAL (info, h)))
9864 || (htab->elf.dynamic_sections_created
9865 && h->dynindx != -1
9866 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9867 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9868 {
9869 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9870 relgot->size += rentsize;
9871 }
9872 }
9873
9874 /* This function merges got entries in the same toc group. */
9875
9876 static void
9877 merge_got_entries (struct got_entry **pent)
9878 {
9879 struct got_entry *ent, *ent2;
9880
9881 for (ent = *pent; ent != NULL; ent = ent->next)
9882 if (!ent->is_indirect)
9883 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9884 if (!ent2->is_indirect
9885 && ent2->addend == ent->addend
9886 && ent2->tls_type == ent->tls_type
9887 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9888 {
9889 ent2->is_indirect = TRUE;
9890 ent2->got.ent = ent;
9891 }
9892 }
9893
9894 /* If H is undefined, make it dynamic if that makes sense. */
9895
9896 static bfd_boolean
9897 ensure_undef_dynamic (struct bfd_link_info *info,
9898 struct elf_link_hash_entry *h)
9899 {
9900 struct elf_link_hash_table *htab = elf_hash_table (info);
9901
9902 if (htab->dynamic_sections_created
9903 && ((info->dynamic_undefined_weak != 0
9904 && h->root.type == bfd_link_hash_undefweak)
9905 || h->root.type == bfd_link_hash_undefined)
9906 && h->dynindx == -1
9907 && !h->forced_local
9908 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9909 return bfd_elf_link_record_dynamic_symbol (info, h);
9910 return TRUE;
9911 }
9912
9913 /* Allocate space in .plt, .got and associated reloc sections for
9914 dynamic relocs. */
9915
9916 static bfd_boolean
9917 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9918 {
9919 struct bfd_link_info *info;
9920 struct ppc_link_hash_table *htab;
9921 asection *s;
9922 struct ppc_link_hash_entry *eh;
9923 struct got_entry **pgent, *gent;
9924
9925 if (h->root.type == bfd_link_hash_indirect)
9926 return TRUE;
9927
9928 info = (struct bfd_link_info *) inf;
9929 htab = ppc_hash_table (info);
9930 if (htab == NULL)
9931 return FALSE;
9932
9933 eh = (struct ppc_link_hash_entry *) h;
9934 /* Run through the TLS GD got entries first if we're changing them
9935 to TPREL. */
9936 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9937 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9938 if (gent->got.refcount > 0
9939 && (gent->tls_type & TLS_GD) != 0)
9940 {
9941 /* This was a GD entry that has been converted to TPREL. If
9942 there happens to be a TPREL entry we can use that one. */
9943 struct got_entry *ent;
9944 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9945 if (ent->got.refcount > 0
9946 && (ent->tls_type & TLS_TPREL) != 0
9947 && ent->addend == gent->addend
9948 && ent->owner == gent->owner)
9949 {
9950 gent->got.refcount = 0;
9951 break;
9952 }
9953
9954 /* If not, then we'll be using our own TPREL entry. */
9955 if (gent->got.refcount != 0)
9956 gent->tls_type = TLS_TLS | TLS_TPREL;
9957 }
9958
9959 /* Remove any list entry that won't generate a word in the GOT before
9960 we call merge_got_entries. Otherwise we risk merging to empty
9961 entries. */
9962 pgent = &h->got.glist;
9963 while ((gent = *pgent) != NULL)
9964 if (gent->got.refcount > 0)
9965 {
9966 if ((gent->tls_type & TLS_LD) != 0
9967 && !h->def_dynamic)
9968 {
9969 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9970 *pgent = gent->next;
9971 }
9972 else
9973 pgent = &gent->next;
9974 }
9975 else
9976 *pgent = gent->next;
9977
9978 if (!htab->do_multi_toc)
9979 merge_got_entries (&h->got.glist);
9980
9981 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9982 if (!gent->is_indirect)
9983 {
9984 /* Make sure this symbol is output as a dynamic symbol. */
9985 if (!ensure_undef_dynamic (info, h))
9986 return FALSE;
9987
9988 if (!is_ppc64_elf (gent->owner))
9989 abort ();
9990
9991 allocate_got (h, info, gent);
9992 }
9993
9994 /* If no dynamic sections we can't have dynamic relocs, except for
9995 IFUNCs which are handled even in static executables. */
9996 if (!htab->elf.dynamic_sections_created
9997 && h->type != STT_GNU_IFUNC)
9998 eh->dyn_relocs = NULL;
9999
10000 /* Discard relocs on undefined symbols that must be local. */
10001 else if (h->root.type == bfd_link_hash_undefined
10002 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10003 eh->dyn_relocs = NULL;
10004
10005 /* Also discard relocs on undefined weak syms with non-default
10006 visibility, or when dynamic_undefined_weak says so. */
10007 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10008 eh->dyn_relocs = NULL;
10009
10010 if (eh->dyn_relocs != NULL)
10011 {
10012 struct elf_dyn_relocs *p, **pp;
10013
10014 /* In the shared -Bsymbolic case, discard space allocated for
10015 dynamic pc-relative relocs against symbols which turn out to
10016 be defined in regular objects. For the normal shared case,
10017 discard space for relocs that have become local due to symbol
10018 visibility changes. */
10019
10020 if (bfd_link_pic (info))
10021 {
10022 /* Relocs that use pc_count are those that appear on a call
10023 insn, or certain REL relocs (see must_be_dyn_reloc) that
10024 can be generated via assembly. We want calls to
10025 protected symbols to resolve directly to the function
10026 rather than going via the plt. If people want function
10027 pointer comparisons to work as expected then they should
10028 avoid writing weird assembly. */
10029 if (SYMBOL_CALLS_LOCAL (info, h))
10030 {
10031 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10032 {
10033 p->count -= p->pc_count;
10034 p->pc_count = 0;
10035 if (p->count == 0)
10036 *pp = p->next;
10037 else
10038 pp = &p->next;
10039 }
10040 }
10041
10042 if (eh->dyn_relocs != NULL)
10043 {
10044 /* Make sure this symbol is output as a dynamic symbol. */
10045 if (!ensure_undef_dynamic (info, h))
10046 return FALSE;
10047 }
10048 }
10049 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10050 {
10051 /* For the non-pic case, discard space for relocs against
10052 symbols which turn out to need copy relocs or are not
10053 dynamic. */
10054 if (h->dynamic_adjusted
10055 && !h->def_regular
10056 && !ELF_COMMON_DEF_P (h))
10057 {
10058 /* Make sure this symbol is output as a dynamic symbol. */
10059 if (!ensure_undef_dynamic (info, h))
10060 return FALSE;
10061
10062 if (h->dynindx == -1)
10063 eh->dyn_relocs = NULL;
10064 }
10065 else
10066 eh->dyn_relocs = NULL;
10067 }
10068
10069 /* Finally, allocate space. */
10070 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10071 {
10072 asection *sreloc = elf_section_data (p->sec)->sreloc;
10073 if (eh->elf.type == STT_GNU_IFUNC)
10074 sreloc = htab->elf.irelplt;
10075 sreloc->size += p->count * sizeof (Elf64_External_Rela);
10076 }
10077 }
10078
10079 /* We might need a PLT entry when the symbol
10080 a) is dynamic, or
10081 b) is an ifunc, or
10082 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10083 d) has plt16 relocs and we are linking statically. */
10084 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10085 || h->type == STT_GNU_IFUNC
10086 || (h->needs_plt && h->dynamic_adjusted)
10087 || (h->needs_plt
10088 && h->def_regular
10089 && !htab->elf.dynamic_sections_created
10090 && !htab->can_convert_all_inline_plt
10091 && (((struct ppc_link_hash_entry *) h)->tls_mask
10092 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10093 {
10094 struct plt_entry *pent;
10095 bfd_boolean doneone = FALSE;
10096 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10097 if (pent->plt.refcount > 0)
10098 {
10099 if (!htab->elf.dynamic_sections_created
10100 || h->dynindx == -1)
10101 {
10102 if (h->type == STT_GNU_IFUNC)
10103 {
10104 s = htab->elf.iplt;
10105 pent->plt.offset = s->size;
10106 s->size += PLT_ENTRY_SIZE (htab);
10107 s = htab->elf.irelplt;
10108 }
10109 else
10110 {
10111 s = htab->pltlocal;
10112 pent->plt.offset = s->size;
10113 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10114 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10115 }
10116 }
10117 else
10118 {
10119 /* If this is the first .plt entry, make room for the special
10120 first entry. */
10121 s = htab->elf.splt;
10122 if (s->size == 0)
10123 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10124
10125 pent->plt.offset = s->size;
10126
10127 /* Make room for this entry. */
10128 s->size += PLT_ENTRY_SIZE (htab);
10129
10130 /* Make room for the .glink code. */
10131 s = htab->glink;
10132 if (s->size == 0)
10133 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10134 if (htab->opd_abi)
10135 {
10136 /* We need bigger stubs past index 32767. */
10137 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10138 s->size += 4;
10139 s->size += 2*4;
10140 }
10141 else
10142 s->size += 4;
10143
10144 /* We also need to make an entry in the .rela.plt section. */
10145 s = htab->elf.srelplt;
10146 }
10147 if (s != NULL)
10148 s->size += sizeof (Elf64_External_Rela);
10149 doneone = TRUE;
10150 }
10151 else
10152 pent->plt.offset = (bfd_vma) -1;
10153 if (!doneone)
10154 {
10155 h->plt.plist = NULL;
10156 h->needs_plt = 0;
10157 }
10158 }
10159 else
10160 {
10161 h->plt.plist = NULL;
10162 h->needs_plt = 0;
10163 }
10164
10165 return TRUE;
10166 }
10167
10168 #define PPC_LO(v) ((v) & 0xffff)
10169 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10170 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10171
10172 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10173 to set up space for global entry stubs. These are put in glink,
10174 after the branch table. */
10175
10176 static bfd_boolean
10177 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10178 {
10179 struct bfd_link_info *info;
10180 struct ppc_link_hash_table *htab;
10181 struct plt_entry *pent;
10182 asection *s, *plt;
10183
10184 if (h->root.type == bfd_link_hash_indirect)
10185 return TRUE;
10186
10187 if (!h->pointer_equality_needed)
10188 return TRUE;
10189
10190 if (h->def_regular)
10191 return TRUE;
10192
10193 info = inf;
10194 htab = ppc_hash_table (info);
10195 if (htab == NULL)
10196 return FALSE;
10197
10198 s = htab->global_entry;
10199 plt = htab->elf.splt;
10200 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10201 if (pent->plt.offset != (bfd_vma) -1
10202 && pent->addend == 0)
10203 {
10204 /* For ELFv2, if this symbol is not defined in a regular file
10205 and we are not generating a shared library or pie, then we
10206 need to define the symbol in the executable on a call stub.
10207 This is to avoid text relocations. */
10208 bfd_vma off, stub_align, stub_off, stub_size;
10209 unsigned int align_power;
10210
10211 stub_size = 16;
10212 stub_off = s->size;
10213 if (htab->params->plt_stub_align >= 0)
10214 align_power = htab->params->plt_stub_align;
10215 else
10216 align_power = -htab->params->plt_stub_align;
10217 /* Setting section alignment is delayed until we know it is
10218 non-empty. Otherwise the .text output section will be
10219 aligned at least to plt_stub_align even when no global
10220 entry stubs are needed. */
10221 if (s->alignment_power < align_power)
10222 s->alignment_power = align_power;
10223 stub_align = (bfd_vma) 1 << align_power;
10224 if (htab->params->plt_stub_align >= 0
10225 || ((((stub_off + stub_size - 1) & -stub_align)
10226 - (stub_off & -stub_align))
10227 > ((stub_size - 1) & -stub_align)))
10228 stub_off = (stub_off + stub_align - 1) & -stub_align;
10229 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10230 off -= stub_off + s->output_offset + s->output_section->vma;
10231 /* Note that for --plt-stub-align negative we have a possible
10232 dependency between stub offset and size. Break that
10233 dependency by assuming the max stub size when calculating
10234 the stub offset. */
10235 if (PPC_HA (off) == 0)
10236 stub_size -= 4;
10237 h->root.type = bfd_link_hash_defined;
10238 h->root.u.def.section = s;
10239 h->root.u.def.value = stub_off;
10240 s->size = stub_off + stub_size;
10241 break;
10242 }
10243 return TRUE;
10244 }
10245
10246 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10247 read-only sections. */
10248
10249 static bfd_boolean
10250 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10251 {
10252 asection *sec;
10253
10254 if (h->root.type == bfd_link_hash_indirect)
10255 return TRUE;
10256
10257 sec = readonly_dynrelocs (h);
10258 if (sec != NULL)
10259 {
10260 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10261
10262 info->flags |= DF_TEXTREL;
10263 info->callbacks->minfo
10264 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10265 sec->owner, h->root.root.string, sec);
10266
10267 /* Not an error, just cut short the traversal. */
10268 return FALSE;
10269 }
10270 return TRUE;
10271 }
10272
10273 /* Set the sizes of the dynamic sections. */
10274
10275 static bfd_boolean
10276 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10277 struct bfd_link_info *info)
10278 {
10279 struct ppc_link_hash_table *htab;
10280 bfd *dynobj;
10281 asection *s;
10282 bfd_boolean relocs;
10283 bfd *ibfd;
10284 struct got_entry *first_tlsld;
10285
10286 htab = ppc_hash_table (info);
10287 if (htab == NULL)
10288 return FALSE;
10289
10290 dynobj = htab->elf.dynobj;
10291 if (dynobj == NULL)
10292 abort ();
10293
10294 if (htab->elf.dynamic_sections_created)
10295 {
10296 /* Set the contents of the .interp section to the interpreter. */
10297 if (bfd_link_executable (info) && !info->nointerp)
10298 {
10299 s = bfd_get_linker_section (dynobj, ".interp");
10300 if (s == NULL)
10301 abort ();
10302 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10303 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10304 }
10305 }
10306
10307 /* Set up .got offsets for local syms, and space for local dynamic
10308 relocs. */
10309 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10310 {
10311 struct got_entry **lgot_ents;
10312 struct got_entry **end_lgot_ents;
10313 struct plt_entry **local_plt;
10314 struct plt_entry **end_local_plt;
10315 unsigned char *lgot_masks;
10316 bfd_size_type locsymcount;
10317 Elf_Internal_Shdr *symtab_hdr;
10318
10319 if (!is_ppc64_elf (ibfd))
10320 continue;
10321
10322 for (s = ibfd->sections; s != NULL; s = s->next)
10323 {
10324 struct ppc_dyn_relocs *p;
10325
10326 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10327 {
10328 if (!bfd_is_abs_section (p->sec)
10329 && bfd_is_abs_section (p->sec->output_section))
10330 {
10331 /* Input section has been discarded, either because
10332 it is a copy of a linkonce section or due to
10333 linker script /DISCARD/, so we'll be discarding
10334 the relocs too. */
10335 }
10336 else if (p->count != 0)
10337 {
10338 asection *srel = elf_section_data (p->sec)->sreloc;
10339 if (p->ifunc)
10340 srel = htab->elf.irelplt;
10341 srel->size += p->count * sizeof (Elf64_External_Rela);
10342 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10343 info->flags |= DF_TEXTREL;
10344 }
10345 }
10346 }
10347
10348 lgot_ents = elf_local_got_ents (ibfd);
10349 if (!lgot_ents)
10350 continue;
10351
10352 symtab_hdr = &elf_symtab_hdr (ibfd);
10353 locsymcount = symtab_hdr->sh_info;
10354 end_lgot_ents = lgot_ents + locsymcount;
10355 local_plt = (struct plt_entry **) end_lgot_ents;
10356 end_local_plt = local_plt + locsymcount;
10357 lgot_masks = (unsigned char *) end_local_plt;
10358 s = ppc64_elf_tdata (ibfd)->got;
10359 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10360 {
10361 struct got_entry **pent, *ent;
10362
10363 pent = lgot_ents;
10364 while ((ent = *pent) != NULL)
10365 if (ent->got.refcount > 0)
10366 {
10367 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10368 {
10369 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10370 *pent = ent->next;
10371 }
10372 else
10373 {
10374 unsigned int ent_size = 8;
10375 unsigned int rel_size = sizeof (Elf64_External_Rela);
10376
10377 ent->got.offset = s->size;
10378 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10379 {
10380 ent_size *= 2;
10381 rel_size *= 2;
10382 }
10383 s->size += ent_size;
10384 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10385 {
10386 htab->elf.irelplt->size += rel_size;
10387 htab->got_reli_size += rel_size;
10388 }
10389 else if (bfd_link_pic (info)
10390 && !((ent->tls_type & TLS_TPREL) != 0
10391 && bfd_link_executable (info)))
10392 {
10393 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10394 srel->size += rel_size;
10395 }
10396 pent = &ent->next;
10397 }
10398 }
10399 else
10400 *pent = ent->next;
10401 }
10402
10403 /* Allocate space for plt calls to local syms. */
10404 lgot_masks = (unsigned char *) end_local_plt;
10405 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10406 {
10407 struct plt_entry *ent;
10408
10409 for (ent = *local_plt; ent != NULL; ent = ent->next)
10410 if (ent->plt.refcount > 0)
10411 {
10412 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10413 {
10414 s = htab->elf.iplt;
10415 ent->plt.offset = s->size;
10416 s->size += PLT_ENTRY_SIZE (htab);
10417 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10418 }
10419 else if (htab->can_convert_all_inline_plt
10420 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10421 ent->plt.offset = (bfd_vma) -1;
10422 else
10423 {
10424 s = htab->pltlocal;
10425 ent->plt.offset = s->size;
10426 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10427 if (bfd_link_pic (info))
10428 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10429 }
10430 }
10431 else
10432 ent->plt.offset = (bfd_vma) -1;
10433 }
10434 }
10435
10436 /* Allocate global sym .plt and .got entries, and space for global
10437 sym dynamic relocs. */
10438 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10439
10440 if (!htab->opd_abi && !bfd_link_pic (info))
10441 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10442
10443 first_tlsld = NULL;
10444 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10445 {
10446 struct got_entry *ent;
10447
10448 if (!is_ppc64_elf (ibfd))
10449 continue;
10450
10451 ent = ppc64_tlsld_got (ibfd);
10452 if (ent->got.refcount > 0)
10453 {
10454 if (!htab->do_multi_toc && first_tlsld != NULL)
10455 {
10456 ent->is_indirect = TRUE;
10457 ent->got.ent = first_tlsld;
10458 }
10459 else
10460 {
10461 if (first_tlsld == NULL)
10462 first_tlsld = ent;
10463 s = ppc64_elf_tdata (ibfd)->got;
10464 ent->got.offset = s->size;
10465 ent->owner = ibfd;
10466 s->size += 16;
10467 if (bfd_link_pic (info))
10468 {
10469 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10470 srel->size += sizeof (Elf64_External_Rela);
10471 }
10472 }
10473 }
10474 else
10475 ent->got.offset = (bfd_vma) -1;
10476 }
10477
10478 /* We now have determined the sizes of the various dynamic sections.
10479 Allocate memory for them. */
10480 relocs = FALSE;
10481 for (s = dynobj->sections; s != NULL; s = s->next)
10482 {
10483 if ((s->flags & SEC_LINKER_CREATED) == 0)
10484 continue;
10485
10486 if (s == htab->brlt || s == htab->relbrlt)
10487 /* These haven't been allocated yet; don't strip. */
10488 continue;
10489 else if (s == htab->elf.sgot
10490 || s == htab->elf.splt
10491 || s == htab->elf.iplt
10492 || s == htab->pltlocal
10493 || s == htab->glink
10494 || s == htab->global_entry
10495 || s == htab->elf.sdynbss
10496 || s == htab->elf.sdynrelro)
10497 {
10498 /* Strip this section if we don't need it; see the
10499 comment below. */
10500 }
10501 else if (s == htab->glink_eh_frame)
10502 {
10503 if (!bfd_is_abs_section (s->output_section))
10504 /* Not sized yet. */
10505 continue;
10506 }
10507 else if (CONST_STRNEQ (s->name, ".rela"))
10508 {
10509 if (s->size != 0)
10510 {
10511 if (s != htab->elf.srelplt)
10512 relocs = TRUE;
10513
10514 /* We use the reloc_count field as a counter if we need
10515 to copy relocs into the output file. */
10516 s->reloc_count = 0;
10517 }
10518 }
10519 else
10520 {
10521 /* It's not one of our sections, so don't allocate space. */
10522 continue;
10523 }
10524
10525 if (s->size == 0)
10526 {
10527 /* If we don't need this section, strip it from the
10528 output file. This is mostly to handle .rela.bss and
10529 .rela.plt. We must create both sections in
10530 create_dynamic_sections, because they must be created
10531 before the linker maps input sections to output
10532 sections. The linker does that before
10533 adjust_dynamic_symbol is called, and it is that
10534 function which decides whether anything needs to go
10535 into these sections. */
10536 s->flags |= SEC_EXCLUDE;
10537 continue;
10538 }
10539
10540 if (bfd_is_abs_section (s->output_section))
10541 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10542 s->name);
10543
10544 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10545 continue;
10546
10547 /* Allocate memory for the section contents. We use bfd_zalloc
10548 here in case unused entries are not reclaimed before the
10549 section's contents are written out. This should not happen,
10550 but this way if it does we get a R_PPC64_NONE reloc in .rela
10551 sections instead of garbage.
10552 We also rely on the section contents being zero when writing
10553 the GOT and .dynrelro. */
10554 s->contents = bfd_zalloc (dynobj, s->size);
10555 if (s->contents == NULL)
10556 return FALSE;
10557 }
10558
10559 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10560 {
10561 if (!is_ppc64_elf (ibfd))
10562 continue;
10563
10564 s = ppc64_elf_tdata (ibfd)->got;
10565 if (s != NULL && s != htab->elf.sgot)
10566 {
10567 if (s->size == 0)
10568 s->flags |= SEC_EXCLUDE;
10569 else
10570 {
10571 s->contents = bfd_zalloc (ibfd, s->size);
10572 if (s->contents == NULL)
10573 return FALSE;
10574 }
10575 }
10576 s = ppc64_elf_tdata (ibfd)->relgot;
10577 if (s != NULL)
10578 {
10579 if (s->size == 0)
10580 s->flags |= SEC_EXCLUDE;
10581 else
10582 {
10583 s->contents = bfd_zalloc (ibfd, s->size);
10584 if (s->contents == NULL)
10585 return FALSE;
10586 relocs = TRUE;
10587 s->reloc_count = 0;
10588 }
10589 }
10590 }
10591
10592 if (htab->elf.dynamic_sections_created)
10593 {
10594 bfd_boolean tls_opt;
10595
10596 /* Add some entries to the .dynamic section. We fill in the
10597 values later, in ppc64_elf_finish_dynamic_sections, but we
10598 must add the entries now so that we get the correct size for
10599 the .dynamic section. The DT_DEBUG entry is filled in by the
10600 dynamic linker and used by the debugger. */
10601 #define add_dynamic_entry(TAG, VAL) \
10602 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10603
10604 if (bfd_link_executable (info))
10605 {
10606 if (!add_dynamic_entry (DT_DEBUG, 0))
10607 return FALSE;
10608 }
10609
10610 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10611 {
10612 if (!add_dynamic_entry (DT_PLTGOT, 0)
10613 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10614 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10615 || !add_dynamic_entry (DT_JMPREL, 0)
10616 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10617 return FALSE;
10618 }
10619
10620 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10621 {
10622 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10623 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10624 return FALSE;
10625 }
10626
10627 tls_opt = (htab->params->tls_get_addr_opt
10628 && htab->tls_get_addr_fd != NULL
10629 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10630 if (tls_opt || !htab->opd_abi)
10631 {
10632 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10633 return FALSE;
10634 }
10635
10636 if (relocs)
10637 {
10638 if (!add_dynamic_entry (DT_RELA, 0)
10639 || !add_dynamic_entry (DT_RELASZ, 0)
10640 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10641 return FALSE;
10642
10643 /* If any dynamic relocs apply to a read-only section,
10644 then we need a DT_TEXTREL entry. */
10645 if ((info->flags & DF_TEXTREL) == 0)
10646 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10647
10648 if ((info->flags & DF_TEXTREL) != 0)
10649 {
10650 if (!add_dynamic_entry (DT_TEXTREL, 0))
10651 return FALSE;
10652 }
10653 }
10654 }
10655 #undef add_dynamic_entry
10656
10657 return TRUE;
10658 }
10659
10660 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10661
10662 static bfd_boolean
10663 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10664 {
10665 if (h->plt.plist != NULL
10666 && !h->def_regular
10667 && !h->pointer_equality_needed)
10668 return FALSE;
10669
10670 return _bfd_elf_hash_symbol (h);
10671 }
10672
10673 /* Determine the type of stub needed, if any, for a call. */
10674
10675 static inline enum ppc_stub_type
10676 ppc_type_of_stub (asection *input_sec,
10677 const Elf_Internal_Rela *rel,
10678 struct ppc_link_hash_entry **hash,
10679 struct plt_entry **plt_ent,
10680 bfd_vma destination,
10681 unsigned long local_off)
10682 {
10683 struct ppc_link_hash_entry *h = *hash;
10684 bfd_vma location;
10685 bfd_vma branch_offset;
10686 bfd_vma max_branch_offset;
10687 enum elf_ppc64_reloc_type r_type;
10688
10689 if (h != NULL)
10690 {
10691 struct plt_entry *ent;
10692 struct ppc_link_hash_entry *fdh = h;
10693 if (h->oh != NULL
10694 && h->oh->is_func_descriptor)
10695 {
10696 fdh = ppc_follow_link (h->oh);
10697 *hash = fdh;
10698 }
10699
10700 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10701 if (ent->addend == rel->r_addend
10702 && ent->plt.offset != (bfd_vma) -1)
10703 {
10704 *plt_ent = ent;
10705 return ppc_stub_plt_call;
10706 }
10707
10708 /* Here, we know we don't have a plt entry. If we don't have a
10709 either a defined function descriptor or a defined entry symbol
10710 in a regular object file, then it is pointless trying to make
10711 any other type of stub. */
10712 if (!is_static_defined (&fdh->elf)
10713 && !is_static_defined (&h->elf))
10714 return ppc_stub_none;
10715 }
10716 else if (elf_local_got_ents (input_sec->owner) != NULL)
10717 {
10718 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10719 struct plt_entry **local_plt = (struct plt_entry **)
10720 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10721 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10722
10723 if (local_plt[r_symndx] != NULL)
10724 {
10725 struct plt_entry *ent;
10726
10727 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10728 if (ent->addend == rel->r_addend
10729 && ent->plt.offset != (bfd_vma) -1)
10730 {
10731 *plt_ent = ent;
10732 return ppc_stub_plt_call;
10733 }
10734 }
10735 }
10736
10737 /* Determine where the call point is. */
10738 location = (input_sec->output_offset
10739 + input_sec->output_section->vma
10740 + rel->r_offset);
10741
10742 branch_offset = destination - location;
10743 r_type = ELF64_R_TYPE (rel->r_info);
10744
10745 /* Determine if a long branch stub is needed. */
10746 max_branch_offset = 1 << 25;
10747 if (r_type == R_PPC64_REL14
10748 || r_type == R_PPC64_REL14_BRTAKEN
10749 || r_type == R_PPC64_REL14_BRNTAKEN)
10750 max_branch_offset = 1 << 15;
10751
10752 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10753 /* We need a stub. Figure out whether a long_branch or plt_branch
10754 is needed later. */
10755 return ppc_stub_long_branch;
10756
10757 return ppc_stub_none;
10758 }
10759
10760 /* With power7 weakly ordered memory model, it is possible for ld.so
10761 to update a plt entry in one thread and have another thread see a
10762 stale zero toc entry. To avoid this we need some sort of acquire
10763 barrier in the call stub. One solution is to make the load of the
10764 toc word seem to appear to depend on the load of the function entry
10765 word. Another solution is to test for r2 being zero, and branch to
10766 the appropriate glink entry if so.
10767
10768 . fake dep barrier compare
10769 . ld 12,xxx(2) ld 12,xxx(2)
10770 . mtctr 12 mtctr 12
10771 . xor 11,12,12 ld 2,xxx+8(2)
10772 . add 2,2,11 cmpldi 2,0
10773 . ld 2,xxx+8(2) bnectr+
10774 . bctr b <glink_entry>
10775
10776 The solution involving the compare turns out to be faster, so
10777 that's what we use unless the branch won't reach. */
10778
10779 #define ALWAYS_USE_FAKE_DEP 0
10780 #define ALWAYS_EMIT_R2SAVE 0
10781
10782 static inline unsigned int
10783 plt_stub_size (struct ppc_link_hash_table *htab,
10784 struct ppc_stub_hash_entry *stub_entry,
10785 bfd_vma off)
10786 {
10787 unsigned size = 12;
10788
10789 if (ALWAYS_EMIT_R2SAVE
10790 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10791 size += 4;
10792 if (PPC_HA (off) != 0)
10793 size += 4;
10794 if (htab->opd_abi)
10795 {
10796 size += 4;
10797 if (htab->params->plt_static_chain)
10798 size += 4;
10799 if (htab->params->plt_thread_safe
10800 && htab->elf.dynamic_sections_created
10801 && stub_entry->h != NULL
10802 && stub_entry->h->elf.dynindx != -1)
10803 size += 8;
10804 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10805 size += 4;
10806 }
10807 if (stub_entry->h != NULL
10808 && (stub_entry->h == htab->tls_get_addr_fd
10809 || stub_entry->h == htab->tls_get_addr)
10810 && htab->params->tls_get_addr_opt)
10811 {
10812 size += 7 * 4;
10813 if (ALWAYS_EMIT_R2SAVE
10814 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10815 size += 6 * 4;
10816 }
10817 return size;
10818 }
10819
10820 /* Depending on the sign of plt_stub_align:
10821 If positive, return the padding to align to a 2**plt_stub_align
10822 boundary.
10823 If negative, if this stub would cross fewer 2**plt_stub_align
10824 boundaries if we align, then return the padding needed to do so. */
10825
10826 static inline unsigned int
10827 plt_stub_pad (struct ppc_link_hash_table *htab,
10828 struct ppc_stub_hash_entry *stub_entry,
10829 bfd_vma plt_off)
10830 {
10831 int stub_align;
10832 unsigned stub_size;
10833 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10834
10835 if (htab->params->plt_stub_align >= 0)
10836 {
10837 stub_align = 1 << htab->params->plt_stub_align;
10838 if ((stub_off & (stub_align - 1)) != 0)
10839 return stub_align - (stub_off & (stub_align - 1));
10840 return 0;
10841 }
10842
10843 stub_align = 1 << -htab->params->plt_stub_align;
10844 stub_size = plt_stub_size (htab, stub_entry, plt_off);
10845 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10846 > ((stub_size - 1) & -stub_align))
10847 return stub_align - (stub_off & (stub_align - 1));
10848 return 0;
10849 }
10850
10851 /* Build a .plt call stub. */
10852
10853 static inline bfd_byte *
10854 build_plt_stub (struct ppc_link_hash_table *htab,
10855 struct ppc_stub_hash_entry *stub_entry,
10856 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10857 {
10858 bfd *obfd = htab->params->stub_bfd;
10859 bfd_boolean plt_load_toc = htab->opd_abi;
10860 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10861 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10862 && htab->elf.dynamic_sections_created
10863 && stub_entry->h != NULL
10864 && stub_entry->h->elf.dynindx != -1);
10865 bfd_boolean use_fake_dep = plt_thread_safe;
10866 bfd_vma cmp_branch_off = 0;
10867
10868 if (!ALWAYS_USE_FAKE_DEP
10869 && plt_load_toc
10870 && plt_thread_safe
10871 && !((stub_entry->h == htab->tls_get_addr_fd
10872 || stub_entry->h == htab->tls_get_addr)
10873 && htab->params->tls_get_addr_opt))
10874 {
10875 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10876 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10877 / PLT_ENTRY_SIZE (htab));
10878 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10879 bfd_vma to, from;
10880
10881 if (pltindex > 32768)
10882 glinkoff += (pltindex - 32768) * 4;
10883 to = (glinkoff
10884 + htab->glink->output_offset
10885 + htab->glink->output_section->vma);
10886 from = (p - stub_entry->group->stub_sec->contents
10887 + 4 * (ALWAYS_EMIT_R2SAVE
10888 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10889 + 4 * (PPC_HA (offset) != 0)
10890 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10891 != PPC_HA (offset))
10892 + 4 * (plt_static_chain != 0)
10893 + 20
10894 + stub_entry->group->stub_sec->output_offset
10895 + stub_entry->group->stub_sec->output_section->vma);
10896 cmp_branch_off = to - from;
10897 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10898 }
10899
10900 if (PPC_HA (offset) != 0)
10901 {
10902 if (r != NULL)
10903 {
10904 if (ALWAYS_EMIT_R2SAVE
10905 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10906 r[0].r_offset += 4;
10907 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10908 r[1].r_offset = r[0].r_offset + 4;
10909 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10910 r[1].r_addend = r[0].r_addend;
10911 if (plt_load_toc)
10912 {
10913 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10914 {
10915 r[2].r_offset = r[1].r_offset + 4;
10916 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10917 r[2].r_addend = r[0].r_addend;
10918 }
10919 else
10920 {
10921 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10922 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10923 r[2].r_addend = r[0].r_addend + 8;
10924 if (plt_static_chain)
10925 {
10926 r[3].r_offset = r[2].r_offset + 4;
10927 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10928 r[3].r_addend = r[0].r_addend + 16;
10929 }
10930 }
10931 }
10932 }
10933 if (ALWAYS_EMIT_R2SAVE
10934 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10935 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10936 if (plt_load_toc)
10937 {
10938 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10939 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10940 }
10941 else
10942 {
10943 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10944 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10945 }
10946 if (plt_load_toc
10947 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10948 {
10949 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10950 offset = 0;
10951 }
10952 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10953 if (plt_load_toc)
10954 {
10955 if (use_fake_dep)
10956 {
10957 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10958 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10959 }
10960 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10961 if (plt_static_chain)
10962 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10963 }
10964 }
10965 else
10966 {
10967 if (r != NULL)
10968 {
10969 if (ALWAYS_EMIT_R2SAVE
10970 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10971 r[0].r_offset += 4;
10972 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10973 if (plt_load_toc)
10974 {
10975 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10976 {
10977 r[1].r_offset = r[0].r_offset + 4;
10978 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10979 r[1].r_addend = r[0].r_addend;
10980 }
10981 else
10982 {
10983 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10984 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10985 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10986 if (plt_static_chain)
10987 {
10988 r[2].r_offset = r[1].r_offset + 4;
10989 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10990 r[2].r_addend = r[0].r_addend + 8;
10991 }
10992 }
10993 }
10994 }
10995 if (ALWAYS_EMIT_R2SAVE
10996 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10997 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10998 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10999 if (plt_load_toc
11000 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11001 {
11002 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11003 offset = 0;
11004 }
11005 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11006 if (plt_load_toc)
11007 {
11008 if (use_fake_dep)
11009 {
11010 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11011 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11012 }
11013 if (plt_static_chain)
11014 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11015 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11016 }
11017 }
11018 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11019 {
11020 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11021 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11022 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11023 }
11024 else
11025 bfd_put_32 (obfd, BCTR, p), p += 4;
11026 return p;
11027 }
11028
11029 /* Build a special .plt call stub for __tls_get_addr. */
11030
11031 #define LD_R11_0R3 0xe9630000
11032 #define LD_R12_0R3 0xe9830000
11033 #define MR_R0_R3 0x7c601b78
11034 #define CMPDI_R11_0 0x2c2b0000
11035 #define ADD_R3_R12_R13 0x7c6c6a14
11036 #define BEQLR 0x4d820020
11037 #define MR_R3_R0 0x7c030378
11038 #define STD_R11_0R1 0xf9610000
11039 #define BCTRL 0x4e800421
11040 #define LD_R11_0R1 0xe9610000
11041 #define MTLR_R11 0x7d6803a6
11042
11043 static inline bfd_byte *
11044 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11045 struct ppc_stub_hash_entry *stub_entry,
11046 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11047 {
11048 bfd *obfd = htab->params->stub_bfd;
11049
11050 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11051 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11052 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11053 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11054 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11055 bfd_put_32 (obfd, BEQLR, p), p += 4;
11056 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11057 if (r != NULL)
11058 r[0].r_offset += 7 * 4;
11059 if (!ALWAYS_EMIT_R2SAVE
11060 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11061 return build_plt_stub (htab, stub_entry, p, offset, r);
11062
11063 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
11064 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11065
11066 if (r != NULL)
11067 r[0].r_offset += 2 * 4;
11068 p = build_plt_stub (htab, stub_entry, p, offset, r);
11069 bfd_put_32 (obfd, BCTRL, p - 4);
11070
11071 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
11072 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11073 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11074 bfd_put_32 (obfd, BLR, p), p += 4;
11075
11076 return p;
11077 }
11078
11079 static Elf_Internal_Rela *
11080 get_relocs (asection *sec, int count)
11081 {
11082 Elf_Internal_Rela *relocs;
11083 struct bfd_elf_section_data *elfsec_data;
11084
11085 elfsec_data = elf_section_data (sec);
11086 relocs = elfsec_data->relocs;
11087 if (relocs == NULL)
11088 {
11089 bfd_size_type relsize;
11090 relsize = sec->reloc_count * sizeof (*relocs);
11091 relocs = bfd_alloc (sec->owner, relsize);
11092 if (relocs == NULL)
11093 return NULL;
11094 elfsec_data->relocs = relocs;
11095 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11096 sizeof (Elf_Internal_Shdr));
11097 if (elfsec_data->rela.hdr == NULL)
11098 return NULL;
11099 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11100 * sizeof (Elf64_External_Rela));
11101 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11102 sec->reloc_count = 0;
11103 }
11104 relocs += sec->reloc_count;
11105 sec->reloc_count += count;
11106 return relocs;
11107 }
11108
11109 static bfd_vma
11110 get_r2off (struct bfd_link_info *info,
11111 struct ppc_stub_hash_entry *stub_entry)
11112 {
11113 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11114 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11115
11116 if (r2off == 0)
11117 {
11118 /* Support linking -R objects. Get the toc pointer from the
11119 opd entry. */
11120 char buf[8];
11121 if (!htab->opd_abi)
11122 return r2off;
11123 asection *opd = stub_entry->h->elf.root.u.def.section;
11124 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11125
11126 if (strcmp (opd->name, ".opd") != 0
11127 || opd->reloc_count != 0)
11128 {
11129 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11130 stub_entry->h->elf.root.root.string);
11131 bfd_set_error (bfd_error_bad_value);
11132 return (bfd_vma) -1;
11133 }
11134 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11135 return (bfd_vma) -1;
11136 r2off = bfd_get_64 (opd->owner, buf);
11137 r2off -= elf_gp (info->output_bfd);
11138 }
11139 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11140 return r2off;
11141 }
11142
11143 static bfd_boolean
11144 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11145 {
11146 struct ppc_stub_hash_entry *stub_entry;
11147 struct ppc_branch_hash_entry *br_entry;
11148 struct bfd_link_info *info;
11149 struct ppc_link_hash_table *htab;
11150 bfd_byte *loc;
11151 bfd_byte *p;
11152 bfd_vma targ, off;
11153 Elf_Internal_Rela *r;
11154 asection *plt;
11155
11156 /* Massage our args to the form they really have. */
11157 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11158 info = in_arg;
11159
11160 htab = ppc_hash_table (info);
11161 if (htab == NULL)
11162 return FALSE;
11163
11164 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11165 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11166
11167 htab->stub_count[stub_entry->stub_type - 1] += 1;
11168 switch (stub_entry->stub_type)
11169 {
11170 case ppc_stub_long_branch:
11171 case ppc_stub_long_branch_r2off:
11172 /* Branches are relative. This is where we are going to. */
11173 targ = (stub_entry->target_value
11174 + stub_entry->target_section->output_offset
11175 + stub_entry->target_section->output_section->vma);
11176 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11177
11178 /* And this is where we are coming from. */
11179 off = (stub_entry->stub_offset
11180 + stub_entry->group->stub_sec->output_offset
11181 + stub_entry->group->stub_sec->output_section->vma);
11182 off = targ - off;
11183
11184 p = loc;
11185 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11186 {
11187 bfd_vma r2off = get_r2off (info, stub_entry);
11188
11189 if (r2off == (bfd_vma) -1)
11190 {
11191 htab->stub_error = TRUE;
11192 return FALSE;
11193 }
11194 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11195 p += 4;
11196 if (PPC_HA (r2off) != 0)
11197 {
11198 bfd_put_32 (htab->params->stub_bfd,
11199 ADDIS_R2_R2 | PPC_HA (r2off), p);
11200 p += 4;
11201 }
11202 if (PPC_LO (r2off) != 0)
11203 {
11204 bfd_put_32 (htab->params->stub_bfd,
11205 ADDI_R2_R2 | PPC_LO (r2off), p);
11206 p += 4;
11207 }
11208 off -= p - loc;
11209 }
11210 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11211 p += 4;
11212
11213 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11214 {
11215 _bfd_error_handler
11216 (_("long branch stub `%s' offset overflow"),
11217 stub_entry->root.string);
11218 htab->stub_error = TRUE;
11219 return FALSE;
11220 }
11221
11222 if (info->emitrelocations)
11223 {
11224 r = get_relocs (stub_entry->group->stub_sec, 1);
11225 if (r == NULL)
11226 return FALSE;
11227 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11228 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11229 r->r_addend = targ;
11230 if (stub_entry->h != NULL)
11231 {
11232 struct elf_link_hash_entry **hashes;
11233 unsigned long symndx;
11234 struct ppc_link_hash_entry *h;
11235
11236 hashes = elf_sym_hashes (htab->params->stub_bfd);
11237 if (hashes == NULL)
11238 {
11239 bfd_size_type hsize;
11240
11241 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11242 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11243 if (hashes == NULL)
11244 return FALSE;
11245 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11246 htab->stub_globals = 1;
11247 }
11248 symndx = htab->stub_globals++;
11249 h = stub_entry->h;
11250 hashes[symndx] = &h->elf;
11251 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11252 if (h->oh != NULL && h->oh->is_func)
11253 h = ppc_follow_link (h->oh);
11254 if (h->elf.root.u.def.section != stub_entry->target_section)
11255 /* H is an opd symbol. The addend must be zero. */
11256 r->r_addend = 0;
11257 else
11258 {
11259 off = (h->elf.root.u.def.value
11260 + h->elf.root.u.def.section->output_offset
11261 + h->elf.root.u.def.section->output_section->vma);
11262 r->r_addend -= off;
11263 }
11264 }
11265 }
11266 break;
11267
11268 case ppc_stub_plt_branch:
11269 case ppc_stub_plt_branch_r2off:
11270 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11271 stub_entry->root.string + 9,
11272 FALSE, FALSE);
11273 if (br_entry == NULL)
11274 {
11275 _bfd_error_handler (_("can't find branch stub `%s'"),
11276 stub_entry->root.string);
11277 htab->stub_error = TRUE;
11278 return FALSE;
11279 }
11280
11281 targ = (stub_entry->target_value
11282 + stub_entry->target_section->output_offset
11283 + stub_entry->target_section->output_section->vma);
11284 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11285 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11286
11287 bfd_put_64 (htab->brlt->owner, targ,
11288 htab->brlt->contents + br_entry->offset);
11289
11290 if (br_entry->iter == htab->stub_iteration)
11291 {
11292 br_entry->iter = 0;
11293
11294 if (htab->relbrlt != NULL)
11295 {
11296 /* Create a reloc for the branch lookup table entry. */
11297 Elf_Internal_Rela rela;
11298 bfd_byte *rl;
11299
11300 rela.r_offset = (br_entry->offset
11301 + htab->brlt->output_offset
11302 + htab->brlt->output_section->vma);
11303 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11304 rela.r_addend = targ;
11305
11306 rl = htab->relbrlt->contents;
11307 rl += (htab->relbrlt->reloc_count++
11308 * sizeof (Elf64_External_Rela));
11309 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11310 }
11311 else if (info->emitrelocations)
11312 {
11313 r = get_relocs (htab->brlt, 1);
11314 if (r == NULL)
11315 return FALSE;
11316 /* brlt, being SEC_LINKER_CREATED does not go through the
11317 normal reloc processing. Symbols and offsets are not
11318 translated from input file to output file form, so
11319 set up the offset per the output file. */
11320 r->r_offset = (br_entry->offset
11321 + htab->brlt->output_offset
11322 + htab->brlt->output_section->vma);
11323 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11324 r->r_addend = targ;
11325 }
11326 }
11327
11328 targ = (br_entry->offset
11329 + htab->brlt->output_offset
11330 + htab->brlt->output_section->vma);
11331
11332 off = (elf_gp (info->output_bfd)
11333 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11334 off = targ - off;
11335
11336 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11337 {
11338 info->callbacks->einfo
11339 (_("%P: linkage table error against `%pT'\n"),
11340 stub_entry->root.string);
11341 bfd_set_error (bfd_error_bad_value);
11342 htab->stub_error = TRUE;
11343 return FALSE;
11344 }
11345
11346 if (info->emitrelocations)
11347 {
11348 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11349 if (r == NULL)
11350 return FALSE;
11351 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11352 if (bfd_big_endian (info->output_bfd))
11353 r[0].r_offset += 2;
11354 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11355 r[0].r_offset += 4;
11356 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11357 r[0].r_addend = targ;
11358 if (PPC_HA (off) != 0)
11359 {
11360 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11361 r[1].r_offset = r[0].r_offset + 4;
11362 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11363 r[1].r_addend = r[0].r_addend;
11364 }
11365 }
11366
11367 p = loc;
11368 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11369 {
11370 if (PPC_HA (off) != 0)
11371 {
11372 bfd_put_32 (htab->params->stub_bfd,
11373 ADDIS_R12_R2 | PPC_HA (off), p);
11374 p += 4;
11375 bfd_put_32 (htab->params->stub_bfd,
11376 LD_R12_0R12 | PPC_LO (off), p);
11377 }
11378 else
11379 bfd_put_32 (htab->params->stub_bfd,
11380 LD_R12_0R2 | PPC_LO (off), p);
11381 }
11382 else
11383 {
11384 bfd_vma r2off = get_r2off (info, stub_entry);
11385
11386 if (r2off == (bfd_vma) -1)
11387 {
11388 htab->stub_error = TRUE;
11389 return FALSE;
11390 }
11391
11392 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11393 p += 4;
11394 if (PPC_HA (off) != 0)
11395 {
11396 bfd_put_32 (htab->params->stub_bfd,
11397 ADDIS_R12_R2 | PPC_HA (off), p);
11398 p += 4;
11399 bfd_put_32 (htab->params->stub_bfd,
11400 LD_R12_0R12 | PPC_LO (off), p);
11401 }
11402 else
11403 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11404
11405 if (PPC_HA (r2off) != 0)
11406 {
11407 p += 4;
11408 bfd_put_32 (htab->params->stub_bfd,
11409 ADDIS_R2_R2 | PPC_HA (r2off), p);
11410 }
11411 if (PPC_LO (r2off) != 0)
11412 {
11413 p += 4;
11414 bfd_put_32 (htab->params->stub_bfd,
11415 ADDI_R2_R2 | PPC_LO (r2off), p);
11416 }
11417 }
11418 p += 4;
11419 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11420 p += 4;
11421 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11422 p += 4;
11423 break;
11424
11425 case ppc_stub_plt_call:
11426 case ppc_stub_plt_call_r2save:
11427 if (stub_entry->h != NULL
11428 && stub_entry->h->is_func_descriptor
11429 && stub_entry->h->oh != NULL)
11430 {
11431 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11432
11433 /* If the old-ABI "dot-symbol" is undefined make it weak so
11434 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11435 if (fh->elf.root.type == bfd_link_hash_undefined
11436 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11437 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11438 fh->elf.root.type = bfd_link_hash_undefweak;
11439 }
11440
11441 /* Now build the stub. */
11442 targ = stub_entry->plt_ent->plt.offset & ~1;
11443 if (targ >= (bfd_vma) -2)
11444 abort ();
11445
11446 plt = htab->elf.splt;
11447 if (!htab->elf.dynamic_sections_created
11448 || stub_entry->h == NULL
11449 || stub_entry->h->elf.dynindx == -1)
11450 {
11451 if (stub_entry->symtype == STT_GNU_IFUNC)
11452 plt = htab->elf.iplt;
11453 else
11454 plt = htab->pltlocal;
11455 }
11456 targ += plt->output_offset + plt->output_section->vma;
11457
11458 off = (elf_gp (info->output_bfd)
11459 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11460 off = targ - off;
11461
11462 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11463 {
11464 info->callbacks->einfo
11465 /* xgettext:c-format */
11466 (_("%P: linkage table error against `%pT'\n"),
11467 stub_entry->h != NULL
11468 ? stub_entry->h->elf.root.root.string
11469 : "<local sym>");
11470 bfd_set_error (bfd_error_bad_value);
11471 htab->stub_error = TRUE;
11472 return FALSE;
11473 }
11474
11475 r = NULL;
11476 if (info->emitrelocations)
11477 {
11478 r = get_relocs (stub_entry->group->stub_sec,
11479 ((PPC_HA (off) != 0)
11480 + (htab->opd_abi
11481 ? 2 + (htab->params->plt_static_chain
11482 && PPC_HA (off + 16) == PPC_HA (off))
11483 : 1)));
11484 if (r == NULL)
11485 return FALSE;
11486 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11487 if (bfd_big_endian (info->output_bfd))
11488 r[0].r_offset += 2;
11489 r[0].r_addend = targ;
11490 }
11491 if (stub_entry->h != NULL
11492 && (stub_entry->h == htab->tls_get_addr_fd
11493 || stub_entry->h == htab->tls_get_addr)
11494 && htab->params->tls_get_addr_opt)
11495 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11496 else
11497 p = build_plt_stub (htab, stub_entry, loc, off, r);
11498 break;
11499
11500 case ppc_stub_save_res:
11501 return TRUE;
11502
11503 default:
11504 BFD_FAIL ();
11505 return FALSE;
11506 }
11507
11508 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11509
11510 if (htab->params->emit_stub_syms)
11511 {
11512 struct elf_link_hash_entry *h;
11513 size_t len1, len2;
11514 char *name;
11515 const char *const stub_str[] = { "long_branch",
11516 "long_branch_r2off",
11517 "plt_branch",
11518 "plt_branch_r2off",
11519 "plt_call",
11520 "plt_call" };
11521
11522 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11523 len2 = strlen (stub_entry->root.string);
11524 name = bfd_malloc (len1 + len2 + 2);
11525 if (name == NULL)
11526 return FALSE;
11527 memcpy (name, stub_entry->root.string, 9);
11528 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11529 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11530 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11531 if (h == NULL)
11532 return FALSE;
11533 if (h->root.type == bfd_link_hash_new)
11534 {
11535 h->root.type = bfd_link_hash_defined;
11536 h->root.u.def.section = stub_entry->group->stub_sec;
11537 h->root.u.def.value = stub_entry->stub_offset;
11538 h->ref_regular = 1;
11539 h->def_regular = 1;
11540 h->ref_regular_nonweak = 1;
11541 h->forced_local = 1;
11542 h->non_elf = 0;
11543 h->root.linker_def = 1;
11544 }
11545 }
11546
11547 return TRUE;
11548 }
11549
11550 /* As above, but don't actually build the stub. Just bump offset so
11551 we know stub section sizes, and select plt_branch stubs where
11552 long_branch stubs won't do. */
11553
11554 static bfd_boolean
11555 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11556 {
11557 struct ppc_stub_hash_entry *stub_entry;
11558 struct bfd_link_info *info;
11559 struct ppc_link_hash_table *htab;
11560 bfd_vma targ, off;
11561 int size;
11562
11563 /* Massage our args to the form they really have. */
11564 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11565 info = in_arg;
11566
11567 htab = ppc_hash_table (info);
11568 if (htab == NULL)
11569 return FALSE;
11570
11571 /* Make a note of the offset within the stubs for this entry. */
11572 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11573
11574 if (stub_entry->h != NULL
11575 && stub_entry->h->save_res
11576 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11577 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11578 {
11579 /* Don't make stubs to out-of-line register save/restore
11580 functions. Instead, emit copies of the functions. */
11581 stub_entry->group->needs_save_res = 1;
11582 stub_entry->stub_type = ppc_stub_save_res;
11583 return TRUE;
11584 }
11585
11586 if (stub_entry->stub_type == ppc_stub_plt_call
11587 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11588 {
11589 asection *plt;
11590 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11591 if (targ >= (bfd_vma) -2)
11592 abort ();
11593 plt = htab->elf.splt;
11594 if (!htab->elf.dynamic_sections_created
11595 || stub_entry->h == NULL
11596 || stub_entry->h->elf.dynindx == -1)
11597 {
11598 if (stub_entry->symtype == STT_GNU_IFUNC)
11599 plt = htab->elf.iplt;
11600 else
11601 plt = htab->pltlocal;
11602 }
11603 targ += plt->output_offset + plt->output_section->vma;
11604
11605 off = (elf_gp (info->output_bfd)
11606 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11607 off = targ - off;
11608
11609 if (htab->params->plt_stub_align != 0)
11610 {
11611 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11612
11613 stub_entry->group->stub_sec->size += pad;
11614 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11615 }
11616
11617 size = plt_stub_size (htab, stub_entry, off);
11618
11619 if (stub_entry->h != NULL
11620 && (stub_entry->h == htab->tls_get_addr_fd
11621 || stub_entry->h == htab->tls_get_addr)
11622 && htab->params->tls_get_addr_opt
11623 && (ALWAYS_EMIT_R2SAVE
11624 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11625 stub_entry->group->tls_get_addr_opt_bctrl
11626 = stub_entry->stub_offset + size - 5 * 4;
11627
11628 if (info->emitrelocations)
11629 {
11630 stub_entry->group->stub_sec->reloc_count
11631 += ((PPC_HA (off) != 0)
11632 + (htab->opd_abi
11633 ? 2 + (htab->params->plt_static_chain
11634 && PPC_HA (off + 16) == PPC_HA (off))
11635 : 1));
11636 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11637 }
11638 }
11639 else
11640 {
11641 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11642 variants. */
11643 bfd_vma r2off = 0;
11644 bfd_vma local_off = 0;
11645
11646 targ = (stub_entry->target_value
11647 + stub_entry->target_section->output_offset
11648 + stub_entry->target_section->output_section->vma);
11649 off = (stub_entry->stub_offset
11650 + stub_entry->group->stub_sec->output_offset
11651 + stub_entry->group->stub_sec->output_section->vma);
11652
11653 /* Reset the stub type from the plt variant in case we now
11654 can reach with a shorter stub. */
11655 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11656 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11657
11658 size = 4;
11659 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11660 {
11661 r2off = get_r2off (info, stub_entry);
11662 if (r2off == (bfd_vma) -1)
11663 {
11664 htab->stub_error = TRUE;
11665 return FALSE;
11666 }
11667 size = 8;
11668 if (PPC_HA (r2off) != 0)
11669 size += 4;
11670 if (PPC_LO (r2off) != 0)
11671 size += 4;
11672 off += size - 4;
11673 }
11674 off = targ - off;
11675
11676 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11677
11678 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11679 Do the same for -R objects without function descriptors. */
11680 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11681 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11682 && r2off == 0
11683 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11684 {
11685 struct ppc_branch_hash_entry *br_entry;
11686
11687 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11688 stub_entry->root.string + 9,
11689 TRUE, FALSE);
11690 if (br_entry == NULL)
11691 {
11692 _bfd_error_handler (_("can't build branch stub `%s'"),
11693 stub_entry->root.string);
11694 htab->stub_error = TRUE;
11695 return FALSE;
11696 }
11697
11698 if (br_entry->iter != htab->stub_iteration)
11699 {
11700 br_entry->iter = htab->stub_iteration;
11701 br_entry->offset = htab->brlt->size;
11702 htab->brlt->size += 8;
11703
11704 if (htab->relbrlt != NULL)
11705 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11706 else if (info->emitrelocations)
11707 {
11708 htab->brlt->reloc_count += 1;
11709 htab->brlt->flags |= SEC_RELOC;
11710 }
11711 }
11712
11713 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11714 targ = (br_entry->offset
11715 + htab->brlt->output_offset
11716 + htab->brlt->output_section->vma);
11717 off = (elf_gp (info->output_bfd)
11718 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11719 off = targ - off;
11720
11721 if (info->emitrelocations)
11722 {
11723 stub_entry->group->stub_sec->reloc_count
11724 += 1 + (PPC_HA (off) != 0);
11725 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11726 }
11727
11728 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11729 {
11730 size = 12;
11731 if (PPC_HA (off) != 0)
11732 size = 16;
11733 }
11734 else
11735 {
11736 size = 16;
11737 if (PPC_HA (off) != 0)
11738 size += 4;
11739
11740 if (PPC_HA (r2off) != 0)
11741 size += 4;
11742 if (PPC_LO (r2off) != 0)
11743 size += 4;
11744 }
11745 }
11746 else if (info->emitrelocations)
11747 {
11748 stub_entry->group->stub_sec->reloc_count += 1;
11749 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11750 }
11751 }
11752
11753 stub_entry->group->stub_sec->size += size;
11754 return TRUE;
11755 }
11756
11757 /* Set up various things so that we can make a list of input sections
11758 for each output section included in the link. Returns -1 on error,
11759 0 when no stubs will be needed, and 1 on success. */
11760
11761 int
11762 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11763 {
11764 unsigned int id;
11765 bfd_size_type amt;
11766 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11767
11768 if (htab == NULL)
11769 return -1;
11770
11771 htab->sec_info_arr_size = _bfd_section_id;
11772 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11773 htab->sec_info = bfd_zmalloc (amt);
11774 if (htab->sec_info == NULL)
11775 return -1;
11776
11777 /* Set toc_off for com, und, abs and ind sections. */
11778 for (id = 0; id < 3; id++)
11779 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11780
11781 return 1;
11782 }
11783
11784 /* Set up for first pass at multitoc partitioning. */
11785
11786 void
11787 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11788 {
11789 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11790
11791 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11792 htab->toc_bfd = NULL;
11793 htab->toc_first_sec = NULL;
11794 }
11795
11796 /* The linker repeatedly calls this function for each TOC input section
11797 and linker generated GOT section. Group input bfds such that the toc
11798 within a group is less than 64k in size. */
11799
11800 bfd_boolean
11801 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11802 {
11803 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11804 bfd_vma addr, off, limit;
11805
11806 if (htab == NULL)
11807 return FALSE;
11808
11809 if (!htab->second_toc_pass)
11810 {
11811 /* Keep track of the first .toc or .got section for this input bfd. */
11812 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11813
11814 if (new_bfd)
11815 {
11816 htab->toc_bfd = isec->owner;
11817 htab->toc_first_sec = isec;
11818 }
11819
11820 addr = isec->output_offset + isec->output_section->vma;
11821 off = addr - htab->toc_curr;
11822 limit = 0x80008000;
11823 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11824 limit = 0x10000;
11825 if (off + isec->size > limit)
11826 {
11827 addr = (htab->toc_first_sec->output_offset
11828 + htab->toc_first_sec->output_section->vma);
11829 htab->toc_curr = addr;
11830 htab->toc_curr &= -TOC_BASE_ALIGN;
11831 }
11832
11833 /* toc_curr is the base address of this toc group. Set elf_gp
11834 for the input section to be the offset relative to the
11835 output toc base plus 0x8000. Making the input elf_gp an
11836 offset allows us to move the toc as a whole without
11837 recalculating input elf_gp. */
11838 off = htab->toc_curr - elf_gp (info->output_bfd);
11839 off += TOC_BASE_OFF;
11840
11841 /* Die if someone uses a linker script that doesn't keep input
11842 file .toc and .got together. */
11843 if (new_bfd
11844 && elf_gp (isec->owner) != 0
11845 && elf_gp (isec->owner) != off)
11846 return FALSE;
11847
11848 elf_gp (isec->owner) = off;
11849 return TRUE;
11850 }
11851
11852 /* During the second pass toc_first_sec points to the start of
11853 a toc group, and toc_curr is used to track the old elf_gp.
11854 We use toc_bfd to ensure we only look at each bfd once. */
11855 if (htab->toc_bfd == isec->owner)
11856 return TRUE;
11857 htab->toc_bfd = isec->owner;
11858
11859 if (htab->toc_first_sec == NULL
11860 || htab->toc_curr != elf_gp (isec->owner))
11861 {
11862 htab->toc_curr = elf_gp (isec->owner);
11863 htab->toc_first_sec = isec;
11864 }
11865 addr = (htab->toc_first_sec->output_offset
11866 + htab->toc_first_sec->output_section->vma);
11867 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11868 elf_gp (isec->owner) = off;
11869
11870 return TRUE;
11871 }
11872
11873 /* Called via elf_link_hash_traverse to merge GOT entries for global
11874 symbol H. */
11875
11876 static bfd_boolean
11877 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11878 {
11879 if (h->root.type == bfd_link_hash_indirect)
11880 return TRUE;
11881
11882 merge_got_entries (&h->got.glist);
11883
11884 return TRUE;
11885 }
11886
11887 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11888 symbol H. */
11889
11890 static bfd_boolean
11891 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11892 {
11893 struct got_entry *gent;
11894
11895 if (h->root.type == bfd_link_hash_indirect)
11896 return TRUE;
11897
11898 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11899 if (!gent->is_indirect)
11900 allocate_got (h, (struct bfd_link_info *) inf, gent);
11901 return TRUE;
11902 }
11903
11904 /* Called on the first multitoc pass after the last call to
11905 ppc64_elf_next_toc_section. This function removes duplicate GOT
11906 entries. */
11907
11908 bfd_boolean
11909 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11910 {
11911 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11912 struct bfd *ibfd, *ibfd2;
11913 bfd_boolean done_something;
11914
11915 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11916
11917 if (!htab->do_multi_toc)
11918 return FALSE;
11919
11920 /* Merge global sym got entries within a toc group. */
11921 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11922
11923 /* And tlsld_got. */
11924 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11925 {
11926 struct got_entry *ent, *ent2;
11927
11928 if (!is_ppc64_elf (ibfd))
11929 continue;
11930
11931 ent = ppc64_tlsld_got (ibfd);
11932 if (!ent->is_indirect
11933 && ent->got.offset != (bfd_vma) -1)
11934 {
11935 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11936 {
11937 if (!is_ppc64_elf (ibfd2))
11938 continue;
11939
11940 ent2 = ppc64_tlsld_got (ibfd2);
11941 if (!ent2->is_indirect
11942 && ent2->got.offset != (bfd_vma) -1
11943 && elf_gp (ibfd2) == elf_gp (ibfd))
11944 {
11945 ent2->is_indirect = TRUE;
11946 ent2->got.ent = ent;
11947 }
11948 }
11949 }
11950 }
11951
11952 /* Zap sizes of got sections. */
11953 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11954 htab->elf.irelplt->size -= htab->got_reli_size;
11955 htab->got_reli_size = 0;
11956
11957 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11958 {
11959 asection *got, *relgot;
11960
11961 if (!is_ppc64_elf (ibfd))
11962 continue;
11963
11964 got = ppc64_elf_tdata (ibfd)->got;
11965 if (got != NULL)
11966 {
11967 got->rawsize = got->size;
11968 got->size = 0;
11969 relgot = ppc64_elf_tdata (ibfd)->relgot;
11970 relgot->rawsize = relgot->size;
11971 relgot->size = 0;
11972 }
11973 }
11974
11975 /* Now reallocate the got, local syms first. We don't need to
11976 allocate section contents again since we never increase size. */
11977 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11978 {
11979 struct got_entry **lgot_ents;
11980 struct got_entry **end_lgot_ents;
11981 struct plt_entry **local_plt;
11982 struct plt_entry **end_local_plt;
11983 unsigned char *lgot_masks;
11984 bfd_size_type locsymcount;
11985 Elf_Internal_Shdr *symtab_hdr;
11986 asection *s;
11987
11988 if (!is_ppc64_elf (ibfd))
11989 continue;
11990
11991 lgot_ents = elf_local_got_ents (ibfd);
11992 if (!lgot_ents)
11993 continue;
11994
11995 symtab_hdr = &elf_symtab_hdr (ibfd);
11996 locsymcount = symtab_hdr->sh_info;
11997 end_lgot_ents = lgot_ents + locsymcount;
11998 local_plt = (struct plt_entry **) end_lgot_ents;
11999 end_local_plt = local_plt + locsymcount;
12000 lgot_masks = (unsigned char *) end_local_plt;
12001 s = ppc64_elf_tdata (ibfd)->got;
12002 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12003 {
12004 struct got_entry *ent;
12005
12006 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12007 {
12008 unsigned int ent_size = 8;
12009 unsigned int rel_size = sizeof (Elf64_External_Rela);
12010
12011 ent->got.offset = s->size;
12012 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12013 {
12014 ent_size *= 2;
12015 rel_size *= 2;
12016 }
12017 s->size += ent_size;
12018 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12019 {
12020 htab->elf.irelplt->size += rel_size;
12021 htab->got_reli_size += rel_size;
12022 }
12023 else if (bfd_link_pic (info)
12024 && !((ent->tls_type & TLS_TPREL) != 0
12025 && bfd_link_executable (info)))
12026 {
12027 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12028 srel->size += rel_size;
12029 }
12030 }
12031 }
12032 }
12033
12034 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12035
12036 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12037 {
12038 struct got_entry *ent;
12039
12040 if (!is_ppc64_elf (ibfd))
12041 continue;
12042
12043 ent = ppc64_tlsld_got (ibfd);
12044 if (!ent->is_indirect
12045 && ent->got.offset != (bfd_vma) -1)
12046 {
12047 asection *s = ppc64_elf_tdata (ibfd)->got;
12048 ent->got.offset = s->size;
12049 s->size += 16;
12050 if (bfd_link_pic (info))
12051 {
12052 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12053 srel->size += sizeof (Elf64_External_Rela);
12054 }
12055 }
12056 }
12057
12058 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12059 if (!done_something)
12060 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12061 {
12062 asection *got;
12063
12064 if (!is_ppc64_elf (ibfd))
12065 continue;
12066
12067 got = ppc64_elf_tdata (ibfd)->got;
12068 if (got != NULL)
12069 {
12070 done_something = got->rawsize != got->size;
12071 if (done_something)
12072 break;
12073 }
12074 }
12075
12076 if (done_something)
12077 (*htab->params->layout_sections_again) ();
12078
12079 /* Set up for second pass over toc sections to recalculate elf_gp
12080 on input sections. */
12081 htab->toc_bfd = NULL;
12082 htab->toc_first_sec = NULL;
12083 htab->second_toc_pass = TRUE;
12084 return done_something;
12085 }
12086
12087 /* Called after second pass of multitoc partitioning. */
12088
12089 void
12090 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12091 {
12092 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12093
12094 /* After the second pass, toc_curr tracks the TOC offset used
12095 for code sections below in ppc64_elf_next_input_section. */
12096 htab->toc_curr = TOC_BASE_OFF;
12097 }
12098
12099 /* No toc references were found in ISEC. If the code in ISEC makes no
12100 calls, then there's no need to use toc adjusting stubs when branching
12101 into ISEC. Actually, indirect calls from ISEC are OK as they will
12102 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12103 needed, and 2 if a cyclical call-graph was found but no other reason
12104 for a stub was detected. If called from the top level, a return of
12105 2 means the same as a return of 0. */
12106
12107 static int
12108 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12109 {
12110 int ret;
12111
12112 /* Mark this section as checked. */
12113 isec->call_check_done = 1;
12114
12115 /* We know none of our code bearing sections will need toc stubs. */
12116 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12117 return 0;
12118
12119 if (isec->size == 0)
12120 return 0;
12121
12122 if (isec->output_section == NULL)
12123 return 0;
12124
12125 ret = 0;
12126 if (isec->reloc_count != 0)
12127 {
12128 Elf_Internal_Rela *relstart, *rel;
12129 Elf_Internal_Sym *local_syms;
12130 struct ppc_link_hash_table *htab;
12131
12132 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12133 info->keep_memory);
12134 if (relstart == NULL)
12135 return -1;
12136
12137 /* Look for branches to outside of this section. */
12138 local_syms = NULL;
12139 htab = ppc_hash_table (info);
12140 if (htab == NULL)
12141 return -1;
12142
12143 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12144 {
12145 enum elf_ppc64_reloc_type r_type;
12146 unsigned long r_symndx;
12147 struct elf_link_hash_entry *h;
12148 struct ppc_link_hash_entry *eh;
12149 Elf_Internal_Sym *sym;
12150 asection *sym_sec;
12151 struct _opd_sec_data *opd;
12152 bfd_vma sym_value;
12153 bfd_vma dest;
12154
12155 r_type = ELF64_R_TYPE (rel->r_info);
12156 if (r_type != R_PPC64_REL24
12157 && r_type != R_PPC64_REL14
12158 && r_type != R_PPC64_REL14_BRTAKEN
12159 && r_type != R_PPC64_REL14_BRNTAKEN
12160 && r_type != R_PPC64_PLTCALL)
12161 continue;
12162
12163 r_symndx = ELF64_R_SYM (rel->r_info);
12164 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12165 isec->owner))
12166 {
12167 ret = -1;
12168 break;
12169 }
12170
12171 /* Calls to dynamic lib functions go through a plt call stub
12172 that uses r2. */
12173 eh = (struct ppc_link_hash_entry *) h;
12174 if (eh != NULL
12175 && (eh->elf.plt.plist != NULL
12176 || (eh->oh != NULL
12177 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12178 {
12179 ret = 1;
12180 break;
12181 }
12182
12183 if (sym_sec == NULL)
12184 /* Ignore other undefined symbols. */
12185 continue;
12186
12187 /* Assume branches to other sections not included in the
12188 link need stubs too, to cover -R and absolute syms. */
12189 if (sym_sec->output_section == NULL)
12190 {
12191 ret = 1;
12192 break;
12193 }
12194
12195 if (h == NULL)
12196 sym_value = sym->st_value;
12197 else
12198 {
12199 if (h->root.type != bfd_link_hash_defined
12200 && h->root.type != bfd_link_hash_defweak)
12201 abort ();
12202 sym_value = h->root.u.def.value;
12203 }
12204 sym_value += rel->r_addend;
12205
12206 /* If this branch reloc uses an opd sym, find the code section. */
12207 opd = get_opd_info (sym_sec);
12208 if (opd != NULL)
12209 {
12210 if (h == NULL && opd->adjust != NULL)
12211 {
12212 long adjust;
12213
12214 adjust = opd->adjust[OPD_NDX (sym_value)];
12215 if (adjust == -1)
12216 /* Assume deleted functions won't ever be called. */
12217 continue;
12218 sym_value += adjust;
12219 }
12220
12221 dest = opd_entry_value (sym_sec, sym_value,
12222 &sym_sec, NULL, FALSE);
12223 if (dest == (bfd_vma) -1)
12224 continue;
12225 }
12226 else
12227 dest = (sym_value
12228 + sym_sec->output_offset
12229 + sym_sec->output_section->vma);
12230
12231 /* Ignore branch to self. */
12232 if (sym_sec == isec)
12233 continue;
12234
12235 /* If the called function uses the toc, we need a stub. */
12236 if (sym_sec->has_toc_reloc
12237 || sym_sec->makes_toc_func_call)
12238 {
12239 ret = 1;
12240 break;
12241 }
12242
12243 /* Assume any branch that needs a long branch stub might in fact
12244 need a plt_branch stub. A plt_branch stub uses r2. */
12245 else if (dest - (isec->output_offset
12246 + isec->output_section->vma
12247 + rel->r_offset) + (1 << 25)
12248 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12249 ? h->other
12250 : sym->st_other))
12251 {
12252 ret = 1;
12253 break;
12254 }
12255
12256 /* If calling back to a section in the process of being
12257 tested, we can't say for sure that no toc adjusting stubs
12258 are needed, so don't return zero. */
12259 else if (sym_sec->call_check_in_progress)
12260 ret = 2;
12261
12262 /* Branches to another section that itself doesn't have any TOC
12263 references are OK. Recursively call ourselves to check. */
12264 else if (!sym_sec->call_check_done)
12265 {
12266 int recur;
12267
12268 /* Mark current section as indeterminate, so that other
12269 sections that call back to current won't be marked as
12270 known. */
12271 isec->call_check_in_progress = 1;
12272 recur = toc_adjusting_stub_needed (info, sym_sec);
12273 isec->call_check_in_progress = 0;
12274
12275 if (recur != 0)
12276 {
12277 ret = recur;
12278 if (recur != 2)
12279 break;
12280 }
12281 }
12282 }
12283
12284 if (local_syms != NULL
12285 && (elf_symtab_hdr (isec->owner).contents
12286 != (unsigned char *) local_syms))
12287 free (local_syms);
12288 if (elf_section_data (isec)->relocs != relstart)
12289 free (relstart);
12290 }
12291
12292 if ((ret & 1) == 0
12293 && isec->map_head.s != NULL
12294 && (strcmp (isec->output_section->name, ".init") == 0
12295 || strcmp (isec->output_section->name, ".fini") == 0))
12296 {
12297 if (isec->map_head.s->has_toc_reloc
12298 || isec->map_head.s->makes_toc_func_call)
12299 ret = 1;
12300 else if (!isec->map_head.s->call_check_done)
12301 {
12302 int recur;
12303 isec->call_check_in_progress = 1;
12304 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12305 isec->call_check_in_progress = 0;
12306 if (recur != 0)
12307 ret = recur;
12308 }
12309 }
12310
12311 if (ret == 1)
12312 isec->makes_toc_func_call = 1;
12313
12314 return ret;
12315 }
12316
12317 /* The linker repeatedly calls this function for each input section,
12318 in the order that input sections are linked into output sections.
12319 Build lists of input sections to determine groupings between which
12320 we may insert linker stubs. */
12321
12322 bfd_boolean
12323 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12324 {
12325 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12326
12327 if (htab == NULL)
12328 return FALSE;
12329
12330 if ((isec->output_section->flags & SEC_CODE) != 0
12331 && isec->output_section->id < htab->sec_info_arr_size)
12332 {
12333 /* This happens to make the list in reverse order,
12334 which is what we want. */
12335 htab->sec_info[isec->id].u.list
12336 = htab->sec_info[isec->output_section->id].u.list;
12337 htab->sec_info[isec->output_section->id].u.list = isec;
12338 }
12339
12340 if (htab->multi_toc_needed)
12341 {
12342 /* Analyse sections that aren't already flagged as needing a
12343 valid toc pointer. Exclude .fixup for the linux kernel.
12344 .fixup contains branches, but only back to the function that
12345 hit an exception. */
12346 if (!(isec->has_toc_reloc
12347 || (isec->flags & SEC_CODE) == 0
12348 || strcmp (isec->name, ".fixup") == 0
12349 || isec->call_check_done))
12350 {
12351 if (toc_adjusting_stub_needed (info, isec) < 0)
12352 return FALSE;
12353 }
12354 /* Make all sections use the TOC assigned for this object file.
12355 This will be wrong for pasted sections; We fix that in
12356 check_pasted_section(). */
12357 if (elf_gp (isec->owner) != 0)
12358 htab->toc_curr = elf_gp (isec->owner);
12359 }
12360
12361 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12362 return TRUE;
12363 }
12364
12365 /* Check that all .init and .fini sections use the same toc, if they
12366 have toc relocs. */
12367
12368 static bfd_boolean
12369 check_pasted_section (struct bfd_link_info *info, const char *name)
12370 {
12371 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12372
12373 if (o != NULL)
12374 {
12375 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12376 bfd_vma toc_off = 0;
12377 asection *i;
12378
12379 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12380 if (i->has_toc_reloc)
12381 {
12382 if (toc_off == 0)
12383 toc_off = htab->sec_info[i->id].toc_off;
12384 else if (toc_off != htab->sec_info[i->id].toc_off)
12385 return FALSE;
12386 }
12387
12388 if (toc_off == 0)
12389 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12390 if (i->makes_toc_func_call)
12391 {
12392 toc_off = htab->sec_info[i->id].toc_off;
12393 break;
12394 }
12395
12396 /* Make sure the whole pasted function uses the same toc offset. */
12397 if (toc_off != 0)
12398 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12399 htab->sec_info[i->id].toc_off = toc_off;
12400 }
12401 return TRUE;
12402 }
12403
12404 bfd_boolean
12405 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12406 {
12407 return (check_pasted_section (info, ".init")
12408 & check_pasted_section (info, ".fini"));
12409 }
12410
12411 /* See whether we can group stub sections together. Grouping stub
12412 sections may result in fewer stubs. More importantly, we need to
12413 put all .init* and .fini* stubs at the beginning of the .init or
12414 .fini output sections respectively, because glibc splits the
12415 _init and _fini functions into multiple parts. Putting a stub in
12416 the middle of a function is not a good idea. */
12417
12418 static bfd_boolean
12419 group_sections (struct bfd_link_info *info,
12420 bfd_size_type stub_group_size,
12421 bfd_boolean stubs_always_before_branch)
12422 {
12423 struct ppc_link_hash_table *htab;
12424 asection *osec;
12425 bfd_boolean suppress_size_errors;
12426
12427 htab = ppc_hash_table (info);
12428 if (htab == NULL)
12429 return FALSE;
12430
12431 suppress_size_errors = FALSE;
12432 if (stub_group_size == 1)
12433 {
12434 /* Default values. */
12435 if (stubs_always_before_branch)
12436 stub_group_size = 0x1e00000;
12437 else
12438 stub_group_size = 0x1c00000;
12439 suppress_size_errors = TRUE;
12440 }
12441
12442 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12443 {
12444 asection *tail;
12445
12446 if (osec->id >= htab->sec_info_arr_size)
12447 continue;
12448
12449 tail = htab->sec_info[osec->id].u.list;
12450 while (tail != NULL)
12451 {
12452 asection *curr;
12453 asection *prev;
12454 bfd_size_type total;
12455 bfd_boolean big_sec;
12456 bfd_vma curr_toc;
12457 struct map_stub *group;
12458 bfd_size_type group_size;
12459
12460 curr = tail;
12461 total = tail->size;
12462 group_size = (ppc64_elf_section_data (tail) != NULL
12463 && ppc64_elf_section_data (tail)->has_14bit_branch
12464 ? stub_group_size >> 10 : stub_group_size);
12465
12466 big_sec = total > group_size;
12467 if (big_sec && !suppress_size_errors)
12468 /* xgettext:c-format */
12469 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12470 tail->owner, tail);
12471 curr_toc = htab->sec_info[tail->id].toc_off;
12472
12473 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12474 && ((total += curr->output_offset - prev->output_offset)
12475 < (ppc64_elf_section_data (prev) != NULL
12476 && ppc64_elf_section_data (prev)->has_14bit_branch
12477 ? (group_size = stub_group_size >> 10) : group_size))
12478 && htab->sec_info[prev->id].toc_off == curr_toc)
12479 curr = prev;
12480
12481 /* OK, the size from the start of CURR to the end is less
12482 than group_size and thus can be handled by one stub
12483 section. (or the tail section is itself larger than
12484 group_size, in which case we may be toast.) We should
12485 really be keeping track of the total size of stubs added
12486 here, as stubs contribute to the final output section
12487 size. That's a little tricky, and this way will only
12488 break if stubs added make the total size more than 2^25,
12489 ie. for the default stub_group_size, if stubs total more
12490 than 2097152 bytes, or nearly 75000 plt call stubs. */
12491 group = bfd_alloc (curr->owner, sizeof (*group));
12492 if (group == NULL)
12493 return FALSE;
12494 group->link_sec = curr;
12495 group->stub_sec = NULL;
12496 group->needs_save_res = 0;
12497 group->tls_get_addr_opt_bctrl = -1u;
12498 group->next = htab->group;
12499 htab->group = group;
12500 do
12501 {
12502 prev = htab->sec_info[tail->id].u.list;
12503 /* Set up this stub group. */
12504 htab->sec_info[tail->id].u.group = group;
12505 }
12506 while (tail != curr && (tail = prev) != NULL);
12507
12508 /* But wait, there's more! Input sections up to group_size
12509 bytes before the stub section can be handled by it too.
12510 Don't do this if we have a really large section after the
12511 stubs, as adding more stubs increases the chance that
12512 branches may not reach into the stub section. */
12513 if (!stubs_always_before_branch && !big_sec)
12514 {
12515 total = 0;
12516 while (prev != NULL
12517 && ((total += tail->output_offset - prev->output_offset)
12518 < (ppc64_elf_section_data (prev) != NULL
12519 && ppc64_elf_section_data (prev)->has_14bit_branch
12520 ? (group_size = stub_group_size >> 10) : group_size))
12521 && htab->sec_info[prev->id].toc_off == curr_toc)
12522 {
12523 tail = prev;
12524 prev = htab->sec_info[tail->id].u.list;
12525 htab->sec_info[tail->id].u.group = group;
12526 }
12527 }
12528 tail = prev;
12529 }
12530 }
12531 return TRUE;
12532 }
12533
12534 static const unsigned char glink_eh_frame_cie[] =
12535 {
12536 0, 0, 0, 16, /* length. */
12537 0, 0, 0, 0, /* id. */
12538 1, /* CIE version. */
12539 'z', 'R', 0, /* Augmentation string. */
12540 4, /* Code alignment. */
12541 0x78, /* Data alignment. */
12542 65, /* RA reg. */
12543 1, /* Augmentation size. */
12544 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12545 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12546 };
12547
12548 static size_t
12549 stub_eh_frame_size (struct map_stub *group, size_t align)
12550 {
12551 size_t this_size = 17;
12552 if (group->tls_get_addr_opt_bctrl != -1u)
12553 {
12554 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12555 if (to_bctrl < 64)
12556 this_size += 1;
12557 else if (to_bctrl < 256)
12558 this_size += 2;
12559 else if (to_bctrl < 65536)
12560 this_size += 3;
12561 else
12562 this_size += 5;
12563 this_size += 6;
12564 }
12565 this_size = (this_size + align - 1) & -align;
12566 return this_size;
12567 }
12568
12569 /* Stripping output sections is normally done before dynamic section
12570 symbols have been allocated. This function is called later, and
12571 handles cases like htab->brlt which is mapped to its own output
12572 section. */
12573
12574 static void
12575 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12576 {
12577 if (isec->size == 0
12578 && isec->output_section->size == 0
12579 && !(isec->output_section->flags & SEC_KEEP)
12580 && !bfd_section_removed_from_list (info->output_bfd,
12581 isec->output_section)
12582 && elf_section_data (isec->output_section)->dynindx == 0)
12583 {
12584 isec->output_section->flags |= SEC_EXCLUDE;
12585 bfd_section_list_remove (info->output_bfd, isec->output_section);
12586 info->output_bfd->section_count--;
12587 }
12588 }
12589
12590 /* Determine and set the size of the stub section for a final link.
12591
12592 The basic idea here is to examine all the relocations looking for
12593 PC-relative calls to a target that is unreachable with a "bl"
12594 instruction. */
12595
12596 bfd_boolean
12597 ppc64_elf_size_stubs (struct bfd_link_info *info)
12598 {
12599 bfd_size_type stub_group_size;
12600 bfd_boolean stubs_always_before_branch;
12601 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12602
12603 if (htab == NULL)
12604 return FALSE;
12605
12606 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12607 htab->params->plt_thread_safe = 1;
12608 if (!htab->opd_abi)
12609 htab->params->plt_thread_safe = 0;
12610 else if (htab->params->plt_thread_safe == -1)
12611 {
12612 static const char *const thread_starter[] =
12613 {
12614 "pthread_create",
12615 /* libstdc++ */
12616 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12617 /* librt */
12618 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12619 "mq_notify", "create_timer",
12620 /* libanl */
12621 "getaddrinfo_a",
12622 /* libgomp */
12623 "GOMP_parallel",
12624 "GOMP_parallel_start",
12625 "GOMP_parallel_loop_static",
12626 "GOMP_parallel_loop_static_start",
12627 "GOMP_parallel_loop_dynamic",
12628 "GOMP_parallel_loop_dynamic_start",
12629 "GOMP_parallel_loop_guided",
12630 "GOMP_parallel_loop_guided_start",
12631 "GOMP_parallel_loop_runtime",
12632 "GOMP_parallel_loop_runtime_start",
12633 "GOMP_parallel_sections",
12634 "GOMP_parallel_sections_start",
12635 /* libgo */
12636 "__go_go",
12637 };
12638 unsigned i;
12639
12640 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12641 {
12642 struct elf_link_hash_entry *h;
12643 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12644 FALSE, FALSE, TRUE);
12645 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12646 if (htab->params->plt_thread_safe)
12647 break;
12648 }
12649 }
12650 stubs_always_before_branch = htab->params->group_size < 0;
12651 if (htab->params->group_size < 0)
12652 stub_group_size = -htab->params->group_size;
12653 else
12654 stub_group_size = htab->params->group_size;
12655
12656 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12657 return FALSE;
12658
12659 #define STUB_SHRINK_ITER 20
12660 /* Loop until no stubs added. After iteration 20 of this loop we may
12661 exit on a stub section shrinking. This is to break out of a
12662 pathological case where adding stubs on one iteration decreases
12663 section gaps (perhaps due to alignment), which then requires
12664 fewer or smaller stubs on the next iteration. */
12665
12666 while (1)
12667 {
12668 bfd *input_bfd;
12669 unsigned int bfd_indx;
12670 struct map_stub *group;
12671
12672 htab->stub_iteration += 1;
12673
12674 for (input_bfd = info->input_bfds, bfd_indx = 0;
12675 input_bfd != NULL;
12676 input_bfd = input_bfd->link.next, bfd_indx++)
12677 {
12678 Elf_Internal_Shdr *symtab_hdr;
12679 asection *section;
12680 Elf_Internal_Sym *local_syms = NULL;
12681
12682 if (!is_ppc64_elf (input_bfd))
12683 continue;
12684
12685 /* We'll need the symbol table in a second. */
12686 symtab_hdr = &elf_symtab_hdr (input_bfd);
12687 if (symtab_hdr->sh_info == 0)
12688 continue;
12689
12690 /* Walk over each section attached to the input bfd. */
12691 for (section = input_bfd->sections;
12692 section != NULL;
12693 section = section->next)
12694 {
12695 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12696
12697 /* If there aren't any relocs, then there's nothing more
12698 to do. */
12699 if ((section->flags & SEC_RELOC) == 0
12700 || (section->flags & SEC_ALLOC) == 0
12701 || (section->flags & SEC_LOAD) == 0
12702 || (section->flags & SEC_CODE) == 0
12703 || section->reloc_count == 0)
12704 continue;
12705
12706 /* If this section is a link-once section that will be
12707 discarded, then don't create any stubs. */
12708 if (section->output_section == NULL
12709 || section->output_section->owner != info->output_bfd)
12710 continue;
12711
12712 /* Get the relocs. */
12713 internal_relocs
12714 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12715 info->keep_memory);
12716 if (internal_relocs == NULL)
12717 goto error_ret_free_local;
12718
12719 /* Now examine each relocation. */
12720 irela = internal_relocs;
12721 irelaend = irela + section->reloc_count;
12722 for (; irela < irelaend; irela++)
12723 {
12724 enum elf_ppc64_reloc_type r_type;
12725 unsigned int r_indx;
12726 enum ppc_stub_type stub_type;
12727 struct ppc_stub_hash_entry *stub_entry;
12728 asection *sym_sec, *code_sec;
12729 bfd_vma sym_value, code_value;
12730 bfd_vma destination;
12731 unsigned long local_off;
12732 bfd_boolean ok_dest;
12733 struct ppc_link_hash_entry *hash;
12734 struct ppc_link_hash_entry *fdh;
12735 struct elf_link_hash_entry *h;
12736 Elf_Internal_Sym *sym;
12737 char *stub_name;
12738 const asection *id_sec;
12739 struct _opd_sec_data *opd;
12740 struct plt_entry *plt_ent;
12741
12742 r_type = ELF64_R_TYPE (irela->r_info);
12743 r_indx = ELF64_R_SYM (irela->r_info);
12744
12745 if (r_type >= R_PPC64_max)
12746 {
12747 bfd_set_error (bfd_error_bad_value);
12748 goto error_ret_free_internal;
12749 }
12750
12751 /* Only look for stubs on branch instructions. */
12752 if (r_type != R_PPC64_REL24
12753 && r_type != R_PPC64_REL14
12754 && r_type != R_PPC64_REL14_BRTAKEN
12755 && r_type != R_PPC64_REL14_BRNTAKEN)
12756 continue;
12757
12758 /* Now determine the call target, its name, value,
12759 section. */
12760 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12761 r_indx, input_bfd))
12762 goto error_ret_free_internal;
12763 hash = (struct ppc_link_hash_entry *) h;
12764
12765 ok_dest = FALSE;
12766 fdh = NULL;
12767 sym_value = 0;
12768 if (hash == NULL)
12769 {
12770 sym_value = sym->st_value;
12771 if (sym_sec != NULL
12772 && sym_sec->output_section != NULL)
12773 ok_dest = TRUE;
12774 }
12775 else if (hash->elf.root.type == bfd_link_hash_defined
12776 || hash->elf.root.type == bfd_link_hash_defweak)
12777 {
12778 sym_value = hash->elf.root.u.def.value;
12779 if (sym_sec->output_section != NULL)
12780 ok_dest = TRUE;
12781 }
12782 else if (hash->elf.root.type == bfd_link_hash_undefweak
12783 || hash->elf.root.type == bfd_link_hash_undefined)
12784 {
12785 /* Recognise an old ABI func code entry sym, and
12786 use the func descriptor sym instead if it is
12787 defined. */
12788 if (hash->elf.root.root.string[0] == '.'
12789 && hash->oh != NULL)
12790 {
12791 fdh = ppc_follow_link (hash->oh);
12792 if (fdh->elf.root.type == bfd_link_hash_defined
12793 || fdh->elf.root.type == bfd_link_hash_defweak)
12794 {
12795 sym_sec = fdh->elf.root.u.def.section;
12796 sym_value = fdh->elf.root.u.def.value;
12797 if (sym_sec->output_section != NULL)
12798 ok_dest = TRUE;
12799 }
12800 else
12801 fdh = NULL;
12802 }
12803 }
12804 else
12805 {
12806 bfd_set_error (bfd_error_bad_value);
12807 goto error_ret_free_internal;
12808 }
12809
12810 destination = 0;
12811 local_off = 0;
12812 if (ok_dest)
12813 {
12814 sym_value += irela->r_addend;
12815 destination = (sym_value
12816 + sym_sec->output_offset
12817 + sym_sec->output_section->vma);
12818 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12819 ? hash->elf.other
12820 : sym->st_other);
12821 }
12822
12823 code_sec = sym_sec;
12824 code_value = sym_value;
12825 opd = get_opd_info (sym_sec);
12826 if (opd != NULL)
12827 {
12828 bfd_vma dest;
12829
12830 if (hash == NULL && opd->adjust != NULL)
12831 {
12832 long adjust = opd->adjust[OPD_NDX (sym_value)];
12833 if (adjust == -1)
12834 continue;
12835 code_value += adjust;
12836 sym_value += adjust;
12837 }
12838 dest = opd_entry_value (sym_sec, sym_value,
12839 &code_sec, &code_value, FALSE);
12840 if (dest != (bfd_vma) -1)
12841 {
12842 destination = dest;
12843 if (fdh != NULL)
12844 {
12845 /* Fixup old ABI sym to point at code
12846 entry. */
12847 hash->elf.root.type = bfd_link_hash_defweak;
12848 hash->elf.root.u.def.section = code_sec;
12849 hash->elf.root.u.def.value = code_value;
12850 }
12851 }
12852 }
12853
12854 /* Determine what (if any) linker stub is needed. */
12855 plt_ent = NULL;
12856 stub_type = ppc_type_of_stub (section, irela, &hash,
12857 &plt_ent, destination,
12858 local_off);
12859
12860 if (stub_type != ppc_stub_plt_call)
12861 {
12862 /* Check whether we need a TOC adjusting stub.
12863 Since the linker pastes together pieces from
12864 different object files when creating the
12865 _init and _fini functions, it may be that a
12866 call to what looks like a local sym is in
12867 fact a call needing a TOC adjustment. */
12868 if ((code_sec != NULL
12869 && code_sec->output_section != NULL
12870 && (htab->sec_info[code_sec->id].toc_off
12871 != htab->sec_info[section->id].toc_off)
12872 && (code_sec->has_toc_reloc
12873 || code_sec->makes_toc_func_call))
12874 || (((hash ? hash->elf.other : sym->st_other)
12875 & STO_PPC64_LOCAL_MASK)
12876 == 1 << STO_PPC64_LOCAL_BIT))
12877 stub_type = ppc_stub_long_branch_r2off;
12878 }
12879
12880 if (stub_type == ppc_stub_none)
12881 continue;
12882
12883 /* __tls_get_addr calls might be eliminated. */
12884 if (stub_type != ppc_stub_plt_call
12885 && hash != NULL
12886 && (hash == htab->tls_get_addr
12887 || hash == htab->tls_get_addr_fd)
12888 && section->has_tls_reloc
12889 && irela != internal_relocs)
12890 {
12891 /* Get tls info. */
12892 unsigned char *tls_mask;
12893
12894 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12895 irela - 1, input_bfd))
12896 goto error_ret_free_internal;
12897 if ((*tls_mask & TLS_TLS) != 0)
12898 continue;
12899 }
12900
12901 if (stub_type == ppc_stub_plt_call)
12902 {
12903 if (!htab->opd_abi
12904 && htab->params->plt_localentry0 != 0
12905 && is_elfv2_localentry0 (&hash->elf))
12906 htab->has_plt_localentry0 = 1;
12907 else if (irela + 1 < irelaend
12908 && irela[1].r_offset == irela->r_offset + 4
12909 && (ELF64_R_TYPE (irela[1].r_info)
12910 == R_PPC64_TOCSAVE))
12911 {
12912 if (!tocsave_find (htab, INSERT,
12913 &local_syms, irela + 1, input_bfd))
12914 goto error_ret_free_internal;
12915 }
12916 else
12917 stub_type = ppc_stub_plt_call_r2save;
12918 }
12919
12920 /* Support for grouping stub sections. */
12921 id_sec = htab->sec_info[section->id].u.group->link_sec;
12922
12923 /* Get the name of this stub. */
12924 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12925 if (!stub_name)
12926 goto error_ret_free_internal;
12927
12928 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12929 stub_name, FALSE, FALSE);
12930 if (stub_entry != NULL)
12931 {
12932 /* The proper stub has already been created. */
12933 free (stub_name);
12934 if (stub_type == ppc_stub_plt_call_r2save)
12935 stub_entry->stub_type = stub_type;
12936 continue;
12937 }
12938
12939 stub_entry = ppc_add_stub (stub_name, section, info);
12940 if (stub_entry == NULL)
12941 {
12942 free (stub_name);
12943 error_ret_free_internal:
12944 if (elf_section_data (section)->relocs == NULL)
12945 free (internal_relocs);
12946 error_ret_free_local:
12947 if (local_syms != NULL
12948 && (symtab_hdr->contents
12949 != (unsigned char *) local_syms))
12950 free (local_syms);
12951 return FALSE;
12952 }
12953
12954 stub_entry->stub_type = stub_type;
12955 if (stub_type != ppc_stub_plt_call
12956 && stub_type != ppc_stub_plt_call_r2save)
12957 {
12958 stub_entry->target_value = code_value;
12959 stub_entry->target_section = code_sec;
12960 }
12961 else
12962 {
12963 stub_entry->target_value = sym_value;
12964 stub_entry->target_section = sym_sec;
12965 }
12966 stub_entry->h = hash;
12967 stub_entry->plt_ent = plt_ent;
12968 stub_entry->symtype
12969 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12970 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12971
12972 if (stub_entry->h != NULL)
12973 htab->stub_globals += 1;
12974 }
12975
12976 /* We're done with the internal relocs, free them. */
12977 if (elf_section_data (section)->relocs != internal_relocs)
12978 free (internal_relocs);
12979 }
12980
12981 if (local_syms != NULL
12982 && symtab_hdr->contents != (unsigned char *) local_syms)
12983 {
12984 if (!info->keep_memory)
12985 free (local_syms);
12986 else
12987 symtab_hdr->contents = (unsigned char *) local_syms;
12988 }
12989 }
12990
12991 /* We may have added some stubs. Find out the new size of the
12992 stub sections. */
12993 for (group = htab->group; group != NULL; group = group->next)
12994 if (group->stub_sec != NULL)
12995 {
12996 asection *stub_sec = group->stub_sec;
12997
12998 if (htab->stub_iteration <= STUB_SHRINK_ITER
12999 || stub_sec->rawsize < stub_sec->size)
13000 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13001 stub_sec->rawsize = stub_sec->size;
13002 stub_sec->size = 0;
13003 stub_sec->reloc_count = 0;
13004 stub_sec->flags &= ~SEC_RELOC;
13005 }
13006
13007 if (htab->stub_iteration <= STUB_SHRINK_ITER
13008 || htab->brlt->rawsize < htab->brlt->size)
13009 htab->brlt->rawsize = htab->brlt->size;
13010 htab->brlt->size = 0;
13011 htab->brlt->reloc_count = 0;
13012 htab->brlt->flags &= ~SEC_RELOC;
13013 if (htab->relbrlt != NULL)
13014 htab->relbrlt->size = 0;
13015
13016 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13017
13018 for (group = htab->group; group != NULL; group = group->next)
13019 if (group->needs_save_res)
13020 group->stub_sec->size += htab->sfpr->size;
13021
13022 if (info->emitrelocations
13023 && htab->glink != NULL && htab->glink->size != 0)
13024 {
13025 htab->glink->reloc_count = 1;
13026 htab->glink->flags |= SEC_RELOC;
13027 }
13028
13029 if (htab->glink_eh_frame != NULL
13030 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13031 && htab->glink_eh_frame->output_section->size > 8)
13032 {
13033 size_t size = 0, align = 4;
13034
13035 for (group = htab->group; group != NULL; group = group->next)
13036 if (group->stub_sec != NULL)
13037 size += stub_eh_frame_size (group, align);
13038 if (htab->glink != NULL && htab->glink->size != 0)
13039 size += (24 + align - 1) & -align;
13040 if (size != 0)
13041 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13042 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13043 size = (size + align - 1) & -align;
13044 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13045 htab->glink_eh_frame->size = size;
13046 }
13047
13048 if (htab->params->plt_stub_align != 0)
13049 for (group = htab->group; group != NULL; group = group->next)
13050 if (group->stub_sec != NULL)
13051 {
13052 int align = abs (htab->params->plt_stub_align);
13053 group->stub_sec->size
13054 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13055 }
13056
13057 for (group = htab->group; group != NULL; group = group->next)
13058 if (group->stub_sec != NULL
13059 && group->stub_sec->rawsize != group->stub_sec->size
13060 && (htab->stub_iteration <= STUB_SHRINK_ITER
13061 || group->stub_sec->rawsize < group->stub_sec->size))
13062 break;
13063
13064 if (group == NULL
13065 && (htab->brlt->rawsize == htab->brlt->size
13066 || (htab->stub_iteration > STUB_SHRINK_ITER
13067 && htab->brlt->rawsize > htab->brlt->size))
13068 && (htab->glink_eh_frame == NULL
13069 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13070 break;
13071
13072 /* Ask the linker to do its stuff. */
13073 (*htab->params->layout_sections_again) ();
13074 }
13075
13076 if (htab->glink_eh_frame != NULL
13077 && htab->glink_eh_frame->size != 0)
13078 {
13079 bfd_vma val;
13080 bfd_byte *p, *last_fde;
13081 size_t last_fde_len, size, align, pad;
13082 struct map_stub *group;
13083
13084 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13085 if (p == NULL)
13086 return FALSE;
13087 htab->glink_eh_frame->contents = p;
13088 last_fde = p;
13089 align = 4;
13090
13091 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13092 /* CIE length (rewrite in case little-endian). */
13093 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13094 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13095 p += last_fde_len + 4;
13096
13097 for (group = htab->group; group != NULL; group = group->next)
13098 if (group->stub_sec != NULL)
13099 {
13100 last_fde = p;
13101 last_fde_len = stub_eh_frame_size (group, align) - 4;
13102 /* FDE length. */
13103 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13104 p += 4;
13105 /* CIE pointer. */
13106 val = p - htab->glink_eh_frame->contents;
13107 bfd_put_32 (htab->elf.dynobj, val, p);
13108 p += 4;
13109 /* Offset to stub section, written later. */
13110 p += 4;
13111 /* stub section size. */
13112 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13113 p += 4;
13114 /* Augmentation. */
13115 p += 1;
13116 if (group->tls_get_addr_opt_bctrl != -1u)
13117 {
13118 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13119
13120 /* This FDE needs more than just the default.
13121 Describe __tls_get_addr_opt stub LR. */
13122 if (to_bctrl < 64)
13123 *p++ = DW_CFA_advance_loc + to_bctrl;
13124 else if (to_bctrl < 256)
13125 {
13126 *p++ = DW_CFA_advance_loc1;
13127 *p++ = to_bctrl;
13128 }
13129 else if (to_bctrl < 65536)
13130 {
13131 *p++ = DW_CFA_advance_loc2;
13132 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13133 p += 2;
13134 }
13135 else
13136 {
13137 *p++ = DW_CFA_advance_loc4;
13138 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13139 p += 4;
13140 }
13141 *p++ = DW_CFA_offset_extended_sf;
13142 *p++ = 65;
13143 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13144 *p++ = DW_CFA_advance_loc + 4;
13145 *p++ = DW_CFA_restore_extended;
13146 *p++ = 65;
13147 }
13148 /* Pad. */
13149 p = last_fde + last_fde_len + 4;
13150 }
13151 if (htab->glink != NULL && htab->glink->size != 0)
13152 {
13153 last_fde = p;
13154 last_fde_len = ((24 + align - 1) & -align) - 4;
13155 /* FDE length. */
13156 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13157 p += 4;
13158 /* CIE pointer. */
13159 val = p - htab->glink_eh_frame->contents;
13160 bfd_put_32 (htab->elf.dynobj, val, p);
13161 p += 4;
13162 /* Offset to .glink, written later. */
13163 p += 4;
13164 /* .glink size. */
13165 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13166 p += 4;
13167 /* Augmentation. */
13168 p += 1;
13169
13170 *p++ = DW_CFA_advance_loc + 1;
13171 *p++ = DW_CFA_register;
13172 *p++ = 65;
13173 *p++ = htab->opd_abi ? 12 : 0;
13174 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13175 *p++ = DW_CFA_restore_extended;
13176 *p++ = 65;
13177 p += ((24 + align - 1) & -align) - 24;
13178 }
13179 /* Subsume any padding into the last FDE if user .eh_frame
13180 sections are aligned more than glink_eh_frame. Otherwise any
13181 zero padding will be seen as a terminator. */
13182 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13183 size = p - htab->glink_eh_frame->contents;
13184 pad = ((size + align - 1) & -align) - size;
13185 htab->glink_eh_frame->size = size + pad;
13186 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13187 }
13188
13189 maybe_strip_output (info, htab->brlt);
13190 if (htab->glink_eh_frame != NULL)
13191 maybe_strip_output (info, htab->glink_eh_frame);
13192
13193 return TRUE;
13194 }
13195
13196 /* Called after we have determined section placement. If sections
13197 move, we'll be called again. Provide a value for TOCstart. */
13198
13199 bfd_vma
13200 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13201 {
13202 asection *s;
13203 bfd_vma TOCstart, adjust;
13204
13205 if (info != NULL)
13206 {
13207 struct elf_link_hash_entry *h;
13208 struct elf_link_hash_table *htab = elf_hash_table (info);
13209
13210 if (is_elf_hash_table (htab)
13211 && htab->hgot != NULL)
13212 h = htab->hgot;
13213 else
13214 {
13215 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13216 if (is_elf_hash_table (htab))
13217 htab->hgot = h;
13218 }
13219 if (h != NULL
13220 && h->root.type == bfd_link_hash_defined
13221 && !h->root.linker_def
13222 && (!is_elf_hash_table (htab)
13223 || h->def_regular))
13224 {
13225 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13226 + h->root.u.def.section->output_offset
13227 + h->root.u.def.section->output_section->vma);
13228 _bfd_set_gp_value (obfd, TOCstart);
13229 return TOCstart;
13230 }
13231 }
13232
13233 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13234 order. The TOC starts where the first of these sections starts. */
13235 s = bfd_get_section_by_name (obfd, ".got");
13236 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13237 s = bfd_get_section_by_name (obfd, ".toc");
13238 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13239 s = bfd_get_section_by_name (obfd, ".tocbss");
13240 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13241 s = bfd_get_section_by_name (obfd, ".plt");
13242 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13243 {
13244 /* This may happen for
13245 o references to TOC base (SYM@toc / TOC[tc0]) without a
13246 .toc directive
13247 o bad linker script
13248 o --gc-sections and empty TOC sections
13249
13250 FIXME: Warn user? */
13251
13252 /* Look for a likely section. We probably won't even be
13253 using TOCstart. */
13254 for (s = obfd->sections; s != NULL; s = s->next)
13255 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13256 | SEC_EXCLUDE))
13257 == (SEC_ALLOC | SEC_SMALL_DATA))
13258 break;
13259 if (s == NULL)
13260 for (s = obfd->sections; s != NULL; s = s->next)
13261 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13262 == (SEC_ALLOC | SEC_SMALL_DATA))
13263 break;
13264 if (s == NULL)
13265 for (s = obfd->sections; s != NULL; s = s->next)
13266 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13267 == SEC_ALLOC)
13268 break;
13269 if (s == NULL)
13270 for (s = obfd->sections; s != NULL; s = s->next)
13271 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13272 break;
13273 }
13274
13275 TOCstart = 0;
13276 if (s != NULL)
13277 TOCstart = s->output_section->vma + s->output_offset;
13278
13279 /* Force alignment. */
13280 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13281 TOCstart -= adjust;
13282 _bfd_set_gp_value (obfd, TOCstart);
13283
13284 if (info != NULL && s != NULL)
13285 {
13286 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13287
13288 if (htab != NULL)
13289 {
13290 if (htab->elf.hgot != NULL)
13291 {
13292 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13293 htab->elf.hgot->root.u.def.section = s;
13294 }
13295 }
13296 else
13297 {
13298 struct bfd_link_hash_entry *bh = NULL;
13299 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13300 s, TOC_BASE_OFF - adjust,
13301 NULL, FALSE, FALSE, &bh);
13302 }
13303 }
13304 return TOCstart;
13305 }
13306
13307 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13308 write out any global entry stubs, and PLT relocations. */
13309
13310 static bfd_boolean
13311 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13312 {
13313 struct bfd_link_info *info;
13314 struct ppc_link_hash_table *htab;
13315 struct plt_entry *ent;
13316 asection *s;
13317
13318 if (h->root.type == bfd_link_hash_indirect)
13319 return TRUE;
13320
13321 info = inf;
13322 htab = ppc_hash_table (info);
13323 if (htab == NULL)
13324 return FALSE;
13325
13326 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13327 if (ent->plt.offset != (bfd_vma) -1)
13328 {
13329 /* This symbol has an entry in the procedure linkage
13330 table. Set it up. */
13331 Elf_Internal_Rela rela;
13332 asection *plt, *relplt;
13333 bfd_byte *loc;
13334
13335 if (!htab->elf.dynamic_sections_created
13336 || h->dynindx == -1)
13337 {
13338 if (!(h->def_regular
13339 && (h->root.type == bfd_link_hash_defined
13340 || h->root.type == bfd_link_hash_defweak)))
13341 continue;
13342 if (h->type == STT_GNU_IFUNC)
13343 {
13344 plt = htab->elf.iplt;
13345 relplt = htab->elf.irelplt;
13346 htab->local_ifunc_resolver = 1;
13347 if (htab->opd_abi)
13348 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13349 else
13350 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13351 }
13352 else
13353 {
13354 plt = htab->pltlocal;
13355 if (bfd_link_pic (info))
13356 {
13357 relplt = htab->relpltlocal;
13358 if (htab->opd_abi)
13359 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13360 else
13361 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13362 }
13363 else
13364 relplt = NULL;
13365 }
13366 rela.r_addend = (h->root.u.def.value
13367 + h->root.u.def.section->output_offset
13368 + h->root.u.def.section->output_section->vma
13369 + ent->addend);
13370
13371 if (relplt == NULL)
13372 {
13373 loc = plt->contents + ent->plt.offset;
13374 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13375 if (htab->opd_abi)
13376 {
13377 bfd_vma toc = elf_gp (info->output_bfd);
13378 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13379 bfd_put_64 (info->output_bfd, toc, loc + 8);
13380 }
13381 }
13382 else
13383 {
13384 rela.r_offset = (plt->output_section->vma
13385 + plt->output_offset
13386 + ent->plt.offset);
13387 loc = relplt->contents + (relplt->reloc_count++
13388 * sizeof (Elf64_External_Rela));
13389 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13390 }
13391 }
13392 else
13393 {
13394 rela.r_offset = (htab->elf.splt->output_section->vma
13395 + htab->elf.splt->output_offset
13396 + ent->plt.offset);
13397 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13398 rela.r_addend = ent->addend;
13399 loc = (htab->elf.srelplt->contents
13400 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13401 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13402 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13403 htab->maybe_local_ifunc_resolver = 1;
13404 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13405 }
13406 }
13407
13408 if (!h->pointer_equality_needed)
13409 return TRUE;
13410
13411 if (h->def_regular)
13412 return TRUE;
13413
13414 s = htab->global_entry;
13415 if (s == NULL || s->size == 0)
13416 return TRUE;
13417
13418 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13419 if (ent->plt.offset != (bfd_vma) -1
13420 && ent->addend == 0)
13421 {
13422 bfd_byte *p;
13423 asection *plt;
13424 bfd_vma off;
13425
13426 p = s->contents + h->root.u.def.value;
13427 plt = htab->elf.splt;
13428 if (!htab->elf.dynamic_sections_created
13429 || h->dynindx == -1)
13430 {
13431 if (h->type == STT_GNU_IFUNC)
13432 plt = htab->elf.iplt;
13433 else
13434 plt = htab->pltlocal;
13435 }
13436 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13437 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13438
13439 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13440 {
13441 info->callbacks->einfo
13442 (_("%P: linkage table error against `%pT'\n"),
13443 h->root.root.string);
13444 bfd_set_error (bfd_error_bad_value);
13445 htab->stub_error = TRUE;
13446 }
13447
13448 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13449 if (htab->params->emit_stub_syms)
13450 {
13451 size_t len = strlen (h->root.root.string);
13452 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13453
13454 if (name == NULL)
13455 return FALSE;
13456
13457 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13458 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13459 if (h == NULL)
13460 return FALSE;
13461 if (h->root.type == bfd_link_hash_new)
13462 {
13463 h->root.type = bfd_link_hash_defined;
13464 h->root.u.def.section = s;
13465 h->root.u.def.value = p - s->contents;
13466 h->ref_regular = 1;
13467 h->def_regular = 1;
13468 h->ref_regular_nonweak = 1;
13469 h->forced_local = 1;
13470 h->non_elf = 0;
13471 h->root.linker_def = 1;
13472 }
13473 }
13474
13475 if (PPC_HA (off) != 0)
13476 {
13477 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13478 p += 4;
13479 }
13480 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13481 p += 4;
13482 bfd_put_32 (s->owner, MTCTR_R12, p);
13483 p += 4;
13484 bfd_put_32 (s->owner, BCTR, p);
13485 break;
13486 }
13487 return TRUE;
13488 }
13489
13490 /* Write PLT relocs for locals. */
13491
13492 static bfd_boolean
13493 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13494 {
13495 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13496 bfd *ibfd;
13497
13498 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13499 {
13500 struct got_entry **lgot_ents, **end_lgot_ents;
13501 struct plt_entry **local_plt, **lplt, **end_local_plt;
13502 Elf_Internal_Shdr *symtab_hdr;
13503 bfd_size_type locsymcount;
13504 Elf_Internal_Sym *local_syms = NULL;
13505 struct plt_entry *ent;
13506
13507 if (!is_ppc64_elf (ibfd))
13508 continue;
13509
13510 lgot_ents = elf_local_got_ents (ibfd);
13511 if (!lgot_ents)
13512 continue;
13513
13514 symtab_hdr = &elf_symtab_hdr (ibfd);
13515 locsymcount = symtab_hdr->sh_info;
13516 end_lgot_ents = lgot_ents + locsymcount;
13517 local_plt = (struct plt_entry **) end_lgot_ents;
13518 end_local_plt = local_plt + locsymcount;
13519 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13520 for (ent = *lplt; ent != NULL; ent = ent->next)
13521 if (ent->plt.offset != (bfd_vma) -1)
13522 {
13523 Elf_Internal_Sym *sym;
13524 asection *sym_sec;
13525 asection *plt, *relplt;
13526 bfd_byte *loc;
13527 bfd_vma val;
13528
13529 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13530 lplt - local_plt, ibfd))
13531 {
13532 if (local_syms != NULL
13533 && symtab_hdr->contents != (unsigned char *) local_syms)
13534 free (local_syms);
13535 return FALSE;
13536 }
13537
13538 val = sym->st_value + ent->addend;
13539 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13540 if (sym_sec != NULL && sym_sec->output_section != NULL)
13541 val += sym_sec->output_offset + sym_sec->output_section->vma;
13542
13543 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13544 {
13545 htab->local_ifunc_resolver = 1;
13546 plt = htab->elf.iplt;
13547 relplt = htab->elf.irelplt;
13548 }
13549 else
13550 {
13551 plt = htab->pltlocal;
13552 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13553 }
13554
13555 if (relplt == NULL)
13556 {
13557 loc = plt->contents + ent->plt.offset;
13558 bfd_put_64 (info->output_bfd, val, loc);
13559 if (htab->opd_abi)
13560 {
13561 bfd_vma toc = elf_gp (ibfd);
13562 bfd_put_64 (info->output_bfd, toc, loc + 8);
13563 }
13564 }
13565 else
13566 {
13567 Elf_Internal_Rela rela;
13568 rela.r_offset = (ent->plt.offset
13569 + plt->output_offset
13570 + plt->output_section->vma);
13571 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13572 {
13573 if (htab->opd_abi)
13574 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13575 else
13576 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13577 }
13578 else
13579 {
13580 if (htab->opd_abi)
13581 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13582 else
13583 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13584 }
13585 rela.r_addend = val;
13586 loc = relplt->contents + (relplt->reloc_count++
13587 * sizeof (Elf64_External_Rela));
13588 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13589 }
13590 }
13591
13592 if (local_syms != NULL
13593 && symtab_hdr->contents != (unsigned char *) local_syms)
13594 {
13595 if (!info->keep_memory)
13596 free (local_syms);
13597 else
13598 symtab_hdr->contents = (unsigned char *) local_syms;
13599 }
13600 }
13601 return TRUE;
13602 }
13603
13604 /* Build all the stubs associated with the current output file.
13605 The stubs are kept in a hash table attached to the main linker
13606 hash table. This function is called via gldelf64ppc_finish. */
13607
13608 bfd_boolean
13609 ppc64_elf_build_stubs (struct bfd_link_info *info,
13610 char **stats)
13611 {
13612 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13613 struct map_stub *group;
13614 asection *stub_sec;
13615 bfd_byte *p;
13616 int stub_sec_count = 0;
13617
13618 if (htab == NULL)
13619 return FALSE;
13620
13621 /* Allocate memory to hold the linker stubs. */
13622 for (group = htab->group; group != NULL; group = group->next)
13623 if ((stub_sec = group->stub_sec) != NULL
13624 && stub_sec->size != 0)
13625 {
13626 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13627 if (stub_sec->contents == NULL)
13628 return FALSE;
13629 stub_sec->size = 0;
13630 }
13631
13632 if (htab->glink != NULL && htab->glink->size != 0)
13633 {
13634 unsigned int indx;
13635 bfd_vma plt0;
13636
13637 /* Build the .glink plt call stub. */
13638 if (htab->params->emit_stub_syms)
13639 {
13640 struct elf_link_hash_entry *h;
13641 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13642 TRUE, FALSE, FALSE);
13643 if (h == NULL)
13644 return FALSE;
13645 if (h->root.type == bfd_link_hash_new)
13646 {
13647 h->root.type = bfd_link_hash_defined;
13648 h->root.u.def.section = htab->glink;
13649 h->root.u.def.value = 8;
13650 h->ref_regular = 1;
13651 h->def_regular = 1;
13652 h->ref_regular_nonweak = 1;
13653 h->forced_local = 1;
13654 h->non_elf = 0;
13655 h->root.linker_def = 1;
13656 }
13657 }
13658 plt0 = (htab->elf.splt->output_section->vma
13659 + htab->elf.splt->output_offset
13660 - 16);
13661 if (info->emitrelocations)
13662 {
13663 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13664 if (r == NULL)
13665 return FALSE;
13666 r->r_offset = (htab->glink->output_offset
13667 + htab->glink->output_section->vma);
13668 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13669 r->r_addend = plt0;
13670 }
13671 p = htab->glink->contents;
13672 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13673 bfd_put_64 (htab->glink->owner, plt0, p);
13674 p += 8;
13675 if (htab->opd_abi)
13676 {
13677 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13678 p += 4;
13679 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13680 p += 4;
13681 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13682 p += 4;
13683 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13684 p += 4;
13685 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13686 p += 4;
13687 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13688 p += 4;
13689 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13690 p += 4;
13691 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13692 p += 4;
13693 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13694 p += 4;
13695 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13696 p += 4;
13697 }
13698 else
13699 {
13700 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13701 p += 4;
13702 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13703 p += 4;
13704 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13705 p += 4;
13706 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13707 p += 4;
13708 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13709 p += 4;
13710 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13711 p += 4;
13712 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13713 p += 4;
13714 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13715 p += 4;
13716 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13717 p += 4;
13718 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13719 p += 4;
13720 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13721 p += 4;
13722 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13723 p += 4;
13724 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13725 p += 4;
13726 }
13727 bfd_put_32 (htab->glink->owner, BCTR, p);
13728 p += 4;
13729 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13730
13731 /* Build the .glink lazy link call stubs. */
13732 indx = 0;
13733 while (p < htab->glink->contents + htab->glink->size)
13734 {
13735 if (htab->opd_abi)
13736 {
13737 if (indx < 0x8000)
13738 {
13739 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13740 p += 4;
13741 }
13742 else
13743 {
13744 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13745 p += 4;
13746 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13747 p);
13748 p += 4;
13749 }
13750 }
13751 bfd_put_32 (htab->glink->owner,
13752 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13753 indx++;
13754 p += 4;
13755 }
13756 }
13757
13758 /* Build .glink global entry stubs, and PLT relocs for globals. */
13759 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13760
13761 if (!write_plt_relocs_for_local_syms (info))
13762 return FALSE;
13763
13764 if (htab->brlt != NULL && htab->brlt->size != 0)
13765 {
13766 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13767 htab->brlt->size);
13768 if (htab->brlt->contents == NULL)
13769 return FALSE;
13770 }
13771 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13772 {
13773 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13774 htab->relbrlt->size);
13775 if (htab->relbrlt->contents == NULL)
13776 return FALSE;
13777 }
13778
13779 /* Build the stubs as directed by the stub hash table. */
13780 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13781
13782 for (group = htab->group; group != NULL; group = group->next)
13783 if (group->needs_save_res)
13784 group->stub_sec->size += htab->sfpr->size;
13785
13786 if (htab->relbrlt != NULL)
13787 htab->relbrlt->reloc_count = 0;
13788
13789 if (htab->params->plt_stub_align != 0)
13790 for (group = htab->group; group != NULL; group = group->next)
13791 if ((stub_sec = group->stub_sec) != NULL)
13792 {
13793 int align = abs (htab->params->plt_stub_align);
13794 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13795 }
13796
13797 for (group = htab->group; group != NULL; group = group->next)
13798 if (group->needs_save_res)
13799 {
13800 stub_sec = group->stub_sec;
13801 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13802 htab->sfpr->contents, htab->sfpr->size);
13803 if (htab->params->emit_stub_syms)
13804 {
13805 unsigned int i;
13806
13807 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13808 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13809 return FALSE;
13810 }
13811 }
13812
13813 for (group = htab->group; group != NULL; group = group->next)
13814 if ((stub_sec = group->stub_sec) != NULL)
13815 {
13816 stub_sec_count += 1;
13817 if (stub_sec->rawsize != stub_sec->size
13818 && (htab->stub_iteration <= STUB_SHRINK_ITER
13819 || stub_sec->rawsize < stub_sec->size))
13820 break;
13821 }
13822
13823 if (group != NULL)
13824 {
13825 htab->stub_error = TRUE;
13826 _bfd_error_handler (_("stubs don't match calculated size"));
13827 }
13828
13829 if (htab->stub_error)
13830 return FALSE;
13831
13832 if (stats != NULL)
13833 {
13834 size_t len;
13835 *stats = bfd_malloc (500);
13836 if (*stats == NULL)
13837 return FALSE;
13838
13839 len = sprintf (*stats,
13840 ngettext ("linker stubs in %u group\n",
13841 "linker stubs in %u groups\n",
13842 stub_sec_count),
13843 stub_sec_count);
13844 sprintf (*stats + len, _(" branch %lu\n"
13845 " toc adjust %lu\n"
13846 " long branch %lu\n"
13847 " long toc adj %lu\n"
13848 " plt call %lu\n"
13849 " plt call toc %lu\n"
13850 " global entry %lu"),
13851 htab->stub_count[ppc_stub_long_branch - 1],
13852 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13853 htab->stub_count[ppc_stub_plt_branch - 1],
13854 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13855 htab->stub_count[ppc_stub_plt_call - 1],
13856 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13857 htab->stub_count[ppc_stub_global_entry - 1]);
13858 }
13859 return TRUE;
13860 }
13861
13862 /* What to do when ld finds relocations against symbols defined in
13863 discarded sections. */
13864
13865 static unsigned int
13866 ppc64_elf_action_discarded (asection *sec)
13867 {
13868 if (strcmp (".opd", sec->name) == 0)
13869 return 0;
13870
13871 if (strcmp (".toc", sec->name) == 0)
13872 return 0;
13873
13874 if (strcmp (".toc1", sec->name) == 0)
13875 return 0;
13876
13877 return _bfd_elf_default_action_discarded (sec);
13878 }
13879
13880 /* The RELOCATE_SECTION function is called by the ELF backend linker
13881 to handle the relocations for a section.
13882
13883 The relocs are always passed as Rela structures; if the section
13884 actually uses Rel structures, the r_addend field will always be
13885 zero.
13886
13887 This function is responsible for adjust the section contents as
13888 necessary, and (if using Rela relocs and generating a
13889 relocatable output file) adjusting the reloc addend as
13890 necessary.
13891
13892 This function does not have to worry about setting the reloc
13893 address or the reloc symbol index.
13894
13895 LOCAL_SYMS is a pointer to the swapped in local symbols.
13896
13897 LOCAL_SECTIONS is an array giving the section in the input file
13898 corresponding to the st_shndx field of each local symbol.
13899
13900 The global hash table entry for the global symbols can be found
13901 via elf_sym_hashes (input_bfd).
13902
13903 When generating relocatable output, this function must handle
13904 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13905 going to be the section symbol corresponding to the output
13906 section, which means that the addend must be adjusted
13907 accordingly. */
13908
13909 static bfd_boolean
13910 ppc64_elf_relocate_section (bfd *output_bfd,
13911 struct bfd_link_info *info,
13912 bfd *input_bfd,
13913 asection *input_section,
13914 bfd_byte *contents,
13915 Elf_Internal_Rela *relocs,
13916 Elf_Internal_Sym *local_syms,
13917 asection **local_sections)
13918 {
13919 struct ppc_link_hash_table *htab;
13920 Elf_Internal_Shdr *symtab_hdr;
13921 struct elf_link_hash_entry **sym_hashes;
13922 Elf_Internal_Rela *rel;
13923 Elf_Internal_Rela *wrel;
13924 Elf_Internal_Rela *relend;
13925 Elf_Internal_Rela outrel;
13926 bfd_byte *loc;
13927 struct got_entry **local_got_ents;
13928 bfd_vma TOCstart;
13929 bfd_boolean ret = TRUE;
13930 bfd_boolean is_opd;
13931 /* Assume 'at' branch hints. */
13932 bfd_boolean is_isa_v2 = TRUE;
13933 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13934
13935 /* Initialize howto table if needed. */
13936 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13937 ppc_howto_init ();
13938
13939 htab = ppc_hash_table (info);
13940 if (htab == NULL)
13941 return FALSE;
13942
13943 /* Don't relocate stub sections. */
13944 if (input_section->owner == htab->params->stub_bfd)
13945 return TRUE;
13946
13947 BFD_ASSERT (is_ppc64_elf (input_bfd));
13948
13949 local_got_ents = elf_local_got_ents (input_bfd);
13950 TOCstart = elf_gp (output_bfd);
13951 symtab_hdr = &elf_symtab_hdr (input_bfd);
13952 sym_hashes = elf_sym_hashes (input_bfd);
13953 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13954
13955 rel = wrel = relocs;
13956 relend = relocs + input_section->reloc_count;
13957 for (; rel < relend; wrel++, rel++)
13958 {
13959 enum elf_ppc64_reloc_type r_type;
13960 bfd_vma addend;
13961 bfd_reloc_status_type r;
13962 Elf_Internal_Sym *sym;
13963 asection *sec;
13964 struct elf_link_hash_entry *h_elf;
13965 struct ppc_link_hash_entry *h;
13966 struct ppc_link_hash_entry *fdh;
13967 const char *sym_name;
13968 unsigned long r_symndx, toc_symndx;
13969 bfd_vma toc_addend;
13970 unsigned char tls_mask, tls_gd, tls_type;
13971 unsigned char sym_type;
13972 bfd_vma relocation;
13973 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13974 bfd_boolean warned;
13975 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13976 unsigned int insn;
13977 unsigned int mask;
13978 struct ppc_stub_hash_entry *stub_entry;
13979 bfd_vma max_br_offset;
13980 bfd_vma from;
13981 Elf_Internal_Rela orig_rel;
13982 reloc_howto_type *howto;
13983 struct reloc_howto_struct alt_howto;
13984
13985 again:
13986 orig_rel = *rel;
13987
13988 r_type = ELF64_R_TYPE (rel->r_info);
13989 r_symndx = ELF64_R_SYM (rel->r_info);
13990
13991 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13992 symbol of the previous ADDR64 reloc. The symbol gives us the
13993 proper TOC base to use. */
13994 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13995 && wrel != relocs
13996 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13997 && is_opd)
13998 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13999
14000 sym = NULL;
14001 sec = NULL;
14002 h_elf = NULL;
14003 sym_name = NULL;
14004 unresolved_reloc = FALSE;
14005 warned = FALSE;
14006
14007 if (r_symndx < symtab_hdr->sh_info)
14008 {
14009 /* It's a local symbol. */
14010 struct _opd_sec_data *opd;
14011
14012 sym = local_syms + r_symndx;
14013 sec = local_sections[r_symndx];
14014 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14015 sym_type = ELF64_ST_TYPE (sym->st_info);
14016 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14017 opd = get_opd_info (sec);
14018 if (opd != NULL && opd->adjust != NULL)
14019 {
14020 long adjust = opd->adjust[OPD_NDX (sym->st_value
14021 + rel->r_addend)];
14022 if (adjust == -1)
14023 relocation = 0;
14024 else
14025 {
14026 /* If this is a relocation against the opd section sym
14027 and we have edited .opd, adjust the reloc addend so
14028 that ld -r and ld --emit-relocs output is correct.
14029 If it is a reloc against some other .opd symbol,
14030 then the symbol value will be adjusted later. */
14031 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14032 rel->r_addend += adjust;
14033 else
14034 relocation += adjust;
14035 }
14036 }
14037 }
14038 else
14039 {
14040 bfd_boolean ignored;
14041
14042 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14043 r_symndx, symtab_hdr, sym_hashes,
14044 h_elf, sec, relocation,
14045 unresolved_reloc, warned, ignored);
14046 sym_name = h_elf->root.root.string;
14047 sym_type = h_elf->type;
14048 if (sec != NULL
14049 && sec->owner == output_bfd
14050 && strcmp (sec->name, ".opd") == 0)
14051 {
14052 /* This is a symbol defined in a linker script. All
14053 such are defined in output sections, even those
14054 defined by simple assignment from a symbol defined in
14055 an input section. Transfer the symbol to an
14056 appropriate input .opd section, so that a branch to
14057 this symbol will be mapped to the location specified
14058 by the opd entry. */
14059 struct bfd_link_order *lo;
14060 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14061 if (lo->type == bfd_indirect_link_order)
14062 {
14063 asection *isec = lo->u.indirect.section;
14064 if (h_elf->root.u.def.value >= isec->output_offset
14065 && h_elf->root.u.def.value < (isec->output_offset
14066 + isec->size))
14067 {
14068 h_elf->root.u.def.value -= isec->output_offset;
14069 h_elf->root.u.def.section = isec;
14070 sec = isec;
14071 break;
14072 }
14073 }
14074 }
14075 }
14076 h = (struct ppc_link_hash_entry *) h_elf;
14077
14078 if (sec != NULL && discarded_section (sec))
14079 {
14080 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14081 input_bfd, input_section,
14082 contents + rel->r_offset);
14083 wrel->r_offset = rel->r_offset;
14084 wrel->r_info = 0;
14085 wrel->r_addend = 0;
14086
14087 /* For ld -r, remove relocations in debug sections against
14088 symbols defined in discarded sections. Not done for
14089 non-debug to preserve relocs in .eh_frame which the
14090 eh_frame editing code expects to be present. */
14091 if (bfd_link_relocatable (info)
14092 && (input_section->flags & SEC_DEBUGGING))
14093 wrel--;
14094
14095 continue;
14096 }
14097
14098 if (bfd_link_relocatable (info))
14099 goto copy_reloc;
14100
14101 if (h != NULL && &h->elf == htab->elf.hgot)
14102 {
14103 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14104 sec = bfd_abs_section_ptr;
14105 unresolved_reloc = FALSE;
14106 }
14107
14108 /* TLS optimizations. Replace instruction sequences and relocs
14109 based on information we collected in tls_optimize. We edit
14110 RELOCS so that --emit-relocs will output something sensible
14111 for the final instruction stream. */
14112 tls_mask = 0;
14113 tls_gd = 0;
14114 toc_symndx = 0;
14115 if (h != NULL)
14116 tls_mask = h->tls_mask;
14117 else if (local_got_ents != NULL)
14118 {
14119 struct plt_entry **local_plt = (struct plt_entry **)
14120 (local_got_ents + symtab_hdr->sh_info);
14121 unsigned char *lgot_masks = (unsigned char *)
14122 (local_plt + symtab_hdr->sh_info);
14123 tls_mask = lgot_masks[r_symndx];
14124 }
14125 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14126 && (r_type == R_PPC64_TLS
14127 || r_type == R_PPC64_TLSGD
14128 || r_type == R_PPC64_TLSLD))
14129 {
14130 /* Check for toc tls entries. */
14131 unsigned char *toc_tls;
14132
14133 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14134 &local_syms, rel, input_bfd))
14135 return FALSE;
14136
14137 if (toc_tls)
14138 tls_mask = *toc_tls;
14139 }
14140
14141 /* Check that tls relocs are used with tls syms, and non-tls
14142 relocs are used with non-tls syms. */
14143 if (r_symndx != STN_UNDEF
14144 && r_type != R_PPC64_NONE
14145 && (h == NULL
14146 || h->elf.root.type == bfd_link_hash_defined
14147 || h->elf.root.type == bfd_link_hash_defweak)
14148 && (IS_PPC64_TLS_RELOC (r_type)
14149 != (sym_type == STT_TLS
14150 || (sym_type == STT_SECTION
14151 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14152 {
14153 if ((tls_mask & TLS_TLS) != 0
14154 && (r_type == R_PPC64_TLS
14155 || r_type == R_PPC64_TLSGD
14156 || r_type == R_PPC64_TLSLD))
14157 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14158 ;
14159 else
14160 info->callbacks->einfo
14161 (!IS_PPC64_TLS_RELOC (r_type)
14162 /* xgettext:c-format */
14163 ? _("%H: %s used with TLS symbol `%pT'\n")
14164 /* xgettext:c-format */
14165 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14166 input_bfd, input_section, rel->r_offset,
14167 ppc64_elf_howto_table[r_type]->name,
14168 sym_name);
14169 }
14170
14171 /* Ensure reloc mapping code below stays sane. */
14172 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14173 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14174 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14175 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14176 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14177 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14178 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14179 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14180 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14181 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14182 abort ();
14183
14184 switch (r_type)
14185 {
14186 default:
14187 break;
14188
14189 case R_PPC64_LO_DS_OPT:
14190 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14191 if ((insn & (0x3f << 26)) != 58u << 26)
14192 abort ();
14193 insn += (14u << 26) - (58u << 26);
14194 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14195 r_type = R_PPC64_TOC16_LO;
14196 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14197 break;
14198
14199 case R_PPC64_TOC16:
14200 case R_PPC64_TOC16_LO:
14201 case R_PPC64_TOC16_DS:
14202 case R_PPC64_TOC16_LO_DS:
14203 {
14204 /* Check for toc tls entries. */
14205 unsigned char *toc_tls;
14206 int retval;
14207
14208 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14209 &local_syms, rel, input_bfd);
14210 if (retval == 0)
14211 return FALSE;
14212
14213 if (toc_tls)
14214 {
14215 tls_mask = *toc_tls;
14216 if (r_type == R_PPC64_TOC16_DS
14217 || r_type == R_PPC64_TOC16_LO_DS)
14218 {
14219 if ((tls_mask & TLS_TLS) != 0
14220 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14221 goto toctprel;
14222 }
14223 else
14224 {
14225 /* If we found a GD reloc pair, then we might be
14226 doing a GD->IE transition. */
14227 if (retval == 2)
14228 {
14229 tls_gd = TLS_TPRELGD;
14230 if ((tls_mask & TLS_TLS) != 0
14231 && (tls_mask & TLS_GD) == 0)
14232 goto tls_ldgd_opt;
14233 }
14234 else if (retval == 3)
14235 {
14236 if ((tls_mask & TLS_TLS) != 0
14237 && (tls_mask & TLS_LD) == 0)
14238 goto tls_ldgd_opt;
14239 }
14240 }
14241 }
14242 }
14243 break;
14244
14245 case R_PPC64_GOT_TPREL16_HI:
14246 case R_PPC64_GOT_TPREL16_HA:
14247 if ((tls_mask & TLS_TLS) != 0
14248 && (tls_mask & TLS_TPREL) == 0)
14249 {
14250 rel->r_offset -= d_offset;
14251 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14252 r_type = R_PPC64_NONE;
14253 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14254 }
14255 break;
14256
14257 case R_PPC64_GOT_TPREL16_DS:
14258 case R_PPC64_GOT_TPREL16_LO_DS:
14259 if ((tls_mask & TLS_TLS) != 0
14260 && (tls_mask & TLS_TPREL) == 0)
14261 {
14262 toctprel:
14263 insn = bfd_get_32 (input_bfd,
14264 contents + rel->r_offset - d_offset);
14265 insn &= 31 << 21;
14266 insn |= 0x3c0d0000; /* addis 0,13,0 */
14267 bfd_put_32 (input_bfd, insn,
14268 contents + rel->r_offset - d_offset);
14269 r_type = R_PPC64_TPREL16_HA;
14270 if (toc_symndx != 0)
14271 {
14272 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14273 rel->r_addend = toc_addend;
14274 /* We changed the symbol. Start over in order to
14275 get h, sym, sec etc. right. */
14276 goto again;
14277 }
14278 else
14279 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14280 }
14281 break;
14282
14283 case R_PPC64_TLS:
14284 if ((tls_mask & TLS_TLS) != 0
14285 && (tls_mask & TLS_TPREL) == 0)
14286 {
14287 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14288 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14289 if (insn == 0)
14290 abort ();
14291 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14292 /* Was PPC64_TLS which sits on insn boundary, now
14293 PPC64_TPREL16_LO which is at low-order half-word. */
14294 rel->r_offset += d_offset;
14295 r_type = R_PPC64_TPREL16_LO;
14296 if (toc_symndx != 0)
14297 {
14298 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14299 rel->r_addend = toc_addend;
14300 /* We changed the symbol. Start over in order to
14301 get h, sym, sec etc. right. */
14302 goto again;
14303 }
14304 else
14305 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14306 }
14307 break;
14308
14309 case R_PPC64_GOT_TLSGD16_HI:
14310 case R_PPC64_GOT_TLSGD16_HA:
14311 tls_gd = TLS_TPRELGD;
14312 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14313 goto tls_gdld_hi;
14314 break;
14315
14316 case R_PPC64_GOT_TLSLD16_HI:
14317 case R_PPC64_GOT_TLSLD16_HA:
14318 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14319 {
14320 tls_gdld_hi:
14321 if ((tls_mask & tls_gd) != 0)
14322 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14323 + R_PPC64_GOT_TPREL16_DS);
14324 else
14325 {
14326 rel->r_offset -= d_offset;
14327 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14328 r_type = R_PPC64_NONE;
14329 }
14330 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14331 }
14332 break;
14333
14334 case R_PPC64_GOT_TLSGD16:
14335 case R_PPC64_GOT_TLSGD16_LO:
14336 tls_gd = TLS_TPRELGD;
14337 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14338 goto tls_ldgd_opt;
14339 break;
14340
14341 case R_PPC64_GOT_TLSLD16:
14342 case R_PPC64_GOT_TLSLD16_LO:
14343 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14344 {
14345 unsigned int insn1, insn2;
14346 bfd_vma offset;
14347
14348 tls_ldgd_opt:
14349 offset = (bfd_vma) -1;
14350 /* If not using the newer R_PPC64_TLSGD/LD to mark
14351 __tls_get_addr calls, we must trust that the call
14352 stays with its arg setup insns, ie. that the next
14353 reloc is the __tls_get_addr call associated with
14354 the current reloc. Edit both insns. */
14355 if (input_section->has_tls_get_addr_call
14356 && rel + 1 < relend
14357 && branch_reloc_hash_match (input_bfd, rel + 1,
14358 htab->tls_get_addr,
14359 htab->tls_get_addr_fd))
14360 offset = rel[1].r_offset;
14361 /* We read the low GOT_TLS (or TOC16) insn because we
14362 need to keep the destination reg. It may be
14363 something other than the usual r3, and moved to r3
14364 before the call by intervening code. */
14365 insn1 = bfd_get_32 (input_bfd,
14366 contents + rel->r_offset - d_offset);
14367 if ((tls_mask & tls_gd) != 0)
14368 {
14369 /* IE */
14370 insn1 &= (0x1f << 21) | (0x1f << 16);
14371 insn1 |= 58 << 26; /* ld */
14372 insn2 = 0x7c636a14; /* add 3,3,13 */
14373 if (offset != (bfd_vma) -1)
14374 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14375 if ((tls_mask & TLS_EXPLICIT) == 0)
14376 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14377 + R_PPC64_GOT_TPREL16_DS);
14378 else
14379 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14380 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14381 }
14382 else
14383 {
14384 /* LE */
14385 insn1 &= 0x1f << 21;
14386 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14387 insn2 = 0x38630000; /* addi 3,3,0 */
14388 if (tls_gd == 0)
14389 {
14390 /* Was an LD reloc. */
14391 if (toc_symndx)
14392 sec = local_sections[toc_symndx];
14393 for (r_symndx = 0;
14394 r_symndx < symtab_hdr->sh_info;
14395 r_symndx++)
14396 if (local_sections[r_symndx] == sec)
14397 break;
14398 if (r_symndx >= symtab_hdr->sh_info)
14399 r_symndx = STN_UNDEF;
14400 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14401 if (r_symndx != STN_UNDEF)
14402 rel->r_addend -= (local_syms[r_symndx].st_value
14403 + sec->output_offset
14404 + sec->output_section->vma);
14405 }
14406 else if (toc_symndx != 0)
14407 {
14408 r_symndx = toc_symndx;
14409 rel->r_addend = toc_addend;
14410 }
14411 r_type = R_PPC64_TPREL16_HA;
14412 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14413 if (offset != (bfd_vma) -1)
14414 {
14415 rel[1].r_info = ELF64_R_INFO (r_symndx,
14416 R_PPC64_TPREL16_LO);
14417 rel[1].r_offset = offset + d_offset;
14418 rel[1].r_addend = rel->r_addend;
14419 }
14420 }
14421 bfd_put_32 (input_bfd, insn1,
14422 contents + rel->r_offset - d_offset);
14423 if (offset != (bfd_vma) -1)
14424 bfd_put_32 (input_bfd, insn2, contents + offset);
14425 if ((tls_mask & tls_gd) == 0
14426 && (tls_gd == 0 || toc_symndx != 0))
14427 {
14428 /* We changed the symbol. Start over in order
14429 to get h, sym, sec etc. right. */
14430 goto again;
14431 }
14432 }
14433 break;
14434
14435 case R_PPC64_TLSGD:
14436 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14437 && rel + 1 < relend)
14438 {
14439 unsigned int insn2;
14440 bfd_vma offset = rel->r_offset;
14441
14442 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14443 {
14444 bfd_put_32 (output_bfd, NOP, contents + offset);
14445 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14446 break;
14447 }
14448
14449 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14450 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14451
14452 if ((tls_mask & TLS_TPRELGD) != 0)
14453 {
14454 /* IE */
14455 r_type = R_PPC64_NONE;
14456 insn2 = 0x7c636a14; /* add 3,3,13 */
14457 }
14458 else
14459 {
14460 /* LE */
14461 if (toc_symndx != 0)
14462 {
14463 r_symndx = toc_symndx;
14464 rel->r_addend = toc_addend;
14465 }
14466 r_type = R_PPC64_TPREL16_LO;
14467 rel->r_offset = offset + d_offset;
14468 insn2 = 0x38630000; /* addi 3,3,0 */
14469 }
14470 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14471 /* Zap the reloc on the _tls_get_addr call too. */
14472 BFD_ASSERT (offset == rel[1].r_offset);
14473 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14474 bfd_put_32 (input_bfd, insn2, contents + offset);
14475 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14476 goto again;
14477 }
14478 break;
14479
14480 case R_PPC64_TLSLD:
14481 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14482 && rel + 1 < relend)
14483 {
14484 unsigned int insn2;
14485 bfd_vma offset = rel->r_offset;
14486
14487 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14488 {
14489 bfd_put_32 (output_bfd, NOP, contents + offset);
14490 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14491 break;
14492 }
14493
14494 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14495 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14496
14497 if (toc_symndx)
14498 sec = local_sections[toc_symndx];
14499 for (r_symndx = 0;
14500 r_symndx < symtab_hdr->sh_info;
14501 r_symndx++)
14502 if (local_sections[r_symndx] == sec)
14503 break;
14504 if (r_symndx >= symtab_hdr->sh_info)
14505 r_symndx = STN_UNDEF;
14506 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14507 if (r_symndx != STN_UNDEF)
14508 rel->r_addend -= (local_syms[r_symndx].st_value
14509 + sec->output_offset
14510 + sec->output_section->vma);
14511
14512 r_type = R_PPC64_TPREL16_LO;
14513 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14514 rel->r_offset = offset + d_offset;
14515 /* Zap the reloc on the _tls_get_addr call too. */
14516 BFD_ASSERT (offset == rel[1].r_offset);
14517 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14518 insn2 = 0x38630000; /* addi 3,3,0 */
14519 bfd_put_32 (input_bfd, insn2, contents + offset);
14520 goto again;
14521 }
14522 break;
14523
14524 case R_PPC64_DTPMOD64:
14525 if (rel + 1 < relend
14526 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14527 && rel[1].r_offset == rel->r_offset + 8)
14528 {
14529 if ((tls_mask & TLS_GD) == 0)
14530 {
14531 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14532 if ((tls_mask & TLS_TPRELGD) != 0)
14533 r_type = R_PPC64_TPREL64;
14534 else
14535 {
14536 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14537 r_type = R_PPC64_NONE;
14538 }
14539 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14540 }
14541 }
14542 else
14543 {
14544 if ((tls_mask & TLS_LD) == 0)
14545 {
14546 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14547 r_type = R_PPC64_NONE;
14548 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14549 }
14550 }
14551 break;
14552
14553 case R_PPC64_TPREL64:
14554 if ((tls_mask & TLS_TPREL) == 0)
14555 {
14556 r_type = R_PPC64_NONE;
14557 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14558 }
14559 break;
14560
14561 case R_PPC64_ENTRY:
14562 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14563 if (!bfd_link_pic (info)
14564 && !info->traditional_format
14565 && relocation + 0x80008000 <= 0xffffffff)
14566 {
14567 unsigned int insn1, insn2;
14568
14569 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14570 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14571 if ((insn1 & ~0xfffc) == LD_R2_0R12
14572 && insn2 == ADD_R2_R2_R12)
14573 {
14574 bfd_put_32 (input_bfd,
14575 LIS_R2 + PPC_HA (relocation),
14576 contents + rel->r_offset);
14577 bfd_put_32 (input_bfd,
14578 ADDI_R2_R2 + PPC_LO (relocation),
14579 contents + rel->r_offset + 4);
14580 }
14581 }
14582 else
14583 {
14584 relocation -= (rel->r_offset
14585 + input_section->output_offset
14586 + input_section->output_section->vma);
14587 if (relocation + 0x80008000 <= 0xffffffff)
14588 {
14589 unsigned int insn1, insn2;
14590
14591 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14592 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14593 if ((insn1 & ~0xfffc) == LD_R2_0R12
14594 && insn2 == ADD_R2_R2_R12)
14595 {
14596 bfd_put_32 (input_bfd,
14597 ADDIS_R2_R12 + PPC_HA (relocation),
14598 contents + rel->r_offset);
14599 bfd_put_32 (input_bfd,
14600 ADDI_R2_R2 + PPC_LO (relocation),
14601 contents + rel->r_offset + 4);
14602 }
14603 }
14604 }
14605 break;
14606
14607 case R_PPC64_REL16_HA:
14608 /* If we are generating a non-PIC executable, edit
14609 . 0: addis 2,12,.TOC.-0b@ha
14610 . addi 2,2,.TOC.-0b@l
14611 used by ELFv2 global entry points to set up r2, to
14612 . lis 2,.TOC.@ha
14613 . addi 2,2,.TOC.@l
14614 if .TOC. is in range. */
14615 if (!bfd_link_pic (info)
14616 && !info->traditional_format
14617 && !htab->opd_abi
14618 && rel->r_addend == d_offset
14619 && h != NULL && &h->elf == htab->elf.hgot
14620 && rel + 1 < relend
14621 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14622 && rel[1].r_offset == rel->r_offset + 4
14623 && rel[1].r_addend == rel->r_addend + 4
14624 && relocation + 0x80008000 <= 0xffffffff)
14625 {
14626 unsigned int insn1, insn2;
14627 bfd_vma offset = rel->r_offset - d_offset;
14628 insn1 = bfd_get_32 (input_bfd, contents + offset);
14629 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14630 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14631 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14632 {
14633 r_type = R_PPC64_ADDR16_HA;
14634 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14635 rel->r_addend -= d_offset;
14636 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14637 rel[1].r_addend -= d_offset + 4;
14638 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14639 }
14640 }
14641 break;
14642 }
14643
14644 /* Handle other relocations that tweak non-addend part of insn. */
14645 insn = 0;
14646 max_br_offset = 1 << 25;
14647 addend = rel->r_addend;
14648 reloc_dest = DEST_NORMAL;
14649 switch (r_type)
14650 {
14651 default:
14652 break;
14653
14654 case R_PPC64_TOCSAVE:
14655 if (relocation + addend == (rel->r_offset
14656 + input_section->output_offset
14657 + input_section->output_section->vma)
14658 && tocsave_find (htab, NO_INSERT,
14659 &local_syms, rel, input_bfd))
14660 {
14661 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14662 if (insn == NOP
14663 || insn == CROR_151515 || insn == CROR_313131)
14664 bfd_put_32 (input_bfd,
14665 STD_R2_0R1 + STK_TOC (htab),
14666 contents + rel->r_offset);
14667 }
14668 break;
14669
14670 /* Branch taken prediction relocations. */
14671 case R_PPC64_ADDR14_BRTAKEN:
14672 case R_PPC64_REL14_BRTAKEN:
14673 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14674 /* Fall through. */
14675
14676 /* Branch not taken prediction relocations. */
14677 case R_PPC64_ADDR14_BRNTAKEN:
14678 case R_PPC64_REL14_BRNTAKEN:
14679 insn |= bfd_get_32 (input_bfd,
14680 contents + rel->r_offset) & ~(0x01 << 21);
14681 /* Fall through. */
14682
14683 case R_PPC64_REL14:
14684 max_br_offset = 1 << 15;
14685 /* Fall through. */
14686
14687 case R_PPC64_REL24:
14688 case R_PPC64_PLTCALL:
14689 /* Calls to functions with a different TOC, such as calls to
14690 shared objects, need to alter the TOC pointer. This is
14691 done using a linkage stub. A REL24 branching to these
14692 linkage stubs needs to be followed by a nop, as the nop
14693 will be replaced with an instruction to restore the TOC
14694 base pointer. */
14695 fdh = h;
14696 if (h != NULL
14697 && h->oh != NULL
14698 && h->oh->is_func_descriptor)
14699 fdh = ppc_follow_link (h->oh);
14700 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14701 htab);
14702 if (r_type == R_PPC64_PLTCALL
14703 && stub_entry != NULL
14704 && (stub_entry->stub_type == ppc_stub_plt_call
14705 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14706 stub_entry = NULL;
14707
14708 if (stub_entry != NULL
14709 && (stub_entry->stub_type == ppc_stub_plt_call
14710 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14711 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14712 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14713 {
14714 bfd_boolean can_plt_call = FALSE;
14715
14716 if (stub_entry->stub_type == ppc_stub_plt_call
14717 && !htab->opd_abi
14718 && htab->params->plt_localentry0 != 0
14719 && is_elfv2_localentry0 (&h->elf))
14720 {
14721 /* The function doesn't use or change r2. */
14722 can_plt_call = TRUE;
14723 }
14724
14725 /* All of these stubs may modify r2, so there must be a
14726 branch and link followed by a nop. The nop is
14727 replaced by an insn to restore r2. */
14728 else if (rel->r_offset + 8 <= input_section->size)
14729 {
14730 unsigned long br;
14731
14732 br = bfd_get_32 (input_bfd,
14733 contents + rel->r_offset);
14734 if ((br & 1) != 0)
14735 {
14736 unsigned long nop;
14737
14738 nop = bfd_get_32 (input_bfd,
14739 contents + rel->r_offset + 4);
14740 if (nop == LD_R2_0R1 + STK_TOC (htab))
14741 can_plt_call = TRUE;
14742 else if (nop == NOP
14743 || nop == CROR_151515
14744 || nop == CROR_313131)
14745 {
14746 if (h != NULL
14747 && (h == htab->tls_get_addr_fd
14748 || h == htab->tls_get_addr)
14749 && htab->params->tls_get_addr_opt)
14750 {
14751 /* Special stub used, leave nop alone. */
14752 }
14753 else
14754 bfd_put_32 (input_bfd,
14755 LD_R2_0R1 + STK_TOC (htab),
14756 contents + rel->r_offset + 4);
14757 can_plt_call = TRUE;
14758 }
14759 }
14760 }
14761
14762 if (!can_plt_call && h != NULL)
14763 {
14764 const char *name = h->elf.root.root.string;
14765
14766 if (*name == '.')
14767 ++name;
14768
14769 if (strncmp (name, "__libc_start_main", 17) == 0
14770 && (name[17] == 0 || name[17] == '@'))
14771 {
14772 /* Allow crt1 branch to go via a toc adjusting
14773 stub. Other calls that never return could do
14774 the same, if we could detect such. */
14775 can_plt_call = TRUE;
14776 }
14777 }
14778
14779 if (!can_plt_call)
14780 {
14781 /* g++ as of 20130507 emits self-calls without a
14782 following nop. This is arguably wrong since we
14783 have conflicting information. On the one hand a
14784 global symbol and on the other a local call
14785 sequence, but don't error for this special case.
14786 It isn't possible to cheaply verify we have
14787 exactly such a call. Allow all calls to the same
14788 section. */
14789 asection *code_sec = sec;
14790
14791 if (get_opd_info (sec) != NULL)
14792 {
14793 bfd_vma off = (relocation + addend
14794 - sec->output_section->vma
14795 - sec->output_offset);
14796
14797 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14798 }
14799 if (code_sec == input_section)
14800 can_plt_call = TRUE;
14801 }
14802
14803 if (!can_plt_call)
14804 {
14805 if (stub_entry->stub_type == ppc_stub_plt_call
14806 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14807 info->callbacks->einfo
14808 /* xgettext:c-format */
14809 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14810 "recompile with -fPIC\n"),
14811 input_bfd, input_section, rel->r_offset, sym_name);
14812 else
14813 info->callbacks->einfo
14814 /* xgettext:c-format */
14815 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14816 "(-mcmodel=small toc adjust stub)\n"),
14817 input_bfd, input_section, rel->r_offset, sym_name);
14818
14819 bfd_set_error (bfd_error_bad_value);
14820 ret = FALSE;
14821 }
14822
14823 if (can_plt_call
14824 && (stub_entry->stub_type == ppc_stub_plt_call
14825 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14826 unresolved_reloc = FALSE;
14827 }
14828
14829 if ((stub_entry == NULL
14830 || stub_entry->stub_type == ppc_stub_long_branch
14831 || stub_entry->stub_type == ppc_stub_plt_branch)
14832 && get_opd_info (sec) != NULL)
14833 {
14834 /* The branch destination is the value of the opd entry. */
14835 bfd_vma off = (relocation + addend
14836 - sec->output_section->vma
14837 - sec->output_offset);
14838 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14839 if (dest != (bfd_vma) -1)
14840 {
14841 relocation = dest;
14842 addend = 0;
14843 reloc_dest = DEST_OPD;
14844 }
14845 }
14846
14847 /* If the branch is out of reach we ought to have a long
14848 branch stub. */
14849 from = (rel->r_offset
14850 + input_section->output_offset
14851 + input_section->output_section->vma);
14852
14853 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14854 ? fdh->elf.other
14855 : sym->st_other);
14856
14857 if (stub_entry != NULL
14858 && (stub_entry->stub_type == ppc_stub_long_branch
14859 || stub_entry->stub_type == ppc_stub_plt_branch)
14860 && (r_type == R_PPC64_ADDR14_BRTAKEN
14861 || r_type == R_PPC64_ADDR14_BRNTAKEN
14862 || (relocation + addend - from + max_br_offset
14863 < 2 * max_br_offset)))
14864 /* Don't use the stub if this branch is in range. */
14865 stub_entry = NULL;
14866
14867 if (stub_entry != NULL)
14868 {
14869 /* Munge up the value and addend so that we call the stub
14870 rather than the procedure directly. */
14871 asection *stub_sec = stub_entry->group->stub_sec;
14872
14873 if (stub_entry->stub_type == ppc_stub_save_res)
14874 relocation += (stub_sec->output_offset
14875 + stub_sec->output_section->vma
14876 + stub_sec->size - htab->sfpr->size
14877 - htab->sfpr->output_offset
14878 - htab->sfpr->output_section->vma);
14879 else
14880 relocation = (stub_entry->stub_offset
14881 + stub_sec->output_offset
14882 + stub_sec->output_section->vma);
14883 addend = 0;
14884 reloc_dest = DEST_STUB;
14885
14886 if ((stub_entry->stub_type == ppc_stub_plt_call
14887 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14888 && (ALWAYS_EMIT_R2SAVE
14889 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14890 && rel + 1 < relend
14891 && rel[1].r_offset == rel->r_offset + 4
14892 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14893 relocation += 4;
14894 }
14895
14896 if (insn != 0)
14897 {
14898 if (is_isa_v2)
14899 {
14900 /* Set 'a' bit. This is 0b00010 in BO field for branch
14901 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14902 for branch on CTR insns (BO == 1a00t or 1a01t). */
14903 if ((insn & (0x14 << 21)) == (0x04 << 21))
14904 insn |= 0x02 << 21;
14905 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14906 insn |= 0x08 << 21;
14907 else
14908 break;
14909 }
14910 else
14911 {
14912 /* Invert 'y' bit if not the default. */
14913 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14914 insn ^= 0x01 << 21;
14915 }
14916
14917 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14918 }
14919
14920 /* NOP out calls to undefined weak functions.
14921 We can thus call a weak function without first
14922 checking whether the function is defined. */
14923 else if (h != NULL
14924 && h->elf.root.type == bfd_link_hash_undefweak
14925 && h->elf.dynindx == -1
14926 && r_type == R_PPC64_REL24
14927 && relocation == 0
14928 && addend == 0)
14929 {
14930 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14931 goto copy_reloc;
14932 }
14933 break;
14934 }
14935
14936 /* Set `addend'. */
14937 tls_type = 0;
14938 save_unresolved_reloc = unresolved_reloc;
14939 switch (r_type)
14940 {
14941 default:
14942 /* xgettext:c-format */
14943 _bfd_error_handler (_("%pB: %s unsupported"),
14944 input_bfd, ppc64_elf_howto_table[r_type]->name);
14945
14946 bfd_set_error (bfd_error_bad_value);
14947 ret = FALSE;
14948 goto copy_reloc;
14949
14950 case R_PPC64_NONE:
14951 case R_PPC64_TLS:
14952 case R_PPC64_TLSGD:
14953 case R_PPC64_TLSLD:
14954 case R_PPC64_TOCSAVE:
14955 case R_PPC64_GNU_VTINHERIT:
14956 case R_PPC64_GNU_VTENTRY:
14957 case R_PPC64_ENTRY:
14958 goto copy_reloc;
14959
14960 /* GOT16 relocations. Like an ADDR16 using the symbol's
14961 address in the GOT as relocation value instead of the
14962 symbol's value itself. Also, create a GOT entry for the
14963 symbol and put the symbol value there. */
14964 case R_PPC64_GOT_TLSGD16:
14965 case R_PPC64_GOT_TLSGD16_LO:
14966 case R_PPC64_GOT_TLSGD16_HI:
14967 case R_PPC64_GOT_TLSGD16_HA:
14968 tls_type = TLS_TLS | TLS_GD;
14969 goto dogot;
14970
14971 case R_PPC64_GOT_TLSLD16:
14972 case R_PPC64_GOT_TLSLD16_LO:
14973 case R_PPC64_GOT_TLSLD16_HI:
14974 case R_PPC64_GOT_TLSLD16_HA:
14975 tls_type = TLS_TLS | TLS_LD;
14976 goto dogot;
14977
14978 case R_PPC64_GOT_TPREL16_DS:
14979 case R_PPC64_GOT_TPREL16_LO_DS:
14980 case R_PPC64_GOT_TPREL16_HI:
14981 case R_PPC64_GOT_TPREL16_HA:
14982 tls_type = TLS_TLS | TLS_TPREL;
14983 goto dogot;
14984
14985 case R_PPC64_GOT_DTPREL16_DS:
14986 case R_PPC64_GOT_DTPREL16_LO_DS:
14987 case R_PPC64_GOT_DTPREL16_HI:
14988 case R_PPC64_GOT_DTPREL16_HA:
14989 tls_type = TLS_TLS | TLS_DTPREL;
14990 goto dogot;
14991
14992 case R_PPC64_GOT16:
14993 case R_PPC64_GOT16_LO:
14994 case R_PPC64_GOT16_HI:
14995 case R_PPC64_GOT16_HA:
14996 case R_PPC64_GOT16_DS:
14997 case R_PPC64_GOT16_LO_DS:
14998 dogot:
14999 {
15000 /* Relocation is to the entry for this symbol in the global
15001 offset table. */
15002 asection *got;
15003 bfd_vma *offp;
15004 bfd_vma off;
15005 unsigned long indx = 0;
15006 struct got_entry *ent;
15007
15008 if (tls_type == (TLS_TLS | TLS_LD)
15009 && (h == NULL
15010 || !h->elf.def_dynamic))
15011 ent = ppc64_tlsld_got (input_bfd);
15012 else
15013 {
15014 if (h != NULL)
15015 {
15016 if (!htab->elf.dynamic_sections_created
15017 || h->elf.dynindx == -1
15018 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15019 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15020 /* This is actually a static link, or it is a
15021 -Bsymbolic link and the symbol is defined
15022 locally, or the symbol was forced to be local
15023 because of a version file. */
15024 ;
15025 else
15026 {
15027 indx = h->elf.dynindx;
15028 unresolved_reloc = FALSE;
15029 }
15030 ent = h->elf.got.glist;
15031 }
15032 else
15033 {
15034 if (local_got_ents == NULL)
15035 abort ();
15036 ent = local_got_ents[r_symndx];
15037 }
15038
15039 for (; ent != NULL; ent = ent->next)
15040 if (ent->addend == orig_rel.r_addend
15041 && ent->owner == input_bfd
15042 && ent->tls_type == tls_type)
15043 break;
15044 }
15045
15046 if (ent == NULL)
15047 abort ();
15048 if (ent->is_indirect)
15049 ent = ent->got.ent;
15050 offp = &ent->got.offset;
15051 got = ppc64_elf_tdata (ent->owner)->got;
15052 if (got == NULL)
15053 abort ();
15054
15055 /* The offset must always be a multiple of 8. We use the
15056 least significant bit to record whether we have already
15057 processed this entry. */
15058 off = *offp;
15059 if ((off & 1) != 0)
15060 off &= ~1;
15061 else
15062 {
15063 /* Generate relocs for the dynamic linker, except in
15064 the case of TLSLD where we'll use one entry per
15065 module. */
15066 asection *relgot;
15067 bfd_boolean ifunc;
15068
15069 *offp = off | 1;
15070 relgot = NULL;
15071 ifunc = (h != NULL
15072 ? h->elf.type == STT_GNU_IFUNC
15073 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15074 if (ifunc)
15075 {
15076 relgot = htab->elf.irelplt;
15077 if (indx == 0)
15078 htab->local_ifunc_resolver = 1;
15079 else if (is_static_defined (&h->elf))
15080 htab->maybe_local_ifunc_resolver = 1;
15081 }
15082 else if (indx != 0
15083 || (bfd_link_pic (info)
15084 && (h == NULL
15085 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15086 || (tls_type == (TLS_TLS | TLS_LD)
15087 && !h->elf.def_dynamic))
15088 && !(tls_type == (TLS_TLS | TLS_TPREL)
15089 && bfd_link_executable (info)
15090 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15091 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15092 if (relgot != NULL)
15093 {
15094 outrel.r_offset = (got->output_section->vma
15095 + got->output_offset
15096 + off);
15097 outrel.r_addend = addend;
15098 if (tls_type & (TLS_LD | TLS_GD))
15099 {
15100 outrel.r_addend = 0;
15101 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15102 if (tls_type == (TLS_TLS | TLS_GD))
15103 {
15104 loc = relgot->contents;
15105 loc += (relgot->reloc_count++
15106 * sizeof (Elf64_External_Rela));
15107 bfd_elf64_swap_reloca_out (output_bfd,
15108 &outrel, loc);
15109 outrel.r_offset += 8;
15110 outrel.r_addend = addend;
15111 outrel.r_info
15112 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15113 }
15114 }
15115 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15116 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15117 else if (tls_type == (TLS_TLS | TLS_TPREL))
15118 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15119 else if (indx != 0)
15120 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15121 else
15122 {
15123 if (ifunc)
15124 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15125 else
15126 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15127
15128 /* Write the .got section contents for the sake
15129 of prelink. */
15130 loc = got->contents + off;
15131 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15132 loc);
15133 }
15134
15135 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15136 {
15137 outrel.r_addend += relocation;
15138 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15139 {
15140 if (htab->elf.tls_sec == NULL)
15141 outrel.r_addend = 0;
15142 else
15143 outrel.r_addend -= htab->elf.tls_sec->vma;
15144 }
15145 }
15146 loc = relgot->contents;
15147 loc += (relgot->reloc_count++
15148 * sizeof (Elf64_External_Rela));
15149 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15150 }
15151
15152 /* Init the .got section contents here if we're not
15153 emitting a reloc. */
15154 else
15155 {
15156 relocation += addend;
15157 if (tls_type != 0)
15158 {
15159 if (htab->elf.tls_sec == NULL)
15160 relocation = 0;
15161 else
15162 {
15163 if (tls_type & TLS_LD)
15164 relocation = 0;
15165 else
15166 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15167 if (tls_type & TLS_TPREL)
15168 relocation += DTP_OFFSET - TP_OFFSET;
15169 }
15170
15171 if (tls_type & (TLS_GD | TLS_LD))
15172 {
15173 bfd_put_64 (output_bfd, relocation,
15174 got->contents + off + 8);
15175 relocation = 1;
15176 }
15177 }
15178 bfd_put_64 (output_bfd, relocation,
15179 got->contents + off);
15180 }
15181 }
15182
15183 if (off >= (bfd_vma) -2)
15184 abort ();
15185
15186 relocation = got->output_section->vma + got->output_offset + off;
15187 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15188 }
15189 break;
15190
15191 case R_PPC64_PLT16_HA:
15192 case R_PPC64_PLT16_HI:
15193 case R_PPC64_PLT16_LO:
15194 case R_PPC64_PLT16_LO_DS:
15195 case R_PPC64_PLT32:
15196 case R_PPC64_PLT64:
15197 case R_PPC64_PLTSEQ:
15198 case R_PPC64_PLTCALL:
15199 /* Relocation is to the entry for this symbol in the
15200 procedure linkage table. */
15201 unresolved_reloc = TRUE;
15202 {
15203 struct plt_entry **plt_list = NULL;
15204 if (h != NULL)
15205 plt_list = &h->elf.plt.plist;
15206 else if (local_got_ents != NULL)
15207 {
15208 struct plt_entry **local_plt = (struct plt_entry **)
15209 (local_got_ents + symtab_hdr->sh_info);
15210 plt_list = local_plt + r_symndx;
15211 }
15212 if (plt_list)
15213 {
15214 struct plt_entry *ent;
15215
15216 for (ent = *plt_list; ent != NULL; ent = ent->next)
15217 if (ent->plt.offset != (bfd_vma) -1
15218 && ent->addend == orig_rel.r_addend)
15219 {
15220 asection *plt;
15221 bfd_vma got;
15222
15223 plt = htab->elf.splt;
15224 if (!htab->elf.dynamic_sections_created
15225 || h == NULL
15226 || h->elf.dynindx == -1)
15227 {
15228 if (h != NULL
15229 ? h->elf.type == STT_GNU_IFUNC
15230 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15231 plt = htab->elf.iplt;
15232 else
15233 plt = htab->pltlocal;
15234 }
15235 relocation = (plt->output_section->vma
15236 + plt->output_offset
15237 + ent->plt.offset);
15238 if (r_type == R_PPC64_PLT16_HA
15239 || r_type ==R_PPC64_PLT16_HI
15240 || r_type ==R_PPC64_PLT16_LO
15241 || r_type ==R_PPC64_PLT16_LO_DS)
15242 {
15243 got = (elf_gp (output_bfd)
15244 + htab->sec_info[input_section->id].toc_off);
15245 relocation -= got;
15246 }
15247 addend = 0;
15248 unresolved_reloc = FALSE;
15249 break;
15250 }
15251 }
15252 }
15253 break;
15254
15255 case R_PPC64_TOC:
15256 /* Relocation value is TOC base. */
15257 relocation = TOCstart;
15258 if (r_symndx == STN_UNDEF)
15259 relocation += htab->sec_info[input_section->id].toc_off;
15260 else if (unresolved_reloc)
15261 ;
15262 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15263 relocation += htab->sec_info[sec->id].toc_off;
15264 else
15265 unresolved_reloc = TRUE;
15266 goto dodyn;
15267
15268 /* TOC16 relocs. We want the offset relative to the TOC base,
15269 which is the address of the start of the TOC plus 0x8000.
15270 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15271 in this order. */
15272 case R_PPC64_TOC16:
15273 case R_PPC64_TOC16_LO:
15274 case R_PPC64_TOC16_HI:
15275 case R_PPC64_TOC16_DS:
15276 case R_PPC64_TOC16_LO_DS:
15277 case R_PPC64_TOC16_HA:
15278 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15279 break;
15280
15281 /* Relocate against the beginning of the section. */
15282 case R_PPC64_SECTOFF:
15283 case R_PPC64_SECTOFF_LO:
15284 case R_PPC64_SECTOFF_HI:
15285 case R_PPC64_SECTOFF_DS:
15286 case R_PPC64_SECTOFF_LO_DS:
15287 case R_PPC64_SECTOFF_HA:
15288 if (sec != NULL)
15289 addend -= sec->output_section->vma;
15290 break;
15291
15292 case R_PPC64_REL16:
15293 case R_PPC64_REL16_LO:
15294 case R_PPC64_REL16_HI:
15295 case R_PPC64_REL16_HA:
15296 case R_PPC64_REL16DX_HA:
15297 break;
15298
15299 case R_PPC64_REL14:
15300 case R_PPC64_REL14_BRNTAKEN:
15301 case R_PPC64_REL14_BRTAKEN:
15302 case R_PPC64_REL24:
15303 break;
15304
15305 case R_PPC64_TPREL16:
15306 case R_PPC64_TPREL16_LO:
15307 case R_PPC64_TPREL16_HI:
15308 case R_PPC64_TPREL16_HA:
15309 case R_PPC64_TPREL16_DS:
15310 case R_PPC64_TPREL16_LO_DS:
15311 case R_PPC64_TPREL16_HIGH:
15312 case R_PPC64_TPREL16_HIGHA:
15313 case R_PPC64_TPREL16_HIGHER:
15314 case R_PPC64_TPREL16_HIGHERA:
15315 case R_PPC64_TPREL16_HIGHEST:
15316 case R_PPC64_TPREL16_HIGHESTA:
15317 if (h != NULL
15318 && h->elf.root.type == bfd_link_hash_undefweak
15319 && h->elf.dynindx == -1)
15320 {
15321 /* Make this relocation against an undefined weak symbol
15322 resolve to zero. This is really just a tweak, since
15323 code using weak externs ought to check that they are
15324 defined before using them. */
15325 bfd_byte *p = contents + rel->r_offset - d_offset;
15326
15327 insn = bfd_get_32 (input_bfd, p);
15328 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15329 if (insn != 0)
15330 bfd_put_32 (input_bfd, insn, p);
15331 break;
15332 }
15333 if (htab->elf.tls_sec != NULL)
15334 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15335 /* The TPREL16 relocs shouldn't really be used in shared
15336 libs or with non-local symbols as that will result in
15337 DT_TEXTREL being set, but support them anyway. */
15338 goto dodyn;
15339
15340 case R_PPC64_DTPREL16:
15341 case R_PPC64_DTPREL16_LO:
15342 case R_PPC64_DTPREL16_HI:
15343 case R_PPC64_DTPREL16_HA:
15344 case R_PPC64_DTPREL16_DS:
15345 case R_PPC64_DTPREL16_LO_DS:
15346 case R_PPC64_DTPREL16_HIGH:
15347 case R_PPC64_DTPREL16_HIGHA:
15348 case R_PPC64_DTPREL16_HIGHER:
15349 case R_PPC64_DTPREL16_HIGHERA:
15350 case R_PPC64_DTPREL16_HIGHEST:
15351 case R_PPC64_DTPREL16_HIGHESTA:
15352 if (htab->elf.tls_sec != NULL)
15353 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15354 break;
15355
15356 case R_PPC64_ADDR64_LOCAL:
15357 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15358 ? h->elf.other
15359 : sym->st_other);
15360 break;
15361
15362 case R_PPC64_DTPMOD64:
15363 relocation = 1;
15364 addend = 0;
15365 goto dodyn;
15366
15367 case R_PPC64_TPREL64:
15368 if (htab->elf.tls_sec != NULL)
15369 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15370 goto dodyn;
15371
15372 case R_PPC64_DTPREL64:
15373 if (htab->elf.tls_sec != NULL)
15374 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15375 /* Fall through. */
15376
15377 /* Relocations that may need to be propagated if this is a
15378 dynamic object. */
15379 case R_PPC64_REL30:
15380 case R_PPC64_REL32:
15381 case R_PPC64_REL64:
15382 case R_PPC64_ADDR14:
15383 case R_PPC64_ADDR14_BRNTAKEN:
15384 case R_PPC64_ADDR14_BRTAKEN:
15385 case R_PPC64_ADDR16:
15386 case R_PPC64_ADDR16_DS:
15387 case R_PPC64_ADDR16_HA:
15388 case R_PPC64_ADDR16_HI:
15389 case R_PPC64_ADDR16_HIGH:
15390 case R_PPC64_ADDR16_HIGHA:
15391 case R_PPC64_ADDR16_HIGHER:
15392 case R_PPC64_ADDR16_HIGHERA:
15393 case R_PPC64_ADDR16_HIGHEST:
15394 case R_PPC64_ADDR16_HIGHESTA:
15395 case R_PPC64_ADDR16_LO:
15396 case R_PPC64_ADDR16_LO_DS:
15397 case R_PPC64_ADDR24:
15398 case R_PPC64_ADDR32:
15399 case R_PPC64_ADDR64:
15400 case R_PPC64_UADDR16:
15401 case R_PPC64_UADDR32:
15402 case R_PPC64_UADDR64:
15403 dodyn:
15404 if ((input_section->flags & SEC_ALLOC) == 0)
15405 break;
15406
15407 if (NO_OPD_RELOCS && is_opd)
15408 break;
15409
15410 if (bfd_link_pic (info)
15411 ? ((h == NULL
15412 || h->dyn_relocs != NULL)
15413 && ((h != NULL && pc_dynrelocs (h))
15414 || must_be_dyn_reloc (info, r_type)))
15415 : (h != NULL
15416 ? h->dyn_relocs != NULL
15417 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15418 {
15419 bfd_boolean skip, relocate;
15420 asection *sreloc;
15421 bfd_vma out_off;
15422 long indx = 0;
15423
15424 /* When generating a dynamic object, these relocations
15425 are copied into the output file to be resolved at run
15426 time. */
15427
15428 skip = FALSE;
15429 relocate = FALSE;
15430
15431 out_off = _bfd_elf_section_offset (output_bfd, info,
15432 input_section, rel->r_offset);
15433 if (out_off == (bfd_vma) -1)
15434 skip = TRUE;
15435 else if (out_off == (bfd_vma) -2)
15436 skip = TRUE, relocate = TRUE;
15437 out_off += (input_section->output_section->vma
15438 + input_section->output_offset);
15439 outrel.r_offset = out_off;
15440 outrel.r_addend = rel->r_addend;
15441
15442 /* Optimize unaligned reloc use. */
15443 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15444 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15445 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15446 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15447 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15448 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15449 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15450 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15451 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15452
15453 if (skip)
15454 memset (&outrel, 0, sizeof outrel);
15455 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15456 && !is_opd
15457 && r_type != R_PPC64_TOC)
15458 {
15459 indx = h->elf.dynindx;
15460 BFD_ASSERT (indx != -1);
15461 outrel.r_info = ELF64_R_INFO (indx, r_type);
15462 }
15463 else
15464 {
15465 /* This symbol is local, or marked to become local,
15466 or this is an opd section reloc which must point
15467 at a local function. */
15468 outrel.r_addend += relocation;
15469 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15470 {
15471 if (is_opd && h != NULL)
15472 {
15473 /* Lie about opd entries. This case occurs
15474 when building shared libraries and we
15475 reference a function in another shared
15476 lib. The same thing happens for a weak
15477 definition in an application that's
15478 overridden by a strong definition in a
15479 shared lib. (I believe this is a generic
15480 bug in binutils handling of weak syms.)
15481 In these cases we won't use the opd
15482 entry in this lib. */
15483 unresolved_reloc = FALSE;
15484 }
15485 if (!is_opd
15486 && r_type == R_PPC64_ADDR64
15487 && (h != NULL
15488 ? h->elf.type == STT_GNU_IFUNC
15489 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15490 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15491 else
15492 {
15493 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15494
15495 /* We need to relocate .opd contents for ld.so.
15496 Prelink also wants simple and consistent rules
15497 for relocs. This make all RELATIVE relocs have
15498 *r_offset equal to r_addend. */
15499 relocate = TRUE;
15500 }
15501 }
15502 else
15503 {
15504 if (h != NULL
15505 ? h->elf.type == STT_GNU_IFUNC
15506 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15507 {
15508 info->callbacks->einfo
15509 /* xgettext:c-format */
15510 (_("%H: %s for indirect "
15511 "function `%pT' unsupported\n"),
15512 input_bfd, input_section, rel->r_offset,
15513 ppc64_elf_howto_table[r_type]->name,
15514 sym_name);
15515 ret = FALSE;
15516 }
15517 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15518 ;
15519 else if (sec == NULL || sec->owner == NULL)
15520 {
15521 bfd_set_error (bfd_error_bad_value);
15522 return FALSE;
15523 }
15524 else
15525 {
15526 asection *osec;
15527
15528 osec = sec->output_section;
15529 indx = elf_section_data (osec)->dynindx;
15530
15531 if (indx == 0)
15532 {
15533 if ((osec->flags & SEC_READONLY) == 0
15534 && htab->elf.data_index_section != NULL)
15535 osec = htab->elf.data_index_section;
15536 else
15537 osec = htab->elf.text_index_section;
15538 indx = elf_section_data (osec)->dynindx;
15539 }
15540 BFD_ASSERT (indx != 0);
15541
15542 /* We are turning this relocation into one
15543 against a section symbol, so subtract out
15544 the output section's address but not the
15545 offset of the input section in the output
15546 section. */
15547 outrel.r_addend -= osec->vma;
15548 }
15549
15550 outrel.r_info = ELF64_R_INFO (indx, r_type);
15551 }
15552 }
15553
15554 sreloc = elf_section_data (input_section)->sreloc;
15555 if (h != NULL
15556 ? h->elf.type == STT_GNU_IFUNC
15557 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15558 {
15559 sreloc = htab->elf.irelplt;
15560 if (indx == 0)
15561 htab->local_ifunc_resolver = 1;
15562 else if (is_static_defined (&h->elf))
15563 htab->maybe_local_ifunc_resolver = 1;
15564 }
15565 if (sreloc == NULL)
15566 abort ();
15567
15568 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15569 >= sreloc->size)
15570 abort ();
15571 loc = sreloc->contents;
15572 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15573 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15574
15575 /* If this reloc is against an external symbol, it will
15576 be computed at runtime, so there's no need to do
15577 anything now. However, for the sake of prelink ensure
15578 that the section contents are a known value. */
15579 if (! relocate)
15580 {
15581 unresolved_reloc = FALSE;
15582 /* The value chosen here is quite arbitrary as ld.so
15583 ignores section contents except for the special
15584 case of .opd where the contents might be accessed
15585 before relocation. Choose zero, as that won't
15586 cause reloc overflow. */
15587 relocation = 0;
15588 addend = 0;
15589 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15590 to improve backward compatibility with older
15591 versions of ld. */
15592 if (r_type == R_PPC64_ADDR64)
15593 addend = outrel.r_addend;
15594 /* Adjust pc_relative relocs to have zero in *r_offset. */
15595 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15596 addend = outrel.r_offset;
15597 }
15598 }
15599 break;
15600
15601 case R_PPC64_COPY:
15602 case R_PPC64_GLOB_DAT:
15603 case R_PPC64_JMP_SLOT:
15604 case R_PPC64_JMP_IREL:
15605 case R_PPC64_RELATIVE:
15606 /* We shouldn't ever see these dynamic relocs in relocatable
15607 files. */
15608 /* Fall through. */
15609
15610 case R_PPC64_PLTGOT16:
15611 case R_PPC64_PLTGOT16_DS:
15612 case R_PPC64_PLTGOT16_HA:
15613 case R_PPC64_PLTGOT16_HI:
15614 case R_PPC64_PLTGOT16_LO:
15615 case R_PPC64_PLTGOT16_LO_DS:
15616 case R_PPC64_PLTREL32:
15617 case R_PPC64_PLTREL64:
15618 /* These ones haven't been implemented yet. */
15619
15620 info->callbacks->einfo
15621 /* xgettext:c-format */
15622 (_("%P: %pB: %s is not supported for `%pT'\n"),
15623 input_bfd,
15624 ppc64_elf_howto_table[r_type]->name, sym_name);
15625
15626 bfd_set_error (bfd_error_invalid_operation);
15627 ret = FALSE;
15628 goto copy_reloc;
15629 }
15630
15631 /* Multi-instruction sequences that access the TOC can be
15632 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15633 to nop; addi rb,r2,x; */
15634 switch (r_type)
15635 {
15636 default:
15637 break;
15638
15639 case R_PPC64_GOT_TLSLD16_HI:
15640 case R_PPC64_GOT_TLSGD16_HI:
15641 case R_PPC64_GOT_TPREL16_HI:
15642 case R_PPC64_GOT_DTPREL16_HI:
15643 case R_PPC64_GOT16_HI:
15644 case R_PPC64_TOC16_HI:
15645 /* These relocs would only be useful if building up an
15646 offset to later add to r2, perhaps in an indexed
15647 addressing mode instruction. Don't try to optimize.
15648 Unfortunately, the possibility of someone building up an
15649 offset like this or even with the HA relocs, means that
15650 we need to check the high insn when optimizing the low
15651 insn. */
15652 break;
15653
15654 case R_PPC64_PLTCALL:
15655 if (unresolved_reloc)
15656 {
15657 /* No plt entry. Make this into a direct call. */
15658 bfd_byte *p = contents + rel->r_offset;
15659 insn = bfd_get_32 (input_bfd, p);
15660 insn &= 1;
15661 bfd_put_32 (input_bfd, B_DOT | insn, p);
15662 bfd_put_32 (input_bfd, NOP, p + 4);
15663 unresolved_reloc = save_unresolved_reloc;
15664 r_type = R_PPC64_REL24;
15665 }
15666 break;
15667
15668 case R_PPC64_PLTSEQ:
15669 if (unresolved_reloc)
15670 {
15671 unresolved_reloc = FALSE;
15672 goto nop_it;
15673 }
15674 break;
15675
15676 case R_PPC64_PLT16_HA:
15677 if (unresolved_reloc)
15678 {
15679 unresolved_reloc = FALSE;
15680 goto nop_it;
15681 }
15682 /* Fall through. */
15683 case R_PPC64_GOT_TLSLD16_HA:
15684 case R_PPC64_GOT_TLSGD16_HA:
15685 case R_PPC64_GOT_TPREL16_HA:
15686 case R_PPC64_GOT_DTPREL16_HA:
15687 case R_PPC64_GOT16_HA:
15688 case R_PPC64_TOC16_HA:
15689 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15690 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15691 {
15692 bfd_byte *p;
15693 nop_it:
15694 p = contents + (rel->r_offset & ~3);
15695 bfd_put_32 (input_bfd, NOP, p);
15696 goto copy_reloc;
15697 }
15698 break;
15699
15700 case R_PPC64_PLT16_LO:
15701 case R_PPC64_PLT16_LO_DS:
15702 if (unresolved_reloc)
15703 {
15704 unresolved_reloc = FALSE;
15705 goto nop_it;
15706 }
15707 /* Fall through. */
15708 case R_PPC64_GOT_TLSLD16_LO:
15709 case R_PPC64_GOT_TLSGD16_LO:
15710 case R_PPC64_GOT_TPREL16_LO_DS:
15711 case R_PPC64_GOT_DTPREL16_LO_DS:
15712 case R_PPC64_GOT16_LO:
15713 case R_PPC64_GOT16_LO_DS:
15714 case R_PPC64_TOC16_LO:
15715 case R_PPC64_TOC16_LO_DS:
15716 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15717 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15718 {
15719 bfd_byte *p = contents + (rel->r_offset & ~3);
15720 insn = bfd_get_32 (input_bfd, p);
15721 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15722 {
15723 /* Transform addic to addi when we change reg. */
15724 insn &= ~((0x3f << 26) | (0x1f << 16));
15725 insn |= (14u << 26) | (2 << 16);
15726 }
15727 else
15728 {
15729 insn &= ~(0x1f << 16);
15730 insn |= 2 << 16;
15731 }
15732 bfd_put_32 (input_bfd, insn, p);
15733 }
15734 break;
15735
15736 case R_PPC64_TPREL16_HA:
15737 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15738 {
15739 bfd_byte *p = contents + (rel->r_offset & ~3);
15740 insn = bfd_get_32 (input_bfd, p);
15741 if ((insn & ((0x3f << 26) | 0x1f << 16))
15742 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15743 /* xgettext:c-format */
15744 info->callbacks->minfo
15745 (_("%H: warning: %s unexpected insn %#x.\n"),
15746 input_bfd, input_section, rel->r_offset,
15747 ppc64_elf_howto_table[r_type]->name, insn);
15748 else
15749 {
15750 bfd_put_32 (input_bfd, NOP, p);
15751 goto copy_reloc;
15752 }
15753 }
15754 break;
15755
15756 case R_PPC64_TPREL16_LO:
15757 case R_PPC64_TPREL16_LO_DS:
15758 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15759 {
15760 bfd_byte *p = contents + (rel->r_offset & ~3);
15761 insn = bfd_get_32 (input_bfd, p);
15762 insn &= ~(0x1f << 16);
15763 insn |= 13 << 16;
15764 bfd_put_32 (input_bfd, insn, p);
15765 }
15766 break;
15767 }
15768
15769 /* Do any further special processing. */
15770 switch (r_type)
15771 {
15772 default:
15773 break;
15774
15775 case R_PPC64_REL16_HA:
15776 case R_PPC64_REL16DX_HA:
15777 case R_PPC64_ADDR16_HA:
15778 case R_PPC64_ADDR16_HIGHA:
15779 case R_PPC64_ADDR16_HIGHERA:
15780 case R_PPC64_ADDR16_HIGHESTA:
15781 case R_PPC64_TOC16_HA:
15782 case R_PPC64_SECTOFF_HA:
15783 case R_PPC64_TPREL16_HA:
15784 case R_PPC64_TPREL16_HIGHA:
15785 case R_PPC64_TPREL16_HIGHERA:
15786 case R_PPC64_TPREL16_HIGHESTA:
15787 case R_PPC64_DTPREL16_HA:
15788 case R_PPC64_DTPREL16_HIGHA:
15789 case R_PPC64_DTPREL16_HIGHERA:
15790 case R_PPC64_DTPREL16_HIGHESTA:
15791 /* It's just possible that this symbol is a weak symbol
15792 that's not actually defined anywhere. In that case,
15793 'sec' would be NULL, and we should leave the symbol
15794 alone (it will be set to zero elsewhere in the link). */
15795 if (sec == NULL)
15796 break;
15797 /* Fall through. */
15798
15799 case R_PPC64_GOT16_HA:
15800 case R_PPC64_PLTGOT16_HA:
15801 case R_PPC64_PLT16_HA:
15802 case R_PPC64_GOT_TLSGD16_HA:
15803 case R_PPC64_GOT_TLSLD16_HA:
15804 case R_PPC64_GOT_TPREL16_HA:
15805 case R_PPC64_GOT_DTPREL16_HA:
15806 /* Add 0x10000 if sign bit in 0:15 is set.
15807 Bits 0:15 are not used. */
15808 addend += 0x8000;
15809 break;
15810
15811 case R_PPC64_ADDR16_DS:
15812 case R_PPC64_ADDR16_LO_DS:
15813 case R_PPC64_GOT16_DS:
15814 case R_PPC64_GOT16_LO_DS:
15815 case R_PPC64_PLT16_LO_DS:
15816 case R_PPC64_SECTOFF_DS:
15817 case R_PPC64_SECTOFF_LO_DS:
15818 case R_PPC64_TOC16_DS:
15819 case R_PPC64_TOC16_LO_DS:
15820 case R_PPC64_PLTGOT16_DS:
15821 case R_PPC64_PLTGOT16_LO_DS:
15822 case R_PPC64_GOT_TPREL16_DS:
15823 case R_PPC64_GOT_TPREL16_LO_DS:
15824 case R_PPC64_GOT_DTPREL16_DS:
15825 case R_PPC64_GOT_DTPREL16_LO_DS:
15826 case R_PPC64_TPREL16_DS:
15827 case R_PPC64_TPREL16_LO_DS:
15828 case R_PPC64_DTPREL16_DS:
15829 case R_PPC64_DTPREL16_LO_DS:
15830 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15831 mask = 3;
15832 /* If this reloc is against an lq, lxv, or stxv insn, then
15833 the value must be a multiple of 16. This is somewhat of
15834 a hack, but the "correct" way to do this by defining _DQ
15835 forms of all the _DS relocs bloats all reloc switches in
15836 this file. It doesn't make much sense to use these
15837 relocs in data, so testing the insn should be safe. */
15838 if ((insn & (0x3f << 26)) == (56u << 26)
15839 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15840 mask = 15;
15841 relocation += addend;
15842 addend = insn & (mask ^ 3);
15843 if ((relocation & mask) != 0)
15844 {
15845 relocation ^= relocation & mask;
15846 info->callbacks->einfo
15847 /* xgettext:c-format */
15848 (_("%H: error: %s not a multiple of %u\n"),
15849 input_bfd, input_section, rel->r_offset,
15850 ppc64_elf_howto_table[r_type]->name,
15851 mask + 1);
15852 bfd_set_error (bfd_error_bad_value);
15853 ret = FALSE;
15854 goto copy_reloc;
15855 }
15856 break;
15857 }
15858
15859 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15860 because such sections are not SEC_ALLOC and thus ld.so will
15861 not process them. */
15862 howto = ppc64_elf_howto_table[(int) r_type];
15863 if (unresolved_reloc
15864 && !((input_section->flags & SEC_DEBUGGING) != 0
15865 && h->elf.def_dynamic)
15866 && _bfd_elf_section_offset (output_bfd, info, input_section,
15867 rel->r_offset) != (bfd_vma) -1)
15868 {
15869 info->callbacks->einfo
15870 /* xgettext:c-format */
15871 (_("%H: unresolvable %s against `%pT'\n"),
15872 input_bfd, input_section, rel->r_offset,
15873 howto->name,
15874 h->elf.root.root.string);
15875 ret = FALSE;
15876 }
15877
15878 /* 16-bit fields in insns mostly have signed values, but a
15879 few insns have 16-bit unsigned values. Really, we should
15880 have different reloc types. */
15881 if (howto->complain_on_overflow != complain_overflow_dont
15882 && howto->dst_mask == 0xffff
15883 && (input_section->flags & SEC_CODE) != 0)
15884 {
15885 enum complain_overflow complain = complain_overflow_signed;
15886
15887 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15888 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15889 complain = complain_overflow_bitfield;
15890 else if (howto->rightshift == 0
15891 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15892 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15893 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15894 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15895 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15896 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15897 complain = complain_overflow_unsigned;
15898 if (howto->complain_on_overflow != complain)
15899 {
15900 alt_howto = *howto;
15901 alt_howto.complain_on_overflow = complain;
15902 howto = &alt_howto;
15903 }
15904 }
15905
15906 if (r_type == R_PPC64_REL16DX_HA)
15907 {
15908 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15909 if (rel->r_offset + 4 > input_section->size)
15910 r = bfd_reloc_outofrange;
15911 else
15912 {
15913 relocation += addend;
15914 relocation -= (rel->r_offset
15915 + input_section->output_offset
15916 + input_section->output_section->vma);
15917 relocation = (bfd_signed_vma) relocation >> 16;
15918 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15919 insn &= ~0x1fffc1;
15920 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15921 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15922 r = bfd_reloc_ok;
15923 if (relocation + 0x8000 > 0xffff)
15924 r = bfd_reloc_overflow;
15925 }
15926 }
15927 else
15928 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15929 rel->r_offset, relocation, addend);
15930
15931 if (r != bfd_reloc_ok)
15932 {
15933 char *more_info = NULL;
15934 const char *reloc_name = howto->name;
15935
15936 if (reloc_dest != DEST_NORMAL)
15937 {
15938 more_info = bfd_malloc (strlen (reloc_name) + 8);
15939 if (more_info != NULL)
15940 {
15941 strcpy (more_info, reloc_name);
15942 strcat (more_info, (reloc_dest == DEST_OPD
15943 ? " (OPD)" : " (stub)"));
15944 reloc_name = more_info;
15945 }
15946 }
15947
15948 if (r == bfd_reloc_overflow)
15949 {
15950 /* On code like "if (foo) foo();" don't report overflow
15951 on a branch to zero when foo is undefined. */
15952 if (!warned
15953 && (reloc_dest == DEST_STUB
15954 || !(h != NULL
15955 && (h->elf.root.type == bfd_link_hash_undefweak
15956 || h->elf.root.type == bfd_link_hash_undefined)
15957 && is_branch_reloc (r_type))))
15958 info->callbacks->reloc_overflow (info, &h->elf.root,
15959 sym_name, reloc_name,
15960 orig_rel.r_addend,
15961 input_bfd, input_section,
15962 rel->r_offset);
15963 }
15964 else
15965 {
15966 info->callbacks->einfo
15967 /* xgettext:c-format */
15968 (_("%H: %s against `%pT': error %d\n"),
15969 input_bfd, input_section, rel->r_offset,
15970 reloc_name, sym_name, (int) r);
15971 ret = FALSE;
15972 }
15973 if (more_info != NULL)
15974 free (more_info);
15975 }
15976 copy_reloc:
15977 if (wrel != rel)
15978 *wrel = *rel;
15979 }
15980
15981 if (wrel != rel)
15982 {
15983 Elf_Internal_Shdr *rel_hdr;
15984 size_t deleted = rel - wrel;
15985
15986 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15987 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15988 if (rel_hdr->sh_size == 0)
15989 {
15990 /* It is too late to remove an empty reloc section. Leave
15991 one NONE reloc.
15992 ??? What is wrong with an empty section??? */
15993 rel_hdr->sh_size = rel_hdr->sh_entsize;
15994 deleted -= 1;
15995 }
15996 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15997 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15998 input_section->reloc_count -= deleted;
15999 }
16000
16001 /* If we're emitting relocations, then shortly after this function
16002 returns, reloc offsets and addends for this section will be
16003 adjusted. Worse, reloc symbol indices will be for the output
16004 file rather than the input. Save a copy of the relocs for
16005 opd_entry_value. */
16006 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16007 {
16008 bfd_size_type amt;
16009 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16010 rel = bfd_alloc (input_bfd, amt);
16011 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16012 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16013 if (rel == NULL)
16014 return FALSE;
16015 memcpy (rel, relocs, amt);
16016 }
16017 return ret;
16018 }
16019
16020 /* Adjust the value of any local symbols in opd sections. */
16021
16022 static int
16023 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16024 const char *name ATTRIBUTE_UNUSED,
16025 Elf_Internal_Sym *elfsym,
16026 asection *input_sec,
16027 struct elf_link_hash_entry *h)
16028 {
16029 struct _opd_sec_data *opd;
16030 long adjust;
16031 bfd_vma value;
16032
16033 if (h != NULL)
16034 return 1;
16035
16036 opd = get_opd_info (input_sec);
16037 if (opd == NULL || opd->adjust == NULL)
16038 return 1;
16039
16040 value = elfsym->st_value - input_sec->output_offset;
16041 if (!bfd_link_relocatable (info))
16042 value -= input_sec->output_section->vma;
16043
16044 adjust = opd->adjust[OPD_NDX (value)];
16045 if (adjust == -1)
16046 return 2;
16047
16048 elfsym->st_value += adjust;
16049 return 1;
16050 }
16051
16052 /* Finish up dynamic symbol handling. We set the contents of various
16053 dynamic sections here. */
16054
16055 static bfd_boolean
16056 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16057 struct bfd_link_info *info,
16058 struct elf_link_hash_entry *h,
16059 Elf_Internal_Sym *sym)
16060 {
16061 struct ppc_link_hash_table *htab;
16062 struct plt_entry *ent;
16063
16064 htab = ppc_hash_table (info);
16065 if (htab == NULL)
16066 return FALSE;
16067
16068 if (!htab->opd_abi && !h->def_regular)
16069 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16070 if (ent->plt.offset != (bfd_vma) -1)
16071 {
16072 /* Mark the symbol as undefined, rather than as
16073 defined in glink. Leave the value if there were
16074 any relocations where pointer equality matters
16075 (this is a clue for the dynamic linker, to make
16076 function pointer comparisons work between an
16077 application and shared library), otherwise set it
16078 to zero. */
16079 sym->st_shndx = SHN_UNDEF;
16080 if (!h->pointer_equality_needed)
16081 sym->st_value = 0;
16082 else if (!h->ref_regular_nonweak)
16083 {
16084 /* This breaks function pointer comparisons, but
16085 that is better than breaking tests for a NULL
16086 function pointer. */
16087 sym->st_value = 0;
16088 }
16089 break;
16090 }
16091
16092 if (h->needs_copy)
16093 {
16094 /* This symbol needs a copy reloc. Set it up. */
16095 Elf_Internal_Rela rela;
16096 asection *srel;
16097 bfd_byte *loc;
16098
16099 if (h->dynindx == -1
16100 || (h->root.type != bfd_link_hash_defined
16101 && h->root.type != bfd_link_hash_defweak)
16102 || htab->elf.srelbss == NULL
16103 || htab->elf.sreldynrelro == NULL)
16104 abort ();
16105
16106 rela.r_offset = (h->root.u.def.value
16107 + h->root.u.def.section->output_section->vma
16108 + h->root.u.def.section->output_offset);
16109 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16110 rela.r_addend = 0;
16111 if (h->root.u.def.section == htab->elf.sdynrelro)
16112 srel = htab->elf.sreldynrelro;
16113 else
16114 srel = htab->elf.srelbss;
16115 loc = srel->contents;
16116 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16117 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16118 }
16119
16120 return TRUE;
16121 }
16122
16123 /* Used to decide how to sort relocs in an optimal manner for the
16124 dynamic linker, before writing them out. */
16125
16126 static enum elf_reloc_type_class
16127 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16128 const asection *rel_sec,
16129 const Elf_Internal_Rela *rela)
16130 {
16131 enum elf_ppc64_reloc_type r_type;
16132 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16133
16134 if (rel_sec == htab->elf.irelplt)
16135 return reloc_class_ifunc;
16136
16137 r_type = ELF64_R_TYPE (rela->r_info);
16138 switch (r_type)
16139 {
16140 case R_PPC64_RELATIVE:
16141 return reloc_class_relative;
16142 case R_PPC64_JMP_SLOT:
16143 return reloc_class_plt;
16144 case R_PPC64_COPY:
16145 return reloc_class_copy;
16146 default:
16147 return reloc_class_normal;
16148 }
16149 }
16150
16151 /* Finish up the dynamic sections. */
16152
16153 static bfd_boolean
16154 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16155 struct bfd_link_info *info)
16156 {
16157 struct ppc_link_hash_table *htab;
16158 bfd *dynobj;
16159 asection *sdyn;
16160
16161 htab = ppc_hash_table (info);
16162 if (htab == NULL)
16163 return FALSE;
16164
16165 dynobj = htab->elf.dynobj;
16166 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16167
16168 if (htab->elf.dynamic_sections_created)
16169 {
16170 Elf64_External_Dyn *dyncon, *dynconend;
16171
16172 if (sdyn == NULL || htab->elf.sgot == NULL)
16173 abort ();
16174
16175 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16176 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16177 for (; dyncon < dynconend; dyncon++)
16178 {
16179 Elf_Internal_Dyn dyn;
16180 asection *s;
16181
16182 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16183
16184 switch (dyn.d_tag)
16185 {
16186 default:
16187 continue;
16188
16189 case DT_PPC64_GLINK:
16190 s = htab->glink;
16191 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16192 /* We stupidly defined DT_PPC64_GLINK to be the start
16193 of glink rather than the first entry point, which is
16194 what ld.so needs, and now have a bigger stub to
16195 support automatic multiple TOCs. */
16196 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16197 break;
16198
16199 case DT_PPC64_OPD:
16200 s = bfd_get_section_by_name (output_bfd, ".opd");
16201 if (s == NULL)
16202 continue;
16203 dyn.d_un.d_ptr = s->vma;
16204 break;
16205
16206 case DT_PPC64_OPT:
16207 if (htab->do_multi_toc && htab->multi_toc_needed)
16208 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16209 if (htab->has_plt_localentry0)
16210 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16211 break;
16212
16213 case DT_PPC64_OPDSZ:
16214 s = bfd_get_section_by_name (output_bfd, ".opd");
16215 if (s == NULL)
16216 continue;
16217 dyn.d_un.d_val = s->size;
16218 break;
16219
16220 case DT_PLTGOT:
16221 s = htab->elf.splt;
16222 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16223 break;
16224
16225 case DT_JMPREL:
16226 s = htab->elf.srelplt;
16227 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16228 break;
16229
16230 case DT_PLTRELSZ:
16231 dyn.d_un.d_val = htab->elf.srelplt->size;
16232 break;
16233
16234 case DT_TEXTREL:
16235 if (htab->local_ifunc_resolver)
16236 info->callbacks->einfo
16237 (_("%X%P: text relocations and GNU indirect "
16238 "functions will result in a segfault at runtime\n"));
16239 else if (htab->maybe_local_ifunc_resolver)
16240 info->callbacks->einfo
16241 (_("%P: warning: text relocations and GNU indirect "
16242 "functions may result in a segfault at runtime\n"));
16243 continue;
16244 }
16245
16246 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16247 }
16248 }
16249
16250 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16251 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16252 {
16253 /* Fill in the first entry in the global offset table.
16254 We use it to hold the link-time TOCbase. */
16255 bfd_put_64 (output_bfd,
16256 elf_gp (output_bfd) + TOC_BASE_OFF,
16257 htab->elf.sgot->contents);
16258
16259 /* Set .got entry size. */
16260 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16261 }
16262
16263 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16264 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16265 {
16266 /* Set .plt entry size. */
16267 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16268 = PLT_ENTRY_SIZE (htab);
16269 }
16270
16271 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16272 brlt ourselves if emitrelocations. */
16273 if (htab->brlt != NULL
16274 && htab->brlt->reloc_count != 0
16275 && !_bfd_elf_link_output_relocs (output_bfd,
16276 htab->brlt,
16277 elf_section_data (htab->brlt)->rela.hdr,
16278 elf_section_data (htab->brlt)->relocs,
16279 NULL))
16280 return FALSE;
16281
16282 if (htab->glink != NULL
16283 && htab->glink->reloc_count != 0
16284 && !_bfd_elf_link_output_relocs (output_bfd,
16285 htab->glink,
16286 elf_section_data (htab->glink)->rela.hdr,
16287 elf_section_data (htab->glink)->relocs,
16288 NULL))
16289 return FALSE;
16290
16291 if (htab->glink_eh_frame != NULL
16292 && htab->glink_eh_frame->size != 0)
16293 {
16294 bfd_vma val;
16295 bfd_byte *p;
16296 struct map_stub *group;
16297 size_t align = 4;
16298
16299 p = htab->glink_eh_frame->contents;
16300 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
16301
16302 for (group = htab->group; group != NULL; group = group->next)
16303 if (group->stub_sec != NULL)
16304 {
16305 /* Offset to stub section. */
16306 val = (group->stub_sec->output_section->vma
16307 + group->stub_sec->output_offset);
16308 val -= (htab->glink_eh_frame->output_section->vma
16309 + htab->glink_eh_frame->output_offset
16310 + (p + 8 - htab->glink_eh_frame->contents));
16311 if (val + 0x80000000 > 0xffffffff)
16312 {
16313 _bfd_error_handler
16314 (_("%s offset too large for .eh_frame sdata4 encoding"),
16315 group->stub_sec->name);
16316 return FALSE;
16317 }
16318 bfd_put_32 (dynobj, val, p + 8);
16319 p += stub_eh_frame_size (group, align);
16320 }
16321 if (htab->glink != NULL && htab->glink->size != 0)
16322 {
16323 /* Offset to .glink. */
16324 val = (htab->glink->output_section->vma
16325 + htab->glink->output_offset
16326 + 8);
16327 val -= (htab->glink_eh_frame->output_section->vma
16328 + htab->glink_eh_frame->output_offset
16329 + (p + 8 - htab->glink_eh_frame->contents));
16330 if (val + 0x80000000 > 0xffffffff)
16331 {
16332 _bfd_error_handler
16333 (_("%s offset too large for .eh_frame sdata4 encoding"),
16334 htab->glink->name);
16335 return FALSE;
16336 }
16337 bfd_put_32 (dynobj, val, p + 8);
16338 p += (24 + align - 1) & -align;
16339 }
16340
16341 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16342 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16343 htab->glink_eh_frame,
16344 htab->glink_eh_frame->contents))
16345 return FALSE;
16346 }
16347
16348 /* We need to handle writing out multiple GOT sections ourselves,
16349 since we didn't add them to DYNOBJ. We know dynobj is the first
16350 bfd. */
16351 while ((dynobj = dynobj->link.next) != NULL)
16352 {
16353 asection *s;
16354
16355 if (!is_ppc64_elf (dynobj))
16356 continue;
16357
16358 s = ppc64_elf_tdata (dynobj)->got;
16359 if (s != NULL
16360 && s->size != 0
16361 && s->output_section != bfd_abs_section_ptr
16362 && !bfd_set_section_contents (output_bfd, s->output_section,
16363 s->contents, s->output_offset,
16364 s->size))
16365 return FALSE;
16366 s = ppc64_elf_tdata (dynobj)->relgot;
16367 if (s != NULL
16368 && s->size != 0
16369 && s->output_section != bfd_abs_section_ptr
16370 && !bfd_set_section_contents (output_bfd, s->output_section,
16371 s->contents, s->output_offset,
16372 s->size))
16373 return FALSE;
16374 }
16375
16376 return TRUE;
16377 }
16378
16379 #include "elf64-target.h"
16380
16381 /* FreeBSD support */
16382
16383 #undef TARGET_LITTLE_SYM
16384 #undef TARGET_LITTLE_NAME
16385
16386 #undef TARGET_BIG_SYM
16387 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16388 #undef TARGET_BIG_NAME
16389 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16390
16391 #undef ELF_OSABI
16392 #define ELF_OSABI ELFOSABI_FREEBSD
16393
16394 #undef elf64_bed
16395 #define elf64_bed elf64_powerpc_fbsd_bed
16396
16397 #include "elf64-target.h"
This page took 0.367022 seconds and 5 git commands to generate.