PowerPC64 EH info for _notoc linkage stubs
[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 ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
169 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
171
172 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177 #define BNECTR 0x4ca20420 /* bnectr+ */
178 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
179
180 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
181 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
182 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
183
184 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
187
188 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
189 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
190 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
192 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
193 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
194 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
196 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
197 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
200
201 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202 #define GLINK_PLTRESOLVE_SIZE(htab) \
203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
204 /* 0: */
205 /* .quad plt0-1f */
206 /* __glink: */
207 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
208 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
209 /* 1: */
210 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
211 /* ld %2,(0b-1b)(%11) */
212 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
213 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
214 /* ld %12,0(%11) */
215 /* ld %2,8(%11) */
216 /* mtctr %12 */
217 /* ld %11,16(%11) */
218 /* bctr */
219 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
224
225 /* Pad with this. */
226 #define NOP 0x60000000
227
228 /* Some other nops. */
229 #define CROR_151515 0x4def7b82
230 #define CROR_313131 0x4ffffb82
231
232 /* .glink entries for the first 32k functions are two instructions. */
233 #define LI_R0_0 0x38000000 /* li %r0,0 */
234 #define B_DOT 0x48000000 /* b . */
235
236 /* After that, we need two instructions to load the index, followed by
237 a branch. */
238 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
239 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
240
241 /* Instructions used by the save and restore reg functions. */
242 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
246 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
248 #define LI_R12_0 0x39800000 /* li %r12,0 */
249 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
252 #define BLR 0x4e800020 /* blr */
253
254 /* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
257 relocate .opd without reloc entries. */
258 #ifndef NO_OPD_RELOCS
259 #define NO_OPD_RELOCS 0
260 #endif
261
262 #ifndef ARRAY_SIZE
263 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
264 #endif
265
266 static inline int
267 abiversion (bfd *abfd)
268 {
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
270 }
271
272 static inline void
273 set_abiversion (bfd *abfd, int ver)
274 {
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
277 }
278 \f
279 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
280
281 /* Relocation HOWTO's. */
282 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
283
284 static reloc_howto_type ppc64_elf_howto_raw[] =
285 {
286 /* This reloc does nothing. */
287 HOWTO (R_PPC64_NONE, /* type */
288 0, /* rightshift */
289 3, /* size (0 = byte, 1 = short, 2 = long) */
290 0, /* bitsize */
291 FALSE, /* pc_relative */
292 0, /* bitpos */
293 complain_overflow_dont, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_PPC64_NONE", /* name */
296 FALSE, /* partial_inplace */
297 0, /* src_mask */
298 0, /* dst_mask */
299 FALSE), /* pcrel_offset */
300
301 /* A standard 32 bit relocation. */
302 HOWTO (R_PPC64_ADDR32, /* type */
303 0, /* rightshift */
304 2, /* size (0 = byte, 1 = short, 2 = long) */
305 32, /* bitsize */
306 FALSE, /* pc_relative */
307 0, /* bitpos */
308 complain_overflow_bitfield, /* complain_on_overflow */
309 bfd_elf_generic_reloc, /* special_function */
310 "R_PPC64_ADDR32", /* name */
311 FALSE, /* partial_inplace */
312 0, /* src_mask */
313 0xffffffff, /* dst_mask */
314 FALSE), /* pcrel_offset */
315
316 /* An absolute 26 bit branch; the lower two bits must be zero.
317 FIXME: we don't check that, we just clear them. */
318 HOWTO (R_PPC64_ADDR24, /* type */
319 0, /* rightshift */
320 2, /* size (0 = byte, 1 = short, 2 = long) */
321 26, /* bitsize */
322 FALSE, /* pc_relative */
323 0, /* bitpos */
324 complain_overflow_bitfield, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_PPC64_ADDR24", /* name */
327 FALSE, /* partial_inplace */
328 0, /* src_mask */
329 0x03fffffc, /* dst_mask */
330 FALSE), /* pcrel_offset */
331
332 /* A standard 16 bit relocation. */
333 HOWTO (R_PPC64_ADDR16, /* type */
334 0, /* rightshift */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
336 16, /* bitsize */
337 FALSE, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_PPC64_ADDR16", /* name */
342 FALSE, /* partial_inplace */
343 0, /* src_mask */
344 0xffff, /* dst_mask */
345 FALSE), /* pcrel_offset */
346
347 /* A 16 bit relocation without overflow. */
348 HOWTO (R_PPC64_ADDR16_LO, /* type */
349 0, /* rightshift */
350 1, /* size (0 = byte, 1 = short, 2 = long) */
351 16, /* bitsize */
352 FALSE, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_dont,/* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_PPC64_ADDR16_LO", /* name */
357 FALSE, /* partial_inplace */
358 0, /* src_mask */
359 0xffff, /* dst_mask */
360 FALSE), /* pcrel_offset */
361
362 /* Bits 16-31 of an address. */
363 HOWTO (R_PPC64_ADDR16_HI, /* type */
364 16, /* rightshift */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_signed, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_PPC64_ADDR16_HI", /* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0xffff, /* dst_mask */
375 FALSE), /* pcrel_offset */
376
377 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
378 bits, treated as a signed number, is negative. */
379 HOWTO (R_PPC64_ADDR16_HA, /* type */
380 16, /* rightshift */
381 1, /* size (0 = byte, 1 = short, 2 = long) */
382 16, /* bitsize */
383 FALSE, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
386 ppc64_elf_ha_reloc, /* special_function */
387 "R_PPC64_ADDR16_HA", /* name */
388 FALSE, /* partial_inplace */
389 0, /* src_mask */
390 0xffff, /* dst_mask */
391 FALSE), /* pcrel_offset */
392
393 /* An absolute 16 bit branch; the lower two bits must be zero.
394 FIXME: we don't check that, we just clear them. */
395 HOWTO (R_PPC64_ADDR14, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_signed, /* complain_on_overflow */
402 ppc64_elf_branch_reloc, /* special_function */
403 "R_PPC64_ADDR14", /* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0x0000fffc, /* dst_mask */
407 FALSE), /* pcrel_offset */
408
409 /* An absolute 16 bit branch, for which bit 10 should be set to
410 indicate that the branch is expected to be taken. The lower two
411 bits must be zero. */
412 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 FALSE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 ppc64_elf_brtaken_reloc, /* special_function */
420 "R_PPC64_ADDR14_BRTAKEN",/* name */
421 FALSE, /* partial_inplace */
422 0, /* src_mask */
423 0x0000fffc, /* dst_mask */
424 FALSE), /* pcrel_offset */
425
426 /* An absolute 16 bit branch, for which bit 10 should be set to
427 indicate that the branch is not expected to be taken. The lower
428 two bits must be zero. */
429 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
430 0, /* rightshift */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
432 16, /* bitsize */
433 FALSE, /* pc_relative */
434 0, /* bitpos */
435 complain_overflow_signed, /* complain_on_overflow */
436 ppc64_elf_brtaken_reloc, /* special_function */
437 "R_PPC64_ADDR14_BRNTAKEN",/* name */
438 FALSE, /* partial_inplace */
439 0, /* src_mask */
440 0x0000fffc, /* dst_mask */
441 FALSE), /* pcrel_offset */
442
443 /* A relative 26 bit branch; the lower two bits must be zero. */
444 HOWTO (R_PPC64_REL24, /* type */
445 0, /* rightshift */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
447 26, /* bitsize */
448 TRUE, /* pc_relative */
449 0, /* bitpos */
450 complain_overflow_signed, /* complain_on_overflow */
451 ppc64_elf_branch_reloc, /* special_function */
452 "R_PPC64_REL24", /* name */
453 FALSE, /* partial_inplace */
454 0, /* src_mask */
455 0x03fffffc, /* dst_mask */
456 TRUE), /* pcrel_offset */
457
458 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
459 HOWTO (R_PPC64_REL24_NOTOC, /* type */
460 0, /* rightshift */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
462 26, /* bitsize */
463 TRUE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_signed, /* complain_on_overflow */
466 ppc64_elf_branch_reloc, /* special_function */
467 "R_PPC64_REL24_NOTOC", /* name */
468 FALSE, /* partial_inplace */
469 0, /* src_mask */
470 0x03fffffc, /* dst_mask */
471 TRUE), /* pcrel_offset */
472
473 /* A relative 16 bit branch; the lower two bits must be zero. */
474 HOWTO (R_PPC64_REL14, /* type */
475 0, /* rightshift */
476 2, /* size (0 = byte, 1 = short, 2 = long) */
477 16, /* bitsize */
478 TRUE, /* pc_relative */
479 0, /* bitpos */
480 complain_overflow_signed, /* complain_on_overflow */
481 ppc64_elf_branch_reloc, /* special_function */
482 "R_PPC64_REL14", /* name */
483 FALSE, /* partial_inplace */
484 0, /* src_mask */
485 0x0000fffc, /* dst_mask */
486 TRUE), /* pcrel_offset */
487
488 /* A relative 16 bit branch. Bit 10 should be set to indicate that
489 the branch is expected to be taken. The lower two bits must be
490 zero. */
491 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
492 0, /* rightshift */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
495 TRUE, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_signed, /* complain_on_overflow */
498 ppc64_elf_brtaken_reloc, /* special_function */
499 "R_PPC64_REL14_BRTAKEN", /* name */
500 FALSE, /* partial_inplace */
501 0, /* src_mask */
502 0x0000fffc, /* dst_mask */
503 TRUE), /* pcrel_offset */
504
505 /* A relative 16 bit branch. Bit 10 should be set to indicate that
506 the branch is not expected to be taken. The lower two bits must
507 be zero. */
508 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
509 0, /* rightshift */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
511 16, /* bitsize */
512 TRUE, /* pc_relative */
513 0, /* bitpos */
514 complain_overflow_signed, /* complain_on_overflow */
515 ppc64_elf_brtaken_reloc, /* special_function */
516 "R_PPC64_REL14_BRNTAKEN",/* name */
517 FALSE, /* partial_inplace */
518 0, /* src_mask */
519 0x0000fffc, /* dst_mask */
520 TRUE), /* pcrel_offset */
521
522 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
523 symbol. */
524 HOWTO (R_PPC64_GOT16, /* type */
525 0, /* rightshift */
526 1, /* size (0 = byte, 1 = short, 2 = long) */
527 16, /* bitsize */
528 FALSE, /* pc_relative */
529 0, /* bitpos */
530 complain_overflow_signed, /* complain_on_overflow */
531 ppc64_elf_unhandled_reloc, /* special_function */
532 "R_PPC64_GOT16", /* name */
533 FALSE, /* partial_inplace */
534 0, /* src_mask */
535 0xffff, /* dst_mask */
536 FALSE), /* pcrel_offset */
537
538 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
539 the symbol. */
540 HOWTO (R_PPC64_GOT16_LO, /* type */
541 0, /* rightshift */
542 1, /* size (0 = byte, 1 = short, 2 = long) */
543 16, /* bitsize */
544 FALSE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_dont, /* complain_on_overflow */
547 ppc64_elf_unhandled_reloc, /* special_function */
548 "R_PPC64_GOT16_LO", /* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0xffff, /* dst_mask */
552 FALSE), /* pcrel_offset */
553
554 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
555 the symbol. */
556 HOWTO (R_PPC64_GOT16_HI, /* type */
557 16, /* rightshift */
558 1, /* size (0 = byte, 1 = short, 2 = long) */
559 16, /* bitsize */
560 FALSE, /* pc_relative */
561 0, /* bitpos */
562 complain_overflow_signed,/* complain_on_overflow */
563 ppc64_elf_unhandled_reloc, /* special_function */
564 "R_PPC64_GOT16_HI", /* name */
565 FALSE, /* partial_inplace */
566 0, /* src_mask */
567 0xffff, /* dst_mask */
568 FALSE), /* pcrel_offset */
569
570 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
571 the symbol. */
572 HOWTO (R_PPC64_GOT16_HA, /* type */
573 16, /* rightshift */
574 1, /* size (0 = byte, 1 = short, 2 = long) */
575 16, /* bitsize */
576 FALSE, /* pc_relative */
577 0, /* bitpos */
578 complain_overflow_signed,/* complain_on_overflow */
579 ppc64_elf_unhandled_reloc, /* special_function */
580 "R_PPC64_GOT16_HA", /* name */
581 FALSE, /* partial_inplace */
582 0, /* src_mask */
583 0xffff, /* dst_mask */
584 FALSE), /* pcrel_offset */
585
586 /* This is used only by the dynamic linker. The symbol should exist
587 both in the object being run and in some shared library. The
588 dynamic linker copies the data addressed by the symbol from the
589 shared library into the object, because the object being
590 run has to have the data at some particular address. */
591 HOWTO (R_PPC64_COPY, /* type */
592 0, /* rightshift */
593 0, /* this one is variable size */
594 0, /* bitsize */
595 FALSE, /* pc_relative */
596 0, /* bitpos */
597 complain_overflow_dont, /* complain_on_overflow */
598 ppc64_elf_unhandled_reloc, /* special_function */
599 "R_PPC64_COPY", /* name */
600 FALSE, /* partial_inplace */
601 0, /* src_mask */
602 0, /* dst_mask */
603 FALSE), /* pcrel_offset */
604
605 /* Like R_PPC64_ADDR64, but used when setting global offset table
606 entries. */
607 HOWTO (R_PPC64_GLOB_DAT, /* type */
608 0, /* rightshift */
609 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
610 64, /* bitsize */
611 FALSE, /* pc_relative */
612 0, /* bitpos */
613 complain_overflow_dont, /* complain_on_overflow */
614 ppc64_elf_unhandled_reloc, /* special_function */
615 "R_PPC64_GLOB_DAT", /* name */
616 FALSE, /* partial_inplace */
617 0, /* src_mask */
618 ONES (64), /* dst_mask */
619 FALSE), /* pcrel_offset */
620
621 /* Created by the link editor. Marks a procedure linkage table
622 entry for a symbol. */
623 HOWTO (R_PPC64_JMP_SLOT, /* type */
624 0, /* rightshift */
625 0, /* size (0 = byte, 1 = short, 2 = long) */
626 0, /* bitsize */
627 FALSE, /* pc_relative */
628 0, /* bitpos */
629 complain_overflow_dont, /* complain_on_overflow */
630 ppc64_elf_unhandled_reloc, /* special_function */
631 "R_PPC64_JMP_SLOT", /* name */
632 FALSE, /* partial_inplace */
633 0, /* src_mask */
634 0, /* dst_mask */
635 FALSE), /* pcrel_offset */
636
637 /* Used only by the dynamic linker. When the object is run, this
638 doubleword64 is set to the load address of the object, plus the
639 addend. */
640 HOWTO (R_PPC64_RELATIVE, /* type */
641 0, /* rightshift */
642 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
643 64, /* bitsize */
644 FALSE, /* pc_relative */
645 0, /* bitpos */
646 complain_overflow_dont, /* complain_on_overflow */
647 bfd_elf_generic_reloc, /* special_function */
648 "R_PPC64_RELATIVE", /* name */
649 FALSE, /* partial_inplace */
650 0, /* src_mask */
651 ONES (64), /* dst_mask */
652 FALSE), /* pcrel_offset */
653
654 /* Like R_PPC64_ADDR32, but may be unaligned. */
655 HOWTO (R_PPC64_UADDR32, /* type */
656 0, /* rightshift */
657 2, /* size (0 = byte, 1 = short, 2 = long) */
658 32, /* bitsize */
659 FALSE, /* pc_relative */
660 0, /* bitpos */
661 complain_overflow_bitfield, /* complain_on_overflow */
662 bfd_elf_generic_reloc, /* special_function */
663 "R_PPC64_UADDR32", /* name */
664 FALSE, /* partial_inplace */
665 0, /* src_mask */
666 0xffffffff, /* dst_mask */
667 FALSE), /* pcrel_offset */
668
669 /* Like R_PPC64_ADDR16, but may be unaligned. */
670 HOWTO (R_PPC64_UADDR16, /* type */
671 0, /* rightshift */
672 1, /* size (0 = byte, 1 = short, 2 = long) */
673 16, /* bitsize */
674 FALSE, /* pc_relative */
675 0, /* bitpos */
676 complain_overflow_bitfield, /* complain_on_overflow */
677 bfd_elf_generic_reloc, /* special_function */
678 "R_PPC64_UADDR16", /* name */
679 FALSE, /* partial_inplace */
680 0, /* src_mask */
681 0xffff, /* dst_mask */
682 FALSE), /* pcrel_offset */
683
684 /* 32-bit PC relative. */
685 HOWTO (R_PPC64_REL32, /* type */
686 0, /* rightshift */
687 2, /* size (0 = byte, 1 = short, 2 = long) */
688 32, /* bitsize */
689 TRUE, /* pc_relative */
690 0, /* bitpos */
691 complain_overflow_signed, /* complain_on_overflow */
692 bfd_elf_generic_reloc, /* special_function */
693 "R_PPC64_REL32", /* name */
694 FALSE, /* partial_inplace */
695 0, /* src_mask */
696 0xffffffff, /* dst_mask */
697 TRUE), /* pcrel_offset */
698
699 /* 32-bit relocation to the symbol's procedure linkage table. */
700 HOWTO (R_PPC64_PLT32, /* type */
701 0, /* rightshift */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
703 32, /* bitsize */
704 FALSE, /* pc_relative */
705 0, /* bitpos */
706 complain_overflow_bitfield, /* complain_on_overflow */
707 ppc64_elf_unhandled_reloc, /* special_function */
708 "R_PPC64_PLT32", /* name */
709 FALSE, /* partial_inplace */
710 0, /* src_mask */
711 0xffffffff, /* dst_mask */
712 FALSE), /* pcrel_offset */
713
714 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
715 FIXME: R_PPC64_PLTREL32 not supported. */
716 HOWTO (R_PPC64_PLTREL32, /* type */
717 0, /* rightshift */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
719 32, /* bitsize */
720 TRUE, /* pc_relative */
721 0, /* bitpos */
722 complain_overflow_signed, /* complain_on_overflow */
723 ppc64_elf_unhandled_reloc, /* special_function */
724 "R_PPC64_PLTREL32", /* name */
725 FALSE, /* partial_inplace */
726 0, /* src_mask */
727 0xffffffff, /* dst_mask */
728 TRUE), /* pcrel_offset */
729
730 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
731 the symbol. */
732 HOWTO (R_PPC64_PLT16_LO, /* type */
733 0, /* rightshift */
734 1, /* size (0 = byte, 1 = short, 2 = long) */
735 16, /* bitsize */
736 FALSE, /* pc_relative */
737 0, /* bitpos */
738 complain_overflow_dont, /* complain_on_overflow */
739 ppc64_elf_unhandled_reloc, /* special_function */
740 "R_PPC64_PLT16_LO", /* name */
741 FALSE, /* partial_inplace */
742 0, /* src_mask */
743 0xffff, /* dst_mask */
744 FALSE), /* pcrel_offset */
745
746 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
747 the symbol. */
748 HOWTO (R_PPC64_PLT16_HI, /* type */
749 16, /* rightshift */
750 1, /* size (0 = byte, 1 = short, 2 = long) */
751 16, /* bitsize */
752 FALSE, /* pc_relative */
753 0, /* bitpos */
754 complain_overflow_signed, /* complain_on_overflow */
755 ppc64_elf_unhandled_reloc, /* special_function */
756 "R_PPC64_PLT16_HI", /* name */
757 FALSE, /* partial_inplace */
758 0, /* src_mask */
759 0xffff, /* dst_mask */
760 FALSE), /* pcrel_offset */
761
762 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
763 the symbol. */
764 HOWTO (R_PPC64_PLT16_HA, /* type */
765 16, /* rightshift */
766 1, /* size (0 = byte, 1 = short, 2 = long) */
767 16, /* bitsize */
768 FALSE, /* pc_relative */
769 0, /* bitpos */
770 complain_overflow_signed, /* complain_on_overflow */
771 ppc64_elf_unhandled_reloc, /* special_function */
772 "R_PPC64_PLT16_HA", /* name */
773 FALSE, /* partial_inplace */
774 0, /* src_mask */
775 0xffff, /* dst_mask */
776 FALSE), /* pcrel_offset */
777
778 /* 16-bit section relative relocation. */
779 HOWTO (R_PPC64_SECTOFF, /* type */
780 0, /* rightshift */
781 1, /* size (0 = byte, 1 = short, 2 = long) */
782 16, /* bitsize */
783 FALSE, /* pc_relative */
784 0, /* bitpos */
785 complain_overflow_signed, /* complain_on_overflow */
786 ppc64_elf_sectoff_reloc, /* special_function */
787 "R_PPC64_SECTOFF", /* name */
788 FALSE, /* partial_inplace */
789 0, /* src_mask */
790 0xffff, /* dst_mask */
791 FALSE), /* pcrel_offset */
792
793 /* Like R_PPC64_SECTOFF, but no overflow warning. */
794 HOWTO (R_PPC64_SECTOFF_LO, /* type */
795 0, /* rightshift */
796 1, /* size (0 = byte, 1 = short, 2 = long) */
797 16, /* bitsize */
798 FALSE, /* pc_relative */
799 0, /* bitpos */
800 complain_overflow_dont, /* complain_on_overflow */
801 ppc64_elf_sectoff_reloc, /* special_function */
802 "R_PPC64_SECTOFF_LO", /* name */
803 FALSE, /* partial_inplace */
804 0, /* src_mask */
805 0xffff, /* dst_mask */
806 FALSE), /* pcrel_offset */
807
808 /* 16-bit upper half section relative relocation. */
809 HOWTO (R_PPC64_SECTOFF_HI, /* type */
810 16, /* rightshift */
811 1, /* size (0 = byte, 1 = short, 2 = long) */
812 16, /* bitsize */
813 FALSE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_signed, /* complain_on_overflow */
816 ppc64_elf_sectoff_reloc, /* special_function */
817 "R_PPC64_SECTOFF_HI", /* name */
818 FALSE, /* partial_inplace */
819 0, /* src_mask */
820 0xffff, /* dst_mask */
821 FALSE), /* pcrel_offset */
822
823 /* 16-bit upper half adjusted section relative relocation. */
824 HOWTO (R_PPC64_SECTOFF_HA, /* type */
825 16, /* rightshift */
826 1, /* size (0 = byte, 1 = short, 2 = long) */
827 16, /* bitsize */
828 FALSE, /* pc_relative */
829 0, /* bitpos */
830 complain_overflow_signed, /* complain_on_overflow */
831 ppc64_elf_sectoff_ha_reloc, /* special_function */
832 "R_PPC64_SECTOFF_HA", /* name */
833 FALSE, /* partial_inplace */
834 0, /* src_mask */
835 0xffff, /* dst_mask */
836 FALSE), /* pcrel_offset */
837
838 /* Like R_PPC64_REL24 without touching the two least significant bits. */
839 HOWTO (R_PPC64_REL30, /* type */
840 2, /* rightshift */
841 2, /* size (0 = byte, 1 = short, 2 = long) */
842 30, /* bitsize */
843 TRUE, /* pc_relative */
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
846 bfd_elf_generic_reloc, /* special_function */
847 "R_PPC64_REL30", /* name */
848 FALSE, /* partial_inplace */
849 0, /* src_mask */
850 0xfffffffc, /* dst_mask */
851 TRUE), /* pcrel_offset */
852
853 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
854
855 /* A standard 64-bit relocation. */
856 HOWTO (R_PPC64_ADDR64, /* type */
857 0, /* rightshift */
858 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
859 64, /* bitsize */
860 FALSE, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_dont, /* complain_on_overflow */
863 bfd_elf_generic_reloc, /* special_function */
864 "R_PPC64_ADDR64", /* name */
865 FALSE, /* partial_inplace */
866 0, /* src_mask */
867 ONES (64), /* dst_mask */
868 FALSE), /* pcrel_offset */
869
870 /* The bits 32-47 of an address. */
871 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
872 32, /* rightshift */
873 1, /* size (0 = byte, 1 = short, 2 = long) */
874 16, /* bitsize */
875 FALSE, /* pc_relative */
876 0, /* bitpos */
877 complain_overflow_dont, /* complain_on_overflow */
878 bfd_elf_generic_reloc, /* special_function */
879 "R_PPC64_ADDR16_HIGHER", /* name */
880 FALSE, /* partial_inplace */
881 0, /* src_mask */
882 0xffff, /* dst_mask */
883 FALSE), /* pcrel_offset */
884
885 /* The bits 32-47 of an address, plus 1 if the contents of the low
886 16 bits, treated as a signed number, is negative. */
887 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
888 32, /* rightshift */
889 1, /* size (0 = byte, 1 = short, 2 = long) */
890 16, /* bitsize */
891 FALSE, /* pc_relative */
892 0, /* bitpos */
893 complain_overflow_dont, /* complain_on_overflow */
894 ppc64_elf_ha_reloc, /* special_function */
895 "R_PPC64_ADDR16_HIGHERA", /* name */
896 FALSE, /* partial_inplace */
897 0, /* src_mask */
898 0xffff, /* dst_mask */
899 FALSE), /* pcrel_offset */
900
901 /* The bits 48-63 of an address. */
902 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
903 48, /* rightshift */
904 1, /* size (0 = byte, 1 = short, 2 = long) */
905 16, /* bitsize */
906 FALSE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_dont, /* complain_on_overflow */
909 bfd_elf_generic_reloc, /* special_function */
910 "R_PPC64_ADDR16_HIGHEST", /* name */
911 FALSE, /* partial_inplace */
912 0, /* src_mask */
913 0xffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
915
916 /* The bits 48-63 of an address, plus 1 if the contents of the low
917 16 bits, treated as a signed number, is negative. */
918 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
919 48, /* rightshift */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
921 16, /* bitsize */
922 FALSE, /* pc_relative */
923 0, /* bitpos */
924 complain_overflow_dont, /* complain_on_overflow */
925 ppc64_elf_ha_reloc, /* special_function */
926 "R_PPC64_ADDR16_HIGHESTA", /* name */
927 FALSE, /* partial_inplace */
928 0, /* src_mask */
929 0xffff, /* dst_mask */
930 FALSE), /* pcrel_offset */
931
932 /* Like ADDR64, but may be unaligned. */
933 HOWTO (R_PPC64_UADDR64, /* type */
934 0, /* rightshift */
935 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
936 64, /* bitsize */
937 FALSE, /* pc_relative */
938 0, /* bitpos */
939 complain_overflow_dont, /* complain_on_overflow */
940 bfd_elf_generic_reloc, /* special_function */
941 "R_PPC64_UADDR64", /* name */
942 FALSE, /* partial_inplace */
943 0, /* src_mask */
944 ONES (64), /* dst_mask */
945 FALSE), /* pcrel_offset */
946
947 /* 64-bit relative relocation. */
948 HOWTO (R_PPC64_REL64, /* type */
949 0, /* rightshift */
950 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
951 64, /* bitsize */
952 TRUE, /* pc_relative */
953 0, /* bitpos */
954 complain_overflow_dont, /* complain_on_overflow */
955 bfd_elf_generic_reloc, /* special_function */
956 "R_PPC64_REL64", /* name */
957 FALSE, /* partial_inplace */
958 0, /* src_mask */
959 ONES (64), /* dst_mask */
960 TRUE), /* pcrel_offset */
961
962 /* 64-bit relocation to the symbol's procedure linkage table. */
963 HOWTO (R_PPC64_PLT64, /* type */
964 0, /* rightshift */
965 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
966 64, /* bitsize */
967 FALSE, /* pc_relative */
968 0, /* bitpos */
969 complain_overflow_dont, /* complain_on_overflow */
970 ppc64_elf_unhandled_reloc, /* special_function */
971 "R_PPC64_PLT64", /* name */
972 FALSE, /* partial_inplace */
973 0, /* src_mask */
974 ONES (64), /* dst_mask */
975 FALSE), /* pcrel_offset */
976
977 /* 64-bit PC relative relocation to the symbol's procedure linkage
978 table. */
979 /* FIXME: R_PPC64_PLTREL64 not supported. */
980 HOWTO (R_PPC64_PLTREL64, /* type */
981 0, /* rightshift */
982 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
983 64, /* bitsize */
984 TRUE, /* pc_relative */
985 0, /* bitpos */
986 complain_overflow_dont, /* complain_on_overflow */
987 ppc64_elf_unhandled_reloc, /* special_function */
988 "R_PPC64_PLTREL64", /* name */
989 FALSE, /* partial_inplace */
990 0, /* src_mask */
991 ONES (64), /* dst_mask */
992 TRUE), /* pcrel_offset */
993
994 /* 16 bit TOC-relative relocation. */
995
996 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
997 HOWTO (R_PPC64_TOC16, /* type */
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_signed, /* complain_on_overflow */
1004 ppc64_elf_toc_reloc, /* special_function */
1005 "R_PPC64_TOC16", /* name */
1006 FALSE, /* partial_inplace */
1007 0, /* src_mask */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1010
1011 /* 16 bit TOC-relative relocation without overflow. */
1012
1013 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
1014 HOWTO (R_PPC64_TOC16_LO, /* type */
1015 0, /* rightshift */
1016 1, /* size (0 = byte, 1 = short, 2 = long) */
1017 16, /* bitsize */
1018 FALSE, /* pc_relative */
1019 0, /* bitpos */
1020 complain_overflow_dont, /* complain_on_overflow */
1021 ppc64_elf_toc_reloc, /* special_function */
1022 "R_PPC64_TOC16_LO", /* name */
1023 FALSE, /* partial_inplace */
1024 0, /* src_mask */
1025 0xffff, /* dst_mask */
1026 FALSE), /* pcrel_offset */
1027
1028 /* 16 bit TOC-relative relocation, high 16 bits. */
1029
1030 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1031 HOWTO (R_PPC64_TOC16_HI, /* type */
1032 16, /* rightshift */
1033 1, /* size (0 = byte, 1 = short, 2 = long) */
1034 16, /* bitsize */
1035 FALSE, /* pc_relative */
1036 0, /* bitpos */
1037 complain_overflow_signed, /* complain_on_overflow */
1038 ppc64_elf_toc_reloc, /* special_function */
1039 "R_PPC64_TOC16_HI", /* name */
1040 FALSE, /* partial_inplace */
1041 0, /* src_mask */
1042 0xffff, /* dst_mask */
1043 FALSE), /* pcrel_offset */
1044
1045 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1046 contents of the low 16 bits, treated as a signed number, is
1047 negative. */
1048
1049 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1050 HOWTO (R_PPC64_TOC16_HA, /* type */
1051 16, /* rightshift */
1052 1, /* size (0 = byte, 1 = short, 2 = long) */
1053 16, /* bitsize */
1054 FALSE, /* pc_relative */
1055 0, /* bitpos */
1056 complain_overflow_signed, /* complain_on_overflow */
1057 ppc64_elf_toc_ha_reloc, /* special_function */
1058 "R_PPC64_TOC16_HA", /* name */
1059 FALSE, /* partial_inplace */
1060 0, /* src_mask */
1061 0xffff, /* dst_mask */
1062 FALSE), /* pcrel_offset */
1063
1064 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1065
1066 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1067 HOWTO (R_PPC64_TOC, /* type */
1068 0, /* rightshift */
1069 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1070 64, /* bitsize */
1071 FALSE, /* pc_relative */
1072 0, /* bitpos */
1073 complain_overflow_dont, /* complain_on_overflow */
1074 ppc64_elf_toc64_reloc, /* special_function */
1075 "R_PPC64_TOC", /* name */
1076 FALSE, /* partial_inplace */
1077 0, /* src_mask */
1078 ONES (64), /* dst_mask */
1079 FALSE), /* pcrel_offset */
1080
1081 /* Like R_PPC64_GOT16, but also informs the link editor that the
1082 value to relocate may (!) refer to a PLT entry which the link
1083 editor (a) may replace with the symbol value. If the link editor
1084 is unable to fully resolve the symbol, it may (b) create a PLT
1085 entry and store the address to the new PLT entry in the GOT.
1086 This permits lazy resolution of function symbols at run time.
1087 The link editor may also skip all of this and just (c) emit a
1088 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1089 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1090 HOWTO (R_PPC64_PLTGOT16, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_signed, /* complain_on_overflow */
1097 ppc64_elf_unhandled_reloc, /* special_function */
1098 "R_PPC64_PLTGOT16", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xffff, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_PLTGOT16, but without overflow. */
1105 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1106 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1107 0, /* rightshift */
1108 1, /* size (0 = byte, 1 = short, 2 = long) */
1109 16, /* bitsize */
1110 FALSE, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_dont, /* complain_on_overflow */
1113 ppc64_elf_unhandled_reloc, /* special_function */
1114 "R_PPC64_PLTGOT16_LO", /* name */
1115 FALSE, /* partial_inplace */
1116 0, /* src_mask */
1117 0xffff, /* dst_mask */
1118 FALSE), /* pcrel_offset */
1119
1120 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1121 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1122 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1123 16, /* rightshift */
1124 1, /* size (0 = byte, 1 = short, 2 = long) */
1125 16, /* bitsize */
1126 FALSE, /* pc_relative */
1127 0, /* bitpos */
1128 complain_overflow_signed, /* complain_on_overflow */
1129 ppc64_elf_unhandled_reloc, /* special_function */
1130 "R_PPC64_PLTGOT16_HI", /* name */
1131 FALSE, /* partial_inplace */
1132 0, /* src_mask */
1133 0xffff, /* dst_mask */
1134 FALSE), /* pcrel_offset */
1135
1136 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1137 1 if the contents of the low 16 bits, treated as a signed number,
1138 is negative. */
1139 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1140 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1141 16, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1143 16, /* bitsize */
1144 FALSE, /* pc_relative */
1145 0, /* bitpos */
1146 complain_overflow_signed, /* complain_on_overflow */
1147 ppc64_elf_unhandled_reloc, /* special_function */
1148 "R_PPC64_PLTGOT16_HA", /* name */
1149 FALSE, /* partial_inplace */
1150 0, /* src_mask */
1151 0xffff, /* dst_mask */
1152 FALSE), /* pcrel_offset */
1153
1154 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1155 HOWTO (R_PPC64_ADDR16_DS, /* type */
1156 0, /* rightshift */
1157 1, /* size (0 = byte, 1 = short, 2 = long) */
1158 16, /* bitsize */
1159 FALSE, /* pc_relative */
1160 0, /* bitpos */
1161 complain_overflow_signed, /* complain_on_overflow */
1162 bfd_elf_generic_reloc, /* special_function */
1163 "R_PPC64_ADDR16_DS", /* name */
1164 FALSE, /* partial_inplace */
1165 0, /* src_mask */
1166 0xfffc, /* dst_mask */
1167 FALSE), /* pcrel_offset */
1168
1169 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1170 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1171 0, /* rightshift */
1172 1, /* size (0 = byte, 1 = short, 2 = long) */
1173 16, /* bitsize */
1174 FALSE, /* pc_relative */
1175 0, /* bitpos */
1176 complain_overflow_dont,/* complain_on_overflow */
1177 bfd_elf_generic_reloc, /* special_function */
1178 "R_PPC64_ADDR16_LO_DS",/* name */
1179 FALSE, /* partial_inplace */
1180 0, /* src_mask */
1181 0xfffc, /* dst_mask */
1182 FALSE), /* pcrel_offset */
1183
1184 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1185 HOWTO (R_PPC64_GOT16_DS, /* type */
1186 0, /* rightshift */
1187 1, /* size (0 = byte, 1 = short, 2 = long) */
1188 16, /* bitsize */
1189 FALSE, /* pc_relative */
1190 0, /* bitpos */
1191 complain_overflow_signed, /* complain_on_overflow */
1192 ppc64_elf_unhandled_reloc, /* special_function */
1193 "R_PPC64_GOT16_DS", /* name */
1194 FALSE, /* partial_inplace */
1195 0, /* src_mask */
1196 0xfffc, /* dst_mask */
1197 FALSE), /* pcrel_offset */
1198
1199 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1200 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1201 0, /* rightshift */
1202 1, /* size (0 = byte, 1 = short, 2 = long) */
1203 16, /* bitsize */
1204 FALSE, /* pc_relative */
1205 0, /* bitpos */
1206 complain_overflow_dont, /* complain_on_overflow */
1207 ppc64_elf_unhandled_reloc, /* special_function */
1208 "R_PPC64_GOT16_LO_DS", /* name */
1209 FALSE, /* partial_inplace */
1210 0, /* src_mask */
1211 0xfffc, /* dst_mask */
1212 FALSE), /* pcrel_offset */
1213
1214 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1215 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1216 0, /* rightshift */
1217 1, /* size (0 = byte, 1 = short, 2 = long) */
1218 16, /* bitsize */
1219 FALSE, /* pc_relative */
1220 0, /* bitpos */
1221 complain_overflow_dont, /* complain_on_overflow */
1222 ppc64_elf_unhandled_reloc, /* special_function */
1223 "R_PPC64_PLT16_LO_DS", /* name */
1224 FALSE, /* partial_inplace */
1225 0, /* src_mask */
1226 0xfffc, /* dst_mask */
1227 FALSE), /* pcrel_offset */
1228
1229 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1230 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1231 0, /* rightshift */
1232 1, /* size (0 = byte, 1 = short, 2 = long) */
1233 16, /* bitsize */
1234 FALSE, /* pc_relative */
1235 0, /* bitpos */
1236 complain_overflow_signed, /* complain_on_overflow */
1237 ppc64_elf_sectoff_reloc, /* special_function */
1238 "R_PPC64_SECTOFF_DS", /* name */
1239 FALSE, /* partial_inplace */
1240 0, /* src_mask */
1241 0xfffc, /* dst_mask */
1242 FALSE), /* pcrel_offset */
1243
1244 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1245 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1246 0, /* rightshift */
1247 1, /* size (0 = byte, 1 = short, 2 = long) */
1248 16, /* bitsize */
1249 FALSE, /* pc_relative */
1250 0, /* bitpos */
1251 complain_overflow_dont, /* complain_on_overflow */
1252 ppc64_elf_sectoff_reloc, /* special_function */
1253 "R_PPC64_SECTOFF_LO_DS",/* name */
1254 FALSE, /* partial_inplace */
1255 0, /* src_mask */
1256 0xfffc, /* dst_mask */
1257 FALSE), /* pcrel_offset */
1258
1259 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1260 HOWTO (R_PPC64_TOC16_DS, /* type */
1261 0, /* rightshift */
1262 1, /* size (0 = byte, 1 = short, 2 = long) */
1263 16, /* bitsize */
1264 FALSE, /* pc_relative */
1265 0, /* bitpos */
1266 complain_overflow_signed, /* complain_on_overflow */
1267 ppc64_elf_toc_reloc, /* special_function */
1268 "R_PPC64_TOC16_DS", /* name */
1269 FALSE, /* partial_inplace */
1270 0, /* src_mask */
1271 0xfffc, /* dst_mask */
1272 FALSE), /* pcrel_offset */
1273
1274 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1275 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1276 0, /* rightshift */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1278 16, /* bitsize */
1279 FALSE, /* pc_relative */
1280 0, /* bitpos */
1281 complain_overflow_dont, /* complain_on_overflow */
1282 ppc64_elf_toc_reloc, /* special_function */
1283 "R_PPC64_TOC16_LO_DS", /* name */
1284 FALSE, /* partial_inplace */
1285 0, /* src_mask */
1286 0xfffc, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1288
1289 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1290 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1291 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1292 0, /* rightshift */
1293 1, /* size (0 = byte, 1 = short, 2 = long) */
1294 16, /* bitsize */
1295 FALSE, /* pc_relative */
1296 0, /* bitpos */
1297 complain_overflow_signed, /* complain_on_overflow */
1298 ppc64_elf_unhandled_reloc, /* special_function */
1299 "R_PPC64_PLTGOT16_DS", /* name */
1300 FALSE, /* partial_inplace */
1301 0, /* src_mask */
1302 0xfffc, /* dst_mask */
1303 FALSE), /* pcrel_offset */
1304
1305 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1306 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1307 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1308 0, /* rightshift */
1309 1, /* size (0 = byte, 1 = short, 2 = long) */
1310 16, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 ppc64_elf_unhandled_reloc, /* special_function */
1315 "R_PPC64_PLTGOT16_LO_DS",/* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0xfffc, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
1321 /* Marker relocs for TLS. */
1322 HOWTO (R_PPC64_TLS,
1323 0, /* rightshift */
1324 2, /* size (0 = byte, 1 = short, 2 = long) */
1325 32, /* bitsize */
1326 FALSE, /* pc_relative */
1327 0, /* bitpos */
1328 complain_overflow_dont, /* complain_on_overflow */
1329 bfd_elf_generic_reloc, /* special_function */
1330 "R_PPC64_TLS", /* name */
1331 FALSE, /* partial_inplace */
1332 0, /* src_mask */
1333 0, /* dst_mask */
1334 FALSE), /* pcrel_offset */
1335
1336 HOWTO (R_PPC64_TLSGD,
1337 0, /* rightshift */
1338 2, /* size (0 = byte, 1 = short, 2 = long) */
1339 32, /* bitsize */
1340 FALSE, /* pc_relative */
1341 0, /* bitpos */
1342 complain_overflow_dont, /* complain_on_overflow */
1343 bfd_elf_generic_reloc, /* special_function */
1344 "R_PPC64_TLSGD", /* name */
1345 FALSE, /* partial_inplace */
1346 0, /* src_mask */
1347 0, /* dst_mask */
1348 FALSE), /* pcrel_offset */
1349
1350 HOWTO (R_PPC64_TLSLD,
1351 0, /* rightshift */
1352 2, /* size (0 = byte, 1 = short, 2 = long) */
1353 32, /* bitsize */
1354 FALSE, /* pc_relative */
1355 0, /* bitpos */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 bfd_elf_generic_reloc, /* special_function */
1358 "R_PPC64_TLSLD", /* name */
1359 FALSE, /* partial_inplace */
1360 0, /* src_mask */
1361 0, /* dst_mask */
1362 FALSE), /* pcrel_offset */
1363
1364 /* Marker reloc for optimizing r2 save in prologue rather than on
1365 each plt call stub. */
1366 HOWTO (R_PPC64_TOCSAVE,
1367 0, /* rightshift */
1368 2, /* size (0 = byte, 1 = short, 2 = long) */
1369 32, /* bitsize */
1370 FALSE, /* pc_relative */
1371 0, /* bitpos */
1372 complain_overflow_dont, /* complain_on_overflow */
1373 bfd_elf_generic_reloc, /* special_function */
1374 "R_PPC64_TOCSAVE", /* name */
1375 FALSE, /* partial_inplace */
1376 0, /* src_mask */
1377 0, /* dst_mask */
1378 FALSE), /* pcrel_offset */
1379
1380 /* Marker relocs on inline plt call instructions. */
1381 HOWTO (R_PPC64_PLTSEQ,
1382 0, /* rightshift */
1383 2, /* size (0 = byte, 1 = short, 2 = long) */
1384 32, /* bitsize */
1385 FALSE, /* pc_relative */
1386 0, /* bitpos */
1387 complain_overflow_dont, /* complain_on_overflow */
1388 bfd_elf_generic_reloc, /* special_function */
1389 "R_PPC64_PLTSEQ", /* name */
1390 FALSE, /* partial_inplace */
1391 0, /* src_mask */
1392 0, /* dst_mask */
1393 FALSE), /* pcrel_offset */
1394
1395 HOWTO (R_PPC64_PLTCALL,
1396 0, /* rightshift */
1397 2, /* size (0 = byte, 1 = short, 2 = long) */
1398 32, /* bitsize */
1399 FALSE, /* pc_relative */
1400 0, /* bitpos */
1401 complain_overflow_dont, /* complain_on_overflow */
1402 bfd_elf_generic_reloc, /* special_function */
1403 "R_PPC64_PLTCALL", /* name */
1404 FALSE, /* partial_inplace */
1405 0, /* src_mask */
1406 0, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1408
1409 /* Computes the load module index of the load module that contains the
1410 definition of its TLS sym. */
1411 HOWTO (R_PPC64_DTPMOD64,
1412 0, /* rightshift */
1413 4, /* size (0 = byte, 1 = short, 2 = long) */
1414 64, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_dont, /* complain_on_overflow */
1418 ppc64_elf_unhandled_reloc, /* special_function */
1419 "R_PPC64_DTPMOD64", /* name */
1420 FALSE, /* partial_inplace */
1421 0, /* src_mask */
1422 ONES (64), /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424
1425 /* Computes a dtv-relative displacement, the difference between the value
1426 of sym+add and the base address of the thread-local storage block that
1427 contains the definition of sym, minus 0x8000. */
1428 HOWTO (R_PPC64_DTPREL64,
1429 0, /* rightshift */
1430 4, /* size (0 = byte, 1 = short, 2 = long) */
1431 64, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_dont, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL64", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 ONES (64), /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441
1442 /* A 16 bit dtprel reloc. */
1443 HOWTO (R_PPC64_DTPREL16,
1444 0, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_signed, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1456
1457 /* Like DTPREL16, but no overflow. */
1458 HOWTO (R_PPC64_DTPREL16_LO,
1459 0, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_LO", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1471
1472 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HI,
1474 16, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
1479 complain_overflow_signed, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HI", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486
1487 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HA,
1489 16, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_signed, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501
1502 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1503 HOWTO (R_PPC64_DTPREL16_HIGHER,
1504 32, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_dont, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_HIGHER", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xffff, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1516
1517 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1518 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1519 32, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_HIGHERA", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xffff, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531
1532 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1533 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1534 48, /* rightshift */
1535 1, /* size (0 = byte, 1 = short, 2 = long) */
1536 16, /* bitsize */
1537 FALSE, /* pc_relative */
1538 0, /* bitpos */
1539 complain_overflow_dont, /* complain_on_overflow */
1540 ppc64_elf_unhandled_reloc, /* special_function */
1541 "R_PPC64_DTPREL16_HIGHEST", /* name */
1542 FALSE, /* partial_inplace */
1543 0, /* src_mask */
1544 0xffff, /* dst_mask */
1545 FALSE), /* pcrel_offset */
1546
1547 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1548 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1549 48, /* rightshift */
1550 1, /* size (0 = byte, 1 = short, 2 = long) */
1551 16, /* bitsize */
1552 FALSE, /* pc_relative */
1553 0, /* bitpos */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 ppc64_elf_unhandled_reloc, /* special_function */
1556 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1557 FALSE, /* partial_inplace */
1558 0, /* src_mask */
1559 0xffff, /* dst_mask */
1560 FALSE), /* pcrel_offset */
1561
1562 /* Like DTPREL16, but for insns with a DS field. */
1563 HOWTO (R_PPC64_DTPREL16_DS,
1564 0, /* rightshift */
1565 1, /* size (0 = byte, 1 = short, 2 = long) */
1566 16, /* bitsize */
1567 FALSE, /* pc_relative */
1568 0, /* bitpos */
1569 complain_overflow_signed, /* complain_on_overflow */
1570 ppc64_elf_unhandled_reloc, /* special_function */
1571 "R_PPC64_DTPREL16_DS", /* name */
1572 FALSE, /* partial_inplace */
1573 0, /* src_mask */
1574 0xfffc, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1576
1577 /* Like DTPREL16_DS, but no overflow. */
1578 HOWTO (R_PPC64_DTPREL16_LO_DS,
1579 0, /* rightshift */
1580 1, /* size (0 = byte, 1 = short, 2 = long) */
1581 16, /* bitsize */
1582 FALSE, /* pc_relative */
1583 0, /* bitpos */
1584 complain_overflow_dont, /* complain_on_overflow */
1585 ppc64_elf_unhandled_reloc, /* special_function */
1586 "R_PPC64_DTPREL16_LO_DS", /* name */
1587 FALSE, /* partial_inplace */
1588 0, /* src_mask */
1589 0xfffc, /* dst_mask */
1590 FALSE), /* pcrel_offset */
1591
1592 /* Computes a tp-relative displacement, the difference between the value of
1593 sym+add and the value of the thread pointer (r13). */
1594 HOWTO (R_PPC64_TPREL64,
1595 0, /* rightshift */
1596 4, /* size (0 = byte, 1 = short, 2 = long) */
1597 64, /* bitsize */
1598 FALSE, /* pc_relative */
1599 0, /* bitpos */
1600 complain_overflow_dont, /* complain_on_overflow */
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL64", /* name */
1603 FALSE, /* partial_inplace */
1604 0, /* src_mask */
1605 ONES (64), /* dst_mask */
1606 FALSE), /* pcrel_offset */
1607
1608 /* A 16 bit tprel reloc. */
1609 HOWTO (R_PPC64_TPREL16,
1610 0, /* rightshift */
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1612 16, /* bitsize */
1613 FALSE, /* pc_relative */
1614 0, /* bitpos */
1615 complain_overflow_signed, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16", /* name */
1618 FALSE, /* partial_inplace */
1619 0, /* src_mask */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1622
1623 /* Like TPREL16, but no overflow. */
1624 HOWTO (R_PPC64_TPREL16_LO,
1625 0, /* rightshift */
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1627 16, /* bitsize */
1628 FALSE, /* pc_relative */
1629 0, /* bitpos */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_LO", /* name */
1633 FALSE, /* partial_inplace */
1634 0, /* src_mask */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1637
1638 /* Like TPREL16_LO, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HI,
1640 16, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 16, /* bitsize */
1643 FALSE, /* pc_relative */
1644 0, /* bitpos */
1645 complain_overflow_signed, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HI", /* name */
1648 FALSE, /* partial_inplace */
1649 0, /* src_mask */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1652
1653 /* Like TPREL16_HI, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HA,
1655 16, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_signed, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HA", /* name */
1663 FALSE, /* partial_inplace */
1664 0, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 /* Like TPREL16_HI, but next higher group of 16 bits. */
1669 HOWTO (R_PPC64_TPREL16_HIGHER,
1670 32, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1672 16, /* bitsize */
1673 FALSE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_dont, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_HIGHER", /* name */
1678 FALSE, /* partial_inplace */
1679 0, /* src_mask */
1680 0xffff, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1682
1683 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1684 HOWTO (R_PPC64_TPREL16_HIGHERA,
1685 32, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 16, /* bitsize */
1688 FALSE, /* pc_relative */
1689 0, /* bitpos */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_HIGHERA", /* name */
1693 FALSE, /* partial_inplace */
1694 0, /* src_mask */
1695 0xffff, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1697
1698 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1699 HOWTO (R_PPC64_TPREL16_HIGHEST,
1700 48, /* rightshift */
1701 1, /* size (0 = byte, 1 = short, 2 = long) */
1702 16, /* bitsize */
1703 FALSE, /* pc_relative */
1704 0, /* bitpos */
1705 complain_overflow_dont, /* complain_on_overflow */
1706 ppc64_elf_unhandled_reloc, /* special_function */
1707 "R_PPC64_TPREL16_HIGHEST", /* name */
1708 FALSE, /* partial_inplace */
1709 0, /* src_mask */
1710 0xffff, /* dst_mask */
1711 FALSE), /* pcrel_offset */
1712
1713 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1714 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1715 48, /* rightshift */
1716 1, /* size (0 = byte, 1 = short, 2 = long) */
1717 16, /* bitsize */
1718 FALSE, /* pc_relative */
1719 0, /* bitpos */
1720 complain_overflow_dont, /* complain_on_overflow */
1721 ppc64_elf_unhandled_reloc, /* special_function */
1722 "R_PPC64_TPREL16_HIGHESTA", /* name */
1723 FALSE, /* partial_inplace */
1724 0, /* src_mask */
1725 0xffff, /* dst_mask */
1726 FALSE), /* pcrel_offset */
1727
1728 /* Like TPREL16, but for insns with a DS field. */
1729 HOWTO (R_PPC64_TPREL16_DS,
1730 0, /* rightshift */
1731 1, /* size (0 = byte, 1 = short, 2 = long) */
1732 16, /* bitsize */
1733 FALSE, /* pc_relative */
1734 0, /* bitpos */
1735 complain_overflow_signed, /* complain_on_overflow */
1736 ppc64_elf_unhandled_reloc, /* special_function */
1737 "R_PPC64_TPREL16_DS", /* name */
1738 FALSE, /* partial_inplace */
1739 0, /* src_mask */
1740 0xfffc, /* dst_mask */
1741 FALSE), /* pcrel_offset */
1742
1743 /* Like TPREL16_DS, but no overflow. */
1744 HOWTO (R_PPC64_TPREL16_LO_DS,
1745 0, /* rightshift */
1746 1, /* size (0 = byte, 1 = short, 2 = long) */
1747 16, /* bitsize */
1748 FALSE, /* pc_relative */
1749 0, /* bitpos */
1750 complain_overflow_dont, /* complain_on_overflow */
1751 ppc64_elf_unhandled_reloc, /* special_function */
1752 "R_PPC64_TPREL16_LO_DS", /* name */
1753 FALSE, /* partial_inplace */
1754 0, /* src_mask */
1755 0xfffc, /* dst_mask */
1756 FALSE), /* pcrel_offset */
1757
1758 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1759 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1760 to the first entry relative to the TOC base (r2). */
1761 HOWTO (R_PPC64_GOT_TLSGD16,
1762 0, /* rightshift */
1763 1, /* size (0 = byte, 1 = short, 2 = long) */
1764 16, /* bitsize */
1765 FALSE, /* pc_relative */
1766 0, /* bitpos */
1767 complain_overflow_signed, /* complain_on_overflow */
1768 ppc64_elf_unhandled_reloc, /* special_function */
1769 "R_PPC64_GOT_TLSGD16", /* name */
1770 FALSE, /* partial_inplace */
1771 0, /* src_mask */
1772 0xffff, /* dst_mask */
1773 FALSE), /* pcrel_offset */
1774
1775 /* Like GOT_TLSGD16, but no overflow. */
1776 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1777 0, /* rightshift */
1778 1, /* size (0 = byte, 1 = short, 2 = long) */
1779 16, /* bitsize */
1780 FALSE, /* pc_relative */
1781 0, /* bitpos */
1782 complain_overflow_dont, /* complain_on_overflow */
1783 ppc64_elf_unhandled_reloc, /* special_function */
1784 "R_PPC64_GOT_TLSGD16_LO", /* name */
1785 FALSE, /* partial_inplace */
1786 0, /* src_mask */
1787 0xffff, /* dst_mask */
1788 FALSE), /* pcrel_offset */
1789
1790 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1791 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1792 16, /* rightshift */
1793 1, /* size (0 = byte, 1 = short, 2 = long) */
1794 16, /* bitsize */
1795 FALSE, /* pc_relative */
1796 0, /* bitpos */
1797 complain_overflow_signed, /* complain_on_overflow */
1798 ppc64_elf_unhandled_reloc, /* special_function */
1799 "R_PPC64_GOT_TLSGD16_HI", /* name */
1800 FALSE, /* partial_inplace */
1801 0, /* src_mask */
1802 0xffff, /* dst_mask */
1803 FALSE), /* pcrel_offset */
1804
1805 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1806 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1807 16, /* rightshift */
1808 1, /* size (0 = byte, 1 = short, 2 = long) */
1809 16, /* bitsize */
1810 FALSE, /* pc_relative */
1811 0, /* bitpos */
1812 complain_overflow_signed, /* complain_on_overflow */
1813 ppc64_elf_unhandled_reloc, /* special_function */
1814 "R_PPC64_GOT_TLSGD16_HA", /* name */
1815 FALSE, /* partial_inplace */
1816 0, /* src_mask */
1817 0xffff, /* dst_mask */
1818 FALSE), /* pcrel_offset */
1819
1820 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1821 with values (sym+add)@dtpmod and zero, and computes the offset to the
1822 first entry relative to the TOC base (r2). */
1823 HOWTO (R_PPC64_GOT_TLSLD16,
1824 0, /* rightshift */
1825 1, /* size (0 = byte, 1 = short, 2 = long) */
1826 16, /* bitsize */
1827 FALSE, /* pc_relative */
1828 0, /* bitpos */
1829 complain_overflow_signed, /* complain_on_overflow */
1830 ppc64_elf_unhandled_reloc, /* special_function */
1831 "R_PPC64_GOT_TLSLD16", /* name */
1832 FALSE, /* partial_inplace */
1833 0, /* src_mask */
1834 0xffff, /* dst_mask */
1835 FALSE), /* pcrel_offset */
1836
1837 /* Like GOT_TLSLD16, but no overflow. */
1838 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1839 0, /* rightshift */
1840 1, /* size (0 = byte, 1 = short, 2 = long) */
1841 16, /* bitsize */
1842 FALSE, /* pc_relative */
1843 0, /* bitpos */
1844 complain_overflow_dont, /* complain_on_overflow */
1845 ppc64_elf_unhandled_reloc, /* special_function */
1846 "R_PPC64_GOT_TLSLD16_LO", /* name */
1847 FALSE, /* partial_inplace */
1848 0, /* src_mask */
1849 0xffff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1851
1852 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1853 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1854 16, /* rightshift */
1855 1, /* size (0 = byte, 1 = short, 2 = long) */
1856 16, /* bitsize */
1857 FALSE, /* pc_relative */
1858 0, /* bitpos */
1859 complain_overflow_signed, /* complain_on_overflow */
1860 ppc64_elf_unhandled_reloc, /* special_function */
1861 "R_PPC64_GOT_TLSLD16_HI", /* name */
1862 FALSE, /* partial_inplace */
1863 0, /* src_mask */
1864 0xffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1866
1867 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1868 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1869 16, /* rightshift */
1870 1, /* size (0 = byte, 1 = short, 2 = long) */
1871 16, /* bitsize */
1872 FALSE, /* pc_relative */
1873 0, /* bitpos */
1874 complain_overflow_signed, /* complain_on_overflow */
1875 ppc64_elf_unhandled_reloc, /* special_function */
1876 "R_PPC64_GOT_TLSLD16_HA", /* name */
1877 FALSE, /* partial_inplace */
1878 0, /* src_mask */
1879 0xffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881
1882 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1883 the offset to the entry relative to the TOC base (r2). */
1884 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1885 0, /* rightshift */
1886 1, /* size (0 = byte, 1 = short, 2 = long) */
1887 16, /* bitsize */
1888 FALSE, /* pc_relative */
1889 0, /* bitpos */
1890 complain_overflow_signed, /* complain_on_overflow */
1891 ppc64_elf_unhandled_reloc, /* special_function */
1892 "R_PPC64_GOT_DTPREL16_DS", /* name */
1893 FALSE, /* partial_inplace */
1894 0, /* src_mask */
1895 0xfffc, /* dst_mask */
1896 FALSE), /* pcrel_offset */
1897
1898 /* Like GOT_DTPREL16_DS, but no overflow. */
1899 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1900 0, /* rightshift */
1901 1, /* size (0 = byte, 1 = short, 2 = long) */
1902 16, /* bitsize */
1903 FALSE, /* pc_relative */
1904 0, /* bitpos */
1905 complain_overflow_dont, /* complain_on_overflow */
1906 ppc64_elf_unhandled_reloc, /* special_function */
1907 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1908 FALSE, /* partial_inplace */
1909 0, /* src_mask */
1910 0xfffc, /* dst_mask */
1911 FALSE), /* pcrel_offset */
1912
1913 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1914 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1915 16, /* rightshift */
1916 1, /* size (0 = byte, 1 = short, 2 = long) */
1917 16, /* bitsize */
1918 FALSE, /* pc_relative */
1919 0, /* bitpos */
1920 complain_overflow_signed, /* complain_on_overflow */
1921 ppc64_elf_unhandled_reloc, /* special_function */
1922 "R_PPC64_GOT_DTPREL16_HI", /* name */
1923 FALSE, /* partial_inplace */
1924 0, /* src_mask */
1925 0xffff, /* dst_mask */
1926 FALSE), /* pcrel_offset */
1927
1928 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1929 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1930 16, /* rightshift */
1931 1, /* size (0 = byte, 1 = short, 2 = long) */
1932 16, /* bitsize */
1933 FALSE, /* pc_relative */
1934 0, /* bitpos */
1935 complain_overflow_signed, /* complain_on_overflow */
1936 ppc64_elf_unhandled_reloc, /* special_function */
1937 "R_PPC64_GOT_DTPREL16_HA", /* name */
1938 FALSE, /* partial_inplace */
1939 0, /* src_mask */
1940 0xffff, /* dst_mask */
1941 FALSE), /* pcrel_offset */
1942
1943 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1944 offset to the entry relative to the TOC base (r2). */
1945 HOWTO (R_PPC64_GOT_TPREL16_DS,
1946 0, /* rightshift */
1947 1, /* size (0 = byte, 1 = short, 2 = long) */
1948 16, /* bitsize */
1949 FALSE, /* pc_relative */
1950 0, /* bitpos */
1951 complain_overflow_signed, /* complain_on_overflow */
1952 ppc64_elf_unhandled_reloc, /* special_function */
1953 "R_PPC64_GOT_TPREL16_DS", /* name */
1954 FALSE, /* partial_inplace */
1955 0, /* src_mask */
1956 0xfffc, /* dst_mask */
1957 FALSE), /* pcrel_offset */
1958
1959 /* Like GOT_TPREL16_DS, but no overflow. */
1960 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1961 0, /* rightshift */
1962 1, /* size (0 = byte, 1 = short, 2 = long) */
1963 16, /* bitsize */
1964 FALSE, /* pc_relative */
1965 0, /* bitpos */
1966 complain_overflow_dont, /* complain_on_overflow */
1967 ppc64_elf_unhandled_reloc, /* special_function */
1968 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1969 FALSE, /* partial_inplace */
1970 0, /* src_mask */
1971 0xfffc, /* dst_mask */
1972 FALSE), /* pcrel_offset */
1973
1974 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1975 HOWTO (R_PPC64_GOT_TPREL16_HI,
1976 16, /* rightshift */
1977 1, /* size (0 = byte, 1 = short, 2 = long) */
1978 16, /* bitsize */
1979 FALSE, /* pc_relative */
1980 0, /* bitpos */
1981 complain_overflow_signed, /* complain_on_overflow */
1982 ppc64_elf_unhandled_reloc, /* special_function */
1983 "R_PPC64_GOT_TPREL16_HI", /* name */
1984 FALSE, /* partial_inplace */
1985 0, /* src_mask */
1986 0xffff, /* dst_mask */
1987 FALSE), /* pcrel_offset */
1988
1989 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1990 HOWTO (R_PPC64_GOT_TPREL16_HA,
1991 16, /* rightshift */
1992 1, /* size (0 = byte, 1 = short, 2 = long) */
1993 16, /* bitsize */
1994 FALSE, /* pc_relative */
1995 0, /* bitpos */
1996 complain_overflow_signed, /* complain_on_overflow */
1997 ppc64_elf_unhandled_reloc, /* special_function */
1998 "R_PPC64_GOT_TPREL16_HA", /* name */
1999 FALSE, /* partial_inplace */
2000 0, /* src_mask */
2001 0xffff, /* dst_mask */
2002 FALSE), /* pcrel_offset */
2003
2004 HOWTO (R_PPC64_JMP_IREL, /* type */
2005 0, /* rightshift */
2006 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2007 0, /* bitsize */
2008 FALSE, /* pc_relative */
2009 0, /* bitpos */
2010 complain_overflow_dont, /* complain_on_overflow */
2011 ppc64_elf_unhandled_reloc, /* special_function */
2012 "R_PPC64_JMP_IREL", /* name */
2013 FALSE, /* partial_inplace */
2014 0, /* src_mask */
2015 0, /* dst_mask */
2016 FALSE), /* pcrel_offset */
2017
2018 HOWTO (R_PPC64_IRELATIVE, /* type */
2019 0, /* rightshift */
2020 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2021 64, /* bitsize */
2022 FALSE, /* pc_relative */
2023 0, /* bitpos */
2024 complain_overflow_dont, /* complain_on_overflow */
2025 bfd_elf_generic_reloc, /* special_function */
2026 "R_PPC64_IRELATIVE", /* name */
2027 FALSE, /* partial_inplace */
2028 0, /* src_mask */
2029 ONES (64), /* dst_mask */
2030 FALSE), /* pcrel_offset */
2031
2032 /* A 16 bit relative relocation. */
2033 HOWTO (R_PPC64_REL16, /* type */
2034 0, /* rightshift */
2035 1, /* size (0 = byte, 1 = short, 2 = long) */
2036 16, /* bitsize */
2037 TRUE, /* pc_relative */
2038 0, /* bitpos */
2039 complain_overflow_signed, /* complain_on_overflow */
2040 bfd_elf_generic_reloc, /* special_function */
2041 "R_PPC64_REL16", /* name */
2042 FALSE, /* partial_inplace */
2043 0, /* src_mask */
2044 0xffff, /* dst_mask */
2045 TRUE), /* pcrel_offset */
2046
2047 /* A 16 bit relative relocation without overflow. */
2048 HOWTO (R_PPC64_REL16_LO, /* type */
2049 0, /* rightshift */
2050 1, /* size (0 = byte, 1 = short, 2 = long) */
2051 16, /* bitsize */
2052 TRUE, /* pc_relative */
2053 0, /* bitpos */
2054 complain_overflow_dont,/* complain_on_overflow */
2055 bfd_elf_generic_reloc, /* special_function */
2056 "R_PPC64_REL16_LO", /* name */
2057 FALSE, /* partial_inplace */
2058 0, /* src_mask */
2059 0xffff, /* dst_mask */
2060 TRUE), /* pcrel_offset */
2061
2062 /* The high order 16 bits of a relative address. */
2063 HOWTO (R_PPC64_REL16_HI, /* type */
2064 16, /* rightshift */
2065 1, /* size (0 = byte, 1 = short, 2 = long) */
2066 16, /* bitsize */
2067 TRUE, /* pc_relative */
2068 0, /* bitpos */
2069 complain_overflow_signed, /* complain_on_overflow */
2070 bfd_elf_generic_reloc, /* special_function */
2071 "R_PPC64_REL16_HI", /* name */
2072 FALSE, /* partial_inplace */
2073 0, /* src_mask */
2074 0xffff, /* dst_mask */
2075 TRUE), /* pcrel_offset */
2076
2077 /* The high order 16 bits of a relative address, plus 1 if the contents of
2078 the low 16 bits, treated as a signed number, is negative. */
2079 HOWTO (R_PPC64_REL16_HA, /* type */
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2082 16, /* bitsize */
2083 TRUE, /* pc_relative */
2084 0, /* bitpos */
2085 complain_overflow_signed, /* complain_on_overflow */
2086 ppc64_elf_ha_reloc, /* special_function */
2087 "R_PPC64_REL16_HA", /* name */
2088 FALSE, /* partial_inplace */
2089 0, /* src_mask */
2090 0xffff, /* dst_mask */
2091 TRUE), /* pcrel_offset */
2092
2093 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2094 HOWTO (R_PPC64_REL16DX_HA, /* type */
2095 16, /* rightshift */
2096 2, /* size (0 = byte, 1 = short, 2 = long) */
2097 16, /* bitsize */
2098 TRUE, /* pc_relative */
2099 0, /* bitpos */
2100 complain_overflow_signed, /* complain_on_overflow */
2101 ppc64_elf_ha_reloc, /* special_function */
2102 "R_PPC64_REL16DX_HA", /* name */
2103 FALSE, /* partial_inplace */
2104 0, /* src_mask */
2105 0x1fffc1, /* dst_mask */
2106 TRUE), /* pcrel_offset */
2107
2108 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2109 HOWTO (R_PPC64_16DX_HA, /* type */
2110 16, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 16, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_signed, /* complain_on_overflow */
2116 ppc64_elf_ha_reloc, /* special_function */
2117 "R_PPC64_16DX_HA", /* name */
2118 FALSE, /* partial_inplace */
2119 0, /* src_mask */
2120 0x1fffc1, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122
2123 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2124 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2127 16, /* bitsize */
2128 FALSE, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 bfd_elf_generic_reloc, /* special_function */
2132 "R_PPC64_ADDR16_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2134 0, /* src_mask */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137
2138 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2139 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2142 16, /* bitsize */
2143 FALSE, /* pc_relative */
2144 0, /* bitpos */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_ha_reloc, /* special_function */
2147 "R_PPC64_ADDR16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2149 0, /* src_mask */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2152
2153 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2154 HOWTO (R_PPC64_DTPREL16_HIGH,
2155 16, /* rightshift */
2156 1, /* size (0 = byte, 1 = short, 2 = long) */
2157 16, /* bitsize */
2158 FALSE, /* pc_relative */
2159 0, /* bitpos */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 ppc64_elf_unhandled_reloc, /* special_function */
2162 "R_PPC64_DTPREL16_HIGH", /* name */
2163 FALSE, /* partial_inplace */
2164 0, /* src_mask */
2165 0xffff, /* dst_mask */
2166 FALSE), /* pcrel_offset */
2167
2168 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2169 HOWTO (R_PPC64_DTPREL16_HIGHA,
2170 16, /* rightshift */
2171 1, /* size (0 = byte, 1 = short, 2 = long) */
2172 16, /* bitsize */
2173 FALSE, /* pc_relative */
2174 0, /* bitpos */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 ppc64_elf_unhandled_reloc, /* special_function */
2177 "R_PPC64_DTPREL16_HIGHA", /* name */
2178 FALSE, /* partial_inplace */
2179 0, /* src_mask */
2180 0xffff, /* dst_mask */
2181 FALSE), /* pcrel_offset */
2182
2183 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2184 HOWTO (R_PPC64_TPREL16_HIGH,
2185 16, /* rightshift */
2186 1, /* size (0 = byte, 1 = short, 2 = long) */
2187 16, /* bitsize */
2188 FALSE, /* pc_relative */
2189 0, /* bitpos */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 ppc64_elf_unhandled_reloc, /* special_function */
2192 "R_PPC64_TPREL16_HIGH", /* name */
2193 FALSE, /* partial_inplace */
2194 0, /* src_mask */
2195 0xffff, /* dst_mask */
2196 FALSE), /* pcrel_offset */
2197
2198 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2199 HOWTO (R_PPC64_TPREL16_HIGHA,
2200 16, /* rightshift */
2201 1, /* size (0 = byte, 1 = short, 2 = long) */
2202 16, /* bitsize */
2203 FALSE, /* pc_relative */
2204 0, /* bitpos */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 ppc64_elf_unhandled_reloc, /* special_function */
2207 "R_PPC64_TPREL16_HIGHA", /* name */
2208 FALSE, /* partial_inplace */
2209 0, /* src_mask */
2210 0xffff, /* dst_mask */
2211 FALSE), /* pcrel_offset */
2212
2213 /* Marker reloc on ELFv2 large-model function entry. */
2214 HOWTO (R_PPC64_ENTRY,
2215 0, /* rightshift */
2216 2, /* size (0 = byte, 1 = short, 2 = long) */
2217 32, /* bitsize */
2218 FALSE, /* pc_relative */
2219 0, /* bitpos */
2220 complain_overflow_dont, /* complain_on_overflow */
2221 bfd_elf_generic_reloc, /* special_function */
2222 "R_PPC64_ENTRY", /* name */
2223 FALSE, /* partial_inplace */
2224 0, /* src_mask */
2225 0, /* dst_mask */
2226 FALSE), /* pcrel_offset */
2227
2228 /* Like ADDR64, but use local entry point of function. */
2229 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2230 0, /* rightshift */
2231 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2232 64, /* bitsize */
2233 FALSE, /* pc_relative */
2234 0, /* bitpos */
2235 complain_overflow_dont, /* complain_on_overflow */
2236 bfd_elf_generic_reloc, /* special_function */
2237 "R_PPC64_ADDR64_LOCAL", /* name */
2238 FALSE, /* partial_inplace */
2239 0, /* src_mask */
2240 ONES (64), /* dst_mask */
2241 FALSE), /* pcrel_offset */
2242
2243 /* GNU extension to record C++ vtable hierarchy. */
2244 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2245 0, /* rightshift */
2246 0, /* size (0 = byte, 1 = short, 2 = long) */
2247 0, /* bitsize */
2248 FALSE, /* pc_relative */
2249 0, /* bitpos */
2250 complain_overflow_dont, /* complain_on_overflow */
2251 NULL, /* special_function */
2252 "R_PPC64_GNU_VTINHERIT", /* name */
2253 FALSE, /* partial_inplace */
2254 0, /* src_mask */
2255 0, /* dst_mask */
2256 FALSE), /* pcrel_offset */
2257
2258 /* GNU extension to record C++ vtable member usage. */
2259 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2260 0, /* rightshift */
2261 0, /* size (0 = byte, 1 = short, 2 = long) */
2262 0, /* bitsize */
2263 FALSE, /* pc_relative */
2264 0, /* bitpos */
2265 complain_overflow_dont, /* complain_on_overflow */
2266 NULL, /* special_function */
2267 "R_PPC64_GNU_VTENTRY", /* name */
2268 FALSE, /* partial_inplace */
2269 0, /* src_mask */
2270 0, /* dst_mask */
2271 FALSE), /* pcrel_offset */
2272 };
2273
2274 \f
2275 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2276 be done. */
2277
2278 static void
2279 ppc_howto_init (void)
2280 {
2281 unsigned int i, type;
2282
2283 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2284 {
2285 type = ppc64_elf_howto_raw[i].type;
2286 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2287 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2288 }
2289 }
2290
2291 static reloc_howto_type *
2292 ppc64_elf_reloc_type_lookup (bfd *abfd,
2293 bfd_reloc_code_real_type code)
2294 {
2295 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2296
2297 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2298 /* Initialize howto table if needed. */
2299 ppc_howto_init ();
2300
2301 switch (code)
2302 {
2303 default:
2304 /* xgettext:c-format */
2305 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2306 bfd_set_error (bfd_error_bad_value);
2307 return NULL;
2308
2309 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2310 break;
2311 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2312 break;
2313 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2314 break;
2315 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2316 break;
2317 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2318 break;
2319 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2320 break;
2321 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2322 break;
2323 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2324 break;
2325 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2326 break;
2327 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2328 break;
2329 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2330 break;
2331 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2332 break;
2333 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2334 break;
2335 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
2336 break;
2337 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2338 break;
2339 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2340 break;
2341 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2342 break;
2343 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2344 break;
2345 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2346 break;
2347 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2348 break;
2349 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2350 break;
2351 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2352 break;
2353 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2354 break;
2355 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2356 break;
2357 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2358 break;
2359 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2360 break;
2361 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2362 break;
2363 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2364 break;
2365 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2366 break;
2367 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2368 break;
2369 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2370 break;
2371 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2372 break;
2373 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2374 break;
2375 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2376 break;
2377 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2378 break;
2379 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2380 break;
2381 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2382 break;
2383 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2384 break;
2385 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2386 break;
2387 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2388 break;
2389 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2390 break;
2391 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2392 break;
2393 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2394 break;
2395 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2396 break;
2397 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2398 break;
2399 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2400 break;
2401 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2402 break;
2403 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2404 break;
2405 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2406 break;
2407 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2408 break;
2409 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2410 break;
2411 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2412 break;
2413 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2414 break;
2415 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2416 break;
2417 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2418 break;
2419 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2420 break;
2421 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2422 break;
2423 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2424 break;
2425 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2426 break;
2427 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2428 break;
2429 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2430 break;
2431 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2432 break;
2433 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2434 break;
2435 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2436 break;
2437 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2438 break;
2439 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2440 break;
2441 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2442 break;
2443 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2444 break;
2445 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2446 break;
2447 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2448 break;
2449 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2450 break;
2451 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2452 break;
2453 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2454 break;
2455 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2456 break;
2457 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2458 break;
2459 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2460 break;
2461 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2462 break;
2463 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2464 break;
2465 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2466 break;
2467 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2468 break;
2469 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2470 break;
2471 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2472 break;
2473 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2474 break;
2475 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2476 break;
2477 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2478 break;
2479 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2480 break;
2481 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2482 break;
2483 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2484 break;
2485 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2486 break;
2487 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2488 break;
2489 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2490 break;
2491 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2492 break;
2493 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2494 break;
2495 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2496 break;
2497 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2498 break;
2499 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2500 break;
2501 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2502 break;
2503 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2504 break;
2505 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2506 break;
2507 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2508 break;
2509 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2510 break;
2511 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2512 break;
2513 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2514 break;
2515 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2516 break;
2517 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2518 break;
2519 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2520 break;
2521 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2522 break;
2523 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2524 break;
2525 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2526 break;
2527 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2528 break;
2529 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2530 break;
2531 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2532 break;
2533 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2534 break;
2535 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2536 break;
2537 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2538 break;
2539 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2540 break;
2541 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2542 break;
2543 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2544 break;
2545 }
2546
2547 return ppc64_elf_howto_table[r];
2548 };
2549
2550 static reloc_howto_type *
2551 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2552 const char *r_name)
2553 {
2554 unsigned int i;
2555
2556 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2557 if (ppc64_elf_howto_raw[i].name != NULL
2558 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2559 return &ppc64_elf_howto_raw[i];
2560
2561
2562 return NULL;
2563 }
2564
2565 /* Set the howto pointer for a PowerPC ELF reloc. */
2566
2567 static bfd_boolean
2568 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2569 Elf_Internal_Rela *dst)
2570 {
2571 unsigned int type;
2572
2573 /* Initialize howto table if needed. */
2574 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2575 ppc_howto_init ();
2576
2577 type = ELF64_R_TYPE (dst->r_info);
2578 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2579 {
2580 /* xgettext:c-format */
2581 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2582 abfd, type);
2583 bfd_set_error (bfd_error_bad_value);
2584 return FALSE;
2585 }
2586 cache_ptr->howto = ppc64_elf_howto_table[type];
2587 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2588 {
2589 /* xgettext:c-format */
2590 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2591 abfd, type);
2592 bfd_set_error (bfd_error_bad_value);
2593 return FALSE;
2594 }
2595
2596 return TRUE;
2597 }
2598
2599 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2600
2601 static bfd_reloc_status_type
2602 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2603 void *data, asection *input_section,
2604 bfd *output_bfd, char **error_message)
2605 {
2606 enum elf_ppc64_reloc_type r_type;
2607 long insn;
2608 bfd_size_type octets;
2609 bfd_vma value;
2610
2611 /* If this is a relocatable link (output_bfd test tells us), just
2612 call the generic function. Any adjustment will be done at final
2613 link time. */
2614 if (output_bfd != NULL)
2615 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2616 input_section, output_bfd, error_message);
2617
2618 /* Adjust the addend for sign extension of the low 16 bits.
2619 We won't actually be using the low 16 bits, so trashing them
2620 doesn't matter. */
2621 reloc_entry->addend += 0x8000;
2622 r_type = reloc_entry->howto->type;
2623 if (r_type != R_PPC64_REL16DX_HA)
2624 return bfd_reloc_continue;
2625
2626 value = 0;
2627 if (!bfd_is_com_section (symbol->section))
2628 value = symbol->value;
2629 value += (reloc_entry->addend
2630 + symbol->section->output_offset
2631 + symbol->section->output_section->vma);
2632 value -= (reloc_entry->address
2633 + input_section->output_offset
2634 + input_section->output_section->vma);
2635 value = (bfd_signed_vma) value >> 16;
2636
2637 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2638 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2639 insn &= ~0x1fffc1;
2640 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2641 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2642 if (value + 0x8000 > 0xffff)
2643 return bfd_reloc_overflow;
2644 return bfd_reloc_ok;
2645 }
2646
2647 static bfd_reloc_status_type
2648 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2649 void *data, asection *input_section,
2650 bfd *output_bfd, char **error_message)
2651 {
2652 if (output_bfd != NULL)
2653 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2654 input_section, output_bfd, error_message);
2655
2656 if (strcmp (symbol->section->name, ".opd") == 0
2657 && (symbol->section->owner->flags & DYNAMIC) == 0)
2658 {
2659 bfd_vma dest = opd_entry_value (symbol->section,
2660 symbol->value + reloc_entry->addend,
2661 NULL, NULL, FALSE);
2662 if (dest != (bfd_vma) -1)
2663 reloc_entry->addend = dest - (symbol->value
2664 + symbol->section->output_section->vma
2665 + symbol->section->output_offset);
2666 }
2667 else
2668 {
2669 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2670
2671 if (symbol->section->owner != abfd
2672 && symbol->section->owner != NULL
2673 && abiversion (symbol->section->owner) >= 2)
2674 {
2675 unsigned int i;
2676
2677 for (i = 0; i < symbol->section->owner->symcount; ++i)
2678 {
2679 asymbol *symdef = symbol->section->owner->outsymbols[i];
2680
2681 if (strcmp (symdef->name, symbol->name) == 0)
2682 {
2683 elfsym = (elf_symbol_type *) symdef;
2684 break;
2685 }
2686 }
2687 }
2688 reloc_entry->addend
2689 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2690 }
2691 return bfd_reloc_continue;
2692 }
2693
2694 static bfd_reloc_status_type
2695 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2696 void *data, asection *input_section,
2697 bfd *output_bfd, char **error_message)
2698 {
2699 long insn;
2700 enum elf_ppc64_reloc_type r_type;
2701 bfd_size_type octets;
2702 /* Assume 'at' branch hints. */
2703 bfd_boolean is_isa_v2 = TRUE;
2704
2705 /* If this is a relocatable link (output_bfd test tells us), just
2706 call the generic function. Any adjustment will be done at final
2707 link time. */
2708 if (output_bfd != NULL)
2709 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2710 input_section, output_bfd, error_message);
2711
2712 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2713 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2714 insn &= ~(0x01 << 21);
2715 r_type = reloc_entry->howto->type;
2716 if (r_type == R_PPC64_ADDR14_BRTAKEN
2717 || r_type == R_PPC64_REL14_BRTAKEN)
2718 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2719
2720 if (is_isa_v2)
2721 {
2722 /* Set 'a' bit. This is 0b00010 in BO field for branch
2723 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2724 for branch on CTR insns (BO == 1a00t or 1a01t). */
2725 if ((insn & (0x14 << 21)) == (0x04 << 21))
2726 insn |= 0x02 << 21;
2727 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2728 insn |= 0x08 << 21;
2729 else
2730 goto out;
2731 }
2732 else
2733 {
2734 bfd_vma target = 0;
2735 bfd_vma from;
2736
2737 if (!bfd_is_com_section (symbol->section))
2738 target = symbol->value;
2739 target += symbol->section->output_section->vma;
2740 target += symbol->section->output_offset;
2741 target += reloc_entry->addend;
2742
2743 from = (reloc_entry->address
2744 + input_section->output_offset
2745 + input_section->output_section->vma);
2746
2747 /* Invert 'y' bit if not the default. */
2748 if ((bfd_signed_vma) (target - from) < 0)
2749 insn ^= 0x01 << 21;
2750 }
2751 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2752 out:
2753 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2754 input_section, output_bfd, error_message);
2755 }
2756
2757 static bfd_reloc_status_type
2758 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2759 void *data, asection *input_section,
2760 bfd *output_bfd, char **error_message)
2761 {
2762 /* If this is a relocatable link (output_bfd test tells us), just
2763 call the generic function. Any adjustment will be done at final
2764 link time. */
2765 if (output_bfd != NULL)
2766 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2767 input_section, output_bfd, error_message);
2768
2769 /* Subtract the symbol section base address. */
2770 reloc_entry->addend -= symbol->section->output_section->vma;
2771 return bfd_reloc_continue;
2772 }
2773
2774 static bfd_reloc_status_type
2775 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2776 void *data, asection *input_section,
2777 bfd *output_bfd, char **error_message)
2778 {
2779 /* If this is a relocatable link (output_bfd test tells us), just
2780 call the generic function. Any adjustment will be done at final
2781 link time. */
2782 if (output_bfd != NULL)
2783 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2784 input_section, output_bfd, error_message);
2785
2786 /* Subtract the symbol section base address. */
2787 reloc_entry->addend -= symbol->section->output_section->vma;
2788
2789 /* Adjust the addend for sign extension of the low 16 bits. */
2790 reloc_entry->addend += 0x8000;
2791 return bfd_reloc_continue;
2792 }
2793
2794 static bfd_reloc_status_type
2795 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2796 void *data, asection *input_section,
2797 bfd *output_bfd, char **error_message)
2798 {
2799 bfd_vma TOCstart;
2800
2801 /* If this is a relocatable link (output_bfd test tells us), just
2802 call the generic function. Any adjustment will be done at final
2803 link time. */
2804 if (output_bfd != NULL)
2805 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2806 input_section, output_bfd, error_message);
2807
2808 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2809 if (TOCstart == 0)
2810 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2811
2812 /* Subtract the TOC base address. */
2813 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2814 return bfd_reloc_continue;
2815 }
2816
2817 static bfd_reloc_status_type
2818 ppc64_elf_toc_ha_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
2824 /* If this is a relocatable link (output_bfd test tells us), just
2825 call the generic function. Any adjustment will be done at final
2826 link time. */
2827 if (output_bfd != NULL)
2828 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2829 input_section, output_bfd, error_message);
2830
2831 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2832 if (TOCstart == 0)
2833 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2834
2835 /* Subtract the TOC base address. */
2836 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2837
2838 /* Adjust the addend for sign extension of the low 16 bits. */
2839 reloc_entry->addend += 0x8000;
2840 return bfd_reloc_continue;
2841 }
2842
2843 static bfd_reloc_status_type
2844 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2845 void *data, asection *input_section,
2846 bfd *output_bfd, char **error_message)
2847 {
2848 bfd_vma TOCstart;
2849 bfd_size_type octets;
2850
2851 /* If this is a relocatable link (output_bfd test tells us), just
2852 call the generic function. Any adjustment will be done at final
2853 link time. */
2854 if (output_bfd != NULL)
2855 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2856 input_section, output_bfd, error_message);
2857
2858 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2859 if (TOCstart == 0)
2860 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2861
2862 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2863 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2864 return bfd_reloc_ok;
2865 }
2866
2867 static bfd_reloc_status_type
2868 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2869 void *data, asection *input_section,
2870 bfd *output_bfd, char **error_message)
2871 {
2872 /* If this is a relocatable link (output_bfd test tells us), just
2873 call the generic function. Any adjustment will be done at final
2874 link time. */
2875 if (output_bfd != NULL)
2876 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2877 input_section, output_bfd, error_message);
2878
2879 if (error_message != NULL)
2880 {
2881 static char buf[60];
2882 sprintf (buf, "generic linker can't handle %s",
2883 reloc_entry->howto->name);
2884 *error_message = buf;
2885 }
2886 return bfd_reloc_dangerous;
2887 }
2888
2889 /* Track GOT entries needed for a given symbol. We might need more
2890 than one got entry per symbol. */
2891 struct got_entry
2892 {
2893 struct got_entry *next;
2894
2895 /* The symbol addend that we'll be placing in the GOT. */
2896 bfd_vma addend;
2897
2898 /* Unlike other ELF targets, we use separate GOT entries for the same
2899 symbol referenced from different input files. This is to support
2900 automatic multiple TOC/GOT sections, where the TOC base can vary
2901 from one input file to another. After partitioning into TOC groups
2902 we merge entries within the group.
2903
2904 Point to the BFD owning this GOT entry. */
2905 bfd *owner;
2906
2907 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2908 TLS_TPREL or TLS_DTPREL for tls entries. */
2909 unsigned char tls_type;
2910
2911 /* Non-zero if got.ent points to real entry. */
2912 unsigned char is_indirect;
2913
2914 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2915 union
2916 {
2917 bfd_signed_vma refcount;
2918 bfd_vma offset;
2919 struct got_entry *ent;
2920 } got;
2921 };
2922
2923 /* The same for PLT. */
2924 struct plt_entry
2925 {
2926 struct plt_entry *next;
2927
2928 bfd_vma addend;
2929
2930 union
2931 {
2932 bfd_signed_vma refcount;
2933 bfd_vma offset;
2934 } plt;
2935 };
2936
2937 struct ppc64_elf_obj_tdata
2938 {
2939 struct elf_obj_tdata elf;
2940
2941 /* Shortcuts to dynamic linker sections. */
2942 asection *got;
2943 asection *relgot;
2944
2945 /* Used during garbage collection. We attach global symbols defined
2946 on removed .opd entries to this section so that the sym is removed. */
2947 asection *deleted_section;
2948
2949 /* TLS local dynamic got entry handling. Support for multiple GOT
2950 sections means we potentially need one of these for each input bfd. */
2951 struct got_entry tlsld_got;
2952
2953 union {
2954 /* A copy of relocs before they are modified for --emit-relocs. */
2955 Elf_Internal_Rela *relocs;
2956
2957 /* Section contents. */
2958 bfd_byte *contents;
2959 } opd;
2960
2961 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2962 the reloc to be in the range -32768 to 32767. */
2963 unsigned int has_small_toc_reloc : 1;
2964
2965 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2966 instruction not one we handle. */
2967 unsigned int unexpected_toc_insn : 1;
2968 };
2969
2970 #define ppc64_elf_tdata(bfd) \
2971 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2972
2973 #define ppc64_tlsld_got(bfd) \
2974 (&ppc64_elf_tdata (bfd)->tlsld_got)
2975
2976 #define is_ppc64_elf(bfd) \
2977 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2978 && elf_object_id (bfd) == PPC64_ELF_DATA)
2979
2980 /* Override the generic function because we store some extras. */
2981
2982 static bfd_boolean
2983 ppc64_elf_mkobject (bfd *abfd)
2984 {
2985 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2986 PPC64_ELF_DATA);
2987 }
2988
2989 /* Fix bad default arch selected for a 64 bit input bfd when the
2990 default is 32 bit. Also select arch based on apuinfo. */
2991
2992 static bfd_boolean
2993 ppc64_elf_object_p (bfd *abfd)
2994 {
2995 if (!abfd->arch_info->the_default)
2996 return TRUE;
2997
2998 if (abfd->arch_info->bits_per_word == 32)
2999 {
3000 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
3001
3002 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
3003 {
3004 /* Relies on arch after 32 bit default being 64 bit default. */
3005 abfd->arch_info = abfd->arch_info->next;
3006 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
3007 }
3008 }
3009 return _bfd_elf_ppc_set_arch (abfd);
3010 }
3011
3012 /* Support for core dump NOTE sections. */
3013
3014 static bfd_boolean
3015 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3016 {
3017 size_t offset, size;
3018
3019 if (note->descsz != 504)
3020 return FALSE;
3021
3022 /* pr_cursig */
3023 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3024
3025 /* pr_pid */
3026 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3027
3028 /* pr_reg */
3029 offset = 112;
3030 size = 384;
3031
3032 /* Make a ".reg/999" section. */
3033 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3034 size, note->descpos + offset);
3035 }
3036
3037 static bfd_boolean
3038 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3039 {
3040 if (note->descsz != 136)
3041 return FALSE;
3042
3043 elf_tdata (abfd)->core->pid
3044 = bfd_get_32 (abfd, note->descdata + 24);
3045 elf_tdata (abfd)->core->program
3046 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3047 elf_tdata (abfd)->core->command
3048 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3049
3050 return TRUE;
3051 }
3052
3053 static char *
3054 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3055 ...)
3056 {
3057 switch (note_type)
3058 {
3059 default:
3060 return NULL;
3061
3062 case NT_PRPSINFO:
3063 {
3064 char data[136] ATTRIBUTE_NONSTRING;
3065 va_list ap;
3066
3067 va_start (ap, note_type);
3068 memset (data, 0, sizeof (data));
3069 strncpy (data + 40, va_arg (ap, const char *), 16);
3070 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3071 DIAGNOSTIC_PUSH;
3072 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
3073 -Wstringop-truncation:
3074 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3075 */
3076 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3077 #endif
3078 strncpy (data + 56, va_arg (ap, const char *), 80);
3079 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3080 DIAGNOSTIC_POP;
3081 #endif
3082 va_end (ap);
3083 return elfcore_write_note (abfd, buf, bufsiz,
3084 "CORE", note_type, data, sizeof (data));
3085 }
3086
3087 case NT_PRSTATUS:
3088 {
3089 char data[504];
3090 va_list ap;
3091 long pid;
3092 int cursig;
3093 const void *greg;
3094
3095 va_start (ap, note_type);
3096 memset (data, 0, 112);
3097 pid = va_arg (ap, long);
3098 bfd_put_32 (abfd, pid, data + 32);
3099 cursig = va_arg (ap, int);
3100 bfd_put_16 (abfd, cursig, data + 12);
3101 greg = va_arg (ap, const void *);
3102 memcpy (data + 112, greg, 384);
3103 memset (data + 496, 0, 8);
3104 va_end (ap);
3105 return elfcore_write_note (abfd, buf, bufsiz,
3106 "CORE", note_type, data, sizeof (data));
3107 }
3108 }
3109 }
3110
3111 /* Add extra PPC sections. */
3112
3113 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3114 {
3115 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3116 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3117 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3118 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3119 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3120 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3121 { NULL, 0, 0, 0, 0 }
3122 };
3123
3124 enum _ppc64_sec_type {
3125 sec_normal = 0,
3126 sec_opd = 1,
3127 sec_toc = 2
3128 };
3129
3130 struct _ppc64_elf_section_data
3131 {
3132 struct bfd_elf_section_data elf;
3133
3134 union
3135 {
3136 /* An array with one entry for each opd function descriptor,
3137 and some spares since opd entries may be either 16 or 24 bytes. */
3138 #define OPD_NDX(OFF) ((OFF) >> 4)
3139 struct _opd_sec_data
3140 {
3141 /* Points to the function code section for local opd entries. */
3142 asection **func_sec;
3143
3144 /* After editing .opd, adjust references to opd local syms. */
3145 long *adjust;
3146 } opd;
3147
3148 /* An array for toc sections, indexed by offset/8. */
3149 struct _toc_sec_data
3150 {
3151 /* Specifies the relocation symbol index used at a given toc offset. */
3152 unsigned *symndx;
3153
3154 /* And the relocation addend. */
3155 bfd_vma *add;
3156 } toc;
3157 } u;
3158
3159 enum _ppc64_sec_type sec_type:2;
3160
3161 /* Flag set when small branches are detected. Used to
3162 select suitable defaults for the stub group size. */
3163 unsigned int has_14bit_branch:1;
3164
3165 /* Flag set when PLTCALL relocs are detected. */
3166 unsigned int has_pltcall:1;
3167 };
3168
3169 #define ppc64_elf_section_data(sec) \
3170 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3171
3172 static bfd_boolean
3173 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3174 {
3175 if (!sec->used_by_bfd)
3176 {
3177 struct _ppc64_elf_section_data *sdata;
3178 bfd_size_type amt = sizeof (*sdata);
3179
3180 sdata = bfd_zalloc (abfd, amt);
3181 if (sdata == NULL)
3182 return FALSE;
3183 sec->used_by_bfd = sdata;
3184 }
3185
3186 return _bfd_elf_new_section_hook (abfd, sec);
3187 }
3188
3189 static struct _opd_sec_data *
3190 get_opd_info (asection * sec)
3191 {
3192 if (sec != NULL
3193 && ppc64_elf_section_data (sec) != NULL
3194 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3195 return &ppc64_elf_section_data (sec)->u.opd;
3196 return NULL;
3197 }
3198 \f
3199 /* Parameters for the qsort hook. */
3200 static bfd_boolean synthetic_relocatable;
3201 static asection *synthetic_opd;
3202
3203 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3204
3205 static int
3206 compare_symbols (const void *ap, const void *bp)
3207 {
3208 const asymbol *a = * (const asymbol **) ap;
3209 const asymbol *b = * (const asymbol **) bp;
3210
3211 /* Section symbols first. */
3212 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3213 return -1;
3214 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3215 return 1;
3216
3217 /* then .opd symbols. */
3218 if (synthetic_opd != NULL)
3219 {
3220 if (strcmp (a->section->name, ".opd") == 0
3221 && strcmp (b->section->name, ".opd") != 0)
3222 return -1;
3223 if (strcmp (a->section->name, ".opd") != 0
3224 && strcmp (b->section->name, ".opd") == 0)
3225 return 1;
3226 }
3227
3228 /* then other code symbols. */
3229 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3230 == (SEC_CODE | SEC_ALLOC)
3231 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3232 != (SEC_CODE | SEC_ALLOC))
3233 return -1;
3234
3235 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3236 != (SEC_CODE | SEC_ALLOC)
3237 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3238 == (SEC_CODE | SEC_ALLOC))
3239 return 1;
3240
3241 if (synthetic_relocatable)
3242 {
3243 if (a->section->id < b->section->id)
3244 return -1;
3245
3246 if (a->section->id > b->section->id)
3247 return 1;
3248 }
3249
3250 if (a->value + a->section->vma < b->value + b->section->vma)
3251 return -1;
3252
3253 if (a->value + a->section->vma > b->value + b->section->vma)
3254 return 1;
3255
3256 /* For syms with the same value, prefer strong dynamic global function
3257 syms over other syms. */
3258 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3259 return -1;
3260
3261 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3262 return 1;
3263
3264 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3265 return -1;
3266
3267 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3268 return 1;
3269
3270 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3271 return -1;
3272
3273 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3274 return 1;
3275
3276 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3277 return -1;
3278
3279 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3280 return 1;
3281
3282 return a > b;
3283 }
3284
3285 /* Search SYMS for a symbol of the given VALUE. */
3286
3287 static asymbol *
3288 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3289 {
3290 long mid;
3291
3292 if (id == (unsigned) -1)
3293 {
3294 while (lo < hi)
3295 {
3296 mid = (lo + hi) >> 1;
3297 if (syms[mid]->value + syms[mid]->section->vma < value)
3298 lo = mid + 1;
3299 else if (syms[mid]->value + syms[mid]->section->vma > value)
3300 hi = mid;
3301 else
3302 return syms[mid];
3303 }
3304 }
3305 else
3306 {
3307 while (lo < hi)
3308 {
3309 mid = (lo + hi) >> 1;
3310 if (syms[mid]->section->id < id)
3311 lo = mid + 1;
3312 else if (syms[mid]->section->id > id)
3313 hi = mid;
3314 else if (syms[mid]->value < value)
3315 lo = mid + 1;
3316 else if (syms[mid]->value > value)
3317 hi = mid;
3318 else
3319 return syms[mid];
3320 }
3321 }
3322 return NULL;
3323 }
3324
3325 static bfd_boolean
3326 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3327 {
3328 bfd_vma vma = *(bfd_vma *) ptr;
3329 return ((section->flags & SEC_ALLOC) != 0
3330 && section->vma <= vma
3331 && vma < section->vma + section->size);
3332 }
3333
3334 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3335 entry syms. Also generate @plt symbols for the glink branch table.
3336 Returns count of synthetic symbols in RET or -1 on error. */
3337
3338 static long
3339 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3340 long static_count, asymbol **static_syms,
3341 long dyn_count, asymbol **dyn_syms,
3342 asymbol **ret)
3343 {
3344 asymbol *s;
3345 size_t i, j, count;
3346 char *names;
3347 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3348 asection *opd = NULL;
3349 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3350 asymbol **syms;
3351 int abi = abiversion (abfd);
3352
3353 *ret = NULL;
3354
3355 if (abi < 2)
3356 {
3357 opd = bfd_get_section_by_name (abfd, ".opd");
3358 if (opd == NULL && abi == 1)
3359 return 0;
3360 }
3361
3362 syms = NULL;
3363 codesecsym = 0;
3364 codesecsymend = 0;
3365 secsymend = 0;
3366 opdsymend = 0;
3367 symcount = 0;
3368 if (opd != NULL)
3369 {
3370 symcount = static_count;
3371 if (!relocatable)
3372 symcount += dyn_count;
3373 if (symcount == 0)
3374 return 0;
3375
3376 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3377 if (syms == NULL)
3378 return -1;
3379
3380 if (!relocatable && static_count != 0 && dyn_count != 0)
3381 {
3382 /* Use both symbol tables. */
3383 memcpy (syms, static_syms, static_count * sizeof (*syms));
3384 memcpy (syms + static_count, dyn_syms,
3385 (dyn_count + 1) * sizeof (*syms));
3386 }
3387 else if (!relocatable && static_count == 0)
3388 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3389 else
3390 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3391
3392 /* Trim uninteresting symbols. Interesting symbols are section,
3393 function, and notype symbols. */
3394 for (i = 0, j = 0; i < symcount; ++i)
3395 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3396 | BSF_RELC | BSF_SRELC)) == 0)
3397 syms[j++] = syms[i];
3398 symcount = j;
3399
3400 synthetic_relocatable = relocatable;
3401 synthetic_opd = opd;
3402 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3403
3404 if (!relocatable && symcount > 1)
3405 {
3406 /* Trim duplicate syms, since we may have merged the normal
3407 and dynamic symbols. Actually, we only care about syms
3408 that have different values, so trim any with the same
3409 value. Don't consider ifunc and ifunc resolver symbols
3410 duplicates however, because GDB wants to know whether a
3411 text symbol is an ifunc resolver. */
3412 for (i = 1, j = 1; i < symcount; ++i)
3413 {
3414 const asymbol *s0 = syms[i - 1];
3415 const asymbol *s1 = syms[i];
3416
3417 if ((s0->value + s0->section->vma
3418 != s1->value + s1->section->vma)
3419 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3420 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3421 syms[j++] = syms[i];
3422 }
3423 symcount = j;
3424 }
3425
3426 i = 0;
3427 /* Note that here and in compare_symbols we can't compare opd and
3428 sym->section directly. With separate debug info files, the
3429 symbols will be extracted from the debug file while abfd passed
3430 to this function is the real binary. */
3431 if (strcmp (syms[i]->section->name, ".opd") == 0)
3432 ++i;
3433 codesecsym = i;
3434
3435 for (; i < symcount; ++i)
3436 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3437 | SEC_THREAD_LOCAL))
3438 != (SEC_CODE | SEC_ALLOC))
3439 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3440 break;
3441 codesecsymend = i;
3442
3443 for (; i < symcount; ++i)
3444 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3445 break;
3446 secsymend = i;
3447
3448 for (; i < symcount; ++i)
3449 if (strcmp (syms[i]->section->name, ".opd") != 0)
3450 break;
3451 opdsymend = i;
3452
3453 for (; i < symcount; ++i)
3454 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3455 != (SEC_CODE | SEC_ALLOC))
3456 break;
3457 symcount = i;
3458 }
3459 count = 0;
3460
3461 if (relocatable)
3462 {
3463 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3464 arelent *r;
3465 size_t size;
3466 size_t relcount;
3467
3468 if (opdsymend == secsymend)
3469 goto done;
3470
3471 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3472 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3473 if (relcount == 0)
3474 goto done;
3475
3476 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3477 {
3478 count = -1;
3479 goto done;
3480 }
3481
3482 size = 0;
3483 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3484 {
3485 asymbol *sym;
3486
3487 while (r < opd->relocation + relcount
3488 && r->address < syms[i]->value + opd->vma)
3489 ++r;
3490
3491 if (r == opd->relocation + relcount)
3492 break;
3493
3494 if (r->address != syms[i]->value + opd->vma)
3495 continue;
3496
3497 if (r->howto->type != R_PPC64_ADDR64)
3498 continue;
3499
3500 sym = *r->sym_ptr_ptr;
3501 if (!sym_exists_at (syms, opdsymend, symcount,
3502 sym->section->id, sym->value + r->addend))
3503 {
3504 ++count;
3505 size += sizeof (asymbol);
3506 size += strlen (syms[i]->name) + 2;
3507 }
3508 }
3509
3510 if (size == 0)
3511 goto done;
3512 s = *ret = bfd_malloc (size);
3513 if (s == NULL)
3514 {
3515 count = -1;
3516 goto done;
3517 }
3518
3519 names = (char *) (s + count);
3520
3521 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3522 {
3523 asymbol *sym;
3524
3525 while (r < opd->relocation + relcount
3526 && r->address < syms[i]->value + opd->vma)
3527 ++r;
3528
3529 if (r == opd->relocation + relcount)
3530 break;
3531
3532 if (r->address != syms[i]->value + opd->vma)
3533 continue;
3534
3535 if (r->howto->type != R_PPC64_ADDR64)
3536 continue;
3537
3538 sym = *r->sym_ptr_ptr;
3539 if (!sym_exists_at (syms, opdsymend, symcount,
3540 sym->section->id, sym->value + r->addend))
3541 {
3542 size_t len;
3543
3544 *s = *syms[i];
3545 s->flags |= BSF_SYNTHETIC;
3546 s->section = sym->section;
3547 s->value = sym->value + r->addend;
3548 s->name = names;
3549 *names++ = '.';
3550 len = strlen (syms[i]->name);
3551 memcpy (names, syms[i]->name, len + 1);
3552 names += len + 1;
3553 /* Have udata.p point back to the original symbol this
3554 synthetic symbol was derived from. */
3555 s->udata.p = syms[i];
3556 s++;
3557 }
3558 }
3559 }
3560 else
3561 {
3562 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3563 bfd_byte *contents = NULL;
3564 size_t size;
3565 size_t plt_count = 0;
3566 bfd_vma glink_vma = 0, resolv_vma = 0;
3567 asection *dynamic, *glink = NULL, *relplt = NULL;
3568 arelent *p;
3569
3570 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3571 {
3572 free_contents_and_exit_err:
3573 count = -1;
3574 free_contents_and_exit:
3575 if (contents)
3576 free (contents);
3577 goto done;
3578 }
3579
3580 size = 0;
3581 for (i = secsymend; i < opdsymend; ++i)
3582 {
3583 bfd_vma ent;
3584
3585 /* Ignore bogus symbols. */
3586 if (syms[i]->value > opd->size - 8)
3587 continue;
3588
3589 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3590 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3591 {
3592 ++count;
3593 size += sizeof (asymbol);
3594 size += strlen (syms[i]->name) + 2;
3595 }
3596 }
3597
3598 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3599 if (dyn_count != 0
3600 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3601 {
3602 bfd_byte *dynbuf, *extdyn, *extdynend;
3603 size_t extdynsize;
3604 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3605
3606 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3607 goto free_contents_and_exit_err;
3608
3609 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3610 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3611
3612 extdyn = dynbuf;
3613 extdynend = extdyn + dynamic->size;
3614 for (; extdyn < extdynend; extdyn += extdynsize)
3615 {
3616 Elf_Internal_Dyn dyn;
3617 (*swap_dyn_in) (abfd, extdyn, &dyn);
3618
3619 if (dyn.d_tag == DT_NULL)
3620 break;
3621
3622 if (dyn.d_tag == DT_PPC64_GLINK)
3623 {
3624 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3625 See comment in ppc64_elf_finish_dynamic_sections. */
3626 glink_vma = dyn.d_un.d_val + 8 * 4;
3627 /* The .glink section usually does not survive the final
3628 link; search for the section (usually .text) where the
3629 glink stubs now reside. */
3630 glink = bfd_sections_find_if (abfd, section_covers_vma,
3631 &glink_vma);
3632 break;
3633 }
3634 }
3635
3636 free (dynbuf);
3637 }
3638
3639 if (glink != NULL)
3640 {
3641 /* Determine __glink trampoline by reading the relative branch
3642 from the first glink stub. */
3643 bfd_byte buf[4];
3644 unsigned int off = 0;
3645
3646 while (bfd_get_section_contents (abfd, glink, buf,
3647 glink_vma + off - glink->vma, 4))
3648 {
3649 unsigned int insn = bfd_get_32 (abfd, buf);
3650 insn ^= B_DOT;
3651 if ((insn & ~0x3fffffc) == 0)
3652 {
3653 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3654 break;
3655 }
3656 off += 4;
3657 if (off > 4)
3658 break;
3659 }
3660
3661 if (resolv_vma)
3662 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3663
3664 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3665 if (relplt != NULL)
3666 {
3667 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3668 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3669 goto free_contents_and_exit_err;
3670
3671 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3672 size += plt_count * sizeof (asymbol);
3673
3674 p = relplt->relocation;
3675 for (i = 0; i < plt_count; i++, p++)
3676 {
3677 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3678 if (p->addend != 0)
3679 size += sizeof ("+0x") - 1 + 16;
3680 }
3681 }
3682 }
3683
3684 if (size == 0)
3685 goto free_contents_and_exit;
3686 s = *ret = bfd_malloc (size);
3687 if (s == NULL)
3688 goto free_contents_and_exit_err;
3689
3690 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3691
3692 for (i = secsymend; i < opdsymend; ++i)
3693 {
3694 bfd_vma ent;
3695
3696 if (syms[i]->value > opd->size - 8)
3697 continue;
3698
3699 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3700 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3701 {
3702 size_t lo, hi;
3703 size_t len;
3704 asection *sec = abfd->sections;
3705
3706 *s = *syms[i];
3707 lo = codesecsym;
3708 hi = codesecsymend;
3709 while (lo < hi)
3710 {
3711 size_t mid = (lo + hi) >> 1;
3712 if (syms[mid]->section->vma < ent)
3713 lo = mid + 1;
3714 else if (syms[mid]->section->vma > ent)
3715 hi = mid;
3716 else
3717 {
3718 sec = syms[mid]->section;
3719 break;
3720 }
3721 }
3722
3723 if (lo >= hi && lo > codesecsym)
3724 sec = syms[lo - 1]->section;
3725
3726 for (; sec != NULL; sec = sec->next)
3727 {
3728 if (sec->vma > ent)
3729 break;
3730 /* SEC_LOAD may not be set if SEC is from a separate debug
3731 info file. */
3732 if ((sec->flags & SEC_ALLOC) == 0)
3733 break;
3734 if ((sec->flags & SEC_CODE) != 0)
3735 s->section = sec;
3736 }
3737 s->flags |= BSF_SYNTHETIC;
3738 s->value = ent - s->section->vma;
3739 s->name = names;
3740 *names++ = '.';
3741 len = strlen (syms[i]->name);
3742 memcpy (names, syms[i]->name, len + 1);
3743 names += len + 1;
3744 /* Have udata.p point back to the original symbol this
3745 synthetic symbol was derived from. */
3746 s->udata.p = syms[i];
3747 s++;
3748 }
3749 }
3750 free (contents);
3751
3752 if (glink != NULL && relplt != NULL)
3753 {
3754 if (resolv_vma)
3755 {
3756 /* Add a symbol for the main glink trampoline. */
3757 memset (s, 0, sizeof *s);
3758 s->the_bfd = abfd;
3759 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3760 s->section = glink;
3761 s->value = resolv_vma - glink->vma;
3762 s->name = names;
3763 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3764 names += sizeof ("__glink_PLTresolve");
3765 s++;
3766 count++;
3767 }
3768
3769 /* FIXME: It would be very much nicer to put sym@plt on the
3770 stub rather than on the glink branch table entry. The
3771 objdump disassembler would then use a sensible symbol
3772 name on plt calls. The difficulty in doing so is
3773 a) finding the stubs, and,
3774 b) matching stubs against plt entries, and,
3775 c) there can be multiple stubs for a given plt entry.
3776
3777 Solving (a) could be done by code scanning, but older
3778 ppc64 binaries used different stubs to current code.
3779 (b) is the tricky one since you need to known the toc
3780 pointer for at least one function that uses a pic stub to
3781 be able to calculate the plt address referenced.
3782 (c) means gdb would need to set multiple breakpoints (or
3783 find the glink branch itself) when setting breakpoints
3784 for pending shared library loads. */
3785 p = relplt->relocation;
3786 for (i = 0; i < plt_count; i++, p++)
3787 {
3788 size_t len;
3789
3790 *s = **p->sym_ptr_ptr;
3791 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3792 we are defining a symbol, ensure one of them is set. */
3793 if ((s->flags & BSF_LOCAL) == 0)
3794 s->flags |= BSF_GLOBAL;
3795 s->flags |= BSF_SYNTHETIC;
3796 s->section = glink;
3797 s->value = glink_vma - glink->vma;
3798 s->name = names;
3799 s->udata.p = NULL;
3800 len = strlen ((*p->sym_ptr_ptr)->name);
3801 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3802 names += len;
3803 if (p->addend != 0)
3804 {
3805 memcpy (names, "+0x", sizeof ("+0x") - 1);
3806 names += sizeof ("+0x") - 1;
3807 bfd_sprintf_vma (abfd, names, p->addend);
3808 names += strlen (names);
3809 }
3810 memcpy (names, "@plt", sizeof ("@plt"));
3811 names += sizeof ("@plt");
3812 s++;
3813 if (abi < 2)
3814 {
3815 glink_vma += 8;
3816 if (i >= 0x8000)
3817 glink_vma += 4;
3818 }
3819 else
3820 glink_vma += 4;
3821 }
3822 count += plt_count;
3823 }
3824 }
3825
3826 done:
3827 free (syms);
3828 return count;
3829 }
3830 \f
3831 /* The following functions are specific to the ELF linker, while
3832 functions above are used generally. Those named ppc64_elf_* are
3833 called by the main ELF linker code. They appear in this file more
3834 or less in the order in which they are called. eg.
3835 ppc64_elf_check_relocs is called early in the link process,
3836 ppc64_elf_finish_dynamic_sections is one of the last functions
3837 called.
3838
3839 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3840 functions have both a function code symbol and a function descriptor
3841 symbol. A call to foo in a relocatable object file looks like:
3842
3843 . .text
3844 . x:
3845 . bl .foo
3846 . nop
3847
3848 The function definition in another object file might be:
3849
3850 . .section .opd
3851 . foo: .quad .foo
3852 . .quad .TOC.@tocbase
3853 . .quad 0
3854 .
3855 . .text
3856 . .foo: blr
3857
3858 When the linker resolves the call during a static link, the branch
3859 unsurprisingly just goes to .foo and the .opd information is unused.
3860 If the function definition is in a shared library, things are a little
3861 different: The call goes via a plt call stub, the opd information gets
3862 copied to the plt, and the linker patches the nop.
3863
3864 . x:
3865 . bl .foo_stub
3866 . ld 2,40(1)
3867 .
3868 .
3869 . .foo_stub:
3870 . std 2,40(1) # in practice, the call stub
3871 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3872 . addi 11,11,Lfoo@toc@l # this is the general idea
3873 . ld 12,0(11)
3874 . ld 2,8(11)
3875 . mtctr 12
3876 . ld 11,16(11)
3877 . bctr
3878 .
3879 . .section .plt
3880 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3881
3882 The "reloc ()" notation is supposed to indicate that the linker emits
3883 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3884 copying.
3885
3886 What are the difficulties here? Well, firstly, the relocations
3887 examined by the linker in check_relocs are against the function code
3888 sym .foo, while the dynamic relocation in the plt is emitted against
3889 the function descriptor symbol, foo. Somewhere along the line, we need
3890 to carefully copy dynamic link information from one symbol to the other.
3891 Secondly, the generic part of the elf linker will make .foo a dynamic
3892 symbol as is normal for most other backends. We need foo dynamic
3893 instead, at least for an application final link. However, when
3894 creating a shared library containing foo, we need to have both symbols
3895 dynamic so that references to .foo are satisfied during the early
3896 stages of linking. Otherwise the linker might decide to pull in a
3897 definition from some other object, eg. a static library.
3898
3899 Update: As of August 2004, we support a new convention. Function
3900 calls may use the function descriptor symbol, ie. "bl foo". This
3901 behaves exactly as "bl .foo". */
3902
3903 /* Of those relocs that might be copied as dynamic relocs, this
3904 function selects those that must be copied when linking a shared
3905 library or PIE, even when the symbol is local. */
3906
3907 static int
3908 must_be_dyn_reloc (struct bfd_link_info *info,
3909 enum elf_ppc64_reloc_type r_type)
3910 {
3911 switch (r_type)
3912 {
3913 default:
3914 /* Only relative relocs can be resolved when the object load
3915 address isn't fixed. DTPREL64 is excluded because the
3916 dynamic linker needs to differentiate global dynamic from
3917 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3918 return 1;
3919
3920 case R_PPC64_REL32:
3921 case R_PPC64_REL64:
3922 case R_PPC64_REL30:
3923 return 0;
3924
3925 case R_PPC64_TPREL16:
3926 case R_PPC64_TPREL16_LO:
3927 case R_PPC64_TPREL16_HI:
3928 case R_PPC64_TPREL16_HA:
3929 case R_PPC64_TPREL16_DS:
3930 case R_PPC64_TPREL16_LO_DS:
3931 case R_PPC64_TPREL16_HIGH:
3932 case R_PPC64_TPREL16_HIGHA:
3933 case R_PPC64_TPREL16_HIGHER:
3934 case R_PPC64_TPREL16_HIGHERA:
3935 case R_PPC64_TPREL16_HIGHEST:
3936 case R_PPC64_TPREL16_HIGHESTA:
3937 case R_PPC64_TPREL64:
3938 /* These relocations are relative but in a shared library the
3939 linker doesn't know the thread pointer base. */
3940 return bfd_link_dll (info);
3941 }
3942 }
3943
3944 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3945 copying dynamic variables from a shared lib into an app's dynbss
3946 section, and instead use a dynamic relocation to point into the
3947 shared lib. With code that gcc generates, it's vital that this be
3948 enabled; In the PowerPC64 ABI, the address of a function is actually
3949 the address of a function descriptor, which resides in the .opd
3950 section. gcc uses the descriptor directly rather than going via the
3951 GOT as some other ABI's do, which means that initialized function
3952 pointers must reference the descriptor. Thus, a function pointer
3953 initialized to the address of a function in a shared library will
3954 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3955 redefines the function descriptor symbol to point to the copy. This
3956 presents a problem as a plt entry for that function is also
3957 initialized from the function descriptor symbol and the copy reloc
3958 may not be initialized first. */
3959 #define ELIMINATE_COPY_RELOCS 1
3960
3961 /* Section name for stubs is the associated section name plus this
3962 string. */
3963 #define STUB_SUFFIX ".stub"
3964
3965 /* Linker stubs.
3966 ppc_stub_long_branch:
3967 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3968 destination, but a 24 bit branch in a stub section will reach.
3969 . b dest
3970
3971 ppc_stub_plt_branch:
3972 Similar to the above, but a 24 bit branch in the stub section won't
3973 reach its destination.
3974 . addis %r11,%r2,xxx@toc@ha
3975 . ld %r12,xxx@toc@l(%r11)
3976 . mtctr %r12
3977 . bctr
3978
3979 ppc_stub_plt_call:
3980 Used to call a function in a shared library. If it so happens that
3981 the plt entry referenced crosses a 64k boundary, then an extra
3982 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3983 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
3984 . addis %r11,%r2,xxx@toc@ha
3985 . ld %r12,xxx+0@toc@l(%r11)
3986 . mtctr %r12
3987 . ld %r2,xxx+8@toc@l(%r11)
3988 . ld %r11,xxx+16@toc@l(%r11)
3989 . bctr
3990
3991 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3992 code to adjust the value and save r2 to support multiple toc sections.
3993 A ppc_stub_long_branch with an r2 offset looks like:
3994 . std %r2,40(%r1)
3995 . addis %r2,%r2,off@ha
3996 . addi %r2,%r2,off@l
3997 . b dest
3998
3999 A ppc_stub_plt_branch with an r2 offset looks like:
4000 . std %r2,40(%r1)
4001 . addis %r11,%r2,xxx@toc@ha
4002 . ld %r12,xxx@toc@l(%r11)
4003 . addis %r2,%r2,off@ha
4004 . addi %r2,%r2,off@l
4005 . mtctr %r12
4006 . bctr
4007
4008 All of the above stubs are shown as their ELFv1 variants. ELFv2
4009 variants exist too, simpler for plt calls since a new toc pointer
4010 and static chain are not loaded by the stub. In addition, ELFv2
4011 has some more complex stubs to handle calls marked with NOTOC
4012 relocs from functions where r2 is not a valid toc pointer. These
4013 come in two flavours, the ones shown below, and _both variants that
4014 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
4015 one call is from a function where r2 is used as the toc pointer but
4016 needs a toc adjusting stub for small-model multi-toc, and another
4017 call is from a function where r2 is not valid.
4018 ppc_stub_long_branch_notoc:
4019 . mflr %r12
4020 . bcl 20,31,1f
4021 . 1:
4022 . mflr %r11
4023 . mtlr %r12
4024 . lis %r12,xxx-1b@highest
4025 . ori %r12,xxx-1b@higher
4026 . sldi %r12,%r12,32
4027 . oris %r12,%r12,xxx-1b@hi
4028 . ori %r12,%r12,xxx-1b@l
4029 . add %r12,%r11,%r12
4030 . b dest
4031
4032 ppc_stub_plt_branch_notoc:
4033 . mflr %r12
4034 . bcl 20,31,1f
4035 . 1:
4036 . mflr %r11
4037 . mtlr %r12
4038 . lis %r12,xxx-1b@highest
4039 . ori %r12,xxx-1b@higher
4040 . sldi %r12,%r12,32
4041 . oris %r12,%r12,xxx-1b@hi
4042 . ori %r12,%r12,xxx-1b@l
4043 . add %r12,%r11,%r12
4044 . mtctr %r12
4045 . bctr
4046
4047 ppc_stub_plt_call_notoc:
4048 . mflr %r12
4049 . bcl 20,31,1f
4050 . 1:
4051 . mflr %r11
4052 . mtlr %r12
4053 . lis %r12,xxx-1b@highest
4054 . ori %r12,xxx-1b@higher
4055 . sldi %r12,%r12,32
4056 . oris %r12,%r12,xxx-1b@hi
4057 . ori %r12,%r12,xxx-1b@l
4058 . ldx %r12,%r11,%r12
4059 . mtctr %r12
4060 . bctr
4061
4062 In cases where the high instructions would add zero, they are
4063 omitted and following instructions modified in some cases.
4064
4065 For a given stub group (a set of sections all using the same toc
4066 pointer value) there will be just one stub type used for any
4067 particular function symbol. For example, if printf is called from
4068 code with the tocsave optimization (ie. r2 saved in function
4069 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
4070 and from other code without the tocsave optimization requiring a
4071 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
4072 type will be created. Calls with the tocsave optimization will
4073 enter this stub after the instruction saving r2. A similar
4074 situation exists when calls are marked with R_PPC64_REL24_NOTOC
4075 relocations. These require a ppc_stub_plt_call_notoc linkage stub
4076 to call an external function like printf. If other calls to printf
4077 require a ppc_stub_plt_call linkage stub then a single
4078 ppc_stub_plt_call_notoc linkage stub will be used for both types of
4079 call. If other calls to printf require a ppc_stub_plt_call_r2save
4080 linkage stub then a single ppc_stub_plt_call_both linkage stub will
4081 be created and calls not requiring r2 to be saved will enter the
4082 stub after the r2 save instruction. There is an analogous
4083 hierarchy of long branch and plt branch stubs for local call
4084 linkage. */
4085
4086 enum ppc_stub_type {
4087 ppc_stub_none,
4088 ppc_stub_long_branch,
4089 ppc_stub_long_branch_r2off,
4090 ppc_stub_long_branch_notoc,
4091 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
4092 ppc_stub_plt_branch,
4093 ppc_stub_plt_branch_r2off,
4094 ppc_stub_plt_branch_notoc,
4095 ppc_stub_plt_branch_both,
4096 ppc_stub_plt_call,
4097 ppc_stub_plt_call_r2save,
4098 ppc_stub_plt_call_notoc,
4099 ppc_stub_plt_call_both,
4100 ppc_stub_global_entry,
4101 ppc_stub_save_res
4102 };
4103
4104 /* Information on stub grouping. */
4105 struct map_stub
4106 {
4107 /* The stub section. */
4108 asection *stub_sec;
4109 /* This is the section to which stubs in the group will be attached. */
4110 asection *link_sec;
4111 /* Next group. */
4112 struct map_stub *next;
4113 /* Whether to emit a copy of register save/restore functions in this
4114 group. */
4115 int needs_save_res;
4116 /* Current offset within stubs after the insn restoring lr in a
4117 _notoc or _both stub using bcl for pc-relative addressing, or
4118 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
4119 unsigned int lr_restore;
4120 /* Accumulated size of EH info emitted to describe return address
4121 if stubs modify lr. Does not include 17 byte FDE header. */
4122 unsigned int eh_size;
4123 /* Offset in glink_eh_frame to the start of EH info for this group. */
4124 unsigned int eh_base;
4125 };
4126
4127 struct ppc_stub_hash_entry {
4128
4129 /* Base hash table entry structure. */
4130 struct bfd_hash_entry root;
4131
4132 enum ppc_stub_type stub_type;
4133
4134 /* Group information. */
4135 struct map_stub *group;
4136
4137 /* Offset within stub_sec of the beginning of this stub. */
4138 bfd_vma stub_offset;
4139
4140 /* Given the symbol's value and its section we can determine its final
4141 value when building the stubs (so the stub knows where to jump. */
4142 bfd_vma target_value;
4143 asection *target_section;
4144
4145 /* The symbol table entry, if any, that this was derived from. */
4146 struct ppc_link_hash_entry *h;
4147 struct plt_entry *plt_ent;
4148
4149 /* Symbol type. */
4150 unsigned char symtype;
4151
4152 /* Symbol st_other. */
4153 unsigned char other;
4154 };
4155
4156 struct ppc_branch_hash_entry {
4157
4158 /* Base hash table entry structure. */
4159 struct bfd_hash_entry root;
4160
4161 /* Offset within branch lookup table. */
4162 unsigned int offset;
4163
4164 /* Generation marker. */
4165 unsigned int iter;
4166 };
4167
4168 /* Used to track dynamic relocations for local symbols. */
4169 struct ppc_dyn_relocs
4170 {
4171 struct ppc_dyn_relocs *next;
4172
4173 /* The input section of the reloc. */
4174 asection *sec;
4175
4176 /* Total number of relocs copied for the input section. */
4177 unsigned int count : 31;
4178
4179 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4180 unsigned int ifunc : 1;
4181 };
4182
4183 struct ppc_link_hash_entry
4184 {
4185 struct elf_link_hash_entry elf;
4186
4187 union {
4188 /* A pointer to the most recently used stub hash entry against this
4189 symbol. */
4190 struct ppc_stub_hash_entry *stub_cache;
4191
4192 /* A pointer to the next symbol starting with a '.' */
4193 struct ppc_link_hash_entry *next_dot_sym;
4194 } u;
4195
4196 /* Track dynamic relocs copied for this symbol. */
4197 struct elf_dyn_relocs *dyn_relocs;
4198
4199 /* Link between function code and descriptor symbols. */
4200 struct ppc_link_hash_entry *oh;
4201
4202 /* Flag function code and descriptor symbols. */
4203 unsigned int is_func:1;
4204 unsigned int is_func_descriptor:1;
4205 unsigned int fake:1;
4206
4207 /* Whether global opd/toc sym has been adjusted or not.
4208 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4209 should be set for all globals defined in any opd/toc section. */
4210 unsigned int adjust_done:1;
4211
4212 /* Set if this is an out-of-line register save/restore function,
4213 with non-standard calling convention. */
4214 unsigned int save_res:1;
4215
4216 /* Set if a duplicate symbol with non-zero localentry is detected,
4217 even when the duplicate symbol does not provide a definition. */
4218 unsigned int non_zero_localentry:1;
4219
4220 /* Contexts in which symbol is used in the GOT (or TOC).
4221 Bits are or'd into the mask as the corresponding relocs are
4222 encountered during check_relocs, with TLS_TLS being set when any
4223 of the other TLS bits are set. tls_optimize clears bits when
4224 optimizing to indicate the corresponding GOT entry type is not
4225 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4226 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4227 separate flag rather than setting TPREL just for convenience in
4228 distinguishing the two cases.
4229 These flags are also kept for local symbols. */
4230 #define TLS_TLS 1 /* Any TLS reloc. */
4231 #define TLS_GD 2 /* GD reloc. */
4232 #define TLS_LD 4 /* LD reloc. */
4233 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4234 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4235 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4236 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4237 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4238 unsigned char tls_mask;
4239
4240 /* The above field is also used to mark function symbols. In which
4241 case TLS_TLS will be 0. */
4242 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4243 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4244 #define NON_GOT 256 /* local symbol plt, not stored. */
4245 };
4246
4247 /* ppc64 ELF linker hash table. */
4248
4249 struct ppc_link_hash_table
4250 {
4251 struct elf_link_hash_table elf;
4252
4253 /* The stub hash table. */
4254 struct bfd_hash_table stub_hash_table;
4255
4256 /* Another hash table for plt_branch stubs. */
4257 struct bfd_hash_table branch_hash_table;
4258
4259 /* Hash table for function prologue tocsave. */
4260 htab_t tocsave_htab;
4261
4262 /* Various options and other info passed from the linker. */
4263 struct ppc64_elf_params *params;
4264
4265 /* The size of sec_info below. */
4266 unsigned int sec_info_arr_size;
4267
4268 /* Per-section array of extra section info. Done this way rather
4269 than as part of ppc64_elf_section_data so we have the info for
4270 non-ppc64 sections. */
4271 struct
4272 {
4273 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4274 bfd_vma toc_off;
4275
4276 union
4277 {
4278 /* The section group that this section belongs to. */
4279 struct map_stub *group;
4280 /* A temp section list pointer. */
4281 asection *list;
4282 } u;
4283 } *sec_info;
4284
4285 /* Linked list of groups. */
4286 struct map_stub *group;
4287
4288 /* Temp used when calculating TOC pointers. */
4289 bfd_vma toc_curr;
4290 bfd *toc_bfd;
4291 asection *toc_first_sec;
4292
4293 /* Used when adding symbols. */
4294 struct ppc_link_hash_entry *dot_syms;
4295
4296 /* Shortcuts to get to dynamic linker sections. */
4297 asection *glink;
4298 asection *global_entry;
4299 asection *sfpr;
4300 asection *pltlocal;
4301 asection *relpltlocal;
4302 asection *brlt;
4303 asection *relbrlt;
4304 asection *glink_eh_frame;
4305
4306 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4307 struct ppc_link_hash_entry *tls_get_addr;
4308 struct ppc_link_hash_entry *tls_get_addr_fd;
4309
4310 /* The size of reliplt used by got entry relocs. */
4311 bfd_size_type got_reli_size;
4312
4313 /* Statistics. */
4314 unsigned long stub_count[ppc_stub_global_entry];
4315
4316 /* Number of stubs against global syms. */
4317 unsigned long stub_globals;
4318
4319 /* Set if we're linking code with function descriptors. */
4320 unsigned int opd_abi:1;
4321
4322 /* Support for multiple toc sections. */
4323 unsigned int do_multi_toc:1;
4324 unsigned int multi_toc_needed:1;
4325 unsigned int second_toc_pass:1;
4326 unsigned int do_toc_opt:1;
4327
4328 /* Set if tls optimization is enabled. */
4329 unsigned int do_tls_opt:1;
4330
4331 /* Set if inline plt calls should be converted to direct calls. */
4332 unsigned int can_convert_all_inline_plt:1;
4333
4334 /* Set on error. */
4335 unsigned int stub_error:1;
4336
4337 /* Whether func_desc_adjust needs to be run over symbols. */
4338 unsigned int need_func_desc_adj:1;
4339
4340 /* Whether there exist local gnu indirect function resolvers,
4341 referenced by dynamic relocations. */
4342 unsigned int local_ifunc_resolver:1;
4343 unsigned int maybe_local_ifunc_resolver:1;
4344
4345 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4346 unsigned int has_plt_localentry0:1;
4347
4348 /* Incremented every time we size stubs. */
4349 unsigned int stub_iteration;
4350
4351 /* Small local sym cache. */
4352 struct sym_cache sym_cache;
4353 };
4354
4355 /* Rename some of the generic section flags to better document how they
4356 are used here. */
4357
4358 /* Nonzero if this section has TLS related relocations. */
4359 #define has_tls_reloc sec_flg0
4360
4361 /* Nonzero if this section has an old-style call to __tls_get_addr. */
4362 #define has_tls_get_addr_call sec_flg1
4363
4364 /* Nonzero if this section has any toc or got relocs. */
4365 #define has_toc_reloc sec_flg2
4366
4367 /* Nonzero if this section has a call to another section that uses
4368 the toc or got. */
4369 #define makes_toc_func_call sec_flg3
4370
4371 /* Recursion protection when determining above flag. */
4372 #define call_check_in_progress sec_flg4
4373 #define call_check_done sec_flg5
4374
4375 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4376
4377 #define ppc_hash_table(p) \
4378 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4379 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4380
4381 #define ppc_stub_hash_lookup(table, string, create, copy) \
4382 ((struct ppc_stub_hash_entry *) \
4383 bfd_hash_lookup ((table), (string), (create), (copy)))
4384
4385 #define ppc_branch_hash_lookup(table, string, create, copy) \
4386 ((struct ppc_branch_hash_entry *) \
4387 bfd_hash_lookup ((table), (string), (create), (copy)))
4388
4389 /* Create an entry in the stub hash table. */
4390
4391 static struct bfd_hash_entry *
4392 stub_hash_newfunc (struct bfd_hash_entry *entry,
4393 struct bfd_hash_table *table,
4394 const char *string)
4395 {
4396 /* Allocate the structure if it has not already been allocated by a
4397 subclass. */
4398 if (entry == NULL)
4399 {
4400 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4401 if (entry == NULL)
4402 return entry;
4403 }
4404
4405 /* Call the allocation method of the superclass. */
4406 entry = bfd_hash_newfunc (entry, table, string);
4407 if (entry != NULL)
4408 {
4409 struct ppc_stub_hash_entry *eh;
4410
4411 /* Initialize the local fields. */
4412 eh = (struct ppc_stub_hash_entry *) entry;
4413 eh->stub_type = ppc_stub_none;
4414 eh->group = NULL;
4415 eh->stub_offset = 0;
4416 eh->target_value = 0;
4417 eh->target_section = NULL;
4418 eh->h = NULL;
4419 eh->plt_ent = NULL;
4420 eh->other = 0;
4421 }
4422
4423 return entry;
4424 }
4425
4426 /* Create an entry in the branch hash table. */
4427
4428 static struct bfd_hash_entry *
4429 branch_hash_newfunc (struct bfd_hash_entry *entry,
4430 struct bfd_hash_table *table,
4431 const char *string)
4432 {
4433 /* Allocate the structure if it has not already been allocated by a
4434 subclass. */
4435 if (entry == NULL)
4436 {
4437 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4438 if (entry == NULL)
4439 return entry;
4440 }
4441
4442 /* Call the allocation method of the superclass. */
4443 entry = bfd_hash_newfunc (entry, table, string);
4444 if (entry != NULL)
4445 {
4446 struct ppc_branch_hash_entry *eh;
4447
4448 /* Initialize the local fields. */
4449 eh = (struct ppc_branch_hash_entry *) entry;
4450 eh->offset = 0;
4451 eh->iter = 0;
4452 }
4453
4454 return entry;
4455 }
4456
4457 /* Create an entry in a ppc64 ELF linker hash table. */
4458
4459 static struct bfd_hash_entry *
4460 link_hash_newfunc (struct bfd_hash_entry *entry,
4461 struct bfd_hash_table *table,
4462 const char *string)
4463 {
4464 /* Allocate the structure if it has not already been allocated by a
4465 subclass. */
4466 if (entry == NULL)
4467 {
4468 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4469 if (entry == NULL)
4470 return entry;
4471 }
4472
4473 /* Call the allocation method of the superclass. */
4474 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4475 if (entry != NULL)
4476 {
4477 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4478
4479 memset (&eh->u.stub_cache, 0,
4480 (sizeof (struct ppc_link_hash_entry)
4481 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4482
4483 /* When making function calls, old ABI code references function entry
4484 points (dot symbols), while new ABI code references the function
4485 descriptor symbol. We need to make any combination of reference and
4486 definition work together, without breaking archive linking.
4487
4488 For a defined function "foo" and an undefined call to "bar":
4489 An old object defines "foo" and ".foo", references ".bar" (possibly
4490 "bar" too).
4491 A new object defines "foo" and references "bar".
4492
4493 A new object thus has no problem with its undefined symbols being
4494 satisfied by definitions in an old object. On the other hand, the
4495 old object won't have ".bar" satisfied by a new object.
4496
4497 Keep a list of newly added dot-symbols. */
4498
4499 if (string[0] == '.')
4500 {
4501 struct ppc_link_hash_table *htab;
4502
4503 htab = (struct ppc_link_hash_table *) table;
4504 eh->u.next_dot_sym = htab->dot_syms;
4505 htab->dot_syms = eh;
4506 }
4507 }
4508
4509 return entry;
4510 }
4511
4512 struct tocsave_entry {
4513 asection *sec;
4514 bfd_vma offset;
4515 };
4516
4517 static hashval_t
4518 tocsave_htab_hash (const void *p)
4519 {
4520 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4521 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4522 }
4523
4524 static int
4525 tocsave_htab_eq (const void *p1, const void *p2)
4526 {
4527 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4528 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4529 return e1->sec == e2->sec && e1->offset == e2->offset;
4530 }
4531
4532 /* Destroy a ppc64 ELF linker hash table. */
4533
4534 static void
4535 ppc64_elf_link_hash_table_free (bfd *obfd)
4536 {
4537 struct ppc_link_hash_table *htab;
4538
4539 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4540 if (htab->tocsave_htab)
4541 htab_delete (htab->tocsave_htab);
4542 bfd_hash_table_free (&htab->branch_hash_table);
4543 bfd_hash_table_free (&htab->stub_hash_table);
4544 _bfd_elf_link_hash_table_free (obfd);
4545 }
4546
4547 /* Create a ppc64 ELF linker hash table. */
4548
4549 static struct bfd_link_hash_table *
4550 ppc64_elf_link_hash_table_create (bfd *abfd)
4551 {
4552 struct ppc_link_hash_table *htab;
4553 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4554
4555 htab = bfd_zmalloc (amt);
4556 if (htab == NULL)
4557 return NULL;
4558
4559 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4560 sizeof (struct ppc_link_hash_entry),
4561 PPC64_ELF_DATA))
4562 {
4563 free (htab);
4564 return NULL;
4565 }
4566
4567 /* Init the stub hash table too. */
4568 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4569 sizeof (struct ppc_stub_hash_entry)))
4570 {
4571 _bfd_elf_link_hash_table_free (abfd);
4572 return NULL;
4573 }
4574
4575 /* And the branch hash table. */
4576 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4577 sizeof (struct ppc_branch_hash_entry)))
4578 {
4579 bfd_hash_table_free (&htab->stub_hash_table);
4580 _bfd_elf_link_hash_table_free (abfd);
4581 return NULL;
4582 }
4583
4584 htab->tocsave_htab = htab_try_create (1024,
4585 tocsave_htab_hash,
4586 tocsave_htab_eq,
4587 NULL);
4588 if (htab->tocsave_htab == NULL)
4589 {
4590 ppc64_elf_link_hash_table_free (abfd);
4591 return NULL;
4592 }
4593 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4594
4595 /* Initializing two fields of the union is just cosmetic. We really
4596 only care about glist, but when compiled on a 32-bit host the
4597 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4598 debugger inspection of these fields look nicer. */
4599 htab->elf.init_got_refcount.refcount = 0;
4600 htab->elf.init_got_refcount.glist = NULL;
4601 htab->elf.init_plt_refcount.refcount = 0;
4602 htab->elf.init_plt_refcount.glist = NULL;
4603 htab->elf.init_got_offset.offset = 0;
4604 htab->elf.init_got_offset.glist = NULL;
4605 htab->elf.init_plt_offset.offset = 0;
4606 htab->elf.init_plt_offset.glist = NULL;
4607
4608 return &htab->elf.root;
4609 }
4610
4611 /* Create sections for linker generated code. */
4612
4613 static bfd_boolean
4614 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4615 {
4616 struct ppc_link_hash_table *htab;
4617 flagword flags;
4618
4619 htab = ppc_hash_table (info);
4620
4621 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4622 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4623 if (htab->params->save_restore_funcs)
4624 {
4625 /* Create .sfpr for code to save and restore fp regs. */
4626 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4627 flags);
4628 if (htab->sfpr == NULL
4629 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4630 return FALSE;
4631 }
4632
4633 if (bfd_link_relocatable (info))
4634 return TRUE;
4635
4636 /* Create .glink for lazy dynamic linking support. */
4637 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4638 flags);
4639 if (htab->glink == NULL
4640 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4641 return FALSE;
4642
4643 /* The part of .glink used by global entry stubs, separate so that
4644 it can be aligned appropriately without affecting htab->glink. */
4645 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4646 flags);
4647 if (htab->global_entry == NULL
4648 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4649 return FALSE;
4650
4651 if (!info->no_ld_generated_unwind_info)
4652 {
4653 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4654 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4655 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4656 ".eh_frame",
4657 flags);
4658 if (htab->glink_eh_frame == NULL
4659 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4660 return FALSE;
4661 }
4662
4663 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4664 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4665 if (htab->elf.iplt == NULL
4666 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4667 return FALSE;
4668
4669 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4670 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4671 htab->elf.irelplt
4672 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4673 if (htab->elf.irelplt == NULL
4674 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4675 return FALSE;
4676
4677 /* Create branch lookup table for plt_branch stubs. */
4678 flags = (SEC_ALLOC | SEC_LOAD
4679 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4680 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4681 flags);
4682 if (htab->brlt == NULL
4683 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4684 return FALSE;
4685
4686 /* Local plt entries, put in .branch_lt but a separate section for
4687 convenience. */
4688 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4689 flags);
4690 if (htab->pltlocal == NULL
4691 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4692 return FALSE;
4693
4694 if (!bfd_link_pic (info))
4695 return TRUE;
4696
4697 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4698 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4699 htab->relbrlt
4700 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4701 if (htab->relbrlt == NULL
4702 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4703 return FALSE;
4704
4705 htab->relpltlocal
4706 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4707 if (htab->relpltlocal == NULL
4708 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4709 return FALSE;
4710
4711 return TRUE;
4712 }
4713
4714 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4715
4716 bfd_boolean
4717 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4718 struct ppc64_elf_params *params)
4719 {
4720 struct ppc_link_hash_table *htab;
4721
4722 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4723
4724 /* Always hook our dynamic sections into the first bfd, which is the
4725 linker created stub bfd. This ensures that the GOT header is at
4726 the start of the output TOC section. */
4727 htab = ppc_hash_table (info);
4728 htab->elf.dynobj = params->stub_bfd;
4729 htab->params = params;
4730
4731 return create_linkage_sections (htab->elf.dynobj, info);
4732 }
4733
4734 /* Build a name for an entry in the stub hash table. */
4735
4736 static char *
4737 ppc_stub_name (const asection *input_section,
4738 const asection *sym_sec,
4739 const struct ppc_link_hash_entry *h,
4740 const Elf_Internal_Rela *rel)
4741 {
4742 char *stub_name;
4743 ssize_t len;
4744
4745 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4746 offsets from a sym as a branch target? In fact, we could
4747 probably assume the addend is always zero. */
4748 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4749
4750 if (h)
4751 {
4752 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4753 stub_name = bfd_malloc (len);
4754 if (stub_name == NULL)
4755 return stub_name;
4756
4757 len = sprintf (stub_name, "%08x.%s+%x",
4758 input_section->id & 0xffffffff,
4759 h->elf.root.root.string,
4760 (int) rel->r_addend & 0xffffffff);
4761 }
4762 else
4763 {
4764 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4765 stub_name = bfd_malloc (len);
4766 if (stub_name == NULL)
4767 return stub_name;
4768
4769 len = sprintf (stub_name, "%08x.%x:%x+%x",
4770 input_section->id & 0xffffffff,
4771 sym_sec->id & 0xffffffff,
4772 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4773 (int) rel->r_addend & 0xffffffff);
4774 }
4775 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4776 stub_name[len - 2] = 0;
4777 return stub_name;
4778 }
4779
4780 /* Look up an entry in the stub hash. Stub entries are cached because
4781 creating the stub name takes a bit of time. */
4782
4783 static struct ppc_stub_hash_entry *
4784 ppc_get_stub_entry (const asection *input_section,
4785 const asection *sym_sec,
4786 struct ppc_link_hash_entry *h,
4787 const Elf_Internal_Rela *rel,
4788 struct ppc_link_hash_table *htab)
4789 {
4790 struct ppc_stub_hash_entry *stub_entry;
4791 struct map_stub *group;
4792
4793 /* If this input section is part of a group of sections sharing one
4794 stub section, then use the id of the first section in the group.
4795 Stub names need to include a section id, as there may well be
4796 more than one stub used to reach say, printf, and we need to
4797 distinguish between them. */
4798 group = htab->sec_info[input_section->id].u.group;
4799 if (group == NULL)
4800 return NULL;
4801
4802 if (h != NULL && h->u.stub_cache != NULL
4803 && h->u.stub_cache->h == h
4804 && h->u.stub_cache->group == group)
4805 {
4806 stub_entry = h->u.stub_cache;
4807 }
4808 else
4809 {
4810 char *stub_name;
4811
4812 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4813 if (stub_name == NULL)
4814 return NULL;
4815
4816 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4817 stub_name, FALSE, FALSE);
4818 if (h != NULL)
4819 h->u.stub_cache = stub_entry;
4820
4821 free (stub_name);
4822 }
4823
4824 return stub_entry;
4825 }
4826
4827 /* Add a new stub entry to the stub hash. Not all fields of the new
4828 stub entry are initialised. */
4829
4830 static struct ppc_stub_hash_entry *
4831 ppc_add_stub (const char *stub_name,
4832 asection *section,
4833 struct bfd_link_info *info)
4834 {
4835 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4836 struct map_stub *group;
4837 asection *link_sec;
4838 asection *stub_sec;
4839 struct ppc_stub_hash_entry *stub_entry;
4840
4841 group = htab->sec_info[section->id].u.group;
4842 link_sec = group->link_sec;
4843 stub_sec = group->stub_sec;
4844 if (stub_sec == NULL)
4845 {
4846 size_t namelen;
4847 bfd_size_type len;
4848 char *s_name;
4849
4850 namelen = strlen (link_sec->name);
4851 len = namelen + sizeof (STUB_SUFFIX);
4852 s_name = bfd_alloc (htab->params->stub_bfd, len);
4853 if (s_name == NULL)
4854 return NULL;
4855
4856 memcpy (s_name, link_sec->name, namelen);
4857 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4858 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4859 if (stub_sec == NULL)
4860 return NULL;
4861 group->stub_sec = stub_sec;
4862 }
4863
4864 /* Enter this entry into the linker stub hash table. */
4865 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4866 TRUE, FALSE);
4867 if (stub_entry == NULL)
4868 {
4869 /* xgettext:c-format */
4870 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4871 section->owner, stub_name);
4872 return NULL;
4873 }
4874
4875 stub_entry->group = group;
4876 stub_entry->stub_offset = 0;
4877 return stub_entry;
4878 }
4879
4880 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4881 not already done. */
4882
4883 static bfd_boolean
4884 create_got_section (bfd *abfd, struct bfd_link_info *info)
4885 {
4886 asection *got, *relgot;
4887 flagword flags;
4888 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4889
4890 if (!is_ppc64_elf (abfd))
4891 return FALSE;
4892 if (htab == NULL)
4893 return FALSE;
4894
4895 if (!htab->elf.sgot
4896 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4897 return FALSE;
4898
4899 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4900 | SEC_LINKER_CREATED);
4901
4902 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4903 if (!got
4904 || !bfd_set_section_alignment (abfd, got, 3))
4905 return FALSE;
4906
4907 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4908 flags | SEC_READONLY);
4909 if (!relgot
4910 || ! bfd_set_section_alignment (abfd, relgot, 3))
4911 return FALSE;
4912
4913 ppc64_elf_tdata (abfd)->got = got;
4914 ppc64_elf_tdata (abfd)->relgot = relgot;
4915 return TRUE;
4916 }
4917
4918 /* Follow indirect and warning symbol links. */
4919
4920 static inline struct bfd_link_hash_entry *
4921 follow_link (struct bfd_link_hash_entry *h)
4922 {
4923 while (h->type == bfd_link_hash_indirect
4924 || h->type == bfd_link_hash_warning)
4925 h = h->u.i.link;
4926 return h;
4927 }
4928
4929 static inline struct elf_link_hash_entry *
4930 elf_follow_link (struct elf_link_hash_entry *h)
4931 {
4932 return (struct elf_link_hash_entry *) follow_link (&h->root);
4933 }
4934
4935 static inline struct ppc_link_hash_entry *
4936 ppc_follow_link (struct ppc_link_hash_entry *h)
4937 {
4938 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4939 }
4940
4941 /* Merge PLT info on FROM with that on TO. */
4942
4943 static void
4944 move_plt_plist (struct ppc_link_hash_entry *from,
4945 struct ppc_link_hash_entry *to)
4946 {
4947 if (from->elf.plt.plist != NULL)
4948 {
4949 if (to->elf.plt.plist != NULL)
4950 {
4951 struct plt_entry **entp;
4952 struct plt_entry *ent;
4953
4954 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4955 {
4956 struct plt_entry *dent;
4957
4958 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4959 if (dent->addend == ent->addend)
4960 {
4961 dent->plt.refcount += ent->plt.refcount;
4962 *entp = ent->next;
4963 break;
4964 }
4965 if (dent == NULL)
4966 entp = &ent->next;
4967 }
4968 *entp = to->elf.plt.plist;
4969 }
4970
4971 to->elf.plt.plist = from->elf.plt.plist;
4972 from->elf.plt.plist = NULL;
4973 }
4974 }
4975
4976 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4977
4978 static void
4979 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4980 struct elf_link_hash_entry *dir,
4981 struct elf_link_hash_entry *ind)
4982 {
4983 struct ppc_link_hash_entry *edir, *eind;
4984
4985 edir = (struct ppc_link_hash_entry *) dir;
4986 eind = (struct ppc_link_hash_entry *) ind;
4987
4988 edir->is_func |= eind->is_func;
4989 edir->is_func_descriptor |= eind->is_func_descriptor;
4990 edir->tls_mask |= eind->tls_mask;
4991 if (eind->oh != NULL)
4992 edir->oh = ppc_follow_link (eind->oh);
4993
4994 if (edir->elf.versioned != versioned_hidden)
4995 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4996 edir->elf.ref_regular |= eind->elf.ref_regular;
4997 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4998 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4999 edir->elf.needs_plt |= eind->elf.needs_plt;
5000 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
5001
5002 /* If we were called to copy over info for a weak sym, don't copy
5003 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
5004 in order to simplify readonly_dynrelocs and save a field in the
5005 symbol hash entry, but that means dyn_relocs can't be used in any
5006 tests about a specific symbol, or affect other symbol flags which
5007 are then tested. */
5008 if (eind->elf.root.type != bfd_link_hash_indirect)
5009 return;
5010
5011 /* Copy over any dynamic relocs we may have on the indirect sym. */
5012 if (eind->dyn_relocs != NULL)
5013 {
5014 if (edir->dyn_relocs != NULL)
5015 {
5016 struct elf_dyn_relocs **pp;
5017 struct elf_dyn_relocs *p;
5018
5019 /* Add reloc counts against the indirect sym to the direct sym
5020 list. Merge any entries against the same section. */
5021 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5022 {
5023 struct elf_dyn_relocs *q;
5024
5025 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5026 if (q->sec == p->sec)
5027 {
5028 q->pc_count += p->pc_count;
5029 q->count += p->count;
5030 *pp = p->next;
5031 break;
5032 }
5033 if (q == NULL)
5034 pp = &p->next;
5035 }
5036 *pp = edir->dyn_relocs;
5037 }
5038
5039 edir->dyn_relocs = eind->dyn_relocs;
5040 eind->dyn_relocs = NULL;
5041 }
5042
5043 /* Copy over got entries that we may have already seen to the
5044 symbol which just became indirect. */
5045 if (eind->elf.got.glist != NULL)
5046 {
5047 if (edir->elf.got.glist != NULL)
5048 {
5049 struct got_entry **entp;
5050 struct got_entry *ent;
5051
5052 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
5053 {
5054 struct got_entry *dent;
5055
5056 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
5057 if (dent->addend == ent->addend
5058 && dent->owner == ent->owner
5059 && dent->tls_type == ent->tls_type)
5060 {
5061 dent->got.refcount += ent->got.refcount;
5062 *entp = ent->next;
5063 break;
5064 }
5065 if (dent == NULL)
5066 entp = &ent->next;
5067 }
5068 *entp = edir->elf.got.glist;
5069 }
5070
5071 edir->elf.got.glist = eind->elf.got.glist;
5072 eind->elf.got.glist = NULL;
5073 }
5074
5075 /* And plt entries. */
5076 move_plt_plist (eind, edir);
5077
5078 if (eind->elf.dynindx != -1)
5079 {
5080 if (edir->elf.dynindx != -1)
5081 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
5082 edir->elf.dynstr_index);
5083 edir->elf.dynindx = eind->elf.dynindx;
5084 edir->elf.dynstr_index = eind->elf.dynstr_index;
5085 eind->elf.dynindx = -1;
5086 eind->elf.dynstr_index = 0;
5087 }
5088 }
5089
5090 /* Find the function descriptor hash entry from the given function code
5091 hash entry FH. Link the entries via their OH fields. */
5092
5093 static struct ppc_link_hash_entry *
5094 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
5095 {
5096 struct ppc_link_hash_entry *fdh = fh->oh;
5097
5098 if (fdh == NULL)
5099 {
5100 const char *fd_name = fh->elf.root.root.string + 1;
5101
5102 fdh = (struct ppc_link_hash_entry *)
5103 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
5104 if (fdh == NULL)
5105 return fdh;
5106
5107 fdh->is_func_descriptor = 1;
5108 fdh->oh = fh;
5109 fh->is_func = 1;
5110 fh->oh = fdh;
5111 }
5112
5113 fdh = ppc_follow_link (fdh);
5114 fdh->is_func_descriptor = 1;
5115 fdh->oh = fh;
5116 return fdh;
5117 }
5118
5119 /* Make a fake function descriptor sym for the undefined code sym FH. */
5120
5121 static struct ppc_link_hash_entry *
5122 make_fdh (struct bfd_link_info *info,
5123 struct ppc_link_hash_entry *fh)
5124 {
5125 bfd *abfd = fh->elf.root.u.undef.abfd;
5126 struct bfd_link_hash_entry *bh = NULL;
5127 struct ppc_link_hash_entry *fdh;
5128 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5129 ? BSF_WEAK
5130 : BSF_GLOBAL);
5131
5132 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5133 fh->elf.root.root.string + 1,
5134 flags, bfd_und_section_ptr, 0,
5135 NULL, FALSE, FALSE, &bh))
5136 return NULL;
5137
5138 fdh = (struct ppc_link_hash_entry *) bh;
5139 fdh->elf.non_elf = 0;
5140 fdh->fake = 1;
5141 fdh->is_func_descriptor = 1;
5142 fdh->oh = fh;
5143 fh->is_func = 1;
5144 fh->oh = fdh;
5145 return fdh;
5146 }
5147
5148 /* Fix function descriptor symbols defined in .opd sections to be
5149 function type. */
5150
5151 static bfd_boolean
5152 ppc64_elf_add_symbol_hook (bfd *ibfd,
5153 struct bfd_link_info *info,
5154 Elf_Internal_Sym *isym,
5155 const char **name,
5156 flagword *flags ATTRIBUTE_UNUSED,
5157 asection **sec,
5158 bfd_vma *value)
5159 {
5160 if (*sec != NULL
5161 && strcmp ((*sec)->name, ".opd") == 0)
5162 {
5163 asection *code_sec;
5164
5165 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5166 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5167 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5168
5169 /* If the symbol is a function defined in .opd, and the function
5170 code is in a discarded group, let it appear to be undefined. */
5171 if (!bfd_link_relocatable (info)
5172 && (*sec)->reloc_count != 0
5173 && opd_entry_value (*sec, *value, &code_sec, NULL,
5174 FALSE) != (bfd_vma) -1
5175 && discarded_section (code_sec))
5176 {
5177 *sec = bfd_und_section_ptr;
5178 isym->st_shndx = SHN_UNDEF;
5179 }
5180 }
5181 else if (*sec != NULL
5182 && strcmp ((*sec)->name, ".toc") == 0
5183 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5184 {
5185 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5186 if (htab != NULL)
5187 htab->params->object_in_toc = 1;
5188 }
5189
5190 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5191 {
5192 if (abiversion (ibfd) == 0)
5193 set_abiversion (ibfd, 2);
5194 else if (abiversion (ibfd) == 1)
5195 {
5196 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5197 " for ABI version 1"), *name);
5198 bfd_set_error (bfd_error_bad_value);
5199 return FALSE;
5200 }
5201 }
5202
5203 return TRUE;
5204 }
5205
5206 /* Merge non-visibility st_other attributes: local entry point. */
5207
5208 static void
5209 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5210 const Elf_Internal_Sym *isym,
5211 bfd_boolean definition,
5212 bfd_boolean dynamic)
5213 {
5214 if (definition && (!dynamic || !h->def_regular))
5215 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5216 | ELF_ST_VISIBILITY (h->other));
5217 }
5218
5219 /* Hook called on merging a symbol. We use this to clear "fake" since
5220 we now have a real symbol. */
5221
5222 static bfd_boolean
5223 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5224 const Elf_Internal_Sym *isym,
5225 asection **psec ATTRIBUTE_UNUSED,
5226 bfd_boolean newdef ATTRIBUTE_UNUSED,
5227 bfd_boolean olddef ATTRIBUTE_UNUSED,
5228 bfd *oldbfd ATTRIBUTE_UNUSED,
5229 const asection *oldsec ATTRIBUTE_UNUSED)
5230 {
5231 ((struct ppc_link_hash_entry *) h)->fake = 0;
5232 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5233 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5234 return TRUE;
5235 }
5236
5237 /* This function makes an old ABI object reference to ".bar" cause the
5238 inclusion of a new ABI object archive that defines "bar".
5239 NAME is a symbol defined in an archive. Return a symbol in the hash
5240 table that might be satisfied by the archive symbols. */
5241
5242 static struct elf_link_hash_entry *
5243 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5244 struct bfd_link_info *info,
5245 const char *name)
5246 {
5247 struct elf_link_hash_entry *h;
5248 char *dot_name;
5249 size_t len;
5250
5251 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5252 if (h != NULL
5253 /* Don't return this sym if it is a fake function descriptor
5254 created by add_symbol_adjust. */
5255 && !((struct ppc_link_hash_entry *) h)->fake)
5256 return h;
5257
5258 if (name[0] == '.')
5259 return h;
5260
5261 len = strlen (name);
5262 dot_name = bfd_alloc (abfd, len + 2);
5263 if (dot_name == NULL)
5264 return (struct elf_link_hash_entry *) -1;
5265 dot_name[0] = '.';
5266 memcpy (dot_name + 1, name, len + 1);
5267 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5268 bfd_release (abfd, dot_name);
5269 return h;
5270 }
5271
5272 /* This function satisfies all old ABI object references to ".bar" if a
5273 new ABI object defines "bar". Well, at least, undefined dot symbols
5274 are made weak. This stops later archive searches from including an
5275 object if we already have a function descriptor definition. It also
5276 prevents the linker complaining about undefined symbols.
5277 We also check and correct mismatched symbol visibility here. The
5278 most restrictive visibility of the function descriptor and the
5279 function entry symbol is used. */
5280
5281 static bfd_boolean
5282 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5283 {
5284 struct ppc_link_hash_table *htab;
5285 struct ppc_link_hash_entry *fdh;
5286
5287 if (eh->elf.root.type == bfd_link_hash_warning)
5288 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5289
5290 if (eh->elf.root.type == bfd_link_hash_indirect)
5291 return TRUE;
5292
5293 if (eh->elf.root.root.string[0] != '.')
5294 abort ();
5295
5296 htab = ppc_hash_table (info);
5297 if (htab == NULL)
5298 return FALSE;
5299
5300 fdh = lookup_fdh (eh, htab);
5301 if (fdh == NULL
5302 && !bfd_link_relocatable (info)
5303 && (eh->elf.root.type == bfd_link_hash_undefined
5304 || eh->elf.root.type == bfd_link_hash_undefweak)
5305 && eh->elf.ref_regular)
5306 {
5307 /* Make an undefined function descriptor sym, in order to
5308 pull in an --as-needed shared lib. Archives are handled
5309 elsewhere. */
5310 fdh = make_fdh (info, eh);
5311 if (fdh == NULL)
5312 return FALSE;
5313 }
5314
5315 if (fdh != NULL)
5316 {
5317 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5318 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5319
5320 /* Make both descriptor and entry symbol have the most
5321 constraining visibility of either symbol. */
5322 if (entry_vis < descr_vis)
5323 fdh->elf.other += entry_vis - descr_vis;
5324 else if (entry_vis > descr_vis)
5325 eh->elf.other += descr_vis - entry_vis;
5326
5327 /* Propagate reference flags from entry symbol to function
5328 descriptor symbol. */
5329 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5330 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5331 fdh->elf.ref_regular |= eh->elf.ref_regular;
5332 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5333
5334 if (!fdh->elf.forced_local
5335 && fdh->elf.dynindx == -1
5336 && fdh->elf.versioned != versioned_hidden
5337 && (bfd_link_dll (info)
5338 || fdh->elf.def_dynamic
5339 || fdh->elf.ref_dynamic)
5340 && (eh->elf.ref_regular
5341 || eh->elf.def_regular))
5342 {
5343 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5344 return FALSE;
5345 }
5346 }
5347
5348 return TRUE;
5349 }
5350
5351 /* Set up opd section info and abiversion for IBFD, and process list
5352 of dot-symbols we made in link_hash_newfunc. */
5353
5354 static bfd_boolean
5355 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5356 {
5357 struct ppc_link_hash_table *htab;
5358 struct ppc_link_hash_entry **p, *eh;
5359 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5360
5361 if (opd != NULL && opd->size != 0)
5362 {
5363 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5364 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5365
5366 if (abiversion (ibfd) == 0)
5367 set_abiversion (ibfd, 1);
5368 else if (abiversion (ibfd) >= 2)
5369 {
5370 /* xgettext:c-format */
5371 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5372 ibfd, abiversion (ibfd));
5373 bfd_set_error (bfd_error_bad_value);
5374 return FALSE;
5375 }
5376 }
5377
5378 if (is_ppc64_elf (info->output_bfd))
5379 {
5380 /* For input files without an explicit abiversion in e_flags
5381 we should have flagged any with symbol st_other bits set
5382 as ELFv1 and above flagged those with .opd as ELFv2.
5383 Set the output abiversion if not yet set, and for any input
5384 still ambiguous, take its abiversion from the output.
5385 Differences in ABI are reported later. */
5386 if (abiversion (info->output_bfd) == 0)
5387 set_abiversion (info->output_bfd, abiversion (ibfd));
5388 else if (abiversion (ibfd) == 0)
5389 set_abiversion (ibfd, abiversion (info->output_bfd));
5390 }
5391
5392 htab = ppc_hash_table (info);
5393 if (htab == NULL)
5394 return TRUE;
5395
5396 if (opd != NULL && opd->size != 0
5397 && (ibfd->flags & DYNAMIC) == 0
5398 && (opd->flags & SEC_RELOC) != 0
5399 && opd->reloc_count != 0
5400 && !bfd_is_abs_section (opd->output_section)
5401 && info->gc_sections)
5402 {
5403 /* Garbage collection needs some extra help with .opd sections.
5404 We don't want to necessarily keep everything referenced by
5405 relocs in .opd, as that would keep all functions. Instead,
5406 if we reference an .opd symbol (a function descriptor), we
5407 want to keep the function code symbol's section. This is
5408 easy for global symbols, but for local syms we need to keep
5409 information about the associated function section. */
5410 bfd_size_type amt;
5411 asection **opd_sym_map;
5412 Elf_Internal_Shdr *symtab_hdr;
5413 Elf_Internal_Rela *relocs, *rel_end, *rel;
5414
5415 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5416 opd_sym_map = bfd_zalloc (ibfd, amt);
5417 if (opd_sym_map == NULL)
5418 return FALSE;
5419 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5420 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5421 info->keep_memory);
5422 if (relocs == NULL)
5423 return FALSE;
5424 symtab_hdr = &elf_symtab_hdr (ibfd);
5425 rel_end = relocs + opd->reloc_count - 1;
5426 for (rel = relocs; rel < rel_end; rel++)
5427 {
5428 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5429 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5430
5431 if (r_type == R_PPC64_ADDR64
5432 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5433 && r_symndx < symtab_hdr->sh_info)
5434 {
5435 Elf_Internal_Sym *isym;
5436 asection *s;
5437
5438 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5439 if (isym == NULL)
5440 {
5441 if (elf_section_data (opd)->relocs != relocs)
5442 free (relocs);
5443 return FALSE;
5444 }
5445
5446 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5447 if (s != NULL && s != opd)
5448 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5449 }
5450 }
5451 if (elf_section_data (opd)->relocs != relocs)
5452 free (relocs);
5453 }
5454
5455 p = &htab->dot_syms;
5456 while ((eh = *p) != NULL)
5457 {
5458 *p = NULL;
5459 if (&eh->elf == htab->elf.hgot)
5460 ;
5461 else if (htab->elf.hgot == NULL
5462 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5463 htab->elf.hgot = &eh->elf;
5464 else if (abiversion (ibfd) <= 1)
5465 {
5466 htab->need_func_desc_adj = 1;
5467 if (!add_symbol_adjust (eh, info))
5468 return FALSE;
5469 }
5470 p = &eh->u.next_dot_sym;
5471 }
5472 return TRUE;
5473 }
5474
5475 /* Undo hash table changes when an --as-needed input file is determined
5476 not to be needed. */
5477
5478 static bfd_boolean
5479 ppc64_elf_notice_as_needed (bfd *ibfd,
5480 struct bfd_link_info *info,
5481 enum notice_asneeded_action act)
5482 {
5483 if (act == notice_not_needed)
5484 {
5485 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5486
5487 if (htab == NULL)
5488 return FALSE;
5489
5490 htab->dot_syms = NULL;
5491 }
5492 return _bfd_elf_notice_as_needed (ibfd, info, act);
5493 }
5494
5495 /* If --just-symbols against a final linked binary, then assume we need
5496 toc adjusting stubs when calling functions defined there. */
5497
5498 static void
5499 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5500 {
5501 if ((sec->flags & SEC_CODE) != 0
5502 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5503 && is_ppc64_elf (sec->owner))
5504 {
5505 if (abiversion (sec->owner) >= 2
5506 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5507 sec->has_toc_reloc = 1;
5508 }
5509 _bfd_elf_link_just_syms (sec, info);
5510 }
5511
5512 static struct plt_entry **
5513 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5514 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5515 {
5516 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5517 struct plt_entry **local_plt;
5518 unsigned char *local_got_tls_masks;
5519
5520 if (local_got_ents == NULL)
5521 {
5522 bfd_size_type size = symtab_hdr->sh_info;
5523
5524 size *= (sizeof (*local_got_ents)
5525 + sizeof (*local_plt)
5526 + sizeof (*local_got_tls_masks));
5527 local_got_ents = bfd_zalloc (abfd, size);
5528 if (local_got_ents == NULL)
5529 return NULL;
5530 elf_local_got_ents (abfd) = local_got_ents;
5531 }
5532
5533 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5534 {
5535 struct got_entry *ent;
5536
5537 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5538 if (ent->addend == r_addend
5539 && ent->owner == abfd
5540 && ent->tls_type == tls_type)
5541 break;
5542 if (ent == NULL)
5543 {
5544 bfd_size_type amt = sizeof (*ent);
5545 ent = bfd_alloc (abfd, amt);
5546 if (ent == NULL)
5547 return FALSE;
5548 ent->next = local_got_ents[r_symndx];
5549 ent->addend = r_addend;
5550 ent->owner = abfd;
5551 ent->tls_type = tls_type;
5552 ent->is_indirect = FALSE;
5553 ent->got.refcount = 0;
5554 local_got_ents[r_symndx] = ent;
5555 }
5556 ent->got.refcount += 1;
5557 }
5558
5559 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5560 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5561 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5562
5563 return local_plt + r_symndx;
5564 }
5565
5566 static bfd_boolean
5567 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5568 {
5569 struct plt_entry *ent;
5570
5571 for (ent = *plist; ent != NULL; ent = ent->next)
5572 if (ent->addend == addend)
5573 break;
5574 if (ent == NULL)
5575 {
5576 bfd_size_type amt = sizeof (*ent);
5577 ent = bfd_alloc (abfd, amt);
5578 if (ent == NULL)
5579 return FALSE;
5580 ent->next = *plist;
5581 ent->addend = addend;
5582 ent->plt.refcount = 0;
5583 *plist = ent;
5584 }
5585 ent->plt.refcount += 1;
5586 return TRUE;
5587 }
5588
5589 static bfd_boolean
5590 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5591 {
5592 return (r_type == R_PPC64_REL24
5593 || r_type == R_PPC64_REL24_NOTOC
5594 || r_type == R_PPC64_REL14
5595 || r_type == R_PPC64_REL14_BRTAKEN
5596 || r_type == R_PPC64_REL14_BRNTAKEN
5597 || r_type == R_PPC64_ADDR24
5598 || r_type == R_PPC64_ADDR14
5599 || r_type == R_PPC64_ADDR14_BRTAKEN
5600 || r_type == R_PPC64_ADDR14_BRNTAKEN
5601 || r_type == R_PPC64_PLTCALL);
5602 }
5603
5604 /* Relocs on inline plt call sequence insns prior to the call. */
5605
5606 static bfd_boolean
5607 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5608 {
5609 return (r_type == R_PPC64_PLT16_HA
5610 || r_type == R_PPC64_PLT16_HI
5611 || r_type == R_PPC64_PLT16_LO
5612 || r_type == R_PPC64_PLT16_LO_DS
5613 || r_type == R_PPC64_PLTSEQ);
5614 }
5615
5616 /* Look through the relocs for a section during the first phase, and
5617 calculate needed space in the global offset table, procedure
5618 linkage table, and dynamic reloc sections. */
5619
5620 static bfd_boolean
5621 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5622 asection *sec, const Elf_Internal_Rela *relocs)
5623 {
5624 struct ppc_link_hash_table *htab;
5625 Elf_Internal_Shdr *symtab_hdr;
5626 struct elf_link_hash_entry **sym_hashes;
5627 const Elf_Internal_Rela *rel;
5628 const Elf_Internal_Rela *rel_end;
5629 asection *sreloc;
5630 struct elf_link_hash_entry *tga, *dottga;
5631 bfd_boolean is_opd;
5632
5633 if (bfd_link_relocatable (info))
5634 return TRUE;
5635
5636 /* Don't do anything special with non-loaded, non-alloced sections.
5637 In particular, any relocs in such sections should not affect GOT
5638 and PLT reference counting (ie. we don't allow them to create GOT
5639 or PLT entries), there's no possibility or desire to optimize TLS
5640 relocs, and there's not much point in propagating relocs to shared
5641 libs that the dynamic linker won't relocate. */
5642 if ((sec->flags & SEC_ALLOC) == 0)
5643 return TRUE;
5644
5645 BFD_ASSERT (is_ppc64_elf (abfd));
5646
5647 htab = ppc_hash_table (info);
5648 if (htab == NULL)
5649 return FALSE;
5650
5651 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5652 FALSE, FALSE, TRUE);
5653 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5654 FALSE, FALSE, TRUE);
5655 symtab_hdr = &elf_symtab_hdr (abfd);
5656 sym_hashes = elf_sym_hashes (abfd);
5657 sreloc = NULL;
5658 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5659 rel_end = relocs + sec->reloc_count;
5660 for (rel = relocs; rel < rel_end; rel++)
5661 {
5662 unsigned long r_symndx;
5663 struct elf_link_hash_entry *h;
5664 enum elf_ppc64_reloc_type r_type;
5665 int tls_type;
5666 struct _ppc64_elf_section_data *ppc64_sec;
5667 struct plt_entry **ifunc, **plt_list;
5668
5669 r_symndx = ELF64_R_SYM (rel->r_info);
5670 if (r_symndx < symtab_hdr->sh_info)
5671 h = NULL;
5672 else
5673 {
5674 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5675 h = elf_follow_link (h);
5676
5677 if (h == htab->elf.hgot)
5678 sec->has_toc_reloc = 1;
5679 }
5680
5681 tls_type = 0;
5682 ifunc = NULL;
5683 if (h != NULL)
5684 {
5685 if (h->type == STT_GNU_IFUNC)
5686 {
5687 h->needs_plt = 1;
5688 ifunc = &h->plt.plist;
5689 }
5690 }
5691 else
5692 {
5693 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5694 abfd, r_symndx);
5695 if (isym == NULL)
5696 return FALSE;
5697
5698 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5699 {
5700 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5701 rel->r_addend,
5702 NON_GOT | PLT_IFUNC);
5703 if (ifunc == NULL)
5704 return FALSE;
5705 }
5706 }
5707
5708 r_type = ELF64_R_TYPE (rel->r_info);
5709 switch (r_type)
5710 {
5711 case R_PPC64_TLSGD:
5712 case R_PPC64_TLSLD:
5713 /* These special tls relocs tie a call to __tls_get_addr with
5714 its parameter symbol. */
5715 if (h != NULL)
5716 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5717 else
5718 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5719 rel->r_addend,
5720 NON_GOT | TLS_TLS | TLS_MARK))
5721 return FALSE;
5722 sec->has_tls_reloc = 1;
5723 break;
5724
5725 case R_PPC64_GOT_TLSLD16:
5726 case R_PPC64_GOT_TLSLD16_LO:
5727 case R_PPC64_GOT_TLSLD16_HI:
5728 case R_PPC64_GOT_TLSLD16_HA:
5729 tls_type = TLS_TLS | TLS_LD;
5730 goto dogottls;
5731
5732 case R_PPC64_GOT_TLSGD16:
5733 case R_PPC64_GOT_TLSGD16_LO:
5734 case R_PPC64_GOT_TLSGD16_HI:
5735 case R_PPC64_GOT_TLSGD16_HA:
5736 tls_type = TLS_TLS | TLS_GD;
5737 goto dogottls;
5738
5739 case R_PPC64_GOT_TPREL16_DS:
5740 case R_PPC64_GOT_TPREL16_LO_DS:
5741 case R_PPC64_GOT_TPREL16_HI:
5742 case R_PPC64_GOT_TPREL16_HA:
5743 if (bfd_link_dll (info))
5744 info->flags |= DF_STATIC_TLS;
5745 tls_type = TLS_TLS | TLS_TPREL;
5746 goto dogottls;
5747
5748 case R_PPC64_GOT_DTPREL16_DS:
5749 case R_PPC64_GOT_DTPREL16_LO_DS:
5750 case R_PPC64_GOT_DTPREL16_HI:
5751 case R_PPC64_GOT_DTPREL16_HA:
5752 tls_type = TLS_TLS | TLS_DTPREL;
5753 dogottls:
5754 sec->has_tls_reloc = 1;
5755 /* Fall through */
5756
5757 case R_PPC64_GOT16:
5758 case R_PPC64_GOT16_DS:
5759 case R_PPC64_GOT16_HA:
5760 case R_PPC64_GOT16_HI:
5761 case R_PPC64_GOT16_LO:
5762 case R_PPC64_GOT16_LO_DS:
5763 /* This symbol requires a global offset table entry. */
5764 sec->has_toc_reloc = 1;
5765 if (r_type == R_PPC64_GOT_TLSLD16
5766 || r_type == R_PPC64_GOT_TLSGD16
5767 || r_type == R_PPC64_GOT_TPREL16_DS
5768 || r_type == R_PPC64_GOT_DTPREL16_DS
5769 || r_type == R_PPC64_GOT16
5770 || r_type == R_PPC64_GOT16_DS)
5771 {
5772 htab->do_multi_toc = 1;
5773 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5774 }
5775
5776 if (ppc64_elf_tdata (abfd)->got == NULL
5777 && !create_got_section (abfd, info))
5778 return FALSE;
5779
5780 if (h != NULL)
5781 {
5782 struct ppc_link_hash_entry *eh;
5783 struct got_entry *ent;
5784
5785 eh = (struct ppc_link_hash_entry *) h;
5786 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5787 if (ent->addend == rel->r_addend
5788 && ent->owner == abfd
5789 && ent->tls_type == tls_type)
5790 break;
5791 if (ent == NULL)
5792 {
5793 bfd_size_type amt = sizeof (*ent);
5794 ent = bfd_alloc (abfd, amt);
5795 if (ent == NULL)
5796 return FALSE;
5797 ent->next = eh->elf.got.glist;
5798 ent->addend = rel->r_addend;
5799 ent->owner = abfd;
5800 ent->tls_type = tls_type;
5801 ent->is_indirect = FALSE;
5802 ent->got.refcount = 0;
5803 eh->elf.got.glist = ent;
5804 }
5805 ent->got.refcount += 1;
5806 eh->tls_mask |= tls_type;
5807 }
5808 else
5809 /* This is a global offset table entry for a local symbol. */
5810 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5811 rel->r_addend, tls_type))
5812 return FALSE;
5813
5814 /* We may also need a plt entry if the symbol turns out to be
5815 an ifunc. */
5816 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5817 {
5818 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5819 return FALSE;
5820 }
5821 break;
5822
5823 case R_PPC64_PLT16_HA:
5824 case R_PPC64_PLT16_HI:
5825 case R_PPC64_PLT16_LO:
5826 case R_PPC64_PLT16_LO_DS:
5827 case R_PPC64_PLT32:
5828 case R_PPC64_PLT64:
5829 /* This symbol requires a procedure linkage table entry. */
5830 plt_list = ifunc;
5831 if (h != NULL)
5832 {
5833 h->needs_plt = 1;
5834 if (h->root.root.string[0] == '.'
5835 && h->root.root.string[1] != '\0')
5836 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5837 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5838 plt_list = &h->plt.plist;
5839 }
5840 if (plt_list == NULL)
5841 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5842 rel->r_addend,
5843 NON_GOT | PLT_KEEP);
5844 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5845 return FALSE;
5846 break;
5847
5848 /* The following relocations don't need to propagate the
5849 relocation if linking a shared object since they are
5850 section relative. */
5851 case R_PPC64_SECTOFF:
5852 case R_PPC64_SECTOFF_LO:
5853 case R_PPC64_SECTOFF_HI:
5854 case R_PPC64_SECTOFF_HA:
5855 case R_PPC64_SECTOFF_DS:
5856 case R_PPC64_SECTOFF_LO_DS:
5857 case R_PPC64_DTPREL16:
5858 case R_PPC64_DTPREL16_LO:
5859 case R_PPC64_DTPREL16_HI:
5860 case R_PPC64_DTPREL16_HA:
5861 case R_PPC64_DTPREL16_DS:
5862 case R_PPC64_DTPREL16_LO_DS:
5863 case R_PPC64_DTPREL16_HIGH:
5864 case R_PPC64_DTPREL16_HIGHA:
5865 case R_PPC64_DTPREL16_HIGHER:
5866 case R_PPC64_DTPREL16_HIGHERA:
5867 case R_PPC64_DTPREL16_HIGHEST:
5868 case R_PPC64_DTPREL16_HIGHESTA:
5869 break;
5870
5871 /* Nor do these. */
5872 case R_PPC64_REL16:
5873 case R_PPC64_REL16_LO:
5874 case R_PPC64_REL16_HI:
5875 case R_PPC64_REL16_HA:
5876 case R_PPC64_REL16DX_HA:
5877 break;
5878
5879 /* Not supported as a dynamic relocation. */
5880 case R_PPC64_ADDR64_LOCAL:
5881 if (bfd_link_pic (info))
5882 {
5883 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5884 ppc_howto_init ();
5885 /* xgettext:c-format */
5886 info->callbacks->einfo (_("%H: %s reloc unsupported "
5887 "in shared libraries and PIEs\n"),
5888 abfd, sec, rel->r_offset,
5889 ppc64_elf_howto_table[r_type]->name);
5890 bfd_set_error (bfd_error_bad_value);
5891 return FALSE;
5892 }
5893 break;
5894
5895 case R_PPC64_TOC16:
5896 case R_PPC64_TOC16_DS:
5897 htab->do_multi_toc = 1;
5898 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5899 /* Fall through. */
5900 case R_PPC64_TOC16_LO:
5901 case R_PPC64_TOC16_HI:
5902 case R_PPC64_TOC16_HA:
5903 case R_PPC64_TOC16_LO_DS:
5904 sec->has_toc_reloc = 1;
5905 break;
5906
5907 /* Marker reloc. */
5908 case R_PPC64_ENTRY:
5909 break;
5910
5911 /* This relocation describes the C++ object vtable hierarchy.
5912 Reconstruct it for later use during GC. */
5913 case R_PPC64_GNU_VTINHERIT:
5914 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5915 return FALSE;
5916 break;
5917
5918 /* This relocation describes which C++ vtable entries are actually
5919 used. Record for later use during GC. */
5920 case R_PPC64_GNU_VTENTRY:
5921 BFD_ASSERT (h != NULL);
5922 if (h != NULL
5923 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5924 return FALSE;
5925 break;
5926
5927 case R_PPC64_REL14:
5928 case R_PPC64_REL14_BRTAKEN:
5929 case R_PPC64_REL14_BRNTAKEN:
5930 {
5931 asection *dest = NULL;
5932
5933 /* Heuristic: If jumping outside our section, chances are
5934 we are going to need a stub. */
5935 if (h != NULL)
5936 {
5937 /* If the sym is weak it may be overridden later, so
5938 don't assume we know where a weak sym lives. */
5939 if (h->root.type == bfd_link_hash_defined)
5940 dest = h->root.u.def.section;
5941 }
5942 else
5943 {
5944 Elf_Internal_Sym *isym;
5945
5946 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5947 abfd, r_symndx);
5948 if (isym == NULL)
5949 return FALSE;
5950
5951 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5952 }
5953
5954 if (dest != sec)
5955 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5956 }
5957 goto rel24;
5958
5959 case R_PPC64_PLTCALL:
5960 ppc64_elf_section_data (sec)->has_pltcall = 1;
5961 /* Fall through. */
5962
5963 case R_PPC64_REL24:
5964 case R_PPC64_REL24_NOTOC:
5965 rel24:
5966 plt_list = ifunc;
5967 if (h != NULL)
5968 {
5969 h->needs_plt = 1;
5970 if (h->root.root.string[0] == '.'
5971 && h->root.root.string[1] != '\0')
5972 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5973
5974 if (h == tga || h == dottga)
5975 {
5976 sec->has_tls_reloc = 1;
5977 if (rel != relocs
5978 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5979 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5980 /* We have a new-style __tls_get_addr call with
5981 a marker reloc. */
5982 ;
5983 else
5984 /* Mark this section as having an old-style call. */
5985 sec->has_tls_get_addr_call = 1;
5986 }
5987 plt_list = &h->plt.plist;
5988 }
5989
5990 /* We may need a .plt entry if the function this reloc
5991 refers to is in a shared lib. */
5992 if (plt_list
5993 && !update_plt_info (abfd, plt_list, rel->r_addend))
5994 return FALSE;
5995 break;
5996
5997 case R_PPC64_ADDR14:
5998 case R_PPC64_ADDR14_BRNTAKEN:
5999 case R_PPC64_ADDR14_BRTAKEN:
6000 case R_PPC64_ADDR24:
6001 goto dodyn;
6002
6003 case R_PPC64_TPREL64:
6004 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
6005 if (bfd_link_dll (info))
6006 info->flags |= DF_STATIC_TLS;
6007 goto dotlstoc;
6008
6009 case R_PPC64_DTPMOD64:
6010 if (rel + 1 < rel_end
6011 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
6012 && rel[1].r_offset == rel->r_offset + 8)
6013 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
6014 else
6015 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
6016 goto dotlstoc;
6017
6018 case R_PPC64_DTPREL64:
6019 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
6020 if (rel != relocs
6021 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
6022 && rel[-1].r_offset == rel->r_offset - 8)
6023 /* This is the second reloc of a dtpmod, dtprel pair.
6024 Don't mark with TLS_DTPREL. */
6025 goto dodyn;
6026
6027 dotlstoc:
6028 sec->has_tls_reloc = 1;
6029 if (h != NULL)
6030 {
6031 struct ppc_link_hash_entry *eh;
6032 eh = (struct ppc_link_hash_entry *) h;
6033 eh->tls_mask |= tls_type;
6034 }
6035 else
6036 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
6037 rel->r_addend, tls_type))
6038 return FALSE;
6039
6040 ppc64_sec = ppc64_elf_section_data (sec);
6041 if (ppc64_sec->sec_type != sec_toc)
6042 {
6043 bfd_size_type amt;
6044
6045 /* One extra to simplify get_tls_mask. */
6046 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
6047 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
6048 if (ppc64_sec->u.toc.symndx == NULL)
6049 return FALSE;
6050 amt = sec->size * sizeof (bfd_vma) / 8;
6051 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
6052 if (ppc64_sec->u.toc.add == NULL)
6053 return FALSE;
6054 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
6055 ppc64_sec->sec_type = sec_toc;
6056 }
6057 BFD_ASSERT (rel->r_offset % 8 == 0);
6058 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
6059 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
6060
6061 /* Mark the second slot of a GD or LD entry.
6062 -1 to indicate GD and -2 to indicate LD. */
6063 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
6064 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
6065 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
6066 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
6067 goto dodyn;
6068
6069 case R_PPC64_TPREL16:
6070 case R_PPC64_TPREL16_LO:
6071 case R_PPC64_TPREL16_HI:
6072 case R_PPC64_TPREL16_HA:
6073 case R_PPC64_TPREL16_DS:
6074 case R_PPC64_TPREL16_LO_DS:
6075 case R_PPC64_TPREL16_HIGH:
6076 case R_PPC64_TPREL16_HIGHA:
6077 case R_PPC64_TPREL16_HIGHER:
6078 case R_PPC64_TPREL16_HIGHERA:
6079 case R_PPC64_TPREL16_HIGHEST:
6080 case R_PPC64_TPREL16_HIGHESTA:
6081 if (bfd_link_dll (info))
6082 info->flags |= DF_STATIC_TLS;
6083 goto dodyn;
6084
6085 case R_PPC64_ADDR64:
6086 if (is_opd
6087 && rel + 1 < rel_end
6088 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
6089 {
6090 if (h != NULL)
6091 ((struct ppc_link_hash_entry *) h)->is_func = 1;
6092 }
6093 /* Fall through. */
6094
6095 case R_PPC64_ADDR16:
6096 case R_PPC64_ADDR16_DS:
6097 case R_PPC64_ADDR16_HA:
6098 case R_PPC64_ADDR16_HI:
6099 case R_PPC64_ADDR16_HIGH:
6100 case R_PPC64_ADDR16_HIGHA:
6101 case R_PPC64_ADDR16_HIGHER:
6102 case R_PPC64_ADDR16_HIGHERA:
6103 case R_PPC64_ADDR16_HIGHEST:
6104 case R_PPC64_ADDR16_HIGHESTA:
6105 case R_PPC64_ADDR16_LO:
6106 case R_PPC64_ADDR16_LO_DS:
6107 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
6108 && rel->r_addend == 0)
6109 {
6110 /* We may need a .plt entry if this reloc refers to a
6111 function in a shared lib. */
6112 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
6113 return FALSE;
6114 h->pointer_equality_needed = 1;
6115 }
6116 /* Fall through. */
6117
6118 case R_PPC64_REL30:
6119 case R_PPC64_REL32:
6120 case R_PPC64_REL64:
6121 case R_PPC64_ADDR32:
6122 case R_PPC64_UADDR16:
6123 case R_PPC64_UADDR32:
6124 case R_PPC64_UADDR64:
6125 case R_PPC64_TOC:
6126 if (h != NULL && !bfd_link_pic (info))
6127 /* We may need a copy reloc. */
6128 h->non_got_ref = 1;
6129
6130 /* Don't propagate .opd relocs. */
6131 if (NO_OPD_RELOCS && is_opd)
6132 break;
6133
6134 /* If we are creating a shared library, and this is a reloc
6135 against a global symbol, or a non PC relative reloc
6136 against a local symbol, then we need to copy the reloc
6137 into the shared library. However, if we are linking with
6138 -Bsymbolic, we do not need to copy a reloc against a
6139 global symbol which is defined in an object we are
6140 including in the link (i.e., DEF_REGULAR is set). At
6141 this point we have not seen all the input files, so it is
6142 possible that DEF_REGULAR is not set now but will be set
6143 later (it is never cleared). In case of a weak definition,
6144 DEF_REGULAR may be cleared later by a strong definition in
6145 a shared library. We account for that possibility below by
6146 storing information in the dyn_relocs field of the hash
6147 table entry. A similar situation occurs when creating
6148 shared libraries and symbol visibility changes render the
6149 symbol local.
6150
6151 If on the other hand, we are creating an executable, we
6152 may need to keep relocations for symbols satisfied by a
6153 dynamic library if we manage to avoid copy relocs for the
6154 symbol. */
6155 dodyn:
6156 if ((bfd_link_pic (info)
6157 && (must_be_dyn_reloc (info, r_type)
6158 || (h != NULL
6159 && (!SYMBOLIC_BIND (info, h)
6160 || h->root.type == bfd_link_hash_defweak
6161 || !h->def_regular))))
6162 || (ELIMINATE_COPY_RELOCS
6163 && !bfd_link_pic (info)
6164 && h != NULL
6165 && (h->root.type == bfd_link_hash_defweak
6166 || !h->def_regular))
6167 || (!bfd_link_pic (info)
6168 && ifunc != NULL))
6169 {
6170 /* We must copy these reloc types into the output file.
6171 Create a reloc section in dynobj and make room for
6172 this reloc. */
6173 if (sreloc == NULL)
6174 {
6175 sreloc = _bfd_elf_make_dynamic_reloc_section
6176 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6177
6178 if (sreloc == NULL)
6179 return FALSE;
6180 }
6181
6182 /* If this is a global symbol, we count the number of
6183 relocations we need for this symbol. */
6184 if (h != NULL)
6185 {
6186 struct elf_dyn_relocs *p;
6187 struct elf_dyn_relocs **head;
6188
6189 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6190 p = *head;
6191 if (p == NULL || p->sec != sec)
6192 {
6193 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6194 if (p == NULL)
6195 return FALSE;
6196 p->next = *head;
6197 *head = p;
6198 p->sec = sec;
6199 p->count = 0;
6200 p->pc_count = 0;
6201 }
6202 p->count += 1;
6203 if (!must_be_dyn_reloc (info, r_type))
6204 p->pc_count += 1;
6205 }
6206 else
6207 {
6208 /* Track dynamic relocs needed for local syms too.
6209 We really need local syms available to do this
6210 easily. Oh well. */
6211 struct ppc_dyn_relocs *p;
6212 struct ppc_dyn_relocs **head;
6213 bfd_boolean is_ifunc;
6214 asection *s;
6215 void *vpp;
6216 Elf_Internal_Sym *isym;
6217
6218 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6219 abfd, r_symndx);
6220 if (isym == NULL)
6221 return FALSE;
6222
6223 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6224 if (s == NULL)
6225 s = sec;
6226
6227 vpp = &elf_section_data (s)->local_dynrel;
6228 head = (struct ppc_dyn_relocs **) vpp;
6229 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6230 p = *head;
6231 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6232 p = p->next;
6233 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6234 {
6235 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6236 if (p == NULL)
6237 return FALSE;
6238 p->next = *head;
6239 *head = p;
6240 p->sec = sec;
6241 p->ifunc = is_ifunc;
6242 p->count = 0;
6243 }
6244 p->count += 1;
6245 }
6246 }
6247 break;
6248
6249 default:
6250 break;
6251 }
6252 }
6253
6254 return TRUE;
6255 }
6256
6257 /* Merge backend specific data from an object file to the output
6258 object file when linking. */
6259
6260 static bfd_boolean
6261 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6262 {
6263 bfd *obfd = info->output_bfd;
6264 unsigned long iflags, oflags;
6265
6266 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6267 return TRUE;
6268
6269 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6270 return TRUE;
6271
6272 if (!_bfd_generic_verify_endian_match (ibfd, info))
6273 return FALSE;
6274
6275 iflags = elf_elfheader (ibfd)->e_flags;
6276 oflags = elf_elfheader (obfd)->e_flags;
6277
6278 if (iflags & ~EF_PPC64_ABI)
6279 {
6280 _bfd_error_handler
6281 /* xgettext:c-format */
6282 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6283 bfd_set_error (bfd_error_bad_value);
6284 return FALSE;
6285 }
6286 else if (iflags != oflags && iflags != 0)
6287 {
6288 _bfd_error_handler
6289 /* xgettext:c-format */
6290 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6291 ibfd, iflags, oflags);
6292 bfd_set_error (bfd_error_bad_value);
6293 return FALSE;
6294 }
6295
6296 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6297 return FALSE;
6298
6299 /* Merge Tag_compatibility attributes and any common GNU ones. */
6300 return _bfd_elf_merge_object_attributes (ibfd, info);
6301 }
6302
6303 static bfd_boolean
6304 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6305 {
6306 /* Print normal ELF private data. */
6307 _bfd_elf_print_private_bfd_data (abfd, ptr);
6308
6309 if (elf_elfheader (abfd)->e_flags != 0)
6310 {
6311 FILE *file = ptr;
6312
6313 fprintf (file, _("private flags = 0x%lx:"),
6314 elf_elfheader (abfd)->e_flags);
6315
6316 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6317 fprintf (file, _(" [abiv%ld]"),
6318 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6319 fputc ('\n', file);
6320 }
6321
6322 return TRUE;
6323 }
6324
6325 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6326 of the code entry point, and its section, which must be in the same
6327 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6328
6329 static bfd_vma
6330 opd_entry_value (asection *opd_sec,
6331 bfd_vma offset,
6332 asection **code_sec,
6333 bfd_vma *code_off,
6334 bfd_boolean in_code_sec)
6335 {
6336 bfd *opd_bfd = opd_sec->owner;
6337 Elf_Internal_Rela *relocs;
6338 Elf_Internal_Rela *lo, *hi, *look;
6339 bfd_vma val;
6340
6341 /* No relocs implies we are linking a --just-symbols object, or looking
6342 at a final linked executable with addr2line or somesuch. */
6343 if (opd_sec->reloc_count == 0)
6344 {
6345 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6346
6347 if (contents == NULL)
6348 {
6349 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6350 return (bfd_vma) -1;
6351 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6352 }
6353
6354 /* PR 17512: file: 64b9dfbb. */
6355 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6356 return (bfd_vma) -1;
6357
6358 val = bfd_get_64 (opd_bfd, contents + offset);
6359 if (code_sec != NULL)
6360 {
6361 asection *sec, *likely = NULL;
6362
6363 if (in_code_sec)
6364 {
6365 sec = *code_sec;
6366 if (sec->vma <= val
6367 && val < sec->vma + sec->size)
6368 likely = sec;
6369 else
6370 val = -1;
6371 }
6372 else
6373 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6374 if (sec->vma <= val
6375 && (sec->flags & SEC_LOAD) != 0
6376 && (sec->flags & SEC_ALLOC) != 0)
6377 likely = sec;
6378 if (likely != NULL)
6379 {
6380 *code_sec = likely;
6381 if (code_off != NULL)
6382 *code_off = val - likely->vma;
6383 }
6384 }
6385 return val;
6386 }
6387
6388 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6389
6390 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6391 if (relocs == NULL)
6392 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6393 /* PR 17512: file: df8e1fd6. */
6394 if (relocs == NULL)
6395 return (bfd_vma) -1;
6396
6397 /* Go find the opd reloc at the sym address. */
6398 lo = relocs;
6399 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6400 val = (bfd_vma) -1;
6401 while (lo < hi)
6402 {
6403 look = lo + (hi - lo) / 2;
6404 if (look->r_offset < offset)
6405 lo = look + 1;
6406 else if (look->r_offset > offset)
6407 hi = look;
6408 else
6409 {
6410 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6411
6412 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6413 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6414 {
6415 unsigned long symndx = ELF64_R_SYM (look->r_info);
6416 asection *sec = NULL;
6417
6418 if (symndx >= symtab_hdr->sh_info
6419 && elf_sym_hashes (opd_bfd) != NULL)
6420 {
6421 struct elf_link_hash_entry **sym_hashes;
6422 struct elf_link_hash_entry *rh;
6423
6424 sym_hashes = elf_sym_hashes (opd_bfd);
6425 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6426 if (rh != NULL)
6427 {
6428 rh = elf_follow_link (rh);
6429 if (rh->root.type != bfd_link_hash_defined
6430 && rh->root.type != bfd_link_hash_defweak)
6431 break;
6432 if (rh->root.u.def.section->owner == opd_bfd)
6433 {
6434 val = rh->root.u.def.value;
6435 sec = rh->root.u.def.section;
6436 }
6437 }
6438 }
6439
6440 if (sec == NULL)
6441 {
6442 Elf_Internal_Sym *sym;
6443
6444 if (symndx < symtab_hdr->sh_info)
6445 {
6446 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6447 if (sym == NULL)
6448 {
6449 size_t symcnt = symtab_hdr->sh_info;
6450 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6451 symcnt, 0,
6452 NULL, NULL, NULL);
6453 if (sym == NULL)
6454 break;
6455 symtab_hdr->contents = (bfd_byte *) sym;
6456 }
6457 sym += symndx;
6458 }
6459 else
6460 {
6461 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6462 1, symndx,
6463 NULL, NULL, NULL);
6464 if (sym == NULL)
6465 break;
6466 }
6467 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6468 if (sec == NULL)
6469 break;
6470 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6471 val = sym->st_value;
6472 }
6473
6474 val += look->r_addend;
6475 if (code_off != NULL)
6476 *code_off = val;
6477 if (code_sec != NULL)
6478 {
6479 if (in_code_sec && *code_sec != sec)
6480 return -1;
6481 else
6482 *code_sec = sec;
6483 }
6484 if (sec->output_section != NULL)
6485 val += sec->output_section->vma + sec->output_offset;
6486 }
6487 break;
6488 }
6489 }
6490
6491 return val;
6492 }
6493
6494 /* If the ELF symbol SYM might be a function in SEC, return the
6495 function size and set *CODE_OFF to the function's entry point,
6496 otherwise return zero. */
6497
6498 static bfd_size_type
6499 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6500 bfd_vma *code_off)
6501 {
6502 bfd_size_type size;
6503
6504 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6505 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6506 return 0;
6507
6508 size = 0;
6509 if (!(sym->flags & BSF_SYNTHETIC))
6510 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6511
6512 if (strcmp (sym->section->name, ".opd") == 0)
6513 {
6514 struct _opd_sec_data *opd = get_opd_info (sym->section);
6515 bfd_vma symval = sym->value;
6516
6517 if (opd != NULL
6518 && opd->adjust != NULL
6519 && elf_section_data (sym->section)->relocs != NULL)
6520 {
6521 /* opd_entry_value will use cached relocs that have been
6522 adjusted, but with raw symbols. That means both local
6523 and global symbols need adjusting. */
6524 long adjust = opd->adjust[OPD_NDX (symval)];
6525 if (adjust == -1)
6526 return 0;
6527 symval += adjust;
6528 }
6529
6530 if (opd_entry_value (sym->section, symval,
6531 &sec, code_off, TRUE) == (bfd_vma) -1)
6532 return 0;
6533 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6534 symbol. This size has nothing to do with the code size of the
6535 function, which is what we're supposed to return, but the
6536 code size isn't available without looking up the dot-sym.
6537 However, doing that would be a waste of time particularly
6538 since elf_find_function will look at the dot-sym anyway.
6539 Now, elf_find_function will keep the largest size of any
6540 function sym found at the code address of interest, so return
6541 1 here to avoid it incorrectly caching a larger function size
6542 for a small function. This does mean we return the wrong
6543 size for a new-ABI function of size 24, but all that does is
6544 disable caching for such functions. */
6545 if (size == 24)
6546 size = 1;
6547 }
6548 else
6549 {
6550 if (sym->section != sec)
6551 return 0;
6552 *code_off = sym->value;
6553 }
6554 if (size == 0)
6555 size = 1;
6556 return size;
6557 }
6558
6559 /* Return true if symbol is a strong function defined in an ELFv2
6560 object with st_other localentry bits of zero, ie. its local entry
6561 point coincides with its global entry point. */
6562
6563 static bfd_boolean
6564 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6565 {
6566 return (h != NULL
6567 && h->type == STT_FUNC
6568 && h->root.type == bfd_link_hash_defined
6569 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6570 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6571 && is_ppc64_elf (h->root.u.def.section->owner)
6572 && abiversion (h->root.u.def.section->owner) >= 2);
6573 }
6574
6575 /* Return true if symbol is defined in a regular object file. */
6576
6577 static bfd_boolean
6578 is_static_defined (struct elf_link_hash_entry *h)
6579 {
6580 return ((h->root.type == bfd_link_hash_defined
6581 || h->root.type == bfd_link_hash_defweak)
6582 && h->root.u.def.section != NULL
6583 && h->root.u.def.section->output_section != NULL);
6584 }
6585
6586 /* If FDH is a function descriptor symbol, return the associated code
6587 entry symbol if it is defined. Return NULL otherwise. */
6588
6589 static struct ppc_link_hash_entry *
6590 defined_code_entry (struct ppc_link_hash_entry *fdh)
6591 {
6592 if (fdh->is_func_descriptor)
6593 {
6594 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6595 if (fh->elf.root.type == bfd_link_hash_defined
6596 || fh->elf.root.type == bfd_link_hash_defweak)
6597 return fh;
6598 }
6599 return NULL;
6600 }
6601
6602 /* If FH is a function code entry symbol, return the associated
6603 function descriptor symbol if it is defined. Return NULL otherwise. */
6604
6605 static struct ppc_link_hash_entry *
6606 defined_func_desc (struct ppc_link_hash_entry *fh)
6607 {
6608 if (fh->oh != NULL
6609 && fh->oh->is_func_descriptor)
6610 {
6611 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6612 if (fdh->elf.root.type == bfd_link_hash_defined
6613 || fdh->elf.root.type == bfd_link_hash_defweak)
6614 return fdh;
6615 }
6616 return NULL;
6617 }
6618
6619 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6620
6621 /* Garbage collect sections, after first dealing with dot-symbols. */
6622
6623 static bfd_boolean
6624 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6625 {
6626 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6627
6628 if (htab != NULL && htab->need_func_desc_adj)
6629 {
6630 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6631 htab->need_func_desc_adj = 0;
6632 }
6633 return bfd_elf_gc_sections (abfd, info);
6634 }
6635
6636 /* Mark all our entry sym sections, both opd and code section. */
6637
6638 static void
6639 ppc64_elf_gc_keep (struct bfd_link_info *info)
6640 {
6641 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6642 struct bfd_sym_chain *sym;
6643
6644 if (htab == NULL)
6645 return;
6646
6647 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6648 {
6649 struct ppc_link_hash_entry *eh, *fh;
6650 asection *sec;
6651
6652 eh = (struct ppc_link_hash_entry *)
6653 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6654 if (eh == NULL)
6655 continue;
6656 if (eh->elf.root.type != bfd_link_hash_defined
6657 && eh->elf.root.type != bfd_link_hash_defweak)
6658 continue;
6659
6660 fh = defined_code_entry (eh);
6661 if (fh != NULL)
6662 {
6663 sec = fh->elf.root.u.def.section;
6664 sec->flags |= SEC_KEEP;
6665 }
6666 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6667 && opd_entry_value (eh->elf.root.u.def.section,
6668 eh->elf.root.u.def.value,
6669 &sec, NULL, FALSE) != (bfd_vma) -1)
6670 sec->flags |= SEC_KEEP;
6671
6672 sec = eh->elf.root.u.def.section;
6673 sec->flags |= SEC_KEEP;
6674 }
6675 }
6676
6677 /* Mark sections containing dynamically referenced symbols. When
6678 building shared libraries, we must assume that any visible symbol is
6679 referenced. */
6680
6681 static bfd_boolean
6682 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6683 {
6684 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6685 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6686 struct ppc_link_hash_entry *fdh;
6687 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6688
6689 /* Dynamic linking info is on the func descriptor sym. */
6690 fdh = defined_func_desc (eh);
6691 if (fdh != NULL)
6692 eh = fdh;
6693
6694 if ((eh->elf.root.type == bfd_link_hash_defined
6695 || eh->elf.root.type == bfd_link_hash_defweak)
6696 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6697 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6698 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6699 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6700 && (!bfd_link_executable (info)
6701 || info->gc_keep_exported
6702 || info->export_dynamic
6703 || (eh->elf.dynamic
6704 && d != NULL
6705 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6706 && (eh->elf.versioned >= versioned
6707 || !bfd_hide_sym_by_version (info->version_info,
6708 eh->elf.root.root.string)))))
6709 {
6710 asection *code_sec;
6711 struct ppc_link_hash_entry *fh;
6712
6713 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6714
6715 /* Function descriptor syms cause the associated
6716 function code sym section to be marked. */
6717 fh = defined_code_entry (eh);
6718 if (fh != NULL)
6719 {
6720 code_sec = fh->elf.root.u.def.section;
6721 code_sec->flags |= SEC_KEEP;
6722 }
6723 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6724 && opd_entry_value (eh->elf.root.u.def.section,
6725 eh->elf.root.u.def.value,
6726 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6727 code_sec->flags |= SEC_KEEP;
6728 }
6729
6730 return TRUE;
6731 }
6732
6733 /* Return the section that should be marked against GC for a given
6734 relocation. */
6735
6736 static asection *
6737 ppc64_elf_gc_mark_hook (asection *sec,
6738 struct bfd_link_info *info,
6739 Elf_Internal_Rela *rel,
6740 struct elf_link_hash_entry *h,
6741 Elf_Internal_Sym *sym)
6742 {
6743 asection *rsec;
6744
6745 /* Syms return NULL if we're marking .opd, so we avoid marking all
6746 function sections, as all functions are referenced in .opd. */
6747 rsec = NULL;
6748 if (get_opd_info (sec) != NULL)
6749 return rsec;
6750
6751 if (h != NULL)
6752 {
6753 enum elf_ppc64_reloc_type r_type;
6754 struct ppc_link_hash_entry *eh, *fh, *fdh;
6755
6756 r_type = ELF64_R_TYPE (rel->r_info);
6757 switch (r_type)
6758 {
6759 case R_PPC64_GNU_VTINHERIT:
6760 case R_PPC64_GNU_VTENTRY:
6761 break;
6762
6763 default:
6764 switch (h->root.type)
6765 {
6766 case bfd_link_hash_defined:
6767 case bfd_link_hash_defweak:
6768 eh = (struct ppc_link_hash_entry *) h;
6769 fdh = defined_func_desc (eh);
6770 if (fdh != NULL)
6771 {
6772 /* -mcall-aixdesc code references the dot-symbol on
6773 a call reloc. Mark the function descriptor too
6774 against garbage collection. */
6775 fdh->elf.mark = 1;
6776 if (fdh->elf.is_weakalias)
6777 weakdef (&fdh->elf)->mark = 1;
6778 eh = fdh;
6779 }
6780
6781 /* Function descriptor syms cause the associated
6782 function code sym section to be marked. */
6783 fh = defined_code_entry (eh);
6784 if (fh != NULL)
6785 {
6786 /* They also mark their opd section. */
6787 eh->elf.root.u.def.section->gc_mark = 1;
6788
6789 rsec = fh->elf.root.u.def.section;
6790 }
6791 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6792 && opd_entry_value (eh->elf.root.u.def.section,
6793 eh->elf.root.u.def.value,
6794 &rsec, NULL, FALSE) != (bfd_vma) -1)
6795 eh->elf.root.u.def.section->gc_mark = 1;
6796 else
6797 rsec = h->root.u.def.section;
6798 break;
6799
6800 case bfd_link_hash_common:
6801 rsec = h->root.u.c.p->section;
6802 break;
6803
6804 default:
6805 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6806 }
6807 }
6808 }
6809 else
6810 {
6811 struct _opd_sec_data *opd;
6812
6813 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6814 opd = get_opd_info (rsec);
6815 if (opd != NULL && opd->func_sec != NULL)
6816 {
6817 rsec->gc_mark = 1;
6818
6819 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6820 }
6821 }
6822
6823 return rsec;
6824 }
6825
6826 /* The maximum size of .sfpr. */
6827 #define SFPR_MAX (218*4)
6828
6829 struct sfpr_def_parms
6830 {
6831 const char name[12];
6832 unsigned char lo, hi;
6833 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6834 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6835 };
6836
6837 /* Auto-generate _save*, _rest* functions in .sfpr.
6838 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6839 instead. */
6840
6841 static bfd_boolean
6842 sfpr_define (struct bfd_link_info *info,
6843 const struct sfpr_def_parms *parm,
6844 asection *stub_sec)
6845 {
6846 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6847 unsigned int i;
6848 size_t len = strlen (parm->name);
6849 bfd_boolean writing = FALSE;
6850 char sym[16];
6851
6852 if (htab == NULL)
6853 return FALSE;
6854
6855 memcpy (sym, parm->name, len);
6856 sym[len + 2] = 0;
6857
6858 for (i = parm->lo; i <= parm->hi; i++)
6859 {
6860 struct ppc_link_hash_entry *h;
6861
6862 sym[len + 0] = i / 10 + '0';
6863 sym[len + 1] = i % 10 + '0';
6864 h = (struct ppc_link_hash_entry *)
6865 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6866 if (stub_sec != NULL)
6867 {
6868 if (h != NULL
6869 && h->elf.root.type == bfd_link_hash_defined
6870 && h->elf.root.u.def.section == htab->sfpr)
6871 {
6872 struct elf_link_hash_entry *s;
6873 char buf[32];
6874 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6875 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6876 if (s == NULL)
6877 return FALSE;
6878 if (s->root.type == bfd_link_hash_new
6879 || (s->root.type = bfd_link_hash_defined
6880 && s->root.u.def.section == stub_sec))
6881 {
6882 s->root.type = bfd_link_hash_defined;
6883 s->root.u.def.section = stub_sec;
6884 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6885 + h->elf.root.u.def.value);
6886 s->ref_regular = 1;
6887 s->def_regular = 1;
6888 s->ref_regular_nonweak = 1;
6889 s->forced_local = 1;
6890 s->non_elf = 0;
6891 s->root.linker_def = 1;
6892 }
6893 }
6894 continue;
6895 }
6896 if (h != NULL)
6897 {
6898 h->save_res = 1;
6899 if (!h->elf.def_regular)
6900 {
6901 h->elf.root.type = bfd_link_hash_defined;
6902 h->elf.root.u.def.section = htab->sfpr;
6903 h->elf.root.u.def.value = htab->sfpr->size;
6904 h->elf.type = STT_FUNC;
6905 h->elf.def_regular = 1;
6906 h->elf.non_elf = 0;
6907 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6908 writing = TRUE;
6909 if (htab->sfpr->contents == NULL)
6910 {
6911 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6912 if (htab->sfpr->contents == NULL)
6913 return FALSE;
6914 }
6915 }
6916 }
6917 if (writing)
6918 {
6919 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6920 if (i != parm->hi)
6921 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6922 else
6923 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6924 htab->sfpr->size = p - htab->sfpr->contents;
6925 }
6926 }
6927
6928 return TRUE;
6929 }
6930
6931 static bfd_byte *
6932 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6933 {
6934 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6935 return p + 4;
6936 }
6937
6938 static bfd_byte *
6939 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6940 {
6941 p = savegpr0 (abfd, p, r);
6942 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6943 p = p + 4;
6944 bfd_put_32 (abfd, BLR, p);
6945 return p + 4;
6946 }
6947
6948 static bfd_byte *
6949 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6950 {
6951 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6952 return p + 4;
6953 }
6954
6955 static bfd_byte *
6956 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6957 {
6958 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6959 p = p + 4;
6960 p = restgpr0 (abfd, p, r);
6961 bfd_put_32 (abfd, MTLR_R0, p);
6962 p = p + 4;
6963 if (r == 29)
6964 {
6965 p = restgpr0 (abfd, p, 30);
6966 p = restgpr0 (abfd, p, 31);
6967 }
6968 bfd_put_32 (abfd, BLR, p);
6969 return p + 4;
6970 }
6971
6972 static bfd_byte *
6973 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6974 {
6975 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6976 return p + 4;
6977 }
6978
6979 static bfd_byte *
6980 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6981 {
6982 p = savegpr1 (abfd, p, r);
6983 bfd_put_32 (abfd, BLR, p);
6984 return p + 4;
6985 }
6986
6987 static bfd_byte *
6988 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6989 {
6990 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6991 return p + 4;
6992 }
6993
6994 static bfd_byte *
6995 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6996 {
6997 p = restgpr1 (abfd, p, r);
6998 bfd_put_32 (abfd, BLR, p);
6999 return p + 4;
7000 }
7001
7002 static bfd_byte *
7003 savefpr (bfd *abfd, bfd_byte *p, int r)
7004 {
7005 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7006 return p + 4;
7007 }
7008
7009 static bfd_byte *
7010 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
7011 {
7012 p = savefpr (abfd, p, r);
7013 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
7014 p = p + 4;
7015 bfd_put_32 (abfd, BLR, p);
7016 return p + 4;
7017 }
7018
7019 static bfd_byte *
7020 restfpr (bfd *abfd, bfd_byte *p, int r)
7021 {
7022 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7023 return p + 4;
7024 }
7025
7026 static bfd_byte *
7027 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
7028 {
7029 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
7030 p = p + 4;
7031 p = restfpr (abfd, p, r);
7032 bfd_put_32 (abfd, MTLR_R0, p);
7033 p = p + 4;
7034 if (r == 29)
7035 {
7036 p = restfpr (abfd, p, 30);
7037 p = restfpr (abfd, p, 31);
7038 }
7039 bfd_put_32 (abfd, BLR, p);
7040 return p + 4;
7041 }
7042
7043 static bfd_byte *
7044 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
7045 {
7046 p = savefpr (abfd, p, r);
7047 bfd_put_32 (abfd, BLR, p);
7048 return p + 4;
7049 }
7050
7051 static bfd_byte *
7052 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
7053 {
7054 p = restfpr (abfd, p, r);
7055 bfd_put_32 (abfd, BLR, p);
7056 return p + 4;
7057 }
7058
7059 static bfd_byte *
7060 savevr (bfd *abfd, bfd_byte *p, int r)
7061 {
7062 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7063 p = p + 4;
7064 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
7065 return p + 4;
7066 }
7067
7068 static bfd_byte *
7069 savevr_tail (bfd *abfd, bfd_byte *p, int r)
7070 {
7071 p = savevr (abfd, p, r);
7072 bfd_put_32 (abfd, BLR, p);
7073 return p + 4;
7074 }
7075
7076 static bfd_byte *
7077 restvr (bfd *abfd, bfd_byte *p, int r)
7078 {
7079 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7080 p = p + 4;
7081 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
7082 return p + 4;
7083 }
7084
7085 static bfd_byte *
7086 restvr_tail (bfd *abfd, bfd_byte *p, int r)
7087 {
7088 p = restvr (abfd, p, r);
7089 bfd_put_32 (abfd, BLR, p);
7090 return p + 4;
7091 }
7092
7093 /* Called via elf_link_hash_traverse to transfer dynamic linking
7094 information on function code symbol entries to their corresponding
7095 function descriptor symbol entries. */
7096
7097 static bfd_boolean
7098 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
7099 {
7100 struct bfd_link_info *info;
7101 struct ppc_link_hash_table *htab;
7102 struct ppc_link_hash_entry *fh;
7103 struct ppc_link_hash_entry *fdh;
7104 bfd_boolean force_local;
7105
7106 fh = (struct ppc_link_hash_entry *) h;
7107 if (fh->elf.root.type == bfd_link_hash_indirect)
7108 return TRUE;
7109
7110 if (!fh->is_func)
7111 return TRUE;
7112
7113 if (fh->elf.root.root.string[0] != '.'
7114 || fh->elf.root.root.string[1] == '\0')
7115 return TRUE;
7116
7117 info = inf;
7118 htab = ppc_hash_table (info);
7119 if (htab == NULL)
7120 return FALSE;
7121
7122 /* Find the corresponding function descriptor symbol. */
7123 fdh = lookup_fdh (fh, htab);
7124
7125 /* Resolve undefined references to dot-symbols as the value
7126 in the function descriptor, if we have one in a regular object.
7127 This is to satisfy cases like ".quad .foo". Calls to functions
7128 in dynamic objects are handled elsewhere. */
7129 if ((fh->elf.root.type == bfd_link_hash_undefined
7130 || fh->elf.root.type == bfd_link_hash_undefweak)
7131 && (fdh->elf.root.type == bfd_link_hash_defined
7132 || fdh->elf.root.type == bfd_link_hash_defweak)
7133 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7134 && opd_entry_value (fdh->elf.root.u.def.section,
7135 fdh->elf.root.u.def.value,
7136 &fh->elf.root.u.def.section,
7137 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7138 {
7139 fh->elf.root.type = fdh->elf.root.type;
7140 fh->elf.forced_local = 1;
7141 fh->elf.def_regular = fdh->elf.def_regular;
7142 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7143 }
7144
7145 if (!fh->elf.dynamic)
7146 {
7147 struct plt_entry *ent;
7148
7149 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7150 if (ent->plt.refcount > 0)
7151 break;
7152 if (ent == NULL)
7153 return TRUE;
7154 }
7155
7156 /* Create a descriptor as undefined if necessary. */
7157 if (fdh == NULL
7158 && !bfd_link_executable (info)
7159 && (fh->elf.root.type == bfd_link_hash_undefined
7160 || fh->elf.root.type == bfd_link_hash_undefweak))
7161 {
7162 fdh = make_fdh (info, fh);
7163 if (fdh == NULL)
7164 return FALSE;
7165 }
7166
7167 /* We can't support overriding of symbols on a fake descriptor. */
7168 if (fdh != NULL
7169 && fdh->fake
7170 && (fh->elf.root.type == bfd_link_hash_defined
7171 || fh->elf.root.type == bfd_link_hash_defweak))
7172 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7173
7174 /* Transfer dynamic linking information to the function descriptor. */
7175 if (fdh != NULL)
7176 {
7177 fdh->elf.ref_regular |= fh->elf.ref_regular;
7178 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7179 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7180 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7181 fdh->elf.dynamic |= fh->elf.dynamic;
7182 fdh->elf.needs_plt |= (fh->elf.needs_plt
7183 || fh->elf.type == STT_FUNC
7184 || fh->elf.type == STT_GNU_IFUNC);
7185 move_plt_plist (fh, fdh);
7186
7187 if (!fdh->elf.forced_local
7188 && fh->elf.dynindx != -1)
7189 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7190 return FALSE;
7191 }
7192
7193 /* Now that the info is on the function descriptor, clear the
7194 function code sym info. Any function code syms for which we
7195 don't have a definition in a regular file, we force local.
7196 This prevents a shared library from exporting syms that have
7197 been imported from another library. Function code syms that
7198 are really in the library we must leave global to prevent the
7199 linker dragging in a definition from a static library. */
7200 force_local = (!fh->elf.def_regular
7201 || fdh == NULL
7202 || !fdh->elf.def_regular
7203 || fdh->elf.forced_local);
7204 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7205
7206 return TRUE;
7207 }
7208
7209 static const struct sfpr_def_parms save_res_funcs[] =
7210 {
7211 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7212 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7213 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7214 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7215 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7216 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7217 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7218 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7219 { "._savef", 14, 31, savefpr, savefpr1_tail },
7220 { "._restf", 14, 31, restfpr, restfpr1_tail },
7221 { "_savevr_", 20, 31, savevr, savevr_tail },
7222 { "_restvr_", 20, 31, restvr, restvr_tail }
7223 };
7224
7225 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7226 this hook to a) provide some gcc support functions, and b) transfer
7227 dynamic linking information gathered so far on function code symbol
7228 entries, to their corresponding function descriptor symbol entries. */
7229
7230 static bfd_boolean
7231 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7232 struct bfd_link_info *info)
7233 {
7234 struct ppc_link_hash_table *htab;
7235
7236 htab = ppc_hash_table (info);
7237 if (htab == NULL)
7238 return FALSE;
7239
7240 /* Provide any missing _save* and _rest* functions. */
7241 if (htab->sfpr != NULL)
7242 {
7243 unsigned int i;
7244
7245 htab->sfpr->size = 0;
7246 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7247 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7248 return FALSE;
7249 if (htab->sfpr->size == 0)
7250 htab->sfpr->flags |= SEC_EXCLUDE;
7251 }
7252
7253 if (bfd_link_relocatable (info))
7254 return TRUE;
7255
7256 if (htab->elf.hgot != NULL)
7257 {
7258 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7259 /* Make .TOC. defined so as to prevent it being made dynamic.
7260 The wrong value here is fixed later in ppc64_elf_set_toc. */
7261 if (!htab->elf.hgot->def_regular
7262 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7263 {
7264 htab->elf.hgot->root.type = bfd_link_hash_defined;
7265 htab->elf.hgot->root.u.def.value = 0;
7266 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7267 htab->elf.hgot->def_regular = 1;
7268 htab->elf.hgot->root.linker_def = 1;
7269 }
7270 htab->elf.hgot->type = STT_OBJECT;
7271 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7272 | STV_HIDDEN);
7273 }
7274
7275 if (htab->need_func_desc_adj)
7276 {
7277 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7278 htab->need_func_desc_adj = 0;
7279 }
7280
7281 return TRUE;
7282 }
7283
7284 /* Find dynamic relocs for H that apply to read-only sections. */
7285
7286 static asection *
7287 readonly_dynrelocs (struct elf_link_hash_entry *h)
7288 {
7289 struct ppc_link_hash_entry *eh;
7290 struct elf_dyn_relocs *p;
7291
7292 eh = (struct ppc_link_hash_entry *) h;
7293 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7294 {
7295 asection *s = p->sec->output_section;
7296
7297 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7298 return p->sec;
7299 }
7300 return NULL;
7301 }
7302
7303 /* Return true if we have dynamic relocs against H or any of its weak
7304 aliases, that apply to read-only sections. Cannot be used after
7305 size_dynamic_sections. */
7306
7307 static bfd_boolean
7308 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7309 {
7310 struct ppc_link_hash_entry *eh;
7311
7312 eh = (struct ppc_link_hash_entry *) h;
7313 do
7314 {
7315 if (readonly_dynrelocs (&eh->elf))
7316 return TRUE;
7317 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7318 } while (eh != NULL && &eh->elf != h);
7319
7320 return FALSE;
7321 }
7322
7323 /* Return whether EH has pc-relative dynamic relocs. */
7324
7325 static bfd_boolean
7326 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7327 {
7328 struct elf_dyn_relocs *p;
7329
7330 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7331 if (p->pc_count != 0)
7332 return TRUE;
7333 return FALSE;
7334 }
7335
7336 /* Return true if a global entry stub will be created for H. Valid
7337 for ELFv2 before plt entries have been allocated. */
7338
7339 static bfd_boolean
7340 global_entry_stub (struct elf_link_hash_entry *h)
7341 {
7342 struct plt_entry *pent;
7343
7344 if (!h->pointer_equality_needed
7345 || h->def_regular)
7346 return FALSE;
7347
7348 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7349 if (pent->plt.refcount > 0
7350 && pent->addend == 0)
7351 return TRUE;
7352
7353 return FALSE;
7354 }
7355
7356 /* Adjust a symbol defined by a dynamic object and referenced by a
7357 regular object. The current definition is in some section of the
7358 dynamic object, but we're not including those sections. We have to
7359 change the definition to something the rest of the link can
7360 understand. */
7361
7362 static bfd_boolean
7363 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7364 struct elf_link_hash_entry *h)
7365 {
7366 struct ppc_link_hash_table *htab;
7367 asection *s, *srel;
7368
7369 htab = ppc_hash_table (info);
7370 if (htab == NULL)
7371 return FALSE;
7372
7373 /* Deal with function syms. */
7374 if (h->type == STT_FUNC
7375 || h->type == STT_GNU_IFUNC
7376 || h->needs_plt)
7377 {
7378 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7379 || SYMBOL_CALLS_LOCAL (info, h)
7380 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7381 /* Discard dyn_relocs when non-pic if we've decided that a
7382 function symbol is local and not an ifunc. We keep dynamic
7383 relocs for ifuncs when local rather than always emitting a
7384 plt call stub for them and defining the symbol on the call
7385 stub. We can't do that for ELFv1 anyway (a function symbol
7386 is defined on a descriptor, not code) and it can be faster at
7387 run-time due to not needing to bounce through a stub. The
7388 dyn_relocs for ifuncs will be applied even in a static
7389 executable. */
7390 if (!bfd_link_pic (info)
7391 && h->type != STT_GNU_IFUNC
7392 && local)
7393 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7394
7395 /* Clear procedure linkage table information for any symbol that
7396 won't need a .plt entry. */
7397 struct plt_entry *ent;
7398 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7399 if (ent->plt.refcount > 0)
7400 break;
7401 if (ent == NULL
7402 || (h->type != STT_GNU_IFUNC
7403 && local
7404 && (htab->can_convert_all_inline_plt
7405 || (((struct ppc_link_hash_entry *) h)->tls_mask
7406 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7407 {
7408 h->plt.plist = NULL;
7409 h->needs_plt = 0;
7410 h->pointer_equality_needed = 0;
7411 }
7412 else if (abiversion (info->output_bfd) >= 2)
7413 {
7414 /* Taking a function's address in a read/write section
7415 doesn't require us to define the function symbol in the
7416 executable on a global entry stub. A dynamic reloc can
7417 be used instead. The reason we prefer a few more dynamic
7418 relocs is that calling via a global entry stub costs a
7419 few more instructions, and pointer_equality_needed causes
7420 extra work in ld.so when resolving these symbols. */
7421 if (global_entry_stub (h))
7422 {
7423 if (!readonly_dynrelocs (h))
7424 {
7425 h->pointer_equality_needed = 0;
7426 /* If we haven't seen a branch reloc and the symbol
7427 isn't an ifunc then we don't need a plt entry. */
7428 if (!h->needs_plt)
7429 h->plt.plist = NULL;
7430 }
7431 else if (!bfd_link_pic (info))
7432 /* We are going to be defining the function symbol on the
7433 plt stub, so no dyn_relocs needed when non-pic. */
7434 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7435 }
7436
7437 /* ELFv2 function symbols can't have copy relocs. */
7438 return TRUE;
7439 }
7440 else if (!h->needs_plt
7441 && !readonly_dynrelocs (h))
7442 {
7443 /* If we haven't seen a branch reloc and the symbol isn't an
7444 ifunc then we don't need a plt entry. */
7445 h->plt.plist = NULL;
7446 h->pointer_equality_needed = 0;
7447 return TRUE;
7448 }
7449 }
7450 else
7451 h->plt.plist = NULL;
7452
7453 /* If this is a weak symbol, and there is a real definition, the
7454 processor independent code will have arranged for us to see the
7455 real definition first, and we can just use the same value. */
7456 if (h->is_weakalias)
7457 {
7458 struct elf_link_hash_entry *def = weakdef (h);
7459 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7460 h->root.u.def.section = def->root.u.def.section;
7461 h->root.u.def.value = def->root.u.def.value;
7462 if (def->root.u.def.section == htab->elf.sdynbss
7463 || def->root.u.def.section == htab->elf.sdynrelro)
7464 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7465 return TRUE;
7466 }
7467
7468 /* If we are creating a shared library, we must presume that the
7469 only references to the symbol are via the global offset table.
7470 For such cases we need not do anything here; the relocations will
7471 be handled correctly by relocate_section. */
7472 if (bfd_link_pic (info))
7473 return TRUE;
7474
7475 /* If there are no references to this symbol that do not use the
7476 GOT, we don't need to generate a copy reloc. */
7477 if (!h->non_got_ref)
7478 return TRUE;
7479
7480 /* Don't generate a copy reloc for symbols defined in the executable. */
7481 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7482
7483 /* If -z nocopyreloc was given, don't generate them either. */
7484 || info->nocopyreloc
7485
7486 /* If we don't find any dynamic relocs in read-only sections, then
7487 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7488 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7489
7490 /* Protected variables do not work with .dynbss. The copy in
7491 .dynbss won't be used by the shared library with the protected
7492 definition for the variable. Text relocations are preferable
7493 to an incorrect program. */
7494 || h->protected_def)
7495 return TRUE;
7496
7497 if (h->plt.plist != NULL)
7498 {
7499 /* We should never get here, but unfortunately there are versions
7500 of gcc out there that improperly (for this ABI) put initialized
7501 function pointers, vtable refs and suchlike in read-only
7502 sections. Allow them to proceed, but warn that this might
7503 break at runtime. */
7504 info->callbacks->einfo
7505 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7506 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7507 h->root.root.string);
7508 }
7509
7510 /* This is a reference to a symbol defined by a dynamic object which
7511 is not a function. */
7512
7513 /* We must allocate the symbol in our .dynbss section, which will
7514 become part of the .bss section of the executable. There will be
7515 an entry for this symbol in the .dynsym section. The dynamic
7516 object will contain position independent code, so all references
7517 from the dynamic object to this symbol will go through the global
7518 offset table. The dynamic linker will use the .dynsym entry to
7519 determine the address it must put in the global offset table, so
7520 both the dynamic object and the regular object will refer to the
7521 same memory location for the variable. */
7522 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7523 {
7524 s = htab->elf.sdynrelro;
7525 srel = htab->elf.sreldynrelro;
7526 }
7527 else
7528 {
7529 s = htab->elf.sdynbss;
7530 srel = htab->elf.srelbss;
7531 }
7532 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7533 {
7534 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7535 linker to copy the initial value out of the dynamic object
7536 and into the runtime process image. */
7537 srel->size += sizeof (Elf64_External_Rela);
7538 h->needs_copy = 1;
7539 }
7540
7541 /* We no longer want dyn_relocs. */
7542 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7543 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7544 }
7545
7546 /* If given a function descriptor symbol, hide both the function code
7547 sym and the descriptor. */
7548 static void
7549 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7550 struct elf_link_hash_entry *h,
7551 bfd_boolean force_local)
7552 {
7553 struct ppc_link_hash_entry *eh;
7554 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7555
7556 eh = (struct ppc_link_hash_entry *) h;
7557 if (eh->is_func_descriptor)
7558 {
7559 struct ppc_link_hash_entry *fh = eh->oh;
7560
7561 if (fh == NULL)
7562 {
7563 const char *p, *q;
7564 struct elf_link_hash_table *htab = elf_hash_table (info);
7565 char save;
7566
7567 /* We aren't supposed to use alloca in BFD because on
7568 systems which do not have alloca the version in libiberty
7569 calls xmalloc, which might cause the program to crash
7570 when it runs out of memory. This function doesn't have a
7571 return status, so there's no way to gracefully return an
7572 error. So cheat. We know that string[-1] can be safely
7573 accessed; It's either a string in an ELF string table,
7574 or allocated in an objalloc structure. */
7575
7576 p = eh->elf.root.root.string - 1;
7577 save = *p;
7578 *(char *) p = '.';
7579 fh = (struct ppc_link_hash_entry *)
7580 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7581 *(char *) p = save;
7582
7583 /* Unfortunately, if it so happens that the string we were
7584 looking for was allocated immediately before this string,
7585 then we overwrote the string terminator. That's the only
7586 reason the lookup should fail. */
7587 if (fh == NULL)
7588 {
7589 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7590 while (q >= eh->elf.root.root.string && *q == *p)
7591 --q, --p;
7592 if (q < eh->elf.root.root.string && *p == '.')
7593 fh = (struct ppc_link_hash_entry *)
7594 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7595 }
7596 if (fh != NULL)
7597 {
7598 eh->oh = fh;
7599 fh->oh = eh;
7600 }
7601 }
7602 if (fh != NULL)
7603 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7604 }
7605 }
7606
7607 static bfd_boolean
7608 get_sym_h (struct elf_link_hash_entry **hp,
7609 Elf_Internal_Sym **symp,
7610 asection **symsecp,
7611 unsigned char **tls_maskp,
7612 Elf_Internal_Sym **locsymsp,
7613 unsigned long r_symndx,
7614 bfd *ibfd)
7615 {
7616 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7617
7618 if (r_symndx >= symtab_hdr->sh_info)
7619 {
7620 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7621 struct elf_link_hash_entry *h;
7622
7623 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7624 h = elf_follow_link (h);
7625
7626 if (hp != NULL)
7627 *hp = h;
7628
7629 if (symp != NULL)
7630 *symp = NULL;
7631
7632 if (symsecp != NULL)
7633 {
7634 asection *symsec = NULL;
7635 if (h->root.type == bfd_link_hash_defined
7636 || h->root.type == bfd_link_hash_defweak)
7637 symsec = h->root.u.def.section;
7638 *symsecp = symsec;
7639 }
7640
7641 if (tls_maskp != NULL)
7642 {
7643 struct ppc_link_hash_entry *eh;
7644
7645 eh = (struct ppc_link_hash_entry *) h;
7646 *tls_maskp = &eh->tls_mask;
7647 }
7648 }
7649 else
7650 {
7651 Elf_Internal_Sym *sym;
7652 Elf_Internal_Sym *locsyms = *locsymsp;
7653
7654 if (locsyms == NULL)
7655 {
7656 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7657 if (locsyms == NULL)
7658 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7659 symtab_hdr->sh_info,
7660 0, NULL, NULL, NULL);
7661 if (locsyms == NULL)
7662 return FALSE;
7663 *locsymsp = locsyms;
7664 }
7665 sym = locsyms + r_symndx;
7666
7667 if (hp != NULL)
7668 *hp = NULL;
7669
7670 if (symp != NULL)
7671 *symp = sym;
7672
7673 if (symsecp != NULL)
7674 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7675
7676 if (tls_maskp != NULL)
7677 {
7678 struct got_entry **lgot_ents;
7679 unsigned char *tls_mask;
7680
7681 tls_mask = NULL;
7682 lgot_ents = elf_local_got_ents (ibfd);
7683 if (lgot_ents != NULL)
7684 {
7685 struct plt_entry **local_plt = (struct plt_entry **)
7686 (lgot_ents + symtab_hdr->sh_info);
7687 unsigned char *lgot_masks = (unsigned char *)
7688 (local_plt + symtab_hdr->sh_info);
7689 tls_mask = &lgot_masks[r_symndx];
7690 }
7691 *tls_maskp = tls_mask;
7692 }
7693 }
7694 return TRUE;
7695 }
7696
7697 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7698 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7699 type suitable for optimization, and 1 otherwise. */
7700
7701 static int
7702 get_tls_mask (unsigned char **tls_maskp,
7703 unsigned long *toc_symndx,
7704 bfd_vma *toc_addend,
7705 Elf_Internal_Sym **locsymsp,
7706 const Elf_Internal_Rela *rel,
7707 bfd *ibfd)
7708 {
7709 unsigned long r_symndx;
7710 int next_r;
7711 struct elf_link_hash_entry *h;
7712 Elf_Internal_Sym *sym;
7713 asection *sec;
7714 bfd_vma off;
7715
7716 r_symndx = ELF64_R_SYM (rel->r_info);
7717 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7718 return 0;
7719
7720 if ((*tls_maskp != NULL
7721 && (**tls_maskp & TLS_TLS) != 0
7722 && **tls_maskp != (TLS_TLS | TLS_MARK))
7723 || sec == NULL
7724 || ppc64_elf_section_data (sec) == NULL
7725 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7726 return 1;
7727
7728 /* Look inside a TOC section too. */
7729 if (h != NULL)
7730 {
7731 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7732 off = h->root.u.def.value;
7733 }
7734 else
7735 off = sym->st_value;
7736 off += rel->r_addend;
7737 BFD_ASSERT (off % 8 == 0);
7738 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7739 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7740 if (toc_symndx != NULL)
7741 *toc_symndx = r_symndx;
7742 if (toc_addend != NULL)
7743 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7744 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7745 return 0;
7746 if ((h == NULL || is_static_defined (h))
7747 && (next_r == -1 || next_r == -2))
7748 return 1 - next_r;
7749 return 1;
7750 }
7751
7752 /* Find (or create) an entry in the tocsave hash table. */
7753
7754 static struct tocsave_entry *
7755 tocsave_find (struct ppc_link_hash_table *htab,
7756 enum insert_option insert,
7757 Elf_Internal_Sym **local_syms,
7758 const Elf_Internal_Rela *irela,
7759 bfd *ibfd)
7760 {
7761 unsigned long r_indx;
7762 struct elf_link_hash_entry *h;
7763 Elf_Internal_Sym *sym;
7764 struct tocsave_entry ent, *p;
7765 hashval_t hash;
7766 struct tocsave_entry **slot;
7767
7768 r_indx = ELF64_R_SYM (irela->r_info);
7769 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7770 return NULL;
7771 if (ent.sec == NULL || ent.sec->output_section == NULL)
7772 {
7773 _bfd_error_handler
7774 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7775 return NULL;
7776 }
7777
7778 if (h != NULL)
7779 ent.offset = h->root.u.def.value;
7780 else
7781 ent.offset = sym->st_value;
7782 ent.offset += irela->r_addend;
7783
7784 hash = tocsave_htab_hash (&ent);
7785 slot = ((struct tocsave_entry **)
7786 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7787 if (slot == NULL)
7788 return NULL;
7789
7790 if (*slot == NULL)
7791 {
7792 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7793 if (p == NULL)
7794 return NULL;
7795 *p = ent;
7796 *slot = p;
7797 }
7798 return *slot;
7799 }
7800
7801 /* Adjust all global syms defined in opd sections. In gcc generated
7802 code for the old ABI, these will already have been done. */
7803
7804 static bfd_boolean
7805 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7806 {
7807 struct ppc_link_hash_entry *eh;
7808 asection *sym_sec;
7809 struct _opd_sec_data *opd;
7810
7811 if (h->root.type == bfd_link_hash_indirect)
7812 return TRUE;
7813
7814 if (h->root.type != bfd_link_hash_defined
7815 && h->root.type != bfd_link_hash_defweak)
7816 return TRUE;
7817
7818 eh = (struct ppc_link_hash_entry *) h;
7819 if (eh->adjust_done)
7820 return TRUE;
7821
7822 sym_sec = eh->elf.root.u.def.section;
7823 opd = get_opd_info (sym_sec);
7824 if (opd != NULL && opd->adjust != NULL)
7825 {
7826 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7827 if (adjust == -1)
7828 {
7829 /* This entry has been deleted. */
7830 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7831 if (dsec == NULL)
7832 {
7833 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7834 if (discarded_section (dsec))
7835 {
7836 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7837 break;
7838 }
7839 }
7840 eh->elf.root.u.def.value = 0;
7841 eh->elf.root.u.def.section = dsec;
7842 }
7843 else
7844 eh->elf.root.u.def.value += adjust;
7845 eh->adjust_done = 1;
7846 }
7847 return TRUE;
7848 }
7849
7850 /* Handles decrementing dynamic reloc counts for the reloc specified by
7851 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7852 have already been determined. */
7853
7854 static bfd_boolean
7855 dec_dynrel_count (bfd_vma r_info,
7856 asection *sec,
7857 struct bfd_link_info *info,
7858 Elf_Internal_Sym **local_syms,
7859 struct elf_link_hash_entry *h,
7860 Elf_Internal_Sym *sym)
7861 {
7862 enum elf_ppc64_reloc_type r_type;
7863 asection *sym_sec = NULL;
7864
7865 /* Can this reloc be dynamic? This switch, and later tests here
7866 should be kept in sync with the code in check_relocs. */
7867 r_type = ELF64_R_TYPE (r_info);
7868 switch (r_type)
7869 {
7870 default:
7871 return TRUE;
7872
7873 case R_PPC64_TPREL16:
7874 case R_PPC64_TPREL16_LO:
7875 case R_PPC64_TPREL16_HI:
7876 case R_PPC64_TPREL16_HA:
7877 case R_PPC64_TPREL16_DS:
7878 case R_PPC64_TPREL16_LO_DS:
7879 case R_PPC64_TPREL16_HIGH:
7880 case R_PPC64_TPREL16_HIGHA:
7881 case R_PPC64_TPREL16_HIGHER:
7882 case R_PPC64_TPREL16_HIGHERA:
7883 case R_PPC64_TPREL16_HIGHEST:
7884 case R_PPC64_TPREL16_HIGHESTA:
7885 case R_PPC64_TPREL64:
7886 case R_PPC64_DTPMOD64:
7887 case R_PPC64_DTPREL64:
7888 case R_PPC64_ADDR64:
7889 case R_PPC64_REL30:
7890 case R_PPC64_REL32:
7891 case R_PPC64_REL64:
7892 case R_PPC64_ADDR14:
7893 case R_PPC64_ADDR14_BRNTAKEN:
7894 case R_PPC64_ADDR14_BRTAKEN:
7895 case R_PPC64_ADDR16:
7896 case R_PPC64_ADDR16_DS:
7897 case R_PPC64_ADDR16_HA:
7898 case R_PPC64_ADDR16_HI:
7899 case R_PPC64_ADDR16_HIGH:
7900 case R_PPC64_ADDR16_HIGHA:
7901 case R_PPC64_ADDR16_HIGHER:
7902 case R_PPC64_ADDR16_HIGHERA:
7903 case R_PPC64_ADDR16_HIGHEST:
7904 case R_PPC64_ADDR16_HIGHESTA:
7905 case R_PPC64_ADDR16_LO:
7906 case R_PPC64_ADDR16_LO_DS:
7907 case R_PPC64_ADDR24:
7908 case R_PPC64_ADDR32:
7909 case R_PPC64_UADDR16:
7910 case R_PPC64_UADDR32:
7911 case R_PPC64_UADDR64:
7912 case R_PPC64_TOC:
7913 break;
7914 }
7915
7916 if (local_syms != NULL)
7917 {
7918 unsigned long r_symndx;
7919 bfd *ibfd = sec->owner;
7920
7921 r_symndx = ELF64_R_SYM (r_info);
7922 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7923 return FALSE;
7924 }
7925
7926 if ((bfd_link_pic (info)
7927 && (must_be_dyn_reloc (info, r_type)
7928 || (h != NULL
7929 && (!SYMBOLIC_BIND (info, h)
7930 || h->root.type == bfd_link_hash_defweak
7931 || !h->def_regular))))
7932 || (ELIMINATE_COPY_RELOCS
7933 && !bfd_link_pic (info)
7934 && h != NULL
7935 && (h->root.type == bfd_link_hash_defweak
7936 || !h->def_regular)))
7937 ;
7938 else
7939 return TRUE;
7940
7941 if (h != NULL)
7942 {
7943 struct elf_dyn_relocs *p;
7944 struct elf_dyn_relocs **pp;
7945 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7946
7947 /* elf_gc_sweep may have already removed all dyn relocs associated
7948 with local syms for a given section. Also, symbol flags are
7949 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7950 report a dynreloc miscount. */
7951 if (*pp == NULL && info->gc_sections)
7952 return TRUE;
7953
7954 while ((p = *pp) != NULL)
7955 {
7956 if (p->sec == sec)
7957 {
7958 if (!must_be_dyn_reloc (info, r_type))
7959 p->pc_count -= 1;
7960 p->count -= 1;
7961 if (p->count == 0)
7962 *pp = p->next;
7963 return TRUE;
7964 }
7965 pp = &p->next;
7966 }
7967 }
7968 else
7969 {
7970 struct ppc_dyn_relocs *p;
7971 struct ppc_dyn_relocs **pp;
7972 void *vpp;
7973 bfd_boolean is_ifunc;
7974
7975 if (local_syms == NULL)
7976 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7977 if (sym_sec == NULL)
7978 sym_sec = sec;
7979
7980 vpp = &elf_section_data (sym_sec)->local_dynrel;
7981 pp = (struct ppc_dyn_relocs **) vpp;
7982
7983 if (*pp == NULL && info->gc_sections)
7984 return TRUE;
7985
7986 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7987 while ((p = *pp) != NULL)
7988 {
7989 if (p->sec == sec && p->ifunc == is_ifunc)
7990 {
7991 p->count -= 1;
7992 if (p->count == 0)
7993 *pp = p->next;
7994 return TRUE;
7995 }
7996 pp = &p->next;
7997 }
7998 }
7999
8000 /* xgettext:c-format */
8001 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
8002 sec->owner, sec);
8003 bfd_set_error (bfd_error_bad_value);
8004 return FALSE;
8005 }
8006
8007 /* Remove unused Official Procedure Descriptor entries. Currently we
8008 only remove those associated with functions in discarded link-once
8009 sections, or weakly defined functions that have been overridden. It
8010 would be possible to remove many more entries for statically linked
8011 applications. */
8012
8013 bfd_boolean
8014 ppc64_elf_edit_opd (struct bfd_link_info *info)
8015 {
8016 bfd *ibfd;
8017 bfd_boolean some_edited = FALSE;
8018 asection *need_pad = NULL;
8019 struct ppc_link_hash_table *htab;
8020
8021 htab = ppc_hash_table (info);
8022 if (htab == NULL)
8023 return FALSE;
8024
8025 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8026 {
8027 asection *sec;
8028 Elf_Internal_Rela *relstart, *rel, *relend;
8029 Elf_Internal_Shdr *symtab_hdr;
8030 Elf_Internal_Sym *local_syms;
8031 struct _opd_sec_data *opd;
8032 bfd_boolean need_edit, add_aux_fields, broken;
8033 bfd_size_type cnt_16b = 0;
8034
8035 if (!is_ppc64_elf (ibfd))
8036 continue;
8037
8038 sec = bfd_get_section_by_name (ibfd, ".opd");
8039 if (sec == NULL || sec->size == 0)
8040 continue;
8041
8042 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
8043 continue;
8044
8045 if (sec->output_section == bfd_abs_section_ptr)
8046 continue;
8047
8048 /* Look through the section relocs. */
8049 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
8050 continue;
8051
8052 local_syms = NULL;
8053 symtab_hdr = &elf_symtab_hdr (ibfd);
8054
8055 /* Read the relocations. */
8056 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8057 info->keep_memory);
8058 if (relstart == NULL)
8059 return FALSE;
8060
8061 /* First run through the relocs to check they are sane, and to
8062 determine whether we need to edit this opd section. */
8063 need_edit = FALSE;
8064 broken = FALSE;
8065 need_pad = sec;
8066 relend = relstart + sec->reloc_count;
8067 for (rel = relstart; rel < relend; )
8068 {
8069 enum elf_ppc64_reloc_type r_type;
8070 unsigned long r_symndx;
8071 asection *sym_sec;
8072 struct elf_link_hash_entry *h;
8073 Elf_Internal_Sym *sym;
8074 bfd_vma offset;
8075
8076 /* .opd contains an array of 16 or 24 byte entries. We're
8077 only interested in the reloc pointing to a function entry
8078 point. */
8079 offset = rel->r_offset;
8080 if (rel + 1 == relend
8081 || rel[1].r_offset != offset + 8)
8082 {
8083 /* If someone messes with .opd alignment then after a
8084 "ld -r" we might have padding in the middle of .opd.
8085 Also, there's nothing to prevent someone putting
8086 something silly in .opd with the assembler. No .opd
8087 optimization for them! */
8088 broken_opd:
8089 _bfd_error_handler
8090 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
8091 broken = TRUE;
8092 break;
8093 }
8094
8095 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
8096 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
8097 {
8098 _bfd_error_handler
8099 /* xgettext:c-format */
8100 (_("%pB: unexpected reloc type %u in .opd section"),
8101 ibfd, r_type);
8102 broken = TRUE;
8103 break;
8104 }
8105
8106 r_symndx = ELF64_R_SYM (rel->r_info);
8107 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8108 r_symndx, ibfd))
8109 goto error_ret;
8110
8111 if (sym_sec == NULL || sym_sec->owner == NULL)
8112 {
8113 const char *sym_name;
8114 if (h != NULL)
8115 sym_name = h->root.root.string;
8116 else
8117 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8118 sym_sec);
8119
8120 _bfd_error_handler
8121 /* xgettext:c-format */
8122 (_("%pB: undefined sym `%s' in .opd section"),
8123 ibfd, sym_name);
8124 broken = TRUE;
8125 break;
8126 }
8127
8128 /* opd entries are always for functions defined in the
8129 current input bfd. If the symbol isn't defined in the
8130 input bfd, then we won't be using the function in this
8131 bfd; It must be defined in a linkonce section in another
8132 bfd, or is weak. It's also possible that we are
8133 discarding the function due to a linker script /DISCARD/,
8134 which we test for via the output_section. */
8135 if (sym_sec->owner != ibfd
8136 || sym_sec->output_section == bfd_abs_section_ptr)
8137 need_edit = TRUE;
8138
8139 rel += 2;
8140 if (rel + 1 == relend
8141 || (rel + 2 < relend
8142 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8143 ++rel;
8144
8145 if (rel == relend)
8146 {
8147 if (sec->size == offset + 24)
8148 {
8149 need_pad = NULL;
8150 break;
8151 }
8152 if (sec->size == offset + 16)
8153 {
8154 cnt_16b++;
8155 break;
8156 }
8157 goto broken_opd;
8158 }
8159 else if (rel + 1 < relend
8160 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8161 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8162 {
8163 if (rel[0].r_offset == offset + 16)
8164 cnt_16b++;
8165 else if (rel[0].r_offset != offset + 24)
8166 goto broken_opd;
8167 }
8168 else
8169 goto broken_opd;
8170 }
8171
8172 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8173
8174 if (!broken && (need_edit || add_aux_fields))
8175 {
8176 Elf_Internal_Rela *write_rel;
8177 Elf_Internal_Shdr *rel_hdr;
8178 bfd_byte *rptr, *wptr;
8179 bfd_byte *new_contents;
8180 bfd_size_type amt;
8181
8182 new_contents = NULL;
8183 amt = OPD_NDX (sec->size) * sizeof (long);
8184 opd = &ppc64_elf_section_data (sec)->u.opd;
8185 opd->adjust = bfd_zalloc (sec->owner, amt);
8186 if (opd->adjust == NULL)
8187 return FALSE;
8188
8189 /* This seems a waste of time as input .opd sections are all
8190 zeros as generated by gcc, but I suppose there's no reason
8191 this will always be so. We might start putting something in
8192 the third word of .opd entries. */
8193 if ((sec->flags & SEC_IN_MEMORY) == 0)
8194 {
8195 bfd_byte *loc;
8196 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8197 {
8198 if (loc != NULL)
8199 free (loc);
8200 error_ret:
8201 if (local_syms != NULL
8202 && symtab_hdr->contents != (unsigned char *) local_syms)
8203 free (local_syms);
8204 if (elf_section_data (sec)->relocs != relstart)
8205 free (relstart);
8206 return FALSE;
8207 }
8208 sec->contents = loc;
8209 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8210 }
8211
8212 elf_section_data (sec)->relocs = relstart;
8213
8214 new_contents = sec->contents;
8215 if (add_aux_fields)
8216 {
8217 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8218 if (new_contents == NULL)
8219 return FALSE;
8220 need_pad = NULL;
8221 }
8222 wptr = new_contents;
8223 rptr = sec->contents;
8224 write_rel = relstart;
8225 for (rel = relstart; rel < relend; )
8226 {
8227 unsigned long r_symndx;
8228 asection *sym_sec;
8229 struct elf_link_hash_entry *h;
8230 struct ppc_link_hash_entry *fdh = NULL;
8231 Elf_Internal_Sym *sym;
8232 long opd_ent_size;
8233 Elf_Internal_Rela *next_rel;
8234 bfd_boolean skip;
8235
8236 r_symndx = ELF64_R_SYM (rel->r_info);
8237 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8238 r_symndx, ibfd))
8239 goto error_ret;
8240
8241 next_rel = rel + 2;
8242 if (next_rel + 1 == relend
8243 || (next_rel + 2 < relend
8244 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8245 ++next_rel;
8246
8247 /* See if the .opd entry is full 24 byte or
8248 16 byte (with fd_aux entry overlapped with next
8249 fd_func). */
8250 opd_ent_size = 24;
8251 if (next_rel == relend)
8252 {
8253 if (sec->size == rel->r_offset + 16)
8254 opd_ent_size = 16;
8255 }
8256 else if (next_rel->r_offset == rel->r_offset + 16)
8257 opd_ent_size = 16;
8258
8259 if (h != NULL
8260 && h->root.root.string[0] == '.')
8261 {
8262 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8263 if (fdh != NULL)
8264 {
8265 fdh = ppc_follow_link (fdh);
8266 if (fdh->elf.root.type != bfd_link_hash_defined
8267 && fdh->elf.root.type != bfd_link_hash_defweak)
8268 fdh = NULL;
8269 }
8270 }
8271
8272 skip = (sym_sec->owner != ibfd
8273 || sym_sec->output_section == bfd_abs_section_ptr);
8274 if (skip)
8275 {
8276 if (fdh != NULL && sym_sec->owner == ibfd)
8277 {
8278 /* Arrange for the function descriptor sym
8279 to be dropped. */
8280 fdh->elf.root.u.def.value = 0;
8281 fdh->elf.root.u.def.section = sym_sec;
8282 }
8283 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8284
8285 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8286 rel = next_rel;
8287 else
8288 while (1)
8289 {
8290 if (!dec_dynrel_count (rel->r_info, sec, info,
8291 NULL, h, sym))
8292 goto error_ret;
8293
8294 if (++rel == next_rel)
8295 break;
8296
8297 r_symndx = ELF64_R_SYM (rel->r_info);
8298 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8299 r_symndx, ibfd))
8300 goto error_ret;
8301 }
8302 }
8303 else
8304 {
8305 /* We'll be keeping this opd entry. */
8306 long adjust;
8307
8308 if (fdh != NULL)
8309 {
8310 /* Redefine the function descriptor symbol to
8311 this location in the opd section. It is
8312 necessary to update the value here rather
8313 than using an array of adjustments as we do
8314 for local symbols, because various places
8315 in the generic ELF code use the value
8316 stored in u.def.value. */
8317 fdh->elf.root.u.def.value = wptr - new_contents;
8318 fdh->adjust_done = 1;
8319 }
8320
8321 /* Local syms are a bit tricky. We could
8322 tweak them as they can be cached, but
8323 we'd need to look through the local syms
8324 for the function descriptor sym which we
8325 don't have at the moment. So keep an
8326 array of adjustments. */
8327 adjust = (wptr - new_contents) - (rptr - sec->contents);
8328 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8329
8330 if (wptr != rptr)
8331 memcpy (wptr, rptr, opd_ent_size);
8332 wptr += opd_ent_size;
8333 if (add_aux_fields && opd_ent_size == 16)
8334 {
8335 memset (wptr, '\0', 8);
8336 wptr += 8;
8337 }
8338
8339 /* We need to adjust any reloc offsets to point to the
8340 new opd entries. */
8341 for ( ; rel != next_rel; ++rel)
8342 {
8343 rel->r_offset += adjust;
8344 if (write_rel != rel)
8345 memcpy (write_rel, rel, sizeof (*rel));
8346 ++write_rel;
8347 }
8348 }
8349
8350 rptr += opd_ent_size;
8351 }
8352
8353 sec->size = wptr - new_contents;
8354 sec->reloc_count = write_rel - relstart;
8355 if (add_aux_fields)
8356 {
8357 free (sec->contents);
8358 sec->contents = new_contents;
8359 }
8360
8361 /* Fudge the header size too, as this is used later in
8362 elf_bfd_final_link if we are emitting relocs. */
8363 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8364 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8365 some_edited = TRUE;
8366 }
8367 else if (elf_section_data (sec)->relocs != relstart)
8368 free (relstart);
8369
8370 if (local_syms != NULL
8371 && symtab_hdr->contents != (unsigned char *) local_syms)
8372 {
8373 if (!info->keep_memory)
8374 free (local_syms);
8375 else
8376 symtab_hdr->contents = (unsigned char *) local_syms;
8377 }
8378 }
8379
8380 if (some_edited)
8381 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8382
8383 /* If we are doing a final link and the last .opd entry is just 16 byte
8384 long, add a 8 byte padding after it. */
8385 if (need_pad != NULL && !bfd_link_relocatable (info))
8386 {
8387 bfd_byte *p;
8388
8389 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8390 {
8391 BFD_ASSERT (need_pad->size > 0);
8392
8393 p = bfd_malloc (need_pad->size + 8);
8394 if (p == NULL)
8395 return FALSE;
8396
8397 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8398 p, 0, need_pad->size))
8399 return FALSE;
8400
8401 need_pad->contents = p;
8402 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8403 }
8404 else
8405 {
8406 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8407 if (p == NULL)
8408 return FALSE;
8409
8410 need_pad->contents = p;
8411 }
8412
8413 memset (need_pad->contents + need_pad->size, 0, 8);
8414 need_pad->size += 8;
8415 }
8416
8417 return TRUE;
8418 }
8419
8420 /* Analyze inline PLT call relocations to see whether calls to locally
8421 defined functions can be converted to direct calls. */
8422
8423 bfd_boolean
8424 ppc64_elf_inline_plt (struct bfd_link_info *info)
8425 {
8426 struct ppc_link_hash_table *htab;
8427 bfd *ibfd;
8428 asection *sec;
8429 bfd_vma low_vma, high_vma, limit;
8430
8431 htab = ppc_hash_table (info);
8432 if (htab == NULL)
8433 return FALSE;
8434
8435 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8436 reduced somewhat to cater for possible stubs that might be added
8437 between the call and its destination. */
8438 if (htab->params->group_size < 0)
8439 {
8440 limit = -htab->params->group_size;
8441 if (limit == 1)
8442 limit = 0x1e00000;
8443 }
8444 else
8445 {
8446 limit = htab->params->group_size;
8447 if (limit == 1)
8448 limit = 0x1c00000;
8449 }
8450
8451 low_vma = -1;
8452 high_vma = 0;
8453 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8454 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8455 {
8456 if (low_vma > sec->vma)
8457 low_vma = sec->vma;
8458 if (high_vma < sec->vma + sec->size)
8459 high_vma = sec->vma + sec->size;
8460 }
8461
8462 /* If a "bl" can reach anywhere in local code sections, then we can
8463 convert all inline PLT sequences to direct calls when the symbol
8464 is local. */
8465 if (high_vma - low_vma < limit)
8466 {
8467 htab->can_convert_all_inline_plt = 1;
8468 return TRUE;
8469 }
8470
8471 /* Otherwise, go looking through relocs for cases where a direct
8472 call won't reach. Mark the symbol on any such reloc to disable
8473 the optimization and keep the PLT entry as it seems likely that
8474 this will be better than creating trampolines. Note that this
8475 will disable the optimization for all inline PLT calls to a
8476 particular symbol, not just those that won't reach. The
8477 difficulty in doing a more precise optimization is that the
8478 linker needs to make a decision depending on whether a
8479 particular R_PPC64_PLTCALL insn can be turned into a direct
8480 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8481 the sequence, and there is nothing that ties those relocs
8482 together except their symbol. */
8483
8484 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8485 {
8486 Elf_Internal_Shdr *symtab_hdr;
8487 Elf_Internal_Sym *local_syms;
8488
8489 if (!is_ppc64_elf (ibfd))
8490 continue;
8491
8492 local_syms = NULL;
8493 symtab_hdr = &elf_symtab_hdr (ibfd);
8494
8495 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8496 if (ppc64_elf_section_data (sec)->has_pltcall
8497 && !bfd_is_abs_section (sec->output_section))
8498 {
8499 Elf_Internal_Rela *relstart, *rel, *relend;
8500
8501 /* Read the relocations. */
8502 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8503 info->keep_memory);
8504 if (relstart == NULL)
8505 return FALSE;
8506
8507 relend = relstart + sec->reloc_count;
8508 for (rel = relstart; rel < relend; )
8509 {
8510 enum elf_ppc64_reloc_type r_type;
8511 unsigned long r_symndx;
8512 asection *sym_sec;
8513 struct elf_link_hash_entry *h;
8514 Elf_Internal_Sym *sym;
8515 unsigned char *tls_maskp;
8516
8517 r_type = ELF64_R_TYPE (rel->r_info);
8518 if (r_type != R_PPC64_PLTCALL)
8519 continue;
8520
8521 r_symndx = ELF64_R_SYM (rel->r_info);
8522 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8523 r_symndx, ibfd))
8524 {
8525 if (elf_section_data (sec)->relocs != relstart)
8526 free (relstart);
8527 if (local_syms != NULL
8528 && symtab_hdr->contents != (unsigned char *) local_syms)
8529 free (local_syms);
8530 return FALSE;
8531 }
8532
8533 if (sym_sec != NULL && sym_sec->output_section != NULL)
8534 {
8535 bfd_vma from, to;
8536 if (h != NULL)
8537 to = h->root.u.def.value;
8538 else
8539 to = sym->st_value;
8540 to += (rel->r_addend
8541 + sym_sec->output_offset
8542 + sym_sec->output_section->vma);
8543 from = (rel->r_offset
8544 + sec->output_offset
8545 + sec->output_section->vma);
8546 if (to - from + limit < 2 * limit)
8547 *tls_maskp &= ~PLT_KEEP;
8548 }
8549 }
8550 if (elf_section_data (sec)->relocs != relstart)
8551 free (relstart);
8552 }
8553
8554 if (local_syms != NULL
8555 && symtab_hdr->contents != (unsigned char *) local_syms)
8556 {
8557 if (!info->keep_memory)
8558 free (local_syms);
8559 else
8560 symtab_hdr->contents = (unsigned char *) local_syms;
8561 }
8562 }
8563
8564 return TRUE;
8565 }
8566
8567 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8568
8569 asection *
8570 ppc64_elf_tls_setup (struct bfd_link_info *info)
8571 {
8572 struct ppc_link_hash_table *htab;
8573
8574 htab = ppc_hash_table (info);
8575 if (htab == NULL)
8576 return NULL;
8577
8578 if (abiversion (info->output_bfd) == 1)
8579 htab->opd_abi = 1;
8580
8581 if (htab->params->no_multi_toc)
8582 htab->do_multi_toc = 0;
8583 else if (!htab->do_multi_toc)
8584 htab->params->no_multi_toc = 1;
8585
8586 /* Default to --no-plt-localentry, as this option can cause problems
8587 with symbol interposition. For example, glibc libpthread.so and
8588 libc.so duplicate many pthread symbols, with a fallback
8589 implementation in libc.so. In some cases the fallback does more
8590 work than the pthread implementation. __pthread_condattr_destroy
8591 is one such symbol: the libpthread.so implementation is
8592 localentry:0 while the libc.so implementation is localentry:8.
8593 An app that "cleverly" uses dlopen to only load necessary
8594 libraries at runtime may omit loading libpthread.so when not
8595 running multi-threaded, which then results in the libc.so
8596 fallback symbols being used and ld.so complaining. Now there
8597 are workarounds in ld (see non_zero_localentry) to detect the
8598 pthread situation, but that may not be the only case where
8599 --plt-localentry can cause trouble. */
8600 if (htab->params->plt_localentry0 < 0)
8601 htab->params->plt_localentry0 = 0;
8602 if (htab->params->plt_localentry0
8603 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8604 FALSE, FALSE, FALSE) == NULL)
8605 _bfd_error_handler
8606 (_("warning: --plt-localentry is especially dangerous without "
8607 "ld.so support to detect ABI violations"));
8608
8609 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8610 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8611 FALSE, FALSE, TRUE));
8612 /* Move dynamic linking info to the function descriptor sym. */
8613 if (htab->tls_get_addr != NULL)
8614 func_desc_adjust (&htab->tls_get_addr->elf, info);
8615 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8616 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8617 FALSE, FALSE, TRUE));
8618 if (htab->params->tls_get_addr_opt)
8619 {
8620 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8621
8622 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8623 FALSE, FALSE, TRUE);
8624 if (opt != NULL)
8625 func_desc_adjust (opt, info);
8626 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8627 FALSE, FALSE, TRUE);
8628 if (opt_fd != NULL
8629 && (opt_fd->root.type == bfd_link_hash_defined
8630 || opt_fd->root.type == bfd_link_hash_defweak))
8631 {
8632 /* If glibc supports an optimized __tls_get_addr call stub,
8633 signalled by the presence of __tls_get_addr_opt, and we'll
8634 be calling __tls_get_addr via a plt call stub, then
8635 make __tls_get_addr point to __tls_get_addr_opt. */
8636 tga_fd = &htab->tls_get_addr_fd->elf;
8637 if (htab->elf.dynamic_sections_created
8638 && tga_fd != NULL
8639 && (tga_fd->type == STT_FUNC
8640 || tga_fd->needs_plt)
8641 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8642 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8643 {
8644 struct plt_entry *ent;
8645
8646 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8647 if (ent->plt.refcount > 0)
8648 break;
8649 if (ent != NULL)
8650 {
8651 tga_fd->root.type = bfd_link_hash_indirect;
8652 tga_fd->root.u.i.link = &opt_fd->root;
8653 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8654 opt_fd->mark = 1;
8655 if (opt_fd->dynindx != -1)
8656 {
8657 /* Use __tls_get_addr_opt in dynamic relocations. */
8658 opt_fd->dynindx = -1;
8659 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8660 opt_fd->dynstr_index);
8661 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8662 return NULL;
8663 }
8664 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8665 tga = &htab->tls_get_addr->elf;
8666 if (opt != NULL && tga != NULL)
8667 {
8668 tga->root.type = bfd_link_hash_indirect;
8669 tga->root.u.i.link = &opt->root;
8670 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8671 opt->mark = 1;
8672 _bfd_elf_link_hash_hide_symbol (info, opt,
8673 tga->forced_local);
8674 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8675 }
8676 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8677 htab->tls_get_addr_fd->is_func_descriptor = 1;
8678 if (htab->tls_get_addr != NULL)
8679 {
8680 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8681 htab->tls_get_addr->is_func = 1;
8682 }
8683 }
8684 }
8685 }
8686 else if (htab->params->tls_get_addr_opt < 0)
8687 htab->params->tls_get_addr_opt = 0;
8688 }
8689 return _bfd_elf_tls_setup (info->output_bfd, info);
8690 }
8691
8692 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8693 HASH1 or HASH2. */
8694
8695 static bfd_boolean
8696 branch_reloc_hash_match (const bfd *ibfd,
8697 const Elf_Internal_Rela *rel,
8698 const struct ppc_link_hash_entry *hash1,
8699 const struct ppc_link_hash_entry *hash2)
8700 {
8701 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8702 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8703 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8704
8705 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8706 {
8707 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8708 struct elf_link_hash_entry *h;
8709
8710 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8711 h = elf_follow_link (h);
8712 if (h == &hash1->elf || h == &hash2->elf)
8713 return TRUE;
8714 }
8715 return FALSE;
8716 }
8717
8718 /* Run through all the TLS relocs looking for optimization
8719 opportunities. The linker has been hacked (see ppc64elf.em) to do
8720 a preliminary section layout so that we know the TLS segment
8721 offsets. We can't optimize earlier because some optimizations need
8722 to know the tp offset, and we need to optimize before allocating
8723 dynamic relocations. */
8724
8725 bfd_boolean
8726 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8727 {
8728 bfd *ibfd;
8729 asection *sec;
8730 struct ppc_link_hash_table *htab;
8731 unsigned char *toc_ref;
8732 int pass;
8733
8734 if (!bfd_link_executable (info))
8735 return TRUE;
8736
8737 htab = ppc_hash_table (info);
8738 if (htab == NULL)
8739 return FALSE;
8740
8741 /* Make two passes over the relocs. On the first pass, mark toc
8742 entries involved with tls relocs, and check that tls relocs
8743 involved in setting up a tls_get_addr call are indeed followed by
8744 such a call. If they are not, we can't do any tls optimization.
8745 On the second pass twiddle tls_mask flags to notify
8746 relocate_section that optimization can be done, and adjust got
8747 and plt refcounts. */
8748 toc_ref = NULL;
8749 for (pass = 0; pass < 2; ++pass)
8750 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8751 {
8752 Elf_Internal_Sym *locsyms = NULL;
8753 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8754
8755 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8756 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8757 {
8758 Elf_Internal_Rela *relstart, *rel, *relend;
8759 bfd_boolean found_tls_get_addr_arg = 0;
8760
8761 /* Read the relocations. */
8762 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8763 info->keep_memory);
8764 if (relstart == NULL)
8765 {
8766 free (toc_ref);
8767 return FALSE;
8768 }
8769
8770 relend = relstart + sec->reloc_count;
8771 for (rel = relstart; rel < relend; rel++)
8772 {
8773 enum elf_ppc64_reloc_type r_type;
8774 unsigned long r_symndx;
8775 struct elf_link_hash_entry *h;
8776 Elf_Internal_Sym *sym;
8777 asection *sym_sec;
8778 unsigned char *tls_mask;
8779 unsigned char tls_set, tls_clear, tls_type = 0;
8780 bfd_vma value;
8781 bfd_boolean ok_tprel, is_local;
8782 long toc_ref_index = 0;
8783 int expecting_tls_get_addr = 0;
8784 bfd_boolean ret = FALSE;
8785
8786 r_symndx = ELF64_R_SYM (rel->r_info);
8787 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8788 r_symndx, ibfd))
8789 {
8790 err_free_rel:
8791 if (elf_section_data (sec)->relocs != relstart)
8792 free (relstart);
8793 if (toc_ref != NULL)
8794 free (toc_ref);
8795 if (locsyms != NULL
8796 && (elf_symtab_hdr (ibfd).contents
8797 != (unsigned char *) locsyms))
8798 free (locsyms);
8799 return ret;
8800 }
8801
8802 if (h != NULL)
8803 {
8804 if (h->root.type == bfd_link_hash_defined
8805 || h->root.type == bfd_link_hash_defweak)
8806 value = h->root.u.def.value;
8807 else if (h->root.type == bfd_link_hash_undefweak)
8808 value = 0;
8809 else
8810 {
8811 found_tls_get_addr_arg = 0;
8812 continue;
8813 }
8814 }
8815 else
8816 /* Symbols referenced by TLS relocs must be of type
8817 STT_TLS. So no need for .opd local sym adjust. */
8818 value = sym->st_value;
8819
8820 ok_tprel = FALSE;
8821 is_local = FALSE;
8822 if (h == NULL
8823 || !h->def_dynamic)
8824 {
8825 is_local = TRUE;
8826 if (h != NULL
8827 && h->root.type == bfd_link_hash_undefweak)
8828 ok_tprel = TRUE;
8829 else if (sym_sec != NULL
8830 && sym_sec->output_section != NULL)
8831 {
8832 value += sym_sec->output_offset;
8833 value += sym_sec->output_section->vma;
8834 value -= htab->elf.tls_sec->vma;
8835 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8836 < (bfd_vma) 1 << 32);
8837 }
8838 }
8839
8840 r_type = ELF64_R_TYPE (rel->r_info);
8841 /* If this section has old-style __tls_get_addr calls
8842 without marker relocs, then check that each
8843 __tls_get_addr call reloc is preceded by a reloc
8844 that conceivably belongs to the __tls_get_addr arg
8845 setup insn. If we don't find matching arg setup
8846 relocs, don't do any tls optimization. */
8847 if (pass == 0
8848 && sec->has_tls_get_addr_call
8849 && h != NULL
8850 && (h == &htab->tls_get_addr->elf
8851 || h == &htab->tls_get_addr_fd->elf)
8852 && !found_tls_get_addr_arg
8853 && is_branch_reloc (r_type))
8854 {
8855 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8856 "TLS optimization disabled\n"),
8857 ibfd, sec, rel->r_offset);
8858 ret = TRUE;
8859 goto err_free_rel;
8860 }
8861
8862 found_tls_get_addr_arg = 0;
8863 switch (r_type)
8864 {
8865 case R_PPC64_GOT_TLSLD16:
8866 case R_PPC64_GOT_TLSLD16_LO:
8867 expecting_tls_get_addr = 1;
8868 found_tls_get_addr_arg = 1;
8869 /* Fall through. */
8870
8871 case R_PPC64_GOT_TLSLD16_HI:
8872 case R_PPC64_GOT_TLSLD16_HA:
8873 /* These relocs should never be against a symbol
8874 defined in a shared lib. Leave them alone if
8875 that turns out to be the case. */
8876 if (!is_local)
8877 continue;
8878
8879 /* LD -> LE */
8880 tls_set = 0;
8881 tls_clear = TLS_LD;
8882 tls_type = TLS_TLS | TLS_LD;
8883 break;
8884
8885 case R_PPC64_GOT_TLSGD16:
8886 case R_PPC64_GOT_TLSGD16_LO:
8887 expecting_tls_get_addr = 1;
8888 found_tls_get_addr_arg = 1;
8889 /* Fall through. */
8890
8891 case R_PPC64_GOT_TLSGD16_HI:
8892 case R_PPC64_GOT_TLSGD16_HA:
8893 if (ok_tprel)
8894 /* GD -> LE */
8895 tls_set = 0;
8896 else
8897 /* GD -> IE */
8898 tls_set = TLS_TLS | TLS_TPRELGD;
8899 tls_clear = TLS_GD;
8900 tls_type = TLS_TLS | TLS_GD;
8901 break;
8902
8903 case R_PPC64_GOT_TPREL16_DS:
8904 case R_PPC64_GOT_TPREL16_LO_DS:
8905 case R_PPC64_GOT_TPREL16_HI:
8906 case R_PPC64_GOT_TPREL16_HA:
8907 if (ok_tprel)
8908 {
8909 /* IE -> LE */
8910 tls_set = 0;
8911 tls_clear = TLS_TPREL;
8912 tls_type = TLS_TLS | TLS_TPREL;
8913 break;
8914 }
8915 continue;
8916
8917 case R_PPC64_TLSGD:
8918 case R_PPC64_TLSLD:
8919 if (rel + 1 < relend
8920 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8921 {
8922 if (pass != 0
8923 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8924 {
8925 r_symndx = ELF64_R_SYM (rel[1].r_info);
8926 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8927 r_symndx, ibfd))
8928 goto err_free_rel;
8929 if (h != NULL)
8930 {
8931 struct plt_entry *ent = NULL;
8932
8933 for (ent = h->plt.plist;
8934 ent != NULL;
8935 ent = ent->next)
8936 if (ent->addend == rel[1].r_addend)
8937 break;
8938
8939 if (ent != NULL
8940 && ent->plt.refcount > 0)
8941 ent->plt.refcount -= 1;
8942 }
8943 }
8944 continue;
8945 }
8946 found_tls_get_addr_arg = 1;
8947 /* Fall through. */
8948
8949 case R_PPC64_TLS:
8950 case R_PPC64_TOC16:
8951 case R_PPC64_TOC16_LO:
8952 if (sym_sec == NULL || sym_sec != toc)
8953 continue;
8954
8955 /* Mark this toc entry as referenced by a TLS
8956 code sequence. We can do that now in the
8957 case of R_PPC64_TLS, and after checking for
8958 tls_get_addr for the TOC16 relocs. */
8959 if (toc_ref == NULL)
8960 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8961 if (toc_ref == NULL)
8962 goto err_free_rel;
8963
8964 if (h != NULL)
8965 value = h->root.u.def.value;
8966 else
8967 value = sym->st_value;
8968 value += rel->r_addend;
8969 if (value % 8 != 0)
8970 continue;
8971 BFD_ASSERT (value < toc->size
8972 && toc->output_offset % 8 == 0);
8973 toc_ref_index = (value + toc->output_offset) / 8;
8974 if (r_type == R_PPC64_TLS
8975 || r_type == R_PPC64_TLSGD
8976 || r_type == R_PPC64_TLSLD)
8977 {
8978 toc_ref[toc_ref_index] = 1;
8979 continue;
8980 }
8981
8982 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8983 continue;
8984
8985 tls_set = 0;
8986 tls_clear = 0;
8987 expecting_tls_get_addr = 2;
8988 break;
8989
8990 case R_PPC64_TPREL64:
8991 if (pass == 0
8992 || sec != toc
8993 || toc_ref == NULL
8994 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8995 continue;
8996 if (ok_tprel)
8997 {
8998 /* IE -> LE */
8999 tls_set = TLS_EXPLICIT;
9000 tls_clear = TLS_TPREL;
9001 break;
9002 }
9003 continue;
9004
9005 case R_PPC64_DTPMOD64:
9006 if (pass == 0
9007 || sec != toc
9008 || toc_ref == NULL
9009 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
9010 continue;
9011 if (rel + 1 < relend
9012 && (rel[1].r_info
9013 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
9014 && rel[1].r_offset == rel->r_offset + 8)
9015 {
9016 if (ok_tprel)
9017 /* GD -> LE */
9018 tls_set = TLS_EXPLICIT | TLS_GD;
9019 else
9020 /* GD -> IE */
9021 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
9022 tls_clear = TLS_GD;
9023 }
9024 else
9025 {
9026 if (!is_local)
9027 continue;
9028
9029 /* LD -> LE */
9030 tls_set = TLS_EXPLICIT;
9031 tls_clear = TLS_LD;
9032 }
9033 break;
9034
9035 default:
9036 continue;
9037 }
9038
9039 if (pass == 0)
9040 {
9041 if (!expecting_tls_get_addr
9042 || !sec->has_tls_get_addr_call)
9043 continue;
9044
9045 if (rel + 1 < relend
9046 && branch_reloc_hash_match (ibfd, rel + 1,
9047 htab->tls_get_addr,
9048 htab->tls_get_addr_fd))
9049 {
9050 if (expecting_tls_get_addr == 2)
9051 {
9052 /* Check for toc tls entries. */
9053 unsigned char *toc_tls;
9054 int retval;
9055
9056 retval = get_tls_mask (&toc_tls, NULL, NULL,
9057 &locsyms,
9058 rel, ibfd);
9059 if (retval == 0)
9060 goto err_free_rel;
9061 if (toc_tls != NULL)
9062 {
9063 if ((*toc_tls & TLS_TLS) != 0
9064 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
9065 found_tls_get_addr_arg = 1;
9066 if (retval > 1)
9067 toc_ref[toc_ref_index] = 1;
9068 }
9069 }
9070 continue;
9071 }
9072
9073 /* Uh oh, we didn't find the expected call. We
9074 could just mark this symbol to exclude it
9075 from tls optimization but it's safer to skip
9076 the entire optimization. */
9077 /* xgettext:c-format */
9078 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
9079 "TLS optimization disabled\n"),
9080 ibfd, sec, rel->r_offset);
9081 ret = TRUE;
9082 goto err_free_rel;
9083 }
9084
9085 /* If we don't have old-style __tls_get_addr calls
9086 without TLSGD/TLSLD marker relocs, and we haven't
9087 found a new-style __tls_get_addr call with a
9088 marker for this symbol, then we either have a
9089 broken object file or an -mlongcall style
9090 indirect call to __tls_get_addr without a marker.
9091 Disable optimization in this case. */
9092 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
9093 && (tls_set & TLS_EXPLICIT) == 0
9094 && !sec->has_tls_get_addr_call
9095 && ((*tls_mask & (TLS_TLS | TLS_MARK))
9096 != (TLS_TLS | TLS_MARK)))
9097 continue;
9098
9099 if (expecting_tls_get_addr)
9100 {
9101 struct plt_entry *ent = NULL;
9102
9103 if (htab->tls_get_addr != NULL)
9104 for (ent = htab->tls_get_addr->elf.plt.plist;
9105 ent != NULL;
9106 ent = ent->next)
9107 if (ent->addend == 0)
9108 break;
9109
9110 if (ent == NULL && htab->tls_get_addr_fd != NULL)
9111 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
9112 ent != NULL;
9113 ent = ent->next)
9114 if (ent->addend == 0)
9115 break;
9116
9117 if (ent != NULL
9118 && ent->plt.refcount > 0)
9119 ent->plt.refcount -= 1;
9120 }
9121
9122 if (tls_clear == 0)
9123 continue;
9124
9125 if ((tls_set & TLS_EXPLICIT) == 0)
9126 {
9127 struct got_entry *ent;
9128
9129 /* Adjust got entry for this reloc. */
9130 if (h != NULL)
9131 ent = h->got.glist;
9132 else
9133 ent = elf_local_got_ents (ibfd)[r_symndx];
9134
9135 for (; ent != NULL; ent = ent->next)
9136 if (ent->addend == rel->r_addend
9137 && ent->owner == ibfd
9138 && ent->tls_type == tls_type)
9139 break;
9140 if (ent == NULL)
9141 abort ();
9142
9143 if (tls_set == 0)
9144 {
9145 /* We managed to get rid of a got entry. */
9146 if (ent->got.refcount > 0)
9147 ent->got.refcount -= 1;
9148 }
9149 }
9150 else
9151 {
9152 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9153 we'll lose one or two dyn relocs. */
9154 if (!dec_dynrel_count (rel->r_info, sec, info,
9155 NULL, h, sym))
9156 return FALSE;
9157
9158 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9159 {
9160 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9161 NULL, h, sym))
9162 return FALSE;
9163 }
9164 }
9165
9166 *tls_mask |= tls_set;
9167 *tls_mask &= ~tls_clear;
9168 }
9169
9170 if (elf_section_data (sec)->relocs != relstart)
9171 free (relstart);
9172 }
9173
9174 if (locsyms != NULL
9175 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9176 {
9177 if (!info->keep_memory)
9178 free (locsyms);
9179 else
9180 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9181 }
9182 }
9183
9184 if (toc_ref != NULL)
9185 free (toc_ref);
9186 htab->do_tls_opt = 1;
9187 return TRUE;
9188 }
9189
9190 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9191 the values of any global symbols in a toc section that has been
9192 edited. Globals in toc sections should be a rarity, so this function
9193 sets a flag if any are found in toc sections other than the one just
9194 edited, so that further hash table traversals can be avoided. */
9195
9196 struct adjust_toc_info
9197 {
9198 asection *toc;
9199 unsigned long *skip;
9200 bfd_boolean global_toc_syms;
9201 };
9202
9203 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9204
9205 static bfd_boolean
9206 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9207 {
9208 struct ppc_link_hash_entry *eh;
9209 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9210 unsigned long i;
9211
9212 if (h->root.type != bfd_link_hash_defined
9213 && h->root.type != bfd_link_hash_defweak)
9214 return TRUE;
9215
9216 eh = (struct ppc_link_hash_entry *) h;
9217 if (eh->adjust_done)
9218 return TRUE;
9219
9220 if (eh->elf.root.u.def.section == toc_inf->toc)
9221 {
9222 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9223 i = toc_inf->toc->rawsize >> 3;
9224 else
9225 i = eh->elf.root.u.def.value >> 3;
9226
9227 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9228 {
9229 _bfd_error_handler
9230 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9231 do
9232 ++i;
9233 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9234 eh->elf.root.u.def.value = (bfd_vma) i << 3;
9235 }
9236
9237 eh->elf.root.u.def.value -= toc_inf->skip[i];
9238 eh->adjust_done = 1;
9239 }
9240 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9241 toc_inf->global_toc_syms = TRUE;
9242
9243 return TRUE;
9244 }
9245
9246 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9247 on a _LO variety toc/got reloc. */
9248
9249 static bfd_boolean
9250 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9251 {
9252 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9253 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9254 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9255 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9256 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9257 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9258 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9259 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9260 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9261 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9262 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9263 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9264 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9265 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9266 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9267 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9268 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9269 /* Exclude lfqu by testing reloc. If relocs are ever
9270 defined for the reduced D field in psq_lu then those
9271 will need testing too. */
9272 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9273 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9274 && (insn & 1) == 0)
9275 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9276 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9277 /* Exclude stfqu. psq_stu as above for psq_lu. */
9278 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9279 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9280 && (insn & 1) == 0));
9281 }
9282
9283 /* Examine all relocs referencing .toc sections in order to remove
9284 unused .toc entries. */
9285
9286 bfd_boolean
9287 ppc64_elf_edit_toc (struct bfd_link_info *info)
9288 {
9289 bfd *ibfd;
9290 struct adjust_toc_info toc_inf;
9291 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9292
9293 htab->do_toc_opt = 1;
9294 toc_inf.global_toc_syms = TRUE;
9295 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9296 {
9297 asection *toc, *sec;
9298 Elf_Internal_Shdr *symtab_hdr;
9299 Elf_Internal_Sym *local_syms;
9300 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9301 unsigned long *skip, *drop;
9302 unsigned char *used;
9303 unsigned char *keep, last, some_unused;
9304
9305 if (!is_ppc64_elf (ibfd))
9306 continue;
9307
9308 toc = bfd_get_section_by_name (ibfd, ".toc");
9309 if (toc == NULL
9310 || toc->size == 0
9311 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9312 || discarded_section (toc))
9313 continue;
9314
9315 toc_relocs = NULL;
9316 local_syms = NULL;
9317 symtab_hdr = &elf_symtab_hdr (ibfd);
9318
9319 /* Look at sections dropped from the final link. */
9320 skip = NULL;
9321 relstart = NULL;
9322 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9323 {
9324 if (sec->reloc_count == 0
9325 || !discarded_section (sec)
9326 || get_opd_info (sec)
9327 || (sec->flags & SEC_ALLOC) == 0
9328 || (sec->flags & SEC_DEBUGGING) != 0)
9329 continue;
9330
9331 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9332 if (relstart == NULL)
9333 goto error_ret;
9334
9335 /* Run through the relocs to see which toc entries might be
9336 unused. */
9337 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9338 {
9339 enum elf_ppc64_reloc_type r_type;
9340 unsigned long r_symndx;
9341 asection *sym_sec;
9342 struct elf_link_hash_entry *h;
9343 Elf_Internal_Sym *sym;
9344 bfd_vma val;
9345
9346 r_type = ELF64_R_TYPE (rel->r_info);
9347 switch (r_type)
9348 {
9349 default:
9350 continue;
9351
9352 case R_PPC64_TOC16:
9353 case R_PPC64_TOC16_LO:
9354 case R_PPC64_TOC16_HI:
9355 case R_PPC64_TOC16_HA:
9356 case R_PPC64_TOC16_DS:
9357 case R_PPC64_TOC16_LO_DS:
9358 break;
9359 }
9360
9361 r_symndx = ELF64_R_SYM (rel->r_info);
9362 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9363 r_symndx, ibfd))
9364 goto error_ret;
9365
9366 if (sym_sec != toc)
9367 continue;
9368
9369 if (h != NULL)
9370 val = h->root.u.def.value;
9371 else
9372 val = sym->st_value;
9373 val += rel->r_addend;
9374
9375 if (val >= toc->size)
9376 continue;
9377
9378 /* Anything in the toc ought to be aligned to 8 bytes.
9379 If not, don't mark as unused. */
9380 if (val & 7)
9381 continue;
9382
9383 if (skip == NULL)
9384 {
9385 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9386 if (skip == NULL)
9387 goto error_ret;
9388 }
9389
9390 skip[val >> 3] = ref_from_discarded;
9391 }
9392
9393 if (elf_section_data (sec)->relocs != relstart)
9394 free (relstart);
9395 }
9396
9397 /* For largetoc loads of address constants, we can convert
9398 . addis rx,2,addr@got@ha
9399 . ld ry,addr@got@l(rx)
9400 to
9401 . addis rx,2,addr@toc@ha
9402 . addi ry,rx,addr@toc@l
9403 when addr is within 2G of the toc pointer. This then means
9404 that the word storing "addr" in the toc is no longer needed. */
9405
9406 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9407 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9408 && toc->reloc_count != 0)
9409 {
9410 /* Read toc relocs. */
9411 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9412 info->keep_memory);
9413 if (toc_relocs == NULL)
9414 goto error_ret;
9415
9416 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9417 {
9418 enum elf_ppc64_reloc_type r_type;
9419 unsigned long r_symndx;
9420 asection *sym_sec;
9421 struct elf_link_hash_entry *h;
9422 Elf_Internal_Sym *sym;
9423 bfd_vma val, addr;
9424
9425 r_type = ELF64_R_TYPE (rel->r_info);
9426 if (r_type != R_PPC64_ADDR64)
9427 continue;
9428
9429 r_symndx = ELF64_R_SYM (rel->r_info);
9430 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9431 r_symndx, ibfd))
9432 goto error_ret;
9433
9434 if (sym_sec == NULL
9435 || sym_sec->output_section == NULL
9436 || discarded_section (sym_sec))
9437 continue;
9438
9439 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9440 continue;
9441
9442 if (h != NULL)
9443 {
9444 if (h->type == STT_GNU_IFUNC)
9445 continue;
9446 val = h->root.u.def.value;
9447 }
9448 else
9449 {
9450 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9451 continue;
9452 val = sym->st_value;
9453 }
9454 val += rel->r_addend;
9455 val += sym_sec->output_section->vma + sym_sec->output_offset;
9456
9457 /* We don't yet know the exact toc pointer value, but we
9458 know it will be somewhere in the toc section. Don't
9459 optimize if the difference from any possible toc
9460 pointer is outside [ff..f80008000, 7fff7fff]. */
9461 addr = toc->output_section->vma + TOC_BASE_OFF;
9462 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9463 continue;
9464
9465 addr = toc->output_section->vma + toc->output_section->rawsize;
9466 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9467 continue;
9468
9469 if (skip == NULL)
9470 {
9471 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9472 if (skip == NULL)
9473 goto error_ret;
9474 }
9475
9476 skip[rel->r_offset >> 3]
9477 |= can_optimize | ((rel - toc_relocs) << 2);
9478 }
9479 }
9480
9481 if (skip == NULL)
9482 continue;
9483
9484 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9485 if (used == NULL)
9486 {
9487 error_ret:
9488 if (local_syms != NULL
9489 && symtab_hdr->contents != (unsigned char *) local_syms)
9490 free (local_syms);
9491 if (sec != NULL
9492 && relstart != NULL
9493 && elf_section_data (sec)->relocs != relstart)
9494 free (relstart);
9495 if (toc_relocs != NULL
9496 && elf_section_data (toc)->relocs != toc_relocs)
9497 free (toc_relocs);
9498 if (skip != NULL)
9499 free (skip);
9500 return FALSE;
9501 }
9502
9503 /* Now check all kept sections that might reference the toc.
9504 Check the toc itself last. */
9505 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9506 : ibfd->sections);
9507 sec != NULL;
9508 sec = (sec == toc ? NULL
9509 : sec->next == NULL ? toc
9510 : sec->next == toc && toc->next ? toc->next
9511 : sec->next))
9512 {
9513 int repeat;
9514
9515 if (sec->reloc_count == 0
9516 || discarded_section (sec)
9517 || get_opd_info (sec)
9518 || (sec->flags & SEC_ALLOC) == 0
9519 || (sec->flags & SEC_DEBUGGING) != 0)
9520 continue;
9521
9522 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9523 info->keep_memory);
9524 if (relstart == NULL)
9525 {
9526 free (used);
9527 goto error_ret;
9528 }
9529
9530 /* Mark toc entries referenced as used. */
9531 do
9532 {
9533 repeat = 0;
9534 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9535 {
9536 enum elf_ppc64_reloc_type r_type;
9537 unsigned long r_symndx;
9538 asection *sym_sec;
9539 struct elf_link_hash_entry *h;
9540 Elf_Internal_Sym *sym;
9541 bfd_vma val;
9542 enum {no_check, check_lo, check_ha} insn_check;
9543
9544 r_type = ELF64_R_TYPE (rel->r_info);
9545 switch (r_type)
9546 {
9547 default:
9548 insn_check = no_check;
9549 break;
9550
9551 case R_PPC64_GOT_TLSLD16_HA:
9552 case R_PPC64_GOT_TLSGD16_HA:
9553 case R_PPC64_GOT_TPREL16_HA:
9554 case R_PPC64_GOT_DTPREL16_HA:
9555 case R_PPC64_GOT16_HA:
9556 case R_PPC64_TOC16_HA:
9557 insn_check = check_ha;
9558 break;
9559
9560 case R_PPC64_GOT_TLSLD16_LO:
9561 case R_PPC64_GOT_TLSGD16_LO:
9562 case R_PPC64_GOT_TPREL16_LO_DS:
9563 case R_PPC64_GOT_DTPREL16_LO_DS:
9564 case R_PPC64_GOT16_LO:
9565 case R_PPC64_GOT16_LO_DS:
9566 case R_PPC64_TOC16_LO:
9567 case R_PPC64_TOC16_LO_DS:
9568 insn_check = check_lo;
9569 break;
9570 }
9571
9572 if (insn_check != no_check)
9573 {
9574 bfd_vma off = rel->r_offset & ~3;
9575 unsigned char buf[4];
9576 unsigned int insn;
9577
9578 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9579 {
9580 free (used);
9581 goto error_ret;
9582 }
9583 insn = bfd_get_32 (ibfd, buf);
9584 if (insn_check == check_lo
9585 ? !ok_lo_toc_insn (insn, r_type)
9586 : ((insn & ((0x3f << 26) | 0x1f << 16))
9587 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9588 {
9589 char str[12];
9590
9591 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9592 sprintf (str, "%#08x", insn);
9593 info->callbacks->einfo
9594 /* xgettext:c-format */
9595 (_("%H: toc optimization is not supported for"
9596 " %s instruction\n"),
9597 ibfd, sec, rel->r_offset & ~3, str);
9598 }
9599 }
9600
9601 switch (r_type)
9602 {
9603 case R_PPC64_TOC16:
9604 case R_PPC64_TOC16_LO:
9605 case R_PPC64_TOC16_HI:
9606 case R_PPC64_TOC16_HA:
9607 case R_PPC64_TOC16_DS:
9608 case R_PPC64_TOC16_LO_DS:
9609 /* In case we're taking addresses of toc entries. */
9610 case R_PPC64_ADDR64:
9611 break;
9612
9613 default:
9614 continue;
9615 }
9616
9617 r_symndx = ELF64_R_SYM (rel->r_info);
9618 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9619 r_symndx, ibfd))
9620 {
9621 free (used);
9622 goto error_ret;
9623 }
9624
9625 if (sym_sec != toc)
9626 continue;
9627
9628 if (h != NULL)
9629 val = h->root.u.def.value;
9630 else
9631 val = sym->st_value;
9632 val += rel->r_addend;
9633
9634 if (val >= toc->size)
9635 continue;
9636
9637 if ((skip[val >> 3] & can_optimize) != 0)
9638 {
9639 bfd_vma off;
9640 unsigned char opc;
9641
9642 switch (r_type)
9643 {
9644 case R_PPC64_TOC16_HA:
9645 break;
9646
9647 case R_PPC64_TOC16_LO_DS:
9648 off = rel->r_offset;
9649 off += (bfd_big_endian (ibfd) ? -2 : 3);
9650 if (!bfd_get_section_contents (ibfd, sec, &opc,
9651 off, 1))
9652 {
9653 free (used);
9654 goto error_ret;
9655 }
9656 if ((opc & (0x3f << 2)) == (58u << 2))
9657 break;
9658 /* Fall through. */
9659
9660 default:
9661 /* Wrong sort of reloc, or not a ld. We may
9662 as well clear ref_from_discarded too. */
9663 skip[val >> 3] = 0;
9664 }
9665 }
9666
9667 if (sec != toc)
9668 used[val >> 3] = 1;
9669 /* For the toc section, we only mark as used if this
9670 entry itself isn't unused. */
9671 else if ((used[rel->r_offset >> 3]
9672 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9673 && !used[val >> 3])
9674 {
9675 /* Do all the relocs again, to catch reference
9676 chains. */
9677 repeat = 1;
9678 used[val >> 3] = 1;
9679 }
9680 }
9681 }
9682 while (repeat);
9683
9684 if (elf_section_data (sec)->relocs != relstart)
9685 free (relstart);
9686 }
9687
9688 /* Merge the used and skip arrays. Assume that TOC
9689 doublewords not appearing as either used or unused belong
9690 to an entry more than one doubleword in size. */
9691 for (drop = skip, keep = used, last = 0, some_unused = 0;
9692 drop < skip + (toc->size + 7) / 8;
9693 ++drop, ++keep)
9694 {
9695 if (*keep)
9696 {
9697 *drop &= ~ref_from_discarded;
9698 if ((*drop & can_optimize) != 0)
9699 some_unused = 1;
9700 last = 0;
9701 }
9702 else if ((*drop & ref_from_discarded) != 0)
9703 {
9704 some_unused = 1;
9705 last = ref_from_discarded;
9706 }
9707 else
9708 *drop = last;
9709 }
9710
9711 free (used);
9712
9713 if (some_unused)
9714 {
9715 bfd_byte *contents, *src;
9716 unsigned long off;
9717 Elf_Internal_Sym *sym;
9718 bfd_boolean local_toc_syms = FALSE;
9719
9720 /* Shuffle the toc contents, and at the same time convert the
9721 skip array from booleans into offsets. */
9722 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9723 goto error_ret;
9724
9725 elf_section_data (toc)->this_hdr.contents = contents;
9726
9727 for (src = contents, off = 0, drop = skip;
9728 src < contents + toc->size;
9729 src += 8, ++drop)
9730 {
9731 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9732 off += 8;
9733 else if (off != 0)
9734 {
9735 *drop = off;
9736 memcpy (src - off, src, 8);
9737 }
9738 }
9739 *drop = off;
9740 toc->rawsize = toc->size;
9741 toc->size = src - contents - off;
9742
9743 /* Adjust addends for relocs against the toc section sym,
9744 and optimize any accesses we can. */
9745 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9746 {
9747 if (sec->reloc_count == 0
9748 || discarded_section (sec))
9749 continue;
9750
9751 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9752 info->keep_memory);
9753 if (relstart == NULL)
9754 goto error_ret;
9755
9756 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9757 {
9758 enum elf_ppc64_reloc_type r_type;
9759 unsigned long r_symndx;
9760 asection *sym_sec;
9761 struct elf_link_hash_entry *h;
9762 bfd_vma val;
9763
9764 r_type = ELF64_R_TYPE (rel->r_info);
9765 switch (r_type)
9766 {
9767 default:
9768 continue;
9769
9770 case R_PPC64_TOC16:
9771 case R_PPC64_TOC16_LO:
9772 case R_PPC64_TOC16_HI:
9773 case R_PPC64_TOC16_HA:
9774 case R_PPC64_TOC16_DS:
9775 case R_PPC64_TOC16_LO_DS:
9776 case R_PPC64_ADDR64:
9777 break;
9778 }
9779
9780 r_symndx = ELF64_R_SYM (rel->r_info);
9781 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9782 r_symndx, ibfd))
9783 goto error_ret;
9784
9785 if (sym_sec != toc)
9786 continue;
9787
9788 if (h != NULL)
9789 val = h->root.u.def.value;
9790 else
9791 {
9792 val = sym->st_value;
9793 if (val != 0)
9794 local_toc_syms = TRUE;
9795 }
9796
9797 val += rel->r_addend;
9798
9799 if (val > toc->rawsize)
9800 val = toc->rawsize;
9801 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9802 continue;
9803 else if ((skip[val >> 3] & can_optimize) != 0)
9804 {
9805 Elf_Internal_Rela *tocrel
9806 = toc_relocs + (skip[val >> 3] >> 2);
9807 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9808
9809 switch (r_type)
9810 {
9811 case R_PPC64_TOC16_HA:
9812 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9813 break;
9814
9815 case R_PPC64_TOC16_LO_DS:
9816 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9817 break;
9818
9819 default:
9820 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9821 ppc_howto_init ();
9822 info->callbacks->einfo
9823 /* xgettext:c-format */
9824 (_("%H: %s references "
9825 "optimized away TOC entry\n"),
9826 ibfd, sec, rel->r_offset,
9827 ppc64_elf_howto_table[r_type]->name);
9828 bfd_set_error (bfd_error_bad_value);
9829 goto error_ret;
9830 }
9831 rel->r_addend = tocrel->r_addend;
9832 elf_section_data (sec)->relocs = relstart;
9833 continue;
9834 }
9835
9836 if (h != NULL || sym->st_value != 0)
9837 continue;
9838
9839 rel->r_addend -= skip[val >> 3];
9840 elf_section_data (sec)->relocs = relstart;
9841 }
9842
9843 if (elf_section_data (sec)->relocs != relstart)
9844 free (relstart);
9845 }
9846
9847 /* We shouldn't have local or global symbols defined in the TOC,
9848 but handle them anyway. */
9849 if (local_syms != NULL)
9850 for (sym = local_syms;
9851 sym < local_syms + symtab_hdr->sh_info;
9852 ++sym)
9853 if (sym->st_value != 0
9854 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9855 {
9856 unsigned long i;
9857
9858 if (sym->st_value > toc->rawsize)
9859 i = toc->rawsize >> 3;
9860 else
9861 i = sym->st_value >> 3;
9862
9863 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9864 {
9865 if (local_toc_syms)
9866 _bfd_error_handler
9867 (_("%s defined on removed toc entry"),
9868 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9869 do
9870 ++i;
9871 while ((skip[i] & (ref_from_discarded | can_optimize)));
9872 sym->st_value = (bfd_vma) i << 3;
9873 }
9874
9875 sym->st_value -= skip[i];
9876 symtab_hdr->contents = (unsigned char *) local_syms;
9877 }
9878
9879 /* Adjust any global syms defined in this toc input section. */
9880 if (toc_inf.global_toc_syms)
9881 {
9882 toc_inf.toc = toc;
9883 toc_inf.skip = skip;
9884 toc_inf.global_toc_syms = FALSE;
9885 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9886 &toc_inf);
9887 }
9888
9889 if (toc->reloc_count != 0)
9890 {
9891 Elf_Internal_Shdr *rel_hdr;
9892 Elf_Internal_Rela *wrel;
9893 bfd_size_type sz;
9894
9895 /* Remove unused toc relocs, and adjust those we keep. */
9896 if (toc_relocs == NULL)
9897 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9898 info->keep_memory);
9899 if (toc_relocs == NULL)
9900 goto error_ret;
9901
9902 wrel = toc_relocs;
9903 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9904 if ((skip[rel->r_offset >> 3]
9905 & (ref_from_discarded | can_optimize)) == 0)
9906 {
9907 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9908 wrel->r_info = rel->r_info;
9909 wrel->r_addend = rel->r_addend;
9910 ++wrel;
9911 }
9912 else if (!dec_dynrel_count (rel->r_info, toc, info,
9913 &local_syms, NULL, NULL))
9914 goto error_ret;
9915
9916 elf_section_data (toc)->relocs = toc_relocs;
9917 toc->reloc_count = wrel - toc_relocs;
9918 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9919 sz = rel_hdr->sh_entsize;
9920 rel_hdr->sh_size = toc->reloc_count * sz;
9921 }
9922 }
9923 else if (toc_relocs != NULL
9924 && elf_section_data (toc)->relocs != toc_relocs)
9925 free (toc_relocs);
9926
9927 if (local_syms != NULL
9928 && symtab_hdr->contents != (unsigned char *) local_syms)
9929 {
9930 if (!info->keep_memory)
9931 free (local_syms);
9932 else
9933 symtab_hdr->contents = (unsigned char *) local_syms;
9934 }
9935 free (skip);
9936 }
9937
9938 return TRUE;
9939 }
9940
9941 /* Return true iff input section I references the TOC using
9942 instructions limited to +/-32k offsets. */
9943
9944 bfd_boolean
9945 ppc64_elf_has_small_toc_reloc (asection *i)
9946 {
9947 return (is_ppc64_elf (i->owner)
9948 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9949 }
9950
9951 /* Allocate space for one GOT entry. */
9952
9953 static void
9954 allocate_got (struct elf_link_hash_entry *h,
9955 struct bfd_link_info *info,
9956 struct got_entry *gent)
9957 {
9958 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9959 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9960 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9961 ? 16 : 8);
9962 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9963 ? 2 : 1) * sizeof (Elf64_External_Rela);
9964 asection *got = ppc64_elf_tdata (gent->owner)->got;
9965
9966 gent->got.offset = got->size;
9967 got->size += entsize;
9968
9969 if (h->type == STT_GNU_IFUNC)
9970 {
9971 htab->elf.irelplt->size += rentsize;
9972 htab->got_reli_size += rentsize;
9973 }
9974 else if (((bfd_link_pic (info)
9975 && !((gent->tls_type & TLS_TPREL) != 0
9976 && bfd_link_executable (info)
9977 && SYMBOL_REFERENCES_LOCAL (info, h)))
9978 || (htab->elf.dynamic_sections_created
9979 && h->dynindx != -1
9980 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9981 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9982 {
9983 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9984 relgot->size += rentsize;
9985 }
9986 }
9987
9988 /* This function merges got entries in the same toc group. */
9989
9990 static void
9991 merge_got_entries (struct got_entry **pent)
9992 {
9993 struct got_entry *ent, *ent2;
9994
9995 for (ent = *pent; ent != NULL; ent = ent->next)
9996 if (!ent->is_indirect)
9997 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9998 if (!ent2->is_indirect
9999 && ent2->addend == ent->addend
10000 && ent2->tls_type == ent->tls_type
10001 && elf_gp (ent2->owner) == elf_gp (ent->owner))
10002 {
10003 ent2->is_indirect = TRUE;
10004 ent2->got.ent = ent;
10005 }
10006 }
10007
10008 /* If H is undefined, make it dynamic if that makes sense. */
10009
10010 static bfd_boolean
10011 ensure_undef_dynamic (struct bfd_link_info *info,
10012 struct elf_link_hash_entry *h)
10013 {
10014 struct elf_link_hash_table *htab = elf_hash_table (info);
10015
10016 if (htab->dynamic_sections_created
10017 && ((info->dynamic_undefined_weak != 0
10018 && h->root.type == bfd_link_hash_undefweak)
10019 || h->root.type == bfd_link_hash_undefined)
10020 && h->dynindx == -1
10021 && !h->forced_local
10022 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
10023 return bfd_elf_link_record_dynamic_symbol (info, h);
10024 return TRUE;
10025 }
10026
10027 /* Allocate space in .plt, .got and associated reloc sections for
10028 dynamic relocs. */
10029
10030 static bfd_boolean
10031 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
10032 {
10033 struct bfd_link_info *info;
10034 struct ppc_link_hash_table *htab;
10035 asection *s;
10036 struct ppc_link_hash_entry *eh;
10037 struct got_entry **pgent, *gent;
10038
10039 if (h->root.type == bfd_link_hash_indirect)
10040 return TRUE;
10041
10042 info = (struct bfd_link_info *) inf;
10043 htab = ppc_hash_table (info);
10044 if (htab == NULL)
10045 return FALSE;
10046
10047 eh = (struct ppc_link_hash_entry *) h;
10048 /* Run through the TLS GD got entries first if we're changing them
10049 to TPREL. */
10050 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
10051 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10052 if (gent->got.refcount > 0
10053 && (gent->tls_type & TLS_GD) != 0)
10054 {
10055 /* This was a GD entry that has been converted to TPREL. If
10056 there happens to be a TPREL entry we can use that one. */
10057 struct got_entry *ent;
10058 for (ent = h->got.glist; ent != NULL; ent = ent->next)
10059 if (ent->got.refcount > 0
10060 && (ent->tls_type & TLS_TPREL) != 0
10061 && ent->addend == gent->addend
10062 && ent->owner == gent->owner)
10063 {
10064 gent->got.refcount = 0;
10065 break;
10066 }
10067
10068 /* If not, then we'll be using our own TPREL entry. */
10069 if (gent->got.refcount != 0)
10070 gent->tls_type = TLS_TLS | TLS_TPREL;
10071 }
10072
10073 /* Remove any list entry that won't generate a word in the GOT before
10074 we call merge_got_entries. Otherwise we risk merging to empty
10075 entries. */
10076 pgent = &h->got.glist;
10077 while ((gent = *pgent) != NULL)
10078 if (gent->got.refcount > 0)
10079 {
10080 if ((gent->tls_type & TLS_LD) != 0
10081 && !h->def_dynamic)
10082 {
10083 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
10084 *pgent = gent->next;
10085 }
10086 else
10087 pgent = &gent->next;
10088 }
10089 else
10090 *pgent = gent->next;
10091
10092 if (!htab->do_multi_toc)
10093 merge_got_entries (&h->got.glist);
10094
10095 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10096 if (!gent->is_indirect)
10097 {
10098 /* Make sure this symbol is output as a dynamic symbol. */
10099 if (!ensure_undef_dynamic (info, h))
10100 return FALSE;
10101
10102 if (!is_ppc64_elf (gent->owner))
10103 abort ();
10104
10105 allocate_got (h, info, gent);
10106 }
10107
10108 /* If no dynamic sections we can't have dynamic relocs, except for
10109 IFUNCs which are handled even in static executables. */
10110 if (!htab->elf.dynamic_sections_created
10111 && h->type != STT_GNU_IFUNC)
10112 eh->dyn_relocs = NULL;
10113
10114 /* Discard relocs on undefined symbols that must be local. */
10115 else if (h->root.type == bfd_link_hash_undefined
10116 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10117 eh->dyn_relocs = NULL;
10118
10119 /* Also discard relocs on undefined weak syms with non-default
10120 visibility, or when dynamic_undefined_weak says so. */
10121 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10122 eh->dyn_relocs = NULL;
10123
10124 if (eh->dyn_relocs != NULL)
10125 {
10126 struct elf_dyn_relocs *p, **pp;
10127
10128 /* In the shared -Bsymbolic case, discard space allocated for
10129 dynamic pc-relative relocs against symbols which turn out to
10130 be defined in regular objects. For the normal shared case,
10131 discard space for relocs that have become local due to symbol
10132 visibility changes. */
10133
10134 if (bfd_link_pic (info))
10135 {
10136 /* Relocs that use pc_count are those that appear on a call
10137 insn, or certain REL relocs (see must_be_dyn_reloc) that
10138 can be generated via assembly. We want calls to
10139 protected symbols to resolve directly to the function
10140 rather than going via the plt. If people want function
10141 pointer comparisons to work as expected then they should
10142 avoid writing weird assembly. */
10143 if (SYMBOL_CALLS_LOCAL (info, h))
10144 {
10145 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10146 {
10147 p->count -= p->pc_count;
10148 p->pc_count = 0;
10149 if (p->count == 0)
10150 *pp = p->next;
10151 else
10152 pp = &p->next;
10153 }
10154 }
10155
10156 if (eh->dyn_relocs != NULL)
10157 {
10158 /* Make sure this symbol is output as a dynamic symbol. */
10159 if (!ensure_undef_dynamic (info, h))
10160 return FALSE;
10161 }
10162 }
10163 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10164 {
10165 /* For the non-pic case, discard space for relocs against
10166 symbols which turn out to need copy relocs or are not
10167 dynamic. */
10168 if (h->dynamic_adjusted
10169 && !h->def_regular
10170 && !ELF_COMMON_DEF_P (h))
10171 {
10172 /* Make sure this symbol is output as a dynamic symbol. */
10173 if (!ensure_undef_dynamic (info, h))
10174 return FALSE;
10175
10176 if (h->dynindx == -1)
10177 eh->dyn_relocs = NULL;
10178 }
10179 else
10180 eh->dyn_relocs = NULL;
10181 }
10182
10183 /* Finally, allocate space. */
10184 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10185 {
10186 asection *sreloc = elf_section_data (p->sec)->sreloc;
10187 if (eh->elf.type == STT_GNU_IFUNC)
10188 sreloc = htab->elf.irelplt;
10189 sreloc->size += p->count * sizeof (Elf64_External_Rela);
10190 }
10191 }
10192
10193 /* We might need a PLT entry when the symbol
10194 a) is dynamic, or
10195 b) is an ifunc, or
10196 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10197 d) has plt16 relocs and we are linking statically. */
10198 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10199 || h->type == STT_GNU_IFUNC
10200 || (h->needs_plt && h->dynamic_adjusted)
10201 || (h->needs_plt
10202 && h->def_regular
10203 && !htab->elf.dynamic_sections_created
10204 && !htab->can_convert_all_inline_plt
10205 && (((struct ppc_link_hash_entry *) h)->tls_mask
10206 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10207 {
10208 struct plt_entry *pent;
10209 bfd_boolean doneone = FALSE;
10210 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10211 if (pent->plt.refcount > 0)
10212 {
10213 if (!htab->elf.dynamic_sections_created
10214 || h->dynindx == -1)
10215 {
10216 if (h->type == STT_GNU_IFUNC)
10217 {
10218 s = htab->elf.iplt;
10219 pent->plt.offset = s->size;
10220 s->size += PLT_ENTRY_SIZE (htab);
10221 s = htab->elf.irelplt;
10222 }
10223 else
10224 {
10225 s = htab->pltlocal;
10226 pent->plt.offset = s->size;
10227 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10228 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10229 }
10230 }
10231 else
10232 {
10233 /* If this is the first .plt entry, make room for the special
10234 first entry. */
10235 s = htab->elf.splt;
10236 if (s->size == 0)
10237 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10238
10239 pent->plt.offset = s->size;
10240
10241 /* Make room for this entry. */
10242 s->size += PLT_ENTRY_SIZE (htab);
10243
10244 /* Make room for the .glink code. */
10245 s = htab->glink;
10246 if (s->size == 0)
10247 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10248 if (htab->opd_abi)
10249 {
10250 /* We need bigger stubs past index 32767. */
10251 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10252 s->size += 4;
10253 s->size += 2*4;
10254 }
10255 else
10256 s->size += 4;
10257
10258 /* We also need to make an entry in the .rela.plt section. */
10259 s = htab->elf.srelplt;
10260 }
10261 if (s != NULL)
10262 s->size += sizeof (Elf64_External_Rela);
10263 doneone = TRUE;
10264 }
10265 else
10266 pent->plt.offset = (bfd_vma) -1;
10267 if (!doneone)
10268 {
10269 h->plt.plist = NULL;
10270 h->needs_plt = 0;
10271 }
10272 }
10273 else
10274 {
10275 h->plt.plist = NULL;
10276 h->needs_plt = 0;
10277 }
10278
10279 return TRUE;
10280 }
10281
10282 #define PPC_LO(v) ((v) & 0xffff)
10283 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10284 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10285
10286 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10287 to set up space for global entry stubs. These are put in glink,
10288 after the branch table. */
10289
10290 static bfd_boolean
10291 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10292 {
10293 struct bfd_link_info *info;
10294 struct ppc_link_hash_table *htab;
10295 struct plt_entry *pent;
10296 asection *s, *plt;
10297
10298 if (h->root.type == bfd_link_hash_indirect)
10299 return TRUE;
10300
10301 if (!h->pointer_equality_needed)
10302 return TRUE;
10303
10304 if (h->def_regular)
10305 return TRUE;
10306
10307 info = inf;
10308 htab = ppc_hash_table (info);
10309 if (htab == NULL)
10310 return FALSE;
10311
10312 s = htab->global_entry;
10313 plt = htab->elf.splt;
10314 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10315 if (pent->plt.offset != (bfd_vma) -1
10316 && pent->addend == 0)
10317 {
10318 /* For ELFv2, if this symbol is not defined in a regular file
10319 and we are not generating a shared library or pie, then we
10320 need to define the symbol in the executable on a call stub.
10321 This is to avoid text relocations. */
10322 bfd_vma off, stub_align, stub_off, stub_size;
10323 unsigned int align_power;
10324
10325 stub_size = 16;
10326 stub_off = s->size;
10327 if (htab->params->plt_stub_align >= 0)
10328 align_power = htab->params->plt_stub_align;
10329 else
10330 align_power = -htab->params->plt_stub_align;
10331 /* Setting section alignment is delayed until we know it is
10332 non-empty. Otherwise the .text output section will be
10333 aligned at least to plt_stub_align even when no global
10334 entry stubs are needed. */
10335 if (s->alignment_power < align_power)
10336 s->alignment_power = align_power;
10337 stub_align = (bfd_vma) 1 << align_power;
10338 if (htab->params->plt_stub_align >= 0
10339 || ((((stub_off + stub_size - 1) & -stub_align)
10340 - (stub_off & -stub_align))
10341 > ((stub_size - 1) & -stub_align)))
10342 stub_off = (stub_off + stub_align - 1) & -stub_align;
10343 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10344 off -= stub_off + s->output_offset + s->output_section->vma;
10345 /* Note that for --plt-stub-align negative we have a possible
10346 dependency between stub offset and size. Break that
10347 dependency by assuming the max stub size when calculating
10348 the stub offset. */
10349 if (PPC_HA (off) == 0)
10350 stub_size -= 4;
10351 h->root.type = bfd_link_hash_defined;
10352 h->root.u.def.section = s;
10353 h->root.u.def.value = stub_off;
10354 s->size = stub_off + stub_size;
10355 break;
10356 }
10357 return TRUE;
10358 }
10359
10360 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10361 read-only sections. */
10362
10363 static bfd_boolean
10364 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10365 {
10366 asection *sec;
10367
10368 if (h->root.type == bfd_link_hash_indirect)
10369 return TRUE;
10370
10371 sec = readonly_dynrelocs (h);
10372 if (sec != NULL)
10373 {
10374 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10375
10376 info->flags |= DF_TEXTREL;
10377 info->callbacks->minfo
10378 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10379 sec->owner, h->root.root.string, sec);
10380
10381 /* Not an error, just cut short the traversal. */
10382 return FALSE;
10383 }
10384 return TRUE;
10385 }
10386
10387 /* Set the sizes of the dynamic sections. */
10388
10389 static bfd_boolean
10390 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10391 struct bfd_link_info *info)
10392 {
10393 struct ppc_link_hash_table *htab;
10394 bfd *dynobj;
10395 asection *s;
10396 bfd_boolean relocs;
10397 bfd *ibfd;
10398 struct got_entry *first_tlsld;
10399
10400 htab = ppc_hash_table (info);
10401 if (htab == NULL)
10402 return FALSE;
10403
10404 dynobj = htab->elf.dynobj;
10405 if (dynobj == NULL)
10406 abort ();
10407
10408 if (htab->elf.dynamic_sections_created)
10409 {
10410 /* Set the contents of the .interp section to the interpreter. */
10411 if (bfd_link_executable (info) && !info->nointerp)
10412 {
10413 s = bfd_get_linker_section (dynobj, ".interp");
10414 if (s == NULL)
10415 abort ();
10416 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10417 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10418 }
10419 }
10420
10421 /* Set up .got offsets for local syms, and space for local dynamic
10422 relocs. */
10423 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10424 {
10425 struct got_entry **lgot_ents;
10426 struct got_entry **end_lgot_ents;
10427 struct plt_entry **local_plt;
10428 struct plt_entry **end_local_plt;
10429 unsigned char *lgot_masks;
10430 bfd_size_type locsymcount;
10431 Elf_Internal_Shdr *symtab_hdr;
10432
10433 if (!is_ppc64_elf (ibfd))
10434 continue;
10435
10436 for (s = ibfd->sections; s != NULL; s = s->next)
10437 {
10438 struct ppc_dyn_relocs *p;
10439
10440 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10441 {
10442 if (!bfd_is_abs_section (p->sec)
10443 && bfd_is_abs_section (p->sec->output_section))
10444 {
10445 /* Input section has been discarded, either because
10446 it is a copy of a linkonce section or due to
10447 linker script /DISCARD/, so we'll be discarding
10448 the relocs too. */
10449 }
10450 else if (p->count != 0)
10451 {
10452 asection *srel = elf_section_data (p->sec)->sreloc;
10453 if (p->ifunc)
10454 srel = htab->elf.irelplt;
10455 srel->size += p->count * sizeof (Elf64_External_Rela);
10456 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10457 info->flags |= DF_TEXTREL;
10458 }
10459 }
10460 }
10461
10462 lgot_ents = elf_local_got_ents (ibfd);
10463 if (!lgot_ents)
10464 continue;
10465
10466 symtab_hdr = &elf_symtab_hdr (ibfd);
10467 locsymcount = symtab_hdr->sh_info;
10468 end_lgot_ents = lgot_ents + locsymcount;
10469 local_plt = (struct plt_entry **) end_lgot_ents;
10470 end_local_plt = local_plt + locsymcount;
10471 lgot_masks = (unsigned char *) end_local_plt;
10472 s = ppc64_elf_tdata (ibfd)->got;
10473 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10474 {
10475 struct got_entry **pent, *ent;
10476
10477 pent = lgot_ents;
10478 while ((ent = *pent) != NULL)
10479 if (ent->got.refcount > 0)
10480 {
10481 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10482 {
10483 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10484 *pent = ent->next;
10485 }
10486 else
10487 {
10488 unsigned int ent_size = 8;
10489 unsigned int rel_size = sizeof (Elf64_External_Rela);
10490
10491 ent->got.offset = s->size;
10492 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10493 {
10494 ent_size *= 2;
10495 rel_size *= 2;
10496 }
10497 s->size += ent_size;
10498 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10499 {
10500 htab->elf.irelplt->size += rel_size;
10501 htab->got_reli_size += rel_size;
10502 }
10503 else if (bfd_link_pic (info)
10504 && !((ent->tls_type & TLS_TPREL) != 0
10505 && bfd_link_executable (info)))
10506 {
10507 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10508 srel->size += rel_size;
10509 }
10510 pent = &ent->next;
10511 }
10512 }
10513 else
10514 *pent = ent->next;
10515 }
10516
10517 /* Allocate space for plt calls to local syms. */
10518 lgot_masks = (unsigned char *) end_local_plt;
10519 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10520 {
10521 struct plt_entry *ent;
10522
10523 for (ent = *local_plt; ent != NULL; ent = ent->next)
10524 if (ent->plt.refcount > 0)
10525 {
10526 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10527 {
10528 s = htab->elf.iplt;
10529 ent->plt.offset = s->size;
10530 s->size += PLT_ENTRY_SIZE (htab);
10531 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10532 }
10533 else if (htab->can_convert_all_inline_plt
10534 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10535 ent->plt.offset = (bfd_vma) -1;
10536 else
10537 {
10538 s = htab->pltlocal;
10539 ent->plt.offset = s->size;
10540 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10541 if (bfd_link_pic (info))
10542 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10543 }
10544 }
10545 else
10546 ent->plt.offset = (bfd_vma) -1;
10547 }
10548 }
10549
10550 /* Allocate global sym .plt and .got entries, and space for global
10551 sym dynamic relocs. */
10552 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10553
10554 if (!htab->opd_abi && !bfd_link_pic (info))
10555 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10556
10557 first_tlsld = NULL;
10558 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10559 {
10560 struct got_entry *ent;
10561
10562 if (!is_ppc64_elf (ibfd))
10563 continue;
10564
10565 ent = ppc64_tlsld_got (ibfd);
10566 if (ent->got.refcount > 0)
10567 {
10568 if (!htab->do_multi_toc && first_tlsld != NULL)
10569 {
10570 ent->is_indirect = TRUE;
10571 ent->got.ent = first_tlsld;
10572 }
10573 else
10574 {
10575 if (first_tlsld == NULL)
10576 first_tlsld = ent;
10577 s = ppc64_elf_tdata (ibfd)->got;
10578 ent->got.offset = s->size;
10579 ent->owner = ibfd;
10580 s->size += 16;
10581 if (bfd_link_pic (info))
10582 {
10583 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10584 srel->size += sizeof (Elf64_External_Rela);
10585 }
10586 }
10587 }
10588 else
10589 ent->got.offset = (bfd_vma) -1;
10590 }
10591
10592 /* We now have determined the sizes of the various dynamic sections.
10593 Allocate memory for them. */
10594 relocs = FALSE;
10595 for (s = dynobj->sections; s != NULL; s = s->next)
10596 {
10597 if ((s->flags & SEC_LINKER_CREATED) == 0)
10598 continue;
10599
10600 if (s == htab->brlt || s == htab->relbrlt)
10601 /* These haven't been allocated yet; don't strip. */
10602 continue;
10603 else if (s == htab->elf.sgot
10604 || s == htab->elf.splt
10605 || s == htab->elf.iplt
10606 || s == htab->pltlocal
10607 || s == htab->glink
10608 || s == htab->global_entry
10609 || s == htab->elf.sdynbss
10610 || s == htab->elf.sdynrelro)
10611 {
10612 /* Strip this section if we don't need it; see the
10613 comment below. */
10614 }
10615 else if (s == htab->glink_eh_frame)
10616 {
10617 if (!bfd_is_abs_section (s->output_section))
10618 /* Not sized yet. */
10619 continue;
10620 }
10621 else if (CONST_STRNEQ (s->name, ".rela"))
10622 {
10623 if (s->size != 0)
10624 {
10625 if (s != htab->elf.srelplt)
10626 relocs = TRUE;
10627
10628 /* We use the reloc_count field as a counter if we need
10629 to copy relocs into the output file. */
10630 s->reloc_count = 0;
10631 }
10632 }
10633 else
10634 {
10635 /* It's not one of our sections, so don't allocate space. */
10636 continue;
10637 }
10638
10639 if (s->size == 0)
10640 {
10641 /* If we don't need this section, strip it from the
10642 output file. This is mostly to handle .rela.bss and
10643 .rela.plt. We must create both sections in
10644 create_dynamic_sections, because they must be created
10645 before the linker maps input sections to output
10646 sections. The linker does that before
10647 adjust_dynamic_symbol is called, and it is that
10648 function which decides whether anything needs to go
10649 into these sections. */
10650 s->flags |= SEC_EXCLUDE;
10651 continue;
10652 }
10653
10654 if (bfd_is_abs_section (s->output_section))
10655 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10656 s->name);
10657
10658 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10659 continue;
10660
10661 /* Allocate memory for the section contents. We use bfd_zalloc
10662 here in case unused entries are not reclaimed before the
10663 section's contents are written out. This should not happen,
10664 but this way if it does we get a R_PPC64_NONE reloc in .rela
10665 sections instead of garbage.
10666 We also rely on the section contents being zero when writing
10667 the GOT and .dynrelro. */
10668 s->contents = bfd_zalloc (dynobj, s->size);
10669 if (s->contents == NULL)
10670 return FALSE;
10671 }
10672
10673 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10674 {
10675 if (!is_ppc64_elf (ibfd))
10676 continue;
10677
10678 s = ppc64_elf_tdata (ibfd)->got;
10679 if (s != NULL && s != htab->elf.sgot)
10680 {
10681 if (s->size == 0)
10682 s->flags |= SEC_EXCLUDE;
10683 else
10684 {
10685 s->contents = bfd_zalloc (ibfd, s->size);
10686 if (s->contents == NULL)
10687 return FALSE;
10688 }
10689 }
10690 s = ppc64_elf_tdata (ibfd)->relgot;
10691 if (s != NULL)
10692 {
10693 if (s->size == 0)
10694 s->flags |= SEC_EXCLUDE;
10695 else
10696 {
10697 s->contents = bfd_zalloc (ibfd, s->size);
10698 if (s->contents == NULL)
10699 return FALSE;
10700 relocs = TRUE;
10701 s->reloc_count = 0;
10702 }
10703 }
10704 }
10705
10706 if (htab->elf.dynamic_sections_created)
10707 {
10708 bfd_boolean tls_opt;
10709
10710 /* Add some entries to the .dynamic section. We fill in the
10711 values later, in ppc64_elf_finish_dynamic_sections, but we
10712 must add the entries now so that we get the correct size for
10713 the .dynamic section. The DT_DEBUG entry is filled in by the
10714 dynamic linker and used by the debugger. */
10715 #define add_dynamic_entry(TAG, VAL) \
10716 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10717
10718 if (bfd_link_executable (info))
10719 {
10720 if (!add_dynamic_entry (DT_DEBUG, 0))
10721 return FALSE;
10722 }
10723
10724 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10725 {
10726 if (!add_dynamic_entry (DT_PLTGOT, 0)
10727 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10728 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10729 || !add_dynamic_entry (DT_JMPREL, 0)
10730 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10731 return FALSE;
10732 }
10733
10734 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10735 {
10736 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10737 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10738 return FALSE;
10739 }
10740
10741 tls_opt = (htab->params->tls_get_addr_opt
10742 && htab->tls_get_addr_fd != NULL
10743 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10744 if (tls_opt || !htab->opd_abi)
10745 {
10746 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10747 return FALSE;
10748 }
10749
10750 if (relocs)
10751 {
10752 if (!add_dynamic_entry (DT_RELA, 0)
10753 || !add_dynamic_entry (DT_RELASZ, 0)
10754 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10755 return FALSE;
10756
10757 /* If any dynamic relocs apply to a read-only section,
10758 then we need a DT_TEXTREL entry. */
10759 if ((info->flags & DF_TEXTREL) == 0)
10760 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10761
10762 if ((info->flags & DF_TEXTREL) != 0)
10763 {
10764 if (!add_dynamic_entry (DT_TEXTREL, 0))
10765 return FALSE;
10766 }
10767 }
10768 }
10769 #undef add_dynamic_entry
10770
10771 return TRUE;
10772 }
10773
10774 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10775
10776 static bfd_boolean
10777 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10778 {
10779 if (h->plt.plist != NULL
10780 && !h->def_regular
10781 && !h->pointer_equality_needed)
10782 return FALSE;
10783
10784 return _bfd_elf_hash_symbol (h);
10785 }
10786
10787 /* Determine the type of stub needed, if any, for a call. */
10788
10789 static inline enum ppc_stub_type
10790 ppc_type_of_stub (asection *input_sec,
10791 const Elf_Internal_Rela *rel,
10792 struct ppc_link_hash_entry **hash,
10793 struct plt_entry **plt_ent,
10794 bfd_vma destination,
10795 unsigned long local_off)
10796 {
10797 struct ppc_link_hash_entry *h = *hash;
10798 bfd_vma location;
10799 bfd_vma branch_offset;
10800 bfd_vma max_branch_offset;
10801 enum elf_ppc64_reloc_type r_type;
10802
10803 if (h != NULL)
10804 {
10805 struct plt_entry *ent;
10806 struct ppc_link_hash_entry *fdh = h;
10807 if (h->oh != NULL
10808 && h->oh->is_func_descriptor)
10809 {
10810 fdh = ppc_follow_link (h->oh);
10811 *hash = fdh;
10812 }
10813
10814 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10815 if (ent->addend == rel->r_addend
10816 && ent->plt.offset != (bfd_vma) -1)
10817 {
10818 *plt_ent = ent;
10819 return ppc_stub_plt_call;
10820 }
10821
10822 /* Here, we know we don't have a plt entry. If we don't have a
10823 either a defined function descriptor or a defined entry symbol
10824 in a regular object file, then it is pointless trying to make
10825 any other type of stub. */
10826 if (!is_static_defined (&fdh->elf)
10827 && !is_static_defined (&h->elf))
10828 return ppc_stub_none;
10829 }
10830 else if (elf_local_got_ents (input_sec->owner) != NULL)
10831 {
10832 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10833 struct plt_entry **local_plt = (struct plt_entry **)
10834 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10835 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10836
10837 if (local_plt[r_symndx] != NULL)
10838 {
10839 struct plt_entry *ent;
10840
10841 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10842 if (ent->addend == rel->r_addend
10843 && ent->plt.offset != (bfd_vma) -1)
10844 {
10845 *plt_ent = ent;
10846 return ppc_stub_plt_call;
10847 }
10848 }
10849 }
10850
10851 /* Determine where the call point is. */
10852 location = (input_sec->output_offset
10853 + input_sec->output_section->vma
10854 + rel->r_offset);
10855
10856 branch_offset = destination - location;
10857 r_type = ELF64_R_TYPE (rel->r_info);
10858
10859 /* Determine if a long branch stub is needed. */
10860 max_branch_offset = 1 << 25;
10861 if (r_type == R_PPC64_REL14
10862 || r_type == R_PPC64_REL14_BRTAKEN
10863 || r_type == R_PPC64_REL14_BRNTAKEN)
10864 max_branch_offset = 1 << 15;
10865
10866 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10867 /* We need a stub. Figure out whether a long_branch or plt_branch
10868 is needed later. */
10869 return ppc_stub_long_branch;
10870
10871 return ppc_stub_none;
10872 }
10873
10874 /* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
10875 loads r12 from r11+r12 (LOAD true).
10876 . lis %r12,xxx-1b@highest
10877 . ori %r12,xxx-1b@higher
10878 . sldi %r12,%r12,32
10879 . oris %r12,%r12,xxx-1b@hi
10880 . ori %r12,%r12,xxx-1b@l
10881 . add %r12,%r11,%r12 */
10882
10883 static bfd_byte *
10884 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10885 {
10886 if (off + 0x8000 < 0x10000)
10887 {
10888 if (load)
10889 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10890 else
10891 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10892 p += 4;
10893 }
10894 else if (off + 0x80008000ULL < 0x100000000ULL)
10895 {
10896 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10897 p += 4;
10898 if (load)
10899 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10900 else
10901 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10902 p += 4;
10903 }
10904 else
10905 {
10906 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10907 {
10908 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10909 p += 4;
10910 }
10911 else
10912 {
10913 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10914 p += 4;
10915 if (((off >> 32) & 0xffff) != 0)
10916 {
10917 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10918 p += 4;
10919 }
10920 }
10921 if (((off >> 32) & 0xffffffffULL) != 0)
10922 {
10923 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10924 p += 4;
10925 }
10926 if (PPC_HI (off) != 0)
10927 {
10928 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10929 p += 4;
10930 }
10931 if (PPC_LO (off) != 0)
10932 {
10933 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10934 p += 4;
10935 }
10936 if (load)
10937 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10938 else
10939 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10940 p += 4;
10941 }
10942 return p;
10943 }
10944
10945 static unsigned int
10946 size_offset (bfd_vma off)
10947 {
10948 unsigned int size;
10949 if (off + 0x8000 < 0x10000)
10950 size = 4;
10951 else if (off + 0x80008000ULL < 0x100000000ULL)
10952 size = 8;
10953 else
10954 {
10955 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10956 size = 4;
10957 else
10958 {
10959 size = 4;
10960 if (((off >> 32) & 0xffff) != 0)
10961 size += 4;
10962 }
10963 if (((off >> 32) & 0xffffffffULL) != 0)
10964 size += 4;
10965 if (PPC_HI (off) != 0)
10966 size += 4;
10967 if (PPC_LO (off) != 0)
10968 size += 4;
10969 size += 4;
10970 }
10971 return size;
10972 }
10973
10974 /* Emit .eh_frame opcode to advance pc by DELTA. */
10975
10976 static bfd_byte *
10977 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10978 {
10979 delta /= 4;
10980 if (delta < 64)
10981 *eh++ = DW_CFA_advance_loc + delta;
10982 else if (delta < 256)
10983 {
10984 *eh++ = DW_CFA_advance_loc1;
10985 *eh++ = delta;
10986 }
10987 else if (delta < 65536)
10988 {
10989 *eh++ = DW_CFA_advance_loc2;
10990 bfd_put_16 (abfd, delta, eh);
10991 eh += 2;
10992 }
10993 else
10994 {
10995 *eh++ = DW_CFA_advance_loc4;
10996 bfd_put_32 (abfd, delta, eh);
10997 eh += 4;
10998 }
10999 return eh;
11000 }
11001
11002 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11003
11004 static unsigned int
11005 eh_advance_size (unsigned int delta)
11006 {
11007 if (delta < 64 * 4)
11008 /* DW_CFA_advance_loc+[1..63]. */
11009 return 1;
11010 if (delta < 256 * 4)
11011 /* DW_CFA_advance_loc1, byte. */
11012 return 2;
11013 if (delta < 65536 * 4)
11014 /* DW_CFA_advance_loc2, 2 bytes. */
11015 return 3;
11016 /* DW_CFA_advance_loc4, 4 bytes. */
11017 return 5;
11018 }
11019
11020 /* With power7 weakly ordered memory model, it is possible for ld.so
11021 to update a plt entry in one thread and have another thread see a
11022 stale zero toc entry. To avoid this we need some sort of acquire
11023 barrier in the call stub. One solution is to make the load of the
11024 toc word seem to appear to depend on the load of the function entry
11025 word. Another solution is to test for r2 being zero, and branch to
11026 the appropriate glink entry if so.
11027
11028 . fake dep barrier compare
11029 . ld 12,xxx(2) ld 12,xxx(2)
11030 . mtctr 12 mtctr 12
11031 . xor 11,12,12 ld 2,xxx+8(2)
11032 . add 2,2,11 cmpldi 2,0
11033 . ld 2,xxx+8(2) bnectr+
11034 . bctr b <glink_entry>
11035
11036 The solution involving the compare turns out to be faster, so
11037 that's what we use unless the branch won't reach. */
11038
11039 #define ALWAYS_USE_FAKE_DEP 0
11040 #define ALWAYS_EMIT_R2SAVE 0
11041
11042 static inline unsigned int
11043 plt_stub_size (struct ppc_link_hash_table *htab,
11044 struct ppc_stub_hash_entry *stub_entry,
11045 bfd_vma off)
11046 {
11047 unsigned size;
11048
11049 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11050 {
11051 size = 24 + size_offset (off);
11052 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11053 size += 4;
11054 return size;
11055 }
11056
11057 size = 12;
11058 if (ALWAYS_EMIT_R2SAVE
11059 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11060 size += 4;
11061 if (PPC_HA (off) != 0)
11062 size += 4;
11063 if (htab->opd_abi)
11064 {
11065 size += 4;
11066 if (htab->params->plt_static_chain)
11067 size += 4;
11068 if (htab->params->plt_thread_safe
11069 && htab->elf.dynamic_sections_created
11070 && stub_entry->h != NULL
11071 && stub_entry->h->elf.dynindx != -1)
11072 size += 8;
11073 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
11074 size += 4;
11075 }
11076 if (stub_entry->h != NULL
11077 && (stub_entry->h == htab->tls_get_addr_fd
11078 || stub_entry->h == htab->tls_get_addr)
11079 && htab->params->tls_get_addr_opt)
11080 {
11081 size += 7 * 4;
11082 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
11083 size += 6 * 4;
11084 }
11085 return size;
11086 }
11087
11088 /* Depending on the sign of plt_stub_align:
11089 If positive, return the padding to align to a 2**plt_stub_align
11090 boundary.
11091 If negative, if this stub would cross fewer 2**plt_stub_align
11092 boundaries if we align, then return the padding needed to do so. */
11093
11094 static inline unsigned int
11095 plt_stub_pad (struct ppc_link_hash_table *htab,
11096 struct ppc_stub_hash_entry *stub_entry,
11097 bfd_vma plt_off)
11098 {
11099 int stub_align;
11100 unsigned stub_size;
11101 bfd_vma stub_off = stub_entry->group->stub_sec->size;
11102
11103 if (htab->params->plt_stub_align >= 0)
11104 {
11105 stub_align = 1 << htab->params->plt_stub_align;
11106 if ((stub_off & (stub_align - 1)) != 0)
11107 return stub_align - (stub_off & (stub_align - 1));
11108 return 0;
11109 }
11110
11111 stub_align = 1 << -htab->params->plt_stub_align;
11112 stub_size = plt_stub_size (htab, stub_entry, plt_off);
11113 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11114 > ((stub_size - 1) & -stub_align))
11115 return stub_align - (stub_off & (stub_align - 1));
11116 return 0;
11117 }
11118
11119 /* Build a .plt call stub. */
11120
11121 static inline bfd_byte *
11122 build_plt_stub (struct ppc_link_hash_table *htab,
11123 struct ppc_stub_hash_entry *stub_entry,
11124 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11125 {
11126 bfd *obfd = htab->params->stub_bfd;
11127 bfd_boolean plt_load_toc = htab->opd_abi;
11128 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
11129 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
11130 && htab->elf.dynamic_sections_created
11131 && stub_entry->h != NULL
11132 && stub_entry->h->elf.dynindx != -1);
11133 bfd_boolean use_fake_dep = plt_thread_safe;
11134 bfd_vma cmp_branch_off = 0;
11135
11136 if (!ALWAYS_USE_FAKE_DEP
11137 && plt_load_toc
11138 && plt_thread_safe
11139 && !((stub_entry->h == htab->tls_get_addr_fd
11140 || stub_entry->h == htab->tls_get_addr)
11141 && htab->params->tls_get_addr_opt))
11142 {
11143 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11144 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11145 / PLT_ENTRY_SIZE (htab));
11146 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11147 bfd_vma to, from;
11148
11149 if (pltindex > 32768)
11150 glinkoff += (pltindex - 32768) * 4;
11151 to = (glinkoff
11152 + htab->glink->output_offset
11153 + htab->glink->output_section->vma);
11154 from = (p - stub_entry->group->stub_sec->contents
11155 + 4 * (ALWAYS_EMIT_R2SAVE
11156 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11157 + 4 * (PPC_HA (offset) != 0)
11158 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11159 != PPC_HA (offset))
11160 + 4 * (plt_static_chain != 0)
11161 + 20
11162 + stub_entry->group->stub_sec->output_offset
11163 + stub_entry->group->stub_sec->output_section->vma);
11164 cmp_branch_off = to - from;
11165 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11166 }
11167
11168 if (PPC_HA (offset) != 0)
11169 {
11170 if (r != NULL)
11171 {
11172 if (ALWAYS_EMIT_R2SAVE
11173 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11174 r[0].r_offset += 4;
11175 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11176 r[1].r_offset = r[0].r_offset + 4;
11177 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11178 r[1].r_addend = r[0].r_addend;
11179 if (plt_load_toc)
11180 {
11181 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11182 {
11183 r[2].r_offset = r[1].r_offset + 4;
11184 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11185 r[2].r_addend = r[0].r_addend;
11186 }
11187 else
11188 {
11189 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11190 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11191 r[2].r_addend = r[0].r_addend + 8;
11192 if (plt_static_chain)
11193 {
11194 r[3].r_offset = r[2].r_offset + 4;
11195 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11196 r[3].r_addend = r[0].r_addend + 16;
11197 }
11198 }
11199 }
11200 }
11201 if (ALWAYS_EMIT_R2SAVE
11202 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11203 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11204 if (plt_load_toc)
11205 {
11206 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11207 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11208 }
11209 else
11210 {
11211 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11212 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11213 }
11214 if (plt_load_toc
11215 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11216 {
11217 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11218 offset = 0;
11219 }
11220 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11221 if (plt_load_toc)
11222 {
11223 if (use_fake_dep)
11224 {
11225 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11226 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11227 }
11228 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11229 if (plt_static_chain)
11230 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11231 }
11232 }
11233 else
11234 {
11235 if (r != NULL)
11236 {
11237 if (ALWAYS_EMIT_R2SAVE
11238 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11239 r[0].r_offset += 4;
11240 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11241 if (plt_load_toc)
11242 {
11243 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11244 {
11245 r[1].r_offset = r[0].r_offset + 4;
11246 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11247 r[1].r_addend = r[0].r_addend;
11248 }
11249 else
11250 {
11251 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11252 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11253 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11254 if (plt_static_chain)
11255 {
11256 r[2].r_offset = r[1].r_offset + 4;
11257 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11258 r[2].r_addend = r[0].r_addend + 8;
11259 }
11260 }
11261 }
11262 }
11263 if (ALWAYS_EMIT_R2SAVE
11264 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11265 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11266 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11267 if (plt_load_toc
11268 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11269 {
11270 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11271 offset = 0;
11272 }
11273 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11274 if (plt_load_toc)
11275 {
11276 if (use_fake_dep)
11277 {
11278 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11279 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11280 }
11281 if (plt_static_chain)
11282 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11283 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11284 }
11285 }
11286 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11287 {
11288 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11289 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11290 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11291 }
11292 else
11293 bfd_put_32 (obfd, BCTR, p), p += 4;
11294 return p;
11295 }
11296
11297 /* Build a special .plt call stub for __tls_get_addr. */
11298
11299 #define LD_R11_0R3 0xe9630000
11300 #define LD_R12_0R3 0xe9830000
11301 #define MR_R0_R3 0x7c601b78
11302 #define CMPDI_R11_0 0x2c2b0000
11303 #define ADD_R3_R12_R13 0x7c6c6a14
11304 #define BEQLR 0x4d820020
11305 #define MR_R3_R0 0x7c030378
11306 #define STD_R11_0R1 0xf9610000
11307 #define BCTRL 0x4e800421
11308 #define LD_R11_0R1 0xe9610000
11309 #define MTLR_R11 0x7d6803a6
11310
11311 static inline bfd_byte *
11312 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11313 struct ppc_stub_hash_entry *stub_entry,
11314 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11315 {
11316 bfd *obfd = htab->params->stub_bfd;
11317 bfd_byte *loc = p;
11318
11319 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11320 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11321 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11322 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11323 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11324 bfd_put_32 (obfd, BEQLR, p), p += 4;
11325 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11326 if (r != NULL)
11327 r[0].r_offset += 7 * 4;
11328 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
11329 return build_plt_stub (htab, stub_entry, p, offset, r);
11330
11331 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
11332 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11333
11334 if (r != NULL)
11335 r[0].r_offset += 2 * 4;
11336 p = build_plt_stub (htab, stub_entry, p, offset, r);
11337 bfd_put_32 (obfd, BCTRL, p - 4);
11338
11339 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
11340 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11341 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11342 bfd_put_32 (obfd, BLR, p), p += 4;
11343
11344 if (htab->glink_eh_frame != NULL
11345 && htab->glink_eh_frame->size != 0)
11346 {
11347 bfd_byte *base, *eh;
11348 unsigned int lr_used, delta;
11349
11350 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11351 eh = base + stub_entry->group->eh_size;
11352 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11353 delta = lr_used - stub_entry->group->lr_restore;
11354 stub_entry->group->lr_restore = lr_used + 16;
11355 eh = eh_advance (htab->elf.dynobj, eh, delta);
11356 *eh++ = DW_CFA_offset_extended_sf;
11357 *eh++ = 65;
11358 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11359 *eh++ = DW_CFA_advance_loc + 4;
11360 *eh++ = DW_CFA_restore_extended;
11361 *eh++ = 65;
11362 stub_entry->group->eh_size = eh - base;
11363 }
11364 return p;
11365 }
11366
11367 static Elf_Internal_Rela *
11368 get_relocs (asection *sec, int count)
11369 {
11370 Elf_Internal_Rela *relocs;
11371 struct bfd_elf_section_data *elfsec_data;
11372
11373 elfsec_data = elf_section_data (sec);
11374 relocs = elfsec_data->relocs;
11375 if (relocs == NULL)
11376 {
11377 bfd_size_type relsize;
11378 relsize = sec->reloc_count * sizeof (*relocs);
11379 relocs = bfd_alloc (sec->owner, relsize);
11380 if (relocs == NULL)
11381 return NULL;
11382 elfsec_data->relocs = relocs;
11383 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11384 sizeof (Elf_Internal_Shdr));
11385 if (elfsec_data->rela.hdr == NULL)
11386 return NULL;
11387 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11388 * sizeof (Elf64_External_Rela));
11389 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11390 sec->reloc_count = 0;
11391 }
11392 relocs += sec->reloc_count;
11393 sec->reloc_count += count;
11394 return relocs;
11395 }
11396
11397 static bfd_vma
11398 get_r2off (struct bfd_link_info *info,
11399 struct ppc_stub_hash_entry *stub_entry)
11400 {
11401 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11402 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11403
11404 if (r2off == 0)
11405 {
11406 /* Support linking -R objects. Get the toc pointer from the
11407 opd entry. */
11408 char buf[8];
11409 if (!htab->opd_abi)
11410 return r2off;
11411 asection *opd = stub_entry->h->elf.root.u.def.section;
11412 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11413
11414 if (strcmp (opd->name, ".opd") != 0
11415 || opd->reloc_count != 0)
11416 {
11417 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11418 stub_entry->h->elf.root.root.string);
11419 bfd_set_error (bfd_error_bad_value);
11420 return (bfd_vma) -1;
11421 }
11422 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11423 return (bfd_vma) -1;
11424 r2off = bfd_get_64 (opd->owner, buf);
11425 r2off -= elf_gp (info->output_bfd);
11426 }
11427 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11428 return r2off;
11429 }
11430
11431 static bfd_boolean
11432 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11433 {
11434 struct ppc_stub_hash_entry *stub_entry;
11435 struct ppc_branch_hash_entry *br_entry;
11436 struct bfd_link_info *info;
11437 struct ppc_link_hash_table *htab;
11438 bfd_byte *loc;
11439 bfd_byte *p;
11440 bfd_vma targ, off;
11441 Elf_Internal_Rela *r;
11442 asection *plt;
11443
11444 /* Massage our args to the form they really have. */
11445 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11446 info = in_arg;
11447
11448 htab = ppc_hash_table (info);
11449 if (htab == NULL)
11450 return FALSE;
11451
11452 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11453 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11454
11455 htab->stub_count[stub_entry->stub_type - 1] += 1;
11456 switch (stub_entry->stub_type)
11457 {
11458 case ppc_stub_long_branch:
11459 case ppc_stub_long_branch_r2off:
11460 /* Branches are relative. This is where we are going to. */
11461 targ = (stub_entry->target_value
11462 + stub_entry->target_section->output_offset
11463 + stub_entry->target_section->output_section->vma);
11464 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11465
11466 /* And this is where we are coming from. */
11467 off = (stub_entry->stub_offset
11468 + stub_entry->group->stub_sec->output_offset
11469 + stub_entry->group->stub_sec->output_section->vma);
11470 off = targ - off;
11471
11472 p = loc;
11473 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11474 {
11475 bfd_vma r2off = get_r2off (info, stub_entry);
11476
11477 if (r2off == (bfd_vma) -1)
11478 {
11479 htab->stub_error = TRUE;
11480 return FALSE;
11481 }
11482 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11483 p += 4;
11484 if (PPC_HA (r2off) != 0)
11485 {
11486 bfd_put_32 (htab->params->stub_bfd,
11487 ADDIS_R2_R2 | PPC_HA (r2off), p);
11488 p += 4;
11489 }
11490 if (PPC_LO (r2off) != 0)
11491 {
11492 bfd_put_32 (htab->params->stub_bfd,
11493 ADDI_R2_R2 | PPC_LO (r2off), p);
11494 p += 4;
11495 }
11496 off -= p - loc;
11497 }
11498 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11499 p += 4;
11500
11501 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11502 {
11503 _bfd_error_handler
11504 (_("long branch stub `%s' offset overflow"),
11505 stub_entry->root.string);
11506 htab->stub_error = TRUE;
11507 return FALSE;
11508 }
11509
11510 if (info->emitrelocations)
11511 {
11512 r = get_relocs (stub_entry->group->stub_sec, 1);
11513 if (r == NULL)
11514 return FALSE;
11515 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11516 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11517 r->r_addend = targ;
11518 if (stub_entry->h != NULL)
11519 {
11520 struct elf_link_hash_entry **hashes;
11521 unsigned long symndx;
11522 struct ppc_link_hash_entry *h;
11523
11524 hashes = elf_sym_hashes (htab->params->stub_bfd);
11525 if (hashes == NULL)
11526 {
11527 bfd_size_type hsize;
11528
11529 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11530 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11531 if (hashes == NULL)
11532 return FALSE;
11533 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11534 htab->stub_globals = 1;
11535 }
11536 symndx = htab->stub_globals++;
11537 h = stub_entry->h;
11538 hashes[symndx] = &h->elf;
11539 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11540 if (h->oh != NULL && h->oh->is_func)
11541 h = ppc_follow_link (h->oh);
11542 if (h->elf.root.u.def.section != stub_entry->target_section)
11543 /* H is an opd symbol. The addend must be zero. */
11544 r->r_addend = 0;
11545 else
11546 {
11547 off = (h->elf.root.u.def.value
11548 + h->elf.root.u.def.section->output_offset
11549 + h->elf.root.u.def.section->output_section->vma);
11550 r->r_addend -= off;
11551 }
11552 }
11553 }
11554 break;
11555
11556 case ppc_stub_plt_branch:
11557 case ppc_stub_plt_branch_r2off:
11558 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11559 stub_entry->root.string + 9,
11560 FALSE, FALSE);
11561 if (br_entry == NULL)
11562 {
11563 _bfd_error_handler (_("can't find branch stub `%s'"),
11564 stub_entry->root.string);
11565 htab->stub_error = TRUE;
11566 return FALSE;
11567 }
11568
11569 targ = (stub_entry->target_value
11570 + stub_entry->target_section->output_offset
11571 + stub_entry->target_section->output_section->vma);
11572 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11573 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11574
11575 bfd_put_64 (htab->brlt->owner, targ,
11576 htab->brlt->contents + br_entry->offset);
11577
11578 if (br_entry->iter == htab->stub_iteration)
11579 {
11580 br_entry->iter = 0;
11581
11582 if (htab->relbrlt != NULL)
11583 {
11584 /* Create a reloc for the branch lookup table entry. */
11585 Elf_Internal_Rela rela;
11586 bfd_byte *rl;
11587
11588 rela.r_offset = (br_entry->offset
11589 + htab->brlt->output_offset
11590 + htab->brlt->output_section->vma);
11591 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11592 rela.r_addend = targ;
11593
11594 rl = htab->relbrlt->contents;
11595 rl += (htab->relbrlt->reloc_count++
11596 * sizeof (Elf64_External_Rela));
11597 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11598 }
11599 else if (info->emitrelocations)
11600 {
11601 r = get_relocs (htab->brlt, 1);
11602 if (r == NULL)
11603 return FALSE;
11604 /* brlt, being SEC_LINKER_CREATED does not go through the
11605 normal reloc processing. Symbols and offsets are not
11606 translated from input file to output file form, so
11607 set up the offset per the output file. */
11608 r->r_offset = (br_entry->offset
11609 + htab->brlt->output_offset
11610 + htab->brlt->output_section->vma);
11611 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11612 r->r_addend = targ;
11613 }
11614 }
11615
11616 targ = (br_entry->offset
11617 + htab->brlt->output_offset
11618 + htab->brlt->output_section->vma);
11619
11620 off = (elf_gp (info->output_bfd)
11621 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11622 off = targ - off;
11623
11624 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11625 {
11626 info->callbacks->einfo
11627 (_("%P: linkage table error against `%pT'\n"),
11628 stub_entry->root.string);
11629 bfd_set_error (bfd_error_bad_value);
11630 htab->stub_error = TRUE;
11631 return FALSE;
11632 }
11633
11634 if (info->emitrelocations)
11635 {
11636 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11637 if (r == NULL)
11638 return FALSE;
11639 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11640 if (bfd_big_endian (info->output_bfd))
11641 r[0].r_offset += 2;
11642 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11643 r[0].r_offset += 4;
11644 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11645 r[0].r_addend = targ;
11646 if (PPC_HA (off) != 0)
11647 {
11648 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11649 r[1].r_offset = r[0].r_offset + 4;
11650 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11651 r[1].r_addend = r[0].r_addend;
11652 }
11653 }
11654
11655 p = loc;
11656 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11657 {
11658 if (PPC_HA (off) != 0)
11659 {
11660 bfd_put_32 (htab->params->stub_bfd,
11661 ADDIS_R12_R2 | PPC_HA (off), p);
11662 p += 4;
11663 bfd_put_32 (htab->params->stub_bfd,
11664 LD_R12_0R12 | PPC_LO (off), p);
11665 }
11666 else
11667 bfd_put_32 (htab->params->stub_bfd,
11668 LD_R12_0R2 | PPC_LO (off), p);
11669 }
11670 else
11671 {
11672 bfd_vma r2off = get_r2off (info, stub_entry);
11673
11674 if (r2off == (bfd_vma) -1)
11675 {
11676 htab->stub_error = TRUE;
11677 return FALSE;
11678 }
11679
11680 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11681 p += 4;
11682 if (PPC_HA (off) != 0)
11683 {
11684 bfd_put_32 (htab->params->stub_bfd,
11685 ADDIS_R12_R2 | PPC_HA (off), p);
11686 p += 4;
11687 bfd_put_32 (htab->params->stub_bfd,
11688 LD_R12_0R12 | PPC_LO (off), p);
11689 }
11690 else
11691 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11692
11693 if (PPC_HA (r2off) != 0)
11694 {
11695 p += 4;
11696 bfd_put_32 (htab->params->stub_bfd,
11697 ADDIS_R2_R2 | PPC_HA (r2off), p);
11698 }
11699 if (PPC_LO (r2off) != 0)
11700 {
11701 p += 4;
11702 bfd_put_32 (htab->params->stub_bfd,
11703 ADDI_R2_R2 | PPC_LO (r2off), p);
11704 }
11705 }
11706 p += 4;
11707 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11708 p += 4;
11709 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11710 p += 4;
11711 break;
11712
11713 case ppc_stub_long_branch_notoc:
11714 case ppc_stub_long_branch_both:
11715 case ppc_stub_plt_branch_notoc:
11716 case ppc_stub_plt_branch_both:
11717 case ppc_stub_plt_call_notoc:
11718 case ppc_stub_plt_call_both:
11719 p = loc;
11720 off = (8 + stub_entry->stub_offset
11721 + stub_entry->group->stub_sec->output_offset
11722 + stub_entry->group->stub_sec->output_section->vma);
11723 if (stub_entry->stub_type == ppc_stub_long_branch_both
11724 || stub_entry->stub_type == ppc_stub_plt_branch_both
11725 || stub_entry->stub_type == ppc_stub_plt_call_both)
11726 {
11727 off += 4;
11728 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11729 p += 4;
11730 }
11731 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11732 {
11733 targ = stub_entry->plt_ent->plt.offset & ~1;
11734 if (targ >= (bfd_vma) -2)
11735 abort ();
11736
11737 plt = htab->elf.splt;
11738 if (!htab->elf.dynamic_sections_created
11739 || stub_entry->h == NULL
11740 || stub_entry->h->elf.dynindx == -1)
11741 {
11742 if (stub_entry->symtype == STT_GNU_IFUNC)
11743 plt = htab->elf.iplt;
11744 else
11745 plt = htab->pltlocal;
11746 }
11747 targ += plt->output_offset + plt->output_section->vma;
11748 }
11749 else
11750 targ = (stub_entry->target_value
11751 + stub_entry->target_section->output_offset
11752 + stub_entry->target_section->output_section->vma);
11753 off = targ - off;
11754 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
11755 p += 4;
11756 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
11757 p += 4;
11758 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
11759 p += 4;
11760 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
11761 p += 4;
11762 p = build_offset (htab->params->stub_bfd, p, off,
11763 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11764 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
11765 {
11766 off += 8;
11767 bfd_put_32 (htab->params->stub_bfd,
11768 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11769 }
11770 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
11771 {
11772 off += 12;
11773 bfd_put_32 (htab->params->stub_bfd,
11774 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11775 }
11776 else
11777 {
11778 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11779 p += 4;
11780 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11781 }
11782 p += 4;
11783
11784 if (htab->glink_eh_frame != NULL
11785 && htab->glink_eh_frame->size != 0)
11786 {
11787 bfd_byte *base, *eh;
11788 unsigned int lr_used, delta;
11789
11790 base = (htab->glink_eh_frame->contents
11791 + stub_entry->group->eh_base + 17);
11792 eh = base + stub_entry->group->eh_size;
11793 lr_used = stub_entry->stub_offset + 8;
11794 if (stub_entry->stub_type == ppc_stub_long_branch_both
11795 || stub_entry->stub_type == ppc_stub_plt_branch_both
11796 || stub_entry->stub_type == ppc_stub_plt_call_both)
11797 lr_used += 4;
11798 delta = lr_used - stub_entry->group->lr_restore;
11799 stub_entry->group->lr_restore = lr_used + 8;
11800 eh = eh_advance (htab->elf.dynobj, eh, delta);
11801 *eh++ = DW_CFA_register;
11802 *eh++ = 65;
11803 *eh++ = 12;
11804 *eh++ = DW_CFA_advance_loc + 2;
11805 *eh++ = DW_CFA_restore_extended;
11806 *eh++ = 65;
11807 stub_entry->group->eh_size = eh - base;
11808 }
11809 break;
11810
11811 case ppc_stub_plt_call:
11812 case ppc_stub_plt_call_r2save:
11813 if (stub_entry->h != NULL
11814 && stub_entry->h->is_func_descriptor
11815 && stub_entry->h->oh != NULL)
11816 {
11817 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11818
11819 /* If the old-ABI "dot-symbol" is undefined make it weak so
11820 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11821 if (fh->elf.root.type == bfd_link_hash_undefined
11822 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11823 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11824 fh->elf.root.type = bfd_link_hash_undefweak;
11825 }
11826
11827 /* Now build the stub. */
11828 targ = stub_entry->plt_ent->plt.offset & ~1;
11829 if (targ >= (bfd_vma) -2)
11830 abort ();
11831
11832 plt = htab->elf.splt;
11833 if (!htab->elf.dynamic_sections_created
11834 || stub_entry->h == NULL
11835 || stub_entry->h->elf.dynindx == -1)
11836 {
11837 if (stub_entry->symtype == STT_GNU_IFUNC)
11838 plt = htab->elf.iplt;
11839 else
11840 plt = htab->pltlocal;
11841 }
11842 targ += plt->output_offset + plt->output_section->vma;
11843
11844 off = (elf_gp (info->output_bfd)
11845 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11846 off = targ - off;
11847
11848 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11849 {
11850 info->callbacks->einfo
11851 /* xgettext:c-format */
11852 (_("%P: linkage table error against `%pT'\n"),
11853 stub_entry->h != NULL
11854 ? stub_entry->h->elf.root.root.string
11855 : "<local sym>");
11856 bfd_set_error (bfd_error_bad_value);
11857 htab->stub_error = TRUE;
11858 return FALSE;
11859 }
11860
11861 r = NULL;
11862 if (info->emitrelocations)
11863 {
11864 r = get_relocs (stub_entry->group->stub_sec,
11865 ((PPC_HA (off) != 0)
11866 + (htab->opd_abi
11867 ? 2 + (htab->params->plt_static_chain
11868 && PPC_HA (off + 16) == PPC_HA (off))
11869 : 1)));
11870 if (r == NULL)
11871 return FALSE;
11872 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11873 if (bfd_big_endian (info->output_bfd))
11874 r[0].r_offset += 2;
11875 r[0].r_addend = targ;
11876 }
11877 if (stub_entry->h != NULL
11878 && (stub_entry->h == htab->tls_get_addr_fd
11879 || stub_entry->h == htab->tls_get_addr)
11880 && htab->params->tls_get_addr_opt)
11881 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11882 else
11883 p = build_plt_stub (htab, stub_entry, loc, off, r);
11884 break;
11885
11886 case ppc_stub_save_res:
11887 return TRUE;
11888
11889 default:
11890 BFD_FAIL ();
11891 return FALSE;
11892 }
11893
11894 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11895
11896 if (htab->params->emit_stub_syms)
11897 {
11898 struct elf_link_hash_entry *h;
11899 size_t len1, len2;
11900 char *name;
11901 const char *const stub_str[] = { "long_branch",
11902 "long_branch",
11903 "long_branch",
11904 "long_branch",
11905 "plt_branch",
11906 "plt_branch",
11907 "plt_branch",
11908 "plt_branch",
11909 "plt_call",
11910 "plt_call",
11911 "plt_call",
11912 "plt_call" };
11913
11914 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11915 len2 = strlen (stub_entry->root.string);
11916 name = bfd_malloc (len1 + len2 + 2);
11917 if (name == NULL)
11918 return FALSE;
11919 memcpy (name, stub_entry->root.string, 9);
11920 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11921 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11922 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11923 if (h == NULL)
11924 return FALSE;
11925 if (h->root.type == bfd_link_hash_new)
11926 {
11927 h->root.type = bfd_link_hash_defined;
11928 h->root.u.def.section = stub_entry->group->stub_sec;
11929 h->root.u.def.value = stub_entry->stub_offset;
11930 h->ref_regular = 1;
11931 h->def_regular = 1;
11932 h->ref_regular_nonweak = 1;
11933 h->forced_local = 1;
11934 h->non_elf = 0;
11935 h->root.linker_def = 1;
11936 }
11937 }
11938
11939 return TRUE;
11940 }
11941
11942 /* As above, but don't actually build the stub. Just bump offset so
11943 we know stub section sizes, and select plt_branch stubs where
11944 long_branch stubs won't do. */
11945
11946 static bfd_boolean
11947 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11948 {
11949 struct ppc_stub_hash_entry *stub_entry;
11950 struct bfd_link_info *info;
11951 struct ppc_link_hash_table *htab;
11952 bfd_vma targ, off;
11953 int size;
11954
11955 /* Massage our args to the form they really have. */
11956 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11957 info = in_arg;
11958
11959 htab = ppc_hash_table (info);
11960 if (htab == NULL)
11961 return FALSE;
11962
11963 /* Make a note of the offset within the stubs for this entry. */
11964 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11965
11966 if (stub_entry->h != NULL
11967 && stub_entry->h->save_res
11968 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11969 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11970 {
11971 /* Don't make stubs to out-of-line register save/restore
11972 functions. Instead, emit copies of the functions. */
11973 stub_entry->group->needs_save_res = 1;
11974 stub_entry->stub_type = ppc_stub_save_res;
11975 return TRUE;
11976 }
11977
11978 if (stub_entry->stub_type >= ppc_stub_plt_call
11979 && stub_entry->stub_type <= ppc_stub_plt_call_both)
11980 {
11981 asection *plt;
11982 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11983 if (targ >= (bfd_vma) -2)
11984 abort ();
11985 plt = htab->elf.splt;
11986 if (!htab->elf.dynamic_sections_created
11987 || stub_entry->h == NULL
11988 || stub_entry->h->elf.dynindx == -1)
11989 {
11990 if (stub_entry->symtype == STT_GNU_IFUNC)
11991 plt = htab->elf.iplt;
11992 else
11993 plt = htab->pltlocal;
11994 }
11995 targ += plt->output_offset + plt->output_section->vma;
11996
11997 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11998 {
11999 off = (8 + stub_entry->stub_offset
12000 + stub_entry->group->stub_sec->output_offset
12001 + stub_entry->group->stub_sec->output_section->vma);
12002 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
12003 off += 4;
12004 }
12005 else
12006 off = (elf_gp (info->output_bfd)
12007 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12008
12009 if (htab->params->plt_stub_align != 0)
12010 {
12011 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
12012
12013 stub_entry->group->stub_sec->size += pad;
12014 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12015 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12016 off += pad;
12017 }
12018
12019 off = targ - off;
12020 size = plt_stub_size (htab, stub_entry, off);
12021
12022 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12023 {
12024 /* After the bcl, lr has been modified so we need to emit
12025 .eh_frame info saying the return address is in r12. */
12026 unsigned int lr_used = stub_entry->stub_offset + 8;
12027 unsigned int delta;
12028 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
12029 lr_used += 4;
12030 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12031 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12032 DW_CFA_restore_extended 65. */
12033 delta = lr_used - stub_entry->group->lr_restore;
12034 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12035 stub_entry->group->lr_restore = lr_used + 8;
12036 }
12037 else
12038 {
12039 if (stub_entry->h != NULL
12040 && (stub_entry->h == htab->tls_get_addr_fd
12041 || stub_entry->h == htab->tls_get_addr)
12042 && htab->params->tls_get_addr_opt
12043 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12044 {
12045 /* After the bctrl, lr has been modified so we need to
12046 emit .eh_frame info saying the return address is
12047 on the stack. In fact we put the EH info specifying
12048 that the return address is on the stack *at* the
12049 call rather than after it, because the EH info for a
12050 call needs to be specified by that point.
12051 See libgcc/unwind-dw2.c execute_cfa_program. */
12052 unsigned int lr_used = stub_entry->stub_offset + size - 20;
12053 unsigned int delta;
12054 /* The eh_frame info will consist of a DW_CFA_advance_loc
12055 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12056 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12057 delta = lr_used - stub_entry->group->lr_restore;
12058 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12059 stub_entry->group->lr_restore = size - 4;
12060 }
12061
12062 if (info->emitrelocations)
12063 {
12064 stub_entry->group->stub_sec->reloc_count
12065 += ((PPC_HA (off) != 0)
12066 + (htab->opd_abi
12067 ? 2 + (htab->params->plt_static_chain
12068 && PPC_HA (off + 16) == PPC_HA (off))
12069 : 1));
12070 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12071 }
12072 }
12073 }
12074 else
12075 {
12076 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
12077 variants. */
12078 bfd_vma r2off = 0;
12079 bfd_vma local_off = 0;
12080
12081 targ = (stub_entry->target_value
12082 + stub_entry->target_section->output_offset
12083 + stub_entry->target_section->output_section->vma);
12084 off = (stub_entry->stub_offset
12085 + stub_entry->group->stub_sec->output_offset
12086 + stub_entry->group->stub_sec->output_section->vma);
12087
12088 /* Reset the stub type from the plt variant in case we now
12089 can reach with a shorter stub. */
12090 if (stub_entry->stub_type >= ppc_stub_plt_branch)
12091 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12092
12093 size = 4;
12094 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12095 {
12096 r2off = get_r2off (info, stub_entry);
12097 if (r2off == (bfd_vma) -1)
12098 {
12099 htab->stub_error = TRUE;
12100 return FALSE;
12101 }
12102 size = 8;
12103 if (PPC_HA (r2off) != 0)
12104 size += 4;
12105 if (PPC_LO (r2off) != 0)
12106 size += 4;
12107 off += size - 4;
12108 }
12109 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
12110 {
12111 size = 20 + size_offset (targ - (off + 8));
12112 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
12113 size += 4;
12114 off += size - 4;
12115 }
12116 off = targ - off;
12117
12118 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
12119 {
12120 /* After the bcl, lr has been modified so we need to emit
12121 .eh_frame info saying the return address is in r12. */
12122 unsigned int lr_used = stub_entry->stub_offset + 8;
12123 unsigned int delta;
12124 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
12125 lr_used += 4;
12126 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12127 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12128 DW_CFA_restore_extended 65. */
12129 delta = lr_used - stub_entry->group->lr_restore;
12130 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12131 stub_entry->group->lr_restore = lr_used + 8;
12132
12133 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12134 {
12135 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12136 - ppc_stub_long_branch_notoc);
12137 size += 4;
12138 }
12139 }
12140 else
12141 {
12142 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12143
12144 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12145 Do the same for -R objects without function descriptors. */
12146 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12147 && r2off == 0
12148 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12149 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
12150 {
12151 struct ppc_branch_hash_entry *br_entry;
12152
12153 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12154 stub_entry->root.string + 9,
12155 TRUE, FALSE);
12156 if (br_entry == NULL)
12157 {
12158 _bfd_error_handler (_("can't build branch stub `%s'"),
12159 stub_entry->root.string);
12160 htab->stub_error = TRUE;
12161 return FALSE;
12162 }
12163
12164 if (br_entry->iter != htab->stub_iteration)
12165 {
12166 br_entry->iter = htab->stub_iteration;
12167 br_entry->offset = htab->brlt->size;
12168 htab->brlt->size += 8;
12169
12170 if (htab->relbrlt != NULL)
12171 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12172 else if (info->emitrelocations)
12173 {
12174 htab->brlt->reloc_count += 1;
12175 htab->brlt->flags |= SEC_RELOC;
12176 }
12177 }
12178
12179 targ = (br_entry->offset
12180 + htab->brlt->output_offset
12181 + htab->brlt->output_section->vma);
12182 off = (elf_gp (info->output_bfd)
12183 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12184 off = targ - off;
12185
12186 if (info->emitrelocations)
12187 {
12188 stub_entry->group->stub_sec->reloc_count
12189 += 1 + (PPC_HA (off) != 0);
12190 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12191 }
12192
12193 stub_entry->stub_type
12194 += ppc_stub_plt_branch - ppc_stub_long_branch;
12195 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12196 {
12197 size = 12;
12198 if (PPC_HA (off) != 0)
12199 size = 16;
12200 }
12201 else
12202 {
12203 size = 16;
12204 if (PPC_HA (off) != 0)
12205 size += 4;
12206
12207 if (PPC_HA (r2off) != 0)
12208 size += 4;
12209 if (PPC_LO (r2off) != 0)
12210 size += 4;
12211 }
12212 }
12213 else if (info->emitrelocations)
12214 {
12215 stub_entry->group->stub_sec->reloc_count += 1;
12216 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12217 }
12218 }
12219 }
12220
12221 stub_entry->group->stub_sec->size += size;
12222 return TRUE;
12223 }
12224
12225 /* Set up various things so that we can make a list of input sections
12226 for each output section included in the link. Returns -1 on error,
12227 0 when no stubs will be needed, and 1 on success. */
12228
12229 int
12230 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12231 {
12232 unsigned int id;
12233 bfd_size_type amt;
12234 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12235
12236 if (htab == NULL)
12237 return -1;
12238
12239 htab->sec_info_arr_size = _bfd_section_id;
12240 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12241 htab->sec_info = bfd_zmalloc (amt);
12242 if (htab->sec_info == NULL)
12243 return -1;
12244
12245 /* Set toc_off for com, und, abs and ind sections. */
12246 for (id = 0; id < 3; id++)
12247 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12248
12249 return 1;
12250 }
12251
12252 /* Set up for first pass at multitoc partitioning. */
12253
12254 void
12255 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12256 {
12257 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12258
12259 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12260 htab->toc_bfd = NULL;
12261 htab->toc_first_sec = NULL;
12262 }
12263
12264 /* The linker repeatedly calls this function for each TOC input section
12265 and linker generated GOT section. Group input bfds such that the toc
12266 within a group is less than 64k in size. */
12267
12268 bfd_boolean
12269 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12270 {
12271 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12272 bfd_vma addr, off, limit;
12273
12274 if (htab == NULL)
12275 return FALSE;
12276
12277 if (!htab->second_toc_pass)
12278 {
12279 /* Keep track of the first .toc or .got section for this input bfd. */
12280 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12281
12282 if (new_bfd)
12283 {
12284 htab->toc_bfd = isec->owner;
12285 htab->toc_first_sec = isec;
12286 }
12287
12288 addr = isec->output_offset + isec->output_section->vma;
12289 off = addr - htab->toc_curr;
12290 limit = 0x80008000;
12291 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12292 limit = 0x10000;
12293 if (off + isec->size > limit)
12294 {
12295 addr = (htab->toc_first_sec->output_offset
12296 + htab->toc_first_sec->output_section->vma);
12297 htab->toc_curr = addr;
12298 htab->toc_curr &= -TOC_BASE_ALIGN;
12299 }
12300
12301 /* toc_curr is the base address of this toc group. Set elf_gp
12302 for the input section to be the offset relative to the
12303 output toc base plus 0x8000. Making the input elf_gp an
12304 offset allows us to move the toc as a whole without
12305 recalculating input elf_gp. */
12306 off = htab->toc_curr - elf_gp (info->output_bfd);
12307 off += TOC_BASE_OFF;
12308
12309 /* Die if someone uses a linker script that doesn't keep input
12310 file .toc and .got together. */
12311 if (new_bfd
12312 && elf_gp (isec->owner) != 0
12313 && elf_gp (isec->owner) != off)
12314 return FALSE;
12315
12316 elf_gp (isec->owner) = off;
12317 return TRUE;
12318 }
12319
12320 /* During the second pass toc_first_sec points to the start of
12321 a toc group, and toc_curr is used to track the old elf_gp.
12322 We use toc_bfd to ensure we only look at each bfd once. */
12323 if (htab->toc_bfd == isec->owner)
12324 return TRUE;
12325 htab->toc_bfd = isec->owner;
12326
12327 if (htab->toc_first_sec == NULL
12328 || htab->toc_curr != elf_gp (isec->owner))
12329 {
12330 htab->toc_curr = elf_gp (isec->owner);
12331 htab->toc_first_sec = isec;
12332 }
12333 addr = (htab->toc_first_sec->output_offset
12334 + htab->toc_first_sec->output_section->vma);
12335 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12336 elf_gp (isec->owner) = off;
12337
12338 return TRUE;
12339 }
12340
12341 /* Called via elf_link_hash_traverse to merge GOT entries for global
12342 symbol H. */
12343
12344 static bfd_boolean
12345 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12346 {
12347 if (h->root.type == bfd_link_hash_indirect)
12348 return TRUE;
12349
12350 merge_got_entries (&h->got.glist);
12351
12352 return TRUE;
12353 }
12354
12355 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12356 symbol H. */
12357
12358 static bfd_boolean
12359 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12360 {
12361 struct got_entry *gent;
12362
12363 if (h->root.type == bfd_link_hash_indirect)
12364 return TRUE;
12365
12366 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12367 if (!gent->is_indirect)
12368 allocate_got (h, (struct bfd_link_info *) inf, gent);
12369 return TRUE;
12370 }
12371
12372 /* Called on the first multitoc pass after the last call to
12373 ppc64_elf_next_toc_section. This function removes duplicate GOT
12374 entries. */
12375
12376 bfd_boolean
12377 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12378 {
12379 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12380 struct bfd *ibfd, *ibfd2;
12381 bfd_boolean done_something;
12382
12383 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12384
12385 if (!htab->do_multi_toc)
12386 return FALSE;
12387
12388 /* Merge global sym got entries within a toc group. */
12389 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12390
12391 /* And tlsld_got. */
12392 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12393 {
12394 struct got_entry *ent, *ent2;
12395
12396 if (!is_ppc64_elf (ibfd))
12397 continue;
12398
12399 ent = ppc64_tlsld_got (ibfd);
12400 if (!ent->is_indirect
12401 && ent->got.offset != (bfd_vma) -1)
12402 {
12403 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12404 {
12405 if (!is_ppc64_elf (ibfd2))
12406 continue;
12407
12408 ent2 = ppc64_tlsld_got (ibfd2);
12409 if (!ent2->is_indirect
12410 && ent2->got.offset != (bfd_vma) -1
12411 && elf_gp (ibfd2) == elf_gp (ibfd))
12412 {
12413 ent2->is_indirect = TRUE;
12414 ent2->got.ent = ent;
12415 }
12416 }
12417 }
12418 }
12419
12420 /* Zap sizes of got sections. */
12421 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12422 htab->elf.irelplt->size -= htab->got_reli_size;
12423 htab->got_reli_size = 0;
12424
12425 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12426 {
12427 asection *got, *relgot;
12428
12429 if (!is_ppc64_elf (ibfd))
12430 continue;
12431
12432 got = ppc64_elf_tdata (ibfd)->got;
12433 if (got != NULL)
12434 {
12435 got->rawsize = got->size;
12436 got->size = 0;
12437 relgot = ppc64_elf_tdata (ibfd)->relgot;
12438 relgot->rawsize = relgot->size;
12439 relgot->size = 0;
12440 }
12441 }
12442
12443 /* Now reallocate the got, local syms first. We don't need to
12444 allocate section contents again since we never increase size. */
12445 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12446 {
12447 struct got_entry **lgot_ents;
12448 struct got_entry **end_lgot_ents;
12449 struct plt_entry **local_plt;
12450 struct plt_entry **end_local_plt;
12451 unsigned char *lgot_masks;
12452 bfd_size_type locsymcount;
12453 Elf_Internal_Shdr *symtab_hdr;
12454 asection *s;
12455
12456 if (!is_ppc64_elf (ibfd))
12457 continue;
12458
12459 lgot_ents = elf_local_got_ents (ibfd);
12460 if (!lgot_ents)
12461 continue;
12462
12463 symtab_hdr = &elf_symtab_hdr (ibfd);
12464 locsymcount = symtab_hdr->sh_info;
12465 end_lgot_ents = lgot_ents + locsymcount;
12466 local_plt = (struct plt_entry **) end_lgot_ents;
12467 end_local_plt = local_plt + locsymcount;
12468 lgot_masks = (unsigned char *) end_local_plt;
12469 s = ppc64_elf_tdata (ibfd)->got;
12470 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12471 {
12472 struct got_entry *ent;
12473
12474 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12475 {
12476 unsigned int ent_size = 8;
12477 unsigned int rel_size = sizeof (Elf64_External_Rela);
12478
12479 ent->got.offset = s->size;
12480 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12481 {
12482 ent_size *= 2;
12483 rel_size *= 2;
12484 }
12485 s->size += ent_size;
12486 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12487 {
12488 htab->elf.irelplt->size += rel_size;
12489 htab->got_reli_size += rel_size;
12490 }
12491 else if (bfd_link_pic (info)
12492 && !((ent->tls_type & TLS_TPREL) != 0
12493 && bfd_link_executable (info)))
12494 {
12495 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12496 srel->size += rel_size;
12497 }
12498 }
12499 }
12500 }
12501
12502 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12503
12504 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12505 {
12506 struct got_entry *ent;
12507
12508 if (!is_ppc64_elf (ibfd))
12509 continue;
12510
12511 ent = ppc64_tlsld_got (ibfd);
12512 if (!ent->is_indirect
12513 && ent->got.offset != (bfd_vma) -1)
12514 {
12515 asection *s = ppc64_elf_tdata (ibfd)->got;
12516 ent->got.offset = s->size;
12517 s->size += 16;
12518 if (bfd_link_pic (info))
12519 {
12520 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12521 srel->size += sizeof (Elf64_External_Rela);
12522 }
12523 }
12524 }
12525
12526 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12527 if (!done_something)
12528 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12529 {
12530 asection *got;
12531
12532 if (!is_ppc64_elf (ibfd))
12533 continue;
12534
12535 got = ppc64_elf_tdata (ibfd)->got;
12536 if (got != NULL)
12537 {
12538 done_something = got->rawsize != got->size;
12539 if (done_something)
12540 break;
12541 }
12542 }
12543
12544 if (done_something)
12545 (*htab->params->layout_sections_again) ();
12546
12547 /* Set up for second pass over toc sections to recalculate elf_gp
12548 on input sections. */
12549 htab->toc_bfd = NULL;
12550 htab->toc_first_sec = NULL;
12551 htab->second_toc_pass = TRUE;
12552 return done_something;
12553 }
12554
12555 /* Called after second pass of multitoc partitioning. */
12556
12557 void
12558 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12559 {
12560 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12561
12562 /* After the second pass, toc_curr tracks the TOC offset used
12563 for code sections below in ppc64_elf_next_input_section. */
12564 htab->toc_curr = TOC_BASE_OFF;
12565 }
12566
12567 /* No toc references were found in ISEC. If the code in ISEC makes no
12568 calls, then there's no need to use toc adjusting stubs when branching
12569 into ISEC. Actually, indirect calls from ISEC are OK as they will
12570 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12571 needed, and 2 if a cyclical call-graph was found but no other reason
12572 for a stub was detected. If called from the top level, a return of
12573 2 means the same as a return of 0. */
12574
12575 static int
12576 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12577 {
12578 int ret;
12579
12580 /* Mark this section as checked. */
12581 isec->call_check_done = 1;
12582
12583 /* We know none of our code bearing sections will need toc stubs. */
12584 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12585 return 0;
12586
12587 if (isec->size == 0)
12588 return 0;
12589
12590 if (isec->output_section == NULL)
12591 return 0;
12592
12593 ret = 0;
12594 if (isec->reloc_count != 0)
12595 {
12596 Elf_Internal_Rela *relstart, *rel;
12597 Elf_Internal_Sym *local_syms;
12598 struct ppc_link_hash_table *htab;
12599
12600 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12601 info->keep_memory);
12602 if (relstart == NULL)
12603 return -1;
12604
12605 /* Look for branches to outside of this section. */
12606 local_syms = NULL;
12607 htab = ppc_hash_table (info);
12608 if (htab == NULL)
12609 return -1;
12610
12611 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12612 {
12613 enum elf_ppc64_reloc_type r_type;
12614 unsigned long r_symndx;
12615 struct elf_link_hash_entry *h;
12616 struct ppc_link_hash_entry *eh;
12617 Elf_Internal_Sym *sym;
12618 asection *sym_sec;
12619 struct _opd_sec_data *opd;
12620 bfd_vma sym_value;
12621 bfd_vma dest;
12622
12623 r_type = ELF64_R_TYPE (rel->r_info);
12624 if (r_type != R_PPC64_REL24
12625 && r_type != R_PPC64_REL24_NOTOC
12626 && r_type != R_PPC64_REL14
12627 && r_type != R_PPC64_REL14_BRTAKEN
12628 && r_type != R_PPC64_REL14_BRNTAKEN
12629 && r_type != R_PPC64_PLTCALL)
12630 continue;
12631
12632 r_symndx = ELF64_R_SYM (rel->r_info);
12633 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12634 isec->owner))
12635 {
12636 ret = -1;
12637 break;
12638 }
12639
12640 /* Calls to dynamic lib functions go through a plt call stub
12641 that uses r2. */
12642 eh = (struct ppc_link_hash_entry *) h;
12643 if (eh != NULL
12644 && (eh->elf.plt.plist != NULL
12645 || (eh->oh != NULL
12646 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12647 {
12648 ret = 1;
12649 break;
12650 }
12651
12652 if (sym_sec == NULL)
12653 /* Ignore other undefined symbols. */
12654 continue;
12655
12656 /* Assume branches to other sections not included in the
12657 link need stubs too, to cover -R and absolute syms. */
12658 if (sym_sec->output_section == NULL)
12659 {
12660 ret = 1;
12661 break;
12662 }
12663
12664 if (h == NULL)
12665 sym_value = sym->st_value;
12666 else
12667 {
12668 if (h->root.type != bfd_link_hash_defined
12669 && h->root.type != bfd_link_hash_defweak)
12670 abort ();
12671 sym_value = h->root.u.def.value;
12672 }
12673 sym_value += rel->r_addend;
12674
12675 /* If this branch reloc uses an opd sym, find the code section. */
12676 opd = get_opd_info (sym_sec);
12677 if (opd != NULL)
12678 {
12679 if (h == NULL && opd->adjust != NULL)
12680 {
12681 long adjust;
12682
12683 adjust = opd->adjust[OPD_NDX (sym_value)];
12684 if (adjust == -1)
12685 /* Assume deleted functions won't ever be called. */
12686 continue;
12687 sym_value += adjust;
12688 }
12689
12690 dest = opd_entry_value (sym_sec, sym_value,
12691 &sym_sec, NULL, FALSE);
12692 if (dest == (bfd_vma) -1)
12693 continue;
12694 }
12695 else
12696 dest = (sym_value
12697 + sym_sec->output_offset
12698 + sym_sec->output_section->vma);
12699
12700 /* Ignore branch to self. */
12701 if (sym_sec == isec)
12702 continue;
12703
12704 /* If the called function uses the toc, we need a stub. */
12705 if (sym_sec->has_toc_reloc
12706 || sym_sec->makes_toc_func_call)
12707 {
12708 ret = 1;
12709 break;
12710 }
12711
12712 /* Assume any branch that needs a long branch stub might in fact
12713 need a plt_branch stub. A plt_branch stub uses r2. */
12714 else if (dest - (isec->output_offset
12715 + isec->output_section->vma
12716 + rel->r_offset) + (1 << 25)
12717 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12718 ? h->other
12719 : sym->st_other))
12720 {
12721 ret = 1;
12722 break;
12723 }
12724
12725 /* If calling back to a section in the process of being
12726 tested, we can't say for sure that no toc adjusting stubs
12727 are needed, so don't return zero. */
12728 else if (sym_sec->call_check_in_progress)
12729 ret = 2;
12730
12731 /* Branches to another section that itself doesn't have any TOC
12732 references are OK. Recursively call ourselves to check. */
12733 else if (!sym_sec->call_check_done)
12734 {
12735 int recur;
12736
12737 /* Mark current section as indeterminate, so that other
12738 sections that call back to current won't be marked as
12739 known. */
12740 isec->call_check_in_progress = 1;
12741 recur = toc_adjusting_stub_needed (info, sym_sec);
12742 isec->call_check_in_progress = 0;
12743
12744 if (recur != 0)
12745 {
12746 ret = recur;
12747 if (recur != 2)
12748 break;
12749 }
12750 }
12751 }
12752
12753 if (local_syms != NULL
12754 && (elf_symtab_hdr (isec->owner).contents
12755 != (unsigned char *) local_syms))
12756 free (local_syms);
12757 if (elf_section_data (isec)->relocs != relstart)
12758 free (relstart);
12759 }
12760
12761 if ((ret & 1) == 0
12762 && isec->map_head.s != NULL
12763 && (strcmp (isec->output_section->name, ".init") == 0
12764 || strcmp (isec->output_section->name, ".fini") == 0))
12765 {
12766 if (isec->map_head.s->has_toc_reloc
12767 || isec->map_head.s->makes_toc_func_call)
12768 ret = 1;
12769 else if (!isec->map_head.s->call_check_done)
12770 {
12771 int recur;
12772 isec->call_check_in_progress = 1;
12773 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12774 isec->call_check_in_progress = 0;
12775 if (recur != 0)
12776 ret = recur;
12777 }
12778 }
12779
12780 if (ret == 1)
12781 isec->makes_toc_func_call = 1;
12782
12783 return ret;
12784 }
12785
12786 /* The linker repeatedly calls this function for each input section,
12787 in the order that input sections are linked into output sections.
12788 Build lists of input sections to determine groupings between which
12789 we may insert linker stubs. */
12790
12791 bfd_boolean
12792 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12793 {
12794 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12795
12796 if (htab == NULL)
12797 return FALSE;
12798
12799 if ((isec->output_section->flags & SEC_CODE) != 0
12800 && isec->output_section->id < htab->sec_info_arr_size)
12801 {
12802 /* This happens to make the list in reverse order,
12803 which is what we want. */
12804 htab->sec_info[isec->id].u.list
12805 = htab->sec_info[isec->output_section->id].u.list;
12806 htab->sec_info[isec->output_section->id].u.list = isec;
12807 }
12808
12809 if (htab->multi_toc_needed)
12810 {
12811 /* Analyse sections that aren't already flagged as needing a
12812 valid toc pointer. Exclude .fixup for the linux kernel.
12813 .fixup contains branches, but only back to the function that
12814 hit an exception. */
12815 if (!(isec->has_toc_reloc
12816 || (isec->flags & SEC_CODE) == 0
12817 || strcmp (isec->name, ".fixup") == 0
12818 || isec->call_check_done))
12819 {
12820 if (toc_adjusting_stub_needed (info, isec) < 0)
12821 return FALSE;
12822 }
12823 /* Make all sections use the TOC assigned for this object file.
12824 This will be wrong for pasted sections; We fix that in
12825 check_pasted_section(). */
12826 if (elf_gp (isec->owner) != 0)
12827 htab->toc_curr = elf_gp (isec->owner);
12828 }
12829
12830 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12831 return TRUE;
12832 }
12833
12834 /* Check that all .init and .fini sections use the same toc, if they
12835 have toc relocs. */
12836
12837 static bfd_boolean
12838 check_pasted_section (struct bfd_link_info *info, const char *name)
12839 {
12840 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12841
12842 if (o != NULL)
12843 {
12844 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12845 bfd_vma toc_off = 0;
12846 asection *i;
12847
12848 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12849 if (i->has_toc_reloc)
12850 {
12851 if (toc_off == 0)
12852 toc_off = htab->sec_info[i->id].toc_off;
12853 else if (toc_off != htab->sec_info[i->id].toc_off)
12854 return FALSE;
12855 }
12856
12857 if (toc_off == 0)
12858 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12859 if (i->makes_toc_func_call)
12860 {
12861 toc_off = htab->sec_info[i->id].toc_off;
12862 break;
12863 }
12864
12865 /* Make sure the whole pasted function uses the same toc offset. */
12866 if (toc_off != 0)
12867 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12868 htab->sec_info[i->id].toc_off = toc_off;
12869 }
12870 return TRUE;
12871 }
12872
12873 bfd_boolean
12874 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12875 {
12876 return (check_pasted_section (info, ".init")
12877 & check_pasted_section (info, ".fini"));
12878 }
12879
12880 /* See whether we can group stub sections together. Grouping stub
12881 sections may result in fewer stubs. More importantly, we need to
12882 put all .init* and .fini* stubs at the beginning of the .init or
12883 .fini output sections respectively, because glibc splits the
12884 _init and _fini functions into multiple parts. Putting a stub in
12885 the middle of a function is not a good idea. */
12886
12887 static bfd_boolean
12888 group_sections (struct bfd_link_info *info,
12889 bfd_size_type stub_group_size,
12890 bfd_boolean stubs_always_before_branch)
12891 {
12892 struct ppc_link_hash_table *htab;
12893 asection *osec;
12894 bfd_boolean suppress_size_errors;
12895
12896 htab = ppc_hash_table (info);
12897 if (htab == NULL)
12898 return FALSE;
12899
12900 suppress_size_errors = FALSE;
12901 if (stub_group_size == 1)
12902 {
12903 /* Default values. */
12904 if (stubs_always_before_branch)
12905 stub_group_size = 0x1e00000;
12906 else
12907 stub_group_size = 0x1c00000;
12908 suppress_size_errors = TRUE;
12909 }
12910
12911 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12912 {
12913 asection *tail;
12914
12915 if (osec->id >= htab->sec_info_arr_size)
12916 continue;
12917
12918 tail = htab->sec_info[osec->id].u.list;
12919 while (tail != NULL)
12920 {
12921 asection *curr;
12922 asection *prev;
12923 bfd_size_type total;
12924 bfd_boolean big_sec;
12925 bfd_vma curr_toc;
12926 struct map_stub *group;
12927 bfd_size_type group_size;
12928
12929 curr = tail;
12930 total = tail->size;
12931 group_size = (ppc64_elf_section_data (tail) != NULL
12932 && ppc64_elf_section_data (tail)->has_14bit_branch
12933 ? stub_group_size >> 10 : stub_group_size);
12934
12935 big_sec = total > group_size;
12936 if (big_sec && !suppress_size_errors)
12937 /* xgettext:c-format */
12938 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12939 tail->owner, tail);
12940 curr_toc = htab->sec_info[tail->id].toc_off;
12941
12942 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12943 && ((total += curr->output_offset - prev->output_offset)
12944 < (ppc64_elf_section_data (prev) != NULL
12945 && ppc64_elf_section_data (prev)->has_14bit_branch
12946 ? (group_size = stub_group_size >> 10) : group_size))
12947 && htab->sec_info[prev->id].toc_off == curr_toc)
12948 curr = prev;
12949
12950 /* OK, the size from the start of CURR to the end is less
12951 than group_size and thus can be handled by one stub
12952 section. (or the tail section is itself larger than
12953 group_size, in which case we may be toast.) We should
12954 really be keeping track of the total size of stubs added
12955 here, as stubs contribute to the final output section
12956 size. That's a little tricky, and this way will only
12957 break if stubs added make the total size more than 2^25,
12958 ie. for the default stub_group_size, if stubs total more
12959 than 2097152 bytes, or nearly 75000 plt call stubs. */
12960 group = bfd_alloc (curr->owner, sizeof (*group));
12961 if (group == NULL)
12962 return FALSE;
12963 group->link_sec = curr;
12964 group->stub_sec = NULL;
12965 group->needs_save_res = 0;
12966 group->lr_restore = 0;
12967 group->eh_size = 0;
12968 group->eh_base = 0;
12969 group->next = htab->group;
12970 htab->group = group;
12971 do
12972 {
12973 prev = htab->sec_info[tail->id].u.list;
12974 /* Set up this stub group. */
12975 htab->sec_info[tail->id].u.group = group;
12976 }
12977 while (tail != curr && (tail = prev) != NULL);
12978
12979 /* But wait, there's more! Input sections up to group_size
12980 bytes before the stub section can be handled by it too.
12981 Don't do this if we have a really large section after the
12982 stubs, as adding more stubs increases the chance that
12983 branches may not reach into the stub section. */
12984 if (!stubs_always_before_branch && !big_sec)
12985 {
12986 total = 0;
12987 while (prev != NULL
12988 && ((total += tail->output_offset - prev->output_offset)
12989 < (ppc64_elf_section_data (prev) != NULL
12990 && ppc64_elf_section_data (prev)->has_14bit_branch
12991 ? (group_size = stub_group_size >> 10) : group_size))
12992 && htab->sec_info[prev->id].toc_off == curr_toc)
12993 {
12994 tail = prev;
12995 prev = htab->sec_info[tail->id].u.list;
12996 htab->sec_info[tail->id].u.group = group;
12997 }
12998 }
12999 tail = prev;
13000 }
13001 }
13002 return TRUE;
13003 }
13004
13005 static const unsigned char glink_eh_frame_cie[] =
13006 {
13007 0, 0, 0, 16, /* length. */
13008 0, 0, 0, 0, /* id. */
13009 1, /* CIE version. */
13010 'z', 'R', 0, /* Augmentation string. */
13011 4, /* Code alignment. */
13012 0x78, /* Data alignment. */
13013 65, /* RA reg. */
13014 1, /* Augmentation size. */
13015 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13016 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13017 };
13018
13019 /* Stripping output sections is normally done before dynamic section
13020 symbols have been allocated. This function is called later, and
13021 handles cases like htab->brlt which is mapped to its own output
13022 section. */
13023
13024 static void
13025 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13026 {
13027 if (isec->size == 0
13028 && isec->output_section->size == 0
13029 && !(isec->output_section->flags & SEC_KEEP)
13030 && !bfd_section_removed_from_list (info->output_bfd,
13031 isec->output_section)
13032 && elf_section_data (isec->output_section)->dynindx == 0)
13033 {
13034 isec->output_section->flags |= SEC_EXCLUDE;
13035 bfd_section_list_remove (info->output_bfd, isec->output_section);
13036 info->output_bfd->section_count--;
13037 }
13038 }
13039
13040 /* Determine and set the size of the stub section for a final link.
13041
13042 The basic idea here is to examine all the relocations looking for
13043 PC-relative calls to a target that is unreachable with a "bl"
13044 instruction. */
13045
13046 bfd_boolean
13047 ppc64_elf_size_stubs (struct bfd_link_info *info)
13048 {
13049 bfd_size_type stub_group_size;
13050 bfd_boolean stubs_always_before_branch;
13051 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13052
13053 if (htab == NULL)
13054 return FALSE;
13055
13056 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13057 htab->params->plt_thread_safe = 1;
13058 if (!htab->opd_abi)
13059 htab->params->plt_thread_safe = 0;
13060 else if (htab->params->plt_thread_safe == -1)
13061 {
13062 static const char *const thread_starter[] =
13063 {
13064 "pthread_create",
13065 /* libstdc++ */
13066 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13067 /* librt */
13068 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13069 "mq_notify", "create_timer",
13070 /* libanl */
13071 "getaddrinfo_a",
13072 /* libgomp */
13073 "GOMP_parallel",
13074 "GOMP_parallel_start",
13075 "GOMP_parallel_loop_static",
13076 "GOMP_parallel_loop_static_start",
13077 "GOMP_parallel_loop_dynamic",
13078 "GOMP_parallel_loop_dynamic_start",
13079 "GOMP_parallel_loop_guided",
13080 "GOMP_parallel_loop_guided_start",
13081 "GOMP_parallel_loop_runtime",
13082 "GOMP_parallel_loop_runtime_start",
13083 "GOMP_parallel_sections",
13084 "GOMP_parallel_sections_start",
13085 /* libgo */
13086 "__go_go",
13087 };
13088 unsigned i;
13089
13090 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13091 {
13092 struct elf_link_hash_entry *h;
13093 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13094 FALSE, FALSE, TRUE);
13095 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13096 if (htab->params->plt_thread_safe)
13097 break;
13098 }
13099 }
13100 stubs_always_before_branch = htab->params->group_size < 0;
13101 if (htab->params->group_size < 0)
13102 stub_group_size = -htab->params->group_size;
13103 else
13104 stub_group_size = htab->params->group_size;
13105
13106 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13107 return FALSE;
13108
13109 #define STUB_SHRINK_ITER 20
13110 /* Loop until no stubs added. After iteration 20 of this loop we may
13111 exit on a stub section shrinking. This is to break out of a
13112 pathological case where adding stubs on one iteration decreases
13113 section gaps (perhaps due to alignment), which then requires
13114 fewer or smaller stubs on the next iteration. */
13115
13116 while (1)
13117 {
13118 bfd *input_bfd;
13119 unsigned int bfd_indx;
13120 struct map_stub *group;
13121
13122 htab->stub_iteration += 1;
13123
13124 for (input_bfd = info->input_bfds, bfd_indx = 0;
13125 input_bfd != NULL;
13126 input_bfd = input_bfd->link.next, bfd_indx++)
13127 {
13128 Elf_Internal_Shdr *symtab_hdr;
13129 asection *section;
13130 Elf_Internal_Sym *local_syms = NULL;
13131
13132 if (!is_ppc64_elf (input_bfd))
13133 continue;
13134
13135 /* We'll need the symbol table in a second. */
13136 symtab_hdr = &elf_symtab_hdr (input_bfd);
13137 if (symtab_hdr->sh_info == 0)
13138 continue;
13139
13140 /* Walk over each section attached to the input bfd. */
13141 for (section = input_bfd->sections;
13142 section != NULL;
13143 section = section->next)
13144 {
13145 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13146
13147 /* If there aren't any relocs, then there's nothing more
13148 to do. */
13149 if ((section->flags & SEC_RELOC) == 0
13150 || (section->flags & SEC_ALLOC) == 0
13151 || (section->flags & SEC_LOAD) == 0
13152 || (section->flags & SEC_CODE) == 0
13153 || section->reloc_count == 0)
13154 continue;
13155
13156 /* If this section is a link-once section that will be
13157 discarded, then don't create any stubs. */
13158 if (section->output_section == NULL
13159 || section->output_section->owner != info->output_bfd)
13160 continue;
13161
13162 /* Get the relocs. */
13163 internal_relocs
13164 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13165 info->keep_memory);
13166 if (internal_relocs == NULL)
13167 goto error_ret_free_local;
13168
13169 /* Now examine each relocation. */
13170 irela = internal_relocs;
13171 irelaend = irela + section->reloc_count;
13172 for (; irela < irelaend; irela++)
13173 {
13174 enum elf_ppc64_reloc_type r_type;
13175 unsigned int r_indx;
13176 enum ppc_stub_type stub_type;
13177 struct ppc_stub_hash_entry *stub_entry;
13178 asection *sym_sec, *code_sec;
13179 bfd_vma sym_value, code_value;
13180 bfd_vma destination;
13181 unsigned long local_off;
13182 bfd_boolean ok_dest;
13183 struct ppc_link_hash_entry *hash;
13184 struct ppc_link_hash_entry *fdh;
13185 struct elf_link_hash_entry *h;
13186 Elf_Internal_Sym *sym;
13187 char *stub_name;
13188 const asection *id_sec;
13189 struct _opd_sec_data *opd;
13190 struct plt_entry *plt_ent;
13191
13192 r_type = ELF64_R_TYPE (irela->r_info);
13193 r_indx = ELF64_R_SYM (irela->r_info);
13194
13195 if (r_type >= R_PPC64_max)
13196 {
13197 bfd_set_error (bfd_error_bad_value);
13198 goto error_ret_free_internal;
13199 }
13200
13201 /* Only look for stubs on branch instructions. */
13202 if (r_type != R_PPC64_REL24
13203 && r_type != R_PPC64_REL24_NOTOC
13204 && r_type != R_PPC64_REL14
13205 && r_type != R_PPC64_REL14_BRTAKEN
13206 && r_type != R_PPC64_REL14_BRNTAKEN)
13207 continue;
13208
13209 /* Now determine the call target, its name, value,
13210 section. */
13211 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13212 r_indx, input_bfd))
13213 goto error_ret_free_internal;
13214 hash = (struct ppc_link_hash_entry *) h;
13215
13216 ok_dest = FALSE;
13217 fdh = NULL;
13218 sym_value = 0;
13219 if (hash == NULL)
13220 {
13221 sym_value = sym->st_value;
13222 if (sym_sec != NULL
13223 && sym_sec->output_section != NULL)
13224 ok_dest = TRUE;
13225 }
13226 else if (hash->elf.root.type == bfd_link_hash_defined
13227 || hash->elf.root.type == bfd_link_hash_defweak)
13228 {
13229 sym_value = hash->elf.root.u.def.value;
13230 if (sym_sec->output_section != NULL)
13231 ok_dest = TRUE;
13232 }
13233 else if (hash->elf.root.type == bfd_link_hash_undefweak
13234 || hash->elf.root.type == bfd_link_hash_undefined)
13235 {
13236 /* Recognise an old ABI func code entry sym, and
13237 use the func descriptor sym instead if it is
13238 defined. */
13239 if (hash->elf.root.root.string[0] == '.'
13240 && hash->oh != NULL)
13241 {
13242 fdh = ppc_follow_link (hash->oh);
13243 if (fdh->elf.root.type == bfd_link_hash_defined
13244 || fdh->elf.root.type == bfd_link_hash_defweak)
13245 {
13246 sym_sec = fdh->elf.root.u.def.section;
13247 sym_value = fdh->elf.root.u.def.value;
13248 if (sym_sec->output_section != NULL)
13249 ok_dest = TRUE;
13250 }
13251 else
13252 fdh = NULL;
13253 }
13254 }
13255 else
13256 {
13257 bfd_set_error (bfd_error_bad_value);
13258 goto error_ret_free_internal;
13259 }
13260
13261 destination = 0;
13262 local_off = 0;
13263 if (ok_dest)
13264 {
13265 sym_value += irela->r_addend;
13266 destination = (sym_value
13267 + sym_sec->output_offset
13268 + sym_sec->output_section->vma);
13269 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13270 ? hash->elf.other
13271 : sym->st_other);
13272 }
13273
13274 code_sec = sym_sec;
13275 code_value = sym_value;
13276 opd = get_opd_info (sym_sec);
13277 if (opd != NULL)
13278 {
13279 bfd_vma dest;
13280
13281 if (hash == NULL && opd->adjust != NULL)
13282 {
13283 long adjust = opd->adjust[OPD_NDX (sym_value)];
13284 if (adjust == -1)
13285 continue;
13286 code_value += adjust;
13287 sym_value += adjust;
13288 }
13289 dest = opd_entry_value (sym_sec, sym_value,
13290 &code_sec, &code_value, FALSE);
13291 if (dest != (bfd_vma) -1)
13292 {
13293 destination = dest;
13294 if (fdh != NULL)
13295 {
13296 /* Fixup old ABI sym to point at code
13297 entry. */
13298 hash->elf.root.type = bfd_link_hash_defweak;
13299 hash->elf.root.u.def.section = code_sec;
13300 hash->elf.root.u.def.value = code_value;
13301 }
13302 }
13303 }
13304
13305 /* Determine what (if any) linker stub is needed. */
13306 plt_ent = NULL;
13307 stub_type = ppc_type_of_stub (section, irela, &hash,
13308 &plt_ent, destination,
13309 local_off);
13310
13311 if (r_type == R_PPC64_REL24_NOTOC)
13312 {
13313 if (stub_type == ppc_stub_plt_call)
13314 stub_type = ppc_stub_plt_call_notoc;
13315 else if (stub_type == ppc_stub_long_branch
13316 || (code_sec != NULL
13317 && code_sec->output_section != NULL
13318 && (((hash ? hash->elf.other : sym->st_other)
13319 & STO_PPC64_LOCAL_MASK)
13320 != 1 << STO_PPC64_LOCAL_BIT)))
13321 stub_type = ppc_stub_long_branch_notoc;
13322 }
13323 else if (stub_type != ppc_stub_plt_call)
13324 {
13325 /* Check whether we need a TOC adjusting stub.
13326 Since the linker pastes together pieces from
13327 different object files when creating the
13328 _init and _fini functions, it may be that a
13329 call to what looks like a local sym is in
13330 fact a call needing a TOC adjustment. */
13331 if ((code_sec != NULL
13332 && code_sec->output_section != NULL
13333 && (htab->sec_info[code_sec->id].toc_off
13334 != htab->sec_info[section->id].toc_off)
13335 && (code_sec->has_toc_reloc
13336 || code_sec->makes_toc_func_call))
13337 || (((hash ? hash->elf.other : sym->st_other)
13338 & STO_PPC64_LOCAL_MASK)
13339 == 1 << STO_PPC64_LOCAL_BIT))
13340 stub_type = ppc_stub_long_branch_r2off;
13341 }
13342
13343 if (stub_type == ppc_stub_none)
13344 continue;
13345
13346 /* __tls_get_addr calls might be eliminated. */
13347 if (stub_type != ppc_stub_plt_call
13348 && stub_type != ppc_stub_plt_call_notoc
13349 && hash != NULL
13350 && (hash == htab->tls_get_addr
13351 || hash == htab->tls_get_addr_fd)
13352 && section->has_tls_reloc
13353 && irela != internal_relocs)
13354 {
13355 /* Get tls info. */
13356 unsigned char *tls_mask;
13357
13358 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13359 irela - 1, input_bfd))
13360 goto error_ret_free_internal;
13361 if ((*tls_mask & TLS_TLS) != 0)
13362 continue;
13363 }
13364
13365 if (stub_type == ppc_stub_plt_call)
13366 {
13367 if (!htab->opd_abi
13368 && htab->params->plt_localentry0 != 0
13369 && is_elfv2_localentry0 (&hash->elf))
13370 htab->has_plt_localentry0 = 1;
13371 else if (irela + 1 < irelaend
13372 && irela[1].r_offset == irela->r_offset + 4
13373 && (ELF64_R_TYPE (irela[1].r_info)
13374 == R_PPC64_TOCSAVE))
13375 {
13376 if (!tocsave_find (htab, INSERT,
13377 &local_syms, irela + 1, input_bfd))
13378 goto error_ret_free_internal;
13379 }
13380 else
13381 stub_type = ppc_stub_plt_call_r2save;
13382 }
13383
13384 /* Support for grouping stub sections. */
13385 id_sec = htab->sec_info[section->id].u.group->link_sec;
13386
13387 /* Get the name of this stub. */
13388 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13389 if (!stub_name)
13390 goto error_ret_free_internal;
13391
13392 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13393 stub_name, FALSE, FALSE);
13394 if (stub_entry != NULL)
13395 {
13396 enum ppc_stub_type old_type;
13397 /* A stub has already been created, but it may
13398 not be the required type. We shouldn't be
13399 transitioning from plt_call to long_branch
13400 stubs or vice versa, but we might be
13401 upgrading from plt_call to plt_call_r2save or
13402 from long_branch to long_branch_r2off. */
13403 free (stub_name);
13404 old_type = stub_entry->stub_type;
13405 switch (old_type)
13406 {
13407 default:
13408 abort ();
13409
13410 case ppc_stub_save_res:
13411 continue;
13412
13413 case ppc_stub_plt_call:
13414 case ppc_stub_plt_call_r2save:
13415 case ppc_stub_plt_call_notoc:
13416 case ppc_stub_plt_call_both:
13417 if (stub_type == ppc_stub_plt_call)
13418 continue;
13419 else if (stub_type == ppc_stub_plt_call_r2save)
13420 {
13421 if (old_type == ppc_stub_plt_call_notoc)
13422 stub_type = ppc_stub_plt_call_both;
13423 }
13424 else if (stub_type == ppc_stub_plt_call_notoc)
13425 {
13426 if (old_type == ppc_stub_plt_call_r2save)
13427 stub_type = ppc_stub_plt_call_both;
13428 }
13429 else
13430 abort ();
13431 break;
13432
13433 case ppc_stub_plt_branch:
13434 case ppc_stub_plt_branch_r2off:
13435 case ppc_stub_plt_branch_notoc:
13436 case ppc_stub_plt_branch_both:
13437 old_type += (ppc_stub_long_branch
13438 - ppc_stub_plt_branch);
13439 /* Fall through. */
13440 case ppc_stub_long_branch:
13441 case ppc_stub_long_branch_r2off:
13442 case ppc_stub_long_branch_notoc:
13443 case ppc_stub_long_branch_both:
13444 if (stub_type == ppc_stub_long_branch)
13445 continue;
13446 else if (stub_type == ppc_stub_long_branch_r2off)
13447 {
13448 if (old_type == ppc_stub_long_branch_notoc)
13449 stub_type = ppc_stub_long_branch_both;
13450 }
13451 else if (stub_type == ppc_stub_long_branch_notoc)
13452 {
13453 if (old_type == ppc_stub_long_branch_r2off)
13454 stub_type = ppc_stub_long_branch_both;
13455 }
13456 else
13457 abort ();
13458 break;
13459 }
13460 if (old_type < stub_type)
13461 stub_entry->stub_type = stub_type;
13462 continue;
13463 }
13464
13465 stub_entry = ppc_add_stub (stub_name, section, info);
13466 if (stub_entry == NULL)
13467 {
13468 free (stub_name);
13469 error_ret_free_internal:
13470 if (elf_section_data (section)->relocs == NULL)
13471 free (internal_relocs);
13472 error_ret_free_local:
13473 if (local_syms != NULL
13474 && (symtab_hdr->contents
13475 != (unsigned char *) local_syms))
13476 free (local_syms);
13477 return FALSE;
13478 }
13479
13480 stub_entry->stub_type = stub_type;
13481 if (stub_type >= ppc_stub_plt_call
13482 && stub_type <= ppc_stub_plt_call_both)
13483 {
13484 stub_entry->target_value = sym_value;
13485 stub_entry->target_section = sym_sec;
13486 }
13487 else
13488 {
13489 stub_entry->target_value = code_value;
13490 stub_entry->target_section = code_sec;
13491 }
13492 stub_entry->h = hash;
13493 stub_entry->plt_ent = plt_ent;
13494 stub_entry->symtype
13495 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13496 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13497
13498 if (stub_entry->h != NULL)
13499 htab->stub_globals += 1;
13500 }
13501
13502 /* We're done with the internal relocs, free them. */
13503 if (elf_section_data (section)->relocs != internal_relocs)
13504 free (internal_relocs);
13505 }
13506
13507 if (local_syms != NULL
13508 && symtab_hdr->contents != (unsigned char *) local_syms)
13509 {
13510 if (!info->keep_memory)
13511 free (local_syms);
13512 else
13513 symtab_hdr->contents = (unsigned char *) local_syms;
13514 }
13515 }
13516
13517 /* We may have added some stubs. Find out the new size of the
13518 stub sections. */
13519 for (group = htab->group; group != NULL; group = group->next)
13520 {
13521 group->lr_restore = 0;
13522 group->eh_size = 0;
13523 if (group->stub_sec != NULL)
13524 {
13525 asection *stub_sec = group->stub_sec;
13526
13527 if (htab->stub_iteration <= STUB_SHRINK_ITER
13528 || stub_sec->rawsize < stub_sec->size)
13529 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13530 stub_sec->rawsize = stub_sec->size;
13531 stub_sec->size = 0;
13532 stub_sec->reloc_count = 0;
13533 stub_sec->flags &= ~SEC_RELOC;
13534 }
13535 }
13536
13537 if (htab->stub_iteration <= STUB_SHRINK_ITER
13538 || htab->brlt->rawsize < htab->brlt->size)
13539 htab->brlt->rawsize = htab->brlt->size;
13540 htab->brlt->size = 0;
13541 htab->brlt->reloc_count = 0;
13542 htab->brlt->flags &= ~SEC_RELOC;
13543 if (htab->relbrlt != NULL)
13544 htab->relbrlt->size = 0;
13545
13546 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13547
13548 for (group = htab->group; group != NULL; group = group->next)
13549 if (group->needs_save_res)
13550 group->stub_sec->size += htab->sfpr->size;
13551
13552 if (info->emitrelocations
13553 && htab->glink != NULL && htab->glink->size != 0)
13554 {
13555 htab->glink->reloc_count = 1;
13556 htab->glink->flags |= SEC_RELOC;
13557 }
13558
13559 if (htab->glink_eh_frame != NULL
13560 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13561 && htab->glink_eh_frame->output_section->size > 8)
13562 {
13563 size_t size = 0, align = 4;
13564
13565 for (group = htab->group; group != NULL; group = group->next)
13566 if (group->eh_size != 0)
13567 size += (group->eh_size + 17 + align - 1) & -align;
13568 if (htab->glink != NULL && htab->glink->size != 0)
13569 size += (24 + align - 1) & -align;
13570 if (size != 0)
13571 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13572 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13573 size = (size + align - 1) & -align;
13574 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13575 htab->glink_eh_frame->size = size;
13576 }
13577
13578 if (htab->params->plt_stub_align != 0)
13579 for (group = htab->group; group != NULL; group = group->next)
13580 if (group->stub_sec != NULL)
13581 {
13582 int align = abs (htab->params->plt_stub_align);
13583 group->stub_sec->size
13584 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13585 }
13586
13587 for (group = htab->group; group != NULL; group = group->next)
13588 if (group->stub_sec != NULL
13589 && group->stub_sec->rawsize != group->stub_sec->size
13590 && (htab->stub_iteration <= STUB_SHRINK_ITER
13591 || group->stub_sec->rawsize < group->stub_sec->size))
13592 break;
13593
13594 if (group == NULL
13595 && (htab->brlt->rawsize == htab->brlt->size
13596 || (htab->stub_iteration > STUB_SHRINK_ITER
13597 && htab->brlt->rawsize > htab->brlt->size))
13598 && (htab->glink_eh_frame == NULL
13599 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13600 break;
13601
13602 /* Ask the linker to do its stuff. */
13603 (*htab->params->layout_sections_again) ();
13604 }
13605
13606 if (htab->glink_eh_frame != NULL
13607 && htab->glink_eh_frame->size != 0)
13608 {
13609 bfd_vma val;
13610 bfd_byte *p, *last_fde;
13611 size_t last_fde_len, size, align, pad;
13612 struct map_stub *group;
13613
13614 /* It is necessary to at least have a rough outline of the
13615 linker generated CIEs and FDEs written before
13616 bfd_elf_discard_info is run, in order for these FDEs to be
13617 indexed in .eh_frame_hdr. */
13618 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13619 if (p == NULL)
13620 return FALSE;
13621 htab->glink_eh_frame->contents = p;
13622 last_fde = p;
13623 align = 4;
13624
13625 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13626 /* CIE length (rewrite in case little-endian). */
13627 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13628 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13629 p += last_fde_len + 4;
13630
13631 for (group = htab->group; group != NULL; group = group->next)
13632 if (group->eh_size != 0)
13633 {
13634 group->eh_base = p - htab->glink_eh_frame->contents;
13635 last_fde = p;
13636 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13637 /* FDE length. */
13638 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13639 p += 4;
13640 /* CIE pointer. */
13641 val = p - htab->glink_eh_frame->contents;
13642 bfd_put_32 (htab->elf.dynobj, val, p);
13643 p += 4;
13644 /* Offset to stub section, written later. */
13645 p += 4;
13646 /* stub section size. */
13647 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13648 p += 4;
13649 /* Augmentation. */
13650 p += 1;
13651 /* Make sure we don't have all nops. This is enough for
13652 elf-eh-frame.c to detect the last non-nop opcode. */
13653 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13654 p = last_fde + last_fde_len + 4;
13655 }
13656 if (htab->glink != NULL && htab->glink->size != 0)
13657 {
13658 last_fde = p;
13659 last_fde_len = ((24 + align - 1) & -align) - 4;
13660 /* FDE length. */
13661 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13662 p += 4;
13663 /* CIE pointer. */
13664 val = p - htab->glink_eh_frame->contents;
13665 bfd_put_32 (htab->elf.dynobj, val, p);
13666 p += 4;
13667 /* Offset to .glink, written later. */
13668 p += 4;
13669 /* .glink size. */
13670 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13671 p += 4;
13672 /* Augmentation. */
13673 p += 1;
13674
13675 *p++ = DW_CFA_advance_loc + 1;
13676 *p++ = DW_CFA_register;
13677 *p++ = 65;
13678 *p++ = htab->opd_abi ? 12 : 0;
13679 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13680 *p++ = DW_CFA_restore_extended;
13681 *p++ = 65;
13682 p += ((24 + align - 1) & -align) - 24;
13683 }
13684 /* Subsume any padding into the last FDE if user .eh_frame
13685 sections are aligned more than glink_eh_frame. Otherwise any
13686 zero padding will be seen as a terminator. */
13687 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13688 size = p - htab->glink_eh_frame->contents;
13689 pad = ((size + align - 1) & -align) - size;
13690 htab->glink_eh_frame->size = size + pad;
13691 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13692 }
13693
13694 maybe_strip_output (info, htab->brlt);
13695 if (htab->glink_eh_frame != NULL)
13696 maybe_strip_output (info, htab->glink_eh_frame);
13697
13698 return TRUE;
13699 }
13700
13701 /* Called after we have determined section placement. If sections
13702 move, we'll be called again. Provide a value for TOCstart. */
13703
13704 bfd_vma
13705 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13706 {
13707 asection *s;
13708 bfd_vma TOCstart, adjust;
13709
13710 if (info != NULL)
13711 {
13712 struct elf_link_hash_entry *h;
13713 struct elf_link_hash_table *htab = elf_hash_table (info);
13714
13715 if (is_elf_hash_table (htab)
13716 && htab->hgot != NULL)
13717 h = htab->hgot;
13718 else
13719 {
13720 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13721 if (is_elf_hash_table (htab))
13722 htab->hgot = h;
13723 }
13724 if (h != NULL
13725 && h->root.type == bfd_link_hash_defined
13726 && !h->root.linker_def
13727 && (!is_elf_hash_table (htab)
13728 || h->def_regular))
13729 {
13730 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13731 + h->root.u.def.section->output_offset
13732 + h->root.u.def.section->output_section->vma);
13733 _bfd_set_gp_value (obfd, TOCstart);
13734 return TOCstart;
13735 }
13736 }
13737
13738 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13739 order. The TOC starts where the first of these sections starts. */
13740 s = bfd_get_section_by_name (obfd, ".got");
13741 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13742 s = bfd_get_section_by_name (obfd, ".toc");
13743 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13744 s = bfd_get_section_by_name (obfd, ".tocbss");
13745 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13746 s = bfd_get_section_by_name (obfd, ".plt");
13747 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13748 {
13749 /* This may happen for
13750 o references to TOC base (SYM@toc / TOC[tc0]) without a
13751 .toc directive
13752 o bad linker script
13753 o --gc-sections and empty TOC sections
13754
13755 FIXME: Warn user? */
13756
13757 /* Look for a likely section. We probably won't even be
13758 using TOCstart. */
13759 for (s = obfd->sections; s != NULL; s = s->next)
13760 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13761 | SEC_EXCLUDE))
13762 == (SEC_ALLOC | SEC_SMALL_DATA))
13763 break;
13764 if (s == NULL)
13765 for (s = obfd->sections; s != NULL; s = s->next)
13766 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13767 == (SEC_ALLOC | SEC_SMALL_DATA))
13768 break;
13769 if (s == NULL)
13770 for (s = obfd->sections; s != NULL; s = s->next)
13771 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13772 == SEC_ALLOC)
13773 break;
13774 if (s == NULL)
13775 for (s = obfd->sections; s != NULL; s = s->next)
13776 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13777 break;
13778 }
13779
13780 TOCstart = 0;
13781 if (s != NULL)
13782 TOCstart = s->output_section->vma + s->output_offset;
13783
13784 /* Force alignment. */
13785 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13786 TOCstart -= adjust;
13787 _bfd_set_gp_value (obfd, TOCstart);
13788
13789 if (info != NULL && s != NULL)
13790 {
13791 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13792
13793 if (htab != NULL)
13794 {
13795 if (htab->elf.hgot != NULL)
13796 {
13797 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13798 htab->elf.hgot->root.u.def.section = s;
13799 }
13800 }
13801 else
13802 {
13803 struct bfd_link_hash_entry *bh = NULL;
13804 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13805 s, TOC_BASE_OFF - adjust,
13806 NULL, FALSE, FALSE, &bh);
13807 }
13808 }
13809 return TOCstart;
13810 }
13811
13812 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13813 write out any global entry stubs, and PLT relocations. */
13814
13815 static bfd_boolean
13816 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13817 {
13818 struct bfd_link_info *info;
13819 struct ppc_link_hash_table *htab;
13820 struct plt_entry *ent;
13821 asection *s;
13822
13823 if (h->root.type == bfd_link_hash_indirect)
13824 return TRUE;
13825
13826 info = inf;
13827 htab = ppc_hash_table (info);
13828 if (htab == NULL)
13829 return FALSE;
13830
13831 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13832 if (ent->plt.offset != (bfd_vma) -1)
13833 {
13834 /* This symbol has an entry in the procedure linkage
13835 table. Set it up. */
13836 Elf_Internal_Rela rela;
13837 asection *plt, *relplt;
13838 bfd_byte *loc;
13839
13840 if (!htab->elf.dynamic_sections_created
13841 || h->dynindx == -1)
13842 {
13843 if (!(h->def_regular
13844 && (h->root.type == bfd_link_hash_defined
13845 || h->root.type == bfd_link_hash_defweak)))
13846 continue;
13847 if (h->type == STT_GNU_IFUNC)
13848 {
13849 plt = htab->elf.iplt;
13850 relplt = htab->elf.irelplt;
13851 htab->local_ifunc_resolver = 1;
13852 if (htab->opd_abi)
13853 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13854 else
13855 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13856 }
13857 else
13858 {
13859 plt = htab->pltlocal;
13860 if (bfd_link_pic (info))
13861 {
13862 relplt = htab->relpltlocal;
13863 if (htab->opd_abi)
13864 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13865 else
13866 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13867 }
13868 else
13869 relplt = NULL;
13870 }
13871 rela.r_addend = (h->root.u.def.value
13872 + h->root.u.def.section->output_offset
13873 + h->root.u.def.section->output_section->vma
13874 + ent->addend);
13875
13876 if (relplt == NULL)
13877 {
13878 loc = plt->contents + ent->plt.offset;
13879 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13880 if (htab->opd_abi)
13881 {
13882 bfd_vma toc = elf_gp (info->output_bfd);
13883 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13884 bfd_put_64 (info->output_bfd, toc, loc + 8);
13885 }
13886 }
13887 else
13888 {
13889 rela.r_offset = (plt->output_section->vma
13890 + plt->output_offset
13891 + ent->plt.offset);
13892 loc = relplt->contents + (relplt->reloc_count++
13893 * sizeof (Elf64_External_Rela));
13894 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13895 }
13896 }
13897 else
13898 {
13899 rela.r_offset = (htab->elf.splt->output_section->vma
13900 + htab->elf.splt->output_offset
13901 + ent->plt.offset);
13902 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13903 rela.r_addend = ent->addend;
13904 loc = (htab->elf.srelplt->contents
13905 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13906 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13907 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13908 htab->maybe_local_ifunc_resolver = 1;
13909 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13910 }
13911 }
13912
13913 if (!h->pointer_equality_needed)
13914 return TRUE;
13915
13916 if (h->def_regular)
13917 return TRUE;
13918
13919 s = htab->global_entry;
13920 if (s == NULL || s->size == 0)
13921 return TRUE;
13922
13923 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13924 if (ent->plt.offset != (bfd_vma) -1
13925 && ent->addend == 0)
13926 {
13927 bfd_byte *p;
13928 asection *plt;
13929 bfd_vma off;
13930
13931 p = s->contents + h->root.u.def.value;
13932 plt = htab->elf.splt;
13933 if (!htab->elf.dynamic_sections_created
13934 || h->dynindx == -1)
13935 {
13936 if (h->type == STT_GNU_IFUNC)
13937 plt = htab->elf.iplt;
13938 else
13939 plt = htab->pltlocal;
13940 }
13941 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13942 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13943
13944 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13945 {
13946 info->callbacks->einfo
13947 (_("%P: linkage table error against `%pT'\n"),
13948 h->root.root.string);
13949 bfd_set_error (bfd_error_bad_value);
13950 htab->stub_error = TRUE;
13951 }
13952
13953 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13954 if (htab->params->emit_stub_syms)
13955 {
13956 size_t len = strlen (h->root.root.string);
13957 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13958
13959 if (name == NULL)
13960 return FALSE;
13961
13962 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13963 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13964 if (h == NULL)
13965 return FALSE;
13966 if (h->root.type == bfd_link_hash_new)
13967 {
13968 h->root.type = bfd_link_hash_defined;
13969 h->root.u.def.section = s;
13970 h->root.u.def.value = p - s->contents;
13971 h->ref_regular = 1;
13972 h->def_regular = 1;
13973 h->ref_regular_nonweak = 1;
13974 h->forced_local = 1;
13975 h->non_elf = 0;
13976 h->root.linker_def = 1;
13977 }
13978 }
13979
13980 if (PPC_HA (off) != 0)
13981 {
13982 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13983 p += 4;
13984 }
13985 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13986 p += 4;
13987 bfd_put_32 (s->owner, MTCTR_R12, p);
13988 p += 4;
13989 bfd_put_32 (s->owner, BCTR, p);
13990 break;
13991 }
13992 return TRUE;
13993 }
13994
13995 /* Write PLT relocs for locals. */
13996
13997 static bfd_boolean
13998 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13999 {
14000 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14001 bfd *ibfd;
14002
14003 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14004 {
14005 struct got_entry **lgot_ents, **end_lgot_ents;
14006 struct plt_entry **local_plt, **lplt, **end_local_plt;
14007 Elf_Internal_Shdr *symtab_hdr;
14008 bfd_size_type locsymcount;
14009 Elf_Internal_Sym *local_syms = NULL;
14010 struct plt_entry *ent;
14011
14012 if (!is_ppc64_elf (ibfd))
14013 continue;
14014
14015 lgot_ents = elf_local_got_ents (ibfd);
14016 if (!lgot_ents)
14017 continue;
14018
14019 symtab_hdr = &elf_symtab_hdr (ibfd);
14020 locsymcount = symtab_hdr->sh_info;
14021 end_lgot_ents = lgot_ents + locsymcount;
14022 local_plt = (struct plt_entry **) end_lgot_ents;
14023 end_local_plt = local_plt + locsymcount;
14024 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14025 for (ent = *lplt; ent != NULL; ent = ent->next)
14026 if (ent->plt.offset != (bfd_vma) -1)
14027 {
14028 Elf_Internal_Sym *sym;
14029 asection *sym_sec;
14030 asection *plt, *relplt;
14031 bfd_byte *loc;
14032 bfd_vma val;
14033
14034 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14035 lplt - local_plt, ibfd))
14036 {
14037 if (local_syms != NULL
14038 && symtab_hdr->contents != (unsigned char *) local_syms)
14039 free (local_syms);
14040 return FALSE;
14041 }
14042
14043 val = sym->st_value + ent->addend;
14044 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
14045 if (sym_sec != NULL && sym_sec->output_section != NULL)
14046 val += sym_sec->output_offset + sym_sec->output_section->vma;
14047
14048 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14049 {
14050 htab->local_ifunc_resolver = 1;
14051 plt = htab->elf.iplt;
14052 relplt = htab->elf.irelplt;
14053 }
14054 else
14055 {
14056 plt = htab->pltlocal;
14057 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14058 }
14059
14060 if (relplt == NULL)
14061 {
14062 loc = plt->contents + ent->plt.offset;
14063 bfd_put_64 (info->output_bfd, val, loc);
14064 if (htab->opd_abi)
14065 {
14066 bfd_vma toc = elf_gp (ibfd);
14067 bfd_put_64 (info->output_bfd, toc, loc + 8);
14068 }
14069 }
14070 else
14071 {
14072 Elf_Internal_Rela rela;
14073 rela.r_offset = (ent->plt.offset
14074 + plt->output_offset
14075 + plt->output_section->vma);
14076 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14077 {
14078 if (htab->opd_abi)
14079 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14080 else
14081 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14082 }
14083 else
14084 {
14085 if (htab->opd_abi)
14086 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14087 else
14088 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14089 }
14090 rela.r_addend = val;
14091 loc = relplt->contents + (relplt->reloc_count++
14092 * sizeof (Elf64_External_Rela));
14093 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14094 }
14095 }
14096
14097 if (local_syms != NULL
14098 && symtab_hdr->contents != (unsigned char *) local_syms)
14099 {
14100 if (!info->keep_memory)
14101 free (local_syms);
14102 else
14103 symtab_hdr->contents = (unsigned char *) local_syms;
14104 }
14105 }
14106 return TRUE;
14107 }
14108
14109 /* Build all the stubs associated with the current output file.
14110 The stubs are kept in a hash table attached to the main linker
14111 hash table. This function is called via gldelf64ppc_finish. */
14112
14113 bfd_boolean
14114 ppc64_elf_build_stubs (struct bfd_link_info *info,
14115 char **stats)
14116 {
14117 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14118 struct map_stub *group;
14119 asection *stub_sec;
14120 bfd_byte *p;
14121 int stub_sec_count = 0;
14122
14123 if (htab == NULL)
14124 return FALSE;
14125
14126 /* Allocate memory to hold the linker stubs. */
14127 for (group = htab->group; group != NULL; group = group->next)
14128 {
14129 group->eh_size = 0;
14130 group->lr_restore = 0;
14131 if ((stub_sec = group->stub_sec) != NULL
14132 && stub_sec->size != 0)
14133 {
14134 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14135 stub_sec->size);
14136 if (stub_sec->contents == NULL)
14137 return FALSE;
14138 stub_sec->size = 0;
14139 }
14140 }
14141
14142 if (htab->glink != NULL && htab->glink->size != 0)
14143 {
14144 unsigned int indx;
14145 bfd_vma plt0;
14146
14147 /* Build the .glink plt call stub. */
14148 if (htab->params->emit_stub_syms)
14149 {
14150 struct elf_link_hash_entry *h;
14151 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14152 TRUE, FALSE, FALSE);
14153 if (h == NULL)
14154 return FALSE;
14155 if (h->root.type == bfd_link_hash_new)
14156 {
14157 h->root.type = bfd_link_hash_defined;
14158 h->root.u.def.section = htab->glink;
14159 h->root.u.def.value = 8;
14160 h->ref_regular = 1;
14161 h->def_regular = 1;
14162 h->ref_regular_nonweak = 1;
14163 h->forced_local = 1;
14164 h->non_elf = 0;
14165 h->root.linker_def = 1;
14166 }
14167 }
14168 plt0 = (htab->elf.splt->output_section->vma
14169 + htab->elf.splt->output_offset
14170 - 16);
14171 if (info->emitrelocations)
14172 {
14173 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14174 if (r == NULL)
14175 return FALSE;
14176 r->r_offset = (htab->glink->output_offset
14177 + htab->glink->output_section->vma);
14178 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14179 r->r_addend = plt0;
14180 }
14181 p = htab->glink->contents;
14182 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14183 bfd_put_64 (htab->glink->owner, plt0, p);
14184 p += 8;
14185 if (htab->opd_abi)
14186 {
14187 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14188 p += 4;
14189 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14190 p += 4;
14191 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14192 p += 4;
14193 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14194 p += 4;
14195 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14196 p += 4;
14197 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14198 p += 4;
14199 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14200 p += 4;
14201 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14202 p += 4;
14203 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14204 p += 4;
14205 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14206 p += 4;
14207 }
14208 else
14209 {
14210 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14211 p += 4;
14212 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14213 p += 4;
14214 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14215 p += 4;
14216 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14217 p += 4;
14218 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14219 p += 4;
14220 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14221 p += 4;
14222 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14223 p += 4;
14224 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14225 p += 4;
14226 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
14227 p += 4;
14228 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14229 p += 4;
14230 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14231 p += 4;
14232 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14233 p += 4;
14234 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14235 p += 4;
14236 }
14237 bfd_put_32 (htab->glink->owner, BCTR, p);
14238 p += 4;
14239 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14240
14241 /* Build the .glink lazy link call stubs. */
14242 indx = 0;
14243 while (p < htab->glink->contents + htab->glink->size)
14244 {
14245 if (htab->opd_abi)
14246 {
14247 if (indx < 0x8000)
14248 {
14249 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14250 p += 4;
14251 }
14252 else
14253 {
14254 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14255 p += 4;
14256 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14257 p);
14258 p += 4;
14259 }
14260 }
14261 bfd_put_32 (htab->glink->owner,
14262 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14263 indx++;
14264 p += 4;
14265 }
14266 }
14267
14268 /* Build .glink global entry stubs, and PLT relocs for globals. */
14269 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14270
14271 if (!write_plt_relocs_for_local_syms (info))
14272 return FALSE;
14273
14274 if (htab->brlt != NULL && htab->brlt->size != 0)
14275 {
14276 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14277 htab->brlt->size);
14278 if (htab->brlt->contents == NULL)
14279 return FALSE;
14280 }
14281 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14282 {
14283 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14284 htab->relbrlt->size);
14285 if (htab->relbrlt->contents == NULL)
14286 return FALSE;
14287 }
14288
14289 /* Build the stubs as directed by the stub hash table. */
14290 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14291
14292 for (group = htab->group; group != NULL; group = group->next)
14293 if (group->needs_save_res)
14294 group->stub_sec->size += htab->sfpr->size;
14295
14296 if (htab->relbrlt != NULL)
14297 htab->relbrlt->reloc_count = 0;
14298
14299 if (htab->params->plt_stub_align != 0)
14300 for (group = htab->group; group != NULL; group = group->next)
14301 if ((stub_sec = group->stub_sec) != NULL)
14302 {
14303 int align = abs (htab->params->plt_stub_align);
14304 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14305 }
14306
14307 for (group = htab->group; group != NULL; group = group->next)
14308 if (group->needs_save_res)
14309 {
14310 stub_sec = group->stub_sec;
14311 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14312 htab->sfpr->contents, htab->sfpr->size);
14313 if (htab->params->emit_stub_syms)
14314 {
14315 unsigned int i;
14316
14317 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14318 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14319 return FALSE;
14320 }
14321 }
14322
14323 if (htab->glink_eh_frame != NULL
14324 && htab->glink_eh_frame->size != 0)
14325 {
14326 bfd_vma val;
14327 size_t align = 4;
14328
14329 p = htab->glink_eh_frame->contents;
14330 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14331
14332 for (group = htab->group; group != NULL; group = group->next)
14333 if (group->eh_size != 0)
14334 {
14335 /* Offset to stub section. */
14336 val = (group->stub_sec->output_section->vma
14337 + group->stub_sec->output_offset);
14338 val -= (htab->glink_eh_frame->output_section->vma
14339 + htab->glink_eh_frame->output_offset
14340 + (p + 8 - htab->glink_eh_frame->contents));
14341 if (val + 0x80000000 > 0xffffffff)
14342 {
14343 _bfd_error_handler
14344 (_("%s offset too large for .eh_frame sdata4 encoding"),
14345 group->stub_sec->name);
14346 return FALSE;
14347 }
14348 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14349 p += (group->eh_size + 17 + 3) & -4;
14350 }
14351 if (htab->glink != NULL && htab->glink->size != 0)
14352 {
14353 /* Offset to .glink. */
14354 val = (htab->glink->output_section->vma
14355 + htab->glink->output_offset
14356 + 8);
14357 val -= (htab->glink_eh_frame->output_section->vma
14358 + htab->glink_eh_frame->output_offset
14359 + (p + 8 - htab->glink_eh_frame->contents));
14360 if (val + 0x80000000 > 0xffffffff)
14361 {
14362 _bfd_error_handler
14363 (_("%s offset too large for .eh_frame sdata4 encoding"),
14364 htab->glink->name);
14365 return FALSE;
14366 }
14367 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14368 p += (24 + align - 1) & -align;
14369 }
14370 }
14371
14372 for (group = htab->group; group != NULL; group = group->next)
14373 if ((stub_sec = group->stub_sec) != NULL)
14374 {
14375 stub_sec_count += 1;
14376 if (stub_sec->rawsize != stub_sec->size
14377 && (htab->stub_iteration <= STUB_SHRINK_ITER
14378 || stub_sec->rawsize < stub_sec->size))
14379 break;
14380 }
14381
14382 if (group != NULL)
14383 {
14384 htab->stub_error = TRUE;
14385 _bfd_error_handler (_("stubs don't match calculated size"));
14386 }
14387
14388 if (htab->stub_error)
14389 return FALSE;
14390
14391 if (stats != NULL)
14392 {
14393 size_t len;
14394 *stats = bfd_malloc (500);
14395 if (*stats == NULL)
14396 return FALSE;
14397
14398 len = sprintf (*stats,
14399 ngettext ("linker stubs in %u group\n",
14400 "linker stubs in %u groups\n",
14401 stub_sec_count),
14402 stub_sec_count);
14403 sprintf (*stats + len, _(" branch %lu\n"
14404 " branch toc adj %lu\n"
14405 " branch notoc %lu\n"
14406 " branch both %lu\n"
14407 " long branch %lu\n"
14408 " long toc adj %lu\n"
14409 " long notoc %lu\n"
14410 " long both %lu\n"
14411 " plt call %lu\n"
14412 " plt call save %lu\n"
14413 " plt call notoc %lu\n"
14414 " plt call both %lu\n"
14415 " global entry %lu"),
14416 htab->stub_count[ppc_stub_long_branch - 1],
14417 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14418 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14419 htab->stub_count[ppc_stub_long_branch_both - 1],
14420 htab->stub_count[ppc_stub_plt_branch - 1],
14421 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14422 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14423 htab->stub_count[ppc_stub_plt_branch_both - 1],
14424 htab->stub_count[ppc_stub_plt_call - 1],
14425 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14426 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14427 htab->stub_count[ppc_stub_plt_call_both - 1],
14428 htab->stub_count[ppc_stub_global_entry - 1]);
14429 }
14430 return TRUE;
14431 }
14432
14433 /* What to do when ld finds relocations against symbols defined in
14434 discarded sections. */
14435
14436 static unsigned int
14437 ppc64_elf_action_discarded (asection *sec)
14438 {
14439 if (strcmp (".opd", sec->name) == 0)
14440 return 0;
14441
14442 if (strcmp (".toc", sec->name) == 0)
14443 return 0;
14444
14445 if (strcmp (".toc1", sec->name) == 0)
14446 return 0;
14447
14448 return _bfd_elf_default_action_discarded (sec);
14449 }
14450
14451 /* The RELOCATE_SECTION function is called by the ELF backend linker
14452 to handle the relocations for a section.
14453
14454 The relocs are always passed as Rela structures; if the section
14455 actually uses Rel structures, the r_addend field will always be
14456 zero.
14457
14458 This function is responsible for adjust the section contents as
14459 necessary, and (if using Rela relocs and generating a
14460 relocatable output file) adjusting the reloc addend as
14461 necessary.
14462
14463 This function does not have to worry about setting the reloc
14464 address or the reloc symbol index.
14465
14466 LOCAL_SYMS is a pointer to the swapped in local symbols.
14467
14468 LOCAL_SECTIONS is an array giving the section in the input file
14469 corresponding to the st_shndx field of each local symbol.
14470
14471 The global hash table entry for the global symbols can be found
14472 via elf_sym_hashes (input_bfd).
14473
14474 When generating relocatable output, this function must handle
14475 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14476 going to be the section symbol corresponding to the output
14477 section, which means that the addend must be adjusted
14478 accordingly. */
14479
14480 static bfd_boolean
14481 ppc64_elf_relocate_section (bfd *output_bfd,
14482 struct bfd_link_info *info,
14483 bfd *input_bfd,
14484 asection *input_section,
14485 bfd_byte *contents,
14486 Elf_Internal_Rela *relocs,
14487 Elf_Internal_Sym *local_syms,
14488 asection **local_sections)
14489 {
14490 struct ppc_link_hash_table *htab;
14491 Elf_Internal_Shdr *symtab_hdr;
14492 struct elf_link_hash_entry **sym_hashes;
14493 Elf_Internal_Rela *rel;
14494 Elf_Internal_Rela *wrel;
14495 Elf_Internal_Rela *relend;
14496 Elf_Internal_Rela outrel;
14497 bfd_byte *loc;
14498 struct got_entry **local_got_ents;
14499 bfd_vma TOCstart;
14500 bfd_boolean ret = TRUE;
14501 bfd_boolean is_opd;
14502 /* Assume 'at' branch hints. */
14503 bfd_boolean is_isa_v2 = TRUE;
14504 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14505
14506 /* Initialize howto table if needed. */
14507 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14508 ppc_howto_init ();
14509
14510 htab = ppc_hash_table (info);
14511 if (htab == NULL)
14512 return FALSE;
14513
14514 /* Don't relocate stub sections. */
14515 if (input_section->owner == htab->params->stub_bfd)
14516 return TRUE;
14517
14518 BFD_ASSERT (is_ppc64_elf (input_bfd));
14519
14520 local_got_ents = elf_local_got_ents (input_bfd);
14521 TOCstart = elf_gp (output_bfd);
14522 symtab_hdr = &elf_symtab_hdr (input_bfd);
14523 sym_hashes = elf_sym_hashes (input_bfd);
14524 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14525
14526 rel = wrel = relocs;
14527 relend = relocs + input_section->reloc_count;
14528 for (; rel < relend; wrel++, rel++)
14529 {
14530 enum elf_ppc64_reloc_type r_type;
14531 bfd_vma addend;
14532 bfd_reloc_status_type r;
14533 Elf_Internal_Sym *sym;
14534 asection *sec;
14535 struct elf_link_hash_entry *h_elf;
14536 struct ppc_link_hash_entry *h;
14537 struct ppc_link_hash_entry *fdh;
14538 const char *sym_name;
14539 unsigned long r_symndx, toc_symndx;
14540 bfd_vma toc_addend;
14541 unsigned char tls_mask, tls_gd, tls_type;
14542 unsigned char sym_type;
14543 bfd_vma relocation;
14544 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14545 bfd_boolean warned;
14546 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14547 unsigned int insn;
14548 unsigned int mask;
14549 struct ppc_stub_hash_entry *stub_entry;
14550 bfd_vma max_br_offset;
14551 bfd_vma from;
14552 Elf_Internal_Rela orig_rel;
14553 reloc_howto_type *howto;
14554 struct reloc_howto_struct alt_howto;
14555
14556 again:
14557 orig_rel = *rel;
14558
14559 r_type = ELF64_R_TYPE (rel->r_info);
14560 r_symndx = ELF64_R_SYM (rel->r_info);
14561
14562 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14563 symbol of the previous ADDR64 reloc. The symbol gives us the
14564 proper TOC base to use. */
14565 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14566 && wrel != relocs
14567 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14568 && is_opd)
14569 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14570
14571 sym = NULL;
14572 sec = NULL;
14573 h_elf = NULL;
14574 sym_name = NULL;
14575 unresolved_reloc = FALSE;
14576 warned = FALSE;
14577
14578 if (r_symndx < symtab_hdr->sh_info)
14579 {
14580 /* It's a local symbol. */
14581 struct _opd_sec_data *opd;
14582
14583 sym = local_syms + r_symndx;
14584 sec = local_sections[r_symndx];
14585 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14586 sym_type = ELF64_ST_TYPE (sym->st_info);
14587 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14588 opd = get_opd_info (sec);
14589 if (opd != NULL && opd->adjust != NULL)
14590 {
14591 long adjust = opd->adjust[OPD_NDX (sym->st_value
14592 + rel->r_addend)];
14593 if (adjust == -1)
14594 relocation = 0;
14595 else
14596 {
14597 /* If this is a relocation against the opd section sym
14598 and we have edited .opd, adjust the reloc addend so
14599 that ld -r and ld --emit-relocs output is correct.
14600 If it is a reloc against some other .opd symbol,
14601 then the symbol value will be adjusted later. */
14602 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14603 rel->r_addend += adjust;
14604 else
14605 relocation += adjust;
14606 }
14607 }
14608 }
14609 else
14610 {
14611 bfd_boolean ignored;
14612
14613 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14614 r_symndx, symtab_hdr, sym_hashes,
14615 h_elf, sec, relocation,
14616 unresolved_reloc, warned, ignored);
14617 sym_name = h_elf->root.root.string;
14618 sym_type = h_elf->type;
14619 if (sec != NULL
14620 && sec->owner == output_bfd
14621 && strcmp (sec->name, ".opd") == 0)
14622 {
14623 /* This is a symbol defined in a linker script. All
14624 such are defined in output sections, even those
14625 defined by simple assignment from a symbol defined in
14626 an input section. Transfer the symbol to an
14627 appropriate input .opd section, so that a branch to
14628 this symbol will be mapped to the location specified
14629 by the opd entry. */
14630 struct bfd_link_order *lo;
14631 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14632 if (lo->type == bfd_indirect_link_order)
14633 {
14634 asection *isec = lo->u.indirect.section;
14635 if (h_elf->root.u.def.value >= isec->output_offset
14636 && h_elf->root.u.def.value < (isec->output_offset
14637 + isec->size))
14638 {
14639 h_elf->root.u.def.value -= isec->output_offset;
14640 h_elf->root.u.def.section = isec;
14641 sec = isec;
14642 break;
14643 }
14644 }
14645 }
14646 }
14647 h = (struct ppc_link_hash_entry *) h_elf;
14648
14649 if (sec != NULL && discarded_section (sec))
14650 {
14651 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14652 input_bfd, input_section,
14653 contents + rel->r_offset);
14654 wrel->r_offset = rel->r_offset;
14655 wrel->r_info = 0;
14656 wrel->r_addend = 0;
14657
14658 /* For ld -r, remove relocations in debug sections against
14659 symbols defined in discarded sections. Not done for
14660 non-debug to preserve relocs in .eh_frame which the
14661 eh_frame editing code expects to be present. */
14662 if (bfd_link_relocatable (info)
14663 && (input_section->flags & SEC_DEBUGGING))
14664 wrel--;
14665
14666 continue;
14667 }
14668
14669 if (bfd_link_relocatable (info))
14670 goto copy_reloc;
14671
14672 if (h != NULL && &h->elf == htab->elf.hgot)
14673 {
14674 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14675 sec = bfd_abs_section_ptr;
14676 unresolved_reloc = FALSE;
14677 }
14678
14679 /* TLS optimizations. Replace instruction sequences and relocs
14680 based on information we collected in tls_optimize. We edit
14681 RELOCS so that --emit-relocs will output something sensible
14682 for the final instruction stream. */
14683 tls_mask = 0;
14684 tls_gd = 0;
14685 toc_symndx = 0;
14686 if (h != NULL)
14687 tls_mask = h->tls_mask;
14688 else if (local_got_ents != NULL)
14689 {
14690 struct plt_entry **local_plt = (struct plt_entry **)
14691 (local_got_ents + symtab_hdr->sh_info);
14692 unsigned char *lgot_masks = (unsigned char *)
14693 (local_plt + symtab_hdr->sh_info);
14694 tls_mask = lgot_masks[r_symndx];
14695 }
14696 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14697 && (r_type == R_PPC64_TLS
14698 || r_type == R_PPC64_TLSGD
14699 || r_type == R_PPC64_TLSLD))
14700 {
14701 /* Check for toc tls entries. */
14702 unsigned char *toc_tls;
14703
14704 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14705 &local_syms, rel, input_bfd))
14706 return FALSE;
14707
14708 if (toc_tls)
14709 tls_mask = *toc_tls;
14710 }
14711
14712 /* Check that tls relocs are used with tls syms, and non-tls
14713 relocs are used with non-tls syms. */
14714 if (r_symndx != STN_UNDEF
14715 && r_type != R_PPC64_NONE
14716 && (h == NULL
14717 || h->elf.root.type == bfd_link_hash_defined
14718 || h->elf.root.type == bfd_link_hash_defweak)
14719 && (IS_PPC64_TLS_RELOC (r_type)
14720 != (sym_type == STT_TLS
14721 || (sym_type == STT_SECTION
14722 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14723 {
14724 if ((tls_mask & TLS_TLS) != 0
14725 && (r_type == R_PPC64_TLS
14726 || r_type == R_PPC64_TLSGD
14727 || r_type == R_PPC64_TLSLD))
14728 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14729 ;
14730 else
14731 info->callbacks->einfo
14732 (!IS_PPC64_TLS_RELOC (r_type)
14733 /* xgettext:c-format */
14734 ? _("%H: %s used with TLS symbol `%pT'\n")
14735 /* xgettext:c-format */
14736 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14737 input_bfd, input_section, rel->r_offset,
14738 ppc64_elf_howto_table[r_type]->name,
14739 sym_name);
14740 }
14741
14742 /* Ensure reloc mapping code below stays sane. */
14743 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14744 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14745 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14746 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14747 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14748 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14749 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14750 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14751 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14752 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14753 abort ();
14754
14755 switch (r_type)
14756 {
14757 default:
14758 break;
14759
14760 case R_PPC64_LO_DS_OPT:
14761 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14762 if ((insn & (0x3f << 26)) != 58u << 26)
14763 abort ();
14764 insn += (14u << 26) - (58u << 26);
14765 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14766 r_type = R_PPC64_TOC16_LO;
14767 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14768 break;
14769
14770 case R_PPC64_TOC16:
14771 case R_PPC64_TOC16_LO:
14772 case R_PPC64_TOC16_DS:
14773 case R_PPC64_TOC16_LO_DS:
14774 {
14775 /* Check for toc tls entries. */
14776 unsigned char *toc_tls;
14777 int retval;
14778
14779 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14780 &local_syms, rel, input_bfd);
14781 if (retval == 0)
14782 return FALSE;
14783
14784 if (toc_tls)
14785 {
14786 tls_mask = *toc_tls;
14787 if (r_type == R_PPC64_TOC16_DS
14788 || r_type == R_PPC64_TOC16_LO_DS)
14789 {
14790 if ((tls_mask & TLS_TLS) != 0
14791 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14792 goto toctprel;
14793 }
14794 else
14795 {
14796 /* If we found a GD reloc pair, then we might be
14797 doing a GD->IE transition. */
14798 if (retval == 2)
14799 {
14800 tls_gd = TLS_TPRELGD;
14801 if ((tls_mask & TLS_TLS) != 0
14802 && (tls_mask & TLS_GD) == 0)
14803 goto tls_ldgd_opt;
14804 }
14805 else if (retval == 3)
14806 {
14807 if ((tls_mask & TLS_TLS) != 0
14808 && (tls_mask & TLS_LD) == 0)
14809 goto tls_ldgd_opt;
14810 }
14811 }
14812 }
14813 }
14814 break;
14815
14816 case R_PPC64_GOT_TPREL16_HI:
14817 case R_PPC64_GOT_TPREL16_HA:
14818 if ((tls_mask & TLS_TLS) != 0
14819 && (tls_mask & TLS_TPREL) == 0)
14820 {
14821 rel->r_offset -= d_offset;
14822 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14823 r_type = R_PPC64_NONE;
14824 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14825 }
14826 break;
14827
14828 case R_PPC64_GOT_TPREL16_DS:
14829 case R_PPC64_GOT_TPREL16_LO_DS:
14830 if ((tls_mask & TLS_TLS) != 0
14831 && (tls_mask & TLS_TPREL) == 0)
14832 {
14833 toctprel:
14834 insn = bfd_get_32 (input_bfd,
14835 contents + rel->r_offset - d_offset);
14836 insn &= 31 << 21;
14837 insn |= 0x3c0d0000; /* addis 0,13,0 */
14838 bfd_put_32 (input_bfd, insn,
14839 contents + rel->r_offset - d_offset);
14840 r_type = R_PPC64_TPREL16_HA;
14841 if (toc_symndx != 0)
14842 {
14843 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14844 rel->r_addend = toc_addend;
14845 /* We changed the symbol. Start over in order to
14846 get h, sym, sec etc. right. */
14847 goto again;
14848 }
14849 else
14850 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14851 }
14852 break;
14853
14854 case R_PPC64_TLS:
14855 if ((tls_mask & TLS_TLS) != 0
14856 && (tls_mask & TLS_TPREL) == 0)
14857 {
14858 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14859 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14860 if (insn == 0)
14861 abort ();
14862 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14863 /* Was PPC64_TLS which sits on insn boundary, now
14864 PPC64_TPREL16_LO which is at low-order half-word. */
14865 rel->r_offset += d_offset;
14866 r_type = R_PPC64_TPREL16_LO;
14867 if (toc_symndx != 0)
14868 {
14869 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14870 rel->r_addend = toc_addend;
14871 /* We changed the symbol. Start over in order to
14872 get h, sym, sec etc. right. */
14873 goto again;
14874 }
14875 else
14876 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14877 }
14878 break;
14879
14880 case R_PPC64_GOT_TLSGD16_HI:
14881 case R_PPC64_GOT_TLSGD16_HA:
14882 tls_gd = TLS_TPRELGD;
14883 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14884 goto tls_gdld_hi;
14885 break;
14886
14887 case R_PPC64_GOT_TLSLD16_HI:
14888 case R_PPC64_GOT_TLSLD16_HA:
14889 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14890 {
14891 tls_gdld_hi:
14892 if ((tls_mask & tls_gd) != 0)
14893 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14894 + R_PPC64_GOT_TPREL16_DS);
14895 else
14896 {
14897 rel->r_offset -= d_offset;
14898 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14899 r_type = R_PPC64_NONE;
14900 }
14901 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14902 }
14903 break;
14904
14905 case R_PPC64_GOT_TLSGD16:
14906 case R_PPC64_GOT_TLSGD16_LO:
14907 tls_gd = TLS_TPRELGD;
14908 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14909 goto tls_ldgd_opt;
14910 break;
14911
14912 case R_PPC64_GOT_TLSLD16:
14913 case R_PPC64_GOT_TLSLD16_LO:
14914 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14915 {
14916 unsigned int insn1, insn2;
14917 bfd_vma offset;
14918
14919 tls_ldgd_opt:
14920 offset = (bfd_vma) -1;
14921 /* If not using the newer R_PPC64_TLSGD/LD to mark
14922 __tls_get_addr calls, we must trust that the call
14923 stays with its arg setup insns, ie. that the next
14924 reloc is the __tls_get_addr call associated with
14925 the current reloc. Edit both insns. */
14926 if (input_section->has_tls_get_addr_call
14927 && rel + 1 < relend
14928 && branch_reloc_hash_match (input_bfd, rel + 1,
14929 htab->tls_get_addr,
14930 htab->tls_get_addr_fd))
14931 offset = rel[1].r_offset;
14932 /* We read the low GOT_TLS (or TOC16) insn because we
14933 need to keep the destination reg. It may be
14934 something other than the usual r3, and moved to r3
14935 before the call by intervening code. */
14936 insn1 = bfd_get_32 (input_bfd,
14937 contents + rel->r_offset - d_offset);
14938 if ((tls_mask & tls_gd) != 0)
14939 {
14940 /* IE */
14941 insn1 &= (0x1f << 21) | (0x1f << 16);
14942 insn1 |= 58 << 26; /* ld */
14943 insn2 = 0x7c636a14; /* add 3,3,13 */
14944 if (offset != (bfd_vma) -1)
14945 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14946 if ((tls_mask & TLS_EXPLICIT) == 0)
14947 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14948 + R_PPC64_GOT_TPREL16_DS);
14949 else
14950 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14951 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14952 }
14953 else
14954 {
14955 /* LE */
14956 insn1 &= 0x1f << 21;
14957 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14958 insn2 = 0x38630000; /* addi 3,3,0 */
14959 if (tls_gd == 0)
14960 {
14961 /* Was an LD reloc. */
14962 if (toc_symndx)
14963 sec = local_sections[toc_symndx];
14964 for (r_symndx = 0;
14965 r_symndx < symtab_hdr->sh_info;
14966 r_symndx++)
14967 if (local_sections[r_symndx] == sec)
14968 break;
14969 if (r_symndx >= symtab_hdr->sh_info)
14970 r_symndx = STN_UNDEF;
14971 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14972 if (r_symndx != STN_UNDEF)
14973 rel->r_addend -= (local_syms[r_symndx].st_value
14974 + sec->output_offset
14975 + sec->output_section->vma);
14976 }
14977 else if (toc_symndx != 0)
14978 {
14979 r_symndx = toc_symndx;
14980 rel->r_addend = toc_addend;
14981 }
14982 r_type = R_PPC64_TPREL16_HA;
14983 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14984 if (offset != (bfd_vma) -1)
14985 {
14986 rel[1].r_info = ELF64_R_INFO (r_symndx,
14987 R_PPC64_TPREL16_LO);
14988 rel[1].r_offset = offset + d_offset;
14989 rel[1].r_addend = rel->r_addend;
14990 }
14991 }
14992 bfd_put_32 (input_bfd, insn1,
14993 contents + rel->r_offset - d_offset);
14994 if (offset != (bfd_vma) -1)
14995 bfd_put_32 (input_bfd, insn2, contents + offset);
14996 if ((tls_mask & tls_gd) == 0
14997 && (tls_gd == 0 || toc_symndx != 0))
14998 {
14999 /* We changed the symbol. Start over in order
15000 to get h, sym, sec etc. right. */
15001 goto again;
15002 }
15003 }
15004 break;
15005
15006 case R_PPC64_TLSGD:
15007 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15008 && rel + 1 < relend)
15009 {
15010 unsigned int insn2;
15011 bfd_vma offset = rel->r_offset;
15012
15013 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
15014 {
15015 bfd_put_32 (output_bfd, NOP, contents + offset);
15016 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15017 break;
15018 }
15019
15020 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15021 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15022
15023 if ((tls_mask & TLS_TPRELGD) != 0)
15024 {
15025 /* IE */
15026 r_type = R_PPC64_NONE;
15027 insn2 = 0x7c636a14; /* add 3,3,13 */
15028 }
15029 else
15030 {
15031 /* LE */
15032 if (toc_symndx != 0)
15033 {
15034 r_symndx = toc_symndx;
15035 rel->r_addend = toc_addend;
15036 }
15037 r_type = R_PPC64_TPREL16_LO;
15038 rel->r_offset = offset + d_offset;
15039 insn2 = 0x38630000; /* addi 3,3,0 */
15040 }
15041 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15042 /* Zap the reloc on the _tls_get_addr call too. */
15043 BFD_ASSERT (offset == rel[1].r_offset);
15044 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15045 bfd_put_32 (input_bfd, insn2, contents + offset);
15046 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
15047 goto again;
15048 }
15049 break;
15050
15051 case R_PPC64_TLSLD:
15052 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15053 && rel + 1 < relend)
15054 {
15055 unsigned int insn2;
15056 bfd_vma offset = rel->r_offset;
15057
15058 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
15059 {
15060 bfd_put_32 (output_bfd, NOP, contents + offset);
15061 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15062 break;
15063 }
15064
15065 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15066 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15067
15068 if (toc_symndx)
15069 sec = local_sections[toc_symndx];
15070 for (r_symndx = 0;
15071 r_symndx < symtab_hdr->sh_info;
15072 r_symndx++)
15073 if (local_sections[r_symndx] == sec)
15074 break;
15075 if (r_symndx >= symtab_hdr->sh_info)
15076 r_symndx = STN_UNDEF;
15077 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15078 if (r_symndx != STN_UNDEF)
15079 rel->r_addend -= (local_syms[r_symndx].st_value
15080 + sec->output_offset
15081 + sec->output_section->vma);
15082
15083 r_type = R_PPC64_TPREL16_LO;
15084 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15085 rel->r_offset = offset + d_offset;
15086 /* Zap the reloc on the _tls_get_addr call too. */
15087 BFD_ASSERT (offset == rel[1].r_offset);
15088 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15089 insn2 = 0x38630000; /* addi 3,3,0 */
15090 bfd_put_32 (input_bfd, insn2, contents + offset);
15091 goto again;
15092 }
15093 break;
15094
15095 case R_PPC64_DTPMOD64:
15096 if (rel + 1 < relend
15097 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15098 && rel[1].r_offset == rel->r_offset + 8)
15099 {
15100 if ((tls_mask & TLS_GD) == 0)
15101 {
15102 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15103 if ((tls_mask & TLS_TPRELGD) != 0)
15104 r_type = R_PPC64_TPREL64;
15105 else
15106 {
15107 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15108 r_type = R_PPC64_NONE;
15109 }
15110 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15111 }
15112 }
15113 else
15114 {
15115 if ((tls_mask & TLS_LD) == 0)
15116 {
15117 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15118 r_type = R_PPC64_NONE;
15119 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15120 }
15121 }
15122 break;
15123
15124 case R_PPC64_TPREL64:
15125 if ((tls_mask & TLS_TPREL) == 0)
15126 {
15127 r_type = R_PPC64_NONE;
15128 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15129 }
15130 break;
15131
15132 case R_PPC64_ENTRY:
15133 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15134 if (!bfd_link_pic (info)
15135 && !info->traditional_format
15136 && relocation + 0x80008000 <= 0xffffffff)
15137 {
15138 unsigned int insn1, insn2;
15139
15140 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15141 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15142 if ((insn1 & ~0xfffc) == LD_R2_0R12
15143 && insn2 == ADD_R2_R2_R12)
15144 {
15145 bfd_put_32 (input_bfd,
15146 LIS_R2 + PPC_HA (relocation),
15147 contents + rel->r_offset);
15148 bfd_put_32 (input_bfd,
15149 ADDI_R2_R2 + PPC_LO (relocation),
15150 contents + rel->r_offset + 4);
15151 }
15152 }
15153 else
15154 {
15155 relocation -= (rel->r_offset
15156 + input_section->output_offset
15157 + input_section->output_section->vma);
15158 if (relocation + 0x80008000 <= 0xffffffff)
15159 {
15160 unsigned int insn1, insn2;
15161
15162 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15163 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15164 if ((insn1 & ~0xfffc) == LD_R2_0R12
15165 && insn2 == ADD_R2_R2_R12)
15166 {
15167 bfd_put_32 (input_bfd,
15168 ADDIS_R2_R12 + PPC_HA (relocation),
15169 contents + rel->r_offset);
15170 bfd_put_32 (input_bfd,
15171 ADDI_R2_R2 + PPC_LO (relocation),
15172 contents + rel->r_offset + 4);
15173 }
15174 }
15175 }
15176 break;
15177
15178 case R_PPC64_REL16_HA:
15179 /* If we are generating a non-PIC executable, edit
15180 . 0: addis 2,12,.TOC.-0b@ha
15181 . addi 2,2,.TOC.-0b@l
15182 used by ELFv2 global entry points to set up r2, to
15183 . lis 2,.TOC.@ha
15184 . addi 2,2,.TOC.@l
15185 if .TOC. is in range. */
15186 if (!bfd_link_pic (info)
15187 && !info->traditional_format
15188 && !htab->opd_abi
15189 && rel->r_addend == d_offset
15190 && h != NULL && &h->elf == htab->elf.hgot
15191 && rel + 1 < relend
15192 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15193 && rel[1].r_offset == rel->r_offset + 4
15194 && rel[1].r_addend == rel->r_addend + 4
15195 && relocation + 0x80008000 <= 0xffffffff)
15196 {
15197 unsigned int insn1, insn2;
15198 bfd_vma offset = rel->r_offset - d_offset;
15199 insn1 = bfd_get_32 (input_bfd, contents + offset);
15200 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15201 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15202 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15203 {
15204 r_type = R_PPC64_ADDR16_HA;
15205 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15206 rel->r_addend -= d_offset;
15207 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15208 rel[1].r_addend -= d_offset + 4;
15209 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15210 }
15211 }
15212 break;
15213 }
15214
15215 /* Handle other relocations that tweak non-addend part of insn. */
15216 insn = 0;
15217 max_br_offset = 1 << 25;
15218 addend = rel->r_addend;
15219 reloc_dest = DEST_NORMAL;
15220 switch (r_type)
15221 {
15222 default:
15223 break;
15224
15225 case R_PPC64_TOCSAVE:
15226 if (relocation + addend == (rel->r_offset
15227 + input_section->output_offset
15228 + input_section->output_section->vma)
15229 && tocsave_find (htab, NO_INSERT,
15230 &local_syms, rel, input_bfd))
15231 {
15232 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15233 if (insn == NOP
15234 || insn == CROR_151515 || insn == CROR_313131)
15235 bfd_put_32 (input_bfd,
15236 STD_R2_0R1 + STK_TOC (htab),
15237 contents + rel->r_offset);
15238 }
15239 break;
15240
15241 /* Branch taken prediction relocations. */
15242 case R_PPC64_ADDR14_BRTAKEN:
15243 case R_PPC64_REL14_BRTAKEN:
15244 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15245 /* Fall through. */
15246
15247 /* Branch not taken prediction relocations. */
15248 case R_PPC64_ADDR14_BRNTAKEN:
15249 case R_PPC64_REL14_BRNTAKEN:
15250 insn |= bfd_get_32 (input_bfd,
15251 contents + rel->r_offset) & ~(0x01 << 21);
15252 /* Fall through. */
15253
15254 case R_PPC64_REL14:
15255 max_br_offset = 1 << 15;
15256 /* Fall through. */
15257
15258 case R_PPC64_REL24:
15259 case R_PPC64_REL24_NOTOC:
15260 case R_PPC64_PLTCALL:
15261 /* Calls to functions with a different TOC, such as calls to
15262 shared objects, need to alter the TOC pointer. This is
15263 done using a linkage stub. A REL24 branching to these
15264 linkage stubs needs to be followed by a nop, as the nop
15265 will be replaced with an instruction to restore the TOC
15266 base pointer. */
15267 fdh = h;
15268 if (h != NULL
15269 && h->oh != NULL
15270 && h->oh->is_func_descriptor)
15271 fdh = ppc_follow_link (h->oh);
15272 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15273 htab);
15274 if (r_type == R_PPC64_PLTCALL
15275 && stub_entry != NULL
15276 && stub_entry->stub_type >= ppc_stub_plt_call
15277 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15278 stub_entry = NULL;
15279
15280 if (stub_entry != NULL
15281 && (stub_entry->stub_type == ppc_stub_plt_call
15282 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15283 || stub_entry->stub_type == ppc_stub_plt_call_both
15284 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15285 || stub_entry->stub_type == ppc_stub_plt_branch_both
15286 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15287 || stub_entry->stub_type == ppc_stub_long_branch_both))
15288 {
15289 bfd_boolean can_plt_call = FALSE;
15290
15291 if (stub_entry->stub_type == ppc_stub_plt_call
15292 && !htab->opd_abi
15293 && htab->params->plt_localentry0 != 0
15294 && is_elfv2_localentry0 (&h->elf))
15295 {
15296 /* The function doesn't use or change r2. */
15297 can_plt_call = TRUE;
15298 }
15299 else if (r_type == R_PPC64_REL24_NOTOC)
15300 {
15301 /* NOTOC calls don't need to restore r2. */
15302 can_plt_call = TRUE;
15303 }
15304
15305 /* All of these stubs may modify r2, so there must be a
15306 branch and link followed by a nop. The nop is
15307 replaced by an insn to restore r2. */
15308 else if (rel->r_offset + 8 <= input_section->size)
15309 {
15310 unsigned long br;
15311
15312 br = bfd_get_32 (input_bfd,
15313 contents + rel->r_offset);
15314 if ((br & 1) != 0)
15315 {
15316 unsigned long nop;
15317
15318 nop = bfd_get_32 (input_bfd,
15319 contents + rel->r_offset + 4);
15320 if (nop == LD_R2_0R1 + STK_TOC (htab))
15321 can_plt_call = TRUE;
15322 else if (nop == NOP
15323 || nop == CROR_151515
15324 || nop == CROR_313131)
15325 {
15326 if (h != NULL
15327 && (h == htab->tls_get_addr_fd
15328 || h == htab->tls_get_addr)
15329 && htab->params->tls_get_addr_opt)
15330 {
15331 /* Special stub used, leave nop alone. */
15332 }
15333 else
15334 bfd_put_32 (input_bfd,
15335 LD_R2_0R1 + STK_TOC (htab),
15336 contents + rel->r_offset + 4);
15337 can_plt_call = TRUE;
15338 }
15339 }
15340 }
15341
15342 if (!can_plt_call && h != NULL)
15343 {
15344 const char *name = h->elf.root.root.string;
15345
15346 if (*name == '.')
15347 ++name;
15348
15349 if (strncmp (name, "__libc_start_main", 17) == 0
15350 && (name[17] == 0 || name[17] == '@'))
15351 {
15352 /* Allow crt1 branch to go via a toc adjusting
15353 stub. Other calls that never return could do
15354 the same, if we could detect such. */
15355 can_plt_call = TRUE;
15356 }
15357 }
15358
15359 if (!can_plt_call)
15360 {
15361 /* g++ as of 20130507 emits self-calls without a
15362 following nop. This is arguably wrong since we
15363 have conflicting information. On the one hand a
15364 global symbol and on the other a local call
15365 sequence, but don't error for this special case.
15366 It isn't possible to cheaply verify we have
15367 exactly such a call. Allow all calls to the same
15368 section. */
15369 asection *code_sec = sec;
15370
15371 if (get_opd_info (sec) != NULL)
15372 {
15373 bfd_vma off = (relocation + addend
15374 - sec->output_section->vma
15375 - sec->output_offset);
15376
15377 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15378 }
15379 if (code_sec == input_section)
15380 can_plt_call = TRUE;
15381 }
15382
15383 if (!can_plt_call)
15384 {
15385 if (stub_entry->stub_type >= ppc_stub_plt_call
15386 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15387 info->callbacks->einfo
15388 /* xgettext:c-format */
15389 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15390 "recompile with -fPIC\n"),
15391 input_bfd, input_section, rel->r_offset, sym_name);
15392 else
15393 info->callbacks->einfo
15394 /* xgettext:c-format */
15395 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15396 "(-mcmodel=small toc adjust stub)\n"),
15397 input_bfd, input_section, rel->r_offset, sym_name);
15398
15399 bfd_set_error (bfd_error_bad_value);
15400 ret = FALSE;
15401 }
15402
15403 if (can_plt_call
15404 && stub_entry->stub_type >= ppc_stub_plt_call
15405 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15406 unresolved_reloc = FALSE;
15407 }
15408
15409 if ((stub_entry == NULL
15410 || stub_entry->stub_type == ppc_stub_long_branch
15411 || stub_entry->stub_type == ppc_stub_plt_branch)
15412 && get_opd_info (sec) != NULL)
15413 {
15414 /* The branch destination is the value of the opd entry. */
15415 bfd_vma off = (relocation + addend
15416 - sec->output_section->vma
15417 - sec->output_offset);
15418 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15419 if (dest != (bfd_vma) -1)
15420 {
15421 relocation = dest;
15422 addend = 0;
15423 reloc_dest = DEST_OPD;
15424 }
15425 }
15426
15427 /* If the branch is out of reach we ought to have a long
15428 branch stub. */
15429 from = (rel->r_offset
15430 + input_section->output_offset
15431 + input_section->output_section->vma);
15432
15433 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15434 ? fdh->elf.other
15435 : sym->st_other);
15436
15437 if (stub_entry != NULL
15438 && (stub_entry->stub_type == ppc_stub_long_branch
15439 || stub_entry->stub_type == ppc_stub_plt_branch)
15440 && (r_type == R_PPC64_ADDR14_BRTAKEN
15441 || r_type == R_PPC64_ADDR14_BRNTAKEN
15442 || (relocation + addend - from + max_br_offset
15443 < 2 * max_br_offset)))
15444 /* Don't use the stub if this branch is in range. */
15445 stub_entry = NULL;
15446
15447 if (stub_entry != NULL
15448 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15449 || stub_entry->stub_type == ppc_stub_long_branch_both
15450 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15451 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15452 && (r_type != R_PPC64_REL24_NOTOC
15453 || ((fdh ? fdh->elf.other : sym->st_other)
15454 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
15455 && (relocation + addend - from + max_br_offset
15456 < 2 * max_br_offset))
15457 stub_entry = NULL;
15458
15459 if (stub_entry != NULL
15460 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15461 || stub_entry->stub_type == ppc_stub_long_branch_both
15462 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15463 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15464 && r_type == R_PPC64_REL24_NOTOC
15465 && (relocation + addend - from + max_br_offset
15466 < 2 * max_br_offset))
15467 stub_entry = NULL;
15468
15469 if (stub_entry != NULL)
15470 {
15471 /* Munge up the value and addend so that we call the stub
15472 rather than the procedure directly. */
15473 asection *stub_sec = stub_entry->group->stub_sec;
15474
15475 if (stub_entry->stub_type == ppc_stub_save_res)
15476 relocation += (stub_sec->output_offset
15477 + stub_sec->output_section->vma
15478 + stub_sec->size - htab->sfpr->size
15479 - htab->sfpr->output_offset
15480 - htab->sfpr->output_section->vma);
15481 else
15482 relocation = (stub_entry->stub_offset
15483 + stub_sec->output_offset
15484 + stub_sec->output_section->vma);
15485 addend = 0;
15486 reloc_dest = DEST_STUB;
15487
15488 if (((stub_entry->stub_type == ppc_stub_plt_call
15489 && ALWAYS_EMIT_R2SAVE)
15490 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15491 || stub_entry->stub_type == ppc_stub_plt_call_both)
15492 && !(h != NULL
15493 && (h == htab->tls_get_addr_fd
15494 || h == htab->tls_get_addr)
15495 && htab->params->tls_get_addr_opt)
15496 && rel + 1 < relend
15497 && rel[1].r_offset == rel->r_offset + 4
15498 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15499 relocation += 4;
15500 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15501 || stub_entry->stub_type == ppc_stub_plt_branch_both
15502 || stub_entry->stub_type == ppc_stub_plt_call_both)
15503 && r_type == R_PPC64_REL24_NOTOC)
15504 relocation += 4;
15505 }
15506
15507 if (insn != 0)
15508 {
15509 if (is_isa_v2)
15510 {
15511 /* Set 'a' bit. This is 0b00010 in BO field for branch
15512 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15513 for branch on CTR insns (BO == 1a00t or 1a01t). */
15514 if ((insn & (0x14 << 21)) == (0x04 << 21))
15515 insn |= 0x02 << 21;
15516 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15517 insn |= 0x08 << 21;
15518 else
15519 break;
15520 }
15521 else
15522 {
15523 /* Invert 'y' bit if not the default. */
15524 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15525 insn ^= 0x01 << 21;
15526 }
15527
15528 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15529 }
15530
15531 /* NOP out calls to undefined weak functions.
15532 We can thus call a weak function without first
15533 checking whether the function is defined. */
15534 else if (h != NULL
15535 && h->elf.root.type == bfd_link_hash_undefweak
15536 && h->elf.dynindx == -1
15537 && (r_type == R_PPC64_REL24
15538 || r_type == R_PPC64_REL24_NOTOC)
15539 && relocation == 0
15540 && addend == 0)
15541 {
15542 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15543 goto copy_reloc;
15544 }
15545 break;
15546 }
15547
15548 /* Set `addend'. */
15549 tls_type = 0;
15550 save_unresolved_reloc = unresolved_reloc;
15551 switch (r_type)
15552 {
15553 default:
15554 /* xgettext:c-format */
15555 _bfd_error_handler (_("%pB: %s unsupported"),
15556 input_bfd, ppc64_elf_howto_table[r_type]->name);
15557
15558 bfd_set_error (bfd_error_bad_value);
15559 ret = FALSE;
15560 goto copy_reloc;
15561
15562 case R_PPC64_NONE:
15563 case R_PPC64_TLS:
15564 case R_PPC64_TLSGD:
15565 case R_PPC64_TLSLD:
15566 case R_PPC64_TOCSAVE:
15567 case R_PPC64_GNU_VTINHERIT:
15568 case R_PPC64_GNU_VTENTRY:
15569 case R_PPC64_ENTRY:
15570 goto copy_reloc;
15571
15572 /* GOT16 relocations. Like an ADDR16 using the symbol's
15573 address in the GOT as relocation value instead of the
15574 symbol's value itself. Also, create a GOT entry for the
15575 symbol and put the symbol value there. */
15576 case R_PPC64_GOT_TLSGD16:
15577 case R_PPC64_GOT_TLSGD16_LO:
15578 case R_PPC64_GOT_TLSGD16_HI:
15579 case R_PPC64_GOT_TLSGD16_HA:
15580 tls_type = TLS_TLS | TLS_GD;
15581 goto dogot;
15582
15583 case R_PPC64_GOT_TLSLD16:
15584 case R_PPC64_GOT_TLSLD16_LO:
15585 case R_PPC64_GOT_TLSLD16_HI:
15586 case R_PPC64_GOT_TLSLD16_HA:
15587 tls_type = TLS_TLS | TLS_LD;
15588 goto dogot;
15589
15590 case R_PPC64_GOT_TPREL16_DS:
15591 case R_PPC64_GOT_TPREL16_LO_DS:
15592 case R_PPC64_GOT_TPREL16_HI:
15593 case R_PPC64_GOT_TPREL16_HA:
15594 tls_type = TLS_TLS | TLS_TPREL;
15595 goto dogot;
15596
15597 case R_PPC64_GOT_DTPREL16_DS:
15598 case R_PPC64_GOT_DTPREL16_LO_DS:
15599 case R_PPC64_GOT_DTPREL16_HI:
15600 case R_PPC64_GOT_DTPREL16_HA:
15601 tls_type = TLS_TLS | TLS_DTPREL;
15602 goto dogot;
15603
15604 case R_PPC64_GOT16:
15605 case R_PPC64_GOT16_LO:
15606 case R_PPC64_GOT16_HI:
15607 case R_PPC64_GOT16_HA:
15608 case R_PPC64_GOT16_DS:
15609 case R_PPC64_GOT16_LO_DS:
15610 dogot:
15611 {
15612 /* Relocation is to the entry for this symbol in the global
15613 offset table. */
15614 asection *got;
15615 bfd_vma *offp;
15616 bfd_vma off;
15617 unsigned long indx = 0;
15618 struct got_entry *ent;
15619
15620 if (tls_type == (TLS_TLS | TLS_LD)
15621 && (h == NULL
15622 || !h->elf.def_dynamic))
15623 ent = ppc64_tlsld_got (input_bfd);
15624 else
15625 {
15626 if (h != NULL)
15627 {
15628 if (!htab->elf.dynamic_sections_created
15629 || h->elf.dynindx == -1
15630 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15631 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15632 /* This is actually a static link, or it is a
15633 -Bsymbolic link and the symbol is defined
15634 locally, or the symbol was forced to be local
15635 because of a version file. */
15636 ;
15637 else
15638 {
15639 indx = h->elf.dynindx;
15640 unresolved_reloc = FALSE;
15641 }
15642 ent = h->elf.got.glist;
15643 }
15644 else
15645 {
15646 if (local_got_ents == NULL)
15647 abort ();
15648 ent = local_got_ents[r_symndx];
15649 }
15650
15651 for (; ent != NULL; ent = ent->next)
15652 if (ent->addend == orig_rel.r_addend
15653 && ent->owner == input_bfd
15654 && ent->tls_type == tls_type)
15655 break;
15656 }
15657
15658 if (ent == NULL)
15659 abort ();
15660 if (ent->is_indirect)
15661 ent = ent->got.ent;
15662 offp = &ent->got.offset;
15663 got = ppc64_elf_tdata (ent->owner)->got;
15664 if (got == NULL)
15665 abort ();
15666
15667 /* The offset must always be a multiple of 8. We use the
15668 least significant bit to record whether we have already
15669 processed this entry. */
15670 off = *offp;
15671 if ((off & 1) != 0)
15672 off &= ~1;
15673 else
15674 {
15675 /* Generate relocs for the dynamic linker, except in
15676 the case of TLSLD where we'll use one entry per
15677 module. */
15678 asection *relgot;
15679 bfd_boolean ifunc;
15680
15681 *offp = off | 1;
15682 relgot = NULL;
15683 ifunc = (h != NULL
15684 ? h->elf.type == STT_GNU_IFUNC
15685 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15686 if (ifunc)
15687 {
15688 relgot = htab->elf.irelplt;
15689 if (indx == 0)
15690 htab->local_ifunc_resolver = 1;
15691 else if (is_static_defined (&h->elf))
15692 htab->maybe_local_ifunc_resolver = 1;
15693 }
15694 else if (indx != 0
15695 || (bfd_link_pic (info)
15696 && (h == NULL
15697 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15698 || (tls_type == (TLS_TLS | TLS_LD)
15699 && !h->elf.def_dynamic))
15700 && !(tls_type == (TLS_TLS | TLS_TPREL)
15701 && bfd_link_executable (info)
15702 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15703 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15704 if (relgot != NULL)
15705 {
15706 outrel.r_offset = (got->output_section->vma
15707 + got->output_offset
15708 + off);
15709 outrel.r_addend = addend;
15710 if (tls_type & (TLS_LD | TLS_GD))
15711 {
15712 outrel.r_addend = 0;
15713 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15714 if (tls_type == (TLS_TLS | TLS_GD))
15715 {
15716 loc = relgot->contents;
15717 loc += (relgot->reloc_count++
15718 * sizeof (Elf64_External_Rela));
15719 bfd_elf64_swap_reloca_out (output_bfd,
15720 &outrel, loc);
15721 outrel.r_offset += 8;
15722 outrel.r_addend = addend;
15723 outrel.r_info
15724 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15725 }
15726 }
15727 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15728 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15729 else if (tls_type == (TLS_TLS | TLS_TPREL))
15730 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15731 else if (indx != 0)
15732 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15733 else
15734 {
15735 if (ifunc)
15736 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15737 else
15738 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15739
15740 /* Write the .got section contents for the sake
15741 of prelink. */
15742 loc = got->contents + off;
15743 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15744 loc);
15745 }
15746
15747 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15748 {
15749 outrel.r_addend += relocation;
15750 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15751 {
15752 if (htab->elf.tls_sec == NULL)
15753 outrel.r_addend = 0;
15754 else
15755 outrel.r_addend -= htab->elf.tls_sec->vma;
15756 }
15757 }
15758 loc = relgot->contents;
15759 loc += (relgot->reloc_count++
15760 * sizeof (Elf64_External_Rela));
15761 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15762 }
15763
15764 /* Init the .got section contents here if we're not
15765 emitting a reloc. */
15766 else
15767 {
15768 relocation += addend;
15769 if (tls_type != 0)
15770 {
15771 if (htab->elf.tls_sec == NULL)
15772 relocation = 0;
15773 else
15774 {
15775 if (tls_type & TLS_LD)
15776 relocation = 0;
15777 else
15778 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15779 if (tls_type & TLS_TPREL)
15780 relocation += DTP_OFFSET - TP_OFFSET;
15781 }
15782
15783 if (tls_type & (TLS_GD | TLS_LD))
15784 {
15785 bfd_put_64 (output_bfd, relocation,
15786 got->contents + off + 8);
15787 relocation = 1;
15788 }
15789 }
15790 bfd_put_64 (output_bfd, relocation,
15791 got->contents + off);
15792 }
15793 }
15794
15795 if (off >= (bfd_vma) -2)
15796 abort ();
15797
15798 relocation = got->output_section->vma + got->output_offset + off;
15799 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15800 }
15801 break;
15802
15803 case R_PPC64_PLT16_HA:
15804 case R_PPC64_PLT16_HI:
15805 case R_PPC64_PLT16_LO:
15806 case R_PPC64_PLT16_LO_DS:
15807 case R_PPC64_PLT32:
15808 case R_PPC64_PLT64:
15809 case R_PPC64_PLTSEQ:
15810 case R_PPC64_PLTCALL:
15811 /* Relocation is to the entry for this symbol in the
15812 procedure linkage table. */
15813 unresolved_reloc = TRUE;
15814 {
15815 struct plt_entry **plt_list = NULL;
15816 if (h != NULL)
15817 plt_list = &h->elf.plt.plist;
15818 else if (local_got_ents != NULL)
15819 {
15820 struct plt_entry **local_plt = (struct plt_entry **)
15821 (local_got_ents + symtab_hdr->sh_info);
15822 plt_list = local_plt + r_symndx;
15823 }
15824 if (plt_list)
15825 {
15826 struct plt_entry *ent;
15827
15828 for (ent = *plt_list; ent != NULL; ent = ent->next)
15829 if (ent->plt.offset != (bfd_vma) -1
15830 && ent->addend == orig_rel.r_addend)
15831 {
15832 asection *plt;
15833 bfd_vma got;
15834
15835 plt = htab->elf.splt;
15836 if (!htab->elf.dynamic_sections_created
15837 || h == NULL
15838 || h->elf.dynindx == -1)
15839 {
15840 if (h != NULL
15841 ? h->elf.type == STT_GNU_IFUNC
15842 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15843 plt = htab->elf.iplt;
15844 else
15845 plt = htab->pltlocal;
15846 }
15847 relocation = (plt->output_section->vma
15848 + plt->output_offset
15849 + ent->plt.offset);
15850 if (r_type == R_PPC64_PLT16_HA
15851 || r_type ==R_PPC64_PLT16_HI
15852 || r_type ==R_PPC64_PLT16_LO
15853 || r_type ==R_PPC64_PLT16_LO_DS)
15854 {
15855 got = (elf_gp (output_bfd)
15856 + htab->sec_info[input_section->id].toc_off);
15857 relocation -= got;
15858 }
15859 addend = 0;
15860 unresolved_reloc = FALSE;
15861 break;
15862 }
15863 }
15864 }
15865 break;
15866
15867 case R_PPC64_TOC:
15868 /* Relocation value is TOC base. */
15869 relocation = TOCstart;
15870 if (r_symndx == STN_UNDEF)
15871 relocation += htab->sec_info[input_section->id].toc_off;
15872 else if (unresolved_reloc)
15873 ;
15874 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15875 relocation += htab->sec_info[sec->id].toc_off;
15876 else
15877 unresolved_reloc = TRUE;
15878 goto dodyn;
15879
15880 /* TOC16 relocs. We want the offset relative to the TOC base,
15881 which is the address of the start of the TOC plus 0x8000.
15882 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15883 in this order. */
15884 case R_PPC64_TOC16:
15885 case R_PPC64_TOC16_LO:
15886 case R_PPC64_TOC16_HI:
15887 case R_PPC64_TOC16_DS:
15888 case R_PPC64_TOC16_LO_DS:
15889 case R_PPC64_TOC16_HA:
15890 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15891 break;
15892
15893 /* Relocate against the beginning of the section. */
15894 case R_PPC64_SECTOFF:
15895 case R_PPC64_SECTOFF_LO:
15896 case R_PPC64_SECTOFF_HI:
15897 case R_PPC64_SECTOFF_DS:
15898 case R_PPC64_SECTOFF_LO_DS:
15899 case R_PPC64_SECTOFF_HA:
15900 if (sec != NULL)
15901 addend -= sec->output_section->vma;
15902 break;
15903
15904 case R_PPC64_REL16:
15905 case R_PPC64_REL16_LO:
15906 case R_PPC64_REL16_HI:
15907 case R_PPC64_REL16_HA:
15908 case R_PPC64_REL16DX_HA:
15909 break;
15910
15911 case R_PPC64_REL14:
15912 case R_PPC64_REL14_BRNTAKEN:
15913 case R_PPC64_REL14_BRTAKEN:
15914 case R_PPC64_REL24:
15915 case R_PPC64_REL24_NOTOC:
15916 break;
15917
15918 case R_PPC64_TPREL16:
15919 case R_PPC64_TPREL16_LO:
15920 case R_PPC64_TPREL16_HI:
15921 case R_PPC64_TPREL16_HA:
15922 case R_PPC64_TPREL16_DS:
15923 case R_PPC64_TPREL16_LO_DS:
15924 case R_PPC64_TPREL16_HIGH:
15925 case R_PPC64_TPREL16_HIGHA:
15926 case R_PPC64_TPREL16_HIGHER:
15927 case R_PPC64_TPREL16_HIGHERA:
15928 case R_PPC64_TPREL16_HIGHEST:
15929 case R_PPC64_TPREL16_HIGHESTA:
15930 if (h != NULL
15931 && h->elf.root.type == bfd_link_hash_undefweak
15932 && h->elf.dynindx == -1)
15933 {
15934 /* Make this relocation against an undefined weak symbol
15935 resolve to zero. This is really just a tweak, since
15936 code using weak externs ought to check that they are
15937 defined before using them. */
15938 bfd_byte *p = contents + rel->r_offset - d_offset;
15939
15940 insn = bfd_get_32 (input_bfd, p);
15941 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15942 if (insn != 0)
15943 bfd_put_32 (input_bfd, insn, p);
15944 break;
15945 }
15946 if (htab->elf.tls_sec != NULL)
15947 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15948 /* The TPREL16 relocs shouldn't really be used in shared
15949 libs or with non-local symbols as that will result in
15950 DT_TEXTREL being set, but support them anyway. */
15951 goto dodyn;
15952
15953 case R_PPC64_DTPREL16:
15954 case R_PPC64_DTPREL16_LO:
15955 case R_PPC64_DTPREL16_HI:
15956 case R_PPC64_DTPREL16_HA:
15957 case R_PPC64_DTPREL16_DS:
15958 case R_PPC64_DTPREL16_LO_DS:
15959 case R_PPC64_DTPREL16_HIGH:
15960 case R_PPC64_DTPREL16_HIGHA:
15961 case R_PPC64_DTPREL16_HIGHER:
15962 case R_PPC64_DTPREL16_HIGHERA:
15963 case R_PPC64_DTPREL16_HIGHEST:
15964 case R_PPC64_DTPREL16_HIGHESTA:
15965 if (htab->elf.tls_sec != NULL)
15966 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15967 break;
15968
15969 case R_PPC64_ADDR64_LOCAL:
15970 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15971 ? h->elf.other
15972 : sym->st_other);
15973 break;
15974
15975 case R_PPC64_DTPMOD64:
15976 relocation = 1;
15977 addend = 0;
15978 goto dodyn;
15979
15980 case R_PPC64_TPREL64:
15981 if (htab->elf.tls_sec != NULL)
15982 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15983 goto dodyn;
15984
15985 case R_PPC64_DTPREL64:
15986 if (htab->elf.tls_sec != NULL)
15987 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15988 /* Fall through. */
15989
15990 /* Relocations that may need to be propagated if this is a
15991 dynamic object. */
15992 case R_PPC64_REL30:
15993 case R_PPC64_REL32:
15994 case R_PPC64_REL64:
15995 case R_PPC64_ADDR14:
15996 case R_PPC64_ADDR14_BRNTAKEN:
15997 case R_PPC64_ADDR14_BRTAKEN:
15998 case R_PPC64_ADDR16:
15999 case R_PPC64_ADDR16_DS:
16000 case R_PPC64_ADDR16_HA:
16001 case R_PPC64_ADDR16_HI:
16002 case R_PPC64_ADDR16_HIGH:
16003 case R_PPC64_ADDR16_HIGHA:
16004 case R_PPC64_ADDR16_HIGHER:
16005 case R_PPC64_ADDR16_HIGHERA:
16006 case R_PPC64_ADDR16_HIGHEST:
16007 case R_PPC64_ADDR16_HIGHESTA:
16008 case R_PPC64_ADDR16_LO:
16009 case R_PPC64_ADDR16_LO_DS:
16010 case R_PPC64_ADDR24:
16011 case R_PPC64_ADDR32:
16012 case R_PPC64_ADDR64:
16013 case R_PPC64_UADDR16:
16014 case R_PPC64_UADDR32:
16015 case R_PPC64_UADDR64:
16016 dodyn:
16017 if ((input_section->flags & SEC_ALLOC) == 0)
16018 break;
16019
16020 if (NO_OPD_RELOCS && is_opd)
16021 break;
16022
16023 if (bfd_link_pic (info)
16024 ? ((h == NULL
16025 || h->dyn_relocs != NULL)
16026 && ((h != NULL && pc_dynrelocs (h))
16027 || must_be_dyn_reloc (info, r_type)))
16028 : (h != NULL
16029 ? h->dyn_relocs != NULL
16030 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16031 {
16032 bfd_boolean skip, relocate;
16033 asection *sreloc;
16034 bfd_vma out_off;
16035 long indx = 0;
16036
16037 /* When generating a dynamic object, these relocations
16038 are copied into the output file to be resolved at run
16039 time. */
16040
16041 skip = FALSE;
16042 relocate = FALSE;
16043
16044 out_off = _bfd_elf_section_offset (output_bfd, info,
16045 input_section, rel->r_offset);
16046 if (out_off == (bfd_vma) -1)
16047 skip = TRUE;
16048 else if (out_off == (bfd_vma) -2)
16049 skip = TRUE, relocate = TRUE;
16050 out_off += (input_section->output_section->vma
16051 + input_section->output_offset);
16052 outrel.r_offset = out_off;
16053 outrel.r_addend = rel->r_addend;
16054
16055 /* Optimize unaligned reloc use. */
16056 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16057 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16058 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16059 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16060 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16061 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16062 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16063 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16064 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16065
16066 if (skip)
16067 memset (&outrel, 0, sizeof outrel);
16068 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16069 && !is_opd
16070 && r_type != R_PPC64_TOC)
16071 {
16072 indx = h->elf.dynindx;
16073 BFD_ASSERT (indx != -1);
16074 outrel.r_info = ELF64_R_INFO (indx, r_type);
16075 }
16076 else
16077 {
16078 /* This symbol is local, or marked to become local,
16079 or this is an opd section reloc which must point
16080 at a local function. */
16081 outrel.r_addend += relocation;
16082 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16083 {
16084 if (is_opd && h != NULL)
16085 {
16086 /* Lie about opd entries. This case occurs
16087 when building shared libraries and we
16088 reference a function in another shared
16089 lib. The same thing happens for a weak
16090 definition in an application that's
16091 overridden by a strong definition in a
16092 shared lib. (I believe this is a generic
16093 bug in binutils handling of weak syms.)
16094 In these cases we won't use the opd
16095 entry in this lib. */
16096 unresolved_reloc = FALSE;
16097 }
16098 if (!is_opd
16099 && r_type == R_PPC64_ADDR64
16100 && (h != NULL
16101 ? h->elf.type == STT_GNU_IFUNC
16102 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16103 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16104 else
16105 {
16106 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16107
16108 /* We need to relocate .opd contents for ld.so.
16109 Prelink also wants simple and consistent rules
16110 for relocs. This make all RELATIVE relocs have
16111 *r_offset equal to r_addend. */
16112 relocate = TRUE;
16113 }
16114 }
16115 else
16116 {
16117 if (h != NULL
16118 ? h->elf.type == STT_GNU_IFUNC
16119 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16120 {
16121 info->callbacks->einfo
16122 /* xgettext:c-format */
16123 (_("%H: %s for indirect "
16124 "function `%pT' unsupported\n"),
16125 input_bfd, input_section, rel->r_offset,
16126 ppc64_elf_howto_table[r_type]->name,
16127 sym_name);
16128 ret = FALSE;
16129 }
16130 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16131 ;
16132 else if (sec == NULL || sec->owner == NULL)
16133 {
16134 bfd_set_error (bfd_error_bad_value);
16135 return FALSE;
16136 }
16137 else
16138 {
16139 asection *osec;
16140
16141 osec = sec->output_section;
16142 indx = elf_section_data (osec)->dynindx;
16143
16144 if (indx == 0)
16145 {
16146 if ((osec->flags & SEC_READONLY) == 0
16147 && htab->elf.data_index_section != NULL)
16148 osec = htab->elf.data_index_section;
16149 else
16150 osec = htab->elf.text_index_section;
16151 indx = elf_section_data (osec)->dynindx;
16152 }
16153 BFD_ASSERT (indx != 0);
16154
16155 /* We are turning this relocation into one
16156 against a section symbol, so subtract out
16157 the output section's address but not the
16158 offset of the input section in the output
16159 section. */
16160 outrel.r_addend -= osec->vma;
16161 }
16162
16163 outrel.r_info = ELF64_R_INFO (indx, r_type);
16164 }
16165 }
16166
16167 sreloc = elf_section_data (input_section)->sreloc;
16168 if (h != NULL
16169 ? h->elf.type == STT_GNU_IFUNC
16170 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16171 {
16172 sreloc = htab->elf.irelplt;
16173 if (indx == 0)
16174 htab->local_ifunc_resolver = 1;
16175 else if (is_static_defined (&h->elf))
16176 htab->maybe_local_ifunc_resolver = 1;
16177 }
16178 if (sreloc == NULL)
16179 abort ();
16180
16181 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16182 >= sreloc->size)
16183 abort ();
16184 loc = sreloc->contents;
16185 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16186 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16187
16188 /* If this reloc is against an external symbol, it will
16189 be computed at runtime, so there's no need to do
16190 anything now. However, for the sake of prelink ensure
16191 that the section contents are a known value. */
16192 if (! relocate)
16193 {
16194 unresolved_reloc = FALSE;
16195 /* The value chosen here is quite arbitrary as ld.so
16196 ignores section contents except for the special
16197 case of .opd where the contents might be accessed
16198 before relocation. Choose zero, as that won't
16199 cause reloc overflow. */
16200 relocation = 0;
16201 addend = 0;
16202 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16203 to improve backward compatibility with older
16204 versions of ld. */
16205 if (r_type == R_PPC64_ADDR64)
16206 addend = outrel.r_addend;
16207 /* Adjust pc_relative relocs to have zero in *r_offset. */
16208 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16209 addend = outrel.r_offset;
16210 }
16211 }
16212 break;
16213
16214 case R_PPC64_COPY:
16215 case R_PPC64_GLOB_DAT:
16216 case R_PPC64_JMP_SLOT:
16217 case R_PPC64_JMP_IREL:
16218 case R_PPC64_RELATIVE:
16219 /* We shouldn't ever see these dynamic relocs in relocatable
16220 files. */
16221 /* Fall through. */
16222
16223 case R_PPC64_PLTGOT16:
16224 case R_PPC64_PLTGOT16_DS:
16225 case R_PPC64_PLTGOT16_HA:
16226 case R_PPC64_PLTGOT16_HI:
16227 case R_PPC64_PLTGOT16_LO:
16228 case R_PPC64_PLTGOT16_LO_DS:
16229 case R_PPC64_PLTREL32:
16230 case R_PPC64_PLTREL64:
16231 /* These ones haven't been implemented yet. */
16232
16233 info->callbacks->einfo
16234 /* xgettext:c-format */
16235 (_("%P: %pB: %s is not supported for `%pT'\n"),
16236 input_bfd,
16237 ppc64_elf_howto_table[r_type]->name, sym_name);
16238
16239 bfd_set_error (bfd_error_invalid_operation);
16240 ret = FALSE;
16241 goto copy_reloc;
16242 }
16243
16244 /* Multi-instruction sequences that access the TOC can be
16245 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16246 to nop; addi rb,r2,x; */
16247 switch (r_type)
16248 {
16249 default:
16250 break;
16251
16252 case R_PPC64_GOT_TLSLD16_HI:
16253 case R_PPC64_GOT_TLSGD16_HI:
16254 case R_PPC64_GOT_TPREL16_HI:
16255 case R_PPC64_GOT_DTPREL16_HI:
16256 case R_PPC64_GOT16_HI:
16257 case R_PPC64_TOC16_HI:
16258 /* These relocs would only be useful if building up an
16259 offset to later add to r2, perhaps in an indexed
16260 addressing mode instruction. Don't try to optimize.
16261 Unfortunately, the possibility of someone building up an
16262 offset like this or even with the HA relocs, means that
16263 we need to check the high insn when optimizing the low
16264 insn. */
16265 break;
16266
16267 case R_PPC64_PLTCALL:
16268 if (unresolved_reloc)
16269 {
16270 /* No plt entry. Make this into a direct call. */
16271 bfd_byte *p = contents + rel->r_offset;
16272 insn = bfd_get_32 (input_bfd, p);
16273 insn &= 1;
16274 bfd_put_32 (input_bfd, B_DOT | insn, p);
16275 bfd_put_32 (input_bfd, NOP, p + 4);
16276 unresolved_reloc = save_unresolved_reloc;
16277 r_type = R_PPC64_REL24;
16278 }
16279 break;
16280
16281 case R_PPC64_PLTSEQ:
16282 if (unresolved_reloc)
16283 {
16284 unresolved_reloc = FALSE;
16285 goto nop_it;
16286 }
16287 break;
16288
16289 case R_PPC64_PLT16_HA:
16290 if (unresolved_reloc)
16291 {
16292 unresolved_reloc = FALSE;
16293 goto nop_it;
16294 }
16295 /* Fall through. */
16296 case R_PPC64_GOT_TLSLD16_HA:
16297 case R_PPC64_GOT_TLSGD16_HA:
16298 case R_PPC64_GOT_TPREL16_HA:
16299 case R_PPC64_GOT_DTPREL16_HA:
16300 case R_PPC64_GOT16_HA:
16301 case R_PPC64_TOC16_HA:
16302 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16303 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16304 {
16305 bfd_byte *p;
16306 nop_it:
16307 p = contents + (rel->r_offset & ~3);
16308 bfd_put_32 (input_bfd, NOP, p);
16309 goto copy_reloc;
16310 }
16311 break;
16312
16313 case R_PPC64_PLT16_LO:
16314 case R_PPC64_PLT16_LO_DS:
16315 if (unresolved_reloc)
16316 {
16317 unresolved_reloc = FALSE;
16318 goto nop_it;
16319 }
16320 /* Fall through. */
16321 case R_PPC64_GOT_TLSLD16_LO:
16322 case R_PPC64_GOT_TLSGD16_LO:
16323 case R_PPC64_GOT_TPREL16_LO_DS:
16324 case R_PPC64_GOT_DTPREL16_LO_DS:
16325 case R_PPC64_GOT16_LO:
16326 case R_PPC64_GOT16_LO_DS:
16327 case R_PPC64_TOC16_LO:
16328 case R_PPC64_TOC16_LO_DS:
16329 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16330 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16331 {
16332 bfd_byte *p = contents + (rel->r_offset & ~3);
16333 insn = bfd_get_32 (input_bfd, p);
16334 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16335 {
16336 /* Transform addic to addi when we change reg. */
16337 insn &= ~((0x3f << 26) | (0x1f << 16));
16338 insn |= (14u << 26) | (2 << 16);
16339 }
16340 else
16341 {
16342 insn &= ~(0x1f << 16);
16343 insn |= 2 << 16;
16344 }
16345 bfd_put_32 (input_bfd, insn, p);
16346 }
16347 break;
16348
16349 case R_PPC64_TPREL16_HA:
16350 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16351 {
16352 bfd_byte *p = contents + (rel->r_offset & ~3);
16353 insn = bfd_get_32 (input_bfd, p);
16354 if ((insn & ((0x3f << 26) | 0x1f << 16))
16355 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16356 /* xgettext:c-format */
16357 info->callbacks->minfo
16358 (_("%H: warning: %s unexpected insn %#x.\n"),
16359 input_bfd, input_section, rel->r_offset,
16360 ppc64_elf_howto_table[r_type]->name, insn);
16361 else
16362 {
16363 bfd_put_32 (input_bfd, NOP, p);
16364 goto copy_reloc;
16365 }
16366 }
16367 break;
16368
16369 case R_PPC64_TPREL16_LO:
16370 case R_PPC64_TPREL16_LO_DS:
16371 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16372 {
16373 bfd_byte *p = contents + (rel->r_offset & ~3);
16374 insn = bfd_get_32 (input_bfd, p);
16375 insn &= ~(0x1f << 16);
16376 insn |= 13 << 16;
16377 bfd_put_32 (input_bfd, insn, p);
16378 }
16379 break;
16380 }
16381
16382 /* Do any further special processing. */
16383 switch (r_type)
16384 {
16385 default:
16386 break;
16387
16388 case R_PPC64_REL16_HA:
16389 case R_PPC64_REL16DX_HA:
16390 case R_PPC64_ADDR16_HA:
16391 case R_PPC64_ADDR16_HIGHA:
16392 case R_PPC64_ADDR16_HIGHERA:
16393 case R_PPC64_ADDR16_HIGHESTA:
16394 case R_PPC64_TOC16_HA:
16395 case R_PPC64_SECTOFF_HA:
16396 case R_PPC64_TPREL16_HA:
16397 case R_PPC64_TPREL16_HIGHA:
16398 case R_PPC64_TPREL16_HIGHERA:
16399 case R_PPC64_TPREL16_HIGHESTA:
16400 case R_PPC64_DTPREL16_HA:
16401 case R_PPC64_DTPREL16_HIGHA:
16402 case R_PPC64_DTPREL16_HIGHERA:
16403 case R_PPC64_DTPREL16_HIGHESTA:
16404 /* It's just possible that this symbol is a weak symbol
16405 that's not actually defined anywhere. In that case,
16406 'sec' would be NULL, and we should leave the symbol
16407 alone (it will be set to zero elsewhere in the link). */
16408 if (sec == NULL)
16409 break;
16410 /* Fall through. */
16411
16412 case R_PPC64_GOT16_HA:
16413 case R_PPC64_PLTGOT16_HA:
16414 case R_PPC64_PLT16_HA:
16415 case R_PPC64_GOT_TLSGD16_HA:
16416 case R_PPC64_GOT_TLSLD16_HA:
16417 case R_PPC64_GOT_TPREL16_HA:
16418 case R_PPC64_GOT_DTPREL16_HA:
16419 /* Add 0x10000 if sign bit in 0:15 is set.
16420 Bits 0:15 are not used. */
16421 addend += 0x8000;
16422 break;
16423
16424 case R_PPC64_ADDR16_DS:
16425 case R_PPC64_ADDR16_LO_DS:
16426 case R_PPC64_GOT16_DS:
16427 case R_PPC64_GOT16_LO_DS:
16428 case R_PPC64_PLT16_LO_DS:
16429 case R_PPC64_SECTOFF_DS:
16430 case R_PPC64_SECTOFF_LO_DS:
16431 case R_PPC64_TOC16_DS:
16432 case R_PPC64_TOC16_LO_DS:
16433 case R_PPC64_PLTGOT16_DS:
16434 case R_PPC64_PLTGOT16_LO_DS:
16435 case R_PPC64_GOT_TPREL16_DS:
16436 case R_PPC64_GOT_TPREL16_LO_DS:
16437 case R_PPC64_GOT_DTPREL16_DS:
16438 case R_PPC64_GOT_DTPREL16_LO_DS:
16439 case R_PPC64_TPREL16_DS:
16440 case R_PPC64_TPREL16_LO_DS:
16441 case R_PPC64_DTPREL16_DS:
16442 case R_PPC64_DTPREL16_LO_DS:
16443 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16444 mask = 3;
16445 /* If this reloc is against an lq, lxv, or stxv insn, then
16446 the value must be a multiple of 16. This is somewhat of
16447 a hack, but the "correct" way to do this by defining _DQ
16448 forms of all the _DS relocs bloats all reloc switches in
16449 this file. It doesn't make much sense to use these
16450 relocs in data, so testing the insn should be safe. */
16451 if ((insn & (0x3f << 26)) == (56u << 26)
16452 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16453 mask = 15;
16454 relocation += addend;
16455 addend = insn & (mask ^ 3);
16456 if ((relocation & mask) != 0)
16457 {
16458 relocation ^= relocation & mask;
16459 info->callbacks->einfo
16460 /* xgettext:c-format */
16461 (_("%H: error: %s not a multiple of %u\n"),
16462 input_bfd, input_section, rel->r_offset,
16463 ppc64_elf_howto_table[r_type]->name,
16464 mask + 1);
16465 bfd_set_error (bfd_error_bad_value);
16466 ret = FALSE;
16467 goto copy_reloc;
16468 }
16469 break;
16470 }
16471
16472 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16473 because such sections are not SEC_ALLOC and thus ld.so will
16474 not process them. */
16475 howto = ppc64_elf_howto_table[(int) r_type];
16476 if (unresolved_reloc
16477 && !((input_section->flags & SEC_DEBUGGING) != 0
16478 && h->elf.def_dynamic)
16479 && _bfd_elf_section_offset (output_bfd, info, input_section,
16480 rel->r_offset) != (bfd_vma) -1)
16481 {
16482 info->callbacks->einfo
16483 /* xgettext:c-format */
16484 (_("%H: unresolvable %s against `%pT'\n"),
16485 input_bfd, input_section, rel->r_offset,
16486 howto->name,
16487 h->elf.root.root.string);
16488 ret = FALSE;
16489 }
16490
16491 /* 16-bit fields in insns mostly have signed values, but a
16492 few insns have 16-bit unsigned values. Really, we should
16493 have different reloc types. */
16494 if (howto->complain_on_overflow != complain_overflow_dont
16495 && howto->dst_mask == 0xffff
16496 && (input_section->flags & SEC_CODE) != 0)
16497 {
16498 enum complain_overflow complain = complain_overflow_signed;
16499
16500 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16501 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16502 complain = complain_overflow_bitfield;
16503 else if (howto->rightshift == 0
16504 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16505 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16506 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16507 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16508 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16509 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16510 complain = complain_overflow_unsigned;
16511 if (howto->complain_on_overflow != complain)
16512 {
16513 alt_howto = *howto;
16514 alt_howto.complain_on_overflow = complain;
16515 howto = &alt_howto;
16516 }
16517 }
16518
16519 if (r_type == R_PPC64_REL16DX_HA)
16520 {
16521 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
16522 if (rel->r_offset + 4 > input_section->size)
16523 r = bfd_reloc_outofrange;
16524 else
16525 {
16526 relocation += addend;
16527 relocation -= (rel->r_offset
16528 + input_section->output_offset
16529 + input_section->output_section->vma);
16530 relocation = (bfd_signed_vma) relocation >> 16;
16531 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16532 insn &= ~0x1fffc1;
16533 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16534 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16535 r = bfd_reloc_ok;
16536 if (relocation + 0x8000 > 0xffff)
16537 r = bfd_reloc_overflow;
16538 }
16539 }
16540 else
16541 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
16542 rel->r_offset, relocation, addend);
16543
16544 if (r != bfd_reloc_ok)
16545 {
16546 char *more_info = NULL;
16547 const char *reloc_name = howto->name;
16548
16549 if (reloc_dest != DEST_NORMAL)
16550 {
16551 more_info = bfd_malloc (strlen (reloc_name) + 8);
16552 if (more_info != NULL)
16553 {
16554 strcpy (more_info, reloc_name);
16555 strcat (more_info, (reloc_dest == DEST_OPD
16556 ? " (OPD)" : " (stub)"));
16557 reloc_name = more_info;
16558 }
16559 }
16560
16561 if (r == bfd_reloc_overflow)
16562 {
16563 /* On code like "if (foo) foo();" don't report overflow
16564 on a branch to zero when foo is undefined. */
16565 if (!warned
16566 && (reloc_dest == DEST_STUB
16567 || !(h != NULL
16568 && (h->elf.root.type == bfd_link_hash_undefweak
16569 || h->elf.root.type == bfd_link_hash_undefined)
16570 && is_branch_reloc (r_type))))
16571 info->callbacks->reloc_overflow (info, &h->elf.root,
16572 sym_name, reloc_name,
16573 orig_rel.r_addend,
16574 input_bfd, input_section,
16575 rel->r_offset);
16576 }
16577 else
16578 {
16579 info->callbacks->einfo
16580 /* xgettext:c-format */
16581 (_("%H: %s against `%pT': error %d\n"),
16582 input_bfd, input_section, rel->r_offset,
16583 reloc_name, sym_name, (int) r);
16584 ret = FALSE;
16585 }
16586 if (more_info != NULL)
16587 free (more_info);
16588 }
16589 copy_reloc:
16590 if (wrel != rel)
16591 *wrel = *rel;
16592 }
16593
16594 if (wrel != rel)
16595 {
16596 Elf_Internal_Shdr *rel_hdr;
16597 size_t deleted = rel - wrel;
16598
16599 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16600 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16601 if (rel_hdr->sh_size == 0)
16602 {
16603 /* It is too late to remove an empty reloc section. Leave
16604 one NONE reloc.
16605 ??? What is wrong with an empty section??? */
16606 rel_hdr->sh_size = rel_hdr->sh_entsize;
16607 deleted -= 1;
16608 }
16609 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16610 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16611 input_section->reloc_count -= deleted;
16612 }
16613
16614 /* If we're emitting relocations, then shortly after this function
16615 returns, reloc offsets and addends for this section will be
16616 adjusted. Worse, reloc symbol indices will be for the output
16617 file rather than the input. Save a copy of the relocs for
16618 opd_entry_value. */
16619 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16620 {
16621 bfd_size_type amt;
16622 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16623 rel = bfd_alloc (input_bfd, amt);
16624 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16625 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16626 if (rel == NULL)
16627 return FALSE;
16628 memcpy (rel, relocs, amt);
16629 }
16630 return ret;
16631 }
16632
16633 /* Adjust the value of any local symbols in opd sections. */
16634
16635 static int
16636 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16637 const char *name ATTRIBUTE_UNUSED,
16638 Elf_Internal_Sym *elfsym,
16639 asection *input_sec,
16640 struct elf_link_hash_entry *h)
16641 {
16642 struct _opd_sec_data *opd;
16643 long adjust;
16644 bfd_vma value;
16645
16646 if (h != NULL)
16647 return 1;
16648
16649 opd = get_opd_info (input_sec);
16650 if (opd == NULL || opd->adjust == NULL)
16651 return 1;
16652
16653 value = elfsym->st_value - input_sec->output_offset;
16654 if (!bfd_link_relocatable (info))
16655 value -= input_sec->output_section->vma;
16656
16657 adjust = opd->adjust[OPD_NDX (value)];
16658 if (adjust == -1)
16659 return 2;
16660
16661 elfsym->st_value += adjust;
16662 return 1;
16663 }
16664
16665 /* Finish up dynamic symbol handling. We set the contents of various
16666 dynamic sections here. */
16667
16668 static bfd_boolean
16669 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16670 struct bfd_link_info *info,
16671 struct elf_link_hash_entry *h,
16672 Elf_Internal_Sym *sym)
16673 {
16674 struct ppc_link_hash_table *htab;
16675 struct plt_entry *ent;
16676
16677 htab = ppc_hash_table (info);
16678 if (htab == NULL)
16679 return FALSE;
16680
16681 if (!htab->opd_abi && !h->def_regular)
16682 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16683 if (ent->plt.offset != (bfd_vma) -1)
16684 {
16685 /* Mark the symbol as undefined, rather than as
16686 defined in glink. Leave the value if there were
16687 any relocations where pointer equality matters
16688 (this is a clue for the dynamic linker, to make
16689 function pointer comparisons work between an
16690 application and shared library), otherwise set it
16691 to zero. */
16692 sym->st_shndx = SHN_UNDEF;
16693 if (!h->pointer_equality_needed)
16694 sym->st_value = 0;
16695 else if (!h->ref_regular_nonweak)
16696 {
16697 /* This breaks function pointer comparisons, but
16698 that is better than breaking tests for a NULL
16699 function pointer. */
16700 sym->st_value = 0;
16701 }
16702 break;
16703 }
16704
16705 if (h->needs_copy)
16706 {
16707 /* This symbol needs a copy reloc. Set it up. */
16708 Elf_Internal_Rela rela;
16709 asection *srel;
16710 bfd_byte *loc;
16711
16712 if (h->dynindx == -1
16713 || (h->root.type != bfd_link_hash_defined
16714 && h->root.type != bfd_link_hash_defweak)
16715 || htab->elf.srelbss == NULL
16716 || htab->elf.sreldynrelro == NULL)
16717 abort ();
16718
16719 rela.r_offset = (h->root.u.def.value
16720 + h->root.u.def.section->output_section->vma
16721 + h->root.u.def.section->output_offset);
16722 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16723 rela.r_addend = 0;
16724 if (h->root.u.def.section == htab->elf.sdynrelro)
16725 srel = htab->elf.sreldynrelro;
16726 else
16727 srel = htab->elf.srelbss;
16728 loc = srel->contents;
16729 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16730 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16731 }
16732
16733 return TRUE;
16734 }
16735
16736 /* Used to decide how to sort relocs in an optimal manner for the
16737 dynamic linker, before writing them out. */
16738
16739 static enum elf_reloc_type_class
16740 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16741 const asection *rel_sec,
16742 const Elf_Internal_Rela *rela)
16743 {
16744 enum elf_ppc64_reloc_type r_type;
16745 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16746
16747 if (rel_sec == htab->elf.irelplt)
16748 return reloc_class_ifunc;
16749
16750 r_type = ELF64_R_TYPE (rela->r_info);
16751 switch (r_type)
16752 {
16753 case R_PPC64_RELATIVE:
16754 return reloc_class_relative;
16755 case R_PPC64_JMP_SLOT:
16756 return reloc_class_plt;
16757 case R_PPC64_COPY:
16758 return reloc_class_copy;
16759 default:
16760 return reloc_class_normal;
16761 }
16762 }
16763
16764 /* Finish up the dynamic sections. */
16765
16766 static bfd_boolean
16767 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16768 struct bfd_link_info *info)
16769 {
16770 struct ppc_link_hash_table *htab;
16771 bfd *dynobj;
16772 asection *sdyn;
16773
16774 htab = ppc_hash_table (info);
16775 if (htab == NULL)
16776 return FALSE;
16777
16778 dynobj = htab->elf.dynobj;
16779 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16780
16781 if (htab->elf.dynamic_sections_created)
16782 {
16783 Elf64_External_Dyn *dyncon, *dynconend;
16784
16785 if (sdyn == NULL || htab->elf.sgot == NULL)
16786 abort ();
16787
16788 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16789 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16790 for (; dyncon < dynconend; dyncon++)
16791 {
16792 Elf_Internal_Dyn dyn;
16793 asection *s;
16794
16795 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16796
16797 switch (dyn.d_tag)
16798 {
16799 default:
16800 continue;
16801
16802 case DT_PPC64_GLINK:
16803 s = htab->glink;
16804 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16805 /* We stupidly defined DT_PPC64_GLINK to be the start
16806 of glink rather than the first entry point, which is
16807 what ld.so needs, and now have a bigger stub to
16808 support automatic multiple TOCs. */
16809 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16810 break;
16811
16812 case DT_PPC64_OPD:
16813 s = bfd_get_section_by_name (output_bfd, ".opd");
16814 if (s == NULL)
16815 continue;
16816 dyn.d_un.d_ptr = s->vma;
16817 break;
16818
16819 case DT_PPC64_OPT:
16820 if (htab->do_multi_toc && htab->multi_toc_needed)
16821 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16822 if (htab->has_plt_localentry0)
16823 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16824 break;
16825
16826 case DT_PPC64_OPDSZ:
16827 s = bfd_get_section_by_name (output_bfd, ".opd");
16828 if (s == NULL)
16829 continue;
16830 dyn.d_un.d_val = s->size;
16831 break;
16832
16833 case DT_PLTGOT:
16834 s = htab->elf.splt;
16835 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16836 break;
16837
16838 case DT_JMPREL:
16839 s = htab->elf.srelplt;
16840 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16841 break;
16842
16843 case DT_PLTRELSZ:
16844 dyn.d_un.d_val = htab->elf.srelplt->size;
16845 break;
16846
16847 case DT_TEXTREL:
16848 if (htab->local_ifunc_resolver)
16849 info->callbacks->einfo
16850 (_("%X%P: text relocations and GNU indirect "
16851 "functions will result in a segfault at runtime\n"));
16852 else if (htab->maybe_local_ifunc_resolver)
16853 info->callbacks->einfo
16854 (_("%P: warning: text relocations and GNU indirect "
16855 "functions may result in a segfault at runtime\n"));
16856 continue;
16857 }
16858
16859 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16860 }
16861 }
16862
16863 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16864 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16865 {
16866 /* Fill in the first entry in the global offset table.
16867 We use it to hold the link-time TOCbase. */
16868 bfd_put_64 (output_bfd,
16869 elf_gp (output_bfd) + TOC_BASE_OFF,
16870 htab->elf.sgot->contents);
16871
16872 /* Set .got entry size. */
16873 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16874 }
16875
16876 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16877 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16878 {
16879 /* Set .plt entry size. */
16880 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16881 = PLT_ENTRY_SIZE (htab);
16882 }
16883
16884 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16885 brlt ourselves if emitrelocations. */
16886 if (htab->brlt != NULL
16887 && htab->brlt->reloc_count != 0
16888 && !_bfd_elf_link_output_relocs (output_bfd,
16889 htab->brlt,
16890 elf_section_data (htab->brlt)->rela.hdr,
16891 elf_section_data (htab->brlt)->relocs,
16892 NULL))
16893 return FALSE;
16894
16895 if (htab->glink != NULL
16896 && htab->glink->reloc_count != 0
16897 && !_bfd_elf_link_output_relocs (output_bfd,
16898 htab->glink,
16899 elf_section_data (htab->glink)->rela.hdr,
16900 elf_section_data (htab->glink)->relocs,
16901 NULL))
16902 return FALSE;
16903
16904
16905 if (htab->glink_eh_frame != NULL
16906 && htab->glink_eh_frame->size != 0
16907 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16908 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16909 htab->glink_eh_frame,
16910 htab->glink_eh_frame->contents))
16911 return FALSE;
16912
16913 /* We need to handle writing out multiple GOT sections ourselves,
16914 since we didn't add them to DYNOBJ. We know dynobj is the first
16915 bfd. */
16916 while ((dynobj = dynobj->link.next) != NULL)
16917 {
16918 asection *s;
16919
16920 if (!is_ppc64_elf (dynobj))
16921 continue;
16922
16923 s = ppc64_elf_tdata (dynobj)->got;
16924 if (s != NULL
16925 && s->size != 0
16926 && s->output_section != bfd_abs_section_ptr
16927 && !bfd_set_section_contents (output_bfd, s->output_section,
16928 s->contents, s->output_offset,
16929 s->size))
16930 return FALSE;
16931 s = ppc64_elf_tdata (dynobj)->relgot;
16932 if (s != NULL
16933 && s->size != 0
16934 && s->output_section != bfd_abs_section_ptr
16935 && !bfd_set_section_contents (output_bfd, s->output_section,
16936 s->contents, s->output_offset,
16937 s->size))
16938 return FALSE;
16939 }
16940
16941 return TRUE;
16942 }
16943
16944 #include "elf64-target.h"
16945
16946 /* FreeBSD support */
16947
16948 #undef TARGET_LITTLE_SYM
16949 #undef TARGET_LITTLE_NAME
16950
16951 #undef TARGET_BIG_SYM
16952 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16953 #undef TARGET_BIG_NAME
16954 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16955
16956 #undef ELF_OSABI
16957 #define ELF_OSABI ELFOSABI_FREEBSD
16958
16959 #undef elf64_bed
16960 #define elf64_bed elf64_powerpc_fbsd_bed
16961
16962 #include "elf64-target.h"
This page took 0.400847 seconds and 4 git commands to generate.