Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22
23
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 #include "sysdep.h"
29 #include <stdarg.h>
30 #include "bfd.h"
31 #include "bfdlink.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
36 #include "dwarf2.h"
37
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
58
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x10000
68 #define elf_info_to_howto ppc64_elf_info_to_howto
69
70 #define elf_backend_want_got_sym 0
71 #define elf_backend_want_plt_sym 0
72 #define elf_backend_plt_alignment 3
73 #define elf_backend_plt_not_loaded 1
74 #define elf_backend_got_header_size 8
75 #define elf_backend_want_dynrelro 1
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_dtrel_excludes_plt 1
80 #define elf_backend_default_execstack 0
81
82 #define bfd_elf64_mkobject ppc64_elf_mkobject
83 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
84 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
85 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
86 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
87 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
88 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
89 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
91 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
92
93 #define elf_backend_object_p ppc64_elf_object_p
94 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
95 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
96 #define elf_backend_write_core_note ppc64_elf_write_core_note
97 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
98 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
99 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
100 #define elf_backend_check_directives ppc64_elf_before_check_relocs
101 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
102 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
103 #define elf_backend_check_relocs ppc64_elf_check_relocs
104 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
105 #define elf_backend_gc_keep ppc64_elf_gc_keep
106 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
107 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
108 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
109 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
110 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
111 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
112 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
113 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
114 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
115 #define elf_backend_action_discarded ppc64_elf_action_discarded
116 #define elf_backend_relocate_section ppc64_elf_relocate_section
117 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
118 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
119 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
120 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
121 #define elf_backend_special_sections ppc64_elf_special_sections
122 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
123 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
124 #define elf_backend_get_reloc_section bfd_get_section_by_name
125
126 /* The name of the dynamic interpreter. This is put in the .interp
127 section. */
128 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
129
130 /* The size in bytes of an entry in the procedure linkage table. */
131 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
132
133 /* The initial size of the plt reserved for the dynamic linker. */
134 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
135
136 /* Offsets to some stack save slots. */
137 #define STK_LR 16
138 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
139 /* This one is dodgy. ELFv2 does not have a linker word, so use the
140 CR save slot. Used only by optimised __tls_get_addr call stub,
141 relying on __tls_get_addr_opt not saving CR.. */
142 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
143
144 /* TOC base pointers offset from start of TOC. */
145 #define TOC_BASE_OFF 0x8000
146 /* TOC base alignment. */
147 #define TOC_BASE_ALIGN 256
148
149 /* Offset of tp and dtp pointers from start of TLS block. */
150 #define TP_OFFSET 0x7000
151 #define DTP_OFFSET 0x8000
152
153 /* .plt call stub instructions. The normal stub is like this, but
154 sometimes the .plt entry crosses a 64k boundary and we need to
155 insert an addi to adjust r11. */
156 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
157 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
158 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
159 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
160 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
161 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
162 #define BCTR 0x4e800420 /* bctr */
163
164 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
165 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
166 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
167
168 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
169 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
170 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
171 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
172 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
173 #define BNECTR 0x4ca20420 /* bnectr+ */
174 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
175
176 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
177 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
178 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
179
180 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
181 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
182 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
183
184 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
185 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
186 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
187 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
188 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
189
190 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
191 #define GLINK_PLTRESOLVE_SIZE(htab) \
192 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
193 /* 0: */
194 /* .quad plt0-1f */
195 /* __glink: */
196 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
197 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
198 /* 1: */
199 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
200 /* ld %2,(0b-1b)(%11) */
201 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
202 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
203 /* ld %12,0(%11) */
204 /* ld %2,8(%11) */
205 /* mtctr %12 */
206 /* ld %11,16(%11) */
207 /* bctr */
208 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
209 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
210 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
211 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
212 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
213
214 /* Pad with this. */
215 #define NOP 0x60000000
216
217 /* Some other nops. */
218 #define CROR_151515 0x4def7b82
219 #define CROR_313131 0x4ffffb82
220
221 /* .glink entries for the first 32k functions are two instructions. */
222 #define LI_R0_0 0x38000000 /* li %r0,0 */
223 #define B_DOT 0x48000000 /* b . */
224
225 /* After that, we need two instructions to load the index, followed by
226 a branch. */
227 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
228 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
229
230 /* Instructions used by the save and restore reg functions. */
231 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
232 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
233 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
234 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
235 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
236 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
237 #define LI_R12_0 0x39800000 /* li %r12,0 */
238 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
239 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
240 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
241 #define BLR 0x4e800020 /* blr */
242
243 /* Since .opd is an array of descriptors and each entry will end up
244 with identical R_PPC64_RELATIVE relocs, there is really no need to
245 propagate .opd relocs; The dynamic linker should be taught to
246 relocate .opd without reloc entries. */
247 #ifndef NO_OPD_RELOCS
248 #define NO_OPD_RELOCS 0
249 #endif
250
251 #ifndef ARRAY_SIZE
252 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
253 #endif
254
255 static inline int
256 abiversion (bfd *abfd)
257 {
258 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
259 }
260
261 static inline void
262 set_abiversion (bfd *abfd, int ver)
263 {
264 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
265 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
266 }
267 \f
268 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
269
270 /* Relocation HOWTO's. */
271 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
272
273 static reloc_howto_type ppc64_elf_howto_raw[] = {
274 /* This reloc does nothing. */
275 HOWTO (R_PPC64_NONE, /* type */
276 0, /* rightshift */
277 3, /* size (0 = byte, 1 = short, 2 = long) */
278 0, /* bitsize */
279 FALSE, /* pc_relative */
280 0, /* bitpos */
281 complain_overflow_dont, /* complain_on_overflow */
282 bfd_elf_generic_reloc, /* special_function */
283 "R_PPC64_NONE", /* name */
284 FALSE, /* partial_inplace */
285 0, /* src_mask */
286 0, /* dst_mask */
287 FALSE), /* pcrel_offset */
288
289 /* A standard 32 bit relocation. */
290 HOWTO (R_PPC64_ADDR32, /* type */
291 0, /* rightshift */
292 2, /* size (0 = byte, 1 = short, 2 = long) */
293 32, /* bitsize */
294 FALSE, /* pc_relative */
295 0, /* bitpos */
296 complain_overflow_bitfield, /* complain_on_overflow */
297 bfd_elf_generic_reloc, /* special_function */
298 "R_PPC64_ADDR32", /* name */
299 FALSE, /* partial_inplace */
300 0, /* src_mask */
301 0xffffffff, /* dst_mask */
302 FALSE), /* pcrel_offset */
303
304 /* An absolute 26 bit branch; the lower two bits must be zero.
305 FIXME: we don't check that, we just clear them. */
306 HOWTO (R_PPC64_ADDR24, /* type */
307 0, /* rightshift */
308 2, /* size (0 = byte, 1 = short, 2 = long) */
309 26, /* bitsize */
310 FALSE, /* pc_relative */
311 0, /* bitpos */
312 complain_overflow_bitfield, /* complain_on_overflow */
313 bfd_elf_generic_reloc, /* special_function */
314 "R_PPC64_ADDR24", /* name */
315 FALSE, /* partial_inplace */
316 0, /* src_mask */
317 0x03fffffc, /* dst_mask */
318 FALSE), /* pcrel_offset */
319
320 /* A standard 16 bit relocation. */
321 HOWTO (R_PPC64_ADDR16, /* type */
322 0, /* rightshift */
323 1, /* size (0 = byte, 1 = short, 2 = long) */
324 16, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_PPC64_ADDR16", /* name */
330 FALSE, /* partial_inplace */
331 0, /* src_mask */
332 0xffff, /* dst_mask */
333 FALSE), /* pcrel_offset */
334
335 /* A 16 bit relocation without overflow. */
336 HOWTO (R_PPC64_ADDR16_LO, /* type */
337 0, /* rightshift */
338 1, /* size (0 = byte, 1 = short, 2 = long) */
339 16, /* bitsize */
340 FALSE, /* pc_relative */
341 0, /* bitpos */
342 complain_overflow_dont,/* complain_on_overflow */
343 bfd_elf_generic_reloc, /* special_function */
344 "R_PPC64_ADDR16_LO", /* name */
345 FALSE, /* partial_inplace */
346 0, /* src_mask */
347 0xffff, /* dst_mask */
348 FALSE), /* pcrel_offset */
349
350 /* Bits 16-31 of an address. */
351 HOWTO (R_PPC64_ADDR16_HI, /* type */
352 16, /* rightshift */
353 1, /* size (0 = byte, 1 = short, 2 = long) */
354 16, /* bitsize */
355 FALSE, /* pc_relative */
356 0, /* bitpos */
357 complain_overflow_signed, /* complain_on_overflow */
358 bfd_elf_generic_reloc, /* special_function */
359 "R_PPC64_ADDR16_HI", /* name */
360 FALSE, /* partial_inplace */
361 0, /* src_mask */
362 0xffff, /* dst_mask */
363 FALSE), /* pcrel_offset */
364
365 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
366 bits, treated as a signed number, is negative. */
367 HOWTO (R_PPC64_ADDR16_HA, /* type */
368 16, /* rightshift */
369 1, /* size (0 = byte, 1 = short, 2 = long) */
370 16, /* bitsize */
371 FALSE, /* pc_relative */
372 0, /* bitpos */
373 complain_overflow_signed, /* complain_on_overflow */
374 ppc64_elf_ha_reloc, /* special_function */
375 "R_PPC64_ADDR16_HA", /* name */
376 FALSE, /* partial_inplace */
377 0, /* src_mask */
378 0xffff, /* dst_mask */
379 FALSE), /* pcrel_offset */
380
381 /* An absolute 16 bit branch; the lower two bits must be zero.
382 FIXME: we don't check that, we just clear them. */
383 HOWTO (R_PPC64_ADDR14, /* type */
384 0, /* rightshift */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
386 16, /* bitsize */
387 FALSE, /* pc_relative */
388 0, /* bitpos */
389 complain_overflow_signed, /* complain_on_overflow */
390 ppc64_elf_branch_reloc, /* special_function */
391 "R_PPC64_ADDR14", /* name */
392 FALSE, /* partial_inplace */
393 0, /* src_mask */
394 0x0000fffc, /* dst_mask */
395 FALSE), /* pcrel_offset */
396
397 /* An absolute 16 bit branch, for which bit 10 should be set to
398 indicate that the branch is expected to be taken. The lower two
399 bits must be zero. */
400 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
401 0, /* rightshift */
402 2, /* size (0 = byte, 1 = short, 2 = long) */
403 16, /* bitsize */
404 FALSE, /* pc_relative */
405 0, /* bitpos */
406 complain_overflow_signed, /* complain_on_overflow */
407 ppc64_elf_brtaken_reloc, /* special_function */
408 "R_PPC64_ADDR14_BRTAKEN",/* name */
409 FALSE, /* partial_inplace */
410 0, /* src_mask */
411 0x0000fffc, /* dst_mask */
412 FALSE), /* pcrel_offset */
413
414 /* An absolute 16 bit branch, for which bit 10 should be set to
415 indicate that the branch is not expected to be taken. The lower
416 two bits must be zero. */
417 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
418 0, /* rightshift */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
420 16, /* bitsize */
421 FALSE, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_signed, /* complain_on_overflow */
424 ppc64_elf_brtaken_reloc, /* special_function */
425 "R_PPC64_ADDR14_BRNTAKEN",/* name */
426 FALSE, /* partial_inplace */
427 0, /* src_mask */
428 0x0000fffc, /* dst_mask */
429 FALSE), /* pcrel_offset */
430
431 /* A relative 26 bit branch; the lower two bits must be zero. */
432 HOWTO (R_PPC64_REL24, /* type */
433 0, /* rightshift */
434 2, /* size (0 = byte, 1 = short, 2 = long) */
435 26, /* bitsize */
436 TRUE, /* pc_relative */
437 0, /* bitpos */
438 complain_overflow_signed, /* complain_on_overflow */
439 ppc64_elf_branch_reloc, /* special_function */
440 "R_PPC64_REL24", /* name */
441 FALSE, /* partial_inplace */
442 0, /* src_mask */
443 0x03fffffc, /* dst_mask */
444 TRUE), /* pcrel_offset */
445
446 /* A relative 16 bit branch; the lower two bits must be zero. */
447 HOWTO (R_PPC64_REL14, /* type */
448 0, /* rightshift */
449 2, /* size (0 = byte, 1 = short, 2 = long) */
450 16, /* bitsize */
451 TRUE, /* pc_relative */
452 0, /* bitpos */
453 complain_overflow_signed, /* complain_on_overflow */
454 ppc64_elf_branch_reloc, /* special_function */
455 "R_PPC64_REL14", /* name */
456 FALSE, /* partial_inplace */
457 0, /* src_mask */
458 0x0000fffc, /* dst_mask */
459 TRUE), /* pcrel_offset */
460
461 /* A relative 16 bit branch. Bit 10 should be set to indicate that
462 the branch is expected to be taken. The lower two bits must be
463 zero. */
464 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
465 0, /* rightshift */
466 2, /* size (0 = byte, 1 = short, 2 = long) */
467 16, /* bitsize */
468 TRUE, /* pc_relative */
469 0, /* bitpos */
470 complain_overflow_signed, /* complain_on_overflow */
471 ppc64_elf_brtaken_reloc, /* special_function */
472 "R_PPC64_REL14_BRTAKEN", /* name */
473 FALSE, /* partial_inplace */
474 0, /* src_mask */
475 0x0000fffc, /* dst_mask */
476 TRUE), /* pcrel_offset */
477
478 /* A relative 16 bit branch. Bit 10 should be set to indicate that
479 the branch is not expected to be taken. The lower two bits must
480 be zero. */
481 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
482 0, /* rightshift */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
484 16, /* bitsize */
485 TRUE, /* pc_relative */
486 0, /* bitpos */
487 complain_overflow_signed, /* complain_on_overflow */
488 ppc64_elf_brtaken_reloc, /* special_function */
489 "R_PPC64_REL14_BRNTAKEN",/* name */
490 FALSE, /* partial_inplace */
491 0, /* src_mask */
492 0x0000fffc, /* dst_mask */
493 TRUE), /* pcrel_offset */
494
495 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
496 symbol. */
497 HOWTO (R_PPC64_GOT16, /* type */
498 0, /* rightshift */
499 1, /* size (0 = byte, 1 = short, 2 = long) */
500 16, /* bitsize */
501 FALSE, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_signed, /* complain_on_overflow */
504 ppc64_elf_unhandled_reloc, /* special_function */
505 "R_PPC64_GOT16", /* name */
506 FALSE, /* partial_inplace */
507 0, /* src_mask */
508 0xffff, /* dst_mask */
509 FALSE), /* pcrel_offset */
510
511 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
512 the symbol. */
513 HOWTO (R_PPC64_GOT16_LO, /* type */
514 0, /* rightshift */
515 1, /* size (0 = byte, 1 = short, 2 = long) */
516 16, /* bitsize */
517 FALSE, /* pc_relative */
518 0, /* bitpos */
519 complain_overflow_dont, /* complain_on_overflow */
520 ppc64_elf_unhandled_reloc, /* special_function */
521 "R_PPC64_GOT16_LO", /* name */
522 FALSE, /* partial_inplace */
523 0, /* src_mask */
524 0xffff, /* dst_mask */
525 FALSE), /* pcrel_offset */
526
527 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
528 the symbol. */
529 HOWTO (R_PPC64_GOT16_HI, /* type */
530 16, /* rightshift */
531 1, /* size (0 = byte, 1 = short, 2 = long) */
532 16, /* bitsize */
533 FALSE, /* pc_relative */
534 0, /* bitpos */
535 complain_overflow_signed,/* complain_on_overflow */
536 ppc64_elf_unhandled_reloc, /* special_function */
537 "R_PPC64_GOT16_HI", /* name */
538 FALSE, /* partial_inplace */
539 0, /* src_mask */
540 0xffff, /* dst_mask */
541 FALSE), /* pcrel_offset */
542
543 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
544 the symbol. */
545 HOWTO (R_PPC64_GOT16_HA, /* type */
546 16, /* rightshift */
547 1, /* size (0 = byte, 1 = short, 2 = long) */
548 16, /* bitsize */
549 FALSE, /* pc_relative */
550 0, /* bitpos */
551 complain_overflow_signed,/* complain_on_overflow */
552 ppc64_elf_unhandled_reloc, /* special_function */
553 "R_PPC64_GOT16_HA", /* name */
554 FALSE, /* partial_inplace */
555 0, /* src_mask */
556 0xffff, /* dst_mask */
557 FALSE), /* pcrel_offset */
558
559 /* This is used only by the dynamic linker. The symbol should exist
560 both in the object being run and in some shared library. The
561 dynamic linker copies the data addressed by the symbol from the
562 shared library into the object, because the object being
563 run has to have the data at some particular address. */
564 HOWTO (R_PPC64_COPY, /* type */
565 0, /* rightshift */
566 0, /* this one is variable size */
567 0, /* bitsize */
568 FALSE, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_dont, /* complain_on_overflow */
571 ppc64_elf_unhandled_reloc, /* special_function */
572 "R_PPC64_COPY", /* name */
573 FALSE, /* partial_inplace */
574 0, /* src_mask */
575 0, /* dst_mask */
576 FALSE), /* pcrel_offset */
577
578 /* Like R_PPC64_ADDR64, but used when setting global offset table
579 entries. */
580 HOWTO (R_PPC64_GLOB_DAT, /* type */
581 0, /* rightshift */
582 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
583 64, /* bitsize */
584 FALSE, /* pc_relative */
585 0, /* bitpos */
586 complain_overflow_dont, /* complain_on_overflow */
587 ppc64_elf_unhandled_reloc, /* special_function */
588 "R_PPC64_GLOB_DAT", /* name */
589 FALSE, /* partial_inplace */
590 0, /* src_mask */
591 ONES (64), /* dst_mask */
592 FALSE), /* pcrel_offset */
593
594 /* Created by the link editor. Marks a procedure linkage table
595 entry for a symbol. */
596 HOWTO (R_PPC64_JMP_SLOT, /* type */
597 0, /* rightshift */
598 0, /* size (0 = byte, 1 = short, 2 = long) */
599 0, /* bitsize */
600 FALSE, /* pc_relative */
601 0, /* bitpos */
602 complain_overflow_dont, /* complain_on_overflow */
603 ppc64_elf_unhandled_reloc, /* special_function */
604 "R_PPC64_JMP_SLOT", /* name */
605 FALSE, /* partial_inplace */
606 0, /* src_mask */
607 0, /* dst_mask */
608 FALSE), /* pcrel_offset */
609
610 /* Used only by the dynamic linker. When the object is run, this
611 doubleword64 is set to the load address of the object, plus the
612 addend. */
613 HOWTO (R_PPC64_RELATIVE, /* type */
614 0, /* rightshift */
615 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
616 64, /* bitsize */
617 FALSE, /* pc_relative */
618 0, /* bitpos */
619 complain_overflow_dont, /* complain_on_overflow */
620 bfd_elf_generic_reloc, /* special_function */
621 "R_PPC64_RELATIVE", /* name */
622 FALSE, /* partial_inplace */
623 0, /* src_mask */
624 ONES (64), /* dst_mask */
625 FALSE), /* pcrel_offset */
626
627 /* Like R_PPC64_ADDR32, but may be unaligned. */
628 HOWTO (R_PPC64_UADDR32, /* type */
629 0, /* rightshift */
630 2, /* size (0 = byte, 1 = short, 2 = long) */
631 32, /* bitsize */
632 FALSE, /* pc_relative */
633 0, /* bitpos */
634 complain_overflow_bitfield, /* complain_on_overflow */
635 bfd_elf_generic_reloc, /* special_function */
636 "R_PPC64_UADDR32", /* name */
637 FALSE, /* partial_inplace */
638 0, /* src_mask */
639 0xffffffff, /* dst_mask */
640 FALSE), /* pcrel_offset */
641
642 /* Like R_PPC64_ADDR16, but may be unaligned. */
643 HOWTO (R_PPC64_UADDR16, /* type */
644 0, /* rightshift */
645 1, /* size (0 = byte, 1 = short, 2 = long) */
646 16, /* bitsize */
647 FALSE, /* pc_relative */
648 0, /* bitpos */
649 complain_overflow_bitfield, /* complain_on_overflow */
650 bfd_elf_generic_reloc, /* special_function */
651 "R_PPC64_UADDR16", /* name */
652 FALSE, /* partial_inplace */
653 0, /* src_mask */
654 0xffff, /* dst_mask */
655 FALSE), /* pcrel_offset */
656
657 /* 32-bit PC relative. */
658 HOWTO (R_PPC64_REL32, /* type */
659 0, /* rightshift */
660 2, /* size (0 = byte, 1 = short, 2 = long) */
661 32, /* bitsize */
662 TRUE, /* pc_relative */
663 0, /* bitpos */
664 complain_overflow_signed, /* complain_on_overflow */
665 bfd_elf_generic_reloc, /* special_function */
666 "R_PPC64_REL32", /* name */
667 FALSE, /* partial_inplace */
668 0, /* src_mask */
669 0xffffffff, /* dst_mask */
670 TRUE), /* pcrel_offset */
671
672 /* 32-bit relocation to the symbol's procedure linkage table. */
673 HOWTO (R_PPC64_PLT32, /* type */
674 0, /* rightshift */
675 2, /* size (0 = byte, 1 = short, 2 = long) */
676 32, /* bitsize */
677 FALSE, /* pc_relative */
678 0, /* bitpos */
679 complain_overflow_bitfield, /* complain_on_overflow */
680 ppc64_elf_unhandled_reloc, /* special_function */
681 "R_PPC64_PLT32", /* name */
682 FALSE, /* partial_inplace */
683 0, /* src_mask */
684 0xffffffff, /* dst_mask */
685 FALSE), /* pcrel_offset */
686
687 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
688 FIXME: R_PPC64_PLTREL32 not supported. */
689 HOWTO (R_PPC64_PLTREL32, /* type */
690 0, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 32, /* bitsize */
693 TRUE, /* pc_relative */
694 0, /* bitpos */
695 complain_overflow_signed, /* complain_on_overflow */
696 ppc64_elf_unhandled_reloc, /* special_function */
697 "R_PPC64_PLTREL32", /* name */
698 FALSE, /* partial_inplace */
699 0, /* src_mask */
700 0xffffffff, /* dst_mask */
701 TRUE), /* pcrel_offset */
702
703 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
704 the symbol. */
705 HOWTO (R_PPC64_PLT16_LO, /* type */
706 0, /* rightshift */
707 1, /* size (0 = byte, 1 = short, 2 = long) */
708 16, /* bitsize */
709 FALSE, /* pc_relative */
710 0, /* bitpos */
711 complain_overflow_dont, /* complain_on_overflow */
712 ppc64_elf_unhandled_reloc, /* special_function */
713 "R_PPC64_PLT16_LO", /* name */
714 FALSE, /* partial_inplace */
715 0, /* src_mask */
716 0xffff, /* dst_mask */
717 FALSE), /* pcrel_offset */
718
719 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
720 the symbol. */
721 HOWTO (R_PPC64_PLT16_HI, /* type */
722 16, /* rightshift */
723 1, /* size (0 = byte, 1 = short, 2 = long) */
724 16, /* bitsize */
725 FALSE, /* pc_relative */
726 0, /* bitpos */
727 complain_overflow_signed, /* complain_on_overflow */
728 ppc64_elf_unhandled_reloc, /* special_function */
729 "R_PPC64_PLT16_HI", /* name */
730 FALSE, /* partial_inplace */
731 0, /* src_mask */
732 0xffff, /* dst_mask */
733 FALSE), /* pcrel_offset */
734
735 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
736 the symbol. */
737 HOWTO (R_PPC64_PLT16_HA, /* type */
738 16, /* rightshift */
739 1, /* size (0 = byte, 1 = short, 2 = long) */
740 16, /* bitsize */
741 FALSE, /* pc_relative */
742 0, /* bitpos */
743 complain_overflow_signed, /* complain_on_overflow */
744 ppc64_elf_unhandled_reloc, /* special_function */
745 "R_PPC64_PLT16_HA", /* name */
746 FALSE, /* partial_inplace */
747 0, /* src_mask */
748 0xffff, /* dst_mask */
749 FALSE), /* pcrel_offset */
750
751 /* 16-bit section relative relocation. */
752 HOWTO (R_PPC64_SECTOFF, /* type */
753 0, /* rightshift */
754 1, /* size (0 = byte, 1 = short, 2 = long) */
755 16, /* bitsize */
756 FALSE, /* pc_relative */
757 0, /* bitpos */
758 complain_overflow_signed, /* complain_on_overflow */
759 ppc64_elf_sectoff_reloc, /* special_function */
760 "R_PPC64_SECTOFF", /* name */
761 FALSE, /* partial_inplace */
762 0, /* src_mask */
763 0xffff, /* dst_mask */
764 FALSE), /* pcrel_offset */
765
766 /* Like R_PPC64_SECTOFF, but no overflow warning. */
767 HOWTO (R_PPC64_SECTOFF_LO, /* type */
768 0, /* rightshift */
769 1, /* size (0 = byte, 1 = short, 2 = long) */
770 16, /* bitsize */
771 FALSE, /* pc_relative */
772 0, /* bitpos */
773 complain_overflow_dont, /* complain_on_overflow */
774 ppc64_elf_sectoff_reloc, /* special_function */
775 "R_PPC64_SECTOFF_LO", /* name */
776 FALSE, /* partial_inplace */
777 0, /* src_mask */
778 0xffff, /* dst_mask */
779 FALSE), /* pcrel_offset */
780
781 /* 16-bit upper half section relative relocation. */
782 HOWTO (R_PPC64_SECTOFF_HI, /* type */
783 16, /* rightshift */
784 1, /* size (0 = byte, 1 = short, 2 = long) */
785 16, /* bitsize */
786 FALSE, /* pc_relative */
787 0, /* bitpos */
788 complain_overflow_signed, /* complain_on_overflow */
789 ppc64_elf_sectoff_reloc, /* special_function */
790 "R_PPC64_SECTOFF_HI", /* name */
791 FALSE, /* partial_inplace */
792 0, /* src_mask */
793 0xffff, /* dst_mask */
794 FALSE), /* pcrel_offset */
795
796 /* 16-bit upper half adjusted section relative relocation. */
797 HOWTO (R_PPC64_SECTOFF_HA, /* type */
798 16, /* rightshift */
799 1, /* size (0 = byte, 1 = short, 2 = long) */
800 16, /* bitsize */
801 FALSE, /* pc_relative */
802 0, /* bitpos */
803 complain_overflow_signed, /* complain_on_overflow */
804 ppc64_elf_sectoff_ha_reloc, /* special_function */
805 "R_PPC64_SECTOFF_HA", /* name */
806 FALSE, /* partial_inplace */
807 0, /* src_mask */
808 0xffff, /* dst_mask */
809 FALSE), /* pcrel_offset */
810
811 /* Like R_PPC64_REL24 without touching the two least significant bits. */
812 HOWTO (R_PPC64_REL30, /* type */
813 2, /* rightshift */
814 2, /* size (0 = byte, 1 = short, 2 = long) */
815 30, /* bitsize */
816 TRUE, /* pc_relative */
817 0, /* bitpos */
818 complain_overflow_dont, /* complain_on_overflow */
819 bfd_elf_generic_reloc, /* special_function */
820 "R_PPC64_REL30", /* name */
821 FALSE, /* partial_inplace */
822 0, /* src_mask */
823 0xfffffffc, /* dst_mask */
824 TRUE), /* pcrel_offset */
825
826 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
827
828 /* A standard 64-bit relocation. */
829 HOWTO (R_PPC64_ADDR64, /* type */
830 0, /* rightshift */
831 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
832 64, /* bitsize */
833 FALSE, /* pc_relative */
834 0, /* bitpos */
835 complain_overflow_dont, /* complain_on_overflow */
836 bfd_elf_generic_reloc, /* special_function */
837 "R_PPC64_ADDR64", /* name */
838 FALSE, /* partial_inplace */
839 0, /* src_mask */
840 ONES (64), /* dst_mask */
841 FALSE), /* pcrel_offset */
842
843 /* The bits 32-47 of an address. */
844 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
845 32, /* rightshift */
846 1, /* size (0 = byte, 1 = short, 2 = long) */
847 16, /* bitsize */
848 FALSE, /* pc_relative */
849 0, /* bitpos */
850 complain_overflow_dont, /* complain_on_overflow */
851 bfd_elf_generic_reloc, /* special_function */
852 "R_PPC64_ADDR16_HIGHER", /* name */
853 FALSE, /* partial_inplace */
854 0, /* src_mask */
855 0xffff, /* dst_mask */
856 FALSE), /* pcrel_offset */
857
858 /* The bits 32-47 of an address, plus 1 if the contents of the low
859 16 bits, treated as a signed number, is negative. */
860 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
861 32, /* rightshift */
862 1, /* size (0 = byte, 1 = short, 2 = long) */
863 16, /* bitsize */
864 FALSE, /* pc_relative */
865 0, /* bitpos */
866 complain_overflow_dont, /* complain_on_overflow */
867 ppc64_elf_ha_reloc, /* special_function */
868 "R_PPC64_ADDR16_HIGHERA", /* name */
869 FALSE, /* partial_inplace */
870 0, /* src_mask */
871 0xffff, /* dst_mask */
872 FALSE), /* pcrel_offset */
873
874 /* The bits 48-63 of an address. */
875 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
876 48, /* rightshift */
877 1, /* size (0 = byte, 1 = short, 2 = long) */
878 16, /* bitsize */
879 FALSE, /* pc_relative */
880 0, /* bitpos */
881 complain_overflow_dont, /* complain_on_overflow */
882 bfd_elf_generic_reloc, /* special_function */
883 "R_PPC64_ADDR16_HIGHEST", /* name */
884 FALSE, /* partial_inplace */
885 0, /* src_mask */
886 0xffff, /* dst_mask */
887 FALSE), /* pcrel_offset */
888
889 /* The bits 48-63 of an address, plus 1 if the contents of the low
890 16 bits, treated as a signed number, is negative. */
891 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
892 48, /* rightshift */
893 1, /* size (0 = byte, 1 = short, 2 = long) */
894 16, /* bitsize */
895 FALSE, /* pc_relative */
896 0, /* bitpos */
897 complain_overflow_dont, /* complain_on_overflow */
898 ppc64_elf_ha_reloc, /* special_function */
899 "R_PPC64_ADDR16_HIGHESTA", /* name */
900 FALSE, /* partial_inplace */
901 0, /* src_mask */
902 0xffff, /* dst_mask */
903 FALSE), /* pcrel_offset */
904
905 /* Like ADDR64, but may be unaligned. */
906 HOWTO (R_PPC64_UADDR64, /* type */
907 0, /* rightshift */
908 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
909 64, /* bitsize */
910 FALSE, /* pc_relative */
911 0, /* bitpos */
912 complain_overflow_dont, /* complain_on_overflow */
913 bfd_elf_generic_reloc, /* special_function */
914 "R_PPC64_UADDR64", /* name */
915 FALSE, /* partial_inplace */
916 0, /* src_mask */
917 ONES (64), /* dst_mask */
918 FALSE), /* pcrel_offset */
919
920 /* 64-bit relative relocation. */
921 HOWTO (R_PPC64_REL64, /* type */
922 0, /* rightshift */
923 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
924 64, /* bitsize */
925 TRUE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_dont, /* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_PPC64_REL64", /* name */
930 FALSE, /* partial_inplace */
931 0, /* src_mask */
932 ONES (64), /* dst_mask */
933 TRUE), /* pcrel_offset */
934
935 /* 64-bit relocation to the symbol's procedure linkage table. */
936 HOWTO (R_PPC64_PLT64, /* type */
937 0, /* rightshift */
938 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
939 64, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_dont, /* complain_on_overflow */
943 ppc64_elf_unhandled_reloc, /* special_function */
944 "R_PPC64_PLT64", /* name */
945 FALSE, /* partial_inplace */
946 0, /* src_mask */
947 ONES (64), /* dst_mask */
948 FALSE), /* pcrel_offset */
949
950 /* 64-bit PC relative relocation to the symbol's procedure linkage
951 table. */
952 /* FIXME: R_PPC64_PLTREL64 not supported. */
953 HOWTO (R_PPC64_PLTREL64, /* type */
954 0, /* rightshift */
955 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
956 64, /* bitsize */
957 TRUE, /* pc_relative */
958 0, /* bitpos */
959 complain_overflow_dont, /* complain_on_overflow */
960 ppc64_elf_unhandled_reloc, /* special_function */
961 "R_PPC64_PLTREL64", /* name */
962 FALSE, /* partial_inplace */
963 0, /* src_mask */
964 ONES (64), /* dst_mask */
965 TRUE), /* pcrel_offset */
966
967 /* 16 bit TOC-relative relocation. */
968
969 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
970 HOWTO (R_PPC64_TOC16, /* type */
971 0, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
974 FALSE, /* pc_relative */
975 0, /* bitpos */
976 complain_overflow_signed, /* complain_on_overflow */
977 ppc64_elf_toc_reloc, /* special_function */
978 "R_PPC64_TOC16", /* name */
979 FALSE, /* partial_inplace */
980 0, /* src_mask */
981 0xffff, /* dst_mask */
982 FALSE), /* pcrel_offset */
983
984 /* 16 bit TOC-relative relocation without overflow. */
985
986 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
987 HOWTO (R_PPC64_TOC16_LO, /* type */
988 0, /* rightshift */
989 1, /* size (0 = byte, 1 = short, 2 = long) */
990 16, /* bitsize */
991 FALSE, /* pc_relative */
992 0, /* bitpos */
993 complain_overflow_dont, /* complain_on_overflow */
994 ppc64_elf_toc_reloc, /* special_function */
995 "R_PPC64_TOC16_LO", /* name */
996 FALSE, /* partial_inplace */
997 0, /* src_mask */
998 0xffff, /* dst_mask */
999 FALSE), /* pcrel_offset */
1000
1001 /* 16 bit TOC-relative relocation, high 16 bits. */
1002
1003 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1004 HOWTO (R_PPC64_TOC16_HI, /* type */
1005 16, /* rightshift */
1006 1, /* size (0 = byte, 1 = short, 2 = long) */
1007 16, /* bitsize */
1008 FALSE, /* pc_relative */
1009 0, /* bitpos */
1010 complain_overflow_signed, /* complain_on_overflow */
1011 ppc64_elf_toc_reloc, /* special_function */
1012 "R_PPC64_TOC16_HI", /* name */
1013 FALSE, /* partial_inplace */
1014 0, /* src_mask */
1015 0xffff, /* dst_mask */
1016 FALSE), /* pcrel_offset */
1017
1018 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1019 contents of the low 16 bits, treated as a signed number, is
1020 negative. */
1021
1022 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1023 HOWTO (R_PPC64_TOC16_HA, /* type */
1024 16, /* rightshift */
1025 1, /* size (0 = byte, 1 = short, 2 = long) */
1026 16, /* bitsize */
1027 FALSE, /* pc_relative */
1028 0, /* bitpos */
1029 complain_overflow_signed, /* complain_on_overflow */
1030 ppc64_elf_toc_ha_reloc, /* special_function */
1031 "R_PPC64_TOC16_HA", /* name */
1032 FALSE, /* partial_inplace */
1033 0, /* src_mask */
1034 0xffff, /* dst_mask */
1035 FALSE), /* pcrel_offset */
1036
1037 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1038
1039 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1040 HOWTO (R_PPC64_TOC, /* type */
1041 0, /* rightshift */
1042 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1043 64, /* bitsize */
1044 FALSE, /* pc_relative */
1045 0, /* bitpos */
1046 complain_overflow_dont, /* complain_on_overflow */
1047 ppc64_elf_toc64_reloc, /* special_function */
1048 "R_PPC64_TOC", /* name */
1049 FALSE, /* partial_inplace */
1050 0, /* src_mask */
1051 ONES (64), /* dst_mask */
1052 FALSE), /* pcrel_offset */
1053
1054 /* Like R_PPC64_GOT16, but also informs the link editor that the
1055 value to relocate may (!) refer to a PLT entry which the link
1056 editor (a) may replace with the symbol value. If the link editor
1057 is unable to fully resolve the symbol, it may (b) create a PLT
1058 entry and store the address to the new PLT entry in the GOT.
1059 This permits lazy resolution of function symbols at run time.
1060 The link editor may also skip all of this and just (c) emit a
1061 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1062 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1063 HOWTO (R_PPC64_PLTGOT16, /* type */
1064 0, /* rightshift */
1065 1, /* size (0 = byte, 1 = short, 2 = long) */
1066 16, /* bitsize */
1067 FALSE, /* pc_relative */
1068 0, /* bitpos */
1069 complain_overflow_signed, /* complain_on_overflow */
1070 ppc64_elf_unhandled_reloc, /* special_function */
1071 "R_PPC64_PLTGOT16", /* name */
1072 FALSE, /* partial_inplace */
1073 0, /* src_mask */
1074 0xffff, /* dst_mask */
1075 FALSE), /* pcrel_offset */
1076
1077 /* Like R_PPC64_PLTGOT16, but without overflow. */
1078 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1079 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1080 0, /* rightshift */
1081 1, /* size (0 = byte, 1 = short, 2 = long) */
1082 16, /* bitsize */
1083 FALSE, /* pc_relative */
1084 0, /* bitpos */
1085 complain_overflow_dont, /* complain_on_overflow */
1086 ppc64_elf_unhandled_reloc, /* special_function */
1087 "R_PPC64_PLTGOT16_LO", /* name */
1088 FALSE, /* partial_inplace */
1089 0, /* src_mask */
1090 0xffff, /* dst_mask */
1091 FALSE), /* pcrel_offset */
1092
1093 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1094 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1095 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1096 16, /* rightshift */
1097 1, /* size (0 = byte, 1 = short, 2 = long) */
1098 16, /* bitsize */
1099 FALSE, /* pc_relative */
1100 0, /* bitpos */
1101 complain_overflow_signed, /* complain_on_overflow */
1102 ppc64_elf_unhandled_reloc, /* special_function */
1103 "R_PPC64_PLTGOT16_HI", /* name */
1104 FALSE, /* partial_inplace */
1105 0, /* src_mask */
1106 0xffff, /* dst_mask */
1107 FALSE), /* pcrel_offset */
1108
1109 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1110 1 if the contents of the low 16 bits, treated as a signed number,
1111 is negative. */
1112 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1113 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1114 16, /* rightshift */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1116 16, /* bitsize */
1117 FALSE, /* pc_relative */
1118 0, /* bitpos */
1119 complain_overflow_signed, /* complain_on_overflow */
1120 ppc64_elf_unhandled_reloc, /* special_function */
1121 "R_PPC64_PLTGOT16_HA", /* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xffff, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1126
1127 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1128 HOWTO (R_PPC64_ADDR16_DS, /* type */
1129 0, /* rightshift */
1130 1, /* size (0 = byte, 1 = short, 2 = long) */
1131 16, /* bitsize */
1132 FALSE, /* pc_relative */
1133 0, /* bitpos */
1134 complain_overflow_signed, /* complain_on_overflow */
1135 bfd_elf_generic_reloc, /* special_function */
1136 "R_PPC64_ADDR16_DS", /* name */
1137 FALSE, /* partial_inplace */
1138 0, /* src_mask */
1139 0xfffc, /* dst_mask */
1140 FALSE), /* pcrel_offset */
1141
1142 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1143 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1144 0, /* rightshift */
1145 1, /* size (0 = byte, 1 = short, 2 = long) */
1146 16, /* bitsize */
1147 FALSE, /* pc_relative */
1148 0, /* bitpos */
1149 complain_overflow_dont,/* complain_on_overflow */
1150 bfd_elf_generic_reloc, /* special_function */
1151 "R_PPC64_ADDR16_LO_DS",/* name */
1152 FALSE, /* partial_inplace */
1153 0, /* src_mask */
1154 0xfffc, /* dst_mask */
1155 FALSE), /* pcrel_offset */
1156
1157 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1158 HOWTO (R_PPC64_GOT16_DS, /* type */
1159 0, /* rightshift */
1160 1, /* size (0 = byte, 1 = short, 2 = long) */
1161 16, /* bitsize */
1162 FALSE, /* pc_relative */
1163 0, /* bitpos */
1164 complain_overflow_signed, /* complain_on_overflow */
1165 ppc64_elf_unhandled_reloc, /* special_function */
1166 "R_PPC64_GOT16_DS", /* name */
1167 FALSE, /* partial_inplace */
1168 0, /* src_mask */
1169 0xfffc, /* dst_mask */
1170 FALSE), /* pcrel_offset */
1171
1172 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1173 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1174 0, /* rightshift */
1175 1, /* size (0 = byte, 1 = short, 2 = long) */
1176 16, /* bitsize */
1177 FALSE, /* pc_relative */
1178 0, /* bitpos */
1179 complain_overflow_dont, /* complain_on_overflow */
1180 ppc64_elf_unhandled_reloc, /* special_function */
1181 "R_PPC64_GOT16_LO_DS", /* name */
1182 FALSE, /* partial_inplace */
1183 0, /* src_mask */
1184 0xfffc, /* dst_mask */
1185 FALSE), /* pcrel_offset */
1186
1187 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1188 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1189 0, /* rightshift */
1190 1, /* size (0 = byte, 1 = short, 2 = long) */
1191 16, /* bitsize */
1192 FALSE, /* pc_relative */
1193 0, /* bitpos */
1194 complain_overflow_dont, /* complain_on_overflow */
1195 ppc64_elf_unhandled_reloc, /* special_function */
1196 "R_PPC64_PLT16_LO_DS", /* name */
1197 FALSE, /* partial_inplace */
1198 0, /* src_mask */
1199 0xfffc, /* dst_mask */
1200 FALSE), /* pcrel_offset */
1201
1202 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1203 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1204 0, /* rightshift */
1205 1, /* size (0 = byte, 1 = short, 2 = long) */
1206 16, /* bitsize */
1207 FALSE, /* pc_relative */
1208 0, /* bitpos */
1209 complain_overflow_signed, /* complain_on_overflow */
1210 ppc64_elf_sectoff_reloc, /* special_function */
1211 "R_PPC64_SECTOFF_DS", /* name */
1212 FALSE, /* partial_inplace */
1213 0, /* src_mask */
1214 0xfffc, /* dst_mask */
1215 FALSE), /* pcrel_offset */
1216
1217 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1218 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1219 0, /* rightshift */
1220 1, /* size (0 = byte, 1 = short, 2 = long) */
1221 16, /* bitsize */
1222 FALSE, /* pc_relative */
1223 0, /* bitpos */
1224 complain_overflow_dont, /* complain_on_overflow */
1225 ppc64_elf_sectoff_reloc, /* special_function */
1226 "R_PPC64_SECTOFF_LO_DS",/* name */
1227 FALSE, /* partial_inplace */
1228 0, /* src_mask */
1229 0xfffc, /* dst_mask */
1230 FALSE), /* pcrel_offset */
1231
1232 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1233 HOWTO (R_PPC64_TOC16_DS, /* type */
1234 0, /* rightshift */
1235 1, /* size (0 = byte, 1 = short, 2 = long) */
1236 16, /* bitsize */
1237 FALSE, /* pc_relative */
1238 0, /* bitpos */
1239 complain_overflow_signed, /* complain_on_overflow */
1240 ppc64_elf_toc_reloc, /* special_function */
1241 "R_PPC64_TOC16_DS", /* name */
1242 FALSE, /* partial_inplace */
1243 0, /* src_mask */
1244 0xfffc, /* dst_mask */
1245 FALSE), /* pcrel_offset */
1246
1247 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1248 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1249 0, /* rightshift */
1250 1, /* size (0 = byte, 1 = short, 2 = long) */
1251 16, /* bitsize */
1252 FALSE, /* pc_relative */
1253 0, /* bitpos */
1254 complain_overflow_dont, /* complain_on_overflow */
1255 ppc64_elf_toc_reloc, /* special_function */
1256 "R_PPC64_TOC16_LO_DS", /* name */
1257 FALSE, /* partial_inplace */
1258 0, /* src_mask */
1259 0xfffc, /* dst_mask */
1260 FALSE), /* pcrel_offset */
1261
1262 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1263 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1264 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1265 0, /* rightshift */
1266 1, /* size (0 = byte, 1 = short, 2 = long) */
1267 16, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_signed, /* complain_on_overflow */
1271 ppc64_elf_unhandled_reloc, /* special_function */
1272 "R_PPC64_PLTGOT16_DS", /* name */
1273 FALSE, /* partial_inplace */
1274 0, /* src_mask */
1275 0xfffc, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1279 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1280 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1281 0, /* rightshift */
1282 1, /* size (0 = byte, 1 = short, 2 = long) */
1283 16, /* bitsize */
1284 FALSE, /* pc_relative */
1285 0, /* bitpos */
1286 complain_overflow_dont, /* complain_on_overflow */
1287 ppc64_elf_unhandled_reloc, /* special_function */
1288 "R_PPC64_PLTGOT16_LO_DS",/* name */
1289 FALSE, /* partial_inplace */
1290 0, /* src_mask */
1291 0xfffc, /* dst_mask */
1292 FALSE), /* pcrel_offset */
1293
1294 /* Marker relocs for TLS. */
1295 HOWTO (R_PPC64_TLS,
1296 0, /* rightshift */
1297 2, /* size (0 = byte, 1 = short, 2 = long) */
1298 32, /* bitsize */
1299 FALSE, /* pc_relative */
1300 0, /* bitpos */
1301 complain_overflow_dont, /* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_PPC64_TLS", /* name */
1304 FALSE, /* partial_inplace */
1305 0, /* src_mask */
1306 0, /* dst_mask */
1307 FALSE), /* pcrel_offset */
1308
1309 HOWTO (R_PPC64_TLSGD,
1310 0, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 32, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_dont, /* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_PPC64_TLSGD", /* name */
1318 FALSE, /* partial_inplace */
1319 0, /* src_mask */
1320 0, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
1323 HOWTO (R_PPC64_TLSLD,
1324 0, /* rightshift */
1325 2, /* size (0 = byte, 1 = short, 2 = long) */
1326 32, /* bitsize */
1327 FALSE, /* pc_relative */
1328 0, /* bitpos */
1329 complain_overflow_dont, /* complain_on_overflow */
1330 bfd_elf_generic_reloc, /* special_function */
1331 "R_PPC64_TLSLD", /* name */
1332 FALSE, /* partial_inplace */
1333 0, /* src_mask */
1334 0, /* dst_mask */
1335 FALSE), /* pcrel_offset */
1336
1337 HOWTO (R_PPC64_TOCSAVE,
1338 0, /* rightshift */
1339 2, /* size (0 = byte, 1 = short, 2 = long) */
1340 32, /* bitsize */
1341 FALSE, /* pc_relative */
1342 0, /* bitpos */
1343 complain_overflow_dont, /* complain_on_overflow */
1344 bfd_elf_generic_reloc, /* special_function */
1345 "R_PPC64_TOCSAVE", /* name */
1346 FALSE, /* partial_inplace */
1347 0, /* src_mask */
1348 0, /* dst_mask */
1349 FALSE), /* pcrel_offset */
1350
1351 /* Computes the load module index of the load module that contains the
1352 definition of its TLS sym. */
1353 HOWTO (R_PPC64_DTPMOD64,
1354 0, /* rightshift */
1355 4, /* size (0 = byte, 1 = short, 2 = long) */
1356 64, /* bitsize */
1357 FALSE, /* pc_relative */
1358 0, /* bitpos */
1359 complain_overflow_dont, /* complain_on_overflow */
1360 ppc64_elf_unhandled_reloc, /* special_function */
1361 "R_PPC64_DTPMOD64", /* name */
1362 FALSE, /* partial_inplace */
1363 0, /* src_mask */
1364 ONES (64), /* dst_mask */
1365 FALSE), /* pcrel_offset */
1366
1367 /* Computes a dtv-relative displacement, the difference between the value
1368 of sym+add and the base address of the thread-local storage block that
1369 contains the definition of sym, minus 0x8000. */
1370 HOWTO (R_PPC64_DTPREL64,
1371 0, /* rightshift */
1372 4, /* size (0 = byte, 1 = short, 2 = long) */
1373 64, /* bitsize */
1374 FALSE, /* pc_relative */
1375 0, /* bitpos */
1376 complain_overflow_dont, /* complain_on_overflow */
1377 ppc64_elf_unhandled_reloc, /* special_function */
1378 "R_PPC64_DTPREL64", /* name */
1379 FALSE, /* partial_inplace */
1380 0, /* src_mask */
1381 ONES (64), /* dst_mask */
1382 FALSE), /* pcrel_offset */
1383
1384 /* A 16 bit dtprel reloc. */
1385 HOWTO (R_PPC64_DTPREL16,
1386 0, /* rightshift */
1387 1, /* size (0 = byte, 1 = short, 2 = long) */
1388 16, /* bitsize */
1389 FALSE, /* pc_relative */
1390 0, /* bitpos */
1391 complain_overflow_signed, /* complain_on_overflow */
1392 ppc64_elf_unhandled_reloc, /* special_function */
1393 "R_PPC64_DTPREL16", /* name */
1394 FALSE, /* partial_inplace */
1395 0, /* src_mask */
1396 0xffff, /* dst_mask */
1397 FALSE), /* pcrel_offset */
1398
1399 /* Like DTPREL16, but no overflow. */
1400 HOWTO (R_PPC64_DTPREL16_LO,
1401 0, /* rightshift */
1402 1, /* size (0 = byte, 1 = short, 2 = long) */
1403 16, /* bitsize */
1404 FALSE, /* pc_relative */
1405 0, /* bitpos */
1406 complain_overflow_dont, /* complain_on_overflow */
1407 ppc64_elf_unhandled_reloc, /* special_function */
1408 "R_PPC64_DTPREL16_LO", /* name */
1409 FALSE, /* partial_inplace */
1410 0, /* src_mask */
1411 0xffff, /* dst_mask */
1412 FALSE), /* pcrel_offset */
1413
1414 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1415 HOWTO (R_PPC64_DTPREL16_HI,
1416 16, /* rightshift */
1417 1, /* size (0 = byte, 1 = short, 2 = long) */
1418 16, /* bitsize */
1419 FALSE, /* pc_relative */
1420 0, /* bitpos */
1421 complain_overflow_signed, /* complain_on_overflow */
1422 ppc64_elf_unhandled_reloc, /* special_function */
1423 "R_PPC64_DTPREL16_HI", /* name */
1424 FALSE, /* partial_inplace */
1425 0, /* src_mask */
1426 0xffff, /* dst_mask */
1427 FALSE), /* pcrel_offset */
1428
1429 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1430 HOWTO (R_PPC64_DTPREL16_HA,
1431 16, /* rightshift */
1432 1, /* size (0 = byte, 1 = short, 2 = long) */
1433 16, /* bitsize */
1434 FALSE, /* pc_relative */
1435 0, /* bitpos */
1436 complain_overflow_signed, /* complain_on_overflow */
1437 ppc64_elf_unhandled_reloc, /* special_function */
1438 "R_PPC64_DTPREL16_HA", /* name */
1439 FALSE, /* partial_inplace */
1440 0, /* src_mask */
1441 0xffff, /* dst_mask */
1442 FALSE), /* pcrel_offset */
1443
1444 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1445 HOWTO (R_PPC64_DTPREL16_HIGHER,
1446 32, /* rightshift */
1447 1, /* size (0 = byte, 1 = short, 2 = long) */
1448 16, /* bitsize */
1449 FALSE, /* pc_relative */
1450 0, /* bitpos */
1451 complain_overflow_dont, /* complain_on_overflow */
1452 ppc64_elf_unhandled_reloc, /* special_function */
1453 "R_PPC64_DTPREL16_HIGHER", /* name */
1454 FALSE, /* partial_inplace */
1455 0, /* src_mask */
1456 0xffff, /* dst_mask */
1457 FALSE), /* pcrel_offset */
1458
1459 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1460 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1461 32, /* rightshift */
1462 1, /* size (0 = byte, 1 = short, 2 = long) */
1463 16, /* bitsize */
1464 FALSE, /* pc_relative */
1465 0, /* bitpos */
1466 complain_overflow_dont, /* complain_on_overflow */
1467 ppc64_elf_unhandled_reloc, /* special_function */
1468 "R_PPC64_DTPREL16_HIGHERA", /* name */
1469 FALSE, /* partial_inplace */
1470 0, /* src_mask */
1471 0xffff, /* dst_mask */
1472 FALSE), /* pcrel_offset */
1473
1474 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1475 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1476 48, /* rightshift */
1477 1, /* size (0 = byte, 1 = short, 2 = long) */
1478 16, /* bitsize */
1479 FALSE, /* pc_relative */
1480 0, /* bitpos */
1481 complain_overflow_dont, /* complain_on_overflow */
1482 ppc64_elf_unhandled_reloc, /* special_function */
1483 "R_PPC64_DTPREL16_HIGHEST", /* name */
1484 FALSE, /* partial_inplace */
1485 0, /* src_mask */
1486 0xffff, /* dst_mask */
1487 FALSE), /* pcrel_offset */
1488
1489 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1490 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1491 48, /* rightshift */
1492 1, /* size (0 = byte, 1 = short, 2 = long) */
1493 16, /* bitsize */
1494 FALSE, /* pc_relative */
1495 0, /* bitpos */
1496 complain_overflow_dont, /* complain_on_overflow */
1497 ppc64_elf_unhandled_reloc, /* special_function */
1498 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1499 FALSE, /* partial_inplace */
1500 0, /* src_mask */
1501 0xffff, /* dst_mask */
1502 FALSE), /* pcrel_offset */
1503
1504 /* Like DTPREL16, but for insns with a DS field. */
1505 HOWTO (R_PPC64_DTPREL16_DS,
1506 0, /* rightshift */
1507 1, /* size (0 = byte, 1 = short, 2 = long) */
1508 16, /* bitsize */
1509 FALSE, /* pc_relative */
1510 0, /* bitpos */
1511 complain_overflow_signed, /* complain_on_overflow */
1512 ppc64_elf_unhandled_reloc, /* special_function */
1513 "R_PPC64_DTPREL16_DS", /* name */
1514 FALSE, /* partial_inplace */
1515 0, /* src_mask */
1516 0xfffc, /* dst_mask */
1517 FALSE), /* pcrel_offset */
1518
1519 /* Like DTPREL16_DS, but no overflow. */
1520 HOWTO (R_PPC64_DTPREL16_LO_DS,
1521 0, /* rightshift */
1522 1, /* size (0 = byte, 1 = short, 2 = long) */
1523 16, /* bitsize */
1524 FALSE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_dont, /* complain_on_overflow */
1527 ppc64_elf_unhandled_reloc, /* special_function */
1528 "R_PPC64_DTPREL16_LO_DS", /* name */
1529 FALSE, /* partial_inplace */
1530 0, /* src_mask */
1531 0xfffc, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533
1534 /* Computes a tp-relative displacement, the difference between the value of
1535 sym+add and the value of the thread pointer (r13). */
1536 HOWTO (R_PPC64_TPREL64,
1537 0, /* rightshift */
1538 4, /* size (0 = byte, 1 = short, 2 = long) */
1539 64, /* bitsize */
1540 FALSE, /* pc_relative */
1541 0, /* bitpos */
1542 complain_overflow_dont, /* complain_on_overflow */
1543 ppc64_elf_unhandled_reloc, /* special_function */
1544 "R_PPC64_TPREL64", /* name */
1545 FALSE, /* partial_inplace */
1546 0, /* src_mask */
1547 ONES (64), /* dst_mask */
1548 FALSE), /* pcrel_offset */
1549
1550 /* A 16 bit tprel reloc. */
1551 HOWTO (R_PPC64_TPREL16,
1552 0, /* rightshift */
1553 1, /* size (0 = byte, 1 = short, 2 = long) */
1554 16, /* bitsize */
1555 FALSE, /* pc_relative */
1556 0, /* bitpos */
1557 complain_overflow_signed, /* complain_on_overflow */
1558 ppc64_elf_unhandled_reloc, /* special_function */
1559 "R_PPC64_TPREL16", /* name */
1560 FALSE, /* partial_inplace */
1561 0, /* src_mask */
1562 0xffff, /* dst_mask */
1563 FALSE), /* pcrel_offset */
1564
1565 /* Like TPREL16, but no overflow. */
1566 HOWTO (R_PPC64_TPREL16_LO,
1567 0, /* rightshift */
1568 1, /* size (0 = byte, 1 = short, 2 = long) */
1569 16, /* bitsize */
1570 FALSE, /* pc_relative */
1571 0, /* bitpos */
1572 complain_overflow_dont, /* complain_on_overflow */
1573 ppc64_elf_unhandled_reloc, /* special_function */
1574 "R_PPC64_TPREL16_LO", /* name */
1575 FALSE, /* partial_inplace */
1576 0, /* src_mask */
1577 0xffff, /* dst_mask */
1578 FALSE), /* pcrel_offset */
1579
1580 /* Like TPREL16_LO, but next higher group of 16 bits. */
1581 HOWTO (R_PPC64_TPREL16_HI,
1582 16, /* rightshift */
1583 1, /* size (0 = byte, 1 = short, 2 = long) */
1584 16, /* bitsize */
1585 FALSE, /* pc_relative */
1586 0, /* bitpos */
1587 complain_overflow_signed, /* complain_on_overflow */
1588 ppc64_elf_unhandled_reloc, /* special_function */
1589 "R_PPC64_TPREL16_HI", /* name */
1590 FALSE, /* partial_inplace */
1591 0, /* src_mask */
1592 0xffff, /* dst_mask */
1593 FALSE), /* pcrel_offset */
1594
1595 /* Like TPREL16_HI, but adjust for low 16 bits. */
1596 HOWTO (R_PPC64_TPREL16_HA,
1597 16, /* rightshift */
1598 1, /* size (0 = byte, 1 = short, 2 = long) */
1599 16, /* bitsize */
1600 FALSE, /* pc_relative */
1601 0, /* bitpos */
1602 complain_overflow_signed, /* complain_on_overflow */
1603 ppc64_elf_unhandled_reloc, /* special_function */
1604 "R_PPC64_TPREL16_HA", /* name */
1605 FALSE, /* partial_inplace */
1606 0, /* src_mask */
1607 0xffff, /* dst_mask */
1608 FALSE), /* pcrel_offset */
1609
1610 /* Like TPREL16_HI, but next higher group of 16 bits. */
1611 HOWTO (R_PPC64_TPREL16_HIGHER,
1612 32, /* rightshift */
1613 1, /* size (0 = byte, 1 = short, 2 = long) */
1614 16, /* bitsize */
1615 FALSE, /* pc_relative */
1616 0, /* bitpos */
1617 complain_overflow_dont, /* complain_on_overflow */
1618 ppc64_elf_unhandled_reloc, /* special_function */
1619 "R_PPC64_TPREL16_HIGHER", /* name */
1620 FALSE, /* partial_inplace */
1621 0, /* src_mask */
1622 0xffff, /* dst_mask */
1623 FALSE), /* pcrel_offset */
1624
1625 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1626 HOWTO (R_PPC64_TPREL16_HIGHERA,
1627 32, /* rightshift */
1628 1, /* size (0 = byte, 1 = short, 2 = long) */
1629 16, /* bitsize */
1630 FALSE, /* pc_relative */
1631 0, /* bitpos */
1632 complain_overflow_dont, /* complain_on_overflow */
1633 ppc64_elf_unhandled_reloc, /* special_function */
1634 "R_PPC64_TPREL16_HIGHERA", /* name */
1635 FALSE, /* partial_inplace */
1636 0, /* src_mask */
1637 0xffff, /* dst_mask */
1638 FALSE), /* pcrel_offset */
1639
1640 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1641 HOWTO (R_PPC64_TPREL16_HIGHEST,
1642 48, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 16, /* bitsize */
1645 FALSE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_dont, /* complain_on_overflow */
1648 ppc64_elf_unhandled_reloc, /* special_function */
1649 "R_PPC64_TPREL16_HIGHEST", /* name */
1650 FALSE, /* partial_inplace */
1651 0, /* src_mask */
1652 0xffff, /* dst_mask */
1653 FALSE), /* pcrel_offset */
1654
1655 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1656 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1657 48, /* rightshift */
1658 1, /* size (0 = byte, 1 = short, 2 = long) */
1659 16, /* bitsize */
1660 FALSE, /* pc_relative */
1661 0, /* bitpos */
1662 complain_overflow_dont, /* complain_on_overflow */
1663 ppc64_elf_unhandled_reloc, /* special_function */
1664 "R_PPC64_TPREL16_HIGHESTA", /* name */
1665 FALSE, /* partial_inplace */
1666 0, /* src_mask */
1667 0xffff, /* dst_mask */
1668 FALSE), /* pcrel_offset */
1669
1670 /* Like TPREL16, but for insns with a DS field. */
1671 HOWTO (R_PPC64_TPREL16_DS,
1672 0, /* rightshift */
1673 1, /* size (0 = byte, 1 = short, 2 = long) */
1674 16, /* bitsize */
1675 FALSE, /* pc_relative */
1676 0, /* bitpos */
1677 complain_overflow_signed, /* complain_on_overflow */
1678 ppc64_elf_unhandled_reloc, /* special_function */
1679 "R_PPC64_TPREL16_DS", /* name */
1680 FALSE, /* partial_inplace */
1681 0, /* src_mask */
1682 0xfffc, /* dst_mask */
1683 FALSE), /* pcrel_offset */
1684
1685 /* Like TPREL16_DS, but no overflow. */
1686 HOWTO (R_PPC64_TPREL16_LO_DS,
1687 0, /* rightshift */
1688 1, /* size (0 = byte, 1 = short, 2 = long) */
1689 16, /* bitsize */
1690 FALSE, /* pc_relative */
1691 0, /* bitpos */
1692 complain_overflow_dont, /* complain_on_overflow */
1693 ppc64_elf_unhandled_reloc, /* special_function */
1694 "R_PPC64_TPREL16_LO_DS", /* name */
1695 FALSE, /* partial_inplace */
1696 0, /* src_mask */
1697 0xfffc, /* dst_mask */
1698 FALSE), /* pcrel_offset */
1699
1700 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1701 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1702 to the first entry relative to the TOC base (r2). */
1703 HOWTO (R_PPC64_GOT_TLSGD16,
1704 0, /* rightshift */
1705 1, /* size (0 = byte, 1 = short, 2 = long) */
1706 16, /* bitsize */
1707 FALSE, /* pc_relative */
1708 0, /* bitpos */
1709 complain_overflow_signed, /* complain_on_overflow */
1710 ppc64_elf_unhandled_reloc, /* special_function */
1711 "R_PPC64_GOT_TLSGD16", /* name */
1712 FALSE, /* partial_inplace */
1713 0, /* src_mask */
1714 0xffff, /* dst_mask */
1715 FALSE), /* pcrel_offset */
1716
1717 /* Like GOT_TLSGD16, but no overflow. */
1718 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1719 0, /* rightshift */
1720 1, /* size (0 = byte, 1 = short, 2 = long) */
1721 16, /* bitsize */
1722 FALSE, /* pc_relative */
1723 0, /* bitpos */
1724 complain_overflow_dont, /* complain_on_overflow */
1725 ppc64_elf_unhandled_reloc, /* special_function */
1726 "R_PPC64_GOT_TLSGD16_LO", /* name */
1727 FALSE, /* partial_inplace */
1728 0, /* src_mask */
1729 0xffff, /* dst_mask */
1730 FALSE), /* pcrel_offset */
1731
1732 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1733 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1734 16, /* rightshift */
1735 1, /* size (0 = byte, 1 = short, 2 = long) */
1736 16, /* bitsize */
1737 FALSE, /* pc_relative */
1738 0, /* bitpos */
1739 complain_overflow_signed, /* complain_on_overflow */
1740 ppc64_elf_unhandled_reloc, /* special_function */
1741 "R_PPC64_GOT_TLSGD16_HI", /* name */
1742 FALSE, /* partial_inplace */
1743 0, /* src_mask */
1744 0xffff, /* dst_mask */
1745 FALSE), /* pcrel_offset */
1746
1747 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1748 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1749 16, /* rightshift */
1750 1, /* size (0 = byte, 1 = short, 2 = long) */
1751 16, /* bitsize */
1752 FALSE, /* pc_relative */
1753 0, /* bitpos */
1754 complain_overflow_signed, /* complain_on_overflow */
1755 ppc64_elf_unhandled_reloc, /* special_function */
1756 "R_PPC64_GOT_TLSGD16_HA", /* name */
1757 FALSE, /* partial_inplace */
1758 0, /* src_mask */
1759 0xffff, /* dst_mask */
1760 FALSE), /* pcrel_offset */
1761
1762 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1763 with values (sym+add)@dtpmod and zero, and computes the offset to the
1764 first entry relative to the TOC base (r2). */
1765 HOWTO (R_PPC64_GOT_TLSLD16,
1766 0, /* rightshift */
1767 1, /* size (0 = byte, 1 = short, 2 = long) */
1768 16, /* bitsize */
1769 FALSE, /* pc_relative */
1770 0, /* bitpos */
1771 complain_overflow_signed, /* complain_on_overflow */
1772 ppc64_elf_unhandled_reloc, /* special_function */
1773 "R_PPC64_GOT_TLSLD16", /* name */
1774 FALSE, /* partial_inplace */
1775 0, /* src_mask */
1776 0xffff, /* dst_mask */
1777 FALSE), /* pcrel_offset */
1778
1779 /* Like GOT_TLSLD16, but no overflow. */
1780 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1781 0, /* rightshift */
1782 1, /* size (0 = byte, 1 = short, 2 = long) */
1783 16, /* bitsize */
1784 FALSE, /* pc_relative */
1785 0, /* bitpos */
1786 complain_overflow_dont, /* complain_on_overflow */
1787 ppc64_elf_unhandled_reloc, /* special_function */
1788 "R_PPC64_GOT_TLSLD16_LO", /* name */
1789 FALSE, /* partial_inplace */
1790 0, /* src_mask */
1791 0xffff, /* dst_mask */
1792 FALSE), /* pcrel_offset */
1793
1794 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1795 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1796 16, /* rightshift */
1797 1, /* size (0 = byte, 1 = short, 2 = long) */
1798 16, /* bitsize */
1799 FALSE, /* pc_relative */
1800 0, /* bitpos */
1801 complain_overflow_signed, /* complain_on_overflow */
1802 ppc64_elf_unhandled_reloc, /* special_function */
1803 "R_PPC64_GOT_TLSLD16_HI", /* name */
1804 FALSE, /* partial_inplace */
1805 0, /* src_mask */
1806 0xffff, /* dst_mask */
1807 FALSE), /* pcrel_offset */
1808
1809 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1810 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1811 16, /* rightshift */
1812 1, /* size (0 = byte, 1 = short, 2 = long) */
1813 16, /* bitsize */
1814 FALSE, /* pc_relative */
1815 0, /* bitpos */
1816 complain_overflow_signed, /* complain_on_overflow */
1817 ppc64_elf_unhandled_reloc, /* special_function */
1818 "R_PPC64_GOT_TLSLD16_HA", /* name */
1819 FALSE, /* partial_inplace */
1820 0, /* src_mask */
1821 0xffff, /* dst_mask */
1822 FALSE), /* pcrel_offset */
1823
1824 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1825 the offset to the entry relative to the TOC base (r2). */
1826 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1827 0, /* rightshift */
1828 1, /* size (0 = byte, 1 = short, 2 = long) */
1829 16, /* bitsize */
1830 FALSE, /* pc_relative */
1831 0, /* bitpos */
1832 complain_overflow_signed, /* complain_on_overflow */
1833 ppc64_elf_unhandled_reloc, /* special_function */
1834 "R_PPC64_GOT_DTPREL16_DS", /* name */
1835 FALSE, /* partial_inplace */
1836 0, /* src_mask */
1837 0xfffc, /* dst_mask */
1838 FALSE), /* pcrel_offset */
1839
1840 /* Like GOT_DTPREL16_DS, but no overflow. */
1841 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1842 0, /* rightshift */
1843 1, /* size (0 = byte, 1 = short, 2 = long) */
1844 16, /* bitsize */
1845 FALSE, /* pc_relative */
1846 0, /* bitpos */
1847 complain_overflow_dont, /* complain_on_overflow */
1848 ppc64_elf_unhandled_reloc, /* special_function */
1849 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1850 FALSE, /* partial_inplace */
1851 0, /* src_mask */
1852 0xfffc, /* dst_mask */
1853 FALSE), /* pcrel_offset */
1854
1855 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1856 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1857 16, /* rightshift */
1858 1, /* size (0 = byte, 1 = short, 2 = long) */
1859 16, /* bitsize */
1860 FALSE, /* pc_relative */
1861 0, /* bitpos */
1862 complain_overflow_signed, /* complain_on_overflow */
1863 ppc64_elf_unhandled_reloc, /* special_function */
1864 "R_PPC64_GOT_DTPREL16_HI", /* name */
1865 FALSE, /* partial_inplace */
1866 0, /* src_mask */
1867 0xffff, /* dst_mask */
1868 FALSE), /* pcrel_offset */
1869
1870 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1871 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1872 16, /* rightshift */
1873 1, /* size (0 = byte, 1 = short, 2 = long) */
1874 16, /* bitsize */
1875 FALSE, /* pc_relative */
1876 0, /* bitpos */
1877 complain_overflow_signed, /* complain_on_overflow */
1878 ppc64_elf_unhandled_reloc, /* special_function */
1879 "R_PPC64_GOT_DTPREL16_HA", /* name */
1880 FALSE, /* partial_inplace */
1881 0, /* src_mask */
1882 0xffff, /* dst_mask */
1883 FALSE), /* pcrel_offset */
1884
1885 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1886 offset to the entry relative to the TOC base (r2). */
1887 HOWTO (R_PPC64_GOT_TPREL16_DS,
1888 0, /* rightshift */
1889 1, /* size (0 = byte, 1 = short, 2 = long) */
1890 16, /* bitsize */
1891 FALSE, /* pc_relative */
1892 0, /* bitpos */
1893 complain_overflow_signed, /* complain_on_overflow */
1894 ppc64_elf_unhandled_reloc, /* special_function */
1895 "R_PPC64_GOT_TPREL16_DS", /* name */
1896 FALSE, /* partial_inplace */
1897 0, /* src_mask */
1898 0xfffc, /* dst_mask */
1899 FALSE), /* pcrel_offset */
1900
1901 /* Like GOT_TPREL16_DS, but no overflow. */
1902 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1903 0, /* rightshift */
1904 1, /* size (0 = byte, 1 = short, 2 = long) */
1905 16, /* bitsize */
1906 FALSE, /* pc_relative */
1907 0, /* bitpos */
1908 complain_overflow_dont, /* complain_on_overflow */
1909 ppc64_elf_unhandled_reloc, /* special_function */
1910 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1911 FALSE, /* partial_inplace */
1912 0, /* src_mask */
1913 0xfffc, /* dst_mask */
1914 FALSE), /* pcrel_offset */
1915
1916 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1917 HOWTO (R_PPC64_GOT_TPREL16_HI,
1918 16, /* rightshift */
1919 1, /* size (0 = byte, 1 = short, 2 = long) */
1920 16, /* bitsize */
1921 FALSE, /* pc_relative */
1922 0, /* bitpos */
1923 complain_overflow_signed, /* complain_on_overflow */
1924 ppc64_elf_unhandled_reloc, /* special_function */
1925 "R_PPC64_GOT_TPREL16_HI", /* name */
1926 FALSE, /* partial_inplace */
1927 0, /* src_mask */
1928 0xffff, /* dst_mask */
1929 FALSE), /* pcrel_offset */
1930
1931 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1932 HOWTO (R_PPC64_GOT_TPREL16_HA,
1933 16, /* rightshift */
1934 1, /* size (0 = byte, 1 = short, 2 = long) */
1935 16, /* bitsize */
1936 FALSE, /* pc_relative */
1937 0, /* bitpos */
1938 complain_overflow_signed, /* complain_on_overflow */
1939 ppc64_elf_unhandled_reloc, /* special_function */
1940 "R_PPC64_GOT_TPREL16_HA", /* name */
1941 FALSE, /* partial_inplace */
1942 0, /* src_mask */
1943 0xffff, /* dst_mask */
1944 FALSE), /* pcrel_offset */
1945
1946 HOWTO (R_PPC64_JMP_IREL, /* type */
1947 0, /* rightshift */
1948 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1949 0, /* bitsize */
1950 FALSE, /* pc_relative */
1951 0, /* bitpos */
1952 complain_overflow_dont, /* complain_on_overflow */
1953 ppc64_elf_unhandled_reloc, /* special_function */
1954 "R_PPC64_JMP_IREL", /* name */
1955 FALSE, /* partial_inplace */
1956 0, /* src_mask */
1957 0, /* dst_mask */
1958 FALSE), /* pcrel_offset */
1959
1960 HOWTO (R_PPC64_IRELATIVE, /* type */
1961 0, /* rightshift */
1962 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1963 64, /* bitsize */
1964 FALSE, /* pc_relative */
1965 0, /* bitpos */
1966 complain_overflow_dont, /* complain_on_overflow */
1967 bfd_elf_generic_reloc, /* special_function */
1968 "R_PPC64_IRELATIVE", /* name */
1969 FALSE, /* partial_inplace */
1970 0, /* src_mask */
1971 ONES (64), /* dst_mask */
1972 FALSE), /* pcrel_offset */
1973
1974 /* A 16 bit relative relocation. */
1975 HOWTO (R_PPC64_REL16, /* type */
1976 0, /* rightshift */
1977 1, /* size (0 = byte, 1 = short, 2 = long) */
1978 16, /* bitsize */
1979 TRUE, /* pc_relative */
1980 0, /* bitpos */
1981 complain_overflow_signed, /* complain_on_overflow */
1982 bfd_elf_generic_reloc, /* special_function */
1983 "R_PPC64_REL16", /* name */
1984 FALSE, /* partial_inplace */
1985 0, /* src_mask */
1986 0xffff, /* dst_mask */
1987 TRUE), /* pcrel_offset */
1988
1989 /* A 16 bit relative relocation without overflow. */
1990 HOWTO (R_PPC64_REL16_LO, /* type */
1991 0, /* rightshift */
1992 1, /* size (0 = byte, 1 = short, 2 = long) */
1993 16, /* bitsize */
1994 TRUE, /* pc_relative */
1995 0, /* bitpos */
1996 complain_overflow_dont,/* complain_on_overflow */
1997 bfd_elf_generic_reloc, /* special_function */
1998 "R_PPC64_REL16_LO", /* name */
1999 FALSE, /* partial_inplace */
2000 0, /* src_mask */
2001 0xffff, /* dst_mask */
2002 TRUE), /* pcrel_offset */
2003
2004 /* The high order 16 bits of a relative address. */
2005 HOWTO (R_PPC64_REL16_HI, /* type */
2006 16, /* rightshift */
2007 1, /* size (0 = byte, 1 = short, 2 = long) */
2008 16, /* bitsize */
2009 TRUE, /* pc_relative */
2010 0, /* bitpos */
2011 complain_overflow_signed, /* complain_on_overflow */
2012 bfd_elf_generic_reloc, /* special_function */
2013 "R_PPC64_REL16_HI", /* name */
2014 FALSE, /* partial_inplace */
2015 0, /* src_mask */
2016 0xffff, /* dst_mask */
2017 TRUE), /* pcrel_offset */
2018
2019 /* The high order 16 bits of a relative address, plus 1 if the contents of
2020 the low 16 bits, treated as a signed number, is negative. */
2021 HOWTO (R_PPC64_REL16_HA, /* type */
2022 16, /* rightshift */
2023 1, /* size (0 = byte, 1 = short, 2 = long) */
2024 16, /* bitsize */
2025 TRUE, /* pc_relative */
2026 0, /* bitpos */
2027 complain_overflow_signed, /* complain_on_overflow */
2028 ppc64_elf_ha_reloc, /* special_function */
2029 "R_PPC64_REL16_HA", /* name */
2030 FALSE, /* partial_inplace */
2031 0, /* src_mask */
2032 0xffff, /* dst_mask */
2033 TRUE), /* pcrel_offset */
2034
2035 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2036 HOWTO (R_PPC64_REL16DX_HA, /* type */
2037 16, /* rightshift */
2038 2, /* size (0 = byte, 1 = short, 2 = long) */
2039 16, /* bitsize */
2040 TRUE, /* pc_relative */
2041 0, /* bitpos */
2042 complain_overflow_signed, /* complain_on_overflow */
2043 ppc64_elf_ha_reloc, /* special_function */
2044 "R_PPC64_REL16DX_HA", /* name */
2045 FALSE, /* partial_inplace */
2046 0, /* src_mask */
2047 0x1fffc1, /* dst_mask */
2048 TRUE), /* pcrel_offset */
2049
2050 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2051 HOWTO (R_PPC64_16DX_HA, /* type */
2052 16, /* rightshift */
2053 2, /* size (0 = byte, 1 = short, 2 = long) */
2054 16, /* bitsize */
2055 FALSE, /* pc_relative */
2056 0, /* bitpos */
2057 complain_overflow_signed, /* complain_on_overflow */
2058 ppc64_elf_ha_reloc, /* special_function */
2059 "R_PPC64_16DX_HA", /* name */
2060 FALSE, /* partial_inplace */
2061 0, /* src_mask */
2062 0x1fffc1, /* dst_mask */
2063 FALSE), /* pcrel_offset */
2064
2065 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2066 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2067 16, /* rightshift */
2068 1, /* size (0 = byte, 1 = short, 2 = long) */
2069 16, /* bitsize */
2070 FALSE, /* pc_relative */
2071 0, /* bitpos */
2072 complain_overflow_dont, /* complain_on_overflow */
2073 bfd_elf_generic_reloc, /* special_function */
2074 "R_PPC64_ADDR16_HIGH", /* name */
2075 FALSE, /* partial_inplace */
2076 0, /* src_mask */
2077 0xffff, /* dst_mask */
2078 FALSE), /* pcrel_offset */
2079
2080 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2081 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2082 16, /* rightshift */
2083 1, /* size (0 = byte, 1 = short, 2 = long) */
2084 16, /* bitsize */
2085 FALSE, /* pc_relative */
2086 0, /* bitpos */
2087 complain_overflow_dont, /* complain_on_overflow */
2088 ppc64_elf_ha_reloc, /* special_function */
2089 "R_PPC64_ADDR16_HIGHA", /* name */
2090 FALSE, /* partial_inplace */
2091 0, /* src_mask */
2092 0xffff, /* dst_mask */
2093 FALSE), /* pcrel_offset */
2094
2095 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2096 HOWTO (R_PPC64_DTPREL16_HIGH,
2097 16, /* rightshift */
2098 1, /* size (0 = byte, 1 = short, 2 = long) */
2099 16, /* bitsize */
2100 FALSE, /* pc_relative */
2101 0, /* bitpos */
2102 complain_overflow_dont, /* complain_on_overflow */
2103 ppc64_elf_unhandled_reloc, /* special_function */
2104 "R_PPC64_DTPREL16_HIGH", /* name */
2105 FALSE, /* partial_inplace */
2106 0, /* src_mask */
2107 0xffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109
2110 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2111 HOWTO (R_PPC64_DTPREL16_HIGHA,
2112 16, /* rightshift */
2113 1, /* size (0 = byte, 1 = short, 2 = long) */
2114 16, /* bitsize */
2115 FALSE, /* pc_relative */
2116 0, /* bitpos */
2117 complain_overflow_dont, /* complain_on_overflow */
2118 ppc64_elf_unhandled_reloc, /* special_function */
2119 "R_PPC64_DTPREL16_HIGHA", /* name */
2120 FALSE, /* partial_inplace */
2121 0, /* src_mask */
2122 0xffff, /* dst_mask */
2123 FALSE), /* pcrel_offset */
2124
2125 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2126 HOWTO (R_PPC64_TPREL16_HIGH,
2127 16, /* rightshift */
2128 1, /* size (0 = byte, 1 = short, 2 = long) */
2129 16, /* bitsize */
2130 FALSE, /* pc_relative */
2131 0, /* bitpos */
2132 complain_overflow_dont, /* complain_on_overflow */
2133 ppc64_elf_unhandled_reloc, /* special_function */
2134 "R_PPC64_TPREL16_HIGH", /* name */
2135 FALSE, /* partial_inplace */
2136 0, /* src_mask */
2137 0xffff, /* dst_mask */
2138 FALSE), /* pcrel_offset */
2139
2140 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2141 HOWTO (R_PPC64_TPREL16_HIGHA,
2142 16, /* rightshift */
2143 1, /* size (0 = byte, 1 = short, 2 = long) */
2144 16, /* bitsize */
2145 FALSE, /* pc_relative */
2146 0, /* bitpos */
2147 complain_overflow_dont, /* complain_on_overflow */
2148 ppc64_elf_unhandled_reloc, /* special_function */
2149 "R_PPC64_TPREL16_HIGHA", /* name */
2150 FALSE, /* partial_inplace */
2151 0, /* src_mask */
2152 0xffff, /* dst_mask */
2153 FALSE), /* pcrel_offset */
2154
2155 /* Marker reloc on ELFv2 large-model function entry. */
2156 HOWTO (R_PPC64_ENTRY,
2157 0, /* rightshift */
2158 2, /* size (0 = byte, 1 = short, 2 = long) */
2159 32, /* bitsize */
2160 FALSE, /* pc_relative */
2161 0, /* bitpos */
2162 complain_overflow_dont, /* complain_on_overflow */
2163 bfd_elf_generic_reloc, /* special_function */
2164 "R_PPC64_ENTRY", /* name */
2165 FALSE, /* partial_inplace */
2166 0, /* src_mask */
2167 0, /* dst_mask */
2168 FALSE), /* pcrel_offset */
2169
2170 /* Like ADDR64, but use local entry point of function. */
2171 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2172 0, /* rightshift */
2173 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2174 64, /* bitsize */
2175 FALSE, /* pc_relative */
2176 0, /* bitpos */
2177 complain_overflow_dont, /* complain_on_overflow */
2178 bfd_elf_generic_reloc, /* special_function */
2179 "R_PPC64_ADDR64_LOCAL", /* name */
2180 FALSE, /* partial_inplace */
2181 0, /* src_mask */
2182 ONES (64), /* dst_mask */
2183 FALSE), /* pcrel_offset */
2184
2185 /* GNU extension to record C++ vtable hierarchy. */
2186 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2187 0, /* rightshift */
2188 0, /* size (0 = byte, 1 = short, 2 = long) */
2189 0, /* bitsize */
2190 FALSE, /* pc_relative */
2191 0, /* bitpos */
2192 complain_overflow_dont, /* complain_on_overflow */
2193 NULL, /* special_function */
2194 "R_PPC64_GNU_VTINHERIT", /* name */
2195 FALSE, /* partial_inplace */
2196 0, /* src_mask */
2197 0, /* dst_mask */
2198 FALSE), /* pcrel_offset */
2199
2200 /* GNU extension to record C++ vtable member usage. */
2201 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2202 0, /* rightshift */
2203 0, /* size (0 = byte, 1 = short, 2 = long) */
2204 0, /* bitsize */
2205 FALSE, /* pc_relative */
2206 0, /* bitpos */
2207 complain_overflow_dont, /* complain_on_overflow */
2208 NULL, /* special_function */
2209 "R_PPC64_GNU_VTENTRY", /* name */
2210 FALSE, /* partial_inplace */
2211 0, /* src_mask */
2212 0, /* dst_mask */
2213 FALSE), /* pcrel_offset */
2214 };
2215
2216 \f
2217 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2218 be done. */
2219
2220 static void
2221 ppc_howto_init (void)
2222 {
2223 unsigned int i, type;
2224
2225 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2226 {
2227 type = ppc64_elf_howto_raw[i].type;
2228 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2229 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2230 }
2231 }
2232
2233 static reloc_howto_type *
2234 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2235 bfd_reloc_code_real_type code)
2236 {
2237 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2238
2239 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2240 /* Initialize howto table if needed. */
2241 ppc_howto_init ();
2242
2243 switch (code)
2244 {
2245 default:
2246 return NULL;
2247
2248 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2249 break;
2250 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2251 break;
2252 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2253 break;
2254 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2255 break;
2256 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2257 break;
2258 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2259 break;
2260 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2261 break;
2262 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2263 break;
2264 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2265 break;
2266 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2267 break;
2268 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2269 break;
2270 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2271 break;
2272 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2273 break;
2274 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2275 break;
2276 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2277 break;
2278 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2279 break;
2280 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2281 break;
2282 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2283 break;
2284 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2285 break;
2286 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2287 break;
2288 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2289 break;
2290 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2291 break;
2292 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2293 break;
2294 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2295 break;
2296 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2297 break;
2298 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2299 break;
2300 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2301 break;
2302 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2303 break;
2304 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2305 break;
2306 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2307 break;
2308 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2309 break;
2310 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2311 break;
2312 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2313 break;
2314 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2315 break;
2316 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2317 break;
2318 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2319 break;
2320 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2321 break;
2322 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2323 break;
2324 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2325 break;
2326 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2327 break;
2328 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2329 break;
2330 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2331 break;
2332 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2333 break;
2334 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2335 break;
2336 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2337 break;
2338 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2339 break;
2340 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2341 break;
2342 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2343 break;
2344 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2345 break;
2346 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2347 break;
2348 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2349 break;
2350 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2351 break;
2352 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2353 break;
2354 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2355 break;
2356 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2357 break;
2358 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2359 break;
2360 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2361 break;
2362 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2363 break;
2364 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2365 break;
2366 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2367 break;
2368 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2369 break;
2370 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2371 break;
2372 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2373 break;
2374 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2375 break;
2376 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2377 break;
2378 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2379 break;
2380 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2381 break;
2382 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2383 break;
2384 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2385 break;
2386 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2387 break;
2388 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2389 break;
2390 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2391 break;
2392 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2393 break;
2394 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2395 break;
2396 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2397 break;
2398 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2399 break;
2400 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2401 break;
2402 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2403 break;
2404 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2405 break;
2406 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2407 break;
2408 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2409 break;
2410 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2411 break;
2412 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2413 break;
2414 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2415 break;
2416 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2417 break;
2418 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2419 break;
2420 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2421 break;
2422 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2423 break;
2424 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2425 break;
2426 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2427 break;
2428 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2429 break;
2430 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2431 break;
2432 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2433 break;
2434 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2435 break;
2436 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2437 break;
2438 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2439 break;
2440 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2441 break;
2442 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2443 break;
2444 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2445 break;
2446 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2447 break;
2448 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2449 break;
2450 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2451 break;
2452 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2453 break;
2454 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2455 break;
2456 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2457 break;
2458 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2459 break;
2460 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2461 break;
2462 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2463 break;
2464 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2465 break;
2466 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2467 break;
2468 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2469 break;
2470 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2471 break;
2472 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2473 break;
2474 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2475 break;
2476 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2477 break;
2478 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2479 break;
2480 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2481 break;
2482 }
2483
2484 return ppc64_elf_howto_table[r];
2485 };
2486
2487 static reloc_howto_type *
2488 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2489 const char *r_name)
2490 {
2491 unsigned int i;
2492
2493 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2494 if (ppc64_elf_howto_raw[i].name != NULL
2495 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2496 return &ppc64_elf_howto_raw[i];
2497
2498 return NULL;
2499 }
2500
2501 /* Set the howto pointer for a PowerPC ELF reloc. */
2502
2503 static void
2504 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2505 Elf_Internal_Rela *dst)
2506 {
2507 unsigned int type;
2508
2509 /* Initialize howto table if needed. */
2510 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2511 ppc_howto_init ();
2512
2513 type = ELF64_R_TYPE (dst->r_info);
2514 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2515 {
2516 /* xgettext:c-format */
2517 _bfd_error_handler (_("%B: invalid relocation type %d"),
2518 abfd, (int) type);
2519 type = R_PPC64_NONE;
2520 }
2521 cache_ptr->howto = ppc64_elf_howto_table[type];
2522 }
2523
2524 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2525
2526 static bfd_reloc_status_type
2527 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2528 void *data, asection *input_section,
2529 bfd *output_bfd, char **error_message)
2530 {
2531 enum elf_ppc64_reloc_type r_type;
2532 long insn;
2533 bfd_size_type octets;
2534 bfd_vma value;
2535
2536 /* If this is a relocatable link (output_bfd test tells us), just
2537 call the generic function. Any adjustment will be done at final
2538 link time. */
2539 if (output_bfd != NULL)
2540 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2541 input_section, output_bfd, error_message);
2542
2543 /* Adjust the addend for sign extension of the low 16 bits.
2544 We won't actually be using the low 16 bits, so trashing them
2545 doesn't matter. */
2546 reloc_entry->addend += 0x8000;
2547 r_type = reloc_entry->howto->type;
2548 if (r_type != R_PPC64_REL16DX_HA)
2549 return bfd_reloc_continue;
2550
2551 value = 0;
2552 if (!bfd_is_com_section (symbol->section))
2553 value = symbol->value;
2554 value += (reloc_entry->addend
2555 + symbol->section->output_offset
2556 + symbol->section->output_section->vma);
2557 value -= (reloc_entry->address
2558 + input_section->output_offset
2559 + input_section->output_section->vma);
2560 value = (bfd_signed_vma) value >> 16;
2561
2562 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2563 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2564 insn &= ~0x1fffc1;
2565 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2566 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2567 if (value + 0x8000 > 0xffff)
2568 return bfd_reloc_overflow;
2569 return bfd_reloc_ok;
2570 }
2571
2572 static bfd_reloc_status_type
2573 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2574 void *data, asection *input_section,
2575 bfd *output_bfd, char **error_message)
2576 {
2577 if (output_bfd != NULL)
2578 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2579 input_section, output_bfd, error_message);
2580
2581 if (strcmp (symbol->section->name, ".opd") == 0
2582 && (symbol->section->owner->flags & DYNAMIC) == 0)
2583 {
2584 bfd_vma dest = opd_entry_value (symbol->section,
2585 symbol->value + reloc_entry->addend,
2586 NULL, NULL, FALSE);
2587 if (dest != (bfd_vma) -1)
2588 reloc_entry->addend = dest - (symbol->value
2589 + symbol->section->output_section->vma
2590 + symbol->section->output_offset);
2591 }
2592 else
2593 {
2594 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2595
2596 if (symbol->section->owner != abfd
2597 && symbol->section->owner != NULL
2598 && abiversion (symbol->section->owner) >= 2)
2599 {
2600 unsigned int i;
2601
2602 for (i = 0; i < symbol->section->owner->symcount; ++i)
2603 {
2604 asymbol *symdef = symbol->section->owner->outsymbols[i];
2605
2606 if (strcmp (symdef->name, symbol->name) == 0)
2607 {
2608 elfsym = (elf_symbol_type *) symdef;
2609 break;
2610 }
2611 }
2612 }
2613 reloc_entry->addend
2614 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2615 }
2616 return bfd_reloc_continue;
2617 }
2618
2619 static bfd_reloc_status_type
2620 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2621 void *data, asection *input_section,
2622 bfd *output_bfd, char **error_message)
2623 {
2624 long insn;
2625 enum elf_ppc64_reloc_type r_type;
2626 bfd_size_type octets;
2627 /* Assume 'at' branch hints. */
2628 bfd_boolean is_isa_v2 = TRUE;
2629
2630 /* If this is a relocatable link (output_bfd test tells us), just
2631 call the generic function. Any adjustment will be done at final
2632 link time. */
2633 if (output_bfd != NULL)
2634 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2635 input_section, output_bfd, error_message);
2636
2637 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2638 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2639 insn &= ~(0x01 << 21);
2640 r_type = reloc_entry->howto->type;
2641 if (r_type == R_PPC64_ADDR14_BRTAKEN
2642 || r_type == R_PPC64_REL14_BRTAKEN)
2643 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2644
2645 if (is_isa_v2)
2646 {
2647 /* Set 'a' bit. This is 0b00010 in BO field for branch
2648 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2649 for branch on CTR insns (BO == 1a00t or 1a01t). */
2650 if ((insn & (0x14 << 21)) == (0x04 << 21))
2651 insn |= 0x02 << 21;
2652 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2653 insn |= 0x08 << 21;
2654 else
2655 goto out;
2656 }
2657 else
2658 {
2659 bfd_vma target = 0;
2660 bfd_vma from;
2661
2662 if (!bfd_is_com_section (symbol->section))
2663 target = symbol->value;
2664 target += symbol->section->output_section->vma;
2665 target += symbol->section->output_offset;
2666 target += reloc_entry->addend;
2667
2668 from = (reloc_entry->address
2669 + input_section->output_offset
2670 + input_section->output_section->vma);
2671
2672 /* Invert 'y' bit if not the default. */
2673 if ((bfd_signed_vma) (target - from) < 0)
2674 insn ^= 0x01 << 21;
2675 }
2676 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2677 out:
2678 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2679 input_section, output_bfd, error_message);
2680 }
2681
2682 static bfd_reloc_status_type
2683 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2684 void *data, asection *input_section,
2685 bfd *output_bfd, char **error_message)
2686 {
2687 /* If this is a relocatable link (output_bfd test tells us), just
2688 call the generic function. Any adjustment will be done at final
2689 link time. */
2690 if (output_bfd != NULL)
2691 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2692 input_section, output_bfd, error_message);
2693
2694 /* Subtract the symbol section base address. */
2695 reloc_entry->addend -= symbol->section->output_section->vma;
2696 return bfd_reloc_continue;
2697 }
2698
2699 static bfd_reloc_status_type
2700 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2701 void *data, asection *input_section,
2702 bfd *output_bfd, char **error_message)
2703 {
2704 /* If this is a relocatable link (output_bfd test tells us), just
2705 call the generic function. Any adjustment will be done at final
2706 link time. */
2707 if (output_bfd != NULL)
2708 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2709 input_section, output_bfd, error_message);
2710
2711 /* Subtract the symbol section base address. */
2712 reloc_entry->addend -= symbol->section->output_section->vma;
2713
2714 /* Adjust the addend for sign extension of the low 16 bits. */
2715 reloc_entry->addend += 0x8000;
2716 return bfd_reloc_continue;
2717 }
2718
2719 static bfd_reloc_status_type
2720 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2721 void *data, asection *input_section,
2722 bfd *output_bfd, char **error_message)
2723 {
2724 bfd_vma TOCstart;
2725
2726 /* If this is a relocatable link (output_bfd test tells us), just
2727 call the generic function. Any adjustment will be done at final
2728 link time. */
2729 if (output_bfd != NULL)
2730 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2731 input_section, output_bfd, error_message);
2732
2733 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2734 if (TOCstart == 0)
2735 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2736
2737 /* Subtract the TOC base address. */
2738 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2739 return bfd_reloc_continue;
2740 }
2741
2742 static bfd_reloc_status_type
2743 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2744 void *data, asection *input_section,
2745 bfd *output_bfd, char **error_message)
2746 {
2747 bfd_vma TOCstart;
2748
2749 /* If this is a relocatable link (output_bfd test tells us), just
2750 call the generic function. Any adjustment will be done at final
2751 link time. */
2752 if (output_bfd != NULL)
2753 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2754 input_section, output_bfd, error_message);
2755
2756 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2757 if (TOCstart == 0)
2758 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2759
2760 /* Subtract the TOC base address. */
2761 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2762
2763 /* Adjust the addend for sign extension of the low 16 bits. */
2764 reloc_entry->addend += 0x8000;
2765 return bfd_reloc_continue;
2766 }
2767
2768 static bfd_reloc_status_type
2769 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2770 void *data, asection *input_section,
2771 bfd *output_bfd, char **error_message)
2772 {
2773 bfd_vma TOCstart;
2774 bfd_size_type octets;
2775
2776 /* If this is a relocatable link (output_bfd test tells us), just
2777 call the generic function. Any adjustment will be done at final
2778 link time. */
2779 if (output_bfd != NULL)
2780 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2781 input_section, output_bfd, error_message);
2782
2783 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2784 if (TOCstart == 0)
2785 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2786
2787 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2788 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2789 return bfd_reloc_ok;
2790 }
2791
2792 static bfd_reloc_status_type
2793 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2794 void *data, asection *input_section,
2795 bfd *output_bfd, char **error_message)
2796 {
2797 /* If this is a relocatable link (output_bfd test tells us), just
2798 call the generic function. Any adjustment will be done at final
2799 link time. */
2800 if (output_bfd != NULL)
2801 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2802 input_section, output_bfd, error_message);
2803
2804 if (error_message != NULL)
2805 {
2806 static char buf[60];
2807 sprintf (buf, "generic linker can't handle %s",
2808 reloc_entry->howto->name);
2809 *error_message = buf;
2810 }
2811 return bfd_reloc_dangerous;
2812 }
2813
2814 /* Track GOT entries needed for a given symbol. We might need more
2815 than one got entry per symbol. */
2816 struct got_entry
2817 {
2818 struct got_entry *next;
2819
2820 /* The symbol addend that we'll be placing in the GOT. */
2821 bfd_vma addend;
2822
2823 /* Unlike other ELF targets, we use separate GOT entries for the same
2824 symbol referenced from different input files. This is to support
2825 automatic multiple TOC/GOT sections, where the TOC base can vary
2826 from one input file to another. After partitioning into TOC groups
2827 we merge entries within the group.
2828
2829 Point to the BFD owning this GOT entry. */
2830 bfd *owner;
2831
2832 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2833 TLS_TPREL or TLS_DTPREL for tls entries. */
2834 unsigned char tls_type;
2835
2836 /* Non-zero if got.ent points to real entry. */
2837 unsigned char is_indirect;
2838
2839 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2840 union
2841 {
2842 bfd_signed_vma refcount;
2843 bfd_vma offset;
2844 struct got_entry *ent;
2845 } got;
2846 };
2847
2848 /* The same for PLT. */
2849 struct plt_entry
2850 {
2851 struct plt_entry *next;
2852
2853 bfd_vma addend;
2854
2855 union
2856 {
2857 bfd_signed_vma refcount;
2858 bfd_vma offset;
2859 } plt;
2860 };
2861
2862 struct ppc64_elf_obj_tdata
2863 {
2864 struct elf_obj_tdata elf;
2865
2866 /* Shortcuts to dynamic linker sections. */
2867 asection *got;
2868 asection *relgot;
2869
2870 /* Used during garbage collection. We attach global symbols defined
2871 on removed .opd entries to this section so that the sym is removed. */
2872 asection *deleted_section;
2873
2874 /* TLS local dynamic got entry handling. Support for multiple GOT
2875 sections means we potentially need one of these for each input bfd. */
2876 struct got_entry tlsld_got;
2877
2878 union {
2879 /* A copy of relocs before they are modified for --emit-relocs. */
2880 Elf_Internal_Rela *relocs;
2881
2882 /* Section contents. */
2883 bfd_byte *contents;
2884 } opd;
2885
2886 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2887 the reloc to be in the range -32768 to 32767. */
2888 unsigned int has_small_toc_reloc : 1;
2889
2890 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2891 instruction not one we handle. */
2892 unsigned int unexpected_toc_insn : 1;
2893 };
2894
2895 #define ppc64_elf_tdata(bfd) \
2896 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2897
2898 #define ppc64_tlsld_got(bfd) \
2899 (&ppc64_elf_tdata (bfd)->tlsld_got)
2900
2901 #define is_ppc64_elf(bfd) \
2902 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2903 && elf_object_id (bfd) == PPC64_ELF_DATA)
2904
2905 /* Override the generic function because we store some extras. */
2906
2907 static bfd_boolean
2908 ppc64_elf_mkobject (bfd *abfd)
2909 {
2910 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2911 PPC64_ELF_DATA);
2912 }
2913
2914 /* Fix bad default arch selected for a 64 bit input bfd when the
2915 default is 32 bit. Also select arch based on apuinfo. */
2916
2917 static bfd_boolean
2918 ppc64_elf_object_p (bfd *abfd)
2919 {
2920 if (!abfd->arch_info->the_default)
2921 return TRUE;
2922
2923 if (abfd->arch_info->bits_per_word == 32)
2924 {
2925 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2926
2927 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2928 {
2929 /* Relies on arch after 32 bit default being 64 bit default. */
2930 abfd->arch_info = abfd->arch_info->next;
2931 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2932 }
2933 }
2934 return _bfd_elf_ppc_set_arch (abfd);
2935 }
2936
2937 /* Support for core dump NOTE sections. */
2938
2939 static bfd_boolean
2940 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2941 {
2942 size_t offset, size;
2943
2944 if (note->descsz != 504)
2945 return FALSE;
2946
2947 /* pr_cursig */
2948 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2949
2950 /* pr_pid */
2951 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2952
2953 /* pr_reg */
2954 offset = 112;
2955 size = 384;
2956
2957 /* Make a ".reg/999" section. */
2958 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2959 size, note->descpos + offset);
2960 }
2961
2962 static bfd_boolean
2963 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2964 {
2965 if (note->descsz != 136)
2966 return FALSE;
2967
2968 elf_tdata (abfd)->core->pid
2969 = bfd_get_32 (abfd, note->descdata + 24);
2970 elf_tdata (abfd)->core->program
2971 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2972 elf_tdata (abfd)->core->command
2973 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2974
2975 return TRUE;
2976 }
2977
2978 static char *
2979 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2980 ...)
2981 {
2982 switch (note_type)
2983 {
2984 default:
2985 return NULL;
2986
2987 case NT_PRPSINFO:
2988 {
2989 char data[136];
2990 va_list ap;
2991
2992 va_start (ap, note_type);
2993 memset (data, 0, sizeof (data));
2994 strncpy (data + 40, va_arg (ap, const char *), 16);
2995 strncpy (data + 56, va_arg (ap, const char *), 80);
2996 va_end (ap);
2997 return elfcore_write_note (abfd, buf, bufsiz,
2998 "CORE", note_type, data, sizeof (data));
2999 }
3000
3001 case NT_PRSTATUS:
3002 {
3003 char data[504];
3004 va_list ap;
3005 long pid;
3006 int cursig;
3007 const void *greg;
3008
3009 va_start (ap, note_type);
3010 memset (data, 0, 112);
3011 pid = va_arg (ap, long);
3012 bfd_put_32 (abfd, pid, data + 32);
3013 cursig = va_arg (ap, int);
3014 bfd_put_16 (abfd, cursig, data + 12);
3015 greg = va_arg (ap, const void *);
3016 memcpy (data + 112, greg, 384);
3017 memset (data + 496, 0, 8);
3018 va_end (ap);
3019 return elfcore_write_note (abfd, buf, bufsiz,
3020 "CORE", note_type, data, sizeof (data));
3021 }
3022 }
3023 }
3024
3025 /* Add extra PPC sections. */
3026
3027 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3028 {
3029 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3030 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3031 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3032 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3033 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3034 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3035 { NULL, 0, 0, 0, 0 }
3036 };
3037
3038 enum _ppc64_sec_type {
3039 sec_normal = 0,
3040 sec_opd = 1,
3041 sec_toc = 2
3042 };
3043
3044 struct _ppc64_elf_section_data
3045 {
3046 struct bfd_elf_section_data elf;
3047
3048 union
3049 {
3050 /* An array with one entry for each opd function descriptor,
3051 and some spares since opd entries may be either 16 or 24 bytes. */
3052 #define OPD_NDX(OFF) ((OFF) >> 4)
3053 struct _opd_sec_data
3054 {
3055 /* Points to the function code section for local opd entries. */
3056 asection **func_sec;
3057
3058 /* After editing .opd, adjust references to opd local syms. */
3059 long *adjust;
3060 } opd;
3061
3062 /* An array for toc sections, indexed by offset/8. */
3063 struct _toc_sec_data
3064 {
3065 /* Specifies the relocation symbol index used at a given toc offset. */
3066 unsigned *symndx;
3067
3068 /* And the relocation addend. */
3069 bfd_vma *add;
3070 } toc;
3071 } u;
3072
3073 enum _ppc64_sec_type sec_type:2;
3074
3075 /* Flag set when small branches are detected. Used to
3076 select suitable defaults for the stub group size. */
3077 unsigned int has_14bit_branch:1;
3078 };
3079
3080 #define ppc64_elf_section_data(sec) \
3081 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3082
3083 static bfd_boolean
3084 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3085 {
3086 if (!sec->used_by_bfd)
3087 {
3088 struct _ppc64_elf_section_data *sdata;
3089 bfd_size_type amt = sizeof (*sdata);
3090
3091 sdata = bfd_zalloc (abfd, amt);
3092 if (sdata == NULL)
3093 return FALSE;
3094 sec->used_by_bfd = sdata;
3095 }
3096
3097 return _bfd_elf_new_section_hook (abfd, sec);
3098 }
3099
3100 static struct _opd_sec_data *
3101 get_opd_info (asection * sec)
3102 {
3103 if (sec != NULL
3104 && ppc64_elf_section_data (sec) != NULL
3105 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3106 return &ppc64_elf_section_data (sec)->u.opd;
3107 return NULL;
3108 }
3109 \f
3110 /* Parameters for the qsort hook. */
3111 static bfd_boolean synthetic_relocatable;
3112 static asection *synthetic_opd;
3113
3114 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3115
3116 static int
3117 compare_symbols (const void *ap, const void *bp)
3118 {
3119 const asymbol *a = * (const asymbol **) ap;
3120 const asymbol *b = * (const asymbol **) bp;
3121
3122 /* Section symbols first. */
3123 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3124 return -1;
3125 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3126 return 1;
3127
3128 /* then .opd symbols. */
3129 if (synthetic_opd != NULL)
3130 {
3131 if (strcmp (a->section->name, ".opd") == 0
3132 && strcmp (b->section->name, ".opd") != 0)
3133 return -1;
3134 if (strcmp (a->section->name, ".opd") != 0
3135 && strcmp (b->section->name, ".opd") == 0)
3136 return 1;
3137 }
3138
3139 /* then other code symbols. */
3140 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3141 == (SEC_CODE | SEC_ALLOC)
3142 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3143 != (SEC_CODE | SEC_ALLOC))
3144 return -1;
3145
3146 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3147 != (SEC_CODE | SEC_ALLOC)
3148 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3149 == (SEC_CODE | SEC_ALLOC))
3150 return 1;
3151
3152 if (synthetic_relocatable)
3153 {
3154 if (a->section->id < b->section->id)
3155 return -1;
3156
3157 if (a->section->id > b->section->id)
3158 return 1;
3159 }
3160
3161 if (a->value + a->section->vma < b->value + b->section->vma)
3162 return -1;
3163
3164 if (a->value + a->section->vma > b->value + b->section->vma)
3165 return 1;
3166
3167 /* For syms with the same value, prefer strong dynamic global function
3168 syms over other syms. */
3169 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3170 return -1;
3171
3172 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3173 return 1;
3174
3175 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3176 return -1;
3177
3178 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3179 return 1;
3180
3181 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3182 return -1;
3183
3184 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3185 return 1;
3186
3187 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3188 return -1;
3189
3190 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3191 return 1;
3192
3193 return a > b;
3194 }
3195
3196 /* Search SYMS for a symbol of the given VALUE. */
3197
3198 static asymbol *
3199 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3200 {
3201 long mid;
3202
3203 if (id == (unsigned) -1)
3204 {
3205 while (lo < hi)
3206 {
3207 mid = (lo + hi) >> 1;
3208 if (syms[mid]->value + syms[mid]->section->vma < value)
3209 lo = mid + 1;
3210 else if (syms[mid]->value + syms[mid]->section->vma > value)
3211 hi = mid;
3212 else
3213 return syms[mid];
3214 }
3215 }
3216 else
3217 {
3218 while (lo < hi)
3219 {
3220 mid = (lo + hi) >> 1;
3221 if (syms[mid]->section->id < id)
3222 lo = mid + 1;
3223 else if (syms[mid]->section->id > id)
3224 hi = mid;
3225 else if (syms[mid]->value < value)
3226 lo = mid + 1;
3227 else if (syms[mid]->value > value)
3228 hi = mid;
3229 else
3230 return syms[mid];
3231 }
3232 }
3233 return NULL;
3234 }
3235
3236 static bfd_boolean
3237 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3238 {
3239 bfd_vma vma = *(bfd_vma *) ptr;
3240 return ((section->flags & SEC_ALLOC) != 0
3241 && section->vma <= vma
3242 && vma < section->vma + section->size);
3243 }
3244
3245 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3246 entry syms. Also generate @plt symbols for the glink branch table.
3247 Returns count of synthetic symbols in RET or -1 on error. */
3248
3249 static long
3250 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3251 long static_count, asymbol **static_syms,
3252 long dyn_count, asymbol **dyn_syms,
3253 asymbol **ret)
3254 {
3255 asymbol *s;
3256 long i;
3257 long count;
3258 char *names;
3259 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3260 asection *opd = NULL;
3261 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3262 asymbol **syms;
3263 int abi = abiversion (abfd);
3264
3265 *ret = NULL;
3266
3267 if (abi < 2)
3268 {
3269 opd = bfd_get_section_by_name (abfd, ".opd");
3270 if (opd == NULL && abi == 1)
3271 return 0;
3272 }
3273
3274 syms = NULL;
3275 codesecsym = 0;
3276 codesecsymend = 0;
3277 secsymend = 0;
3278 opdsymend = 0;
3279 symcount = 0;
3280 if (opd != NULL)
3281 {
3282 symcount = static_count;
3283 if (!relocatable)
3284 symcount += dyn_count;
3285 if (symcount == 0)
3286 return 0;
3287
3288 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3289 if (syms == NULL)
3290 return -1;
3291
3292 if (!relocatable && static_count != 0 && dyn_count != 0)
3293 {
3294 /* Use both symbol tables. */
3295 memcpy (syms, static_syms, static_count * sizeof (*syms));
3296 memcpy (syms + static_count, dyn_syms,
3297 (dyn_count + 1) * sizeof (*syms));
3298 }
3299 else if (!relocatable && static_count == 0)
3300 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3301 else
3302 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3303
3304 synthetic_relocatable = relocatable;
3305 synthetic_opd = opd;
3306 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3307
3308 if (!relocatable && symcount > 1)
3309 {
3310 long j;
3311 /* Trim duplicate syms, since we may have merged the normal and
3312 dynamic symbols. Actually, we only care about syms that have
3313 different values, so trim any with the same value. */
3314 for (i = 1, j = 1; i < symcount; ++i)
3315 if (syms[i - 1]->value + syms[i - 1]->section->vma
3316 != syms[i]->value + syms[i]->section->vma)
3317 syms[j++] = syms[i];
3318 symcount = j;
3319 }
3320
3321 i = 0;
3322 /* Note that here and in compare_symbols we can't compare opd and
3323 sym->section directly. With separate debug info files, the
3324 symbols will be extracted from the debug file while abfd passed
3325 to this function is the real binary. */
3326 if (opd != NULL && strcmp (syms[i]->section->name, ".opd") == 0)
3327 ++i;
3328 codesecsym = i;
3329
3330 for (; i < symcount; ++i)
3331 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3332 | SEC_THREAD_LOCAL))
3333 != (SEC_CODE | SEC_ALLOC))
3334 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3335 break;
3336 codesecsymend = i;
3337
3338 for (; i < symcount; ++i)
3339 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3340 break;
3341 secsymend = i;
3342
3343 for (; i < symcount; ++i)
3344 if (strcmp (syms[i]->section->name, ".opd") != 0)
3345 break;
3346 opdsymend = i;
3347
3348 for (; i < symcount; ++i)
3349 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3350 != (SEC_CODE | SEC_ALLOC))
3351 break;
3352 symcount = i;
3353 }
3354 count = 0;
3355
3356 if (relocatable)
3357 {
3358 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3359 arelent *r;
3360 size_t size;
3361 long relcount;
3362
3363 if (opdsymend == secsymend)
3364 goto done;
3365
3366 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3367 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3368 if (relcount == 0)
3369 goto done;
3370
3371 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3372 {
3373 count = -1;
3374 goto done;
3375 }
3376
3377 size = 0;
3378 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3379 {
3380 asymbol *sym;
3381
3382 while (r < opd->relocation + relcount
3383 && r->address < syms[i]->value + opd->vma)
3384 ++r;
3385
3386 if (r == opd->relocation + relcount)
3387 break;
3388
3389 if (r->address != syms[i]->value + opd->vma)
3390 continue;
3391
3392 if (r->howto->type != R_PPC64_ADDR64)
3393 continue;
3394
3395 sym = *r->sym_ptr_ptr;
3396 if (!sym_exists_at (syms, opdsymend, symcount,
3397 sym->section->id, sym->value + r->addend))
3398 {
3399 ++count;
3400 size += sizeof (asymbol);
3401 size += strlen (syms[i]->name) + 2;
3402 }
3403 }
3404
3405 if (size == 0)
3406 goto done;
3407 s = *ret = bfd_malloc (size);
3408 if (s == NULL)
3409 {
3410 count = -1;
3411 goto done;
3412 }
3413
3414 names = (char *) (s + count);
3415
3416 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3417 {
3418 asymbol *sym;
3419
3420 while (r < opd->relocation + relcount
3421 && r->address < syms[i]->value + opd->vma)
3422 ++r;
3423
3424 if (r == opd->relocation + relcount)
3425 break;
3426
3427 if (r->address != syms[i]->value + opd->vma)
3428 continue;
3429
3430 if (r->howto->type != R_PPC64_ADDR64)
3431 continue;
3432
3433 sym = *r->sym_ptr_ptr;
3434 if (!sym_exists_at (syms, opdsymend, symcount,
3435 sym->section->id, sym->value + r->addend))
3436 {
3437 size_t len;
3438
3439 *s = *syms[i];
3440 s->flags |= BSF_SYNTHETIC;
3441 s->section = sym->section;
3442 s->value = sym->value + r->addend;
3443 s->name = names;
3444 *names++ = '.';
3445 len = strlen (syms[i]->name);
3446 memcpy (names, syms[i]->name, len + 1);
3447 names += len + 1;
3448 /* Have udata.p point back to the original symbol this
3449 synthetic symbol was derived from. */
3450 s->udata.p = syms[i];
3451 s++;
3452 }
3453 }
3454 }
3455 else
3456 {
3457 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3458 bfd_byte *contents = NULL;
3459 size_t size;
3460 long plt_count = 0;
3461 bfd_vma glink_vma = 0, resolv_vma = 0;
3462 asection *dynamic, *glink = NULL, *relplt = NULL;
3463 arelent *p;
3464
3465 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3466 {
3467 free_contents_and_exit_err:
3468 count = -1;
3469 free_contents_and_exit:
3470 if (contents)
3471 free (contents);
3472 goto done;
3473 }
3474
3475 size = 0;
3476 for (i = secsymend; i < opdsymend; ++i)
3477 {
3478 bfd_vma ent;
3479
3480 /* Ignore bogus symbols. */
3481 if (syms[i]->value > opd->size - 8)
3482 continue;
3483
3484 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3485 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3486 {
3487 ++count;
3488 size += sizeof (asymbol);
3489 size += strlen (syms[i]->name) + 2;
3490 }
3491 }
3492
3493 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3494 if (dyn_count != 0
3495 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3496 {
3497 bfd_byte *dynbuf, *extdyn, *extdynend;
3498 size_t extdynsize;
3499 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3500
3501 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3502 goto free_contents_and_exit_err;
3503
3504 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3505 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3506
3507 extdyn = dynbuf;
3508 extdynend = extdyn + dynamic->size;
3509 for (; extdyn < extdynend; extdyn += extdynsize)
3510 {
3511 Elf_Internal_Dyn dyn;
3512 (*swap_dyn_in) (abfd, extdyn, &dyn);
3513
3514 if (dyn.d_tag == DT_NULL)
3515 break;
3516
3517 if (dyn.d_tag == DT_PPC64_GLINK)
3518 {
3519 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3520 See comment in ppc64_elf_finish_dynamic_sections. */
3521 glink_vma = dyn.d_un.d_val + 8 * 4;
3522 /* The .glink section usually does not survive the final
3523 link; search for the section (usually .text) where the
3524 glink stubs now reside. */
3525 glink = bfd_sections_find_if (abfd, section_covers_vma,
3526 &glink_vma);
3527 break;
3528 }
3529 }
3530
3531 free (dynbuf);
3532 }
3533
3534 if (glink != NULL)
3535 {
3536 /* Determine __glink trampoline by reading the relative branch
3537 from the first glink stub. */
3538 bfd_byte buf[4];
3539 unsigned int off = 0;
3540
3541 while (bfd_get_section_contents (abfd, glink, buf,
3542 glink_vma + off - glink->vma, 4))
3543 {
3544 unsigned int insn = bfd_get_32 (abfd, buf);
3545 insn ^= B_DOT;
3546 if ((insn & ~0x3fffffc) == 0)
3547 {
3548 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3549 break;
3550 }
3551 off += 4;
3552 if (off > 4)
3553 break;
3554 }
3555
3556 if (resolv_vma)
3557 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3558
3559 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3560 if (relplt != NULL)
3561 {
3562 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3563 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3564 goto free_contents_and_exit_err;
3565
3566 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3567 size += plt_count * sizeof (asymbol);
3568
3569 p = relplt->relocation;
3570 for (i = 0; i < plt_count; i++, p++)
3571 {
3572 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3573 if (p->addend != 0)
3574 size += sizeof ("+0x") - 1 + 16;
3575 }
3576 }
3577 }
3578
3579 if (size == 0)
3580 goto free_contents_and_exit;
3581 s = *ret = bfd_malloc (size);
3582 if (s == NULL)
3583 goto free_contents_and_exit_err;
3584
3585 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3586
3587 for (i = secsymend; i < opdsymend; ++i)
3588 {
3589 bfd_vma ent;
3590
3591 if (syms[i]->value > opd->size - 8)
3592 continue;
3593
3594 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3595 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3596 {
3597 long lo, hi;
3598 size_t len;
3599 asection *sec = abfd->sections;
3600
3601 *s = *syms[i];
3602 lo = codesecsym;
3603 hi = codesecsymend;
3604 while (lo < hi)
3605 {
3606 long mid = (lo + hi) >> 1;
3607 if (syms[mid]->section->vma < ent)
3608 lo = mid + 1;
3609 else if (syms[mid]->section->vma > ent)
3610 hi = mid;
3611 else
3612 {
3613 sec = syms[mid]->section;
3614 break;
3615 }
3616 }
3617
3618 if (lo >= hi && lo > codesecsym)
3619 sec = syms[lo - 1]->section;
3620
3621 for (; sec != NULL; sec = sec->next)
3622 {
3623 if (sec->vma > ent)
3624 break;
3625 /* SEC_LOAD may not be set if SEC is from a separate debug
3626 info file. */
3627 if ((sec->flags & SEC_ALLOC) == 0)
3628 break;
3629 if ((sec->flags & SEC_CODE) != 0)
3630 s->section = sec;
3631 }
3632 s->flags |= BSF_SYNTHETIC;
3633 s->value = ent - s->section->vma;
3634 s->name = names;
3635 *names++ = '.';
3636 len = strlen (syms[i]->name);
3637 memcpy (names, syms[i]->name, len + 1);
3638 names += len + 1;
3639 /* Have udata.p point back to the original symbol this
3640 synthetic symbol was derived from. */
3641 s->udata.p = syms[i];
3642 s++;
3643 }
3644 }
3645 free (contents);
3646
3647 if (glink != NULL && relplt != NULL)
3648 {
3649 if (resolv_vma)
3650 {
3651 /* Add a symbol for the main glink trampoline. */
3652 memset (s, 0, sizeof *s);
3653 s->the_bfd = abfd;
3654 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3655 s->section = glink;
3656 s->value = resolv_vma - glink->vma;
3657 s->name = names;
3658 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3659 names += sizeof ("__glink_PLTresolve");
3660 s++;
3661 count++;
3662 }
3663
3664 /* FIXME: It would be very much nicer to put sym@plt on the
3665 stub rather than on the glink branch table entry. The
3666 objdump disassembler would then use a sensible symbol
3667 name on plt calls. The difficulty in doing so is
3668 a) finding the stubs, and,
3669 b) matching stubs against plt entries, and,
3670 c) there can be multiple stubs for a given plt entry.
3671
3672 Solving (a) could be done by code scanning, but older
3673 ppc64 binaries used different stubs to current code.
3674 (b) is the tricky one since you need to known the toc
3675 pointer for at least one function that uses a pic stub to
3676 be able to calculate the plt address referenced.
3677 (c) means gdb would need to set multiple breakpoints (or
3678 find the glink branch itself) when setting breakpoints
3679 for pending shared library loads. */
3680 p = relplt->relocation;
3681 for (i = 0; i < plt_count; i++, p++)
3682 {
3683 size_t len;
3684
3685 *s = **p->sym_ptr_ptr;
3686 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3687 we are defining a symbol, ensure one of them is set. */
3688 if ((s->flags & BSF_LOCAL) == 0)
3689 s->flags |= BSF_GLOBAL;
3690 s->flags |= BSF_SYNTHETIC;
3691 s->section = glink;
3692 s->value = glink_vma - glink->vma;
3693 s->name = names;
3694 s->udata.p = NULL;
3695 len = strlen ((*p->sym_ptr_ptr)->name);
3696 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3697 names += len;
3698 if (p->addend != 0)
3699 {
3700 memcpy (names, "+0x", sizeof ("+0x") - 1);
3701 names += sizeof ("+0x") - 1;
3702 bfd_sprintf_vma (abfd, names, p->addend);
3703 names += strlen (names);
3704 }
3705 memcpy (names, "@plt", sizeof ("@plt"));
3706 names += sizeof ("@plt");
3707 s++;
3708 if (abi < 2)
3709 {
3710 glink_vma += 8;
3711 if (i >= 0x8000)
3712 glink_vma += 4;
3713 }
3714 else
3715 glink_vma += 4;
3716 }
3717 count += plt_count;
3718 }
3719 }
3720
3721 done:
3722 free (syms);
3723 return count;
3724 }
3725 \f
3726 /* The following functions are specific to the ELF linker, while
3727 functions above are used generally. Those named ppc64_elf_* are
3728 called by the main ELF linker code. They appear in this file more
3729 or less in the order in which they are called. eg.
3730 ppc64_elf_check_relocs is called early in the link process,
3731 ppc64_elf_finish_dynamic_sections is one of the last functions
3732 called.
3733
3734 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3735 functions have both a function code symbol and a function descriptor
3736 symbol. A call to foo in a relocatable object file looks like:
3737
3738 . .text
3739 . x:
3740 . bl .foo
3741 . nop
3742
3743 The function definition in another object file might be:
3744
3745 . .section .opd
3746 . foo: .quad .foo
3747 . .quad .TOC.@tocbase
3748 . .quad 0
3749 .
3750 . .text
3751 . .foo: blr
3752
3753 When the linker resolves the call during a static link, the branch
3754 unsurprisingly just goes to .foo and the .opd information is unused.
3755 If the function definition is in a shared library, things are a little
3756 different: The call goes via a plt call stub, the opd information gets
3757 copied to the plt, and the linker patches the nop.
3758
3759 . x:
3760 . bl .foo_stub
3761 . ld 2,40(1)
3762 .
3763 .
3764 . .foo_stub:
3765 . std 2,40(1) # in practice, the call stub
3766 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3767 . addi 11,11,Lfoo@toc@l # this is the general idea
3768 . ld 12,0(11)
3769 . ld 2,8(11)
3770 . mtctr 12
3771 . ld 11,16(11)
3772 . bctr
3773 .
3774 . .section .plt
3775 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3776
3777 The "reloc ()" notation is supposed to indicate that the linker emits
3778 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3779 copying.
3780
3781 What are the difficulties here? Well, firstly, the relocations
3782 examined by the linker in check_relocs are against the function code
3783 sym .foo, while the dynamic relocation in the plt is emitted against
3784 the function descriptor symbol, foo. Somewhere along the line, we need
3785 to carefully copy dynamic link information from one symbol to the other.
3786 Secondly, the generic part of the elf linker will make .foo a dynamic
3787 symbol as is normal for most other backends. We need foo dynamic
3788 instead, at least for an application final link. However, when
3789 creating a shared library containing foo, we need to have both symbols
3790 dynamic so that references to .foo are satisfied during the early
3791 stages of linking. Otherwise the linker might decide to pull in a
3792 definition from some other object, eg. a static library.
3793
3794 Update: As of August 2004, we support a new convention. Function
3795 calls may use the function descriptor symbol, ie. "bl foo". This
3796 behaves exactly as "bl .foo". */
3797
3798 /* Of those relocs that might be copied as dynamic relocs, this
3799 function selects those that must be copied when linking a shared
3800 library or PIE, even when the symbol is local. */
3801
3802 static int
3803 must_be_dyn_reloc (struct bfd_link_info *info,
3804 enum elf_ppc64_reloc_type r_type)
3805 {
3806 switch (r_type)
3807 {
3808 default:
3809 /* Only relative relocs can be resolved when the object load
3810 address isn't fixed. DTPREL64 is excluded because the
3811 dynamic linker needs to differentiate global dynamic from
3812 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3813 return 1;
3814
3815 case R_PPC64_REL32:
3816 case R_PPC64_REL64:
3817 case R_PPC64_REL30:
3818 return 0;
3819
3820 case R_PPC64_TPREL16:
3821 case R_PPC64_TPREL16_LO:
3822 case R_PPC64_TPREL16_HI:
3823 case R_PPC64_TPREL16_HA:
3824 case R_PPC64_TPREL16_DS:
3825 case R_PPC64_TPREL16_LO_DS:
3826 case R_PPC64_TPREL16_HIGH:
3827 case R_PPC64_TPREL16_HIGHA:
3828 case R_PPC64_TPREL16_HIGHER:
3829 case R_PPC64_TPREL16_HIGHERA:
3830 case R_PPC64_TPREL16_HIGHEST:
3831 case R_PPC64_TPREL16_HIGHESTA:
3832 case R_PPC64_TPREL64:
3833 /* These relocations are relative but in a shared library the
3834 linker doesn't know the thread pointer base. */
3835 return bfd_link_dll (info);
3836 }
3837 }
3838
3839 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3840 copying dynamic variables from a shared lib into an app's dynbss
3841 section, and instead use a dynamic relocation to point into the
3842 shared lib. With code that gcc generates, it's vital that this be
3843 enabled; In the PowerPC64 ABI, the address of a function is actually
3844 the address of a function descriptor, which resides in the .opd
3845 section. gcc uses the descriptor directly rather than going via the
3846 GOT as some other ABI's do, which means that initialized function
3847 pointers must reference the descriptor. Thus, a function pointer
3848 initialized to the address of a function in a shared library will
3849 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3850 redefines the function descriptor symbol to point to the copy. This
3851 presents a problem as a plt entry for that function is also
3852 initialized from the function descriptor symbol and the copy reloc
3853 may not be initialized first. */
3854 #define ELIMINATE_COPY_RELOCS 1
3855
3856 /* Section name for stubs is the associated section name plus this
3857 string. */
3858 #define STUB_SUFFIX ".stub"
3859
3860 /* Linker stubs.
3861 ppc_stub_long_branch:
3862 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3863 destination, but a 24 bit branch in a stub section will reach.
3864 . b dest
3865
3866 ppc_stub_plt_branch:
3867 Similar to the above, but a 24 bit branch in the stub section won't
3868 reach its destination.
3869 . addis %r11,%r2,xxx@toc@ha
3870 . ld %r12,xxx@toc@l(%r11)
3871 . mtctr %r12
3872 . bctr
3873
3874 ppc_stub_plt_call:
3875 Used to call a function in a shared library. If it so happens that
3876 the plt entry referenced crosses a 64k boundary, then an extra
3877 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3878 . std %r2,40(%r1)
3879 . addis %r11,%r2,xxx@toc@ha
3880 . ld %r12,xxx+0@toc@l(%r11)
3881 . mtctr %r12
3882 . ld %r2,xxx+8@toc@l(%r11)
3883 . ld %r11,xxx+16@toc@l(%r11)
3884 . bctr
3885
3886 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3887 code to adjust the value and save r2 to support multiple toc sections.
3888 A ppc_stub_long_branch with an r2 offset looks like:
3889 . std %r2,40(%r1)
3890 . addis %r2,%r2,off@ha
3891 . addi %r2,%r2,off@l
3892 . b dest
3893
3894 A ppc_stub_plt_branch with an r2 offset looks like:
3895 . std %r2,40(%r1)
3896 . addis %r11,%r2,xxx@toc@ha
3897 . ld %r12,xxx@toc@l(%r11)
3898 . addis %r2,%r2,off@ha
3899 . addi %r2,%r2,off@l
3900 . mtctr %r12
3901 . bctr
3902
3903 In cases where the "addis" instruction would add zero, the "addis" is
3904 omitted and following instructions modified slightly in some cases.
3905 */
3906
3907 enum ppc_stub_type {
3908 ppc_stub_none,
3909 ppc_stub_long_branch,
3910 ppc_stub_long_branch_r2off,
3911 ppc_stub_plt_branch,
3912 ppc_stub_plt_branch_r2off,
3913 ppc_stub_plt_call,
3914 ppc_stub_plt_call_r2save,
3915 ppc_stub_global_entry,
3916 ppc_stub_save_res
3917 };
3918
3919 /* Information on stub grouping. */
3920 struct map_stub
3921 {
3922 /* The stub section. */
3923 asection *stub_sec;
3924 /* This is the section to which stubs in the group will be attached. */
3925 asection *link_sec;
3926 /* Next group. */
3927 struct map_stub *next;
3928 /* Whether to emit a copy of register save/restore functions in this
3929 group. */
3930 int needs_save_res;
3931 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
3932 or -1u if no such stub with bctrl exists. */
3933 unsigned int tls_get_addr_opt_bctrl;
3934 };
3935
3936 struct ppc_stub_hash_entry {
3937
3938 /* Base hash table entry structure. */
3939 struct bfd_hash_entry root;
3940
3941 enum ppc_stub_type stub_type;
3942
3943 /* Group information. */
3944 struct map_stub *group;
3945
3946 /* Offset within stub_sec of the beginning of this stub. */
3947 bfd_vma stub_offset;
3948
3949 /* Given the symbol's value and its section we can determine its final
3950 value when building the stubs (so the stub knows where to jump. */
3951 bfd_vma target_value;
3952 asection *target_section;
3953
3954 /* The symbol table entry, if any, that this was derived from. */
3955 struct ppc_link_hash_entry *h;
3956 struct plt_entry *plt_ent;
3957
3958 /* Symbol st_other. */
3959 unsigned char other;
3960 };
3961
3962 struct ppc_branch_hash_entry {
3963
3964 /* Base hash table entry structure. */
3965 struct bfd_hash_entry root;
3966
3967 /* Offset within branch lookup table. */
3968 unsigned int offset;
3969
3970 /* Generation marker. */
3971 unsigned int iter;
3972 };
3973
3974 /* Used to track dynamic relocations for local symbols. */
3975 struct ppc_dyn_relocs
3976 {
3977 struct ppc_dyn_relocs *next;
3978
3979 /* The input section of the reloc. */
3980 asection *sec;
3981
3982 /* Total number of relocs copied for the input section. */
3983 unsigned int count : 31;
3984
3985 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3986 unsigned int ifunc : 1;
3987 };
3988
3989 struct ppc_link_hash_entry
3990 {
3991 struct elf_link_hash_entry elf;
3992
3993 union {
3994 /* A pointer to the most recently used stub hash entry against this
3995 symbol. */
3996 struct ppc_stub_hash_entry *stub_cache;
3997
3998 /* A pointer to the next symbol starting with a '.' */
3999 struct ppc_link_hash_entry *next_dot_sym;
4000 } u;
4001
4002 /* Track dynamic relocs copied for this symbol. */
4003 struct elf_dyn_relocs *dyn_relocs;
4004
4005 /* Link between function code and descriptor symbols. */
4006 struct ppc_link_hash_entry *oh;
4007
4008 /* Flag function code and descriptor symbols. */
4009 unsigned int is_func:1;
4010 unsigned int is_func_descriptor:1;
4011 unsigned int fake:1;
4012
4013 /* Whether global opd/toc sym has been adjusted or not.
4014 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4015 should be set for all globals defined in any opd/toc section. */
4016 unsigned int adjust_done:1;
4017
4018 /* Set if this is an out-of-line register save/restore function,
4019 with non-standard calling convention. */
4020 unsigned int save_res:1;
4021
4022 /* Set if a duplicate symbol with non-zero localentry is detected,
4023 even when the duplicate symbol does not provide a definition. */
4024 unsigned int non_zero_localentry:1;
4025
4026 /* Contexts in which symbol is used in the GOT (or TOC).
4027 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
4028 corresponding relocs are encountered during check_relocs.
4029 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
4030 indicate the corresponding GOT entry type is not needed.
4031 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
4032 a TPREL one. We use a separate flag rather than setting TPREL
4033 just for convenience in distinguishing the two cases. */
4034 #define TLS_GD 1 /* GD reloc. */
4035 #define TLS_LD 2 /* LD reloc. */
4036 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
4037 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
4038 #define TLS_TLS 16 /* Any TLS reloc. */
4039 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
4040 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4041 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
4042 unsigned char tls_mask;
4043 };
4044
4045 /* ppc64 ELF linker hash table. */
4046
4047 struct ppc_link_hash_table
4048 {
4049 struct elf_link_hash_table elf;
4050
4051 /* The stub hash table. */
4052 struct bfd_hash_table stub_hash_table;
4053
4054 /* Another hash table for plt_branch stubs. */
4055 struct bfd_hash_table branch_hash_table;
4056
4057 /* Hash table for function prologue tocsave. */
4058 htab_t tocsave_htab;
4059
4060 /* Various options and other info passed from the linker. */
4061 struct ppc64_elf_params *params;
4062
4063 /* The size of sec_info below. */
4064 unsigned int sec_info_arr_size;
4065
4066 /* Per-section array of extra section info. Done this way rather
4067 than as part of ppc64_elf_section_data so we have the info for
4068 non-ppc64 sections. */
4069 struct
4070 {
4071 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4072 bfd_vma toc_off;
4073
4074 union
4075 {
4076 /* The section group that this section belongs to. */
4077 struct map_stub *group;
4078 /* A temp section list pointer. */
4079 asection *list;
4080 } u;
4081 } *sec_info;
4082
4083 /* Linked list of groups. */
4084 struct map_stub *group;
4085
4086 /* Temp used when calculating TOC pointers. */
4087 bfd_vma toc_curr;
4088 bfd *toc_bfd;
4089 asection *toc_first_sec;
4090
4091 /* Used when adding symbols. */
4092 struct ppc_link_hash_entry *dot_syms;
4093
4094 /* Shortcuts to get to dynamic linker sections. */
4095 asection *glink;
4096 asection *global_entry;
4097 asection *sfpr;
4098 asection *brlt;
4099 asection *relbrlt;
4100 asection *glink_eh_frame;
4101
4102 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4103 struct ppc_link_hash_entry *tls_get_addr;
4104 struct ppc_link_hash_entry *tls_get_addr_fd;
4105
4106 /* The size of reliplt used by got entry relocs. */
4107 bfd_size_type got_reli_size;
4108
4109 /* Statistics. */
4110 unsigned long stub_count[ppc_stub_global_entry];
4111
4112 /* Number of stubs against global syms. */
4113 unsigned long stub_globals;
4114
4115 /* Set if we're linking code with function descriptors. */
4116 unsigned int opd_abi:1;
4117
4118 /* Support for multiple toc sections. */
4119 unsigned int do_multi_toc:1;
4120 unsigned int multi_toc_needed:1;
4121 unsigned int second_toc_pass:1;
4122 unsigned int do_toc_opt:1;
4123
4124 /* Set if tls optimization is enabled. */
4125 unsigned int do_tls_opt:1;
4126
4127 /* Set on error. */
4128 unsigned int stub_error:1;
4129
4130 /* Whether func_desc_adjust needs to be run over symbols. */
4131 unsigned int need_func_desc_adj:1;
4132
4133 /* Whether there exist local gnu indirect function resolvers,
4134 referenced by dynamic relocations. */
4135 unsigned int local_ifunc_resolver:1;
4136 unsigned int maybe_local_ifunc_resolver:1;
4137
4138 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4139 unsigned int has_plt_localentry0:1;
4140
4141 /* Incremented every time we size stubs. */
4142 unsigned int stub_iteration;
4143
4144 /* Small local sym cache. */
4145 struct sym_cache sym_cache;
4146 };
4147
4148 /* Rename some of the generic section flags to better document how they
4149 are used here. */
4150
4151 /* Nonzero if this section has TLS related relocations. */
4152 #define has_tls_reloc sec_flg0
4153
4154 /* Nonzero if this section has a call to __tls_get_addr. */
4155 #define has_tls_get_addr_call sec_flg1
4156
4157 /* Nonzero if this section has any toc or got relocs. */
4158 #define has_toc_reloc sec_flg2
4159
4160 /* Nonzero if this section has a call to another section that uses
4161 the toc or got. */
4162 #define makes_toc_func_call sec_flg3
4163
4164 /* Recursion protection when determining above flag. */
4165 #define call_check_in_progress sec_flg4
4166 #define call_check_done sec_flg5
4167
4168 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4169
4170 #define ppc_hash_table(p) \
4171 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4172 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4173
4174 #define ppc_stub_hash_lookup(table, string, create, copy) \
4175 ((struct ppc_stub_hash_entry *) \
4176 bfd_hash_lookup ((table), (string), (create), (copy)))
4177
4178 #define ppc_branch_hash_lookup(table, string, create, copy) \
4179 ((struct ppc_branch_hash_entry *) \
4180 bfd_hash_lookup ((table), (string), (create), (copy)))
4181
4182 /* Create an entry in the stub hash table. */
4183
4184 static struct bfd_hash_entry *
4185 stub_hash_newfunc (struct bfd_hash_entry *entry,
4186 struct bfd_hash_table *table,
4187 const char *string)
4188 {
4189 /* Allocate the structure if it has not already been allocated by a
4190 subclass. */
4191 if (entry == NULL)
4192 {
4193 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4194 if (entry == NULL)
4195 return entry;
4196 }
4197
4198 /* Call the allocation method of the superclass. */
4199 entry = bfd_hash_newfunc (entry, table, string);
4200 if (entry != NULL)
4201 {
4202 struct ppc_stub_hash_entry *eh;
4203
4204 /* Initialize the local fields. */
4205 eh = (struct ppc_stub_hash_entry *) entry;
4206 eh->stub_type = ppc_stub_none;
4207 eh->group = NULL;
4208 eh->stub_offset = 0;
4209 eh->target_value = 0;
4210 eh->target_section = NULL;
4211 eh->h = NULL;
4212 eh->plt_ent = NULL;
4213 eh->other = 0;
4214 }
4215
4216 return entry;
4217 }
4218
4219 /* Create an entry in the branch hash table. */
4220
4221 static struct bfd_hash_entry *
4222 branch_hash_newfunc (struct bfd_hash_entry *entry,
4223 struct bfd_hash_table *table,
4224 const char *string)
4225 {
4226 /* Allocate the structure if it has not already been allocated by a
4227 subclass. */
4228 if (entry == NULL)
4229 {
4230 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4231 if (entry == NULL)
4232 return entry;
4233 }
4234
4235 /* Call the allocation method of the superclass. */
4236 entry = bfd_hash_newfunc (entry, table, string);
4237 if (entry != NULL)
4238 {
4239 struct ppc_branch_hash_entry *eh;
4240
4241 /* Initialize the local fields. */
4242 eh = (struct ppc_branch_hash_entry *) entry;
4243 eh->offset = 0;
4244 eh->iter = 0;
4245 }
4246
4247 return entry;
4248 }
4249
4250 /* Create an entry in a ppc64 ELF linker hash table. */
4251
4252 static struct bfd_hash_entry *
4253 link_hash_newfunc (struct bfd_hash_entry *entry,
4254 struct bfd_hash_table *table,
4255 const char *string)
4256 {
4257 /* Allocate the structure if it has not already been allocated by a
4258 subclass. */
4259 if (entry == NULL)
4260 {
4261 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4262 if (entry == NULL)
4263 return entry;
4264 }
4265
4266 /* Call the allocation method of the superclass. */
4267 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4268 if (entry != NULL)
4269 {
4270 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4271
4272 memset (&eh->u.stub_cache, 0,
4273 (sizeof (struct ppc_link_hash_entry)
4274 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4275
4276 /* When making function calls, old ABI code references function entry
4277 points (dot symbols), while new ABI code references the function
4278 descriptor symbol. We need to make any combination of reference and
4279 definition work together, without breaking archive linking.
4280
4281 For a defined function "foo" and an undefined call to "bar":
4282 An old object defines "foo" and ".foo", references ".bar" (possibly
4283 "bar" too).
4284 A new object defines "foo" and references "bar".
4285
4286 A new object thus has no problem with its undefined symbols being
4287 satisfied by definitions in an old object. On the other hand, the
4288 old object won't have ".bar" satisfied by a new object.
4289
4290 Keep a list of newly added dot-symbols. */
4291
4292 if (string[0] == '.')
4293 {
4294 struct ppc_link_hash_table *htab;
4295
4296 htab = (struct ppc_link_hash_table *) table;
4297 eh->u.next_dot_sym = htab->dot_syms;
4298 htab->dot_syms = eh;
4299 }
4300 }
4301
4302 return entry;
4303 }
4304
4305 struct tocsave_entry {
4306 asection *sec;
4307 bfd_vma offset;
4308 };
4309
4310 static hashval_t
4311 tocsave_htab_hash (const void *p)
4312 {
4313 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4314 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4315 }
4316
4317 static int
4318 tocsave_htab_eq (const void *p1, const void *p2)
4319 {
4320 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4321 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4322 return e1->sec == e2->sec && e1->offset == e2->offset;
4323 }
4324
4325 /* Destroy a ppc64 ELF linker hash table. */
4326
4327 static void
4328 ppc64_elf_link_hash_table_free (bfd *obfd)
4329 {
4330 struct ppc_link_hash_table *htab;
4331
4332 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4333 if (htab->tocsave_htab)
4334 htab_delete (htab->tocsave_htab);
4335 bfd_hash_table_free (&htab->branch_hash_table);
4336 bfd_hash_table_free (&htab->stub_hash_table);
4337 _bfd_elf_link_hash_table_free (obfd);
4338 }
4339
4340 /* Create a ppc64 ELF linker hash table. */
4341
4342 static struct bfd_link_hash_table *
4343 ppc64_elf_link_hash_table_create (bfd *abfd)
4344 {
4345 struct ppc_link_hash_table *htab;
4346 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4347
4348 htab = bfd_zmalloc (amt);
4349 if (htab == NULL)
4350 return NULL;
4351
4352 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4353 sizeof (struct ppc_link_hash_entry),
4354 PPC64_ELF_DATA))
4355 {
4356 free (htab);
4357 return NULL;
4358 }
4359
4360 /* Init the stub hash table too. */
4361 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4362 sizeof (struct ppc_stub_hash_entry)))
4363 {
4364 _bfd_elf_link_hash_table_free (abfd);
4365 return NULL;
4366 }
4367
4368 /* And the branch hash table. */
4369 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4370 sizeof (struct ppc_branch_hash_entry)))
4371 {
4372 bfd_hash_table_free (&htab->stub_hash_table);
4373 _bfd_elf_link_hash_table_free (abfd);
4374 return NULL;
4375 }
4376
4377 htab->tocsave_htab = htab_try_create (1024,
4378 tocsave_htab_hash,
4379 tocsave_htab_eq,
4380 NULL);
4381 if (htab->tocsave_htab == NULL)
4382 {
4383 ppc64_elf_link_hash_table_free (abfd);
4384 return NULL;
4385 }
4386 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4387
4388 /* Initializing two fields of the union is just cosmetic. We really
4389 only care about glist, but when compiled on a 32-bit host the
4390 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4391 debugger inspection of these fields look nicer. */
4392 htab->elf.init_got_refcount.refcount = 0;
4393 htab->elf.init_got_refcount.glist = NULL;
4394 htab->elf.init_plt_refcount.refcount = 0;
4395 htab->elf.init_plt_refcount.glist = NULL;
4396 htab->elf.init_got_offset.offset = 0;
4397 htab->elf.init_got_offset.glist = NULL;
4398 htab->elf.init_plt_offset.offset = 0;
4399 htab->elf.init_plt_offset.glist = NULL;
4400
4401 return &htab->elf.root;
4402 }
4403
4404 /* Create sections for linker generated code. */
4405
4406 static bfd_boolean
4407 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4408 {
4409 struct ppc_link_hash_table *htab;
4410 flagword flags;
4411
4412 htab = ppc_hash_table (info);
4413
4414 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4415 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4416 if (htab->params->save_restore_funcs)
4417 {
4418 /* Create .sfpr for code to save and restore fp regs. */
4419 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4420 flags);
4421 if (htab->sfpr == NULL
4422 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4423 return FALSE;
4424 }
4425
4426 if (bfd_link_relocatable (info))
4427 return TRUE;
4428
4429 /* Create .glink for lazy dynamic linking support. */
4430 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4431 flags);
4432 if (htab->glink == NULL
4433 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4434 return FALSE;
4435
4436 /* The part of .glink used by global entry stubs, separate so that
4437 it can be aligned appropriately without affecting htab->glink. */
4438 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4439 flags);
4440 if (htab->global_entry == NULL
4441 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4442 return FALSE;
4443
4444 if (!info->no_ld_generated_unwind_info)
4445 {
4446 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4447 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4448 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4449 ".eh_frame",
4450 flags);
4451 if (htab->glink_eh_frame == NULL
4452 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4453 return FALSE;
4454 }
4455
4456 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4457 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4458 if (htab->elf.iplt == NULL
4459 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4460 return FALSE;
4461
4462 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4463 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4464 htab->elf.irelplt
4465 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4466 if (htab->elf.irelplt == NULL
4467 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4468 return FALSE;
4469
4470 /* Create branch lookup table for plt_branch stubs. */
4471 flags = (SEC_ALLOC | SEC_LOAD
4472 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4473 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4474 flags);
4475 if (htab->brlt == NULL
4476 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4477 return FALSE;
4478
4479 if (!bfd_link_pic (info))
4480 return TRUE;
4481
4482 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4483 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4484 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4485 ".rela.branch_lt",
4486 flags);
4487 if (htab->relbrlt == NULL
4488 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4489 return FALSE;
4490
4491 return TRUE;
4492 }
4493
4494 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4495
4496 bfd_boolean
4497 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4498 struct ppc64_elf_params *params)
4499 {
4500 struct ppc_link_hash_table *htab;
4501
4502 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4503
4504 /* Always hook our dynamic sections into the first bfd, which is the
4505 linker created stub bfd. This ensures that the GOT header is at
4506 the start of the output TOC section. */
4507 htab = ppc_hash_table (info);
4508 htab->elf.dynobj = params->stub_bfd;
4509 htab->params = params;
4510
4511 return create_linkage_sections (htab->elf.dynobj, info);
4512 }
4513
4514 /* Build a name for an entry in the stub hash table. */
4515
4516 static char *
4517 ppc_stub_name (const asection *input_section,
4518 const asection *sym_sec,
4519 const struct ppc_link_hash_entry *h,
4520 const Elf_Internal_Rela *rel)
4521 {
4522 char *stub_name;
4523 ssize_t len;
4524
4525 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4526 offsets from a sym as a branch target? In fact, we could
4527 probably assume the addend is always zero. */
4528 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4529
4530 if (h)
4531 {
4532 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4533 stub_name = bfd_malloc (len);
4534 if (stub_name == NULL)
4535 return stub_name;
4536
4537 len = sprintf (stub_name, "%08x.%s+%x",
4538 input_section->id & 0xffffffff,
4539 h->elf.root.root.string,
4540 (int) rel->r_addend & 0xffffffff);
4541 }
4542 else
4543 {
4544 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4545 stub_name = bfd_malloc (len);
4546 if (stub_name == NULL)
4547 return stub_name;
4548
4549 len = sprintf (stub_name, "%08x.%x:%x+%x",
4550 input_section->id & 0xffffffff,
4551 sym_sec->id & 0xffffffff,
4552 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4553 (int) rel->r_addend & 0xffffffff);
4554 }
4555 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4556 stub_name[len - 2] = 0;
4557 return stub_name;
4558 }
4559
4560 /* Look up an entry in the stub hash. Stub entries are cached because
4561 creating the stub name takes a bit of time. */
4562
4563 static struct ppc_stub_hash_entry *
4564 ppc_get_stub_entry (const asection *input_section,
4565 const asection *sym_sec,
4566 struct ppc_link_hash_entry *h,
4567 const Elf_Internal_Rela *rel,
4568 struct ppc_link_hash_table *htab)
4569 {
4570 struct ppc_stub_hash_entry *stub_entry;
4571 struct map_stub *group;
4572
4573 /* If this input section is part of a group of sections sharing one
4574 stub section, then use the id of the first section in the group.
4575 Stub names need to include a section id, as there may well be
4576 more than one stub used to reach say, printf, and we need to
4577 distinguish between them. */
4578 group = htab->sec_info[input_section->id].u.group;
4579 if (group == NULL)
4580 return NULL;
4581
4582 if (h != NULL && h->u.stub_cache != NULL
4583 && h->u.stub_cache->h == h
4584 && h->u.stub_cache->group == group)
4585 {
4586 stub_entry = h->u.stub_cache;
4587 }
4588 else
4589 {
4590 char *stub_name;
4591
4592 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4593 if (stub_name == NULL)
4594 return NULL;
4595
4596 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4597 stub_name, FALSE, FALSE);
4598 if (h != NULL)
4599 h->u.stub_cache = stub_entry;
4600
4601 free (stub_name);
4602 }
4603
4604 return stub_entry;
4605 }
4606
4607 /* Add a new stub entry to the stub hash. Not all fields of the new
4608 stub entry are initialised. */
4609
4610 static struct ppc_stub_hash_entry *
4611 ppc_add_stub (const char *stub_name,
4612 asection *section,
4613 struct bfd_link_info *info)
4614 {
4615 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4616 struct map_stub *group;
4617 asection *link_sec;
4618 asection *stub_sec;
4619 struct ppc_stub_hash_entry *stub_entry;
4620
4621 group = htab->sec_info[section->id].u.group;
4622 link_sec = group->link_sec;
4623 stub_sec = group->stub_sec;
4624 if (stub_sec == NULL)
4625 {
4626 size_t namelen;
4627 bfd_size_type len;
4628 char *s_name;
4629
4630 namelen = strlen (link_sec->name);
4631 len = namelen + sizeof (STUB_SUFFIX);
4632 s_name = bfd_alloc (htab->params->stub_bfd, len);
4633 if (s_name == NULL)
4634 return NULL;
4635
4636 memcpy (s_name, link_sec->name, namelen);
4637 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4638 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4639 if (stub_sec == NULL)
4640 return NULL;
4641 group->stub_sec = stub_sec;
4642 }
4643
4644 /* Enter this entry into the linker stub hash table. */
4645 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4646 TRUE, FALSE);
4647 if (stub_entry == NULL)
4648 {
4649 /* xgettext:c-format */
4650 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4651 section->owner, stub_name);
4652 return NULL;
4653 }
4654
4655 stub_entry->group = group;
4656 stub_entry->stub_offset = 0;
4657 return stub_entry;
4658 }
4659
4660 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4661 not already done. */
4662
4663 static bfd_boolean
4664 create_got_section (bfd *abfd, struct bfd_link_info *info)
4665 {
4666 asection *got, *relgot;
4667 flagword flags;
4668 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4669
4670 if (!is_ppc64_elf (abfd))
4671 return FALSE;
4672 if (htab == NULL)
4673 return FALSE;
4674
4675 if (!htab->elf.sgot
4676 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4677 return FALSE;
4678
4679 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4680 | SEC_LINKER_CREATED);
4681
4682 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4683 if (!got
4684 || !bfd_set_section_alignment (abfd, got, 3))
4685 return FALSE;
4686
4687 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4688 flags | SEC_READONLY);
4689 if (!relgot
4690 || ! bfd_set_section_alignment (abfd, relgot, 3))
4691 return FALSE;
4692
4693 ppc64_elf_tdata (abfd)->got = got;
4694 ppc64_elf_tdata (abfd)->relgot = relgot;
4695 return TRUE;
4696 }
4697
4698 /* Follow indirect and warning symbol links. */
4699
4700 static inline struct bfd_link_hash_entry *
4701 follow_link (struct bfd_link_hash_entry *h)
4702 {
4703 while (h->type == bfd_link_hash_indirect
4704 || h->type == bfd_link_hash_warning)
4705 h = h->u.i.link;
4706 return h;
4707 }
4708
4709 static inline struct elf_link_hash_entry *
4710 elf_follow_link (struct elf_link_hash_entry *h)
4711 {
4712 return (struct elf_link_hash_entry *) follow_link (&h->root);
4713 }
4714
4715 static inline struct ppc_link_hash_entry *
4716 ppc_follow_link (struct ppc_link_hash_entry *h)
4717 {
4718 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4719 }
4720
4721 /* Merge PLT info on FROM with that on TO. */
4722
4723 static void
4724 move_plt_plist (struct ppc_link_hash_entry *from,
4725 struct ppc_link_hash_entry *to)
4726 {
4727 if (from->elf.plt.plist != NULL)
4728 {
4729 if (to->elf.plt.plist != NULL)
4730 {
4731 struct plt_entry **entp;
4732 struct plt_entry *ent;
4733
4734 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4735 {
4736 struct plt_entry *dent;
4737
4738 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4739 if (dent->addend == ent->addend)
4740 {
4741 dent->plt.refcount += ent->plt.refcount;
4742 *entp = ent->next;
4743 break;
4744 }
4745 if (dent == NULL)
4746 entp = &ent->next;
4747 }
4748 *entp = to->elf.plt.plist;
4749 }
4750
4751 to->elf.plt.plist = from->elf.plt.plist;
4752 from->elf.plt.plist = NULL;
4753 }
4754 }
4755
4756 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4757
4758 static void
4759 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4760 struct elf_link_hash_entry *dir,
4761 struct elf_link_hash_entry *ind)
4762 {
4763 struct ppc_link_hash_entry *edir, *eind;
4764
4765 edir = (struct ppc_link_hash_entry *) dir;
4766 eind = (struct ppc_link_hash_entry *) ind;
4767
4768 edir->is_func |= eind->is_func;
4769 edir->is_func_descriptor |= eind->is_func_descriptor;
4770 edir->tls_mask |= eind->tls_mask;
4771 if (eind->oh != NULL)
4772 edir->oh = ppc_follow_link (eind->oh);
4773
4774 if (edir->elf.versioned != versioned_hidden)
4775 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4776 edir->elf.ref_regular |= eind->elf.ref_regular;
4777 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4778 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4779 edir->elf.needs_plt |= eind->elf.needs_plt;
4780 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4781
4782 /* If we were called to copy over info for a weak sym, don't copy
4783 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4784 in order to simplify readonly_dynrelocs and save a field in the
4785 symbol hash entry, but that means dyn_relocs can't be used in any
4786 tests about a specific symbol, or affect other symbol flags which
4787 are then tested. */
4788 if (eind->elf.root.type != bfd_link_hash_indirect)
4789 return;
4790
4791 /* Copy over any dynamic relocs we may have on the indirect sym. */
4792 if (eind->dyn_relocs != NULL)
4793 {
4794 if (edir->dyn_relocs != NULL)
4795 {
4796 struct elf_dyn_relocs **pp;
4797 struct elf_dyn_relocs *p;
4798
4799 /* Add reloc counts against the indirect sym to the direct sym
4800 list. Merge any entries against the same section. */
4801 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4802 {
4803 struct elf_dyn_relocs *q;
4804
4805 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4806 if (q->sec == p->sec)
4807 {
4808 q->pc_count += p->pc_count;
4809 q->count += p->count;
4810 *pp = p->next;
4811 break;
4812 }
4813 if (q == NULL)
4814 pp = &p->next;
4815 }
4816 *pp = edir->dyn_relocs;
4817 }
4818
4819 edir->dyn_relocs = eind->dyn_relocs;
4820 eind->dyn_relocs = NULL;
4821 }
4822
4823 /* Copy over got entries that we may have already seen to the
4824 symbol which just became indirect. */
4825 if (eind->elf.got.glist != NULL)
4826 {
4827 if (edir->elf.got.glist != NULL)
4828 {
4829 struct got_entry **entp;
4830 struct got_entry *ent;
4831
4832 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4833 {
4834 struct got_entry *dent;
4835
4836 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4837 if (dent->addend == ent->addend
4838 && dent->owner == ent->owner
4839 && dent->tls_type == ent->tls_type)
4840 {
4841 dent->got.refcount += ent->got.refcount;
4842 *entp = ent->next;
4843 break;
4844 }
4845 if (dent == NULL)
4846 entp = &ent->next;
4847 }
4848 *entp = edir->elf.got.glist;
4849 }
4850
4851 edir->elf.got.glist = eind->elf.got.glist;
4852 eind->elf.got.glist = NULL;
4853 }
4854
4855 /* And plt entries. */
4856 move_plt_plist (eind, edir);
4857
4858 if (eind->elf.dynindx != -1)
4859 {
4860 if (edir->elf.dynindx != -1)
4861 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4862 edir->elf.dynstr_index);
4863 edir->elf.dynindx = eind->elf.dynindx;
4864 edir->elf.dynstr_index = eind->elf.dynstr_index;
4865 eind->elf.dynindx = -1;
4866 eind->elf.dynstr_index = 0;
4867 }
4868 }
4869
4870 /* Find the function descriptor hash entry from the given function code
4871 hash entry FH. Link the entries via their OH fields. */
4872
4873 static struct ppc_link_hash_entry *
4874 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4875 {
4876 struct ppc_link_hash_entry *fdh = fh->oh;
4877
4878 if (fdh == NULL)
4879 {
4880 const char *fd_name = fh->elf.root.root.string + 1;
4881
4882 fdh = (struct ppc_link_hash_entry *)
4883 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4884 if (fdh == NULL)
4885 return fdh;
4886
4887 fdh->is_func_descriptor = 1;
4888 fdh->oh = fh;
4889 fh->is_func = 1;
4890 fh->oh = fdh;
4891 }
4892
4893 fdh = ppc_follow_link (fdh);
4894 fdh->is_func_descriptor = 1;
4895 fdh->oh = fh;
4896 return fdh;
4897 }
4898
4899 /* Make a fake function descriptor sym for the undefined code sym FH. */
4900
4901 static struct ppc_link_hash_entry *
4902 make_fdh (struct bfd_link_info *info,
4903 struct ppc_link_hash_entry *fh)
4904 {
4905 bfd *abfd = fh->elf.root.u.undef.abfd;
4906 struct bfd_link_hash_entry *bh = NULL;
4907 struct ppc_link_hash_entry *fdh;
4908 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4909 ? BSF_WEAK
4910 : BSF_GLOBAL);
4911
4912 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4913 fh->elf.root.root.string + 1,
4914 flags, bfd_und_section_ptr, 0,
4915 NULL, FALSE, FALSE, &bh))
4916 return NULL;
4917
4918 fdh = (struct ppc_link_hash_entry *) bh;
4919 fdh->elf.non_elf = 0;
4920 fdh->fake = 1;
4921 fdh->is_func_descriptor = 1;
4922 fdh->oh = fh;
4923 fh->is_func = 1;
4924 fh->oh = fdh;
4925 return fdh;
4926 }
4927
4928 /* Fix function descriptor symbols defined in .opd sections to be
4929 function type. */
4930
4931 static bfd_boolean
4932 ppc64_elf_add_symbol_hook (bfd *ibfd,
4933 struct bfd_link_info *info,
4934 Elf_Internal_Sym *isym,
4935 const char **name,
4936 flagword *flags ATTRIBUTE_UNUSED,
4937 asection **sec,
4938 bfd_vma *value)
4939 {
4940 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4941 && (ibfd->flags & DYNAMIC) == 0
4942 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
4943 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_ifunc;
4944
4945 if (*sec != NULL
4946 && strcmp ((*sec)->name, ".opd") == 0)
4947 {
4948 asection *code_sec;
4949
4950 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4951 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4952 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4953
4954 /* If the symbol is a function defined in .opd, and the function
4955 code is in a discarded group, let it appear to be undefined. */
4956 if (!bfd_link_relocatable (info)
4957 && (*sec)->reloc_count != 0
4958 && opd_entry_value (*sec, *value, &code_sec, NULL,
4959 FALSE) != (bfd_vma) -1
4960 && discarded_section (code_sec))
4961 {
4962 *sec = bfd_und_section_ptr;
4963 isym->st_shndx = SHN_UNDEF;
4964 }
4965 }
4966 else if (*sec != NULL
4967 && strcmp ((*sec)->name, ".toc") == 0
4968 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4969 {
4970 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4971 if (htab != NULL)
4972 htab->params->object_in_toc = 1;
4973 }
4974
4975 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4976 {
4977 if (abiversion (ibfd) == 0)
4978 set_abiversion (ibfd, 2);
4979 else if (abiversion (ibfd) == 1)
4980 {
4981 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4982 " for ABI version 1\n"), name);
4983 bfd_set_error (bfd_error_bad_value);
4984 return FALSE;
4985 }
4986 }
4987
4988 return TRUE;
4989 }
4990
4991 /* Merge non-visibility st_other attributes: local entry point. */
4992
4993 static void
4994 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4995 const Elf_Internal_Sym *isym,
4996 bfd_boolean definition,
4997 bfd_boolean dynamic)
4998 {
4999 if (definition && (!dynamic || !h->def_regular))
5000 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5001 | ELF_ST_VISIBILITY (h->other));
5002 }
5003
5004 /* Hook called on merging a symbol. We use this to clear "fake" since
5005 we now have a real symbol. */
5006
5007 static bfd_boolean
5008 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5009 const Elf_Internal_Sym *isym,
5010 asection **psec ATTRIBUTE_UNUSED,
5011 bfd_boolean newdef ATTRIBUTE_UNUSED,
5012 bfd_boolean olddef ATTRIBUTE_UNUSED,
5013 bfd *oldbfd ATTRIBUTE_UNUSED,
5014 const asection *oldsec ATTRIBUTE_UNUSED)
5015 {
5016 ((struct ppc_link_hash_entry *) h)->fake = 0;
5017 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5018 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5019 return TRUE;
5020 }
5021
5022 /* This function makes an old ABI object reference to ".bar" cause the
5023 inclusion of a new ABI object archive that defines "bar".
5024 NAME is a symbol defined in an archive. Return a symbol in the hash
5025 table that might be satisfied by the archive symbols. */
5026
5027 static struct elf_link_hash_entry *
5028 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5029 struct bfd_link_info *info,
5030 const char *name)
5031 {
5032 struct elf_link_hash_entry *h;
5033 char *dot_name;
5034 size_t len;
5035
5036 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5037 if (h != NULL
5038 /* Don't return this sym if it is a fake function descriptor
5039 created by add_symbol_adjust. */
5040 && !((struct ppc_link_hash_entry *) h)->fake)
5041 return h;
5042
5043 if (name[0] == '.')
5044 return h;
5045
5046 len = strlen (name);
5047 dot_name = bfd_alloc (abfd, len + 2);
5048 if (dot_name == NULL)
5049 return (struct elf_link_hash_entry *) -1;
5050 dot_name[0] = '.';
5051 memcpy (dot_name + 1, name, len + 1);
5052 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5053 bfd_release (abfd, dot_name);
5054 return h;
5055 }
5056
5057 /* This function satisfies all old ABI object references to ".bar" if a
5058 new ABI object defines "bar". Well, at least, undefined dot symbols
5059 are made weak. This stops later archive searches from including an
5060 object if we already have a function descriptor definition. It also
5061 prevents the linker complaining about undefined symbols.
5062 We also check and correct mismatched symbol visibility here. The
5063 most restrictive visibility of the function descriptor and the
5064 function entry symbol is used. */
5065
5066 static bfd_boolean
5067 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5068 {
5069 struct ppc_link_hash_table *htab;
5070 struct ppc_link_hash_entry *fdh;
5071
5072 if (eh->elf.root.type == bfd_link_hash_warning)
5073 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5074
5075 if (eh->elf.root.type == bfd_link_hash_indirect)
5076 return TRUE;
5077
5078 if (eh->elf.root.root.string[0] != '.')
5079 abort ();
5080
5081 htab = ppc_hash_table (info);
5082 if (htab == NULL)
5083 return FALSE;
5084
5085 fdh = lookup_fdh (eh, htab);
5086 if (fdh == NULL
5087 && !bfd_link_relocatable (info)
5088 && (eh->elf.root.type == bfd_link_hash_undefined
5089 || eh->elf.root.type == bfd_link_hash_undefweak)
5090 && eh->elf.ref_regular)
5091 {
5092 /* Make an undefined function descriptor sym, in order to
5093 pull in an --as-needed shared lib. Archives are handled
5094 elsewhere. */
5095 fdh = make_fdh (info, eh);
5096 if (fdh == NULL)
5097 return FALSE;
5098 }
5099
5100 if (fdh != NULL)
5101 {
5102 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5103 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5104
5105 /* Make both descriptor and entry symbol have the most
5106 constraining visibility of either symbol. */
5107 if (entry_vis < descr_vis)
5108 fdh->elf.other += entry_vis - descr_vis;
5109 else if (entry_vis > descr_vis)
5110 eh->elf.other += descr_vis - entry_vis;
5111
5112 /* Propagate reference flags from entry symbol to function
5113 descriptor symbol. */
5114 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5115 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5116 fdh->elf.ref_regular |= eh->elf.ref_regular;
5117 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5118
5119 if (!fdh->elf.forced_local
5120 && fdh->elf.dynindx == -1
5121 && fdh->elf.versioned != versioned_hidden
5122 && (bfd_link_dll (info)
5123 || fdh->elf.def_dynamic
5124 || fdh->elf.ref_dynamic)
5125 && (eh->elf.ref_regular
5126 || eh->elf.def_regular))
5127 {
5128 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5129 return FALSE;
5130 }
5131 }
5132
5133 return TRUE;
5134 }
5135
5136 /* Set up opd section info and abiversion for IBFD, and process list
5137 of dot-symbols we made in link_hash_newfunc. */
5138
5139 static bfd_boolean
5140 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5141 {
5142 struct ppc_link_hash_table *htab;
5143 struct ppc_link_hash_entry **p, *eh;
5144 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5145
5146 if (opd != NULL && opd->size != 0)
5147 {
5148 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5149 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5150
5151 if (abiversion (ibfd) == 0)
5152 set_abiversion (ibfd, 1);
5153 else if (abiversion (ibfd) >= 2)
5154 {
5155 /* xgettext:c-format */
5156 info->callbacks->einfo (_("%P: %B .opd not allowed in ABI"
5157 " version %d\n"),
5158 ibfd, abiversion (ibfd));
5159 bfd_set_error (bfd_error_bad_value);
5160 return FALSE;
5161 }
5162 }
5163
5164 if (is_ppc64_elf (info->output_bfd))
5165 {
5166 /* For input files without an explicit abiversion in e_flags
5167 we should have flagged any with symbol st_other bits set
5168 as ELFv1 and above flagged those with .opd as ELFv2.
5169 Set the output abiversion if not yet set, and for any input
5170 still ambiguous, take its abiversion from the output.
5171 Differences in ABI are reported later. */
5172 if (abiversion (info->output_bfd) == 0)
5173 set_abiversion (info->output_bfd, abiversion (ibfd));
5174 else if (abiversion (ibfd) == 0)
5175 set_abiversion (ibfd, abiversion (info->output_bfd));
5176 }
5177
5178 htab = ppc_hash_table (info);
5179 if (htab == NULL)
5180 return TRUE;
5181
5182 if (opd != NULL && opd->size != 0
5183 && (ibfd->flags & DYNAMIC) == 0
5184 && (opd->flags & SEC_RELOC) != 0
5185 && opd->reloc_count != 0
5186 && !bfd_is_abs_section (opd->output_section)
5187 && info->gc_sections)
5188 {
5189 /* Garbage collection needs some extra help with .opd sections.
5190 We don't want to necessarily keep everything referenced by
5191 relocs in .opd, as that would keep all functions. Instead,
5192 if we reference an .opd symbol (a function descriptor), we
5193 want to keep the function code symbol's section. This is
5194 easy for global symbols, but for local syms we need to keep
5195 information about the associated function section. */
5196 bfd_size_type amt;
5197 asection **opd_sym_map;
5198 Elf_Internal_Shdr *symtab_hdr;
5199 Elf_Internal_Rela *relocs, *rel_end, *rel;
5200
5201 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5202 opd_sym_map = bfd_zalloc (ibfd, amt);
5203 if (opd_sym_map == NULL)
5204 return FALSE;
5205 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5206 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5207 info->keep_memory);
5208 if (relocs == NULL)
5209 return FALSE;
5210 symtab_hdr = &elf_symtab_hdr (ibfd);
5211 rel_end = relocs + opd->reloc_count - 1;
5212 for (rel = relocs; rel < rel_end; rel++)
5213 {
5214 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5215 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5216
5217 if (r_type == R_PPC64_ADDR64
5218 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5219 && r_symndx < symtab_hdr->sh_info)
5220 {
5221 Elf_Internal_Sym *isym;
5222 asection *s;
5223
5224 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5225 if (isym == NULL)
5226 {
5227 if (elf_section_data (opd)->relocs != relocs)
5228 free (relocs);
5229 return FALSE;
5230 }
5231
5232 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5233 if (s != NULL && s != opd)
5234 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5235 }
5236 }
5237 if (elf_section_data (opd)->relocs != relocs)
5238 free (relocs);
5239 }
5240
5241 p = &htab->dot_syms;
5242 while ((eh = *p) != NULL)
5243 {
5244 *p = NULL;
5245 if (&eh->elf == htab->elf.hgot)
5246 ;
5247 else if (htab->elf.hgot == NULL
5248 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5249 htab->elf.hgot = &eh->elf;
5250 else if (abiversion (ibfd) <= 1)
5251 {
5252 htab->need_func_desc_adj = 1;
5253 if (!add_symbol_adjust (eh, info))
5254 return FALSE;
5255 }
5256 p = &eh->u.next_dot_sym;
5257 }
5258 return TRUE;
5259 }
5260
5261 /* Undo hash table changes when an --as-needed input file is determined
5262 not to be needed. */
5263
5264 static bfd_boolean
5265 ppc64_elf_notice_as_needed (bfd *ibfd,
5266 struct bfd_link_info *info,
5267 enum notice_asneeded_action act)
5268 {
5269 if (act == notice_not_needed)
5270 {
5271 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5272
5273 if (htab == NULL)
5274 return FALSE;
5275
5276 htab->dot_syms = NULL;
5277 }
5278 return _bfd_elf_notice_as_needed (ibfd, info, act);
5279 }
5280
5281 /* If --just-symbols against a final linked binary, then assume we need
5282 toc adjusting stubs when calling functions defined there. */
5283
5284 static void
5285 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5286 {
5287 if ((sec->flags & SEC_CODE) != 0
5288 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5289 && is_ppc64_elf (sec->owner))
5290 {
5291 if (abiversion (sec->owner) >= 2
5292 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5293 sec->has_toc_reloc = 1;
5294 }
5295 _bfd_elf_link_just_syms (sec, info);
5296 }
5297
5298 static struct plt_entry **
5299 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5300 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5301 {
5302 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5303 struct plt_entry **local_plt;
5304 unsigned char *local_got_tls_masks;
5305
5306 if (local_got_ents == NULL)
5307 {
5308 bfd_size_type size = symtab_hdr->sh_info;
5309
5310 size *= (sizeof (*local_got_ents)
5311 + sizeof (*local_plt)
5312 + sizeof (*local_got_tls_masks));
5313 local_got_ents = bfd_zalloc (abfd, size);
5314 if (local_got_ents == NULL)
5315 return NULL;
5316 elf_local_got_ents (abfd) = local_got_ents;
5317 }
5318
5319 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5320 {
5321 struct got_entry *ent;
5322
5323 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5324 if (ent->addend == r_addend
5325 && ent->owner == abfd
5326 && ent->tls_type == tls_type)
5327 break;
5328 if (ent == NULL)
5329 {
5330 bfd_size_type amt = sizeof (*ent);
5331 ent = bfd_alloc (abfd, amt);
5332 if (ent == NULL)
5333 return FALSE;
5334 ent->next = local_got_ents[r_symndx];
5335 ent->addend = r_addend;
5336 ent->owner = abfd;
5337 ent->tls_type = tls_type;
5338 ent->is_indirect = FALSE;
5339 ent->got.refcount = 0;
5340 local_got_ents[r_symndx] = ent;
5341 }
5342 ent->got.refcount += 1;
5343 }
5344
5345 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5346 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5347 local_got_tls_masks[r_symndx] |= tls_type;
5348
5349 return local_plt + r_symndx;
5350 }
5351
5352 static bfd_boolean
5353 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5354 {
5355 struct plt_entry *ent;
5356
5357 for (ent = *plist; ent != NULL; ent = ent->next)
5358 if (ent->addend == addend)
5359 break;
5360 if (ent == NULL)
5361 {
5362 bfd_size_type amt = sizeof (*ent);
5363 ent = bfd_alloc (abfd, amt);
5364 if (ent == NULL)
5365 return FALSE;
5366 ent->next = *plist;
5367 ent->addend = addend;
5368 ent->plt.refcount = 0;
5369 *plist = ent;
5370 }
5371 ent->plt.refcount += 1;
5372 return TRUE;
5373 }
5374
5375 static bfd_boolean
5376 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5377 {
5378 return (r_type == R_PPC64_REL24
5379 || r_type == R_PPC64_REL14
5380 || r_type == R_PPC64_REL14_BRTAKEN
5381 || r_type == R_PPC64_REL14_BRNTAKEN
5382 || r_type == R_PPC64_ADDR24
5383 || r_type == R_PPC64_ADDR14
5384 || r_type == R_PPC64_ADDR14_BRTAKEN
5385 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5386 }
5387
5388 /* Look through the relocs for a section during the first phase, and
5389 calculate needed space in the global offset table, procedure
5390 linkage table, and dynamic reloc sections. */
5391
5392 static bfd_boolean
5393 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5394 asection *sec, const Elf_Internal_Rela *relocs)
5395 {
5396 struct ppc_link_hash_table *htab;
5397 Elf_Internal_Shdr *symtab_hdr;
5398 struct elf_link_hash_entry **sym_hashes;
5399 const Elf_Internal_Rela *rel;
5400 const Elf_Internal_Rela *rel_end;
5401 asection *sreloc;
5402 struct elf_link_hash_entry *tga, *dottga;
5403 bfd_boolean is_opd;
5404
5405 if (bfd_link_relocatable (info))
5406 return TRUE;
5407
5408 /* Don't do anything special with non-loaded, non-alloced sections.
5409 In particular, any relocs in such sections should not affect GOT
5410 and PLT reference counting (ie. we don't allow them to create GOT
5411 or PLT entries), there's no possibility or desire to optimize TLS
5412 relocs, and there's not much point in propagating relocs to shared
5413 libs that the dynamic linker won't relocate. */
5414 if ((sec->flags & SEC_ALLOC) == 0)
5415 return TRUE;
5416
5417 BFD_ASSERT (is_ppc64_elf (abfd));
5418
5419 htab = ppc_hash_table (info);
5420 if (htab == NULL)
5421 return FALSE;
5422
5423 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5424 FALSE, FALSE, TRUE);
5425 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5426 FALSE, FALSE, TRUE);
5427 symtab_hdr = &elf_symtab_hdr (abfd);
5428 sym_hashes = elf_sym_hashes (abfd);
5429 sreloc = NULL;
5430 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5431 rel_end = relocs + sec->reloc_count;
5432 for (rel = relocs; rel < rel_end; rel++)
5433 {
5434 unsigned long r_symndx;
5435 struct elf_link_hash_entry *h;
5436 enum elf_ppc64_reloc_type r_type;
5437 int tls_type;
5438 struct _ppc64_elf_section_data *ppc64_sec;
5439 struct plt_entry **ifunc, **plt_list;
5440
5441 r_symndx = ELF64_R_SYM (rel->r_info);
5442 if (r_symndx < symtab_hdr->sh_info)
5443 h = NULL;
5444 else
5445 {
5446 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5447 h = elf_follow_link (h);
5448
5449 if (h == htab->elf.hgot)
5450 sec->has_toc_reloc = 1;
5451 }
5452
5453 tls_type = 0;
5454 ifunc = NULL;
5455 if (h != NULL)
5456 {
5457 if (h->type == STT_GNU_IFUNC)
5458 {
5459 h->needs_plt = 1;
5460 ifunc = &h->plt.plist;
5461 }
5462 }
5463 else
5464 {
5465 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5466 abfd, r_symndx);
5467 if (isym == NULL)
5468 return FALSE;
5469
5470 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5471 {
5472 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5473 rel->r_addend, PLT_IFUNC);
5474 if (ifunc == NULL)
5475 return FALSE;
5476 }
5477 }
5478
5479 r_type = ELF64_R_TYPE (rel->r_info);
5480 switch (r_type)
5481 {
5482 case R_PPC64_TLSGD:
5483 case R_PPC64_TLSLD:
5484 /* These special tls relocs tie a call to __tls_get_addr with
5485 its parameter symbol. */
5486 break;
5487
5488 case R_PPC64_GOT_TLSLD16:
5489 case R_PPC64_GOT_TLSLD16_LO:
5490 case R_PPC64_GOT_TLSLD16_HI:
5491 case R_PPC64_GOT_TLSLD16_HA:
5492 tls_type = TLS_TLS | TLS_LD;
5493 goto dogottls;
5494
5495 case R_PPC64_GOT_TLSGD16:
5496 case R_PPC64_GOT_TLSGD16_LO:
5497 case R_PPC64_GOT_TLSGD16_HI:
5498 case R_PPC64_GOT_TLSGD16_HA:
5499 tls_type = TLS_TLS | TLS_GD;
5500 goto dogottls;
5501
5502 case R_PPC64_GOT_TPREL16_DS:
5503 case R_PPC64_GOT_TPREL16_LO_DS:
5504 case R_PPC64_GOT_TPREL16_HI:
5505 case R_PPC64_GOT_TPREL16_HA:
5506 if (bfd_link_dll (info))
5507 info->flags |= DF_STATIC_TLS;
5508 tls_type = TLS_TLS | TLS_TPREL;
5509 goto dogottls;
5510
5511 case R_PPC64_GOT_DTPREL16_DS:
5512 case R_PPC64_GOT_DTPREL16_LO_DS:
5513 case R_PPC64_GOT_DTPREL16_HI:
5514 case R_PPC64_GOT_DTPREL16_HA:
5515 tls_type = TLS_TLS | TLS_DTPREL;
5516 dogottls:
5517 sec->has_tls_reloc = 1;
5518 /* Fall through */
5519
5520 case R_PPC64_GOT16:
5521 case R_PPC64_GOT16_DS:
5522 case R_PPC64_GOT16_HA:
5523 case R_PPC64_GOT16_HI:
5524 case R_PPC64_GOT16_LO:
5525 case R_PPC64_GOT16_LO_DS:
5526 /* This symbol requires a global offset table entry. */
5527 sec->has_toc_reloc = 1;
5528 if (r_type == R_PPC64_GOT_TLSLD16
5529 || r_type == R_PPC64_GOT_TLSGD16
5530 || r_type == R_PPC64_GOT_TPREL16_DS
5531 || r_type == R_PPC64_GOT_DTPREL16_DS
5532 || r_type == R_PPC64_GOT16
5533 || r_type == R_PPC64_GOT16_DS)
5534 {
5535 htab->do_multi_toc = 1;
5536 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5537 }
5538
5539 if (ppc64_elf_tdata (abfd)->got == NULL
5540 && !create_got_section (abfd, info))
5541 return FALSE;
5542
5543 if (h != NULL)
5544 {
5545 struct ppc_link_hash_entry *eh;
5546 struct got_entry *ent;
5547
5548 eh = (struct ppc_link_hash_entry *) h;
5549 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5550 if (ent->addend == rel->r_addend
5551 && ent->owner == abfd
5552 && ent->tls_type == tls_type)
5553 break;
5554 if (ent == NULL)
5555 {
5556 bfd_size_type amt = sizeof (*ent);
5557 ent = bfd_alloc (abfd, amt);
5558 if (ent == NULL)
5559 return FALSE;
5560 ent->next = eh->elf.got.glist;
5561 ent->addend = rel->r_addend;
5562 ent->owner = abfd;
5563 ent->tls_type = tls_type;
5564 ent->is_indirect = FALSE;
5565 ent->got.refcount = 0;
5566 eh->elf.got.glist = ent;
5567 }
5568 ent->got.refcount += 1;
5569 eh->tls_mask |= tls_type;
5570 }
5571 else
5572 /* This is a global offset table entry for a local symbol. */
5573 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5574 rel->r_addend, tls_type))
5575 return FALSE;
5576
5577 /* We may also need a plt entry if the symbol turns out to be
5578 an ifunc. */
5579 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5580 {
5581 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5582 return FALSE;
5583 }
5584 break;
5585
5586 case R_PPC64_PLT16_HA:
5587 case R_PPC64_PLT16_HI:
5588 case R_PPC64_PLT16_LO:
5589 case R_PPC64_PLT32:
5590 case R_PPC64_PLT64:
5591 /* This symbol requires a procedure linkage table entry. */
5592 plt_list = ifunc;
5593 if (h != NULL)
5594 {
5595 h->needs_plt = 1;
5596 if (h->root.root.string[0] == '.'
5597 && h->root.root.string[1] != '\0')
5598 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5599 plt_list = &h->plt.plist;
5600 }
5601 if (plt_list == NULL)
5602 {
5603 /* It does not make sense to have a procedure linkage
5604 table entry for a non-ifunc local symbol. */
5605 info->callbacks->einfo
5606 /* xgettext:c-format */
5607 (_("%H: %s reloc against local symbol\n"),
5608 abfd, sec, rel->r_offset,
5609 ppc64_elf_howto_table[r_type]->name);
5610 bfd_set_error (bfd_error_bad_value);
5611 return FALSE;
5612 }
5613 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5614 return FALSE;
5615 break;
5616
5617 /* The following relocations don't need to propagate the
5618 relocation if linking a shared object since they are
5619 section relative. */
5620 case R_PPC64_SECTOFF:
5621 case R_PPC64_SECTOFF_LO:
5622 case R_PPC64_SECTOFF_HI:
5623 case R_PPC64_SECTOFF_HA:
5624 case R_PPC64_SECTOFF_DS:
5625 case R_PPC64_SECTOFF_LO_DS:
5626 case R_PPC64_DTPREL16:
5627 case R_PPC64_DTPREL16_LO:
5628 case R_PPC64_DTPREL16_HI:
5629 case R_PPC64_DTPREL16_HA:
5630 case R_PPC64_DTPREL16_DS:
5631 case R_PPC64_DTPREL16_LO_DS:
5632 case R_PPC64_DTPREL16_HIGH:
5633 case R_PPC64_DTPREL16_HIGHA:
5634 case R_PPC64_DTPREL16_HIGHER:
5635 case R_PPC64_DTPREL16_HIGHERA:
5636 case R_PPC64_DTPREL16_HIGHEST:
5637 case R_PPC64_DTPREL16_HIGHESTA:
5638 break;
5639
5640 /* Nor do these. */
5641 case R_PPC64_REL16:
5642 case R_PPC64_REL16_LO:
5643 case R_PPC64_REL16_HI:
5644 case R_PPC64_REL16_HA:
5645 case R_PPC64_REL16DX_HA:
5646 break;
5647
5648 /* Not supported as a dynamic relocation. */
5649 case R_PPC64_ADDR64_LOCAL:
5650 if (bfd_link_pic (info))
5651 {
5652 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5653 ppc_howto_init ();
5654 /* xgettext:c-format */
5655 info->callbacks->einfo (_("%H: %s reloc unsupported "
5656 "in shared libraries and PIEs.\n"),
5657 abfd, sec, rel->r_offset,
5658 ppc64_elf_howto_table[r_type]->name);
5659 bfd_set_error (bfd_error_bad_value);
5660 return FALSE;
5661 }
5662 break;
5663
5664 case R_PPC64_TOC16:
5665 case R_PPC64_TOC16_DS:
5666 htab->do_multi_toc = 1;
5667 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5668 /* Fall through. */
5669 case R_PPC64_TOC16_LO:
5670 case R_PPC64_TOC16_HI:
5671 case R_PPC64_TOC16_HA:
5672 case R_PPC64_TOC16_LO_DS:
5673 sec->has_toc_reloc = 1;
5674 break;
5675
5676 /* Marker reloc. */
5677 case R_PPC64_ENTRY:
5678 break;
5679
5680 /* This relocation describes the C++ object vtable hierarchy.
5681 Reconstruct it for later use during GC. */
5682 case R_PPC64_GNU_VTINHERIT:
5683 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5684 return FALSE;
5685 break;
5686
5687 /* This relocation describes which C++ vtable entries are actually
5688 used. Record for later use during GC. */
5689 case R_PPC64_GNU_VTENTRY:
5690 BFD_ASSERT (h != NULL);
5691 if (h != NULL
5692 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5693 return FALSE;
5694 break;
5695
5696 case R_PPC64_REL14:
5697 case R_PPC64_REL14_BRTAKEN:
5698 case R_PPC64_REL14_BRNTAKEN:
5699 {
5700 asection *dest = NULL;
5701
5702 /* Heuristic: If jumping outside our section, chances are
5703 we are going to need a stub. */
5704 if (h != NULL)
5705 {
5706 /* If the sym is weak it may be overridden later, so
5707 don't assume we know where a weak sym lives. */
5708 if (h->root.type == bfd_link_hash_defined)
5709 dest = h->root.u.def.section;
5710 }
5711 else
5712 {
5713 Elf_Internal_Sym *isym;
5714
5715 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5716 abfd, r_symndx);
5717 if (isym == NULL)
5718 return FALSE;
5719
5720 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5721 }
5722
5723 if (dest != sec)
5724 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5725 }
5726 /* Fall through. */
5727
5728 case R_PPC64_REL24:
5729 plt_list = ifunc;
5730 if (h != NULL)
5731 {
5732 h->needs_plt = 1;
5733 if (h->root.root.string[0] == '.'
5734 && h->root.root.string[1] != '\0')
5735 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5736
5737 if (h == tga || h == dottga)
5738 {
5739 sec->has_tls_reloc = 1;
5740 if (rel != relocs
5741 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5742 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5743 /* We have a new-style __tls_get_addr call with
5744 a marker reloc. */
5745 ;
5746 else
5747 /* Mark this section as having an old-style call. */
5748 sec->has_tls_get_addr_call = 1;
5749 }
5750 plt_list = &h->plt.plist;
5751 }
5752
5753 /* We may need a .plt entry if the function this reloc
5754 refers to is in a shared lib. */
5755 if (plt_list
5756 && !update_plt_info (abfd, plt_list, rel->r_addend))
5757 return FALSE;
5758 break;
5759
5760 case R_PPC64_ADDR14:
5761 case R_PPC64_ADDR14_BRNTAKEN:
5762 case R_PPC64_ADDR14_BRTAKEN:
5763 case R_PPC64_ADDR24:
5764 goto dodyn;
5765
5766 case R_PPC64_TPREL64:
5767 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5768 if (bfd_link_dll (info))
5769 info->flags |= DF_STATIC_TLS;
5770 goto dotlstoc;
5771
5772 case R_PPC64_DTPMOD64:
5773 if (rel + 1 < rel_end
5774 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5775 && rel[1].r_offset == rel->r_offset + 8)
5776 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5777 else
5778 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5779 goto dotlstoc;
5780
5781 case R_PPC64_DTPREL64:
5782 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5783 if (rel != relocs
5784 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5785 && rel[-1].r_offset == rel->r_offset - 8)
5786 /* This is the second reloc of a dtpmod, dtprel pair.
5787 Don't mark with TLS_DTPREL. */
5788 goto dodyn;
5789
5790 dotlstoc:
5791 sec->has_tls_reloc = 1;
5792 if (h != NULL)
5793 {
5794 struct ppc_link_hash_entry *eh;
5795 eh = (struct ppc_link_hash_entry *) h;
5796 eh->tls_mask |= tls_type;
5797 }
5798 else
5799 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5800 rel->r_addend, tls_type))
5801 return FALSE;
5802
5803 ppc64_sec = ppc64_elf_section_data (sec);
5804 if (ppc64_sec->sec_type != sec_toc)
5805 {
5806 bfd_size_type amt;
5807
5808 /* One extra to simplify get_tls_mask. */
5809 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5810 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5811 if (ppc64_sec->u.toc.symndx == NULL)
5812 return FALSE;
5813 amt = sec->size * sizeof (bfd_vma) / 8;
5814 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5815 if (ppc64_sec->u.toc.add == NULL)
5816 return FALSE;
5817 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5818 ppc64_sec->sec_type = sec_toc;
5819 }
5820 BFD_ASSERT (rel->r_offset % 8 == 0);
5821 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5822 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5823
5824 /* Mark the second slot of a GD or LD entry.
5825 -1 to indicate GD and -2 to indicate LD. */
5826 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5827 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5828 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5829 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5830 goto dodyn;
5831
5832 case R_PPC64_TPREL16:
5833 case R_PPC64_TPREL16_LO:
5834 case R_PPC64_TPREL16_HI:
5835 case R_PPC64_TPREL16_HA:
5836 case R_PPC64_TPREL16_DS:
5837 case R_PPC64_TPREL16_LO_DS:
5838 case R_PPC64_TPREL16_HIGH:
5839 case R_PPC64_TPREL16_HIGHA:
5840 case R_PPC64_TPREL16_HIGHER:
5841 case R_PPC64_TPREL16_HIGHERA:
5842 case R_PPC64_TPREL16_HIGHEST:
5843 case R_PPC64_TPREL16_HIGHESTA:
5844 if (bfd_link_dll (info))
5845 info->flags |= DF_STATIC_TLS;
5846 goto dodyn;
5847
5848 case R_PPC64_ADDR64:
5849 if (is_opd
5850 && rel + 1 < rel_end
5851 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5852 {
5853 if (h != NULL)
5854 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5855 }
5856 /* Fall through. */
5857
5858 case R_PPC64_ADDR16:
5859 case R_PPC64_ADDR16_DS:
5860 case R_PPC64_ADDR16_HA:
5861 case R_PPC64_ADDR16_HI:
5862 case R_PPC64_ADDR16_HIGH:
5863 case R_PPC64_ADDR16_HIGHA:
5864 case R_PPC64_ADDR16_HIGHER:
5865 case R_PPC64_ADDR16_HIGHERA:
5866 case R_PPC64_ADDR16_HIGHEST:
5867 case R_PPC64_ADDR16_HIGHESTA:
5868 case R_PPC64_ADDR16_LO:
5869 case R_PPC64_ADDR16_LO_DS:
5870 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5871 && rel->r_addend == 0)
5872 {
5873 /* We may need a .plt entry if this reloc refers to a
5874 function in a shared lib. */
5875 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5876 return FALSE;
5877 h->pointer_equality_needed = 1;
5878 }
5879 /* Fall through. */
5880
5881 case R_PPC64_REL30:
5882 case R_PPC64_REL32:
5883 case R_PPC64_REL64:
5884 case R_PPC64_ADDR32:
5885 case R_PPC64_UADDR16:
5886 case R_PPC64_UADDR32:
5887 case R_PPC64_UADDR64:
5888 case R_PPC64_TOC:
5889 if (h != NULL && !bfd_link_pic (info))
5890 /* We may need a copy reloc. */
5891 h->non_got_ref = 1;
5892
5893 /* Don't propagate .opd relocs. */
5894 if (NO_OPD_RELOCS && is_opd)
5895 break;
5896
5897 /* If we are creating a shared library, and this is a reloc
5898 against a global symbol, or a non PC relative reloc
5899 against a local symbol, then we need to copy the reloc
5900 into the shared library. However, if we are linking with
5901 -Bsymbolic, we do not need to copy a reloc against a
5902 global symbol which is defined in an object we are
5903 including in the link (i.e., DEF_REGULAR is set). At
5904 this point we have not seen all the input files, so it is
5905 possible that DEF_REGULAR is not set now but will be set
5906 later (it is never cleared). In case of a weak definition,
5907 DEF_REGULAR may be cleared later by a strong definition in
5908 a shared library. We account for that possibility below by
5909 storing information in the dyn_relocs field of the hash
5910 table entry. A similar situation occurs when creating
5911 shared libraries and symbol visibility changes render the
5912 symbol local.
5913
5914 If on the other hand, we are creating an executable, we
5915 may need to keep relocations for symbols satisfied by a
5916 dynamic library if we manage to avoid copy relocs for the
5917 symbol. */
5918 dodyn:
5919 if ((bfd_link_pic (info)
5920 && (must_be_dyn_reloc (info, r_type)
5921 || (h != NULL
5922 && (!SYMBOLIC_BIND (info, h)
5923 || h->root.type == bfd_link_hash_defweak
5924 || !h->def_regular))))
5925 || (ELIMINATE_COPY_RELOCS
5926 && !bfd_link_pic (info)
5927 && h != NULL
5928 && (h->root.type == bfd_link_hash_defweak
5929 || !h->def_regular))
5930 || (!bfd_link_pic (info)
5931 && ifunc != NULL))
5932 {
5933 /* We must copy these reloc types into the output file.
5934 Create a reloc section in dynobj and make room for
5935 this reloc. */
5936 if (sreloc == NULL)
5937 {
5938 sreloc = _bfd_elf_make_dynamic_reloc_section
5939 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5940
5941 if (sreloc == NULL)
5942 return FALSE;
5943 }
5944
5945 /* If this is a global symbol, we count the number of
5946 relocations we need for this symbol. */
5947 if (h != NULL)
5948 {
5949 struct elf_dyn_relocs *p;
5950 struct elf_dyn_relocs **head;
5951
5952 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5953 p = *head;
5954 if (p == NULL || p->sec != sec)
5955 {
5956 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5957 if (p == NULL)
5958 return FALSE;
5959 p->next = *head;
5960 *head = p;
5961 p->sec = sec;
5962 p->count = 0;
5963 p->pc_count = 0;
5964 }
5965 p->count += 1;
5966 if (!must_be_dyn_reloc (info, r_type))
5967 p->pc_count += 1;
5968 }
5969 else
5970 {
5971 /* Track dynamic relocs needed for local syms too.
5972 We really need local syms available to do this
5973 easily. Oh well. */
5974 struct ppc_dyn_relocs *p;
5975 struct ppc_dyn_relocs **head;
5976 bfd_boolean is_ifunc;
5977 asection *s;
5978 void *vpp;
5979 Elf_Internal_Sym *isym;
5980
5981 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5982 abfd, r_symndx);
5983 if (isym == NULL)
5984 return FALSE;
5985
5986 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5987 if (s == NULL)
5988 s = sec;
5989
5990 vpp = &elf_section_data (s)->local_dynrel;
5991 head = (struct ppc_dyn_relocs **) vpp;
5992 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5993 p = *head;
5994 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5995 p = p->next;
5996 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5997 {
5998 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5999 if (p == NULL)
6000 return FALSE;
6001 p->next = *head;
6002 *head = p;
6003 p->sec = sec;
6004 p->ifunc = is_ifunc;
6005 p->count = 0;
6006 }
6007 p->count += 1;
6008 }
6009 }
6010 break;
6011
6012 default:
6013 break;
6014 }
6015 }
6016
6017 return TRUE;
6018 }
6019
6020 /* Merge backend specific data from an object file to the output
6021 object file when linking. */
6022
6023 static bfd_boolean
6024 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6025 {
6026 bfd *obfd = info->output_bfd;
6027 unsigned long iflags, oflags;
6028
6029 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6030 return TRUE;
6031
6032 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6033 return TRUE;
6034
6035 if (!_bfd_generic_verify_endian_match (ibfd, info))
6036 return FALSE;
6037
6038 iflags = elf_elfheader (ibfd)->e_flags;
6039 oflags = elf_elfheader (obfd)->e_flags;
6040
6041 if (iflags & ~EF_PPC64_ABI)
6042 {
6043 _bfd_error_handler
6044 /* xgettext:c-format */
6045 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
6046 bfd_set_error (bfd_error_bad_value);
6047 return FALSE;
6048 }
6049 else if (iflags != oflags && iflags != 0)
6050 {
6051 _bfd_error_handler
6052 /* xgettext:c-format */
6053 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
6054 ibfd, iflags, oflags);
6055 bfd_set_error (bfd_error_bad_value);
6056 return FALSE;
6057 }
6058
6059 _bfd_elf_ppc_merge_fp_attributes (ibfd, info);
6060
6061 /* Merge Tag_compatibility attributes and any common GNU ones. */
6062 _bfd_elf_merge_object_attributes (ibfd, info);
6063
6064 return TRUE;
6065 }
6066
6067 static bfd_boolean
6068 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6069 {
6070 /* Print normal ELF private data. */
6071 _bfd_elf_print_private_bfd_data (abfd, ptr);
6072
6073 if (elf_elfheader (abfd)->e_flags != 0)
6074 {
6075 FILE *file = ptr;
6076
6077 fprintf (file, _("private flags = 0x%lx:"),
6078 elf_elfheader (abfd)->e_flags);
6079
6080 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6081 fprintf (file, _(" [abiv%ld]"),
6082 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6083 fputc ('\n', file);
6084 }
6085
6086 return TRUE;
6087 }
6088
6089 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6090 of the code entry point, and its section, which must be in the same
6091 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6092
6093 static bfd_vma
6094 opd_entry_value (asection *opd_sec,
6095 bfd_vma offset,
6096 asection **code_sec,
6097 bfd_vma *code_off,
6098 bfd_boolean in_code_sec)
6099 {
6100 bfd *opd_bfd = opd_sec->owner;
6101 Elf_Internal_Rela *relocs;
6102 Elf_Internal_Rela *lo, *hi, *look;
6103 bfd_vma val;
6104
6105 /* No relocs implies we are linking a --just-symbols object, or looking
6106 at a final linked executable with addr2line or somesuch. */
6107 if (opd_sec->reloc_count == 0)
6108 {
6109 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6110
6111 if (contents == NULL)
6112 {
6113 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6114 return (bfd_vma) -1;
6115 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6116 }
6117
6118 /* PR 17512: file: 64b9dfbb. */
6119 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6120 return (bfd_vma) -1;
6121
6122 val = bfd_get_64 (opd_bfd, contents + offset);
6123 if (code_sec != NULL)
6124 {
6125 asection *sec, *likely = NULL;
6126
6127 if (in_code_sec)
6128 {
6129 sec = *code_sec;
6130 if (sec->vma <= val
6131 && val < sec->vma + sec->size)
6132 likely = sec;
6133 else
6134 val = -1;
6135 }
6136 else
6137 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6138 if (sec->vma <= val
6139 && (sec->flags & SEC_LOAD) != 0
6140 && (sec->flags & SEC_ALLOC) != 0)
6141 likely = sec;
6142 if (likely != NULL)
6143 {
6144 *code_sec = likely;
6145 if (code_off != NULL)
6146 *code_off = val - likely->vma;
6147 }
6148 }
6149 return val;
6150 }
6151
6152 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6153
6154 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6155 if (relocs == NULL)
6156 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6157 /* PR 17512: file: df8e1fd6. */
6158 if (relocs == NULL)
6159 return (bfd_vma) -1;
6160
6161 /* Go find the opd reloc at the sym address. */
6162 lo = relocs;
6163 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6164 val = (bfd_vma) -1;
6165 while (lo < hi)
6166 {
6167 look = lo + (hi - lo) / 2;
6168 if (look->r_offset < offset)
6169 lo = look + 1;
6170 else if (look->r_offset > offset)
6171 hi = look;
6172 else
6173 {
6174 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6175
6176 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6177 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6178 {
6179 unsigned long symndx = ELF64_R_SYM (look->r_info);
6180 asection *sec = NULL;
6181
6182 if (symndx >= symtab_hdr->sh_info
6183 && elf_sym_hashes (opd_bfd) != NULL)
6184 {
6185 struct elf_link_hash_entry **sym_hashes;
6186 struct elf_link_hash_entry *rh;
6187
6188 sym_hashes = elf_sym_hashes (opd_bfd);
6189 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6190 if (rh != NULL)
6191 {
6192 rh = elf_follow_link (rh);
6193 if (rh->root.type != bfd_link_hash_defined
6194 && rh->root.type != bfd_link_hash_defweak)
6195 break;
6196 if (rh->root.u.def.section->owner == opd_bfd)
6197 {
6198 val = rh->root.u.def.value;
6199 sec = rh->root.u.def.section;
6200 }
6201 }
6202 }
6203
6204 if (sec == NULL)
6205 {
6206 Elf_Internal_Sym *sym;
6207
6208 if (symndx < symtab_hdr->sh_info)
6209 {
6210 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6211 if (sym == NULL)
6212 {
6213 size_t symcnt = symtab_hdr->sh_info;
6214 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6215 symcnt, 0,
6216 NULL, NULL, NULL);
6217 if (sym == NULL)
6218 break;
6219 symtab_hdr->contents = (bfd_byte *) sym;
6220 }
6221 sym += symndx;
6222 }
6223 else
6224 {
6225 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6226 1, symndx,
6227 NULL, NULL, NULL);
6228 if (sym == NULL)
6229 break;
6230 }
6231 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6232 if (sec == NULL)
6233 break;
6234 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6235 val = sym->st_value;
6236 }
6237
6238 val += look->r_addend;
6239 if (code_off != NULL)
6240 *code_off = val;
6241 if (code_sec != NULL)
6242 {
6243 if (in_code_sec && *code_sec != sec)
6244 return -1;
6245 else
6246 *code_sec = sec;
6247 }
6248 if (sec->output_section != NULL)
6249 val += sec->output_section->vma + sec->output_offset;
6250 }
6251 break;
6252 }
6253 }
6254
6255 return val;
6256 }
6257
6258 /* If the ELF symbol SYM might be a function in SEC, return the
6259 function size and set *CODE_OFF to the function's entry point,
6260 otherwise return zero. */
6261
6262 static bfd_size_type
6263 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6264 bfd_vma *code_off)
6265 {
6266 bfd_size_type size;
6267
6268 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6269 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6270 return 0;
6271
6272 size = 0;
6273 if (!(sym->flags & BSF_SYNTHETIC))
6274 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6275
6276 if (strcmp (sym->section->name, ".opd") == 0)
6277 {
6278 struct _opd_sec_data *opd = get_opd_info (sym->section);
6279 bfd_vma symval = sym->value;
6280
6281 if (opd != NULL
6282 && opd->adjust != NULL
6283 && elf_section_data (sym->section)->relocs != NULL)
6284 {
6285 /* opd_entry_value will use cached relocs that have been
6286 adjusted, but with raw symbols. That means both local
6287 and global symbols need adjusting. */
6288 long adjust = opd->adjust[OPD_NDX (symval)];
6289 if (adjust == -1)
6290 return 0;
6291 symval += adjust;
6292 }
6293
6294 if (opd_entry_value (sym->section, symval,
6295 &sec, code_off, TRUE) == (bfd_vma) -1)
6296 return 0;
6297 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6298 symbol. This size has nothing to do with the code size of the
6299 function, which is what we're supposed to return, but the
6300 code size isn't available without looking up the dot-sym.
6301 However, doing that would be a waste of time particularly
6302 since elf_find_function will look at the dot-sym anyway.
6303 Now, elf_find_function will keep the largest size of any
6304 function sym found at the code address of interest, so return
6305 1 here to avoid it incorrectly caching a larger function size
6306 for a small function. This does mean we return the wrong
6307 size for a new-ABI function of size 24, but all that does is
6308 disable caching for such functions. */
6309 if (size == 24)
6310 size = 1;
6311 }
6312 else
6313 {
6314 if (sym->section != sec)
6315 return 0;
6316 *code_off = sym->value;
6317 }
6318 if (size == 0)
6319 size = 1;
6320 return size;
6321 }
6322
6323 /* Return true if symbol is a strong function defined in an ELFv2
6324 object with st_other localentry bits of zero, ie. its local entry
6325 point coincides with its global entry point. */
6326
6327 static bfd_boolean
6328 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6329 {
6330 return (h != NULL
6331 && h->type == STT_FUNC
6332 && h->root.type == bfd_link_hash_defined
6333 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6334 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6335 && is_ppc64_elf (h->root.u.def.section->owner)
6336 && abiversion (h->root.u.def.section->owner) >= 2);
6337 }
6338
6339 /* Return true if symbol is defined in a regular object file. */
6340
6341 static bfd_boolean
6342 is_static_defined (struct elf_link_hash_entry *h)
6343 {
6344 return ((h->root.type == bfd_link_hash_defined
6345 || h->root.type == bfd_link_hash_defweak)
6346 && h->root.u.def.section != NULL
6347 && h->root.u.def.section->output_section != NULL);
6348 }
6349
6350 /* If FDH is a function descriptor symbol, return the associated code
6351 entry symbol if it is defined. Return NULL otherwise. */
6352
6353 static struct ppc_link_hash_entry *
6354 defined_code_entry (struct ppc_link_hash_entry *fdh)
6355 {
6356 if (fdh->is_func_descriptor)
6357 {
6358 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6359 if (fh->elf.root.type == bfd_link_hash_defined
6360 || fh->elf.root.type == bfd_link_hash_defweak)
6361 return fh;
6362 }
6363 return NULL;
6364 }
6365
6366 /* If FH is a function code entry symbol, return the associated
6367 function descriptor symbol if it is defined. Return NULL otherwise. */
6368
6369 static struct ppc_link_hash_entry *
6370 defined_func_desc (struct ppc_link_hash_entry *fh)
6371 {
6372 if (fh->oh != NULL
6373 && fh->oh->is_func_descriptor)
6374 {
6375 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6376 if (fdh->elf.root.type == bfd_link_hash_defined
6377 || fdh->elf.root.type == bfd_link_hash_defweak)
6378 return fdh;
6379 }
6380 return NULL;
6381 }
6382
6383 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6384
6385 /* Garbage collect sections, after first dealing with dot-symbols. */
6386
6387 static bfd_boolean
6388 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6389 {
6390 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6391
6392 if (htab != NULL && htab->need_func_desc_adj)
6393 {
6394 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6395 htab->need_func_desc_adj = 0;
6396 }
6397 return bfd_elf_gc_sections (abfd, info);
6398 }
6399
6400 /* Mark all our entry sym sections, both opd and code section. */
6401
6402 static void
6403 ppc64_elf_gc_keep (struct bfd_link_info *info)
6404 {
6405 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6406 struct bfd_sym_chain *sym;
6407
6408 if (htab == NULL)
6409 return;
6410
6411 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6412 {
6413 struct ppc_link_hash_entry *eh, *fh;
6414 asection *sec;
6415
6416 eh = (struct ppc_link_hash_entry *)
6417 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6418 if (eh == NULL)
6419 continue;
6420 if (eh->elf.root.type != bfd_link_hash_defined
6421 && eh->elf.root.type != bfd_link_hash_defweak)
6422 continue;
6423
6424 fh = defined_code_entry (eh);
6425 if (fh != NULL)
6426 {
6427 sec = fh->elf.root.u.def.section;
6428 sec->flags |= SEC_KEEP;
6429 }
6430 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6431 && opd_entry_value (eh->elf.root.u.def.section,
6432 eh->elf.root.u.def.value,
6433 &sec, NULL, FALSE) != (bfd_vma) -1)
6434 sec->flags |= SEC_KEEP;
6435
6436 sec = eh->elf.root.u.def.section;
6437 sec->flags |= SEC_KEEP;
6438 }
6439 }
6440
6441 /* Mark sections containing dynamically referenced symbols. When
6442 building shared libraries, we must assume that any visible symbol is
6443 referenced. */
6444
6445 static bfd_boolean
6446 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6447 {
6448 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6449 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6450 struct ppc_link_hash_entry *fdh;
6451 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6452
6453 /* Dynamic linking info is on the func descriptor sym. */
6454 fdh = defined_func_desc (eh);
6455 if (fdh != NULL)
6456 eh = fdh;
6457
6458 if ((eh->elf.root.type == bfd_link_hash_defined
6459 || eh->elf.root.type == bfd_link_hash_defweak)
6460 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6461 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6462 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6463 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6464 && (!bfd_link_executable (info)
6465 || info->gc_keep_exported
6466 || info->export_dynamic
6467 || (eh->elf.dynamic
6468 && d != NULL
6469 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6470 && (eh->elf.versioned >= versioned
6471 || !bfd_hide_sym_by_version (info->version_info,
6472 eh->elf.root.root.string)))))
6473 {
6474 asection *code_sec;
6475 struct ppc_link_hash_entry *fh;
6476
6477 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6478
6479 /* Function descriptor syms cause the associated
6480 function code sym section to be marked. */
6481 fh = defined_code_entry (eh);
6482 if (fh != NULL)
6483 {
6484 code_sec = fh->elf.root.u.def.section;
6485 code_sec->flags |= SEC_KEEP;
6486 }
6487 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6488 && opd_entry_value (eh->elf.root.u.def.section,
6489 eh->elf.root.u.def.value,
6490 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6491 code_sec->flags |= SEC_KEEP;
6492 }
6493
6494 return TRUE;
6495 }
6496
6497 /* Return the section that should be marked against GC for a given
6498 relocation. */
6499
6500 static asection *
6501 ppc64_elf_gc_mark_hook (asection *sec,
6502 struct bfd_link_info *info,
6503 Elf_Internal_Rela *rel,
6504 struct elf_link_hash_entry *h,
6505 Elf_Internal_Sym *sym)
6506 {
6507 asection *rsec;
6508
6509 /* Syms return NULL if we're marking .opd, so we avoid marking all
6510 function sections, as all functions are referenced in .opd. */
6511 rsec = NULL;
6512 if (get_opd_info (sec) != NULL)
6513 return rsec;
6514
6515 if (h != NULL)
6516 {
6517 enum elf_ppc64_reloc_type r_type;
6518 struct ppc_link_hash_entry *eh, *fh, *fdh;
6519
6520 r_type = ELF64_R_TYPE (rel->r_info);
6521 switch (r_type)
6522 {
6523 case R_PPC64_GNU_VTINHERIT:
6524 case R_PPC64_GNU_VTENTRY:
6525 break;
6526
6527 default:
6528 switch (h->root.type)
6529 {
6530 case bfd_link_hash_defined:
6531 case bfd_link_hash_defweak:
6532 eh = (struct ppc_link_hash_entry *) h;
6533 fdh = defined_func_desc (eh);
6534 if (fdh != NULL)
6535 {
6536 /* -mcall-aixdesc code references the dot-symbol on
6537 a call reloc. Mark the function descriptor too
6538 against garbage collection. */
6539 fdh->elf.mark = 1;
6540 if (fdh->elf.is_weakalias)
6541 weakdef (&fdh->elf)->mark = 1;
6542 eh = fdh;
6543 }
6544
6545 /* Function descriptor syms cause the associated
6546 function code sym section to be marked. */
6547 fh = defined_code_entry (eh);
6548 if (fh != NULL)
6549 {
6550 /* They also mark their opd section. */
6551 eh->elf.root.u.def.section->gc_mark = 1;
6552
6553 rsec = fh->elf.root.u.def.section;
6554 }
6555 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6556 && opd_entry_value (eh->elf.root.u.def.section,
6557 eh->elf.root.u.def.value,
6558 &rsec, NULL, FALSE) != (bfd_vma) -1)
6559 eh->elf.root.u.def.section->gc_mark = 1;
6560 else
6561 rsec = h->root.u.def.section;
6562 break;
6563
6564 case bfd_link_hash_common:
6565 rsec = h->root.u.c.p->section;
6566 break;
6567
6568 default:
6569 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6570 }
6571 }
6572 }
6573 else
6574 {
6575 struct _opd_sec_data *opd;
6576
6577 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6578 opd = get_opd_info (rsec);
6579 if (opd != NULL && opd->func_sec != NULL)
6580 {
6581 rsec->gc_mark = 1;
6582
6583 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6584 }
6585 }
6586
6587 return rsec;
6588 }
6589
6590 /* The maximum size of .sfpr. */
6591 #define SFPR_MAX (218*4)
6592
6593 struct sfpr_def_parms
6594 {
6595 const char name[12];
6596 unsigned char lo, hi;
6597 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6598 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6599 };
6600
6601 /* Auto-generate _save*, _rest* functions in .sfpr.
6602 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6603 instead. */
6604
6605 static bfd_boolean
6606 sfpr_define (struct bfd_link_info *info,
6607 const struct sfpr_def_parms *parm,
6608 asection *stub_sec)
6609 {
6610 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6611 unsigned int i;
6612 size_t len = strlen (parm->name);
6613 bfd_boolean writing = FALSE;
6614 char sym[16];
6615
6616 if (htab == NULL)
6617 return FALSE;
6618
6619 memcpy (sym, parm->name, len);
6620 sym[len + 2] = 0;
6621
6622 for (i = parm->lo; i <= parm->hi; i++)
6623 {
6624 struct ppc_link_hash_entry *h;
6625
6626 sym[len + 0] = i / 10 + '0';
6627 sym[len + 1] = i % 10 + '0';
6628 h = (struct ppc_link_hash_entry *)
6629 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6630 if (stub_sec != NULL)
6631 {
6632 if (h != NULL
6633 && h->elf.root.type == bfd_link_hash_defined
6634 && h->elf.root.u.def.section == htab->sfpr)
6635 {
6636 struct elf_link_hash_entry *s;
6637 char buf[32];
6638 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6639 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6640 if (s == NULL)
6641 return FALSE;
6642 if (s->root.type == bfd_link_hash_new
6643 || (s->root.type = bfd_link_hash_defined
6644 && s->root.u.def.section == stub_sec))
6645 {
6646 s->root.type = bfd_link_hash_defined;
6647 s->root.u.def.section = stub_sec;
6648 s->root.u.def.value = (stub_sec->size
6649 + h->elf.root.u.def.value);
6650 s->ref_regular = 1;
6651 s->def_regular = 1;
6652 s->ref_regular_nonweak = 1;
6653 s->forced_local = 1;
6654 s->non_elf = 0;
6655 s->root.linker_def = 1;
6656 }
6657 }
6658 continue;
6659 }
6660 if (h != NULL)
6661 {
6662 h->save_res = 1;
6663 if (!h->elf.def_regular)
6664 {
6665 h->elf.root.type = bfd_link_hash_defined;
6666 h->elf.root.u.def.section = htab->sfpr;
6667 h->elf.root.u.def.value = htab->sfpr->size;
6668 h->elf.type = STT_FUNC;
6669 h->elf.def_regular = 1;
6670 h->elf.non_elf = 0;
6671 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6672 writing = TRUE;
6673 if (htab->sfpr->contents == NULL)
6674 {
6675 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6676 if (htab->sfpr->contents == NULL)
6677 return FALSE;
6678 }
6679 }
6680 }
6681 if (writing)
6682 {
6683 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6684 if (i != parm->hi)
6685 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6686 else
6687 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6688 htab->sfpr->size = p - htab->sfpr->contents;
6689 }
6690 }
6691
6692 return TRUE;
6693 }
6694
6695 static bfd_byte *
6696 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6697 {
6698 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6699 return p + 4;
6700 }
6701
6702 static bfd_byte *
6703 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6704 {
6705 p = savegpr0 (abfd, p, r);
6706 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6707 p = p + 4;
6708 bfd_put_32 (abfd, BLR, p);
6709 return p + 4;
6710 }
6711
6712 static bfd_byte *
6713 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6714 {
6715 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6716 return p + 4;
6717 }
6718
6719 static bfd_byte *
6720 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6721 {
6722 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6723 p = p + 4;
6724 p = restgpr0 (abfd, p, r);
6725 bfd_put_32 (abfd, MTLR_R0, p);
6726 p = p + 4;
6727 if (r == 29)
6728 {
6729 p = restgpr0 (abfd, p, 30);
6730 p = restgpr0 (abfd, p, 31);
6731 }
6732 bfd_put_32 (abfd, BLR, p);
6733 return p + 4;
6734 }
6735
6736 static bfd_byte *
6737 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6738 {
6739 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6740 return p + 4;
6741 }
6742
6743 static bfd_byte *
6744 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6745 {
6746 p = savegpr1 (abfd, p, r);
6747 bfd_put_32 (abfd, BLR, p);
6748 return p + 4;
6749 }
6750
6751 static bfd_byte *
6752 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6753 {
6754 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6755 return p + 4;
6756 }
6757
6758 static bfd_byte *
6759 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6760 {
6761 p = restgpr1 (abfd, p, r);
6762 bfd_put_32 (abfd, BLR, p);
6763 return p + 4;
6764 }
6765
6766 static bfd_byte *
6767 savefpr (bfd *abfd, bfd_byte *p, int r)
6768 {
6769 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6770 return p + 4;
6771 }
6772
6773 static bfd_byte *
6774 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6775 {
6776 p = savefpr (abfd, p, r);
6777 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6778 p = p + 4;
6779 bfd_put_32 (abfd, BLR, p);
6780 return p + 4;
6781 }
6782
6783 static bfd_byte *
6784 restfpr (bfd *abfd, bfd_byte *p, int r)
6785 {
6786 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6787 return p + 4;
6788 }
6789
6790 static bfd_byte *
6791 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6792 {
6793 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6794 p = p + 4;
6795 p = restfpr (abfd, p, r);
6796 bfd_put_32 (abfd, MTLR_R0, p);
6797 p = p + 4;
6798 if (r == 29)
6799 {
6800 p = restfpr (abfd, p, 30);
6801 p = restfpr (abfd, p, 31);
6802 }
6803 bfd_put_32 (abfd, BLR, p);
6804 return p + 4;
6805 }
6806
6807 static bfd_byte *
6808 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6809 {
6810 p = savefpr (abfd, p, r);
6811 bfd_put_32 (abfd, BLR, p);
6812 return p + 4;
6813 }
6814
6815 static bfd_byte *
6816 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6817 {
6818 p = restfpr (abfd, p, r);
6819 bfd_put_32 (abfd, BLR, p);
6820 return p + 4;
6821 }
6822
6823 static bfd_byte *
6824 savevr (bfd *abfd, bfd_byte *p, int r)
6825 {
6826 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6827 p = p + 4;
6828 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6829 return p + 4;
6830 }
6831
6832 static bfd_byte *
6833 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6834 {
6835 p = savevr (abfd, p, r);
6836 bfd_put_32 (abfd, BLR, p);
6837 return p + 4;
6838 }
6839
6840 static bfd_byte *
6841 restvr (bfd *abfd, bfd_byte *p, int r)
6842 {
6843 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6844 p = p + 4;
6845 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6846 return p + 4;
6847 }
6848
6849 static bfd_byte *
6850 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6851 {
6852 p = restvr (abfd, p, r);
6853 bfd_put_32 (abfd, BLR, p);
6854 return p + 4;
6855 }
6856
6857 /* Called via elf_link_hash_traverse to transfer dynamic linking
6858 information on function code symbol entries to their corresponding
6859 function descriptor symbol entries. */
6860
6861 static bfd_boolean
6862 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6863 {
6864 struct bfd_link_info *info;
6865 struct ppc_link_hash_table *htab;
6866 struct ppc_link_hash_entry *fh;
6867 struct ppc_link_hash_entry *fdh;
6868 bfd_boolean force_local;
6869
6870 fh = (struct ppc_link_hash_entry *) h;
6871 if (fh->elf.root.type == bfd_link_hash_indirect)
6872 return TRUE;
6873
6874 if (!fh->is_func)
6875 return TRUE;
6876
6877 if (fh->elf.root.root.string[0] != '.'
6878 || fh->elf.root.root.string[1] == '\0')
6879 return TRUE;
6880
6881 info = inf;
6882 htab = ppc_hash_table (info);
6883 if (htab == NULL)
6884 return FALSE;
6885
6886 /* Find the corresponding function descriptor symbol. */
6887 fdh = lookup_fdh (fh, htab);
6888
6889 /* Resolve undefined references to dot-symbols as the value
6890 in the function descriptor, if we have one in a regular object.
6891 This is to satisfy cases like ".quad .foo". Calls to functions
6892 in dynamic objects are handled elsewhere. */
6893 if ((fh->elf.root.type == bfd_link_hash_undefined
6894 || fh->elf.root.type == bfd_link_hash_undefweak)
6895 && (fdh->elf.root.type == bfd_link_hash_defined
6896 || fdh->elf.root.type == bfd_link_hash_defweak)
6897 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6898 && opd_entry_value (fdh->elf.root.u.def.section,
6899 fdh->elf.root.u.def.value,
6900 &fh->elf.root.u.def.section,
6901 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6902 {
6903 fh->elf.root.type = fdh->elf.root.type;
6904 fh->elf.forced_local = 1;
6905 fh->elf.def_regular = fdh->elf.def_regular;
6906 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6907 }
6908
6909 if (!fh->elf.dynamic)
6910 {
6911 struct plt_entry *ent;
6912
6913 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6914 if (ent->plt.refcount > 0)
6915 break;
6916 if (ent == NULL)
6917 return TRUE;
6918 }
6919
6920 /* Create a descriptor as undefined if necessary. */
6921 if (fdh == NULL
6922 && !bfd_link_executable (info)
6923 && (fh->elf.root.type == bfd_link_hash_undefined
6924 || fh->elf.root.type == bfd_link_hash_undefweak))
6925 {
6926 fdh = make_fdh (info, fh);
6927 if (fdh == NULL)
6928 return FALSE;
6929 }
6930
6931 /* We can't support overriding of symbols on a fake descriptor. */
6932 if (fdh != NULL
6933 && fdh->fake
6934 && (fh->elf.root.type == bfd_link_hash_defined
6935 || fh->elf.root.type == bfd_link_hash_defweak))
6936 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6937
6938 /* Transfer dynamic linking information to the function descriptor. */
6939 if (fdh != NULL)
6940 {
6941 fdh->elf.ref_regular |= fh->elf.ref_regular;
6942 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6943 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6944 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6945 fdh->elf.dynamic |= fh->elf.dynamic;
6946 fdh->elf.needs_plt |= (fh->elf.needs_plt
6947 || fh->elf.type == STT_FUNC
6948 || fh->elf.type == STT_GNU_IFUNC);
6949 move_plt_plist (fh, fdh);
6950
6951 if (!fdh->elf.forced_local
6952 && fh->elf.dynindx != -1)
6953 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6954 return FALSE;
6955 }
6956
6957 /* Now that the info is on the function descriptor, clear the
6958 function code sym info. Any function code syms for which we
6959 don't have a definition in a regular file, we force local.
6960 This prevents a shared library from exporting syms that have
6961 been imported from another library. Function code syms that
6962 are really in the library we must leave global to prevent the
6963 linker dragging in a definition from a static library. */
6964 force_local = (!fh->elf.def_regular
6965 || fdh == NULL
6966 || !fdh->elf.def_regular
6967 || fdh->elf.forced_local);
6968 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6969
6970 return TRUE;
6971 }
6972
6973 static const struct sfpr_def_parms save_res_funcs[] =
6974 {
6975 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6976 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6977 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6978 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6979 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6980 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6981 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6982 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6983 { "._savef", 14, 31, savefpr, savefpr1_tail },
6984 { "._restf", 14, 31, restfpr, restfpr1_tail },
6985 { "_savevr_", 20, 31, savevr, savevr_tail },
6986 { "_restvr_", 20, 31, restvr, restvr_tail }
6987 };
6988
6989 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6990 this hook to a) provide some gcc support functions, and b) transfer
6991 dynamic linking information gathered so far on function code symbol
6992 entries, to their corresponding function descriptor symbol entries. */
6993
6994 static bfd_boolean
6995 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6996 struct bfd_link_info *info)
6997 {
6998 struct ppc_link_hash_table *htab;
6999
7000 htab = ppc_hash_table (info);
7001 if (htab == NULL)
7002 return FALSE;
7003
7004 /* Provide any missing _save* and _rest* functions. */
7005 if (htab->sfpr != NULL)
7006 {
7007 unsigned int i;
7008
7009 htab->sfpr->size = 0;
7010 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7011 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7012 return FALSE;
7013 if (htab->sfpr->size == 0)
7014 htab->sfpr->flags |= SEC_EXCLUDE;
7015 }
7016
7017 if (bfd_link_relocatable (info))
7018 return TRUE;
7019
7020 if (htab->elf.hgot != NULL)
7021 {
7022 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7023 /* Make .TOC. defined so as to prevent it being made dynamic.
7024 The wrong value here is fixed later in ppc64_elf_set_toc. */
7025 if (!htab->elf.hgot->def_regular
7026 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7027 {
7028 htab->elf.hgot->root.type = bfd_link_hash_defined;
7029 htab->elf.hgot->root.u.def.value = 0;
7030 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7031 htab->elf.hgot->def_regular = 1;
7032 htab->elf.hgot->root.linker_def = 1;
7033 }
7034 htab->elf.hgot->type = STT_OBJECT;
7035 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7036 | STV_HIDDEN);
7037 }
7038
7039 if (htab->need_func_desc_adj)
7040 {
7041 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7042 htab->need_func_desc_adj = 0;
7043 }
7044
7045 return TRUE;
7046 }
7047
7048 /* Find dynamic relocs for H that apply to read-only sections. */
7049
7050 static asection *
7051 readonly_dynrelocs (struct elf_link_hash_entry *h)
7052 {
7053 struct ppc_link_hash_entry *eh;
7054 struct elf_dyn_relocs *p;
7055
7056 eh = (struct ppc_link_hash_entry *) h;
7057 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7058 {
7059 asection *s = p->sec->output_section;
7060
7061 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7062 return p->sec;
7063 }
7064 return NULL;
7065 }
7066
7067 /* Return true if we have dynamic relocs against H or any of its weak
7068 aliases, that apply to read-only sections. Cannot be used after
7069 size_dynamic_sections. */
7070
7071 static bfd_boolean
7072 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7073 {
7074 struct ppc_link_hash_entry *eh;
7075
7076 eh = (struct ppc_link_hash_entry *) h;
7077 do
7078 {
7079 if (readonly_dynrelocs (&eh->elf))
7080 return TRUE;
7081 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7082 } while (eh != NULL && &eh->elf != h);
7083
7084 return FALSE;
7085 }
7086
7087 /* Return whether EH has pc-relative dynamic relocs. */
7088
7089 static bfd_boolean
7090 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7091 {
7092 struct elf_dyn_relocs *p;
7093
7094 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7095 if (p->pc_count != 0)
7096 return TRUE;
7097 return FALSE;
7098 }
7099
7100 /* Return true if a global entry stub will be created for H. Valid
7101 for ELFv2 before plt entries have been allocated. */
7102
7103 static bfd_boolean
7104 global_entry_stub (struct elf_link_hash_entry *h)
7105 {
7106 struct plt_entry *pent;
7107
7108 if (!h->pointer_equality_needed
7109 || h->def_regular)
7110 return FALSE;
7111
7112 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7113 if (pent->plt.refcount > 0
7114 && pent->addend == 0)
7115 return TRUE;
7116
7117 return FALSE;
7118 }
7119
7120 /* Adjust a symbol defined by a dynamic object and referenced by a
7121 regular object. The current definition is in some section of the
7122 dynamic object, but we're not including those sections. We have to
7123 change the definition to something the rest of the link can
7124 understand. */
7125
7126 static bfd_boolean
7127 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7128 struct elf_link_hash_entry *h)
7129 {
7130 struct ppc_link_hash_table *htab;
7131 asection *s, *srel;
7132
7133 htab = ppc_hash_table (info);
7134 if (htab == NULL)
7135 return FALSE;
7136
7137 /* Deal with function syms. */
7138 if (h->type == STT_FUNC
7139 || h->type == STT_GNU_IFUNC
7140 || h->needs_plt)
7141 {
7142 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7143 || SYMBOL_CALLS_LOCAL (info, h)
7144 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7145 /* Discard dyn_relocs when non-pic if we've decided that a
7146 function symbol is local and not an ifunc. We keep dynamic
7147 relocs for ifuncs when local rather than always emitting a
7148 plt call stub for them and defining the symbol on the call
7149 stub. We can't do that for ELFv1 anyway (a function symbol
7150 is defined on a descriptor, not code) and it can be faster at
7151 run-time due to not needing to bounce through a stub. The
7152 dyn_relocs for ifuncs will be applied even in a static
7153 executable. */
7154 if (!bfd_link_pic (info)
7155 && h->type != STT_GNU_IFUNC
7156 && local)
7157 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7158
7159 /* Clear procedure linkage table information for any symbol that
7160 won't need a .plt entry. */
7161 struct plt_entry *ent;
7162 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7163 if (ent->plt.refcount > 0)
7164 break;
7165 if (ent == NULL
7166 || (h->type != STT_GNU_IFUNC && local))
7167 {
7168 h->plt.plist = NULL;
7169 h->needs_plt = 0;
7170 h->pointer_equality_needed = 0;
7171 }
7172 else if (abiversion (info->output_bfd) >= 2)
7173 {
7174 /* Taking a function's address in a read/write section
7175 doesn't require us to define the function symbol in the
7176 executable on a global entry stub. A dynamic reloc can
7177 be used instead. The reason we prefer a few more dynamic
7178 relocs is that calling via a global entry stub costs a
7179 few more instructions, and pointer_equality_needed causes
7180 extra work in ld.so when resolving these symbols. */
7181 if (global_entry_stub (h))
7182 {
7183 if (!readonly_dynrelocs (h))
7184 {
7185 h->pointer_equality_needed = 0;
7186 /* If we haven't seen a branch reloc then we don't need
7187 a plt entry. */
7188 if (!h->needs_plt)
7189 h->plt.plist = NULL;
7190 }
7191 else if (!bfd_link_pic (info))
7192 /* We are going to be defining the function symbol on the
7193 plt stub, so no dyn_relocs needed when non-pic. */
7194 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7195 }
7196
7197 /* ELFv2 function symbols can't have copy relocs. */
7198 return TRUE;
7199 }
7200 else if (!h->needs_plt
7201 && !readonly_dynrelocs (h))
7202 {
7203 /* If we haven't seen a branch reloc then we don't need a
7204 plt entry. */
7205 h->plt.plist = NULL;
7206 h->pointer_equality_needed = 0;
7207 return TRUE;
7208 }
7209 }
7210 else
7211 h->plt.plist = NULL;
7212
7213 /* If this is a weak symbol, and there is a real definition, the
7214 processor independent code will have arranged for us to see the
7215 real definition first, and we can just use the same value. */
7216 if (h->is_weakalias)
7217 {
7218 struct elf_link_hash_entry *def = weakdef (h);
7219 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7220 h->root.u.def.section = def->root.u.def.section;
7221 h->root.u.def.value = def->root.u.def.value;
7222 if (def->root.u.def.section == htab->elf.sdynbss
7223 || def->root.u.def.section == htab->elf.sdynrelro)
7224 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7225 return TRUE;
7226 }
7227
7228 /* If we are creating a shared library, we must presume that the
7229 only references to the symbol are via the global offset table.
7230 For such cases we need not do anything here; the relocations will
7231 be handled correctly by relocate_section. */
7232 if (bfd_link_pic (info))
7233 return TRUE;
7234
7235 /* If there are no references to this symbol that do not use the
7236 GOT, we don't need to generate a copy reloc. */
7237 if (!h->non_got_ref)
7238 return TRUE;
7239
7240 /* Don't generate a copy reloc for symbols defined in the executable. */
7241 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7242
7243 /* If -z nocopyreloc was given, don't generate them either. */
7244 || info->nocopyreloc
7245
7246 /* If we don't find any dynamic relocs in read-only sections, then
7247 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7248 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7249
7250 /* Protected variables do not work with .dynbss. The copy in
7251 .dynbss won't be used by the shared library with the protected
7252 definition for the variable. Text relocations are preferable
7253 to an incorrect program. */
7254 || h->protected_def)
7255 return TRUE;
7256
7257 if (h->plt.plist != NULL)
7258 {
7259 /* We should never get here, but unfortunately there are versions
7260 of gcc out there that improperly (for this ABI) put initialized
7261 function pointers, vtable refs and suchlike in read-only
7262 sections. Allow them to proceed, but warn that this might
7263 break at runtime. */
7264 info->callbacks->einfo
7265 (_("%P: copy reloc against `%T' requires lazy plt linking; "
7266 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7267 h->root.root.string);
7268 }
7269
7270 /* This is a reference to a symbol defined by a dynamic object which
7271 is not a function. */
7272
7273 /* We must allocate the symbol in our .dynbss section, which will
7274 become part of the .bss section of the executable. There will be
7275 an entry for this symbol in the .dynsym section. The dynamic
7276 object will contain position independent code, so all references
7277 from the dynamic object to this symbol will go through the global
7278 offset table. The dynamic linker will use the .dynsym entry to
7279 determine the address it must put in the global offset table, so
7280 both the dynamic object and the regular object will refer to the
7281 same memory location for the variable. */
7282 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7283 {
7284 s = htab->elf.sdynrelro;
7285 srel = htab->elf.sreldynrelro;
7286 }
7287 else
7288 {
7289 s = htab->elf.sdynbss;
7290 srel = htab->elf.srelbss;
7291 }
7292 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7293 {
7294 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7295 linker to copy the initial value out of the dynamic object
7296 and into the runtime process image. */
7297 srel->size += sizeof (Elf64_External_Rela);
7298 h->needs_copy = 1;
7299 }
7300
7301 /* We no longer want dyn_relocs. */
7302 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7303 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7304 }
7305
7306 /* If given a function descriptor symbol, hide both the function code
7307 sym and the descriptor. */
7308 static void
7309 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7310 struct elf_link_hash_entry *h,
7311 bfd_boolean force_local)
7312 {
7313 struct ppc_link_hash_entry *eh;
7314 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7315
7316 eh = (struct ppc_link_hash_entry *) h;
7317 if (eh->is_func_descriptor)
7318 {
7319 struct ppc_link_hash_entry *fh = eh->oh;
7320
7321 if (fh == NULL)
7322 {
7323 const char *p, *q;
7324 struct elf_link_hash_table *htab = elf_hash_table (info);
7325 char save;
7326
7327 /* We aren't supposed to use alloca in BFD because on
7328 systems which do not have alloca the version in libiberty
7329 calls xmalloc, which might cause the program to crash
7330 when it runs out of memory. This function doesn't have a
7331 return status, so there's no way to gracefully return an
7332 error. So cheat. We know that string[-1] can be safely
7333 accessed; It's either a string in an ELF string table,
7334 or allocated in an objalloc structure. */
7335
7336 p = eh->elf.root.root.string - 1;
7337 save = *p;
7338 *(char *) p = '.';
7339 fh = (struct ppc_link_hash_entry *)
7340 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7341 *(char *) p = save;
7342
7343 /* Unfortunately, if it so happens that the string we were
7344 looking for was allocated immediately before this string,
7345 then we overwrote the string terminator. That's the only
7346 reason the lookup should fail. */
7347 if (fh == NULL)
7348 {
7349 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7350 while (q >= eh->elf.root.root.string && *q == *p)
7351 --q, --p;
7352 if (q < eh->elf.root.root.string && *p == '.')
7353 fh = (struct ppc_link_hash_entry *)
7354 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7355 }
7356 if (fh != NULL)
7357 {
7358 eh->oh = fh;
7359 fh->oh = eh;
7360 }
7361 }
7362 if (fh != NULL)
7363 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7364 }
7365 }
7366
7367 static bfd_boolean
7368 get_sym_h (struct elf_link_hash_entry **hp,
7369 Elf_Internal_Sym **symp,
7370 asection **symsecp,
7371 unsigned char **tls_maskp,
7372 Elf_Internal_Sym **locsymsp,
7373 unsigned long r_symndx,
7374 bfd *ibfd)
7375 {
7376 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7377
7378 if (r_symndx >= symtab_hdr->sh_info)
7379 {
7380 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7381 struct elf_link_hash_entry *h;
7382
7383 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7384 h = elf_follow_link (h);
7385
7386 if (hp != NULL)
7387 *hp = h;
7388
7389 if (symp != NULL)
7390 *symp = NULL;
7391
7392 if (symsecp != NULL)
7393 {
7394 asection *symsec = NULL;
7395 if (h->root.type == bfd_link_hash_defined
7396 || h->root.type == bfd_link_hash_defweak)
7397 symsec = h->root.u.def.section;
7398 *symsecp = symsec;
7399 }
7400
7401 if (tls_maskp != NULL)
7402 {
7403 struct ppc_link_hash_entry *eh;
7404
7405 eh = (struct ppc_link_hash_entry *) h;
7406 *tls_maskp = &eh->tls_mask;
7407 }
7408 }
7409 else
7410 {
7411 Elf_Internal_Sym *sym;
7412 Elf_Internal_Sym *locsyms = *locsymsp;
7413
7414 if (locsyms == NULL)
7415 {
7416 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7417 if (locsyms == NULL)
7418 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7419 symtab_hdr->sh_info,
7420 0, NULL, NULL, NULL);
7421 if (locsyms == NULL)
7422 return FALSE;
7423 *locsymsp = locsyms;
7424 }
7425 sym = locsyms + r_symndx;
7426
7427 if (hp != NULL)
7428 *hp = NULL;
7429
7430 if (symp != NULL)
7431 *symp = sym;
7432
7433 if (symsecp != NULL)
7434 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7435
7436 if (tls_maskp != NULL)
7437 {
7438 struct got_entry **lgot_ents;
7439 unsigned char *tls_mask;
7440
7441 tls_mask = NULL;
7442 lgot_ents = elf_local_got_ents (ibfd);
7443 if (lgot_ents != NULL)
7444 {
7445 struct plt_entry **local_plt = (struct plt_entry **)
7446 (lgot_ents + symtab_hdr->sh_info);
7447 unsigned char *lgot_masks = (unsigned char *)
7448 (local_plt + symtab_hdr->sh_info);
7449 tls_mask = &lgot_masks[r_symndx];
7450 }
7451 *tls_maskp = tls_mask;
7452 }
7453 }
7454 return TRUE;
7455 }
7456
7457 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7458 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7459 type suitable for optimization, and 1 otherwise. */
7460
7461 static int
7462 get_tls_mask (unsigned char **tls_maskp,
7463 unsigned long *toc_symndx,
7464 bfd_vma *toc_addend,
7465 Elf_Internal_Sym **locsymsp,
7466 const Elf_Internal_Rela *rel,
7467 bfd *ibfd)
7468 {
7469 unsigned long r_symndx;
7470 int next_r;
7471 struct elf_link_hash_entry *h;
7472 Elf_Internal_Sym *sym;
7473 asection *sec;
7474 bfd_vma off;
7475
7476 r_symndx = ELF64_R_SYM (rel->r_info);
7477 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7478 return 0;
7479
7480 if ((*tls_maskp != NULL && **tls_maskp != 0)
7481 || sec == NULL
7482 || ppc64_elf_section_data (sec) == NULL
7483 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7484 return 1;
7485
7486 /* Look inside a TOC section too. */
7487 if (h != NULL)
7488 {
7489 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7490 off = h->root.u.def.value;
7491 }
7492 else
7493 off = sym->st_value;
7494 off += rel->r_addend;
7495 BFD_ASSERT (off % 8 == 0);
7496 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7497 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7498 if (toc_symndx != NULL)
7499 *toc_symndx = r_symndx;
7500 if (toc_addend != NULL)
7501 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7502 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7503 return 0;
7504 if ((h == NULL || is_static_defined (h))
7505 && (next_r == -1 || next_r == -2))
7506 return 1 - next_r;
7507 return 1;
7508 }
7509
7510 /* Find (or create) an entry in the tocsave hash table. */
7511
7512 static struct tocsave_entry *
7513 tocsave_find (struct ppc_link_hash_table *htab,
7514 enum insert_option insert,
7515 Elf_Internal_Sym **local_syms,
7516 const Elf_Internal_Rela *irela,
7517 bfd *ibfd)
7518 {
7519 unsigned long r_indx;
7520 struct elf_link_hash_entry *h;
7521 Elf_Internal_Sym *sym;
7522 struct tocsave_entry ent, *p;
7523 hashval_t hash;
7524 struct tocsave_entry **slot;
7525
7526 r_indx = ELF64_R_SYM (irela->r_info);
7527 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7528 return NULL;
7529 if (ent.sec == NULL || ent.sec->output_section == NULL)
7530 {
7531 _bfd_error_handler
7532 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7533 return NULL;
7534 }
7535
7536 if (h != NULL)
7537 ent.offset = h->root.u.def.value;
7538 else
7539 ent.offset = sym->st_value;
7540 ent.offset += irela->r_addend;
7541
7542 hash = tocsave_htab_hash (&ent);
7543 slot = ((struct tocsave_entry **)
7544 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7545 if (slot == NULL)
7546 return NULL;
7547
7548 if (*slot == NULL)
7549 {
7550 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7551 if (p == NULL)
7552 return NULL;
7553 *p = ent;
7554 *slot = p;
7555 }
7556 return *slot;
7557 }
7558
7559 /* Adjust all global syms defined in opd sections. In gcc generated
7560 code for the old ABI, these will already have been done. */
7561
7562 static bfd_boolean
7563 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7564 {
7565 struct ppc_link_hash_entry *eh;
7566 asection *sym_sec;
7567 struct _opd_sec_data *opd;
7568
7569 if (h->root.type == bfd_link_hash_indirect)
7570 return TRUE;
7571
7572 if (h->root.type != bfd_link_hash_defined
7573 && h->root.type != bfd_link_hash_defweak)
7574 return TRUE;
7575
7576 eh = (struct ppc_link_hash_entry *) h;
7577 if (eh->adjust_done)
7578 return TRUE;
7579
7580 sym_sec = eh->elf.root.u.def.section;
7581 opd = get_opd_info (sym_sec);
7582 if (opd != NULL && opd->adjust != NULL)
7583 {
7584 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7585 if (adjust == -1)
7586 {
7587 /* This entry has been deleted. */
7588 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7589 if (dsec == NULL)
7590 {
7591 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7592 if (discarded_section (dsec))
7593 {
7594 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7595 break;
7596 }
7597 }
7598 eh->elf.root.u.def.value = 0;
7599 eh->elf.root.u.def.section = dsec;
7600 }
7601 else
7602 eh->elf.root.u.def.value += adjust;
7603 eh->adjust_done = 1;
7604 }
7605 return TRUE;
7606 }
7607
7608 /* Handles decrementing dynamic reloc counts for the reloc specified by
7609 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7610 have already been determined. */
7611
7612 static bfd_boolean
7613 dec_dynrel_count (bfd_vma r_info,
7614 asection *sec,
7615 struct bfd_link_info *info,
7616 Elf_Internal_Sym **local_syms,
7617 struct elf_link_hash_entry *h,
7618 Elf_Internal_Sym *sym)
7619 {
7620 enum elf_ppc64_reloc_type r_type;
7621 asection *sym_sec = NULL;
7622
7623 /* Can this reloc be dynamic? This switch, and later tests here
7624 should be kept in sync with the code in check_relocs. */
7625 r_type = ELF64_R_TYPE (r_info);
7626 switch (r_type)
7627 {
7628 default:
7629 return TRUE;
7630
7631 case R_PPC64_TPREL16:
7632 case R_PPC64_TPREL16_LO:
7633 case R_PPC64_TPREL16_HI:
7634 case R_PPC64_TPREL16_HA:
7635 case R_PPC64_TPREL16_DS:
7636 case R_PPC64_TPREL16_LO_DS:
7637 case R_PPC64_TPREL16_HIGH:
7638 case R_PPC64_TPREL16_HIGHA:
7639 case R_PPC64_TPREL16_HIGHER:
7640 case R_PPC64_TPREL16_HIGHERA:
7641 case R_PPC64_TPREL16_HIGHEST:
7642 case R_PPC64_TPREL16_HIGHESTA:
7643 case R_PPC64_TPREL64:
7644 case R_PPC64_DTPMOD64:
7645 case R_PPC64_DTPREL64:
7646 case R_PPC64_ADDR64:
7647 case R_PPC64_REL30:
7648 case R_PPC64_REL32:
7649 case R_PPC64_REL64:
7650 case R_PPC64_ADDR14:
7651 case R_PPC64_ADDR14_BRNTAKEN:
7652 case R_PPC64_ADDR14_BRTAKEN:
7653 case R_PPC64_ADDR16:
7654 case R_PPC64_ADDR16_DS:
7655 case R_PPC64_ADDR16_HA:
7656 case R_PPC64_ADDR16_HI:
7657 case R_PPC64_ADDR16_HIGH:
7658 case R_PPC64_ADDR16_HIGHA:
7659 case R_PPC64_ADDR16_HIGHER:
7660 case R_PPC64_ADDR16_HIGHERA:
7661 case R_PPC64_ADDR16_HIGHEST:
7662 case R_PPC64_ADDR16_HIGHESTA:
7663 case R_PPC64_ADDR16_LO:
7664 case R_PPC64_ADDR16_LO_DS:
7665 case R_PPC64_ADDR24:
7666 case R_PPC64_ADDR32:
7667 case R_PPC64_UADDR16:
7668 case R_PPC64_UADDR32:
7669 case R_PPC64_UADDR64:
7670 case R_PPC64_TOC:
7671 break;
7672 }
7673
7674 if (local_syms != NULL)
7675 {
7676 unsigned long r_symndx;
7677 bfd *ibfd = sec->owner;
7678
7679 r_symndx = ELF64_R_SYM (r_info);
7680 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7681 return FALSE;
7682 }
7683
7684 if ((bfd_link_pic (info)
7685 && (must_be_dyn_reloc (info, r_type)
7686 || (h != NULL
7687 && (!SYMBOLIC_BIND (info, h)
7688 || h->root.type == bfd_link_hash_defweak
7689 || !h->def_regular))))
7690 || (ELIMINATE_COPY_RELOCS
7691 && !bfd_link_pic (info)
7692 && h != NULL
7693 && (h->root.type == bfd_link_hash_defweak
7694 || !h->def_regular)))
7695 ;
7696 else
7697 return TRUE;
7698
7699 if (h != NULL)
7700 {
7701 struct elf_dyn_relocs *p;
7702 struct elf_dyn_relocs **pp;
7703 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7704
7705 /* elf_gc_sweep may have already removed all dyn relocs associated
7706 with local syms for a given section. Also, symbol flags are
7707 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7708 report a dynreloc miscount. */
7709 if (*pp == NULL && info->gc_sections)
7710 return TRUE;
7711
7712 while ((p = *pp) != NULL)
7713 {
7714 if (p->sec == sec)
7715 {
7716 if (!must_be_dyn_reloc (info, r_type))
7717 p->pc_count -= 1;
7718 p->count -= 1;
7719 if (p->count == 0)
7720 *pp = p->next;
7721 return TRUE;
7722 }
7723 pp = &p->next;
7724 }
7725 }
7726 else
7727 {
7728 struct ppc_dyn_relocs *p;
7729 struct ppc_dyn_relocs **pp;
7730 void *vpp;
7731 bfd_boolean is_ifunc;
7732
7733 if (local_syms == NULL)
7734 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7735 if (sym_sec == NULL)
7736 sym_sec = sec;
7737
7738 vpp = &elf_section_data (sym_sec)->local_dynrel;
7739 pp = (struct ppc_dyn_relocs **) vpp;
7740
7741 if (*pp == NULL && info->gc_sections)
7742 return TRUE;
7743
7744 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7745 while ((p = *pp) != NULL)
7746 {
7747 if (p->sec == sec && p->ifunc == is_ifunc)
7748 {
7749 p->count -= 1;
7750 if (p->count == 0)
7751 *pp = p->next;
7752 return TRUE;
7753 }
7754 pp = &p->next;
7755 }
7756 }
7757
7758 /* xgettext:c-format */
7759 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7760 sec->owner, sec);
7761 bfd_set_error (bfd_error_bad_value);
7762 return FALSE;
7763 }
7764
7765 /* Remove unused Official Procedure Descriptor entries. Currently we
7766 only remove those associated with functions in discarded link-once
7767 sections, or weakly defined functions that have been overridden. It
7768 would be possible to remove many more entries for statically linked
7769 applications. */
7770
7771 bfd_boolean
7772 ppc64_elf_edit_opd (struct bfd_link_info *info)
7773 {
7774 bfd *ibfd;
7775 bfd_boolean some_edited = FALSE;
7776 asection *need_pad = NULL;
7777 struct ppc_link_hash_table *htab;
7778
7779 htab = ppc_hash_table (info);
7780 if (htab == NULL)
7781 return FALSE;
7782
7783 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7784 {
7785 asection *sec;
7786 Elf_Internal_Rela *relstart, *rel, *relend;
7787 Elf_Internal_Shdr *symtab_hdr;
7788 Elf_Internal_Sym *local_syms;
7789 struct _opd_sec_data *opd;
7790 bfd_boolean need_edit, add_aux_fields, broken;
7791 bfd_size_type cnt_16b = 0;
7792
7793 if (!is_ppc64_elf (ibfd))
7794 continue;
7795
7796 sec = bfd_get_section_by_name (ibfd, ".opd");
7797 if (sec == NULL || sec->size == 0)
7798 continue;
7799
7800 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7801 continue;
7802
7803 if (sec->output_section == bfd_abs_section_ptr)
7804 continue;
7805
7806 /* Look through the section relocs. */
7807 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7808 continue;
7809
7810 local_syms = NULL;
7811 symtab_hdr = &elf_symtab_hdr (ibfd);
7812
7813 /* Read the relocations. */
7814 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7815 info->keep_memory);
7816 if (relstart == NULL)
7817 return FALSE;
7818
7819 /* First run through the relocs to check they are sane, and to
7820 determine whether we need to edit this opd section. */
7821 need_edit = FALSE;
7822 broken = FALSE;
7823 need_pad = sec;
7824 relend = relstart + sec->reloc_count;
7825 for (rel = relstart; rel < relend; )
7826 {
7827 enum elf_ppc64_reloc_type r_type;
7828 unsigned long r_symndx;
7829 asection *sym_sec;
7830 struct elf_link_hash_entry *h;
7831 Elf_Internal_Sym *sym;
7832 bfd_vma offset;
7833
7834 /* .opd contains an array of 16 or 24 byte entries. We're
7835 only interested in the reloc pointing to a function entry
7836 point. */
7837 offset = rel->r_offset;
7838 if (rel + 1 == relend
7839 || rel[1].r_offset != offset + 8)
7840 {
7841 /* If someone messes with .opd alignment then after a
7842 "ld -r" we might have padding in the middle of .opd.
7843 Also, there's nothing to prevent someone putting
7844 something silly in .opd with the assembler. No .opd
7845 optimization for them! */
7846 broken_opd:
7847 _bfd_error_handler
7848 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7849 broken = TRUE;
7850 break;
7851 }
7852
7853 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7854 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7855 {
7856 _bfd_error_handler
7857 /* xgettext:c-format */
7858 (_("%B: unexpected reloc type %u in .opd section"),
7859 ibfd, r_type);
7860 broken = TRUE;
7861 break;
7862 }
7863
7864 r_symndx = ELF64_R_SYM (rel->r_info);
7865 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7866 r_symndx, ibfd))
7867 goto error_ret;
7868
7869 if (sym_sec == NULL || sym_sec->owner == NULL)
7870 {
7871 const char *sym_name;
7872 if (h != NULL)
7873 sym_name = h->root.root.string;
7874 else
7875 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7876 sym_sec);
7877
7878 _bfd_error_handler
7879 /* xgettext:c-format */
7880 (_("%B: undefined sym `%s' in .opd section"),
7881 ibfd, sym_name);
7882 broken = TRUE;
7883 break;
7884 }
7885
7886 /* opd entries are always for functions defined in the
7887 current input bfd. If the symbol isn't defined in the
7888 input bfd, then we won't be using the function in this
7889 bfd; It must be defined in a linkonce section in another
7890 bfd, or is weak. It's also possible that we are
7891 discarding the function due to a linker script /DISCARD/,
7892 which we test for via the output_section. */
7893 if (sym_sec->owner != ibfd
7894 || sym_sec->output_section == bfd_abs_section_ptr)
7895 need_edit = TRUE;
7896
7897 rel += 2;
7898 if (rel + 1 == relend
7899 || (rel + 2 < relend
7900 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7901 ++rel;
7902
7903 if (rel == relend)
7904 {
7905 if (sec->size == offset + 24)
7906 {
7907 need_pad = NULL;
7908 break;
7909 }
7910 if (sec->size == offset + 16)
7911 {
7912 cnt_16b++;
7913 break;
7914 }
7915 goto broken_opd;
7916 }
7917 else if (rel + 1 < relend
7918 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7919 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7920 {
7921 if (rel[0].r_offset == offset + 16)
7922 cnt_16b++;
7923 else if (rel[0].r_offset != offset + 24)
7924 goto broken_opd;
7925 }
7926 else
7927 goto broken_opd;
7928 }
7929
7930 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7931
7932 if (!broken && (need_edit || add_aux_fields))
7933 {
7934 Elf_Internal_Rela *write_rel;
7935 Elf_Internal_Shdr *rel_hdr;
7936 bfd_byte *rptr, *wptr;
7937 bfd_byte *new_contents;
7938 bfd_size_type amt;
7939
7940 new_contents = NULL;
7941 amt = OPD_NDX (sec->size) * sizeof (long);
7942 opd = &ppc64_elf_section_data (sec)->u.opd;
7943 opd->adjust = bfd_zalloc (sec->owner, amt);
7944 if (opd->adjust == NULL)
7945 return FALSE;
7946
7947 /* This seems a waste of time as input .opd sections are all
7948 zeros as generated by gcc, but I suppose there's no reason
7949 this will always be so. We might start putting something in
7950 the third word of .opd entries. */
7951 if ((sec->flags & SEC_IN_MEMORY) == 0)
7952 {
7953 bfd_byte *loc;
7954 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7955 {
7956 if (loc != NULL)
7957 free (loc);
7958 error_ret:
7959 if (local_syms != NULL
7960 && symtab_hdr->contents != (unsigned char *) local_syms)
7961 free (local_syms);
7962 if (elf_section_data (sec)->relocs != relstart)
7963 free (relstart);
7964 return FALSE;
7965 }
7966 sec->contents = loc;
7967 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7968 }
7969
7970 elf_section_data (sec)->relocs = relstart;
7971
7972 new_contents = sec->contents;
7973 if (add_aux_fields)
7974 {
7975 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7976 if (new_contents == NULL)
7977 return FALSE;
7978 need_pad = NULL;
7979 }
7980 wptr = new_contents;
7981 rptr = sec->contents;
7982 write_rel = relstart;
7983 for (rel = relstart; rel < relend; )
7984 {
7985 unsigned long r_symndx;
7986 asection *sym_sec;
7987 struct elf_link_hash_entry *h;
7988 struct ppc_link_hash_entry *fdh = NULL;
7989 Elf_Internal_Sym *sym;
7990 long opd_ent_size;
7991 Elf_Internal_Rela *next_rel;
7992 bfd_boolean skip;
7993
7994 r_symndx = ELF64_R_SYM (rel->r_info);
7995 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7996 r_symndx, ibfd))
7997 goto error_ret;
7998
7999 next_rel = rel + 2;
8000 if (next_rel + 1 == relend
8001 || (next_rel + 2 < relend
8002 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8003 ++next_rel;
8004
8005 /* See if the .opd entry is full 24 byte or
8006 16 byte (with fd_aux entry overlapped with next
8007 fd_func). */
8008 opd_ent_size = 24;
8009 if (next_rel == relend)
8010 {
8011 if (sec->size == rel->r_offset + 16)
8012 opd_ent_size = 16;
8013 }
8014 else if (next_rel->r_offset == rel->r_offset + 16)
8015 opd_ent_size = 16;
8016
8017 if (h != NULL
8018 && h->root.root.string[0] == '.')
8019 {
8020 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8021 if (fdh != NULL)
8022 {
8023 fdh = ppc_follow_link (fdh);
8024 if (fdh->elf.root.type != bfd_link_hash_defined
8025 && fdh->elf.root.type != bfd_link_hash_defweak)
8026 fdh = NULL;
8027 }
8028 }
8029
8030 skip = (sym_sec->owner != ibfd
8031 || sym_sec->output_section == bfd_abs_section_ptr);
8032 if (skip)
8033 {
8034 if (fdh != NULL && sym_sec->owner == ibfd)
8035 {
8036 /* Arrange for the function descriptor sym
8037 to be dropped. */
8038 fdh->elf.root.u.def.value = 0;
8039 fdh->elf.root.u.def.section = sym_sec;
8040 }
8041 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8042
8043 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8044 rel = next_rel;
8045 else
8046 while (1)
8047 {
8048 if (!dec_dynrel_count (rel->r_info, sec, info,
8049 NULL, h, sym))
8050 goto error_ret;
8051
8052 if (++rel == next_rel)
8053 break;
8054
8055 r_symndx = ELF64_R_SYM (rel->r_info);
8056 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8057 r_symndx, ibfd))
8058 goto error_ret;
8059 }
8060 }
8061 else
8062 {
8063 /* We'll be keeping this opd entry. */
8064 long adjust;
8065
8066 if (fdh != NULL)
8067 {
8068 /* Redefine the function descriptor symbol to
8069 this location in the opd section. It is
8070 necessary to update the value here rather
8071 than using an array of adjustments as we do
8072 for local symbols, because various places
8073 in the generic ELF code use the value
8074 stored in u.def.value. */
8075 fdh->elf.root.u.def.value = wptr - new_contents;
8076 fdh->adjust_done = 1;
8077 }
8078
8079 /* Local syms are a bit tricky. We could
8080 tweak them as they can be cached, but
8081 we'd need to look through the local syms
8082 for the function descriptor sym which we
8083 don't have at the moment. So keep an
8084 array of adjustments. */
8085 adjust = (wptr - new_contents) - (rptr - sec->contents);
8086 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8087
8088 if (wptr != rptr)
8089 memcpy (wptr, rptr, opd_ent_size);
8090 wptr += opd_ent_size;
8091 if (add_aux_fields && opd_ent_size == 16)
8092 {
8093 memset (wptr, '\0', 8);
8094 wptr += 8;
8095 }
8096
8097 /* We need to adjust any reloc offsets to point to the
8098 new opd entries. */
8099 for ( ; rel != next_rel; ++rel)
8100 {
8101 rel->r_offset += adjust;
8102 if (write_rel != rel)
8103 memcpy (write_rel, rel, sizeof (*rel));
8104 ++write_rel;
8105 }
8106 }
8107
8108 rptr += opd_ent_size;
8109 }
8110
8111 sec->size = wptr - new_contents;
8112 sec->reloc_count = write_rel - relstart;
8113 if (add_aux_fields)
8114 {
8115 free (sec->contents);
8116 sec->contents = new_contents;
8117 }
8118
8119 /* Fudge the header size too, as this is used later in
8120 elf_bfd_final_link if we are emitting relocs. */
8121 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8122 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8123 some_edited = TRUE;
8124 }
8125 else if (elf_section_data (sec)->relocs != relstart)
8126 free (relstart);
8127
8128 if (local_syms != NULL
8129 && symtab_hdr->contents != (unsigned char *) local_syms)
8130 {
8131 if (!info->keep_memory)
8132 free (local_syms);
8133 else
8134 symtab_hdr->contents = (unsigned char *) local_syms;
8135 }
8136 }
8137
8138 if (some_edited)
8139 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8140
8141 /* If we are doing a final link and the last .opd entry is just 16 byte
8142 long, add a 8 byte padding after it. */
8143 if (need_pad != NULL && !bfd_link_relocatable (info))
8144 {
8145 bfd_byte *p;
8146
8147 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8148 {
8149 BFD_ASSERT (need_pad->size > 0);
8150
8151 p = bfd_malloc (need_pad->size + 8);
8152 if (p == NULL)
8153 return FALSE;
8154
8155 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8156 p, 0, need_pad->size))
8157 return FALSE;
8158
8159 need_pad->contents = p;
8160 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8161 }
8162 else
8163 {
8164 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8165 if (p == NULL)
8166 return FALSE;
8167
8168 need_pad->contents = p;
8169 }
8170
8171 memset (need_pad->contents + need_pad->size, 0, 8);
8172 need_pad->size += 8;
8173 }
8174
8175 return TRUE;
8176 }
8177
8178 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8179
8180 asection *
8181 ppc64_elf_tls_setup (struct bfd_link_info *info)
8182 {
8183 struct ppc_link_hash_table *htab;
8184
8185 htab = ppc_hash_table (info);
8186 if (htab == NULL)
8187 return NULL;
8188
8189 if (abiversion (info->output_bfd) == 1)
8190 htab->opd_abi = 1;
8191
8192 if (htab->params->no_multi_toc)
8193 htab->do_multi_toc = 0;
8194 else if (!htab->do_multi_toc)
8195 htab->params->no_multi_toc = 1;
8196
8197 /* Default to --no-plt-localentry, as this option can cause problems
8198 with symbol interposition. For example, glibc libpthread.so and
8199 libc.so duplicate many pthread symbols, with a fallback
8200 implementation in libc.so. In some cases the fallback does more
8201 work than the pthread implementation. __pthread_condattr_destroy
8202 is one such symbol: the libpthread.so implementation is
8203 localentry:0 while the libc.so implementation is localentry:8.
8204 An app that "cleverly" uses dlopen to only load necessary
8205 libraries at runtime may omit loading libpthread.so when not
8206 running multi-threaded, which then results in the libc.so
8207 fallback symbols being used and ld.so complaining. Now there
8208 are workarounds in ld (see non_zero_localentry) to detect the
8209 pthread situation, but that may not be the only case where
8210 --plt-localentry can cause trouble. */
8211 if (htab->params->plt_localentry0 < 0)
8212 htab->params->plt_localentry0 = 0;
8213 if (htab->params->plt_localentry0
8214 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8215 FALSE, FALSE, FALSE) == NULL)
8216 info->callbacks->einfo
8217 (_("%P: warning: --plt-localentry is especially dangerous without "
8218 "ld.so support to detect ABI violations.\n"));
8219
8220 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8221 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8222 FALSE, FALSE, TRUE));
8223 /* Move dynamic linking info to the function descriptor sym. */
8224 if (htab->tls_get_addr != NULL)
8225 func_desc_adjust (&htab->tls_get_addr->elf, info);
8226 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8227 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8228 FALSE, FALSE, TRUE));
8229 if (htab->params->tls_get_addr_opt)
8230 {
8231 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8232
8233 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8234 FALSE, FALSE, TRUE);
8235 if (opt != NULL)
8236 func_desc_adjust (opt, info);
8237 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8238 FALSE, FALSE, TRUE);
8239 if (opt_fd != NULL
8240 && (opt_fd->root.type == bfd_link_hash_defined
8241 || opt_fd->root.type == bfd_link_hash_defweak))
8242 {
8243 /* If glibc supports an optimized __tls_get_addr call stub,
8244 signalled by the presence of __tls_get_addr_opt, and we'll
8245 be calling __tls_get_addr via a plt call stub, then
8246 make __tls_get_addr point to __tls_get_addr_opt. */
8247 tga_fd = &htab->tls_get_addr_fd->elf;
8248 if (htab->elf.dynamic_sections_created
8249 && tga_fd != NULL
8250 && (tga_fd->type == STT_FUNC
8251 || tga_fd->needs_plt)
8252 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8253 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8254 {
8255 struct plt_entry *ent;
8256
8257 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8258 if (ent->plt.refcount > 0)
8259 break;
8260 if (ent != NULL)
8261 {
8262 tga_fd->root.type = bfd_link_hash_indirect;
8263 tga_fd->root.u.i.link = &opt_fd->root;
8264 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8265 opt_fd->mark = 1;
8266 if (opt_fd->dynindx != -1)
8267 {
8268 /* Use __tls_get_addr_opt in dynamic relocations. */
8269 opt_fd->dynindx = -1;
8270 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8271 opt_fd->dynstr_index);
8272 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8273 return NULL;
8274 }
8275 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8276 tga = &htab->tls_get_addr->elf;
8277 if (opt != NULL && tga != NULL)
8278 {
8279 tga->root.type = bfd_link_hash_indirect;
8280 tga->root.u.i.link = &opt->root;
8281 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8282 opt->mark = 1;
8283 _bfd_elf_link_hash_hide_symbol (info, opt,
8284 tga->forced_local);
8285 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8286 }
8287 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8288 htab->tls_get_addr_fd->is_func_descriptor = 1;
8289 if (htab->tls_get_addr != NULL)
8290 {
8291 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8292 htab->tls_get_addr->is_func = 1;
8293 }
8294 }
8295 }
8296 }
8297 else if (htab->params->tls_get_addr_opt < 0)
8298 htab->params->tls_get_addr_opt = 0;
8299 }
8300 return _bfd_elf_tls_setup (info->output_bfd, info);
8301 }
8302
8303 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8304 HASH1 or HASH2. */
8305
8306 static bfd_boolean
8307 branch_reloc_hash_match (const bfd *ibfd,
8308 const Elf_Internal_Rela *rel,
8309 const struct ppc_link_hash_entry *hash1,
8310 const struct ppc_link_hash_entry *hash2)
8311 {
8312 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8313 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8314 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8315
8316 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8317 {
8318 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8319 struct elf_link_hash_entry *h;
8320
8321 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8322 h = elf_follow_link (h);
8323 if (h == &hash1->elf || h == &hash2->elf)
8324 return TRUE;
8325 }
8326 return FALSE;
8327 }
8328
8329 /* Run through all the TLS relocs looking for optimization
8330 opportunities. The linker has been hacked (see ppc64elf.em) to do
8331 a preliminary section layout so that we know the TLS segment
8332 offsets. We can't optimize earlier because some optimizations need
8333 to know the tp offset, and we need to optimize before allocating
8334 dynamic relocations. */
8335
8336 bfd_boolean
8337 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8338 {
8339 bfd *ibfd;
8340 asection *sec;
8341 struct ppc_link_hash_table *htab;
8342 unsigned char *toc_ref;
8343 int pass;
8344
8345 if (!bfd_link_executable (info))
8346 return TRUE;
8347
8348 htab = ppc_hash_table (info);
8349 if (htab == NULL)
8350 return FALSE;
8351
8352 /* Make two passes over the relocs. On the first pass, mark toc
8353 entries involved with tls relocs, and check that tls relocs
8354 involved in setting up a tls_get_addr call are indeed followed by
8355 such a call. If they are not, we can't do any tls optimization.
8356 On the second pass twiddle tls_mask flags to notify
8357 relocate_section that optimization can be done, and adjust got
8358 and plt refcounts. */
8359 toc_ref = NULL;
8360 for (pass = 0; pass < 2; ++pass)
8361 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8362 {
8363 Elf_Internal_Sym *locsyms = NULL;
8364 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8365
8366 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8367 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8368 {
8369 Elf_Internal_Rela *relstart, *rel, *relend;
8370 bfd_boolean found_tls_get_addr_arg = 0;
8371
8372 /* Read the relocations. */
8373 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8374 info->keep_memory);
8375 if (relstart == NULL)
8376 {
8377 free (toc_ref);
8378 return FALSE;
8379 }
8380
8381 relend = relstart + sec->reloc_count;
8382 for (rel = relstart; rel < relend; rel++)
8383 {
8384 enum elf_ppc64_reloc_type r_type;
8385 unsigned long r_symndx;
8386 struct elf_link_hash_entry *h;
8387 Elf_Internal_Sym *sym;
8388 asection *sym_sec;
8389 unsigned char *tls_mask;
8390 unsigned char tls_set, tls_clear, tls_type = 0;
8391 bfd_vma value;
8392 bfd_boolean ok_tprel, is_local;
8393 long toc_ref_index = 0;
8394 int expecting_tls_get_addr = 0;
8395 bfd_boolean ret = FALSE;
8396
8397 r_symndx = ELF64_R_SYM (rel->r_info);
8398 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8399 r_symndx, ibfd))
8400 {
8401 err_free_rel:
8402 if (elf_section_data (sec)->relocs != relstart)
8403 free (relstart);
8404 if (toc_ref != NULL)
8405 free (toc_ref);
8406 if (locsyms != NULL
8407 && (elf_symtab_hdr (ibfd).contents
8408 != (unsigned char *) locsyms))
8409 free (locsyms);
8410 return ret;
8411 }
8412
8413 if (h != NULL)
8414 {
8415 if (h->root.type == bfd_link_hash_defined
8416 || h->root.type == bfd_link_hash_defweak)
8417 value = h->root.u.def.value;
8418 else if (h->root.type == bfd_link_hash_undefweak)
8419 value = 0;
8420 else
8421 {
8422 found_tls_get_addr_arg = 0;
8423 continue;
8424 }
8425 }
8426 else
8427 /* Symbols referenced by TLS relocs must be of type
8428 STT_TLS. So no need for .opd local sym adjust. */
8429 value = sym->st_value;
8430
8431 ok_tprel = FALSE;
8432 is_local = FALSE;
8433 if (h == NULL
8434 || !h->def_dynamic)
8435 {
8436 is_local = TRUE;
8437 if (h != NULL
8438 && h->root.type == bfd_link_hash_undefweak)
8439 ok_tprel = TRUE;
8440 else if (sym_sec != NULL
8441 && sym_sec->output_section != NULL)
8442 {
8443 value += sym_sec->output_offset;
8444 value += sym_sec->output_section->vma;
8445 value -= htab->elf.tls_sec->vma;
8446 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8447 < (bfd_vma) 1 << 32);
8448 }
8449 }
8450
8451 r_type = ELF64_R_TYPE (rel->r_info);
8452 /* If this section has old-style __tls_get_addr calls
8453 without marker relocs, then check that each
8454 __tls_get_addr call reloc is preceded by a reloc
8455 that conceivably belongs to the __tls_get_addr arg
8456 setup insn. If we don't find matching arg setup
8457 relocs, don't do any tls optimization. */
8458 if (pass == 0
8459 && sec->has_tls_get_addr_call
8460 && h != NULL
8461 && (h == &htab->tls_get_addr->elf
8462 || h == &htab->tls_get_addr_fd->elf)
8463 && !found_tls_get_addr_arg
8464 && is_branch_reloc (r_type))
8465 {
8466 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8467 "TLS optimization disabled\n"),
8468 ibfd, sec, rel->r_offset);
8469 ret = TRUE;
8470 goto err_free_rel;
8471 }
8472
8473 found_tls_get_addr_arg = 0;
8474 switch (r_type)
8475 {
8476 case R_PPC64_GOT_TLSLD16:
8477 case R_PPC64_GOT_TLSLD16_LO:
8478 expecting_tls_get_addr = 1;
8479 found_tls_get_addr_arg = 1;
8480 /* Fall through. */
8481
8482 case R_PPC64_GOT_TLSLD16_HI:
8483 case R_PPC64_GOT_TLSLD16_HA:
8484 /* These relocs should never be against a symbol
8485 defined in a shared lib. Leave them alone if
8486 that turns out to be the case. */
8487 if (!is_local)
8488 continue;
8489
8490 /* LD -> LE */
8491 tls_set = 0;
8492 tls_clear = TLS_LD;
8493 tls_type = TLS_TLS | TLS_LD;
8494 break;
8495
8496 case R_PPC64_GOT_TLSGD16:
8497 case R_PPC64_GOT_TLSGD16_LO:
8498 expecting_tls_get_addr = 1;
8499 found_tls_get_addr_arg = 1;
8500 /* Fall through. */
8501
8502 case R_PPC64_GOT_TLSGD16_HI:
8503 case R_PPC64_GOT_TLSGD16_HA:
8504 if (ok_tprel)
8505 /* GD -> LE */
8506 tls_set = 0;
8507 else
8508 /* GD -> IE */
8509 tls_set = TLS_TLS | TLS_TPRELGD;
8510 tls_clear = TLS_GD;
8511 tls_type = TLS_TLS | TLS_GD;
8512 break;
8513
8514 case R_PPC64_GOT_TPREL16_DS:
8515 case R_PPC64_GOT_TPREL16_LO_DS:
8516 case R_PPC64_GOT_TPREL16_HI:
8517 case R_PPC64_GOT_TPREL16_HA:
8518 if (ok_tprel)
8519 {
8520 /* IE -> LE */
8521 tls_set = 0;
8522 tls_clear = TLS_TPREL;
8523 tls_type = TLS_TLS | TLS_TPREL;
8524 break;
8525 }
8526 continue;
8527
8528 case R_PPC64_TLSGD:
8529 case R_PPC64_TLSLD:
8530 found_tls_get_addr_arg = 1;
8531 /* Fall through. */
8532
8533 case R_PPC64_TLS:
8534 case R_PPC64_TOC16:
8535 case R_PPC64_TOC16_LO:
8536 if (sym_sec == NULL || sym_sec != toc)
8537 continue;
8538
8539 /* Mark this toc entry as referenced by a TLS
8540 code sequence. We can do that now in the
8541 case of R_PPC64_TLS, and after checking for
8542 tls_get_addr for the TOC16 relocs. */
8543 if (toc_ref == NULL)
8544 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8545 if (toc_ref == NULL)
8546 goto err_free_rel;
8547
8548 if (h != NULL)
8549 value = h->root.u.def.value;
8550 else
8551 value = sym->st_value;
8552 value += rel->r_addend;
8553 if (value % 8 != 0)
8554 continue;
8555 BFD_ASSERT (value < toc->size
8556 && toc->output_offset % 8 == 0);
8557 toc_ref_index = (value + toc->output_offset) / 8;
8558 if (r_type == R_PPC64_TLS
8559 || r_type == R_PPC64_TLSGD
8560 || r_type == R_PPC64_TLSLD)
8561 {
8562 toc_ref[toc_ref_index] = 1;
8563 continue;
8564 }
8565
8566 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8567 continue;
8568
8569 tls_set = 0;
8570 tls_clear = 0;
8571 expecting_tls_get_addr = 2;
8572 break;
8573
8574 case R_PPC64_TPREL64:
8575 if (pass == 0
8576 || sec != toc
8577 || toc_ref == NULL
8578 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8579 continue;
8580 if (ok_tprel)
8581 {
8582 /* IE -> LE */
8583 tls_set = TLS_EXPLICIT;
8584 tls_clear = TLS_TPREL;
8585 break;
8586 }
8587 continue;
8588
8589 case R_PPC64_DTPMOD64:
8590 if (pass == 0
8591 || sec != toc
8592 || toc_ref == NULL
8593 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8594 continue;
8595 if (rel + 1 < relend
8596 && (rel[1].r_info
8597 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8598 && rel[1].r_offset == rel->r_offset + 8)
8599 {
8600 if (ok_tprel)
8601 /* GD -> LE */
8602 tls_set = TLS_EXPLICIT | TLS_GD;
8603 else
8604 /* GD -> IE */
8605 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8606 tls_clear = TLS_GD;
8607 }
8608 else
8609 {
8610 if (!is_local)
8611 continue;
8612
8613 /* LD -> LE */
8614 tls_set = TLS_EXPLICIT;
8615 tls_clear = TLS_LD;
8616 }
8617 break;
8618
8619 default:
8620 continue;
8621 }
8622
8623 if (pass == 0)
8624 {
8625 if (!expecting_tls_get_addr
8626 || !sec->has_tls_get_addr_call)
8627 continue;
8628
8629 if (rel + 1 < relend
8630 && branch_reloc_hash_match (ibfd, rel + 1,
8631 htab->tls_get_addr,
8632 htab->tls_get_addr_fd))
8633 {
8634 if (expecting_tls_get_addr == 2)
8635 {
8636 /* Check for toc tls entries. */
8637 unsigned char *toc_tls;
8638 int retval;
8639
8640 retval = get_tls_mask (&toc_tls, NULL, NULL,
8641 &locsyms,
8642 rel, ibfd);
8643 if (retval == 0)
8644 goto err_free_rel;
8645 if (toc_tls != NULL)
8646 {
8647 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8648 found_tls_get_addr_arg = 1;
8649 if (retval > 1)
8650 toc_ref[toc_ref_index] = 1;
8651 }
8652 }
8653 continue;
8654 }
8655
8656 if (expecting_tls_get_addr != 1)
8657 continue;
8658
8659 /* Uh oh, we didn't find the expected call. We
8660 could just mark this symbol to exclude it
8661 from tls optimization but it's safer to skip
8662 the entire optimization. */
8663 /* xgettext:c-format */
8664 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8665 "TLS optimization disabled\n"),
8666 ibfd, sec, rel->r_offset);
8667 ret = TRUE;
8668 goto err_free_rel;
8669 }
8670
8671 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8672 {
8673 struct plt_entry *ent;
8674 for (ent = htab->tls_get_addr->elf.plt.plist;
8675 ent != NULL;
8676 ent = ent->next)
8677 if (ent->addend == 0)
8678 {
8679 if (ent->plt.refcount > 0)
8680 {
8681 ent->plt.refcount -= 1;
8682 expecting_tls_get_addr = 0;
8683 }
8684 break;
8685 }
8686 }
8687
8688 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8689 {
8690 struct plt_entry *ent;
8691 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8692 ent != NULL;
8693 ent = ent->next)
8694 if (ent->addend == 0)
8695 {
8696 if (ent->plt.refcount > 0)
8697 ent->plt.refcount -= 1;
8698 break;
8699 }
8700 }
8701
8702 if (tls_clear == 0)
8703 continue;
8704
8705 if ((tls_set & TLS_EXPLICIT) == 0)
8706 {
8707 struct got_entry *ent;
8708
8709 /* Adjust got entry for this reloc. */
8710 if (h != NULL)
8711 ent = h->got.glist;
8712 else
8713 ent = elf_local_got_ents (ibfd)[r_symndx];
8714
8715 for (; ent != NULL; ent = ent->next)
8716 if (ent->addend == rel->r_addend
8717 && ent->owner == ibfd
8718 && ent->tls_type == tls_type)
8719 break;
8720 if (ent == NULL)
8721 abort ();
8722
8723 if (tls_set == 0)
8724 {
8725 /* We managed to get rid of a got entry. */
8726 if (ent->got.refcount > 0)
8727 ent->got.refcount -= 1;
8728 }
8729 }
8730 else
8731 {
8732 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8733 we'll lose one or two dyn relocs. */
8734 if (!dec_dynrel_count (rel->r_info, sec, info,
8735 NULL, h, sym))
8736 return FALSE;
8737
8738 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8739 {
8740 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8741 NULL, h, sym))
8742 return FALSE;
8743 }
8744 }
8745
8746 *tls_mask |= tls_set;
8747 *tls_mask &= ~tls_clear;
8748 }
8749
8750 if (elf_section_data (sec)->relocs != relstart)
8751 free (relstart);
8752 }
8753
8754 if (locsyms != NULL
8755 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8756 {
8757 if (!info->keep_memory)
8758 free (locsyms);
8759 else
8760 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8761 }
8762 }
8763
8764 if (toc_ref != NULL)
8765 free (toc_ref);
8766 htab->do_tls_opt = 1;
8767 return TRUE;
8768 }
8769
8770 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8771 the values of any global symbols in a toc section that has been
8772 edited. Globals in toc sections should be a rarity, so this function
8773 sets a flag if any are found in toc sections other than the one just
8774 edited, so that further hash table traversals can be avoided. */
8775
8776 struct adjust_toc_info
8777 {
8778 asection *toc;
8779 unsigned long *skip;
8780 bfd_boolean global_toc_syms;
8781 };
8782
8783 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8784
8785 static bfd_boolean
8786 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8787 {
8788 struct ppc_link_hash_entry *eh;
8789 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8790 unsigned long i;
8791
8792 if (h->root.type != bfd_link_hash_defined
8793 && h->root.type != bfd_link_hash_defweak)
8794 return TRUE;
8795
8796 eh = (struct ppc_link_hash_entry *) h;
8797 if (eh->adjust_done)
8798 return TRUE;
8799
8800 if (eh->elf.root.u.def.section == toc_inf->toc)
8801 {
8802 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8803 i = toc_inf->toc->rawsize >> 3;
8804 else
8805 i = eh->elf.root.u.def.value >> 3;
8806
8807 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8808 {
8809 _bfd_error_handler
8810 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8811 do
8812 ++i;
8813 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8814 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8815 }
8816
8817 eh->elf.root.u.def.value -= toc_inf->skip[i];
8818 eh->adjust_done = 1;
8819 }
8820 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8821 toc_inf->global_toc_syms = TRUE;
8822
8823 return TRUE;
8824 }
8825
8826 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8827 on a _LO variety toc/got reloc. */
8828
8829 static bfd_boolean
8830 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8831 {
8832 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
8833 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
8834 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8835 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8836 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8837 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8838 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8839 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8840 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8841 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8842 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8843 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8844 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8845 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8846 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8847 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
8848 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8849 /* Exclude lfqu by testing reloc. If relocs are ever
8850 defined for the reduced D field in psq_lu then those
8851 will need testing too. */
8852 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8853 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
8854 && (insn & 1) == 0)
8855 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
8856 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8857 /* Exclude stfqu. psq_stu as above for psq_lu. */
8858 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8859 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
8860 && (insn & 1) == 0));
8861 }
8862
8863 /* Examine all relocs referencing .toc sections in order to remove
8864 unused .toc entries. */
8865
8866 bfd_boolean
8867 ppc64_elf_edit_toc (struct bfd_link_info *info)
8868 {
8869 bfd *ibfd;
8870 struct adjust_toc_info toc_inf;
8871 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8872
8873 htab->do_toc_opt = 1;
8874 toc_inf.global_toc_syms = TRUE;
8875 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8876 {
8877 asection *toc, *sec;
8878 Elf_Internal_Shdr *symtab_hdr;
8879 Elf_Internal_Sym *local_syms;
8880 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8881 unsigned long *skip, *drop;
8882 unsigned char *used;
8883 unsigned char *keep, last, some_unused;
8884
8885 if (!is_ppc64_elf (ibfd))
8886 continue;
8887
8888 toc = bfd_get_section_by_name (ibfd, ".toc");
8889 if (toc == NULL
8890 || toc->size == 0
8891 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8892 || discarded_section (toc))
8893 continue;
8894
8895 toc_relocs = NULL;
8896 local_syms = NULL;
8897 symtab_hdr = &elf_symtab_hdr (ibfd);
8898
8899 /* Look at sections dropped from the final link. */
8900 skip = NULL;
8901 relstart = NULL;
8902 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8903 {
8904 if (sec->reloc_count == 0
8905 || !discarded_section (sec)
8906 || get_opd_info (sec)
8907 || (sec->flags & SEC_ALLOC) == 0
8908 || (sec->flags & SEC_DEBUGGING) != 0)
8909 continue;
8910
8911 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8912 if (relstart == NULL)
8913 goto error_ret;
8914
8915 /* Run through the relocs to see which toc entries might be
8916 unused. */
8917 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8918 {
8919 enum elf_ppc64_reloc_type r_type;
8920 unsigned long r_symndx;
8921 asection *sym_sec;
8922 struct elf_link_hash_entry *h;
8923 Elf_Internal_Sym *sym;
8924 bfd_vma val;
8925
8926 r_type = ELF64_R_TYPE (rel->r_info);
8927 switch (r_type)
8928 {
8929 default:
8930 continue;
8931
8932 case R_PPC64_TOC16:
8933 case R_PPC64_TOC16_LO:
8934 case R_PPC64_TOC16_HI:
8935 case R_PPC64_TOC16_HA:
8936 case R_PPC64_TOC16_DS:
8937 case R_PPC64_TOC16_LO_DS:
8938 break;
8939 }
8940
8941 r_symndx = ELF64_R_SYM (rel->r_info);
8942 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8943 r_symndx, ibfd))
8944 goto error_ret;
8945
8946 if (sym_sec != toc)
8947 continue;
8948
8949 if (h != NULL)
8950 val = h->root.u.def.value;
8951 else
8952 val = sym->st_value;
8953 val += rel->r_addend;
8954
8955 if (val >= toc->size)
8956 continue;
8957
8958 /* Anything in the toc ought to be aligned to 8 bytes.
8959 If not, don't mark as unused. */
8960 if (val & 7)
8961 continue;
8962
8963 if (skip == NULL)
8964 {
8965 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8966 if (skip == NULL)
8967 goto error_ret;
8968 }
8969
8970 skip[val >> 3] = ref_from_discarded;
8971 }
8972
8973 if (elf_section_data (sec)->relocs != relstart)
8974 free (relstart);
8975 }
8976
8977 /* For largetoc loads of address constants, we can convert
8978 . addis rx,2,addr@got@ha
8979 . ld ry,addr@got@l(rx)
8980 to
8981 . addis rx,2,addr@toc@ha
8982 . addi ry,rx,addr@toc@l
8983 when addr is within 2G of the toc pointer. This then means
8984 that the word storing "addr" in the toc is no longer needed. */
8985
8986 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8987 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8988 && toc->reloc_count != 0)
8989 {
8990 /* Read toc relocs. */
8991 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8992 info->keep_memory);
8993 if (toc_relocs == NULL)
8994 goto error_ret;
8995
8996 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8997 {
8998 enum elf_ppc64_reloc_type r_type;
8999 unsigned long r_symndx;
9000 asection *sym_sec;
9001 struct elf_link_hash_entry *h;
9002 Elf_Internal_Sym *sym;
9003 bfd_vma val, addr;
9004
9005 r_type = ELF64_R_TYPE (rel->r_info);
9006 if (r_type != R_PPC64_ADDR64)
9007 continue;
9008
9009 r_symndx = ELF64_R_SYM (rel->r_info);
9010 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9011 r_symndx, ibfd))
9012 goto error_ret;
9013
9014 if (sym_sec == NULL
9015 || sym_sec->output_section == NULL
9016 || discarded_section (sym_sec))
9017 continue;
9018
9019 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9020 continue;
9021
9022 if (h != NULL)
9023 {
9024 if (h->type == STT_GNU_IFUNC)
9025 continue;
9026 val = h->root.u.def.value;
9027 }
9028 else
9029 {
9030 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9031 continue;
9032 val = sym->st_value;
9033 }
9034 val += rel->r_addend;
9035 val += sym_sec->output_section->vma + sym_sec->output_offset;
9036
9037 /* We don't yet know the exact toc pointer value, but we
9038 know it will be somewhere in the toc section. Don't
9039 optimize if the difference from any possible toc
9040 pointer is outside [ff..f80008000, 7fff7fff]. */
9041 addr = toc->output_section->vma + TOC_BASE_OFF;
9042 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9043 continue;
9044
9045 addr = toc->output_section->vma + toc->output_section->rawsize;
9046 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9047 continue;
9048
9049 if (skip == NULL)
9050 {
9051 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9052 if (skip == NULL)
9053 goto error_ret;
9054 }
9055
9056 skip[rel->r_offset >> 3]
9057 |= can_optimize | ((rel - toc_relocs) << 2);
9058 }
9059 }
9060
9061 if (skip == NULL)
9062 continue;
9063
9064 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9065 if (used == NULL)
9066 {
9067 error_ret:
9068 if (local_syms != NULL
9069 && symtab_hdr->contents != (unsigned char *) local_syms)
9070 free (local_syms);
9071 if (sec != NULL
9072 && relstart != NULL
9073 && elf_section_data (sec)->relocs != relstart)
9074 free (relstart);
9075 if (toc_relocs != NULL
9076 && elf_section_data (toc)->relocs != toc_relocs)
9077 free (toc_relocs);
9078 if (skip != NULL)
9079 free (skip);
9080 return FALSE;
9081 }
9082
9083 /* Now check all kept sections that might reference the toc.
9084 Check the toc itself last. */
9085 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9086 : ibfd->sections);
9087 sec != NULL;
9088 sec = (sec == toc ? NULL
9089 : sec->next == NULL ? toc
9090 : sec->next == toc && toc->next ? toc->next
9091 : sec->next))
9092 {
9093 int repeat;
9094
9095 if (sec->reloc_count == 0
9096 || discarded_section (sec)
9097 || get_opd_info (sec)
9098 || (sec->flags & SEC_ALLOC) == 0
9099 || (sec->flags & SEC_DEBUGGING) != 0)
9100 continue;
9101
9102 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9103 info->keep_memory);
9104 if (relstart == NULL)
9105 {
9106 free (used);
9107 goto error_ret;
9108 }
9109
9110 /* Mark toc entries referenced as used. */
9111 do
9112 {
9113 repeat = 0;
9114 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9115 {
9116 enum elf_ppc64_reloc_type r_type;
9117 unsigned long r_symndx;
9118 asection *sym_sec;
9119 struct elf_link_hash_entry *h;
9120 Elf_Internal_Sym *sym;
9121 bfd_vma val;
9122 enum {no_check, check_lo, check_ha} insn_check;
9123
9124 r_type = ELF64_R_TYPE (rel->r_info);
9125 switch (r_type)
9126 {
9127 default:
9128 insn_check = no_check;
9129 break;
9130
9131 case R_PPC64_GOT_TLSLD16_HA:
9132 case R_PPC64_GOT_TLSGD16_HA:
9133 case R_PPC64_GOT_TPREL16_HA:
9134 case R_PPC64_GOT_DTPREL16_HA:
9135 case R_PPC64_GOT16_HA:
9136 case R_PPC64_TOC16_HA:
9137 insn_check = check_ha;
9138 break;
9139
9140 case R_PPC64_GOT_TLSLD16_LO:
9141 case R_PPC64_GOT_TLSGD16_LO:
9142 case R_PPC64_GOT_TPREL16_LO_DS:
9143 case R_PPC64_GOT_DTPREL16_LO_DS:
9144 case R_PPC64_GOT16_LO:
9145 case R_PPC64_GOT16_LO_DS:
9146 case R_PPC64_TOC16_LO:
9147 case R_PPC64_TOC16_LO_DS:
9148 insn_check = check_lo;
9149 break;
9150 }
9151
9152 if (insn_check != no_check)
9153 {
9154 bfd_vma off = rel->r_offset & ~3;
9155 unsigned char buf[4];
9156 unsigned int insn;
9157
9158 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9159 {
9160 free (used);
9161 goto error_ret;
9162 }
9163 insn = bfd_get_32 (ibfd, buf);
9164 if (insn_check == check_lo
9165 ? !ok_lo_toc_insn (insn, r_type)
9166 : ((insn & ((0x3f << 26) | 0x1f << 16))
9167 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9168 {
9169 char str[12];
9170
9171 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9172 sprintf (str, "%#08x", insn);
9173 info->callbacks->einfo
9174 /* xgettext:c-format */
9175 (_("%H: toc optimization is not supported for"
9176 " %s instruction.\n"),
9177 ibfd, sec, rel->r_offset & ~3, str);
9178 }
9179 }
9180
9181 switch (r_type)
9182 {
9183 case R_PPC64_TOC16:
9184 case R_PPC64_TOC16_LO:
9185 case R_PPC64_TOC16_HI:
9186 case R_PPC64_TOC16_HA:
9187 case R_PPC64_TOC16_DS:
9188 case R_PPC64_TOC16_LO_DS:
9189 /* In case we're taking addresses of toc entries. */
9190 case R_PPC64_ADDR64:
9191 break;
9192
9193 default:
9194 continue;
9195 }
9196
9197 r_symndx = ELF64_R_SYM (rel->r_info);
9198 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9199 r_symndx, ibfd))
9200 {
9201 free (used);
9202 goto error_ret;
9203 }
9204
9205 if (sym_sec != toc)
9206 continue;
9207
9208 if (h != NULL)
9209 val = h->root.u.def.value;
9210 else
9211 val = sym->st_value;
9212 val += rel->r_addend;
9213
9214 if (val >= toc->size)
9215 continue;
9216
9217 if ((skip[val >> 3] & can_optimize) != 0)
9218 {
9219 bfd_vma off;
9220 unsigned char opc;
9221
9222 switch (r_type)
9223 {
9224 case R_PPC64_TOC16_HA:
9225 break;
9226
9227 case R_PPC64_TOC16_LO_DS:
9228 off = rel->r_offset;
9229 off += (bfd_big_endian (ibfd) ? -2 : 3);
9230 if (!bfd_get_section_contents (ibfd, sec, &opc,
9231 off, 1))
9232 {
9233 free (used);
9234 goto error_ret;
9235 }
9236 if ((opc & (0x3f << 2)) == (58u << 2))
9237 break;
9238 /* Fall through. */
9239
9240 default:
9241 /* Wrong sort of reloc, or not a ld. We may
9242 as well clear ref_from_discarded too. */
9243 skip[val >> 3] = 0;
9244 }
9245 }
9246
9247 if (sec != toc)
9248 used[val >> 3] = 1;
9249 /* For the toc section, we only mark as used if this
9250 entry itself isn't unused. */
9251 else if ((used[rel->r_offset >> 3]
9252 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9253 && !used[val >> 3])
9254 {
9255 /* Do all the relocs again, to catch reference
9256 chains. */
9257 repeat = 1;
9258 used[val >> 3] = 1;
9259 }
9260 }
9261 }
9262 while (repeat);
9263
9264 if (elf_section_data (sec)->relocs != relstart)
9265 free (relstart);
9266 }
9267
9268 /* Merge the used and skip arrays. Assume that TOC
9269 doublewords not appearing as either used or unused belong
9270 to an entry more than one doubleword in size. */
9271 for (drop = skip, keep = used, last = 0, some_unused = 0;
9272 drop < skip + (toc->size + 7) / 8;
9273 ++drop, ++keep)
9274 {
9275 if (*keep)
9276 {
9277 *drop &= ~ref_from_discarded;
9278 if ((*drop & can_optimize) != 0)
9279 some_unused = 1;
9280 last = 0;
9281 }
9282 else if ((*drop & ref_from_discarded) != 0)
9283 {
9284 some_unused = 1;
9285 last = ref_from_discarded;
9286 }
9287 else
9288 *drop = last;
9289 }
9290
9291 free (used);
9292
9293 if (some_unused)
9294 {
9295 bfd_byte *contents, *src;
9296 unsigned long off;
9297 Elf_Internal_Sym *sym;
9298 bfd_boolean local_toc_syms = FALSE;
9299
9300 /* Shuffle the toc contents, and at the same time convert the
9301 skip array from booleans into offsets. */
9302 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9303 goto error_ret;
9304
9305 elf_section_data (toc)->this_hdr.contents = contents;
9306
9307 for (src = contents, off = 0, drop = skip;
9308 src < contents + toc->size;
9309 src += 8, ++drop)
9310 {
9311 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9312 off += 8;
9313 else if (off != 0)
9314 {
9315 *drop = off;
9316 memcpy (src - off, src, 8);
9317 }
9318 }
9319 *drop = off;
9320 toc->rawsize = toc->size;
9321 toc->size = src - contents - off;
9322
9323 /* Adjust addends for relocs against the toc section sym,
9324 and optimize any accesses we can. */
9325 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9326 {
9327 if (sec->reloc_count == 0
9328 || discarded_section (sec))
9329 continue;
9330
9331 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9332 info->keep_memory);
9333 if (relstart == NULL)
9334 goto error_ret;
9335
9336 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9337 {
9338 enum elf_ppc64_reloc_type r_type;
9339 unsigned long r_symndx;
9340 asection *sym_sec;
9341 struct elf_link_hash_entry *h;
9342 bfd_vma val;
9343
9344 r_type = ELF64_R_TYPE (rel->r_info);
9345 switch (r_type)
9346 {
9347 default:
9348 continue;
9349
9350 case R_PPC64_TOC16:
9351 case R_PPC64_TOC16_LO:
9352 case R_PPC64_TOC16_HI:
9353 case R_PPC64_TOC16_HA:
9354 case R_PPC64_TOC16_DS:
9355 case R_PPC64_TOC16_LO_DS:
9356 case R_PPC64_ADDR64:
9357 break;
9358 }
9359
9360 r_symndx = ELF64_R_SYM (rel->r_info);
9361 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9362 r_symndx, ibfd))
9363 goto error_ret;
9364
9365 if (sym_sec != toc)
9366 continue;
9367
9368 if (h != NULL)
9369 val = h->root.u.def.value;
9370 else
9371 {
9372 val = sym->st_value;
9373 if (val != 0)
9374 local_toc_syms = TRUE;
9375 }
9376
9377 val += rel->r_addend;
9378
9379 if (val > toc->rawsize)
9380 val = toc->rawsize;
9381 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9382 continue;
9383 else if ((skip[val >> 3] & can_optimize) != 0)
9384 {
9385 Elf_Internal_Rela *tocrel
9386 = toc_relocs + (skip[val >> 3] >> 2);
9387 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9388
9389 switch (r_type)
9390 {
9391 case R_PPC64_TOC16_HA:
9392 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9393 break;
9394
9395 case R_PPC64_TOC16_LO_DS:
9396 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9397 break;
9398
9399 default:
9400 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9401 ppc_howto_init ();
9402 info->callbacks->einfo
9403 /* xgettext:c-format */
9404 (_("%H: %s references "
9405 "optimized away TOC entry\n"),
9406 ibfd, sec, rel->r_offset,
9407 ppc64_elf_howto_table[r_type]->name);
9408 bfd_set_error (bfd_error_bad_value);
9409 goto error_ret;
9410 }
9411 rel->r_addend = tocrel->r_addend;
9412 elf_section_data (sec)->relocs = relstart;
9413 continue;
9414 }
9415
9416 if (h != NULL || sym->st_value != 0)
9417 continue;
9418
9419 rel->r_addend -= skip[val >> 3];
9420 elf_section_data (sec)->relocs = relstart;
9421 }
9422
9423 if (elf_section_data (sec)->relocs != relstart)
9424 free (relstart);
9425 }
9426
9427 /* We shouldn't have local or global symbols defined in the TOC,
9428 but handle them anyway. */
9429 if (local_syms != NULL)
9430 for (sym = local_syms;
9431 sym < local_syms + symtab_hdr->sh_info;
9432 ++sym)
9433 if (sym->st_value != 0
9434 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9435 {
9436 unsigned long i;
9437
9438 if (sym->st_value > toc->rawsize)
9439 i = toc->rawsize >> 3;
9440 else
9441 i = sym->st_value >> 3;
9442
9443 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9444 {
9445 if (local_toc_syms)
9446 _bfd_error_handler
9447 (_("%s defined on removed toc entry"),
9448 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9449 do
9450 ++i;
9451 while ((skip[i] & (ref_from_discarded | can_optimize)));
9452 sym->st_value = (bfd_vma) i << 3;
9453 }
9454
9455 sym->st_value -= skip[i];
9456 symtab_hdr->contents = (unsigned char *) local_syms;
9457 }
9458
9459 /* Adjust any global syms defined in this toc input section. */
9460 if (toc_inf.global_toc_syms)
9461 {
9462 toc_inf.toc = toc;
9463 toc_inf.skip = skip;
9464 toc_inf.global_toc_syms = FALSE;
9465 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9466 &toc_inf);
9467 }
9468
9469 if (toc->reloc_count != 0)
9470 {
9471 Elf_Internal_Shdr *rel_hdr;
9472 Elf_Internal_Rela *wrel;
9473 bfd_size_type sz;
9474
9475 /* Remove unused toc relocs, and adjust those we keep. */
9476 if (toc_relocs == NULL)
9477 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9478 info->keep_memory);
9479 if (toc_relocs == NULL)
9480 goto error_ret;
9481
9482 wrel = toc_relocs;
9483 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9484 if ((skip[rel->r_offset >> 3]
9485 & (ref_from_discarded | can_optimize)) == 0)
9486 {
9487 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9488 wrel->r_info = rel->r_info;
9489 wrel->r_addend = rel->r_addend;
9490 ++wrel;
9491 }
9492 else if (!dec_dynrel_count (rel->r_info, toc, info,
9493 &local_syms, NULL, NULL))
9494 goto error_ret;
9495
9496 elf_section_data (toc)->relocs = toc_relocs;
9497 toc->reloc_count = wrel - toc_relocs;
9498 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9499 sz = rel_hdr->sh_entsize;
9500 rel_hdr->sh_size = toc->reloc_count * sz;
9501 }
9502 }
9503 else if (toc_relocs != NULL
9504 && elf_section_data (toc)->relocs != toc_relocs)
9505 free (toc_relocs);
9506
9507 if (local_syms != NULL
9508 && symtab_hdr->contents != (unsigned char *) local_syms)
9509 {
9510 if (!info->keep_memory)
9511 free (local_syms);
9512 else
9513 symtab_hdr->contents = (unsigned char *) local_syms;
9514 }
9515 free (skip);
9516 }
9517
9518 return TRUE;
9519 }
9520
9521 /* Return true iff input section I references the TOC using
9522 instructions limited to +/-32k offsets. */
9523
9524 bfd_boolean
9525 ppc64_elf_has_small_toc_reloc (asection *i)
9526 {
9527 return (is_ppc64_elf (i->owner)
9528 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9529 }
9530
9531 /* Allocate space for one GOT entry. */
9532
9533 static void
9534 allocate_got (struct elf_link_hash_entry *h,
9535 struct bfd_link_info *info,
9536 struct got_entry *gent)
9537 {
9538 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9539 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9540 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9541 ? 16 : 8);
9542 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9543 ? 2 : 1) * sizeof (Elf64_External_Rela);
9544 asection *got = ppc64_elf_tdata (gent->owner)->got;
9545
9546 gent->got.offset = got->size;
9547 got->size += entsize;
9548
9549 if (h->type == STT_GNU_IFUNC)
9550 {
9551 htab->elf.irelplt->size += rentsize;
9552 htab->got_reli_size += rentsize;
9553 }
9554 else if (((bfd_link_pic (info)
9555 && !((gent->tls_type & TLS_TPREL) != 0
9556 && bfd_link_executable (info)
9557 && SYMBOL_REFERENCES_LOCAL (info, h)))
9558 || (htab->elf.dynamic_sections_created
9559 && h->dynindx != -1
9560 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9561 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9562 {
9563 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9564 relgot->size += rentsize;
9565 }
9566 }
9567
9568 /* This function merges got entries in the same toc group. */
9569
9570 static void
9571 merge_got_entries (struct got_entry **pent)
9572 {
9573 struct got_entry *ent, *ent2;
9574
9575 for (ent = *pent; ent != NULL; ent = ent->next)
9576 if (!ent->is_indirect)
9577 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9578 if (!ent2->is_indirect
9579 && ent2->addend == ent->addend
9580 && ent2->tls_type == ent->tls_type
9581 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9582 {
9583 ent2->is_indirect = TRUE;
9584 ent2->got.ent = ent;
9585 }
9586 }
9587
9588 /* If H is undefined, make it dynamic if that makes sense. */
9589
9590 static bfd_boolean
9591 ensure_undef_dynamic (struct bfd_link_info *info,
9592 struct elf_link_hash_entry *h)
9593 {
9594 struct elf_link_hash_table *htab = elf_hash_table (info);
9595
9596 if (htab->dynamic_sections_created
9597 && ((info->dynamic_undefined_weak != 0
9598 && h->root.type == bfd_link_hash_undefweak)
9599 || h->root.type == bfd_link_hash_undefined)
9600 && h->dynindx == -1
9601 && !h->forced_local
9602 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9603 return bfd_elf_link_record_dynamic_symbol (info, h);
9604 return TRUE;
9605 }
9606
9607 /* Allocate space in .plt, .got and associated reloc sections for
9608 dynamic relocs. */
9609
9610 static bfd_boolean
9611 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9612 {
9613 struct bfd_link_info *info;
9614 struct ppc_link_hash_table *htab;
9615 asection *s;
9616 struct ppc_link_hash_entry *eh;
9617 struct got_entry **pgent, *gent;
9618
9619 if (h->root.type == bfd_link_hash_indirect)
9620 return TRUE;
9621
9622 info = (struct bfd_link_info *) inf;
9623 htab = ppc_hash_table (info);
9624 if (htab == NULL)
9625 return FALSE;
9626
9627 eh = (struct ppc_link_hash_entry *) h;
9628 /* Run through the TLS GD got entries first if we're changing them
9629 to TPREL. */
9630 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9631 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9632 if (gent->got.refcount > 0
9633 && (gent->tls_type & TLS_GD) != 0)
9634 {
9635 /* This was a GD entry that has been converted to TPREL. If
9636 there happens to be a TPREL entry we can use that one. */
9637 struct got_entry *ent;
9638 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9639 if (ent->got.refcount > 0
9640 && (ent->tls_type & TLS_TPREL) != 0
9641 && ent->addend == gent->addend
9642 && ent->owner == gent->owner)
9643 {
9644 gent->got.refcount = 0;
9645 break;
9646 }
9647
9648 /* If not, then we'll be using our own TPREL entry. */
9649 if (gent->got.refcount != 0)
9650 gent->tls_type = TLS_TLS | TLS_TPREL;
9651 }
9652
9653 /* Remove any list entry that won't generate a word in the GOT before
9654 we call merge_got_entries. Otherwise we risk merging to empty
9655 entries. */
9656 pgent = &h->got.glist;
9657 while ((gent = *pgent) != NULL)
9658 if (gent->got.refcount > 0)
9659 {
9660 if ((gent->tls_type & TLS_LD) != 0
9661 && !h->def_dynamic)
9662 {
9663 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9664 *pgent = gent->next;
9665 }
9666 else
9667 pgent = &gent->next;
9668 }
9669 else
9670 *pgent = gent->next;
9671
9672 if (!htab->do_multi_toc)
9673 merge_got_entries (&h->got.glist);
9674
9675 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9676 if (!gent->is_indirect)
9677 {
9678 /* Make sure this symbol is output as a dynamic symbol. */
9679 if (!ensure_undef_dynamic (info, h))
9680 return FALSE;
9681
9682 if (!is_ppc64_elf (gent->owner))
9683 abort ();
9684
9685 allocate_got (h, info, gent);
9686 }
9687
9688 /* If no dynamic sections we can't have dynamic relocs, except for
9689 IFUNCs which are handled even in static executables. */
9690 if (!htab->elf.dynamic_sections_created
9691 && h->type != STT_GNU_IFUNC)
9692 eh->dyn_relocs = NULL;
9693
9694 /* Discard relocs on undefined symbols that must be local. */
9695 else if (h->root.type == bfd_link_hash_undefined
9696 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9697 eh->dyn_relocs = NULL;
9698
9699 /* Also discard relocs on undefined weak syms with non-default
9700 visibility, or when dynamic_undefined_weak says so. */
9701 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9702 eh->dyn_relocs = NULL;
9703
9704 if (eh->dyn_relocs != NULL)
9705 {
9706 struct elf_dyn_relocs *p, **pp;
9707
9708 /* In the shared -Bsymbolic case, discard space allocated for
9709 dynamic pc-relative relocs against symbols which turn out to
9710 be defined in regular objects. For the normal shared case,
9711 discard space for relocs that have become local due to symbol
9712 visibility changes. */
9713
9714 if (bfd_link_pic (info))
9715 {
9716 /* Relocs that use pc_count are those that appear on a call
9717 insn, or certain REL relocs (see must_be_dyn_reloc) that
9718 can be generated via assembly. We want calls to
9719 protected symbols to resolve directly to the function
9720 rather than going via the plt. If people want function
9721 pointer comparisons to work as expected then they should
9722 avoid writing weird assembly. */
9723 if (SYMBOL_CALLS_LOCAL (info, h))
9724 {
9725 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9726 {
9727 p->count -= p->pc_count;
9728 p->pc_count = 0;
9729 if (p->count == 0)
9730 *pp = p->next;
9731 else
9732 pp = &p->next;
9733 }
9734 }
9735
9736 if (eh->dyn_relocs != NULL)
9737 {
9738 /* Make sure this symbol is output as a dynamic symbol. */
9739 if (!ensure_undef_dynamic (info, h))
9740 return FALSE;
9741 }
9742 }
9743 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
9744 {
9745 /* For the non-pic case, discard space for relocs against
9746 symbols which turn out to need copy relocs or are not
9747 dynamic. */
9748 if (h->dynamic_adjusted
9749 && !h->def_regular
9750 && !ELF_COMMON_DEF_P (h))
9751 {
9752 /* Make sure this symbol is output as a dynamic symbol. */
9753 if (!ensure_undef_dynamic (info, h))
9754 return FALSE;
9755
9756 if (h->dynindx == -1)
9757 eh->dyn_relocs = NULL;
9758 }
9759 else
9760 eh->dyn_relocs = NULL;
9761 }
9762
9763 /* Finally, allocate space. */
9764 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9765 {
9766 asection *sreloc = elf_section_data (p->sec)->sreloc;
9767 if (eh->elf.type == STT_GNU_IFUNC)
9768 sreloc = htab->elf.irelplt;
9769 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9770 }
9771 }
9772
9773 if ((htab->elf.dynamic_sections_created
9774 && h->dynindx != -1)
9775 || h->type == STT_GNU_IFUNC)
9776 {
9777 struct plt_entry *pent;
9778 bfd_boolean doneone = FALSE;
9779 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9780 if (pent->plt.refcount > 0)
9781 {
9782 if (!htab->elf.dynamic_sections_created
9783 || h->dynindx == -1)
9784 {
9785 s = htab->elf.iplt;
9786 pent->plt.offset = s->size;
9787 s->size += PLT_ENTRY_SIZE (htab);
9788 s = htab->elf.irelplt;
9789 }
9790 else
9791 {
9792 /* If this is the first .plt entry, make room for the special
9793 first entry. */
9794 s = htab->elf.splt;
9795 if (s->size == 0)
9796 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9797
9798 pent->plt.offset = s->size;
9799
9800 /* Make room for this entry. */
9801 s->size += PLT_ENTRY_SIZE (htab);
9802
9803 /* Make room for the .glink code. */
9804 s = htab->glink;
9805 if (s->size == 0)
9806 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9807 if (htab->opd_abi)
9808 {
9809 /* We need bigger stubs past index 32767. */
9810 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9811 s->size += 4;
9812 s->size += 2*4;
9813 }
9814 else
9815 s->size += 4;
9816
9817 /* We also need to make an entry in the .rela.plt section. */
9818 s = htab->elf.srelplt;
9819 }
9820 s->size += sizeof (Elf64_External_Rela);
9821 doneone = TRUE;
9822 }
9823 else
9824 pent->plt.offset = (bfd_vma) -1;
9825 if (!doneone)
9826 {
9827 h->plt.plist = NULL;
9828 h->needs_plt = 0;
9829 }
9830 }
9831 else
9832 {
9833 h->plt.plist = NULL;
9834 h->needs_plt = 0;
9835 }
9836
9837 return TRUE;
9838 }
9839
9840 #define PPC_LO(v) ((v) & 0xffff)
9841 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9842 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9843
9844 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9845 to set up space for global entry stubs. These are put in glink,
9846 after the branch table. */
9847
9848 static bfd_boolean
9849 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9850 {
9851 struct bfd_link_info *info;
9852 struct ppc_link_hash_table *htab;
9853 struct plt_entry *pent;
9854 asection *s, *plt;
9855
9856 if (h->root.type == bfd_link_hash_indirect)
9857 return TRUE;
9858
9859 if (!h->pointer_equality_needed)
9860 return TRUE;
9861
9862 if (h->def_regular)
9863 return TRUE;
9864
9865 info = inf;
9866 htab = ppc_hash_table (info);
9867 if (htab == NULL)
9868 return FALSE;
9869
9870 s = htab->global_entry;
9871 plt = htab->elf.splt;
9872 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9873 if (pent->plt.offset != (bfd_vma) -1
9874 && pent->addend == 0)
9875 {
9876 /* For ELFv2, if this symbol is not defined in a regular file
9877 and we are not generating a shared library or pie, then we
9878 need to define the symbol in the executable on a call stub.
9879 This is to avoid text relocations. */
9880 bfd_vma off, stub_align, stub_off, stub_size;
9881 unsigned int align_power;
9882
9883 stub_size = 16;
9884 stub_off = s->size;
9885 if (htab->params->plt_stub_align >= 0)
9886 align_power = htab->params->plt_stub_align;
9887 else
9888 align_power = -htab->params->plt_stub_align;
9889 /* Setting section alignment is delayed until we know it is
9890 non-empty. Otherwise the .text output section will be
9891 aligned at least to plt_stub_align even when no global
9892 entry stubs are needed. */
9893 if (s->alignment_power < align_power)
9894 s->alignment_power = align_power;
9895 stub_align = (bfd_vma) 1 << align_power;
9896 if (htab->params->plt_stub_align >= 0
9897 || ((((stub_off + stub_size - 1) & -stub_align)
9898 - (stub_off & -stub_align))
9899 > ((stub_size - 1) & -stub_align)))
9900 stub_off = (stub_off + stub_align - 1) & -stub_align;
9901 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9902 off -= stub_off + s->output_offset + s->output_section->vma;
9903 /* Note that for --plt-stub-align negative we have a possible
9904 dependency between stub offset and size. Break that
9905 dependency by assuming the max stub size when calculating
9906 the stub offset. */
9907 if (PPC_HA (off) == 0)
9908 stub_size -= 4;
9909 h->root.type = bfd_link_hash_defined;
9910 h->root.u.def.section = s;
9911 h->root.u.def.value = stub_off;
9912 s->size = stub_off + stub_size;
9913 break;
9914 }
9915 return TRUE;
9916 }
9917
9918 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
9919 read-only sections. */
9920
9921 static bfd_boolean
9922 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
9923 {
9924 asection *sec;
9925
9926 if (h->root.type == bfd_link_hash_indirect)
9927 return TRUE;
9928
9929 sec = readonly_dynrelocs (h);
9930 if (sec != NULL)
9931 {
9932 struct bfd_link_info *info = (struct bfd_link_info *) inf;
9933
9934 info->flags |= DF_TEXTREL;
9935 info->callbacks->minfo
9936 (_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
9937 sec->owner, h->root.root.string, sec);
9938
9939 /* Not an error, just cut short the traversal. */
9940 return FALSE;
9941 }
9942 return TRUE;
9943 }
9944
9945 /* Set the sizes of the dynamic sections. */
9946
9947 static bfd_boolean
9948 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9949 struct bfd_link_info *info)
9950 {
9951 struct ppc_link_hash_table *htab;
9952 bfd *dynobj;
9953 asection *s;
9954 bfd_boolean relocs;
9955 bfd *ibfd;
9956 struct got_entry *first_tlsld;
9957
9958 htab = ppc_hash_table (info);
9959 if (htab == NULL)
9960 return FALSE;
9961
9962 dynobj = htab->elf.dynobj;
9963 if (dynobj == NULL)
9964 abort ();
9965
9966 if (htab->elf.dynamic_sections_created)
9967 {
9968 /* Set the contents of the .interp section to the interpreter. */
9969 if (bfd_link_executable (info) && !info->nointerp)
9970 {
9971 s = bfd_get_linker_section (dynobj, ".interp");
9972 if (s == NULL)
9973 abort ();
9974 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9975 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9976 }
9977 }
9978
9979 /* Set up .got offsets for local syms, and space for local dynamic
9980 relocs. */
9981 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9982 {
9983 struct got_entry **lgot_ents;
9984 struct got_entry **end_lgot_ents;
9985 struct plt_entry **local_plt;
9986 struct plt_entry **end_local_plt;
9987 unsigned char *lgot_masks;
9988 bfd_size_type locsymcount;
9989 Elf_Internal_Shdr *symtab_hdr;
9990
9991 if (!is_ppc64_elf (ibfd))
9992 continue;
9993
9994 for (s = ibfd->sections; s != NULL; s = s->next)
9995 {
9996 struct ppc_dyn_relocs *p;
9997
9998 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9999 {
10000 if (!bfd_is_abs_section (p->sec)
10001 && bfd_is_abs_section (p->sec->output_section))
10002 {
10003 /* Input section has been discarded, either because
10004 it is a copy of a linkonce section or due to
10005 linker script /DISCARD/, so we'll be discarding
10006 the relocs too. */
10007 }
10008 else if (p->count != 0)
10009 {
10010 asection *srel = elf_section_data (p->sec)->sreloc;
10011 if (p->ifunc)
10012 srel = htab->elf.irelplt;
10013 srel->size += p->count * sizeof (Elf64_External_Rela);
10014 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10015 info->flags |= DF_TEXTREL;
10016 }
10017 }
10018 }
10019
10020 lgot_ents = elf_local_got_ents (ibfd);
10021 if (!lgot_ents)
10022 continue;
10023
10024 symtab_hdr = &elf_symtab_hdr (ibfd);
10025 locsymcount = symtab_hdr->sh_info;
10026 end_lgot_ents = lgot_ents + locsymcount;
10027 local_plt = (struct plt_entry **) end_lgot_ents;
10028 end_local_plt = local_plt + locsymcount;
10029 lgot_masks = (unsigned char *) end_local_plt;
10030 s = ppc64_elf_tdata (ibfd)->got;
10031 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10032 {
10033 struct got_entry **pent, *ent;
10034
10035 pent = lgot_ents;
10036 while ((ent = *pent) != NULL)
10037 if (ent->got.refcount > 0)
10038 {
10039 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10040 {
10041 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10042 *pent = ent->next;
10043 }
10044 else
10045 {
10046 unsigned int ent_size = 8;
10047 unsigned int rel_size = sizeof (Elf64_External_Rela);
10048
10049 ent->got.offset = s->size;
10050 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10051 {
10052 ent_size *= 2;
10053 rel_size *= 2;
10054 }
10055 s->size += ent_size;
10056 if ((*lgot_masks & PLT_IFUNC) != 0)
10057 {
10058 htab->elf.irelplt->size += rel_size;
10059 htab->got_reli_size += rel_size;
10060 }
10061 else if (bfd_link_pic (info)
10062 && !((ent->tls_type & TLS_TPREL) != 0
10063 && bfd_link_executable (info)))
10064 {
10065 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10066 srel->size += rel_size;
10067 }
10068 pent = &ent->next;
10069 }
10070 }
10071 else
10072 *pent = ent->next;
10073 }
10074
10075 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
10076 for (; local_plt < end_local_plt; ++local_plt)
10077 {
10078 struct plt_entry *ent;
10079
10080 for (ent = *local_plt; ent != NULL; ent = ent->next)
10081 if (ent->plt.refcount > 0)
10082 {
10083 s = htab->elf.iplt;
10084 ent->plt.offset = s->size;
10085 s->size += PLT_ENTRY_SIZE (htab);
10086
10087 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10088 }
10089 else
10090 ent->plt.offset = (bfd_vma) -1;
10091 }
10092 }
10093
10094 /* Allocate global sym .plt and .got entries, and space for global
10095 sym dynamic relocs. */
10096 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10097
10098 if (!htab->opd_abi && !bfd_link_pic (info))
10099 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10100
10101 first_tlsld = NULL;
10102 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10103 {
10104 struct got_entry *ent;
10105
10106 if (!is_ppc64_elf (ibfd))
10107 continue;
10108
10109 ent = ppc64_tlsld_got (ibfd);
10110 if (ent->got.refcount > 0)
10111 {
10112 if (!htab->do_multi_toc && first_tlsld != NULL)
10113 {
10114 ent->is_indirect = TRUE;
10115 ent->got.ent = first_tlsld;
10116 }
10117 else
10118 {
10119 if (first_tlsld == NULL)
10120 first_tlsld = ent;
10121 s = ppc64_elf_tdata (ibfd)->got;
10122 ent->got.offset = s->size;
10123 ent->owner = ibfd;
10124 s->size += 16;
10125 if (bfd_link_pic (info))
10126 {
10127 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10128 srel->size += sizeof (Elf64_External_Rela);
10129 }
10130 }
10131 }
10132 else
10133 ent->got.offset = (bfd_vma) -1;
10134 }
10135
10136 /* We now have determined the sizes of the various dynamic sections.
10137 Allocate memory for them. */
10138 relocs = FALSE;
10139 for (s = dynobj->sections; s != NULL; s = s->next)
10140 {
10141 if ((s->flags & SEC_LINKER_CREATED) == 0)
10142 continue;
10143
10144 if (s == htab->brlt || s == htab->relbrlt)
10145 /* These haven't been allocated yet; don't strip. */
10146 continue;
10147 else if (s == htab->elf.sgot
10148 || s == htab->elf.splt
10149 || s == htab->elf.iplt
10150 || s == htab->glink
10151 || s == htab->global_entry
10152 || s == htab->elf.sdynbss
10153 || s == htab->elf.sdynrelro)
10154 {
10155 /* Strip this section if we don't need it; see the
10156 comment below. */
10157 }
10158 else if (s == htab->glink_eh_frame)
10159 {
10160 if (!bfd_is_abs_section (s->output_section))
10161 /* Not sized yet. */
10162 continue;
10163 }
10164 else if (CONST_STRNEQ (s->name, ".rela"))
10165 {
10166 if (s->size != 0)
10167 {
10168 if (s != htab->elf.srelplt)
10169 relocs = TRUE;
10170
10171 /* We use the reloc_count field as a counter if we need
10172 to copy relocs into the output file. */
10173 s->reloc_count = 0;
10174 }
10175 }
10176 else
10177 {
10178 /* It's not one of our sections, so don't allocate space. */
10179 continue;
10180 }
10181
10182 if (s->size == 0)
10183 {
10184 /* If we don't need this section, strip it from the
10185 output file. This is mostly to handle .rela.bss and
10186 .rela.plt. We must create both sections in
10187 create_dynamic_sections, because they must be created
10188 before the linker maps input sections to output
10189 sections. The linker does that before
10190 adjust_dynamic_symbol is called, and it is that
10191 function which decides whether anything needs to go
10192 into these sections. */
10193 s->flags |= SEC_EXCLUDE;
10194 continue;
10195 }
10196
10197 if (bfd_is_abs_section (s->output_section))
10198 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10199 s->name);
10200
10201 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10202 continue;
10203
10204 /* Allocate memory for the section contents. We use bfd_zalloc
10205 here in case unused entries are not reclaimed before the
10206 section's contents are written out. This should not happen,
10207 but this way if it does we get a R_PPC64_NONE reloc in .rela
10208 sections instead of garbage.
10209 We also rely on the section contents being zero when writing
10210 the GOT and .dynrelro. */
10211 s->contents = bfd_zalloc (dynobj, s->size);
10212 if (s->contents == NULL)
10213 return FALSE;
10214 }
10215
10216 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10217 {
10218 if (!is_ppc64_elf (ibfd))
10219 continue;
10220
10221 s = ppc64_elf_tdata (ibfd)->got;
10222 if (s != NULL && s != htab->elf.sgot)
10223 {
10224 if (s->size == 0)
10225 s->flags |= SEC_EXCLUDE;
10226 else
10227 {
10228 s->contents = bfd_zalloc (ibfd, s->size);
10229 if (s->contents == NULL)
10230 return FALSE;
10231 }
10232 }
10233 s = ppc64_elf_tdata (ibfd)->relgot;
10234 if (s != NULL)
10235 {
10236 if (s->size == 0)
10237 s->flags |= SEC_EXCLUDE;
10238 else
10239 {
10240 s->contents = bfd_zalloc (ibfd, s->size);
10241 if (s->contents == NULL)
10242 return FALSE;
10243 relocs = TRUE;
10244 s->reloc_count = 0;
10245 }
10246 }
10247 }
10248
10249 if (htab->elf.dynamic_sections_created)
10250 {
10251 bfd_boolean tls_opt;
10252
10253 /* Add some entries to the .dynamic section. We fill in the
10254 values later, in ppc64_elf_finish_dynamic_sections, but we
10255 must add the entries now so that we get the correct size for
10256 the .dynamic section. The DT_DEBUG entry is filled in by the
10257 dynamic linker and used by the debugger. */
10258 #define add_dynamic_entry(TAG, VAL) \
10259 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10260
10261 if (bfd_link_executable (info))
10262 {
10263 if (!add_dynamic_entry (DT_DEBUG, 0))
10264 return FALSE;
10265 }
10266
10267 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10268 {
10269 if (!add_dynamic_entry (DT_PLTGOT, 0)
10270 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10271 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10272 || !add_dynamic_entry (DT_JMPREL, 0)
10273 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10274 return FALSE;
10275 }
10276
10277 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10278 {
10279 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10280 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10281 return FALSE;
10282 }
10283
10284 tls_opt = (htab->params->tls_get_addr_opt
10285 && htab->tls_get_addr_fd != NULL
10286 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10287 if (tls_opt || !htab->opd_abi)
10288 {
10289 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10290 return FALSE;
10291 }
10292
10293 if (relocs)
10294 {
10295 if (!add_dynamic_entry (DT_RELA, 0)
10296 || !add_dynamic_entry (DT_RELASZ, 0)
10297 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10298 return FALSE;
10299
10300 /* If any dynamic relocs apply to a read-only section,
10301 then we need a DT_TEXTREL entry. */
10302 if ((info->flags & DF_TEXTREL) == 0)
10303 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10304
10305 if ((info->flags & DF_TEXTREL) != 0)
10306 {
10307 if (!add_dynamic_entry (DT_TEXTREL, 0))
10308 return FALSE;
10309 }
10310 }
10311 }
10312 #undef add_dynamic_entry
10313
10314 return TRUE;
10315 }
10316
10317 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10318
10319 static bfd_boolean
10320 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10321 {
10322 if (h->plt.plist != NULL
10323 && !h->def_regular
10324 && !h->pointer_equality_needed)
10325 return FALSE;
10326
10327 return _bfd_elf_hash_symbol (h);
10328 }
10329
10330 /* Determine the type of stub needed, if any, for a call. */
10331
10332 static inline enum ppc_stub_type
10333 ppc_type_of_stub (asection *input_sec,
10334 const Elf_Internal_Rela *rel,
10335 struct ppc_link_hash_entry **hash,
10336 struct plt_entry **plt_ent,
10337 bfd_vma destination,
10338 unsigned long local_off)
10339 {
10340 struct ppc_link_hash_entry *h = *hash;
10341 bfd_vma location;
10342 bfd_vma branch_offset;
10343 bfd_vma max_branch_offset;
10344 enum elf_ppc64_reloc_type r_type;
10345
10346 if (h != NULL)
10347 {
10348 struct plt_entry *ent;
10349 struct ppc_link_hash_entry *fdh = h;
10350 if (h->oh != NULL
10351 && h->oh->is_func_descriptor)
10352 {
10353 fdh = ppc_follow_link (h->oh);
10354 *hash = fdh;
10355 }
10356
10357 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10358 if (ent->addend == rel->r_addend
10359 && ent->plt.offset != (bfd_vma) -1)
10360 {
10361 *plt_ent = ent;
10362 return ppc_stub_plt_call;
10363 }
10364
10365 /* Here, we know we don't have a plt entry. If we don't have a
10366 either a defined function descriptor or a defined entry symbol
10367 in a regular object file, then it is pointless trying to make
10368 any other type of stub. */
10369 if (!is_static_defined (&fdh->elf)
10370 && !is_static_defined (&h->elf))
10371 return ppc_stub_none;
10372 }
10373 else if (elf_local_got_ents (input_sec->owner) != NULL)
10374 {
10375 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10376 struct plt_entry **local_plt = (struct plt_entry **)
10377 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10378 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10379
10380 if (local_plt[r_symndx] != NULL)
10381 {
10382 struct plt_entry *ent;
10383
10384 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10385 if (ent->addend == rel->r_addend
10386 && ent->plt.offset != (bfd_vma) -1)
10387 {
10388 *plt_ent = ent;
10389 return ppc_stub_plt_call;
10390 }
10391 }
10392 }
10393
10394 /* Determine where the call point is. */
10395 location = (input_sec->output_offset
10396 + input_sec->output_section->vma
10397 + rel->r_offset);
10398
10399 branch_offset = destination - location;
10400 r_type = ELF64_R_TYPE (rel->r_info);
10401
10402 /* Determine if a long branch stub is needed. */
10403 max_branch_offset = 1 << 25;
10404 if (r_type != R_PPC64_REL24)
10405 max_branch_offset = 1 << 15;
10406
10407 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10408 /* We need a stub. Figure out whether a long_branch or plt_branch
10409 is needed later. */
10410 return ppc_stub_long_branch;
10411
10412 return ppc_stub_none;
10413 }
10414
10415 /* With power7 weakly ordered memory model, it is possible for ld.so
10416 to update a plt entry in one thread and have another thread see a
10417 stale zero toc entry. To avoid this we need some sort of acquire
10418 barrier in the call stub. One solution is to make the load of the
10419 toc word seem to appear to depend on the load of the function entry
10420 word. Another solution is to test for r2 being zero, and branch to
10421 the appropriate glink entry if so.
10422
10423 . fake dep barrier compare
10424 . ld 12,xxx(2) ld 12,xxx(2)
10425 . mtctr 12 mtctr 12
10426 . xor 11,12,12 ld 2,xxx+8(2)
10427 . add 2,2,11 cmpldi 2,0
10428 . ld 2,xxx+8(2) bnectr+
10429 . bctr b <glink_entry>
10430
10431 The solution involving the compare turns out to be faster, so
10432 that's what we use unless the branch won't reach. */
10433
10434 #define ALWAYS_USE_FAKE_DEP 0
10435 #define ALWAYS_EMIT_R2SAVE 0
10436
10437 static inline unsigned int
10438 plt_stub_size (struct ppc_link_hash_table *htab,
10439 struct ppc_stub_hash_entry *stub_entry,
10440 bfd_vma off)
10441 {
10442 unsigned size = 12;
10443
10444 if (ALWAYS_EMIT_R2SAVE
10445 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10446 size += 4;
10447 if (PPC_HA (off) != 0)
10448 size += 4;
10449 if (htab->opd_abi)
10450 {
10451 size += 4;
10452 if (htab->params->plt_static_chain)
10453 size += 4;
10454 if (htab->params->plt_thread_safe
10455 && htab->elf.dynamic_sections_created
10456 && stub_entry->h != NULL
10457 && stub_entry->h->elf.dynindx != -1)
10458 size += 8;
10459 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
10460 size += 4;
10461 }
10462 if (stub_entry->h != NULL
10463 && (stub_entry->h == htab->tls_get_addr_fd
10464 || stub_entry->h == htab->tls_get_addr)
10465 && htab->params->tls_get_addr_opt)
10466 {
10467 size += 7 * 4;
10468 if (ALWAYS_EMIT_R2SAVE
10469 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10470 size += 6 * 4;
10471 }
10472 return size;
10473 }
10474
10475 /* Depending on the sign of plt_stub_align:
10476 If positive, return the padding to align to a 2**plt_stub_align
10477 boundary.
10478 If negative, if this stub would cross fewer 2**plt_stub_align
10479 boundaries if we align, then return the padding needed to do so. */
10480
10481 static inline unsigned int
10482 plt_stub_pad (struct ppc_link_hash_table *htab,
10483 struct ppc_stub_hash_entry *stub_entry,
10484 bfd_vma plt_off)
10485 {
10486 int stub_align;
10487 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
10488 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10489
10490 if (htab->params->plt_stub_align >= 0)
10491 {
10492 stub_align = 1 << htab->params->plt_stub_align;
10493 if ((stub_off & (stub_align - 1)) != 0)
10494 return stub_align - (stub_off & (stub_align - 1));
10495 return 0;
10496 }
10497
10498 stub_align = 1 << -htab->params->plt_stub_align;
10499 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10500 > ((stub_size - 1) & -stub_align))
10501 return stub_align - (stub_off & (stub_align - 1));
10502 return 0;
10503 }
10504
10505 /* Build a .plt call stub. */
10506
10507 static inline bfd_byte *
10508 build_plt_stub (struct ppc_link_hash_table *htab,
10509 struct ppc_stub_hash_entry *stub_entry,
10510 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10511 {
10512 bfd *obfd = htab->params->stub_bfd;
10513 bfd_boolean plt_load_toc = htab->opd_abi;
10514 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
10515 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
10516 && htab->elf.dynamic_sections_created
10517 && stub_entry->h != NULL
10518 && stub_entry->h->elf.dynindx != -1);
10519 bfd_boolean use_fake_dep = plt_thread_safe;
10520 bfd_vma cmp_branch_off = 0;
10521
10522 if (!ALWAYS_USE_FAKE_DEP
10523 && plt_load_toc
10524 && plt_thread_safe
10525 && !((stub_entry->h == htab->tls_get_addr_fd
10526 || stub_entry->h == htab->tls_get_addr)
10527 && htab->params->tls_get_addr_opt))
10528 {
10529 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
10530 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
10531 / PLT_ENTRY_SIZE (htab));
10532 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
10533 bfd_vma to, from;
10534
10535 if (pltindex > 32768)
10536 glinkoff += (pltindex - 32768) * 4;
10537 to = (glinkoff
10538 + htab->glink->output_offset
10539 + htab->glink->output_section->vma);
10540 from = (p - stub_entry->group->stub_sec->contents
10541 + 4 * (ALWAYS_EMIT_R2SAVE
10542 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10543 + 4 * (PPC_HA (offset) != 0)
10544 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10545 != PPC_HA (offset))
10546 + 4 * (plt_static_chain != 0)
10547 + 20
10548 + stub_entry->group->stub_sec->output_offset
10549 + stub_entry->group->stub_sec->output_section->vma);
10550 cmp_branch_off = to - from;
10551 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10552 }
10553
10554 if (PPC_HA (offset) != 0)
10555 {
10556 if (r != NULL)
10557 {
10558 if (ALWAYS_EMIT_R2SAVE
10559 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10560 r[0].r_offset += 4;
10561 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10562 r[1].r_offset = r[0].r_offset + 4;
10563 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10564 r[1].r_addend = r[0].r_addend;
10565 if (plt_load_toc)
10566 {
10567 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10568 {
10569 r[2].r_offset = r[1].r_offset + 4;
10570 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10571 r[2].r_addend = r[0].r_addend;
10572 }
10573 else
10574 {
10575 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10576 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10577 r[2].r_addend = r[0].r_addend + 8;
10578 if (plt_static_chain)
10579 {
10580 r[3].r_offset = r[2].r_offset + 4;
10581 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10582 r[3].r_addend = r[0].r_addend + 16;
10583 }
10584 }
10585 }
10586 }
10587 if (ALWAYS_EMIT_R2SAVE
10588 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10589 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10590 if (plt_load_toc)
10591 {
10592 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10593 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10594 }
10595 else
10596 {
10597 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
10598 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
10599 }
10600 if (plt_load_toc
10601 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10602 {
10603 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10604 offset = 0;
10605 }
10606 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10607 if (plt_load_toc)
10608 {
10609 if (use_fake_dep)
10610 {
10611 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10612 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10613 }
10614 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10615 if (plt_static_chain)
10616 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10617 }
10618 }
10619 else
10620 {
10621 if (r != NULL)
10622 {
10623 if (ALWAYS_EMIT_R2SAVE
10624 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10625 r[0].r_offset += 4;
10626 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10627 if (plt_load_toc)
10628 {
10629 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10630 {
10631 r[1].r_offset = r[0].r_offset + 4;
10632 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10633 r[1].r_addend = r[0].r_addend;
10634 }
10635 else
10636 {
10637 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10638 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10639 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10640 if (plt_static_chain)
10641 {
10642 r[2].r_offset = r[1].r_offset + 4;
10643 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10644 r[2].r_addend = r[0].r_addend + 8;
10645 }
10646 }
10647 }
10648 }
10649 if (ALWAYS_EMIT_R2SAVE
10650 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10651 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
10652 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10653 if (plt_load_toc
10654 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10655 {
10656 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10657 offset = 0;
10658 }
10659 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10660 if (plt_load_toc)
10661 {
10662 if (use_fake_dep)
10663 {
10664 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10665 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10666 }
10667 if (plt_static_chain)
10668 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10669 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10670 }
10671 }
10672 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
10673 {
10674 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10675 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10676 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10677 }
10678 else
10679 bfd_put_32 (obfd, BCTR, p), p += 4;
10680 return p;
10681 }
10682
10683 /* Build a special .plt call stub for __tls_get_addr. */
10684
10685 #define LD_R11_0R3 0xe9630000
10686 #define LD_R12_0R3 0xe9830000
10687 #define MR_R0_R3 0x7c601b78
10688 #define CMPDI_R11_0 0x2c2b0000
10689 #define ADD_R3_R12_R13 0x7c6c6a14
10690 #define BEQLR 0x4d820020
10691 #define MR_R3_R0 0x7c030378
10692 #define STD_R11_0R1 0xf9610000
10693 #define BCTRL 0x4e800421
10694 #define LD_R11_0R1 0xe9610000
10695 #define MTLR_R11 0x7d6803a6
10696
10697 static inline bfd_byte *
10698 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10699 struct ppc_stub_hash_entry *stub_entry,
10700 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10701 {
10702 bfd *obfd = htab->params->stub_bfd;
10703
10704 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10705 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10706 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10707 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10708 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10709 bfd_put_32 (obfd, BEQLR, p), p += 4;
10710 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10711 if (r != NULL)
10712 r[0].r_offset += 7 * 4;
10713 if (!ALWAYS_EMIT_R2SAVE
10714 && stub_entry->stub_type != ppc_stub_plt_call_r2save)
10715 return build_plt_stub (htab, stub_entry, p, offset, r);
10716
10717 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10718 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10719
10720 if (r != NULL)
10721 r[0].r_offset += 2 * 4;
10722 p = build_plt_stub (htab, stub_entry, p, offset, r);
10723 bfd_put_32 (obfd, BCTRL, p - 4);
10724
10725 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
10726 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
10727 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10728 bfd_put_32 (obfd, BLR, p), p += 4;
10729
10730 return p;
10731 }
10732
10733 static Elf_Internal_Rela *
10734 get_relocs (asection *sec, int count)
10735 {
10736 Elf_Internal_Rela *relocs;
10737 struct bfd_elf_section_data *elfsec_data;
10738
10739 elfsec_data = elf_section_data (sec);
10740 relocs = elfsec_data->relocs;
10741 if (relocs == NULL)
10742 {
10743 bfd_size_type relsize;
10744 relsize = sec->reloc_count * sizeof (*relocs);
10745 relocs = bfd_alloc (sec->owner, relsize);
10746 if (relocs == NULL)
10747 return NULL;
10748 elfsec_data->relocs = relocs;
10749 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10750 sizeof (Elf_Internal_Shdr));
10751 if (elfsec_data->rela.hdr == NULL)
10752 return NULL;
10753 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10754 * sizeof (Elf64_External_Rela));
10755 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10756 sec->reloc_count = 0;
10757 }
10758 relocs += sec->reloc_count;
10759 sec->reloc_count += count;
10760 return relocs;
10761 }
10762
10763 static bfd_vma
10764 get_r2off (struct bfd_link_info *info,
10765 struct ppc_stub_hash_entry *stub_entry)
10766 {
10767 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10768 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
10769
10770 if (r2off == 0)
10771 {
10772 /* Support linking -R objects. Get the toc pointer from the
10773 opd entry. */
10774 char buf[8];
10775 if (!htab->opd_abi)
10776 return r2off;
10777 asection *opd = stub_entry->h->elf.root.u.def.section;
10778 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10779
10780 if (strcmp (opd->name, ".opd") != 0
10781 || opd->reloc_count != 0)
10782 {
10783 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10784 stub_entry->h->elf.root.root.string);
10785 bfd_set_error (bfd_error_bad_value);
10786 return (bfd_vma) -1;
10787 }
10788 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10789 return (bfd_vma) -1;
10790 r2off = bfd_get_64 (opd->owner, buf);
10791 r2off -= elf_gp (info->output_bfd);
10792 }
10793 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
10794 return r2off;
10795 }
10796
10797 static bfd_boolean
10798 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10799 {
10800 struct ppc_stub_hash_entry *stub_entry;
10801 struct ppc_branch_hash_entry *br_entry;
10802 struct bfd_link_info *info;
10803 struct ppc_link_hash_table *htab;
10804 bfd_byte *loc;
10805 bfd_byte *p;
10806 bfd_vma dest, off;
10807 Elf_Internal_Rela *r;
10808 asection *plt;
10809
10810 /* Massage our args to the form they really have. */
10811 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10812 info = in_arg;
10813
10814 htab = ppc_hash_table (info);
10815 if (htab == NULL)
10816 return FALSE;
10817
10818 /* Make a note of the offset within the stubs for this entry. */
10819 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
10820 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
10821
10822 htab->stub_count[stub_entry->stub_type - 1] += 1;
10823 switch (stub_entry->stub_type)
10824 {
10825 case ppc_stub_long_branch:
10826 case ppc_stub_long_branch_r2off:
10827 /* Branches are relative. This is where we are going to. */
10828 dest = (stub_entry->target_value
10829 + stub_entry->target_section->output_offset
10830 + stub_entry->target_section->output_section->vma);
10831 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10832 off = dest;
10833
10834 /* And this is where we are coming from. */
10835 off -= (stub_entry->stub_offset
10836 + stub_entry->group->stub_sec->output_offset
10837 + stub_entry->group->stub_sec->output_section->vma);
10838
10839 p = loc;
10840 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10841 {
10842 bfd_vma r2off = get_r2off (info, stub_entry);
10843
10844 if (r2off == (bfd_vma) -1)
10845 {
10846 htab->stub_error = TRUE;
10847 return FALSE;
10848 }
10849 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
10850 p += 4;
10851 if (PPC_HA (r2off) != 0)
10852 {
10853 bfd_put_32 (htab->params->stub_bfd,
10854 ADDIS_R2_R2 | PPC_HA (r2off), p);
10855 p += 4;
10856 }
10857 if (PPC_LO (r2off) != 0)
10858 {
10859 bfd_put_32 (htab->params->stub_bfd,
10860 ADDI_R2_R2 | PPC_LO (r2off), p);
10861 p += 4;
10862 }
10863 off -= p - loc;
10864 }
10865 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
10866 p += 4;
10867
10868 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10869 {
10870 info->callbacks->einfo
10871 (_("%P: long branch stub `%s' offset overflow\n"),
10872 stub_entry->root.string);
10873 htab->stub_error = TRUE;
10874 return FALSE;
10875 }
10876
10877 if (info->emitrelocations)
10878 {
10879 r = get_relocs (stub_entry->group->stub_sec, 1);
10880 if (r == NULL)
10881 return FALSE;
10882 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
10883 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10884 r->r_addend = dest;
10885 if (stub_entry->h != NULL)
10886 {
10887 struct elf_link_hash_entry **hashes;
10888 unsigned long symndx;
10889 struct ppc_link_hash_entry *h;
10890
10891 hashes = elf_sym_hashes (htab->params->stub_bfd);
10892 if (hashes == NULL)
10893 {
10894 bfd_size_type hsize;
10895
10896 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10897 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
10898 if (hashes == NULL)
10899 return FALSE;
10900 elf_sym_hashes (htab->params->stub_bfd) = hashes;
10901 htab->stub_globals = 1;
10902 }
10903 symndx = htab->stub_globals++;
10904 h = stub_entry->h;
10905 hashes[symndx] = &h->elf;
10906 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10907 if (h->oh != NULL && h->oh->is_func)
10908 h = ppc_follow_link (h->oh);
10909 if (h->elf.root.u.def.section != stub_entry->target_section)
10910 /* H is an opd symbol. The addend must be zero. */
10911 r->r_addend = 0;
10912 else
10913 {
10914 off = (h->elf.root.u.def.value
10915 + h->elf.root.u.def.section->output_offset
10916 + h->elf.root.u.def.section->output_section->vma);
10917 r->r_addend -= off;
10918 }
10919 }
10920 }
10921 break;
10922
10923 case ppc_stub_plt_branch:
10924 case ppc_stub_plt_branch_r2off:
10925 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10926 stub_entry->root.string + 9,
10927 FALSE, FALSE);
10928 if (br_entry == NULL)
10929 {
10930 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10931 stub_entry->root.string);
10932 htab->stub_error = TRUE;
10933 return FALSE;
10934 }
10935
10936 dest = (stub_entry->target_value
10937 + stub_entry->target_section->output_offset
10938 + stub_entry->target_section->output_section->vma);
10939 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10940 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10941
10942 bfd_put_64 (htab->brlt->owner, dest,
10943 htab->brlt->contents + br_entry->offset);
10944
10945 if (br_entry->iter == htab->stub_iteration)
10946 {
10947 br_entry->iter = 0;
10948
10949 if (htab->relbrlt != NULL)
10950 {
10951 /* Create a reloc for the branch lookup table entry. */
10952 Elf_Internal_Rela rela;
10953 bfd_byte *rl;
10954
10955 rela.r_offset = (br_entry->offset
10956 + htab->brlt->output_offset
10957 + htab->brlt->output_section->vma);
10958 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10959 rela.r_addend = dest;
10960
10961 rl = htab->relbrlt->contents;
10962 rl += (htab->relbrlt->reloc_count++
10963 * sizeof (Elf64_External_Rela));
10964 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10965 }
10966 else if (info->emitrelocations)
10967 {
10968 r = get_relocs (htab->brlt, 1);
10969 if (r == NULL)
10970 return FALSE;
10971 /* brlt, being SEC_LINKER_CREATED does not go through the
10972 normal reloc processing. Symbols and offsets are not
10973 translated from input file to output file form, so
10974 set up the offset per the output file. */
10975 r->r_offset = (br_entry->offset
10976 + htab->brlt->output_offset
10977 + htab->brlt->output_section->vma);
10978 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10979 r->r_addend = dest;
10980 }
10981 }
10982
10983 dest = (br_entry->offset
10984 + htab->brlt->output_offset
10985 + htab->brlt->output_section->vma);
10986
10987 off = (dest
10988 - elf_gp (info->output_bfd)
10989 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
10990
10991 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10992 {
10993 info->callbacks->einfo
10994 (_("%P: linkage table error against `%T'\n"),
10995 stub_entry->root.string);
10996 bfd_set_error (bfd_error_bad_value);
10997 htab->stub_error = TRUE;
10998 return FALSE;
10999 }
11000
11001 if (info->emitrelocations)
11002 {
11003 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11004 if (r == NULL)
11005 return FALSE;
11006 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11007 if (bfd_big_endian (info->output_bfd))
11008 r[0].r_offset += 2;
11009 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11010 r[0].r_offset += 4;
11011 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11012 r[0].r_addend = dest;
11013 if (PPC_HA (off) != 0)
11014 {
11015 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11016 r[1].r_offset = r[0].r_offset + 4;
11017 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11018 r[1].r_addend = r[0].r_addend;
11019 }
11020 }
11021
11022 p = loc;
11023 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11024 {
11025 if (PPC_HA (off) != 0)
11026 {
11027 bfd_put_32 (htab->params->stub_bfd,
11028 ADDIS_R12_R2 | PPC_HA (off), p);
11029 p += 4;
11030 bfd_put_32 (htab->params->stub_bfd,
11031 LD_R12_0R12 | PPC_LO (off), p);
11032 }
11033 else
11034 bfd_put_32 (htab->params->stub_bfd,
11035 LD_R12_0R2 | PPC_LO (off), p);
11036 }
11037 else
11038 {
11039 bfd_vma r2off = get_r2off (info, stub_entry);
11040
11041 if (r2off == (bfd_vma) -1)
11042 {
11043 htab->stub_error = TRUE;
11044 return FALSE;
11045 }
11046
11047 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11048 p += 4;
11049 if (PPC_HA (off) != 0)
11050 {
11051 bfd_put_32 (htab->params->stub_bfd,
11052 ADDIS_R12_R2 | PPC_HA (off), p);
11053 p += 4;
11054 bfd_put_32 (htab->params->stub_bfd,
11055 LD_R12_0R12 | PPC_LO (off), p);
11056 }
11057 else
11058 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11059
11060 if (PPC_HA (r2off) != 0)
11061 {
11062 p += 4;
11063 bfd_put_32 (htab->params->stub_bfd,
11064 ADDIS_R2_R2 | PPC_HA (r2off), p);
11065 }
11066 if (PPC_LO (r2off) != 0)
11067 {
11068 p += 4;
11069 bfd_put_32 (htab->params->stub_bfd,
11070 ADDI_R2_R2 | PPC_LO (r2off), p);
11071 }
11072 }
11073 p += 4;
11074 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11075 p += 4;
11076 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11077 p += 4;
11078 break;
11079
11080 case ppc_stub_plt_call:
11081 case ppc_stub_plt_call_r2save:
11082 if (stub_entry->h != NULL
11083 && stub_entry->h->is_func_descriptor
11084 && stub_entry->h->oh != NULL)
11085 {
11086 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11087
11088 /* If the old-ABI "dot-symbol" is undefined make it weak so
11089 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11090 if (fh->elf.root.type == bfd_link_hash_undefined
11091 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11092 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11093 fh->elf.root.type = bfd_link_hash_undefweak;
11094 }
11095
11096 /* Now build the stub. */
11097 dest = stub_entry->plt_ent->plt.offset & ~1;
11098 if (dest >= (bfd_vma) -2)
11099 abort ();
11100
11101 plt = htab->elf.splt;
11102 if (!htab->elf.dynamic_sections_created
11103 || stub_entry->h == NULL
11104 || stub_entry->h->elf.dynindx == -1)
11105 plt = htab->elf.iplt;
11106
11107 dest += plt->output_offset + plt->output_section->vma;
11108
11109 if (stub_entry->h == NULL
11110 && (stub_entry->plt_ent->plt.offset & 1) == 0)
11111 {
11112 Elf_Internal_Rela rela;
11113 bfd_byte *rl;
11114
11115 rela.r_offset = dest;
11116 if (htab->opd_abi)
11117 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
11118 else
11119 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
11120 rela.r_addend = (stub_entry->target_value
11121 + stub_entry->target_section->output_offset
11122 + stub_entry->target_section->output_section->vma);
11123
11124 rl = (htab->elf.irelplt->contents
11125 + (htab->elf.irelplt->reloc_count++
11126 * sizeof (Elf64_External_Rela)));
11127 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
11128 stub_entry->plt_ent->plt.offset |= 1;
11129 htab->local_ifunc_resolver = 1;
11130 }
11131
11132 off = (dest
11133 - elf_gp (info->output_bfd)
11134 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11135
11136 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11137 {
11138 info->callbacks->einfo
11139 /* xgettext:c-format */
11140 (_("%P: linkage table error against `%T'\n"),
11141 stub_entry->h != NULL
11142 ? stub_entry->h->elf.root.root.string
11143 : "<local sym>");
11144 bfd_set_error (bfd_error_bad_value);
11145 htab->stub_error = TRUE;
11146 return FALSE;
11147 }
11148
11149 if (htab->params->plt_stub_align != 0)
11150 {
11151 unsigned pad = plt_stub_pad (htab, stub_entry, off);
11152
11153 stub_entry->group->stub_sec->size += pad;
11154 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11155 loc += pad;
11156 }
11157
11158 r = NULL;
11159 if (info->emitrelocations)
11160 {
11161 r = get_relocs (stub_entry->group->stub_sec,
11162 ((PPC_HA (off) != 0)
11163 + (htab->opd_abi
11164 ? 2 + (htab->params->plt_static_chain
11165 && PPC_HA (off + 16) == PPC_HA (off))
11166 : 1)));
11167 if (r == NULL)
11168 return FALSE;
11169 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11170 if (bfd_big_endian (info->output_bfd))
11171 r[0].r_offset += 2;
11172 r[0].r_addend = dest;
11173 }
11174 if (stub_entry->h != NULL
11175 && (stub_entry->h == htab->tls_get_addr_fd
11176 || stub_entry->h == htab->tls_get_addr)
11177 && htab->params->tls_get_addr_opt)
11178 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11179 else
11180 p = build_plt_stub (htab, stub_entry, loc, off, r);
11181 break;
11182
11183 case ppc_stub_save_res:
11184 return TRUE;
11185
11186 default:
11187 BFD_FAIL ();
11188 return FALSE;
11189 }
11190
11191 stub_entry->group->stub_sec->size += p - loc;
11192
11193 if (htab->params->emit_stub_syms)
11194 {
11195 struct elf_link_hash_entry *h;
11196 size_t len1, len2;
11197 char *name;
11198 const char *const stub_str[] = { "long_branch",
11199 "long_branch_r2off",
11200 "plt_branch",
11201 "plt_branch_r2off",
11202 "plt_call",
11203 "plt_call" };
11204
11205 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11206 len2 = strlen (stub_entry->root.string);
11207 name = bfd_malloc (len1 + len2 + 2);
11208 if (name == NULL)
11209 return FALSE;
11210 memcpy (name, stub_entry->root.string, 9);
11211 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11212 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11213 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11214 if (h == NULL)
11215 return FALSE;
11216 if (h->root.type == bfd_link_hash_new)
11217 {
11218 h->root.type = bfd_link_hash_defined;
11219 h->root.u.def.section = stub_entry->group->stub_sec;
11220 h->root.u.def.value = stub_entry->stub_offset;
11221 h->ref_regular = 1;
11222 h->def_regular = 1;
11223 h->ref_regular_nonweak = 1;
11224 h->forced_local = 1;
11225 h->non_elf = 0;
11226 h->root.linker_def = 1;
11227 }
11228 }
11229
11230 return TRUE;
11231 }
11232
11233 /* As above, but don't actually build the stub. Just bump offset so
11234 we know stub section sizes, and select plt_branch stubs where
11235 long_branch stubs won't do. */
11236
11237 static bfd_boolean
11238 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11239 {
11240 struct ppc_stub_hash_entry *stub_entry;
11241 struct bfd_link_info *info;
11242 struct ppc_link_hash_table *htab;
11243 bfd_vma off;
11244 int size;
11245
11246 /* Massage our args to the form they really have. */
11247 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11248 info = in_arg;
11249
11250 htab = ppc_hash_table (info);
11251 if (htab == NULL)
11252 return FALSE;
11253
11254 if (stub_entry->h != NULL
11255 && stub_entry->h->save_res
11256 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11257 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11258 {
11259 /* Don't make stubs to out-of-line register save/restore
11260 functions. Instead, emit copies of the functions. */
11261 stub_entry->group->needs_save_res = 1;
11262 stub_entry->stub_type = ppc_stub_save_res;
11263 return TRUE;
11264 }
11265
11266 if (stub_entry->stub_type == ppc_stub_plt_call
11267 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11268 {
11269 asection *plt;
11270 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11271 if (off >= (bfd_vma) -2)
11272 abort ();
11273 plt = htab->elf.splt;
11274 if (!htab->elf.dynamic_sections_created
11275 || stub_entry->h == NULL
11276 || stub_entry->h->elf.dynindx == -1)
11277 plt = htab->elf.iplt;
11278 off += (plt->output_offset
11279 + plt->output_section->vma
11280 - elf_gp (info->output_bfd)
11281 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11282
11283 size = plt_stub_size (htab, stub_entry, off);
11284 if (stub_entry->h != NULL
11285 && (stub_entry->h == htab->tls_get_addr_fd
11286 || stub_entry->h == htab->tls_get_addr)
11287 && htab->params->tls_get_addr_opt
11288 && (ALWAYS_EMIT_R2SAVE
11289 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
11290 stub_entry->group->tls_get_addr_opt_bctrl
11291 = stub_entry->group->stub_sec->size + size - 5 * 4;
11292
11293 if (htab->params->plt_stub_align)
11294 size += plt_stub_pad (htab, stub_entry, off);
11295 if (info->emitrelocations)
11296 {
11297 stub_entry->group->stub_sec->reloc_count
11298 += ((PPC_HA (off) != 0)
11299 + (htab->opd_abi
11300 ? 2 + (htab->params->plt_static_chain
11301 && PPC_HA (off + 16) == PPC_HA (off))
11302 : 1));
11303 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11304 }
11305 }
11306 else
11307 {
11308 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11309 variants. */
11310 bfd_vma r2off = 0;
11311 bfd_vma local_off = 0;
11312
11313 off = (stub_entry->target_value
11314 + stub_entry->target_section->output_offset
11315 + stub_entry->target_section->output_section->vma);
11316 off -= (stub_entry->group->stub_sec->size
11317 + stub_entry->group->stub_sec->output_offset
11318 + stub_entry->group->stub_sec->output_section->vma);
11319
11320 /* Reset the stub type from the plt variant in case we now
11321 can reach with a shorter stub. */
11322 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11323 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11324
11325 size = 4;
11326 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11327 {
11328 r2off = get_r2off (info, stub_entry);
11329 if (r2off == (bfd_vma) -1)
11330 {
11331 htab->stub_error = TRUE;
11332 return FALSE;
11333 }
11334 size = 8;
11335 if (PPC_HA (r2off) != 0)
11336 size += 4;
11337 if (PPC_LO (r2off) != 0)
11338 size += 4;
11339 off -= size - 4;
11340 }
11341
11342 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11343
11344 /* If the branch offset if too big, use a ppc_stub_plt_branch.
11345 Do the same for -R objects without function descriptors. */
11346 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off
11347 || (stub_entry->stub_type == ppc_stub_long_branch_r2off
11348 && r2off == 0
11349 && htab->sec_info[stub_entry->target_section->id].toc_off == 0))
11350 {
11351 struct ppc_branch_hash_entry *br_entry;
11352
11353 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11354 stub_entry->root.string + 9,
11355 TRUE, FALSE);
11356 if (br_entry == NULL)
11357 {
11358 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
11359 stub_entry->root.string);
11360 htab->stub_error = TRUE;
11361 return FALSE;
11362 }
11363
11364 if (br_entry->iter != htab->stub_iteration)
11365 {
11366 br_entry->iter = htab->stub_iteration;
11367 br_entry->offset = htab->brlt->size;
11368 htab->brlt->size += 8;
11369
11370 if (htab->relbrlt != NULL)
11371 htab->relbrlt->size += sizeof (Elf64_External_Rela);
11372 else if (info->emitrelocations)
11373 {
11374 htab->brlt->reloc_count += 1;
11375 htab->brlt->flags |= SEC_RELOC;
11376 }
11377 }
11378
11379 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
11380 off = (br_entry->offset
11381 + htab->brlt->output_offset
11382 + htab->brlt->output_section->vma
11383 - elf_gp (info->output_bfd)
11384 - htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11385
11386 if (info->emitrelocations)
11387 {
11388 stub_entry->group->stub_sec->reloc_count
11389 += 1 + (PPC_HA (off) != 0);
11390 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11391 }
11392
11393 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11394 {
11395 size = 12;
11396 if (PPC_HA (off) != 0)
11397 size = 16;
11398 }
11399 else
11400 {
11401 size = 16;
11402 if (PPC_HA (off) != 0)
11403 size += 4;
11404
11405 if (PPC_HA (r2off) != 0)
11406 size += 4;
11407 if (PPC_LO (r2off) != 0)
11408 size += 4;
11409 }
11410 }
11411 else if (info->emitrelocations)
11412 {
11413 stub_entry->group->stub_sec->reloc_count += 1;
11414 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11415 }
11416 }
11417
11418 stub_entry->group->stub_sec->size += size;
11419 return TRUE;
11420 }
11421
11422 /* Set up various things so that we can make a list of input sections
11423 for each output section included in the link. Returns -1 on error,
11424 0 when no stubs will be needed, and 1 on success. */
11425
11426 int
11427 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
11428 {
11429 unsigned int id;
11430 bfd_size_type amt;
11431 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11432
11433 if (htab == NULL)
11434 return -1;
11435
11436 htab->sec_info_arr_size = bfd_get_next_section_id ();
11437 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
11438 htab->sec_info = bfd_zmalloc (amt);
11439 if (htab->sec_info == NULL)
11440 return -1;
11441
11442 /* Set toc_off for com, und, abs and ind sections. */
11443 for (id = 0; id < 3; id++)
11444 htab->sec_info[id].toc_off = TOC_BASE_OFF;
11445
11446 return 1;
11447 }
11448
11449 /* Set up for first pass at multitoc partitioning. */
11450
11451 void
11452 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
11453 {
11454 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11455
11456 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
11457 htab->toc_bfd = NULL;
11458 htab->toc_first_sec = NULL;
11459 }
11460
11461 /* The linker repeatedly calls this function for each TOC input section
11462 and linker generated GOT section. Group input bfds such that the toc
11463 within a group is less than 64k in size. */
11464
11465 bfd_boolean
11466 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
11467 {
11468 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11469 bfd_vma addr, off, limit;
11470
11471 if (htab == NULL)
11472 return FALSE;
11473
11474 if (!htab->second_toc_pass)
11475 {
11476 /* Keep track of the first .toc or .got section for this input bfd. */
11477 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
11478
11479 if (new_bfd)
11480 {
11481 htab->toc_bfd = isec->owner;
11482 htab->toc_first_sec = isec;
11483 }
11484
11485 addr = isec->output_offset + isec->output_section->vma;
11486 off = addr - htab->toc_curr;
11487 limit = 0x80008000;
11488 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
11489 limit = 0x10000;
11490 if (off + isec->size > limit)
11491 {
11492 addr = (htab->toc_first_sec->output_offset
11493 + htab->toc_first_sec->output_section->vma);
11494 htab->toc_curr = addr;
11495 htab->toc_curr &= -TOC_BASE_ALIGN;
11496 }
11497
11498 /* toc_curr is the base address of this toc group. Set elf_gp
11499 for the input section to be the offset relative to the
11500 output toc base plus 0x8000. Making the input elf_gp an
11501 offset allows us to move the toc as a whole without
11502 recalculating input elf_gp. */
11503 off = htab->toc_curr - elf_gp (info->output_bfd);
11504 off += TOC_BASE_OFF;
11505
11506 /* Die if someone uses a linker script that doesn't keep input
11507 file .toc and .got together. */
11508 if (new_bfd
11509 && elf_gp (isec->owner) != 0
11510 && elf_gp (isec->owner) != off)
11511 return FALSE;
11512
11513 elf_gp (isec->owner) = off;
11514 return TRUE;
11515 }
11516
11517 /* During the second pass toc_first_sec points to the start of
11518 a toc group, and toc_curr is used to track the old elf_gp.
11519 We use toc_bfd to ensure we only look at each bfd once. */
11520 if (htab->toc_bfd == isec->owner)
11521 return TRUE;
11522 htab->toc_bfd = isec->owner;
11523
11524 if (htab->toc_first_sec == NULL
11525 || htab->toc_curr != elf_gp (isec->owner))
11526 {
11527 htab->toc_curr = elf_gp (isec->owner);
11528 htab->toc_first_sec = isec;
11529 }
11530 addr = (htab->toc_first_sec->output_offset
11531 + htab->toc_first_sec->output_section->vma);
11532 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
11533 elf_gp (isec->owner) = off;
11534
11535 return TRUE;
11536 }
11537
11538 /* Called via elf_link_hash_traverse to merge GOT entries for global
11539 symbol H. */
11540
11541 static bfd_boolean
11542 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11543 {
11544 if (h->root.type == bfd_link_hash_indirect)
11545 return TRUE;
11546
11547 merge_got_entries (&h->got.glist);
11548
11549 return TRUE;
11550 }
11551
11552 /* Called via elf_link_hash_traverse to allocate GOT entries for global
11553 symbol H. */
11554
11555 static bfd_boolean
11556 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11557 {
11558 struct got_entry *gent;
11559
11560 if (h->root.type == bfd_link_hash_indirect)
11561 return TRUE;
11562
11563 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11564 if (!gent->is_indirect)
11565 allocate_got (h, (struct bfd_link_info *) inf, gent);
11566 return TRUE;
11567 }
11568
11569 /* Called on the first multitoc pass after the last call to
11570 ppc64_elf_next_toc_section. This function removes duplicate GOT
11571 entries. */
11572
11573 bfd_boolean
11574 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11575 {
11576 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11577 struct bfd *ibfd, *ibfd2;
11578 bfd_boolean done_something;
11579
11580 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11581
11582 if (!htab->do_multi_toc)
11583 return FALSE;
11584
11585 /* Merge global sym got entries within a toc group. */
11586 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11587
11588 /* And tlsld_got. */
11589 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11590 {
11591 struct got_entry *ent, *ent2;
11592
11593 if (!is_ppc64_elf (ibfd))
11594 continue;
11595
11596 ent = ppc64_tlsld_got (ibfd);
11597 if (!ent->is_indirect
11598 && ent->got.offset != (bfd_vma) -1)
11599 {
11600 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
11601 {
11602 if (!is_ppc64_elf (ibfd2))
11603 continue;
11604
11605 ent2 = ppc64_tlsld_got (ibfd2);
11606 if (!ent2->is_indirect
11607 && ent2->got.offset != (bfd_vma) -1
11608 && elf_gp (ibfd2) == elf_gp (ibfd))
11609 {
11610 ent2->is_indirect = TRUE;
11611 ent2->got.ent = ent;
11612 }
11613 }
11614 }
11615 }
11616
11617 /* Zap sizes of got sections. */
11618 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
11619 htab->elf.irelplt->size -= htab->got_reli_size;
11620 htab->got_reli_size = 0;
11621
11622 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11623 {
11624 asection *got, *relgot;
11625
11626 if (!is_ppc64_elf (ibfd))
11627 continue;
11628
11629 got = ppc64_elf_tdata (ibfd)->got;
11630 if (got != NULL)
11631 {
11632 got->rawsize = got->size;
11633 got->size = 0;
11634 relgot = ppc64_elf_tdata (ibfd)->relgot;
11635 relgot->rawsize = relgot->size;
11636 relgot->size = 0;
11637 }
11638 }
11639
11640 /* Now reallocate the got, local syms first. We don't need to
11641 allocate section contents again since we never increase size. */
11642 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11643 {
11644 struct got_entry **lgot_ents;
11645 struct got_entry **end_lgot_ents;
11646 struct plt_entry **local_plt;
11647 struct plt_entry **end_local_plt;
11648 unsigned char *lgot_masks;
11649 bfd_size_type locsymcount;
11650 Elf_Internal_Shdr *symtab_hdr;
11651 asection *s;
11652
11653 if (!is_ppc64_elf (ibfd))
11654 continue;
11655
11656 lgot_ents = elf_local_got_ents (ibfd);
11657 if (!lgot_ents)
11658 continue;
11659
11660 symtab_hdr = &elf_symtab_hdr (ibfd);
11661 locsymcount = symtab_hdr->sh_info;
11662 end_lgot_ents = lgot_ents + locsymcount;
11663 local_plt = (struct plt_entry **) end_lgot_ents;
11664 end_local_plt = local_plt + locsymcount;
11665 lgot_masks = (unsigned char *) end_local_plt;
11666 s = ppc64_elf_tdata (ibfd)->got;
11667 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11668 {
11669 struct got_entry *ent;
11670
11671 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11672 {
11673 unsigned int ent_size = 8;
11674 unsigned int rel_size = sizeof (Elf64_External_Rela);
11675
11676 ent->got.offset = s->size;
11677 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11678 {
11679 ent_size *= 2;
11680 rel_size *= 2;
11681 }
11682 s->size += ent_size;
11683 if ((*lgot_masks & PLT_IFUNC) != 0)
11684 {
11685 htab->elf.irelplt->size += rel_size;
11686 htab->got_reli_size += rel_size;
11687 }
11688 else if (bfd_link_pic (info))
11689 {
11690 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11691 srel->size += rel_size;
11692 }
11693 }
11694 }
11695 }
11696
11697 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11698
11699 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11700 {
11701 struct got_entry *ent;
11702
11703 if (!is_ppc64_elf (ibfd))
11704 continue;
11705
11706 ent = ppc64_tlsld_got (ibfd);
11707 if (!ent->is_indirect
11708 && ent->got.offset != (bfd_vma) -1)
11709 {
11710 asection *s = ppc64_elf_tdata (ibfd)->got;
11711 ent->got.offset = s->size;
11712 s->size += 16;
11713 if (bfd_link_pic (info))
11714 {
11715 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11716 srel->size += sizeof (Elf64_External_Rela);
11717 }
11718 }
11719 }
11720
11721 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
11722 if (!done_something)
11723 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
11724 {
11725 asection *got;
11726
11727 if (!is_ppc64_elf (ibfd))
11728 continue;
11729
11730 got = ppc64_elf_tdata (ibfd)->got;
11731 if (got != NULL)
11732 {
11733 done_something = got->rawsize != got->size;
11734 if (done_something)
11735 break;
11736 }
11737 }
11738
11739 if (done_something)
11740 (*htab->params->layout_sections_again) ();
11741
11742 /* Set up for second pass over toc sections to recalculate elf_gp
11743 on input sections. */
11744 htab->toc_bfd = NULL;
11745 htab->toc_first_sec = NULL;
11746 htab->second_toc_pass = TRUE;
11747 return done_something;
11748 }
11749
11750 /* Called after second pass of multitoc partitioning. */
11751
11752 void
11753 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11754 {
11755 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11756
11757 /* After the second pass, toc_curr tracks the TOC offset used
11758 for code sections below in ppc64_elf_next_input_section. */
11759 htab->toc_curr = TOC_BASE_OFF;
11760 }
11761
11762 /* No toc references were found in ISEC. If the code in ISEC makes no
11763 calls, then there's no need to use toc adjusting stubs when branching
11764 into ISEC. Actually, indirect calls from ISEC are OK as they will
11765 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11766 needed, and 2 if a cyclical call-graph was found but no other reason
11767 for a stub was detected. If called from the top level, a return of
11768 2 means the same as a return of 0. */
11769
11770 static int
11771 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11772 {
11773 int ret;
11774
11775 /* Mark this section as checked. */
11776 isec->call_check_done = 1;
11777
11778 /* We know none of our code bearing sections will need toc stubs. */
11779 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11780 return 0;
11781
11782 if (isec->size == 0)
11783 return 0;
11784
11785 if (isec->output_section == NULL)
11786 return 0;
11787
11788 ret = 0;
11789 if (isec->reloc_count != 0)
11790 {
11791 Elf_Internal_Rela *relstart, *rel;
11792 Elf_Internal_Sym *local_syms;
11793 struct ppc_link_hash_table *htab;
11794
11795 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11796 info->keep_memory);
11797 if (relstart == NULL)
11798 return -1;
11799
11800 /* Look for branches to outside of this section. */
11801 local_syms = NULL;
11802 htab = ppc_hash_table (info);
11803 if (htab == NULL)
11804 return -1;
11805
11806 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11807 {
11808 enum elf_ppc64_reloc_type r_type;
11809 unsigned long r_symndx;
11810 struct elf_link_hash_entry *h;
11811 struct ppc_link_hash_entry *eh;
11812 Elf_Internal_Sym *sym;
11813 asection *sym_sec;
11814 struct _opd_sec_data *opd;
11815 bfd_vma sym_value;
11816 bfd_vma dest;
11817
11818 r_type = ELF64_R_TYPE (rel->r_info);
11819 if (r_type != R_PPC64_REL24
11820 && r_type != R_PPC64_REL14
11821 && r_type != R_PPC64_REL14_BRTAKEN
11822 && r_type != R_PPC64_REL14_BRNTAKEN)
11823 continue;
11824
11825 r_symndx = ELF64_R_SYM (rel->r_info);
11826 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11827 isec->owner))
11828 {
11829 ret = -1;
11830 break;
11831 }
11832
11833 /* Calls to dynamic lib functions go through a plt call stub
11834 that uses r2. */
11835 eh = (struct ppc_link_hash_entry *) h;
11836 if (eh != NULL
11837 && (eh->elf.plt.plist != NULL
11838 || (eh->oh != NULL
11839 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11840 {
11841 ret = 1;
11842 break;
11843 }
11844
11845 if (sym_sec == NULL)
11846 /* Ignore other undefined symbols. */
11847 continue;
11848
11849 /* Assume branches to other sections not included in the
11850 link need stubs too, to cover -R and absolute syms. */
11851 if (sym_sec->output_section == NULL)
11852 {
11853 ret = 1;
11854 break;
11855 }
11856
11857 if (h == NULL)
11858 sym_value = sym->st_value;
11859 else
11860 {
11861 if (h->root.type != bfd_link_hash_defined
11862 && h->root.type != bfd_link_hash_defweak)
11863 abort ();
11864 sym_value = h->root.u.def.value;
11865 }
11866 sym_value += rel->r_addend;
11867
11868 /* If this branch reloc uses an opd sym, find the code section. */
11869 opd = get_opd_info (sym_sec);
11870 if (opd != NULL)
11871 {
11872 if (h == NULL && opd->adjust != NULL)
11873 {
11874 long adjust;
11875
11876 adjust = opd->adjust[OPD_NDX (sym_value)];
11877 if (adjust == -1)
11878 /* Assume deleted functions won't ever be called. */
11879 continue;
11880 sym_value += adjust;
11881 }
11882
11883 dest = opd_entry_value (sym_sec, sym_value,
11884 &sym_sec, NULL, FALSE);
11885 if (dest == (bfd_vma) -1)
11886 continue;
11887 }
11888 else
11889 dest = (sym_value
11890 + sym_sec->output_offset
11891 + sym_sec->output_section->vma);
11892
11893 /* Ignore branch to self. */
11894 if (sym_sec == isec)
11895 continue;
11896
11897 /* If the called function uses the toc, we need a stub. */
11898 if (sym_sec->has_toc_reloc
11899 || sym_sec->makes_toc_func_call)
11900 {
11901 ret = 1;
11902 break;
11903 }
11904
11905 /* Assume any branch that needs a long branch stub might in fact
11906 need a plt_branch stub. A plt_branch stub uses r2. */
11907 else if (dest - (isec->output_offset
11908 + isec->output_section->vma
11909 + rel->r_offset) + (1 << 25)
11910 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11911 ? h->other
11912 : sym->st_other))
11913 {
11914 ret = 1;
11915 break;
11916 }
11917
11918 /* If calling back to a section in the process of being
11919 tested, we can't say for sure that no toc adjusting stubs
11920 are needed, so don't return zero. */
11921 else if (sym_sec->call_check_in_progress)
11922 ret = 2;
11923
11924 /* Branches to another section that itself doesn't have any TOC
11925 references are OK. Recursively call ourselves to check. */
11926 else if (!sym_sec->call_check_done)
11927 {
11928 int recur;
11929
11930 /* Mark current section as indeterminate, so that other
11931 sections that call back to current won't be marked as
11932 known. */
11933 isec->call_check_in_progress = 1;
11934 recur = toc_adjusting_stub_needed (info, sym_sec);
11935 isec->call_check_in_progress = 0;
11936
11937 if (recur != 0)
11938 {
11939 ret = recur;
11940 if (recur != 2)
11941 break;
11942 }
11943 }
11944 }
11945
11946 if (local_syms != NULL
11947 && (elf_symtab_hdr (isec->owner).contents
11948 != (unsigned char *) local_syms))
11949 free (local_syms);
11950 if (elf_section_data (isec)->relocs != relstart)
11951 free (relstart);
11952 }
11953
11954 if ((ret & 1) == 0
11955 && isec->map_head.s != NULL
11956 && (strcmp (isec->output_section->name, ".init") == 0
11957 || strcmp (isec->output_section->name, ".fini") == 0))
11958 {
11959 if (isec->map_head.s->has_toc_reloc
11960 || isec->map_head.s->makes_toc_func_call)
11961 ret = 1;
11962 else if (!isec->map_head.s->call_check_done)
11963 {
11964 int recur;
11965 isec->call_check_in_progress = 1;
11966 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11967 isec->call_check_in_progress = 0;
11968 if (recur != 0)
11969 ret = recur;
11970 }
11971 }
11972
11973 if (ret == 1)
11974 isec->makes_toc_func_call = 1;
11975
11976 return ret;
11977 }
11978
11979 /* The linker repeatedly calls this function for each input section,
11980 in the order that input sections are linked into output sections.
11981 Build lists of input sections to determine groupings between which
11982 we may insert linker stubs. */
11983
11984 bfd_boolean
11985 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11986 {
11987 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11988
11989 if (htab == NULL)
11990 return FALSE;
11991
11992 if ((isec->output_section->flags & SEC_CODE) != 0
11993 && isec->output_section->id < htab->sec_info_arr_size)
11994 {
11995 /* This happens to make the list in reverse order,
11996 which is what we want. */
11997 htab->sec_info[isec->id].u.list
11998 = htab->sec_info[isec->output_section->id].u.list;
11999 htab->sec_info[isec->output_section->id].u.list = isec;
12000 }
12001
12002 if (htab->multi_toc_needed)
12003 {
12004 /* Analyse sections that aren't already flagged as needing a
12005 valid toc pointer. Exclude .fixup for the linux kernel.
12006 .fixup contains branches, but only back to the function that
12007 hit an exception. */
12008 if (!(isec->has_toc_reloc
12009 || (isec->flags & SEC_CODE) == 0
12010 || strcmp (isec->name, ".fixup") == 0
12011 || isec->call_check_done))
12012 {
12013 if (toc_adjusting_stub_needed (info, isec) < 0)
12014 return FALSE;
12015 }
12016 /* Make all sections use the TOC assigned for this object file.
12017 This will be wrong for pasted sections; We fix that in
12018 check_pasted_section(). */
12019 if (elf_gp (isec->owner) != 0)
12020 htab->toc_curr = elf_gp (isec->owner);
12021 }
12022
12023 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12024 return TRUE;
12025 }
12026
12027 /* Check that all .init and .fini sections use the same toc, if they
12028 have toc relocs. */
12029
12030 static bfd_boolean
12031 check_pasted_section (struct bfd_link_info *info, const char *name)
12032 {
12033 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12034
12035 if (o != NULL)
12036 {
12037 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12038 bfd_vma toc_off = 0;
12039 asection *i;
12040
12041 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12042 if (i->has_toc_reloc)
12043 {
12044 if (toc_off == 0)
12045 toc_off = htab->sec_info[i->id].toc_off;
12046 else if (toc_off != htab->sec_info[i->id].toc_off)
12047 return FALSE;
12048 }
12049
12050 if (toc_off == 0)
12051 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12052 if (i->makes_toc_func_call)
12053 {
12054 toc_off = htab->sec_info[i->id].toc_off;
12055 break;
12056 }
12057
12058 /* Make sure the whole pasted function uses the same toc offset. */
12059 if (toc_off != 0)
12060 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12061 htab->sec_info[i->id].toc_off = toc_off;
12062 }
12063 return TRUE;
12064 }
12065
12066 bfd_boolean
12067 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12068 {
12069 return (check_pasted_section (info, ".init")
12070 & check_pasted_section (info, ".fini"));
12071 }
12072
12073 /* See whether we can group stub sections together. Grouping stub
12074 sections may result in fewer stubs. More importantly, we need to
12075 put all .init* and .fini* stubs at the beginning of the .init or
12076 .fini output sections respectively, because glibc splits the
12077 _init and _fini functions into multiple parts. Putting a stub in
12078 the middle of a function is not a good idea. */
12079
12080 static bfd_boolean
12081 group_sections (struct bfd_link_info *info,
12082 bfd_size_type stub_group_size,
12083 bfd_boolean stubs_always_before_branch)
12084 {
12085 struct ppc_link_hash_table *htab;
12086 asection *osec;
12087 bfd_boolean suppress_size_errors;
12088
12089 htab = ppc_hash_table (info);
12090 if (htab == NULL)
12091 return FALSE;
12092
12093 suppress_size_errors = FALSE;
12094 if (stub_group_size == 1)
12095 {
12096 /* Default values. */
12097 if (stubs_always_before_branch)
12098 stub_group_size = 0x1e00000;
12099 else
12100 stub_group_size = 0x1c00000;
12101 suppress_size_errors = TRUE;
12102 }
12103
12104 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12105 {
12106 asection *tail;
12107
12108 if (osec->id >= htab->sec_info_arr_size)
12109 continue;
12110
12111 tail = htab->sec_info[osec->id].u.list;
12112 while (tail != NULL)
12113 {
12114 asection *curr;
12115 asection *prev;
12116 bfd_size_type total;
12117 bfd_boolean big_sec;
12118 bfd_vma curr_toc;
12119 struct map_stub *group;
12120 bfd_size_type group_size;
12121
12122 curr = tail;
12123 total = tail->size;
12124 group_size = (ppc64_elf_section_data (tail) != NULL
12125 && ppc64_elf_section_data (tail)->has_14bit_branch
12126 ? stub_group_size >> 10 : stub_group_size);
12127
12128 big_sec = total > group_size;
12129 if (big_sec && !suppress_size_errors)
12130 /* xgettext:c-format */
12131 _bfd_error_handler (_("%B section %A exceeds stub group size"),
12132 tail->owner, tail);
12133 curr_toc = htab->sec_info[tail->id].toc_off;
12134
12135 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12136 && ((total += curr->output_offset - prev->output_offset)
12137 < (ppc64_elf_section_data (prev) != NULL
12138 && ppc64_elf_section_data (prev)->has_14bit_branch
12139 ? (group_size = stub_group_size >> 10) : group_size))
12140 && htab->sec_info[prev->id].toc_off == curr_toc)
12141 curr = prev;
12142
12143 /* OK, the size from the start of CURR to the end is less
12144 than group_size and thus can be handled by one stub
12145 section. (or the tail section is itself larger than
12146 group_size, in which case we may be toast.) We should
12147 really be keeping track of the total size of stubs added
12148 here, as stubs contribute to the final output section
12149 size. That's a little tricky, and this way will only
12150 break if stubs added make the total size more than 2^25,
12151 ie. for the default stub_group_size, if stubs total more
12152 than 2097152 bytes, or nearly 75000 plt call stubs. */
12153 group = bfd_alloc (curr->owner, sizeof (*group));
12154 if (group == NULL)
12155 return FALSE;
12156 group->link_sec = curr;
12157 group->stub_sec = NULL;
12158 group->needs_save_res = 0;
12159 group->tls_get_addr_opt_bctrl = -1u;
12160 group->next = htab->group;
12161 htab->group = group;
12162 do
12163 {
12164 prev = htab->sec_info[tail->id].u.list;
12165 /* Set up this stub group. */
12166 htab->sec_info[tail->id].u.group = group;
12167 }
12168 while (tail != curr && (tail = prev) != NULL);
12169
12170 /* But wait, there's more! Input sections up to group_size
12171 bytes before the stub section can be handled by it too.
12172 Don't do this if we have a really large section after the
12173 stubs, as adding more stubs increases the chance that
12174 branches may not reach into the stub section. */
12175 if (!stubs_always_before_branch && !big_sec)
12176 {
12177 total = 0;
12178 while (prev != NULL
12179 && ((total += tail->output_offset - prev->output_offset)
12180 < (ppc64_elf_section_data (prev) != NULL
12181 && ppc64_elf_section_data (prev)->has_14bit_branch
12182 ? (group_size = stub_group_size >> 10) : group_size))
12183 && htab->sec_info[prev->id].toc_off == curr_toc)
12184 {
12185 tail = prev;
12186 prev = htab->sec_info[tail->id].u.list;
12187 htab->sec_info[tail->id].u.group = group;
12188 }
12189 }
12190 tail = prev;
12191 }
12192 }
12193 return TRUE;
12194 }
12195
12196 static const unsigned char glink_eh_frame_cie[] =
12197 {
12198 0, 0, 0, 16, /* length. */
12199 0, 0, 0, 0, /* id. */
12200 1, /* CIE version. */
12201 'z', 'R', 0, /* Augmentation string. */
12202 4, /* Code alignment. */
12203 0x78, /* Data alignment. */
12204 65, /* RA reg. */
12205 1, /* Augmentation size. */
12206 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12207 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12208 };
12209
12210 static size_t
12211 stub_eh_frame_size (struct map_stub *group, size_t align)
12212 {
12213 size_t this_size = 17;
12214 if (group->tls_get_addr_opt_bctrl != -1u)
12215 {
12216 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12217 if (to_bctrl < 64)
12218 this_size += 1;
12219 else if (to_bctrl < 256)
12220 this_size += 2;
12221 else if (to_bctrl < 65536)
12222 this_size += 3;
12223 else
12224 this_size += 5;
12225 this_size += 6;
12226 }
12227 this_size = (this_size + align - 1) & -align;
12228 return this_size;
12229 }
12230
12231 /* Stripping output sections is normally done before dynamic section
12232 symbols have been allocated. This function is called later, and
12233 handles cases like htab->brlt which is mapped to its own output
12234 section. */
12235
12236 static void
12237 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12238 {
12239 if (isec->size == 0
12240 && isec->output_section->size == 0
12241 && !(isec->output_section->flags & SEC_KEEP)
12242 && !bfd_section_removed_from_list (info->output_bfd,
12243 isec->output_section)
12244 && elf_section_data (isec->output_section)->dynindx == 0)
12245 {
12246 isec->output_section->flags |= SEC_EXCLUDE;
12247 bfd_section_list_remove (info->output_bfd, isec->output_section);
12248 info->output_bfd->section_count--;
12249 }
12250 }
12251
12252 /* Determine and set the size of the stub section for a final link.
12253
12254 The basic idea here is to examine all the relocations looking for
12255 PC-relative calls to a target that is unreachable with a "bl"
12256 instruction. */
12257
12258 bfd_boolean
12259 ppc64_elf_size_stubs (struct bfd_link_info *info)
12260 {
12261 bfd_size_type stub_group_size;
12262 bfd_boolean stubs_always_before_branch;
12263 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12264
12265 if (htab == NULL)
12266 return FALSE;
12267
12268 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12269 htab->params->plt_thread_safe = 1;
12270 if (!htab->opd_abi)
12271 htab->params->plt_thread_safe = 0;
12272 else if (htab->params->plt_thread_safe == -1)
12273 {
12274 static const char *const thread_starter[] =
12275 {
12276 "pthread_create",
12277 /* libstdc++ */
12278 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12279 /* librt */
12280 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12281 "mq_notify", "create_timer",
12282 /* libanl */
12283 "getaddrinfo_a",
12284 /* libgomp */
12285 "GOMP_parallel",
12286 "GOMP_parallel_start",
12287 "GOMP_parallel_loop_static",
12288 "GOMP_parallel_loop_static_start",
12289 "GOMP_parallel_loop_dynamic",
12290 "GOMP_parallel_loop_dynamic_start",
12291 "GOMP_parallel_loop_guided",
12292 "GOMP_parallel_loop_guided_start",
12293 "GOMP_parallel_loop_runtime",
12294 "GOMP_parallel_loop_runtime_start",
12295 "GOMP_parallel_sections",
12296 "GOMP_parallel_sections_start",
12297 /* libgo */
12298 "__go_go",
12299 };
12300 unsigned i;
12301
12302 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12303 {
12304 struct elf_link_hash_entry *h;
12305 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12306 FALSE, FALSE, TRUE);
12307 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12308 if (htab->params->plt_thread_safe)
12309 break;
12310 }
12311 }
12312 stubs_always_before_branch = htab->params->group_size < 0;
12313 if (htab->params->group_size < 0)
12314 stub_group_size = -htab->params->group_size;
12315 else
12316 stub_group_size = htab->params->group_size;
12317
12318 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12319 return FALSE;
12320
12321 #define STUB_SHRINK_ITER 20
12322 /* Loop until no stubs added. After iteration 20 of this loop we may
12323 exit on a stub section shrinking. This is to break out of a
12324 pathological case where adding stubs on one iteration decreases
12325 section gaps (perhaps due to alignment), which then requires
12326 fewer or smaller stubs on the next iteration. */
12327
12328 while (1)
12329 {
12330 bfd *input_bfd;
12331 unsigned int bfd_indx;
12332 struct map_stub *group;
12333
12334 htab->stub_iteration += 1;
12335
12336 for (input_bfd = info->input_bfds, bfd_indx = 0;
12337 input_bfd != NULL;
12338 input_bfd = input_bfd->link.next, bfd_indx++)
12339 {
12340 Elf_Internal_Shdr *symtab_hdr;
12341 asection *section;
12342 Elf_Internal_Sym *local_syms = NULL;
12343
12344 if (!is_ppc64_elf (input_bfd))
12345 continue;
12346
12347 /* We'll need the symbol table in a second. */
12348 symtab_hdr = &elf_symtab_hdr (input_bfd);
12349 if (symtab_hdr->sh_info == 0)
12350 continue;
12351
12352 /* Walk over each section attached to the input bfd. */
12353 for (section = input_bfd->sections;
12354 section != NULL;
12355 section = section->next)
12356 {
12357 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
12358
12359 /* If there aren't any relocs, then there's nothing more
12360 to do. */
12361 if ((section->flags & SEC_RELOC) == 0
12362 || (section->flags & SEC_ALLOC) == 0
12363 || (section->flags & SEC_LOAD) == 0
12364 || (section->flags & SEC_CODE) == 0
12365 || section->reloc_count == 0)
12366 continue;
12367
12368 /* If this section is a link-once section that will be
12369 discarded, then don't create any stubs. */
12370 if (section->output_section == NULL
12371 || section->output_section->owner != info->output_bfd)
12372 continue;
12373
12374 /* Get the relocs. */
12375 internal_relocs
12376 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
12377 info->keep_memory);
12378 if (internal_relocs == NULL)
12379 goto error_ret_free_local;
12380
12381 /* Now examine each relocation. */
12382 irela = internal_relocs;
12383 irelaend = irela + section->reloc_count;
12384 for (; irela < irelaend; irela++)
12385 {
12386 enum elf_ppc64_reloc_type r_type;
12387 unsigned int r_indx;
12388 enum ppc_stub_type stub_type;
12389 struct ppc_stub_hash_entry *stub_entry;
12390 asection *sym_sec, *code_sec;
12391 bfd_vma sym_value, code_value;
12392 bfd_vma destination;
12393 unsigned long local_off;
12394 bfd_boolean ok_dest;
12395 struct ppc_link_hash_entry *hash;
12396 struct ppc_link_hash_entry *fdh;
12397 struct elf_link_hash_entry *h;
12398 Elf_Internal_Sym *sym;
12399 char *stub_name;
12400 const asection *id_sec;
12401 struct _opd_sec_data *opd;
12402 struct plt_entry *plt_ent;
12403
12404 r_type = ELF64_R_TYPE (irela->r_info);
12405 r_indx = ELF64_R_SYM (irela->r_info);
12406
12407 if (r_type >= R_PPC64_max)
12408 {
12409 bfd_set_error (bfd_error_bad_value);
12410 goto error_ret_free_internal;
12411 }
12412
12413 /* Only look for stubs on branch instructions. */
12414 if (r_type != R_PPC64_REL24
12415 && r_type != R_PPC64_REL14
12416 && r_type != R_PPC64_REL14_BRTAKEN
12417 && r_type != R_PPC64_REL14_BRNTAKEN)
12418 continue;
12419
12420 /* Now determine the call target, its name, value,
12421 section. */
12422 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
12423 r_indx, input_bfd))
12424 goto error_ret_free_internal;
12425 hash = (struct ppc_link_hash_entry *) h;
12426
12427 ok_dest = FALSE;
12428 fdh = NULL;
12429 sym_value = 0;
12430 if (hash == NULL)
12431 {
12432 sym_value = sym->st_value;
12433 if (sym_sec != NULL
12434 && sym_sec->output_section != NULL)
12435 ok_dest = TRUE;
12436 }
12437 else if (hash->elf.root.type == bfd_link_hash_defined
12438 || hash->elf.root.type == bfd_link_hash_defweak)
12439 {
12440 sym_value = hash->elf.root.u.def.value;
12441 if (sym_sec->output_section != NULL)
12442 ok_dest = TRUE;
12443 }
12444 else if (hash->elf.root.type == bfd_link_hash_undefweak
12445 || hash->elf.root.type == bfd_link_hash_undefined)
12446 {
12447 /* Recognise an old ABI func code entry sym, and
12448 use the func descriptor sym instead if it is
12449 defined. */
12450 if (hash->elf.root.root.string[0] == '.'
12451 && hash->oh != NULL)
12452 {
12453 fdh = ppc_follow_link (hash->oh);
12454 if (fdh->elf.root.type == bfd_link_hash_defined
12455 || fdh->elf.root.type == bfd_link_hash_defweak)
12456 {
12457 sym_sec = fdh->elf.root.u.def.section;
12458 sym_value = fdh->elf.root.u.def.value;
12459 if (sym_sec->output_section != NULL)
12460 ok_dest = TRUE;
12461 }
12462 else
12463 fdh = NULL;
12464 }
12465 }
12466 else
12467 {
12468 bfd_set_error (bfd_error_bad_value);
12469 goto error_ret_free_internal;
12470 }
12471
12472 destination = 0;
12473 local_off = 0;
12474 if (ok_dest)
12475 {
12476 sym_value += irela->r_addend;
12477 destination = (sym_value
12478 + sym_sec->output_offset
12479 + sym_sec->output_section->vma);
12480 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
12481 ? hash->elf.other
12482 : sym->st_other);
12483 }
12484
12485 code_sec = sym_sec;
12486 code_value = sym_value;
12487 opd = get_opd_info (sym_sec);
12488 if (opd != NULL)
12489 {
12490 bfd_vma dest;
12491
12492 if (hash == NULL && opd->adjust != NULL)
12493 {
12494 long adjust = opd->adjust[OPD_NDX (sym_value)];
12495 if (adjust == -1)
12496 continue;
12497 code_value += adjust;
12498 sym_value += adjust;
12499 }
12500 dest = opd_entry_value (sym_sec, sym_value,
12501 &code_sec, &code_value, FALSE);
12502 if (dest != (bfd_vma) -1)
12503 {
12504 destination = dest;
12505 if (fdh != NULL)
12506 {
12507 /* Fixup old ABI sym to point at code
12508 entry. */
12509 hash->elf.root.type = bfd_link_hash_defweak;
12510 hash->elf.root.u.def.section = code_sec;
12511 hash->elf.root.u.def.value = code_value;
12512 }
12513 }
12514 }
12515
12516 /* Determine what (if any) linker stub is needed. */
12517 plt_ent = NULL;
12518 stub_type = ppc_type_of_stub (section, irela, &hash,
12519 &plt_ent, destination,
12520 local_off);
12521
12522 if (stub_type != ppc_stub_plt_call)
12523 {
12524 /* Check whether we need a TOC adjusting stub.
12525 Since the linker pastes together pieces from
12526 different object files when creating the
12527 _init and _fini functions, it may be that a
12528 call to what looks like a local sym is in
12529 fact a call needing a TOC adjustment. */
12530 if (code_sec != NULL
12531 && code_sec->output_section != NULL
12532 && (htab->sec_info[code_sec->id].toc_off
12533 != htab->sec_info[section->id].toc_off)
12534 && (code_sec->has_toc_reloc
12535 || code_sec->makes_toc_func_call))
12536 stub_type = ppc_stub_long_branch_r2off;
12537 }
12538
12539 if (stub_type == ppc_stub_none)
12540 continue;
12541
12542 /* __tls_get_addr calls might be eliminated. */
12543 if (stub_type != ppc_stub_plt_call
12544 && hash != NULL
12545 && (hash == htab->tls_get_addr
12546 || hash == htab->tls_get_addr_fd)
12547 && section->has_tls_reloc
12548 && irela != internal_relocs)
12549 {
12550 /* Get tls info. */
12551 unsigned char *tls_mask;
12552
12553 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
12554 irela - 1, input_bfd))
12555 goto error_ret_free_internal;
12556 if (*tls_mask != 0)
12557 continue;
12558 }
12559
12560 if (stub_type == ppc_stub_plt_call)
12561 {
12562 if (!htab->opd_abi
12563 && htab->params->plt_localentry0 != 0
12564 && is_elfv2_localentry0 (&hash->elf))
12565 htab->has_plt_localentry0 = 1;
12566 else if (irela + 1 < irelaend
12567 && irela[1].r_offset == irela->r_offset + 4
12568 && (ELF64_R_TYPE (irela[1].r_info)
12569 == R_PPC64_TOCSAVE))
12570 {
12571 if (!tocsave_find (htab, INSERT,
12572 &local_syms, irela + 1, input_bfd))
12573 goto error_ret_free_internal;
12574 }
12575 else
12576 stub_type = ppc_stub_plt_call_r2save;
12577 }
12578
12579 /* Support for grouping stub sections. */
12580 id_sec = htab->sec_info[section->id].u.group->link_sec;
12581
12582 /* Get the name of this stub. */
12583 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
12584 if (!stub_name)
12585 goto error_ret_free_internal;
12586
12587 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
12588 stub_name, FALSE, FALSE);
12589 if (stub_entry != NULL)
12590 {
12591 /* The proper stub has already been created. */
12592 free (stub_name);
12593 if (stub_type == ppc_stub_plt_call_r2save)
12594 stub_entry->stub_type = stub_type;
12595 continue;
12596 }
12597
12598 stub_entry = ppc_add_stub (stub_name, section, info);
12599 if (stub_entry == NULL)
12600 {
12601 free (stub_name);
12602 error_ret_free_internal:
12603 if (elf_section_data (section)->relocs == NULL)
12604 free (internal_relocs);
12605 error_ret_free_local:
12606 if (local_syms != NULL
12607 && (symtab_hdr->contents
12608 != (unsigned char *) local_syms))
12609 free (local_syms);
12610 return FALSE;
12611 }
12612
12613 stub_entry->stub_type = stub_type;
12614 if (stub_type != ppc_stub_plt_call
12615 && stub_type != ppc_stub_plt_call_r2save)
12616 {
12617 stub_entry->target_value = code_value;
12618 stub_entry->target_section = code_sec;
12619 }
12620 else
12621 {
12622 stub_entry->target_value = sym_value;
12623 stub_entry->target_section = sym_sec;
12624 }
12625 stub_entry->h = hash;
12626 stub_entry->plt_ent = plt_ent;
12627 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12628
12629 if (stub_entry->h != NULL)
12630 htab->stub_globals += 1;
12631 }
12632
12633 /* We're done with the internal relocs, free them. */
12634 if (elf_section_data (section)->relocs != internal_relocs)
12635 free (internal_relocs);
12636 }
12637
12638 if (local_syms != NULL
12639 && symtab_hdr->contents != (unsigned char *) local_syms)
12640 {
12641 if (!info->keep_memory)
12642 free (local_syms);
12643 else
12644 symtab_hdr->contents = (unsigned char *) local_syms;
12645 }
12646 }
12647
12648 /* We may have added some stubs. Find out the new size of the
12649 stub sections. */
12650 for (group = htab->group; group != NULL; group = group->next)
12651 if (group->stub_sec != NULL)
12652 {
12653 asection *stub_sec = group->stub_sec;
12654
12655 if (htab->stub_iteration <= STUB_SHRINK_ITER
12656 || stub_sec->rawsize < stub_sec->size)
12657 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
12658 stub_sec->rawsize = stub_sec->size;
12659 stub_sec->size = 0;
12660 stub_sec->reloc_count = 0;
12661 stub_sec->flags &= ~SEC_RELOC;
12662 }
12663
12664 if (htab->stub_iteration <= STUB_SHRINK_ITER
12665 || htab->brlt->rawsize < htab->brlt->size)
12666 htab->brlt->rawsize = htab->brlt->size;
12667 htab->brlt->size = 0;
12668 htab->brlt->reloc_count = 0;
12669 htab->brlt->flags &= ~SEC_RELOC;
12670 if (htab->relbrlt != NULL)
12671 htab->relbrlt->size = 0;
12672
12673 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12674
12675 for (group = htab->group; group != NULL; group = group->next)
12676 if (group->needs_save_res)
12677 group->stub_sec->size += htab->sfpr->size;
12678
12679 if (info->emitrelocations
12680 && htab->glink != NULL && htab->glink->size != 0)
12681 {
12682 htab->glink->reloc_count = 1;
12683 htab->glink->flags |= SEC_RELOC;
12684 }
12685
12686 if (htab->glink_eh_frame != NULL
12687 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12688 && htab->glink_eh_frame->output_section->size > 8)
12689 {
12690 size_t size = 0, align = 4;
12691
12692 for (group = htab->group; group != NULL; group = group->next)
12693 if (group->stub_sec != NULL)
12694 size += stub_eh_frame_size (group, align);
12695 if (htab->glink != NULL && htab->glink->size != 0)
12696 size += (24 + align - 1) & -align;
12697 if (size != 0)
12698 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
12699 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12700 size = (size + align - 1) & -align;
12701 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12702 htab->glink_eh_frame->size = size;
12703 }
12704
12705 if (htab->params->plt_stub_align != 0)
12706 for (group = htab->group; group != NULL; group = group->next)
12707 if (group->stub_sec != NULL)
12708 {
12709 int align = abs (htab->params->plt_stub_align);
12710 group->stub_sec->size
12711 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
12712 }
12713
12714 for (group = htab->group; group != NULL; group = group->next)
12715 if (group->stub_sec != NULL
12716 && group->stub_sec->rawsize != group->stub_sec->size
12717 && (htab->stub_iteration <= STUB_SHRINK_ITER
12718 || group->stub_sec->rawsize < group->stub_sec->size))
12719 break;
12720
12721 if (group == NULL
12722 && (htab->brlt->rawsize == htab->brlt->size
12723 || (htab->stub_iteration > STUB_SHRINK_ITER
12724 && htab->brlt->rawsize > htab->brlt->size))
12725 && (htab->glink_eh_frame == NULL
12726 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12727 break;
12728
12729 /* Ask the linker to do its stuff. */
12730 (*htab->params->layout_sections_again) ();
12731 }
12732
12733 if (htab->glink_eh_frame != NULL
12734 && htab->glink_eh_frame->size != 0)
12735 {
12736 bfd_vma val;
12737 bfd_byte *p, *last_fde;
12738 size_t last_fde_len, size, align, pad;
12739 struct map_stub *group;
12740
12741 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12742 if (p == NULL)
12743 return FALSE;
12744 htab->glink_eh_frame->contents = p;
12745 last_fde = p;
12746 align = 4;
12747
12748 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12749 /* CIE length (rewrite in case little-endian). */
12750 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
12751 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12752 p += last_fde_len + 4;
12753
12754 for (group = htab->group; group != NULL; group = group->next)
12755 if (group->stub_sec != NULL)
12756 {
12757 last_fde = p;
12758 last_fde_len = stub_eh_frame_size (group, align) - 4;
12759 /* FDE length. */
12760 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12761 p += 4;
12762 /* CIE pointer. */
12763 val = p - htab->glink_eh_frame->contents;
12764 bfd_put_32 (htab->elf.dynobj, val, p);
12765 p += 4;
12766 /* Offset to stub section, written later. */
12767 p += 4;
12768 /* stub section size. */
12769 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
12770 p += 4;
12771 /* Augmentation. */
12772 p += 1;
12773 if (group->tls_get_addr_opt_bctrl != -1u)
12774 {
12775 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12776
12777 /* This FDE needs more than just the default.
12778 Describe __tls_get_addr_opt stub LR. */
12779 if (to_bctrl < 64)
12780 *p++ = DW_CFA_advance_loc + to_bctrl;
12781 else if (to_bctrl < 256)
12782 {
12783 *p++ = DW_CFA_advance_loc1;
12784 *p++ = to_bctrl;
12785 }
12786 else if (to_bctrl < 65536)
12787 {
12788 *p++ = DW_CFA_advance_loc2;
12789 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
12790 p += 2;
12791 }
12792 else
12793 {
12794 *p++ = DW_CFA_advance_loc4;
12795 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
12796 p += 4;
12797 }
12798 *p++ = DW_CFA_offset_extended_sf;
12799 *p++ = 65;
12800 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
12801 *p++ = DW_CFA_advance_loc + 4;
12802 *p++ = DW_CFA_restore_extended;
12803 *p++ = 65;
12804 }
12805 /* Pad. */
12806 p = last_fde + last_fde_len + 4;
12807 }
12808 if (htab->glink != NULL && htab->glink->size != 0)
12809 {
12810 last_fde = p;
12811 last_fde_len = ((24 + align - 1) & -align) - 4;
12812 /* FDE length. */
12813 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12814 p += 4;
12815 /* CIE pointer. */
12816 val = p - htab->glink_eh_frame->contents;
12817 bfd_put_32 (htab->elf.dynobj, val, p);
12818 p += 4;
12819 /* Offset to .glink, written later. */
12820 p += 4;
12821 /* .glink size. */
12822 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
12823 p += 4;
12824 /* Augmentation. */
12825 p += 1;
12826
12827 *p++ = DW_CFA_advance_loc + 1;
12828 *p++ = DW_CFA_register;
12829 *p++ = 65;
12830 *p++ = htab->opd_abi ? 12 : 0;
12831 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
12832 *p++ = DW_CFA_restore_extended;
12833 *p++ = 65;
12834 p += ((24 + align - 1) & -align) - 24;
12835 }
12836 /* Subsume any padding into the last FDE if user .eh_frame
12837 sections are aligned more than glink_eh_frame. Otherwise any
12838 zero padding will be seen as a terminator. */
12839 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
12840 size = p - htab->glink_eh_frame->contents;
12841 pad = ((size + align - 1) & -align) - size;
12842 htab->glink_eh_frame->size = size + pad;
12843 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12844 }
12845
12846 maybe_strip_output (info, htab->brlt);
12847 if (htab->glink_eh_frame != NULL)
12848 maybe_strip_output (info, htab->glink_eh_frame);
12849
12850 return TRUE;
12851 }
12852
12853 /* Called after we have determined section placement. If sections
12854 move, we'll be called again. Provide a value for TOCstart. */
12855
12856 bfd_vma
12857 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12858 {
12859 asection *s;
12860 bfd_vma TOCstart, adjust;
12861
12862 if (info != NULL)
12863 {
12864 struct elf_link_hash_entry *h;
12865 struct elf_link_hash_table *htab = elf_hash_table (info);
12866
12867 if (is_elf_hash_table (htab)
12868 && htab->hgot != NULL)
12869 h = htab->hgot;
12870 else
12871 {
12872 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
12873 if (is_elf_hash_table (htab))
12874 htab->hgot = h;
12875 }
12876 if (h != NULL
12877 && h->root.type == bfd_link_hash_defined
12878 && !h->root.linker_def
12879 && (!is_elf_hash_table (htab)
12880 || h->def_regular))
12881 {
12882 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
12883 + h->root.u.def.section->output_offset
12884 + h->root.u.def.section->output_section->vma);
12885 _bfd_set_gp_value (obfd, TOCstart);
12886 return TOCstart;
12887 }
12888 }
12889
12890 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12891 order. The TOC starts where the first of these sections starts. */
12892 s = bfd_get_section_by_name (obfd, ".got");
12893 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12894 s = bfd_get_section_by_name (obfd, ".toc");
12895 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12896 s = bfd_get_section_by_name (obfd, ".tocbss");
12897 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12898 s = bfd_get_section_by_name (obfd, ".plt");
12899 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12900 {
12901 /* This may happen for
12902 o references to TOC base (SYM@toc / TOC[tc0]) without a
12903 .toc directive
12904 o bad linker script
12905 o --gc-sections and empty TOC sections
12906
12907 FIXME: Warn user? */
12908
12909 /* Look for a likely section. We probably won't even be
12910 using TOCstart. */
12911 for (s = obfd->sections; s != NULL; s = s->next)
12912 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12913 | SEC_EXCLUDE))
12914 == (SEC_ALLOC | SEC_SMALL_DATA))
12915 break;
12916 if (s == NULL)
12917 for (s = obfd->sections; s != NULL; s = s->next)
12918 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12919 == (SEC_ALLOC | SEC_SMALL_DATA))
12920 break;
12921 if (s == NULL)
12922 for (s = obfd->sections; s != NULL; s = s->next)
12923 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12924 == SEC_ALLOC)
12925 break;
12926 if (s == NULL)
12927 for (s = obfd->sections; s != NULL; s = s->next)
12928 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12929 break;
12930 }
12931
12932 TOCstart = 0;
12933 if (s != NULL)
12934 TOCstart = s->output_section->vma + s->output_offset;
12935
12936 /* Force alignment. */
12937 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
12938 TOCstart -= adjust;
12939 _bfd_set_gp_value (obfd, TOCstart);
12940
12941 if (info != NULL && s != NULL)
12942 {
12943 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12944
12945 if (htab != NULL)
12946 {
12947 if (htab->elf.hgot != NULL)
12948 {
12949 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
12950 htab->elf.hgot->root.u.def.section = s;
12951 }
12952 }
12953 else
12954 {
12955 struct bfd_link_hash_entry *bh = NULL;
12956 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
12957 s, TOC_BASE_OFF - adjust,
12958 NULL, FALSE, FALSE, &bh);
12959 }
12960 }
12961 return TOCstart;
12962 }
12963
12964 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
12965 write out any global entry stubs. */
12966
12967 static bfd_boolean
12968 build_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
12969 {
12970 struct bfd_link_info *info;
12971 struct ppc_link_hash_table *htab;
12972 struct plt_entry *pent;
12973 asection *s;
12974
12975 if (h->root.type == bfd_link_hash_indirect)
12976 return TRUE;
12977
12978 if (!h->pointer_equality_needed)
12979 return TRUE;
12980
12981 if (h->def_regular)
12982 return TRUE;
12983
12984 info = inf;
12985 htab = ppc_hash_table (info);
12986 if (htab == NULL)
12987 return FALSE;
12988
12989 s = htab->global_entry;
12990 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
12991 if (pent->plt.offset != (bfd_vma) -1
12992 && pent->addend == 0)
12993 {
12994 bfd_byte *p;
12995 asection *plt;
12996 bfd_vma off;
12997
12998 p = s->contents + h->root.u.def.value;
12999 plt = htab->elf.splt;
13000 if (!htab->elf.dynamic_sections_created
13001 || h->dynindx == -1)
13002 plt = htab->elf.iplt;
13003 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
13004 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13005
13006 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13007 {
13008 info->callbacks->einfo
13009 (_("%P: linkage table error against `%T'\n"),
13010 h->root.root.string);
13011 bfd_set_error (bfd_error_bad_value);
13012 htab->stub_error = TRUE;
13013 }
13014
13015 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13016 if (htab->params->emit_stub_syms)
13017 {
13018 size_t len = strlen (h->root.root.string);
13019 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13020
13021 if (name == NULL)
13022 return FALSE;
13023
13024 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13025 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13026 if (h == NULL)
13027 return FALSE;
13028 if (h->root.type == bfd_link_hash_new)
13029 {
13030 h->root.type = bfd_link_hash_defined;
13031 h->root.u.def.section = s;
13032 h->root.u.def.value = p - s->contents;
13033 h->ref_regular = 1;
13034 h->def_regular = 1;
13035 h->ref_regular_nonweak = 1;
13036 h->forced_local = 1;
13037 h->non_elf = 0;
13038 h->root.linker_def = 1;
13039 }
13040 }
13041
13042 if (PPC_HA (off) != 0)
13043 {
13044 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13045 p += 4;
13046 }
13047 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13048 p += 4;
13049 bfd_put_32 (s->owner, MTCTR_R12, p);
13050 p += 4;
13051 bfd_put_32 (s->owner, BCTR, p);
13052 break;
13053 }
13054 return TRUE;
13055 }
13056
13057 /* Build all the stubs associated with the current output file.
13058 The stubs are kept in a hash table attached to the main linker
13059 hash table. This function is called via gldelf64ppc_finish. */
13060
13061 bfd_boolean
13062 ppc64_elf_build_stubs (struct bfd_link_info *info,
13063 char **stats)
13064 {
13065 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13066 struct map_stub *group;
13067 asection *stub_sec;
13068 bfd_byte *p;
13069 int stub_sec_count = 0;
13070
13071 if (htab == NULL)
13072 return FALSE;
13073
13074 /* Allocate memory to hold the linker stubs. */
13075 for (group = htab->group; group != NULL; group = group->next)
13076 if ((stub_sec = group->stub_sec) != NULL
13077 && stub_sec->size != 0)
13078 {
13079 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
13080 if (stub_sec->contents == NULL)
13081 return FALSE;
13082 stub_sec->size = 0;
13083 }
13084
13085 if (htab->glink != NULL && htab->glink->size != 0)
13086 {
13087 unsigned int indx;
13088 bfd_vma plt0;
13089
13090 /* Build the .glink plt call stub. */
13091 if (htab->params->emit_stub_syms)
13092 {
13093 struct elf_link_hash_entry *h;
13094 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
13095 TRUE, FALSE, FALSE);
13096 if (h == NULL)
13097 return FALSE;
13098 if (h->root.type == bfd_link_hash_new)
13099 {
13100 h->root.type = bfd_link_hash_defined;
13101 h->root.u.def.section = htab->glink;
13102 h->root.u.def.value = 8;
13103 h->ref_regular = 1;
13104 h->def_regular = 1;
13105 h->ref_regular_nonweak = 1;
13106 h->forced_local = 1;
13107 h->non_elf = 0;
13108 h->root.linker_def = 1;
13109 }
13110 }
13111 plt0 = (htab->elf.splt->output_section->vma
13112 + htab->elf.splt->output_offset
13113 - 16);
13114 if (info->emitrelocations)
13115 {
13116 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
13117 if (r == NULL)
13118 return FALSE;
13119 r->r_offset = (htab->glink->output_offset
13120 + htab->glink->output_section->vma);
13121 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
13122 r->r_addend = plt0;
13123 }
13124 p = htab->glink->contents;
13125 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
13126 bfd_put_64 (htab->glink->owner, plt0, p);
13127 p += 8;
13128 if (htab->opd_abi)
13129 {
13130 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
13131 p += 4;
13132 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13133 p += 4;
13134 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13135 p += 4;
13136 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13137 p += 4;
13138 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
13139 p += 4;
13140 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13141 p += 4;
13142 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13143 p += 4;
13144 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
13145 p += 4;
13146 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13147 p += 4;
13148 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
13149 p += 4;
13150 }
13151 else
13152 {
13153 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
13154 p += 4;
13155 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
13156 p += 4;
13157 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
13158 p += 4;
13159 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
13160 p += 4;
13161 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
13162 p += 4;
13163 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
13164 p += 4;
13165 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
13166 p += 4;
13167 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
13168 p += 4;
13169 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
13170 p += 4;
13171 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
13172 p += 4;
13173 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
13174 p += 4;
13175 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
13176 p += 4;
13177 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
13178 p += 4;
13179 }
13180 bfd_put_32 (htab->glink->owner, BCTR, p);
13181 p += 4;
13182 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
13183
13184 /* Build the .glink lazy link call stubs. */
13185 indx = 0;
13186 while (p < htab->glink->contents + htab->glink->size)
13187 {
13188 if (htab->opd_abi)
13189 {
13190 if (indx < 0x8000)
13191 {
13192 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
13193 p += 4;
13194 }
13195 else
13196 {
13197 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
13198 p += 4;
13199 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
13200 p);
13201 p += 4;
13202 }
13203 }
13204 bfd_put_32 (htab->glink->owner,
13205 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
13206 indx++;
13207 p += 4;
13208 }
13209 }
13210
13211 /* Build .glink global entry stubs. */
13212 if (htab->global_entry != NULL && htab->global_entry->size != 0)
13213 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs, info);
13214
13215 if (htab->brlt != NULL && htab->brlt->size != 0)
13216 {
13217 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
13218 htab->brlt->size);
13219 if (htab->brlt->contents == NULL)
13220 return FALSE;
13221 }
13222 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
13223 {
13224 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
13225 htab->relbrlt->size);
13226 if (htab->relbrlt->contents == NULL)
13227 return FALSE;
13228 }
13229
13230 /* Build the stubs as directed by the stub hash table. */
13231 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
13232
13233 for (group = htab->group; group != NULL; group = group->next)
13234 if (group->needs_save_res)
13235 {
13236 stub_sec = group->stub_sec;
13237 memcpy (stub_sec->contents + stub_sec->size, htab->sfpr->contents,
13238 htab->sfpr->size);
13239 if (htab->params->emit_stub_syms)
13240 {
13241 unsigned int i;
13242
13243 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
13244 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
13245 return FALSE;
13246 }
13247 stub_sec->size += htab->sfpr->size;
13248 }
13249
13250 if (htab->relbrlt != NULL)
13251 htab->relbrlt->reloc_count = 0;
13252
13253 if (htab->params->plt_stub_align != 0)
13254 for (group = htab->group; group != NULL; group = group->next)
13255 if ((stub_sec = group->stub_sec) != NULL)
13256 {
13257 int align = abs (htab->params->plt_stub_align);
13258 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
13259 }
13260
13261 for (group = htab->group; group != NULL; group = group->next)
13262 if ((stub_sec = group->stub_sec) != NULL)
13263 {
13264 stub_sec_count += 1;
13265 if (stub_sec->rawsize != stub_sec->size
13266 && (htab->stub_iteration <= STUB_SHRINK_ITER
13267 || stub_sec->rawsize < stub_sec->size))
13268 break;
13269 }
13270
13271 if (group != NULL)
13272 {
13273 htab->stub_error = TRUE;
13274 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
13275 }
13276
13277 if (htab->stub_error)
13278 return FALSE;
13279
13280 if (stats != NULL)
13281 {
13282 size_t len;
13283 *stats = bfd_malloc (500);
13284 if (*stats == NULL)
13285 return FALSE;
13286
13287 len = sprintf (*stats,
13288 ngettext ("linker stubs in %u group\n",
13289 "linker stubs in %u groups\n",
13290 stub_sec_count),
13291 stub_sec_count);
13292 sprintf (*stats + len, _(" branch %lu\n"
13293 " toc adjust %lu\n"
13294 " long branch %lu\n"
13295 " long toc adj %lu\n"
13296 " plt call %lu\n"
13297 " plt call toc %lu\n"
13298 " global entry %lu"),
13299 htab->stub_count[ppc_stub_long_branch - 1],
13300 htab->stub_count[ppc_stub_long_branch_r2off - 1],
13301 htab->stub_count[ppc_stub_plt_branch - 1],
13302 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
13303 htab->stub_count[ppc_stub_plt_call - 1],
13304 htab->stub_count[ppc_stub_plt_call_r2save - 1],
13305 htab->stub_count[ppc_stub_global_entry - 1]);
13306 }
13307 return TRUE;
13308 }
13309
13310 /* What to do when ld finds relocations against symbols defined in
13311 discarded sections. */
13312
13313 static unsigned int
13314 ppc64_elf_action_discarded (asection *sec)
13315 {
13316 if (strcmp (".opd", sec->name) == 0)
13317 return 0;
13318
13319 if (strcmp (".toc", sec->name) == 0)
13320 return 0;
13321
13322 if (strcmp (".toc1", sec->name) == 0)
13323 return 0;
13324
13325 return _bfd_elf_default_action_discarded (sec);
13326 }
13327
13328 /* The RELOCATE_SECTION function is called by the ELF backend linker
13329 to handle the relocations for a section.
13330
13331 The relocs are always passed as Rela structures; if the section
13332 actually uses Rel structures, the r_addend field will always be
13333 zero.
13334
13335 This function is responsible for adjust the section contents as
13336 necessary, and (if using Rela relocs and generating a
13337 relocatable output file) adjusting the reloc addend as
13338 necessary.
13339
13340 This function does not have to worry about setting the reloc
13341 address or the reloc symbol index.
13342
13343 LOCAL_SYMS is a pointer to the swapped in local symbols.
13344
13345 LOCAL_SECTIONS is an array giving the section in the input file
13346 corresponding to the st_shndx field of each local symbol.
13347
13348 The global hash table entry for the global symbols can be found
13349 via elf_sym_hashes (input_bfd).
13350
13351 When generating relocatable output, this function must handle
13352 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
13353 going to be the section symbol corresponding to the output
13354 section, which means that the addend must be adjusted
13355 accordingly. */
13356
13357 static bfd_boolean
13358 ppc64_elf_relocate_section (bfd *output_bfd,
13359 struct bfd_link_info *info,
13360 bfd *input_bfd,
13361 asection *input_section,
13362 bfd_byte *contents,
13363 Elf_Internal_Rela *relocs,
13364 Elf_Internal_Sym *local_syms,
13365 asection **local_sections)
13366 {
13367 struct ppc_link_hash_table *htab;
13368 Elf_Internal_Shdr *symtab_hdr;
13369 struct elf_link_hash_entry **sym_hashes;
13370 Elf_Internal_Rela *rel;
13371 Elf_Internal_Rela *wrel;
13372 Elf_Internal_Rela *relend;
13373 Elf_Internal_Rela outrel;
13374 bfd_byte *loc;
13375 struct got_entry **local_got_ents;
13376 bfd_vma TOCstart;
13377 bfd_boolean ret = TRUE;
13378 bfd_boolean is_opd;
13379 /* Assume 'at' branch hints. */
13380 bfd_boolean is_isa_v2 = TRUE;
13381 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
13382
13383 /* Initialize howto table if needed. */
13384 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
13385 ppc_howto_init ();
13386
13387 htab = ppc_hash_table (info);
13388 if (htab == NULL)
13389 return FALSE;
13390
13391 /* Don't relocate stub sections. */
13392 if (input_section->owner == htab->params->stub_bfd)
13393 return TRUE;
13394
13395 BFD_ASSERT (is_ppc64_elf (input_bfd));
13396
13397 local_got_ents = elf_local_got_ents (input_bfd);
13398 TOCstart = elf_gp (output_bfd);
13399 symtab_hdr = &elf_symtab_hdr (input_bfd);
13400 sym_hashes = elf_sym_hashes (input_bfd);
13401 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
13402
13403 rel = wrel = relocs;
13404 relend = relocs + input_section->reloc_count;
13405 for (; rel < relend; wrel++, rel++)
13406 {
13407 enum elf_ppc64_reloc_type r_type;
13408 bfd_vma addend;
13409 bfd_reloc_status_type r;
13410 Elf_Internal_Sym *sym;
13411 asection *sec;
13412 struct elf_link_hash_entry *h_elf;
13413 struct ppc_link_hash_entry *h;
13414 struct ppc_link_hash_entry *fdh;
13415 const char *sym_name;
13416 unsigned long r_symndx, toc_symndx;
13417 bfd_vma toc_addend;
13418 unsigned char tls_mask, tls_gd, tls_type;
13419 unsigned char sym_type;
13420 bfd_vma relocation;
13421 bfd_boolean unresolved_reloc;
13422 bfd_boolean warned;
13423 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
13424 unsigned int insn;
13425 unsigned int mask;
13426 struct ppc_stub_hash_entry *stub_entry;
13427 bfd_vma max_br_offset;
13428 bfd_vma from;
13429 Elf_Internal_Rela orig_rel;
13430 reloc_howto_type *howto;
13431 struct reloc_howto_struct alt_howto;
13432
13433 again:
13434 orig_rel = *rel;
13435
13436 r_type = ELF64_R_TYPE (rel->r_info);
13437 r_symndx = ELF64_R_SYM (rel->r_info);
13438
13439 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
13440 symbol of the previous ADDR64 reloc. The symbol gives us the
13441 proper TOC base to use. */
13442 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
13443 && wrel != relocs
13444 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
13445 && is_opd)
13446 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
13447
13448 sym = NULL;
13449 sec = NULL;
13450 h_elf = NULL;
13451 sym_name = NULL;
13452 unresolved_reloc = FALSE;
13453 warned = FALSE;
13454
13455 if (r_symndx < symtab_hdr->sh_info)
13456 {
13457 /* It's a local symbol. */
13458 struct _opd_sec_data *opd;
13459
13460 sym = local_syms + r_symndx;
13461 sec = local_sections[r_symndx];
13462 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
13463 sym_type = ELF64_ST_TYPE (sym->st_info);
13464 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
13465 opd = get_opd_info (sec);
13466 if (opd != NULL && opd->adjust != NULL)
13467 {
13468 long adjust = opd->adjust[OPD_NDX (sym->st_value
13469 + rel->r_addend)];
13470 if (adjust == -1)
13471 relocation = 0;
13472 else
13473 {
13474 /* If this is a relocation against the opd section sym
13475 and we have edited .opd, adjust the reloc addend so
13476 that ld -r and ld --emit-relocs output is correct.
13477 If it is a reloc against some other .opd symbol,
13478 then the symbol value will be adjusted later. */
13479 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
13480 rel->r_addend += adjust;
13481 else
13482 relocation += adjust;
13483 }
13484 }
13485 }
13486 else
13487 {
13488 bfd_boolean ignored;
13489
13490 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
13491 r_symndx, symtab_hdr, sym_hashes,
13492 h_elf, sec, relocation,
13493 unresolved_reloc, warned, ignored);
13494 sym_name = h_elf->root.root.string;
13495 sym_type = h_elf->type;
13496 if (sec != NULL
13497 && sec->owner == output_bfd
13498 && strcmp (sec->name, ".opd") == 0)
13499 {
13500 /* This is a symbol defined in a linker script. All
13501 such are defined in output sections, even those
13502 defined by simple assignment from a symbol defined in
13503 an input section. Transfer the symbol to an
13504 appropriate input .opd section, so that a branch to
13505 this symbol will be mapped to the location specified
13506 by the opd entry. */
13507 struct bfd_link_order *lo;
13508 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
13509 if (lo->type == bfd_indirect_link_order)
13510 {
13511 asection *isec = lo->u.indirect.section;
13512 if (h_elf->root.u.def.value >= isec->output_offset
13513 && h_elf->root.u.def.value < (isec->output_offset
13514 + isec->size))
13515 {
13516 h_elf->root.u.def.value -= isec->output_offset;
13517 h_elf->root.u.def.section = isec;
13518 sec = isec;
13519 break;
13520 }
13521 }
13522 }
13523 }
13524 h = (struct ppc_link_hash_entry *) h_elf;
13525
13526 if (sec != NULL && discarded_section (sec))
13527 {
13528 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
13529 input_bfd, input_section,
13530 contents + rel->r_offset);
13531 wrel->r_offset = rel->r_offset;
13532 wrel->r_info = 0;
13533 wrel->r_addend = 0;
13534
13535 /* For ld -r, remove relocations in debug sections against
13536 symbols defined in discarded sections. Not done for
13537 non-debug to preserve relocs in .eh_frame which the
13538 eh_frame editing code expects to be present. */
13539 if (bfd_link_relocatable (info)
13540 && (input_section->flags & SEC_DEBUGGING))
13541 wrel--;
13542
13543 continue;
13544 }
13545
13546 if (bfd_link_relocatable (info))
13547 goto copy_reloc;
13548
13549 if (h != NULL && &h->elf == htab->elf.hgot)
13550 {
13551 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13552 sec = bfd_abs_section_ptr;
13553 unresolved_reloc = FALSE;
13554 }
13555
13556 /* TLS optimizations. Replace instruction sequences and relocs
13557 based on information we collected in tls_optimize. We edit
13558 RELOCS so that --emit-relocs will output something sensible
13559 for the final instruction stream. */
13560 tls_mask = 0;
13561 tls_gd = 0;
13562 toc_symndx = 0;
13563 if (h != NULL)
13564 tls_mask = h->tls_mask;
13565 else if (local_got_ents != NULL)
13566 {
13567 struct plt_entry **local_plt = (struct plt_entry **)
13568 (local_got_ents + symtab_hdr->sh_info);
13569 unsigned char *lgot_masks = (unsigned char *)
13570 (local_plt + symtab_hdr->sh_info);
13571 tls_mask = lgot_masks[r_symndx];
13572 }
13573 if (tls_mask == 0
13574 && (r_type == R_PPC64_TLS
13575 || r_type == R_PPC64_TLSGD
13576 || r_type == R_PPC64_TLSLD))
13577 {
13578 /* Check for toc tls entries. */
13579 unsigned char *toc_tls;
13580
13581 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13582 &local_syms, rel, input_bfd))
13583 return FALSE;
13584
13585 if (toc_tls)
13586 tls_mask = *toc_tls;
13587 }
13588
13589 /* Check that tls relocs are used with tls syms, and non-tls
13590 relocs are used with non-tls syms. */
13591 if (r_symndx != STN_UNDEF
13592 && r_type != R_PPC64_NONE
13593 && (h == NULL
13594 || h->elf.root.type == bfd_link_hash_defined
13595 || h->elf.root.type == bfd_link_hash_defweak)
13596 && (IS_PPC64_TLS_RELOC (r_type)
13597 != (sym_type == STT_TLS
13598 || (sym_type == STT_SECTION
13599 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
13600 {
13601 if (tls_mask != 0
13602 && (r_type == R_PPC64_TLS
13603 || r_type == R_PPC64_TLSGD
13604 || r_type == R_PPC64_TLSLD))
13605 /* R_PPC64_TLS is OK against a symbol in the TOC. */
13606 ;
13607 else
13608 info->callbacks->einfo
13609 (!IS_PPC64_TLS_RELOC (r_type)
13610 /* xgettext:c-format */
13611 ? _("%H: %s used with TLS symbol `%T'\n")
13612 /* xgettext:c-format */
13613 : _("%H: %s used with non-TLS symbol `%T'\n"),
13614 input_bfd, input_section, rel->r_offset,
13615 ppc64_elf_howto_table[r_type]->name,
13616 sym_name);
13617 }
13618
13619 /* Ensure reloc mapping code below stays sane. */
13620 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
13621 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
13622 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
13623 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
13624 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
13625 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
13626 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
13627 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
13628 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
13629 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
13630 abort ();
13631
13632 switch (r_type)
13633 {
13634 default:
13635 break;
13636
13637 case R_PPC64_LO_DS_OPT:
13638 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
13639 if ((insn & (0x3f << 26)) != 58u << 26)
13640 abort ();
13641 insn += (14u << 26) - (58u << 26);
13642 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
13643 r_type = R_PPC64_TOC16_LO;
13644 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13645 break;
13646
13647 case R_PPC64_TOC16:
13648 case R_PPC64_TOC16_LO:
13649 case R_PPC64_TOC16_DS:
13650 case R_PPC64_TOC16_LO_DS:
13651 {
13652 /* Check for toc tls entries. */
13653 unsigned char *toc_tls;
13654 int retval;
13655
13656 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
13657 &local_syms, rel, input_bfd);
13658 if (retval == 0)
13659 return FALSE;
13660
13661 if (toc_tls)
13662 {
13663 tls_mask = *toc_tls;
13664 if (r_type == R_PPC64_TOC16_DS
13665 || r_type == R_PPC64_TOC16_LO_DS)
13666 {
13667 if (tls_mask != 0
13668 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
13669 goto toctprel;
13670 }
13671 else
13672 {
13673 /* If we found a GD reloc pair, then we might be
13674 doing a GD->IE transition. */
13675 if (retval == 2)
13676 {
13677 tls_gd = TLS_TPRELGD;
13678 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13679 goto tls_ldgd_opt;
13680 }
13681 else if (retval == 3)
13682 {
13683 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13684 goto tls_ldgd_opt;
13685 }
13686 }
13687 }
13688 }
13689 break;
13690
13691 case R_PPC64_GOT_TPREL16_HI:
13692 case R_PPC64_GOT_TPREL16_HA:
13693 if (tls_mask != 0
13694 && (tls_mask & TLS_TPREL) == 0)
13695 {
13696 rel->r_offset -= d_offset;
13697 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13698 r_type = R_PPC64_NONE;
13699 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13700 }
13701 break;
13702
13703 case R_PPC64_GOT_TPREL16_DS:
13704 case R_PPC64_GOT_TPREL16_LO_DS:
13705 if (tls_mask != 0
13706 && (tls_mask & TLS_TPREL) == 0)
13707 {
13708 toctprel:
13709 insn = bfd_get_32 (input_bfd,
13710 contents + rel->r_offset - d_offset);
13711 insn &= 31 << 21;
13712 insn |= 0x3c0d0000; /* addis 0,13,0 */
13713 bfd_put_32 (input_bfd, insn,
13714 contents + rel->r_offset - d_offset);
13715 r_type = R_PPC64_TPREL16_HA;
13716 if (toc_symndx != 0)
13717 {
13718 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13719 rel->r_addend = toc_addend;
13720 /* We changed the symbol. Start over in order to
13721 get h, sym, sec etc. right. */
13722 goto again;
13723 }
13724 else
13725 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13726 }
13727 break;
13728
13729 case R_PPC64_TLS:
13730 if (tls_mask != 0
13731 && (tls_mask & TLS_TPREL) == 0)
13732 {
13733 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13734 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
13735 if (insn == 0)
13736 abort ();
13737 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
13738 /* Was PPC64_TLS which sits on insn boundary, now
13739 PPC64_TPREL16_LO which is at low-order half-word. */
13740 rel->r_offset += d_offset;
13741 r_type = R_PPC64_TPREL16_LO;
13742 if (toc_symndx != 0)
13743 {
13744 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
13745 rel->r_addend = toc_addend;
13746 /* We changed the symbol. Start over in order to
13747 get h, sym, sec etc. right. */
13748 goto again;
13749 }
13750 else
13751 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13752 }
13753 break;
13754
13755 case R_PPC64_GOT_TLSGD16_HI:
13756 case R_PPC64_GOT_TLSGD16_HA:
13757 tls_gd = TLS_TPRELGD;
13758 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13759 goto tls_gdld_hi;
13760 break;
13761
13762 case R_PPC64_GOT_TLSLD16_HI:
13763 case R_PPC64_GOT_TLSLD16_HA:
13764 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13765 {
13766 tls_gdld_hi:
13767 if ((tls_mask & tls_gd) != 0)
13768 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13769 + R_PPC64_GOT_TPREL16_DS);
13770 else
13771 {
13772 rel->r_offset -= d_offset;
13773 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
13774 r_type = R_PPC64_NONE;
13775 }
13776 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13777 }
13778 break;
13779
13780 case R_PPC64_GOT_TLSGD16:
13781 case R_PPC64_GOT_TLSGD16_LO:
13782 tls_gd = TLS_TPRELGD;
13783 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13784 goto tls_ldgd_opt;
13785 break;
13786
13787 case R_PPC64_GOT_TLSLD16:
13788 case R_PPC64_GOT_TLSLD16_LO:
13789 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13790 {
13791 unsigned int insn1, insn2;
13792 bfd_vma offset;
13793
13794 tls_ldgd_opt:
13795 offset = (bfd_vma) -1;
13796 /* If not using the newer R_PPC64_TLSGD/LD to mark
13797 __tls_get_addr calls, we must trust that the call
13798 stays with its arg setup insns, ie. that the next
13799 reloc is the __tls_get_addr call associated with
13800 the current reloc. Edit both insns. */
13801 if (input_section->has_tls_get_addr_call
13802 && rel + 1 < relend
13803 && branch_reloc_hash_match (input_bfd, rel + 1,
13804 htab->tls_get_addr,
13805 htab->tls_get_addr_fd))
13806 offset = rel[1].r_offset;
13807 /* We read the low GOT_TLS (or TOC16) insn because we
13808 need to keep the destination reg. It may be
13809 something other than the usual r3, and moved to r3
13810 before the call by intervening code. */
13811 insn1 = bfd_get_32 (input_bfd,
13812 contents + rel->r_offset - d_offset);
13813 if ((tls_mask & tls_gd) != 0)
13814 {
13815 /* IE */
13816 insn1 &= (0x1f << 21) | (0x1f << 16);
13817 insn1 |= 58 << 26; /* ld */
13818 insn2 = 0x7c636a14; /* add 3,3,13 */
13819 if (offset != (bfd_vma) -1)
13820 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13821 if ((tls_mask & TLS_EXPLICIT) == 0)
13822 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13823 + R_PPC64_GOT_TPREL16_DS);
13824 else
13825 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13826 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13827 }
13828 else
13829 {
13830 /* LE */
13831 insn1 &= 0x1f << 21;
13832 insn1 |= 0x3c0d0000; /* addis r,13,0 */
13833 insn2 = 0x38630000; /* addi 3,3,0 */
13834 if (tls_gd == 0)
13835 {
13836 /* Was an LD reloc. */
13837 if (toc_symndx)
13838 sec = local_sections[toc_symndx];
13839 for (r_symndx = 0;
13840 r_symndx < symtab_hdr->sh_info;
13841 r_symndx++)
13842 if (local_sections[r_symndx] == sec)
13843 break;
13844 if (r_symndx >= symtab_hdr->sh_info)
13845 r_symndx = STN_UNDEF;
13846 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13847 if (r_symndx != STN_UNDEF)
13848 rel->r_addend -= (local_syms[r_symndx].st_value
13849 + sec->output_offset
13850 + sec->output_section->vma);
13851 }
13852 else if (toc_symndx != 0)
13853 {
13854 r_symndx = toc_symndx;
13855 rel->r_addend = toc_addend;
13856 }
13857 r_type = R_PPC64_TPREL16_HA;
13858 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13859 if (offset != (bfd_vma) -1)
13860 {
13861 rel[1].r_info = ELF64_R_INFO (r_symndx,
13862 R_PPC64_TPREL16_LO);
13863 rel[1].r_offset = offset + d_offset;
13864 rel[1].r_addend = rel->r_addend;
13865 }
13866 }
13867 bfd_put_32 (input_bfd, insn1,
13868 contents + rel->r_offset - d_offset);
13869 if (offset != (bfd_vma) -1)
13870 bfd_put_32 (input_bfd, insn2, contents + offset);
13871 if ((tls_mask & tls_gd) == 0
13872 && (tls_gd == 0 || toc_symndx != 0))
13873 {
13874 /* We changed the symbol. Start over in order
13875 to get h, sym, sec etc. right. */
13876 goto again;
13877 }
13878 }
13879 break;
13880
13881 case R_PPC64_TLSGD:
13882 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13883 {
13884 unsigned int insn2;
13885 bfd_vma offset = rel->r_offset;
13886
13887 if ((tls_mask & TLS_TPRELGD) != 0)
13888 {
13889 /* IE */
13890 r_type = R_PPC64_NONE;
13891 insn2 = 0x7c636a14; /* add 3,3,13 */
13892 }
13893 else
13894 {
13895 /* LE */
13896 if (toc_symndx != 0)
13897 {
13898 r_symndx = toc_symndx;
13899 rel->r_addend = toc_addend;
13900 }
13901 r_type = R_PPC64_TPREL16_LO;
13902 rel->r_offset = offset + d_offset;
13903 insn2 = 0x38630000; /* addi 3,3,0 */
13904 }
13905 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13906 /* Zap the reloc on the _tls_get_addr call too. */
13907 BFD_ASSERT (offset == rel[1].r_offset);
13908 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13909 bfd_put_32 (input_bfd, insn2, contents + offset);
13910 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13911 goto again;
13912 }
13913 break;
13914
13915 case R_PPC64_TLSLD:
13916 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13917 {
13918 unsigned int insn2;
13919 bfd_vma offset = rel->r_offset;
13920
13921 if (toc_symndx)
13922 sec = local_sections[toc_symndx];
13923 for (r_symndx = 0;
13924 r_symndx < symtab_hdr->sh_info;
13925 r_symndx++)
13926 if (local_sections[r_symndx] == sec)
13927 break;
13928 if (r_symndx >= symtab_hdr->sh_info)
13929 r_symndx = STN_UNDEF;
13930 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13931 if (r_symndx != STN_UNDEF)
13932 rel->r_addend -= (local_syms[r_symndx].st_value
13933 + sec->output_offset
13934 + sec->output_section->vma);
13935
13936 r_type = R_PPC64_TPREL16_LO;
13937 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13938 rel->r_offset = offset + d_offset;
13939 /* Zap the reloc on the _tls_get_addr call too. */
13940 BFD_ASSERT (offset == rel[1].r_offset);
13941 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13942 insn2 = 0x38630000; /* addi 3,3,0 */
13943 bfd_put_32 (input_bfd, insn2, contents + offset);
13944 goto again;
13945 }
13946 break;
13947
13948 case R_PPC64_DTPMOD64:
13949 if (rel + 1 < relend
13950 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13951 && rel[1].r_offset == rel->r_offset + 8)
13952 {
13953 if ((tls_mask & TLS_GD) == 0)
13954 {
13955 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13956 if ((tls_mask & TLS_TPRELGD) != 0)
13957 r_type = R_PPC64_TPREL64;
13958 else
13959 {
13960 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13961 r_type = R_PPC64_NONE;
13962 }
13963 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13964 }
13965 }
13966 else
13967 {
13968 if ((tls_mask & TLS_LD) == 0)
13969 {
13970 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13971 r_type = R_PPC64_NONE;
13972 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13973 }
13974 }
13975 break;
13976
13977 case R_PPC64_TPREL64:
13978 if ((tls_mask & TLS_TPREL) == 0)
13979 {
13980 r_type = R_PPC64_NONE;
13981 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13982 }
13983 break;
13984
13985 case R_PPC64_ENTRY:
13986 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
13987 if (!bfd_link_pic (info)
13988 && !info->traditional_format
13989 && relocation + 0x80008000 <= 0xffffffff)
13990 {
13991 unsigned int insn1, insn2;
13992
13993 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
13994 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
13995 if ((insn1 & ~0xfffc) == LD_R2_0R12
13996 && insn2 == ADD_R2_R2_R12)
13997 {
13998 bfd_put_32 (input_bfd,
13999 LIS_R2 + PPC_HA (relocation),
14000 contents + rel->r_offset);
14001 bfd_put_32 (input_bfd,
14002 ADDI_R2_R2 + PPC_LO (relocation),
14003 contents + rel->r_offset + 4);
14004 }
14005 }
14006 else
14007 {
14008 relocation -= (rel->r_offset
14009 + input_section->output_offset
14010 + input_section->output_section->vma);
14011 if (relocation + 0x80008000 <= 0xffffffff)
14012 {
14013 unsigned int insn1, insn2;
14014
14015 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14016 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14017 if ((insn1 & ~0xfffc) == LD_R2_0R12
14018 && insn2 == ADD_R2_R2_R12)
14019 {
14020 bfd_put_32 (input_bfd,
14021 ADDIS_R2_R12 + PPC_HA (relocation),
14022 contents + rel->r_offset);
14023 bfd_put_32 (input_bfd,
14024 ADDI_R2_R2 + PPC_LO (relocation),
14025 contents + rel->r_offset + 4);
14026 }
14027 }
14028 }
14029 break;
14030
14031 case R_PPC64_REL16_HA:
14032 /* If we are generating a non-PIC executable, edit
14033 . 0: addis 2,12,.TOC.-0b@ha
14034 . addi 2,2,.TOC.-0b@l
14035 used by ELFv2 global entry points to set up r2, to
14036 . lis 2,.TOC.@ha
14037 . addi 2,2,.TOC.@l
14038 if .TOC. is in range. */
14039 if (!bfd_link_pic (info)
14040 && !info->traditional_format
14041 && !htab->opd_abi
14042 && rel->r_addend == d_offset
14043 && h != NULL && &h->elf == htab->elf.hgot
14044 && rel + 1 < relend
14045 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
14046 && rel[1].r_offset == rel->r_offset + 4
14047 && rel[1].r_addend == rel->r_addend + 4
14048 && relocation + 0x80008000 <= 0xffffffff)
14049 {
14050 unsigned int insn1, insn2;
14051 bfd_vma offset = rel->r_offset - d_offset;
14052 insn1 = bfd_get_32 (input_bfd, contents + offset);
14053 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
14054 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
14055 && (insn2 & 0xffff0000) == ADDI_R2_R2)
14056 {
14057 r_type = R_PPC64_ADDR16_HA;
14058 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14059 rel->r_addend -= d_offset;
14060 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
14061 rel[1].r_addend -= d_offset + 4;
14062 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
14063 }
14064 }
14065 break;
14066 }
14067
14068 /* Handle other relocations that tweak non-addend part of insn. */
14069 insn = 0;
14070 max_br_offset = 1 << 25;
14071 addend = rel->r_addend;
14072 reloc_dest = DEST_NORMAL;
14073 switch (r_type)
14074 {
14075 default:
14076 break;
14077
14078 case R_PPC64_TOCSAVE:
14079 if (relocation + addend == (rel->r_offset
14080 + input_section->output_offset
14081 + input_section->output_section->vma)
14082 && tocsave_find (htab, NO_INSERT,
14083 &local_syms, rel, input_bfd))
14084 {
14085 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14086 if (insn == NOP
14087 || insn == CROR_151515 || insn == CROR_313131)
14088 bfd_put_32 (input_bfd,
14089 STD_R2_0R1 + STK_TOC (htab),
14090 contents + rel->r_offset);
14091 }
14092 break;
14093
14094 /* Branch taken prediction relocations. */
14095 case R_PPC64_ADDR14_BRTAKEN:
14096 case R_PPC64_REL14_BRTAKEN:
14097 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
14098 /* Fall through. */
14099
14100 /* Branch not taken prediction relocations. */
14101 case R_PPC64_ADDR14_BRNTAKEN:
14102 case R_PPC64_REL14_BRNTAKEN:
14103 insn |= bfd_get_32 (input_bfd,
14104 contents + rel->r_offset) & ~(0x01 << 21);
14105 /* Fall through. */
14106
14107 case R_PPC64_REL14:
14108 max_br_offset = 1 << 15;
14109 /* Fall through. */
14110
14111 case R_PPC64_REL24:
14112 /* Calls to functions with a different TOC, such as calls to
14113 shared objects, need to alter the TOC pointer. This is
14114 done using a linkage stub. A REL24 branching to these
14115 linkage stubs needs to be followed by a nop, as the nop
14116 will be replaced with an instruction to restore the TOC
14117 base pointer. */
14118 fdh = h;
14119 if (h != NULL
14120 && h->oh != NULL
14121 && h->oh->is_func_descriptor)
14122 fdh = ppc_follow_link (h->oh);
14123 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
14124 htab);
14125 if (stub_entry != NULL
14126 && (stub_entry->stub_type == ppc_stub_plt_call
14127 || stub_entry->stub_type == ppc_stub_plt_call_r2save
14128 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
14129 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
14130 {
14131 bfd_boolean can_plt_call = FALSE;
14132
14133 if (stub_entry->stub_type == ppc_stub_plt_call
14134 && !htab->opd_abi
14135 && htab->params->plt_localentry0 != 0
14136 && is_elfv2_localentry0 (&h->elf))
14137 {
14138 /* The function doesn't use or change r2. */
14139 can_plt_call = TRUE;
14140 }
14141
14142 /* All of these stubs may modify r2, so there must be a
14143 branch and link followed by a nop. The nop is
14144 replaced by an insn to restore r2. */
14145 else if (rel->r_offset + 8 <= input_section->size)
14146 {
14147 unsigned long br;
14148
14149 br = bfd_get_32 (input_bfd,
14150 contents + rel->r_offset);
14151 if ((br & 1) != 0)
14152 {
14153 unsigned long nop;
14154
14155 nop = bfd_get_32 (input_bfd,
14156 contents + rel->r_offset + 4);
14157 if (nop == NOP
14158 || nop == CROR_151515 || nop == CROR_313131)
14159 {
14160 if (h != NULL
14161 && (h == htab->tls_get_addr_fd
14162 || h == htab->tls_get_addr)
14163 && htab->params->tls_get_addr_opt)
14164 {
14165 /* Special stub used, leave nop alone. */
14166 }
14167 else
14168 bfd_put_32 (input_bfd,
14169 LD_R2_0R1 + STK_TOC (htab),
14170 contents + rel->r_offset + 4);
14171 can_plt_call = TRUE;
14172 }
14173 }
14174 }
14175
14176 if (!can_plt_call && h != NULL)
14177 {
14178 const char *name = h->elf.root.root.string;
14179
14180 if (*name == '.')
14181 ++name;
14182
14183 if (strncmp (name, "__libc_start_main", 17) == 0
14184 && (name[17] == 0 || name[17] == '@'))
14185 {
14186 /* Allow crt1 branch to go via a toc adjusting
14187 stub. Other calls that never return could do
14188 the same, if we could detect such. */
14189 can_plt_call = TRUE;
14190 }
14191 }
14192
14193 if (!can_plt_call)
14194 {
14195 /* g++ as of 20130507 emits self-calls without a
14196 following nop. This is arguably wrong since we
14197 have conflicting information. On the one hand a
14198 global symbol and on the other a local call
14199 sequence, but don't error for this special case.
14200 It isn't possible to cheaply verify we have
14201 exactly such a call. Allow all calls to the same
14202 section. */
14203 asection *code_sec = sec;
14204
14205 if (get_opd_info (sec) != NULL)
14206 {
14207 bfd_vma off = (relocation + addend
14208 - sec->output_section->vma
14209 - sec->output_offset);
14210
14211 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
14212 }
14213 if (code_sec == input_section)
14214 can_plt_call = TRUE;
14215 }
14216
14217 if (!can_plt_call)
14218 {
14219 if (stub_entry->stub_type == ppc_stub_plt_call
14220 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14221 info->callbacks->einfo
14222 /* xgettext:c-format */
14223 (_("%H: call to `%T' lacks nop, can't restore toc; "
14224 "recompile with -fPIC\n"),
14225 input_bfd, input_section, rel->r_offset, sym_name);
14226 else
14227 info->callbacks->einfo
14228 /* xgettext:c-format */
14229 (_("%H: call to `%T' lacks nop, can't restore toc; "
14230 "(-mcmodel=small toc adjust stub)\n"),
14231 input_bfd, input_section, rel->r_offset, sym_name);
14232
14233 bfd_set_error (bfd_error_bad_value);
14234 ret = FALSE;
14235 }
14236
14237 if (can_plt_call
14238 && (stub_entry->stub_type == ppc_stub_plt_call
14239 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
14240 unresolved_reloc = FALSE;
14241 }
14242
14243 if ((stub_entry == NULL
14244 || stub_entry->stub_type == ppc_stub_long_branch
14245 || stub_entry->stub_type == ppc_stub_plt_branch)
14246 && get_opd_info (sec) != NULL)
14247 {
14248 /* The branch destination is the value of the opd entry. */
14249 bfd_vma off = (relocation + addend
14250 - sec->output_section->vma
14251 - sec->output_offset);
14252 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
14253 if (dest != (bfd_vma) -1)
14254 {
14255 relocation = dest;
14256 addend = 0;
14257 reloc_dest = DEST_OPD;
14258 }
14259 }
14260
14261 /* If the branch is out of reach we ought to have a long
14262 branch stub. */
14263 from = (rel->r_offset
14264 + input_section->output_offset
14265 + input_section->output_section->vma);
14266
14267 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
14268 ? fdh->elf.other
14269 : sym->st_other);
14270
14271 if (stub_entry != NULL
14272 && (stub_entry->stub_type == ppc_stub_long_branch
14273 || stub_entry->stub_type == ppc_stub_plt_branch)
14274 && (r_type == R_PPC64_ADDR14_BRTAKEN
14275 || r_type == R_PPC64_ADDR14_BRNTAKEN
14276 || (relocation + addend - from + max_br_offset
14277 < 2 * max_br_offset)))
14278 /* Don't use the stub if this branch is in range. */
14279 stub_entry = NULL;
14280
14281 if (stub_entry != NULL)
14282 {
14283 /* Munge up the value and addend so that we call the stub
14284 rather than the procedure directly. */
14285 asection *stub_sec = stub_entry->group->stub_sec;
14286
14287 if (stub_entry->stub_type == ppc_stub_save_res)
14288 relocation += (stub_sec->output_offset
14289 + stub_sec->output_section->vma
14290 + stub_sec->size - htab->sfpr->size
14291 - htab->sfpr->output_offset
14292 - htab->sfpr->output_section->vma);
14293 else
14294 relocation = (stub_entry->stub_offset
14295 + stub_sec->output_offset
14296 + stub_sec->output_section->vma);
14297 addend = 0;
14298 reloc_dest = DEST_STUB;
14299
14300 if ((stub_entry->stub_type == ppc_stub_plt_call
14301 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14302 && (ALWAYS_EMIT_R2SAVE
14303 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
14304 && rel + 1 < relend
14305 && rel[1].r_offset == rel->r_offset + 4
14306 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
14307 relocation += 4;
14308 }
14309
14310 if (insn != 0)
14311 {
14312 if (is_isa_v2)
14313 {
14314 /* Set 'a' bit. This is 0b00010 in BO field for branch
14315 on CR(BI) insns (BO == 001at or 011at), and 0b01000
14316 for branch on CTR insns (BO == 1a00t or 1a01t). */
14317 if ((insn & (0x14 << 21)) == (0x04 << 21))
14318 insn |= 0x02 << 21;
14319 else if ((insn & (0x14 << 21)) == (0x10 << 21))
14320 insn |= 0x08 << 21;
14321 else
14322 break;
14323 }
14324 else
14325 {
14326 /* Invert 'y' bit if not the default. */
14327 if ((bfd_signed_vma) (relocation + addend - from) < 0)
14328 insn ^= 0x01 << 21;
14329 }
14330
14331 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14332 }
14333
14334 /* NOP out calls to undefined weak functions.
14335 We can thus call a weak function without first
14336 checking whether the function is defined. */
14337 else if (h != NULL
14338 && h->elf.root.type == bfd_link_hash_undefweak
14339 && h->elf.dynindx == -1
14340 && r_type == R_PPC64_REL24
14341 && relocation == 0
14342 && addend == 0)
14343 {
14344 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14345 goto copy_reloc;
14346 }
14347 break;
14348 }
14349
14350 /* Set `addend'. */
14351 tls_type = 0;
14352 switch (r_type)
14353 {
14354 default:
14355 info->callbacks->einfo
14356 /* xgettext:c-format */
14357 (_("%P: %B: unknown relocation type %d for `%T'\n"),
14358 input_bfd, (int) r_type, sym_name);
14359
14360 bfd_set_error (bfd_error_bad_value);
14361 ret = FALSE;
14362 goto copy_reloc;
14363
14364 case R_PPC64_NONE:
14365 case R_PPC64_TLS:
14366 case R_PPC64_TLSGD:
14367 case R_PPC64_TLSLD:
14368 case R_PPC64_TOCSAVE:
14369 case R_PPC64_GNU_VTINHERIT:
14370 case R_PPC64_GNU_VTENTRY:
14371 case R_PPC64_ENTRY:
14372 goto copy_reloc;
14373
14374 /* GOT16 relocations. Like an ADDR16 using the symbol's
14375 address in the GOT as relocation value instead of the
14376 symbol's value itself. Also, create a GOT entry for the
14377 symbol and put the symbol value there. */
14378 case R_PPC64_GOT_TLSGD16:
14379 case R_PPC64_GOT_TLSGD16_LO:
14380 case R_PPC64_GOT_TLSGD16_HI:
14381 case R_PPC64_GOT_TLSGD16_HA:
14382 tls_type = TLS_TLS | TLS_GD;
14383 goto dogot;
14384
14385 case R_PPC64_GOT_TLSLD16:
14386 case R_PPC64_GOT_TLSLD16_LO:
14387 case R_PPC64_GOT_TLSLD16_HI:
14388 case R_PPC64_GOT_TLSLD16_HA:
14389 tls_type = TLS_TLS | TLS_LD;
14390 goto dogot;
14391
14392 case R_PPC64_GOT_TPREL16_DS:
14393 case R_PPC64_GOT_TPREL16_LO_DS:
14394 case R_PPC64_GOT_TPREL16_HI:
14395 case R_PPC64_GOT_TPREL16_HA:
14396 tls_type = TLS_TLS | TLS_TPREL;
14397 goto dogot;
14398
14399 case R_PPC64_GOT_DTPREL16_DS:
14400 case R_PPC64_GOT_DTPREL16_LO_DS:
14401 case R_PPC64_GOT_DTPREL16_HI:
14402 case R_PPC64_GOT_DTPREL16_HA:
14403 tls_type = TLS_TLS | TLS_DTPREL;
14404 goto dogot;
14405
14406 case R_PPC64_GOT16:
14407 case R_PPC64_GOT16_LO:
14408 case R_PPC64_GOT16_HI:
14409 case R_PPC64_GOT16_HA:
14410 case R_PPC64_GOT16_DS:
14411 case R_PPC64_GOT16_LO_DS:
14412 dogot:
14413 {
14414 /* Relocation is to the entry for this symbol in the global
14415 offset table. */
14416 asection *got;
14417 bfd_vma *offp;
14418 bfd_vma off;
14419 unsigned long indx = 0;
14420 struct got_entry *ent;
14421
14422 if (tls_type == (TLS_TLS | TLS_LD)
14423 && (h == NULL
14424 || !h->elf.def_dynamic))
14425 ent = ppc64_tlsld_got (input_bfd);
14426 else
14427 {
14428 if (h != NULL)
14429 {
14430 if (!htab->elf.dynamic_sections_created
14431 || h->elf.dynindx == -1
14432 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14433 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
14434 /* This is actually a static link, or it is a
14435 -Bsymbolic link and the symbol is defined
14436 locally, or the symbol was forced to be local
14437 because of a version file. */
14438 ;
14439 else
14440 {
14441 indx = h->elf.dynindx;
14442 unresolved_reloc = FALSE;
14443 }
14444 ent = h->elf.got.glist;
14445 }
14446 else
14447 {
14448 if (local_got_ents == NULL)
14449 abort ();
14450 ent = local_got_ents[r_symndx];
14451 }
14452
14453 for (; ent != NULL; ent = ent->next)
14454 if (ent->addend == orig_rel.r_addend
14455 && ent->owner == input_bfd
14456 && ent->tls_type == tls_type)
14457 break;
14458 }
14459
14460 if (ent == NULL)
14461 abort ();
14462 if (ent->is_indirect)
14463 ent = ent->got.ent;
14464 offp = &ent->got.offset;
14465 got = ppc64_elf_tdata (ent->owner)->got;
14466 if (got == NULL)
14467 abort ();
14468
14469 /* The offset must always be a multiple of 8. We use the
14470 least significant bit to record whether we have already
14471 processed this entry. */
14472 off = *offp;
14473 if ((off & 1) != 0)
14474 off &= ~1;
14475 else
14476 {
14477 /* Generate relocs for the dynamic linker, except in
14478 the case of TLSLD where we'll use one entry per
14479 module. */
14480 asection *relgot;
14481 bfd_boolean ifunc;
14482
14483 *offp = off | 1;
14484 relgot = NULL;
14485 ifunc = (h != NULL
14486 ? h->elf.type == STT_GNU_IFUNC
14487 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
14488 if (ifunc)
14489 {
14490 relgot = htab->elf.irelplt;
14491 if (indx == 0)
14492 htab->local_ifunc_resolver = 1;
14493 else if (is_static_defined (&h->elf))
14494 htab->maybe_local_ifunc_resolver = 1;
14495 }
14496 else if (indx != 0
14497 || (bfd_link_pic (info)
14498 && (h == NULL
14499 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
14500 || (tls_type == (TLS_TLS | TLS_LD)
14501 && !h->elf.def_dynamic))
14502 && !(tls_type == (TLS_TLS | TLS_TPREL)
14503 && bfd_link_executable (info)
14504 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
14505 relgot = ppc64_elf_tdata (ent->owner)->relgot;
14506 if (relgot != NULL)
14507 {
14508 outrel.r_offset = (got->output_section->vma
14509 + got->output_offset
14510 + off);
14511 outrel.r_addend = addend;
14512 if (tls_type & (TLS_LD | TLS_GD))
14513 {
14514 outrel.r_addend = 0;
14515 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
14516 if (tls_type == (TLS_TLS | TLS_GD))
14517 {
14518 loc = relgot->contents;
14519 loc += (relgot->reloc_count++
14520 * sizeof (Elf64_External_Rela));
14521 bfd_elf64_swap_reloca_out (output_bfd,
14522 &outrel, loc);
14523 outrel.r_offset += 8;
14524 outrel.r_addend = addend;
14525 outrel.r_info
14526 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14527 }
14528 }
14529 else if (tls_type == (TLS_TLS | TLS_DTPREL))
14530 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
14531 else if (tls_type == (TLS_TLS | TLS_TPREL))
14532 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
14533 else if (indx != 0)
14534 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
14535 else
14536 {
14537 if (ifunc)
14538 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14539 else
14540 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14541
14542 /* Write the .got section contents for the sake
14543 of prelink. */
14544 loc = got->contents + off;
14545 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
14546 loc);
14547 }
14548
14549 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
14550 {
14551 outrel.r_addend += relocation;
14552 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
14553 {
14554 if (htab->elf.tls_sec == NULL)
14555 outrel.r_addend = 0;
14556 else
14557 outrel.r_addend -= htab->elf.tls_sec->vma;
14558 }
14559 }
14560 loc = relgot->contents;
14561 loc += (relgot->reloc_count++
14562 * sizeof (Elf64_External_Rela));
14563 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14564 }
14565
14566 /* Init the .got section contents here if we're not
14567 emitting a reloc. */
14568 else
14569 {
14570 relocation += addend;
14571 if (tls_type != 0)
14572 {
14573 if (htab->elf.tls_sec == NULL)
14574 relocation = 0;
14575 else
14576 {
14577 if (tls_type & TLS_LD)
14578 relocation = 0;
14579 else
14580 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
14581 if (tls_type & TLS_TPREL)
14582 relocation += DTP_OFFSET - TP_OFFSET;
14583 }
14584
14585 if (tls_type & (TLS_GD | TLS_LD))
14586 {
14587 bfd_put_64 (output_bfd, relocation,
14588 got->contents + off + 8);
14589 relocation = 1;
14590 }
14591 }
14592 bfd_put_64 (output_bfd, relocation,
14593 got->contents + off);
14594 }
14595 }
14596
14597 if (off >= (bfd_vma) -2)
14598 abort ();
14599
14600 relocation = got->output_section->vma + got->output_offset + off;
14601 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
14602 }
14603 break;
14604
14605 case R_PPC64_PLT16_HA:
14606 case R_PPC64_PLT16_HI:
14607 case R_PPC64_PLT16_LO:
14608 case R_PPC64_PLT32:
14609 case R_PPC64_PLT64:
14610 /* Relocation is to the entry for this symbol in the
14611 procedure linkage table. */
14612 {
14613 struct plt_entry **plt_list = NULL;
14614 if (h != NULL)
14615 plt_list = &h->elf.plt.plist;
14616 else if (local_got_ents != NULL)
14617 {
14618 struct plt_entry **local_plt = (struct plt_entry **)
14619 (local_got_ents + symtab_hdr->sh_info);
14620 unsigned char *local_got_tls_masks = (unsigned char *)
14621 (local_plt + symtab_hdr->sh_info);
14622 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
14623 plt_list = local_plt + r_symndx;
14624 }
14625 if (plt_list)
14626 {
14627 struct plt_entry *ent;
14628
14629 for (ent = *plt_list; ent != NULL; ent = ent->next)
14630 if (ent->plt.offset != (bfd_vma) -1
14631 && ent->addend == orig_rel.r_addend)
14632 {
14633 asection *plt;
14634
14635 plt = htab->elf.splt;
14636 if (!htab->elf.dynamic_sections_created
14637 || h == NULL
14638 || h->elf.dynindx == -1)
14639 plt = htab->elf.iplt;
14640 relocation = (plt->output_section->vma
14641 + plt->output_offset
14642 + ent->plt.offset);
14643 addend = 0;
14644 unresolved_reloc = FALSE;
14645 break;
14646 }
14647 }
14648 }
14649 break;
14650
14651 case R_PPC64_TOC:
14652 /* Relocation value is TOC base. */
14653 relocation = TOCstart;
14654 if (r_symndx == STN_UNDEF)
14655 relocation += htab->sec_info[input_section->id].toc_off;
14656 else if (unresolved_reloc)
14657 ;
14658 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
14659 relocation += htab->sec_info[sec->id].toc_off;
14660 else
14661 unresolved_reloc = TRUE;
14662 goto dodyn;
14663
14664 /* TOC16 relocs. We want the offset relative to the TOC base,
14665 which is the address of the start of the TOC plus 0x8000.
14666 The TOC consists of sections .got, .toc, .tocbss, and .plt,
14667 in this order. */
14668 case R_PPC64_TOC16:
14669 case R_PPC64_TOC16_LO:
14670 case R_PPC64_TOC16_HI:
14671 case R_PPC64_TOC16_DS:
14672 case R_PPC64_TOC16_LO_DS:
14673 case R_PPC64_TOC16_HA:
14674 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
14675 break;
14676
14677 /* Relocate against the beginning of the section. */
14678 case R_PPC64_SECTOFF:
14679 case R_PPC64_SECTOFF_LO:
14680 case R_PPC64_SECTOFF_HI:
14681 case R_PPC64_SECTOFF_DS:
14682 case R_PPC64_SECTOFF_LO_DS:
14683 case R_PPC64_SECTOFF_HA:
14684 if (sec != NULL)
14685 addend -= sec->output_section->vma;
14686 break;
14687
14688 case R_PPC64_REL16:
14689 case R_PPC64_REL16_LO:
14690 case R_PPC64_REL16_HI:
14691 case R_PPC64_REL16_HA:
14692 case R_PPC64_REL16DX_HA:
14693 break;
14694
14695 case R_PPC64_REL14:
14696 case R_PPC64_REL14_BRNTAKEN:
14697 case R_PPC64_REL14_BRTAKEN:
14698 case R_PPC64_REL24:
14699 break;
14700
14701 case R_PPC64_TPREL16:
14702 case R_PPC64_TPREL16_LO:
14703 case R_PPC64_TPREL16_HI:
14704 case R_PPC64_TPREL16_HA:
14705 case R_PPC64_TPREL16_DS:
14706 case R_PPC64_TPREL16_LO_DS:
14707 case R_PPC64_TPREL16_HIGH:
14708 case R_PPC64_TPREL16_HIGHA:
14709 case R_PPC64_TPREL16_HIGHER:
14710 case R_PPC64_TPREL16_HIGHERA:
14711 case R_PPC64_TPREL16_HIGHEST:
14712 case R_PPC64_TPREL16_HIGHESTA:
14713 if (h != NULL
14714 && h->elf.root.type == bfd_link_hash_undefweak
14715 && h->elf.dynindx == -1)
14716 {
14717 /* Make this relocation against an undefined weak symbol
14718 resolve to zero. This is really just a tweak, since
14719 code using weak externs ought to check that they are
14720 defined before using them. */
14721 bfd_byte *p = contents + rel->r_offset - d_offset;
14722
14723 insn = bfd_get_32 (input_bfd, p);
14724 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
14725 if (insn != 0)
14726 bfd_put_32 (input_bfd, insn, p);
14727 break;
14728 }
14729 if (htab->elf.tls_sec != NULL)
14730 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14731 /* The TPREL16 relocs shouldn't really be used in shared
14732 libs or with non-local symbols as that will result in
14733 DT_TEXTREL being set, but support them anyway. */
14734 goto dodyn;
14735
14736 case R_PPC64_DTPREL16:
14737 case R_PPC64_DTPREL16_LO:
14738 case R_PPC64_DTPREL16_HI:
14739 case R_PPC64_DTPREL16_HA:
14740 case R_PPC64_DTPREL16_DS:
14741 case R_PPC64_DTPREL16_LO_DS:
14742 case R_PPC64_DTPREL16_HIGH:
14743 case R_PPC64_DTPREL16_HIGHA:
14744 case R_PPC64_DTPREL16_HIGHER:
14745 case R_PPC64_DTPREL16_HIGHERA:
14746 case R_PPC64_DTPREL16_HIGHEST:
14747 case R_PPC64_DTPREL16_HIGHESTA:
14748 if (htab->elf.tls_sec != NULL)
14749 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14750 break;
14751
14752 case R_PPC64_ADDR64_LOCAL:
14753 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
14754 ? h->elf.other
14755 : sym->st_other);
14756 break;
14757
14758 case R_PPC64_DTPMOD64:
14759 relocation = 1;
14760 addend = 0;
14761 goto dodyn;
14762
14763 case R_PPC64_TPREL64:
14764 if (htab->elf.tls_sec != NULL)
14765 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
14766 goto dodyn;
14767
14768 case R_PPC64_DTPREL64:
14769 if (htab->elf.tls_sec != NULL)
14770 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
14771 /* Fall through. */
14772
14773 /* Relocations that may need to be propagated if this is a
14774 dynamic object. */
14775 case R_PPC64_REL30:
14776 case R_PPC64_REL32:
14777 case R_PPC64_REL64:
14778 case R_PPC64_ADDR14:
14779 case R_PPC64_ADDR14_BRNTAKEN:
14780 case R_PPC64_ADDR14_BRTAKEN:
14781 case R_PPC64_ADDR16:
14782 case R_PPC64_ADDR16_DS:
14783 case R_PPC64_ADDR16_HA:
14784 case R_PPC64_ADDR16_HI:
14785 case R_PPC64_ADDR16_HIGH:
14786 case R_PPC64_ADDR16_HIGHA:
14787 case R_PPC64_ADDR16_HIGHER:
14788 case R_PPC64_ADDR16_HIGHERA:
14789 case R_PPC64_ADDR16_HIGHEST:
14790 case R_PPC64_ADDR16_HIGHESTA:
14791 case R_PPC64_ADDR16_LO:
14792 case R_PPC64_ADDR16_LO_DS:
14793 case R_PPC64_ADDR24:
14794 case R_PPC64_ADDR32:
14795 case R_PPC64_ADDR64:
14796 case R_PPC64_UADDR16:
14797 case R_PPC64_UADDR32:
14798 case R_PPC64_UADDR64:
14799 dodyn:
14800 if ((input_section->flags & SEC_ALLOC) == 0)
14801 break;
14802
14803 if (NO_OPD_RELOCS && is_opd)
14804 break;
14805
14806 if (bfd_link_pic (info)
14807 ? ((h == NULL
14808 || h->dyn_relocs != NULL)
14809 && ((h != NULL && pc_dynrelocs (h))
14810 || must_be_dyn_reloc (info, r_type)))
14811 : (h != NULL
14812 ? h->dyn_relocs != NULL
14813 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14814 {
14815 bfd_boolean skip, relocate;
14816 asection *sreloc;
14817 bfd_vma out_off;
14818 long indx = 0;
14819
14820 /* When generating a dynamic object, these relocations
14821 are copied into the output file to be resolved at run
14822 time. */
14823
14824 skip = FALSE;
14825 relocate = FALSE;
14826
14827 out_off = _bfd_elf_section_offset (output_bfd, info,
14828 input_section, rel->r_offset);
14829 if (out_off == (bfd_vma) -1)
14830 skip = TRUE;
14831 else if (out_off == (bfd_vma) -2)
14832 skip = TRUE, relocate = TRUE;
14833 out_off += (input_section->output_section->vma
14834 + input_section->output_offset);
14835 outrel.r_offset = out_off;
14836 outrel.r_addend = rel->r_addend;
14837
14838 /* Optimize unaligned reloc use. */
14839 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
14840 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
14841 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
14842 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
14843 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
14844 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
14845 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
14846 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
14847 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
14848
14849 if (skip)
14850 memset (&outrel, 0, sizeof outrel);
14851 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
14852 && !is_opd
14853 && r_type != R_PPC64_TOC)
14854 {
14855 indx = h->elf.dynindx;
14856 BFD_ASSERT (indx != -1);
14857 outrel.r_info = ELF64_R_INFO (indx, r_type);
14858 }
14859 else
14860 {
14861 /* This symbol is local, or marked to become local,
14862 or this is an opd section reloc which must point
14863 at a local function. */
14864 outrel.r_addend += relocation;
14865 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
14866 {
14867 if (is_opd && h != NULL)
14868 {
14869 /* Lie about opd entries. This case occurs
14870 when building shared libraries and we
14871 reference a function in another shared
14872 lib. The same thing happens for a weak
14873 definition in an application that's
14874 overridden by a strong definition in a
14875 shared lib. (I believe this is a generic
14876 bug in binutils handling of weak syms.)
14877 In these cases we won't use the opd
14878 entry in this lib. */
14879 unresolved_reloc = FALSE;
14880 }
14881 if (!is_opd
14882 && r_type == R_PPC64_ADDR64
14883 && (h != NULL
14884 ? h->elf.type == STT_GNU_IFUNC
14885 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
14886 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14887 else
14888 {
14889 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14890
14891 /* We need to relocate .opd contents for ld.so.
14892 Prelink also wants simple and consistent rules
14893 for relocs. This make all RELATIVE relocs have
14894 *r_offset equal to r_addend. */
14895 relocate = TRUE;
14896 }
14897 }
14898 else
14899 {
14900 if (h != NULL
14901 ? h->elf.type == STT_GNU_IFUNC
14902 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14903 {
14904 info->callbacks->einfo
14905 /* xgettext:c-format */
14906 (_("%H: %s for indirect "
14907 "function `%T' unsupported\n"),
14908 input_bfd, input_section, rel->r_offset,
14909 ppc64_elf_howto_table[r_type]->name,
14910 sym_name);
14911 ret = FALSE;
14912 }
14913 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14914 ;
14915 else if (sec == NULL || sec->owner == NULL)
14916 {
14917 bfd_set_error (bfd_error_bad_value);
14918 return FALSE;
14919 }
14920 else
14921 {
14922 asection *osec;
14923
14924 osec = sec->output_section;
14925 indx = elf_section_data (osec)->dynindx;
14926
14927 if (indx == 0)
14928 {
14929 if ((osec->flags & SEC_READONLY) == 0
14930 && htab->elf.data_index_section != NULL)
14931 osec = htab->elf.data_index_section;
14932 else
14933 osec = htab->elf.text_index_section;
14934 indx = elf_section_data (osec)->dynindx;
14935 }
14936 BFD_ASSERT (indx != 0);
14937
14938 /* We are turning this relocation into one
14939 against a section symbol, so subtract out
14940 the output section's address but not the
14941 offset of the input section in the output
14942 section. */
14943 outrel.r_addend -= osec->vma;
14944 }
14945
14946 outrel.r_info = ELF64_R_INFO (indx, r_type);
14947 }
14948 }
14949
14950 sreloc = elf_section_data (input_section)->sreloc;
14951 if (h != NULL
14952 ? h->elf.type == STT_GNU_IFUNC
14953 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14954 {
14955 sreloc = htab->elf.irelplt;
14956 if (indx == 0)
14957 htab->local_ifunc_resolver = 1;
14958 else if (is_static_defined (&h->elf))
14959 htab->maybe_local_ifunc_resolver = 1;
14960 }
14961 if (sreloc == NULL)
14962 abort ();
14963
14964 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14965 >= sreloc->size)
14966 abort ();
14967 loc = sreloc->contents;
14968 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14969 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14970
14971 /* If this reloc is against an external symbol, it will
14972 be computed at runtime, so there's no need to do
14973 anything now. However, for the sake of prelink ensure
14974 that the section contents are a known value. */
14975 if (! relocate)
14976 {
14977 unresolved_reloc = FALSE;
14978 /* The value chosen here is quite arbitrary as ld.so
14979 ignores section contents except for the special
14980 case of .opd where the contents might be accessed
14981 before relocation. Choose zero, as that won't
14982 cause reloc overflow. */
14983 relocation = 0;
14984 addend = 0;
14985 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14986 to improve backward compatibility with older
14987 versions of ld. */
14988 if (r_type == R_PPC64_ADDR64)
14989 addend = outrel.r_addend;
14990 /* Adjust pc_relative relocs to have zero in *r_offset. */
14991 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14992 addend = outrel.r_offset;
14993 }
14994 }
14995 break;
14996
14997 case R_PPC64_COPY:
14998 case R_PPC64_GLOB_DAT:
14999 case R_PPC64_JMP_SLOT:
15000 case R_PPC64_JMP_IREL:
15001 case R_PPC64_RELATIVE:
15002 /* We shouldn't ever see these dynamic relocs in relocatable
15003 files. */
15004 /* Fall through. */
15005
15006 case R_PPC64_PLTGOT16:
15007 case R_PPC64_PLTGOT16_DS:
15008 case R_PPC64_PLTGOT16_HA:
15009 case R_PPC64_PLTGOT16_HI:
15010 case R_PPC64_PLTGOT16_LO:
15011 case R_PPC64_PLTGOT16_LO_DS:
15012 case R_PPC64_PLTREL32:
15013 case R_PPC64_PLTREL64:
15014 /* These ones haven't been implemented yet. */
15015
15016 info->callbacks->einfo
15017 /* xgettext:c-format */
15018 (_("%P: %B: %s is not supported for `%T'\n"),
15019 input_bfd,
15020 ppc64_elf_howto_table[r_type]->name, sym_name);
15021
15022 bfd_set_error (bfd_error_invalid_operation);
15023 ret = FALSE;
15024 goto copy_reloc;
15025 }
15026
15027 /* Multi-instruction sequences that access the TOC can be
15028 optimized, eg. addis ra,r2,0; addi rb,ra,x;
15029 to nop; addi rb,r2,x; */
15030 switch (r_type)
15031 {
15032 default:
15033 break;
15034
15035 case R_PPC64_GOT_TLSLD16_HI:
15036 case R_PPC64_GOT_TLSGD16_HI:
15037 case R_PPC64_GOT_TPREL16_HI:
15038 case R_PPC64_GOT_DTPREL16_HI:
15039 case R_PPC64_GOT16_HI:
15040 case R_PPC64_TOC16_HI:
15041 /* These relocs would only be useful if building up an
15042 offset to later add to r2, perhaps in an indexed
15043 addressing mode instruction. Don't try to optimize.
15044 Unfortunately, the possibility of someone building up an
15045 offset like this or even with the HA relocs, means that
15046 we need to check the high insn when optimizing the low
15047 insn. */
15048 break;
15049
15050 case R_PPC64_GOT_TLSLD16_HA:
15051 case R_PPC64_GOT_TLSGD16_HA:
15052 case R_PPC64_GOT_TPREL16_HA:
15053 case R_PPC64_GOT_DTPREL16_HA:
15054 case R_PPC64_GOT16_HA:
15055 case R_PPC64_TOC16_HA:
15056 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15057 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15058 {
15059 bfd_byte *p = contents + (rel->r_offset & ~3);
15060 bfd_put_32 (input_bfd, NOP, p);
15061 goto copy_reloc;
15062 }
15063 break;
15064
15065 case R_PPC64_GOT_TLSLD16_LO:
15066 case R_PPC64_GOT_TLSGD16_LO:
15067 case R_PPC64_GOT_TPREL16_LO_DS:
15068 case R_PPC64_GOT_DTPREL16_LO_DS:
15069 case R_PPC64_GOT16_LO:
15070 case R_PPC64_GOT16_LO_DS:
15071 case R_PPC64_TOC16_LO:
15072 case R_PPC64_TOC16_LO_DS:
15073 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
15074 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
15075 {
15076 bfd_byte *p = contents + (rel->r_offset & ~3);
15077 insn = bfd_get_32 (input_bfd, p);
15078 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
15079 {
15080 /* Transform addic to addi when we change reg. */
15081 insn &= ~((0x3f << 26) | (0x1f << 16));
15082 insn |= (14u << 26) | (2 << 16);
15083 }
15084 else
15085 {
15086 insn &= ~(0x1f << 16);
15087 insn |= 2 << 16;
15088 }
15089 bfd_put_32 (input_bfd, insn, p);
15090 }
15091 break;
15092
15093 case R_PPC64_TPREL16_HA:
15094 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15095 {
15096 bfd_byte *p = contents + (rel->r_offset & ~3);
15097 insn = bfd_get_32 (input_bfd, p);
15098 if ((insn & ((0x3f << 26) | 0x1f << 16))
15099 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
15100 /* xgettext:c-format */
15101 info->callbacks->minfo
15102 (_("%H: warning: %s unexpected insn %#x.\n"),
15103 input_bfd, input_section, rel->r_offset,
15104 ppc64_elf_howto_table[r_type]->name, insn);
15105 else
15106 {
15107 bfd_put_32 (input_bfd, NOP, p);
15108 goto copy_reloc;
15109 }
15110 }
15111 break;
15112
15113 case R_PPC64_TPREL16_LO:
15114 case R_PPC64_TPREL16_LO_DS:
15115 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
15116 {
15117 bfd_byte *p = contents + (rel->r_offset & ~3);
15118 insn = bfd_get_32 (input_bfd, p);
15119 insn &= ~(0x1f << 16);
15120 insn |= 13 << 16;
15121 bfd_put_32 (input_bfd, insn, p);
15122 }
15123 break;
15124 }
15125
15126 /* Do any further special processing. */
15127 switch (r_type)
15128 {
15129 default:
15130 break;
15131
15132 case R_PPC64_REL16_HA:
15133 case R_PPC64_REL16DX_HA:
15134 case R_PPC64_ADDR16_HA:
15135 case R_PPC64_ADDR16_HIGHA:
15136 case R_PPC64_ADDR16_HIGHERA:
15137 case R_PPC64_ADDR16_HIGHESTA:
15138 case R_PPC64_TOC16_HA:
15139 case R_PPC64_SECTOFF_HA:
15140 case R_PPC64_TPREL16_HA:
15141 case R_PPC64_TPREL16_HIGHA:
15142 case R_PPC64_TPREL16_HIGHERA:
15143 case R_PPC64_TPREL16_HIGHESTA:
15144 case R_PPC64_DTPREL16_HA:
15145 case R_PPC64_DTPREL16_HIGHA:
15146 case R_PPC64_DTPREL16_HIGHERA:
15147 case R_PPC64_DTPREL16_HIGHESTA:
15148 /* It's just possible that this symbol is a weak symbol
15149 that's not actually defined anywhere. In that case,
15150 'sec' would be NULL, and we should leave the symbol
15151 alone (it will be set to zero elsewhere in the link). */
15152 if (sec == NULL)
15153 break;
15154 /* Fall through. */
15155
15156 case R_PPC64_GOT16_HA:
15157 case R_PPC64_PLTGOT16_HA:
15158 case R_PPC64_PLT16_HA:
15159 case R_PPC64_GOT_TLSGD16_HA:
15160 case R_PPC64_GOT_TLSLD16_HA:
15161 case R_PPC64_GOT_TPREL16_HA:
15162 case R_PPC64_GOT_DTPREL16_HA:
15163 /* Add 0x10000 if sign bit in 0:15 is set.
15164 Bits 0:15 are not used. */
15165 addend += 0x8000;
15166 break;
15167
15168 case R_PPC64_ADDR16_DS:
15169 case R_PPC64_ADDR16_LO_DS:
15170 case R_PPC64_GOT16_DS:
15171 case R_PPC64_GOT16_LO_DS:
15172 case R_PPC64_PLT16_LO_DS:
15173 case R_PPC64_SECTOFF_DS:
15174 case R_PPC64_SECTOFF_LO_DS:
15175 case R_PPC64_TOC16_DS:
15176 case R_PPC64_TOC16_LO_DS:
15177 case R_PPC64_PLTGOT16_DS:
15178 case R_PPC64_PLTGOT16_LO_DS:
15179 case R_PPC64_GOT_TPREL16_DS:
15180 case R_PPC64_GOT_TPREL16_LO_DS:
15181 case R_PPC64_GOT_DTPREL16_DS:
15182 case R_PPC64_GOT_DTPREL16_LO_DS:
15183 case R_PPC64_TPREL16_DS:
15184 case R_PPC64_TPREL16_LO_DS:
15185 case R_PPC64_DTPREL16_DS:
15186 case R_PPC64_DTPREL16_LO_DS:
15187 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15188 mask = 3;
15189 /* If this reloc is against an lq, lxv, or stxv insn, then
15190 the value must be a multiple of 16. This is somewhat of
15191 a hack, but the "correct" way to do this by defining _DQ
15192 forms of all the _DS relocs bloats all reloc switches in
15193 this file. It doesn't make much sense to use these
15194 relocs in data, so testing the insn should be safe. */
15195 if ((insn & (0x3f << 26)) == (56u << 26)
15196 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
15197 mask = 15;
15198 relocation += addend;
15199 addend = insn & (mask ^ 3);
15200 if ((relocation & mask) != 0)
15201 {
15202 relocation ^= relocation & mask;
15203 info->callbacks->einfo
15204 /* xgettext:c-format */
15205 (_("%H: error: %s not a multiple of %u\n"),
15206 input_bfd, input_section, rel->r_offset,
15207 ppc64_elf_howto_table[r_type]->name,
15208 mask + 1);
15209 bfd_set_error (bfd_error_bad_value);
15210 ret = FALSE;
15211 goto copy_reloc;
15212 }
15213 break;
15214 }
15215
15216 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
15217 because such sections are not SEC_ALLOC and thus ld.so will
15218 not process them. */
15219 howto = ppc64_elf_howto_table[(int) r_type];
15220 if (unresolved_reloc
15221 && !((input_section->flags & SEC_DEBUGGING) != 0
15222 && h->elf.def_dynamic)
15223 && _bfd_elf_section_offset (output_bfd, info, input_section,
15224 rel->r_offset) != (bfd_vma) -1)
15225 {
15226 info->callbacks->einfo
15227 /* xgettext:c-format */
15228 (_("%H: unresolvable %s against `%T'\n"),
15229 input_bfd, input_section, rel->r_offset,
15230 howto->name,
15231 h->elf.root.root.string);
15232 ret = FALSE;
15233 }
15234
15235 /* 16-bit fields in insns mostly have signed values, but a
15236 few insns have 16-bit unsigned values. Really, we should
15237 have different reloc types. */
15238 if (howto->complain_on_overflow != complain_overflow_dont
15239 && howto->dst_mask == 0xffff
15240 && (input_section->flags & SEC_CODE) != 0)
15241 {
15242 enum complain_overflow complain = complain_overflow_signed;
15243
15244 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15245 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
15246 complain = complain_overflow_bitfield;
15247 else if (howto->rightshift == 0
15248 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
15249 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
15250 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
15251 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
15252 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
15253 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
15254 complain = complain_overflow_unsigned;
15255 if (howto->complain_on_overflow != complain)
15256 {
15257 alt_howto = *howto;
15258 alt_howto.complain_on_overflow = complain;
15259 howto = &alt_howto;
15260 }
15261 }
15262
15263 if (r_type == R_PPC64_REL16DX_HA)
15264 {
15265 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
15266 if (rel->r_offset + 4 > input_section->size)
15267 r = bfd_reloc_outofrange;
15268 else
15269 {
15270 relocation += addend;
15271 relocation -= (rel->r_offset
15272 + input_section->output_offset
15273 + input_section->output_section->vma);
15274 relocation = (bfd_signed_vma) relocation >> 16;
15275 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15276 insn &= ~0x1fffc1;
15277 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
15278 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15279 r = bfd_reloc_ok;
15280 if (relocation + 0x8000 > 0xffff)
15281 r = bfd_reloc_overflow;
15282 }
15283 }
15284 else
15285 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
15286 rel->r_offset, relocation, addend);
15287
15288 if (r != bfd_reloc_ok)
15289 {
15290 char *more_info = NULL;
15291 const char *reloc_name = howto->name;
15292
15293 if (reloc_dest != DEST_NORMAL)
15294 {
15295 more_info = bfd_malloc (strlen (reloc_name) + 8);
15296 if (more_info != NULL)
15297 {
15298 strcpy (more_info, reloc_name);
15299 strcat (more_info, (reloc_dest == DEST_OPD
15300 ? " (OPD)" : " (stub)"));
15301 reloc_name = more_info;
15302 }
15303 }
15304
15305 if (r == bfd_reloc_overflow)
15306 {
15307 /* On code like "if (foo) foo();" don't report overflow
15308 on a branch to zero when foo is undefined. */
15309 if (!warned
15310 && (reloc_dest == DEST_STUB
15311 || !(h != NULL
15312 && (h->elf.root.type == bfd_link_hash_undefweak
15313 || h->elf.root.type == bfd_link_hash_undefined)
15314 && is_branch_reloc (r_type))))
15315 info->callbacks->reloc_overflow (info, &h->elf.root,
15316 sym_name, reloc_name,
15317 orig_rel.r_addend,
15318 input_bfd, input_section,
15319 rel->r_offset);
15320 }
15321 else
15322 {
15323 info->callbacks->einfo
15324 /* xgettext:c-format */
15325 (_("%H: %s against `%T': error %d\n"),
15326 input_bfd, input_section, rel->r_offset,
15327 reloc_name, sym_name, (int) r);
15328 ret = FALSE;
15329 }
15330 if (more_info != NULL)
15331 free (more_info);
15332 }
15333 copy_reloc:
15334 if (wrel != rel)
15335 *wrel = *rel;
15336 }
15337
15338 if (wrel != rel)
15339 {
15340 Elf_Internal_Shdr *rel_hdr;
15341 size_t deleted = rel - wrel;
15342
15343 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
15344 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15345 if (rel_hdr->sh_size == 0)
15346 {
15347 /* It is too late to remove an empty reloc section. Leave
15348 one NONE reloc.
15349 ??? What is wrong with an empty section??? */
15350 rel_hdr->sh_size = rel_hdr->sh_entsize;
15351 deleted -= 1;
15352 }
15353 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
15354 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
15355 input_section->reloc_count -= deleted;
15356 }
15357
15358 /* If we're emitting relocations, then shortly after this function
15359 returns, reloc offsets and addends for this section will be
15360 adjusted. Worse, reloc symbol indices will be for the output
15361 file rather than the input. Save a copy of the relocs for
15362 opd_entry_value. */
15363 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
15364 {
15365 bfd_size_type amt;
15366 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
15367 rel = bfd_alloc (input_bfd, amt);
15368 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
15369 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
15370 if (rel == NULL)
15371 return FALSE;
15372 memcpy (rel, relocs, amt);
15373 }
15374 return ret;
15375 }
15376
15377 /* Adjust the value of any local symbols in opd sections. */
15378
15379 static int
15380 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
15381 const char *name ATTRIBUTE_UNUSED,
15382 Elf_Internal_Sym *elfsym,
15383 asection *input_sec,
15384 struct elf_link_hash_entry *h)
15385 {
15386 struct _opd_sec_data *opd;
15387 long adjust;
15388 bfd_vma value;
15389
15390 if (h != NULL)
15391 return 1;
15392
15393 opd = get_opd_info (input_sec);
15394 if (opd == NULL || opd->adjust == NULL)
15395 return 1;
15396
15397 value = elfsym->st_value - input_sec->output_offset;
15398 if (!bfd_link_relocatable (info))
15399 value -= input_sec->output_section->vma;
15400
15401 adjust = opd->adjust[OPD_NDX (value)];
15402 if (adjust == -1)
15403 return 2;
15404
15405 elfsym->st_value += adjust;
15406 return 1;
15407 }
15408
15409 /* Finish up dynamic symbol handling. We set the contents of various
15410 dynamic sections here. */
15411
15412 static bfd_boolean
15413 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
15414 struct bfd_link_info *info,
15415 struct elf_link_hash_entry *h,
15416 Elf_Internal_Sym *sym)
15417 {
15418 struct ppc_link_hash_table *htab;
15419 struct plt_entry *ent;
15420 Elf_Internal_Rela rela;
15421 bfd_byte *loc;
15422
15423 htab = ppc_hash_table (info);
15424 if (htab == NULL)
15425 return FALSE;
15426
15427 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
15428 if (ent->plt.offset != (bfd_vma) -1)
15429 {
15430 /* This symbol has an entry in the procedure linkage
15431 table. Set it up. */
15432 if (!htab->elf.dynamic_sections_created
15433 || h->dynindx == -1)
15434 {
15435 BFD_ASSERT (h->type == STT_GNU_IFUNC
15436 && h->def_regular
15437 && (h->root.type == bfd_link_hash_defined
15438 || h->root.type == bfd_link_hash_defweak));
15439 rela.r_offset = (htab->elf.iplt->output_section->vma
15440 + htab->elf.iplt->output_offset
15441 + ent->plt.offset);
15442 if (htab->opd_abi)
15443 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
15444 else
15445 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15446 rela.r_addend = (h->root.u.def.value
15447 + h->root.u.def.section->output_offset
15448 + h->root.u.def.section->output_section->vma
15449 + ent->addend);
15450 loc = (htab->elf.irelplt->contents
15451 + (htab->elf.irelplt->reloc_count++
15452 * sizeof (Elf64_External_Rela)));
15453 htab->local_ifunc_resolver = 1;
15454 }
15455 else
15456 {
15457 rela.r_offset = (htab->elf.splt->output_section->vma
15458 + htab->elf.splt->output_offset
15459 + ent->plt.offset);
15460 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
15461 rela.r_addend = ent->addend;
15462 loc = (htab->elf.srelplt->contents
15463 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
15464 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
15465 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
15466 htab->maybe_local_ifunc_resolver = 1;
15467 }
15468 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15469
15470 if (!htab->opd_abi)
15471 {
15472 if (!h->def_regular)
15473 {
15474 /* Mark the symbol as undefined, rather than as
15475 defined in glink. Leave the value if there were
15476 any relocations where pointer equality matters
15477 (this is a clue for the dynamic linker, to make
15478 function pointer comparisons work between an
15479 application and shared library), otherwise set it
15480 to zero. */
15481 sym->st_shndx = SHN_UNDEF;
15482 if (!h->pointer_equality_needed)
15483 sym->st_value = 0;
15484 else if (!h->ref_regular_nonweak)
15485 {
15486 /* This breaks function pointer comparisons, but
15487 that is better than breaking tests for a NULL
15488 function pointer. */
15489 sym->st_value = 0;
15490 }
15491 }
15492 }
15493 }
15494
15495 if (h->needs_copy)
15496 {
15497 /* This symbol needs a copy reloc. Set it up. */
15498 asection *srel;
15499
15500 if (h->dynindx == -1
15501 || (h->root.type != bfd_link_hash_defined
15502 && h->root.type != bfd_link_hash_defweak)
15503 || htab->elf.srelbss == NULL
15504 || htab->elf.sreldynrelro == NULL)
15505 abort ();
15506
15507 rela.r_offset = (h->root.u.def.value
15508 + h->root.u.def.section->output_section->vma
15509 + h->root.u.def.section->output_offset);
15510 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
15511 rela.r_addend = 0;
15512 if (h->root.u.def.section == htab->elf.sdynrelro)
15513 srel = htab->elf.sreldynrelro;
15514 else
15515 srel = htab->elf.srelbss;
15516 loc = srel->contents;
15517 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
15518 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
15519 }
15520
15521 return TRUE;
15522 }
15523
15524 /* Used to decide how to sort relocs in an optimal manner for the
15525 dynamic linker, before writing them out. */
15526
15527 static enum elf_reloc_type_class
15528 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
15529 const asection *rel_sec,
15530 const Elf_Internal_Rela *rela)
15531 {
15532 enum elf_ppc64_reloc_type r_type;
15533 struct ppc_link_hash_table *htab = ppc_hash_table (info);
15534
15535 if (rel_sec == htab->elf.irelplt)
15536 return reloc_class_ifunc;
15537
15538 r_type = ELF64_R_TYPE (rela->r_info);
15539 switch (r_type)
15540 {
15541 case R_PPC64_RELATIVE:
15542 return reloc_class_relative;
15543 case R_PPC64_JMP_SLOT:
15544 return reloc_class_plt;
15545 case R_PPC64_COPY:
15546 return reloc_class_copy;
15547 default:
15548 return reloc_class_normal;
15549 }
15550 }
15551
15552 /* Finish up the dynamic sections. */
15553
15554 static bfd_boolean
15555 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
15556 struct bfd_link_info *info)
15557 {
15558 struct ppc_link_hash_table *htab;
15559 bfd *dynobj;
15560 asection *sdyn;
15561
15562 htab = ppc_hash_table (info);
15563 if (htab == NULL)
15564 return FALSE;
15565
15566 dynobj = htab->elf.dynobj;
15567 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15568
15569 if (htab->elf.dynamic_sections_created)
15570 {
15571 Elf64_External_Dyn *dyncon, *dynconend;
15572
15573 if (sdyn == NULL || htab->elf.sgot == NULL)
15574 abort ();
15575
15576 dyncon = (Elf64_External_Dyn *) sdyn->contents;
15577 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15578 for (; dyncon < dynconend; dyncon++)
15579 {
15580 Elf_Internal_Dyn dyn;
15581 asection *s;
15582
15583 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
15584
15585 switch (dyn.d_tag)
15586 {
15587 default:
15588 continue;
15589
15590 case DT_PPC64_GLINK:
15591 s = htab->glink;
15592 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15593 /* We stupidly defined DT_PPC64_GLINK to be the start
15594 of glink rather than the first entry point, which is
15595 what ld.so needs, and now have a bigger stub to
15596 support automatic multiple TOCs. */
15597 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
15598 break;
15599
15600 case DT_PPC64_OPD:
15601 s = bfd_get_section_by_name (output_bfd, ".opd");
15602 if (s == NULL)
15603 continue;
15604 dyn.d_un.d_ptr = s->vma;
15605 break;
15606
15607 case DT_PPC64_OPT:
15608 if (htab->do_multi_toc && htab->multi_toc_needed)
15609 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
15610 if (htab->has_plt_localentry0)
15611 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
15612 break;
15613
15614 case DT_PPC64_OPDSZ:
15615 s = bfd_get_section_by_name (output_bfd, ".opd");
15616 if (s == NULL)
15617 continue;
15618 dyn.d_un.d_val = s->size;
15619 break;
15620
15621 case DT_PLTGOT:
15622 s = htab->elf.splt;
15623 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15624 break;
15625
15626 case DT_JMPREL:
15627 s = htab->elf.srelplt;
15628 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
15629 break;
15630
15631 case DT_PLTRELSZ:
15632 dyn.d_un.d_val = htab->elf.srelplt->size;
15633 break;
15634
15635 case DT_TEXTREL:
15636 if (htab->local_ifunc_resolver)
15637 info->callbacks->einfo
15638 (_("%X%P: text relocations and GNU indirect "
15639 "functions will result in a segfault at runtime\n"));
15640 else if (htab->maybe_local_ifunc_resolver)
15641 info->callbacks->einfo
15642 (_("%P: warning: text relocations and GNU indirect "
15643 "functions may result in a segfault at runtime\n"));
15644 continue;
15645 }
15646
15647 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
15648 }
15649 }
15650
15651 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
15652 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
15653 {
15654 /* Fill in the first entry in the global offset table.
15655 We use it to hold the link-time TOCbase. */
15656 bfd_put_64 (output_bfd,
15657 elf_gp (output_bfd) + TOC_BASE_OFF,
15658 htab->elf.sgot->contents);
15659
15660 /* Set .got entry size. */
15661 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
15662 }
15663
15664 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
15665 && htab->elf.splt->output_section != bfd_abs_section_ptr)
15666 {
15667 /* Set .plt entry size. */
15668 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
15669 = PLT_ENTRY_SIZE (htab);
15670 }
15671
15672 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
15673 brlt ourselves if emitrelocations. */
15674 if (htab->brlt != NULL
15675 && htab->brlt->reloc_count != 0
15676 && !_bfd_elf_link_output_relocs (output_bfd,
15677 htab->brlt,
15678 elf_section_data (htab->brlt)->rela.hdr,
15679 elf_section_data (htab->brlt)->relocs,
15680 NULL))
15681 return FALSE;
15682
15683 if (htab->glink != NULL
15684 && htab->glink->reloc_count != 0
15685 && !_bfd_elf_link_output_relocs (output_bfd,
15686 htab->glink,
15687 elf_section_data (htab->glink)->rela.hdr,
15688 elf_section_data (htab->glink)->relocs,
15689 NULL))
15690 return FALSE;
15691
15692 if (htab->glink_eh_frame != NULL
15693 && htab->glink_eh_frame->size != 0)
15694 {
15695 bfd_vma val;
15696 bfd_byte *p;
15697 struct map_stub *group;
15698 size_t align = 4;
15699
15700 p = htab->glink_eh_frame->contents;
15701 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15702
15703 for (group = htab->group; group != NULL; group = group->next)
15704 if (group->stub_sec != NULL)
15705 {
15706 /* Offset to stub section. */
15707 val = (group->stub_sec->output_section->vma
15708 + group->stub_sec->output_offset);
15709 val -= (htab->glink_eh_frame->output_section->vma
15710 + htab->glink_eh_frame->output_offset
15711 + (p + 8 - htab->glink_eh_frame->contents));
15712 if (val + 0x80000000 > 0xffffffff)
15713 {
15714 info->callbacks->einfo
15715 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15716 group->stub_sec->name);
15717 return FALSE;
15718 }
15719 bfd_put_32 (dynobj, val, p + 8);
15720 p += stub_eh_frame_size (group, align);
15721 }
15722 if (htab->glink != NULL && htab->glink->size != 0)
15723 {
15724 /* Offset to .glink. */
15725 val = (htab->glink->output_section->vma
15726 + htab->glink->output_offset
15727 + 8);
15728 val -= (htab->glink_eh_frame->output_section->vma
15729 + htab->glink_eh_frame->output_offset
15730 + (p + 8 - htab->glink_eh_frame->contents));
15731 if (val + 0x80000000 > 0xffffffff)
15732 {
15733 info->callbacks->einfo
15734 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
15735 htab->glink->name);
15736 return FALSE;
15737 }
15738 bfd_put_32 (dynobj, val, p + 8);
15739 p += (24 + align - 1) & -align;
15740 }
15741
15742 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
15743 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
15744 htab->glink_eh_frame,
15745 htab->glink_eh_frame->contents))
15746 return FALSE;
15747 }
15748
15749 /* We need to handle writing out multiple GOT sections ourselves,
15750 since we didn't add them to DYNOBJ. We know dynobj is the first
15751 bfd. */
15752 while ((dynobj = dynobj->link.next) != NULL)
15753 {
15754 asection *s;
15755
15756 if (!is_ppc64_elf (dynobj))
15757 continue;
15758
15759 s = ppc64_elf_tdata (dynobj)->got;
15760 if (s != NULL
15761 && s->size != 0
15762 && s->output_section != bfd_abs_section_ptr
15763 && !bfd_set_section_contents (output_bfd, s->output_section,
15764 s->contents, s->output_offset,
15765 s->size))
15766 return FALSE;
15767 s = ppc64_elf_tdata (dynobj)->relgot;
15768 if (s != NULL
15769 && s->size != 0
15770 && s->output_section != bfd_abs_section_ptr
15771 && !bfd_set_section_contents (output_bfd, s->output_section,
15772 s->contents, s->output_offset,
15773 s->size))
15774 return FALSE;
15775 }
15776
15777 return TRUE;
15778 }
15779
15780 #include "elf64-target.h"
15781
15782 /* FreeBSD support */
15783
15784 #undef TARGET_LITTLE_SYM
15785 #undef TARGET_LITTLE_NAME
15786
15787 #undef TARGET_BIG_SYM
15788 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
15789 #undef TARGET_BIG_NAME
15790 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
15791
15792 #undef ELF_OSABI
15793 #define ELF_OSABI ELFOSABI_FREEBSD
15794
15795 #undef elf64_bed
15796 #define elf64_bed elf64_powerpc_fbsd_bed
15797
15798 #include "elf64-target.h"
This page took 0.671733 seconds and 4 git commands to generate.