]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf64-ppc.c
Correct elf64-ppc.c linkage stub comment and formatting fixes
[thirdparty/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,
2306 (int) code);
2307 bfd_set_error (bfd_error_bad_value);
2308 return NULL;
2309
2310 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2311 break;
2312 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2313 break;
2314 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2315 break;
2316 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2317 break;
2318 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2319 break;
2320 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2321 break;
2322 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2323 break;
2324 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2325 break;
2326 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2327 break;
2328 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2329 break;
2330 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2331 break;
2332 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2333 break;
2334 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2335 break;
2336 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
2337 break;
2338 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2339 break;
2340 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2341 break;
2342 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2343 break;
2344 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2345 break;
2346 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2347 break;
2348 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2349 break;
2350 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2351 break;
2352 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2353 break;
2354 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2355 break;
2356 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2357 break;
2358 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2359 break;
2360 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2361 break;
2362 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2363 break;
2364 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2365 break;
2366 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2367 break;
2368 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2369 break;
2370 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2371 break;
2372 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2373 break;
2374 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2375 break;
2376 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2377 break;
2378 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2379 break;
2380 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2381 break;
2382 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2383 break;
2384 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2385 break;
2386 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2387 break;
2388 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2389 break;
2390 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2391 break;
2392 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2393 break;
2394 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2395 break;
2396 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2397 break;
2398 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2399 break;
2400 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2401 break;
2402 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2403 break;
2404 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2405 break;
2406 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2407 break;
2408 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2409 break;
2410 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2411 break;
2412 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2413 break;
2414 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2415 break;
2416 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2417 break;
2418 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2419 break;
2420 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2421 break;
2422 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2423 break;
2424 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2425 break;
2426 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2427 break;
2428 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2429 break;
2430 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2431 break;
2432 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2433 break;
2434 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2435 break;
2436 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2437 break;
2438 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2439 break;
2440 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2441 break;
2442 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2443 break;
2444 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2445 break;
2446 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2447 break;
2448 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2449 break;
2450 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2451 break;
2452 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2453 break;
2454 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2455 break;
2456 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2457 break;
2458 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2459 break;
2460 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2461 break;
2462 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2463 break;
2464 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2465 break;
2466 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2467 break;
2468 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2469 break;
2470 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2471 break;
2472 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2473 break;
2474 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2475 break;
2476 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2477 break;
2478 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2479 break;
2480 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2481 break;
2482 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2483 break;
2484 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2485 break;
2486 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2487 break;
2488 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2489 break;
2490 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2491 break;
2492 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2493 break;
2494 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2495 break;
2496 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2497 break;
2498 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2499 break;
2500 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2501 break;
2502 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2503 break;
2504 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2505 break;
2506 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2507 break;
2508 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2509 break;
2510 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2511 break;
2512 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2513 break;
2514 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2515 break;
2516 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2517 break;
2518 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2519 break;
2520 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2521 break;
2522 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2523 break;
2524 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2525 break;
2526 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2527 break;
2528 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2529 break;
2530 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2531 break;
2532 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2533 break;
2534 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2535 break;
2536 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2537 break;
2538 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2539 break;
2540 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2541 break;
2542 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2543 break;
2544 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2545 break;
2546 }
2547
2548 return ppc64_elf_howto_table[r];
2549 };
2550
2551 static reloc_howto_type *
2552 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2553 const char *r_name)
2554 {
2555 unsigned int i;
2556
2557 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2558 if (ppc64_elf_howto_raw[i].name != NULL
2559 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2560 return &ppc64_elf_howto_raw[i];
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 {
2955 /* A copy of relocs before they are modified for --emit-relocs. */
2956 Elf_Internal_Rela *relocs;
2957
2958 /* Section contents. */
2959 bfd_byte *contents;
2960 } opd;
2961
2962 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2963 the reloc to be in the range -32768 to 32767. */
2964 unsigned int has_small_toc_reloc : 1;
2965
2966 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2967 instruction not one we handle. */
2968 unsigned int unexpected_toc_insn : 1;
2969 };
2970
2971 #define ppc64_elf_tdata(bfd) \
2972 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2973
2974 #define ppc64_tlsld_got(bfd) \
2975 (&ppc64_elf_tdata (bfd)->tlsld_got)
2976
2977 #define is_ppc64_elf(bfd) \
2978 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2979 && elf_object_id (bfd) == PPC64_ELF_DATA)
2980
2981 /* Override the generic function because we store some extras. */
2982
2983 static bfd_boolean
2984 ppc64_elf_mkobject (bfd *abfd)
2985 {
2986 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2987 PPC64_ELF_DATA);
2988 }
2989
2990 /* Fix bad default arch selected for a 64 bit input bfd when the
2991 default is 32 bit. Also select arch based on apuinfo. */
2992
2993 static bfd_boolean
2994 ppc64_elf_object_p (bfd *abfd)
2995 {
2996 if (!abfd->arch_info->the_default)
2997 return TRUE;
2998
2999 if (abfd->arch_info->bits_per_word == 32)
3000 {
3001 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
3002
3003 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
3004 {
3005 /* Relies on arch after 32 bit default being 64 bit default. */
3006 abfd->arch_info = abfd->arch_info->next;
3007 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
3008 }
3009 }
3010 return _bfd_elf_ppc_set_arch (abfd);
3011 }
3012
3013 /* Support for core dump NOTE sections. */
3014
3015 static bfd_boolean
3016 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3017 {
3018 size_t offset, size;
3019
3020 if (note->descsz != 504)
3021 return FALSE;
3022
3023 /* pr_cursig */
3024 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3025
3026 /* pr_pid */
3027 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3028
3029 /* pr_reg */
3030 offset = 112;
3031 size = 384;
3032
3033 /* Make a ".reg/999" section. */
3034 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3035 size, note->descpos + offset);
3036 }
3037
3038 static bfd_boolean
3039 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3040 {
3041 if (note->descsz != 136)
3042 return FALSE;
3043
3044 elf_tdata (abfd)->core->pid
3045 = bfd_get_32 (abfd, note->descdata + 24);
3046 elf_tdata (abfd)->core->program
3047 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3048 elf_tdata (abfd)->core->command
3049 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3050
3051 return TRUE;
3052 }
3053
3054 static char *
3055 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3056 ...)
3057 {
3058 switch (note_type)
3059 {
3060 default:
3061 return NULL;
3062
3063 case NT_PRPSINFO:
3064 {
3065 char data[136] ATTRIBUTE_NONSTRING;
3066 va_list ap;
3067
3068 va_start (ap, note_type);
3069 memset (data, 0, sizeof (data));
3070 strncpy (data + 40, va_arg (ap, const char *), 16);
3071 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3072 DIAGNOSTIC_PUSH;
3073 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
3074 -Wstringop-truncation:
3075 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3076 */
3077 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3078 #endif
3079 strncpy (data + 56, va_arg (ap, const char *), 80);
3080 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3081 DIAGNOSTIC_POP;
3082 #endif
3083 va_end (ap);
3084 return elfcore_write_note (abfd, buf, bufsiz,
3085 "CORE", note_type, data, sizeof (data));
3086 }
3087
3088 case NT_PRSTATUS:
3089 {
3090 char data[504];
3091 va_list ap;
3092 long pid;
3093 int cursig;
3094 const void *greg;
3095
3096 va_start (ap, note_type);
3097 memset (data, 0, 112);
3098 pid = va_arg (ap, long);
3099 bfd_put_32 (abfd, pid, data + 32);
3100 cursig = va_arg (ap, int);
3101 bfd_put_16 (abfd, cursig, data + 12);
3102 greg = va_arg (ap, const void *);
3103 memcpy (data + 112, greg, 384);
3104 memset (data + 496, 0, 8);
3105 va_end (ap);
3106 return elfcore_write_note (abfd, buf, bufsiz,
3107 "CORE", note_type, data, sizeof (data));
3108 }
3109 }
3110 }
3111
3112 /* Add extra PPC sections. */
3113
3114 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
3115 {
3116 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3117 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3118 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3119 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3120 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3121 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3122 { NULL, 0, 0, 0, 0 }
3123 };
3124
3125 enum _ppc64_sec_type {
3126 sec_normal = 0,
3127 sec_opd = 1,
3128 sec_toc = 2
3129 };
3130
3131 struct _ppc64_elf_section_data
3132 {
3133 struct bfd_elf_section_data elf;
3134
3135 union
3136 {
3137 /* An array with one entry for each opd function descriptor,
3138 and some spares since opd entries may be either 16 or 24 bytes. */
3139 #define OPD_NDX(OFF) ((OFF) >> 4)
3140 struct _opd_sec_data
3141 {
3142 /* Points to the function code section for local opd entries. */
3143 asection **func_sec;
3144
3145 /* After editing .opd, adjust references to opd local syms. */
3146 long *adjust;
3147 } opd;
3148
3149 /* An array for toc sections, indexed by offset/8. */
3150 struct _toc_sec_data
3151 {
3152 /* Specifies the relocation symbol index used at a given toc offset. */
3153 unsigned *symndx;
3154
3155 /* And the relocation addend. */
3156 bfd_vma *add;
3157 } toc;
3158 } u;
3159
3160 enum _ppc64_sec_type sec_type:2;
3161
3162 /* Flag set when small branches are detected. Used to
3163 select suitable defaults for the stub group size. */
3164 unsigned int has_14bit_branch:1;
3165
3166 /* Flag set when PLTCALL relocs are detected. */
3167 unsigned int has_pltcall:1;
3168 };
3169
3170 #define ppc64_elf_section_data(sec) \
3171 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3172
3173 static bfd_boolean
3174 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3175 {
3176 if (!sec->used_by_bfd)
3177 {
3178 struct _ppc64_elf_section_data *sdata;
3179 bfd_size_type amt = sizeof (*sdata);
3180
3181 sdata = bfd_zalloc (abfd, amt);
3182 if (sdata == NULL)
3183 return FALSE;
3184 sec->used_by_bfd = sdata;
3185 }
3186
3187 return _bfd_elf_new_section_hook (abfd, sec);
3188 }
3189
3190 static struct _opd_sec_data *
3191 get_opd_info (asection * sec)
3192 {
3193 if (sec != NULL
3194 && ppc64_elf_section_data (sec) != NULL
3195 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3196 return &ppc64_elf_section_data (sec)->u.opd;
3197 return NULL;
3198 }
3199 \f
3200 /* Parameters for the qsort hook. */
3201 static bfd_boolean synthetic_relocatable;
3202 static asection *synthetic_opd;
3203
3204 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3205
3206 static int
3207 compare_symbols (const void *ap, const void *bp)
3208 {
3209 const asymbol *a = *(const asymbol **) ap;
3210 const asymbol *b = *(const asymbol **) bp;
3211
3212 /* Section symbols first. */
3213 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3214 return -1;
3215 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3216 return 1;
3217
3218 /* then .opd symbols. */
3219 if (synthetic_opd != NULL)
3220 {
3221 if (strcmp (a->section->name, ".opd") == 0
3222 && strcmp (b->section->name, ".opd") != 0)
3223 return -1;
3224 if (strcmp (a->section->name, ".opd") != 0
3225 && strcmp (b->section->name, ".opd") == 0)
3226 return 1;
3227 }
3228
3229 /* then other code symbols. */
3230 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3231 == (SEC_CODE | SEC_ALLOC))
3232 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3233 != (SEC_CODE | SEC_ALLOC)))
3234 return -1;
3235
3236 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3237 != (SEC_CODE | SEC_ALLOC))
3238 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3239 == (SEC_CODE | SEC_ALLOC)))
3240 return 1;
3241
3242 if (synthetic_relocatable)
3243 {
3244 if (a->section->id < b->section->id)
3245 return -1;
3246
3247 if (a->section->id > b->section->id)
3248 return 1;
3249 }
3250
3251 if (a->value + a->section->vma < b->value + b->section->vma)
3252 return -1;
3253
3254 if (a->value + a->section->vma > b->value + b->section->vma)
3255 return 1;
3256
3257 /* For syms with the same value, prefer strong dynamic global function
3258 syms over other syms. */
3259 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3260 return -1;
3261
3262 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3263 return 1;
3264
3265 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3266 return -1;
3267
3268 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3269 return 1;
3270
3271 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3272 return -1;
3273
3274 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3275 return 1;
3276
3277 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3278 return -1;
3279
3280 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3281 return 1;
3282
3283 return a > b;
3284 }
3285
3286 /* Search SYMS for a symbol of the given VALUE. */
3287
3288 static asymbol *
3289 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3290 {
3291 long mid;
3292
3293 if (id == (unsigned) -1)
3294 {
3295 while (lo < hi)
3296 {
3297 mid = (lo + hi) >> 1;
3298 if (syms[mid]->value + syms[mid]->section->vma < value)
3299 lo = mid + 1;
3300 else if (syms[mid]->value + syms[mid]->section->vma > value)
3301 hi = mid;
3302 else
3303 return syms[mid];
3304 }
3305 }
3306 else
3307 {
3308 while (lo < hi)
3309 {
3310 mid = (lo + hi) >> 1;
3311 if (syms[mid]->section->id < id)
3312 lo = mid + 1;
3313 else if (syms[mid]->section->id > id)
3314 hi = mid;
3315 else if (syms[mid]->value < value)
3316 lo = mid + 1;
3317 else if (syms[mid]->value > value)
3318 hi = mid;
3319 else
3320 return syms[mid];
3321 }
3322 }
3323 return NULL;
3324 }
3325
3326 static bfd_boolean
3327 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3328 {
3329 bfd_vma vma = *(bfd_vma *) ptr;
3330 return ((section->flags & SEC_ALLOC) != 0
3331 && section->vma <= vma
3332 && vma < section->vma + section->size);
3333 }
3334
3335 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3336 entry syms. Also generate @plt symbols for the glink branch table.
3337 Returns count of synthetic symbols in RET or -1 on error. */
3338
3339 static long
3340 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3341 long static_count, asymbol **static_syms,
3342 long dyn_count, asymbol **dyn_syms,
3343 asymbol **ret)
3344 {
3345 asymbol *s;
3346 size_t i, j, count;
3347 char *names;
3348 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3349 asection *opd = NULL;
3350 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3351 asymbol **syms;
3352 int abi = abiversion (abfd);
3353
3354 *ret = NULL;
3355
3356 if (abi < 2)
3357 {
3358 opd = bfd_get_section_by_name (abfd, ".opd");
3359 if (opd == NULL && abi == 1)
3360 return 0;
3361 }
3362
3363 syms = NULL;
3364 codesecsym = 0;
3365 codesecsymend = 0;
3366 secsymend = 0;
3367 opdsymend = 0;
3368 symcount = 0;
3369 if (opd != NULL)
3370 {
3371 symcount = static_count;
3372 if (!relocatable)
3373 symcount += dyn_count;
3374 if (symcount == 0)
3375 return 0;
3376
3377 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3378 if (syms == NULL)
3379 return -1;
3380
3381 if (!relocatable && static_count != 0 && dyn_count != 0)
3382 {
3383 /* Use both symbol tables. */
3384 memcpy (syms, static_syms, static_count * sizeof (*syms));
3385 memcpy (syms + static_count, dyn_syms,
3386 (dyn_count + 1) * sizeof (*syms));
3387 }
3388 else if (!relocatable && static_count == 0)
3389 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3390 else
3391 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3392
3393 /* Trim uninteresting symbols. Interesting symbols are section,
3394 function, and notype symbols. */
3395 for (i = 0, j = 0; i < symcount; ++i)
3396 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3397 | BSF_RELC | BSF_SRELC)) == 0)
3398 syms[j++] = syms[i];
3399 symcount = j;
3400
3401 synthetic_relocatable = relocatable;
3402 synthetic_opd = opd;
3403 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3404
3405 if (!relocatable && symcount > 1)
3406 {
3407 /* Trim duplicate syms, since we may have merged the normal
3408 and dynamic symbols. Actually, we only care about syms
3409 that have different values, so trim any with the same
3410 value. Don't consider ifunc and ifunc resolver symbols
3411 duplicates however, because GDB wants to know whether a
3412 text symbol is an ifunc resolver. */
3413 for (i = 1, j = 1; i < symcount; ++i)
3414 {
3415 const asymbol *s0 = syms[i - 1];
3416 const asymbol *s1 = syms[i];
3417
3418 if ((s0->value + s0->section->vma
3419 != s1->value + s1->section->vma)
3420 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3421 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3422 syms[j++] = syms[i];
3423 }
3424 symcount = j;
3425 }
3426
3427 i = 0;
3428 /* Note that here and in compare_symbols we can't compare opd and
3429 sym->section directly. With separate debug info files, the
3430 symbols will be extracted from the debug file while abfd passed
3431 to this function is the real binary. */
3432 if (strcmp (syms[i]->section->name, ".opd") == 0)
3433 ++i;
3434 codesecsym = i;
3435
3436 for (; i < symcount; ++i)
3437 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3438 | SEC_THREAD_LOCAL))
3439 != (SEC_CODE | SEC_ALLOC))
3440 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3441 break;
3442 codesecsymend = i;
3443
3444 for (; i < symcount; ++i)
3445 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3446 break;
3447 secsymend = i;
3448
3449 for (; i < symcount; ++i)
3450 if (strcmp (syms[i]->section->name, ".opd") != 0)
3451 break;
3452 opdsymend = i;
3453
3454 for (; i < symcount; ++i)
3455 if (((syms[i]->section->flags
3456 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
3457 != (SEC_CODE | SEC_ALLOC))
3458 break;
3459 symcount = i;
3460 }
3461 count = 0;
3462
3463 if (relocatable)
3464 {
3465 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3466 arelent *r;
3467 size_t size;
3468 size_t relcount;
3469
3470 if (opdsymend == secsymend)
3471 goto done;
3472
3473 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3474 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3475 if (relcount == 0)
3476 goto done;
3477
3478 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3479 {
3480 count = -1;
3481 goto done;
3482 }
3483
3484 size = 0;
3485 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3486 {
3487 asymbol *sym;
3488
3489 while (r < opd->relocation + relcount
3490 && r->address < syms[i]->value + opd->vma)
3491 ++r;
3492
3493 if (r == opd->relocation + relcount)
3494 break;
3495
3496 if (r->address != syms[i]->value + opd->vma)
3497 continue;
3498
3499 if (r->howto->type != R_PPC64_ADDR64)
3500 continue;
3501
3502 sym = *r->sym_ptr_ptr;
3503 if (!sym_exists_at (syms, opdsymend, symcount,
3504 sym->section->id, sym->value + r->addend))
3505 {
3506 ++count;
3507 size += sizeof (asymbol);
3508 size += strlen (syms[i]->name) + 2;
3509 }
3510 }
3511
3512 if (size == 0)
3513 goto done;
3514 s = *ret = bfd_malloc (size);
3515 if (s == NULL)
3516 {
3517 count = -1;
3518 goto done;
3519 }
3520
3521 names = (char *) (s + count);
3522
3523 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3524 {
3525 asymbol *sym;
3526
3527 while (r < opd->relocation + relcount
3528 && r->address < syms[i]->value + opd->vma)
3529 ++r;
3530
3531 if (r == opd->relocation + relcount)
3532 break;
3533
3534 if (r->address != syms[i]->value + opd->vma)
3535 continue;
3536
3537 if (r->howto->type != R_PPC64_ADDR64)
3538 continue;
3539
3540 sym = *r->sym_ptr_ptr;
3541 if (!sym_exists_at (syms, opdsymend, symcount,
3542 sym->section->id, sym->value + r->addend))
3543 {
3544 size_t len;
3545
3546 *s = *syms[i];
3547 s->flags |= BSF_SYNTHETIC;
3548 s->section = sym->section;
3549 s->value = sym->value + r->addend;
3550 s->name = names;
3551 *names++ = '.';
3552 len = strlen (syms[i]->name);
3553 memcpy (names, syms[i]->name, len + 1);
3554 names += len + 1;
3555 /* Have udata.p point back to the original symbol this
3556 synthetic symbol was derived from. */
3557 s->udata.p = syms[i];
3558 s++;
3559 }
3560 }
3561 }
3562 else
3563 {
3564 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3565 bfd_byte *contents = NULL;
3566 size_t size;
3567 size_t plt_count = 0;
3568 bfd_vma glink_vma = 0, resolv_vma = 0;
3569 asection *dynamic, *glink = NULL, *relplt = NULL;
3570 arelent *p;
3571
3572 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3573 {
3574 free_contents_and_exit_err:
3575 count = -1;
3576 free_contents_and_exit:
3577 if (contents)
3578 free (contents);
3579 goto done;
3580 }
3581
3582 size = 0;
3583 for (i = secsymend; i < opdsymend; ++i)
3584 {
3585 bfd_vma ent;
3586
3587 /* Ignore bogus symbols. */
3588 if (syms[i]->value > opd->size - 8)
3589 continue;
3590
3591 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3592 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3593 {
3594 ++count;
3595 size += sizeof (asymbol);
3596 size += strlen (syms[i]->name) + 2;
3597 }
3598 }
3599
3600 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3601 if (dyn_count != 0
3602 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3603 {
3604 bfd_byte *dynbuf, *extdyn, *extdynend;
3605 size_t extdynsize;
3606 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3607
3608 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3609 goto free_contents_and_exit_err;
3610
3611 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3612 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3613
3614 extdyn = dynbuf;
3615 extdynend = extdyn + dynamic->size;
3616 for (; extdyn < extdynend; extdyn += extdynsize)
3617 {
3618 Elf_Internal_Dyn dyn;
3619 (*swap_dyn_in) (abfd, extdyn, &dyn);
3620
3621 if (dyn.d_tag == DT_NULL)
3622 break;
3623
3624 if (dyn.d_tag == DT_PPC64_GLINK)
3625 {
3626 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3627 See comment in ppc64_elf_finish_dynamic_sections. */
3628 glink_vma = dyn.d_un.d_val + 8 * 4;
3629 /* The .glink section usually does not survive the final
3630 link; search for the section (usually .text) where the
3631 glink stubs now reside. */
3632 glink = bfd_sections_find_if (abfd, section_covers_vma,
3633 &glink_vma);
3634 break;
3635 }
3636 }
3637
3638 free (dynbuf);
3639 }
3640
3641 if (glink != NULL)
3642 {
3643 /* Determine __glink trampoline by reading the relative branch
3644 from the first glink stub. */
3645 bfd_byte buf[4];
3646 unsigned int off = 0;
3647
3648 while (bfd_get_section_contents (abfd, glink, buf,
3649 glink_vma + off - glink->vma, 4))
3650 {
3651 unsigned int insn = bfd_get_32 (abfd, buf);
3652 insn ^= B_DOT;
3653 if ((insn & ~0x3fffffc) == 0)
3654 {
3655 resolv_vma
3656 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3657 break;
3658 }
3659 off += 4;
3660 if (off > 4)
3661 break;
3662 }
3663
3664 if (resolv_vma)
3665 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3666
3667 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3668 if (relplt != NULL)
3669 {
3670 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3671 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3672 goto free_contents_and_exit_err;
3673
3674 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3675 size += plt_count * sizeof (asymbol);
3676
3677 p = relplt->relocation;
3678 for (i = 0; i < plt_count; i++, p++)
3679 {
3680 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3681 if (p->addend != 0)
3682 size += sizeof ("+0x") - 1 + 16;
3683 }
3684 }
3685 }
3686
3687 if (size == 0)
3688 goto free_contents_and_exit;
3689 s = *ret = bfd_malloc (size);
3690 if (s == NULL)
3691 goto free_contents_and_exit_err;
3692
3693 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3694
3695 for (i = secsymend; i < opdsymend; ++i)
3696 {
3697 bfd_vma ent;
3698
3699 if (syms[i]->value > opd->size - 8)
3700 continue;
3701
3702 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3703 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3704 {
3705 size_t lo, hi;
3706 size_t len;
3707 asection *sec = abfd->sections;
3708
3709 *s = *syms[i];
3710 lo = codesecsym;
3711 hi = codesecsymend;
3712 while (lo < hi)
3713 {
3714 size_t mid = (lo + hi) >> 1;
3715 if (syms[mid]->section->vma < ent)
3716 lo = mid + 1;
3717 else if (syms[mid]->section->vma > ent)
3718 hi = mid;
3719 else
3720 {
3721 sec = syms[mid]->section;
3722 break;
3723 }
3724 }
3725
3726 if (lo >= hi && lo > codesecsym)
3727 sec = syms[lo - 1]->section;
3728
3729 for (; sec != NULL; sec = sec->next)
3730 {
3731 if (sec->vma > ent)
3732 break;
3733 /* SEC_LOAD may not be set if SEC is from a separate debug
3734 info file. */
3735 if ((sec->flags & SEC_ALLOC) == 0)
3736 break;
3737 if ((sec->flags & SEC_CODE) != 0)
3738 s->section = sec;
3739 }
3740 s->flags |= BSF_SYNTHETIC;
3741 s->value = ent - s->section->vma;
3742 s->name = names;
3743 *names++ = '.';
3744 len = strlen (syms[i]->name);
3745 memcpy (names, syms[i]->name, len + 1);
3746 names += len + 1;
3747 /* Have udata.p point back to the original symbol this
3748 synthetic symbol was derived from. */
3749 s->udata.p = syms[i];
3750 s++;
3751 }
3752 }
3753 free (contents);
3754
3755 if (glink != NULL && relplt != NULL)
3756 {
3757 if (resolv_vma)
3758 {
3759 /* Add a symbol for the main glink trampoline. */
3760 memset (s, 0, sizeof *s);
3761 s->the_bfd = abfd;
3762 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3763 s->section = glink;
3764 s->value = resolv_vma - glink->vma;
3765 s->name = names;
3766 memcpy (names, "__glink_PLTresolve",
3767 sizeof ("__glink_PLTresolve"));
3768 names += sizeof ("__glink_PLTresolve");
3769 s++;
3770 count++;
3771 }
3772
3773 /* FIXME: It would be very much nicer to put sym@plt on the
3774 stub rather than on the glink branch table entry. The
3775 objdump disassembler would then use a sensible symbol
3776 name on plt calls. The difficulty in doing so is
3777 a) finding the stubs, and,
3778 b) matching stubs against plt entries, and,
3779 c) there can be multiple stubs for a given plt entry.
3780
3781 Solving (a) could be done by code scanning, but older
3782 ppc64 binaries used different stubs to current code.
3783 (b) is the tricky one since you need to known the toc
3784 pointer for at least one function that uses a pic stub to
3785 be able to calculate the plt address referenced.
3786 (c) means gdb would need to set multiple breakpoints (or
3787 find the glink branch itself) when setting breakpoints
3788 for pending shared library loads. */
3789 p = relplt->relocation;
3790 for (i = 0; i < plt_count; i++, p++)
3791 {
3792 size_t len;
3793
3794 *s = **p->sym_ptr_ptr;
3795 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3796 we are defining a symbol, ensure one of them is set. */
3797 if ((s->flags & BSF_LOCAL) == 0)
3798 s->flags |= BSF_GLOBAL;
3799 s->flags |= BSF_SYNTHETIC;
3800 s->section = glink;
3801 s->value = glink_vma - glink->vma;
3802 s->name = names;
3803 s->udata.p = NULL;
3804 len = strlen ((*p->sym_ptr_ptr)->name);
3805 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3806 names += len;
3807 if (p->addend != 0)
3808 {
3809 memcpy (names, "+0x", sizeof ("+0x") - 1);
3810 names += sizeof ("+0x") - 1;
3811 bfd_sprintf_vma (abfd, names, p->addend);
3812 names += strlen (names);
3813 }
3814 memcpy (names, "@plt", sizeof ("@plt"));
3815 names += sizeof ("@plt");
3816 s++;
3817 if (abi < 2)
3818 {
3819 glink_vma += 8;
3820 if (i >= 0x8000)
3821 glink_vma += 4;
3822 }
3823 else
3824 glink_vma += 4;
3825 }
3826 count += plt_count;
3827 }
3828 }
3829
3830 done:
3831 free (syms);
3832 return count;
3833 }
3834 \f
3835 /* The following functions are specific to the ELF linker, while
3836 functions above are used generally. Those named ppc64_elf_* are
3837 called by the main ELF linker code. They appear in this file more
3838 or less in the order in which they are called. eg.
3839 ppc64_elf_check_relocs is called early in the link process,
3840 ppc64_elf_finish_dynamic_sections is one of the last functions
3841 called.
3842
3843 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3844 functions have both a function code symbol and a function descriptor
3845 symbol. A call to foo in a relocatable object file looks like:
3846
3847 . .text
3848 . x:
3849 . bl .foo
3850 . nop
3851
3852 The function definition in another object file might be:
3853
3854 . .section .opd
3855 . foo: .quad .foo
3856 . .quad .TOC.@tocbase
3857 . .quad 0
3858 .
3859 . .text
3860 . .foo: blr
3861
3862 When the linker resolves the call during a static link, the branch
3863 unsurprisingly just goes to .foo and the .opd information is unused.
3864 If the function definition is in a shared library, things are a little
3865 different: The call goes via a plt call stub, the opd information gets
3866 copied to the plt, and the linker patches the nop.
3867
3868 . x:
3869 . bl .foo_stub
3870 . ld 2,40(1)
3871 .
3872 .
3873 . .foo_stub:
3874 . std 2,40(1) # in practice, the call stub
3875 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3876 . addi 11,11,Lfoo@toc@l # this is the general idea
3877 . ld 12,0(11)
3878 . ld 2,8(11)
3879 . mtctr 12
3880 . ld 11,16(11)
3881 . bctr
3882 .
3883 . .section .plt
3884 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3885
3886 The "reloc ()" notation is supposed to indicate that the linker emits
3887 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3888 copying.
3889
3890 What are the difficulties here? Well, firstly, the relocations
3891 examined by the linker in check_relocs are against the function code
3892 sym .foo, while the dynamic relocation in the plt is emitted against
3893 the function descriptor symbol, foo. Somewhere along the line, we need
3894 to carefully copy dynamic link information from one symbol to the other.
3895 Secondly, the generic part of the elf linker will make .foo a dynamic
3896 symbol as is normal for most other backends. We need foo dynamic
3897 instead, at least for an application final link. However, when
3898 creating a shared library containing foo, we need to have both symbols
3899 dynamic so that references to .foo are satisfied during the early
3900 stages of linking. Otherwise the linker might decide to pull in a
3901 definition from some other object, eg. a static library.
3902
3903 Update: As of August 2004, we support a new convention. Function
3904 calls may use the function descriptor symbol, ie. "bl foo". This
3905 behaves exactly as "bl .foo". */
3906
3907 /* Of those relocs that might be copied as dynamic relocs, this
3908 function selects those that must be copied when linking a shared
3909 library or PIE, even when the symbol is local. */
3910
3911 static int
3912 must_be_dyn_reloc (struct bfd_link_info *info,
3913 enum elf_ppc64_reloc_type r_type)
3914 {
3915 switch (r_type)
3916 {
3917 default:
3918 /* Only relative relocs can be resolved when the object load
3919 address isn't fixed. DTPREL64 is excluded because the
3920 dynamic linker needs to differentiate global dynamic from
3921 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3922 return 1;
3923
3924 case R_PPC64_REL32:
3925 case R_PPC64_REL64:
3926 case R_PPC64_REL30:
3927 return 0;
3928
3929 case R_PPC64_TPREL16:
3930 case R_PPC64_TPREL16_LO:
3931 case R_PPC64_TPREL16_HI:
3932 case R_PPC64_TPREL16_HA:
3933 case R_PPC64_TPREL16_DS:
3934 case R_PPC64_TPREL16_LO_DS:
3935 case R_PPC64_TPREL16_HIGH:
3936 case R_PPC64_TPREL16_HIGHA:
3937 case R_PPC64_TPREL16_HIGHER:
3938 case R_PPC64_TPREL16_HIGHERA:
3939 case R_PPC64_TPREL16_HIGHEST:
3940 case R_PPC64_TPREL16_HIGHESTA:
3941 case R_PPC64_TPREL64:
3942 /* These relocations are relative but in a shared library the
3943 linker doesn't know the thread pointer base. */
3944 return bfd_link_dll (info);
3945 }
3946 }
3947
3948 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3949 copying dynamic variables from a shared lib into an app's dynbss
3950 section, and instead use a dynamic relocation to point into the
3951 shared lib. With code that gcc generates, it's vital that this be
3952 enabled; In the PowerPC64 ABI, the address of a function is actually
3953 the address of a function descriptor, which resides in the .opd
3954 section. gcc uses the descriptor directly rather than going via the
3955 GOT as some other ABI's do, which means that initialized function
3956 pointers must reference the descriptor. Thus, a function pointer
3957 initialized to the address of a function in a shared library will
3958 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3959 redefines the function descriptor symbol to point to the copy. This
3960 presents a problem as a plt entry for that function is also
3961 initialized from the function descriptor symbol and the copy reloc
3962 may not be initialized first. */
3963 #define ELIMINATE_COPY_RELOCS 1
3964
3965 /* Section name for stubs is the associated section name plus this
3966 string. */
3967 #define STUB_SUFFIX ".stub"
3968
3969 /* Linker stubs.
3970 ppc_stub_long_branch:
3971 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3972 destination, but a 24 bit branch in a stub section will reach.
3973 . b dest
3974
3975 ppc_stub_plt_branch:
3976 Similar to the above, but a 24 bit branch in the stub section won't
3977 reach its destination.
3978 . addis %r11,%r2,xxx@toc@ha
3979 . ld %r12,xxx@toc@l(%r11)
3980 . mtctr %r12
3981 . bctr
3982
3983 ppc_stub_plt_call:
3984 Used to call a function in a shared library. If it so happens that
3985 the plt entry referenced crosses a 64k boundary, then an extra
3986 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3987 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
3988 . addis %r11,%r2,xxx@toc@ha
3989 . ld %r12,xxx+0@toc@l(%r11)
3990 . mtctr %r12
3991 . ld %r2,xxx+8@toc@l(%r11)
3992 . ld %r11,xxx+16@toc@l(%r11)
3993 . bctr
3994
3995 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3996 code to adjust the value and save r2 to support multiple toc sections.
3997 A ppc_stub_long_branch with an r2 offset looks like:
3998 . std %r2,40(%r1)
3999 . addis %r2,%r2,off@ha
4000 . addi %r2,%r2,off@l
4001 . b dest
4002
4003 A ppc_stub_plt_branch with an r2 offset looks like:
4004 . std %r2,40(%r1)
4005 . addis %r11,%r2,xxx@toc@ha
4006 . ld %r12,xxx@toc@l(%r11)
4007 . addis %r2,%r2,off@ha
4008 . addi %r2,%r2,off@l
4009 . mtctr %r12
4010 . bctr
4011
4012 All of the above stubs are shown as their ELFv1 variants. ELFv2
4013 variants exist too, simpler for plt calls since a new toc pointer
4014 and static chain are not loaded by the stub. In addition, ELFv2
4015 has some more complex stubs to handle calls marked with NOTOC
4016 relocs from functions where r2 is not a valid toc pointer. These
4017 come in two flavours, the ones shown below, and _both variants that
4018 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
4019 one call is from a function where r2 is used as the toc pointer but
4020 needs a toc adjusting stub for small-model multi-toc, and another
4021 call is from a function where r2 is not valid.
4022 ppc_stub_long_branch_notoc:
4023 . mflr %r12
4024 . bcl 20,31,1f
4025 . 1:
4026 . mflr %r11
4027 . mtlr %r12
4028 . addis %r12,%r11,dest-1b@ha
4029 . addi %r12,%r12,dest-1b@l
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 {
4088 ppc_stub_none,
4089 ppc_stub_long_branch,
4090 ppc_stub_long_branch_r2off,
4091 ppc_stub_long_branch_notoc,
4092 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
4093 ppc_stub_plt_branch,
4094 ppc_stub_plt_branch_r2off,
4095 ppc_stub_plt_branch_notoc,
4096 ppc_stub_plt_branch_both,
4097 ppc_stub_plt_call,
4098 ppc_stub_plt_call_r2save,
4099 ppc_stub_plt_call_notoc,
4100 ppc_stub_plt_call_both,
4101 ppc_stub_global_entry,
4102 ppc_stub_save_res
4103 };
4104
4105 /* Information on stub grouping. */
4106 struct map_stub
4107 {
4108 /* The stub section. */
4109 asection *stub_sec;
4110 /* This is the section to which stubs in the group will be attached. */
4111 asection *link_sec;
4112 /* Next group. */
4113 struct map_stub *next;
4114 /* Whether to emit a copy of register save/restore functions in this
4115 group. */
4116 int needs_save_res;
4117 /* Current offset within stubs after the insn restoring lr in a
4118 _notoc or _both stub using bcl for pc-relative addressing, or
4119 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
4120 unsigned int lr_restore;
4121 /* Accumulated size of EH info emitted to describe return address
4122 if stubs modify lr. Does not include 17 byte FDE header. */
4123 unsigned int eh_size;
4124 /* Offset in glink_eh_frame to the start of EH info for this group. */
4125 unsigned int eh_base;
4126 };
4127
4128 struct ppc_stub_hash_entry
4129 {
4130 /* Base hash table entry structure. */
4131 struct bfd_hash_entry root;
4132
4133 enum ppc_stub_type stub_type;
4134
4135 /* Group information. */
4136 struct map_stub *group;
4137
4138 /* Offset within stub_sec of the beginning of this stub. */
4139 bfd_vma stub_offset;
4140
4141 /* Given the symbol's value and its section we can determine its final
4142 value when building the stubs (so the stub knows where to jump. */
4143 bfd_vma target_value;
4144 asection *target_section;
4145
4146 /* The symbol table entry, if any, that this was derived from. */
4147 struct ppc_link_hash_entry *h;
4148 struct plt_entry *plt_ent;
4149
4150 /* Symbol type. */
4151 unsigned char symtype;
4152
4153 /* Symbol st_other. */
4154 unsigned char other;
4155 };
4156
4157 struct ppc_branch_hash_entry
4158 {
4159 /* Base hash table entry structure. */
4160 struct bfd_hash_entry root;
4161
4162 /* Offset within branch lookup table. */
4163 unsigned int offset;
4164
4165 /* Generation marker. */
4166 unsigned int iter;
4167 };
4168
4169 /* Used to track dynamic relocations for local symbols. */
4170 struct ppc_dyn_relocs
4171 {
4172 struct ppc_dyn_relocs *next;
4173
4174 /* The input section of the reloc. */
4175 asection *sec;
4176
4177 /* Total number of relocs copied for the input section. */
4178 unsigned int count : 31;
4179
4180 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4181 unsigned int ifunc : 1;
4182 };
4183
4184 struct ppc_link_hash_entry
4185 {
4186 struct elf_link_hash_entry elf;
4187
4188 union
4189 {
4190 /* A pointer to the most recently used stub hash entry against this
4191 symbol. */
4192 struct ppc_stub_hash_entry *stub_cache;
4193
4194 /* A pointer to the next symbol starting with a '.' */
4195 struct ppc_link_hash_entry *next_dot_sym;
4196 } u;
4197
4198 /* Track dynamic relocs copied for this symbol. */
4199 struct elf_dyn_relocs *dyn_relocs;
4200
4201 /* Link between function code and descriptor symbols. */
4202 struct ppc_link_hash_entry *oh;
4203
4204 /* Flag function code and descriptor symbols. */
4205 unsigned int is_func:1;
4206 unsigned int is_func_descriptor:1;
4207 unsigned int fake:1;
4208
4209 /* Whether global opd/toc sym has been adjusted or not.
4210 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4211 should be set for all globals defined in any opd/toc section. */
4212 unsigned int adjust_done:1;
4213
4214 /* Set if this is an out-of-line register save/restore function,
4215 with non-standard calling convention. */
4216 unsigned int save_res:1;
4217
4218 /* Set if a duplicate symbol with non-zero localentry is detected,
4219 even when the duplicate symbol does not provide a definition. */
4220 unsigned int non_zero_localentry:1;
4221
4222 /* Contexts in which symbol is used in the GOT (or TOC).
4223 Bits are or'd into the mask as the corresponding relocs are
4224 encountered during check_relocs, with TLS_TLS being set when any
4225 of the other TLS bits are set. tls_optimize clears bits when
4226 optimizing to indicate the corresponding GOT entry type is not
4227 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4228 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4229 separate flag rather than setting TPREL just for convenience in
4230 distinguishing the two cases.
4231 These flags are also kept for local symbols. */
4232 #define TLS_TLS 1 /* Any TLS reloc. */
4233 #define TLS_GD 2 /* GD reloc. */
4234 #define TLS_LD 4 /* LD reloc. */
4235 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4236 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4237 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4238 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4239 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4240 unsigned char tls_mask;
4241
4242 /* The above field is also used to mark function symbols. In which
4243 case TLS_TLS will be 0. */
4244 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4245 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4246 #define NON_GOT 256 /* local symbol plt, not stored. */
4247 };
4248
4249 /* ppc64 ELF linker hash table. */
4250
4251 struct ppc_link_hash_table
4252 {
4253 struct elf_link_hash_table elf;
4254
4255 /* The stub hash table. */
4256 struct bfd_hash_table stub_hash_table;
4257
4258 /* Another hash table for plt_branch stubs. */
4259 struct bfd_hash_table branch_hash_table;
4260
4261 /* Hash table for function prologue tocsave. */
4262 htab_t tocsave_htab;
4263
4264 /* Various options and other info passed from the linker. */
4265 struct ppc64_elf_params *params;
4266
4267 /* The size of sec_info below. */
4268 unsigned int sec_info_arr_size;
4269
4270 /* Per-section array of extra section info. Done this way rather
4271 than as part of ppc64_elf_section_data so we have the info for
4272 non-ppc64 sections. */
4273 struct
4274 {
4275 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4276 bfd_vma toc_off;
4277
4278 union
4279 {
4280 /* The section group that this section belongs to. */
4281 struct map_stub *group;
4282 /* A temp section list pointer. */
4283 asection *list;
4284 } u;
4285 } *sec_info;
4286
4287 /* Linked list of groups. */
4288 struct map_stub *group;
4289
4290 /* Temp used when calculating TOC pointers. */
4291 bfd_vma toc_curr;
4292 bfd *toc_bfd;
4293 asection *toc_first_sec;
4294
4295 /* Used when adding symbols. */
4296 struct ppc_link_hash_entry *dot_syms;
4297
4298 /* Shortcuts to get to dynamic linker sections. */
4299 asection *glink;
4300 asection *global_entry;
4301 asection *sfpr;
4302 asection *pltlocal;
4303 asection *relpltlocal;
4304 asection *brlt;
4305 asection *relbrlt;
4306 asection *glink_eh_frame;
4307
4308 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4309 struct ppc_link_hash_entry *tls_get_addr;
4310 struct ppc_link_hash_entry *tls_get_addr_fd;
4311
4312 /* The size of reliplt used by got entry relocs. */
4313 bfd_size_type got_reli_size;
4314
4315 /* Statistics. */
4316 unsigned long stub_count[ppc_stub_global_entry];
4317
4318 /* Number of stubs against global syms. */
4319 unsigned long stub_globals;
4320
4321 /* Set if we're linking code with function descriptors. */
4322 unsigned int opd_abi:1;
4323
4324 /* Support for multiple toc sections. */
4325 unsigned int do_multi_toc:1;
4326 unsigned int multi_toc_needed:1;
4327 unsigned int second_toc_pass:1;
4328 unsigned int do_toc_opt:1;
4329
4330 /* Set if tls optimization is enabled. */
4331 unsigned int do_tls_opt:1;
4332
4333 /* Set if inline plt calls should be converted to direct calls. */
4334 unsigned int can_convert_all_inline_plt:1;
4335
4336 /* Set on error. */
4337 unsigned int stub_error:1;
4338
4339 /* Whether func_desc_adjust needs to be run over symbols. */
4340 unsigned int need_func_desc_adj:1;
4341
4342 /* Whether there exist local gnu indirect function resolvers,
4343 referenced by dynamic relocations. */
4344 unsigned int local_ifunc_resolver:1;
4345 unsigned int maybe_local_ifunc_resolver:1;
4346
4347 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4348 unsigned int has_plt_localentry0:1;
4349
4350 /* Incremented every time we size stubs. */
4351 unsigned int stub_iteration;
4352
4353 /* Small local sym cache. */
4354 struct sym_cache sym_cache;
4355 };
4356
4357 /* Rename some of the generic section flags to better document how they
4358 are used here. */
4359
4360 /* Nonzero if this section has TLS related relocations. */
4361 #define has_tls_reloc sec_flg0
4362
4363 /* Nonzero if this section has an old-style call to __tls_get_addr. */
4364 #define has_tls_get_addr_call sec_flg1
4365
4366 /* Nonzero if this section has any toc or got relocs. */
4367 #define has_toc_reloc sec_flg2
4368
4369 /* Nonzero if this section has a call to another section that uses
4370 the toc or got. */
4371 #define makes_toc_func_call sec_flg3
4372
4373 /* Recursion protection when determining above flag. */
4374 #define call_check_in_progress sec_flg4
4375 #define call_check_done sec_flg5
4376
4377 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4378
4379 #define ppc_hash_table(p) \
4380 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4381 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4382
4383 #define ppc_stub_hash_lookup(table, string, create, copy) \
4384 ((struct ppc_stub_hash_entry *) \
4385 bfd_hash_lookup ((table), (string), (create), (copy)))
4386
4387 #define ppc_branch_hash_lookup(table, string, create, copy) \
4388 ((struct ppc_branch_hash_entry *) \
4389 bfd_hash_lookup ((table), (string), (create), (copy)))
4390
4391 /* Create an entry in the stub hash table. */
4392
4393 static struct bfd_hash_entry *
4394 stub_hash_newfunc (struct bfd_hash_entry *entry,
4395 struct bfd_hash_table *table,
4396 const char *string)
4397 {
4398 /* Allocate the structure if it has not already been allocated by a
4399 subclass. */
4400 if (entry == NULL)
4401 {
4402 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4403 if (entry == NULL)
4404 return entry;
4405 }
4406
4407 /* Call the allocation method of the superclass. */
4408 entry = bfd_hash_newfunc (entry, table, string);
4409 if (entry != NULL)
4410 {
4411 struct ppc_stub_hash_entry *eh;
4412
4413 /* Initialize the local fields. */
4414 eh = (struct ppc_stub_hash_entry *) entry;
4415 eh->stub_type = ppc_stub_none;
4416 eh->group = NULL;
4417 eh->stub_offset = 0;
4418 eh->target_value = 0;
4419 eh->target_section = NULL;
4420 eh->h = NULL;
4421 eh->plt_ent = NULL;
4422 eh->other = 0;
4423 }
4424
4425 return entry;
4426 }
4427
4428 /* Create an entry in the branch hash table. */
4429
4430 static struct bfd_hash_entry *
4431 branch_hash_newfunc (struct bfd_hash_entry *entry,
4432 struct bfd_hash_table *table,
4433 const char *string)
4434 {
4435 /* Allocate the structure if it has not already been allocated by a
4436 subclass. */
4437 if (entry == NULL)
4438 {
4439 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4440 if (entry == NULL)
4441 return entry;
4442 }
4443
4444 /* Call the allocation method of the superclass. */
4445 entry = bfd_hash_newfunc (entry, table, string);
4446 if (entry != NULL)
4447 {
4448 struct ppc_branch_hash_entry *eh;
4449
4450 /* Initialize the local fields. */
4451 eh = (struct ppc_branch_hash_entry *) entry;
4452 eh->offset = 0;
4453 eh->iter = 0;
4454 }
4455
4456 return entry;
4457 }
4458
4459 /* Create an entry in a ppc64 ELF linker hash table. */
4460
4461 static struct bfd_hash_entry *
4462 link_hash_newfunc (struct bfd_hash_entry *entry,
4463 struct bfd_hash_table *table,
4464 const char *string)
4465 {
4466 /* Allocate the structure if it has not already been allocated by a
4467 subclass. */
4468 if (entry == NULL)
4469 {
4470 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4471 if (entry == NULL)
4472 return entry;
4473 }
4474
4475 /* Call the allocation method of the superclass. */
4476 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4477 if (entry != NULL)
4478 {
4479 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4480
4481 memset (&eh->u.stub_cache, 0,
4482 (sizeof (struct ppc_link_hash_entry)
4483 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4484
4485 /* When making function calls, old ABI code references function entry
4486 points (dot symbols), while new ABI code references the function
4487 descriptor symbol. We need to make any combination of reference and
4488 definition work together, without breaking archive linking.
4489
4490 For a defined function "foo" and an undefined call to "bar":
4491 An old object defines "foo" and ".foo", references ".bar" (possibly
4492 "bar" too).
4493 A new object defines "foo" and references "bar".
4494
4495 A new object thus has no problem with its undefined symbols being
4496 satisfied by definitions in an old object. On the other hand, the
4497 old object won't have ".bar" satisfied by a new object.
4498
4499 Keep a list of newly added dot-symbols. */
4500
4501 if (string[0] == '.')
4502 {
4503 struct ppc_link_hash_table *htab;
4504
4505 htab = (struct ppc_link_hash_table *) table;
4506 eh->u.next_dot_sym = htab->dot_syms;
4507 htab->dot_syms = eh;
4508 }
4509 }
4510
4511 return entry;
4512 }
4513
4514 struct tocsave_entry
4515 {
4516 asection *sec;
4517 bfd_vma offset;
4518 };
4519
4520 static hashval_t
4521 tocsave_htab_hash (const void *p)
4522 {
4523 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4524 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4525 }
4526
4527 static int
4528 tocsave_htab_eq (const void *p1, const void *p2)
4529 {
4530 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4531 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4532 return e1->sec == e2->sec && e1->offset == e2->offset;
4533 }
4534
4535 /* Destroy a ppc64 ELF linker hash table. */
4536
4537 static void
4538 ppc64_elf_link_hash_table_free (bfd *obfd)
4539 {
4540 struct ppc_link_hash_table *htab;
4541
4542 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4543 if (htab->tocsave_htab)
4544 htab_delete (htab->tocsave_htab);
4545 bfd_hash_table_free (&htab->branch_hash_table);
4546 bfd_hash_table_free (&htab->stub_hash_table);
4547 _bfd_elf_link_hash_table_free (obfd);
4548 }
4549
4550 /* Create a ppc64 ELF linker hash table. */
4551
4552 static struct bfd_link_hash_table *
4553 ppc64_elf_link_hash_table_create (bfd *abfd)
4554 {
4555 struct ppc_link_hash_table *htab;
4556 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4557
4558 htab = bfd_zmalloc (amt);
4559 if (htab == NULL)
4560 return NULL;
4561
4562 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4563 sizeof (struct ppc_link_hash_entry),
4564 PPC64_ELF_DATA))
4565 {
4566 free (htab);
4567 return NULL;
4568 }
4569
4570 /* Init the stub hash table too. */
4571 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4572 sizeof (struct ppc_stub_hash_entry)))
4573 {
4574 _bfd_elf_link_hash_table_free (abfd);
4575 return NULL;
4576 }
4577
4578 /* And the branch hash table. */
4579 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4580 sizeof (struct ppc_branch_hash_entry)))
4581 {
4582 bfd_hash_table_free (&htab->stub_hash_table);
4583 _bfd_elf_link_hash_table_free (abfd);
4584 return NULL;
4585 }
4586
4587 htab->tocsave_htab = htab_try_create (1024,
4588 tocsave_htab_hash,
4589 tocsave_htab_eq,
4590 NULL);
4591 if (htab->tocsave_htab == NULL)
4592 {
4593 ppc64_elf_link_hash_table_free (abfd);
4594 return NULL;
4595 }
4596 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4597
4598 /* Initializing two fields of the union is just cosmetic. We really
4599 only care about glist, but when compiled on a 32-bit host the
4600 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4601 debugger inspection of these fields look nicer. */
4602 htab->elf.init_got_refcount.refcount = 0;
4603 htab->elf.init_got_refcount.glist = NULL;
4604 htab->elf.init_plt_refcount.refcount = 0;
4605 htab->elf.init_plt_refcount.glist = NULL;
4606 htab->elf.init_got_offset.offset = 0;
4607 htab->elf.init_got_offset.glist = NULL;
4608 htab->elf.init_plt_offset.offset = 0;
4609 htab->elf.init_plt_offset.glist = NULL;
4610
4611 return &htab->elf.root;
4612 }
4613
4614 /* Create sections for linker generated code. */
4615
4616 static bfd_boolean
4617 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4618 {
4619 struct ppc_link_hash_table *htab;
4620 flagword flags;
4621
4622 htab = ppc_hash_table (info);
4623
4624 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4625 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4626 if (htab->params->save_restore_funcs)
4627 {
4628 /* Create .sfpr for code to save and restore fp regs. */
4629 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4630 flags);
4631 if (htab->sfpr == NULL
4632 || !bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4633 return FALSE;
4634 }
4635
4636 if (bfd_link_relocatable (info))
4637 return TRUE;
4638
4639 /* Create .glink for lazy dynamic linking support. */
4640 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4641 flags);
4642 if (htab->glink == NULL
4643 || !bfd_set_section_alignment (dynobj, htab->glink, 3))
4644 return FALSE;
4645
4646 /* The part of .glink used by global entry stubs, separate so that
4647 it can be aligned appropriately without affecting htab->glink. */
4648 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4649 flags);
4650 if (htab->global_entry == NULL
4651 || !bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4652 return FALSE;
4653
4654 if (!info->no_ld_generated_unwind_info)
4655 {
4656 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4657 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4658 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4659 ".eh_frame",
4660 flags);
4661 if (htab->glink_eh_frame == NULL
4662 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4663 return FALSE;
4664 }
4665
4666 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4667 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4668 if (htab->elf.iplt == NULL
4669 || !bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4670 return FALSE;
4671
4672 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4673 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4674 htab->elf.irelplt
4675 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4676 if (htab->elf.irelplt == NULL
4677 || !bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4678 return FALSE;
4679
4680 /* Create branch lookup table for plt_branch stubs. */
4681 flags = (SEC_ALLOC | SEC_LOAD
4682 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4683 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4684 flags);
4685 if (htab->brlt == NULL
4686 || !bfd_set_section_alignment (dynobj, htab->brlt, 3))
4687 return FALSE;
4688
4689 /* Local plt entries, put in .branch_lt but a separate section for
4690 convenience. */
4691 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4692 flags);
4693 if (htab->pltlocal == NULL
4694 || !bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4695 return FALSE;
4696
4697 if (!bfd_link_pic (info))
4698 return TRUE;
4699
4700 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4701 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4702 htab->relbrlt
4703 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4704 if (htab->relbrlt == NULL
4705 || !bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4706 return FALSE;
4707
4708 htab->relpltlocal
4709 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4710 if (htab->relpltlocal == NULL
4711 || !bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4712 return FALSE;
4713
4714 return TRUE;
4715 }
4716
4717 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4718
4719 bfd_boolean
4720 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4721 struct ppc64_elf_params *params)
4722 {
4723 struct ppc_link_hash_table *htab;
4724
4725 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4726
4727 /* Always hook our dynamic sections into the first bfd, which is the
4728 linker created stub bfd. This ensures that the GOT header is at
4729 the start of the output TOC section. */
4730 htab = ppc_hash_table (info);
4731 htab->elf.dynobj = params->stub_bfd;
4732 htab->params = params;
4733
4734 return create_linkage_sections (htab->elf.dynobj, info);
4735 }
4736
4737 /* Build a name for an entry in the stub hash table. */
4738
4739 static char *
4740 ppc_stub_name (const asection *input_section,
4741 const asection *sym_sec,
4742 const struct ppc_link_hash_entry *h,
4743 const Elf_Internal_Rela *rel)
4744 {
4745 char *stub_name;
4746 ssize_t len;
4747
4748 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4749 offsets from a sym as a branch target? In fact, we could
4750 probably assume the addend is always zero. */
4751 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4752
4753 if (h)
4754 {
4755 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4756 stub_name = bfd_malloc (len);
4757 if (stub_name == NULL)
4758 return stub_name;
4759
4760 len = sprintf (stub_name, "%08x.%s+%x",
4761 input_section->id & 0xffffffff,
4762 h->elf.root.root.string,
4763 (int) rel->r_addend & 0xffffffff);
4764 }
4765 else
4766 {
4767 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4768 stub_name = bfd_malloc (len);
4769 if (stub_name == NULL)
4770 return stub_name;
4771
4772 len = sprintf (stub_name, "%08x.%x:%x+%x",
4773 input_section->id & 0xffffffff,
4774 sym_sec->id & 0xffffffff,
4775 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4776 (int) rel->r_addend & 0xffffffff);
4777 }
4778 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4779 stub_name[len - 2] = 0;
4780 return stub_name;
4781 }
4782
4783 /* Look up an entry in the stub hash. Stub entries are cached because
4784 creating the stub name takes a bit of time. */
4785
4786 static struct ppc_stub_hash_entry *
4787 ppc_get_stub_entry (const asection *input_section,
4788 const asection *sym_sec,
4789 struct ppc_link_hash_entry *h,
4790 const Elf_Internal_Rela *rel,
4791 struct ppc_link_hash_table *htab)
4792 {
4793 struct ppc_stub_hash_entry *stub_entry;
4794 struct map_stub *group;
4795
4796 /* If this input section is part of a group of sections sharing one
4797 stub section, then use the id of the first section in the group.
4798 Stub names need to include a section id, as there may well be
4799 more than one stub used to reach say, printf, and we need to
4800 distinguish between them. */
4801 group = htab->sec_info[input_section->id].u.group;
4802 if (group == NULL)
4803 return NULL;
4804
4805 if (h != NULL && h->u.stub_cache != NULL
4806 && h->u.stub_cache->h == h
4807 && h->u.stub_cache->group == group)
4808 {
4809 stub_entry = h->u.stub_cache;
4810 }
4811 else
4812 {
4813 char *stub_name;
4814
4815 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4816 if (stub_name == NULL)
4817 return NULL;
4818
4819 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4820 stub_name, FALSE, FALSE);
4821 if (h != NULL)
4822 h->u.stub_cache = stub_entry;
4823
4824 free (stub_name);
4825 }
4826
4827 return stub_entry;
4828 }
4829
4830 /* Add a new stub entry to the stub hash. Not all fields of the new
4831 stub entry are initialised. */
4832
4833 static struct ppc_stub_hash_entry *
4834 ppc_add_stub (const char *stub_name,
4835 asection *section,
4836 struct bfd_link_info *info)
4837 {
4838 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4839 struct map_stub *group;
4840 asection *link_sec;
4841 asection *stub_sec;
4842 struct ppc_stub_hash_entry *stub_entry;
4843
4844 group = htab->sec_info[section->id].u.group;
4845 link_sec = group->link_sec;
4846 stub_sec = group->stub_sec;
4847 if (stub_sec == NULL)
4848 {
4849 size_t namelen;
4850 bfd_size_type len;
4851 char *s_name;
4852
4853 namelen = strlen (link_sec->name);
4854 len = namelen + sizeof (STUB_SUFFIX);
4855 s_name = bfd_alloc (htab->params->stub_bfd, len);
4856 if (s_name == NULL)
4857 return NULL;
4858
4859 memcpy (s_name, link_sec->name, namelen);
4860 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4861 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4862 if (stub_sec == NULL)
4863 return NULL;
4864 group->stub_sec = stub_sec;
4865 }
4866
4867 /* Enter this entry into the linker stub hash table. */
4868 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4869 TRUE, FALSE);
4870 if (stub_entry == NULL)
4871 {
4872 /* xgettext:c-format */
4873 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4874 section->owner, stub_name);
4875 return NULL;
4876 }
4877
4878 stub_entry->group = group;
4879 stub_entry->stub_offset = 0;
4880 return stub_entry;
4881 }
4882
4883 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4884 not already done. */
4885
4886 static bfd_boolean
4887 create_got_section (bfd *abfd, struct bfd_link_info *info)
4888 {
4889 asection *got, *relgot;
4890 flagword flags;
4891 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4892
4893 if (!is_ppc64_elf (abfd))
4894 return FALSE;
4895 if (htab == NULL)
4896 return FALSE;
4897
4898 if (!htab->elf.sgot
4899 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4900 return FALSE;
4901
4902 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4903 | SEC_LINKER_CREATED);
4904
4905 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4906 if (!got
4907 || !bfd_set_section_alignment (abfd, got, 3))
4908 return FALSE;
4909
4910 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4911 flags | SEC_READONLY);
4912 if (!relgot
4913 || !bfd_set_section_alignment (abfd, relgot, 3))
4914 return FALSE;
4915
4916 ppc64_elf_tdata (abfd)->got = got;
4917 ppc64_elf_tdata (abfd)->relgot = relgot;
4918 return TRUE;
4919 }
4920
4921 /* Follow indirect and warning symbol links. */
4922
4923 static inline struct bfd_link_hash_entry *
4924 follow_link (struct bfd_link_hash_entry *h)
4925 {
4926 while (h->type == bfd_link_hash_indirect
4927 || h->type == bfd_link_hash_warning)
4928 h = h->u.i.link;
4929 return h;
4930 }
4931
4932 static inline struct elf_link_hash_entry *
4933 elf_follow_link (struct elf_link_hash_entry *h)
4934 {
4935 return (struct elf_link_hash_entry *) follow_link (&h->root);
4936 }
4937
4938 static inline struct ppc_link_hash_entry *
4939 ppc_follow_link (struct ppc_link_hash_entry *h)
4940 {
4941 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4942 }
4943
4944 /* Merge PLT info on FROM with that on TO. */
4945
4946 static void
4947 move_plt_plist (struct ppc_link_hash_entry *from,
4948 struct ppc_link_hash_entry *to)
4949 {
4950 if (from->elf.plt.plist != NULL)
4951 {
4952 if (to->elf.plt.plist != NULL)
4953 {
4954 struct plt_entry **entp;
4955 struct plt_entry *ent;
4956
4957 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4958 {
4959 struct plt_entry *dent;
4960
4961 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4962 if (dent->addend == ent->addend)
4963 {
4964 dent->plt.refcount += ent->plt.refcount;
4965 *entp = ent->next;
4966 break;
4967 }
4968 if (dent == NULL)
4969 entp = &ent->next;
4970 }
4971 *entp = to->elf.plt.plist;
4972 }
4973
4974 to->elf.plt.plist = from->elf.plt.plist;
4975 from->elf.plt.plist = NULL;
4976 }
4977 }
4978
4979 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4980
4981 static void
4982 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4983 struct elf_link_hash_entry *dir,
4984 struct elf_link_hash_entry *ind)
4985 {
4986 struct ppc_link_hash_entry *edir, *eind;
4987
4988 edir = (struct ppc_link_hash_entry *) dir;
4989 eind = (struct ppc_link_hash_entry *) ind;
4990
4991 edir->is_func |= eind->is_func;
4992 edir->is_func_descriptor |= eind->is_func_descriptor;
4993 edir->tls_mask |= eind->tls_mask;
4994 if (eind->oh != NULL)
4995 edir->oh = ppc_follow_link (eind->oh);
4996
4997 if (edir->elf.versioned != versioned_hidden)
4998 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4999 edir->elf.ref_regular |= eind->elf.ref_regular;
5000 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
5001 edir->elf.non_got_ref |= eind->elf.non_got_ref;
5002 edir->elf.needs_plt |= eind->elf.needs_plt;
5003 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
5004
5005 /* If we were called to copy over info for a weak sym, don't copy
5006 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
5007 in order to simplify readonly_dynrelocs and save a field in the
5008 symbol hash entry, but that means dyn_relocs can't be used in any
5009 tests about a specific symbol, or affect other symbol flags which
5010 are then tested. */
5011 if (eind->elf.root.type != bfd_link_hash_indirect)
5012 return;
5013
5014 /* Copy over any dynamic relocs we may have on the indirect sym. */
5015 if (eind->dyn_relocs != NULL)
5016 {
5017 if (edir->dyn_relocs != NULL)
5018 {
5019 struct elf_dyn_relocs **pp;
5020 struct elf_dyn_relocs *p;
5021
5022 /* Add reloc counts against the indirect sym to the direct sym
5023 list. Merge any entries against the same section. */
5024 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5025 {
5026 struct elf_dyn_relocs *q;
5027
5028 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5029 if (q->sec == p->sec)
5030 {
5031 q->pc_count += p->pc_count;
5032 q->count += p->count;
5033 *pp = p->next;
5034 break;
5035 }
5036 if (q == NULL)
5037 pp = &p->next;
5038 }
5039 *pp = edir->dyn_relocs;
5040 }
5041
5042 edir->dyn_relocs = eind->dyn_relocs;
5043 eind->dyn_relocs = NULL;
5044 }
5045
5046 /* Copy over got entries that we may have already seen to the
5047 symbol which just became indirect. */
5048 if (eind->elf.got.glist != NULL)
5049 {
5050 if (edir->elf.got.glist != NULL)
5051 {
5052 struct got_entry **entp;
5053 struct got_entry *ent;
5054
5055 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
5056 {
5057 struct got_entry *dent;
5058
5059 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
5060 if (dent->addend == ent->addend
5061 && dent->owner == ent->owner
5062 && dent->tls_type == ent->tls_type)
5063 {
5064 dent->got.refcount += ent->got.refcount;
5065 *entp = ent->next;
5066 break;
5067 }
5068 if (dent == NULL)
5069 entp = &ent->next;
5070 }
5071 *entp = edir->elf.got.glist;
5072 }
5073
5074 edir->elf.got.glist = eind->elf.got.glist;
5075 eind->elf.got.glist = NULL;
5076 }
5077
5078 /* And plt entries. */
5079 move_plt_plist (eind, edir);
5080
5081 if (eind->elf.dynindx != -1)
5082 {
5083 if (edir->elf.dynindx != -1)
5084 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
5085 edir->elf.dynstr_index);
5086 edir->elf.dynindx = eind->elf.dynindx;
5087 edir->elf.dynstr_index = eind->elf.dynstr_index;
5088 eind->elf.dynindx = -1;
5089 eind->elf.dynstr_index = 0;
5090 }
5091 }
5092
5093 /* Find the function descriptor hash entry from the given function code
5094 hash entry FH. Link the entries via their OH fields. */
5095
5096 static struct ppc_link_hash_entry *
5097 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
5098 {
5099 struct ppc_link_hash_entry *fdh = fh->oh;
5100
5101 if (fdh == NULL)
5102 {
5103 const char *fd_name = fh->elf.root.root.string + 1;
5104
5105 fdh = (struct ppc_link_hash_entry *)
5106 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
5107 if (fdh == NULL)
5108 return fdh;
5109
5110 fdh->is_func_descriptor = 1;
5111 fdh->oh = fh;
5112 fh->is_func = 1;
5113 fh->oh = fdh;
5114 }
5115
5116 fdh = ppc_follow_link (fdh);
5117 fdh->is_func_descriptor = 1;
5118 fdh->oh = fh;
5119 return fdh;
5120 }
5121
5122 /* Make a fake function descriptor sym for the undefined code sym FH. */
5123
5124 static struct ppc_link_hash_entry *
5125 make_fdh (struct bfd_link_info *info,
5126 struct ppc_link_hash_entry *fh)
5127 {
5128 bfd *abfd = fh->elf.root.u.undef.abfd;
5129 struct bfd_link_hash_entry *bh = NULL;
5130 struct ppc_link_hash_entry *fdh;
5131 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5132 ? BSF_WEAK
5133 : BSF_GLOBAL);
5134
5135 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5136 fh->elf.root.root.string + 1,
5137 flags, bfd_und_section_ptr, 0,
5138 NULL, FALSE, FALSE, &bh))
5139 return NULL;
5140
5141 fdh = (struct ppc_link_hash_entry *) bh;
5142 fdh->elf.non_elf = 0;
5143 fdh->fake = 1;
5144 fdh->is_func_descriptor = 1;
5145 fdh->oh = fh;
5146 fh->is_func = 1;
5147 fh->oh = fdh;
5148 return fdh;
5149 }
5150
5151 /* Fix function descriptor symbols defined in .opd sections to be
5152 function type. */
5153
5154 static bfd_boolean
5155 ppc64_elf_add_symbol_hook (bfd *ibfd,
5156 struct bfd_link_info *info,
5157 Elf_Internal_Sym *isym,
5158 const char **name,
5159 flagword *flags ATTRIBUTE_UNUSED,
5160 asection **sec,
5161 bfd_vma *value)
5162 {
5163 if (*sec != NULL
5164 && strcmp ((*sec)->name, ".opd") == 0)
5165 {
5166 asection *code_sec;
5167
5168 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5169 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5170 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5171
5172 /* If the symbol is a function defined in .opd, and the function
5173 code is in a discarded group, let it appear to be undefined. */
5174 if (!bfd_link_relocatable (info)
5175 && (*sec)->reloc_count != 0
5176 && opd_entry_value (*sec, *value, &code_sec, NULL,
5177 FALSE) != (bfd_vma) -1
5178 && discarded_section (code_sec))
5179 {
5180 *sec = bfd_und_section_ptr;
5181 isym->st_shndx = SHN_UNDEF;
5182 }
5183 }
5184 else if (*sec != NULL
5185 && strcmp ((*sec)->name, ".toc") == 0
5186 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5187 {
5188 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5189 if (htab != NULL)
5190 htab->params->object_in_toc = 1;
5191 }
5192
5193 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5194 {
5195 if (abiversion (ibfd) == 0)
5196 set_abiversion (ibfd, 2);
5197 else if (abiversion (ibfd) == 1)
5198 {
5199 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5200 " for ABI version 1"), *name);
5201 bfd_set_error (bfd_error_bad_value);
5202 return FALSE;
5203 }
5204 }
5205
5206 return TRUE;
5207 }
5208
5209 /* Merge non-visibility st_other attributes: local entry point. */
5210
5211 static void
5212 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5213 const Elf_Internal_Sym *isym,
5214 bfd_boolean definition,
5215 bfd_boolean dynamic)
5216 {
5217 if (definition && (!dynamic || !h->def_regular))
5218 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5219 | ELF_ST_VISIBILITY (h->other));
5220 }
5221
5222 /* Hook called on merging a symbol. We use this to clear "fake" since
5223 we now have a real symbol. */
5224
5225 static bfd_boolean
5226 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5227 const Elf_Internal_Sym *isym,
5228 asection **psec ATTRIBUTE_UNUSED,
5229 bfd_boolean newdef ATTRIBUTE_UNUSED,
5230 bfd_boolean olddef ATTRIBUTE_UNUSED,
5231 bfd *oldbfd ATTRIBUTE_UNUSED,
5232 const asection *oldsec ATTRIBUTE_UNUSED)
5233 {
5234 ((struct ppc_link_hash_entry *) h)->fake = 0;
5235 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5236 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5237 return TRUE;
5238 }
5239
5240 /* This function makes an old ABI object reference to ".bar" cause the
5241 inclusion of a new ABI object archive that defines "bar".
5242 NAME is a symbol defined in an archive. Return a symbol in the hash
5243 table that might be satisfied by the archive symbols. */
5244
5245 static struct elf_link_hash_entry *
5246 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5247 struct bfd_link_info *info,
5248 const char *name)
5249 {
5250 struct elf_link_hash_entry *h;
5251 char *dot_name;
5252 size_t len;
5253
5254 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5255 if (h != NULL
5256 /* Don't return this sym if it is a fake function descriptor
5257 created by add_symbol_adjust. */
5258 && !((struct ppc_link_hash_entry *) h)->fake)
5259 return h;
5260
5261 if (name[0] == '.')
5262 return h;
5263
5264 len = strlen (name);
5265 dot_name = bfd_alloc (abfd, len + 2);
5266 if (dot_name == NULL)
5267 return (struct elf_link_hash_entry *) -1;
5268 dot_name[0] = '.';
5269 memcpy (dot_name + 1, name, len + 1);
5270 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5271 bfd_release (abfd, dot_name);
5272 return h;
5273 }
5274
5275 /* This function satisfies all old ABI object references to ".bar" if a
5276 new ABI object defines "bar". Well, at least, undefined dot symbols
5277 are made weak. This stops later archive searches from including an
5278 object if we already have a function descriptor definition. It also
5279 prevents the linker complaining about undefined symbols.
5280 We also check and correct mismatched symbol visibility here. The
5281 most restrictive visibility of the function descriptor and the
5282 function entry symbol is used. */
5283
5284 static bfd_boolean
5285 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5286 {
5287 struct ppc_link_hash_table *htab;
5288 struct ppc_link_hash_entry *fdh;
5289
5290 if (eh->elf.root.type == bfd_link_hash_warning)
5291 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5292
5293 if (eh->elf.root.type == bfd_link_hash_indirect)
5294 return TRUE;
5295
5296 if (eh->elf.root.root.string[0] != '.')
5297 abort ();
5298
5299 htab = ppc_hash_table (info);
5300 if (htab == NULL)
5301 return FALSE;
5302
5303 fdh = lookup_fdh (eh, htab);
5304 if (fdh == NULL
5305 && !bfd_link_relocatable (info)
5306 && (eh->elf.root.type == bfd_link_hash_undefined
5307 || eh->elf.root.type == bfd_link_hash_undefweak)
5308 && eh->elf.ref_regular)
5309 {
5310 /* Make an undefined function descriptor sym, in order to
5311 pull in an --as-needed shared lib. Archives are handled
5312 elsewhere. */
5313 fdh = make_fdh (info, eh);
5314 if (fdh == NULL)
5315 return FALSE;
5316 }
5317
5318 if (fdh != NULL)
5319 {
5320 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5321 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5322
5323 /* Make both descriptor and entry symbol have the most
5324 constraining visibility of either symbol. */
5325 if (entry_vis < descr_vis)
5326 fdh->elf.other += entry_vis - descr_vis;
5327 else if (entry_vis > descr_vis)
5328 eh->elf.other += descr_vis - entry_vis;
5329
5330 /* Propagate reference flags from entry symbol to function
5331 descriptor symbol. */
5332 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5333 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5334 fdh->elf.ref_regular |= eh->elf.ref_regular;
5335 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5336
5337 if (!fdh->elf.forced_local
5338 && fdh->elf.dynindx == -1
5339 && fdh->elf.versioned != versioned_hidden
5340 && (bfd_link_dll (info)
5341 || fdh->elf.def_dynamic
5342 || fdh->elf.ref_dynamic)
5343 && (eh->elf.ref_regular
5344 || eh->elf.def_regular))
5345 {
5346 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5347 return FALSE;
5348 }
5349 }
5350
5351 return TRUE;
5352 }
5353
5354 /* Set up opd section info and abiversion for IBFD, and process list
5355 of dot-symbols we made in link_hash_newfunc. */
5356
5357 static bfd_boolean
5358 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5359 {
5360 struct ppc_link_hash_table *htab;
5361 struct ppc_link_hash_entry **p, *eh;
5362 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5363
5364 if (opd != NULL && opd->size != 0)
5365 {
5366 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5367 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5368
5369 if (abiversion (ibfd) == 0)
5370 set_abiversion (ibfd, 1);
5371 else if (abiversion (ibfd) >= 2)
5372 {
5373 /* xgettext:c-format */
5374 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5375 ibfd, abiversion (ibfd));
5376 bfd_set_error (bfd_error_bad_value);
5377 return FALSE;
5378 }
5379 }
5380
5381 if (is_ppc64_elf (info->output_bfd))
5382 {
5383 /* For input files without an explicit abiversion in e_flags
5384 we should have flagged any with symbol st_other bits set
5385 as ELFv1 and above flagged those with .opd as ELFv2.
5386 Set the output abiversion if not yet set, and for any input
5387 still ambiguous, take its abiversion from the output.
5388 Differences in ABI are reported later. */
5389 if (abiversion (info->output_bfd) == 0)
5390 set_abiversion (info->output_bfd, abiversion (ibfd));
5391 else if (abiversion (ibfd) == 0)
5392 set_abiversion (ibfd, abiversion (info->output_bfd));
5393 }
5394
5395 htab = ppc_hash_table (info);
5396 if (htab == NULL)
5397 return TRUE;
5398
5399 if (opd != NULL && opd->size != 0
5400 && (ibfd->flags & DYNAMIC) == 0
5401 && (opd->flags & SEC_RELOC) != 0
5402 && opd->reloc_count != 0
5403 && !bfd_is_abs_section (opd->output_section)
5404 && info->gc_sections)
5405 {
5406 /* Garbage collection needs some extra help with .opd sections.
5407 We don't want to necessarily keep everything referenced by
5408 relocs in .opd, as that would keep all functions. Instead,
5409 if we reference an .opd symbol (a function descriptor), we
5410 want to keep the function code symbol's section. This is
5411 easy for global symbols, but for local syms we need to keep
5412 information about the associated function section. */
5413 bfd_size_type amt;
5414 asection **opd_sym_map;
5415 Elf_Internal_Shdr *symtab_hdr;
5416 Elf_Internal_Rela *relocs, *rel_end, *rel;
5417
5418 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5419 opd_sym_map = bfd_zalloc (ibfd, amt);
5420 if (opd_sym_map == NULL)
5421 return FALSE;
5422 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5423 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5424 info->keep_memory);
5425 if (relocs == NULL)
5426 return FALSE;
5427 symtab_hdr = &elf_symtab_hdr (ibfd);
5428 rel_end = relocs + opd->reloc_count - 1;
5429 for (rel = relocs; rel < rel_end; rel++)
5430 {
5431 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5432 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5433
5434 if (r_type == R_PPC64_ADDR64
5435 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5436 && r_symndx < symtab_hdr->sh_info)
5437 {
5438 Elf_Internal_Sym *isym;
5439 asection *s;
5440
5441 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5442 if (isym == NULL)
5443 {
5444 if (elf_section_data (opd)->relocs != relocs)
5445 free (relocs);
5446 return FALSE;
5447 }
5448
5449 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5450 if (s != NULL && s != opd)
5451 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5452 }
5453 }
5454 if (elf_section_data (opd)->relocs != relocs)
5455 free (relocs);
5456 }
5457
5458 p = &htab->dot_syms;
5459 while ((eh = *p) != NULL)
5460 {
5461 *p = NULL;
5462 if (&eh->elf == htab->elf.hgot)
5463 ;
5464 else if (htab->elf.hgot == NULL
5465 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5466 htab->elf.hgot = &eh->elf;
5467 else if (abiversion (ibfd) <= 1)
5468 {
5469 htab->need_func_desc_adj = 1;
5470 if (!add_symbol_adjust (eh, info))
5471 return FALSE;
5472 }
5473 p = &eh->u.next_dot_sym;
5474 }
5475 return TRUE;
5476 }
5477
5478 /* Undo hash table changes when an --as-needed input file is determined
5479 not to be needed. */
5480
5481 static bfd_boolean
5482 ppc64_elf_notice_as_needed (bfd *ibfd,
5483 struct bfd_link_info *info,
5484 enum notice_asneeded_action act)
5485 {
5486 if (act == notice_not_needed)
5487 {
5488 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5489
5490 if (htab == NULL)
5491 return FALSE;
5492
5493 htab->dot_syms = NULL;
5494 }
5495 return _bfd_elf_notice_as_needed (ibfd, info, act);
5496 }
5497
5498 /* If --just-symbols against a final linked binary, then assume we need
5499 toc adjusting stubs when calling functions defined there. */
5500
5501 static void
5502 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5503 {
5504 if ((sec->flags & SEC_CODE) != 0
5505 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5506 && is_ppc64_elf (sec->owner))
5507 {
5508 if (abiversion (sec->owner) >= 2
5509 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5510 sec->has_toc_reloc = 1;
5511 }
5512 _bfd_elf_link_just_syms (sec, info);
5513 }
5514
5515 static struct plt_entry **
5516 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5517 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5518 {
5519 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5520 struct plt_entry **local_plt;
5521 unsigned char *local_got_tls_masks;
5522
5523 if (local_got_ents == NULL)
5524 {
5525 bfd_size_type size = symtab_hdr->sh_info;
5526
5527 size *= (sizeof (*local_got_ents)
5528 + sizeof (*local_plt)
5529 + sizeof (*local_got_tls_masks));
5530 local_got_ents = bfd_zalloc (abfd, size);
5531 if (local_got_ents == NULL)
5532 return NULL;
5533 elf_local_got_ents (abfd) = local_got_ents;
5534 }
5535
5536 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5537 {
5538 struct got_entry *ent;
5539
5540 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5541 if (ent->addend == r_addend
5542 && ent->owner == abfd
5543 && ent->tls_type == tls_type)
5544 break;
5545 if (ent == NULL)
5546 {
5547 bfd_size_type amt = sizeof (*ent);
5548 ent = bfd_alloc (abfd, amt);
5549 if (ent == NULL)
5550 return FALSE;
5551 ent->next = local_got_ents[r_symndx];
5552 ent->addend = r_addend;
5553 ent->owner = abfd;
5554 ent->tls_type = tls_type;
5555 ent->is_indirect = FALSE;
5556 ent->got.refcount = 0;
5557 local_got_ents[r_symndx] = ent;
5558 }
5559 ent->got.refcount += 1;
5560 }
5561
5562 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5563 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5564 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5565
5566 return local_plt + r_symndx;
5567 }
5568
5569 static bfd_boolean
5570 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5571 {
5572 struct plt_entry *ent;
5573
5574 for (ent = *plist; ent != NULL; ent = ent->next)
5575 if (ent->addend == addend)
5576 break;
5577 if (ent == NULL)
5578 {
5579 bfd_size_type amt = sizeof (*ent);
5580 ent = bfd_alloc (abfd, amt);
5581 if (ent == NULL)
5582 return FALSE;
5583 ent->next = *plist;
5584 ent->addend = addend;
5585 ent->plt.refcount = 0;
5586 *plist = ent;
5587 }
5588 ent->plt.refcount += 1;
5589 return TRUE;
5590 }
5591
5592 static bfd_boolean
5593 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5594 {
5595 return (r_type == R_PPC64_REL24
5596 || r_type == R_PPC64_REL24_NOTOC
5597 || r_type == R_PPC64_REL14
5598 || r_type == R_PPC64_REL14_BRTAKEN
5599 || r_type == R_PPC64_REL14_BRNTAKEN
5600 || r_type == R_PPC64_ADDR24
5601 || r_type == R_PPC64_ADDR14
5602 || r_type == R_PPC64_ADDR14_BRTAKEN
5603 || r_type == R_PPC64_ADDR14_BRNTAKEN
5604 || r_type == R_PPC64_PLTCALL);
5605 }
5606
5607 /* Relocs on inline plt call sequence insns prior to the call. */
5608
5609 static bfd_boolean
5610 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5611 {
5612 return (r_type == R_PPC64_PLT16_HA
5613 || r_type == R_PPC64_PLT16_HI
5614 || r_type == R_PPC64_PLT16_LO
5615 || r_type == R_PPC64_PLT16_LO_DS
5616 || r_type == R_PPC64_PLTSEQ);
5617 }
5618
5619 /* Look through the relocs for a section during the first phase, and
5620 calculate needed space in the global offset table, procedure
5621 linkage table, and dynamic reloc sections. */
5622
5623 static bfd_boolean
5624 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5625 asection *sec, const Elf_Internal_Rela *relocs)
5626 {
5627 struct ppc_link_hash_table *htab;
5628 Elf_Internal_Shdr *symtab_hdr;
5629 struct elf_link_hash_entry **sym_hashes;
5630 const Elf_Internal_Rela *rel;
5631 const Elf_Internal_Rela *rel_end;
5632 asection *sreloc;
5633 struct elf_link_hash_entry *tga, *dottga;
5634 bfd_boolean is_opd;
5635
5636 if (bfd_link_relocatable (info))
5637 return TRUE;
5638
5639 /* Don't do anything special with non-loaded, non-alloced sections.
5640 In particular, any relocs in such sections should not affect GOT
5641 and PLT reference counting (ie. we don't allow them to create GOT
5642 or PLT entries), there's no possibility or desire to optimize TLS
5643 relocs, and there's not much point in propagating relocs to shared
5644 libs that the dynamic linker won't relocate. */
5645 if ((sec->flags & SEC_ALLOC) == 0)
5646 return TRUE;
5647
5648 BFD_ASSERT (is_ppc64_elf (abfd));
5649
5650 htab = ppc_hash_table (info);
5651 if (htab == NULL)
5652 return FALSE;
5653
5654 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5655 FALSE, FALSE, TRUE);
5656 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5657 FALSE, FALSE, TRUE);
5658 symtab_hdr = &elf_symtab_hdr (abfd);
5659 sym_hashes = elf_sym_hashes (abfd);
5660 sreloc = NULL;
5661 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5662 rel_end = relocs + sec->reloc_count;
5663 for (rel = relocs; rel < rel_end; rel++)
5664 {
5665 unsigned long r_symndx;
5666 struct elf_link_hash_entry *h;
5667 enum elf_ppc64_reloc_type r_type;
5668 int tls_type;
5669 struct _ppc64_elf_section_data *ppc64_sec;
5670 struct plt_entry **ifunc, **plt_list;
5671
5672 r_symndx = ELF64_R_SYM (rel->r_info);
5673 if (r_symndx < symtab_hdr->sh_info)
5674 h = NULL;
5675 else
5676 {
5677 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5678 h = elf_follow_link (h);
5679
5680 if (h == htab->elf.hgot)
5681 sec->has_toc_reloc = 1;
5682 }
5683
5684 tls_type = 0;
5685 ifunc = NULL;
5686 if (h != NULL)
5687 {
5688 if (h->type == STT_GNU_IFUNC)
5689 {
5690 h->needs_plt = 1;
5691 ifunc = &h->plt.plist;
5692 }
5693 }
5694 else
5695 {
5696 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5697 abfd, r_symndx);
5698 if (isym == NULL)
5699 return FALSE;
5700
5701 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5702 {
5703 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5704 rel->r_addend,
5705 NON_GOT | PLT_IFUNC);
5706 if (ifunc == NULL)
5707 return FALSE;
5708 }
5709 }
5710
5711 r_type = ELF64_R_TYPE (rel->r_info);
5712 switch (r_type)
5713 {
5714 case R_PPC64_TLSGD:
5715 case R_PPC64_TLSLD:
5716 /* These special tls relocs tie a call to __tls_get_addr with
5717 its parameter symbol. */
5718 if (h != NULL)
5719 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5720 else
5721 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5722 rel->r_addend,
5723 NON_GOT | TLS_TLS | TLS_MARK))
5724 return FALSE;
5725 sec->has_tls_reloc = 1;
5726 break;
5727
5728 case R_PPC64_GOT_TLSLD16:
5729 case R_PPC64_GOT_TLSLD16_LO:
5730 case R_PPC64_GOT_TLSLD16_HI:
5731 case R_PPC64_GOT_TLSLD16_HA:
5732 tls_type = TLS_TLS | TLS_LD;
5733 goto dogottls;
5734
5735 case R_PPC64_GOT_TLSGD16:
5736 case R_PPC64_GOT_TLSGD16_LO:
5737 case R_PPC64_GOT_TLSGD16_HI:
5738 case R_PPC64_GOT_TLSGD16_HA:
5739 tls_type = TLS_TLS | TLS_GD;
5740 goto dogottls;
5741
5742 case R_PPC64_GOT_TPREL16_DS:
5743 case R_PPC64_GOT_TPREL16_LO_DS:
5744 case R_PPC64_GOT_TPREL16_HI:
5745 case R_PPC64_GOT_TPREL16_HA:
5746 if (bfd_link_dll (info))
5747 info->flags |= DF_STATIC_TLS;
5748 tls_type = TLS_TLS | TLS_TPREL;
5749 goto dogottls;
5750
5751 case R_PPC64_GOT_DTPREL16_DS:
5752 case R_PPC64_GOT_DTPREL16_LO_DS:
5753 case R_PPC64_GOT_DTPREL16_HI:
5754 case R_PPC64_GOT_DTPREL16_HA:
5755 tls_type = TLS_TLS | TLS_DTPREL;
5756 dogottls:
5757 sec->has_tls_reloc = 1;
5758 /* Fall through */
5759
5760 case R_PPC64_GOT16:
5761 case R_PPC64_GOT16_DS:
5762 case R_PPC64_GOT16_HA:
5763 case R_PPC64_GOT16_HI:
5764 case R_PPC64_GOT16_LO:
5765 case R_PPC64_GOT16_LO_DS:
5766 /* This symbol requires a global offset table entry. */
5767 sec->has_toc_reloc = 1;
5768 if (r_type == R_PPC64_GOT_TLSLD16
5769 || r_type == R_PPC64_GOT_TLSGD16
5770 || r_type == R_PPC64_GOT_TPREL16_DS
5771 || r_type == R_PPC64_GOT_DTPREL16_DS
5772 || r_type == R_PPC64_GOT16
5773 || r_type == R_PPC64_GOT16_DS)
5774 {
5775 htab->do_multi_toc = 1;
5776 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5777 }
5778
5779 if (ppc64_elf_tdata (abfd)->got == NULL
5780 && !create_got_section (abfd, info))
5781 return FALSE;
5782
5783 if (h != NULL)
5784 {
5785 struct ppc_link_hash_entry *eh;
5786 struct got_entry *ent;
5787
5788 eh = (struct ppc_link_hash_entry *) h;
5789 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5790 if (ent->addend == rel->r_addend
5791 && ent->owner == abfd
5792 && ent->tls_type == tls_type)
5793 break;
5794 if (ent == NULL)
5795 {
5796 bfd_size_type amt = sizeof (*ent);
5797 ent = bfd_alloc (abfd, amt);
5798 if (ent == NULL)
5799 return FALSE;
5800 ent->next = eh->elf.got.glist;
5801 ent->addend = rel->r_addend;
5802 ent->owner = abfd;
5803 ent->tls_type = tls_type;
5804 ent->is_indirect = FALSE;
5805 ent->got.refcount = 0;
5806 eh->elf.got.glist = ent;
5807 }
5808 ent->got.refcount += 1;
5809 eh->tls_mask |= tls_type;
5810 }
5811 else
5812 /* This is a global offset table entry for a local symbol. */
5813 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5814 rel->r_addend, tls_type))
5815 return FALSE;
5816
5817 /* We may also need a plt entry if the symbol turns out to be
5818 an ifunc. */
5819 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5820 {
5821 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5822 return FALSE;
5823 }
5824 break;
5825
5826 case R_PPC64_PLT16_HA:
5827 case R_PPC64_PLT16_HI:
5828 case R_PPC64_PLT16_LO:
5829 case R_PPC64_PLT16_LO_DS:
5830 case R_PPC64_PLT32:
5831 case R_PPC64_PLT64:
5832 /* This symbol requires a procedure linkage table entry. */
5833 plt_list = ifunc;
5834 if (h != NULL)
5835 {
5836 h->needs_plt = 1;
5837 if (h->root.root.string[0] == '.'
5838 && h->root.root.string[1] != '\0')
5839 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5840 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5841 plt_list = &h->plt.plist;
5842 }
5843 if (plt_list == NULL)
5844 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5845 rel->r_addend,
5846 NON_GOT | PLT_KEEP);
5847 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5848 return FALSE;
5849 break;
5850
5851 /* The following relocations don't need to propagate the
5852 relocation if linking a shared object since they are
5853 section relative. */
5854 case R_PPC64_SECTOFF:
5855 case R_PPC64_SECTOFF_LO:
5856 case R_PPC64_SECTOFF_HI:
5857 case R_PPC64_SECTOFF_HA:
5858 case R_PPC64_SECTOFF_DS:
5859 case R_PPC64_SECTOFF_LO_DS:
5860 case R_PPC64_DTPREL16:
5861 case R_PPC64_DTPREL16_LO:
5862 case R_PPC64_DTPREL16_HI:
5863 case R_PPC64_DTPREL16_HA:
5864 case R_PPC64_DTPREL16_DS:
5865 case R_PPC64_DTPREL16_LO_DS:
5866 case R_PPC64_DTPREL16_HIGH:
5867 case R_PPC64_DTPREL16_HIGHA:
5868 case R_PPC64_DTPREL16_HIGHER:
5869 case R_PPC64_DTPREL16_HIGHERA:
5870 case R_PPC64_DTPREL16_HIGHEST:
5871 case R_PPC64_DTPREL16_HIGHESTA:
5872 break;
5873
5874 /* Nor do these. */
5875 case R_PPC64_REL16:
5876 case R_PPC64_REL16_LO:
5877 case R_PPC64_REL16_HI:
5878 case R_PPC64_REL16_HA:
5879 case R_PPC64_REL16DX_HA:
5880 break;
5881
5882 /* Not supported as a dynamic relocation. */
5883 case R_PPC64_ADDR64_LOCAL:
5884 if (bfd_link_pic (info))
5885 {
5886 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5887 ppc_howto_init ();
5888 /* xgettext:c-format */
5889 info->callbacks->einfo (_("%H: %s reloc unsupported "
5890 "in shared libraries and PIEs\n"),
5891 abfd, sec, rel->r_offset,
5892 ppc64_elf_howto_table[r_type]->name);
5893 bfd_set_error (bfd_error_bad_value);
5894 return FALSE;
5895 }
5896 break;
5897
5898 case R_PPC64_TOC16:
5899 case R_PPC64_TOC16_DS:
5900 htab->do_multi_toc = 1;
5901 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5902 /* Fall through. */
5903 case R_PPC64_TOC16_LO:
5904 case R_PPC64_TOC16_HI:
5905 case R_PPC64_TOC16_HA:
5906 case R_PPC64_TOC16_LO_DS:
5907 sec->has_toc_reloc = 1;
5908 break;
5909
5910 /* Marker reloc. */
5911 case R_PPC64_ENTRY:
5912 break;
5913
5914 /* This relocation describes the C++ object vtable hierarchy.
5915 Reconstruct it for later use during GC. */
5916 case R_PPC64_GNU_VTINHERIT:
5917 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5918 return FALSE;
5919 break;
5920
5921 /* This relocation describes which C++ vtable entries are actually
5922 used. Record for later use during GC. */
5923 case R_PPC64_GNU_VTENTRY:
5924 BFD_ASSERT (h != NULL);
5925 if (h != NULL
5926 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5927 return FALSE;
5928 break;
5929
5930 case R_PPC64_REL14:
5931 case R_PPC64_REL14_BRTAKEN:
5932 case R_PPC64_REL14_BRNTAKEN:
5933 {
5934 asection *dest = NULL;
5935
5936 /* Heuristic: If jumping outside our section, chances are
5937 we are going to need a stub. */
5938 if (h != NULL)
5939 {
5940 /* If the sym is weak it may be overridden later, so
5941 don't assume we know where a weak sym lives. */
5942 if (h->root.type == bfd_link_hash_defined)
5943 dest = h->root.u.def.section;
5944 }
5945 else
5946 {
5947 Elf_Internal_Sym *isym;
5948
5949 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5950 abfd, r_symndx);
5951 if (isym == NULL)
5952 return FALSE;
5953
5954 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5955 }
5956
5957 if (dest != sec)
5958 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5959 }
5960 goto rel24;
5961
5962 case R_PPC64_PLTCALL:
5963 ppc64_elf_section_data (sec)->has_pltcall = 1;
5964 /* Fall through. */
5965
5966 case R_PPC64_REL24:
5967 case R_PPC64_REL24_NOTOC:
5968 rel24:
5969 plt_list = ifunc;
5970 if (h != NULL)
5971 {
5972 h->needs_plt = 1;
5973 if (h->root.root.string[0] == '.'
5974 && h->root.root.string[1] != '\0')
5975 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5976
5977 if (h == tga || h == dottga)
5978 {
5979 sec->has_tls_reloc = 1;
5980 if (rel != relocs
5981 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5982 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5983 /* We have a new-style __tls_get_addr call with
5984 a marker reloc. */
5985 ;
5986 else
5987 /* Mark this section as having an old-style call. */
5988 sec->has_tls_get_addr_call = 1;
5989 }
5990 plt_list = &h->plt.plist;
5991 }
5992
5993 /* We may need a .plt entry if the function this reloc
5994 refers to is in a shared lib. */
5995 if (plt_list
5996 && !update_plt_info (abfd, plt_list, rel->r_addend))
5997 return FALSE;
5998 break;
5999
6000 case R_PPC64_ADDR14:
6001 case R_PPC64_ADDR14_BRNTAKEN:
6002 case R_PPC64_ADDR14_BRTAKEN:
6003 case R_PPC64_ADDR24:
6004 goto dodyn;
6005
6006 case R_PPC64_TPREL64:
6007 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
6008 if (bfd_link_dll (info))
6009 info->flags |= DF_STATIC_TLS;
6010 goto dotlstoc;
6011
6012 case R_PPC64_DTPMOD64:
6013 if (rel + 1 < rel_end
6014 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
6015 && rel[1].r_offset == rel->r_offset + 8)
6016 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
6017 else
6018 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
6019 goto dotlstoc;
6020
6021 case R_PPC64_DTPREL64:
6022 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
6023 if (rel != relocs
6024 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
6025 && rel[-1].r_offset == rel->r_offset - 8)
6026 /* This is the second reloc of a dtpmod, dtprel pair.
6027 Don't mark with TLS_DTPREL. */
6028 goto dodyn;
6029
6030 dotlstoc:
6031 sec->has_tls_reloc = 1;
6032 if (h != NULL)
6033 {
6034 struct ppc_link_hash_entry *eh;
6035 eh = (struct ppc_link_hash_entry *) h;
6036 eh->tls_mask |= tls_type;
6037 }
6038 else
6039 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
6040 rel->r_addend, tls_type))
6041 return FALSE;
6042
6043 ppc64_sec = ppc64_elf_section_data (sec);
6044 if (ppc64_sec->sec_type != sec_toc)
6045 {
6046 bfd_size_type amt;
6047
6048 /* One extra to simplify get_tls_mask. */
6049 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
6050 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
6051 if (ppc64_sec->u.toc.symndx == NULL)
6052 return FALSE;
6053 amt = sec->size * sizeof (bfd_vma) / 8;
6054 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
6055 if (ppc64_sec->u.toc.add == NULL)
6056 return FALSE;
6057 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
6058 ppc64_sec->sec_type = sec_toc;
6059 }
6060 BFD_ASSERT (rel->r_offset % 8 == 0);
6061 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
6062 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
6063
6064 /* Mark the second slot of a GD or LD entry.
6065 -1 to indicate GD and -2 to indicate LD. */
6066 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
6067 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
6068 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
6069 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
6070 goto dodyn;
6071
6072 case R_PPC64_TPREL16:
6073 case R_PPC64_TPREL16_LO:
6074 case R_PPC64_TPREL16_HI:
6075 case R_PPC64_TPREL16_HA:
6076 case R_PPC64_TPREL16_DS:
6077 case R_PPC64_TPREL16_LO_DS:
6078 case R_PPC64_TPREL16_HIGH:
6079 case R_PPC64_TPREL16_HIGHA:
6080 case R_PPC64_TPREL16_HIGHER:
6081 case R_PPC64_TPREL16_HIGHERA:
6082 case R_PPC64_TPREL16_HIGHEST:
6083 case R_PPC64_TPREL16_HIGHESTA:
6084 if (bfd_link_dll (info))
6085 info->flags |= DF_STATIC_TLS;
6086 goto dodyn;
6087
6088 case R_PPC64_ADDR64:
6089 if (is_opd
6090 && rel + 1 < rel_end
6091 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
6092 {
6093 if (h != NULL)
6094 ((struct ppc_link_hash_entry *) h)->is_func = 1;
6095 }
6096 /* Fall through. */
6097
6098 case R_PPC64_ADDR16:
6099 case R_PPC64_ADDR16_DS:
6100 case R_PPC64_ADDR16_HA:
6101 case R_PPC64_ADDR16_HI:
6102 case R_PPC64_ADDR16_HIGH:
6103 case R_PPC64_ADDR16_HIGHA:
6104 case R_PPC64_ADDR16_HIGHER:
6105 case R_PPC64_ADDR16_HIGHERA:
6106 case R_PPC64_ADDR16_HIGHEST:
6107 case R_PPC64_ADDR16_HIGHESTA:
6108 case R_PPC64_ADDR16_LO:
6109 case R_PPC64_ADDR16_LO_DS:
6110 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
6111 && rel->r_addend == 0)
6112 {
6113 /* We may need a .plt entry if this reloc refers to a
6114 function in a shared lib. */
6115 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
6116 return FALSE;
6117 h->pointer_equality_needed = 1;
6118 }
6119 /* Fall through. */
6120
6121 case R_PPC64_REL30:
6122 case R_PPC64_REL32:
6123 case R_PPC64_REL64:
6124 case R_PPC64_ADDR32:
6125 case R_PPC64_UADDR16:
6126 case R_PPC64_UADDR32:
6127 case R_PPC64_UADDR64:
6128 case R_PPC64_TOC:
6129 if (h != NULL && !bfd_link_pic (info))
6130 /* We may need a copy reloc. */
6131 h->non_got_ref = 1;
6132
6133 /* Don't propagate .opd relocs. */
6134 if (NO_OPD_RELOCS && is_opd)
6135 break;
6136
6137 /* If we are creating a shared library, and this is a reloc
6138 against a global symbol, or a non PC relative reloc
6139 against a local symbol, then we need to copy the reloc
6140 into the shared library. However, if we are linking with
6141 -Bsymbolic, we do not need to copy a reloc against a
6142 global symbol which is defined in an object we are
6143 including in the link (i.e., DEF_REGULAR is set). At
6144 this point we have not seen all the input files, so it is
6145 possible that DEF_REGULAR is not set now but will be set
6146 later (it is never cleared). In case of a weak definition,
6147 DEF_REGULAR may be cleared later by a strong definition in
6148 a shared library. We account for that possibility below by
6149 storing information in the dyn_relocs field of the hash
6150 table entry. A similar situation occurs when creating
6151 shared libraries and symbol visibility changes render the
6152 symbol local.
6153
6154 If on the other hand, we are creating an executable, we
6155 may need to keep relocations for symbols satisfied by a
6156 dynamic library if we manage to avoid copy relocs for the
6157 symbol. */
6158 dodyn:
6159 if ((bfd_link_pic (info)
6160 && (must_be_dyn_reloc (info, r_type)
6161 || (h != NULL
6162 && (!SYMBOLIC_BIND (info, h)
6163 || h->root.type == bfd_link_hash_defweak
6164 || !h->def_regular))))
6165 || (ELIMINATE_COPY_RELOCS
6166 && !bfd_link_pic (info)
6167 && h != NULL
6168 && (h->root.type == bfd_link_hash_defweak
6169 || !h->def_regular))
6170 || (!bfd_link_pic (info)
6171 && ifunc != NULL))
6172 {
6173 /* We must copy these reloc types into the output file.
6174 Create a reloc section in dynobj and make room for
6175 this reloc. */
6176 if (sreloc == NULL)
6177 {
6178 sreloc = _bfd_elf_make_dynamic_reloc_section
6179 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6180
6181 if (sreloc == NULL)
6182 return FALSE;
6183 }
6184
6185 /* If this is a global symbol, we count the number of
6186 relocations we need for this symbol. */
6187 if (h != NULL)
6188 {
6189 struct elf_dyn_relocs *p;
6190 struct elf_dyn_relocs **head;
6191
6192 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6193 p = *head;
6194 if (p == NULL || p->sec != sec)
6195 {
6196 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6197 if (p == NULL)
6198 return FALSE;
6199 p->next = *head;
6200 *head = p;
6201 p->sec = sec;
6202 p->count = 0;
6203 p->pc_count = 0;
6204 }
6205 p->count += 1;
6206 if (!must_be_dyn_reloc (info, r_type))
6207 p->pc_count += 1;
6208 }
6209 else
6210 {
6211 /* Track dynamic relocs needed for local syms too.
6212 We really need local syms available to do this
6213 easily. Oh well. */
6214 struct ppc_dyn_relocs *p;
6215 struct ppc_dyn_relocs **head;
6216 bfd_boolean is_ifunc;
6217 asection *s;
6218 void *vpp;
6219 Elf_Internal_Sym *isym;
6220
6221 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6222 abfd, r_symndx);
6223 if (isym == NULL)
6224 return FALSE;
6225
6226 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6227 if (s == NULL)
6228 s = sec;
6229
6230 vpp = &elf_section_data (s)->local_dynrel;
6231 head = (struct ppc_dyn_relocs **) vpp;
6232 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6233 p = *head;
6234 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6235 p = p->next;
6236 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6237 {
6238 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6239 if (p == NULL)
6240 return FALSE;
6241 p->next = *head;
6242 *head = p;
6243 p->sec = sec;
6244 p->ifunc = is_ifunc;
6245 p->count = 0;
6246 }
6247 p->count += 1;
6248 }
6249 }
6250 break;
6251
6252 default:
6253 break;
6254 }
6255 }
6256
6257 return TRUE;
6258 }
6259
6260 /* Merge backend specific data from an object file to the output
6261 object file when linking. */
6262
6263 static bfd_boolean
6264 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6265 {
6266 bfd *obfd = info->output_bfd;
6267 unsigned long iflags, oflags;
6268
6269 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6270 return TRUE;
6271
6272 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6273 return TRUE;
6274
6275 if (!_bfd_generic_verify_endian_match (ibfd, info))
6276 return FALSE;
6277
6278 iflags = elf_elfheader (ibfd)->e_flags;
6279 oflags = elf_elfheader (obfd)->e_flags;
6280
6281 if (iflags & ~EF_PPC64_ABI)
6282 {
6283 _bfd_error_handler
6284 /* xgettext:c-format */
6285 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6286 bfd_set_error (bfd_error_bad_value);
6287 return FALSE;
6288 }
6289 else if (iflags != oflags && iflags != 0)
6290 {
6291 _bfd_error_handler
6292 /* xgettext:c-format */
6293 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6294 ibfd, iflags, oflags);
6295 bfd_set_error (bfd_error_bad_value);
6296 return FALSE;
6297 }
6298
6299 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6300 return FALSE;
6301
6302 /* Merge Tag_compatibility attributes and any common GNU ones. */
6303 return _bfd_elf_merge_object_attributes (ibfd, info);
6304 }
6305
6306 static bfd_boolean
6307 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6308 {
6309 /* Print normal ELF private data. */
6310 _bfd_elf_print_private_bfd_data (abfd, ptr);
6311
6312 if (elf_elfheader (abfd)->e_flags != 0)
6313 {
6314 FILE *file = ptr;
6315
6316 fprintf (file, _("private flags = 0x%lx:"),
6317 elf_elfheader (abfd)->e_flags);
6318
6319 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6320 fprintf (file, _(" [abiv%ld]"),
6321 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6322 fputc ('\n', file);
6323 }
6324
6325 return TRUE;
6326 }
6327
6328 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6329 of the code entry point, and its section, which must be in the same
6330 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6331
6332 static bfd_vma
6333 opd_entry_value (asection *opd_sec,
6334 bfd_vma offset,
6335 asection **code_sec,
6336 bfd_vma *code_off,
6337 bfd_boolean in_code_sec)
6338 {
6339 bfd *opd_bfd = opd_sec->owner;
6340 Elf_Internal_Rela *relocs;
6341 Elf_Internal_Rela *lo, *hi, *look;
6342 bfd_vma val;
6343
6344 /* No relocs implies we are linking a --just-symbols object, or looking
6345 at a final linked executable with addr2line or somesuch. */
6346 if (opd_sec->reloc_count == 0)
6347 {
6348 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6349
6350 if (contents == NULL)
6351 {
6352 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6353 return (bfd_vma) -1;
6354 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6355 }
6356
6357 /* PR 17512: file: 64b9dfbb. */
6358 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6359 return (bfd_vma) -1;
6360
6361 val = bfd_get_64 (opd_bfd, contents + offset);
6362 if (code_sec != NULL)
6363 {
6364 asection *sec, *likely = NULL;
6365
6366 if (in_code_sec)
6367 {
6368 sec = *code_sec;
6369 if (sec->vma <= val
6370 && val < sec->vma + sec->size)
6371 likely = sec;
6372 else
6373 val = -1;
6374 }
6375 else
6376 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6377 if (sec->vma <= val
6378 && (sec->flags & SEC_LOAD) != 0
6379 && (sec->flags & SEC_ALLOC) != 0)
6380 likely = sec;
6381 if (likely != NULL)
6382 {
6383 *code_sec = likely;
6384 if (code_off != NULL)
6385 *code_off = val - likely->vma;
6386 }
6387 }
6388 return val;
6389 }
6390
6391 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6392
6393 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6394 if (relocs == NULL)
6395 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6396 /* PR 17512: file: df8e1fd6. */
6397 if (relocs == NULL)
6398 return (bfd_vma) -1;
6399
6400 /* Go find the opd reloc at the sym address. */
6401 lo = relocs;
6402 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6403 val = (bfd_vma) -1;
6404 while (lo < hi)
6405 {
6406 look = lo + (hi - lo) / 2;
6407 if (look->r_offset < offset)
6408 lo = look + 1;
6409 else if (look->r_offset > offset)
6410 hi = look;
6411 else
6412 {
6413 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6414
6415 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6416 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6417 {
6418 unsigned long symndx = ELF64_R_SYM (look->r_info);
6419 asection *sec = NULL;
6420
6421 if (symndx >= symtab_hdr->sh_info
6422 && elf_sym_hashes (opd_bfd) != NULL)
6423 {
6424 struct elf_link_hash_entry **sym_hashes;
6425 struct elf_link_hash_entry *rh;
6426
6427 sym_hashes = elf_sym_hashes (opd_bfd);
6428 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6429 if (rh != NULL)
6430 {
6431 rh = elf_follow_link (rh);
6432 if (rh->root.type != bfd_link_hash_defined
6433 && rh->root.type != bfd_link_hash_defweak)
6434 break;
6435 if (rh->root.u.def.section->owner == opd_bfd)
6436 {
6437 val = rh->root.u.def.value;
6438 sec = rh->root.u.def.section;
6439 }
6440 }
6441 }
6442
6443 if (sec == NULL)
6444 {
6445 Elf_Internal_Sym *sym;
6446
6447 if (symndx < symtab_hdr->sh_info)
6448 {
6449 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6450 if (sym == NULL)
6451 {
6452 size_t symcnt = symtab_hdr->sh_info;
6453 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6454 symcnt, 0,
6455 NULL, NULL, NULL);
6456 if (sym == NULL)
6457 break;
6458 symtab_hdr->contents = (bfd_byte *) sym;
6459 }
6460 sym += symndx;
6461 }
6462 else
6463 {
6464 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6465 1, symndx,
6466 NULL, NULL, NULL);
6467 if (sym == NULL)
6468 break;
6469 }
6470 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6471 if (sec == NULL)
6472 break;
6473 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6474 val = sym->st_value;
6475 }
6476
6477 val += look->r_addend;
6478 if (code_off != NULL)
6479 *code_off = val;
6480 if (code_sec != NULL)
6481 {
6482 if (in_code_sec && *code_sec != sec)
6483 return -1;
6484 else
6485 *code_sec = sec;
6486 }
6487 if (sec->output_section != NULL)
6488 val += sec->output_section->vma + sec->output_offset;
6489 }
6490 break;
6491 }
6492 }
6493
6494 return val;
6495 }
6496
6497 /* If the ELF symbol SYM might be a function in SEC, return the
6498 function size and set *CODE_OFF to the function's entry point,
6499 otherwise return zero. */
6500
6501 static bfd_size_type
6502 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6503 bfd_vma *code_off)
6504 {
6505 bfd_size_type size;
6506
6507 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6508 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6509 return 0;
6510
6511 size = 0;
6512 if (!(sym->flags & BSF_SYNTHETIC))
6513 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6514
6515 if (strcmp (sym->section->name, ".opd") == 0)
6516 {
6517 struct _opd_sec_data *opd = get_opd_info (sym->section);
6518 bfd_vma symval = sym->value;
6519
6520 if (opd != NULL
6521 && opd->adjust != NULL
6522 && elf_section_data (sym->section)->relocs != NULL)
6523 {
6524 /* opd_entry_value will use cached relocs that have been
6525 adjusted, but with raw symbols. That means both local
6526 and global symbols need adjusting. */
6527 long adjust = opd->adjust[OPD_NDX (symval)];
6528 if (adjust == -1)
6529 return 0;
6530 symval += adjust;
6531 }
6532
6533 if (opd_entry_value (sym->section, symval,
6534 &sec, code_off, TRUE) == (bfd_vma) -1)
6535 return 0;
6536 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6537 symbol. This size has nothing to do with the code size of the
6538 function, which is what we're supposed to return, but the
6539 code size isn't available without looking up the dot-sym.
6540 However, doing that would be a waste of time particularly
6541 since elf_find_function will look at the dot-sym anyway.
6542 Now, elf_find_function will keep the largest size of any
6543 function sym found at the code address of interest, so return
6544 1 here to avoid it incorrectly caching a larger function size
6545 for a small function. This does mean we return the wrong
6546 size for a new-ABI function of size 24, but all that does is
6547 disable caching for such functions. */
6548 if (size == 24)
6549 size = 1;
6550 }
6551 else
6552 {
6553 if (sym->section != sec)
6554 return 0;
6555 *code_off = sym->value;
6556 }
6557 if (size == 0)
6558 size = 1;
6559 return size;
6560 }
6561
6562 /* Return true if symbol is a strong function defined in an ELFv2
6563 object with st_other localentry bits of zero, ie. its local entry
6564 point coincides with its global entry point. */
6565
6566 static bfd_boolean
6567 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6568 {
6569 return (h != NULL
6570 && h->type == STT_FUNC
6571 && h->root.type == bfd_link_hash_defined
6572 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6573 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6574 && is_ppc64_elf (h->root.u.def.section->owner)
6575 && abiversion (h->root.u.def.section->owner) >= 2);
6576 }
6577
6578 /* Return true if symbol is defined in a regular object file. */
6579
6580 static bfd_boolean
6581 is_static_defined (struct elf_link_hash_entry *h)
6582 {
6583 return ((h->root.type == bfd_link_hash_defined
6584 || h->root.type == bfd_link_hash_defweak)
6585 && h->root.u.def.section != NULL
6586 && h->root.u.def.section->output_section != NULL);
6587 }
6588
6589 /* If FDH is a function descriptor symbol, return the associated code
6590 entry symbol if it is defined. Return NULL otherwise. */
6591
6592 static struct ppc_link_hash_entry *
6593 defined_code_entry (struct ppc_link_hash_entry *fdh)
6594 {
6595 if (fdh->is_func_descriptor)
6596 {
6597 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6598 if (fh->elf.root.type == bfd_link_hash_defined
6599 || fh->elf.root.type == bfd_link_hash_defweak)
6600 return fh;
6601 }
6602 return NULL;
6603 }
6604
6605 /* If FH is a function code entry symbol, return the associated
6606 function descriptor symbol if it is defined. Return NULL otherwise. */
6607
6608 static struct ppc_link_hash_entry *
6609 defined_func_desc (struct ppc_link_hash_entry *fh)
6610 {
6611 if (fh->oh != NULL
6612 && fh->oh->is_func_descriptor)
6613 {
6614 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6615 if (fdh->elf.root.type == bfd_link_hash_defined
6616 || fdh->elf.root.type == bfd_link_hash_defweak)
6617 return fdh;
6618 }
6619 return NULL;
6620 }
6621
6622 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6623
6624 /* Garbage collect sections, after first dealing with dot-symbols. */
6625
6626 static bfd_boolean
6627 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6628 {
6629 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6630
6631 if (htab != NULL && htab->need_func_desc_adj)
6632 {
6633 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6634 htab->need_func_desc_adj = 0;
6635 }
6636 return bfd_elf_gc_sections (abfd, info);
6637 }
6638
6639 /* Mark all our entry sym sections, both opd and code section. */
6640
6641 static void
6642 ppc64_elf_gc_keep (struct bfd_link_info *info)
6643 {
6644 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6645 struct bfd_sym_chain *sym;
6646
6647 if (htab == NULL)
6648 return;
6649
6650 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6651 {
6652 struct ppc_link_hash_entry *eh, *fh;
6653 asection *sec;
6654
6655 eh = (struct ppc_link_hash_entry *)
6656 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6657 if (eh == NULL)
6658 continue;
6659 if (eh->elf.root.type != bfd_link_hash_defined
6660 && eh->elf.root.type != bfd_link_hash_defweak)
6661 continue;
6662
6663 fh = defined_code_entry (eh);
6664 if (fh != NULL)
6665 {
6666 sec = fh->elf.root.u.def.section;
6667 sec->flags |= SEC_KEEP;
6668 }
6669 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6670 && opd_entry_value (eh->elf.root.u.def.section,
6671 eh->elf.root.u.def.value,
6672 &sec, NULL, FALSE) != (bfd_vma) -1)
6673 sec->flags |= SEC_KEEP;
6674
6675 sec = eh->elf.root.u.def.section;
6676 sec->flags |= SEC_KEEP;
6677 }
6678 }
6679
6680 /* Mark sections containing dynamically referenced symbols. When
6681 building shared libraries, we must assume that any visible symbol is
6682 referenced. */
6683
6684 static bfd_boolean
6685 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6686 {
6687 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6688 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6689 struct ppc_link_hash_entry *fdh;
6690 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6691
6692 /* Dynamic linking info is on the func descriptor sym. */
6693 fdh = defined_func_desc (eh);
6694 if (fdh != NULL)
6695 eh = fdh;
6696
6697 if ((eh->elf.root.type == bfd_link_hash_defined
6698 || eh->elf.root.type == bfd_link_hash_defweak)
6699 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6700 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6701 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6702 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6703 && (!bfd_link_executable (info)
6704 || info->gc_keep_exported
6705 || info->export_dynamic
6706 || (eh->elf.dynamic
6707 && d != NULL
6708 && (*d->match) (&d->head, NULL,
6709 eh->elf.root.root.string)))
6710 && (eh->elf.versioned >= versioned
6711 || !bfd_hide_sym_by_version (info->version_info,
6712 eh->elf.root.root.string)))))
6713 {
6714 asection *code_sec;
6715 struct ppc_link_hash_entry *fh;
6716
6717 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6718
6719 /* Function descriptor syms cause the associated
6720 function code sym section to be marked. */
6721 fh = defined_code_entry (eh);
6722 if (fh != NULL)
6723 {
6724 code_sec = fh->elf.root.u.def.section;
6725 code_sec->flags |= SEC_KEEP;
6726 }
6727 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6728 && opd_entry_value (eh->elf.root.u.def.section,
6729 eh->elf.root.u.def.value,
6730 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6731 code_sec->flags |= SEC_KEEP;
6732 }
6733
6734 return TRUE;
6735 }
6736
6737 /* Return the section that should be marked against GC for a given
6738 relocation. */
6739
6740 static asection *
6741 ppc64_elf_gc_mark_hook (asection *sec,
6742 struct bfd_link_info *info,
6743 Elf_Internal_Rela *rel,
6744 struct elf_link_hash_entry *h,
6745 Elf_Internal_Sym *sym)
6746 {
6747 asection *rsec;
6748
6749 /* Syms return NULL if we're marking .opd, so we avoid marking all
6750 function sections, as all functions are referenced in .opd. */
6751 rsec = NULL;
6752 if (get_opd_info (sec) != NULL)
6753 return rsec;
6754
6755 if (h != NULL)
6756 {
6757 enum elf_ppc64_reloc_type r_type;
6758 struct ppc_link_hash_entry *eh, *fh, *fdh;
6759
6760 r_type = ELF64_R_TYPE (rel->r_info);
6761 switch (r_type)
6762 {
6763 case R_PPC64_GNU_VTINHERIT:
6764 case R_PPC64_GNU_VTENTRY:
6765 break;
6766
6767 default:
6768 switch (h->root.type)
6769 {
6770 case bfd_link_hash_defined:
6771 case bfd_link_hash_defweak:
6772 eh = (struct ppc_link_hash_entry *) h;
6773 fdh = defined_func_desc (eh);
6774 if (fdh != NULL)
6775 {
6776 /* -mcall-aixdesc code references the dot-symbol on
6777 a call reloc. Mark the function descriptor too
6778 against garbage collection. */
6779 fdh->elf.mark = 1;
6780 if (fdh->elf.is_weakalias)
6781 weakdef (&fdh->elf)->mark = 1;
6782 eh = fdh;
6783 }
6784
6785 /* Function descriptor syms cause the associated
6786 function code sym section to be marked. */
6787 fh = defined_code_entry (eh);
6788 if (fh != NULL)
6789 {
6790 /* They also mark their opd section. */
6791 eh->elf.root.u.def.section->gc_mark = 1;
6792
6793 rsec = fh->elf.root.u.def.section;
6794 }
6795 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6796 && opd_entry_value (eh->elf.root.u.def.section,
6797 eh->elf.root.u.def.value,
6798 &rsec, NULL, FALSE) != (bfd_vma) -1)
6799 eh->elf.root.u.def.section->gc_mark = 1;
6800 else
6801 rsec = h->root.u.def.section;
6802 break;
6803
6804 case bfd_link_hash_common:
6805 rsec = h->root.u.c.p->section;
6806 break;
6807
6808 default:
6809 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6810 }
6811 }
6812 }
6813 else
6814 {
6815 struct _opd_sec_data *opd;
6816
6817 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6818 opd = get_opd_info (rsec);
6819 if (opd != NULL && opd->func_sec != NULL)
6820 {
6821 rsec->gc_mark = 1;
6822
6823 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6824 }
6825 }
6826
6827 return rsec;
6828 }
6829
6830 /* The maximum size of .sfpr. */
6831 #define SFPR_MAX (218*4)
6832
6833 struct sfpr_def_parms
6834 {
6835 const char name[12];
6836 unsigned char lo, hi;
6837 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6838 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6839 };
6840
6841 /* Auto-generate _save*, _rest* functions in .sfpr.
6842 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6843 instead. */
6844
6845 static bfd_boolean
6846 sfpr_define (struct bfd_link_info *info,
6847 const struct sfpr_def_parms *parm,
6848 asection *stub_sec)
6849 {
6850 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6851 unsigned int i;
6852 size_t len = strlen (parm->name);
6853 bfd_boolean writing = FALSE;
6854 char sym[16];
6855
6856 if (htab == NULL)
6857 return FALSE;
6858
6859 memcpy (sym, parm->name, len);
6860 sym[len + 2] = 0;
6861
6862 for (i = parm->lo; i <= parm->hi; i++)
6863 {
6864 struct ppc_link_hash_entry *h;
6865
6866 sym[len + 0] = i / 10 + '0';
6867 sym[len + 1] = i % 10 + '0';
6868 h = (struct ppc_link_hash_entry *)
6869 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6870 if (stub_sec != NULL)
6871 {
6872 if (h != NULL
6873 && h->elf.root.type == bfd_link_hash_defined
6874 && h->elf.root.u.def.section == htab->sfpr)
6875 {
6876 struct elf_link_hash_entry *s;
6877 char buf[32];
6878 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6879 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6880 if (s == NULL)
6881 return FALSE;
6882 if (s->root.type == bfd_link_hash_new
6883 || (s->root.type = bfd_link_hash_defined
6884 && s->root.u.def.section == stub_sec))
6885 {
6886 s->root.type = bfd_link_hash_defined;
6887 s->root.u.def.section = stub_sec;
6888 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6889 + h->elf.root.u.def.value);
6890 s->ref_regular = 1;
6891 s->def_regular = 1;
6892 s->ref_regular_nonweak = 1;
6893 s->forced_local = 1;
6894 s->non_elf = 0;
6895 s->root.linker_def = 1;
6896 }
6897 }
6898 continue;
6899 }
6900 if (h != NULL)
6901 {
6902 h->save_res = 1;
6903 if (!h->elf.def_regular)
6904 {
6905 h->elf.root.type = bfd_link_hash_defined;
6906 h->elf.root.u.def.section = htab->sfpr;
6907 h->elf.root.u.def.value = htab->sfpr->size;
6908 h->elf.type = STT_FUNC;
6909 h->elf.def_regular = 1;
6910 h->elf.non_elf = 0;
6911 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6912 writing = TRUE;
6913 if (htab->sfpr->contents == NULL)
6914 {
6915 htab->sfpr->contents
6916 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6917 if (htab->sfpr->contents == NULL)
6918 return FALSE;
6919 }
6920 }
6921 }
6922 if (writing)
6923 {
6924 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6925 if (i != parm->hi)
6926 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6927 else
6928 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6929 htab->sfpr->size = p - htab->sfpr->contents;
6930 }
6931 }
6932
6933 return TRUE;
6934 }
6935
6936 static bfd_byte *
6937 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6938 {
6939 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6940 return p + 4;
6941 }
6942
6943 static bfd_byte *
6944 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6945 {
6946 p = savegpr0 (abfd, p, r);
6947 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6948 p = p + 4;
6949 bfd_put_32 (abfd, BLR, p);
6950 return p + 4;
6951 }
6952
6953 static bfd_byte *
6954 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6955 {
6956 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6957 return p + 4;
6958 }
6959
6960 static bfd_byte *
6961 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6962 {
6963 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6964 p = p + 4;
6965 p = restgpr0 (abfd, p, r);
6966 bfd_put_32 (abfd, MTLR_R0, p);
6967 p = p + 4;
6968 if (r == 29)
6969 {
6970 p = restgpr0 (abfd, p, 30);
6971 p = restgpr0 (abfd, p, 31);
6972 }
6973 bfd_put_32 (abfd, BLR, p);
6974 return p + 4;
6975 }
6976
6977 static bfd_byte *
6978 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6979 {
6980 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6981 return p + 4;
6982 }
6983
6984 static bfd_byte *
6985 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6986 {
6987 p = savegpr1 (abfd, p, r);
6988 bfd_put_32 (abfd, BLR, p);
6989 return p + 4;
6990 }
6991
6992 static bfd_byte *
6993 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6994 {
6995 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6996 return p + 4;
6997 }
6998
6999 static bfd_byte *
7000 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
7001 {
7002 p = restgpr1 (abfd, p, r);
7003 bfd_put_32 (abfd, BLR, p);
7004 return p + 4;
7005 }
7006
7007 static bfd_byte *
7008 savefpr (bfd *abfd, bfd_byte *p, int r)
7009 {
7010 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7011 return p + 4;
7012 }
7013
7014 static bfd_byte *
7015 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
7016 {
7017 p = savefpr (abfd, p, r);
7018 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
7019 p = p + 4;
7020 bfd_put_32 (abfd, BLR, p);
7021 return p + 4;
7022 }
7023
7024 static bfd_byte *
7025 restfpr (bfd *abfd, bfd_byte *p, int r)
7026 {
7027 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7028 return p + 4;
7029 }
7030
7031 static bfd_byte *
7032 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
7033 {
7034 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
7035 p = p + 4;
7036 p = restfpr (abfd, p, r);
7037 bfd_put_32 (abfd, MTLR_R0, p);
7038 p = p + 4;
7039 if (r == 29)
7040 {
7041 p = restfpr (abfd, p, 30);
7042 p = restfpr (abfd, p, 31);
7043 }
7044 bfd_put_32 (abfd, BLR, p);
7045 return p + 4;
7046 }
7047
7048 static bfd_byte *
7049 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
7050 {
7051 p = savefpr (abfd, p, r);
7052 bfd_put_32 (abfd, BLR, p);
7053 return p + 4;
7054 }
7055
7056 static bfd_byte *
7057 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
7058 {
7059 p = restfpr (abfd, p, r);
7060 bfd_put_32 (abfd, BLR, p);
7061 return p + 4;
7062 }
7063
7064 static bfd_byte *
7065 savevr (bfd *abfd, bfd_byte *p, int r)
7066 {
7067 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7068 p = p + 4;
7069 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
7070 return p + 4;
7071 }
7072
7073 static bfd_byte *
7074 savevr_tail (bfd *abfd, bfd_byte *p, int r)
7075 {
7076 p = savevr (abfd, p, r);
7077 bfd_put_32 (abfd, BLR, p);
7078 return p + 4;
7079 }
7080
7081 static bfd_byte *
7082 restvr (bfd *abfd, bfd_byte *p, int r)
7083 {
7084 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7085 p = p + 4;
7086 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
7087 return p + 4;
7088 }
7089
7090 static bfd_byte *
7091 restvr_tail (bfd *abfd, bfd_byte *p, int r)
7092 {
7093 p = restvr (abfd, p, r);
7094 bfd_put_32 (abfd, BLR, p);
7095 return p + 4;
7096 }
7097
7098 /* Called via elf_link_hash_traverse to transfer dynamic linking
7099 information on function code symbol entries to their corresponding
7100 function descriptor symbol entries. */
7101
7102 static bfd_boolean
7103 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
7104 {
7105 struct bfd_link_info *info;
7106 struct ppc_link_hash_table *htab;
7107 struct ppc_link_hash_entry *fh;
7108 struct ppc_link_hash_entry *fdh;
7109 bfd_boolean force_local;
7110
7111 fh = (struct ppc_link_hash_entry *) h;
7112 if (fh->elf.root.type == bfd_link_hash_indirect)
7113 return TRUE;
7114
7115 if (!fh->is_func)
7116 return TRUE;
7117
7118 if (fh->elf.root.root.string[0] != '.'
7119 || fh->elf.root.root.string[1] == '\0')
7120 return TRUE;
7121
7122 info = inf;
7123 htab = ppc_hash_table (info);
7124 if (htab == NULL)
7125 return FALSE;
7126
7127 /* Find the corresponding function descriptor symbol. */
7128 fdh = lookup_fdh (fh, htab);
7129
7130 /* Resolve undefined references to dot-symbols as the value
7131 in the function descriptor, if we have one in a regular object.
7132 This is to satisfy cases like ".quad .foo". Calls to functions
7133 in dynamic objects are handled elsewhere. */
7134 if ((fh->elf.root.type == bfd_link_hash_undefined
7135 || fh->elf.root.type == bfd_link_hash_undefweak)
7136 && (fdh->elf.root.type == bfd_link_hash_defined
7137 || fdh->elf.root.type == bfd_link_hash_defweak)
7138 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7139 && opd_entry_value (fdh->elf.root.u.def.section,
7140 fdh->elf.root.u.def.value,
7141 &fh->elf.root.u.def.section,
7142 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7143 {
7144 fh->elf.root.type = fdh->elf.root.type;
7145 fh->elf.forced_local = 1;
7146 fh->elf.def_regular = fdh->elf.def_regular;
7147 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7148 }
7149
7150 if (!fh->elf.dynamic)
7151 {
7152 struct plt_entry *ent;
7153
7154 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7155 if (ent->plt.refcount > 0)
7156 break;
7157 if (ent == NULL)
7158 return TRUE;
7159 }
7160
7161 /* Create a descriptor as undefined if necessary. */
7162 if (fdh == NULL
7163 && !bfd_link_executable (info)
7164 && (fh->elf.root.type == bfd_link_hash_undefined
7165 || fh->elf.root.type == bfd_link_hash_undefweak))
7166 {
7167 fdh = make_fdh (info, fh);
7168 if (fdh == NULL)
7169 return FALSE;
7170 }
7171
7172 /* We can't support overriding of symbols on a fake descriptor. */
7173 if (fdh != NULL
7174 && fdh->fake
7175 && (fh->elf.root.type == bfd_link_hash_defined
7176 || fh->elf.root.type == bfd_link_hash_defweak))
7177 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7178
7179 /* Transfer dynamic linking information to the function descriptor. */
7180 if (fdh != NULL)
7181 {
7182 fdh->elf.ref_regular |= fh->elf.ref_regular;
7183 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7184 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7185 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7186 fdh->elf.dynamic |= fh->elf.dynamic;
7187 fdh->elf.needs_plt |= (fh->elf.needs_plt
7188 || fh->elf.type == STT_FUNC
7189 || fh->elf.type == STT_GNU_IFUNC);
7190 move_plt_plist (fh, fdh);
7191
7192 if (!fdh->elf.forced_local
7193 && fh->elf.dynindx != -1)
7194 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7195 return FALSE;
7196 }
7197
7198 /* Now that the info is on the function descriptor, clear the
7199 function code sym info. Any function code syms for which we
7200 don't have a definition in a regular file, we force local.
7201 This prevents a shared library from exporting syms that have
7202 been imported from another library. Function code syms that
7203 are really in the library we must leave global to prevent the
7204 linker dragging in a definition from a static library. */
7205 force_local = (!fh->elf.def_regular
7206 || fdh == NULL
7207 || !fdh->elf.def_regular
7208 || fdh->elf.forced_local);
7209 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7210
7211 return TRUE;
7212 }
7213
7214 static const struct sfpr_def_parms save_res_funcs[] =
7215 {
7216 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7217 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7218 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7219 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7220 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7221 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7222 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7223 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7224 { "._savef", 14, 31, savefpr, savefpr1_tail },
7225 { "._restf", 14, 31, restfpr, restfpr1_tail },
7226 { "_savevr_", 20, 31, savevr, savevr_tail },
7227 { "_restvr_", 20, 31, restvr, restvr_tail }
7228 };
7229
7230 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7231 this hook to a) provide some gcc support functions, and b) transfer
7232 dynamic linking information gathered so far on function code symbol
7233 entries, to their corresponding function descriptor symbol entries. */
7234
7235 static bfd_boolean
7236 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7237 struct bfd_link_info *info)
7238 {
7239 struct ppc_link_hash_table *htab;
7240
7241 htab = ppc_hash_table (info);
7242 if (htab == NULL)
7243 return FALSE;
7244
7245 /* Provide any missing _save* and _rest* functions. */
7246 if (htab->sfpr != NULL)
7247 {
7248 unsigned int i;
7249
7250 htab->sfpr->size = 0;
7251 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7252 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7253 return FALSE;
7254 if (htab->sfpr->size == 0)
7255 htab->sfpr->flags |= SEC_EXCLUDE;
7256 }
7257
7258 if (bfd_link_relocatable (info))
7259 return TRUE;
7260
7261 if (htab->elf.hgot != NULL)
7262 {
7263 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7264 /* Make .TOC. defined so as to prevent it being made dynamic.
7265 The wrong value here is fixed later in ppc64_elf_set_toc. */
7266 if (!htab->elf.hgot->def_regular
7267 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7268 {
7269 htab->elf.hgot->root.type = bfd_link_hash_defined;
7270 htab->elf.hgot->root.u.def.value = 0;
7271 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7272 htab->elf.hgot->def_regular = 1;
7273 htab->elf.hgot->root.linker_def = 1;
7274 }
7275 htab->elf.hgot->type = STT_OBJECT;
7276 htab->elf.hgot->other
7277 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
7278 }
7279
7280 if (htab->need_func_desc_adj)
7281 {
7282 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7283 htab->need_func_desc_adj = 0;
7284 }
7285
7286 return TRUE;
7287 }
7288
7289 /* Find dynamic relocs for H that apply to read-only sections. */
7290
7291 static asection *
7292 readonly_dynrelocs (struct elf_link_hash_entry *h)
7293 {
7294 struct ppc_link_hash_entry *eh;
7295 struct elf_dyn_relocs *p;
7296
7297 eh = (struct ppc_link_hash_entry *) h;
7298 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7299 {
7300 asection *s = p->sec->output_section;
7301
7302 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7303 return p->sec;
7304 }
7305 return NULL;
7306 }
7307
7308 /* Return true if we have dynamic relocs against H or any of its weak
7309 aliases, that apply to read-only sections. Cannot be used after
7310 size_dynamic_sections. */
7311
7312 static bfd_boolean
7313 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7314 {
7315 struct ppc_link_hash_entry *eh;
7316
7317 eh = (struct ppc_link_hash_entry *) h;
7318 do
7319 {
7320 if (readonly_dynrelocs (&eh->elf))
7321 return TRUE;
7322 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7323 }
7324 while (eh != NULL && &eh->elf != h);
7325
7326 return FALSE;
7327 }
7328
7329 /* Return whether EH has pc-relative dynamic relocs. */
7330
7331 static bfd_boolean
7332 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7333 {
7334 struct elf_dyn_relocs *p;
7335
7336 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7337 if (p->pc_count != 0)
7338 return TRUE;
7339 return FALSE;
7340 }
7341
7342 /* Return true if a global entry stub will be created for H. Valid
7343 for ELFv2 before plt entries have been allocated. */
7344
7345 static bfd_boolean
7346 global_entry_stub (struct elf_link_hash_entry *h)
7347 {
7348 struct plt_entry *pent;
7349
7350 if (!h->pointer_equality_needed
7351 || h->def_regular)
7352 return FALSE;
7353
7354 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7355 if (pent->plt.refcount > 0
7356 && pent->addend == 0)
7357 return TRUE;
7358
7359 return FALSE;
7360 }
7361
7362 /* Adjust a symbol defined by a dynamic object and referenced by a
7363 regular object. The current definition is in some section of the
7364 dynamic object, but we're not including those sections. We have to
7365 change the definition to something the rest of the link can
7366 understand. */
7367
7368 static bfd_boolean
7369 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7370 struct elf_link_hash_entry *h)
7371 {
7372 struct ppc_link_hash_table *htab;
7373 asection *s, *srel;
7374
7375 htab = ppc_hash_table (info);
7376 if (htab == NULL)
7377 return FALSE;
7378
7379 /* Deal with function syms. */
7380 if (h->type == STT_FUNC
7381 || h->type == STT_GNU_IFUNC
7382 || h->needs_plt)
7383 {
7384 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7385 || SYMBOL_CALLS_LOCAL (info, h)
7386 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7387 /* Discard dyn_relocs when non-pic if we've decided that a
7388 function symbol is local and not an ifunc. We keep dynamic
7389 relocs for ifuncs when local rather than always emitting a
7390 plt call stub for them and defining the symbol on the call
7391 stub. We can't do that for ELFv1 anyway (a function symbol
7392 is defined on a descriptor, not code) and it can be faster at
7393 run-time due to not needing to bounce through a stub. The
7394 dyn_relocs for ifuncs will be applied even in a static
7395 executable. */
7396 if (!bfd_link_pic (info)
7397 && h->type != STT_GNU_IFUNC
7398 && local)
7399 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7400
7401 /* Clear procedure linkage table information for any symbol that
7402 won't need a .plt entry. */
7403 struct plt_entry *ent;
7404 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7405 if (ent->plt.refcount > 0)
7406 break;
7407 if (ent == NULL
7408 || (h->type != STT_GNU_IFUNC
7409 && local
7410 && (htab->can_convert_all_inline_plt
7411 || (((struct ppc_link_hash_entry *) h)->tls_mask
7412 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7413 {
7414 h->plt.plist = NULL;
7415 h->needs_plt = 0;
7416 h->pointer_equality_needed = 0;
7417 }
7418 else if (abiversion (info->output_bfd) >= 2)
7419 {
7420 /* Taking a function's address in a read/write section
7421 doesn't require us to define the function symbol in the
7422 executable on a global entry stub. A dynamic reloc can
7423 be used instead. The reason we prefer a few more dynamic
7424 relocs is that calling via a global entry stub costs a
7425 few more instructions, and pointer_equality_needed causes
7426 extra work in ld.so when resolving these symbols. */
7427 if (global_entry_stub (h))
7428 {
7429 if (!readonly_dynrelocs (h))
7430 {
7431 h->pointer_equality_needed = 0;
7432 /* If we haven't seen a branch reloc and the symbol
7433 isn't an ifunc then we don't need a plt entry. */
7434 if (!h->needs_plt)
7435 h->plt.plist = NULL;
7436 }
7437 else if (!bfd_link_pic (info))
7438 /* We are going to be defining the function symbol on the
7439 plt stub, so no dyn_relocs needed when non-pic. */
7440 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7441 }
7442
7443 /* ELFv2 function symbols can't have copy relocs. */
7444 return TRUE;
7445 }
7446 else if (!h->needs_plt
7447 && !readonly_dynrelocs (h))
7448 {
7449 /* If we haven't seen a branch reloc and the symbol isn't an
7450 ifunc then we don't need a plt entry. */
7451 h->plt.plist = NULL;
7452 h->pointer_equality_needed = 0;
7453 return TRUE;
7454 }
7455 }
7456 else
7457 h->plt.plist = NULL;
7458
7459 /* If this is a weak symbol, and there is a real definition, the
7460 processor independent code will have arranged for us to see the
7461 real definition first, and we can just use the same value. */
7462 if (h->is_weakalias)
7463 {
7464 struct elf_link_hash_entry *def = weakdef (h);
7465 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7466 h->root.u.def.section = def->root.u.def.section;
7467 h->root.u.def.value = def->root.u.def.value;
7468 if (def->root.u.def.section == htab->elf.sdynbss
7469 || def->root.u.def.section == htab->elf.sdynrelro)
7470 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7471 return TRUE;
7472 }
7473
7474 /* If we are creating a shared library, we must presume that the
7475 only references to the symbol are via the global offset table.
7476 For such cases we need not do anything here; the relocations will
7477 be handled correctly by relocate_section. */
7478 if (bfd_link_pic (info))
7479 return TRUE;
7480
7481 /* If there are no references to this symbol that do not use the
7482 GOT, we don't need to generate a copy reloc. */
7483 if (!h->non_got_ref)
7484 return TRUE;
7485
7486 /* Don't generate a copy reloc for symbols defined in the executable. */
7487 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7488
7489 /* If -z nocopyreloc was given, don't generate them either. */
7490 || info->nocopyreloc
7491
7492 /* If we don't find any dynamic relocs in read-only sections, then
7493 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7494 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7495
7496 /* Protected variables do not work with .dynbss. The copy in
7497 .dynbss won't be used by the shared library with the protected
7498 definition for the variable. Text relocations are preferable
7499 to an incorrect program. */
7500 || h->protected_def)
7501 return TRUE;
7502
7503 if (h->plt.plist != NULL)
7504 {
7505 /* We should never get here, but unfortunately there are versions
7506 of gcc out there that improperly (for this ABI) put initialized
7507 function pointers, vtable refs and suchlike in read-only
7508 sections. Allow them to proceed, but warn that this might
7509 break at runtime. */
7510 info->callbacks->einfo
7511 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7512 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7513 h->root.root.string);
7514 }
7515
7516 /* This is a reference to a symbol defined by a dynamic object which
7517 is not a function. */
7518
7519 /* We must allocate the symbol in our .dynbss section, which will
7520 become part of the .bss section of the executable. There will be
7521 an entry for this symbol in the .dynsym section. The dynamic
7522 object will contain position independent code, so all references
7523 from the dynamic object to this symbol will go through the global
7524 offset table. The dynamic linker will use the .dynsym entry to
7525 determine the address it must put in the global offset table, so
7526 both the dynamic object and the regular object will refer to the
7527 same memory location for the variable. */
7528 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7529 {
7530 s = htab->elf.sdynrelro;
7531 srel = htab->elf.sreldynrelro;
7532 }
7533 else
7534 {
7535 s = htab->elf.sdynbss;
7536 srel = htab->elf.srelbss;
7537 }
7538 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7539 {
7540 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7541 linker to copy the initial value out of the dynamic object
7542 and into the runtime process image. */
7543 srel->size += sizeof (Elf64_External_Rela);
7544 h->needs_copy = 1;
7545 }
7546
7547 /* We no longer want dyn_relocs. */
7548 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7549 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7550 }
7551
7552 /* If given a function descriptor symbol, hide both the function code
7553 sym and the descriptor. */
7554 static void
7555 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7556 struct elf_link_hash_entry *h,
7557 bfd_boolean force_local)
7558 {
7559 struct ppc_link_hash_entry *eh;
7560 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7561
7562 eh = (struct ppc_link_hash_entry *) h;
7563 if (eh->is_func_descriptor)
7564 {
7565 struct ppc_link_hash_entry *fh = eh->oh;
7566
7567 if (fh == NULL)
7568 {
7569 const char *p, *q;
7570 struct elf_link_hash_table *htab = elf_hash_table (info);
7571 char save;
7572
7573 /* We aren't supposed to use alloca in BFD because on
7574 systems which do not have alloca the version in libiberty
7575 calls xmalloc, which might cause the program to crash
7576 when it runs out of memory. This function doesn't have a
7577 return status, so there's no way to gracefully return an
7578 error. So cheat. We know that string[-1] can be safely
7579 accessed; It's either a string in an ELF string table,
7580 or allocated in an objalloc structure. */
7581
7582 p = eh->elf.root.root.string - 1;
7583 save = *p;
7584 *(char *) p = '.';
7585 fh = (struct ppc_link_hash_entry *)
7586 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7587 *(char *) p = save;
7588
7589 /* Unfortunately, if it so happens that the string we were
7590 looking for was allocated immediately before this string,
7591 then we overwrote the string terminator. That's the only
7592 reason the lookup should fail. */
7593 if (fh == NULL)
7594 {
7595 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7596 while (q >= eh->elf.root.root.string && *q == *p)
7597 --q, --p;
7598 if (q < eh->elf.root.root.string && *p == '.')
7599 fh = (struct ppc_link_hash_entry *)
7600 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7601 }
7602 if (fh != NULL)
7603 {
7604 eh->oh = fh;
7605 fh->oh = eh;
7606 }
7607 }
7608 if (fh != NULL)
7609 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7610 }
7611 }
7612
7613 static bfd_boolean
7614 get_sym_h (struct elf_link_hash_entry **hp,
7615 Elf_Internal_Sym **symp,
7616 asection **symsecp,
7617 unsigned char **tls_maskp,
7618 Elf_Internal_Sym **locsymsp,
7619 unsigned long r_symndx,
7620 bfd *ibfd)
7621 {
7622 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7623
7624 if (r_symndx >= symtab_hdr->sh_info)
7625 {
7626 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7627 struct elf_link_hash_entry *h;
7628
7629 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7630 h = elf_follow_link (h);
7631
7632 if (hp != NULL)
7633 *hp = h;
7634
7635 if (symp != NULL)
7636 *symp = NULL;
7637
7638 if (symsecp != NULL)
7639 {
7640 asection *symsec = NULL;
7641 if (h->root.type == bfd_link_hash_defined
7642 || h->root.type == bfd_link_hash_defweak)
7643 symsec = h->root.u.def.section;
7644 *symsecp = symsec;
7645 }
7646
7647 if (tls_maskp != NULL)
7648 {
7649 struct ppc_link_hash_entry *eh;
7650
7651 eh = (struct ppc_link_hash_entry *) h;
7652 *tls_maskp = &eh->tls_mask;
7653 }
7654 }
7655 else
7656 {
7657 Elf_Internal_Sym *sym;
7658 Elf_Internal_Sym *locsyms = *locsymsp;
7659
7660 if (locsyms == NULL)
7661 {
7662 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7663 if (locsyms == NULL)
7664 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7665 symtab_hdr->sh_info,
7666 0, NULL, NULL, NULL);
7667 if (locsyms == NULL)
7668 return FALSE;
7669 *locsymsp = locsyms;
7670 }
7671 sym = locsyms + r_symndx;
7672
7673 if (hp != NULL)
7674 *hp = NULL;
7675
7676 if (symp != NULL)
7677 *symp = sym;
7678
7679 if (symsecp != NULL)
7680 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7681
7682 if (tls_maskp != NULL)
7683 {
7684 struct got_entry **lgot_ents;
7685 unsigned char *tls_mask;
7686
7687 tls_mask = NULL;
7688 lgot_ents = elf_local_got_ents (ibfd);
7689 if (lgot_ents != NULL)
7690 {
7691 struct plt_entry **local_plt = (struct plt_entry **)
7692 (lgot_ents + symtab_hdr->sh_info);
7693 unsigned char *lgot_masks = (unsigned char *)
7694 (local_plt + symtab_hdr->sh_info);
7695 tls_mask = &lgot_masks[r_symndx];
7696 }
7697 *tls_maskp = tls_mask;
7698 }
7699 }
7700 return TRUE;
7701 }
7702
7703 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7704 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7705 type suitable for optimization, and 1 otherwise. */
7706
7707 static int
7708 get_tls_mask (unsigned char **tls_maskp,
7709 unsigned long *toc_symndx,
7710 bfd_vma *toc_addend,
7711 Elf_Internal_Sym **locsymsp,
7712 const Elf_Internal_Rela *rel,
7713 bfd *ibfd)
7714 {
7715 unsigned long r_symndx;
7716 int next_r;
7717 struct elf_link_hash_entry *h;
7718 Elf_Internal_Sym *sym;
7719 asection *sec;
7720 bfd_vma off;
7721
7722 r_symndx = ELF64_R_SYM (rel->r_info);
7723 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7724 return 0;
7725
7726 if ((*tls_maskp != NULL
7727 && (**tls_maskp & TLS_TLS) != 0
7728 && **tls_maskp != (TLS_TLS | TLS_MARK))
7729 || sec == NULL
7730 || ppc64_elf_section_data (sec) == NULL
7731 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7732 return 1;
7733
7734 /* Look inside a TOC section too. */
7735 if (h != NULL)
7736 {
7737 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7738 off = h->root.u.def.value;
7739 }
7740 else
7741 off = sym->st_value;
7742 off += rel->r_addend;
7743 BFD_ASSERT (off % 8 == 0);
7744 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7745 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7746 if (toc_symndx != NULL)
7747 *toc_symndx = r_symndx;
7748 if (toc_addend != NULL)
7749 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7750 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7751 return 0;
7752 if ((h == NULL || is_static_defined (h))
7753 && (next_r == -1 || next_r == -2))
7754 return 1 - next_r;
7755 return 1;
7756 }
7757
7758 /* Find (or create) an entry in the tocsave hash table. */
7759
7760 static struct tocsave_entry *
7761 tocsave_find (struct ppc_link_hash_table *htab,
7762 enum insert_option insert,
7763 Elf_Internal_Sym **local_syms,
7764 const Elf_Internal_Rela *irela,
7765 bfd *ibfd)
7766 {
7767 unsigned long r_indx;
7768 struct elf_link_hash_entry *h;
7769 Elf_Internal_Sym *sym;
7770 struct tocsave_entry ent, *p;
7771 hashval_t hash;
7772 struct tocsave_entry **slot;
7773
7774 r_indx = ELF64_R_SYM (irela->r_info);
7775 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7776 return NULL;
7777 if (ent.sec == NULL || ent.sec->output_section == NULL)
7778 {
7779 _bfd_error_handler
7780 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7781 return NULL;
7782 }
7783
7784 if (h != NULL)
7785 ent.offset = h->root.u.def.value;
7786 else
7787 ent.offset = sym->st_value;
7788 ent.offset += irela->r_addend;
7789
7790 hash = tocsave_htab_hash (&ent);
7791 slot = ((struct tocsave_entry **)
7792 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7793 if (slot == NULL)
7794 return NULL;
7795
7796 if (*slot == NULL)
7797 {
7798 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7799 if (p == NULL)
7800 return NULL;
7801 *p = ent;
7802 *slot = p;
7803 }
7804 return *slot;
7805 }
7806
7807 /* Adjust all global syms defined in opd sections. In gcc generated
7808 code for the old ABI, these will already have been done. */
7809
7810 static bfd_boolean
7811 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7812 {
7813 struct ppc_link_hash_entry *eh;
7814 asection *sym_sec;
7815 struct _opd_sec_data *opd;
7816
7817 if (h->root.type == bfd_link_hash_indirect)
7818 return TRUE;
7819
7820 if (h->root.type != bfd_link_hash_defined
7821 && h->root.type != bfd_link_hash_defweak)
7822 return TRUE;
7823
7824 eh = (struct ppc_link_hash_entry *) h;
7825 if (eh->adjust_done)
7826 return TRUE;
7827
7828 sym_sec = eh->elf.root.u.def.section;
7829 opd = get_opd_info (sym_sec);
7830 if (opd != NULL && opd->adjust != NULL)
7831 {
7832 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7833 if (adjust == -1)
7834 {
7835 /* This entry has been deleted. */
7836 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7837 if (dsec == NULL)
7838 {
7839 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7840 if (discarded_section (dsec))
7841 {
7842 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7843 break;
7844 }
7845 }
7846 eh->elf.root.u.def.value = 0;
7847 eh->elf.root.u.def.section = dsec;
7848 }
7849 else
7850 eh->elf.root.u.def.value += adjust;
7851 eh->adjust_done = 1;
7852 }
7853 return TRUE;
7854 }
7855
7856 /* Handles decrementing dynamic reloc counts for the reloc specified by
7857 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7858 have already been determined. */
7859
7860 static bfd_boolean
7861 dec_dynrel_count (bfd_vma r_info,
7862 asection *sec,
7863 struct bfd_link_info *info,
7864 Elf_Internal_Sym **local_syms,
7865 struct elf_link_hash_entry *h,
7866 Elf_Internal_Sym *sym)
7867 {
7868 enum elf_ppc64_reloc_type r_type;
7869 asection *sym_sec = NULL;
7870
7871 /* Can this reloc be dynamic? This switch, and later tests here
7872 should be kept in sync with the code in check_relocs. */
7873 r_type = ELF64_R_TYPE (r_info);
7874 switch (r_type)
7875 {
7876 default:
7877 return TRUE;
7878
7879 case R_PPC64_TPREL16:
7880 case R_PPC64_TPREL16_LO:
7881 case R_PPC64_TPREL16_HI:
7882 case R_PPC64_TPREL16_HA:
7883 case R_PPC64_TPREL16_DS:
7884 case R_PPC64_TPREL16_LO_DS:
7885 case R_PPC64_TPREL16_HIGH:
7886 case R_PPC64_TPREL16_HIGHA:
7887 case R_PPC64_TPREL16_HIGHER:
7888 case R_PPC64_TPREL16_HIGHERA:
7889 case R_PPC64_TPREL16_HIGHEST:
7890 case R_PPC64_TPREL16_HIGHESTA:
7891 case R_PPC64_TPREL64:
7892 case R_PPC64_DTPMOD64:
7893 case R_PPC64_DTPREL64:
7894 case R_PPC64_ADDR64:
7895 case R_PPC64_REL30:
7896 case R_PPC64_REL32:
7897 case R_PPC64_REL64:
7898 case R_PPC64_ADDR14:
7899 case R_PPC64_ADDR14_BRNTAKEN:
7900 case R_PPC64_ADDR14_BRTAKEN:
7901 case R_PPC64_ADDR16:
7902 case R_PPC64_ADDR16_DS:
7903 case R_PPC64_ADDR16_HA:
7904 case R_PPC64_ADDR16_HI:
7905 case R_PPC64_ADDR16_HIGH:
7906 case R_PPC64_ADDR16_HIGHA:
7907 case R_PPC64_ADDR16_HIGHER:
7908 case R_PPC64_ADDR16_HIGHERA:
7909 case R_PPC64_ADDR16_HIGHEST:
7910 case R_PPC64_ADDR16_HIGHESTA:
7911 case R_PPC64_ADDR16_LO:
7912 case R_PPC64_ADDR16_LO_DS:
7913 case R_PPC64_ADDR24:
7914 case R_PPC64_ADDR32:
7915 case R_PPC64_UADDR16:
7916 case R_PPC64_UADDR32:
7917 case R_PPC64_UADDR64:
7918 case R_PPC64_TOC:
7919 break;
7920 }
7921
7922 if (local_syms != NULL)
7923 {
7924 unsigned long r_symndx;
7925 bfd *ibfd = sec->owner;
7926
7927 r_symndx = ELF64_R_SYM (r_info);
7928 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7929 return FALSE;
7930 }
7931
7932 if ((bfd_link_pic (info)
7933 && (must_be_dyn_reloc (info, r_type)
7934 || (h != NULL
7935 && (!SYMBOLIC_BIND (info, h)
7936 || h->root.type == bfd_link_hash_defweak
7937 || !h->def_regular))))
7938 || (ELIMINATE_COPY_RELOCS
7939 && !bfd_link_pic (info)
7940 && h != NULL
7941 && (h->root.type == bfd_link_hash_defweak
7942 || !h->def_regular)))
7943 ;
7944 else
7945 return TRUE;
7946
7947 if (h != NULL)
7948 {
7949 struct elf_dyn_relocs *p;
7950 struct elf_dyn_relocs **pp;
7951 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7952
7953 /* elf_gc_sweep may have already removed all dyn relocs associated
7954 with local syms for a given section. Also, symbol flags are
7955 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7956 report a dynreloc miscount. */
7957 if (*pp == NULL && info->gc_sections)
7958 return TRUE;
7959
7960 while ((p = *pp) != NULL)
7961 {
7962 if (p->sec == sec)
7963 {
7964 if (!must_be_dyn_reloc (info, r_type))
7965 p->pc_count -= 1;
7966 p->count -= 1;
7967 if (p->count == 0)
7968 *pp = p->next;
7969 return TRUE;
7970 }
7971 pp = &p->next;
7972 }
7973 }
7974 else
7975 {
7976 struct ppc_dyn_relocs *p;
7977 struct ppc_dyn_relocs **pp;
7978 void *vpp;
7979 bfd_boolean is_ifunc;
7980
7981 if (local_syms == NULL)
7982 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7983 if (sym_sec == NULL)
7984 sym_sec = sec;
7985
7986 vpp = &elf_section_data (sym_sec)->local_dynrel;
7987 pp = (struct ppc_dyn_relocs **) vpp;
7988
7989 if (*pp == NULL && info->gc_sections)
7990 return TRUE;
7991
7992 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7993 while ((p = *pp) != NULL)
7994 {
7995 if (p->sec == sec && p->ifunc == is_ifunc)
7996 {
7997 p->count -= 1;
7998 if (p->count == 0)
7999 *pp = p->next;
8000 return TRUE;
8001 }
8002 pp = &p->next;
8003 }
8004 }
8005
8006 /* xgettext:c-format */
8007 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
8008 sec->owner, sec);
8009 bfd_set_error (bfd_error_bad_value);
8010 return FALSE;
8011 }
8012
8013 /* Remove unused Official Procedure Descriptor entries. Currently we
8014 only remove those associated with functions in discarded link-once
8015 sections, or weakly defined functions that have been overridden. It
8016 would be possible to remove many more entries for statically linked
8017 applications. */
8018
8019 bfd_boolean
8020 ppc64_elf_edit_opd (struct bfd_link_info *info)
8021 {
8022 bfd *ibfd;
8023 bfd_boolean some_edited = FALSE;
8024 asection *need_pad = NULL;
8025 struct ppc_link_hash_table *htab;
8026
8027 htab = ppc_hash_table (info);
8028 if (htab == NULL)
8029 return FALSE;
8030
8031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8032 {
8033 asection *sec;
8034 Elf_Internal_Rela *relstart, *rel, *relend;
8035 Elf_Internal_Shdr *symtab_hdr;
8036 Elf_Internal_Sym *local_syms;
8037 struct _opd_sec_data *opd;
8038 bfd_boolean need_edit, add_aux_fields, broken;
8039 bfd_size_type cnt_16b = 0;
8040
8041 if (!is_ppc64_elf (ibfd))
8042 continue;
8043
8044 sec = bfd_get_section_by_name (ibfd, ".opd");
8045 if (sec == NULL || sec->size == 0)
8046 continue;
8047
8048 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
8049 continue;
8050
8051 if (sec->output_section == bfd_abs_section_ptr)
8052 continue;
8053
8054 /* Look through the section relocs. */
8055 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
8056 continue;
8057
8058 local_syms = NULL;
8059 symtab_hdr = &elf_symtab_hdr (ibfd);
8060
8061 /* Read the relocations. */
8062 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8063 info->keep_memory);
8064 if (relstart == NULL)
8065 return FALSE;
8066
8067 /* First run through the relocs to check they are sane, and to
8068 determine whether we need to edit this opd section. */
8069 need_edit = FALSE;
8070 broken = FALSE;
8071 need_pad = sec;
8072 relend = relstart + sec->reloc_count;
8073 for (rel = relstart; rel < relend; )
8074 {
8075 enum elf_ppc64_reloc_type r_type;
8076 unsigned long r_symndx;
8077 asection *sym_sec;
8078 struct elf_link_hash_entry *h;
8079 Elf_Internal_Sym *sym;
8080 bfd_vma offset;
8081
8082 /* .opd contains an array of 16 or 24 byte entries. We're
8083 only interested in the reloc pointing to a function entry
8084 point. */
8085 offset = rel->r_offset;
8086 if (rel + 1 == relend
8087 || rel[1].r_offset != offset + 8)
8088 {
8089 /* If someone messes with .opd alignment then after a
8090 "ld -r" we might have padding in the middle of .opd.
8091 Also, there's nothing to prevent someone putting
8092 something silly in .opd with the assembler. No .opd
8093 optimization for them! */
8094 broken_opd:
8095 _bfd_error_handler
8096 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
8097 broken = TRUE;
8098 break;
8099 }
8100
8101 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
8102 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
8103 {
8104 _bfd_error_handler
8105 /* xgettext:c-format */
8106 (_("%pB: unexpected reloc type %u in .opd section"),
8107 ibfd, r_type);
8108 broken = TRUE;
8109 break;
8110 }
8111
8112 r_symndx = ELF64_R_SYM (rel->r_info);
8113 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8114 r_symndx, ibfd))
8115 goto error_ret;
8116
8117 if (sym_sec == NULL || sym_sec->owner == NULL)
8118 {
8119 const char *sym_name;
8120 if (h != NULL)
8121 sym_name = h->root.root.string;
8122 else
8123 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8124 sym_sec);
8125
8126 _bfd_error_handler
8127 /* xgettext:c-format */
8128 (_("%pB: undefined sym `%s' in .opd section"),
8129 ibfd, sym_name);
8130 broken = TRUE;
8131 break;
8132 }
8133
8134 /* opd entries are always for functions defined in the
8135 current input bfd. If the symbol isn't defined in the
8136 input bfd, then we won't be using the function in this
8137 bfd; It must be defined in a linkonce section in another
8138 bfd, or is weak. It's also possible that we are
8139 discarding the function due to a linker script /DISCARD/,
8140 which we test for via the output_section. */
8141 if (sym_sec->owner != ibfd
8142 || sym_sec->output_section == bfd_abs_section_ptr)
8143 need_edit = TRUE;
8144
8145 rel += 2;
8146 if (rel + 1 == relend
8147 || (rel + 2 < relend
8148 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8149 ++rel;
8150
8151 if (rel == relend)
8152 {
8153 if (sec->size == offset + 24)
8154 {
8155 need_pad = NULL;
8156 break;
8157 }
8158 if (sec->size == offset + 16)
8159 {
8160 cnt_16b++;
8161 break;
8162 }
8163 goto broken_opd;
8164 }
8165 else if (rel + 1 < relend
8166 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8167 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8168 {
8169 if (rel[0].r_offset == offset + 16)
8170 cnt_16b++;
8171 else if (rel[0].r_offset != offset + 24)
8172 goto broken_opd;
8173 }
8174 else
8175 goto broken_opd;
8176 }
8177
8178 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8179
8180 if (!broken && (need_edit || add_aux_fields))
8181 {
8182 Elf_Internal_Rela *write_rel;
8183 Elf_Internal_Shdr *rel_hdr;
8184 bfd_byte *rptr, *wptr;
8185 bfd_byte *new_contents;
8186 bfd_size_type amt;
8187
8188 new_contents = NULL;
8189 amt = OPD_NDX (sec->size) * sizeof (long);
8190 opd = &ppc64_elf_section_data (sec)->u.opd;
8191 opd->adjust = bfd_zalloc (sec->owner, amt);
8192 if (opd->adjust == NULL)
8193 return FALSE;
8194
8195 /* This seems a waste of time as input .opd sections are all
8196 zeros as generated by gcc, but I suppose there's no reason
8197 this will always be so. We might start putting something in
8198 the third word of .opd entries. */
8199 if ((sec->flags & SEC_IN_MEMORY) == 0)
8200 {
8201 bfd_byte *loc;
8202 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8203 {
8204 if (loc != NULL)
8205 free (loc);
8206 error_ret:
8207 if (local_syms != NULL
8208 && symtab_hdr->contents != (unsigned char *) local_syms)
8209 free (local_syms);
8210 if (elf_section_data (sec)->relocs != relstart)
8211 free (relstart);
8212 return FALSE;
8213 }
8214 sec->contents = loc;
8215 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8216 }
8217
8218 elf_section_data (sec)->relocs = relstart;
8219
8220 new_contents = sec->contents;
8221 if (add_aux_fields)
8222 {
8223 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8224 if (new_contents == NULL)
8225 return FALSE;
8226 need_pad = NULL;
8227 }
8228 wptr = new_contents;
8229 rptr = sec->contents;
8230 write_rel = relstart;
8231 for (rel = relstart; rel < relend; )
8232 {
8233 unsigned long r_symndx;
8234 asection *sym_sec;
8235 struct elf_link_hash_entry *h;
8236 struct ppc_link_hash_entry *fdh = NULL;
8237 Elf_Internal_Sym *sym;
8238 long opd_ent_size;
8239 Elf_Internal_Rela *next_rel;
8240 bfd_boolean skip;
8241
8242 r_symndx = ELF64_R_SYM (rel->r_info);
8243 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8244 r_symndx, ibfd))
8245 goto error_ret;
8246
8247 next_rel = rel + 2;
8248 if (next_rel + 1 == relend
8249 || (next_rel + 2 < relend
8250 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8251 ++next_rel;
8252
8253 /* See if the .opd entry is full 24 byte or
8254 16 byte (with fd_aux entry overlapped with next
8255 fd_func). */
8256 opd_ent_size = 24;
8257 if (next_rel == relend)
8258 {
8259 if (sec->size == rel->r_offset + 16)
8260 opd_ent_size = 16;
8261 }
8262 else if (next_rel->r_offset == rel->r_offset + 16)
8263 opd_ent_size = 16;
8264
8265 if (h != NULL
8266 && h->root.root.string[0] == '.')
8267 {
8268 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8269 if (fdh != NULL)
8270 {
8271 fdh = ppc_follow_link (fdh);
8272 if (fdh->elf.root.type != bfd_link_hash_defined
8273 && fdh->elf.root.type != bfd_link_hash_defweak)
8274 fdh = NULL;
8275 }
8276 }
8277
8278 skip = (sym_sec->owner != ibfd
8279 || sym_sec->output_section == bfd_abs_section_ptr);
8280 if (skip)
8281 {
8282 if (fdh != NULL && sym_sec->owner == ibfd)
8283 {
8284 /* Arrange for the function descriptor sym
8285 to be dropped. */
8286 fdh->elf.root.u.def.value = 0;
8287 fdh->elf.root.u.def.section = sym_sec;
8288 }
8289 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8290
8291 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8292 rel = next_rel;
8293 else
8294 while (1)
8295 {
8296 if (!dec_dynrel_count (rel->r_info, sec, info,
8297 NULL, h, sym))
8298 goto error_ret;
8299
8300 if (++rel == next_rel)
8301 break;
8302
8303 r_symndx = ELF64_R_SYM (rel->r_info);
8304 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8305 r_symndx, ibfd))
8306 goto error_ret;
8307 }
8308 }
8309 else
8310 {
8311 /* We'll be keeping this opd entry. */
8312 long adjust;
8313
8314 if (fdh != NULL)
8315 {
8316 /* Redefine the function descriptor symbol to
8317 this location in the opd section. It is
8318 necessary to update the value here rather
8319 than using an array of adjustments as we do
8320 for local symbols, because various places
8321 in the generic ELF code use the value
8322 stored in u.def.value. */
8323 fdh->elf.root.u.def.value = wptr - new_contents;
8324 fdh->adjust_done = 1;
8325 }
8326
8327 /* Local syms are a bit tricky. We could
8328 tweak them as they can be cached, but
8329 we'd need to look through the local syms
8330 for the function descriptor sym which we
8331 don't have at the moment. So keep an
8332 array of adjustments. */
8333 adjust = (wptr - new_contents) - (rptr - sec->contents);
8334 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8335
8336 if (wptr != rptr)
8337 memcpy (wptr, rptr, opd_ent_size);
8338 wptr += opd_ent_size;
8339 if (add_aux_fields && opd_ent_size == 16)
8340 {
8341 memset (wptr, '\0', 8);
8342 wptr += 8;
8343 }
8344
8345 /* We need to adjust any reloc offsets to point to the
8346 new opd entries. */
8347 for ( ; rel != next_rel; ++rel)
8348 {
8349 rel->r_offset += adjust;
8350 if (write_rel != rel)
8351 memcpy (write_rel, rel, sizeof (*rel));
8352 ++write_rel;
8353 }
8354 }
8355
8356 rptr += opd_ent_size;
8357 }
8358
8359 sec->size = wptr - new_contents;
8360 sec->reloc_count = write_rel - relstart;
8361 if (add_aux_fields)
8362 {
8363 free (sec->contents);
8364 sec->contents = new_contents;
8365 }
8366
8367 /* Fudge the header size too, as this is used later in
8368 elf_bfd_final_link if we are emitting relocs. */
8369 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8370 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8371 some_edited = TRUE;
8372 }
8373 else if (elf_section_data (sec)->relocs != relstart)
8374 free (relstart);
8375
8376 if (local_syms != NULL
8377 && symtab_hdr->contents != (unsigned char *) local_syms)
8378 {
8379 if (!info->keep_memory)
8380 free (local_syms);
8381 else
8382 symtab_hdr->contents = (unsigned char *) local_syms;
8383 }
8384 }
8385
8386 if (some_edited)
8387 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8388
8389 /* If we are doing a final link and the last .opd entry is just 16 byte
8390 long, add a 8 byte padding after it. */
8391 if (need_pad != NULL && !bfd_link_relocatable (info))
8392 {
8393 bfd_byte *p;
8394
8395 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8396 {
8397 BFD_ASSERT (need_pad->size > 0);
8398
8399 p = bfd_malloc (need_pad->size + 8);
8400 if (p == NULL)
8401 return FALSE;
8402
8403 if (!bfd_get_section_contents (need_pad->owner, need_pad,
8404 p, 0, need_pad->size))
8405 return FALSE;
8406
8407 need_pad->contents = p;
8408 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8409 }
8410 else
8411 {
8412 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8413 if (p == NULL)
8414 return FALSE;
8415
8416 need_pad->contents = p;
8417 }
8418
8419 memset (need_pad->contents + need_pad->size, 0, 8);
8420 need_pad->size += 8;
8421 }
8422
8423 return TRUE;
8424 }
8425
8426 /* Analyze inline PLT call relocations to see whether calls to locally
8427 defined functions can be converted to direct calls. */
8428
8429 bfd_boolean
8430 ppc64_elf_inline_plt (struct bfd_link_info *info)
8431 {
8432 struct ppc_link_hash_table *htab;
8433 bfd *ibfd;
8434 asection *sec;
8435 bfd_vma low_vma, high_vma, limit;
8436
8437 htab = ppc_hash_table (info);
8438 if (htab == NULL)
8439 return FALSE;
8440
8441 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8442 reduced somewhat to cater for possible stubs that might be added
8443 between the call and its destination. */
8444 if (htab->params->group_size < 0)
8445 {
8446 limit = -htab->params->group_size;
8447 if (limit == 1)
8448 limit = 0x1e00000;
8449 }
8450 else
8451 {
8452 limit = htab->params->group_size;
8453 if (limit == 1)
8454 limit = 0x1c00000;
8455 }
8456
8457 low_vma = -1;
8458 high_vma = 0;
8459 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8460 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8461 {
8462 if (low_vma > sec->vma)
8463 low_vma = sec->vma;
8464 if (high_vma < sec->vma + sec->size)
8465 high_vma = sec->vma + sec->size;
8466 }
8467
8468 /* If a "bl" can reach anywhere in local code sections, then we can
8469 convert all inline PLT sequences to direct calls when the symbol
8470 is local. */
8471 if (high_vma - low_vma < limit)
8472 {
8473 htab->can_convert_all_inline_plt = 1;
8474 return TRUE;
8475 }
8476
8477 /* Otherwise, go looking through relocs for cases where a direct
8478 call won't reach. Mark the symbol on any such reloc to disable
8479 the optimization and keep the PLT entry as it seems likely that
8480 this will be better than creating trampolines. Note that this
8481 will disable the optimization for all inline PLT calls to a
8482 particular symbol, not just those that won't reach. The
8483 difficulty in doing a more precise optimization is that the
8484 linker needs to make a decision depending on whether a
8485 particular R_PPC64_PLTCALL insn can be turned into a direct
8486 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8487 the sequence, and there is nothing that ties those relocs
8488 together except their symbol. */
8489
8490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8491 {
8492 Elf_Internal_Shdr *symtab_hdr;
8493 Elf_Internal_Sym *local_syms;
8494
8495 if (!is_ppc64_elf (ibfd))
8496 continue;
8497
8498 local_syms = NULL;
8499 symtab_hdr = &elf_symtab_hdr (ibfd);
8500
8501 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8502 if (ppc64_elf_section_data (sec)->has_pltcall
8503 && !bfd_is_abs_section (sec->output_section))
8504 {
8505 Elf_Internal_Rela *relstart, *rel, *relend;
8506
8507 /* Read the relocations. */
8508 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8509 info->keep_memory);
8510 if (relstart == NULL)
8511 return FALSE;
8512
8513 relend = relstart + sec->reloc_count;
8514 for (rel = relstart; rel < relend; )
8515 {
8516 enum elf_ppc64_reloc_type r_type;
8517 unsigned long r_symndx;
8518 asection *sym_sec;
8519 struct elf_link_hash_entry *h;
8520 Elf_Internal_Sym *sym;
8521 unsigned char *tls_maskp;
8522
8523 r_type = ELF64_R_TYPE (rel->r_info);
8524 if (r_type != R_PPC64_PLTCALL)
8525 continue;
8526
8527 r_symndx = ELF64_R_SYM (rel->r_info);
8528 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8529 r_symndx, ibfd))
8530 {
8531 if (elf_section_data (sec)->relocs != relstart)
8532 free (relstart);
8533 if (local_syms != NULL
8534 && symtab_hdr->contents != (bfd_byte *) local_syms)
8535 free (local_syms);
8536 return FALSE;
8537 }
8538
8539 if (sym_sec != NULL && sym_sec->output_section != NULL)
8540 {
8541 bfd_vma from, to;
8542 if (h != NULL)
8543 to = h->root.u.def.value;
8544 else
8545 to = sym->st_value;
8546 to += (rel->r_addend
8547 + sym_sec->output_offset
8548 + sym_sec->output_section->vma);
8549 from = (rel->r_offset
8550 + sec->output_offset
8551 + sec->output_section->vma);
8552 if (to - from + limit < 2 * limit)
8553 *tls_maskp &= ~PLT_KEEP;
8554 }
8555 }
8556 if (elf_section_data (sec)->relocs != relstart)
8557 free (relstart);
8558 }
8559
8560 if (local_syms != NULL
8561 && symtab_hdr->contents != (unsigned char *) local_syms)
8562 {
8563 if (!info->keep_memory)
8564 free (local_syms);
8565 else
8566 symtab_hdr->contents = (unsigned char *) local_syms;
8567 }
8568 }
8569
8570 return TRUE;
8571 }
8572
8573 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8574
8575 asection *
8576 ppc64_elf_tls_setup (struct bfd_link_info *info)
8577 {
8578 struct ppc_link_hash_table *htab;
8579
8580 htab = ppc_hash_table (info);
8581 if (htab == NULL)
8582 return NULL;
8583
8584 if (abiversion (info->output_bfd) == 1)
8585 htab->opd_abi = 1;
8586
8587 if (htab->params->no_multi_toc)
8588 htab->do_multi_toc = 0;
8589 else if (!htab->do_multi_toc)
8590 htab->params->no_multi_toc = 1;
8591
8592 /* Default to --no-plt-localentry, as this option can cause problems
8593 with symbol interposition. For example, glibc libpthread.so and
8594 libc.so duplicate many pthread symbols, with a fallback
8595 implementation in libc.so. In some cases the fallback does more
8596 work than the pthread implementation. __pthread_condattr_destroy
8597 is one such symbol: the libpthread.so implementation is
8598 localentry:0 while the libc.so implementation is localentry:8.
8599 An app that "cleverly" uses dlopen to only load necessary
8600 libraries at runtime may omit loading libpthread.so when not
8601 running multi-threaded, which then results in the libc.so
8602 fallback symbols being used and ld.so complaining. Now there
8603 are workarounds in ld (see non_zero_localentry) to detect the
8604 pthread situation, but that may not be the only case where
8605 --plt-localentry can cause trouble. */
8606 if (htab->params->plt_localentry0 < 0)
8607 htab->params->plt_localentry0 = 0;
8608 if (htab->params->plt_localentry0
8609 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8610 FALSE, FALSE, FALSE) == NULL)
8611 _bfd_error_handler
8612 (_("warning: --plt-localentry is especially dangerous without "
8613 "ld.so support to detect ABI violations"));
8614
8615 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8616 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8617 FALSE, FALSE, TRUE));
8618 /* Move dynamic linking info to the function descriptor sym. */
8619 if (htab->tls_get_addr != NULL)
8620 func_desc_adjust (&htab->tls_get_addr->elf, info);
8621 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8622 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8623 FALSE, FALSE, TRUE));
8624 if (htab->params->tls_get_addr_opt)
8625 {
8626 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8627
8628 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8629 FALSE, FALSE, TRUE);
8630 if (opt != NULL)
8631 func_desc_adjust (opt, info);
8632 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8633 FALSE, FALSE, TRUE);
8634 if (opt_fd != NULL
8635 && (opt_fd->root.type == bfd_link_hash_defined
8636 || opt_fd->root.type == bfd_link_hash_defweak))
8637 {
8638 /* If glibc supports an optimized __tls_get_addr call stub,
8639 signalled by the presence of __tls_get_addr_opt, and we'll
8640 be calling __tls_get_addr via a plt call stub, then
8641 make __tls_get_addr point to __tls_get_addr_opt. */
8642 tga_fd = &htab->tls_get_addr_fd->elf;
8643 if (htab->elf.dynamic_sections_created
8644 && tga_fd != NULL
8645 && (tga_fd->type == STT_FUNC
8646 || tga_fd->needs_plt)
8647 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8648 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8649 {
8650 struct plt_entry *ent;
8651
8652 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8653 if (ent->plt.refcount > 0)
8654 break;
8655 if (ent != NULL)
8656 {
8657 tga_fd->root.type = bfd_link_hash_indirect;
8658 tga_fd->root.u.i.link = &opt_fd->root;
8659 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8660 opt_fd->mark = 1;
8661 if (opt_fd->dynindx != -1)
8662 {
8663 /* Use __tls_get_addr_opt in dynamic relocations. */
8664 opt_fd->dynindx = -1;
8665 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8666 opt_fd->dynstr_index);
8667 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8668 return NULL;
8669 }
8670 htab->tls_get_addr_fd
8671 = (struct ppc_link_hash_entry *) opt_fd;
8672 tga = &htab->tls_get_addr->elf;
8673 if (opt != NULL && tga != NULL)
8674 {
8675 tga->root.type = bfd_link_hash_indirect;
8676 tga->root.u.i.link = &opt->root;
8677 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8678 opt->mark = 1;
8679 _bfd_elf_link_hash_hide_symbol (info, opt,
8680 tga->forced_local);
8681 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8682 }
8683 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8684 htab->tls_get_addr_fd->is_func_descriptor = 1;
8685 if (htab->tls_get_addr != NULL)
8686 {
8687 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8688 htab->tls_get_addr->is_func = 1;
8689 }
8690 }
8691 }
8692 }
8693 else if (htab->params->tls_get_addr_opt < 0)
8694 htab->params->tls_get_addr_opt = 0;
8695 }
8696 return _bfd_elf_tls_setup (info->output_bfd, info);
8697 }
8698
8699 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8700 HASH1 or HASH2. */
8701
8702 static bfd_boolean
8703 branch_reloc_hash_match (const bfd *ibfd,
8704 const Elf_Internal_Rela *rel,
8705 const struct ppc_link_hash_entry *hash1,
8706 const struct ppc_link_hash_entry *hash2)
8707 {
8708 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8709 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8710 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8711
8712 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8713 {
8714 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8715 struct elf_link_hash_entry *h;
8716
8717 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8718 h = elf_follow_link (h);
8719 if (h == &hash1->elf || h == &hash2->elf)
8720 return TRUE;
8721 }
8722 return FALSE;
8723 }
8724
8725 /* Run through all the TLS relocs looking for optimization
8726 opportunities. The linker has been hacked (see ppc64elf.em) to do
8727 a preliminary section layout so that we know the TLS segment
8728 offsets. We can't optimize earlier because some optimizations need
8729 to know the tp offset, and we need to optimize before allocating
8730 dynamic relocations. */
8731
8732 bfd_boolean
8733 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8734 {
8735 bfd *ibfd;
8736 asection *sec;
8737 struct ppc_link_hash_table *htab;
8738 unsigned char *toc_ref;
8739 int pass;
8740
8741 if (!bfd_link_executable (info))
8742 return TRUE;
8743
8744 htab = ppc_hash_table (info);
8745 if (htab == NULL)
8746 return FALSE;
8747
8748 /* Make two passes over the relocs. On the first pass, mark toc
8749 entries involved with tls relocs, and check that tls relocs
8750 involved in setting up a tls_get_addr call are indeed followed by
8751 such a call. If they are not, we can't do any tls optimization.
8752 On the second pass twiddle tls_mask flags to notify
8753 relocate_section that optimization can be done, and adjust got
8754 and plt refcounts. */
8755 toc_ref = NULL;
8756 for (pass = 0; pass < 2; ++pass)
8757 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8758 {
8759 Elf_Internal_Sym *locsyms = NULL;
8760 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8761
8762 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8763 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8764 {
8765 Elf_Internal_Rela *relstart, *rel, *relend;
8766 bfd_boolean found_tls_get_addr_arg = 0;
8767
8768 /* Read the relocations. */
8769 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8770 info->keep_memory);
8771 if (relstart == NULL)
8772 {
8773 free (toc_ref);
8774 return FALSE;
8775 }
8776
8777 relend = relstart + sec->reloc_count;
8778 for (rel = relstart; rel < relend; rel++)
8779 {
8780 enum elf_ppc64_reloc_type r_type;
8781 unsigned long r_symndx;
8782 struct elf_link_hash_entry *h;
8783 Elf_Internal_Sym *sym;
8784 asection *sym_sec;
8785 unsigned char *tls_mask;
8786 unsigned char tls_set, tls_clear, tls_type = 0;
8787 bfd_vma value;
8788 bfd_boolean ok_tprel, is_local;
8789 long toc_ref_index = 0;
8790 int expecting_tls_get_addr = 0;
8791 bfd_boolean ret = FALSE;
8792
8793 r_symndx = ELF64_R_SYM (rel->r_info);
8794 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8795 r_symndx, ibfd))
8796 {
8797 err_free_rel:
8798 if (elf_section_data (sec)->relocs != relstart)
8799 free (relstart);
8800 if (toc_ref != NULL)
8801 free (toc_ref);
8802 if (locsyms != NULL
8803 && (elf_symtab_hdr (ibfd).contents
8804 != (unsigned char *) locsyms))
8805 free (locsyms);
8806 return ret;
8807 }
8808
8809 if (h != NULL)
8810 {
8811 if (h->root.type == bfd_link_hash_defined
8812 || h->root.type == bfd_link_hash_defweak)
8813 value = h->root.u.def.value;
8814 else if (h->root.type == bfd_link_hash_undefweak)
8815 value = 0;
8816 else
8817 {
8818 found_tls_get_addr_arg = 0;
8819 continue;
8820 }
8821 }
8822 else
8823 /* Symbols referenced by TLS relocs must be of type
8824 STT_TLS. So no need for .opd local sym adjust. */
8825 value = sym->st_value;
8826
8827 ok_tprel = FALSE;
8828 is_local = FALSE;
8829 if (h == NULL
8830 || !h->def_dynamic)
8831 {
8832 is_local = TRUE;
8833 if (h != NULL
8834 && h->root.type == bfd_link_hash_undefweak)
8835 ok_tprel = TRUE;
8836 else if (sym_sec != NULL
8837 && sym_sec->output_section != NULL)
8838 {
8839 value += sym_sec->output_offset;
8840 value += sym_sec->output_section->vma;
8841 value -= htab->elf.tls_sec->vma;
8842 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8843 < (bfd_vma) 1 << 32);
8844 }
8845 }
8846
8847 r_type = ELF64_R_TYPE (rel->r_info);
8848 /* If this section has old-style __tls_get_addr calls
8849 without marker relocs, then check that each
8850 __tls_get_addr call reloc is preceded by a reloc
8851 that conceivably belongs to the __tls_get_addr arg
8852 setup insn. If we don't find matching arg setup
8853 relocs, don't do any tls optimization. */
8854 if (pass == 0
8855 && sec->has_tls_get_addr_call
8856 && h != NULL
8857 && (h == &htab->tls_get_addr->elf
8858 || h == &htab->tls_get_addr_fd->elf)
8859 && !found_tls_get_addr_arg
8860 && is_branch_reloc (r_type))
8861 {
8862 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8863 "TLS optimization disabled\n"),
8864 ibfd, sec, rel->r_offset);
8865 ret = TRUE;
8866 goto err_free_rel;
8867 }
8868
8869 found_tls_get_addr_arg = 0;
8870 switch (r_type)
8871 {
8872 case R_PPC64_GOT_TLSLD16:
8873 case R_PPC64_GOT_TLSLD16_LO:
8874 expecting_tls_get_addr = 1;
8875 found_tls_get_addr_arg = 1;
8876 /* Fall through. */
8877
8878 case R_PPC64_GOT_TLSLD16_HI:
8879 case R_PPC64_GOT_TLSLD16_HA:
8880 /* These relocs should never be against a symbol
8881 defined in a shared lib. Leave them alone if
8882 that turns out to be the case. */
8883 if (!is_local)
8884 continue;
8885
8886 /* LD -> LE */
8887 tls_set = 0;
8888 tls_clear = TLS_LD;
8889 tls_type = TLS_TLS | TLS_LD;
8890 break;
8891
8892 case R_PPC64_GOT_TLSGD16:
8893 case R_PPC64_GOT_TLSGD16_LO:
8894 expecting_tls_get_addr = 1;
8895 found_tls_get_addr_arg = 1;
8896 /* Fall through. */
8897
8898 case R_PPC64_GOT_TLSGD16_HI:
8899 case R_PPC64_GOT_TLSGD16_HA:
8900 if (ok_tprel)
8901 /* GD -> LE */
8902 tls_set = 0;
8903 else
8904 /* GD -> IE */
8905 tls_set = TLS_TLS | TLS_TPRELGD;
8906 tls_clear = TLS_GD;
8907 tls_type = TLS_TLS | TLS_GD;
8908 break;
8909
8910 case R_PPC64_GOT_TPREL16_DS:
8911 case R_PPC64_GOT_TPREL16_LO_DS:
8912 case R_PPC64_GOT_TPREL16_HI:
8913 case R_PPC64_GOT_TPREL16_HA:
8914 if (ok_tprel)
8915 {
8916 /* IE -> LE */
8917 tls_set = 0;
8918 tls_clear = TLS_TPREL;
8919 tls_type = TLS_TLS | TLS_TPREL;
8920 break;
8921 }
8922 continue;
8923
8924 case R_PPC64_TLSGD:
8925 case R_PPC64_TLSLD:
8926 if (rel + 1 < relend
8927 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8928 {
8929 if (pass != 0
8930 && (ELF64_R_TYPE (rel[1].r_info)
8931 != R_PPC64_PLTSEQ))
8932 {
8933 r_symndx = ELF64_R_SYM (rel[1].r_info);
8934 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8935 r_symndx, ibfd))
8936 goto err_free_rel;
8937 if (h != NULL)
8938 {
8939 struct plt_entry *ent = NULL;
8940
8941 for (ent = h->plt.plist;
8942 ent != NULL;
8943 ent = ent->next)
8944 if (ent->addend == rel[1].r_addend)
8945 break;
8946
8947 if (ent != NULL
8948 && ent->plt.refcount > 0)
8949 ent->plt.refcount -= 1;
8950 }
8951 }
8952 continue;
8953 }
8954 found_tls_get_addr_arg = 1;
8955 /* Fall through. */
8956
8957 case R_PPC64_TLS:
8958 case R_PPC64_TOC16:
8959 case R_PPC64_TOC16_LO:
8960 if (sym_sec == NULL || sym_sec != toc)
8961 continue;
8962
8963 /* Mark this toc entry as referenced by a TLS
8964 code sequence. We can do that now in the
8965 case of R_PPC64_TLS, and after checking for
8966 tls_get_addr for the TOC16 relocs. */
8967 if (toc_ref == NULL)
8968 toc_ref
8969 = bfd_zmalloc (toc->output_section->rawsize / 8);
8970 if (toc_ref == NULL)
8971 goto err_free_rel;
8972
8973 if (h != NULL)
8974 value = h->root.u.def.value;
8975 else
8976 value = sym->st_value;
8977 value += rel->r_addend;
8978 if (value % 8 != 0)
8979 continue;
8980 BFD_ASSERT (value < toc->size
8981 && toc->output_offset % 8 == 0);
8982 toc_ref_index = (value + toc->output_offset) / 8;
8983 if (r_type == R_PPC64_TLS
8984 || r_type == R_PPC64_TLSGD
8985 || r_type == R_PPC64_TLSLD)
8986 {
8987 toc_ref[toc_ref_index] = 1;
8988 continue;
8989 }
8990
8991 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8992 continue;
8993
8994 tls_set = 0;
8995 tls_clear = 0;
8996 expecting_tls_get_addr = 2;
8997 break;
8998
8999 case R_PPC64_TPREL64:
9000 if (pass == 0
9001 || sec != toc
9002 || toc_ref == NULL
9003 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
9004 continue;
9005 if (ok_tprel)
9006 {
9007 /* IE -> LE */
9008 tls_set = TLS_EXPLICIT;
9009 tls_clear = TLS_TPREL;
9010 break;
9011 }
9012 continue;
9013
9014 case R_PPC64_DTPMOD64:
9015 if (pass == 0
9016 || sec != toc
9017 || toc_ref == NULL
9018 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
9019 continue;
9020 if (rel + 1 < relend
9021 && (rel[1].r_info
9022 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
9023 && rel[1].r_offset == rel->r_offset + 8)
9024 {
9025 if (ok_tprel)
9026 /* GD -> LE */
9027 tls_set = TLS_EXPLICIT | TLS_GD;
9028 else
9029 /* GD -> IE */
9030 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
9031 tls_clear = TLS_GD;
9032 }
9033 else
9034 {
9035 if (!is_local)
9036 continue;
9037
9038 /* LD -> LE */
9039 tls_set = TLS_EXPLICIT;
9040 tls_clear = TLS_LD;
9041 }
9042 break;
9043
9044 default:
9045 continue;
9046 }
9047
9048 if (pass == 0)
9049 {
9050 if (!expecting_tls_get_addr
9051 || !sec->has_tls_get_addr_call)
9052 continue;
9053
9054 if (rel + 1 < relend
9055 && branch_reloc_hash_match (ibfd, rel + 1,
9056 htab->tls_get_addr,
9057 htab->tls_get_addr_fd))
9058 {
9059 if (expecting_tls_get_addr == 2)
9060 {
9061 /* Check for toc tls entries. */
9062 unsigned char *toc_tls;
9063 int retval;
9064
9065 retval = get_tls_mask (&toc_tls, NULL, NULL,
9066 &locsyms,
9067 rel, ibfd);
9068 if (retval == 0)
9069 goto err_free_rel;
9070 if (toc_tls != NULL)
9071 {
9072 if ((*toc_tls & TLS_TLS) != 0
9073 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
9074 found_tls_get_addr_arg = 1;
9075 if (retval > 1)
9076 toc_ref[toc_ref_index] = 1;
9077 }
9078 }
9079 continue;
9080 }
9081
9082 /* Uh oh, we didn't find the expected call. We
9083 could just mark this symbol to exclude it
9084 from tls optimization but it's safer to skip
9085 the entire optimization. */
9086 /* xgettext:c-format */
9087 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
9088 "TLS optimization disabled\n"),
9089 ibfd, sec, rel->r_offset);
9090 ret = TRUE;
9091 goto err_free_rel;
9092 }
9093
9094 /* If we don't have old-style __tls_get_addr calls
9095 without TLSGD/TLSLD marker relocs, and we haven't
9096 found a new-style __tls_get_addr call with a
9097 marker for this symbol, then we either have a
9098 broken object file or an -mlongcall style
9099 indirect call to __tls_get_addr without a marker.
9100 Disable optimization in this case. */
9101 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
9102 && (tls_set & TLS_EXPLICIT) == 0
9103 && !sec->has_tls_get_addr_call
9104 && ((*tls_mask & (TLS_TLS | TLS_MARK))
9105 != (TLS_TLS | TLS_MARK)))
9106 continue;
9107
9108 if (expecting_tls_get_addr)
9109 {
9110 struct plt_entry *ent = NULL;
9111
9112 if (htab->tls_get_addr != NULL)
9113 for (ent = htab->tls_get_addr->elf.plt.plist;
9114 ent != NULL;
9115 ent = ent->next)
9116 if (ent->addend == 0)
9117 break;
9118
9119 if (ent == NULL && htab->tls_get_addr_fd != NULL)
9120 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
9121 ent != NULL;
9122 ent = ent->next)
9123 if (ent->addend == 0)
9124 break;
9125
9126 if (ent != NULL
9127 && ent->plt.refcount > 0)
9128 ent->plt.refcount -= 1;
9129 }
9130
9131 if (tls_clear == 0)
9132 continue;
9133
9134 if ((tls_set & TLS_EXPLICIT) == 0)
9135 {
9136 struct got_entry *ent;
9137
9138 /* Adjust got entry for this reloc. */
9139 if (h != NULL)
9140 ent = h->got.glist;
9141 else
9142 ent = elf_local_got_ents (ibfd)[r_symndx];
9143
9144 for (; ent != NULL; ent = ent->next)
9145 if (ent->addend == rel->r_addend
9146 && ent->owner == ibfd
9147 && ent->tls_type == tls_type)
9148 break;
9149 if (ent == NULL)
9150 abort ();
9151
9152 if (tls_set == 0)
9153 {
9154 /* We managed to get rid of a got entry. */
9155 if (ent->got.refcount > 0)
9156 ent->got.refcount -= 1;
9157 }
9158 }
9159 else
9160 {
9161 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9162 we'll lose one or two dyn relocs. */
9163 if (!dec_dynrel_count (rel->r_info, sec, info,
9164 NULL, h, sym))
9165 return FALSE;
9166
9167 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9168 {
9169 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9170 NULL, h, sym))
9171 return FALSE;
9172 }
9173 }
9174
9175 *tls_mask |= tls_set;
9176 *tls_mask &= ~tls_clear;
9177 }
9178
9179 if (elf_section_data (sec)->relocs != relstart)
9180 free (relstart);
9181 }
9182
9183 if (locsyms != NULL
9184 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9185 {
9186 if (!info->keep_memory)
9187 free (locsyms);
9188 else
9189 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9190 }
9191 }
9192
9193 if (toc_ref != NULL)
9194 free (toc_ref);
9195 htab->do_tls_opt = 1;
9196 return TRUE;
9197 }
9198
9199 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9200 the values of any global symbols in a toc section that has been
9201 edited. Globals in toc sections should be a rarity, so this function
9202 sets a flag if any are found in toc sections other than the one just
9203 edited, so that further hash table traversals can be avoided. */
9204
9205 struct adjust_toc_info
9206 {
9207 asection *toc;
9208 unsigned long *skip;
9209 bfd_boolean global_toc_syms;
9210 };
9211
9212 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9213
9214 static bfd_boolean
9215 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9216 {
9217 struct ppc_link_hash_entry *eh;
9218 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9219 unsigned long i;
9220
9221 if (h->root.type != bfd_link_hash_defined
9222 && h->root.type != bfd_link_hash_defweak)
9223 return TRUE;
9224
9225 eh = (struct ppc_link_hash_entry *) h;
9226 if (eh->adjust_done)
9227 return TRUE;
9228
9229 if (eh->elf.root.u.def.section == toc_inf->toc)
9230 {
9231 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9232 i = toc_inf->toc->rawsize >> 3;
9233 else
9234 i = eh->elf.root.u.def.value >> 3;
9235
9236 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9237 {
9238 _bfd_error_handler
9239 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9240 do
9241 ++i;
9242 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9243 eh->elf.root.u.def.value = (bfd_vma) i << 3;
9244 }
9245
9246 eh->elf.root.u.def.value -= toc_inf->skip[i];
9247 eh->adjust_done = 1;
9248 }
9249 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9250 toc_inf->global_toc_syms = TRUE;
9251
9252 return TRUE;
9253 }
9254
9255 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9256 on a _LO variety toc/got reloc. */
9257
9258 static bfd_boolean
9259 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9260 {
9261 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9262 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9263 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9264 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9265 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9266 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9267 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9268 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9269 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9270 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9271 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9272 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9273 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9274 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9275 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9276 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9277 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9278 /* Exclude lfqu by testing reloc. If relocs are ever
9279 defined for the reduced D field in psq_lu then those
9280 will need testing too. */
9281 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9282 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9283 && (insn & 1) == 0)
9284 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9285 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9286 /* Exclude stfqu. psq_stu as above for psq_lu. */
9287 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9288 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9289 && (insn & 1) == 0));
9290 }
9291
9292 /* Examine all relocs referencing .toc sections in order to remove
9293 unused .toc entries. */
9294
9295 bfd_boolean
9296 ppc64_elf_edit_toc (struct bfd_link_info *info)
9297 {
9298 bfd *ibfd;
9299 struct adjust_toc_info toc_inf;
9300 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9301
9302 htab->do_toc_opt = 1;
9303 toc_inf.global_toc_syms = TRUE;
9304 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9305 {
9306 asection *toc, *sec;
9307 Elf_Internal_Shdr *symtab_hdr;
9308 Elf_Internal_Sym *local_syms;
9309 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9310 unsigned long *skip, *drop;
9311 unsigned char *used;
9312 unsigned char *keep, last, some_unused;
9313
9314 if (!is_ppc64_elf (ibfd))
9315 continue;
9316
9317 toc = bfd_get_section_by_name (ibfd, ".toc");
9318 if (toc == NULL
9319 || toc->size == 0
9320 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9321 || discarded_section (toc))
9322 continue;
9323
9324 toc_relocs = NULL;
9325 local_syms = NULL;
9326 symtab_hdr = &elf_symtab_hdr (ibfd);
9327
9328 /* Look at sections dropped from the final link. */
9329 skip = NULL;
9330 relstart = NULL;
9331 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9332 {
9333 if (sec->reloc_count == 0
9334 || !discarded_section (sec)
9335 || get_opd_info (sec)
9336 || (sec->flags & SEC_ALLOC) == 0
9337 || (sec->flags & SEC_DEBUGGING) != 0)
9338 continue;
9339
9340 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9341 if (relstart == NULL)
9342 goto error_ret;
9343
9344 /* Run through the relocs to see which toc entries might be
9345 unused. */
9346 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9347 {
9348 enum elf_ppc64_reloc_type r_type;
9349 unsigned long r_symndx;
9350 asection *sym_sec;
9351 struct elf_link_hash_entry *h;
9352 Elf_Internal_Sym *sym;
9353 bfd_vma val;
9354
9355 r_type = ELF64_R_TYPE (rel->r_info);
9356 switch (r_type)
9357 {
9358 default:
9359 continue;
9360
9361 case R_PPC64_TOC16:
9362 case R_PPC64_TOC16_LO:
9363 case R_PPC64_TOC16_HI:
9364 case R_PPC64_TOC16_HA:
9365 case R_PPC64_TOC16_DS:
9366 case R_PPC64_TOC16_LO_DS:
9367 break;
9368 }
9369
9370 r_symndx = ELF64_R_SYM (rel->r_info);
9371 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9372 r_symndx, ibfd))
9373 goto error_ret;
9374
9375 if (sym_sec != toc)
9376 continue;
9377
9378 if (h != NULL)
9379 val = h->root.u.def.value;
9380 else
9381 val = sym->st_value;
9382 val += rel->r_addend;
9383
9384 if (val >= toc->size)
9385 continue;
9386
9387 /* Anything in the toc ought to be aligned to 8 bytes.
9388 If not, don't mark as unused. */
9389 if (val & 7)
9390 continue;
9391
9392 if (skip == NULL)
9393 {
9394 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9395 if (skip == NULL)
9396 goto error_ret;
9397 }
9398
9399 skip[val >> 3] = ref_from_discarded;
9400 }
9401
9402 if (elf_section_data (sec)->relocs != relstart)
9403 free (relstart);
9404 }
9405
9406 /* For largetoc loads of address constants, we can convert
9407 . addis rx,2,addr@got@ha
9408 . ld ry,addr@got@l(rx)
9409 to
9410 . addis rx,2,addr@toc@ha
9411 . addi ry,rx,addr@toc@l
9412 when addr is within 2G of the toc pointer. This then means
9413 that the word storing "addr" in the toc is no longer needed. */
9414
9415 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9416 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9417 && toc->reloc_count != 0)
9418 {
9419 /* Read toc relocs. */
9420 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9421 info->keep_memory);
9422 if (toc_relocs == NULL)
9423 goto error_ret;
9424
9425 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9426 {
9427 enum elf_ppc64_reloc_type r_type;
9428 unsigned long r_symndx;
9429 asection *sym_sec;
9430 struct elf_link_hash_entry *h;
9431 Elf_Internal_Sym *sym;
9432 bfd_vma val, addr;
9433
9434 r_type = ELF64_R_TYPE (rel->r_info);
9435 if (r_type != R_PPC64_ADDR64)
9436 continue;
9437
9438 r_symndx = ELF64_R_SYM (rel->r_info);
9439 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9440 r_symndx, ibfd))
9441 goto error_ret;
9442
9443 if (sym_sec == NULL
9444 || sym_sec->output_section == NULL
9445 || discarded_section (sym_sec))
9446 continue;
9447
9448 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9449 continue;
9450
9451 if (h != NULL)
9452 {
9453 if (h->type == STT_GNU_IFUNC)
9454 continue;
9455 val = h->root.u.def.value;
9456 }
9457 else
9458 {
9459 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9460 continue;
9461 val = sym->st_value;
9462 }
9463 val += rel->r_addend;
9464 val += sym_sec->output_section->vma + sym_sec->output_offset;
9465
9466 /* We don't yet know the exact toc pointer value, but we
9467 know it will be somewhere in the toc section. Don't
9468 optimize if the difference from any possible toc
9469 pointer is outside [ff..f80008000, 7fff7fff]. */
9470 addr = toc->output_section->vma + TOC_BASE_OFF;
9471 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9472 continue;
9473
9474 addr = toc->output_section->vma + toc->output_section->rawsize;
9475 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9476 continue;
9477
9478 if (skip == NULL)
9479 {
9480 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9481 if (skip == NULL)
9482 goto error_ret;
9483 }
9484
9485 skip[rel->r_offset >> 3]
9486 |= can_optimize | ((rel - toc_relocs) << 2);
9487 }
9488 }
9489
9490 if (skip == NULL)
9491 continue;
9492
9493 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9494 if (used == NULL)
9495 {
9496 error_ret:
9497 if (local_syms != NULL
9498 && symtab_hdr->contents != (unsigned char *) local_syms)
9499 free (local_syms);
9500 if (sec != NULL
9501 && relstart != NULL
9502 && elf_section_data (sec)->relocs != relstart)
9503 free (relstart);
9504 if (toc_relocs != NULL
9505 && elf_section_data (toc)->relocs != toc_relocs)
9506 free (toc_relocs);
9507 if (skip != NULL)
9508 free (skip);
9509 return FALSE;
9510 }
9511
9512 /* Now check all kept sections that might reference the toc.
9513 Check the toc itself last. */
9514 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9515 : ibfd->sections);
9516 sec != NULL;
9517 sec = (sec == toc ? NULL
9518 : sec->next == NULL ? toc
9519 : sec->next == toc && toc->next ? toc->next
9520 : sec->next))
9521 {
9522 int repeat;
9523
9524 if (sec->reloc_count == 0
9525 || discarded_section (sec)
9526 || get_opd_info (sec)
9527 || (sec->flags & SEC_ALLOC) == 0
9528 || (sec->flags & SEC_DEBUGGING) != 0)
9529 continue;
9530
9531 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9532 info->keep_memory);
9533 if (relstart == NULL)
9534 {
9535 free (used);
9536 goto error_ret;
9537 }
9538
9539 /* Mark toc entries referenced as used. */
9540 do
9541 {
9542 repeat = 0;
9543 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9544 {
9545 enum elf_ppc64_reloc_type r_type;
9546 unsigned long r_symndx;
9547 asection *sym_sec;
9548 struct elf_link_hash_entry *h;
9549 Elf_Internal_Sym *sym;
9550 bfd_vma val;
9551 enum {no_check, check_lo, check_ha} insn_check;
9552
9553 r_type = ELF64_R_TYPE (rel->r_info);
9554 switch (r_type)
9555 {
9556 default:
9557 insn_check = no_check;
9558 break;
9559
9560 case R_PPC64_GOT_TLSLD16_HA:
9561 case R_PPC64_GOT_TLSGD16_HA:
9562 case R_PPC64_GOT_TPREL16_HA:
9563 case R_PPC64_GOT_DTPREL16_HA:
9564 case R_PPC64_GOT16_HA:
9565 case R_PPC64_TOC16_HA:
9566 insn_check = check_ha;
9567 break;
9568
9569 case R_PPC64_GOT_TLSLD16_LO:
9570 case R_PPC64_GOT_TLSGD16_LO:
9571 case R_PPC64_GOT_TPREL16_LO_DS:
9572 case R_PPC64_GOT_DTPREL16_LO_DS:
9573 case R_PPC64_GOT16_LO:
9574 case R_PPC64_GOT16_LO_DS:
9575 case R_PPC64_TOC16_LO:
9576 case R_PPC64_TOC16_LO_DS:
9577 insn_check = check_lo;
9578 break;
9579 }
9580
9581 if (insn_check != no_check)
9582 {
9583 bfd_vma off = rel->r_offset & ~3;
9584 unsigned char buf[4];
9585 unsigned int insn;
9586
9587 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9588 {
9589 free (used);
9590 goto error_ret;
9591 }
9592 insn = bfd_get_32 (ibfd, buf);
9593 if (insn_check == check_lo
9594 ? !ok_lo_toc_insn (insn, r_type)
9595 : ((insn & ((0x3f << 26) | 0x1f << 16))
9596 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9597 {
9598 char str[12];
9599
9600 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9601 sprintf (str, "%#08x", insn);
9602 info->callbacks->einfo
9603 /* xgettext:c-format */
9604 (_("%H: toc optimization is not supported for"
9605 " %s instruction\n"),
9606 ibfd, sec, rel->r_offset & ~3, str);
9607 }
9608 }
9609
9610 switch (r_type)
9611 {
9612 case R_PPC64_TOC16:
9613 case R_PPC64_TOC16_LO:
9614 case R_PPC64_TOC16_HI:
9615 case R_PPC64_TOC16_HA:
9616 case R_PPC64_TOC16_DS:
9617 case R_PPC64_TOC16_LO_DS:
9618 /* In case we're taking addresses of toc entries. */
9619 case R_PPC64_ADDR64:
9620 break;
9621
9622 default:
9623 continue;
9624 }
9625
9626 r_symndx = ELF64_R_SYM (rel->r_info);
9627 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9628 r_symndx, ibfd))
9629 {
9630 free (used);
9631 goto error_ret;
9632 }
9633
9634 if (sym_sec != toc)
9635 continue;
9636
9637 if (h != NULL)
9638 val = h->root.u.def.value;
9639 else
9640 val = sym->st_value;
9641 val += rel->r_addend;
9642
9643 if (val >= toc->size)
9644 continue;
9645
9646 if ((skip[val >> 3] & can_optimize) != 0)
9647 {
9648 bfd_vma off;
9649 unsigned char opc;
9650
9651 switch (r_type)
9652 {
9653 case R_PPC64_TOC16_HA:
9654 break;
9655
9656 case R_PPC64_TOC16_LO_DS:
9657 off = rel->r_offset;
9658 off += (bfd_big_endian (ibfd) ? -2 : 3);
9659 if (!bfd_get_section_contents (ibfd, sec, &opc,
9660 off, 1))
9661 {
9662 free (used);
9663 goto error_ret;
9664 }
9665 if ((opc & (0x3f << 2)) == (58u << 2))
9666 break;
9667 /* Fall through. */
9668
9669 default:
9670 /* Wrong sort of reloc, or not a ld. We may
9671 as well clear ref_from_discarded too. */
9672 skip[val >> 3] = 0;
9673 }
9674 }
9675
9676 if (sec != toc)
9677 used[val >> 3] = 1;
9678 /* For the toc section, we only mark as used if this
9679 entry itself isn't unused. */
9680 else if ((used[rel->r_offset >> 3]
9681 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9682 && !used[val >> 3])
9683 {
9684 /* Do all the relocs again, to catch reference
9685 chains. */
9686 repeat = 1;
9687 used[val >> 3] = 1;
9688 }
9689 }
9690 }
9691 while (repeat);
9692
9693 if (elf_section_data (sec)->relocs != relstart)
9694 free (relstart);
9695 }
9696
9697 /* Merge the used and skip arrays. Assume that TOC
9698 doublewords not appearing as either used or unused belong
9699 to an entry more than one doubleword in size. */
9700 for (drop = skip, keep = used, last = 0, some_unused = 0;
9701 drop < skip + (toc->size + 7) / 8;
9702 ++drop, ++keep)
9703 {
9704 if (*keep)
9705 {
9706 *drop &= ~ref_from_discarded;
9707 if ((*drop & can_optimize) != 0)
9708 some_unused = 1;
9709 last = 0;
9710 }
9711 else if ((*drop & ref_from_discarded) != 0)
9712 {
9713 some_unused = 1;
9714 last = ref_from_discarded;
9715 }
9716 else
9717 *drop = last;
9718 }
9719
9720 free (used);
9721
9722 if (some_unused)
9723 {
9724 bfd_byte *contents, *src;
9725 unsigned long off;
9726 Elf_Internal_Sym *sym;
9727 bfd_boolean local_toc_syms = FALSE;
9728
9729 /* Shuffle the toc contents, and at the same time convert the
9730 skip array from booleans into offsets. */
9731 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9732 goto error_ret;
9733
9734 elf_section_data (toc)->this_hdr.contents = contents;
9735
9736 for (src = contents, off = 0, drop = skip;
9737 src < contents + toc->size;
9738 src += 8, ++drop)
9739 {
9740 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9741 off += 8;
9742 else if (off != 0)
9743 {
9744 *drop = off;
9745 memcpy (src - off, src, 8);
9746 }
9747 }
9748 *drop = off;
9749 toc->rawsize = toc->size;
9750 toc->size = src - contents - off;
9751
9752 /* Adjust addends for relocs against the toc section sym,
9753 and optimize any accesses we can. */
9754 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9755 {
9756 if (sec->reloc_count == 0
9757 || discarded_section (sec))
9758 continue;
9759
9760 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9761 info->keep_memory);
9762 if (relstart == NULL)
9763 goto error_ret;
9764
9765 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9766 {
9767 enum elf_ppc64_reloc_type r_type;
9768 unsigned long r_symndx;
9769 asection *sym_sec;
9770 struct elf_link_hash_entry *h;
9771 bfd_vma val;
9772
9773 r_type = ELF64_R_TYPE (rel->r_info);
9774 switch (r_type)
9775 {
9776 default:
9777 continue;
9778
9779 case R_PPC64_TOC16:
9780 case R_PPC64_TOC16_LO:
9781 case R_PPC64_TOC16_HI:
9782 case R_PPC64_TOC16_HA:
9783 case R_PPC64_TOC16_DS:
9784 case R_PPC64_TOC16_LO_DS:
9785 case R_PPC64_ADDR64:
9786 break;
9787 }
9788
9789 r_symndx = ELF64_R_SYM (rel->r_info);
9790 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9791 r_symndx, ibfd))
9792 goto error_ret;
9793
9794 if (sym_sec != toc)
9795 continue;
9796
9797 if (h != NULL)
9798 val = h->root.u.def.value;
9799 else
9800 {
9801 val = sym->st_value;
9802 if (val != 0)
9803 local_toc_syms = TRUE;
9804 }
9805
9806 val += rel->r_addend;
9807
9808 if (val > toc->rawsize)
9809 val = toc->rawsize;
9810 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9811 continue;
9812 else if ((skip[val >> 3] & can_optimize) != 0)
9813 {
9814 Elf_Internal_Rela *tocrel
9815 = toc_relocs + (skip[val >> 3] >> 2);
9816 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9817
9818 switch (r_type)
9819 {
9820 case R_PPC64_TOC16_HA:
9821 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9822 break;
9823
9824 case R_PPC64_TOC16_LO_DS:
9825 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9826 break;
9827
9828 default:
9829 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9830 ppc_howto_init ();
9831 info->callbacks->einfo
9832 /* xgettext:c-format */
9833 (_("%H: %s references "
9834 "optimized away TOC entry\n"),
9835 ibfd, sec, rel->r_offset,
9836 ppc64_elf_howto_table[r_type]->name);
9837 bfd_set_error (bfd_error_bad_value);
9838 goto error_ret;
9839 }
9840 rel->r_addend = tocrel->r_addend;
9841 elf_section_data (sec)->relocs = relstart;
9842 continue;
9843 }
9844
9845 if (h != NULL || sym->st_value != 0)
9846 continue;
9847
9848 rel->r_addend -= skip[val >> 3];
9849 elf_section_data (sec)->relocs = relstart;
9850 }
9851
9852 if (elf_section_data (sec)->relocs != relstart)
9853 free (relstart);
9854 }
9855
9856 /* We shouldn't have local or global symbols defined in the TOC,
9857 but handle them anyway. */
9858 if (local_syms != NULL)
9859 for (sym = local_syms;
9860 sym < local_syms + symtab_hdr->sh_info;
9861 ++sym)
9862 if (sym->st_value != 0
9863 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9864 {
9865 unsigned long i;
9866
9867 if (sym->st_value > toc->rawsize)
9868 i = toc->rawsize >> 3;
9869 else
9870 i = sym->st_value >> 3;
9871
9872 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9873 {
9874 if (local_toc_syms)
9875 _bfd_error_handler
9876 (_("%s defined on removed toc entry"),
9877 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9878 do
9879 ++i;
9880 while ((skip[i] & (ref_from_discarded | can_optimize)));
9881 sym->st_value = (bfd_vma) i << 3;
9882 }
9883
9884 sym->st_value -= skip[i];
9885 symtab_hdr->contents = (unsigned char *) local_syms;
9886 }
9887
9888 /* Adjust any global syms defined in this toc input section. */
9889 if (toc_inf.global_toc_syms)
9890 {
9891 toc_inf.toc = toc;
9892 toc_inf.skip = skip;
9893 toc_inf.global_toc_syms = FALSE;
9894 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9895 &toc_inf);
9896 }
9897
9898 if (toc->reloc_count != 0)
9899 {
9900 Elf_Internal_Shdr *rel_hdr;
9901 Elf_Internal_Rela *wrel;
9902 bfd_size_type sz;
9903
9904 /* Remove unused toc relocs, and adjust those we keep. */
9905 if (toc_relocs == NULL)
9906 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9907 info->keep_memory);
9908 if (toc_relocs == NULL)
9909 goto error_ret;
9910
9911 wrel = toc_relocs;
9912 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9913 if ((skip[rel->r_offset >> 3]
9914 & (ref_from_discarded | can_optimize)) == 0)
9915 {
9916 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9917 wrel->r_info = rel->r_info;
9918 wrel->r_addend = rel->r_addend;
9919 ++wrel;
9920 }
9921 else if (!dec_dynrel_count (rel->r_info, toc, info,
9922 &local_syms, NULL, NULL))
9923 goto error_ret;
9924
9925 elf_section_data (toc)->relocs = toc_relocs;
9926 toc->reloc_count = wrel - toc_relocs;
9927 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9928 sz = rel_hdr->sh_entsize;
9929 rel_hdr->sh_size = toc->reloc_count * sz;
9930 }
9931 }
9932 else if (toc_relocs != NULL
9933 && elf_section_data (toc)->relocs != toc_relocs)
9934 free (toc_relocs);
9935
9936 if (local_syms != NULL
9937 && symtab_hdr->contents != (unsigned char *) local_syms)
9938 {
9939 if (!info->keep_memory)
9940 free (local_syms);
9941 else
9942 symtab_hdr->contents = (unsigned char *) local_syms;
9943 }
9944 free (skip);
9945 }
9946
9947 return TRUE;
9948 }
9949
9950 /* Return true iff input section I references the TOC using
9951 instructions limited to +/-32k offsets. */
9952
9953 bfd_boolean
9954 ppc64_elf_has_small_toc_reloc (asection *i)
9955 {
9956 return (is_ppc64_elf (i->owner)
9957 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9958 }
9959
9960 /* Allocate space for one GOT entry. */
9961
9962 static void
9963 allocate_got (struct elf_link_hash_entry *h,
9964 struct bfd_link_info *info,
9965 struct got_entry *gent)
9966 {
9967 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9968 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9969 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9970 ? 16 : 8);
9971 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9972 ? 2 : 1) * sizeof (Elf64_External_Rela);
9973 asection *got = ppc64_elf_tdata (gent->owner)->got;
9974
9975 gent->got.offset = got->size;
9976 got->size += entsize;
9977
9978 if (h->type == STT_GNU_IFUNC)
9979 {
9980 htab->elf.irelplt->size += rentsize;
9981 htab->got_reli_size += rentsize;
9982 }
9983 else if (((bfd_link_pic (info)
9984 && !((gent->tls_type & TLS_TPREL) != 0
9985 && bfd_link_executable (info)
9986 && SYMBOL_REFERENCES_LOCAL (info, h)))
9987 || (htab->elf.dynamic_sections_created
9988 && h->dynindx != -1
9989 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9990 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9991 {
9992 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9993 relgot->size += rentsize;
9994 }
9995 }
9996
9997 /* This function merges got entries in the same toc group. */
9998
9999 static void
10000 merge_got_entries (struct got_entry **pent)
10001 {
10002 struct got_entry *ent, *ent2;
10003
10004 for (ent = *pent; ent != NULL; ent = ent->next)
10005 if (!ent->is_indirect)
10006 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
10007 if (!ent2->is_indirect
10008 && ent2->addend == ent->addend
10009 && ent2->tls_type == ent->tls_type
10010 && elf_gp (ent2->owner) == elf_gp (ent->owner))
10011 {
10012 ent2->is_indirect = TRUE;
10013 ent2->got.ent = ent;
10014 }
10015 }
10016
10017 /* If H is undefined, make it dynamic if that makes sense. */
10018
10019 static bfd_boolean
10020 ensure_undef_dynamic (struct bfd_link_info *info,
10021 struct elf_link_hash_entry *h)
10022 {
10023 struct elf_link_hash_table *htab = elf_hash_table (info);
10024
10025 if (htab->dynamic_sections_created
10026 && ((info->dynamic_undefined_weak != 0
10027 && h->root.type == bfd_link_hash_undefweak)
10028 || h->root.type == bfd_link_hash_undefined)
10029 && h->dynindx == -1
10030 && !h->forced_local
10031 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
10032 return bfd_elf_link_record_dynamic_symbol (info, h);
10033 return TRUE;
10034 }
10035
10036 /* Allocate space in .plt, .got and associated reloc sections for
10037 dynamic relocs. */
10038
10039 static bfd_boolean
10040 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
10041 {
10042 struct bfd_link_info *info;
10043 struct ppc_link_hash_table *htab;
10044 asection *s;
10045 struct ppc_link_hash_entry *eh;
10046 struct got_entry **pgent, *gent;
10047
10048 if (h->root.type == bfd_link_hash_indirect)
10049 return TRUE;
10050
10051 info = (struct bfd_link_info *) inf;
10052 htab = ppc_hash_table (info);
10053 if (htab == NULL)
10054 return FALSE;
10055
10056 eh = (struct ppc_link_hash_entry *) h;
10057 /* Run through the TLS GD got entries first if we're changing them
10058 to TPREL. */
10059 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
10060 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10061 if (gent->got.refcount > 0
10062 && (gent->tls_type & TLS_GD) != 0)
10063 {
10064 /* This was a GD entry that has been converted to TPREL. If
10065 there happens to be a TPREL entry we can use that one. */
10066 struct got_entry *ent;
10067 for (ent = h->got.glist; ent != NULL; ent = ent->next)
10068 if (ent->got.refcount > 0
10069 && (ent->tls_type & TLS_TPREL) != 0
10070 && ent->addend == gent->addend
10071 && ent->owner == gent->owner)
10072 {
10073 gent->got.refcount = 0;
10074 break;
10075 }
10076
10077 /* If not, then we'll be using our own TPREL entry. */
10078 if (gent->got.refcount != 0)
10079 gent->tls_type = TLS_TLS | TLS_TPREL;
10080 }
10081
10082 /* Remove any list entry that won't generate a word in the GOT before
10083 we call merge_got_entries. Otherwise we risk merging to empty
10084 entries. */
10085 pgent = &h->got.glist;
10086 while ((gent = *pgent) != NULL)
10087 if (gent->got.refcount > 0)
10088 {
10089 if ((gent->tls_type & TLS_LD) != 0
10090 && !h->def_dynamic)
10091 {
10092 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
10093 *pgent = gent->next;
10094 }
10095 else
10096 pgent = &gent->next;
10097 }
10098 else
10099 *pgent = gent->next;
10100
10101 if (!htab->do_multi_toc)
10102 merge_got_entries (&h->got.glist);
10103
10104 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10105 if (!gent->is_indirect)
10106 {
10107 /* Make sure this symbol is output as a dynamic symbol. */
10108 if (!ensure_undef_dynamic (info, h))
10109 return FALSE;
10110
10111 if (!is_ppc64_elf (gent->owner))
10112 abort ();
10113
10114 allocate_got (h, info, gent);
10115 }
10116
10117 /* If no dynamic sections we can't have dynamic relocs, except for
10118 IFUNCs which are handled even in static executables. */
10119 if (!htab->elf.dynamic_sections_created
10120 && h->type != STT_GNU_IFUNC)
10121 eh->dyn_relocs = NULL;
10122
10123 /* Discard relocs on undefined symbols that must be local. */
10124 else if (h->root.type == bfd_link_hash_undefined
10125 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10126 eh->dyn_relocs = NULL;
10127
10128 /* Also discard relocs on undefined weak syms with non-default
10129 visibility, or when dynamic_undefined_weak says so. */
10130 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10131 eh->dyn_relocs = NULL;
10132
10133 if (eh->dyn_relocs != NULL)
10134 {
10135 struct elf_dyn_relocs *p, **pp;
10136
10137 /* In the shared -Bsymbolic case, discard space allocated for
10138 dynamic pc-relative relocs against symbols which turn out to
10139 be defined in regular objects. For the normal shared case,
10140 discard space for relocs that have become local due to symbol
10141 visibility changes. */
10142
10143 if (bfd_link_pic (info))
10144 {
10145 /* Relocs that use pc_count are those that appear on a call
10146 insn, or certain REL relocs (see must_be_dyn_reloc) that
10147 can be generated via assembly. We want calls to
10148 protected symbols to resolve directly to the function
10149 rather than going via the plt. If people want function
10150 pointer comparisons to work as expected then they should
10151 avoid writing weird assembly. */
10152 if (SYMBOL_CALLS_LOCAL (info, h))
10153 {
10154 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10155 {
10156 p->count -= p->pc_count;
10157 p->pc_count = 0;
10158 if (p->count == 0)
10159 *pp = p->next;
10160 else
10161 pp = &p->next;
10162 }
10163 }
10164
10165 if (eh->dyn_relocs != NULL)
10166 {
10167 /* Make sure this symbol is output as a dynamic symbol. */
10168 if (!ensure_undef_dynamic (info, h))
10169 return FALSE;
10170 }
10171 }
10172 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10173 {
10174 /* For the non-pic case, discard space for relocs against
10175 symbols which turn out to need copy relocs or are not
10176 dynamic. */
10177 if (h->dynamic_adjusted
10178 && !h->def_regular
10179 && !ELF_COMMON_DEF_P (h))
10180 {
10181 /* Make sure this symbol is output as a dynamic symbol. */
10182 if (!ensure_undef_dynamic (info, h))
10183 return FALSE;
10184
10185 if (h->dynindx == -1)
10186 eh->dyn_relocs = NULL;
10187 }
10188 else
10189 eh->dyn_relocs = NULL;
10190 }
10191
10192 /* Finally, allocate space. */
10193 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10194 {
10195 asection *sreloc = elf_section_data (p->sec)->sreloc;
10196 if (eh->elf.type == STT_GNU_IFUNC)
10197 sreloc = htab->elf.irelplt;
10198 sreloc->size += p->count * sizeof (Elf64_External_Rela);
10199 }
10200 }
10201
10202 /* We might need a PLT entry when the symbol
10203 a) is dynamic, or
10204 b) is an ifunc, or
10205 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10206 d) has plt16 relocs and we are linking statically. */
10207 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10208 || h->type == STT_GNU_IFUNC
10209 || (h->needs_plt && h->dynamic_adjusted)
10210 || (h->needs_plt
10211 && h->def_regular
10212 && !htab->elf.dynamic_sections_created
10213 && !htab->can_convert_all_inline_plt
10214 && (((struct ppc_link_hash_entry *) h)->tls_mask
10215 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10216 {
10217 struct plt_entry *pent;
10218 bfd_boolean doneone = FALSE;
10219 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10220 if (pent->plt.refcount > 0)
10221 {
10222 if (!htab->elf.dynamic_sections_created
10223 || h->dynindx == -1)
10224 {
10225 if (h->type == STT_GNU_IFUNC)
10226 {
10227 s = htab->elf.iplt;
10228 pent->plt.offset = s->size;
10229 s->size += PLT_ENTRY_SIZE (htab);
10230 s = htab->elf.irelplt;
10231 }
10232 else
10233 {
10234 s = htab->pltlocal;
10235 pent->plt.offset = s->size;
10236 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10237 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10238 }
10239 }
10240 else
10241 {
10242 /* If this is the first .plt entry, make room for the special
10243 first entry. */
10244 s = htab->elf.splt;
10245 if (s->size == 0)
10246 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10247
10248 pent->plt.offset = s->size;
10249
10250 /* Make room for this entry. */
10251 s->size += PLT_ENTRY_SIZE (htab);
10252
10253 /* Make room for the .glink code. */
10254 s = htab->glink;
10255 if (s->size == 0)
10256 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10257 if (htab->opd_abi)
10258 {
10259 /* We need bigger stubs past index 32767. */
10260 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10261 s->size += 4;
10262 s->size += 2*4;
10263 }
10264 else
10265 s->size += 4;
10266
10267 /* We also need to make an entry in the .rela.plt section. */
10268 s = htab->elf.srelplt;
10269 }
10270 if (s != NULL)
10271 s->size += sizeof (Elf64_External_Rela);
10272 doneone = TRUE;
10273 }
10274 else
10275 pent->plt.offset = (bfd_vma) -1;
10276 if (!doneone)
10277 {
10278 h->plt.plist = NULL;
10279 h->needs_plt = 0;
10280 }
10281 }
10282 else
10283 {
10284 h->plt.plist = NULL;
10285 h->needs_plt = 0;
10286 }
10287
10288 return TRUE;
10289 }
10290
10291 #define PPC_LO(v) ((v) & 0xffff)
10292 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10293 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10294
10295 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10296 to set up space for global entry stubs. These are put in glink,
10297 after the branch table. */
10298
10299 static bfd_boolean
10300 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10301 {
10302 struct bfd_link_info *info;
10303 struct ppc_link_hash_table *htab;
10304 struct plt_entry *pent;
10305 asection *s, *plt;
10306
10307 if (h->root.type == bfd_link_hash_indirect)
10308 return TRUE;
10309
10310 if (!h->pointer_equality_needed)
10311 return TRUE;
10312
10313 if (h->def_regular)
10314 return TRUE;
10315
10316 info = inf;
10317 htab = ppc_hash_table (info);
10318 if (htab == NULL)
10319 return FALSE;
10320
10321 s = htab->global_entry;
10322 plt = htab->elf.splt;
10323 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10324 if (pent->plt.offset != (bfd_vma) -1
10325 && pent->addend == 0)
10326 {
10327 /* For ELFv2, if this symbol is not defined in a regular file
10328 and we are not generating a shared library or pie, then we
10329 need to define the symbol in the executable on a call stub.
10330 This is to avoid text relocations. */
10331 bfd_vma off, stub_align, stub_off, stub_size;
10332 unsigned int align_power;
10333
10334 stub_size = 16;
10335 stub_off = s->size;
10336 if (htab->params->plt_stub_align >= 0)
10337 align_power = htab->params->plt_stub_align;
10338 else
10339 align_power = -htab->params->plt_stub_align;
10340 /* Setting section alignment is delayed until we know it is
10341 non-empty. Otherwise the .text output section will be
10342 aligned at least to plt_stub_align even when no global
10343 entry stubs are needed. */
10344 if (s->alignment_power < align_power)
10345 s->alignment_power = align_power;
10346 stub_align = (bfd_vma) 1 << align_power;
10347 if (htab->params->plt_stub_align >= 0
10348 || ((((stub_off + stub_size - 1) & -stub_align)
10349 - (stub_off & -stub_align))
10350 > ((stub_size - 1) & -stub_align)))
10351 stub_off = (stub_off + stub_align - 1) & -stub_align;
10352 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10353 off -= stub_off + s->output_offset + s->output_section->vma;
10354 /* Note that for --plt-stub-align negative we have a possible
10355 dependency between stub offset and size. Break that
10356 dependency by assuming the max stub size when calculating
10357 the stub offset. */
10358 if (PPC_HA (off) == 0)
10359 stub_size -= 4;
10360 h->root.type = bfd_link_hash_defined;
10361 h->root.u.def.section = s;
10362 h->root.u.def.value = stub_off;
10363 s->size = stub_off + stub_size;
10364 break;
10365 }
10366 return TRUE;
10367 }
10368
10369 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10370 read-only sections. */
10371
10372 static bfd_boolean
10373 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10374 {
10375 asection *sec;
10376
10377 if (h->root.type == bfd_link_hash_indirect)
10378 return TRUE;
10379
10380 sec = readonly_dynrelocs (h);
10381 if (sec != NULL)
10382 {
10383 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10384
10385 info->flags |= DF_TEXTREL;
10386 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT'"
10387 " in read-only section `%pA'\n"),
10388 sec->owner, h->root.root.string, sec);
10389
10390 /* Not an error, just cut short the traversal. */
10391 return FALSE;
10392 }
10393 return TRUE;
10394 }
10395
10396 /* Set the sizes of the dynamic sections. */
10397
10398 static bfd_boolean
10399 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10400 struct bfd_link_info *info)
10401 {
10402 struct ppc_link_hash_table *htab;
10403 bfd *dynobj;
10404 asection *s;
10405 bfd_boolean relocs;
10406 bfd *ibfd;
10407 struct got_entry *first_tlsld;
10408
10409 htab = ppc_hash_table (info);
10410 if (htab == NULL)
10411 return FALSE;
10412
10413 dynobj = htab->elf.dynobj;
10414 if (dynobj == NULL)
10415 abort ();
10416
10417 if (htab->elf.dynamic_sections_created)
10418 {
10419 /* Set the contents of the .interp section to the interpreter. */
10420 if (bfd_link_executable (info) && !info->nointerp)
10421 {
10422 s = bfd_get_linker_section (dynobj, ".interp");
10423 if (s == NULL)
10424 abort ();
10425 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10426 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10427 }
10428 }
10429
10430 /* Set up .got offsets for local syms, and space for local dynamic
10431 relocs. */
10432 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10433 {
10434 struct got_entry **lgot_ents;
10435 struct got_entry **end_lgot_ents;
10436 struct plt_entry **local_plt;
10437 struct plt_entry **end_local_plt;
10438 unsigned char *lgot_masks;
10439 bfd_size_type locsymcount;
10440 Elf_Internal_Shdr *symtab_hdr;
10441
10442 if (!is_ppc64_elf (ibfd))
10443 continue;
10444
10445 for (s = ibfd->sections; s != NULL; s = s->next)
10446 {
10447 struct ppc_dyn_relocs *p;
10448
10449 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10450 {
10451 if (!bfd_is_abs_section (p->sec)
10452 && bfd_is_abs_section (p->sec->output_section))
10453 {
10454 /* Input section has been discarded, either because
10455 it is a copy of a linkonce section or due to
10456 linker script /DISCARD/, so we'll be discarding
10457 the relocs too. */
10458 }
10459 else if (p->count != 0)
10460 {
10461 asection *srel = elf_section_data (p->sec)->sreloc;
10462 if (p->ifunc)
10463 srel = htab->elf.irelplt;
10464 srel->size += p->count * sizeof (Elf64_External_Rela);
10465 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10466 info->flags |= DF_TEXTREL;
10467 }
10468 }
10469 }
10470
10471 lgot_ents = elf_local_got_ents (ibfd);
10472 if (!lgot_ents)
10473 continue;
10474
10475 symtab_hdr = &elf_symtab_hdr (ibfd);
10476 locsymcount = symtab_hdr->sh_info;
10477 end_lgot_ents = lgot_ents + locsymcount;
10478 local_plt = (struct plt_entry **) end_lgot_ents;
10479 end_local_plt = local_plt + locsymcount;
10480 lgot_masks = (unsigned char *) end_local_plt;
10481 s = ppc64_elf_tdata (ibfd)->got;
10482 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10483 {
10484 struct got_entry **pent, *ent;
10485
10486 pent = lgot_ents;
10487 while ((ent = *pent) != NULL)
10488 if (ent->got.refcount > 0)
10489 {
10490 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10491 {
10492 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10493 *pent = ent->next;
10494 }
10495 else
10496 {
10497 unsigned int ent_size = 8;
10498 unsigned int rel_size = sizeof (Elf64_External_Rela);
10499
10500 ent->got.offset = s->size;
10501 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10502 {
10503 ent_size *= 2;
10504 rel_size *= 2;
10505 }
10506 s->size += ent_size;
10507 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10508 {
10509 htab->elf.irelplt->size += rel_size;
10510 htab->got_reli_size += rel_size;
10511 }
10512 else if (bfd_link_pic (info)
10513 && !((ent->tls_type & TLS_TPREL) != 0
10514 && bfd_link_executable (info)))
10515 {
10516 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10517 srel->size += rel_size;
10518 }
10519 pent = &ent->next;
10520 }
10521 }
10522 else
10523 *pent = ent->next;
10524 }
10525
10526 /* Allocate space for plt calls to local syms. */
10527 lgot_masks = (unsigned char *) end_local_plt;
10528 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10529 {
10530 struct plt_entry *ent;
10531
10532 for (ent = *local_plt; ent != NULL; ent = ent->next)
10533 if (ent->plt.refcount > 0)
10534 {
10535 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10536 {
10537 s = htab->elf.iplt;
10538 ent->plt.offset = s->size;
10539 s->size += PLT_ENTRY_SIZE (htab);
10540 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10541 }
10542 else if (htab->can_convert_all_inline_plt
10543 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10544 ent->plt.offset = (bfd_vma) -1;
10545 else
10546 {
10547 s = htab->pltlocal;
10548 ent->plt.offset = s->size;
10549 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10550 if (bfd_link_pic (info))
10551 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10552 }
10553 }
10554 else
10555 ent->plt.offset = (bfd_vma) -1;
10556 }
10557 }
10558
10559 /* Allocate global sym .plt and .got entries, and space for global
10560 sym dynamic relocs. */
10561 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10562
10563 if (!htab->opd_abi && !bfd_link_pic (info))
10564 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10565
10566 first_tlsld = NULL;
10567 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10568 {
10569 struct got_entry *ent;
10570
10571 if (!is_ppc64_elf (ibfd))
10572 continue;
10573
10574 ent = ppc64_tlsld_got (ibfd);
10575 if (ent->got.refcount > 0)
10576 {
10577 if (!htab->do_multi_toc && first_tlsld != NULL)
10578 {
10579 ent->is_indirect = TRUE;
10580 ent->got.ent = first_tlsld;
10581 }
10582 else
10583 {
10584 if (first_tlsld == NULL)
10585 first_tlsld = ent;
10586 s = ppc64_elf_tdata (ibfd)->got;
10587 ent->got.offset = s->size;
10588 ent->owner = ibfd;
10589 s->size += 16;
10590 if (bfd_link_pic (info))
10591 {
10592 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10593 srel->size += sizeof (Elf64_External_Rela);
10594 }
10595 }
10596 }
10597 else
10598 ent->got.offset = (bfd_vma) -1;
10599 }
10600
10601 /* We now have determined the sizes of the various dynamic sections.
10602 Allocate memory for them. */
10603 relocs = FALSE;
10604 for (s = dynobj->sections; s != NULL; s = s->next)
10605 {
10606 if ((s->flags & SEC_LINKER_CREATED) == 0)
10607 continue;
10608
10609 if (s == htab->brlt || s == htab->relbrlt)
10610 /* These haven't been allocated yet; don't strip. */
10611 continue;
10612 else if (s == htab->elf.sgot
10613 || s == htab->elf.splt
10614 || s == htab->elf.iplt
10615 || s == htab->pltlocal
10616 || s == htab->glink
10617 || s == htab->global_entry
10618 || s == htab->elf.sdynbss
10619 || s == htab->elf.sdynrelro)
10620 {
10621 /* Strip this section if we don't need it; see the
10622 comment below. */
10623 }
10624 else if (s == htab->glink_eh_frame)
10625 {
10626 if (!bfd_is_abs_section (s->output_section))
10627 /* Not sized yet. */
10628 continue;
10629 }
10630 else if (CONST_STRNEQ (s->name, ".rela"))
10631 {
10632 if (s->size != 0)
10633 {
10634 if (s != htab->elf.srelplt)
10635 relocs = TRUE;
10636
10637 /* We use the reloc_count field as a counter if we need
10638 to copy relocs into the output file. */
10639 s->reloc_count = 0;
10640 }
10641 }
10642 else
10643 {
10644 /* It's not one of our sections, so don't allocate space. */
10645 continue;
10646 }
10647
10648 if (s->size == 0)
10649 {
10650 /* If we don't need this section, strip it from the
10651 output file. This is mostly to handle .rela.bss and
10652 .rela.plt. We must create both sections in
10653 create_dynamic_sections, because they must be created
10654 before the linker maps input sections to output
10655 sections. The linker does that before
10656 adjust_dynamic_symbol is called, and it is that
10657 function which decides whether anything needs to go
10658 into these sections. */
10659 s->flags |= SEC_EXCLUDE;
10660 continue;
10661 }
10662
10663 if (bfd_is_abs_section (s->output_section))
10664 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10665 s->name);
10666
10667 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10668 continue;
10669
10670 /* Allocate memory for the section contents. We use bfd_zalloc
10671 here in case unused entries are not reclaimed before the
10672 section's contents are written out. This should not happen,
10673 but this way if it does we get a R_PPC64_NONE reloc in .rela
10674 sections instead of garbage.
10675 We also rely on the section contents being zero when writing
10676 the GOT and .dynrelro. */
10677 s->contents = bfd_zalloc (dynobj, s->size);
10678 if (s->contents == NULL)
10679 return FALSE;
10680 }
10681
10682 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10683 {
10684 if (!is_ppc64_elf (ibfd))
10685 continue;
10686
10687 s = ppc64_elf_tdata (ibfd)->got;
10688 if (s != NULL && s != htab->elf.sgot)
10689 {
10690 if (s->size == 0)
10691 s->flags |= SEC_EXCLUDE;
10692 else
10693 {
10694 s->contents = bfd_zalloc (ibfd, s->size);
10695 if (s->contents == NULL)
10696 return FALSE;
10697 }
10698 }
10699 s = ppc64_elf_tdata (ibfd)->relgot;
10700 if (s != NULL)
10701 {
10702 if (s->size == 0)
10703 s->flags |= SEC_EXCLUDE;
10704 else
10705 {
10706 s->contents = bfd_zalloc (ibfd, s->size);
10707 if (s->contents == NULL)
10708 return FALSE;
10709 relocs = TRUE;
10710 s->reloc_count = 0;
10711 }
10712 }
10713 }
10714
10715 if (htab->elf.dynamic_sections_created)
10716 {
10717 bfd_boolean tls_opt;
10718
10719 /* Add some entries to the .dynamic section. We fill in the
10720 values later, in ppc64_elf_finish_dynamic_sections, but we
10721 must add the entries now so that we get the correct size for
10722 the .dynamic section. The DT_DEBUG entry is filled in by the
10723 dynamic linker and used by the debugger. */
10724 #define add_dynamic_entry(TAG, VAL) \
10725 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10726
10727 if (bfd_link_executable (info))
10728 {
10729 if (!add_dynamic_entry (DT_DEBUG, 0))
10730 return FALSE;
10731 }
10732
10733 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10734 {
10735 if (!add_dynamic_entry (DT_PLTGOT, 0)
10736 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10737 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10738 || !add_dynamic_entry (DT_JMPREL, 0)
10739 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10740 return FALSE;
10741 }
10742
10743 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10744 {
10745 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10746 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10747 return FALSE;
10748 }
10749
10750 tls_opt = (htab->params->tls_get_addr_opt
10751 && htab->tls_get_addr_fd != NULL
10752 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10753 if (tls_opt || !htab->opd_abi)
10754 {
10755 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10756 return FALSE;
10757 }
10758
10759 if (relocs)
10760 {
10761 if (!add_dynamic_entry (DT_RELA, 0)
10762 || !add_dynamic_entry (DT_RELASZ, 0)
10763 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10764 return FALSE;
10765
10766 /* If any dynamic relocs apply to a read-only section,
10767 then we need a DT_TEXTREL entry. */
10768 if ((info->flags & DF_TEXTREL) == 0)
10769 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10770
10771 if ((info->flags & DF_TEXTREL) != 0)
10772 {
10773 if (!add_dynamic_entry (DT_TEXTREL, 0))
10774 return FALSE;
10775 }
10776 }
10777 }
10778 #undef add_dynamic_entry
10779
10780 return TRUE;
10781 }
10782
10783 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10784
10785 static bfd_boolean
10786 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10787 {
10788 if (h->plt.plist != NULL
10789 && !h->def_regular
10790 && !h->pointer_equality_needed)
10791 return FALSE;
10792
10793 return _bfd_elf_hash_symbol (h);
10794 }
10795
10796 /* Determine the type of stub needed, if any, for a call. */
10797
10798 static inline enum ppc_stub_type
10799 ppc_type_of_stub (asection *input_sec,
10800 const Elf_Internal_Rela *rel,
10801 struct ppc_link_hash_entry **hash,
10802 struct plt_entry **plt_ent,
10803 bfd_vma destination,
10804 unsigned long local_off)
10805 {
10806 struct ppc_link_hash_entry *h = *hash;
10807 bfd_vma location;
10808 bfd_vma branch_offset;
10809 bfd_vma max_branch_offset;
10810 enum elf_ppc64_reloc_type r_type;
10811
10812 if (h != NULL)
10813 {
10814 struct plt_entry *ent;
10815 struct ppc_link_hash_entry *fdh = h;
10816 if (h->oh != NULL
10817 && h->oh->is_func_descriptor)
10818 {
10819 fdh = ppc_follow_link (h->oh);
10820 *hash = fdh;
10821 }
10822
10823 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10824 if (ent->addend == rel->r_addend
10825 && ent->plt.offset != (bfd_vma) -1)
10826 {
10827 *plt_ent = ent;
10828 return ppc_stub_plt_call;
10829 }
10830
10831 /* Here, we know we don't have a plt entry. If we don't have a
10832 either a defined function descriptor or a defined entry symbol
10833 in a regular object file, then it is pointless trying to make
10834 any other type of stub. */
10835 if (!is_static_defined (&fdh->elf)
10836 && !is_static_defined (&h->elf))
10837 return ppc_stub_none;
10838 }
10839 else if (elf_local_got_ents (input_sec->owner) != NULL)
10840 {
10841 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10842 struct plt_entry **local_plt = (struct plt_entry **)
10843 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10844 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10845
10846 if (local_plt[r_symndx] != NULL)
10847 {
10848 struct plt_entry *ent;
10849
10850 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10851 if (ent->addend == rel->r_addend
10852 && ent->plt.offset != (bfd_vma) -1)
10853 {
10854 *plt_ent = ent;
10855 return ppc_stub_plt_call;
10856 }
10857 }
10858 }
10859
10860 /* Determine where the call point is. */
10861 location = (input_sec->output_offset
10862 + input_sec->output_section->vma
10863 + rel->r_offset);
10864
10865 branch_offset = destination - location;
10866 r_type = ELF64_R_TYPE (rel->r_info);
10867
10868 /* Determine if a long branch stub is needed. */
10869 max_branch_offset = 1 << 25;
10870 if (r_type == R_PPC64_REL14
10871 || r_type == R_PPC64_REL14_BRTAKEN
10872 || r_type == R_PPC64_REL14_BRNTAKEN)
10873 max_branch_offset = 1 << 15;
10874
10875 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10876 /* We need a stub. Figure out whether a long_branch or plt_branch
10877 is needed later. */
10878 return ppc_stub_long_branch;
10879
10880 return ppc_stub_none;
10881 }
10882
10883 /* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
10884 loads r12 from r11+r12 (LOAD true).
10885 . lis %r12,xxx-1b@highest
10886 . ori %r12,xxx-1b@higher
10887 . sldi %r12,%r12,32
10888 . oris %r12,%r12,xxx-1b@hi
10889 . ori %r12,%r12,xxx-1b@l
10890 . add %r12,%r11,%r12 */
10891
10892 static bfd_byte *
10893 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10894 {
10895 if (off + 0x8000 < 0x10000)
10896 {
10897 if (load)
10898 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10899 else
10900 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10901 p += 4;
10902 }
10903 else if (off + 0x80008000ULL < 0x100000000ULL)
10904 {
10905 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10906 p += 4;
10907 if (load)
10908 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10909 else
10910 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10911 p += 4;
10912 }
10913 else
10914 {
10915 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10916 {
10917 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10918 p += 4;
10919 }
10920 else
10921 {
10922 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10923 p += 4;
10924 if (((off >> 32) & 0xffff) != 0)
10925 {
10926 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10927 p += 4;
10928 }
10929 }
10930 if (((off >> 32) & 0xffffffffULL) != 0)
10931 {
10932 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10933 p += 4;
10934 }
10935 if (PPC_HI (off) != 0)
10936 {
10937 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10938 p += 4;
10939 }
10940 if (PPC_LO (off) != 0)
10941 {
10942 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10943 p += 4;
10944 }
10945 if (load)
10946 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10947 else
10948 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10949 p += 4;
10950 }
10951 return p;
10952 }
10953
10954 static unsigned int
10955 size_offset (bfd_vma off)
10956 {
10957 unsigned int size;
10958 if (off + 0x8000 < 0x10000)
10959 size = 4;
10960 else if (off + 0x80008000ULL < 0x100000000ULL)
10961 size = 8;
10962 else
10963 {
10964 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10965 size = 4;
10966 else
10967 {
10968 size = 4;
10969 if (((off >> 32) & 0xffff) != 0)
10970 size += 4;
10971 }
10972 if (((off >> 32) & 0xffffffffULL) != 0)
10973 size += 4;
10974 if (PPC_HI (off) != 0)
10975 size += 4;
10976 if (PPC_LO (off) != 0)
10977 size += 4;
10978 size += 4;
10979 }
10980 return size;
10981 }
10982
10983 /* Emit .eh_frame opcode to advance pc by DELTA. */
10984
10985 static bfd_byte *
10986 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10987 {
10988 delta /= 4;
10989 if (delta < 64)
10990 *eh++ = DW_CFA_advance_loc + delta;
10991 else if (delta < 256)
10992 {
10993 *eh++ = DW_CFA_advance_loc1;
10994 *eh++ = delta;
10995 }
10996 else if (delta < 65536)
10997 {
10998 *eh++ = DW_CFA_advance_loc2;
10999 bfd_put_16 (abfd, delta, eh);
11000 eh += 2;
11001 }
11002 else
11003 {
11004 *eh++ = DW_CFA_advance_loc4;
11005 bfd_put_32 (abfd, delta, eh);
11006 eh += 4;
11007 }
11008 return eh;
11009 }
11010
11011 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11012
11013 static unsigned int
11014 eh_advance_size (unsigned int delta)
11015 {
11016 if (delta < 64 * 4)
11017 /* DW_CFA_advance_loc+[1..63]. */
11018 return 1;
11019 if (delta < 256 * 4)
11020 /* DW_CFA_advance_loc1, byte. */
11021 return 2;
11022 if (delta < 65536 * 4)
11023 /* DW_CFA_advance_loc2, 2 bytes. */
11024 return 3;
11025 /* DW_CFA_advance_loc4, 4 bytes. */
11026 return 5;
11027 }
11028
11029 /* With power7 weakly ordered memory model, it is possible for ld.so
11030 to update a plt entry in one thread and have another thread see a
11031 stale zero toc entry. To avoid this we need some sort of acquire
11032 barrier in the call stub. One solution is to make the load of the
11033 toc word seem to appear to depend on the load of the function entry
11034 word. Another solution is to test for r2 being zero, and branch to
11035 the appropriate glink entry if so.
11036
11037 . fake dep barrier compare
11038 . ld 12,xxx(2) ld 12,xxx(2)
11039 . mtctr 12 mtctr 12
11040 . xor 11,12,12 ld 2,xxx+8(2)
11041 . add 2,2,11 cmpldi 2,0
11042 . ld 2,xxx+8(2) bnectr+
11043 . bctr b <glink_entry>
11044
11045 The solution involving the compare turns out to be faster, so
11046 that's what we use unless the branch won't reach. */
11047
11048 #define ALWAYS_USE_FAKE_DEP 0
11049 #define ALWAYS_EMIT_R2SAVE 0
11050
11051 static inline unsigned int
11052 plt_stub_size (struct ppc_link_hash_table *htab,
11053 struct ppc_stub_hash_entry *stub_entry,
11054 bfd_vma off)
11055 {
11056 unsigned size;
11057
11058 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11059 {
11060 size = 24 + size_offset (off);
11061 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11062 size += 4;
11063 return size;
11064 }
11065
11066 size = 12;
11067 if (ALWAYS_EMIT_R2SAVE
11068 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11069 size += 4;
11070 if (PPC_HA (off) != 0)
11071 size += 4;
11072 if (htab->opd_abi)
11073 {
11074 size += 4;
11075 if (htab->params->plt_static_chain)
11076 size += 4;
11077 if (htab->params->plt_thread_safe
11078 && htab->elf.dynamic_sections_created
11079 && stub_entry->h != NULL
11080 && stub_entry->h->elf.dynindx != -1)
11081 size += 8;
11082 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
11083 size += 4;
11084 }
11085 if (stub_entry->h != NULL
11086 && (stub_entry->h == htab->tls_get_addr_fd
11087 || stub_entry->h == htab->tls_get_addr)
11088 && htab->params->tls_get_addr_opt)
11089 {
11090 size += 7 * 4;
11091 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
11092 size += 6 * 4;
11093 }
11094 return size;
11095 }
11096
11097 /* Depending on the sign of plt_stub_align:
11098 If positive, return the padding to align to a 2**plt_stub_align
11099 boundary.
11100 If negative, if this stub would cross fewer 2**plt_stub_align
11101 boundaries if we align, then return the padding needed to do so. */
11102
11103 static inline unsigned int
11104 plt_stub_pad (struct ppc_link_hash_table *htab,
11105 struct ppc_stub_hash_entry *stub_entry,
11106 bfd_vma plt_off)
11107 {
11108 int stub_align;
11109 unsigned stub_size;
11110 bfd_vma stub_off = stub_entry->group->stub_sec->size;
11111
11112 if (htab->params->plt_stub_align >= 0)
11113 {
11114 stub_align = 1 << htab->params->plt_stub_align;
11115 if ((stub_off & (stub_align - 1)) != 0)
11116 return stub_align - (stub_off & (stub_align - 1));
11117 return 0;
11118 }
11119
11120 stub_align = 1 << -htab->params->plt_stub_align;
11121 stub_size = plt_stub_size (htab, stub_entry, plt_off);
11122 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11123 > ((stub_size - 1) & -stub_align))
11124 return stub_align - (stub_off & (stub_align - 1));
11125 return 0;
11126 }
11127
11128 /* Build a .plt call stub. */
11129
11130 static inline bfd_byte *
11131 build_plt_stub (struct ppc_link_hash_table *htab,
11132 struct ppc_stub_hash_entry *stub_entry,
11133 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11134 {
11135 bfd *obfd = htab->params->stub_bfd;
11136 bfd_boolean plt_load_toc = htab->opd_abi;
11137 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
11138 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
11139 && htab->elf.dynamic_sections_created
11140 && stub_entry->h != NULL
11141 && stub_entry->h->elf.dynindx != -1);
11142 bfd_boolean use_fake_dep = plt_thread_safe;
11143 bfd_vma cmp_branch_off = 0;
11144
11145 if (!ALWAYS_USE_FAKE_DEP
11146 && plt_load_toc
11147 && plt_thread_safe
11148 && !((stub_entry->h == htab->tls_get_addr_fd
11149 || stub_entry->h == htab->tls_get_addr)
11150 && htab->params->tls_get_addr_opt))
11151 {
11152 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11153 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11154 / PLT_ENTRY_SIZE (htab));
11155 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11156 bfd_vma to, from;
11157
11158 if (pltindex > 32768)
11159 glinkoff += (pltindex - 32768) * 4;
11160 to = (glinkoff
11161 + htab->glink->output_offset
11162 + htab->glink->output_section->vma);
11163 from = (p - stub_entry->group->stub_sec->contents
11164 + 4 * (ALWAYS_EMIT_R2SAVE
11165 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11166 + 4 * (PPC_HA (offset) != 0)
11167 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11168 != PPC_HA (offset))
11169 + 4 * (plt_static_chain != 0)
11170 + 20
11171 + stub_entry->group->stub_sec->output_offset
11172 + stub_entry->group->stub_sec->output_section->vma);
11173 cmp_branch_off = to - from;
11174 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11175 }
11176
11177 if (PPC_HA (offset) != 0)
11178 {
11179 if (r != NULL)
11180 {
11181 if (ALWAYS_EMIT_R2SAVE
11182 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11183 r[0].r_offset += 4;
11184 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11185 r[1].r_offset = r[0].r_offset + 4;
11186 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11187 r[1].r_addend = r[0].r_addend;
11188 if (plt_load_toc)
11189 {
11190 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11191 {
11192 r[2].r_offset = r[1].r_offset + 4;
11193 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11194 r[2].r_addend = r[0].r_addend;
11195 }
11196 else
11197 {
11198 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11199 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11200 r[2].r_addend = r[0].r_addend + 8;
11201 if (plt_static_chain)
11202 {
11203 r[3].r_offset = r[2].r_offset + 4;
11204 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11205 r[3].r_addend = r[0].r_addend + 16;
11206 }
11207 }
11208 }
11209 }
11210 if (ALWAYS_EMIT_R2SAVE
11211 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11212 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11213 if (plt_load_toc)
11214 {
11215 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11216 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11217 }
11218 else
11219 {
11220 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11221 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11222 }
11223 if (plt_load_toc
11224 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11225 {
11226 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11227 offset = 0;
11228 }
11229 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11230 if (plt_load_toc)
11231 {
11232 if (use_fake_dep)
11233 {
11234 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11235 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11236 }
11237 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11238 if (plt_static_chain)
11239 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11240 }
11241 }
11242 else
11243 {
11244 if (r != NULL)
11245 {
11246 if (ALWAYS_EMIT_R2SAVE
11247 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11248 r[0].r_offset += 4;
11249 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11250 if (plt_load_toc)
11251 {
11252 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11253 {
11254 r[1].r_offset = r[0].r_offset + 4;
11255 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11256 r[1].r_addend = r[0].r_addend;
11257 }
11258 else
11259 {
11260 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11261 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11262 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11263 if (plt_static_chain)
11264 {
11265 r[2].r_offset = r[1].r_offset + 4;
11266 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11267 r[2].r_addend = r[0].r_addend + 8;
11268 }
11269 }
11270 }
11271 }
11272 if (ALWAYS_EMIT_R2SAVE
11273 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11274 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11275 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11276 if (plt_load_toc
11277 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11278 {
11279 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11280 offset = 0;
11281 }
11282 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11283 if (plt_load_toc)
11284 {
11285 if (use_fake_dep)
11286 {
11287 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11288 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11289 }
11290 if (plt_static_chain)
11291 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11292 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11293 }
11294 }
11295 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11296 {
11297 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11298 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11299 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11300 }
11301 else
11302 bfd_put_32 (obfd, BCTR, p), p += 4;
11303 return p;
11304 }
11305
11306 /* Build a special .plt call stub for __tls_get_addr. */
11307
11308 #define LD_R11_0R3 0xe9630000
11309 #define LD_R12_0R3 0xe9830000
11310 #define MR_R0_R3 0x7c601b78
11311 #define CMPDI_R11_0 0x2c2b0000
11312 #define ADD_R3_R12_R13 0x7c6c6a14
11313 #define BEQLR 0x4d820020
11314 #define MR_R3_R0 0x7c030378
11315 #define STD_R11_0R1 0xf9610000
11316 #define BCTRL 0x4e800421
11317 #define LD_R11_0R1 0xe9610000
11318 #define MTLR_R11 0x7d6803a6
11319
11320 static inline bfd_byte *
11321 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11322 struct ppc_stub_hash_entry *stub_entry,
11323 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11324 {
11325 bfd *obfd = htab->params->stub_bfd;
11326 bfd_byte *loc = p;
11327
11328 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11329 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11330 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11331 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11332 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11333 bfd_put_32 (obfd, BEQLR, p), p += 4;
11334 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11335 if (r != NULL)
11336 r[0].r_offset += 7 * 4;
11337 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
11338 return build_plt_stub (htab, stub_entry, p, offset, r);
11339
11340 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
11341 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11342
11343 if (r != NULL)
11344 r[0].r_offset += 2 * 4;
11345 p = build_plt_stub (htab, stub_entry, p, offset, r);
11346 bfd_put_32 (obfd, BCTRL, p - 4);
11347
11348 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
11349 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11350 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11351 bfd_put_32 (obfd, BLR, p), p += 4;
11352
11353 if (htab->glink_eh_frame != NULL
11354 && htab->glink_eh_frame->size != 0)
11355 {
11356 bfd_byte *base, *eh;
11357 unsigned int lr_used, delta;
11358
11359 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11360 eh = base + stub_entry->group->eh_size;
11361 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11362 delta = lr_used - stub_entry->group->lr_restore;
11363 stub_entry->group->lr_restore = lr_used + 16;
11364 eh = eh_advance (htab->elf.dynobj, eh, delta);
11365 *eh++ = DW_CFA_offset_extended_sf;
11366 *eh++ = 65;
11367 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11368 *eh++ = DW_CFA_advance_loc + 4;
11369 *eh++ = DW_CFA_restore_extended;
11370 *eh++ = 65;
11371 stub_entry->group->eh_size = eh - base;
11372 }
11373 return p;
11374 }
11375
11376 static Elf_Internal_Rela *
11377 get_relocs (asection *sec, int count)
11378 {
11379 Elf_Internal_Rela *relocs;
11380 struct bfd_elf_section_data *elfsec_data;
11381
11382 elfsec_data = elf_section_data (sec);
11383 relocs = elfsec_data->relocs;
11384 if (relocs == NULL)
11385 {
11386 bfd_size_type relsize;
11387 relsize = sec->reloc_count * sizeof (*relocs);
11388 relocs = bfd_alloc (sec->owner, relsize);
11389 if (relocs == NULL)
11390 return NULL;
11391 elfsec_data->relocs = relocs;
11392 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11393 sizeof (Elf_Internal_Shdr));
11394 if (elfsec_data->rela.hdr == NULL)
11395 return NULL;
11396 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11397 * sizeof (Elf64_External_Rela));
11398 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11399 sec->reloc_count = 0;
11400 }
11401 relocs += sec->reloc_count;
11402 sec->reloc_count += count;
11403 return relocs;
11404 }
11405
11406 static bfd_vma
11407 get_r2off (struct bfd_link_info *info,
11408 struct ppc_stub_hash_entry *stub_entry)
11409 {
11410 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11411 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11412
11413 if (r2off == 0)
11414 {
11415 /* Support linking -R objects. Get the toc pointer from the
11416 opd entry. */
11417 char buf[8];
11418 if (!htab->opd_abi)
11419 return r2off;
11420 asection *opd = stub_entry->h->elf.root.u.def.section;
11421 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11422
11423 if (strcmp (opd->name, ".opd") != 0
11424 || opd->reloc_count != 0)
11425 {
11426 info->callbacks->einfo
11427 (_("%P: cannot find opd entry toc for `%pT'\n"),
11428 stub_entry->h->elf.root.root.string);
11429 bfd_set_error (bfd_error_bad_value);
11430 return (bfd_vma) -1;
11431 }
11432 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11433 return (bfd_vma) -1;
11434 r2off = bfd_get_64 (opd->owner, buf);
11435 r2off -= elf_gp (info->output_bfd);
11436 }
11437 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11438 return r2off;
11439 }
11440
11441 static bfd_boolean
11442 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11443 {
11444 struct ppc_stub_hash_entry *stub_entry;
11445 struct ppc_branch_hash_entry *br_entry;
11446 struct bfd_link_info *info;
11447 struct ppc_link_hash_table *htab;
11448 bfd_byte *loc;
11449 bfd_byte *p;
11450 bfd_vma targ, off;
11451 Elf_Internal_Rela *r;
11452 asection *plt;
11453
11454 /* Massage our args to the form they really have. */
11455 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11456 info = in_arg;
11457
11458 htab = ppc_hash_table (info);
11459 if (htab == NULL)
11460 return FALSE;
11461
11462 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11463 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11464
11465 htab->stub_count[stub_entry->stub_type - 1] += 1;
11466 switch (stub_entry->stub_type)
11467 {
11468 case ppc_stub_long_branch:
11469 case ppc_stub_long_branch_r2off:
11470 /* Branches are relative. This is where we are going to. */
11471 targ = (stub_entry->target_value
11472 + stub_entry->target_section->output_offset
11473 + stub_entry->target_section->output_section->vma);
11474 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11475
11476 /* And this is where we are coming from. */
11477 off = (stub_entry->stub_offset
11478 + stub_entry->group->stub_sec->output_offset
11479 + stub_entry->group->stub_sec->output_section->vma);
11480 off = targ - off;
11481
11482 p = loc;
11483 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11484 {
11485 bfd_vma r2off = get_r2off (info, stub_entry);
11486
11487 if (r2off == (bfd_vma) -1)
11488 {
11489 htab->stub_error = TRUE;
11490 return FALSE;
11491 }
11492 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11493 p += 4;
11494 if (PPC_HA (r2off) != 0)
11495 {
11496 bfd_put_32 (htab->params->stub_bfd,
11497 ADDIS_R2_R2 | PPC_HA (r2off), p);
11498 p += 4;
11499 }
11500 if (PPC_LO (r2off) != 0)
11501 {
11502 bfd_put_32 (htab->params->stub_bfd,
11503 ADDI_R2_R2 | PPC_LO (r2off), p);
11504 p += 4;
11505 }
11506 off -= p - loc;
11507 }
11508 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11509 p += 4;
11510
11511 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11512 {
11513 _bfd_error_handler
11514 (_("long branch stub `%s' offset overflow"),
11515 stub_entry->root.string);
11516 htab->stub_error = TRUE;
11517 return FALSE;
11518 }
11519
11520 if (info->emitrelocations)
11521 {
11522 r = get_relocs (stub_entry->group->stub_sec, 1);
11523 if (r == NULL)
11524 return FALSE;
11525 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11526 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11527 r->r_addend = targ;
11528 if (stub_entry->h != NULL)
11529 {
11530 struct elf_link_hash_entry **hashes;
11531 unsigned long symndx;
11532 struct ppc_link_hash_entry *h;
11533
11534 hashes = elf_sym_hashes (htab->params->stub_bfd);
11535 if (hashes == NULL)
11536 {
11537 bfd_size_type hsize;
11538
11539 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11540 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11541 if (hashes == NULL)
11542 return FALSE;
11543 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11544 htab->stub_globals = 1;
11545 }
11546 symndx = htab->stub_globals++;
11547 h = stub_entry->h;
11548 hashes[symndx] = &h->elf;
11549 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11550 if (h->oh != NULL && h->oh->is_func)
11551 h = ppc_follow_link (h->oh);
11552 if (h->elf.root.u.def.section != stub_entry->target_section)
11553 /* H is an opd symbol. The addend must be zero. */
11554 r->r_addend = 0;
11555 else
11556 {
11557 off = (h->elf.root.u.def.value
11558 + h->elf.root.u.def.section->output_offset
11559 + h->elf.root.u.def.section->output_section->vma);
11560 r->r_addend -= off;
11561 }
11562 }
11563 }
11564 break;
11565
11566 case ppc_stub_plt_branch:
11567 case ppc_stub_plt_branch_r2off:
11568 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11569 stub_entry->root.string + 9,
11570 FALSE, FALSE);
11571 if (br_entry == NULL)
11572 {
11573 _bfd_error_handler (_("can't find branch stub `%s'"),
11574 stub_entry->root.string);
11575 htab->stub_error = TRUE;
11576 return FALSE;
11577 }
11578
11579 targ = (stub_entry->target_value
11580 + stub_entry->target_section->output_offset
11581 + stub_entry->target_section->output_section->vma);
11582 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11583 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11584
11585 bfd_put_64 (htab->brlt->owner, targ,
11586 htab->brlt->contents + br_entry->offset);
11587
11588 if (br_entry->iter == htab->stub_iteration)
11589 {
11590 br_entry->iter = 0;
11591
11592 if (htab->relbrlt != NULL)
11593 {
11594 /* Create a reloc for the branch lookup table entry. */
11595 Elf_Internal_Rela rela;
11596 bfd_byte *rl;
11597
11598 rela.r_offset = (br_entry->offset
11599 + htab->brlt->output_offset
11600 + htab->brlt->output_section->vma);
11601 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11602 rela.r_addend = targ;
11603
11604 rl = htab->relbrlt->contents;
11605 rl += (htab->relbrlt->reloc_count++
11606 * sizeof (Elf64_External_Rela));
11607 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11608 }
11609 else if (info->emitrelocations)
11610 {
11611 r = get_relocs (htab->brlt, 1);
11612 if (r == NULL)
11613 return FALSE;
11614 /* brlt, being SEC_LINKER_CREATED does not go through the
11615 normal reloc processing. Symbols and offsets are not
11616 translated from input file to output file form, so
11617 set up the offset per the output file. */
11618 r->r_offset = (br_entry->offset
11619 + htab->brlt->output_offset
11620 + htab->brlt->output_section->vma);
11621 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11622 r->r_addend = targ;
11623 }
11624 }
11625
11626 targ = (br_entry->offset
11627 + htab->brlt->output_offset
11628 + htab->brlt->output_section->vma);
11629
11630 off = (elf_gp (info->output_bfd)
11631 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11632 off = targ - off;
11633
11634 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11635 {
11636 info->callbacks->einfo
11637 (_("%P: linkage table error against `%pT'\n"),
11638 stub_entry->root.string);
11639 bfd_set_error (bfd_error_bad_value);
11640 htab->stub_error = TRUE;
11641 return FALSE;
11642 }
11643
11644 if (info->emitrelocations)
11645 {
11646 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11647 if (r == NULL)
11648 return FALSE;
11649 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11650 if (bfd_big_endian (info->output_bfd))
11651 r[0].r_offset += 2;
11652 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11653 r[0].r_offset += 4;
11654 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11655 r[0].r_addend = targ;
11656 if (PPC_HA (off) != 0)
11657 {
11658 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11659 r[1].r_offset = r[0].r_offset + 4;
11660 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11661 r[1].r_addend = r[0].r_addend;
11662 }
11663 }
11664
11665 p = loc;
11666 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11667 {
11668 if (PPC_HA (off) != 0)
11669 {
11670 bfd_put_32 (htab->params->stub_bfd,
11671 ADDIS_R12_R2 | PPC_HA (off), p);
11672 p += 4;
11673 bfd_put_32 (htab->params->stub_bfd,
11674 LD_R12_0R12 | PPC_LO (off), p);
11675 }
11676 else
11677 bfd_put_32 (htab->params->stub_bfd,
11678 LD_R12_0R2 | PPC_LO (off), p);
11679 }
11680 else
11681 {
11682 bfd_vma r2off = get_r2off (info, stub_entry);
11683
11684 if (r2off == (bfd_vma) -1)
11685 {
11686 htab->stub_error = TRUE;
11687 return FALSE;
11688 }
11689
11690 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11691 p += 4;
11692 if (PPC_HA (off) != 0)
11693 {
11694 bfd_put_32 (htab->params->stub_bfd,
11695 ADDIS_R12_R2 | PPC_HA (off), p);
11696 p += 4;
11697 bfd_put_32 (htab->params->stub_bfd,
11698 LD_R12_0R12 | PPC_LO (off), p);
11699 }
11700 else
11701 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11702
11703 if (PPC_HA (r2off) != 0)
11704 {
11705 p += 4;
11706 bfd_put_32 (htab->params->stub_bfd,
11707 ADDIS_R2_R2 | PPC_HA (r2off), p);
11708 }
11709 if (PPC_LO (r2off) != 0)
11710 {
11711 p += 4;
11712 bfd_put_32 (htab->params->stub_bfd,
11713 ADDI_R2_R2 | PPC_LO (r2off), p);
11714 }
11715 }
11716 p += 4;
11717 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11718 p += 4;
11719 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11720 p += 4;
11721 break;
11722
11723 case ppc_stub_long_branch_notoc:
11724 case ppc_stub_long_branch_both:
11725 case ppc_stub_plt_branch_notoc:
11726 case ppc_stub_plt_branch_both:
11727 case ppc_stub_plt_call_notoc:
11728 case ppc_stub_plt_call_both:
11729 p = loc;
11730 off = (8 + stub_entry->stub_offset
11731 + stub_entry->group->stub_sec->output_offset
11732 + stub_entry->group->stub_sec->output_section->vma);
11733 if (stub_entry->stub_type == ppc_stub_long_branch_both
11734 || stub_entry->stub_type == ppc_stub_plt_branch_both
11735 || stub_entry->stub_type == ppc_stub_plt_call_both)
11736 {
11737 off += 4;
11738 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11739 p += 4;
11740 }
11741 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11742 {
11743 targ = stub_entry->plt_ent->plt.offset & ~1;
11744 if (targ >= (bfd_vma) -2)
11745 abort ();
11746
11747 plt = htab->elf.splt;
11748 if (!htab->elf.dynamic_sections_created
11749 || stub_entry->h == NULL
11750 || stub_entry->h->elf.dynindx == -1)
11751 {
11752 if (stub_entry->symtype == STT_GNU_IFUNC)
11753 plt = htab->elf.iplt;
11754 else
11755 plt = htab->pltlocal;
11756 }
11757 targ += plt->output_offset + plt->output_section->vma;
11758 }
11759 else
11760 targ = (stub_entry->target_value
11761 + stub_entry->target_section->output_offset
11762 + stub_entry->target_section->output_section->vma);
11763 off = targ - off;
11764 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
11765 p += 4;
11766 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
11767 p += 4;
11768 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
11769 p += 4;
11770 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
11771 p += 4;
11772 p = build_offset (htab->params->stub_bfd, p, off,
11773 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11774 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
11775 {
11776 off += 8;
11777 bfd_put_32 (htab->params->stub_bfd,
11778 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11779 }
11780 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
11781 {
11782 off += 12;
11783 bfd_put_32 (htab->params->stub_bfd,
11784 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11785 }
11786 else
11787 {
11788 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11789 p += 4;
11790 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11791 }
11792 p += 4;
11793
11794 if (htab->glink_eh_frame != NULL
11795 && htab->glink_eh_frame->size != 0)
11796 {
11797 bfd_byte *base, *eh;
11798 unsigned int lr_used, delta;
11799
11800 base = (htab->glink_eh_frame->contents
11801 + stub_entry->group->eh_base + 17);
11802 eh = base + stub_entry->group->eh_size;
11803 lr_used = stub_entry->stub_offset + 8;
11804 if (stub_entry->stub_type == ppc_stub_long_branch_both
11805 || stub_entry->stub_type == ppc_stub_plt_branch_both
11806 || stub_entry->stub_type == ppc_stub_plt_call_both)
11807 lr_used += 4;
11808 delta = lr_used - stub_entry->group->lr_restore;
11809 stub_entry->group->lr_restore = lr_used + 8;
11810 eh = eh_advance (htab->elf.dynobj, eh, delta);
11811 *eh++ = DW_CFA_register;
11812 *eh++ = 65;
11813 *eh++ = 12;
11814 *eh++ = DW_CFA_advance_loc + 2;
11815 *eh++ = DW_CFA_restore_extended;
11816 *eh++ = 65;
11817 stub_entry->group->eh_size = eh - base;
11818 }
11819 break;
11820
11821 case ppc_stub_plt_call:
11822 case ppc_stub_plt_call_r2save:
11823 if (stub_entry->h != NULL
11824 && stub_entry->h->is_func_descriptor
11825 && stub_entry->h->oh != NULL)
11826 {
11827 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11828
11829 /* If the old-ABI "dot-symbol" is undefined make it weak so
11830 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11831 if (fh->elf.root.type == bfd_link_hash_undefined
11832 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11833 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11834 fh->elf.root.type = bfd_link_hash_undefweak;
11835 }
11836
11837 /* Now build the stub. */
11838 targ = stub_entry->plt_ent->plt.offset & ~1;
11839 if (targ >= (bfd_vma) -2)
11840 abort ();
11841
11842 plt = htab->elf.splt;
11843 if (!htab->elf.dynamic_sections_created
11844 || stub_entry->h == NULL
11845 || stub_entry->h->elf.dynindx == -1)
11846 {
11847 if (stub_entry->symtype == STT_GNU_IFUNC)
11848 plt = htab->elf.iplt;
11849 else
11850 plt = htab->pltlocal;
11851 }
11852 targ += plt->output_offset + plt->output_section->vma;
11853
11854 off = (elf_gp (info->output_bfd)
11855 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11856 off = targ - off;
11857
11858 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11859 {
11860 info->callbacks->einfo
11861 /* xgettext:c-format */
11862 (_("%P: linkage table error against `%pT'\n"),
11863 stub_entry->h != NULL
11864 ? stub_entry->h->elf.root.root.string
11865 : "<local sym>");
11866 bfd_set_error (bfd_error_bad_value);
11867 htab->stub_error = TRUE;
11868 return FALSE;
11869 }
11870
11871 r = NULL;
11872 if (info->emitrelocations)
11873 {
11874 r = get_relocs (stub_entry->group->stub_sec,
11875 ((PPC_HA (off) != 0)
11876 + (htab->opd_abi
11877 ? 2 + (htab->params->plt_static_chain
11878 && PPC_HA (off + 16) == PPC_HA (off))
11879 : 1)));
11880 if (r == NULL)
11881 return FALSE;
11882 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11883 if (bfd_big_endian (info->output_bfd))
11884 r[0].r_offset += 2;
11885 r[0].r_addend = targ;
11886 }
11887 if (stub_entry->h != NULL
11888 && (stub_entry->h == htab->tls_get_addr_fd
11889 || stub_entry->h == htab->tls_get_addr)
11890 && htab->params->tls_get_addr_opt)
11891 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11892 else
11893 p = build_plt_stub (htab, stub_entry, loc, off, r);
11894 break;
11895
11896 case ppc_stub_save_res:
11897 return TRUE;
11898
11899 default:
11900 BFD_FAIL ();
11901 return FALSE;
11902 }
11903
11904 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11905
11906 if (htab->params->emit_stub_syms)
11907 {
11908 struct elf_link_hash_entry *h;
11909 size_t len1, len2;
11910 char *name;
11911 const char *const stub_str[] = { "long_branch",
11912 "long_branch",
11913 "long_branch",
11914 "long_branch",
11915 "plt_branch",
11916 "plt_branch",
11917 "plt_branch",
11918 "plt_branch",
11919 "plt_call",
11920 "plt_call",
11921 "plt_call",
11922 "plt_call" };
11923
11924 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11925 len2 = strlen (stub_entry->root.string);
11926 name = bfd_malloc (len1 + len2 + 2);
11927 if (name == NULL)
11928 return FALSE;
11929 memcpy (name, stub_entry->root.string, 9);
11930 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11931 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11932 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11933 if (h == NULL)
11934 return FALSE;
11935 if (h->root.type == bfd_link_hash_new)
11936 {
11937 h->root.type = bfd_link_hash_defined;
11938 h->root.u.def.section = stub_entry->group->stub_sec;
11939 h->root.u.def.value = stub_entry->stub_offset;
11940 h->ref_regular = 1;
11941 h->def_regular = 1;
11942 h->ref_regular_nonweak = 1;
11943 h->forced_local = 1;
11944 h->non_elf = 0;
11945 h->root.linker_def = 1;
11946 }
11947 }
11948
11949 return TRUE;
11950 }
11951
11952 /* As above, but don't actually build the stub. Just bump offset so
11953 we know stub section sizes, and select plt_branch stubs where
11954 long_branch stubs won't do. */
11955
11956 static bfd_boolean
11957 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11958 {
11959 struct ppc_stub_hash_entry *stub_entry;
11960 struct bfd_link_info *info;
11961 struct ppc_link_hash_table *htab;
11962 bfd_vma targ, off;
11963 int size;
11964
11965 /* Massage our args to the form they really have. */
11966 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11967 info = in_arg;
11968
11969 htab = ppc_hash_table (info);
11970 if (htab == NULL)
11971 return FALSE;
11972
11973 /* Make a note of the offset within the stubs for this entry. */
11974 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11975
11976 if (stub_entry->h != NULL
11977 && stub_entry->h->save_res
11978 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11979 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11980 {
11981 /* Don't make stubs to out-of-line register save/restore
11982 functions. Instead, emit copies of the functions. */
11983 stub_entry->group->needs_save_res = 1;
11984 stub_entry->stub_type = ppc_stub_save_res;
11985 return TRUE;
11986 }
11987
11988 if (stub_entry->stub_type >= ppc_stub_plt_call
11989 && stub_entry->stub_type <= ppc_stub_plt_call_both)
11990 {
11991 asection *plt;
11992 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11993 if (targ >= (bfd_vma) -2)
11994 abort ();
11995 plt = htab->elf.splt;
11996 if (!htab->elf.dynamic_sections_created
11997 || stub_entry->h == NULL
11998 || stub_entry->h->elf.dynindx == -1)
11999 {
12000 if (stub_entry->symtype == STT_GNU_IFUNC)
12001 plt = htab->elf.iplt;
12002 else
12003 plt = htab->pltlocal;
12004 }
12005 targ += plt->output_offset + plt->output_section->vma;
12006
12007 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12008 {
12009 off = (8 + stub_entry->stub_offset
12010 + stub_entry->group->stub_sec->output_offset
12011 + stub_entry->group->stub_sec->output_section->vma);
12012 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
12013 off += 4;
12014 }
12015 else
12016 off = (elf_gp (info->output_bfd)
12017 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12018
12019 if (htab->params->plt_stub_align != 0)
12020 {
12021 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
12022
12023 stub_entry->group->stub_sec->size += pad;
12024 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12025 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12026 off += pad;
12027 }
12028
12029 off = targ - off;
12030 size = plt_stub_size (htab, stub_entry, off);
12031
12032 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
12033 {
12034 /* After the bcl, lr has been modified so we need to emit
12035 .eh_frame info saying the return address is in r12. */
12036 unsigned int lr_used = stub_entry->stub_offset + 8;
12037 unsigned int delta;
12038 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
12039 lr_used += 4;
12040 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12041 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12042 DW_CFA_restore_extended 65. */
12043 delta = lr_used - stub_entry->group->lr_restore;
12044 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12045 stub_entry->group->lr_restore = lr_used + 8;
12046 }
12047 else
12048 {
12049 if (stub_entry->h != NULL
12050 && (stub_entry->h == htab->tls_get_addr_fd
12051 || stub_entry->h == htab->tls_get_addr)
12052 && htab->params->tls_get_addr_opt
12053 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12054 {
12055 /* After the bctrl, lr has been modified so we need to
12056 emit .eh_frame info saying the return address is
12057 on the stack. In fact we put the EH info specifying
12058 that the return address is on the stack *at* the
12059 call rather than after it, because the EH info for a
12060 call needs to be specified by that point.
12061 See libgcc/unwind-dw2.c execute_cfa_program. */
12062 unsigned int lr_used = stub_entry->stub_offset + size - 20;
12063 unsigned int delta;
12064 /* The eh_frame info will consist of a DW_CFA_advance_loc
12065 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12066 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12067 delta = lr_used - stub_entry->group->lr_restore;
12068 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12069 stub_entry->group->lr_restore = size - 4;
12070 }
12071
12072 if (info->emitrelocations)
12073 {
12074 stub_entry->group->stub_sec->reloc_count
12075 += ((PPC_HA (off) != 0)
12076 + (htab->opd_abi
12077 ? 2 + (htab->params->plt_static_chain
12078 && PPC_HA (off + 16) == PPC_HA (off))
12079 : 1));
12080 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12081 }
12082 }
12083 }
12084 else
12085 {
12086 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
12087 variants. */
12088 bfd_vma r2off = 0;
12089 bfd_vma local_off = 0;
12090
12091 targ = (stub_entry->target_value
12092 + stub_entry->target_section->output_offset
12093 + stub_entry->target_section->output_section->vma);
12094 off = (stub_entry->stub_offset
12095 + stub_entry->group->stub_sec->output_offset
12096 + stub_entry->group->stub_sec->output_section->vma);
12097
12098 /* Reset the stub type from the plt variant in case we now
12099 can reach with a shorter stub. */
12100 if (stub_entry->stub_type >= ppc_stub_plt_branch)
12101 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12102
12103 size = 4;
12104 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12105 {
12106 r2off = get_r2off (info, stub_entry);
12107 if (r2off == (bfd_vma) -1)
12108 {
12109 htab->stub_error = TRUE;
12110 return FALSE;
12111 }
12112 size = 8;
12113 if (PPC_HA (r2off) != 0)
12114 size += 4;
12115 if (PPC_LO (r2off) != 0)
12116 size += 4;
12117 off += size - 4;
12118 }
12119 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
12120 {
12121 size = 20 + size_offset (targ - (off + 8));
12122 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
12123 size += 4;
12124 off += size - 4;
12125 }
12126 off = targ - off;
12127
12128 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
12129 {
12130 /* After the bcl, lr has been modified so we need to emit
12131 .eh_frame info saying the return address is in r12. */
12132 unsigned int lr_used = stub_entry->stub_offset + 8;
12133 unsigned int delta;
12134 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
12135 lr_used += 4;
12136 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12137 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12138 DW_CFA_restore_extended 65. */
12139 delta = lr_used - stub_entry->group->lr_restore;
12140 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12141 stub_entry->group->lr_restore = lr_used + 8;
12142
12143 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12144 {
12145 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12146 - ppc_stub_long_branch_notoc);
12147 size += 4;
12148 }
12149 }
12150 else
12151 {
12152 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12153
12154 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12155 Do the same for -R objects without function descriptors. */
12156 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12157 && r2off == 0
12158 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12159 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
12160 {
12161 struct ppc_branch_hash_entry *br_entry;
12162
12163 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12164 stub_entry->root.string + 9,
12165 TRUE, FALSE);
12166 if (br_entry == NULL)
12167 {
12168 _bfd_error_handler (_("can't build branch stub `%s'"),
12169 stub_entry->root.string);
12170 htab->stub_error = TRUE;
12171 return FALSE;
12172 }
12173
12174 if (br_entry->iter != htab->stub_iteration)
12175 {
12176 br_entry->iter = htab->stub_iteration;
12177 br_entry->offset = htab->brlt->size;
12178 htab->brlt->size += 8;
12179
12180 if (htab->relbrlt != NULL)
12181 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12182 else if (info->emitrelocations)
12183 {
12184 htab->brlt->reloc_count += 1;
12185 htab->brlt->flags |= SEC_RELOC;
12186 }
12187 }
12188
12189 targ = (br_entry->offset
12190 + htab->brlt->output_offset
12191 + htab->brlt->output_section->vma);
12192 off = (elf_gp (info->output_bfd)
12193 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12194 off = targ - off;
12195
12196 if (info->emitrelocations)
12197 {
12198 stub_entry->group->stub_sec->reloc_count
12199 += 1 + (PPC_HA (off) != 0);
12200 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12201 }
12202
12203 stub_entry->stub_type
12204 += ppc_stub_plt_branch - ppc_stub_long_branch;
12205 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12206 {
12207 size = 12;
12208 if (PPC_HA (off) != 0)
12209 size = 16;
12210 }
12211 else
12212 {
12213 size = 16;
12214 if (PPC_HA (off) != 0)
12215 size += 4;
12216
12217 if (PPC_HA (r2off) != 0)
12218 size += 4;
12219 if (PPC_LO (r2off) != 0)
12220 size += 4;
12221 }
12222 }
12223 else if (info->emitrelocations)
12224 {
12225 stub_entry->group->stub_sec->reloc_count += 1;
12226 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12227 }
12228 }
12229 }
12230
12231 stub_entry->group->stub_sec->size += size;
12232 return TRUE;
12233 }
12234
12235 /* Set up various things so that we can make a list of input sections
12236 for each output section included in the link. Returns -1 on error,
12237 0 when no stubs will be needed, and 1 on success. */
12238
12239 int
12240 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12241 {
12242 unsigned int id;
12243 bfd_size_type amt;
12244 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12245
12246 if (htab == NULL)
12247 return -1;
12248
12249 htab->sec_info_arr_size = _bfd_section_id;
12250 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12251 htab->sec_info = bfd_zmalloc (amt);
12252 if (htab->sec_info == NULL)
12253 return -1;
12254
12255 /* Set toc_off for com, und, abs and ind sections. */
12256 for (id = 0; id < 3; id++)
12257 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12258
12259 return 1;
12260 }
12261
12262 /* Set up for first pass at multitoc partitioning. */
12263
12264 void
12265 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12266 {
12267 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12268
12269 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12270 htab->toc_bfd = NULL;
12271 htab->toc_first_sec = NULL;
12272 }
12273
12274 /* The linker repeatedly calls this function for each TOC input section
12275 and linker generated GOT section. Group input bfds such that the toc
12276 within a group is less than 64k in size. */
12277
12278 bfd_boolean
12279 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12280 {
12281 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12282 bfd_vma addr, off, limit;
12283
12284 if (htab == NULL)
12285 return FALSE;
12286
12287 if (!htab->second_toc_pass)
12288 {
12289 /* Keep track of the first .toc or .got section for this input bfd. */
12290 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12291
12292 if (new_bfd)
12293 {
12294 htab->toc_bfd = isec->owner;
12295 htab->toc_first_sec = isec;
12296 }
12297
12298 addr = isec->output_offset + isec->output_section->vma;
12299 off = addr - htab->toc_curr;
12300 limit = 0x80008000;
12301 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12302 limit = 0x10000;
12303 if (off + isec->size > limit)
12304 {
12305 addr = (htab->toc_first_sec->output_offset
12306 + htab->toc_first_sec->output_section->vma);
12307 htab->toc_curr = addr;
12308 htab->toc_curr &= -TOC_BASE_ALIGN;
12309 }
12310
12311 /* toc_curr is the base address of this toc group. Set elf_gp
12312 for the input section to be the offset relative to the
12313 output toc base plus 0x8000. Making the input elf_gp an
12314 offset allows us to move the toc as a whole without
12315 recalculating input elf_gp. */
12316 off = htab->toc_curr - elf_gp (info->output_bfd);
12317 off += TOC_BASE_OFF;
12318
12319 /* Die if someone uses a linker script that doesn't keep input
12320 file .toc and .got together. */
12321 if (new_bfd
12322 && elf_gp (isec->owner) != 0
12323 && elf_gp (isec->owner) != off)
12324 return FALSE;
12325
12326 elf_gp (isec->owner) = off;
12327 return TRUE;
12328 }
12329
12330 /* During the second pass toc_first_sec points to the start of
12331 a toc group, and toc_curr is used to track the old elf_gp.
12332 We use toc_bfd to ensure we only look at each bfd once. */
12333 if (htab->toc_bfd == isec->owner)
12334 return TRUE;
12335 htab->toc_bfd = isec->owner;
12336
12337 if (htab->toc_first_sec == NULL
12338 || htab->toc_curr != elf_gp (isec->owner))
12339 {
12340 htab->toc_curr = elf_gp (isec->owner);
12341 htab->toc_first_sec = isec;
12342 }
12343 addr = (htab->toc_first_sec->output_offset
12344 + htab->toc_first_sec->output_section->vma);
12345 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12346 elf_gp (isec->owner) = off;
12347
12348 return TRUE;
12349 }
12350
12351 /* Called via elf_link_hash_traverse to merge GOT entries for global
12352 symbol H. */
12353
12354 static bfd_boolean
12355 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12356 {
12357 if (h->root.type == bfd_link_hash_indirect)
12358 return TRUE;
12359
12360 merge_got_entries (&h->got.glist);
12361
12362 return TRUE;
12363 }
12364
12365 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12366 symbol H. */
12367
12368 static bfd_boolean
12369 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12370 {
12371 struct got_entry *gent;
12372
12373 if (h->root.type == bfd_link_hash_indirect)
12374 return TRUE;
12375
12376 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12377 if (!gent->is_indirect)
12378 allocate_got (h, (struct bfd_link_info *) inf, gent);
12379 return TRUE;
12380 }
12381
12382 /* Called on the first multitoc pass after the last call to
12383 ppc64_elf_next_toc_section. This function removes duplicate GOT
12384 entries. */
12385
12386 bfd_boolean
12387 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12388 {
12389 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12390 struct bfd *ibfd, *ibfd2;
12391 bfd_boolean done_something;
12392
12393 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12394
12395 if (!htab->do_multi_toc)
12396 return FALSE;
12397
12398 /* Merge global sym got entries within a toc group. */
12399 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12400
12401 /* And tlsld_got. */
12402 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12403 {
12404 struct got_entry *ent, *ent2;
12405
12406 if (!is_ppc64_elf (ibfd))
12407 continue;
12408
12409 ent = ppc64_tlsld_got (ibfd);
12410 if (!ent->is_indirect
12411 && ent->got.offset != (bfd_vma) -1)
12412 {
12413 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12414 {
12415 if (!is_ppc64_elf (ibfd2))
12416 continue;
12417
12418 ent2 = ppc64_tlsld_got (ibfd2);
12419 if (!ent2->is_indirect
12420 && ent2->got.offset != (bfd_vma) -1
12421 && elf_gp (ibfd2) == elf_gp (ibfd))
12422 {
12423 ent2->is_indirect = TRUE;
12424 ent2->got.ent = ent;
12425 }
12426 }
12427 }
12428 }
12429
12430 /* Zap sizes of got sections. */
12431 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12432 htab->elf.irelplt->size -= htab->got_reli_size;
12433 htab->got_reli_size = 0;
12434
12435 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12436 {
12437 asection *got, *relgot;
12438
12439 if (!is_ppc64_elf (ibfd))
12440 continue;
12441
12442 got = ppc64_elf_tdata (ibfd)->got;
12443 if (got != NULL)
12444 {
12445 got->rawsize = got->size;
12446 got->size = 0;
12447 relgot = ppc64_elf_tdata (ibfd)->relgot;
12448 relgot->rawsize = relgot->size;
12449 relgot->size = 0;
12450 }
12451 }
12452
12453 /* Now reallocate the got, local syms first. We don't need to
12454 allocate section contents again since we never increase size. */
12455 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12456 {
12457 struct got_entry **lgot_ents;
12458 struct got_entry **end_lgot_ents;
12459 struct plt_entry **local_plt;
12460 struct plt_entry **end_local_plt;
12461 unsigned char *lgot_masks;
12462 bfd_size_type locsymcount;
12463 Elf_Internal_Shdr *symtab_hdr;
12464 asection *s;
12465
12466 if (!is_ppc64_elf (ibfd))
12467 continue;
12468
12469 lgot_ents = elf_local_got_ents (ibfd);
12470 if (!lgot_ents)
12471 continue;
12472
12473 symtab_hdr = &elf_symtab_hdr (ibfd);
12474 locsymcount = symtab_hdr->sh_info;
12475 end_lgot_ents = lgot_ents + locsymcount;
12476 local_plt = (struct plt_entry **) end_lgot_ents;
12477 end_local_plt = local_plt + locsymcount;
12478 lgot_masks = (unsigned char *) end_local_plt;
12479 s = ppc64_elf_tdata (ibfd)->got;
12480 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12481 {
12482 struct got_entry *ent;
12483
12484 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12485 {
12486 unsigned int ent_size = 8;
12487 unsigned int rel_size = sizeof (Elf64_External_Rela);
12488
12489 ent->got.offset = s->size;
12490 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12491 {
12492 ent_size *= 2;
12493 rel_size *= 2;
12494 }
12495 s->size += ent_size;
12496 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12497 {
12498 htab->elf.irelplt->size += rel_size;
12499 htab->got_reli_size += rel_size;
12500 }
12501 else if (bfd_link_pic (info)
12502 && !((ent->tls_type & TLS_TPREL) != 0
12503 && bfd_link_executable (info)))
12504 {
12505 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12506 srel->size += rel_size;
12507 }
12508 }
12509 }
12510 }
12511
12512 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12513
12514 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12515 {
12516 struct got_entry *ent;
12517
12518 if (!is_ppc64_elf (ibfd))
12519 continue;
12520
12521 ent = ppc64_tlsld_got (ibfd);
12522 if (!ent->is_indirect
12523 && ent->got.offset != (bfd_vma) -1)
12524 {
12525 asection *s = ppc64_elf_tdata (ibfd)->got;
12526 ent->got.offset = s->size;
12527 s->size += 16;
12528 if (bfd_link_pic (info))
12529 {
12530 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12531 srel->size += sizeof (Elf64_External_Rela);
12532 }
12533 }
12534 }
12535
12536 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12537 if (!done_something)
12538 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12539 {
12540 asection *got;
12541
12542 if (!is_ppc64_elf (ibfd))
12543 continue;
12544
12545 got = ppc64_elf_tdata (ibfd)->got;
12546 if (got != NULL)
12547 {
12548 done_something = got->rawsize != got->size;
12549 if (done_something)
12550 break;
12551 }
12552 }
12553
12554 if (done_something)
12555 (*htab->params->layout_sections_again) ();
12556
12557 /* Set up for second pass over toc sections to recalculate elf_gp
12558 on input sections. */
12559 htab->toc_bfd = NULL;
12560 htab->toc_first_sec = NULL;
12561 htab->second_toc_pass = TRUE;
12562 return done_something;
12563 }
12564
12565 /* Called after second pass of multitoc partitioning. */
12566
12567 void
12568 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12569 {
12570 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12571
12572 /* After the second pass, toc_curr tracks the TOC offset used
12573 for code sections below in ppc64_elf_next_input_section. */
12574 htab->toc_curr = TOC_BASE_OFF;
12575 }
12576
12577 /* No toc references were found in ISEC. If the code in ISEC makes no
12578 calls, then there's no need to use toc adjusting stubs when branching
12579 into ISEC. Actually, indirect calls from ISEC are OK as they will
12580 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12581 needed, and 2 if a cyclical call-graph was found but no other reason
12582 for a stub was detected. If called from the top level, a return of
12583 2 means the same as a return of 0. */
12584
12585 static int
12586 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12587 {
12588 int ret;
12589
12590 /* Mark this section as checked. */
12591 isec->call_check_done = 1;
12592
12593 /* We know none of our code bearing sections will need toc stubs. */
12594 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12595 return 0;
12596
12597 if (isec->size == 0)
12598 return 0;
12599
12600 if (isec->output_section == NULL)
12601 return 0;
12602
12603 ret = 0;
12604 if (isec->reloc_count != 0)
12605 {
12606 Elf_Internal_Rela *relstart, *rel;
12607 Elf_Internal_Sym *local_syms;
12608 struct ppc_link_hash_table *htab;
12609
12610 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12611 info->keep_memory);
12612 if (relstart == NULL)
12613 return -1;
12614
12615 /* Look for branches to outside of this section. */
12616 local_syms = NULL;
12617 htab = ppc_hash_table (info);
12618 if (htab == NULL)
12619 return -1;
12620
12621 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12622 {
12623 enum elf_ppc64_reloc_type r_type;
12624 unsigned long r_symndx;
12625 struct elf_link_hash_entry *h;
12626 struct ppc_link_hash_entry *eh;
12627 Elf_Internal_Sym *sym;
12628 asection *sym_sec;
12629 struct _opd_sec_data *opd;
12630 bfd_vma sym_value;
12631 bfd_vma dest;
12632
12633 r_type = ELF64_R_TYPE (rel->r_info);
12634 if (r_type != R_PPC64_REL24
12635 && r_type != R_PPC64_REL24_NOTOC
12636 && r_type != R_PPC64_REL14
12637 && r_type != R_PPC64_REL14_BRTAKEN
12638 && r_type != R_PPC64_REL14_BRNTAKEN
12639 && r_type != R_PPC64_PLTCALL)
12640 continue;
12641
12642 r_symndx = ELF64_R_SYM (rel->r_info);
12643 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12644 isec->owner))
12645 {
12646 ret = -1;
12647 break;
12648 }
12649
12650 /* Calls to dynamic lib functions go through a plt call stub
12651 that uses r2. */
12652 eh = (struct ppc_link_hash_entry *) h;
12653 if (eh != NULL
12654 && (eh->elf.plt.plist != NULL
12655 || (eh->oh != NULL
12656 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12657 {
12658 ret = 1;
12659 break;
12660 }
12661
12662 if (sym_sec == NULL)
12663 /* Ignore other undefined symbols. */
12664 continue;
12665
12666 /* Assume branches to other sections not included in the
12667 link need stubs too, to cover -R and absolute syms. */
12668 if (sym_sec->output_section == NULL)
12669 {
12670 ret = 1;
12671 break;
12672 }
12673
12674 if (h == NULL)
12675 sym_value = sym->st_value;
12676 else
12677 {
12678 if (h->root.type != bfd_link_hash_defined
12679 && h->root.type != bfd_link_hash_defweak)
12680 abort ();
12681 sym_value = h->root.u.def.value;
12682 }
12683 sym_value += rel->r_addend;
12684
12685 /* If this branch reloc uses an opd sym, find the code section. */
12686 opd = get_opd_info (sym_sec);
12687 if (opd != NULL)
12688 {
12689 if (h == NULL && opd->adjust != NULL)
12690 {
12691 long adjust;
12692
12693 adjust = opd->adjust[OPD_NDX (sym_value)];
12694 if (adjust == -1)
12695 /* Assume deleted functions won't ever be called. */
12696 continue;
12697 sym_value += adjust;
12698 }
12699
12700 dest = opd_entry_value (sym_sec, sym_value,
12701 &sym_sec, NULL, FALSE);
12702 if (dest == (bfd_vma) -1)
12703 continue;
12704 }
12705 else
12706 dest = (sym_value
12707 + sym_sec->output_offset
12708 + sym_sec->output_section->vma);
12709
12710 /* Ignore branch to self. */
12711 if (sym_sec == isec)
12712 continue;
12713
12714 /* If the called function uses the toc, we need a stub. */
12715 if (sym_sec->has_toc_reloc
12716 || sym_sec->makes_toc_func_call)
12717 {
12718 ret = 1;
12719 break;
12720 }
12721
12722 /* Assume any branch that needs a long branch stub might in fact
12723 need a plt_branch stub. A plt_branch stub uses r2. */
12724 else if (dest - (isec->output_offset
12725 + isec->output_section->vma
12726 + rel->r_offset) + (1 << 25)
12727 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12728 ? h->other
12729 : sym->st_other))
12730 {
12731 ret = 1;
12732 break;
12733 }
12734
12735 /* If calling back to a section in the process of being
12736 tested, we can't say for sure that no toc adjusting stubs
12737 are needed, so don't return zero. */
12738 else if (sym_sec->call_check_in_progress)
12739 ret = 2;
12740
12741 /* Branches to another section that itself doesn't have any TOC
12742 references are OK. Recursively call ourselves to check. */
12743 else if (!sym_sec->call_check_done)
12744 {
12745 int recur;
12746
12747 /* Mark current section as indeterminate, so that other
12748 sections that call back to current won't be marked as
12749 known. */
12750 isec->call_check_in_progress = 1;
12751 recur = toc_adjusting_stub_needed (info, sym_sec);
12752 isec->call_check_in_progress = 0;
12753
12754 if (recur != 0)
12755 {
12756 ret = recur;
12757 if (recur != 2)
12758 break;
12759 }
12760 }
12761 }
12762
12763 if (local_syms != NULL
12764 && (elf_symtab_hdr (isec->owner).contents
12765 != (unsigned char *) local_syms))
12766 free (local_syms);
12767 if (elf_section_data (isec)->relocs != relstart)
12768 free (relstart);
12769 }
12770
12771 if ((ret & 1) == 0
12772 && isec->map_head.s != NULL
12773 && (strcmp (isec->output_section->name, ".init") == 0
12774 || strcmp (isec->output_section->name, ".fini") == 0))
12775 {
12776 if (isec->map_head.s->has_toc_reloc
12777 || isec->map_head.s->makes_toc_func_call)
12778 ret = 1;
12779 else if (!isec->map_head.s->call_check_done)
12780 {
12781 int recur;
12782 isec->call_check_in_progress = 1;
12783 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12784 isec->call_check_in_progress = 0;
12785 if (recur != 0)
12786 ret = recur;
12787 }
12788 }
12789
12790 if (ret == 1)
12791 isec->makes_toc_func_call = 1;
12792
12793 return ret;
12794 }
12795
12796 /* The linker repeatedly calls this function for each input section,
12797 in the order that input sections are linked into output sections.
12798 Build lists of input sections to determine groupings between which
12799 we may insert linker stubs. */
12800
12801 bfd_boolean
12802 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12803 {
12804 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12805
12806 if (htab == NULL)
12807 return FALSE;
12808
12809 if ((isec->output_section->flags & SEC_CODE) != 0
12810 && isec->output_section->id < htab->sec_info_arr_size)
12811 {
12812 /* This happens to make the list in reverse order,
12813 which is what we want. */
12814 htab->sec_info[isec->id].u.list
12815 = htab->sec_info[isec->output_section->id].u.list;
12816 htab->sec_info[isec->output_section->id].u.list = isec;
12817 }
12818
12819 if (htab->multi_toc_needed)
12820 {
12821 /* Analyse sections that aren't already flagged as needing a
12822 valid toc pointer. Exclude .fixup for the linux kernel.
12823 .fixup contains branches, but only back to the function that
12824 hit an exception. */
12825 if (!(isec->has_toc_reloc
12826 || (isec->flags & SEC_CODE) == 0
12827 || strcmp (isec->name, ".fixup") == 0
12828 || isec->call_check_done))
12829 {
12830 if (toc_adjusting_stub_needed (info, isec) < 0)
12831 return FALSE;
12832 }
12833 /* Make all sections use the TOC assigned for this object file.
12834 This will be wrong for pasted sections; We fix that in
12835 check_pasted_section(). */
12836 if (elf_gp (isec->owner) != 0)
12837 htab->toc_curr = elf_gp (isec->owner);
12838 }
12839
12840 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12841 return TRUE;
12842 }
12843
12844 /* Check that all .init and .fini sections use the same toc, if they
12845 have toc relocs. */
12846
12847 static bfd_boolean
12848 check_pasted_section (struct bfd_link_info *info, const char *name)
12849 {
12850 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12851
12852 if (o != NULL)
12853 {
12854 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12855 bfd_vma toc_off = 0;
12856 asection *i;
12857
12858 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12859 if (i->has_toc_reloc)
12860 {
12861 if (toc_off == 0)
12862 toc_off = htab->sec_info[i->id].toc_off;
12863 else if (toc_off != htab->sec_info[i->id].toc_off)
12864 return FALSE;
12865 }
12866
12867 if (toc_off == 0)
12868 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12869 if (i->makes_toc_func_call)
12870 {
12871 toc_off = htab->sec_info[i->id].toc_off;
12872 break;
12873 }
12874
12875 /* Make sure the whole pasted function uses the same toc offset. */
12876 if (toc_off != 0)
12877 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12878 htab->sec_info[i->id].toc_off = toc_off;
12879 }
12880 return TRUE;
12881 }
12882
12883 bfd_boolean
12884 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12885 {
12886 return (check_pasted_section (info, ".init")
12887 & check_pasted_section (info, ".fini"));
12888 }
12889
12890 /* See whether we can group stub sections together. Grouping stub
12891 sections may result in fewer stubs. More importantly, we need to
12892 put all .init* and .fini* stubs at the beginning of the .init or
12893 .fini output sections respectively, because glibc splits the
12894 _init and _fini functions into multiple parts. Putting a stub in
12895 the middle of a function is not a good idea. */
12896
12897 static bfd_boolean
12898 group_sections (struct bfd_link_info *info,
12899 bfd_size_type stub_group_size,
12900 bfd_boolean stubs_always_before_branch)
12901 {
12902 struct ppc_link_hash_table *htab;
12903 asection *osec;
12904 bfd_boolean suppress_size_errors;
12905
12906 htab = ppc_hash_table (info);
12907 if (htab == NULL)
12908 return FALSE;
12909
12910 suppress_size_errors = FALSE;
12911 if (stub_group_size == 1)
12912 {
12913 /* Default values. */
12914 if (stubs_always_before_branch)
12915 stub_group_size = 0x1e00000;
12916 else
12917 stub_group_size = 0x1c00000;
12918 suppress_size_errors = TRUE;
12919 }
12920
12921 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12922 {
12923 asection *tail;
12924
12925 if (osec->id >= htab->sec_info_arr_size)
12926 continue;
12927
12928 tail = htab->sec_info[osec->id].u.list;
12929 while (tail != NULL)
12930 {
12931 asection *curr;
12932 asection *prev;
12933 bfd_size_type total;
12934 bfd_boolean big_sec;
12935 bfd_vma curr_toc;
12936 struct map_stub *group;
12937 bfd_size_type group_size;
12938
12939 curr = tail;
12940 total = tail->size;
12941 group_size = (ppc64_elf_section_data (tail) != NULL
12942 && ppc64_elf_section_data (tail)->has_14bit_branch
12943 ? stub_group_size >> 10 : stub_group_size);
12944
12945 big_sec = total > group_size;
12946 if (big_sec && !suppress_size_errors)
12947 /* xgettext:c-format */
12948 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12949 tail->owner, tail);
12950 curr_toc = htab->sec_info[tail->id].toc_off;
12951
12952 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12953 && ((total += curr->output_offset - prev->output_offset)
12954 < (ppc64_elf_section_data (prev) != NULL
12955 && ppc64_elf_section_data (prev)->has_14bit_branch
12956 ? (group_size = stub_group_size >> 10) : group_size))
12957 && htab->sec_info[prev->id].toc_off == curr_toc)
12958 curr = prev;
12959
12960 /* OK, the size from the start of CURR to the end is less
12961 than group_size and thus can be handled by one stub
12962 section. (or the tail section is itself larger than
12963 group_size, in which case we may be toast.) We should
12964 really be keeping track of the total size of stubs added
12965 here, as stubs contribute to the final output section
12966 size. That's a little tricky, and this way will only
12967 break if stubs added make the total size more than 2^25,
12968 ie. for the default stub_group_size, if stubs total more
12969 than 2097152 bytes, or nearly 75000 plt call stubs. */
12970 group = bfd_alloc (curr->owner, sizeof (*group));
12971 if (group == NULL)
12972 return FALSE;
12973 group->link_sec = curr;
12974 group->stub_sec = NULL;
12975 group->needs_save_res = 0;
12976 group->lr_restore = 0;
12977 group->eh_size = 0;
12978 group->eh_base = 0;
12979 group->next = htab->group;
12980 htab->group = group;
12981 do
12982 {
12983 prev = htab->sec_info[tail->id].u.list;
12984 /* Set up this stub group. */
12985 htab->sec_info[tail->id].u.group = group;
12986 }
12987 while (tail != curr && (tail = prev) != NULL);
12988
12989 /* But wait, there's more! Input sections up to group_size
12990 bytes before the stub section can be handled by it too.
12991 Don't do this if we have a really large section after the
12992 stubs, as adding more stubs increases the chance that
12993 branches may not reach into the stub section. */
12994 if (!stubs_always_before_branch && !big_sec)
12995 {
12996 total = 0;
12997 while (prev != NULL
12998 && ((total += tail->output_offset - prev->output_offset)
12999 < (ppc64_elf_section_data (prev) != NULL
13000 && ppc64_elf_section_data (prev)->has_14bit_branch
13001 ? (group_size = stub_group_size >> 10)
13002 : group_size))
13003 && htab->sec_info[prev->id].toc_off == curr_toc)
13004 {
13005 tail = prev;
13006 prev = htab->sec_info[tail->id].u.list;
13007 htab->sec_info[tail->id].u.group = group;
13008 }
13009 }
13010 tail = prev;
13011 }
13012 }
13013 return TRUE;
13014 }
13015
13016 static const unsigned char glink_eh_frame_cie[] =
13017 {
13018 0, 0, 0, 16, /* length. */
13019 0, 0, 0, 0, /* id. */
13020 1, /* CIE version. */
13021 'z', 'R', 0, /* Augmentation string. */
13022 4, /* Code alignment. */
13023 0x78, /* Data alignment. */
13024 65, /* RA reg. */
13025 1, /* Augmentation size. */
13026 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13027 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13028 };
13029
13030 /* Stripping output sections is normally done before dynamic section
13031 symbols have been allocated. This function is called later, and
13032 handles cases like htab->brlt which is mapped to its own output
13033 section. */
13034
13035 static void
13036 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13037 {
13038 if (isec->size == 0
13039 && isec->output_section->size == 0
13040 && !(isec->output_section->flags & SEC_KEEP)
13041 && !bfd_section_removed_from_list (info->output_bfd,
13042 isec->output_section)
13043 && elf_section_data (isec->output_section)->dynindx == 0)
13044 {
13045 isec->output_section->flags |= SEC_EXCLUDE;
13046 bfd_section_list_remove (info->output_bfd, isec->output_section);
13047 info->output_bfd->section_count--;
13048 }
13049 }
13050
13051 /* Determine and set the size of the stub section for a final link.
13052
13053 The basic idea here is to examine all the relocations looking for
13054 PC-relative calls to a target that is unreachable with a "bl"
13055 instruction. */
13056
13057 bfd_boolean
13058 ppc64_elf_size_stubs (struct bfd_link_info *info)
13059 {
13060 bfd_size_type stub_group_size;
13061 bfd_boolean stubs_always_before_branch;
13062 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13063
13064 if (htab == NULL)
13065 return FALSE;
13066
13067 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13068 htab->params->plt_thread_safe = 1;
13069 if (!htab->opd_abi)
13070 htab->params->plt_thread_safe = 0;
13071 else if (htab->params->plt_thread_safe == -1)
13072 {
13073 static const char *const thread_starter[] =
13074 {
13075 "pthread_create",
13076 /* libstdc++ */
13077 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13078 /* librt */
13079 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13080 "mq_notify", "create_timer",
13081 /* libanl */
13082 "getaddrinfo_a",
13083 /* libgomp */
13084 "GOMP_parallel",
13085 "GOMP_parallel_start",
13086 "GOMP_parallel_loop_static",
13087 "GOMP_parallel_loop_static_start",
13088 "GOMP_parallel_loop_dynamic",
13089 "GOMP_parallel_loop_dynamic_start",
13090 "GOMP_parallel_loop_guided",
13091 "GOMP_parallel_loop_guided_start",
13092 "GOMP_parallel_loop_runtime",
13093 "GOMP_parallel_loop_runtime_start",
13094 "GOMP_parallel_sections",
13095 "GOMP_parallel_sections_start",
13096 /* libgo */
13097 "__go_go",
13098 };
13099 unsigned i;
13100
13101 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13102 {
13103 struct elf_link_hash_entry *h;
13104 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13105 FALSE, FALSE, TRUE);
13106 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13107 if (htab->params->plt_thread_safe)
13108 break;
13109 }
13110 }
13111 stubs_always_before_branch = htab->params->group_size < 0;
13112 if (htab->params->group_size < 0)
13113 stub_group_size = -htab->params->group_size;
13114 else
13115 stub_group_size = htab->params->group_size;
13116
13117 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13118 return FALSE;
13119
13120 #define STUB_SHRINK_ITER 20
13121 /* Loop until no stubs added. After iteration 20 of this loop we may
13122 exit on a stub section shrinking. This is to break out of a
13123 pathological case where adding stubs on one iteration decreases
13124 section gaps (perhaps due to alignment), which then requires
13125 fewer or smaller stubs on the next iteration. */
13126
13127 while (1)
13128 {
13129 bfd *input_bfd;
13130 unsigned int bfd_indx;
13131 struct map_stub *group;
13132
13133 htab->stub_iteration += 1;
13134
13135 for (input_bfd = info->input_bfds, bfd_indx = 0;
13136 input_bfd != NULL;
13137 input_bfd = input_bfd->link.next, bfd_indx++)
13138 {
13139 Elf_Internal_Shdr *symtab_hdr;
13140 asection *section;
13141 Elf_Internal_Sym *local_syms = NULL;
13142
13143 if (!is_ppc64_elf (input_bfd))
13144 continue;
13145
13146 /* We'll need the symbol table in a second. */
13147 symtab_hdr = &elf_symtab_hdr (input_bfd);
13148 if (symtab_hdr->sh_info == 0)
13149 continue;
13150
13151 /* Walk over each section attached to the input bfd. */
13152 for (section = input_bfd->sections;
13153 section != NULL;
13154 section = section->next)
13155 {
13156 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13157
13158 /* If there aren't any relocs, then there's nothing more
13159 to do. */
13160 if ((section->flags & SEC_RELOC) == 0
13161 || (section->flags & SEC_ALLOC) == 0
13162 || (section->flags & SEC_LOAD) == 0
13163 || (section->flags & SEC_CODE) == 0
13164 || section->reloc_count == 0)
13165 continue;
13166
13167 /* If this section is a link-once section that will be
13168 discarded, then don't create any stubs. */
13169 if (section->output_section == NULL
13170 || section->output_section->owner != info->output_bfd)
13171 continue;
13172
13173 /* Get the relocs. */
13174 internal_relocs
13175 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13176 info->keep_memory);
13177 if (internal_relocs == NULL)
13178 goto error_ret_free_local;
13179
13180 /* Now examine each relocation. */
13181 irela = internal_relocs;
13182 irelaend = irela + section->reloc_count;
13183 for (; irela < irelaend; irela++)
13184 {
13185 enum elf_ppc64_reloc_type r_type;
13186 unsigned int r_indx;
13187 enum ppc_stub_type stub_type;
13188 struct ppc_stub_hash_entry *stub_entry;
13189 asection *sym_sec, *code_sec;
13190 bfd_vma sym_value, code_value;
13191 bfd_vma destination;
13192 unsigned long local_off;
13193 bfd_boolean ok_dest;
13194 struct ppc_link_hash_entry *hash;
13195 struct ppc_link_hash_entry *fdh;
13196 struct elf_link_hash_entry *h;
13197 Elf_Internal_Sym *sym;
13198 char *stub_name;
13199 const asection *id_sec;
13200 struct _opd_sec_data *opd;
13201 struct plt_entry *plt_ent;
13202
13203 r_type = ELF64_R_TYPE (irela->r_info);
13204 r_indx = ELF64_R_SYM (irela->r_info);
13205
13206 if (r_type >= R_PPC64_max)
13207 {
13208 bfd_set_error (bfd_error_bad_value);
13209 goto error_ret_free_internal;
13210 }
13211
13212 /* Only look for stubs on branch instructions. */
13213 if (r_type != R_PPC64_REL24
13214 && r_type != R_PPC64_REL24_NOTOC
13215 && r_type != R_PPC64_REL14
13216 && r_type != R_PPC64_REL14_BRTAKEN
13217 && r_type != R_PPC64_REL14_BRNTAKEN)
13218 continue;
13219
13220 /* Now determine the call target, its name, value,
13221 section. */
13222 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13223 r_indx, input_bfd))
13224 goto error_ret_free_internal;
13225 hash = (struct ppc_link_hash_entry *) h;
13226
13227 ok_dest = FALSE;
13228 fdh = NULL;
13229 sym_value = 0;
13230 if (hash == NULL)
13231 {
13232 sym_value = sym->st_value;
13233 if (sym_sec != NULL
13234 && sym_sec->output_section != NULL)
13235 ok_dest = TRUE;
13236 }
13237 else if (hash->elf.root.type == bfd_link_hash_defined
13238 || hash->elf.root.type == bfd_link_hash_defweak)
13239 {
13240 sym_value = hash->elf.root.u.def.value;
13241 if (sym_sec->output_section != NULL)
13242 ok_dest = TRUE;
13243 }
13244 else if (hash->elf.root.type == bfd_link_hash_undefweak
13245 || hash->elf.root.type == bfd_link_hash_undefined)
13246 {
13247 /* Recognise an old ABI func code entry sym, and
13248 use the func descriptor sym instead if it is
13249 defined. */
13250 if (hash->elf.root.root.string[0] == '.'
13251 && hash->oh != NULL)
13252 {
13253 fdh = ppc_follow_link (hash->oh);
13254 if (fdh->elf.root.type == bfd_link_hash_defined
13255 || fdh->elf.root.type == bfd_link_hash_defweak)
13256 {
13257 sym_sec = fdh->elf.root.u.def.section;
13258 sym_value = fdh->elf.root.u.def.value;
13259 if (sym_sec->output_section != NULL)
13260 ok_dest = TRUE;
13261 }
13262 else
13263 fdh = NULL;
13264 }
13265 }
13266 else
13267 {
13268 bfd_set_error (bfd_error_bad_value);
13269 goto error_ret_free_internal;
13270 }
13271
13272 destination = 0;
13273 local_off = 0;
13274 if (ok_dest)
13275 {
13276 sym_value += irela->r_addend;
13277 destination = (sym_value
13278 + sym_sec->output_offset
13279 + sym_sec->output_section->vma);
13280 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13281 ? hash->elf.other
13282 : sym->st_other);
13283 }
13284
13285 code_sec = sym_sec;
13286 code_value = sym_value;
13287 opd = get_opd_info (sym_sec);
13288 if (opd != NULL)
13289 {
13290 bfd_vma dest;
13291
13292 if (hash == NULL && opd->adjust != NULL)
13293 {
13294 long adjust = opd->adjust[OPD_NDX (sym_value)];
13295 if (adjust == -1)
13296 continue;
13297 code_value += adjust;
13298 sym_value += adjust;
13299 }
13300 dest = opd_entry_value (sym_sec, sym_value,
13301 &code_sec, &code_value, FALSE);
13302 if (dest != (bfd_vma) -1)
13303 {
13304 destination = dest;
13305 if (fdh != NULL)
13306 {
13307 /* Fixup old ABI sym to point at code
13308 entry. */
13309 hash->elf.root.type = bfd_link_hash_defweak;
13310 hash->elf.root.u.def.section = code_sec;
13311 hash->elf.root.u.def.value = code_value;
13312 }
13313 }
13314 }
13315
13316 /* Determine what (if any) linker stub is needed. */
13317 plt_ent = NULL;
13318 stub_type = ppc_type_of_stub (section, irela, &hash,
13319 &plt_ent, destination,
13320 local_off);
13321
13322 if (r_type == R_PPC64_REL24_NOTOC)
13323 {
13324 if (stub_type == ppc_stub_plt_call)
13325 stub_type = ppc_stub_plt_call_notoc;
13326 else if (stub_type == ppc_stub_long_branch
13327 || (code_sec != NULL
13328 && code_sec->output_section != NULL
13329 && (((hash ? hash->elf.other : sym->st_other)
13330 & STO_PPC64_LOCAL_MASK)
13331 != 1 << STO_PPC64_LOCAL_BIT)))
13332 stub_type = ppc_stub_long_branch_notoc;
13333 }
13334 else if (stub_type != ppc_stub_plt_call)
13335 {
13336 /* Check whether we need a TOC adjusting stub.
13337 Since the linker pastes together pieces from
13338 different object files when creating the
13339 _init and _fini functions, it may be that a
13340 call to what looks like a local sym is in
13341 fact a call needing a TOC adjustment. */
13342 if ((code_sec != NULL
13343 && code_sec->output_section != NULL
13344 && (htab->sec_info[code_sec->id].toc_off
13345 != htab->sec_info[section->id].toc_off)
13346 && (code_sec->has_toc_reloc
13347 || code_sec->makes_toc_func_call))
13348 || (((hash ? hash->elf.other : sym->st_other)
13349 & STO_PPC64_LOCAL_MASK)
13350 == 1 << STO_PPC64_LOCAL_BIT))
13351 stub_type = ppc_stub_long_branch_r2off;
13352 }
13353
13354 if (stub_type == ppc_stub_none)
13355 continue;
13356
13357 /* __tls_get_addr calls might be eliminated. */
13358 if (stub_type != ppc_stub_plt_call
13359 && stub_type != ppc_stub_plt_call_notoc
13360 && hash != NULL
13361 && (hash == htab->tls_get_addr
13362 || hash == htab->tls_get_addr_fd)
13363 && section->has_tls_reloc
13364 && irela != internal_relocs)
13365 {
13366 /* Get tls info. */
13367 unsigned char *tls_mask;
13368
13369 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13370 irela - 1, input_bfd))
13371 goto error_ret_free_internal;
13372 if ((*tls_mask & TLS_TLS) != 0)
13373 continue;
13374 }
13375
13376 if (stub_type == ppc_stub_plt_call)
13377 {
13378 if (!htab->opd_abi
13379 && htab->params->plt_localentry0 != 0
13380 && is_elfv2_localentry0 (&hash->elf))
13381 htab->has_plt_localentry0 = 1;
13382 else if (irela + 1 < irelaend
13383 && irela[1].r_offset == irela->r_offset + 4
13384 && (ELF64_R_TYPE (irela[1].r_info)
13385 == R_PPC64_TOCSAVE))
13386 {
13387 if (!tocsave_find (htab, INSERT,
13388 &local_syms, irela + 1, input_bfd))
13389 goto error_ret_free_internal;
13390 }
13391 else
13392 stub_type = ppc_stub_plt_call_r2save;
13393 }
13394
13395 /* Support for grouping stub sections. */
13396 id_sec = htab->sec_info[section->id].u.group->link_sec;
13397
13398 /* Get the name of this stub. */
13399 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13400 if (!stub_name)
13401 goto error_ret_free_internal;
13402
13403 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13404 stub_name, FALSE, FALSE);
13405 if (stub_entry != NULL)
13406 {
13407 enum ppc_stub_type old_type;
13408 /* A stub has already been created, but it may
13409 not be the required type. We shouldn't be
13410 transitioning from plt_call to long_branch
13411 stubs or vice versa, but we might be
13412 upgrading from plt_call to plt_call_r2save or
13413 from long_branch to long_branch_r2off. */
13414 free (stub_name);
13415 old_type = stub_entry->stub_type;
13416 switch (old_type)
13417 {
13418 default:
13419 abort ();
13420
13421 case ppc_stub_save_res:
13422 continue;
13423
13424 case ppc_stub_plt_call:
13425 case ppc_stub_plt_call_r2save:
13426 case ppc_stub_plt_call_notoc:
13427 case ppc_stub_plt_call_both:
13428 if (stub_type == ppc_stub_plt_call)
13429 continue;
13430 else if (stub_type == ppc_stub_plt_call_r2save)
13431 {
13432 if (old_type == ppc_stub_plt_call_notoc)
13433 stub_type = ppc_stub_plt_call_both;
13434 }
13435 else if (stub_type == ppc_stub_plt_call_notoc)
13436 {
13437 if (old_type == ppc_stub_plt_call_r2save)
13438 stub_type = ppc_stub_plt_call_both;
13439 }
13440 else
13441 abort ();
13442 break;
13443
13444 case ppc_stub_plt_branch:
13445 case ppc_stub_plt_branch_r2off:
13446 case ppc_stub_plt_branch_notoc:
13447 case ppc_stub_plt_branch_both:
13448 old_type += (ppc_stub_long_branch
13449 - ppc_stub_plt_branch);
13450 /* Fall through. */
13451 case ppc_stub_long_branch:
13452 case ppc_stub_long_branch_r2off:
13453 case ppc_stub_long_branch_notoc:
13454 case ppc_stub_long_branch_both:
13455 if (stub_type == ppc_stub_long_branch)
13456 continue;
13457 else if (stub_type == ppc_stub_long_branch_r2off)
13458 {
13459 if (old_type == ppc_stub_long_branch_notoc)
13460 stub_type = ppc_stub_long_branch_both;
13461 }
13462 else if (stub_type == ppc_stub_long_branch_notoc)
13463 {
13464 if (old_type == ppc_stub_long_branch_r2off)
13465 stub_type = ppc_stub_long_branch_both;
13466 }
13467 else
13468 abort ();
13469 break;
13470 }
13471 if (old_type < stub_type)
13472 stub_entry->stub_type = stub_type;
13473 continue;
13474 }
13475
13476 stub_entry = ppc_add_stub (stub_name, section, info);
13477 if (stub_entry == NULL)
13478 {
13479 free (stub_name);
13480 error_ret_free_internal:
13481 if (elf_section_data (section)->relocs == NULL)
13482 free (internal_relocs);
13483 error_ret_free_local:
13484 if (local_syms != NULL
13485 && (symtab_hdr->contents
13486 != (unsigned char *) local_syms))
13487 free (local_syms);
13488 return FALSE;
13489 }
13490
13491 stub_entry->stub_type = stub_type;
13492 if (stub_type >= ppc_stub_plt_call
13493 && stub_type <= ppc_stub_plt_call_both)
13494 {
13495 stub_entry->target_value = sym_value;
13496 stub_entry->target_section = sym_sec;
13497 }
13498 else
13499 {
13500 stub_entry->target_value = code_value;
13501 stub_entry->target_section = code_sec;
13502 }
13503 stub_entry->h = hash;
13504 stub_entry->plt_ent = plt_ent;
13505 stub_entry->symtype
13506 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13507 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13508
13509 if (stub_entry->h != NULL)
13510 htab->stub_globals += 1;
13511 }
13512
13513 /* We're done with the internal relocs, free them. */
13514 if (elf_section_data (section)->relocs != internal_relocs)
13515 free (internal_relocs);
13516 }
13517
13518 if (local_syms != NULL
13519 && symtab_hdr->contents != (unsigned char *) local_syms)
13520 {
13521 if (!info->keep_memory)
13522 free (local_syms);
13523 else
13524 symtab_hdr->contents = (unsigned char *) local_syms;
13525 }
13526 }
13527
13528 /* We may have added some stubs. Find out the new size of the
13529 stub sections. */
13530 for (group = htab->group; group != NULL; group = group->next)
13531 {
13532 group->lr_restore = 0;
13533 group->eh_size = 0;
13534 if (group->stub_sec != NULL)
13535 {
13536 asection *stub_sec = group->stub_sec;
13537
13538 if (htab->stub_iteration <= STUB_SHRINK_ITER
13539 || stub_sec->rawsize < stub_sec->size)
13540 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13541 stub_sec->rawsize = stub_sec->size;
13542 stub_sec->size = 0;
13543 stub_sec->reloc_count = 0;
13544 stub_sec->flags &= ~SEC_RELOC;
13545 }
13546 }
13547
13548 if (htab->stub_iteration <= STUB_SHRINK_ITER
13549 || htab->brlt->rawsize < htab->brlt->size)
13550 htab->brlt->rawsize = htab->brlt->size;
13551 htab->brlt->size = 0;
13552 htab->brlt->reloc_count = 0;
13553 htab->brlt->flags &= ~SEC_RELOC;
13554 if (htab->relbrlt != NULL)
13555 htab->relbrlt->size = 0;
13556
13557 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13558
13559 for (group = htab->group; group != NULL; group = group->next)
13560 if (group->needs_save_res)
13561 group->stub_sec->size += htab->sfpr->size;
13562
13563 if (info->emitrelocations
13564 && htab->glink != NULL && htab->glink->size != 0)
13565 {
13566 htab->glink->reloc_count = 1;
13567 htab->glink->flags |= SEC_RELOC;
13568 }
13569
13570 if (htab->glink_eh_frame != NULL
13571 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13572 && htab->glink_eh_frame->output_section->size > 8)
13573 {
13574 size_t size = 0, align = 4;
13575
13576 for (group = htab->group; group != NULL; group = group->next)
13577 if (group->eh_size != 0)
13578 size += (group->eh_size + 17 + align - 1) & -align;
13579 if (htab->glink != NULL && htab->glink->size != 0)
13580 size += (24 + align - 1) & -align;
13581 if (size != 0)
13582 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13583 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13584 size = (size + align - 1) & -align;
13585 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13586 htab->glink_eh_frame->size = size;
13587 }
13588
13589 if (htab->params->plt_stub_align != 0)
13590 for (group = htab->group; group != NULL; group = group->next)
13591 if (group->stub_sec != NULL)
13592 {
13593 int align = abs (htab->params->plt_stub_align);
13594 group->stub_sec->size
13595 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13596 }
13597
13598 for (group = htab->group; group != NULL; group = group->next)
13599 if (group->stub_sec != NULL
13600 && group->stub_sec->rawsize != group->stub_sec->size
13601 && (htab->stub_iteration <= STUB_SHRINK_ITER
13602 || group->stub_sec->rawsize < group->stub_sec->size))
13603 break;
13604
13605 if (group == NULL
13606 && (htab->brlt->rawsize == htab->brlt->size
13607 || (htab->stub_iteration > STUB_SHRINK_ITER
13608 && htab->brlt->rawsize > htab->brlt->size))
13609 && (htab->glink_eh_frame == NULL
13610 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13611 break;
13612
13613 /* Ask the linker to do its stuff. */
13614 (*htab->params->layout_sections_again) ();
13615 }
13616
13617 if (htab->glink_eh_frame != NULL
13618 && htab->glink_eh_frame->size != 0)
13619 {
13620 bfd_vma val;
13621 bfd_byte *p, *last_fde;
13622 size_t last_fde_len, size, align, pad;
13623 struct map_stub *group;
13624
13625 /* It is necessary to at least have a rough outline of the
13626 linker generated CIEs and FDEs written before
13627 bfd_elf_discard_info is run, in order for these FDEs to be
13628 indexed in .eh_frame_hdr. */
13629 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13630 if (p == NULL)
13631 return FALSE;
13632 htab->glink_eh_frame->contents = p;
13633 last_fde = p;
13634 align = 4;
13635
13636 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13637 /* CIE length (rewrite in case little-endian). */
13638 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13639 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13640 p += last_fde_len + 4;
13641
13642 for (group = htab->group; group != NULL; group = group->next)
13643 if (group->eh_size != 0)
13644 {
13645 group->eh_base = p - htab->glink_eh_frame->contents;
13646 last_fde = p;
13647 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13648 /* FDE length. */
13649 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13650 p += 4;
13651 /* CIE pointer. */
13652 val = p - htab->glink_eh_frame->contents;
13653 bfd_put_32 (htab->elf.dynobj, val, p);
13654 p += 4;
13655 /* Offset to stub section, written later. */
13656 p += 4;
13657 /* stub section size. */
13658 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13659 p += 4;
13660 /* Augmentation. */
13661 p += 1;
13662 /* Make sure we don't have all nops. This is enough for
13663 elf-eh-frame.c to detect the last non-nop opcode. */
13664 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13665 p = last_fde + last_fde_len + 4;
13666 }
13667 if (htab->glink != NULL && htab->glink->size != 0)
13668 {
13669 last_fde = p;
13670 last_fde_len = ((24 + align - 1) & -align) - 4;
13671 /* FDE length. */
13672 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13673 p += 4;
13674 /* CIE pointer. */
13675 val = p - htab->glink_eh_frame->contents;
13676 bfd_put_32 (htab->elf.dynobj, val, p);
13677 p += 4;
13678 /* Offset to .glink, written later. */
13679 p += 4;
13680 /* .glink size. */
13681 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13682 p += 4;
13683 /* Augmentation. */
13684 p += 1;
13685
13686 *p++ = DW_CFA_advance_loc + 1;
13687 *p++ = DW_CFA_register;
13688 *p++ = 65;
13689 *p++ = htab->opd_abi ? 12 : 0;
13690 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13691 *p++ = DW_CFA_restore_extended;
13692 *p++ = 65;
13693 p += ((24 + align - 1) & -align) - 24;
13694 }
13695 /* Subsume any padding into the last FDE if user .eh_frame
13696 sections are aligned more than glink_eh_frame. Otherwise any
13697 zero padding will be seen as a terminator. */
13698 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13699 size = p - htab->glink_eh_frame->contents;
13700 pad = ((size + align - 1) & -align) - size;
13701 htab->glink_eh_frame->size = size + pad;
13702 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13703 }
13704
13705 maybe_strip_output (info, htab->brlt);
13706 if (htab->glink_eh_frame != NULL)
13707 maybe_strip_output (info, htab->glink_eh_frame);
13708
13709 return TRUE;
13710 }
13711
13712 /* Called after we have determined section placement. If sections
13713 move, we'll be called again. Provide a value for TOCstart. */
13714
13715 bfd_vma
13716 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13717 {
13718 asection *s;
13719 bfd_vma TOCstart, adjust;
13720
13721 if (info != NULL)
13722 {
13723 struct elf_link_hash_entry *h;
13724 struct elf_link_hash_table *htab = elf_hash_table (info);
13725
13726 if (is_elf_hash_table (htab)
13727 && htab->hgot != NULL)
13728 h = htab->hgot;
13729 else
13730 {
13731 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13732 if (is_elf_hash_table (htab))
13733 htab->hgot = h;
13734 }
13735 if (h != NULL
13736 && h->root.type == bfd_link_hash_defined
13737 && !h->root.linker_def
13738 && (!is_elf_hash_table (htab)
13739 || h->def_regular))
13740 {
13741 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13742 + h->root.u.def.section->output_offset
13743 + h->root.u.def.section->output_section->vma);
13744 _bfd_set_gp_value (obfd, TOCstart);
13745 return TOCstart;
13746 }
13747 }
13748
13749 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13750 order. The TOC starts where the first of these sections starts. */
13751 s = bfd_get_section_by_name (obfd, ".got");
13752 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13753 s = bfd_get_section_by_name (obfd, ".toc");
13754 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13755 s = bfd_get_section_by_name (obfd, ".tocbss");
13756 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13757 s = bfd_get_section_by_name (obfd, ".plt");
13758 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13759 {
13760 /* This may happen for
13761 o references to TOC base (SYM@toc / TOC[tc0]) without a
13762 .toc directive
13763 o bad linker script
13764 o --gc-sections and empty TOC sections
13765
13766 FIXME: Warn user? */
13767
13768 /* Look for a likely section. We probably won't even be
13769 using TOCstart. */
13770 for (s = obfd->sections; s != NULL; s = s->next)
13771 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13772 | SEC_EXCLUDE))
13773 == (SEC_ALLOC | SEC_SMALL_DATA))
13774 break;
13775 if (s == NULL)
13776 for (s = obfd->sections; s != NULL; s = s->next)
13777 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13778 == (SEC_ALLOC | SEC_SMALL_DATA))
13779 break;
13780 if (s == NULL)
13781 for (s = obfd->sections; s != NULL; s = s->next)
13782 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13783 == SEC_ALLOC)
13784 break;
13785 if (s == NULL)
13786 for (s = obfd->sections; s != NULL; s = s->next)
13787 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13788 break;
13789 }
13790
13791 TOCstart = 0;
13792 if (s != NULL)
13793 TOCstart = s->output_section->vma + s->output_offset;
13794
13795 /* Force alignment. */
13796 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13797 TOCstart -= adjust;
13798 _bfd_set_gp_value (obfd, TOCstart);
13799
13800 if (info != NULL && s != NULL)
13801 {
13802 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13803
13804 if (htab != NULL)
13805 {
13806 if (htab->elf.hgot != NULL)
13807 {
13808 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13809 htab->elf.hgot->root.u.def.section = s;
13810 }
13811 }
13812 else
13813 {
13814 struct bfd_link_hash_entry *bh = NULL;
13815 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13816 s, TOC_BASE_OFF - adjust,
13817 NULL, FALSE, FALSE, &bh);
13818 }
13819 }
13820 return TOCstart;
13821 }
13822
13823 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13824 write out any global entry stubs, and PLT relocations. */
13825
13826 static bfd_boolean
13827 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13828 {
13829 struct bfd_link_info *info;
13830 struct ppc_link_hash_table *htab;
13831 struct plt_entry *ent;
13832 asection *s;
13833
13834 if (h->root.type == bfd_link_hash_indirect)
13835 return TRUE;
13836
13837 info = inf;
13838 htab = ppc_hash_table (info);
13839 if (htab == NULL)
13840 return FALSE;
13841
13842 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13843 if (ent->plt.offset != (bfd_vma) -1)
13844 {
13845 /* This symbol has an entry in the procedure linkage
13846 table. Set it up. */
13847 Elf_Internal_Rela rela;
13848 asection *plt, *relplt;
13849 bfd_byte *loc;
13850
13851 if (!htab->elf.dynamic_sections_created
13852 || h->dynindx == -1)
13853 {
13854 if (!(h->def_regular
13855 && (h->root.type == bfd_link_hash_defined
13856 || h->root.type == bfd_link_hash_defweak)))
13857 continue;
13858 if (h->type == STT_GNU_IFUNC)
13859 {
13860 plt = htab->elf.iplt;
13861 relplt = htab->elf.irelplt;
13862 htab->local_ifunc_resolver = 1;
13863 if (htab->opd_abi)
13864 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13865 else
13866 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13867 }
13868 else
13869 {
13870 plt = htab->pltlocal;
13871 if (bfd_link_pic (info))
13872 {
13873 relplt = htab->relpltlocal;
13874 if (htab->opd_abi)
13875 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13876 else
13877 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13878 }
13879 else
13880 relplt = NULL;
13881 }
13882 rela.r_addend = (h->root.u.def.value
13883 + h->root.u.def.section->output_offset
13884 + h->root.u.def.section->output_section->vma
13885 + ent->addend);
13886
13887 if (relplt == NULL)
13888 {
13889 loc = plt->contents + ent->plt.offset;
13890 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13891 if (htab->opd_abi)
13892 {
13893 bfd_vma toc = elf_gp (info->output_bfd);
13894 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13895 bfd_put_64 (info->output_bfd, toc, loc + 8);
13896 }
13897 }
13898 else
13899 {
13900 rela.r_offset = (plt->output_section->vma
13901 + plt->output_offset
13902 + ent->plt.offset);
13903 loc = relplt->contents + (relplt->reloc_count++
13904 * sizeof (Elf64_External_Rela));
13905 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13906 }
13907 }
13908 else
13909 {
13910 rela.r_offset = (htab->elf.splt->output_section->vma
13911 + htab->elf.splt->output_offset
13912 + ent->plt.offset);
13913 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13914 rela.r_addend = ent->addend;
13915 loc = (htab->elf.srelplt->contents
13916 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13917 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13918 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13919 htab->maybe_local_ifunc_resolver = 1;
13920 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13921 }
13922 }
13923
13924 if (!h->pointer_equality_needed)
13925 return TRUE;
13926
13927 if (h->def_regular)
13928 return TRUE;
13929
13930 s = htab->global_entry;
13931 if (s == NULL || s->size == 0)
13932 return TRUE;
13933
13934 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13935 if (ent->plt.offset != (bfd_vma) -1
13936 && ent->addend == 0)
13937 {
13938 bfd_byte *p;
13939 asection *plt;
13940 bfd_vma off;
13941
13942 p = s->contents + h->root.u.def.value;
13943 plt = htab->elf.splt;
13944 if (!htab->elf.dynamic_sections_created
13945 || h->dynindx == -1)
13946 {
13947 if (h->type == STT_GNU_IFUNC)
13948 plt = htab->elf.iplt;
13949 else
13950 plt = htab->pltlocal;
13951 }
13952 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13953 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13954
13955 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13956 {
13957 info->callbacks->einfo
13958 (_("%P: linkage table error against `%pT'\n"),
13959 h->root.root.string);
13960 bfd_set_error (bfd_error_bad_value);
13961 htab->stub_error = TRUE;
13962 }
13963
13964 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13965 if (htab->params->emit_stub_syms)
13966 {
13967 size_t len = strlen (h->root.root.string);
13968 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13969
13970 if (name == NULL)
13971 return FALSE;
13972
13973 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13974 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13975 if (h == NULL)
13976 return FALSE;
13977 if (h->root.type == bfd_link_hash_new)
13978 {
13979 h->root.type = bfd_link_hash_defined;
13980 h->root.u.def.section = s;
13981 h->root.u.def.value = p - s->contents;
13982 h->ref_regular = 1;
13983 h->def_regular = 1;
13984 h->ref_regular_nonweak = 1;
13985 h->forced_local = 1;
13986 h->non_elf = 0;
13987 h->root.linker_def = 1;
13988 }
13989 }
13990
13991 if (PPC_HA (off) != 0)
13992 {
13993 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13994 p += 4;
13995 }
13996 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13997 p += 4;
13998 bfd_put_32 (s->owner, MTCTR_R12, p);
13999 p += 4;
14000 bfd_put_32 (s->owner, BCTR, p);
14001 break;
14002 }
14003 return TRUE;
14004 }
14005
14006 /* Write PLT relocs for locals. */
14007
14008 static bfd_boolean
14009 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14010 {
14011 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14012 bfd *ibfd;
14013
14014 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14015 {
14016 struct got_entry **lgot_ents, **end_lgot_ents;
14017 struct plt_entry **local_plt, **lplt, **end_local_plt;
14018 Elf_Internal_Shdr *symtab_hdr;
14019 bfd_size_type locsymcount;
14020 Elf_Internal_Sym *local_syms = NULL;
14021 struct plt_entry *ent;
14022
14023 if (!is_ppc64_elf (ibfd))
14024 continue;
14025
14026 lgot_ents = elf_local_got_ents (ibfd);
14027 if (!lgot_ents)
14028 continue;
14029
14030 symtab_hdr = &elf_symtab_hdr (ibfd);
14031 locsymcount = symtab_hdr->sh_info;
14032 end_lgot_ents = lgot_ents + locsymcount;
14033 local_plt = (struct plt_entry **) end_lgot_ents;
14034 end_local_plt = local_plt + locsymcount;
14035 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14036 for (ent = *lplt; ent != NULL; ent = ent->next)
14037 if (ent->plt.offset != (bfd_vma) -1)
14038 {
14039 Elf_Internal_Sym *sym;
14040 asection *sym_sec;
14041 asection *plt, *relplt;
14042 bfd_byte *loc;
14043 bfd_vma val;
14044
14045 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14046 lplt - local_plt, ibfd))
14047 {
14048 if (local_syms != NULL
14049 && symtab_hdr->contents != (unsigned char *) local_syms)
14050 free (local_syms);
14051 return FALSE;
14052 }
14053
14054 val = sym->st_value + ent->addend;
14055 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
14056 if (sym_sec != NULL && sym_sec->output_section != NULL)
14057 val += sym_sec->output_offset + sym_sec->output_section->vma;
14058
14059 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14060 {
14061 htab->local_ifunc_resolver = 1;
14062 plt = htab->elf.iplt;
14063 relplt = htab->elf.irelplt;
14064 }
14065 else
14066 {
14067 plt = htab->pltlocal;
14068 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14069 }
14070
14071 if (relplt == NULL)
14072 {
14073 loc = plt->contents + ent->plt.offset;
14074 bfd_put_64 (info->output_bfd, val, loc);
14075 if (htab->opd_abi)
14076 {
14077 bfd_vma toc = elf_gp (ibfd);
14078 bfd_put_64 (info->output_bfd, toc, loc + 8);
14079 }
14080 }
14081 else
14082 {
14083 Elf_Internal_Rela rela;
14084 rela.r_offset = (ent->plt.offset
14085 + plt->output_offset
14086 + plt->output_section->vma);
14087 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14088 {
14089 if (htab->opd_abi)
14090 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14091 else
14092 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14093 }
14094 else
14095 {
14096 if (htab->opd_abi)
14097 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14098 else
14099 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14100 }
14101 rela.r_addend = val;
14102 loc = relplt->contents + (relplt->reloc_count++
14103 * sizeof (Elf64_External_Rela));
14104 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14105 }
14106 }
14107
14108 if (local_syms != NULL
14109 && symtab_hdr->contents != (unsigned char *) local_syms)
14110 {
14111 if (!info->keep_memory)
14112 free (local_syms);
14113 else
14114 symtab_hdr->contents = (unsigned char *) local_syms;
14115 }
14116 }
14117 return TRUE;
14118 }
14119
14120 /* Build all the stubs associated with the current output file.
14121 The stubs are kept in a hash table attached to the main linker
14122 hash table. This function is called via gldelf64ppc_finish. */
14123
14124 bfd_boolean
14125 ppc64_elf_build_stubs (struct bfd_link_info *info,
14126 char **stats)
14127 {
14128 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14129 struct map_stub *group;
14130 asection *stub_sec;
14131 bfd_byte *p;
14132 int stub_sec_count = 0;
14133
14134 if (htab == NULL)
14135 return FALSE;
14136
14137 /* Allocate memory to hold the linker stubs. */
14138 for (group = htab->group; group != NULL; group = group->next)
14139 {
14140 group->eh_size = 0;
14141 group->lr_restore = 0;
14142 if ((stub_sec = group->stub_sec) != NULL
14143 && stub_sec->size != 0)
14144 {
14145 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14146 stub_sec->size);
14147 if (stub_sec->contents == NULL)
14148 return FALSE;
14149 stub_sec->size = 0;
14150 }
14151 }
14152
14153 if (htab->glink != NULL && htab->glink->size != 0)
14154 {
14155 unsigned int indx;
14156 bfd_vma plt0;
14157
14158 /* Build the .glink plt call stub. */
14159 if (htab->params->emit_stub_syms)
14160 {
14161 struct elf_link_hash_entry *h;
14162 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14163 TRUE, FALSE, FALSE);
14164 if (h == NULL)
14165 return FALSE;
14166 if (h->root.type == bfd_link_hash_new)
14167 {
14168 h->root.type = bfd_link_hash_defined;
14169 h->root.u.def.section = htab->glink;
14170 h->root.u.def.value = 8;
14171 h->ref_regular = 1;
14172 h->def_regular = 1;
14173 h->ref_regular_nonweak = 1;
14174 h->forced_local = 1;
14175 h->non_elf = 0;
14176 h->root.linker_def = 1;
14177 }
14178 }
14179 plt0 = (htab->elf.splt->output_section->vma
14180 + htab->elf.splt->output_offset
14181 - 16);
14182 if (info->emitrelocations)
14183 {
14184 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14185 if (r == NULL)
14186 return FALSE;
14187 r->r_offset = (htab->glink->output_offset
14188 + htab->glink->output_section->vma);
14189 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14190 r->r_addend = plt0;
14191 }
14192 p = htab->glink->contents;
14193 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14194 bfd_put_64 (htab->glink->owner, plt0, p);
14195 p += 8;
14196 if (htab->opd_abi)
14197 {
14198 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14199 p += 4;
14200 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14201 p += 4;
14202 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14203 p += 4;
14204 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14205 p += 4;
14206 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14207 p += 4;
14208 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14209 p += 4;
14210 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14211 p += 4;
14212 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14213 p += 4;
14214 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14215 p += 4;
14216 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14217 p += 4;
14218 }
14219 else
14220 {
14221 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14222 p += 4;
14223 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14224 p += 4;
14225 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14226 p += 4;
14227 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14228 p += 4;
14229 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14230 p += 4;
14231 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14232 p += 4;
14233 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14234 p += 4;
14235 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14236 p += 4;
14237 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
14238 p += 4;
14239 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14240 p += 4;
14241 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14242 p += 4;
14243 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14244 p += 4;
14245 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14246 p += 4;
14247 }
14248 bfd_put_32 (htab->glink->owner, BCTR, p);
14249 p += 4;
14250 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14251
14252 /* Build the .glink lazy link call stubs. */
14253 indx = 0;
14254 while (p < htab->glink->contents + htab->glink->size)
14255 {
14256 if (htab->opd_abi)
14257 {
14258 if (indx < 0x8000)
14259 {
14260 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14261 p += 4;
14262 }
14263 else
14264 {
14265 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14266 p += 4;
14267 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14268 p);
14269 p += 4;
14270 }
14271 }
14272 bfd_put_32 (htab->glink->owner,
14273 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14274 indx++;
14275 p += 4;
14276 }
14277 }
14278
14279 /* Build .glink global entry stubs, and PLT relocs for globals. */
14280 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14281
14282 if (!write_plt_relocs_for_local_syms (info))
14283 return FALSE;
14284
14285 if (htab->brlt != NULL && htab->brlt->size != 0)
14286 {
14287 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14288 htab->brlt->size);
14289 if (htab->brlt->contents == NULL)
14290 return FALSE;
14291 }
14292 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14293 {
14294 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14295 htab->relbrlt->size);
14296 if (htab->relbrlt->contents == NULL)
14297 return FALSE;
14298 }
14299
14300 /* Build the stubs as directed by the stub hash table. */
14301 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14302
14303 for (group = htab->group; group != NULL; group = group->next)
14304 if (group->needs_save_res)
14305 group->stub_sec->size += htab->sfpr->size;
14306
14307 if (htab->relbrlt != NULL)
14308 htab->relbrlt->reloc_count = 0;
14309
14310 if (htab->params->plt_stub_align != 0)
14311 for (group = htab->group; group != NULL; group = group->next)
14312 if ((stub_sec = group->stub_sec) != NULL)
14313 {
14314 int align = abs (htab->params->plt_stub_align);
14315 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14316 }
14317
14318 for (group = htab->group; group != NULL; group = group->next)
14319 if (group->needs_save_res)
14320 {
14321 stub_sec = group->stub_sec;
14322 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14323 htab->sfpr->contents, htab->sfpr->size);
14324 if (htab->params->emit_stub_syms)
14325 {
14326 unsigned int i;
14327
14328 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14329 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14330 return FALSE;
14331 }
14332 }
14333
14334 if (htab->glink_eh_frame != NULL
14335 && htab->glink_eh_frame->size != 0)
14336 {
14337 bfd_vma val;
14338 size_t align = 4;
14339
14340 p = htab->glink_eh_frame->contents;
14341 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14342
14343 for (group = htab->group; group != NULL; group = group->next)
14344 if (group->eh_size != 0)
14345 {
14346 /* Offset to stub section. */
14347 val = (group->stub_sec->output_section->vma
14348 + group->stub_sec->output_offset);
14349 val -= (htab->glink_eh_frame->output_section->vma
14350 + htab->glink_eh_frame->output_offset
14351 + (p + 8 - htab->glink_eh_frame->contents));
14352 if (val + 0x80000000 > 0xffffffff)
14353 {
14354 _bfd_error_handler
14355 (_("%s offset too large for .eh_frame sdata4 encoding"),
14356 group->stub_sec->name);
14357 return FALSE;
14358 }
14359 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14360 p += (group->eh_size + 17 + 3) & -4;
14361 }
14362 if (htab->glink != NULL && htab->glink->size != 0)
14363 {
14364 /* Offset to .glink. */
14365 val = (htab->glink->output_section->vma
14366 + htab->glink->output_offset
14367 + 8);
14368 val -= (htab->glink_eh_frame->output_section->vma
14369 + htab->glink_eh_frame->output_offset
14370 + (p + 8 - htab->glink_eh_frame->contents));
14371 if (val + 0x80000000 > 0xffffffff)
14372 {
14373 _bfd_error_handler
14374 (_("%s offset too large for .eh_frame sdata4 encoding"),
14375 htab->glink->name);
14376 return FALSE;
14377 }
14378 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14379 p += (24 + align - 1) & -align;
14380 }
14381 }
14382
14383 for (group = htab->group; group != NULL; group = group->next)
14384 if ((stub_sec = group->stub_sec) != NULL)
14385 {
14386 stub_sec_count += 1;
14387 if (stub_sec->rawsize != stub_sec->size
14388 && (htab->stub_iteration <= STUB_SHRINK_ITER
14389 || stub_sec->rawsize < stub_sec->size))
14390 break;
14391 }
14392
14393 if (group != NULL)
14394 {
14395 htab->stub_error = TRUE;
14396 _bfd_error_handler (_("stubs don't match calculated size"));
14397 }
14398
14399 if (htab->stub_error)
14400 return FALSE;
14401
14402 if (stats != NULL)
14403 {
14404 size_t len;
14405 *stats = bfd_malloc (500);
14406 if (*stats == NULL)
14407 return FALSE;
14408
14409 len = sprintf (*stats,
14410 ngettext ("linker stubs in %u group\n",
14411 "linker stubs in %u groups\n",
14412 stub_sec_count),
14413 stub_sec_count);
14414 sprintf (*stats + len, _(" branch %lu\n"
14415 " branch toc adj %lu\n"
14416 " branch notoc %lu\n"
14417 " branch both %lu\n"
14418 " long branch %lu\n"
14419 " long toc adj %lu\n"
14420 " long notoc %lu\n"
14421 " long both %lu\n"
14422 " plt call %lu\n"
14423 " plt call save %lu\n"
14424 " plt call notoc %lu\n"
14425 " plt call both %lu\n"
14426 " global entry %lu"),
14427 htab->stub_count[ppc_stub_long_branch - 1],
14428 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14429 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14430 htab->stub_count[ppc_stub_long_branch_both - 1],
14431 htab->stub_count[ppc_stub_plt_branch - 1],
14432 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14433 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14434 htab->stub_count[ppc_stub_plt_branch_both - 1],
14435 htab->stub_count[ppc_stub_plt_call - 1],
14436 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14437 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14438 htab->stub_count[ppc_stub_plt_call_both - 1],
14439 htab->stub_count[ppc_stub_global_entry - 1]);
14440 }
14441 return TRUE;
14442 }
14443
14444 /* What to do when ld finds relocations against symbols defined in
14445 discarded sections. */
14446
14447 static unsigned int
14448 ppc64_elf_action_discarded (asection *sec)
14449 {
14450 if (strcmp (".opd", sec->name) == 0)
14451 return 0;
14452
14453 if (strcmp (".toc", sec->name) == 0)
14454 return 0;
14455
14456 if (strcmp (".toc1", sec->name) == 0)
14457 return 0;
14458
14459 return _bfd_elf_default_action_discarded (sec);
14460 }
14461
14462 /* The RELOCATE_SECTION function is called by the ELF backend linker
14463 to handle the relocations for a section.
14464
14465 The relocs are always passed as Rela structures; if the section
14466 actually uses Rel structures, the r_addend field will always be
14467 zero.
14468
14469 This function is responsible for adjust the section contents as
14470 necessary, and (if using Rela relocs and generating a
14471 relocatable output file) adjusting the reloc addend as
14472 necessary.
14473
14474 This function does not have to worry about setting the reloc
14475 address or the reloc symbol index.
14476
14477 LOCAL_SYMS is a pointer to the swapped in local symbols.
14478
14479 LOCAL_SECTIONS is an array giving the section in the input file
14480 corresponding to the st_shndx field of each local symbol.
14481
14482 The global hash table entry for the global symbols can be found
14483 via elf_sym_hashes (input_bfd).
14484
14485 When generating relocatable output, this function must handle
14486 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14487 going to be the section symbol corresponding to the output
14488 section, which means that the addend must be adjusted
14489 accordingly. */
14490
14491 static bfd_boolean
14492 ppc64_elf_relocate_section (bfd *output_bfd,
14493 struct bfd_link_info *info,
14494 bfd *input_bfd,
14495 asection *input_section,
14496 bfd_byte *contents,
14497 Elf_Internal_Rela *relocs,
14498 Elf_Internal_Sym *local_syms,
14499 asection **local_sections)
14500 {
14501 struct ppc_link_hash_table *htab;
14502 Elf_Internal_Shdr *symtab_hdr;
14503 struct elf_link_hash_entry **sym_hashes;
14504 Elf_Internal_Rela *rel;
14505 Elf_Internal_Rela *wrel;
14506 Elf_Internal_Rela *relend;
14507 Elf_Internal_Rela outrel;
14508 bfd_byte *loc;
14509 struct got_entry **local_got_ents;
14510 bfd_vma TOCstart;
14511 bfd_boolean ret = TRUE;
14512 bfd_boolean is_opd;
14513 /* Assume 'at' branch hints. */
14514 bfd_boolean is_isa_v2 = TRUE;
14515 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14516
14517 /* Initialize howto table if needed. */
14518 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14519 ppc_howto_init ();
14520
14521 htab = ppc_hash_table (info);
14522 if (htab == NULL)
14523 return FALSE;
14524
14525 /* Don't relocate stub sections. */
14526 if (input_section->owner == htab->params->stub_bfd)
14527 return TRUE;
14528
14529 BFD_ASSERT (is_ppc64_elf (input_bfd));
14530
14531 local_got_ents = elf_local_got_ents (input_bfd);
14532 TOCstart = elf_gp (output_bfd);
14533 symtab_hdr = &elf_symtab_hdr (input_bfd);
14534 sym_hashes = elf_sym_hashes (input_bfd);
14535 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14536
14537 rel = wrel = relocs;
14538 relend = relocs + input_section->reloc_count;
14539 for (; rel < relend; wrel++, rel++)
14540 {
14541 enum elf_ppc64_reloc_type r_type;
14542 bfd_vma addend;
14543 bfd_reloc_status_type r;
14544 Elf_Internal_Sym *sym;
14545 asection *sec;
14546 struct elf_link_hash_entry *h_elf;
14547 struct ppc_link_hash_entry *h;
14548 struct ppc_link_hash_entry *fdh;
14549 const char *sym_name;
14550 unsigned long r_symndx, toc_symndx;
14551 bfd_vma toc_addend;
14552 unsigned char tls_mask, tls_gd, tls_type;
14553 unsigned char sym_type;
14554 bfd_vma relocation;
14555 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14556 bfd_boolean warned;
14557 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14558 unsigned int insn;
14559 unsigned int mask;
14560 struct ppc_stub_hash_entry *stub_entry;
14561 bfd_vma max_br_offset;
14562 bfd_vma from;
14563 Elf_Internal_Rela orig_rel;
14564 reloc_howto_type *howto;
14565 struct reloc_howto_struct alt_howto;
14566
14567 again:
14568 orig_rel = *rel;
14569
14570 r_type = ELF64_R_TYPE (rel->r_info);
14571 r_symndx = ELF64_R_SYM (rel->r_info);
14572
14573 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14574 symbol of the previous ADDR64 reloc. The symbol gives us the
14575 proper TOC base to use. */
14576 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14577 && wrel != relocs
14578 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14579 && is_opd)
14580 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14581
14582 sym = NULL;
14583 sec = NULL;
14584 h_elf = NULL;
14585 sym_name = NULL;
14586 unresolved_reloc = FALSE;
14587 warned = FALSE;
14588
14589 if (r_symndx < symtab_hdr->sh_info)
14590 {
14591 /* It's a local symbol. */
14592 struct _opd_sec_data *opd;
14593
14594 sym = local_syms + r_symndx;
14595 sec = local_sections[r_symndx];
14596 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14597 sym_type = ELF64_ST_TYPE (sym->st_info);
14598 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14599 opd = get_opd_info (sec);
14600 if (opd != NULL && opd->adjust != NULL)
14601 {
14602 long adjust = opd->adjust[OPD_NDX (sym->st_value
14603 + rel->r_addend)];
14604 if (adjust == -1)
14605 relocation = 0;
14606 else
14607 {
14608 /* If this is a relocation against the opd section sym
14609 and we have edited .opd, adjust the reloc addend so
14610 that ld -r and ld --emit-relocs output is correct.
14611 If it is a reloc against some other .opd symbol,
14612 then the symbol value will be adjusted later. */
14613 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14614 rel->r_addend += adjust;
14615 else
14616 relocation += adjust;
14617 }
14618 }
14619 }
14620 else
14621 {
14622 bfd_boolean ignored;
14623
14624 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14625 r_symndx, symtab_hdr, sym_hashes,
14626 h_elf, sec, relocation,
14627 unresolved_reloc, warned, ignored);
14628 sym_name = h_elf->root.root.string;
14629 sym_type = h_elf->type;
14630 if (sec != NULL
14631 && sec->owner == output_bfd
14632 && strcmp (sec->name, ".opd") == 0)
14633 {
14634 /* This is a symbol defined in a linker script. All
14635 such are defined in output sections, even those
14636 defined by simple assignment from a symbol defined in
14637 an input section. Transfer the symbol to an
14638 appropriate input .opd section, so that a branch to
14639 this symbol will be mapped to the location specified
14640 by the opd entry. */
14641 struct bfd_link_order *lo;
14642 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14643 if (lo->type == bfd_indirect_link_order)
14644 {
14645 asection *isec = lo->u.indirect.section;
14646 if (h_elf->root.u.def.value >= isec->output_offset
14647 && h_elf->root.u.def.value < (isec->output_offset
14648 + isec->size))
14649 {
14650 h_elf->root.u.def.value -= isec->output_offset;
14651 h_elf->root.u.def.section = isec;
14652 sec = isec;
14653 break;
14654 }
14655 }
14656 }
14657 }
14658 h = (struct ppc_link_hash_entry *) h_elf;
14659
14660 if (sec != NULL && discarded_section (sec))
14661 {
14662 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14663 input_bfd, input_section,
14664 contents + rel->r_offset);
14665 wrel->r_offset = rel->r_offset;
14666 wrel->r_info = 0;
14667 wrel->r_addend = 0;
14668
14669 /* For ld -r, remove relocations in debug sections against
14670 symbols defined in discarded sections. Not done for
14671 non-debug to preserve relocs in .eh_frame which the
14672 eh_frame editing code expects to be present. */
14673 if (bfd_link_relocatable (info)
14674 && (input_section->flags & SEC_DEBUGGING))
14675 wrel--;
14676
14677 continue;
14678 }
14679
14680 if (bfd_link_relocatable (info))
14681 goto copy_reloc;
14682
14683 if (h != NULL && &h->elf == htab->elf.hgot)
14684 {
14685 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14686 sec = bfd_abs_section_ptr;
14687 unresolved_reloc = FALSE;
14688 }
14689
14690 /* TLS optimizations. Replace instruction sequences and relocs
14691 based on information we collected in tls_optimize. We edit
14692 RELOCS so that --emit-relocs will output something sensible
14693 for the final instruction stream. */
14694 tls_mask = 0;
14695 tls_gd = 0;
14696 toc_symndx = 0;
14697 if (h != NULL)
14698 tls_mask = h->tls_mask;
14699 else if (local_got_ents != NULL)
14700 {
14701 struct plt_entry **local_plt = (struct plt_entry **)
14702 (local_got_ents + symtab_hdr->sh_info);
14703 unsigned char *lgot_masks = (unsigned char *)
14704 (local_plt + symtab_hdr->sh_info);
14705 tls_mask = lgot_masks[r_symndx];
14706 }
14707 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14708 && (r_type == R_PPC64_TLS
14709 || r_type == R_PPC64_TLSGD
14710 || r_type == R_PPC64_TLSLD))
14711 {
14712 /* Check for toc tls entries. */
14713 unsigned char *toc_tls;
14714
14715 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14716 &local_syms, rel, input_bfd))
14717 return FALSE;
14718
14719 if (toc_tls)
14720 tls_mask = *toc_tls;
14721 }
14722
14723 /* Check that tls relocs are used with tls syms, and non-tls
14724 relocs are used with non-tls syms. */
14725 if (r_symndx != STN_UNDEF
14726 && r_type != R_PPC64_NONE
14727 && (h == NULL
14728 || h->elf.root.type == bfd_link_hash_defined
14729 || h->elf.root.type == bfd_link_hash_defweak)
14730 && (IS_PPC64_TLS_RELOC (r_type)
14731 != (sym_type == STT_TLS
14732 || (sym_type == STT_SECTION
14733 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14734 {
14735 if ((tls_mask & TLS_TLS) != 0
14736 && (r_type == R_PPC64_TLS
14737 || r_type == R_PPC64_TLSGD
14738 || r_type == R_PPC64_TLSLD))
14739 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14740 ;
14741 else
14742 info->callbacks->einfo
14743 (!IS_PPC64_TLS_RELOC (r_type)
14744 /* xgettext:c-format */
14745 ? _("%H: %s used with TLS symbol `%pT'\n")
14746 /* xgettext:c-format */
14747 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14748 input_bfd, input_section, rel->r_offset,
14749 ppc64_elf_howto_table[r_type]->name,
14750 sym_name);
14751 }
14752
14753 /* Ensure reloc mapping code below stays sane. */
14754 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14755 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14756 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14757 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14758 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14759 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14760 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14761 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14762 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14763 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14764 abort ();
14765
14766 switch (r_type)
14767 {
14768 default:
14769 break;
14770
14771 case R_PPC64_LO_DS_OPT:
14772 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14773 if ((insn & (0x3f << 26)) != 58u << 26)
14774 abort ();
14775 insn += (14u << 26) - (58u << 26);
14776 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14777 r_type = R_PPC64_TOC16_LO;
14778 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14779 break;
14780
14781 case R_PPC64_TOC16:
14782 case R_PPC64_TOC16_LO:
14783 case R_PPC64_TOC16_DS:
14784 case R_PPC64_TOC16_LO_DS:
14785 {
14786 /* Check for toc tls entries. */
14787 unsigned char *toc_tls;
14788 int retval;
14789
14790 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14791 &local_syms, rel, input_bfd);
14792 if (retval == 0)
14793 return FALSE;
14794
14795 if (toc_tls)
14796 {
14797 tls_mask = *toc_tls;
14798 if (r_type == R_PPC64_TOC16_DS
14799 || r_type == R_PPC64_TOC16_LO_DS)
14800 {
14801 if ((tls_mask & TLS_TLS) != 0
14802 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14803 goto toctprel;
14804 }
14805 else
14806 {
14807 /* If we found a GD reloc pair, then we might be
14808 doing a GD->IE transition. */
14809 if (retval == 2)
14810 {
14811 tls_gd = TLS_TPRELGD;
14812 if ((tls_mask & TLS_TLS) != 0
14813 && (tls_mask & TLS_GD) == 0)
14814 goto tls_ldgd_opt;
14815 }
14816 else if (retval == 3)
14817 {
14818 if ((tls_mask & TLS_TLS) != 0
14819 && (tls_mask & TLS_LD) == 0)
14820 goto tls_ldgd_opt;
14821 }
14822 }
14823 }
14824 }
14825 break;
14826
14827 case R_PPC64_GOT_TPREL16_HI:
14828 case R_PPC64_GOT_TPREL16_HA:
14829 if ((tls_mask & TLS_TLS) != 0
14830 && (tls_mask & TLS_TPREL) == 0)
14831 {
14832 rel->r_offset -= d_offset;
14833 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14834 r_type = R_PPC64_NONE;
14835 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14836 }
14837 break;
14838
14839 case R_PPC64_GOT_TPREL16_DS:
14840 case R_PPC64_GOT_TPREL16_LO_DS:
14841 if ((tls_mask & TLS_TLS) != 0
14842 && (tls_mask & TLS_TPREL) == 0)
14843 {
14844 toctprel:
14845 insn = bfd_get_32 (input_bfd,
14846 contents + rel->r_offset - d_offset);
14847 insn &= 31 << 21;
14848 insn |= 0x3c0d0000; /* addis 0,13,0 */
14849 bfd_put_32 (input_bfd, insn,
14850 contents + rel->r_offset - d_offset);
14851 r_type = R_PPC64_TPREL16_HA;
14852 if (toc_symndx != 0)
14853 {
14854 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14855 rel->r_addend = toc_addend;
14856 /* We changed the symbol. Start over in order to
14857 get h, sym, sec etc. right. */
14858 goto again;
14859 }
14860 else
14861 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14862 }
14863 break;
14864
14865 case R_PPC64_TLS:
14866 if ((tls_mask & TLS_TLS) != 0
14867 && (tls_mask & TLS_TPREL) == 0)
14868 {
14869 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14870 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14871 if (insn == 0)
14872 abort ();
14873 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14874 /* Was PPC64_TLS which sits on insn boundary, now
14875 PPC64_TPREL16_LO which is at low-order half-word. */
14876 rel->r_offset += d_offset;
14877 r_type = R_PPC64_TPREL16_LO;
14878 if (toc_symndx != 0)
14879 {
14880 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14881 rel->r_addend = toc_addend;
14882 /* We changed the symbol. Start over in order to
14883 get h, sym, sec etc. right. */
14884 goto again;
14885 }
14886 else
14887 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14888 }
14889 break;
14890
14891 case R_PPC64_GOT_TLSGD16_HI:
14892 case R_PPC64_GOT_TLSGD16_HA:
14893 tls_gd = TLS_TPRELGD;
14894 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14895 goto tls_gdld_hi;
14896 break;
14897
14898 case R_PPC64_GOT_TLSLD16_HI:
14899 case R_PPC64_GOT_TLSLD16_HA:
14900 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14901 {
14902 tls_gdld_hi:
14903 if ((tls_mask & tls_gd) != 0)
14904 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14905 + R_PPC64_GOT_TPREL16_DS);
14906 else
14907 {
14908 rel->r_offset -= d_offset;
14909 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14910 r_type = R_PPC64_NONE;
14911 }
14912 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14913 }
14914 break;
14915
14916 case R_PPC64_GOT_TLSGD16:
14917 case R_PPC64_GOT_TLSGD16_LO:
14918 tls_gd = TLS_TPRELGD;
14919 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14920 goto tls_ldgd_opt;
14921 break;
14922
14923 case R_PPC64_GOT_TLSLD16:
14924 case R_PPC64_GOT_TLSLD16_LO:
14925 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14926 {
14927 unsigned int insn1, insn2;
14928 bfd_vma offset;
14929
14930 tls_ldgd_opt:
14931 offset = (bfd_vma) -1;
14932 /* If not using the newer R_PPC64_TLSGD/LD to mark
14933 __tls_get_addr calls, we must trust that the call
14934 stays with its arg setup insns, ie. that the next
14935 reloc is the __tls_get_addr call associated with
14936 the current reloc. Edit both insns. */
14937 if (input_section->has_tls_get_addr_call
14938 && rel + 1 < relend
14939 && branch_reloc_hash_match (input_bfd, rel + 1,
14940 htab->tls_get_addr,
14941 htab->tls_get_addr_fd))
14942 offset = rel[1].r_offset;
14943 /* We read the low GOT_TLS (or TOC16) insn because we
14944 need to keep the destination reg. It may be
14945 something other than the usual r3, and moved to r3
14946 before the call by intervening code. */
14947 insn1 = bfd_get_32 (input_bfd,
14948 contents + rel->r_offset - d_offset);
14949 if ((tls_mask & tls_gd) != 0)
14950 {
14951 /* IE */
14952 insn1 &= (0x1f << 21) | (0x1f << 16);
14953 insn1 |= 58 << 26; /* ld */
14954 insn2 = 0x7c636a14; /* add 3,3,13 */
14955 if (offset != (bfd_vma) -1)
14956 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14957 if ((tls_mask & TLS_EXPLICIT) == 0)
14958 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14959 + R_PPC64_GOT_TPREL16_DS);
14960 else
14961 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14962 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14963 }
14964 else
14965 {
14966 /* LE */
14967 insn1 &= 0x1f << 21;
14968 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14969 insn2 = 0x38630000; /* addi 3,3,0 */
14970 if (tls_gd == 0)
14971 {
14972 /* Was an LD reloc. */
14973 if (toc_symndx)
14974 sec = local_sections[toc_symndx];
14975 for (r_symndx = 0;
14976 r_symndx < symtab_hdr->sh_info;
14977 r_symndx++)
14978 if (local_sections[r_symndx] == sec)
14979 break;
14980 if (r_symndx >= symtab_hdr->sh_info)
14981 r_symndx = STN_UNDEF;
14982 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14983 if (r_symndx != STN_UNDEF)
14984 rel->r_addend -= (local_syms[r_symndx].st_value
14985 + sec->output_offset
14986 + sec->output_section->vma);
14987 }
14988 else if (toc_symndx != 0)
14989 {
14990 r_symndx = toc_symndx;
14991 rel->r_addend = toc_addend;
14992 }
14993 r_type = R_PPC64_TPREL16_HA;
14994 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14995 if (offset != (bfd_vma) -1)
14996 {
14997 rel[1].r_info = ELF64_R_INFO (r_symndx,
14998 R_PPC64_TPREL16_LO);
14999 rel[1].r_offset = offset + d_offset;
15000 rel[1].r_addend = rel->r_addend;
15001 }
15002 }
15003 bfd_put_32 (input_bfd, insn1,
15004 contents + rel->r_offset - d_offset);
15005 if (offset != (bfd_vma) -1)
15006 bfd_put_32 (input_bfd, insn2, contents + offset);
15007 if ((tls_mask & tls_gd) == 0
15008 && (tls_gd == 0 || toc_symndx != 0))
15009 {
15010 /* We changed the symbol. Start over in order
15011 to get h, sym, sec etc. right. */
15012 goto again;
15013 }
15014 }
15015 break;
15016
15017 case R_PPC64_TLSGD:
15018 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15019 && rel + 1 < relend)
15020 {
15021 unsigned int insn2;
15022 bfd_vma offset = rel->r_offset;
15023
15024 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
15025 {
15026 bfd_put_32 (output_bfd, NOP, contents + offset);
15027 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15028 break;
15029 }
15030
15031 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15032 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15033
15034 if ((tls_mask & TLS_TPRELGD) != 0)
15035 {
15036 /* IE */
15037 r_type = R_PPC64_NONE;
15038 insn2 = 0x7c636a14; /* add 3,3,13 */
15039 }
15040 else
15041 {
15042 /* LE */
15043 if (toc_symndx != 0)
15044 {
15045 r_symndx = toc_symndx;
15046 rel->r_addend = toc_addend;
15047 }
15048 r_type = R_PPC64_TPREL16_LO;
15049 rel->r_offset = offset + d_offset;
15050 insn2 = 0x38630000; /* addi 3,3,0 */
15051 }
15052 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15053 /* Zap the reloc on the _tls_get_addr call too. */
15054 BFD_ASSERT (offset == rel[1].r_offset);
15055 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15056 bfd_put_32 (input_bfd, insn2, contents + offset);
15057 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
15058 goto again;
15059 }
15060 break;
15061
15062 case R_PPC64_TLSLD:
15063 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15064 && rel + 1 < relend)
15065 {
15066 unsigned int insn2;
15067 bfd_vma offset = rel->r_offset;
15068
15069 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
15070 {
15071 bfd_put_32 (output_bfd, NOP, contents + offset);
15072 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15073 break;
15074 }
15075
15076 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15077 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15078
15079 if (toc_symndx)
15080 sec = local_sections[toc_symndx];
15081 for (r_symndx = 0;
15082 r_symndx < symtab_hdr->sh_info;
15083 r_symndx++)
15084 if (local_sections[r_symndx] == sec)
15085 break;
15086 if (r_symndx >= symtab_hdr->sh_info)
15087 r_symndx = STN_UNDEF;
15088 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15089 if (r_symndx != STN_UNDEF)
15090 rel->r_addend -= (local_syms[r_symndx].st_value
15091 + sec->output_offset
15092 + sec->output_section->vma);
15093
15094 r_type = R_PPC64_TPREL16_LO;
15095 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15096 rel->r_offset = offset + d_offset;
15097 /* Zap the reloc on the _tls_get_addr call too. */
15098 BFD_ASSERT (offset == rel[1].r_offset);
15099 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15100 insn2 = 0x38630000; /* addi 3,3,0 */
15101 bfd_put_32 (input_bfd, insn2, contents + offset);
15102 goto again;
15103 }
15104 break;
15105
15106 case R_PPC64_DTPMOD64:
15107 if (rel + 1 < relend
15108 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15109 && rel[1].r_offset == rel->r_offset + 8)
15110 {
15111 if ((tls_mask & TLS_GD) == 0)
15112 {
15113 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15114 if ((tls_mask & TLS_TPRELGD) != 0)
15115 r_type = R_PPC64_TPREL64;
15116 else
15117 {
15118 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15119 r_type = R_PPC64_NONE;
15120 }
15121 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15122 }
15123 }
15124 else
15125 {
15126 if ((tls_mask & TLS_LD) == 0)
15127 {
15128 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15129 r_type = R_PPC64_NONE;
15130 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15131 }
15132 }
15133 break;
15134
15135 case R_PPC64_TPREL64:
15136 if ((tls_mask & TLS_TPREL) == 0)
15137 {
15138 r_type = R_PPC64_NONE;
15139 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15140 }
15141 break;
15142
15143 case R_PPC64_ENTRY:
15144 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15145 if (!bfd_link_pic (info)
15146 && !info->traditional_format
15147 && relocation + 0x80008000 <= 0xffffffff)
15148 {
15149 unsigned int insn1, insn2;
15150
15151 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15152 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15153 if ((insn1 & ~0xfffc) == LD_R2_0R12
15154 && insn2 == ADD_R2_R2_R12)
15155 {
15156 bfd_put_32 (input_bfd,
15157 LIS_R2 + PPC_HA (relocation),
15158 contents + rel->r_offset);
15159 bfd_put_32 (input_bfd,
15160 ADDI_R2_R2 + PPC_LO (relocation),
15161 contents + rel->r_offset + 4);
15162 }
15163 }
15164 else
15165 {
15166 relocation -= (rel->r_offset
15167 + input_section->output_offset
15168 + input_section->output_section->vma);
15169 if (relocation + 0x80008000 <= 0xffffffff)
15170 {
15171 unsigned int insn1, insn2;
15172
15173 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15174 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15175 if ((insn1 & ~0xfffc) == LD_R2_0R12
15176 && insn2 == ADD_R2_R2_R12)
15177 {
15178 bfd_put_32 (input_bfd,
15179 ADDIS_R2_R12 + PPC_HA (relocation),
15180 contents + rel->r_offset);
15181 bfd_put_32 (input_bfd,
15182 ADDI_R2_R2 + PPC_LO (relocation),
15183 contents + rel->r_offset + 4);
15184 }
15185 }
15186 }
15187 break;
15188
15189 case R_PPC64_REL16_HA:
15190 /* If we are generating a non-PIC executable, edit
15191 . 0: addis 2,12,.TOC.-0b@ha
15192 . addi 2,2,.TOC.-0b@l
15193 used by ELFv2 global entry points to set up r2, to
15194 . lis 2,.TOC.@ha
15195 . addi 2,2,.TOC.@l
15196 if .TOC. is in range. */
15197 if (!bfd_link_pic (info)
15198 && !info->traditional_format
15199 && !htab->opd_abi
15200 && rel->r_addend == d_offset
15201 && h != NULL && &h->elf == htab->elf.hgot
15202 && rel + 1 < relend
15203 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15204 && rel[1].r_offset == rel->r_offset + 4
15205 && rel[1].r_addend == rel->r_addend + 4
15206 && relocation + 0x80008000 <= 0xffffffff)
15207 {
15208 unsigned int insn1, insn2;
15209 bfd_vma offset = rel->r_offset - d_offset;
15210 insn1 = bfd_get_32 (input_bfd, contents + offset);
15211 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15212 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15213 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15214 {
15215 r_type = R_PPC64_ADDR16_HA;
15216 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15217 rel->r_addend -= d_offset;
15218 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15219 rel[1].r_addend -= d_offset + 4;
15220 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15221 }
15222 }
15223 break;
15224 }
15225
15226 /* Handle other relocations that tweak non-addend part of insn. */
15227 insn = 0;
15228 max_br_offset = 1 << 25;
15229 addend = rel->r_addend;
15230 reloc_dest = DEST_NORMAL;
15231 switch (r_type)
15232 {
15233 default:
15234 break;
15235
15236 case R_PPC64_TOCSAVE:
15237 if (relocation + addend == (rel->r_offset
15238 + input_section->output_offset
15239 + input_section->output_section->vma)
15240 && tocsave_find (htab, NO_INSERT,
15241 &local_syms, rel, input_bfd))
15242 {
15243 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15244 if (insn == NOP
15245 || insn == CROR_151515 || insn == CROR_313131)
15246 bfd_put_32 (input_bfd,
15247 STD_R2_0R1 + STK_TOC (htab),
15248 contents + rel->r_offset);
15249 }
15250 break;
15251
15252 /* Branch taken prediction relocations. */
15253 case R_PPC64_ADDR14_BRTAKEN:
15254 case R_PPC64_REL14_BRTAKEN:
15255 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15256 /* Fall through. */
15257
15258 /* Branch not taken prediction relocations. */
15259 case R_PPC64_ADDR14_BRNTAKEN:
15260 case R_PPC64_REL14_BRNTAKEN:
15261 insn |= bfd_get_32 (input_bfd,
15262 contents + rel->r_offset) & ~(0x01 << 21);
15263 /* Fall through. */
15264
15265 case R_PPC64_REL14:
15266 max_br_offset = 1 << 15;
15267 /* Fall through. */
15268
15269 case R_PPC64_REL24:
15270 case R_PPC64_REL24_NOTOC:
15271 case R_PPC64_PLTCALL:
15272 /* Calls to functions with a different TOC, such as calls to
15273 shared objects, need to alter the TOC pointer. This is
15274 done using a linkage stub. A REL24 branching to these
15275 linkage stubs needs to be followed by a nop, as the nop
15276 will be replaced with an instruction to restore the TOC
15277 base pointer. */
15278 fdh = h;
15279 if (h != NULL
15280 && h->oh != NULL
15281 && h->oh->is_func_descriptor)
15282 fdh = ppc_follow_link (h->oh);
15283 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15284 htab);
15285 if (r_type == R_PPC64_PLTCALL
15286 && stub_entry != NULL
15287 && stub_entry->stub_type >= ppc_stub_plt_call
15288 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15289 stub_entry = NULL;
15290
15291 if (stub_entry != NULL
15292 && (stub_entry->stub_type == ppc_stub_plt_call
15293 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15294 || stub_entry->stub_type == ppc_stub_plt_call_both
15295 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15296 || stub_entry->stub_type == ppc_stub_plt_branch_both
15297 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15298 || stub_entry->stub_type == ppc_stub_long_branch_both))
15299 {
15300 bfd_boolean can_plt_call = FALSE;
15301
15302 if (stub_entry->stub_type == ppc_stub_plt_call
15303 && !htab->opd_abi
15304 && htab->params->plt_localentry0 != 0
15305 && is_elfv2_localentry0 (&h->elf))
15306 {
15307 /* The function doesn't use or change r2. */
15308 can_plt_call = TRUE;
15309 }
15310 else if (r_type == R_PPC64_REL24_NOTOC)
15311 {
15312 /* NOTOC calls don't need to restore r2. */
15313 can_plt_call = TRUE;
15314 }
15315
15316 /* All of these stubs may modify r2, so there must be a
15317 branch and link followed by a nop. The nop is
15318 replaced by an insn to restore r2. */
15319 else if (rel->r_offset + 8 <= input_section->size)
15320 {
15321 unsigned long br;
15322
15323 br = bfd_get_32 (input_bfd,
15324 contents + rel->r_offset);
15325 if ((br & 1) != 0)
15326 {
15327 unsigned long nop;
15328
15329 nop = bfd_get_32 (input_bfd,
15330 contents + rel->r_offset + 4);
15331 if (nop == LD_R2_0R1 + STK_TOC (htab))
15332 can_plt_call = TRUE;
15333 else if (nop == NOP
15334 || nop == CROR_151515
15335 || nop == CROR_313131)
15336 {
15337 if (h != NULL
15338 && (h == htab->tls_get_addr_fd
15339 || h == htab->tls_get_addr)
15340 && htab->params->tls_get_addr_opt)
15341 {
15342 /* Special stub used, leave nop alone. */
15343 }
15344 else
15345 bfd_put_32 (input_bfd,
15346 LD_R2_0R1 + STK_TOC (htab),
15347 contents + rel->r_offset + 4);
15348 can_plt_call = TRUE;
15349 }
15350 }
15351 }
15352
15353 if (!can_plt_call && h != NULL)
15354 {
15355 const char *name = h->elf.root.root.string;
15356
15357 if (*name == '.')
15358 ++name;
15359
15360 if (strncmp (name, "__libc_start_main", 17) == 0
15361 && (name[17] == 0 || name[17] == '@'))
15362 {
15363 /* Allow crt1 branch to go via a toc adjusting
15364 stub. Other calls that never return could do
15365 the same, if we could detect such. */
15366 can_plt_call = TRUE;
15367 }
15368 }
15369
15370 if (!can_plt_call)
15371 {
15372 /* g++ as of 20130507 emits self-calls without a
15373 following nop. This is arguably wrong since we
15374 have conflicting information. On the one hand a
15375 global symbol and on the other a local call
15376 sequence, but don't error for this special case.
15377 It isn't possible to cheaply verify we have
15378 exactly such a call. Allow all calls to the same
15379 section. */
15380 asection *code_sec = sec;
15381
15382 if (get_opd_info (sec) != NULL)
15383 {
15384 bfd_vma off = (relocation + addend
15385 - sec->output_section->vma
15386 - sec->output_offset);
15387
15388 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15389 }
15390 if (code_sec == input_section)
15391 can_plt_call = TRUE;
15392 }
15393
15394 if (!can_plt_call)
15395 {
15396 if (stub_entry->stub_type >= ppc_stub_plt_call
15397 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15398 info->callbacks->einfo
15399 /* xgettext:c-format */
15400 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15401 "recompile with -fPIC\n"),
15402 input_bfd, input_section, rel->r_offset, sym_name);
15403 else
15404 info->callbacks->einfo
15405 /* xgettext:c-format */
15406 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15407 "(-mcmodel=small toc adjust stub)\n"),
15408 input_bfd, input_section, rel->r_offset, sym_name);
15409
15410 bfd_set_error (bfd_error_bad_value);
15411 ret = FALSE;
15412 }
15413
15414 if (can_plt_call
15415 && stub_entry->stub_type >= ppc_stub_plt_call
15416 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15417 unresolved_reloc = FALSE;
15418 }
15419
15420 if ((stub_entry == NULL
15421 || stub_entry->stub_type == ppc_stub_long_branch
15422 || stub_entry->stub_type == ppc_stub_plt_branch)
15423 && get_opd_info (sec) != NULL)
15424 {
15425 /* The branch destination is the value of the opd entry. */
15426 bfd_vma off = (relocation + addend
15427 - sec->output_section->vma
15428 - sec->output_offset);
15429 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15430 if (dest != (bfd_vma) -1)
15431 {
15432 relocation = dest;
15433 addend = 0;
15434 reloc_dest = DEST_OPD;
15435 }
15436 }
15437
15438 /* If the branch is out of reach we ought to have a long
15439 branch stub. */
15440 from = (rel->r_offset
15441 + input_section->output_offset
15442 + input_section->output_section->vma);
15443
15444 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15445 ? fdh->elf.other
15446 : sym->st_other);
15447
15448 if (stub_entry != NULL
15449 && (stub_entry->stub_type == ppc_stub_long_branch
15450 || stub_entry->stub_type == ppc_stub_plt_branch)
15451 && (r_type == R_PPC64_ADDR14_BRTAKEN
15452 || r_type == R_PPC64_ADDR14_BRNTAKEN
15453 || (relocation + addend - from + max_br_offset
15454 < 2 * max_br_offset)))
15455 /* Don't use the stub if this branch is in range. */
15456 stub_entry = NULL;
15457
15458 if (stub_entry != NULL
15459 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15460 || stub_entry->stub_type == ppc_stub_long_branch_both
15461 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15462 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15463 && (r_type != R_PPC64_REL24_NOTOC
15464 || ((fdh ? fdh->elf.other : sym->st_other)
15465 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
15466 && (relocation + addend - from + max_br_offset
15467 < 2 * max_br_offset))
15468 stub_entry = NULL;
15469
15470 if (stub_entry != NULL
15471 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15472 || stub_entry->stub_type == ppc_stub_long_branch_both
15473 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15474 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15475 && r_type == R_PPC64_REL24_NOTOC
15476 && (relocation + addend - from + max_br_offset
15477 < 2 * max_br_offset))
15478 stub_entry = NULL;
15479
15480 if (stub_entry != NULL)
15481 {
15482 /* Munge up the value and addend so that we call the stub
15483 rather than the procedure directly. */
15484 asection *stub_sec = stub_entry->group->stub_sec;
15485
15486 if (stub_entry->stub_type == ppc_stub_save_res)
15487 relocation += (stub_sec->output_offset
15488 + stub_sec->output_section->vma
15489 + stub_sec->size - htab->sfpr->size
15490 - htab->sfpr->output_offset
15491 - htab->sfpr->output_section->vma);
15492 else
15493 relocation = (stub_entry->stub_offset
15494 + stub_sec->output_offset
15495 + stub_sec->output_section->vma);
15496 addend = 0;
15497 reloc_dest = DEST_STUB;
15498
15499 if (((stub_entry->stub_type == ppc_stub_plt_call
15500 && ALWAYS_EMIT_R2SAVE)
15501 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15502 || stub_entry->stub_type == ppc_stub_plt_call_both)
15503 && !(h != NULL
15504 && (h == htab->tls_get_addr_fd
15505 || h == htab->tls_get_addr)
15506 && htab->params->tls_get_addr_opt)
15507 && rel + 1 < relend
15508 && rel[1].r_offset == rel->r_offset + 4
15509 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15510 relocation += 4;
15511 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15512 || stub_entry->stub_type == ppc_stub_plt_branch_both
15513 || stub_entry->stub_type == ppc_stub_plt_call_both)
15514 && r_type == R_PPC64_REL24_NOTOC)
15515 relocation += 4;
15516 }
15517
15518 if (insn != 0)
15519 {
15520 if (is_isa_v2)
15521 {
15522 /* Set 'a' bit. This is 0b00010 in BO field for branch
15523 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15524 for branch on CTR insns (BO == 1a00t or 1a01t). */
15525 if ((insn & (0x14 << 21)) == (0x04 << 21))
15526 insn |= 0x02 << 21;
15527 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15528 insn |= 0x08 << 21;
15529 else
15530 break;
15531 }
15532 else
15533 {
15534 /* Invert 'y' bit if not the default. */
15535 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15536 insn ^= 0x01 << 21;
15537 }
15538
15539 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15540 }
15541
15542 /* NOP out calls to undefined weak functions.
15543 We can thus call a weak function without first
15544 checking whether the function is defined. */
15545 else if (h != NULL
15546 && h->elf.root.type == bfd_link_hash_undefweak
15547 && h->elf.dynindx == -1
15548 && (r_type == R_PPC64_REL24
15549 || r_type == R_PPC64_REL24_NOTOC)
15550 && relocation == 0
15551 && addend == 0)
15552 {
15553 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15554 goto copy_reloc;
15555 }
15556 break;
15557 }
15558
15559 /* Set `addend'. */
15560 tls_type = 0;
15561 save_unresolved_reloc = unresolved_reloc;
15562 switch (r_type)
15563 {
15564 default:
15565 /* xgettext:c-format */
15566 _bfd_error_handler (_("%pB: %s unsupported"),
15567 input_bfd, ppc64_elf_howto_table[r_type]->name);
15568
15569 bfd_set_error (bfd_error_bad_value);
15570 ret = FALSE;
15571 goto copy_reloc;
15572
15573 case R_PPC64_NONE:
15574 case R_PPC64_TLS:
15575 case R_PPC64_TLSGD:
15576 case R_PPC64_TLSLD:
15577 case R_PPC64_TOCSAVE:
15578 case R_PPC64_GNU_VTINHERIT:
15579 case R_PPC64_GNU_VTENTRY:
15580 case R_PPC64_ENTRY:
15581 goto copy_reloc;
15582
15583 /* GOT16 relocations. Like an ADDR16 using the symbol's
15584 address in the GOT as relocation value instead of the
15585 symbol's value itself. Also, create a GOT entry for the
15586 symbol and put the symbol value there. */
15587 case R_PPC64_GOT_TLSGD16:
15588 case R_PPC64_GOT_TLSGD16_LO:
15589 case R_PPC64_GOT_TLSGD16_HI:
15590 case R_PPC64_GOT_TLSGD16_HA:
15591 tls_type = TLS_TLS | TLS_GD;
15592 goto dogot;
15593
15594 case R_PPC64_GOT_TLSLD16:
15595 case R_PPC64_GOT_TLSLD16_LO:
15596 case R_PPC64_GOT_TLSLD16_HI:
15597 case R_PPC64_GOT_TLSLD16_HA:
15598 tls_type = TLS_TLS | TLS_LD;
15599 goto dogot;
15600
15601 case R_PPC64_GOT_TPREL16_DS:
15602 case R_PPC64_GOT_TPREL16_LO_DS:
15603 case R_PPC64_GOT_TPREL16_HI:
15604 case R_PPC64_GOT_TPREL16_HA:
15605 tls_type = TLS_TLS | TLS_TPREL;
15606 goto dogot;
15607
15608 case R_PPC64_GOT_DTPREL16_DS:
15609 case R_PPC64_GOT_DTPREL16_LO_DS:
15610 case R_PPC64_GOT_DTPREL16_HI:
15611 case R_PPC64_GOT_DTPREL16_HA:
15612 tls_type = TLS_TLS | TLS_DTPREL;
15613 goto dogot;
15614
15615 case R_PPC64_GOT16:
15616 case R_PPC64_GOT16_LO:
15617 case R_PPC64_GOT16_HI:
15618 case R_PPC64_GOT16_HA:
15619 case R_PPC64_GOT16_DS:
15620 case R_PPC64_GOT16_LO_DS:
15621 dogot:
15622 {
15623 /* Relocation is to the entry for this symbol in the global
15624 offset table. */
15625 asection *got;
15626 bfd_vma *offp;
15627 bfd_vma off;
15628 unsigned long indx = 0;
15629 struct got_entry *ent;
15630
15631 if (tls_type == (TLS_TLS | TLS_LD)
15632 && (h == NULL
15633 || !h->elf.def_dynamic))
15634 ent = ppc64_tlsld_got (input_bfd);
15635 else
15636 {
15637 if (h != NULL)
15638 {
15639 if (!htab->elf.dynamic_sections_created
15640 || h->elf.dynindx == -1
15641 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15642 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15643 /* This is actually a static link, or it is a
15644 -Bsymbolic link and the symbol is defined
15645 locally, or the symbol was forced to be local
15646 because of a version file. */
15647 ;
15648 else
15649 {
15650 indx = h->elf.dynindx;
15651 unresolved_reloc = FALSE;
15652 }
15653 ent = h->elf.got.glist;
15654 }
15655 else
15656 {
15657 if (local_got_ents == NULL)
15658 abort ();
15659 ent = local_got_ents[r_symndx];
15660 }
15661
15662 for (; ent != NULL; ent = ent->next)
15663 if (ent->addend == orig_rel.r_addend
15664 && ent->owner == input_bfd
15665 && ent->tls_type == tls_type)
15666 break;
15667 }
15668
15669 if (ent == NULL)
15670 abort ();
15671 if (ent->is_indirect)
15672 ent = ent->got.ent;
15673 offp = &ent->got.offset;
15674 got = ppc64_elf_tdata (ent->owner)->got;
15675 if (got == NULL)
15676 abort ();
15677
15678 /* The offset must always be a multiple of 8. We use the
15679 least significant bit to record whether we have already
15680 processed this entry. */
15681 off = *offp;
15682 if ((off & 1) != 0)
15683 off &= ~1;
15684 else
15685 {
15686 /* Generate relocs for the dynamic linker, except in
15687 the case of TLSLD where we'll use one entry per
15688 module. */
15689 asection *relgot;
15690 bfd_boolean ifunc;
15691
15692 *offp = off | 1;
15693 relgot = NULL;
15694 ifunc = (h != NULL
15695 ? h->elf.type == STT_GNU_IFUNC
15696 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15697 if (ifunc)
15698 {
15699 relgot = htab->elf.irelplt;
15700 if (indx == 0)
15701 htab->local_ifunc_resolver = 1;
15702 else if (is_static_defined (&h->elf))
15703 htab->maybe_local_ifunc_resolver = 1;
15704 }
15705 else if (indx != 0
15706 || (bfd_link_pic (info)
15707 && (h == NULL
15708 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15709 || (tls_type == (TLS_TLS | TLS_LD)
15710 && !h->elf.def_dynamic))
15711 && !(tls_type == (TLS_TLS | TLS_TPREL)
15712 && bfd_link_executable (info)
15713 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15714 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15715 if (relgot != NULL)
15716 {
15717 outrel.r_offset = (got->output_section->vma
15718 + got->output_offset
15719 + off);
15720 outrel.r_addend = addend;
15721 if (tls_type & (TLS_LD | TLS_GD))
15722 {
15723 outrel.r_addend = 0;
15724 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15725 if (tls_type == (TLS_TLS | TLS_GD))
15726 {
15727 loc = relgot->contents;
15728 loc += (relgot->reloc_count++
15729 * sizeof (Elf64_External_Rela));
15730 bfd_elf64_swap_reloca_out (output_bfd,
15731 &outrel, loc);
15732 outrel.r_offset += 8;
15733 outrel.r_addend = addend;
15734 outrel.r_info
15735 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15736 }
15737 }
15738 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15739 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15740 else if (tls_type == (TLS_TLS | TLS_TPREL))
15741 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15742 else if (indx != 0)
15743 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15744 else
15745 {
15746 if (ifunc)
15747 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15748 else
15749 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15750
15751 /* Write the .got section contents for the sake
15752 of prelink. */
15753 loc = got->contents + off;
15754 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15755 loc);
15756 }
15757
15758 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15759 {
15760 outrel.r_addend += relocation;
15761 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15762 {
15763 if (htab->elf.tls_sec == NULL)
15764 outrel.r_addend = 0;
15765 else
15766 outrel.r_addend -= htab->elf.tls_sec->vma;
15767 }
15768 }
15769 loc = relgot->contents;
15770 loc += (relgot->reloc_count++
15771 * sizeof (Elf64_External_Rela));
15772 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15773 }
15774
15775 /* Init the .got section contents here if we're not
15776 emitting a reloc. */
15777 else
15778 {
15779 relocation += addend;
15780 if (tls_type != 0)
15781 {
15782 if (htab->elf.tls_sec == NULL)
15783 relocation = 0;
15784 else
15785 {
15786 if (tls_type & TLS_LD)
15787 relocation = 0;
15788 else
15789 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15790 if (tls_type & TLS_TPREL)
15791 relocation += DTP_OFFSET - TP_OFFSET;
15792 }
15793
15794 if (tls_type & (TLS_GD | TLS_LD))
15795 {
15796 bfd_put_64 (output_bfd, relocation,
15797 got->contents + off + 8);
15798 relocation = 1;
15799 }
15800 }
15801 bfd_put_64 (output_bfd, relocation,
15802 got->contents + off);
15803 }
15804 }
15805
15806 if (off >= (bfd_vma) -2)
15807 abort ();
15808
15809 relocation = got->output_section->vma + got->output_offset + off;
15810 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15811 }
15812 break;
15813
15814 case R_PPC64_PLT16_HA:
15815 case R_PPC64_PLT16_HI:
15816 case R_PPC64_PLT16_LO:
15817 case R_PPC64_PLT16_LO_DS:
15818 case R_PPC64_PLT32:
15819 case R_PPC64_PLT64:
15820 case R_PPC64_PLTSEQ:
15821 case R_PPC64_PLTCALL:
15822 /* Relocation is to the entry for this symbol in the
15823 procedure linkage table. */
15824 unresolved_reloc = TRUE;
15825 {
15826 struct plt_entry **plt_list = NULL;
15827 if (h != NULL)
15828 plt_list = &h->elf.plt.plist;
15829 else if (local_got_ents != NULL)
15830 {
15831 struct plt_entry **local_plt = (struct plt_entry **)
15832 (local_got_ents + symtab_hdr->sh_info);
15833 plt_list = local_plt + r_symndx;
15834 }
15835 if (plt_list)
15836 {
15837 struct plt_entry *ent;
15838
15839 for (ent = *plt_list; ent != NULL; ent = ent->next)
15840 if (ent->plt.offset != (bfd_vma) -1
15841 && ent->addend == orig_rel.r_addend)
15842 {
15843 asection *plt;
15844 bfd_vma got;
15845
15846 plt = htab->elf.splt;
15847 if (!htab->elf.dynamic_sections_created
15848 || h == NULL
15849 || h->elf.dynindx == -1)
15850 {
15851 if (h != NULL
15852 ? h->elf.type == STT_GNU_IFUNC
15853 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15854 plt = htab->elf.iplt;
15855 else
15856 plt = htab->pltlocal;
15857 }
15858 relocation = (plt->output_section->vma
15859 + plt->output_offset
15860 + ent->plt.offset);
15861 if (r_type == R_PPC64_PLT16_HA
15862 || r_type == R_PPC64_PLT16_HI
15863 || r_type == R_PPC64_PLT16_LO
15864 || r_type == R_PPC64_PLT16_LO_DS)
15865 {
15866 got = (elf_gp (output_bfd)
15867 + htab->sec_info[input_section->id].toc_off);
15868 relocation -= got;
15869 }
15870 addend = 0;
15871 unresolved_reloc = FALSE;
15872 break;
15873 }
15874 }
15875 }
15876 break;
15877
15878 case R_PPC64_TOC:
15879 /* Relocation value is TOC base. */
15880 relocation = TOCstart;
15881 if (r_symndx == STN_UNDEF)
15882 relocation += htab->sec_info[input_section->id].toc_off;
15883 else if (unresolved_reloc)
15884 ;
15885 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15886 relocation += htab->sec_info[sec->id].toc_off;
15887 else
15888 unresolved_reloc = TRUE;
15889 goto dodyn;
15890
15891 /* TOC16 relocs. We want the offset relative to the TOC base,
15892 which is the address of the start of the TOC plus 0x8000.
15893 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15894 in this order. */
15895 case R_PPC64_TOC16:
15896 case R_PPC64_TOC16_LO:
15897 case R_PPC64_TOC16_HI:
15898 case R_PPC64_TOC16_DS:
15899 case R_PPC64_TOC16_LO_DS:
15900 case R_PPC64_TOC16_HA:
15901 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15902 break;
15903
15904 /* Relocate against the beginning of the section. */
15905 case R_PPC64_SECTOFF:
15906 case R_PPC64_SECTOFF_LO:
15907 case R_PPC64_SECTOFF_HI:
15908 case R_PPC64_SECTOFF_DS:
15909 case R_PPC64_SECTOFF_LO_DS:
15910 case R_PPC64_SECTOFF_HA:
15911 if (sec != NULL)
15912 addend -= sec->output_section->vma;
15913 break;
15914
15915 case R_PPC64_REL16:
15916 case R_PPC64_REL16_LO:
15917 case R_PPC64_REL16_HI:
15918 case R_PPC64_REL16_HA:
15919 case R_PPC64_REL16DX_HA:
15920 break;
15921
15922 case R_PPC64_REL14:
15923 case R_PPC64_REL14_BRNTAKEN:
15924 case R_PPC64_REL14_BRTAKEN:
15925 case R_PPC64_REL24:
15926 case R_PPC64_REL24_NOTOC:
15927 break;
15928
15929 case R_PPC64_TPREL16:
15930 case R_PPC64_TPREL16_LO:
15931 case R_PPC64_TPREL16_HI:
15932 case R_PPC64_TPREL16_HA:
15933 case R_PPC64_TPREL16_DS:
15934 case R_PPC64_TPREL16_LO_DS:
15935 case R_PPC64_TPREL16_HIGH:
15936 case R_PPC64_TPREL16_HIGHA:
15937 case R_PPC64_TPREL16_HIGHER:
15938 case R_PPC64_TPREL16_HIGHERA:
15939 case R_PPC64_TPREL16_HIGHEST:
15940 case R_PPC64_TPREL16_HIGHESTA:
15941 if (h != NULL
15942 && h->elf.root.type == bfd_link_hash_undefweak
15943 && h->elf.dynindx == -1)
15944 {
15945 /* Make this relocation against an undefined weak symbol
15946 resolve to zero. This is really just a tweak, since
15947 code using weak externs ought to check that they are
15948 defined before using them. */
15949 bfd_byte *p = contents + rel->r_offset - d_offset;
15950
15951 insn = bfd_get_32 (input_bfd, p);
15952 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15953 if (insn != 0)
15954 bfd_put_32 (input_bfd, insn, p);
15955 break;
15956 }
15957 if (htab->elf.tls_sec != NULL)
15958 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15959 /* The TPREL16 relocs shouldn't really be used in shared
15960 libs or with non-local symbols as that will result in
15961 DT_TEXTREL being set, but support them anyway. */
15962 goto dodyn;
15963
15964 case R_PPC64_DTPREL16:
15965 case R_PPC64_DTPREL16_LO:
15966 case R_PPC64_DTPREL16_HI:
15967 case R_PPC64_DTPREL16_HA:
15968 case R_PPC64_DTPREL16_DS:
15969 case R_PPC64_DTPREL16_LO_DS:
15970 case R_PPC64_DTPREL16_HIGH:
15971 case R_PPC64_DTPREL16_HIGHA:
15972 case R_PPC64_DTPREL16_HIGHER:
15973 case R_PPC64_DTPREL16_HIGHERA:
15974 case R_PPC64_DTPREL16_HIGHEST:
15975 case R_PPC64_DTPREL16_HIGHESTA:
15976 if (htab->elf.tls_sec != NULL)
15977 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15978 break;
15979
15980 case R_PPC64_ADDR64_LOCAL:
15981 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15982 ? h->elf.other
15983 : sym->st_other);
15984 break;
15985
15986 case R_PPC64_DTPMOD64:
15987 relocation = 1;
15988 addend = 0;
15989 goto dodyn;
15990
15991 case R_PPC64_TPREL64:
15992 if (htab->elf.tls_sec != NULL)
15993 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15994 goto dodyn;
15995
15996 case R_PPC64_DTPREL64:
15997 if (htab->elf.tls_sec != NULL)
15998 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15999 /* Fall through. */
16000
16001 /* Relocations that may need to be propagated if this is a
16002 dynamic object. */
16003 case R_PPC64_REL30:
16004 case R_PPC64_REL32:
16005 case R_PPC64_REL64:
16006 case R_PPC64_ADDR14:
16007 case R_PPC64_ADDR14_BRNTAKEN:
16008 case R_PPC64_ADDR14_BRTAKEN:
16009 case R_PPC64_ADDR16:
16010 case R_PPC64_ADDR16_DS:
16011 case R_PPC64_ADDR16_HA:
16012 case R_PPC64_ADDR16_HI:
16013 case R_PPC64_ADDR16_HIGH:
16014 case R_PPC64_ADDR16_HIGHA:
16015 case R_PPC64_ADDR16_HIGHER:
16016 case R_PPC64_ADDR16_HIGHERA:
16017 case R_PPC64_ADDR16_HIGHEST:
16018 case R_PPC64_ADDR16_HIGHESTA:
16019 case R_PPC64_ADDR16_LO:
16020 case R_PPC64_ADDR16_LO_DS:
16021 case R_PPC64_ADDR24:
16022 case R_PPC64_ADDR32:
16023 case R_PPC64_ADDR64:
16024 case R_PPC64_UADDR16:
16025 case R_PPC64_UADDR32:
16026 case R_PPC64_UADDR64:
16027 dodyn:
16028 if ((input_section->flags & SEC_ALLOC) == 0)
16029 break;
16030
16031 if (NO_OPD_RELOCS && is_opd)
16032 break;
16033
16034 if (bfd_link_pic (info)
16035 ? ((h == NULL
16036 || h->dyn_relocs != NULL)
16037 && ((h != NULL && pc_dynrelocs (h))
16038 || must_be_dyn_reloc (info, r_type)))
16039 : (h != NULL
16040 ? h->dyn_relocs != NULL
16041 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16042 {
16043 bfd_boolean skip, relocate;
16044 asection *sreloc;
16045 bfd_vma out_off;
16046 long indx = 0;
16047
16048 /* When generating a dynamic object, these relocations
16049 are copied into the output file to be resolved at run
16050 time. */
16051
16052 skip = FALSE;
16053 relocate = FALSE;
16054
16055 out_off = _bfd_elf_section_offset (output_bfd, info,
16056 input_section, rel->r_offset);
16057 if (out_off == (bfd_vma) -1)
16058 skip = TRUE;
16059 else if (out_off == (bfd_vma) -2)
16060 skip = TRUE, relocate = TRUE;
16061 out_off += (input_section->output_section->vma
16062 + input_section->output_offset);
16063 outrel.r_offset = out_off;
16064 outrel.r_addend = rel->r_addend;
16065
16066 /* Optimize unaligned reloc use. */
16067 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16068 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16069 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16070 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16071 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16072 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16073 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16074 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16075 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16076
16077 if (skip)
16078 memset (&outrel, 0, sizeof outrel);
16079 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16080 && !is_opd
16081 && r_type != R_PPC64_TOC)
16082 {
16083 indx = h->elf.dynindx;
16084 BFD_ASSERT (indx != -1);
16085 outrel.r_info = ELF64_R_INFO (indx, r_type);
16086 }
16087 else
16088 {
16089 /* This symbol is local, or marked to become local,
16090 or this is an opd section reloc which must point
16091 at a local function. */
16092 outrel.r_addend += relocation;
16093 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16094 {
16095 if (is_opd && h != NULL)
16096 {
16097 /* Lie about opd entries. This case occurs
16098 when building shared libraries and we
16099 reference a function in another shared
16100 lib. The same thing happens for a weak
16101 definition in an application that's
16102 overridden by a strong definition in a
16103 shared lib. (I believe this is a generic
16104 bug in binutils handling of weak syms.)
16105 In these cases we won't use the opd
16106 entry in this lib. */
16107 unresolved_reloc = FALSE;
16108 }
16109 if (!is_opd
16110 && r_type == R_PPC64_ADDR64
16111 && (h != NULL
16112 ? h->elf.type == STT_GNU_IFUNC
16113 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16114 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16115 else
16116 {
16117 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16118
16119 /* We need to relocate .opd contents for ld.so.
16120 Prelink also wants simple and consistent rules
16121 for relocs. This make all RELATIVE relocs have
16122 *r_offset equal to r_addend. */
16123 relocate = TRUE;
16124 }
16125 }
16126 else
16127 {
16128 if (h != NULL
16129 ? h->elf.type == STT_GNU_IFUNC
16130 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16131 {
16132 info->callbacks->einfo
16133 /* xgettext:c-format */
16134 (_("%H: %s for indirect "
16135 "function `%pT' unsupported\n"),
16136 input_bfd, input_section, rel->r_offset,
16137 ppc64_elf_howto_table[r_type]->name,
16138 sym_name);
16139 ret = FALSE;
16140 }
16141 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16142 ;
16143 else if (sec == NULL || sec->owner == NULL)
16144 {
16145 bfd_set_error (bfd_error_bad_value);
16146 return FALSE;
16147 }
16148 else
16149 {
16150 asection *osec;
16151
16152 osec = sec->output_section;
16153 indx = elf_section_data (osec)->dynindx;
16154
16155 if (indx == 0)
16156 {
16157 if ((osec->flags & SEC_READONLY) == 0
16158 && htab->elf.data_index_section != NULL)
16159 osec = htab->elf.data_index_section;
16160 else
16161 osec = htab->elf.text_index_section;
16162 indx = elf_section_data (osec)->dynindx;
16163 }
16164 BFD_ASSERT (indx != 0);
16165
16166 /* We are turning this relocation into one
16167 against a section symbol, so subtract out
16168 the output section's address but not the
16169 offset of the input section in the output
16170 section. */
16171 outrel.r_addend -= osec->vma;
16172 }
16173
16174 outrel.r_info = ELF64_R_INFO (indx, r_type);
16175 }
16176 }
16177
16178 sreloc = elf_section_data (input_section)->sreloc;
16179 if (h != NULL
16180 ? h->elf.type == STT_GNU_IFUNC
16181 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16182 {
16183 sreloc = htab->elf.irelplt;
16184 if (indx == 0)
16185 htab->local_ifunc_resolver = 1;
16186 else if (is_static_defined (&h->elf))
16187 htab->maybe_local_ifunc_resolver = 1;
16188 }
16189 if (sreloc == NULL)
16190 abort ();
16191
16192 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16193 >= sreloc->size)
16194 abort ();
16195 loc = sreloc->contents;
16196 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16197 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16198
16199 /* If this reloc is against an external symbol, it will
16200 be computed at runtime, so there's no need to do
16201 anything now. However, for the sake of prelink ensure
16202 that the section contents are a known value. */
16203 if (!relocate)
16204 {
16205 unresolved_reloc = FALSE;
16206 /* The value chosen here is quite arbitrary as ld.so
16207 ignores section contents except for the special
16208 case of .opd where the contents might be accessed
16209 before relocation. Choose zero, as that won't
16210 cause reloc overflow. */
16211 relocation = 0;
16212 addend = 0;
16213 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16214 to improve backward compatibility with older
16215 versions of ld. */
16216 if (r_type == R_PPC64_ADDR64)
16217 addend = outrel.r_addend;
16218 /* Adjust pc_relative relocs to have zero in *r_offset. */
16219 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16220 addend = outrel.r_offset;
16221 }
16222 }
16223 break;
16224
16225 case R_PPC64_COPY:
16226 case R_PPC64_GLOB_DAT:
16227 case R_PPC64_JMP_SLOT:
16228 case R_PPC64_JMP_IREL:
16229 case R_PPC64_RELATIVE:
16230 /* We shouldn't ever see these dynamic relocs in relocatable
16231 files. */
16232 /* Fall through. */
16233
16234 case R_PPC64_PLTGOT16:
16235 case R_PPC64_PLTGOT16_DS:
16236 case R_PPC64_PLTGOT16_HA:
16237 case R_PPC64_PLTGOT16_HI:
16238 case R_PPC64_PLTGOT16_LO:
16239 case R_PPC64_PLTGOT16_LO_DS:
16240 case R_PPC64_PLTREL32:
16241 case R_PPC64_PLTREL64:
16242 /* These ones haven't been implemented yet. */
16243
16244 info->callbacks->einfo
16245 /* xgettext:c-format */
16246 (_("%P: %pB: %s is not supported for `%pT'\n"),
16247 input_bfd,
16248 ppc64_elf_howto_table[r_type]->name, sym_name);
16249
16250 bfd_set_error (bfd_error_invalid_operation);
16251 ret = FALSE;
16252 goto copy_reloc;
16253 }
16254
16255 /* Multi-instruction sequences that access the TOC can be
16256 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16257 to nop; addi rb,r2,x; */
16258 switch (r_type)
16259 {
16260 default:
16261 break;
16262
16263 case R_PPC64_GOT_TLSLD16_HI:
16264 case R_PPC64_GOT_TLSGD16_HI:
16265 case R_PPC64_GOT_TPREL16_HI:
16266 case R_PPC64_GOT_DTPREL16_HI:
16267 case R_PPC64_GOT16_HI:
16268 case R_PPC64_TOC16_HI:
16269 /* These relocs would only be useful if building up an
16270 offset to later add to r2, perhaps in an indexed
16271 addressing mode instruction. Don't try to optimize.
16272 Unfortunately, the possibility of someone building up an
16273 offset like this or even with the HA relocs, means that
16274 we need to check the high insn when optimizing the low
16275 insn. */
16276 break;
16277
16278 case R_PPC64_PLTCALL:
16279 if (unresolved_reloc)
16280 {
16281 /* No plt entry. Make this into a direct call. */
16282 bfd_byte *p = contents + rel->r_offset;
16283 insn = bfd_get_32 (input_bfd, p);
16284 insn &= 1;
16285 bfd_put_32 (input_bfd, B_DOT | insn, p);
16286 bfd_put_32 (input_bfd, NOP, p + 4);
16287 unresolved_reloc = save_unresolved_reloc;
16288 r_type = R_PPC64_REL24;
16289 }
16290 break;
16291
16292 case R_PPC64_PLTSEQ:
16293 if (unresolved_reloc)
16294 {
16295 unresolved_reloc = FALSE;
16296 goto nop_it;
16297 }
16298 break;
16299
16300 case R_PPC64_PLT16_HA:
16301 if (unresolved_reloc)
16302 {
16303 unresolved_reloc = FALSE;
16304 goto nop_it;
16305 }
16306 /* Fall through. */
16307 case R_PPC64_GOT_TLSLD16_HA:
16308 case R_PPC64_GOT_TLSGD16_HA:
16309 case R_PPC64_GOT_TPREL16_HA:
16310 case R_PPC64_GOT_DTPREL16_HA:
16311 case R_PPC64_GOT16_HA:
16312 case R_PPC64_TOC16_HA:
16313 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16314 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16315 {
16316 bfd_byte *p;
16317 nop_it:
16318 p = contents + (rel->r_offset & ~3);
16319 bfd_put_32 (input_bfd, NOP, p);
16320 goto copy_reloc;
16321 }
16322 break;
16323
16324 case R_PPC64_PLT16_LO:
16325 case R_PPC64_PLT16_LO_DS:
16326 if (unresolved_reloc)
16327 {
16328 unresolved_reloc = FALSE;
16329 goto nop_it;
16330 }
16331 /* Fall through. */
16332 case R_PPC64_GOT_TLSLD16_LO:
16333 case R_PPC64_GOT_TLSGD16_LO:
16334 case R_PPC64_GOT_TPREL16_LO_DS:
16335 case R_PPC64_GOT_DTPREL16_LO_DS:
16336 case R_PPC64_GOT16_LO:
16337 case R_PPC64_GOT16_LO_DS:
16338 case R_PPC64_TOC16_LO:
16339 case R_PPC64_TOC16_LO_DS:
16340 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16341 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16342 {
16343 bfd_byte *p = contents + (rel->r_offset & ~3);
16344 insn = bfd_get_32 (input_bfd, p);
16345 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16346 {
16347 /* Transform addic to addi when we change reg. */
16348 insn &= ~((0x3f << 26) | (0x1f << 16));
16349 insn |= (14u << 26) | (2 << 16);
16350 }
16351 else
16352 {
16353 insn &= ~(0x1f << 16);
16354 insn |= 2 << 16;
16355 }
16356 bfd_put_32 (input_bfd, insn, p);
16357 }
16358 break;
16359
16360 case R_PPC64_TPREL16_HA:
16361 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16362 {
16363 bfd_byte *p = contents + (rel->r_offset & ~3);
16364 insn = bfd_get_32 (input_bfd, p);
16365 if ((insn & ((0x3f << 26) | 0x1f << 16))
16366 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16367 /* xgettext:c-format */
16368 info->callbacks->minfo
16369 (_("%H: warning: %s unexpected insn %#x.\n"),
16370 input_bfd, input_section, rel->r_offset,
16371 ppc64_elf_howto_table[r_type]->name, insn);
16372 else
16373 {
16374 bfd_put_32 (input_bfd, NOP, p);
16375 goto copy_reloc;
16376 }
16377 }
16378 break;
16379
16380 case R_PPC64_TPREL16_LO:
16381 case R_PPC64_TPREL16_LO_DS:
16382 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16383 {
16384 bfd_byte *p = contents + (rel->r_offset & ~3);
16385 insn = bfd_get_32 (input_bfd, p);
16386 insn &= ~(0x1f << 16);
16387 insn |= 13 << 16;
16388 bfd_put_32 (input_bfd, insn, p);
16389 }
16390 break;
16391 }
16392
16393 /* Do any further special processing. */
16394 switch (r_type)
16395 {
16396 default:
16397 break;
16398
16399 case R_PPC64_REL16_HA:
16400 case R_PPC64_REL16DX_HA:
16401 case R_PPC64_ADDR16_HA:
16402 case R_PPC64_ADDR16_HIGHA:
16403 case R_PPC64_ADDR16_HIGHERA:
16404 case R_PPC64_ADDR16_HIGHESTA:
16405 case R_PPC64_TOC16_HA:
16406 case R_PPC64_SECTOFF_HA:
16407 case R_PPC64_TPREL16_HA:
16408 case R_PPC64_TPREL16_HIGHA:
16409 case R_PPC64_TPREL16_HIGHERA:
16410 case R_PPC64_TPREL16_HIGHESTA:
16411 case R_PPC64_DTPREL16_HA:
16412 case R_PPC64_DTPREL16_HIGHA:
16413 case R_PPC64_DTPREL16_HIGHERA:
16414 case R_PPC64_DTPREL16_HIGHESTA:
16415 /* It's just possible that this symbol is a weak symbol
16416 that's not actually defined anywhere. In that case,
16417 'sec' would be NULL, and we should leave the symbol
16418 alone (it will be set to zero elsewhere in the link). */
16419 if (sec == NULL)
16420 break;
16421 /* Fall through. */
16422
16423 case R_PPC64_GOT16_HA:
16424 case R_PPC64_PLTGOT16_HA:
16425 case R_PPC64_PLT16_HA:
16426 case R_PPC64_GOT_TLSGD16_HA:
16427 case R_PPC64_GOT_TLSLD16_HA:
16428 case R_PPC64_GOT_TPREL16_HA:
16429 case R_PPC64_GOT_DTPREL16_HA:
16430 /* Add 0x10000 if sign bit in 0:15 is set.
16431 Bits 0:15 are not used. */
16432 addend += 0x8000;
16433 break;
16434
16435 case R_PPC64_ADDR16_DS:
16436 case R_PPC64_ADDR16_LO_DS:
16437 case R_PPC64_GOT16_DS:
16438 case R_PPC64_GOT16_LO_DS:
16439 case R_PPC64_PLT16_LO_DS:
16440 case R_PPC64_SECTOFF_DS:
16441 case R_PPC64_SECTOFF_LO_DS:
16442 case R_PPC64_TOC16_DS:
16443 case R_PPC64_TOC16_LO_DS:
16444 case R_PPC64_PLTGOT16_DS:
16445 case R_PPC64_PLTGOT16_LO_DS:
16446 case R_PPC64_GOT_TPREL16_DS:
16447 case R_PPC64_GOT_TPREL16_LO_DS:
16448 case R_PPC64_GOT_DTPREL16_DS:
16449 case R_PPC64_GOT_DTPREL16_LO_DS:
16450 case R_PPC64_TPREL16_DS:
16451 case R_PPC64_TPREL16_LO_DS:
16452 case R_PPC64_DTPREL16_DS:
16453 case R_PPC64_DTPREL16_LO_DS:
16454 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16455 mask = 3;
16456 /* If this reloc is against an lq, lxv, or stxv insn, then
16457 the value must be a multiple of 16. This is somewhat of
16458 a hack, but the "correct" way to do this by defining _DQ
16459 forms of all the _DS relocs bloats all reloc switches in
16460 this file. It doesn't make much sense to use these
16461 relocs in data, so testing the insn should be safe. */
16462 if ((insn & (0x3f << 26)) == (56u << 26)
16463 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16464 mask = 15;
16465 relocation += addend;
16466 addend = insn & (mask ^ 3);
16467 if ((relocation & mask) != 0)
16468 {
16469 relocation ^= relocation & mask;
16470 info->callbacks->einfo
16471 /* xgettext:c-format */
16472 (_("%H: error: %s not a multiple of %u\n"),
16473 input_bfd, input_section, rel->r_offset,
16474 ppc64_elf_howto_table[r_type]->name,
16475 mask + 1);
16476 bfd_set_error (bfd_error_bad_value);
16477 ret = FALSE;
16478 goto copy_reloc;
16479 }
16480 break;
16481 }
16482
16483 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16484 because such sections are not SEC_ALLOC and thus ld.so will
16485 not process them. */
16486 howto = ppc64_elf_howto_table[(int) r_type];
16487 if (unresolved_reloc
16488 && !((input_section->flags & SEC_DEBUGGING) != 0
16489 && h->elf.def_dynamic)
16490 && _bfd_elf_section_offset (output_bfd, info, input_section,
16491 rel->r_offset) != (bfd_vma) -1)
16492 {
16493 info->callbacks->einfo
16494 /* xgettext:c-format */
16495 (_("%H: unresolvable %s against `%pT'\n"),
16496 input_bfd, input_section, rel->r_offset,
16497 howto->name,
16498 h->elf.root.root.string);
16499 ret = FALSE;
16500 }
16501
16502 /* 16-bit fields in insns mostly have signed values, but a
16503 few insns have 16-bit unsigned values. Really, we should
16504 have different reloc types. */
16505 if (howto->complain_on_overflow != complain_overflow_dont
16506 && howto->dst_mask == 0xffff
16507 && (input_section->flags & SEC_CODE) != 0)
16508 {
16509 enum complain_overflow complain = complain_overflow_signed;
16510
16511 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16512 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16513 complain = complain_overflow_bitfield;
16514 else if (howto->rightshift == 0
16515 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16516 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16517 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16518 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16519 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16520 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16521 complain = complain_overflow_unsigned;
16522 if (howto->complain_on_overflow != complain)
16523 {
16524 alt_howto = *howto;
16525 alt_howto.complain_on_overflow = complain;
16526 howto = &alt_howto;
16527 }
16528 }
16529
16530 if (r_type == R_PPC64_REL16DX_HA)
16531 {
16532 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
16533 if (rel->r_offset + 4 > input_section->size)
16534 r = bfd_reloc_outofrange;
16535 else
16536 {
16537 relocation += addend;
16538 relocation -= (rel->r_offset
16539 + input_section->output_offset
16540 + input_section->output_section->vma);
16541 relocation = (bfd_signed_vma) relocation >> 16;
16542 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16543 insn &= ~0x1fffc1;
16544 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16545 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16546 r = bfd_reloc_ok;
16547 if (relocation + 0x8000 > 0xffff)
16548 r = bfd_reloc_overflow;
16549 }
16550 }
16551 else
16552 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
16553 rel->r_offset, relocation, addend);
16554
16555 if (r != bfd_reloc_ok)
16556 {
16557 char *more_info = NULL;
16558 const char *reloc_name = howto->name;
16559
16560 if (reloc_dest != DEST_NORMAL)
16561 {
16562 more_info = bfd_malloc (strlen (reloc_name) + 8);
16563 if (more_info != NULL)
16564 {
16565 strcpy (more_info, reloc_name);
16566 strcat (more_info, (reloc_dest == DEST_OPD
16567 ? " (OPD)" : " (stub)"));
16568 reloc_name = more_info;
16569 }
16570 }
16571
16572 if (r == bfd_reloc_overflow)
16573 {
16574 /* On code like "if (foo) foo();" don't report overflow
16575 on a branch to zero when foo is undefined. */
16576 if (!warned
16577 && (reloc_dest == DEST_STUB
16578 || !(h != NULL
16579 && (h->elf.root.type == bfd_link_hash_undefweak
16580 || h->elf.root.type == bfd_link_hash_undefined)
16581 && is_branch_reloc (r_type))))
16582 info->callbacks->reloc_overflow (info, &h->elf.root,
16583 sym_name, reloc_name,
16584 orig_rel.r_addend,
16585 input_bfd, input_section,
16586 rel->r_offset);
16587 }
16588 else
16589 {
16590 info->callbacks->einfo
16591 /* xgettext:c-format */
16592 (_("%H: %s against `%pT': error %d\n"),
16593 input_bfd, input_section, rel->r_offset,
16594 reloc_name, sym_name, (int) r);
16595 ret = FALSE;
16596 }
16597 if (more_info != NULL)
16598 free (more_info);
16599 }
16600 copy_reloc:
16601 if (wrel != rel)
16602 *wrel = *rel;
16603 }
16604
16605 if (wrel != rel)
16606 {
16607 Elf_Internal_Shdr *rel_hdr;
16608 size_t deleted = rel - wrel;
16609
16610 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16611 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16612 if (rel_hdr->sh_size == 0)
16613 {
16614 /* It is too late to remove an empty reloc section. Leave
16615 one NONE reloc.
16616 ??? What is wrong with an empty section??? */
16617 rel_hdr->sh_size = rel_hdr->sh_entsize;
16618 deleted -= 1;
16619 }
16620 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16621 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16622 input_section->reloc_count -= deleted;
16623 }
16624
16625 /* If we're emitting relocations, then shortly after this function
16626 returns, reloc offsets and addends for this section will be
16627 adjusted. Worse, reloc symbol indices will be for the output
16628 file rather than the input. Save a copy of the relocs for
16629 opd_entry_value. */
16630 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16631 {
16632 bfd_size_type amt;
16633 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16634 rel = bfd_alloc (input_bfd, amt);
16635 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16636 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16637 if (rel == NULL)
16638 return FALSE;
16639 memcpy (rel, relocs, amt);
16640 }
16641 return ret;
16642 }
16643
16644 /* Adjust the value of any local symbols in opd sections. */
16645
16646 static int
16647 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16648 const char *name ATTRIBUTE_UNUSED,
16649 Elf_Internal_Sym *elfsym,
16650 asection *input_sec,
16651 struct elf_link_hash_entry *h)
16652 {
16653 struct _opd_sec_data *opd;
16654 long adjust;
16655 bfd_vma value;
16656
16657 if (h != NULL)
16658 return 1;
16659
16660 opd = get_opd_info (input_sec);
16661 if (opd == NULL || opd->adjust == NULL)
16662 return 1;
16663
16664 value = elfsym->st_value - input_sec->output_offset;
16665 if (!bfd_link_relocatable (info))
16666 value -= input_sec->output_section->vma;
16667
16668 adjust = opd->adjust[OPD_NDX (value)];
16669 if (adjust == -1)
16670 return 2;
16671
16672 elfsym->st_value += adjust;
16673 return 1;
16674 }
16675
16676 /* Finish up dynamic symbol handling. We set the contents of various
16677 dynamic sections here. */
16678
16679 static bfd_boolean
16680 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16681 struct bfd_link_info *info,
16682 struct elf_link_hash_entry *h,
16683 Elf_Internal_Sym *sym)
16684 {
16685 struct ppc_link_hash_table *htab;
16686 struct plt_entry *ent;
16687
16688 htab = ppc_hash_table (info);
16689 if (htab == NULL)
16690 return FALSE;
16691
16692 if (!htab->opd_abi && !h->def_regular)
16693 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16694 if (ent->plt.offset != (bfd_vma) -1)
16695 {
16696 /* Mark the symbol as undefined, rather than as
16697 defined in glink. Leave the value if there were
16698 any relocations where pointer equality matters
16699 (this is a clue for the dynamic linker, to make
16700 function pointer comparisons work between an
16701 application and shared library), otherwise set it
16702 to zero. */
16703 sym->st_shndx = SHN_UNDEF;
16704 if (!h->pointer_equality_needed)
16705 sym->st_value = 0;
16706 else if (!h->ref_regular_nonweak)
16707 {
16708 /* This breaks function pointer comparisons, but
16709 that is better than breaking tests for a NULL
16710 function pointer. */
16711 sym->st_value = 0;
16712 }
16713 break;
16714 }
16715
16716 if (h->needs_copy)
16717 {
16718 /* This symbol needs a copy reloc. Set it up. */
16719 Elf_Internal_Rela rela;
16720 asection *srel;
16721 bfd_byte *loc;
16722
16723 if (h->dynindx == -1
16724 || (h->root.type != bfd_link_hash_defined
16725 && h->root.type != bfd_link_hash_defweak)
16726 || htab->elf.srelbss == NULL
16727 || htab->elf.sreldynrelro == NULL)
16728 abort ();
16729
16730 rela.r_offset = (h->root.u.def.value
16731 + h->root.u.def.section->output_section->vma
16732 + h->root.u.def.section->output_offset);
16733 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16734 rela.r_addend = 0;
16735 if (h->root.u.def.section == htab->elf.sdynrelro)
16736 srel = htab->elf.sreldynrelro;
16737 else
16738 srel = htab->elf.srelbss;
16739 loc = srel->contents;
16740 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16741 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16742 }
16743
16744 return TRUE;
16745 }
16746
16747 /* Used to decide how to sort relocs in an optimal manner for the
16748 dynamic linker, before writing them out. */
16749
16750 static enum elf_reloc_type_class
16751 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16752 const asection *rel_sec,
16753 const Elf_Internal_Rela *rela)
16754 {
16755 enum elf_ppc64_reloc_type r_type;
16756 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16757
16758 if (rel_sec == htab->elf.irelplt)
16759 return reloc_class_ifunc;
16760
16761 r_type = ELF64_R_TYPE (rela->r_info);
16762 switch (r_type)
16763 {
16764 case R_PPC64_RELATIVE:
16765 return reloc_class_relative;
16766 case R_PPC64_JMP_SLOT:
16767 return reloc_class_plt;
16768 case R_PPC64_COPY:
16769 return reloc_class_copy;
16770 default:
16771 return reloc_class_normal;
16772 }
16773 }
16774
16775 /* Finish up the dynamic sections. */
16776
16777 static bfd_boolean
16778 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16779 struct bfd_link_info *info)
16780 {
16781 struct ppc_link_hash_table *htab;
16782 bfd *dynobj;
16783 asection *sdyn;
16784
16785 htab = ppc_hash_table (info);
16786 if (htab == NULL)
16787 return FALSE;
16788
16789 dynobj = htab->elf.dynobj;
16790 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16791
16792 if (htab->elf.dynamic_sections_created)
16793 {
16794 Elf64_External_Dyn *dyncon, *dynconend;
16795
16796 if (sdyn == NULL || htab->elf.sgot == NULL)
16797 abort ();
16798
16799 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16800 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16801 for (; dyncon < dynconend; dyncon++)
16802 {
16803 Elf_Internal_Dyn dyn;
16804 asection *s;
16805
16806 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16807
16808 switch (dyn.d_tag)
16809 {
16810 default:
16811 continue;
16812
16813 case DT_PPC64_GLINK:
16814 s = htab->glink;
16815 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16816 /* We stupidly defined DT_PPC64_GLINK to be the start
16817 of glink rather than the first entry point, which is
16818 what ld.so needs, and now have a bigger stub to
16819 support automatic multiple TOCs. */
16820 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16821 break;
16822
16823 case DT_PPC64_OPD:
16824 s = bfd_get_section_by_name (output_bfd, ".opd");
16825 if (s == NULL)
16826 continue;
16827 dyn.d_un.d_ptr = s->vma;
16828 break;
16829
16830 case DT_PPC64_OPT:
16831 if (htab->do_multi_toc && htab->multi_toc_needed)
16832 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16833 if (htab->has_plt_localentry0)
16834 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16835 break;
16836
16837 case DT_PPC64_OPDSZ:
16838 s = bfd_get_section_by_name (output_bfd, ".opd");
16839 if (s == NULL)
16840 continue;
16841 dyn.d_un.d_val = s->size;
16842 break;
16843
16844 case DT_PLTGOT:
16845 s = htab->elf.splt;
16846 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16847 break;
16848
16849 case DT_JMPREL:
16850 s = htab->elf.srelplt;
16851 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16852 break;
16853
16854 case DT_PLTRELSZ:
16855 dyn.d_un.d_val = htab->elf.srelplt->size;
16856 break;
16857
16858 case DT_TEXTREL:
16859 if (htab->local_ifunc_resolver)
16860 info->callbacks->einfo
16861 (_("%X%P: text relocations and GNU indirect "
16862 "functions will result in a segfault at runtime\n"));
16863 else if (htab->maybe_local_ifunc_resolver)
16864 info->callbacks->einfo
16865 (_("%P: warning: text relocations and GNU indirect "
16866 "functions may result in a segfault at runtime\n"));
16867 continue;
16868 }
16869
16870 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16871 }
16872 }
16873
16874 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16875 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16876 {
16877 /* Fill in the first entry in the global offset table.
16878 We use it to hold the link-time TOCbase. */
16879 bfd_put_64 (output_bfd,
16880 elf_gp (output_bfd) + TOC_BASE_OFF,
16881 htab->elf.sgot->contents);
16882
16883 /* Set .got entry size. */
16884 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
16885 = 8;
16886 }
16887
16888 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16889 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16890 {
16891 /* Set .plt entry size. */
16892 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16893 = PLT_ENTRY_SIZE (htab);
16894 }
16895
16896 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16897 brlt ourselves if emitrelocations. */
16898 if (htab->brlt != NULL
16899 && htab->brlt->reloc_count != 0
16900 && !_bfd_elf_link_output_relocs (output_bfd,
16901 htab->brlt,
16902 elf_section_data (htab->brlt)->rela.hdr,
16903 elf_section_data (htab->brlt)->relocs,
16904 NULL))
16905 return FALSE;
16906
16907 if (htab->glink != NULL
16908 && htab->glink->reloc_count != 0
16909 && !_bfd_elf_link_output_relocs (output_bfd,
16910 htab->glink,
16911 elf_section_data (htab->glink)->rela.hdr,
16912 elf_section_data (htab->glink)->relocs,
16913 NULL))
16914 return FALSE;
16915
16916
16917 if (htab->glink_eh_frame != NULL
16918 && htab->glink_eh_frame->size != 0
16919 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16920 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16921 htab->glink_eh_frame,
16922 htab->glink_eh_frame->contents))
16923 return FALSE;
16924
16925 /* We need to handle writing out multiple GOT sections ourselves,
16926 since we didn't add them to DYNOBJ. We know dynobj is the first
16927 bfd. */
16928 while ((dynobj = dynobj->link.next) != NULL)
16929 {
16930 asection *s;
16931
16932 if (!is_ppc64_elf (dynobj))
16933 continue;
16934
16935 s = ppc64_elf_tdata (dynobj)->got;
16936 if (s != NULL
16937 && s->size != 0
16938 && s->output_section != bfd_abs_section_ptr
16939 && !bfd_set_section_contents (output_bfd, s->output_section,
16940 s->contents, s->output_offset,
16941 s->size))
16942 return FALSE;
16943 s = ppc64_elf_tdata (dynobj)->relgot;
16944 if (s != NULL
16945 && s->size != 0
16946 && s->output_section != bfd_abs_section_ptr
16947 && !bfd_set_section_contents (output_bfd, s->output_section,
16948 s->contents, s->output_offset,
16949 s->size))
16950 return FALSE;
16951 }
16952
16953 return TRUE;
16954 }
16955
16956 #include "elf64-target.h"
16957
16958 /* FreeBSD support */
16959
16960 #undef TARGET_LITTLE_SYM
16961 #undef TARGET_LITTLE_NAME
16962
16963 #undef TARGET_BIG_SYM
16964 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16965 #undef TARGET_BIG_NAME
16966 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16967
16968 #undef ELF_OSABI
16969 #define ELF_OSABI ELFOSABI_FREEBSD
16970
16971 #undef elf64_bed
16972 #define elf64_bed elf64_powerpc_fbsd_bed
16973
16974 #include "elf64-target.h"