*** empty log message ***
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra.
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
23
24
25 /* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
28
29 #include "sysdep.h"
30 #include <stdarg.h>
31 #include "bfd.h"
32 #include "bfdlink.h"
33 #include "libbfd.h"
34 #include "elf-bfd.h"
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.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 *);
58
59 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define elf_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_can_gc_sections 1
76 #define elf_backend_can_refcount 1
77 #define elf_backend_rela_normal 1
78 #define elf_backend_default_execstack 0
79
80 #define bfd_elf64_mkobject ppc64_elf_mkobject
81 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
82 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
83 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
84 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
85 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
86 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
87 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
88 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
89
90 #define elf_backend_object_p ppc64_elf_object_p
91 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
92 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
93 #define elf_backend_write_core_note ppc64_elf_write_core_note
94 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
95 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
96 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
97 #define elf_backend_check_directives ppc64_elf_process_dot_syms
98 #define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
99 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
100 #define elf_backend_check_relocs ppc64_elf_check_relocs
101 #define elf_backend_gc_keep ppc64_elf_gc_keep
102 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
103 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
104 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
105 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
106 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
107 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
108 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
109 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
110 #define elf_backend_action_discarded ppc64_elf_action_discarded
111 #define elf_backend_relocate_section ppc64_elf_relocate_section
112 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
113 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
114 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
115 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
116 #define elf_backend_special_sections ppc64_elf_special_sections
117 #define elf_backend_post_process_headers _bfd_elf_set_osabi
118
119 /* The name of the dynamic interpreter. This is put in the .interp
120 section. */
121 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
122
123 /* The size in bytes of an entry in the procedure linkage table. */
124 #define PLT_ENTRY_SIZE 24
125
126 /* The initial size of the plt reserved for the dynamic linker. */
127 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
128
129 /* TOC base pointers offset from start of TOC. */
130 #define TOC_BASE_OFF 0x8000
131
132 /* Offset of tp and dtp pointers from start of TLS block. */
133 #define TP_OFFSET 0x7000
134 #define DTP_OFFSET 0x8000
135
136 /* .plt call stub instructions. The normal stub is like this, but
137 sometimes the .plt entry crosses a 64k boundary and we need to
138 insert an addi to adjust r12. */
139 #define PLT_CALL_STUB_SIZE (7*4)
140 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
141 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
142 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
143 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
144 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
145 /* ld %r11,xxx+16@l(%r12) */
146 #define BCTR 0x4e800420 /* bctr */
147
148
149 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
150 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
151 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
152 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
153
154 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
155 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
156
157 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
158
159 /* glink call stub instructions. We enter with the index in R0. */
160 #define GLINK_CALL_STUB_SIZE (16*4)
161 /* 0: */
162 /* .quad plt0-1f */
163 /* __glink: */
164 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
165 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
166 /* 1: */
167 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
168 #define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
169 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
170 #define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
171 /* ld %11,0(%12) */
172 /* ld %2,8(%12) */
173 /* mtctr %11 */
174 /* ld %11,16(%12) */
175 /* bctr */
176
177 /* Pad with this. */
178 #define NOP 0x60000000
179
180 /* Some other nops. */
181 #define CROR_151515 0x4def7b82
182 #define CROR_313131 0x4ffffb82
183
184 /* .glink entries for the first 32k functions are two instructions. */
185 #define LI_R0_0 0x38000000 /* li %r0,0 */
186 #define B_DOT 0x48000000 /* b . */
187
188 /* After that, we need two instructions to load the index, followed by
189 a branch. */
190 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
191 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
192
193 /* Instructions used by the save and restore reg functions. */
194 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
195 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
196 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
197 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
198 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
199 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
200 #define LI_R12_0 0x39800000 /* li %r12,0 */
201 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
202 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
203 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
204 #define BLR 0x4e800020 /* blr */
205
206 /* Since .opd is an array of descriptors and each entry will end up
207 with identical R_PPC64_RELATIVE relocs, there is really no need to
208 propagate .opd relocs; The dynamic linker should be taught to
209 relocate .opd without reloc entries. */
210 #ifndef NO_OPD_RELOCS
211 #define NO_OPD_RELOCS 0
212 #endif
213 \f
214 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
215
216 /* Relocation HOWTO's. */
217 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
218
219 static reloc_howto_type ppc64_elf_howto_raw[] = {
220 /* This reloc does nothing. */
221 HOWTO (R_PPC64_NONE, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
225 FALSE, /* pc_relative */
226 0, /* bitpos */
227 complain_overflow_dont, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_NONE", /* name */
230 FALSE, /* partial_inplace */
231 0, /* src_mask */
232 0, /* dst_mask */
233 FALSE), /* pcrel_offset */
234
235 /* A standard 32 bit relocation. */
236 HOWTO (R_PPC64_ADDR32, /* type */
237 0, /* rightshift */
238 2, /* size (0 = byte, 1 = short, 2 = long) */
239 32, /* bitsize */
240 FALSE, /* pc_relative */
241 0, /* bitpos */
242 complain_overflow_bitfield, /* complain_on_overflow */
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_ADDR32", /* name */
245 FALSE, /* partial_inplace */
246 0, /* src_mask */
247 0xffffffff, /* dst_mask */
248 FALSE), /* pcrel_offset */
249
250 /* An absolute 26 bit branch; the lower two bits must be zero.
251 FIXME: we don't check that, we just clear them. */
252 HOWTO (R_PPC64_ADDR24, /* type */
253 0, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 26, /* bitsize */
256 FALSE, /* pc_relative */
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_PPC64_ADDR24", /* name */
261 FALSE, /* partial_inplace */
262 0, /* src_mask */
263 0x03fffffc, /* dst_mask */
264 FALSE), /* pcrel_offset */
265
266 /* A standard 16 bit relocation. */
267 HOWTO (R_PPC64_ADDR16, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
271 FALSE, /* pc_relative */
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16", /* name */
276 FALSE, /* partial_inplace */
277 0, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
280
281 /* A 16 bit relocation without overflow. */
282 HOWTO (R_PPC64_ADDR16_LO, /* type */
283 0, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
286 FALSE, /* pc_relative */
287 0, /* bitpos */
288 complain_overflow_dont,/* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16_LO", /* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xffff, /* dst_mask */
294 FALSE), /* pcrel_offset */
295
296 /* Bits 16-31 of an address. */
297 HOWTO (R_PPC64_ADDR16_HI, /* type */
298 16, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
301 FALSE, /* pc_relative */
302 0, /* bitpos */
303 complain_overflow_dont, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_ADDR16_HI", /* name */
306 FALSE, /* partial_inplace */
307 0, /* src_mask */
308 0xffff, /* dst_mask */
309 FALSE), /* pcrel_offset */
310
311 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
312 bits, treated as a signed number, is negative. */
313 HOWTO (R_PPC64_ADDR16_HA, /* type */
314 16, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
317 FALSE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
320 ppc64_elf_ha_reloc, /* special_function */
321 "R_PPC64_ADDR16_HA", /* name */
322 FALSE, /* partial_inplace */
323 0, /* src_mask */
324 0xffff, /* dst_mask */
325 FALSE), /* pcrel_offset */
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOWTO (R_PPC64_ADDR14, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
333 FALSE, /* pc_relative */
334 0, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
336 ppc64_elf_branch_reloc, /* special_function */
337 "R_PPC64_ADDR14", /* name */
338 FALSE, /* partial_inplace */
339 0, /* src_mask */
340 0x0000fffc, /* dst_mask */
341 FALSE), /* pcrel_offset */
342
343 /* An absolute 16 bit branch, for which bit 10 should be set to
344 indicate that the branch is expected to be taken. The lower two
345 bits must be zero. */
346 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
350 FALSE, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
353 ppc64_elf_brtaken_reloc, /* special_function */
354 "R_PPC64_ADDR14_BRTAKEN",/* name */
355 FALSE, /* partial_inplace */
356 0, /* src_mask */
357 0x0000fffc, /* dst_mask */
358 FALSE), /* pcrel_offset */
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
370 ppc64_elf_brtaken_reloc, /* special_function */
371 "R_PPC64_ADDR14_BRNTAKEN",/* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0x0000fffc, /* dst_mask */
375 FALSE), /* pcrel_offset */
376
377 /* A relative 26 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL24, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 26, /* bitsize */
382 TRUE, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
385 ppc64_elf_branch_reloc, /* special_function */
386 "R_PPC64_REL24", /* name */
387 FALSE, /* partial_inplace */
388 0, /* src_mask */
389 0x03fffffc, /* dst_mask */
390 TRUE), /* pcrel_offset */
391
392 /* A relative 16 bit branch; the lower two bits must be zero. */
393 HOWTO (R_PPC64_REL14, /* type */
394 0, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
397 TRUE, /* pc_relative */
398 0, /* bitpos */
399 complain_overflow_signed, /* complain_on_overflow */
400 ppc64_elf_branch_reloc, /* special_function */
401 "R_PPC64_REL14", /* name */
402 FALSE, /* partial_inplace */
403 0, /* src_mask */
404 0x0000fffc, /* dst_mask */
405 TRUE), /* pcrel_offset */
406
407 /* A relative 16 bit branch. Bit 10 should be set to indicate that
408 the branch is expected to be taken. The lower two bits must be
409 zero. */
410 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
411 0, /* rightshift */
412 2, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
414 TRUE, /* pc_relative */
415 0, /* bitpos */
416 complain_overflow_signed, /* complain_on_overflow */
417 ppc64_elf_brtaken_reloc, /* special_function */
418 "R_PPC64_REL14_BRTAKEN", /* name */
419 FALSE, /* partial_inplace */
420 0, /* src_mask */
421 0x0000fffc, /* dst_mask */
422 TRUE), /* pcrel_offset */
423
424 /* A relative 16 bit branch. Bit 10 should be set to indicate that
425 the branch is not expected to be taken. The lower two bits must
426 be zero. */
427 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
431 TRUE, /* pc_relative */
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
434 ppc64_elf_brtaken_reloc, /* special_function */
435 "R_PPC64_REL14_BRNTAKEN",/* name */
436 FALSE, /* partial_inplace */
437 0, /* src_mask */
438 0x0000fffc, /* dst_mask */
439 TRUE), /* pcrel_offset */
440
441 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
442 symbol. */
443 HOWTO (R_PPC64_GOT16, /* type */
444 0, /* rightshift */
445 1, /* size (0 = byte, 1 = short, 2 = long) */
446 16, /* bitsize */
447 FALSE, /* pc_relative */
448 0, /* bitpos */
449 complain_overflow_signed, /* complain_on_overflow */
450 ppc64_elf_unhandled_reloc, /* special_function */
451 "R_PPC64_GOT16", /* name */
452 FALSE, /* partial_inplace */
453 0, /* src_mask */
454 0xffff, /* dst_mask */
455 FALSE), /* pcrel_offset */
456
457 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
458 the symbol. */
459 HOWTO (R_PPC64_GOT16_LO, /* type */
460 0, /* rightshift */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
463 FALSE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_dont, /* complain_on_overflow */
466 ppc64_elf_unhandled_reloc, /* special_function */
467 "R_PPC64_GOT16_LO", /* name */
468 FALSE, /* partial_inplace */
469 0, /* src_mask */
470 0xffff, /* dst_mask */
471 FALSE), /* pcrel_offset */
472
473 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
474 the symbol. */
475 HOWTO (R_PPC64_GOT16_HI, /* type */
476 16, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont,/* complain_on_overflow */
482 ppc64_elf_unhandled_reloc, /* special_function */
483 "R_PPC64_GOT16_HI", /* name */
484 FALSE, /* partial_inplace */
485 0, /* src_mask */
486 0xffff, /* dst_mask */
487 FALSE), /* pcrel_offset */
488
489 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
490 the symbol. */
491 HOWTO (R_PPC64_GOT16_HA, /* type */
492 16, /* rightshift */
493 1, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
495 FALSE, /* pc_relative */
496 0, /* bitpos */
497 complain_overflow_dont,/* complain_on_overflow */
498 ppc64_elf_unhandled_reloc, /* special_function */
499 "R_PPC64_GOT16_HA", /* name */
500 FALSE, /* partial_inplace */
501 0, /* src_mask */
502 0xffff, /* dst_mask */
503 FALSE), /* pcrel_offset */
504
505 /* This is used only by the dynamic linker. The symbol should exist
506 both in the object being run and in some shared library. The
507 dynamic linker copies the data addressed by the symbol from the
508 shared library into the object, because the object being
509 run has to have the data at some particular address. */
510 HOWTO (R_PPC64_COPY, /* type */
511 0, /* rightshift */
512 0, /* this one is variable size */
513 0, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_dont, /* complain_on_overflow */
517 ppc64_elf_unhandled_reloc, /* special_function */
518 "R_PPC64_COPY", /* name */
519 FALSE, /* partial_inplace */
520 0, /* src_mask */
521 0, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 /* Like R_PPC64_ADDR64, but used when setting global offset table
525 entries. */
526 HOWTO (R_PPC64_GLOB_DAT, /* type */
527 0, /* rightshift */
528 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
529 64, /* bitsize */
530 FALSE, /* pc_relative */
531 0, /* bitpos */
532 complain_overflow_dont, /* complain_on_overflow */
533 ppc64_elf_unhandled_reloc, /* special_function */
534 "R_PPC64_GLOB_DAT", /* name */
535 FALSE, /* partial_inplace */
536 0, /* src_mask */
537 ONES (64), /* dst_mask */
538 FALSE), /* pcrel_offset */
539
540 /* Created by the link editor. Marks a procedure linkage table
541 entry for a symbol. */
542 HOWTO (R_PPC64_JMP_SLOT, /* type */
543 0, /* rightshift */
544 0, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
549 ppc64_elf_unhandled_reloc, /* special_function */
550 "R_PPC64_JMP_SLOT", /* name */
551 FALSE, /* partial_inplace */
552 0, /* src_mask */
553 0, /* dst_mask */
554 FALSE), /* pcrel_offset */
555
556 /* Used only by the dynamic linker. When the object is run, this
557 doubleword64 is set to the load address of the object, plus the
558 addend. */
559 HOWTO (R_PPC64_RELATIVE, /* type */
560 0, /* rightshift */
561 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
562 64, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_dont, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_RELATIVE", /* name */
568 FALSE, /* partial_inplace */
569 0, /* src_mask */
570 ONES (64), /* dst_mask */
571 FALSE), /* pcrel_offset */
572
573 /* Like R_PPC64_ADDR32, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR32, /* type */
575 0, /* rightshift */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
577 32, /* bitsize */
578 FALSE, /* pc_relative */
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_UADDR32", /* name */
583 FALSE, /* partial_inplace */
584 0, /* src_mask */
585 0xffffffff, /* dst_mask */
586 FALSE), /* pcrel_offset */
587
588 /* Like R_PPC64_ADDR16, but may be unaligned. */
589 HOWTO (R_PPC64_UADDR16, /* type */
590 0, /* rightshift */
591 1, /* size (0 = byte, 1 = short, 2 = long) */
592 16, /* bitsize */
593 FALSE, /* pc_relative */
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 bfd_elf_generic_reloc, /* special_function */
597 "R_PPC64_UADDR16", /* name */
598 FALSE, /* partial_inplace */
599 0, /* src_mask */
600 0xffff, /* dst_mask */
601 FALSE), /* pcrel_offset */
602
603 /* 32-bit PC relative. */
604 HOWTO (R_PPC64_REL32, /* type */
605 0, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 32, /* bitsize */
608 TRUE, /* pc_relative */
609 0, /* bitpos */
610 /* FIXME: Verify. Was complain_overflow_bitfield. */
611 complain_overflow_signed, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_PPC64_REL32", /* name */
614 FALSE, /* partial_inplace */
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
617 TRUE), /* pcrel_offset */
618
619 /* 32-bit relocation to the symbol's procedure linkage table. */
620 HOWTO (R_PPC64_PLT32, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 32, /* bitsize */
624 FALSE, /* pc_relative */
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
627 ppc64_elf_unhandled_reloc, /* special_function */
628 "R_PPC64_PLT32", /* name */
629 FALSE, /* partial_inplace */
630 0, /* src_mask */
631 0xffffffff, /* dst_mask */
632 FALSE), /* pcrel_offset */
633
634 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
635 FIXME: R_PPC64_PLTREL32 not supported. */
636 HOWTO (R_PPC64_PLTREL32, /* type */
637 0, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 32, /* bitsize */
640 TRUE, /* pc_relative */
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 "R_PPC64_PLTREL32", /* name */
645 FALSE, /* partial_inplace */
646 0, /* src_mask */
647 0xffffffff, /* dst_mask */
648 TRUE), /* pcrel_offset */
649
650 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
651 the symbol. */
652 HOWTO (R_PPC64_PLT16_LO, /* type */
653 0, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
656 FALSE, /* pc_relative */
657 0, /* bitpos */
658 complain_overflow_dont, /* complain_on_overflow */
659 ppc64_elf_unhandled_reloc, /* special_function */
660 "R_PPC64_PLT16_LO", /* name */
661 FALSE, /* partial_inplace */
662 0, /* src_mask */
663 0xffff, /* dst_mask */
664 FALSE), /* pcrel_offset */
665
666 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
667 the symbol. */
668 HOWTO (R_PPC64_PLT16_HI, /* type */
669 16, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
675 ppc64_elf_unhandled_reloc, /* special_function */
676 "R_PPC64_PLT16_HI", /* name */
677 FALSE, /* partial_inplace */
678 0, /* src_mask */
679 0xffff, /* dst_mask */
680 FALSE), /* pcrel_offset */
681
682 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
683 the symbol. */
684 HOWTO (R_PPC64_PLT16_HA, /* type */
685 16, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
691 ppc64_elf_unhandled_reloc, /* special_function */
692 "R_PPC64_PLT16_HA", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0xffff, /* dst_mask */
696 FALSE), /* pcrel_offset */
697
698 /* 16-bit section relative relocation. */
699 HOWTO (R_PPC64_SECTOFF, /* type */
700 0, /* rightshift */
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
703 FALSE, /* pc_relative */
704 0, /* bitpos */
705 complain_overflow_bitfield, /* complain_on_overflow */
706 ppc64_elf_sectoff_reloc, /* special_function */
707 "R_PPC64_SECTOFF", /* name */
708 FALSE, /* partial_inplace */
709 0, /* src_mask */
710 0xffff, /* dst_mask */
711 FALSE), /* pcrel_offset */
712
713 /* Like R_PPC64_SECTOFF, but no overflow warning. */
714 HOWTO (R_PPC64_SECTOFF_LO, /* type */
715 0, /* rightshift */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
718 FALSE, /* pc_relative */
719 0, /* bitpos */
720 complain_overflow_dont, /* complain_on_overflow */
721 ppc64_elf_sectoff_reloc, /* special_function */
722 "R_PPC64_SECTOFF_LO", /* name */
723 FALSE, /* partial_inplace */
724 0, /* src_mask */
725 0xffff, /* dst_mask */
726 FALSE), /* pcrel_offset */
727
728 /* 16-bit upper half section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HI, /* type */
730 16, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
733 FALSE, /* pc_relative */
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
736 ppc64_elf_sectoff_reloc, /* special_function */
737 "R_PPC64_SECTOFF_HI", /* name */
738 FALSE, /* partial_inplace */
739 0, /* src_mask */
740 0xffff, /* dst_mask */
741 FALSE), /* pcrel_offset */
742
743 /* 16-bit upper half adjusted section relative relocation. */
744 HOWTO (R_PPC64_SECTOFF_HA, /* type */
745 16, /* rightshift */
746 1, /* size (0 = byte, 1 = short, 2 = long) */
747 16, /* bitsize */
748 FALSE, /* pc_relative */
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
751 ppc64_elf_sectoff_ha_reloc, /* special_function */
752 "R_PPC64_SECTOFF_HA", /* name */
753 FALSE, /* partial_inplace */
754 0, /* src_mask */
755 0xffff, /* dst_mask */
756 FALSE), /* pcrel_offset */
757
758 /* Like R_PPC64_REL24 without touching the two least significant bits. */
759 HOWTO (R_PPC64_REL30, /* type */
760 2, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 30, /* bitsize */
763 TRUE, /* pc_relative */
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
767 "R_PPC64_REL30", /* name */
768 FALSE, /* partial_inplace */
769 0, /* src_mask */
770 0xfffffffc, /* dst_mask */
771 TRUE), /* pcrel_offset */
772
773 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
774
775 /* A standard 64-bit relocation. */
776 HOWTO (R_PPC64_ADDR64, /* type */
777 0, /* rightshift */
778 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
779 64, /* bitsize */
780 FALSE, /* pc_relative */
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC64_ADDR64", /* name */
785 FALSE, /* partial_inplace */
786 0, /* src_mask */
787 ONES (64), /* dst_mask */
788 FALSE), /* pcrel_offset */
789
790 /* The bits 32-47 of an address. */
791 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
792 32, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR16_HIGHER", /* name */
800 FALSE, /* partial_inplace */
801 0, /* src_mask */
802 0xffff, /* dst_mask */
803 FALSE), /* pcrel_offset */
804
805 /* The bits 32-47 of an address, plus 1 if the contents of the low
806 16 bits, treated as a signed number, is negative. */
807 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
808 32, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
814 ppc64_elf_ha_reloc, /* special_function */
815 "R_PPC64_ADDR16_HIGHERA", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* The bits 48-63 of an address. */
822 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
823 48, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC64_ADDR16_HIGHEST", /* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0xffff, /* dst_mask */
834 FALSE), /* pcrel_offset */
835
836 /* The bits 48-63 of an address, plus 1 if the contents of the low
837 16 bits, treated as a signed number, is negative. */
838 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
839 48, /* rightshift */
840 1, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 ppc64_elf_ha_reloc, /* special_function */
846 "R_PPC64_ADDR16_HIGHESTA", /* name */
847 FALSE, /* partial_inplace */
848 0, /* src_mask */
849 0xffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
851
852 /* Like ADDR64, but may be unaligned. */
853 HOWTO (R_PPC64_UADDR64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
857 FALSE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_UADDR64", /* name */
862 FALSE, /* partial_inplace */
863 0, /* src_mask */
864 ONES (64), /* dst_mask */
865 FALSE), /* pcrel_offset */
866
867 /* 64-bit relative relocation. */
868 HOWTO (R_PPC64_REL64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
872 TRUE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 "R_PPC64_REL64", /* name */
877 FALSE, /* partial_inplace */
878 0, /* src_mask */
879 ONES (64), /* dst_mask */
880 TRUE), /* pcrel_offset */
881
882 /* 64-bit relocation to the symbol's procedure linkage table. */
883 HOWTO (R_PPC64_PLT64, /* type */
884 0, /* rightshift */
885 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 64, /* bitsize */
887 FALSE, /* pc_relative */
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
890 ppc64_elf_unhandled_reloc, /* special_function */
891 "R_PPC64_PLT64", /* name */
892 FALSE, /* partial_inplace */
893 0, /* src_mask */
894 ONES (64), /* dst_mask */
895 FALSE), /* pcrel_offset */
896
897 /* 64-bit PC relative relocation to the symbol's procedure linkage
898 table. */
899 /* FIXME: R_PPC64_PLTREL64 not supported. */
900 HOWTO (R_PPC64_PLTREL64, /* type */
901 0, /* rightshift */
902 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903 64, /* bitsize */
904 TRUE, /* pc_relative */
905 0, /* bitpos */
906 complain_overflow_dont, /* complain_on_overflow */
907 ppc64_elf_unhandled_reloc, /* special_function */
908 "R_PPC64_PLTREL64", /* name */
909 FALSE, /* partial_inplace */
910 0, /* src_mask */
911 ONES (64), /* dst_mask */
912 TRUE), /* pcrel_offset */
913
914 /* 16 bit TOC-relative relocation. */
915
916 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
917 HOWTO (R_PPC64_TOC16, /* type */
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
921 FALSE, /* pc_relative */
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
924 ppc64_elf_toc_reloc, /* special_function */
925 "R_PPC64_TOC16", /* name */
926 FALSE, /* partial_inplace */
927 0, /* src_mask */
928 0xffff, /* dst_mask */
929 FALSE), /* pcrel_offset */
930
931 /* 16 bit TOC-relative relocation without overflow. */
932
933 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
934 HOWTO (R_PPC64_TOC16_LO, /* type */
935 0, /* rightshift */
936 1, /* size (0 = byte, 1 = short, 2 = long) */
937 16, /* bitsize */
938 FALSE, /* pc_relative */
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
941 ppc64_elf_toc_reloc, /* special_function */
942 "R_PPC64_TOC16_LO", /* name */
943 FALSE, /* partial_inplace */
944 0, /* src_mask */
945 0xffff, /* dst_mask */
946 FALSE), /* pcrel_offset */
947
948 /* 16 bit TOC-relative relocation, high 16 bits. */
949
950 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
951 HOWTO (R_PPC64_TOC16_HI, /* type */
952 16, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
955 FALSE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
958 ppc64_elf_toc_reloc, /* special_function */
959 "R_PPC64_TOC16_HI", /* name */
960 FALSE, /* partial_inplace */
961 0, /* src_mask */
962 0xffff, /* dst_mask */
963 FALSE), /* pcrel_offset */
964
965 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
966 contents of the low 16 bits, treated as a signed number, is
967 negative. */
968
969 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
970 HOWTO (R_PPC64_TOC16_HA, /* type */
971 16, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
974 FALSE, /* pc_relative */
975 0, /* bitpos */
976 complain_overflow_dont, /* complain_on_overflow */
977 ppc64_elf_toc_ha_reloc, /* special_function */
978 "R_PPC64_TOC16_HA", /* name */
979 FALSE, /* partial_inplace */
980 0, /* src_mask */
981 0xffff, /* dst_mask */
982 FALSE), /* pcrel_offset */
983
984 /* 64-bit relocation; insert value of TOC base (.TOC.). */
985
986 /* R_PPC64_TOC 51 doubleword64 .TOC. */
987 HOWTO (R_PPC64_TOC, /* type */
988 0, /* rightshift */
989 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
990 64, /* bitsize */
991 FALSE, /* pc_relative */
992 0, /* bitpos */
993 complain_overflow_bitfield, /* complain_on_overflow */
994 ppc64_elf_toc64_reloc, /* special_function */
995 "R_PPC64_TOC", /* name */
996 FALSE, /* partial_inplace */
997 0, /* src_mask */
998 ONES (64), /* dst_mask */
999 FALSE), /* pcrel_offset */
1000
1001 /* Like R_PPC64_GOT16, but also informs the link editor that the
1002 value to relocate may (!) refer to a PLT entry which the link
1003 editor (a) may replace with the symbol value. If the link editor
1004 is unable to fully resolve the symbol, it may (b) create a PLT
1005 entry and store the address to the new PLT entry in the GOT.
1006 This permits lazy resolution of function symbols at run time.
1007 The link editor may also skip all of this and just (c) emit a
1008 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1009 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1010 HOWTO (R_PPC64_PLTGOT16, /* type */
1011 0, /* rightshift */
1012 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 16, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_signed, /* complain_on_overflow */
1017 ppc64_elf_unhandled_reloc, /* special_function */
1018 "R_PPC64_PLTGOT16", /* name */
1019 FALSE, /* partial_inplace */
1020 0, /* src_mask */
1021 0xffff, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1023
1024 /* Like R_PPC64_PLTGOT16, but without overflow. */
1025 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1026 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont, /* complain_on_overflow */
1033 ppc64_elf_unhandled_reloc, /* special_function */
1034 "R_PPC64_PLTGOT16_LO", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1039
1040 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1041 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1042 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1043 16, /* rightshift */
1044 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 16, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_dont, /* complain_on_overflow */
1049 ppc64_elf_unhandled_reloc, /* special_function */
1050 "R_PPC64_PLTGOT16_HI", /* name */
1051 FALSE, /* partial_inplace */
1052 0, /* src_mask */
1053 0xffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1055
1056 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1057 1 if the contents of the low 16 bits, treated as a signed number,
1058 is negative. */
1059 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1060 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1061 16, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_dont,/* complain_on_overflow */
1067 ppc64_elf_unhandled_reloc, /* special_function */
1068 "R_PPC64_PLTGOT16_HA", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xffff, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1073
1074 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_DS, /* type */
1076 0, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_bitfield, /* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_DS", /* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1088
1089 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont,/* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 "R_PPC64_ADDR16_LO_DS",/* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_signed, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1118
1119 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_GOT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1133
1134 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc64_elf_unhandled_reloc, /* special_function */
1143 "R_PPC64_PLT16_LO_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1148
1149 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_bitfield, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_DS", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1163
1164 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 ppc64_elf_sectoff_reloc, /* special_function */
1173 "R_PPC64_SECTOFF_LO_DS",/* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_signed, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1195 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1196 0, /* rightshift */
1197 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 16, /* bitsize */
1199 FALSE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_dont, /* complain_on_overflow */
1202 ppc64_elf_toc_reloc, /* special_function */
1203 "R_PPC64_TOC16_LO_DS", /* name */
1204 FALSE, /* partial_inplace */
1205 0, /* src_mask */
1206 0xfffc, /* dst_mask */
1207 FALSE), /* pcrel_offset */
1208
1209 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1210 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1211 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1212 0, /* rightshift */
1213 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 16, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_signed, /* complain_on_overflow */
1218 ppc64_elf_unhandled_reloc, /* special_function */
1219 "R_PPC64_PLTGOT16_DS", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0xfffc, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1224
1225 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1226 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1227 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1228 0, /* rightshift */
1229 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 16, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc64_elf_unhandled_reloc, /* special_function */
1235 "R_PPC64_PLTGOT16_LO_DS",/* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0xfffc, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1240
1241 /* Marker relocs for TLS. */
1242 HOWTO (R_PPC64_TLS,
1243 0, /* rightshift */
1244 2, /* size (0 = byte, 1 = short, 2 = long) */
1245 32, /* bitsize */
1246 FALSE, /* pc_relative */
1247 0, /* bitpos */
1248 complain_overflow_dont, /* complain_on_overflow */
1249 bfd_elf_generic_reloc, /* special_function */
1250 "R_PPC64_TLS", /* name */
1251 FALSE, /* partial_inplace */
1252 0, /* src_mask */
1253 0, /* dst_mask */
1254 FALSE), /* pcrel_offset */
1255
1256 HOWTO (R_PPC64_TLSGD,
1257 0, /* rightshift */
1258 2, /* size (0 = byte, 1 = short, 2 = long) */
1259 32, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont, /* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC64_TLSGD", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1269
1270 HOWTO (R_PPC64_TLSLD,
1271 0, /* rightshift */
1272 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 32, /* bitsize */
1274 FALSE, /* pc_relative */
1275 0, /* bitpos */
1276 complain_overflow_dont, /* complain_on_overflow */
1277 bfd_elf_generic_reloc, /* special_function */
1278 "R_PPC64_TLSLD", /* name */
1279 FALSE, /* partial_inplace */
1280 0, /* src_mask */
1281 0, /* dst_mask */
1282 FALSE), /* pcrel_offset */
1283
1284 /* Computes the load module index of the load module that contains the
1285 definition of its TLS sym. */
1286 HOWTO (R_PPC64_DTPMOD64,
1287 0, /* rightshift */
1288 4, /* size (0 = byte, 1 = short, 2 = long) */
1289 64, /* bitsize */
1290 FALSE, /* pc_relative */
1291 0, /* bitpos */
1292 complain_overflow_dont, /* complain_on_overflow */
1293 ppc64_elf_unhandled_reloc, /* special_function */
1294 "R_PPC64_DTPMOD64", /* name */
1295 FALSE, /* partial_inplace */
1296 0, /* src_mask */
1297 ONES (64), /* dst_mask */
1298 FALSE), /* pcrel_offset */
1299
1300 /* Computes a dtv-relative displacement, the difference between the value
1301 of sym+add and the base address of the thread-local storage block that
1302 contains the definition of sym, minus 0x8000. */
1303 HOWTO (R_PPC64_DTPREL64,
1304 0, /* rightshift */
1305 4, /* size (0 = byte, 1 = short, 2 = long) */
1306 64, /* bitsize */
1307 FALSE, /* pc_relative */
1308 0, /* bitpos */
1309 complain_overflow_dont, /* complain_on_overflow */
1310 ppc64_elf_unhandled_reloc, /* special_function */
1311 "R_PPC64_DTPREL64", /* name */
1312 FALSE, /* partial_inplace */
1313 0, /* src_mask */
1314 ONES (64), /* dst_mask */
1315 FALSE), /* pcrel_offset */
1316
1317 /* A 16 bit dtprel reloc. */
1318 HOWTO (R_PPC64_DTPREL16,
1319 0, /* rightshift */
1320 1, /* size (0 = byte, 1 = short, 2 = long) */
1321 16, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_signed, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL16", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0xffff, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* Like DTPREL16, but no overflow. */
1333 HOWTO (R_PPC64_DTPREL16_LO,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_dont, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16_LO", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1348 HOWTO (R_PPC64_DTPREL16_HI,
1349 16, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_HI", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HA,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HA", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HIGHER,
1379 32, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HIGHER", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHERA", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1409 48, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHEST", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16, but for insns with a DS field. */
1438 HOWTO (R_PPC64_DTPREL16_DS,
1439 0, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_signed, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_DS", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xfffc, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16_DS, but no overflow. */
1453 HOWTO (R_PPC64_DTPREL16_LO_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_dont, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_LO_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Computes a tp-relative displacement, the difference between the value of
1468 sym+add and the value of the thread pointer (r13). */
1469 HOWTO (R_PPC64_TPREL64,
1470 0, /* rightshift */
1471 4, /* size (0 = byte, 1 = short, 2 = long) */
1472 64, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 ppc64_elf_unhandled_reloc, /* special_function */
1477 "R_PPC64_TPREL64", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 ONES (64), /* dst_mask */
1481 FALSE), /* pcrel_offset */
1482
1483 /* A 16 bit tprel reloc. */
1484 HOWTO (R_PPC64_TPREL16,
1485 0, /* rightshift */
1486 1, /* size (0 = byte, 1 = short, 2 = long) */
1487 16, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_signed, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL16", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0xffff, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* Like TPREL16, but no overflow. */
1499 HOWTO (R_PPC64_TPREL16_LO,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_dont, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16_LO", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16_LO, but next higher group of 16 bits. */
1514 HOWTO (R_PPC64_TPREL16_HI,
1515 16, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_HI", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_HI, but adjust for low 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HA,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HA", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but next higher group of 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HIGHER,
1545 32, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HIGHER", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHERA,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHERA", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHEST,
1575 48, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHEST", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHESTA", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16, but for insns with a DS field. */
1604 HOWTO (R_PPC64_TPREL16_DS,
1605 0, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_signed, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_DS", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xfffc, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16_DS, but no overflow. */
1619 HOWTO (R_PPC64_TPREL16_LO_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_dont, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_LO_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1634 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1635 to the first entry relative to the TOC base (r2). */
1636 HOWTO (R_PPC64_GOT_TLSGD16,
1637 0, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_signed, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_GOT_TLSGD16", /* name */
1645 FALSE, /* partial_inplace */
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
1648 FALSE), /* pcrel_offset */
1649
1650 /* Like GOT_TLSGD16, but no overflow. */
1651 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_dont, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_GOT_TLSGD16_LO", /* name */
1660 FALSE, /* partial_inplace */
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
1663 FALSE), /* pcrel_offset */
1664
1665 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1667 16, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
1670 FALSE, /* pc_relative */
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
1673 ppc64_elf_unhandled_reloc, /* special_function */
1674 "R_PPC64_GOT_TLSGD16_HI", /* name */
1675 FALSE, /* partial_inplace */
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
1678 FALSE), /* pcrel_offset */
1679
1680 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
1685 FALSE, /* pc_relative */
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
1688 ppc64_elf_unhandled_reloc, /* special_function */
1689 "R_PPC64_GOT_TLSGD16_HA", /* name */
1690 FALSE, /* partial_inplace */
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
1693 FALSE), /* pcrel_offset */
1694
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and zero, and computes the offset to the
1697 first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSLD16,
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
1702 FALSE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSLD16", /* name */
1707 FALSE, /* partial_inplace */
1708 0, /* src_mask */
1709 0xffff, /* dst_mask */
1710 FALSE), /* pcrel_offset */
1711
1712 /* Like GOT_TLSLD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
1717 FALSE, /* pc_relative */
1718 0, /* bitpos */
1719 complain_overflow_dont, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16_LO", /* name */
1722 FALSE, /* partial_inplace */
1723 0, /* src_mask */
1724 0xffff, /* dst_mask */
1725 FALSE), /* pcrel_offset */
1726
1727 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1729 16, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
1732 FALSE, /* pc_relative */
1733 0, /* bitpos */
1734 complain_overflow_dont, /* complain_on_overflow */
1735 ppc64_elf_unhandled_reloc, /* special_function */
1736 "R_PPC64_GOT_TLSLD16_HI", /* name */
1737 FALSE, /* partial_inplace */
1738 0, /* src_mask */
1739 0xffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1741
1742 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1744 16, /* rightshift */
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
1747 FALSE, /* pc_relative */
1748 0, /* bitpos */
1749 complain_overflow_dont, /* complain_on_overflow */
1750 ppc64_elf_unhandled_reloc, /* special_function */
1751 "R_PPC64_GOT_TLSLD16_HA", /* name */
1752 FALSE, /* partial_inplace */
1753 0, /* src_mask */
1754 0xffff, /* dst_mask */
1755 FALSE), /* pcrel_offset */
1756
1757 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1758 the offset to the entry relative to the TOC base (r2). */
1759 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1760 0, /* rightshift */
1761 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 16, /* bitsize */
1763 FALSE, /* pc_relative */
1764 0, /* bitpos */
1765 complain_overflow_signed, /* complain_on_overflow */
1766 ppc64_elf_unhandled_reloc, /* special_function */
1767 "R_PPC64_GOT_DTPREL16_DS", /* name */
1768 FALSE, /* partial_inplace */
1769 0, /* src_mask */
1770 0xfffc, /* dst_mask */
1771 FALSE), /* pcrel_offset */
1772
1773 /* Like GOT_DTPREL16_DS, but no overflow. */
1774 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1775 0, /* rightshift */
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
1778 FALSE, /* pc_relative */
1779 0, /* bitpos */
1780 complain_overflow_dont, /* complain_on_overflow */
1781 ppc64_elf_unhandled_reloc, /* special_function */
1782 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1783 FALSE, /* partial_inplace */
1784 0, /* src_mask */
1785 0xfffc, /* dst_mask */
1786 FALSE), /* pcrel_offset */
1787
1788 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1790 16, /* rightshift */
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
1793 FALSE, /* pc_relative */
1794 0, /* bitpos */
1795 complain_overflow_dont, /* complain_on_overflow */
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_HI", /* name */
1798 FALSE, /* partial_inplace */
1799 0, /* src_mask */
1800 0xffff, /* dst_mask */
1801 FALSE), /* pcrel_offset */
1802
1803 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1805 16, /* rightshift */
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
1808 FALSE, /* pc_relative */
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HA", /* name */
1813 FALSE, /* partial_inplace */
1814 0, /* src_mask */
1815 0xffff, /* dst_mask */
1816 FALSE), /* pcrel_offset */
1817
1818 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1819 offset to the entry relative to the TOC base (r2). */
1820 HOWTO (R_PPC64_GOT_TPREL16_DS,
1821 0, /* rightshift */
1822 1, /* size (0 = byte, 1 = short, 2 = long) */
1823 16, /* bitsize */
1824 FALSE, /* pc_relative */
1825 0, /* bitpos */
1826 complain_overflow_signed, /* complain_on_overflow */
1827 ppc64_elf_unhandled_reloc, /* special_function */
1828 "R_PPC64_GOT_TPREL16_DS", /* name */
1829 FALSE, /* partial_inplace */
1830 0, /* src_mask */
1831 0xfffc, /* dst_mask */
1832 FALSE), /* pcrel_offset */
1833
1834 /* Like GOT_TPREL16_DS, but no overflow. */
1835 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
1839 FALSE, /* pc_relative */
1840 0, /* bitpos */
1841 complain_overflow_dont, /* complain_on_overflow */
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1844 FALSE, /* partial_inplace */
1845 0, /* src_mask */
1846 0xfffc, /* dst_mask */
1847 FALSE), /* pcrel_offset */
1848
1849 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1850 HOWTO (R_PPC64_GOT_TPREL16_HI,
1851 16, /* rightshift */
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
1854 FALSE, /* pc_relative */
1855 0, /* bitpos */
1856 complain_overflow_dont, /* complain_on_overflow */
1857 ppc64_elf_unhandled_reloc, /* special_function */
1858 "R_PPC64_GOT_TPREL16_HI", /* name */
1859 FALSE, /* partial_inplace */
1860 0, /* src_mask */
1861 0xffff, /* dst_mask */
1862 FALSE), /* pcrel_offset */
1863
1864 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HA,
1866 16, /* rightshift */
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
1869 FALSE, /* pc_relative */
1870 0, /* bitpos */
1871 complain_overflow_dont, /* complain_on_overflow */
1872 ppc64_elf_unhandled_reloc, /* special_function */
1873 "R_PPC64_GOT_TPREL16_HA", /* name */
1874 FALSE, /* partial_inplace */
1875 0, /* src_mask */
1876 0xffff, /* dst_mask */
1877 FALSE), /* pcrel_offset */
1878
1879 HOWTO (R_PPC64_JMP_IREL, /* type */
1880 0, /* rightshift */
1881 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1882 0, /* bitsize */
1883 FALSE, /* pc_relative */
1884 0, /* bitpos */
1885 complain_overflow_dont, /* complain_on_overflow */
1886 ppc64_elf_unhandled_reloc, /* special_function */
1887 "R_PPC64_JMP_IREL", /* name */
1888 FALSE, /* partial_inplace */
1889 0, /* src_mask */
1890 0, /* dst_mask */
1891 FALSE), /* pcrel_offset */
1892
1893 HOWTO (R_PPC64_IRELATIVE, /* type */
1894 0, /* rightshift */
1895 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1896 64, /* bitsize */
1897 FALSE, /* pc_relative */
1898 0, /* bitpos */
1899 complain_overflow_dont, /* complain_on_overflow */
1900 bfd_elf_generic_reloc, /* special_function */
1901 "R_PPC64_IRELATIVE", /* name */
1902 FALSE, /* partial_inplace */
1903 0, /* src_mask */
1904 ONES (64), /* dst_mask */
1905 FALSE), /* pcrel_offset */
1906
1907 /* A 16 bit relative relocation. */
1908 HOWTO (R_PPC64_REL16, /* type */
1909 0, /* rightshift */
1910 1, /* size (0 = byte, 1 = short, 2 = long) */
1911 16, /* bitsize */
1912 TRUE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_bitfield, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_REL16", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 0xffff, /* dst_mask */
1920 TRUE), /* pcrel_offset */
1921
1922 /* A 16 bit relative relocation without overflow. */
1923 HOWTO (R_PPC64_REL16_LO, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_dont,/* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16_LO", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* The high order 16 bits of a relative address. */
1938 HOWTO (R_PPC64_REL16_HI, /* type */
1939 16, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont, /* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_HI", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1951
1952 /* The high order 16 bits of a relative address, plus 1 if the contents of
1953 the low 16 bits, treated as a signed number, is negative. */
1954 HOWTO (R_PPC64_REL16_HA, /* type */
1955 16, /* rightshift */
1956 1, /* size (0 = byte, 1 = short, 2 = long) */
1957 16, /* bitsize */
1958 TRUE, /* pc_relative */
1959 0, /* bitpos */
1960 complain_overflow_dont, /* complain_on_overflow */
1961 ppc64_elf_ha_reloc, /* special_function */
1962 "R_PPC64_REL16_HA", /* name */
1963 FALSE, /* partial_inplace */
1964 0, /* src_mask */
1965 0xffff, /* dst_mask */
1966 TRUE), /* pcrel_offset */
1967
1968 /* GNU extension to record C++ vtable hierarchy. */
1969 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1970 0, /* rightshift */
1971 0, /* size (0 = byte, 1 = short, 2 = long) */
1972 0, /* bitsize */
1973 FALSE, /* pc_relative */
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 NULL, /* special_function */
1977 "R_PPC64_GNU_VTINHERIT", /* name */
1978 FALSE, /* partial_inplace */
1979 0, /* src_mask */
1980 0, /* dst_mask */
1981 FALSE), /* pcrel_offset */
1982
1983 /* GNU extension to record C++ vtable member usage. */
1984 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
1988 FALSE, /* pc_relative */
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTENTRY", /* name */
1993 FALSE, /* partial_inplace */
1994 0, /* src_mask */
1995 0, /* dst_mask */
1996 FALSE), /* pcrel_offset */
1997 };
1998
1999 \f
2000 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2001 be done. */
2002
2003 static void
2004 ppc_howto_init (void)
2005 {
2006 unsigned int i, type;
2007
2008 for (i = 0;
2009 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2010 i++)
2011 {
2012 type = ppc64_elf_howto_raw[i].type;
2013 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2014 / sizeof (ppc64_elf_howto_table[0])));
2015 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2016 }
2017 }
2018
2019 static reloc_howto_type *
2020 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2021 bfd_reloc_code_real_type code)
2022 {
2023 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2024
2025 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2026 /* Initialize howto table if needed. */
2027 ppc_howto_init ();
2028
2029 switch (code)
2030 {
2031 default:
2032 return NULL;
2033
2034 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2035 break;
2036 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2037 break;
2038 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2039 break;
2040 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2041 break;
2042 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2043 break;
2044 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2045 break;
2046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2047 break;
2048 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2049 break;
2050 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2051 break;
2052 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2053 break;
2054 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2055 break;
2056 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2057 break;
2058 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2059 break;
2060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2061 break;
2062 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2063 break;
2064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2065 break;
2066 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2067 break;
2068 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2069 break;
2070 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2071 break;
2072 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2073 break;
2074 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2075 break;
2076 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2077 break;
2078 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2079 break;
2080 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2081 break;
2082 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2083 break;
2084 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2085 break;
2086 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2087 break;
2088 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2089 break;
2090 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2091 break;
2092 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2093 break;
2094 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2095 break;
2096 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2097 break;
2098 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2099 break;
2100 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2101 break;
2102 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2103 break;
2104 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2105 break;
2106 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2107 break;
2108 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2109 break;
2110 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2111 break;
2112 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2113 break;
2114 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2115 break;
2116 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2117 break;
2118 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2119 break;
2120 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2121 break;
2122 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2123 break;
2124 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2125 break;
2126 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2127 break;
2128 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2129 break;
2130 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2131 break;
2132 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2133 break;
2134 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2135 break;
2136 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2137 break;
2138 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2139 break;
2140 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2141 break;
2142 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2143 break;
2144 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2145 break;
2146 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2147 break;
2148 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2149 break;
2150 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2151 break;
2152 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2153 break;
2154 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2155 break;
2156 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2157 break;
2158 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2159 break;
2160 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2161 break;
2162 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2163 break;
2164 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2165 break;
2166 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2167 break;
2168 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2169 break;
2170 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2171 break;
2172 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2173 break;
2174 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2175 break;
2176 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2177 break;
2178 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2179 break;
2180 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2181 break;
2182 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2183 break;
2184 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2185 break;
2186 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2187 break;
2188 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2189 break;
2190 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2191 break;
2192 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2193 break;
2194 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2195 break;
2196 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2197 break;
2198 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2199 break;
2200 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2201 break;
2202 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2203 break;
2204 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2205 break;
2206 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2207 break;
2208 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2209 break;
2210 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2211 break;
2212 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2213 break;
2214 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2215 break;
2216 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2217 break;
2218 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2219 break;
2220 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2221 break;
2222 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2223 break;
2224 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2225 break;
2226 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2227 break;
2228 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2229 break;
2230 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2231 break;
2232 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2233 break;
2234 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2235 break;
2236 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2237 break;
2238 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2239 break;
2240 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2241 break;
2242 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2243 break;
2244 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2245 break;
2246 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2247 break;
2248 }
2249
2250 return ppc64_elf_howto_table[r];
2251 };
2252
2253 static reloc_howto_type *
2254 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2255 const char *r_name)
2256 {
2257 unsigned int i;
2258
2259 for (i = 0;
2260 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2261 i++)
2262 if (ppc64_elf_howto_raw[i].name != NULL
2263 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2264 return &ppc64_elf_howto_raw[i];
2265
2266 return NULL;
2267 }
2268
2269 /* Set the howto pointer for a PowerPC ELF reloc. */
2270
2271 static void
2272 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2273 Elf_Internal_Rela *dst)
2274 {
2275 unsigned int type;
2276
2277 /* Initialize howto table if needed. */
2278 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2279 ppc_howto_init ();
2280
2281 type = ELF64_R_TYPE (dst->r_info);
2282 if (type >= (sizeof (ppc64_elf_howto_table)
2283 / sizeof (ppc64_elf_howto_table[0])))
2284 {
2285 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2286 abfd, (int) type);
2287 type = R_PPC64_NONE;
2288 }
2289 cache_ptr->howto = ppc64_elf_howto_table[type];
2290 }
2291
2292 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2293
2294 static bfd_reloc_status_type
2295 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2296 void *data, asection *input_section,
2297 bfd *output_bfd, char **error_message)
2298 {
2299 /* If this is a relocatable link (output_bfd test tells us), just
2300 call the generic function. Any adjustment will be done at final
2301 link time. */
2302 if (output_bfd != NULL)
2303 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2304 input_section, output_bfd, error_message);
2305
2306 /* Adjust the addend for sign extension of the low 16 bits.
2307 We won't actually be using the low 16 bits, so trashing them
2308 doesn't matter. */
2309 reloc_entry->addend += 0x8000;
2310 return bfd_reloc_continue;
2311 }
2312
2313 static bfd_reloc_status_type
2314 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2315 void *data, asection *input_section,
2316 bfd *output_bfd, char **error_message)
2317 {
2318 if (output_bfd != NULL)
2319 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2320 input_section, output_bfd, error_message);
2321
2322 if (strcmp (symbol->section->name, ".opd") == 0
2323 && (symbol->section->owner->flags & DYNAMIC) == 0)
2324 {
2325 bfd_vma dest = opd_entry_value (symbol->section,
2326 symbol->value + reloc_entry->addend,
2327 NULL, NULL);
2328 if (dest != (bfd_vma) -1)
2329 reloc_entry->addend = dest - (symbol->value
2330 + symbol->section->output_section->vma
2331 + symbol->section->output_offset);
2332 }
2333 return bfd_reloc_continue;
2334 }
2335
2336 static bfd_reloc_status_type
2337 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2338 void *data, asection *input_section,
2339 bfd *output_bfd, char **error_message)
2340 {
2341 long insn;
2342 enum elf_ppc64_reloc_type r_type;
2343 bfd_size_type octets;
2344 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2345 bfd_boolean is_power4 = FALSE;
2346
2347 /* If this is a relocatable link (output_bfd test tells us), just
2348 call the generic function. Any adjustment will be done at final
2349 link time. */
2350 if (output_bfd != NULL)
2351 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2352 input_section, output_bfd, error_message);
2353
2354 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2355 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2356 insn &= ~(0x01 << 21);
2357 r_type = reloc_entry->howto->type;
2358 if (r_type == R_PPC64_ADDR14_BRTAKEN
2359 || r_type == R_PPC64_REL14_BRTAKEN)
2360 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2361
2362 if (is_power4)
2363 {
2364 /* Set 'a' bit. This is 0b00010 in BO field for branch
2365 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2366 for branch on CTR insns (BO == 1a00t or 1a01t). */
2367 if ((insn & (0x14 << 21)) == (0x04 << 21))
2368 insn |= 0x02 << 21;
2369 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2370 insn |= 0x08 << 21;
2371 else
2372 goto out;
2373 }
2374 else
2375 {
2376 bfd_vma target = 0;
2377 bfd_vma from;
2378
2379 if (!bfd_is_com_section (symbol->section))
2380 target = symbol->value;
2381 target += symbol->section->output_section->vma;
2382 target += symbol->section->output_offset;
2383 target += reloc_entry->addend;
2384
2385 from = (reloc_entry->address
2386 + input_section->output_offset
2387 + input_section->output_section->vma);
2388
2389 /* Invert 'y' bit if not the default. */
2390 if ((bfd_signed_vma) (target - from) < 0)
2391 insn ^= 0x01 << 21;
2392 }
2393 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2394 out:
2395 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd, error_message);
2397 }
2398
2399 static bfd_reloc_status_type
2400 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2401 void *data, asection *input_section,
2402 bfd *output_bfd, char **error_message)
2403 {
2404 /* If this is a relocatable link (output_bfd test tells us), just
2405 call the generic function. Any adjustment will be done at final
2406 link time. */
2407 if (output_bfd != NULL)
2408 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2409 input_section, output_bfd, error_message);
2410
2411 /* Subtract the symbol section base address. */
2412 reloc_entry->addend -= symbol->section->output_section->vma;
2413 return bfd_reloc_continue;
2414 }
2415
2416 static bfd_reloc_status_type
2417 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2418 void *data, asection *input_section,
2419 bfd *output_bfd, char **error_message)
2420 {
2421 /* If this is a relocatable link (output_bfd test tells us), just
2422 call the generic function. Any adjustment will be done at final
2423 link time. */
2424 if (output_bfd != NULL)
2425 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2426 input_section, output_bfd, error_message);
2427
2428 /* Subtract the symbol section base address. */
2429 reloc_entry->addend -= symbol->section->output_section->vma;
2430
2431 /* Adjust the addend for sign extension of the low 16 bits. */
2432 reloc_entry->addend += 0x8000;
2433 return bfd_reloc_continue;
2434 }
2435
2436 static bfd_reloc_status_type
2437 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2438 void *data, asection *input_section,
2439 bfd *output_bfd, char **error_message)
2440 {
2441 bfd_vma TOCstart;
2442
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2445 link time. */
2446 if (output_bfd != NULL)
2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2448 input_section, output_bfd, error_message);
2449
2450 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2451 if (TOCstart == 0)
2452 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2453
2454 /* Subtract the TOC base address. */
2455 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2456 return bfd_reloc_continue;
2457 }
2458
2459 static bfd_reloc_status_type
2460 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2461 void *data, asection *input_section,
2462 bfd *output_bfd, char **error_message)
2463 {
2464 bfd_vma TOCstart;
2465
2466 /* If this is a relocatable link (output_bfd test tells us), just
2467 call the generic function. Any adjustment will be done at final
2468 link time. */
2469 if (output_bfd != NULL)
2470 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2471 input_section, output_bfd, error_message);
2472
2473 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474 if (TOCstart == 0)
2475 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2476
2477 /* Subtract the TOC base address. */
2478 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2479
2480 /* Adjust the addend for sign extension of the low 16 bits. */
2481 reloc_entry->addend += 0x8000;
2482 return bfd_reloc_continue;
2483 }
2484
2485 static bfd_reloc_status_type
2486 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
2489 {
2490 bfd_vma TOCstart;
2491 bfd_size_type octets;
2492
2493 /* If this is a relocatable link (output_bfd test tells us), just
2494 call the generic function. Any adjustment will be done at final
2495 link time. */
2496 if (output_bfd != NULL)
2497 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2498 input_section, output_bfd, error_message);
2499
2500 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2501 if (TOCstart == 0)
2502 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2503
2504 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2505 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2506 return bfd_reloc_ok;
2507 }
2508
2509 static bfd_reloc_status_type
2510 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2511 void *data, asection *input_section,
2512 bfd *output_bfd, char **error_message)
2513 {
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2519 input_section, output_bfd, error_message);
2520
2521 if (error_message != NULL)
2522 {
2523 static char buf[60];
2524 sprintf (buf, "generic linker can't handle %s",
2525 reloc_entry->howto->name);
2526 *error_message = buf;
2527 }
2528 return bfd_reloc_dangerous;
2529 }
2530
2531 /* Track GOT entries needed for a given symbol. We might need more
2532 than one got entry per symbol. */
2533 struct got_entry
2534 {
2535 struct got_entry *next;
2536
2537 /* The symbol addend that we'll be placing in the GOT. */
2538 bfd_vma addend;
2539
2540 /* Unlike other ELF targets, we use separate GOT entries for the same
2541 symbol referenced from different input files. This is to support
2542 automatic multiple TOC/GOT sections, where the TOC base can vary
2543 from one input file to another. After partitioning into TOC groups
2544 we merge entries within the group.
2545
2546 Point to the BFD owning this GOT entry. */
2547 bfd *owner;
2548
2549 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2550 TLS_TPREL or TLS_DTPREL for tls entries. */
2551 unsigned char tls_type;
2552
2553 /* Non-zero if got.ent points to real entry. */
2554 unsigned char is_indirect;
2555
2556 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2557 union
2558 {
2559 bfd_signed_vma refcount;
2560 bfd_vma offset;
2561 struct got_entry *ent;
2562 } got;
2563 };
2564
2565 /* The same for PLT. */
2566 struct plt_entry
2567 {
2568 struct plt_entry *next;
2569
2570 bfd_vma addend;
2571
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 } plt;
2577 };
2578
2579 struct ppc64_elf_obj_tdata
2580 {
2581 struct elf_obj_tdata elf;
2582
2583 /* Shortcuts to dynamic linker sections. */
2584 asection *got;
2585 asection *relgot;
2586
2587 /* Used during garbage collection. We attach global symbols defined
2588 on removed .opd entries to this section so that the sym is removed. */
2589 asection *deleted_section;
2590
2591 /* TLS local dynamic got entry handling. Support for multiple GOT
2592 sections means we potentially need one of these for each input bfd. */
2593 struct got_entry tlsld_got;
2594
2595 /* A copy of relocs before they are modified for --emit-relocs. */
2596 Elf_Internal_Rela *opd_relocs;
2597
2598 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2599 the reloc to be in the range -32768 to 32767. */
2600 unsigned int has_small_toc_reloc;
2601 };
2602
2603 #define ppc64_elf_tdata(bfd) \
2604 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2605
2606 #define ppc64_tlsld_got(bfd) \
2607 (&ppc64_elf_tdata (bfd)->tlsld_got)
2608
2609 #define is_ppc64_elf(bfd) \
2610 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2611 && elf_object_id (bfd) == PPC64_ELF_DATA)
2612
2613 /* Override the generic function because we store some extras. */
2614
2615 static bfd_boolean
2616 ppc64_elf_mkobject (bfd *abfd)
2617 {
2618 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2619 PPC64_ELF_DATA);
2620 }
2621
2622 /* Fix bad default arch selected for a 64 bit input bfd when the
2623 default is 32 bit. */
2624
2625 static bfd_boolean
2626 ppc64_elf_object_p (bfd *abfd)
2627 {
2628 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2629 {
2630 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2631
2632 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2633 {
2634 /* Relies on arch after 32 bit default being 64 bit default. */
2635 abfd->arch_info = abfd->arch_info->next;
2636 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2637 }
2638 }
2639 return TRUE;
2640 }
2641
2642 /* Support for core dump NOTE sections. */
2643
2644 static bfd_boolean
2645 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2646 {
2647 size_t offset, size;
2648
2649 if (note->descsz != 504)
2650 return FALSE;
2651
2652 /* pr_cursig */
2653 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2654
2655 /* pr_pid */
2656 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
2657
2658 /* pr_reg */
2659 offset = 112;
2660 size = 384;
2661
2662 /* Make a ".reg/999" section. */
2663 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2664 size, note->descpos + offset);
2665 }
2666
2667 static bfd_boolean
2668 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2669 {
2670 if (note->descsz != 136)
2671 return FALSE;
2672
2673 elf_tdata (abfd)->core_pid
2674 = bfd_get_32 (abfd, note->descdata + 24);
2675 elf_tdata (abfd)->core_program
2676 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2677 elf_tdata (abfd)->core_command
2678 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2679
2680 return TRUE;
2681 }
2682
2683 static char *
2684 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2685 ...)
2686 {
2687 switch (note_type)
2688 {
2689 default:
2690 return NULL;
2691
2692 case NT_PRPSINFO:
2693 {
2694 char data[136];
2695 va_list ap;
2696
2697 va_start (ap, note_type);
2698 memset (data, 0, 40);
2699 strncpy (data + 40, va_arg (ap, const char *), 16);
2700 strncpy (data + 56, va_arg (ap, const char *), 80);
2701 va_end (ap);
2702 return elfcore_write_note (abfd, buf, bufsiz,
2703 "CORE", note_type, data, sizeof (data));
2704 }
2705
2706 case NT_PRSTATUS:
2707 {
2708 char data[504];
2709 va_list ap;
2710 long pid;
2711 int cursig;
2712 const void *greg;
2713
2714 va_start (ap, note_type);
2715 memset (data, 0, 112);
2716 pid = va_arg (ap, long);
2717 bfd_put_32 (abfd, pid, data + 32);
2718 cursig = va_arg (ap, int);
2719 bfd_put_16 (abfd, cursig, data + 12);
2720 greg = va_arg (ap, const void *);
2721 memcpy (data + 112, greg, 384);
2722 memset (data + 496, 0, 8);
2723 va_end (ap);
2724 return elfcore_write_note (abfd, buf, bufsiz,
2725 "CORE", note_type, data, sizeof (data));
2726 }
2727 }
2728 }
2729
2730 /* Merge backend specific data from an object file to the output
2731 object file when linking. */
2732
2733 static bfd_boolean
2734 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2735 {
2736 /* Check if we have the same endianness. */
2737 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2738 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2739 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2740 {
2741 const char *msg;
2742
2743 if (bfd_big_endian (ibfd))
2744 msg = _("%B: compiled for a big endian system "
2745 "and target is little endian");
2746 else
2747 msg = _("%B: compiled for a little endian system "
2748 "and target is big endian");
2749
2750 (*_bfd_error_handler) (msg, ibfd);
2751
2752 bfd_set_error (bfd_error_wrong_format);
2753 return FALSE;
2754 }
2755
2756 return TRUE;
2757 }
2758
2759 /* Add extra PPC sections. */
2760
2761 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2762 {
2763 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2764 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2765 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2766 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2767 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2768 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2769 { NULL, 0, 0, 0, 0 }
2770 };
2771
2772 enum _ppc64_sec_type {
2773 sec_normal = 0,
2774 sec_opd = 1,
2775 sec_toc = 2
2776 };
2777
2778 struct _ppc64_elf_section_data
2779 {
2780 struct bfd_elf_section_data elf;
2781
2782 union
2783 {
2784 /* An array with one entry for each opd function descriptor. */
2785 struct _opd_sec_data
2786 {
2787 /* Points to the function code section for local opd entries. */
2788 asection **func_sec;
2789
2790 /* After editing .opd, adjust references to opd local syms. */
2791 long *adjust;
2792 } opd;
2793
2794 /* An array for toc sections, indexed by offset/8. */
2795 struct _toc_sec_data
2796 {
2797 /* Specifies the relocation symbol index used at a given toc offset. */
2798 unsigned *symndx;
2799
2800 /* And the relocation addend. */
2801 bfd_vma *add;
2802 } toc;
2803 } u;
2804
2805 enum _ppc64_sec_type sec_type:2;
2806
2807 /* Flag set when small branches are detected. Used to
2808 select suitable defaults for the stub group size. */
2809 unsigned int has_14bit_branch:1;
2810 };
2811
2812 #define ppc64_elf_section_data(sec) \
2813 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2814
2815 static bfd_boolean
2816 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2817 {
2818 if (!sec->used_by_bfd)
2819 {
2820 struct _ppc64_elf_section_data *sdata;
2821 bfd_size_type amt = sizeof (*sdata);
2822
2823 sdata = bfd_zalloc (abfd, amt);
2824 if (sdata == NULL)
2825 return FALSE;
2826 sec->used_by_bfd = sdata;
2827 }
2828
2829 return _bfd_elf_new_section_hook (abfd, sec);
2830 }
2831
2832 static struct _opd_sec_data *
2833 get_opd_info (asection * sec)
2834 {
2835 if (sec != NULL
2836 && ppc64_elf_section_data (sec) != NULL
2837 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2838 return &ppc64_elf_section_data (sec)->u.opd;
2839 return NULL;
2840 }
2841 \f
2842 /* Parameters for the qsort hook. */
2843 static bfd_boolean synthetic_relocatable;
2844
2845 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2846
2847 static int
2848 compare_symbols (const void *ap, const void *bp)
2849 {
2850 const asymbol *a = * (const asymbol **) ap;
2851 const asymbol *b = * (const asymbol **) bp;
2852
2853 /* Section symbols first. */
2854 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2855 return -1;
2856 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2857 return 1;
2858
2859 /* then .opd symbols. */
2860 if (strcmp (a->section->name, ".opd") == 0
2861 && strcmp (b->section->name, ".opd") != 0)
2862 return -1;
2863 if (strcmp (a->section->name, ".opd") != 0
2864 && strcmp (b->section->name, ".opd") == 0)
2865 return 1;
2866
2867 /* then other code symbols. */
2868 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2869 == (SEC_CODE | SEC_ALLOC)
2870 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2871 != (SEC_CODE | SEC_ALLOC))
2872 return -1;
2873
2874 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2875 != (SEC_CODE | SEC_ALLOC)
2876 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2877 == (SEC_CODE | SEC_ALLOC))
2878 return 1;
2879
2880 if (synthetic_relocatable)
2881 {
2882 if (a->section->id < b->section->id)
2883 return -1;
2884
2885 if (a->section->id > b->section->id)
2886 return 1;
2887 }
2888
2889 if (a->value + a->section->vma < b->value + b->section->vma)
2890 return -1;
2891
2892 if (a->value + a->section->vma > b->value + b->section->vma)
2893 return 1;
2894
2895 /* For syms with the same value, prefer strong dynamic global function
2896 syms over other syms. */
2897 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2898 return -1;
2899
2900 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2901 return 1;
2902
2903 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2904 return -1;
2905
2906 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2907 return 1;
2908
2909 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2910 return -1;
2911
2912 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2913 return 1;
2914
2915 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2916 return -1;
2917
2918 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2919 return 1;
2920
2921 return 0;
2922 }
2923
2924 /* Search SYMS for a symbol of the given VALUE. */
2925
2926 static asymbol *
2927 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2928 {
2929 long mid;
2930
2931 if (id == -1)
2932 {
2933 while (lo < hi)
2934 {
2935 mid = (lo + hi) >> 1;
2936 if (syms[mid]->value + syms[mid]->section->vma < value)
2937 lo = mid + 1;
2938 else if (syms[mid]->value + syms[mid]->section->vma > value)
2939 hi = mid;
2940 else
2941 return syms[mid];
2942 }
2943 }
2944 else
2945 {
2946 while (lo < hi)
2947 {
2948 mid = (lo + hi) >> 1;
2949 if (syms[mid]->section->id < id)
2950 lo = mid + 1;
2951 else if (syms[mid]->section->id > id)
2952 hi = mid;
2953 else if (syms[mid]->value < value)
2954 lo = mid + 1;
2955 else if (syms[mid]->value > value)
2956 hi = mid;
2957 else
2958 return syms[mid];
2959 }
2960 }
2961 return NULL;
2962 }
2963
2964 static bfd_boolean
2965 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2966 {
2967 bfd_vma vma = *(bfd_vma *) ptr;
2968 return ((section->flags & SEC_ALLOC) != 0
2969 && section->vma <= vma
2970 && vma < section->vma + section->size);
2971 }
2972
2973 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2974 entry syms. Also generate @plt symbols for the glink branch table. */
2975
2976 static long
2977 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2978 long static_count, asymbol **static_syms,
2979 long dyn_count, asymbol **dyn_syms,
2980 asymbol **ret)
2981 {
2982 asymbol *s;
2983 long i;
2984 long count;
2985 char *names;
2986 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2987 asection *opd;
2988 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2989 asymbol **syms;
2990
2991 *ret = NULL;
2992
2993 opd = bfd_get_section_by_name (abfd, ".opd");
2994 if (opd == NULL)
2995 return 0;
2996
2997 symcount = static_count;
2998 if (!relocatable)
2999 symcount += dyn_count;
3000 if (symcount == 0)
3001 return 0;
3002
3003 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3004 if (syms == NULL)
3005 return -1;
3006
3007 if (!relocatable && static_count != 0 && dyn_count != 0)
3008 {
3009 /* Use both symbol tables. */
3010 memcpy (syms, static_syms, static_count * sizeof (*syms));
3011 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3012 }
3013 else if (!relocatable && static_count == 0)
3014 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3015 else
3016 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3017
3018 synthetic_relocatable = relocatable;
3019 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3020
3021 if (!relocatable && symcount > 1)
3022 {
3023 long j;
3024 /* Trim duplicate syms, since we may have merged the normal and
3025 dynamic symbols. Actually, we only care about syms that have
3026 different values, so trim any with the same value. */
3027 for (i = 1, j = 1; i < symcount; ++i)
3028 if (syms[i - 1]->value + syms[i - 1]->section->vma
3029 != syms[i]->value + syms[i]->section->vma)
3030 syms[j++] = syms[i];
3031 symcount = j;
3032 }
3033
3034 i = 0;
3035 if (strcmp (syms[i]->section->name, ".opd") == 0)
3036 ++i;
3037 codesecsym = i;
3038
3039 for (; i < symcount; ++i)
3040 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3041 != (SEC_CODE | SEC_ALLOC))
3042 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3043 break;
3044 codesecsymend = i;
3045
3046 for (; i < symcount; ++i)
3047 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3048 break;
3049 secsymend = i;
3050
3051 for (; i < symcount; ++i)
3052 if (strcmp (syms[i]->section->name, ".opd") != 0)
3053 break;
3054 opdsymend = i;
3055
3056 for (; i < symcount; ++i)
3057 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3058 != (SEC_CODE | SEC_ALLOC))
3059 break;
3060 symcount = i;
3061
3062 count = 0;
3063
3064 if (relocatable)
3065 {
3066 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3067 arelent *r;
3068 size_t size;
3069 long relcount;
3070
3071 if (opdsymend == secsymend)
3072 goto done;
3073
3074 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3075 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3076 if (relcount == 0)
3077 goto done;
3078
3079 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3080 {
3081 count = -1;
3082 goto done;
3083 }
3084
3085 size = 0;
3086 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3087 {
3088 asymbol *sym;
3089
3090 while (r < opd->relocation + relcount
3091 && r->address < syms[i]->value + opd->vma)
3092 ++r;
3093
3094 if (r == opd->relocation + relcount)
3095 break;
3096
3097 if (r->address != syms[i]->value + opd->vma)
3098 continue;
3099
3100 if (r->howto->type != R_PPC64_ADDR64)
3101 continue;
3102
3103 sym = *r->sym_ptr_ptr;
3104 if (!sym_exists_at (syms, opdsymend, symcount,
3105 sym->section->id, sym->value + r->addend))
3106 {
3107 ++count;
3108 size += sizeof (asymbol);
3109 size += strlen (syms[i]->name) + 2;
3110 }
3111 }
3112
3113 s = *ret = bfd_malloc (size);
3114 if (s == NULL)
3115 {
3116 count = -1;
3117 goto done;
3118 }
3119
3120 names = (char *) (s + count);
3121
3122 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3123 {
3124 asymbol *sym;
3125
3126 while (r < opd->relocation + relcount
3127 && r->address < syms[i]->value + opd->vma)
3128 ++r;
3129
3130 if (r == opd->relocation + relcount)
3131 break;
3132
3133 if (r->address != syms[i]->value + opd->vma)
3134 continue;
3135
3136 if (r->howto->type != R_PPC64_ADDR64)
3137 continue;
3138
3139 sym = *r->sym_ptr_ptr;
3140 if (!sym_exists_at (syms, opdsymend, symcount,
3141 sym->section->id, sym->value + r->addend))
3142 {
3143 size_t len;
3144
3145 *s = *syms[i];
3146 s->flags |= BSF_SYNTHETIC;
3147 s->section = sym->section;
3148 s->value = sym->value + r->addend;
3149 s->name = names;
3150 *names++ = '.';
3151 len = strlen (syms[i]->name);
3152 memcpy (names, syms[i]->name, len + 1);
3153 names += len + 1;
3154 /* Have udata.p point back to the original symbol this
3155 synthetic symbol was derived from. */
3156 s->udata.p = syms[i];
3157 s++;
3158 }
3159 }
3160 }
3161 else
3162 {
3163 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3164 bfd_byte *contents;
3165 size_t size;
3166 long plt_count = 0;
3167 bfd_vma glink_vma = 0, resolv_vma = 0;
3168 asection *dynamic, *glink = NULL, *relplt = NULL;
3169 arelent *p;
3170
3171 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3172 {
3173 if (contents)
3174 {
3175 free_contents_and_exit:
3176 free (contents);
3177 }
3178 count = -1;
3179 goto done;
3180 }
3181
3182 size = 0;
3183 for (i = secsymend; i < opdsymend; ++i)
3184 {
3185 bfd_vma ent;
3186
3187 /* Ignore bogus symbols. */
3188 if (syms[i]->value > opd->size - 8)
3189 continue;
3190
3191 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3192 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3193 {
3194 ++count;
3195 size += sizeof (asymbol);
3196 size += strlen (syms[i]->name) + 2;
3197 }
3198 }
3199
3200 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3201 if (dyn_count != 0
3202 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3203 {
3204 bfd_byte *dynbuf, *extdyn, *extdynend;
3205 size_t extdynsize;
3206 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3207
3208 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3209 goto free_contents_and_exit;
3210
3211 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3212 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3213
3214 extdyn = dynbuf;
3215 extdynend = extdyn + dynamic->size;
3216 for (; extdyn < extdynend; extdyn += extdynsize)
3217 {
3218 Elf_Internal_Dyn dyn;
3219 (*swap_dyn_in) (abfd, extdyn, &dyn);
3220
3221 if (dyn.d_tag == DT_NULL)
3222 break;
3223
3224 if (dyn.d_tag == DT_PPC64_GLINK)
3225 {
3226 /* The first glink stub starts at offset 32; see comment in
3227 ppc64_elf_finish_dynamic_sections. */
3228 glink_vma = dyn.d_un.d_val + 32;
3229 /* The .glink section usually does not survive the final
3230 link; search for the section (usually .text) where the
3231 glink stubs now reside. */
3232 glink = bfd_sections_find_if (abfd, section_covers_vma,
3233 &glink_vma);
3234 break;
3235 }
3236 }
3237
3238 free (dynbuf);
3239 }
3240
3241 if (glink != NULL)
3242 {
3243 /* Determine __glink trampoline by reading the relative branch
3244 from the first glink stub. */
3245 bfd_byte buf[4];
3246 if (bfd_get_section_contents (abfd, glink, buf,
3247 glink_vma + 4 - glink->vma, 4))
3248 {
3249 unsigned int insn = bfd_get_32 (abfd, buf);
3250 insn ^= B_DOT;
3251 if ((insn & ~0x3fffffc) == 0)
3252 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3253 }
3254
3255 if (resolv_vma)
3256 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3257
3258 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3259 if (relplt != NULL)
3260 {
3261 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3262 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3263 goto free_contents_and_exit;
3264
3265 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3266 size += plt_count * sizeof (asymbol);
3267
3268 p = relplt->relocation;
3269 for (i = 0; i < plt_count; i++, p++)
3270 {
3271 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3272 if (p->addend != 0)
3273 size += sizeof ("+0x") - 1 + 16;
3274 }
3275 }
3276 }
3277
3278 s = *ret = bfd_malloc (size);
3279 if (s == NULL)
3280 goto free_contents_and_exit;
3281
3282 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3283
3284 for (i = secsymend; i < opdsymend; ++i)
3285 {
3286 bfd_vma ent;
3287
3288 if (syms[i]->value > opd->size - 8)
3289 continue;
3290
3291 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3292 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3293 {
3294 long lo, hi;
3295 size_t len;
3296 asection *sec = abfd->sections;
3297
3298 *s = *syms[i];
3299 lo = codesecsym;
3300 hi = codesecsymend;
3301 while (lo < hi)
3302 {
3303 long mid = (lo + hi) >> 1;
3304 if (syms[mid]->section->vma < ent)
3305 lo = mid + 1;
3306 else if (syms[mid]->section->vma > ent)
3307 hi = mid;
3308 else
3309 {
3310 sec = syms[mid]->section;
3311 break;
3312 }
3313 }
3314
3315 if (lo >= hi && lo > codesecsym)
3316 sec = syms[lo - 1]->section;
3317
3318 for (; sec != NULL; sec = sec->next)
3319 {
3320 if (sec->vma > ent)
3321 break;
3322 /* SEC_LOAD may not be set if SEC is from a separate debug
3323 info file. */
3324 if ((sec->flags & SEC_ALLOC) == 0)
3325 break;
3326 if ((sec->flags & SEC_CODE) != 0)
3327 s->section = sec;
3328 }
3329 s->flags |= BSF_SYNTHETIC;
3330 s->value = ent - s->section->vma;
3331 s->name = names;
3332 *names++ = '.';
3333 len = strlen (syms[i]->name);
3334 memcpy (names, syms[i]->name, len + 1);
3335 names += len + 1;
3336 /* Have udata.p point back to the original symbol this
3337 synthetic symbol was derived from. */
3338 s->udata.p = syms[i];
3339 s++;
3340 }
3341 }
3342 free (contents);
3343
3344 if (glink != NULL && relplt != NULL)
3345 {
3346 if (resolv_vma)
3347 {
3348 /* Add a symbol for the main glink trampoline. */
3349 memset (s, 0, sizeof *s);
3350 s->the_bfd = abfd;
3351 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3352 s->section = glink;
3353 s->value = resolv_vma - glink->vma;
3354 s->name = names;
3355 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3356 names += sizeof ("__glink_PLTresolve");
3357 s++;
3358 count++;
3359 }
3360
3361 /* FIXME: It would be very much nicer to put sym@plt on the
3362 stub rather than on the glink branch table entry. The
3363 objdump disassembler would then use a sensible symbol
3364 name on plt calls. The difficulty in doing so is
3365 a) finding the stubs, and,
3366 b) matching stubs against plt entries, and,
3367 c) there can be multiple stubs for a given plt entry.
3368
3369 Solving (a) could be done by code scanning, but older
3370 ppc64 binaries used different stubs to current code.
3371 (b) is the tricky one since you need to known the toc
3372 pointer for at least one function that uses a pic stub to
3373 be able to calculate the plt address referenced.
3374 (c) means gdb would need to set multiple breakpoints (or
3375 find the glink branch itself) when setting breakpoints
3376 for pending shared library loads. */
3377 p = relplt->relocation;
3378 for (i = 0; i < plt_count; i++, p++)
3379 {
3380 size_t len;
3381
3382 *s = **p->sym_ptr_ptr;
3383 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3384 we are defining a symbol, ensure one of them is set. */
3385 if ((s->flags & BSF_LOCAL) == 0)
3386 s->flags |= BSF_GLOBAL;
3387 s->flags |= BSF_SYNTHETIC;
3388 s->section = glink;
3389 s->value = glink_vma - glink->vma;
3390 s->name = names;
3391 s->udata.p = NULL;
3392 len = strlen ((*p->sym_ptr_ptr)->name);
3393 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3394 names += len;
3395 if (p->addend != 0)
3396 {
3397 memcpy (names, "+0x", sizeof ("+0x") - 1);
3398 names += sizeof ("+0x") - 1;
3399 bfd_sprintf_vma (abfd, names, p->addend);
3400 names += strlen (names);
3401 }
3402 memcpy (names, "@plt", sizeof ("@plt"));
3403 names += sizeof ("@plt");
3404 s++;
3405 glink_vma += 8;
3406 if (i >= 0x8000)
3407 glink_vma += 4;
3408 }
3409 count += plt_count;
3410 }
3411 }
3412
3413 done:
3414 free (syms);
3415 return count;
3416 }
3417 \f
3418 /* The following functions are specific to the ELF linker, while
3419 functions above are used generally. Those named ppc64_elf_* are
3420 called by the main ELF linker code. They appear in this file more
3421 or less in the order in which they are called. eg.
3422 ppc64_elf_check_relocs is called early in the link process,
3423 ppc64_elf_finish_dynamic_sections is one of the last functions
3424 called.
3425
3426 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3427 functions have both a function code symbol and a function descriptor
3428 symbol. A call to foo in a relocatable object file looks like:
3429
3430 . .text
3431 . x:
3432 . bl .foo
3433 . nop
3434
3435 The function definition in another object file might be:
3436
3437 . .section .opd
3438 . foo: .quad .foo
3439 . .quad .TOC.@tocbase
3440 . .quad 0
3441 .
3442 . .text
3443 . .foo: blr
3444
3445 When the linker resolves the call during a static link, the branch
3446 unsurprisingly just goes to .foo and the .opd information is unused.
3447 If the function definition is in a shared library, things are a little
3448 different: The call goes via a plt call stub, the opd information gets
3449 copied to the plt, and the linker patches the nop.
3450
3451 . x:
3452 . bl .foo_stub
3453 . ld 2,40(1)
3454 .
3455 .
3456 . .foo_stub:
3457 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
3458 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
3459 . std 2,40(1) # this is the general idea
3460 . ld 11,0(12)
3461 . ld 2,8(12)
3462 . mtctr 11
3463 . ld 11,16(12)
3464 . bctr
3465 .
3466 . .section .plt
3467 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3468
3469 The "reloc ()" notation is supposed to indicate that the linker emits
3470 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3471 copying.
3472
3473 What are the difficulties here? Well, firstly, the relocations
3474 examined by the linker in check_relocs are against the function code
3475 sym .foo, while the dynamic relocation in the plt is emitted against
3476 the function descriptor symbol, foo. Somewhere along the line, we need
3477 to carefully copy dynamic link information from one symbol to the other.
3478 Secondly, the generic part of the elf linker will make .foo a dynamic
3479 symbol as is normal for most other backends. We need foo dynamic
3480 instead, at least for an application final link. However, when
3481 creating a shared library containing foo, we need to have both symbols
3482 dynamic so that references to .foo are satisfied during the early
3483 stages of linking. Otherwise the linker might decide to pull in a
3484 definition from some other object, eg. a static library.
3485
3486 Update: As of August 2004, we support a new convention. Function
3487 calls may use the function descriptor symbol, ie. "bl foo". This
3488 behaves exactly as "bl .foo". */
3489
3490 /* Of those relocs that might be copied as dynamic relocs, this function
3491 selects those that must be copied when linking a shared library,
3492 even when the symbol is local. */
3493
3494 static int
3495 must_be_dyn_reloc (struct bfd_link_info *info,
3496 enum elf_ppc64_reloc_type r_type)
3497 {
3498 switch (r_type)
3499 {
3500 default:
3501 return 1;
3502
3503 case R_PPC64_REL32:
3504 case R_PPC64_REL64:
3505 case R_PPC64_REL30:
3506 return 0;
3507
3508 case R_PPC64_TPREL16:
3509 case R_PPC64_TPREL16_LO:
3510 case R_PPC64_TPREL16_HI:
3511 case R_PPC64_TPREL16_HA:
3512 case R_PPC64_TPREL16_DS:
3513 case R_PPC64_TPREL16_LO_DS:
3514 case R_PPC64_TPREL16_HIGHER:
3515 case R_PPC64_TPREL16_HIGHERA:
3516 case R_PPC64_TPREL16_HIGHEST:
3517 case R_PPC64_TPREL16_HIGHESTA:
3518 case R_PPC64_TPREL64:
3519 return !info->executable;
3520 }
3521 }
3522
3523 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3524 copying dynamic variables from a shared lib into an app's dynbss
3525 section, and instead use a dynamic relocation to point into the
3526 shared lib. With code that gcc generates, it's vital that this be
3527 enabled; In the PowerPC64 ABI, the address of a function is actually
3528 the address of a function descriptor, which resides in the .opd
3529 section. gcc uses the descriptor directly rather than going via the
3530 GOT as some other ABI's do, which means that initialized function
3531 pointers must reference the descriptor. Thus, a function pointer
3532 initialized to the address of a function in a shared library will
3533 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3534 redefines the function descriptor symbol to point to the copy. This
3535 presents a problem as a plt entry for that function is also
3536 initialized from the function descriptor symbol and the copy reloc
3537 may not be initialized first. */
3538 #define ELIMINATE_COPY_RELOCS 1
3539
3540 /* Section name for stubs is the associated section name plus this
3541 string. */
3542 #define STUB_SUFFIX ".stub"
3543
3544 /* Linker stubs.
3545 ppc_stub_long_branch:
3546 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3547 destination, but a 24 bit branch in a stub section will reach.
3548 . b dest
3549
3550 ppc_stub_plt_branch:
3551 Similar to the above, but a 24 bit branch in the stub section won't
3552 reach its destination.
3553 . addis %r12,%r2,xxx@toc@ha
3554 . ld %r11,xxx@toc@l(%r12)
3555 . mtctr %r11
3556 . bctr
3557
3558 ppc_stub_plt_call:
3559 Used to call a function in a shared library. If it so happens that
3560 the plt entry referenced crosses a 64k boundary, then an extra
3561 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
3562 . addis %r12,%r2,xxx@toc@ha
3563 . std %r2,40(%r1)
3564 . ld %r11,xxx+0@toc@l(%r12)
3565 . mtctr %r11
3566 . ld %r2,xxx+8@toc@l(%r12)
3567 . ld %r11,xxx+16@toc@l(%r12)
3568 . bctr
3569
3570 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3571 code to adjust the value and save r2 to support multiple toc sections.
3572 A ppc_stub_long_branch with an r2 offset looks like:
3573 . std %r2,40(%r1)
3574 . addis %r2,%r2,off@ha
3575 . addi %r2,%r2,off@l
3576 . b dest
3577
3578 A ppc_stub_plt_branch with an r2 offset looks like:
3579 . std %r2,40(%r1)
3580 . addis %r12,%r2,xxx@toc@ha
3581 . ld %r11,xxx@toc@l(%r12)
3582 . addis %r2,%r2,off@ha
3583 . addi %r2,%r2,off@l
3584 . mtctr %r11
3585 . bctr
3586
3587 In cases where the "addis" instruction would add zero, the "addis" is
3588 omitted and following instructions modified slightly in some cases.
3589 */
3590
3591 enum ppc_stub_type {
3592 ppc_stub_none,
3593 ppc_stub_long_branch,
3594 ppc_stub_long_branch_r2off,
3595 ppc_stub_plt_branch,
3596 ppc_stub_plt_branch_r2off,
3597 ppc_stub_plt_call
3598 };
3599
3600 struct ppc_stub_hash_entry {
3601
3602 /* Base hash table entry structure. */
3603 struct bfd_hash_entry root;
3604
3605 enum ppc_stub_type stub_type;
3606
3607 /* The stub section. */
3608 asection *stub_sec;
3609
3610 /* Offset within stub_sec of the beginning of this stub. */
3611 bfd_vma stub_offset;
3612
3613 /* Given the symbol's value and its section we can determine its final
3614 value when building the stubs (so the stub knows where to jump. */
3615 bfd_vma target_value;
3616 asection *target_section;
3617
3618 /* The symbol table entry, if any, that this was derived from. */
3619 struct ppc_link_hash_entry *h;
3620 struct plt_entry *plt_ent;
3621
3622 /* And the reloc addend that this was derived from. */
3623 bfd_vma addend;
3624
3625 /* Where this stub is being called from, or, in the case of combined
3626 stub sections, the first input section in the group. */
3627 asection *id_sec;
3628 };
3629
3630 struct ppc_branch_hash_entry {
3631
3632 /* Base hash table entry structure. */
3633 struct bfd_hash_entry root;
3634
3635 /* Offset within branch lookup table. */
3636 unsigned int offset;
3637
3638 /* Generation marker. */
3639 unsigned int iter;
3640 };
3641
3642 struct ppc_link_hash_entry
3643 {
3644 struct elf_link_hash_entry elf;
3645
3646 union {
3647 /* A pointer to the most recently used stub hash entry against this
3648 symbol. */
3649 struct ppc_stub_hash_entry *stub_cache;
3650
3651 /* A pointer to the next symbol starting with a '.' */
3652 struct ppc_link_hash_entry *next_dot_sym;
3653 } u;
3654
3655 /* Track dynamic relocs copied for this symbol. */
3656 struct elf_dyn_relocs *dyn_relocs;
3657
3658 /* Link between function code and descriptor symbols. */
3659 struct ppc_link_hash_entry *oh;
3660
3661 /* Flag function code and descriptor symbols. */
3662 unsigned int is_func:1;
3663 unsigned int is_func_descriptor:1;
3664 unsigned int fake:1;
3665
3666 /* Whether global opd/toc sym has been adjusted or not.
3667 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3668 should be set for all globals defined in any opd/toc section. */
3669 unsigned int adjust_done:1;
3670
3671 /* Set if we twiddled this symbol to weak at some stage. */
3672 unsigned int was_undefined:1;
3673
3674 /* Contexts in which symbol is used in the GOT (or TOC).
3675 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3676 corresponding relocs are encountered during check_relocs.
3677 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3678 indicate the corresponding GOT entry type is not needed.
3679 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3680 a TPREL one. We use a separate flag rather than setting TPREL
3681 just for convenience in distinguishing the two cases. */
3682 #define TLS_GD 1 /* GD reloc. */
3683 #define TLS_LD 2 /* LD reloc. */
3684 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3685 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3686 #define TLS_TLS 16 /* Any TLS reloc. */
3687 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3688 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3689 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3690 unsigned char tls_mask;
3691 };
3692
3693 /* ppc64 ELF linker hash table. */
3694
3695 struct ppc_link_hash_table
3696 {
3697 struct elf_link_hash_table elf;
3698
3699 /* The stub hash table. */
3700 struct bfd_hash_table stub_hash_table;
3701
3702 /* Another hash table for plt_branch stubs. */
3703 struct bfd_hash_table branch_hash_table;
3704
3705 /* Linker stub bfd. */
3706 bfd *stub_bfd;
3707
3708 /* Linker call-backs. */
3709 asection * (*add_stub_section) (const char *, asection *);
3710 void (*layout_sections_again) (void);
3711
3712 /* Array to keep track of which stub sections have been created, and
3713 information on stub grouping. */
3714 struct map_stub {
3715 /* This is the section to which stubs in the group will be attached. */
3716 asection *link_sec;
3717 /* The stub section. */
3718 asection *stub_sec;
3719 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3720 bfd_vma toc_off;
3721 } *stub_group;
3722
3723 /* Temp used when calculating TOC pointers. */
3724 bfd_vma toc_curr;
3725 bfd *toc_bfd;
3726 asection *toc_first_sec;
3727
3728 /* Highest input section id. */
3729 int top_id;
3730
3731 /* Highest output section index. */
3732 int top_index;
3733
3734 /* Used when adding symbols. */
3735 struct ppc_link_hash_entry *dot_syms;
3736
3737 /* List of input sections for each output section. */
3738 asection **input_list;
3739
3740 /* Short-cuts to get to dynamic linker sections. */
3741 asection *got;
3742 asection *plt;
3743 asection *relplt;
3744 asection *iplt;
3745 asection *reliplt;
3746 asection *dynbss;
3747 asection *relbss;
3748 asection *glink;
3749 asection *sfpr;
3750 asection *brlt;
3751 asection *relbrlt;
3752
3753 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3754 struct ppc_link_hash_entry *tls_get_addr;
3755 struct ppc_link_hash_entry *tls_get_addr_fd;
3756
3757 /* The size of reliplt used by got entry relocs. */
3758 bfd_size_type got_reli_size;
3759
3760 /* Statistics. */
3761 unsigned long stub_count[ppc_stub_plt_call];
3762
3763 /* Number of stubs against global syms. */
3764 unsigned long stub_globals;
3765
3766 /* Set if we should emit symbols for stubs. */
3767 unsigned int emit_stub_syms:1;
3768
3769 /* Set if __tls_get_addr optimization should not be done. */
3770 unsigned int no_tls_get_addr_opt:1;
3771
3772 /* Support for multiple toc sections. */
3773 unsigned int do_multi_toc:1;
3774 unsigned int multi_toc_needed:1;
3775 unsigned int second_toc_pass:1;
3776 unsigned int do_toc_opt:1;
3777
3778 /* Set on error. */
3779 unsigned int stub_error:1;
3780
3781 /* Temp used by ppc64_elf_process_dot_syms. */
3782 unsigned int twiddled_syms:1;
3783
3784 /* Incremented every time we size stubs. */
3785 unsigned int stub_iteration;
3786
3787 /* Small local sym cache. */
3788 struct sym_cache sym_cache;
3789 };
3790
3791 /* Rename some of the generic section flags to better document how they
3792 are used here. */
3793
3794 /* Nonzero if this section has TLS related relocations. */
3795 #define has_tls_reloc sec_flg0
3796
3797 /* Nonzero if this section has a call to __tls_get_addr. */
3798 #define has_tls_get_addr_call sec_flg1
3799
3800 /* Nonzero if this section has any toc or got relocs. */
3801 #define has_toc_reloc sec_flg2
3802
3803 /* Nonzero if this section has a call to another section that uses
3804 the toc or got. */
3805 #define makes_toc_func_call sec_flg3
3806
3807 /* Recursion protection when determining above flag. */
3808 #define call_check_in_progress sec_flg4
3809 #define call_check_done sec_flg5
3810
3811 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3812
3813 #define ppc_hash_table(p) \
3814 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3815 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3816
3817 #define ppc_stub_hash_lookup(table, string, create, copy) \
3818 ((struct ppc_stub_hash_entry *) \
3819 bfd_hash_lookup ((table), (string), (create), (copy)))
3820
3821 #define ppc_branch_hash_lookup(table, string, create, copy) \
3822 ((struct ppc_branch_hash_entry *) \
3823 bfd_hash_lookup ((table), (string), (create), (copy)))
3824
3825 /* Create an entry in the stub hash table. */
3826
3827 static struct bfd_hash_entry *
3828 stub_hash_newfunc (struct bfd_hash_entry *entry,
3829 struct bfd_hash_table *table,
3830 const char *string)
3831 {
3832 /* Allocate the structure if it has not already been allocated by a
3833 subclass. */
3834 if (entry == NULL)
3835 {
3836 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3837 if (entry == NULL)
3838 return entry;
3839 }
3840
3841 /* Call the allocation method of the superclass. */
3842 entry = bfd_hash_newfunc (entry, table, string);
3843 if (entry != NULL)
3844 {
3845 struct ppc_stub_hash_entry *eh;
3846
3847 /* Initialize the local fields. */
3848 eh = (struct ppc_stub_hash_entry *) entry;
3849 eh->stub_type = ppc_stub_none;
3850 eh->stub_sec = NULL;
3851 eh->stub_offset = 0;
3852 eh->target_value = 0;
3853 eh->target_section = NULL;
3854 eh->h = NULL;
3855 eh->id_sec = NULL;
3856 }
3857
3858 return entry;
3859 }
3860
3861 /* Create an entry in the branch hash table. */
3862
3863 static struct bfd_hash_entry *
3864 branch_hash_newfunc (struct bfd_hash_entry *entry,
3865 struct bfd_hash_table *table,
3866 const char *string)
3867 {
3868 /* Allocate the structure if it has not already been allocated by a
3869 subclass. */
3870 if (entry == NULL)
3871 {
3872 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3873 if (entry == NULL)
3874 return entry;
3875 }
3876
3877 /* Call the allocation method of the superclass. */
3878 entry = bfd_hash_newfunc (entry, table, string);
3879 if (entry != NULL)
3880 {
3881 struct ppc_branch_hash_entry *eh;
3882
3883 /* Initialize the local fields. */
3884 eh = (struct ppc_branch_hash_entry *) entry;
3885 eh->offset = 0;
3886 eh->iter = 0;
3887 }
3888
3889 return entry;
3890 }
3891
3892 /* Create an entry in a ppc64 ELF linker hash table. */
3893
3894 static struct bfd_hash_entry *
3895 link_hash_newfunc (struct bfd_hash_entry *entry,
3896 struct bfd_hash_table *table,
3897 const char *string)
3898 {
3899 /* Allocate the structure if it has not already been allocated by a
3900 subclass. */
3901 if (entry == NULL)
3902 {
3903 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3904 if (entry == NULL)
3905 return entry;
3906 }
3907
3908 /* Call the allocation method of the superclass. */
3909 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3910 if (entry != NULL)
3911 {
3912 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3913
3914 memset (&eh->u.stub_cache, 0,
3915 (sizeof (struct ppc_link_hash_entry)
3916 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3917
3918 /* When making function calls, old ABI code references function entry
3919 points (dot symbols), while new ABI code references the function
3920 descriptor symbol. We need to make any combination of reference and
3921 definition work together, without breaking archive linking.
3922
3923 For a defined function "foo" and an undefined call to "bar":
3924 An old object defines "foo" and ".foo", references ".bar" (possibly
3925 "bar" too).
3926 A new object defines "foo" and references "bar".
3927
3928 A new object thus has no problem with its undefined symbols being
3929 satisfied by definitions in an old object. On the other hand, the
3930 old object won't have ".bar" satisfied by a new object.
3931
3932 Keep a list of newly added dot-symbols. */
3933
3934 if (string[0] == '.')
3935 {
3936 struct ppc_link_hash_table *htab;
3937
3938 htab = (struct ppc_link_hash_table *) table;
3939 eh->u.next_dot_sym = htab->dot_syms;
3940 htab->dot_syms = eh;
3941 }
3942 }
3943
3944 return entry;
3945 }
3946
3947 /* Create a ppc64 ELF linker hash table. */
3948
3949 static struct bfd_link_hash_table *
3950 ppc64_elf_link_hash_table_create (bfd *abfd)
3951 {
3952 struct ppc_link_hash_table *htab;
3953 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3954
3955 htab = bfd_zmalloc (amt);
3956 if (htab == NULL)
3957 return NULL;
3958
3959 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3960 sizeof (struct ppc_link_hash_entry),
3961 PPC64_ELF_DATA))
3962 {
3963 free (htab);
3964 return NULL;
3965 }
3966
3967 /* Init the stub hash table too. */
3968 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3969 sizeof (struct ppc_stub_hash_entry)))
3970 return NULL;
3971
3972 /* And the branch hash table. */
3973 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3974 sizeof (struct ppc_branch_hash_entry)))
3975 return NULL;
3976
3977 /* Initializing two fields of the union is just cosmetic. We really
3978 only care about glist, but when compiled on a 32-bit host the
3979 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3980 debugger inspection of these fields look nicer. */
3981 htab->elf.init_got_refcount.refcount = 0;
3982 htab->elf.init_got_refcount.glist = NULL;
3983 htab->elf.init_plt_refcount.refcount = 0;
3984 htab->elf.init_plt_refcount.glist = NULL;
3985 htab->elf.init_got_offset.offset = 0;
3986 htab->elf.init_got_offset.glist = NULL;
3987 htab->elf.init_plt_offset.offset = 0;
3988 htab->elf.init_plt_offset.glist = NULL;
3989
3990 return &htab->elf.root;
3991 }
3992
3993 /* Free the derived linker hash table. */
3994
3995 static void
3996 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3997 {
3998 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3999
4000 bfd_hash_table_free (&ret->stub_hash_table);
4001 bfd_hash_table_free (&ret->branch_hash_table);
4002 _bfd_generic_link_hash_table_free (hash);
4003 }
4004
4005 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4006
4007 void
4008 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4009 {
4010 struct ppc_link_hash_table *htab;
4011
4012 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4013
4014 /* Always hook our dynamic sections into the first bfd, which is the
4015 linker created stub bfd. This ensures that the GOT header is at
4016 the start of the output TOC section. */
4017 htab = ppc_hash_table (info);
4018 if (htab == NULL)
4019 return;
4020 htab->stub_bfd = abfd;
4021 htab->elf.dynobj = abfd;
4022 }
4023
4024 /* Build a name for an entry in the stub hash table. */
4025
4026 static char *
4027 ppc_stub_name (const asection *input_section,
4028 const asection *sym_sec,
4029 const struct ppc_link_hash_entry *h,
4030 const Elf_Internal_Rela *rel)
4031 {
4032 char *stub_name;
4033 bfd_size_type len;
4034
4035 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4036 offsets from a sym as a branch target? In fact, we could
4037 probably assume the addend is always zero. */
4038 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4039
4040 if (h)
4041 {
4042 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4043 stub_name = bfd_malloc (len);
4044 if (stub_name == NULL)
4045 return stub_name;
4046
4047 sprintf (stub_name, "%08x.%s+%x",
4048 input_section->id & 0xffffffff,
4049 h->elf.root.root.string,
4050 (int) rel->r_addend & 0xffffffff);
4051 }
4052 else
4053 {
4054 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4055 stub_name = bfd_malloc (len);
4056 if (stub_name == NULL)
4057 return stub_name;
4058
4059 sprintf (stub_name, "%08x.%x:%x+%x",
4060 input_section->id & 0xffffffff,
4061 sym_sec->id & 0xffffffff,
4062 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4063 (int) rel->r_addend & 0xffffffff);
4064 }
4065 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4066 stub_name[len - 2] = 0;
4067 return stub_name;
4068 }
4069
4070 /* Look up an entry in the stub hash. Stub entries are cached because
4071 creating the stub name takes a bit of time. */
4072
4073 static struct ppc_stub_hash_entry *
4074 ppc_get_stub_entry (const asection *input_section,
4075 const asection *sym_sec,
4076 struct ppc_link_hash_entry *h,
4077 const Elf_Internal_Rela *rel,
4078 struct ppc_link_hash_table *htab)
4079 {
4080 struct ppc_stub_hash_entry *stub_entry;
4081 const asection *id_sec;
4082
4083 /* If this input section is part of a group of sections sharing one
4084 stub section, then use the id of the first section in the group.
4085 Stub names need to include a section id, as there may well be
4086 more than one stub used to reach say, printf, and we need to
4087 distinguish between them. */
4088 id_sec = htab->stub_group[input_section->id].link_sec;
4089
4090 if (h != NULL && h->u.stub_cache != NULL
4091 && h->u.stub_cache->h == h
4092 && h->u.stub_cache->id_sec == id_sec)
4093 {
4094 stub_entry = h->u.stub_cache;
4095 }
4096 else
4097 {
4098 char *stub_name;
4099
4100 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4101 if (stub_name == NULL)
4102 return NULL;
4103
4104 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4105 stub_name, FALSE, FALSE);
4106 if (h != NULL)
4107 h->u.stub_cache = stub_entry;
4108
4109 free (stub_name);
4110 }
4111
4112 return stub_entry;
4113 }
4114
4115 /* Add a new stub entry to the stub hash. Not all fields of the new
4116 stub entry are initialised. */
4117
4118 static struct ppc_stub_hash_entry *
4119 ppc_add_stub (const char *stub_name,
4120 asection *section,
4121 struct bfd_link_info *info)
4122 {
4123 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4124 asection *link_sec;
4125 asection *stub_sec;
4126 struct ppc_stub_hash_entry *stub_entry;
4127
4128 link_sec = htab->stub_group[section->id].link_sec;
4129 stub_sec = htab->stub_group[section->id].stub_sec;
4130 if (stub_sec == NULL)
4131 {
4132 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4133 if (stub_sec == NULL)
4134 {
4135 size_t namelen;
4136 bfd_size_type len;
4137 char *s_name;
4138
4139 namelen = strlen (link_sec->name);
4140 len = namelen + sizeof (STUB_SUFFIX);
4141 s_name = bfd_alloc (htab->stub_bfd, len);
4142 if (s_name == NULL)
4143 return NULL;
4144
4145 memcpy (s_name, link_sec->name, namelen);
4146 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4147 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4148 if (stub_sec == NULL)
4149 return NULL;
4150 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4151 }
4152 htab->stub_group[section->id].stub_sec = stub_sec;
4153 }
4154
4155 /* Enter this entry into the linker stub hash table. */
4156 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4157 TRUE, FALSE);
4158 if (stub_entry == NULL)
4159 {
4160 info->callbacks->einfo (_("%B: cannot create stub entry %s\n"),
4161 section->owner, stub_name);
4162 return NULL;
4163 }
4164
4165 stub_entry->stub_sec = stub_sec;
4166 stub_entry->stub_offset = 0;
4167 stub_entry->id_sec = link_sec;
4168 return stub_entry;
4169 }
4170
4171 /* Create sections for linker generated code. */
4172
4173 static bfd_boolean
4174 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4175 {
4176 struct ppc_link_hash_table *htab;
4177 flagword flags;
4178
4179 htab = ppc_hash_table (info);
4180 if (htab == NULL)
4181 return FALSE;
4182
4183 /* Create .sfpr for code to save and restore fp regs. */
4184 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4185 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4186 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4187 flags);
4188 if (htab->sfpr == NULL
4189 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4190 return FALSE;
4191
4192 /* Create .glink for lazy dynamic linking support. */
4193 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4194 flags);
4195 if (htab->glink == NULL
4196 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4197 return FALSE;
4198
4199 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4200 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4201 if (htab->iplt == NULL
4202 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4203 return FALSE;
4204
4205 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4206 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4207 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4208 ".rela.iplt",
4209 flags);
4210 if (htab->reliplt == NULL
4211 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4212 return FALSE;
4213
4214 /* Create branch lookup table for plt_branch stubs. */
4215 flags = (SEC_ALLOC | SEC_LOAD
4216 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4217 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4218 flags);
4219 if (htab->brlt == NULL
4220 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4221 return FALSE;
4222
4223 if (!info->shared)
4224 return TRUE;
4225
4226 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4227 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4228 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4229 ".rela.branch_lt",
4230 flags);
4231 if (htab->relbrlt == NULL
4232 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4233 return FALSE;
4234
4235 return TRUE;
4236 }
4237
4238 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4239 not already done. */
4240
4241 static bfd_boolean
4242 create_got_section (bfd *abfd, struct bfd_link_info *info)
4243 {
4244 asection *got, *relgot;
4245 flagword flags;
4246 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4247
4248 if (!is_ppc64_elf (abfd))
4249 return FALSE;
4250 if (htab == NULL)
4251 return FALSE;
4252
4253 if (!htab->got)
4254 {
4255 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4256 return FALSE;
4257
4258 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4259 if (!htab->got)
4260 abort ();
4261 }
4262
4263 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4264 | SEC_LINKER_CREATED);
4265
4266 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4267 if (!got
4268 || !bfd_set_section_alignment (abfd, got, 3))
4269 return FALSE;
4270
4271 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4272 flags | SEC_READONLY);
4273 if (!relgot
4274 || ! bfd_set_section_alignment (abfd, relgot, 3))
4275 return FALSE;
4276
4277 ppc64_elf_tdata (abfd)->got = got;
4278 ppc64_elf_tdata (abfd)->relgot = relgot;
4279 return TRUE;
4280 }
4281
4282 /* Create the dynamic sections, and set up shortcuts. */
4283
4284 static bfd_boolean
4285 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4286 {
4287 struct ppc_link_hash_table *htab;
4288
4289 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4290 return FALSE;
4291
4292 htab = ppc_hash_table (info);
4293 if (htab == NULL)
4294 return FALSE;
4295
4296 if (!htab->got)
4297 htab->got = bfd_get_section_by_name (dynobj, ".got");
4298 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4299 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4300 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
4301 if (!info->shared)
4302 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
4303
4304 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4305 || (!info->shared && !htab->relbss))
4306 abort ();
4307
4308 return TRUE;
4309 }
4310
4311 /* Follow indirect and warning symbol links. */
4312
4313 static inline struct bfd_link_hash_entry *
4314 follow_link (struct bfd_link_hash_entry *h)
4315 {
4316 while (h->type == bfd_link_hash_indirect
4317 || h->type == bfd_link_hash_warning)
4318 h = h->u.i.link;
4319 return h;
4320 }
4321
4322 static inline struct elf_link_hash_entry *
4323 elf_follow_link (struct elf_link_hash_entry *h)
4324 {
4325 return (struct elf_link_hash_entry *) follow_link (&h->root);
4326 }
4327
4328 static inline struct ppc_link_hash_entry *
4329 ppc_follow_link (struct ppc_link_hash_entry *h)
4330 {
4331 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4332 }
4333
4334 /* Merge PLT info on FROM with that on TO. */
4335
4336 static void
4337 move_plt_plist (struct ppc_link_hash_entry *from,
4338 struct ppc_link_hash_entry *to)
4339 {
4340 if (from->elf.plt.plist != NULL)
4341 {
4342 if (to->elf.plt.plist != NULL)
4343 {
4344 struct plt_entry **entp;
4345 struct plt_entry *ent;
4346
4347 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4348 {
4349 struct plt_entry *dent;
4350
4351 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4352 if (dent->addend == ent->addend)
4353 {
4354 dent->plt.refcount += ent->plt.refcount;
4355 *entp = ent->next;
4356 break;
4357 }
4358 if (dent == NULL)
4359 entp = &ent->next;
4360 }
4361 *entp = to->elf.plt.plist;
4362 }
4363
4364 to->elf.plt.plist = from->elf.plt.plist;
4365 from->elf.plt.plist = NULL;
4366 }
4367 }
4368
4369 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4370
4371 static void
4372 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4373 struct elf_link_hash_entry *dir,
4374 struct elf_link_hash_entry *ind)
4375 {
4376 struct ppc_link_hash_entry *edir, *eind;
4377
4378 edir = (struct ppc_link_hash_entry *) dir;
4379 eind = (struct ppc_link_hash_entry *) ind;
4380
4381 /* Copy over any dynamic relocs we may have on the indirect sym. */
4382 if (eind->dyn_relocs != NULL)
4383 {
4384 if (edir->dyn_relocs != NULL)
4385 {
4386 struct elf_dyn_relocs **pp;
4387 struct elf_dyn_relocs *p;
4388
4389 /* Add reloc counts against the indirect sym to the direct sym
4390 list. Merge any entries against the same section. */
4391 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4392 {
4393 struct elf_dyn_relocs *q;
4394
4395 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4396 if (q->sec == p->sec)
4397 {
4398 q->pc_count += p->pc_count;
4399 q->count += p->count;
4400 *pp = p->next;
4401 break;
4402 }
4403 if (q == NULL)
4404 pp = &p->next;
4405 }
4406 *pp = edir->dyn_relocs;
4407 }
4408
4409 edir->dyn_relocs = eind->dyn_relocs;
4410 eind->dyn_relocs = NULL;
4411 }
4412
4413 edir->is_func |= eind->is_func;
4414 edir->is_func_descriptor |= eind->is_func_descriptor;
4415 edir->tls_mask |= eind->tls_mask;
4416 if (eind->oh != NULL)
4417 edir->oh = ppc_follow_link (eind->oh);
4418
4419 /* If called to transfer flags for a weakdef during processing
4420 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4421 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4422 if (!(ELIMINATE_COPY_RELOCS
4423 && eind->elf.root.type != bfd_link_hash_indirect
4424 && edir->elf.dynamic_adjusted))
4425 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4426
4427 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4428 edir->elf.ref_regular |= eind->elf.ref_regular;
4429 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4430 edir->elf.needs_plt |= eind->elf.needs_plt;
4431
4432 /* If we were called to copy over info for a weak sym, that's all. */
4433 if (eind->elf.root.type != bfd_link_hash_indirect)
4434 return;
4435
4436 /* Copy over got entries that we may have already seen to the
4437 symbol which just became indirect. */
4438 if (eind->elf.got.glist != NULL)
4439 {
4440 if (edir->elf.got.glist != NULL)
4441 {
4442 struct got_entry **entp;
4443 struct got_entry *ent;
4444
4445 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4446 {
4447 struct got_entry *dent;
4448
4449 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4450 if (dent->addend == ent->addend
4451 && dent->owner == ent->owner
4452 && dent->tls_type == ent->tls_type)
4453 {
4454 dent->got.refcount += ent->got.refcount;
4455 *entp = ent->next;
4456 break;
4457 }
4458 if (dent == NULL)
4459 entp = &ent->next;
4460 }
4461 *entp = edir->elf.got.glist;
4462 }
4463
4464 edir->elf.got.glist = eind->elf.got.glist;
4465 eind->elf.got.glist = NULL;
4466 }
4467
4468 /* And plt entries. */
4469 move_plt_plist (eind, edir);
4470
4471 if (eind->elf.dynindx != -1)
4472 {
4473 if (edir->elf.dynindx != -1)
4474 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4475 edir->elf.dynstr_index);
4476 edir->elf.dynindx = eind->elf.dynindx;
4477 edir->elf.dynstr_index = eind->elf.dynstr_index;
4478 eind->elf.dynindx = -1;
4479 eind->elf.dynstr_index = 0;
4480 }
4481 }
4482
4483 /* Find the function descriptor hash entry from the given function code
4484 hash entry FH. Link the entries via their OH fields. */
4485
4486 static struct ppc_link_hash_entry *
4487 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4488 {
4489 struct ppc_link_hash_entry *fdh = fh->oh;
4490
4491 if (fdh == NULL)
4492 {
4493 const char *fd_name = fh->elf.root.root.string + 1;
4494
4495 fdh = (struct ppc_link_hash_entry *)
4496 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4497 if (fdh == NULL)
4498 return fdh;
4499
4500 fdh->is_func_descriptor = 1;
4501 fdh->oh = fh;
4502 fh->is_func = 1;
4503 fh->oh = fdh;
4504 }
4505
4506 return ppc_follow_link (fdh);
4507 }
4508
4509 /* Make a fake function descriptor sym for the code sym FH. */
4510
4511 static struct ppc_link_hash_entry *
4512 make_fdh (struct bfd_link_info *info,
4513 struct ppc_link_hash_entry *fh)
4514 {
4515 bfd *abfd;
4516 asymbol *newsym;
4517 struct bfd_link_hash_entry *bh;
4518 struct ppc_link_hash_entry *fdh;
4519
4520 abfd = fh->elf.root.u.undef.abfd;
4521 newsym = bfd_make_empty_symbol (abfd);
4522 newsym->name = fh->elf.root.root.string + 1;
4523 newsym->section = bfd_und_section_ptr;
4524 newsym->value = 0;
4525 newsym->flags = BSF_WEAK;
4526
4527 bh = NULL;
4528 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4529 newsym->flags, newsym->section,
4530 newsym->value, NULL, FALSE, FALSE,
4531 &bh))
4532 return NULL;
4533
4534 fdh = (struct ppc_link_hash_entry *) bh;
4535 fdh->elf.non_elf = 0;
4536 fdh->fake = 1;
4537 fdh->is_func_descriptor = 1;
4538 fdh->oh = fh;
4539 fh->is_func = 1;
4540 fh->oh = fdh;
4541 return fdh;
4542 }
4543
4544 /* Fix function descriptor symbols defined in .opd sections to be
4545 function type. */
4546
4547 static bfd_boolean
4548 ppc64_elf_add_symbol_hook (bfd *ibfd,
4549 struct bfd_link_info *info,
4550 Elf_Internal_Sym *isym,
4551 const char **name ATTRIBUTE_UNUSED,
4552 flagword *flags ATTRIBUTE_UNUSED,
4553 asection **sec,
4554 bfd_vma *value ATTRIBUTE_UNUSED)
4555 {
4556 if ((ibfd->flags & DYNAMIC) == 0
4557 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4558 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4559
4560 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4561 {
4562 if ((ibfd->flags & DYNAMIC) == 0)
4563 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4564 }
4565 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4566 ;
4567 else if (*sec != NULL
4568 && strcmp ((*sec)->name, ".opd") == 0)
4569 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4570
4571 return TRUE;
4572 }
4573
4574 /* This function makes an old ABI object reference to ".bar" cause the
4575 inclusion of a new ABI object archive that defines "bar".
4576 NAME is a symbol defined in an archive. Return a symbol in the hash
4577 table that might be satisfied by the archive symbols. */
4578
4579 static struct elf_link_hash_entry *
4580 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4581 struct bfd_link_info *info,
4582 const char *name)
4583 {
4584 struct elf_link_hash_entry *h;
4585 char *dot_name;
4586 size_t len;
4587
4588 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4589 if (h != NULL
4590 /* Don't return this sym if it is a fake function descriptor
4591 created by add_symbol_adjust. */
4592 && !(h->root.type == bfd_link_hash_undefweak
4593 && ((struct ppc_link_hash_entry *) h)->fake))
4594 return h;
4595
4596 if (name[0] == '.')
4597 return h;
4598
4599 len = strlen (name);
4600 dot_name = bfd_alloc (abfd, len + 2);
4601 if (dot_name == NULL)
4602 return (struct elf_link_hash_entry *) 0 - 1;
4603 dot_name[0] = '.';
4604 memcpy (dot_name + 1, name, len + 1);
4605 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4606 bfd_release (abfd, dot_name);
4607 return h;
4608 }
4609
4610 /* This function satisfies all old ABI object references to ".bar" if a
4611 new ABI object defines "bar". Well, at least, undefined dot symbols
4612 are made weak. This stops later archive searches from including an
4613 object if we already have a function descriptor definition. It also
4614 prevents the linker complaining about undefined symbols.
4615 We also check and correct mismatched symbol visibility here. The
4616 most restrictive visibility of the function descriptor and the
4617 function entry symbol is used. */
4618
4619 static bfd_boolean
4620 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4621 {
4622 struct ppc_link_hash_table *htab;
4623 struct ppc_link_hash_entry *fdh;
4624
4625 if (eh->elf.root.type == bfd_link_hash_indirect)
4626 return TRUE;
4627
4628 if (eh->elf.root.type == bfd_link_hash_warning)
4629 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4630
4631 if (eh->elf.root.root.string[0] != '.')
4632 abort ();
4633
4634 htab = ppc_hash_table (info);
4635 if (htab == NULL)
4636 return FALSE;
4637
4638 fdh = lookup_fdh (eh, htab);
4639 if (fdh == NULL)
4640 {
4641 if (!info->relocatable
4642 && (eh->elf.root.type == bfd_link_hash_undefined
4643 || eh->elf.root.type == bfd_link_hash_undefweak)
4644 && eh->elf.ref_regular)
4645 {
4646 /* Make an undefweak function descriptor sym, which is enough to
4647 pull in an --as-needed shared lib, but won't cause link
4648 errors. Archives are handled elsewhere. */
4649 fdh = make_fdh (info, eh);
4650 if (fdh == NULL)
4651 return FALSE;
4652 fdh->elf.ref_regular = 1;
4653 }
4654 }
4655 else
4656 {
4657 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4658 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4659 if (entry_vis < descr_vis)
4660 fdh->elf.other += entry_vis - descr_vis;
4661 else if (entry_vis > descr_vis)
4662 eh->elf.other += descr_vis - entry_vis;
4663
4664 if ((fdh->elf.root.type == bfd_link_hash_defined
4665 || fdh->elf.root.type == bfd_link_hash_defweak)
4666 && eh->elf.root.type == bfd_link_hash_undefined)
4667 {
4668 eh->elf.root.type = bfd_link_hash_undefweak;
4669 eh->was_undefined = 1;
4670 htab->twiddled_syms = 1;
4671 }
4672 }
4673
4674 return TRUE;
4675 }
4676
4677 /* Process list of dot-symbols we made in link_hash_newfunc. */
4678
4679 static bfd_boolean
4680 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4681 {
4682 struct ppc_link_hash_table *htab;
4683 struct ppc_link_hash_entry **p, *eh;
4684
4685 if (!is_ppc64_elf (info->output_bfd))
4686 return TRUE;
4687 htab = ppc_hash_table (info);
4688 if (htab == NULL)
4689 return FALSE;
4690
4691 if (is_ppc64_elf (ibfd))
4692 {
4693 p = &htab->dot_syms;
4694 while ((eh = *p) != NULL)
4695 {
4696 *p = NULL;
4697 if (!add_symbol_adjust (eh, info))
4698 return FALSE;
4699 p = &eh->u.next_dot_sym;
4700 }
4701 }
4702
4703 /* Clear the list for non-ppc64 input files. */
4704 p = &htab->dot_syms;
4705 while ((eh = *p) != NULL)
4706 {
4707 *p = NULL;
4708 p = &eh->u.next_dot_sym;
4709 }
4710
4711 /* We need to fix the undefs list for any syms we have twiddled to
4712 undef_weak. */
4713 if (htab->twiddled_syms)
4714 {
4715 bfd_link_repair_undef_list (&htab->elf.root);
4716 htab->twiddled_syms = 0;
4717 }
4718 return TRUE;
4719 }
4720
4721 /* Undo hash table changes when an --as-needed input file is determined
4722 not to be needed. */
4723
4724 static bfd_boolean
4725 ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4726 struct bfd_link_info *info)
4727 {
4728 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4729
4730 if (htab == NULL)
4731 return FALSE;
4732
4733 htab->dot_syms = NULL;
4734 return TRUE;
4735 }
4736
4737 /* If --just-symbols against a final linked binary, then assume we need
4738 toc adjusting stubs when calling functions defined there. */
4739
4740 static void
4741 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4742 {
4743 if ((sec->flags & SEC_CODE) != 0
4744 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4745 && is_ppc64_elf (sec->owner))
4746 {
4747 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4748 if (got != NULL
4749 && got->size >= elf_backend_got_header_size
4750 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4751 sec->has_toc_reloc = 1;
4752 }
4753 _bfd_elf_link_just_syms (sec, info);
4754 }
4755
4756 static struct plt_entry **
4757 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4758 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4759 {
4760 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4761 struct plt_entry **local_plt;
4762 unsigned char *local_got_tls_masks;
4763
4764 if (local_got_ents == NULL)
4765 {
4766 bfd_size_type size = symtab_hdr->sh_info;
4767
4768 size *= (sizeof (*local_got_ents)
4769 + sizeof (*local_plt)
4770 + sizeof (*local_got_tls_masks));
4771 local_got_ents = bfd_zalloc (abfd, size);
4772 if (local_got_ents == NULL)
4773 return NULL;
4774 elf_local_got_ents (abfd) = local_got_ents;
4775 }
4776
4777 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
4778 {
4779 struct got_entry *ent;
4780
4781 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4782 if (ent->addend == r_addend
4783 && ent->owner == abfd
4784 && ent->tls_type == tls_type)
4785 break;
4786 if (ent == NULL)
4787 {
4788 bfd_size_type amt = sizeof (*ent);
4789 ent = bfd_alloc (abfd, amt);
4790 if (ent == NULL)
4791 return FALSE;
4792 ent->next = local_got_ents[r_symndx];
4793 ent->addend = r_addend;
4794 ent->owner = abfd;
4795 ent->tls_type = tls_type;
4796 ent->is_indirect = FALSE;
4797 ent->got.refcount = 0;
4798 local_got_ents[r_symndx] = ent;
4799 }
4800 ent->got.refcount += 1;
4801 }
4802
4803 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4804 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4805 local_got_tls_masks[r_symndx] |= tls_type;
4806
4807 return local_plt + r_symndx;
4808 }
4809
4810 static bfd_boolean
4811 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4812 {
4813 struct plt_entry *ent;
4814
4815 for (ent = *plist; ent != NULL; ent = ent->next)
4816 if (ent->addend == addend)
4817 break;
4818 if (ent == NULL)
4819 {
4820 bfd_size_type amt = sizeof (*ent);
4821 ent = bfd_alloc (abfd, amt);
4822 if (ent == NULL)
4823 return FALSE;
4824 ent->next = *plist;
4825 ent->addend = addend;
4826 ent->plt.refcount = 0;
4827 *plist = ent;
4828 }
4829 ent->plt.refcount += 1;
4830 return TRUE;
4831 }
4832
4833 static bfd_boolean
4834 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4835 {
4836 return (r_type == R_PPC64_REL24
4837 || r_type == R_PPC64_REL14
4838 || r_type == R_PPC64_REL14_BRTAKEN
4839 || r_type == R_PPC64_REL14_BRNTAKEN
4840 || r_type == R_PPC64_ADDR24
4841 || r_type == R_PPC64_ADDR14
4842 || r_type == R_PPC64_ADDR14_BRTAKEN
4843 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4844 }
4845
4846 /* Look through the relocs for a section during the first phase, and
4847 calculate needed space in the global offset table, procedure
4848 linkage table, and dynamic reloc sections. */
4849
4850 static bfd_boolean
4851 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4852 asection *sec, const Elf_Internal_Rela *relocs)
4853 {
4854 struct ppc_link_hash_table *htab;
4855 Elf_Internal_Shdr *symtab_hdr;
4856 struct elf_link_hash_entry **sym_hashes;
4857 const Elf_Internal_Rela *rel;
4858 const Elf_Internal_Rela *rel_end;
4859 asection *sreloc;
4860 asection **opd_sym_map;
4861 struct elf_link_hash_entry *tga, *dottga;
4862
4863 if (info->relocatable)
4864 return TRUE;
4865
4866 /* Don't do anything special with non-loaded, non-alloced sections.
4867 In particular, any relocs in such sections should not affect GOT
4868 and PLT reference counting (ie. we don't allow them to create GOT
4869 or PLT entries), there's no possibility or desire to optimize TLS
4870 relocs, and there's not much point in propagating relocs to shared
4871 libs that the dynamic linker won't relocate. */
4872 if ((sec->flags & SEC_ALLOC) == 0)
4873 return TRUE;
4874
4875 BFD_ASSERT (is_ppc64_elf (abfd));
4876
4877 htab = ppc_hash_table (info);
4878 if (htab == NULL)
4879 return FALSE;
4880
4881 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4882 FALSE, FALSE, TRUE);
4883 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4884 FALSE, FALSE, TRUE);
4885 symtab_hdr = &elf_symtab_hdr (abfd);
4886 sym_hashes = elf_sym_hashes (abfd);
4887 sreloc = NULL;
4888 opd_sym_map = NULL;
4889 if (strcmp (sec->name, ".opd") == 0)
4890 {
4891 /* Garbage collection needs some extra help with .opd sections.
4892 We don't want to necessarily keep everything referenced by
4893 relocs in .opd, as that would keep all functions. Instead,
4894 if we reference an .opd symbol (a function descriptor), we
4895 want to keep the function code symbol's section. This is
4896 easy for global symbols, but for local syms we need to keep
4897 information about the associated function section. */
4898 bfd_size_type amt;
4899
4900 amt = sec->size * sizeof (*opd_sym_map) / 8;
4901 opd_sym_map = bfd_zalloc (abfd, amt);
4902 if (opd_sym_map == NULL)
4903 return FALSE;
4904 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
4905 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4906 ppc64_elf_section_data (sec)->sec_type = sec_opd;
4907 }
4908
4909 if (htab->sfpr == NULL
4910 && !create_linkage_sections (htab->elf.dynobj, info))
4911 return FALSE;
4912
4913 rel_end = relocs + sec->reloc_count;
4914 for (rel = relocs; rel < rel_end; rel++)
4915 {
4916 unsigned long r_symndx;
4917 struct elf_link_hash_entry *h;
4918 enum elf_ppc64_reloc_type r_type;
4919 int tls_type;
4920 struct _ppc64_elf_section_data *ppc64_sec;
4921 struct plt_entry **ifunc;
4922
4923 r_symndx = ELF64_R_SYM (rel->r_info);
4924 if (r_symndx < symtab_hdr->sh_info)
4925 h = NULL;
4926 else
4927 {
4928 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4929 h = elf_follow_link (h);
4930 }
4931
4932 tls_type = 0;
4933 ifunc = NULL;
4934 if (h != NULL)
4935 {
4936 if (h->type == STT_GNU_IFUNC)
4937 {
4938 h->needs_plt = 1;
4939 ifunc = &h->plt.plist;
4940 }
4941 }
4942 else
4943 {
4944 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4945 abfd, r_symndx);
4946 if (isym == NULL)
4947 return FALSE;
4948
4949 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4950 {
4951 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4952 rel->r_addend, PLT_IFUNC);
4953 if (ifunc == NULL)
4954 return FALSE;
4955 }
4956 }
4957 r_type = ELF64_R_TYPE (rel->r_info);
4958 if (is_branch_reloc (r_type))
4959 {
4960 if (h != NULL && (h == tga || h == dottga))
4961 {
4962 if (rel != relocs
4963 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4964 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4965 /* We have a new-style __tls_get_addr call with a marker
4966 reloc. */
4967 ;
4968 else
4969 /* Mark this section as having an old-style call. */
4970 sec->has_tls_get_addr_call = 1;
4971 }
4972
4973 /* STT_GNU_IFUNC symbols must have a PLT entry. */
4974 if (ifunc != NULL
4975 && !update_plt_info (abfd, ifunc, rel->r_addend))
4976 return FALSE;
4977 }
4978
4979 switch (r_type)
4980 {
4981 case R_PPC64_TLSGD:
4982 case R_PPC64_TLSLD:
4983 /* These special tls relocs tie a call to __tls_get_addr with
4984 its parameter symbol. */
4985 break;
4986
4987 case R_PPC64_GOT_TLSLD16:
4988 case R_PPC64_GOT_TLSLD16_LO:
4989 case R_PPC64_GOT_TLSLD16_HI:
4990 case R_PPC64_GOT_TLSLD16_HA:
4991 tls_type = TLS_TLS | TLS_LD;
4992 goto dogottls;
4993
4994 case R_PPC64_GOT_TLSGD16:
4995 case R_PPC64_GOT_TLSGD16_LO:
4996 case R_PPC64_GOT_TLSGD16_HI:
4997 case R_PPC64_GOT_TLSGD16_HA:
4998 tls_type = TLS_TLS | TLS_GD;
4999 goto dogottls;
5000
5001 case R_PPC64_GOT_TPREL16_DS:
5002 case R_PPC64_GOT_TPREL16_LO_DS:
5003 case R_PPC64_GOT_TPREL16_HI:
5004 case R_PPC64_GOT_TPREL16_HA:
5005 if (!info->executable)
5006 info->flags |= DF_STATIC_TLS;
5007 tls_type = TLS_TLS | TLS_TPREL;
5008 goto dogottls;
5009
5010 case R_PPC64_GOT_DTPREL16_DS:
5011 case R_PPC64_GOT_DTPREL16_LO_DS:
5012 case R_PPC64_GOT_DTPREL16_HI:
5013 case R_PPC64_GOT_DTPREL16_HA:
5014 tls_type = TLS_TLS | TLS_DTPREL;
5015 dogottls:
5016 sec->has_tls_reloc = 1;
5017 /* Fall thru */
5018
5019 case R_PPC64_GOT16:
5020 case R_PPC64_GOT16_DS:
5021 case R_PPC64_GOT16_HA:
5022 case R_PPC64_GOT16_HI:
5023 case R_PPC64_GOT16_LO:
5024 case R_PPC64_GOT16_LO_DS:
5025 /* This symbol requires a global offset table entry. */
5026 sec->has_toc_reloc = 1;
5027 if (r_type == R_PPC64_GOT_TLSLD16
5028 || r_type == R_PPC64_GOT_TLSGD16
5029 || r_type == R_PPC64_GOT_TPREL16_DS
5030 || r_type == R_PPC64_GOT_DTPREL16_DS
5031 || r_type == R_PPC64_GOT16
5032 || r_type == R_PPC64_GOT16_DS)
5033 {
5034 htab->do_multi_toc = 1;
5035 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5036 }
5037
5038 if (ppc64_elf_tdata (abfd)->got == NULL
5039 && !create_got_section (abfd, info))
5040 return FALSE;
5041
5042 if (h != NULL)
5043 {
5044 struct ppc_link_hash_entry *eh;
5045 struct got_entry *ent;
5046
5047 eh = (struct ppc_link_hash_entry *) h;
5048 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5049 if (ent->addend == rel->r_addend
5050 && ent->owner == abfd
5051 && ent->tls_type == tls_type)
5052 break;
5053 if (ent == NULL)
5054 {
5055 bfd_size_type amt = sizeof (*ent);
5056 ent = bfd_alloc (abfd, amt);
5057 if (ent == NULL)
5058 return FALSE;
5059 ent->next = eh->elf.got.glist;
5060 ent->addend = rel->r_addend;
5061 ent->owner = abfd;
5062 ent->tls_type = tls_type;
5063 ent->is_indirect = FALSE;
5064 ent->got.refcount = 0;
5065 eh->elf.got.glist = ent;
5066 }
5067 ent->got.refcount += 1;
5068 eh->tls_mask |= tls_type;
5069 }
5070 else
5071 /* This is a global offset table entry for a local symbol. */
5072 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5073 rel->r_addend, tls_type))
5074 return FALSE;
5075 break;
5076
5077 case R_PPC64_PLT16_HA:
5078 case R_PPC64_PLT16_HI:
5079 case R_PPC64_PLT16_LO:
5080 case R_PPC64_PLT32:
5081 case R_PPC64_PLT64:
5082 /* This symbol requires a procedure linkage table entry. We
5083 actually build the entry in adjust_dynamic_symbol,
5084 because this might be a case of linking PIC code without
5085 linking in any dynamic objects, in which case we don't
5086 need to generate a procedure linkage table after all. */
5087 if (h == NULL)
5088 {
5089 /* It does not make sense to have a procedure linkage
5090 table entry for a local symbol. */
5091 bfd_set_error (bfd_error_bad_value);
5092 return FALSE;
5093 }
5094 else
5095 {
5096 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5097 return FALSE;
5098 h->needs_plt = 1;
5099 if (h->root.root.string[0] == '.'
5100 && h->root.root.string[1] != '\0')
5101 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5102 }
5103 break;
5104
5105 /* The following relocations don't need to propagate the
5106 relocation if linking a shared object since they are
5107 section relative. */
5108 case R_PPC64_SECTOFF:
5109 case R_PPC64_SECTOFF_LO:
5110 case R_PPC64_SECTOFF_HI:
5111 case R_PPC64_SECTOFF_HA:
5112 case R_PPC64_SECTOFF_DS:
5113 case R_PPC64_SECTOFF_LO_DS:
5114 case R_PPC64_DTPREL16:
5115 case R_PPC64_DTPREL16_LO:
5116 case R_PPC64_DTPREL16_HI:
5117 case R_PPC64_DTPREL16_HA:
5118 case R_PPC64_DTPREL16_DS:
5119 case R_PPC64_DTPREL16_LO_DS:
5120 case R_PPC64_DTPREL16_HIGHER:
5121 case R_PPC64_DTPREL16_HIGHERA:
5122 case R_PPC64_DTPREL16_HIGHEST:
5123 case R_PPC64_DTPREL16_HIGHESTA:
5124 break;
5125
5126 /* Nor do these. */
5127 case R_PPC64_REL16:
5128 case R_PPC64_REL16_LO:
5129 case R_PPC64_REL16_HI:
5130 case R_PPC64_REL16_HA:
5131 break;
5132
5133 case R_PPC64_TOC16:
5134 case R_PPC64_TOC16_DS:
5135 htab->do_multi_toc = 1;
5136 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5137 case R_PPC64_TOC16_LO:
5138 case R_PPC64_TOC16_HI:
5139 case R_PPC64_TOC16_HA:
5140 case R_PPC64_TOC16_LO_DS:
5141 sec->has_toc_reloc = 1;
5142 break;
5143
5144 /* This relocation describes the C++ object vtable hierarchy.
5145 Reconstruct it for later use during GC. */
5146 case R_PPC64_GNU_VTINHERIT:
5147 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5148 return FALSE;
5149 break;
5150
5151 /* This relocation describes which C++ vtable entries are actually
5152 used. Record for later use during GC. */
5153 case R_PPC64_GNU_VTENTRY:
5154 BFD_ASSERT (h != NULL);
5155 if (h != NULL
5156 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5157 return FALSE;
5158 break;
5159
5160 case R_PPC64_REL14:
5161 case R_PPC64_REL14_BRTAKEN:
5162 case R_PPC64_REL14_BRNTAKEN:
5163 {
5164 asection *dest = NULL;
5165
5166 /* Heuristic: If jumping outside our section, chances are
5167 we are going to need a stub. */
5168 if (h != NULL)
5169 {
5170 /* If the sym is weak it may be overridden later, so
5171 don't assume we know where a weak sym lives. */
5172 if (h->root.type == bfd_link_hash_defined)
5173 dest = h->root.u.def.section;
5174 }
5175 else
5176 {
5177 Elf_Internal_Sym *isym;
5178
5179 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5180 abfd, r_symndx);
5181 if (isym == NULL)
5182 return FALSE;
5183
5184 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5185 }
5186
5187 if (dest != sec)
5188 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5189 }
5190 /* Fall through. */
5191
5192 case R_PPC64_REL24:
5193 if (h != NULL && ifunc == NULL)
5194 {
5195 /* We may need a .plt entry if the function this reloc
5196 refers to is in a shared lib. */
5197 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5198 return FALSE;
5199 h->needs_plt = 1;
5200 if (h->root.root.string[0] == '.'
5201 && h->root.root.string[1] != '\0')
5202 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5203 if (h == tga || h == dottga)
5204 sec->has_tls_reloc = 1;
5205 }
5206 break;
5207
5208 case R_PPC64_TPREL64:
5209 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5210 if (!info->executable)
5211 info->flags |= DF_STATIC_TLS;
5212 goto dotlstoc;
5213
5214 case R_PPC64_DTPMOD64:
5215 if (rel + 1 < rel_end
5216 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5217 && rel[1].r_offset == rel->r_offset + 8)
5218 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5219 else
5220 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5221 goto dotlstoc;
5222
5223 case R_PPC64_DTPREL64:
5224 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5225 if (rel != relocs
5226 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5227 && rel[-1].r_offset == rel->r_offset - 8)
5228 /* This is the second reloc of a dtpmod, dtprel pair.
5229 Don't mark with TLS_DTPREL. */
5230 goto dodyn;
5231
5232 dotlstoc:
5233 sec->has_tls_reloc = 1;
5234 if (h != NULL)
5235 {
5236 struct ppc_link_hash_entry *eh;
5237 eh = (struct ppc_link_hash_entry *) h;
5238 eh->tls_mask |= tls_type;
5239 }
5240 else
5241 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5242 rel->r_addend, tls_type))
5243 return FALSE;
5244
5245 ppc64_sec = ppc64_elf_section_data (sec);
5246 if (ppc64_sec->sec_type != sec_toc)
5247 {
5248 bfd_size_type amt;
5249
5250 /* One extra to simplify get_tls_mask. */
5251 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5252 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5253 if (ppc64_sec->u.toc.symndx == NULL)
5254 return FALSE;
5255 amt = sec->size * sizeof (bfd_vma) / 8;
5256 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5257 if (ppc64_sec->u.toc.add == NULL)
5258 return FALSE;
5259 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5260 ppc64_sec->sec_type = sec_toc;
5261 }
5262 BFD_ASSERT (rel->r_offset % 8 == 0);
5263 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5264 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5265
5266 /* Mark the second slot of a GD or LD entry.
5267 -1 to indicate GD and -2 to indicate LD. */
5268 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5269 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5270 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5271 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5272 goto dodyn;
5273
5274 case R_PPC64_TPREL16:
5275 case R_PPC64_TPREL16_LO:
5276 case R_PPC64_TPREL16_HI:
5277 case R_PPC64_TPREL16_HA:
5278 case R_PPC64_TPREL16_DS:
5279 case R_PPC64_TPREL16_LO_DS:
5280 case R_PPC64_TPREL16_HIGHER:
5281 case R_PPC64_TPREL16_HIGHERA:
5282 case R_PPC64_TPREL16_HIGHEST:
5283 case R_PPC64_TPREL16_HIGHESTA:
5284 if (info->shared)
5285 {
5286 if (!info->executable)
5287 info->flags |= DF_STATIC_TLS;
5288 goto dodyn;
5289 }
5290 break;
5291
5292 case R_PPC64_ADDR64:
5293 if (opd_sym_map != NULL
5294 && rel + 1 < rel_end
5295 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5296 {
5297 if (h != NULL)
5298 {
5299 if (h->root.root.string[0] == '.'
5300 && h->root.root.string[1] != 0
5301 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5302 ;
5303 else
5304 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5305 }
5306 else
5307 {
5308 asection *s;
5309 Elf_Internal_Sym *isym;
5310
5311 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5312 abfd, r_symndx);
5313 if (isym == NULL)
5314 return FALSE;
5315
5316 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5317 if (s != NULL && s != sec)
5318 opd_sym_map[rel->r_offset / 8] = s;
5319 }
5320 }
5321 /* Fall through. */
5322
5323 case R_PPC64_REL30:
5324 case R_PPC64_REL32:
5325 case R_PPC64_REL64:
5326 case R_PPC64_ADDR14:
5327 case R_PPC64_ADDR14_BRNTAKEN:
5328 case R_PPC64_ADDR14_BRTAKEN:
5329 case R_PPC64_ADDR16:
5330 case R_PPC64_ADDR16_DS:
5331 case R_PPC64_ADDR16_HA:
5332 case R_PPC64_ADDR16_HI:
5333 case R_PPC64_ADDR16_HIGHER:
5334 case R_PPC64_ADDR16_HIGHERA:
5335 case R_PPC64_ADDR16_HIGHEST:
5336 case R_PPC64_ADDR16_HIGHESTA:
5337 case R_PPC64_ADDR16_LO:
5338 case R_PPC64_ADDR16_LO_DS:
5339 case R_PPC64_ADDR24:
5340 case R_PPC64_ADDR32:
5341 case R_PPC64_UADDR16:
5342 case R_PPC64_UADDR32:
5343 case R_PPC64_UADDR64:
5344 case R_PPC64_TOC:
5345 if (h != NULL && !info->shared)
5346 /* We may need a copy reloc. */
5347 h->non_got_ref = 1;
5348
5349 /* Don't propagate .opd relocs. */
5350 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5351 break;
5352
5353 /* If we are creating a shared library, and this is a reloc
5354 against a global symbol, or a non PC relative reloc
5355 against a local symbol, then we need to copy the reloc
5356 into the shared library. However, if we are linking with
5357 -Bsymbolic, we do not need to copy a reloc against a
5358 global symbol which is defined in an object we are
5359 including in the link (i.e., DEF_REGULAR is set). At
5360 this point we have not seen all the input files, so it is
5361 possible that DEF_REGULAR is not set now but will be set
5362 later (it is never cleared). In case of a weak definition,
5363 DEF_REGULAR may be cleared later by a strong definition in
5364 a shared library. We account for that possibility below by
5365 storing information in the dyn_relocs field of the hash
5366 table entry. A similar situation occurs when creating
5367 shared libraries and symbol visibility changes render the
5368 symbol local.
5369
5370 If on the other hand, we are creating an executable, we
5371 may need to keep relocations for symbols satisfied by a
5372 dynamic library if we manage to avoid copy relocs for the
5373 symbol. */
5374 dodyn:
5375 if ((info->shared
5376 && (must_be_dyn_reloc (info, r_type)
5377 || (h != NULL
5378 && (! info->symbolic
5379 || h->root.type == bfd_link_hash_defweak
5380 || !h->def_regular))))
5381 || (ELIMINATE_COPY_RELOCS
5382 && !info->shared
5383 && h != NULL
5384 && (h->root.type == bfd_link_hash_defweak
5385 || !h->def_regular))
5386 || (!info->shared
5387 && ifunc != NULL))
5388 {
5389 struct elf_dyn_relocs *p;
5390 struct elf_dyn_relocs **head;
5391
5392 /* We must copy these reloc types into the output file.
5393 Create a reloc section in dynobj and make room for
5394 this reloc. */
5395 if (sreloc == NULL)
5396 {
5397 sreloc = _bfd_elf_make_dynamic_reloc_section
5398 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5399
5400 if (sreloc == NULL)
5401 return FALSE;
5402 }
5403
5404 /* If this is a global symbol, we count the number of
5405 relocations we need for this symbol. */
5406 if (h != NULL)
5407 {
5408 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5409 }
5410 else
5411 {
5412 /* Track dynamic relocs needed for local syms too.
5413 We really need local syms available to do this
5414 easily. Oh well. */
5415 asection *s;
5416 void *vpp;
5417 Elf_Internal_Sym *isym;
5418
5419 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5420 abfd, r_symndx);
5421 if (isym == NULL)
5422 return FALSE;
5423
5424 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5425 if (s == NULL)
5426 s = sec;
5427
5428 vpp = &elf_section_data (s)->local_dynrel;
5429 head = (struct elf_dyn_relocs **) vpp;
5430 }
5431
5432 p = *head;
5433 if (p == NULL || p->sec != sec)
5434 {
5435 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5436 if (p == NULL)
5437 return FALSE;
5438 p->next = *head;
5439 *head = p;
5440 p->sec = sec;
5441 p->count = 0;
5442 p->pc_count = 0;
5443 }
5444
5445 p->count += 1;
5446 if (!must_be_dyn_reloc (info, r_type))
5447 p->pc_count += 1;
5448 }
5449 break;
5450
5451 default:
5452 break;
5453 }
5454 }
5455
5456 return TRUE;
5457 }
5458
5459 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5460 of the code entry point, and its section. */
5461
5462 static bfd_vma
5463 opd_entry_value (asection *opd_sec,
5464 bfd_vma offset,
5465 asection **code_sec,
5466 bfd_vma *code_off)
5467 {
5468 bfd *opd_bfd = opd_sec->owner;
5469 Elf_Internal_Rela *relocs;
5470 Elf_Internal_Rela *lo, *hi, *look;
5471 bfd_vma val;
5472
5473 /* No relocs implies we are linking a --just-symbols object. */
5474 if (opd_sec->reloc_count == 0)
5475 {
5476 char buf[8];
5477
5478 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
5479 return (bfd_vma) -1;
5480
5481 val = bfd_get_64 (opd_bfd, buf);
5482 if (code_sec != NULL)
5483 {
5484 asection *sec, *likely = NULL;
5485 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5486 if (sec->vma <= val
5487 && (sec->flags & SEC_LOAD) != 0
5488 && (sec->flags & SEC_ALLOC) != 0)
5489 likely = sec;
5490 if (likely != NULL)
5491 {
5492 *code_sec = likely;
5493 if (code_off != NULL)
5494 *code_off = val - likely->vma;
5495 }
5496 }
5497 return val;
5498 }
5499
5500 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5501
5502 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5503 if (relocs == NULL)
5504 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5505
5506 /* Go find the opd reloc at the sym address. */
5507 lo = relocs;
5508 BFD_ASSERT (lo != NULL);
5509 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5510 val = (bfd_vma) -1;
5511 while (lo < hi)
5512 {
5513 look = lo + (hi - lo) / 2;
5514 if (look->r_offset < offset)
5515 lo = look + 1;
5516 else if (look->r_offset > offset)
5517 hi = look;
5518 else
5519 {
5520 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5521
5522 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5523 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5524 {
5525 unsigned long symndx = ELF64_R_SYM (look->r_info);
5526 asection *sec;
5527
5528 if (symndx < symtab_hdr->sh_info)
5529 {
5530 Elf_Internal_Sym *sym;
5531
5532 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5533 if (sym == NULL)
5534 {
5535 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5536 symtab_hdr->sh_info,
5537 0, NULL, NULL, NULL);
5538 if (sym == NULL)
5539 break;
5540 symtab_hdr->contents = (bfd_byte *) sym;
5541 }
5542
5543 sym += symndx;
5544 val = sym->st_value;
5545 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5546 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5547 }
5548 else
5549 {
5550 struct elf_link_hash_entry **sym_hashes;
5551 struct elf_link_hash_entry *rh;
5552
5553 sym_hashes = elf_sym_hashes (opd_bfd);
5554 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5555 rh = elf_follow_link (rh);
5556 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5557 || rh->root.type == bfd_link_hash_defweak);
5558 val = rh->root.u.def.value;
5559 sec = rh->root.u.def.section;
5560 }
5561 val += look->r_addend;
5562 if (code_off != NULL)
5563 *code_off = val;
5564 if (code_sec != NULL)
5565 *code_sec = sec;
5566 if (sec != NULL && sec->output_section != NULL)
5567 val += sec->output_section->vma + sec->output_offset;
5568 }
5569 break;
5570 }
5571 }
5572
5573 return val;
5574 }
5575
5576 /* Return true if symbol is defined in a regular object file. */
5577
5578 static bfd_boolean
5579 is_static_defined (struct elf_link_hash_entry *h)
5580 {
5581 return ((h->root.type == bfd_link_hash_defined
5582 || h->root.type == bfd_link_hash_defweak)
5583 && h->root.u.def.section != NULL
5584 && h->root.u.def.section->output_section != NULL);
5585 }
5586
5587 /* If FDH is a function descriptor symbol, return the associated code
5588 entry symbol if it is defined. Return NULL otherwise. */
5589
5590 static struct ppc_link_hash_entry *
5591 defined_code_entry (struct ppc_link_hash_entry *fdh)
5592 {
5593 if (fdh->is_func_descriptor)
5594 {
5595 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5596 if (fh->elf.root.type == bfd_link_hash_defined
5597 || fh->elf.root.type == bfd_link_hash_defweak)
5598 return fh;
5599 }
5600 return NULL;
5601 }
5602
5603 /* If FH is a function code entry symbol, return the associated
5604 function descriptor symbol if it is defined. Return NULL otherwise. */
5605
5606 static struct ppc_link_hash_entry *
5607 defined_func_desc (struct ppc_link_hash_entry *fh)
5608 {
5609 if (fh->oh != NULL
5610 && fh->oh->is_func_descriptor)
5611 {
5612 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5613 if (fdh->elf.root.type == bfd_link_hash_defined
5614 || fdh->elf.root.type == bfd_link_hash_defweak)
5615 return fdh;
5616 }
5617 return NULL;
5618 }
5619
5620 /* Mark all our entry sym sections, both opd and code section. */
5621
5622 static void
5623 ppc64_elf_gc_keep (struct bfd_link_info *info)
5624 {
5625 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5626 struct bfd_sym_chain *sym;
5627
5628 if (htab == NULL)
5629 return;
5630
5631 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5632 {
5633 struct ppc_link_hash_entry *eh, *fh;
5634 asection *sec;
5635
5636 eh = (struct ppc_link_hash_entry *)
5637 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
5638 if (eh == NULL)
5639 continue;
5640 if (eh->elf.root.type != bfd_link_hash_defined
5641 && eh->elf.root.type != bfd_link_hash_defweak)
5642 continue;
5643
5644 fh = defined_code_entry (eh);
5645 if (fh != NULL)
5646 {
5647 sec = fh->elf.root.u.def.section;
5648 sec->flags |= SEC_KEEP;
5649 }
5650 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5651 && opd_entry_value (eh->elf.root.u.def.section,
5652 eh->elf.root.u.def.value,
5653 &sec, NULL) != (bfd_vma) -1)
5654 sec->flags |= SEC_KEEP;
5655
5656 sec = eh->elf.root.u.def.section;
5657 sec->flags |= SEC_KEEP;
5658 }
5659 }
5660
5661 /* Mark sections containing dynamically referenced symbols. When
5662 building shared libraries, we must assume that any visible symbol is
5663 referenced. */
5664
5665 static bfd_boolean
5666 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5667 {
5668 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5669 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5670 struct ppc_link_hash_entry *fdh;
5671
5672 /* Dynamic linking info is on the func descriptor sym. */
5673 fdh = defined_func_desc (eh);
5674 if (fdh != NULL)
5675 eh = fdh;
5676
5677 if ((eh->elf.root.type == bfd_link_hash_defined
5678 || eh->elf.root.type == bfd_link_hash_defweak)
5679 && (eh->elf.ref_dynamic
5680 || (!info->executable
5681 && eh->elf.def_regular
5682 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5683 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5684 {
5685 asection *code_sec;
5686 struct ppc_link_hash_entry *fh;
5687
5688 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5689
5690 /* Function descriptor syms cause the associated
5691 function code sym section to be marked. */
5692 fh = defined_code_entry (eh);
5693 if (fh != NULL)
5694 {
5695 code_sec = fh->elf.root.u.def.section;
5696 code_sec->flags |= SEC_KEEP;
5697 }
5698 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5699 && opd_entry_value (eh->elf.root.u.def.section,
5700 eh->elf.root.u.def.value,
5701 &code_sec, NULL) != (bfd_vma) -1)
5702 code_sec->flags |= SEC_KEEP;
5703 }
5704
5705 return TRUE;
5706 }
5707
5708 /* Return the section that should be marked against GC for a given
5709 relocation. */
5710
5711 static asection *
5712 ppc64_elf_gc_mark_hook (asection *sec,
5713 struct bfd_link_info *info,
5714 Elf_Internal_Rela *rel,
5715 struct elf_link_hash_entry *h,
5716 Elf_Internal_Sym *sym)
5717 {
5718 asection *rsec;
5719
5720 /* Syms return NULL if we're marking .opd, so we avoid marking all
5721 function sections, as all functions are referenced in .opd. */
5722 rsec = NULL;
5723 if (get_opd_info (sec) != NULL)
5724 return rsec;
5725
5726 if (h != NULL)
5727 {
5728 enum elf_ppc64_reloc_type r_type;
5729 struct ppc_link_hash_entry *eh, *fh, *fdh;
5730
5731 r_type = ELF64_R_TYPE (rel->r_info);
5732 switch (r_type)
5733 {
5734 case R_PPC64_GNU_VTINHERIT:
5735 case R_PPC64_GNU_VTENTRY:
5736 break;
5737
5738 default:
5739 switch (h->root.type)
5740 {
5741 case bfd_link_hash_defined:
5742 case bfd_link_hash_defweak:
5743 eh = (struct ppc_link_hash_entry *) h;
5744 fdh = defined_func_desc (eh);
5745 if (fdh != NULL)
5746 eh = fdh;
5747
5748 /* Function descriptor syms cause the associated
5749 function code sym section to be marked. */
5750 fh = defined_code_entry (eh);
5751 if (fh != NULL)
5752 {
5753 /* They also mark their opd section. */
5754 eh->elf.root.u.def.section->gc_mark = 1;
5755
5756 rsec = fh->elf.root.u.def.section;
5757 }
5758 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5759 && opd_entry_value (eh->elf.root.u.def.section,
5760 eh->elf.root.u.def.value,
5761 &rsec, NULL) != (bfd_vma) -1)
5762 eh->elf.root.u.def.section->gc_mark = 1;
5763 else
5764 rsec = h->root.u.def.section;
5765 break;
5766
5767 case bfd_link_hash_common:
5768 rsec = h->root.u.c.p->section;
5769 break;
5770
5771 default:
5772 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5773 }
5774 }
5775 }
5776 else
5777 {
5778 struct _opd_sec_data *opd;
5779
5780 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5781 opd = get_opd_info (rsec);
5782 if (opd != NULL && opd->func_sec != NULL)
5783 {
5784 rsec->gc_mark = 1;
5785
5786 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
5787 }
5788 }
5789
5790 return rsec;
5791 }
5792
5793 /* Update the .got, .plt. and dynamic reloc reference counts for the
5794 section being removed. */
5795
5796 static bfd_boolean
5797 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5798 asection *sec, const Elf_Internal_Rela *relocs)
5799 {
5800 struct ppc_link_hash_table *htab;
5801 Elf_Internal_Shdr *symtab_hdr;
5802 struct elf_link_hash_entry **sym_hashes;
5803 struct got_entry **local_got_ents;
5804 const Elf_Internal_Rela *rel, *relend;
5805
5806 if (info->relocatable)
5807 return TRUE;
5808
5809 if ((sec->flags & SEC_ALLOC) == 0)
5810 return TRUE;
5811
5812 elf_section_data (sec)->local_dynrel = NULL;
5813
5814 htab = ppc_hash_table (info);
5815 if (htab == NULL)
5816 return FALSE;
5817
5818 symtab_hdr = &elf_symtab_hdr (abfd);
5819 sym_hashes = elf_sym_hashes (abfd);
5820 local_got_ents = elf_local_got_ents (abfd);
5821
5822 relend = relocs + sec->reloc_count;
5823 for (rel = relocs; rel < relend; rel++)
5824 {
5825 unsigned long r_symndx;
5826 enum elf_ppc64_reloc_type r_type;
5827 struct elf_link_hash_entry *h = NULL;
5828 unsigned char tls_type = 0;
5829
5830 r_symndx = ELF64_R_SYM (rel->r_info);
5831 r_type = ELF64_R_TYPE (rel->r_info);
5832 if (r_symndx >= symtab_hdr->sh_info)
5833 {
5834 struct ppc_link_hash_entry *eh;
5835 struct elf_dyn_relocs **pp;
5836 struct elf_dyn_relocs *p;
5837
5838 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5839 h = elf_follow_link (h);
5840 eh = (struct ppc_link_hash_entry *) h;
5841
5842 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5843 if (p->sec == sec)
5844 {
5845 /* Everything must go for SEC. */
5846 *pp = p->next;
5847 break;
5848 }
5849 }
5850
5851 if (is_branch_reloc (r_type))
5852 {
5853 struct plt_entry **ifunc = NULL;
5854 if (h != NULL)
5855 {
5856 if (h->type == STT_GNU_IFUNC)
5857 ifunc = &h->plt.plist;
5858 }
5859 else if (local_got_ents != NULL)
5860 {
5861 struct plt_entry **local_plt = (struct plt_entry **)
5862 (local_got_ents + symtab_hdr->sh_info);
5863 unsigned char *local_got_tls_masks = (unsigned char *)
5864 (local_plt + symtab_hdr->sh_info);
5865 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5866 ifunc = local_plt + r_symndx;
5867 }
5868 if (ifunc != NULL)
5869 {
5870 struct plt_entry *ent;
5871
5872 for (ent = *ifunc; ent != NULL; ent = ent->next)
5873 if (ent->addend == rel->r_addend)
5874 break;
5875 if (ent == NULL)
5876 abort ();
5877 if (ent->plt.refcount > 0)
5878 ent->plt.refcount -= 1;
5879 continue;
5880 }
5881 }
5882
5883 switch (r_type)
5884 {
5885 case R_PPC64_GOT_TLSLD16:
5886 case R_PPC64_GOT_TLSLD16_LO:
5887 case R_PPC64_GOT_TLSLD16_HI:
5888 case R_PPC64_GOT_TLSLD16_HA:
5889 tls_type = TLS_TLS | TLS_LD;
5890 goto dogot;
5891
5892 case R_PPC64_GOT_TLSGD16:
5893 case R_PPC64_GOT_TLSGD16_LO:
5894 case R_PPC64_GOT_TLSGD16_HI:
5895 case R_PPC64_GOT_TLSGD16_HA:
5896 tls_type = TLS_TLS | TLS_GD;
5897 goto dogot;
5898
5899 case R_PPC64_GOT_TPREL16_DS:
5900 case R_PPC64_GOT_TPREL16_LO_DS:
5901 case R_PPC64_GOT_TPREL16_HI:
5902 case R_PPC64_GOT_TPREL16_HA:
5903 tls_type = TLS_TLS | TLS_TPREL;
5904 goto dogot;
5905
5906 case R_PPC64_GOT_DTPREL16_DS:
5907 case R_PPC64_GOT_DTPREL16_LO_DS:
5908 case R_PPC64_GOT_DTPREL16_HI:
5909 case R_PPC64_GOT_DTPREL16_HA:
5910 tls_type = TLS_TLS | TLS_DTPREL;
5911 goto dogot;
5912
5913 case R_PPC64_GOT16:
5914 case R_PPC64_GOT16_DS:
5915 case R_PPC64_GOT16_HA:
5916 case R_PPC64_GOT16_HI:
5917 case R_PPC64_GOT16_LO:
5918 case R_PPC64_GOT16_LO_DS:
5919 dogot:
5920 {
5921 struct got_entry *ent;
5922
5923 if (h != NULL)
5924 ent = h->got.glist;
5925 else
5926 ent = local_got_ents[r_symndx];
5927
5928 for (; ent != NULL; ent = ent->next)
5929 if (ent->addend == rel->r_addend
5930 && ent->owner == abfd
5931 && ent->tls_type == tls_type)
5932 break;
5933 if (ent == NULL)
5934 abort ();
5935 if (ent->got.refcount > 0)
5936 ent->got.refcount -= 1;
5937 }
5938 break;
5939
5940 case R_PPC64_PLT16_HA:
5941 case R_PPC64_PLT16_HI:
5942 case R_PPC64_PLT16_LO:
5943 case R_PPC64_PLT32:
5944 case R_PPC64_PLT64:
5945 case R_PPC64_REL14:
5946 case R_PPC64_REL14_BRNTAKEN:
5947 case R_PPC64_REL14_BRTAKEN:
5948 case R_PPC64_REL24:
5949 if (h != NULL)
5950 {
5951 struct plt_entry *ent;
5952
5953 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5954 if (ent->addend == rel->r_addend)
5955 break;
5956 if (ent != NULL && ent->plt.refcount > 0)
5957 ent->plt.refcount -= 1;
5958 }
5959 break;
5960
5961 default:
5962 break;
5963 }
5964 }
5965 return TRUE;
5966 }
5967
5968 /* The maximum size of .sfpr. */
5969 #define SFPR_MAX (218*4)
5970
5971 struct sfpr_def_parms
5972 {
5973 const char name[12];
5974 unsigned char lo, hi;
5975 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5976 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5977 };
5978
5979 /* Auto-generate _save*, _rest* functions in .sfpr. */
5980
5981 static bfd_boolean
5982 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5983 {
5984 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5985 unsigned int i;
5986 size_t len = strlen (parm->name);
5987 bfd_boolean writing = FALSE;
5988 char sym[16];
5989
5990 if (htab == NULL)
5991 return FALSE;
5992
5993 memcpy (sym, parm->name, len);
5994 sym[len + 2] = 0;
5995
5996 for (i = parm->lo; i <= parm->hi; i++)
5997 {
5998 struct elf_link_hash_entry *h;
5999
6000 sym[len + 0] = i / 10 + '0';
6001 sym[len + 1] = i % 10 + '0';
6002 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6003 if (h != NULL
6004 && !h->def_regular)
6005 {
6006 h->root.type = bfd_link_hash_defined;
6007 h->root.u.def.section = htab->sfpr;
6008 h->root.u.def.value = htab->sfpr->size;
6009 h->type = STT_FUNC;
6010 h->def_regular = 1;
6011 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6012 writing = TRUE;
6013 if (htab->sfpr->contents == NULL)
6014 {
6015 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6016 if (htab->sfpr->contents == NULL)
6017 return FALSE;
6018 }
6019 }
6020 if (writing)
6021 {
6022 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6023 if (i != parm->hi)
6024 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6025 else
6026 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6027 htab->sfpr->size = p - htab->sfpr->contents;
6028 }
6029 }
6030
6031 return TRUE;
6032 }
6033
6034 static bfd_byte *
6035 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6036 {
6037 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6038 return p + 4;
6039 }
6040
6041 static bfd_byte *
6042 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6043 {
6044 p = savegpr0 (abfd, p, r);
6045 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6046 p = p + 4;
6047 bfd_put_32 (abfd, BLR, p);
6048 return p + 4;
6049 }
6050
6051 static bfd_byte *
6052 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6053 {
6054 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6055 return p + 4;
6056 }
6057
6058 static bfd_byte *
6059 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6060 {
6061 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6062 p = p + 4;
6063 p = restgpr0 (abfd, p, r);
6064 bfd_put_32 (abfd, MTLR_R0, p);
6065 p = p + 4;
6066 if (r == 29)
6067 {
6068 p = restgpr0 (abfd, p, 30);
6069 p = restgpr0 (abfd, p, 31);
6070 }
6071 bfd_put_32 (abfd, BLR, p);
6072 return p + 4;
6073 }
6074
6075 static bfd_byte *
6076 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6077 {
6078 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6079 return p + 4;
6080 }
6081
6082 static bfd_byte *
6083 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6084 {
6085 p = savegpr1 (abfd, p, r);
6086 bfd_put_32 (abfd, BLR, p);
6087 return p + 4;
6088 }
6089
6090 static bfd_byte *
6091 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6092 {
6093 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6094 return p + 4;
6095 }
6096
6097 static bfd_byte *
6098 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6099 {
6100 p = restgpr1 (abfd, p, r);
6101 bfd_put_32 (abfd, BLR, p);
6102 return p + 4;
6103 }
6104
6105 static bfd_byte *
6106 savefpr (bfd *abfd, bfd_byte *p, int r)
6107 {
6108 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6109 return p + 4;
6110 }
6111
6112 static bfd_byte *
6113 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6114 {
6115 p = savefpr (abfd, p, r);
6116 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6117 p = p + 4;
6118 bfd_put_32 (abfd, BLR, p);
6119 return p + 4;
6120 }
6121
6122 static bfd_byte *
6123 restfpr (bfd *abfd, bfd_byte *p, int r)
6124 {
6125 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6126 return p + 4;
6127 }
6128
6129 static bfd_byte *
6130 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6131 {
6132 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6133 p = p + 4;
6134 p = restfpr (abfd, p, r);
6135 bfd_put_32 (abfd, MTLR_R0, p);
6136 p = p + 4;
6137 if (r == 29)
6138 {
6139 p = restfpr (abfd, p, 30);
6140 p = restfpr (abfd, p, 31);
6141 }
6142 bfd_put_32 (abfd, BLR, p);
6143 return p + 4;
6144 }
6145
6146 static bfd_byte *
6147 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6148 {
6149 p = savefpr (abfd, p, r);
6150 bfd_put_32 (abfd, BLR, p);
6151 return p + 4;
6152 }
6153
6154 static bfd_byte *
6155 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6156 {
6157 p = restfpr (abfd, p, r);
6158 bfd_put_32 (abfd, BLR, p);
6159 return p + 4;
6160 }
6161
6162 static bfd_byte *
6163 savevr (bfd *abfd, bfd_byte *p, int r)
6164 {
6165 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6166 p = p + 4;
6167 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6168 return p + 4;
6169 }
6170
6171 static bfd_byte *
6172 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6173 {
6174 p = savevr (abfd, p, r);
6175 bfd_put_32 (abfd, BLR, p);
6176 return p + 4;
6177 }
6178
6179 static bfd_byte *
6180 restvr (bfd *abfd, bfd_byte *p, int r)
6181 {
6182 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6183 p = p + 4;
6184 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6185 return p + 4;
6186 }
6187
6188 static bfd_byte *
6189 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6190 {
6191 p = restvr (abfd, p, r);
6192 bfd_put_32 (abfd, BLR, p);
6193 return p + 4;
6194 }
6195
6196 /* Called via elf_link_hash_traverse to transfer dynamic linking
6197 information on function code symbol entries to their corresponding
6198 function descriptor symbol entries. */
6199
6200 static bfd_boolean
6201 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6202 {
6203 struct bfd_link_info *info;
6204 struct ppc_link_hash_table *htab;
6205 struct plt_entry *ent;
6206 struct ppc_link_hash_entry *fh;
6207 struct ppc_link_hash_entry *fdh;
6208 bfd_boolean force_local;
6209
6210 fh = (struct ppc_link_hash_entry *) h;
6211 if (fh->elf.root.type == bfd_link_hash_indirect)
6212 return TRUE;
6213
6214 info = inf;
6215 htab = ppc_hash_table (info);
6216 if (htab == NULL)
6217 return FALSE;
6218
6219 /* Resolve undefined references to dot-symbols as the value
6220 in the function descriptor, if we have one in a regular object.
6221 This is to satisfy cases like ".quad .foo". Calls to functions
6222 in dynamic objects are handled elsewhere. */
6223 if (fh->elf.root.type == bfd_link_hash_undefweak
6224 && fh->was_undefined
6225 && (fdh = defined_func_desc (fh)) != NULL
6226 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6227 && opd_entry_value (fdh->elf.root.u.def.section,
6228 fdh->elf.root.u.def.value,
6229 &fh->elf.root.u.def.section,
6230 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6231 {
6232 fh->elf.root.type = fdh->elf.root.type;
6233 fh->elf.forced_local = 1;
6234 fh->elf.def_regular = fdh->elf.def_regular;
6235 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6236 }
6237
6238 /* If this is a function code symbol, transfer dynamic linking
6239 information to the function descriptor symbol. */
6240 if (!fh->is_func)
6241 return TRUE;
6242
6243 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6244 if (ent->plt.refcount > 0)
6245 break;
6246 if (ent == NULL
6247 || fh->elf.root.root.string[0] != '.'
6248 || fh->elf.root.root.string[1] == '\0')
6249 return TRUE;
6250
6251 /* Find the corresponding function descriptor symbol. Create it
6252 as undefined if necessary. */
6253
6254 fdh = lookup_fdh (fh, htab);
6255 if (fdh == NULL
6256 && !info->executable
6257 && (fh->elf.root.type == bfd_link_hash_undefined
6258 || fh->elf.root.type == bfd_link_hash_undefweak))
6259 {
6260 fdh = make_fdh (info, fh);
6261 if (fdh == NULL)
6262 return FALSE;
6263 }
6264
6265 /* Fake function descriptors are made undefweak. If the function
6266 code symbol is strong undefined, make the fake sym the same.
6267 If the function code symbol is defined, then force the fake
6268 descriptor local; We can't support overriding of symbols in a
6269 shared library on a fake descriptor. */
6270
6271 if (fdh != NULL
6272 && fdh->fake
6273 && fdh->elf.root.type == bfd_link_hash_undefweak)
6274 {
6275 if (fh->elf.root.type == bfd_link_hash_undefined)
6276 {
6277 fdh->elf.root.type = bfd_link_hash_undefined;
6278 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6279 }
6280 else if (fh->elf.root.type == bfd_link_hash_defined
6281 || fh->elf.root.type == bfd_link_hash_defweak)
6282 {
6283 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6284 }
6285 }
6286
6287 if (fdh != NULL
6288 && !fdh->elf.forced_local
6289 && (!info->executable
6290 || fdh->elf.def_dynamic
6291 || fdh->elf.ref_dynamic
6292 || (fdh->elf.root.type == bfd_link_hash_undefweak
6293 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6294 {
6295 if (fdh->elf.dynindx == -1)
6296 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6297 return FALSE;
6298 fdh->elf.ref_regular |= fh->elf.ref_regular;
6299 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6300 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6301 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6302 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6303 {
6304 move_plt_plist (fh, fdh);
6305 fdh->elf.needs_plt = 1;
6306 }
6307 fdh->is_func_descriptor = 1;
6308 fdh->oh = fh;
6309 fh->oh = fdh;
6310 }
6311
6312 /* Now that the info is on the function descriptor, clear the
6313 function code sym info. Any function code syms for which we
6314 don't have a definition in a regular file, we force local.
6315 This prevents a shared library from exporting syms that have
6316 been imported from another library. Function code syms that
6317 are really in the library we must leave global to prevent the
6318 linker dragging in a definition from a static library. */
6319 force_local = (!fh->elf.def_regular
6320 || fdh == NULL
6321 || !fdh->elf.def_regular
6322 || fdh->elf.forced_local);
6323 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6324
6325 return TRUE;
6326 }
6327
6328 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6329 this hook to a) provide some gcc support functions, and b) transfer
6330 dynamic linking information gathered so far on function code symbol
6331 entries, to their corresponding function descriptor symbol entries. */
6332
6333 static bfd_boolean
6334 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6335 struct bfd_link_info *info)
6336 {
6337 struct ppc_link_hash_table *htab;
6338 unsigned int i;
6339 const struct sfpr_def_parms funcs[] =
6340 {
6341 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6342 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6343 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6344 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6345 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6346 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6347 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6348 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6349 { "._savef", 14, 31, savefpr, savefpr1_tail },
6350 { "._restf", 14, 31, restfpr, restfpr1_tail },
6351 { "_savevr_", 20, 31, savevr, savevr_tail },
6352 { "_restvr_", 20, 31, restvr, restvr_tail }
6353 };
6354
6355 htab = ppc_hash_table (info);
6356 if (htab == NULL)
6357 return FALSE;
6358
6359 if (htab->sfpr == NULL)
6360 /* We don't have any relocs. */
6361 return TRUE;
6362
6363 /* Provide any missing _save* and _rest* functions. */
6364 htab->sfpr->size = 0;
6365 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6366 if (!sfpr_define (info, &funcs[i]))
6367 return FALSE;
6368
6369 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6370
6371 if (htab->sfpr->size == 0)
6372 htab->sfpr->flags |= SEC_EXCLUDE;
6373
6374 return TRUE;
6375 }
6376
6377 /* Adjust a symbol defined by a dynamic object and referenced by a
6378 regular object. The current definition is in some section of the
6379 dynamic object, but we're not including those sections. We have to
6380 change the definition to something the rest of the link can
6381 understand. */
6382
6383 static bfd_boolean
6384 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6385 struct elf_link_hash_entry *h)
6386 {
6387 struct ppc_link_hash_table *htab;
6388 asection *s;
6389
6390 htab = ppc_hash_table (info);
6391 if (htab == NULL)
6392 return FALSE;
6393
6394 /* Deal with function syms. */
6395 if (h->type == STT_FUNC
6396 || h->type == STT_GNU_IFUNC
6397 || h->needs_plt)
6398 {
6399 /* Clear procedure linkage table information for any symbol that
6400 won't need a .plt entry. */
6401 struct plt_entry *ent;
6402 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6403 if (ent->plt.refcount > 0)
6404 break;
6405 if (ent == NULL
6406 || (h->type != STT_GNU_IFUNC
6407 && (SYMBOL_CALLS_LOCAL (info, h)
6408 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6409 && h->root.type == bfd_link_hash_undefweak))))
6410 {
6411 h->plt.plist = NULL;
6412 h->needs_plt = 0;
6413 }
6414 }
6415 else
6416 h->plt.plist = NULL;
6417
6418 /* If this is a weak symbol, and there is a real definition, the
6419 processor independent code will have arranged for us to see the
6420 real definition first, and we can just use the same value. */
6421 if (h->u.weakdef != NULL)
6422 {
6423 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6424 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6425 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6426 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6427 if (ELIMINATE_COPY_RELOCS)
6428 h->non_got_ref = h->u.weakdef->non_got_ref;
6429 return TRUE;
6430 }
6431
6432 /* If we are creating a shared library, we must presume that the
6433 only references to the symbol are via the global offset table.
6434 For such cases we need not do anything here; the relocations will
6435 be handled correctly by relocate_section. */
6436 if (info->shared)
6437 return TRUE;
6438
6439 /* If there are no references to this symbol that do not use the
6440 GOT, we don't need to generate a copy reloc. */
6441 if (!h->non_got_ref)
6442 return TRUE;
6443
6444 /* Don't generate a copy reloc for symbols defined in the executable. */
6445 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6446 return TRUE;
6447
6448 if (ELIMINATE_COPY_RELOCS)
6449 {
6450 struct ppc_link_hash_entry * eh;
6451 struct elf_dyn_relocs *p;
6452
6453 eh = (struct ppc_link_hash_entry *) h;
6454 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6455 {
6456 s = p->sec->output_section;
6457 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6458 break;
6459 }
6460
6461 /* If we didn't find any dynamic relocs in read-only sections, then
6462 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6463 if (p == NULL)
6464 {
6465 h->non_got_ref = 0;
6466 return TRUE;
6467 }
6468 }
6469
6470 if (h->plt.plist != NULL)
6471 {
6472 /* We should never get here, but unfortunately there are versions
6473 of gcc out there that improperly (for this ABI) put initialized
6474 function pointers, vtable refs and suchlike in read-only
6475 sections. Allow them to proceed, but warn that this might
6476 break at runtime. */
6477 info->callbacks->einfo
6478 (_("copy reloc against `%s' requires lazy plt linking; "
6479 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6480 h->root.root.string);
6481 }
6482
6483 /* This is a reference to a symbol defined by a dynamic object which
6484 is not a function. */
6485
6486 if (h->size == 0)
6487 {
6488 info->callbacks->einfo (_("dynamic variable `%s' is zero size\n"),
6489 h->root.root.string);
6490 return TRUE;
6491 }
6492
6493 /* We must allocate the symbol in our .dynbss section, which will
6494 become part of the .bss section of the executable. There will be
6495 an entry for this symbol in the .dynsym section. The dynamic
6496 object will contain position independent code, so all references
6497 from the dynamic object to this symbol will go through the global
6498 offset table. The dynamic linker will use the .dynsym entry to
6499 determine the address it must put in the global offset table, so
6500 both the dynamic object and the regular object will refer to the
6501 same memory location for the variable. */
6502
6503 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6504 to copy the initial value out of the dynamic object and into the
6505 runtime process image. We need to remember the offset into the
6506 .rela.bss section we are going to use. */
6507 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6508 {
6509 htab->relbss->size += sizeof (Elf64_External_Rela);
6510 h->needs_copy = 1;
6511 }
6512
6513 s = htab->dynbss;
6514
6515 return _bfd_elf_adjust_dynamic_copy (h, s);
6516 }
6517
6518 /* If given a function descriptor symbol, hide both the function code
6519 sym and the descriptor. */
6520 static void
6521 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6522 struct elf_link_hash_entry *h,
6523 bfd_boolean force_local)
6524 {
6525 struct ppc_link_hash_entry *eh;
6526 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6527
6528 eh = (struct ppc_link_hash_entry *) h;
6529 if (eh->is_func_descriptor)
6530 {
6531 struct ppc_link_hash_entry *fh = eh->oh;
6532
6533 if (fh == NULL)
6534 {
6535 const char *p, *q;
6536 struct ppc_link_hash_table *htab;
6537 char save;
6538
6539 /* We aren't supposed to use alloca in BFD because on
6540 systems which do not have alloca the version in libiberty
6541 calls xmalloc, which might cause the program to crash
6542 when it runs out of memory. This function doesn't have a
6543 return status, so there's no way to gracefully return an
6544 error. So cheat. We know that string[-1] can be safely
6545 accessed; It's either a string in an ELF string table,
6546 or allocated in an objalloc structure. */
6547
6548 p = eh->elf.root.root.string - 1;
6549 save = *p;
6550 *(char *) p = '.';
6551 htab = ppc_hash_table (info);
6552 if (htab == NULL)
6553 return;
6554
6555 fh = (struct ppc_link_hash_entry *)
6556 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6557 *(char *) p = save;
6558
6559 /* Unfortunately, if it so happens that the string we were
6560 looking for was allocated immediately before this string,
6561 then we overwrote the string terminator. That's the only
6562 reason the lookup should fail. */
6563 if (fh == NULL)
6564 {
6565 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6566 while (q >= eh->elf.root.root.string && *q == *p)
6567 --q, --p;
6568 if (q < eh->elf.root.root.string && *p == '.')
6569 fh = (struct ppc_link_hash_entry *)
6570 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
6571 }
6572 if (fh != NULL)
6573 {
6574 eh->oh = fh;
6575 fh->oh = eh;
6576 }
6577 }
6578 if (fh != NULL)
6579 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6580 }
6581 }
6582
6583 static bfd_boolean
6584 get_sym_h (struct elf_link_hash_entry **hp,
6585 Elf_Internal_Sym **symp,
6586 asection **symsecp,
6587 unsigned char **tls_maskp,
6588 Elf_Internal_Sym **locsymsp,
6589 unsigned long r_symndx,
6590 bfd *ibfd)
6591 {
6592 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6593
6594 if (r_symndx >= symtab_hdr->sh_info)
6595 {
6596 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6597 struct elf_link_hash_entry *h;
6598
6599 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6600 h = elf_follow_link (h);
6601
6602 if (hp != NULL)
6603 *hp = h;
6604
6605 if (symp != NULL)
6606 *symp = NULL;
6607
6608 if (symsecp != NULL)
6609 {
6610 asection *symsec = NULL;
6611 if (h->root.type == bfd_link_hash_defined
6612 || h->root.type == bfd_link_hash_defweak)
6613 symsec = h->root.u.def.section;
6614 *symsecp = symsec;
6615 }
6616
6617 if (tls_maskp != NULL)
6618 {
6619 struct ppc_link_hash_entry *eh;
6620
6621 eh = (struct ppc_link_hash_entry *) h;
6622 *tls_maskp = &eh->tls_mask;
6623 }
6624 }
6625 else
6626 {
6627 Elf_Internal_Sym *sym;
6628 Elf_Internal_Sym *locsyms = *locsymsp;
6629
6630 if (locsyms == NULL)
6631 {
6632 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6633 if (locsyms == NULL)
6634 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6635 symtab_hdr->sh_info,
6636 0, NULL, NULL, NULL);
6637 if (locsyms == NULL)
6638 return FALSE;
6639 *locsymsp = locsyms;
6640 }
6641 sym = locsyms + r_symndx;
6642
6643 if (hp != NULL)
6644 *hp = NULL;
6645
6646 if (symp != NULL)
6647 *symp = sym;
6648
6649 if (symsecp != NULL)
6650 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6651
6652 if (tls_maskp != NULL)
6653 {
6654 struct got_entry **lgot_ents;
6655 unsigned char *tls_mask;
6656
6657 tls_mask = NULL;
6658 lgot_ents = elf_local_got_ents (ibfd);
6659 if (lgot_ents != NULL)
6660 {
6661 struct plt_entry **local_plt = (struct plt_entry **)
6662 (lgot_ents + symtab_hdr->sh_info);
6663 unsigned char *lgot_masks = (unsigned char *)
6664 (local_plt + symtab_hdr->sh_info);
6665 tls_mask = &lgot_masks[r_symndx];
6666 }
6667 *tls_maskp = tls_mask;
6668 }
6669 }
6670 return TRUE;
6671 }
6672
6673 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6674 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6675 type suitable for optimization, and 1 otherwise. */
6676
6677 static int
6678 get_tls_mask (unsigned char **tls_maskp,
6679 unsigned long *toc_symndx,
6680 bfd_vma *toc_addend,
6681 Elf_Internal_Sym **locsymsp,
6682 const Elf_Internal_Rela *rel,
6683 bfd *ibfd)
6684 {
6685 unsigned long r_symndx;
6686 int next_r;
6687 struct elf_link_hash_entry *h;
6688 Elf_Internal_Sym *sym;
6689 asection *sec;
6690 bfd_vma off;
6691
6692 r_symndx = ELF64_R_SYM (rel->r_info);
6693 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6694 return 0;
6695
6696 if ((*tls_maskp != NULL && **tls_maskp != 0)
6697 || sec == NULL
6698 || ppc64_elf_section_data (sec) == NULL
6699 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6700 return 1;
6701
6702 /* Look inside a TOC section too. */
6703 if (h != NULL)
6704 {
6705 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6706 off = h->root.u.def.value;
6707 }
6708 else
6709 off = sym->st_value;
6710 off += rel->r_addend;
6711 BFD_ASSERT (off % 8 == 0);
6712 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6713 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6714 if (toc_symndx != NULL)
6715 *toc_symndx = r_symndx;
6716 if (toc_addend != NULL)
6717 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6718 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6719 return 0;
6720 if ((h == NULL || is_static_defined (h))
6721 && (next_r == -1 || next_r == -2))
6722 return 1 - next_r;
6723 return 1;
6724 }
6725
6726 /* Adjust all global syms defined in opd sections. In gcc generated
6727 code for the old ABI, these will already have been done. */
6728
6729 static bfd_boolean
6730 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6731 {
6732 struct ppc_link_hash_entry *eh;
6733 asection *sym_sec;
6734 struct _opd_sec_data *opd;
6735
6736 if (h->root.type == bfd_link_hash_indirect)
6737 return TRUE;
6738
6739 if (h->root.type != bfd_link_hash_defined
6740 && h->root.type != bfd_link_hash_defweak)
6741 return TRUE;
6742
6743 eh = (struct ppc_link_hash_entry *) h;
6744 if (eh->adjust_done)
6745 return TRUE;
6746
6747 sym_sec = eh->elf.root.u.def.section;
6748 opd = get_opd_info (sym_sec);
6749 if (opd != NULL && opd->adjust != NULL)
6750 {
6751 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
6752 if (adjust == -1)
6753 {
6754 /* This entry has been deleted. */
6755 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6756 if (dsec == NULL)
6757 {
6758 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6759 if (elf_discarded_section (dsec))
6760 {
6761 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6762 break;
6763 }
6764 }
6765 eh->elf.root.u.def.value = 0;
6766 eh->elf.root.u.def.section = dsec;
6767 }
6768 else
6769 eh->elf.root.u.def.value += adjust;
6770 eh->adjust_done = 1;
6771 }
6772 return TRUE;
6773 }
6774
6775 /* Handles decrementing dynamic reloc counts for the reloc specified by
6776 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6777 have already been determined. */
6778
6779 static bfd_boolean
6780 dec_dynrel_count (bfd_vma r_info,
6781 asection *sec,
6782 struct bfd_link_info *info,
6783 Elf_Internal_Sym **local_syms,
6784 struct elf_link_hash_entry *h,
6785 asection *sym_sec)
6786 {
6787 enum elf_ppc64_reloc_type r_type;
6788 struct elf_dyn_relocs *p;
6789 struct elf_dyn_relocs **pp;
6790
6791 /* Can this reloc be dynamic? This switch, and later tests here
6792 should be kept in sync with the code in check_relocs. */
6793 r_type = ELF64_R_TYPE (r_info);
6794 switch (r_type)
6795 {
6796 default:
6797 return TRUE;
6798
6799 case R_PPC64_TPREL16:
6800 case R_PPC64_TPREL16_LO:
6801 case R_PPC64_TPREL16_HI:
6802 case R_PPC64_TPREL16_HA:
6803 case R_PPC64_TPREL16_DS:
6804 case R_PPC64_TPREL16_LO_DS:
6805 case R_PPC64_TPREL16_HIGHER:
6806 case R_PPC64_TPREL16_HIGHERA:
6807 case R_PPC64_TPREL16_HIGHEST:
6808 case R_PPC64_TPREL16_HIGHESTA:
6809 if (!info->shared)
6810 return TRUE;
6811
6812 case R_PPC64_TPREL64:
6813 case R_PPC64_DTPMOD64:
6814 case R_PPC64_DTPREL64:
6815 case R_PPC64_ADDR64:
6816 case R_PPC64_REL30:
6817 case R_PPC64_REL32:
6818 case R_PPC64_REL64:
6819 case R_PPC64_ADDR14:
6820 case R_PPC64_ADDR14_BRNTAKEN:
6821 case R_PPC64_ADDR14_BRTAKEN:
6822 case R_PPC64_ADDR16:
6823 case R_PPC64_ADDR16_DS:
6824 case R_PPC64_ADDR16_HA:
6825 case R_PPC64_ADDR16_HI:
6826 case R_PPC64_ADDR16_HIGHER:
6827 case R_PPC64_ADDR16_HIGHERA:
6828 case R_PPC64_ADDR16_HIGHEST:
6829 case R_PPC64_ADDR16_HIGHESTA:
6830 case R_PPC64_ADDR16_LO:
6831 case R_PPC64_ADDR16_LO_DS:
6832 case R_PPC64_ADDR24:
6833 case R_PPC64_ADDR32:
6834 case R_PPC64_UADDR16:
6835 case R_PPC64_UADDR32:
6836 case R_PPC64_UADDR64:
6837 case R_PPC64_TOC:
6838 break;
6839 }
6840
6841 if (local_syms != NULL)
6842 {
6843 unsigned long r_symndx;
6844 Elf_Internal_Sym *sym;
6845 bfd *ibfd = sec->owner;
6846
6847 r_symndx = ELF64_R_SYM (r_info);
6848 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6849 return FALSE;
6850 }
6851
6852 if ((info->shared
6853 && (must_be_dyn_reloc (info, r_type)
6854 || (h != NULL
6855 && (!info->symbolic
6856 || h->root.type == bfd_link_hash_defweak
6857 || !h->def_regular))))
6858 || (ELIMINATE_COPY_RELOCS
6859 && !info->shared
6860 && h != NULL
6861 && (h->root.type == bfd_link_hash_defweak
6862 || !h->def_regular)))
6863 ;
6864 else
6865 return TRUE;
6866
6867 if (h != NULL)
6868 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6869 else
6870 {
6871 if (sym_sec != NULL)
6872 {
6873 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6874 pp = (struct elf_dyn_relocs **) vpp;
6875 }
6876 else
6877 {
6878 void *vpp = &elf_section_data (sec)->local_dynrel;
6879 pp = (struct elf_dyn_relocs **) vpp;
6880 }
6881
6882 /* elf_gc_sweep may have already removed all dyn relocs associated
6883 with local syms for a given section. Don't report a dynreloc
6884 miscount. */
6885 if (*pp == NULL)
6886 return TRUE;
6887 }
6888
6889 while ((p = *pp) != NULL)
6890 {
6891 if (p->sec == sec)
6892 {
6893 if (!must_be_dyn_reloc (info, r_type))
6894 p->pc_count -= 1;
6895 p->count -= 1;
6896 if (p->count == 0)
6897 *pp = p->next;
6898 return TRUE;
6899 }
6900 pp = &p->next;
6901 }
6902
6903 info->callbacks->einfo (_("dynreloc miscount for %B, section %A\n"),
6904 sec->owner, sec);
6905 bfd_set_error (bfd_error_bad_value);
6906 return FALSE;
6907 }
6908
6909 /* Remove unused Official Procedure Descriptor entries. Currently we
6910 only remove those associated with functions in discarded link-once
6911 sections, or weakly defined functions that have been overridden. It
6912 would be possible to remove many more entries for statically linked
6913 applications. */
6914
6915 bfd_boolean
6916 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
6917 {
6918 bfd *ibfd;
6919 bfd_boolean some_edited = FALSE;
6920 asection *need_pad = NULL;
6921
6922 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6923 {
6924 asection *sec;
6925 Elf_Internal_Rela *relstart, *rel, *relend;
6926 Elf_Internal_Shdr *symtab_hdr;
6927 Elf_Internal_Sym *local_syms;
6928 bfd_vma offset;
6929 struct _opd_sec_data *opd;
6930 bfd_boolean need_edit, add_aux_fields;
6931 bfd_size_type cnt_16b = 0;
6932
6933 if (!is_ppc64_elf (ibfd))
6934 continue;
6935
6936 sec = bfd_get_section_by_name (ibfd, ".opd");
6937 if (sec == NULL || sec->size == 0)
6938 continue;
6939
6940 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6941 continue;
6942
6943 if (sec->output_section == bfd_abs_section_ptr)
6944 continue;
6945
6946 /* Look through the section relocs. */
6947 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6948 continue;
6949
6950 local_syms = NULL;
6951 symtab_hdr = &elf_symtab_hdr (ibfd);
6952
6953 /* Read the relocations. */
6954 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6955 info->keep_memory);
6956 if (relstart == NULL)
6957 return FALSE;
6958
6959 /* First run through the relocs to check they are sane, and to
6960 determine whether we need to edit this opd section. */
6961 need_edit = FALSE;
6962 need_pad = sec;
6963 offset = 0;
6964 relend = relstart + sec->reloc_count;
6965 for (rel = relstart; rel < relend; )
6966 {
6967 enum elf_ppc64_reloc_type r_type;
6968 unsigned long r_symndx;
6969 asection *sym_sec;
6970 struct elf_link_hash_entry *h;
6971 Elf_Internal_Sym *sym;
6972
6973 /* .opd contains a regular array of 16 or 24 byte entries. We're
6974 only interested in the reloc pointing to a function entry
6975 point. */
6976 if (rel->r_offset != offset
6977 || rel + 1 >= relend
6978 || (rel + 1)->r_offset != offset + 8)
6979 {
6980 /* If someone messes with .opd alignment then after a
6981 "ld -r" we might have padding in the middle of .opd.
6982 Also, there's nothing to prevent someone putting
6983 something silly in .opd with the assembler. No .opd
6984 optimization for them! */
6985 broken_opd:
6986 (*_bfd_error_handler)
6987 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6988 need_edit = FALSE;
6989 break;
6990 }
6991
6992 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6993 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6994 {
6995 (*_bfd_error_handler)
6996 (_("%B: unexpected reloc type %u in .opd section"),
6997 ibfd, r_type);
6998 need_edit = FALSE;
6999 break;
7000 }
7001
7002 r_symndx = ELF64_R_SYM (rel->r_info);
7003 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7004 r_symndx, ibfd))
7005 goto error_ret;
7006
7007 if (sym_sec == NULL || sym_sec->owner == NULL)
7008 {
7009 const char *sym_name;
7010 if (h != NULL)
7011 sym_name = h->root.root.string;
7012 else
7013 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7014 sym_sec);
7015
7016 (*_bfd_error_handler)
7017 (_("%B: undefined sym `%s' in .opd section"),
7018 ibfd, sym_name);
7019 need_edit = FALSE;
7020 break;
7021 }
7022
7023 /* opd entries are always for functions defined in the
7024 current input bfd. If the symbol isn't defined in the
7025 input bfd, then we won't be using the function in this
7026 bfd; It must be defined in a linkonce section in another
7027 bfd, or is weak. It's also possible that we are
7028 discarding the function due to a linker script /DISCARD/,
7029 which we test for via the output_section. */
7030 if (sym_sec->owner != ibfd
7031 || sym_sec->output_section == bfd_abs_section_ptr)
7032 need_edit = TRUE;
7033
7034 rel += 2;
7035 if (rel == relend
7036 || (rel + 1 == relend && rel->r_offset == offset + 16))
7037 {
7038 if (sec->size == offset + 24)
7039 {
7040 need_pad = NULL;
7041 break;
7042 }
7043 if (rel == relend && sec->size == offset + 16)
7044 {
7045 cnt_16b++;
7046 break;
7047 }
7048 goto broken_opd;
7049 }
7050
7051 if (rel->r_offset == offset + 24)
7052 offset += 24;
7053 else if (rel->r_offset != offset + 16)
7054 goto broken_opd;
7055 else if (rel + 1 < relend
7056 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7057 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7058 {
7059 offset += 16;
7060 cnt_16b++;
7061 }
7062 else if (rel + 2 < relend
7063 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7064 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7065 {
7066 offset += 24;
7067 rel += 1;
7068 }
7069 else
7070 goto broken_opd;
7071 }
7072
7073 add_aux_fields = non_overlapping && cnt_16b > 0;
7074
7075 if (need_edit || add_aux_fields)
7076 {
7077 Elf_Internal_Rela *write_rel;
7078 Elf_Internal_Shdr *rel_hdr;
7079 bfd_byte *rptr, *wptr;
7080 bfd_byte *new_contents;
7081 bfd_boolean skip;
7082 long opd_ent_size;
7083 bfd_size_type amt;
7084
7085 new_contents = NULL;
7086 amt = sec->size * sizeof (long) / 8;
7087 opd = &ppc64_elf_section_data (sec)->u.opd;
7088 opd->adjust = bfd_zalloc (sec->owner, amt);
7089 if (opd->adjust == NULL)
7090 return FALSE;
7091 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7092
7093 /* This seems a waste of time as input .opd sections are all
7094 zeros as generated by gcc, but I suppose there's no reason
7095 this will always be so. We might start putting something in
7096 the third word of .opd entries. */
7097 if ((sec->flags & SEC_IN_MEMORY) == 0)
7098 {
7099 bfd_byte *loc;
7100 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7101 {
7102 if (loc != NULL)
7103 free (loc);
7104 error_ret:
7105 if (local_syms != NULL
7106 && symtab_hdr->contents != (unsigned char *) local_syms)
7107 free (local_syms);
7108 if (elf_section_data (sec)->relocs != relstart)
7109 free (relstart);
7110 return FALSE;
7111 }
7112 sec->contents = loc;
7113 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7114 }
7115
7116 elf_section_data (sec)->relocs = relstart;
7117
7118 new_contents = sec->contents;
7119 if (add_aux_fields)
7120 {
7121 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7122 if (new_contents == NULL)
7123 return FALSE;
7124 need_pad = FALSE;
7125 }
7126 wptr = new_contents;
7127 rptr = sec->contents;
7128
7129 write_rel = relstart;
7130 skip = FALSE;
7131 offset = 0;
7132 opd_ent_size = 0;
7133 for (rel = relstart; rel < relend; rel++)
7134 {
7135 unsigned long r_symndx;
7136 asection *sym_sec;
7137 struct elf_link_hash_entry *h;
7138 Elf_Internal_Sym *sym;
7139
7140 r_symndx = ELF64_R_SYM (rel->r_info);
7141 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7142 r_symndx, ibfd))
7143 goto error_ret;
7144
7145 if (rel->r_offset == offset)
7146 {
7147 struct ppc_link_hash_entry *fdh = NULL;
7148
7149 /* See if the .opd entry is full 24 byte or
7150 16 byte (with fd_aux entry overlapped with next
7151 fd_func). */
7152 opd_ent_size = 24;
7153 if ((rel + 2 == relend && sec->size == offset + 16)
7154 || (rel + 3 < relend
7155 && rel[2].r_offset == offset + 16
7156 && rel[3].r_offset == offset + 24
7157 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7158 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7159 opd_ent_size = 16;
7160
7161 if (h != NULL
7162 && h->root.root.string[0] == '.')
7163 {
7164 struct ppc_link_hash_table *htab;
7165
7166 htab = ppc_hash_table (info);
7167 if (htab != NULL)
7168 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7169 htab);
7170 if (fdh != NULL
7171 && fdh->elf.root.type != bfd_link_hash_defined
7172 && fdh->elf.root.type != bfd_link_hash_defweak)
7173 fdh = NULL;
7174 }
7175
7176 skip = (sym_sec->owner != ibfd
7177 || sym_sec->output_section == bfd_abs_section_ptr);
7178 if (skip)
7179 {
7180 if (fdh != NULL && sym_sec->owner == ibfd)
7181 {
7182 /* Arrange for the function descriptor sym
7183 to be dropped. */
7184 fdh->elf.root.u.def.value = 0;
7185 fdh->elf.root.u.def.section = sym_sec;
7186 }
7187 opd->adjust[rel->r_offset / 8] = -1;
7188 }
7189 else
7190 {
7191 /* We'll be keeping this opd entry. */
7192
7193 if (fdh != NULL)
7194 {
7195 /* Redefine the function descriptor symbol to
7196 this location in the opd section. It is
7197 necessary to update the value here rather
7198 than using an array of adjustments as we do
7199 for local symbols, because various places
7200 in the generic ELF code use the value
7201 stored in u.def.value. */
7202 fdh->elf.root.u.def.value = wptr - new_contents;
7203 fdh->adjust_done = 1;
7204 }
7205
7206 /* Local syms are a bit tricky. We could
7207 tweak them as they can be cached, but
7208 we'd need to look through the local syms
7209 for the function descriptor sym which we
7210 don't have at the moment. So keep an
7211 array of adjustments. */
7212 opd->adjust[rel->r_offset / 8]
7213 = (wptr - new_contents) - (rptr - sec->contents);
7214
7215 if (wptr != rptr)
7216 memcpy (wptr, rptr, opd_ent_size);
7217 wptr += opd_ent_size;
7218 if (add_aux_fields && opd_ent_size == 16)
7219 {
7220 memset (wptr, '\0', 8);
7221 wptr += 8;
7222 }
7223 }
7224 rptr += opd_ent_size;
7225 offset += opd_ent_size;
7226 }
7227
7228 if (skip)
7229 {
7230 if (!NO_OPD_RELOCS
7231 && !info->relocatable
7232 && !dec_dynrel_count (rel->r_info, sec, info,
7233 NULL, h, sym_sec))
7234 goto error_ret;
7235 }
7236 else
7237 {
7238 /* We need to adjust any reloc offsets to point to the
7239 new opd entries. While we're at it, we may as well
7240 remove redundant relocs. */
7241 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7242 if (write_rel != rel)
7243 memcpy (write_rel, rel, sizeof (*rel));
7244 ++write_rel;
7245 }
7246 }
7247
7248 sec->size = wptr - new_contents;
7249 sec->reloc_count = write_rel - relstart;
7250 if (add_aux_fields)
7251 {
7252 free (sec->contents);
7253 sec->contents = new_contents;
7254 }
7255
7256 /* Fudge the header size too, as this is used later in
7257 elf_bfd_final_link if we are emitting relocs. */
7258 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7259 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7260 some_edited = TRUE;
7261 }
7262 else if (elf_section_data (sec)->relocs != relstart)
7263 free (relstart);
7264
7265 if (local_syms != NULL
7266 && symtab_hdr->contents != (unsigned char *) local_syms)
7267 {
7268 if (!info->keep_memory)
7269 free (local_syms);
7270 else
7271 symtab_hdr->contents = (unsigned char *) local_syms;
7272 }
7273 }
7274
7275 if (some_edited)
7276 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7277
7278 /* If we are doing a final link and the last .opd entry is just 16 byte
7279 long, add a 8 byte padding after it. */
7280 if (need_pad != NULL && !info->relocatable)
7281 {
7282 bfd_byte *p;
7283
7284 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7285 {
7286 BFD_ASSERT (need_pad->size > 0);
7287
7288 p = bfd_malloc (need_pad->size + 8);
7289 if (p == NULL)
7290 return FALSE;
7291
7292 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7293 p, 0, need_pad->size))
7294 return FALSE;
7295
7296 need_pad->contents = p;
7297 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7298 }
7299 else
7300 {
7301 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7302 if (p == NULL)
7303 return FALSE;
7304
7305 need_pad->contents = p;
7306 }
7307
7308 memset (need_pad->contents + need_pad->size, 0, 8);
7309 need_pad->size += 8;
7310 }
7311
7312 return TRUE;
7313 }
7314
7315 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7316
7317 asection *
7318 ppc64_elf_tls_setup (struct bfd_link_info *info,
7319 int no_tls_get_addr_opt,
7320 int *no_multi_toc)
7321 {
7322 struct ppc_link_hash_table *htab;
7323
7324 htab = ppc_hash_table (info);
7325 if (htab == NULL)
7326 return NULL;
7327
7328 if (*no_multi_toc)
7329 htab->do_multi_toc = 0;
7330 else if (!htab->do_multi_toc)
7331 *no_multi_toc = 1;
7332
7333 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7334 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7335 FALSE, FALSE, TRUE));
7336 /* Move dynamic linking info to the function descriptor sym. */
7337 if (htab->tls_get_addr != NULL)
7338 func_desc_adjust (&htab->tls_get_addr->elf, info);
7339 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7340 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7341 FALSE, FALSE, TRUE));
7342 if (!no_tls_get_addr_opt)
7343 {
7344 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7345
7346 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7347 FALSE, FALSE, TRUE);
7348 if (opt != NULL)
7349 func_desc_adjust (opt, info);
7350 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7351 FALSE, FALSE, TRUE);
7352 if (opt_fd != NULL
7353 && (opt_fd->root.type == bfd_link_hash_defined
7354 || opt_fd->root.type == bfd_link_hash_defweak))
7355 {
7356 /* If glibc supports an optimized __tls_get_addr call stub,
7357 signalled by the presence of __tls_get_addr_opt, and we'll
7358 be calling __tls_get_addr via a plt call stub, then
7359 make __tls_get_addr point to __tls_get_addr_opt. */
7360 tga_fd = &htab->tls_get_addr_fd->elf;
7361 if (htab->elf.dynamic_sections_created
7362 && tga_fd != NULL
7363 && (tga_fd->type == STT_FUNC
7364 || tga_fd->needs_plt)
7365 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7366 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7367 && tga_fd->root.type == bfd_link_hash_undefweak)))
7368 {
7369 struct plt_entry *ent;
7370
7371 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7372 if (ent->plt.refcount > 0)
7373 break;
7374 if (ent != NULL)
7375 {
7376 tga_fd->root.type = bfd_link_hash_indirect;
7377 tga_fd->root.u.i.link = &opt_fd->root;
7378 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7379 if (opt_fd->dynindx != -1)
7380 {
7381 /* Use __tls_get_addr_opt in dynamic relocations. */
7382 opt_fd->dynindx = -1;
7383 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7384 opt_fd->dynstr_index);
7385 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7386 return NULL;
7387 }
7388 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7389 tga = &htab->tls_get_addr->elf;
7390 if (opt != NULL && tga != NULL)
7391 {
7392 tga->root.type = bfd_link_hash_indirect;
7393 tga->root.u.i.link = &opt->root;
7394 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7395 _bfd_elf_link_hash_hide_symbol (info, opt,
7396 tga->forced_local);
7397 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7398 }
7399 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7400 htab->tls_get_addr_fd->is_func_descriptor = 1;
7401 if (htab->tls_get_addr != NULL)
7402 {
7403 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7404 htab->tls_get_addr->is_func = 1;
7405 }
7406 }
7407 }
7408 }
7409 else
7410 no_tls_get_addr_opt = TRUE;
7411 }
7412 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7413 return _bfd_elf_tls_setup (info->output_bfd, info);
7414 }
7415
7416 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7417 HASH1 or HASH2. */
7418
7419 static bfd_boolean
7420 branch_reloc_hash_match (const bfd *ibfd,
7421 const Elf_Internal_Rela *rel,
7422 const struct ppc_link_hash_entry *hash1,
7423 const struct ppc_link_hash_entry *hash2)
7424 {
7425 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7426 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7427 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7428
7429 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7430 {
7431 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7432 struct elf_link_hash_entry *h;
7433
7434 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7435 h = elf_follow_link (h);
7436 if (h == &hash1->elf || h == &hash2->elf)
7437 return TRUE;
7438 }
7439 return FALSE;
7440 }
7441
7442 /* Run through all the TLS relocs looking for optimization
7443 opportunities. The linker has been hacked (see ppc64elf.em) to do
7444 a preliminary section layout so that we know the TLS segment
7445 offsets. We can't optimize earlier because some optimizations need
7446 to know the tp offset, and we need to optimize before allocating
7447 dynamic relocations. */
7448
7449 bfd_boolean
7450 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7451 {
7452 bfd *ibfd;
7453 asection *sec;
7454 struct ppc_link_hash_table *htab;
7455 unsigned char *toc_ref;
7456 int pass;
7457
7458 if (info->relocatable || !info->executable)
7459 return TRUE;
7460
7461 htab = ppc_hash_table (info);
7462 if (htab == NULL)
7463 return FALSE;
7464
7465 /* Make two passes over the relocs. On the first pass, mark toc
7466 entries involved with tls relocs, and check that tls relocs
7467 involved in setting up a tls_get_addr call are indeed followed by
7468 such a call. If they are not, we can't do any tls optimization.
7469 On the second pass twiddle tls_mask flags to notify
7470 relocate_section that optimization can be done, and adjust got
7471 and plt refcounts. */
7472 toc_ref = NULL;
7473 for (pass = 0; pass < 2; ++pass)
7474 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7475 {
7476 Elf_Internal_Sym *locsyms = NULL;
7477 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7478
7479 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7480 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7481 {
7482 Elf_Internal_Rela *relstart, *rel, *relend;
7483 bfd_boolean found_tls_get_addr_arg = 0;
7484
7485 /* Read the relocations. */
7486 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7487 info->keep_memory);
7488 if (relstart == NULL)
7489 return FALSE;
7490
7491 relend = relstart + sec->reloc_count;
7492 for (rel = relstart; rel < relend; rel++)
7493 {
7494 enum elf_ppc64_reloc_type r_type;
7495 unsigned long r_symndx;
7496 struct elf_link_hash_entry *h;
7497 Elf_Internal_Sym *sym;
7498 asection *sym_sec;
7499 unsigned char *tls_mask;
7500 unsigned char tls_set, tls_clear, tls_type = 0;
7501 bfd_vma value;
7502 bfd_boolean ok_tprel, is_local;
7503 long toc_ref_index = 0;
7504 int expecting_tls_get_addr = 0;
7505 bfd_boolean ret = FALSE;
7506
7507 r_symndx = ELF64_R_SYM (rel->r_info);
7508 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7509 r_symndx, ibfd))
7510 {
7511 err_free_rel:
7512 if (elf_section_data (sec)->relocs != relstart)
7513 free (relstart);
7514 if (toc_ref != NULL)
7515 free (toc_ref);
7516 if (locsyms != NULL
7517 && (elf_symtab_hdr (ibfd).contents
7518 != (unsigned char *) locsyms))
7519 free (locsyms);
7520 return ret;
7521 }
7522
7523 if (h != NULL)
7524 {
7525 if (h->root.type == bfd_link_hash_defined
7526 || h->root.type == bfd_link_hash_defweak)
7527 value = h->root.u.def.value;
7528 else if (h->root.type == bfd_link_hash_undefweak)
7529 value = 0;
7530 else
7531 {
7532 found_tls_get_addr_arg = 0;
7533 continue;
7534 }
7535 }
7536 else
7537 /* Symbols referenced by TLS relocs must be of type
7538 STT_TLS. So no need for .opd local sym adjust. */
7539 value = sym->st_value;
7540
7541 ok_tprel = FALSE;
7542 is_local = FALSE;
7543 if (h == NULL
7544 || !h->def_dynamic)
7545 {
7546 is_local = TRUE;
7547 if (h != NULL
7548 && h->root.type == bfd_link_hash_undefweak)
7549 ok_tprel = TRUE;
7550 else
7551 {
7552 value += sym_sec->output_offset;
7553 value += sym_sec->output_section->vma;
7554 value -= htab->elf.tls_sec->vma;
7555 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7556 < (bfd_vma) 1 << 32);
7557 }
7558 }
7559
7560 r_type = ELF64_R_TYPE (rel->r_info);
7561 /* If this section has old-style __tls_get_addr calls
7562 without marker relocs, then check that each
7563 __tls_get_addr call reloc is preceded by a reloc
7564 that conceivably belongs to the __tls_get_addr arg
7565 setup insn. If we don't find matching arg setup
7566 relocs, don't do any tls optimization. */
7567 if (pass == 0
7568 && sec->has_tls_get_addr_call
7569 && h != NULL
7570 && (h == &htab->tls_get_addr->elf
7571 || h == &htab->tls_get_addr_fd->elf)
7572 && !found_tls_get_addr_arg
7573 && is_branch_reloc (r_type))
7574 {
7575 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
7576 "TLS optimization disabled\n"),
7577 ibfd, sec, rel->r_offset);
7578 ret = TRUE;
7579 goto err_free_rel;
7580 }
7581
7582 found_tls_get_addr_arg = 0;
7583 switch (r_type)
7584 {
7585 case R_PPC64_GOT_TLSLD16:
7586 case R_PPC64_GOT_TLSLD16_LO:
7587 expecting_tls_get_addr = 1;
7588 found_tls_get_addr_arg = 1;
7589 /* Fall thru */
7590
7591 case R_PPC64_GOT_TLSLD16_HI:
7592 case R_PPC64_GOT_TLSLD16_HA:
7593 /* These relocs should never be against a symbol
7594 defined in a shared lib. Leave them alone if
7595 that turns out to be the case. */
7596 if (!is_local)
7597 continue;
7598
7599 /* LD -> LE */
7600 tls_set = 0;
7601 tls_clear = TLS_LD;
7602 tls_type = TLS_TLS | TLS_LD;
7603 break;
7604
7605 case R_PPC64_GOT_TLSGD16:
7606 case R_PPC64_GOT_TLSGD16_LO:
7607 expecting_tls_get_addr = 1;
7608 found_tls_get_addr_arg = 1;
7609 /* Fall thru */
7610
7611 case R_PPC64_GOT_TLSGD16_HI:
7612 case R_PPC64_GOT_TLSGD16_HA:
7613 if (ok_tprel)
7614 /* GD -> LE */
7615 tls_set = 0;
7616 else
7617 /* GD -> IE */
7618 tls_set = TLS_TLS | TLS_TPRELGD;
7619 tls_clear = TLS_GD;
7620 tls_type = TLS_TLS | TLS_GD;
7621 break;
7622
7623 case R_PPC64_GOT_TPREL16_DS:
7624 case R_PPC64_GOT_TPREL16_LO_DS:
7625 case R_PPC64_GOT_TPREL16_HI:
7626 case R_PPC64_GOT_TPREL16_HA:
7627 if (ok_tprel)
7628 {
7629 /* IE -> LE */
7630 tls_set = 0;
7631 tls_clear = TLS_TPREL;
7632 tls_type = TLS_TLS | TLS_TPREL;
7633 break;
7634 }
7635 continue;
7636
7637 case R_PPC64_TLSGD:
7638 case R_PPC64_TLSLD:
7639 found_tls_get_addr_arg = 1;
7640 /* Fall thru */
7641
7642 case R_PPC64_TLS:
7643 case R_PPC64_TOC16:
7644 case R_PPC64_TOC16_LO:
7645 if (sym_sec == NULL || sym_sec != toc)
7646 continue;
7647
7648 /* Mark this toc entry as referenced by a TLS
7649 code sequence. We can do that now in the
7650 case of R_PPC64_TLS, and after checking for
7651 tls_get_addr for the TOC16 relocs. */
7652 if (toc_ref == NULL)
7653 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7654 if (toc_ref == NULL)
7655 goto err_free_rel;
7656
7657 if (h != NULL)
7658 value = h->root.u.def.value;
7659 else
7660 value = sym->st_value;
7661 value += rel->r_addend;
7662 BFD_ASSERT (value < toc->size && value % 8 == 0);
7663 toc_ref_index = (value + toc->output_offset) / 8;
7664 if (r_type == R_PPC64_TLS
7665 || r_type == R_PPC64_TLSGD
7666 || r_type == R_PPC64_TLSLD)
7667 {
7668 toc_ref[toc_ref_index] = 1;
7669 continue;
7670 }
7671
7672 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7673 continue;
7674
7675 tls_set = 0;
7676 tls_clear = 0;
7677 expecting_tls_get_addr = 2;
7678 break;
7679
7680 case R_PPC64_TPREL64:
7681 if (pass == 0
7682 || sec != toc
7683 || toc_ref == NULL
7684 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7685 continue;
7686 if (ok_tprel)
7687 {
7688 /* IE -> LE */
7689 tls_set = TLS_EXPLICIT;
7690 tls_clear = TLS_TPREL;
7691 break;
7692 }
7693 continue;
7694
7695 case R_PPC64_DTPMOD64:
7696 if (pass == 0
7697 || sec != toc
7698 || toc_ref == NULL
7699 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
7700 continue;
7701 if (rel + 1 < relend
7702 && (rel[1].r_info
7703 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7704 && rel[1].r_offset == rel->r_offset + 8)
7705 {
7706 if (ok_tprel)
7707 /* GD -> LE */
7708 tls_set = TLS_EXPLICIT | TLS_GD;
7709 else
7710 /* GD -> IE */
7711 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7712 tls_clear = TLS_GD;
7713 }
7714 else
7715 {
7716 if (!is_local)
7717 continue;
7718
7719 /* LD -> LE */
7720 tls_set = TLS_EXPLICIT;
7721 tls_clear = TLS_LD;
7722 }
7723 break;
7724
7725 default:
7726 continue;
7727 }
7728
7729 if (pass == 0)
7730 {
7731 if (!expecting_tls_get_addr
7732 || !sec->has_tls_get_addr_call)
7733 continue;
7734
7735 if (rel + 1 < relend
7736 && branch_reloc_hash_match (ibfd, rel + 1,
7737 htab->tls_get_addr,
7738 htab->tls_get_addr_fd))
7739 {
7740 if (expecting_tls_get_addr == 2)
7741 {
7742 /* Check for toc tls entries. */
7743 unsigned char *toc_tls;
7744 int retval;
7745
7746 retval = get_tls_mask (&toc_tls, NULL, NULL,
7747 &locsyms,
7748 rel, ibfd);
7749 if (retval == 0)
7750 goto err_free_rel;
7751 if (toc_tls != NULL)
7752 {
7753 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7754 found_tls_get_addr_arg = 1;
7755 if (retval > 1)
7756 toc_ref[toc_ref_index] = 1;
7757 }
7758 }
7759 continue;
7760 }
7761
7762 if (expecting_tls_get_addr != 1)
7763 continue;
7764
7765 /* Uh oh, we didn't find the expected call. We
7766 could just mark this symbol to exclude it
7767 from tls optimization but it's safer to skip
7768 the entire optimization. */
7769 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
7770 "TLS optimization disabled\n"),
7771 ibfd, sec, rel->r_offset);
7772 ret = TRUE;
7773 goto err_free_rel;
7774 }
7775
7776 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
7777 {
7778 struct plt_entry *ent;
7779 for (ent = htab->tls_get_addr->elf.plt.plist;
7780 ent != NULL;
7781 ent = ent->next)
7782 if (ent->addend == 0)
7783 {
7784 if (ent->plt.refcount > 0)
7785 {
7786 ent->plt.refcount -= 1;
7787 expecting_tls_get_addr = 0;
7788 }
7789 break;
7790 }
7791 }
7792
7793 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
7794 {
7795 struct plt_entry *ent;
7796 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7797 ent != NULL;
7798 ent = ent->next)
7799 if (ent->addend == 0)
7800 {
7801 if (ent->plt.refcount > 0)
7802 ent->plt.refcount -= 1;
7803 break;
7804 }
7805 }
7806
7807 if (tls_clear == 0)
7808 continue;
7809
7810 if ((tls_set & TLS_EXPLICIT) == 0)
7811 {
7812 struct got_entry *ent;
7813
7814 /* Adjust got entry for this reloc. */
7815 if (h != NULL)
7816 ent = h->got.glist;
7817 else
7818 ent = elf_local_got_ents (ibfd)[r_symndx];
7819
7820 for (; ent != NULL; ent = ent->next)
7821 if (ent->addend == rel->r_addend
7822 && ent->owner == ibfd
7823 && ent->tls_type == tls_type)
7824 break;
7825 if (ent == NULL)
7826 abort ();
7827
7828 if (tls_set == 0)
7829 {
7830 /* We managed to get rid of a got entry. */
7831 if (ent->got.refcount > 0)
7832 ent->got.refcount -= 1;
7833 }
7834 }
7835 else
7836 {
7837 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7838 we'll lose one or two dyn relocs. */
7839 if (!dec_dynrel_count (rel->r_info, sec, info,
7840 NULL, h, sym_sec))
7841 return FALSE;
7842
7843 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7844 {
7845 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7846 NULL, h, sym_sec))
7847 return FALSE;
7848 }
7849 }
7850
7851 *tls_mask |= tls_set;
7852 *tls_mask &= ~tls_clear;
7853 }
7854
7855 if (elf_section_data (sec)->relocs != relstart)
7856 free (relstart);
7857 }
7858
7859 if (locsyms != NULL
7860 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7861 {
7862 if (!info->keep_memory)
7863 free (locsyms);
7864 else
7865 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7866 }
7867 }
7868
7869 if (toc_ref != NULL)
7870 free (toc_ref);
7871 return TRUE;
7872 }
7873
7874 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7875 the values of any global symbols in a toc section that has been
7876 edited. Globals in toc sections should be a rarity, so this function
7877 sets a flag if any are found in toc sections other than the one just
7878 edited, so that futher hash table traversals can be avoided. */
7879
7880 struct adjust_toc_info
7881 {
7882 asection *toc;
7883 unsigned long *skip;
7884 bfd_boolean global_toc_syms;
7885 };
7886
7887 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7888
7889 static bfd_boolean
7890 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7891 {
7892 struct ppc_link_hash_entry *eh;
7893 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7894 unsigned long i;
7895
7896 if (h->root.type == bfd_link_hash_indirect)
7897 return TRUE;
7898
7899 if (h->root.type != bfd_link_hash_defined
7900 && h->root.type != bfd_link_hash_defweak)
7901 return TRUE;
7902
7903 eh = (struct ppc_link_hash_entry *) h;
7904 if (eh->adjust_done)
7905 return TRUE;
7906
7907 if (eh->elf.root.u.def.section == toc_inf->toc)
7908 {
7909 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7910 i = toc_inf->toc->rawsize >> 3;
7911 else
7912 i = eh->elf.root.u.def.value >> 3;
7913
7914 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
7915 {
7916 (*_bfd_error_handler)
7917 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7918 do
7919 ++i;
7920 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
7921 eh->elf.root.u.def.value = (bfd_vma) i << 3;
7922 }
7923
7924 eh->elf.root.u.def.value -= toc_inf->skip[i];
7925 eh->adjust_done = 1;
7926 }
7927 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7928 toc_inf->global_toc_syms = TRUE;
7929
7930 return TRUE;
7931 }
7932
7933 /* Examine all relocs referencing .toc sections in order to remove
7934 unused .toc entries. */
7935
7936 bfd_boolean
7937 ppc64_elf_edit_toc (struct bfd_link_info *info)
7938 {
7939 bfd *ibfd;
7940 struct adjust_toc_info toc_inf;
7941 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7942
7943 htab->do_toc_opt = 1;
7944 toc_inf.global_toc_syms = TRUE;
7945 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7946 {
7947 asection *toc, *sec;
7948 Elf_Internal_Shdr *symtab_hdr;
7949 Elf_Internal_Sym *local_syms;
7950 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
7951 unsigned long *skip, *drop;
7952 unsigned char *used;
7953 unsigned char *keep, last, some_unused;
7954
7955 if (!is_ppc64_elf (ibfd))
7956 continue;
7957
7958 toc = bfd_get_section_by_name (ibfd, ".toc");
7959 if (toc == NULL
7960 || toc->size == 0
7961 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7962 || elf_discarded_section (toc))
7963 continue;
7964
7965 toc_relocs = NULL;
7966 local_syms = NULL;
7967 symtab_hdr = &elf_symtab_hdr (ibfd);
7968
7969 /* Look at sections dropped from the final link. */
7970 skip = NULL;
7971 relstart = NULL;
7972 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7973 {
7974 if (sec->reloc_count == 0
7975 || !elf_discarded_section (sec)
7976 || get_opd_info (sec)
7977 || (sec->flags & SEC_ALLOC) == 0
7978 || (sec->flags & SEC_DEBUGGING) != 0)
7979 continue;
7980
7981 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7982 if (relstart == NULL)
7983 goto error_ret;
7984
7985 /* Run through the relocs to see which toc entries might be
7986 unused. */
7987 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7988 {
7989 enum elf_ppc64_reloc_type r_type;
7990 unsigned long r_symndx;
7991 asection *sym_sec;
7992 struct elf_link_hash_entry *h;
7993 Elf_Internal_Sym *sym;
7994 bfd_vma val;
7995
7996 r_type = ELF64_R_TYPE (rel->r_info);
7997 switch (r_type)
7998 {
7999 default:
8000 continue;
8001
8002 case R_PPC64_TOC16:
8003 case R_PPC64_TOC16_LO:
8004 case R_PPC64_TOC16_HI:
8005 case R_PPC64_TOC16_HA:
8006 case R_PPC64_TOC16_DS:
8007 case R_PPC64_TOC16_LO_DS:
8008 break;
8009 }
8010
8011 r_symndx = ELF64_R_SYM (rel->r_info);
8012 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8013 r_symndx, ibfd))
8014 goto error_ret;
8015
8016 if (sym_sec != toc)
8017 continue;
8018
8019 if (h != NULL)
8020 val = h->root.u.def.value;
8021 else
8022 val = sym->st_value;
8023 val += rel->r_addend;
8024
8025 if (val >= toc->size)
8026 continue;
8027
8028 /* Anything in the toc ought to be aligned to 8 bytes.
8029 If not, don't mark as unused. */
8030 if (val & 7)
8031 continue;
8032
8033 if (skip == NULL)
8034 {
8035 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8036 if (skip == NULL)
8037 goto error_ret;
8038 }
8039
8040 skip[val >> 3] = ref_from_discarded;
8041 }
8042
8043 if (elf_section_data (sec)->relocs != relstart)
8044 free (relstart);
8045 }
8046
8047 /* For largetoc loads of address constants, we can convert
8048 . addis rx,2,addr@got@ha
8049 . ld ry,addr@got@l(rx)
8050 to
8051 . addis rx,2,addr@toc@ha
8052 . addi ry,rx,addr@toc@l
8053 when addr is within 2G of the toc pointer. This then means
8054 that the word storing "addr" in the toc is no longer needed. */
8055
8056 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8057 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8058 && toc->reloc_count != 0)
8059 {
8060 /* Read toc relocs. */
8061 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8062 info->keep_memory);
8063 if (toc_relocs == NULL)
8064 goto error_ret;
8065
8066 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8067 {
8068 enum elf_ppc64_reloc_type r_type;
8069 unsigned long r_symndx;
8070 asection *sym_sec;
8071 struct elf_link_hash_entry *h;
8072 Elf_Internal_Sym *sym;
8073 bfd_vma val, addr;
8074
8075 r_type = ELF64_R_TYPE (rel->r_info);
8076 if (r_type != R_PPC64_ADDR64)
8077 continue;
8078
8079 r_symndx = ELF64_R_SYM (rel->r_info);
8080 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8081 r_symndx, ibfd))
8082 goto error_ret;
8083
8084 if (sym_sec == NULL
8085 || elf_discarded_section (sym_sec))
8086 continue;
8087
8088 if (!SYMBOL_CALLS_LOCAL (info, h))
8089 continue;
8090
8091 if (h != NULL)
8092 {
8093 if (h->type == STT_GNU_IFUNC)
8094 continue;
8095 val = h->root.u.def.value;
8096 }
8097 else
8098 {
8099 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8100 continue;
8101 val = sym->st_value;
8102 }
8103 val += rel->r_addend;
8104 val += sym_sec->output_section->vma + sym_sec->output_offset;
8105
8106 /* We don't yet know the exact toc pointer value, but we
8107 know it will be somewhere in the toc section. Don't
8108 optimize if the difference from any possible toc
8109 pointer is outside [ff..f80008000, 7fff7fff]. */
8110 addr = toc->output_section->vma + TOC_BASE_OFF;
8111 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8112 continue;
8113
8114 addr = toc->output_section->vma + toc->output_section->rawsize;
8115 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8116 continue;
8117
8118 if (skip == NULL)
8119 {
8120 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8121 if (skip == NULL)
8122 goto error_ret;
8123 }
8124
8125 skip[rel->r_offset >> 3]
8126 |= can_optimize | ((rel - toc_relocs) << 2);
8127 }
8128 }
8129
8130 if (skip == NULL)
8131 continue;
8132
8133 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8134 if (used == NULL)
8135 {
8136 error_ret:
8137 if (local_syms != NULL
8138 && symtab_hdr->contents != (unsigned char *) local_syms)
8139 free (local_syms);
8140 if (sec != NULL
8141 && relstart != NULL
8142 && elf_section_data (sec)->relocs != relstart)
8143 free (relstart);
8144 if (toc_relocs != NULL
8145 && elf_section_data (toc)->relocs != toc_relocs)
8146 free (toc_relocs);
8147 if (skip != NULL)
8148 free (skip);
8149 return FALSE;
8150 }
8151
8152 /* Now check all kept sections that might reference the toc.
8153 Check the toc itself last. */
8154 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8155 : ibfd->sections);
8156 sec != NULL;
8157 sec = (sec == toc ? NULL
8158 : sec->next == NULL ? toc
8159 : sec->next == toc && toc->next ? toc->next
8160 : sec->next))
8161 {
8162 int repeat;
8163
8164 if (sec->reloc_count == 0
8165 || elf_discarded_section (sec)
8166 || get_opd_info (sec)
8167 || (sec->flags & SEC_ALLOC) == 0
8168 || (sec->flags & SEC_DEBUGGING) != 0)
8169 continue;
8170
8171 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8172 info->keep_memory);
8173 if (relstart == NULL)
8174 goto error_ret;
8175
8176 /* Mark toc entries referenced as used. */
8177 repeat = 0;
8178 do
8179 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8180 {
8181 enum elf_ppc64_reloc_type r_type;
8182 unsigned long r_symndx;
8183 asection *sym_sec;
8184 struct elf_link_hash_entry *h;
8185 Elf_Internal_Sym *sym;
8186 bfd_vma val;
8187
8188 r_type = ELF64_R_TYPE (rel->r_info);
8189 switch (r_type)
8190 {
8191 case R_PPC64_TOC16:
8192 case R_PPC64_TOC16_LO:
8193 case R_PPC64_TOC16_HI:
8194 case R_PPC64_TOC16_HA:
8195 case R_PPC64_TOC16_DS:
8196 case R_PPC64_TOC16_LO_DS:
8197 /* In case we're taking addresses of toc entries. */
8198 case R_PPC64_ADDR64:
8199 break;
8200
8201 default:
8202 continue;
8203 }
8204
8205 r_symndx = ELF64_R_SYM (rel->r_info);
8206 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8207 r_symndx, ibfd))
8208 {
8209 free (used);
8210 goto error_ret;
8211 }
8212
8213 if (sym_sec != toc)
8214 continue;
8215
8216 if (h != NULL)
8217 val = h->root.u.def.value;
8218 else
8219 val = sym->st_value;
8220 val += rel->r_addend;
8221
8222 if (val >= toc->size)
8223 continue;
8224
8225 if ((skip[val >> 3] & can_optimize) != 0)
8226 {
8227 bfd_vma off;
8228 unsigned char opc;
8229
8230 switch (r_type)
8231 {
8232 case R_PPC64_TOC16_HA:
8233 break;
8234
8235 case R_PPC64_TOC16_LO_DS:
8236 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8237 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8238 return FALSE;
8239 if ((opc & (0x3f << 2)) == (58u << 2))
8240 break;
8241 /* Fall thru */
8242
8243 default:
8244 /* Wrong sort of reloc, or not a ld. We may
8245 as well clear ref_from_discarded too. */
8246 skip[val >> 3] = 0;
8247 }
8248 }
8249
8250 /* For the toc section, we only mark as used if
8251 this entry itself isn't unused. */
8252 if (sec == toc
8253 && !used[val >> 3]
8254 && (used[rel->r_offset >> 3]
8255 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
8256 /* Do all the relocs again, to catch reference
8257 chains. */
8258 repeat = 1;
8259
8260 used[val >> 3] = 1;
8261 }
8262 while (repeat);
8263
8264 if (elf_section_data (sec)->relocs != relstart)
8265 free (relstart);
8266 }
8267
8268 /* Merge the used and skip arrays. Assume that TOC
8269 doublewords not appearing as either used or unused belong
8270 to to an entry more than one doubleword in size. */
8271 for (drop = skip, keep = used, last = 0, some_unused = 0;
8272 drop < skip + (toc->size + 7) / 8;
8273 ++drop, ++keep)
8274 {
8275 if (*keep)
8276 {
8277 *drop &= ~ref_from_discarded;
8278 if ((*drop & can_optimize) != 0)
8279 some_unused = 1;
8280 last = 0;
8281 }
8282 else if (*drop)
8283 {
8284 some_unused = 1;
8285 last = ref_from_discarded;
8286 }
8287 else
8288 *drop = last;
8289 }
8290
8291 free (used);
8292
8293 if (some_unused)
8294 {
8295 bfd_byte *contents, *src;
8296 unsigned long off;
8297 Elf_Internal_Sym *sym;
8298 bfd_boolean local_toc_syms = FALSE;
8299
8300 /* Shuffle the toc contents, and at the same time convert the
8301 skip array from booleans into offsets. */
8302 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8303 goto error_ret;
8304
8305 elf_section_data (toc)->this_hdr.contents = contents;
8306
8307 for (src = contents, off = 0, drop = skip;
8308 src < contents + toc->size;
8309 src += 8, ++drop)
8310 {
8311 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8312 off += 8;
8313 else if (off != 0)
8314 {
8315 *drop = off;
8316 memcpy (src - off, src, 8);
8317 }
8318 }
8319 *drop = off;
8320 toc->rawsize = toc->size;
8321 toc->size = src - contents - off;
8322
8323 /* Adjust addends for relocs against the toc section sym,
8324 and optimize any accesses we can. */
8325 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8326 {
8327 if (sec->reloc_count == 0
8328 || elf_discarded_section (sec))
8329 continue;
8330
8331 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8332 info->keep_memory);
8333 if (relstart == NULL)
8334 goto error_ret;
8335
8336 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8337 {
8338 enum elf_ppc64_reloc_type r_type;
8339 unsigned long r_symndx;
8340 asection *sym_sec;
8341 struct elf_link_hash_entry *h;
8342 bfd_vma val;
8343
8344 r_type = ELF64_R_TYPE (rel->r_info);
8345 switch (r_type)
8346 {
8347 default:
8348 continue;
8349
8350 case R_PPC64_TOC16:
8351 case R_PPC64_TOC16_LO:
8352 case R_PPC64_TOC16_HI:
8353 case R_PPC64_TOC16_HA:
8354 case R_PPC64_TOC16_DS:
8355 case R_PPC64_TOC16_LO_DS:
8356 case R_PPC64_ADDR64:
8357 break;
8358 }
8359
8360 r_symndx = ELF64_R_SYM (rel->r_info);
8361 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8362 r_symndx, ibfd))
8363 goto error_ret;
8364
8365 if (sym_sec != toc)
8366 continue;
8367
8368 if (h != NULL)
8369 val = h->root.u.def.value;
8370 else
8371 {
8372 val = sym->st_value;
8373 if (val != 0)
8374 local_toc_syms = TRUE;
8375 }
8376
8377 val += rel->r_addend;
8378
8379 if (val > toc->rawsize)
8380 val = toc->rawsize;
8381 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8382 continue;
8383 else if ((skip[val >> 3] & can_optimize) != 0)
8384 {
8385 Elf_Internal_Rela *tocrel
8386 = toc_relocs + (skip[val >> 3] >> 2);
8387 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8388
8389 switch (r_type)
8390 {
8391 case R_PPC64_TOC16_HA:
8392 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8393 break;
8394
8395 case R_PPC64_TOC16_LO_DS:
8396 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8397 break;
8398
8399 default:
8400 abort ();
8401 }
8402 rel->r_addend = tocrel->r_addend;
8403 elf_section_data (sec)->relocs = relstart;
8404 continue;
8405 }
8406
8407 if (h != NULL || sym->st_value != 0)
8408 continue;
8409
8410 rel->r_addend -= skip[val >> 3];
8411 elf_section_data (sec)->relocs = relstart;
8412 }
8413
8414 if (elf_section_data (sec)->relocs != relstart)
8415 free (relstart);
8416 }
8417
8418 /* We shouldn't have local or global symbols defined in the TOC,
8419 but handle them anyway. */
8420 if (local_syms != NULL)
8421 for (sym = local_syms;
8422 sym < local_syms + symtab_hdr->sh_info;
8423 ++sym)
8424 if (sym->st_value != 0
8425 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8426 {
8427 unsigned long i;
8428
8429 if (sym->st_value > toc->rawsize)
8430 i = toc->rawsize >> 3;
8431 else
8432 i = sym->st_value >> 3;
8433
8434 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8435 {
8436 if (local_toc_syms)
8437 (*_bfd_error_handler)
8438 (_("%s defined on removed toc entry"),
8439 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8440 do
8441 ++i;
8442 while ((skip[i] & (ref_from_discarded | can_optimize)));
8443 sym->st_value = (bfd_vma) i << 3;
8444 }
8445
8446 sym->st_value -= skip[i];
8447 symtab_hdr->contents = (unsigned char *) local_syms;
8448 }
8449
8450 /* Adjust any global syms defined in this toc input section. */
8451 if (toc_inf.global_toc_syms)
8452 {
8453 toc_inf.toc = toc;
8454 toc_inf.skip = skip;
8455 toc_inf.global_toc_syms = FALSE;
8456 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8457 &toc_inf);
8458 }
8459
8460 if (toc->reloc_count != 0)
8461 {
8462 Elf_Internal_Shdr *rel_hdr;
8463 Elf_Internal_Rela *wrel;
8464 bfd_size_type sz;
8465
8466 /* Remove unused toc relocs, and adjust those we keep. */
8467 if (toc_relocs == NULL)
8468 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8469 info->keep_memory);
8470 if (toc_relocs == NULL)
8471 goto error_ret;
8472
8473 wrel = toc_relocs;
8474 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8475 if ((skip[rel->r_offset >> 3]
8476 & (ref_from_discarded | can_optimize)) == 0)
8477 {
8478 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8479 wrel->r_info = rel->r_info;
8480 wrel->r_addend = rel->r_addend;
8481 ++wrel;
8482 }
8483 else if (!dec_dynrel_count (rel->r_info, toc, info,
8484 &local_syms, NULL, NULL))
8485 goto error_ret;
8486
8487 elf_section_data (toc)->relocs = toc_relocs;
8488 toc->reloc_count = wrel - toc_relocs;
8489 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8490 sz = rel_hdr->sh_entsize;
8491 rel_hdr->sh_size = toc->reloc_count * sz;
8492 }
8493 }
8494 else if (toc_relocs != NULL
8495 && elf_section_data (toc)->relocs != toc_relocs)
8496 free (toc_relocs);
8497
8498 if (local_syms != NULL
8499 && symtab_hdr->contents != (unsigned char *) local_syms)
8500 {
8501 if (!info->keep_memory)
8502 free (local_syms);
8503 else
8504 symtab_hdr->contents = (unsigned char *) local_syms;
8505 }
8506 free (skip);
8507 }
8508
8509 return TRUE;
8510 }
8511
8512 /* Return true iff input section I references the TOC using
8513 instructions limited to +/-32k offsets. */
8514
8515 bfd_boolean
8516 ppc64_elf_has_small_toc_reloc (asection *i)
8517 {
8518 return (is_ppc64_elf (i->owner)
8519 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8520 }
8521
8522 /* Allocate space for one GOT entry. */
8523
8524 static void
8525 allocate_got (struct elf_link_hash_entry *h,
8526 struct bfd_link_info *info,
8527 struct got_entry *gent)
8528 {
8529 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8530 bfd_boolean dyn;
8531 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8532 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8533 ? 16 : 8);
8534 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8535 ? 2 : 1) * sizeof (Elf64_External_Rela);
8536 asection *got = ppc64_elf_tdata (gent->owner)->got;
8537
8538 gent->got.offset = got->size;
8539 got->size += entsize;
8540
8541 dyn = htab->elf.dynamic_sections_created;
8542 if ((info->shared
8543 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8544 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8545 || h->root.type != bfd_link_hash_undefweak))
8546 {
8547 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8548 relgot->size += rentsize;
8549 }
8550 else if (h->type == STT_GNU_IFUNC)
8551 {
8552 asection *relgot = htab->reliplt;
8553 relgot->size += rentsize;
8554 htab->got_reli_size += rentsize;
8555 }
8556 }
8557
8558 /* This function merges got entries in the same toc group. */
8559
8560 static void
8561 merge_got_entries (struct got_entry **pent)
8562 {
8563 struct got_entry *ent, *ent2;
8564
8565 for (ent = *pent; ent != NULL; ent = ent->next)
8566 if (!ent->is_indirect)
8567 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8568 if (!ent2->is_indirect
8569 && ent2->addend == ent->addend
8570 && ent2->tls_type == ent->tls_type
8571 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8572 {
8573 ent2->is_indirect = TRUE;
8574 ent2->got.ent = ent;
8575 }
8576 }
8577
8578 /* Allocate space in .plt, .got and associated reloc sections for
8579 dynamic relocs. */
8580
8581 static bfd_boolean
8582 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8583 {
8584 struct bfd_link_info *info;
8585 struct ppc_link_hash_table *htab;
8586 asection *s;
8587 struct ppc_link_hash_entry *eh;
8588 struct elf_dyn_relocs *p;
8589 struct got_entry **pgent, *gent;
8590
8591 if (h->root.type == bfd_link_hash_indirect)
8592 return TRUE;
8593
8594 info = (struct bfd_link_info *) inf;
8595 htab = ppc_hash_table (info);
8596 if (htab == NULL)
8597 return FALSE;
8598
8599 if ((htab->elf.dynamic_sections_created
8600 && h->dynindx != -1
8601 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8602 || h->type == STT_GNU_IFUNC)
8603 {
8604 struct plt_entry *pent;
8605 bfd_boolean doneone = FALSE;
8606 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8607 if (pent->plt.refcount > 0)
8608 {
8609 if (!htab->elf.dynamic_sections_created
8610 || h->dynindx == -1)
8611 {
8612 s = htab->iplt;
8613 pent->plt.offset = s->size;
8614 s->size += PLT_ENTRY_SIZE;
8615 s = htab->reliplt;
8616 }
8617 else
8618 {
8619 /* If this is the first .plt entry, make room for the special
8620 first entry. */
8621 s = htab->plt;
8622 if (s->size == 0)
8623 s->size += PLT_INITIAL_ENTRY_SIZE;
8624
8625 pent->plt.offset = s->size;
8626
8627 /* Make room for this entry. */
8628 s->size += PLT_ENTRY_SIZE;
8629
8630 /* Make room for the .glink code. */
8631 s = htab->glink;
8632 if (s->size == 0)
8633 s->size += GLINK_CALL_STUB_SIZE;
8634 /* We need bigger stubs past index 32767. */
8635 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8636 s->size += 4;
8637 s->size += 2*4;
8638
8639 /* We also need to make an entry in the .rela.plt section. */
8640 s = htab->relplt;
8641 }
8642 s->size += sizeof (Elf64_External_Rela);
8643 doneone = TRUE;
8644 }
8645 else
8646 pent->plt.offset = (bfd_vma) -1;
8647 if (!doneone)
8648 {
8649 h->plt.plist = NULL;
8650 h->needs_plt = 0;
8651 }
8652 }
8653 else
8654 {
8655 h->plt.plist = NULL;
8656 h->needs_plt = 0;
8657 }
8658
8659 eh = (struct ppc_link_hash_entry *) h;
8660 /* Run through the TLS GD got entries first if we're changing them
8661 to TPREL. */
8662 if ((eh->tls_mask & TLS_TPRELGD) != 0)
8663 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8664 if (gent->got.refcount > 0
8665 && (gent->tls_type & TLS_GD) != 0)
8666 {
8667 /* This was a GD entry that has been converted to TPREL. If
8668 there happens to be a TPREL entry we can use that one. */
8669 struct got_entry *ent;
8670 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8671 if (ent->got.refcount > 0
8672 && (ent->tls_type & TLS_TPREL) != 0
8673 && ent->addend == gent->addend
8674 && ent->owner == gent->owner)
8675 {
8676 gent->got.refcount = 0;
8677 break;
8678 }
8679
8680 /* If not, then we'll be using our own TPREL entry. */
8681 if (gent->got.refcount != 0)
8682 gent->tls_type = TLS_TLS | TLS_TPREL;
8683 }
8684
8685 /* Remove any list entry that won't generate a word in the GOT before
8686 we call merge_got_entries. Otherwise we risk merging to empty
8687 entries. */
8688 pgent = &h->got.glist;
8689 while ((gent = *pgent) != NULL)
8690 if (gent->got.refcount > 0)
8691 {
8692 if ((gent->tls_type & TLS_LD) != 0
8693 && !h->def_dynamic)
8694 {
8695 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8696 *pgent = gent->next;
8697 }
8698 else
8699 pgent = &gent->next;
8700 }
8701 else
8702 *pgent = gent->next;
8703
8704 if (!htab->do_multi_toc)
8705 merge_got_entries (&h->got.glist);
8706
8707 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8708 if (!gent->is_indirect)
8709 {
8710 /* Make sure this symbol is output as a dynamic symbol.
8711 Undefined weak syms won't yet be marked as dynamic,
8712 nor will all TLS symbols. */
8713 if (h->dynindx == -1
8714 && !h->forced_local
8715 && h->type != STT_GNU_IFUNC
8716 && htab->elf.dynamic_sections_created)
8717 {
8718 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8719 return FALSE;
8720 }
8721
8722 if (!is_ppc64_elf (gent->owner))
8723 abort ();
8724
8725 allocate_got (h, info, gent);
8726 }
8727
8728 if (eh->dyn_relocs == NULL
8729 || (!htab->elf.dynamic_sections_created
8730 && h->type != STT_GNU_IFUNC))
8731 return TRUE;
8732
8733 /* In the shared -Bsymbolic case, discard space allocated for
8734 dynamic pc-relative relocs against symbols which turn out to be
8735 defined in regular objects. For the normal shared case, discard
8736 space for relocs that have become local due to symbol visibility
8737 changes. */
8738
8739 if (info->shared)
8740 {
8741 /* Relocs that use pc_count are those that appear on a call insn,
8742 or certain REL relocs (see must_be_dyn_reloc) that can be
8743 generated via assembly. We want calls to protected symbols to
8744 resolve directly to the function rather than going via the plt.
8745 If people want function pointer comparisons to work as expected
8746 then they should avoid writing weird assembly. */
8747 if (SYMBOL_CALLS_LOCAL (info, h))
8748 {
8749 struct elf_dyn_relocs **pp;
8750
8751 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
8752 {
8753 p->count -= p->pc_count;
8754 p->pc_count = 0;
8755 if (p->count == 0)
8756 *pp = p->next;
8757 else
8758 pp = &p->next;
8759 }
8760 }
8761
8762 /* Also discard relocs on undefined weak syms with non-default
8763 visibility. */
8764 if (eh->dyn_relocs != NULL
8765 && h->root.type == bfd_link_hash_undefweak)
8766 {
8767 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8768 eh->dyn_relocs = NULL;
8769
8770 /* Make sure this symbol is output as a dynamic symbol.
8771 Undefined weak syms won't yet be marked as dynamic. */
8772 else if (h->dynindx == -1
8773 && !h->forced_local)
8774 {
8775 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8776 return FALSE;
8777 }
8778 }
8779 }
8780 else if (h->type == STT_GNU_IFUNC)
8781 {
8782 if (!h->non_got_ref)
8783 eh->dyn_relocs = NULL;
8784 }
8785 else if (ELIMINATE_COPY_RELOCS)
8786 {
8787 /* For the non-shared case, discard space for relocs against
8788 symbols which turn out to need copy relocs or are not
8789 dynamic. */
8790
8791 if (!h->non_got_ref
8792 && !h->def_regular)
8793 {
8794 /* Make sure this symbol is output as a dynamic symbol.
8795 Undefined weak syms won't yet be marked as dynamic. */
8796 if (h->dynindx == -1
8797 && !h->forced_local)
8798 {
8799 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8800 return FALSE;
8801 }
8802
8803 /* If that succeeded, we know we'll be keeping all the
8804 relocs. */
8805 if (h->dynindx != -1)
8806 goto keep;
8807 }
8808
8809 eh->dyn_relocs = NULL;
8810
8811 keep: ;
8812 }
8813
8814 /* Finally, allocate space. */
8815 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8816 {
8817 asection *sreloc = elf_section_data (p->sec)->sreloc;
8818 if (!htab->elf.dynamic_sections_created)
8819 sreloc = htab->reliplt;
8820 sreloc->size += p->count * sizeof (Elf64_External_Rela);
8821 }
8822
8823 return TRUE;
8824 }
8825
8826 /* Find any dynamic relocs that apply to read-only sections. */
8827
8828 static bfd_boolean
8829 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
8830 {
8831 struct ppc_link_hash_entry *eh;
8832 struct elf_dyn_relocs *p;
8833
8834 eh = (struct ppc_link_hash_entry *) h;
8835 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8836 {
8837 asection *s = p->sec->output_section;
8838
8839 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8840 {
8841 struct bfd_link_info *info = inf;
8842
8843 info->flags |= DF_TEXTREL;
8844
8845 /* Not an error, just cut short the traversal. */
8846 return FALSE;
8847 }
8848 }
8849 return TRUE;
8850 }
8851
8852 /* Set the sizes of the dynamic sections. */
8853
8854 static bfd_boolean
8855 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8856 struct bfd_link_info *info)
8857 {
8858 struct ppc_link_hash_table *htab;
8859 bfd *dynobj;
8860 asection *s;
8861 bfd_boolean relocs;
8862 bfd *ibfd;
8863 struct got_entry *first_tlsld;
8864
8865 htab = ppc_hash_table (info);
8866 if (htab == NULL)
8867 return FALSE;
8868
8869 dynobj = htab->elf.dynobj;
8870 if (dynobj == NULL)
8871 abort ();
8872
8873 if (htab->elf.dynamic_sections_created)
8874 {
8875 /* Set the contents of the .interp section to the interpreter. */
8876 if (info->executable)
8877 {
8878 s = bfd_get_section_by_name (dynobj, ".interp");
8879 if (s == NULL)
8880 abort ();
8881 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
8882 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8883 }
8884 }
8885
8886 /* Set up .got offsets for local syms, and space for local dynamic
8887 relocs. */
8888 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8889 {
8890 struct got_entry **lgot_ents;
8891 struct got_entry **end_lgot_ents;
8892 struct plt_entry **local_plt;
8893 struct plt_entry **end_local_plt;
8894 unsigned char *lgot_masks;
8895 bfd_size_type locsymcount;
8896 Elf_Internal_Shdr *symtab_hdr;
8897 asection *srel;
8898
8899 if (!is_ppc64_elf (ibfd))
8900 continue;
8901
8902 for (s = ibfd->sections; s != NULL; s = s->next)
8903 {
8904 struct elf_dyn_relocs *p;
8905
8906 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
8907 {
8908 if (!bfd_is_abs_section (p->sec)
8909 && bfd_is_abs_section (p->sec->output_section))
8910 {
8911 /* Input section has been discarded, either because
8912 it is a copy of a linkonce section or due to
8913 linker script /DISCARD/, so we'll be discarding
8914 the relocs too. */
8915 }
8916 else if (p->count != 0)
8917 {
8918 srel = elf_section_data (p->sec)->sreloc;
8919 if (!htab->elf.dynamic_sections_created)
8920 srel = htab->reliplt;
8921 srel->size += p->count * sizeof (Elf64_External_Rela);
8922 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8923 info->flags |= DF_TEXTREL;
8924 }
8925 }
8926 }
8927
8928 lgot_ents = elf_local_got_ents (ibfd);
8929 if (!lgot_ents)
8930 continue;
8931
8932 symtab_hdr = &elf_symtab_hdr (ibfd);
8933 locsymcount = symtab_hdr->sh_info;
8934 end_lgot_ents = lgot_ents + locsymcount;
8935 local_plt = (struct plt_entry **) end_lgot_ents;
8936 end_local_plt = local_plt + locsymcount;
8937 lgot_masks = (unsigned char *) end_local_plt;
8938 s = ppc64_elf_tdata (ibfd)->got;
8939 srel = ppc64_elf_tdata (ibfd)->relgot;
8940 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
8941 {
8942 struct got_entry **pent, *ent;
8943
8944 pent = lgot_ents;
8945 while ((ent = *pent) != NULL)
8946 if (ent->got.refcount > 0)
8947 {
8948 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
8949 {
8950 ppc64_tlsld_got (ibfd)->got.refcount += 1;
8951 *pent = ent->next;
8952 }
8953 else
8954 {
8955 unsigned int num = 1;
8956 ent->got.offset = s->size;
8957 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
8958 num = 2;
8959 s->size += num * 8;
8960 if (info->shared)
8961 srel->size += num * sizeof (Elf64_External_Rela);
8962 else if ((*lgot_masks & PLT_IFUNC) != 0)
8963 {
8964 htab->reliplt->size
8965 += num * sizeof (Elf64_External_Rela);
8966 htab->got_reli_size
8967 += num * sizeof (Elf64_External_Rela);
8968 }
8969 pent = &ent->next;
8970 }
8971 }
8972 else
8973 *pent = ent->next;
8974 }
8975
8976 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8977 for (; local_plt < end_local_plt; ++local_plt)
8978 {
8979 struct plt_entry *ent;
8980
8981 for (ent = *local_plt; ent != NULL; ent = ent->next)
8982 if (ent->plt.refcount > 0)
8983 {
8984 s = htab->iplt;
8985 ent->plt.offset = s->size;
8986 s->size += PLT_ENTRY_SIZE;
8987
8988 htab->reliplt->size += sizeof (Elf64_External_Rela);
8989 }
8990 else
8991 ent->plt.offset = (bfd_vma) -1;
8992 }
8993 }
8994
8995 /* Allocate global sym .plt and .got entries, and space for global
8996 sym dynamic relocs. */
8997 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
8998
8999 first_tlsld = NULL;
9000 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9001 {
9002 struct got_entry *ent;
9003
9004 if (!is_ppc64_elf (ibfd))
9005 continue;
9006
9007 ent = ppc64_tlsld_got (ibfd);
9008 if (ent->got.refcount > 0)
9009 {
9010 if (!htab->do_multi_toc && first_tlsld != NULL)
9011 {
9012 ent->is_indirect = TRUE;
9013 ent->got.ent = first_tlsld;
9014 }
9015 else
9016 {
9017 if (first_tlsld == NULL)
9018 first_tlsld = ent;
9019 s = ppc64_elf_tdata (ibfd)->got;
9020 ent->got.offset = s->size;
9021 ent->owner = ibfd;
9022 s->size += 16;
9023 if (info->shared)
9024 {
9025 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9026 srel->size += sizeof (Elf64_External_Rela);
9027 }
9028 }
9029 }
9030 else
9031 ent->got.offset = (bfd_vma) -1;
9032 }
9033
9034 /* We now have determined the sizes of the various dynamic sections.
9035 Allocate memory for them. */
9036 relocs = FALSE;
9037 for (s = dynobj->sections; s != NULL; s = s->next)
9038 {
9039 if ((s->flags & SEC_LINKER_CREATED) == 0)
9040 continue;
9041
9042 if (s == htab->brlt || s == htab->relbrlt)
9043 /* These haven't been allocated yet; don't strip. */
9044 continue;
9045 else if (s == htab->got
9046 || s == htab->plt
9047 || s == htab->iplt
9048 || s == htab->glink
9049 || s == htab->dynbss)
9050 {
9051 /* Strip this section if we don't need it; see the
9052 comment below. */
9053 }
9054 else if (CONST_STRNEQ (s->name, ".rela"))
9055 {
9056 if (s->size != 0)
9057 {
9058 if (s != htab->relplt)
9059 relocs = TRUE;
9060
9061 /* We use the reloc_count field as a counter if we need
9062 to copy relocs into the output file. */
9063 s->reloc_count = 0;
9064 }
9065 }
9066 else
9067 {
9068 /* It's not one of our sections, so don't allocate space. */
9069 continue;
9070 }
9071
9072 if (s->size == 0)
9073 {
9074 /* If we don't need this section, strip it from the
9075 output file. This is mostly to handle .rela.bss and
9076 .rela.plt. We must create both sections in
9077 create_dynamic_sections, because they must be created
9078 before the linker maps input sections to output
9079 sections. The linker does that before
9080 adjust_dynamic_symbol is called, and it is that
9081 function which decides whether anything needs to go
9082 into these sections. */
9083 s->flags |= SEC_EXCLUDE;
9084 continue;
9085 }
9086
9087 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9088 continue;
9089
9090 /* Allocate memory for the section contents. We use bfd_zalloc
9091 here in case unused entries are not reclaimed before the
9092 section's contents are written out. This should not happen,
9093 but this way if it does we get a R_PPC64_NONE reloc in .rela
9094 sections instead of garbage.
9095 We also rely on the section contents being zero when writing
9096 the GOT. */
9097 s->contents = bfd_zalloc (dynobj, s->size);
9098 if (s->contents == NULL)
9099 return FALSE;
9100 }
9101
9102 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9103 {
9104 if (!is_ppc64_elf (ibfd))
9105 continue;
9106
9107 s = ppc64_elf_tdata (ibfd)->got;
9108 if (s != NULL && s != htab->got)
9109 {
9110 if (s->size == 0)
9111 s->flags |= SEC_EXCLUDE;
9112 else
9113 {
9114 s->contents = bfd_zalloc (ibfd, s->size);
9115 if (s->contents == NULL)
9116 return FALSE;
9117 }
9118 }
9119 s = ppc64_elf_tdata (ibfd)->relgot;
9120 if (s != NULL)
9121 {
9122 if (s->size == 0)
9123 s->flags |= SEC_EXCLUDE;
9124 else
9125 {
9126 s->contents = bfd_zalloc (ibfd, s->size);
9127 if (s->contents == NULL)
9128 return FALSE;
9129 relocs = TRUE;
9130 s->reloc_count = 0;
9131 }
9132 }
9133 }
9134
9135 if (htab->elf.dynamic_sections_created)
9136 {
9137 /* Add some entries to the .dynamic section. We fill in the
9138 values later, in ppc64_elf_finish_dynamic_sections, but we
9139 must add the entries now so that we get the correct size for
9140 the .dynamic section. The DT_DEBUG entry is filled in by the
9141 dynamic linker and used by the debugger. */
9142 #define add_dynamic_entry(TAG, VAL) \
9143 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9144
9145 if (info->executable)
9146 {
9147 if (!add_dynamic_entry (DT_DEBUG, 0))
9148 return FALSE;
9149 }
9150
9151 if (htab->plt != NULL && htab->plt->size != 0)
9152 {
9153 if (!add_dynamic_entry (DT_PLTGOT, 0)
9154 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9155 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9156 || !add_dynamic_entry (DT_JMPREL, 0)
9157 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9158 return FALSE;
9159 }
9160
9161 if (NO_OPD_RELOCS)
9162 {
9163 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9164 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9165 return FALSE;
9166 }
9167
9168 if (!htab->no_tls_get_addr_opt
9169 && htab->tls_get_addr_fd != NULL
9170 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9171 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9172 return FALSE;
9173
9174 if (relocs)
9175 {
9176 if (!add_dynamic_entry (DT_RELA, 0)
9177 || !add_dynamic_entry (DT_RELASZ, 0)
9178 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9179 return FALSE;
9180
9181 /* If any dynamic relocs apply to a read-only section,
9182 then we need a DT_TEXTREL entry. */
9183 if ((info->flags & DF_TEXTREL) == 0)
9184 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9185
9186 if ((info->flags & DF_TEXTREL) != 0)
9187 {
9188 if (!add_dynamic_entry (DT_TEXTREL, 0))
9189 return FALSE;
9190 }
9191 }
9192 }
9193 #undef add_dynamic_entry
9194
9195 return TRUE;
9196 }
9197
9198 /* Determine the type of stub needed, if any, for a call. */
9199
9200 static inline enum ppc_stub_type
9201 ppc_type_of_stub (asection *input_sec,
9202 const Elf_Internal_Rela *rel,
9203 struct ppc_link_hash_entry **hash,
9204 struct plt_entry **plt_ent,
9205 bfd_vma destination)
9206 {
9207 struct ppc_link_hash_entry *h = *hash;
9208 bfd_vma location;
9209 bfd_vma branch_offset;
9210 bfd_vma max_branch_offset;
9211 enum elf_ppc64_reloc_type r_type;
9212
9213 if (h != NULL)
9214 {
9215 struct plt_entry *ent;
9216 struct ppc_link_hash_entry *fdh = h;
9217 if (h->oh != NULL
9218 && h->oh->is_func_descriptor)
9219 {
9220 fdh = ppc_follow_link (h->oh);
9221 *hash = fdh;
9222 }
9223
9224 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9225 if (ent->addend == rel->r_addend
9226 && ent->plt.offset != (bfd_vma) -1)
9227 {
9228 *plt_ent = ent;
9229 return ppc_stub_plt_call;
9230 }
9231
9232 /* Here, we know we don't have a plt entry. If we don't have a
9233 either a defined function descriptor or a defined entry symbol
9234 in a regular object file, then it is pointless trying to make
9235 any other type of stub. */
9236 if (!is_static_defined (&fdh->elf)
9237 && !is_static_defined (&h->elf))
9238 return ppc_stub_none;
9239 }
9240 else if (elf_local_got_ents (input_sec->owner) != NULL)
9241 {
9242 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9243 struct plt_entry **local_plt = (struct plt_entry **)
9244 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9245 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9246
9247 if (local_plt[r_symndx] != NULL)
9248 {
9249 struct plt_entry *ent;
9250
9251 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9252 if (ent->addend == rel->r_addend
9253 && ent->plt.offset != (bfd_vma) -1)
9254 {
9255 *plt_ent = ent;
9256 return ppc_stub_plt_call;
9257 }
9258 }
9259 }
9260
9261 /* Determine where the call point is. */
9262 location = (input_sec->output_offset
9263 + input_sec->output_section->vma
9264 + rel->r_offset);
9265
9266 branch_offset = destination - location;
9267 r_type = ELF64_R_TYPE (rel->r_info);
9268
9269 /* Determine if a long branch stub is needed. */
9270 max_branch_offset = 1 << 25;
9271 if (r_type != R_PPC64_REL24)
9272 max_branch_offset = 1 << 15;
9273
9274 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9275 /* We need a stub. Figure out whether a long_branch or plt_branch
9276 is needed later. */
9277 return ppc_stub_long_branch;
9278
9279 return ppc_stub_none;
9280 }
9281
9282 /* Build a .plt call stub. */
9283
9284 static inline bfd_byte *
9285 build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
9286 {
9287 #define PPC_LO(v) ((v) & 0xffff)
9288 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9289 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9290
9291 if (PPC_HA (offset) != 0)
9292 {
9293 if (r != NULL)
9294 {
9295 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9296 r[1].r_offset = r[0].r_offset + 8;
9297 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9298 r[1].r_addend = r[0].r_addend;
9299 if (PPC_HA (offset + 16) != PPC_HA (offset))
9300 {
9301 r[2].r_offset = r[1].r_offset + 4;
9302 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9303 r[2].r_addend = r[0].r_addend;
9304 }
9305 else
9306 {
9307 r[2].r_offset = r[1].r_offset + 8;
9308 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9309 r[2].r_addend = r[0].r_addend + 8;
9310 r[3].r_offset = r[2].r_offset + 4;
9311 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9312 r[3].r_addend = r[0].r_addend + 16;
9313 }
9314 }
9315 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9316 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9317 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9318 if (PPC_HA (offset + 16) != PPC_HA (offset))
9319 {
9320 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9321 offset = 0;
9322 }
9323 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9324 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9325 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9326 bfd_put_32 (obfd, BCTR, p), p += 4;
9327 }
9328 else
9329 {
9330 if (r != NULL)
9331 {
9332 r[0].r_offset += 4;
9333 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9334 if (PPC_HA (offset + 16) != PPC_HA (offset))
9335 {
9336 r[1].r_offset = r[0].r_offset + 4;
9337 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9338 r[1].r_addend = r[0].r_addend;
9339 }
9340 else
9341 {
9342 r[1].r_offset = r[0].r_offset + 8;
9343 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9344 r[1].r_addend = r[0].r_addend + 16;
9345 r[2].r_offset = r[1].r_offset + 4;
9346 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9347 r[2].r_addend = r[0].r_addend + 8;
9348 }
9349 }
9350 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9351 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9352 if (PPC_HA (offset + 16) != PPC_HA (offset))
9353 {
9354 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9355 offset = 0;
9356 }
9357 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9358 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9359 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9360 bfd_put_32 (obfd, BCTR, p), p += 4;
9361 }
9362 return p;
9363 }
9364
9365 /* Build a special .plt call stub for __tls_get_addr. */
9366
9367 #define LD_R11_0R3 0xe9630000
9368 #define LD_R12_0R3 0xe9830000
9369 #define MR_R0_R3 0x7c601b78
9370 #define CMPDI_R11_0 0x2c2b0000
9371 #define ADD_R3_R12_R13 0x7c6c6a14
9372 #define BEQLR 0x4d820020
9373 #define MR_R3_R0 0x7c030378
9374 #define MFLR_R11 0x7d6802a6
9375 #define STD_R11_0R1 0xf9610000
9376 #define BCTRL 0x4e800421
9377 #define LD_R11_0R1 0xe9610000
9378 #define LD_R2_0R1 0xe8410000
9379 #define MTLR_R11 0x7d6803a6
9380
9381 static inline bfd_byte *
9382 build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9383 Elf_Internal_Rela *r)
9384 {
9385 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9386 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9387 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9388 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9389 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9390 bfd_put_32 (obfd, BEQLR, p), p += 4;
9391 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9392 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9393 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9394
9395 if (r != NULL)
9396 r[0].r_offset += 9 * 4;
9397 p = build_plt_stub (obfd, p, offset, r);
9398 bfd_put_32 (obfd, BCTRL, p - 4);
9399
9400 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9401 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9402 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9403 bfd_put_32 (obfd, BLR, p), p += 4;
9404
9405 return p;
9406 }
9407
9408 static Elf_Internal_Rela *
9409 get_relocs (asection *sec, int count)
9410 {
9411 Elf_Internal_Rela *relocs;
9412 struct bfd_elf_section_data *elfsec_data;
9413
9414 elfsec_data = elf_section_data (sec);
9415 relocs = elfsec_data->relocs;
9416 if (relocs == NULL)
9417 {
9418 bfd_size_type relsize;
9419 relsize = sec->reloc_count * sizeof (*relocs);
9420 relocs = bfd_alloc (sec->owner, relsize);
9421 if (relocs == NULL)
9422 return NULL;
9423 elfsec_data->relocs = relocs;
9424 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9425 sizeof (Elf_Internal_Shdr));
9426 if (elfsec_data->rela.hdr == NULL)
9427 return NULL;
9428 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9429 * sizeof (Elf64_External_Rela));
9430 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
9431 sec->reloc_count = 0;
9432 }
9433 relocs += sec->reloc_count;
9434 sec->reloc_count += count;
9435 return relocs;
9436 }
9437
9438 static bfd_vma
9439 get_r2off (struct bfd_link_info *info,
9440 struct ppc_stub_hash_entry *stub_entry)
9441 {
9442 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9443 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9444
9445 if (r2off == 0)
9446 {
9447 /* Support linking -R objects. Get the toc pointer from the
9448 opd entry. */
9449 char buf[8];
9450 asection *opd = stub_entry->h->elf.root.u.def.section;
9451 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9452
9453 if (strcmp (opd->name, ".opd") != 0
9454 || opd->reloc_count != 0)
9455 {
9456 info->callbacks->einfo (_("cannot find opd entry toc for %s\n"),
9457 stub_entry->h->elf.root.root.string);
9458 bfd_set_error (bfd_error_bad_value);
9459 return 0;
9460 }
9461 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9462 return 0;
9463 r2off = bfd_get_64 (opd->owner, buf);
9464 r2off -= elf_gp (info->output_bfd);
9465 }
9466 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9467 return r2off;
9468 }
9469
9470 static bfd_boolean
9471 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9472 {
9473 struct ppc_stub_hash_entry *stub_entry;
9474 struct ppc_branch_hash_entry *br_entry;
9475 struct bfd_link_info *info;
9476 struct ppc_link_hash_table *htab;
9477 bfd_byte *loc;
9478 bfd_byte *p;
9479 bfd_vma dest, off;
9480 int size;
9481 Elf_Internal_Rela *r;
9482 asection *plt;
9483
9484 /* Massage our args to the form they really have. */
9485 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9486 info = in_arg;
9487
9488 htab = ppc_hash_table (info);
9489 if (htab == NULL)
9490 return FALSE;
9491
9492 /* Make a note of the offset within the stubs for this entry. */
9493 stub_entry->stub_offset = stub_entry->stub_sec->size;
9494 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
9495
9496 htab->stub_count[stub_entry->stub_type - 1] += 1;
9497 switch (stub_entry->stub_type)
9498 {
9499 case ppc_stub_long_branch:
9500 case ppc_stub_long_branch_r2off:
9501 /* Branches are relative. This is where we are going to. */
9502 off = dest = (stub_entry->target_value
9503 + stub_entry->target_section->output_offset
9504 + stub_entry->target_section->output_section->vma);
9505
9506 /* And this is where we are coming from. */
9507 off -= (stub_entry->stub_offset
9508 + stub_entry->stub_sec->output_offset
9509 + stub_entry->stub_sec->output_section->vma);
9510
9511 size = 4;
9512 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9513 {
9514 bfd_vma r2off = get_r2off (info, stub_entry);
9515
9516 if (r2off == 0)
9517 {
9518 htab->stub_error = TRUE;
9519 return FALSE;
9520 }
9521 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9522 loc += 4;
9523 size = 12;
9524 if (PPC_HA (r2off) != 0)
9525 {
9526 size = 16;
9527 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9528 loc += 4;
9529 }
9530 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9531 loc += 4;
9532 off -= size - 4;
9533 }
9534 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
9535
9536 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9537 {
9538 info->callbacks->einfo (_("long branch stub `%s' offset overflow\n"),
9539 stub_entry->root.string);
9540 htab->stub_error = TRUE;
9541 return FALSE;
9542 }
9543
9544 if (info->emitrelocations)
9545 {
9546 r = get_relocs (stub_entry->stub_sec, 1);
9547 if (r == NULL)
9548 return FALSE;
9549 r->r_offset = loc - stub_entry->stub_sec->contents;
9550 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9551 r->r_addend = dest;
9552 if (stub_entry->h != NULL)
9553 {
9554 struct elf_link_hash_entry **hashes;
9555 unsigned long symndx;
9556 struct ppc_link_hash_entry *h;
9557
9558 hashes = elf_sym_hashes (htab->stub_bfd);
9559 if (hashes == NULL)
9560 {
9561 bfd_size_type hsize;
9562
9563 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9564 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9565 if (hashes == NULL)
9566 return FALSE;
9567 elf_sym_hashes (htab->stub_bfd) = hashes;
9568 htab->stub_globals = 1;
9569 }
9570 symndx = htab->stub_globals++;
9571 h = stub_entry->h;
9572 hashes[symndx] = &h->elf;
9573 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9574 if (h->oh != NULL && h->oh->is_func)
9575 h = ppc_follow_link (h->oh);
9576 if (h->elf.root.u.def.section != stub_entry->target_section)
9577 /* H is an opd symbol. The addend must be zero. */
9578 r->r_addend = 0;
9579 else
9580 {
9581 off = (h->elf.root.u.def.value
9582 + h->elf.root.u.def.section->output_offset
9583 + h->elf.root.u.def.section->output_section->vma);
9584 r->r_addend -= off;
9585 }
9586 }
9587 }
9588 break;
9589
9590 case ppc_stub_plt_branch:
9591 case ppc_stub_plt_branch_r2off:
9592 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9593 stub_entry->root.string + 9,
9594 FALSE, FALSE);
9595 if (br_entry == NULL)
9596 {
9597 info->callbacks->einfo (_("can't find branch stub `%s'\n"),
9598 stub_entry->root.string);
9599 htab->stub_error = TRUE;
9600 return FALSE;
9601 }
9602
9603 dest = (stub_entry->target_value
9604 + stub_entry->target_section->output_offset
9605 + stub_entry->target_section->output_section->vma);
9606
9607 bfd_put_64 (htab->brlt->owner, dest,
9608 htab->brlt->contents + br_entry->offset);
9609
9610 if (br_entry->iter == htab->stub_iteration)
9611 {
9612 br_entry->iter = 0;
9613
9614 if (htab->relbrlt != NULL)
9615 {
9616 /* Create a reloc for the branch lookup table entry. */
9617 Elf_Internal_Rela rela;
9618 bfd_byte *rl;
9619
9620 rela.r_offset = (br_entry->offset
9621 + htab->brlt->output_offset
9622 + htab->brlt->output_section->vma);
9623 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9624 rela.r_addend = dest;
9625
9626 rl = htab->relbrlt->contents;
9627 rl += (htab->relbrlt->reloc_count++
9628 * sizeof (Elf64_External_Rela));
9629 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9630 }
9631 else if (info->emitrelocations)
9632 {
9633 r = get_relocs (htab->brlt, 1);
9634 if (r == NULL)
9635 return FALSE;
9636 /* brlt, being SEC_LINKER_CREATED does not go through the
9637 normal reloc processing. Symbols and offsets are not
9638 translated from input file to output file form, so
9639 set up the offset per the output file. */
9640 r->r_offset = (br_entry->offset
9641 + htab->brlt->output_offset
9642 + htab->brlt->output_section->vma);
9643 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
9644 r->r_addend = dest;
9645 }
9646 }
9647
9648 dest = (br_entry->offset
9649 + htab->brlt->output_offset
9650 + htab->brlt->output_section->vma);
9651
9652 off = (dest
9653 - elf_gp (htab->brlt->output_section->owner)
9654 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9655
9656 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9657 {
9658 info->callbacks->einfo
9659 (_("linkage table error against `%s'\n"),
9660 stub_entry->root.string);
9661 bfd_set_error (bfd_error_bad_value);
9662 htab->stub_error = TRUE;
9663 return FALSE;
9664 }
9665
9666 if (info->emitrelocations)
9667 {
9668 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9669 if (r == NULL)
9670 return FALSE;
9671 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9672 if (bfd_big_endian (info->output_bfd))
9673 r[0].r_offset += 2;
9674 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9675 r[0].r_offset += 4;
9676 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9677 r[0].r_addend = dest;
9678 if (PPC_HA (off) != 0)
9679 {
9680 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9681 r[1].r_offset = r[0].r_offset + 4;
9682 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9683 r[1].r_addend = r[0].r_addend;
9684 }
9685 }
9686
9687 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9688 {
9689 if (PPC_HA (off) != 0)
9690 {
9691 size = 16;
9692 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9693 loc += 4;
9694 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9695 }
9696 else
9697 {
9698 size = 12;
9699 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9700 }
9701 }
9702 else
9703 {
9704 bfd_vma r2off = get_r2off (info, stub_entry);
9705
9706 if (r2off == 0)
9707 {
9708 htab->stub_error = TRUE;
9709 return FALSE;
9710 }
9711
9712 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
9713 loc += 4;
9714 size = 20;
9715 if (PPC_HA (off) != 0)
9716 {
9717 size += 4;
9718 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
9719 loc += 4;
9720 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
9721 loc += 4;
9722 }
9723 else
9724 {
9725 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
9726 loc += 4;
9727 }
9728
9729 if (PPC_HA (r2off) != 0)
9730 {
9731 size += 4;
9732 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9733 loc += 4;
9734 }
9735 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
9736 }
9737 loc += 4;
9738 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
9739 loc += 4;
9740 bfd_put_32 (htab->stub_bfd, BCTR, loc);
9741 break;
9742
9743 case ppc_stub_plt_call:
9744 if (stub_entry->h != NULL
9745 && stub_entry->h->is_func_descriptor
9746 && stub_entry->h->oh != NULL)
9747 {
9748 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9749
9750 /* If the old-ABI "dot-symbol" is undefined make it weak so
9751 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9752 FIXME: We used to define the symbol on one of the call
9753 stubs instead, which is why we test symbol section id
9754 against htab->top_id in various places. Likely all
9755 these checks could now disappear. */
9756 if (fh->elf.root.type == bfd_link_hash_undefined)
9757 fh->elf.root.type = bfd_link_hash_undefweak;
9758 /* Stop undo_symbol_twiddle changing it back to undefined. */
9759 fh->was_undefined = 0;
9760 }
9761
9762 /* Now build the stub. */
9763 dest = stub_entry->plt_ent->plt.offset & ~1;
9764 if (dest >= (bfd_vma) -2)
9765 abort ();
9766
9767 plt = htab->plt;
9768 if (!htab->elf.dynamic_sections_created
9769 || stub_entry->h == NULL
9770 || stub_entry->h->elf.dynindx == -1)
9771 plt = htab->iplt;
9772
9773 dest += plt->output_offset + plt->output_section->vma;
9774
9775 if (stub_entry->h == NULL
9776 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9777 {
9778 Elf_Internal_Rela rela;
9779 bfd_byte *rl;
9780
9781 rela.r_offset = dest;
9782 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
9783 rela.r_addend = (stub_entry->target_value
9784 + stub_entry->target_section->output_offset
9785 + stub_entry->target_section->output_section->vma);
9786
9787 rl = (htab->reliplt->contents
9788 + (htab->reliplt->reloc_count++
9789 * sizeof (Elf64_External_Rela)));
9790 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
9791 stub_entry->plt_ent->plt.offset |= 1;
9792 }
9793
9794 off = (dest
9795 - elf_gp (plt->output_section->owner)
9796 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9797
9798 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
9799 {
9800 info->callbacks->einfo
9801 (_("linkage table error against `%s'\n"),
9802 stub_entry->h != NULL
9803 ? stub_entry->h->elf.root.root.string
9804 : "<local sym>");
9805 bfd_set_error (bfd_error_bad_value);
9806 htab->stub_error = TRUE;
9807 return FALSE;
9808 }
9809
9810 r = NULL;
9811 if (info->emitrelocations)
9812 {
9813 r = get_relocs (stub_entry->stub_sec,
9814 (2 + (PPC_HA (off) != 0)
9815 + (PPC_HA (off + 16) == PPC_HA (off))));
9816 if (r == NULL)
9817 return FALSE;
9818 r[0].r_offset = loc - stub_entry->stub_sec->contents;
9819 if (bfd_big_endian (info->output_bfd))
9820 r[0].r_offset += 2;
9821 r[0].r_addend = dest;
9822 }
9823 if (stub_entry->h != NULL
9824 && (stub_entry->h == htab->tls_get_addr_fd
9825 || stub_entry->h == htab->tls_get_addr)
9826 && !htab->no_tls_get_addr_opt)
9827 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9828 else
9829 p = build_plt_stub (htab->stub_bfd, loc, off, r);
9830 size = p - loc;
9831 break;
9832
9833 default:
9834 BFD_FAIL ();
9835 return FALSE;
9836 }
9837
9838 stub_entry->stub_sec->size += size;
9839
9840 if (htab->emit_stub_syms)
9841 {
9842 struct elf_link_hash_entry *h;
9843 size_t len1, len2;
9844 char *name;
9845 const char *const stub_str[] = { "long_branch",
9846 "long_branch_r2off",
9847 "plt_branch",
9848 "plt_branch_r2off",
9849 "plt_call" };
9850
9851 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9852 len2 = strlen (stub_entry->root.string);
9853 name = bfd_malloc (len1 + len2 + 2);
9854 if (name == NULL)
9855 return FALSE;
9856 memcpy (name, stub_entry->root.string, 9);
9857 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9858 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9859 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
9860 if (h == NULL)
9861 return FALSE;
9862 if (h->root.type == bfd_link_hash_new)
9863 {
9864 h->root.type = bfd_link_hash_defined;
9865 h->root.u.def.section = stub_entry->stub_sec;
9866 h->root.u.def.value = stub_entry->stub_offset;
9867 h->ref_regular = 1;
9868 h->def_regular = 1;
9869 h->ref_regular_nonweak = 1;
9870 h->forced_local = 1;
9871 h->non_elf = 0;
9872 }
9873 }
9874
9875 return TRUE;
9876 }
9877
9878 /* As above, but don't actually build the stub. Just bump offset so
9879 we know stub section sizes, and select plt_branch stubs where
9880 long_branch stubs won't do. */
9881
9882 static bfd_boolean
9883 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
9884 {
9885 struct ppc_stub_hash_entry *stub_entry;
9886 struct bfd_link_info *info;
9887 struct ppc_link_hash_table *htab;
9888 bfd_vma off;
9889 int size;
9890
9891 /* Massage our args to the form they really have. */
9892 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
9893 info = in_arg;
9894
9895 htab = ppc_hash_table (info);
9896 if (htab == NULL)
9897 return FALSE;
9898
9899 if (stub_entry->stub_type == ppc_stub_plt_call)
9900 {
9901 asection *plt;
9902 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
9903 if (off >= (bfd_vma) -2)
9904 abort ();
9905 plt = htab->plt;
9906 if (!htab->elf.dynamic_sections_created
9907 || stub_entry->h == NULL
9908 || stub_entry->h->elf.dynindx == -1)
9909 plt = htab->iplt;
9910 off += (plt->output_offset
9911 + plt->output_section->vma
9912 - elf_gp (plt->output_section->owner)
9913 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9914
9915 size = PLT_CALL_STUB_SIZE;
9916 if (PPC_HA (off) == 0)
9917 size -= 4;
9918 if (PPC_HA (off + 16) != PPC_HA (off))
9919 size += 4;
9920 if (stub_entry->h != NULL
9921 && (stub_entry->h == htab->tls_get_addr_fd
9922 || stub_entry->h == htab->tls_get_addr)
9923 && !htab->no_tls_get_addr_opt)
9924 size += 13 * 4;
9925 if (info->emitrelocations)
9926 {
9927 stub_entry->stub_sec->reloc_count
9928 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9929 stub_entry->stub_sec->flags |= SEC_RELOC;
9930 }
9931 }
9932 else
9933 {
9934 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9935 variants. */
9936 bfd_vma r2off = 0;
9937
9938 off = (stub_entry->target_value
9939 + stub_entry->target_section->output_offset
9940 + stub_entry->target_section->output_section->vma);
9941 off -= (stub_entry->stub_sec->size
9942 + stub_entry->stub_sec->output_offset
9943 + stub_entry->stub_sec->output_section->vma);
9944
9945 /* Reset the stub type from the plt variant in case we now
9946 can reach with a shorter stub. */
9947 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9948 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9949
9950 size = 4;
9951 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9952 {
9953 r2off = get_r2off (info, stub_entry);
9954 if (r2off == 0)
9955 {
9956 htab->stub_error = TRUE;
9957 return FALSE;
9958 }
9959 size = 12;
9960 if (PPC_HA (r2off) != 0)
9961 size = 16;
9962 off -= size - 4;
9963 }
9964
9965 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
9966 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9967 {
9968 struct ppc_branch_hash_entry *br_entry;
9969
9970 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9971 stub_entry->root.string + 9,
9972 TRUE, FALSE);
9973 if (br_entry == NULL)
9974 {
9975 info->callbacks->einfo (_("can't build branch stub `%s'\n"),
9976 stub_entry->root.string);
9977 htab->stub_error = TRUE;
9978 return FALSE;
9979 }
9980
9981 if (br_entry->iter != htab->stub_iteration)
9982 {
9983 br_entry->iter = htab->stub_iteration;
9984 br_entry->offset = htab->brlt->size;
9985 htab->brlt->size += 8;
9986
9987 if (htab->relbrlt != NULL)
9988 htab->relbrlt->size += sizeof (Elf64_External_Rela);
9989 else if (info->emitrelocations)
9990 {
9991 htab->brlt->reloc_count += 1;
9992 htab->brlt->flags |= SEC_RELOC;
9993 }
9994 }
9995
9996 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
9997 off = (br_entry->offset
9998 + htab->brlt->output_offset
9999 + htab->brlt->output_section->vma
10000 - elf_gp (htab->brlt->output_section->owner)
10001 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10002
10003 if (info->emitrelocations)
10004 {
10005 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10006 stub_entry->stub_sec->flags |= SEC_RELOC;
10007 }
10008
10009 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10010 {
10011 size = 12;
10012 if (PPC_HA (off) != 0)
10013 size = 16;
10014 }
10015 else
10016 {
10017 size = 20;
10018 if (PPC_HA (off) != 0)
10019 size += 4;
10020
10021 if (PPC_HA (r2off) != 0)
10022 size += 4;
10023 }
10024 }
10025 else if (info->emitrelocations)
10026 {
10027 stub_entry->stub_sec->reloc_count += 1;
10028 stub_entry->stub_sec->flags |= SEC_RELOC;
10029 }
10030 }
10031
10032 stub_entry->stub_sec->size += size;
10033 return TRUE;
10034 }
10035
10036 /* Set up various things so that we can make a list of input sections
10037 for each output section included in the link. Returns -1 on error,
10038 0 when no stubs will be needed, and 1 on success. */
10039
10040 int
10041 ppc64_elf_setup_section_lists
10042 (struct bfd_link_info *info,
10043 asection *(*add_stub_section) (const char *, asection *),
10044 void (*layout_sections_again) (void))
10045 {
10046 bfd *input_bfd;
10047 int top_id, top_index, id;
10048 asection *section;
10049 asection **input_list;
10050 bfd_size_type amt;
10051 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10052
10053 if (htab == NULL)
10054 return -1;
10055 /* Stash our params away. */
10056 htab->add_stub_section = add_stub_section;
10057 htab->layout_sections_again = layout_sections_again;
10058
10059 if (htab->brlt == NULL)
10060 return 0;
10061
10062 /* Find the top input section id. */
10063 for (input_bfd = info->input_bfds, top_id = 3;
10064 input_bfd != NULL;
10065 input_bfd = input_bfd->link_next)
10066 {
10067 for (section = input_bfd->sections;
10068 section != NULL;
10069 section = section->next)
10070 {
10071 if (top_id < section->id)
10072 top_id = section->id;
10073 }
10074 }
10075
10076 htab->top_id = top_id;
10077 amt = sizeof (struct map_stub) * (top_id + 1);
10078 htab->stub_group = bfd_zmalloc (amt);
10079 if (htab->stub_group == NULL)
10080 return -1;
10081
10082 /* Set toc_off for com, und, abs and ind sections. */
10083 for (id = 0; id < 3; id++)
10084 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10085
10086 /* We can't use output_bfd->section_count here to find the top output
10087 section index as some sections may have been removed, and
10088 strip_excluded_output_sections doesn't renumber the indices. */
10089 for (section = info->output_bfd->sections, top_index = 0;
10090 section != NULL;
10091 section = section->next)
10092 {
10093 if (top_index < section->index)
10094 top_index = section->index;
10095 }
10096
10097 htab->top_index = top_index;
10098 amt = sizeof (asection *) * (top_index + 1);
10099 input_list = bfd_zmalloc (amt);
10100 htab->input_list = input_list;
10101 if (input_list == NULL)
10102 return -1;
10103
10104 return 1;
10105 }
10106
10107 /* Set up for first pass at multitoc partitioning. */
10108
10109 void
10110 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10111 {
10112 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10113
10114 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10115 htab->toc_curr = elf_gp (info->output_bfd);
10116 htab->toc_bfd = NULL;
10117 htab->toc_first_sec = NULL;
10118 }
10119
10120 /* The linker repeatedly calls this function for each TOC input section
10121 and linker generated GOT section. Group input bfds such that the toc
10122 within a group is less than 64k in size. */
10123
10124 bfd_boolean
10125 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10126 {
10127 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10128 bfd_vma addr, off, limit;
10129
10130 if (htab == NULL)
10131 return FALSE;
10132
10133 if (!htab->second_toc_pass)
10134 {
10135 /* Keep track of the first .toc or .got section for this input bfd. */
10136 if (htab->toc_bfd != isec->owner)
10137 {
10138 htab->toc_bfd = isec->owner;
10139 htab->toc_first_sec = isec;
10140 }
10141
10142 addr = isec->output_offset + isec->output_section->vma;
10143 off = addr - htab->toc_curr;
10144 limit = 0x80008000;
10145 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10146 limit = 0x10000;
10147 if (off + isec->size > limit)
10148 {
10149 addr = (htab->toc_first_sec->output_offset
10150 + htab->toc_first_sec->output_section->vma);
10151 htab->toc_curr = addr;
10152 }
10153
10154 /* toc_curr is the base address of this toc group. Set elf_gp
10155 for the input section to be the offset relative to the
10156 output toc base plus 0x8000. Making the input elf_gp an
10157 offset allows us to move the toc as a whole without
10158 recalculating input elf_gp. */
10159 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10160 off += TOC_BASE_OFF;
10161
10162 /* Die if someone uses a linker script that doesn't keep input
10163 file .toc and .got together. */
10164 if (elf_gp (isec->owner) != 0
10165 && elf_gp (isec->owner) != off)
10166 return FALSE;
10167
10168 elf_gp (isec->owner) = off;
10169 return TRUE;
10170 }
10171
10172 /* During the second pass toc_first_sec points to the start of
10173 a toc group, and toc_curr is used to track the old elf_gp.
10174 We use toc_bfd to ensure we only look at each bfd once. */
10175 if (htab->toc_bfd == isec->owner)
10176 return TRUE;
10177 htab->toc_bfd = isec->owner;
10178
10179 if (htab->toc_first_sec == NULL
10180 || htab->toc_curr != elf_gp (isec->owner))
10181 {
10182 htab->toc_curr = elf_gp (isec->owner);
10183 htab->toc_first_sec = isec;
10184 }
10185 addr = (htab->toc_first_sec->output_offset
10186 + htab->toc_first_sec->output_section->vma);
10187 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10188 elf_gp (isec->owner) = off;
10189
10190 return TRUE;
10191 }
10192
10193 /* Called via elf_link_hash_traverse to merge GOT entries for global
10194 symbol H. */
10195
10196 static bfd_boolean
10197 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10198 {
10199 if (h->root.type == bfd_link_hash_indirect)
10200 return TRUE;
10201
10202 merge_got_entries (&h->got.glist);
10203
10204 return TRUE;
10205 }
10206
10207 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10208 symbol H. */
10209
10210 static bfd_boolean
10211 reallocate_got (struct elf_link_hash_entry *h, void *inf)
10212 {
10213 struct got_entry *gent;
10214
10215 if (h->root.type == bfd_link_hash_indirect)
10216 return TRUE;
10217
10218 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10219 if (!gent->is_indirect)
10220 allocate_got (h, (struct bfd_link_info *) inf, gent);
10221 return TRUE;
10222 }
10223
10224 /* Called on the first multitoc pass after the last call to
10225 ppc64_elf_next_toc_section. This function removes duplicate GOT
10226 entries. */
10227
10228 bfd_boolean
10229 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
10230 {
10231 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10232 struct bfd *ibfd, *ibfd2;
10233 bfd_boolean done_something;
10234
10235 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
10236
10237 if (!htab->do_multi_toc)
10238 return FALSE;
10239
10240 /* Merge global sym got entries within a toc group. */
10241 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10242
10243 /* And tlsld_got. */
10244 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10245 {
10246 struct got_entry *ent, *ent2;
10247
10248 if (!is_ppc64_elf (ibfd))
10249 continue;
10250
10251 ent = ppc64_tlsld_got (ibfd);
10252 if (!ent->is_indirect
10253 && ent->got.offset != (bfd_vma) -1)
10254 {
10255 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10256 {
10257 if (!is_ppc64_elf (ibfd2))
10258 continue;
10259
10260 ent2 = ppc64_tlsld_got (ibfd2);
10261 if (!ent2->is_indirect
10262 && ent2->got.offset != (bfd_vma) -1
10263 && elf_gp (ibfd2) == elf_gp (ibfd))
10264 {
10265 ent2->is_indirect = TRUE;
10266 ent2->got.ent = ent;
10267 }
10268 }
10269 }
10270 }
10271
10272 /* Zap sizes of got sections. */
10273 htab->reliplt->rawsize = htab->reliplt->size;
10274 htab->reliplt->size -= htab->got_reli_size;
10275 htab->got_reli_size = 0;
10276
10277 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10278 {
10279 asection *got, *relgot;
10280
10281 if (!is_ppc64_elf (ibfd))
10282 continue;
10283
10284 got = ppc64_elf_tdata (ibfd)->got;
10285 if (got != NULL)
10286 {
10287 got->rawsize = got->size;
10288 got->size = 0;
10289 relgot = ppc64_elf_tdata (ibfd)->relgot;
10290 relgot->rawsize = relgot->size;
10291 relgot->size = 0;
10292 }
10293 }
10294
10295 /* Now reallocate the got, local syms first. We don't need to
10296 allocate section contents again since we never increase size. */
10297 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10298 {
10299 struct got_entry **lgot_ents;
10300 struct got_entry **end_lgot_ents;
10301 struct plt_entry **local_plt;
10302 struct plt_entry **end_local_plt;
10303 unsigned char *lgot_masks;
10304 bfd_size_type locsymcount;
10305 Elf_Internal_Shdr *symtab_hdr;
10306 asection *s, *srel;
10307
10308 if (!is_ppc64_elf (ibfd))
10309 continue;
10310
10311 lgot_ents = elf_local_got_ents (ibfd);
10312 if (!lgot_ents)
10313 continue;
10314
10315 symtab_hdr = &elf_symtab_hdr (ibfd);
10316 locsymcount = symtab_hdr->sh_info;
10317 end_lgot_ents = lgot_ents + locsymcount;
10318 local_plt = (struct plt_entry **) end_lgot_ents;
10319 end_local_plt = local_plt + locsymcount;
10320 lgot_masks = (unsigned char *) end_local_plt;
10321 s = ppc64_elf_tdata (ibfd)->got;
10322 srel = ppc64_elf_tdata (ibfd)->relgot;
10323 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10324 {
10325 struct got_entry *ent;
10326
10327 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
10328 {
10329 unsigned int num = 1;
10330 ent->got.offset = s->size;
10331 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10332 num = 2;
10333 s->size += num * 8;
10334 if (info->shared)
10335 srel->size += num * sizeof (Elf64_External_Rela);
10336 else if ((*lgot_masks & PLT_IFUNC) != 0)
10337 {
10338 htab->reliplt->size
10339 += num * sizeof (Elf64_External_Rela);
10340 htab->got_reli_size
10341 += num * sizeof (Elf64_External_Rela);
10342 }
10343 }
10344 }
10345 }
10346
10347 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10348
10349 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10350 {
10351 struct got_entry *ent;
10352
10353 if (!is_ppc64_elf (ibfd))
10354 continue;
10355
10356 ent = ppc64_tlsld_got (ibfd);
10357 if (!ent->is_indirect
10358 && ent->got.offset != (bfd_vma) -1)
10359 {
10360 asection *s = ppc64_elf_tdata (ibfd)->got;
10361 ent->got.offset = s->size;
10362 s->size += 16;
10363 if (info->shared)
10364 {
10365 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10366 srel->size += sizeof (Elf64_External_Rela);
10367 }
10368 }
10369 }
10370
10371 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10372 if (!done_something)
10373 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10374 {
10375 asection *got;
10376
10377 if (!is_ppc64_elf (ibfd))
10378 continue;
10379
10380 got = ppc64_elf_tdata (ibfd)->got;
10381 if (got != NULL)
10382 {
10383 done_something = got->rawsize != got->size;
10384 if (done_something)
10385 break;
10386 }
10387 }
10388
10389 if (done_something)
10390 (*htab->layout_sections_again) ();
10391
10392 /* Set up for second pass over toc sections to recalculate elf_gp
10393 on input sections. */
10394 htab->toc_bfd = NULL;
10395 htab->toc_first_sec = NULL;
10396 htab->second_toc_pass = TRUE;
10397 return done_something;
10398 }
10399
10400 /* Called after second pass of multitoc partitioning. */
10401
10402 void
10403 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10404 {
10405 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10406
10407 /* After the second pass, toc_curr tracks the TOC offset used
10408 for code sections below in ppc64_elf_next_input_section. */
10409 htab->toc_curr = TOC_BASE_OFF;
10410 }
10411
10412 /* No toc references were found in ISEC. If the code in ISEC makes no
10413 calls, then there's no need to use toc adjusting stubs when branching
10414 into ISEC. Actually, indirect calls from ISEC are OK as they will
10415 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10416 needed, and 2 if a cyclical call-graph was found but no other reason
10417 for a stub was detected. If called from the top level, a return of
10418 2 means the same as a return of 0. */
10419
10420 static int
10421 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
10422 {
10423 int ret;
10424
10425 /* Mark this section as checked. */
10426 isec->call_check_done = 1;
10427
10428 /* We know none of our code bearing sections will need toc stubs. */
10429 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10430 return 0;
10431
10432 if (isec->size == 0)
10433 return 0;
10434
10435 if (isec->output_section == NULL)
10436 return 0;
10437
10438 ret = 0;
10439 if (isec->reloc_count != 0)
10440 {
10441 Elf_Internal_Rela *relstart, *rel;
10442 Elf_Internal_Sym *local_syms;
10443 struct ppc_link_hash_table *htab;
10444
10445 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10446 info->keep_memory);
10447 if (relstart == NULL)
10448 return -1;
10449
10450 /* Look for branches to outside of this section. */
10451 local_syms = NULL;
10452 htab = ppc_hash_table (info);
10453 if (htab == NULL)
10454 return -1;
10455
10456 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
10457 {
10458 enum elf_ppc64_reloc_type r_type;
10459 unsigned long r_symndx;
10460 struct elf_link_hash_entry *h;
10461 struct ppc_link_hash_entry *eh;
10462 Elf_Internal_Sym *sym;
10463 asection *sym_sec;
10464 struct _opd_sec_data *opd;
10465 bfd_vma sym_value;
10466 bfd_vma dest;
10467
10468 r_type = ELF64_R_TYPE (rel->r_info);
10469 if (r_type != R_PPC64_REL24
10470 && r_type != R_PPC64_REL14
10471 && r_type != R_PPC64_REL14_BRTAKEN
10472 && r_type != R_PPC64_REL14_BRNTAKEN)
10473 continue;
10474
10475 r_symndx = ELF64_R_SYM (rel->r_info);
10476 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10477 isec->owner))
10478 {
10479 ret = -1;
10480 break;
10481 }
10482
10483 /* Calls to dynamic lib functions go through a plt call stub
10484 that uses r2. */
10485 eh = (struct ppc_link_hash_entry *) h;
10486 if (eh != NULL
10487 && (eh->elf.plt.plist != NULL
10488 || (eh->oh != NULL
10489 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10490 {
10491 ret = 1;
10492 break;
10493 }
10494
10495 if (sym_sec == NULL)
10496 /* Ignore other undefined symbols. */
10497 continue;
10498
10499 /* Assume branches to other sections not included in the
10500 link need stubs too, to cover -R and absolute syms. */
10501 if (sym_sec->output_section == NULL)
10502 {
10503 ret = 1;
10504 break;
10505 }
10506
10507 if (h == NULL)
10508 sym_value = sym->st_value;
10509 else
10510 {
10511 if (h->root.type != bfd_link_hash_defined
10512 && h->root.type != bfd_link_hash_defweak)
10513 abort ();
10514 sym_value = h->root.u.def.value;
10515 }
10516 sym_value += rel->r_addend;
10517
10518 /* If this branch reloc uses an opd sym, find the code section. */
10519 opd = get_opd_info (sym_sec);
10520 if (opd != NULL)
10521 {
10522 if (h == NULL && opd->adjust != NULL)
10523 {
10524 long adjust;
10525
10526 adjust = opd->adjust[sym->st_value / 8];
10527 if (adjust == -1)
10528 /* Assume deleted functions won't ever be called. */
10529 continue;
10530 sym_value += adjust;
10531 }
10532
10533 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10534 if (dest == (bfd_vma) -1)
10535 continue;
10536 }
10537 else
10538 dest = (sym_value
10539 + sym_sec->output_offset
10540 + sym_sec->output_section->vma);
10541
10542 /* Ignore branch to self. */
10543 if (sym_sec == isec)
10544 continue;
10545
10546 /* If the called function uses the toc, we need a stub. */
10547 if (sym_sec->has_toc_reloc
10548 || sym_sec->makes_toc_func_call)
10549 {
10550 ret = 1;
10551 break;
10552 }
10553
10554 /* Assume any branch that needs a long branch stub might in fact
10555 need a plt_branch stub. A plt_branch stub uses r2. */
10556 else if (dest - (isec->output_offset
10557 + isec->output_section->vma
10558 + rel->r_offset) + (1 << 25) >= (2 << 25))
10559 {
10560 ret = 1;
10561 break;
10562 }
10563
10564 /* If calling back to a section in the process of being
10565 tested, we can't say for sure that no toc adjusting stubs
10566 are needed, so don't return zero. */
10567 else if (sym_sec->call_check_in_progress)
10568 ret = 2;
10569
10570 /* Branches to another section that itself doesn't have any TOC
10571 references are OK. Recursively call ourselves to check. */
10572 else if (!sym_sec->call_check_done)
10573 {
10574 int recur;
10575
10576 /* Mark current section as indeterminate, so that other
10577 sections that call back to current won't be marked as
10578 known. */
10579 isec->call_check_in_progress = 1;
10580 recur = toc_adjusting_stub_needed (info, sym_sec);
10581 isec->call_check_in_progress = 0;
10582
10583 if (recur != 0)
10584 {
10585 ret = recur;
10586 if (recur != 2)
10587 break;
10588 }
10589 }
10590 }
10591
10592 if (local_syms != NULL
10593 && (elf_symtab_hdr (isec->owner).contents
10594 != (unsigned char *) local_syms))
10595 free (local_syms);
10596 if (elf_section_data (isec)->relocs != relstart)
10597 free (relstart);
10598 }
10599
10600 if ((ret & 1) == 0
10601 && isec->map_head.s != NULL
10602 && (strcmp (isec->output_section->name, ".init") == 0
10603 || strcmp (isec->output_section->name, ".fini") == 0))
10604 {
10605 if (isec->map_head.s->has_toc_reloc
10606 || isec->map_head.s->makes_toc_func_call)
10607 ret = 1;
10608 else if (!isec->map_head.s->call_check_done)
10609 {
10610 int recur;
10611 isec->call_check_in_progress = 1;
10612 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10613 isec->call_check_in_progress = 0;
10614 if (recur != 0)
10615 ret = recur;
10616 }
10617 }
10618
10619 if (ret == 1)
10620 isec->makes_toc_func_call = 1;
10621
10622 return ret;
10623 }
10624
10625 /* The linker repeatedly calls this function for each input section,
10626 in the order that input sections are linked into output sections.
10627 Build lists of input sections to determine groupings between which
10628 we may insert linker stubs. */
10629
10630 bfd_boolean
10631 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
10632 {
10633 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10634
10635 if (htab == NULL)
10636 return FALSE;
10637
10638 if ((isec->output_section->flags & SEC_CODE) != 0
10639 && isec->output_section->index <= htab->top_index)
10640 {
10641 asection **list = htab->input_list + isec->output_section->index;
10642 /* Steal the link_sec pointer for our list. */
10643 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
10644 /* This happens to make the list in reverse order,
10645 which is what we want. */
10646 PREV_SEC (isec) = *list;
10647 *list = isec;
10648 }
10649
10650 if (htab->multi_toc_needed)
10651 {
10652 /* If a code section has a function that uses the TOC then we need
10653 to use the right TOC (obviously). Also, make sure that .opd gets
10654 the correct TOC value for R_PPC64_TOC relocs that don't have or
10655 can't find their function symbol (shouldn't ever happen now).
10656 Also specially treat .fixup for the linux kernel. .fixup
10657 contains branches, but only back to the function that hit an
10658 exception. */
10659 if (isec->has_toc_reloc
10660 || (isec->flags & SEC_CODE) == 0
10661 || strcmp (isec->name, ".fixup") == 0)
10662 {
10663 if (elf_gp (isec->owner) != 0)
10664 htab->toc_curr = elf_gp (isec->owner);
10665 }
10666 else
10667 {
10668 if (!isec->call_check_done
10669 && toc_adjusting_stub_needed (info, isec) < 0)
10670 return FALSE;
10671 /* If we make a local call from this section, ie. a branch
10672 without a following nop, then we have no place to put a
10673 toc restoring insn. We must use the same toc group as
10674 the callee.
10675 Testing makes_toc_func_call actually tests for *any*
10676 calls to functions that need a good toc pointer. A more
10677 precise test would be better, as this one will set
10678 incorrect values for pasted .init/.fini fragments.
10679 (Fixed later in check_pasted_section.) */
10680 if (isec->makes_toc_func_call
10681 && elf_gp (isec->owner) != 0)
10682 htab->toc_curr = elf_gp (isec->owner);
10683 }
10684 }
10685
10686 /* Functions that don't use the TOC can belong in any TOC group.
10687 Use the last TOC base. */
10688 htab->stub_group[isec->id].toc_off = htab->toc_curr;
10689 return TRUE;
10690 }
10691
10692 /* Check that all .init and .fini sections use the same toc, if they
10693 have toc relocs. */
10694
10695 static bfd_boolean
10696 check_pasted_section (struct bfd_link_info *info, const char *name)
10697 {
10698 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10699
10700 if (o != NULL)
10701 {
10702 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10703 bfd_vma toc_off = 0;
10704 asection *i;
10705
10706 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10707 if (i->has_toc_reloc)
10708 {
10709 if (toc_off == 0)
10710 toc_off = htab->stub_group[i->id].toc_off;
10711 else if (toc_off != htab->stub_group[i->id].toc_off)
10712 return FALSE;
10713 }
10714
10715 if (toc_off == 0)
10716 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10717 if (i->makes_toc_func_call)
10718 {
10719 toc_off = htab->stub_group[i->id].toc_off;
10720 break;
10721 }
10722
10723 /* Make sure the whole pasted function uses the same toc offset. */
10724 if (toc_off != 0)
10725 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10726 htab->stub_group[i->id].toc_off = toc_off;
10727 }
10728 return TRUE;
10729 }
10730
10731 bfd_boolean
10732 ppc64_elf_check_init_fini (struct bfd_link_info *info)
10733 {
10734 return (check_pasted_section (info, ".init")
10735 & check_pasted_section (info, ".fini"));
10736 }
10737
10738 /* See whether we can group stub sections together. Grouping stub
10739 sections may result in fewer stubs. More importantly, we need to
10740 put all .init* and .fini* stubs at the beginning of the .init or
10741 .fini output sections respectively, because glibc splits the
10742 _init and _fini functions into multiple parts. Putting a stub in
10743 the middle of a function is not a good idea. */
10744
10745 static void
10746 group_sections (struct ppc_link_hash_table *htab,
10747 bfd_size_type stub_group_size,
10748 bfd_boolean stubs_always_before_branch)
10749 {
10750 asection **list;
10751 bfd_size_type stub14_group_size;
10752 bfd_boolean suppress_size_errors;
10753
10754 suppress_size_errors = FALSE;
10755 stub14_group_size = stub_group_size;
10756 if (stub_group_size == 1)
10757 {
10758 /* Default values. */
10759 if (stubs_always_before_branch)
10760 {
10761 stub_group_size = 0x1e00000;
10762 stub14_group_size = 0x7800;
10763 }
10764 else
10765 {
10766 stub_group_size = 0x1c00000;
10767 stub14_group_size = 0x7000;
10768 }
10769 suppress_size_errors = TRUE;
10770 }
10771
10772 list = htab->input_list + htab->top_index;
10773 do
10774 {
10775 asection *tail = *list;
10776 while (tail != NULL)
10777 {
10778 asection *curr;
10779 asection *prev;
10780 bfd_size_type total;
10781 bfd_boolean big_sec;
10782 bfd_vma curr_toc;
10783
10784 curr = tail;
10785 total = tail->size;
10786 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10787 && ppc64_elf_section_data (tail)->has_14bit_branch
10788 ? stub14_group_size : stub_group_size);
10789 if (big_sec && !suppress_size_errors)
10790 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10791 tail->owner, tail);
10792 curr_toc = htab->stub_group[tail->id].toc_off;
10793
10794 while ((prev = PREV_SEC (curr)) != NULL
10795 && ((total += curr->output_offset - prev->output_offset)
10796 < (ppc64_elf_section_data (prev) != NULL
10797 && ppc64_elf_section_data (prev)->has_14bit_branch
10798 ? stub14_group_size : stub_group_size))
10799 && htab->stub_group[prev->id].toc_off == curr_toc)
10800 curr = prev;
10801
10802 /* OK, the size from the start of CURR to the end is less
10803 than stub_group_size and thus can be handled by one stub
10804 section. (or the tail section is itself larger than
10805 stub_group_size, in which case we may be toast.) We
10806 should really be keeping track of the total size of stubs
10807 added here, as stubs contribute to the final output
10808 section size. That's a little tricky, and this way will
10809 only break if stubs added make the total size more than
10810 2^25, ie. for the default stub_group_size, if stubs total
10811 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10812 do
10813 {
10814 prev = PREV_SEC (tail);
10815 /* Set up this stub group. */
10816 htab->stub_group[tail->id].link_sec = curr;
10817 }
10818 while (tail != curr && (tail = prev) != NULL);
10819
10820 /* But wait, there's more! Input sections up to stub_group_size
10821 bytes before the stub section can be handled by it too.
10822 Don't do this if we have a really large section after the
10823 stubs, as adding more stubs increases the chance that
10824 branches may not reach into the stub section. */
10825 if (!stubs_always_before_branch && !big_sec)
10826 {
10827 total = 0;
10828 while (prev != NULL
10829 && ((total += tail->output_offset - prev->output_offset)
10830 < (ppc64_elf_section_data (prev) != NULL
10831 && ppc64_elf_section_data (prev)->has_14bit_branch
10832 ? stub14_group_size : stub_group_size))
10833 && htab->stub_group[prev->id].toc_off == curr_toc)
10834 {
10835 tail = prev;
10836 prev = PREV_SEC (tail);
10837 htab->stub_group[tail->id].link_sec = curr;
10838 }
10839 }
10840 tail = prev;
10841 }
10842 }
10843 while (list-- != htab->input_list);
10844 free (htab->input_list);
10845 #undef PREV_SEC
10846 }
10847
10848 /* Determine and set the size of the stub section for a final link.
10849
10850 The basic idea here is to examine all the relocations looking for
10851 PC-relative calls to a target that is unreachable with a "bl"
10852 instruction. */
10853
10854 bfd_boolean
10855 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
10856 {
10857 bfd_size_type stub_group_size;
10858 bfd_boolean stubs_always_before_branch;
10859 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10860
10861 if (htab == NULL)
10862 return FALSE;
10863
10864 stubs_always_before_branch = group_size < 0;
10865 if (group_size < 0)
10866 stub_group_size = -group_size;
10867 else
10868 stub_group_size = group_size;
10869
10870 group_sections (htab, stub_group_size, stubs_always_before_branch);
10871
10872 while (1)
10873 {
10874 bfd *input_bfd;
10875 unsigned int bfd_indx;
10876 asection *stub_sec;
10877
10878 htab->stub_iteration += 1;
10879
10880 for (input_bfd = info->input_bfds, bfd_indx = 0;
10881 input_bfd != NULL;
10882 input_bfd = input_bfd->link_next, bfd_indx++)
10883 {
10884 Elf_Internal_Shdr *symtab_hdr;
10885 asection *section;
10886 Elf_Internal_Sym *local_syms = NULL;
10887
10888 if (!is_ppc64_elf (input_bfd))
10889 continue;
10890
10891 /* We'll need the symbol table in a second. */
10892 symtab_hdr = &elf_symtab_hdr (input_bfd);
10893 if (symtab_hdr->sh_info == 0)
10894 continue;
10895
10896 /* Walk over each section attached to the input bfd. */
10897 for (section = input_bfd->sections;
10898 section != NULL;
10899 section = section->next)
10900 {
10901 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
10902
10903 /* If there aren't any relocs, then there's nothing more
10904 to do. */
10905 if ((section->flags & SEC_RELOC) == 0
10906 || (section->flags & SEC_ALLOC) == 0
10907 || (section->flags & SEC_LOAD) == 0
10908 || (section->flags & SEC_CODE) == 0
10909 || section->reloc_count == 0)
10910 continue;
10911
10912 /* If this section is a link-once section that will be
10913 discarded, then don't create any stubs. */
10914 if (section->output_section == NULL
10915 || section->output_section->owner != info->output_bfd)
10916 continue;
10917
10918 /* Get the relocs. */
10919 internal_relocs
10920 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
10921 info->keep_memory);
10922 if (internal_relocs == NULL)
10923 goto error_ret_free_local;
10924
10925 /* Now examine each relocation. */
10926 irela = internal_relocs;
10927 irelaend = irela + section->reloc_count;
10928 for (; irela < irelaend; irela++)
10929 {
10930 enum elf_ppc64_reloc_type r_type;
10931 unsigned int r_indx;
10932 enum ppc_stub_type stub_type;
10933 struct ppc_stub_hash_entry *stub_entry;
10934 asection *sym_sec, *code_sec;
10935 bfd_vma sym_value, code_value;
10936 bfd_vma destination;
10937 bfd_boolean ok_dest;
10938 struct ppc_link_hash_entry *hash;
10939 struct ppc_link_hash_entry *fdh;
10940 struct elf_link_hash_entry *h;
10941 Elf_Internal_Sym *sym;
10942 char *stub_name;
10943 const asection *id_sec;
10944 struct _opd_sec_data *opd;
10945 struct plt_entry *plt_ent;
10946
10947 r_type = ELF64_R_TYPE (irela->r_info);
10948 r_indx = ELF64_R_SYM (irela->r_info);
10949
10950 if (r_type >= R_PPC64_max)
10951 {
10952 bfd_set_error (bfd_error_bad_value);
10953 goto error_ret_free_internal;
10954 }
10955
10956 /* Only look for stubs on branch instructions. */
10957 if (r_type != R_PPC64_REL24
10958 && r_type != R_PPC64_REL14
10959 && r_type != R_PPC64_REL14_BRTAKEN
10960 && r_type != R_PPC64_REL14_BRNTAKEN)
10961 continue;
10962
10963 /* Now determine the call target, its name, value,
10964 section. */
10965 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10966 r_indx, input_bfd))
10967 goto error_ret_free_internal;
10968 hash = (struct ppc_link_hash_entry *) h;
10969
10970 ok_dest = FALSE;
10971 fdh = NULL;
10972 sym_value = 0;
10973 if (hash == NULL)
10974 {
10975 sym_value = sym->st_value;
10976 ok_dest = TRUE;
10977 }
10978 else if (hash->elf.root.type == bfd_link_hash_defined
10979 || hash->elf.root.type == bfd_link_hash_defweak)
10980 {
10981 sym_value = hash->elf.root.u.def.value;
10982 if (sym_sec->output_section != NULL)
10983 ok_dest = TRUE;
10984 }
10985 else if (hash->elf.root.type == bfd_link_hash_undefweak
10986 || hash->elf.root.type == bfd_link_hash_undefined)
10987 {
10988 /* Recognise an old ABI func code entry sym, and
10989 use the func descriptor sym instead if it is
10990 defined. */
10991 if (hash->elf.root.root.string[0] == '.'
10992 && (fdh = lookup_fdh (hash, htab)) != NULL)
10993 {
10994 if (fdh->elf.root.type == bfd_link_hash_defined
10995 || fdh->elf.root.type == bfd_link_hash_defweak)
10996 {
10997 sym_sec = fdh->elf.root.u.def.section;
10998 sym_value = fdh->elf.root.u.def.value;
10999 if (sym_sec->output_section != NULL)
11000 ok_dest = TRUE;
11001 }
11002 else
11003 fdh = NULL;
11004 }
11005 }
11006 else
11007 {
11008 bfd_set_error (bfd_error_bad_value);
11009 goto error_ret_free_internal;
11010 }
11011
11012 destination = 0;
11013 if (ok_dest)
11014 {
11015 sym_value += irela->r_addend;
11016 destination = (sym_value
11017 + sym_sec->output_offset
11018 + sym_sec->output_section->vma);
11019 }
11020
11021 code_sec = sym_sec;
11022 code_value = sym_value;
11023 opd = get_opd_info (sym_sec);
11024 if (opd != NULL)
11025 {
11026 bfd_vma dest;
11027
11028 if (hash == NULL && opd->adjust != NULL)
11029 {
11030 long adjust = opd->adjust[sym_value / 8];
11031 if (adjust == -1)
11032 continue;
11033 code_value += adjust;
11034 sym_value += adjust;
11035 }
11036 dest = opd_entry_value (sym_sec, sym_value,
11037 &code_sec, &code_value);
11038 if (dest != (bfd_vma) -1)
11039 {
11040 destination = dest;
11041 if (fdh != NULL)
11042 {
11043 /* Fixup old ABI sym to point at code
11044 entry. */
11045 hash->elf.root.type = bfd_link_hash_defweak;
11046 hash->elf.root.u.def.section = code_sec;
11047 hash->elf.root.u.def.value = code_value;
11048 }
11049 }
11050 }
11051
11052 /* Determine what (if any) linker stub is needed. */
11053 plt_ent = NULL;
11054 stub_type = ppc_type_of_stub (section, irela, &hash,
11055 &plt_ent, destination);
11056
11057 if (stub_type != ppc_stub_plt_call)
11058 {
11059 /* Check whether we need a TOC adjusting stub.
11060 Since the linker pastes together pieces from
11061 different object files when creating the
11062 _init and _fini functions, it may be that a
11063 call to what looks like a local sym is in
11064 fact a call needing a TOC adjustment. */
11065 if (code_sec != NULL
11066 && code_sec->output_section != NULL
11067 && (htab->stub_group[code_sec->id].toc_off
11068 != htab->stub_group[section->id].toc_off)
11069 && (code_sec->has_toc_reloc
11070 || code_sec->makes_toc_func_call))
11071 stub_type = ppc_stub_long_branch_r2off;
11072 }
11073
11074 if (stub_type == ppc_stub_none)
11075 continue;
11076
11077 /* __tls_get_addr calls might be eliminated. */
11078 if (stub_type != ppc_stub_plt_call
11079 && hash != NULL
11080 && (hash == htab->tls_get_addr
11081 || hash == htab->tls_get_addr_fd)
11082 && section->has_tls_reloc
11083 && irela != internal_relocs)
11084 {
11085 /* Get tls info. */
11086 unsigned char *tls_mask;
11087
11088 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11089 irela - 1, input_bfd))
11090 goto error_ret_free_internal;
11091 if (*tls_mask != 0)
11092 continue;
11093 }
11094
11095 /* Support for grouping stub sections. */
11096 id_sec = htab->stub_group[section->id].link_sec;
11097
11098 /* Get the name of this stub. */
11099 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11100 if (!stub_name)
11101 goto error_ret_free_internal;
11102
11103 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11104 stub_name, FALSE, FALSE);
11105 if (stub_entry != NULL)
11106 {
11107 /* The proper stub has already been created. */
11108 free (stub_name);
11109 continue;
11110 }
11111
11112 stub_entry = ppc_add_stub (stub_name, section, info);
11113 if (stub_entry == NULL)
11114 {
11115 free (stub_name);
11116 error_ret_free_internal:
11117 if (elf_section_data (section)->relocs == NULL)
11118 free (internal_relocs);
11119 error_ret_free_local:
11120 if (local_syms != NULL
11121 && (symtab_hdr->contents
11122 != (unsigned char *) local_syms))
11123 free (local_syms);
11124 return FALSE;
11125 }
11126
11127 stub_entry->stub_type = stub_type;
11128 if (stub_type != ppc_stub_plt_call)
11129 {
11130 stub_entry->target_value = code_value;
11131 stub_entry->target_section = code_sec;
11132 }
11133 else
11134 {
11135 stub_entry->target_value = sym_value;
11136 stub_entry->target_section = sym_sec;
11137 }
11138 stub_entry->h = hash;
11139 stub_entry->plt_ent = plt_ent;
11140 stub_entry->addend = irela->r_addend;
11141
11142 if (stub_entry->h != NULL)
11143 htab->stub_globals += 1;
11144 }
11145
11146 /* We're done with the internal relocs, free them. */
11147 if (elf_section_data (section)->relocs != internal_relocs)
11148 free (internal_relocs);
11149 }
11150
11151 if (local_syms != NULL
11152 && symtab_hdr->contents != (unsigned char *) local_syms)
11153 {
11154 if (!info->keep_memory)
11155 free (local_syms);
11156 else
11157 symtab_hdr->contents = (unsigned char *) local_syms;
11158 }
11159 }
11160
11161 /* We may have added some stubs. Find out the new size of the
11162 stub sections. */
11163 for (stub_sec = htab->stub_bfd->sections;
11164 stub_sec != NULL;
11165 stub_sec = stub_sec->next)
11166 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11167 {
11168 stub_sec->rawsize = stub_sec->size;
11169 stub_sec->size = 0;
11170 stub_sec->reloc_count = 0;
11171 stub_sec->flags &= ~SEC_RELOC;
11172 }
11173
11174 htab->brlt->size = 0;
11175 htab->brlt->reloc_count = 0;
11176 htab->brlt->flags &= ~SEC_RELOC;
11177 if (htab->relbrlt != NULL)
11178 htab->relbrlt->size = 0;
11179
11180 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
11181
11182 if (info->emitrelocations
11183 && htab->glink != NULL && htab->glink->size != 0)
11184 {
11185 htab->glink->reloc_count = 1;
11186 htab->glink->flags |= SEC_RELOC;
11187 }
11188
11189 for (stub_sec = htab->stub_bfd->sections;
11190 stub_sec != NULL;
11191 stub_sec = stub_sec->next)
11192 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11193 && stub_sec->rawsize != stub_sec->size)
11194 break;
11195
11196 /* Exit from this loop when no stubs have been added, and no stubs
11197 have changed size. */
11198 if (stub_sec == NULL)
11199 break;
11200
11201 /* Ask the linker to do its stuff. */
11202 (*htab->layout_sections_again) ();
11203 }
11204
11205 /* It would be nice to strip htab->brlt from the output if the
11206 section is empty, but it's too late. If we strip sections here,
11207 the dynamic symbol table is corrupted since the section symbol
11208 for the stripped section isn't written. */
11209
11210 return TRUE;
11211 }
11212
11213 /* Called after we have determined section placement. If sections
11214 move, we'll be called again. Provide a value for TOCstart. */
11215
11216 bfd_vma
11217 ppc64_elf_toc (bfd *obfd)
11218 {
11219 asection *s;
11220 bfd_vma TOCstart;
11221
11222 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11223 order. The TOC starts where the first of these sections starts. */
11224 s = bfd_get_section_by_name (obfd, ".got");
11225 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11226 s = bfd_get_section_by_name (obfd, ".toc");
11227 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11228 s = bfd_get_section_by_name (obfd, ".tocbss");
11229 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11230 s = bfd_get_section_by_name (obfd, ".plt");
11231 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
11232 {
11233 /* This may happen for
11234 o references to TOC base (SYM@toc / TOC[tc0]) without a
11235 .toc directive
11236 o bad linker script
11237 o --gc-sections and empty TOC sections
11238
11239 FIXME: Warn user? */
11240
11241 /* Look for a likely section. We probably won't even be
11242 using TOCstart. */
11243 for (s = obfd->sections; s != NULL; s = s->next)
11244 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11245 | SEC_EXCLUDE))
11246 == (SEC_ALLOC | SEC_SMALL_DATA))
11247 break;
11248 if (s == NULL)
11249 for (s = obfd->sections; s != NULL; s = s->next)
11250 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
11251 == (SEC_ALLOC | SEC_SMALL_DATA))
11252 break;
11253 if (s == NULL)
11254 for (s = obfd->sections; s != NULL; s = s->next)
11255 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11256 == SEC_ALLOC)
11257 break;
11258 if (s == NULL)
11259 for (s = obfd->sections; s != NULL; s = s->next)
11260 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
11261 break;
11262 }
11263
11264 TOCstart = 0;
11265 if (s != NULL)
11266 TOCstart = s->output_section->vma + s->output_offset;
11267
11268 return TOCstart;
11269 }
11270
11271 /* Build all the stubs associated with the current output file.
11272 The stubs are kept in a hash table attached to the main linker
11273 hash table. This function is called via gldelf64ppc_finish. */
11274
11275 bfd_boolean
11276 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11277 struct bfd_link_info *info,
11278 char **stats)
11279 {
11280 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11281 asection *stub_sec;
11282 bfd_byte *p;
11283 int stub_sec_count = 0;
11284
11285 if (htab == NULL)
11286 return FALSE;
11287
11288 htab->emit_stub_syms = emit_stub_syms;
11289
11290 /* Allocate memory to hold the linker stubs. */
11291 for (stub_sec = htab->stub_bfd->sections;
11292 stub_sec != NULL;
11293 stub_sec = stub_sec->next)
11294 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11295 && stub_sec->size != 0)
11296 {
11297 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11298 if (stub_sec->contents == NULL)
11299 return FALSE;
11300 /* We want to check that built size is the same as calculated
11301 size. rawsize is a convenient location to use. */
11302 stub_sec->rawsize = stub_sec->size;
11303 stub_sec->size = 0;
11304 }
11305
11306 if (htab->glink != NULL && htab->glink->size != 0)
11307 {
11308 unsigned int indx;
11309 bfd_vma plt0;
11310
11311 /* Build the .glink plt call stub. */
11312 if (htab->emit_stub_syms)
11313 {
11314 struct elf_link_hash_entry *h;
11315 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11316 TRUE, FALSE, FALSE);
11317 if (h == NULL)
11318 return FALSE;
11319 if (h->root.type == bfd_link_hash_new)
11320 {
11321 h->root.type = bfd_link_hash_defined;
11322 h->root.u.def.section = htab->glink;
11323 h->root.u.def.value = 8;
11324 h->ref_regular = 1;
11325 h->def_regular = 1;
11326 h->ref_regular_nonweak = 1;
11327 h->forced_local = 1;
11328 h->non_elf = 0;
11329 }
11330 }
11331 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11332 if (info->emitrelocations)
11333 {
11334 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11335 if (r == NULL)
11336 return FALSE;
11337 r->r_offset = (htab->glink->output_offset
11338 + htab->glink->output_section->vma);
11339 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11340 r->r_addend = plt0;
11341 }
11342 p = htab->glink->contents;
11343 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
11344 bfd_put_64 (htab->glink->owner, plt0, p);
11345 p += 8;
11346 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
11347 p += 4;
11348 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
11349 p += 4;
11350 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
11351 p += 4;
11352 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
11353 p += 4;
11354 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
11355 p += 4;
11356 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
11357 p += 4;
11358 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
11359 p += 4;
11360 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
11361 p += 4;
11362 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
11363 p += 4;
11364 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
11365 p += 4;
11366 bfd_put_32 (htab->glink->owner, BCTR, p);
11367 p += 4;
11368 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11369 {
11370 bfd_put_32 (htab->glink->owner, NOP, p);
11371 p += 4;
11372 }
11373
11374 /* Build the .glink lazy link call stubs. */
11375 indx = 0;
11376 while (p < htab->glink->contents + htab->glink->size)
11377 {
11378 if (indx < 0x8000)
11379 {
11380 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
11381 p += 4;
11382 }
11383 else
11384 {
11385 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
11386 p += 4;
11387 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
11388 p += 4;
11389 }
11390 bfd_put_32 (htab->glink->owner,
11391 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
11392 indx++;
11393 p += 4;
11394 }
11395 htab->glink->rawsize = p - htab->glink->contents;
11396 }
11397
11398 if (htab->brlt->size != 0)
11399 {
11400 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
11401 htab->brlt->size);
11402 if (htab->brlt->contents == NULL)
11403 return FALSE;
11404 }
11405 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
11406 {
11407 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
11408 htab->relbrlt->size);
11409 if (htab->relbrlt->contents == NULL)
11410 return FALSE;
11411 }
11412
11413 /* Build the stubs as directed by the stub hash table. */
11414 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
11415
11416 if (htab->relbrlt != NULL)
11417 htab->relbrlt->reloc_count = 0;
11418
11419 for (stub_sec = htab->stub_bfd->sections;
11420 stub_sec != NULL;
11421 stub_sec = stub_sec->next)
11422 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11423 {
11424 stub_sec_count += 1;
11425 if (stub_sec->rawsize != stub_sec->size)
11426 break;
11427 }
11428
11429 if (stub_sec != NULL
11430 || htab->glink->rawsize != htab->glink->size)
11431 {
11432 htab->stub_error = TRUE;
11433 info->callbacks->einfo (_("stubs don't match calculated size\n"));
11434 }
11435
11436 if (htab->stub_error)
11437 return FALSE;
11438
11439 if (stats != NULL)
11440 {
11441 *stats = bfd_malloc (500);
11442 if (*stats == NULL)
11443 return FALSE;
11444
11445 sprintf (*stats, _("linker stubs in %u group%s\n"
11446 " branch %lu\n"
11447 " toc adjust %lu\n"
11448 " long branch %lu\n"
11449 " long toc adj %lu\n"
11450 " plt call %lu"),
11451 stub_sec_count,
11452 stub_sec_count == 1 ? "" : "s",
11453 htab->stub_count[ppc_stub_long_branch - 1],
11454 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11455 htab->stub_count[ppc_stub_plt_branch - 1],
11456 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11457 htab->stub_count[ppc_stub_plt_call - 1]);
11458 }
11459 return TRUE;
11460 }
11461
11462 /* This function undoes the changes made by add_symbol_adjust. */
11463
11464 static bfd_boolean
11465 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11466 {
11467 struct ppc_link_hash_entry *eh;
11468
11469 if (h->root.type == bfd_link_hash_indirect)
11470 return TRUE;
11471
11472 eh = (struct ppc_link_hash_entry *) h;
11473 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11474 return TRUE;
11475
11476 eh->elf.root.type = bfd_link_hash_undefined;
11477 return TRUE;
11478 }
11479
11480 void
11481 ppc64_elf_restore_symbols (struct bfd_link_info *info)
11482 {
11483 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11484
11485 if (htab != NULL)
11486 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
11487 }
11488
11489 /* What to do when ld finds relocations against symbols defined in
11490 discarded sections. */
11491
11492 static unsigned int
11493 ppc64_elf_action_discarded (asection *sec)
11494 {
11495 if (strcmp (".opd", sec->name) == 0)
11496 return 0;
11497
11498 if (strcmp (".toc", sec->name) == 0)
11499 return 0;
11500
11501 if (strcmp (".toc1", sec->name) == 0)
11502 return 0;
11503
11504 return _bfd_elf_default_action_discarded (sec);
11505 }
11506
11507 /* REL points to a low-part reloc on a largetoc instruction sequence.
11508 Find the matching high-part reloc instruction and verify that it
11509 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11510 the high-part reloc. */
11511
11512 static const Elf_Internal_Rela *
11513 ha_reloc_match (const Elf_Internal_Rela *relocs,
11514 const Elf_Internal_Rela *rel,
11515 unsigned int *reg,
11516 bfd_boolean match_addend,
11517 const bfd *input_bfd,
11518 const bfd_byte *contents)
11519 {
11520 enum elf_ppc64_reloc_type r_type, r_type_ha;
11521 bfd_vma r_info_ha, r_addend;
11522
11523 r_type = ELF64_R_TYPE (rel->r_info);
11524 switch (r_type)
11525 {
11526 case R_PPC64_GOT_TLSLD16_LO:
11527 case R_PPC64_GOT_TLSGD16_LO:
11528 case R_PPC64_GOT_TPREL16_LO_DS:
11529 case R_PPC64_GOT_DTPREL16_LO_DS:
11530 case R_PPC64_GOT16_LO:
11531 case R_PPC64_TOC16_LO:
11532 r_type_ha = r_type + 2;
11533 break;
11534 case R_PPC64_GOT16_LO_DS:
11535 r_type_ha = R_PPC64_GOT16_HA;
11536 break;
11537 case R_PPC64_TOC16_LO_DS:
11538 r_type_ha = R_PPC64_TOC16_HA;
11539 break;
11540 default:
11541 abort ();
11542 }
11543 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11544 r_addend = rel->r_addend;
11545
11546 while (--rel >= relocs)
11547 if (rel->r_info == r_info_ha
11548 && (!match_addend
11549 || rel->r_addend == r_addend))
11550 {
11551 const bfd_byte *p = contents + (rel->r_offset & ~3);
11552 unsigned int insn = bfd_get_32 (input_bfd, p);
11553 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11554 && (insn & (0x1f << 21)) == (*reg << 21))
11555 {
11556 *reg = (insn >> 16) & 0x1f;
11557 return rel;
11558 }
11559 break;
11560 }
11561 return NULL;
11562 }
11563
11564 /* The RELOCATE_SECTION function is called by the ELF backend linker
11565 to handle the relocations for a section.
11566
11567 The relocs are always passed as Rela structures; if the section
11568 actually uses Rel structures, the r_addend field will always be
11569 zero.
11570
11571 This function is responsible for adjust the section contents as
11572 necessary, and (if using Rela relocs and generating a
11573 relocatable output file) adjusting the reloc addend as
11574 necessary.
11575
11576 This function does not have to worry about setting the reloc
11577 address or the reloc symbol index.
11578
11579 LOCAL_SYMS is a pointer to the swapped in local symbols.
11580
11581 LOCAL_SECTIONS is an array giving the section in the input file
11582 corresponding to the st_shndx field of each local symbol.
11583
11584 The global hash table entry for the global symbols can be found
11585 via elf_sym_hashes (input_bfd).
11586
11587 When generating relocatable output, this function must handle
11588 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11589 going to be the section symbol corresponding to the output
11590 section, which means that the addend must be adjusted
11591 accordingly. */
11592
11593 static bfd_boolean
11594 ppc64_elf_relocate_section (bfd *output_bfd,
11595 struct bfd_link_info *info,
11596 bfd *input_bfd,
11597 asection *input_section,
11598 bfd_byte *contents,
11599 Elf_Internal_Rela *relocs,
11600 Elf_Internal_Sym *local_syms,
11601 asection **local_sections)
11602 {
11603 struct ppc_link_hash_table *htab;
11604 Elf_Internal_Shdr *symtab_hdr;
11605 struct elf_link_hash_entry **sym_hashes;
11606 Elf_Internal_Rela *rel;
11607 Elf_Internal_Rela *relend;
11608 Elf_Internal_Rela outrel;
11609 bfd_byte *loc;
11610 struct got_entry **local_got_ents;
11611 unsigned char *ha_opt;
11612 bfd_vma TOCstart;
11613 bfd_boolean no_ha_opt;
11614 bfd_boolean ret = TRUE;
11615 bfd_boolean is_opd;
11616 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
11617 bfd_boolean is_power4 = FALSE;
11618 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
11619
11620 /* Initialize howto table if needed. */
11621 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
11622 ppc_howto_init ();
11623
11624 htab = ppc_hash_table (info);
11625 if (htab == NULL)
11626 return FALSE;
11627
11628 /* Don't relocate stub sections. */
11629 if (input_section->owner == htab->stub_bfd)
11630 return TRUE;
11631
11632 BFD_ASSERT (is_ppc64_elf (input_bfd));
11633
11634 local_got_ents = elf_local_got_ents (input_bfd);
11635 TOCstart = elf_gp (output_bfd);
11636 symtab_hdr = &elf_symtab_hdr (input_bfd);
11637 sym_hashes = elf_sym_hashes (input_bfd);
11638 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
11639 ha_opt = NULL;
11640 no_ha_opt = FALSE;
11641
11642 rel = relocs;
11643 relend = relocs + input_section->reloc_count;
11644 for (; rel < relend; rel++)
11645 {
11646 enum elf_ppc64_reloc_type r_type;
11647 bfd_vma addend, orig_addend;
11648 bfd_reloc_status_type r;
11649 Elf_Internal_Sym *sym;
11650 asection *sec;
11651 struct elf_link_hash_entry *h_elf;
11652 struct ppc_link_hash_entry *h;
11653 struct ppc_link_hash_entry *fdh;
11654 const char *sym_name;
11655 unsigned long r_symndx, toc_symndx;
11656 bfd_vma toc_addend;
11657 unsigned char tls_mask, tls_gd, tls_type;
11658 unsigned char sym_type;
11659 bfd_vma relocation;
11660 bfd_boolean unresolved_reloc;
11661 bfd_boolean warned;
11662 unsigned int insn;
11663 unsigned int mask;
11664 struct ppc_stub_hash_entry *stub_entry;
11665 bfd_vma max_br_offset;
11666 bfd_vma from;
11667
11668 r_type = ELF64_R_TYPE (rel->r_info);
11669 r_symndx = ELF64_R_SYM (rel->r_info);
11670
11671 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11672 symbol of the previous ADDR64 reloc. The symbol gives us the
11673 proper TOC base to use. */
11674 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11675 && rel != relocs
11676 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11677 && is_opd)
11678 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11679
11680 sym = NULL;
11681 sec = NULL;
11682 h_elf = NULL;
11683 sym_name = NULL;
11684 unresolved_reloc = FALSE;
11685 warned = FALSE;
11686 orig_addend = rel->r_addend;
11687
11688 if (r_symndx < symtab_hdr->sh_info)
11689 {
11690 /* It's a local symbol. */
11691 struct _opd_sec_data *opd;
11692
11693 sym = local_syms + r_symndx;
11694 sec = local_sections[r_symndx];
11695 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
11696 sym_type = ELF64_ST_TYPE (sym->st_info);
11697 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
11698 opd = get_opd_info (sec);
11699 if (opd != NULL && opd->adjust != NULL)
11700 {
11701 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
11702 if (adjust == -1)
11703 relocation = 0;
11704 else
11705 {
11706 /* If this is a relocation against the opd section sym
11707 and we have edited .opd, adjust the reloc addend so
11708 that ld -r and ld --emit-relocs output is correct.
11709 If it is a reloc against some other .opd symbol,
11710 then the symbol value will be adjusted later. */
11711 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11712 rel->r_addend += adjust;
11713 else
11714 relocation += adjust;
11715 }
11716 }
11717 }
11718 else
11719 {
11720 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11721 r_symndx, symtab_hdr, sym_hashes,
11722 h_elf, sec, relocation,
11723 unresolved_reloc, warned);
11724 sym_name = h_elf->root.root.string;
11725 sym_type = h_elf->type;
11726 }
11727 h = (struct ppc_link_hash_entry *) h_elf;
11728
11729 if (sec != NULL && elf_discarded_section (sec))
11730 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11731 rel, relend,
11732 ppc64_elf_howto_table[r_type],
11733 contents);
11734
11735 if (info->relocatable)
11736 continue;
11737
11738 /* TLS optimizations. Replace instruction sequences and relocs
11739 based on information we collected in tls_optimize. We edit
11740 RELOCS so that --emit-relocs will output something sensible
11741 for the final instruction stream. */
11742 tls_mask = 0;
11743 tls_gd = 0;
11744 toc_symndx = 0;
11745 if (h != NULL)
11746 tls_mask = h->tls_mask;
11747 else if (local_got_ents != NULL)
11748 {
11749 struct plt_entry **local_plt = (struct plt_entry **)
11750 (local_got_ents + symtab_hdr->sh_info);
11751 unsigned char *lgot_masks = (unsigned char *)
11752 (local_plt + symtab_hdr->sh_info);
11753 tls_mask = lgot_masks[r_symndx];
11754 }
11755 if (tls_mask == 0
11756 && (r_type == R_PPC64_TLS
11757 || r_type == R_PPC64_TLSGD
11758 || r_type == R_PPC64_TLSLD))
11759 {
11760 /* Check for toc tls entries. */
11761 unsigned char *toc_tls;
11762
11763 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11764 &local_syms, rel, input_bfd))
11765 return FALSE;
11766
11767 if (toc_tls)
11768 tls_mask = *toc_tls;
11769 }
11770
11771 /* Check that tls relocs are used with tls syms, and non-tls
11772 relocs are used with non-tls syms. */
11773 if (r_symndx != STN_UNDEF
11774 && r_type != R_PPC64_NONE
11775 && (h == NULL
11776 || h->elf.root.type == bfd_link_hash_defined
11777 || h->elf.root.type == bfd_link_hash_defweak)
11778 && (IS_PPC64_TLS_RELOC (r_type)
11779 != (sym_type == STT_TLS
11780 || (sym_type == STT_SECTION
11781 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
11782 {
11783 if (tls_mask != 0
11784 && (r_type == R_PPC64_TLS
11785 || r_type == R_PPC64_TLSGD
11786 || r_type == R_PPC64_TLSLD))
11787 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11788 ;
11789 else
11790 info->callbacks->einfo
11791 (!IS_PPC64_TLS_RELOC (r_type)
11792 ? _("%H: %s used with TLS symbol %s\n")
11793 : _("%H: %s used with non-TLS symbol %s\n"),
11794 input_bfd, input_section, rel->r_offset,
11795 ppc64_elf_howto_table[r_type]->name,
11796 sym_name);
11797 }
11798
11799 /* Ensure reloc mapping code below stays sane. */
11800 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11801 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11802 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11803 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11804 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11805 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11806 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11807 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11808 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11809 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11810 abort ();
11811
11812 switch (r_type)
11813 {
11814 default:
11815 break;
11816
11817 case R_PPC64_LO_DS_OPT:
11818 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11819 if ((insn & (0x3f << 26)) != 58u << 26)
11820 abort ();
11821 insn += (14u << 26) - (58u << 26);
11822 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11823 r_type = R_PPC64_TOC16_LO;
11824 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11825 break;
11826
11827 case R_PPC64_TOC16:
11828 case R_PPC64_TOC16_LO:
11829 case R_PPC64_TOC16_DS:
11830 case R_PPC64_TOC16_LO_DS:
11831 {
11832 /* Check for toc tls entries. */
11833 unsigned char *toc_tls;
11834 int retval;
11835
11836 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11837 &local_syms, rel, input_bfd);
11838 if (retval == 0)
11839 return FALSE;
11840
11841 if (toc_tls)
11842 {
11843 tls_mask = *toc_tls;
11844 if (r_type == R_PPC64_TOC16_DS
11845 || r_type == R_PPC64_TOC16_LO_DS)
11846 {
11847 if (tls_mask != 0
11848 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11849 goto toctprel;
11850 }
11851 else
11852 {
11853 /* If we found a GD reloc pair, then we might be
11854 doing a GD->IE transition. */
11855 if (retval == 2)
11856 {
11857 tls_gd = TLS_TPRELGD;
11858 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11859 goto tls_ldgd_opt;
11860 }
11861 else if (retval == 3)
11862 {
11863 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11864 goto tls_ldgd_opt;
11865 }
11866 }
11867 }
11868 }
11869 break;
11870
11871 case R_PPC64_GOT_TPREL16_HI:
11872 case R_PPC64_GOT_TPREL16_HA:
11873 if (tls_mask != 0
11874 && (tls_mask & TLS_TPREL) == 0)
11875 {
11876 rel->r_offset -= d_offset;
11877 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11878 r_type = R_PPC64_NONE;
11879 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11880 }
11881 break;
11882
11883 case R_PPC64_GOT_TPREL16_DS:
11884 case R_PPC64_GOT_TPREL16_LO_DS:
11885 if (tls_mask != 0
11886 && (tls_mask & TLS_TPREL) == 0)
11887 {
11888 toctprel:
11889 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11890 insn &= 31 << 21;
11891 insn |= 0x3c0d0000; /* addis 0,13,0 */
11892 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11893 r_type = R_PPC64_TPREL16_HA;
11894 if (toc_symndx != 0)
11895 {
11896 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11897 rel->r_addend = toc_addend;
11898 /* We changed the symbol. Start over in order to
11899 get h, sym, sec etc. right. */
11900 rel--;
11901 continue;
11902 }
11903 else
11904 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11905 }
11906 break;
11907
11908 case R_PPC64_TLS:
11909 if (tls_mask != 0
11910 && (tls_mask & TLS_TPREL) == 0)
11911 {
11912 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
11913 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11914 if (insn == 0)
11915 abort ();
11916 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
11917 /* Was PPC64_TLS which sits on insn boundary, now
11918 PPC64_TPREL16_LO which is at low-order half-word. */
11919 rel->r_offset += d_offset;
11920 r_type = R_PPC64_TPREL16_LO;
11921 if (toc_symndx != 0)
11922 {
11923 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
11924 rel->r_addend = toc_addend;
11925 /* We changed the symbol. Start over in order to
11926 get h, sym, sec etc. right. */
11927 rel--;
11928 continue;
11929 }
11930 else
11931 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11932 }
11933 break;
11934
11935 case R_PPC64_GOT_TLSGD16_HI:
11936 case R_PPC64_GOT_TLSGD16_HA:
11937 tls_gd = TLS_TPRELGD;
11938 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11939 goto tls_gdld_hi;
11940 break;
11941
11942 case R_PPC64_GOT_TLSLD16_HI:
11943 case R_PPC64_GOT_TLSLD16_HA:
11944 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11945 {
11946 tls_gdld_hi:
11947 if ((tls_mask & tls_gd) != 0)
11948 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11949 + R_PPC64_GOT_TPREL16_DS);
11950 else
11951 {
11952 rel->r_offset -= d_offset;
11953 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11954 r_type = R_PPC64_NONE;
11955 }
11956 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11957 }
11958 break;
11959
11960 case R_PPC64_GOT_TLSGD16:
11961 case R_PPC64_GOT_TLSGD16_LO:
11962 tls_gd = TLS_TPRELGD;
11963 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11964 goto tls_ldgd_opt;
11965 break;
11966
11967 case R_PPC64_GOT_TLSLD16:
11968 case R_PPC64_GOT_TLSLD16_LO:
11969 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11970 {
11971 unsigned int insn1, insn2, insn3;
11972 bfd_vma offset;
11973
11974 tls_ldgd_opt:
11975 offset = (bfd_vma) -1;
11976 /* If not using the newer R_PPC64_TLSGD/LD to mark
11977 __tls_get_addr calls, we must trust that the call
11978 stays with its arg setup insns, ie. that the next
11979 reloc is the __tls_get_addr call associated with
11980 the current reloc. Edit both insns. */
11981 if (input_section->has_tls_get_addr_call
11982 && rel + 1 < relend
11983 && branch_reloc_hash_match (input_bfd, rel + 1,
11984 htab->tls_get_addr,
11985 htab->tls_get_addr_fd))
11986 offset = rel[1].r_offset;
11987 if ((tls_mask & tls_gd) != 0)
11988 {
11989 /* IE */
11990 insn1 = bfd_get_32 (output_bfd,
11991 contents + rel->r_offset - d_offset);
11992 insn1 &= (1 << 26) - (1 << 2);
11993 insn1 |= 58 << 26; /* ld */
11994 insn2 = 0x7c636a14; /* add 3,3,13 */
11995 if (offset != (bfd_vma) -1)
11996 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
11997 if ((tls_mask & TLS_EXPLICIT) == 0)
11998 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11999 + R_PPC64_GOT_TPREL16_DS);
12000 else
12001 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12002 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12003 }
12004 else
12005 {
12006 /* LE */
12007 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12008 insn2 = 0x38630000; /* addi 3,3,0 */
12009 if (tls_gd == 0)
12010 {
12011 /* Was an LD reloc. */
12012 if (toc_symndx)
12013 sec = local_sections[toc_symndx];
12014 for (r_symndx = 0;
12015 r_symndx < symtab_hdr->sh_info;
12016 r_symndx++)
12017 if (local_sections[r_symndx] == sec)
12018 break;
12019 if (r_symndx >= symtab_hdr->sh_info)
12020 r_symndx = STN_UNDEF;
12021 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12022 if (r_symndx != STN_UNDEF)
12023 rel->r_addend -= (local_syms[r_symndx].st_value
12024 + sec->output_offset
12025 + sec->output_section->vma);
12026 }
12027 else if (toc_symndx != 0)
12028 {
12029 r_symndx = toc_symndx;
12030 rel->r_addend = toc_addend;
12031 }
12032 r_type = R_PPC64_TPREL16_HA;
12033 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12034 if (offset != (bfd_vma) -1)
12035 {
12036 rel[1].r_info = ELF64_R_INFO (r_symndx,
12037 R_PPC64_TPREL16_LO);
12038 rel[1].r_offset = offset + d_offset;
12039 rel[1].r_addend = rel->r_addend;
12040 }
12041 }
12042 bfd_put_32 (output_bfd, insn1,
12043 contents + rel->r_offset - d_offset);
12044 if (offset != (bfd_vma) -1)
12045 {
12046 insn3 = bfd_get_32 (output_bfd,
12047 contents + offset + 4);
12048 if (insn3 == NOP
12049 || insn3 == CROR_151515 || insn3 == CROR_313131)
12050 {
12051 rel[1].r_offset += 4;
12052 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12053 insn2 = NOP;
12054 }
12055 bfd_put_32 (output_bfd, insn2, contents + offset);
12056 }
12057 if ((tls_mask & tls_gd) == 0
12058 && (tls_gd == 0 || toc_symndx != 0))
12059 {
12060 /* We changed the symbol. Start over in order
12061 to get h, sym, sec etc. right. */
12062 rel--;
12063 continue;
12064 }
12065 }
12066 break;
12067
12068 case R_PPC64_TLSGD:
12069 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12070 {
12071 unsigned int insn2, insn3;
12072 bfd_vma offset = rel->r_offset;
12073
12074 if ((tls_mask & TLS_TPRELGD) != 0)
12075 {
12076 /* IE */
12077 r_type = R_PPC64_NONE;
12078 insn2 = 0x7c636a14; /* add 3,3,13 */
12079 }
12080 else
12081 {
12082 /* LE */
12083 if (toc_symndx != 0)
12084 {
12085 r_symndx = toc_symndx;
12086 rel->r_addend = toc_addend;
12087 }
12088 r_type = R_PPC64_TPREL16_LO;
12089 rel->r_offset = offset + d_offset;
12090 insn2 = 0x38630000; /* addi 3,3,0 */
12091 }
12092 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12093 /* Zap the reloc on the _tls_get_addr call too. */
12094 BFD_ASSERT (offset == rel[1].r_offset);
12095 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12096 insn3 = bfd_get_32 (output_bfd,
12097 contents + offset + 4);
12098 if (insn3 == NOP
12099 || insn3 == CROR_151515 || insn3 == CROR_313131)
12100 {
12101 rel->r_offset += 4;
12102 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12103 insn2 = NOP;
12104 }
12105 bfd_put_32 (output_bfd, insn2, contents + offset);
12106 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
12107 {
12108 rel--;
12109 continue;
12110 }
12111 }
12112 break;
12113
12114 case R_PPC64_TLSLD:
12115 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12116 {
12117 unsigned int insn2, insn3;
12118 bfd_vma offset = rel->r_offset;
12119
12120 if (toc_symndx)
12121 sec = local_sections[toc_symndx];
12122 for (r_symndx = 0;
12123 r_symndx < symtab_hdr->sh_info;
12124 r_symndx++)
12125 if (local_sections[r_symndx] == sec)
12126 break;
12127 if (r_symndx >= symtab_hdr->sh_info)
12128 r_symndx = STN_UNDEF;
12129 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
12130 if (r_symndx != STN_UNDEF)
12131 rel->r_addend -= (local_syms[r_symndx].st_value
12132 + sec->output_offset
12133 + sec->output_section->vma);
12134
12135 r_type = R_PPC64_TPREL16_LO;
12136 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12137 rel->r_offset = offset + d_offset;
12138 /* Zap the reloc on the _tls_get_addr call too. */
12139 BFD_ASSERT (offset == rel[1].r_offset);
12140 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
12141 insn2 = 0x38630000; /* addi 3,3,0 */
12142 insn3 = bfd_get_32 (output_bfd,
12143 contents + offset + 4);
12144 if (insn3 == NOP
12145 || insn3 == CROR_151515 || insn3 == CROR_313131)
12146 {
12147 rel->r_offset += 4;
12148 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12149 insn2 = NOP;
12150 }
12151 bfd_put_32 (output_bfd, insn2, contents + offset);
12152 rel--;
12153 continue;
12154 }
12155 break;
12156
12157 case R_PPC64_DTPMOD64:
12158 if (rel + 1 < relend
12159 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12160 && rel[1].r_offset == rel->r_offset + 8)
12161 {
12162 if ((tls_mask & TLS_GD) == 0)
12163 {
12164 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12165 if ((tls_mask & TLS_TPRELGD) != 0)
12166 r_type = R_PPC64_TPREL64;
12167 else
12168 {
12169 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12170 r_type = R_PPC64_NONE;
12171 }
12172 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12173 }
12174 }
12175 else
12176 {
12177 if ((tls_mask & TLS_LD) == 0)
12178 {
12179 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
12180 r_type = R_PPC64_NONE;
12181 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12182 }
12183 }
12184 break;
12185
12186 case R_PPC64_TPREL64:
12187 if ((tls_mask & TLS_TPREL) == 0)
12188 {
12189 r_type = R_PPC64_NONE;
12190 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12191 }
12192 break;
12193 }
12194
12195 /* Handle other relocations that tweak non-addend part of insn. */
12196 insn = 0;
12197 max_br_offset = 1 << 25;
12198 addend = rel->r_addend;
12199 switch (r_type)
12200 {
12201 default:
12202 break;
12203
12204 /* Branch taken prediction relocations. */
12205 case R_PPC64_ADDR14_BRTAKEN:
12206 case R_PPC64_REL14_BRTAKEN:
12207 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12208 /* Fall thru. */
12209
12210 /* Branch not taken prediction relocations. */
12211 case R_PPC64_ADDR14_BRNTAKEN:
12212 case R_PPC64_REL14_BRNTAKEN:
12213 insn |= bfd_get_32 (output_bfd,
12214 contents + rel->r_offset) & ~(0x01 << 21);
12215 /* Fall thru. */
12216
12217 case R_PPC64_REL14:
12218 max_br_offset = 1 << 15;
12219 /* Fall thru. */
12220
12221 case R_PPC64_REL24:
12222 /* Calls to functions with a different TOC, such as calls to
12223 shared objects, need to alter the TOC pointer. This is
12224 done using a linkage stub. A REL24 branching to these
12225 linkage stubs needs to be followed by a nop, as the nop
12226 will be replaced with an instruction to restore the TOC
12227 base pointer. */
12228 fdh = h;
12229 if (h != NULL
12230 && h->oh != NULL
12231 && h->oh->is_func_descriptor)
12232 fdh = ppc_follow_link (h->oh);
12233 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12234 if (stub_entry != NULL
12235 && (stub_entry->stub_type == ppc_stub_plt_call
12236 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12237 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
12238 {
12239 bfd_boolean can_plt_call = FALSE;
12240
12241 if (rel->r_offset + 8 <= input_section->size)
12242 {
12243 unsigned long nop;
12244 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12245 if (nop == NOP
12246 || nop == CROR_151515 || nop == CROR_313131)
12247 {
12248 if (h != NULL
12249 && (h == htab->tls_get_addr_fd
12250 || h == htab->tls_get_addr)
12251 && !htab->no_tls_get_addr_opt)
12252 {
12253 /* Special stub used, leave nop alone. */
12254 }
12255 else
12256 bfd_put_32 (input_bfd, LD_R2_40R1,
12257 contents + rel->r_offset + 4);
12258 can_plt_call = TRUE;
12259 }
12260 }
12261
12262 if (!can_plt_call)
12263 {
12264 if (stub_entry->stub_type == ppc_stub_plt_call)
12265 {
12266 /* If this is a plain branch rather than a branch
12267 and link, don't require a nop. However, don't
12268 allow tail calls in a shared library as they
12269 will result in r2 being corrupted. */
12270 unsigned long br;
12271 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
12272 if (info->executable && (br & 1) == 0)
12273 can_plt_call = TRUE;
12274 else
12275 stub_entry = NULL;
12276 }
12277 else if (h != NULL
12278 && strcmp (h->elf.root.root.string,
12279 ".__libc_start_main") == 0)
12280 {
12281 /* Allow crt1 branch to go via a toc adjusting stub. */
12282 can_plt_call = TRUE;
12283 }
12284 else
12285 {
12286 if (strcmp (input_section->output_section->name,
12287 ".init") == 0
12288 || strcmp (input_section->output_section->name,
12289 ".fini") == 0)
12290 info->callbacks->einfo
12291 (_("%H: automatic multiple TOCs "
12292 "not supported using your crt files; "
12293 "recompile with -mminimal-toc or upgrade gcc\n"),
12294 input_bfd, input_section, rel->r_offset);
12295 else
12296 info->callbacks->einfo
12297 (_("%H: sibling call optimization to `%s' "
12298 "does not allow automatic multiple TOCs; "
12299 "recompile with -mminimal-toc or "
12300 "-fno-optimize-sibling-calls, "
12301 "or make `%s' extern\n"),
12302 input_bfd, input_section, rel->r_offset,
12303 sym_name,
12304 sym_name);
12305 bfd_set_error (bfd_error_bad_value);
12306 ret = FALSE;
12307 }
12308 }
12309
12310 if (can_plt_call
12311 && stub_entry->stub_type == ppc_stub_plt_call)
12312 unresolved_reloc = FALSE;
12313 }
12314
12315 if ((stub_entry == NULL
12316 || stub_entry->stub_type == ppc_stub_long_branch
12317 || stub_entry->stub_type == ppc_stub_plt_branch)
12318 && get_opd_info (sec) != NULL)
12319 {
12320 /* The branch destination is the value of the opd entry. */
12321 bfd_vma off = (relocation + addend
12322 - sec->output_section->vma
12323 - sec->output_offset);
12324 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12325 if (dest != (bfd_vma) -1)
12326 {
12327 relocation = dest;
12328 addend = 0;
12329 }
12330 }
12331
12332 /* If the branch is out of reach we ought to have a long
12333 branch stub. */
12334 from = (rel->r_offset
12335 + input_section->output_offset
12336 + input_section->output_section->vma);
12337
12338 if (stub_entry != NULL
12339 && (stub_entry->stub_type == ppc_stub_long_branch
12340 || stub_entry->stub_type == ppc_stub_plt_branch)
12341 && (r_type == R_PPC64_ADDR14_BRTAKEN
12342 || r_type == R_PPC64_ADDR14_BRNTAKEN
12343 || (relocation + addend - from + max_br_offset
12344 < 2 * max_br_offset)))
12345 /* Don't use the stub if this branch is in range. */
12346 stub_entry = NULL;
12347
12348 if (stub_entry != NULL)
12349 {
12350 /* Munge up the value and addend so that we call the stub
12351 rather than the procedure directly. */
12352 relocation = (stub_entry->stub_offset
12353 + stub_entry->stub_sec->output_offset
12354 + stub_entry->stub_sec->output_section->vma);
12355 addend = 0;
12356 }
12357
12358 if (insn != 0)
12359 {
12360 if (is_power4)
12361 {
12362 /* Set 'a' bit. This is 0b00010 in BO field for branch
12363 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12364 for branch on CTR insns (BO == 1a00t or 1a01t). */
12365 if ((insn & (0x14 << 21)) == (0x04 << 21))
12366 insn |= 0x02 << 21;
12367 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12368 insn |= 0x08 << 21;
12369 else
12370 break;
12371 }
12372 else
12373 {
12374 /* Invert 'y' bit if not the default. */
12375 if ((bfd_signed_vma) (relocation + addend - from) < 0)
12376 insn ^= 0x01 << 21;
12377 }
12378
12379 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12380 }
12381
12382 /* NOP out calls to undefined weak functions.
12383 We can thus call a weak function without first
12384 checking whether the function is defined. */
12385 else if (h != NULL
12386 && h->elf.root.type == bfd_link_hash_undefweak
12387 && h->elf.dynindx == -1
12388 && r_type == R_PPC64_REL24
12389 && relocation == 0
12390 && addend == 0)
12391 {
12392 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12393 continue;
12394 }
12395 break;
12396 }
12397
12398 /* Set `addend'. */
12399 tls_type = 0;
12400 switch (r_type)
12401 {
12402 default:
12403 info->callbacks->einfo
12404 (_("%B: unknown relocation type %d for symbol %s\n"),
12405 input_bfd, (int) r_type, sym_name);
12406
12407 bfd_set_error (bfd_error_bad_value);
12408 ret = FALSE;
12409 continue;
12410
12411 case R_PPC64_NONE:
12412 case R_PPC64_TLS:
12413 case R_PPC64_TLSGD:
12414 case R_PPC64_TLSLD:
12415 case R_PPC64_GNU_VTINHERIT:
12416 case R_PPC64_GNU_VTENTRY:
12417 continue;
12418
12419 /* GOT16 relocations. Like an ADDR16 using the symbol's
12420 address in the GOT as relocation value instead of the
12421 symbol's value itself. Also, create a GOT entry for the
12422 symbol and put the symbol value there. */
12423 case R_PPC64_GOT_TLSGD16:
12424 case R_PPC64_GOT_TLSGD16_LO:
12425 case R_PPC64_GOT_TLSGD16_HI:
12426 case R_PPC64_GOT_TLSGD16_HA:
12427 tls_type = TLS_TLS | TLS_GD;
12428 goto dogot;
12429
12430 case R_PPC64_GOT_TLSLD16:
12431 case R_PPC64_GOT_TLSLD16_LO:
12432 case R_PPC64_GOT_TLSLD16_HI:
12433 case R_PPC64_GOT_TLSLD16_HA:
12434 tls_type = TLS_TLS | TLS_LD;
12435 goto dogot;
12436
12437 case R_PPC64_GOT_TPREL16_DS:
12438 case R_PPC64_GOT_TPREL16_LO_DS:
12439 case R_PPC64_GOT_TPREL16_HI:
12440 case R_PPC64_GOT_TPREL16_HA:
12441 tls_type = TLS_TLS | TLS_TPREL;
12442 goto dogot;
12443
12444 case R_PPC64_GOT_DTPREL16_DS:
12445 case R_PPC64_GOT_DTPREL16_LO_DS:
12446 case R_PPC64_GOT_DTPREL16_HI:
12447 case R_PPC64_GOT_DTPREL16_HA:
12448 tls_type = TLS_TLS | TLS_DTPREL;
12449 goto dogot;
12450
12451 case R_PPC64_GOT16:
12452 case R_PPC64_GOT16_LO:
12453 case R_PPC64_GOT16_HI:
12454 case R_PPC64_GOT16_HA:
12455 case R_PPC64_GOT16_DS:
12456 case R_PPC64_GOT16_LO_DS:
12457 dogot:
12458 {
12459 /* Relocation is to the entry for this symbol in the global
12460 offset table. */
12461 asection *got;
12462 bfd_vma *offp;
12463 bfd_vma off;
12464 unsigned long indx = 0;
12465 struct got_entry *ent;
12466
12467 if (tls_type == (TLS_TLS | TLS_LD)
12468 && (h == NULL
12469 || !h->elf.def_dynamic))
12470 ent = ppc64_tlsld_got (input_bfd);
12471 else
12472 {
12473
12474 if (h != NULL)
12475 {
12476 bfd_boolean dyn = htab->elf.dynamic_sections_created;
12477 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12478 &h->elf)
12479 || (info->shared
12480 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
12481 /* This is actually a static link, or it is a
12482 -Bsymbolic link and the symbol is defined
12483 locally, or the symbol was forced to be local
12484 because of a version file. */
12485 ;
12486 else
12487 {
12488 indx = h->elf.dynindx;
12489 unresolved_reloc = FALSE;
12490 }
12491 ent = h->elf.got.glist;
12492 }
12493 else
12494 {
12495 if (local_got_ents == NULL)
12496 abort ();
12497 ent = local_got_ents[r_symndx];
12498 }
12499
12500 for (; ent != NULL; ent = ent->next)
12501 if (ent->addend == orig_addend
12502 && ent->owner == input_bfd
12503 && ent->tls_type == tls_type)
12504 break;
12505 }
12506
12507 if (ent == NULL)
12508 abort ();
12509 if (ent->is_indirect)
12510 ent = ent->got.ent;
12511 offp = &ent->got.offset;
12512 got = ppc64_elf_tdata (ent->owner)->got;
12513 if (got == NULL)
12514 abort ();
12515
12516 /* The offset must always be a multiple of 8. We use the
12517 least significant bit to record whether we have already
12518 processed this entry. */
12519 off = *offp;
12520 if ((off & 1) != 0)
12521 off &= ~1;
12522 else
12523 {
12524 /* Generate relocs for the dynamic linker, except in
12525 the case of TLSLD where we'll use one entry per
12526 module. */
12527 asection *relgot;
12528 bfd_boolean ifunc;
12529
12530 *offp = off | 1;
12531 relgot = NULL;
12532 ifunc = (h != NULL
12533 ? h->elf.type == STT_GNU_IFUNC
12534 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
12535 if ((info->shared || indx != 0)
12536 && (h == NULL
12537 || (tls_type == (TLS_TLS | TLS_LD)
12538 && !h->elf.def_dynamic)
12539 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12540 || h->elf.root.type != bfd_link_hash_undefweak))
12541 relgot = ppc64_elf_tdata (ent->owner)->relgot;
12542 else if (ifunc)
12543 relgot = htab->reliplt;
12544 if (relgot != NULL)
12545 {
12546 outrel.r_offset = (got->output_section->vma
12547 + got->output_offset
12548 + off);
12549 outrel.r_addend = addend;
12550 if (tls_type & (TLS_LD | TLS_GD))
12551 {
12552 outrel.r_addend = 0;
12553 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
12554 if (tls_type == (TLS_TLS | TLS_GD))
12555 {
12556 loc = relgot->contents;
12557 loc += (relgot->reloc_count++
12558 * sizeof (Elf64_External_Rela));
12559 bfd_elf64_swap_reloca_out (output_bfd,
12560 &outrel, loc);
12561 outrel.r_offset += 8;
12562 outrel.r_addend = addend;
12563 outrel.r_info
12564 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12565 }
12566 }
12567 else if (tls_type == (TLS_TLS | TLS_DTPREL))
12568 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
12569 else if (tls_type == (TLS_TLS | TLS_TPREL))
12570 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
12571 else if (indx != 0)
12572 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12573 else
12574 {
12575 if (ifunc)
12576 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12577 else
12578 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12579
12580 /* Write the .got section contents for the sake
12581 of prelink. */
12582 loc = got->contents + off;
12583 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12584 loc);
12585 }
12586
12587 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
12588 {
12589 outrel.r_addend += relocation;
12590 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
12591 outrel.r_addend -= htab->elf.tls_sec->vma;
12592 }
12593 loc = relgot->contents;
12594 loc += (relgot->reloc_count++
12595 * sizeof (Elf64_External_Rela));
12596 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12597 }
12598
12599 /* Init the .got section contents here if we're not
12600 emitting a reloc. */
12601 else
12602 {
12603 relocation += addend;
12604 if (tls_type == (TLS_TLS | TLS_LD))
12605 relocation = 1;
12606 else if (tls_type != 0)
12607 {
12608 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
12609 if (tls_type == (TLS_TLS | TLS_TPREL))
12610 relocation += DTP_OFFSET - TP_OFFSET;
12611
12612 if (tls_type == (TLS_TLS | TLS_GD))
12613 {
12614 bfd_put_64 (output_bfd, relocation,
12615 got->contents + off + 8);
12616 relocation = 1;
12617 }
12618 }
12619
12620 bfd_put_64 (output_bfd, relocation,
12621 got->contents + off);
12622 }
12623 }
12624
12625 if (off >= (bfd_vma) -2)
12626 abort ();
12627
12628 relocation = got->output_section->vma + got->output_offset + off;
12629 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
12630 }
12631 break;
12632
12633 case R_PPC64_PLT16_HA:
12634 case R_PPC64_PLT16_HI:
12635 case R_PPC64_PLT16_LO:
12636 case R_PPC64_PLT32:
12637 case R_PPC64_PLT64:
12638 /* Relocation is to the entry for this symbol in the
12639 procedure linkage table. */
12640
12641 /* Resolve a PLT reloc against a local symbol directly,
12642 without using the procedure linkage table. */
12643 if (h == NULL)
12644 break;
12645
12646 /* It's possible that we didn't make a PLT entry for this
12647 symbol. This happens when statically linking PIC code,
12648 or when using -Bsymbolic. Go find a match if there is a
12649 PLT entry. */
12650 if (htab->plt != NULL)
12651 {
12652 struct plt_entry *ent;
12653 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
12654 if (ent->addend == orig_addend
12655 && ent->plt.offset != (bfd_vma) -1)
12656 {
12657 relocation = (htab->plt->output_section->vma
12658 + htab->plt->output_offset
12659 + ent->plt.offset);
12660 unresolved_reloc = FALSE;
12661 }
12662 }
12663 break;
12664
12665 case R_PPC64_TOC:
12666 /* Relocation value is TOC base. */
12667 relocation = TOCstart;
12668 if (r_symndx == STN_UNDEF)
12669 relocation += htab->stub_group[input_section->id].toc_off;
12670 else if (unresolved_reloc)
12671 ;
12672 else if (sec != NULL && sec->id <= htab->top_id)
12673 relocation += htab->stub_group[sec->id].toc_off;
12674 else
12675 unresolved_reloc = TRUE;
12676 goto dodyn;
12677
12678 /* TOC16 relocs. We want the offset relative to the TOC base,
12679 which is the address of the start of the TOC plus 0x8000.
12680 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12681 in this order. */
12682 case R_PPC64_TOC16:
12683 case R_PPC64_TOC16_LO:
12684 case R_PPC64_TOC16_HI:
12685 case R_PPC64_TOC16_DS:
12686 case R_PPC64_TOC16_LO_DS:
12687 case R_PPC64_TOC16_HA:
12688 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
12689 break;
12690
12691 /* Relocate against the beginning of the section. */
12692 case R_PPC64_SECTOFF:
12693 case R_PPC64_SECTOFF_LO:
12694 case R_PPC64_SECTOFF_HI:
12695 case R_PPC64_SECTOFF_DS:
12696 case R_PPC64_SECTOFF_LO_DS:
12697 case R_PPC64_SECTOFF_HA:
12698 if (sec != NULL)
12699 addend -= sec->output_section->vma;
12700 break;
12701
12702 case R_PPC64_REL16:
12703 case R_PPC64_REL16_LO:
12704 case R_PPC64_REL16_HI:
12705 case R_PPC64_REL16_HA:
12706 break;
12707
12708 case R_PPC64_REL14:
12709 case R_PPC64_REL14_BRNTAKEN:
12710 case R_PPC64_REL14_BRTAKEN:
12711 case R_PPC64_REL24:
12712 break;
12713
12714 case R_PPC64_TPREL16:
12715 case R_PPC64_TPREL16_LO:
12716 case R_PPC64_TPREL16_HI:
12717 case R_PPC64_TPREL16_HA:
12718 case R_PPC64_TPREL16_DS:
12719 case R_PPC64_TPREL16_LO_DS:
12720 case R_PPC64_TPREL16_HIGHER:
12721 case R_PPC64_TPREL16_HIGHERA:
12722 case R_PPC64_TPREL16_HIGHEST:
12723 case R_PPC64_TPREL16_HIGHESTA:
12724 if (h != NULL
12725 && h->elf.root.type == bfd_link_hash_undefweak
12726 && h->elf.dynindx == -1)
12727 {
12728 /* Make this relocation against an undefined weak symbol
12729 resolve to zero. This is really just a tweak, since
12730 code using weak externs ought to check that they are
12731 defined before using them. */
12732 bfd_byte *p = contents + rel->r_offset - d_offset;
12733
12734 insn = bfd_get_32 (output_bfd, p);
12735 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12736 if (insn != 0)
12737 bfd_put_32 (output_bfd, insn, p);
12738 break;
12739 }
12740 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12741 if (info->shared)
12742 /* The TPREL16 relocs shouldn't really be used in shared
12743 libs as they will result in DT_TEXTREL being set, but
12744 support them anyway. */
12745 goto dodyn;
12746 break;
12747
12748 case R_PPC64_DTPREL16:
12749 case R_PPC64_DTPREL16_LO:
12750 case R_PPC64_DTPREL16_HI:
12751 case R_PPC64_DTPREL16_HA:
12752 case R_PPC64_DTPREL16_DS:
12753 case R_PPC64_DTPREL16_LO_DS:
12754 case R_PPC64_DTPREL16_HIGHER:
12755 case R_PPC64_DTPREL16_HIGHERA:
12756 case R_PPC64_DTPREL16_HIGHEST:
12757 case R_PPC64_DTPREL16_HIGHESTA:
12758 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12759 break;
12760
12761 case R_PPC64_DTPMOD64:
12762 relocation = 1;
12763 addend = 0;
12764 goto dodyn;
12765
12766 case R_PPC64_TPREL64:
12767 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
12768 goto dodyn;
12769
12770 case R_PPC64_DTPREL64:
12771 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
12772 /* Fall thru */
12773
12774 /* Relocations that may need to be propagated if this is a
12775 dynamic object. */
12776 case R_PPC64_REL30:
12777 case R_PPC64_REL32:
12778 case R_PPC64_REL64:
12779 case R_PPC64_ADDR14:
12780 case R_PPC64_ADDR14_BRNTAKEN:
12781 case R_PPC64_ADDR14_BRTAKEN:
12782 case R_PPC64_ADDR16:
12783 case R_PPC64_ADDR16_DS:
12784 case R_PPC64_ADDR16_HA:
12785 case R_PPC64_ADDR16_HI:
12786 case R_PPC64_ADDR16_HIGHER:
12787 case R_PPC64_ADDR16_HIGHERA:
12788 case R_PPC64_ADDR16_HIGHEST:
12789 case R_PPC64_ADDR16_HIGHESTA:
12790 case R_PPC64_ADDR16_LO:
12791 case R_PPC64_ADDR16_LO_DS:
12792 case R_PPC64_ADDR24:
12793 case R_PPC64_ADDR32:
12794 case R_PPC64_ADDR64:
12795 case R_PPC64_UADDR16:
12796 case R_PPC64_UADDR32:
12797 case R_PPC64_UADDR64:
12798 dodyn:
12799 if ((input_section->flags & SEC_ALLOC) == 0)
12800 break;
12801
12802 if (NO_OPD_RELOCS && is_opd)
12803 break;
12804
12805 if ((info->shared
12806 && (h == NULL
12807 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12808 || h->elf.root.type != bfd_link_hash_undefweak)
12809 && (must_be_dyn_reloc (info, r_type)
12810 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
12811 || (ELIMINATE_COPY_RELOCS
12812 && !info->shared
12813 && h != NULL
12814 && h->elf.dynindx != -1
12815 && !h->elf.non_got_ref
12816 && !h->elf.def_regular)
12817 || (!info->shared
12818 && (h != NULL
12819 ? h->elf.type == STT_GNU_IFUNC
12820 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
12821 {
12822 bfd_boolean skip, relocate;
12823 asection *sreloc;
12824 bfd_vma out_off;
12825
12826 /* When generating a dynamic object, these relocations
12827 are copied into the output file to be resolved at run
12828 time. */
12829
12830 skip = FALSE;
12831 relocate = FALSE;
12832
12833 out_off = _bfd_elf_section_offset (output_bfd, info,
12834 input_section, rel->r_offset);
12835 if (out_off == (bfd_vma) -1)
12836 skip = TRUE;
12837 else if (out_off == (bfd_vma) -2)
12838 skip = TRUE, relocate = TRUE;
12839 out_off += (input_section->output_section->vma
12840 + input_section->output_offset);
12841 outrel.r_offset = out_off;
12842 outrel.r_addend = rel->r_addend;
12843
12844 /* Optimize unaligned reloc use. */
12845 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12846 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12847 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12848 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12849 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12850 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12851 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12852 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12853 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12854
12855 if (skip)
12856 memset (&outrel, 0, sizeof outrel);
12857 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
12858 && !is_opd
12859 && r_type != R_PPC64_TOC)
12860 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
12861 else
12862 {
12863 /* This symbol is local, or marked to become local,
12864 or this is an opd section reloc which must point
12865 at a local function. */
12866 outrel.r_addend += relocation;
12867 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
12868 {
12869 if (is_opd && h != NULL)
12870 {
12871 /* Lie about opd entries. This case occurs
12872 when building shared libraries and we
12873 reference a function in another shared
12874 lib. The same thing happens for a weak
12875 definition in an application that's
12876 overridden by a strong definition in a
12877 shared lib. (I believe this is a generic
12878 bug in binutils handling of weak syms.)
12879 In these cases we won't use the opd
12880 entry in this lib. */
12881 unresolved_reloc = FALSE;
12882 }
12883 if (!is_opd
12884 && r_type == R_PPC64_ADDR64
12885 && (h != NULL
12886 ? h->elf.type == STT_GNU_IFUNC
12887 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12888 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12889 else
12890 {
12891 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
12892
12893 /* We need to relocate .opd contents for ld.so.
12894 Prelink also wants simple and consistent rules
12895 for relocs. This make all RELATIVE relocs have
12896 *r_offset equal to r_addend. */
12897 relocate = TRUE;
12898 }
12899 }
12900 else
12901 {
12902 long indx = 0;
12903
12904 if (h != NULL
12905 ? h->elf.type == STT_GNU_IFUNC
12906 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12907 {
12908 info->callbacks->einfo
12909 (_("%H: relocation %s for indirect "
12910 "function %s unsupported\n"),
12911 input_bfd, input_section, rel->r_offset,
12912 ppc64_elf_howto_table[r_type]->name,
12913 sym_name);
12914 ret = FALSE;
12915 }
12916 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
12917 ;
12918 else if (sec == NULL || sec->owner == NULL)
12919 {
12920 bfd_set_error (bfd_error_bad_value);
12921 return FALSE;
12922 }
12923 else
12924 {
12925 asection *osec;
12926
12927 osec = sec->output_section;
12928 indx = elf_section_data (osec)->dynindx;
12929
12930 if (indx == 0)
12931 {
12932 if ((osec->flags & SEC_READONLY) == 0
12933 && htab->elf.data_index_section != NULL)
12934 osec = htab->elf.data_index_section;
12935 else
12936 osec = htab->elf.text_index_section;
12937 indx = elf_section_data (osec)->dynindx;
12938 }
12939 BFD_ASSERT (indx != 0);
12940
12941 /* We are turning this relocation into one
12942 against a section symbol, so subtract out
12943 the output section's address but not the
12944 offset of the input section in the output
12945 section. */
12946 outrel.r_addend -= osec->vma;
12947 }
12948
12949 outrel.r_info = ELF64_R_INFO (indx, r_type);
12950 }
12951 }
12952
12953 sreloc = elf_section_data (input_section)->sreloc;
12954 if (!htab->elf.dynamic_sections_created)
12955 sreloc = htab->reliplt;
12956 if (sreloc == NULL)
12957 abort ();
12958
12959 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12960 >= sreloc->size)
12961 abort ();
12962 loc = sreloc->contents;
12963 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
12964 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12965
12966 /* If this reloc is against an external symbol, it will
12967 be computed at runtime, so there's no need to do
12968 anything now. However, for the sake of prelink ensure
12969 that the section contents are a known value. */
12970 if (! relocate)
12971 {
12972 unresolved_reloc = FALSE;
12973 /* The value chosen here is quite arbitrary as ld.so
12974 ignores section contents except for the special
12975 case of .opd where the contents might be accessed
12976 before relocation. Choose zero, as that won't
12977 cause reloc overflow. */
12978 relocation = 0;
12979 addend = 0;
12980 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12981 to improve backward compatibility with older
12982 versions of ld. */
12983 if (r_type == R_PPC64_ADDR64)
12984 addend = outrel.r_addend;
12985 /* Adjust pc_relative relocs to have zero in *r_offset. */
12986 else if (ppc64_elf_howto_table[r_type]->pc_relative)
12987 addend = (input_section->output_section->vma
12988 + input_section->output_offset
12989 + rel->r_offset);
12990 }
12991 }
12992 break;
12993
12994 case R_PPC64_COPY:
12995 case R_PPC64_GLOB_DAT:
12996 case R_PPC64_JMP_SLOT:
12997 case R_PPC64_JMP_IREL:
12998 case R_PPC64_RELATIVE:
12999 /* We shouldn't ever see these dynamic relocs in relocatable
13000 files. */
13001 /* Fall through. */
13002
13003 case R_PPC64_PLTGOT16:
13004 case R_PPC64_PLTGOT16_DS:
13005 case R_PPC64_PLTGOT16_HA:
13006 case R_PPC64_PLTGOT16_HI:
13007 case R_PPC64_PLTGOT16_LO:
13008 case R_PPC64_PLTGOT16_LO_DS:
13009 case R_PPC64_PLTREL32:
13010 case R_PPC64_PLTREL64:
13011 /* These ones haven't been implemented yet. */
13012
13013 info->callbacks->einfo
13014 (_("%B: relocation %s is not supported for symbol %s\n"),
13015 input_bfd,
13016 ppc64_elf_howto_table[r_type]->name, sym_name);
13017
13018 bfd_set_error (bfd_error_invalid_operation);
13019 ret = FALSE;
13020 continue;
13021 }
13022
13023 /* Multi-instruction sequences that access the TOC can be
13024 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13025 to nop; addi rb,r2,x; */
13026 switch (r_type)
13027 {
13028 default:
13029 break;
13030
13031 case R_PPC64_GOT_TLSLD16_HI:
13032 case R_PPC64_GOT_TLSGD16_HI:
13033 case R_PPC64_GOT_TPREL16_HI:
13034 case R_PPC64_GOT_DTPREL16_HI:
13035 case R_PPC64_GOT16_HI:
13036 case R_PPC64_TOC16_HI:
13037 /* These relocs would only be useful if building up an
13038 offset to later add to r2, perhaps in an indexed
13039 addressing mode instruction. Don't try to optimize.
13040 Unfortunately, the possibility of someone building up an
13041 offset like this or even with the HA relocs, means that
13042 we need to check the high insn when optimizing the low
13043 insn. */
13044 break;
13045
13046 case R_PPC64_GOT_TLSLD16_HA:
13047 case R_PPC64_GOT_TLSGD16_HA:
13048 case R_PPC64_GOT_TPREL16_HA:
13049 case R_PPC64_GOT_DTPREL16_HA:
13050 case R_PPC64_GOT16_HA:
13051 case R_PPC64_TOC16_HA:
13052 /* nop is done later. */
13053 break;
13054
13055 case R_PPC64_GOT_TLSLD16_LO:
13056 case R_PPC64_GOT_TLSGD16_LO:
13057 case R_PPC64_GOT_TPREL16_LO_DS:
13058 case R_PPC64_GOT_DTPREL16_LO_DS:
13059 case R_PPC64_GOT16_LO:
13060 case R_PPC64_GOT16_LO_DS:
13061 case R_PPC64_TOC16_LO:
13062 case R_PPC64_TOC16_LO_DS:
13063 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13064 {
13065 bfd_byte *p = contents + (rel->r_offset & ~3);
13066 insn = bfd_get_32 (input_bfd, p);
13067 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13068 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13069 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13070 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13071 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13072 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13073 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13074 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13075 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13076 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13077 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13078 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13079 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13080 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13081 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13082 && (insn & 3) != 1)
13083 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13084 && ((insn & 3) == 0 || (insn & 3) == 3)))
13085 {
13086 unsigned int reg = (insn >> 16) & 0x1f;
13087 const Elf_Internal_Rela *ha;
13088 bfd_boolean match_addend;
13089
13090 match_addend = (sym != NULL
13091 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13092 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13093 input_bfd, contents);
13094 if (ha != NULL)
13095 {
13096 insn &= ~(0x1f << 16);
13097 insn |= reg << 16;
13098 bfd_put_32 (input_bfd, insn, p);
13099 if (ha_opt == NULL)
13100 {
13101 ha_opt = bfd_zmalloc (input_section->reloc_count);
13102 if (ha_opt == NULL)
13103 return FALSE;
13104 }
13105 ha_opt[ha - relocs] = 1;
13106 }
13107 else
13108 /* If we don't find a matching high part insn,
13109 something is fishy. Refuse to nop any high
13110 part insn in this section. */
13111 no_ha_opt = TRUE;
13112 }
13113 }
13114 break;
13115 }
13116
13117 /* Do any further special processing. */
13118 switch (r_type)
13119 {
13120 default:
13121 break;
13122
13123 case R_PPC64_ADDR16_HA:
13124 case R_PPC64_REL16_HA:
13125 case R_PPC64_ADDR16_HIGHERA:
13126 case R_PPC64_ADDR16_HIGHESTA:
13127 case R_PPC64_TOC16_HA:
13128 case R_PPC64_SECTOFF_HA:
13129 case R_PPC64_TPREL16_HA:
13130 case R_PPC64_DTPREL16_HA:
13131 case R_PPC64_TPREL16_HIGHER:
13132 case R_PPC64_TPREL16_HIGHERA:
13133 case R_PPC64_TPREL16_HIGHEST:
13134 case R_PPC64_TPREL16_HIGHESTA:
13135 case R_PPC64_DTPREL16_HIGHER:
13136 case R_PPC64_DTPREL16_HIGHERA:
13137 case R_PPC64_DTPREL16_HIGHEST:
13138 case R_PPC64_DTPREL16_HIGHESTA:
13139 /* It's just possible that this symbol is a weak symbol
13140 that's not actually defined anywhere. In that case,
13141 'sec' would be NULL, and we should leave the symbol
13142 alone (it will be set to zero elsewhere in the link). */
13143 if (sec == NULL)
13144 break;
13145 /* Fall thru */
13146
13147 case R_PPC64_GOT16_HA:
13148 case R_PPC64_PLTGOT16_HA:
13149 case R_PPC64_PLT16_HA:
13150 case R_PPC64_GOT_TLSGD16_HA:
13151 case R_PPC64_GOT_TLSLD16_HA:
13152 case R_PPC64_GOT_TPREL16_HA:
13153 case R_PPC64_GOT_DTPREL16_HA:
13154 /* Add 0x10000 if sign bit in 0:15 is set.
13155 Bits 0:15 are not used. */
13156 addend += 0x8000;
13157 break;
13158
13159 case R_PPC64_ADDR16_DS:
13160 case R_PPC64_ADDR16_LO_DS:
13161 case R_PPC64_GOT16_DS:
13162 case R_PPC64_GOT16_LO_DS:
13163 case R_PPC64_PLT16_LO_DS:
13164 case R_PPC64_SECTOFF_DS:
13165 case R_PPC64_SECTOFF_LO_DS:
13166 case R_PPC64_TOC16_DS:
13167 case R_PPC64_TOC16_LO_DS:
13168 case R_PPC64_PLTGOT16_DS:
13169 case R_PPC64_PLTGOT16_LO_DS:
13170 case R_PPC64_GOT_TPREL16_DS:
13171 case R_PPC64_GOT_TPREL16_LO_DS:
13172 case R_PPC64_GOT_DTPREL16_DS:
13173 case R_PPC64_GOT_DTPREL16_LO_DS:
13174 case R_PPC64_TPREL16_DS:
13175 case R_PPC64_TPREL16_LO_DS:
13176 case R_PPC64_DTPREL16_DS:
13177 case R_PPC64_DTPREL16_LO_DS:
13178 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13179 mask = 3;
13180 /* If this reloc is against an lq insn, then the value must be
13181 a multiple of 16. This is somewhat of a hack, but the
13182 "correct" way to do this by defining _DQ forms of all the
13183 _DS relocs bloats all reloc switches in this file. It
13184 doesn't seem to make much sense to use any of these relocs
13185 in data, so testing the insn should be safe. */
13186 if ((insn & (0x3f << 26)) == (56u << 26))
13187 mask = 15;
13188 if (((relocation + addend) & mask) != 0)
13189 {
13190 info->callbacks->einfo
13191 (_("%H: error: %s not a multiple of %u\n"),
13192 input_bfd, input_section, rel->r_offset,
13193 ppc64_elf_howto_table[r_type]->name,
13194 mask + 1);
13195 bfd_set_error (bfd_error_bad_value);
13196 ret = FALSE;
13197 continue;
13198 }
13199 break;
13200 }
13201
13202 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13203 because such sections are not SEC_ALLOC and thus ld.so will
13204 not process them. */
13205 if (unresolved_reloc
13206 && !((input_section->flags & SEC_DEBUGGING) != 0
13207 && h->elf.def_dynamic))
13208 {
13209 info->callbacks->einfo
13210 (_("%H: unresolvable %s relocation against symbol `%s'\n"),
13211 input_bfd, input_section, rel->r_offset,
13212 ppc64_elf_howto_table[(int) r_type]->name,
13213 h->elf.root.root.string);
13214 ret = FALSE;
13215 }
13216
13217 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
13218 input_bfd,
13219 input_section,
13220 contents,
13221 rel->r_offset,
13222 relocation,
13223 addend);
13224
13225 if (r != bfd_reloc_ok)
13226 {
13227 if (sym_name == NULL)
13228 sym_name = "(null)";
13229 if (r == bfd_reloc_overflow)
13230 {
13231 if (warned)
13232 continue;
13233 if (h != NULL
13234 && h->elf.root.type == bfd_link_hash_undefweak
13235 && ppc64_elf_howto_table[r_type]->pc_relative)
13236 {
13237 /* Assume this is a call protected by other code that
13238 detects the symbol is undefined. If this is the case,
13239 we can safely ignore the overflow. If not, the
13240 program is hosed anyway, and a little warning isn't
13241 going to help. */
13242
13243 continue;
13244 }
13245
13246 if (!((*info->callbacks->reloc_overflow)
13247 (info, (h ? &h->elf.root : NULL), sym_name,
13248 ppc64_elf_howto_table[r_type]->name,
13249 orig_addend, input_bfd, input_section, rel->r_offset)))
13250 return FALSE;
13251 }
13252 else
13253 {
13254 info->callbacks->einfo
13255 (_("%H: %s reloc against `%s': error %d\n"),
13256 input_bfd, input_section, rel->r_offset,
13257 ppc64_elf_howto_table[r_type]->name,
13258 sym_name,
13259 (int) r);
13260 ret = FALSE;
13261 }
13262 }
13263 }
13264
13265 if (ha_opt != NULL)
13266 {
13267 if (!no_ha_opt)
13268 {
13269 unsigned char *opt = ha_opt;
13270 rel = relocs;
13271 relend = relocs + input_section->reloc_count;
13272 for (; rel < relend; opt++, rel++)
13273 if (*opt != 0)
13274 {
13275 bfd_byte *p = contents + (rel->r_offset & ~3);
13276 bfd_put_32 (input_bfd, NOP, p);
13277 }
13278 }
13279 free (ha_opt);
13280 }
13281
13282 /* If we're emitting relocations, then shortly after this function
13283 returns, reloc offsets and addends for this section will be
13284 adjusted. Worse, reloc symbol indices will be for the output
13285 file rather than the input. Save a copy of the relocs for
13286 opd_entry_value. */
13287 if (is_opd && (info->emitrelocations || info->relocatable))
13288 {
13289 bfd_size_type amt;
13290 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13291 rel = bfd_alloc (input_bfd, amt);
13292 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13293 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13294 if (rel == NULL)
13295 return FALSE;
13296 memcpy (rel, relocs, amt);
13297 }
13298 return ret;
13299 }
13300
13301 /* Adjust the value of any local symbols in opd sections. */
13302
13303 static int
13304 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13305 const char *name ATTRIBUTE_UNUSED,
13306 Elf_Internal_Sym *elfsym,
13307 asection *input_sec,
13308 struct elf_link_hash_entry *h)
13309 {
13310 struct _opd_sec_data *opd;
13311 long adjust;
13312 bfd_vma value;
13313
13314 if (h != NULL)
13315 return 1;
13316
13317 opd = get_opd_info (input_sec);
13318 if (opd == NULL || opd->adjust == NULL)
13319 return 1;
13320
13321 value = elfsym->st_value - input_sec->output_offset;
13322 if (!info->relocatable)
13323 value -= input_sec->output_section->vma;
13324
13325 adjust = opd->adjust[value / 8];
13326 if (adjust == -1)
13327 return 2;
13328
13329 elfsym->st_value += adjust;
13330 return 1;
13331 }
13332
13333 /* Finish up dynamic symbol handling. We set the contents of various
13334 dynamic sections here. */
13335
13336 static bfd_boolean
13337 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13338 struct bfd_link_info *info,
13339 struct elf_link_hash_entry *h,
13340 Elf_Internal_Sym *sym)
13341 {
13342 struct ppc_link_hash_table *htab;
13343 struct plt_entry *ent;
13344 Elf_Internal_Rela rela;
13345 bfd_byte *loc;
13346
13347 htab = ppc_hash_table (info);
13348 if (htab == NULL)
13349 return FALSE;
13350
13351 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13352 if (ent->plt.offset != (bfd_vma) -1)
13353 {
13354 /* This symbol has an entry in the procedure linkage
13355 table. Set it up. */
13356 if (!htab->elf.dynamic_sections_created
13357 || h->dynindx == -1)
13358 {
13359 BFD_ASSERT (h->type == STT_GNU_IFUNC
13360 && h->def_regular
13361 && (h->root.type == bfd_link_hash_defined
13362 || h->root.type == bfd_link_hash_defweak));
13363 rela.r_offset = (htab->iplt->output_section->vma
13364 + htab->iplt->output_offset
13365 + ent->plt.offset);
13366 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13367 rela.r_addend = (h->root.u.def.value
13368 + h->root.u.def.section->output_offset
13369 + h->root.u.def.section->output_section->vma
13370 + ent->addend);
13371 loc = (htab->reliplt->contents
13372 + (htab->reliplt->reloc_count++
13373 * sizeof (Elf64_External_Rela)));
13374 }
13375 else
13376 {
13377 rela.r_offset = (htab->plt->output_section->vma
13378 + htab->plt->output_offset
13379 + ent->plt.offset);
13380 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13381 rela.r_addend = ent->addend;
13382 loc = (htab->relplt->contents
13383 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13384 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
13385 }
13386 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13387 }
13388
13389 if (h->needs_copy)
13390 {
13391 /* This symbol needs a copy reloc. Set it up. */
13392
13393 if (h->dynindx == -1
13394 || (h->root.type != bfd_link_hash_defined
13395 && h->root.type != bfd_link_hash_defweak)
13396 || htab->relbss == NULL)
13397 abort ();
13398
13399 rela.r_offset = (h->root.u.def.value
13400 + h->root.u.def.section->output_section->vma
13401 + h->root.u.def.section->output_offset);
13402 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13403 rela.r_addend = 0;
13404 loc = htab->relbss->contents;
13405 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
13406 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13407 }
13408
13409 /* Mark some specially defined symbols as absolute. */
13410 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13411 sym->st_shndx = SHN_ABS;
13412
13413 return TRUE;
13414 }
13415
13416 /* Used to decide how to sort relocs in an optimal manner for the
13417 dynamic linker, before writing them out. */
13418
13419 static enum elf_reloc_type_class
13420 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
13421 {
13422 enum elf_ppc64_reloc_type r_type;
13423
13424 r_type = ELF64_R_TYPE (rela->r_info);
13425 switch (r_type)
13426 {
13427 case R_PPC64_RELATIVE:
13428 return reloc_class_relative;
13429 case R_PPC64_JMP_SLOT:
13430 return reloc_class_plt;
13431 case R_PPC64_COPY:
13432 return reloc_class_copy;
13433 default:
13434 return reloc_class_normal;
13435 }
13436 }
13437
13438 /* Finish up the dynamic sections. */
13439
13440 static bfd_boolean
13441 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13442 struct bfd_link_info *info)
13443 {
13444 struct ppc_link_hash_table *htab;
13445 bfd *dynobj;
13446 asection *sdyn;
13447
13448 htab = ppc_hash_table (info);
13449 if (htab == NULL)
13450 return FALSE;
13451
13452 dynobj = htab->elf.dynobj;
13453 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13454
13455 if (htab->elf.dynamic_sections_created)
13456 {
13457 Elf64_External_Dyn *dyncon, *dynconend;
13458
13459 if (sdyn == NULL || htab->got == NULL)
13460 abort ();
13461
13462 dyncon = (Elf64_External_Dyn *) sdyn->contents;
13463 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
13464 for (; dyncon < dynconend; dyncon++)
13465 {
13466 Elf_Internal_Dyn dyn;
13467 asection *s;
13468
13469 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13470
13471 switch (dyn.d_tag)
13472 {
13473 default:
13474 continue;
13475
13476 case DT_PPC64_GLINK:
13477 s = htab->glink;
13478 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13479 /* We stupidly defined DT_PPC64_GLINK to be the start
13480 of glink rather than the first entry point, which is
13481 what ld.so needs, and now have a bigger stub to
13482 support automatic multiple TOCs. */
13483 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
13484 break;
13485
13486 case DT_PPC64_OPD:
13487 s = bfd_get_section_by_name (output_bfd, ".opd");
13488 if (s == NULL)
13489 continue;
13490 dyn.d_un.d_ptr = s->vma;
13491 break;
13492
13493 case DT_PPC64_OPDSZ:
13494 s = bfd_get_section_by_name (output_bfd, ".opd");
13495 if (s == NULL)
13496 continue;
13497 dyn.d_un.d_val = s->size;
13498 break;
13499
13500 case DT_PLTGOT:
13501 s = htab->plt;
13502 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13503 break;
13504
13505 case DT_JMPREL:
13506 s = htab->relplt;
13507 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
13508 break;
13509
13510 case DT_PLTRELSZ:
13511 dyn.d_un.d_val = htab->relplt->size;
13512 break;
13513
13514 case DT_RELASZ:
13515 /* Don't count procedure linkage table relocs in the
13516 overall reloc count. */
13517 s = htab->relplt;
13518 if (s == NULL)
13519 continue;
13520 dyn.d_un.d_val -= s->size;
13521 break;
13522
13523 case DT_RELA:
13524 /* We may not be using the standard ELF linker script.
13525 If .rela.plt is the first .rela section, we adjust
13526 DT_RELA to not include it. */
13527 s = htab->relplt;
13528 if (s == NULL)
13529 continue;
13530 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13531 continue;
13532 dyn.d_un.d_ptr += s->size;
13533 break;
13534 }
13535
13536 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
13537 }
13538 }
13539
13540 if (htab->got != NULL && htab->got->size != 0)
13541 {
13542 /* Fill in the first entry in the global offset table.
13543 We use it to hold the link-time TOCbase. */
13544 bfd_put_64 (output_bfd,
13545 elf_gp (output_bfd) + TOC_BASE_OFF,
13546 htab->got->contents);
13547
13548 /* Set .got entry size. */
13549 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
13550 }
13551
13552 if (htab->plt != NULL && htab->plt->size != 0)
13553 {
13554 /* Set .plt entry size. */
13555 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
13556 = PLT_ENTRY_SIZE;
13557 }
13558
13559 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13560 brlt ourselves if emitrelocations. */
13561 if (htab->brlt != NULL
13562 && htab->brlt->reloc_count != 0
13563 && !_bfd_elf_link_output_relocs (output_bfd,
13564 htab->brlt,
13565 elf_section_data (htab->brlt)->rela.hdr,
13566 elf_section_data (htab->brlt)->relocs,
13567 NULL))
13568 return FALSE;
13569
13570 if (htab->glink != NULL
13571 && htab->glink->reloc_count != 0
13572 && !_bfd_elf_link_output_relocs (output_bfd,
13573 htab->glink,
13574 elf_section_data (htab->glink)->rela.hdr,
13575 elf_section_data (htab->glink)->relocs,
13576 NULL))
13577 return FALSE;
13578
13579 /* We need to handle writing out multiple GOT sections ourselves,
13580 since we didn't add them to DYNOBJ. We know dynobj is the first
13581 bfd. */
13582 while ((dynobj = dynobj->link_next) != NULL)
13583 {
13584 asection *s;
13585
13586 if (!is_ppc64_elf (dynobj))
13587 continue;
13588
13589 s = ppc64_elf_tdata (dynobj)->got;
13590 if (s != NULL
13591 && s->size != 0
13592 && s->output_section != bfd_abs_section_ptr
13593 && !bfd_set_section_contents (output_bfd, s->output_section,
13594 s->contents, s->output_offset,
13595 s->size))
13596 return FALSE;
13597 s = ppc64_elf_tdata (dynobj)->relgot;
13598 if (s != NULL
13599 && s->size != 0
13600 && s->output_section != bfd_abs_section_ptr
13601 && !bfd_set_section_contents (output_bfd, s->output_section,
13602 s->contents, s->output_offset,
13603 s->size))
13604 return FALSE;
13605 }
13606
13607 return TRUE;
13608 }
13609
13610 #include "elf64-target.h"
This page took 0.764527 seconds and 4 git commands to generate.