971adc4960cf4a5ad707233b342a047a995d401f
[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];
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 strncpy (data + 56, va_arg (ap, const char *), 80);
3045 va_end (ap);
3046 return elfcore_write_note (abfd, buf, bufsiz,
3047 "CORE", note_type, data, sizeof (data));
3048 }
3049
3050 case NT_PRSTATUS:
3051 {
3052 char data[504];
3053 va_list ap;
3054 long pid;
3055 int cursig;
3056 const void *greg;
3057
3058 va_start (ap, note_type);
3059 memset (data, 0, 112);
3060 pid = va_arg (ap, long);
3061 bfd_put_32 (abfd, pid, data + 32);
3062 cursig = va_arg (ap, int);
3063 bfd_put_16 (abfd, cursig, data + 12);
3064 greg = va_arg (ap, const void *);
3065 memcpy (data + 112, greg, 384);
3066 memset (data + 496, 0, 8);
3067 va_end (ap);
3068 return elfcore_write_note (abfd, buf, bufsiz,
3069 "CORE", note_type, data, sizeof (data));
3070 }
3071 }
3072 }
3073
3074 /* Add extra PPC sections. */
3075
3076 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3077 {
3078 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3079 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3080 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3081 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3082 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3083 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3084 { NULL, 0, 0, 0, 0 }
3085 };
3086
3087 enum _ppc64_sec_type {
3088 sec_normal = 0,
3089 sec_opd = 1,
3090 sec_toc = 2
3091 };
3092
3093 struct _ppc64_elf_section_data
3094 {
3095 struct bfd_elf_section_data elf;
3096
3097 union
3098 {
3099 /* An array with one entry for each opd function descriptor,
3100 and some spares since opd entries may be either 16 or 24 bytes. */
3101 #define OPD_NDX(OFF) ((OFF) >> 4)
3102 struct _opd_sec_data
3103 {
3104 /* Points to the function code section for local opd entries. */
3105 asection **func_sec;
3106
3107 /* After editing .opd, adjust references to opd local syms. */
3108 long *adjust;
3109 } opd;
3110
3111 /* An array for toc sections, indexed by offset/8. */
3112 struct _toc_sec_data
3113 {
3114 /* Specifies the relocation symbol index used at a given toc offset. */
3115 unsigned *symndx;
3116
3117 /* And the relocation addend. */
3118 bfd_vma *add;
3119 } toc;
3120 } u;
3121
3122 enum _ppc64_sec_type sec_type:2;
3123
3124 /* Flag set when small branches are detected. Used to
3125 select suitable defaults for the stub group size. */
3126 unsigned int has_14bit_branch:1;
3127
3128 /* Flag set when PLTCALL relocs are detected. */
3129 unsigned int has_pltcall:1;
3130 };
3131
3132 #define ppc64_elf_section_data(sec) \
3133 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3134
3135 static bfd_boolean
3136 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3137 {
3138 if (!sec->used_by_bfd)
3139 {
3140 struct _ppc64_elf_section_data *sdata;
3141 bfd_size_type amt = sizeof (*sdata);
3142
3143 sdata = bfd_zalloc (abfd, amt);
3144 if (sdata == NULL)
3145 return FALSE;
3146 sec->used_by_bfd = sdata;
3147 }
3148
3149 return _bfd_elf_new_section_hook (abfd, sec);
3150 }
3151
3152 static struct _opd_sec_data *
3153 get_opd_info (asection * sec)
3154 {
3155 if (sec != NULL
3156 && ppc64_elf_section_data (sec) != NULL
3157 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3158 return &ppc64_elf_section_data (sec)->u.opd;
3159 return NULL;
3160 }
3161 \f
3162 /* Parameters for the qsort hook. */
3163 static bfd_boolean synthetic_relocatable;
3164 static asection *synthetic_opd;
3165
3166 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3167
3168 static int
3169 compare_symbols (const void *ap, const void *bp)
3170 {
3171 const asymbol *a = * (const asymbol **) ap;
3172 const asymbol *b = * (const asymbol **) bp;
3173
3174 /* Section symbols first. */
3175 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3176 return -1;
3177 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3178 return 1;
3179
3180 /* then .opd symbols. */
3181 if (synthetic_opd != NULL)
3182 {
3183 if (strcmp (a->section->name, ".opd") == 0
3184 && strcmp (b->section->name, ".opd") != 0)
3185 return -1;
3186 if (strcmp (a->section->name, ".opd") != 0
3187 && strcmp (b->section->name, ".opd") == 0)
3188 return 1;
3189 }
3190
3191 /* then other code symbols. */
3192 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3193 == (SEC_CODE | SEC_ALLOC)
3194 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3195 != (SEC_CODE | SEC_ALLOC))
3196 return -1;
3197
3198 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3199 != (SEC_CODE | SEC_ALLOC)
3200 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3201 == (SEC_CODE | SEC_ALLOC))
3202 return 1;
3203
3204 if (synthetic_relocatable)
3205 {
3206 if (a->section->id < b->section->id)
3207 return -1;
3208
3209 if (a->section->id > b->section->id)
3210 return 1;
3211 }
3212
3213 if (a->value + a->section->vma < b->value + b->section->vma)
3214 return -1;
3215
3216 if (a->value + a->section->vma > b->value + b->section->vma)
3217 return 1;
3218
3219 /* For syms with the same value, prefer strong dynamic global function
3220 syms over other syms. */
3221 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3222 return -1;
3223
3224 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3225 return 1;
3226
3227 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3228 return -1;
3229
3230 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3231 return 1;
3232
3233 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3234 return -1;
3235
3236 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3237 return 1;
3238
3239 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3240 return -1;
3241
3242 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3243 return 1;
3244
3245 return a > b;
3246 }
3247
3248 /* Search SYMS for a symbol of the given VALUE. */
3249
3250 static asymbol *
3251 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3252 {
3253 long mid;
3254
3255 if (id == (unsigned) -1)
3256 {
3257 while (lo < hi)
3258 {
3259 mid = (lo + hi) >> 1;
3260 if (syms[mid]->value + syms[mid]->section->vma < value)
3261 lo = mid + 1;
3262 else if (syms[mid]->value + syms[mid]->section->vma > value)
3263 hi = mid;
3264 else
3265 return syms[mid];
3266 }
3267 }
3268 else
3269 {
3270 while (lo < hi)
3271 {
3272 mid = (lo + hi) >> 1;
3273 if (syms[mid]->section->id < id)
3274 lo = mid + 1;
3275 else if (syms[mid]->section->id > id)
3276 hi = mid;
3277 else if (syms[mid]->value < value)
3278 lo = mid + 1;
3279 else if (syms[mid]->value > value)
3280 hi = mid;
3281 else
3282 return syms[mid];
3283 }
3284 }
3285 return NULL;
3286 }
3287
3288 static bfd_boolean
3289 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3290 {
3291 bfd_vma vma = *(bfd_vma *) ptr;
3292 return ((section->flags & SEC_ALLOC) != 0
3293 && section->vma <= vma
3294 && vma < section->vma + section->size);
3295 }
3296
3297 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3298 entry syms. Also generate @plt symbols for the glink branch table.
3299 Returns count of synthetic symbols in RET or -1 on error. */
3300
3301 static long
3302 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3303 long static_count, asymbol **static_syms,
3304 long dyn_count, asymbol **dyn_syms,
3305 asymbol **ret)
3306 {
3307 asymbol *s;
3308 size_t i, j, count;
3309 char *names;
3310 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3311 asection *opd = NULL;
3312 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3313 asymbol **syms;
3314 int abi = abiversion (abfd);
3315
3316 *ret = NULL;
3317
3318 if (abi < 2)
3319 {
3320 opd = bfd_get_section_by_name (abfd, ".opd");
3321 if (opd == NULL && abi == 1)
3322 return 0;
3323 }
3324
3325 syms = NULL;
3326 codesecsym = 0;
3327 codesecsymend = 0;
3328 secsymend = 0;
3329 opdsymend = 0;
3330 symcount = 0;
3331 if (opd != NULL)
3332 {
3333 symcount = static_count;
3334 if (!relocatable)
3335 symcount += dyn_count;
3336 if (symcount == 0)
3337 return 0;
3338
3339 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3340 if (syms == NULL)
3341 return -1;
3342
3343 if (!relocatable && static_count != 0 && dyn_count != 0)
3344 {
3345 /* Use both symbol tables. */
3346 memcpy (syms, static_syms, static_count * sizeof (*syms));
3347 memcpy (syms + static_count, dyn_syms,
3348 (dyn_count + 1) * sizeof (*syms));
3349 }
3350 else if (!relocatable && static_count == 0)
3351 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3352 else
3353 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3354
3355 /* Trim uninteresting symbols. Interesting symbols are section,
3356 function, and notype symbols. */
3357 for (i = 0, j = 0; i < symcount; ++i)
3358 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3359 | BSF_RELC | BSF_SRELC)) == 0)
3360 syms[j++] = syms[i];
3361 symcount = j;
3362
3363 synthetic_relocatable = relocatable;
3364 synthetic_opd = opd;
3365 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3366
3367 if (!relocatable && symcount > 1)
3368 {
3369 /* Trim duplicate syms, since we may have merged the normal and
3370 dynamic symbols. Actually, we only care about syms that have
3371 different values, so trim any with the same value. */
3372 for (i = 1, j = 1; i < symcount; ++i)
3373 if (syms[i - 1]->value + syms[i - 1]->section->vma
3374 != syms[i]->value + syms[i]->section->vma)
3375 syms[j++] = syms[i];
3376 symcount = j;
3377 }
3378
3379 i = 0;
3380 /* Note that here and in compare_symbols we can't compare opd and
3381 sym->section directly. With separate debug info files, the
3382 symbols will be extracted from the debug file while abfd passed
3383 to this function is the real binary. */
3384 if (strcmp (syms[i]->section->name, ".opd") == 0)
3385 ++i;
3386 codesecsym = i;
3387
3388 for (; i < symcount; ++i)
3389 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3390 | SEC_THREAD_LOCAL))
3391 != (SEC_CODE | SEC_ALLOC))
3392 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3393 break;
3394 codesecsymend = i;
3395
3396 for (; i < symcount; ++i)
3397 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3398 break;
3399 secsymend = i;
3400
3401 for (; i < symcount; ++i)
3402 if (strcmp (syms[i]->section->name, ".opd") != 0)
3403 break;
3404 opdsymend = i;
3405
3406 for (; i < symcount; ++i)
3407 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3408 != (SEC_CODE | SEC_ALLOC))
3409 break;
3410 symcount = i;
3411 }
3412 count = 0;
3413
3414 if (relocatable)
3415 {
3416 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3417 arelent *r;
3418 size_t size;
3419 size_t relcount;
3420
3421 if (opdsymend == secsymend)
3422 goto done;
3423
3424 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3425 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3426 if (relcount == 0)
3427 goto done;
3428
3429 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3430 {
3431 count = -1;
3432 goto done;
3433 }
3434
3435 size = 0;
3436 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3437 {
3438 asymbol *sym;
3439
3440 while (r < opd->relocation + relcount
3441 && r->address < syms[i]->value + opd->vma)
3442 ++r;
3443
3444 if (r == opd->relocation + relcount)
3445 break;
3446
3447 if (r->address != syms[i]->value + opd->vma)
3448 continue;
3449
3450 if (r->howto->type != R_PPC64_ADDR64)
3451 continue;
3452
3453 sym = *r->sym_ptr_ptr;
3454 if (!sym_exists_at (syms, opdsymend, symcount,
3455 sym->section->id, sym->value + r->addend))
3456 {
3457 ++count;
3458 size += sizeof (asymbol);
3459 size += strlen (syms[i]->name) + 2;
3460 }
3461 }
3462
3463 if (size == 0)
3464 goto done;
3465 s = *ret = bfd_malloc (size);
3466 if (s == NULL)
3467 {
3468 count = -1;
3469 goto done;
3470 }
3471
3472 names = (char *) (s + count);
3473
3474 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3475 {
3476 asymbol *sym;
3477
3478 while (r < opd->relocation + relcount
3479 && r->address < syms[i]->value + opd->vma)
3480 ++r;
3481
3482 if (r == opd->relocation + relcount)
3483 break;
3484
3485 if (r->address != syms[i]->value + opd->vma)
3486 continue;
3487
3488 if (r->howto->type != R_PPC64_ADDR64)
3489 continue;
3490
3491 sym = *r->sym_ptr_ptr;
3492 if (!sym_exists_at (syms, opdsymend, symcount,
3493 sym->section->id, sym->value + r->addend))
3494 {
3495 size_t len;
3496
3497 *s = *syms[i];
3498 s->flags |= BSF_SYNTHETIC;
3499 s->section = sym->section;
3500 s->value = sym->value + r->addend;
3501 s->name = names;
3502 *names++ = '.';
3503 len = strlen (syms[i]->name);
3504 memcpy (names, syms[i]->name, len + 1);
3505 names += len + 1;
3506 /* Have udata.p point back to the original symbol this
3507 synthetic symbol was derived from. */
3508 s->udata.p = syms[i];
3509 s++;
3510 }
3511 }
3512 }
3513 else
3514 {
3515 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3516 bfd_byte *contents = NULL;
3517 size_t size;
3518 size_t plt_count = 0;
3519 bfd_vma glink_vma = 0, resolv_vma = 0;
3520 asection *dynamic, *glink = NULL, *relplt = NULL;
3521 arelent *p;
3522
3523 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3524 {
3525 free_contents_and_exit_err:
3526 count = -1;
3527 free_contents_and_exit:
3528 if (contents)
3529 free (contents);
3530 goto done;
3531 }
3532
3533 size = 0;
3534 for (i = secsymend; i < opdsymend; ++i)
3535 {
3536 bfd_vma ent;
3537
3538 /* Ignore bogus symbols. */
3539 if (syms[i]->value > opd->size - 8)
3540 continue;
3541
3542 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3543 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3544 {
3545 ++count;
3546 size += sizeof (asymbol);
3547 size += strlen (syms[i]->name) + 2;
3548 }
3549 }
3550
3551 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3552 if (dyn_count != 0
3553 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3554 {
3555 bfd_byte *dynbuf, *extdyn, *extdynend;
3556 size_t extdynsize;
3557 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3558
3559 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3560 goto free_contents_and_exit_err;
3561
3562 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3563 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3564
3565 extdyn = dynbuf;
3566 extdynend = extdyn + dynamic->size;
3567 for (; extdyn < extdynend; extdyn += extdynsize)
3568 {
3569 Elf_Internal_Dyn dyn;
3570 (*swap_dyn_in) (abfd, extdyn, &dyn);
3571
3572 if (dyn.d_tag == DT_NULL)
3573 break;
3574
3575 if (dyn.d_tag == DT_PPC64_GLINK)
3576 {
3577 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3578 See comment in ppc64_elf_finish_dynamic_sections. */
3579 glink_vma = dyn.d_un.d_val + 8 * 4;
3580 /* The .glink section usually does not survive the final
3581 link; search for the section (usually .text) where the
3582 glink stubs now reside. */
3583 glink = bfd_sections_find_if (abfd, section_covers_vma,
3584 &glink_vma);
3585 break;
3586 }
3587 }
3588
3589 free (dynbuf);
3590 }
3591
3592 if (glink != NULL)
3593 {
3594 /* Determine __glink trampoline by reading the relative branch
3595 from the first glink stub. */
3596 bfd_byte buf[4];
3597 unsigned int off = 0;
3598
3599 while (bfd_get_section_contents (abfd, glink, buf,
3600 glink_vma + off - glink->vma, 4))
3601 {
3602 unsigned int insn = bfd_get_32 (abfd, buf);
3603 insn ^= B_DOT;
3604 if ((insn & ~0x3fffffc) == 0)
3605 {
3606 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3607 break;
3608 }
3609 off += 4;
3610 if (off > 4)
3611 break;
3612 }
3613
3614 if (resolv_vma)
3615 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3616
3617 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3618 if (relplt != NULL)
3619 {
3620 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3621 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3622 goto free_contents_and_exit_err;
3623
3624 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3625 size += plt_count * sizeof (asymbol);
3626
3627 p = relplt->relocation;
3628 for (i = 0; i < plt_count; i++, p++)
3629 {
3630 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3631 if (p->addend != 0)
3632 size += sizeof ("+0x") - 1 + 16;
3633 }
3634 }
3635 }
3636
3637 if (size == 0)
3638 goto free_contents_and_exit;
3639 s = *ret = bfd_malloc (size);
3640 if (s == NULL)
3641 goto free_contents_and_exit_err;
3642
3643 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3644
3645 for (i = secsymend; i < opdsymend; ++i)
3646 {
3647 bfd_vma ent;
3648
3649 if (syms[i]->value > opd->size - 8)
3650 continue;
3651
3652 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3653 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3654 {
3655 size_t lo, hi;
3656 size_t len;
3657 asection *sec = abfd->sections;
3658
3659 *s = *syms[i];
3660 lo = codesecsym;
3661 hi = codesecsymend;
3662 while (lo < hi)
3663 {
3664 size_t mid = (lo + hi) >> 1;
3665 if (syms[mid]->section->vma < ent)
3666 lo = mid + 1;
3667 else if (syms[mid]->section->vma > ent)
3668 hi = mid;
3669 else
3670 {
3671 sec = syms[mid]->section;
3672 break;
3673 }
3674 }
3675
3676 if (lo >= hi && lo > codesecsym)
3677 sec = syms[lo - 1]->section;
3678
3679 for (; sec != NULL; sec = sec->next)
3680 {
3681 if (sec->vma > ent)
3682 break;
3683 /* SEC_LOAD may not be set if SEC is from a separate debug
3684 info file. */
3685 if ((sec->flags & SEC_ALLOC) == 0)
3686 break;
3687 if ((sec->flags & SEC_CODE) != 0)
3688 s->section = sec;
3689 }
3690 s->flags |= BSF_SYNTHETIC;
3691 s->value = ent - s->section->vma;
3692 s->name = names;
3693 *names++ = '.';
3694 len = strlen (syms[i]->name);
3695 memcpy (names, syms[i]->name, len + 1);
3696 names += len + 1;
3697 /* Have udata.p point back to the original symbol this
3698 synthetic symbol was derived from. */
3699 s->udata.p = syms[i];
3700 s++;
3701 }
3702 }
3703 free (contents);
3704
3705 if (glink != NULL && relplt != NULL)
3706 {
3707 if (resolv_vma)
3708 {
3709 /* Add a symbol for the main glink trampoline. */
3710 memset (s, 0, sizeof *s);
3711 s->the_bfd = abfd;
3712 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3713 s->section = glink;
3714 s->value = resolv_vma - glink->vma;
3715 s->name = names;
3716 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3717 names += sizeof ("__glink_PLTresolve");
3718 s++;
3719 count++;
3720 }
3721
3722 /* FIXME: It would be very much nicer to put sym@plt on the
3723 stub rather than on the glink branch table entry. The
3724 objdump disassembler would then use a sensible symbol
3725 name on plt calls. The difficulty in doing so is
3726 a) finding the stubs, and,
3727 b) matching stubs against plt entries, and,
3728 c) there can be multiple stubs for a given plt entry.
3729
3730 Solving (a) could be done by code scanning, but older
3731 ppc64 binaries used different stubs to current code.
3732 (b) is the tricky one since you need to known the toc
3733 pointer for at least one function that uses a pic stub to
3734 be able to calculate the plt address referenced.
3735 (c) means gdb would need to set multiple breakpoints (or
3736 find the glink branch itself) when setting breakpoints
3737 for pending shared library loads. */
3738 p = relplt->relocation;
3739 for (i = 0; i < plt_count; i++, p++)
3740 {
3741 size_t len;
3742
3743 *s = **p->sym_ptr_ptr;
3744 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3745 we are defining a symbol, ensure one of them is set. */
3746 if ((s->flags & BSF_LOCAL) == 0)
3747 s->flags |= BSF_GLOBAL;
3748 s->flags |= BSF_SYNTHETIC;
3749 s->section = glink;
3750 s->value = glink_vma - glink->vma;
3751 s->name = names;
3752 s->udata.p = NULL;
3753 len = strlen ((*p->sym_ptr_ptr)->name);
3754 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3755 names += len;
3756 if (p->addend != 0)
3757 {
3758 memcpy (names, "+0x", sizeof ("+0x") - 1);
3759 names += sizeof ("+0x") - 1;
3760 bfd_sprintf_vma (abfd, names, p->addend);
3761 names += strlen (names);
3762 }
3763 memcpy (names, "@plt", sizeof ("@plt"));
3764 names += sizeof ("@plt");
3765 s++;
3766 if (abi < 2)
3767 {
3768 glink_vma += 8;
3769 if (i >= 0x8000)
3770 glink_vma += 4;
3771 }
3772 else
3773 glink_vma += 4;
3774 }
3775 count += plt_count;
3776 }
3777 }
3778
3779 done:
3780 free (syms);
3781 return count;
3782 }
3783 \f
3784 /* The following functions are specific to the ELF linker, while
3785 functions above are used generally. Those named ppc64_elf_* are
3786 called by the main ELF linker code. They appear in this file more
3787 or less in the order in which they are called. eg.
3788 ppc64_elf_check_relocs is called early in the link process,
3789 ppc64_elf_finish_dynamic_sections is one of the last functions
3790 called.
3791
3792 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3793 functions have both a function code symbol and a function descriptor
3794 symbol. A call to foo in a relocatable object file looks like:
3795
3796 . .text
3797 . x:
3798 . bl .foo
3799 . nop
3800
3801 The function definition in another object file might be:
3802
3803 . .section .opd
3804 . foo: .quad .foo
3805 . .quad .TOC.@tocbase
3806 . .quad 0
3807 .
3808 . .text
3809 . .foo: blr
3810
3811 When the linker resolves the call during a static link, the branch
3812 unsurprisingly just goes to .foo and the .opd information is unused.
3813 If the function definition is in a shared library, things are a little
3814 different: The call goes via a plt call stub, the opd information gets
3815 copied to the plt, and the linker patches the nop.
3816
3817 . x:
3818 . bl .foo_stub
3819 . ld 2,40(1)
3820 .
3821 .
3822 . .foo_stub:
3823 . std 2,40(1) # in practice, the call stub
3824 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3825 . addi 11,11,Lfoo@toc@l # this is the general idea
3826 . ld 12,0(11)
3827 . ld 2,8(11)
3828 . mtctr 12
3829 . ld 11,16(11)
3830 . bctr
3831 .
3832 . .section .plt
3833 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3834
3835 The "reloc ()" notation is supposed to indicate that the linker emits
3836 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3837 copying.
3838
3839 What are the difficulties here? Well, firstly, the relocations
3840 examined by the linker in check_relocs are against the function code
3841 sym .foo, while the dynamic relocation in the plt is emitted against
3842 the function descriptor symbol, foo. Somewhere along the line, we need
3843 to carefully copy dynamic link information from one symbol to the other.
3844 Secondly, the generic part of the elf linker will make .foo a dynamic
3845 symbol as is normal for most other backends. We need foo dynamic
3846 instead, at least for an application final link. However, when
3847 creating a shared library containing foo, we need to have both symbols
3848 dynamic so that references to .foo are satisfied during the early
3849 stages of linking. Otherwise the linker might decide to pull in a
3850 definition from some other object, eg. a static library.
3851
3852 Update: As of August 2004, we support a new convention. Function
3853 calls may use the function descriptor symbol, ie. "bl foo". This
3854 behaves exactly as "bl .foo". */
3855
3856 /* Of those relocs that might be copied as dynamic relocs, this
3857 function selects those that must be copied when linking a shared
3858 library or PIE, even when the symbol is local. */
3859
3860 static int
3861 must_be_dyn_reloc (struct bfd_link_info *info,
3862 enum elf_ppc64_reloc_type r_type)
3863 {
3864 switch (r_type)
3865 {
3866 default:
3867 /* Only relative relocs can be resolved when the object load
3868 address isn't fixed. DTPREL64 is excluded because the
3869 dynamic linker needs to differentiate global dynamic from
3870 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3871 return 1;
3872
3873 case R_PPC64_REL32:
3874 case R_PPC64_REL64:
3875 case R_PPC64_REL30:
3876 return 0;
3877
3878 case R_PPC64_TPREL16:
3879 case R_PPC64_TPREL16_LO:
3880 case R_PPC64_TPREL16_HI:
3881 case R_PPC64_TPREL16_HA:
3882 case R_PPC64_TPREL16_DS:
3883 case R_PPC64_TPREL16_LO_DS:
3884 case R_PPC64_TPREL16_HIGH:
3885 case R_PPC64_TPREL16_HIGHA:
3886 case R_PPC64_TPREL16_HIGHER:
3887 case R_PPC64_TPREL16_HIGHERA:
3888 case R_PPC64_TPREL16_HIGHEST:
3889 case R_PPC64_TPREL16_HIGHESTA:
3890 case R_PPC64_TPREL64:
3891 /* These relocations are relative but in a shared library the
3892 linker doesn't know the thread pointer base. */
3893 return bfd_link_dll (info);
3894 }
3895 }
3896
3897 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3898 copying dynamic variables from a shared lib into an app's dynbss
3899 section, and instead use a dynamic relocation to point into the
3900 shared lib. With code that gcc generates, it's vital that this be
3901 enabled; In the PowerPC64 ABI, the address of a function is actually
3902 the address of a function descriptor, which resides in the .opd
3903 section. gcc uses the descriptor directly rather than going via the
3904 GOT as some other ABI's do, which means that initialized function
3905 pointers must reference the descriptor. Thus, a function pointer
3906 initialized to the address of a function in a shared library will
3907 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3908 redefines the function descriptor symbol to point to the copy. This
3909 presents a problem as a plt entry for that function is also
3910 initialized from the function descriptor symbol and the copy reloc
3911 may not be initialized first. */
3912 #define ELIMINATE_COPY_RELOCS 1
3913
3914 /* Section name for stubs is the associated section name plus this
3915 string. */
3916 #define STUB_SUFFIX ".stub"
3917
3918 /* Linker stubs.
3919 ppc_stub_long_branch:
3920 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3921 destination, but a 24 bit branch in a stub section will reach.
3922 . b dest
3923
3924 ppc_stub_plt_branch:
3925 Similar to the above, but a 24 bit branch in the stub section won't
3926 reach its destination.
3927 . addis %r11,%r2,xxx@toc@ha
3928 . ld %r12,xxx@toc@l(%r11)
3929 . mtctr %r12
3930 . bctr
3931
3932 ppc_stub_plt_call:
3933 Used to call a function in a shared library. If it so happens that
3934 the plt entry referenced crosses a 64k boundary, then an extra
3935 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3936 . std %r2,40(%r1)
3937 . addis %r11,%r2,xxx@toc@ha
3938 . ld %r12,xxx+0@toc@l(%r11)
3939 . mtctr %r12
3940 . ld %r2,xxx+8@toc@l(%r11)
3941 . ld %r11,xxx+16@toc@l(%r11)
3942 . bctr
3943
3944 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3945 code to adjust the value and save r2 to support multiple toc sections.
3946 A ppc_stub_long_branch with an r2 offset looks like:
3947 . std %r2,40(%r1)
3948 . addis %r2,%r2,off@ha
3949 . addi %r2,%r2,off@l
3950 . b dest
3951
3952 A ppc_stub_plt_branch with an r2 offset looks like:
3953 . std %r2,40(%r1)
3954 . addis %r11,%r2,xxx@toc@ha
3955 . ld %r12,xxx@toc@l(%r11)
3956 . addis %r2,%r2,off@ha
3957 . addi %r2,%r2,off@l
3958 . mtctr %r12
3959 . bctr
3960
3961 In cases where the "addis" instruction would add zero, the "addis" is
3962 omitted and following instructions modified slightly in some cases.
3963 */
3964
3965 enum ppc_stub_type {
3966 ppc_stub_none,
3967 ppc_stub_long_branch,
3968 ppc_stub_long_branch_r2off,
3969 ppc_stub_plt_branch,
3970 ppc_stub_plt_branch_r2off,
3971 ppc_stub_plt_call,
3972 ppc_stub_plt_call_r2save,
3973 ppc_stub_global_entry,
3974 ppc_stub_save_res
3975 };
3976
3977 /* Information on stub grouping. */
3978 struct map_stub
3979 {
3980 /* The stub section. */
3981 asection *stub_sec;
3982 /* This is the section to which stubs in the group will be attached. */
3983 asection *link_sec;
3984 /* Next group. */
3985 struct map_stub *next;
3986 /* Whether to emit a copy of register save/restore functions in this
3987 group. */
3988 int needs_save_res;
3989 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3990 or -1u if no such stub with bctrl exists. */
3991 unsigned int tls_get_addr_opt_bctrl;
3992 };
3993
3994 struct ppc_stub_hash_entry {
3995
3996 /* Base hash table entry structure. */
3997 struct bfd_hash_entry root;
3998
3999 enum ppc_stub_type stub_type;
4000
4001 /* Group information. */
4002 struct map_stub *group;
4003
4004 /* Offset within stub_sec of the beginning of this stub. */
4005 bfd_vma stub_offset;
4006
4007 /* Given the symbol's value and its section we can determine its final
4008 value when building the stubs (so the stub knows where to jump. */
4009 bfd_vma target_value;
4010 asection *target_section;
4011
4012 /* The symbol table entry, if any, that this was derived from. */
4013 struct ppc_link_hash_entry *h;
4014 struct plt_entry *plt_ent;
4015
4016 /* Symbol type. */
4017 unsigned char symtype;
4018
4019 /* Symbol st_other. */
4020 unsigned char other;
4021 };
4022
4023 struct ppc_branch_hash_entry {
4024
4025 /* Base hash table entry structure. */
4026 struct bfd_hash_entry root;
4027
4028 /* Offset within branch lookup table. */
4029 unsigned int offset;
4030
4031 /* Generation marker. */
4032 unsigned int iter;
4033 };
4034
4035 /* Used to track dynamic relocations for local symbols. */
4036 struct ppc_dyn_relocs
4037 {
4038 struct ppc_dyn_relocs *next;
4039
4040 /* The input section of the reloc. */
4041 asection *sec;
4042
4043 /* Total number of relocs copied for the input section. */
4044 unsigned int count : 31;
4045
4046 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4047 unsigned int ifunc : 1;
4048 };
4049
4050 struct ppc_link_hash_entry
4051 {
4052 struct elf_link_hash_entry elf;
4053
4054 union {
4055 /* A pointer to the most recently used stub hash entry against this
4056 symbol. */
4057 struct ppc_stub_hash_entry *stub_cache;
4058
4059 /* A pointer to the next symbol starting with a '.' */
4060 struct ppc_link_hash_entry *next_dot_sym;
4061 } u;
4062
4063 /* Track dynamic relocs copied for this symbol. */
4064 struct elf_dyn_relocs *dyn_relocs;
4065
4066 /* Link between function code and descriptor symbols. */
4067 struct ppc_link_hash_entry *oh;
4068
4069 /* Flag function code and descriptor symbols. */
4070 unsigned int is_func:1;
4071 unsigned int is_func_descriptor:1;
4072 unsigned int fake:1;
4073
4074 /* Whether global opd/toc sym has been adjusted or not.
4075 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4076 should be set for all globals defined in any opd/toc section. */
4077 unsigned int adjust_done:1;
4078
4079 /* Set if this is an out-of-line register save/restore function,
4080 with non-standard calling convention. */
4081 unsigned int save_res:1;
4082
4083 /* Set if a duplicate symbol with non-zero localentry is detected,
4084 even when the duplicate symbol does not provide a definition. */
4085 unsigned int non_zero_localentry:1;
4086
4087 /* Contexts in which symbol is used in the GOT (or TOC).
4088 Bits are or'd into the mask as the corresponding relocs are
4089 encountered during check_relocs, with TLS_TLS being set when any
4090 of the other TLS bits are set. tls_optimize clears bits when
4091 optimizing to indicate the corresponding GOT entry type is not
4092 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4093 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4094 separate flag rather than setting TPREL just for convenience in
4095 distinguishing the two cases.
4096 These flags are also kept for local symbols. */
4097 #define TLS_TLS 1 /* Any TLS reloc. */
4098 #define TLS_GD 2 /* GD reloc. */
4099 #define TLS_LD 4 /* LD reloc. */
4100 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4101 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4102 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4103 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4104 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4105 unsigned char tls_mask;
4106
4107 /* The above field is also used to mark function symbols. In which
4108 case TLS_TLS will be 0. */
4109 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4110 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4111 #define NON_GOT 256 /* local symbol plt, not stored. */
4112 };
4113
4114 /* ppc64 ELF linker hash table. */
4115
4116 struct ppc_link_hash_table
4117 {
4118 struct elf_link_hash_table elf;
4119
4120 /* The stub hash table. */
4121 struct bfd_hash_table stub_hash_table;
4122
4123 /* Another hash table for plt_branch stubs. */
4124 struct bfd_hash_table branch_hash_table;
4125
4126 /* Hash table for function prologue tocsave. */
4127 htab_t tocsave_htab;
4128
4129 /* Various options and other info passed from the linker. */
4130 struct ppc64_elf_params *params;
4131
4132 /* The size of sec_info below. */
4133 unsigned int sec_info_arr_size;
4134
4135 /* Per-section array of extra section info. Done this way rather
4136 than as part of ppc64_elf_section_data so we have the info for
4137 non-ppc64 sections. */
4138 struct
4139 {
4140 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4141 bfd_vma toc_off;
4142
4143 union
4144 {
4145 /* The section group that this section belongs to. */
4146 struct map_stub *group;
4147 /* A temp section list pointer. */
4148 asection *list;
4149 } u;
4150 } *sec_info;
4151
4152 /* Linked list of groups. */
4153 struct map_stub *group;
4154
4155 /* Temp used when calculating TOC pointers. */
4156 bfd_vma toc_curr;
4157 bfd *toc_bfd;
4158 asection *toc_first_sec;
4159
4160 /* Used when adding symbols. */
4161 struct ppc_link_hash_entry *dot_syms;
4162
4163 /* Shortcuts to get to dynamic linker sections. */
4164 asection *glink;
4165 asection *global_entry;
4166 asection *sfpr;
4167 asection *pltlocal;
4168 asection *relpltlocal;
4169 asection *brlt;
4170 asection *relbrlt;
4171 asection *glink_eh_frame;
4172
4173 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4174 struct ppc_link_hash_entry *tls_get_addr;
4175 struct ppc_link_hash_entry *tls_get_addr_fd;
4176
4177 /* The size of reliplt used by got entry relocs. */
4178 bfd_size_type got_reli_size;
4179
4180 /* Statistics. */
4181 unsigned long stub_count[ppc_stub_global_entry];
4182
4183 /* Number of stubs against global syms. */
4184 unsigned long stub_globals;
4185
4186 /* Set if we're linking code with function descriptors. */
4187 unsigned int opd_abi:1;
4188
4189 /* Support for multiple toc sections. */
4190 unsigned int do_multi_toc:1;
4191 unsigned int multi_toc_needed:1;
4192 unsigned int second_toc_pass:1;
4193 unsigned int do_toc_opt:1;
4194
4195 /* Set if tls optimization is enabled. */
4196 unsigned int do_tls_opt:1;
4197
4198 /* Set if inline plt calls should be converted to direct calls. */
4199 unsigned int can_convert_all_inline_plt:1;
4200
4201 /* Set on error. */
4202 unsigned int stub_error:1;
4203
4204 /* Whether func_desc_adjust needs to be run over symbols. */
4205 unsigned int need_func_desc_adj:1;
4206
4207 /* Whether there exist local gnu indirect function resolvers,
4208 referenced by dynamic relocations. */
4209 unsigned int local_ifunc_resolver:1;
4210 unsigned int maybe_local_ifunc_resolver:1;
4211
4212 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4213 unsigned int has_plt_localentry0:1;
4214
4215 /* Incremented every time we size stubs. */
4216 unsigned int stub_iteration;
4217
4218 /* Small local sym cache. */
4219 struct sym_cache sym_cache;
4220 };
4221
4222 /* Rename some of the generic section flags to better document how they
4223 are used here. */
4224
4225 /* Nonzero if this section has TLS related relocations. */
4226 #define has_tls_reloc sec_flg0
4227
4228 /* Nonzero if this section has an old-style call to __tls_get_addr. */
4229 #define has_tls_get_addr_call sec_flg1
4230
4231 /* Nonzero if this section has any toc or got relocs. */
4232 #define has_toc_reloc sec_flg2
4233
4234 /* Nonzero if this section has a call to another section that uses
4235 the toc or got. */
4236 #define makes_toc_func_call sec_flg3
4237
4238 /* Recursion protection when determining above flag. */
4239 #define call_check_in_progress sec_flg4
4240 #define call_check_done sec_flg5
4241
4242 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4243
4244 #define ppc_hash_table(p) \
4245 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4246 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4247
4248 #define ppc_stub_hash_lookup(table, string, create, copy) \
4249 ((struct ppc_stub_hash_entry *) \
4250 bfd_hash_lookup ((table), (string), (create), (copy)))
4251
4252 #define ppc_branch_hash_lookup(table, string, create, copy) \
4253 ((struct ppc_branch_hash_entry *) \
4254 bfd_hash_lookup ((table), (string), (create), (copy)))
4255
4256 /* Create an entry in the stub hash table. */
4257
4258 static struct bfd_hash_entry *
4259 stub_hash_newfunc (struct bfd_hash_entry *entry,
4260 struct bfd_hash_table *table,
4261 const char *string)
4262 {
4263 /* Allocate the structure if it has not already been allocated by a
4264 subclass. */
4265 if (entry == NULL)
4266 {
4267 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4268 if (entry == NULL)
4269 return entry;
4270 }
4271
4272 /* Call the allocation method of the superclass. */
4273 entry = bfd_hash_newfunc (entry, table, string);
4274 if (entry != NULL)
4275 {
4276 struct ppc_stub_hash_entry *eh;
4277
4278 /* Initialize the local fields. */
4279 eh = (struct ppc_stub_hash_entry *) entry;
4280 eh->stub_type = ppc_stub_none;
4281 eh->group = NULL;
4282 eh->stub_offset = 0;
4283 eh->target_value = 0;
4284 eh->target_section = NULL;
4285 eh->h = NULL;
4286 eh->plt_ent = NULL;
4287 eh->other = 0;
4288 }
4289
4290 return entry;
4291 }
4292
4293 /* Create an entry in the branch hash table. */
4294
4295 static struct bfd_hash_entry *
4296 branch_hash_newfunc (struct bfd_hash_entry *entry,
4297 struct bfd_hash_table *table,
4298 const char *string)
4299 {
4300 /* Allocate the structure if it has not already been allocated by a
4301 subclass. */
4302 if (entry == NULL)
4303 {
4304 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4305 if (entry == NULL)
4306 return entry;
4307 }
4308
4309 /* Call the allocation method of the superclass. */
4310 entry = bfd_hash_newfunc (entry, table, string);
4311 if (entry != NULL)
4312 {
4313 struct ppc_branch_hash_entry *eh;
4314
4315 /* Initialize the local fields. */
4316 eh = (struct ppc_branch_hash_entry *) entry;
4317 eh->offset = 0;
4318 eh->iter = 0;
4319 }
4320
4321 return entry;
4322 }
4323
4324 /* Create an entry in a ppc64 ELF linker hash table. */
4325
4326 static struct bfd_hash_entry *
4327 link_hash_newfunc (struct bfd_hash_entry *entry,
4328 struct bfd_hash_table *table,
4329 const char *string)
4330 {
4331 /* Allocate the structure if it has not already been allocated by a
4332 subclass. */
4333 if (entry == NULL)
4334 {
4335 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4336 if (entry == NULL)
4337 return entry;
4338 }
4339
4340 /* Call the allocation method of the superclass. */
4341 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4342 if (entry != NULL)
4343 {
4344 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4345
4346 memset (&eh->u.stub_cache, 0,
4347 (sizeof (struct ppc_link_hash_entry)
4348 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4349
4350 /* When making function calls, old ABI code references function entry
4351 points (dot symbols), while new ABI code references the function
4352 descriptor symbol. We need to make any combination of reference and
4353 definition work together, without breaking archive linking.
4354
4355 For a defined function "foo" and an undefined call to "bar":
4356 An old object defines "foo" and ".foo", references ".bar" (possibly
4357 "bar" too).
4358 A new object defines "foo" and references "bar".
4359
4360 A new object thus has no problem with its undefined symbols being
4361 satisfied by definitions in an old object. On the other hand, the
4362 old object won't have ".bar" satisfied by a new object.
4363
4364 Keep a list of newly added dot-symbols. */
4365
4366 if (string[0] == '.')
4367 {
4368 struct ppc_link_hash_table *htab;
4369
4370 htab = (struct ppc_link_hash_table *) table;
4371 eh->u.next_dot_sym = htab->dot_syms;
4372 htab->dot_syms = eh;
4373 }
4374 }
4375
4376 return entry;
4377 }
4378
4379 struct tocsave_entry {
4380 asection *sec;
4381 bfd_vma offset;
4382 };
4383
4384 static hashval_t
4385 tocsave_htab_hash (const void *p)
4386 {
4387 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4388 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4389 }
4390
4391 static int
4392 tocsave_htab_eq (const void *p1, const void *p2)
4393 {
4394 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4395 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4396 return e1->sec == e2->sec && e1->offset == e2->offset;
4397 }
4398
4399 /* Destroy a ppc64 ELF linker hash table. */
4400
4401 static void
4402 ppc64_elf_link_hash_table_free (bfd *obfd)
4403 {
4404 struct ppc_link_hash_table *htab;
4405
4406 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4407 if (htab->tocsave_htab)
4408 htab_delete (htab->tocsave_htab);
4409 bfd_hash_table_free (&htab->branch_hash_table);
4410 bfd_hash_table_free (&htab->stub_hash_table);
4411 _bfd_elf_link_hash_table_free (obfd);
4412 }
4413
4414 /* Create a ppc64 ELF linker hash table. */
4415
4416 static struct bfd_link_hash_table *
4417 ppc64_elf_link_hash_table_create (bfd *abfd)
4418 {
4419 struct ppc_link_hash_table *htab;
4420 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4421
4422 htab = bfd_zmalloc (amt);
4423 if (htab == NULL)
4424 return NULL;
4425
4426 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4427 sizeof (struct ppc_link_hash_entry),
4428 PPC64_ELF_DATA))
4429 {
4430 free (htab);
4431 return NULL;
4432 }
4433
4434 /* Init the stub hash table too. */
4435 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4436 sizeof (struct ppc_stub_hash_entry)))
4437 {
4438 _bfd_elf_link_hash_table_free (abfd);
4439 return NULL;
4440 }
4441
4442 /* And the branch hash table. */
4443 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4444 sizeof (struct ppc_branch_hash_entry)))
4445 {
4446 bfd_hash_table_free (&htab->stub_hash_table);
4447 _bfd_elf_link_hash_table_free (abfd);
4448 return NULL;
4449 }
4450
4451 htab->tocsave_htab = htab_try_create (1024,
4452 tocsave_htab_hash,
4453 tocsave_htab_eq,
4454 NULL);
4455 if (htab->tocsave_htab == NULL)
4456 {
4457 ppc64_elf_link_hash_table_free (abfd);
4458 return NULL;
4459 }
4460 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4461
4462 /* Initializing two fields of the union is just cosmetic. We really
4463 only care about glist, but when compiled on a 32-bit host the
4464 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4465 debugger inspection of these fields look nicer. */
4466 htab->elf.init_got_refcount.refcount = 0;
4467 htab->elf.init_got_refcount.glist = NULL;
4468 htab->elf.init_plt_refcount.refcount = 0;
4469 htab->elf.init_plt_refcount.glist = NULL;
4470 htab->elf.init_got_offset.offset = 0;
4471 htab->elf.init_got_offset.glist = NULL;
4472 htab->elf.init_plt_offset.offset = 0;
4473 htab->elf.init_plt_offset.glist = NULL;
4474
4475 return &htab->elf.root;
4476 }
4477
4478 /* Create sections for linker generated code. */
4479
4480 static bfd_boolean
4481 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4482 {
4483 struct ppc_link_hash_table *htab;
4484 flagword flags;
4485
4486 htab = ppc_hash_table (info);
4487
4488 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4489 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4490 if (htab->params->save_restore_funcs)
4491 {
4492 /* Create .sfpr for code to save and restore fp regs. */
4493 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4494 flags);
4495 if (htab->sfpr == NULL
4496 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4497 return FALSE;
4498 }
4499
4500 if (bfd_link_relocatable (info))
4501 return TRUE;
4502
4503 /* Create .glink for lazy dynamic linking support. */
4504 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4505 flags);
4506 if (htab->glink == NULL
4507 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4508 return FALSE;
4509
4510 /* The part of .glink used by global entry stubs, separate so that
4511 it can be aligned appropriately without affecting htab->glink. */
4512 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4513 flags);
4514 if (htab->global_entry == NULL
4515 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4516 return FALSE;
4517
4518 if (!info->no_ld_generated_unwind_info)
4519 {
4520 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4521 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4522 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4523 ".eh_frame",
4524 flags);
4525 if (htab->glink_eh_frame == NULL
4526 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4527 return FALSE;
4528 }
4529
4530 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4531 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4532 if (htab->elf.iplt == NULL
4533 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4534 return FALSE;
4535
4536 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4537 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4538 htab->elf.irelplt
4539 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4540 if (htab->elf.irelplt == NULL
4541 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4542 return FALSE;
4543
4544 /* Create branch lookup table for plt_branch stubs. */
4545 flags = (SEC_ALLOC | SEC_LOAD
4546 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4547 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4548 flags);
4549 if (htab->brlt == NULL
4550 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4551 return FALSE;
4552
4553 /* Local plt entries, put in .branch_lt but a separate section for
4554 convenience. */
4555 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4556 flags);
4557 if (htab->pltlocal == NULL
4558 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4559 return FALSE;
4560
4561 if (!bfd_link_pic (info))
4562 return TRUE;
4563
4564 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4565 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4566 htab->relbrlt
4567 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4568 if (htab->relbrlt == NULL
4569 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4570 return FALSE;
4571
4572 htab->relpltlocal
4573 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4574 if (htab->relpltlocal == NULL
4575 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4576 return FALSE;
4577
4578 return TRUE;
4579 }
4580
4581 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4582
4583 bfd_boolean
4584 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4585 struct ppc64_elf_params *params)
4586 {
4587 struct ppc_link_hash_table *htab;
4588
4589 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4590
4591 /* Always hook our dynamic sections into the first bfd, which is the
4592 linker created stub bfd. This ensures that the GOT header is at
4593 the start of the output TOC section. */
4594 htab = ppc_hash_table (info);
4595 htab->elf.dynobj = params->stub_bfd;
4596 htab->params = params;
4597
4598 return create_linkage_sections (htab->elf.dynobj, info);
4599 }
4600
4601 /* Build a name for an entry in the stub hash table. */
4602
4603 static char *
4604 ppc_stub_name (const asection *input_section,
4605 const asection *sym_sec,
4606 const struct ppc_link_hash_entry *h,
4607 const Elf_Internal_Rela *rel)
4608 {
4609 char *stub_name;
4610 ssize_t len;
4611
4612 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4613 offsets from a sym as a branch target? In fact, we could
4614 probably assume the addend is always zero. */
4615 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4616
4617 if (h)
4618 {
4619 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4620 stub_name = bfd_malloc (len);
4621 if (stub_name == NULL)
4622 return stub_name;
4623
4624 len = sprintf (stub_name, "%08x.%s+%x",
4625 input_section->id & 0xffffffff,
4626 h->elf.root.root.string,
4627 (int) rel->r_addend & 0xffffffff);
4628 }
4629 else
4630 {
4631 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4632 stub_name = bfd_malloc (len);
4633 if (stub_name == NULL)
4634 return stub_name;
4635
4636 len = sprintf (stub_name, "%08x.%x:%x+%x",
4637 input_section->id & 0xffffffff,
4638 sym_sec->id & 0xffffffff,
4639 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4640 (int) rel->r_addend & 0xffffffff);
4641 }
4642 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4643 stub_name[len - 2] = 0;
4644 return stub_name;
4645 }
4646
4647 /* Look up an entry in the stub hash. Stub entries are cached because
4648 creating the stub name takes a bit of time. */
4649
4650 static struct ppc_stub_hash_entry *
4651 ppc_get_stub_entry (const asection *input_section,
4652 const asection *sym_sec,
4653 struct ppc_link_hash_entry *h,
4654 const Elf_Internal_Rela *rel,
4655 struct ppc_link_hash_table *htab)
4656 {
4657 struct ppc_stub_hash_entry *stub_entry;
4658 struct map_stub *group;
4659
4660 /* If this input section is part of a group of sections sharing one
4661 stub section, then use the id of the first section in the group.
4662 Stub names need to include a section id, as there may well be
4663 more than one stub used to reach say, printf, and we need to
4664 distinguish between them. */
4665 group = htab->sec_info[input_section->id].u.group;
4666 if (group == NULL)
4667 return NULL;
4668
4669 if (h != NULL && h->u.stub_cache != NULL
4670 && h->u.stub_cache->h == h
4671 && h->u.stub_cache->group == group)
4672 {
4673 stub_entry = h->u.stub_cache;
4674 }
4675 else
4676 {
4677 char *stub_name;
4678
4679 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4680 if (stub_name == NULL)
4681 return NULL;
4682
4683 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4684 stub_name, FALSE, FALSE);
4685 if (h != NULL)
4686 h->u.stub_cache = stub_entry;
4687
4688 free (stub_name);
4689 }
4690
4691 return stub_entry;
4692 }
4693
4694 /* Add a new stub entry to the stub hash. Not all fields of the new
4695 stub entry are initialised. */
4696
4697 static struct ppc_stub_hash_entry *
4698 ppc_add_stub (const char *stub_name,
4699 asection *section,
4700 struct bfd_link_info *info)
4701 {
4702 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4703 struct map_stub *group;
4704 asection *link_sec;
4705 asection *stub_sec;
4706 struct ppc_stub_hash_entry *stub_entry;
4707
4708 group = htab->sec_info[section->id].u.group;
4709 link_sec = group->link_sec;
4710 stub_sec = group->stub_sec;
4711 if (stub_sec == NULL)
4712 {
4713 size_t namelen;
4714 bfd_size_type len;
4715 char *s_name;
4716
4717 namelen = strlen (link_sec->name);
4718 len = namelen + sizeof (STUB_SUFFIX);
4719 s_name = bfd_alloc (htab->params->stub_bfd, len);
4720 if (s_name == NULL)
4721 return NULL;
4722
4723 memcpy (s_name, link_sec->name, namelen);
4724 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4725 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4726 if (stub_sec == NULL)
4727 return NULL;
4728 group->stub_sec = stub_sec;
4729 }
4730
4731 /* Enter this entry into the linker stub hash table. */
4732 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4733 TRUE, FALSE);
4734 if (stub_entry == NULL)
4735 {
4736 /* xgettext:c-format */
4737 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4738 section->owner, stub_name);
4739 return NULL;
4740 }
4741
4742 stub_entry->group = group;
4743 stub_entry->stub_offset = 0;
4744 return stub_entry;
4745 }
4746
4747 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4748 not already done. */
4749
4750 static bfd_boolean
4751 create_got_section (bfd *abfd, struct bfd_link_info *info)
4752 {
4753 asection *got, *relgot;
4754 flagword flags;
4755 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4756
4757 if (!is_ppc64_elf (abfd))
4758 return FALSE;
4759 if (htab == NULL)
4760 return FALSE;
4761
4762 if (!htab->elf.sgot
4763 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4764 return FALSE;
4765
4766 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4767 | SEC_LINKER_CREATED);
4768
4769 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4770 if (!got
4771 || !bfd_set_section_alignment (abfd, got, 3))
4772 return FALSE;
4773
4774 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4775 flags | SEC_READONLY);
4776 if (!relgot
4777 || ! bfd_set_section_alignment (abfd, relgot, 3))
4778 return FALSE;
4779
4780 ppc64_elf_tdata (abfd)->got = got;
4781 ppc64_elf_tdata (abfd)->relgot = relgot;
4782 return TRUE;
4783 }
4784
4785 /* Follow indirect and warning symbol links. */
4786
4787 static inline struct bfd_link_hash_entry *
4788 follow_link (struct bfd_link_hash_entry *h)
4789 {
4790 while (h->type == bfd_link_hash_indirect
4791 || h->type == bfd_link_hash_warning)
4792 h = h->u.i.link;
4793 return h;
4794 }
4795
4796 static inline struct elf_link_hash_entry *
4797 elf_follow_link (struct elf_link_hash_entry *h)
4798 {
4799 return (struct elf_link_hash_entry *) follow_link (&h->root);
4800 }
4801
4802 static inline struct ppc_link_hash_entry *
4803 ppc_follow_link (struct ppc_link_hash_entry *h)
4804 {
4805 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4806 }
4807
4808 /* Merge PLT info on FROM with that on TO. */
4809
4810 static void
4811 move_plt_plist (struct ppc_link_hash_entry *from,
4812 struct ppc_link_hash_entry *to)
4813 {
4814 if (from->elf.plt.plist != NULL)
4815 {
4816 if (to->elf.plt.plist != NULL)
4817 {
4818 struct plt_entry **entp;
4819 struct plt_entry *ent;
4820
4821 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4822 {
4823 struct plt_entry *dent;
4824
4825 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4826 if (dent->addend == ent->addend)
4827 {
4828 dent->plt.refcount += ent->plt.refcount;
4829 *entp = ent->next;
4830 break;
4831 }
4832 if (dent == NULL)
4833 entp = &ent->next;
4834 }
4835 *entp = to->elf.plt.plist;
4836 }
4837
4838 to->elf.plt.plist = from->elf.plt.plist;
4839 from->elf.plt.plist = NULL;
4840 }
4841 }
4842
4843 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4844
4845 static void
4846 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4847 struct elf_link_hash_entry *dir,
4848 struct elf_link_hash_entry *ind)
4849 {
4850 struct ppc_link_hash_entry *edir, *eind;
4851
4852 edir = (struct ppc_link_hash_entry *) dir;
4853 eind = (struct ppc_link_hash_entry *) ind;
4854
4855 edir->is_func |= eind->is_func;
4856 edir->is_func_descriptor |= eind->is_func_descriptor;
4857 edir->tls_mask |= eind->tls_mask;
4858 if (eind->oh != NULL)
4859 edir->oh = ppc_follow_link (eind->oh);
4860
4861 if (edir->elf.versioned != versioned_hidden)
4862 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4863 edir->elf.ref_regular |= eind->elf.ref_regular;
4864 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4865 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4866 edir->elf.needs_plt |= eind->elf.needs_plt;
4867 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4868
4869 /* If we were called to copy over info for a weak sym, don't copy
4870 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4871 in order to simplify readonly_dynrelocs and save a field in the
4872 symbol hash entry, but that means dyn_relocs can't be used in any
4873 tests about a specific symbol, or affect other symbol flags which
4874 are then tested. */
4875 if (eind->elf.root.type != bfd_link_hash_indirect)
4876 return;
4877
4878 /* Copy over any dynamic relocs we may have on the indirect sym. */
4879 if (eind->dyn_relocs != NULL)
4880 {
4881 if (edir->dyn_relocs != NULL)
4882 {
4883 struct elf_dyn_relocs **pp;
4884 struct elf_dyn_relocs *p;
4885
4886 /* Add reloc counts against the indirect sym to the direct sym
4887 list. Merge any entries against the same section. */
4888 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4889 {
4890 struct elf_dyn_relocs *q;
4891
4892 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4893 if (q->sec == p->sec)
4894 {
4895 q->pc_count += p->pc_count;
4896 q->count += p->count;
4897 *pp = p->next;
4898 break;
4899 }
4900 if (q == NULL)
4901 pp = &p->next;
4902 }
4903 *pp = edir->dyn_relocs;
4904 }
4905
4906 edir->dyn_relocs = eind->dyn_relocs;
4907 eind->dyn_relocs = NULL;
4908 }
4909
4910 /* Copy over got entries that we may have already seen to the
4911 symbol which just became indirect. */
4912 if (eind->elf.got.glist != NULL)
4913 {
4914 if (edir->elf.got.glist != NULL)
4915 {
4916 struct got_entry **entp;
4917 struct got_entry *ent;
4918
4919 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4920 {
4921 struct got_entry *dent;
4922
4923 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4924 if (dent->addend == ent->addend
4925 && dent->owner == ent->owner
4926 && dent->tls_type == ent->tls_type)
4927 {
4928 dent->got.refcount += ent->got.refcount;
4929 *entp = ent->next;
4930 break;
4931 }
4932 if (dent == NULL)
4933 entp = &ent->next;
4934 }
4935 *entp = edir->elf.got.glist;
4936 }
4937
4938 edir->elf.got.glist = eind->elf.got.glist;
4939 eind->elf.got.glist = NULL;
4940 }
4941
4942 /* And plt entries. */
4943 move_plt_plist (eind, edir);
4944
4945 if (eind->elf.dynindx != -1)
4946 {
4947 if (edir->elf.dynindx != -1)
4948 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4949 edir->elf.dynstr_index);
4950 edir->elf.dynindx = eind->elf.dynindx;
4951 edir->elf.dynstr_index = eind->elf.dynstr_index;
4952 eind->elf.dynindx = -1;
4953 eind->elf.dynstr_index = 0;
4954 }
4955 }
4956
4957 /* Find the function descriptor hash entry from the given function code
4958 hash entry FH. Link the entries via their OH fields. */
4959
4960 static struct ppc_link_hash_entry *
4961 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4962 {
4963 struct ppc_link_hash_entry *fdh = fh->oh;
4964
4965 if (fdh == NULL)
4966 {
4967 const char *fd_name = fh->elf.root.root.string + 1;
4968
4969 fdh = (struct ppc_link_hash_entry *)
4970 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4971 if (fdh == NULL)
4972 return fdh;
4973
4974 fdh->is_func_descriptor = 1;
4975 fdh->oh = fh;
4976 fh->is_func = 1;
4977 fh->oh = fdh;
4978 }
4979
4980 fdh = ppc_follow_link (fdh);
4981 fdh->is_func_descriptor = 1;
4982 fdh->oh = fh;
4983 return fdh;
4984 }
4985
4986 /* Make a fake function descriptor sym for the undefined code sym FH. */
4987
4988 static struct ppc_link_hash_entry *
4989 make_fdh (struct bfd_link_info *info,
4990 struct ppc_link_hash_entry *fh)
4991 {
4992 bfd *abfd = fh->elf.root.u.undef.abfd;
4993 struct bfd_link_hash_entry *bh = NULL;
4994 struct ppc_link_hash_entry *fdh;
4995 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4996 ? BSF_WEAK
4997 : BSF_GLOBAL);
4998
4999 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5000 fh->elf.root.root.string + 1,
5001 flags, bfd_und_section_ptr, 0,
5002 NULL, FALSE, FALSE, &bh))
5003 return NULL;
5004
5005 fdh = (struct ppc_link_hash_entry *) bh;
5006 fdh->elf.non_elf = 0;
5007 fdh->fake = 1;
5008 fdh->is_func_descriptor = 1;
5009 fdh->oh = fh;
5010 fh->is_func = 1;
5011 fh->oh = fdh;
5012 return fdh;
5013 }
5014
5015 /* Fix function descriptor symbols defined in .opd sections to be
5016 function type. */
5017
5018 static bfd_boolean
5019 ppc64_elf_add_symbol_hook (bfd *ibfd,
5020 struct bfd_link_info *info,
5021 Elf_Internal_Sym *isym,
5022 const char **name,
5023 flagword *flags ATTRIBUTE_UNUSED,
5024 asection **sec,
5025 bfd_vma *value)
5026 {
5027 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5028 && (ibfd->flags & DYNAMIC) == 0
5029 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
5030 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
5031
5032 if (*sec != NULL
5033 && strcmp ((*sec)->name, ".opd") == 0)
5034 {
5035 asection *code_sec;
5036
5037 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5038 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5039 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5040
5041 /* If the symbol is a function defined in .opd, and the function
5042 code is in a discarded group, let it appear to be undefined. */
5043 if (!bfd_link_relocatable (info)
5044 && (*sec)->reloc_count != 0
5045 && opd_entry_value (*sec, *value, &code_sec, NULL,
5046 FALSE) != (bfd_vma) -1
5047 && discarded_section (code_sec))
5048 {
5049 *sec = bfd_und_section_ptr;
5050 isym->st_shndx = SHN_UNDEF;
5051 }
5052 }
5053 else if (*sec != NULL
5054 && strcmp ((*sec)->name, ".toc") == 0
5055 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5056 {
5057 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5058 if (htab != NULL)
5059 htab->params->object_in_toc = 1;
5060 }
5061
5062 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5063 {
5064 if (abiversion (ibfd) == 0)
5065 set_abiversion (ibfd, 2);
5066 else if (abiversion (ibfd) == 1)
5067 {
5068 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5069 " for ABI version 1"), *name);
5070 bfd_set_error (bfd_error_bad_value);
5071 return FALSE;
5072 }
5073 }
5074
5075 return TRUE;
5076 }
5077
5078 /* Merge non-visibility st_other attributes: local entry point. */
5079
5080 static void
5081 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5082 const Elf_Internal_Sym *isym,
5083 bfd_boolean definition,
5084 bfd_boolean dynamic)
5085 {
5086 if (definition && (!dynamic || !h->def_regular))
5087 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5088 | ELF_ST_VISIBILITY (h->other));
5089 }
5090
5091 /* Hook called on merging a symbol. We use this to clear "fake" since
5092 we now have a real symbol. */
5093
5094 static bfd_boolean
5095 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5096 const Elf_Internal_Sym *isym,
5097 asection **psec ATTRIBUTE_UNUSED,
5098 bfd_boolean newdef ATTRIBUTE_UNUSED,
5099 bfd_boolean olddef ATTRIBUTE_UNUSED,
5100 bfd *oldbfd ATTRIBUTE_UNUSED,
5101 const asection *oldsec ATTRIBUTE_UNUSED)
5102 {
5103 ((struct ppc_link_hash_entry *) h)->fake = 0;
5104 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5105 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5106 return TRUE;
5107 }
5108
5109 /* This function makes an old ABI object reference to ".bar" cause the
5110 inclusion of a new ABI object archive that defines "bar".
5111 NAME is a symbol defined in an archive. Return a symbol in the hash
5112 table that might be satisfied by the archive symbols. */
5113
5114 static struct elf_link_hash_entry *
5115 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5116 struct bfd_link_info *info,
5117 const char *name)
5118 {
5119 struct elf_link_hash_entry *h;
5120 char *dot_name;
5121 size_t len;
5122
5123 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5124 if (h != NULL
5125 /* Don't return this sym if it is a fake function descriptor
5126 created by add_symbol_adjust. */
5127 && !((struct ppc_link_hash_entry *) h)->fake)
5128 return h;
5129
5130 if (name[0] == '.')
5131 return h;
5132
5133 len = strlen (name);
5134 dot_name = bfd_alloc (abfd, len + 2);
5135 if (dot_name == NULL)
5136 return (struct elf_link_hash_entry *) -1;
5137 dot_name[0] = '.';
5138 memcpy (dot_name + 1, name, len + 1);
5139 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5140 bfd_release (abfd, dot_name);
5141 return h;
5142 }
5143
5144 /* This function satisfies all old ABI object references to ".bar" if a
5145 new ABI object defines "bar". Well, at least, undefined dot symbols
5146 are made weak. This stops later archive searches from including an
5147 object if we already have a function descriptor definition. It also
5148 prevents the linker complaining about undefined symbols.
5149 We also check and correct mismatched symbol visibility here. The
5150 most restrictive visibility of the function descriptor and the
5151 function entry symbol is used. */
5152
5153 static bfd_boolean
5154 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5155 {
5156 struct ppc_link_hash_table *htab;
5157 struct ppc_link_hash_entry *fdh;
5158
5159 if (eh->elf.root.type == bfd_link_hash_warning)
5160 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5161
5162 if (eh->elf.root.type == bfd_link_hash_indirect)
5163 return TRUE;
5164
5165 if (eh->elf.root.root.string[0] != '.')
5166 abort ();
5167
5168 htab = ppc_hash_table (info);
5169 if (htab == NULL)
5170 return FALSE;
5171
5172 fdh = lookup_fdh (eh, htab);
5173 if (fdh == NULL
5174 && !bfd_link_relocatable (info)
5175 && (eh->elf.root.type == bfd_link_hash_undefined
5176 || eh->elf.root.type == bfd_link_hash_undefweak)
5177 && eh->elf.ref_regular)
5178 {
5179 /* Make an undefined function descriptor sym, in order to
5180 pull in an --as-needed shared lib. Archives are handled
5181 elsewhere. */
5182 fdh = make_fdh (info, eh);
5183 if (fdh == NULL)
5184 return FALSE;
5185 }
5186
5187 if (fdh != NULL)
5188 {
5189 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5190 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5191
5192 /* Make both descriptor and entry symbol have the most
5193 constraining visibility of either symbol. */
5194 if (entry_vis < descr_vis)
5195 fdh->elf.other += entry_vis - descr_vis;
5196 else if (entry_vis > descr_vis)
5197 eh->elf.other += descr_vis - entry_vis;
5198
5199 /* Propagate reference flags from entry symbol to function
5200 descriptor symbol. */
5201 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5202 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5203 fdh->elf.ref_regular |= eh->elf.ref_regular;
5204 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5205
5206 if (!fdh->elf.forced_local
5207 && fdh->elf.dynindx == -1
5208 && fdh->elf.versioned != versioned_hidden
5209 && (bfd_link_dll (info)
5210 || fdh->elf.def_dynamic
5211 || fdh->elf.ref_dynamic)
5212 && (eh->elf.ref_regular
5213 || eh->elf.def_regular))
5214 {
5215 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5216 return FALSE;
5217 }
5218 }
5219
5220 return TRUE;
5221 }
5222
5223 /* Set up opd section info and abiversion for IBFD, and process list
5224 of dot-symbols we made in link_hash_newfunc. */
5225
5226 static bfd_boolean
5227 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5228 {
5229 struct ppc_link_hash_table *htab;
5230 struct ppc_link_hash_entry **p, *eh;
5231 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5232
5233 if (opd != NULL && opd->size != 0)
5234 {
5235 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5236 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5237
5238 if (abiversion (ibfd) == 0)
5239 set_abiversion (ibfd, 1);
5240 else if (abiversion (ibfd) >= 2)
5241 {
5242 /* xgettext:c-format */
5243 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5244 ibfd, abiversion (ibfd));
5245 bfd_set_error (bfd_error_bad_value);
5246 return FALSE;
5247 }
5248 }
5249
5250 if (is_ppc64_elf (info->output_bfd))
5251 {
5252 /* For input files without an explicit abiversion in e_flags
5253 we should have flagged any with symbol st_other bits set
5254 as ELFv1 and above flagged those with .opd as ELFv2.
5255 Set the output abiversion if not yet set, and for any input
5256 still ambiguous, take its abiversion from the output.
5257 Differences in ABI are reported later. */
5258 if (abiversion (info->output_bfd) == 0)
5259 set_abiversion (info->output_bfd, abiversion (ibfd));
5260 else if (abiversion (ibfd) == 0)
5261 set_abiversion (ibfd, abiversion (info->output_bfd));
5262 }
5263
5264 htab = ppc_hash_table (info);
5265 if (htab == NULL)
5266 return TRUE;
5267
5268 if (opd != NULL && opd->size != 0
5269 && (ibfd->flags & DYNAMIC) == 0
5270 && (opd->flags & SEC_RELOC) != 0
5271 && opd->reloc_count != 0
5272 && !bfd_is_abs_section (opd->output_section)
5273 && info->gc_sections)
5274 {
5275 /* Garbage collection needs some extra help with .opd sections.
5276 We don't want to necessarily keep everything referenced by
5277 relocs in .opd, as that would keep all functions. Instead,
5278 if we reference an .opd symbol (a function descriptor), we
5279 want to keep the function code symbol's section. This is
5280 easy for global symbols, but for local syms we need to keep
5281 information about the associated function section. */
5282 bfd_size_type amt;
5283 asection **opd_sym_map;
5284 Elf_Internal_Shdr *symtab_hdr;
5285 Elf_Internal_Rela *relocs, *rel_end, *rel;
5286
5287 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5288 opd_sym_map = bfd_zalloc (ibfd, amt);
5289 if (opd_sym_map == NULL)
5290 return FALSE;
5291 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5292 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5293 info->keep_memory);
5294 if (relocs == NULL)
5295 return FALSE;
5296 symtab_hdr = &elf_symtab_hdr (ibfd);
5297 rel_end = relocs + opd->reloc_count - 1;
5298 for (rel = relocs; rel < rel_end; rel++)
5299 {
5300 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5301 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5302
5303 if (r_type == R_PPC64_ADDR64
5304 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5305 && r_symndx < symtab_hdr->sh_info)
5306 {
5307 Elf_Internal_Sym *isym;
5308 asection *s;
5309
5310 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5311 if (isym == NULL)
5312 {
5313 if (elf_section_data (opd)->relocs != relocs)
5314 free (relocs);
5315 return FALSE;
5316 }
5317
5318 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5319 if (s != NULL && s != opd)
5320 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5321 }
5322 }
5323 if (elf_section_data (opd)->relocs != relocs)
5324 free (relocs);
5325 }
5326
5327 p = &htab->dot_syms;
5328 while ((eh = *p) != NULL)
5329 {
5330 *p = NULL;
5331 if (&eh->elf == htab->elf.hgot)
5332 ;
5333 else if (htab->elf.hgot == NULL
5334 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5335 htab->elf.hgot = &eh->elf;
5336 else if (abiversion (ibfd) <= 1)
5337 {
5338 htab->need_func_desc_adj = 1;
5339 if (!add_symbol_adjust (eh, info))
5340 return FALSE;
5341 }
5342 p = &eh->u.next_dot_sym;
5343 }
5344 return TRUE;
5345 }
5346
5347 /* Undo hash table changes when an --as-needed input file is determined
5348 not to be needed. */
5349
5350 static bfd_boolean
5351 ppc64_elf_notice_as_needed (bfd *ibfd,
5352 struct bfd_link_info *info,
5353 enum notice_asneeded_action act)
5354 {
5355 if (act == notice_not_needed)
5356 {
5357 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5358
5359 if (htab == NULL)
5360 return FALSE;
5361
5362 htab->dot_syms = NULL;
5363 }
5364 return _bfd_elf_notice_as_needed (ibfd, info, act);
5365 }
5366
5367 /* If --just-symbols against a final linked binary, then assume we need
5368 toc adjusting stubs when calling functions defined there. */
5369
5370 static void
5371 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5372 {
5373 if ((sec->flags & SEC_CODE) != 0
5374 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5375 && is_ppc64_elf (sec->owner))
5376 {
5377 if (abiversion (sec->owner) >= 2
5378 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5379 sec->has_toc_reloc = 1;
5380 }
5381 _bfd_elf_link_just_syms (sec, info);
5382 }
5383
5384 static struct plt_entry **
5385 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5386 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5387 {
5388 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5389 struct plt_entry **local_plt;
5390 unsigned char *local_got_tls_masks;
5391
5392 if (local_got_ents == NULL)
5393 {
5394 bfd_size_type size = symtab_hdr->sh_info;
5395
5396 size *= (sizeof (*local_got_ents)
5397 + sizeof (*local_plt)
5398 + sizeof (*local_got_tls_masks));
5399 local_got_ents = bfd_zalloc (abfd, size);
5400 if (local_got_ents == NULL)
5401 return NULL;
5402 elf_local_got_ents (abfd) = local_got_ents;
5403 }
5404
5405 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5406 {
5407 struct got_entry *ent;
5408
5409 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5410 if (ent->addend == r_addend
5411 && ent->owner == abfd
5412 && ent->tls_type == tls_type)
5413 break;
5414 if (ent == NULL)
5415 {
5416 bfd_size_type amt = sizeof (*ent);
5417 ent = bfd_alloc (abfd, amt);
5418 if (ent == NULL)
5419 return FALSE;
5420 ent->next = local_got_ents[r_symndx];
5421 ent->addend = r_addend;
5422 ent->owner = abfd;
5423 ent->tls_type = tls_type;
5424 ent->is_indirect = FALSE;
5425 ent->got.refcount = 0;
5426 local_got_ents[r_symndx] = ent;
5427 }
5428 ent->got.refcount += 1;
5429 }
5430
5431 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5432 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5433 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5434
5435 return local_plt + r_symndx;
5436 }
5437
5438 static bfd_boolean
5439 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5440 {
5441 struct plt_entry *ent;
5442
5443 for (ent = *plist; ent != NULL; ent = ent->next)
5444 if (ent->addend == addend)
5445 break;
5446 if (ent == NULL)
5447 {
5448 bfd_size_type amt = sizeof (*ent);
5449 ent = bfd_alloc (abfd, amt);
5450 if (ent == NULL)
5451 return FALSE;
5452 ent->next = *plist;
5453 ent->addend = addend;
5454 ent->plt.refcount = 0;
5455 *plist = ent;
5456 }
5457 ent->plt.refcount += 1;
5458 return TRUE;
5459 }
5460
5461 static bfd_boolean
5462 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5463 {
5464 return (r_type == R_PPC64_REL24
5465 || r_type == R_PPC64_REL14
5466 || r_type == R_PPC64_REL14_BRTAKEN
5467 || r_type == R_PPC64_REL14_BRNTAKEN
5468 || r_type == R_PPC64_ADDR24
5469 || r_type == R_PPC64_ADDR14
5470 || r_type == R_PPC64_ADDR14_BRTAKEN
5471 || r_type == R_PPC64_ADDR14_BRNTAKEN
5472 || r_type == R_PPC64_PLTCALL);
5473 }
5474
5475 /* Relocs on inline plt call sequence insns prior to the call. */
5476
5477 static bfd_boolean
5478 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5479 {
5480 return (r_type == R_PPC64_PLT16_HA
5481 || r_type == R_PPC64_PLT16_HI
5482 || r_type == R_PPC64_PLT16_LO
5483 || r_type == R_PPC64_PLT16_LO_DS
5484 || r_type == R_PPC64_PLTSEQ);
5485 }
5486
5487 /* Look through the relocs for a section during the first phase, and
5488 calculate needed space in the global offset table, procedure
5489 linkage table, and dynamic reloc sections. */
5490
5491 static bfd_boolean
5492 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5493 asection *sec, const Elf_Internal_Rela *relocs)
5494 {
5495 struct ppc_link_hash_table *htab;
5496 Elf_Internal_Shdr *symtab_hdr;
5497 struct elf_link_hash_entry **sym_hashes;
5498 const Elf_Internal_Rela *rel;
5499 const Elf_Internal_Rela *rel_end;
5500 asection *sreloc;
5501 struct elf_link_hash_entry *tga, *dottga;
5502 bfd_boolean is_opd;
5503
5504 if (bfd_link_relocatable (info))
5505 return TRUE;
5506
5507 /* Don't do anything special with non-loaded, non-alloced sections.
5508 In particular, any relocs in such sections should not affect GOT
5509 and PLT reference counting (ie. we don't allow them to create GOT
5510 or PLT entries), there's no possibility or desire to optimize TLS
5511 relocs, and there's not much point in propagating relocs to shared
5512 libs that the dynamic linker won't relocate. */
5513 if ((sec->flags & SEC_ALLOC) == 0)
5514 return TRUE;
5515
5516 BFD_ASSERT (is_ppc64_elf (abfd));
5517
5518 htab = ppc_hash_table (info);
5519 if (htab == NULL)
5520 return FALSE;
5521
5522 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5523 FALSE, FALSE, TRUE);
5524 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5525 FALSE, FALSE, TRUE);
5526 symtab_hdr = &elf_symtab_hdr (abfd);
5527 sym_hashes = elf_sym_hashes (abfd);
5528 sreloc = NULL;
5529 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5530 rel_end = relocs + sec->reloc_count;
5531 for (rel = relocs; rel < rel_end; rel++)
5532 {
5533 unsigned long r_symndx;
5534 struct elf_link_hash_entry *h;
5535 enum elf_ppc64_reloc_type r_type;
5536 int tls_type;
5537 struct _ppc64_elf_section_data *ppc64_sec;
5538 struct plt_entry **ifunc, **plt_list;
5539
5540 r_symndx = ELF64_R_SYM (rel->r_info);
5541 if (r_symndx < symtab_hdr->sh_info)
5542 h = NULL;
5543 else
5544 {
5545 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5546 h = elf_follow_link (h);
5547
5548 if (h == htab->elf.hgot)
5549 sec->has_toc_reloc = 1;
5550 }
5551
5552 tls_type = 0;
5553 ifunc = NULL;
5554 if (h != NULL)
5555 {
5556 if (h->type == STT_GNU_IFUNC)
5557 {
5558 h->needs_plt = 1;
5559 ifunc = &h->plt.plist;
5560 }
5561 }
5562 else
5563 {
5564 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5565 abfd, r_symndx);
5566 if (isym == NULL)
5567 return FALSE;
5568
5569 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5570 {
5571 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5572 rel->r_addend,
5573 NON_GOT | PLT_IFUNC);
5574 if (ifunc == NULL)
5575 return FALSE;
5576 }
5577 }
5578
5579 r_type = ELF64_R_TYPE (rel->r_info);
5580 switch (r_type)
5581 {
5582 case R_PPC64_TLSGD:
5583 case R_PPC64_TLSLD:
5584 /* These special tls relocs tie a call to __tls_get_addr with
5585 its parameter symbol. */
5586 if (h != NULL)
5587 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5588 else
5589 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5590 rel->r_addend,
5591 NON_GOT | TLS_TLS | TLS_MARK))
5592 return FALSE;
5593 sec->has_tls_reloc = 1;
5594 break;
5595
5596 case R_PPC64_GOT_TLSLD16:
5597 case R_PPC64_GOT_TLSLD16_LO:
5598 case R_PPC64_GOT_TLSLD16_HI:
5599 case R_PPC64_GOT_TLSLD16_HA:
5600 tls_type = TLS_TLS | TLS_LD;
5601 goto dogottls;
5602
5603 case R_PPC64_GOT_TLSGD16:
5604 case R_PPC64_GOT_TLSGD16_LO:
5605 case R_PPC64_GOT_TLSGD16_HI:
5606 case R_PPC64_GOT_TLSGD16_HA:
5607 tls_type = TLS_TLS | TLS_GD;
5608 goto dogottls;
5609
5610 case R_PPC64_GOT_TPREL16_DS:
5611 case R_PPC64_GOT_TPREL16_LO_DS:
5612 case R_PPC64_GOT_TPREL16_HI:
5613 case R_PPC64_GOT_TPREL16_HA:
5614 if (bfd_link_dll (info))
5615 info->flags |= DF_STATIC_TLS;
5616 tls_type = TLS_TLS | TLS_TPREL;
5617 goto dogottls;
5618
5619 case R_PPC64_GOT_DTPREL16_DS:
5620 case R_PPC64_GOT_DTPREL16_LO_DS:
5621 case R_PPC64_GOT_DTPREL16_HI:
5622 case R_PPC64_GOT_DTPREL16_HA:
5623 tls_type = TLS_TLS | TLS_DTPREL;
5624 dogottls:
5625 sec->has_tls_reloc = 1;
5626 /* Fall through */
5627
5628 case R_PPC64_GOT16:
5629 case R_PPC64_GOT16_DS:
5630 case R_PPC64_GOT16_HA:
5631 case R_PPC64_GOT16_HI:
5632 case R_PPC64_GOT16_LO:
5633 case R_PPC64_GOT16_LO_DS:
5634 /* This symbol requires a global offset table entry. */
5635 sec->has_toc_reloc = 1;
5636 if (r_type == R_PPC64_GOT_TLSLD16
5637 || r_type == R_PPC64_GOT_TLSGD16
5638 || r_type == R_PPC64_GOT_TPREL16_DS
5639 || r_type == R_PPC64_GOT_DTPREL16_DS
5640 || r_type == R_PPC64_GOT16
5641 || r_type == R_PPC64_GOT16_DS)
5642 {
5643 htab->do_multi_toc = 1;
5644 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5645 }
5646
5647 if (ppc64_elf_tdata (abfd)->got == NULL
5648 && !create_got_section (abfd, info))
5649 return FALSE;
5650
5651 if (h != NULL)
5652 {
5653 struct ppc_link_hash_entry *eh;
5654 struct got_entry *ent;
5655
5656 eh = (struct ppc_link_hash_entry *) h;
5657 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5658 if (ent->addend == rel->r_addend
5659 && ent->owner == abfd
5660 && ent->tls_type == tls_type)
5661 break;
5662 if (ent == NULL)
5663 {
5664 bfd_size_type amt = sizeof (*ent);
5665 ent = bfd_alloc (abfd, amt);
5666 if (ent == NULL)
5667 return FALSE;
5668 ent->next = eh->elf.got.glist;
5669 ent->addend = rel->r_addend;
5670 ent->owner = abfd;
5671 ent->tls_type = tls_type;
5672 ent->is_indirect = FALSE;
5673 ent->got.refcount = 0;
5674 eh->elf.got.glist = ent;
5675 }
5676 ent->got.refcount += 1;
5677 eh->tls_mask |= tls_type;
5678 }
5679 else
5680 /* This is a global offset table entry for a local symbol. */
5681 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5682 rel->r_addend, tls_type))
5683 return FALSE;
5684
5685 /* We may also need a plt entry if the symbol turns out to be
5686 an ifunc. */
5687 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5688 {
5689 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5690 return FALSE;
5691 }
5692 break;
5693
5694 case R_PPC64_PLT16_HA:
5695 case R_PPC64_PLT16_HI:
5696 case R_PPC64_PLT16_LO:
5697 case R_PPC64_PLT16_LO_DS:
5698 case R_PPC64_PLT32:
5699 case R_PPC64_PLT64:
5700 /* This symbol requires a procedure linkage table entry. */
5701 plt_list = ifunc;
5702 if (h != NULL)
5703 {
5704 h->needs_plt = 1;
5705 if (h->root.root.string[0] == '.'
5706 && h->root.root.string[1] != '\0')
5707 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5708 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5709 plt_list = &h->plt.plist;
5710 }
5711 if (plt_list == NULL)
5712 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5713 rel->r_addend,
5714 NON_GOT | PLT_KEEP);
5715 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5716 return FALSE;
5717 break;
5718
5719 /* The following relocations don't need to propagate the
5720 relocation if linking a shared object since they are
5721 section relative. */
5722 case R_PPC64_SECTOFF:
5723 case R_PPC64_SECTOFF_LO:
5724 case R_PPC64_SECTOFF_HI:
5725 case R_PPC64_SECTOFF_HA:
5726 case R_PPC64_SECTOFF_DS:
5727 case R_PPC64_SECTOFF_LO_DS:
5728 case R_PPC64_DTPREL16:
5729 case R_PPC64_DTPREL16_LO:
5730 case R_PPC64_DTPREL16_HI:
5731 case R_PPC64_DTPREL16_HA:
5732 case R_PPC64_DTPREL16_DS:
5733 case R_PPC64_DTPREL16_LO_DS:
5734 case R_PPC64_DTPREL16_HIGH:
5735 case R_PPC64_DTPREL16_HIGHA:
5736 case R_PPC64_DTPREL16_HIGHER:
5737 case R_PPC64_DTPREL16_HIGHERA:
5738 case R_PPC64_DTPREL16_HIGHEST:
5739 case R_PPC64_DTPREL16_HIGHESTA:
5740 break;
5741
5742 /* Nor do these. */
5743 case R_PPC64_REL16:
5744 case R_PPC64_REL16_LO:
5745 case R_PPC64_REL16_HI:
5746 case R_PPC64_REL16_HA:
5747 case R_PPC64_REL16DX_HA:
5748 break;
5749
5750 /* Not supported as a dynamic relocation. */
5751 case R_PPC64_ADDR64_LOCAL:
5752 if (bfd_link_pic (info))
5753 {
5754 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5755 ppc_howto_init ();
5756 /* xgettext:c-format */
5757 info->callbacks->einfo (_("%H: %s reloc unsupported "
5758 "in shared libraries and PIEs\n"),
5759 abfd, sec, rel->r_offset,
5760 ppc64_elf_howto_table[r_type]->name);
5761 bfd_set_error (bfd_error_bad_value);
5762 return FALSE;
5763 }
5764 break;
5765
5766 case R_PPC64_TOC16:
5767 case R_PPC64_TOC16_DS:
5768 htab->do_multi_toc = 1;
5769 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5770 /* Fall through. */
5771 case R_PPC64_TOC16_LO:
5772 case R_PPC64_TOC16_HI:
5773 case R_PPC64_TOC16_HA:
5774 case R_PPC64_TOC16_LO_DS:
5775 sec->has_toc_reloc = 1;
5776 break;
5777
5778 /* Marker reloc. */
5779 case R_PPC64_ENTRY:
5780 break;
5781
5782 /* This relocation describes the C++ object vtable hierarchy.
5783 Reconstruct it for later use during GC. */
5784 case R_PPC64_GNU_VTINHERIT:
5785 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5786 return FALSE;
5787 break;
5788
5789 /* This relocation describes which C++ vtable entries are actually
5790 used. Record for later use during GC. */
5791 case R_PPC64_GNU_VTENTRY:
5792 BFD_ASSERT (h != NULL);
5793 if (h != NULL
5794 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5795 return FALSE;
5796 break;
5797
5798 case R_PPC64_REL14:
5799 case R_PPC64_REL14_BRTAKEN:
5800 case R_PPC64_REL14_BRNTAKEN:
5801 {
5802 asection *dest = NULL;
5803
5804 /* Heuristic: If jumping outside our section, chances are
5805 we are going to need a stub. */
5806 if (h != NULL)
5807 {
5808 /* If the sym is weak it may be overridden later, so
5809 don't assume we know where a weak sym lives. */
5810 if (h->root.type == bfd_link_hash_defined)
5811 dest = h->root.u.def.section;
5812 }
5813 else
5814 {
5815 Elf_Internal_Sym *isym;
5816
5817 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5818 abfd, r_symndx);
5819 if (isym == NULL)
5820 return FALSE;
5821
5822 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5823 }
5824
5825 if (dest != sec)
5826 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5827 }
5828 goto rel24;
5829
5830 case R_PPC64_PLTCALL:
5831 ppc64_elf_section_data (sec)->has_pltcall = 1;
5832 /* Fall through. */
5833
5834 case R_PPC64_REL24:
5835 rel24:
5836 plt_list = ifunc;
5837 if (h != NULL)
5838 {
5839 h->needs_plt = 1;
5840 if (h->root.root.string[0] == '.'
5841 && h->root.root.string[1] != '\0')
5842 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5843
5844 if (h == tga || h == dottga)
5845 {
5846 sec->has_tls_reloc = 1;
5847 if (rel != relocs
5848 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5849 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5850 /* We have a new-style __tls_get_addr call with
5851 a marker reloc. */
5852 ;
5853 else
5854 /* Mark this section as having an old-style call. */
5855 sec->has_tls_get_addr_call = 1;
5856 }
5857 plt_list = &h->plt.plist;
5858 }
5859
5860 /* We may need a .plt entry if the function this reloc
5861 refers to is in a shared lib. */
5862 if (plt_list
5863 && !update_plt_info (abfd, plt_list, rel->r_addend))
5864 return FALSE;
5865 break;
5866
5867 case R_PPC64_ADDR14:
5868 case R_PPC64_ADDR14_BRNTAKEN:
5869 case R_PPC64_ADDR14_BRTAKEN:
5870 case R_PPC64_ADDR24:
5871 goto dodyn;
5872
5873 case R_PPC64_TPREL64:
5874 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5875 if (bfd_link_dll (info))
5876 info->flags |= DF_STATIC_TLS;
5877 goto dotlstoc;
5878
5879 case R_PPC64_DTPMOD64:
5880 if (rel + 1 < rel_end
5881 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5882 && rel[1].r_offset == rel->r_offset + 8)
5883 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5884 else
5885 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5886 goto dotlstoc;
5887
5888 case R_PPC64_DTPREL64:
5889 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5890 if (rel != relocs
5891 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5892 && rel[-1].r_offset == rel->r_offset - 8)
5893 /* This is the second reloc of a dtpmod, dtprel pair.
5894 Don't mark with TLS_DTPREL. */
5895 goto dodyn;
5896
5897 dotlstoc:
5898 sec->has_tls_reloc = 1;
5899 if (h != NULL)
5900 {
5901 struct ppc_link_hash_entry *eh;
5902 eh = (struct ppc_link_hash_entry *) h;
5903 eh->tls_mask |= tls_type;
5904 }
5905 else
5906 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5907 rel->r_addend, tls_type))
5908 return FALSE;
5909
5910 ppc64_sec = ppc64_elf_section_data (sec);
5911 if (ppc64_sec->sec_type != sec_toc)
5912 {
5913 bfd_size_type amt;
5914
5915 /* One extra to simplify get_tls_mask. */
5916 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5917 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5918 if (ppc64_sec->u.toc.symndx == NULL)
5919 return FALSE;
5920 amt = sec->size * sizeof (bfd_vma) / 8;
5921 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5922 if (ppc64_sec->u.toc.add == NULL)
5923 return FALSE;
5924 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5925 ppc64_sec->sec_type = sec_toc;
5926 }
5927 BFD_ASSERT (rel->r_offset % 8 == 0);
5928 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5929 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5930
5931 /* Mark the second slot of a GD or LD entry.
5932 -1 to indicate GD and -2 to indicate LD. */
5933 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5934 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5935 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5936 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5937 goto dodyn;
5938
5939 case R_PPC64_TPREL16:
5940 case R_PPC64_TPREL16_LO:
5941 case R_PPC64_TPREL16_HI:
5942 case R_PPC64_TPREL16_HA:
5943 case R_PPC64_TPREL16_DS:
5944 case R_PPC64_TPREL16_LO_DS:
5945 case R_PPC64_TPREL16_HIGH:
5946 case R_PPC64_TPREL16_HIGHA:
5947 case R_PPC64_TPREL16_HIGHER:
5948 case R_PPC64_TPREL16_HIGHERA:
5949 case R_PPC64_TPREL16_HIGHEST:
5950 case R_PPC64_TPREL16_HIGHESTA:
5951 if (bfd_link_dll (info))
5952 info->flags |= DF_STATIC_TLS;
5953 goto dodyn;
5954
5955 case R_PPC64_ADDR64:
5956 if (is_opd
5957 && rel + 1 < rel_end
5958 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5959 {
5960 if (h != NULL)
5961 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5962 }
5963 /* Fall through. */
5964
5965 case R_PPC64_ADDR16:
5966 case R_PPC64_ADDR16_DS:
5967 case R_PPC64_ADDR16_HA:
5968 case R_PPC64_ADDR16_HI:
5969 case R_PPC64_ADDR16_HIGH:
5970 case R_PPC64_ADDR16_HIGHA:
5971 case R_PPC64_ADDR16_HIGHER:
5972 case R_PPC64_ADDR16_HIGHERA:
5973 case R_PPC64_ADDR16_HIGHEST:
5974 case R_PPC64_ADDR16_HIGHESTA:
5975 case R_PPC64_ADDR16_LO:
5976 case R_PPC64_ADDR16_LO_DS:
5977 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5978 && rel->r_addend == 0)
5979 {
5980 /* We may need a .plt entry if this reloc refers to a
5981 function in a shared lib. */
5982 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5983 return FALSE;
5984 h->pointer_equality_needed = 1;
5985 }
5986 /* Fall through. */
5987
5988 case R_PPC64_REL30:
5989 case R_PPC64_REL32:
5990 case R_PPC64_REL64:
5991 case R_PPC64_ADDR32:
5992 case R_PPC64_UADDR16:
5993 case R_PPC64_UADDR32:
5994 case R_PPC64_UADDR64:
5995 case R_PPC64_TOC:
5996 if (h != NULL && !bfd_link_pic (info))
5997 /* We may need a copy reloc. */
5998 h->non_got_ref = 1;
5999
6000 /* Don't propagate .opd relocs. */
6001 if (NO_OPD_RELOCS && is_opd)
6002 break;
6003
6004 /* If we are creating a shared library, and this is a reloc
6005 against a global symbol, or a non PC relative reloc
6006 against a local symbol, then we need to copy the reloc
6007 into the shared library. However, if we are linking with
6008 -Bsymbolic, we do not need to copy a reloc against a
6009 global symbol which is defined in an object we are
6010 including in the link (i.e., DEF_REGULAR is set). At
6011 this point we have not seen all the input files, so it is
6012 possible that DEF_REGULAR is not set now but will be set
6013 later (it is never cleared). In case of a weak definition,
6014 DEF_REGULAR may be cleared later by a strong definition in
6015 a shared library. We account for that possibility below by
6016 storing information in the dyn_relocs field of the hash
6017 table entry. A similar situation occurs when creating
6018 shared libraries and symbol visibility changes render the
6019 symbol local.
6020
6021 If on the other hand, we are creating an executable, we
6022 may need to keep relocations for symbols satisfied by a
6023 dynamic library if we manage to avoid copy relocs for the
6024 symbol. */
6025 dodyn:
6026 if ((bfd_link_pic (info)
6027 && (must_be_dyn_reloc (info, r_type)
6028 || (h != NULL
6029 && (!SYMBOLIC_BIND (info, h)
6030 || h->root.type == bfd_link_hash_defweak
6031 || !h->def_regular))))
6032 || (ELIMINATE_COPY_RELOCS
6033 && !bfd_link_pic (info)
6034 && h != NULL
6035 && (h->root.type == bfd_link_hash_defweak
6036 || !h->def_regular))
6037 || (!bfd_link_pic (info)
6038 && ifunc != NULL))
6039 {
6040 /* We must copy these reloc types into the output file.
6041 Create a reloc section in dynobj and make room for
6042 this reloc. */
6043 if (sreloc == NULL)
6044 {
6045 sreloc = _bfd_elf_make_dynamic_reloc_section
6046 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6047
6048 if (sreloc == NULL)
6049 return FALSE;
6050 }
6051
6052 /* If this is a global symbol, we count the number of
6053 relocations we need for this symbol. */
6054 if (h != NULL)
6055 {
6056 struct elf_dyn_relocs *p;
6057 struct elf_dyn_relocs **head;
6058
6059 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6060 p = *head;
6061 if (p == NULL || p->sec != sec)
6062 {
6063 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6064 if (p == NULL)
6065 return FALSE;
6066 p->next = *head;
6067 *head = p;
6068 p->sec = sec;
6069 p->count = 0;
6070 p->pc_count = 0;
6071 }
6072 p->count += 1;
6073 if (!must_be_dyn_reloc (info, r_type))
6074 p->pc_count += 1;
6075 }
6076 else
6077 {
6078 /* Track dynamic relocs needed for local syms too.
6079 We really need local syms available to do this
6080 easily. Oh well. */
6081 struct ppc_dyn_relocs *p;
6082 struct ppc_dyn_relocs **head;
6083 bfd_boolean is_ifunc;
6084 asection *s;
6085 void *vpp;
6086 Elf_Internal_Sym *isym;
6087
6088 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6089 abfd, r_symndx);
6090 if (isym == NULL)
6091 return FALSE;
6092
6093 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6094 if (s == NULL)
6095 s = sec;
6096
6097 vpp = &elf_section_data (s)->local_dynrel;
6098 head = (struct ppc_dyn_relocs **) vpp;
6099 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6100 p = *head;
6101 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6102 p = p->next;
6103 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6104 {
6105 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6106 if (p == NULL)
6107 return FALSE;
6108 p->next = *head;
6109 *head = p;
6110 p->sec = sec;
6111 p->ifunc = is_ifunc;
6112 p->count = 0;
6113 }
6114 p->count += 1;
6115 }
6116 }
6117 break;
6118
6119 default:
6120 break;
6121 }
6122 }
6123
6124 return TRUE;
6125 }
6126
6127 /* Merge backend specific data from an object file to the output
6128 object file when linking. */
6129
6130 static bfd_boolean
6131 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6132 {
6133 bfd *obfd = info->output_bfd;
6134 unsigned long iflags, oflags;
6135
6136 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6137 return TRUE;
6138
6139 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6140 return TRUE;
6141
6142 if (!_bfd_generic_verify_endian_match (ibfd, info))
6143 return FALSE;
6144
6145 iflags = elf_elfheader (ibfd)->e_flags;
6146 oflags = elf_elfheader (obfd)->e_flags;
6147
6148 if (iflags & ~EF_PPC64_ABI)
6149 {
6150 _bfd_error_handler
6151 /* xgettext:c-format */
6152 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6153 bfd_set_error (bfd_error_bad_value);
6154 return FALSE;
6155 }
6156 else if (iflags != oflags && iflags != 0)
6157 {
6158 _bfd_error_handler
6159 /* xgettext:c-format */
6160 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6161 ibfd, iflags, oflags);
6162 bfd_set_error (bfd_error_bad_value);
6163 return FALSE;
6164 }
6165
6166 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6167
6168 /* Merge Tag_compatibility attributes and any common GNU ones. */
6169 _bfd_elf_merge_object_attributes (ibfd, info);
6170
6171 return TRUE;
6172 }
6173
6174 static bfd_boolean
6175 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6176 {
6177 /* Print normal ELF private data. */
6178 _bfd_elf_print_private_bfd_data (abfd, ptr);
6179
6180 if (elf_elfheader (abfd)->e_flags != 0)
6181 {
6182 FILE *file = ptr;
6183
6184 fprintf (file, _("private flags = 0x%lx:"),
6185 elf_elfheader (abfd)->e_flags);
6186
6187 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6188 fprintf (file, _(" [abiv%ld]"),
6189 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6190 fputc ('\n', file);
6191 }
6192
6193 return TRUE;
6194 }
6195
6196 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6197 of the code entry point, and its section, which must be in the same
6198 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6199
6200 static bfd_vma
6201 opd_entry_value (asection *opd_sec,
6202 bfd_vma offset,
6203 asection **code_sec,
6204 bfd_vma *code_off,
6205 bfd_boolean in_code_sec)
6206 {
6207 bfd *opd_bfd = opd_sec->owner;
6208 Elf_Internal_Rela *relocs;
6209 Elf_Internal_Rela *lo, *hi, *look;
6210 bfd_vma val;
6211
6212 /* No relocs implies we are linking a --just-symbols object, or looking
6213 at a final linked executable with addr2line or somesuch. */
6214 if (opd_sec->reloc_count == 0)
6215 {
6216 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6217
6218 if (contents == NULL)
6219 {
6220 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6221 return (bfd_vma) -1;
6222 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6223 }
6224
6225 /* PR 17512: file: 64b9dfbb. */
6226 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6227 return (bfd_vma) -1;
6228
6229 val = bfd_get_64 (opd_bfd, contents + offset);
6230 if (code_sec != NULL)
6231 {
6232 asection *sec, *likely = NULL;
6233
6234 if (in_code_sec)
6235 {
6236 sec = *code_sec;
6237 if (sec->vma <= val
6238 && val < sec->vma + sec->size)
6239 likely = sec;
6240 else
6241 val = -1;
6242 }
6243 else
6244 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6245 if (sec->vma <= val
6246 && (sec->flags & SEC_LOAD) != 0
6247 && (sec->flags & SEC_ALLOC) != 0)
6248 likely = sec;
6249 if (likely != NULL)
6250 {
6251 *code_sec = likely;
6252 if (code_off != NULL)
6253 *code_off = val - likely->vma;
6254 }
6255 }
6256 return val;
6257 }
6258
6259 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6260
6261 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6262 if (relocs == NULL)
6263 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6264 /* PR 17512: file: df8e1fd6. */
6265 if (relocs == NULL)
6266 return (bfd_vma) -1;
6267
6268 /* Go find the opd reloc at the sym address. */
6269 lo = relocs;
6270 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6271 val = (bfd_vma) -1;
6272 while (lo < hi)
6273 {
6274 look = lo + (hi - lo) / 2;
6275 if (look->r_offset < offset)
6276 lo = look + 1;
6277 else if (look->r_offset > offset)
6278 hi = look;
6279 else
6280 {
6281 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6282
6283 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6284 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6285 {
6286 unsigned long symndx = ELF64_R_SYM (look->r_info);
6287 asection *sec = NULL;
6288
6289 if (symndx >= symtab_hdr->sh_info
6290 && elf_sym_hashes (opd_bfd) != NULL)
6291 {
6292 struct elf_link_hash_entry **sym_hashes;
6293 struct elf_link_hash_entry *rh;
6294
6295 sym_hashes = elf_sym_hashes (opd_bfd);
6296 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6297 if (rh != NULL)
6298 {
6299 rh = elf_follow_link (rh);
6300 if (rh->root.type != bfd_link_hash_defined
6301 && rh->root.type != bfd_link_hash_defweak)
6302 break;
6303 if (rh->root.u.def.section->owner == opd_bfd)
6304 {
6305 val = rh->root.u.def.value;
6306 sec = rh->root.u.def.section;
6307 }
6308 }
6309 }
6310
6311 if (sec == NULL)
6312 {
6313 Elf_Internal_Sym *sym;
6314
6315 if (symndx < symtab_hdr->sh_info)
6316 {
6317 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6318 if (sym == NULL)
6319 {
6320 size_t symcnt = symtab_hdr->sh_info;
6321 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6322 symcnt, 0,
6323 NULL, NULL, NULL);
6324 if (sym == NULL)
6325 break;
6326 symtab_hdr->contents = (bfd_byte *) sym;
6327 }
6328 sym += symndx;
6329 }
6330 else
6331 {
6332 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6333 1, symndx,
6334 NULL, NULL, NULL);
6335 if (sym == NULL)
6336 break;
6337 }
6338 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6339 if (sec == NULL)
6340 break;
6341 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6342 val = sym->st_value;
6343 }
6344
6345 val += look->r_addend;
6346 if (code_off != NULL)
6347 *code_off = val;
6348 if (code_sec != NULL)
6349 {
6350 if (in_code_sec && *code_sec != sec)
6351 return -1;
6352 else
6353 *code_sec = sec;
6354 }
6355 if (sec->output_section != NULL)
6356 val += sec->output_section->vma + sec->output_offset;
6357 }
6358 break;
6359 }
6360 }
6361
6362 return val;
6363 }
6364
6365 /* If the ELF symbol SYM might be a function in SEC, return the
6366 function size and set *CODE_OFF to the function's entry point,
6367 otherwise return zero. */
6368
6369 static bfd_size_type
6370 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6371 bfd_vma *code_off)
6372 {
6373 bfd_size_type size;
6374
6375 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6376 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6377 return 0;
6378
6379 size = 0;
6380 if (!(sym->flags & BSF_SYNTHETIC))
6381 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6382
6383 if (strcmp (sym->section->name, ".opd") == 0)
6384 {
6385 struct _opd_sec_data *opd = get_opd_info (sym->section);
6386 bfd_vma symval = sym->value;
6387
6388 if (opd != NULL
6389 && opd->adjust != NULL
6390 && elf_section_data (sym->section)->relocs != NULL)
6391 {
6392 /* opd_entry_value will use cached relocs that have been
6393 adjusted, but with raw symbols. That means both local
6394 and global symbols need adjusting. */
6395 long adjust = opd->adjust[OPD_NDX (symval)];
6396 if (adjust == -1)
6397 return 0;
6398 symval += adjust;
6399 }
6400
6401 if (opd_entry_value (sym->section, symval,
6402 &sec, code_off, TRUE) == (bfd_vma) -1)
6403 return 0;
6404 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6405 symbol. This size has nothing to do with the code size of the
6406 function, which is what we're supposed to return, but the
6407 code size isn't available without looking up the dot-sym.
6408 However, doing that would be a waste of time particularly
6409 since elf_find_function will look at the dot-sym anyway.
6410 Now, elf_find_function will keep the largest size of any
6411 function sym found at the code address of interest, so return
6412 1 here to avoid it incorrectly caching a larger function size
6413 for a small function. This does mean we return the wrong
6414 size for a new-ABI function of size 24, but all that does is
6415 disable caching for such functions. */
6416 if (size == 24)
6417 size = 1;
6418 }
6419 else
6420 {
6421 if (sym->section != sec)
6422 return 0;
6423 *code_off = sym->value;
6424 }
6425 if (size == 0)
6426 size = 1;
6427 return size;
6428 }
6429
6430 /* Return true if symbol is a strong function defined in an ELFv2
6431 object with st_other localentry bits of zero, ie. its local entry
6432 point coincides with its global entry point. */
6433
6434 static bfd_boolean
6435 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6436 {
6437 return (h != NULL
6438 && h->type == STT_FUNC
6439 && h->root.type == bfd_link_hash_defined
6440 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6441 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6442 && is_ppc64_elf (h->root.u.def.section->owner)
6443 && abiversion (h->root.u.def.section->owner) >= 2);
6444 }
6445
6446 /* Return true if symbol is defined in a regular object file. */
6447
6448 static bfd_boolean
6449 is_static_defined (struct elf_link_hash_entry *h)
6450 {
6451 return ((h->root.type == bfd_link_hash_defined
6452 || h->root.type == bfd_link_hash_defweak)
6453 && h->root.u.def.section != NULL
6454 && h->root.u.def.section->output_section != NULL);
6455 }
6456
6457 /* If FDH is a function descriptor symbol, return the associated code
6458 entry symbol if it is defined. Return NULL otherwise. */
6459
6460 static struct ppc_link_hash_entry *
6461 defined_code_entry (struct ppc_link_hash_entry *fdh)
6462 {
6463 if (fdh->is_func_descriptor)
6464 {
6465 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6466 if (fh->elf.root.type == bfd_link_hash_defined
6467 || fh->elf.root.type == bfd_link_hash_defweak)
6468 return fh;
6469 }
6470 return NULL;
6471 }
6472
6473 /* If FH is a function code entry symbol, return the associated
6474 function descriptor symbol if it is defined. Return NULL otherwise. */
6475
6476 static struct ppc_link_hash_entry *
6477 defined_func_desc (struct ppc_link_hash_entry *fh)
6478 {
6479 if (fh->oh != NULL
6480 && fh->oh->is_func_descriptor)
6481 {
6482 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6483 if (fdh->elf.root.type == bfd_link_hash_defined
6484 || fdh->elf.root.type == bfd_link_hash_defweak)
6485 return fdh;
6486 }
6487 return NULL;
6488 }
6489
6490 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6491
6492 /* Garbage collect sections, after first dealing with dot-symbols. */
6493
6494 static bfd_boolean
6495 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6496 {
6497 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6498
6499 if (htab != NULL && htab->need_func_desc_adj)
6500 {
6501 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6502 htab->need_func_desc_adj = 0;
6503 }
6504 return bfd_elf_gc_sections (abfd, info);
6505 }
6506
6507 /* Mark all our entry sym sections, both opd and code section. */
6508
6509 static void
6510 ppc64_elf_gc_keep (struct bfd_link_info *info)
6511 {
6512 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6513 struct bfd_sym_chain *sym;
6514
6515 if (htab == NULL)
6516 return;
6517
6518 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6519 {
6520 struct ppc_link_hash_entry *eh, *fh;
6521 asection *sec;
6522
6523 eh = (struct ppc_link_hash_entry *)
6524 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6525 if (eh == NULL)
6526 continue;
6527 if (eh->elf.root.type != bfd_link_hash_defined
6528 && eh->elf.root.type != bfd_link_hash_defweak)
6529 continue;
6530
6531 fh = defined_code_entry (eh);
6532 if (fh != NULL)
6533 {
6534 sec = fh->elf.root.u.def.section;
6535 sec->flags |= SEC_KEEP;
6536 }
6537 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6538 && opd_entry_value (eh->elf.root.u.def.section,
6539 eh->elf.root.u.def.value,
6540 &sec, NULL, FALSE) != (bfd_vma) -1)
6541 sec->flags |= SEC_KEEP;
6542
6543 sec = eh->elf.root.u.def.section;
6544 sec->flags |= SEC_KEEP;
6545 }
6546 }
6547
6548 /* Mark sections containing dynamically referenced symbols. When
6549 building shared libraries, we must assume that any visible symbol is
6550 referenced. */
6551
6552 static bfd_boolean
6553 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6554 {
6555 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6556 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6557 struct ppc_link_hash_entry *fdh;
6558 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6559
6560 /* Dynamic linking info is on the func descriptor sym. */
6561 fdh = defined_func_desc (eh);
6562 if (fdh != NULL)
6563 eh = fdh;
6564
6565 if ((eh->elf.root.type == bfd_link_hash_defined
6566 || eh->elf.root.type == bfd_link_hash_defweak)
6567 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6568 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6569 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6570 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6571 && (!bfd_link_executable (info)
6572 || info->gc_keep_exported
6573 || info->export_dynamic
6574 || (eh->elf.dynamic
6575 && d != NULL
6576 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6577 && (eh->elf.versioned >= versioned
6578 || !bfd_hide_sym_by_version (info->version_info,
6579 eh->elf.root.root.string)))))
6580 {
6581 asection *code_sec;
6582 struct ppc_link_hash_entry *fh;
6583
6584 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6585
6586 /* Function descriptor syms cause the associated
6587 function code sym section to be marked. */
6588 fh = defined_code_entry (eh);
6589 if (fh != NULL)
6590 {
6591 code_sec = fh->elf.root.u.def.section;
6592 code_sec->flags |= SEC_KEEP;
6593 }
6594 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6595 && opd_entry_value (eh->elf.root.u.def.section,
6596 eh->elf.root.u.def.value,
6597 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6598 code_sec->flags |= SEC_KEEP;
6599 }
6600
6601 return TRUE;
6602 }
6603
6604 /* Return the section that should be marked against GC for a given
6605 relocation. */
6606
6607 static asection *
6608 ppc64_elf_gc_mark_hook (asection *sec,
6609 struct bfd_link_info *info,
6610 Elf_Internal_Rela *rel,
6611 struct elf_link_hash_entry *h,
6612 Elf_Internal_Sym *sym)
6613 {
6614 asection *rsec;
6615
6616 /* Syms return NULL if we're marking .opd, so we avoid marking all
6617 function sections, as all functions are referenced in .opd. */
6618 rsec = NULL;
6619 if (get_opd_info (sec) != NULL)
6620 return rsec;
6621
6622 if (h != NULL)
6623 {
6624 enum elf_ppc64_reloc_type r_type;
6625 struct ppc_link_hash_entry *eh, *fh, *fdh;
6626
6627 r_type = ELF64_R_TYPE (rel->r_info);
6628 switch (r_type)
6629 {
6630 case R_PPC64_GNU_VTINHERIT:
6631 case R_PPC64_GNU_VTENTRY:
6632 break;
6633
6634 default:
6635 switch (h->root.type)
6636 {
6637 case bfd_link_hash_defined:
6638 case bfd_link_hash_defweak:
6639 eh = (struct ppc_link_hash_entry *) h;
6640 fdh = defined_func_desc (eh);
6641 if (fdh != NULL)
6642 {
6643 /* -mcall-aixdesc code references the dot-symbol on
6644 a call reloc. Mark the function descriptor too
6645 against garbage collection. */
6646 fdh->elf.mark = 1;
6647 if (fdh->elf.is_weakalias)
6648 weakdef (&fdh->elf)->mark = 1;
6649 eh = fdh;
6650 }
6651
6652 /* Function descriptor syms cause the associated
6653 function code sym section to be marked. */
6654 fh = defined_code_entry (eh);
6655 if (fh != NULL)
6656 {
6657 /* They also mark their opd section. */
6658 eh->elf.root.u.def.section->gc_mark = 1;
6659
6660 rsec = fh->elf.root.u.def.section;
6661 }
6662 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6663 && opd_entry_value (eh->elf.root.u.def.section,
6664 eh->elf.root.u.def.value,
6665 &rsec, NULL, FALSE) != (bfd_vma) -1)
6666 eh->elf.root.u.def.section->gc_mark = 1;
6667 else
6668 rsec = h->root.u.def.section;
6669 break;
6670
6671 case bfd_link_hash_common:
6672 rsec = h->root.u.c.p->section;
6673 break;
6674
6675 default:
6676 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6677 }
6678 }
6679 }
6680 else
6681 {
6682 struct _opd_sec_data *opd;
6683
6684 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6685 opd = get_opd_info (rsec);
6686 if (opd != NULL && opd->func_sec != NULL)
6687 {
6688 rsec->gc_mark = 1;
6689
6690 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6691 }
6692 }
6693
6694 return rsec;
6695 }
6696
6697 /* The maximum size of .sfpr. */
6698 #define SFPR_MAX (218*4)
6699
6700 struct sfpr_def_parms
6701 {
6702 const char name[12];
6703 unsigned char lo, hi;
6704 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6705 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6706 };
6707
6708 /* Auto-generate _save*, _rest* functions in .sfpr.
6709 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6710 instead. */
6711
6712 static bfd_boolean
6713 sfpr_define (struct bfd_link_info *info,
6714 const struct sfpr_def_parms *parm,
6715 asection *stub_sec)
6716 {
6717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6718 unsigned int i;
6719 size_t len = strlen (parm->name);
6720 bfd_boolean writing = FALSE;
6721 char sym[16];
6722
6723 if (htab == NULL)
6724 return FALSE;
6725
6726 memcpy (sym, parm->name, len);
6727 sym[len + 2] = 0;
6728
6729 for (i = parm->lo; i <= parm->hi; i++)
6730 {
6731 struct ppc_link_hash_entry *h;
6732
6733 sym[len + 0] = i / 10 + '0';
6734 sym[len + 1] = i % 10 + '0';
6735 h = (struct ppc_link_hash_entry *)
6736 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6737 if (stub_sec != NULL)
6738 {
6739 if (h != NULL
6740 && h->elf.root.type == bfd_link_hash_defined
6741 && h->elf.root.u.def.section == htab->sfpr)
6742 {
6743 struct elf_link_hash_entry *s;
6744 char buf[32];
6745 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6746 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6747 if (s == NULL)
6748 return FALSE;
6749 if (s->root.type == bfd_link_hash_new
6750 || (s->root.type = bfd_link_hash_defined
6751 && s->root.u.def.section == stub_sec))
6752 {
6753 s->root.type = bfd_link_hash_defined;
6754 s->root.u.def.section = stub_sec;
6755 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6756 + h->elf.root.u.def.value);
6757 s->ref_regular = 1;
6758 s->def_regular = 1;
6759 s->ref_regular_nonweak = 1;
6760 s->forced_local = 1;
6761 s->non_elf = 0;
6762 s->root.linker_def = 1;
6763 }
6764 }
6765 continue;
6766 }
6767 if (h != NULL)
6768 {
6769 h->save_res = 1;
6770 if (!h->elf.def_regular)
6771 {
6772 h->elf.root.type = bfd_link_hash_defined;
6773 h->elf.root.u.def.section = htab->sfpr;
6774 h->elf.root.u.def.value = htab->sfpr->size;
6775 h->elf.type = STT_FUNC;
6776 h->elf.def_regular = 1;
6777 h->elf.non_elf = 0;
6778 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6779 writing = TRUE;
6780 if (htab->sfpr->contents == NULL)
6781 {
6782 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6783 if (htab->sfpr->contents == NULL)
6784 return FALSE;
6785 }
6786 }
6787 }
6788 if (writing)
6789 {
6790 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6791 if (i != parm->hi)
6792 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6793 else
6794 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6795 htab->sfpr->size = p - htab->sfpr->contents;
6796 }
6797 }
6798
6799 return TRUE;
6800 }
6801
6802 static bfd_byte *
6803 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6804 {
6805 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6806 return p + 4;
6807 }
6808
6809 static bfd_byte *
6810 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6811 {
6812 p = savegpr0 (abfd, p, r);
6813 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6814 p = p + 4;
6815 bfd_put_32 (abfd, BLR, p);
6816 return p + 4;
6817 }
6818
6819 static bfd_byte *
6820 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6821 {
6822 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6823 return p + 4;
6824 }
6825
6826 static bfd_byte *
6827 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6828 {
6829 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6830 p = p + 4;
6831 p = restgpr0 (abfd, p, r);
6832 bfd_put_32 (abfd, MTLR_R0, p);
6833 p = p + 4;
6834 if (r == 29)
6835 {
6836 p = restgpr0 (abfd, p, 30);
6837 p = restgpr0 (abfd, p, 31);
6838 }
6839 bfd_put_32 (abfd, BLR, p);
6840 return p + 4;
6841 }
6842
6843 static bfd_byte *
6844 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6845 {
6846 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6847 return p + 4;
6848 }
6849
6850 static bfd_byte *
6851 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6852 {
6853 p = savegpr1 (abfd, p, r);
6854 bfd_put_32 (abfd, BLR, p);
6855 return p + 4;
6856 }
6857
6858 static bfd_byte *
6859 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6860 {
6861 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6862 return p + 4;
6863 }
6864
6865 static bfd_byte *
6866 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6867 {
6868 p = restgpr1 (abfd, p, r);
6869 bfd_put_32 (abfd, BLR, p);
6870 return p + 4;
6871 }
6872
6873 static bfd_byte *
6874 savefpr (bfd *abfd, bfd_byte *p, int r)
6875 {
6876 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6877 return p + 4;
6878 }
6879
6880 static bfd_byte *
6881 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6882 {
6883 p = savefpr (abfd, p, r);
6884 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6885 p = p + 4;
6886 bfd_put_32 (abfd, BLR, p);
6887 return p + 4;
6888 }
6889
6890 static bfd_byte *
6891 restfpr (bfd *abfd, bfd_byte *p, int r)
6892 {
6893 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6894 return p + 4;
6895 }
6896
6897 static bfd_byte *
6898 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6899 {
6900 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6901 p = p + 4;
6902 p = restfpr (abfd, p, r);
6903 bfd_put_32 (abfd, MTLR_R0, p);
6904 p = p + 4;
6905 if (r == 29)
6906 {
6907 p = restfpr (abfd, p, 30);
6908 p = restfpr (abfd, p, 31);
6909 }
6910 bfd_put_32 (abfd, BLR, p);
6911 return p + 4;
6912 }
6913
6914 static bfd_byte *
6915 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6916 {
6917 p = savefpr (abfd, p, r);
6918 bfd_put_32 (abfd, BLR, p);
6919 return p + 4;
6920 }
6921
6922 static bfd_byte *
6923 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6924 {
6925 p = restfpr (abfd, p, r);
6926 bfd_put_32 (abfd, BLR, p);
6927 return p + 4;
6928 }
6929
6930 static bfd_byte *
6931 savevr (bfd *abfd, bfd_byte *p, int r)
6932 {
6933 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6934 p = p + 4;
6935 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6936 return p + 4;
6937 }
6938
6939 static bfd_byte *
6940 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6941 {
6942 p = savevr (abfd, p, r);
6943 bfd_put_32 (abfd, BLR, p);
6944 return p + 4;
6945 }
6946
6947 static bfd_byte *
6948 restvr (bfd *abfd, bfd_byte *p, int r)
6949 {
6950 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6951 p = p + 4;
6952 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6953 return p + 4;
6954 }
6955
6956 static bfd_byte *
6957 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6958 {
6959 p = restvr (abfd, p, r);
6960 bfd_put_32 (abfd, BLR, p);
6961 return p + 4;
6962 }
6963
6964 /* Called via elf_link_hash_traverse to transfer dynamic linking
6965 information on function code symbol entries to their corresponding
6966 function descriptor symbol entries. */
6967
6968 static bfd_boolean
6969 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6970 {
6971 struct bfd_link_info *info;
6972 struct ppc_link_hash_table *htab;
6973 struct ppc_link_hash_entry *fh;
6974 struct ppc_link_hash_entry *fdh;
6975 bfd_boolean force_local;
6976
6977 fh = (struct ppc_link_hash_entry *) h;
6978 if (fh->elf.root.type == bfd_link_hash_indirect)
6979 return TRUE;
6980
6981 if (!fh->is_func)
6982 return TRUE;
6983
6984 if (fh->elf.root.root.string[0] != '.'
6985 || fh->elf.root.root.string[1] == '\0')
6986 return TRUE;
6987
6988 info = inf;
6989 htab = ppc_hash_table (info);
6990 if (htab == NULL)
6991 return FALSE;
6992
6993 /* Find the corresponding function descriptor symbol. */
6994 fdh = lookup_fdh (fh, htab);
6995
6996 /* Resolve undefined references to dot-symbols as the value
6997 in the function descriptor, if we have one in a regular object.
6998 This is to satisfy cases like ".quad .foo". Calls to functions
6999 in dynamic objects are handled elsewhere. */
7000 if ((fh->elf.root.type == bfd_link_hash_undefined
7001 || fh->elf.root.type == bfd_link_hash_undefweak)
7002 && (fdh->elf.root.type == bfd_link_hash_defined
7003 || fdh->elf.root.type == bfd_link_hash_defweak)
7004 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7005 && opd_entry_value (fdh->elf.root.u.def.section,
7006 fdh->elf.root.u.def.value,
7007 &fh->elf.root.u.def.section,
7008 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7009 {
7010 fh->elf.root.type = fdh->elf.root.type;
7011 fh->elf.forced_local = 1;
7012 fh->elf.def_regular = fdh->elf.def_regular;
7013 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7014 }
7015
7016 if (!fh->elf.dynamic)
7017 {
7018 struct plt_entry *ent;
7019
7020 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7021 if (ent->plt.refcount > 0)
7022 break;
7023 if (ent == NULL)
7024 return TRUE;
7025 }
7026
7027 /* Create a descriptor as undefined if necessary. */
7028 if (fdh == NULL
7029 && !bfd_link_executable (info)
7030 && (fh->elf.root.type == bfd_link_hash_undefined
7031 || fh->elf.root.type == bfd_link_hash_undefweak))
7032 {
7033 fdh = make_fdh (info, fh);
7034 if (fdh == NULL)
7035 return FALSE;
7036 }
7037
7038 /* We can't support overriding of symbols on a fake descriptor. */
7039 if (fdh != NULL
7040 && fdh->fake
7041 && (fh->elf.root.type == bfd_link_hash_defined
7042 || fh->elf.root.type == bfd_link_hash_defweak))
7043 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7044
7045 /* Transfer dynamic linking information to the function descriptor. */
7046 if (fdh != NULL)
7047 {
7048 fdh->elf.ref_regular |= fh->elf.ref_regular;
7049 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7050 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7051 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7052 fdh->elf.dynamic |= fh->elf.dynamic;
7053 fdh->elf.needs_plt |= (fh->elf.needs_plt
7054 || fh->elf.type == STT_FUNC
7055 || fh->elf.type == STT_GNU_IFUNC);
7056 move_plt_plist (fh, fdh);
7057
7058 if (!fdh->elf.forced_local
7059 && fh->elf.dynindx != -1)
7060 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7061 return FALSE;
7062 }
7063
7064 /* Now that the info is on the function descriptor, clear the
7065 function code sym info. Any function code syms for which we
7066 don't have a definition in a regular file, we force local.
7067 This prevents a shared library from exporting syms that have
7068 been imported from another library. Function code syms that
7069 are really in the library we must leave global to prevent the
7070 linker dragging in a definition from a static library. */
7071 force_local = (!fh->elf.def_regular
7072 || fdh == NULL
7073 || !fdh->elf.def_regular
7074 || fdh->elf.forced_local);
7075 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7076
7077 return TRUE;
7078 }
7079
7080 static const struct sfpr_def_parms save_res_funcs[] =
7081 {
7082 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7083 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7084 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7085 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7086 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7087 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7088 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7089 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7090 { "._savef", 14, 31, savefpr, savefpr1_tail },
7091 { "._restf", 14, 31, restfpr, restfpr1_tail },
7092 { "_savevr_", 20, 31, savevr, savevr_tail },
7093 { "_restvr_", 20, 31, restvr, restvr_tail }
7094 };
7095
7096 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7097 this hook to a) provide some gcc support functions, and b) transfer
7098 dynamic linking information gathered so far on function code symbol
7099 entries, to their corresponding function descriptor symbol entries. */
7100
7101 static bfd_boolean
7102 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7103 struct bfd_link_info *info)
7104 {
7105 struct ppc_link_hash_table *htab;
7106
7107 htab = ppc_hash_table (info);
7108 if (htab == NULL)
7109 return FALSE;
7110
7111 /* Provide any missing _save* and _rest* functions. */
7112 if (htab->sfpr != NULL)
7113 {
7114 unsigned int i;
7115
7116 htab->sfpr->size = 0;
7117 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7118 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7119 return FALSE;
7120 if (htab->sfpr->size == 0)
7121 htab->sfpr->flags |= SEC_EXCLUDE;
7122 }
7123
7124 if (bfd_link_relocatable (info))
7125 return TRUE;
7126
7127 if (htab->elf.hgot != NULL)
7128 {
7129 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7130 /* Make .TOC. defined so as to prevent it being made dynamic.
7131 The wrong value here is fixed later in ppc64_elf_set_toc. */
7132 if (!htab->elf.hgot->def_regular
7133 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7134 {
7135 htab->elf.hgot->root.type = bfd_link_hash_defined;
7136 htab->elf.hgot->root.u.def.value = 0;
7137 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7138 htab->elf.hgot->def_regular = 1;
7139 htab->elf.hgot->root.linker_def = 1;
7140 }
7141 htab->elf.hgot->type = STT_OBJECT;
7142 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7143 | STV_HIDDEN);
7144 }
7145
7146 if (htab->need_func_desc_adj)
7147 {
7148 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7149 htab->need_func_desc_adj = 0;
7150 }
7151
7152 return TRUE;
7153 }
7154
7155 /* Find dynamic relocs for H that apply to read-only sections. */
7156
7157 static asection *
7158 readonly_dynrelocs (struct elf_link_hash_entry *h)
7159 {
7160 struct ppc_link_hash_entry *eh;
7161 struct elf_dyn_relocs *p;
7162
7163 eh = (struct ppc_link_hash_entry *) h;
7164 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7165 {
7166 asection *s = p->sec->output_section;
7167
7168 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7169 return p->sec;
7170 }
7171 return NULL;
7172 }
7173
7174 /* Return true if we have dynamic relocs against H or any of its weak
7175 aliases, that apply to read-only sections. Cannot be used after
7176 size_dynamic_sections. */
7177
7178 static bfd_boolean
7179 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7180 {
7181 struct ppc_link_hash_entry *eh;
7182
7183 eh = (struct ppc_link_hash_entry *) h;
7184 do
7185 {
7186 if (readonly_dynrelocs (&eh->elf))
7187 return TRUE;
7188 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7189 } while (eh != NULL && &eh->elf != h);
7190
7191 return FALSE;
7192 }
7193
7194 /* Return whether EH has pc-relative dynamic relocs. */
7195
7196 static bfd_boolean
7197 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7198 {
7199 struct elf_dyn_relocs *p;
7200
7201 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7202 if (p->pc_count != 0)
7203 return TRUE;
7204 return FALSE;
7205 }
7206
7207 /* Return true if a global entry stub will be created for H. Valid
7208 for ELFv2 before plt entries have been allocated. */
7209
7210 static bfd_boolean
7211 global_entry_stub (struct elf_link_hash_entry *h)
7212 {
7213 struct plt_entry *pent;
7214
7215 if (!h->pointer_equality_needed
7216 || h->def_regular)
7217 return FALSE;
7218
7219 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7220 if (pent->plt.refcount > 0
7221 && pent->addend == 0)
7222 return TRUE;
7223
7224 return FALSE;
7225 }
7226
7227 /* Adjust a symbol defined by a dynamic object and referenced by a
7228 regular object. The current definition is in some section of the
7229 dynamic object, but we're not including those sections. We have to
7230 change the definition to something the rest of the link can
7231 understand. */
7232
7233 static bfd_boolean
7234 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7235 struct elf_link_hash_entry *h)
7236 {
7237 struct ppc_link_hash_table *htab;
7238 asection *s, *srel;
7239
7240 htab = ppc_hash_table (info);
7241 if (htab == NULL)
7242 return FALSE;
7243
7244 /* Deal with function syms. */
7245 if (h->type == STT_FUNC
7246 || h->type == STT_GNU_IFUNC
7247 || h->needs_plt)
7248 {
7249 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7250 || SYMBOL_CALLS_LOCAL (info, h)
7251 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7252 /* Discard dyn_relocs when non-pic if we've decided that a
7253 function symbol is local and not an ifunc. We keep dynamic
7254 relocs for ifuncs when local rather than always emitting a
7255 plt call stub for them and defining the symbol on the call
7256 stub. We can't do that for ELFv1 anyway (a function symbol
7257 is defined on a descriptor, not code) and it can be faster at
7258 run-time due to not needing to bounce through a stub. The
7259 dyn_relocs for ifuncs will be applied even in a static
7260 executable. */
7261 if (!bfd_link_pic (info)
7262 && h->type != STT_GNU_IFUNC
7263 && local)
7264 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7265
7266 /* Clear procedure linkage table information for any symbol that
7267 won't need a .plt entry. */
7268 struct plt_entry *ent;
7269 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7270 if (ent->plt.refcount > 0)
7271 break;
7272 if (ent == NULL
7273 || (h->type != STT_GNU_IFUNC
7274 && local
7275 && (htab->can_convert_all_inline_plt
7276 || (((struct ppc_link_hash_entry *) h)->tls_mask
7277 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7278 {
7279 h->plt.plist = NULL;
7280 h->needs_plt = 0;
7281 h->pointer_equality_needed = 0;
7282 }
7283 else if (abiversion (info->output_bfd) >= 2)
7284 {
7285 /* Taking a function's address in a read/write section
7286 doesn't require us to define the function symbol in the
7287 executable on a global entry stub. A dynamic reloc can
7288 be used instead. The reason we prefer a few more dynamic
7289 relocs is that calling via a global entry stub costs a
7290 few more instructions, and pointer_equality_needed causes
7291 extra work in ld.so when resolving these symbols. */
7292 if (global_entry_stub (h))
7293 {
7294 if (!readonly_dynrelocs (h))
7295 {
7296 h->pointer_equality_needed = 0;
7297 /* If we haven't seen a branch reloc then we don't need
7298 a plt entry. */
7299 if (!h->needs_plt)
7300 h->plt.plist = NULL;
7301 }
7302 else if (!bfd_link_pic (info))
7303 /* We are going to be defining the function symbol on the
7304 plt stub, so no dyn_relocs needed when non-pic. */
7305 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7306 }
7307
7308 /* ELFv2 function symbols can't have copy relocs. */
7309 return TRUE;
7310 }
7311 else if (!h->needs_plt
7312 && !readonly_dynrelocs (h))
7313 {
7314 /* If we haven't seen a branch reloc then we don't need a
7315 plt entry. */
7316 h->plt.plist = NULL;
7317 h->pointer_equality_needed = 0;
7318 return TRUE;
7319 }
7320 }
7321 else
7322 h->plt.plist = NULL;
7323
7324 /* If this is a weak symbol, and there is a real definition, the
7325 processor independent code will have arranged for us to see the
7326 real definition first, and we can just use the same value. */
7327 if (h->is_weakalias)
7328 {
7329 struct elf_link_hash_entry *def = weakdef (h);
7330 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7331 h->root.u.def.section = def->root.u.def.section;
7332 h->root.u.def.value = def->root.u.def.value;
7333 if (def->root.u.def.section == htab->elf.sdynbss
7334 || def->root.u.def.section == htab->elf.sdynrelro)
7335 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7336 return TRUE;
7337 }
7338
7339 /* If we are creating a shared library, we must presume that the
7340 only references to the symbol are via the global offset table.
7341 For such cases we need not do anything here; the relocations will
7342 be handled correctly by relocate_section. */
7343 if (bfd_link_pic (info))
7344 return TRUE;
7345
7346 /* If there are no references to this symbol that do not use the
7347 GOT, we don't need to generate a copy reloc. */
7348 if (!h->non_got_ref)
7349 return TRUE;
7350
7351 /* Don't generate a copy reloc for symbols defined in the executable. */
7352 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7353
7354 /* If -z nocopyreloc was given, don't generate them either. */
7355 || info->nocopyreloc
7356
7357 /* If we don't find any dynamic relocs in read-only sections, then
7358 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7359 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7360
7361 /* Protected variables do not work with .dynbss. The copy in
7362 .dynbss won't be used by the shared library with the protected
7363 definition for the variable. Text relocations are preferable
7364 to an incorrect program. */
7365 || h->protected_def)
7366 return TRUE;
7367
7368 if (h->plt.plist != NULL)
7369 {
7370 /* We should never get here, but unfortunately there are versions
7371 of gcc out there that improperly (for this ABI) put initialized
7372 function pointers, vtable refs and suchlike in read-only
7373 sections. Allow them to proceed, but warn that this might
7374 break at runtime. */
7375 info->callbacks->einfo
7376 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7377 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7378 h->root.root.string);
7379 }
7380
7381 /* This is a reference to a symbol defined by a dynamic object which
7382 is not a function. */
7383
7384 /* We must allocate the symbol in our .dynbss section, which will
7385 become part of the .bss section of the executable. There will be
7386 an entry for this symbol in the .dynsym section. The dynamic
7387 object will contain position independent code, so all references
7388 from the dynamic object to this symbol will go through the global
7389 offset table. The dynamic linker will use the .dynsym entry to
7390 determine the address it must put in the global offset table, so
7391 both the dynamic object and the regular object will refer to the
7392 same memory location for the variable. */
7393 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7394 {
7395 s = htab->elf.sdynrelro;
7396 srel = htab->elf.sreldynrelro;
7397 }
7398 else
7399 {
7400 s = htab->elf.sdynbss;
7401 srel = htab->elf.srelbss;
7402 }
7403 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7404 {
7405 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7406 linker to copy the initial value out of the dynamic object
7407 and into the runtime process image. */
7408 srel->size += sizeof (Elf64_External_Rela);
7409 h->needs_copy = 1;
7410 }
7411
7412 /* We no longer want dyn_relocs. */
7413 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7414 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7415 }
7416
7417 /* If given a function descriptor symbol, hide both the function code
7418 sym and the descriptor. */
7419 static void
7420 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7421 struct elf_link_hash_entry *h,
7422 bfd_boolean force_local)
7423 {
7424 struct ppc_link_hash_entry *eh;
7425 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7426
7427 eh = (struct ppc_link_hash_entry *) h;
7428 if (eh->is_func_descriptor)
7429 {
7430 struct ppc_link_hash_entry *fh = eh->oh;
7431
7432 if (fh == NULL)
7433 {
7434 const char *p, *q;
7435 struct elf_link_hash_table *htab = elf_hash_table (info);
7436 char save;
7437
7438 /* We aren't supposed to use alloca in BFD because on
7439 systems which do not have alloca the version in libiberty
7440 calls xmalloc, which might cause the program to crash
7441 when it runs out of memory. This function doesn't have a
7442 return status, so there's no way to gracefully return an
7443 error. So cheat. We know that string[-1] can be safely
7444 accessed; It's either a string in an ELF string table,
7445 or allocated in an objalloc structure. */
7446
7447 p = eh->elf.root.root.string - 1;
7448 save = *p;
7449 *(char *) p = '.';
7450 fh = (struct ppc_link_hash_entry *)
7451 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7452 *(char *) p = save;
7453
7454 /* Unfortunately, if it so happens that the string we were
7455 looking for was allocated immediately before this string,
7456 then we overwrote the string terminator. That's the only
7457 reason the lookup should fail. */
7458 if (fh == NULL)
7459 {
7460 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7461 while (q >= eh->elf.root.root.string && *q == *p)
7462 --q, --p;
7463 if (q < eh->elf.root.root.string && *p == '.')
7464 fh = (struct ppc_link_hash_entry *)
7465 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7466 }
7467 if (fh != NULL)
7468 {
7469 eh->oh = fh;
7470 fh->oh = eh;
7471 }
7472 }
7473 if (fh != NULL)
7474 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7475 }
7476 }
7477
7478 static bfd_boolean
7479 get_sym_h (struct elf_link_hash_entry **hp,
7480 Elf_Internal_Sym **symp,
7481 asection **symsecp,
7482 unsigned char **tls_maskp,
7483 Elf_Internal_Sym **locsymsp,
7484 unsigned long r_symndx,
7485 bfd *ibfd)
7486 {
7487 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7488
7489 if (r_symndx >= symtab_hdr->sh_info)
7490 {
7491 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7492 struct elf_link_hash_entry *h;
7493
7494 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7495 h = elf_follow_link (h);
7496
7497 if (hp != NULL)
7498 *hp = h;
7499
7500 if (symp != NULL)
7501 *symp = NULL;
7502
7503 if (symsecp != NULL)
7504 {
7505 asection *symsec = NULL;
7506 if (h->root.type == bfd_link_hash_defined
7507 || h->root.type == bfd_link_hash_defweak)
7508 symsec = h->root.u.def.section;
7509 *symsecp = symsec;
7510 }
7511
7512 if (tls_maskp != NULL)
7513 {
7514 struct ppc_link_hash_entry *eh;
7515
7516 eh = (struct ppc_link_hash_entry *) h;
7517 *tls_maskp = &eh->tls_mask;
7518 }
7519 }
7520 else
7521 {
7522 Elf_Internal_Sym *sym;
7523 Elf_Internal_Sym *locsyms = *locsymsp;
7524
7525 if (locsyms == NULL)
7526 {
7527 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7528 if (locsyms == NULL)
7529 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7530 symtab_hdr->sh_info,
7531 0, NULL, NULL, NULL);
7532 if (locsyms == NULL)
7533 return FALSE;
7534 *locsymsp = locsyms;
7535 }
7536 sym = locsyms + r_symndx;
7537
7538 if (hp != NULL)
7539 *hp = NULL;
7540
7541 if (symp != NULL)
7542 *symp = sym;
7543
7544 if (symsecp != NULL)
7545 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7546
7547 if (tls_maskp != NULL)
7548 {
7549 struct got_entry **lgot_ents;
7550 unsigned char *tls_mask;
7551
7552 tls_mask = NULL;
7553 lgot_ents = elf_local_got_ents (ibfd);
7554 if (lgot_ents != NULL)
7555 {
7556 struct plt_entry **local_plt = (struct plt_entry **)
7557 (lgot_ents + symtab_hdr->sh_info);
7558 unsigned char *lgot_masks = (unsigned char *)
7559 (local_plt + symtab_hdr->sh_info);
7560 tls_mask = &lgot_masks[r_symndx];
7561 }
7562 *tls_maskp = tls_mask;
7563 }
7564 }
7565 return TRUE;
7566 }
7567
7568 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7569 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7570 type suitable for optimization, and 1 otherwise. */
7571
7572 static int
7573 get_tls_mask (unsigned char **tls_maskp,
7574 unsigned long *toc_symndx,
7575 bfd_vma *toc_addend,
7576 Elf_Internal_Sym **locsymsp,
7577 const Elf_Internal_Rela *rel,
7578 bfd *ibfd)
7579 {
7580 unsigned long r_symndx;
7581 int next_r;
7582 struct elf_link_hash_entry *h;
7583 Elf_Internal_Sym *sym;
7584 asection *sec;
7585 bfd_vma off;
7586
7587 r_symndx = ELF64_R_SYM (rel->r_info);
7588 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7589 return 0;
7590
7591 if ((*tls_maskp != NULL
7592 && (**tls_maskp & TLS_TLS) != 0
7593 && **tls_maskp != (TLS_TLS | TLS_MARK))
7594 || sec == NULL
7595 || ppc64_elf_section_data (sec) == NULL
7596 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7597 return 1;
7598
7599 /* Look inside a TOC section too. */
7600 if (h != NULL)
7601 {
7602 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7603 off = h->root.u.def.value;
7604 }
7605 else
7606 off = sym->st_value;
7607 off += rel->r_addend;
7608 BFD_ASSERT (off % 8 == 0);
7609 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7610 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7611 if (toc_symndx != NULL)
7612 *toc_symndx = r_symndx;
7613 if (toc_addend != NULL)
7614 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7615 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7616 return 0;
7617 if ((h == NULL || is_static_defined (h))
7618 && (next_r == -1 || next_r == -2))
7619 return 1 - next_r;
7620 return 1;
7621 }
7622
7623 /* Find (or create) an entry in the tocsave hash table. */
7624
7625 static struct tocsave_entry *
7626 tocsave_find (struct ppc_link_hash_table *htab,
7627 enum insert_option insert,
7628 Elf_Internal_Sym **local_syms,
7629 const Elf_Internal_Rela *irela,
7630 bfd *ibfd)
7631 {
7632 unsigned long r_indx;
7633 struct elf_link_hash_entry *h;
7634 Elf_Internal_Sym *sym;
7635 struct tocsave_entry ent, *p;
7636 hashval_t hash;
7637 struct tocsave_entry **slot;
7638
7639 r_indx = ELF64_R_SYM (irela->r_info);
7640 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7641 return NULL;
7642 if (ent.sec == NULL || ent.sec->output_section == NULL)
7643 {
7644 _bfd_error_handler
7645 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7646 return NULL;
7647 }
7648
7649 if (h != NULL)
7650 ent.offset = h->root.u.def.value;
7651 else
7652 ent.offset = sym->st_value;
7653 ent.offset += irela->r_addend;
7654
7655 hash = tocsave_htab_hash (&ent);
7656 slot = ((struct tocsave_entry **)
7657 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7658 if (slot == NULL)
7659 return NULL;
7660
7661 if (*slot == NULL)
7662 {
7663 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7664 if (p == NULL)
7665 return NULL;
7666 *p = ent;
7667 *slot = p;
7668 }
7669 return *slot;
7670 }
7671
7672 /* Adjust all global syms defined in opd sections. In gcc generated
7673 code for the old ABI, these will already have been done. */
7674
7675 static bfd_boolean
7676 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7677 {
7678 struct ppc_link_hash_entry *eh;
7679 asection *sym_sec;
7680 struct _opd_sec_data *opd;
7681
7682 if (h->root.type == bfd_link_hash_indirect)
7683 return TRUE;
7684
7685 if (h->root.type != bfd_link_hash_defined
7686 && h->root.type != bfd_link_hash_defweak)
7687 return TRUE;
7688
7689 eh = (struct ppc_link_hash_entry *) h;
7690 if (eh->adjust_done)
7691 return TRUE;
7692
7693 sym_sec = eh->elf.root.u.def.section;
7694 opd = get_opd_info (sym_sec);
7695 if (opd != NULL && opd->adjust != NULL)
7696 {
7697 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7698 if (adjust == -1)
7699 {
7700 /* This entry has been deleted. */
7701 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7702 if (dsec == NULL)
7703 {
7704 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7705 if (discarded_section (dsec))
7706 {
7707 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7708 break;
7709 }
7710 }
7711 eh->elf.root.u.def.value = 0;
7712 eh->elf.root.u.def.section = dsec;
7713 }
7714 else
7715 eh->elf.root.u.def.value += adjust;
7716 eh->adjust_done = 1;
7717 }
7718 return TRUE;
7719 }
7720
7721 /* Handles decrementing dynamic reloc counts for the reloc specified by
7722 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7723 have already been determined. */
7724
7725 static bfd_boolean
7726 dec_dynrel_count (bfd_vma r_info,
7727 asection *sec,
7728 struct bfd_link_info *info,
7729 Elf_Internal_Sym **local_syms,
7730 struct elf_link_hash_entry *h,
7731 Elf_Internal_Sym *sym)
7732 {
7733 enum elf_ppc64_reloc_type r_type;
7734 asection *sym_sec = NULL;
7735
7736 /* Can this reloc be dynamic? This switch, and later tests here
7737 should be kept in sync with the code in check_relocs. */
7738 r_type = ELF64_R_TYPE (r_info);
7739 switch (r_type)
7740 {
7741 default:
7742 return TRUE;
7743
7744 case R_PPC64_TPREL16:
7745 case R_PPC64_TPREL16_LO:
7746 case R_PPC64_TPREL16_HI:
7747 case R_PPC64_TPREL16_HA:
7748 case R_PPC64_TPREL16_DS:
7749 case R_PPC64_TPREL16_LO_DS:
7750 case R_PPC64_TPREL16_HIGH:
7751 case R_PPC64_TPREL16_HIGHA:
7752 case R_PPC64_TPREL16_HIGHER:
7753 case R_PPC64_TPREL16_HIGHERA:
7754 case R_PPC64_TPREL16_HIGHEST:
7755 case R_PPC64_TPREL16_HIGHESTA:
7756 case R_PPC64_TPREL64:
7757 case R_PPC64_DTPMOD64:
7758 case R_PPC64_DTPREL64:
7759 case R_PPC64_ADDR64:
7760 case R_PPC64_REL30:
7761 case R_PPC64_REL32:
7762 case R_PPC64_REL64:
7763 case R_PPC64_ADDR14:
7764 case R_PPC64_ADDR14_BRNTAKEN:
7765 case R_PPC64_ADDR14_BRTAKEN:
7766 case R_PPC64_ADDR16:
7767 case R_PPC64_ADDR16_DS:
7768 case R_PPC64_ADDR16_HA:
7769 case R_PPC64_ADDR16_HI:
7770 case R_PPC64_ADDR16_HIGH:
7771 case R_PPC64_ADDR16_HIGHA:
7772 case R_PPC64_ADDR16_HIGHER:
7773 case R_PPC64_ADDR16_HIGHERA:
7774 case R_PPC64_ADDR16_HIGHEST:
7775 case R_PPC64_ADDR16_HIGHESTA:
7776 case R_PPC64_ADDR16_LO:
7777 case R_PPC64_ADDR16_LO_DS:
7778 case R_PPC64_ADDR24:
7779 case R_PPC64_ADDR32:
7780 case R_PPC64_UADDR16:
7781 case R_PPC64_UADDR32:
7782 case R_PPC64_UADDR64:
7783 case R_PPC64_TOC:
7784 break;
7785 }
7786
7787 if (local_syms != NULL)
7788 {
7789 unsigned long r_symndx;
7790 bfd *ibfd = sec->owner;
7791
7792 r_symndx = ELF64_R_SYM (r_info);
7793 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7794 return FALSE;
7795 }
7796
7797 if ((bfd_link_pic (info)
7798 && (must_be_dyn_reloc (info, r_type)
7799 || (h != NULL
7800 && (!SYMBOLIC_BIND (info, h)
7801 || h->root.type == bfd_link_hash_defweak
7802 || !h->def_regular))))
7803 || (ELIMINATE_COPY_RELOCS
7804 && !bfd_link_pic (info)
7805 && h != NULL
7806 && (h->root.type == bfd_link_hash_defweak
7807 || !h->def_regular)))
7808 ;
7809 else
7810 return TRUE;
7811
7812 if (h != NULL)
7813 {
7814 struct elf_dyn_relocs *p;
7815 struct elf_dyn_relocs **pp;
7816 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7817
7818 /* elf_gc_sweep may have already removed all dyn relocs associated
7819 with local syms for a given section. Also, symbol flags are
7820 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7821 report a dynreloc miscount. */
7822 if (*pp == NULL && info->gc_sections)
7823 return TRUE;
7824
7825 while ((p = *pp) != NULL)
7826 {
7827 if (p->sec == sec)
7828 {
7829 if (!must_be_dyn_reloc (info, r_type))
7830 p->pc_count -= 1;
7831 p->count -= 1;
7832 if (p->count == 0)
7833 *pp = p->next;
7834 return TRUE;
7835 }
7836 pp = &p->next;
7837 }
7838 }
7839 else
7840 {
7841 struct ppc_dyn_relocs *p;
7842 struct ppc_dyn_relocs **pp;
7843 void *vpp;
7844 bfd_boolean is_ifunc;
7845
7846 if (local_syms == NULL)
7847 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7848 if (sym_sec == NULL)
7849 sym_sec = sec;
7850
7851 vpp = &elf_section_data (sym_sec)->local_dynrel;
7852 pp = (struct ppc_dyn_relocs **) vpp;
7853
7854 if (*pp == NULL && info->gc_sections)
7855 return TRUE;
7856
7857 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7858 while ((p = *pp) != NULL)
7859 {
7860 if (p->sec == sec && p->ifunc == is_ifunc)
7861 {
7862 p->count -= 1;
7863 if (p->count == 0)
7864 *pp = p->next;
7865 return TRUE;
7866 }
7867 pp = &p->next;
7868 }
7869 }
7870
7871 /* xgettext:c-format */
7872 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7873 sec->owner, sec);
7874 bfd_set_error (bfd_error_bad_value);
7875 return FALSE;
7876 }
7877
7878 /* Remove unused Official Procedure Descriptor entries. Currently we
7879 only remove those associated with functions in discarded link-once
7880 sections, or weakly defined functions that have been overridden. It
7881 would be possible to remove many more entries for statically linked
7882 applications. */
7883
7884 bfd_boolean
7885 ppc64_elf_edit_opd (struct bfd_link_info *info)
7886 {
7887 bfd *ibfd;
7888 bfd_boolean some_edited = FALSE;
7889 asection *need_pad = NULL;
7890 struct ppc_link_hash_table *htab;
7891
7892 htab = ppc_hash_table (info);
7893 if (htab == NULL)
7894 return FALSE;
7895
7896 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7897 {
7898 asection *sec;
7899 Elf_Internal_Rela *relstart, *rel, *relend;
7900 Elf_Internal_Shdr *symtab_hdr;
7901 Elf_Internal_Sym *local_syms;
7902 struct _opd_sec_data *opd;
7903 bfd_boolean need_edit, add_aux_fields, broken;
7904 bfd_size_type cnt_16b = 0;
7905
7906 if (!is_ppc64_elf (ibfd))
7907 continue;
7908
7909 sec = bfd_get_section_by_name (ibfd, ".opd");
7910 if (sec == NULL || sec->size == 0)
7911 continue;
7912
7913 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7914 continue;
7915
7916 if (sec->output_section == bfd_abs_section_ptr)
7917 continue;
7918
7919 /* Look through the section relocs. */
7920 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7921 continue;
7922
7923 local_syms = NULL;
7924 symtab_hdr = &elf_symtab_hdr (ibfd);
7925
7926 /* Read the relocations. */
7927 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7928 info->keep_memory);
7929 if (relstart == NULL)
7930 return FALSE;
7931
7932 /* First run through the relocs to check they are sane, and to
7933 determine whether we need to edit this opd section. */
7934 need_edit = FALSE;
7935 broken = FALSE;
7936 need_pad = sec;
7937 relend = relstart + sec->reloc_count;
7938 for (rel = relstart; rel < relend; )
7939 {
7940 enum elf_ppc64_reloc_type r_type;
7941 unsigned long r_symndx;
7942 asection *sym_sec;
7943 struct elf_link_hash_entry *h;
7944 Elf_Internal_Sym *sym;
7945 bfd_vma offset;
7946
7947 /* .opd contains an array of 16 or 24 byte entries. We're
7948 only interested in the reloc pointing to a function entry
7949 point. */
7950 offset = rel->r_offset;
7951 if (rel + 1 == relend
7952 || rel[1].r_offset != offset + 8)
7953 {
7954 /* If someone messes with .opd alignment then after a
7955 "ld -r" we might have padding in the middle of .opd.
7956 Also, there's nothing to prevent someone putting
7957 something silly in .opd with the assembler. No .opd
7958 optimization for them! */
7959 broken_opd:
7960 _bfd_error_handler
7961 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7962 broken = TRUE;
7963 break;
7964 }
7965
7966 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7967 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7968 {
7969 _bfd_error_handler
7970 /* xgettext:c-format */
7971 (_("%pB: unexpected reloc type %u in .opd section"),
7972 ibfd, r_type);
7973 broken = TRUE;
7974 break;
7975 }
7976
7977 r_symndx = ELF64_R_SYM (rel->r_info);
7978 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7979 r_symndx, ibfd))
7980 goto error_ret;
7981
7982 if (sym_sec == NULL || sym_sec->owner == NULL)
7983 {
7984 const char *sym_name;
7985 if (h != NULL)
7986 sym_name = h->root.root.string;
7987 else
7988 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7989 sym_sec);
7990
7991 _bfd_error_handler
7992 /* xgettext:c-format */
7993 (_("%pB: undefined sym `%s' in .opd section"),
7994 ibfd, sym_name);
7995 broken = TRUE;
7996 break;
7997 }
7998
7999 /* opd entries are always for functions defined in the
8000 current input bfd. If the symbol isn't defined in the
8001 input bfd, then we won't be using the function in this
8002 bfd; It must be defined in a linkonce section in another
8003 bfd, or is weak. It's also possible that we are
8004 discarding the function due to a linker script /DISCARD/,
8005 which we test for via the output_section. */
8006 if (sym_sec->owner != ibfd
8007 || sym_sec->output_section == bfd_abs_section_ptr)
8008 need_edit = TRUE;
8009
8010 rel += 2;
8011 if (rel + 1 == relend
8012 || (rel + 2 < relend
8013 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8014 ++rel;
8015
8016 if (rel == relend)
8017 {
8018 if (sec->size == offset + 24)
8019 {
8020 need_pad = NULL;
8021 break;
8022 }
8023 if (sec->size == offset + 16)
8024 {
8025 cnt_16b++;
8026 break;
8027 }
8028 goto broken_opd;
8029 }
8030 else if (rel + 1 < relend
8031 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8032 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8033 {
8034 if (rel[0].r_offset == offset + 16)
8035 cnt_16b++;
8036 else if (rel[0].r_offset != offset + 24)
8037 goto broken_opd;
8038 }
8039 else
8040 goto broken_opd;
8041 }
8042
8043 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8044
8045 if (!broken && (need_edit || add_aux_fields))
8046 {
8047 Elf_Internal_Rela *write_rel;
8048 Elf_Internal_Shdr *rel_hdr;
8049 bfd_byte *rptr, *wptr;
8050 bfd_byte *new_contents;
8051 bfd_size_type amt;
8052
8053 new_contents = NULL;
8054 amt = OPD_NDX (sec->size) * sizeof (long);
8055 opd = &ppc64_elf_section_data (sec)->u.opd;
8056 opd->adjust = bfd_zalloc (sec->owner, amt);
8057 if (opd->adjust == NULL)
8058 return FALSE;
8059
8060 /* This seems a waste of time as input .opd sections are all
8061 zeros as generated by gcc, but I suppose there's no reason
8062 this will always be so. We might start putting something in
8063 the third word of .opd entries. */
8064 if ((sec->flags & SEC_IN_MEMORY) == 0)
8065 {
8066 bfd_byte *loc;
8067 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8068 {
8069 if (loc != NULL)
8070 free (loc);
8071 error_ret:
8072 if (local_syms != NULL
8073 && symtab_hdr->contents != (unsigned char *) local_syms)
8074 free (local_syms);
8075 if (elf_section_data (sec)->relocs != relstart)
8076 free (relstart);
8077 return FALSE;
8078 }
8079 sec->contents = loc;
8080 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8081 }
8082
8083 elf_section_data (sec)->relocs = relstart;
8084
8085 new_contents = sec->contents;
8086 if (add_aux_fields)
8087 {
8088 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8089 if (new_contents == NULL)
8090 return FALSE;
8091 need_pad = NULL;
8092 }
8093 wptr = new_contents;
8094 rptr = sec->contents;
8095 write_rel = relstart;
8096 for (rel = relstart; rel < relend; )
8097 {
8098 unsigned long r_symndx;
8099 asection *sym_sec;
8100 struct elf_link_hash_entry *h;
8101 struct ppc_link_hash_entry *fdh = NULL;
8102 Elf_Internal_Sym *sym;
8103 long opd_ent_size;
8104 Elf_Internal_Rela *next_rel;
8105 bfd_boolean skip;
8106
8107 r_symndx = ELF64_R_SYM (rel->r_info);
8108 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8109 r_symndx, ibfd))
8110 goto error_ret;
8111
8112 next_rel = rel + 2;
8113 if (next_rel + 1 == relend
8114 || (next_rel + 2 < relend
8115 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8116 ++next_rel;
8117
8118 /* See if the .opd entry is full 24 byte or
8119 16 byte (with fd_aux entry overlapped with next
8120 fd_func). */
8121 opd_ent_size = 24;
8122 if (next_rel == relend)
8123 {
8124 if (sec->size == rel->r_offset + 16)
8125 opd_ent_size = 16;
8126 }
8127 else if (next_rel->r_offset == rel->r_offset + 16)
8128 opd_ent_size = 16;
8129
8130 if (h != NULL
8131 && h->root.root.string[0] == '.')
8132 {
8133 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8134 if (fdh != NULL)
8135 {
8136 fdh = ppc_follow_link (fdh);
8137 if (fdh->elf.root.type != bfd_link_hash_defined
8138 && fdh->elf.root.type != bfd_link_hash_defweak)
8139 fdh = NULL;
8140 }
8141 }
8142
8143 skip = (sym_sec->owner != ibfd
8144 || sym_sec->output_section == bfd_abs_section_ptr);
8145 if (skip)
8146 {
8147 if (fdh != NULL && sym_sec->owner == ibfd)
8148 {
8149 /* Arrange for the function descriptor sym
8150 to be dropped. */
8151 fdh->elf.root.u.def.value = 0;
8152 fdh->elf.root.u.def.section = sym_sec;
8153 }
8154 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8155
8156 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8157 rel = next_rel;
8158 else
8159 while (1)
8160 {
8161 if (!dec_dynrel_count (rel->r_info, sec, info,
8162 NULL, h, sym))
8163 goto error_ret;
8164
8165 if (++rel == next_rel)
8166 break;
8167
8168 r_symndx = ELF64_R_SYM (rel->r_info);
8169 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8170 r_symndx, ibfd))
8171 goto error_ret;
8172 }
8173 }
8174 else
8175 {
8176 /* We'll be keeping this opd entry. */
8177 long adjust;
8178
8179 if (fdh != NULL)
8180 {
8181 /* Redefine the function descriptor symbol to
8182 this location in the opd section. It is
8183 necessary to update the value here rather
8184 than using an array of adjustments as we do
8185 for local symbols, because various places
8186 in the generic ELF code use the value
8187 stored in u.def.value. */
8188 fdh->elf.root.u.def.value = wptr - new_contents;
8189 fdh->adjust_done = 1;
8190 }
8191
8192 /* Local syms are a bit tricky. We could
8193 tweak them as they can be cached, but
8194 we'd need to look through the local syms
8195 for the function descriptor sym which we
8196 don't have at the moment. So keep an
8197 array of adjustments. */
8198 adjust = (wptr - new_contents) - (rptr - sec->contents);
8199 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8200
8201 if (wptr != rptr)
8202 memcpy (wptr, rptr, opd_ent_size);
8203 wptr += opd_ent_size;
8204 if (add_aux_fields && opd_ent_size == 16)
8205 {
8206 memset (wptr, '\0', 8);
8207 wptr += 8;
8208 }
8209
8210 /* We need to adjust any reloc offsets to point to the
8211 new opd entries. */
8212 for ( ; rel != next_rel; ++rel)
8213 {
8214 rel->r_offset += adjust;
8215 if (write_rel != rel)
8216 memcpy (write_rel, rel, sizeof (*rel));
8217 ++write_rel;
8218 }
8219 }
8220
8221 rptr += opd_ent_size;
8222 }
8223
8224 sec->size = wptr - new_contents;
8225 sec->reloc_count = write_rel - relstart;
8226 if (add_aux_fields)
8227 {
8228 free (sec->contents);
8229 sec->contents = new_contents;
8230 }
8231
8232 /* Fudge the header size too, as this is used later in
8233 elf_bfd_final_link if we are emitting relocs. */
8234 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8235 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8236 some_edited = TRUE;
8237 }
8238 else if (elf_section_data (sec)->relocs != relstart)
8239 free (relstart);
8240
8241 if (local_syms != NULL
8242 && symtab_hdr->contents != (unsigned char *) local_syms)
8243 {
8244 if (!info->keep_memory)
8245 free (local_syms);
8246 else
8247 symtab_hdr->contents = (unsigned char *) local_syms;
8248 }
8249 }
8250
8251 if (some_edited)
8252 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8253
8254 /* If we are doing a final link and the last .opd entry is just 16 byte
8255 long, add a 8 byte padding after it. */
8256 if (need_pad != NULL && !bfd_link_relocatable (info))
8257 {
8258 bfd_byte *p;
8259
8260 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8261 {
8262 BFD_ASSERT (need_pad->size > 0);
8263
8264 p = bfd_malloc (need_pad->size + 8);
8265 if (p == NULL)
8266 return FALSE;
8267
8268 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8269 p, 0, need_pad->size))
8270 return FALSE;
8271
8272 need_pad->contents = p;
8273 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8274 }
8275 else
8276 {
8277 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8278 if (p == NULL)
8279 return FALSE;
8280
8281 need_pad->contents = p;
8282 }
8283
8284 memset (need_pad->contents + need_pad->size, 0, 8);
8285 need_pad->size += 8;
8286 }
8287
8288 return TRUE;
8289 }
8290
8291 /* Analyze inline PLT call relocations to see whether calls to locally
8292 defined functions can be converted to direct calls. */
8293
8294 bfd_boolean
8295 ppc64_elf_inline_plt (struct bfd_link_info *info)
8296 {
8297 struct ppc_link_hash_table *htab;
8298 bfd *ibfd;
8299 asection *sec;
8300 bfd_vma low_vma, high_vma, limit;
8301
8302 htab = ppc_hash_table (info);
8303 if (htab == NULL)
8304 return FALSE;
8305
8306 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8307 reduced somewhat to cater for possible stubs that might be added
8308 between the call and its destination. */
8309 if (htab->params->group_size < 0)
8310 {
8311 limit = -htab->params->group_size;
8312 if (limit == 1)
8313 limit = 0x1e00000;
8314 }
8315 else
8316 {
8317 limit = htab->params->group_size;
8318 if (limit == 1)
8319 limit = 0x1c00000;
8320 }
8321
8322 low_vma = -1;
8323 high_vma = 0;
8324 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8325 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8326 {
8327 if (low_vma > sec->vma)
8328 low_vma = sec->vma;
8329 if (high_vma < sec->vma + sec->size)
8330 high_vma = sec->vma + sec->size;
8331 }
8332
8333 /* If a "bl" can reach anywhere in local code sections, then we can
8334 convert all inline PLT sequences to direct calls when the symbol
8335 is local. */
8336 if (high_vma - low_vma < limit)
8337 {
8338 htab->can_convert_all_inline_plt = 1;
8339 return TRUE;
8340 }
8341
8342 /* Otherwise, go looking through relocs for cases where a direct
8343 call won't reach. Mark the symbol on any such reloc to disable
8344 the optimization and keep the PLT entry as it seems likely that
8345 this will be better than creating trampolines. Note that this
8346 will disable the optimization for all inline PLT calls to a
8347 particular symbol, not just those that won't reach. The
8348 difficulty in doing a more precise optimization is that the
8349 linker needs to make a decision depending on whether a
8350 particular R_PPC64_PLTCALL insn can be turned into a direct
8351 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8352 the sequence, and there is nothing that ties those relocs
8353 together except their symbol. */
8354
8355 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8356 {
8357 Elf_Internal_Shdr *symtab_hdr;
8358 Elf_Internal_Sym *local_syms;
8359
8360 if (!is_ppc64_elf (ibfd))
8361 continue;
8362
8363 local_syms = NULL;
8364 symtab_hdr = &elf_symtab_hdr (ibfd);
8365
8366 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8367 if (ppc64_elf_section_data (sec)->has_pltcall
8368 && !bfd_is_abs_section (sec->output_section))
8369 {
8370 Elf_Internal_Rela *relstart, *rel, *relend;
8371
8372 /* Read the relocations. */
8373 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8374 info->keep_memory);
8375 if (relstart == NULL)
8376 return FALSE;
8377
8378 relend = relstart + sec->reloc_count;
8379 for (rel = relstart; rel < relend; )
8380 {
8381 enum elf_ppc64_reloc_type r_type;
8382 unsigned long r_symndx;
8383 asection *sym_sec;
8384 struct elf_link_hash_entry *h;
8385 Elf_Internal_Sym *sym;
8386 unsigned char *tls_maskp;
8387
8388 r_type = ELF64_R_TYPE (rel->r_info);
8389 if (r_type != R_PPC64_PLTCALL)
8390 continue;
8391
8392 r_symndx = ELF64_R_SYM (rel->r_info);
8393 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8394 r_symndx, ibfd))
8395 {
8396 if (elf_section_data (sec)->relocs != relstart)
8397 free (relstart);
8398 if (local_syms != NULL
8399 && symtab_hdr->contents != (unsigned char *) local_syms)
8400 free (local_syms);
8401 return FALSE;
8402 }
8403
8404 if (sym_sec != NULL && sym_sec->output_section != NULL)
8405 {
8406 bfd_vma from, to;
8407 if (h != NULL)
8408 to = h->root.u.def.value;
8409 else
8410 to = sym->st_value;
8411 to += (rel->r_addend
8412 + sym_sec->output_offset
8413 + sym_sec->output_section->vma);
8414 from = (rel->r_offset
8415 + sec->output_offset
8416 + sec->output_section->vma);
8417 if (to - from + limit < 2 * limit)
8418 *tls_maskp &= ~PLT_KEEP;
8419 }
8420 }
8421 if (elf_section_data (sec)->relocs != relstart)
8422 free (relstart);
8423 }
8424
8425 if (local_syms != NULL
8426 && symtab_hdr->contents != (unsigned char *) local_syms)
8427 {
8428 if (!info->keep_memory)
8429 free (local_syms);
8430 else
8431 symtab_hdr->contents = (unsigned char *) local_syms;
8432 }
8433 }
8434
8435 return TRUE;
8436 }
8437
8438 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8439
8440 asection *
8441 ppc64_elf_tls_setup (struct bfd_link_info *info)
8442 {
8443 struct ppc_link_hash_table *htab;
8444
8445 htab = ppc_hash_table (info);
8446 if (htab == NULL)
8447 return NULL;
8448
8449 if (abiversion (info->output_bfd) == 1)
8450 htab->opd_abi = 1;
8451
8452 if (htab->params->no_multi_toc)
8453 htab->do_multi_toc = 0;
8454 else if (!htab->do_multi_toc)
8455 htab->params->no_multi_toc = 1;
8456
8457 /* Default to --no-plt-localentry, as this option can cause problems
8458 with symbol interposition. For example, glibc libpthread.so and
8459 libc.so duplicate many pthread symbols, with a fallback
8460 implementation in libc.so. In some cases the fallback does more
8461 work than the pthread implementation. __pthread_condattr_destroy
8462 is one such symbol: the libpthread.so implementation is
8463 localentry:0 while the libc.so implementation is localentry:8.
8464 An app that "cleverly" uses dlopen to only load necessary
8465 libraries at runtime may omit loading libpthread.so when not
8466 running multi-threaded, which then results in the libc.so
8467 fallback symbols being used and ld.so complaining. Now there
8468 are workarounds in ld (see non_zero_localentry) to detect the
8469 pthread situation, but that may not be the only case where
8470 --plt-localentry can cause trouble. */
8471 if (htab->params->plt_localentry0 < 0)
8472 htab->params->plt_localentry0 = 0;
8473 if (htab->params->plt_localentry0
8474 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8475 FALSE, FALSE, FALSE) == NULL)
8476 _bfd_error_handler
8477 (_("warning: --plt-localentry is especially dangerous without "
8478 "ld.so support to detect ABI violations"));
8479
8480 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8481 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8482 FALSE, FALSE, TRUE));
8483 /* Move dynamic linking info to the function descriptor sym. */
8484 if (htab->tls_get_addr != NULL)
8485 func_desc_adjust (&htab->tls_get_addr->elf, info);
8486 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8487 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8488 FALSE, FALSE, TRUE));
8489 if (htab->params->tls_get_addr_opt)
8490 {
8491 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8492
8493 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8494 FALSE, FALSE, TRUE);
8495 if (opt != NULL)
8496 func_desc_adjust (opt, info);
8497 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8498 FALSE, FALSE, TRUE);
8499 if (opt_fd != NULL
8500 && (opt_fd->root.type == bfd_link_hash_defined
8501 || opt_fd->root.type == bfd_link_hash_defweak))
8502 {
8503 /* If glibc supports an optimized __tls_get_addr call stub,
8504 signalled by the presence of __tls_get_addr_opt, and we'll
8505 be calling __tls_get_addr via a plt call stub, then
8506 make __tls_get_addr point to __tls_get_addr_opt. */
8507 tga_fd = &htab->tls_get_addr_fd->elf;
8508 if (htab->elf.dynamic_sections_created
8509 && tga_fd != NULL
8510 && (tga_fd->type == STT_FUNC
8511 || tga_fd->needs_plt)
8512 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8513 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8514 {
8515 struct plt_entry *ent;
8516
8517 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8518 if (ent->plt.refcount > 0)
8519 break;
8520 if (ent != NULL)
8521 {
8522 tga_fd->root.type = bfd_link_hash_indirect;
8523 tga_fd->root.u.i.link = &opt_fd->root;
8524 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8525 opt_fd->mark = 1;
8526 if (opt_fd->dynindx != -1)
8527 {
8528 /* Use __tls_get_addr_opt in dynamic relocations. */
8529 opt_fd->dynindx = -1;
8530 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8531 opt_fd->dynstr_index);
8532 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8533 return NULL;
8534 }
8535 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8536 tga = &htab->tls_get_addr->elf;
8537 if (opt != NULL && tga != NULL)
8538 {
8539 tga->root.type = bfd_link_hash_indirect;
8540 tga->root.u.i.link = &opt->root;
8541 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8542 opt->mark = 1;
8543 _bfd_elf_link_hash_hide_symbol (info, opt,
8544 tga->forced_local);
8545 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8546 }
8547 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8548 htab->tls_get_addr_fd->is_func_descriptor = 1;
8549 if (htab->tls_get_addr != NULL)
8550 {
8551 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8552 htab->tls_get_addr->is_func = 1;
8553 }
8554 }
8555 }
8556 }
8557 else if (htab->params->tls_get_addr_opt < 0)
8558 htab->params->tls_get_addr_opt = 0;
8559 }
8560 return _bfd_elf_tls_setup (info->output_bfd, info);
8561 }
8562
8563 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8564 HASH1 or HASH2. */
8565
8566 static bfd_boolean
8567 branch_reloc_hash_match (const bfd *ibfd,
8568 const Elf_Internal_Rela *rel,
8569 const struct ppc_link_hash_entry *hash1,
8570 const struct ppc_link_hash_entry *hash2)
8571 {
8572 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8573 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8574 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8575
8576 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8577 {
8578 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8579 struct elf_link_hash_entry *h;
8580
8581 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8582 h = elf_follow_link (h);
8583 if (h == &hash1->elf || h == &hash2->elf)
8584 return TRUE;
8585 }
8586 return FALSE;
8587 }
8588
8589 /* Run through all the TLS relocs looking for optimization
8590 opportunities. The linker has been hacked (see ppc64elf.em) to do
8591 a preliminary section layout so that we know the TLS segment
8592 offsets. We can't optimize earlier because some optimizations need
8593 to know the tp offset, and we need to optimize before allocating
8594 dynamic relocations. */
8595
8596 bfd_boolean
8597 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8598 {
8599 bfd *ibfd;
8600 asection *sec;
8601 struct ppc_link_hash_table *htab;
8602 unsigned char *toc_ref;
8603 int pass;
8604
8605 if (!bfd_link_executable (info))
8606 return TRUE;
8607
8608 htab = ppc_hash_table (info);
8609 if (htab == NULL)
8610 return FALSE;
8611
8612 /* Make two passes over the relocs. On the first pass, mark toc
8613 entries involved with tls relocs, and check that tls relocs
8614 involved in setting up a tls_get_addr call are indeed followed by
8615 such a call. If they are not, we can't do any tls optimization.
8616 On the second pass twiddle tls_mask flags to notify
8617 relocate_section that optimization can be done, and adjust got
8618 and plt refcounts. */
8619 toc_ref = NULL;
8620 for (pass = 0; pass < 2; ++pass)
8621 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8622 {
8623 Elf_Internal_Sym *locsyms = NULL;
8624 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8625
8626 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8627 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8628 {
8629 Elf_Internal_Rela *relstart, *rel, *relend;
8630 bfd_boolean found_tls_get_addr_arg = 0;
8631
8632 /* Read the relocations. */
8633 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8634 info->keep_memory);
8635 if (relstart == NULL)
8636 {
8637 free (toc_ref);
8638 return FALSE;
8639 }
8640
8641 relend = relstart + sec->reloc_count;
8642 for (rel = relstart; rel < relend; rel++)
8643 {
8644 enum elf_ppc64_reloc_type r_type;
8645 unsigned long r_symndx;
8646 struct elf_link_hash_entry *h;
8647 Elf_Internal_Sym *sym;
8648 asection *sym_sec;
8649 unsigned char *tls_mask;
8650 unsigned char tls_set, tls_clear, tls_type = 0;
8651 bfd_vma value;
8652 bfd_boolean ok_tprel, is_local;
8653 long toc_ref_index = 0;
8654 int expecting_tls_get_addr = 0;
8655 bfd_boolean ret = FALSE;
8656
8657 r_symndx = ELF64_R_SYM (rel->r_info);
8658 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8659 r_symndx, ibfd))
8660 {
8661 err_free_rel:
8662 if (elf_section_data (sec)->relocs != relstart)
8663 free (relstart);
8664 if (toc_ref != NULL)
8665 free (toc_ref);
8666 if (locsyms != NULL
8667 && (elf_symtab_hdr (ibfd).contents
8668 != (unsigned char *) locsyms))
8669 free (locsyms);
8670 return ret;
8671 }
8672
8673 if (h != NULL)
8674 {
8675 if (h->root.type == bfd_link_hash_defined
8676 || h->root.type == bfd_link_hash_defweak)
8677 value = h->root.u.def.value;
8678 else if (h->root.type == bfd_link_hash_undefweak)
8679 value = 0;
8680 else
8681 {
8682 found_tls_get_addr_arg = 0;
8683 continue;
8684 }
8685 }
8686 else
8687 /* Symbols referenced by TLS relocs must be of type
8688 STT_TLS. So no need for .opd local sym adjust. */
8689 value = sym->st_value;
8690
8691 ok_tprel = FALSE;
8692 is_local = FALSE;
8693 if (h == NULL
8694 || !h->def_dynamic)
8695 {
8696 is_local = TRUE;
8697 if (h != NULL
8698 && h->root.type == bfd_link_hash_undefweak)
8699 ok_tprel = TRUE;
8700 else if (sym_sec != NULL
8701 && sym_sec->output_section != NULL)
8702 {
8703 value += sym_sec->output_offset;
8704 value += sym_sec->output_section->vma;
8705 value -= htab->elf.tls_sec->vma;
8706 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8707 < (bfd_vma) 1 << 32);
8708 }
8709 }
8710
8711 r_type = ELF64_R_TYPE (rel->r_info);
8712 /* If this section has old-style __tls_get_addr calls
8713 without marker relocs, then check that each
8714 __tls_get_addr call reloc is preceded by a reloc
8715 that conceivably belongs to the __tls_get_addr arg
8716 setup insn. If we don't find matching arg setup
8717 relocs, don't do any tls optimization. */
8718 if (pass == 0
8719 && sec->has_tls_get_addr_call
8720 && h != NULL
8721 && (h == &htab->tls_get_addr->elf
8722 || h == &htab->tls_get_addr_fd->elf)
8723 && !found_tls_get_addr_arg
8724 && is_branch_reloc (r_type))
8725 {
8726 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8727 "TLS optimization disabled\n"),
8728 ibfd, sec, rel->r_offset);
8729 ret = TRUE;
8730 goto err_free_rel;
8731 }
8732
8733 found_tls_get_addr_arg = 0;
8734 switch (r_type)
8735 {
8736 case R_PPC64_GOT_TLSLD16:
8737 case R_PPC64_GOT_TLSLD16_LO:
8738 expecting_tls_get_addr = 1;
8739 found_tls_get_addr_arg = 1;
8740 /* Fall through. */
8741
8742 case R_PPC64_GOT_TLSLD16_HI:
8743 case R_PPC64_GOT_TLSLD16_HA:
8744 /* These relocs should never be against a symbol
8745 defined in a shared lib. Leave them alone if
8746 that turns out to be the case. */
8747 if (!is_local)
8748 continue;
8749
8750 /* LD -> LE */
8751 tls_set = 0;
8752 tls_clear = TLS_LD;
8753 tls_type = TLS_TLS | TLS_LD;
8754 break;
8755
8756 case R_PPC64_GOT_TLSGD16:
8757 case R_PPC64_GOT_TLSGD16_LO:
8758 expecting_tls_get_addr = 1;
8759 found_tls_get_addr_arg = 1;
8760 /* Fall through. */
8761
8762 case R_PPC64_GOT_TLSGD16_HI:
8763 case R_PPC64_GOT_TLSGD16_HA:
8764 if (ok_tprel)
8765 /* GD -> LE */
8766 tls_set = 0;
8767 else
8768 /* GD -> IE */
8769 tls_set = TLS_TLS | TLS_TPRELGD;
8770 tls_clear = TLS_GD;
8771 tls_type = TLS_TLS | TLS_GD;
8772 break;
8773
8774 case R_PPC64_GOT_TPREL16_DS:
8775 case R_PPC64_GOT_TPREL16_LO_DS:
8776 case R_PPC64_GOT_TPREL16_HI:
8777 case R_PPC64_GOT_TPREL16_HA:
8778 if (ok_tprel)
8779 {
8780 /* IE -> LE */
8781 tls_set = 0;
8782 tls_clear = TLS_TPREL;
8783 tls_type = TLS_TLS | TLS_TPREL;
8784 break;
8785 }
8786 continue;
8787
8788 case R_PPC64_TLSGD:
8789 case R_PPC64_TLSLD:
8790 if (rel + 1 < relend
8791 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8792 {
8793 if (pass != 0
8794 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8795 {
8796 r_symndx = ELF64_R_SYM (rel[1].r_info);
8797 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8798 r_symndx, ibfd))
8799 goto err_free_rel;
8800 if (h != NULL)
8801 {
8802 struct plt_entry *ent = NULL;
8803
8804 for (ent = h->plt.plist;
8805 ent != NULL;
8806 ent = ent->next)
8807 if (ent->addend == rel[1].r_addend)
8808 break;
8809
8810 if (ent != NULL
8811 && ent->plt.refcount > 0)
8812 ent->plt.refcount -= 1;
8813 }
8814 }
8815 continue;
8816 }
8817 found_tls_get_addr_arg = 1;
8818 /* Fall through. */
8819
8820 case R_PPC64_TLS:
8821 case R_PPC64_TOC16:
8822 case R_PPC64_TOC16_LO:
8823 if (sym_sec == NULL || sym_sec != toc)
8824 continue;
8825
8826 /* Mark this toc entry as referenced by a TLS
8827 code sequence. We can do that now in the
8828 case of R_PPC64_TLS, and after checking for
8829 tls_get_addr for the TOC16 relocs. */
8830 if (toc_ref == NULL)
8831 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8832 if (toc_ref == NULL)
8833 goto err_free_rel;
8834
8835 if (h != NULL)
8836 value = h->root.u.def.value;
8837 else
8838 value = sym->st_value;
8839 value += rel->r_addend;
8840 if (value % 8 != 0)
8841 continue;
8842 BFD_ASSERT (value < toc->size
8843 && toc->output_offset % 8 == 0);
8844 toc_ref_index = (value + toc->output_offset) / 8;
8845 if (r_type == R_PPC64_TLS
8846 || r_type == R_PPC64_TLSGD
8847 || r_type == R_PPC64_TLSLD)
8848 {
8849 toc_ref[toc_ref_index] = 1;
8850 continue;
8851 }
8852
8853 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8854 continue;
8855
8856 tls_set = 0;
8857 tls_clear = 0;
8858 expecting_tls_get_addr = 2;
8859 break;
8860
8861 case R_PPC64_TPREL64:
8862 if (pass == 0
8863 || sec != toc
8864 || toc_ref == NULL
8865 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8866 continue;
8867 if (ok_tprel)
8868 {
8869 /* IE -> LE */
8870 tls_set = TLS_EXPLICIT;
8871 tls_clear = TLS_TPREL;
8872 break;
8873 }
8874 continue;
8875
8876 case R_PPC64_DTPMOD64:
8877 if (pass == 0
8878 || sec != toc
8879 || toc_ref == NULL
8880 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8881 continue;
8882 if (rel + 1 < relend
8883 && (rel[1].r_info
8884 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8885 && rel[1].r_offset == rel->r_offset + 8)
8886 {
8887 if (ok_tprel)
8888 /* GD -> LE */
8889 tls_set = TLS_EXPLICIT | TLS_GD;
8890 else
8891 /* GD -> IE */
8892 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8893 tls_clear = TLS_GD;
8894 }
8895 else
8896 {
8897 if (!is_local)
8898 continue;
8899
8900 /* LD -> LE */
8901 tls_set = TLS_EXPLICIT;
8902 tls_clear = TLS_LD;
8903 }
8904 break;
8905
8906 default:
8907 continue;
8908 }
8909
8910 if (pass == 0)
8911 {
8912 if (!expecting_tls_get_addr
8913 || !sec->has_tls_get_addr_call)
8914 continue;
8915
8916 if (rel + 1 < relend
8917 && branch_reloc_hash_match (ibfd, rel + 1,
8918 htab->tls_get_addr,
8919 htab->tls_get_addr_fd))
8920 {
8921 if (expecting_tls_get_addr == 2)
8922 {
8923 /* Check for toc tls entries. */
8924 unsigned char *toc_tls;
8925 int retval;
8926
8927 retval = get_tls_mask (&toc_tls, NULL, NULL,
8928 &locsyms,
8929 rel, ibfd);
8930 if (retval == 0)
8931 goto err_free_rel;
8932 if (toc_tls != NULL)
8933 {
8934 if ((*toc_tls & TLS_TLS) != 0
8935 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8936 found_tls_get_addr_arg = 1;
8937 if (retval > 1)
8938 toc_ref[toc_ref_index] = 1;
8939 }
8940 }
8941 continue;
8942 }
8943
8944 /* Uh oh, we didn't find the expected call. We
8945 could just mark this symbol to exclude it
8946 from tls optimization but it's safer to skip
8947 the entire optimization. */
8948 /* xgettext:c-format */
8949 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8950 "TLS optimization disabled\n"),
8951 ibfd, sec, rel->r_offset);
8952 ret = TRUE;
8953 goto err_free_rel;
8954 }
8955
8956 /* If we don't have old-style __tls_get_addr calls
8957 without TLSGD/TLSLD marker relocs, and we haven't
8958 found a new-style __tls_get_addr call with a
8959 marker for this symbol, then we either have a
8960 broken object file or an -mlongcall style
8961 indirect call to __tls_get_addr without a marker.
8962 Disable optimization in this case. */
8963 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8964 && (tls_set & TLS_EXPLICIT) == 0
8965 && !sec->has_tls_get_addr_call
8966 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8967 != (TLS_TLS | TLS_MARK)))
8968 continue;
8969
8970 if (expecting_tls_get_addr)
8971 {
8972 struct plt_entry *ent = NULL;
8973
8974 if (htab->tls_get_addr != NULL)
8975 for (ent = htab->tls_get_addr->elf.plt.plist;
8976 ent != NULL;
8977 ent = ent->next)
8978 if (ent->addend == 0)
8979 break;
8980
8981 if (ent == NULL && htab->tls_get_addr_fd != NULL)
8982 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8983 ent != NULL;
8984 ent = ent->next)
8985 if (ent->addend == 0)
8986 break;
8987
8988 if (ent != NULL
8989 && ent->plt.refcount > 0)
8990 ent->plt.refcount -= 1;
8991 }
8992
8993 if (tls_clear == 0)
8994 continue;
8995
8996 if ((tls_set & TLS_EXPLICIT) == 0)
8997 {
8998 struct got_entry *ent;
8999
9000 /* Adjust got entry for this reloc. */
9001 if (h != NULL)
9002 ent = h->got.glist;
9003 else
9004 ent = elf_local_got_ents (ibfd)[r_symndx];
9005
9006 for (; ent != NULL; ent = ent->next)
9007 if (ent->addend == rel->r_addend
9008 && ent->owner == ibfd
9009 && ent->tls_type == tls_type)
9010 break;
9011 if (ent == NULL)
9012 abort ();
9013
9014 if (tls_set == 0)
9015 {
9016 /* We managed to get rid of a got entry. */
9017 if (ent->got.refcount > 0)
9018 ent->got.refcount -= 1;
9019 }
9020 }
9021 else
9022 {
9023 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9024 we'll lose one or two dyn relocs. */
9025 if (!dec_dynrel_count (rel->r_info, sec, info,
9026 NULL, h, sym))
9027 return FALSE;
9028
9029 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9030 {
9031 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9032 NULL, h, sym))
9033 return FALSE;
9034 }
9035 }
9036
9037 *tls_mask |= tls_set;
9038 *tls_mask &= ~tls_clear;
9039 }
9040
9041 if (elf_section_data (sec)->relocs != relstart)
9042 free (relstart);
9043 }
9044
9045 if (locsyms != NULL
9046 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9047 {
9048 if (!info->keep_memory)
9049 free (locsyms);
9050 else
9051 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9052 }
9053 }
9054
9055 if (toc_ref != NULL)
9056 free (toc_ref);
9057 htab->do_tls_opt = 1;
9058 return TRUE;
9059 }
9060
9061 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9062 the values of any global symbols in a toc section that has been
9063 edited. Globals in toc sections should be a rarity, so this function
9064 sets a flag if any are found in toc sections other than the one just
9065 edited, so that further hash table traversals can be avoided. */
9066
9067 struct adjust_toc_info
9068 {
9069 asection *toc;
9070 unsigned long *skip;
9071 bfd_boolean global_toc_syms;
9072 };
9073
9074 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9075
9076 static bfd_boolean
9077 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9078 {
9079 struct ppc_link_hash_entry *eh;
9080 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9081 unsigned long i;
9082
9083 if (h->root.type != bfd_link_hash_defined
9084 && h->root.type != bfd_link_hash_defweak)
9085 return TRUE;
9086
9087 eh = (struct ppc_link_hash_entry *) h;
9088 if (eh->adjust_done)
9089 return TRUE;
9090
9091 if (eh->elf.root.u.def.section == toc_inf->toc)
9092 {
9093 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9094 i = toc_inf->toc->rawsize >> 3;
9095 else
9096 i = eh->elf.root.u.def.value >> 3;
9097
9098 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9099 {
9100 _bfd_error_handler
9101 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9102 do
9103 ++i;
9104 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9105 eh->elf.root.u.def.value = (bfd_vma) i << 3;
9106 }
9107
9108 eh->elf.root.u.def.value -= toc_inf->skip[i];
9109 eh->adjust_done = 1;
9110 }
9111 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9112 toc_inf->global_toc_syms = TRUE;
9113
9114 return TRUE;
9115 }
9116
9117 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9118 on a _LO variety toc/got reloc. */
9119
9120 static bfd_boolean
9121 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9122 {
9123 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9124 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9125 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9126 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9127 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9128 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9129 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9130 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9131 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9132 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9133 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9134 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9135 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9136 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9137 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9138 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9139 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9140 /* Exclude lfqu by testing reloc. If relocs are ever
9141 defined for the reduced D field in psq_lu then those
9142 will need testing too. */
9143 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9144 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9145 && (insn & 1) == 0)
9146 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9147 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9148 /* Exclude stfqu. psq_stu as above for psq_lu. */
9149 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9150 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9151 && (insn & 1) == 0));
9152 }
9153
9154 /* Examine all relocs referencing .toc sections in order to remove
9155 unused .toc entries. */
9156
9157 bfd_boolean
9158 ppc64_elf_edit_toc (struct bfd_link_info *info)
9159 {
9160 bfd *ibfd;
9161 struct adjust_toc_info toc_inf;
9162 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9163
9164 htab->do_toc_opt = 1;
9165 toc_inf.global_toc_syms = TRUE;
9166 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9167 {
9168 asection *toc, *sec;
9169 Elf_Internal_Shdr *symtab_hdr;
9170 Elf_Internal_Sym *local_syms;
9171 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9172 unsigned long *skip, *drop;
9173 unsigned char *used;
9174 unsigned char *keep, last, some_unused;
9175
9176 if (!is_ppc64_elf (ibfd))
9177 continue;
9178
9179 toc = bfd_get_section_by_name (ibfd, ".toc");
9180 if (toc == NULL
9181 || toc->size == 0
9182 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9183 || discarded_section (toc))
9184 continue;
9185
9186 toc_relocs = NULL;
9187 local_syms = NULL;
9188 symtab_hdr = &elf_symtab_hdr (ibfd);
9189
9190 /* Look at sections dropped from the final link. */
9191 skip = NULL;
9192 relstart = NULL;
9193 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9194 {
9195 if (sec->reloc_count == 0
9196 || !discarded_section (sec)
9197 || get_opd_info (sec)
9198 || (sec->flags & SEC_ALLOC) == 0
9199 || (sec->flags & SEC_DEBUGGING) != 0)
9200 continue;
9201
9202 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9203 if (relstart == NULL)
9204 goto error_ret;
9205
9206 /* Run through the relocs to see which toc entries might be
9207 unused. */
9208 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9209 {
9210 enum elf_ppc64_reloc_type r_type;
9211 unsigned long r_symndx;
9212 asection *sym_sec;
9213 struct elf_link_hash_entry *h;
9214 Elf_Internal_Sym *sym;
9215 bfd_vma val;
9216
9217 r_type = ELF64_R_TYPE (rel->r_info);
9218 switch (r_type)
9219 {
9220 default:
9221 continue;
9222
9223 case R_PPC64_TOC16:
9224 case R_PPC64_TOC16_LO:
9225 case R_PPC64_TOC16_HI:
9226 case R_PPC64_TOC16_HA:
9227 case R_PPC64_TOC16_DS:
9228 case R_PPC64_TOC16_LO_DS:
9229 break;
9230 }
9231
9232 r_symndx = ELF64_R_SYM (rel->r_info);
9233 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9234 r_symndx, ibfd))
9235 goto error_ret;
9236
9237 if (sym_sec != toc)
9238 continue;
9239
9240 if (h != NULL)
9241 val = h->root.u.def.value;
9242 else
9243 val = sym->st_value;
9244 val += rel->r_addend;
9245
9246 if (val >= toc->size)
9247 continue;
9248
9249 /* Anything in the toc ought to be aligned to 8 bytes.
9250 If not, don't mark as unused. */
9251 if (val & 7)
9252 continue;
9253
9254 if (skip == NULL)
9255 {
9256 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9257 if (skip == NULL)
9258 goto error_ret;
9259 }
9260
9261 skip[val >> 3] = ref_from_discarded;
9262 }
9263
9264 if (elf_section_data (sec)->relocs != relstart)
9265 free (relstart);
9266 }
9267
9268 /* For largetoc loads of address constants, we can convert
9269 . addis rx,2,addr@got@ha
9270 . ld ry,addr@got@l(rx)
9271 to
9272 . addis rx,2,addr@toc@ha
9273 . addi ry,rx,addr@toc@l
9274 when addr is within 2G of the toc pointer. This then means
9275 that the word storing "addr" in the toc is no longer needed. */
9276
9277 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9278 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9279 && toc->reloc_count != 0)
9280 {
9281 /* Read toc relocs. */
9282 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9283 info->keep_memory);
9284 if (toc_relocs == NULL)
9285 goto error_ret;
9286
9287 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9288 {
9289 enum elf_ppc64_reloc_type r_type;
9290 unsigned long r_symndx;
9291 asection *sym_sec;
9292 struct elf_link_hash_entry *h;
9293 Elf_Internal_Sym *sym;
9294 bfd_vma val, addr;
9295
9296 r_type = ELF64_R_TYPE (rel->r_info);
9297 if (r_type != R_PPC64_ADDR64)
9298 continue;
9299
9300 r_symndx = ELF64_R_SYM (rel->r_info);
9301 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9302 r_symndx, ibfd))
9303 goto error_ret;
9304
9305 if (sym_sec == NULL
9306 || sym_sec->output_section == NULL
9307 || discarded_section (sym_sec))
9308 continue;
9309
9310 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9311 continue;
9312
9313 if (h != NULL)
9314 {
9315 if (h->type == STT_GNU_IFUNC)
9316 continue;
9317 val = h->root.u.def.value;
9318 }
9319 else
9320 {
9321 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9322 continue;
9323 val = sym->st_value;
9324 }
9325 val += rel->r_addend;
9326 val += sym_sec->output_section->vma + sym_sec->output_offset;
9327
9328 /* We don't yet know the exact toc pointer value, but we
9329 know it will be somewhere in the toc section. Don't
9330 optimize if the difference from any possible toc
9331 pointer is outside [ff..f80008000, 7fff7fff]. */
9332 addr = toc->output_section->vma + TOC_BASE_OFF;
9333 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9334 continue;
9335
9336 addr = toc->output_section->vma + toc->output_section->rawsize;
9337 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9338 continue;
9339
9340 if (skip == NULL)
9341 {
9342 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9343 if (skip == NULL)
9344 goto error_ret;
9345 }
9346
9347 skip[rel->r_offset >> 3]
9348 |= can_optimize | ((rel - toc_relocs) << 2);
9349 }
9350 }
9351
9352 if (skip == NULL)
9353 continue;
9354
9355 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9356 if (used == NULL)
9357 {
9358 error_ret:
9359 if (local_syms != NULL
9360 && symtab_hdr->contents != (unsigned char *) local_syms)
9361 free (local_syms);
9362 if (sec != NULL
9363 && relstart != NULL
9364 && elf_section_data (sec)->relocs != relstart)
9365 free (relstart);
9366 if (toc_relocs != NULL
9367 && elf_section_data (toc)->relocs != toc_relocs)
9368 free (toc_relocs);
9369 if (skip != NULL)
9370 free (skip);
9371 return FALSE;
9372 }
9373
9374 /* Now check all kept sections that might reference the toc.
9375 Check the toc itself last. */
9376 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9377 : ibfd->sections);
9378 sec != NULL;
9379 sec = (sec == toc ? NULL
9380 : sec->next == NULL ? toc
9381 : sec->next == toc && toc->next ? toc->next
9382 : sec->next))
9383 {
9384 int repeat;
9385
9386 if (sec->reloc_count == 0
9387 || discarded_section (sec)
9388 || get_opd_info (sec)
9389 || (sec->flags & SEC_ALLOC) == 0
9390 || (sec->flags & SEC_DEBUGGING) != 0)
9391 continue;
9392
9393 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9394 info->keep_memory);
9395 if (relstart == NULL)
9396 {
9397 free (used);
9398 goto error_ret;
9399 }
9400
9401 /* Mark toc entries referenced as used. */
9402 do
9403 {
9404 repeat = 0;
9405 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9406 {
9407 enum elf_ppc64_reloc_type r_type;
9408 unsigned long r_symndx;
9409 asection *sym_sec;
9410 struct elf_link_hash_entry *h;
9411 Elf_Internal_Sym *sym;
9412 bfd_vma val;
9413 enum {no_check, check_lo, check_ha} insn_check;
9414
9415 r_type = ELF64_R_TYPE (rel->r_info);
9416 switch (r_type)
9417 {
9418 default:
9419 insn_check = no_check;
9420 break;
9421
9422 case R_PPC64_GOT_TLSLD16_HA:
9423 case R_PPC64_GOT_TLSGD16_HA:
9424 case R_PPC64_GOT_TPREL16_HA:
9425 case R_PPC64_GOT_DTPREL16_HA:
9426 case R_PPC64_GOT16_HA:
9427 case R_PPC64_TOC16_HA:
9428 insn_check = check_ha;
9429 break;
9430
9431 case R_PPC64_GOT_TLSLD16_LO:
9432 case R_PPC64_GOT_TLSGD16_LO:
9433 case R_PPC64_GOT_TPREL16_LO_DS:
9434 case R_PPC64_GOT_DTPREL16_LO_DS:
9435 case R_PPC64_GOT16_LO:
9436 case R_PPC64_GOT16_LO_DS:
9437 case R_PPC64_TOC16_LO:
9438 case R_PPC64_TOC16_LO_DS:
9439 insn_check = check_lo;
9440 break;
9441 }
9442
9443 if (insn_check != no_check)
9444 {
9445 bfd_vma off = rel->r_offset & ~3;
9446 unsigned char buf[4];
9447 unsigned int insn;
9448
9449 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9450 {
9451 free (used);
9452 goto error_ret;
9453 }
9454 insn = bfd_get_32 (ibfd, buf);
9455 if (insn_check == check_lo
9456 ? !ok_lo_toc_insn (insn, r_type)
9457 : ((insn & ((0x3f << 26) | 0x1f << 16))
9458 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9459 {
9460 char str[12];
9461
9462 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9463 sprintf (str, "%#08x", insn);
9464 info->callbacks->einfo
9465 /* xgettext:c-format */
9466 (_("%H: toc optimization is not supported for"
9467 " %s instruction\n"),
9468 ibfd, sec, rel->r_offset & ~3, str);
9469 }
9470 }
9471
9472 switch (r_type)
9473 {
9474 case R_PPC64_TOC16:
9475 case R_PPC64_TOC16_LO:
9476 case R_PPC64_TOC16_HI:
9477 case R_PPC64_TOC16_HA:
9478 case R_PPC64_TOC16_DS:
9479 case R_PPC64_TOC16_LO_DS:
9480 /* In case we're taking addresses of toc entries. */
9481 case R_PPC64_ADDR64:
9482 break;
9483
9484 default:
9485 continue;
9486 }
9487
9488 r_symndx = ELF64_R_SYM (rel->r_info);
9489 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9490 r_symndx, ibfd))
9491 {
9492 free (used);
9493 goto error_ret;
9494 }
9495
9496 if (sym_sec != toc)
9497 continue;
9498
9499 if (h != NULL)
9500 val = h->root.u.def.value;
9501 else
9502 val = sym->st_value;
9503 val += rel->r_addend;
9504
9505 if (val >= toc->size)
9506 continue;
9507
9508 if ((skip[val >> 3] & can_optimize) != 0)
9509 {
9510 bfd_vma off;
9511 unsigned char opc;
9512
9513 switch (r_type)
9514 {
9515 case R_PPC64_TOC16_HA:
9516 break;
9517
9518 case R_PPC64_TOC16_LO_DS:
9519 off = rel->r_offset;
9520 off += (bfd_big_endian (ibfd) ? -2 : 3);
9521 if (!bfd_get_section_contents (ibfd, sec, &opc,
9522 off, 1))
9523 {
9524 free (used);
9525 goto error_ret;
9526 }
9527 if ((opc & (0x3f << 2)) == (58u << 2))
9528 break;
9529 /* Fall through. */
9530
9531 default:
9532 /* Wrong sort of reloc, or not a ld. We may
9533 as well clear ref_from_discarded too. */
9534 skip[val >> 3] = 0;
9535 }
9536 }
9537
9538 if (sec != toc)
9539 used[val >> 3] = 1;
9540 /* For the toc section, we only mark as used if this
9541 entry itself isn't unused. */
9542 else if ((used[rel->r_offset >> 3]
9543 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9544 && !used[val >> 3])
9545 {
9546 /* Do all the relocs again, to catch reference
9547 chains. */
9548 repeat = 1;
9549 used[val >> 3] = 1;
9550 }
9551 }
9552 }
9553 while (repeat);
9554
9555 if (elf_section_data (sec)->relocs != relstart)
9556 free (relstart);
9557 }
9558
9559 /* Merge the used and skip arrays. Assume that TOC
9560 doublewords not appearing as either used or unused belong
9561 to an entry more than one doubleword in size. */
9562 for (drop = skip, keep = used, last = 0, some_unused = 0;
9563 drop < skip + (toc->size + 7) / 8;
9564 ++drop, ++keep)
9565 {
9566 if (*keep)
9567 {
9568 *drop &= ~ref_from_discarded;
9569 if ((*drop & can_optimize) != 0)
9570 some_unused = 1;
9571 last = 0;
9572 }
9573 else if ((*drop & ref_from_discarded) != 0)
9574 {
9575 some_unused = 1;
9576 last = ref_from_discarded;
9577 }
9578 else
9579 *drop = last;
9580 }
9581
9582 free (used);
9583
9584 if (some_unused)
9585 {
9586 bfd_byte *contents, *src;
9587 unsigned long off;
9588 Elf_Internal_Sym *sym;
9589 bfd_boolean local_toc_syms = FALSE;
9590
9591 /* Shuffle the toc contents, and at the same time convert the
9592 skip array from booleans into offsets. */
9593 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9594 goto error_ret;
9595
9596 elf_section_data (toc)->this_hdr.contents = contents;
9597
9598 for (src = contents, off = 0, drop = skip;
9599 src < contents + toc->size;
9600 src += 8, ++drop)
9601 {
9602 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9603 off += 8;
9604 else if (off != 0)
9605 {
9606 *drop = off;
9607 memcpy (src - off, src, 8);
9608 }
9609 }
9610 *drop = off;
9611 toc->rawsize = toc->size;
9612 toc->size = src - contents - off;
9613
9614 /* Adjust addends for relocs against the toc section sym,
9615 and optimize any accesses we can. */
9616 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9617 {
9618 if (sec->reloc_count == 0
9619 || discarded_section (sec))
9620 continue;
9621
9622 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9623 info->keep_memory);
9624 if (relstart == NULL)
9625 goto error_ret;
9626
9627 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9628 {
9629 enum elf_ppc64_reloc_type r_type;
9630 unsigned long r_symndx;
9631 asection *sym_sec;
9632 struct elf_link_hash_entry *h;
9633 bfd_vma val;
9634
9635 r_type = ELF64_R_TYPE (rel->r_info);
9636 switch (r_type)
9637 {
9638 default:
9639 continue;
9640
9641 case R_PPC64_TOC16:
9642 case R_PPC64_TOC16_LO:
9643 case R_PPC64_TOC16_HI:
9644 case R_PPC64_TOC16_HA:
9645 case R_PPC64_TOC16_DS:
9646 case R_PPC64_TOC16_LO_DS:
9647 case R_PPC64_ADDR64:
9648 break;
9649 }
9650
9651 r_symndx = ELF64_R_SYM (rel->r_info);
9652 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9653 r_symndx, ibfd))
9654 goto error_ret;
9655
9656 if (sym_sec != toc)
9657 continue;
9658
9659 if (h != NULL)
9660 val = h->root.u.def.value;
9661 else
9662 {
9663 val = sym->st_value;
9664 if (val != 0)
9665 local_toc_syms = TRUE;
9666 }
9667
9668 val += rel->r_addend;
9669
9670 if (val > toc->rawsize)
9671 val = toc->rawsize;
9672 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9673 continue;
9674 else if ((skip[val >> 3] & can_optimize) != 0)
9675 {
9676 Elf_Internal_Rela *tocrel
9677 = toc_relocs + (skip[val >> 3] >> 2);
9678 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9679
9680 switch (r_type)
9681 {
9682 case R_PPC64_TOC16_HA:
9683 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9684 break;
9685
9686 case R_PPC64_TOC16_LO_DS:
9687 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9688 break;
9689
9690 default:
9691 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9692 ppc_howto_init ();
9693 info->callbacks->einfo
9694 /* xgettext:c-format */
9695 (_("%H: %s references "
9696 "optimized away TOC entry\n"),
9697 ibfd, sec, rel->r_offset,
9698 ppc64_elf_howto_table[r_type]->name);
9699 bfd_set_error (bfd_error_bad_value);
9700 goto error_ret;
9701 }
9702 rel->r_addend = tocrel->r_addend;
9703 elf_section_data (sec)->relocs = relstart;
9704 continue;
9705 }
9706
9707 if (h != NULL || sym->st_value != 0)
9708 continue;
9709
9710 rel->r_addend -= skip[val >> 3];
9711 elf_section_data (sec)->relocs = relstart;
9712 }
9713
9714 if (elf_section_data (sec)->relocs != relstart)
9715 free (relstart);
9716 }
9717
9718 /* We shouldn't have local or global symbols defined in the TOC,
9719 but handle them anyway. */
9720 if (local_syms != NULL)
9721 for (sym = local_syms;
9722 sym < local_syms + symtab_hdr->sh_info;
9723 ++sym)
9724 if (sym->st_value != 0
9725 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9726 {
9727 unsigned long i;
9728
9729 if (sym->st_value > toc->rawsize)
9730 i = toc->rawsize >> 3;
9731 else
9732 i = sym->st_value >> 3;
9733
9734 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9735 {
9736 if (local_toc_syms)
9737 _bfd_error_handler
9738 (_("%s defined on removed toc entry"),
9739 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9740 do
9741 ++i;
9742 while ((skip[i] & (ref_from_discarded | can_optimize)));
9743 sym->st_value = (bfd_vma) i << 3;
9744 }
9745
9746 sym->st_value -= skip[i];
9747 symtab_hdr->contents = (unsigned char *) local_syms;
9748 }
9749
9750 /* Adjust any global syms defined in this toc input section. */
9751 if (toc_inf.global_toc_syms)
9752 {
9753 toc_inf.toc = toc;
9754 toc_inf.skip = skip;
9755 toc_inf.global_toc_syms = FALSE;
9756 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9757 &toc_inf);
9758 }
9759
9760 if (toc->reloc_count != 0)
9761 {
9762 Elf_Internal_Shdr *rel_hdr;
9763 Elf_Internal_Rela *wrel;
9764 bfd_size_type sz;
9765
9766 /* Remove unused toc relocs, and adjust those we keep. */
9767 if (toc_relocs == NULL)
9768 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9769 info->keep_memory);
9770 if (toc_relocs == NULL)
9771 goto error_ret;
9772
9773 wrel = toc_relocs;
9774 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9775 if ((skip[rel->r_offset >> 3]
9776 & (ref_from_discarded | can_optimize)) == 0)
9777 {
9778 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9779 wrel->r_info = rel->r_info;
9780 wrel->r_addend = rel->r_addend;
9781 ++wrel;
9782 }
9783 else if (!dec_dynrel_count (rel->r_info, toc, info,
9784 &local_syms, NULL, NULL))
9785 goto error_ret;
9786
9787 elf_section_data (toc)->relocs = toc_relocs;
9788 toc->reloc_count = wrel - toc_relocs;
9789 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9790 sz = rel_hdr->sh_entsize;
9791 rel_hdr->sh_size = toc->reloc_count * sz;
9792 }
9793 }
9794 else if (toc_relocs != NULL
9795 && elf_section_data (toc)->relocs != toc_relocs)
9796 free (toc_relocs);
9797
9798 if (local_syms != NULL
9799 && symtab_hdr->contents != (unsigned char *) local_syms)
9800 {
9801 if (!info->keep_memory)
9802 free (local_syms);
9803 else
9804 symtab_hdr->contents = (unsigned char *) local_syms;
9805 }
9806 free (skip);
9807 }
9808
9809 return TRUE;
9810 }
9811
9812 /* Return true iff input section I references the TOC using
9813 instructions limited to +/-32k offsets. */
9814
9815 bfd_boolean
9816 ppc64_elf_has_small_toc_reloc (asection *i)
9817 {
9818 return (is_ppc64_elf (i->owner)
9819 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9820 }
9821
9822 /* Allocate space for one GOT entry. */
9823
9824 static void
9825 allocate_got (struct elf_link_hash_entry *h,
9826 struct bfd_link_info *info,
9827 struct got_entry *gent)
9828 {
9829 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9830 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9831 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9832 ? 16 : 8);
9833 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9834 ? 2 : 1) * sizeof (Elf64_External_Rela);
9835 asection *got = ppc64_elf_tdata (gent->owner)->got;
9836
9837 gent->got.offset = got->size;
9838 got->size += entsize;
9839
9840 if (h->type == STT_GNU_IFUNC)
9841 {
9842 htab->elf.irelplt->size += rentsize;
9843 htab->got_reli_size += rentsize;
9844 }
9845 else if (((bfd_link_pic (info)
9846 && !((gent->tls_type & TLS_TPREL) != 0
9847 && bfd_link_executable (info)
9848 && SYMBOL_REFERENCES_LOCAL (info, h)))
9849 || (htab->elf.dynamic_sections_created
9850 && h->dynindx != -1
9851 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9852 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9853 {
9854 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9855 relgot->size += rentsize;
9856 }
9857 }
9858
9859 /* This function merges got entries in the same toc group. */
9860
9861 static void
9862 merge_got_entries (struct got_entry **pent)
9863 {
9864 struct got_entry *ent, *ent2;
9865
9866 for (ent = *pent; ent != NULL; ent = ent->next)
9867 if (!ent->is_indirect)
9868 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9869 if (!ent2->is_indirect
9870 && ent2->addend == ent->addend
9871 && ent2->tls_type == ent->tls_type
9872 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9873 {
9874 ent2->is_indirect = TRUE;
9875 ent2->got.ent = ent;
9876 }
9877 }
9878
9879 /* If H is undefined, make it dynamic if that makes sense. */
9880
9881 static bfd_boolean
9882 ensure_undef_dynamic (struct bfd_link_info *info,
9883 struct elf_link_hash_entry *h)
9884 {
9885 struct elf_link_hash_table *htab = elf_hash_table (info);
9886
9887 if (htab->dynamic_sections_created
9888 && ((info->dynamic_undefined_weak != 0
9889 && h->root.type == bfd_link_hash_undefweak)
9890 || h->root.type == bfd_link_hash_undefined)
9891 && h->dynindx == -1
9892 && !h->forced_local
9893 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9894 return bfd_elf_link_record_dynamic_symbol (info, h);
9895 return TRUE;
9896 }
9897
9898 /* Allocate space in .plt, .got and associated reloc sections for
9899 dynamic relocs. */
9900
9901 static bfd_boolean
9902 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9903 {
9904 struct bfd_link_info *info;
9905 struct ppc_link_hash_table *htab;
9906 asection *s;
9907 struct ppc_link_hash_entry *eh;
9908 struct got_entry **pgent, *gent;
9909
9910 if (h->root.type == bfd_link_hash_indirect)
9911 return TRUE;
9912
9913 info = (struct bfd_link_info *) inf;
9914 htab = ppc_hash_table (info);
9915 if (htab == NULL)
9916 return FALSE;
9917
9918 eh = (struct ppc_link_hash_entry *) h;
9919 /* Run through the TLS GD got entries first if we're changing them
9920 to TPREL. */
9921 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
9922 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9923 if (gent->got.refcount > 0
9924 && (gent->tls_type & TLS_GD) != 0)
9925 {
9926 /* This was a GD entry that has been converted to TPREL. If
9927 there happens to be a TPREL entry we can use that one. */
9928 struct got_entry *ent;
9929 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9930 if (ent->got.refcount > 0
9931 && (ent->tls_type & TLS_TPREL) != 0
9932 && ent->addend == gent->addend
9933 && ent->owner == gent->owner)
9934 {
9935 gent->got.refcount = 0;
9936 break;
9937 }
9938
9939 /* If not, then we'll be using our own TPREL entry. */
9940 if (gent->got.refcount != 0)
9941 gent->tls_type = TLS_TLS | TLS_TPREL;
9942 }
9943
9944 /* Remove any list entry that won't generate a word in the GOT before
9945 we call merge_got_entries. Otherwise we risk merging to empty
9946 entries. */
9947 pgent = &h->got.glist;
9948 while ((gent = *pgent) != NULL)
9949 if (gent->got.refcount > 0)
9950 {
9951 if ((gent->tls_type & TLS_LD) != 0
9952 && !h->def_dynamic)
9953 {
9954 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9955 *pgent = gent->next;
9956 }
9957 else
9958 pgent = &gent->next;
9959 }
9960 else
9961 *pgent = gent->next;
9962
9963 if (!htab->do_multi_toc)
9964 merge_got_entries (&h->got.glist);
9965
9966 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9967 if (!gent->is_indirect)
9968 {
9969 /* Make sure this symbol is output as a dynamic symbol. */
9970 if (!ensure_undef_dynamic (info, h))
9971 return FALSE;
9972
9973 if (!is_ppc64_elf (gent->owner))
9974 abort ();
9975
9976 allocate_got (h, info, gent);
9977 }
9978
9979 /* If no dynamic sections we can't have dynamic relocs, except for
9980 IFUNCs which are handled even in static executables. */
9981 if (!htab->elf.dynamic_sections_created
9982 && h->type != STT_GNU_IFUNC)
9983 eh->dyn_relocs = NULL;
9984
9985 /* Discard relocs on undefined symbols that must be local. */
9986 else if (h->root.type == bfd_link_hash_undefined
9987 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9988 eh->dyn_relocs = NULL;
9989
9990 /* Also discard relocs on undefined weak syms with non-default
9991 visibility, or when dynamic_undefined_weak says so. */
9992 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9993 eh->dyn_relocs = NULL;
9994
9995 if (eh->dyn_relocs != NULL)
9996 {
9997 struct elf_dyn_relocs *p, **pp;
9998
9999 /* In the shared -Bsymbolic case, discard space allocated for
10000 dynamic pc-relative relocs against symbols which turn out to
10001 be defined in regular objects. For the normal shared case,
10002 discard space for relocs that have become local due to symbol
10003 visibility changes. */
10004
10005 if (bfd_link_pic (info))
10006 {
10007 /* Relocs that use pc_count are those that appear on a call
10008 insn, or certain REL relocs (see must_be_dyn_reloc) that
10009 can be generated via assembly. We want calls to
10010 protected symbols to resolve directly to the function
10011 rather than going via the plt. If people want function
10012 pointer comparisons to work as expected then they should
10013 avoid writing weird assembly. */
10014 if (SYMBOL_CALLS_LOCAL (info, h))
10015 {
10016 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10017 {
10018 p->count -= p->pc_count;
10019 p->pc_count = 0;
10020 if (p->count == 0)
10021 *pp = p->next;
10022 else
10023 pp = &p->next;
10024 }
10025 }
10026
10027 if (eh->dyn_relocs != NULL)
10028 {
10029 /* Make sure this symbol is output as a dynamic symbol. */
10030 if (!ensure_undef_dynamic (info, h))
10031 return FALSE;
10032 }
10033 }
10034 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10035 {
10036 /* For the non-pic case, discard space for relocs against
10037 symbols which turn out to need copy relocs or are not
10038 dynamic. */
10039 if (h->dynamic_adjusted
10040 && !h->def_regular
10041 && !ELF_COMMON_DEF_P (h))
10042 {
10043 /* Make sure this symbol is output as a dynamic symbol. */
10044 if (!ensure_undef_dynamic (info, h))
10045 return FALSE;
10046
10047 if (h->dynindx == -1)
10048 eh->dyn_relocs = NULL;
10049 }
10050 else
10051 eh->dyn_relocs = NULL;
10052 }
10053
10054 /* Finally, allocate space. */
10055 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10056 {
10057 asection *sreloc = elf_section_data (p->sec)->sreloc;
10058 if (eh->elf.type == STT_GNU_IFUNC)
10059 sreloc = htab->elf.irelplt;
10060 sreloc->size += p->count * sizeof (Elf64_External_Rela);
10061 }
10062 }
10063
10064 /* We might need a PLT entry when the symbol
10065 a) is dynamic, or
10066 b) is an ifunc, or
10067 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10068 d) has plt16 relocs and we are linking statically. */
10069 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10070 || h->type == STT_GNU_IFUNC
10071 || (h->needs_plt && h->dynamic_adjusted)
10072 || (h->needs_plt
10073 && h->def_regular
10074 && !htab->elf.dynamic_sections_created
10075 && !htab->can_convert_all_inline_plt
10076 && (((struct ppc_link_hash_entry *) h)->tls_mask
10077 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10078 {
10079 struct plt_entry *pent;
10080 bfd_boolean doneone = FALSE;
10081 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10082 if (pent->plt.refcount > 0)
10083 {
10084 if (!htab->elf.dynamic_sections_created
10085 || h->dynindx == -1)
10086 {
10087 if (h->type == STT_GNU_IFUNC)
10088 {
10089 s = htab->elf.iplt;
10090 pent->plt.offset = s->size;
10091 s->size += PLT_ENTRY_SIZE (htab);
10092 s = htab->elf.irelplt;
10093 }
10094 else
10095 {
10096 s = htab->pltlocal;
10097 pent->plt.offset = s->size;
10098 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10099 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10100 }
10101 }
10102 else
10103 {
10104 /* If this is the first .plt entry, make room for the special
10105 first entry. */
10106 s = htab->elf.splt;
10107 if (s->size == 0)
10108 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10109
10110 pent->plt.offset = s->size;
10111
10112 /* Make room for this entry. */
10113 s->size += PLT_ENTRY_SIZE (htab);
10114
10115 /* Make room for the .glink code. */
10116 s = htab->glink;
10117 if (s->size == 0)
10118 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10119 if (htab->opd_abi)
10120 {
10121 /* We need bigger stubs past index 32767. */
10122 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10123 s->size += 4;
10124 s->size += 2*4;
10125 }
10126 else
10127 s->size += 4;
10128
10129 /* We also need to make an entry in the .rela.plt section. */
10130 s = htab->elf.srelplt;
10131 }
10132 if (s != NULL)
10133 s->size += sizeof (Elf64_External_Rela);
10134 doneone = TRUE;
10135 }
10136 else
10137 pent->plt.offset = (bfd_vma) -1;
10138 if (!doneone)
10139 {
10140 h->plt.plist = NULL;
10141 h->needs_plt = 0;
10142 }
10143 }
10144 else
10145 {
10146 h->plt.plist = NULL;
10147 h->needs_plt = 0;
10148 }
10149
10150 return TRUE;
10151 }
10152
10153 #define PPC_LO(v) ((v) & 0xffff)
10154 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10155 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10156
10157 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10158 to set up space for global entry stubs. These are put in glink,
10159 after the branch table. */
10160
10161 static bfd_boolean
10162 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10163 {
10164 struct bfd_link_info *info;
10165 struct ppc_link_hash_table *htab;
10166 struct plt_entry *pent;
10167 asection *s, *plt;
10168
10169 if (h->root.type == bfd_link_hash_indirect)
10170 return TRUE;
10171
10172 if (!h->pointer_equality_needed)
10173 return TRUE;
10174
10175 if (h->def_regular)
10176 return TRUE;
10177
10178 info = inf;
10179 htab = ppc_hash_table (info);
10180 if (htab == NULL)
10181 return FALSE;
10182
10183 s = htab->global_entry;
10184 plt = htab->elf.splt;
10185 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10186 if (pent->plt.offset != (bfd_vma) -1
10187 && pent->addend == 0)
10188 {
10189 /* For ELFv2, if this symbol is not defined in a regular file
10190 and we are not generating a shared library or pie, then we
10191 need to define the symbol in the executable on a call stub.
10192 This is to avoid text relocations. */
10193 bfd_vma off, stub_align, stub_off, stub_size;
10194 unsigned int align_power;
10195
10196 stub_size = 16;
10197 stub_off = s->size;
10198 if (htab->params->plt_stub_align >= 0)
10199 align_power = htab->params->plt_stub_align;
10200 else
10201 align_power = -htab->params->plt_stub_align;
10202 /* Setting section alignment is delayed until we know it is
10203 non-empty. Otherwise the .text output section will be
10204 aligned at least to plt_stub_align even when no global
10205 entry stubs are needed. */
10206 if (s->alignment_power < align_power)
10207 s->alignment_power = align_power;
10208 stub_align = (bfd_vma) 1 << align_power;
10209 if (htab->params->plt_stub_align >= 0
10210 || ((((stub_off + stub_size - 1) & -stub_align)
10211 - (stub_off & -stub_align))
10212 > ((stub_size - 1) & -stub_align)))
10213 stub_off = (stub_off + stub_align - 1) & -stub_align;
10214 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10215 off -= stub_off + s->output_offset + s->output_section->vma;
10216 /* Note that for --plt-stub-align negative we have a possible
10217 dependency between stub offset and size. Break that
10218 dependency by assuming the max stub size when calculating
10219 the stub offset. */
10220 if (PPC_HA (off) == 0)
10221 stub_size -= 4;
10222 h->root.type = bfd_link_hash_defined;
10223 h->root.u.def.section = s;
10224 h->root.u.def.value = stub_off;
10225 s->size = stub_off + stub_size;
10226 break;
10227 }
10228 return TRUE;
10229 }
10230
10231 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10232 read-only sections. */
10233
10234 static bfd_boolean
10235 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10236 {
10237 asection *sec;
10238
10239 if (h->root.type == bfd_link_hash_indirect)
10240 return TRUE;
10241
10242 sec = readonly_dynrelocs (h);
10243 if (sec != NULL)
10244 {
10245 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10246
10247 info->flags |= DF_TEXTREL;
10248 info->callbacks->minfo
10249 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10250 sec->owner, h->root.root.string, sec);
10251
10252 /* Not an error, just cut short the traversal. */
10253 return FALSE;
10254 }
10255 return TRUE;
10256 }
10257
10258 /* Set the sizes of the dynamic sections. */
10259
10260 static bfd_boolean
10261 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10262 struct bfd_link_info *info)
10263 {
10264 struct ppc_link_hash_table *htab;
10265 bfd *dynobj;
10266 asection *s;
10267 bfd_boolean relocs;
10268 bfd *ibfd;
10269 struct got_entry *first_tlsld;
10270
10271 htab = ppc_hash_table (info);
10272 if (htab == NULL)
10273 return FALSE;
10274
10275 dynobj = htab->elf.dynobj;
10276 if (dynobj == NULL)
10277 abort ();
10278
10279 if (htab->elf.dynamic_sections_created)
10280 {
10281 /* Set the contents of the .interp section to the interpreter. */
10282 if (bfd_link_executable (info) && !info->nointerp)
10283 {
10284 s = bfd_get_linker_section (dynobj, ".interp");
10285 if (s == NULL)
10286 abort ();
10287 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10288 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10289 }
10290 }
10291
10292 /* Set up .got offsets for local syms, and space for local dynamic
10293 relocs. */
10294 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10295 {
10296 struct got_entry **lgot_ents;
10297 struct got_entry **end_lgot_ents;
10298 struct plt_entry **local_plt;
10299 struct plt_entry **end_local_plt;
10300 unsigned char *lgot_masks;
10301 bfd_size_type locsymcount;
10302 Elf_Internal_Shdr *symtab_hdr;
10303
10304 if (!is_ppc64_elf (ibfd))
10305 continue;
10306
10307 for (s = ibfd->sections; s != NULL; s = s->next)
10308 {
10309 struct ppc_dyn_relocs *p;
10310
10311 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10312 {
10313 if (!bfd_is_abs_section (p->sec)
10314 && bfd_is_abs_section (p->sec->output_section))
10315 {
10316 /* Input section has been discarded, either because
10317 it is a copy of a linkonce section or due to
10318 linker script /DISCARD/, so we'll be discarding
10319 the relocs too. */
10320 }
10321 else if (p->count != 0)
10322 {
10323 asection *srel = elf_section_data (p->sec)->sreloc;
10324 if (p->ifunc)
10325 srel = htab->elf.irelplt;
10326 srel->size += p->count * sizeof (Elf64_External_Rela);
10327 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10328 info->flags |= DF_TEXTREL;
10329 }
10330 }
10331 }
10332
10333 lgot_ents = elf_local_got_ents (ibfd);
10334 if (!lgot_ents)
10335 continue;
10336
10337 symtab_hdr = &elf_symtab_hdr (ibfd);
10338 locsymcount = symtab_hdr->sh_info;
10339 end_lgot_ents = lgot_ents + locsymcount;
10340 local_plt = (struct plt_entry **) end_lgot_ents;
10341 end_local_plt = local_plt + locsymcount;
10342 lgot_masks = (unsigned char *) end_local_plt;
10343 s = ppc64_elf_tdata (ibfd)->got;
10344 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10345 {
10346 struct got_entry **pent, *ent;
10347
10348 pent = lgot_ents;
10349 while ((ent = *pent) != NULL)
10350 if (ent->got.refcount > 0)
10351 {
10352 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10353 {
10354 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10355 *pent = ent->next;
10356 }
10357 else
10358 {
10359 unsigned int ent_size = 8;
10360 unsigned int rel_size = sizeof (Elf64_External_Rela);
10361
10362 ent->got.offset = s->size;
10363 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10364 {
10365 ent_size *= 2;
10366 rel_size *= 2;
10367 }
10368 s->size += ent_size;
10369 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10370 {
10371 htab->elf.irelplt->size += rel_size;
10372 htab->got_reli_size += rel_size;
10373 }
10374 else if (bfd_link_pic (info)
10375 && !((ent->tls_type & TLS_TPREL) != 0
10376 && bfd_link_executable (info)))
10377 {
10378 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10379 srel->size += rel_size;
10380 }
10381 pent = &ent->next;
10382 }
10383 }
10384 else
10385 *pent = ent->next;
10386 }
10387
10388 /* Allocate space for plt calls to local syms. */
10389 lgot_masks = (unsigned char *) end_local_plt;
10390 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10391 {
10392 struct plt_entry *ent;
10393
10394 for (ent = *local_plt; ent != NULL; ent = ent->next)
10395 if (ent->plt.refcount > 0)
10396 {
10397 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10398 {
10399 s = htab->elf.iplt;
10400 ent->plt.offset = s->size;
10401 s->size += PLT_ENTRY_SIZE (htab);
10402 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10403 }
10404 else if (htab->can_convert_all_inline_plt
10405 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10406 ent->plt.offset = (bfd_vma) -1;
10407 else
10408 {
10409 s = htab->pltlocal;
10410 ent->plt.offset = s->size;
10411 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10412 if (bfd_link_pic (info))
10413 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10414 }
10415 }
10416 else
10417 ent->plt.offset = (bfd_vma) -1;
10418 }
10419 }
10420
10421 /* Allocate global sym .plt and .got entries, and space for global
10422 sym dynamic relocs. */
10423 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10424
10425 if (!htab->opd_abi && !bfd_link_pic (info))
10426 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10427
10428 first_tlsld = NULL;
10429 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10430 {
10431 struct got_entry *ent;
10432
10433 if (!is_ppc64_elf (ibfd))
10434 continue;
10435
10436 ent = ppc64_tlsld_got (ibfd);
10437 if (ent->got.refcount > 0)
10438 {
10439 if (!htab->do_multi_toc && first_tlsld != NULL)
10440 {
10441 ent->is_indirect = TRUE;
10442 ent->got.ent = first_tlsld;
10443 }
10444 else
10445 {
10446 if (first_tlsld == NULL)
10447 first_tlsld = ent;
10448 s = ppc64_elf_tdata (ibfd)->got;
10449 ent->got.offset = s->size;
10450 ent->owner = ibfd;
10451 s->size += 16;
10452 if (bfd_link_pic (info))
10453 {
10454 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10455 srel->size += sizeof (Elf64_External_Rela);
10456 }
10457 }
10458 }
10459 else
10460 ent->got.offset = (bfd_vma) -1;
10461 }
10462
10463 /* We now have determined the sizes of the various dynamic sections.
10464 Allocate memory for them. */
10465 relocs = FALSE;
10466 for (s = dynobj->sections; s != NULL; s = s->next)
10467 {
10468 if ((s->flags & SEC_LINKER_CREATED) == 0)
10469 continue;
10470
10471 if (s == htab->brlt || s == htab->relbrlt)
10472 /* These haven't been allocated yet; don't strip. */
10473 continue;
10474 else if (s == htab->elf.sgot
10475 || s == htab->elf.splt
10476 || s == htab->elf.iplt
10477 || s == htab->pltlocal
10478 || s == htab->glink
10479 || s == htab->global_entry
10480 || s == htab->elf.sdynbss
10481 || s == htab->elf.sdynrelro)
10482 {
10483 /* Strip this section if we don't need it; see the
10484 comment below. */
10485 }
10486 else if (s == htab->glink_eh_frame)
10487 {
10488 if (!bfd_is_abs_section (s->output_section))
10489 /* Not sized yet. */
10490 continue;
10491 }
10492 else if (CONST_STRNEQ (s->name, ".rela"))
10493 {
10494 if (s->size != 0)
10495 {
10496 if (s != htab->elf.srelplt)
10497 relocs = TRUE;
10498
10499 /* We use the reloc_count field as a counter if we need
10500 to copy relocs into the output file. */
10501 s->reloc_count = 0;
10502 }
10503 }
10504 else
10505 {
10506 /* It's not one of our sections, so don't allocate space. */
10507 continue;
10508 }
10509
10510 if (s->size == 0)
10511 {
10512 /* If we don't need this section, strip it from the
10513 output file. This is mostly to handle .rela.bss and
10514 .rela.plt. We must create both sections in
10515 create_dynamic_sections, because they must be created
10516 before the linker maps input sections to output
10517 sections. The linker does that before
10518 adjust_dynamic_symbol is called, and it is that
10519 function which decides whether anything needs to go
10520 into these sections. */
10521 s->flags |= SEC_EXCLUDE;
10522 continue;
10523 }
10524
10525 if (bfd_is_abs_section (s->output_section))
10526 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10527 s->name);
10528
10529 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10530 continue;
10531
10532 /* Allocate memory for the section contents. We use bfd_zalloc
10533 here in case unused entries are not reclaimed before the
10534 section's contents are written out. This should not happen,
10535 but this way if it does we get a R_PPC64_NONE reloc in .rela
10536 sections instead of garbage.
10537 We also rely on the section contents being zero when writing
10538 the GOT and .dynrelro. */
10539 s->contents = bfd_zalloc (dynobj, s->size);
10540 if (s->contents == NULL)
10541 return FALSE;
10542 }
10543
10544 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10545 {
10546 if (!is_ppc64_elf (ibfd))
10547 continue;
10548
10549 s = ppc64_elf_tdata (ibfd)->got;
10550 if (s != NULL && s != htab->elf.sgot)
10551 {
10552 if (s->size == 0)
10553 s->flags |= SEC_EXCLUDE;
10554 else
10555 {
10556 s->contents = bfd_zalloc (ibfd, s->size);
10557 if (s->contents == NULL)
10558 return FALSE;
10559 }
10560 }
10561 s = ppc64_elf_tdata (ibfd)->relgot;
10562 if (s != NULL)
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 relocs = TRUE;
10572 s->reloc_count = 0;
10573 }
10574 }
10575 }
10576
10577 if (htab->elf.dynamic_sections_created)
10578 {
10579 bfd_boolean tls_opt;
10580
10581 /* Add some entries to the .dynamic section. We fill in the
10582 values later, in ppc64_elf_finish_dynamic_sections, but we
10583 must add the entries now so that we get the correct size for
10584 the .dynamic section. The DT_DEBUG entry is filled in by the
10585 dynamic linker and used by the debugger. */
10586 #define add_dynamic_entry(TAG, VAL) \
10587 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10588
10589 if (bfd_link_executable (info))
10590 {
10591 if (!add_dynamic_entry (DT_DEBUG, 0))
10592 return FALSE;
10593 }
10594
10595 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10596 {
10597 if (!add_dynamic_entry (DT_PLTGOT, 0)
10598 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10599 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10600 || !add_dynamic_entry (DT_JMPREL, 0)
10601 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10602 return FALSE;
10603 }
10604
10605 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10606 {
10607 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10608 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10609 return FALSE;
10610 }
10611
10612 tls_opt = (htab->params->tls_get_addr_opt
10613 && htab->tls_get_addr_fd != NULL
10614 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10615 if (tls_opt || !htab->opd_abi)
10616 {
10617 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10618 return FALSE;
10619 }
10620
10621 if (relocs)
10622 {
10623 if (!add_dynamic_entry (DT_RELA, 0)
10624 || !add_dynamic_entry (DT_RELASZ, 0)
10625 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10626 return FALSE;
10627
10628 /* If any dynamic relocs apply to a read-only section,
10629 then we need a DT_TEXTREL entry. */
10630 if ((info->flags & DF_TEXTREL) == 0)
10631 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10632
10633 if ((info->flags & DF_TEXTREL) != 0)
10634 {
10635 if (!add_dynamic_entry (DT_TEXTREL, 0))
10636 return FALSE;
10637 }
10638 }
10639 }
10640 #undef add_dynamic_entry
10641
10642 return TRUE;
10643 }
10644
10645 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10646
10647 static bfd_boolean
10648 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10649 {
10650 if (h->plt.plist != NULL
10651 && !h->def_regular
10652 && !h->pointer_equality_needed)
10653 return FALSE;
10654
10655 return _bfd_elf_hash_symbol (h);
10656 }
10657
10658 /* Determine the type of stub needed, if any, for a call. */
10659
10660 static inline enum ppc_stub_type
10661 ppc_type_of_stub (asection *input_sec,
10662 const Elf_Internal_Rela *rel,
10663 struct ppc_link_hash_entry **hash,
10664 struct plt_entry **plt_ent,
10665 bfd_vma destination,
10666 unsigned long local_off)
10667 {
10668 struct ppc_link_hash_entry *h = *hash;
10669 bfd_vma location;
10670 bfd_vma branch_offset;
10671 bfd_vma max_branch_offset;
10672 enum elf_ppc64_reloc_type r_type;
10673
10674 if (h != NULL)
10675 {
10676 struct plt_entry *ent;
10677 struct ppc_link_hash_entry *fdh = h;
10678 if (h->oh != NULL
10679 && h->oh->is_func_descriptor)
10680 {
10681 fdh = ppc_follow_link (h->oh);
10682 *hash = fdh;
10683 }
10684
10685 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10686 if (ent->addend == rel->r_addend
10687 && ent->plt.offset != (bfd_vma) -1)
10688 {
10689 *plt_ent = ent;
10690 return ppc_stub_plt_call;
10691 }
10692
10693 /* Here, we know we don't have a plt entry. If we don't have a
10694 either a defined function descriptor or a defined entry symbol
10695 in a regular object file, then it is pointless trying to make
10696 any other type of stub. */
10697 if (!is_static_defined (&fdh->elf)
10698 && !is_static_defined (&h->elf))
10699 return ppc_stub_none;
10700 }
10701 else if (elf_local_got_ents (input_sec->owner) != NULL)
10702 {
10703 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10704 struct plt_entry **local_plt = (struct plt_entry **)
10705 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10706 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10707
10708 if (local_plt[r_symndx] != NULL)
10709 {
10710 struct plt_entry *ent;
10711
10712 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10713 if (ent->addend == rel->r_addend
10714 && ent->plt.offset != (bfd_vma) -1)
10715 {
10716 *plt_ent = ent;
10717 return ppc_stub_plt_call;
10718 }
10719 }
10720 }
10721
10722 /* Determine where the call point is. */
10723 location = (input_sec->output_offset
10724 + input_sec->output_section->vma
10725 + rel->r_offset);
10726
10727 branch_offset = destination - location;
10728 r_type = ELF64_R_TYPE (rel->r_info);
10729
10730 /* Determine if a long branch stub is needed. */
10731 max_branch_offset = 1 << 25;
10732 if (r_type == R_PPC64_REL14
10733 || r_type == R_PPC64_REL14_BRTAKEN
10734 || r_type == R_PPC64_REL14_BRNTAKEN)
10735 max_branch_offset = 1 << 15;
10736
10737 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10738 /* We need a stub. Figure out whether a long_branch or plt_branch
10739 is needed later. */
10740 return ppc_stub_long_branch;
10741
10742 return ppc_stub_none;
10743 }
10744
10745 /* With power7 weakly ordered memory model, it is possible for ld.so
10746 to update a plt entry in one thread and have another thread see a
10747 stale zero toc entry. To avoid this we need some sort of acquire
10748 barrier in the call stub. One solution is to make the load of the
10749 toc word seem to appear to depend on the load of the function entry
10750 word. Another solution is to test for r2 being zero, and branch to
10751 the appropriate glink entry if so.
10752
10753 . fake dep barrier compare
10754 . ld 12,xxx(2) ld 12,xxx(2)
10755 . mtctr 12 mtctr 12
10756 . xor 11,12,12 ld 2,xxx+8(2)
10757 . add 2,2,11 cmpldi 2,0
10758 . ld 2,xxx+8(2) bnectr+
10759 . bctr b <glink_entry>
10760
10761 The solution involving the compare turns out to be faster, so
10762 that's what we use unless the branch won't reach. */
10763
10764 #define ALWAYS_USE_FAKE_DEP 0
10765 #define ALWAYS_EMIT_R2SAVE 0
10766
10767 static inline unsigned int
10768 plt_stub_size (struct ppc_link_hash_table *htab,
10769 struct ppc_stub_hash_entry *stub_entry,
10770 bfd_vma off)
10771 {
10772 unsigned size = 12;
10773
10774 if (ALWAYS_EMIT_R2SAVE
10775 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10776 size += 4;
10777 if (PPC_HA (off) != 0)
10778 size += 4;
10779 if (htab->opd_abi)
10780 {
10781 size += 4;
10782 if (htab->params->plt_static_chain)
10783 size += 4;
10784 if (htab->params->plt_thread_safe
10785 && htab->elf.dynamic_sections_created
10786 && stub_entry->h != NULL
10787 && stub_entry->h->elf.dynindx != -1)
10788 size += 8;
10789 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10790 size += 4;
10791 }
10792 if (stub_entry->h != NULL
10793 && (stub_entry->h == htab->tls_get_addr_fd
10794 || stub_entry->h == htab->tls_get_addr)
10795 && htab->params->tls_get_addr_opt)
10796 {
10797 size += 7 * 4;
10798 if (ALWAYS_EMIT_R2SAVE
10799 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10800 size += 6 * 4;
10801 }
10802 return size;
10803 }
10804
10805 /* Depending on the sign of plt_stub_align:
10806 If positive, return the padding to align to a 2**plt_stub_align
10807 boundary.
10808 If negative, if this stub would cross fewer 2**plt_stub_align
10809 boundaries if we align, then return the padding needed to do so. */
10810
10811 static inline unsigned int
10812 plt_stub_pad (struct ppc_link_hash_table *htab,
10813 struct ppc_stub_hash_entry *stub_entry,
10814 bfd_vma plt_off)
10815 {
10816 int stub_align;
10817 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10818 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10819
10820 if (htab->params->plt_stub_align >= 0)
10821 {
10822 stub_align = 1 << htab->params->plt_stub_align;
10823 if ((stub_off & (stub_align - 1)) != 0)
10824 return stub_align - (stub_off & (stub_align - 1));
10825 return 0;
10826 }
10827
10828 stub_align = 1 << -htab->params->plt_stub_align;
10829 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10830 > ((stub_size - 1) & -stub_align))
10831 return stub_align - (stub_off & (stub_align - 1));
10832 return 0;
10833 }
10834
10835 /* Build a .plt call stub. */
10836
10837 static inline bfd_byte *
10838 build_plt_stub (struct ppc_link_hash_table *htab,
10839 struct ppc_stub_hash_entry *stub_entry,
10840 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10841 {
10842 bfd *obfd = htab->params->stub_bfd;
10843 bfd_boolean plt_load_toc = htab->opd_abi;
10844 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10845 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10846 && htab->elf.dynamic_sections_created
10847 && stub_entry->h != NULL
10848 && stub_entry->h->elf.dynindx != -1);
10849 bfd_boolean use_fake_dep = plt_thread_safe;
10850 bfd_vma cmp_branch_off = 0;
10851
10852 if (!ALWAYS_USE_FAKE_DEP
10853 && plt_load_toc
10854 && plt_thread_safe
10855 && !((stub_entry->h == htab->tls_get_addr_fd
10856 || stub_entry->h == htab->tls_get_addr)
10857 && htab->params->tls_get_addr_opt))
10858 {
10859 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10860 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10861 / PLT_ENTRY_SIZE (htab));
10862 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10863 bfd_vma to, from;
10864
10865 if (pltindex > 32768)
10866 glinkoff += (pltindex - 32768) * 4;
10867 to = (glinkoff
10868 + htab->glink->output_offset
10869 + htab->glink->output_section->vma);
10870 from = (p - stub_entry->group->stub_sec->contents
10871 + 4 * (ALWAYS_EMIT_R2SAVE
10872 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10873 + 4 * (PPC_HA (offset) != 0)
10874 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10875 != PPC_HA (offset))
10876 + 4 * (plt_static_chain != 0)
10877 + 20
10878 + stub_entry->group->stub_sec->output_offset
10879 + stub_entry->group->stub_sec->output_section->vma);
10880 cmp_branch_off = to - from;
10881 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10882 }
10883
10884 if (PPC_HA (offset) != 0)
10885 {
10886 if (r != NULL)
10887 {
10888 if (ALWAYS_EMIT_R2SAVE
10889 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10890 r[0].r_offset += 4;
10891 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10892 r[1].r_offset = r[0].r_offset + 4;
10893 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10894 r[1].r_addend = r[0].r_addend;
10895 if (plt_load_toc)
10896 {
10897 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10898 {
10899 r[2].r_offset = r[1].r_offset + 4;
10900 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10901 r[2].r_addend = r[0].r_addend;
10902 }
10903 else
10904 {
10905 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10906 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10907 r[2].r_addend = r[0].r_addend + 8;
10908 if (plt_static_chain)
10909 {
10910 r[3].r_offset = r[2].r_offset + 4;
10911 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10912 r[3].r_addend = r[0].r_addend + 16;
10913 }
10914 }
10915 }
10916 }
10917 if (ALWAYS_EMIT_R2SAVE
10918 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10919 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10920 if (plt_load_toc)
10921 {
10922 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10923 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10924 }
10925 else
10926 {
10927 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10928 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10929 }
10930 if (plt_load_toc
10931 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10932 {
10933 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10934 offset = 0;
10935 }
10936 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10937 if (plt_load_toc)
10938 {
10939 if (use_fake_dep)
10940 {
10941 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10942 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10943 }
10944 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10945 if (plt_static_chain)
10946 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10947 }
10948 }
10949 else
10950 {
10951 if (r != NULL)
10952 {
10953 if (ALWAYS_EMIT_R2SAVE
10954 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10955 r[0].r_offset += 4;
10956 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10957 if (plt_load_toc)
10958 {
10959 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10960 {
10961 r[1].r_offset = r[0].r_offset + 4;
10962 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10963 r[1].r_addend = r[0].r_addend;
10964 }
10965 else
10966 {
10967 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10968 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10969 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10970 if (plt_static_chain)
10971 {
10972 r[2].r_offset = r[1].r_offset + 4;
10973 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10974 r[2].r_addend = r[0].r_addend + 8;
10975 }
10976 }
10977 }
10978 }
10979 if (ALWAYS_EMIT_R2SAVE
10980 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10981 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10982 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10983 if (plt_load_toc
10984 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10985 {
10986 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10987 offset = 0;
10988 }
10989 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10990 if (plt_load_toc)
10991 {
10992 if (use_fake_dep)
10993 {
10994 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10995 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10996 }
10997 if (plt_static_chain)
10998 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10999 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11000 }
11001 }
11002 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11003 {
11004 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11005 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11006 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11007 }
11008 else
11009 bfd_put_32 (obfd, BCTR, p), p += 4;
11010 return p;
11011 }
11012
11013 /* Build a special .plt call stub for __tls_get_addr. */
11014
11015 #define LD_R11_0R3 0xe9630000
11016 #define LD_R12_0R3 0xe9830000
11017 #define MR_R0_R3 0x7c601b78
11018 #define CMPDI_R11_0 0x2c2b0000
11019 #define ADD_R3_R12_R13 0x7c6c6a14
11020 #define BEQLR 0x4d820020
11021 #define MR_R3_R0 0x7c030378
11022 #define STD_R11_0R1 0xf9610000
11023 #define BCTRL 0x4e800421
11024 #define LD_R11_0R1 0xe9610000
11025 #define MTLR_R11 0x7d6803a6
11026
11027 static inline bfd_byte *
11028 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11029 struct ppc_stub_hash_entry *stub_entry,
11030 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11031 {
11032 bfd *obfd = htab->params->stub_bfd;
11033
11034 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11035 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11036 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11037 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11038 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11039 bfd_put_32 (obfd, BEQLR, p), p += 4;
11040 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11041 if (r != NULL)
11042 r[0].r_offset += 7 * 4;
11043 if (!ALWAYS_EMIT_R2SAVE
11044 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
11045 return build_plt_stub (htab, stub_entry, p, offset, r);
11046
11047 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
11048 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11049
11050 if (r != NULL)
11051 r[0].r_offset += 2 * 4;
11052 p = build_plt_stub (htab, stub_entry, p, offset, r);
11053 bfd_put_32 (obfd, BCTRL, p - 4);
11054
11055 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
11056 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11057 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11058 bfd_put_32 (obfd, BLR, p), p += 4;
11059
11060 return p;
11061 }
11062
11063 static Elf_Internal_Rela *
11064 get_relocs (asection *sec, int count)
11065 {
11066 Elf_Internal_Rela *relocs;
11067 struct bfd_elf_section_data *elfsec_data;
11068
11069 elfsec_data = elf_section_data (sec);
11070 relocs = elfsec_data->relocs;
11071 if (relocs == NULL)
11072 {
11073 bfd_size_type relsize;
11074 relsize = sec->reloc_count * sizeof (*relocs);
11075 relocs = bfd_alloc (sec->owner, relsize);
11076 if (relocs == NULL)
11077 return NULL;
11078 elfsec_data->relocs = relocs;
11079 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11080 sizeof (Elf_Internal_Shdr));
11081 if (elfsec_data->rela.hdr == NULL)
11082 return NULL;
11083 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11084 * sizeof (Elf64_External_Rela));
11085 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11086 sec->reloc_count = 0;
11087 }
11088 relocs += sec->reloc_count;
11089 sec->reloc_count += count;
11090 return relocs;
11091 }
11092
11093 static bfd_vma
11094 get_r2off (struct bfd_link_info *info,
11095 struct ppc_stub_hash_entry *stub_entry)
11096 {
11097 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11098 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11099
11100 if (r2off == 0)
11101 {
11102 /* Support linking -R objects. Get the toc pointer from the
11103 opd entry. */
11104 char buf[8];
11105 if (!htab->opd_abi)
11106 return r2off;
11107 asection *opd = stub_entry->h->elf.root.u.def.section;
11108 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11109
11110 if (strcmp (opd->name, ".opd") != 0
11111 || opd->reloc_count != 0)
11112 {
11113 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11114 stub_entry->h->elf.root.root.string);
11115 bfd_set_error (bfd_error_bad_value);
11116 return (bfd_vma) -1;
11117 }
11118 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11119 return (bfd_vma) -1;
11120 r2off = bfd_get_64 (opd->owner, buf);
11121 r2off -= elf_gp (info->output_bfd);
11122 }
11123 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11124 return r2off;
11125 }
11126
11127 static bfd_boolean
11128 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11129 {
11130 struct ppc_stub_hash_entry *stub_entry;
11131 struct ppc_branch_hash_entry *br_entry;
11132 struct bfd_link_info *info;
11133 struct ppc_link_hash_table *htab;
11134 bfd_byte *loc;
11135 bfd_byte *p;
11136 bfd_vma dest, off;
11137 Elf_Internal_Rela *r;
11138 asection *plt;
11139
11140 /* Massage our args to the form they really have. */
11141 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11142 info = in_arg;
11143
11144 htab = ppc_hash_table (info);
11145 if (htab == NULL)
11146 return FALSE;
11147
11148 /* Make a note of the offset within the stubs for this entry. */
11149 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11150 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11151
11152 htab->stub_count[stub_entry->stub_type - 1] += 1;
11153 switch (stub_entry->stub_type)
11154 {
11155 case ppc_stub_long_branch:
11156 case ppc_stub_long_branch_r2off:
11157 /* Branches are relative. This is where we are going to. */
11158 dest = (stub_entry->target_value
11159 + stub_entry->target_section->output_offset
11160 + stub_entry->target_section->output_section->vma);
11161 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11162 off = dest;
11163
11164 /* And this is where we are coming from. */
11165 off -= (stub_entry->stub_offset
11166 + stub_entry->group->stub_sec->output_offset
11167 + stub_entry->group->stub_sec->output_section->vma);
11168
11169 p = loc;
11170 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11171 {
11172 bfd_vma r2off = get_r2off (info, stub_entry);
11173
11174 if (r2off == (bfd_vma) -1)
11175 {
11176 htab->stub_error = TRUE;
11177 return FALSE;
11178 }
11179 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11180 p += 4;
11181 if (PPC_HA (r2off) != 0)
11182 {
11183 bfd_put_32 (htab->params->stub_bfd,
11184 ADDIS_R2_R2 | PPC_HA (r2off), p);
11185 p += 4;
11186 }
11187 if (PPC_LO (r2off) != 0)
11188 {
11189 bfd_put_32 (htab->params->stub_bfd,
11190 ADDI_R2_R2 | PPC_LO (r2off), p);
11191 p += 4;
11192 }
11193 off -= p - loc;
11194 }
11195 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11196 p += 4;
11197
11198 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11199 {
11200 _bfd_error_handler
11201 (_("long branch stub `%s' offset overflow"),
11202 stub_entry->root.string);
11203 htab->stub_error = TRUE;
11204 return FALSE;
11205 }
11206
11207 if (info->emitrelocations)
11208 {
11209 r = get_relocs (stub_entry->group->stub_sec, 1);
11210 if (r == NULL)
11211 return FALSE;
11212 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11213 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11214 r->r_addend = dest;
11215 if (stub_entry->h != NULL)
11216 {
11217 struct elf_link_hash_entry **hashes;
11218 unsigned long symndx;
11219 struct ppc_link_hash_entry *h;
11220
11221 hashes = elf_sym_hashes (htab->params->stub_bfd);
11222 if (hashes == NULL)
11223 {
11224 bfd_size_type hsize;
11225
11226 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11227 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11228 if (hashes == NULL)
11229 return FALSE;
11230 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11231 htab->stub_globals = 1;
11232 }
11233 symndx = htab->stub_globals++;
11234 h = stub_entry->h;
11235 hashes[symndx] = &h->elf;
11236 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11237 if (h->oh != NULL && h->oh->is_func)
11238 h = ppc_follow_link (h->oh);
11239 if (h->elf.root.u.def.section != stub_entry->target_section)
11240 /* H is an opd symbol. The addend must be zero. */
11241 r->r_addend = 0;
11242 else
11243 {
11244 off = (h->elf.root.u.def.value
11245 + h->elf.root.u.def.section->output_offset
11246 + h->elf.root.u.def.section->output_section->vma);
11247 r->r_addend -= off;
11248 }
11249 }
11250 }
11251 break;
11252
11253 case ppc_stub_plt_branch:
11254 case ppc_stub_plt_branch_r2off:
11255 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11256 stub_entry->root.string + 9,
11257 FALSE, FALSE);
11258 if (br_entry == NULL)
11259 {
11260 _bfd_error_handler (_("can't find branch stub `%s'"),
11261 stub_entry->root.string);
11262 htab->stub_error = TRUE;
11263 return FALSE;
11264 }
11265
11266 dest = (stub_entry->target_value
11267 + stub_entry->target_section->output_offset
11268 + stub_entry->target_section->output_section->vma);
11269 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11270 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11271
11272 bfd_put_64 (htab->brlt->owner, dest,
11273 htab->brlt->contents + br_entry->offset);
11274
11275 if (br_entry->iter == htab->stub_iteration)
11276 {
11277 br_entry->iter = 0;
11278
11279 if (htab->relbrlt != NULL)
11280 {
11281 /* Create a reloc for the branch lookup table entry. */
11282 Elf_Internal_Rela rela;
11283 bfd_byte *rl;
11284
11285 rela.r_offset = (br_entry->offset
11286 + htab->brlt->output_offset
11287 + htab->brlt->output_section->vma);
11288 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11289 rela.r_addend = dest;
11290
11291 rl = htab->relbrlt->contents;
11292 rl += (htab->relbrlt->reloc_count++
11293 * sizeof (Elf64_External_Rela));
11294 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11295 }
11296 else if (info->emitrelocations)
11297 {
11298 r = get_relocs (htab->brlt, 1);
11299 if (r == NULL)
11300 return FALSE;
11301 /* brlt, being SEC_LINKER_CREATED does not go through the
11302 normal reloc processing. Symbols and offsets are not
11303 translated from input file to output file form, so
11304 set up the offset per the output file. */
11305 r->r_offset = (br_entry->offset
11306 + htab->brlt->output_offset
11307 + htab->brlt->output_section->vma);
11308 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11309 r->r_addend = dest;
11310 }
11311 }
11312
11313 dest = (br_entry->offset
11314 + htab->brlt->output_offset
11315 + htab->brlt->output_section->vma);
11316
11317 off = (dest
11318 - elf_gp (info->output_bfd)
11319 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11320
11321 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11322 {
11323 info->callbacks->einfo
11324 (_("%P: linkage table error against `%pT'\n"),
11325 stub_entry->root.string);
11326 bfd_set_error (bfd_error_bad_value);
11327 htab->stub_error = TRUE;
11328 return FALSE;
11329 }
11330
11331 if (info->emitrelocations)
11332 {
11333 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11334 if (r == NULL)
11335 return FALSE;
11336 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11337 if (bfd_big_endian (info->output_bfd))
11338 r[0].r_offset += 2;
11339 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11340 r[0].r_offset += 4;
11341 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11342 r[0].r_addend = dest;
11343 if (PPC_HA (off) != 0)
11344 {
11345 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11346 r[1].r_offset = r[0].r_offset + 4;
11347 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11348 r[1].r_addend = r[0].r_addend;
11349 }
11350 }
11351
11352 p = loc;
11353 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11354 {
11355 if (PPC_HA (off) != 0)
11356 {
11357 bfd_put_32 (htab->params->stub_bfd,
11358 ADDIS_R12_R2 | PPC_HA (off), p);
11359 p += 4;
11360 bfd_put_32 (htab->params->stub_bfd,
11361 LD_R12_0R12 | PPC_LO (off), p);
11362 }
11363 else
11364 bfd_put_32 (htab->params->stub_bfd,
11365 LD_R12_0R2 | PPC_LO (off), p);
11366 }
11367 else
11368 {
11369 bfd_vma r2off = get_r2off (info, stub_entry);
11370
11371 if (r2off == (bfd_vma) -1)
11372 {
11373 htab->stub_error = TRUE;
11374 return FALSE;
11375 }
11376
11377 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11378 p += 4;
11379 if (PPC_HA (off) != 0)
11380 {
11381 bfd_put_32 (htab->params->stub_bfd,
11382 ADDIS_R12_R2 | PPC_HA (off), p);
11383 p += 4;
11384 bfd_put_32 (htab->params->stub_bfd,
11385 LD_R12_0R12 | PPC_LO (off), p);
11386 }
11387 else
11388 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11389
11390 if (PPC_HA (r2off) != 0)
11391 {
11392 p += 4;
11393 bfd_put_32 (htab->params->stub_bfd,
11394 ADDIS_R2_R2 | PPC_HA (r2off), p);
11395 }
11396 if (PPC_LO (r2off) != 0)
11397 {
11398 p += 4;
11399 bfd_put_32 (htab->params->stub_bfd,
11400 ADDI_R2_R2 | PPC_LO (r2off), p);
11401 }
11402 }
11403 p += 4;
11404 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11405 p += 4;
11406 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11407 p += 4;
11408 break;
11409
11410 case ppc_stub_plt_call:
11411 case ppc_stub_plt_call_r2save:
11412 if (stub_entry->h != NULL
11413 && stub_entry->h->is_func_descriptor
11414 && stub_entry->h->oh != NULL)
11415 {
11416 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11417
11418 /* If the old-ABI "dot-symbol" is undefined make it weak so
11419 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11420 if (fh->elf.root.type == bfd_link_hash_undefined
11421 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11422 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11423 fh->elf.root.type = bfd_link_hash_undefweak;
11424 }
11425
11426 /* Now build the stub. */
11427 dest = stub_entry->plt_ent->plt.offset & ~1;
11428 if (dest >= (bfd_vma) -2)
11429 abort ();
11430
11431 plt = htab->elf.splt;
11432 if (!htab->elf.dynamic_sections_created
11433 || stub_entry->h == NULL
11434 || stub_entry->h->elf.dynindx == -1)
11435 {
11436 if (stub_entry->symtype == STT_GNU_IFUNC)
11437 plt = htab->elf.iplt;
11438 else
11439 plt = htab->pltlocal;
11440 }
11441
11442 dest += plt->output_offset + plt->output_section->vma;
11443
11444 off = (dest
11445 - elf_gp (info->output_bfd)
11446 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11447
11448 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11449 {
11450 info->callbacks->einfo
11451 /* xgettext:c-format */
11452 (_("%P: linkage table error against `%pT'\n"),
11453 stub_entry->h != NULL
11454 ? stub_entry->h->elf.root.root.string
11455 : "<local sym>");
11456 bfd_set_error (bfd_error_bad_value);
11457 htab->stub_error = TRUE;
11458 return FALSE;
11459 }
11460
11461 if (htab->params->plt_stub_align != 0)
11462 {
11463 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11464
11465 stub_entry->group->stub_sec->size += pad;
11466 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11467 loc += pad;
11468 }
11469
11470 r = NULL;
11471 if (info->emitrelocations)
11472 {
11473 r = get_relocs (stub_entry->group->stub_sec,
11474 ((PPC_HA (off) != 0)
11475 + (htab->opd_abi
11476 ? 2 + (htab->params->plt_static_chain
11477 && PPC_HA (off + 16) == PPC_HA (off))
11478 : 1)));
11479 if (r == NULL)
11480 return FALSE;
11481 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11482 if (bfd_big_endian (info->output_bfd))
11483 r[0].r_offset += 2;
11484 r[0].r_addend = dest;
11485 }
11486 if (stub_entry->h != NULL
11487 && (stub_entry->h == htab->tls_get_addr_fd
11488 || stub_entry->h == htab->tls_get_addr)
11489 && htab->params->tls_get_addr_opt)
11490 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11491 else
11492 p = build_plt_stub (htab, stub_entry, loc, off, r);
11493 break;
11494
11495 case ppc_stub_save_res:
11496 return TRUE;
11497
11498 default:
11499 BFD_FAIL ();
11500 return FALSE;
11501 }
11502
11503 stub_entry->group->stub_sec->size += p - loc;
11504
11505 if (htab->params->emit_stub_syms)
11506 {
11507 struct elf_link_hash_entry *h;
11508 size_t len1, len2;
11509 char *name;
11510 const char *const stub_str[] = { "long_branch",
11511 "long_branch_r2off",
11512 "plt_branch",
11513 "plt_branch_r2off",
11514 "plt_call",
11515 "plt_call" };
11516
11517 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11518 len2 = strlen (stub_entry->root.string);
11519 name = bfd_malloc (len1 + len2 + 2);
11520 if (name == NULL)
11521 return FALSE;
11522 memcpy (name, stub_entry->root.string, 9);
11523 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11524 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11525 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11526 if (h == NULL)
11527 return FALSE;
11528 if (h->root.type == bfd_link_hash_new)
11529 {
11530 h->root.type = bfd_link_hash_defined;
11531 h->root.u.def.section = stub_entry->group->stub_sec;
11532 h->root.u.def.value = stub_entry->stub_offset;
11533 h->ref_regular = 1;
11534 h->def_regular = 1;
11535 h->ref_regular_nonweak = 1;
11536 h->forced_local = 1;
11537 h->non_elf = 0;
11538 h->root.linker_def = 1;
11539 }
11540 }
11541
11542 return TRUE;
11543 }
11544
11545 /* As above, but don't actually build the stub. Just bump offset so
11546 we know stub section sizes, and select plt_branch stubs where
11547 long_branch stubs won't do. */
11548
11549 static bfd_boolean
11550 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11551 {
11552 struct ppc_stub_hash_entry *stub_entry;
11553 struct bfd_link_info *info;
11554 struct ppc_link_hash_table *htab;
11555 bfd_vma off;
11556 int size;
11557
11558 /* Massage our args to the form they really have. */
11559 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11560 info = in_arg;
11561
11562 htab = ppc_hash_table (info);
11563 if (htab == NULL)
11564 return FALSE;
11565
11566 if (stub_entry->h != NULL
11567 && stub_entry->h->save_res
11568 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11569 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11570 {
11571 /* Don't make stubs to out-of-line register save/restore
11572 functions. Instead, emit copies of the functions. */
11573 stub_entry->group->needs_save_res = 1;
11574 stub_entry->stub_type = ppc_stub_save_res;
11575 return TRUE;
11576 }
11577
11578 if (stub_entry->stub_type == ppc_stub_plt_call
11579 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11580 {
11581 asection *plt;
11582 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11583 if (off >= (bfd_vma) -2)
11584 abort ();
11585 plt = htab->elf.splt;
11586 if (!htab->elf.dynamic_sections_created
11587 || stub_entry->h == NULL
11588 || stub_entry->h->elf.dynindx == -1)
11589 {
11590 if (stub_entry->symtype == STT_GNU_IFUNC)
11591 plt = htab->elf.iplt;
11592 else
11593 plt = htab->pltlocal;
11594 }
11595 off += (plt->output_offset
11596 + plt->output_section->vma
11597 - elf_gp (info->output_bfd)
11598 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11599
11600 size = plt_stub_size (htab, stub_entry, off);
11601 if (stub_entry->h != NULL
11602 && (stub_entry->h == htab->tls_get_addr_fd
11603 || stub_entry->h == htab->tls_get_addr)
11604 && htab->params->tls_get_addr_opt
11605 && (ALWAYS_EMIT_R2SAVE
11606 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11607 stub_entry->group->tls_get_addr_opt_bctrl
11608 = stub_entry->group->stub_sec->size + size - 5 * 4;
11609
11610 if (htab->params->plt_stub_align)
11611 size += plt_stub_pad (htab, stub_entry, off);
11612 if (info->emitrelocations)
11613 {
11614 stub_entry->group->stub_sec->reloc_count
11615 += ((PPC_HA (off) != 0)
11616 + (htab->opd_abi
11617 ? 2 + (htab->params->plt_static_chain
11618 && PPC_HA (off + 16) == PPC_HA (off))
11619 : 1));
11620 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11621 }
11622 }
11623 else
11624 {
11625 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11626 variants. */
11627 bfd_vma r2off = 0;
11628 bfd_vma local_off = 0;
11629
11630 off = (stub_entry->target_value
11631 + stub_entry->target_section->output_offset
11632 + stub_entry->target_section->output_section->vma);
11633 off -= (stub_entry->group->stub_sec->size
11634 + stub_entry->group->stub_sec->output_offset
11635 + stub_entry->group->stub_sec->output_section->vma);
11636
11637 /* Reset the stub type from the plt variant in case we now
11638 can reach with a shorter stub. */
11639 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11640 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11641
11642 size = 4;
11643 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11644 {
11645 r2off = get_r2off (info, stub_entry);
11646 if (r2off == (bfd_vma) -1)
11647 {
11648 htab->stub_error = TRUE;
11649 return FALSE;
11650 }
11651 size = 8;
11652 if (PPC_HA (r2off) != 0)
11653 size += 4;
11654 if (PPC_LO (r2off) != 0)
11655 size += 4;
11656 off -= size - 4;
11657 }
11658
11659 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11660
11661 /* If the branch offset is too big, use a ppc_stub_plt_branch.
11662 Do the same for -R objects without function descriptors. */
11663 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11664 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11665 && r2off == 0
11666 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11667 {
11668 struct ppc_branch_hash_entry *br_entry;
11669
11670 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11671 stub_entry->root.string + 9,
11672 TRUE, FALSE);
11673 if (br_entry == NULL)
11674 {
11675 _bfd_error_handler (_("can't build branch stub `%s'"),
11676 stub_entry->root.string);
11677 htab->stub_error = TRUE;
11678 return FALSE;
11679 }
11680
11681 if (br_entry->iter != htab->stub_iteration)
11682 {
11683 br_entry->iter = htab->stub_iteration;
11684 br_entry->offset = htab->brlt->size;
11685 htab->brlt->size += 8;
11686
11687 if (htab->relbrlt != NULL)
11688 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11689 else if (info->emitrelocations)
11690 {
11691 htab->brlt->reloc_count += 1;
11692 htab->brlt->flags |= SEC_RELOC;
11693 }
11694 }
11695
11696 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11697 off = (br_entry->offset
11698 + htab->brlt->output_offset
11699 + htab->brlt->output_section->vma
11700 - elf_gp (info->output_bfd)
11701 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11702
11703 if (info->emitrelocations)
11704 {
11705 stub_entry->group->stub_sec->reloc_count
11706 += 1 + (PPC_HA (off) != 0);
11707 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11708 }
11709
11710 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11711 {
11712 size = 12;
11713 if (PPC_HA (off) != 0)
11714 size = 16;
11715 }
11716 else
11717 {
11718 size = 16;
11719 if (PPC_HA (off) != 0)
11720 size += 4;
11721
11722 if (PPC_HA (r2off) != 0)
11723 size += 4;
11724 if (PPC_LO (r2off) != 0)
11725 size += 4;
11726 }
11727 }
11728 else if (info->emitrelocations)
11729 {
11730 stub_entry->group->stub_sec->reloc_count += 1;
11731 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11732 }
11733 }
11734
11735 stub_entry->group->stub_sec->size += size;
11736 return TRUE;
11737 }
11738
11739 /* Set up various things so that we can make a list of input sections
11740 for each output section included in the link. Returns -1 on error,
11741 0 when no stubs will be needed, and 1 on success. */
11742
11743 int
11744 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11745 {
11746 unsigned int id;
11747 bfd_size_type amt;
11748 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11749
11750 if (htab == NULL)
11751 return -1;
11752
11753 htab->sec_info_arr_size = bfd_get_next_section_id ();
11754 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11755 htab->sec_info = bfd_zmalloc (amt);
11756 if (htab->sec_info == NULL)
11757 return -1;
11758
11759 /* Set toc_off for com, und, abs and ind sections. */
11760 for (id = 0; id < 3; id++)
11761 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11762
11763 return 1;
11764 }
11765
11766 /* Set up for first pass at multitoc partitioning. */
11767
11768 void
11769 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11770 {
11771 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11772
11773 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11774 htab->toc_bfd = NULL;
11775 htab->toc_first_sec = NULL;
11776 }
11777
11778 /* The linker repeatedly calls this function for each TOC input section
11779 and linker generated GOT section. Group input bfds such that the toc
11780 within a group is less than 64k in size. */
11781
11782 bfd_boolean
11783 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11784 {
11785 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11786 bfd_vma addr, off, limit;
11787
11788 if (htab == NULL)
11789 return FALSE;
11790
11791 if (!htab->second_toc_pass)
11792 {
11793 /* Keep track of the first .toc or .got section for this input bfd. */
11794 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11795
11796 if (new_bfd)
11797 {
11798 htab->toc_bfd = isec->owner;
11799 htab->toc_first_sec = isec;
11800 }
11801
11802 addr = isec->output_offset + isec->output_section->vma;
11803 off = addr - htab->toc_curr;
11804 limit = 0x80008000;
11805 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11806 limit = 0x10000;
11807 if (off + isec->size > limit)
11808 {
11809 addr = (htab->toc_first_sec->output_offset
11810 + htab->toc_first_sec->output_section->vma);
11811 htab->toc_curr = addr;
11812 htab->toc_curr &= -TOC_BASE_ALIGN;
11813 }
11814
11815 /* toc_curr is the base address of this toc group. Set elf_gp
11816 for the input section to be the offset relative to the
11817 output toc base plus 0x8000. Making the input elf_gp an
11818 offset allows us to move the toc as a whole without
11819 recalculating input elf_gp. */
11820 off = htab->toc_curr - elf_gp (info->output_bfd);
11821 off += TOC_BASE_OFF;
11822
11823 /* Die if someone uses a linker script that doesn't keep input
11824 file .toc and .got together. */
11825 if (new_bfd
11826 && elf_gp (isec->owner) != 0
11827 && elf_gp (isec->owner) != off)
11828 return FALSE;
11829
11830 elf_gp (isec->owner) = off;
11831 return TRUE;
11832 }
11833
11834 /* During the second pass toc_first_sec points to the start of
11835 a toc group, and toc_curr is used to track the old elf_gp.
11836 We use toc_bfd to ensure we only look at each bfd once. */
11837 if (htab->toc_bfd == isec->owner)
11838 return TRUE;
11839 htab->toc_bfd = isec->owner;
11840
11841 if (htab->toc_first_sec == NULL
11842 || htab->toc_curr != elf_gp (isec->owner))
11843 {
11844 htab->toc_curr = elf_gp (isec->owner);
11845 htab->toc_first_sec = isec;
11846 }
11847 addr = (htab->toc_first_sec->output_offset
11848 + htab->toc_first_sec->output_section->vma);
11849 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11850 elf_gp (isec->owner) = off;
11851
11852 return TRUE;
11853 }
11854
11855 /* Called via elf_link_hash_traverse to merge GOT entries for global
11856 symbol H. */
11857
11858 static bfd_boolean
11859 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11860 {
11861 if (h->root.type == bfd_link_hash_indirect)
11862 return TRUE;
11863
11864 merge_got_entries (&h->got.glist);
11865
11866 return TRUE;
11867 }
11868
11869 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11870 symbol H. */
11871
11872 static bfd_boolean
11873 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11874 {
11875 struct got_entry *gent;
11876
11877 if (h->root.type == bfd_link_hash_indirect)
11878 return TRUE;
11879
11880 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11881 if (!gent->is_indirect)
11882 allocate_got (h, (struct bfd_link_info *) inf, gent);
11883 return TRUE;
11884 }
11885
11886 /* Called on the first multitoc pass after the last call to
11887 ppc64_elf_next_toc_section. This function removes duplicate GOT
11888 entries. */
11889
11890 bfd_boolean
11891 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11892 {
11893 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11894 struct bfd *ibfd, *ibfd2;
11895 bfd_boolean done_something;
11896
11897 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11898
11899 if (!htab->do_multi_toc)
11900 return FALSE;
11901
11902 /* Merge global sym got entries within a toc group. */
11903 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11904
11905 /* And tlsld_got. */
11906 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11907 {
11908 struct got_entry *ent, *ent2;
11909
11910 if (!is_ppc64_elf (ibfd))
11911 continue;
11912
11913 ent = ppc64_tlsld_got (ibfd);
11914 if (!ent->is_indirect
11915 && ent->got.offset != (bfd_vma) -1)
11916 {
11917 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11918 {
11919 if (!is_ppc64_elf (ibfd2))
11920 continue;
11921
11922 ent2 = ppc64_tlsld_got (ibfd2);
11923 if (!ent2->is_indirect
11924 && ent2->got.offset != (bfd_vma) -1
11925 && elf_gp (ibfd2) == elf_gp (ibfd))
11926 {
11927 ent2->is_indirect = TRUE;
11928 ent2->got.ent = ent;
11929 }
11930 }
11931 }
11932 }
11933
11934 /* Zap sizes of got sections. */
11935 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11936 htab->elf.irelplt->size -= htab->got_reli_size;
11937 htab->got_reli_size = 0;
11938
11939 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11940 {
11941 asection *got, *relgot;
11942
11943 if (!is_ppc64_elf (ibfd))
11944 continue;
11945
11946 got = ppc64_elf_tdata (ibfd)->got;
11947 if (got != NULL)
11948 {
11949 got->rawsize = got->size;
11950 got->size = 0;
11951 relgot = ppc64_elf_tdata (ibfd)->relgot;
11952 relgot->rawsize = relgot->size;
11953 relgot->size = 0;
11954 }
11955 }
11956
11957 /* Now reallocate the got, local syms first. We don't need to
11958 allocate section contents again since we never increase size. */
11959 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11960 {
11961 struct got_entry **lgot_ents;
11962 struct got_entry **end_lgot_ents;
11963 struct plt_entry **local_plt;
11964 struct plt_entry **end_local_plt;
11965 unsigned char *lgot_masks;
11966 bfd_size_type locsymcount;
11967 Elf_Internal_Shdr *symtab_hdr;
11968 asection *s;
11969
11970 if (!is_ppc64_elf (ibfd))
11971 continue;
11972
11973 lgot_ents = elf_local_got_ents (ibfd);
11974 if (!lgot_ents)
11975 continue;
11976
11977 symtab_hdr = &elf_symtab_hdr (ibfd);
11978 locsymcount = symtab_hdr->sh_info;
11979 end_lgot_ents = lgot_ents + locsymcount;
11980 local_plt = (struct plt_entry **) end_lgot_ents;
11981 end_local_plt = local_plt + locsymcount;
11982 lgot_masks = (unsigned char *) end_local_plt;
11983 s = ppc64_elf_tdata (ibfd)->got;
11984 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11985 {
11986 struct got_entry *ent;
11987
11988 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11989 {
11990 unsigned int ent_size = 8;
11991 unsigned int rel_size = sizeof (Elf64_External_Rela);
11992
11993 ent->got.offset = s->size;
11994 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11995 {
11996 ent_size *= 2;
11997 rel_size *= 2;
11998 }
11999 s->size += ent_size;
12000 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12001 {
12002 htab->elf.irelplt->size += rel_size;
12003 htab->got_reli_size += rel_size;
12004 }
12005 else if (bfd_link_pic (info)
12006 && !((ent->tls_type & TLS_TPREL) != 0
12007 && bfd_link_executable (info)))
12008 {
12009 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12010 srel->size += rel_size;
12011 }
12012 }
12013 }
12014 }
12015
12016 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12017
12018 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12019 {
12020 struct got_entry *ent;
12021
12022 if (!is_ppc64_elf (ibfd))
12023 continue;
12024
12025 ent = ppc64_tlsld_got (ibfd);
12026 if (!ent->is_indirect
12027 && ent->got.offset != (bfd_vma) -1)
12028 {
12029 asection *s = ppc64_elf_tdata (ibfd)->got;
12030 ent->got.offset = s->size;
12031 s->size += 16;
12032 if (bfd_link_pic (info))
12033 {
12034 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12035 srel->size += sizeof (Elf64_External_Rela);
12036 }
12037 }
12038 }
12039
12040 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12041 if (!done_something)
12042 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12043 {
12044 asection *got;
12045
12046 if (!is_ppc64_elf (ibfd))
12047 continue;
12048
12049 got = ppc64_elf_tdata (ibfd)->got;
12050 if (got != NULL)
12051 {
12052 done_something = got->rawsize != got->size;
12053 if (done_something)
12054 break;
12055 }
12056 }
12057
12058 if (done_something)
12059 (*htab->params->layout_sections_again) ();
12060
12061 /* Set up for second pass over toc sections to recalculate elf_gp
12062 on input sections. */
12063 htab->toc_bfd = NULL;
12064 htab->toc_first_sec = NULL;
12065 htab->second_toc_pass = TRUE;
12066 return done_something;
12067 }
12068
12069 /* Called after second pass of multitoc partitioning. */
12070
12071 void
12072 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12073 {
12074 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12075
12076 /* After the second pass, toc_curr tracks the TOC offset used
12077 for code sections below in ppc64_elf_next_input_section. */
12078 htab->toc_curr = TOC_BASE_OFF;
12079 }
12080
12081 /* No toc references were found in ISEC. If the code in ISEC makes no
12082 calls, then there's no need to use toc adjusting stubs when branching
12083 into ISEC. Actually, indirect calls from ISEC are OK as they will
12084 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12085 needed, and 2 if a cyclical call-graph was found but no other reason
12086 for a stub was detected. If called from the top level, a return of
12087 2 means the same as a return of 0. */
12088
12089 static int
12090 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12091 {
12092 int ret;
12093
12094 /* Mark this section as checked. */
12095 isec->call_check_done = 1;
12096
12097 /* We know none of our code bearing sections will need toc stubs. */
12098 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12099 return 0;
12100
12101 if (isec->size == 0)
12102 return 0;
12103
12104 if (isec->output_section == NULL)
12105 return 0;
12106
12107 ret = 0;
12108 if (isec->reloc_count != 0)
12109 {
12110 Elf_Internal_Rela *relstart, *rel;
12111 Elf_Internal_Sym *local_syms;
12112 struct ppc_link_hash_table *htab;
12113
12114 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12115 info->keep_memory);
12116 if (relstart == NULL)
12117 return -1;
12118
12119 /* Look for branches to outside of this section. */
12120 local_syms = NULL;
12121 htab = ppc_hash_table (info);
12122 if (htab == NULL)
12123 return -1;
12124
12125 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12126 {
12127 enum elf_ppc64_reloc_type r_type;
12128 unsigned long r_symndx;
12129 struct elf_link_hash_entry *h;
12130 struct ppc_link_hash_entry *eh;
12131 Elf_Internal_Sym *sym;
12132 asection *sym_sec;
12133 struct _opd_sec_data *opd;
12134 bfd_vma sym_value;
12135 bfd_vma dest;
12136
12137 r_type = ELF64_R_TYPE (rel->r_info);
12138 if (r_type != R_PPC64_REL24
12139 && r_type != R_PPC64_REL14
12140 && r_type != R_PPC64_REL14_BRTAKEN
12141 && r_type != R_PPC64_REL14_BRNTAKEN
12142 && r_type != R_PPC64_PLTCALL)
12143 continue;
12144
12145 r_symndx = ELF64_R_SYM (rel->r_info);
12146 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12147 isec->owner))
12148 {
12149 ret = -1;
12150 break;
12151 }
12152
12153 /* Calls to dynamic lib functions go through a plt call stub
12154 that uses r2. */
12155 eh = (struct ppc_link_hash_entry *) h;
12156 if (eh != NULL
12157 && (eh->elf.plt.plist != NULL
12158 || (eh->oh != NULL
12159 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12160 {
12161 ret = 1;
12162 break;
12163 }
12164
12165 if (sym_sec == NULL)
12166 /* Ignore other undefined symbols. */
12167 continue;
12168
12169 /* Assume branches to other sections not included in the
12170 link need stubs too, to cover -R and absolute syms. */
12171 if (sym_sec->output_section == NULL)
12172 {
12173 ret = 1;
12174 break;
12175 }
12176
12177 if (h == NULL)
12178 sym_value = sym->st_value;
12179 else
12180 {
12181 if (h->root.type != bfd_link_hash_defined
12182 && h->root.type != bfd_link_hash_defweak)
12183 abort ();
12184 sym_value = h->root.u.def.value;
12185 }
12186 sym_value += rel->r_addend;
12187
12188 /* If this branch reloc uses an opd sym, find the code section. */
12189 opd = get_opd_info (sym_sec);
12190 if (opd != NULL)
12191 {
12192 if (h == NULL && opd->adjust != NULL)
12193 {
12194 long adjust;
12195
12196 adjust = opd->adjust[OPD_NDX (sym_value)];
12197 if (adjust == -1)
12198 /* Assume deleted functions won't ever be called. */
12199 continue;
12200 sym_value += adjust;
12201 }
12202
12203 dest = opd_entry_value (sym_sec, sym_value,
12204 &sym_sec, NULL, FALSE);
12205 if (dest == (bfd_vma) -1)
12206 continue;
12207 }
12208 else
12209 dest = (sym_value
12210 + sym_sec->output_offset
12211 + sym_sec->output_section->vma);
12212
12213 /* Ignore branch to self. */
12214 if (sym_sec == isec)
12215 continue;
12216
12217 /* If the called function uses the toc, we need a stub. */
12218 if (sym_sec->has_toc_reloc
12219 || sym_sec->makes_toc_func_call)
12220 {
12221 ret = 1;
12222 break;
12223 }
12224
12225 /* Assume any branch that needs a long branch stub might in fact
12226 need a plt_branch stub. A plt_branch stub uses r2. */
12227 else if (dest - (isec->output_offset
12228 + isec->output_section->vma
12229 + rel->r_offset) + (1 << 25)
12230 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12231 ? h->other
12232 : sym->st_other))
12233 {
12234 ret = 1;
12235 break;
12236 }
12237
12238 /* If calling back to a section in the process of being
12239 tested, we can't say for sure that no toc adjusting stubs
12240 are needed, so don't return zero. */
12241 else if (sym_sec->call_check_in_progress)
12242 ret = 2;
12243
12244 /* Branches to another section that itself doesn't have any TOC
12245 references are OK. Recursively call ourselves to check. */
12246 else if (!sym_sec->call_check_done)
12247 {
12248 int recur;
12249
12250 /* Mark current section as indeterminate, so that other
12251 sections that call back to current won't be marked as
12252 known. */
12253 isec->call_check_in_progress = 1;
12254 recur = toc_adjusting_stub_needed (info, sym_sec);
12255 isec->call_check_in_progress = 0;
12256
12257 if (recur != 0)
12258 {
12259 ret = recur;
12260 if (recur != 2)
12261 break;
12262 }
12263 }
12264 }
12265
12266 if (local_syms != NULL
12267 && (elf_symtab_hdr (isec->owner).contents
12268 != (unsigned char *) local_syms))
12269 free (local_syms);
12270 if (elf_section_data (isec)->relocs != relstart)
12271 free (relstart);
12272 }
12273
12274 if ((ret & 1) == 0
12275 && isec->map_head.s != NULL
12276 && (strcmp (isec->output_section->name, ".init") == 0
12277 || strcmp (isec->output_section->name, ".fini") == 0))
12278 {
12279 if (isec->map_head.s->has_toc_reloc
12280 || isec->map_head.s->makes_toc_func_call)
12281 ret = 1;
12282 else if (!isec->map_head.s->call_check_done)
12283 {
12284 int recur;
12285 isec->call_check_in_progress = 1;
12286 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12287 isec->call_check_in_progress = 0;
12288 if (recur != 0)
12289 ret = recur;
12290 }
12291 }
12292
12293 if (ret == 1)
12294 isec->makes_toc_func_call = 1;
12295
12296 return ret;
12297 }
12298
12299 /* The linker repeatedly calls this function for each input section,
12300 in the order that input sections are linked into output sections.
12301 Build lists of input sections to determine groupings between which
12302 we may insert linker stubs. */
12303
12304 bfd_boolean
12305 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12306 {
12307 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12308
12309 if (htab == NULL)
12310 return FALSE;
12311
12312 if ((isec->output_section->flags & SEC_CODE) != 0
12313 && isec->output_section->id < htab->sec_info_arr_size)
12314 {
12315 /* This happens to make the list in reverse order,
12316 which is what we want. */
12317 htab->sec_info[isec->id].u.list
12318 = htab->sec_info[isec->output_section->id].u.list;
12319 htab->sec_info[isec->output_section->id].u.list = isec;
12320 }
12321
12322 if (htab->multi_toc_needed)
12323 {
12324 /* Analyse sections that aren't already flagged as needing a
12325 valid toc pointer. Exclude .fixup for the linux kernel.
12326 .fixup contains branches, but only back to the function that
12327 hit an exception. */
12328 if (!(isec->has_toc_reloc
12329 || (isec->flags & SEC_CODE) == 0
12330 || strcmp (isec->name, ".fixup") == 0
12331 || isec->call_check_done))
12332 {
12333 if (toc_adjusting_stub_needed (info, isec) < 0)
12334 return FALSE;
12335 }
12336 /* Make all sections use the TOC assigned for this object file.
12337 This will be wrong for pasted sections; We fix that in
12338 check_pasted_section(). */
12339 if (elf_gp (isec->owner) != 0)
12340 htab->toc_curr = elf_gp (isec->owner);
12341 }
12342
12343 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12344 return TRUE;
12345 }
12346
12347 /* Check that all .init and .fini sections use the same toc, if they
12348 have toc relocs. */
12349
12350 static bfd_boolean
12351 check_pasted_section (struct bfd_link_info *info, const char *name)
12352 {
12353 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12354
12355 if (o != NULL)
12356 {
12357 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12358 bfd_vma toc_off = 0;
12359 asection *i;
12360
12361 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12362 if (i->has_toc_reloc)
12363 {
12364 if (toc_off == 0)
12365 toc_off = htab->sec_info[i->id].toc_off;
12366 else if (toc_off != htab->sec_info[i->id].toc_off)
12367 return FALSE;
12368 }
12369
12370 if (toc_off == 0)
12371 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12372 if (i->makes_toc_func_call)
12373 {
12374 toc_off = htab->sec_info[i->id].toc_off;
12375 break;
12376 }
12377
12378 /* Make sure the whole pasted function uses the same toc offset. */
12379 if (toc_off != 0)
12380 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12381 htab->sec_info[i->id].toc_off = toc_off;
12382 }
12383 return TRUE;
12384 }
12385
12386 bfd_boolean
12387 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12388 {
12389 return (check_pasted_section (info, ".init")
12390 & check_pasted_section (info, ".fini"));
12391 }
12392
12393 /* See whether we can group stub sections together. Grouping stub
12394 sections may result in fewer stubs. More importantly, we need to
12395 put all .init* and .fini* stubs at the beginning of the .init or
12396 .fini output sections respectively, because glibc splits the
12397 _init and _fini functions into multiple parts. Putting a stub in
12398 the middle of a function is not a good idea. */
12399
12400 static bfd_boolean
12401 group_sections (struct bfd_link_info *info,
12402 bfd_size_type stub_group_size,
12403 bfd_boolean stubs_always_before_branch)
12404 {
12405 struct ppc_link_hash_table *htab;
12406 asection *osec;
12407 bfd_boolean suppress_size_errors;
12408
12409 htab = ppc_hash_table (info);
12410 if (htab == NULL)
12411 return FALSE;
12412
12413 suppress_size_errors = FALSE;
12414 if (stub_group_size == 1)
12415 {
12416 /* Default values. */
12417 if (stubs_always_before_branch)
12418 stub_group_size = 0x1e00000;
12419 else
12420 stub_group_size = 0x1c00000;
12421 suppress_size_errors = TRUE;
12422 }
12423
12424 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12425 {
12426 asection *tail;
12427
12428 if (osec->id >= htab->sec_info_arr_size)
12429 continue;
12430
12431 tail = htab->sec_info[osec->id].u.list;
12432 while (tail != NULL)
12433 {
12434 asection *curr;
12435 asection *prev;
12436 bfd_size_type total;
12437 bfd_boolean big_sec;
12438 bfd_vma curr_toc;
12439 struct map_stub *group;
12440 bfd_size_type group_size;
12441
12442 curr = tail;
12443 total = tail->size;
12444 group_size = (ppc64_elf_section_data (tail) != NULL
12445 && ppc64_elf_section_data (tail)->has_14bit_branch
12446 ? stub_group_size >> 10 : stub_group_size);
12447
12448 big_sec = total > group_size;
12449 if (big_sec && !suppress_size_errors)
12450 /* xgettext:c-format */
12451 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12452 tail->owner, tail);
12453 curr_toc = htab->sec_info[tail->id].toc_off;
12454
12455 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12456 && ((total += curr->output_offset - prev->output_offset)
12457 < (ppc64_elf_section_data (prev) != NULL
12458 && ppc64_elf_section_data (prev)->has_14bit_branch
12459 ? (group_size = stub_group_size >> 10) : group_size))
12460 && htab->sec_info[prev->id].toc_off == curr_toc)
12461 curr = prev;
12462
12463 /* OK, the size from the start of CURR to the end is less
12464 than group_size and thus can be handled by one stub
12465 section. (or the tail section is itself larger than
12466 group_size, in which case we may be toast.) We should
12467 really be keeping track of the total size of stubs added
12468 here, as stubs contribute to the final output section
12469 size. That's a little tricky, and this way will only
12470 break if stubs added make the total size more than 2^25,
12471 ie. for the default stub_group_size, if stubs total more
12472 than 2097152 bytes, or nearly 75000 plt call stubs. */
12473 group = bfd_alloc (curr->owner, sizeof (*group));
12474 if (group == NULL)
12475 return FALSE;
12476 group->link_sec = curr;
12477 group->stub_sec = NULL;
12478 group->needs_save_res = 0;
12479 group->tls_get_addr_opt_bctrl = -1u;
12480 group->next = htab->group;
12481 htab->group = group;
12482 do
12483 {
12484 prev = htab->sec_info[tail->id].u.list;
12485 /* Set up this stub group. */
12486 htab->sec_info[tail->id].u.group = group;
12487 }
12488 while (tail != curr && (tail = prev) != NULL);
12489
12490 /* But wait, there's more! Input sections up to group_size
12491 bytes before the stub section can be handled by it too.
12492 Don't do this if we have a really large section after the
12493 stubs, as adding more stubs increases the chance that
12494 branches may not reach into the stub section. */
12495 if (!stubs_always_before_branch && !big_sec)
12496 {
12497 total = 0;
12498 while (prev != NULL
12499 && ((total += tail->output_offset - prev->output_offset)
12500 < (ppc64_elf_section_data (prev) != NULL
12501 && ppc64_elf_section_data (prev)->has_14bit_branch
12502 ? (group_size = stub_group_size >> 10) : group_size))
12503 && htab->sec_info[prev->id].toc_off == curr_toc)
12504 {
12505 tail = prev;
12506 prev = htab->sec_info[tail->id].u.list;
12507 htab->sec_info[tail->id].u.group = group;
12508 }
12509 }
12510 tail = prev;
12511 }
12512 }
12513 return TRUE;
12514 }
12515
12516 static const unsigned char glink_eh_frame_cie[] =
12517 {
12518 0, 0, 0, 16, /* length. */
12519 0, 0, 0, 0, /* id. */
12520 1, /* CIE version. */
12521 'z', 'R', 0, /* Augmentation string. */
12522 4, /* Code alignment. */
12523 0x78, /* Data alignment. */
12524 65, /* RA reg. */
12525 1, /* Augmentation size. */
12526 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12527 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12528 };
12529
12530 static size_t
12531 stub_eh_frame_size (struct map_stub *group, size_t align)
12532 {
12533 size_t this_size = 17;
12534 if (group->tls_get_addr_opt_bctrl != -1u)
12535 {
12536 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12537 if (to_bctrl < 64)
12538 this_size += 1;
12539 else if (to_bctrl < 256)
12540 this_size += 2;
12541 else if (to_bctrl < 65536)
12542 this_size += 3;
12543 else
12544 this_size += 5;
12545 this_size += 6;
12546 }
12547 this_size = (this_size + align - 1) & -align;
12548 return this_size;
12549 }
12550
12551 /* Stripping output sections is normally done before dynamic section
12552 symbols have been allocated. This function is called later, and
12553 handles cases like htab->brlt which is mapped to its own output
12554 section. */
12555
12556 static void
12557 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12558 {
12559 if (isec->size == 0
12560 && isec->output_section->size == 0
12561 && !(isec->output_section->flags & SEC_KEEP)
12562 && !bfd_section_removed_from_list (info->output_bfd,
12563 isec->output_section)
12564 && elf_section_data (isec->output_section)->dynindx == 0)
12565 {
12566 isec->output_section->flags |= SEC_EXCLUDE;
12567 bfd_section_list_remove (info->output_bfd, isec->output_section);
12568 info->output_bfd->section_count--;
12569 }
12570 }
12571
12572 /* Determine and set the size of the stub section for a final link.
12573
12574 The basic idea here is to examine all the relocations looking for
12575 PC-relative calls to a target that is unreachable with a "bl"
12576 instruction. */
12577
12578 bfd_boolean
12579 ppc64_elf_size_stubs (struct bfd_link_info *info)
12580 {
12581 bfd_size_type stub_group_size;
12582 bfd_boolean stubs_always_before_branch;
12583 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12584
12585 if (htab == NULL)
12586 return FALSE;
12587
12588 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12589 htab->params->plt_thread_safe = 1;
12590 if (!htab->opd_abi)
12591 htab->params->plt_thread_safe = 0;
12592 else if (htab->params->plt_thread_safe == -1)
12593 {
12594 static const char *const thread_starter[] =
12595 {
12596 "pthread_create",
12597 /* libstdc++ */
12598 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12599 /* librt */
12600 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12601 "mq_notify", "create_timer",
12602 /* libanl */
12603 "getaddrinfo_a",
12604 /* libgomp */
12605 "GOMP_parallel",
12606 "GOMP_parallel_start",
12607 "GOMP_parallel_loop_static",
12608 "GOMP_parallel_loop_static_start",
12609 "GOMP_parallel_loop_dynamic",
12610 "GOMP_parallel_loop_dynamic_start",
12611 "GOMP_parallel_loop_guided",
12612 "GOMP_parallel_loop_guided_start",
12613 "GOMP_parallel_loop_runtime",
12614 "GOMP_parallel_loop_runtime_start",
12615 "GOMP_parallel_sections",
12616 "GOMP_parallel_sections_start",
12617 /* libgo */
12618 "__go_go",
12619 };
12620 unsigned i;
12621
12622 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12623 {
12624 struct elf_link_hash_entry *h;
12625 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12626 FALSE, FALSE, TRUE);
12627 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12628 if (htab->params->plt_thread_safe)
12629 break;
12630 }
12631 }
12632 stubs_always_before_branch = htab->params->group_size < 0;
12633 if (htab->params->group_size < 0)
12634 stub_group_size = -htab->params->group_size;
12635 else
12636 stub_group_size = htab->params->group_size;
12637
12638 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12639 return FALSE;
12640
12641 #define STUB_SHRINK_ITER 20
12642 /* Loop until no stubs added. After iteration 20 of this loop we may
12643 exit on a stub section shrinking. This is to break out of a
12644 pathological case where adding stubs on one iteration decreases
12645 section gaps (perhaps due to alignment), which then requires
12646 fewer or smaller stubs on the next iteration. */
12647
12648 while (1)
12649 {
12650 bfd *input_bfd;
12651 unsigned int bfd_indx;
12652 struct map_stub *group;
12653
12654 htab->stub_iteration += 1;
12655
12656 for (input_bfd = info->input_bfds, bfd_indx = 0;
12657 input_bfd != NULL;
12658 input_bfd = input_bfd->link.next, bfd_indx++)
12659 {
12660 Elf_Internal_Shdr *symtab_hdr;
12661 asection *section;
12662 Elf_Internal_Sym *local_syms = NULL;
12663
12664 if (!is_ppc64_elf (input_bfd))
12665 continue;
12666
12667 /* We'll need the symbol table in a second. */
12668 symtab_hdr = &elf_symtab_hdr (input_bfd);
12669 if (symtab_hdr->sh_info == 0)
12670 continue;
12671
12672 /* Walk over each section attached to the input bfd. */
12673 for (section = input_bfd->sections;
12674 section != NULL;
12675 section = section->next)
12676 {
12677 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12678
12679 /* If there aren't any relocs, then there's nothing more
12680 to do. */
12681 if ((section->flags & SEC_RELOC) == 0
12682 || (section->flags & SEC_ALLOC) == 0
12683 || (section->flags & SEC_LOAD) == 0
12684 || (section->flags & SEC_CODE) == 0
12685 || section->reloc_count == 0)
12686 continue;
12687
12688 /* If this section is a link-once section that will be
12689 discarded, then don't create any stubs. */
12690 if (section->output_section == NULL
12691 || section->output_section->owner != info->output_bfd)
12692 continue;
12693
12694 /* Get the relocs. */
12695 internal_relocs
12696 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12697 info->keep_memory);
12698 if (internal_relocs == NULL)
12699 goto error_ret_free_local;
12700
12701 /* Now examine each relocation. */
12702 irela = internal_relocs;
12703 irelaend = irela + section->reloc_count;
12704 for (; irela < irelaend; irela++)
12705 {
12706 enum elf_ppc64_reloc_type r_type;
12707 unsigned int r_indx;
12708 enum ppc_stub_type stub_type;
12709 struct ppc_stub_hash_entry *stub_entry;
12710 asection *sym_sec, *code_sec;
12711 bfd_vma sym_value, code_value;
12712 bfd_vma destination;
12713 unsigned long local_off;
12714 bfd_boolean ok_dest;
12715 struct ppc_link_hash_entry *hash;
12716 struct ppc_link_hash_entry *fdh;
12717 struct elf_link_hash_entry *h;
12718 Elf_Internal_Sym *sym;
12719 char *stub_name;
12720 const asection *id_sec;
12721 struct _opd_sec_data *opd;
12722 struct plt_entry *plt_ent;
12723
12724 r_type = ELF64_R_TYPE (irela->r_info);
12725 r_indx = ELF64_R_SYM (irela->r_info);
12726
12727 if (r_type >= R_PPC64_max)
12728 {
12729 bfd_set_error (bfd_error_bad_value);
12730 goto error_ret_free_internal;
12731 }
12732
12733 /* Only look for stubs on branch instructions. */
12734 if (r_type != R_PPC64_REL24
12735 && r_type != R_PPC64_REL14
12736 && r_type != R_PPC64_REL14_BRTAKEN
12737 && r_type != R_PPC64_REL14_BRNTAKEN)
12738 continue;
12739
12740 /* Now determine the call target, its name, value,
12741 section. */
12742 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12743 r_indx, input_bfd))
12744 goto error_ret_free_internal;
12745 hash = (struct ppc_link_hash_entry *) h;
12746
12747 ok_dest = FALSE;
12748 fdh = NULL;
12749 sym_value = 0;
12750 if (hash == NULL)
12751 {
12752 sym_value = sym->st_value;
12753 if (sym_sec != NULL
12754 && sym_sec->output_section != NULL)
12755 ok_dest = TRUE;
12756 }
12757 else if (hash->elf.root.type == bfd_link_hash_defined
12758 || hash->elf.root.type == bfd_link_hash_defweak)
12759 {
12760 sym_value = hash->elf.root.u.def.value;
12761 if (sym_sec->output_section != NULL)
12762 ok_dest = TRUE;
12763 }
12764 else if (hash->elf.root.type == bfd_link_hash_undefweak
12765 || hash->elf.root.type == bfd_link_hash_undefined)
12766 {
12767 /* Recognise an old ABI func code entry sym, and
12768 use the func descriptor sym instead if it is
12769 defined. */
12770 if (hash->elf.root.root.string[0] == '.'
12771 && hash->oh != NULL)
12772 {
12773 fdh = ppc_follow_link (hash->oh);
12774 if (fdh->elf.root.type == bfd_link_hash_defined
12775 || fdh->elf.root.type == bfd_link_hash_defweak)
12776 {
12777 sym_sec = fdh->elf.root.u.def.section;
12778 sym_value = fdh->elf.root.u.def.value;
12779 if (sym_sec->output_section != NULL)
12780 ok_dest = TRUE;
12781 }
12782 else
12783 fdh = NULL;
12784 }
12785 }
12786 else
12787 {
12788 bfd_set_error (bfd_error_bad_value);
12789 goto error_ret_free_internal;
12790 }
12791
12792 destination = 0;
12793 local_off = 0;
12794 if (ok_dest)
12795 {
12796 sym_value += irela->r_addend;
12797 destination = (sym_value
12798 + sym_sec->output_offset
12799 + sym_sec->output_section->vma);
12800 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12801 ? hash->elf.other
12802 : sym->st_other);
12803 }
12804
12805 code_sec = sym_sec;
12806 code_value = sym_value;
12807 opd = get_opd_info (sym_sec);
12808 if (opd != NULL)
12809 {
12810 bfd_vma dest;
12811
12812 if (hash == NULL && opd->adjust != NULL)
12813 {
12814 long adjust = opd->adjust[OPD_NDX (sym_value)];
12815 if (adjust == -1)
12816 continue;
12817 code_value += adjust;
12818 sym_value += adjust;
12819 }
12820 dest = opd_entry_value (sym_sec, sym_value,
12821 &code_sec, &code_value, FALSE);
12822 if (dest != (bfd_vma) -1)
12823 {
12824 destination = dest;
12825 if (fdh != NULL)
12826 {
12827 /* Fixup old ABI sym to point at code
12828 entry. */
12829 hash->elf.root.type = bfd_link_hash_defweak;
12830 hash->elf.root.u.def.section = code_sec;
12831 hash->elf.root.u.def.value = code_value;
12832 }
12833 }
12834 }
12835
12836 /* Determine what (if any) linker stub is needed. */
12837 plt_ent = NULL;
12838 stub_type = ppc_type_of_stub (section, irela, &hash,
12839 &plt_ent, destination,
12840 local_off);
12841
12842 if (stub_type != ppc_stub_plt_call)
12843 {
12844 /* Check whether we need a TOC adjusting stub.
12845 Since the linker pastes together pieces from
12846 different object files when creating the
12847 _init and _fini functions, it may be that a
12848 call to what looks like a local sym is in
12849 fact a call needing a TOC adjustment. */
12850 if (code_sec != NULL
12851 && code_sec->output_section != NULL
12852 && (htab->sec_info[code_sec->id].toc_off
12853 != htab->sec_info[section->id].toc_off)
12854 && (code_sec->has_toc_reloc
12855 || code_sec->makes_toc_func_call))
12856 stub_type = ppc_stub_long_branch_r2off;
12857 }
12858
12859 if (stub_type == ppc_stub_none)
12860 continue;
12861
12862 /* __tls_get_addr calls might be eliminated. */
12863 if (stub_type != ppc_stub_plt_call
12864 && hash != NULL
12865 && (hash == htab->tls_get_addr
12866 || hash == htab->tls_get_addr_fd)
12867 && section->has_tls_reloc
12868 && irela != internal_relocs)
12869 {
12870 /* Get tls info. */
12871 unsigned char *tls_mask;
12872
12873 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12874 irela - 1, input_bfd))
12875 goto error_ret_free_internal;
12876 if ((*tls_mask & TLS_TLS) != 0)
12877 continue;
12878 }
12879
12880 if (stub_type == ppc_stub_plt_call)
12881 {
12882 if (!htab->opd_abi
12883 && htab->params->plt_localentry0 != 0
12884 && is_elfv2_localentry0 (&hash->elf))
12885 htab->has_plt_localentry0 = 1;
12886 else if (irela + 1 < irelaend
12887 && irela[1].r_offset == irela->r_offset + 4
12888 && (ELF64_R_TYPE (irela[1].r_info)
12889 == R_PPC64_TOCSAVE))
12890 {
12891 if (!tocsave_find (htab, INSERT,
12892 &local_syms, irela + 1, input_bfd))
12893 goto error_ret_free_internal;
12894 }
12895 else
12896 stub_type = ppc_stub_plt_call_r2save;
12897 }
12898
12899 /* Support for grouping stub sections. */
12900 id_sec = htab->sec_info[section->id].u.group->link_sec;
12901
12902 /* Get the name of this stub. */
12903 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12904 if (!stub_name)
12905 goto error_ret_free_internal;
12906
12907 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12908 stub_name, FALSE, FALSE);
12909 if (stub_entry != NULL)
12910 {
12911 /* The proper stub has already been created. */
12912 free (stub_name);
12913 if (stub_type == ppc_stub_plt_call_r2save)
12914 stub_entry->stub_type = stub_type;
12915 continue;
12916 }
12917
12918 stub_entry = ppc_add_stub (stub_name, section, info);
12919 if (stub_entry == NULL)
12920 {
12921 free (stub_name);
12922 error_ret_free_internal:
12923 if (elf_section_data (section)->relocs == NULL)
12924 free (internal_relocs);
12925 error_ret_free_local:
12926 if (local_syms != NULL
12927 && (symtab_hdr->contents
12928 != (unsigned char *) local_syms))
12929 free (local_syms);
12930 return FALSE;
12931 }
12932
12933 stub_entry->stub_type = stub_type;
12934 if (stub_type != ppc_stub_plt_call
12935 && stub_type != ppc_stub_plt_call_r2save)
12936 {
12937 stub_entry->target_value = code_value;
12938 stub_entry->target_section = code_sec;
12939 }
12940 else
12941 {
12942 stub_entry->target_value = sym_value;
12943 stub_entry->target_section = sym_sec;
12944 }
12945 stub_entry->h = hash;
12946 stub_entry->plt_ent = plt_ent;
12947 stub_entry->symtype
12948 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
12949 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12950
12951 if (stub_entry->h != NULL)
12952 htab->stub_globals += 1;
12953 }
12954
12955 /* We're done with the internal relocs, free them. */
12956 if (elf_section_data (section)->relocs != internal_relocs)
12957 free (internal_relocs);
12958 }
12959
12960 if (local_syms != NULL
12961 && symtab_hdr->contents != (unsigned char *) local_syms)
12962 {
12963 if (!info->keep_memory)
12964 free (local_syms);
12965 else
12966 symtab_hdr->contents = (unsigned char *) local_syms;
12967 }
12968 }
12969
12970 /* We may have added some stubs. Find out the new size of the
12971 stub sections. */
12972 for (group = htab->group; group != NULL; group = group->next)
12973 if (group->stub_sec != NULL)
12974 {
12975 asection *stub_sec = group->stub_sec;
12976
12977 if (htab->stub_iteration <= STUB_SHRINK_ITER
12978 || stub_sec->rawsize < stub_sec->size)
12979 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12980 stub_sec->rawsize = stub_sec->size;
12981 stub_sec->size = 0;
12982 stub_sec->reloc_count = 0;
12983 stub_sec->flags &= ~SEC_RELOC;
12984 }
12985
12986 if (htab->stub_iteration <= STUB_SHRINK_ITER
12987 || htab->brlt->rawsize < htab->brlt->size)
12988 htab->brlt->rawsize = htab->brlt->size;
12989 htab->brlt->size = 0;
12990 htab->brlt->reloc_count = 0;
12991 htab->brlt->flags &= ~SEC_RELOC;
12992 if (htab->relbrlt != NULL)
12993 htab->relbrlt->size = 0;
12994
12995 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12996
12997 for (group = htab->group; group != NULL; group = group->next)
12998 if (group->needs_save_res)
12999 group->stub_sec->size += htab->sfpr->size;
13000
13001 if (info->emitrelocations
13002 && htab->glink != NULL && htab->glink->size != 0)
13003 {
13004 htab->glink->reloc_count = 1;
13005 htab->glink->flags |= SEC_RELOC;
13006 }
13007
13008 if (htab->glink_eh_frame != NULL
13009 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13010 && htab->glink_eh_frame->output_section->size > 8)
13011 {
13012 size_t size = 0, align = 4;
13013
13014 for (group = htab->group; group != NULL; group = group->next)
13015 if (group->stub_sec != NULL)
13016 size += stub_eh_frame_size (group, align);
13017 if (htab->glink != NULL && htab->glink->size != 0)
13018 size += (24 + align - 1) & -align;
13019 if (size != 0)
13020 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13021 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13022 size = (size + align - 1) & -align;
13023 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13024 htab->glink_eh_frame->size = size;
13025 }
13026
13027 if (htab->params->plt_stub_align != 0)
13028 for (group = htab->group; group != NULL; group = group->next)
13029 if (group->stub_sec != NULL)
13030 {
13031 int align = abs (htab->params->plt_stub_align);
13032 group->stub_sec->size
13033 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13034 }
13035
13036 for (group = htab->group; group != NULL; group = group->next)
13037 if (group->stub_sec != NULL
13038 && group->stub_sec->rawsize != group->stub_sec->size
13039 && (htab->stub_iteration <= STUB_SHRINK_ITER
13040 || group->stub_sec->rawsize < group->stub_sec->size))
13041 break;
13042
13043 if (group == NULL
13044 && (htab->brlt->rawsize == htab->brlt->size
13045 || (htab->stub_iteration > STUB_SHRINK_ITER
13046 && htab->brlt->rawsize > htab->brlt->size))
13047 && (htab->glink_eh_frame == NULL
13048 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13049 break;
13050
13051 /* Ask the linker to do its stuff. */
13052 (*htab->params->layout_sections_again) ();
13053 }
13054
13055 if (htab->glink_eh_frame != NULL
13056 && htab->glink_eh_frame->size != 0)
13057 {
13058 bfd_vma val;
13059 bfd_byte *p, *last_fde;
13060 size_t last_fde_len, size, align, pad;
13061 struct map_stub *group;
13062
13063 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13064 if (p == NULL)
13065 return FALSE;
13066 htab->glink_eh_frame->contents = p;
13067 last_fde = p;
13068 align = 4;
13069
13070 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13071 /* CIE length (rewrite in case little-endian). */
13072 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13073 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13074 p += last_fde_len + 4;
13075
13076 for (group = htab->group; group != NULL; group = group->next)
13077 if (group->stub_sec != NULL)
13078 {
13079 last_fde = p;
13080 last_fde_len = stub_eh_frame_size (group, align) - 4;
13081 /* FDE length. */
13082 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13083 p += 4;
13084 /* CIE pointer. */
13085 val = p - htab->glink_eh_frame->contents;
13086 bfd_put_32 (htab->elf.dynobj, val, p);
13087 p += 4;
13088 /* Offset to stub section, written later. */
13089 p += 4;
13090 /* stub section size. */
13091 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13092 p += 4;
13093 /* Augmentation. */
13094 p += 1;
13095 if (group->tls_get_addr_opt_bctrl != -1u)
13096 {
13097 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13098
13099 /* This FDE needs more than just the default.
13100 Describe __tls_get_addr_opt stub LR. */
13101 if (to_bctrl < 64)
13102 *p++ = DW_CFA_advance_loc + to_bctrl;
13103 else if (to_bctrl < 256)
13104 {
13105 *p++ = DW_CFA_advance_loc1;
13106 *p++ = to_bctrl;
13107 }
13108 else if (to_bctrl < 65536)
13109 {
13110 *p++ = DW_CFA_advance_loc2;
13111 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13112 p += 2;
13113 }
13114 else
13115 {
13116 *p++ = DW_CFA_advance_loc4;
13117 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13118 p += 4;
13119 }
13120 *p++ = DW_CFA_offset_extended_sf;
13121 *p++ = 65;
13122 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13123 *p++ = DW_CFA_advance_loc + 4;
13124 *p++ = DW_CFA_restore_extended;
13125 *p++ = 65;
13126 }
13127 /* Pad. */
13128 p = last_fde + last_fde_len + 4;
13129 }
13130 if (htab->glink != NULL && htab->glink->size != 0)
13131 {
13132 last_fde = p;
13133 last_fde_len = ((24 + align - 1) & -align) - 4;
13134 /* FDE length. */
13135 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13136 p += 4;
13137 /* CIE pointer. */
13138 val = p - htab->glink_eh_frame->contents;
13139 bfd_put_32 (htab->elf.dynobj, val, p);
13140 p += 4;
13141 /* Offset to .glink, written later. */
13142 p += 4;
13143 /* .glink size. */
13144 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13145 p += 4;
13146 /* Augmentation. */
13147 p += 1;
13148
13149 *p++ = DW_CFA_advance_loc + 1;
13150 *p++ = DW_CFA_register;
13151 *p++ = 65;
13152 *p++ = htab->opd_abi ? 12 : 0;
13153 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13154 *p++ = DW_CFA_restore_extended;
13155 *p++ = 65;
13156 p += ((24 + align - 1) & -align) - 24;
13157 }
13158 /* Subsume any padding into the last FDE if user .eh_frame
13159 sections are aligned more than glink_eh_frame. Otherwise any
13160 zero padding will be seen as a terminator. */
13161 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13162 size = p - htab->glink_eh_frame->contents;
13163 pad = ((size + align - 1) & -align) - size;
13164 htab->glink_eh_frame->size = size + pad;
13165 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13166 }
13167
13168 maybe_strip_output (info, htab->brlt);
13169 if (htab->glink_eh_frame != NULL)
13170 maybe_strip_output (info, htab->glink_eh_frame);
13171
13172 return TRUE;
13173 }
13174
13175 /* Called after we have determined section placement. If sections
13176 move, we'll be called again. Provide a value for TOCstart. */
13177
13178 bfd_vma
13179 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13180 {
13181 asection *s;
13182 bfd_vma TOCstart, adjust;
13183
13184 if (info != NULL)
13185 {
13186 struct elf_link_hash_entry *h;
13187 struct elf_link_hash_table *htab = elf_hash_table (info);
13188
13189 if (is_elf_hash_table (htab)
13190 && htab->hgot != NULL)
13191 h = htab->hgot;
13192 else
13193 {
13194 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13195 if (is_elf_hash_table (htab))
13196 htab->hgot = h;
13197 }
13198 if (h != NULL
13199 && h->root.type == bfd_link_hash_defined
13200 && !h->root.linker_def
13201 && (!is_elf_hash_table (htab)
13202 || h->def_regular))
13203 {
13204 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13205 + h->root.u.def.section->output_offset
13206 + h->root.u.def.section->output_section->vma);
13207 _bfd_set_gp_value (obfd, TOCstart);
13208 return TOCstart;
13209 }
13210 }
13211
13212 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13213 order. The TOC starts where the first of these sections starts. */
13214 s = bfd_get_section_by_name (obfd, ".got");
13215 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13216 s = bfd_get_section_by_name (obfd, ".toc");
13217 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13218 s = bfd_get_section_by_name (obfd, ".tocbss");
13219 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13220 s = bfd_get_section_by_name (obfd, ".plt");
13221 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13222 {
13223 /* This may happen for
13224 o references to TOC base (SYM@toc / TOC[tc0]) without a
13225 .toc directive
13226 o bad linker script
13227 o --gc-sections and empty TOC sections
13228
13229 FIXME: Warn user? */
13230
13231 /* Look for a likely section. We probably won't even be
13232 using TOCstart. */
13233 for (s = obfd->sections; s != NULL; s = s->next)
13234 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13235 | SEC_EXCLUDE))
13236 == (SEC_ALLOC | SEC_SMALL_DATA))
13237 break;
13238 if (s == NULL)
13239 for (s = obfd->sections; s != NULL; s = s->next)
13240 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13241 == (SEC_ALLOC | SEC_SMALL_DATA))
13242 break;
13243 if (s == NULL)
13244 for (s = obfd->sections; s != NULL; s = s->next)
13245 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13246 == SEC_ALLOC)
13247 break;
13248 if (s == NULL)
13249 for (s = obfd->sections; s != NULL; s = s->next)
13250 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13251 break;
13252 }
13253
13254 TOCstart = 0;
13255 if (s != NULL)
13256 TOCstart = s->output_section->vma + s->output_offset;
13257
13258 /* Force alignment. */
13259 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13260 TOCstart -= adjust;
13261 _bfd_set_gp_value (obfd, TOCstart);
13262
13263 if (info != NULL && s != NULL)
13264 {
13265 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13266
13267 if (htab != NULL)
13268 {
13269 if (htab->elf.hgot != NULL)
13270 {
13271 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13272 htab->elf.hgot->root.u.def.section = s;
13273 }
13274 }
13275 else
13276 {
13277 struct bfd_link_hash_entry *bh = NULL;
13278 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13279 s, TOC_BASE_OFF - adjust,
13280 NULL, FALSE, FALSE, &bh);
13281 }
13282 }
13283 return TOCstart;
13284 }
13285
13286 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13287 write out any global entry stubs, and PLT relocations. */
13288
13289 static bfd_boolean
13290 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13291 {
13292 struct bfd_link_info *info;
13293 struct ppc_link_hash_table *htab;
13294 struct plt_entry *ent;
13295 asection *s;
13296
13297 if (h->root.type == bfd_link_hash_indirect)
13298 return TRUE;
13299
13300 info = inf;
13301 htab = ppc_hash_table (info);
13302 if (htab == NULL)
13303 return FALSE;
13304
13305 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13306 if (ent->plt.offset != (bfd_vma) -1)
13307 {
13308 /* This symbol has an entry in the procedure linkage
13309 table. Set it up. */
13310 Elf_Internal_Rela rela;
13311 asection *plt, *relplt;
13312 bfd_byte *loc;
13313
13314 if (!htab->elf.dynamic_sections_created
13315 || h->dynindx == -1)
13316 {
13317 if (!(h->def_regular
13318 && (h->root.type == bfd_link_hash_defined
13319 || h->root.type == bfd_link_hash_defweak)))
13320 continue;
13321 if (h->type == STT_GNU_IFUNC)
13322 {
13323 plt = htab->elf.iplt;
13324 relplt = htab->elf.irelplt;
13325 htab->local_ifunc_resolver = 1;
13326 if (htab->opd_abi)
13327 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13328 else
13329 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13330 }
13331 else
13332 {
13333 plt = htab->pltlocal;
13334 if (bfd_link_pic (info))
13335 {
13336 relplt = htab->relpltlocal;
13337 if (htab->opd_abi)
13338 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13339 else
13340 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13341 }
13342 else
13343 relplt = NULL;
13344 }
13345 rela.r_addend = (h->root.u.def.value
13346 + h->root.u.def.section->output_offset
13347 + h->root.u.def.section->output_section->vma
13348 + ent->addend);
13349
13350 if (relplt == NULL)
13351 {
13352 loc = plt->contents + ent->plt.offset;
13353 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13354 if (htab->opd_abi)
13355 {
13356 bfd_vma toc = elf_gp (info->output_bfd);
13357 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13358 bfd_put_64 (info->output_bfd, toc, loc + 8);
13359 }
13360 }
13361 else
13362 {
13363 rela.r_offset = (plt->output_section->vma
13364 + plt->output_offset
13365 + ent->plt.offset);
13366 loc = relplt->contents + (relplt->reloc_count++
13367 * sizeof (Elf64_External_Rela));
13368 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13369 }
13370 }
13371 else
13372 {
13373 rela.r_offset = (htab->elf.splt->output_section->vma
13374 + htab->elf.splt->output_offset
13375 + ent->plt.offset);
13376 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13377 rela.r_addend = ent->addend;
13378 loc = (htab->elf.srelplt->contents
13379 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13380 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13381 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13382 htab->maybe_local_ifunc_resolver = 1;
13383 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13384 }
13385 }
13386
13387 if (!h->pointer_equality_needed)
13388 return TRUE;
13389
13390 if (h->def_regular)
13391 return TRUE;
13392
13393 s = htab->global_entry;
13394 if (s == NULL || s->size == 0)
13395 return TRUE;
13396
13397 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13398 if (ent->plt.offset != (bfd_vma) -1
13399 && ent->addend == 0)
13400 {
13401 bfd_byte *p;
13402 asection *plt;
13403 bfd_vma off;
13404
13405 p = s->contents + h->root.u.def.value;
13406 plt = htab->elf.splt;
13407 if (!htab->elf.dynamic_sections_created
13408 || h->dynindx == -1)
13409 {
13410 if (h->type == STT_GNU_IFUNC)
13411 plt = htab->elf.iplt;
13412 else
13413 plt = htab->pltlocal;
13414 }
13415 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13416 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13417
13418 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13419 {
13420 info->callbacks->einfo
13421 (_("%P: linkage table error against `%pT'\n"),
13422 h->root.root.string);
13423 bfd_set_error (bfd_error_bad_value);
13424 htab->stub_error = TRUE;
13425 }
13426
13427 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13428 if (htab->params->emit_stub_syms)
13429 {
13430 size_t len = strlen (h->root.root.string);
13431 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13432
13433 if (name == NULL)
13434 return FALSE;
13435
13436 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13437 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13438 if (h == NULL)
13439 return FALSE;
13440 if (h->root.type == bfd_link_hash_new)
13441 {
13442 h->root.type = bfd_link_hash_defined;
13443 h->root.u.def.section = s;
13444 h->root.u.def.value = p - s->contents;
13445 h->ref_regular = 1;
13446 h->def_regular = 1;
13447 h->ref_regular_nonweak = 1;
13448 h->forced_local = 1;
13449 h->non_elf = 0;
13450 h->root.linker_def = 1;
13451 }
13452 }
13453
13454 if (PPC_HA (off) != 0)
13455 {
13456 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13457 p += 4;
13458 }
13459 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13460 p += 4;
13461 bfd_put_32 (s->owner, MTCTR_R12, p);
13462 p += 4;
13463 bfd_put_32 (s->owner, BCTR, p);
13464 break;
13465 }
13466 return TRUE;
13467 }
13468
13469 /* Write PLT relocs for locals. */
13470
13471 static bfd_boolean
13472 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13473 {
13474 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13475 bfd *ibfd;
13476
13477 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13478 {
13479 struct got_entry **lgot_ents, **end_lgot_ents;
13480 struct plt_entry **local_plt, **lplt, **end_local_plt;
13481 Elf_Internal_Shdr *symtab_hdr;
13482 bfd_size_type locsymcount;
13483 Elf_Internal_Sym *local_syms = NULL;
13484 struct plt_entry *ent;
13485
13486 if (!is_ppc64_elf (ibfd))
13487 continue;
13488
13489 lgot_ents = elf_local_got_ents (ibfd);
13490 if (!lgot_ents)
13491 continue;
13492
13493 symtab_hdr = &elf_symtab_hdr (ibfd);
13494 locsymcount = symtab_hdr->sh_info;
13495 end_lgot_ents = lgot_ents + locsymcount;
13496 local_plt = (struct plt_entry **) end_lgot_ents;
13497 end_local_plt = local_plt + locsymcount;
13498 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13499 for (ent = *lplt; ent != NULL; ent = ent->next)
13500 if (ent->plt.offset != (bfd_vma) -1)
13501 {
13502 Elf_Internal_Sym *sym;
13503 asection *sym_sec;
13504 asection *plt, *relplt;
13505 bfd_byte *loc;
13506 bfd_vma val;
13507
13508 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13509 lplt - local_plt, ibfd))
13510 {
13511 if (local_syms != NULL
13512 && symtab_hdr->contents != (unsigned char *) local_syms)
13513 free (local_syms);
13514 return FALSE;
13515 }
13516
13517 val = sym->st_value + ent->addend;
13518 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13519 if (sym_sec != NULL && sym_sec->output_section != NULL)
13520 val += sym_sec->output_offset + sym_sec->output_section->vma;
13521
13522 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13523 {
13524 htab->local_ifunc_resolver = 1;
13525 plt = htab->elf.iplt;
13526 relplt = htab->elf.irelplt;
13527 }
13528 else
13529 {
13530 plt = htab->pltlocal;
13531 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13532 }
13533
13534 if (relplt == NULL)
13535 {
13536 loc = plt->contents + ent->plt.offset;
13537 bfd_put_64 (info->output_bfd, val, loc);
13538 if (htab->opd_abi)
13539 {
13540 bfd_vma toc = elf_gp (ibfd);
13541 bfd_put_64 (info->output_bfd, toc, loc + 8);
13542 }
13543 }
13544 else
13545 {
13546 Elf_Internal_Rela rela;
13547 rela.r_offset = (ent->plt.offset
13548 + plt->output_offset
13549 + plt->output_section->vma);
13550 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13551 {
13552 if (htab->opd_abi)
13553 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13554 else
13555 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13556 }
13557 else
13558 {
13559 if (htab->opd_abi)
13560 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13561 else
13562 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13563 }
13564 rela.r_addend = val;
13565 loc = relplt->contents + (relplt->reloc_count++
13566 * sizeof (Elf64_External_Rela));
13567 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13568 }
13569 }
13570
13571 if (local_syms != NULL
13572 && symtab_hdr->contents != (unsigned char *) local_syms)
13573 {
13574 if (!info->keep_memory)
13575 free (local_syms);
13576 else
13577 symtab_hdr->contents = (unsigned char *) local_syms;
13578 }
13579 }
13580 return TRUE;
13581 }
13582
13583 /* Build all the stubs associated with the current output file.
13584 The stubs are kept in a hash table attached to the main linker
13585 hash table. This function is called via gldelf64ppc_finish. */
13586
13587 bfd_boolean
13588 ppc64_elf_build_stubs (struct bfd_link_info *info,
13589 char **stats)
13590 {
13591 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13592 struct map_stub *group;
13593 asection *stub_sec;
13594 bfd_byte *p;
13595 int stub_sec_count = 0;
13596
13597 if (htab == NULL)
13598 return FALSE;
13599
13600 /* Allocate memory to hold the linker stubs. */
13601 for (group = htab->group; group != NULL; group = group->next)
13602 if ((stub_sec = group->stub_sec) != NULL
13603 && stub_sec->size != 0)
13604 {
13605 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13606 if (stub_sec->contents == NULL)
13607 return FALSE;
13608 stub_sec->size = 0;
13609 }
13610
13611 if (htab->glink != NULL && htab->glink->size != 0)
13612 {
13613 unsigned int indx;
13614 bfd_vma plt0;
13615
13616 /* Build the .glink plt call stub. */
13617 if (htab->params->emit_stub_syms)
13618 {
13619 struct elf_link_hash_entry *h;
13620 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13621 TRUE, FALSE, FALSE);
13622 if (h == NULL)
13623 return FALSE;
13624 if (h->root.type == bfd_link_hash_new)
13625 {
13626 h->root.type = bfd_link_hash_defined;
13627 h->root.u.def.section = htab->glink;
13628 h->root.u.def.value = 8;
13629 h->ref_regular = 1;
13630 h->def_regular = 1;
13631 h->ref_regular_nonweak = 1;
13632 h->forced_local = 1;
13633 h->non_elf = 0;
13634 h->root.linker_def = 1;
13635 }
13636 }
13637 plt0 = (htab->elf.splt->output_section->vma
13638 + htab->elf.splt->output_offset
13639 - 16);
13640 if (info->emitrelocations)
13641 {
13642 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13643 if (r == NULL)
13644 return FALSE;
13645 r->r_offset = (htab->glink->output_offset
13646 + htab->glink->output_section->vma);
13647 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13648 r->r_addend = plt0;
13649 }
13650 p = htab->glink->contents;
13651 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13652 bfd_put_64 (htab->glink->owner, plt0, p);
13653 p += 8;
13654 if (htab->opd_abi)
13655 {
13656 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13657 p += 4;
13658 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13659 p += 4;
13660 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13661 p += 4;
13662 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13663 p += 4;
13664 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13665 p += 4;
13666 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13667 p += 4;
13668 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13669 p += 4;
13670 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13671 p += 4;
13672 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13673 p += 4;
13674 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13675 p += 4;
13676 }
13677 else
13678 {
13679 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13680 p += 4;
13681 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13682 p += 4;
13683 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13684 p += 4;
13685 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13686 p += 4;
13687 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13688 p += 4;
13689 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13690 p += 4;
13691 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13692 p += 4;
13693 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13694 p += 4;
13695 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13696 p += 4;
13697 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13698 p += 4;
13699 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13700 p += 4;
13701 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13702 p += 4;
13703 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13704 p += 4;
13705 }
13706 bfd_put_32 (htab->glink->owner, BCTR, p);
13707 p += 4;
13708 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13709
13710 /* Build the .glink lazy link call stubs. */
13711 indx = 0;
13712 while (p < htab->glink->contents + htab->glink->size)
13713 {
13714 if (htab->opd_abi)
13715 {
13716 if (indx < 0x8000)
13717 {
13718 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13719 p += 4;
13720 }
13721 else
13722 {
13723 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13724 p += 4;
13725 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13726 p);
13727 p += 4;
13728 }
13729 }
13730 bfd_put_32 (htab->glink->owner,
13731 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13732 indx++;
13733 p += 4;
13734 }
13735 }
13736
13737 /* Build .glink global entry stubs, and PLT relocs for globals. */
13738 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
13739
13740 if (!write_plt_relocs_for_local_syms (info))
13741 return FALSE;
13742
13743 if (htab->brlt != NULL && htab->brlt->size != 0)
13744 {
13745 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13746 htab->brlt->size);
13747 if (htab->brlt->contents == NULL)
13748 return FALSE;
13749 }
13750 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13751 {
13752 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13753 htab->relbrlt->size);
13754 if (htab->relbrlt->contents == NULL)
13755 return FALSE;
13756 }
13757
13758 /* Build the stubs as directed by the stub hash table. */
13759 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13760
13761 for (group = htab->group; group != NULL; group = group->next)
13762 if (group->needs_save_res)
13763 group->stub_sec->size += htab->sfpr->size;
13764
13765 if (htab->relbrlt != NULL)
13766 htab->relbrlt->reloc_count = 0;
13767
13768 if (htab->params->plt_stub_align != 0)
13769 for (group = htab->group; group != NULL; group = group->next)
13770 if ((stub_sec = group->stub_sec) != NULL)
13771 {
13772 int align = abs (htab->params->plt_stub_align);
13773 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13774 }
13775
13776 for (group = htab->group; group != NULL; group = group->next)
13777 if (group->needs_save_res)
13778 {
13779 stub_sec = group->stub_sec;
13780 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
13781 htab->sfpr->contents, htab->sfpr->size);
13782 if (htab->params->emit_stub_syms)
13783 {
13784 unsigned int i;
13785
13786 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13787 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13788 return FALSE;
13789 }
13790 }
13791
13792 for (group = htab->group; group != NULL; group = group->next)
13793 if ((stub_sec = group->stub_sec) != NULL)
13794 {
13795 stub_sec_count += 1;
13796 if (stub_sec->rawsize != stub_sec->size
13797 && (htab->stub_iteration <= STUB_SHRINK_ITER
13798 || stub_sec->rawsize < stub_sec->size))
13799 break;
13800 }
13801
13802 if (group != NULL)
13803 {
13804 htab->stub_error = TRUE;
13805 _bfd_error_handler (_("stubs don't match calculated size"));
13806 }
13807
13808 if (htab->stub_error)
13809 return FALSE;
13810
13811 if (stats != NULL)
13812 {
13813 size_t len;
13814 *stats = bfd_malloc (500);
13815 if (*stats == NULL)
13816 return FALSE;
13817
13818 len = sprintf (*stats,
13819 ngettext ("linker stubs in %u group\n",
13820 "linker stubs in %u groups\n",
13821 stub_sec_count),
13822 stub_sec_count);
13823 sprintf (*stats + len, _(" branch %lu\n"
13824 " toc adjust %lu\n"
13825 " long branch %lu\n"
13826 " long toc adj %lu\n"
13827 " plt call %lu\n"
13828 " plt call toc %lu\n"
13829 " global entry %lu"),
13830 htab->stub_count[ppc_stub_long_branch - 1],
13831 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13832 htab->stub_count[ppc_stub_plt_branch - 1],
13833 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13834 htab->stub_count[ppc_stub_plt_call - 1],
13835 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13836 htab->stub_count[ppc_stub_global_entry - 1]);
13837 }
13838 return TRUE;
13839 }
13840
13841 /* What to do when ld finds relocations against symbols defined in
13842 discarded sections. */
13843
13844 static unsigned int
13845 ppc64_elf_action_discarded (asection *sec)
13846 {
13847 if (strcmp (".opd", sec->name) == 0)
13848 return 0;
13849
13850 if (strcmp (".toc", sec->name) == 0)
13851 return 0;
13852
13853 if (strcmp (".toc1", sec->name) == 0)
13854 return 0;
13855
13856 return _bfd_elf_default_action_discarded (sec);
13857 }
13858
13859 /* The RELOCATE_SECTION function is called by the ELF backend linker
13860 to handle the relocations for a section.
13861
13862 The relocs are always passed as Rela structures; if the section
13863 actually uses Rel structures, the r_addend field will always be
13864 zero.
13865
13866 This function is responsible for adjust the section contents as
13867 necessary, and (if using Rela relocs and generating a
13868 relocatable output file) adjusting the reloc addend as
13869 necessary.
13870
13871 This function does not have to worry about setting the reloc
13872 address or the reloc symbol index.
13873
13874 LOCAL_SYMS is a pointer to the swapped in local symbols.
13875
13876 LOCAL_SECTIONS is an array giving the section in the input file
13877 corresponding to the st_shndx field of each local symbol.
13878
13879 The global hash table entry for the global symbols can be found
13880 via elf_sym_hashes (input_bfd).
13881
13882 When generating relocatable output, this function must handle
13883 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13884 going to be the section symbol corresponding to the output
13885 section, which means that the addend must be adjusted
13886 accordingly. */
13887
13888 static bfd_boolean
13889 ppc64_elf_relocate_section (bfd *output_bfd,
13890 struct bfd_link_info *info,
13891 bfd *input_bfd,
13892 asection *input_section,
13893 bfd_byte *contents,
13894 Elf_Internal_Rela *relocs,
13895 Elf_Internal_Sym *local_syms,
13896 asection **local_sections)
13897 {
13898 struct ppc_link_hash_table *htab;
13899 Elf_Internal_Shdr *symtab_hdr;
13900 struct elf_link_hash_entry **sym_hashes;
13901 Elf_Internal_Rela *rel;
13902 Elf_Internal_Rela *wrel;
13903 Elf_Internal_Rela *relend;
13904 Elf_Internal_Rela outrel;
13905 bfd_byte *loc;
13906 struct got_entry **local_got_ents;
13907 bfd_vma TOCstart;
13908 bfd_boolean ret = TRUE;
13909 bfd_boolean is_opd;
13910 /* Assume 'at' branch hints. */
13911 bfd_boolean is_isa_v2 = TRUE;
13912 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13913
13914 /* Initialize howto table if needed. */
13915 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13916 ppc_howto_init ();
13917
13918 htab = ppc_hash_table (info);
13919 if (htab == NULL)
13920 return FALSE;
13921
13922 /* Don't relocate stub sections. */
13923 if (input_section->owner == htab->params->stub_bfd)
13924 return TRUE;
13925
13926 BFD_ASSERT (is_ppc64_elf (input_bfd));
13927
13928 local_got_ents = elf_local_got_ents (input_bfd);
13929 TOCstart = elf_gp (output_bfd);
13930 symtab_hdr = &elf_symtab_hdr (input_bfd);
13931 sym_hashes = elf_sym_hashes (input_bfd);
13932 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13933
13934 rel = wrel = relocs;
13935 relend = relocs + input_section->reloc_count;
13936 for (; rel < relend; wrel++, rel++)
13937 {
13938 enum elf_ppc64_reloc_type r_type;
13939 bfd_vma addend;
13940 bfd_reloc_status_type r;
13941 Elf_Internal_Sym *sym;
13942 asection *sec;
13943 struct elf_link_hash_entry *h_elf;
13944 struct ppc_link_hash_entry *h;
13945 struct ppc_link_hash_entry *fdh;
13946 const char *sym_name;
13947 unsigned long r_symndx, toc_symndx;
13948 bfd_vma toc_addend;
13949 unsigned char tls_mask, tls_gd, tls_type;
13950 unsigned char sym_type;
13951 bfd_vma relocation;
13952 bfd_boolean unresolved_reloc, save_unresolved_reloc;
13953 bfd_boolean warned;
13954 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13955 unsigned int insn;
13956 unsigned int mask;
13957 struct ppc_stub_hash_entry *stub_entry;
13958 bfd_vma max_br_offset;
13959 bfd_vma from;
13960 Elf_Internal_Rela orig_rel;
13961 reloc_howto_type *howto;
13962 struct reloc_howto_struct alt_howto;
13963
13964 again:
13965 orig_rel = *rel;
13966
13967 r_type = ELF64_R_TYPE (rel->r_info);
13968 r_symndx = ELF64_R_SYM (rel->r_info);
13969
13970 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13971 symbol of the previous ADDR64 reloc. The symbol gives us the
13972 proper TOC base to use. */
13973 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13974 && wrel != relocs
13975 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13976 && is_opd)
13977 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13978
13979 sym = NULL;
13980 sec = NULL;
13981 h_elf = NULL;
13982 sym_name = NULL;
13983 unresolved_reloc = FALSE;
13984 warned = FALSE;
13985
13986 if (r_symndx < symtab_hdr->sh_info)
13987 {
13988 /* It's a local symbol. */
13989 struct _opd_sec_data *opd;
13990
13991 sym = local_syms + r_symndx;
13992 sec = local_sections[r_symndx];
13993 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13994 sym_type = ELF64_ST_TYPE (sym->st_info);
13995 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13996 opd = get_opd_info (sec);
13997 if (opd != NULL && opd->adjust != NULL)
13998 {
13999 long adjust = opd->adjust[OPD_NDX (sym->st_value
14000 + rel->r_addend)];
14001 if (adjust == -1)
14002 relocation = 0;
14003 else
14004 {
14005 /* If this is a relocation against the opd section sym
14006 and we have edited .opd, adjust the reloc addend so
14007 that ld -r and ld --emit-relocs output is correct.
14008 If it is a reloc against some other .opd symbol,
14009 then the symbol value will be adjusted later. */
14010 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14011 rel->r_addend += adjust;
14012 else
14013 relocation += adjust;
14014 }
14015 }
14016 }
14017 else
14018 {
14019 bfd_boolean ignored;
14020
14021 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14022 r_symndx, symtab_hdr, sym_hashes,
14023 h_elf, sec, relocation,
14024 unresolved_reloc, warned, ignored);
14025 sym_name = h_elf->root.root.string;
14026 sym_type = h_elf->type;
14027 if (sec != NULL
14028 && sec->owner == output_bfd
14029 && strcmp (sec->name, ".opd") == 0)
14030 {
14031 /* This is a symbol defined in a linker script. All
14032 such are defined in output sections, even those
14033 defined by simple assignment from a symbol defined in
14034 an input section. Transfer the symbol to an
14035 appropriate input .opd section, so that a branch to
14036 this symbol will be mapped to the location specified
14037 by the opd entry. */
14038 struct bfd_link_order *lo;
14039 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14040 if (lo->type == bfd_indirect_link_order)
14041 {
14042 asection *isec = lo->u.indirect.section;
14043 if (h_elf->root.u.def.value >= isec->output_offset
14044 && h_elf->root.u.def.value < (isec->output_offset
14045 + isec->size))
14046 {
14047 h_elf->root.u.def.value -= isec->output_offset;
14048 h_elf->root.u.def.section = isec;
14049 sec = isec;
14050 break;
14051 }
14052 }
14053 }
14054 }
14055 h = (struct ppc_link_hash_entry *) h_elf;
14056
14057 if (sec != NULL && discarded_section (sec))
14058 {
14059 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14060 input_bfd, input_section,
14061 contents + rel->r_offset);
14062 wrel->r_offset = rel->r_offset;
14063 wrel->r_info = 0;
14064 wrel->r_addend = 0;
14065
14066 /* For ld -r, remove relocations in debug sections against
14067 symbols defined in discarded sections. Not done for
14068 non-debug to preserve relocs in .eh_frame which the
14069 eh_frame editing code expects to be present. */
14070 if (bfd_link_relocatable (info)
14071 && (input_section->flags & SEC_DEBUGGING))
14072 wrel--;
14073
14074 continue;
14075 }
14076
14077 if (bfd_link_relocatable (info))
14078 goto copy_reloc;
14079
14080 if (h != NULL && &h->elf == htab->elf.hgot)
14081 {
14082 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14083 sec = bfd_abs_section_ptr;
14084 unresolved_reloc = FALSE;
14085 }
14086
14087 /* TLS optimizations. Replace instruction sequences and relocs
14088 based on information we collected in tls_optimize. We edit
14089 RELOCS so that --emit-relocs will output something sensible
14090 for the final instruction stream. */
14091 tls_mask = 0;
14092 tls_gd = 0;
14093 toc_symndx = 0;
14094 if (h != NULL)
14095 tls_mask = h->tls_mask;
14096 else if (local_got_ents != NULL)
14097 {
14098 struct plt_entry **local_plt = (struct plt_entry **)
14099 (local_got_ents + symtab_hdr->sh_info);
14100 unsigned char *lgot_masks = (unsigned char *)
14101 (local_plt + symtab_hdr->sh_info);
14102 tls_mask = lgot_masks[r_symndx];
14103 }
14104 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14105 && (r_type == R_PPC64_TLS
14106 || r_type == R_PPC64_TLSGD
14107 || r_type == R_PPC64_TLSLD))
14108 {
14109 /* Check for toc tls entries. */
14110 unsigned char *toc_tls;
14111
14112 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14113 &local_syms, rel, input_bfd))
14114 return FALSE;
14115
14116 if (toc_tls)
14117 tls_mask = *toc_tls;
14118 }
14119
14120 /* Check that tls relocs are used with tls syms, and non-tls
14121 relocs are used with non-tls syms. */
14122 if (r_symndx != STN_UNDEF
14123 && r_type != R_PPC64_NONE
14124 && (h == NULL
14125 || h->elf.root.type == bfd_link_hash_defined
14126 || h->elf.root.type == bfd_link_hash_defweak)
14127 && (IS_PPC64_TLS_RELOC (r_type)
14128 != (sym_type == STT_TLS
14129 || (sym_type == STT_SECTION
14130 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14131 {
14132 if ((tls_mask & TLS_TLS) != 0
14133 && (r_type == R_PPC64_TLS
14134 || r_type == R_PPC64_TLSGD
14135 || r_type == R_PPC64_TLSLD))
14136 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14137 ;
14138 else
14139 info->callbacks->einfo
14140 (!IS_PPC64_TLS_RELOC (r_type)
14141 /* xgettext:c-format */
14142 ? _("%H: %s used with TLS symbol `%pT'\n")
14143 /* xgettext:c-format */
14144 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14145 input_bfd, input_section, rel->r_offset,
14146 ppc64_elf_howto_table[r_type]->name,
14147 sym_name);
14148 }
14149
14150 /* Ensure reloc mapping code below stays sane. */
14151 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14152 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14153 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14154 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14155 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14156 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14157 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14158 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14159 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14160 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14161 abort ();
14162
14163 switch (r_type)
14164 {
14165 default:
14166 break;
14167
14168 case R_PPC64_LO_DS_OPT:
14169 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14170 if ((insn & (0x3f << 26)) != 58u << 26)
14171 abort ();
14172 insn += (14u << 26) - (58u << 26);
14173 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14174 r_type = R_PPC64_TOC16_LO;
14175 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14176 break;
14177
14178 case R_PPC64_TOC16:
14179 case R_PPC64_TOC16_LO:
14180 case R_PPC64_TOC16_DS:
14181 case R_PPC64_TOC16_LO_DS:
14182 {
14183 /* Check for toc tls entries. */
14184 unsigned char *toc_tls;
14185 int retval;
14186
14187 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14188 &local_syms, rel, input_bfd);
14189 if (retval == 0)
14190 return FALSE;
14191
14192 if (toc_tls)
14193 {
14194 tls_mask = *toc_tls;
14195 if (r_type == R_PPC64_TOC16_DS
14196 || r_type == R_PPC64_TOC16_LO_DS)
14197 {
14198 if ((tls_mask & TLS_TLS) != 0
14199 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14200 goto toctprel;
14201 }
14202 else
14203 {
14204 /* If we found a GD reloc pair, then we might be
14205 doing a GD->IE transition. */
14206 if (retval == 2)
14207 {
14208 tls_gd = TLS_TPRELGD;
14209 if ((tls_mask & TLS_TLS) != 0
14210 && (tls_mask & TLS_GD) == 0)
14211 goto tls_ldgd_opt;
14212 }
14213 else if (retval == 3)
14214 {
14215 if ((tls_mask & TLS_TLS) != 0
14216 && (tls_mask & TLS_LD) == 0)
14217 goto tls_ldgd_opt;
14218 }
14219 }
14220 }
14221 }
14222 break;
14223
14224 case R_PPC64_GOT_TPREL16_HI:
14225 case R_PPC64_GOT_TPREL16_HA:
14226 if ((tls_mask & TLS_TLS) != 0
14227 && (tls_mask & TLS_TPREL) == 0)
14228 {
14229 rel->r_offset -= d_offset;
14230 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14231 r_type = R_PPC64_NONE;
14232 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14233 }
14234 break;
14235
14236 case R_PPC64_GOT_TPREL16_DS:
14237 case R_PPC64_GOT_TPREL16_LO_DS:
14238 if ((tls_mask & TLS_TLS) != 0
14239 && (tls_mask & TLS_TPREL) == 0)
14240 {
14241 toctprel:
14242 insn = bfd_get_32 (input_bfd,
14243 contents + rel->r_offset - d_offset);
14244 insn &= 31 << 21;
14245 insn |= 0x3c0d0000; /* addis 0,13,0 */
14246 bfd_put_32 (input_bfd, insn,
14247 contents + rel->r_offset - d_offset);
14248 r_type = R_PPC64_TPREL16_HA;
14249 if (toc_symndx != 0)
14250 {
14251 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14252 rel->r_addend = toc_addend;
14253 /* We changed the symbol. Start over in order to
14254 get h, sym, sec etc. right. */
14255 goto again;
14256 }
14257 else
14258 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14259 }
14260 break;
14261
14262 case R_PPC64_TLS:
14263 if ((tls_mask & TLS_TLS) != 0
14264 && (tls_mask & TLS_TPREL) == 0)
14265 {
14266 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14267 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14268 if (insn == 0)
14269 abort ();
14270 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14271 /* Was PPC64_TLS which sits on insn boundary, now
14272 PPC64_TPREL16_LO which is at low-order half-word. */
14273 rel->r_offset += d_offset;
14274 r_type = R_PPC64_TPREL16_LO;
14275 if (toc_symndx != 0)
14276 {
14277 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14278 rel->r_addend = toc_addend;
14279 /* We changed the symbol. Start over in order to
14280 get h, sym, sec etc. right. */
14281 goto again;
14282 }
14283 else
14284 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14285 }
14286 break;
14287
14288 case R_PPC64_GOT_TLSGD16_HI:
14289 case R_PPC64_GOT_TLSGD16_HA:
14290 tls_gd = TLS_TPRELGD;
14291 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14292 goto tls_gdld_hi;
14293 break;
14294
14295 case R_PPC64_GOT_TLSLD16_HI:
14296 case R_PPC64_GOT_TLSLD16_HA:
14297 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14298 {
14299 tls_gdld_hi:
14300 if ((tls_mask & tls_gd) != 0)
14301 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14302 + R_PPC64_GOT_TPREL16_DS);
14303 else
14304 {
14305 rel->r_offset -= d_offset;
14306 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14307 r_type = R_PPC64_NONE;
14308 }
14309 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14310 }
14311 break;
14312
14313 case R_PPC64_GOT_TLSGD16:
14314 case R_PPC64_GOT_TLSGD16_LO:
14315 tls_gd = TLS_TPRELGD;
14316 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14317 goto tls_ldgd_opt;
14318 break;
14319
14320 case R_PPC64_GOT_TLSLD16:
14321 case R_PPC64_GOT_TLSLD16_LO:
14322 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14323 {
14324 unsigned int insn1, insn2;
14325 bfd_vma offset;
14326
14327 tls_ldgd_opt:
14328 offset = (bfd_vma) -1;
14329 /* If not using the newer R_PPC64_TLSGD/LD to mark
14330 __tls_get_addr calls, we must trust that the call
14331 stays with its arg setup insns, ie. that the next
14332 reloc is the __tls_get_addr call associated with
14333 the current reloc. Edit both insns. */
14334 if (input_section->has_tls_get_addr_call
14335 && rel + 1 < relend
14336 && branch_reloc_hash_match (input_bfd, rel + 1,
14337 htab->tls_get_addr,
14338 htab->tls_get_addr_fd))
14339 offset = rel[1].r_offset;
14340 /* We read the low GOT_TLS (or TOC16) insn because we
14341 need to keep the destination reg. It may be
14342 something other than the usual r3, and moved to r3
14343 before the call by intervening code. */
14344 insn1 = bfd_get_32 (input_bfd,
14345 contents + rel->r_offset - d_offset);
14346 if ((tls_mask & tls_gd) != 0)
14347 {
14348 /* IE */
14349 insn1 &= (0x1f << 21) | (0x1f << 16);
14350 insn1 |= 58 << 26; /* ld */
14351 insn2 = 0x7c636a14; /* add 3,3,13 */
14352 if (offset != (bfd_vma) -1)
14353 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14354 if ((tls_mask & TLS_EXPLICIT) == 0)
14355 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14356 + R_PPC64_GOT_TPREL16_DS);
14357 else
14358 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14359 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14360 }
14361 else
14362 {
14363 /* LE */
14364 insn1 &= 0x1f << 21;
14365 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14366 insn2 = 0x38630000; /* addi 3,3,0 */
14367 if (tls_gd == 0)
14368 {
14369 /* Was an LD reloc. */
14370 if (toc_symndx)
14371 sec = local_sections[toc_symndx];
14372 for (r_symndx = 0;
14373 r_symndx < symtab_hdr->sh_info;
14374 r_symndx++)
14375 if (local_sections[r_symndx] == sec)
14376 break;
14377 if (r_symndx >= symtab_hdr->sh_info)
14378 r_symndx = STN_UNDEF;
14379 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14380 if (r_symndx != STN_UNDEF)
14381 rel->r_addend -= (local_syms[r_symndx].st_value
14382 + sec->output_offset
14383 + sec->output_section->vma);
14384 }
14385 else if (toc_symndx != 0)
14386 {
14387 r_symndx = toc_symndx;
14388 rel->r_addend = toc_addend;
14389 }
14390 r_type = R_PPC64_TPREL16_HA;
14391 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14392 if (offset != (bfd_vma) -1)
14393 {
14394 rel[1].r_info = ELF64_R_INFO (r_symndx,
14395 R_PPC64_TPREL16_LO);
14396 rel[1].r_offset = offset + d_offset;
14397 rel[1].r_addend = rel->r_addend;
14398 }
14399 }
14400 bfd_put_32 (input_bfd, insn1,
14401 contents + rel->r_offset - d_offset);
14402 if (offset != (bfd_vma) -1)
14403 bfd_put_32 (input_bfd, insn2, contents + offset);
14404 if ((tls_mask & tls_gd) == 0
14405 && (tls_gd == 0 || toc_symndx != 0))
14406 {
14407 /* We changed the symbol. Start over in order
14408 to get h, sym, sec etc. right. */
14409 goto again;
14410 }
14411 }
14412 break;
14413
14414 case R_PPC64_TLSGD:
14415 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14416 && rel + 1 < relend)
14417 {
14418 unsigned int insn2;
14419 bfd_vma offset = rel->r_offset;
14420
14421 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14422 {
14423 bfd_put_32 (output_bfd, NOP, contents + offset);
14424 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14425 break;
14426 }
14427
14428 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14429 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14430
14431 if ((tls_mask & TLS_TPRELGD) != 0)
14432 {
14433 /* IE */
14434 r_type = R_PPC64_NONE;
14435 insn2 = 0x7c636a14; /* add 3,3,13 */
14436 }
14437 else
14438 {
14439 /* LE */
14440 if (toc_symndx != 0)
14441 {
14442 r_symndx = toc_symndx;
14443 rel->r_addend = toc_addend;
14444 }
14445 r_type = R_PPC64_TPREL16_LO;
14446 rel->r_offset = offset + d_offset;
14447 insn2 = 0x38630000; /* addi 3,3,0 */
14448 }
14449 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14450 /* Zap the reloc on the _tls_get_addr call too. */
14451 BFD_ASSERT (offset == rel[1].r_offset);
14452 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14453 bfd_put_32 (input_bfd, insn2, contents + offset);
14454 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14455 goto again;
14456 }
14457 break;
14458
14459 case R_PPC64_TLSLD:
14460 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14461 && rel + 1 < relend)
14462 {
14463 unsigned int insn2;
14464 bfd_vma offset = rel->r_offset;
14465
14466 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14467 {
14468 bfd_put_32 (output_bfd, NOP, contents + offset);
14469 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14470 break;
14471 }
14472
14473 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14474 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14475
14476 if (toc_symndx)
14477 sec = local_sections[toc_symndx];
14478 for (r_symndx = 0;
14479 r_symndx < symtab_hdr->sh_info;
14480 r_symndx++)
14481 if (local_sections[r_symndx] == sec)
14482 break;
14483 if (r_symndx >= symtab_hdr->sh_info)
14484 r_symndx = STN_UNDEF;
14485 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14486 if (r_symndx != STN_UNDEF)
14487 rel->r_addend -= (local_syms[r_symndx].st_value
14488 + sec->output_offset
14489 + sec->output_section->vma);
14490
14491 r_type = R_PPC64_TPREL16_LO;
14492 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14493 rel->r_offset = offset + d_offset;
14494 /* Zap the reloc on the _tls_get_addr call too. */
14495 BFD_ASSERT (offset == rel[1].r_offset);
14496 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14497 insn2 = 0x38630000; /* addi 3,3,0 */
14498 bfd_put_32 (input_bfd, insn2, contents + offset);
14499 goto again;
14500 }
14501 break;
14502
14503 case R_PPC64_DTPMOD64:
14504 if (rel + 1 < relend
14505 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14506 && rel[1].r_offset == rel->r_offset + 8)
14507 {
14508 if ((tls_mask & TLS_GD) == 0)
14509 {
14510 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14511 if ((tls_mask & TLS_TPRELGD) != 0)
14512 r_type = R_PPC64_TPREL64;
14513 else
14514 {
14515 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14516 r_type = R_PPC64_NONE;
14517 }
14518 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14519 }
14520 }
14521 else
14522 {
14523 if ((tls_mask & TLS_LD) == 0)
14524 {
14525 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14526 r_type = R_PPC64_NONE;
14527 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14528 }
14529 }
14530 break;
14531
14532 case R_PPC64_TPREL64:
14533 if ((tls_mask & TLS_TPREL) == 0)
14534 {
14535 r_type = R_PPC64_NONE;
14536 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14537 }
14538 break;
14539
14540 case R_PPC64_ENTRY:
14541 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14542 if (!bfd_link_pic (info)
14543 && !info->traditional_format
14544 && relocation + 0x80008000 <= 0xffffffff)
14545 {
14546 unsigned int insn1, insn2;
14547
14548 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14549 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14550 if ((insn1 & ~0xfffc) == LD_R2_0R12
14551 && insn2 == ADD_R2_R2_R12)
14552 {
14553 bfd_put_32 (input_bfd,
14554 LIS_R2 + PPC_HA (relocation),
14555 contents + rel->r_offset);
14556 bfd_put_32 (input_bfd,
14557 ADDI_R2_R2 + PPC_LO (relocation),
14558 contents + rel->r_offset + 4);
14559 }
14560 }
14561 else
14562 {
14563 relocation -= (rel->r_offset
14564 + input_section->output_offset
14565 + input_section->output_section->vma);
14566 if (relocation + 0x80008000 <= 0xffffffff)
14567 {
14568 unsigned int insn1, insn2;
14569
14570 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14571 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14572 if ((insn1 & ~0xfffc) == LD_R2_0R12
14573 && insn2 == ADD_R2_R2_R12)
14574 {
14575 bfd_put_32 (input_bfd,
14576 ADDIS_R2_R12 + PPC_HA (relocation),
14577 contents + rel->r_offset);
14578 bfd_put_32 (input_bfd,
14579 ADDI_R2_R2 + PPC_LO (relocation),
14580 contents + rel->r_offset + 4);
14581 }
14582 }
14583 }
14584 break;
14585
14586 case R_PPC64_REL16_HA:
14587 /* If we are generating a non-PIC executable, edit
14588 . 0: addis 2,12,.TOC.-0b@ha
14589 . addi 2,2,.TOC.-0b@l
14590 used by ELFv2 global entry points to set up r2, to
14591 . lis 2,.TOC.@ha
14592 . addi 2,2,.TOC.@l
14593 if .TOC. is in range. */
14594 if (!bfd_link_pic (info)
14595 && !info->traditional_format
14596 && !htab->opd_abi
14597 && rel->r_addend == d_offset
14598 && h != NULL && &h->elf == htab->elf.hgot
14599 && rel + 1 < relend
14600 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14601 && rel[1].r_offset == rel->r_offset + 4
14602 && rel[1].r_addend == rel->r_addend + 4
14603 && relocation + 0x80008000 <= 0xffffffff)
14604 {
14605 unsigned int insn1, insn2;
14606 bfd_vma offset = rel->r_offset - d_offset;
14607 insn1 = bfd_get_32 (input_bfd, contents + offset);
14608 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14609 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14610 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14611 {
14612 r_type = R_PPC64_ADDR16_HA;
14613 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14614 rel->r_addend -= d_offset;
14615 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14616 rel[1].r_addend -= d_offset + 4;
14617 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14618 }
14619 }
14620 break;
14621 }
14622
14623 /* Handle other relocations that tweak non-addend part of insn. */
14624 insn = 0;
14625 max_br_offset = 1 << 25;
14626 addend = rel->r_addend;
14627 reloc_dest = DEST_NORMAL;
14628 switch (r_type)
14629 {
14630 default:
14631 break;
14632
14633 case R_PPC64_TOCSAVE:
14634 if (relocation + addend == (rel->r_offset
14635 + input_section->output_offset
14636 + input_section->output_section->vma)
14637 && tocsave_find (htab, NO_INSERT,
14638 &local_syms, rel, input_bfd))
14639 {
14640 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14641 if (insn == NOP
14642 || insn == CROR_151515 || insn == CROR_313131)
14643 bfd_put_32 (input_bfd,
14644 STD_R2_0R1 + STK_TOC (htab),
14645 contents + rel->r_offset);
14646 }
14647 break;
14648
14649 /* Branch taken prediction relocations. */
14650 case R_PPC64_ADDR14_BRTAKEN:
14651 case R_PPC64_REL14_BRTAKEN:
14652 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14653 /* Fall through. */
14654
14655 /* Branch not taken prediction relocations. */
14656 case R_PPC64_ADDR14_BRNTAKEN:
14657 case R_PPC64_REL14_BRNTAKEN:
14658 insn |= bfd_get_32 (input_bfd,
14659 contents + rel->r_offset) & ~(0x01 << 21);
14660 /* Fall through. */
14661
14662 case R_PPC64_REL14:
14663 max_br_offset = 1 << 15;
14664 /* Fall through. */
14665
14666 case R_PPC64_REL24:
14667 case R_PPC64_PLTCALL:
14668 /* Calls to functions with a different TOC, such as calls to
14669 shared objects, need to alter the TOC pointer. This is
14670 done using a linkage stub. A REL24 branching to these
14671 linkage stubs needs to be followed by a nop, as the nop
14672 will be replaced with an instruction to restore the TOC
14673 base pointer. */
14674 fdh = h;
14675 if (h != NULL
14676 && h->oh != NULL
14677 && h->oh->is_func_descriptor)
14678 fdh = ppc_follow_link (h->oh);
14679 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14680 htab);
14681 if (r_type == R_PPC64_PLTCALL
14682 && stub_entry != NULL
14683 && (stub_entry->stub_type == ppc_stub_plt_call
14684 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14685 stub_entry = NULL;
14686
14687 if (stub_entry != NULL
14688 && (stub_entry->stub_type == ppc_stub_plt_call
14689 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14690 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14691 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14692 {
14693 bfd_boolean can_plt_call = FALSE;
14694
14695 if (stub_entry->stub_type == ppc_stub_plt_call
14696 && !htab->opd_abi
14697 && htab->params->plt_localentry0 != 0
14698 && is_elfv2_localentry0 (&h->elf))
14699 {
14700 /* The function doesn't use or change r2. */
14701 can_plt_call = TRUE;
14702 }
14703
14704 /* All of these stubs may modify r2, so there must be a
14705 branch and link followed by a nop. The nop is
14706 replaced by an insn to restore r2. */
14707 else if (rel->r_offset + 8 <= input_section->size)
14708 {
14709 unsigned long br;
14710
14711 br = bfd_get_32 (input_bfd,
14712 contents + rel->r_offset);
14713 if ((br & 1) != 0)
14714 {
14715 unsigned long nop;
14716
14717 nop = bfd_get_32 (input_bfd,
14718 contents + rel->r_offset + 4);
14719 if (nop == LD_R2_0R1 + STK_TOC (htab))
14720 can_plt_call = TRUE;
14721 else if (nop == NOP
14722 || nop == CROR_151515
14723 || nop == CROR_313131)
14724 {
14725 if (h != NULL
14726 && (h == htab->tls_get_addr_fd
14727 || h == htab->tls_get_addr)
14728 && htab->params->tls_get_addr_opt)
14729 {
14730 /* Special stub used, leave nop alone. */
14731 }
14732 else
14733 bfd_put_32 (input_bfd,
14734 LD_R2_0R1 + STK_TOC (htab),
14735 contents + rel->r_offset + 4);
14736 can_plt_call = TRUE;
14737 }
14738 }
14739 }
14740
14741 if (!can_plt_call && h != NULL)
14742 {
14743 const char *name = h->elf.root.root.string;
14744
14745 if (*name == '.')
14746 ++name;
14747
14748 if (strncmp (name, "__libc_start_main", 17) == 0
14749 && (name[17] == 0 || name[17] == '@'))
14750 {
14751 /* Allow crt1 branch to go via a toc adjusting
14752 stub. Other calls that never return could do
14753 the same, if we could detect such. */
14754 can_plt_call = TRUE;
14755 }
14756 }
14757
14758 if (!can_plt_call)
14759 {
14760 /* g++ as of 20130507 emits self-calls without a
14761 following nop. This is arguably wrong since we
14762 have conflicting information. On the one hand a
14763 global symbol and on the other a local call
14764 sequence, but don't error for this special case.
14765 It isn't possible to cheaply verify we have
14766 exactly such a call. Allow all calls to the same
14767 section. */
14768 asection *code_sec = sec;
14769
14770 if (get_opd_info (sec) != NULL)
14771 {
14772 bfd_vma off = (relocation + addend
14773 - sec->output_section->vma
14774 - sec->output_offset);
14775
14776 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14777 }
14778 if (code_sec == input_section)
14779 can_plt_call = TRUE;
14780 }
14781
14782 if (!can_plt_call)
14783 {
14784 if (stub_entry->stub_type == ppc_stub_plt_call
14785 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14786 info->callbacks->einfo
14787 /* xgettext:c-format */
14788 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14789 "recompile with -fPIC\n"),
14790 input_bfd, input_section, rel->r_offset, sym_name);
14791 else
14792 info->callbacks->einfo
14793 /* xgettext:c-format */
14794 (_("%H: call to `%pT' lacks nop, can't restore toc; "
14795 "(-mcmodel=small toc adjust stub)\n"),
14796 input_bfd, input_section, rel->r_offset, sym_name);
14797
14798 bfd_set_error (bfd_error_bad_value);
14799 ret = FALSE;
14800 }
14801
14802 if (can_plt_call
14803 && (stub_entry->stub_type == ppc_stub_plt_call
14804 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14805 unresolved_reloc = FALSE;
14806 }
14807
14808 if ((stub_entry == NULL
14809 || stub_entry->stub_type == ppc_stub_long_branch
14810 || stub_entry->stub_type == ppc_stub_plt_branch)
14811 && get_opd_info (sec) != NULL)
14812 {
14813 /* The branch destination is the value of the opd entry. */
14814 bfd_vma off = (relocation + addend
14815 - sec->output_section->vma
14816 - sec->output_offset);
14817 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14818 if (dest != (bfd_vma) -1)
14819 {
14820 relocation = dest;
14821 addend = 0;
14822 reloc_dest = DEST_OPD;
14823 }
14824 }
14825
14826 /* If the branch is out of reach we ought to have a long
14827 branch stub. */
14828 from = (rel->r_offset
14829 + input_section->output_offset
14830 + input_section->output_section->vma);
14831
14832 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14833 ? fdh->elf.other
14834 : sym->st_other);
14835
14836 if (stub_entry != NULL
14837 && (stub_entry->stub_type == ppc_stub_long_branch
14838 || stub_entry->stub_type == ppc_stub_plt_branch)
14839 && (r_type == R_PPC64_ADDR14_BRTAKEN
14840 || r_type == R_PPC64_ADDR14_BRNTAKEN
14841 || (relocation + addend - from + max_br_offset
14842 < 2 * max_br_offset)))
14843 /* Don't use the stub if this branch is in range. */
14844 stub_entry = NULL;
14845
14846 if (stub_entry != NULL)
14847 {
14848 /* Munge up the value and addend so that we call the stub
14849 rather than the procedure directly. */
14850 asection *stub_sec = stub_entry->group->stub_sec;
14851
14852 if (stub_entry->stub_type == ppc_stub_save_res)
14853 relocation += (stub_sec->output_offset
14854 + stub_sec->output_section->vma
14855 + stub_sec->size - htab->sfpr->size
14856 - htab->sfpr->output_offset
14857 - htab->sfpr->output_section->vma);
14858 else
14859 relocation = (stub_entry->stub_offset
14860 + stub_sec->output_offset
14861 + stub_sec->output_section->vma);
14862 addend = 0;
14863 reloc_dest = DEST_STUB;
14864
14865 if ((stub_entry->stub_type == ppc_stub_plt_call
14866 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14867 && (ALWAYS_EMIT_R2SAVE
14868 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14869 && rel + 1 < relend
14870 && rel[1].r_offset == rel->r_offset + 4
14871 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14872 relocation += 4;
14873 }
14874
14875 if (insn != 0)
14876 {
14877 if (is_isa_v2)
14878 {
14879 /* Set 'a' bit. This is 0b00010 in BO field for branch
14880 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14881 for branch on CTR insns (BO == 1a00t or 1a01t). */
14882 if ((insn & (0x14 << 21)) == (0x04 << 21))
14883 insn |= 0x02 << 21;
14884 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14885 insn |= 0x08 << 21;
14886 else
14887 break;
14888 }
14889 else
14890 {
14891 /* Invert 'y' bit if not the default. */
14892 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14893 insn ^= 0x01 << 21;
14894 }
14895
14896 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14897 }
14898
14899 /* NOP out calls to undefined weak functions.
14900 We can thus call a weak function without first
14901 checking whether the function is defined. */
14902 else if (h != NULL
14903 && h->elf.root.type == bfd_link_hash_undefweak
14904 && h->elf.dynindx == -1
14905 && r_type == R_PPC64_REL24
14906 && relocation == 0
14907 && addend == 0)
14908 {
14909 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14910 goto copy_reloc;
14911 }
14912 break;
14913 }
14914
14915 /* Set `addend'. */
14916 tls_type = 0;
14917 save_unresolved_reloc = unresolved_reloc;
14918 switch (r_type)
14919 {
14920 default:
14921 /* xgettext:c-format */
14922 _bfd_error_handler (_("%pB: %s unsupported"),
14923 input_bfd, ppc64_elf_howto_table[r_type]->name);
14924
14925 bfd_set_error (bfd_error_bad_value);
14926 ret = FALSE;
14927 goto copy_reloc;
14928
14929 case R_PPC64_NONE:
14930 case R_PPC64_TLS:
14931 case R_PPC64_TLSGD:
14932 case R_PPC64_TLSLD:
14933 case R_PPC64_TOCSAVE:
14934 case R_PPC64_GNU_VTINHERIT:
14935 case R_PPC64_GNU_VTENTRY:
14936 case R_PPC64_ENTRY:
14937 goto copy_reloc;
14938
14939 /* GOT16 relocations. Like an ADDR16 using the symbol's
14940 address in the GOT as relocation value instead of the
14941 symbol's value itself. Also, create a GOT entry for the
14942 symbol and put the symbol value there. */
14943 case R_PPC64_GOT_TLSGD16:
14944 case R_PPC64_GOT_TLSGD16_LO:
14945 case R_PPC64_GOT_TLSGD16_HI:
14946 case R_PPC64_GOT_TLSGD16_HA:
14947 tls_type = TLS_TLS | TLS_GD;
14948 goto dogot;
14949
14950 case R_PPC64_GOT_TLSLD16:
14951 case R_PPC64_GOT_TLSLD16_LO:
14952 case R_PPC64_GOT_TLSLD16_HI:
14953 case R_PPC64_GOT_TLSLD16_HA:
14954 tls_type = TLS_TLS | TLS_LD;
14955 goto dogot;
14956
14957 case R_PPC64_GOT_TPREL16_DS:
14958 case R_PPC64_GOT_TPREL16_LO_DS:
14959 case R_PPC64_GOT_TPREL16_HI:
14960 case R_PPC64_GOT_TPREL16_HA:
14961 tls_type = TLS_TLS | TLS_TPREL;
14962 goto dogot;
14963
14964 case R_PPC64_GOT_DTPREL16_DS:
14965 case R_PPC64_GOT_DTPREL16_LO_DS:
14966 case R_PPC64_GOT_DTPREL16_HI:
14967 case R_PPC64_GOT_DTPREL16_HA:
14968 tls_type = TLS_TLS | TLS_DTPREL;
14969 goto dogot;
14970
14971 case R_PPC64_GOT16:
14972 case R_PPC64_GOT16_LO:
14973 case R_PPC64_GOT16_HI:
14974 case R_PPC64_GOT16_HA:
14975 case R_PPC64_GOT16_DS:
14976 case R_PPC64_GOT16_LO_DS:
14977 dogot:
14978 {
14979 /* Relocation is to the entry for this symbol in the global
14980 offset table. */
14981 asection *got;
14982 bfd_vma *offp;
14983 bfd_vma off;
14984 unsigned long indx = 0;
14985 struct got_entry *ent;
14986
14987 if (tls_type == (TLS_TLS | TLS_LD)
14988 && (h == NULL
14989 || !h->elf.def_dynamic))
14990 ent = ppc64_tlsld_got (input_bfd);
14991 else
14992 {
14993 if (h != NULL)
14994 {
14995 if (!htab->elf.dynamic_sections_created
14996 || h->elf.dynindx == -1
14997 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14998 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14999 /* This is actually a static link, or it is a
15000 -Bsymbolic link and the symbol is defined
15001 locally, or the symbol was forced to be local
15002 because of a version file. */
15003 ;
15004 else
15005 {
15006 indx = h->elf.dynindx;
15007 unresolved_reloc = FALSE;
15008 }
15009 ent = h->elf.got.glist;
15010 }
15011 else
15012 {
15013 if (local_got_ents == NULL)
15014 abort ();
15015 ent = local_got_ents[r_symndx];
15016 }
15017
15018 for (; ent != NULL; ent = ent->next)
15019 if (ent->addend == orig_rel.r_addend
15020 && ent->owner == input_bfd
15021 && ent->tls_type == tls_type)
15022 break;
15023 }
15024
15025 if (ent == NULL)
15026 abort ();
15027 if (ent->is_indirect)
15028 ent = ent->got.ent;
15029 offp = &ent->got.offset;
15030 got = ppc64_elf_tdata (ent->owner)->got;
15031 if (got == NULL)
15032 abort ();
15033
15034 /* The offset must always be a multiple of 8. We use the
15035 least significant bit to record whether we have already
15036 processed this entry. */
15037 off = *offp;
15038 if ((off & 1) != 0)
15039 off &= ~1;
15040 else
15041 {
15042 /* Generate relocs for the dynamic linker, except in
15043 the case of TLSLD where we'll use one entry per
15044 module. */
15045 asection *relgot;
15046 bfd_boolean ifunc;
15047
15048 *offp = off | 1;
15049 relgot = NULL;
15050 ifunc = (h != NULL
15051 ? h->elf.type == STT_GNU_IFUNC
15052 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15053 if (ifunc)
15054 {
15055 relgot = htab->elf.irelplt;
15056 if (indx == 0)
15057 htab->local_ifunc_resolver = 1;
15058 else if (is_static_defined (&h->elf))
15059 htab->maybe_local_ifunc_resolver = 1;
15060 }
15061 else if (indx != 0
15062 || (bfd_link_pic (info)
15063 && (h == NULL
15064 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15065 || (tls_type == (TLS_TLS | TLS_LD)
15066 && !h->elf.def_dynamic))
15067 && !(tls_type == (TLS_TLS | TLS_TPREL)
15068 && bfd_link_executable (info)
15069 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15070 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15071 if (relgot != NULL)
15072 {
15073 outrel.r_offset = (got->output_section->vma
15074 + got->output_offset
15075 + off);
15076 outrel.r_addend = addend;
15077 if (tls_type & (TLS_LD | TLS_GD))
15078 {
15079 outrel.r_addend = 0;
15080 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15081 if (tls_type == (TLS_TLS | TLS_GD))
15082 {
15083 loc = relgot->contents;
15084 loc += (relgot->reloc_count++
15085 * sizeof (Elf64_External_Rela));
15086 bfd_elf64_swap_reloca_out (output_bfd,
15087 &outrel, loc);
15088 outrel.r_offset += 8;
15089 outrel.r_addend = addend;
15090 outrel.r_info
15091 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15092 }
15093 }
15094 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15095 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15096 else if (tls_type == (TLS_TLS | TLS_TPREL))
15097 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15098 else if (indx != 0)
15099 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15100 else
15101 {
15102 if (ifunc)
15103 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15104 else
15105 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15106
15107 /* Write the .got section contents for the sake
15108 of prelink. */
15109 loc = got->contents + off;
15110 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15111 loc);
15112 }
15113
15114 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15115 {
15116 outrel.r_addend += relocation;
15117 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15118 {
15119 if (htab->elf.tls_sec == NULL)
15120 outrel.r_addend = 0;
15121 else
15122 outrel.r_addend -= htab->elf.tls_sec->vma;
15123 }
15124 }
15125 loc = relgot->contents;
15126 loc += (relgot->reloc_count++
15127 * sizeof (Elf64_External_Rela));
15128 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15129 }
15130
15131 /* Init the .got section contents here if we're not
15132 emitting a reloc. */
15133 else
15134 {
15135 relocation += addend;
15136 if (tls_type != 0)
15137 {
15138 if (htab->elf.tls_sec == NULL)
15139 relocation = 0;
15140 else
15141 {
15142 if (tls_type & TLS_LD)
15143 relocation = 0;
15144 else
15145 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15146 if (tls_type & TLS_TPREL)
15147 relocation += DTP_OFFSET - TP_OFFSET;
15148 }
15149
15150 if (tls_type & (TLS_GD | TLS_LD))
15151 {
15152 bfd_put_64 (output_bfd, relocation,
15153 got->contents + off + 8);
15154 relocation = 1;
15155 }
15156 }
15157 bfd_put_64 (output_bfd, relocation,
15158 got->contents + off);
15159 }
15160 }
15161
15162 if (off >= (bfd_vma) -2)
15163 abort ();
15164
15165 relocation = got->output_section->vma + got->output_offset + off;
15166 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15167 }
15168 break;
15169
15170 case R_PPC64_PLT16_HA:
15171 case R_PPC64_PLT16_HI:
15172 case R_PPC64_PLT16_LO:
15173 case R_PPC64_PLT16_LO_DS:
15174 case R_PPC64_PLT32:
15175 case R_PPC64_PLT64:
15176 case R_PPC64_PLTSEQ:
15177 case R_PPC64_PLTCALL:
15178 /* Relocation is to the entry for this symbol in the
15179 procedure linkage table. */
15180 unresolved_reloc = TRUE;
15181 {
15182 struct plt_entry **plt_list = NULL;
15183 if (h != NULL)
15184 plt_list = &h->elf.plt.plist;
15185 else if (local_got_ents != NULL)
15186 {
15187 struct plt_entry **local_plt = (struct plt_entry **)
15188 (local_got_ents + symtab_hdr->sh_info);
15189 plt_list = local_plt + r_symndx;
15190 }
15191 if (plt_list)
15192 {
15193 struct plt_entry *ent;
15194
15195 for (ent = *plt_list; ent != NULL; ent = ent->next)
15196 if (ent->plt.offset != (bfd_vma) -1
15197 && ent->addend == orig_rel.r_addend)
15198 {
15199 asection *plt;
15200 bfd_vma got;
15201
15202 plt = htab->elf.splt;
15203 if (!htab->elf.dynamic_sections_created
15204 || h == NULL
15205 || h->elf.dynindx == -1)
15206 {
15207 if (h != NULL
15208 ? h->elf.type == STT_GNU_IFUNC
15209 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15210 plt = htab->elf.iplt;
15211 else
15212 plt = htab->pltlocal;
15213 }
15214 relocation = (plt->output_section->vma
15215 + plt->output_offset
15216 + ent->plt.offset);
15217 if (r_type == R_PPC64_PLT16_HA
15218 || r_type ==R_PPC64_PLT16_HI
15219 || r_type ==R_PPC64_PLT16_LO
15220 || r_type ==R_PPC64_PLT16_LO_DS)
15221 {
15222 got = (elf_gp (output_bfd)
15223 + htab->sec_info[input_section->id].toc_off);
15224 relocation -= got;
15225 }
15226 addend = 0;
15227 unresolved_reloc = FALSE;
15228 break;
15229 }
15230 }
15231 }
15232 break;
15233
15234 case R_PPC64_TOC:
15235 /* Relocation value is TOC base. */
15236 relocation = TOCstart;
15237 if (r_symndx == STN_UNDEF)
15238 relocation += htab->sec_info[input_section->id].toc_off;
15239 else if (unresolved_reloc)
15240 ;
15241 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15242 relocation += htab->sec_info[sec->id].toc_off;
15243 else
15244 unresolved_reloc = TRUE;
15245 goto dodyn;
15246
15247 /* TOC16 relocs. We want the offset relative to the TOC base,
15248 which is the address of the start of the TOC plus 0x8000.
15249 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15250 in this order. */
15251 case R_PPC64_TOC16:
15252 case R_PPC64_TOC16_LO:
15253 case R_PPC64_TOC16_HI:
15254 case R_PPC64_TOC16_DS:
15255 case R_PPC64_TOC16_LO_DS:
15256 case R_PPC64_TOC16_HA:
15257 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15258 break;
15259
15260 /* Relocate against the beginning of the section. */
15261 case R_PPC64_SECTOFF:
15262 case R_PPC64_SECTOFF_LO:
15263 case R_PPC64_SECTOFF_HI:
15264 case R_PPC64_SECTOFF_DS:
15265 case R_PPC64_SECTOFF_LO_DS:
15266 case R_PPC64_SECTOFF_HA:
15267 if (sec != NULL)
15268 addend -= sec->output_section->vma;
15269 break;
15270
15271 case R_PPC64_REL16:
15272 case R_PPC64_REL16_LO:
15273 case R_PPC64_REL16_HI:
15274 case R_PPC64_REL16_HA:
15275 case R_PPC64_REL16DX_HA:
15276 break;
15277
15278 case R_PPC64_REL14:
15279 case R_PPC64_REL14_BRNTAKEN:
15280 case R_PPC64_REL14_BRTAKEN:
15281 case R_PPC64_REL24:
15282 break;
15283
15284 case R_PPC64_TPREL16:
15285 case R_PPC64_TPREL16_LO:
15286 case R_PPC64_TPREL16_HI:
15287 case R_PPC64_TPREL16_HA:
15288 case R_PPC64_TPREL16_DS:
15289 case R_PPC64_TPREL16_LO_DS:
15290 case R_PPC64_TPREL16_HIGH:
15291 case R_PPC64_TPREL16_HIGHA:
15292 case R_PPC64_TPREL16_HIGHER:
15293 case R_PPC64_TPREL16_HIGHERA:
15294 case R_PPC64_TPREL16_HIGHEST:
15295 case R_PPC64_TPREL16_HIGHESTA:
15296 if (h != NULL
15297 && h->elf.root.type == bfd_link_hash_undefweak
15298 && h->elf.dynindx == -1)
15299 {
15300 /* Make this relocation against an undefined weak symbol
15301 resolve to zero. This is really just a tweak, since
15302 code using weak externs ought to check that they are
15303 defined before using them. */
15304 bfd_byte *p = contents + rel->r_offset - d_offset;
15305
15306 insn = bfd_get_32 (input_bfd, p);
15307 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15308 if (insn != 0)
15309 bfd_put_32 (input_bfd, insn, p);
15310 break;
15311 }
15312 if (htab->elf.tls_sec != NULL)
15313 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15314 /* The TPREL16 relocs shouldn't really be used in shared
15315 libs or with non-local symbols as that will result in
15316 DT_TEXTREL being set, but support them anyway. */
15317 goto dodyn;
15318
15319 case R_PPC64_DTPREL16:
15320 case R_PPC64_DTPREL16_LO:
15321 case R_PPC64_DTPREL16_HI:
15322 case R_PPC64_DTPREL16_HA:
15323 case R_PPC64_DTPREL16_DS:
15324 case R_PPC64_DTPREL16_LO_DS:
15325 case R_PPC64_DTPREL16_HIGH:
15326 case R_PPC64_DTPREL16_HIGHA:
15327 case R_PPC64_DTPREL16_HIGHER:
15328 case R_PPC64_DTPREL16_HIGHERA:
15329 case R_PPC64_DTPREL16_HIGHEST:
15330 case R_PPC64_DTPREL16_HIGHESTA:
15331 if (htab->elf.tls_sec != NULL)
15332 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15333 break;
15334
15335 case R_PPC64_ADDR64_LOCAL:
15336 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15337 ? h->elf.other
15338 : sym->st_other);
15339 break;
15340
15341 case R_PPC64_DTPMOD64:
15342 relocation = 1;
15343 addend = 0;
15344 goto dodyn;
15345
15346 case R_PPC64_TPREL64:
15347 if (htab->elf.tls_sec != NULL)
15348 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15349 goto dodyn;
15350
15351 case R_PPC64_DTPREL64:
15352 if (htab->elf.tls_sec != NULL)
15353 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15354 /* Fall through. */
15355
15356 /* Relocations that may need to be propagated if this is a
15357 dynamic object. */
15358 case R_PPC64_REL30:
15359 case R_PPC64_REL32:
15360 case R_PPC64_REL64:
15361 case R_PPC64_ADDR14:
15362 case R_PPC64_ADDR14_BRNTAKEN:
15363 case R_PPC64_ADDR14_BRTAKEN:
15364 case R_PPC64_ADDR16:
15365 case R_PPC64_ADDR16_DS:
15366 case R_PPC64_ADDR16_HA:
15367 case R_PPC64_ADDR16_HI:
15368 case R_PPC64_ADDR16_HIGH:
15369 case R_PPC64_ADDR16_HIGHA:
15370 case R_PPC64_ADDR16_HIGHER:
15371 case R_PPC64_ADDR16_HIGHERA:
15372 case R_PPC64_ADDR16_HIGHEST:
15373 case R_PPC64_ADDR16_HIGHESTA:
15374 case R_PPC64_ADDR16_LO:
15375 case R_PPC64_ADDR16_LO_DS:
15376 case R_PPC64_ADDR24:
15377 case R_PPC64_ADDR32:
15378 case R_PPC64_ADDR64:
15379 case R_PPC64_UADDR16:
15380 case R_PPC64_UADDR32:
15381 case R_PPC64_UADDR64:
15382 dodyn:
15383 if ((input_section->flags & SEC_ALLOC) == 0)
15384 break;
15385
15386 if (NO_OPD_RELOCS && is_opd)
15387 break;
15388
15389 if (bfd_link_pic (info)
15390 ? ((h == NULL
15391 || h->dyn_relocs != NULL)
15392 && ((h != NULL && pc_dynrelocs (h))
15393 || must_be_dyn_reloc (info, r_type)))
15394 : (h != NULL
15395 ? h->dyn_relocs != NULL
15396 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15397 {
15398 bfd_boolean skip, relocate;
15399 asection *sreloc;
15400 bfd_vma out_off;
15401 long indx = 0;
15402
15403 /* When generating a dynamic object, these relocations
15404 are copied into the output file to be resolved at run
15405 time. */
15406
15407 skip = FALSE;
15408 relocate = FALSE;
15409
15410 out_off = _bfd_elf_section_offset (output_bfd, info,
15411 input_section, rel->r_offset);
15412 if (out_off == (bfd_vma) -1)
15413 skip = TRUE;
15414 else if (out_off == (bfd_vma) -2)
15415 skip = TRUE, relocate = TRUE;
15416 out_off += (input_section->output_section->vma
15417 + input_section->output_offset);
15418 outrel.r_offset = out_off;
15419 outrel.r_addend = rel->r_addend;
15420
15421 /* Optimize unaligned reloc use. */
15422 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15423 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15424 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15425 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15426 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15427 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15428 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15429 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15430 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15431
15432 if (skip)
15433 memset (&outrel, 0, sizeof outrel);
15434 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15435 && !is_opd
15436 && r_type != R_PPC64_TOC)
15437 {
15438 indx = h->elf.dynindx;
15439 BFD_ASSERT (indx != -1);
15440 outrel.r_info = ELF64_R_INFO (indx, r_type);
15441 }
15442 else
15443 {
15444 /* This symbol is local, or marked to become local,
15445 or this is an opd section reloc which must point
15446 at a local function. */
15447 outrel.r_addend += relocation;
15448 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15449 {
15450 if (is_opd && h != NULL)
15451 {
15452 /* Lie about opd entries. This case occurs
15453 when building shared libraries and we
15454 reference a function in another shared
15455 lib. The same thing happens for a weak
15456 definition in an application that's
15457 overridden by a strong definition in a
15458 shared lib. (I believe this is a generic
15459 bug in binutils handling of weak syms.)
15460 In these cases we won't use the opd
15461 entry in this lib. */
15462 unresolved_reloc = FALSE;
15463 }
15464 if (!is_opd
15465 && r_type == R_PPC64_ADDR64
15466 && (h != NULL
15467 ? h->elf.type == STT_GNU_IFUNC
15468 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15469 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15470 else
15471 {
15472 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15473
15474 /* We need to relocate .opd contents for ld.so.
15475 Prelink also wants simple and consistent rules
15476 for relocs. This make all RELATIVE relocs have
15477 *r_offset equal to r_addend. */
15478 relocate = TRUE;
15479 }
15480 }
15481 else
15482 {
15483 if (h != NULL
15484 ? h->elf.type == STT_GNU_IFUNC
15485 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15486 {
15487 info->callbacks->einfo
15488 /* xgettext:c-format */
15489 (_("%H: %s for indirect "
15490 "function `%pT' unsupported\n"),
15491 input_bfd, input_section, rel->r_offset,
15492 ppc64_elf_howto_table[r_type]->name,
15493 sym_name);
15494 ret = FALSE;
15495 }
15496 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15497 ;
15498 else if (sec == NULL || sec->owner == NULL)
15499 {
15500 bfd_set_error (bfd_error_bad_value);
15501 return FALSE;
15502 }
15503 else
15504 {
15505 asection *osec;
15506
15507 osec = sec->output_section;
15508 indx = elf_section_data (osec)->dynindx;
15509
15510 if (indx == 0)
15511 {
15512 if ((osec->flags & SEC_READONLY) == 0
15513 && htab->elf.data_index_section != NULL)
15514 osec = htab->elf.data_index_section;
15515 else
15516 osec = htab->elf.text_index_section;
15517 indx = elf_section_data (osec)->dynindx;
15518 }
15519 BFD_ASSERT (indx != 0);
15520
15521 /* We are turning this relocation into one
15522 against a section symbol, so subtract out
15523 the output section's address but not the
15524 offset of the input section in the output
15525 section. */
15526 outrel.r_addend -= osec->vma;
15527 }
15528
15529 outrel.r_info = ELF64_R_INFO (indx, r_type);
15530 }
15531 }
15532
15533 sreloc = elf_section_data (input_section)->sreloc;
15534 if (h != NULL
15535 ? h->elf.type == STT_GNU_IFUNC
15536 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15537 {
15538 sreloc = htab->elf.irelplt;
15539 if (indx == 0)
15540 htab->local_ifunc_resolver = 1;
15541 else if (is_static_defined (&h->elf))
15542 htab->maybe_local_ifunc_resolver = 1;
15543 }
15544 if (sreloc == NULL)
15545 abort ();
15546
15547 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
15548 >= sreloc->size)
15549 abort ();
15550 loc = sreloc->contents;
15551 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
15552 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15553
15554 /* If this reloc is against an external symbol, it will
15555 be computed at runtime, so there's no need to do
15556 anything now. However, for the sake of prelink ensure
15557 that the section contents are a known value. */
15558 if (! relocate)
15559 {
15560 unresolved_reloc = FALSE;
15561 /* The value chosen here is quite arbitrary as ld.so
15562 ignores section contents except for the special
15563 case of .opd where the contents might be accessed
15564 before relocation. Choose zero, as that won't
15565 cause reloc overflow. */
15566 relocation = 0;
15567 addend = 0;
15568 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
15569 to improve backward compatibility with older
15570 versions of ld. */
15571 if (r_type == R_PPC64_ADDR64)
15572 addend = outrel.r_addend;
15573 /* Adjust pc_relative relocs to have zero in *r_offset. */
15574 else if (ppc64_elf_howto_table[r_type]->pc_relative)
15575 addend = outrel.r_offset;
15576 }
15577 }
15578 break;
15579
15580 case R_PPC64_COPY:
15581 case R_PPC64_GLOB_DAT:
15582 case R_PPC64_JMP_SLOT:
15583 case R_PPC64_JMP_IREL:
15584 case R_PPC64_RELATIVE:
15585 /* We shouldn't ever see these dynamic relocs in relocatable
15586 files. */
15587 /* Fall through. */
15588
15589 case R_PPC64_PLTGOT16:
15590 case R_PPC64_PLTGOT16_DS:
15591 case R_PPC64_PLTGOT16_HA:
15592 case R_PPC64_PLTGOT16_HI:
15593 case R_PPC64_PLTGOT16_LO:
15594 case R_PPC64_PLTGOT16_LO_DS:
15595 case R_PPC64_PLTREL32:
15596 case R_PPC64_PLTREL64:
15597 /* These ones haven't been implemented yet. */
15598
15599 info->callbacks->einfo
15600 /* xgettext:c-format */
15601 (_("%P: %pB: %s is not supported for `%pT'\n"),
15602 input_bfd,
15603 ppc64_elf_howto_table[r_type]->name, sym_name);
15604
15605 bfd_set_error (bfd_error_invalid_operation);
15606 ret = FALSE;
15607 goto copy_reloc;
15608 }
15609
15610 /* Multi-instruction sequences that access the TOC can be
15611 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15612 to nop; addi rb,r2,x; */
15613 switch (r_type)
15614 {
15615 default:
15616 break;
15617
15618 case R_PPC64_GOT_TLSLD16_HI:
15619 case R_PPC64_GOT_TLSGD16_HI:
15620 case R_PPC64_GOT_TPREL16_HI:
15621 case R_PPC64_GOT_DTPREL16_HI:
15622 case R_PPC64_GOT16_HI:
15623 case R_PPC64_TOC16_HI:
15624 /* These relocs would only be useful if building up an
15625 offset to later add to r2, perhaps in an indexed
15626 addressing mode instruction. Don't try to optimize.
15627 Unfortunately, the possibility of someone building up an
15628 offset like this or even with the HA relocs, means that
15629 we need to check the high insn when optimizing the low
15630 insn. */
15631 break;
15632
15633 case R_PPC64_PLTCALL:
15634 if (unresolved_reloc)
15635 {
15636 /* No plt entry. Make this into a direct call. */
15637 bfd_byte *p = contents + rel->r_offset;
15638 insn = bfd_get_32 (input_bfd, p);
15639 insn &= 1;
15640 bfd_put_32 (input_bfd, B_DOT | insn, p);
15641 bfd_put_32 (input_bfd, NOP, p + 4);
15642 unresolved_reloc = save_unresolved_reloc;
15643 r_type = R_PPC64_REL24;
15644 }
15645 break;
15646
15647 case R_PPC64_PLTSEQ:
15648 if (unresolved_reloc)
15649 {
15650 unresolved_reloc = FALSE;
15651 goto nop_it;
15652 }
15653 break;
15654
15655 case R_PPC64_PLT16_HA:
15656 if (unresolved_reloc)
15657 {
15658 unresolved_reloc = FALSE;
15659 goto nop_it;
15660 }
15661 /* Fall through. */
15662 case R_PPC64_GOT_TLSLD16_HA:
15663 case R_PPC64_GOT_TLSGD16_HA:
15664 case R_PPC64_GOT_TPREL16_HA:
15665 case R_PPC64_GOT_DTPREL16_HA:
15666 case R_PPC64_GOT16_HA:
15667 case R_PPC64_TOC16_HA:
15668 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15669 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15670 {
15671 bfd_byte *p;
15672 nop_it:
15673 p = contents + (rel->r_offset & ~3);
15674 bfd_put_32 (input_bfd, NOP, p);
15675 goto copy_reloc;
15676 }
15677 break;
15678
15679 case R_PPC64_PLT16_LO:
15680 case R_PPC64_PLT16_LO_DS:
15681 if (unresolved_reloc)
15682 {
15683 unresolved_reloc = FALSE;
15684 goto nop_it;
15685 }
15686 /* Fall through. */
15687 case R_PPC64_GOT_TLSLD16_LO:
15688 case R_PPC64_GOT_TLSGD16_LO:
15689 case R_PPC64_GOT_TPREL16_LO_DS:
15690 case R_PPC64_GOT_DTPREL16_LO_DS:
15691 case R_PPC64_GOT16_LO:
15692 case R_PPC64_GOT16_LO_DS:
15693 case R_PPC64_TOC16_LO:
15694 case R_PPC64_TOC16_LO_DS:
15695 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15696 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15697 {
15698 bfd_byte *p = contents + (rel->r_offset & ~3);
15699 insn = bfd_get_32 (input_bfd, p);
15700 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15701 {
15702 /* Transform addic to addi when we change reg. */
15703 insn &= ~((0x3f << 26) | (0x1f << 16));
15704 insn |= (14u << 26) | (2 << 16);
15705 }
15706 else
15707 {
15708 insn &= ~(0x1f << 16);
15709 insn |= 2 << 16;
15710 }
15711 bfd_put_32 (input_bfd, insn, p);
15712 }
15713 break;
15714
15715 case R_PPC64_TPREL16_HA:
15716 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15717 {
15718 bfd_byte *p = contents + (rel->r_offset & ~3);
15719 insn = bfd_get_32 (input_bfd, p);
15720 if ((insn & ((0x3f << 26) | 0x1f << 16))
15721 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15722 /* xgettext:c-format */
15723 info->callbacks->minfo
15724 (_("%H: warning: %s unexpected insn %#x.\n"),
15725 input_bfd, input_section, rel->r_offset,
15726 ppc64_elf_howto_table[r_type]->name, insn);
15727 else
15728 {
15729 bfd_put_32 (input_bfd, NOP, p);
15730 goto copy_reloc;
15731 }
15732 }
15733 break;
15734
15735 case R_PPC64_TPREL16_LO:
15736 case R_PPC64_TPREL16_LO_DS:
15737 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15738 {
15739 bfd_byte *p = contents + (rel->r_offset & ~3);
15740 insn = bfd_get_32 (input_bfd, p);
15741 insn &= ~(0x1f << 16);
15742 insn |= 13 << 16;
15743 bfd_put_32 (input_bfd, insn, p);
15744 }
15745 break;
15746 }
15747
15748 /* Do any further special processing. */
15749 switch (r_type)
15750 {
15751 default:
15752 break;
15753
15754 case R_PPC64_REL16_HA:
15755 case R_PPC64_REL16DX_HA:
15756 case R_PPC64_ADDR16_HA:
15757 case R_PPC64_ADDR16_HIGHA:
15758 case R_PPC64_ADDR16_HIGHERA:
15759 case R_PPC64_ADDR16_HIGHESTA:
15760 case R_PPC64_TOC16_HA:
15761 case R_PPC64_SECTOFF_HA:
15762 case R_PPC64_TPREL16_HA:
15763 case R_PPC64_TPREL16_HIGHA:
15764 case R_PPC64_TPREL16_HIGHERA:
15765 case R_PPC64_TPREL16_HIGHESTA:
15766 case R_PPC64_DTPREL16_HA:
15767 case R_PPC64_DTPREL16_HIGHA:
15768 case R_PPC64_DTPREL16_HIGHERA:
15769 case R_PPC64_DTPREL16_HIGHESTA:
15770 /* It's just possible that this symbol is a weak symbol
15771 that's not actually defined anywhere. In that case,
15772 'sec' would be NULL, and we should leave the symbol
15773 alone (it will be set to zero elsewhere in the link). */
15774 if (sec == NULL)
15775 break;
15776 /* Fall through. */
15777
15778 case R_PPC64_GOT16_HA:
15779 case R_PPC64_PLTGOT16_HA:
15780 case R_PPC64_PLT16_HA:
15781 case R_PPC64_GOT_TLSGD16_HA:
15782 case R_PPC64_GOT_TLSLD16_HA:
15783 case R_PPC64_GOT_TPREL16_HA:
15784 case R_PPC64_GOT_DTPREL16_HA:
15785 /* Add 0x10000 if sign bit in 0:15 is set.
15786 Bits 0:15 are not used. */
15787 addend += 0x8000;
15788 break;
15789
15790 case R_PPC64_ADDR16_DS:
15791 case R_PPC64_ADDR16_LO_DS:
15792 case R_PPC64_GOT16_DS:
15793 case R_PPC64_GOT16_LO_DS:
15794 case R_PPC64_PLT16_LO_DS:
15795 case R_PPC64_SECTOFF_DS:
15796 case R_PPC64_SECTOFF_LO_DS:
15797 case R_PPC64_TOC16_DS:
15798 case R_PPC64_TOC16_LO_DS:
15799 case R_PPC64_PLTGOT16_DS:
15800 case R_PPC64_PLTGOT16_LO_DS:
15801 case R_PPC64_GOT_TPREL16_DS:
15802 case R_PPC64_GOT_TPREL16_LO_DS:
15803 case R_PPC64_GOT_DTPREL16_DS:
15804 case R_PPC64_GOT_DTPREL16_LO_DS:
15805 case R_PPC64_TPREL16_DS:
15806 case R_PPC64_TPREL16_LO_DS:
15807 case R_PPC64_DTPREL16_DS:
15808 case R_PPC64_DTPREL16_LO_DS:
15809 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15810 mask = 3;
15811 /* If this reloc is against an lq, lxv, or stxv insn, then
15812 the value must be a multiple of 16. This is somewhat of
15813 a hack, but the "correct" way to do this by defining _DQ
15814 forms of all the _DS relocs bloats all reloc switches in
15815 this file. It doesn't make much sense to use these
15816 relocs in data, so testing the insn should be safe. */
15817 if ((insn & (0x3f << 26)) == (56u << 26)
15818 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15819 mask = 15;
15820 relocation += addend;
15821 addend = insn & (mask ^ 3);
15822 if ((relocation & mask) != 0)
15823 {
15824 relocation ^= relocation & mask;
15825 info->callbacks->einfo
15826 /* xgettext:c-format */
15827 (_("%H: error: %s not a multiple of %u\n"),
15828 input_bfd, input_section, rel->r_offset,
15829 ppc64_elf_howto_table[r_type]->name,
15830 mask + 1);
15831 bfd_set_error (bfd_error_bad_value);
15832 ret = FALSE;
15833 goto copy_reloc;
15834 }
15835 break;
15836 }
15837
15838 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15839 because such sections are not SEC_ALLOC and thus ld.so will
15840 not process them. */
15841 howto = ppc64_elf_howto_table[(int) r_type];
15842 if (unresolved_reloc
15843 && !((input_section->flags & SEC_DEBUGGING) != 0
15844 && h->elf.def_dynamic)
15845 && _bfd_elf_section_offset (output_bfd, info, input_section,
15846 rel->r_offset) != (bfd_vma) -1)
15847 {
15848 info->callbacks->einfo
15849 /* xgettext:c-format */
15850 (_("%H: unresolvable %s against `%pT'\n"),
15851 input_bfd, input_section, rel->r_offset,
15852 howto->name,
15853 h->elf.root.root.string);
15854 ret = FALSE;
15855 }
15856
15857 /* 16-bit fields in insns mostly have signed values, but a
15858 few insns have 16-bit unsigned values. Really, we should
15859 have different reloc types. */
15860 if (howto->complain_on_overflow != complain_overflow_dont
15861 && howto->dst_mask == 0xffff
15862 && (input_section->flags & SEC_CODE) != 0)
15863 {
15864 enum complain_overflow complain = complain_overflow_signed;
15865
15866 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15867 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15868 complain = complain_overflow_bitfield;
15869 else if (howto->rightshift == 0
15870 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15871 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15872 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15873 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15874 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15875 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15876 complain = complain_overflow_unsigned;
15877 if (howto->complain_on_overflow != complain)
15878 {
15879 alt_howto = *howto;
15880 alt_howto.complain_on_overflow = complain;
15881 howto = &alt_howto;
15882 }
15883 }
15884
15885 if (r_type == R_PPC64_REL16DX_HA)
15886 {
15887 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15888 if (rel->r_offset + 4 > input_section->size)
15889 r = bfd_reloc_outofrange;
15890 else
15891 {
15892 relocation += addend;
15893 relocation -= (rel->r_offset
15894 + input_section->output_offset
15895 + input_section->output_section->vma);
15896 relocation = (bfd_signed_vma) relocation >> 16;
15897 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15898 insn &= ~0x1fffc1;
15899 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15900 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15901 r = bfd_reloc_ok;
15902 if (relocation + 0x8000 > 0xffff)
15903 r = bfd_reloc_overflow;
15904 }
15905 }
15906 else
15907 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15908 rel->r_offset, relocation, addend);
15909
15910 if (r != bfd_reloc_ok)
15911 {
15912 char *more_info = NULL;
15913 const char *reloc_name = howto->name;
15914
15915 if (reloc_dest != DEST_NORMAL)
15916 {
15917 more_info = bfd_malloc (strlen (reloc_name) + 8);
15918 if (more_info != NULL)
15919 {
15920 strcpy (more_info, reloc_name);
15921 strcat (more_info, (reloc_dest == DEST_OPD
15922 ? " (OPD)" : " (stub)"));
15923 reloc_name = more_info;
15924 }
15925 }
15926
15927 if (r == bfd_reloc_overflow)
15928 {
15929 /* On code like "if (foo) foo();" don't report overflow
15930 on a branch to zero when foo is undefined. */
15931 if (!warned
15932 && (reloc_dest == DEST_STUB
15933 || !(h != NULL
15934 && (h->elf.root.type == bfd_link_hash_undefweak
15935 || h->elf.root.type == bfd_link_hash_undefined)
15936 && is_branch_reloc (r_type))))
15937 info->callbacks->reloc_overflow (info, &h->elf.root,
15938 sym_name, reloc_name,
15939 orig_rel.r_addend,
15940 input_bfd, input_section,
15941 rel->r_offset);
15942 }
15943 else
15944 {
15945 info->callbacks->einfo
15946 /* xgettext:c-format */
15947 (_("%H: %s against `%pT': error %d\n"),
15948 input_bfd, input_section, rel->r_offset,
15949 reloc_name, sym_name, (int) r);
15950 ret = FALSE;
15951 }
15952 if (more_info != NULL)
15953 free (more_info);
15954 }
15955 copy_reloc:
15956 if (wrel != rel)
15957 *wrel = *rel;
15958 }
15959
15960 if (wrel != rel)
15961 {
15962 Elf_Internal_Shdr *rel_hdr;
15963 size_t deleted = rel - wrel;
15964
15965 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15966 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15967 if (rel_hdr->sh_size == 0)
15968 {
15969 /* It is too late to remove an empty reloc section. Leave
15970 one NONE reloc.
15971 ??? What is wrong with an empty section??? */
15972 rel_hdr->sh_size = rel_hdr->sh_entsize;
15973 deleted -= 1;
15974 }
15975 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15976 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15977 input_section->reloc_count -= deleted;
15978 }
15979
15980 /* If we're emitting relocations, then shortly after this function
15981 returns, reloc offsets and addends for this section will be
15982 adjusted. Worse, reloc symbol indices will be for the output
15983 file rather than the input. Save a copy of the relocs for
15984 opd_entry_value. */
15985 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15986 {
15987 bfd_size_type amt;
15988 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15989 rel = bfd_alloc (input_bfd, amt);
15990 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15991 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15992 if (rel == NULL)
15993 return FALSE;
15994 memcpy (rel, relocs, amt);
15995 }
15996 return ret;
15997 }
15998
15999 /* Adjust the value of any local symbols in opd sections. */
16000
16001 static int
16002 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16003 const char *name ATTRIBUTE_UNUSED,
16004 Elf_Internal_Sym *elfsym,
16005 asection *input_sec,
16006 struct elf_link_hash_entry *h)
16007 {
16008 struct _opd_sec_data *opd;
16009 long adjust;
16010 bfd_vma value;
16011
16012 if (h != NULL)
16013 return 1;
16014
16015 opd = get_opd_info (input_sec);
16016 if (opd == NULL || opd->adjust == NULL)
16017 return 1;
16018
16019 value = elfsym->st_value - input_sec->output_offset;
16020 if (!bfd_link_relocatable (info))
16021 value -= input_sec->output_section->vma;
16022
16023 adjust = opd->adjust[OPD_NDX (value)];
16024 if (adjust == -1)
16025 return 2;
16026
16027 elfsym->st_value += adjust;
16028 return 1;
16029 }
16030
16031 /* Finish up dynamic symbol handling. We set the contents of various
16032 dynamic sections here. */
16033
16034 static bfd_boolean
16035 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16036 struct bfd_link_info *info,
16037 struct elf_link_hash_entry *h,
16038 Elf_Internal_Sym *sym)
16039 {
16040 struct ppc_link_hash_table *htab;
16041 struct plt_entry *ent;
16042
16043 htab = ppc_hash_table (info);
16044 if (htab == NULL)
16045 return FALSE;
16046
16047 if (!htab->opd_abi && !h->def_regular)
16048 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16049 if (ent->plt.offset != (bfd_vma) -1)
16050 {
16051 /* Mark the symbol as undefined, rather than as
16052 defined in glink. Leave the value if there were
16053 any relocations where pointer equality matters
16054 (this is a clue for the dynamic linker, to make
16055 function pointer comparisons work between an
16056 application and shared library), otherwise set it
16057 to zero. */
16058 sym->st_shndx = SHN_UNDEF;
16059 if (!h->pointer_equality_needed)
16060 sym->st_value = 0;
16061 else if (!h->ref_regular_nonweak)
16062 {
16063 /* This breaks function pointer comparisons, but
16064 that is better than breaking tests for a NULL
16065 function pointer. */
16066 sym->st_value = 0;
16067 }
16068 break;
16069 }
16070
16071 if (h->needs_copy)
16072 {
16073 /* This symbol needs a copy reloc. Set it up. */
16074 Elf_Internal_Rela rela;
16075 asection *srel;
16076 bfd_byte *loc;
16077
16078 if (h->dynindx == -1
16079 || (h->root.type != bfd_link_hash_defined
16080 && h->root.type != bfd_link_hash_defweak)
16081 || htab->elf.srelbss == NULL
16082 || htab->elf.sreldynrelro == NULL)
16083 abort ();
16084
16085 rela.r_offset = (h->root.u.def.value
16086 + h->root.u.def.section->output_section->vma
16087 + h->root.u.def.section->output_offset);
16088 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16089 rela.r_addend = 0;
16090 if (h->root.u.def.section == htab->elf.sdynrelro)
16091 srel = htab->elf.sreldynrelro;
16092 else
16093 srel = htab->elf.srelbss;
16094 loc = srel->contents;
16095 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16096 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16097 }
16098
16099 return TRUE;
16100 }
16101
16102 /* Used to decide how to sort relocs in an optimal manner for the
16103 dynamic linker, before writing them out. */
16104
16105 static enum elf_reloc_type_class
16106 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16107 const asection *rel_sec,
16108 const Elf_Internal_Rela *rela)
16109 {
16110 enum elf_ppc64_reloc_type r_type;
16111 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16112
16113 if (rel_sec == htab->elf.irelplt)
16114 return reloc_class_ifunc;
16115
16116 r_type = ELF64_R_TYPE (rela->r_info);
16117 switch (r_type)
16118 {
16119 case R_PPC64_RELATIVE:
16120 return reloc_class_relative;
16121 case R_PPC64_JMP_SLOT:
16122 return reloc_class_plt;
16123 case R_PPC64_COPY:
16124 return reloc_class_copy;
16125 default:
16126 return reloc_class_normal;
16127 }
16128 }
16129
16130 /* Finish up the dynamic sections. */
16131
16132 static bfd_boolean
16133 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16134 struct bfd_link_info *info)
16135 {
16136 struct ppc_link_hash_table *htab;
16137 bfd *dynobj;
16138 asection *sdyn;
16139
16140 htab = ppc_hash_table (info);
16141 if (htab == NULL)
16142 return FALSE;
16143
16144 dynobj = htab->elf.dynobj;
16145 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16146
16147 if (htab->elf.dynamic_sections_created)
16148 {
16149 Elf64_External_Dyn *dyncon, *dynconend;
16150
16151 if (sdyn == NULL || htab->elf.sgot == NULL)
16152 abort ();
16153
16154 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16155 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16156 for (; dyncon < dynconend; dyncon++)
16157 {
16158 Elf_Internal_Dyn dyn;
16159 asection *s;
16160
16161 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16162
16163 switch (dyn.d_tag)
16164 {
16165 default:
16166 continue;
16167
16168 case DT_PPC64_GLINK:
16169 s = htab->glink;
16170 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16171 /* We stupidly defined DT_PPC64_GLINK to be the start
16172 of glink rather than the first entry point, which is
16173 what ld.so needs, and now have a bigger stub to
16174 support automatic multiple TOCs. */
16175 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16176 break;
16177
16178 case DT_PPC64_OPD:
16179 s = bfd_get_section_by_name (output_bfd, ".opd");
16180 if (s == NULL)
16181 continue;
16182 dyn.d_un.d_ptr = s->vma;
16183 break;
16184
16185 case DT_PPC64_OPT:
16186 if (htab->do_multi_toc && htab->multi_toc_needed)
16187 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16188 if (htab->has_plt_localentry0)
16189 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16190 break;
16191
16192 case DT_PPC64_OPDSZ:
16193 s = bfd_get_section_by_name (output_bfd, ".opd");
16194 if (s == NULL)
16195 continue;
16196 dyn.d_un.d_val = s->size;
16197 break;
16198
16199 case DT_PLTGOT:
16200 s = htab->elf.splt;
16201 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16202 break;
16203
16204 case DT_JMPREL:
16205 s = htab->elf.srelplt;
16206 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16207 break;
16208
16209 case DT_PLTRELSZ:
16210 dyn.d_un.d_val = htab->elf.srelplt->size;
16211 break;
16212
16213 case DT_TEXTREL:
16214 if (htab->local_ifunc_resolver)
16215 info->callbacks->einfo
16216 (_("%X%P: text relocations and GNU indirect "
16217 "functions will result in a segfault at runtime\n"));
16218 else if (htab->maybe_local_ifunc_resolver)
16219 info->callbacks->einfo
16220 (_("%P: warning: text relocations and GNU indirect "
16221 "functions may result in a segfault at runtime\n"));
16222 continue;
16223 }
16224
16225 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16226 }
16227 }
16228
16229 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16230 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16231 {
16232 /* Fill in the first entry in the global offset table.
16233 We use it to hold the link-time TOCbase. */
16234 bfd_put_64 (output_bfd,
16235 elf_gp (output_bfd) + TOC_BASE_OFF,
16236 htab->elf.sgot->contents);
16237
16238 /* Set .got entry size. */
16239 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16240 }
16241
16242 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16243 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16244 {
16245 /* Set .plt entry size. */
16246 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16247 = PLT_ENTRY_SIZE (htab);
16248 }
16249
16250 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16251 brlt ourselves if emitrelocations. */
16252 if (htab->brlt != NULL
16253 && htab->brlt->reloc_count != 0
16254 && !_bfd_elf_link_output_relocs (output_bfd,
16255 htab->brlt,
16256 elf_section_data (htab->brlt)->rela.hdr,
16257 elf_section_data (htab->brlt)->relocs,
16258 NULL))
16259 return FALSE;
16260
16261 if (htab->glink != NULL
16262 && htab->glink->reloc_count != 0
16263 && !_bfd_elf_link_output_relocs (output_bfd,
16264 htab->glink,
16265 elf_section_data (htab->glink)->rela.hdr,
16266 elf_section_data (htab->glink)->relocs,
16267 NULL))
16268 return FALSE;
16269
16270 if (htab->glink_eh_frame != NULL
16271 && htab->glink_eh_frame->size != 0)
16272 {
16273 bfd_vma val;
16274 bfd_byte *p;
16275 struct map_stub *group;
16276 size_t align = 4;
16277
16278 p = htab->glink_eh_frame->contents;
16279 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
16280
16281 for (group = htab->group; group != NULL; group = group->next)
16282 if (group->stub_sec != NULL)
16283 {
16284 /* Offset to stub section. */
16285 val = (group->stub_sec->output_section->vma
16286 + group->stub_sec->output_offset);
16287 val -= (htab->glink_eh_frame->output_section->vma
16288 + htab->glink_eh_frame->output_offset
16289 + (p + 8 - htab->glink_eh_frame->contents));
16290 if (val + 0x80000000 > 0xffffffff)
16291 {
16292 _bfd_error_handler
16293 (_("%s offset too large for .eh_frame sdata4 encoding"),
16294 group->stub_sec->name);
16295 return FALSE;
16296 }
16297 bfd_put_32 (dynobj, val, p + 8);
16298 p += stub_eh_frame_size (group, align);
16299 }
16300 if (htab->glink != NULL && htab->glink->size != 0)
16301 {
16302 /* Offset to .glink. */
16303 val = (htab->glink->output_section->vma
16304 + htab->glink->output_offset
16305 + 8);
16306 val -= (htab->glink_eh_frame->output_section->vma
16307 + htab->glink_eh_frame->output_offset
16308 + (p + 8 - htab->glink_eh_frame->contents));
16309 if (val + 0x80000000 > 0xffffffff)
16310 {
16311 _bfd_error_handler
16312 (_("%s offset too large for .eh_frame sdata4 encoding"),
16313 htab->glink->name);
16314 return FALSE;
16315 }
16316 bfd_put_32 (dynobj, val, p + 8);
16317 p += (24 + align - 1) & -align;
16318 }
16319
16320 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16321 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16322 htab->glink_eh_frame,
16323 htab->glink_eh_frame->contents))
16324 return FALSE;
16325 }
16326
16327 /* We need to handle writing out multiple GOT sections ourselves,
16328 since we didn't add them to DYNOBJ. We know dynobj is the first
16329 bfd. */
16330 while ((dynobj = dynobj->link.next) != NULL)
16331 {
16332 asection *s;
16333
16334 if (!is_ppc64_elf (dynobj))
16335 continue;
16336
16337 s = ppc64_elf_tdata (dynobj)->got;
16338 if (s != NULL
16339 && s->size != 0
16340 && s->output_section != bfd_abs_section_ptr
16341 && !bfd_set_section_contents (output_bfd, s->output_section,
16342 s->contents, s->output_offset,
16343 s->size))
16344 return FALSE;
16345 s = ppc64_elf_tdata (dynobj)->relgot;
16346 if (s != NULL
16347 && s->size != 0
16348 && s->output_section != bfd_abs_section_ptr
16349 && !bfd_set_section_contents (output_bfd, s->output_section,
16350 s->contents, s->output_offset,
16351 s->size))
16352 return FALSE;
16353 }
16354
16355 return TRUE;
16356 }
16357
16358 #include "elf64-target.h"
16359
16360 /* FreeBSD support */
16361
16362 #undef TARGET_LITTLE_SYM
16363 #undef TARGET_LITTLE_NAME
16364
16365 #undef TARGET_BIG_SYM
16366 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16367 #undef TARGET_BIG_NAME
16368 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16369
16370 #undef ELF_OSABI
16371 #define ELF_OSABI ELFOSABI_FREEBSD
16372
16373 #undef elf64_bed
16374 #define elf64_bed elf64_powerpc_fbsd_bed
16375
16376 #include "elf64-target.h"
This page took 0.384841 seconds and 4 git commands to generate.