GNU attribute output on errors
[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 DIAGNOSTIC_PUSH;
3045 /* GCC 8.1 warns about 80 equals destination size with
3046 -Wstringop-truncation:
3047 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3048 */
3049 #if GCC_VERSION == 8001
3050 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3051 #endif
3052 strncpy (data + 56, va_arg (ap, const char *), 80);
3053 DIAGNOSTIC_POP;
3054 va_end (ap);
3055 return elfcore_write_note (abfd, buf, bufsiz,
3056 "CORE", note_type, data, sizeof (data));
3057 }
3058
3059 case NT_PRSTATUS:
3060 {
3061 char data[504];
3062 va_list ap;
3063 long pid;
3064 int cursig;
3065 const void *greg;
3066
3067 va_start (ap, note_type);
3068 memset (data, 0, 112);
3069 pid = va_arg (ap, long);
3070 bfd_put_32 (abfd, pid, data + 32);
3071 cursig = va_arg (ap, int);
3072 bfd_put_16 (abfd, cursig, data + 12);
3073 greg = va_arg (ap, const void *);
3074 memcpy (data + 112, greg, 384);
3075 memset (data + 496, 0, 8);
3076 va_end (ap);
3077 return elfcore_write_note (abfd, buf, bufsiz,
3078 "CORE", note_type, data, sizeof (data));
3079 }
3080 }
3081 }
3082
3083 /* Add extra PPC sections. */
3084
3085 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3086 {
3087 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3088 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3089 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3090 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3091 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3092 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3093 { NULL, 0, 0, 0, 0 }
3094 };
3095
3096 enum _ppc64_sec_type {
3097 sec_normal = 0,
3098 sec_opd = 1,
3099 sec_toc = 2
3100 };
3101
3102 struct _ppc64_elf_section_data
3103 {
3104 struct bfd_elf_section_data elf;
3105
3106 union
3107 {
3108 /* An array with one entry for each opd function descriptor,
3109 and some spares since opd entries may be either 16 or 24 bytes. */
3110 #define OPD_NDX(OFF) ((OFF) >> 4)
3111 struct _opd_sec_data
3112 {
3113 /* Points to the function code section for local opd entries. */
3114 asection **func_sec;
3115
3116 /* After editing .opd, adjust references to opd local syms. */
3117 long *adjust;
3118 } opd;
3119
3120 /* An array for toc sections, indexed by offset/8. */
3121 struct _toc_sec_data
3122 {
3123 /* Specifies the relocation symbol index used at a given toc offset. */
3124 unsigned *symndx;
3125
3126 /* And the relocation addend. */
3127 bfd_vma *add;
3128 } toc;
3129 } u;
3130
3131 enum _ppc64_sec_type sec_type:2;
3132
3133 /* Flag set when small branches are detected. Used to
3134 select suitable defaults for the stub group size. */
3135 unsigned int has_14bit_branch:1;
3136
3137 /* Flag set when PLTCALL relocs are detected. */
3138 unsigned int has_pltcall:1;
3139 };
3140
3141 #define ppc64_elf_section_data(sec) \
3142 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3143
3144 static bfd_boolean
3145 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3146 {
3147 if (!sec->used_by_bfd)
3148 {
3149 struct _ppc64_elf_section_data *sdata;
3150 bfd_size_type amt = sizeof (*sdata);
3151
3152 sdata = bfd_zalloc (abfd, amt);
3153 if (sdata == NULL)
3154 return FALSE;
3155 sec->used_by_bfd = sdata;
3156 }
3157
3158 return _bfd_elf_new_section_hook (abfd, sec);
3159 }
3160
3161 static struct _opd_sec_data *
3162 get_opd_info (asection * sec)
3163 {
3164 if (sec != NULL
3165 && ppc64_elf_section_data (sec) != NULL
3166 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3167 return &ppc64_elf_section_data (sec)->u.opd;
3168 return NULL;
3169 }
3170 \f
3171 /* Parameters for the qsort hook. */
3172 static bfd_boolean synthetic_relocatable;
3173 static asection *synthetic_opd;
3174
3175 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3176
3177 static int
3178 compare_symbols (const void *ap, const void *bp)
3179 {
3180 const asymbol *a = * (const asymbol **) ap;
3181 const asymbol *b = * (const asymbol **) bp;
3182
3183 /* Section symbols first. */
3184 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3185 return -1;
3186 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3187 return 1;
3188
3189 /* then .opd symbols. */
3190 if (synthetic_opd != NULL)
3191 {
3192 if (strcmp (a->section->name, ".opd") == 0
3193 && strcmp (b->section->name, ".opd") != 0)
3194 return -1;
3195 if (strcmp (a->section->name, ".opd") != 0
3196 && strcmp (b->section->name, ".opd") == 0)
3197 return 1;
3198 }
3199
3200 /* then other code symbols. */
3201 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3202 == (SEC_CODE | SEC_ALLOC)
3203 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3204 != (SEC_CODE | SEC_ALLOC))
3205 return -1;
3206
3207 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3208 != (SEC_CODE | SEC_ALLOC)
3209 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3210 == (SEC_CODE | SEC_ALLOC))
3211 return 1;
3212
3213 if (synthetic_relocatable)
3214 {
3215 if (a->section->id < b->section->id)
3216 return -1;
3217
3218 if (a->section->id > b->section->id)
3219 return 1;
3220 }
3221
3222 if (a->value + a->section->vma < b->value + b->section->vma)
3223 return -1;
3224
3225 if (a->value + a->section->vma > b->value + b->section->vma)
3226 return 1;
3227
3228 /* For syms with the same value, prefer strong dynamic global function
3229 syms over other syms. */
3230 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3231 return -1;
3232
3233 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3234 return 1;
3235
3236 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3237 return -1;
3238
3239 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3240 return 1;
3241
3242 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3243 return -1;
3244
3245 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3246 return 1;
3247
3248 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3249 return -1;
3250
3251 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3252 return 1;
3253
3254 return a > b;
3255 }
3256
3257 /* Search SYMS for a symbol of the given VALUE. */
3258
3259 static asymbol *
3260 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3261 {
3262 long mid;
3263
3264 if (id == (unsigned) -1)
3265 {
3266 while (lo < hi)
3267 {
3268 mid = (lo + hi) >> 1;
3269 if (syms[mid]->value + syms[mid]->section->vma < value)
3270 lo = mid + 1;
3271 else if (syms[mid]->value + syms[mid]->section->vma > value)
3272 hi = mid;
3273 else
3274 return syms[mid];
3275 }
3276 }
3277 else
3278 {
3279 while (lo < hi)
3280 {
3281 mid = (lo + hi) >> 1;
3282 if (syms[mid]->section->id < id)
3283 lo = mid + 1;
3284 else if (syms[mid]->section->id > id)
3285 hi = mid;
3286 else if (syms[mid]->value < value)
3287 lo = mid + 1;
3288 else if (syms[mid]->value > value)
3289 hi = mid;
3290 else
3291 return syms[mid];
3292 }
3293 }
3294 return NULL;
3295 }
3296
3297 static bfd_boolean
3298 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3299 {
3300 bfd_vma vma = *(bfd_vma *) ptr;
3301 return ((section->flags & SEC_ALLOC) != 0
3302 && section->vma <= vma
3303 && vma < section->vma + section->size);
3304 }
3305
3306 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3307 entry syms. Also generate @plt symbols for the glink branch table.
3308 Returns count of synthetic symbols in RET or -1 on error. */
3309
3310 static long
3311 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3312 long static_count, asymbol **static_syms,
3313 long dyn_count, asymbol **dyn_syms,
3314 asymbol **ret)
3315 {
3316 asymbol *s;
3317 size_t i, j, count;
3318 char *names;
3319 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3320 asection *opd = NULL;
3321 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3322 asymbol **syms;
3323 int abi = abiversion (abfd);
3324
3325 *ret = NULL;
3326
3327 if (abi < 2)
3328 {
3329 opd = bfd_get_section_by_name (abfd, ".opd");
3330 if (opd == NULL && abi == 1)
3331 return 0;
3332 }
3333
3334 syms = NULL;
3335 codesecsym = 0;
3336 codesecsymend = 0;
3337 secsymend = 0;
3338 opdsymend = 0;
3339 symcount = 0;
3340 if (opd != NULL)
3341 {
3342 symcount = static_count;
3343 if (!relocatable)
3344 symcount += dyn_count;
3345 if (symcount == 0)
3346 return 0;
3347
3348 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3349 if (syms == NULL)
3350 return -1;
3351
3352 if (!relocatable && static_count != 0 && dyn_count != 0)
3353 {
3354 /* Use both symbol tables. */
3355 memcpy (syms, static_syms, static_count * sizeof (*syms));
3356 memcpy (syms + static_count, dyn_syms,
3357 (dyn_count + 1) * sizeof (*syms));
3358 }
3359 else if (!relocatable && static_count == 0)
3360 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3361 else
3362 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3363
3364 /* Trim uninteresting symbols. Interesting symbols are section,
3365 function, and notype symbols. */
3366 for (i = 0, j = 0; i < symcount; ++i)
3367 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3368 | BSF_RELC | BSF_SRELC)) == 0)
3369 syms[j++] = syms[i];
3370 symcount = j;
3371
3372 synthetic_relocatable = relocatable;
3373 synthetic_opd = opd;
3374 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3375
3376 if (!relocatable && symcount > 1)
3377 {
3378 /* Trim duplicate syms, since we may have merged the normal
3379 and dynamic symbols. Actually, we only care about syms
3380 that have different values, so trim any with the same
3381 value. Don't consider ifunc and ifunc resolver symbols
3382 duplicates however, because GDB wants to know whether a
3383 text symbol is an ifunc resolver. */
3384 for (i = 1, j = 1; i < symcount; ++i)
3385 {
3386 const asymbol *s0 = syms[i - 1];
3387 const asymbol *s1 = syms[i];
3388
3389 if ((s0->value + s0->section->vma
3390 != s1->value + s1->section->vma)
3391 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3392 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3393 syms[j++] = syms[i];
3394 }
3395 symcount = j;
3396 }
3397
3398 i = 0;
3399 /* Note that here and in compare_symbols we can't compare opd and
3400 sym->section directly. With separate debug info files, the
3401 symbols will be extracted from the debug file while abfd passed
3402 to this function is the real binary. */
3403 if (strcmp (syms[i]->section->name, ".opd") == 0)
3404 ++i;
3405 codesecsym = i;
3406
3407 for (; i < symcount; ++i)
3408 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3409 | SEC_THREAD_LOCAL))
3410 != (SEC_CODE | SEC_ALLOC))
3411 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3412 break;
3413 codesecsymend = i;
3414
3415 for (; i < symcount; ++i)
3416 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3417 break;
3418 secsymend = i;
3419
3420 for (; i < symcount; ++i)
3421 if (strcmp (syms[i]->section->name, ".opd") != 0)
3422 break;
3423 opdsymend = i;
3424
3425 for (; i < symcount; ++i)
3426 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3427 != (SEC_CODE | SEC_ALLOC))
3428 break;
3429 symcount = i;
3430 }
3431 count = 0;
3432
3433 if (relocatable)
3434 {
3435 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3436 arelent *r;
3437 size_t size;
3438 size_t relcount;
3439
3440 if (opdsymend == secsymend)
3441 goto done;
3442
3443 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3444 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3445 if (relcount == 0)
3446 goto done;
3447
3448 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3449 {
3450 count = -1;
3451 goto done;
3452 }
3453
3454 size = 0;
3455 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3456 {
3457 asymbol *sym;
3458
3459 while (r < opd->relocation + relcount
3460 && r->address < syms[i]->value + opd->vma)
3461 ++r;
3462
3463 if (r == opd->relocation + relcount)
3464 break;
3465
3466 if (r->address != syms[i]->value + opd->vma)
3467 continue;
3468
3469 if (r->howto->type != R_PPC64_ADDR64)
3470 continue;
3471
3472 sym = *r->sym_ptr_ptr;
3473 if (!sym_exists_at (syms, opdsymend, symcount,
3474 sym->section->id, sym->value + r->addend))
3475 {
3476 ++count;
3477 size += sizeof (asymbol);
3478 size += strlen (syms[i]->name) + 2;
3479 }
3480 }
3481
3482 if (size == 0)
3483 goto done;
3484 s = *ret = bfd_malloc (size);
3485 if (s == NULL)
3486 {
3487 count = -1;
3488 goto done;
3489 }
3490
3491 names = (char *) (s + count);
3492
3493 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3494 {
3495 asymbol *sym;
3496
3497 while (r < opd->relocation + relcount
3498 && r->address < syms[i]->value + opd->vma)
3499 ++r;
3500
3501 if (r == opd->relocation + relcount)
3502 break;
3503
3504 if (r->address != syms[i]->value + opd->vma)
3505 continue;
3506
3507 if (r->howto->type != R_PPC64_ADDR64)
3508 continue;
3509
3510 sym = *r->sym_ptr_ptr;
3511 if (!sym_exists_at (syms, opdsymend, symcount,
3512 sym->section->id, sym->value + r->addend))
3513 {
3514 size_t len;
3515
3516 *s = *syms[i];
3517 s->flags |= BSF_SYNTHETIC;
3518 s->section = sym->section;
3519 s->value = sym->value + r->addend;
3520 s->name = names;
3521 *names++ = '.';
3522 len = strlen (syms[i]->name);
3523 memcpy (names, syms[i]->name, len + 1);
3524 names += len + 1;
3525 /* Have udata.p point back to the original symbol this
3526 synthetic symbol was derived from. */
3527 s->udata.p = syms[i];
3528 s++;
3529 }
3530 }
3531 }
3532 else
3533 {
3534 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3535 bfd_byte *contents = NULL;
3536 size_t size;
3537 size_t plt_count = 0;
3538 bfd_vma glink_vma = 0, resolv_vma = 0;
3539 asection *dynamic, *glink = NULL, *relplt = NULL;
3540 arelent *p;
3541
3542 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3543 {
3544 free_contents_and_exit_err:
3545 count = -1;
3546 free_contents_and_exit:
3547 if (contents)
3548 free (contents);
3549 goto done;
3550 }
3551
3552 size = 0;
3553 for (i = secsymend; i < opdsymend; ++i)
3554 {
3555 bfd_vma ent;
3556
3557 /* Ignore bogus symbols. */
3558 if (syms[i]->value > opd->size - 8)
3559 continue;
3560
3561 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3562 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3563 {
3564 ++count;
3565 size += sizeof (asymbol);
3566 size += strlen (syms[i]->name) + 2;
3567 }
3568 }
3569
3570 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3571 if (dyn_count != 0
3572 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3573 {
3574 bfd_byte *dynbuf, *extdyn, *extdynend;
3575 size_t extdynsize;
3576 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3577
3578 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3579 goto free_contents_and_exit_err;
3580
3581 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3582 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3583
3584 extdyn = dynbuf;
3585 extdynend = extdyn + dynamic->size;
3586 for (; extdyn < extdynend; extdyn += extdynsize)
3587 {
3588 Elf_Internal_Dyn dyn;
3589 (*swap_dyn_in) (abfd, extdyn, &dyn);
3590
3591 if (dyn.d_tag == DT_NULL)
3592 break;
3593
3594 if (dyn.d_tag == DT_PPC64_GLINK)
3595 {
3596 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3597 See comment in ppc64_elf_finish_dynamic_sections. */
3598 glink_vma = dyn.d_un.d_val + 8 * 4;
3599 /* The .glink section usually does not survive the final
3600 link; search for the section (usually .text) where the
3601 glink stubs now reside. */
3602 glink = bfd_sections_find_if (abfd, section_covers_vma,
3603 &glink_vma);
3604 break;
3605 }
3606 }
3607
3608 free (dynbuf);
3609 }
3610
3611 if (glink != NULL)
3612 {
3613 /* Determine __glink trampoline by reading the relative branch
3614 from the first glink stub. */
3615 bfd_byte buf[4];
3616 unsigned int off = 0;
3617
3618 while (bfd_get_section_contents (abfd, glink, buf,
3619 glink_vma + off - glink->vma, 4))
3620 {
3621 unsigned int insn = bfd_get_32 (abfd, buf);
3622 insn ^= B_DOT;
3623 if ((insn & ~0x3fffffc) == 0)
3624 {
3625 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3626 break;
3627 }
3628 off += 4;
3629 if (off > 4)
3630 break;
3631 }
3632
3633 if (resolv_vma)
3634 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3635
3636 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3637 if (relplt != NULL)
3638 {
3639 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3640 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3641 goto free_contents_and_exit_err;
3642
3643 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3644 size += plt_count * sizeof (asymbol);
3645
3646 p = relplt->relocation;
3647 for (i = 0; i < plt_count; i++, p++)
3648 {
3649 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3650 if (p->addend != 0)
3651 size += sizeof ("+0x") - 1 + 16;
3652 }
3653 }
3654 }
3655
3656 if (size == 0)
3657 goto free_contents_and_exit;
3658 s = *ret = bfd_malloc (size);
3659 if (s == NULL)
3660 goto free_contents_and_exit_err;
3661
3662 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3663
3664 for (i = secsymend; i < opdsymend; ++i)
3665 {
3666 bfd_vma ent;
3667
3668 if (syms[i]->value > opd->size - 8)
3669 continue;
3670
3671 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3672 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3673 {
3674 size_t lo, hi;
3675 size_t len;
3676 asection *sec = abfd->sections;
3677
3678 *s = *syms[i];
3679 lo = codesecsym;
3680 hi = codesecsymend;
3681 while (lo < hi)
3682 {
3683 size_t mid = (lo + hi) >> 1;
3684 if (syms[mid]->section->vma < ent)
3685 lo = mid + 1;
3686 else if (syms[mid]->section->vma > ent)
3687 hi = mid;
3688 else
3689 {
3690 sec = syms[mid]->section;
3691 break;
3692 }
3693 }
3694
3695 if (lo >= hi && lo > codesecsym)
3696 sec = syms[lo - 1]->section;
3697
3698 for (; sec != NULL; sec = sec->next)
3699 {
3700 if (sec->vma > ent)
3701 break;
3702 /* SEC_LOAD may not be set if SEC is from a separate debug
3703 info file. */
3704 if ((sec->flags & SEC_ALLOC) == 0)
3705 break;
3706 if ((sec->flags & SEC_CODE) != 0)
3707 s->section = sec;
3708 }
3709 s->flags |= BSF_SYNTHETIC;
3710 s->value = ent - s->section->vma;
3711 s->name = names;
3712 *names++ = '.';
3713 len = strlen (syms[i]->name);
3714 memcpy (names, syms[i]->name, len + 1);
3715 names += len + 1;
3716 /* Have udata.p point back to the original symbol this
3717 synthetic symbol was derived from. */
3718 s->udata.p = syms[i];
3719 s++;
3720 }
3721 }
3722 free (contents);
3723
3724 if (glink != NULL && relplt != NULL)
3725 {
3726 if (resolv_vma)
3727 {
3728 /* Add a symbol for the main glink trampoline. */
3729 memset (s, 0, sizeof *s);
3730 s->the_bfd = abfd;
3731 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3732 s->section = glink;
3733 s->value = resolv_vma - glink->vma;
3734 s->name = names;
3735 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3736 names += sizeof ("__glink_PLTresolve");
3737 s++;
3738 count++;
3739 }
3740
3741 /* FIXME: It would be very much nicer to put sym@plt on the
3742 stub rather than on the glink branch table entry. The
3743 objdump disassembler would then use a sensible symbol
3744 name on plt calls. The difficulty in doing so is
3745 a) finding the stubs, and,
3746 b) matching stubs against plt entries, and,
3747 c) there can be multiple stubs for a given plt entry.
3748
3749 Solving (a) could be done by code scanning, but older
3750 ppc64 binaries used different stubs to current code.
3751 (b) is the tricky one since you need to known the toc
3752 pointer for at least one function that uses a pic stub to
3753 be able to calculate the plt address referenced.
3754 (c) means gdb would need to set multiple breakpoints (or
3755 find the glink branch itself) when setting breakpoints
3756 for pending shared library loads. */
3757 p = relplt->relocation;
3758 for (i = 0; i < plt_count; i++, p++)
3759 {
3760 size_t len;
3761
3762 *s = **p->sym_ptr_ptr;
3763 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3764 we are defining a symbol, ensure one of them is set. */
3765 if ((s->flags & BSF_LOCAL) == 0)
3766 s->flags |= BSF_GLOBAL;
3767 s->flags |= BSF_SYNTHETIC;
3768 s->section = glink;
3769 s->value = glink_vma - glink->vma;
3770 s->name = names;
3771 s->udata.p = NULL;
3772 len = strlen ((*p->sym_ptr_ptr)->name);
3773 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3774 names += len;
3775 if (p->addend != 0)
3776 {
3777 memcpy (names, "+0x", sizeof ("+0x") - 1);
3778 names += sizeof ("+0x") - 1;
3779 bfd_sprintf_vma (abfd, names, p->addend);
3780 names += strlen (names);
3781 }
3782 memcpy (names, "@plt", sizeof ("@plt"));
3783 names += sizeof ("@plt");
3784 s++;
3785 if (abi < 2)
3786 {
3787 glink_vma += 8;
3788 if (i >= 0x8000)
3789 glink_vma += 4;
3790 }
3791 else
3792 glink_vma += 4;
3793 }
3794 count += plt_count;
3795 }
3796 }
3797
3798 done:
3799 free (syms);
3800 return count;
3801 }
3802 \f
3803 /* The following functions are specific to the ELF linker, while
3804 functions above are used generally. Those named ppc64_elf_* are
3805 called by the main ELF linker code. They appear in this file more
3806 or less in the order in which they are called. eg.
3807 ppc64_elf_check_relocs is called early in the link process,
3808 ppc64_elf_finish_dynamic_sections is one of the last functions
3809 called.
3810
3811 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3812 functions have both a function code symbol and a function descriptor
3813 symbol. A call to foo in a relocatable object file looks like:
3814
3815 . .text
3816 . x:
3817 . bl .foo
3818 . nop
3819
3820 The function definition in another object file might be:
3821
3822 . .section .opd
3823 . foo: .quad .foo
3824 . .quad .TOC.@tocbase
3825 . .quad 0
3826 .
3827 . .text
3828 . .foo: blr
3829
3830 When the linker resolves the call during a static link, the branch
3831 unsurprisingly just goes to .foo and the .opd information is unused.
3832 If the function definition is in a shared library, things are a little
3833 different: The call goes via a plt call stub, the opd information gets
3834 copied to the plt, and the linker patches the nop.
3835
3836 . x:
3837 . bl .foo_stub
3838 . ld 2,40(1)
3839 .
3840 .
3841 . .foo_stub:
3842 . std 2,40(1) # in practice, the call stub
3843 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3844 . addi 11,11,Lfoo@toc@l # this is the general idea
3845 . ld 12,0(11)
3846 . ld 2,8(11)
3847 . mtctr 12
3848 . ld 11,16(11)
3849 . bctr
3850 .
3851 . .section .plt
3852 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3853
3854 The "reloc ()" notation is supposed to indicate that the linker emits
3855 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3856 copying.
3857
3858 What are the difficulties here? Well, firstly, the relocations
3859 examined by the linker in check_relocs are against the function code
3860 sym .foo, while the dynamic relocation in the plt is emitted against
3861 the function descriptor symbol, foo. Somewhere along the line, we need
3862 to carefully copy dynamic link information from one symbol to the other.
3863 Secondly, the generic part of the elf linker will make .foo a dynamic
3864 symbol as is normal for most other backends. We need foo dynamic
3865 instead, at least for an application final link. However, when
3866 creating a shared library containing foo, we need to have both symbols
3867 dynamic so that references to .foo are satisfied during the early
3868 stages of linking. Otherwise the linker might decide to pull in a
3869 definition from some other object, eg. a static library.
3870
3871 Update: As of August 2004, we support a new convention. Function
3872 calls may use the function descriptor symbol, ie. "bl foo". This
3873 behaves exactly as "bl .foo". */
3874
3875 /* Of those relocs that might be copied as dynamic relocs, this
3876 function selects those that must be copied when linking a shared
3877 library or PIE, even when the symbol is local. */
3878
3879 static int
3880 must_be_dyn_reloc (struct bfd_link_info *info,
3881 enum elf_ppc64_reloc_type r_type)
3882 {
3883 switch (r_type)
3884 {
3885 default:
3886 /* Only relative relocs can be resolved when the object load
3887 address isn't fixed. DTPREL64 is excluded because the
3888 dynamic linker needs to differentiate global dynamic from
3889 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3890 return 1;
3891
3892 case R_PPC64_REL32:
3893 case R_PPC64_REL64:
3894 case R_PPC64_REL30:
3895 return 0;
3896
3897 case R_PPC64_TPREL16:
3898 case R_PPC64_TPREL16_LO:
3899 case R_PPC64_TPREL16_HI:
3900 case R_PPC64_TPREL16_HA:
3901 case R_PPC64_TPREL16_DS:
3902 case R_PPC64_TPREL16_LO_DS:
3903 case R_PPC64_TPREL16_HIGH:
3904 case R_PPC64_TPREL16_HIGHA:
3905 case R_PPC64_TPREL16_HIGHER:
3906 case R_PPC64_TPREL16_HIGHERA:
3907 case R_PPC64_TPREL16_HIGHEST:
3908 case R_PPC64_TPREL16_HIGHESTA:
3909 case R_PPC64_TPREL64:
3910 /* These relocations are relative but in a shared library the
3911 linker doesn't know the thread pointer base. */
3912 return bfd_link_dll (info);
3913 }
3914 }
3915
3916 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3917 copying dynamic variables from a shared lib into an app's dynbss
3918 section, and instead use a dynamic relocation to point into the
3919 shared lib. With code that gcc generates, it's vital that this be
3920 enabled; In the PowerPC64 ABI, the address of a function is actually
3921 the address of a function descriptor, which resides in the .opd
3922 section. gcc uses the descriptor directly rather than going via the
3923 GOT as some other ABI's do, which means that initialized function
3924 pointers must reference the descriptor. Thus, a function pointer
3925 initialized to the address of a function in a shared library will
3926 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3927 redefines the function descriptor symbol to point to the copy. This
3928 presents a problem as a plt entry for that function is also
3929 initialized from the function descriptor symbol and the copy reloc
3930 may not be initialized first. */
3931 #define ELIMINATE_COPY_RELOCS 1
3932
3933 /* Section name for stubs is the associated section name plus this
3934 string. */
3935 #define STUB_SUFFIX ".stub"
3936
3937 /* Linker stubs.
3938 ppc_stub_long_branch:
3939 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3940 destination, but a 24 bit branch in a stub section will reach.
3941 . b dest
3942
3943 ppc_stub_plt_branch:
3944 Similar to the above, but a 24 bit branch in the stub section won't
3945 reach its destination.
3946 . addis %r11,%r2,xxx@toc@ha
3947 . ld %r12,xxx@toc@l(%r11)
3948 . mtctr %r12
3949 . bctr
3950
3951 ppc_stub_plt_call:
3952 Used to call a function in a shared library. If it so happens that
3953 the plt entry referenced crosses a 64k boundary, then an extra
3954 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3955 . std %r2,40(%r1)
3956 . addis %r11,%r2,xxx@toc@ha
3957 . ld %r12,xxx+0@toc@l(%r11)
3958 . mtctr %r12
3959 . ld %r2,xxx+8@toc@l(%r11)
3960 . ld %r11,xxx+16@toc@l(%r11)
3961 . bctr
3962
3963 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3964 code to adjust the value and save r2 to support multiple toc sections.
3965 A ppc_stub_long_branch with an r2 offset looks like:
3966 . std %r2,40(%r1)
3967 . addis %r2,%r2,off@ha
3968 . addi %r2,%r2,off@l
3969 . b dest
3970
3971 A ppc_stub_plt_branch with an r2 offset looks like:
3972 . std %r2,40(%r1)
3973 . addis %r11,%r2,xxx@toc@ha
3974 . ld %r12,xxx@toc@l(%r11)
3975 . addis %r2,%r2,off@ha
3976 . addi %r2,%r2,off@l
3977 . mtctr %r12
3978 . bctr
3979
3980 In cases where the "addis" instruction would add zero, the "addis" is
3981 omitted and following instructions modified slightly in some cases.
3982 */
3983
3984 enum ppc_stub_type {
3985 ppc_stub_none,
3986 ppc_stub_long_branch,
3987 ppc_stub_long_branch_r2off,
3988 ppc_stub_plt_branch,
3989 ppc_stub_plt_branch_r2off,
3990 ppc_stub_plt_call,
3991 ppc_stub_plt_call_r2save,
3992 ppc_stub_global_entry,
3993 ppc_stub_save_res
3994 };
3995
3996 /* Information on stub grouping. */
3997 struct map_stub
3998 {
3999 /* The stub section. */
4000 asection *stub_sec;
4001 /* This is the section to which stubs in the group will be attached. */
4002 asection *link_sec;
4003 /* Next group. */
4004 struct map_stub *next;
4005 /* Whether to emit a copy of register save/restore functions in this
4006 group. */
4007 int needs_save_res;
4008 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4009 or -1u if no such stub with bctrl exists. */
4010 unsigned int tls_get_addr_opt_bctrl;
4011 };
4012
4013 struct ppc_stub_hash_entry {
4014
4015 /* Base hash table entry structure. */
4016 struct bfd_hash_entry root;
4017
4018 enum ppc_stub_type stub_type;
4019
4020 /* Group information. */
4021 struct map_stub *group;
4022
4023 /* Offset within stub_sec of the beginning of this stub. */
4024 bfd_vma stub_offset;
4025
4026 /* Given the symbol's value and its section we can determine its final
4027 value when building the stubs (so the stub knows where to jump. */
4028 bfd_vma target_value;
4029 asection *target_section;
4030
4031 /* The symbol table entry, if any, that this was derived from. */
4032 struct ppc_link_hash_entry *h;
4033 struct plt_entry *plt_ent;
4034
4035 /* Symbol type. */
4036 unsigned char symtype;
4037
4038 /* Symbol st_other. */
4039 unsigned char other;
4040 };
4041
4042 struct ppc_branch_hash_entry {
4043
4044 /* Base hash table entry structure. */
4045 struct bfd_hash_entry root;
4046
4047 /* Offset within branch lookup table. */
4048 unsigned int offset;
4049
4050 /* Generation marker. */
4051 unsigned int iter;
4052 };
4053
4054 /* Used to track dynamic relocations for local symbols. */
4055 struct ppc_dyn_relocs
4056 {
4057 struct ppc_dyn_relocs *next;
4058
4059 /* The input section of the reloc. */
4060 asection *sec;
4061
4062 /* Total number of relocs copied for the input section. */
4063 unsigned int count : 31;
4064
4065 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4066 unsigned int ifunc : 1;
4067 };
4068
4069 struct ppc_link_hash_entry
4070 {
4071 struct elf_link_hash_entry elf;
4072
4073 union {
4074 /* A pointer to the most recently used stub hash entry against this
4075 symbol. */
4076 struct ppc_stub_hash_entry *stub_cache;
4077
4078 /* A pointer to the next symbol starting with a '.' */
4079 struct ppc_link_hash_entry *next_dot_sym;
4080 } u;
4081
4082 /* Track dynamic relocs copied for this symbol. */
4083 struct elf_dyn_relocs *dyn_relocs;
4084
4085 /* Link between function code and descriptor symbols. */
4086 struct ppc_link_hash_entry *oh;
4087
4088 /* Flag function code and descriptor symbols. */
4089 unsigned int is_func:1;
4090 unsigned int is_func_descriptor:1;
4091 unsigned int fake:1;
4092
4093 /* Whether global opd/toc sym has been adjusted or not.
4094 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4095 should be set for all globals defined in any opd/toc section. */
4096 unsigned int adjust_done:1;
4097
4098 /* Set if this is an out-of-line register save/restore function,
4099 with non-standard calling convention. */
4100 unsigned int save_res:1;
4101
4102 /* Set if a duplicate symbol with non-zero localentry is detected,
4103 even when the duplicate symbol does not provide a definition. */
4104 unsigned int non_zero_localentry:1;
4105
4106 /* Contexts in which symbol is used in the GOT (or TOC).
4107 Bits are or'd into the mask as the corresponding relocs are
4108 encountered during check_relocs, with TLS_TLS being set when any
4109 of the other TLS bits are set. tls_optimize clears bits when
4110 optimizing to indicate the corresponding GOT entry type is not
4111 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4112 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4113 separate flag rather than setting TPREL just for convenience in
4114 distinguishing the two cases.
4115 These flags are also kept for local symbols. */
4116 #define TLS_TLS 1 /* Any TLS reloc. */
4117 #define TLS_GD 2 /* GD reloc. */
4118 #define TLS_LD 4 /* LD reloc. */
4119 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4120 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4121 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4122 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4123 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4124 unsigned char tls_mask;
4125
4126 /* The above field is also used to mark function symbols. In which
4127 case TLS_TLS will be 0. */
4128 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4129 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4130 #define NON_GOT 256 /* local symbol plt, not stored. */
4131 };
4132
4133 /* ppc64 ELF linker hash table. */
4134
4135 struct ppc_link_hash_table
4136 {
4137 struct elf_link_hash_table elf;
4138
4139 /* The stub hash table. */
4140 struct bfd_hash_table stub_hash_table;
4141
4142 /* Another hash table for plt_branch stubs. */
4143 struct bfd_hash_table branch_hash_table;
4144
4145 /* Hash table for function prologue tocsave. */
4146 htab_t tocsave_htab;
4147
4148 /* Various options and other info passed from the linker. */
4149 struct ppc64_elf_params *params;
4150
4151 /* The size of sec_info below. */
4152 unsigned int sec_info_arr_size;
4153
4154 /* Per-section array of extra section info. Done this way rather
4155 than as part of ppc64_elf_section_data so we have the info for
4156 non-ppc64 sections. */
4157 struct
4158 {
4159 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4160 bfd_vma toc_off;
4161
4162 union
4163 {
4164 /* The section group that this section belongs to. */
4165 struct map_stub *group;
4166 /* A temp section list pointer. */
4167 asection *list;
4168 } u;
4169 } *sec_info;
4170
4171 /* Linked list of groups. */
4172 struct map_stub *group;
4173
4174 /* Temp used when calculating TOC pointers. */
4175 bfd_vma toc_curr;
4176 bfd *toc_bfd;
4177 asection *toc_first_sec;
4178
4179 /* Used when adding symbols. */
4180 struct ppc_link_hash_entry *dot_syms;
4181
4182 /* Shortcuts to get to dynamic linker sections. */
4183 asection *glink;
4184 asection *global_entry;
4185 asection *sfpr;
4186 asection *pltlocal;
4187 asection *relpltlocal;
4188 asection *brlt;
4189 asection *relbrlt;
4190 asection *glink_eh_frame;
4191
4192 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4193 struct ppc_link_hash_entry *tls_get_addr;
4194 struct ppc_link_hash_entry *tls_get_addr_fd;
4195
4196 /* The size of reliplt used by got entry relocs. */
4197 bfd_size_type got_reli_size;
4198
4199 /* Statistics. */
4200 unsigned long stub_count[ppc_stub_global_entry];
4201
4202 /* Number of stubs against global syms. */
4203 unsigned long stub_globals;
4204
4205 /* Set if we're linking code with function descriptors. */
4206 unsigned int opd_abi:1;
4207
4208 /* Support for multiple toc sections. */
4209 unsigned int do_multi_toc:1;
4210 unsigned int multi_toc_needed:1;
4211 unsigned int second_toc_pass:1;
4212 unsigned int do_toc_opt:1;
4213
4214 /* Set if tls optimization is enabled. */
4215 unsigned int do_tls_opt:1;
4216
4217 /* Set if inline plt calls should be converted to direct calls. */
4218 unsigned int can_convert_all_inline_plt:1;
4219
4220 /* Set on error. */
4221 unsigned int stub_error:1;
4222
4223 /* Whether func_desc_adjust needs to be run over symbols. */
4224 unsigned int need_func_desc_adj:1;
4225
4226 /* Whether there exist local gnu indirect function resolvers,
4227 referenced by dynamic relocations. */
4228 unsigned int local_ifunc_resolver:1;
4229 unsigned int maybe_local_ifunc_resolver:1;
4230
4231 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4232 unsigned int has_plt_localentry0:1;
4233
4234 /* Incremented every time we size stubs. */
4235 unsigned int stub_iteration;
4236
4237 /* Small local sym cache. */
4238 struct sym_cache sym_cache;
4239 };
4240
4241 /* Rename some of the generic section flags to better document how they
4242 are used here. */
4243
4244 /* Nonzero if this section has TLS related relocations. */
4245 #define has_tls_reloc sec_flg0
4246
4247 /* Nonzero if this section has an old-style call to __tls_get_addr. */
4248 #define has_tls_get_addr_call sec_flg1
4249
4250 /* Nonzero if this section has any toc or got relocs. */
4251 #define has_toc_reloc sec_flg2
4252
4253 /* Nonzero if this section has a call to another section that uses
4254 the toc or got. */
4255 #define makes_toc_func_call sec_flg3
4256
4257 /* Recursion protection when determining above flag. */
4258 #define call_check_in_progress sec_flg4
4259 #define call_check_done sec_flg5
4260
4261 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4262
4263 #define ppc_hash_table(p) \
4264 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4265 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4266
4267 #define ppc_stub_hash_lookup(table, string, create, copy) \
4268 ((struct ppc_stub_hash_entry *) \
4269 bfd_hash_lookup ((table), (string), (create), (copy)))
4270
4271 #define ppc_branch_hash_lookup(table, string, create, copy) \
4272 ((struct ppc_branch_hash_entry *) \
4273 bfd_hash_lookup ((table), (string), (create), (copy)))
4274
4275 /* Create an entry in the stub hash table. */
4276
4277 static struct bfd_hash_entry *
4278 stub_hash_newfunc (struct bfd_hash_entry *entry,
4279 struct bfd_hash_table *table,
4280 const char *string)
4281 {
4282 /* Allocate the structure if it has not already been allocated by a
4283 subclass. */
4284 if (entry == NULL)
4285 {
4286 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4287 if (entry == NULL)
4288 return entry;
4289 }
4290
4291 /* Call the allocation method of the superclass. */
4292 entry = bfd_hash_newfunc (entry, table, string);
4293 if (entry != NULL)
4294 {
4295 struct ppc_stub_hash_entry *eh;
4296
4297 /* Initialize the local fields. */
4298 eh = (struct ppc_stub_hash_entry *) entry;
4299 eh->stub_type = ppc_stub_none;
4300 eh->group = NULL;
4301 eh->stub_offset = 0;
4302 eh->target_value = 0;
4303 eh->target_section = NULL;
4304 eh->h = NULL;
4305 eh->plt_ent = NULL;
4306 eh->other = 0;
4307 }
4308
4309 return entry;
4310 }
4311
4312 /* Create an entry in the branch hash table. */
4313
4314 static struct bfd_hash_entry *
4315 branch_hash_newfunc (struct bfd_hash_entry *entry,
4316 struct bfd_hash_table *table,
4317 const char *string)
4318 {
4319 /* Allocate the structure if it has not already been allocated by a
4320 subclass. */
4321 if (entry == NULL)
4322 {
4323 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4324 if (entry == NULL)
4325 return entry;
4326 }
4327
4328 /* Call the allocation method of the superclass. */
4329 entry = bfd_hash_newfunc (entry, table, string);
4330 if (entry != NULL)
4331 {
4332 struct ppc_branch_hash_entry *eh;
4333
4334 /* Initialize the local fields. */
4335 eh = (struct ppc_branch_hash_entry *) entry;
4336 eh->offset = 0;
4337 eh->iter = 0;
4338 }
4339
4340 return entry;
4341 }
4342
4343 /* Create an entry in a ppc64 ELF linker hash table. */
4344
4345 static struct bfd_hash_entry *
4346 link_hash_newfunc (struct bfd_hash_entry *entry,
4347 struct bfd_hash_table *table,
4348 const char *string)
4349 {
4350 /* Allocate the structure if it has not already been allocated by a
4351 subclass. */
4352 if (entry == NULL)
4353 {
4354 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4355 if (entry == NULL)
4356 return entry;
4357 }
4358
4359 /* Call the allocation method of the superclass. */
4360 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4361 if (entry != NULL)
4362 {
4363 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4364
4365 memset (&eh->u.stub_cache, 0,
4366 (sizeof (struct ppc_link_hash_entry)
4367 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4368
4369 /* When making function calls, old ABI code references function entry
4370 points (dot symbols), while new ABI code references the function
4371 descriptor symbol. We need to make any combination of reference and
4372 definition work together, without breaking archive linking.
4373
4374 For a defined function "foo" and an undefined call to "bar":
4375 An old object defines "foo" and ".foo", references ".bar" (possibly
4376 "bar" too).
4377 A new object defines "foo" and references "bar".
4378
4379 A new object thus has no problem with its undefined symbols being
4380 satisfied by definitions in an old object. On the other hand, the
4381 old object won't have ".bar" satisfied by a new object.
4382
4383 Keep a list of newly added dot-symbols. */
4384
4385 if (string[0] == '.')
4386 {
4387 struct ppc_link_hash_table *htab;
4388
4389 htab = (struct ppc_link_hash_table *) table;
4390 eh->u.next_dot_sym = htab->dot_syms;
4391 htab->dot_syms = eh;
4392 }
4393 }
4394
4395 return entry;
4396 }
4397
4398 struct tocsave_entry {
4399 asection *sec;
4400 bfd_vma offset;
4401 };
4402
4403 static hashval_t
4404 tocsave_htab_hash (const void *p)
4405 {
4406 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4407 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4408 }
4409
4410 static int
4411 tocsave_htab_eq (const void *p1, const void *p2)
4412 {
4413 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4414 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4415 return e1->sec == e2->sec && e1->offset == e2->offset;
4416 }
4417
4418 /* Destroy a ppc64 ELF linker hash table. */
4419
4420 static void
4421 ppc64_elf_link_hash_table_free (bfd *obfd)
4422 {
4423 struct ppc_link_hash_table *htab;
4424
4425 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4426 if (htab->tocsave_htab)
4427 htab_delete (htab->tocsave_htab);
4428 bfd_hash_table_free (&htab->branch_hash_table);
4429 bfd_hash_table_free (&htab->stub_hash_table);
4430 _bfd_elf_link_hash_table_free (obfd);
4431 }
4432
4433 /* Create a ppc64 ELF linker hash table. */
4434
4435 static struct bfd_link_hash_table *
4436 ppc64_elf_link_hash_table_create (bfd *abfd)
4437 {
4438 struct ppc_link_hash_table *htab;
4439 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4440
4441 htab = bfd_zmalloc (amt);
4442 if (htab == NULL)
4443 return NULL;
4444
4445 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4446 sizeof (struct ppc_link_hash_entry),
4447 PPC64_ELF_DATA))
4448 {
4449 free (htab);
4450 return NULL;
4451 }
4452
4453 /* Init the stub hash table too. */
4454 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4455 sizeof (struct ppc_stub_hash_entry)))
4456 {
4457 _bfd_elf_link_hash_table_free (abfd);
4458 return NULL;
4459 }
4460
4461 /* And the branch hash table. */
4462 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4463 sizeof (struct ppc_branch_hash_entry)))
4464 {
4465 bfd_hash_table_free (&htab->stub_hash_table);
4466 _bfd_elf_link_hash_table_free (abfd);
4467 return NULL;
4468 }
4469
4470 htab->tocsave_htab = htab_try_create (1024,
4471 tocsave_htab_hash,
4472 tocsave_htab_eq,
4473 NULL);
4474 if (htab->tocsave_htab == NULL)
4475 {
4476 ppc64_elf_link_hash_table_free (abfd);
4477 return NULL;
4478 }
4479 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4480
4481 /* Initializing two fields of the union is just cosmetic. We really
4482 only care about glist, but when compiled on a 32-bit host the
4483 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4484 debugger inspection of these fields look nicer. */
4485 htab->elf.init_got_refcount.refcount = 0;
4486 htab->elf.init_got_refcount.glist = NULL;
4487 htab->elf.init_plt_refcount.refcount = 0;
4488 htab->elf.init_plt_refcount.glist = NULL;
4489 htab->elf.init_got_offset.offset = 0;
4490 htab->elf.init_got_offset.glist = NULL;
4491 htab->elf.init_plt_offset.offset = 0;
4492 htab->elf.init_plt_offset.glist = NULL;
4493
4494 return &htab->elf.root;
4495 }
4496
4497 /* Create sections for linker generated code. */
4498
4499 static bfd_boolean
4500 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4501 {
4502 struct ppc_link_hash_table *htab;
4503 flagword flags;
4504
4505 htab = ppc_hash_table (info);
4506
4507 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4508 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4509 if (htab->params->save_restore_funcs)
4510 {
4511 /* Create .sfpr for code to save and restore fp regs. */
4512 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4513 flags);
4514 if (htab->sfpr == NULL
4515 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4516 return FALSE;
4517 }
4518
4519 if (bfd_link_relocatable (info))
4520 return TRUE;
4521
4522 /* Create .glink for lazy dynamic linking support. */
4523 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4524 flags);
4525 if (htab->glink == NULL
4526 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4527 return FALSE;
4528
4529 /* The part of .glink used by global entry stubs, separate so that
4530 it can be aligned appropriately without affecting htab->glink. */
4531 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4532 flags);
4533 if (htab->global_entry == NULL
4534 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4535 return FALSE;
4536
4537 if (!info->no_ld_generated_unwind_info)
4538 {
4539 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4540 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4541 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4542 ".eh_frame",
4543 flags);
4544 if (htab->glink_eh_frame == NULL
4545 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4546 return FALSE;
4547 }
4548
4549 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4550 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4551 if (htab->elf.iplt == NULL
4552 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4553 return FALSE;
4554
4555 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4556 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4557 htab->elf.irelplt
4558 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4559 if (htab->elf.irelplt == NULL
4560 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4561 return FALSE;
4562
4563 /* Create branch lookup table for plt_branch stubs. */
4564 flags = (SEC_ALLOC | SEC_LOAD
4565 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4566 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4567 flags);
4568 if (htab->brlt == NULL
4569 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4570 return FALSE;
4571
4572 /* Local plt entries, put in .branch_lt but a separate section for
4573 convenience. */
4574 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4575 flags);
4576 if (htab->pltlocal == NULL
4577 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4578 return FALSE;
4579
4580 if (!bfd_link_pic (info))
4581 return TRUE;
4582
4583 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4584 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4585 htab->relbrlt
4586 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4587 if (htab->relbrlt == NULL
4588 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4589 return FALSE;
4590
4591 htab->relpltlocal
4592 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4593 if (htab->relpltlocal == NULL
4594 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4595 return FALSE;
4596
4597 return TRUE;
4598 }
4599
4600 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4601
4602 bfd_boolean
4603 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4604 struct ppc64_elf_params *params)
4605 {
4606 struct ppc_link_hash_table *htab;
4607
4608 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4609
4610 /* Always hook our dynamic sections into the first bfd, which is the
4611 linker created stub bfd. This ensures that the GOT header is at
4612 the start of the output TOC section. */
4613 htab = ppc_hash_table (info);
4614 htab->elf.dynobj = params->stub_bfd;
4615 htab->params = params;
4616
4617 return create_linkage_sections (htab->elf.dynobj, info);
4618 }
4619
4620 /* Build a name for an entry in the stub hash table. */
4621
4622 static char *
4623 ppc_stub_name (const asection *input_section,
4624 const asection *sym_sec,
4625 const struct ppc_link_hash_entry *h,
4626 const Elf_Internal_Rela *rel)
4627 {
4628 char *stub_name;
4629 ssize_t len;
4630
4631 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4632 offsets from a sym as a branch target? In fact, we could
4633 probably assume the addend is always zero. */
4634 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4635
4636 if (h)
4637 {
4638 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4639 stub_name = bfd_malloc (len);
4640 if (stub_name == NULL)
4641 return stub_name;
4642
4643 len = sprintf (stub_name, "%08x.%s+%x",
4644 input_section->id & 0xffffffff,
4645 h->elf.root.root.string,
4646 (int) rel->r_addend & 0xffffffff);
4647 }
4648 else
4649 {
4650 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4651 stub_name = bfd_malloc (len);
4652 if (stub_name == NULL)
4653 return stub_name;
4654
4655 len = sprintf (stub_name, "%08x.%x:%x+%x",
4656 input_section->id & 0xffffffff,
4657 sym_sec->id & 0xffffffff,
4658 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4659 (int) rel->r_addend & 0xffffffff);
4660 }
4661 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4662 stub_name[len - 2] = 0;
4663 return stub_name;
4664 }
4665
4666 /* Look up an entry in the stub hash. Stub entries are cached because
4667 creating the stub name takes a bit of time. */
4668
4669 static struct ppc_stub_hash_entry *
4670 ppc_get_stub_entry (const asection *input_section,
4671 const asection *sym_sec,
4672 struct ppc_link_hash_entry *h,
4673 const Elf_Internal_Rela *rel,
4674 struct ppc_link_hash_table *htab)
4675 {
4676 struct ppc_stub_hash_entry *stub_entry;
4677 struct map_stub *group;
4678
4679 /* If this input section is part of a group of sections sharing one
4680 stub section, then use the id of the first section in the group.
4681 Stub names need to include a section id, as there may well be
4682 more than one stub used to reach say, printf, and we need to
4683 distinguish between them. */
4684 group = htab->sec_info[input_section->id].u.group;
4685 if (group == NULL)
4686 return NULL;
4687
4688 if (h != NULL && h->u.stub_cache != NULL
4689 && h->u.stub_cache->h == h
4690 && h->u.stub_cache->group == group)
4691 {
4692 stub_entry = h->u.stub_cache;
4693 }
4694 else
4695 {
4696 char *stub_name;
4697
4698 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4699 if (stub_name == NULL)
4700 return NULL;
4701
4702 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4703 stub_name, FALSE, FALSE);
4704 if (h != NULL)
4705 h->u.stub_cache = stub_entry;
4706
4707 free (stub_name);
4708 }
4709
4710 return stub_entry;
4711 }
4712
4713 /* Add a new stub entry to the stub hash. Not all fields of the new
4714 stub entry are initialised. */
4715
4716 static struct ppc_stub_hash_entry *
4717 ppc_add_stub (const char *stub_name,
4718 asection *section,
4719 struct bfd_link_info *info)
4720 {
4721 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4722 struct map_stub *group;
4723 asection *link_sec;
4724 asection *stub_sec;
4725 struct ppc_stub_hash_entry *stub_entry;
4726
4727 group = htab->sec_info[section->id].u.group;
4728 link_sec = group->link_sec;
4729 stub_sec = group->stub_sec;
4730 if (stub_sec == NULL)
4731 {
4732 size_t namelen;
4733 bfd_size_type len;
4734 char *s_name;
4735
4736 namelen = strlen (link_sec->name);
4737 len = namelen + sizeof (STUB_SUFFIX);
4738 s_name = bfd_alloc (htab->params->stub_bfd, len);
4739 if (s_name == NULL)
4740 return NULL;
4741
4742 memcpy (s_name, link_sec->name, namelen);
4743 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4744 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4745 if (stub_sec == NULL)
4746 return NULL;
4747 group->stub_sec = stub_sec;
4748 }
4749
4750 /* Enter this entry into the linker stub hash table. */
4751 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4752 TRUE, FALSE);
4753 if (stub_entry == NULL)
4754 {
4755 /* xgettext:c-format */
4756 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4757 section->owner, stub_name);
4758 return NULL;
4759 }
4760
4761 stub_entry->group = group;
4762 stub_entry->stub_offset = 0;
4763 return stub_entry;
4764 }
4765
4766 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4767 not already done. */
4768
4769 static bfd_boolean
4770 create_got_section (bfd *abfd, struct bfd_link_info *info)
4771 {
4772 asection *got, *relgot;
4773 flagword flags;
4774 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4775
4776 if (!is_ppc64_elf (abfd))
4777 return FALSE;
4778 if (htab == NULL)
4779 return FALSE;
4780
4781 if (!htab->elf.sgot
4782 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4783 return FALSE;
4784
4785 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4786 | SEC_LINKER_CREATED);
4787
4788 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4789 if (!got
4790 || !bfd_set_section_alignment (abfd, got, 3))
4791 return FALSE;
4792
4793 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4794 flags | SEC_READONLY);
4795 if (!relgot
4796 || ! bfd_set_section_alignment (abfd, relgot, 3))
4797 return FALSE;
4798
4799 ppc64_elf_tdata (abfd)->got = got;
4800 ppc64_elf_tdata (abfd)->relgot = relgot;
4801 return TRUE;
4802 }
4803
4804 /* Follow indirect and warning symbol links. */
4805
4806 static inline struct bfd_link_hash_entry *
4807 follow_link (struct bfd_link_hash_entry *h)
4808 {
4809 while (h->type == bfd_link_hash_indirect
4810 || h->type == bfd_link_hash_warning)
4811 h = h->u.i.link;
4812 return h;
4813 }
4814
4815 static inline struct elf_link_hash_entry *
4816 elf_follow_link (struct elf_link_hash_entry *h)
4817 {
4818 return (struct elf_link_hash_entry *) follow_link (&h->root);
4819 }
4820
4821 static inline struct ppc_link_hash_entry *
4822 ppc_follow_link (struct ppc_link_hash_entry *h)
4823 {
4824 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4825 }
4826
4827 /* Merge PLT info on FROM with that on TO. */
4828
4829 static void
4830 move_plt_plist (struct ppc_link_hash_entry *from,
4831 struct ppc_link_hash_entry *to)
4832 {
4833 if (from->elf.plt.plist != NULL)
4834 {
4835 if (to->elf.plt.plist != NULL)
4836 {
4837 struct plt_entry **entp;
4838 struct plt_entry *ent;
4839
4840 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4841 {
4842 struct plt_entry *dent;
4843
4844 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4845 if (dent->addend == ent->addend)
4846 {
4847 dent->plt.refcount += ent->plt.refcount;
4848 *entp = ent->next;
4849 break;
4850 }
4851 if (dent == NULL)
4852 entp = &ent->next;
4853 }
4854 *entp = to->elf.plt.plist;
4855 }
4856
4857 to->elf.plt.plist = from->elf.plt.plist;
4858 from->elf.plt.plist = NULL;
4859 }
4860 }
4861
4862 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4863
4864 static void
4865 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4866 struct elf_link_hash_entry *dir,
4867 struct elf_link_hash_entry *ind)
4868 {
4869 struct ppc_link_hash_entry *edir, *eind;
4870
4871 edir = (struct ppc_link_hash_entry *) dir;
4872 eind = (struct ppc_link_hash_entry *) ind;
4873
4874 edir->is_func |= eind->is_func;
4875 edir->is_func_descriptor |= eind->is_func_descriptor;
4876 edir->tls_mask |= eind->tls_mask;
4877 if (eind->oh != NULL)
4878 edir->oh = ppc_follow_link (eind->oh);
4879
4880 if (edir->elf.versioned != versioned_hidden)
4881 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4882 edir->elf.ref_regular |= eind->elf.ref_regular;
4883 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4884 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4885 edir->elf.needs_plt |= eind->elf.needs_plt;
4886 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4887
4888 /* If we were called to copy over info for a weak sym, don't copy
4889 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4890 in order to simplify readonly_dynrelocs and save a field in the
4891 symbol hash entry, but that means dyn_relocs can't be used in any
4892 tests about a specific symbol, or affect other symbol flags which
4893 are then tested. */
4894 if (eind->elf.root.type != bfd_link_hash_indirect)
4895 return;
4896
4897 /* Copy over any dynamic relocs we may have on the indirect sym. */
4898 if (eind->dyn_relocs != NULL)
4899 {
4900 if (edir->dyn_relocs != NULL)
4901 {
4902 struct elf_dyn_relocs **pp;
4903 struct elf_dyn_relocs *p;
4904
4905 /* Add reloc counts against the indirect sym to the direct sym
4906 list. Merge any entries against the same section. */
4907 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4908 {
4909 struct elf_dyn_relocs *q;
4910
4911 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4912 if (q->sec == p->sec)
4913 {
4914 q->pc_count += p->pc_count;
4915 q->count += p->count;
4916 *pp = p->next;
4917 break;
4918 }
4919 if (q == NULL)
4920 pp = &p->next;
4921 }
4922 *pp = edir->dyn_relocs;
4923 }
4924
4925 edir->dyn_relocs = eind->dyn_relocs;
4926 eind->dyn_relocs = NULL;
4927 }
4928
4929 /* Copy over got entries that we may have already seen to the
4930 symbol which just became indirect. */
4931 if (eind->elf.got.glist != NULL)
4932 {
4933 if (edir->elf.got.glist != NULL)
4934 {
4935 struct got_entry **entp;
4936 struct got_entry *ent;
4937
4938 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4939 {
4940 struct got_entry *dent;
4941
4942 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4943 if (dent->addend == ent->addend
4944 && dent->owner == ent->owner
4945 && dent->tls_type == ent->tls_type)
4946 {
4947 dent->got.refcount += ent->got.refcount;
4948 *entp = ent->next;
4949 break;
4950 }
4951 if (dent == NULL)
4952 entp = &ent->next;
4953 }
4954 *entp = edir->elf.got.glist;
4955 }
4956
4957 edir->elf.got.glist = eind->elf.got.glist;
4958 eind->elf.got.glist = NULL;
4959 }
4960
4961 /* And plt entries. */
4962 move_plt_plist (eind, edir);
4963
4964 if (eind->elf.dynindx != -1)
4965 {
4966 if (edir->elf.dynindx != -1)
4967 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4968 edir->elf.dynstr_index);
4969 edir->elf.dynindx = eind->elf.dynindx;
4970 edir->elf.dynstr_index = eind->elf.dynstr_index;
4971 eind->elf.dynindx = -1;
4972 eind->elf.dynstr_index = 0;
4973 }
4974 }
4975
4976 /* Find the function descriptor hash entry from the given function code
4977 hash entry FH. Link the entries via their OH fields. */
4978
4979 static struct ppc_link_hash_entry *
4980 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4981 {
4982 struct ppc_link_hash_entry *fdh = fh->oh;
4983
4984 if (fdh == NULL)
4985 {
4986 const char *fd_name = fh->elf.root.root.string + 1;
4987
4988 fdh = (struct ppc_link_hash_entry *)
4989 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4990 if (fdh == NULL)
4991 return fdh;
4992
4993 fdh->is_func_descriptor = 1;
4994 fdh->oh = fh;
4995 fh->is_func = 1;
4996 fh->oh = fdh;
4997 }
4998
4999 fdh = ppc_follow_link (fdh);
5000 fdh->is_func_descriptor = 1;
5001 fdh->oh = fh;
5002 return fdh;
5003 }
5004
5005 /* Make a fake function descriptor sym for the undefined code sym FH. */
5006
5007 static struct ppc_link_hash_entry *
5008 make_fdh (struct bfd_link_info *info,
5009 struct ppc_link_hash_entry *fh)
5010 {
5011 bfd *abfd = fh->elf.root.u.undef.abfd;
5012 struct bfd_link_hash_entry *bh = NULL;
5013 struct ppc_link_hash_entry *fdh;
5014 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5015 ? BSF_WEAK
5016 : BSF_GLOBAL);
5017
5018 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5019 fh->elf.root.root.string + 1,
5020 flags, bfd_und_section_ptr, 0,
5021 NULL, FALSE, FALSE, &bh))
5022 return NULL;
5023
5024 fdh = (struct ppc_link_hash_entry *) bh;
5025 fdh->elf.non_elf = 0;
5026 fdh->fake = 1;
5027 fdh->is_func_descriptor = 1;
5028 fdh->oh = fh;
5029 fh->is_func = 1;
5030 fh->oh = fdh;
5031 return fdh;
5032 }
5033
5034 /* Fix function descriptor symbols defined in .opd sections to be
5035 function type. */
5036
5037 static bfd_boolean
5038 ppc64_elf_add_symbol_hook (bfd *ibfd,
5039 struct bfd_link_info *info,
5040 Elf_Internal_Sym *isym,
5041 const char **name,
5042 flagword *flags ATTRIBUTE_UNUSED,
5043 asection **sec,
5044 bfd_vma *value)
5045 {
5046 if (*sec != NULL
5047 && strcmp ((*sec)->name, ".opd") == 0)
5048 {
5049 asection *code_sec;
5050
5051 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5052 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5053 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5054
5055 /* If the symbol is a function defined in .opd, and the function
5056 code is in a discarded group, let it appear to be undefined. */
5057 if (!bfd_link_relocatable (info)
5058 && (*sec)->reloc_count != 0
5059 && opd_entry_value (*sec, *value, &code_sec, NULL,
5060 FALSE) != (bfd_vma) -1
5061 && discarded_section (code_sec))
5062 {
5063 *sec = bfd_und_section_ptr;
5064 isym->st_shndx = SHN_UNDEF;
5065 }
5066 }
5067 else if (*sec != NULL
5068 && strcmp ((*sec)->name, ".toc") == 0
5069 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5070 {
5071 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5072 if (htab != NULL)
5073 htab->params->object_in_toc = 1;
5074 }
5075
5076 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5077 {
5078 if (abiversion (ibfd) == 0)
5079 set_abiversion (ibfd, 2);
5080 else if (abiversion (ibfd) == 1)
5081 {
5082 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5083 " for ABI version 1"), *name);
5084 bfd_set_error (bfd_error_bad_value);
5085 return FALSE;
5086 }
5087 }
5088
5089 return TRUE;
5090 }
5091
5092 /* Merge non-visibility st_other attributes: local entry point. */
5093
5094 static void
5095 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5096 const Elf_Internal_Sym *isym,
5097 bfd_boolean definition,
5098 bfd_boolean dynamic)
5099 {
5100 if (definition && (!dynamic || !h->def_regular))
5101 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5102 | ELF_ST_VISIBILITY (h->other));
5103 }
5104
5105 /* Hook called on merging a symbol. We use this to clear "fake" since
5106 we now have a real symbol. */
5107
5108 static bfd_boolean
5109 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5110 const Elf_Internal_Sym *isym,
5111 asection **psec ATTRIBUTE_UNUSED,
5112 bfd_boolean newdef ATTRIBUTE_UNUSED,
5113 bfd_boolean olddef ATTRIBUTE_UNUSED,
5114 bfd *oldbfd ATTRIBUTE_UNUSED,
5115 const asection *oldsec ATTRIBUTE_UNUSED)
5116 {
5117 ((struct ppc_link_hash_entry *) h)->fake = 0;
5118 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5119 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5120 return TRUE;
5121 }
5122
5123 /* This function makes an old ABI object reference to ".bar" cause the
5124 inclusion of a new ABI object archive that defines "bar".
5125 NAME is a symbol defined in an archive. Return a symbol in the hash
5126 table that might be satisfied by the archive symbols. */
5127
5128 static struct elf_link_hash_entry *
5129 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5130 struct bfd_link_info *info,
5131 const char *name)
5132 {
5133 struct elf_link_hash_entry *h;
5134 char *dot_name;
5135 size_t len;
5136
5137 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5138 if (h != NULL
5139 /* Don't return this sym if it is a fake function descriptor
5140 created by add_symbol_adjust. */
5141 && !((struct ppc_link_hash_entry *) h)->fake)
5142 return h;
5143
5144 if (name[0] == '.')
5145 return h;
5146
5147 len = strlen (name);
5148 dot_name = bfd_alloc (abfd, len + 2);
5149 if (dot_name == NULL)
5150 return (struct elf_link_hash_entry *) -1;
5151 dot_name[0] = '.';
5152 memcpy (dot_name + 1, name, len + 1);
5153 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5154 bfd_release (abfd, dot_name);
5155 return h;
5156 }
5157
5158 /* This function satisfies all old ABI object references to ".bar" if a
5159 new ABI object defines "bar". Well, at least, undefined dot symbols
5160 are made weak. This stops later archive searches from including an
5161 object if we already have a function descriptor definition. It also
5162 prevents the linker complaining about undefined symbols.
5163 We also check and correct mismatched symbol visibility here. The
5164 most restrictive visibility of the function descriptor and the
5165 function entry symbol is used. */
5166
5167 static bfd_boolean
5168 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5169 {
5170 struct ppc_link_hash_table *htab;
5171 struct ppc_link_hash_entry *fdh;
5172
5173 if (eh->elf.root.type == bfd_link_hash_warning)
5174 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5175
5176 if (eh->elf.root.type == bfd_link_hash_indirect)
5177 return TRUE;
5178
5179 if (eh->elf.root.root.string[0] != '.')
5180 abort ();
5181
5182 htab = ppc_hash_table (info);
5183 if (htab == NULL)
5184 return FALSE;
5185
5186 fdh = lookup_fdh (eh, htab);
5187 if (fdh == NULL
5188 && !bfd_link_relocatable (info)
5189 && (eh->elf.root.type == bfd_link_hash_undefined
5190 || eh->elf.root.type == bfd_link_hash_undefweak)
5191 && eh->elf.ref_regular)
5192 {
5193 /* Make an undefined function descriptor sym, in order to
5194 pull in an --as-needed shared lib. Archives are handled
5195 elsewhere. */
5196 fdh = make_fdh (info, eh);
5197 if (fdh == NULL)
5198 return FALSE;
5199 }
5200
5201 if (fdh != NULL)
5202 {
5203 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5204 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5205
5206 /* Make both descriptor and entry symbol have the most
5207 constraining visibility of either symbol. */
5208 if (entry_vis < descr_vis)
5209 fdh->elf.other += entry_vis - descr_vis;
5210 else if (entry_vis > descr_vis)
5211 eh->elf.other += descr_vis - entry_vis;
5212
5213 /* Propagate reference flags from entry symbol to function
5214 descriptor symbol. */
5215 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5216 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5217 fdh->elf.ref_regular |= eh->elf.ref_regular;
5218 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5219
5220 if (!fdh->elf.forced_local
5221 && fdh->elf.dynindx == -1
5222 && fdh->elf.versioned != versioned_hidden
5223 && (bfd_link_dll (info)
5224 || fdh->elf.def_dynamic
5225 || fdh->elf.ref_dynamic)
5226 && (eh->elf.ref_regular
5227 || eh->elf.def_regular))
5228 {
5229 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5230 return FALSE;
5231 }
5232 }
5233
5234 return TRUE;
5235 }
5236
5237 /* Set up opd section info and abiversion for IBFD, and process list
5238 of dot-symbols we made in link_hash_newfunc. */
5239
5240 static bfd_boolean
5241 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5242 {
5243 struct ppc_link_hash_table *htab;
5244 struct ppc_link_hash_entry **p, *eh;
5245 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5246
5247 if (opd != NULL && opd->size != 0)
5248 {
5249 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5250 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5251
5252 if (abiversion (ibfd) == 0)
5253 set_abiversion (ibfd, 1);
5254 else if (abiversion (ibfd) >= 2)
5255 {
5256 /* xgettext:c-format */
5257 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5258 ibfd, abiversion (ibfd));
5259 bfd_set_error (bfd_error_bad_value);
5260 return FALSE;
5261 }
5262 }
5263
5264 if (is_ppc64_elf (info->output_bfd))
5265 {
5266 /* For input files without an explicit abiversion in e_flags
5267 we should have flagged any with symbol st_other bits set
5268 as ELFv1 and above flagged those with .opd as ELFv2.
5269 Set the output abiversion if not yet set, and for any input
5270 still ambiguous, take its abiversion from the output.
5271 Differences in ABI are reported later. */
5272 if (abiversion (info->output_bfd) == 0)
5273 set_abiversion (info->output_bfd, abiversion (ibfd));
5274 else if (abiversion (ibfd) == 0)
5275 set_abiversion (ibfd, abiversion (info->output_bfd));
5276 }
5277
5278 htab = ppc_hash_table (info);
5279 if (htab == NULL)
5280 return TRUE;
5281
5282 if (opd != NULL && opd->size != 0
5283 && (ibfd->flags & DYNAMIC) == 0
5284 && (opd->flags & SEC_RELOC) != 0
5285 && opd->reloc_count != 0
5286 && !bfd_is_abs_section (opd->output_section)
5287 && info->gc_sections)
5288 {
5289 /* Garbage collection needs some extra help with .opd sections.
5290 We don't want to necessarily keep everything referenced by
5291 relocs in .opd, as that would keep all functions. Instead,
5292 if we reference an .opd symbol (a function descriptor), we
5293 want to keep the function code symbol's section. This is
5294 easy for global symbols, but for local syms we need to keep
5295 information about the associated function section. */
5296 bfd_size_type amt;
5297 asection **opd_sym_map;
5298 Elf_Internal_Shdr *symtab_hdr;
5299 Elf_Internal_Rela *relocs, *rel_end, *rel;
5300
5301 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5302 opd_sym_map = bfd_zalloc (ibfd, amt);
5303 if (opd_sym_map == NULL)
5304 return FALSE;
5305 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5306 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5307 info->keep_memory);
5308 if (relocs == NULL)
5309 return FALSE;
5310 symtab_hdr = &elf_symtab_hdr (ibfd);
5311 rel_end = relocs + opd->reloc_count - 1;
5312 for (rel = relocs; rel < rel_end; rel++)
5313 {
5314 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5315 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5316
5317 if (r_type == R_PPC64_ADDR64
5318 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5319 && r_symndx < symtab_hdr->sh_info)
5320 {
5321 Elf_Internal_Sym *isym;
5322 asection *s;
5323
5324 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5325 if (isym == NULL)
5326 {
5327 if (elf_section_data (opd)->relocs != relocs)
5328 free (relocs);
5329 return FALSE;
5330 }
5331
5332 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5333 if (s != NULL && s != opd)
5334 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5335 }
5336 }
5337 if (elf_section_data (opd)->relocs != relocs)
5338 free (relocs);
5339 }
5340
5341 p = &htab->dot_syms;
5342 while ((eh = *p) != NULL)
5343 {
5344 *p = NULL;
5345 if (&eh->elf == htab->elf.hgot)
5346 ;
5347 else if (htab->elf.hgot == NULL
5348 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5349 htab->elf.hgot = &eh->elf;
5350 else if (abiversion (ibfd) <= 1)
5351 {
5352 htab->need_func_desc_adj = 1;
5353 if (!add_symbol_adjust (eh, info))
5354 return FALSE;
5355 }
5356 p = &eh->u.next_dot_sym;
5357 }
5358 return TRUE;
5359 }
5360
5361 /* Undo hash table changes when an --as-needed input file is determined
5362 not to be needed. */
5363
5364 static bfd_boolean
5365 ppc64_elf_notice_as_needed (bfd *ibfd,
5366 struct bfd_link_info *info,
5367 enum notice_asneeded_action act)
5368 {
5369 if (act == notice_not_needed)
5370 {
5371 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5372
5373 if (htab == NULL)
5374 return FALSE;
5375
5376 htab->dot_syms = NULL;
5377 }
5378 return _bfd_elf_notice_as_needed (ibfd, info, act);
5379 }
5380
5381 /* If --just-symbols against a final linked binary, then assume we need
5382 toc adjusting stubs when calling functions defined there. */
5383
5384 static void
5385 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5386 {
5387 if ((sec->flags & SEC_CODE) != 0
5388 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5389 && is_ppc64_elf (sec->owner))
5390 {
5391 if (abiversion (sec->owner) >= 2
5392 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5393 sec->has_toc_reloc = 1;
5394 }
5395 _bfd_elf_link_just_syms (sec, info);
5396 }
5397
5398 static struct plt_entry **
5399 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5400 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5401 {
5402 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5403 struct plt_entry **local_plt;
5404 unsigned char *local_got_tls_masks;
5405
5406 if (local_got_ents == NULL)
5407 {
5408 bfd_size_type size = symtab_hdr->sh_info;
5409
5410 size *= (sizeof (*local_got_ents)
5411 + sizeof (*local_plt)
5412 + sizeof (*local_got_tls_masks));
5413 local_got_ents = bfd_zalloc (abfd, size);
5414 if (local_got_ents == NULL)
5415 return NULL;
5416 elf_local_got_ents (abfd) = local_got_ents;
5417 }
5418
5419 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5420 {
5421 struct got_entry *ent;
5422
5423 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5424 if (ent->addend == r_addend
5425 && ent->owner == abfd
5426 && ent->tls_type == tls_type)
5427 break;
5428 if (ent == NULL)
5429 {
5430 bfd_size_type amt = sizeof (*ent);
5431 ent = bfd_alloc (abfd, amt);
5432 if (ent == NULL)
5433 return FALSE;
5434 ent->next = local_got_ents[r_symndx];
5435 ent->addend = r_addend;
5436 ent->owner = abfd;
5437 ent->tls_type = tls_type;
5438 ent->is_indirect = FALSE;
5439 ent->got.refcount = 0;
5440 local_got_ents[r_symndx] = ent;
5441 }
5442 ent->got.refcount += 1;
5443 }
5444
5445 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5446 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5447 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5448
5449 return local_plt + r_symndx;
5450 }
5451
5452 static bfd_boolean
5453 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5454 {
5455 struct plt_entry *ent;
5456
5457 for (ent = *plist; ent != NULL; ent = ent->next)
5458 if (ent->addend == addend)
5459 break;
5460 if (ent == NULL)
5461 {
5462 bfd_size_type amt = sizeof (*ent);
5463 ent = bfd_alloc (abfd, amt);
5464 if (ent == NULL)
5465 return FALSE;
5466 ent->next = *plist;
5467 ent->addend = addend;
5468 ent->plt.refcount = 0;
5469 *plist = ent;
5470 }
5471 ent->plt.refcount += 1;
5472 return TRUE;
5473 }
5474
5475 static bfd_boolean
5476 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5477 {
5478 return (r_type == R_PPC64_REL24
5479 || r_type == R_PPC64_REL14
5480 || r_type == R_PPC64_REL14_BRTAKEN
5481 || r_type == R_PPC64_REL14_BRNTAKEN
5482 || r_type == R_PPC64_ADDR24
5483 || r_type == R_PPC64_ADDR14
5484 || r_type == R_PPC64_ADDR14_BRTAKEN
5485 || r_type == R_PPC64_ADDR14_BRNTAKEN
5486 || r_type == R_PPC64_PLTCALL);
5487 }
5488
5489 /* Relocs on inline plt call sequence insns prior to the call. */
5490
5491 static bfd_boolean
5492 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5493 {
5494 return (r_type == R_PPC64_PLT16_HA
5495 || r_type == R_PPC64_PLT16_HI
5496 || r_type == R_PPC64_PLT16_LO
5497 || r_type == R_PPC64_PLT16_LO_DS
5498 || r_type == R_PPC64_PLTSEQ);
5499 }
5500
5501 /* Look through the relocs for a section during the first phase, and
5502 calculate needed space in the global offset table, procedure
5503 linkage table, and dynamic reloc sections. */
5504
5505 static bfd_boolean
5506 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5507 asection *sec, const Elf_Internal_Rela *relocs)
5508 {
5509 struct ppc_link_hash_table *htab;
5510 Elf_Internal_Shdr *symtab_hdr;
5511 struct elf_link_hash_entry **sym_hashes;
5512 const Elf_Internal_Rela *rel;
5513 const Elf_Internal_Rela *rel_end;
5514 asection *sreloc;
5515 struct elf_link_hash_entry *tga, *dottga;
5516 bfd_boolean is_opd;
5517
5518 if (bfd_link_relocatable (info))
5519 return TRUE;
5520
5521 /* Don't do anything special with non-loaded, non-alloced sections.
5522 In particular, any relocs in such sections should not affect GOT
5523 and PLT reference counting (ie. we don't allow them to create GOT
5524 or PLT entries), there's no possibility or desire to optimize TLS
5525 relocs, and there's not much point in propagating relocs to shared
5526 libs that the dynamic linker won't relocate. */
5527 if ((sec->flags & SEC_ALLOC) == 0)
5528 return TRUE;
5529
5530 BFD_ASSERT (is_ppc64_elf (abfd));
5531
5532 htab = ppc_hash_table (info);
5533 if (htab == NULL)
5534 return FALSE;
5535
5536 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5537 FALSE, FALSE, TRUE);
5538 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5539 FALSE, FALSE, TRUE);
5540 symtab_hdr = &elf_symtab_hdr (abfd);
5541 sym_hashes = elf_sym_hashes (abfd);
5542 sreloc = NULL;
5543 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5544 rel_end = relocs + sec->reloc_count;
5545 for (rel = relocs; rel < rel_end; rel++)
5546 {
5547 unsigned long r_symndx;
5548 struct elf_link_hash_entry *h;
5549 enum elf_ppc64_reloc_type r_type;
5550 int tls_type;
5551 struct _ppc64_elf_section_data *ppc64_sec;
5552 struct plt_entry **ifunc, **plt_list;
5553
5554 r_symndx = ELF64_R_SYM (rel->r_info);
5555 if (r_symndx < symtab_hdr->sh_info)
5556 h = NULL;
5557 else
5558 {
5559 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5560 h = elf_follow_link (h);
5561
5562 if (h == htab->elf.hgot)
5563 sec->has_toc_reloc = 1;
5564 }
5565
5566 tls_type = 0;
5567 ifunc = NULL;
5568 if (h != NULL)
5569 {
5570 if (h->type == STT_GNU_IFUNC)
5571 {
5572 h->needs_plt = 1;
5573 ifunc = &h->plt.plist;
5574 }
5575 }
5576 else
5577 {
5578 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5579 abfd, r_symndx);
5580 if (isym == NULL)
5581 return FALSE;
5582
5583 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5584 {
5585 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5586 rel->r_addend,
5587 NON_GOT | PLT_IFUNC);
5588 if (ifunc == NULL)
5589 return FALSE;
5590 }
5591 }
5592
5593 r_type = ELF64_R_TYPE (rel->r_info);
5594 switch (r_type)
5595 {
5596 case R_PPC64_TLSGD:
5597 case R_PPC64_TLSLD:
5598 /* These special tls relocs tie a call to __tls_get_addr with
5599 its parameter symbol. */
5600 if (h != NULL)
5601 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5602 else
5603 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5604 rel->r_addend,
5605 NON_GOT | TLS_TLS | TLS_MARK))
5606 return FALSE;
5607 sec->has_tls_reloc = 1;
5608 break;
5609
5610 case R_PPC64_GOT_TLSLD16:
5611 case R_PPC64_GOT_TLSLD16_LO:
5612 case R_PPC64_GOT_TLSLD16_HI:
5613 case R_PPC64_GOT_TLSLD16_HA:
5614 tls_type = TLS_TLS | TLS_LD;
5615 goto dogottls;
5616
5617 case R_PPC64_GOT_TLSGD16:
5618 case R_PPC64_GOT_TLSGD16_LO:
5619 case R_PPC64_GOT_TLSGD16_HI:
5620 case R_PPC64_GOT_TLSGD16_HA:
5621 tls_type = TLS_TLS | TLS_GD;
5622 goto dogottls;
5623
5624 case R_PPC64_GOT_TPREL16_DS:
5625 case R_PPC64_GOT_TPREL16_LO_DS:
5626 case R_PPC64_GOT_TPREL16_HI:
5627 case R_PPC64_GOT_TPREL16_HA:
5628 if (bfd_link_dll (info))
5629 info->flags |= DF_STATIC_TLS;
5630 tls_type = TLS_TLS | TLS_TPREL;
5631 goto dogottls;
5632
5633 case R_PPC64_GOT_DTPREL16_DS:
5634 case R_PPC64_GOT_DTPREL16_LO_DS:
5635 case R_PPC64_GOT_DTPREL16_HI:
5636 case R_PPC64_GOT_DTPREL16_HA:
5637 tls_type = TLS_TLS | TLS_DTPREL;
5638 dogottls:
5639 sec->has_tls_reloc = 1;
5640 /* Fall through */
5641
5642 case R_PPC64_GOT16:
5643 case R_PPC64_GOT16_DS:
5644 case R_PPC64_GOT16_HA:
5645 case R_PPC64_GOT16_HI:
5646 case R_PPC64_GOT16_LO:
5647 case R_PPC64_GOT16_LO_DS:
5648 /* This symbol requires a global offset table entry. */
5649 sec->has_toc_reloc = 1;
5650 if (r_type == R_PPC64_GOT_TLSLD16
5651 || r_type == R_PPC64_GOT_TLSGD16
5652 || r_type == R_PPC64_GOT_TPREL16_DS
5653 || r_type == R_PPC64_GOT_DTPREL16_DS
5654 || r_type == R_PPC64_GOT16
5655 || r_type == R_PPC64_GOT16_DS)
5656 {
5657 htab->do_multi_toc = 1;
5658 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5659 }
5660
5661 if (ppc64_elf_tdata (abfd)->got == NULL
5662 && !create_got_section (abfd, info))
5663 return FALSE;
5664
5665 if (h != NULL)
5666 {
5667 struct ppc_link_hash_entry *eh;
5668 struct got_entry *ent;
5669
5670 eh = (struct ppc_link_hash_entry *) h;
5671 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5672 if (ent->addend == rel->r_addend
5673 && ent->owner == abfd
5674 && ent->tls_type == tls_type)
5675 break;
5676 if (ent == NULL)
5677 {
5678 bfd_size_type amt = sizeof (*ent);
5679 ent = bfd_alloc (abfd, amt);
5680 if (ent == NULL)
5681 return FALSE;
5682 ent->next = eh->elf.got.glist;
5683 ent->addend = rel->r_addend;
5684 ent->owner = abfd;
5685 ent->tls_type = tls_type;
5686 ent->is_indirect = FALSE;
5687 ent->got.refcount = 0;
5688 eh->elf.got.glist = ent;
5689 }
5690 ent->got.refcount += 1;
5691 eh->tls_mask |= tls_type;
5692 }
5693 else
5694 /* This is a global offset table entry for a local symbol. */
5695 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5696 rel->r_addend, tls_type))
5697 return FALSE;
5698
5699 /* We may also need a plt entry if the symbol turns out to be
5700 an ifunc. */
5701 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5702 {
5703 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5704 return FALSE;
5705 }
5706 break;
5707
5708 case R_PPC64_PLT16_HA:
5709 case R_PPC64_PLT16_HI:
5710 case R_PPC64_PLT16_LO:
5711 case R_PPC64_PLT16_LO_DS:
5712 case R_PPC64_PLT32:
5713 case R_PPC64_PLT64:
5714 /* This symbol requires a procedure linkage table entry. */
5715 plt_list = ifunc;
5716 if (h != NULL)
5717 {
5718 h->needs_plt = 1;
5719 if (h->root.root.string[0] == '.'
5720 && h->root.root.string[1] != '\0')
5721 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5722 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5723 plt_list = &h->plt.plist;
5724 }
5725 if (plt_list == NULL)
5726 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5727 rel->r_addend,
5728 NON_GOT | PLT_KEEP);
5729 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5730 return FALSE;
5731 break;
5732
5733 /* The following relocations don't need to propagate the
5734 relocation if linking a shared object since they are
5735 section relative. */
5736 case R_PPC64_SECTOFF:
5737 case R_PPC64_SECTOFF_LO:
5738 case R_PPC64_SECTOFF_HI:
5739 case R_PPC64_SECTOFF_HA:
5740 case R_PPC64_SECTOFF_DS:
5741 case R_PPC64_SECTOFF_LO_DS:
5742 case R_PPC64_DTPREL16:
5743 case R_PPC64_DTPREL16_LO:
5744 case R_PPC64_DTPREL16_HI:
5745 case R_PPC64_DTPREL16_HA:
5746 case R_PPC64_DTPREL16_DS:
5747 case R_PPC64_DTPREL16_LO_DS:
5748 case R_PPC64_DTPREL16_HIGH:
5749 case R_PPC64_DTPREL16_HIGHA:
5750 case R_PPC64_DTPREL16_HIGHER:
5751 case R_PPC64_DTPREL16_HIGHERA:
5752 case R_PPC64_DTPREL16_HIGHEST:
5753 case R_PPC64_DTPREL16_HIGHESTA:
5754 break;
5755
5756 /* Nor do these. */
5757 case R_PPC64_REL16:
5758 case R_PPC64_REL16_LO:
5759 case R_PPC64_REL16_HI:
5760 case R_PPC64_REL16_HA:
5761 case R_PPC64_REL16DX_HA:
5762 break;
5763
5764 /* Not supported as a dynamic relocation. */
5765 case R_PPC64_ADDR64_LOCAL:
5766 if (bfd_link_pic (info))
5767 {
5768 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5769 ppc_howto_init ();
5770 /* xgettext:c-format */
5771 info->callbacks->einfo (_("%H: %s reloc unsupported "
5772 "in shared libraries and PIEs\n"),
5773 abfd, sec, rel->r_offset,
5774 ppc64_elf_howto_table[r_type]->name);
5775 bfd_set_error (bfd_error_bad_value);
5776 return FALSE;
5777 }
5778 break;
5779
5780 case R_PPC64_TOC16:
5781 case R_PPC64_TOC16_DS:
5782 htab->do_multi_toc = 1;
5783 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5784 /* Fall through. */
5785 case R_PPC64_TOC16_LO:
5786 case R_PPC64_TOC16_HI:
5787 case R_PPC64_TOC16_HA:
5788 case R_PPC64_TOC16_LO_DS:
5789 sec->has_toc_reloc = 1;
5790 break;
5791
5792 /* Marker reloc. */
5793 case R_PPC64_ENTRY:
5794 break;
5795
5796 /* This relocation describes the C++ object vtable hierarchy.
5797 Reconstruct it for later use during GC. */
5798 case R_PPC64_GNU_VTINHERIT:
5799 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5800 return FALSE;
5801 break;
5802
5803 /* This relocation describes which C++ vtable entries are actually
5804 used. Record for later use during GC. */
5805 case R_PPC64_GNU_VTENTRY:
5806 BFD_ASSERT (h != NULL);
5807 if (h != NULL
5808 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5809 return FALSE;
5810 break;
5811
5812 case R_PPC64_REL14:
5813 case R_PPC64_REL14_BRTAKEN:
5814 case R_PPC64_REL14_BRNTAKEN:
5815 {
5816 asection *dest = NULL;
5817
5818 /* Heuristic: If jumping outside our section, chances are
5819 we are going to need a stub. */
5820 if (h != NULL)
5821 {
5822 /* If the sym is weak it may be overridden later, so
5823 don't assume we know where a weak sym lives. */
5824 if (h->root.type == bfd_link_hash_defined)
5825 dest = h->root.u.def.section;
5826 }
5827 else
5828 {
5829 Elf_Internal_Sym *isym;
5830
5831 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5832 abfd, r_symndx);
5833 if (isym == NULL)
5834 return FALSE;
5835
5836 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5837 }
5838
5839 if (dest != sec)
5840 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5841 }
5842 goto rel24;
5843
5844 case R_PPC64_PLTCALL:
5845 ppc64_elf_section_data (sec)->has_pltcall = 1;
5846 /* Fall through. */
5847
5848 case R_PPC64_REL24:
5849 rel24:
5850 plt_list = ifunc;
5851 if (h != NULL)
5852 {
5853 h->needs_plt = 1;
5854 if (h->root.root.string[0] == '.'
5855 && h->root.root.string[1] != '\0')
5856 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5857
5858 if (h == tga || h == dottga)
5859 {
5860 sec->has_tls_reloc = 1;
5861 if (rel != relocs
5862 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5863 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5864 /* We have a new-style __tls_get_addr call with
5865 a marker reloc. */
5866 ;
5867 else
5868 /* Mark this section as having an old-style call. */
5869 sec->has_tls_get_addr_call = 1;
5870 }
5871 plt_list = &h->plt.plist;
5872 }
5873
5874 /* We may need a .plt entry if the function this reloc
5875 refers to is in a shared lib. */
5876 if (plt_list
5877 && !update_plt_info (abfd, plt_list, rel->r_addend))
5878 return FALSE;
5879 break;
5880
5881 case R_PPC64_ADDR14:
5882 case R_PPC64_ADDR14_BRNTAKEN:
5883 case R_PPC64_ADDR14_BRTAKEN:
5884 case R_PPC64_ADDR24:
5885 goto dodyn;
5886
5887 case R_PPC64_TPREL64:
5888 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5889 if (bfd_link_dll (info))
5890 info->flags |= DF_STATIC_TLS;
5891 goto dotlstoc;
5892
5893 case R_PPC64_DTPMOD64:
5894 if (rel + 1 < rel_end
5895 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5896 && rel[1].r_offset == rel->r_offset + 8)
5897 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5898 else
5899 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5900 goto dotlstoc;
5901
5902 case R_PPC64_DTPREL64:
5903 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5904 if (rel != relocs
5905 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5906 && rel[-1].r_offset == rel->r_offset - 8)
5907 /* This is the second reloc of a dtpmod, dtprel pair.
5908 Don't mark with TLS_DTPREL. */
5909 goto dodyn;
5910
5911 dotlstoc:
5912 sec->has_tls_reloc = 1;
5913 if (h != NULL)
5914 {
5915 struct ppc_link_hash_entry *eh;
5916 eh = (struct ppc_link_hash_entry *) h;
5917 eh->tls_mask |= tls_type;
5918 }
5919 else
5920 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5921 rel->r_addend, tls_type))
5922 return FALSE;
5923
5924 ppc64_sec = ppc64_elf_section_data (sec);
5925 if (ppc64_sec->sec_type != sec_toc)
5926 {
5927 bfd_size_type amt;
5928
5929 /* One extra to simplify get_tls_mask. */
5930 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5931 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5932 if (ppc64_sec->u.toc.symndx == NULL)
5933 return FALSE;
5934 amt = sec->size * sizeof (bfd_vma) / 8;
5935 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5936 if (ppc64_sec->u.toc.add == NULL)
5937 return FALSE;
5938 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5939 ppc64_sec->sec_type = sec_toc;
5940 }
5941 BFD_ASSERT (rel->r_offset % 8 == 0);
5942 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5943 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5944
5945 /* Mark the second slot of a GD or LD entry.
5946 -1 to indicate GD and -2 to indicate LD. */
5947 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5948 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5949 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5950 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5951 goto dodyn;
5952
5953 case R_PPC64_TPREL16:
5954 case R_PPC64_TPREL16_LO:
5955 case R_PPC64_TPREL16_HI:
5956 case R_PPC64_TPREL16_HA:
5957 case R_PPC64_TPREL16_DS:
5958 case R_PPC64_TPREL16_LO_DS:
5959 case R_PPC64_TPREL16_HIGH:
5960 case R_PPC64_TPREL16_HIGHA:
5961 case R_PPC64_TPREL16_HIGHER:
5962 case R_PPC64_TPREL16_HIGHERA:
5963 case R_PPC64_TPREL16_HIGHEST:
5964 case R_PPC64_TPREL16_HIGHESTA:
5965 if (bfd_link_dll (info))
5966 info->flags |= DF_STATIC_TLS;
5967 goto dodyn;
5968
5969 case R_PPC64_ADDR64:
5970 if (is_opd
5971 && rel + 1 < rel_end
5972 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5973 {
5974 if (h != NULL)
5975 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5976 }
5977 /* Fall through. */
5978
5979 case R_PPC64_ADDR16:
5980 case R_PPC64_ADDR16_DS:
5981 case R_PPC64_ADDR16_HA:
5982 case R_PPC64_ADDR16_HI:
5983 case R_PPC64_ADDR16_HIGH:
5984 case R_PPC64_ADDR16_HIGHA:
5985 case R_PPC64_ADDR16_HIGHER:
5986 case R_PPC64_ADDR16_HIGHERA:
5987 case R_PPC64_ADDR16_HIGHEST:
5988 case R_PPC64_ADDR16_HIGHESTA:
5989 case R_PPC64_ADDR16_LO:
5990 case R_PPC64_ADDR16_LO_DS:
5991 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5992 && rel->r_addend == 0)
5993 {
5994 /* We may need a .plt entry if this reloc refers to a
5995 function in a shared lib. */
5996 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5997 return FALSE;
5998 h->pointer_equality_needed = 1;
5999 }
6000 /* Fall through. */
6001
6002 case R_PPC64_REL30:
6003 case R_PPC64_REL32:
6004 case R_PPC64_REL64:
6005 case R_PPC64_ADDR32:
6006 case R_PPC64_UADDR16:
6007 case R_PPC64_UADDR32:
6008 case R_PPC64_UADDR64:
6009 case R_PPC64_TOC:
6010 if (h != NULL && !bfd_link_pic (info))
6011 /* We may need a copy reloc. */
6012 h->non_got_ref = 1;
6013
6014 /* Don't propagate .opd relocs. */
6015 if (NO_OPD_RELOCS && is_opd)
6016 break;
6017
6018 /* If we are creating a shared library, and this is a reloc
6019 against a global symbol, or a non PC relative reloc
6020 against a local symbol, then we need to copy the reloc
6021 into the shared library. However, if we are linking with
6022 -Bsymbolic, we do not need to copy a reloc against a
6023 global symbol which is defined in an object we are
6024 including in the link (i.e., DEF_REGULAR is set). At
6025 this point we have not seen all the input files, so it is
6026 possible that DEF_REGULAR is not set now but will be set
6027 later (it is never cleared). In case of a weak definition,
6028 DEF_REGULAR may be cleared later by a strong definition in
6029 a shared library. We account for that possibility below by
6030 storing information in the dyn_relocs field of the hash
6031 table entry. A similar situation occurs when creating
6032 shared libraries and symbol visibility changes render the
6033 symbol local.
6034
6035 If on the other hand, we are creating an executable, we
6036 may need to keep relocations for symbols satisfied by a
6037 dynamic library if we manage to avoid copy relocs for the
6038 symbol. */
6039 dodyn:
6040 if ((bfd_link_pic (info)
6041 && (must_be_dyn_reloc (info, r_type)
6042 || (h != NULL
6043 && (!SYMBOLIC_BIND (info, h)
6044 || h->root.type == bfd_link_hash_defweak
6045 || !h->def_regular))))
6046 || (ELIMINATE_COPY_RELOCS
6047 && !bfd_link_pic (info)
6048 && h != NULL
6049 && (h->root.type == bfd_link_hash_defweak
6050 || !h->def_regular))
6051 || (!bfd_link_pic (info)
6052 && ifunc != NULL))
6053 {
6054 /* We must copy these reloc types into the output file.
6055 Create a reloc section in dynobj and make room for
6056 this reloc. */
6057 if (sreloc == NULL)
6058 {
6059 sreloc = _bfd_elf_make_dynamic_reloc_section
6060 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6061
6062 if (sreloc == NULL)
6063 return FALSE;
6064 }
6065
6066 /* If this is a global symbol, we count the number of
6067 relocations we need for this symbol. */
6068 if (h != NULL)
6069 {
6070 struct elf_dyn_relocs *p;
6071 struct elf_dyn_relocs **head;
6072
6073 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6074 p = *head;
6075 if (p == NULL || p->sec != sec)
6076 {
6077 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6078 if (p == NULL)
6079 return FALSE;
6080 p->next = *head;
6081 *head = p;
6082 p->sec = sec;
6083 p->count = 0;
6084 p->pc_count = 0;
6085 }
6086 p->count += 1;
6087 if (!must_be_dyn_reloc (info, r_type))
6088 p->pc_count += 1;
6089 }
6090 else
6091 {
6092 /* Track dynamic relocs needed for local syms too.
6093 We really need local syms available to do this
6094 easily. Oh well. */
6095 struct ppc_dyn_relocs *p;
6096 struct ppc_dyn_relocs **head;
6097 bfd_boolean is_ifunc;
6098 asection *s;
6099 void *vpp;
6100 Elf_Internal_Sym *isym;
6101
6102 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6103 abfd, r_symndx);
6104 if (isym == NULL)
6105 return FALSE;
6106
6107 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6108 if (s == NULL)
6109 s = sec;
6110
6111 vpp = &elf_section_data (s)->local_dynrel;
6112 head = (struct ppc_dyn_relocs **) vpp;
6113 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6114 p = *head;
6115 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6116 p = p->next;
6117 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6118 {
6119 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6120 if (p == NULL)
6121 return FALSE;
6122 p->next = *head;
6123 *head = p;
6124 p->sec = sec;
6125 p->ifunc = is_ifunc;
6126 p->count = 0;
6127 }
6128 p->count += 1;
6129 }
6130 }
6131 break;
6132
6133 default:
6134 break;
6135 }
6136 }
6137
6138 return TRUE;
6139 }
6140
6141 /* Merge backend specific data from an object file to the output
6142 object file when linking. */
6143
6144 static bfd_boolean
6145 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6146 {
6147 bfd *obfd = info->output_bfd;
6148 unsigned long iflags, oflags;
6149
6150 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6151 return TRUE;
6152
6153 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6154 return TRUE;
6155
6156 if (!_bfd_generic_verify_endian_match (ibfd, info))
6157 return FALSE;
6158
6159 iflags = elf_elfheader (ibfd)->e_flags;
6160 oflags = elf_elfheader (obfd)->e_flags;
6161
6162 if (iflags & ~EF_PPC64_ABI)
6163 {
6164 _bfd_error_handler
6165 /* xgettext:c-format */
6166 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6167 bfd_set_error (bfd_error_bad_value);
6168 return FALSE;
6169 }
6170 else if (iflags != oflags && iflags != 0)
6171 {
6172 _bfd_error_handler
6173 /* xgettext:c-format */
6174 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6175 ibfd, iflags, oflags);
6176 bfd_set_error (bfd_error_bad_value);
6177 return FALSE;
6178 }
6179
6180 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6181
6182 /* Merge Tag_compatibility attributes and any common GNU ones. */
6183 return _bfd_elf_merge_object_attributes (ibfd, info);
6184 }
6185
6186 static bfd_boolean
6187 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6188 {
6189 /* Print normal ELF private data. */
6190 _bfd_elf_print_private_bfd_data (abfd, ptr);
6191
6192 if (elf_elfheader (abfd)->e_flags != 0)
6193 {
6194 FILE *file = ptr;
6195
6196 fprintf (file, _("private flags = 0x%lx:"),
6197 elf_elfheader (abfd)->e_flags);
6198
6199 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6200 fprintf (file, _(" [abiv%ld]"),
6201 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6202 fputc ('\n', file);
6203 }
6204
6205 return TRUE;
6206 }
6207
6208 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6209 of the code entry point, and its section, which must be in the same
6210 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6211
6212 static bfd_vma
6213 opd_entry_value (asection *opd_sec,
6214 bfd_vma offset,
6215 asection **code_sec,
6216 bfd_vma *code_off,
6217 bfd_boolean in_code_sec)
6218 {
6219 bfd *opd_bfd = opd_sec->owner;
6220 Elf_Internal_Rela *relocs;
6221 Elf_Internal_Rela *lo, *hi, *look;
6222 bfd_vma val;
6223
6224 /* No relocs implies we are linking a --just-symbols object, or looking
6225 at a final linked executable with addr2line or somesuch. */
6226 if (opd_sec->reloc_count == 0)
6227 {
6228 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6229
6230 if (contents == NULL)
6231 {
6232 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6233 return (bfd_vma) -1;
6234 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6235 }
6236
6237 /* PR 17512: file: 64b9dfbb. */
6238 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6239 return (bfd_vma) -1;
6240
6241 val = bfd_get_64 (opd_bfd, contents + offset);
6242 if (code_sec != NULL)
6243 {
6244 asection *sec, *likely = NULL;
6245
6246 if (in_code_sec)
6247 {
6248 sec = *code_sec;
6249 if (sec->vma <= val
6250 && val < sec->vma + sec->size)
6251 likely = sec;
6252 else
6253 val = -1;
6254 }
6255 else
6256 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6257 if (sec->vma <= val
6258 && (sec->flags & SEC_LOAD) != 0
6259 && (sec->flags & SEC_ALLOC) != 0)
6260 likely = sec;
6261 if (likely != NULL)
6262 {
6263 *code_sec = likely;
6264 if (code_off != NULL)
6265 *code_off = val - likely->vma;
6266 }
6267 }
6268 return val;
6269 }
6270
6271 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6272
6273 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6274 if (relocs == NULL)
6275 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6276 /* PR 17512: file: df8e1fd6. */
6277 if (relocs == NULL)
6278 return (bfd_vma) -1;
6279
6280 /* Go find the opd reloc at the sym address. */
6281 lo = relocs;
6282 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6283 val = (bfd_vma) -1;
6284 while (lo < hi)
6285 {
6286 look = lo + (hi - lo) / 2;
6287 if (look->r_offset < offset)
6288 lo = look + 1;
6289 else if (look->r_offset > offset)
6290 hi = look;
6291 else
6292 {
6293 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6294
6295 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6296 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6297 {
6298 unsigned long symndx = ELF64_R_SYM (look->r_info);
6299 asection *sec = NULL;
6300
6301 if (symndx >= symtab_hdr->sh_info
6302 && elf_sym_hashes (opd_bfd) != NULL)
6303 {
6304 struct elf_link_hash_entry **sym_hashes;
6305 struct elf_link_hash_entry *rh;
6306
6307 sym_hashes = elf_sym_hashes (opd_bfd);
6308 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6309 if (rh != NULL)
6310 {
6311 rh = elf_follow_link (rh);
6312 if (rh->root.type != bfd_link_hash_defined
6313 && rh->root.type != bfd_link_hash_defweak)
6314 break;
6315 if (rh->root.u.def.section->owner == opd_bfd)
6316 {
6317 val = rh->root.u.def.value;
6318 sec = rh->root.u.def.section;
6319 }
6320 }
6321 }
6322
6323 if (sec == NULL)
6324 {
6325 Elf_Internal_Sym *sym;
6326
6327 if (symndx < symtab_hdr->sh_info)
6328 {
6329 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6330 if (sym == NULL)
6331 {
6332 size_t symcnt = symtab_hdr->sh_info;
6333 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6334 symcnt, 0,
6335 NULL, NULL, NULL);
6336 if (sym == NULL)
6337 break;
6338 symtab_hdr->contents = (bfd_byte *) sym;
6339 }
6340 sym += symndx;
6341 }
6342 else
6343 {
6344 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6345 1, symndx,
6346 NULL, NULL, NULL);
6347 if (sym == NULL)
6348 break;
6349 }
6350 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6351 if (sec == NULL)
6352 break;
6353 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6354 val = sym->st_value;
6355 }
6356
6357 val += look->r_addend;
6358 if (code_off != NULL)
6359 *code_off = val;
6360 if (code_sec != NULL)
6361 {
6362 if (in_code_sec && *code_sec != sec)
6363 return -1;
6364 else
6365 *code_sec = sec;
6366 }
6367 if (sec->output_section != NULL)
6368 val += sec->output_section->vma + sec->output_offset;
6369 }
6370 break;
6371 }
6372 }
6373
6374 return val;
6375 }
6376
6377 /* If the ELF symbol SYM might be a function in SEC, return the
6378 function size and set *CODE_OFF to the function's entry point,
6379 otherwise return zero. */
6380
6381 static bfd_size_type
6382 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6383 bfd_vma *code_off)
6384 {
6385 bfd_size_type size;
6386
6387 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6388 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6389 return 0;
6390
6391 size = 0;
6392 if (!(sym->flags & BSF_SYNTHETIC))
6393 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6394
6395 if (strcmp (sym->section->name, ".opd") == 0)
6396 {
6397 struct _opd_sec_data *opd = get_opd_info (sym->section);
6398 bfd_vma symval = sym->value;
6399
6400 if (opd != NULL
6401 && opd->adjust != NULL
6402 && elf_section_data (sym->section)->relocs != NULL)
6403 {
6404 /* opd_entry_value will use cached relocs that have been
6405 adjusted, but with raw symbols. That means both local
6406 and global symbols need adjusting. */
6407 long adjust = opd->adjust[OPD_NDX (symval)];
6408 if (adjust == -1)
6409 return 0;
6410 symval += adjust;
6411 }
6412
6413 if (opd_entry_value (sym->section, symval,
6414 &sec, code_off, TRUE) == (bfd_vma) -1)
6415 return 0;
6416 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6417 symbol. This size has nothing to do with the code size of the
6418 function, which is what we're supposed to return, but the
6419 code size isn't available without looking up the dot-sym.
6420 However, doing that would be a waste of time particularly
6421 since elf_find_function will look at the dot-sym anyway.
6422 Now, elf_find_function will keep the largest size of any
6423 function sym found at the code address of interest, so return
6424 1 here to avoid it incorrectly caching a larger function size
6425 for a small function. This does mean we return the wrong
6426 size for a new-ABI function of size 24, but all that does is
6427 disable caching for such functions. */
6428 if (size == 24)
6429 size = 1;
6430 }
6431 else
6432 {
6433 if (sym->section != sec)
6434 return 0;
6435 *code_off = sym->value;
6436 }
6437 if (size == 0)
6438 size = 1;
6439 return size;
6440 }
6441
6442 /* Return true if symbol is a strong function defined in an ELFv2
6443 object with st_other localentry bits of zero, ie. its local entry
6444 point coincides with its global entry point. */
6445
6446 static bfd_boolean
6447 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6448 {
6449 return (h != NULL
6450 && h->type == STT_FUNC
6451 && h->root.type == bfd_link_hash_defined
6452 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6453 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6454 && is_ppc64_elf (h->root.u.def.section->owner)
6455 && abiversion (h->root.u.def.section->owner) >= 2);
6456 }
6457
6458 /* Return true if symbol is defined in a regular object file. */
6459
6460 static bfd_boolean
6461 is_static_defined (struct elf_link_hash_entry *h)
6462 {
6463 return ((h->root.type == bfd_link_hash_defined
6464 || h->root.type == bfd_link_hash_defweak)
6465 && h->root.u.def.section != NULL
6466 && h->root.u.def.section->output_section != NULL);
6467 }
6468
6469 /* If FDH is a function descriptor symbol, return the associated code
6470 entry symbol if it is defined. Return NULL otherwise. */
6471
6472 static struct ppc_link_hash_entry *
6473 defined_code_entry (struct ppc_link_hash_entry *fdh)
6474 {
6475 if (fdh->is_func_descriptor)
6476 {
6477 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6478 if (fh->elf.root.type == bfd_link_hash_defined
6479 || fh->elf.root.type == bfd_link_hash_defweak)
6480 return fh;
6481 }
6482 return NULL;
6483 }
6484
6485 /* If FH is a function code entry symbol, return the associated
6486 function descriptor symbol if it is defined. Return NULL otherwise. */
6487
6488 static struct ppc_link_hash_entry *
6489 defined_func_desc (struct ppc_link_hash_entry *fh)
6490 {
6491 if (fh->oh != NULL
6492 && fh->oh->is_func_descriptor)
6493 {
6494 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6495 if (fdh->elf.root.type == bfd_link_hash_defined
6496 || fdh->elf.root.type == bfd_link_hash_defweak)
6497 return fdh;
6498 }
6499 return NULL;
6500 }
6501
6502 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6503
6504 /* Garbage collect sections, after first dealing with dot-symbols. */
6505
6506 static bfd_boolean
6507 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6508 {
6509 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6510
6511 if (htab != NULL && htab->need_func_desc_adj)
6512 {
6513 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6514 htab->need_func_desc_adj = 0;
6515 }
6516 return bfd_elf_gc_sections (abfd, info);
6517 }
6518
6519 /* Mark all our entry sym sections, both opd and code section. */
6520
6521 static void
6522 ppc64_elf_gc_keep (struct bfd_link_info *info)
6523 {
6524 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6525 struct bfd_sym_chain *sym;
6526
6527 if (htab == NULL)
6528 return;
6529
6530 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6531 {
6532 struct ppc_link_hash_entry *eh, *fh;
6533 asection *sec;
6534
6535 eh = (struct ppc_link_hash_entry *)
6536 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6537 if (eh == NULL)
6538 continue;
6539 if (eh->elf.root.type != bfd_link_hash_defined
6540 && eh->elf.root.type != bfd_link_hash_defweak)
6541 continue;
6542
6543 fh = defined_code_entry (eh);
6544 if (fh != NULL)
6545 {
6546 sec = fh->elf.root.u.def.section;
6547 sec->flags |= SEC_KEEP;
6548 }
6549 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6550 && opd_entry_value (eh->elf.root.u.def.section,
6551 eh->elf.root.u.def.value,
6552 &sec, NULL, FALSE) != (bfd_vma) -1)
6553 sec->flags |= SEC_KEEP;
6554
6555 sec = eh->elf.root.u.def.section;
6556 sec->flags |= SEC_KEEP;
6557 }
6558 }
6559
6560 /* Mark sections containing dynamically referenced symbols. When
6561 building shared libraries, we must assume that any visible symbol is
6562 referenced. */
6563
6564 static bfd_boolean
6565 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6566 {
6567 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6568 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6569 struct ppc_link_hash_entry *fdh;
6570 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6571
6572 /* Dynamic linking info is on the func descriptor sym. */
6573 fdh = defined_func_desc (eh);
6574 if (fdh != NULL)
6575 eh = fdh;
6576
6577 if ((eh->elf.root.type == bfd_link_hash_defined
6578 || eh->elf.root.type == bfd_link_hash_defweak)
6579 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6580 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6581 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6582 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6583 && (!bfd_link_executable (info)
6584 || info->gc_keep_exported
6585 || info->export_dynamic
6586 || (eh->elf.dynamic
6587 && d != NULL
6588 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6589 && (eh->elf.versioned >= versioned
6590 || !bfd_hide_sym_by_version (info->version_info,
6591 eh->elf.root.root.string)))))
6592 {
6593 asection *code_sec;
6594 struct ppc_link_hash_entry *fh;
6595
6596 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6597
6598 /* Function descriptor syms cause the associated
6599 function code sym section to be marked. */
6600 fh = defined_code_entry (eh);
6601 if (fh != NULL)
6602 {
6603 code_sec = fh->elf.root.u.def.section;
6604 code_sec->flags |= SEC_KEEP;
6605 }
6606 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6607 && opd_entry_value (eh->elf.root.u.def.section,
6608 eh->elf.root.u.def.value,
6609 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6610 code_sec->flags |= SEC_KEEP;
6611 }
6612
6613 return TRUE;
6614 }
6615
6616 /* Return the section that should be marked against GC for a given
6617 relocation. */
6618
6619 static asection *
6620 ppc64_elf_gc_mark_hook (asection *sec,
6621 struct bfd_link_info *info,
6622 Elf_Internal_Rela *rel,
6623 struct elf_link_hash_entry *h,
6624 Elf_Internal_Sym *sym)
6625 {
6626 asection *rsec;
6627
6628 /* Syms return NULL if we're marking .opd, so we avoid marking all
6629 function sections, as all functions are referenced in .opd. */
6630 rsec = NULL;
6631 if (get_opd_info (sec) != NULL)
6632 return rsec;
6633
6634 if (h != NULL)
6635 {
6636 enum elf_ppc64_reloc_type r_type;
6637 struct ppc_link_hash_entry *eh, *fh, *fdh;
6638
6639 r_type = ELF64_R_TYPE (rel->r_info);
6640 switch (r_type)
6641 {
6642 case R_PPC64_GNU_VTINHERIT:
6643 case R_PPC64_GNU_VTENTRY:
6644 break;
6645
6646 default:
6647 switch (h->root.type)
6648 {
6649 case bfd_link_hash_defined:
6650 case bfd_link_hash_defweak:
6651 eh = (struct ppc_link_hash_entry *) h;
6652 fdh = defined_func_desc (eh);
6653 if (fdh != NULL)
6654 {
6655 /* -mcall-aixdesc code references the dot-symbol on
6656 a call reloc. Mark the function descriptor too
6657 against garbage collection. */
6658 fdh->elf.mark = 1;
6659 if (fdh->elf.is_weakalias)
6660 weakdef (&fdh->elf)->mark = 1;
6661 eh = fdh;
6662 }
6663
6664 /* Function descriptor syms cause the associated
6665 function code sym section to be marked. */
6666 fh = defined_code_entry (eh);
6667 if (fh != NULL)
6668 {
6669 /* They also mark their opd section. */
6670 eh->elf.root.u.def.section->gc_mark = 1;
6671
6672 rsec = fh->elf.root.u.def.section;
6673 }
6674 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6675 && opd_entry_value (eh->elf.root.u.def.section,
6676 eh->elf.root.u.def.value,
6677 &rsec, NULL, FALSE) != (bfd_vma) -1)
6678 eh->elf.root.u.def.section->gc_mark = 1;
6679 else
6680 rsec = h->root.u.def.section;
6681 break;
6682
6683 case bfd_link_hash_common:
6684 rsec = h->root.u.c.p->section;
6685 break;
6686
6687 default:
6688 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6689 }
6690 }
6691 }
6692 else
6693 {
6694 struct _opd_sec_data *opd;
6695
6696 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6697 opd = get_opd_info (rsec);
6698 if (opd != NULL && opd->func_sec != NULL)
6699 {
6700 rsec->gc_mark = 1;
6701
6702 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6703 }
6704 }
6705
6706 return rsec;
6707 }
6708
6709 /* The maximum size of .sfpr. */
6710 #define SFPR_MAX (218*4)
6711
6712 struct sfpr_def_parms
6713 {
6714 const char name[12];
6715 unsigned char lo, hi;
6716 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6717 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6718 };
6719
6720 /* Auto-generate _save*, _rest* functions in .sfpr.
6721 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6722 instead. */
6723
6724 static bfd_boolean
6725 sfpr_define (struct bfd_link_info *info,
6726 const struct sfpr_def_parms *parm,
6727 asection *stub_sec)
6728 {
6729 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6730 unsigned int i;
6731 size_t len = strlen (parm->name);
6732 bfd_boolean writing = FALSE;
6733 char sym[16];
6734
6735 if (htab == NULL)
6736 return FALSE;
6737
6738 memcpy (sym, parm->name, len);
6739 sym[len + 2] = 0;
6740
6741 for (i = parm->lo; i <= parm->hi; i++)
6742 {
6743 struct ppc_link_hash_entry *h;
6744
6745 sym[len + 0] = i / 10 + '0';
6746 sym[len + 1] = i % 10 + '0';
6747 h = (struct ppc_link_hash_entry *)
6748 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6749 if (stub_sec != NULL)
6750 {
6751 if (h != NULL
6752 && h->elf.root.type == bfd_link_hash_defined
6753 && h->elf.root.u.def.section == htab->sfpr)
6754 {
6755 struct elf_link_hash_entry *s;
6756 char buf[32];
6757 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6758 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6759 if (s == NULL)
6760 return FALSE;
6761 if (s->root.type == bfd_link_hash_new
6762 || (s->root.type = bfd_link_hash_defined
6763 && s->root.u.def.section == stub_sec))
6764 {
6765 s->root.type = bfd_link_hash_defined;
6766 s->root.u.def.section = stub_sec;
6767 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6768 + h->elf.root.u.def.value);
6769 s->ref_regular = 1;
6770 s->def_regular = 1;
6771 s->ref_regular_nonweak = 1;
6772 s->forced_local = 1;
6773 s->non_elf = 0;
6774 s->root.linker_def = 1;
6775 }
6776 }
6777 continue;
6778 }
6779 if (h != NULL)
6780 {
6781 h->save_res = 1;
6782 if (!h->elf.def_regular)
6783 {
6784 h->elf.root.type = bfd_link_hash_defined;
6785 h->elf.root.u.def.section = htab->sfpr;
6786 h->elf.root.u.def.value = htab->sfpr->size;
6787 h->elf.type = STT_FUNC;
6788 h->elf.def_regular = 1;
6789 h->elf.non_elf = 0;
6790 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6791 writing = TRUE;
6792 if (htab->sfpr->contents == NULL)
6793 {
6794 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6795 if (htab->sfpr->contents == NULL)
6796 return FALSE;
6797 }
6798 }
6799 }
6800 if (writing)
6801 {
6802 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6803 if (i != parm->hi)
6804 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6805 else
6806 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6807 htab->sfpr->size = p - htab->sfpr->contents;
6808 }
6809 }
6810
6811 return TRUE;
6812 }
6813
6814 static bfd_byte *
6815 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6816 {
6817 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6818 return p + 4;
6819 }
6820
6821 static bfd_byte *
6822 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6823 {
6824 p = savegpr0 (abfd, p, r);
6825 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6826 p = p + 4;
6827 bfd_put_32 (abfd, BLR, p);
6828 return p + 4;
6829 }
6830
6831 static bfd_byte *
6832 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6833 {
6834 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6835 return p + 4;
6836 }
6837
6838 static bfd_byte *
6839 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6840 {
6841 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6842 p = p + 4;
6843 p = restgpr0 (abfd, p, r);
6844 bfd_put_32 (abfd, MTLR_R0, p);
6845 p = p + 4;
6846 if (r == 29)
6847 {
6848 p = restgpr0 (abfd, p, 30);
6849 p = restgpr0 (abfd, p, 31);
6850 }
6851 bfd_put_32 (abfd, BLR, p);
6852 return p + 4;
6853 }
6854
6855 static bfd_byte *
6856 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6857 {
6858 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6859 return p + 4;
6860 }
6861
6862 static bfd_byte *
6863 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6864 {
6865 p = savegpr1 (abfd, p, r);
6866 bfd_put_32 (abfd, BLR, p);
6867 return p + 4;
6868 }
6869
6870 static bfd_byte *
6871 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6872 {
6873 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6874 return p + 4;
6875 }
6876
6877 static bfd_byte *
6878 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6879 {
6880 p = restgpr1 (abfd, p, r);
6881 bfd_put_32 (abfd, BLR, p);
6882 return p + 4;
6883 }
6884
6885 static bfd_byte *
6886 savefpr (bfd *abfd, bfd_byte *p, int r)
6887 {
6888 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6889 return p + 4;
6890 }
6891
6892 static bfd_byte *
6893 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6894 {
6895 p = savefpr (abfd, p, r);
6896 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6897 p = p + 4;
6898 bfd_put_32 (abfd, BLR, p);
6899 return p + 4;
6900 }
6901
6902 static bfd_byte *
6903 restfpr (bfd *abfd, bfd_byte *p, int r)
6904 {
6905 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6906 return p + 4;
6907 }
6908
6909 static bfd_byte *
6910 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6911 {
6912 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6913 p = p + 4;
6914 p = restfpr (abfd, p, r);
6915 bfd_put_32 (abfd, MTLR_R0, p);
6916 p = p + 4;
6917 if (r == 29)
6918 {
6919 p = restfpr (abfd, p, 30);
6920 p = restfpr (abfd, p, 31);
6921 }
6922 bfd_put_32 (abfd, BLR, p);
6923 return p + 4;
6924 }
6925
6926 static bfd_byte *
6927 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6928 {
6929 p = savefpr (abfd, p, r);
6930 bfd_put_32 (abfd, BLR, p);
6931 return p + 4;
6932 }
6933
6934 static bfd_byte *
6935 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6936 {
6937 p = restfpr (abfd, p, r);
6938 bfd_put_32 (abfd, BLR, p);
6939 return p + 4;
6940 }
6941
6942 static bfd_byte *
6943 savevr (bfd *abfd, bfd_byte *p, int r)
6944 {
6945 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6946 p = p + 4;
6947 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6948 return p + 4;
6949 }
6950
6951 static bfd_byte *
6952 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6953 {
6954 p = savevr (abfd, p, r);
6955 bfd_put_32 (abfd, BLR, p);
6956 return p + 4;
6957 }
6958
6959 static bfd_byte *
6960 restvr (bfd *abfd, bfd_byte *p, int r)
6961 {
6962 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6963 p = p + 4;
6964 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6965 return p + 4;
6966 }
6967
6968 static bfd_byte *
6969 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6970 {
6971 p = restvr (abfd, p, r);
6972 bfd_put_32 (abfd, BLR, p);
6973 return p + 4;
6974 }
6975
6976 /* Called via elf_link_hash_traverse to transfer dynamic linking
6977 information on function code symbol entries to their corresponding
6978 function descriptor symbol entries. */
6979
6980 static bfd_boolean
6981 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6982 {
6983 struct bfd_link_info *info;
6984 struct ppc_link_hash_table *htab;
6985 struct ppc_link_hash_entry *fh;
6986 struct ppc_link_hash_entry *fdh;
6987 bfd_boolean force_local;
6988
6989 fh = (struct ppc_link_hash_entry *) h;
6990 if (fh->elf.root.type == bfd_link_hash_indirect)
6991 return TRUE;
6992
6993 if (!fh->is_func)
6994 return TRUE;
6995
6996 if (fh->elf.root.root.string[0] != '.'
6997 || fh->elf.root.root.string[1] == '\0')
6998 return TRUE;
6999
7000 info = inf;
7001 htab = ppc_hash_table (info);
7002 if (htab == NULL)
7003 return FALSE;
7004
7005 /* Find the corresponding function descriptor symbol. */
7006 fdh = lookup_fdh (fh, htab);
7007
7008 /* Resolve undefined references to dot-symbols as the value
7009 in the function descriptor, if we have one in a regular object.
7010 This is to satisfy cases like ".quad .foo". Calls to functions
7011 in dynamic objects are handled elsewhere. */
7012 if ((fh->elf.root.type == bfd_link_hash_undefined
7013 || fh->elf.root.type == bfd_link_hash_undefweak)
7014 && (fdh->elf.root.type == bfd_link_hash_defined
7015 || fdh->elf.root.type == bfd_link_hash_defweak)
7016 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7017 && opd_entry_value (fdh->elf.root.u.def.section,
7018 fdh->elf.root.u.def.value,
7019 &fh->elf.root.u.def.section,
7020 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7021 {
7022 fh->elf.root.type = fdh->elf.root.type;
7023 fh->elf.forced_local = 1;
7024 fh->elf.def_regular = fdh->elf.def_regular;
7025 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7026 }
7027
7028 if (!fh->elf.dynamic)
7029 {
7030 struct plt_entry *ent;
7031
7032 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7033 if (ent->plt.refcount > 0)
7034 break;
7035 if (ent == NULL)
7036 return TRUE;
7037 }
7038
7039 /* Create a descriptor as undefined if necessary. */
7040 if (fdh == NULL
7041 && !bfd_link_executable (info)
7042 && (fh->elf.root.type == bfd_link_hash_undefined
7043 || fh->elf.root.type == bfd_link_hash_undefweak))
7044 {
7045 fdh = make_fdh (info, fh);
7046 if (fdh == NULL)
7047 return FALSE;
7048 }
7049
7050 /* We can't support overriding of symbols on a fake descriptor. */
7051 if (fdh != NULL
7052 && fdh->fake
7053 && (fh->elf.root.type == bfd_link_hash_defined
7054 || fh->elf.root.type == bfd_link_hash_defweak))
7055 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7056
7057 /* Transfer dynamic linking information to the function descriptor. */
7058 if (fdh != NULL)
7059 {
7060 fdh->elf.ref_regular |= fh->elf.ref_regular;
7061 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7062 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7063 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7064 fdh->elf.dynamic |= fh->elf.dynamic;
7065 fdh->elf.needs_plt |= (fh->elf.needs_plt
7066 || fh->elf.type == STT_FUNC
7067 || fh->elf.type == STT_GNU_IFUNC);
7068 move_plt_plist (fh, fdh);
7069
7070 if (!fdh->elf.forced_local
7071 && fh->elf.dynindx != -1)
7072 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7073 return FALSE;
7074 }
7075
7076 /* Now that the info is on the function descriptor, clear the
7077 function code sym info. Any function code syms for which we
7078 don't have a definition in a regular file, we force local.
7079 This prevents a shared library from exporting syms that have
7080 been imported from another library. Function code syms that
7081 are really in the library we must leave global to prevent the
7082 linker dragging in a definition from a static library. */
7083 force_local = (!fh->elf.def_regular
7084 || fdh == NULL
7085 || !fdh->elf.def_regular
7086 || fdh->elf.forced_local);
7087 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7088
7089 return TRUE;
7090 }
7091
7092 static const struct sfpr_def_parms save_res_funcs[] =
7093 {
7094 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7095 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7096 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7097 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7098 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7099 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7100 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7101 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7102 { "._savef", 14, 31, savefpr, savefpr1_tail },
7103 { "._restf", 14, 31, restfpr, restfpr1_tail },
7104 { "_savevr_", 20, 31, savevr, savevr_tail },
7105 { "_restvr_", 20, 31, restvr, restvr_tail }
7106 };
7107
7108 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7109 this hook to a) provide some gcc support functions, and b) transfer
7110 dynamic linking information gathered so far on function code symbol
7111 entries, to their corresponding function descriptor symbol entries. */
7112
7113 static bfd_boolean
7114 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7115 struct bfd_link_info *info)
7116 {
7117 struct ppc_link_hash_table *htab;
7118
7119 htab = ppc_hash_table (info);
7120 if (htab == NULL)
7121 return FALSE;
7122
7123 /* Provide any missing _save* and _rest* functions. */
7124 if (htab->sfpr != NULL)
7125 {
7126 unsigned int i;
7127
7128 htab->sfpr->size = 0;
7129 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7130 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7131 return FALSE;
7132 if (htab->sfpr->size == 0)
7133 htab->sfpr->flags |= SEC_EXCLUDE;
7134 }
7135
7136 if (bfd_link_relocatable (info))
7137 return TRUE;
7138
7139 if (htab->elf.hgot != NULL)
7140 {
7141 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7142 /* Make .TOC. defined so as to prevent it being made dynamic.
7143 The wrong value here is fixed later in ppc64_elf_set_toc. */
7144 if (!htab->elf.hgot->def_regular
7145 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7146 {
7147 htab->elf.hgot->root.type = bfd_link_hash_defined;
7148 htab->elf.hgot->root.u.def.value = 0;
7149 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7150 htab->elf.hgot->def_regular = 1;
7151 htab->elf.hgot->root.linker_def = 1;
7152 }
7153 htab->elf.hgot->type = STT_OBJECT;
7154 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7155 | STV_HIDDEN);
7156 }
7157
7158 if (htab->need_func_desc_adj)
7159 {
7160 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7161 htab->need_func_desc_adj = 0;
7162 }
7163
7164 return TRUE;
7165 }
7166
7167 /* Find dynamic relocs for H that apply to read-only sections. */
7168
7169 static asection *
7170 readonly_dynrelocs (struct elf_link_hash_entry *h)
7171 {
7172 struct ppc_link_hash_entry *eh;
7173 struct elf_dyn_relocs *p;
7174
7175 eh = (struct ppc_link_hash_entry *) h;
7176 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7177 {
7178 asection *s = p->sec->output_section;
7179
7180 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7181 return p->sec;
7182 }
7183 return NULL;
7184 }
7185
7186 /* Return true if we have dynamic relocs against H or any of its weak
7187 aliases, that apply to read-only sections. Cannot be used after
7188 size_dynamic_sections. */
7189
7190 static bfd_boolean
7191 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7192 {
7193 struct ppc_link_hash_entry *eh;
7194
7195 eh = (struct ppc_link_hash_entry *) h;
7196 do
7197 {
7198 if (readonly_dynrelocs (&eh->elf))
7199 return TRUE;
7200 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7201 } while (eh != NULL && &eh->elf != h);
7202
7203 return FALSE;
7204 }
7205
7206 /* Return whether EH has pc-relative dynamic relocs. */
7207
7208 static bfd_boolean
7209 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7210 {
7211 struct elf_dyn_relocs *p;
7212
7213 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7214 if (p->pc_count != 0)
7215 return TRUE;
7216 return FALSE;
7217 }
7218
7219 /* Return true if a global entry stub will be created for H. Valid
7220 for ELFv2 before plt entries have been allocated. */
7221
7222 static bfd_boolean
7223 global_entry_stub (struct elf_link_hash_entry *h)
7224 {
7225 struct plt_entry *pent;
7226
7227 if (!h->pointer_equality_needed
7228 || h->def_regular)
7229 return FALSE;
7230
7231 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7232 if (pent->plt.refcount > 0
7233 && pent->addend == 0)
7234 return TRUE;
7235
7236 return FALSE;
7237 }
7238
7239 /* Adjust a symbol defined by a dynamic object and referenced by a
7240 regular object. The current definition is in some section of the
7241 dynamic object, but we're not including those sections. We have to
7242 change the definition to something the rest of the link can
7243 understand. */
7244
7245 static bfd_boolean
7246 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7247 struct elf_link_hash_entry *h)
7248 {
7249 struct ppc_link_hash_table *htab;
7250 asection *s, *srel;
7251
7252 htab = ppc_hash_table (info);
7253 if (htab == NULL)
7254 return FALSE;
7255
7256 /* Deal with function syms. */
7257 if (h->type == STT_FUNC
7258 || h->type == STT_GNU_IFUNC
7259 || h->needs_plt)
7260 {
7261 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7262 || SYMBOL_CALLS_LOCAL (info, h)
7263 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7264 /* Discard dyn_relocs when non-pic if we've decided that a
7265 function symbol is local and not an ifunc. We keep dynamic
7266 relocs for ifuncs when local rather than always emitting a
7267 plt call stub for them and defining the symbol on the call
7268 stub. We can't do that for ELFv1 anyway (a function symbol
7269 is defined on a descriptor, not code) and it can be faster at
7270 run-time due to not needing to bounce through a stub. The
7271 dyn_relocs for ifuncs will be applied even in a static
7272 executable. */
7273 if (!bfd_link_pic (info)
7274 && h->type != STT_GNU_IFUNC
7275 && local)
7276 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7277
7278 /* Clear procedure linkage table information for any symbol that
7279 won't need a .plt entry. */
7280 struct plt_entry *ent;
7281 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7282 if (ent->plt.refcount > 0)
7283 break;
7284 if (ent == NULL
7285 || (h->type != STT_GNU_IFUNC
7286 && local
7287 && (htab->can_convert_all_inline_plt
7288 || (((struct ppc_link_hash_entry *) h)->tls_mask
7289 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7290 {
7291 h->plt.plist = NULL;
7292 h->needs_plt = 0;
7293 h->pointer_equality_needed = 0;
7294 }
7295 else if (abiversion (info->output_bfd) >= 2)
7296 {
7297 /* Taking a function's address in a read/write section
7298 doesn't require us to define the function symbol in the
7299 executable on a global entry stub. A dynamic reloc can
7300 be used instead. The reason we prefer a few more dynamic
7301 relocs is that calling via a global entry stub costs a
7302 few more instructions, and pointer_equality_needed causes
7303 extra work in ld.so when resolving these symbols. */
7304 if (global_entry_stub (h))
7305 {
7306 if (!readonly_dynrelocs (h))
7307 {
7308 h->pointer_equality_needed = 0;
7309 /* If we haven't seen a branch reloc and the symbol
7310 isn't an ifunc then we don't need a plt entry. */
7311 if (!h->needs_plt)
7312 h->plt.plist = NULL;
7313 }
7314 else if (!bfd_link_pic (info))
7315 /* We are going to be defining the function symbol on the
7316 plt stub, so no dyn_relocs needed when non-pic. */
7317 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7318 }
7319
7320 /* ELFv2 function symbols can't have copy relocs. */
7321 return TRUE;
7322 }
7323 else if (!h->needs_plt
7324 && !readonly_dynrelocs (h))
7325 {
7326 /* If we haven't seen a branch reloc and the symbol isn't an
7327 ifunc then we don't need a plt entry. */
7328 h->plt.plist = NULL;
7329 h->pointer_equality_needed = 0;
7330 return TRUE;
7331 }
7332 }
7333 else
7334 h->plt.plist = NULL;
7335
7336 /* If this is a weak symbol, and there is a real definition, the
7337 processor independent code will have arranged for us to see the
7338 real definition first, and we can just use the same value. */
7339 if (h->is_weakalias)
7340 {
7341 struct elf_link_hash_entry *def = weakdef (h);
7342 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7343 h->root.u.def.section = def->root.u.def.section;
7344 h->root.u.def.value = def->root.u.def.value;
7345 if (def->root.u.def.section == htab->elf.sdynbss
7346 || def->root.u.def.section == htab->elf.sdynrelro)
7347 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7348 return TRUE;
7349 }
7350
7351 /* If we are creating a shared library, we must presume that the
7352 only references to the symbol are via the global offset table.
7353 For such cases we need not do anything here; the relocations will
7354 be handled correctly by relocate_section. */
7355 if (bfd_link_pic (info))
7356 return TRUE;
7357
7358 /* If there are no references to this symbol that do not use the
7359 GOT, we don't need to generate a copy reloc. */
7360 if (!h->non_got_ref)
7361 return TRUE;
7362
7363 /* Don't generate a copy reloc for symbols defined in the executable. */
7364 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7365
7366 /* If -z nocopyreloc was given, don't generate them either. */
7367 || info->nocopyreloc
7368
7369 /* If we don't find any dynamic relocs in read-only sections, then
7370 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7371 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7372
7373 /* Protected variables do not work with .dynbss. The copy in
7374 .dynbss won't be used by the shared library with the protected
7375 definition for the variable. Text relocations are preferable
7376 to an incorrect program. */
7377 || h->protected_def)
7378 return TRUE;
7379
7380 if (h->plt.plist != NULL)
7381 {
7382 /* We should never get here, but unfortunately there are versions
7383 of gcc out there that improperly (for this ABI) put initialized
7384 function pointers, vtable refs and suchlike in read-only
7385 sections. Allow them to proceed, but warn that this might
7386 break at runtime. */
7387 info->callbacks->einfo
7388 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7389 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7390 h->root.root.string);
7391 }
7392
7393 /* This is a reference to a symbol defined by a dynamic object which
7394 is not a function. */
7395
7396 /* We must allocate the symbol in our .dynbss section, which will
7397 become part of the .bss section of the executable. There will be
7398 an entry for this symbol in the .dynsym section. The dynamic
7399 object will contain position independent code, so all references
7400 from the dynamic object to this symbol will go through the global
7401 offset table. The dynamic linker will use the .dynsym entry to
7402 determine the address it must put in the global offset table, so
7403 both the dynamic object and the regular object will refer to the
7404 same memory location for the variable. */
7405 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7406 {
7407 s = htab->elf.sdynrelro;
7408 srel = htab->elf.sreldynrelro;
7409 }
7410 else
7411 {
7412 s = htab->elf.sdynbss;
7413 srel = htab->elf.srelbss;
7414 }
7415 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7416 {
7417 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7418 linker to copy the initial value out of the dynamic object
7419 and into the runtime process image. */
7420 srel->size += sizeof (Elf64_External_Rela);
7421 h->needs_copy = 1;
7422 }
7423
7424 /* We no longer want dyn_relocs. */
7425 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7426 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7427 }
7428
7429 /* If given a function descriptor symbol, hide both the function code
7430 sym and the descriptor. */
7431 static void
7432 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7433 struct elf_link_hash_entry *h,
7434 bfd_boolean force_local)
7435 {
7436 struct ppc_link_hash_entry *eh;
7437 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7438
7439 eh = (struct ppc_link_hash_entry *) h;
7440 if (eh->is_func_descriptor)
7441 {
7442 struct ppc_link_hash_entry *fh = eh->oh;
7443
7444 if (fh == NULL)
7445 {
7446 const char *p, *q;
7447 struct elf_link_hash_table *htab = elf_hash_table (info);
7448 char save;
7449
7450 /* We aren't supposed to use alloca in BFD because on
7451 systems which do not have alloca the version in libiberty
7452 calls xmalloc, which might cause the program to crash
7453 when it runs out of memory. This function doesn't have a
7454 return status, so there's no way to gracefully return an
7455 error. So cheat. We know that string[-1] can be safely
7456 accessed; It's either a string in an ELF string table,
7457 or allocated in an objalloc structure. */
7458
7459 p = eh->elf.root.root.string - 1;
7460 save = *p;
7461 *(char *) p = '.';
7462 fh = (struct ppc_link_hash_entry *)
7463 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7464 *(char *) p = save;
7465
7466 /* Unfortunately, if it so happens that the string we were
7467 looking for was allocated immediately before this string,
7468 then we overwrote the string terminator. That's the only
7469 reason the lookup should fail. */
7470 if (fh == NULL)
7471 {
7472 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7473 while (q >= eh->elf.root.root.string && *q == *p)
7474 --q, --p;
7475 if (q < eh->elf.root.root.string && *p == '.')
7476 fh = (struct ppc_link_hash_entry *)
7477 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7478 }
7479 if (fh != NULL)
7480 {
7481 eh->oh = fh;
7482 fh->oh = eh;
7483 }
7484 }
7485 if (fh != NULL)
7486 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7487 }
7488 }
7489
7490 static bfd_boolean
7491 get_sym_h (struct elf_link_hash_entry **hp,
7492 Elf_Internal_Sym **symp,
7493 asection **symsecp,
7494 unsigned char **tls_maskp,
7495 Elf_Internal_Sym **locsymsp,
7496 unsigned long r_symndx,
7497 bfd *ibfd)
7498 {
7499 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7500
7501 if (r_symndx >= symtab_hdr->sh_info)
7502 {
7503 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7504 struct elf_link_hash_entry *h;
7505
7506 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7507 h = elf_follow_link (h);
7508
7509 if (hp != NULL)
7510 *hp = h;
7511
7512 if (symp != NULL)
7513 *symp = NULL;
7514
7515 if (symsecp != NULL)
7516 {
7517 asection *symsec = NULL;
7518 if (h->root.type == bfd_link_hash_defined
7519 || h->root.type == bfd_link_hash_defweak)
7520 symsec = h->root.u.def.section;
7521 *symsecp = symsec;
7522 }
7523
7524 if (tls_maskp != NULL)
7525 {
7526 struct ppc_link_hash_entry *eh;
7527
7528 eh = (struct ppc_link_hash_entry *) h;
7529 *tls_maskp = &eh->tls_mask;
7530 }
7531 }
7532 else
7533 {
7534 Elf_Internal_Sym *sym;
7535 Elf_Internal_Sym *locsyms = *locsymsp;
7536
7537 if (locsyms == NULL)
7538 {
7539 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7540 if (locsyms == NULL)
7541 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7542 symtab_hdr->sh_info,
7543 0, NULL, NULL, NULL);
7544 if (locsyms == NULL)
7545 return FALSE;
7546 *locsymsp = locsyms;
7547 }
7548 sym = locsyms + r_symndx;
7549
7550 if (hp != NULL)
7551 *hp = NULL;
7552
7553 if (symp != NULL)
7554 *symp = sym;
7555
7556 if (symsecp != NULL)
7557 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7558
7559 if (tls_maskp != NULL)
7560 {
7561 struct got_entry **lgot_ents;
7562 unsigned char *tls_mask;
7563
7564 tls_mask = NULL;
7565 lgot_ents = elf_local_got_ents (ibfd);
7566 if (lgot_ents != NULL)
7567 {
7568 struct plt_entry **local_plt = (struct plt_entry **)
7569 (lgot_ents + symtab_hdr->sh_info);
7570 unsigned char *lgot_masks = (unsigned char *)
7571 (local_plt + symtab_hdr->sh_info);
7572 tls_mask = &lgot_masks[r_symndx];
7573 }
7574 *tls_maskp = tls_mask;
7575 }
7576 }
7577 return TRUE;
7578 }
7579
7580 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7581 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7582 type suitable for optimization, and 1 otherwise. */
7583
7584 static int
7585 get_tls_mask (unsigned char **tls_maskp,
7586 unsigned long *toc_symndx,
7587 bfd_vma *toc_addend,
7588 Elf_Internal_Sym **locsymsp,
7589 const Elf_Internal_Rela *rel,
7590 bfd *ibfd)
7591 {
7592 unsigned long r_symndx;
7593 int next_r;
7594 struct elf_link_hash_entry *h;
7595 Elf_Internal_Sym *sym;
7596 asection *sec;
7597 bfd_vma off;
7598
7599 r_symndx = ELF64_R_SYM (rel->r_info);
7600 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7601 return 0;
7602
7603 if ((*tls_maskp != NULL
7604 && (**tls_maskp & TLS_TLS) != 0
7605 && **tls_maskp != (TLS_TLS | TLS_MARK))
7606 || sec == NULL
7607 || ppc64_elf_section_data (sec) == NULL
7608 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7609 return 1;
7610
7611 /* Look inside a TOC section too. */
7612 if (h != NULL)
7613 {
7614 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7615 off = h->root.u.def.value;
7616 }
7617 else
7618 off = sym->st_value;
7619 off += rel->r_addend;
7620 BFD_ASSERT (off % 8 == 0);
7621 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7622 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7623 if (toc_symndx != NULL)
7624 *toc_symndx = r_symndx;
7625 if (toc_addend != NULL)
7626 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7627 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7628 return 0;
7629 if ((h == NULL || is_static_defined (h))
7630 && (next_r == -1 || next_r == -2))
7631 return 1 - next_r;
7632 return 1;
7633 }
7634
7635 /* Find (or create) an entry in the tocsave hash table. */
7636
7637 static struct tocsave_entry *
7638 tocsave_find (struct ppc_link_hash_table *htab,
7639 enum insert_option insert,
7640 Elf_Internal_Sym **local_syms,
7641 const Elf_Internal_Rela *irela,
7642 bfd *ibfd)
7643 {
7644 unsigned long r_indx;
7645 struct elf_link_hash_entry *h;
7646 Elf_Internal_Sym *sym;
7647 struct tocsave_entry ent, *p;
7648 hashval_t hash;
7649 struct tocsave_entry **slot;
7650
7651 r_indx = ELF64_R_SYM (irela->r_info);
7652 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7653 return NULL;
7654 if (ent.sec == NULL || ent.sec->output_section == NULL)
7655 {
7656 _bfd_error_handler
7657 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7658 return NULL;
7659 }
7660
7661 if (h != NULL)
7662 ent.offset = h->root.u.def.value;
7663 else
7664 ent.offset = sym->st_value;
7665 ent.offset += irela->r_addend;
7666
7667 hash = tocsave_htab_hash (&ent);
7668 slot = ((struct tocsave_entry **)
7669 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7670 if (slot == NULL)
7671 return NULL;
7672
7673 if (*slot == NULL)
7674 {
7675 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7676 if (p == NULL)
7677 return NULL;
7678 *p = ent;
7679 *slot = p;
7680 }
7681 return *slot;
7682 }
7683
7684 /* Adjust all global syms defined in opd sections. In gcc generated
7685 code for the old ABI, these will already have been done. */
7686
7687 static bfd_boolean
7688 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7689 {
7690 struct ppc_link_hash_entry *eh;
7691 asection *sym_sec;
7692 struct _opd_sec_data *opd;
7693
7694 if (h->root.type == bfd_link_hash_indirect)
7695 return TRUE;
7696
7697 if (h->root.type != bfd_link_hash_defined
7698 && h->root.type != bfd_link_hash_defweak)
7699 return TRUE;
7700
7701 eh = (struct ppc_link_hash_entry *) h;
7702 if (eh->adjust_done)
7703 return TRUE;
7704
7705 sym_sec = eh->elf.root.u.def.section;
7706 opd = get_opd_info (sym_sec);
7707 if (opd != NULL && opd->adjust != NULL)
7708 {
7709 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7710 if (adjust == -1)
7711 {
7712 /* This entry has been deleted. */
7713 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7714 if (dsec == NULL)
7715 {
7716 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7717 if (discarded_section (dsec))
7718 {
7719 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7720 break;
7721 }
7722 }
7723 eh->elf.root.u.def.value = 0;
7724 eh->elf.root.u.def.section = dsec;
7725 }
7726 else
7727 eh->elf.root.u.def.value += adjust;
7728 eh->adjust_done = 1;
7729 }
7730 return TRUE;
7731 }
7732
7733 /* Handles decrementing dynamic reloc counts for the reloc specified by
7734 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7735 have already been determined. */
7736
7737 static bfd_boolean
7738 dec_dynrel_count (bfd_vma r_info,
7739 asection *sec,
7740 struct bfd_link_info *info,
7741 Elf_Internal_Sym **local_syms,
7742 struct elf_link_hash_entry *h,
7743 Elf_Internal_Sym *sym)
7744 {
7745 enum elf_ppc64_reloc_type r_type;
7746 asection *sym_sec = NULL;
7747
7748 /* Can this reloc be dynamic? This switch, and later tests here
7749 should be kept in sync with the code in check_relocs. */
7750 r_type = ELF64_R_TYPE (r_info);
7751 switch (r_type)
7752 {
7753 default:
7754 return TRUE;
7755
7756 case R_PPC64_TPREL16:
7757 case R_PPC64_TPREL16_LO:
7758 case R_PPC64_TPREL16_HI:
7759 case R_PPC64_TPREL16_HA:
7760 case R_PPC64_TPREL16_DS:
7761 case R_PPC64_TPREL16_LO_DS:
7762 case R_PPC64_TPREL16_HIGH:
7763 case R_PPC64_TPREL16_HIGHA:
7764 case R_PPC64_TPREL16_HIGHER:
7765 case R_PPC64_TPREL16_HIGHERA:
7766 case R_PPC64_TPREL16_HIGHEST:
7767 case R_PPC64_TPREL16_HIGHESTA:
7768 case R_PPC64_TPREL64:
7769 case R_PPC64_DTPMOD64:
7770 case R_PPC64_DTPREL64:
7771 case R_PPC64_ADDR64:
7772 case R_PPC64_REL30:
7773 case R_PPC64_REL32:
7774 case R_PPC64_REL64:
7775 case R_PPC64_ADDR14:
7776 case R_PPC64_ADDR14_BRNTAKEN:
7777 case R_PPC64_ADDR14_BRTAKEN:
7778 case R_PPC64_ADDR16:
7779 case R_PPC64_ADDR16_DS:
7780 case R_PPC64_ADDR16_HA:
7781 case R_PPC64_ADDR16_HI:
7782 case R_PPC64_ADDR16_HIGH:
7783 case R_PPC64_ADDR16_HIGHA:
7784 case R_PPC64_ADDR16_HIGHER:
7785 case R_PPC64_ADDR16_HIGHERA:
7786 case R_PPC64_ADDR16_HIGHEST:
7787 case R_PPC64_ADDR16_HIGHESTA:
7788 case R_PPC64_ADDR16_LO:
7789 case R_PPC64_ADDR16_LO_DS:
7790 case R_PPC64_ADDR24:
7791 case R_PPC64_ADDR32:
7792 case R_PPC64_UADDR16:
7793 case R_PPC64_UADDR32:
7794 case R_PPC64_UADDR64:
7795 case R_PPC64_TOC:
7796 break;
7797 }
7798
7799 if (local_syms != NULL)
7800 {
7801 unsigned long r_symndx;
7802 bfd *ibfd = sec->owner;
7803
7804 r_symndx = ELF64_R_SYM (r_info);
7805 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7806 return FALSE;
7807 }
7808
7809 if ((bfd_link_pic (info)
7810 && (must_be_dyn_reloc (info, r_type)
7811 || (h != NULL
7812 && (!SYMBOLIC_BIND (info, h)
7813 || h->root.type == bfd_link_hash_defweak
7814 || !h->def_regular))))
7815 || (ELIMINATE_COPY_RELOCS
7816 && !bfd_link_pic (info)
7817 && h != NULL
7818 && (h->root.type == bfd_link_hash_defweak
7819 || !h->def_regular)))
7820 ;
7821 else
7822 return TRUE;
7823
7824 if (h != NULL)
7825 {
7826 struct elf_dyn_relocs *p;
7827 struct elf_dyn_relocs **pp;
7828 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7829
7830 /* elf_gc_sweep may have already removed all dyn relocs associated
7831 with local syms for a given section. Also, symbol flags are
7832 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7833 report a dynreloc miscount. */
7834 if (*pp == NULL && info->gc_sections)
7835 return TRUE;
7836
7837 while ((p = *pp) != NULL)
7838 {
7839 if (p->sec == sec)
7840 {
7841 if (!must_be_dyn_reloc (info, r_type))
7842 p->pc_count -= 1;
7843 p->count -= 1;
7844 if (p->count == 0)
7845 *pp = p->next;
7846 return TRUE;
7847 }
7848 pp = &p->next;
7849 }
7850 }
7851 else
7852 {
7853 struct ppc_dyn_relocs *p;
7854 struct ppc_dyn_relocs **pp;
7855 void *vpp;
7856 bfd_boolean is_ifunc;
7857
7858 if (local_syms == NULL)
7859 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7860 if (sym_sec == NULL)
7861 sym_sec = sec;
7862
7863 vpp = &elf_section_data (sym_sec)->local_dynrel;
7864 pp = (struct ppc_dyn_relocs **) vpp;
7865
7866 if (*pp == NULL && info->gc_sections)
7867 return TRUE;
7868
7869 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7870 while ((p = *pp) != NULL)
7871 {
7872 if (p->sec == sec && p->ifunc == is_ifunc)
7873 {
7874 p->count -= 1;
7875 if (p->count == 0)
7876 *pp = p->next;
7877 return TRUE;
7878 }
7879 pp = &p->next;
7880 }
7881 }
7882
7883 /* xgettext:c-format */
7884 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7885 sec->owner, sec);
7886 bfd_set_error (bfd_error_bad_value);
7887 return FALSE;
7888 }
7889
7890 /* Remove unused Official Procedure Descriptor entries. Currently we
7891 only remove those associated with functions in discarded link-once
7892 sections, or weakly defined functions that have been overridden. It
7893 would be possible to remove many more entries for statically linked
7894 applications. */
7895
7896 bfd_boolean
7897 ppc64_elf_edit_opd (struct bfd_link_info *info)
7898 {
7899 bfd *ibfd;
7900 bfd_boolean some_edited = FALSE;
7901 asection *need_pad = NULL;
7902 struct ppc_link_hash_table *htab;
7903
7904 htab = ppc_hash_table (info);
7905 if (htab == NULL)
7906 return FALSE;
7907
7908 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7909 {
7910 asection *sec;
7911 Elf_Internal_Rela *relstart, *rel, *relend;
7912 Elf_Internal_Shdr *symtab_hdr;
7913 Elf_Internal_Sym *local_syms;
7914 struct _opd_sec_data *opd;
7915 bfd_boolean need_edit, add_aux_fields, broken;
7916 bfd_size_type cnt_16b = 0;
7917
7918 if (!is_ppc64_elf (ibfd))
7919 continue;
7920
7921 sec = bfd_get_section_by_name (ibfd, ".opd");
7922 if (sec == NULL || sec->size == 0)
7923 continue;
7924
7925 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7926 continue;
7927
7928 if (sec->output_section == bfd_abs_section_ptr)
7929 continue;
7930
7931 /* Look through the section relocs. */
7932 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7933 continue;
7934
7935 local_syms = NULL;
7936 symtab_hdr = &elf_symtab_hdr (ibfd);
7937
7938 /* Read the relocations. */
7939 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7940 info->keep_memory);
7941 if (relstart == NULL)
7942 return FALSE;
7943
7944 /* First run through the relocs to check they are sane, and to
7945 determine whether we need to edit this opd section. */
7946 need_edit = FALSE;
7947 broken = FALSE;
7948 need_pad = sec;
7949 relend = relstart + sec->reloc_count;
7950 for (rel = relstart; rel < relend; )
7951 {
7952 enum elf_ppc64_reloc_type r_type;
7953 unsigned long r_symndx;
7954 asection *sym_sec;
7955 struct elf_link_hash_entry *h;
7956 Elf_Internal_Sym *sym;
7957 bfd_vma offset;
7958
7959 /* .opd contains an array of 16 or 24 byte entries. We're
7960 only interested in the reloc pointing to a function entry
7961 point. */
7962 offset = rel->r_offset;
7963 if (rel + 1 == relend
7964 || rel[1].r_offset != offset + 8)
7965 {
7966 /* If someone messes with .opd alignment then after a
7967 "ld -r" we might have padding in the middle of .opd.
7968 Also, there's nothing to prevent someone putting
7969 something silly in .opd with the assembler. No .opd
7970 optimization for them! */
7971 broken_opd:
7972 _bfd_error_handler
7973 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7974 broken = TRUE;
7975 break;
7976 }
7977
7978 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7979 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7980 {
7981 _bfd_error_handler
7982 /* xgettext:c-format */
7983 (_("%pB: unexpected reloc type %u in .opd section"),
7984 ibfd, r_type);
7985 broken = TRUE;
7986 break;
7987 }
7988
7989 r_symndx = ELF64_R_SYM (rel->r_info);
7990 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7991 r_symndx, ibfd))
7992 goto error_ret;
7993
7994 if (sym_sec == NULL || sym_sec->owner == NULL)
7995 {
7996 const char *sym_name;
7997 if (h != NULL)
7998 sym_name = h->root.root.string;
7999 else
8000 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8001 sym_sec);
8002
8003 _bfd_error_handler
8004 /* xgettext:c-format */
8005 (_("%pB: undefined sym `%s' in .opd section"),
8006 ibfd, sym_name);
8007 broken = TRUE;
8008 break;
8009 }
8010
8011 /* opd entries are always for functions defined in the
8012 current input bfd. If the symbol isn't defined in the
8013 input bfd, then we won't be using the function in this
8014 bfd; It must be defined in a linkonce section in another
8015 bfd, or is weak. It's also possible that we are
8016 discarding the function due to a linker script /DISCARD/,
8017 which we test for via the output_section. */
8018 if (sym_sec->owner != ibfd
8019 || sym_sec->output_section == bfd_abs_section_ptr)
8020 need_edit = TRUE;
8021
8022 rel += 2;
8023 if (rel + 1 == relend
8024 || (rel + 2 < relend
8025 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8026 ++rel;
8027
8028 if (rel == relend)
8029 {
8030 if (sec->size == offset + 24)
8031 {
8032 need_pad = NULL;
8033 break;
8034 }
8035 if (sec->size == offset + 16)
8036 {
8037 cnt_16b++;
8038 break;
8039 }
8040 goto broken_opd;
8041 }
8042 else if (rel + 1 < relend
8043 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8044 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8045 {
8046 if (rel[0].r_offset == offset + 16)
8047 cnt_16b++;
8048 else if (rel[0].r_offset != offset + 24)
8049 goto broken_opd;
8050 }
8051 else
8052 goto broken_opd;
8053 }
8054
8055 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8056
8057 if (!broken && (need_edit || add_aux_fields))
8058 {
8059 Elf_Internal_Rela *write_rel;
8060 Elf_Internal_Shdr *rel_hdr;
8061 bfd_byte *rptr, *wptr;
8062 bfd_byte *new_contents;
8063 bfd_size_type amt;
8064
8065 new_contents = NULL;
8066 amt = OPD_NDX (sec->size) * sizeof (long);
8067 opd = &ppc64_elf_section_data (sec)->u.opd;
8068 opd->adjust = bfd_zalloc (sec->owner, amt);
8069 if (opd->adjust == NULL)
8070 return FALSE;
8071
8072 /* This seems a waste of time as input .opd sections are all
8073 zeros as generated by gcc, but I suppose there's no reason
8074 this will always be so. We might start putting something in
8075 the third word of .opd entries. */
8076 if ((sec->flags & SEC_IN_MEMORY) == 0)
8077 {
8078 bfd_byte *loc;
8079 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8080 {
8081 if (loc != NULL)
8082 free (loc);
8083 error_ret:
8084 if (local_syms != NULL
8085 && symtab_hdr->contents != (unsigned char *) local_syms)
8086 free (local_syms);
8087 if (elf_section_data (sec)->relocs != relstart)
8088 free (relstart);
8089 return FALSE;
8090 }
8091 sec->contents = loc;
8092 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8093 }
8094
8095 elf_section_data (sec)->relocs = relstart;
8096
8097 new_contents = sec->contents;
8098 if (add_aux_fields)
8099 {
8100 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8101 if (new_contents == NULL)
8102 return FALSE;
8103 need_pad = NULL;
8104 }
8105 wptr = new_contents;
8106 rptr = sec->contents;
8107 write_rel = relstart;
8108 for (rel = relstart; rel < relend; )
8109 {
8110 unsigned long r_symndx;
8111 asection *sym_sec;
8112 struct elf_link_hash_entry *h;
8113 struct ppc_link_hash_entry *fdh = NULL;
8114 Elf_Internal_Sym *sym;
8115 long opd_ent_size;
8116 Elf_Internal_Rela *next_rel;
8117 bfd_boolean skip;
8118
8119 r_symndx = ELF64_R_SYM (rel->r_info);
8120 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8121 r_symndx, ibfd))
8122 goto error_ret;
8123
8124 next_rel = rel + 2;
8125 if (next_rel + 1 == relend
8126 || (next_rel + 2 < relend
8127 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8128 ++next_rel;
8129
8130 /* See if the .opd entry is full 24 byte or
8131 16 byte (with fd_aux entry overlapped with next
8132 fd_func). */
8133 opd_ent_size = 24;
8134 if (next_rel == relend)
8135 {
8136 if (sec->size == rel->r_offset + 16)
8137 opd_ent_size = 16;
8138 }
8139 else if (next_rel->r_offset == rel->r_offset + 16)
8140 opd_ent_size = 16;
8141
8142 if (h != NULL
8143 && h->root.root.string[0] == '.')
8144 {
8145 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8146 if (fdh != NULL)
8147 {
8148 fdh = ppc_follow_link (fdh);
8149 if (fdh->elf.root.type != bfd_link_hash_defined
8150 && fdh->elf.root.type != bfd_link_hash_defweak)
8151 fdh = NULL;
8152 }
8153 }
8154
8155 skip = (sym_sec->owner != ibfd
8156 || sym_sec->output_section == bfd_abs_section_ptr);
8157 if (skip)
8158 {
8159 if (fdh != NULL && sym_sec->owner == ibfd)
8160 {
8161 /* Arrange for the function descriptor sym
8162 to be dropped. */
8163 fdh->elf.root.u.def.value = 0;
8164 fdh->elf.root.u.def.section = sym_sec;
8165 }
8166 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8167
8168 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8169 rel = next_rel;
8170 else
8171 while (1)
8172 {
8173 if (!dec_dynrel_count (rel->r_info, sec, info,
8174 NULL, h, sym))
8175 goto error_ret;
8176
8177 if (++rel == next_rel)
8178 break;
8179
8180 r_symndx = ELF64_R_SYM (rel->r_info);
8181 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8182 r_symndx, ibfd))
8183 goto error_ret;
8184 }
8185 }
8186 else
8187 {
8188 /* We'll be keeping this opd entry. */
8189 long adjust;
8190
8191 if (fdh != NULL)
8192 {
8193 /* Redefine the function descriptor symbol to
8194 this location in the opd section. It is
8195 necessary to update the value here rather
8196 than using an array of adjustments as we do
8197 for local symbols, because various places
8198 in the generic ELF code use the value
8199 stored in u.def.value. */
8200 fdh->elf.root.u.def.value = wptr - new_contents;
8201 fdh->adjust_done = 1;
8202 }
8203
8204 /* Local syms are a bit tricky. We could
8205 tweak them as they can be cached, but
8206 we'd need to look through the local syms
8207 for the function descriptor sym which we
8208 don't have at the moment. So keep an
8209 array of adjustments. */
8210 adjust = (wptr - new_contents) - (rptr - sec->contents);
8211 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8212
8213 if (wptr != rptr)
8214 memcpy (wptr, rptr, opd_ent_size);
8215 wptr += opd_ent_size;
8216 if (add_aux_fields && opd_ent_size == 16)
8217 {
8218 memset (wptr, '\0', 8);
8219 wptr += 8;
8220 }
8221
8222 /* We need to adjust any reloc offsets to point to the
8223 new opd entries. */
8224 for ( ; rel != next_rel; ++rel)
8225 {
8226 rel->r_offset += adjust;
8227 if (write_rel != rel)
8228 memcpy (write_rel, rel, sizeof (*rel));
8229 ++write_rel;
8230 }
8231 }
8232
8233 rptr += opd_ent_size;
8234 }
8235
8236 sec->size = wptr - new_contents;
8237 sec->reloc_count = write_rel - relstart;
8238 if (add_aux_fields)
8239 {
8240 free (sec->contents);
8241 sec->contents = new_contents;
8242 }
8243
8244 /* Fudge the header size too, as this is used later in
8245 elf_bfd_final_link if we are emitting relocs. */
8246 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8247 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8248 some_edited = TRUE;
8249 }
8250 else if (elf_section_data (sec)->relocs != relstart)
8251 free (relstart);
8252
8253 if (local_syms != NULL
8254 && symtab_hdr->contents != (unsigned char *) local_syms)
8255 {
8256 if (!info->keep_memory)
8257 free (local_syms);
8258 else
8259 symtab_hdr->contents = (unsigned char *) local_syms;
8260 }
8261 }
8262
8263 if (some_edited)
8264 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8265
8266 /* If we are doing a final link and the last .opd entry is just 16 byte
8267 long, add a 8 byte padding after it. */
8268 if (need_pad != NULL && !bfd_link_relocatable (info))
8269 {
8270 bfd_byte *p;
8271
8272 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8273 {
8274 BFD_ASSERT (need_pad->size > 0);
8275
8276 p = bfd_malloc (need_pad->size + 8);
8277 if (p == NULL)
8278 return FALSE;
8279
8280 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8281 p, 0, need_pad->size))
8282 return FALSE;
8283
8284 need_pad->contents = p;
8285 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8286 }
8287 else
8288 {
8289 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8290 if (p == NULL)
8291 return FALSE;
8292
8293 need_pad->contents = p;
8294 }
8295
8296 memset (need_pad->contents + need_pad->size, 0, 8);
8297 need_pad->size += 8;
8298 }
8299
8300 return TRUE;
8301 }
8302
8303 /* Analyze inline PLT call relocations to see whether calls to locally
8304 defined functions can be converted to direct calls. */
8305
8306 bfd_boolean
8307 ppc64_elf_inline_plt (struct bfd_link_info *info)
8308 {
8309 struct ppc_link_hash_table *htab;
8310 bfd *ibfd;
8311 asection *sec;
8312 bfd_vma low_vma, high_vma, limit;
8313
8314 htab = ppc_hash_table (info);
8315 if (htab == NULL)
8316 return FALSE;
8317
8318 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8319 reduced somewhat to cater for possible stubs that might be added
8320 between the call and its destination. */
8321 if (htab->params->group_size < 0)
8322 {
8323 limit = -htab->params->group_size;
8324 if (limit == 1)
8325 limit = 0x1e00000;
8326 }
8327 else
8328 {
8329 limit = htab->params->group_size;
8330 if (limit == 1)
8331 limit = 0x1c00000;
8332 }
8333
8334 low_vma = -1;
8335 high_vma = 0;
8336 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8337 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8338 {
8339 if (low_vma > sec->vma)
8340 low_vma = sec->vma;
8341 if (high_vma < sec->vma + sec->size)
8342 high_vma = sec->vma + sec->size;
8343 }
8344
8345 /* If a "bl" can reach anywhere in local code sections, then we can
8346 convert all inline PLT sequences to direct calls when the symbol
8347 is local. */
8348 if (high_vma - low_vma < limit)
8349 {
8350 htab->can_convert_all_inline_plt = 1;
8351 return TRUE;
8352 }
8353
8354 /* Otherwise, go looking through relocs for cases where a direct
8355 call won't reach. Mark the symbol on any such reloc to disable
8356 the optimization and keep the PLT entry as it seems likely that
8357 this will be better than creating trampolines. Note that this
8358 will disable the optimization for all inline PLT calls to a
8359 particular symbol, not just those that won't reach. The
8360 difficulty in doing a more precise optimization is that the
8361 linker needs to make a decision depending on whether a
8362 particular R_PPC64_PLTCALL insn can be turned into a direct
8363 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8364 the sequence, and there is nothing that ties those relocs
8365 together except their symbol. */
8366
8367 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8368 {
8369 Elf_Internal_Shdr *symtab_hdr;
8370 Elf_Internal_Sym *local_syms;
8371
8372 if (!is_ppc64_elf (ibfd))
8373 continue;
8374
8375 local_syms = NULL;
8376 symtab_hdr = &elf_symtab_hdr (ibfd);
8377
8378 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8379 if (ppc64_elf_section_data (sec)->has_pltcall
8380 && !bfd_is_abs_section (sec->output_section))
8381 {
8382 Elf_Internal_Rela *relstart, *rel, *relend;
8383
8384 /* Read the relocations. */
8385 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8386 info->keep_memory);
8387 if (relstart == NULL)
8388 return FALSE;
8389
8390 relend = relstart + sec->reloc_count;
8391 for (rel = relstart; rel < relend; )
8392 {
8393 enum elf_ppc64_reloc_type r_type;
8394 unsigned long r_symndx;
8395 asection *sym_sec;
8396 struct elf_link_hash_entry *h;
8397 Elf_Internal_Sym *sym;
8398 unsigned char *tls_maskp;
8399
8400 r_type = ELF64_R_TYPE (rel->r_info);
8401 if (r_type != R_PPC64_PLTCALL)
8402 continue;
8403
8404 r_symndx = ELF64_R_SYM (rel->r_info);
8405 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8406 r_symndx, ibfd))
8407 {
8408 if (elf_section_data (sec)->relocs != relstart)
8409 free (relstart);
8410 if (local_syms != NULL
8411 && symtab_hdr->contents != (unsigned char *) local_syms)
8412 free (local_syms);
8413 return FALSE;
8414 }
8415
8416 if (sym_sec != NULL && sym_sec->output_section != NULL)
8417 {
8418 bfd_vma from, to;
8419 if (h != NULL)
8420 to = h->root.u.def.value;
8421 else
8422 to = sym->st_value;
8423 to += (rel->r_addend
8424 + sym_sec->output_offset
8425 + sym_sec->output_section->vma);
8426 from = (rel->r_offset
8427 + sec->output_offset
8428 + sec->output_section->vma);
8429 if (to - from + limit < 2 * limit)
8430 *tls_maskp &= ~PLT_KEEP;
8431 }
8432 }
8433 if (elf_section_data (sec)->relocs != relstart)
8434 free (relstart);
8435 }
8436
8437 if (local_syms != NULL
8438 && symtab_hdr->contents != (unsigned char *) local_syms)
8439 {
8440 if (!info->keep_memory)
8441 free (local_syms);
8442 else
8443 symtab_hdr->contents = (unsigned char *) local_syms;
8444 }
8445 }
8446
8447 return TRUE;
8448 }
8449
8450 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8451
8452 asection *
8453 ppc64_elf_tls_setup (struct bfd_link_info *info)
8454 {
8455 struct ppc_link_hash_table *htab;
8456
8457 htab = ppc_hash_table (info);
8458 if (htab == NULL)
8459 return NULL;
8460
8461 if (abiversion (info->output_bfd) == 1)
8462 htab->opd_abi = 1;
8463
8464 if (htab->params->no_multi_toc)
8465 htab->do_multi_toc = 0;
8466 else if (!htab->do_multi_toc)
8467 htab->params->no_multi_toc = 1;
8468
8469 /* Default to --no-plt-localentry, as this option can cause problems
8470 with symbol interposition. For example, glibc libpthread.so and
8471 libc.so duplicate many pthread symbols, with a fallback
8472 implementation in libc.so. In some cases the fallback does more
8473 work than the pthread implementation. __pthread_condattr_destroy
8474 is one such symbol: the libpthread.so implementation is
8475 localentry:0 while the libc.so implementation is localentry:8.
8476 An app that "cleverly" uses dlopen to only load necessary
8477 libraries at runtime may omit loading libpthread.so when not
8478 running multi-threaded, which then results in the libc.so
8479 fallback symbols being used and ld.so complaining. Now there
8480 are workarounds in ld (see non_zero_localentry) to detect the
8481 pthread situation, but that may not be the only case where
8482 --plt-localentry can cause trouble. */
8483 if (htab->params->plt_localentry0 < 0)
8484 htab->params->plt_localentry0 = 0;
8485 if (htab->params->plt_localentry0
8486 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8487 FALSE, FALSE, FALSE) == NULL)
8488 _bfd_error_handler
8489 (_("warning: --plt-localentry is especially dangerous without "
8490 "ld.so support to detect ABI violations"));
8491
8492 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8493 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8494 FALSE, FALSE, TRUE));
8495 /* Move dynamic linking info to the function descriptor sym. */
8496 if (htab->tls_get_addr != NULL)
8497 func_desc_adjust (&htab->tls_get_addr->elf, info);
8498 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8499 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8500 FALSE, FALSE, TRUE));
8501 if (htab->params->tls_get_addr_opt)
8502 {
8503 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8504
8505 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8506 FALSE, FALSE, TRUE);
8507 if (opt != NULL)
8508 func_desc_adjust (opt, info);
8509 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8510 FALSE, FALSE, TRUE);
8511 if (opt_fd != NULL
8512 && (opt_fd->root.type == bfd_link_hash_defined
8513 || opt_fd->root.type == bfd_link_hash_defweak))
8514 {
8515 /* If glibc supports an optimized __tls_get_addr call stub,
8516 signalled by the presence of __tls_get_addr_opt, and we'll
8517 be calling __tls_get_addr via a plt call stub, then
8518 make __tls_get_addr point to __tls_get_addr_opt. */
8519 tga_fd = &htab->tls_get_addr_fd->elf;
8520 if (htab->elf.dynamic_sections_created
8521 && tga_fd != NULL
8522 && (tga_fd->type == STT_FUNC
8523 || tga_fd->needs_plt)
8524 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8525 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8526 {
8527 struct plt_entry *ent;
8528
8529 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8530 if (ent->plt.refcount > 0)
8531 break;
8532 if (ent != NULL)
8533 {
8534 tga_fd->root.type = bfd_link_hash_indirect;
8535 tga_fd->root.u.i.link = &opt_fd->root;
8536 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8537 opt_fd->mark = 1;
8538 if (opt_fd->dynindx != -1)
8539 {
8540 /* Use __tls_get_addr_opt in dynamic relocations. */
8541 opt_fd->dynindx = -1;
8542 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8543 opt_fd->dynstr_index);
8544 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8545 return NULL;
8546 }
8547 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8548 tga = &htab->tls_get_addr->elf;
8549 if (opt != NULL && tga != NULL)
8550 {
8551 tga->root.type = bfd_link_hash_indirect;
8552 tga->root.u.i.link = &opt->root;
8553 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8554 opt->mark = 1;
8555 _bfd_elf_link_hash_hide_symbol (info, opt,
8556 tga->forced_local);
8557 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8558 }
8559 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8560 htab->tls_get_addr_fd->is_func_descriptor = 1;
8561 if (htab->tls_get_addr != NULL)
8562 {
8563 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8564 htab->tls_get_addr->is_func = 1;
8565 }
8566 }
8567 }
8568 }
8569 else if (htab->params->tls_get_addr_opt < 0)
8570 htab->params->tls_get_addr_opt = 0;
8571 }
8572 return _bfd_elf_tls_setup (info->output_bfd, info);
8573 }
8574
8575 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8576 HASH1 or HASH2. */
8577
8578 static bfd_boolean
8579 branch_reloc_hash_match (const bfd *ibfd,
8580 const Elf_Internal_Rela *rel,
8581 const struct ppc_link_hash_entry *hash1,
8582 const struct ppc_link_hash_entry *hash2)
8583 {
8584 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8585 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8586 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8587
8588 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8589 {
8590 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8591 struct elf_link_hash_entry *h;
8592
8593 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8594 h = elf_follow_link (h);
8595 if (h == &hash1->elf || h == &hash2->elf)
8596 return TRUE;
8597 }
8598 return FALSE;
8599 }
8600
8601 /* Run through all the TLS relocs looking for optimization
8602 opportunities. The linker has been hacked (see ppc64elf.em) to do
8603 a preliminary section layout so that we know the TLS segment
8604 offsets. We can't optimize earlier because some optimizations need
8605 to know the tp offset, and we need to optimize before allocating
8606 dynamic relocations. */
8607
8608 bfd_boolean
8609 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8610 {
8611 bfd *ibfd;
8612 asection *sec;
8613 struct ppc_link_hash_table *htab;
8614 unsigned char *toc_ref;
8615 int pass;
8616
8617 if (!bfd_link_executable (info))
8618 return TRUE;
8619
8620 htab = ppc_hash_table (info);
8621 if (htab == NULL)
8622 return FALSE;
8623
8624 /* Make two passes over the relocs. On the first pass, mark toc
8625 entries involved with tls relocs, and check that tls relocs
8626 involved in setting up a tls_get_addr call are indeed followed by
8627 such a call. If they are not, we can't do any tls optimization.
8628 On the second pass twiddle tls_mask flags to notify
8629 relocate_section that optimization can be done, and adjust got
8630 and plt refcounts. */
8631 toc_ref = NULL;
8632 for (pass = 0; pass < 2; ++pass)
8633 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8634 {
8635 Elf_Internal_Sym *locsyms = NULL;
8636 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8637
8638 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8639 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8640 {
8641 Elf_Internal_Rela *relstart, *rel, *relend;
8642 bfd_boolean found_tls_get_addr_arg = 0;
8643
8644 /* Read the relocations. */
8645 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8646 info->keep_memory);
8647 if (relstart == NULL)
8648 {
8649 free (toc_ref);
8650 return FALSE;
8651 }
8652
8653 relend = relstart + sec->reloc_count;
8654 for (rel = relstart; rel < relend; rel++)
8655 {
8656 enum elf_ppc64_reloc_type r_type;
8657 unsigned long r_symndx;
8658 struct elf_link_hash_entry *h;
8659 Elf_Internal_Sym *sym;
8660 asection *sym_sec;
8661 unsigned char *tls_mask;
8662 unsigned char tls_set, tls_clear, tls_type = 0;
8663 bfd_vma value;
8664 bfd_boolean ok_tprel, is_local;
8665 long toc_ref_index = 0;
8666 int expecting_tls_get_addr = 0;
8667 bfd_boolean ret = FALSE;
8668
8669 r_symndx = ELF64_R_SYM (rel->r_info);
8670 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8671 r_symndx, ibfd))
8672 {
8673 err_free_rel:
8674 if (elf_section_data (sec)->relocs != relstart)
8675 free (relstart);
8676 if (toc_ref != NULL)
8677 free (toc_ref);
8678 if (locsyms != NULL
8679 && (elf_symtab_hdr (ibfd).contents
8680 != (unsigned char *) locsyms))
8681 free (locsyms);
8682 return ret;
8683 }
8684
8685 if (h != NULL)
8686 {
8687 if (h->root.type == bfd_link_hash_defined
8688 || h->root.type == bfd_link_hash_defweak)
8689 value = h->root.u.def.value;
8690 else if (h->root.type == bfd_link_hash_undefweak)
8691 value = 0;
8692 else
8693 {
8694 found_tls_get_addr_arg = 0;
8695 continue;
8696 }
8697 }
8698 else
8699 /* Symbols referenced by TLS relocs must be of type
8700 STT_TLS. So no need for .opd local sym adjust. */
8701 value = sym->st_value;
8702
8703 ok_tprel = FALSE;
8704 is_local = FALSE;
8705 if (h == NULL
8706 || !h->def_dynamic)
8707 {
8708 is_local = TRUE;
8709 if (h != NULL
8710 && h->root.type == bfd_link_hash_undefweak)
8711 ok_tprel = TRUE;
8712 else if (sym_sec != NULL
8713 && sym_sec->output_section != NULL)
8714 {
8715 value += sym_sec->output_offset;
8716 value += sym_sec->output_section->vma;
8717 value -= htab->elf.tls_sec->vma;
8718 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8719 < (bfd_vma) 1 << 32);
8720 }
8721 }
8722
8723 r_type = ELF64_R_TYPE (rel->r_info);
8724 /* If this section has old-style __tls_get_addr calls
8725 without marker relocs, then check that each
8726 __tls_get_addr call reloc is preceded by a reloc
8727 that conceivably belongs to the __tls_get_addr arg
8728 setup insn. If we don't find matching arg setup
8729 relocs, don't do any tls optimization. */
8730 if (pass == 0
8731 && sec->has_tls_get_addr_call
8732 && h != NULL
8733 && (h == &htab->tls_get_addr->elf
8734 || h == &htab->tls_get_addr_fd->elf)
8735 && !found_tls_get_addr_arg
8736 && is_branch_reloc (r_type))
8737 {
8738 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8739 "TLS optimization disabled\n"),
8740 ibfd, sec, rel->r_offset);
8741 ret = TRUE;
8742 goto err_free_rel;
8743 }
8744
8745 found_tls_get_addr_arg = 0;
8746 switch (r_type)
8747 {
8748 case R_PPC64_GOT_TLSLD16:
8749 case R_PPC64_GOT_TLSLD16_LO:
8750 expecting_tls_get_addr = 1;
8751 found_tls_get_addr_arg = 1;
8752 /* Fall through. */
8753
8754 case R_PPC64_GOT_TLSLD16_HI:
8755 case R_PPC64_GOT_TLSLD16_HA:
8756 /* These relocs should never be against a symbol
8757 defined in a shared lib. Leave them alone if
8758 that turns out to be the case. */
8759 if (!is_local)
8760 continue;
8761
8762 /* LD -> LE */
8763 tls_set = 0;
8764 tls_clear = TLS_LD;
8765 tls_type = TLS_TLS | TLS_LD;
8766 break;
8767
8768 case R_PPC64_GOT_TLSGD16:
8769 case R_PPC64_GOT_TLSGD16_LO:
8770 expecting_tls_get_addr = 1;
8771 found_tls_get_addr_arg = 1;
8772 /* Fall through. */
8773
8774 case R_PPC64_GOT_TLSGD16_HI:
8775 case R_PPC64_GOT_TLSGD16_HA:
8776 if (ok_tprel)
8777 /* GD -> LE */
8778 tls_set = 0;
8779 else
8780 /* GD -> IE */
8781 tls_set = TLS_TLS | TLS_TPRELGD;
8782 tls_clear = TLS_GD;
8783 tls_type = TLS_TLS | TLS_GD;
8784 break;
8785
8786 case R_PPC64_GOT_TPREL16_DS:
8787 case R_PPC64_GOT_TPREL16_LO_DS:
8788 case R_PPC64_GOT_TPREL16_HI:
8789 case R_PPC64_GOT_TPREL16_HA:
8790 if (ok_tprel)
8791 {
8792 /* IE -> LE */
8793 tls_set = 0;
8794 tls_clear = TLS_TPREL;
8795 tls_type = TLS_TLS | TLS_TPREL;
8796 break;
8797 }
8798 continue;
8799
8800 case R_PPC64_TLSGD:
8801 case R_PPC64_TLSLD:
8802 if (rel + 1 < relend
8803 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8804 {
8805 if (pass != 0
8806 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8807 {
8808 r_symndx = ELF64_R_SYM (rel[1].r_info);
8809 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8810 r_symndx, ibfd))
8811 goto err_free_rel;
8812 if (h != NULL)
8813 {
8814 struct plt_entry *ent = NULL;
8815
8816 for (ent = h->plt.plist;
8817 ent != NULL;
8818 ent = ent->next)
8819 if (ent->addend == rel[1].r_addend)
8820 break;
8821
8822 if (ent != NULL
8823 && ent->plt.refcount > 0)
8824 ent->plt.refcount -= 1;
8825 }
8826 }
8827 continue;
8828 }
8829 found_tls_get_addr_arg = 1;
8830 /* Fall through. */
8831
8832 case R_PPC64_TLS:
8833 case R_PPC64_TOC16:
8834 case R_PPC64_TOC16_LO:
8835 if (sym_sec == NULL || sym_sec != toc)
8836 continue;
8837
8838 /* Mark this toc entry as referenced by a TLS
8839 code sequence. We can do that now in the
8840 case of R_PPC64_TLS, and after checking for
8841 tls_get_addr for the TOC16 relocs. */
8842 if (toc_ref == NULL)
8843 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8844 if (toc_ref == NULL)
8845 goto err_free_rel;
8846
8847 if (h != NULL)
8848 value = h->root.u.def.value;
8849 else
8850 value = sym->st_value;
8851 value += rel->r_addend;
8852 if (value % 8 != 0)
8853 continue;
8854 BFD_ASSERT (value < toc->size
8855 && toc->output_offset % 8 == 0);
8856 toc_ref_index = (value + toc->output_offset) / 8;
8857 if (r_type == R_PPC64_TLS
8858 || r_type == R_PPC64_TLSGD
8859 || r_type == R_PPC64_TLSLD)
8860 {
8861 toc_ref[toc_ref_index] = 1;
8862 continue;
8863 }
8864
8865 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8866 continue;
8867
8868 tls_set = 0;
8869 tls_clear = 0;
8870 expecting_tls_get_addr = 2;
8871 break;
8872
8873 case R_PPC64_TPREL64:
8874 if (pass == 0
8875 || sec != toc
8876 || toc_ref == NULL
8877 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8878 continue;
8879 if (ok_tprel)
8880 {
8881 /* IE -> LE */
8882 tls_set = TLS_EXPLICIT;
8883 tls_clear = TLS_TPREL;
8884 break;
8885 }
8886 continue;
8887
8888 case R_PPC64_DTPMOD64:
8889 if (pass == 0
8890 || sec != toc
8891 || toc_ref == NULL
8892 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8893 continue;
8894 if (rel + 1 < relend
8895 && (rel[1].r_info
8896 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8897 && rel[1].r_offset == rel->r_offset + 8)
8898 {
8899 if (ok_tprel)
8900 /* GD -> LE */
8901 tls_set = TLS_EXPLICIT | TLS_GD;
8902 else
8903 /* GD -> IE */
8904 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8905 tls_clear = TLS_GD;
8906 }
8907 else
8908 {
8909 if (!is_local)
8910 continue;
8911
8912 /* LD -> LE */
8913 tls_set = TLS_EXPLICIT;
8914 tls_clear = TLS_LD;
8915 }
8916 break;
8917
8918 default:
8919 continue;
8920 }
8921
8922 if (pass == 0)
8923 {
8924 if (!expecting_tls_get_addr
8925 || !sec->has_tls_get_addr_call)
8926 continue;
8927
8928 if (rel + 1 < relend
8929 && branch_reloc_hash_match (ibfd, rel + 1,
8930 htab->tls_get_addr,
8931 htab->tls_get_addr_fd))
8932 {
8933 if (expecting_tls_get_addr == 2)
8934 {
8935 /* Check for toc tls entries. */
8936 unsigned char *toc_tls;
8937 int retval;
8938
8939 retval = get_tls_mask (&toc_tls, NULL, NULL,
8940 &locsyms,
8941 rel, ibfd);
8942 if (retval == 0)
8943 goto err_free_rel;
8944 if (toc_tls != NULL)
8945 {
8946 if ((*toc_tls & TLS_TLS) != 0
8947 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8948 found_tls_get_addr_arg = 1;
8949 if (retval > 1)
8950 toc_ref[toc_ref_index] = 1;
8951 }
8952 }
8953 continue;
8954 }
8955
8956 /* Uh oh, we didn't find the expected call. We
8957 could just mark this symbol to exclude it
8958 from tls optimization but it's safer to skip
8959 the entire optimization. */
8960 /* xgettext:c-format */
8961 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8962 "TLS optimization disabled\n"),
8963 ibfd, sec, rel->r_offset);
8964 ret = TRUE;
8965 goto err_free_rel;
8966 }
8967
8968 /* If we don't have old-style __tls_get_addr calls
8969 without TLSGD/TLSLD marker relocs, and we haven't
8970 found a new-style __tls_get_addr call with a
8971 marker for this symbol, then we either have a
8972 broken object file or an -mlongcall style
8973 indirect call to __tls_get_addr without a marker.
8974 Disable optimization in this case. */
8975 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8976 && (tls_set & TLS_EXPLICIT) == 0
8977 && !sec->has_tls_get_addr_call
8978 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8979 != (TLS_TLS | TLS_MARK)))
8980 continue;
8981
8982 if (expecting_tls_get_addr)
8983 {
8984 struct plt_entry *ent = NULL;
8985
8986 if (htab->tls_get_addr != NULL)
8987 for (ent = htab->tls_get_addr->elf.plt.plist;
8988 ent != NULL;
8989 ent = ent->next)
8990 if (ent->addend == 0)
8991 break;
8992
8993 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8994 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8995 ent != NULL;
8996 ent = ent->next)
8997 if (ent->addend == 0)
8998 break;
8999
9000 if (ent != NULL
9001 && ent->plt.refcount > 0)
9002 ent->plt.refcount -= 1;
9003 }
9004
9005 if (tls_clear == 0)
9006 continue;
9007
9008 if ((tls_set & TLS_EXPLICIT) == 0)
9009 {
9010 struct got_entry *ent;
9011
9012 /* Adjust got entry for this reloc. */
9013 if (h != NULL)
9014 ent = h->got.glist;
9015 else
9016 ent = elf_local_got_ents (ibfd)[r_symndx];
9017
9018 for (; ent != NULL; ent = ent->next)
9019 if (ent->addend == rel->r_addend
9020 && ent->owner == ibfd
9021 && ent->tls_type == tls_type)
9022 break;
9023 if (ent == NULL)
9024 abort ();
9025
9026 if (tls_set == 0)
9027 {
9028 /* We managed to get rid of a got entry. */
9029 if (ent->got.refcount > 0)
9030 ent->got.refcount -= 1;
9031 }
9032 }
9033 else
9034 {
9035 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9036 we'll lose one or two dyn relocs. */
9037 if (!dec_dynrel_count (rel->r_info, sec, info,
9038 NULL, h, sym))
9039 return FALSE;
9040
9041 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9042 {
9043 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9044 NULL, h, sym))
9045 return FALSE;
9046 }
9047 }
9048
9049 *tls_mask |= tls_set;
9050 *tls_mask &= ~tls_clear;
9051 }
9052
9053 if (elf_section_data (sec)->relocs != relstart)
9054 free (relstart);
9055 }
9056
9057 if (locsyms != NULL
9058 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9059 {
9060 if (!info->keep_memory)
9061 free (locsyms);
9062 else
9063 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9064 }
9065 }
9066
9067 if (toc_ref != NULL)
9068 free (toc_ref);
9069 htab->do_tls_opt = 1;
9070 return TRUE;
9071 }
9072
9073 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9074 the values of any global symbols in a toc section that has been
9075 edited. Globals in toc sections should be a rarity, so this function
9076 sets a flag if any are found in toc sections other than the one just
9077 edited, so that further hash table traversals can be avoided. */
9078
9079 struct adjust_toc_info
9080 {
9081 asection *toc;
9082 unsigned long *skip;
9083 bfd_boolean global_toc_syms;
9084 };
9085
9086 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9087
9088 static bfd_boolean
9089 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9090 {
9091 struct ppc_link_hash_entry *eh;
9092 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9093 unsigned long i;
9094
9095 if (h->root.type != bfd_link_hash_defined
9096 && h->root.type != bfd_link_hash_defweak)
9097 return TRUE;
9098
9099 eh = (struct ppc_link_hash_entry *) h;
9100 if (eh->adjust_done)
9101 return TRUE;
9102
9103 if (eh->elf.root.u.def.section == toc_inf->toc)
9104 {
9105 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9106 i = toc_inf->toc->rawsize >> 3;
9107 else
9108 i = eh->elf.root.u.def.value >> 3;
9109
9110 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9111 {
9112 _bfd_error_handler
9113 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9114 do
9115 ++i;
9116 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9117 eh->elf.root.u.def.value = (bfd_vma) i << 3;
9118 }
9119
9120 eh->elf.root.u.def.value -= toc_inf->skip[i];
9121 eh->adjust_done = 1;
9122 }
9123 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9124 toc_inf->global_toc_syms = TRUE;
9125
9126 return TRUE;
9127 }
9128
9129 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9130 on a _LO variety toc/got reloc. */
9131
9132 static bfd_boolean
9133 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9134 {
9135 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9136 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9137 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9138 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9139 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9140 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9141 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9142 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9143 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9144 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9145 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9146 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9147 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9148 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9149 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9150 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9151 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9152 /* Exclude lfqu by testing reloc. If relocs are ever
9153 defined for the reduced D field in psq_lu then those
9154 will need testing too. */
9155 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9156 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9157 && (insn & 1) == 0)
9158 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9159 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9160 /* Exclude stfqu. psq_stu as above for psq_lu. */
9161 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9162 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9163 && (insn & 1) == 0));
9164 }
9165
9166 /* Examine all relocs referencing .toc sections in order to remove
9167 unused .toc entries. */
9168
9169 bfd_boolean
9170 ppc64_elf_edit_toc (struct bfd_link_info *info)
9171 {
9172 bfd *ibfd;
9173 struct adjust_toc_info toc_inf;
9174 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9175
9176 htab->do_toc_opt = 1;
9177 toc_inf.global_toc_syms = TRUE;
9178 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9179 {
9180 asection *toc, *sec;
9181 Elf_Internal_Shdr *symtab_hdr;
9182 Elf_Internal_Sym *local_syms;
9183 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9184 unsigned long *skip, *drop;
9185 unsigned char *used;
9186 unsigned char *keep, last, some_unused;
9187
9188 if (!is_ppc64_elf (ibfd))
9189 continue;
9190
9191 toc = bfd_get_section_by_name (ibfd, ".toc");
9192 if (toc == NULL
9193 || toc->size == 0
9194 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9195 || discarded_section (toc))
9196 continue;
9197
9198 toc_relocs = NULL;
9199 local_syms = NULL;
9200 symtab_hdr = &elf_symtab_hdr (ibfd);
9201
9202 /* Look at sections dropped from the final link. */
9203 skip = NULL;
9204 relstart = NULL;
9205 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9206 {
9207 if (sec->reloc_count == 0
9208 || !discarded_section (sec)
9209 || get_opd_info (sec)
9210 || (sec->flags & SEC_ALLOC) == 0
9211 || (sec->flags & SEC_DEBUGGING) != 0)
9212 continue;
9213
9214 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9215 if (relstart == NULL)
9216 goto error_ret;
9217
9218 /* Run through the relocs to see which toc entries might be
9219 unused. */
9220 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9221 {
9222 enum elf_ppc64_reloc_type r_type;
9223 unsigned long r_symndx;
9224 asection *sym_sec;
9225 struct elf_link_hash_entry *h;
9226 Elf_Internal_Sym *sym;
9227 bfd_vma val;
9228
9229 r_type = ELF64_R_TYPE (rel->r_info);
9230 switch (r_type)
9231 {
9232 default:
9233 continue;
9234
9235 case R_PPC64_TOC16:
9236 case R_PPC64_TOC16_LO:
9237 case R_PPC64_TOC16_HI:
9238 case R_PPC64_TOC16_HA:
9239 case R_PPC64_TOC16_DS:
9240 case R_PPC64_TOC16_LO_DS:
9241 break;
9242 }
9243
9244 r_symndx = ELF64_R_SYM (rel->r_info);
9245 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9246 r_symndx, ibfd))
9247 goto error_ret;
9248
9249 if (sym_sec != toc)
9250 continue;
9251
9252 if (h != NULL)
9253 val = h->root.u.def.value;
9254 else
9255 val = sym->st_value;
9256 val += rel->r_addend;
9257
9258 if (val >= toc->size)
9259 continue;
9260
9261 /* Anything in the toc ought to be aligned to 8 bytes.
9262 If not, don't mark as unused. */
9263 if (val & 7)
9264 continue;
9265
9266 if (skip == NULL)
9267 {
9268 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9269 if (skip == NULL)
9270 goto error_ret;
9271 }
9272
9273 skip[val >> 3] = ref_from_discarded;
9274 }
9275
9276 if (elf_section_data (sec)->relocs != relstart)
9277 free (relstart);
9278 }
9279
9280 /* For largetoc loads of address constants, we can convert
9281 . addis rx,2,addr@got@ha
9282 . ld ry,addr@got@l(rx)
9283 to
9284 . addis rx,2,addr@toc@ha
9285 . addi ry,rx,addr@toc@l
9286 when addr is within 2G of the toc pointer. This then means
9287 that the word storing "addr" in the toc is no longer needed. */
9288
9289 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9290 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9291 && toc->reloc_count != 0)
9292 {
9293 /* Read toc relocs. */
9294 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9295 info->keep_memory);
9296 if (toc_relocs == NULL)
9297 goto error_ret;
9298
9299 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9300 {
9301 enum elf_ppc64_reloc_type r_type;
9302 unsigned long r_symndx;
9303 asection *sym_sec;
9304 struct elf_link_hash_entry *h;
9305 Elf_Internal_Sym *sym;
9306 bfd_vma val, addr;
9307
9308 r_type = ELF64_R_TYPE (rel->r_info);
9309 if (r_type != R_PPC64_ADDR64)
9310 continue;
9311
9312 r_symndx = ELF64_R_SYM (rel->r_info);
9313 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9314 r_symndx, ibfd))
9315 goto error_ret;
9316
9317 if (sym_sec == NULL
9318 || sym_sec->output_section == NULL
9319 || discarded_section (sym_sec))
9320 continue;
9321
9322 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9323 continue;
9324
9325 if (h != NULL)
9326 {
9327 if (h->type == STT_GNU_IFUNC)
9328 continue;
9329 val = h->root.u.def.value;
9330 }
9331 else
9332 {
9333 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9334 continue;
9335 val = sym->st_value;
9336 }
9337 val += rel->r_addend;
9338 val += sym_sec->output_section->vma + sym_sec->output_offset;
9339
9340 /* We don't yet know the exact toc pointer value, but we
9341 know it will be somewhere in the toc section. Don't
9342 optimize if the difference from any possible toc
9343 pointer is outside [ff..f80008000, 7fff7fff]. */
9344 addr = toc->output_section->vma + TOC_BASE_OFF;
9345 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9346 continue;
9347
9348 addr = toc->output_section->vma + toc->output_section->rawsize;
9349 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9350 continue;
9351
9352 if (skip == NULL)
9353 {
9354 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9355 if (skip == NULL)
9356 goto error_ret;
9357 }
9358
9359 skip[rel->r_offset >> 3]
9360 |= can_optimize | ((rel - toc_relocs) << 2);
9361 }
9362 }
9363
9364 if (skip == NULL)
9365 continue;
9366
9367 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9368 if (used == NULL)
9369 {
9370 error_ret:
9371 if (local_syms != NULL
9372 && symtab_hdr->contents != (unsigned char *) local_syms)
9373 free (local_syms);
9374 if (sec != NULL
9375 && relstart != NULL
9376 && elf_section_data (sec)->relocs != relstart)
9377 free (relstart);
9378 if (toc_relocs != NULL
9379 && elf_section_data (toc)->relocs != toc_relocs)
9380 free (toc_relocs);
9381 if (skip != NULL)
9382 free (skip);
9383 return FALSE;
9384 }
9385
9386 /* Now check all kept sections that might reference the toc.
9387 Check the toc itself last. */
9388 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9389 : ibfd->sections);
9390 sec != NULL;
9391 sec = (sec == toc ? NULL
9392 : sec->next == NULL ? toc
9393 : sec->next == toc && toc->next ? toc->next
9394 : sec->next))
9395 {
9396 int repeat;
9397
9398 if (sec->reloc_count == 0
9399 || discarded_section (sec)
9400 || get_opd_info (sec)
9401 || (sec->flags & SEC_ALLOC) == 0
9402 || (sec->flags & SEC_DEBUGGING) != 0)
9403 continue;
9404
9405 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9406 info->keep_memory);
9407 if (relstart == NULL)
9408 {
9409 free (used);
9410 goto error_ret;
9411 }
9412
9413 /* Mark toc entries referenced as used. */
9414 do
9415 {
9416 repeat = 0;
9417 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9418 {
9419 enum elf_ppc64_reloc_type r_type;
9420 unsigned long r_symndx;
9421 asection *sym_sec;
9422 struct elf_link_hash_entry *h;
9423 Elf_Internal_Sym *sym;
9424 bfd_vma val;
9425 enum {no_check, check_lo, check_ha} insn_check;
9426
9427 r_type = ELF64_R_TYPE (rel->r_info);
9428 switch (r_type)
9429 {
9430 default:
9431 insn_check = no_check;
9432 break;
9433
9434 case R_PPC64_GOT_TLSLD16_HA:
9435 case R_PPC64_GOT_TLSGD16_HA:
9436 case R_PPC64_GOT_TPREL16_HA:
9437 case R_PPC64_GOT_DTPREL16_HA:
9438 case R_PPC64_GOT16_HA:
9439 case R_PPC64_TOC16_HA:
9440 insn_check = check_ha;
9441 break;
9442
9443 case R_PPC64_GOT_TLSLD16_LO:
9444 case R_PPC64_GOT_TLSGD16_LO:
9445 case R_PPC64_GOT_TPREL16_LO_DS:
9446 case R_PPC64_GOT_DTPREL16_LO_DS:
9447 case R_PPC64_GOT16_LO:
9448 case R_PPC64_GOT16_LO_DS:
9449 case R_PPC64_TOC16_LO:
9450 case R_PPC64_TOC16_LO_DS:
9451 insn_check = check_lo;
9452 break;
9453 }
9454
9455 if (insn_check != no_check)
9456 {
9457 bfd_vma off = rel->r_offset & ~3;
9458 unsigned char buf[4];
9459 unsigned int insn;
9460
9461 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9462 {
9463 free (used);
9464 goto error_ret;
9465 }
9466 insn = bfd_get_32 (ibfd, buf);
9467 if (insn_check == check_lo
9468 ? !ok_lo_toc_insn (insn, r_type)
9469 : ((insn & ((0x3f << 26) | 0x1f << 16))
9470 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9471 {
9472 char str[12];
9473
9474 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9475 sprintf (str, "%#08x", insn);
9476 info->callbacks->einfo
9477 /* xgettext:c-format */
9478 (_("%H: toc optimization is not supported for"
9479 " %s instruction\n"),
9480 ibfd, sec, rel->r_offset & ~3, str);
9481 }
9482 }
9483
9484 switch (r_type)
9485 {
9486 case R_PPC64_TOC16:
9487 case R_PPC64_TOC16_LO:
9488 case R_PPC64_TOC16_HI:
9489 case R_PPC64_TOC16_HA:
9490 case R_PPC64_TOC16_DS:
9491 case R_PPC64_TOC16_LO_DS:
9492 /* In case we're taking addresses of toc entries. */
9493 case R_PPC64_ADDR64:
9494 break;
9495
9496 default:
9497 continue;
9498 }
9499
9500 r_symndx = ELF64_R_SYM (rel->r_info);
9501 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9502 r_symndx, ibfd))
9503 {
9504 free (used);
9505 goto error_ret;
9506 }
9507
9508 if (sym_sec != toc)
9509 continue;
9510
9511 if (h != NULL)
9512 val = h->root.u.def.value;
9513 else
9514 val = sym->st_value;
9515 val += rel->r_addend;
9516
9517 if (val >= toc->size)
9518 continue;
9519
9520 if ((skip[val >> 3] & can_optimize) != 0)
9521 {
9522 bfd_vma off;
9523 unsigned char opc;
9524
9525 switch (r_type)
9526 {
9527 case R_PPC64_TOC16_HA:
9528 break;
9529
9530 case R_PPC64_TOC16_LO_DS:
9531 off = rel->r_offset;
9532 off += (bfd_big_endian (ibfd) ? -2 : 3);
9533 if (!bfd_get_section_contents (ibfd, sec, &opc,
9534 off, 1))
9535 {
9536 free (used);
9537 goto error_ret;
9538 }
9539 if ((opc & (0x3f << 2)) == (58u << 2))
9540 break;
9541 /* Fall through. */
9542
9543 default:
9544 /* Wrong sort of reloc, or not a ld. We may
9545 as well clear ref_from_discarded too. */
9546 skip[val >> 3] = 0;
9547 }
9548 }
9549
9550 if (sec != toc)
9551 used[val >> 3] = 1;
9552 /* For the toc section, we only mark as used if this
9553 entry itself isn't unused. */
9554 else if ((used[rel->r_offset >> 3]
9555 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9556 && !used[val >> 3])
9557 {
9558 /* Do all the relocs again, to catch reference
9559 chains. */
9560 repeat = 1;
9561 used[val >> 3] = 1;
9562 }
9563 }
9564 }
9565 while (repeat);
9566
9567 if (elf_section_data (sec)->relocs != relstart)
9568 free (relstart);
9569 }
9570
9571 /* Merge the used and skip arrays. Assume that TOC
9572 doublewords not appearing as either used or unused belong
9573 to an entry more than one doubleword in size. */
9574 for (drop = skip, keep = used, last = 0, some_unused = 0;
9575 drop < skip + (toc->size + 7) / 8;
9576 ++drop, ++keep)
9577 {
9578 if (*keep)
9579 {
9580 *drop &= ~ref_from_discarded;
9581 if ((*drop & can_optimize) != 0)
9582 some_unused = 1;
9583 last = 0;
9584 }
9585 else if ((*drop & ref_from_discarded) != 0)
9586 {
9587 some_unused = 1;
9588 last = ref_from_discarded;
9589 }
9590 else
9591 *drop = last;
9592 }
9593
9594 free (used);
9595
9596 if (some_unused)
9597 {
9598 bfd_byte *contents, *src;
9599 unsigned long off;
9600 Elf_Internal_Sym *sym;
9601 bfd_boolean local_toc_syms = FALSE;
9602
9603 /* Shuffle the toc contents, and at the same time convert the
9604 skip array from booleans into offsets. */
9605 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9606 goto error_ret;
9607
9608 elf_section_data (toc)->this_hdr.contents = contents;
9609
9610 for (src = contents, off = 0, drop = skip;
9611 src < contents + toc->size;
9612 src += 8, ++drop)
9613 {
9614 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9615 off += 8;
9616 else if (off != 0)
9617 {
9618 *drop = off;
9619 memcpy (src - off, src, 8);
9620 }
9621 }
9622 *drop = off;
9623 toc->rawsize = toc->size;
9624 toc->size = src - contents - off;
9625
9626 /* Adjust addends for relocs against the toc section sym,
9627 and optimize any accesses we can. */
9628 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9629 {
9630 if (sec->reloc_count == 0
9631 || discarded_section (sec))
9632 continue;
9633
9634 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9635 info->keep_memory);
9636 if (relstart == NULL)
9637 goto error_ret;
9638
9639 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9640 {
9641 enum elf_ppc64_reloc_type r_type;
9642 unsigned long r_symndx;
9643 asection *sym_sec;
9644 struct elf_link_hash_entry *h;
9645 bfd_vma val;
9646
9647 r_type = ELF64_R_TYPE (rel->r_info);
9648 switch (r_type)
9649 {
9650 default:
9651 continue;
9652
9653 case R_PPC64_TOC16:
9654 case R_PPC64_TOC16_LO:
9655 case R_PPC64_TOC16_HI:
9656 case R_PPC64_TOC16_HA:
9657 case R_PPC64_TOC16_DS:
9658 case R_PPC64_TOC16_LO_DS:
9659 case R_PPC64_ADDR64:
9660 break;
9661 }
9662
9663 r_symndx = ELF64_R_SYM (rel->r_info);
9664 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9665 r_symndx, ibfd))
9666 goto error_ret;
9667
9668 if (sym_sec != toc)
9669 continue;
9670
9671 if (h != NULL)
9672 val = h->root.u.def.value;
9673 else
9674 {
9675 val = sym->st_value;
9676 if (val != 0)
9677 local_toc_syms = TRUE;
9678 }
9679
9680 val += rel->r_addend;
9681
9682 if (val > toc->rawsize)
9683 val = toc->rawsize;
9684 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9685 continue;
9686 else if ((skip[val >> 3] & can_optimize) != 0)
9687 {
9688 Elf_Internal_Rela *tocrel
9689 = toc_relocs + (skip[val >> 3] >> 2);
9690 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9691
9692 switch (r_type)
9693 {
9694 case R_PPC64_TOC16_HA:
9695 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9696 break;
9697
9698 case R_PPC64_TOC16_LO_DS:
9699 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9700 break;
9701
9702 default:
9703 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9704 ppc_howto_init ();
9705 info->callbacks->einfo
9706 /* xgettext:c-format */
9707 (_("%H: %s references "
9708 "optimized away TOC entry\n"),
9709 ibfd, sec, rel->r_offset,
9710 ppc64_elf_howto_table[r_type]->name);
9711 bfd_set_error (bfd_error_bad_value);
9712 goto error_ret;
9713 }
9714 rel->r_addend = tocrel->r_addend;
9715 elf_section_data (sec)->relocs = relstart;
9716 continue;
9717 }
9718
9719 if (h != NULL || sym->st_value != 0)
9720 continue;
9721
9722 rel->r_addend -= skip[val >> 3];
9723 elf_section_data (sec)->relocs = relstart;
9724 }
9725
9726 if (elf_section_data (sec)->relocs != relstart)
9727 free (relstart);
9728 }
9729
9730 /* We shouldn't have local or global symbols defined in the TOC,
9731 but handle them anyway. */
9732 if (local_syms != NULL)
9733 for (sym = local_syms;
9734 sym < local_syms + symtab_hdr->sh_info;
9735 ++sym)
9736 if (sym->st_value != 0
9737 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9738 {
9739 unsigned long i;
9740
9741 if (sym->st_value > toc->rawsize)
9742 i = toc->rawsize >> 3;
9743 else
9744 i = sym->st_value >> 3;
9745
9746 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9747 {
9748 if (local_toc_syms)
9749 _bfd_error_handler
9750 (_("%s defined on removed toc entry"),
9751 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9752 do
9753 ++i;
9754 while ((skip[i] & (ref_from_discarded | can_optimize)));
9755 sym->st_value = (bfd_vma) i << 3;
9756 }
9757
9758 sym->st_value -= skip[i];
9759 symtab_hdr->contents = (unsigned char *) local_syms;
9760 }
9761
9762 /* Adjust any global syms defined in this toc input section. */
9763 if (toc_inf.global_toc_syms)
9764 {
9765 toc_inf.toc = toc;
9766 toc_inf.skip = skip;
9767 toc_inf.global_toc_syms = FALSE;
9768 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9769 &toc_inf);
9770 }
9771
9772 if (toc->reloc_count != 0)
9773 {
9774 Elf_Internal_Shdr *rel_hdr;
9775 Elf_Internal_Rela *wrel;
9776 bfd_size_type sz;
9777
9778 /* Remove unused toc relocs, and adjust those we keep. */
9779 if (toc_relocs == NULL)
9780 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9781 info->keep_memory);
9782 if (toc_relocs == NULL)
9783 goto error_ret;
9784
9785 wrel = toc_relocs;
9786 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9787 if ((skip[rel->r_offset >> 3]
9788 & (ref_from_discarded | can_optimize)) == 0)
9789 {
9790 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9791 wrel->r_info = rel->r_info;
9792 wrel->r_addend = rel->r_addend;
9793 ++wrel;
9794 }
9795 else if (!dec_dynrel_count (rel->r_info, toc, info,
9796 &local_syms, NULL, NULL))
9797 goto error_ret;
9798
9799 elf_section_data (toc)->relocs = toc_relocs;
9800 toc->reloc_count = wrel - toc_relocs;
9801 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9802 sz = rel_hdr->sh_entsize;
9803 rel_hdr->sh_size = toc->reloc_count * sz;
9804 }
9805 }
9806 else if (toc_relocs != NULL
9807 && elf_section_data (toc)->relocs != toc_relocs)
9808 free (toc_relocs);
9809
9810 if (local_syms != NULL
9811 && symtab_hdr->contents != (unsigned char *) local_syms)
9812 {
9813 if (!info->keep_memory)
9814 free (local_syms);
9815 else
9816 symtab_hdr->contents = (unsigned char *) local_syms;
9817 }
9818 free (skip);
9819 }
9820
9821 return TRUE;
9822 }
9823
9824 /* Return true iff input section I references the TOC using
9825 instructions limited to +/-32k offsets. */
9826
9827 bfd_boolean
9828 ppc64_elf_has_small_toc_reloc (asection *i)
9829 {
9830 return (is_ppc64_elf (i->owner)
9831 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9832 }
9833
9834 /* Allocate space for one GOT entry. */
9835
9836 static void
9837 allocate_got (struct elf_link_hash_entry *h,
9838 struct bfd_link_info *info,
9839 struct got_entry *gent)
9840 {
9841 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9842 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9843 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9844 ? 16 : 8);
9845 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9846 ? 2 : 1) * sizeof (Elf64_External_Rela);
9847 asection *got = ppc64_elf_tdata (gent->owner)->got;
9848
9849 gent->got.offset = got->size;
9850 got->size += entsize;
9851
9852 if (h->type == STT_GNU_IFUNC)
9853 {
9854 htab->elf.irelplt->size += rentsize;
9855 htab->got_reli_size += rentsize;
9856 }
9857 else if (((bfd_link_pic (info)
9858 && !((gent->tls_type & TLS_TPREL) != 0
9859 && bfd_link_executable (info)
9860 && SYMBOL_REFERENCES_LOCAL (info, h)))
9861 || (htab->elf.dynamic_sections_created
9862 && h->dynindx != -1
9863 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9864 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9865 {
9866 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9867 relgot->size += rentsize;
9868 }
9869 }
9870
9871 /* This function merges got entries in the same toc group. */
9872
9873 static void
9874 merge_got_entries (struct got_entry **pent)
9875 {
9876 struct got_entry *ent, *ent2;
9877
9878 for (ent = *pent; ent != NULL; ent = ent->next)
9879 if (!ent->is_indirect)
9880 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9881 if (!ent2->is_indirect
9882 && ent2->addend == ent->addend
9883 && ent2->tls_type == ent->tls_type
9884 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9885 {
9886 ent2->is_indirect = TRUE;
9887 ent2->got.ent = ent;
9888 }
9889 }
9890
9891 /* If H is undefined, make it dynamic if that makes sense. */
9892
9893 static bfd_boolean
9894 ensure_undef_dynamic (struct bfd_link_info *info,
9895 struct elf_link_hash_entry *h)
9896 {
9897 struct elf_link_hash_table *htab = elf_hash_table (info);
9898
9899 if (htab->dynamic_sections_created
9900 && ((info->dynamic_undefined_weak != 0
9901 && h->root.type == bfd_link_hash_undefweak)
9902 || h->root.type == bfd_link_hash_undefined)
9903 && h->dynindx == -1
9904 && !h->forced_local
9905 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9906 return bfd_elf_link_record_dynamic_symbol (info, h);
9907 return TRUE;
9908 }
9909
9910 /* Allocate space in .plt, .got and associated reloc sections for
9911 dynamic relocs. */
9912
9913 static bfd_boolean
9914 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9915 {
9916 struct bfd_link_info *info;
9917 struct ppc_link_hash_table *htab;
9918 asection *s;
9919 struct ppc_link_hash_entry *eh;
9920 struct got_entry **pgent, *gent;
9921
9922 if (h->root.type == bfd_link_hash_indirect)
9923 return TRUE;
9924
9925 info = (struct bfd_link_info *) inf;
9926 htab = ppc_hash_table (info);
9927 if (htab == NULL)
9928 return FALSE;
9929
9930 eh = (struct ppc_link_hash_entry *) h;
9931 /* Run through the TLS GD got entries first if we're changing them
9932 to TPREL. */
9933 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9934 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9935 if (gent->got.refcount > 0
9936 && (gent->tls_type & TLS_GD) != 0)
9937 {
9938 /* This was a GD entry that has been converted to TPREL. If
9939 there happens to be a TPREL entry we can use that one. */
9940 struct got_entry *ent;
9941 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9942 if (ent->got.refcount > 0
9943 && (ent->tls_type & TLS_TPREL) != 0
9944 && ent->addend == gent->addend
9945 && ent->owner == gent->owner)
9946 {
9947 gent->got.refcount = 0;
9948 break;
9949 }
9950
9951 /* If not, then we'll be using our own TPREL entry. */
9952 if (gent->got.refcount != 0)
9953 gent->tls_type = TLS_TLS | TLS_TPREL;
9954 }
9955
9956 /* Remove any list entry that won't generate a word in the GOT before
9957 we call merge_got_entries. Otherwise we risk merging to empty
9958 entries. */
9959 pgent = &h->got.glist;
9960 while ((gent = *pgent) != NULL)
9961 if (gent->got.refcount > 0)
9962 {
9963 if ((gent->tls_type & TLS_LD) != 0
9964 && !h->def_dynamic)
9965 {
9966 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9967 *pgent = gent->next;
9968 }
9969 else
9970 pgent = &gent->next;
9971 }
9972 else
9973 *pgent = gent->next;
9974
9975 if (!htab->do_multi_toc)
9976 merge_got_entries (&h->got.glist);
9977
9978 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9979 if (!gent->is_indirect)
9980 {
9981 /* Make sure this symbol is output as a dynamic symbol. */
9982 if (!ensure_undef_dynamic (info, h))
9983 return FALSE;
9984
9985 if (!is_ppc64_elf (gent->owner))
9986 abort ();
9987
9988 allocate_got (h, info, gent);
9989 }
9990
9991 /* If no dynamic sections we can't have dynamic relocs, except for
9992 IFUNCs which are handled even in static executables. */
9993 if (!htab->elf.dynamic_sections_created
9994 && h->type != STT_GNU_IFUNC)
9995 eh->dyn_relocs = NULL;
9996
9997 /* Discard relocs on undefined symbols that must be local. */
9998 else if (h->root.type == bfd_link_hash_undefined
9999 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10000 eh->dyn_relocs = NULL;
10001
10002 /* Also discard relocs on undefined weak syms with non-default
10003 visibility, or when dynamic_undefined_weak says so. */
10004 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10005 eh->dyn_relocs = NULL;
10006
10007 if (eh->dyn_relocs != NULL)
10008 {
10009 struct elf_dyn_relocs *p, **pp;
10010
10011 /* In the shared -Bsymbolic case, discard space allocated for
10012 dynamic pc-relative relocs against symbols which turn out to
10013 be defined in regular objects. For the normal shared case,
10014 discard space for relocs that have become local due to symbol
10015 visibility changes. */
10016
10017 if (bfd_link_pic (info))
10018 {
10019 /* Relocs that use pc_count are those that appear on a call
10020 insn, or certain REL relocs (see must_be_dyn_reloc) that
10021 can be generated via assembly. We want calls to
10022 protected symbols to resolve directly to the function
10023 rather than going via the plt. If people want function
10024 pointer comparisons to work as expected then they should
10025 avoid writing weird assembly. */
10026 if (SYMBOL_CALLS_LOCAL (info, h))
10027 {
10028 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10029 {
10030 p->count -= p->pc_count;
10031 p->pc_count = 0;
10032 if (p->count == 0)
10033 *pp = p->next;
10034 else
10035 pp = &p->next;
10036 }
10037 }
10038
10039 if (eh->dyn_relocs != NULL)
10040 {
10041 /* Make sure this symbol is output as a dynamic symbol. */
10042 if (!ensure_undef_dynamic (info, h))
10043 return FALSE;
10044 }
10045 }
10046 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10047 {
10048 /* For the non-pic case, discard space for relocs against
10049 symbols which turn out to need copy relocs or are not
10050 dynamic. */
10051 if (h->dynamic_adjusted
10052 && !h->def_regular
10053 && !ELF_COMMON_DEF_P (h))
10054 {
10055 /* Make sure this symbol is output as a dynamic symbol. */
10056 if (!ensure_undef_dynamic (info, h))
10057 return FALSE;
10058
10059 if (h->dynindx == -1)
10060 eh->dyn_relocs = NULL;
10061 }
10062 else
10063 eh->dyn_relocs = NULL;
10064 }
10065
10066 /* Finally, allocate space. */
10067 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10068 {
10069 asection *sreloc = elf_section_data (p->sec)->sreloc;
10070 if (eh->elf.type == STT_GNU_IFUNC)
10071 sreloc = htab->elf.irelplt;
10072 sreloc->size += p->count * sizeof (Elf64_External_Rela);
10073 }
10074 }
10075
10076 /* We might need a PLT entry when the symbol
10077 a) is dynamic, or
10078 b) is an ifunc, or
10079 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10080 d) has plt16 relocs and we are linking statically. */
10081 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10082 || h->type == STT_GNU_IFUNC
10083 || (h->needs_plt && h->dynamic_adjusted)
10084 || (h->needs_plt
10085 && h->def_regular
10086 && !htab->elf.dynamic_sections_created
10087 && !htab->can_convert_all_inline_plt
10088 && (((struct ppc_link_hash_entry *) h)->tls_mask
10089 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10090 {
10091 struct plt_entry *pent;
10092 bfd_boolean doneone = FALSE;
10093 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10094 if (pent->plt.refcount > 0)
10095 {
10096 if (!htab->elf.dynamic_sections_created
10097 || h->dynindx == -1)
10098 {
10099 if (h->type == STT_GNU_IFUNC)
10100 {
10101 s = htab->elf.iplt;
10102 pent->plt.offset = s->size;
10103 s->size += PLT_ENTRY_SIZE (htab);
10104 s = htab->elf.irelplt;
10105 }
10106 else
10107 {
10108 s = htab->pltlocal;
10109 pent->plt.offset = s->size;
10110 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10111 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10112 }
10113 }
10114 else
10115 {
10116 /* If this is the first .plt entry, make room for the special
10117 first entry. */
10118 s = htab->elf.splt;
10119 if (s->size == 0)
10120 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10121
10122 pent->plt.offset = s->size;
10123
10124 /* Make room for this entry. */
10125 s->size += PLT_ENTRY_SIZE (htab);
10126
10127 /* Make room for the .glink code. */
10128 s = htab->glink;
10129 if (s->size == 0)
10130 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10131 if (htab->opd_abi)
10132 {
10133 /* We need bigger stubs past index 32767. */
10134 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10135 s->size += 4;
10136 s->size += 2*4;
10137 }
10138 else
10139 s->size += 4;
10140
10141 /* We also need to make an entry in the .rela.plt section. */
10142 s = htab->elf.srelplt;
10143 }
10144 if (s != NULL)
10145 s->size += sizeof (Elf64_External_Rela);
10146 doneone = TRUE;
10147 }
10148 else
10149 pent->plt.offset = (bfd_vma) -1;
10150 if (!doneone)
10151 {
10152 h->plt.plist = NULL;
10153 h->needs_plt = 0;
10154 }
10155 }
10156 else
10157 {
10158 h->plt.plist = NULL;
10159 h->needs_plt = 0;
10160 }
10161
10162 return TRUE;
10163 }
10164
10165 #define PPC_LO(v) ((v) & 0xffff)
10166 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10167 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10168
10169 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10170 to set up space for global entry stubs. These are put in glink,
10171 after the branch table. */
10172
10173 static bfd_boolean
10174 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10175 {
10176 struct bfd_link_info *info;
10177 struct ppc_link_hash_table *htab;
10178 struct plt_entry *pent;
10179 asection *s, *plt;
10180
10181 if (h->root.type == bfd_link_hash_indirect)
10182 return TRUE;
10183
10184 if (!h->pointer_equality_needed)
10185 return TRUE;
10186
10187 if (h->def_regular)
10188 return TRUE;
10189
10190 info = inf;
10191 htab = ppc_hash_table (info);
10192 if (htab == NULL)
10193 return FALSE;
10194
10195 s = htab->global_entry;
10196 plt = htab->elf.splt;
10197 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10198 if (pent->plt.offset != (bfd_vma) -1
10199 && pent->addend == 0)
10200 {
10201 /* For ELFv2, if this symbol is not defined in a regular file
10202 and we are not generating a shared library or pie, then we
10203 need to define the symbol in the executable on a call stub.
10204 This is to avoid text relocations. */
10205 bfd_vma off, stub_align, stub_off, stub_size;
10206 unsigned int align_power;
10207
10208 stub_size = 16;
10209 stub_off = s->size;
10210 if (htab->params->plt_stub_align >= 0)
10211 align_power = htab->params->plt_stub_align;
10212 else
10213 align_power = -htab->params->plt_stub_align;
10214 /* Setting section alignment is delayed until we know it is
10215 non-empty. Otherwise the .text output section will be
10216 aligned at least to plt_stub_align even when no global
10217 entry stubs are needed. */
10218 if (s->alignment_power < align_power)
10219 s->alignment_power = align_power;
10220 stub_align = (bfd_vma) 1 << align_power;
10221 if (htab->params->plt_stub_align >= 0
10222 || ((((stub_off + stub_size - 1) & -stub_align)
10223 - (stub_off & -stub_align))
10224 > ((stub_size - 1) & -stub_align)))
10225 stub_off = (stub_off + stub_align - 1) & -stub_align;
10226 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10227 off -= stub_off + s->output_offset + s->output_section->vma;
10228 /* Note that for --plt-stub-align negative we have a possible
10229 dependency between stub offset and size. Break that
10230 dependency by assuming the max stub size when calculating
10231 the stub offset. */
10232 if (PPC_HA (off) == 0)
10233 stub_size -= 4;
10234 h->root.type = bfd_link_hash_defined;
10235 h->root.u.def.section = s;
10236 h->root.u.def.value = stub_off;
10237 s->size = stub_off + stub_size;
10238 break;
10239 }
10240 return TRUE;
10241 }
10242
10243 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10244 read-only sections. */
10245
10246 static bfd_boolean
10247 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10248 {
10249 asection *sec;
10250
10251 if (h->root.type == bfd_link_hash_indirect)
10252 return TRUE;
10253
10254 sec = readonly_dynrelocs (h);
10255 if (sec != NULL)
10256 {
10257 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10258
10259 info->flags |= DF_TEXTREL;
10260 info->callbacks->minfo
10261 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10262 sec->owner, h->root.root.string, sec);
10263
10264 /* Not an error, just cut short the traversal. */
10265 return FALSE;
10266 }
10267 return TRUE;
10268 }
10269
10270 /* Set the sizes of the dynamic sections. */
10271
10272 static bfd_boolean
10273 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10274 struct bfd_link_info *info)
10275 {
10276 struct ppc_link_hash_table *htab;
10277 bfd *dynobj;
10278 asection *s;
10279 bfd_boolean relocs;
10280 bfd *ibfd;
10281 struct got_entry *first_tlsld;
10282
10283 htab = ppc_hash_table (info);
10284 if (htab == NULL)
10285 return FALSE;
10286
10287 dynobj = htab->elf.dynobj;
10288 if (dynobj == NULL)
10289 abort ();
10290
10291 if (htab->elf.dynamic_sections_created)
10292 {
10293 /* Set the contents of the .interp section to the interpreter. */
10294 if (bfd_link_executable (info) && !info->nointerp)
10295 {
10296 s = bfd_get_linker_section (dynobj, ".interp");
10297 if (s == NULL)
10298 abort ();
10299 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10300 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10301 }
10302 }
10303
10304 /* Set up .got offsets for local syms, and space for local dynamic
10305 relocs. */
10306 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10307 {
10308 struct got_entry **lgot_ents;
10309 struct got_entry **end_lgot_ents;
10310 struct plt_entry **local_plt;
10311 struct plt_entry **end_local_plt;
10312 unsigned char *lgot_masks;
10313 bfd_size_type locsymcount;
10314 Elf_Internal_Shdr *symtab_hdr;
10315
10316 if (!is_ppc64_elf (ibfd))
10317 continue;
10318
10319 for (s = ibfd->sections; s != NULL; s = s->next)
10320 {
10321 struct ppc_dyn_relocs *p;
10322
10323 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10324 {
10325 if (!bfd_is_abs_section (p->sec)
10326 && bfd_is_abs_section (p->sec->output_section))
10327 {
10328 /* Input section has been discarded, either because
10329 it is a copy of a linkonce section or due to
10330 linker script /DISCARD/, so we'll be discarding
10331 the relocs too. */
10332 }
10333 else if (p->count != 0)
10334 {
10335 asection *srel = elf_section_data (p->sec)->sreloc;
10336 if (p->ifunc)
10337 srel = htab->elf.irelplt;
10338 srel->size += p->count * sizeof (Elf64_External_Rela);
10339 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10340 info->flags |= DF_TEXTREL;
10341 }
10342 }
10343 }
10344
10345 lgot_ents = elf_local_got_ents (ibfd);
10346 if (!lgot_ents)
10347 continue;
10348
10349 symtab_hdr = &elf_symtab_hdr (ibfd);
10350 locsymcount = symtab_hdr->sh_info;
10351 end_lgot_ents = lgot_ents + locsymcount;
10352 local_plt = (struct plt_entry **) end_lgot_ents;
10353 end_local_plt = local_plt + locsymcount;
10354 lgot_masks = (unsigned char *) end_local_plt;
10355 s = ppc64_elf_tdata (ibfd)->got;
10356 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10357 {
10358 struct got_entry **pent, *ent;
10359
10360 pent = lgot_ents;
10361 while ((ent = *pent) != NULL)
10362 if (ent->got.refcount > 0)
10363 {
10364 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10365 {
10366 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10367 *pent = ent->next;
10368 }
10369 else
10370 {
10371 unsigned int ent_size = 8;
10372 unsigned int rel_size = sizeof (Elf64_External_Rela);
10373
10374 ent->got.offset = s->size;
10375 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10376 {
10377 ent_size *= 2;
10378 rel_size *= 2;
10379 }
10380 s->size += ent_size;
10381 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10382 {
10383 htab->elf.irelplt->size += rel_size;
10384 htab->got_reli_size += rel_size;
10385 }
10386 else if (bfd_link_pic (info)
10387 && !((ent->tls_type & TLS_TPREL) != 0
10388 && bfd_link_executable (info)))
10389 {
10390 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10391 srel->size += rel_size;
10392 }
10393 pent = &ent->next;
10394 }
10395 }
10396 else
10397 *pent = ent->next;
10398 }
10399
10400 /* Allocate space for plt calls to local syms. */
10401 lgot_masks = (unsigned char *) end_local_plt;
10402 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10403 {
10404 struct plt_entry *ent;
10405
10406 for (ent = *local_plt; ent != NULL; ent = ent->next)
10407 if (ent->plt.refcount > 0)
10408 {
10409 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10410 {
10411 s = htab->elf.iplt;
10412 ent->plt.offset = s->size;
10413 s->size += PLT_ENTRY_SIZE (htab);
10414 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10415 }
10416 else if (htab->can_convert_all_inline_plt
10417 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10418 ent->plt.offset = (bfd_vma) -1;
10419 else
10420 {
10421 s = htab->pltlocal;
10422 ent->plt.offset = s->size;
10423 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10424 if (bfd_link_pic (info))
10425 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10426 }
10427 }
10428 else
10429 ent->plt.offset = (bfd_vma) -1;
10430 }
10431 }
10432
10433 /* Allocate global sym .plt and .got entries, and space for global
10434 sym dynamic relocs. */
10435 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10436
10437 if (!htab->opd_abi && !bfd_link_pic (info))
10438 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10439
10440 first_tlsld = NULL;
10441 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10442 {
10443 struct got_entry *ent;
10444
10445 if (!is_ppc64_elf (ibfd))
10446 continue;
10447
10448 ent = ppc64_tlsld_got (ibfd);
10449 if (ent->got.refcount > 0)
10450 {
10451 if (!htab->do_multi_toc && first_tlsld != NULL)
10452 {
10453 ent->is_indirect = TRUE;
10454 ent->got.ent = first_tlsld;
10455 }
10456 else
10457 {
10458 if (first_tlsld == NULL)
10459 first_tlsld = ent;
10460 s = ppc64_elf_tdata (ibfd)->got;
10461 ent->got.offset = s->size;
10462 ent->owner = ibfd;
10463 s->size += 16;
10464 if (bfd_link_pic (info))
10465 {
10466 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10467 srel->size += sizeof (Elf64_External_Rela);
10468 }
10469 }
10470 }
10471 else
10472 ent->got.offset = (bfd_vma) -1;
10473 }
10474
10475 /* We now have determined the sizes of the various dynamic sections.
10476 Allocate memory for them. */
10477 relocs = FALSE;
10478 for (s = dynobj->sections; s != NULL; s = s->next)
10479 {
10480 if ((s->flags & SEC_LINKER_CREATED) == 0)
10481 continue;
10482
10483 if (s == htab->brlt || s == htab->relbrlt)
10484 /* These haven't been allocated yet; don't strip. */
10485 continue;
10486 else if (s == htab->elf.sgot
10487 || s == htab->elf.splt
10488 || s == htab->elf.iplt
10489 || s == htab->pltlocal
10490 || s == htab->glink
10491 || s == htab->global_entry
10492 || s == htab->elf.sdynbss
10493 || s == htab->elf.sdynrelro)
10494 {
10495 /* Strip this section if we don't need it; see the
10496 comment below. */
10497 }
10498 else if (s == htab->glink_eh_frame)
10499 {
10500 if (!bfd_is_abs_section (s->output_section))
10501 /* Not sized yet. */
10502 continue;
10503 }
10504 else if (CONST_STRNEQ (s->name, ".rela"))
10505 {
10506 if (s->size != 0)
10507 {
10508 if (s != htab->elf.srelplt)
10509 relocs = TRUE;
10510
10511 /* We use the reloc_count field as a counter if we need
10512 to copy relocs into the output file. */
10513 s->reloc_count = 0;
10514 }
10515 }
10516 else
10517 {
10518 /* It's not one of our sections, so don't allocate space. */
10519 continue;
10520 }
10521
10522 if (s->size == 0)
10523 {
10524 /* If we don't need this section, strip it from the
10525 output file. This is mostly to handle .rela.bss and
10526 .rela.plt. We must create both sections in
10527 create_dynamic_sections, because they must be created
10528 before the linker maps input sections to output
10529 sections. The linker does that before
10530 adjust_dynamic_symbol is called, and it is that
10531 function which decides whether anything needs to go
10532 into these sections. */
10533 s->flags |= SEC_EXCLUDE;
10534 continue;
10535 }
10536
10537 if (bfd_is_abs_section (s->output_section))
10538 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10539 s->name);
10540
10541 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10542 continue;
10543
10544 /* Allocate memory for the section contents. We use bfd_zalloc
10545 here in case unused entries are not reclaimed before the
10546 section's contents are written out. This should not happen,
10547 but this way if it does we get a R_PPC64_NONE reloc in .rela
10548 sections instead of garbage.
10549 We also rely on the section contents being zero when writing
10550 the GOT and .dynrelro. */
10551 s->contents = bfd_zalloc (dynobj, s->size);
10552 if (s->contents == NULL)
10553 return FALSE;
10554 }
10555
10556 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10557 {
10558 if (!is_ppc64_elf (ibfd))
10559 continue;
10560
10561 s = ppc64_elf_tdata (ibfd)->got;
10562 if (s != NULL && s != htab->elf.sgot)
10563 {
10564 if (s->size == 0)
10565 s->flags |= SEC_EXCLUDE;
10566 else
10567 {
10568 s->contents = bfd_zalloc (ibfd, s->size);
10569 if (s->contents == NULL)
10570 return FALSE;
10571 }
10572 }
10573 s = ppc64_elf_tdata (ibfd)->relgot;
10574 if (s != NULL)
10575 {
10576 if (s->size == 0)
10577 s->flags |= SEC_EXCLUDE;
10578 else
10579 {
10580 s->contents = bfd_zalloc (ibfd, s->size);
10581 if (s->contents == NULL)
10582 return FALSE;
10583 relocs = TRUE;
10584 s->reloc_count = 0;
10585 }
10586 }
10587 }
10588
10589 if (htab->elf.dynamic_sections_created)
10590 {
10591 bfd_boolean tls_opt;
10592
10593 /* Add some entries to the .dynamic section. We fill in the
10594 values later, in ppc64_elf_finish_dynamic_sections, but we
10595 must add the entries now so that we get the correct size for
10596 the .dynamic section. The DT_DEBUG entry is filled in by the
10597 dynamic linker and used by the debugger. */
10598 #define add_dynamic_entry(TAG, VAL) \
10599 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10600
10601 if (bfd_link_executable (info))
10602 {
10603 if (!add_dynamic_entry (DT_DEBUG, 0))
10604 return FALSE;
10605 }
10606
10607 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10608 {
10609 if (!add_dynamic_entry (DT_PLTGOT, 0)
10610 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10611 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10612 || !add_dynamic_entry (DT_JMPREL, 0)
10613 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10614 return FALSE;
10615 }
10616
10617 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10618 {
10619 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10620 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10621 return FALSE;
10622 }
10623
10624 tls_opt = (htab->params->tls_get_addr_opt
10625 && htab->tls_get_addr_fd != NULL
10626 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10627 if (tls_opt || !htab->opd_abi)
10628 {
10629 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10630 return FALSE;
10631 }
10632
10633 if (relocs)
10634 {
10635 if (!add_dynamic_entry (DT_RELA, 0)
10636 || !add_dynamic_entry (DT_RELASZ, 0)
10637 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10638 return FALSE;
10639
10640 /* If any dynamic relocs apply to a read-only section,
10641 then we need a DT_TEXTREL entry. */
10642 if ((info->flags & DF_TEXTREL) == 0)
10643 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10644
10645 if ((info->flags & DF_TEXTREL) != 0)
10646 {
10647 if (!add_dynamic_entry (DT_TEXTREL, 0))
10648 return FALSE;
10649 }
10650 }
10651 }
10652 #undef add_dynamic_entry
10653
10654 return TRUE;
10655 }
10656
10657 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10658
10659 static bfd_boolean
10660 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10661 {
10662 if (h->plt.plist != NULL
10663 && !h->def_regular
10664 && !h->pointer_equality_needed)
10665 return FALSE;
10666
10667 return _bfd_elf_hash_symbol (h);
10668 }
10669
10670 /* Determine the type of stub needed, if any, for a call. */
10671
10672 static inline enum ppc_stub_type
10673 ppc_type_of_stub (asection *input_sec,
10674 const Elf_Internal_Rela *rel,
10675 struct ppc_link_hash_entry **hash,
10676 struct plt_entry **plt_ent,
10677 bfd_vma destination,
10678 unsigned long local_off)
10679 {
10680 struct ppc_link_hash_entry *h = *hash;
10681 bfd_vma location;
10682 bfd_vma branch_offset;
10683 bfd_vma max_branch_offset;
10684 enum elf_ppc64_reloc_type r_type;
10685
10686 if (h != NULL)
10687 {
10688 struct plt_entry *ent;
10689 struct ppc_link_hash_entry *fdh = h;
10690 if (h->oh != NULL
10691 && h->oh->is_func_descriptor)
10692 {
10693 fdh = ppc_follow_link (h->oh);
10694 *hash = fdh;
10695 }
10696
10697 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10698 if (ent->addend == rel->r_addend
10699 && ent->plt.offset != (bfd_vma) -1)
10700 {
10701 *plt_ent = ent;
10702 return ppc_stub_plt_call;
10703 }
10704
10705 /* Here, we know we don't have a plt entry. If we don't have a
10706 either a defined function descriptor or a defined entry symbol
10707 in a regular object file, then it is pointless trying to make
10708 any other type of stub. */
10709 if (!is_static_defined (&fdh->elf)
10710 && !is_static_defined (&h->elf))
10711 return ppc_stub_none;
10712 }
10713 else if (elf_local_got_ents (input_sec->owner) != NULL)
10714 {
10715 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10716 struct plt_entry **local_plt = (struct plt_entry **)
10717 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10718 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10719
10720 if (local_plt[r_symndx] != NULL)
10721 {
10722 struct plt_entry *ent;
10723
10724 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10725 if (ent->addend == rel->r_addend
10726 && ent->plt.offset != (bfd_vma) -1)
10727 {
10728 *plt_ent = ent;
10729 return ppc_stub_plt_call;
10730 }
10731 }
10732 }
10733
10734 /* Determine where the call point is. */
10735 location = (input_sec->output_offset
10736 + input_sec->output_section->vma
10737 + rel->r_offset);
10738
10739 branch_offset = destination - location;
10740 r_type = ELF64_R_TYPE (rel->r_info);
10741
10742 /* Determine if a long branch stub is needed. */
10743 max_branch_offset = 1 << 25;
10744 if (r_type == R_PPC64_REL14
10745 || r_type == R_PPC64_REL14_BRTAKEN
10746 || r_type == R_PPC64_REL14_BRNTAKEN)
10747 max_branch_offset = 1 << 15;
10748
10749 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10750 /* We need a stub. Figure out whether a long_branch or plt_branch
10751 is needed later. */
10752 return ppc_stub_long_branch;
10753
10754 return ppc_stub_none;
10755 }
10756
10757 /* With power7 weakly ordered memory model, it is possible for ld.so
10758 to update a plt entry in one thread and have another thread see a
10759 stale zero toc entry. To avoid this we need some sort of acquire
10760 barrier in the call stub. One solution is to make the load of the
10761 toc word seem to appear to depend on the load of the function entry
10762 word. Another solution is to test for r2 being zero, and branch to
10763 the appropriate glink entry if so.
10764
10765 . fake dep barrier compare
10766 . ld 12,xxx(2) ld 12,xxx(2)
10767 . mtctr 12 mtctr 12
10768 . xor 11,12,12 ld 2,xxx+8(2)
10769 . add 2,2,11 cmpldi 2,0
10770 . ld 2,xxx+8(2) bnectr+
10771 . bctr b <glink_entry>
10772
10773 The solution involving the compare turns out to be faster, so
10774 that's what we use unless the branch won't reach. */
10775
10776 #define ALWAYS_USE_FAKE_DEP 0
10777 #define ALWAYS_EMIT_R2SAVE 0
10778
10779 static inline unsigned int
10780 plt_stub_size (struct ppc_link_hash_table *htab,
10781 struct ppc_stub_hash_entry *stub_entry,
10782 bfd_vma off)
10783 {
10784 unsigned size = 12;
10785
10786 if (ALWAYS_EMIT_R2SAVE
10787 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10788 size += 4;
10789 if (PPC_HA (off) != 0)
10790 size += 4;
10791 if (htab->opd_abi)
10792 {
10793 size += 4;
10794 if (htab->params->plt_static_chain)
10795 size += 4;
10796 if (htab->params->plt_thread_safe
10797 && htab->elf.dynamic_sections_created
10798 && stub_entry->h != NULL
10799 && stub_entry->h->elf.dynindx != -1)
10800 size += 8;
10801 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10802 size += 4;
10803 }
10804 if (stub_entry->h != NULL
10805 && (stub_entry->h == htab->tls_get_addr_fd
10806 || stub_entry->h == htab->tls_get_addr)
10807 && htab->params->tls_get_addr_opt)
10808 {
10809 size += 7 * 4;
10810 if (ALWAYS_EMIT_R2SAVE
10811 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10812 size += 6 * 4;
10813 }
10814 return size;
10815 }
10816
10817 /* Depending on the sign of plt_stub_align:
10818 If positive, return the padding to align to a 2**plt_stub_align
10819 boundary.
10820 If negative, if this stub would cross fewer 2**plt_stub_align
10821 boundaries if we align, then return the padding needed to do so. */
10822
10823 static inline unsigned int
10824 plt_stub_pad (struct ppc_link_hash_table *htab,
10825 struct ppc_stub_hash_entry *stub_entry,
10826 bfd_vma plt_off)
10827 {
10828 int stub_align;
10829 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10830 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10831
10832 if (htab->params->plt_stub_align >= 0)
10833 {
10834 stub_align = 1 << htab->params->plt_stub_align;
10835 if ((stub_off & (stub_align - 1)) != 0)
10836 return stub_align - (stub_off & (stub_align - 1));
10837 return 0;
10838 }
10839
10840 stub_align = 1 << -htab->params->plt_stub_align;
10841 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10842 > ((stub_size - 1) & -stub_align))
10843 return stub_align - (stub_off & (stub_align - 1));
10844 return 0;
10845 }
10846
10847 /* Build a .plt call stub. */
10848
10849 static inline bfd_byte *
10850 build_plt_stub (struct ppc_link_hash_table *htab,
10851 struct ppc_stub_hash_entry *stub_entry,
10852 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10853 {
10854 bfd *obfd = htab->params->stub_bfd;
10855 bfd_boolean plt_load_toc = htab->opd_abi;
10856 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10857 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10858 && htab->elf.dynamic_sections_created
10859 && stub_entry->h != NULL
10860 && stub_entry->h->elf.dynindx != -1);
10861 bfd_boolean use_fake_dep = plt_thread_safe;
10862 bfd_vma cmp_branch_off = 0;
10863
10864 if (!ALWAYS_USE_FAKE_DEP
10865 && plt_load_toc
10866 && plt_thread_safe
10867 && !((stub_entry->h == htab->tls_get_addr_fd
10868 || stub_entry->h == htab->tls_get_addr)
10869 && htab->params->tls_get_addr_opt))
10870 {
10871 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10872 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10873 / PLT_ENTRY_SIZE (htab));
10874 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10875 bfd_vma to, from;
10876
10877 if (pltindex > 32768)
10878 glinkoff += (pltindex - 32768) * 4;
10879 to = (glinkoff
10880 + htab->glink->output_offset
10881 + htab->glink->output_section->vma);
10882 from = (p - stub_entry->group->stub_sec->contents
10883 + 4 * (ALWAYS_EMIT_R2SAVE
10884 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10885 + 4 * (PPC_HA (offset) != 0)
10886 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10887 != PPC_HA (offset))
10888 + 4 * (plt_static_chain != 0)
10889 + 20
10890 + stub_entry->group->stub_sec->output_offset
10891 + stub_entry->group->stub_sec->output_section->vma);
10892 cmp_branch_off = to - from;
10893 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10894 }
10895
10896 if (PPC_HA (offset) != 0)
10897 {
10898 if (r != NULL)
10899 {
10900 if (ALWAYS_EMIT_R2SAVE
10901 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10902 r[0].r_offset += 4;
10903 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10904 r[1].r_offset = r[0].r_offset + 4;
10905 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10906 r[1].r_addend = r[0].r_addend;
10907 if (plt_load_toc)
10908 {
10909 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10910 {
10911 r[2].r_offset = r[1].r_offset + 4;
10912 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10913 r[2].r_addend = r[0].r_addend;
10914 }
10915 else
10916 {
10917 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10918 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10919 r[2].r_addend = r[0].r_addend + 8;
10920 if (plt_static_chain)
10921 {
10922 r[3].r_offset = r[2].r_offset + 4;
10923 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10924 r[3].r_addend = r[0].r_addend + 16;
10925 }
10926 }
10927 }
10928 }
10929 if (ALWAYS_EMIT_R2SAVE
10930 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10931 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10932 if (plt_load_toc)
10933 {
10934 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10935 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10936 }
10937 else
10938 {
10939 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10940 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10941 }
10942 if (plt_load_toc
10943 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10944 {
10945 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10946 offset = 0;
10947 }
10948 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10949 if (plt_load_toc)
10950 {
10951 if (use_fake_dep)
10952 {
10953 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10954 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10955 }
10956 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10957 if (plt_static_chain)
10958 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10959 }
10960 }
10961 else
10962 {
10963 if (r != NULL)
10964 {
10965 if (ALWAYS_EMIT_R2SAVE
10966 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10967 r[0].r_offset += 4;
10968 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10969 if (plt_load_toc)
10970 {
10971 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10972 {
10973 r[1].r_offset = r[0].r_offset + 4;
10974 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10975 r[1].r_addend = r[0].r_addend;
10976 }
10977 else
10978 {
10979 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10980 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10981 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10982 if (plt_static_chain)
10983 {
10984 r[2].r_offset = r[1].r_offset + 4;
10985 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10986 r[2].r_addend = r[0].r_addend + 8;
10987 }
10988 }
10989 }
10990 }
10991 if (ALWAYS_EMIT_R2SAVE
10992 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10993 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10994 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10995 if (plt_load_toc
10996 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10997 {
10998 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10999 offset = 0;
11000 }
11001 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11002 if (plt_load_toc)
11003 {
11004 if (use_fake_dep)
11005 {
11006 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11007 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11008 }
11009 if (plt_static_chain)
11010 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11011 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11012 }
11013 }
11014 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11015 {
11016 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11017 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11018 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11019 }
11020 else
11021 bfd_put_32 (obfd, BCTR, p), p += 4;
11022 return p;
11023 }
11024
11025 /* Build a special .plt call stub for __tls_get_addr. */
11026
11027 #define LD_R11_0R3 0xe9630000
11028 #define LD_R12_0R3 0xe9830000
11029 #define MR_R0_R3 0x7c601b78
11030 #define CMPDI_R11_0 0x2c2b0000
11031 #define ADD_R3_R12_R13 0x7c6c6a14
11032 #define BEQLR 0x4d820020
11033 #define MR_R3_R0 0x7c030378
11034 #define STD_R11_0R1 0xf9610000
11035 #define BCTRL 0x4e800421
11036 #define LD_R11_0R1 0xe9610000
11037 #define MTLR_R11 0x7d6803a6
11038
11039 static inline bfd_byte *
11040 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11041 struct ppc_stub_hash_entry *stub_entry,
11042 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11043 {
11044 bfd *obfd = htab->params->stub_bfd;
11045
11046 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11047 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11048 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11049 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11050 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11051 bfd_put_32 (obfd, BEQLR, p), p += 4;
11052 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11053 if (r != NULL)
11054 r[0].r_offset += 7 * 4;
11055 if (!ALWAYS_EMIT_R2SAVE
11056 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11057 return build_plt_stub (htab, stub_entry, p, offset, r);
11058
11059 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
11060 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11061
11062 if (r != NULL)
11063 r[0].r_offset += 2 * 4;
11064 p = build_plt_stub (htab, stub_entry, p, offset, r);
11065 bfd_put_32 (obfd, BCTRL, p - 4);
11066
11067 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
11068 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11069 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11070 bfd_put_32 (obfd, BLR, p), p += 4;
11071
11072 return p;
11073 }
11074
11075 static Elf_Internal_Rela *
11076 get_relocs (asection *sec, int count)
11077 {
11078 Elf_Internal_Rela *relocs;
11079 struct bfd_elf_section_data *elfsec_data;
11080
11081 elfsec_data = elf_section_data (sec);
11082 relocs = elfsec_data->relocs;
11083 if (relocs == NULL)
11084 {
11085 bfd_size_type relsize;
11086 relsize = sec->reloc_count * sizeof (*relocs);
11087 relocs = bfd_alloc (sec->owner, relsize);
11088 if (relocs == NULL)
11089 return NULL;
11090 elfsec_data->relocs = relocs;
11091 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11092 sizeof (Elf_Internal_Shdr));
11093 if (elfsec_data->rela.hdr == NULL)
11094 return NULL;
11095 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11096 * sizeof (Elf64_External_Rela));
11097 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11098 sec->reloc_count = 0;
11099 }
11100 relocs += sec->reloc_count;
11101 sec->reloc_count += count;
11102 return relocs;
11103 }
11104
11105 static bfd_vma
11106 get_r2off (struct bfd_link_info *info,
11107 struct ppc_stub_hash_entry *stub_entry)
11108 {
11109 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11110 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11111
11112 if (r2off == 0)
11113 {
11114 /* Support linking -R objects. Get the toc pointer from the
11115 opd entry. */
11116 char buf[8];
11117 if (!htab->opd_abi)
11118 return r2off;
11119 asection *opd = stub_entry->h->elf.root.u.def.section;
11120 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11121
11122 if (strcmp (opd->name, ".opd") != 0
11123 || opd->reloc_count != 0)
11124 {
11125 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11126 stub_entry->h->elf.root.root.string);
11127 bfd_set_error (bfd_error_bad_value);
11128 return (bfd_vma) -1;
11129 }
11130 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11131 return (bfd_vma) -1;
11132 r2off = bfd_get_64 (opd->owner, buf);
11133 r2off -= elf_gp (info->output_bfd);
11134 }
11135 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11136 return r2off;
11137 }
11138
11139 static bfd_boolean
11140 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11141 {
11142 struct ppc_stub_hash_entry *stub_entry;
11143 struct ppc_branch_hash_entry *br_entry;
11144 struct bfd_link_info *info;
11145 struct ppc_link_hash_table *htab;
11146 bfd_byte *loc;
11147 bfd_byte *p;
11148 bfd_vma dest, off;
11149 Elf_Internal_Rela *r;
11150 asection *plt;
11151
11152 /* Massage our args to the form they really have. */
11153 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11154 info = in_arg;
11155
11156 htab = ppc_hash_table (info);
11157 if (htab == NULL)
11158 return FALSE;
11159
11160 /* Make a note of the offset within the stubs for this entry. */
11161 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11162 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11163
11164 htab->stub_count[stub_entry->stub_type - 1] += 1;
11165 switch (stub_entry->stub_type)
11166 {
11167 case ppc_stub_long_branch:
11168 case ppc_stub_long_branch_r2off:
11169 /* Branches are relative. This is where we are going to. */
11170 dest = (stub_entry->target_value
11171 + stub_entry->target_section->output_offset
11172 + stub_entry->target_section->output_section->vma);
11173 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11174 off = dest;
11175
11176 /* And this is where we are coming from. */
11177 off -= (stub_entry->stub_offset
11178 + stub_entry->group->stub_sec->output_offset
11179 + stub_entry->group->stub_sec->output_section->vma);
11180
11181 p = loc;
11182 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11183 {
11184 bfd_vma r2off = get_r2off (info, stub_entry);
11185
11186 if (r2off == (bfd_vma) -1)
11187 {
11188 htab->stub_error = TRUE;
11189 return FALSE;
11190 }
11191 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11192 p += 4;
11193 if (PPC_HA (r2off) != 0)
11194 {
11195 bfd_put_32 (htab->params->stub_bfd,
11196 ADDIS_R2_R2 | PPC_HA (r2off), p);
11197 p += 4;
11198 }
11199 if (PPC_LO (r2off) != 0)
11200 {
11201 bfd_put_32 (htab->params->stub_bfd,
11202 ADDI_R2_R2 | PPC_LO (r2off), p);
11203 p += 4;
11204 }
11205 off -= p - loc;
11206 }
11207 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11208 p += 4;
11209
11210 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11211 {
11212 _bfd_error_handler
11213 (_("long branch stub `%s' offset overflow"),
11214 stub_entry->root.string);
11215 htab->stub_error = TRUE;
11216 return FALSE;
11217 }
11218
11219 if (info->emitrelocations)
11220 {
11221 r = get_relocs (stub_entry->group->stub_sec, 1);
11222 if (r == NULL)
11223 return FALSE;
11224 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11225 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11226 r->r_addend = dest;
11227 if (stub_entry->h != NULL)
11228 {
11229 struct elf_link_hash_entry **hashes;
11230 unsigned long symndx;
11231 struct ppc_link_hash_entry *h;
11232
11233 hashes = elf_sym_hashes (htab->params->stub_bfd);
11234 if (hashes == NULL)
11235 {
11236 bfd_size_type hsize;
11237
11238 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11239 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11240 if (hashes == NULL)
11241 return FALSE;
11242 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11243 htab->stub_globals = 1;
11244 }
11245 symndx = htab->stub_globals++;
11246 h = stub_entry->h;
11247 hashes[symndx] = &h->elf;
11248 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11249 if (h->oh != NULL && h->oh->is_func)
11250 h = ppc_follow_link (h->oh);
11251 if (h->elf.root.u.def.section != stub_entry->target_section)
11252 /* H is an opd symbol. The addend must be zero. */
11253 r->r_addend = 0;
11254 else
11255 {
11256 off = (h->elf.root.u.def.value
11257 + h->elf.root.u.def.section->output_offset
11258 + h->elf.root.u.def.section->output_section->vma);
11259 r->r_addend -= off;
11260 }
11261 }
11262 }
11263 break;
11264
11265 case ppc_stub_plt_branch:
11266 case ppc_stub_plt_branch_r2off:
11267 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11268 stub_entry->root.string + 9,
11269 FALSE, FALSE);
11270 if (br_entry == NULL)
11271 {
11272 _bfd_error_handler (_("can't find branch stub `%s'"),
11273 stub_entry->root.string);
11274 htab->stub_error = TRUE;
11275 return FALSE;
11276 }
11277
11278 dest = (stub_entry->target_value
11279 + stub_entry->target_section->output_offset
11280 + stub_entry->target_section->output_section->vma);
11281 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11282 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11283
11284 bfd_put_64 (htab->brlt->owner, dest,
11285 htab->brlt->contents + br_entry->offset);
11286
11287 if (br_entry->iter == htab->stub_iteration)
11288 {
11289 br_entry->iter = 0;
11290
11291 if (htab->relbrlt != NULL)
11292 {
11293 /* Create a reloc for the branch lookup table entry. */
11294 Elf_Internal_Rela rela;
11295 bfd_byte *rl;
11296
11297 rela.r_offset = (br_entry->offset
11298 + htab->brlt->output_offset
11299 + htab->brlt->output_section->vma);
11300 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11301 rela.r_addend = dest;
11302
11303 rl = htab->relbrlt->contents;
11304 rl += (htab->relbrlt->reloc_count++
11305 * sizeof (Elf64_External_Rela));
11306 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11307 }
11308 else if (info->emitrelocations)
11309 {
11310 r = get_relocs (htab->brlt, 1);
11311 if (r == NULL)
11312 return FALSE;
11313 /* brlt, being SEC_LINKER_CREATED does not go through the
11314 normal reloc processing. Symbols and offsets are not
11315 translated from input file to output file form, so
11316 set up the offset per the output file. */
11317 r->r_offset = (br_entry->offset
11318 + htab->brlt->output_offset
11319 + htab->brlt->output_section->vma);
11320 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11321 r->r_addend = dest;
11322 }
11323 }
11324
11325 dest = (br_entry->offset
11326 + htab->brlt->output_offset
11327 + htab->brlt->output_section->vma);
11328
11329 off = (dest
11330 - elf_gp (info->output_bfd)
11331 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11332
11333 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11334 {
11335 info->callbacks->einfo
11336 (_("%P: linkage table error against `%pT'\n"),
11337 stub_entry->root.string);
11338 bfd_set_error (bfd_error_bad_value);
11339 htab->stub_error = TRUE;
11340 return FALSE;
11341 }
11342
11343 if (info->emitrelocations)
11344 {
11345 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11346 if (r == NULL)
11347 return FALSE;
11348 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11349 if (bfd_big_endian (info->output_bfd))
11350 r[0].r_offset += 2;
11351 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11352 r[0].r_offset += 4;
11353 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11354 r[0].r_addend = dest;
11355 if (PPC_HA (off) != 0)
11356 {
11357 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11358 r[1].r_offset = r[0].r_offset + 4;
11359 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11360 r[1].r_addend = r[0].r_addend;
11361 }
11362 }
11363
11364 p = loc;
11365 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11366 {
11367 if (PPC_HA (off) != 0)
11368 {
11369 bfd_put_32 (htab->params->stub_bfd,
11370 ADDIS_R12_R2 | PPC_HA (off), p);
11371 p += 4;
11372 bfd_put_32 (htab->params->stub_bfd,
11373 LD_R12_0R12 | PPC_LO (off), p);
11374 }
11375 else
11376 bfd_put_32 (htab->params->stub_bfd,
11377 LD_R12_0R2 | PPC_LO (off), p);
11378 }
11379 else
11380 {
11381 bfd_vma r2off = get_r2off (info, stub_entry);
11382
11383 if (r2off == (bfd_vma) -1)
11384 {
11385 htab->stub_error = TRUE;
11386 return FALSE;
11387 }
11388
11389 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11390 p += 4;
11391 if (PPC_HA (off) != 0)
11392 {
11393 bfd_put_32 (htab->params->stub_bfd,
11394 ADDIS_R12_R2 | PPC_HA (off), p);
11395 p += 4;
11396 bfd_put_32 (htab->params->stub_bfd,
11397 LD_R12_0R12 | PPC_LO (off), p);
11398 }
11399 else
11400 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11401
11402 if (PPC_HA (r2off) != 0)
11403 {
11404 p += 4;
11405 bfd_put_32 (htab->params->stub_bfd,
11406 ADDIS_R2_R2 | PPC_HA (r2off), p);
11407 }
11408 if (PPC_LO (r2off) != 0)
11409 {
11410 p += 4;
11411 bfd_put_32 (htab->params->stub_bfd,
11412 ADDI_R2_R2 | PPC_LO (r2off), p);
11413 }
11414 }
11415 p += 4;
11416 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11417 p += 4;
11418 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11419 p += 4;
11420 break;
11421
11422 case ppc_stub_plt_call:
11423 case ppc_stub_plt_call_r2save:
11424 if (stub_entry->h != NULL
11425 && stub_entry->h->is_func_descriptor
11426 && stub_entry->h->oh != NULL)
11427 {
11428 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11429
11430 /* If the old-ABI "dot-symbol" is undefined make it weak so
11431 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11432 if (fh->elf.root.type == bfd_link_hash_undefined
11433 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11434 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11435 fh->elf.root.type = bfd_link_hash_undefweak;
11436 }
11437
11438 /* Now build the stub. */
11439 dest = stub_entry->plt_ent->plt.offset & ~1;
11440 if (dest >= (bfd_vma) -2)
11441 abort ();
11442
11443 plt = htab->elf.splt;
11444 if (!htab->elf.dynamic_sections_created
11445 || stub_entry->h == NULL
11446 || stub_entry->h->elf.dynindx == -1)
11447 {
11448 if (stub_entry->symtype == STT_GNU_IFUNC)
11449 plt = htab->elf.iplt;
11450 else
11451 plt = htab->pltlocal;
11452 }
11453
11454 dest += plt->output_offset + plt->output_section->vma;
11455
11456 off = (dest
11457 - elf_gp (info->output_bfd)
11458 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11459
11460 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11461 {
11462 info->callbacks->einfo
11463 /* xgettext:c-format */
11464 (_("%P: linkage table error against `%pT'\n"),
11465 stub_entry->h != NULL
11466 ? stub_entry->h->elf.root.root.string
11467 : "<local sym>");
11468 bfd_set_error (bfd_error_bad_value);
11469 htab->stub_error = TRUE;
11470 return FALSE;
11471 }
11472
11473 if (htab->params->plt_stub_align != 0)
11474 {
11475 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11476
11477 stub_entry->group->stub_sec->size += pad;
11478 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11479 loc += pad;
11480 }
11481
11482 r = NULL;
11483 if (info->emitrelocations)
11484 {
11485 r = get_relocs (stub_entry->group->stub_sec,
11486 ((PPC_HA (off) != 0)
11487 + (htab->opd_abi
11488 ? 2 + (htab->params->plt_static_chain
11489 && PPC_HA (off + 16) == PPC_HA (off))
11490 : 1)));
11491 if (r == NULL)
11492 return FALSE;
11493 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11494 if (bfd_big_endian (info->output_bfd))
11495 r[0].r_offset += 2;
11496 r[0].r_addend = dest;
11497 }
11498 if (stub_entry->h != NULL
11499 && (stub_entry->h == htab->tls_get_addr_fd
11500 || stub_entry->h == htab->tls_get_addr)
11501 && htab->params->tls_get_addr_opt)
11502 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11503 else
11504 p = build_plt_stub (htab, stub_entry, loc, off, r);
11505 break;
11506
11507 case ppc_stub_save_res:
11508 return TRUE;
11509
11510 default:
11511 BFD_FAIL ();
11512 return FALSE;
11513 }
11514
11515 stub_entry->group->stub_sec->size += p - loc;
11516
11517 if (htab->params->emit_stub_syms)
11518 {
11519 struct elf_link_hash_entry *h;
11520 size_t len1, len2;
11521 char *name;
11522 const char *const stub_str[] = { "long_branch",
11523 "long_branch_r2off",
11524 "plt_branch",
11525 "plt_branch_r2off",
11526 "plt_call",
11527 "plt_call" };
11528
11529 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11530 len2 = strlen (stub_entry->root.string);
11531 name = bfd_malloc (len1 + len2 + 2);
11532 if (name == NULL)
11533 return FALSE;
11534 memcpy (name, stub_entry->root.string, 9);
11535 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11536 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11537 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11538 if (h == NULL)
11539 return FALSE;
11540 if (h->root.type == bfd_link_hash_new)
11541 {
11542 h->root.type = bfd_link_hash_defined;
11543 h->root.u.def.section = stub_entry->group->stub_sec;
11544 h->root.u.def.value = stub_entry->stub_offset;
11545 h->ref_regular = 1;
11546 h->def_regular = 1;
11547 h->ref_regular_nonweak = 1;
11548 h->forced_local = 1;
11549 h->non_elf = 0;
11550 h->root.linker_def = 1;
11551 }
11552 }
11553
11554 return TRUE;
11555 }
11556
11557 /* As above, but don't actually build the stub. Just bump offset so
11558 we know stub section sizes, and select plt_branch stubs where
11559 long_branch stubs won't do. */
11560
11561 static bfd_boolean
11562 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11563 {
11564 struct ppc_stub_hash_entry *stub_entry;
11565 struct bfd_link_info *info;
11566 struct ppc_link_hash_table *htab;
11567 bfd_vma off;
11568 int size;
11569
11570 /* Massage our args to the form they really have. */
11571 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11572 info = in_arg;
11573
11574 htab = ppc_hash_table (info);
11575 if (htab == NULL)
11576 return FALSE;
11577
11578 if (stub_entry->h != NULL
11579 && stub_entry->h->save_res
11580 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11581 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11582 {
11583 /* Don't make stubs to out-of-line register save/restore
11584 functions. Instead, emit copies of the functions. */
11585 stub_entry->group->needs_save_res = 1;
11586 stub_entry->stub_type = ppc_stub_save_res;
11587 return TRUE;
11588 }
11589
11590 if (stub_entry->stub_type == ppc_stub_plt_call
11591 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11592 {
11593 asection *plt;
11594 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11595 if (off >= (bfd_vma) -2)
11596 abort ();
11597 plt = htab->elf.splt;
11598 if (!htab->elf.dynamic_sections_created
11599 || stub_entry->h == NULL
11600 || stub_entry->h->elf.dynindx == -1)
11601 {
11602 if (stub_entry->symtype == STT_GNU_IFUNC)
11603 plt = htab->elf.iplt;
11604 else
11605 plt = htab->pltlocal;
11606 }
11607 off += (plt->output_offset
11608 + plt->output_section->vma
11609 - elf_gp (info->output_bfd)
11610 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11611
11612 size = plt_stub_size (htab, stub_entry, off);
11613 if (stub_entry->h != NULL
11614 && (stub_entry->h == htab->tls_get_addr_fd
11615 || stub_entry->h == htab->tls_get_addr)
11616 && htab->params->tls_get_addr_opt
11617 && (ALWAYS_EMIT_R2SAVE
11618 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11619 stub_entry->group->tls_get_addr_opt_bctrl
11620 = stub_entry->group->stub_sec->size + size - 5 * 4;
11621
11622 if (htab->params->plt_stub_align)
11623 size += plt_stub_pad (htab, stub_entry, off);
11624 if (info->emitrelocations)
11625 {
11626 stub_entry->group->stub_sec->reloc_count
11627 += ((PPC_HA (off) != 0)
11628 + (htab->opd_abi
11629 ? 2 + (htab->params->plt_static_chain
11630 && PPC_HA (off + 16) == PPC_HA (off))
11631 : 1));
11632 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11633 }
11634 }
11635 else
11636 {
11637 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11638 variants. */
11639 bfd_vma r2off = 0;
11640 bfd_vma local_off = 0;
11641
11642 off = (stub_entry->target_value
11643 + stub_entry->target_section->output_offset
11644 + stub_entry->target_section->output_section->vma);
11645 off -= (stub_entry->group->stub_sec->size
11646 + stub_entry->group->stub_sec->output_offset
11647 + stub_entry->group->stub_sec->output_section->vma);
11648
11649 /* Reset the stub type from the plt variant in case we now
11650 can reach with a shorter stub. */
11651 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11652 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11653
11654 size = 4;
11655 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11656 {
11657 r2off = get_r2off (info, stub_entry);
11658 if (r2off == (bfd_vma) -1)
11659 {
11660 htab->stub_error = TRUE;
11661 return FALSE;
11662 }
11663 size = 8;
11664 if (PPC_HA (r2off) != 0)
11665 size += 4;
11666 if (PPC_LO (r2off) != 0)
11667 size += 4;
11668 off -= size - 4;
11669 }
11670
11671 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11672
11673 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11674 Do the same for -R objects without function descriptors. */
11675 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11676 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11677 && r2off == 0
11678 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11679 {
11680 struct ppc_branch_hash_entry *br_entry;
11681
11682 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11683 stub_entry->root.string + 9,
11684 TRUE, FALSE);
11685 if (br_entry == NULL)
11686 {
11687 _bfd_error_handler (_("can't build branch stub `%s'"),
11688 stub_entry->root.string);
11689 htab->stub_error = TRUE;
11690 return FALSE;
11691 }
11692
11693 if (br_entry->iter != htab->stub_iteration)
11694 {
11695 br_entry->iter = htab->stub_iteration;
11696 br_entry->offset = htab->brlt->size;
11697 htab->brlt->size += 8;
11698
11699 if (htab->relbrlt != NULL)
11700 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11701 else if (info->emitrelocations)
11702 {
11703 htab->brlt->reloc_count += 1;
11704 htab->brlt->flags |= SEC_RELOC;
11705 }
11706 }
11707
11708 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11709 off = (br_entry->offset
11710 + htab->brlt->output_offset
11711 + htab->brlt->output_section->vma
11712 - elf_gp (info->output_bfd)
11713 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11714
11715 if (info->emitrelocations)
11716 {
11717 stub_entry->group->stub_sec->reloc_count
11718 += 1 + (PPC_HA (off) != 0);
11719 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11720 }
11721
11722 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11723 {
11724 size = 12;
11725 if (PPC_HA (off) != 0)
11726 size = 16;
11727 }
11728 else
11729 {
11730 size = 16;
11731 if (PPC_HA (off) != 0)
11732 size += 4;
11733
11734 if (PPC_HA (r2off) != 0)
11735 size += 4;
11736 if (PPC_LO (r2off) != 0)
11737 size += 4;
11738 }
11739 }
11740 else if (info->emitrelocations)
11741 {
11742 stub_entry->group->stub_sec->reloc_count += 1;
11743 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11744 }
11745 }
11746
11747 stub_entry->group->stub_sec->size += size;
11748 return TRUE;
11749 }
11750
11751 /* Set up various things so that we can make a list of input sections
11752 for each output section included in the link. Returns -1 on error,
11753 0 when no stubs will be needed, and 1 on success. */
11754
11755 int
11756 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11757 {
11758 unsigned int id;
11759 bfd_size_type amt;
11760 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11761
11762 if (htab == NULL)
11763 return -1;
11764
11765 htab->sec_info_arr_size = _bfd_section_id;
11766 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11767 htab->sec_info = bfd_zmalloc (amt);
11768 if (htab->sec_info == NULL)
11769 return -1;
11770
11771 /* Set toc_off for com, und, abs and ind sections. */
11772 for (id = 0; id < 3; id++)
11773 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11774
11775 return 1;
11776 }
11777
11778 /* Set up for first pass at multitoc partitioning. */
11779
11780 void
11781 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11782 {
11783 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11784
11785 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11786 htab->toc_bfd = NULL;
11787 htab->toc_first_sec = NULL;
11788 }
11789
11790 /* The linker repeatedly calls this function for each TOC input section
11791 and linker generated GOT section. Group input bfds such that the toc
11792 within a group is less than 64k in size. */
11793
11794 bfd_boolean
11795 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11796 {
11797 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11798 bfd_vma addr, off, limit;
11799
11800 if (htab == NULL)
11801 return FALSE;
11802
11803 if (!htab->second_toc_pass)
11804 {
11805 /* Keep track of the first .toc or .got section for this input bfd. */
11806 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11807
11808 if (new_bfd)
11809 {
11810 htab->toc_bfd = isec->owner;
11811 htab->toc_first_sec = isec;
11812 }
11813
11814 addr = isec->output_offset + isec->output_section->vma;
11815 off = addr - htab->toc_curr;
11816 limit = 0x80008000;
11817 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11818 limit = 0x10000;
11819 if (off + isec->size > limit)
11820 {
11821 addr = (htab->toc_first_sec->output_offset
11822 + htab->toc_first_sec->output_section->vma);
11823 htab->toc_curr = addr;
11824 htab->toc_curr &= -TOC_BASE_ALIGN;
11825 }
11826
11827 /* toc_curr is the base address of this toc group. Set elf_gp
11828 for the input section to be the offset relative to the
11829 output toc base plus 0x8000. Making the input elf_gp an
11830 offset allows us to move the toc as a whole without
11831 recalculating input elf_gp. */
11832 off = htab->toc_curr - elf_gp (info->output_bfd);
11833 off += TOC_BASE_OFF;
11834
11835 /* Die if someone uses a linker script that doesn't keep input
11836 file .toc and .got together. */
11837 if (new_bfd
11838 && elf_gp (isec->owner) != 0
11839 && elf_gp (isec->owner) != off)
11840 return FALSE;
11841
11842 elf_gp (isec->owner) = off;
11843 return TRUE;
11844 }
11845
11846 /* During the second pass toc_first_sec points to the start of
11847 a toc group, and toc_curr is used to track the old elf_gp.
11848 We use toc_bfd to ensure we only look at each bfd once. */
11849 if (htab->toc_bfd == isec->owner)
11850 return TRUE;
11851 htab->toc_bfd = isec->owner;
11852
11853 if (htab->toc_first_sec == NULL
11854 || htab->toc_curr != elf_gp (isec->owner))
11855 {
11856 htab->toc_curr = elf_gp (isec->owner);
11857 htab->toc_first_sec = isec;
11858 }
11859 addr = (htab->toc_first_sec->output_offset
11860 + htab->toc_first_sec->output_section->vma);
11861 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11862 elf_gp (isec->owner) = off;
11863
11864 return TRUE;
11865 }
11866
11867 /* Called via elf_link_hash_traverse to merge GOT entries for global
11868 symbol H. */
11869
11870 static bfd_boolean
11871 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11872 {
11873 if (h->root.type == bfd_link_hash_indirect)
11874 return TRUE;
11875
11876 merge_got_entries (&h->got.glist);
11877
11878 return TRUE;
11879 }
11880
11881 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11882 symbol H. */
11883
11884 static bfd_boolean
11885 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11886 {
11887 struct got_entry *gent;
11888
11889 if (h->root.type == bfd_link_hash_indirect)
11890 return TRUE;
11891
11892 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11893 if (!gent->is_indirect)
11894 allocate_got (h, (struct bfd_link_info *) inf, gent);
11895 return TRUE;
11896 }
11897
11898 /* Called on the first multitoc pass after the last call to
11899 ppc64_elf_next_toc_section. This function removes duplicate GOT
11900 entries. */
11901
11902 bfd_boolean
11903 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11904 {
11905 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11906 struct bfd *ibfd, *ibfd2;
11907 bfd_boolean done_something;
11908
11909 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11910
11911 if (!htab->do_multi_toc)
11912 return FALSE;
11913
11914 /* Merge global sym got entries within a toc group. */
11915 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11916
11917 /* And tlsld_got. */
11918 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11919 {
11920 struct got_entry *ent, *ent2;
11921
11922 if (!is_ppc64_elf (ibfd))
11923 continue;
11924
11925 ent = ppc64_tlsld_got (ibfd);
11926 if (!ent->is_indirect
11927 && ent->got.offset != (bfd_vma) -1)
11928 {
11929 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11930 {
11931 if (!is_ppc64_elf (ibfd2))
11932 continue;
11933
11934 ent2 = ppc64_tlsld_got (ibfd2);
11935 if (!ent2->is_indirect
11936 && ent2->got.offset != (bfd_vma) -1
11937 && elf_gp (ibfd2) == elf_gp (ibfd))
11938 {
11939 ent2->is_indirect = TRUE;
11940 ent2->got.ent = ent;
11941 }
11942 }
11943 }
11944 }
11945
11946 /* Zap sizes of got sections. */
11947 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11948 htab->elf.irelplt->size -= htab->got_reli_size;
11949 htab->got_reli_size = 0;
11950
11951 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11952 {
11953 asection *got, *relgot;
11954
11955 if (!is_ppc64_elf (ibfd))
11956 continue;
11957
11958 got = ppc64_elf_tdata (ibfd)->got;
11959 if (got != NULL)
11960 {
11961 got->rawsize = got->size;
11962 got->size = 0;
11963 relgot = ppc64_elf_tdata (ibfd)->relgot;
11964 relgot->rawsize = relgot->size;
11965 relgot->size = 0;
11966 }
11967 }
11968
11969 /* Now reallocate the got, local syms first. We don't need to
11970 allocate section contents again since we never increase size. */
11971 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11972 {
11973 struct got_entry **lgot_ents;
11974 struct got_entry **end_lgot_ents;
11975 struct plt_entry **local_plt;
11976 struct plt_entry **end_local_plt;
11977 unsigned char *lgot_masks;
11978 bfd_size_type locsymcount;
11979 Elf_Internal_Shdr *symtab_hdr;
11980 asection *s;
11981
11982 if (!is_ppc64_elf (ibfd))
11983 continue;
11984
11985 lgot_ents = elf_local_got_ents (ibfd);
11986 if (!lgot_ents)
11987 continue;
11988
11989 symtab_hdr = &elf_symtab_hdr (ibfd);
11990 locsymcount = symtab_hdr->sh_info;
11991 end_lgot_ents = lgot_ents + locsymcount;
11992 local_plt = (struct plt_entry **) end_lgot_ents;
11993 end_local_plt = local_plt + locsymcount;
11994 lgot_masks = (unsigned char *) end_local_plt;
11995 s = ppc64_elf_tdata (ibfd)->got;
11996 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11997 {
11998 struct got_entry *ent;
11999
12000 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12001 {
12002 unsigned int ent_size = 8;
12003 unsigned int rel_size = sizeof (Elf64_External_Rela);
12004
12005 ent->got.offset = s->size;
12006 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12007 {
12008 ent_size *= 2;
12009 rel_size *= 2;
12010 }
12011 s->size += ent_size;
12012 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12013 {
12014 htab->elf.irelplt->size += rel_size;
12015 htab->got_reli_size += rel_size;
12016 }
12017 else if (bfd_link_pic (info)
12018 && !((ent->tls_type & TLS_TPREL) != 0
12019 && bfd_link_executable (info)))
12020 {
12021 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12022 srel->size += rel_size;
12023 }
12024 }
12025 }
12026 }
12027
12028 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12029
12030 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12031 {
12032 struct got_entry *ent;
12033
12034 if (!is_ppc64_elf (ibfd))
12035 continue;
12036
12037 ent = ppc64_tlsld_got (ibfd);
12038 if (!ent->is_indirect
12039 && ent->got.offset != (bfd_vma) -1)
12040 {
12041 asection *s = ppc64_elf_tdata (ibfd)->got;
12042 ent->got.offset = s->size;
12043 s->size += 16;
12044 if (bfd_link_pic (info))
12045 {
12046 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12047 srel->size += sizeof (Elf64_External_Rela);
12048 }
12049 }
12050 }
12051
12052 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12053 if (!done_something)
12054 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12055 {
12056 asection *got;
12057
12058 if (!is_ppc64_elf (ibfd))
12059 continue;
12060
12061 got = ppc64_elf_tdata (ibfd)->got;
12062 if (got != NULL)
12063 {
12064 done_something = got->rawsize != got->size;
12065 if (done_something)
12066 break;
12067 }
12068 }
12069
12070 if (done_something)
12071 (*htab->params->layout_sections_again) ();
12072
12073 /* Set up for second pass over toc sections to recalculate elf_gp
12074 on input sections. */
12075 htab->toc_bfd = NULL;
12076 htab->toc_first_sec = NULL;
12077 htab->second_toc_pass = TRUE;
12078 return done_something;
12079 }
12080
12081 /* Called after second pass of multitoc partitioning. */
12082
12083 void
12084 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12085 {
12086 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12087
12088 /* After the second pass, toc_curr tracks the TOC offset used
12089 for code sections below in ppc64_elf_next_input_section. */
12090 htab->toc_curr = TOC_BASE_OFF;
12091 }
12092
12093 /* No toc references were found in ISEC. If the code in ISEC makes no
12094 calls, then there's no need to use toc adjusting stubs when branching
12095 into ISEC. Actually, indirect calls from ISEC are OK as they will
12096 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12097 needed, and 2 if a cyclical call-graph was found but no other reason
12098 for a stub was detected. If called from the top level, a return of
12099 2 means the same as a return of 0. */
12100
12101 static int
12102 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12103 {
12104 int ret;
12105
12106 /* Mark this section as checked. */
12107 isec->call_check_done = 1;
12108
12109 /* We know none of our code bearing sections will need toc stubs. */
12110 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12111 return 0;
12112
12113 if (isec->size == 0)
12114 return 0;
12115
12116 if (isec->output_section == NULL)
12117 return 0;
12118
12119 ret = 0;
12120 if (isec->reloc_count != 0)
12121 {
12122 Elf_Internal_Rela *relstart, *rel;
12123 Elf_Internal_Sym *local_syms;
12124 struct ppc_link_hash_table *htab;
12125
12126 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12127 info->keep_memory);
12128 if (relstart == NULL)
12129 return -1;
12130
12131 /* Look for branches to outside of this section. */
12132 local_syms = NULL;
12133 htab = ppc_hash_table (info);
12134 if (htab == NULL)
12135 return -1;
12136
12137 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12138 {
12139 enum elf_ppc64_reloc_type r_type;
12140 unsigned long r_symndx;
12141 struct elf_link_hash_entry *h;
12142 struct ppc_link_hash_entry *eh;
12143 Elf_Internal_Sym *sym;
12144 asection *sym_sec;
12145 struct _opd_sec_data *opd;
12146 bfd_vma sym_value;
12147 bfd_vma dest;
12148
12149 r_type = ELF64_R_TYPE (rel->r_info);
12150 if (r_type != R_PPC64_REL24
12151 && r_type != R_PPC64_REL14
12152 && r_type != R_PPC64_REL14_BRTAKEN
12153 && r_type != R_PPC64_REL14_BRNTAKEN
12154 && r_type != R_PPC64_PLTCALL)
12155 continue;
12156
12157 r_symndx = ELF64_R_SYM (rel->r_info);
12158 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12159 isec->owner))
12160 {
12161 ret = -1;
12162 break;
12163 }
12164
12165 /* Calls to dynamic lib functions go through a plt call stub
12166 that uses r2. */
12167 eh = (struct ppc_link_hash_entry *) h;
12168 if (eh != NULL
12169 && (eh->elf.plt.plist != NULL
12170 || (eh->oh != NULL
12171 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12172 {
12173 ret = 1;
12174 break;
12175 }
12176
12177 if (sym_sec == NULL)
12178 /* Ignore other undefined symbols. */
12179 continue;
12180
12181 /* Assume branches to other sections not included in the
12182 link need stubs too, to cover -R and absolute syms. */
12183 if (sym_sec->output_section == NULL)
12184 {
12185 ret = 1;
12186 break;
12187 }
12188
12189 if (h == NULL)
12190 sym_value = sym->st_value;
12191 else
12192 {
12193 if (h->root.type != bfd_link_hash_defined
12194 && h->root.type != bfd_link_hash_defweak)
12195 abort ();
12196 sym_value = h->root.u.def.value;
12197 }
12198 sym_value += rel->r_addend;
12199
12200 /* If this branch reloc uses an opd sym, find the code section. */
12201 opd = get_opd_info (sym_sec);
12202 if (opd != NULL)
12203 {
12204 if (h == NULL && opd->adjust != NULL)
12205 {
12206 long adjust;
12207
12208 adjust = opd->adjust[OPD_NDX (sym_value)];
12209 if (adjust == -1)
12210 /* Assume deleted functions won't ever be called. */
12211 continue;
12212 sym_value += adjust;
12213 }
12214
12215 dest = opd_entry_value (sym_sec, sym_value,
12216 &sym_sec, NULL, FALSE);
12217 if (dest == (bfd_vma) -1)
12218 continue;
12219 }
12220 else
12221 dest = (sym_value
12222 + sym_sec->output_offset
12223 + sym_sec->output_section->vma);
12224
12225 /* Ignore branch to self. */
12226 if (sym_sec == isec)
12227 continue;
12228
12229 /* If the called function uses the toc, we need a stub. */
12230 if (sym_sec->has_toc_reloc
12231 || sym_sec->makes_toc_func_call)
12232 {
12233 ret = 1;
12234 break;
12235 }
12236
12237 /* Assume any branch that needs a long branch stub might in fact
12238 need a plt_branch stub. A plt_branch stub uses r2. */
12239 else if (dest - (isec->output_offset
12240 + isec->output_section->vma
12241 + rel->r_offset) + (1 << 25)
12242 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12243 ? h->other
12244 : sym->st_other))
12245 {
12246 ret = 1;
12247 break;
12248 }
12249
12250 /* If calling back to a section in the process of being
12251 tested, we can't say for sure that no toc adjusting stubs
12252 are needed, so don't return zero. */
12253 else if (sym_sec->call_check_in_progress)
12254 ret = 2;
12255
12256 /* Branches to another section that itself doesn't have any TOC
12257 references are OK. Recursively call ourselves to check. */
12258 else if (!sym_sec->call_check_done)
12259 {
12260 int recur;
12261
12262 /* Mark current section as indeterminate, so that other
12263 sections that call back to current won't be marked as
12264 known. */
12265 isec->call_check_in_progress = 1;
12266 recur = toc_adjusting_stub_needed (info, sym_sec);
12267 isec->call_check_in_progress = 0;
12268
12269 if (recur != 0)
12270 {
12271 ret = recur;
12272 if (recur != 2)
12273 break;
12274 }
12275 }
12276 }
12277
12278 if (local_syms != NULL
12279 && (elf_symtab_hdr (isec->owner).contents
12280 != (unsigned char *) local_syms))
12281 free (local_syms);
12282 if (elf_section_data (isec)->relocs != relstart)
12283 free (relstart);
12284 }
12285
12286 if ((ret & 1) == 0
12287 && isec->map_head.s != NULL
12288 && (strcmp (isec->output_section->name, ".init") == 0
12289 || strcmp (isec->output_section->name, ".fini") == 0))
12290 {
12291 if (isec->map_head.s->has_toc_reloc
12292 || isec->map_head.s->makes_toc_func_call)
12293 ret = 1;
12294 else if (!isec->map_head.s->call_check_done)
12295 {
12296 int recur;
12297 isec->call_check_in_progress = 1;
12298 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12299 isec->call_check_in_progress = 0;
12300 if (recur != 0)
12301 ret = recur;
12302 }
12303 }
12304
12305 if (ret == 1)
12306 isec->makes_toc_func_call = 1;
12307
12308 return ret;
12309 }
12310
12311 /* The linker repeatedly calls this function for each input section,
12312 in the order that input sections are linked into output sections.
12313 Build lists of input sections to determine groupings between which
12314 we may insert linker stubs. */
12315
12316 bfd_boolean
12317 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12318 {
12319 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12320
12321 if (htab == NULL)
12322 return FALSE;
12323
12324 if ((isec->output_section->flags & SEC_CODE) != 0
12325 && isec->output_section->id < htab->sec_info_arr_size)
12326 {
12327 /* This happens to make the list in reverse order,
12328 which is what we want. */
12329 htab->sec_info[isec->id].u.list
12330 = htab->sec_info[isec->output_section->id].u.list;
12331 htab->sec_info[isec->output_section->id].u.list = isec;
12332 }
12333
12334 if (htab->multi_toc_needed)
12335 {
12336 /* Analyse sections that aren't already flagged as needing a
12337 valid toc pointer. Exclude .fixup for the linux kernel.
12338 .fixup contains branches, but only back to the function that
12339 hit an exception. */
12340 if (!(isec->has_toc_reloc
12341 || (isec->flags & SEC_CODE) == 0
12342 || strcmp (isec->name, ".fixup") == 0
12343 || isec->call_check_done))
12344 {
12345 if (toc_adjusting_stub_needed (info, isec) < 0)
12346 return FALSE;
12347 }
12348 /* Make all sections use the TOC assigned for this object file.
12349 This will be wrong for pasted sections; We fix that in
12350 check_pasted_section(). */
12351 if (elf_gp (isec->owner) != 0)
12352 htab->toc_curr = elf_gp (isec->owner);
12353 }
12354
12355 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12356 return TRUE;
12357 }
12358
12359 /* Check that all .init and .fini sections use the same toc, if they
12360 have toc relocs. */
12361
12362 static bfd_boolean
12363 check_pasted_section (struct bfd_link_info *info, const char *name)
12364 {
12365 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12366
12367 if (o != NULL)
12368 {
12369 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12370 bfd_vma toc_off = 0;
12371 asection *i;
12372
12373 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12374 if (i->has_toc_reloc)
12375 {
12376 if (toc_off == 0)
12377 toc_off = htab->sec_info[i->id].toc_off;
12378 else if (toc_off != htab->sec_info[i->id].toc_off)
12379 return FALSE;
12380 }
12381
12382 if (toc_off == 0)
12383 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12384 if (i->makes_toc_func_call)
12385 {
12386 toc_off = htab->sec_info[i->id].toc_off;
12387 break;
12388 }
12389
12390 /* Make sure the whole pasted function uses the same toc offset. */
12391 if (toc_off != 0)
12392 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12393 htab->sec_info[i->id].toc_off = toc_off;
12394 }
12395 return TRUE;
12396 }
12397
12398 bfd_boolean
12399 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12400 {
12401 return (check_pasted_section (info, ".init")
12402 & check_pasted_section (info, ".fini"));
12403 }
12404
12405 /* See whether we can group stub sections together. Grouping stub
12406 sections may result in fewer stubs. More importantly, we need to
12407 put all .init* and .fini* stubs at the beginning of the .init or
12408 .fini output sections respectively, because glibc splits the
12409 _init and _fini functions into multiple parts. Putting a stub in
12410 the middle of a function is not a good idea. */
12411
12412 static bfd_boolean
12413 group_sections (struct bfd_link_info *info,
12414 bfd_size_type stub_group_size,
12415 bfd_boolean stubs_always_before_branch)
12416 {
12417 struct ppc_link_hash_table *htab;
12418 asection *osec;
12419 bfd_boolean suppress_size_errors;
12420
12421 htab = ppc_hash_table (info);
12422 if (htab == NULL)
12423 return FALSE;
12424
12425 suppress_size_errors = FALSE;
12426 if (stub_group_size == 1)
12427 {
12428 /* Default values. */
12429 if (stubs_always_before_branch)
12430 stub_group_size = 0x1e00000;
12431 else
12432 stub_group_size = 0x1c00000;
12433 suppress_size_errors = TRUE;
12434 }
12435
12436 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12437 {
12438 asection *tail;
12439
12440 if (osec->id >= htab->sec_info_arr_size)
12441 continue;
12442
12443 tail = htab->sec_info[osec->id].u.list;
12444 while (tail != NULL)
12445 {
12446 asection *curr;
12447 asection *prev;
12448 bfd_size_type total;
12449 bfd_boolean big_sec;
12450 bfd_vma curr_toc;
12451 struct map_stub *group;
12452 bfd_size_type group_size;
12453
12454 curr = tail;
12455 total = tail->size;
12456 group_size = (ppc64_elf_section_data (tail) != NULL
12457 && ppc64_elf_section_data (tail)->has_14bit_branch
12458 ? stub_group_size >> 10 : stub_group_size);
12459
12460 big_sec = total > group_size;
12461 if (big_sec && !suppress_size_errors)
12462 /* xgettext:c-format */
12463 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12464 tail->owner, tail);
12465 curr_toc = htab->sec_info[tail->id].toc_off;
12466
12467 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12468 && ((total += curr->output_offset - prev->output_offset)
12469 < (ppc64_elf_section_data (prev) != NULL
12470 && ppc64_elf_section_data (prev)->has_14bit_branch
12471 ? (group_size = stub_group_size >> 10) : group_size))
12472 && htab->sec_info[prev->id].toc_off == curr_toc)
12473 curr = prev;
12474
12475 /* OK, the size from the start of CURR to the end is less
12476 than group_size and thus can be handled by one stub
12477 section. (or the tail section is itself larger than
12478 group_size, in which case we may be toast.) We should
12479 really be keeping track of the total size of stubs added
12480 here, as stubs contribute to the final output section
12481 size. That's a little tricky, and this way will only
12482 break if stubs added make the total size more than 2^25,
12483 ie. for the default stub_group_size, if stubs total more
12484 than 2097152 bytes, or nearly 75000 plt call stubs. */
12485 group = bfd_alloc (curr->owner, sizeof (*group));
12486 if (group == NULL)
12487 return FALSE;
12488 group->link_sec = curr;
12489 group->stub_sec = NULL;
12490 group->needs_save_res = 0;
12491 group->tls_get_addr_opt_bctrl = -1u;
12492 group->next = htab->group;
12493 htab->group = group;
12494 do
12495 {
12496 prev = htab->sec_info[tail->id].u.list;
12497 /* Set up this stub group. */
12498 htab->sec_info[tail->id].u.group = group;
12499 }
12500 while (tail != curr && (tail = prev) != NULL);
12501
12502 /* But wait, there's more! Input sections up to group_size
12503 bytes before the stub section can be handled by it too.
12504 Don't do this if we have a really large section after the
12505 stubs, as adding more stubs increases the chance that
12506 branches may not reach into the stub section. */
12507 if (!stubs_always_before_branch && !big_sec)
12508 {
12509 total = 0;
12510 while (prev != NULL
12511 && ((total += tail->output_offset - prev->output_offset)
12512 < (ppc64_elf_section_data (prev) != NULL
12513 && ppc64_elf_section_data (prev)->has_14bit_branch
12514 ? (group_size = stub_group_size >> 10) : group_size))
12515 && htab->sec_info[prev->id].toc_off == curr_toc)
12516 {
12517 tail = prev;
12518 prev = htab->sec_info[tail->id].u.list;
12519 htab->sec_info[tail->id].u.group = group;
12520 }
12521 }
12522 tail = prev;
12523 }
12524 }
12525 return TRUE;
12526 }
12527
12528 static const unsigned char glink_eh_frame_cie[] =
12529 {
12530 0, 0, 0, 16, /* length. */
12531 0, 0, 0, 0, /* id. */
12532 1, /* CIE version. */
12533 'z', 'R', 0, /* Augmentation string. */
12534 4, /* Code alignment. */
12535 0x78, /* Data alignment. */
12536 65, /* RA reg. */
12537 1, /* Augmentation size. */
12538 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12539 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12540 };
12541
12542 static size_t
12543 stub_eh_frame_size (struct map_stub *group, size_t align)
12544 {
12545 size_t this_size = 17;
12546 if (group->tls_get_addr_opt_bctrl != -1u)
12547 {
12548 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12549 if (to_bctrl < 64)
12550 this_size += 1;
12551 else if (to_bctrl < 256)
12552 this_size += 2;
12553 else if (to_bctrl < 65536)
12554 this_size += 3;
12555 else
12556 this_size += 5;
12557 this_size += 6;
12558 }
12559 this_size = (this_size + align - 1) & -align;
12560 return this_size;
12561 }
12562
12563 /* Stripping output sections is normally done before dynamic section
12564 symbols have been allocated. This function is called later, and
12565 handles cases like htab->brlt which is mapped to its own output
12566 section. */
12567
12568 static void
12569 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12570 {
12571 if (isec->size == 0
12572 && isec->output_section->size == 0
12573 && !(isec->output_section->flags & SEC_KEEP)
12574 && !bfd_section_removed_from_list (info->output_bfd,
12575 isec->output_section)
12576 && elf_section_data (isec->output_section)->dynindx == 0)
12577 {
12578 isec->output_section->flags |= SEC_EXCLUDE;
12579 bfd_section_list_remove (info->output_bfd, isec->output_section);
12580 info->output_bfd->section_count--;
12581 }
12582 }
12583
12584 /* Determine and set the size of the stub section for a final link.
12585
12586 The basic idea here is to examine all the relocations looking for
12587 PC-relative calls to a target that is unreachable with a "bl"
12588 instruction. */
12589
12590 bfd_boolean
12591 ppc64_elf_size_stubs (struct bfd_link_info *info)
12592 {
12593 bfd_size_type stub_group_size;
12594 bfd_boolean stubs_always_before_branch;
12595 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12596
12597 if (htab == NULL)
12598 return FALSE;
12599
12600 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12601 htab->params->plt_thread_safe = 1;
12602 if (!htab->opd_abi)
12603 htab->params->plt_thread_safe = 0;
12604 else if (htab->params->plt_thread_safe == -1)
12605 {
12606 static const char *const thread_starter[] =
12607 {
12608 "pthread_create",
12609 /* libstdc++ */
12610 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12611 /* librt */
12612 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12613 "mq_notify", "create_timer",
12614 /* libanl */
12615 "getaddrinfo_a",
12616 /* libgomp */
12617 "GOMP_parallel",
12618 "GOMP_parallel_start",
12619 "GOMP_parallel_loop_static",
12620 "GOMP_parallel_loop_static_start",
12621 "GOMP_parallel_loop_dynamic",
12622 "GOMP_parallel_loop_dynamic_start",
12623 "GOMP_parallel_loop_guided",
12624 "GOMP_parallel_loop_guided_start",
12625 "GOMP_parallel_loop_runtime",
12626 "GOMP_parallel_loop_runtime_start",
12627 "GOMP_parallel_sections",
12628 "GOMP_parallel_sections_start",
12629 /* libgo */
12630 "__go_go",
12631 };
12632 unsigned i;
12633
12634 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12635 {
12636 struct elf_link_hash_entry *h;
12637 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12638 FALSE, FALSE, TRUE);
12639 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12640 if (htab->params->plt_thread_safe)
12641 break;
12642 }
12643 }
12644 stubs_always_before_branch = htab->params->group_size < 0;
12645 if (htab->params->group_size < 0)
12646 stub_group_size = -htab->params->group_size;
12647 else
12648 stub_group_size = htab->params->group_size;
12649
12650 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12651 return FALSE;
12652
12653 #define STUB_SHRINK_ITER 20
12654 /* Loop until no stubs added. After iteration 20 of this loop we may
12655 exit on a stub section shrinking. This is to break out of a
12656 pathological case where adding stubs on one iteration decreases
12657 section gaps (perhaps due to alignment), which then requires
12658 fewer or smaller stubs on the next iteration. */
12659
12660 while (1)
12661 {
12662 bfd *input_bfd;
12663 unsigned int bfd_indx;
12664 struct map_stub *group;
12665
12666 htab->stub_iteration += 1;
12667
12668 for (input_bfd = info->input_bfds, bfd_indx = 0;
12669 input_bfd != NULL;
12670 input_bfd = input_bfd->link.next, bfd_indx++)
12671 {
12672 Elf_Internal_Shdr *symtab_hdr;
12673 asection *section;
12674 Elf_Internal_Sym *local_syms = NULL;
12675
12676 if (!is_ppc64_elf (input_bfd))
12677 continue;
12678
12679 /* We'll need the symbol table in a second. */
12680 symtab_hdr = &elf_symtab_hdr (input_bfd);
12681 if (symtab_hdr->sh_info == 0)
12682 continue;
12683
12684 /* Walk over each section attached to the input bfd. */
12685 for (section = input_bfd->sections;
12686 section != NULL;
12687 section = section->next)
12688 {
12689 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12690
12691 /* If there aren't any relocs, then there's nothing more
12692 to do. */
12693 if ((section->flags & SEC_RELOC) == 0
12694 || (section->flags & SEC_ALLOC) == 0
12695 || (section->flags & SEC_LOAD) == 0
12696 || (section->flags & SEC_CODE) == 0
12697 || section->reloc_count == 0)
12698 continue;
12699
12700 /* If this section is a link-once section that will be
12701 discarded, then don't create any stubs. */
12702 if (section->output_section == NULL
12703 || section->output_section->owner != info->output_bfd)
12704 continue;
12705
12706 /* Get the relocs. */
12707 internal_relocs
12708 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12709 info->keep_memory);
12710 if (internal_relocs == NULL)
12711 goto error_ret_free_local;
12712
12713 /* Now examine each relocation. */
12714 irela = internal_relocs;
12715 irelaend = irela + section->reloc_count;
12716 for (; irela < irelaend; irela++)
12717 {
12718 enum elf_ppc64_reloc_type r_type;
12719 unsigned int r_indx;
12720 enum ppc_stub_type stub_type;
12721 struct ppc_stub_hash_entry *stub_entry;
12722 asection *sym_sec, *code_sec;
12723 bfd_vma sym_value, code_value;
12724 bfd_vma destination;
12725 unsigned long local_off;
12726 bfd_boolean ok_dest;
12727 struct ppc_link_hash_entry *hash;
12728 struct ppc_link_hash_entry *fdh;
12729 struct elf_link_hash_entry *h;
12730 Elf_Internal_Sym *sym;
12731 char *stub_name;
12732 const asection *id_sec;
12733 struct _opd_sec_data *opd;
12734 struct plt_entry *plt_ent;
12735
12736 r_type = ELF64_R_TYPE (irela->r_info);
12737 r_indx = ELF64_R_SYM (irela->r_info);
12738
12739 if (r_type >= R_PPC64_max)
12740 {
12741 bfd_set_error (bfd_error_bad_value);
12742 goto error_ret_free_internal;
12743 }
12744
12745 /* Only look for stubs on branch instructions. */
12746 if (r_type != R_PPC64_REL24
12747 && r_type != R_PPC64_REL14
12748 && r_type != R_PPC64_REL14_BRTAKEN
12749 && r_type != R_PPC64_REL14_BRNTAKEN)
12750 continue;
12751
12752 /* Now determine the call target, its name, value,
12753 section. */
12754 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12755 r_indx, input_bfd))
12756 goto error_ret_free_internal;
12757 hash = (struct ppc_link_hash_entry *) h;
12758
12759 ok_dest = FALSE;
12760 fdh = NULL;
12761 sym_value = 0;
12762 if (hash == NULL)
12763 {
12764 sym_value = sym->st_value;
12765 if (sym_sec != NULL
12766 && sym_sec->output_section != NULL)
12767 ok_dest = TRUE;
12768 }
12769 else if (hash->elf.root.type == bfd_link_hash_defined
12770 || hash->elf.root.type == bfd_link_hash_defweak)
12771 {
12772 sym_value = hash->elf.root.u.def.value;
12773 if (sym_sec->output_section != NULL)
12774 ok_dest = TRUE;
12775 }
12776 else if (hash->elf.root.type == bfd_link_hash_undefweak
12777 || hash->elf.root.type == bfd_link_hash_undefined)
12778 {
12779 /* Recognise an old ABI func code entry sym, and
12780 use the func descriptor sym instead if it is
12781 defined. */
12782 if (hash->elf.root.root.string[0] == '.'
12783 && hash->oh != NULL)
12784 {
12785 fdh = ppc_follow_link (hash->oh);
12786 if (fdh->elf.root.type == bfd_link_hash_defined
12787 || fdh->elf.root.type == bfd_link_hash_defweak)
12788 {
12789 sym_sec = fdh->elf.root.u.def.section;
12790 sym_value = fdh->elf.root.u.def.value;
12791 if (sym_sec->output_section != NULL)
12792 ok_dest = TRUE;
12793 }
12794 else
12795 fdh = NULL;
12796 }
12797 }
12798 else
12799 {
12800 bfd_set_error (bfd_error_bad_value);
12801 goto error_ret_free_internal;
12802 }
12803
12804 destination = 0;
12805 local_off = 0;
12806 if (ok_dest)
12807 {
12808 sym_value += irela->r_addend;
12809 destination = (sym_value
12810 + sym_sec->output_offset
12811 + sym_sec->output_section->vma);
12812 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12813 ? hash->elf.other
12814 : sym->st_other);
12815 }
12816
12817 code_sec = sym_sec;
12818 code_value = sym_value;
12819 opd = get_opd_info (sym_sec);
12820 if (opd != NULL)
12821 {
12822 bfd_vma dest;
12823
12824 if (hash == NULL && opd->adjust != NULL)
12825 {
12826 long adjust = opd->adjust[OPD_NDX (sym_value)];
12827 if (adjust == -1)
12828 continue;
12829 code_value += adjust;
12830 sym_value += adjust;
12831 }
12832 dest = opd_entry_value (sym_sec, sym_value,
12833 &code_sec, &code_value, FALSE);
12834 if (dest != (bfd_vma) -1)
12835 {
12836 destination = dest;
12837 if (fdh != NULL)
12838 {
12839 /* Fixup old ABI sym to point at code
12840 entry. */
12841 hash->elf.root.type = bfd_link_hash_defweak;
12842 hash->elf.root.u.def.section = code_sec;
12843 hash->elf.root.u.def.value = code_value;
12844 }
12845 }
12846 }
12847
12848 /* Determine what (if any) linker stub is needed. */
12849 plt_ent = NULL;
12850 stub_type = ppc_type_of_stub (section, irela, &hash,
12851 &plt_ent, destination,
12852 local_off);
12853
12854 if (stub_type != ppc_stub_plt_call)
12855 {
12856 /* Check whether we need a TOC adjusting stub.
12857 Since the linker pastes together pieces from
12858 different object files when creating the
12859 _init and _fini functions, it may be that a
12860 call to what looks like a local sym is in
12861 fact a call needing a TOC adjustment. */
12862 if (code_sec != NULL
12863 && code_sec->output_section != NULL
12864 && (htab->sec_info[code_sec->id].toc_off
12865 != htab->sec_info[section->id].toc_off)
12866 && (code_sec->has_toc_reloc
12867 || code_sec->makes_toc_func_call))
12868 stub_type = ppc_stub_long_branch_r2off;
12869 }
12870
12871 if (stub_type == ppc_stub_none)
12872 continue;
12873
12874 /* __tls_get_addr calls might be eliminated. */
12875 if (stub_type != ppc_stub_plt_call
12876 && hash != NULL
12877 && (hash == htab->tls_get_addr
12878 || hash == htab->tls_get_addr_fd)
12879 && section->has_tls_reloc
12880 && irela != internal_relocs)
12881 {
12882 /* Get tls info. */
12883 unsigned char *tls_mask;
12884
12885 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12886 irela - 1, input_bfd))
12887 goto error_ret_free_internal;
12888 if ((*tls_mask & TLS_TLS) != 0)
12889 continue;
12890 }
12891
12892 if (stub_type == ppc_stub_plt_call)
12893 {
12894 if (!htab->opd_abi
12895 && htab->params->plt_localentry0 != 0
12896 && is_elfv2_localentry0 (&hash->elf))
12897 htab->has_plt_localentry0 = 1;
12898 else if (irela + 1 < irelaend
12899 && irela[1].r_offset == irela->r_offset + 4
12900 && (ELF64_R_TYPE (irela[1].r_info)
12901 == R_PPC64_TOCSAVE))
12902 {
12903 if (!tocsave_find (htab, INSERT,
12904 &local_syms, irela + 1, input_bfd))
12905 goto error_ret_free_internal;
12906 }
12907 else
12908 stub_type = ppc_stub_plt_call_r2save;
12909 }
12910
12911 /* Support for grouping stub sections. */
12912 id_sec = htab->sec_info[section->id].u.group->link_sec;
12913
12914 /* Get the name of this stub. */
12915 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12916 if (!stub_name)
12917 goto error_ret_free_internal;
12918
12919 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12920 stub_name, FALSE, FALSE);
12921 if (stub_entry != NULL)
12922 {
12923 /* The proper stub has already been created. */
12924 free (stub_name);
12925 if (stub_type == ppc_stub_plt_call_r2save)
12926 stub_entry->stub_type = stub_type;
12927 continue;
12928 }
12929
12930 stub_entry = ppc_add_stub (stub_name, section, info);
12931 if (stub_entry == NULL)
12932 {
12933 free (stub_name);
12934 error_ret_free_internal:
12935 if (elf_section_data (section)->relocs == NULL)
12936 free (internal_relocs);
12937 error_ret_free_local:
12938 if (local_syms != NULL
12939 && (symtab_hdr->contents
12940 != (unsigned char *) local_syms))
12941 free (local_syms);
12942 return FALSE;
12943 }
12944
12945 stub_entry->stub_type = stub_type;
12946 if (stub_type != ppc_stub_plt_call
12947 && stub_type != ppc_stub_plt_call_r2save)
12948 {
12949 stub_entry->target_value = code_value;
12950 stub_entry->target_section = code_sec;
12951 }
12952 else
12953 {
12954 stub_entry->target_value = sym_value;
12955 stub_entry->target_section = sym_sec;
12956 }
12957 stub_entry->h = hash;
12958 stub_entry->plt_ent = plt_ent;
12959 stub_entry->symtype
12960 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12961 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12962
12963 if (stub_entry->h != NULL)
12964 htab->stub_globals += 1;
12965 }
12966
12967 /* We're done with the internal relocs, free them. */
12968 if (elf_section_data (section)->relocs != internal_relocs)
12969 free (internal_relocs);
12970 }
12971
12972 if (local_syms != NULL
12973 && symtab_hdr->contents != (unsigned char *) local_syms)
12974 {
12975 if (!info->keep_memory)
12976 free (local_syms);
12977 else
12978 symtab_hdr->contents = (unsigned char *) local_syms;
12979 }
12980 }
12981
12982 /* We may have added some stubs. Find out the new size of the
12983 stub sections. */
12984 for (group = htab->group; group != NULL; group = group->next)
12985 if (group->stub_sec != NULL)
12986 {
12987 asection *stub_sec = group->stub_sec;
12988
12989 if (htab->stub_iteration <= STUB_SHRINK_ITER
12990 || stub_sec->rawsize < stub_sec->size)
12991 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12992 stub_sec->rawsize = stub_sec->size;
12993 stub_sec->size = 0;
12994 stub_sec->reloc_count = 0;
12995 stub_sec->flags &= ~SEC_RELOC;
12996 }
12997
12998 if (htab->stub_iteration <= STUB_SHRINK_ITER
12999 || htab->brlt->rawsize < htab->brlt->size)
13000 htab->brlt->rawsize = htab->brlt->size;
13001 htab->brlt->size = 0;
13002 htab->brlt->reloc_count = 0;
13003 htab->brlt->flags &= ~SEC_RELOC;
13004 if (htab->relbrlt != NULL)
13005 htab->relbrlt->size = 0;
13006
13007 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13008
13009 for (group = htab->group; group != NULL; group = group->next)
13010 if (group->needs_save_res)
13011 group->stub_sec->size += htab->sfpr->size;
13012
13013 if (info->emitrelocations
13014 && htab->glink != NULL && htab->glink->size != 0)
13015 {
13016 htab->glink->reloc_count = 1;
13017 htab->glink->flags |= SEC_RELOC;
13018 }
13019
13020 if (htab->glink_eh_frame != NULL
13021 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13022 && htab->glink_eh_frame->output_section->size > 8)
13023 {
13024 size_t size = 0, align = 4;
13025
13026 for (group = htab->group; group != NULL; group = group->next)
13027 if (group->stub_sec != NULL)
13028 size += stub_eh_frame_size (group, align);
13029 if (htab->glink != NULL && htab->glink->size != 0)
13030 size += (24 + align - 1) & -align;
13031 if (size != 0)
13032 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13033 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13034 size = (size + align - 1) & -align;
13035 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13036 htab->glink_eh_frame->size = size;
13037 }
13038
13039 if (htab->params->plt_stub_align != 0)
13040 for (group = htab->group; group != NULL; group = group->next)
13041 if (group->stub_sec != NULL)
13042 {
13043 int align = abs (htab->params->plt_stub_align);
13044 group->stub_sec->size
13045 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13046 }
13047
13048 for (group = htab->group; group != NULL; group = group->next)
13049 if (group->stub_sec != NULL
13050 && group->stub_sec->rawsize != group->stub_sec->size
13051 && (htab->stub_iteration <= STUB_SHRINK_ITER
13052 || group->stub_sec->rawsize < group->stub_sec->size))
13053 break;
13054
13055 if (group == NULL
13056 && (htab->brlt->rawsize == htab->brlt->size
13057 || (htab->stub_iteration > STUB_SHRINK_ITER
13058 && htab->brlt->rawsize > htab->brlt->size))
13059 && (htab->glink_eh_frame == NULL
13060 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13061 break;
13062
13063 /* Ask the linker to do its stuff. */
13064 (*htab->params->layout_sections_again) ();
13065 }
13066
13067 if (htab->glink_eh_frame != NULL
13068 && htab->glink_eh_frame->size != 0)
13069 {
13070 bfd_vma val;
13071 bfd_byte *p, *last_fde;
13072 size_t last_fde_len, size, align, pad;
13073 struct map_stub *group;
13074
13075 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13076 if (p == NULL)
13077 return FALSE;
13078 htab->glink_eh_frame->contents = p;
13079 last_fde = p;
13080 align = 4;
13081
13082 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13083 /* CIE length (rewrite in case little-endian). */
13084 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13085 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13086 p += last_fde_len + 4;
13087
13088 for (group = htab->group; group != NULL; group = group->next)
13089 if (group->stub_sec != NULL)
13090 {
13091 last_fde = p;
13092 last_fde_len = stub_eh_frame_size (group, align) - 4;
13093 /* FDE length. */
13094 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13095 p += 4;
13096 /* CIE pointer. */
13097 val = p - htab->glink_eh_frame->contents;
13098 bfd_put_32 (htab->elf.dynobj, val, p);
13099 p += 4;
13100 /* Offset to stub section, written later. */
13101 p += 4;
13102 /* stub section size. */
13103 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13104 p += 4;
13105 /* Augmentation. */
13106 p += 1;
13107 if (group->tls_get_addr_opt_bctrl != -1u)
13108 {
13109 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13110
13111 /* This FDE needs more than just the default.
13112 Describe __tls_get_addr_opt stub LR. */
13113 if (to_bctrl < 64)
13114 *p++ = DW_CFA_advance_loc + to_bctrl;
13115 else if (to_bctrl < 256)
13116 {
13117 *p++ = DW_CFA_advance_loc1;
13118 *p++ = to_bctrl;
13119 }
13120 else if (to_bctrl < 65536)
13121 {
13122 *p++ = DW_CFA_advance_loc2;
13123 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13124 p += 2;
13125 }
13126 else
13127 {
13128 *p++ = DW_CFA_advance_loc4;
13129 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13130 p += 4;
13131 }
13132 *p++ = DW_CFA_offset_extended_sf;
13133 *p++ = 65;
13134 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13135 *p++ = DW_CFA_advance_loc + 4;
13136 *p++ = DW_CFA_restore_extended;
13137 *p++ = 65;
13138 }
13139 /* Pad. */
13140 p = last_fde + last_fde_len + 4;
13141 }
13142 if (htab->glink != NULL && htab->glink->size != 0)
13143 {
13144 last_fde = p;
13145 last_fde_len = ((24 + align - 1) & -align) - 4;
13146 /* FDE length. */
13147 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13148 p += 4;
13149 /* CIE pointer. */
13150 val = p - htab->glink_eh_frame->contents;
13151 bfd_put_32 (htab->elf.dynobj, val, p);
13152 p += 4;
13153 /* Offset to .glink, written later. */
13154 p += 4;
13155 /* .glink size. */
13156 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13157 p += 4;
13158 /* Augmentation. */
13159 p += 1;
13160
13161 *p++ = DW_CFA_advance_loc + 1;
13162 *p++ = DW_CFA_register;
13163 *p++ = 65;
13164 *p++ = htab->opd_abi ? 12 : 0;
13165 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13166 *p++ = DW_CFA_restore_extended;
13167 *p++ = 65;
13168 p += ((24 + align - 1) & -align) - 24;
13169 }
13170 /* Subsume any padding into the last FDE if user .eh_frame
13171 sections are aligned more than glink_eh_frame. Otherwise any
13172 zero padding will be seen as a terminator. */
13173 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13174 size = p - htab->glink_eh_frame->contents;
13175 pad = ((size + align - 1) & -align) - size;
13176 htab->glink_eh_frame->size = size + pad;
13177 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13178 }
13179
13180 maybe_strip_output (info, htab->brlt);
13181 if (htab->glink_eh_frame != NULL)
13182 maybe_strip_output (info, htab->glink_eh_frame);
13183
13184 return TRUE;
13185 }
13186
13187 /* Called after we have determined section placement. If sections
13188 move, we'll be called again. Provide a value for TOCstart. */
13189
13190 bfd_vma
13191 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13192 {
13193 asection *s;
13194 bfd_vma TOCstart, adjust;
13195
13196 if (info != NULL)
13197 {
13198 struct elf_link_hash_entry *h;
13199 struct elf_link_hash_table *htab = elf_hash_table (info);
13200
13201 if (is_elf_hash_table (htab)
13202 && htab->hgot != NULL)
13203 h = htab->hgot;
13204 else
13205 {
13206 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13207 if (is_elf_hash_table (htab))
13208 htab->hgot = h;
13209 }
13210 if (h != NULL
13211 && h->root.type == bfd_link_hash_defined
13212 && !h->root.linker_def
13213 && (!is_elf_hash_table (htab)
13214 || h->def_regular))
13215 {
13216 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13217 + h->root.u.def.section->output_offset
13218 + h->root.u.def.section->output_section->vma);
13219 _bfd_set_gp_value (obfd, TOCstart);
13220 return TOCstart;
13221 }
13222 }
13223
13224 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13225 order. The TOC starts where the first of these sections starts. */
13226 s = bfd_get_section_by_name (obfd, ".got");
13227 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13228 s = bfd_get_section_by_name (obfd, ".toc");
13229 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13230 s = bfd_get_section_by_name (obfd, ".tocbss");
13231 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13232 s = bfd_get_section_by_name (obfd, ".plt");
13233 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13234 {
13235 /* This may happen for
13236 o references to TOC base (SYM@toc / TOC[tc0]) without a
13237 .toc directive
13238 o bad linker script
13239 o --gc-sections and empty TOC sections
13240
13241 FIXME: Warn user? */
13242
13243 /* Look for a likely section. We probably won't even be
13244 using TOCstart. */
13245 for (s = obfd->sections; s != NULL; s = s->next)
13246 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13247 | SEC_EXCLUDE))
13248 == (SEC_ALLOC | SEC_SMALL_DATA))
13249 break;
13250 if (s == NULL)
13251 for (s = obfd->sections; s != NULL; s = s->next)
13252 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13253 == (SEC_ALLOC | SEC_SMALL_DATA))
13254 break;
13255 if (s == NULL)
13256 for (s = obfd->sections; s != NULL; s = s->next)
13257 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13258 == SEC_ALLOC)
13259 break;
13260 if (s == NULL)
13261 for (s = obfd->sections; s != NULL; s = s->next)
13262 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13263 break;
13264 }
13265
13266 TOCstart = 0;
13267 if (s != NULL)
13268 TOCstart = s->output_section->vma + s->output_offset;
13269
13270 /* Force alignment. */
13271 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13272 TOCstart -= adjust;
13273 _bfd_set_gp_value (obfd, TOCstart);
13274
13275 if (info != NULL && s != NULL)
13276 {
13277 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13278
13279 if (htab != NULL)
13280 {
13281 if (htab->elf.hgot != NULL)
13282 {
13283 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13284 htab->elf.hgot->root.u.def.section = s;
13285 }
13286 }
13287 else
13288 {
13289 struct bfd_link_hash_entry *bh = NULL;
13290 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13291 s, TOC_BASE_OFF - adjust,
13292 NULL, FALSE, FALSE, &bh);
13293 }
13294 }
13295 return TOCstart;
13296 }
13297
13298 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13299 write out any global entry stubs, and PLT relocations. */
13300
13301 static bfd_boolean
13302 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13303 {
13304 struct bfd_link_info *info;
13305 struct ppc_link_hash_table *htab;
13306 struct plt_entry *ent;
13307 asection *s;
13308
13309 if (h->root.type == bfd_link_hash_indirect)
13310 return TRUE;
13311
13312 info = inf;
13313 htab = ppc_hash_table (info);
13314 if (htab == NULL)
13315 return FALSE;
13316
13317 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13318 if (ent->plt.offset != (bfd_vma) -1)
13319 {
13320 /* This symbol has an entry in the procedure linkage
13321 table. Set it up. */
13322 Elf_Internal_Rela rela;
13323 asection *plt, *relplt;
13324 bfd_byte *loc;
13325
13326 if (!htab->elf.dynamic_sections_created
13327 || h->dynindx == -1)
13328 {
13329 if (!(h->def_regular
13330 && (h->root.type == bfd_link_hash_defined
13331 || h->root.type == bfd_link_hash_defweak)))
13332 continue;
13333 if (h->type == STT_GNU_IFUNC)
13334 {
13335 plt = htab->elf.iplt;
13336 relplt = htab->elf.irelplt;
13337 htab->local_ifunc_resolver = 1;
13338 if (htab->opd_abi)
13339 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13340 else
13341 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13342 }
13343 else
13344 {
13345 plt = htab->pltlocal;
13346 if (bfd_link_pic (info))
13347 {
13348 relplt = htab->relpltlocal;
13349 if (htab->opd_abi)
13350 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13351 else
13352 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13353 }
13354 else
13355 relplt = NULL;
13356 }
13357 rela.r_addend = (h->root.u.def.value
13358 + h->root.u.def.section->output_offset
13359 + h->root.u.def.section->output_section->vma
13360 + ent->addend);
13361
13362 if (relplt == NULL)
13363 {
13364 loc = plt->contents + ent->plt.offset;
13365 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13366 if (htab->opd_abi)
13367 {
13368 bfd_vma toc = elf_gp (info->output_bfd);
13369 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13370 bfd_put_64 (info->output_bfd, toc, loc + 8);
13371 }
13372 }
13373 else
13374 {
13375 rela.r_offset = (plt->output_section->vma
13376 + plt->output_offset
13377 + ent->plt.offset);
13378 loc = relplt->contents + (relplt->reloc_count++
13379 * sizeof (Elf64_External_Rela));
13380 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13381 }
13382 }
13383 else
13384 {
13385 rela.r_offset = (htab->elf.splt->output_section->vma
13386 + htab->elf.splt->output_offset
13387 + ent->plt.offset);
13388 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13389 rela.r_addend = ent->addend;
13390 loc = (htab->elf.srelplt->contents
13391 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13392 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13393 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13394 htab->maybe_local_ifunc_resolver = 1;
13395 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13396 }
13397 }
13398
13399 if (!h->pointer_equality_needed)
13400 return TRUE;
13401
13402 if (h->def_regular)
13403 return TRUE;
13404
13405 s = htab->global_entry;
13406 if (s == NULL || s->size == 0)
13407 return TRUE;
13408
13409 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13410 if (ent->plt.offset != (bfd_vma) -1
13411 && ent->addend == 0)
13412 {
13413 bfd_byte *p;
13414 asection *plt;
13415 bfd_vma off;
13416
13417 p = s->contents + h->root.u.def.value;
13418 plt = htab->elf.splt;
13419 if (!htab->elf.dynamic_sections_created
13420 || h->dynindx == -1)
13421 {
13422 if (h->type == STT_GNU_IFUNC)
13423 plt = htab->elf.iplt;
13424 else
13425 plt = htab->pltlocal;
13426 }
13427 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13428 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13429
13430 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13431 {
13432 info->callbacks->einfo
13433 (_("%P: linkage table error against `%pT'\n"),
13434 h->root.root.string);
13435 bfd_set_error (bfd_error_bad_value);
13436 htab->stub_error = TRUE;
13437 }
13438
13439 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13440 if (htab->params->emit_stub_syms)
13441 {
13442 size_t len = strlen (h->root.root.string);
13443 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13444
13445 if (name == NULL)
13446 return FALSE;
13447
13448 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13449 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13450 if (h == NULL)
13451 return FALSE;
13452 if (h->root.type == bfd_link_hash_new)
13453 {
13454 h->root.type = bfd_link_hash_defined;
13455 h->root.u.def.section = s;
13456 h->root.u.def.value = p - s->contents;
13457 h->ref_regular = 1;
13458 h->def_regular = 1;
13459 h->ref_regular_nonweak = 1;
13460 h->forced_local = 1;
13461 h->non_elf = 0;
13462 h->root.linker_def = 1;
13463 }
13464 }
13465
13466 if (PPC_HA (off) != 0)
13467 {
13468 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13469 p += 4;
13470 }
13471 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13472 p += 4;
13473 bfd_put_32 (s->owner, MTCTR_R12, p);
13474 p += 4;
13475 bfd_put_32 (s->owner, BCTR, p);
13476 break;
13477 }
13478 return TRUE;
13479 }
13480
13481 /* Write PLT relocs for locals. */
13482
13483 static bfd_boolean
13484 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13485 {
13486 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13487 bfd *ibfd;
13488
13489 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13490 {
13491 struct got_entry **lgot_ents, **end_lgot_ents;
13492 struct plt_entry **local_plt, **lplt, **end_local_plt;
13493 Elf_Internal_Shdr *symtab_hdr;
13494 bfd_size_type locsymcount;
13495 Elf_Internal_Sym *local_syms = NULL;
13496 struct plt_entry *ent;
13497
13498 if (!is_ppc64_elf (ibfd))
13499 continue;
13500
13501 lgot_ents = elf_local_got_ents (ibfd);
13502 if (!lgot_ents)
13503 continue;
13504
13505 symtab_hdr = &elf_symtab_hdr (ibfd);
13506 locsymcount = symtab_hdr->sh_info;
13507 end_lgot_ents = lgot_ents + locsymcount;
13508 local_plt = (struct plt_entry **) end_lgot_ents;
13509 end_local_plt = local_plt + locsymcount;
13510 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13511 for (ent = *lplt; ent != NULL; ent = ent->next)
13512 if (ent->plt.offset != (bfd_vma) -1)
13513 {
13514 Elf_Internal_Sym *sym;
13515 asection *sym_sec;
13516 asection *plt, *relplt;
13517 bfd_byte *loc;
13518 bfd_vma val;
13519
13520 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13521 lplt - local_plt, ibfd))
13522 {
13523 if (local_syms != NULL
13524 && symtab_hdr->contents != (unsigned char *) local_syms)
13525 free (local_syms);
13526 return FALSE;
13527 }
13528
13529 val = sym->st_value + ent->addend;
13530 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13531 if (sym_sec != NULL && sym_sec->output_section != NULL)
13532 val += sym_sec->output_offset + sym_sec->output_section->vma;
13533
13534 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13535 {
13536 htab->local_ifunc_resolver = 1;
13537 plt = htab->elf.iplt;
13538 relplt = htab->elf.irelplt;
13539 }
13540 else
13541 {
13542 plt = htab->pltlocal;
13543 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13544 }
13545
13546 if (relplt == NULL)
13547 {
13548 loc = plt->contents + ent->plt.offset;
13549 bfd_put_64 (info->output_bfd, val, loc);
13550 if (htab->opd_abi)
13551 {
13552 bfd_vma toc = elf_gp (ibfd);
13553 bfd_put_64 (info->output_bfd, toc, loc + 8);
13554 }
13555 }
13556 else
13557 {
13558 Elf_Internal_Rela rela;
13559 rela.r_offset = (ent->plt.offset
13560 + plt->output_offset
13561 + plt->output_section->vma);
13562 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13563 {
13564 if (htab->opd_abi)
13565 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13566 else
13567 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13568 }
13569 else
13570 {
13571 if (htab->opd_abi)
13572 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13573 else
13574 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13575 }
13576 rela.r_addend = val;
13577 loc = relplt->contents + (relplt->reloc_count++
13578 * sizeof (Elf64_External_Rela));
13579 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13580 }
13581 }
13582
13583 if (local_syms != NULL
13584 && symtab_hdr->contents != (unsigned char *) local_syms)
13585 {
13586 if (!info->keep_memory)
13587 free (local_syms);
13588 else
13589 symtab_hdr->contents = (unsigned char *) local_syms;
13590 }
13591 }
13592 return TRUE;
13593 }
13594
13595 /* Build all the stubs associated with the current output file.
13596 The stubs are kept in a hash table attached to the main linker
13597 hash table. This function is called via gldelf64ppc_finish. */
13598
13599 bfd_boolean
13600 ppc64_elf_build_stubs (struct bfd_link_info *info,
13601 char **stats)
13602 {
13603 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13604 struct map_stub *group;
13605 asection *stub_sec;
13606 bfd_byte *p;
13607 int stub_sec_count = 0;
13608
13609 if (htab == NULL)
13610 return FALSE;
13611
13612 /* Allocate memory to hold the linker stubs. */
13613 for (group = htab->group; group != NULL; group = group->next)
13614 if ((stub_sec = group->stub_sec) != NULL
13615 && stub_sec->size != 0)
13616 {
13617 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13618 if (stub_sec->contents == NULL)
13619 return FALSE;
13620 stub_sec->size = 0;
13621 }
13622
13623 if (htab->glink != NULL && htab->glink->size != 0)
13624 {
13625 unsigned int indx;
13626 bfd_vma plt0;
13627
13628 /* Build the .glink plt call stub. */
13629 if (htab->params->emit_stub_syms)
13630 {
13631 struct elf_link_hash_entry *h;
13632 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13633 TRUE, FALSE, FALSE);
13634 if (h == NULL)
13635 return FALSE;
13636 if (h->root.type == bfd_link_hash_new)
13637 {
13638 h->root.type = bfd_link_hash_defined;
13639 h->root.u.def.section = htab->glink;
13640 h->root.u.def.value = 8;
13641 h->ref_regular = 1;
13642 h->def_regular = 1;
13643 h->ref_regular_nonweak = 1;
13644 h->forced_local = 1;
13645 h->non_elf = 0;
13646 h->root.linker_def = 1;
13647 }
13648 }
13649 plt0 = (htab->elf.splt->output_section->vma
13650 + htab->elf.splt->output_offset
13651 - 16);
13652 if (info->emitrelocations)
13653 {
13654 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13655 if (r == NULL)
13656 return FALSE;
13657 r->r_offset = (htab->glink->output_offset
13658 + htab->glink->output_section->vma);
13659 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13660 r->r_addend = plt0;
13661 }
13662 p = htab->glink->contents;
13663 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13664 bfd_put_64 (htab->glink->owner, plt0, p);
13665 p += 8;
13666 if (htab->opd_abi)
13667 {
13668 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13669 p += 4;
13670 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13671 p += 4;
13672 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13673 p += 4;
13674 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13675 p += 4;
13676 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13677 p += 4;
13678 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13679 p += 4;
13680 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13681 p += 4;
13682 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13683 p += 4;
13684 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13685 p += 4;
13686 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13687 p += 4;
13688 }
13689 else
13690 {
13691 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13692 p += 4;
13693 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13694 p += 4;
13695 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13696 p += 4;
13697 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13698 p += 4;
13699 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13700 p += 4;
13701 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13702 p += 4;
13703 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13704 p += 4;
13705 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13706 p += 4;
13707 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13708 p += 4;
13709 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13710 p += 4;
13711 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13712 p += 4;
13713 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13714 p += 4;
13715 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13716 p += 4;
13717 }
13718 bfd_put_32 (htab->glink->owner, BCTR, p);
13719 p += 4;
13720 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13721
13722 /* Build the .glink lazy link call stubs. */
13723 indx = 0;
13724 while (p < htab->glink->contents + htab->glink->size)
13725 {
13726 if (htab->opd_abi)
13727 {
13728 if (indx < 0x8000)
13729 {
13730 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13731 p += 4;
13732 }
13733 else
13734 {
13735 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13736 p += 4;
13737 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13738 p);
13739 p += 4;
13740 }
13741 }
13742 bfd_put_32 (htab->glink->owner,
13743 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13744 indx++;
13745 p += 4;
13746 }
13747 }
13748
13749 /* Build .glink global entry stubs, and PLT relocs for globals. */
13750 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13751
13752 if (!write_plt_relocs_for_local_syms (info))
13753 return FALSE;
13754
13755 if (htab->brlt != NULL && htab->brlt->size != 0)
13756 {
13757 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13758 htab->brlt->size);
13759 if (htab->brlt->contents == NULL)
13760 return FALSE;
13761 }
13762 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13763 {
13764 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13765 htab->relbrlt->size);
13766 if (htab->relbrlt->contents == NULL)
13767 return FALSE;
13768 }
13769
13770 /* Build the stubs as directed by the stub hash table. */
13771 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13772
13773 for (group = htab->group; group != NULL; group = group->next)
13774 if (group->needs_save_res)
13775 group->stub_sec->size += htab->sfpr->size;
13776
13777 if (htab->relbrlt != NULL)
13778 htab->relbrlt->reloc_count = 0;
13779
13780 if (htab->params->plt_stub_align != 0)
13781 for (group = htab->group; group != NULL; group = group->next)
13782 if ((stub_sec = group->stub_sec) != NULL)
13783 {
13784 int align = abs (htab->params->plt_stub_align);
13785 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13786 }
13787
13788 for (group = htab->group; group != NULL; group = group->next)
13789 if (group->needs_save_res)
13790 {
13791 stub_sec = group->stub_sec;
13792 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13793 htab->sfpr->contents, htab->sfpr->size);
13794 if (htab->params->emit_stub_syms)
13795 {
13796 unsigned int i;
13797
13798 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13799 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13800 return FALSE;
13801 }
13802 }
13803
13804 for (group = htab->group; group != NULL; group = group->next)
13805 if ((stub_sec = group->stub_sec) != NULL)
13806 {
13807 stub_sec_count += 1;
13808 if (stub_sec->rawsize != stub_sec->size
13809 && (htab->stub_iteration <= STUB_SHRINK_ITER
13810 || stub_sec->rawsize < stub_sec->size))
13811 break;
13812 }
13813
13814 if (group != NULL)
13815 {
13816 htab->stub_error = TRUE;
13817 _bfd_error_handler (_("stubs don't match calculated size"));
13818 }
13819
13820 if (htab->stub_error)
13821 return FALSE;
13822
13823 if (stats != NULL)
13824 {
13825 size_t len;
13826 *stats = bfd_malloc (500);
13827 if (*stats == NULL)
13828 return FALSE;
13829
13830 len = sprintf (*stats,
13831 ngettext ("linker stubs in %u group\n",
13832 "linker stubs in %u groups\n",
13833 stub_sec_count),
13834 stub_sec_count);
13835 sprintf (*stats + len, _(" branch %lu\n"
13836 " toc adjust %lu\n"
13837 " long branch %lu\n"
13838 " long toc adj %lu\n"
13839 " plt call %lu\n"
13840 " plt call toc %lu\n"
13841 " global entry %lu"),
13842 htab->stub_count[ppc_stub_long_branch - 1],
13843 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13844 htab->stub_count[ppc_stub_plt_branch - 1],
13845 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13846 htab->stub_count[ppc_stub_plt_call - 1],
13847 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13848 htab->stub_count[ppc_stub_global_entry - 1]);
13849 }
13850 return TRUE;
13851 }
13852
13853 /* What to do when ld finds relocations against symbols defined in
13854 discarded sections. */
13855
13856 static unsigned int
13857 ppc64_elf_action_discarded (asection *sec)
13858 {
13859 if (strcmp (".opd", sec->name) == 0)
13860 return 0;
13861
13862 if (strcmp (".toc", sec->name) == 0)
13863 return 0;
13864
13865 if (strcmp (".toc1", sec->name) == 0)
13866 return 0;
13867
13868 return _bfd_elf_default_action_discarded (sec);
13869 }
13870
13871 /* The RELOCATE_SECTION function is called by the ELF backend linker
13872 to handle the relocations for a section.
13873
13874 The relocs are always passed as Rela structures; if the section
13875 actually uses Rel structures, the r_addend field will always be
13876 zero.
13877
13878 This function is responsible for adjust the section contents as
13879 necessary, and (if using Rela relocs and generating a
13880 relocatable output file) adjusting the reloc addend as
13881 necessary.
13882
13883 This function does not have to worry about setting the reloc
13884 address or the reloc symbol index.
13885
13886 LOCAL_SYMS is a pointer to the swapped in local symbols.
13887
13888 LOCAL_SECTIONS is an array giving the section in the input file
13889 corresponding to the st_shndx field of each local symbol.
13890
13891 The global hash table entry for the global symbols can be found
13892 via elf_sym_hashes (input_bfd).
13893
13894 When generating relocatable output, this function must handle
13895 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13896 going to be the section symbol corresponding to the output
13897 section, which means that the addend must be adjusted
13898 accordingly. */
13899
13900 static bfd_boolean
13901 ppc64_elf_relocate_section (bfd *output_bfd,
13902 struct bfd_link_info *info,
13903 bfd *input_bfd,
13904 asection *input_section,
13905 bfd_byte *contents,
13906 Elf_Internal_Rela *relocs,
13907 Elf_Internal_Sym *local_syms,
13908 asection **local_sections)
13909 {
13910 struct ppc_link_hash_table *htab;
13911 Elf_Internal_Shdr *symtab_hdr;
13912 struct elf_link_hash_entry **sym_hashes;
13913 Elf_Internal_Rela *rel;
13914 Elf_Internal_Rela *wrel;
13915 Elf_Internal_Rela *relend;
13916 Elf_Internal_Rela outrel;
13917 bfd_byte *loc;
13918 struct got_entry **local_got_ents;
13919 bfd_vma TOCstart;
13920 bfd_boolean ret = TRUE;
13921 bfd_boolean is_opd;
13922 /* Assume 'at' branch hints. */
13923 bfd_boolean is_isa_v2 = TRUE;
13924 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13925
13926 /* Initialize howto table if needed. */
13927 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13928 ppc_howto_init ();
13929
13930 htab = ppc_hash_table (info);
13931 if (htab == NULL)
13932 return FALSE;
13933
13934 /* Don't relocate stub sections. */
13935 if (input_section->owner == htab->params->stub_bfd)
13936 return TRUE;
13937
13938 BFD_ASSERT (is_ppc64_elf (input_bfd));
13939
13940 local_got_ents = elf_local_got_ents (input_bfd);
13941 TOCstart = elf_gp (output_bfd);
13942 symtab_hdr = &elf_symtab_hdr (input_bfd);
13943 sym_hashes = elf_sym_hashes (input_bfd);
13944 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13945
13946 rel = wrel = relocs;
13947 relend = relocs + input_section->reloc_count;
13948 for (; rel < relend; wrel++, rel++)
13949 {
13950 enum elf_ppc64_reloc_type r_type;
13951 bfd_vma addend;
13952 bfd_reloc_status_type r;
13953 Elf_Internal_Sym *sym;
13954 asection *sec;
13955 struct elf_link_hash_entry *h_elf;
13956 struct ppc_link_hash_entry *h;
13957 struct ppc_link_hash_entry *fdh;
13958 const char *sym_name;
13959 unsigned long r_symndx, toc_symndx;
13960 bfd_vma toc_addend;
13961 unsigned char tls_mask, tls_gd, tls_type;
13962 unsigned char sym_type;
13963 bfd_vma relocation;
13964 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13965 bfd_boolean warned;
13966 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13967 unsigned int insn;
13968 unsigned int mask;
13969 struct ppc_stub_hash_entry *stub_entry;
13970 bfd_vma max_br_offset;
13971 bfd_vma from;
13972 Elf_Internal_Rela orig_rel;
13973 reloc_howto_type *howto;
13974 struct reloc_howto_struct alt_howto;
13975
13976 again:
13977 orig_rel = *rel;
13978
13979 r_type = ELF64_R_TYPE (rel->r_info);
13980 r_symndx = ELF64_R_SYM (rel->r_info);
13981
13982 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13983 symbol of the previous ADDR64 reloc. The symbol gives us the
13984 proper TOC base to use. */
13985 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13986 && wrel != relocs
13987 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13988 && is_opd)
13989 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13990
13991 sym = NULL;
13992 sec = NULL;
13993 h_elf = NULL;
13994 sym_name = NULL;
13995 unresolved_reloc = FALSE;
13996 warned = FALSE;
13997
13998 if (r_symndx < symtab_hdr->sh_info)
13999 {
14000 /* It's a local symbol. */
14001 struct _opd_sec_data *opd;
14002
14003 sym = local_syms + r_symndx;
14004 sec = local_sections[r_symndx];
14005 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14006 sym_type = ELF64_ST_TYPE (sym->st_info);
14007 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14008 opd = get_opd_info (sec);
14009 if (opd != NULL && opd->adjust != NULL)
14010 {
14011 long adjust = opd->adjust[OPD_NDX (sym->st_value
14012 + rel->r_addend)];
14013 if (adjust == -1)
14014 relocation = 0;
14015 else
14016 {
14017 /* If this is a relocation against the opd section sym
14018 and we have edited .opd, adjust the reloc addend so
14019 that ld -r and ld --emit-relocs output is correct.
14020 If it is a reloc against some other .opd symbol,
14021 then the symbol value will be adjusted later. */
14022 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14023 rel->r_addend += adjust;
14024 else
14025 relocation += adjust;
14026 }
14027 }
14028 }
14029 else
14030 {
14031 bfd_boolean ignored;
14032
14033 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14034 r_symndx, symtab_hdr, sym_hashes,
14035 h_elf, sec, relocation,
14036 unresolved_reloc, warned, ignored);
14037 sym_name = h_elf->root.root.string;
14038 sym_type = h_elf->type;
14039 if (sec != NULL
14040 && sec->owner == output_bfd
14041 && strcmp (sec->name, ".opd") == 0)
14042 {
14043 /* This is a symbol defined in a linker script. All
14044 such are defined in output sections, even those
14045 defined by simple assignment from a symbol defined in
14046 an input section. Transfer the symbol to an
14047 appropriate input .opd section, so that a branch to
14048 this symbol will be mapped to the location specified
14049 by the opd entry. */
14050 struct bfd_link_order *lo;
14051 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14052 if (lo->type == bfd_indirect_link_order)
14053 {
14054 asection *isec = lo->u.indirect.section;
14055 if (h_elf->root.u.def.value >= isec->output_offset
14056 && h_elf->root.u.def.value < (isec->output_offset
14057 + isec->size))
14058 {
14059 h_elf->root.u.def.value -= isec->output_offset;
14060 h_elf->root.u.def.section = isec;
14061 sec = isec;
14062 break;
14063 }
14064 }
14065 }
14066 }
14067 h = (struct ppc_link_hash_entry *) h_elf;
14068
14069 if (sec != NULL && discarded_section (sec))
14070 {
14071 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14072 input_bfd, input_section,
14073 contents + rel->r_offset);
14074 wrel->r_offset = rel->r_offset;
14075 wrel->r_info = 0;
14076 wrel->r_addend = 0;
14077
14078 /* For ld -r, remove relocations in debug sections against
14079 symbols defined in discarded sections. Not done for
14080 non-debug to preserve relocs in .eh_frame which the
14081 eh_frame editing code expects to be present. */
14082 if (bfd_link_relocatable (info)
14083 && (input_section->flags & SEC_DEBUGGING))
14084 wrel--;
14085
14086 continue;
14087 }
14088
14089 if (bfd_link_relocatable (info))
14090 goto copy_reloc;
14091
14092 if (h != NULL && &h->elf == htab->elf.hgot)
14093 {
14094 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14095 sec = bfd_abs_section_ptr;
14096 unresolved_reloc = FALSE;
14097 }
14098
14099 /* TLS optimizations. Replace instruction sequences and relocs
14100 based on information we collected in tls_optimize. We edit
14101 RELOCS so that --emit-relocs will output something sensible
14102 for the final instruction stream. */
14103 tls_mask = 0;
14104 tls_gd = 0;
14105 toc_symndx = 0;
14106 if (h != NULL)
14107 tls_mask = h->tls_mask;
14108 else if (local_got_ents != NULL)
14109 {
14110 struct plt_entry **local_plt = (struct plt_entry **)
14111 (local_got_ents + symtab_hdr->sh_info);
14112 unsigned char *lgot_masks = (unsigned char *)
14113 (local_plt + symtab_hdr->sh_info);
14114 tls_mask = lgot_masks[r_symndx];
14115 }
14116 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14117 && (r_type == R_PPC64_TLS
14118 || r_type == R_PPC64_TLSGD
14119 || r_type == R_PPC64_TLSLD))
14120 {
14121 /* Check for toc tls entries. */
14122 unsigned char *toc_tls;
14123
14124 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14125 &local_syms, rel, input_bfd))
14126 return FALSE;
14127
14128 if (toc_tls)
14129 tls_mask = *toc_tls;
14130 }
14131
14132 /* Check that tls relocs are used with tls syms, and non-tls
14133 relocs are used with non-tls syms. */
14134 if (r_symndx != STN_UNDEF
14135 && r_type != R_PPC64_NONE
14136 && (h == NULL
14137 || h->elf.root.type == bfd_link_hash_defined
14138 || h->elf.root.type == bfd_link_hash_defweak)
14139 && (IS_PPC64_TLS_RELOC (r_type)
14140 != (sym_type == STT_TLS
14141 || (sym_type == STT_SECTION
14142 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14143 {
14144 if ((tls_mask & TLS_TLS) != 0
14145 && (r_type == R_PPC64_TLS
14146 || r_type == R_PPC64_TLSGD
14147 || r_type == R_PPC64_TLSLD))
14148 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14149 ;
14150 else
14151 info->callbacks->einfo
14152 (!IS_PPC64_TLS_RELOC (r_type)
14153 /* xgettext:c-format */
14154 ? _("%H: %s used with TLS symbol `%pT'\n")
14155 /* xgettext:c-format */
14156 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14157 input_bfd, input_section, rel->r_offset,
14158 ppc64_elf_howto_table[r_type]->name,
14159 sym_name);
14160 }
14161
14162 /* Ensure reloc mapping code below stays sane. */
14163 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14164 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14165 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14166 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14167 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14168 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14169 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14170 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14171 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14172 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14173 abort ();
14174
14175 switch (r_type)
14176 {
14177 default:
14178 break;
14179
14180 case R_PPC64_LO_DS_OPT:
14181 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14182 if ((insn & (0x3f << 26)) != 58u << 26)
14183 abort ();
14184 insn += (14u << 26) - (58u << 26);
14185 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14186 r_type = R_PPC64_TOC16_LO;
14187 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14188 break;
14189
14190 case R_PPC64_TOC16:
14191 case R_PPC64_TOC16_LO:
14192 case R_PPC64_TOC16_DS:
14193 case R_PPC64_TOC16_LO_DS:
14194 {
14195 /* Check for toc tls entries. */
14196 unsigned char *toc_tls;
14197 int retval;
14198
14199 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14200 &local_syms, rel, input_bfd);
14201 if (retval == 0)
14202 return FALSE;
14203
14204 if (toc_tls)
14205 {
14206 tls_mask = *toc_tls;
14207 if (r_type == R_PPC64_TOC16_DS
14208 || r_type == R_PPC64_TOC16_LO_DS)
14209 {
14210 if ((tls_mask & TLS_TLS) != 0
14211 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14212 goto toctprel;
14213 }
14214 else
14215 {
14216 /* If we found a GD reloc pair, then we might be
14217 doing a GD->IE transition. */
14218 if (retval == 2)
14219 {
14220 tls_gd = TLS_TPRELGD;
14221 if ((tls_mask & TLS_TLS) != 0
14222 && (tls_mask & TLS_GD) == 0)
14223 goto tls_ldgd_opt;
14224 }
14225 else if (retval == 3)
14226 {
14227 if ((tls_mask & TLS_TLS) != 0
14228 && (tls_mask & TLS_LD) == 0)
14229 goto tls_ldgd_opt;
14230 }
14231 }
14232 }
14233 }
14234 break;
14235
14236 case R_PPC64_GOT_TPREL16_HI:
14237 case R_PPC64_GOT_TPREL16_HA:
14238 if ((tls_mask & TLS_TLS) != 0
14239 && (tls_mask & TLS_TPREL) == 0)
14240 {
14241 rel->r_offset -= d_offset;
14242 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14243 r_type = R_PPC64_NONE;
14244 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14245 }
14246 break;
14247
14248 case R_PPC64_GOT_TPREL16_DS:
14249 case R_PPC64_GOT_TPREL16_LO_DS:
14250 if ((tls_mask & TLS_TLS) != 0
14251 && (tls_mask & TLS_TPREL) == 0)
14252 {
14253 toctprel:
14254 insn = bfd_get_32 (input_bfd,
14255 contents + rel->r_offset - d_offset);
14256 insn &= 31 << 21;
14257 insn |= 0x3c0d0000; /* addis 0,13,0 */
14258 bfd_put_32 (input_bfd, insn,
14259 contents + rel->r_offset - d_offset);
14260 r_type = R_PPC64_TPREL16_HA;
14261 if (toc_symndx != 0)
14262 {
14263 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14264 rel->r_addend = toc_addend;
14265 /* We changed the symbol. Start over in order to
14266 get h, sym, sec etc. right. */
14267 goto again;
14268 }
14269 else
14270 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14271 }
14272 break;
14273
14274 case R_PPC64_TLS:
14275 if ((tls_mask & TLS_TLS) != 0
14276 && (tls_mask & TLS_TPREL) == 0)
14277 {
14278 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14279 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14280 if (insn == 0)
14281 abort ();
14282 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14283 /* Was PPC64_TLS which sits on insn boundary, now
14284 PPC64_TPREL16_LO which is at low-order half-word. */
14285 rel->r_offset += d_offset;
14286 r_type = R_PPC64_TPREL16_LO;
14287 if (toc_symndx != 0)
14288 {
14289 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14290 rel->r_addend = toc_addend;
14291 /* We changed the symbol. Start over in order to
14292 get h, sym, sec etc. right. */
14293 goto again;
14294 }
14295 else
14296 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14297 }
14298 break;
14299
14300 case R_PPC64_GOT_TLSGD16_HI:
14301 case R_PPC64_GOT_TLSGD16_HA:
14302 tls_gd = TLS_TPRELGD;
14303 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14304 goto tls_gdld_hi;
14305 break;
14306
14307 case R_PPC64_GOT_TLSLD16_HI:
14308 case R_PPC64_GOT_TLSLD16_HA:
14309 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14310 {
14311 tls_gdld_hi:
14312 if ((tls_mask & tls_gd) != 0)
14313 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14314 + R_PPC64_GOT_TPREL16_DS);
14315 else
14316 {
14317 rel->r_offset -= d_offset;
14318 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14319 r_type = R_PPC64_NONE;
14320 }
14321 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14322 }
14323 break;
14324
14325 case R_PPC64_GOT_TLSGD16:
14326 case R_PPC64_GOT_TLSGD16_LO:
14327 tls_gd = TLS_TPRELGD;
14328 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14329 goto tls_ldgd_opt;
14330 break;
14331
14332 case R_PPC64_GOT_TLSLD16:
14333 case R_PPC64_GOT_TLSLD16_LO:
14334 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14335 {
14336 unsigned int insn1, insn2;
14337 bfd_vma offset;
14338
14339 tls_ldgd_opt:
14340 offset = (bfd_vma) -1;
14341 /* If not using the newer R_PPC64_TLSGD/LD to mark
14342 __tls_get_addr calls, we must trust that the call
14343 stays with its arg setup insns, ie. that the next
14344 reloc is the __tls_get_addr call associated with
14345 the current reloc. Edit both insns. */
14346 if (input_section->has_tls_get_addr_call
14347 && rel + 1 < relend
14348 && branch_reloc_hash_match (input_bfd, rel + 1,
14349 htab->tls_get_addr,
14350 htab->tls_get_addr_fd))
14351 offset = rel[1].r_offset;
14352 /* We read the low GOT_TLS (or TOC16) insn because we
14353 need to keep the destination reg. It may be
14354 something other than the usual r3, and moved to r3
14355 before the call by intervening code. */
14356 insn1 = bfd_get_32 (input_bfd,
14357 contents + rel->r_offset - d_offset);
14358 if ((tls_mask & tls_gd) != 0)
14359 {
14360 /* IE */
14361 insn1 &= (0x1f << 21) | (0x1f << 16);
14362 insn1 |= 58 << 26; /* ld */
14363 insn2 = 0x7c636a14; /* add 3,3,13 */
14364 if (offset != (bfd_vma) -1)
14365 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14366 if ((tls_mask & TLS_EXPLICIT) == 0)
14367 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14368 + R_PPC64_GOT_TPREL16_DS);
14369 else
14370 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14371 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14372 }
14373 else
14374 {
14375 /* LE */
14376 insn1 &= 0x1f << 21;
14377 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14378 insn2 = 0x38630000; /* addi 3,3,0 */
14379 if (tls_gd == 0)
14380 {
14381 /* Was an LD reloc. */
14382 if (toc_symndx)
14383 sec = local_sections[toc_symndx];
14384 for (r_symndx = 0;
14385 r_symndx < symtab_hdr->sh_info;
14386 r_symndx++)
14387 if (local_sections[r_symndx] == sec)
14388 break;
14389 if (r_symndx >= symtab_hdr->sh_info)
14390 r_symndx = STN_UNDEF;
14391 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14392 if (r_symndx != STN_UNDEF)
14393 rel->r_addend -= (local_syms[r_symndx].st_value
14394 + sec->output_offset
14395 + sec->output_section->vma);
14396 }
14397 else if (toc_symndx != 0)
14398 {
14399 r_symndx = toc_symndx;
14400 rel->r_addend = toc_addend;
14401 }
14402 r_type = R_PPC64_TPREL16_HA;
14403 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14404 if (offset != (bfd_vma) -1)
14405 {
14406 rel[1].r_info = ELF64_R_INFO (r_symndx,
14407 R_PPC64_TPREL16_LO);
14408 rel[1].r_offset = offset + d_offset;
14409 rel[1].r_addend = rel->r_addend;
14410 }
14411 }
14412 bfd_put_32 (input_bfd, insn1,
14413 contents + rel->r_offset - d_offset);
14414 if (offset != (bfd_vma) -1)
14415 bfd_put_32 (input_bfd, insn2, contents + offset);
14416 if ((tls_mask & tls_gd) == 0
14417 && (tls_gd == 0 || toc_symndx != 0))
14418 {
14419 /* We changed the symbol. Start over in order
14420 to get h, sym, sec etc. right. */
14421 goto again;
14422 }
14423 }
14424 break;
14425
14426 case R_PPC64_TLSGD:
14427 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14428 && rel + 1 < relend)
14429 {
14430 unsigned int insn2;
14431 bfd_vma offset = rel->r_offset;
14432
14433 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14434 {
14435 bfd_put_32 (output_bfd, NOP, contents + offset);
14436 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14437 break;
14438 }
14439
14440 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14441 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14442
14443 if ((tls_mask & TLS_TPRELGD) != 0)
14444 {
14445 /* IE */
14446 r_type = R_PPC64_NONE;
14447 insn2 = 0x7c636a14; /* add 3,3,13 */
14448 }
14449 else
14450 {
14451 /* LE */
14452 if (toc_symndx != 0)
14453 {
14454 r_symndx = toc_symndx;
14455 rel->r_addend = toc_addend;
14456 }
14457 r_type = R_PPC64_TPREL16_LO;
14458 rel->r_offset = offset + d_offset;
14459 insn2 = 0x38630000; /* addi 3,3,0 */
14460 }
14461 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14462 /* Zap the reloc on the _tls_get_addr call too. */
14463 BFD_ASSERT (offset == rel[1].r_offset);
14464 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14465 bfd_put_32 (input_bfd, insn2, contents + offset);
14466 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14467 goto again;
14468 }
14469 break;
14470
14471 case R_PPC64_TLSLD:
14472 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14473 && rel + 1 < relend)
14474 {
14475 unsigned int insn2;
14476 bfd_vma offset = rel->r_offset;
14477
14478 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14479 {
14480 bfd_put_32 (output_bfd, NOP, contents + offset);
14481 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14482 break;
14483 }
14484
14485 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14486 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14487
14488 if (toc_symndx)
14489 sec = local_sections[toc_symndx];
14490 for (r_symndx = 0;
14491 r_symndx < symtab_hdr->sh_info;
14492 r_symndx++)
14493 if (local_sections[r_symndx] == sec)
14494 break;
14495 if (r_symndx >= symtab_hdr->sh_info)
14496 r_symndx = STN_UNDEF;
14497 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14498 if (r_symndx != STN_UNDEF)
14499 rel->r_addend -= (local_syms[r_symndx].st_value
14500 + sec->output_offset
14501 + sec->output_section->vma);
14502
14503 r_type = R_PPC64_TPREL16_LO;
14504 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14505 rel->r_offset = offset + d_offset;
14506 /* Zap the reloc on the _tls_get_addr call too. */
14507 BFD_ASSERT (offset == rel[1].r_offset);
14508 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14509 insn2 = 0x38630000; /* addi 3,3,0 */
14510 bfd_put_32 (input_bfd, insn2, contents + offset);
14511 goto again;
14512 }
14513 break;
14514
14515 case R_PPC64_DTPMOD64:
14516 if (rel + 1 < relend
14517 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14518 && rel[1].r_offset == rel->r_offset + 8)
14519 {
14520 if ((tls_mask & TLS_GD) == 0)
14521 {
14522 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14523 if ((tls_mask & TLS_TPRELGD) != 0)
14524 r_type = R_PPC64_TPREL64;
14525 else
14526 {
14527 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14528 r_type = R_PPC64_NONE;
14529 }
14530 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14531 }
14532 }
14533 else
14534 {
14535 if ((tls_mask & TLS_LD) == 0)
14536 {
14537 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14538 r_type = R_PPC64_NONE;
14539 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14540 }
14541 }
14542 break;
14543
14544 case R_PPC64_TPREL64:
14545 if ((tls_mask & TLS_TPREL) == 0)
14546 {
14547 r_type = R_PPC64_NONE;
14548 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14549 }
14550 break;
14551
14552 case R_PPC64_ENTRY:
14553 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14554 if (!bfd_link_pic (info)
14555 && !info->traditional_format
14556 && relocation + 0x80008000 <= 0xffffffff)
14557 {
14558 unsigned int insn1, insn2;
14559
14560 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14561 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14562 if ((insn1 & ~0xfffc) == LD_R2_0R12
14563 && insn2 == ADD_R2_R2_R12)
14564 {
14565 bfd_put_32 (input_bfd,
14566 LIS_R2 + PPC_HA (relocation),
14567 contents + rel->r_offset);
14568 bfd_put_32 (input_bfd,
14569 ADDI_R2_R2 + PPC_LO (relocation),
14570 contents + rel->r_offset + 4);
14571 }
14572 }
14573 else
14574 {
14575 relocation -= (rel->r_offset
14576 + input_section->output_offset
14577 + input_section->output_section->vma);
14578 if (relocation + 0x80008000 <= 0xffffffff)
14579 {
14580 unsigned int insn1, insn2;
14581
14582 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14583 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14584 if ((insn1 & ~0xfffc) == LD_R2_0R12
14585 && insn2 == ADD_R2_R2_R12)
14586 {
14587 bfd_put_32 (input_bfd,
14588 ADDIS_R2_R12 + PPC_HA (relocation),
14589 contents + rel->r_offset);
14590 bfd_put_32 (input_bfd,
14591 ADDI_R2_R2 + PPC_LO (relocation),
14592 contents + rel->r_offset + 4);
14593 }
14594 }
14595 }
14596 break;
14597
14598 case R_PPC64_REL16_HA:
14599 /* If we are generating a non-PIC executable, edit
14600 . 0: addis 2,12,.TOC.-0b@ha
14601 . addi 2,2,.TOC.-0b@l
14602 used by ELFv2 global entry points to set up r2, to
14603 . lis 2,.TOC.@ha
14604 . addi 2,2,.TOC.@l
14605 if .TOC. is in range. */
14606 if (!bfd_link_pic (info)
14607 && !info->traditional_format
14608 && !htab->opd_abi
14609 && rel->r_addend == d_offset
14610 && h != NULL && &h->elf == htab->elf.hgot
14611 && rel + 1 < relend
14612 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14613 && rel[1].r_offset == rel->r_offset + 4
14614 && rel[1].r_addend == rel->r_addend + 4
14615 && relocation + 0x80008000 <= 0xffffffff)
14616 {
14617 unsigned int insn1, insn2;
14618 bfd_vma offset = rel->r_offset - d_offset;
14619 insn1 = bfd_get_32 (input_bfd, contents + offset);
14620 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14621 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14622 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14623 {
14624 r_type = R_PPC64_ADDR16_HA;
14625 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14626 rel->r_addend -= d_offset;
14627 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14628 rel[1].r_addend -= d_offset + 4;
14629 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14630 }
14631 }
14632 break;
14633 }
14634
14635 /* Handle other relocations that tweak non-addend part of insn. */
14636 insn = 0;
14637 max_br_offset = 1 << 25;
14638 addend = rel->r_addend;
14639 reloc_dest = DEST_NORMAL;
14640 switch (r_type)
14641 {
14642 default:
14643 break;
14644
14645 case R_PPC64_TOCSAVE:
14646 if (relocation + addend == (rel->r_offset
14647 + input_section->output_offset
14648 + input_section->output_section->vma)
14649 && tocsave_find (htab, NO_INSERT,
14650 &local_syms, rel, input_bfd))
14651 {
14652 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14653 if (insn == NOP
14654 || insn == CROR_151515 || insn == CROR_313131)
14655 bfd_put_32 (input_bfd,
14656 STD_R2_0R1 + STK_TOC (htab),
14657 contents + rel->r_offset);
14658 }
14659 break;
14660
14661 /* Branch taken prediction relocations. */
14662 case R_PPC64_ADDR14_BRTAKEN:
14663 case R_PPC64_REL14_BRTAKEN:
14664 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14665 /* Fall through. */
14666
14667 /* Branch not taken prediction relocations. */
14668 case R_PPC64_ADDR14_BRNTAKEN:
14669 case R_PPC64_REL14_BRNTAKEN:
14670 insn |= bfd_get_32 (input_bfd,
14671 contents + rel->r_offset) & ~(0x01 << 21);
14672 /* Fall through. */
14673
14674 case R_PPC64_REL14:
14675 max_br_offset = 1 << 15;
14676 /* Fall through. */
14677
14678 case R_PPC64_REL24:
14679 case R_PPC64_PLTCALL:
14680 /* Calls to functions with a different TOC, such as calls to
14681 shared objects, need to alter the TOC pointer. This is
14682 done using a linkage stub. A REL24 branching to these
14683 linkage stubs needs to be followed by a nop, as the nop
14684 will be replaced with an instruction to restore the TOC
14685 base pointer. */
14686 fdh = h;
14687 if (h != NULL
14688 && h->oh != NULL
14689 && h->oh->is_func_descriptor)
14690 fdh = ppc_follow_link (h->oh);
14691 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14692 htab);
14693 if (r_type == R_PPC64_PLTCALL
14694 && stub_entry != NULL
14695 && (stub_entry->stub_type == ppc_stub_plt_call
14696 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14697 stub_entry = NULL;
14698
14699 if (stub_entry != NULL
14700 && (stub_entry->stub_type == ppc_stub_plt_call
14701 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14702 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14703 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14704 {
14705 bfd_boolean can_plt_call = FALSE;
14706
14707 if (stub_entry->stub_type == ppc_stub_plt_call
14708 && !htab->opd_abi
14709 && htab->params->plt_localentry0 != 0
14710 && is_elfv2_localentry0 (&h->elf))
14711 {
14712 /* The function doesn't use or change r2. */
14713 can_plt_call = TRUE;
14714 }
14715
14716 /* All of these stubs may modify r2, so there must be a
14717 branch and link followed by a nop. The nop is
14718 replaced by an insn to restore r2. */
14719 else if (rel->r_offset + 8 <= input_section->size)
14720 {
14721 unsigned long br;
14722
14723 br = bfd_get_32 (input_bfd,
14724 contents + rel->r_offset);
14725 if ((br & 1) != 0)
14726 {
14727 unsigned long nop;
14728
14729 nop = bfd_get_32 (input_bfd,
14730 contents + rel->r_offset + 4);
14731 if (nop == LD_R2_0R1 + STK_TOC (htab))
14732 can_plt_call = TRUE;
14733 else if (nop == NOP
14734 || nop == CROR_151515
14735 || nop == CROR_313131)
14736 {
14737 if (h != NULL
14738 && (h == htab->tls_get_addr_fd
14739 || h == htab->tls_get_addr)
14740 && htab->params->tls_get_addr_opt)
14741 {
14742 /* Special stub used, leave nop alone. */
14743 }
14744 else
14745 bfd_put_32 (input_bfd,
14746 LD_R2_0R1 + STK_TOC (htab),
14747 contents + rel->r_offset + 4);
14748 can_plt_call = TRUE;
14749 }
14750 }
14751 }
14752
14753 if (!can_plt_call && h != NULL)
14754 {
14755 const char *name = h->elf.root.root.string;
14756
14757 if (*name == '.')
14758 ++name;
14759
14760 if (strncmp (name, "__libc_start_main", 17) == 0
14761 && (name[17] == 0 || name[17] == '@'))
14762 {
14763 /* Allow crt1 branch to go via a toc adjusting
14764 stub. Other calls that never return could do
14765 the same, if we could detect such. */
14766 can_plt_call = TRUE;
14767 }
14768 }
14769
14770 if (!can_plt_call)
14771 {
14772 /* g++ as of 20130507 emits self-calls without a
14773 following nop. This is arguably wrong since we
14774 have conflicting information. On the one hand a
14775 global symbol and on the other a local call
14776 sequence, but don't error for this special case.
14777 It isn't possible to cheaply verify we have
14778 exactly such a call. Allow all calls to the same
14779 section. */
14780 asection *code_sec = sec;
14781
14782 if (get_opd_info (sec) != NULL)
14783 {
14784 bfd_vma off = (relocation + addend
14785 - sec->output_section->vma
14786 - sec->output_offset);
14787
14788 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14789 }
14790 if (code_sec == input_section)
14791 can_plt_call = TRUE;
14792 }
14793
14794 if (!can_plt_call)
14795 {
14796 if (stub_entry->stub_type == ppc_stub_plt_call
14797 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14798 info->callbacks->einfo
14799 /* xgettext:c-format */
14800 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14801 "recompile with -fPIC\n"),
14802 input_bfd, input_section, rel->r_offset, sym_name);
14803 else
14804 info->callbacks->einfo
14805 /* xgettext:c-format */
14806 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14807 "(-mcmodel=small toc adjust stub)\n"),
14808 input_bfd, input_section, rel->r_offset, sym_name);
14809
14810 bfd_set_error (bfd_error_bad_value);
14811 ret = FALSE;
14812 }
14813
14814 if (can_plt_call
14815 && (stub_entry->stub_type == ppc_stub_plt_call
14816 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14817 unresolved_reloc = FALSE;
14818 }
14819
14820 if ((stub_entry == NULL
14821 || stub_entry->stub_type == ppc_stub_long_branch
14822 || stub_entry->stub_type == ppc_stub_plt_branch)
14823 && get_opd_info (sec) != NULL)
14824 {
14825 /* The branch destination is the value of the opd entry. */
14826 bfd_vma off = (relocation + addend
14827 - sec->output_section->vma
14828 - sec->output_offset);
14829 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14830 if (dest != (bfd_vma) -1)
14831 {
14832 relocation = dest;
14833 addend = 0;
14834 reloc_dest = DEST_OPD;
14835 }
14836 }
14837
14838 /* If the branch is out of reach we ought to have a long
14839 branch stub. */
14840 from = (rel->r_offset
14841 + input_section->output_offset
14842 + input_section->output_section->vma);
14843
14844 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14845 ? fdh->elf.other
14846 : sym->st_other);
14847
14848 if (stub_entry != NULL
14849 && (stub_entry->stub_type == ppc_stub_long_branch
14850 || stub_entry->stub_type == ppc_stub_plt_branch)
14851 && (r_type == R_PPC64_ADDR14_BRTAKEN
14852 || r_type == R_PPC64_ADDR14_BRNTAKEN
14853 || (relocation + addend - from + max_br_offset
14854 < 2 * max_br_offset)))
14855 /* Don't use the stub if this branch is in range. */
14856 stub_entry = NULL;
14857
14858 if (stub_entry != NULL)
14859 {
14860 /* Munge up the value and addend so that we call the stub
14861 rather than the procedure directly. */
14862 asection *stub_sec = stub_entry->group->stub_sec;
14863
14864 if (stub_entry->stub_type == ppc_stub_save_res)
14865 relocation += (stub_sec->output_offset
14866 + stub_sec->output_section->vma
14867 + stub_sec->size - htab->sfpr->size
14868 - htab->sfpr->output_offset
14869 - htab->sfpr->output_section->vma);
14870 else
14871 relocation = (stub_entry->stub_offset
14872 + stub_sec->output_offset
14873 + stub_sec->output_section->vma);
14874 addend = 0;
14875 reloc_dest = DEST_STUB;
14876
14877 if ((stub_entry->stub_type == ppc_stub_plt_call
14878 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14879 && (ALWAYS_EMIT_R2SAVE
14880 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14881 && rel + 1 < relend
14882 && rel[1].r_offset == rel->r_offset + 4
14883 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14884 relocation += 4;
14885 }
14886
14887 if (insn != 0)
14888 {
14889 if (is_isa_v2)
14890 {
14891 /* Set 'a' bit. This is 0b00010 in BO field for branch
14892 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14893 for branch on CTR insns (BO == 1a00t or 1a01t). */
14894 if ((insn & (0x14 << 21)) == (0x04 << 21))
14895 insn |= 0x02 << 21;
14896 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14897 insn |= 0x08 << 21;
14898 else
14899 break;
14900 }
14901 else
14902 {
14903 /* Invert 'y' bit if not the default. */
14904 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14905 insn ^= 0x01 << 21;
14906 }
14907
14908 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14909 }
14910
14911 /* NOP out calls to undefined weak functions.
14912 We can thus call a weak function without first
14913 checking whether the function is defined. */
14914 else if (h != NULL
14915 && h->elf.root.type == bfd_link_hash_undefweak
14916 && h->elf.dynindx == -1
14917 && r_type == R_PPC64_REL24
14918 && relocation == 0
14919 && addend == 0)
14920 {
14921 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14922 goto copy_reloc;
14923 }
14924 break;
14925 }
14926
14927 /* Set `addend'. */
14928 tls_type = 0;
14929 save_unresolved_reloc = unresolved_reloc;
14930 switch (r_type)
14931 {
14932 default:
14933 /* xgettext:c-format */
14934 _bfd_error_handler (_("%pB: %s unsupported"),
14935 input_bfd, ppc64_elf_howto_table[r_type]->name);
14936
14937 bfd_set_error (bfd_error_bad_value);
14938 ret = FALSE;
14939 goto copy_reloc;
14940
14941 case R_PPC64_NONE:
14942 case R_PPC64_TLS:
14943 case R_PPC64_TLSGD:
14944 case R_PPC64_TLSLD:
14945 case R_PPC64_TOCSAVE:
14946 case R_PPC64_GNU_VTINHERIT:
14947 case R_PPC64_GNU_VTENTRY:
14948 case R_PPC64_ENTRY:
14949 goto copy_reloc;
14950
14951 /* GOT16 relocations. Like an ADDR16 using the symbol's
14952 address in the GOT as relocation value instead of the
14953 symbol's value itself. Also, create a GOT entry for the
14954 symbol and put the symbol value there. */
14955 case R_PPC64_GOT_TLSGD16:
14956 case R_PPC64_GOT_TLSGD16_LO:
14957 case R_PPC64_GOT_TLSGD16_HI:
14958 case R_PPC64_GOT_TLSGD16_HA:
14959 tls_type = TLS_TLS | TLS_GD;
14960 goto dogot;
14961
14962 case R_PPC64_GOT_TLSLD16:
14963 case R_PPC64_GOT_TLSLD16_LO:
14964 case R_PPC64_GOT_TLSLD16_HI:
14965 case R_PPC64_GOT_TLSLD16_HA:
14966 tls_type = TLS_TLS | TLS_LD;
14967 goto dogot;
14968
14969 case R_PPC64_GOT_TPREL16_DS:
14970 case R_PPC64_GOT_TPREL16_LO_DS:
14971 case R_PPC64_GOT_TPREL16_HI:
14972 case R_PPC64_GOT_TPREL16_HA:
14973 tls_type = TLS_TLS | TLS_TPREL;
14974 goto dogot;
14975
14976 case R_PPC64_GOT_DTPREL16_DS:
14977 case R_PPC64_GOT_DTPREL16_LO_DS:
14978 case R_PPC64_GOT_DTPREL16_HI:
14979 case R_PPC64_GOT_DTPREL16_HA:
14980 tls_type = TLS_TLS | TLS_DTPREL;
14981 goto dogot;
14982
14983 case R_PPC64_GOT16:
14984 case R_PPC64_GOT16_LO:
14985 case R_PPC64_GOT16_HI:
14986 case R_PPC64_GOT16_HA:
14987 case R_PPC64_GOT16_DS:
14988 case R_PPC64_GOT16_LO_DS:
14989 dogot:
14990 {
14991 /* Relocation is to the entry for this symbol in the global
14992 offset table. */
14993 asection *got;
14994 bfd_vma *offp;
14995 bfd_vma off;
14996 unsigned long indx = 0;
14997 struct got_entry *ent;
14998
14999 if (tls_type == (TLS_TLS | TLS_LD)
15000 && (h == NULL
15001 || !h->elf.def_dynamic))
15002 ent = ppc64_tlsld_got (input_bfd);
15003 else
15004 {
15005 if (h != NULL)
15006 {
15007 if (!htab->elf.dynamic_sections_created
15008 || h->elf.dynindx == -1
15009 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15010 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15011 /* This is actually a static link, or it is a
15012 -Bsymbolic link and the symbol is defined
15013 locally, or the symbol was forced to be local
15014 because of a version file. */
15015 ;
15016 else
15017 {
15018 indx = h->elf.dynindx;
15019 unresolved_reloc = FALSE;
15020 }
15021 ent = h->elf.got.glist;
15022 }
15023 else
15024 {
15025 if (local_got_ents == NULL)
15026 abort ();
15027 ent = local_got_ents[r_symndx];
15028 }
15029
15030 for (; ent != NULL; ent = ent->next)
15031 if (ent->addend == orig_rel.r_addend
15032 && ent->owner == input_bfd
15033 && ent->tls_type == tls_type)
15034 break;
15035 }
15036
15037 if (ent == NULL)
15038 abort ();
15039 if (ent->is_indirect)
15040 ent = ent->got.ent;
15041 offp = &ent->got.offset;
15042 got = ppc64_elf_tdata (ent->owner)->got;
15043 if (got == NULL)
15044 abort ();
15045
15046 /* The offset must always be a multiple of 8. We use the
15047 least significant bit to record whether we have already
15048 processed this entry. */
15049 off = *offp;
15050 if ((off & 1) != 0)
15051 off &= ~1;
15052 else
15053 {
15054 /* Generate relocs for the dynamic linker, except in
15055 the case of TLSLD where we'll use one entry per
15056 module. */
15057 asection *relgot;
15058 bfd_boolean ifunc;
15059
15060 *offp = off | 1;
15061 relgot = NULL;
15062 ifunc = (h != NULL
15063 ? h->elf.type == STT_GNU_IFUNC
15064 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15065 if (ifunc)
15066 {
15067 relgot = htab->elf.irelplt;
15068 if (indx == 0)
15069 htab->local_ifunc_resolver = 1;
15070 else if (is_static_defined (&h->elf))
15071 htab->maybe_local_ifunc_resolver = 1;
15072 }
15073 else if (indx != 0
15074 || (bfd_link_pic (info)
15075 && (h == NULL
15076 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15077 || (tls_type == (TLS_TLS | TLS_LD)
15078 && !h->elf.def_dynamic))
15079 && !(tls_type == (TLS_TLS | TLS_TPREL)
15080 && bfd_link_executable (info)
15081 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15082 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15083 if (relgot != NULL)
15084 {
15085 outrel.r_offset = (got->output_section->vma
15086 + got->output_offset
15087 + off);
15088 outrel.r_addend = addend;
15089 if (tls_type & (TLS_LD | TLS_GD))
15090 {
15091 outrel.r_addend = 0;
15092 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15093 if (tls_type == (TLS_TLS | TLS_GD))
15094 {
15095 loc = relgot->contents;
15096 loc += (relgot->reloc_count++
15097 * sizeof (Elf64_External_Rela));
15098 bfd_elf64_swap_reloca_out (output_bfd,
15099 &outrel, loc);
15100 outrel.r_offset += 8;
15101 outrel.r_addend = addend;
15102 outrel.r_info
15103 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15104 }
15105 }
15106 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15107 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15108 else if (tls_type == (TLS_TLS | TLS_TPREL))
15109 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15110 else if (indx != 0)
15111 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15112 else
15113 {
15114 if (ifunc)
15115 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15116 else
15117 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15118
15119 /* Write the .got section contents for the sake
15120 of prelink. */
15121 loc = got->contents + off;
15122 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15123 loc);
15124 }
15125
15126 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15127 {
15128 outrel.r_addend += relocation;
15129 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15130 {
15131 if (htab->elf.tls_sec == NULL)
15132 outrel.r_addend = 0;
15133 else
15134 outrel.r_addend -= htab->elf.tls_sec->vma;
15135 }
15136 }
15137 loc = relgot->contents;
15138 loc += (relgot->reloc_count++
15139 * sizeof (Elf64_External_Rela));
15140 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15141 }
15142
15143 /* Init the .got section contents here if we're not
15144 emitting a reloc. */
15145 else
15146 {
15147 relocation += addend;
15148 if (tls_type != 0)
15149 {
15150 if (htab->elf.tls_sec == NULL)
15151 relocation = 0;
15152 else
15153 {
15154 if (tls_type & TLS_LD)
15155 relocation = 0;
15156 else
15157 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15158 if (tls_type & TLS_TPREL)
15159 relocation += DTP_OFFSET - TP_OFFSET;
15160 }
15161
15162 if (tls_type & (TLS_GD | TLS_LD))
15163 {
15164 bfd_put_64 (output_bfd, relocation,
15165 got->contents + off + 8);
15166 relocation = 1;
15167 }
15168 }
15169 bfd_put_64 (output_bfd, relocation,
15170 got->contents + off);
15171 }
15172 }
15173
15174 if (off >= (bfd_vma) -2)
15175 abort ();
15176
15177 relocation = got->output_section->vma + got->output_offset + off;
15178 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15179 }
15180 break;
15181
15182 case R_PPC64_PLT16_HA:
15183 case R_PPC64_PLT16_HI:
15184 case R_PPC64_PLT16_LO:
15185 case R_PPC64_PLT16_LO_DS:
15186 case R_PPC64_PLT32:
15187 case R_PPC64_PLT64:
15188 case R_PPC64_PLTSEQ:
15189 case R_PPC64_PLTCALL:
15190 /* Relocation is to the entry for this symbol in the
15191 procedure linkage table. */
15192 unresolved_reloc = TRUE;
15193 {
15194 struct plt_entry **plt_list = NULL;
15195 if (h != NULL)
15196 plt_list = &h->elf.plt.plist;
15197 else if (local_got_ents != NULL)
15198 {
15199 struct plt_entry **local_plt = (struct plt_entry **)
15200 (local_got_ents + symtab_hdr->sh_info);
15201 plt_list = local_plt + r_symndx;
15202 }
15203 if (plt_list)
15204 {
15205 struct plt_entry *ent;
15206
15207 for (ent = *plt_list; ent != NULL; ent = ent->next)
15208 if (ent->plt.offset != (bfd_vma) -1
15209 && ent->addend == orig_rel.r_addend)
15210 {
15211 asection *plt;
15212 bfd_vma got;
15213
15214 plt = htab->elf.splt;
15215 if (!htab->elf.dynamic_sections_created
15216 || h == NULL
15217 || h->elf.dynindx == -1)
15218 {
15219 if (h != NULL
15220 ? h->elf.type == STT_GNU_IFUNC
15221 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15222 plt = htab->elf.iplt;
15223 else
15224 plt = htab->pltlocal;
15225 }
15226 relocation = (plt->output_section->vma
15227 + plt->output_offset
15228 + ent->plt.offset);
15229 if (r_type == R_PPC64_PLT16_HA
15230 || r_type ==R_PPC64_PLT16_HI
15231 || r_type ==R_PPC64_PLT16_LO
15232 || r_type ==R_PPC64_PLT16_LO_DS)
15233 {
15234 got = (elf_gp (output_bfd)
15235 + htab->sec_info[input_section->id].toc_off);
15236 relocation -= got;
15237 }
15238 addend = 0;
15239 unresolved_reloc = FALSE;
15240 break;
15241 }
15242 }
15243 }
15244 break;
15245
15246 case R_PPC64_TOC:
15247 /* Relocation value is TOC base. */
15248 relocation = TOCstart;
15249 if (r_symndx == STN_UNDEF)
15250 relocation += htab->sec_info[input_section->id].toc_off;
15251 else if (unresolved_reloc)
15252 ;
15253 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15254 relocation += htab->sec_info[sec->id].toc_off;
15255 else
15256 unresolved_reloc = TRUE;
15257 goto dodyn;
15258
15259 /* TOC16 relocs. We want the offset relative to the TOC base,
15260 which is the address of the start of the TOC plus 0x8000.
15261 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15262 in this order. */
15263 case R_PPC64_TOC16:
15264 case R_PPC64_TOC16_LO:
15265 case R_PPC64_TOC16_HI:
15266 case R_PPC64_TOC16_DS:
15267 case R_PPC64_TOC16_LO_DS:
15268 case R_PPC64_TOC16_HA:
15269 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15270 break;
15271
15272 /* Relocate against the beginning of the section. */
15273 case R_PPC64_SECTOFF:
15274 case R_PPC64_SECTOFF_LO:
15275 case R_PPC64_SECTOFF_HI:
15276 case R_PPC64_SECTOFF_DS:
15277 case R_PPC64_SECTOFF_LO_DS:
15278 case R_PPC64_SECTOFF_HA:
15279 if (sec != NULL)
15280 addend -= sec->output_section->vma;
15281 break;
15282
15283 case R_PPC64_REL16:
15284 case R_PPC64_REL16_LO:
15285 case R_PPC64_REL16_HI:
15286 case R_PPC64_REL16_HA:
15287 case R_PPC64_REL16DX_HA:
15288 break;
15289
15290 case R_PPC64_REL14:
15291 case R_PPC64_REL14_BRNTAKEN:
15292 case R_PPC64_REL14_BRTAKEN:
15293 case R_PPC64_REL24:
15294 break;
15295
15296 case R_PPC64_TPREL16:
15297 case R_PPC64_TPREL16_LO:
15298 case R_PPC64_TPREL16_HI:
15299 case R_PPC64_TPREL16_HA:
15300 case R_PPC64_TPREL16_DS:
15301 case R_PPC64_TPREL16_LO_DS:
15302 case R_PPC64_TPREL16_HIGH:
15303 case R_PPC64_TPREL16_HIGHA:
15304 case R_PPC64_TPREL16_HIGHER:
15305 case R_PPC64_TPREL16_HIGHERA:
15306 case R_PPC64_TPREL16_HIGHEST:
15307 case R_PPC64_TPREL16_HIGHESTA:
15308 if (h != NULL
15309 && h->elf.root.type == bfd_link_hash_undefweak
15310 && h->elf.dynindx == -1)
15311 {
15312 /* Make this relocation against an undefined weak symbol
15313 resolve to zero. This is really just a tweak, since
15314 code using weak externs ought to check that they are
15315 defined before using them. */
15316 bfd_byte *p = contents + rel->r_offset - d_offset;
15317
15318 insn = bfd_get_32 (input_bfd, p);
15319 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15320 if (insn != 0)
15321 bfd_put_32 (input_bfd, insn, p);
15322 break;
15323 }
15324 if (htab->elf.tls_sec != NULL)
15325 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15326 /* The TPREL16 relocs shouldn't really be used in shared
15327 libs or with non-local symbols as that will result in
15328 DT_TEXTREL being set, but support them anyway. */
15329 goto dodyn;
15330
15331 case R_PPC64_DTPREL16:
15332 case R_PPC64_DTPREL16_LO:
15333 case R_PPC64_DTPREL16_HI:
15334 case R_PPC64_DTPREL16_HA:
15335 case R_PPC64_DTPREL16_DS:
15336 case R_PPC64_DTPREL16_LO_DS:
15337 case R_PPC64_DTPREL16_HIGH:
15338 case R_PPC64_DTPREL16_HIGHA:
15339 case R_PPC64_DTPREL16_HIGHER:
15340 case R_PPC64_DTPREL16_HIGHERA:
15341 case R_PPC64_DTPREL16_HIGHEST:
15342 case R_PPC64_DTPREL16_HIGHESTA:
15343 if (htab->elf.tls_sec != NULL)
15344 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15345 break;
15346
15347 case R_PPC64_ADDR64_LOCAL:
15348 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15349 ? h->elf.other
15350 : sym->st_other);
15351 break;
15352
15353 case R_PPC64_DTPMOD64:
15354 relocation = 1;
15355 addend = 0;
15356 goto dodyn;
15357
15358 case R_PPC64_TPREL64:
15359 if (htab->elf.tls_sec != NULL)
15360 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15361 goto dodyn;
15362
15363 case R_PPC64_DTPREL64:
15364 if (htab->elf.tls_sec != NULL)
15365 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15366 /* Fall through. */
15367
15368 /* Relocations that may need to be propagated if this is a
15369 dynamic object. */
15370 case R_PPC64_REL30:
15371 case R_PPC64_REL32:
15372 case R_PPC64_REL64:
15373 case R_PPC64_ADDR14:
15374 case R_PPC64_ADDR14_BRNTAKEN:
15375 case R_PPC64_ADDR14_BRTAKEN:
15376 case R_PPC64_ADDR16:
15377 case R_PPC64_ADDR16_DS:
15378 case R_PPC64_ADDR16_HA:
15379 case R_PPC64_ADDR16_HI:
15380 case R_PPC64_ADDR16_HIGH:
15381 case R_PPC64_ADDR16_HIGHA:
15382 case R_PPC64_ADDR16_HIGHER:
15383 case R_PPC64_ADDR16_HIGHERA:
15384 case R_PPC64_ADDR16_HIGHEST:
15385 case R_PPC64_ADDR16_HIGHESTA:
15386 case R_PPC64_ADDR16_LO:
15387 case R_PPC64_ADDR16_LO_DS:
15388 case R_PPC64_ADDR24:
15389 case R_PPC64_ADDR32:
15390 case R_PPC64_ADDR64:
15391 case R_PPC64_UADDR16:
15392 case R_PPC64_UADDR32:
15393 case R_PPC64_UADDR64:
15394 dodyn:
15395 if ((input_section->flags & SEC_ALLOC) == 0)
15396 break;
15397
15398 if (NO_OPD_RELOCS && is_opd)
15399 break;
15400
15401 if (bfd_link_pic (info)
15402 ? ((h == NULL
15403 || h->dyn_relocs != NULL)
15404 && ((h != NULL && pc_dynrelocs (h))
15405 || must_be_dyn_reloc (info, r_type)))
15406 : (h != NULL
15407 ? h->dyn_relocs != NULL
15408 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15409 {
15410 bfd_boolean skip, relocate;
15411 asection *sreloc;
15412 bfd_vma out_off;
15413 long indx = 0;
15414
15415 /* When generating a dynamic object, these relocations
15416 are copied into the output file to be resolved at run
15417 time. */
15418
15419 skip = FALSE;
15420 relocate = FALSE;
15421
15422 out_off = _bfd_elf_section_offset (output_bfd, info,
15423 input_section, rel->r_offset);
15424 if (out_off == (bfd_vma) -1)
15425 skip = TRUE;
15426 else if (out_off == (bfd_vma) -2)
15427 skip = TRUE, relocate = TRUE;
15428 out_off += (input_section->output_section->vma
15429 + input_section->output_offset);
15430 outrel.r_offset = out_off;
15431 outrel.r_addend = rel->r_addend;
15432
15433 /* Optimize unaligned reloc use. */
15434 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15435 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15436 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15437 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15438 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15439 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15440 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15441 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15442 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15443
15444 if (skip)
15445 memset (&outrel, 0, sizeof outrel);
15446 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15447 && !is_opd
15448 && r_type != R_PPC64_TOC)
15449 {
15450 indx = h->elf.dynindx;
15451 BFD_ASSERT (indx != -1);
15452 outrel.r_info = ELF64_R_INFO (indx, r_type);
15453 }
15454 else
15455 {
15456 /* This symbol is local, or marked to become local,
15457 or this is an opd section reloc which must point
15458 at a local function. */
15459 outrel.r_addend += relocation;
15460 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15461 {
15462 if (is_opd && h != NULL)
15463 {
15464 /* Lie about opd entries. This case occurs
15465 when building shared libraries and we
15466 reference a function in another shared
15467 lib. The same thing happens for a weak
15468 definition in an application that's
15469 overridden by a strong definition in a
15470 shared lib. (I believe this is a generic
15471 bug in binutils handling of weak syms.)
15472 In these cases we won't use the opd
15473 entry in this lib. */
15474 unresolved_reloc = FALSE;
15475 }
15476 if (!is_opd
15477 && r_type == R_PPC64_ADDR64
15478 && (h != NULL
15479 ? h->elf.type == STT_GNU_IFUNC
15480 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15481 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15482 else
15483 {
15484 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15485
15486 /* We need to relocate .opd contents for ld.so.
15487 Prelink also wants simple and consistent rules
15488 for relocs. This make all RELATIVE relocs have
15489 *r_offset equal to r_addend. */
15490 relocate = TRUE;
15491 }
15492 }
15493 else
15494 {
15495 if (h != NULL
15496 ? h->elf.type == STT_GNU_IFUNC
15497 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15498 {
15499 info->callbacks->einfo
15500 /* xgettext:c-format */
15501 (_("%H: %s for indirect "
15502 "function `%pT' unsupported\n"),
15503 input_bfd, input_section, rel->r_offset,
15504 ppc64_elf_howto_table[r_type]->name,
15505 sym_name);
15506 ret = FALSE;
15507 }
15508 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15509 ;
15510 else if (sec == NULL || sec->owner == NULL)
15511 {
15512 bfd_set_error (bfd_error_bad_value);
15513 return FALSE;
15514 }
15515 else
15516 {
15517 asection *osec;
15518
15519 osec = sec->output_section;
15520 indx = elf_section_data (osec)->dynindx;
15521
15522 if (indx == 0)
15523 {
15524 if ((osec->flags & SEC_READONLY) == 0
15525 && htab->elf.data_index_section != NULL)
15526 osec = htab->elf.data_index_section;
15527 else
15528 osec = htab->elf.text_index_section;
15529 indx = elf_section_data (osec)->dynindx;
15530 }
15531 BFD_ASSERT (indx != 0);
15532
15533 /* We are turning this relocation into one
15534 against a section symbol, so subtract out
15535 the output section's address but not the
15536 offset of the input section in the output
15537 section. */
15538 outrel.r_addend -= osec->vma;
15539 }
15540
15541 outrel.r_info = ELF64_R_INFO (indx, r_type);
15542 }
15543 }
15544
15545 sreloc = elf_section_data (input_section)->sreloc;
15546 if (h != NULL
15547 ? h->elf.type == STT_GNU_IFUNC
15548 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15549 {
15550 sreloc = htab->elf.irelplt;
15551 if (indx == 0)
15552 htab->local_ifunc_resolver = 1;
15553 else if (is_static_defined (&h->elf))
15554 htab->maybe_local_ifunc_resolver = 1;
15555 }
15556 if (sreloc == NULL)
15557 abort ();
15558
15559 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15560 >= sreloc->size)
15561 abort ();
15562 loc = sreloc->contents;
15563 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15564 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15565
15566 /* If this reloc is against an external symbol, it will
15567 be computed at runtime, so there's no need to do
15568 anything now. However, for the sake of prelink ensure
15569 that the section contents are a known value. */
15570 if (! relocate)
15571 {
15572 unresolved_reloc = FALSE;
15573 /* The value chosen here is quite arbitrary as ld.so
15574 ignores section contents except for the special
15575 case of .opd where the contents might be accessed
15576 before relocation. Choose zero, as that won't
15577 cause reloc overflow. */
15578 relocation = 0;
15579 addend = 0;
15580 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15581 to improve backward compatibility with older
15582 versions of ld. */
15583 if (r_type == R_PPC64_ADDR64)
15584 addend = outrel.r_addend;
15585 /* Adjust pc_relative relocs to have zero in *r_offset. */
15586 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15587 addend = outrel.r_offset;
15588 }
15589 }
15590 break;
15591
15592 case R_PPC64_COPY:
15593 case R_PPC64_GLOB_DAT:
15594 case R_PPC64_JMP_SLOT:
15595 case R_PPC64_JMP_IREL:
15596 case R_PPC64_RELATIVE:
15597 /* We shouldn't ever see these dynamic relocs in relocatable
15598 files. */
15599 /* Fall through. */
15600
15601 case R_PPC64_PLTGOT16:
15602 case R_PPC64_PLTGOT16_DS:
15603 case R_PPC64_PLTGOT16_HA:
15604 case R_PPC64_PLTGOT16_HI:
15605 case R_PPC64_PLTGOT16_LO:
15606 case R_PPC64_PLTGOT16_LO_DS:
15607 case R_PPC64_PLTREL32:
15608 case R_PPC64_PLTREL64:
15609 /* These ones haven't been implemented yet. */
15610
15611 info->callbacks->einfo
15612 /* xgettext:c-format */
15613 (_("%P: %pB: %s is not supported for `%pT'\n"),
15614 input_bfd,
15615 ppc64_elf_howto_table[r_type]->name, sym_name);
15616
15617 bfd_set_error (bfd_error_invalid_operation);
15618 ret = FALSE;
15619 goto copy_reloc;
15620 }
15621
15622 /* Multi-instruction sequences that access the TOC can be
15623 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15624 to nop; addi rb,r2,x; */
15625 switch (r_type)
15626 {
15627 default:
15628 break;
15629
15630 case R_PPC64_GOT_TLSLD16_HI:
15631 case R_PPC64_GOT_TLSGD16_HI:
15632 case R_PPC64_GOT_TPREL16_HI:
15633 case R_PPC64_GOT_DTPREL16_HI:
15634 case R_PPC64_GOT16_HI:
15635 case R_PPC64_TOC16_HI:
15636 /* These relocs would only be useful if building up an
15637 offset to later add to r2, perhaps in an indexed
15638 addressing mode instruction. Don't try to optimize.
15639 Unfortunately, the possibility of someone building up an
15640 offset like this or even with the HA relocs, means that
15641 we need to check the high insn when optimizing the low
15642 insn. */
15643 break;
15644
15645 case R_PPC64_PLTCALL:
15646 if (unresolved_reloc)
15647 {
15648 /* No plt entry. Make this into a direct call. */
15649 bfd_byte *p = contents + rel->r_offset;
15650 insn = bfd_get_32 (input_bfd, p);
15651 insn &= 1;
15652 bfd_put_32 (input_bfd, B_DOT | insn, p);
15653 bfd_put_32 (input_bfd, NOP, p + 4);
15654 unresolved_reloc = save_unresolved_reloc;
15655 r_type = R_PPC64_REL24;
15656 }
15657 break;
15658
15659 case R_PPC64_PLTSEQ:
15660 if (unresolved_reloc)
15661 {
15662 unresolved_reloc = FALSE;
15663 goto nop_it;
15664 }
15665 break;
15666
15667 case R_PPC64_PLT16_HA:
15668 if (unresolved_reloc)
15669 {
15670 unresolved_reloc = FALSE;
15671 goto nop_it;
15672 }
15673 /* Fall through. */
15674 case R_PPC64_GOT_TLSLD16_HA:
15675 case R_PPC64_GOT_TLSGD16_HA:
15676 case R_PPC64_GOT_TPREL16_HA:
15677 case R_PPC64_GOT_DTPREL16_HA:
15678 case R_PPC64_GOT16_HA:
15679 case R_PPC64_TOC16_HA:
15680 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15681 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15682 {
15683 bfd_byte *p;
15684 nop_it:
15685 p = contents + (rel->r_offset & ~3);
15686 bfd_put_32 (input_bfd, NOP, p);
15687 goto copy_reloc;
15688 }
15689 break;
15690
15691 case R_PPC64_PLT16_LO:
15692 case R_PPC64_PLT16_LO_DS:
15693 if (unresolved_reloc)
15694 {
15695 unresolved_reloc = FALSE;
15696 goto nop_it;
15697 }
15698 /* Fall through. */
15699 case R_PPC64_GOT_TLSLD16_LO:
15700 case R_PPC64_GOT_TLSGD16_LO:
15701 case R_PPC64_GOT_TPREL16_LO_DS:
15702 case R_PPC64_GOT_DTPREL16_LO_DS:
15703 case R_PPC64_GOT16_LO:
15704 case R_PPC64_GOT16_LO_DS:
15705 case R_PPC64_TOC16_LO:
15706 case R_PPC64_TOC16_LO_DS:
15707 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15708 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15709 {
15710 bfd_byte *p = contents + (rel->r_offset & ~3);
15711 insn = bfd_get_32 (input_bfd, p);
15712 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15713 {
15714 /* Transform addic to addi when we change reg. */
15715 insn &= ~((0x3f << 26) | (0x1f << 16));
15716 insn |= (14u << 26) | (2 << 16);
15717 }
15718 else
15719 {
15720 insn &= ~(0x1f << 16);
15721 insn |= 2 << 16;
15722 }
15723 bfd_put_32 (input_bfd, insn, p);
15724 }
15725 break;
15726
15727 case R_PPC64_TPREL16_HA:
15728 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15729 {
15730 bfd_byte *p = contents + (rel->r_offset & ~3);
15731 insn = bfd_get_32 (input_bfd, p);
15732 if ((insn & ((0x3f << 26) | 0x1f << 16))
15733 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15734 /* xgettext:c-format */
15735 info->callbacks->minfo
15736 (_("%H: warning: %s unexpected insn %#x.\n"),
15737 input_bfd, input_section, rel->r_offset,
15738 ppc64_elf_howto_table[r_type]->name, insn);
15739 else
15740 {
15741 bfd_put_32 (input_bfd, NOP, p);
15742 goto copy_reloc;
15743 }
15744 }
15745 break;
15746
15747 case R_PPC64_TPREL16_LO:
15748 case R_PPC64_TPREL16_LO_DS:
15749 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15750 {
15751 bfd_byte *p = contents + (rel->r_offset & ~3);
15752 insn = bfd_get_32 (input_bfd, p);
15753 insn &= ~(0x1f << 16);
15754 insn |= 13 << 16;
15755 bfd_put_32 (input_bfd, insn, p);
15756 }
15757 break;
15758 }
15759
15760 /* Do any further special processing. */
15761 switch (r_type)
15762 {
15763 default:
15764 break;
15765
15766 case R_PPC64_REL16_HA:
15767 case R_PPC64_REL16DX_HA:
15768 case R_PPC64_ADDR16_HA:
15769 case R_PPC64_ADDR16_HIGHA:
15770 case R_PPC64_ADDR16_HIGHERA:
15771 case R_PPC64_ADDR16_HIGHESTA:
15772 case R_PPC64_TOC16_HA:
15773 case R_PPC64_SECTOFF_HA:
15774 case R_PPC64_TPREL16_HA:
15775 case R_PPC64_TPREL16_HIGHA:
15776 case R_PPC64_TPREL16_HIGHERA:
15777 case R_PPC64_TPREL16_HIGHESTA:
15778 case R_PPC64_DTPREL16_HA:
15779 case R_PPC64_DTPREL16_HIGHA:
15780 case R_PPC64_DTPREL16_HIGHERA:
15781 case R_PPC64_DTPREL16_HIGHESTA:
15782 /* It's just possible that this symbol is a weak symbol
15783 that's not actually defined anywhere. In that case,
15784 'sec' would be NULL, and we should leave the symbol
15785 alone (it will be set to zero elsewhere in the link). */
15786 if (sec == NULL)
15787 break;
15788 /* Fall through. */
15789
15790 case R_PPC64_GOT16_HA:
15791 case R_PPC64_PLTGOT16_HA:
15792 case R_PPC64_PLT16_HA:
15793 case R_PPC64_GOT_TLSGD16_HA:
15794 case R_PPC64_GOT_TLSLD16_HA:
15795 case R_PPC64_GOT_TPREL16_HA:
15796 case R_PPC64_GOT_DTPREL16_HA:
15797 /* Add 0x10000 if sign bit in 0:15 is set.
15798 Bits 0:15 are not used. */
15799 addend += 0x8000;
15800 break;
15801
15802 case R_PPC64_ADDR16_DS:
15803 case R_PPC64_ADDR16_LO_DS:
15804 case R_PPC64_GOT16_DS:
15805 case R_PPC64_GOT16_LO_DS:
15806 case R_PPC64_PLT16_LO_DS:
15807 case R_PPC64_SECTOFF_DS:
15808 case R_PPC64_SECTOFF_LO_DS:
15809 case R_PPC64_TOC16_DS:
15810 case R_PPC64_TOC16_LO_DS:
15811 case R_PPC64_PLTGOT16_DS:
15812 case R_PPC64_PLTGOT16_LO_DS:
15813 case R_PPC64_GOT_TPREL16_DS:
15814 case R_PPC64_GOT_TPREL16_LO_DS:
15815 case R_PPC64_GOT_DTPREL16_DS:
15816 case R_PPC64_GOT_DTPREL16_LO_DS:
15817 case R_PPC64_TPREL16_DS:
15818 case R_PPC64_TPREL16_LO_DS:
15819 case R_PPC64_DTPREL16_DS:
15820 case R_PPC64_DTPREL16_LO_DS:
15821 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15822 mask = 3;
15823 /* If this reloc is against an lq, lxv, or stxv insn, then
15824 the value must be a multiple of 16. This is somewhat of
15825 a hack, but the "correct" way to do this by defining _DQ
15826 forms of all the _DS relocs bloats all reloc switches in
15827 this file. It doesn't make much sense to use these
15828 relocs in data, so testing the insn should be safe. */
15829 if ((insn & (0x3f << 26)) == (56u << 26)
15830 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15831 mask = 15;
15832 relocation += addend;
15833 addend = insn & (mask ^ 3);
15834 if ((relocation & mask) != 0)
15835 {
15836 relocation ^= relocation & mask;
15837 info->callbacks->einfo
15838 /* xgettext:c-format */
15839 (_("%H: error: %s not a multiple of %u\n"),
15840 input_bfd, input_section, rel->r_offset,
15841 ppc64_elf_howto_table[r_type]->name,
15842 mask + 1);
15843 bfd_set_error (bfd_error_bad_value);
15844 ret = FALSE;
15845 goto copy_reloc;
15846 }
15847 break;
15848 }
15849
15850 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15851 because such sections are not SEC_ALLOC and thus ld.so will
15852 not process them. */
15853 howto = ppc64_elf_howto_table[(int) r_type];
15854 if (unresolved_reloc
15855 && !((input_section->flags & SEC_DEBUGGING) != 0
15856 && h->elf.def_dynamic)
15857 && _bfd_elf_section_offset (output_bfd, info, input_section,
15858 rel->r_offset) != (bfd_vma) -1)
15859 {
15860 info->callbacks->einfo
15861 /* xgettext:c-format */
15862 (_("%H: unresolvable %s against `%pT'\n"),
15863 input_bfd, input_section, rel->r_offset,
15864 howto->name,
15865 h->elf.root.root.string);
15866 ret = FALSE;
15867 }
15868
15869 /* 16-bit fields in insns mostly have signed values, but a
15870 few insns have 16-bit unsigned values. Really, we should
15871 have different reloc types. */
15872 if (howto->complain_on_overflow != complain_overflow_dont
15873 && howto->dst_mask == 0xffff
15874 && (input_section->flags & SEC_CODE) != 0)
15875 {
15876 enum complain_overflow complain = complain_overflow_signed;
15877
15878 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15879 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15880 complain = complain_overflow_bitfield;
15881 else if (howto->rightshift == 0
15882 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15883 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15884 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15885 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15886 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15887 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15888 complain = complain_overflow_unsigned;
15889 if (howto->complain_on_overflow != complain)
15890 {
15891 alt_howto = *howto;
15892 alt_howto.complain_on_overflow = complain;
15893 howto = &alt_howto;
15894 }
15895 }
15896
15897 if (r_type == R_PPC64_REL16DX_HA)
15898 {
15899 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15900 if (rel->r_offset + 4 > input_section->size)
15901 r = bfd_reloc_outofrange;
15902 else
15903 {
15904 relocation += addend;
15905 relocation -= (rel->r_offset
15906 + input_section->output_offset
15907 + input_section->output_section->vma);
15908 relocation = (bfd_signed_vma) relocation >> 16;
15909 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15910 insn &= ~0x1fffc1;
15911 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15912 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15913 r = bfd_reloc_ok;
15914 if (relocation + 0x8000 > 0xffff)
15915 r = bfd_reloc_overflow;
15916 }
15917 }
15918 else
15919 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15920 rel->r_offset, relocation, addend);
15921
15922 if (r != bfd_reloc_ok)
15923 {
15924 char *more_info = NULL;
15925 const char *reloc_name = howto->name;
15926
15927 if (reloc_dest != DEST_NORMAL)
15928 {
15929 more_info = bfd_malloc (strlen (reloc_name) + 8);
15930 if (more_info != NULL)
15931 {
15932 strcpy (more_info, reloc_name);
15933 strcat (more_info, (reloc_dest == DEST_OPD
15934 ? " (OPD)" : " (stub)"));
15935 reloc_name = more_info;
15936 }
15937 }
15938
15939 if (r == bfd_reloc_overflow)
15940 {
15941 /* On code like "if (foo) foo();" don't report overflow
15942 on a branch to zero when foo is undefined. */
15943 if (!warned
15944 && (reloc_dest == DEST_STUB
15945 || !(h != NULL
15946 && (h->elf.root.type == bfd_link_hash_undefweak
15947 || h->elf.root.type == bfd_link_hash_undefined)
15948 && is_branch_reloc (r_type))))
15949 info->callbacks->reloc_overflow (info, &h->elf.root,
15950 sym_name, reloc_name,
15951 orig_rel.r_addend,
15952 input_bfd, input_section,
15953 rel->r_offset);
15954 }
15955 else
15956 {
15957 info->callbacks->einfo
15958 /* xgettext:c-format */
15959 (_("%H: %s against `%pT': error %d\n"),
15960 input_bfd, input_section, rel->r_offset,
15961 reloc_name, sym_name, (int) r);
15962 ret = FALSE;
15963 }
15964 if (more_info != NULL)
15965 free (more_info);
15966 }
15967 copy_reloc:
15968 if (wrel != rel)
15969 *wrel = *rel;
15970 }
15971
15972 if (wrel != rel)
15973 {
15974 Elf_Internal_Shdr *rel_hdr;
15975 size_t deleted = rel - wrel;
15976
15977 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15978 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15979 if (rel_hdr->sh_size == 0)
15980 {
15981 /* It is too late to remove an empty reloc section. Leave
15982 one NONE reloc.
15983 ??? What is wrong with an empty section??? */
15984 rel_hdr->sh_size = rel_hdr->sh_entsize;
15985 deleted -= 1;
15986 }
15987 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15988 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15989 input_section->reloc_count -= deleted;
15990 }
15991
15992 /* If we're emitting relocations, then shortly after this function
15993 returns, reloc offsets and addends for this section will be
15994 adjusted. Worse, reloc symbol indices will be for the output
15995 file rather than the input. Save a copy of the relocs for
15996 opd_entry_value. */
15997 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15998 {
15999 bfd_size_type amt;
16000 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16001 rel = bfd_alloc (input_bfd, amt);
16002 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16003 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16004 if (rel == NULL)
16005 return FALSE;
16006 memcpy (rel, relocs, amt);
16007 }
16008 return ret;
16009 }
16010
16011 /* Adjust the value of any local symbols in opd sections. */
16012
16013 static int
16014 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16015 const char *name ATTRIBUTE_UNUSED,
16016 Elf_Internal_Sym *elfsym,
16017 asection *input_sec,
16018 struct elf_link_hash_entry *h)
16019 {
16020 struct _opd_sec_data *opd;
16021 long adjust;
16022 bfd_vma value;
16023
16024 if (h != NULL)
16025 return 1;
16026
16027 opd = get_opd_info (input_sec);
16028 if (opd == NULL || opd->adjust == NULL)
16029 return 1;
16030
16031 value = elfsym->st_value - input_sec->output_offset;
16032 if (!bfd_link_relocatable (info))
16033 value -= input_sec->output_section->vma;
16034
16035 adjust = opd->adjust[OPD_NDX (value)];
16036 if (adjust == -1)
16037 return 2;
16038
16039 elfsym->st_value += adjust;
16040 return 1;
16041 }
16042
16043 /* Finish up dynamic symbol handling. We set the contents of various
16044 dynamic sections here. */
16045
16046 static bfd_boolean
16047 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16048 struct bfd_link_info *info,
16049 struct elf_link_hash_entry *h,
16050 Elf_Internal_Sym *sym)
16051 {
16052 struct ppc_link_hash_table *htab;
16053 struct plt_entry *ent;
16054
16055 htab = ppc_hash_table (info);
16056 if (htab == NULL)
16057 return FALSE;
16058
16059 if (!htab->opd_abi && !h->def_regular)
16060 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16061 if (ent->plt.offset != (bfd_vma) -1)
16062 {
16063 /* Mark the symbol as undefined, rather than as
16064 defined in glink. Leave the value if there were
16065 any relocations where pointer equality matters
16066 (this is a clue for the dynamic linker, to make
16067 function pointer comparisons work between an
16068 application and shared library), otherwise set it
16069 to zero. */
16070 sym->st_shndx = SHN_UNDEF;
16071 if (!h->pointer_equality_needed)
16072 sym->st_value = 0;
16073 else if (!h->ref_regular_nonweak)
16074 {
16075 /* This breaks function pointer comparisons, but
16076 that is better than breaking tests for a NULL
16077 function pointer. */
16078 sym->st_value = 0;
16079 }
16080 break;
16081 }
16082
16083 if (h->needs_copy)
16084 {
16085 /* This symbol needs a copy reloc. Set it up. */
16086 Elf_Internal_Rela rela;
16087 asection *srel;
16088 bfd_byte *loc;
16089
16090 if (h->dynindx == -1
16091 || (h->root.type != bfd_link_hash_defined
16092 && h->root.type != bfd_link_hash_defweak)
16093 || htab->elf.srelbss == NULL
16094 || htab->elf.sreldynrelro == NULL)
16095 abort ();
16096
16097 rela.r_offset = (h->root.u.def.value
16098 + h->root.u.def.section->output_section->vma
16099 + h->root.u.def.section->output_offset);
16100 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16101 rela.r_addend = 0;
16102 if (h->root.u.def.section == htab->elf.sdynrelro)
16103 srel = htab->elf.sreldynrelro;
16104 else
16105 srel = htab->elf.srelbss;
16106 loc = srel->contents;
16107 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16108 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16109 }
16110
16111 return TRUE;
16112 }
16113
16114 /* Used to decide how to sort relocs in an optimal manner for the
16115 dynamic linker, before writing them out. */
16116
16117 static enum elf_reloc_type_class
16118 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16119 const asection *rel_sec,
16120 const Elf_Internal_Rela *rela)
16121 {
16122 enum elf_ppc64_reloc_type r_type;
16123 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16124
16125 if (rel_sec == htab->elf.irelplt)
16126 return reloc_class_ifunc;
16127
16128 r_type = ELF64_R_TYPE (rela->r_info);
16129 switch (r_type)
16130 {
16131 case R_PPC64_RELATIVE:
16132 return reloc_class_relative;
16133 case R_PPC64_JMP_SLOT:
16134 return reloc_class_plt;
16135 case R_PPC64_COPY:
16136 return reloc_class_copy;
16137 default:
16138 return reloc_class_normal;
16139 }
16140 }
16141
16142 /* Finish up the dynamic sections. */
16143
16144 static bfd_boolean
16145 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16146 struct bfd_link_info *info)
16147 {
16148 struct ppc_link_hash_table *htab;
16149 bfd *dynobj;
16150 asection *sdyn;
16151
16152 htab = ppc_hash_table (info);
16153 if (htab == NULL)
16154 return FALSE;
16155
16156 dynobj = htab->elf.dynobj;
16157 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16158
16159 if (htab->elf.dynamic_sections_created)
16160 {
16161 Elf64_External_Dyn *dyncon, *dynconend;
16162
16163 if (sdyn == NULL || htab->elf.sgot == NULL)
16164 abort ();
16165
16166 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16167 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16168 for (; dyncon < dynconend; dyncon++)
16169 {
16170 Elf_Internal_Dyn dyn;
16171 asection *s;
16172
16173 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16174
16175 switch (dyn.d_tag)
16176 {
16177 default:
16178 continue;
16179
16180 case DT_PPC64_GLINK:
16181 s = htab->glink;
16182 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16183 /* We stupidly defined DT_PPC64_GLINK to be the start
16184 of glink rather than the first entry point, which is
16185 what ld.so needs, and now have a bigger stub to
16186 support automatic multiple TOCs. */
16187 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16188 break;
16189
16190 case DT_PPC64_OPD:
16191 s = bfd_get_section_by_name (output_bfd, ".opd");
16192 if (s == NULL)
16193 continue;
16194 dyn.d_un.d_ptr = s->vma;
16195 break;
16196
16197 case DT_PPC64_OPT:
16198 if (htab->do_multi_toc && htab->multi_toc_needed)
16199 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16200 if (htab->has_plt_localentry0)
16201 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16202 break;
16203
16204 case DT_PPC64_OPDSZ:
16205 s = bfd_get_section_by_name (output_bfd, ".opd");
16206 if (s == NULL)
16207 continue;
16208 dyn.d_un.d_val = s->size;
16209 break;
16210
16211 case DT_PLTGOT:
16212 s = htab->elf.splt;
16213 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16214 break;
16215
16216 case DT_JMPREL:
16217 s = htab->elf.srelplt;
16218 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16219 break;
16220
16221 case DT_PLTRELSZ:
16222 dyn.d_un.d_val = htab->elf.srelplt->size;
16223 break;
16224
16225 case DT_TEXTREL:
16226 if (htab->local_ifunc_resolver)
16227 info->callbacks->einfo
16228 (_("%X%P: text relocations and GNU indirect "
16229 "functions will result in a segfault at runtime\n"));
16230 else if (htab->maybe_local_ifunc_resolver)
16231 info->callbacks->einfo
16232 (_("%P: warning: text relocations and GNU indirect "
16233 "functions may result in a segfault at runtime\n"));
16234 continue;
16235 }
16236
16237 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16238 }
16239 }
16240
16241 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16242 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16243 {
16244 /* Fill in the first entry in the global offset table.
16245 We use it to hold the link-time TOCbase. */
16246 bfd_put_64 (output_bfd,
16247 elf_gp (output_bfd) + TOC_BASE_OFF,
16248 htab->elf.sgot->contents);
16249
16250 /* Set .got entry size. */
16251 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16252 }
16253
16254 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16255 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16256 {
16257 /* Set .plt entry size. */
16258 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16259 = PLT_ENTRY_SIZE (htab);
16260 }
16261
16262 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16263 brlt ourselves if emitrelocations. */
16264 if (htab->brlt != NULL
16265 && htab->brlt->reloc_count != 0
16266 && !_bfd_elf_link_output_relocs (output_bfd,
16267 htab->brlt,
16268 elf_section_data (htab->brlt)->rela.hdr,
16269 elf_section_data (htab->brlt)->relocs,
16270 NULL))
16271 return FALSE;
16272
16273 if (htab->glink != NULL
16274 && htab->glink->reloc_count != 0
16275 && !_bfd_elf_link_output_relocs (output_bfd,
16276 htab->glink,
16277 elf_section_data (htab->glink)->rela.hdr,
16278 elf_section_data (htab->glink)->relocs,
16279 NULL))
16280 return FALSE;
16281
16282 if (htab->glink_eh_frame != NULL
16283 && htab->glink_eh_frame->size != 0)
16284 {
16285 bfd_vma val;
16286 bfd_byte *p;
16287 struct map_stub *group;
16288 size_t align = 4;
16289
16290 p = htab->glink_eh_frame->contents;
16291 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
16292
16293 for (group = htab->group; group != NULL; group = group->next)
16294 if (group->stub_sec != NULL)
16295 {
16296 /* Offset to stub section. */
16297 val = (group->stub_sec->output_section->vma
16298 + group->stub_sec->output_offset);
16299 val -= (htab->glink_eh_frame->output_section->vma
16300 + htab->glink_eh_frame->output_offset
16301 + (p + 8 - htab->glink_eh_frame->contents));
16302 if (val + 0x80000000 > 0xffffffff)
16303 {
16304 _bfd_error_handler
16305 (_("%s offset too large for .eh_frame sdata4 encoding"),
16306 group->stub_sec->name);
16307 return FALSE;
16308 }
16309 bfd_put_32 (dynobj, val, p + 8);
16310 p += stub_eh_frame_size (group, align);
16311 }
16312 if (htab->glink != NULL && htab->glink->size != 0)
16313 {
16314 /* Offset to .glink. */
16315 val = (htab->glink->output_section->vma
16316 + htab->glink->output_offset
16317 + 8);
16318 val -= (htab->glink_eh_frame->output_section->vma
16319 + htab->glink_eh_frame->output_offset
16320 + (p + 8 - htab->glink_eh_frame->contents));
16321 if (val + 0x80000000 > 0xffffffff)
16322 {
16323 _bfd_error_handler
16324 (_("%s offset too large for .eh_frame sdata4 encoding"),
16325 htab->glink->name);
16326 return FALSE;
16327 }
16328 bfd_put_32 (dynobj, val, p + 8);
16329 p += (24 + align - 1) & -align;
16330 }
16331
16332 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16333 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16334 htab->glink_eh_frame,
16335 htab->glink_eh_frame->contents))
16336 return FALSE;
16337 }
16338
16339 /* We need to handle writing out multiple GOT sections ourselves,
16340 since we didn't add them to DYNOBJ. We know dynobj is the first
16341 bfd. */
16342 while ((dynobj = dynobj->link.next) != NULL)
16343 {
16344 asection *s;
16345
16346 if (!is_ppc64_elf (dynobj))
16347 continue;
16348
16349 s = ppc64_elf_tdata (dynobj)->got;
16350 if (s != NULL
16351 && s->size != 0
16352 && s->output_section != bfd_abs_section_ptr
16353 && !bfd_set_section_contents (output_bfd, s->output_section,
16354 s->contents, s->output_offset,
16355 s->size))
16356 return FALSE;
16357 s = ppc64_elf_tdata (dynobj)->relgot;
16358 if (s != NULL
16359 && s->size != 0
16360 && s->output_section != bfd_abs_section_ptr
16361 && !bfd_set_section_contents (output_bfd, s->output_section,
16362 s->contents, s->output_offset,
16363 s->size))
16364 return FALSE;
16365 }
16366
16367 return TRUE;
16368 }
16369
16370 #include "elf64-target.h"
16371
16372 /* FreeBSD support */
16373
16374 #undef TARGET_LITTLE_SYM
16375 #undef TARGET_LITTLE_NAME
16376
16377 #undef TARGET_BIG_SYM
16378 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16379 #undef TARGET_BIG_NAME
16380 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16381
16382 #undef ELF_OSABI
16383 #define ELF_OSABI ELFOSABI_FREEBSD
16384
16385 #undef elf64_bed
16386 #define elf64_bed elf64_powerpc_fbsd_bed
16387
16388 #include "elf64-target.h"
This page took 0.551801 seconds and 5 git commands to generate.