daily update
[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
3 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 <amodra@bigpond.net.au>
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 2 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 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
27
28 #include "bfd.h"
29 #include "sysdep.h"
30 #include "bfdlink.h"
31 #include "libbfd.h"
32 #include "elf-bfd.h"
33 #include "elf/ppc64.h"
34 #include "elf64-ppc.h"
35
36 static bfd_reloc_status_type ppc64_elf_ha_reloc
37 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38 static bfd_reloc_status_type ppc64_elf_branch_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_toc_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc64_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_vma opd_entry_value
55 (asection *, bfd_vma, asection **, bfd_vma *);
56
57 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
58 #define TARGET_LITTLE_NAME "elf64-powerpcle"
59 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
60 #define TARGET_BIG_NAME "elf64-powerpc"
61 #define ELF_ARCH bfd_arch_powerpc
62 #define ELF_MACHINE_CODE EM_PPC64
63 #define ELF_MAXPAGESIZE 0x10000
64 #define elf_info_to_howto ppc64_elf_info_to_howto
65
66 #define elf_backend_want_got_sym 0
67 #define elf_backend_want_plt_sym 0
68 #define elf_backend_plt_alignment 3
69 #define elf_backend_plt_not_loaded 1
70 #define elf_backend_got_header_size 8
71 #define elf_backend_can_gc_sections 1
72 #define elf_backend_can_refcount 1
73 #define elf_backend_rela_normal 1
74
75 #define bfd_elf64_mkobject ppc64_elf_mkobject
76 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
77 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
78 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
79 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
80 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
81 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
82
83 #define elf_backend_object_p ppc64_elf_object_p
84 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
85 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
86 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
87 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
88 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
89 #define elf_backend_check_directives ppc64_elf_check_directives
90 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
91 #define elf_backend_check_relocs ppc64_elf_check_relocs
92 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
93 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
94 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
95 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
96 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
97 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
98 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
99 #define elf_backend_action_discarded ppc64_elf_action_discarded
100 #define elf_backend_relocate_section ppc64_elf_relocate_section
101 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
102 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
103 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
104 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
105 #define elf_backend_special_sections ppc64_elf_special_sections
106
107 /* The name of the dynamic interpreter. This is put in the .interp
108 section. */
109 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
110
111 /* The size in bytes of an entry in the procedure linkage table. */
112 #define PLT_ENTRY_SIZE 24
113
114 /* The initial size of the plt reserved for the dynamic linker. */
115 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
116
117 /* TOC base pointers offset from start of TOC. */
118 #define TOC_BASE_OFF 0x8000
119
120 /* Offset of tp and dtp pointers from start of TLS block. */
121 #define TP_OFFSET 0x7000
122 #define DTP_OFFSET 0x8000
123
124 /* .plt call stub instructions. The normal stub is like this, but
125 sometimes the .plt entry crosses a 64k boundary and we need to
126 insert an addis to adjust r12. */
127 #define PLT_CALL_STUB_SIZE (7*4)
128 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
129 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
130 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
131 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
132 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
133 /* ld %r11,xxx+16@l(%r12) */
134 #define BCTR 0x4e800420 /* bctr */
135
136
137 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
138 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
139
140 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
141
142 /* glink call stub instructions. We enter with the index in R0, and the
143 address of glink entry in CTR. From that, we can calculate PLT0. */
144 #define GLINK_CALL_STUB_SIZE (16*4)
145 #define MFCTR_R12 0x7d8902a6 /* mfctr %r12 */
146 #define SLDI_R11_R0_3 0x780b1f24 /* sldi %r11,%r0,3 */
147 #define ADDIC_R2_R0_32K 0x34408000 /* addic. %r2,%r0,-32768 */
148 #define SUB_R12_R12_R11 0x7d8b6050 /* sub %r12,%r12,%r11 */
149 #define SRADI_R2_R2_63 0x7c42fe76 /* sradi %r2,%r2,63 */
150 #define SLDI_R11_R0_2 0x780b1764 /* sldi %r11,%r0,2 */
151 #define AND_R2_R2_R11 0x7c425838 /* and %r2,%r2,%r11 */
152 /* sub %r12,%r12,%r11 */
153 #define ADD_R12_R12_R2 0x7d8c1214 /* add %r12,%r12,%r2 */
154 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
155 /* ld %r11,xxx@l(%r12) */
156 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,xxx@l */
157 /* ld %r2,8(%r12) */
158 /* mtctr %r11 */
159 /* ld %r11,16(%r12) */
160 /* bctr */
161
162 /* Pad with this. */
163 #define NOP 0x60000000
164
165 /* Some other nops. */
166 #define CROR_151515 0x4def7b82
167 #define CROR_313131 0x4ffffb82
168
169 /* .glink entries for the first 32k functions are two instructions. */
170 #define LI_R0_0 0x38000000 /* li %r0,0 */
171 #define B_DOT 0x48000000 /* b . */
172
173 /* After that, we need two instructions to load the index, followed by
174 a branch. */
175 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
176 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
177
178 /* Instructions used by the save and restore reg functions. */
179 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
180 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
181 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
182 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
183 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
184 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
185 #define LI_R12_0 0x39800000 /* li %r12,0 */
186 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
187 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
188 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
189 #define BLR 0x4e800020 /* blr */
190
191 /* Since .opd is an array of descriptors and each entry will end up
192 with identical R_PPC64_RELATIVE relocs, there is really no need to
193 propagate .opd relocs; The dynamic linker should be taught to
194 relocate .opd without reloc entries. */
195 #ifndef NO_OPD_RELOCS
196 #define NO_OPD_RELOCS 0
197 #endif
198 \f
199 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
200
201 /* Relocation HOWTO's. */
202 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
203
204 static reloc_howto_type ppc64_elf_howto_raw[] = {
205 /* This reloc does nothing. */
206 HOWTO (R_PPC64_NONE, /* type */
207 0, /* rightshift */
208 2, /* size (0 = byte, 1 = short, 2 = long) */
209 32, /* bitsize */
210 FALSE, /* pc_relative */
211 0, /* bitpos */
212 complain_overflow_dont, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_PPC64_NONE", /* name */
215 FALSE, /* partial_inplace */
216 0, /* src_mask */
217 0, /* dst_mask */
218 FALSE), /* pcrel_offset */
219
220 /* A standard 32 bit relocation. */
221 HOWTO (R_PPC64_ADDR32, /* 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_bitfield, /* complain_on_overflow */
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_ADDR32", /* name */
230 FALSE, /* partial_inplace */
231 0, /* src_mask */
232 0xffffffff, /* dst_mask */
233 FALSE), /* pcrel_offset */
234
235 /* An absolute 26 bit branch; the lower two bits must be zero.
236 FIXME: we don't check that, we just clear them. */
237 HOWTO (R_PPC64_ADDR24, /* type */
238 0, /* rightshift */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
240 26, /* bitsize */
241 FALSE, /* pc_relative */
242 0, /* bitpos */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 bfd_elf_generic_reloc, /* special_function */
245 "R_PPC64_ADDR24", /* name */
246 FALSE, /* partial_inplace */
247 0, /* src_mask */
248 0x03fffffc, /* dst_mask */
249 FALSE), /* pcrel_offset */
250
251 /* A standard 16 bit relocation. */
252 HOWTO (R_PPC64_ADDR16, /* type */
253 0, /* rightshift */
254 1, /* size (0 = byte, 1 = short, 2 = long) */
255 16, /* 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_ADDR16", /* name */
261 FALSE, /* partial_inplace */
262 0, /* src_mask */
263 0xffff, /* dst_mask */
264 FALSE), /* pcrel_offset */
265
266 /* A 16 bit relocation without overflow. */
267 HOWTO (R_PPC64_ADDR16_LO, /* 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_dont,/* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16_LO", /* name */
276 FALSE, /* partial_inplace */
277 0, /* src_mask */
278 0xffff, /* dst_mask */
279 FALSE), /* pcrel_offset */
280
281 /* Bits 16-31 of an address. */
282 HOWTO (R_PPC64_ADDR16_HI, /* type */
283 16, /* 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_HI", /* 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, plus 1 if the contents of the low 16
297 bits, treated as a signed number, is negative. */
298 HOWTO (R_PPC64_ADDR16_HA, /* type */
299 16, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
302 FALSE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 ppc64_elf_ha_reloc, /* special_function */
306 "R_PPC64_ADDR16_HA", /* name */
307 FALSE, /* partial_inplace */
308 0, /* src_mask */
309 0xffff, /* dst_mask */
310 FALSE), /* pcrel_offset */
311
312 /* An absolute 16 bit branch; the lower two bits must be zero.
313 FIXME: we don't check that, we just clear them. */
314 HOWTO (R_PPC64_ADDR14, /* type */
315 0, /* rightshift */
316 2, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
318 FALSE, /* pc_relative */
319 0, /* bitpos */
320 complain_overflow_bitfield, /* complain_on_overflow */
321 ppc64_elf_branch_reloc, /* special_function */
322 "R_PPC64_ADDR14", /* name */
323 FALSE, /* partial_inplace */
324 0, /* src_mask */
325 0x0000fffc, /* dst_mask */
326 FALSE), /* pcrel_offset */
327
328 /* An absolute 16 bit branch, for which bit 10 should be set to
329 indicate that the branch is expected to be taken. The lower two
330 bits must be zero. */
331 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
332 0, /* rightshift */
333 2, /* size (0 = byte, 1 = short, 2 = long) */
334 16, /* bitsize */
335 FALSE, /* pc_relative */
336 0, /* bitpos */
337 complain_overflow_bitfield, /* complain_on_overflow */
338 ppc64_elf_brtaken_reloc, /* special_function */
339 "R_PPC64_ADDR14_BRTAKEN",/* name */
340 FALSE, /* partial_inplace */
341 0, /* src_mask */
342 0x0000fffc, /* dst_mask */
343 FALSE), /* pcrel_offset */
344
345 /* An absolute 16 bit branch, for which bit 10 should be set to
346 indicate that the branch is not expected to be taken. The lower
347 two bits must be zero. */
348 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
349 0, /* rightshift */
350 2, /* size (0 = byte, 1 = short, 2 = long) */
351 16, /* bitsize */
352 FALSE, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_bitfield, /* complain_on_overflow */
355 ppc64_elf_brtaken_reloc, /* special_function */
356 "R_PPC64_ADDR14_BRNTAKEN",/* name */
357 FALSE, /* partial_inplace */
358 0, /* src_mask */
359 0x0000fffc, /* dst_mask */
360 FALSE), /* pcrel_offset */
361
362 /* A relative 26 bit branch; the lower two bits must be zero. */
363 HOWTO (R_PPC64_REL24, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 26, /* bitsize */
367 TRUE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_signed, /* complain_on_overflow */
370 ppc64_elf_branch_reloc, /* special_function */
371 "R_PPC64_REL24", /* name */
372 FALSE, /* partial_inplace */
373 0, /* src_mask */
374 0x03fffffc, /* dst_mask */
375 TRUE), /* pcrel_offset */
376
377 /* A relative 16 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL14, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 16, /* 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_REL14", /* name */
387 FALSE, /* partial_inplace */
388 0, /* src_mask */
389 0x0000fffc, /* dst_mask */
390 TRUE), /* pcrel_offset */
391
392 /* A relative 16 bit branch. Bit 10 should be set to indicate that
393 the branch is expected to be taken. The lower two bits must be
394 zero. */
395 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 16, /* bitsize */
399 TRUE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_signed, /* complain_on_overflow */
402 ppc64_elf_brtaken_reloc, /* special_function */
403 "R_PPC64_REL14_BRTAKEN", /* name */
404 FALSE, /* partial_inplace */
405 0, /* src_mask */
406 0x0000fffc, /* dst_mask */
407 TRUE), /* pcrel_offset */
408
409 /* A relative 16 bit branch. Bit 10 should be set to indicate that
410 the branch is not expected to be taken. The lower two bits must
411 be zero. */
412 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
413 0, /* rightshift */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 TRUE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 ppc64_elf_brtaken_reloc, /* special_function */
420 "R_PPC64_REL14_BRNTAKEN",/* name */
421 FALSE, /* partial_inplace */
422 0, /* src_mask */
423 0x0000fffc, /* dst_mask */
424 TRUE), /* pcrel_offset */
425
426 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
427 symbol. */
428 HOWTO (R_PPC64_GOT16, /* type */
429 0, /* rightshift */
430 1, /* size (0 = byte, 1 = short, 2 = long) */
431 16, /* bitsize */
432 FALSE, /* pc_relative */
433 0, /* bitpos */
434 complain_overflow_signed, /* complain_on_overflow */
435 ppc64_elf_unhandled_reloc, /* special_function */
436 "R_PPC64_GOT16", /* name */
437 FALSE, /* partial_inplace */
438 0, /* src_mask */
439 0xffff, /* dst_mask */
440 FALSE), /* pcrel_offset */
441
442 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
443 the symbol. */
444 HOWTO (R_PPC64_GOT16_LO, /* type */
445 0, /* rightshift */
446 1, /* size (0 = byte, 1 = short, 2 = long) */
447 16, /* bitsize */
448 FALSE, /* pc_relative */
449 0, /* bitpos */
450 complain_overflow_dont, /* complain_on_overflow */
451 ppc64_elf_unhandled_reloc, /* special_function */
452 "R_PPC64_GOT16_LO", /* name */
453 FALSE, /* partial_inplace */
454 0, /* src_mask */
455 0xffff, /* dst_mask */
456 FALSE), /* pcrel_offset */
457
458 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
459 the symbol. */
460 HOWTO (R_PPC64_GOT16_HI, /* type */
461 16, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 16, /* bitsize */
464 FALSE, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_dont,/* complain_on_overflow */
467 ppc64_elf_unhandled_reloc, /* special_function */
468 "R_PPC64_GOT16_HI", /* name */
469 FALSE, /* partial_inplace */
470 0, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
473
474 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
475 the symbol. */
476 HOWTO (R_PPC64_GOT16_HA, /* type */
477 16, /* rightshift */
478 1, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
480 FALSE, /* pc_relative */
481 0, /* bitpos */
482 complain_overflow_dont,/* complain_on_overflow */
483 ppc64_elf_unhandled_reloc, /* special_function */
484 "R_PPC64_GOT16_HA", /* name */
485 FALSE, /* partial_inplace */
486 0, /* src_mask */
487 0xffff, /* dst_mask */
488 FALSE), /* pcrel_offset */
489
490 /* This is used only by the dynamic linker. The symbol should exist
491 both in the object being run and in some shared library. The
492 dynamic linker copies the data addressed by the symbol from the
493 shared library into the object, because the object being
494 run has to have the data at some particular address. */
495 HOWTO (R_PPC64_COPY, /* type */
496 0, /* rightshift */
497 0, /* this one is variable size */
498 0, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_dont, /* complain_on_overflow */
502 ppc64_elf_unhandled_reloc, /* special_function */
503 "R_PPC64_COPY", /* name */
504 FALSE, /* partial_inplace */
505 0, /* src_mask */
506 0, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Like R_PPC64_ADDR64, but used when setting global offset table
510 entries. */
511 HOWTO (R_PPC64_GLOB_DAT, /* type */
512 0, /* rightshift */
513 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
514 64, /* bitsize */
515 FALSE, /* pc_relative */
516 0, /* bitpos */
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
519 "R_PPC64_GLOB_DAT", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 ONES (64), /* dst_mask */
523 FALSE), /* pcrel_offset */
524
525 /* Created by the link editor. Marks a procedure linkage table
526 entry for a symbol. */
527 HOWTO (R_PPC64_JMP_SLOT, /* type */
528 0, /* rightshift */
529 0, /* size (0 = byte, 1 = short, 2 = long) */
530 0, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_dont, /* complain_on_overflow */
534 ppc64_elf_unhandled_reloc, /* special_function */
535 "R_PPC64_JMP_SLOT", /* name */
536 FALSE, /* partial_inplace */
537 0, /* src_mask */
538 0, /* dst_mask */
539 FALSE), /* pcrel_offset */
540
541 /* Used only by the dynamic linker. When the object is run, this
542 doubleword64 is set to the load address of the object, plus the
543 addend. */
544 HOWTO (R_PPC64_RELATIVE, /* type */
545 0, /* rightshift */
546 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
547 64, /* bitsize */
548 FALSE, /* pc_relative */
549 0, /* bitpos */
550 complain_overflow_dont, /* complain_on_overflow */
551 bfd_elf_generic_reloc, /* special_function */
552 "R_PPC64_RELATIVE", /* name */
553 FALSE, /* partial_inplace */
554 0, /* src_mask */
555 ONES (64), /* dst_mask */
556 FALSE), /* pcrel_offset */
557
558 /* Like R_PPC64_ADDR32, but may be unaligned. */
559 HOWTO (R_PPC64_UADDR32, /* type */
560 0, /* rightshift */
561 2, /* size (0 = byte, 1 = short, 2 = long) */
562 32, /* bitsize */
563 FALSE, /* pc_relative */
564 0, /* bitpos */
565 complain_overflow_bitfield, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_UADDR32", /* name */
568 FALSE, /* partial_inplace */
569 0, /* src_mask */
570 0xffffffff, /* dst_mask */
571 FALSE), /* pcrel_offset */
572
573 /* Like R_PPC64_ADDR16, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR16, /* type */
575 0, /* rightshift */
576 1, /* size (0 = byte, 1 = short, 2 = long) */
577 16, /* 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_UADDR16", /* name */
583 FALSE, /* partial_inplace */
584 0, /* src_mask */
585 0xffff, /* dst_mask */
586 FALSE), /* pcrel_offset */
587
588 /* 32-bit PC relative. */
589 HOWTO (R_PPC64_REL32, /* type */
590 0, /* rightshift */
591 2, /* size (0 = byte, 1 = short, 2 = long) */
592 32, /* bitsize */
593 TRUE, /* pc_relative */
594 0, /* bitpos */
595 /* FIXME: Verify. Was complain_overflow_bitfield. */
596 complain_overflow_signed, /* complain_on_overflow */
597 bfd_elf_generic_reloc, /* special_function */
598 "R_PPC64_REL32", /* name */
599 FALSE, /* partial_inplace */
600 0, /* src_mask */
601 0xffffffff, /* dst_mask */
602 TRUE), /* pcrel_offset */
603
604 /* 32-bit relocation to the symbol's procedure linkage table. */
605 HOWTO (R_PPC64_PLT32, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 32, /* bitsize */
609 FALSE, /* pc_relative */
610 0, /* bitpos */
611 complain_overflow_bitfield, /* complain_on_overflow */
612 ppc64_elf_unhandled_reloc, /* special_function */
613 "R_PPC64_PLT32", /* name */
614 FALSE, /* partial_inplace */
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
617 FALSE), /* pcrel_offset */
618
619 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
620 FIXME: R_PPC64_PLTREL32 not supported. */
621 HOWTO (R_PPC64_PLTREL32, /* type */
622 0, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
625 TRUE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_signed, /* complain_on_overflow */
628 bfd_elf_generic_reloc, /* special_function */
629 "R_PPC64_PLTREL32", /* name */
630 FALSE, /* partial_inplace */
631 0, /* src_mask */
632 0xffffffff, /* dst_mask */
633 TRUE), /* pcrel_offset */
634
635 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
636 the symbol. */
637 HOWTO (R_PPC64_PLT16_LO, /* type */
638 0, /* rightshift */
639 1, /* size (0 = byte, 1 = short, 2 = long) */
640 16, /* bitsize */
641 FALSE, /* pc_relative */
642 0, /* bitpos */
643 complain_overflow_dont, /* complain_on_overflow */
644 ppc64_elf_unhandled_reloc, /* special_function */
645 "R_PPC64_PLT16_LO", /* name */
646 FALSE, /* partial_inplace */
647 0, /* src_mask */
648 0xffff, /* dst_mask */
649 FALSE), /* pcrel_offset */
650
651 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
652 the symbol. */
653 HOWTO (R_PPC64_PLT16_HI, /* type */
654 16, /* rightshift */
655 1, /* size (0 = byte, 1 = short, 2 = long) */
656 16, /* bitsize */
657 FALSE, /* pc_relative */
658 0, /* bitpos */
659 complain_overflow_dont, /* complain_on_overflow */
660 ppc64_elf_unhandled_reloc, /* special_function */
661 "R_PPC64_PLT16_HI", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0xffff, /* dst_mask */
665 FALSE), /* pcrel_offset */
666
667 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
668 the symbol. */
669 HOWTO (R_PPC64_PLT16_HA, /* type */
670 16, /* rightshift */
671 1, /* size (0 = byte, 1 = short, 2 = long) */
672 16, /* bitsize */
673 FALSE, /* pc_relative */
674 0, /* bitpos */
675 complain_overflow_dont, /* complain_on_overflow */
676 ppc64_elf_unhandled_reloc, /* special_function */
677 "R_PPC64_PLT16_HA", /* name */
678 FALSE, /* partial_inplace */
679 0, /* src_mask */
680 0xffff, /* dst_mask */
681 FALSE), /* pcrel_offset */
682
683 /* 16-bit section relative relocation. */
684 HOWTO (R_PPC64_SECTOFF, /* type */
685 0, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
688 FALSE, /* pc_relative */
689 0, /* bitpos */
690 complain_overflow_bitfield, /* complain_on_overflow */
691 ppc64_elf_sectoff_reloc, /* special_function */
692 "R_PPC64_SECTOFF", /* name */
693 FALSE, /* partial_inplace */
694 0, /* src_mask */
695 0xffff, /* dst_mask */
696 FALSE), /* pcrel_offset */
697
698 /* Like R_PPC64_SECTOFF, but no overflow warning. */
699 HOWTO (R_PPC64_SECTOFF_LO, /* 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_dont, /* complain_on_overflow */
706 ppc64_elf_sectoff_reloc, /* special_function */
707 "R_PPC64_SECTOFF_LO", /* name */
708 FALSE, /* partial_inplace */
709 0, /* src_mask */
710 0xffff, /* dst_mask */
711 FALSE), /* pcrel_offset */
712
713 /* 16-bit upper half section relative relocation. */
714 HOWTO (R_PPC64_SECTOFF_HI, /* type */
715 16, /* 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_HI", /* name */
723 FALSE, /* partial_inplace */
724 0, /* src_mask */
725 0xffff, /* dst_mask */
726 FALSE), /* pcrel_offset */
727
728 /* 16-bit upper half adjusted section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HA, /* 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_ha_reloc, /* special_function */
737 "R_PPC64_SECTOFF_HA", /* name */
738 FALSE, /* partial_inplace */
739 0, /* src_mask */
740 0xffff, /* dst_mask */
741 FALSE), /* pcrel_offset */
742
743 /* Like R_PPC64_REL24 without touching the two least significant bits. */
744 HOWTO (R_PPC64_REL30, /* type */
745 2, /* rightshift */
746 2, /* size (0 = byte, 1 = short, 2 = long) */
747 30, /* bitsize */
748 TRUE, /* pc_relative */
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
751 bfd_elf_generic_reloc, /* special_function */
752 "R_PPC64_REL30", /* name */
753 FALSE, /* partial_inplace */
754 0, /* src_mask */
755 0xfffffffc, /* dst_mask */
756 TRUE), /* pcrel_offset */
757
758 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
759
760 /* A standard 64-bit relocation. */
761 HOWTO (R_PPC64_ADDR64, /* type */
762 0, /* rightshift */
763 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
764 64, /* bitsize */
765 FALSE, /* pc_relative */
766 0, /* bitpos */
767 complain_overflow_dont, /* complain_on_overflow */
768 bfd_elf_generic_reloc, /* special_function */
769 "R_PPC64_ADDR64", /* name */
770 FALSE, /* partial_inplace */
771 0, /* src_mask */
772 ONES (64), /* dst_mask */
773 FALSE), /* pcrel_offset */
774
775 /* The bits 32-47 of an address. */
776 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
777 32, /* rightshift */
778 1, /* size (0 = byte, 1 = short, 2 = long) */
779 16, /* 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_ADDR16_HIGHER", /* name */
785 FALSE, /* partial_inplace */
786 0, /* src_mask */
787 0xffff, /* dst_mask */
788 FALSE), /* pcrel_offset */
789
790 /* The bits 32-47 of an address, plus 1 if the contents of the low
791 16 bits, treated as a signed number, is negative. */
792 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
793 32, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_dont, /* complain_on_overflow */
799 ppc64_elf_ha_reloc, /* special_function */
800 "R_PPC64_ADDR16_HIGHERA", /* name */
801 FALSE, /* partial_inplace */
802 0, /* src_mask */
803 0xffff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* The bits 48-63 of an address. */
807 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
808 48, /* 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 bfd_elf_generic_reloc, /* special_function */
815 "R_PPC64_ADDR16_HIGHEST", /* 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, plus 1 if the contents of the low
822 16 bits, treated as a signed number, is negative. */
823 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
824 48, /* rightshift */
825 1, /* size (0 = byte, 1 = short, 2 = long) */
826 16, /* bitsize */
827 FALSE, /* pc_relative */
828 0, /* bitpos */
829 complain_overflow_dont, /* complain_on_overflow */
830 ppc64_elf_ha_reloc, /* special_function */
831 "R_PPC64_ADDR16_HIGHESTA", /* name */
832 FALSE, /* partial_inplace */
833 0, /* src_mask */
834 0xffff, /* dst_mask */
835 FALSE), /* pcrel_offset */
836
837 /* Like ADDR64, but may be unaligned. */
838 HOWTO (R_PPC64_UADDR64, /* type */
839 0, /* rightshift */
840 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
841 64, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_PPC64_UADDR64", /* name */
847 FALSE, /* partial_inplace */
848 0, /* src_mask */
849 ONES (64), /* dst_mask */
850 FALSE), /* pcrel_offset */
851
852 /* 64-bit relative relocation. */
853 HOWTO (R_PPC64_REL64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
857 TRUE, /* pc_relative */
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_REL64", /* name */
862 FALSE, /* partial_inplace */
863 0, /* src_mask */
864 ONES (64), /* dst_mask */
865 TRUE), /* pcrel_offset */
866
867 /* 64-bit relocation to the symbol's procedure linkage table. */
868 HOWTO (R_PPC64_PLT64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
872 FALSE, /* pc_relative */
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 ppc64_elf_unhandled_reloc, /* special_function */
876 "R_PPC64_PLT64", /* name */
877 FALSE, /* partial_inplace */
878 0, /* src_mask */
879 ONES (64), /* dst_mask */
880 FALSE), /* pcrel_offset */
881
882 /* 64-bit PC relative relocation to the symbol's procedure linkage
883 table. */
884 /* FIXME: R_PPC64_PLTREL64 not supported. */
885 HOWTO (R_PPC64_PLTREL64, /* type */
886 0, /* rightshift */
887 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
888 64, /* bitsize */
889 TRUE, /* pc_relative */
890 0, /* bitpos */
891 complain_overflow_dont, /* complain_on_overflow */
892 ppc64_elf_unhandled_reloc, /* special_function */
893 "R_PPC64_PLTREL64", /* name */
894 FALSE, /* partial_inplace */
895 0, /* src_mask */
896 ONES (64), /* dst_mask */
897 TRUE), /* pcrel_offset */
898
899 /* 16 bit TOC-relative relocation. */
900
901 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
902 HOWTO (R_PPC64_TOC16, /* type */
903 0, /* rightshift */
904 1, /* size (0 = byte, 1 = short, 2 = long) */
905 16, /* bitsize */
906 FALSE, /* pc_relative */
907 0, /* bitpos */
908 complain_overflow_signed, /* complain_on_overflow */
909 ppc64_elf_toc_reloc, /* special_function */
910 "R_PPC64_TOC16", /* name */
911 FALSE, /* partial_inplace */
912 0, /* src_mask */
913 0xffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
915
916 /* 16 bit TOC-relative relocation without overflow. */
917
918 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
919 HOWTO (R_PPC64_TOC16_LO, /* type */
920 0, /* rightshift */
921 1, /* size (0 = byte, 1 = short, 2 = long) */
922 16, /* bitsize */
923 FALSE, /* pc_relative */
924 0, /* bitpos */
925 complain_overflow_dont, /* complain_on_overflow */
926 ppc64_elf_toc_reloc, /* special_function */
927 "R_PPC64_TOC16_LO", /* name */
928 FALSE, /* partial_inplace */
929 0, /* src_mask */
930 0xffff, /* dst_mask */
931 FALSE), /* pcrel_offset */
932
933 /* 16 bit TOC-relative relocation, high 16 bits. */
934
935 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
936 HOWTO (R_PPC64_TOC16_HI, /* type */
937 16, /* rightshift */
938 1, /* size (0 = byte, 1 = short, 2 = long) */
939 16, /* bitsize */
940 FALSE, /* pc_relative */
941 0, /* bitpos */
942 complain_overflow_dont, /* complain_on_overflow */
943 ppc64_elf_toc_reloc, /* special_function */
944 "R_PPC64_TOC16_HI", /* name */
945 FALSE, /* partial_inplace */
946 0, /* src_mask */
947 0xffff, /* dst_mask */
948 FALSE), /* pcrel_offset */
949
950 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
951 contents of the low 16 bits, treated as a signed number, is
952 negative. */
953
954 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
955 HOWTO (R_PPC64_TOC16_HA, /* type */
956 16, /* rightshift */
957 1, /* size (0 = byte, 1 = short, 2 = long) */
958 16, /* bitsize */
959 FALSE, /* pc_relative */
960 0, /* bitpos */
961 complain_overflow_dont, /* complain_on_overflow */
962 ppc64_elf_toc_ha_reloc, /* special_function */
963 "R_PPC64_TOC16_HA", /* name */
964 FALSE, /* partial_inplace */
965 0, /* src_mask */
966 0xffff, /* dst_mask */
967 FALSE), /* pcrel_offset */
968
969 /* 64-bit relocation; insert value of TOC base (.TOC.). */
970
971 /* R_PPC64_TOC 51 doubleword64 .TOC. */
972 HOWTO (R_PPC64_TOC, /* type */
973 0, /* rightshift */
974 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
975 64, /* bitsize */
976 FALSE, /* pc_relative */
977 0, /* bitpos */
978 complain_overflow_bitfield, /* complain_on_overflow */
979 ppc64_elf_toc64_reloc, /* special_function */
980 "R_PPC64_TOC", /* name */
981 FALSE, /* partial_inplace */
982 0, /* src_mask */
983 ONES (64), /* dst_mask */
984 FALSE), /* pcrel_offset */
985
986 /* Like R_PPC64_GOT16, but also informs the link editor that the
987 value to relocate may (!) refer to a PLT entry which the link
988 editor (a) may replace with the symbol value. If the link editor
989 is unable to fully resolve the symbol, it may (b) create a PLT
990 entry and store the address to the new PLT entry in the GOT.
991 This permits lazy resolution of function symbols at run time.
992 The link editor may also skip all of this and just (c) emit a
993 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
994 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
995 HOWTO (R_PPC64_PLTGOT16, /* type */
996 0, /* rightshift */
997 1, /* size (0 = byte, 1 = short, 2 = long) */
998 16, /* bitsize */
999 FALSE, /* pc_relative */
1000 0, /* bitpos */
1001 complain_overflow_signed, /* complain_on_overflow */
1002 ppc64_elf_unhandled_reloc, /* special_function */
1003 "R_PPC64_PLTGOT16", /* name */
1004 FALSE, /* partial_inplace */
1005 0, /* src_mask */
1006 0xffff, /* dst_mask */
1007 FALSE), /* pcrel_offset */
1008
1009 /* Like R_PPC64_PLTGOT16, but without overflow. */
1010 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1011 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1012 0, /* rightshift */
1013 1, /* size (0 = byte, 1 = short, 2 = long) */
1014 16, /* bitsize */
1015 FALSE, /* pc_relative */
1016 0, /* bitpos */
1017 complain_overflow_dont, /* complain_on_overflow */
1018 ppc64_elf_unhandled_reloc, /* special_function */
1019 "R_PPC64_PLTGOT16_LO", /* name */
1020 FALSE, /* partial_inplace */
1021 0, /* src_mask */
1022 0xffff, /* dst_mask */
1023 FALSE), /* pcrel_offset */
1024
1025 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1026 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1027 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1028 16, /* rightshift */
1029 1, /* size (0 = byte, 1 = short, 2 = long) */
1030 16, /* bitsize */
1031 FALSE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_dont, /* complain_on_overflow */
1034 ppc64_elf_unhandled_reloc, /* special_function */
1035 "R_PPC64_PLTGOT16_HI", /* name */
1036 FALSE, /* partial_inplace */
1037 0, /* src_mask */
1038 0xffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1040
1041 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1042 1 if the contents of the low 16 bits, treated as a signed number,
1043 is negative. */
1044 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1045 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1046 16, /* rightshift */
1047 1, /* size (0 = byte, 1 = short, 2 = long) */
1048 16, /* bitsize */
1049 FALSE, /* pc_relative */
1050 0, /* bitpos */
1051 complain_overflow_dont,/* complain_on_overflow */
1052 ppc64_elf_unhandled_reloc, /* special_function */
1053 "R_PPC64_PLTGOT16_HA", /* name */
1054 FALSE, /* partial_inplace */
1055 0, /* src_mask */
1056 0xffff, /* dst_mask */
1057 FALSE), /* pcrel_offset */
1058
1059 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1060 HOWTO (R_PPC64_ADDR16_DS, /* type */
1061 0, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_bitfield, /* complain_on_overflow */
1067 bfd_elf_generic_reloc, /* special_function */
1068 "R_PPC64_ADDR16_DS", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xfffc, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1073
1074 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_LO_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_dont,/* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_LO_DS",/* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1088
1089 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_GOT16_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_signed, /* complain_on_overflow */
1097 ppc64_elf_unhandled_reloc, /* special_function */
1098 "R_PPC64_GOT16_DS", /* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_LO_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_dont, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_LO_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1118
1119 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_PLT16_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_PLT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1133
1134 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_SECTOFF_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_bitfield, /* complain_on_overflow */
1142 ppc64_elf_sectoff_reloc, /* special_function */
1143 "R_PPC64_SECTOFF_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1148
1149 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_LO_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_dont, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_LO_DS",/* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1163
1164 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_TOC16_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_signed, /* complain_on_overflow */
1172 ppc64_elf_toc_reloc, /* special_function */
1173 "R_PPC64_TOC16_DS", /* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_LO_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_dont, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_LO_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1195 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1196 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1197 0, /* rightshift */
1198 1, /* size (0 = byte, 1 = short, 2 = long) */
1199 16, /* bitsize */
1200 FALSE, /* pc_relative */
1201 0, /* bitpos */
1202 complain_overflow_signed, /* complain_on_overflow */
1203 ppc64_elf_unhandled_reloc, /* special_function */
1204 "R_PPC64_PLTGOT16_DS", /* name */
1205 FALSE, /* partial_inplace */
1206 0, /* src_mask */
1207 0xfffc, /* dst_mask */
1208 FALSE), /* pcrel_offset */
1209
1210 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1211 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1212 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1213 0, /* rightshift */
1214 1, /* size (0 = byte, 1 = short, 2 = long) */
1215 16, /* bitsize */
1216 FALSE, /* pc_relative */
1217 0, /* bitpos */
1218 complain_overflow_dont, /* complain_on_overflow */
1219 ppc64_elf_unhandled_reloc, /* special_function */
1220 "R_PPC64_PLTGOT16_LO_DS",/* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xfffc, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1225
1226 /* Marker reloc for TLS. */
1227 HOWTO (R_PPC64_TLS,
1228 0, /* rightshift */
1229 2, /* size (0 = byte, 1 = short, 2 = long) */
1230 32, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 bfd_elf_generic_reloc, /* special_function */
1235 "R_PPC64_TLS", /* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1240
1241 /* Computes the load module index of the load module that contains the
1242 definition of its TLS sym. */
1243 HOWTO (R_PPC64_DTPMOD64,
1244 0, /* rightshift */
1245 4, /* size (0 = byte, 1 = short, 2 = long) */
1246 64, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 ppc64_elf_unhandled_reloc, /* special_function */
1251 "R_PPC64_DTPMOD64", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 ONES (64), /* dst_mask */
1255 FALSE), /* pcrel_offset */
1256
1257 /* Computes a dtv-relative displacement, the difference between the value
1258 of sym+add and the base address of the thread-local storage block that
1259 contains the definition of sym, minus 0x8000. */
1260 HOWTO (R_PPC64_DTPREL64,
1261 0, /* rightshift */
1262 4, /* size (0 = byte, 1 = short, 2 = long) */
1263 64, /* bitsize */
1264 FALSE, /* pc_relative */
1265 0, /* bitpos */
1266 complain_overflow_dont, /* complain_on_overflow */
1267 ppc64_elf_unhandled_reloc, /* special_function */
1268 "R_PPC64_DTPREL64", /* name */
1269 FALSE, /* partial_inplace */
1270 0, /* src_mask */
1271 ONES (64), /* dst_mask */
1272 FALSE), /* pcrel_offset */
1273
1274 /* A 16 bit dtprel reloc. */
1275 HOWTO (R_PPC64_DTPREL16,
1276 0, /* rightshift */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1278 16, /* bitsize */
1279 FALSE, /* pc_relative */
1280 0, /* bitpos */
1281 complain_overflow_signed, /* complain_on_overflow */
1282 ppc64_elf_unhandled_reloc, /* special_function */
1283 "R_PPC64_DTPREL16", /* name */
1284 FALSE, /* partial_inplace */
1285 0, /* src_mask */
1286 0xffff, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1288
1289 /* Like DTPREL16, but no overflow. */
1290 HOWTO (R_PPC64_DTPREL16_LO,
1291 0, /* rightshift */
1292 1, /* size (0 = byte, 1 = short, 2 = long) */
1293 16, /* bitsize */
1294 FALSE, /* pc_relative */
1295 0, /* bitpos */
1296 complain_overflow_dont, /* complain_on_overflow */
1297 ppc64_elf_unhandled_reloc, /* special_function */
1298 "R_PPC64_DTPREL16_LO", /* name */
1299 FALSE, /* partial_inplace */
1300 0, /* src_mask */
1301 0xffff, /* dst_mask */
1302 FALSE), /* pcrel_offset */
1303
1304 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1305 HOWTO (R_PPC64_DTPREL16_HI,
1306 16, /* rightshift */
1307 1, /* size (0 = byte, 1 = short, 2 = long) */
1308 16, /* bitsize */
1309 FALSE, /* pc_relative */
1310 0, /* bitpos */
1311 complain_overflow_dont, /* complain_on_overflow */
1312 ppc64_elf_unhandled_reloc, /* special_function */
1313 "R_PPC64_DTPREL16_HI", /* name */
1314 FALSE, /* partial_inplace */
1315 0, /* src_mask */
1316 0xffff, /* dst_mask */
1317 FALSE), /* pcrel_offset */
1318
1319 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1320 HOWTO (R_PPC64_DTPREL16_HA,
1321 16, /* rightshift */
1322 1, /* size (0 = byte, 1 = short, 2 = long) */
1323 16, /* bitsize */
1324 FALSE, /* pc_relative */
1325 0, /* bitpos */
1326 complain_overflow_dont, /* complain_on_overflow */
1327 ppc64_elf_unhandled_reloc, /* special_function */
1328 "R_PPC64_DTPREL16_HA", /* name */
1329 FALSE, /* partial_inplace */
1330 0, /* src_mask */
1331 0xffff, /* dst_mask */
1332 FALSE), /* pcrel_offset */
1333
1334 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1335 HOWTO (R_PPC64_DTPREL16_HIGHER,
1336 32, /* rightshift */
1337 1, /* size (0 = byte, 1 = short, 2 = long) */
1338 16, /* bitsize */
1339 FALSE, /* pc_relative */
1340 0, /* bitpos */
1341 complain_overflow_dont, /* complain_on_overflow */
1342 ppc64_elf_unhandled_reloc, /* special_function */
1343 "R_PPC64_DTPREL16_HIGHER", /* name */
1344 FALSE, /* partial_inplace */
1345 0, /* src_mask */
1346 0xffff, /* dst_mask */
1347 FALSE), /* pcrel_offset */
1348
1349 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1350 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1351 32, /* rightshift */
1352 1, /* size (0 = byte, 1 = short, 2 = long) */
1353 16, /* bitsize */
1354 FALSE, /* pc_relative */
1355 0, /* bitpos */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 ppc64_elf_unhandled_reloc, /* special_function */
1358 "R_PPC64_DTPREL16_HIGHERA", /* name */
1359 FALSE, /* partial_inplace */
1360 0, /* src_mask */
1361 0xffff, /* dst_mask */
1362 FALSE), /* pcrel_offset */
1363
1364 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1365 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1366 48, /* rightshift */
1367 1, /* size (0 = byte, 1 = short, 2 = long) */
1368 16, /* bitsize */
1369 FALSE, /* pc_relative */
1370 0, /* bitpos */
1371 complain_overflow_dont, /* complain_on_overflow */
1372 ppc64_elf_unhandled_reloc, /* special_function */
1373 "R_PPC64_DTPREL16_HIGHEST", /* name */
1374 FALSE, /* partial_inplace */
1375 0, /* src_mask */
1376 0xffff, /* dst_mask */
1377 FALSE), /* pcrel_offset */
1378
1379 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1380 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1381 48, /* rightshift */
1382 1, /* size (0 = byte, 1 = short, 2 = long) */
1383 16, /* bitsize */
1384 FALSE, /* pc_relative */
1385 0, /* bitpos */
1386 complain_overflow_dont, /* complain_on_overflow */
1387 ppc64_elf_unhandled_reloc, /* special_function */
1388 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1389 FALSE, /* partial_inplace */
1390 0, /* src_mask */
1391 0xffff, /* dst_mask */
1392 FALSE), /* pcrel_offset */
1393
1394 /* Like DTPREL16, but for insns with a DS field. */
1395 HOWTO (R_PPC64_DTPREL16_DS,
1396 0, /* rightshift */
1397 1, /* size (0 = byte, 1 = short, 2 = long) */
1398 16, /* bitsize */
1399 FALSE, /* pc_relative */
1400 0, /* bitpos */
1401 complain_overflow_signed, /* complain_on_overflow */
1402 ppc64_elf_unhandled_reloc, /* special_function */
1403 "R_PPC64_DTPREL16_DS", /* name */
1404 FALSE, /* partial_inplace */
1405 0, /* src_mask */
1406 0xfffc, /* dst_mask */
1407 FALSE), /* pcrel_offset */
1408
1409 /* Like DTPREL16_DS, but no overflow. */
1410 HOWTO (R_PPC64_DTPREL16_LO_DS,
1411 0, /* rightshift */
1412 1, /* size (0 = byte, 1 = short, 2 = long) */
1413 16, /* bitsize */
1414 FALSE, /* pc_relative */
1415 0, /* bitpos */
1416 complain_overflow_dont, /* complain_on_overflow */
1417 ppc64_elf_unhandled_reloc, /* special_function */
1418 "R_PPC64_DTPREL16_LO_DS", /* name */
1419 FALSE, /* partial_inplace */
1420 0, /* src_mask */
1421 0xfffc, /* dst_mask */
1422 FALSE), /* pcrel_offset */
1423
1424 /* Computes a tp-relative displacement, the difference between the value of
1425 sym+add and the value of the thread pointer (r13). */
1426 HOWTO (R_PPC64_TPREL64,
1427 0, /* rightshift */
1428 4, /* size (0 = byte, 1 = short, 2 = long) */
1429 64, /* bitsize */
1430 FALSE, /* pc_relative */
1431 0, /* bitpos */
1432 complain_overflow_dont, /* complain_on_overflow */
1433 ppc64_elf_unhandled_reloc, /* special_function */
1434 "R_PPC64_TPREL64", /* name */
1435 FALSE, /* partial_inplace */
1436 0, /* src_mask */
1437 ONES (64), /* dst_mask */
1438 FALSE), /* pcrel_offset */
1439
1440 /* A 16 bit tprel reloc. */
1441 HOWTO (R_PPC64_TPREL16,
1442 0, /* rightshift */
1443 1, /* size (0 = byte, 1 = short, 2 = long) */
1444 16, /* bitsize */
1445 FALSE, /* pc_relative */
1446 0, /* bitpos */
1447 complain_overflow_signed, /* complain_on_overflow */
1448 ppc64_elf_unhandled_reloc, /* special_function */
1449 "R_PPC64_TPREL16", /* name */
1450 FALSE, /* partial_inplace */
1451 0, /* src_mask */
1452 0xffff, /* dst_mask */
1453 FALSE), /* pcrel_offset */
1454
1455 /* Like TPREL16, but no overflow. */
1456 HOWTO (R_PPC64_TPREL16_LO,
1457 0, /* rightshift */
1458 1, /* size (0 = byte, 1 = short, 2 = long) */
1459 16, /* bitsize */
1460 FALSE, /* pc_relative */
1461 0, /* bitpos */
1462 complain_overflow_dont, /* complain_on_overflow */
1463 ppc64_elf_unhandled_reloc, /* special_function */
1464 "R_PPC64_TPREL16_LO", /* name */
1465 FALSE, /* partial_inplace */
1466 0, /* src_mask */
1467 0xffff, /* dst_mask */
1468 FALSE), /* pcrel_offset */
1469
1470 /* Like TPREL16_LO, but next higher group of 16 bits. */
1471 HOWTO (R_PPC64_TPREL16_HI,
1472 16, /* rightshift */
1473 1, /* size (0 = byte, 1 = short, 2 = long) */
1474 16, /* bitsize */
1475 FALSE, /* pc_relative */
1476 0, /* bitpos */
1477 complain_overflow_dont, /* complain_on_overflow */
1478 ppc64_elf_unhandled_reloc, /* special_function */
1479 "R_PPC64_TPREL16_HI", /* name */
1480 FALSE, /* partial_inplace */
1481 0, /* src_mask */
1482 0xffff, /* dst_mask */
1483 FALSE), /* pcrel_offset */
1484
1485 /* Like TPREL16_HI, but adjust for low 16 bits. */
1486 HOWTO (R_PPC64_TPREL16_HA,
1487 16, /* rightshift */
1488 1, /* size (0 = byte, 1 = short, 2 = long) */
1489 16, /* bitsize */
1490 FALSE, /* pc_relative */
1491 0, /* bitpos */
1492 complain_overflow_dont, /* complain_on_overflow */
1493 ppc64_elf_unhandled_reloc, /* special_function */
1494 "R_PPC64_TPREL16_HA", /* name */
1495 FALSE, /* partial_inplace */
1496 0, /* src_mask */
1497 0xffff, /* dst_mask */
1498 FALSE), /* pcrel_offset */
1499
1500 /* Like TPREL16_HI, but next higher group of 16 bits. */
1501 HOWTO (R_PPC64_TPREL16_HIGHER,
1502 32, /* rightshift */
1503 1, /* size (0 = byte, 1 = short, 2 = long) */
1504 16, /* bitsize */
1505 FALSE, /* pc_relative */
1506 0, /* bitpos */
1507 complain_overflow_dont, /* complain_on_overflow */
1508 ppc64_elf_unhandled_reloc, /* special_function */
1509 "R_PPC64_TPREL16_HIGHER", /* name */
1510 FALSE, /* partial_inplace */
1511 0, /* src_mask */
1512 0xffff, /* dst_mask */
1513 FALSE), /* pcrel_offset */
1514
1515 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1516 HOWTO (R_PPC64_TPREL16_HIGHERA,
1517 32, /* rightshift */
1518 1, /* size (0 = byte, 1 = short, 2 = long) */
1519 16, /* bitsize */
1520 FALSE, /* pc_relative */
1521 0, /* bitpos */
1522 complain_overflow_dont, /* complain_on_overflow */
1523 ppc64_elf_unhandled_reloc, /* special_function */
1524 "R_PPC64_TPREL16_HIGHERA", /* name */
1525 FALSE, /* partial_inplace */
1526 0, /* src_mask */
1527 0xffff, /* dst_mask */
1528 FALSE), /* pcrel_offset */
1529
1530 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1531 HOWTO (R_PPC64_TPREL16_HIGHEST,
1532 48, /* rightshift */
1533 1, /* size (0 = byte, 1 = short, 2 = long) */
1534 16, /* bitsize */
1535 FALSE, /* pc_relative */
1536 0, /* bitpos */
1537 complain_overflow_dont, /* complain_on_overflow */
1538 ppc64_elf_unhandled_reloc, /* special_function */
1539 "R_PPC64_TPREL16_HIGHEST", /* name */
1540 FALSE, /* partial_inplace */
1541 0, /* src_mask */
1542 0xffff, /* dst_mask */
1543 FALSE), /* pcrel_offset */
1544
1545 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1546 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1547 48, /* rightshift */
1548 1, /* size (0 = byte, 1 = short, 2 = long) */
1549 16, /* bitsize */
1550 FALSE, /* pc_relative */
1551 0, /* bitpos */
1552 complain_overflow_dont, /* complain_on_overflow */
1553 ppc64_elf_unhandled_reloc, /* special_function */
1554 "R_PPC64_TPREL16_HIGHESTA", /* name */
1555 FALSE, /* partial_inplace */
1556 0, /* src_mask */
1557 0xffff, /* dst_mask */
1558 FALSE), /* pcrel_offset */
1559
1560 /* Like TPREL16, but for insns with a DS field. */
1561 HOWTO (R_PPC64_TPREL16_DS,
1562 0, /* rightshift */
1563 1, /* size (0 = byte, 1 = short, 2 = long) */
1564 16, /* bitsize */
1565 FALSE, /* pc_relative */
1566 0, /* bitpos */
1567 complain_overflow_signed, /* complain_on_overflow */
1568 ppc64_elf_unhandled_reloc, /* special_function */
1569 "R_PPC64_TPREL16_DS", /* name */
1570 FALSE, /* partial_inplace */
1571 0, /* src_mask */
1572 0xfffc, /* dst_mask */
1573 FALSE), /* pcrel_offset */
1574
1575 /* Like TPREL16_DS, but no overflow. */
1576 HOWTO (R_PPC64_TPREL16_LO_DS,
1577 0, /* rightshift */
1578 1, /* size (0 = byte, 1 = short, 2 = long) */
1579 16, /* bitsize */
1580 FALSE, /* pc_relative */
1581 0, /* bitpos */
1582 complain_overflow_dont, /* complain_on_overflow */
1583 ppc64_elf_unhandled_reloc, /* special_function */
1584 "R_PPC64_TPREL16_LO_DS", /* name */
1585 FALSE, /* partial_inplace */
1586 0, /* src_mask */
1587 0xfffc, /* dst_mask */
1588 FALSE), /* pcrel_offset */
1589
1590 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1591 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1592 to the first entry relative to the TOC base (r2). */
1593 HOWTO (R_PPC64_GOT_TLSGD16,
1594 0, /* rightshift */
1595 1, /* size (0 = byte, 1 = short, 2 = long) */
1596 16, /* bitsize */
1597 FALSE, /* pc_relative */
1598 0, /* bitpos */
1599 complain_overflow_signed, /* complain_on_overflow */
1600 ppc64_elf_unhandled_reloc, /* special_function */
1601 "R_PPC64_GOT_TLSGD16", /* name */
1602 FALSE, /* partial_inplace */
1603 0, /* src_mask */
1604 0xffff, /* dst_mask */
1605 FALSE), /* pcrel_offset */
1606
1607 /* Like GOT_TLSGD16, but no overflow. */
1608 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1609 0, /* rightshift */
1610 1, /* size (0 = byte, 1 = short, 2 = long) */
1611 16, /* bitsize */
1612 FALSE, /* pc_relative */
1613 0, /* bitpos */
1614 complain_overflow_dont, /* complain_on_overflow */
1615 ppc64_elf_unhandled_reloc, /* special_function */
1616 "R_PPC64_GOT_TLSGD16_LO", /* name */
1617 FALSE, /* partial_inplace */
1618 0, /* src_mask */
1619 0xffff, /* dst_mask */
1620 FALSE), /* pcrel_offset */
1621
1622 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1623 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1624 16, /* rightshift */
1625 1, /* size (0 = byte, 1 = short, 2 = long) */
1626 16, /* bitsize */
1627 FALSE, /* pc_relative */
1628 0, /* bitpos */
1629 complain_overflow_dont, /* complain_on_overflow */
1630 ppc64_elf_unhandled_reloc, /* special_function */
1631 "R_PPC64_GOT_TLSGD16_HI", /* name */
1632 FALSE, /* partial_inplace */
1633 0, /* src_mask */
1634 0xffff, /* dst_mask */
1635 FALSE), /* pcrel_offset */
1636
1637 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1638 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1639 16, /* rightshift */
1640 1, /* size (0 = byte, 1 = short, 2 = long) */
1641 16, /* bitsize */
1642 FALSE, /* pc_relative */
1643 0, /* bitpos */
1644 complain_overflow_dont, /* complain_on_overflow */
1645 ppc64_elf_unhandled_reloc, /* special_function */
1646 "R_PPC64_GOT_TLSGD16_HA", /* name */
1647 FALSE, /* partial_inplace */
1648 0, /* src_mask */
1649 0xffff, /* dst_mask */
1650 FALSE), /* pcrel_offset */
1651
1652 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1653 with values (sym+add)@dtpmod and zero, and computes the offset to the
1654 first entry relative to the TOC base (r2). */
1655 HOWTO (R_PPC64_GOT_TLSLD16,
1656 0, /* rightshift */
1657 1, /* size (0 = byte, 1 = short, 2 = long) */
1658 16, /* bitsize */
1659 FALSE, /* pc_relative */
1660 0, /* bitpos */
1661 complain_overflow_signed, /* complain_on_overflow */
1662 ppc64_elf_unhandled_reloc, /* special_function */
1663 "R_PPC64_GOT_TLSLD16", /* name */
1664 FALSE, /* partial_inplace */
1665 0, /* src_mask */
1666 0xffff, /* dst_mask */
1667 FALSE), /* pcrel_offset */
1668
1669 /* Like GOT_TLSLD16, but no overflow. */
1670 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1671 0, /* rightshift */
1672 1, /* size (0 = byte, 1 = short, 2 = long) */
1673 16, /* bitsize */
1674 FALSE, /* pc_relative */
1675 0, /* bitpos */
1676 complain_overflow_dont, /* complain_on_overflow */
1677 ppc64_elf_unhandled_reloc, /* special_function */
1678 "R_PPC64_GOT_TLSLD16_LO", /* name */
1679 FALSE, /* partial_inplace */
1680 0, /* src_mask */
1681 0xffff, /* dst_mask */
1682 FALSE), /* pcrel_offset */
1683
1684 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1685 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1686 16, /* rightshift */
1687 1, /* size (0 = byte, 1 = short, 2 = long) */
1688 16, /* bitsize */
1689 FALSE, /* pc_relative */
1690 0, /* bitpos */
1691 complain_overflow_dont, /* complain_on_overflow */
1692 ppc64_elf_unhandled_reloc, /* special_function */
1693 "R_PPC64_GOT_TLSLD16_HI", /* name */
1694 FALSE, /* partial_inplace */
1695 0, /* src_mask */
1696 0xffff, /* dst_mask */
1697 FALSE), /* pcrel_offset */
1698
1699 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1700 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1701 16, /* rightshift */
1702 1, /* size (0 = byte, 1 = short, 2 = long) */
1703 16, /* bitsize */
1704 FALSE, /* pc_relative */
1705 0, /* bitpos */
1706 complain_overflow_dont, /* complain_on_overflow */
1707 ppc64_elf_unhandled_reloc, /* special_function */
1708 "R_PPC64_GOT_TLSLD16_HA", /* name */
1709 FALSE, /* partial_inplace */
1710 0, /* src_mask */
1711 0xffff, /* dst_mask */
1712 FALSE), /* pcrel_offset */
1713
1714 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1715 the offset to the entry relative to the TOC base (r2). */
1716 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1717 0, /* rightshift */
1718 1, /* size (0 = byte, 1 = short, 2 = long) */
1719 16, /* bitsize */
1720 FALSE, /* pc_relative */
1721 0, /* bitpos */
1722 complain_overflow_signed, /* complain_on_overflow */
1723 ppc64_elf_unhandled_reloc, /* special_function */
1724 "R_PPC64_GOT_DTPREL16_DS", /* name */
1725 FALSE, /* partial_inplace */
1726 0, /* src_mask */
1727 0xfffc, /* dst_mask */
1728 FALSE), /* pcrel_offset */
1729
1730 /* Like GOT_DTPREL16_DS, but no overflow. */
1731 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1732 0, /* rightshift */
1733 1, /* size (0 = byte, 1 = short, 2 = long) */
1734 16, /* bitsize */
1735 FALSE, /* pc_relative */
1736 0, /* bitpos */
1737 complain_overflow_dont, /* complain_on_overflow */
1738 ppc64_elf_unhandled_reloc, /* special_function */
1739 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1740 FALSE, /* partial_inplace */
1741 0, /* src_mask */
1742 0xfffc, /* dst_mask */
1743 FALSE), /* pcrel_offset */
1744
1745 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1746 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1747 16, /* rightshift */
1748 1, /* size (0 = byte, 1 = short, 2 = long) */
1749 16, /* bitsize */
1750 FALSE, /* pc_relative */
1751 0, /* bitpos */
1752 complain_overflow_dont, /* complain_on_overflow */
1753 ppc64_elf_unhandled_reloc, /* special_function */
1754 "R_PPC64_GOT_DTPREL16_HI", /* name */
1755 FALSE, /* partial_inplace */
1756 0, /* src_mask */
1757 0xffff, /* dst_mask */
1758 FALSE), /* pcrel_offset */
1759
1760 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1761 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1762 16, /* rightshift */
1763 1, /* size (0 = byte, 1 = short, 2 = long) */
1764 16, /* bitsize */
1765 FALSE, /* pc_relative */
1766 0, /* bitpos */
1767 complain_overflow_dont, /* complain_on_overflow */
1768 ppc64_elf_unhandled_reloc, /* special_function */
1769 "R_PPC64_GOT_DTPREL16_HA", /* name */
1770 FALSE, /* partial_inplace */
1771 0, /* src_mask */
1772 0xffff, /* dst_mask */
1773 FALSE), /* pcrel_offset */
1774
1775 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1776 offset to the entry relative to the TOC base (r2). */
1777 HOWTO (R_PPC64_GOT_TPREL16_DS,
1778 0, /* rightshift */
1779 1, /* size (0 = byte, 1 = short, 2 = long) */
1780 16, /* bitsize */
1781 FALSE, /* pc_relative */
1782 0, /* bitpos */
1783 complain_overflow_signed, /* complain_on_overflow */
1784 ppc64_elf_unhandled_reloc, /* special_function */
1785 "R_PPC64_GOT_TPREL16_DS", /* name */
1786 FALSE, /* partial_inplace */
1787 0, /* src_mask */
1788 0xfffc, /* dst_mask */
1789 FALSE), /* pcrel_offset */
1790
1791 /* Like GOT_TPREL16_DS, but no overflow. */
1792 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1793 0, /* rightshift */
1794 1, /* size (0 = byte, 1 = short, 2 = long) */
1795 16, /* bitsize */
1796 FALSE, /* pc_relative */
1797 0, /* bitpos */
1798 complain_overflow_dont, /* complain_on_overflow */
1799 ppc64_elf_unhandled_reloc, /* special_function */
1800 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1801 FALSE, /* partial_inplace */
1802 0, /* src_mask */
1803 0xfffc, /* dst_mask */
1804 FALSE), /* pcrel_offset */
1805
1806 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1807 HOWTO (R_PPC64_GOT_TPREL16_HI,
1808 16, /* rightshift */
1809 1, /* size (0 = byte, 1 = short, 2 = long) */
1810 16, /* bitsize */
1811 FALSE, /* pc_relative */
1812 0, /* bitpos */
1813 complain_overflow_dont, /* complain_on_overflow */
1814 ppc64_elf_unhandled_reloc, /* special_function */
1815 "R_PPC64_GOT_TPREL16_HI", /* name */
1816 FALSE, /* partial_inplace */
1817 0, /* src_mask */
1818 0xffff, /* dst_mask */
1819 FALSE), /* pcrel_offset */
1820
1821 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1822 HOWTO (R_PPC64_GOT_TPREL16_HA,
1823 16, /* rightshift */
1824 1, /* size (0 = byte, 1 = short, 2 = long) */
1825 16, /* bitsize */
1826 FALSE, /* pc_relative */
1827 0, /* bitpos */
1828 complain_overflow_dont, /* complain_on_overflow */
1829 ppc64_elf_unhandled_reloc, /* special_function */
1830 "R_PPC64_GOT_TPREL16_HA", /* name */
1831 FALSE, /* partial_inplace */
1832 0, /* src_mask */
1833 0xffff, /* dst_mask */
1834 FALSE), /* pcrel_offset */
1835
1836 /* GNU extension to record C++ vtable hierarchy. */
1837 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1838 0, /* rightshift */
1839 0, /* size (0 = byte, 1 = short, 2 = long) */
1840 0, /* bitsize */
1841 FALSE, /* pc_relative */
1842 0, /* bitpos */
1843 complain_overflow_dont, /* complain_on_overflow */
1844 NULL, /* special_function */
1845 "R_PPC64_GNU_VTINHERIT", /* name */
1846 FALSE, /* partial_inplace */
1847 0, /* src_mask */
1848 0, /* dst_mask */
1849 FALSE), /* pcrel_offset */
1850
1851 /* GNU extension to record C++ vtable member usage. */
1852 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1853 0, /* rightshift */
1854 0, /* size (0 = byte, 1 = short, 2 = long) */
1855 0, /* bitsize */
1856 FALSE, /* pc_relative */
1857 0, /* bitpos */
1858 complain_overflow_dont, /* complain_on_overflow */
1859 NULL, /* special_function */
1860 "R_PPC64_GNU_VTENTRY", /* name */
1861 FALSE, /* partial_inplace */
1862 0, /* src_mask */
1863 0, /* dst_mask */
1864 FALSE), /* pcrel_offset */
1865 };
1866
1867 \f
1868 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1869 be done. */
1870
1871 static void
1872 ppc_howto_init (void)
1873 {
1874 unsigned int i, type;
1875
1876 for (i = 0;
1877 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1878 i++)
1879 {
1880 type = ppc64_elf_howto_raw[i].type;
1881 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1882 / sizeof (ppc64_elf_howto_table[0])));
1883 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1884 }
1885 }
1886
1887 static reloc_howto_type *
1888 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1889 bfd_reloc_code_real_type code)
1890 {
1891 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1892
1893 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1894 /* Initialize howto table if needed. */
1895 ppc_howto_init ();
1896
1897 switch (code)
1898 {
1899 default:
1900 return NULL;
1901
1902 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1903 break;
1904 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1905 break;
1906 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1907 break;
1908 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1909 break;
1910 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1911 break;
1912 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1913 break;
1914 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1915 break;
1916 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1917 break;
1918 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1919 break;
1920 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1921 break;
1922 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1923 break;
1924 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1925 break;
1926 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1927 break;
1928 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1929 break;
1930 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1931 break;
1932 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1933 break;
1934 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1935 break;
1936 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1937 break;
1938 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1939 break;
1940 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1941 break;
1942 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1943 break;
1944 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1945 break;
1946 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1947 break;
1948 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1949 break;
1950 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1951 break;
1952 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1953 break;
1954 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1955 break;
1956 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1957 break;
1958 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1959 break;
1960 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1961 break;
1962 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1963 break;
1964 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1965 break;
1966 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1967 break;
1968 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1969 break;
1970 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1971 break;
1972 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1973 break;
1974 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1975 break;
1976 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1977 break;
1978 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1979 break;
1980 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1981 break;
1982 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1983 break;
1984 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1985 break;
1986 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1987 break;
1988 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1989 break;
1990 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1991 break;
1992 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1993 break;
1994 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1995 break;
1996 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1997 break;
1998 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1999 break;
2000 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2001 break;
2002 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2003 break;
2004 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2005 break;
2006 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2007 break;
2008 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2009 break;
2010 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2011 break;
2012 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2013 break;
2014 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2015 break;
2016 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2017 break;
2018 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2019 break;
2020 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2021 break;
2022 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2023 break;
2024 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2025 break;
2026 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2027 break;
2028 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2029 break;
2030 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2031 break;
2032 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2033 break;
2034 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2035 break;
2036 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2037 break;
2038 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2039 break;
2040 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2041 break;
2042 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2043 break;
2044 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2045 break;
2046 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2047 break;
2048 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2049 break;
2050 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2051 break;
2052 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2053 break;
2054 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2055 break;
2056 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2057 break;
2058 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2059 break;
2060 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2061 break;
2062 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2063 break;
2064 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2065 break;
2066 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2067 break;
2068 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2069 break;
2070 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2071 break;
2072 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2073 break;
2074 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2075 break;
2076 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2077 break;
2078 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2079 break;
2080 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2081 break;
2082 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2083 break;
2084 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2085 break;
2086 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2087 break;
2088 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2089 break;
2090 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2091 break;
2092 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2093 break;
2094 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2095 break;
2096 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2097 break;
2098 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2099 break;
2100 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2101 break;
2102 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2103 break;
2104 }
2105
2106 return ppc64_elf_howto_table[r];
2107 };
2108
2109 /* Set the howto pointer for a PowerPC ELF reloc. */
2110
2111 static void
2112 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2113 Elf_Internal_Rela *dst)
2114 {
2115 unsigned int type;
2116
2117 /* Initialize howto table if needed. */
2118 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2119 ppc_howto_init ();
2120
2121 type = ELF64_R_TYPE (dst->r_info);
2122 if (type >= (sizeof (ppc64_elf_howto_table)
2123 / sizeof (ppc64_elf_howto_table[0])))
2124 {
2125 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2126 abfd, (int) type);
2127 type = R_PPC64_NONE;
2128 }
2129 cache_ptr->howto = ppc64_elf_howto_table[type];
2130 }
2131
2132 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2133
2134 static bfd_reloc_status_type
2135 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2136 void *data, asection *input_section,
2137 bfd *output_bfd, char **error_message)
2138 {
2139 /* If this is a relocatable link (output_bfd test tells us), just
2140 call the generic function. Any adjustment will be done at final
2141 link time. */
2142 if (output_bfd != NULL)
2143 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2144 input_section, output_bfd, error_message);
2145
2146 /* Adjust the addend for sign extension of the low 16 bits.
2147 We won't actually be using the low 16 bits, so trashing them
2148 doesn't matter. */
2149 reloc_entry->addend += 0x8000;
2150 return bfd_reloc_continue;
2151 }
2152
2153 static bfd_reloc_status_type
2154 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2155 void *data, asection *input_section,
2156 bfd *output_bfd, char **error_message)
2157 {
2158 if (output_bfd != NULL)
2159 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2160 input_section, output_bfd, error_message);
2161
2162 if (strcmp (symbol->section->name, ".opd") == 0
2163 && (symbol->section->owner->flags & DYNAMIC) == 0)
2164 {
2165 bfd_vma dest = opd_entry_value (symbol->section,
2166 symbol->value + reloc_entry->addend,
2167 NULL, NULL);
2168 if (dest != (bfd_vma) -1)
2169 reloc_entry->addend = dest - (symbol->value
2170 + symbol->section->output_section->vma
2171 + symbol->section->output_offset);
2172 }
2173 return bfd_reloc_continue;
2174 }
2175
2176 static bfd_reloc_status_type
2177 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2178 void *data, asection *input_section,
2179 bfd *output_bfd, char **error_message)
2180 {
2181 long insn;
2182 enum elf_ppc64_reloc_type r_type;
2183 bfd_size_type octets;
2184 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2185 bfd_boolean is_power4 = FALSE;
2186
2187 /* If this is a relocatable link (output_bfd test tells us), just
2188 call the generic function. Any adjustment will be done at final
2189 link time. */
2190 if (output_bfd != NULL)
2191 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2192 input_section, output_bfd, error_message);
2193
2194 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2195 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2196 insn &= ~(0x01 << 21);
2197 r_type = reloc_entry->howto->type;
2198 if (r_type == R_PPC64_ADDR14_BRTAKEN
2199 || r_type == R_PPC64_REL14_BRTAKEN)
2200 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2201
2202 if (is_power4)
2203 {
2204 /* Set 'a' bit. This is 0b00010 in BO field for branch
2205 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2206 for branch on CTR insns (BO == 1a00t or 1a01t). */
2207 if ((insn & (0x14 << 21)) == (0x04 << 21))
2208 insn |= 0x02 << 21;
2209 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2210 insn |= 0x08 << 21;
2211 else
2212 goto out;
2213 }
2214 else
2215 {
2216 bfd_vma target = 0;
2217 bfd_vma from;
2218
2219 if (!bfd_is_com_section (symbol->section))
2220 target = symbol->value;
2221 target += symbol->section->output_section->vma;
2222 target += symbol->section->output_offset;
2223 target += reloc_entry->addend;
2224
2225 from = (reloc_entry->address
2226 + input_section->output_offset
2227 + input_section->output_section->vma);
2228
2229 /* Invert 'y' bit if not the default. */
2230 if ((bfd_signed_vma) (target - from) < 0)
2231 insn ^= 0x01 << 21;
2232 }
2233 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2234 out:
2235 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2236 input_section, output_bfd, error_message);
2237 }
2238
2239 static bfd_reloc_status_type
2240 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2241 void *data, asection *input_section,
2242 bfd *output_bfd, char **error_message)
2243 {
2244 /* If this is a relocatable link (output_bfd test tells us), just
2245 call the generic function. Any adjustment will be done at final
2246 link time. */
2247 if (output_bfd != NULL)
2248 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2249 input_section, output_bfd, error_message);
2250
2251 /* Subtract the symbol section base address. */
2252 reloc_entry->addend -= symbol->section->output_section->vma;
2253 return bfd_reloc_continue;
2254 }
2255
2256 static bfd_reloc_status_type
2257 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2258 void *data, asection *input_section,
2259 bfd *output_bfd, char **error_message)
2260 {
2261 /* If this is a relocatable link (output_bfd test tells us), just
2262 call the generic function. Any adjustment will be done at final
2263 link time. */
2264 if (output_bfd != NULL)
2265 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2266 input_section, output_bfd, error_message);
2267
2268 /* Subtract the symbol section base address. */
2269 reloc_entry->addend -= symbol->section->output_section->vma;
2270
2271 /* Adjust the addend for sign extension of the low 16 bits. */
2272 reloc_entry->addend += 0x8000;
2273 return bfd_reloc_continue;
2274 }
2275
2276 static bfd_reloc_status_type
2277 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2278 void *data, asection *input_section,
2279 bfd *output_bfd, char **error_message)
2280 {
2281 bfd_vma TOCstart;
2282
2283 /* If this is a relocatable link (output_bfd test tells us), just
2284 call the generic function. Any adjustment will be done at final
2285 link time. */
2286 if (output_bfd != NULL)
2287 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2288 input_section, output_bfd, error_message);
2289
2290 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2291 if (TOCstart == 0)
2292 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2293
2294 /* Subtract the TOC base address. */
2295 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2296 return bfd_reloc_continue;
2297 }
2298
2299 static bfd_reloc_status_type
2300 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2301 void *data, asection *input_section,
2302 bfd *output_bfd, char **error_message)
2303 {
2304 bfd_vma TOCstart;
2305
2306 /* If this is a relocatable link (output_bfd test tells us), just
2307 call the generic function. Any adjustment will be done at final
2308 link time. */
2309 if (output_bfd != NULL)
2310 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2311 input_section, output_bfd, error_message);
2312
2313 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2314 if (TOCstart == 0)
2315 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2316
2317 /* Subtract the TOC base address. */
2318 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2319
2320 /* Adjust the addend for sign extension of the low 16 bits. */
2321 reloc_entry->addend += 0x8000;
2322 return bfd_reloc_continue;
2323 }
2324
2325 static bfd_reloc_status_type
2326 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2327 void *data, asection *input_section,
2328 bfd *output_bfd, char **error_message)
2329 {
2330 bfd_vma TOCstart;
2331 bfd_size_type octets;
2332
2333 /* If this is a relocatable link (output_bfd test tells us), just
2334 call the generic function. Any adjustment will be done at final
2335 link time. */
2336 if (output_bfd != NULL)
2337 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2338 input_section, output_bfd, error_message);
2339
2340 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2341 if (TOCstart == 0)
2342 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2343
2344 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2345 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2346 return bfd_reloc_ok;
2347 }
2348
2349 static bfd_reloc_status_type
2350 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2351 void *data, asection *input_section,
2352 bfd *output_bfd, char **error_message)
2353 {
2354 /* If this is a relocatable link (output_bfd test tells us), just
2355 call the generic function. Any adjustment will be done at final
2356 link time. */
2357 if (output_bfd != NULL)
2358 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2359 input_section, output_bfd, error_message);
2360
2361 if (error_message != NULL)
2362 {
2363 static char buf[60];
2364 sprintf (buf, "generic linker can't handle %s",
2365 reloc_entry->howto->name);
2366 *error_message = buf;
2367 }
2368 return bfd_reloc_dangerous;
2369 }
2370
2371 struct ppc64_elf_obj_tdata
2372 {
2373 struct elf_obj_tdata elf;
2374
2375 /* Shortcuts to dynamic linker sections. */
2376 asection *got;
2377 asection *relgot;
2378
2379 union {
2380 /* Used during garbage collection. We attach global symbols defined
2381 on removed .opd entries to this section so that the sym is removed. */
2382 asection *deleted_section;
2383
2384 /* Used when adding symbols. */
2385 bfd_boolean has_dotsym;
2386 } u;
2387
2388 /* TLS local dynamic got entry handling. Suppose for multiple GOT
2389 sections means we potentially need one of these for each input bfd. */
2390 union {
2391 bfd_signed_vma refcount;
2392 bfd_vma offset;
2393 } tlsld_got;
2394
2395 /* A copy of relocs before they are modified for --emit-relocs. */
2396 Elf_Internal_Rela *opd_relocs;
2397 };
2398
2399 #define ppc64_elf_tdata(bfd) \
2400 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2401
2402 #define ppc64_tlsld_got(bfd) \
2403 (&ppc64_elf_tdata (bfd)->tlsld_got)
2404
2405 /* Override the generic function because we store some extras. */
2406
2407 static bfd_boolean
2408 ppc64_elf_mkobject (bfd *abfd)
2409 {
2410 bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2411 abfd->tdata.any = bfd_zalloc (abfd, amt);
2412 if (abfd->tdata.any == NULL)
2413 return FALSE;
2414 return TRUE;
2415 }
2416
2417 /* Return 1 if target is one of ours. */
2418
2419 static bfd_boolean
2420 is_ppc64_elf_target (const struct bfd_target *targ)
2421 {
2422 extern const bfd_target bfd_elf64_powerpc_vec;
2423 extern const bfd_target bfd_elf64_powerpcle_vec;
2424
2425 return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
2426 }
2427
2428 /* Fix bad default arch selected for a 64 bit input bfd when the
2429 default is 32 bit. */
2430
2431 static bfd_boolean
2432 ppc64_elf_object_p (bfd *abfd)
2433 {
2434 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2435 {
2436 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2437
2438 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2439 {
2440 /* Relies on arch after 32 bit default being 64 bit default. */
2441 abfd->arch_info = abfd->arch_info->next;
2442 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2443 }
2444 }
2445 return TRUE;
2446 }
2447
2448 /* Support for core dump NOTE sections. */
2449
2450 static bfd_boolean
2451 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2452 {
2453 size_t offset, size;
2454
2455 if (note->descsz != 504)
2456 return FALSE;
2457
2458 /* pr_cursig */
2459 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2460
2461 /* pr_pid */
2462 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2463
2464 /* pr_reg */
2465 offset = 112;
2466 size = 384;
2467
2468 /* Make a ".reg/999" section. */
2469 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2470 size, note->descpos + offset);
2471 }
2472
2473 static bfd_boolean
2474 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2475 {
2476 if (note->descsz != 136)
2477 return FALSE;
2478
2479 elf_tdata (abfd)->core_program
2480 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2481 elf_tdata (abfd)->core_command
2482 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2483
2484 return TRUE;
2485 }
2486
2487 /* Merge backend specific data from an object file to the output
2488 object file when linking. */
2489
2490 static bfd_boolean
2491 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2492 {
2493 /* Check if we have the same endianess. */
2494 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2495 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2496 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2497 {
2498 const char *msg;
2499
2500 if (bfd_big_endian (ibfd))
2501 msg = _("%B: compiled for a big endian system "
2502 "and target is little endian");
2503 else
2504 msg = _("%B: compiled for a little endian system "
2505 "and target is big endian");
2506
2507 (*_bfd_error_handler) (msg, ibfd);
2508
2509 bfd_set_error (bfd_error_wrong_format);
2510 return FALSE;
2511 }
2512
2513 return TRUE;
2514 }
2515
2516 /* Add extra PPC sections. */
2517
2518 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2519 {
2520 { ".plt", 4, 0, SHT_NOBITS, 0 },
2521 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2522 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2523 { ".toc", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2524 { ".toc1", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2525 { ".tocbss", 7, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2526 { NULL, 0, 0, 0, 0 }
2527 };
2528
2529 struct _ppc64_elf_section_data
2530 {
2531 struct bfd_elf_section_data elf;
2532
2533 /* An array with one entry for each opd function descriptor. */
2534 union
2535 {
2536 /* Points to the function code section for local opd entries. */
2537 asection **func_sec;
2538 /* After editing .opd, adjust references to opd local syms. */
2539 long *adjust;
2540 } opd;
2541
2542 /* An array for toc sections, indexed by offset/8.
2543 Specifies the relocation symbol index used at a given toc offset. */
2544 unsigned *t_symndx;
2545 };
2546
2547 #define ppc64_elf_section_data(sec) \
2548 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2549
2550 static bfd_boolean
2551 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2552 {
2553 struct _ppc64_elf_section_data *sdata;
2554 bfd_size_type amt = sizeof (*sdata);
2555
2556 sdata = bfd_zalloc (abfd, amt);
2557 if (sdata == NULL)
2558 return FALSE;
2559 sec->used_by_bfd = sdata;
2560
2561 return _bfd_elf_new_section_hook (abfd, sec);
2562 }
2563
2564 static void *
2565 get_opd_info (asection * sec)
2566 {
2567 if (sec != NULL
2568 && ppc64_elf_section_data (sec) != NULL
2569 && ppc64_elf_section_data (sec)->opd.adjust != NULL)
2570 return ppc64_elf_section_data (sec)->opd.adjust;
2571 return NULL;
2572 }
2573 \f
2574 /* Parameters for the qsort hook. */
2575 static asection *synthetic_opd;
2576 static bfd_boolean synthetic_relocatable;
2577
2578 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2579
2580 static int
2581 compare_symbols (const void *ap, const void *bp)
2582 {
2583 const asymbol *a = * (const asymbol **) ap;
2584 const asymbol *b = * (const asymbol **) bp;
2585
2586 /* Section symbols first. */
2587 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2588 return -1;
2589 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2590 return 1;
2591
2592 /* then .opd symbols. */
2593 if (a->section == synthetic_opd && b->section != synthetic_opd)
2594 return -1;
2595 if (a->section != synthetic_opd && b->section == synthetic_opd)
2596 return 1;
2597
2598 /* then other code symbols. */
2599 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2600 == (SEC_CODE | SEC_ALLOC)
2601 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2602 != (SEC_CODE | SEC_ALLOC))
2603 return -1;
2604
2605 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2606 != (SEC_CODE | SEC_ALLOC)
2607 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2608 == (SEC_CODE | SEC_ALLOC))
2609 return 1;
2610
2611 if (synthetic_relocatable)
2612 {
2613 if (a->section->id < b->section->id)
2614 return -1;
2615
2616 if (a->section->id > b->section->id)
2617 return 1;
2618 }
2619
2620 if (a->value + a->section->vma < b->value + b->section->vma)
2621 return -1;
2622
2623 if (a->value + a->section->vma > b->value + b->section->vma)
2624 return 1;
2625
2626 return 0;
2627 }
2628
2629 /* Search SYMS for a symbol of the given VALUE. */
2630
2631 static asymbol *
2632 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
2633 {
2634 long mid;
2635
2636 if (id == -1)
2637 {
2638 while (lo < hi)
2639 {
2640 mid = (lo + hi) >> 1;
2641 if (syms[mid]->value + syms[mid]->section->vma < value)
2642 lo = mid + 1;
2643 else if (syms[mid]->value + syms[mid]->section->vma > value)
2644 hi = mid;
2645 else
2646 return syms[mid];
2647 }
2648 }
2649 else
2650 {
2651 while (lo < hi)
2652 {
2653 mid = (lo + hi) >> 1;
2654 if (syms[mid]->section->id < id)
2655 lo = mid + 1;
2656 else if (syms[mid]->section->id > id)
2657 hi = mid;
2658 else if (syms[mid]->value < value)
2659 lo = mid + 1;
2660 else if (syms[mid]->value > value)
2661 hi = mid;
2662 else
2663 return syms[mid];
2664 }
2665 }
2666 return NULL;
2667 }
2668
2669 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2670 entry syms. */
2671
2672 static long
2673 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2674 long static_count, asymbol **static_syms,
2675 long dyn_count, asymbol **dyn_syms,
2676 asymbol **ret)
2677 {
2678 asymbol *s;
2679 long i;
2680 long count;
2681 char *names;
2682 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2683 asection *opd;
2684 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2685 asymbol **syms;
2686
2687 *ret = NULL;
2688
2689 opd = bfd_get_section_by_name (abfd, ".opd");
2690 if (opd == NULL)
2691 return 0;
2692
2693 symcount = static_count;
2694 if (!relocatable)
2695 symcount += dyn_count;
2696 if (symcount == 0)
2697 return 0;
2698
2699 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2700 if (syms == NULL)
2701 return -1;
2702
2703 if (!relocatable && static_count != 0 && dyn_count != 0)
2704 {
2705 /* Use both symbol tables. */
2706 memcpy (syms, static_syms, static_count * sizeof (*syms));
2707 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2708 }
2709 else if (!relocatable && static_count == 0)
2710 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2711 else
2712 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2713
2714 synthetic_opd = opd;
2715 synthetic_relocatable = relocatable;
2716 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2717
2718 if (!relocatable && symcount > 1)
2719 {
2720 long j;
2721 /* Trim duplicate syms, since we may have merged the normal and
2722 dynamic symbols. Actually, we only care about syms that have
2723 different values, so trim any with the same value. */
2724 for (i = 1, j = 1; i < symcount; ++i)
2725 if (syms[i - 1]->value + syms[i - 1]->section->vma
2726 != syms[i]->value + syms[i]->section->vma)
2727 syms[j++] = syms[i];
2728 symcount = j;
2729 }
2730
2731 i = 0;
2732 if (syms[i]->section == opd)
2733 ++i;
2734 codesecsym = i;
2735
2736 for (; i < symcount; ++i)
2737 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2738 != (SEC_CODE | SEC_ALLOC))
2739 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2740 break;
2741 codesecsymend = i;
2742
2743 for (; i < symcount; ++i)
2744 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2745 break;
2746 secsymend = i;
2747
2748 for (; i < symcount; ++i)
2749 if (syms[i]->section != opd)
2750 break;
2751 opdsymend = i;
2752
2753 for (; i < symcount; ++i)
2754 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2755 != (SEC_CODE | SEC_ALLOC))
2756 break;
2757 symcount = i;
2758
2759 count = 0;
2760 if (opdsymend == secsymend)
2761 goto done;
2762
2763 if (relocatable)
2764 {
2765 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2766 arelent *r;
2767 size_t size;
2768 long relcount;
2769
2770 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2771 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2772 if (relcount == 0)
2773 goto done;
2774
2775 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2776 {
2777 count = -1;
2778 goto done;
2779 }
2780
2781 size = 0;
2782 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2783 {
2784 asymbol *sym;
2785
2786 while (r < opd->relocation + relcount
2787 && r->address < syms[i]->value + opd->vma)
2788 ++r;
2789
2790 if (r == opd->relocation + relcount)
2791 break;
2792
2793 if (r->address != syms[i]->value + opd->vma)
2794 continue;
2795
2796 if (r->howto->type != R_PPC64_ADDR64)
2797 continue;
2798
2799 sym = *r->sym_ptr_ptr;
2800 if (!sym_exists_at (syms, opdsymend, symcount,
2801 sym->section->id, sym->value + r->addend))
2802 {
2803 ++count;
2804 size += sizeof (asymbol);
2805 size += strlen (syms[i]->name) + 2;
2806 }
2807 }
2808
2809 s = *ret = bfd_malloc (size);
2810 if (s == NULL)
2811 {
2812 count = -1;
2813 goto done;
2814 }
2815
2816 names = (char *) (s + count);
2817
2818 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2819 {
2820 asymbol *sym;
2821
2822 while (r < opd->relocation + relcount
2823 && r->address < syms[i]->value + opd->vma)
2824 ++r;
2825
2826 if (r == opd->relocation + relcount)
2827 break;
2828
2829 if (r->address != syms[i]->value + opd->vma)
2830 continue;
2831
2832 if (r->howto->type != R_PPC64_ADDR64)
2833 continue;
2834
2835 sym = *r->sym_ptr_ptr;
2836 if (!sym_exists_at (syms, opdsymend, symcount,
2837 sym->section->id, sym->value + r->addend))
2838 {
2839 size_t len;
2840
2841 *s = *syms[i];
2842 s->section = sym->section;
2843 s->value = sym->value + r->addend;
2844 s->name = names;
2845 *names++ = '.';
2846 len = strlen (syms[i]->name);
2847 memcpy (names, syms[i]->name, len + 1);
2848 names += len + 1;
2849 s++;
2850 }
2851 }
2852 }
2853 else
2854 {
2855 bfd_byte *contents;
2856 size_t size;
2857
2858 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2859 {
2860 if (contents)
2861 {
2862 free_contents_and_exit:
2863 free (contents);
2864 }
2865 count = -1;
2866 goto done;
2867 }
2868
2869 size = 0;
2870 for (i = secsymend; i < opdsymend; ++i)
2871 {
2872 bfd_vma ent;
2873
2874 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2875 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2876 {
2877 ++count;
2878 size += sizeof (asymbol);
2879 size += strlen (syms[i]->name) + 2;
2880 }
2881 }
2882
2883 s = *ret = bfd_malloc (size);
2884 if (s == NULL)
2885 goto free_contents_and_exit;
2886
2887 names = (char *) (s + count);
2888
2889 for (i = secsymend; i < opdsymend; ++i)
2890 {
2891 bfd_vma ent;
2892
2893 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2894 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2895 {
2896 long lo, hi;
2897 size_t len;
2898 asection *sec = abfd->sections;
2899
2900 *s = *syms[i];
2901 lo = codesecsym;
2902 hi = codesecsymend;
2903 while (lo < hi)
2904 {
2905 long mid = (lo + hi) >> 1;
2906 if (syms[mid]->section->vma < ent)
2907 lo = mid + 1;
2908 else if (syms[mid]->section->vma > ent)
2909 hi = mid;
2910 else
2911 {
2912 sec = syms[mid]->section;
2913 break;
2914 }
2915 }
2916
2917 if (lo >= hi && lo > codesecsym)
2918 sec = syms[lo - 1]->section;
2919
2920 for (; sec != NULL; sec = sec->next)
2921 {
2922 if (sec->vma > ent)
2923 break;
2924 if ((sec->flags & SEC_ALLOC) == 0
2925 || (sec->flags & SEC_LOAD) == 0)
2926 break;
2927 if ((sec->flags & SEC_CODE) != 0)
2928 s->section = sec;
2929 }
2930 s->value = ent - s->section->vma;
2931 s->name = names;
2932 *names++ = '.';
2933 len = strlen (syms[i]->name);
2934 memcpy (names, syms[i]->name, len + 1);
2935 names += len + 1;
2936 s++;
2937 }
2938 }
2939 free (contents);
2940 }
2941
2942 done:
2943 free (syms);
2944 return count;
2945 }
2946 \f
2947 /* The following functions are specific to the ELF linker, while
2948 functions above are used generally. Those named ppc64_elf_* are
2949 called by the main ELF linker code. They appear in this file more
2950 or less in the order in which they are called. eg.
2951 ppc64_elf_check_relocs is called early in the link process,
2952 ppc64_elf_finish_dynamic_sections is one of the last functions
2953 called.
2954
2955 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2956 functions have both a function code symbol and a function descriptor
2957 symbol. A call to foo in a relocatable object file looks like:
2958
2959 . .text
2960 . x:
2961 . bl .foo
2962 . nop
2963
2964 The function definition in another object file might be:
2965
2966 . .section .opd
2967 . foo: .quad .foo
2968 . .quad .TOC.@tocbase
2969 . .quad 0
2970 .
2971 . .text
2972 . .foo: blr
2973
2974 When the linker resolves the call during a static link, the branch
2975 unsurprisingly just goes to .foo and the .opd information is unused.
2976 If the function definition is in a shared library, things are a little
2977 different: The call goes via a plt call stub, the opd information gets
2978 copied to the plt, and the linker patches the nop.
2979
2980 . x:
2981 . bl .foo_stub
2982 . ld 2,40(1)
2983 .
2984 .
2985 . .foo_stub:
2986 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
2987 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
2988 . std 2,40(1) # this is the general idea
2989 . ld 11,0(12)
2990 . ld 2,8(12)
2991 . mtctr 11
2992 . ld 11,16(12)
2993 . bctr
2994 .
2995 . .section .plt
2996 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2997
2998 The "reloc ()" notation is supposed to indicate that the linker emits
2999 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3000 copying.
3001
3002 What are the difficulties here? Well, firstly, the relocations
3003 examined by the linker in check_relocs are against the function code
3004 sym .foo, while the dynamic relocation in the plt is emitted against
3005 the function descriptor symbol, foo. Somewhere along the line, we need
3006 to carefully copy dynamic link information from one symbol to the other.
3007 Secondly, the generic part of the elf linker will make .foo a dynamic
3008 symbol as is normal for most other backends. We need foo dynamic
3009 instead, at least for an application final link. However, when
3010 creating a shared library containing foo, we need to have both symbols
3011 dynamic so that references to .foo are satisfied during the early
3012 stages of linking. Otherwise the linker might decide to pull in a
3013 definition from some other object, eg. a static library.
3014
3015 Update: As of August 2004, we support a new convention. Function
3016 calls may use the function descriptor symbol, ie. "bl foo". This
3017 behaves exactly as "bl .foo". */
3018
3019 /* The linker needs to keep track of the number of relocs that it
3020 decides to copy as dynamic relocs in check_relocs for each symbol.
3021 This is so that it can later discard them if they are found to be
3022 unnecessary. We store the information in a field extending the
3023 regular ELF linker hash table. */
3024
3025 struct ppc_dyn_relocs
3026 {
3027 struct ppc_dyn_relocs *next;
3028
3029 /* The input section of the reloc. */
3030 asection *sec;
3031
3032 /* Total number of relocs copied for the input section. */
3033 bfd_size_type count;
3034
3035 /* Number of pc-relative relocs copied for the input section. */
3036 bfd_size_type pc_count;
3037 };
3038
3039 /* Track GOT entries needed for a given symbol. We might need more
3040 than one got entry per symbol. */
3041 struct got_entry
3042 {
3043 struct got_entry *next;
3044
3045 /* The symbol addend that we'll be placing in the GOT. */
3046 bfd_vma addend;
3047
3048 /* Unlike other ELF targets, we use separate GOT entries for the same
3049 symbol referenced from different input files. This is to support
3050 automatic multiple TOC/GOT sections, where the TOC base can vary
3051 from one input file to another.
3052
3053 Point to the BFD owning this GOT entry. */
3054 bfd *owner;
3055
3056 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3057 TLS_TPREL or TLS_DTPREL for tls entries. */
3058 char tls_type;
3059
3060 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
3061 union
3062 {
3063 bfd_signed_vma refcount;
3064 bfd_vma offset;
3065 } got;
3066 };
3067
3068 /* The same for PLT. */
3069 struct plt_entry
3070 {
3071 struct plt_entry *next;
3072
3073 bfd_vma addend;
3074
3075 union
3076 {
3077 bfd_signed_vma refcount;
3078 bfd_vma offset;
3079 } plt;
3080 };
3081
3082 /* Of those relocs that might be copied as dynamic relocs, this macro
3083 selects those that must be copied when linking a shared library,
3084 even when the symbol is local. */
3085
3086 #define MUST_BE_DYN_RELOC(RTYPE) \
3087 ((RTYPE) != R_PPC64_REL32 \
3088 && (RTYPE) != R_PPC64_REL64 \
3089 && (RTYPE) != R_PPC64_REL30)
3090
3091 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3092 copying dynamic variables from a shared lib into an app's dynbss
3093 section, and instead use a dynamic relocation to point into the
3094 shared lib. With code that gcc generates, it's vital that this be
3095 enabled; In the PowerPC64 ABI, the address of a function is actually
3096 the address of a function descriptor, which resides in the .opd
3097 section. gcc uses the descriptor directly rather than going via the
3098 GOT as some other ABI's do, which means that initialized function
3099 pointers must reference the descriptor. Thus, a function pointer
3100 initialized to the address of a function in a shared library will
3101 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3102 redefines the function descriptor symbol to point to the copy. This
3103 presents a problem as a plt entry for that function is also
3104 initialized from the function descriptor symbol and the copy reloc
3105 may not be initialized first. */
3106 #define ELIMINATE_COPY_RELOCS 1
3107
3108 /* Section name for stubs is the associated section name plus this
3109 string. */
3110 #define STUB_SUFFIX ".stub"
3111
3112 /* Linker stubs.
3113 ppc_stub_long_branch:
3114 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3115 destination, but a 24 bit branch in a stub section will reach.
3116 . b dest
3117
3118 ppc_stub_plt_branch:
3119 Similar to the above, but a 24 bit branch in the stub section won't
3120 reach its destination.
3121 . addis %r12,%r2,xxx@toc@ha
3122 . ld %r11,xxx@toc@l(%r12)
3123 . mtctr %r11
3124 . bctr
3125
3126 ppc_stub_plt_call:
3127 Used to call a function in a shared library. If it so happens that
3128 the plt entry referenced crosses a 64k boundary, then an extra
3129 "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3130 xxx+16 as appropriate.
3131 . addis %r12,%r2,xxx@toc@ha
3132 . std %r2,40(%r1)
3133 . ld %r11,xxx+0@toc@l(%r12)
3134 . ld %r2,xxx+8@toc@l(%r12)
3135 . mtctr %r11
3136 . ld %r11,xxx+16@toc@l(%r12)
3137 . bctr
3138
3139 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3140 code to adjust the value and save r2 to support multiple toc sections.
3141 A ppc_stub_long_branch with an r2 offset looks like:
3142 . std %r2,40(%r1)
3143 . addis %r2,%r2,off@ha
3144 . addi %r2,%r2,off@l
3145 . b dest
3146
3147 A ppc_stub_plt_branch with an r2 offset looks like:
3148 . std %r2,40(%r1)
3149 . addis %r12,%r2,xxx@toc@ha
3150 . ld %r11,xxx@toc@l(%r12)
3151 . addis %r2,%r2,off@ha
3152 . addi %r2,%r2,off@l
3153 . mtctr %r11
3154 . bctr
3155 */
3156
3157 enum ppc_stub_type {
3158 ppc_stub_none,
3159 ppc_stub_long_branch,
3160 ppc_stub_long_branch_r2off,
3161 ppc_stub_plt_branch,
3162 ppc_stub_plt_branch_r2off,
3163 ppc_stub_plt_call
3164 };
3165
3166 struct ppc_stub_hash_entry {
3167
3168 /* Base hash table entry structure. */
3169 struct bfd_hash_entry root;
3170
3171 enum ppc_stub_type stub_type;
3172
3173 /* The stub section. */
3174 asection *stub_sec;
3175
3176 /* Offset within stub_sec of the beginning of this stub. */
3177 bfd_vma stub_offset;
3178
3179 /* Given the symbol's value and its section we can determine its final
3180 value when building the stubs (so the stub knows where to jump. */
3181 bfd_vma target_value;
3182 asection *target_section;
3183
3184 /* The symbol table entry, if any, that this was derived from. */
3185 struct ppc_link_hash_entry *h;
3186
3187 /* And the reloc addend that this was derived from. */
3188 bfd_vma addend;
3189
3190 /* Where this stub is being called from, or, in the case of combined
3191 stub sections, the first input section in the group. */
3192 asection *id_sec;
3193 };
3194
3195 struct ppc_branch_hash_entry {
3196
3197 /* Base hash table entry structure. */
3198 struct bfd_hash_entry root;
3199
3200 /* Offset within branch lookup table. */
3201 unsigned int offset;
3202
3203 /* Generation marker. */
3204 unsigned int iter;
3205 };
3206
3207 struct ppc_link_hash_entry
3208 {
3209 struct elf_link_hash_entry elf;
3210
3211 /* A pointer to the most recently used stub hash entry against this
3212 symbol. */
3213 struct ppc_stub_hash_entry *stub_cache;
3214
3215 /* Track dynamic relocs copied for this symbol. */
3216 struct ppc_dyn_relocs *dyn_relocs;
3217
3218 /* Link between function code and descriptor symbols. */
3219 struct ppc_link_hash_entry *oh;
3220
3221 /* Flag function code and descriptor symbols. */
3222 unsigned int is_func:1;
3223 unsigned int is_func_descriptor:1;
3224 unsigned int fake:1;
3225
3226 /* Whether global opd/toc sym has been adjusted or not.
3227 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3228 should be set for all globals defined in any opd/toc section. */
3229 unsigned int adjust_done:1;
3230
3231 /* Set if we twiddled this symbol to weak at some stage. */
3232 unsigned int was_undefined:1;
3233
3234 /* Contexts in which symbol is used in the GOT (or TOC).
3235 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3236 corresponding relocs are encountered during check_relocs.
3237 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3238 indicate the corresponding GOT entry type is not needed.
3239 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3240 a TPREL one. We use a separate flag rather than setting TPREL
3241 just for convenience in distinguishing the two cases. */
3242 #define TLS_GD 1 /* GD reloc. */
3243 #define TLS_LD 2 /* LD reloc. */
3244 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3245 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3246 #define TLS_TLS 16 /* Any TLS reloc. */
3247 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3248 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3249 char tls_mask;
3250 };
3251
3252 /* ppc64 ELF linker hash table. */
3253
3254 struct ppc_link_hash_table
3255 {
3256 struct elf_link_hash_table elf;
3257
3258 /* The stub hash table. */
3259 struct bfd_hash_table stub_hash_table;
3260
3261 /* Another hash table for plt_branch stubs. */
3262 struct bfd_hash_table branch_hash_table;
3263
3264 /* Linker stub bfd. */
3265 bfd *stub_bfd;
3266
3267 /* Linker call-backs. */
3268 asection * (*add_stub_section) (const char *, asection *);
3269 void (*layout_sections_again) (void);
3270
3271 /* Array to keep track of which stub sections have been created, and
3272 information on stub grouping. */
3273 struct map_stub {
3274 /* This is the section to which stubs in the group will be attached. */
3275 asection *link_sec;
3276 /* The stub section. */
3277 asection *stub_sec;
3278 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3279 bfd_vma toc_off;
3280 } *stub_group;
3281
3282 /* Temp used when calculating TOC pointers. */
3283 bfd_vma toc_curr;
3284
3285 /* Highest input section id. */
3286 int top_id;
3287
3288 /* Highest output section index. */
3289 int top_index;
3290
3291 /* List of input sections for each output section. */
3292 asection **input_list;
3293
3294 /* Short-cuts to get to dynamic linker sections. */
3295 asection *got;
3296 asection *plt;
3297 asection *relplt;
3298 asection *dynbss;
3299 asection *relbss;
3300 asection *glink;
3301 asection *sfpr;
3302 asection *brlt;
3303 asection *relbrlt;
3304
3305 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3306 struct ppc_link_hash_entry *tls_get_addr;
3307 struct ppc_link_hash_entry *tls_get_addr_fd;
3308
3309 /* Statistics. */
3310 unsigned long stub_count[ppc_stub_plt_call];
3311
3312 /* Number of stubs against global syms. */
3313 unsigned long stub_globals;
3314
3315 /* Set if we should emit symbols for stubs. */
3316 unsigned int emit_stub_syms:1;
3317
3318 /* Support for multiple toc sections. */
3319 unsigned int no_multi_toc:1;
3320 unsigned int multi_toc_needed:1;
3321
3322 /* Set on error. */
3323 unsigned int stub_error:1;
3324
3325 /* Flag set when small branches are detected. Used to
3326 select suitable defaults for the stub group size. */
3327 unsigned int has_14bit_branch:1;
3328
3329 /* Temp used by ppc64_elf_check_directives. */
3330 unsigned int twiddled_syms:1;
3331
3332 /* Incremented every time we size stubs. */
3333 unsigned int stub_iteration;
3334
3335 /* Small local sym to section mapping cache. */
3336 struct sym_sec_cache sym_sec;
3337 };
3338
3339 /* Rename some of the generic section flags to better document how they
3340 are used here. */
3341 #define has_toc_reloc has_gp_reloc
3342 #define makes_toc_func_call need_finalize_relax
3343 #define call_check_in_progress reloc_done
3344
3345 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3346
3347 #define ppc_hash_table(p) \
3348 ((struct ppc_link_hash_table *) ((p)->hash))
3349
3350 #define ppc_stub_hash_lookup(table, string, create, copy) \
3351 ((struct ppc_stub_hash_entry *) \
3352 bfd_hash_lookup ((table), (string), (create), (copy)))
3353
3354 #define ppc_branch_hash_lookup(table, string, create, copy) \
3355 ((struct ppc_branch_hash_entry *) \
3356 bfd_hash_lookup ((table), (string), (create), (copy)))
3357
3358 /* Create an entry in the stub hash table. */
3359
3360 static struct bfd_hash_entry *
3361 stub_hash_newfunc (struct bfd_hash_entry *entry,
3362 struct bfd_hash_table *table,
3363 const char *string)
3364 {
3365 /* Allocate the structure if it has not already been allocated by a
3366 subclass. */
3367 if (entry == NULL)
3368 {
3369 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3370 if (entry == NULL)
3371 return entry;
3372 }
3373
3374 /* Call the allocation method of the superclass. */
3375 entry = bfd_hash_newfunc (entry, table, string);
3376 if (entry != NULL)
3377 {
3378 struct ppc_stub_hash_entry *eh;
3379
3380 /* Initialize the local fields. */
3381 eh = (struct ppc_stub_hash_entry *) entry;
3382 eh->stub_type = ppc_stub_none;
3383 eh->stub_sec = NULL;
3384 eh->stub_offset = 0;
3385 eh->target_value = 0;
3386 eh->target_section = NULL;
3387 eh->h = NULL;
3388 eh->id_sec = NULL;
3389 }
3390
3391 return entry;
3392 }
3393
3394 /* Create an entry in the branch hash table. */
3395
3396 static struct bfd_hash_entry *
3397 branch_hash_newfunc (struct bfd_hash_entry *entry,
3398 struct bfd_hash_table *table,
3399 const char *string)
3400 {
3401 /* Allocate the structure if it has not already been allocated by a
3402 subclass. */
3403 if (entry == NULL)
3404 {
3405 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3406 if (entry == NULL)
3407 return entry;
3408 }
3409
3410 /* Call the allocation method of the superclass. */
3411 entry = bfd_hash_newfunc (entry, table, string);
3412 if (entry != NULL)
3413 {
3414 struct ppc_branch_hash_entry *eh;
3415
3416 /* Initialize the local fields. */
3417 eh = (struct ppc_branch_hash_entry *) entry;
3418 eh->offset = 0;
3419 eh->iter = 0;
3420 }
3421
3422 return entry;
3423 }
3424
3425 /* Create an entry in a ppc64 ELF linker hash table. */
3426
3427 static struct bfd_hash_entry *
3428 link_hash_newfunc (struct bfd_hash_entry *entry,
3429 struct bfd_hash_table *table,
3430 const char *string)
3431 {
3432 /* Allocate the structure if it has not already been allocated by a
3433 subclass. */
3434 if (entry == NULL)
3435 {
3436 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3437 if (entry == NULL)
3438 return entry;
3439 }
3440
3441 /* Call the allocation method of the superclass. */
3442 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3443 if (entry != NULL)
3444 {
3445 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3446
3447 memset (&eh->stub_cache, 0,
3448 (sizeof (struct ppc_link_hash_entry)
3449 - offsetof (struct ppc_link_hash_entry, stub_cache)));
3450 }
3451
3452 return entry;
3453 }
3454
3455 /* Create a ppc64 ELF linker hash table. */
3456
3457 static struct bfd_link_hash_table *
3458 ppc64_elf_link_hash_table_create (bfd *abfd)
3459 {
3460 struct ppc_link_hash_table *htab;
3461 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3462
3463 htab = bfd_zmalloc (amt);
3464 if (htab == NULL)
3465 return NULL;
3466
3467 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3468 sizeof (struct ppc_link_hash_entry)))
3469 {
3470 free (htab);
3471 return NULL;
3472 }
3473
3474 /* Init the stub hash table too. */
3475 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3476 sizeof (struct ppc_stub_hash_entry)))
3477 return NULL;
3478
3479 /* And the branch hash table. */
3480 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3481 sizeof (struct ppc_branch_hash_entry)))
3482 return NULL;
3483
3484 /* Initializing two fields of the union is just cosmetic. We really
3485 only care about glist, but when compiled on a 32-bit host the
3486 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3487 debugger inspection of these fields look nicer. */
3488 htab->elf.init_got_refcount.refcount = 0;
3489 htab->elf.init_got_refcount.glist = NULL;
3490 htab->elf.init_plt_refcount.refcount = 0;
3491 htab->elf.init_plt_refcount.glist = NULL;
3492 htab->elf.init_got_offset.offset = 0;
3493 htab->elf.init_got_offset.glist = NULL;
3494 htab->elf.init_plt_offset.offset = 0;
3495 htab->elf.init_plt_offset.glist = NULL;
3496
3497 return &htab->elf.root;
3498 }
3499
3500 /* Free the derived linker hash table. */
3501
3502 static void
3503 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3504 {
3505 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3506
3507 bfd_hash_table_free (&ret->stub_hash_table);
3508 bfd_hash_table_free (&ret->branch_hash_table);
3509 _bfd_generic_link_hash_table_free (hash);
3510 }
3511
3512 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3513
3514 void
3515 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3516 {
3517 struct ppc_link_hash_table *htab;
3518
3519 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3520
3521 /* Always hook our dynamic sections into the first bfd, which is the
3522 linker created stub bfd. This ensures that the GOT header is at
3523 the start of the output TOC section. */
3524 htab = ppc_hash_table (info);
3525 htab->stub_bfd = abfd;
3526 htab->elf.dynobj = abfd;
3527 }
3528
3529 /* Build a name for an entry in the stub hash table. */
3530
3531 static char *
3532 ppc_stub_name (const asection *input_section,
3533 const asection *sym_sec,
3534 const struct ppc_link_hash_entry *h,
3535 const Elf_Internal_Rela *rel)
3536 {
3537 char *stub_name;
3538 bfd_size_type len;
3539
3540 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3541 offsets from a sym as a branch target? In fact, we could
3542 probably assume the addend is always zero. */
3543 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3544
3545 if (h)
3546 {
3547 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3548 stub_name = bfd_malloc (len);
3549 if (stub_name == NULL)
3550 return stub_name;
3551
3552 sprintf (stub_name, "%08x.%s+%x",
3553 input_section->id & 0xffffffff,
3554 h->elf.root.root.string,
3555 (int) rel->r_addend & 0xffffffff);
3556 }
3557 else
3558 {
3559 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3560 stub_name = bfd_malloc (len);
3561 if (stub_name == NULL)
3562 return stub_name;
3563
3564 sprintf (stub_name, "%08x.%x:%x+%x",
3565 input_section->id & 0xffffffff,
3566 sym_sec->id & 0xffffffff,
3567 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3568 (int) rel->r_addend & 0xffffffff);
3569 }
3570 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3571 stub_name[len - 2] = 0;
3572 return stub_name;
3573 }
3574
3575 /* Look up an entry in the stub hash. Stub entries are cached because
3576 creating the stub name takes a bit of time. */
3577
3578 static struct ppc_stub_hash_entry *
3579 ppc_get_stub_entry (const asection *input_section,
3580 const asection *sym_sec,
3581 struct ppc_link_hash_entry *h,
3582 const Elf_Internal_Rela *rel,
3583 struct ppc_link_hash_table *htab)
3584 {
3585 struct ppc_stub_hash_entry *stub_entry;
3586 const asection *id_sec;
3587
3588 /* If this input section is part of a group of sections sharing one
3589 stub section, then use the id of the first section in the group.
3590 Stub names need to include a section id, as there may well be
3591 more than one stub used to reach say, printf, and we need to
3592 distinguish between them. */
3593 id_sec = htab->stub_group[input_section->id].link_sec;
3594
3595 if (h != NULL && h->stub_cache != NULL
3596 && h->stub_cache->h == h
3597 && h->stub_cache->id_sec == id_sec)
3598 {
3599 stub_entry = h->stub_cache;
3600 }
3601 else
3602 {
3603 char *stub_name;
3604
3605 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3606 if (stub_name == NULL)
3607 return NULL;
3608
3609 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3610 stub_name, FALSE, FALSE);
3611 if (h != NULL)
3612 h->stub_cache = stub_entry;
3613
3614 free (stub_name);
3615 }
3616
3617 return stub_entry;
3618 }
3619
3620 /* Add a new stub entry to the stub hash. Not all fields of the new
3621 stub entry are initialised. */
3622
3623 static struct ppc_stub_hash_entry *
3624 ppc_add_stub (const char *stub_name,
3625 asection *section,
3626 struct ppc_link_hash_table *htab)
3627 {
3628 asection *link_sec;
3629 asection *stub_sec;
3630 struct ppc_stub_hash_entry *stub_entry;
3631
3632 link_sec = htab->stub_group[section->id].link_sec;
3633 stub_sec = htab->stub_group[section->id].stub_sec;
3634 if (stub_sec == NULL)
3635 {
3636 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3637 if (stub_sec == NULL)
3638 {
3639 size_t namelen;
3640 bfd_size_type len;
3641 char *s_name;
3642
3643 namelen = strlen (link_sec->name);
3644 len = namelen + sizeof (STUB_SUFFIX);
3645 s_name = bfd_alloc (htab->stub_bfd, len);
3646 if (s_name == NULL)
3647 return NULL;
3648
3649 memcpy (s_name, link_sec->name, namelen);
3650 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3651 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3652 if (stub_sec == NULL)
3653 return NULL;
3654 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3655 }
3656 htab->stub_group[section->id].stub_sec = stub_sec;
3657 }
3658
3659 /* Enter this entry into the linker stub hash table. */
3660 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3661 TRUE, FALSE);
3662 if (stub_entry == NULL)
3663 {
3664 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3665 section->owner, stub_name);
3666 return NULL;
3667 }
3668
3669 stub_entry->stub_sec = stub_sec;
3670 stub_entry->stub_offset = 0;
3671 stub_entry->id_sec = link_sec;
3672 return stub_entry;
3673 }
3674
3675 /* Create sections for linker generated code. */
3676
3677 static bfd_boolean
3678 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3679 {
3680 struct ppc_link_hash_table *htab;
3681 flagword flags;
3682
3683 htab = ppc_hash_table (info);
3684
3685 /* Create .sfpr for code to save and restore fp regs. */
3686 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3687 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3688 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3689 flags);
3690 if (htab->sfpr == NULL
3691 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3692 return FALSE;
3693
3694 /* Create .glink for lazy dynamic linking support. */
3695 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3696 flags);
3697 if (htab->glink == NULL
3698 || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
3699 return FALSE;
3700
3701 /* Create branch lookup table for plt_branch stubs. */
3702 if (info->shared)
3703 {
3704 flags = (SEC_ALLOC | SEC_LOAD
3705 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3706 htab->brlt
3707 = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro.brlt",
3708 flags);
3709 }
3710 else
3711 {
3712 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3713 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3714 htab->brlt
3715 = bfd_make_section_anyway_with_flags (dynobj, ".rodata.brlt", flags);
3716 }
3717
3718 if (htab->brlt == NULL
3719 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3720 return FALSE;
3721
3722 if (info->shared)
3723 {
3724 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3725 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3726 htab->relbrlt
3727 = bfd_make_section_anyway_with_flags (dynobj, ".rela.data.rel.ro.brlt",
3728 flags);
3729 }
3730 else if (info->emitrelocations)
3731 {
3732 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3733 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3734 htab->relbrlt
3735 = bfd_make_section_anyway_with_flags (dynobj, ".rela.rodata.brlt",
3736 flags);
3737 }
3738 else
3739 return TRUE;
3740
3741 if (!htab->relbrlt
3742 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3743 return FALSE;
3744
3745 return TRUE;
3746 }
3747
3748 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3749 not already done. */
3750
3751 static bfd_boolean
3752 create_got_section (bfd *abfd, struct bfd_link_info *info)
3753 {
3754 asection *got, *relgot;
3755 flagword flags;
3756 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3757
3758 if (!htab->got)
3759 {
3760 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3761 return FALSE;
3762
3763 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3764 if (!htab->got)
3765 abort ();
3766 }
3767
3768 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3769 | SEC_LINKER_CREATED);
3770
3771 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3772 if (!got
3773 || !bfd_set_section_alignment (abfd, got, 3))
3774 return FALSE;
3775
3776 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3777 flags | SEC_READONLY);
3778 if (!relgot
3779 || ! bfd_set_section_alignment (abfd, relgot, 3))
3780 return FALSE;
3781
3782 ppc64_elf_tdata (abfd)->got = got;
3783 ppc64_elf_tdata (abfd)->relgot = relgot;
3784 return TRUE;
3785 }
3786
3787 /* Create the dynamic sections, and set up shortcuts. */
3788
3789 static bfd_boolean
3790 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3791 {
3792 struct ppc_link_hash_table *htab;
3793
3794 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3795 return FALSE;
3796
3797 htab = ppc_hash_table (info);
3798 if (!htab->got)
3799 htab->got = bfd_get_section_by_name (dynobj, ".got");
3800 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3801 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3802 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3803 if (!info->shared)
3804 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3805
3806 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3807 || (!info->shared && !htab->relbss))
3808 abort ();
3809
3810 return TRUE;
3811 }
3812
3813 /* Merge PLT info on FROM with that on TO. */
3814
3815 static void
3816 move_plt_plist (struct ppc_link_hash_entry *from,
3817 struct ppc_link_hash_entry *to)
3818 {
3819 if (from->elf.plt.plist != NULL)
3820 {
3821 if (to->elf.plt.plist != NULL)
3822 {
3823 struct plt_entry **entp;
3824 struct plt_entry *ent;
3825
3826 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3827 {
3828 struct plt_entry *dent;
3829
3830 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3831 if (dent->addend == ent->addend)
3832 {
3833 dent->plt.refcount += ent->plt.refcount;
3834 *entp = ent->next;
3835 break;
3836 }
3837 if (dent == NULL)
3838 entp = &ent->next;
3839 }
3840 *entp = to->elf.plt.plist;
3841 }
3842
3843 to->elf.plt.plist = from->elf.plt.plist;
3844 from->elf.plt.plist = NULL;
3845 }
3846 }
3847
3848 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3849
3850 static void
3851 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3852 struct elf_link_hash_entry *dir,
3853 struct elf_link_hash_entry *ind)
3854 {
3855 struct ppc_link_hash_entry *edir, *eind;
3856
3857 edir = (struct ppc_link_hash_entry *) dir;
3858 eind = (struct ppc_link_hash_entry *) ind;
3859
3860 /* Copy over any dynamic relocs we may have on the indirect sym. */
3861 if (eind->dyn_relocs != NULL)
3862 {
3863 if (edir->dyn_relocs != NULL)
3864 {
3865 struct ppc_dyn_relocs **pp;
3866 struct ppc_dyn_relocs *p;
3867
3868 /* Add reloc counts against the indirect sym to the direct sym
3869 list. Merge any entries against the same section. */
3870 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3871 {
3872 struct ppc_dyn_relocs *q;
3873
3874 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3875 if (q->sec == p->sec)
3876 {
3877 q->pc_count += p->pc_count;
3878 q->count += p->count;
3879 *pp = p->next;
3880 break;
3881 }
3882 if (q == NULL)
3883 pp = &p->next;
3884 }
3885 *pp = edir->dyn_relocs;
3886 }
3887
3888 edir->dyn_relocs = eind->dyn_relocs;
3889 eind->dyn_relocs = NULL;
3890 }
3891
3892 edir->is_func |= eind->is_func;
3893 edir->is_func_descriptor |= eind->is_func_descriptor;
3894 edir->tls_mask |= eind->tls_mask;
3895
3896 /* If called to transfer flags for a weakdef during processing
3897 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
3898 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3899 if (!(ELIMINATE_COPY_RELOCS
3900 && eind->elf.root.type != bfd_link_hash_indirect
3901 && edir->elf.dynamic_adjusted))
3902 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3903
3904 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3905 edir->elf.ref_regular |= eind->elf.ref_regular;
3906 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3907 edir->elf.needs_plt |= eind->elf.needs_plt;
3908
3909 /* If we were called to copy over info for a weak sym, that's all. */
3910 if (eind->elf.root.type != bfd_link_hash_indirect)
3911 return;
3912
3913 /* Copy over got entries that we may have already seen to the
3914 symbol which just became indirect. */
3915 if (eind->elf.got.glist != NULL)
3916 {
3917 if (edir->elf.got.glist != NULL)
3918 {
3919 struct got_entry **entp;
3920 struct got_entry *ent;
3921
3922 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3923 {
3924 struct got_entry *dent;
3925
3926 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3927 if (dent->addend == ent->addend
3928 && dent->owner == ent->owner
3929 && dent->tls_type == ent->tls_type)
3930 {
3931 dent->got.refcount += ent->got.refcount;
3932 *entp = ent->next;
3933 break;
3934 }
3935 if (dent == NULL)
3936 entp = &ent->next;
3937 }
3938 *entp = edir->elf.got.glist;
3939 }
3940
3941 edir->elf.got.glist = eind->elf.got.glist;
3942 eind->elf.got.glist = NULL;
3943 }
3944
3945 /* And plt entries. */
3946 move_plt_plist (eind, edir);
3947
3948 if (eind->elf.dynindx != -1)
3949 {
3950 if (edir->elf.dynindx != -1)
3951 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3952 edir->elf.dynstr_index);
3953 edir->elf.dynindx = eind->elf.dynindx;
3954 edir->elf.dynstr_index = eind->elf.dynstr_index;
3955 eind->elf.dynindx = -1;
3956 eind->elf.dynstr_index = 0;
3957 }
3958 }
3959
3960 /* Find the function descriptor hash entry from the given function code
3961 hash entry FH. Link the entries via their OH fields. */
3962
3963 static struct ppc_link_hash_entry *
3964 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3965 {
3966 struct ppc_link_hash_entry *fdh = fh->oh;
3967
3968 if (fdh == NULL)
3969 {
3970 const char *fd_name = fh->elf.root.root.string + 1;
3971
3972 fdh = (struct ppc_link_hash_entry *)
3973 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3974 if (fdh != NULL)
3975 {
3976 fdh->is_func_descriptor = 1;
3977 fdh->oh = fh;
3978 fh->is_func = 1;
3979 fh->oh = fdh;
3980 }
3981 }
3982
3983 return fdh;
3984 }
3985
3986 /* Make a fake function descriptor sym for the code sym FH. */
3987
3988 static struct ppc_link_hash_entry *
3989 make_fdh (struct bfd_link_info *info,
3990 struct ppc_link_hash_entry *fh)
3991 {
3992 bfd *abfd;
3993 asymbol *newsym;
3994 struct bfd_link_hash_entry *bh;
3995 struct ppc_link_hash_entry *fdh;
3996
3997 abfd = fh->elf.root.u.undef.abfd;
3998 newsym = bfd_make_empty_symbol (abfd);
3999 newsym->name = fh->elf.root.root.string + 1;
4000 newsym->section = bfd_und_section_ptr;
4001 newsym->value = 0;
4002 newsym->flags = BSF_WEAK;
4003
4004 bh = NULL;
4005 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4006 newsym->flags, newsym->section,
4007 newsym->value, NULL, FALSE, FALSE,
4008 &bh))
4009 return NULL;
4010
4011 fdh = (struct ppc_link_hash_entry *) bh;
4012 fdh->elf.non_elf = 0;
4013 fdh->fake = 1;
4014 fdh->is_func_descriptor = 1;
4015 fdh->oh = fh;
4016 fh->is_func = 1;
4017 fh->oh = fdh;
4018 return fdh;
4019 }
4020
4021 /* Hacks to support old ABI code.
4022 When making function calls, old ABI code references function entry
4023 points (dot symbols), while new ABI code references the function
4024 descriptor symbol. We need to make any combination of reference and
4025 definition work together, without breaking archive linking.
4026
4027 For a defined function "foo" and an undefined call to "bar":
4028 An old object defines "foo" and ".foo", references ".bar" (possibly
4029 "bar" too).
4030 A new object defines "foo" and references "bar".
4031
4032 A new object thus has no problem with its undefined symbols being
4033 satisfied by definitions in an old object. On the other hand, the
4034 old object won't have ".bar" satisfied by a new object. */
4035
4036 /* Fix function descriptor symbols defined in .opd sections to be
4037 function type. */
4038
4039 static bfd_boolean
4040 ppc64_elf_add_symbol_hook (bfd *ibfd,
4041 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4042 Elf_Internal_Sym *isym,
4043 const char **name,
4044 flagword *flags ATTRIBUTE_UNUSED,
4045 asection **sec,
4046 bfd_vma *value ATTRIBUTE_UNUSED)
4047 {
4048 if (*sec != NULL
4049 && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
4050 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4051
4052 if ((*name)[0] == '.'
4053 && ELF_ST_BIND (isym->st_info) == STB_GLOBAL
4054 && ELF_ST_TYPE (isym->st_info) < STT_SECTION
4055 && is_ppc64_elf_target (ibfd->xvec))
4056 ppc64_elf_tdata (ibfd)->u.has_dotsym = 1;
4057
4058 return TRUE;
4059 }
4060
4061 /* This function makes an old ABI object reference to ".bar" cause the
4062 inclusion of a new ABI object archive that defines "bar".
4063 NAME is a symbol defined in an archive. Return a symbol in the hash
4064 table that might be satisfied by the archive symbols. */
4065
4066 static struct elf_link_hash_entry *
4067 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4068 struct bfd_link_info *info,
4069 const char *name)
4070 {
4071 struct elf_link_hash_entry *h;
4072 char *dot_name;
4073 size_t len;
4074
4075 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4076 if (h != NULL
4077 /* Don't return this sym if it is a fake function descriptor
4078 created by add_symbol_adjust. */
4079 && !(h->root.type == bfd_link_hash_undefweak
4080 && ((struct ppc_link_hash_entry *) h)->fake))
4081 return h;
4082
4083 if (name[0] == '.')
4084 return h;
4085
4086 len = strlen (name);
4087 dot_name = bfd_alloc (abfd, len + 2);
4088 if (dot_name == NULL)
4089 return (struct elf_link_hash_entry *) 0 - 1;
4090 dot_name[0] = '.';
4091 memcpy (dot_name + 1, name, len + 1);
4092 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4093 bfd_release (abfd, dot_name);
4094 return h;
4095 }
4096
4097 /* This function satisfies all old ABI object references to ".bar" if a
4098 new ABI object defines "bar". Well, at least, undefined dot symbols
4099 are made weak. This stops later archive searches from including an
4100 object if we already have a function descriptor definition. It also
4101 prevents the linker complaining about undefined symbols.
4102 We also check and correct mismatched symbol visibility here. The
4103 most restrictive visibility of the function descriptor and the
4104 function entry symbol is used. */
4105
4106 struct add_symbol_adjust_data
4107 {
4108 struct bfd_link_info *info;
4109 bfd_boolean ok;
4110 };
4111
4112 static bfd_boolean
4113 add_symbol_adjust (struct elf_link_hash_entry *h, void *inf)
4114 {
4115 struct add_symbol_adjust_data *data;
4116 struct ppc_link_hash_table *htab;
4117 struct ppc_link_hash_entry *eh;
4118 struct ppc_link_hash_entry *fdh;
4119
4120 if (h->root.type == bfd_link_hash_indirect)
4121 return TRUE;
4122
4123 if (h->root.type == bfd_link_hash_warning)
4124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4125
4126 if (h->root.root.string[0] != '.')
4127 return TRUE;
4128
4129 data = inf;
4130 htab = ppc_hash_table (data->info);
4131 eh = (struct ppc_link_hash_entry *) h;
4132 fdh = get_fdh (eh, htab);
4133 if (fdh == NULL
4134 && !data->info->relocatable
4135 && (eh->elf.root.type == bfd_link_hash_undefined
4136 || eh->elf.root.type == bfd_link_hash_undefweak)
4137 && eh->elf.ref_regular)
4138 {
4139 /* Make an undefweak function descriptor sym, which is enough to
4140 pull in an --as-needed shared lib, but won't cause link
4141 errors. Archives are handled elsewhere. */
4142 fdh = make_fdh (data->info, eh);
4143 if (fdh == NULL)
4144 data->ok = FALSE;
4145 else
4146 fdh->elf.ref_regular = 1;
4147 }
4148 else if (fdh != NULL)
4149 {
4150 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4151 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4152 if (entry_vis < descr_vis)
4153 fdh->elf.other += entry_vis - descr_vis;
4154 else if (entry_vis > descr_vis)
4155 eh->elf.other += descr_vis - entry_vis;
4156
4157 if ((fdh->elf.root.type == bfd_link_hash_defined
4158 || fdh->elf.root.type == bfd_link_hash_defweak)
4159 && eh->elf.root.type == bfd_link_hash_undefined)
4160 {
4161 eh->elf.root.type = bfd_link_hash_undefweak;
4162 eh->was_undefined = 1;
4163 htab->twiddled_syms = 1;
4164 }
4165 }
4166
4167 return TRUE;
4168 }
4169
4170 static bfd_boolean
4171 ppc64_elf_check_directives (bfd *abfd, struct bfd_link_info *info)
4172 {
4173 struct ppc_link_hash_table *htab;
4174 struct add_symbol_adjust_data data;
4175
4176 if (!is_ppc64_elf_target (abfd->xvec))
4177 return TRUE;
4178
4179 if (!ppc64_elf_tdata (abfd)->u.has_dotsym)
4180 return TRUE;
4181 ppc64_elf_tdata (abfd)->u.deleted_section = NULL;
4182
4183 htab = ppc_hash_table (info);
4184 if (!is_ppc64_elf_target (htab->elf.root.creator))
4185 return TRUE;
4186
4187 data.info = info;
4188 data.ok = TRUE;
4189 elf_link_hash_traverse (&htab->elf, add_symbol_adjust, &data);
4190
4191 /* We need to fix the undefs list for any syms we have twiddled to
4192 undef_weak. */
4193 if (htab->twiddled_syms)
4194 {
4195 bfd_link_repair_undef_list (&htab->elf.root);
4196 htab->twiddled_syms = 0;
4197 }
4198 return data.ok;
4199 }
4200
4201 static bfd_boolean
4202 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4203 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4204 {
4205 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4206 char *local_got_tls_masks;
4207
4208 if (local_got_ents == NULL)
4209 {
4210 bfd_size_type size = symtab_hdr->sh_info;
4211
4212 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4213 local_got_ents = bfd_zalloc (abfd, size);
4214 if (local_got_ents == NULL)
4215 return FALSE;
4216 elf_local_got_ents (abfd) = local_got_ents;
4217 }
4218
4219 if ((tls_type & TLS_EXPLICIT) == 0)
4220 {
4221 struct got_entry *ent;
4222
4223 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4224 if (ent->addend == r_addend
4225 && ent->owner == abfd
4226 && ent->tls_type == tls_type)
4227 break;
4228 if (ent == NULL)
4229 {
4230 bfd_size_type amt = sizeof (*ent);
4231 ent = bfd_alloc (abfd, amt);
4232 if (ent == NULL)
4233 return FALSE;
4234 ent->next = local_got_ents[r_symndx];
4235 ent->addend = r_addend;
4236 ent->owner = abfd;
4237 ent->tls_type = tls_type;
4238 ent->got.refcount = 0;
4239 local_got_ents[r_symndx] = ent;
4240 }
4241 ent->got.refcount += 1;
4242 }
4243
4244 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4245 local_got_tls_masks[r_symndx] |= tls_type;
4246 return TRUE;
4247 }
4248
4249 static bfd_boolean
4250 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
4251 {
4252 struct plt_entry *ent;
4253
4254 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4255 if (ent->addend == addend)
4256 break;
4257 if (ent == NULL)
4258 {
4259 bfd_size_type amt = sizeof (*ent);
4260 ent = bfd_alloc (abfd, amt);
4261 if (ent == NULL)
4262 return FALSE;
4263 ent->next = eh->elf.plt.plist;
4264 ent->addend = addend;
4265 ent->plt.refcount = 0;
4266 eh->elf.plt.plist = ent;
4267 }
4268 ent->plt.refcount += 1;
4269 eh->elf.needs_plt = 1;
4270 if (eh->elf.root.root.string[0] == '.'
4271 && eh->elf.root.root.string[1] != '\0')
4272 eh->is_func = 1;
4273 return TRUE;
4274 }
4275
4276 /* Look through the relocs for a section during the first phase, and
4277 calculate needed space in the global offset table, procedure
4278 linkage table, and dynamic reloc sections. */
4279
4280 static bfd_boolean
4281 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4282 asection *sec, const Elf_Internal_Rela *relocs)
4283 {
4284 struct ppc_link_hash_table *htab;
4285 Elf_Internal_Shdr *symtab_hdr;
4286 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4287 const Elf_Internal_Rela *rel;
4288 const Elf_Internal_Rela *rel_end;
4289 asection *sreloc;
4290 asection **opd_sym_map;
4291
4292 if (info->relocatable)
4293 return TRUE;
4294
4295 /* Don't do anything special with non-loaded, non-alloced sections.
4296 In particular, any relocs in such sections should not affect GOT
4297 and PLT reference counting (ie. we don't allow them to create GOT
4298 or PLT entries), there's no possibility or desire to optimize TLS
4299 relocs, and there's not much point in propagating relocs to shared
4300 libs that the dynamic linker won't relocate. */
4301 if ((sec->flags & SEC_ALLOC) == 0)
4302 return TRUE;
4303
4304 htab = ppc_hash_table (info);
4305 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4306
4307 sym_hashes = elf_sym_hashes (abfd);
4308 sym_hashes_end = (sym_hashes
4309 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4310 - symtab_hdr->sh_info);
4311
4312 sreloc = NULL;
4313 opd_sym_map = NULL;
4314 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4315 {
4316 /* Garbage collection needs some extra help with .opd sections.
4317 We don't want to necessarily keep everything referenced by
4318 relocs in .opd, as that would keep all functions. Instead,
4319 if we reference an .opd symbol (a function descriptor), we
4320 want to keep the function code symbol's section. This is
4321 easy for global symbols, but for local syms we need to keep
4322 information about the associated function section. Later, if
4323 edit_opd deletes entries, we'll use this array to adjust
4324 local syms in .opd. */
4325 union opd_info {
4326 asection *func_section;
4327 long entry_adjust;
4328 };
4329 bfd_size_type amt;
4330
4331 amt = sec->size * sizeof (union opd_info) / 8;
4332 opd_sym_map = bfd_zalloc (abfd, amt);
4333 if (opd_sym_map == NULL)
4334 return FALSE;
4335 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
4336 }
4337
4338 if (htab->sfpr == NULL
4339 && !create_linkage_sections (htab->elf.dynobj, info))
4340 return FALSE;
4341
4342 rel_end = relocs + sec->reloc_count;
4343 for (rel = relocs; rel < rel_end; rel++)
4344 {
4345 unsigned long r_symndx;
4346 struct elf_link_hash_entry *h;
4347 enum elf_ppc64_reloc_type r_type;
4348 int tls_type = 0;
4349
4350 r_symndx = ELF64_R_SYM (rel->r_info);
4351 if (r_symndx < symtab_hdr->sh_info)
4352 h = NULL;
4353 else
4354 {
4355 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4356 while (h->root.type == bfd_link_hash_indirect
4357 || h->root.type == bfd_link_hash_warning)
4358 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4359 }
4360
4361 r_type = ELF64_R_TYPE (rel->r_info);
4362 switch (r_type)
4363 {
4364 case R_PPC64_GOT_TLSLD16:
4365 case R_PPC64_GOT_TLSLD16_LO:
4366 case R_PPC64_GOT_TLSLD16_HI:
4367 case R_PPC64_GOT_TLSLD16_HA:
4368 ppc64_tlsld_got (abfd)->refcount += 1;
4369 tls_type = TLS_TLS | TLS_LD;
4370 goto dogottls;
4371
4372 case R_PPC64_GOT_TLSGD16:
4373 case R_PPC64_GOT_TLSGD16_LO:
4374 case R_PPC64_GOT_TLSGD16_HI:
4375 case R_PPC64_GOT_TLSGD16_HA:
4376 tls_type = TLS_TLS | TLS_GD;
4377 goto dogottls;
4378
4379 case R_PPC64_GOT_TPREL16_DS:
4380 case R_PPC64_GOT_TPREL16_LO_DS:
4381 case R_PPC64_GOT_TPREL16_HI:
4382 case R_PPC64_GOT_TPREL16_HA:
4383 if (info->shared)
4384 info->flags |= DF_STATIC_TLS;
4385 tls_type = TLS_TLS | TLS_TPREL;
4386 goto dogottls;
4387
4388 case R_PPC64_GOT_DTPREL16_DS:
4389 case R_PPC64_GOT_DTPREL16_LO_DS:
4390 case R_PPC64_GOT_DTPREL16_HI:
4391 case R_PPC64_GOT_DTPREL16_HA:
4392 tls_type = TLS_TLS | TLS_DTPREL;
4393 dogottls:
4394 sec->has_tls_reloc = 1;
4395 /* Fall thru */
4396
4397 case R_PPC64_GOT16:
4398 case R_PPC64_GOT16_DS:
4399 case R_PPC64_GOT16_HA:
4400 case R_PPC64_GOT16_HI:
4401 case R_PPC64_GOT16_LO:
4402 case R_PPC64_GOT16_LO_DS:
4403 /* This symbol requires a global offset table entry. */
4404 sec->has_toc_reloc = 1;
4405 if (ppc64_elf_tdata (abfd)->got == NULL
4406 && !create_got_section (abfd, info))
4407 return FALSE;
4408
4409 if (h != NULL)
4410 {
4411 struct ppc_link_hash_entry *eh;
4412 struct got_entry *ent;
4413
4414 eh = (struct ppc_link_hash_entry *) h;
4415 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4416 if (ent->addend == rel->r_addend
4417 && ent->owner == abfd
4418 && ent->tls_type == tls_type)
4419 break;
4420 if (ent == NULL)
4421 {
4422 bfd_size_type amt = sizeof (*ent);
4423 ent = bfd_alloc (abfd, amt);
4424 if (ent == NULL)
4425 return FALSE;
4426 ent->next = eh->elf.got.glist;
4427 ent->addend = rel->r_addend;
4428 ent->owner = abfd;
4429 ent->tls_type = tls_type;
4430 ent->got.refcount = 0;
4431 eh->elf.got.glist = ent;
4432 }
4433 ent->got.refcount += 1;
4434 eh->tls_mask |= tls_type;
4435 }
4436 else
4437 /* This is a global offset table entry for a local symbol. */
4438 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4439 rel->r_addend, tls_type))
4440 return FALSE;
4441 break;
4442
4443 case R_PPC64_PLT16_HA:
4444 case R_PPC64_PLT16_HI:
4445 case R_PPC64_PLT16_LO:
4446 case R_PPC64_PLT32:
4447 case R_PPC64_PLT64:
4448 /* This symbol requires a procedure linkage table entry. We
4449 actually build the entry in adjust_dynamic_symbol,
4450 because this might be a case of linking PIC code without
4451 linking in any dynamic objects, in which case we don't
4452 need to generate a procedure linkage table after all. */
4453 if (h == NULL)
4454 {
4455 /* It does not make sense to have a procedure linkage
4456 table entry for a local symbol. */
4457 bfd_set_error (bfd_error_bad_value);
4458 return FALSE;
4459 }
4460 else
4461 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4462 rel->r_addend))
4463 return FALSE;
4464 break;
4465
4466 /* The following relocations don't need to propagate the
4467 relocation if linking a shared object since they are
4468 section relative. */
4469 case R_PPC64_SECTOFF:
4470 case R_PPC64_SECTOFF_LO:
4471 case R_PPC64_SECTOFF_HI:
4472 case R_PPC64_SECTOFF_HA:
4473 case R_PPC64_SECTOFF_DS:
4474 case R_PPC64_SECTOFF_LO_DS:
4475 case R_PPC64_DTPREL16:
4476 case R_PPC64_DTPREL16_LO:
4477 case R_PPC64_DTPREL16_HI:
4478 case R_PPC64_DTPREL16_HA:
4479 case R_PPC64_DTPREL16_DS:
4480 case R_PPC64_DTPREL16_LO_DS:
4481 case R_PPC64_DTPREL16_HIGHER:
4482 case R_PPC64_DTPREL16_HIGHERA:
4483 case R_PPC64_DTPREL16_HIGHEST:
4484 case R_PPC64_DTPREL16_HIGHESTA:
4485 break;
4486
4487 /* Nor do these. */
4488 case R_PPC64_TOC16:
4489 case R_PPC64_TOC16_LO:
4490 case R_PPC64_TOC16_HI:
4491 case R_PPC64_TOC16_HA:
4492 case R_PPC64_TOC16_DS:
4493 case R_PPC64_TOC16_LO_DS:
4494 sec->has_toc_reloc = 1;
4495 break;
4496
4497 /* This relocation describes the C++ object vtable hierarchy.
4498 Reconstruct it for later use during GC. */
4499 case R_PPC64_GNU_VTINHERIT:
4500 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4501 return FALSE;
4502 break;
4503
4504 /* This relocation describes which C++ vtable entries are actually
4505 used. Record for later use during GC. */
4506 case R_PPC64_GNU_VTENTRY:
4507 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4508 return FALSE;
4509 break;
4510
4511 case R_PPC64_REL14:
4512 case R_PPC64_REL14_BRTAKEN:
4513 case R_PPC64_REL14_BRNTAKEN:
4514 {
4515 asection *dest = NULL;
4516
4517 /* Heuristic: If jumping outside our section, chances are
4518 we are going to need a stub. */
4519 if (h != NULL)
4520 {
4521 /* If the sym is weak it may be overridden later, so
4522 don't assume we know where a weak sym lives. */
4523 if (h->root.type == bfd_link_hash_defined)
4524 dest = h->root.u.def.section;
4525 }
4526 else
4527 dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4528 sec, r_symndx);
4529 if (dest != sec)
4530 htab->has_14bit_branch = 1;
4531 }
4532 /* Fall through. */
4533
4534 case R_PPC64_REL24:
4535 if (h != NULL)
4536 {
4537 /* We may need a .plt entry if the function this reloc
4538 refers to is in a shared lib. */
4539 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4540 rel->r_addend))
4541 return FALSE;
4542 if (h == &htab->tls_get_addr->elf
4543 || h == &htab->tls_get_addr_fd->elf)
4544 sec->has_tls_reloc = 1;
4545 else if (htab->tls_get_addr == NULL
4546 && !strncmp (h->root.root.string, ".__tls_get_addr", 15)
4547 && (h->root.root.string[15] == 0
4548 || h->root.root.string[15] == '@'))
4549 {
4550 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4551 sec->has_tls_reloc = 1;
4552 }
4553 else if (htab->tls_get_addr_fd == NULL
4554 && !strncmp (h->root.root.string, "__tls_get_addr", 14)
4555 && (h->root.root.string[14] == 0
4556 || h->root.root.string[14] == '@'))
4557 {
4558 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
4559 sec->has_tls_reloc = 1;
4560 }
4561 }
4562 break;
4563
4564 case R_PPC64_TPREL64:
4565 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4566 if (info->shared)
4567 info->flags |= DF_STATIC_TLS;
4568 goto dotlstoc;
4569
4570 case R_PPC64_DTPMOD64:
4571 if (rel + 1 < rel_end
4572 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4573 && rel[1].r_offset == rel->r_offset + 8)
4574 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
4575 else
4576 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
4577 goto dotlstoc;
4578
4579 case R_PPC64_DTPREL64:
4580 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4581 if (rel != relocs
4582 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4583 && rel[-1].r_offset == rel->r_offset - 8)
4584 /* This is the second reloc of a dtpmod, dtprel pair.
4585 Don't mark with TLS_DTPREL. */
4586 goto dodyn;
4587
4588 dotlstoc:
4589 sec->has_tls_reloc = 1;
4590 if (h != NULL)
4591 {
4592 struct ppc_link_hash_entry *eh;
4593 eh = (struct ppc_link_hash_entry *) h;
4594 eh->tls_mask |= tls_type;
4595 }
4596 else
4597 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4598 rel->r_addend, tls_type))
4599 return FALSE;
4600
4601 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4602 {
4603 /* One extra to simplify get_tls_mask. */
4604 bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
4605 ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
4606 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
4607 return FALSE;
4608 }
4609 BFD_ASSERT (rel->r_offset % 8 == 0);
4610 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
4611
4612 /* Mark the second slot of a GD or LD entry.
4613 -1 to indicate GD and -2 to indicate LD. */
4614 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
4615 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
4616 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
4617 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
4618 goto dodyn;
4619
4620 case R_PPC64_TPREL16:
4621 case R_PPC64_TPREL16_LO:
4622 case R_PPC64_TPREL16_HI:
4623 case R_PPC64_TPREL16_HA:
4624 case R_PPC64_TPREL16_DS:
4625 case R_PPC64_TPREL16_LO_DS:
4626 case R_PPC64_TPREL16_HIGHER:
4627 case R_PPC64_TPREL16_HIGHERA:
4628 case R_PPC64_TPREL16_HIGHEST:
4629 case R_PPC64_TPREL16_HIGHESTA:
4630 if (info->shared)
4631 {
4632 info->flags |= DF_STATIC_TLS;
4633 goto dodyn;
4634 }
4635 break;
4636
4637 case R_PPC64_ADDR64:
4638 if (opd_sym_map != NULL
4639 && rel + 1 < rel_end
4640 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
4641 {
4642 if (h != NULL)
4643 {
4644 if (h->root.root.string[0] == '.'
4645 && h->root.root.string[1] != 0
4646 && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4647 ;
4648 else
4649 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4650 }
4651 else
4652 {
4653 asection *s;
4654
4655 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4656 r_symndx);
4657 if (s == NULL)
4658 return FALSE;
4659 else if (s != sec)
4660 opd_sym_map[rel->r_offset / 8] = s;
4661 }
4662 }
4663 /* Fall through. */
4664
4665 case R_PPC64_REL30:
4666 case R_PPC64_REL32:
4667 case R_PPC64_REL64:
4668 case R_PPC64_ADDR14:
4669 case R_PPC64_ADDR14_BRNTAKEN:
4670 case R_PPC64_ADDR14_BRTAKEN:
4671 case R_PPC64_ADDR16:
4672 case R_PPC64_ADDR16_DS:
4673 case R_PPC64_ADDR16_HA:
4674 case R_PPC64_ADDR16_HI:
4675 case R_PPC64_ADDR16_HIGHER:
4676 case R_PPC64_ADDR16_HIGHERA:
4677 case R_PPC64_ADDR16_HIGHEST:
4678 case R_PPC64_ADDR16_HIGHESTA:
4679 case R_PPC64_ADDR16_LO:
4680 case R_PPC64_ADDR16_LO_DS:
4681 case R_PPC64_ADDR24:
4682 case R_PPC64_ADDR32:
4683 case R_PPC64_UADDR16:
4684 case R_PPC64_UADDR32:
4685 case R_PPC64_UADDR64:
4686 case R_PPC64_TOC:
4687 if (h != NULL && !info->shared)
4688 /* We may need a copy reloc. */
4689 h->non_got_ref = 1;
4690
4691 /* Don't propagate .opd relocs. */
4692 if (NO_OPD_RELOCS && opd_sym_map != NULL)
4693 break;
4694
4695 /* If we are creating a shared library, and this is a reloc
4696 against a global symbol, or a non PC relative reloc
4697 against a local symbol, then we need to copy the reloc
4698 into the shared library. However, if we are linking with
4699 -Bsymbolic, we do not need to copy a reloc against a
4700 global symbol which is defined in an object we are
4701 including in the link (i.e., DEF_REGULAR is set). At
4702 this point we have not seen all the input files, so it is
4703 possible that DEF_REGULAR is not set now but will be set
4704 later (it is never cleared). In case of a weak definition,
4705 DEF_REGULAR may be cleared later by a strong definition in
4706 a shared library. We account for that possibility below by
4707 storing information in the dyn_relocs field of the hash
4708 table entry. A similar situation occurs when creating
4709 shared libraries and symbol visibility changes render the
4710 symbol local.
4711
4712 If on the other hand, we are creating an executable, we
4713 may need to keep relocations for symbols satisfied by a
4714 dynamic library if we manage to avoid copy relocs for the
4715 symbol. */
4716 dodyn:
4717 if ((info->shared
4718 && (MUST_BE_DYN_RELOC (r_type)
4719 || (h != NULL
4720 && (! info->symbolic
4721 || h->root.type == bfd_link_hash_defweak
4722 || !h->def_regular))))
4723 || (ELIMINATE_COPY_RELOCS
4724 && !info->shared
4725 && h != NULL
4726 && (h->root.type == bfd_link_hash_defweak
4727 || !h->def_regular)))
4728 {
4729 struct ppc_dyn_relocs *p;
4730 struct ppc_dyn_relocs **head;
4731
4732 /* We must copy these reloc types into the output file.
4733 Create a reloc section in dynobj and make room for
4734 this reloc. */
4735 if (sreloc == NULL)
4736 {
4737 const char *name;
4738 bfd *dynobj;
4739
4740 name = (bfd_elf_string_from_elf_section
4741 (abfd,
4742 elf_elfheader (abfd)->e_shstrndx,
4743 elf_section_data (sec)->rel_hdr.sh_name));
4744 if (name == NULL)
4745 return FALSE;
4746
4747 if (strncmp (name, ".rela", 5) != 0
4748 || strcmp (bfd_get_section_name (abfd, sec),
4749 name + 5) != 0)
4750 {
4751 (*_bfd_error_handler)
4752 (_("%B: bad relocation section name `%s\'"),
4753 abfd, name);
4754 bfd_set_error (bfd_error_bad_value);
4755 }
4756
4757 dynobj = htab->elf.dynobj;
4758 sreloc = bfd_get_section_by_name (dynobj, name);
4759 if (sreloc == NULL)
4760 {
4761 flagword flags;
4762
4763 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4764 | SEC_IN_MEMORY | SEC_LINKER_CREATED
4765 | SEC_ALLOC | SEC_LOAD);
4766 sreloc = bfd_make_section_with_flags (dynobj,
4767 name,
4768 flags);
4769 if (sreloc == NULL
4770 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4771 return FALSE;
4772 }
4773 elf_section_data (sec)->sreloc = sreloc;
4774 }
4775
4776 /* If this is a global symbol, we count the number of
4777 relocations we need for this symbol. */
4778 if (h != NULL)
4779 {
4780 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4781 }
4782 else
4783 {
4784 /* Track dynamic relocs needed for local syms too.
4785 We really need local syms available to do this
4786 easily. Oh well. */
4787
4788 asection *s;
4789 void *vpp;
4790
4791 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4792 sec, r_symndx);
4793 if (s == NULL)
4794 return FALSE;
4795
4796 vpp = &elf_section_data (s)->local_dynrel;
4797 head = (struct ppc_dyn_relocs **) vpp;
4798 }
4799
4800 p = *head;
4801 if (p == NULL || p->sec != sec)
4802 {
4803 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4804 if (p == NULL)
4805 return FALSE;
4806 p->next = *head;
4807 *head = p;
4808 p->sec = sec;
4809 p->count = 0;
4810 p->pc_count = 0;
4811 }
4812
4813 p->count += 1;
4814 if (!MUST_BE_DYN_RELOC (r_type))
4815 p->pc_count += 1;
4816 }
4817 break;
4818
4819 default:
4820 break;
4821 }
4822 }
4823
4824 return TRUE;
4825 }
4826
4827 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4828 of the code entry point, and its section. */
4829
4830 static bfd_vma
4831 opd_entry_value (asection *opd_sec,
4832 bfd_vma offset,
4833 asection **code_sec,
4834 bfd_vma *code_off)
4835 {
4836 bfd *opd_bfd = opd_sec->owner;
4837 Elf_Internal_Rela *relocs;
4838 Elf_Internal_Rela *lo, *hi, *look;
4839 bfd_vma val;
4840
4841 /* No relocs implies we are linking a --just-symbols object. */
4842 if (opd_sec->reloc_count == 0)
4843 {
4844 bfd_vma val;
4845
4846 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4847 return (bfd_vma) -1;
4848
4849 if (code_sec != NULL)
4850 {
4851 asection *sec, *likely = NULL;
4852 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4853 if (sec->vma <= val
4854 && (sec->flags & SEC_LOAD) != 0
4855 && (sec->flags & SEC_ALLOC) != 0)
4856 likely = sec;
4857 if (likely != NULL)
4858 {
4859 *code_sec = likely;
4860 if (code_off != NULL)
4861 *code_off = val - likely->vma;
4862 }
4863 }
4864 return val;
4865 }
4866
4867 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4868 if (relocs == NULL)
4869 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
4870
4871 /* Go find the opd reloc at the sym address. */
4872 lo = relocs;
4873 BFD_ASSERT (lo != NULL);
4874 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
4875 val = (bfd_vma) -1;
4876 while (lo < hi)
4877 {
4878 look = lo + (hi - lo) / 2;
4879 if (look->r_offset < offset)
4880 lo = look + 1;
4881 else if (look->r_offset > offset)
4882 hi = look;
4883 else
4884 {
4885 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4886 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4887 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4888 {
4889 unsigned long symndx = ELF64_R_SYM (look->r_info);
4890 asection *sec;
4891
4892 if (symndx < symtab_hdr->sh_info)
4893 {
4894 Elf_Internal_Sym *sym;
4895
4896 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
4897 if (sym == NULL)
4898 {
4899 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
4900 symtab_hdr->sh_info,
4901 0, NULL, NULL, NULL);
4902 if (sym == NULL)
4903 break;
4904 symtab_hdr->contents = (bfd_byte *) sym;
4905 }
4906
4907 sym += symndx;
4908 val = sym->st_value;
4909 sec = NULL;
4910 if ((sym->st_shndx != SHN_UNDEF
4911 && sym->st_shndx < SHN_LORESERVE)
4912 || sym->st_shndx > SHN_HIRESERVE)
4913 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
4914 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
4915 }
4916 else
4917 {
4918 struct elf_link_hash_entry **sym_hashes;
4919 struct elf_link_hash_entry *rh;
4920
4921 sym_hashes = elf_sym_hashes (opd_bfd);
4922 rh = sym_hashes[symndx - symtab_hdr->sh_info];
4923 while (rh->root.type == bfd_link_hash_indirect
4924 || rh->root.type == bfd_link_hash_warning)
4925 rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
4926 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
4927 || rh->root.type == bfd_link_hash_defweak);
4928 val = rh->root.u.def.value;
4929 sec = rh->root.u.def.section;
4930 }
4931 val += look->r_addend;
4932 if (code_off != NULL)
4933 *code_off = val;
4934 if (code_sec != NULL)
4935 *code_sec = sec;
4936 if (sec != NULL && sec->output_section != NULL)
4937 val += sec->output_section->vma + sec->output_offset;
4938 }
4939 break;
4940 }
4941 }
4942
4943 return val;
4944 }
4945
4946 /* Mark sections containing dynamically referenced symbols. When
4947 building shared libraries, we must assume that any visible symbol is
4948 referenced. */
4949
4950 static bfd_boolean
4951 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
4952 {
4953 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4954 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
4955
4956 if (eh->elf.root.type == bfd_link_hash_warning)
4957 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4958
4959 /* Dynamic linking info is on the func descriptor sym. */
4960 if (eh->oh != NULL
4961 && eh->oh->is_func_descriptor
4962 && (eh->oh->elf.root.type == bfd_link_hash_defined
4963 || eh->oh->elf.root.type == bfd_link_hash_defweak))
4964 eh = eh->oh;
4965
4966 if ((eh->elf.root.type == bfd_link_hash_defined
4967 || eh->elf.root.type == bfd_link_hash_defweak)
4968 && (eh->elf.ref_dynamic
4969 || (!info->executable
4970 && eh->elf.def_regular
4971 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4972 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
4973 {
4974 asection *code_sec;
4975
4976 eh->elf.root.u.def.section->flags |= SEC_KEEP;
4977
4978 /* Function descriptor syms cause the associated
4979 function code sym section to be marked. */
4980 if (eh->is_func_descriptor
4981 && (eh->oh->elf.root.type == bfd_link_hash_defined
4982 || eh->oh->elf.root.type == bfd_link_hash_defweak))
4983 eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
4984 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
4985 && opd_entry_value (eh->elf.root.u.def.section,
4986 eh->elf.root.u.def.value,
4987 &code_sec, NULL) != (bfd_vma) -1)
4988 code_sec->flags |= SEC_KEEP;
4989 }
4990
4991 return TRUE;
4992 }
4993
4994 /* Return the section that should be marked against GC for a given
4995 relocation. */
4996
4997 static asection *
4998 ppc64_elf_gc_mark_hook (asection *sec,
4999 struct bfd_link_info *info,
5000 Elf_Internal_Rela *rel,
5001 struct elf_link_hash_entry *h,
5002 Elf_Internal_Sym *sym)
5003 {
5004 asection *rsec;
5005
5006 /* First mark all our entry sym sections. */
5007 if (info->gc_sym_list != NULL)
5008 {
5009 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5010 struct bfd_sym_chain *sym = info->gc_sym_list;
5011
5012 info->gc_sym_list = NULL;
5013 for (; sym != NULL; sym = sym->next)
5014 {
5015 struct ppc_link_hash_entry *eh;
5016
5017 eh = (struct ppc_link_hash_entry *)
5018 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5019 if (eh == NULL)
5020 continue;
5021 if (eh->elf.root.type != bfd_link_hash_defined
5022 && eh->elf.root.type != bfd_link_hash_defweak)
5023 continue;
5024
5025 if (eh->is_func_descriptor
5026 && (eh->oh->elf.root.type == bfd_link_hash_defined
5027 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5028 rsec = eh->oh->elf.root.u.def.section;
5029 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5030 && opd_entry_value (eh->elf.root.u.def.section,
5031 eh->elf.root.u.def.value,
5032 &rsec, NULL) != (bfd_vma) -1)
5033 ;
5034 else
5035 continue;
5036
5037 if (!rsec->gc_mark)
5038 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5039
5040 rsec = eh->elf.root.u.def.section;
5041 if (!rsec->gc_mark)
5042 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5043 }
5044 }
5045
5046 /* Syms return NULL if we're marking .opd, so we avoid marking all
5047 function sections, as all functions are referenced in .opd. */
5048 rsec = NULL;
5049 if (get_opd_info (sec) != NULL)
5050 return rsec;
5051
5052 if (h != NULL)
5053 {
5054 enum elf_ppc64_reloc_type r_type;
5055 struct ppc_link_hash_entry *eh;
5056
5057 r_type = ELF64_R_TYPE (rel->r_info);
5058 switch (r_type)
5059 {
5060 case R_PPC64_GNU_VTINHERIT:
5061 case R_PPC64_GNU_VTENTRY:
5062 break;
5063
5064 default:
5065 switch (h->root.type)
5066 {
5067 case bfd_link_hash_defined:
5068 case bfd_link_hash_defweak:
5069 eh = (struct ppc_link_hash_entry *) h;
5070 if (eh->oh != NULL
5071 && eh->oh->is_func_descriptor
5072 && (eh->oh->elf.root.type == bfd_link_hash_defined
5073 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5074 eh = eh->oh;
5075
5076 /* Function descriptor syms cause the associated
5077 function code sym section to be marked. */
5078 if (eh->is_func_descriptor
5079 && (eh->oh->elf.root.type == bfd_link_hash_defined
5080 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5081 {
5082 /* They also mark their opd section. */
5083 if (!eh->elf.root.u.def.section->gc_mark)
5084 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5085 ppc64_elf_gc_mark_hook);
5086
5087 rsec = eh->oh->elf.root.u.def.section;
5088 }
5089 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5090 && opd_entry_value (eh->elf.root.u.def.section,
5091 eh->elf.root.u.def.value,
5092 &rsec, NULL) != (bfd_vma) -1)
5093 {
5094 if (!eh->elf.root.u.def.section->gc_mark)
5095 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5096 ppc64_elf_gc_mark_hook);
5097 }
5098 else
5099 rsec = h->root.u.def.section;
5100 break;
5101
5102 case bfd_link_hash_common:
5103 rsec = h->root.u.c.p->section;
5104 break;
5105
5106 default:
5107 break;
5108 }
5109 }
5110 }
5111 else
5112 {
5113 asection **opd_sym_section;
5114
5115 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5116 opd_sym_section = get_opd_info (rsec);
5117 if (opd_sym_section != NULL)
5118 {
5119 if (!rsec->gc_mark)
5120 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5121
5122 rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
5123 }
5124 }
5125
5126 return rsec;
5127 }
5128
5129 /* Update the .got, .plt. and dynamic reloc reference counts for the
5130 section being removed. */
5131
5132 static bfd_boolean
5133 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5134 asection *sec, const Elf_Internal_Rela *relocs)
5135 {
5136 struct ppc_link_hash_table *htab;
5137 Elf_Internal_Shdr *symtab_hdr;
5138 struct elf_link_hash_entry **sym_hashes;
5139 struct got_entry **local_got_ents;
5140 const Elf_Internal_Rela *rel, *relend;
5141
5142 if ((sec->flags & SEC_ALLOC) == 0)
5143 return TRUE;
5144
5145 elf_section_data (sec)->local_dynrel = NULL;
5146
5147 htab = ppc_hash_table (info);
5148 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5149 sym_hashes = elf_sym_hashes (abfd);
5150 local_got_ents = elf_local_got_ents (abfd);
5151
5152 relend = relocs + sec->reloc_count;
5153 for (rel = relocs; rel < relend; rel++)
5154 {
5155 unsigned long r_symndx;
5156 enum elf_ppc64_reloc_type r_type;
5157 struct elf_link_hash_entry *h = NULL;
5158 char tls_type = 0;
5159
5160 r_symndx = ELF64_R_SYM (rel->r_info);
5161 r_type = ELF64_R_TYPE (rel->r_info);
5162 if (r_symndx >= symtab_hdr->sh_info)
5163 {
5164 struct ppc_link_hash_entry *eh;
5165 struct ppc_dyn_relocs **pp;
5166 struct ppc_dyn_relocs *p;
5167
5168 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5169 while (h->root.type == bfd_link_hash_indirect
5170 || h->root.type == bfd_link_hash_warning)
5171 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5172 eh = (struct ppc_link_hash_entry *) h;
5173
5174 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5175 if (p->sec == sec)
5176 {
5177 /* Everything must go for SEC. */
5178 *pp = p->next;
5179 break;
5180 }
5181 }
5182
5183 switch (r_type)
5184 {
5185 case R_PPC64_GOT_TLSLD16:
5186 case R_PPC64_GOT_TLSLD16_LO:
5187 case R_PPC64_GOT_TLSLD16_HI:
5188 case R_PPC64_GOT_TLSLD16_HA:
5189 ppc64_tlsld_got (abfd)->refcount -= 1;
5190 tls_type = TLS_TLS | TLS_LD;
5191 goto dogot;
5192
5193 case R_PPC64_GOT_TLSGD16:
5194 case R_PPC64_GOT_TLSGD16_LO:
5195 case R_PPC64_GOT_TLSGD16_HI:
5196 case R_PPC64_GOT_TLSGD16_HA:
5197 tls_type = TLS_TLS | TLS_GD;
5198 goto dogot;
5199
5200 case R_PPC64_GOT_TPREL16_DS:
5201 case R_PPC64_GOT_TPREL16_LO_DS:
5202 case R_PPC64_GOT_TPREL16_HI:
5203 case R_PPC64_GOT_TPREL16_HA:
5204 tls_type = TLS_TLS | TLS_TPREL;
5205 goto dogot;
5206
5207 case R_PPC64_GOT_DTPREL16_DS:
5208 case R_PPC64_GOT_DTPREL16_LO_DS:
5209 case R_PPC64_GOT_DTPREL16_HI:
5210 case R_PPC64_GOT_DTPREL16_HA:
5211 tls_type = TLS_TLS | TLS_DTPREL;
5212 goto dogot;
5213
5214 case R_PPC64_GOT16:
5215 case R_PPC64_GOT16_DS:
5216 case R_PPC64_GOT16_HA:
5217 case R_PPC64_GOT16_HI:
5218 case R_PPC64_GOT16_LO:
5219 case R_PPC64_GOT16_LO_DS:
5220 dogot:
5221 {
5222 struct got_entry *ent;
5223
5224 if (h != NULL)
5225 ent = h->got.glist;
5226 else
5227 ent = local_got_ents[r_symndx];
5228
5229 for (; ent != NULL; ent = ent->next)
5230 if (ent->addend == rel->r_addend
5231 && ent->owner == abfd
5232 && ent->tls_type == tls_type)
5233 break;
5234 if (ent == NULL)
5235 abort ();
5236 if (ent->got.refcount > 0)
5237 ent->got.refcount -= 1;
5238 }
5239 break;
5240
5241 case R_PPC64_PLT16_HA:
5242 case R_PPC64_PLT16_HI:
5243 case R_PPC64_PLT16_LO:
5244 case R_PPC64_PLT32:
5245 case R_PPC64_PLT64:
5246 case R_PPC64_REL14:
5247 case R_PPC64_REL14_BRNTAKEN:
5248 case R_PPC64_REL14_BRTAKEN:
5249 case R_PPC64_REL24:
5250 if (h != NULL)
5251 {
5252 struct plt_entry *ent;
5253
5254 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5255 if (ent->addend == rel->r_addend)
5256 break;
5257 if (ent == NULL)
5258 abort ();
5259 if (ent->plt.refcount > 0)
5260 ent->plt.refcount -= 1;
5261 }
5262 break;
5263
5264 default:
5265 break;
5266 }
5267 }
5268 return TRUE;
5269 }
5270
5271 /* The maximum size of .sfpr. */
5272 #define SFPR_MAX (218*4)
5273
5274 struct sfpr_def_parms
5275 {
5276 const char name[12];
5277 unsigned char lo, hi;
5278 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5279 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5280 };
5281
5282 /* Auto-generate _save*, _rest* functions in .sfpr. */
5283
5284 static unsigned int
5285 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5286 {
5287 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5288 unsigned int i;
5289 size_t len = strlen (parm->name);
5290 bfd_boolean writing = FALSE;
5291 char sym[16];
5292
5293 memcpy (sym, parm->name, len);
5294 sym[len + 2] = 0;
5295
5296 for (i = parm->lo; i <= parm->hi; i++)
5297 {
5298 struct elf_link_hash_entry *h;
5299
5300 sym[len + 0] = i / 10 + '0';
5301 sym[len + 1] = i % 10 + '0';
5302 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5303 if (h != NULL
5304 && !h->def_regular)
5305 {
5306 h->root.type = bfd_link_hash_defined;
5307 h->root.u.def.section = htab->sfpr;
5308 h->root.u.def.value = htab->sfpr->size;
5309 h->type = STT_FUNC;
5310 h->def_regular = 1;
5311 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5312 writing = TRUE;
5313 if (htab->sfpr->contents == NULL)
5314 {
5315 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5316 if (htab->sfpr->contents == NULL)
5317 return FALSE;
5318 }
5319 }
5320 if (writing)
5321 {
5322 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5323 if (i != parm->hi)
5324 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5325 else
5326 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5327 htab->sfpr->size = p - htab->sfpr->contents;
5328 }
5329 }
5330
5331 return TRUE;
5332 }
5333
5334 static bfd_byte *
5335 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5336 {
5337 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5338 return p + 4;
5339 }
5340
5341 static bfd_byte *
5342 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5343 {
5344 p = savegpr0 (abfd, p, r);
5345 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5346 p = p + 4;
5347 bfd_put_32 (abfd, BLR, p);
5348 return p + 4;
5349 }
5350
5351 static bfd_byte *
5352 restgpr0 (bfd *abfd, bfd_byte *p, int r)
5353 {
5354 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5355 return p + 4;
5356 }
5357
5358 static bfd_byte *
5359 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5360 {
5361 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5362 p = p + 4;
5363 p = restgpr0 (abfd, p, r);
5364 bfd_put_32 (abfd, MTLR_R0, p);
5365 p = p + 4;
5366 if (r == 29)
5367 {
5368 p = restgpr0 (abfd, p, 30);
5369 p = restgpr0 (abfd, p, 31);
5370 }
5371 bfd_put_32 (abfd, BLR, p);
5372 return p + 4;
5373 }
5374
5375 static bfd_byte *
5376 savegpr1 (bfd *abfd, bfd_byte *p, int r)
5377 {
5378 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5379 return p + 4;
5380 }
5381
5382 static bfd_byte *
5383 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5384 {
5385 p = savegpr1 (abfd, p, r);
5386 bfd_put_32 (abfd, BLR, p);
5387 return p + 4;
5388 }
5389
5390 static bfd_byte *
5391 restgpr1 (bfd *abfd, bfd_byte *p, int r)
5392 {
5393 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5394 return p + 4;
5395 }
5396
5397 static bfd_byte *
5398 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5399 {
5400 p = restgpr1 (abfd, p, r);
5401 bfd_put_32 (abfd, BLR, p);
5402 return p + 4;
5403 }
5404
5405 static bfd_byte *
5406 savefpr (bfd *abfd, bfd_byte *p, int r)
5407 {
5408 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5409 return p + 4;
5410 }
5411
5412 static bfd_byte *
5413 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5414 {
5415 p = savefpr (abfd, p, r);
5416 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5417 p = p + 4;
5418 bfd_put_32 (abfd, BLR, p);
5419 return p + 4;
5420 }
5421
5422 static bfd_byte *
5423 restfpr (bfd *abfd, bfd_byte *p, int r)
5424 {
5425 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5426 return p + 4;
5427 }
5428
5429 static bfd_byte *
5430 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5431 {
5432 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5433 p = p + 4;
5434 p = restfpr (abfd, p, r);
5435 bfd_put_32 (abfd, MTLR_R0, p);
5436 p = p + 4;
5437 if (r == 29)
5438 {
5439 p = restfpr (abfd, p, 30);
5440 p = restfpr (abfd, p, 31);
5441 }
5442 bfd_put_32 (abfd, BLR, p);
5443 return p + 4;
5444 }
5445
5446 static bfd_byte *
5447 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5448 {
5449 p = savefpr (abfd, p, r);
5450 bfd_put_32 (abfd, BLR, p);
5451 return p + 4;
5452 }
5453
5454 static bfd_byte *
5455 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5456 {
5457 p = restfpr (abfd, p, r);
5458 bfd_put_32 (abfd, BLR, p);
5459 return p + 4;
5460 }
5461
5462 static bfd_byte *
5463 savevr (bfd *abfd, bfd_byte *p, int r)
5464 {
5465 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5466 p = p + 4;
5467 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5468 return p + 4;
5469 }
5470
5471 static bfd_byte *
5472 savevr_tail (bfd *abfd, bfd_byte *p, int r)
5473 {
5474 p = savevr (abfd, p, r);
5475 bfd_put_32 (abfd, BLR, p);
5476 return p + 4;
5477 }
5478
5479 static bfd_byte *
5480 restvr (bfd *abfd, bfd_byte *p, int r)
5481 {
5482 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5483 p = p + 4;
5484 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5485 return p + 4;
5486 }
5487
5488 static bfd_byte *
5489 restvr_tail (bfd *abfd, bfd_byte *p, int r)
5490 {
5491 p = restvr (abfd, p, r);
5492 bfd_put_32 (abfd, BLR, p);
5493 return p + 4;
5494 }
5495
5496 /* Called via elf_link_hash_traverse to transfer dynamic linking
5497 information on function code symbol entries to their corresponding
5498 function descriptor symbol entries. */
5499
5500 static bfd_boolean
5501 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5502 {
5503 struct bfd_link_info *info;
5504 struct ppc_link_hash_table *htab;
5505 struct plt_entry *ent;
5506 struct ppc_link_hash_entry *fh;
5507 struct ppc_link_hash_entry *fdh;
5508 bfd_boolean force_local;
5509
5510 fh = (struct ppc_link_hash_entry *) h;
5511 if (fh->elf.root.type == bfd_link_hash_indirect)
5512 return TRUE;
5513
5514 if (fh->elf.root.type == bfd_link_hash_warning)
5515 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
5516
5517 info = inf;
5518 htab = ppc_hash_table (info);
5519
5520 /* Resolve undefined references to dot-symbols as the value
5521 in the function descriptor, if we have one in a regular object.
5522 This is to satisfy cases like ".quad .foo". Calls to functions
5523 in dynamic objects are handled elsewhere. */
5524 if (fh->elf.root.type == bfd_link_hash_undefweak
5525 && fh->was_undefined
5526 && (fh->oh->elf.root.type == bfd_link_hash_defined
5527 || fh->oh->elf.root.type == bfd_link_hash_defweak)
5528 && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5529 && opd_entry_value (fh->oh->elf.root.u.def.section,
5530 fh->oh->elf.root.u.def.value,
5531 &fh->elf.root.u.def.section,
5532 &fh->elf.root.u.def.value) != (bfd_vma) -1)
5533 {
5534 fh->elf.root.type = fh->oh->elf.root.type;
5535 fh->elf.forced_local = 1;
5536 }
5537
5538 /* If this is a function code symbol, transfer dynamic linking
5539 information to the function descriptor symbol. */
5540 if (!fh->is_func)
5541 return TRUE;
5542
5543 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
5544 if (ent->plt.refcount > 0)
5545 break;
5546 if (ent == NULL
5547 || fh->elf.root.root.string[0] != '.'
5548 || fh->elf.root.root.string[1] == '\0')
5549 return TRUE;
5550
5551 /* Find the corresponding function descriptor symbol. Create it
5552 as undefined if necessary. */
5553
5554 fdh = get_fdh (fh, htab);
5555 if (fdh != NULL)
5556 while (fdh->elf.root.type == bfd_link_hash_indirect
5557 || fdh->elf.root.type == bfd_link_hash_warning)
5558 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5559
5560 if (fdh == NULL
5561 && info->shared
5562 && (fh->elf.root.type == bfd_link_hash_undefined
5563 || fh->elf.root.type == bfd_link_hash_undefweak))
5564 {
5565 fdh = make_fdh (info, fh);
5566 if (fdh == NULL)
5567 return FALSE;
5568 }
5569
5570 /* Fake function descriptors are made undefweak. If the function
5571 code symbol is strong undefined, make the fake sym the same.
5572 If the function code symbol is defined, then force the fake
5573 descriptor local; We can't support overriding of symbols in a
5574 shared library on a fake descriptor. */
5575
5576 if (fdh != NULL
5577 && fdh->fake
5578 && fdh->elf.root.type == bfd_link_hash_undefweak)
5579 {
5580 if (fh->elf.root.type == bfd_link_hash_undefined)
5581 {
5582 fdh->elf.root.type = bfd_link_hash_undefined;
5583 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5584 }
5585 else if (fh->elf.root.type == bfd_link_hash_defined
5586 || fh->elf.root.type == bfd_link_hash_defweak)
5587 {
5588 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5589 }
5590 }
5591
5592 if (fdh != NULL
5593 && !fdh->elf.forced_local
5594 && (info->shared
5595 || fdh->elf.def_dynamic
5596 || fdh->elf.ref_dynamic
5597 || (fdh->elf.root.type == bfd_link_hash_undefweak
5598 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5599 {
5600 if (fdh->elf.dynindx == -1)
5601 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5602 return FALSE;
5603 fdh->elf.ref_regular |= fh->elf.ref_regular;
5604 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5605 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5606 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
5607 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
5608 {
5609 move_plt_plist (fh, fdh);
5610 fdh->elf.needs_plt = 1;
5611 }
5612 fdh->is_func_descriptor = 1;
5613 fdh->oh = fh;
5614 fh->oh = fdh;
5615 }
5616
5617 /* Now that the info is on the function descriptor, clear the
5618 function code sym info. Any function code syms for which we
5619 don't have a definition in a regular file, we force local.
5620 This prevents a shared library from exporting syms that have
5621 been imported from another library. Function code syms that
5622 are really in the library we must leave global to prevent the
5623 linker dragging in a definition from a static library. */
5624 force_local = (!fh->elf.def_regular
5625 || fdh == NULL
5626 || !fdh->elf.def_regular
5627 || fdh->elf.forced_local);
5628 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5629
5630 return TRUE;
5631 }
5632
5633 /* Called near the start of bfd_elf_size_dynamic_sections. We use
5634 this hook to a) provide some gcc support functions, and b) transfer
5635 dynamic linking information gathered so far on function code symbol
5636 entries, to their corresponding function descriptor symbol entries. */
5637
5638 static bfd_boolean
5639 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5640 struct bfd_link_info *info)
5641 {
5642 struct ppc_link_hash_table *htab;
5643 unsigned int i;
5644 const struct sfpr_def_parms funcs[] =
5645 {
5646 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5647 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5648 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5649 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5650 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5651 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5652 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5653 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5654 { "._savef", 14, 31, savefpr, savefpr1_tail },
5655 { "._restf", 14, 31, restfpr, restfpr1_tail },
5656 { "_savevr_", 20, 31, savevr, savevr_tail },
5657 { "_restvr_", 20, 31, restvr, restvr_tail }
5658 };
5659
5660 htab = ppc_hash_table (info);
5661 if (htab->sfpr == NULL)
5662 /* We don't have any relocs. */
5663 return TRUE;
5664
5665 /* Provide any missing _save* and _rest* functions. */
5666 htab->sfpr->size = 0;
5667 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5668 if (!sfpr_define (info, &funcs[i]))
5669 return FALSE;
5670
5671 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5672
5673 if (htab->sfpr->size == 0)
5674 htab->sfpr->flags |= SEC_EXCLUDE;
5675
5676 return TRUE;
5677 }
5678
5679 /* Adjust a symbol defined by a dynamic object and referenced by a
5680 regular object. The current definition is in some section of the
5681 dynamic object, but we're not including those sections. We have to
5682 change the definition to something the rest of the link can
5683 understand. */
5684
5685 static bfd_boolean
5686 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5687 struct elf_link_hash_entry *h)
5688 {
5689 struct ppc_link_hash_table *htab;
5690 asection *s;
5691 unsigned int power_of_two;
5692
5693 htab = ppc_hash_table (info);
5694
5695 /* Deal with function syms. */
5696 if (h->type == STT_FUNC
5697 || h->needs_plt)
5698 {
5699 /* Clear procedure linkage table information for any symbol that
5700 won't need a .plt entry. */
5701 struct plt_entry *ent;
5702 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5703 if (ent->plt.refcount > 0)
5704 break;
5705 if (ent == NULL
5706 || SYMBOL_CALLS_LOCAL (info, h)
5707 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5708 && h->root.type == bfd_link_hash_undefweak))
5709 {
5710 h->plt.plist = NULL;
5711 h->needs_plt = 0;
5712 }
5713 }
5714 else
5715 h->plt.plist = NULL;
5716
5717 /* If this is a weak symbol, and there is a real definition, the
5718 processor independent code will have arranged for us to see the
5719 real definition first, and we can just use the same value. */
5720 if (h->u.weakdef != NULL)
5721 {
5722 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5723 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5724 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5725 h->root.u.def.value = h->u.weakdef->root.u.def.value;
5726 if (ELIMINATE_COPY_RELOCS)
5727 h->non_got_ref = h->u.weakdef->non_got_ref;
5728 return TRUE;
5729 }
5730
5731 /* If we are creating a shared library, we must presume that the
5732 only references to the symbol are via the global offset table.
5733 For such cases we need not do anything here; the relocations will
5734 be handled correctly by relocate_section. */
5735 if (info->shared)
5736 return TRUE;
5737
5738 /* If there are no references to this symbol that do not use the
5739 GOT, we don't need to generate a copy reloc. */
5740 if (!h->non_got_ref)
5741 return TRUE;
5742
5743 if (ELIMINATE_COPY_RELOCS)
5744 {
5745 struct ppc_link_hash_entry * eh;
5746 struct ppc_dyn_relocs *p;
5747
5748 eh = (struct ppc_link_hash_entry *) h;
5749 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5750 {
5751 s = p->sec->output_section;
5752 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5753 break;
5754 }
5755
5756 /* If we didn't find any dynamic relocs in read-only sections, then
5757 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5758 if (p == NULL)
5759 {
5760 h->non_got_ref = 0;
5761 return TRUE;
5762 }
5763 }
5764
5765 if (h->plt.plist != NULL)
5766 {
5767 /* We should never get here, but unfortunately there are versions
5768 of gcc out there that improperly (for this ABI) put initialized
5769 function pointers, vtable refs and suchlike in read-only
5770 sections. Allow them to proceed, but warn that this might
5771 break at runtime. */
5772 (*_bfd_error_handler)
5773 (_("copy reloc against `%s' requires lazy plt linking; "
5774 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5775 h->root.root.string);
5776 }
5777
5778 /* This is a reference to a symbol defined by a dynamic object which
5779 is not a function. */
5780
5781 if (h->size == 0)
5782 {
5783 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5784 h->root.root.string);
5785 return TRUE;
5786 }
5787
5788 /* We must allocate the symbol in our .dynbss section, which will
5789 become part of the .bss section of the executable. There will be
5790 an entry for this symbol in the .dynsym section. The dynamic
5791 object will contain position independent code, so all references
5792 from the dynamic object to this symbol will go through the global
5793 offset table. The dynamic linker will use the .dynsym entry to
5794 determine the address it must put in the global offset table, so
5795 both the dynamic object and the regular object will refer to the
5796 same memory location for the variable. */
5797
5798 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5799 to copy the initial value out of the dynamic object and into the
5800 runtime process image. We need to remember the offset into the
5801 .rela.bss section we are going to use. */
5802 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5803 {
5804 htab->relbss->size += sizeof (Elf64_External_Rela);
5805 h->needs_copy = 1;
5806 }
5807
5808 /* We need to figure out the alignment required for this symbol. I
5809 have no idea how ELF linkers handle this. */
5810 power_of_two = bfd_log2 (h->size);
5811 if (power_of_two > 4)
5812 power_of_two = 4;
5813
5814 /* Apply the required alignment. */
5815 s = htab->dynbss;
5816 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
5817 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5818 {
5819 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
5820 return FALSE;
5821 }
5822
5823 /* Define the symbol as being at this point in the section. */
5824 h->root.u.def.section = s;
5825 h->root.u.def.value = s->size;
5826
5827 /* Increment the section size to make room for the symbol. */
5828 s->size += h->size;
5829
5830 return TRUE;
5831 }
5832
5833 /* If given a function descriptor symbol, hide both the function code
5834 sym and the descriptor. */
5835 static void
5836 ppc64_elf_hide_symbol (struct bfd_link_info *info,
5837 struct elf_link_hash_entry *h,
5838 bfd_boolean force_local)
5839 {
5840 struct ppc_link_hash_entry *eh;
5841 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5842
5843 eh = (struct ppc_link_hash_entry *) h;
5844 if (eh->is_func_descriptor)
5845 {
5846 struct ppc_link_hash_entry *fh = eh->oh;
5847
5848 if (fh == NULL)
5849 {
5850 const char *p, *q;
5851 struct ppc_link_hash_table *htab;
5852 char save;
5853
5854 /* We aren't supposed to use alloca in BFD because on
5855 systems which do not have alloca the version in libiberty
5856 calls xmalloc, which might cause the program to crash
5857 when it runs out of memory. This function doesn't have a
5858 return status, so there's no way to gracefully return an
5859 error. So cheat. We know that string[-1] can be safely
5860 accessed; It's either a string in an ELF string table,
5861 or allocated in an objalloc structure. */
5862
5863 p = eh->elf.root.root.string - 1;
5864 save = *p;
5865 *(char *) p = '.';
5866 htab = ppc_hash_table (info);
5867 fh = (struct ppc_link_hash_entry *)
5868 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5869 *(char *) p = save;
5870
5871 /* Unfortunately, if it so happens that the string we were
5872 looking for was allocated immediately before this string,
5873 then we overwrote the string terminator. That's the only
5874 reason the lookup should fail. */
5875 if (fh == NULL)
5876 {
5877 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5878 while (q >= eh->elf.root.root.string && *q == *p)
5879 --q, --p;
5880 if (q < eh->elf.root.root.string && *p == '.')
5881 fh = (struct ppc_link_hash_entry *)
5882 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
5883 }
5884 if (fh != NULL)
5885 {
5886 eh->oh = fh;
5887 fh->oh = eh;
5888 }
5889 }
5890 if (fh != NULL)
5891 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5892 }
5893 }
5894
5895 static bfd_boolean
5896 get_sym_h (struct elf_link_hash_entry **hp,
5897 Elf_Internal_Sym **symp,
5898 asection **symsecp,
5899 char **tls_maskp,
5900 Elf_Internal_Sym **locsymsp,
5901 unsigned long r_symndx,
5902 bfd *ibfd)
5903 {
5904 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5905
5906 if (r_symndx >= symtab_hdr->sh_info)
5907 {
5908 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
5909 struct elf_link_hash_entry *h;
5910
5911 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5912 while (h->root.type == bfd_link_hash_indirect
5913 || h->root.type == bfd_link_hash_warning)
5914 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5915
5916 if (hp != NULL)
5917 *hp = h;
5918
5919 if (symp != NULL)
5920 *symp = NULL;
5921
5922 if (symsecp != NULL)
5923 {
5924 asection *symsec = NULL;
5925 if (h->root.type == bfd_link_hash_defined
5926 || h->root.type == bfd_link_hash_defweak)
5927 symsec = h->root.u.def.section;
5928 *symsecp = symsec;
5929 }
5930
5931 if (tls_maskp != NULL)
5932 {
5933 struct ppc_link_hash_entry *eh;
5934
5935 eh = (struct ppc_link_hash_entry *) h;
5936 *tls_maskp = &eh->tls_mask;
5937 }
5938 }
5939 else
5940 {
5941 Elf_Internal_Sym *sym;
5942 Elf_Internal_Sym *locsyms = *locsymsp;
5943
5944 if (locsyms == NULL)
5945 {
5946 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
5947 if (locsyms == NULL)
5948 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
5949 symtab_hdr->sh_info,
5950 0, NULL, NULL, NULL);
5951 if (locsyms == NULL)
5952 return FALSE;
5953 *locsymsp = locsyms;
5954 }
5955 sym = locsyms + r_symndx;
5956
5957 if (hp != NULL)
5958 *hp = NULL;
5959
5960 if (symp != NULL)
5961 *symp = sym;
5962
5963 if (symsecp != NULL)
5964 {
5965 asection *symsec = NULL;
5966 if ((sym->st_shndx != SHN_UNDEF
5967 && sym->st_shndx < SHN_LORESERVE)
5968 || sym->st_shndx > SHN_HIRESERVE)
5969 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
5970 *symsecp = symsec;
5971 }
5972
5973 if (tls_maskp != NULL)
5974 {
5975 struct got_entry **lgot_ents;
5976 char *tls_mask;
5977
5978 tls_mask = NULL;
5979 lgot_ents = elf_local_got_ents (ibfd);
5980 if (lgot_ents != NULL)
5981 {
5982 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
5983 tls_mask = &lgot_masks[r_symndx];
5984 }
5985 *tls_maskp = tls_mask;
5986 }
5987 }
5988 return TRUE;
5989 }
5990
5991 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
5992 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
5993 type suitable for optimization, and 1 otherwise. */
5994
5995 static int
5996 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
5997 Elf_Internal_Sym **locsymsp,
5998 const Elf_Internal_Rela *rel, bfd *ibfd)
5999 {
6000 unsigned long r_symndx;
6001 int next_r;
6002 struct elf_link_hash_entry *h;
6003 Elf_Internal_Sym *sym;
6004 asection *sec;
6005 bfd_vma off;
6006
6007 r_symndx = ELF64_R_SYM (rel->r_info);
6008 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6009 return 0;
6010
6011 if ((*tls_maskp != NULL && **tls_maskp != 0)
6012 || sec == NULL
6013 || ppc64_elf_section_data (sec)->t_symndx == NULL)
6014 return 1;
6015
6016 /* Look inside a TOC section too. */
6017 if (h != NULL)
6018 {
6019 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6020 off = h->root.u.def.value;
6021 }
6022 else
6023 off = sym->st_value;
6024 off += rel->r_addend;
6025 BFD_ASSERT (off % 8 == 0);
6026 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
6027 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
6028 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6029 return 0;
6030 if (toc_symndx != NULL)
6031 *toc_symndx = r_symndx;
6032 if ((h == NULL
6033 || ((h->root.type == bfd_link_hash_defined
6034 || h->root.type == bfd_link_hash_defweak)
6035 && !h->def_dynamic))
6036 && (next_r == -1 || next_r == -2))
6037 return 1 - next_r;
6038 return 1;
6039 }
6040
6041 /* Adjust all global syms defined in opd sections. In gcc generated
6042 code for the old ABI, these will already have been done. */
6043
6044 static bfd_boolean
6045 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6046 {
6047 struct ppc_link_hash_entry *eh;
6048 asection *sym_sec;
6049 long *opd_adjust;
6050
6051 if (h->root.type == bfd_link_hash_indirect)
6052 return TRUE;
6053
6054 if (h->root.type == bfd_link_hash_warning)
6055 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6056
6057 if (h->root.type != bfd_link_hash_defined
6058 && h->root.type != bfd_link_hash_defweak)
6059 return TRUE;
6060
6061 eh = (struct ppc_link_hash_entry *) h;
6062 if (eh->adjust_done)
6063 return TRUE;
6064
6065 sym_sec = eh->elf.root.u.def.section;
6066 opd_adjust = get_opd_info (sym_sec);
6067 if (opd_adjust != NULL)
6068 {
6069 long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
6070 if (adjust == -1)
6071 {
6072 /* This entry has been deleted. */
6073 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->u.deleted_section;
6074 if (dsec == NULL)
6075 {
6076 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6077 if (elf_discarded_section (dsec))
6078 {
6079 ppc64_elf_tdata (sym_sec->owner)->u.deleted_section = dsec;
6080 break;
6081 }
6082 }
6083 eh->elf.root.u.def.value = 0;
6084 eh->elf.root.u.def.section = dsec;
6085 }
6086 else
6087 eh->elf.root.u.def.value += adjust;
6088 eh->adjust_done = 1;
6089 }
6090 return TRUE;
6091 }
6092
6093 /* Handles decrementing dynamic reloc counts for the reloc specified by
6094 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6095 have already been determined. */
6096
6097 static bfd_boolean
6098 dec_dynrel_count (bfd_vma r_info,
6099 asection *sec,
6100 struct bfd_link_info *info,
6101 Elf_Internal_Sym **local_syms,
6102 struct elf_link_hash_entry *h,
6103 asection *sym_sec)
6104 {
6105 enum elf_ppc64_reloc_type r_type;
6106 struct ppc_dyn_relocs *p;
6107 struct ppc_dyn_relocs **pp;
6108
6109 /* Can this reloc be dynamic? This switch, and later tests here
6110 should be kept in sync with the code in check_relocs. */
6111 r_type = ELF64_R_TYPE (r_info);
6112 switch (r_type)
6113 {
6114 default:
6115 return TRUE;
6116
6117 case R_PPC64_TPREL16:
6118 case R_PPC64_TPREL16_LO:
6119 case R_PPC64_TPREL16_HI:
6120 case R_PPC64_TPREL16_HA:
6121 case R_PPC64_TPREL16_DS:
6122 case R_PPC64_TPREL16_LO_DS:
6123 case R_PPC64_TPREL16_HIGHER:
6124 case R_PPC64_TPREL16_HIGHERA:
6125 case R_PPC64_TPREL16_HIGHEST:
6126 case R_PPC64_TPREL16_HIGHESTA:
6127 if (!info->shared)
6128 return TRUE;
6129
6130 case R_PPC64_TPREL64:
6131 case R_PPC64_DTPMOD64:
6132 case R_PPC64_DTPREL64:
6133 case R_PPC64_ADDR64:
6134 case R_PPC64_REL30:
6135 case R_PPC64_REL32:
6136 case R_PPC64_REL64:
6137 case R_PPC64_ADDR14:
6138 case R_PPC64_ADDR14_BRNTAKEN:
6139 case R_PPC64_ADDR14_BRTAKEN:
6140 case R_PPC64_ADDR16:
6141 case R_PPC64_ADDR16_DS:
6142 case R_PPC64_ADDR16_HA:
6143 case R_PPC64_ADDR16_HI:
6144 case R_PPC64_ADDR16_HIGHER:
6145 case R_PPC64_ADDR16_HIGHERA:
6146 case R_PPC64_ADDR16_HIGHEST:
6147 case R_PPC64_ADDR16_HIGHESTA:
6148 case R_PPC64_ADDR16_LO:
6149 case R_PPC64_ADDR16_LO_DS:
6150 case R_PPC64_ADDR24:
6151 case R_PPC64_ADDR32:
6152 case R_PPC64_UADDR16:
6153 case R_PPC64_UADDR32:
6154 case R_PPC64_UADDR64:
6155 case R_PPC64_TOC:
6156 break;
6157 }
6158
6159 if (local_syms != NULL)
6160 {
6161 unsigned long r_symndx;
6162 Elf_Internal_Sym *sym;
6163 bfd *ibfd = sec->owner;
6164
6165 r_symndx = ELF64_R_SYM (r_info);
6166 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6167 return FALSE;
6168 }
6169
6170 if ((info->shared
6171 && (MUST_BE_DYN_RELOC (r_type)
6172 || (h != NULL
6173 && (!info->symbolic
6174 || h->root.type == bfd_link_hash_defweak
6175 || !h->def_regular))))
6176 || (ELIMINATE_COPY_RELOCS
6177 && !info->shared
6178 && h != NULL
6179 && (h->root.type == bfd_link_hash_defweak
6180 || !h->def_regular)))
6181 ;
6182 else
6183 return TRUE;
6184
6185 if (h != NULL)
6186 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6187 else
6188 {
6189 if (sym_sec != NULL)
6190 {
6191 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6192 pp = (struct ppc_dyn_relocs **) vpp;
6193 }
6194 else
6195 {
6196 void *vpp = &elf_section_data (sec)->local_dynrel;
6197 pp = (struct ppc_dyn_relocs **) vpp;
6198 }
6199
6200 /* elf_gc_sweep may have already removed all dyn relocs associated
6201 with local syms for a given section. Don't report a dynreloc
6202 miscount. */
6203 if (*pp == NULL)
6204 return TRUE;
6205 }
6206
6207 while ((p = *pp) != NULL)
6208 {
6209 if (p->sec == sec)
6210 {
6211 if (!MUST_BE_DYN_RELOC (r_type))
6212 p->pc_count -= 1;
6213 p->count -= 1;
6214 if (p->count == 0)
6215 *pp = p->next;
6216 return TRUE;
6217 }
6218 pp = &p->next;
6219 }
6220
6221 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6222 sec->owner, sec);
6223 bfd_set_error (bfd_error_bad_value);
6224 return FALSE;
6225 }
6226
6227 /* Remove unused Official Procedure Descriptor entries. Currently we
6228 only remove those associated with functions in discarded link-once
6229 sections, or weakly defined functions that have been overridden. It
6230 would be possible to remove many more entries for statically linked
6231 applications. */
6232
6233 bfd_boolean
6234 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
6235 bfd_boolean no_opd_opt,
6236 bfd_boolean non_overlapping)
6237 {
6238 bfd *ibfd;
6239 bfd_boolean some_edited = FALSE;
6240 asection *need_pad = NULL;
6241
6242 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6243 {
6244 asection *sec;
6245 Elf_Internal_Rela *relstart, *rel, *relend;
6246 Elf_Internal_Shdr *symtab_hdr;
6247 Elf_Internal_Sym *local_syms;
6248 struct elf_link_hash_entry **sym_hashes;
6249 bfd_vma offset;
6250 bfd_size_type amt;
6251 long *opd_adjust;
6252 bfd_boolean need_edit, add_aux_fields;
6253 bfd_size_type cnt_16b = 0;
6254
6255 sec = bfd_get_section_by_name (ibfd, ".opd");
6256 if (sec == NULL || sec->size == 0)
6257 continue;
6258
6259 amt = sec->size * sizeof (long) / 8;
6260 opd_adjust = get_opd_info (sec);
6261 if (opd_adjust == NULL)
6262 {
6263 /* check_relocs hasn't been called. Must be a ld -r link
6264 or --just-symbols object. */
6265 opd_adjust = bfd_alloc (obfd, amt);
6266 if (opd_adjust == NULL)
6267 return FALSE;
6268 ppc64_elf_section_data (sec)->opd.adjust = opd_adjust;
6269 }
6270 memset (opd_adjust, 0, amt);
6271
6272 if (no_opd_opt)
6273 continue;
6274
6275 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6276 continue;
6277
6278 if (sec->output_section == bfd_abs_section_ptr)
6279 continue;
6280
6281 /* Look through the section relocs. */
6282 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6283 continue;
6284
6285 local_syms = NULL;
6286 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6287 sym_hashes = elf_sym_hashes (ibfd);
6288
6289 /* Read the relocations. */
6290 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6291 info->keep_memory);
6292 if (relstart == NULL)
6293 return FALSE;
6294
6295 /* First run through the relocs to check they are sane, and to
6296 determine whether we need to edit this opd section. */
6297 need_edit = FALSE;
6298 need_pad = sec;
6299 offset = 0;
6300 relend = relstart + sec->reloc_count;
6301 for (rel = relstart; rel < relend; )
6302 {
6303 enum elf_ppc64_reloc_type r_type;
6304 unsigned long r_symndx;
6305 asection *sym_sec;
6306 struct elf_link_hash_entry *h;
6307 Elf_Internal_Sym *sym;
6308
6309 /* .opd contains a regular array of 16 or 24 byte entries. We're
6310 only interested in the reloc pointing to a function entry
6311 point. */
6312 if (rel->r_offset != offset
6313 || rel + 1 >= relend
6314 || (rel + 1)->r_offset != offset + 8)
6315 {
6316 /* If someone messes with .opd alignment then after a
6317 "ld -r" we might have padding in the middle of .opd.
6318 Also, there's nothing to prevent someone putting
6319 something silly in .opd with the assembler. No .opd
6320 optimization for them! */
6321 broken_opd:
6322 (*_bfd_error_handler)
6323 (_("%B: .opd is not a regular array of opd entries"), ibfd);
6324 need_edit = FALSE;
6325 break;
6326 }
6327
6328 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6329 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6330 {
6331 (*_bfd_error_handler)
6332 (_("%B: unexpected reloc type %u in .opd section"),
6333 ibfd, r_type);
6334 need_edit = FALSE;
6335 break;
6336 }
6337
6338 r_symndx = ELF64_R_SYM (rel->r_info);
6339 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6340 r_symndx, ibfd))
6341 goto error_ret;
6342
6343 if (sym_sec == NULL || sym_sec->owner == NULL)
6344 {
6345 const char *sym_name;
6346 if (h != NULL)
6347 sym_name = h->root.root.string;
6348 else
6349 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6350 sym_sec);
6351
6352 (*_bfd_error_handler)
6353 (_("%B: undefined sym `%s' in .opd section"),
6354 ibfd, sym_name);
6355 need_edit = FALSE;
6356 break;
6357 }
6358
6359 /* opd entries are always for functions defined in the
6360 current input bfd. If the symbol isn't defined in the
6361 input bfd, then we won't be using the function in this
6362 bfd; It must be defined in a linkonce section in another
6363 bfd, or is weak. It's also possible that we are
6364 discarding the function due to a linker script /DISCARD/,
6365 which we test for via the output_section. */
6366 if (sym_sec->owner != ibfd
6367 || sym_sec->output_section == bfd_abs_section_ptr)
6368 need_edit = TRUE;
6369
6370 rel += 2;
6371 if (rel == relend
6372 || (rel + 1 == relend && rel->r_offset == offset + 16))
6373 {
6374 if (sec->size == offset + 24)
6375 {
6376 need_pad = NULL;
6377 break;
6378 }
6379 if (rel == relend && sec->size == offset + 16)
6380 {
6381 cnt_16b++;
6382 break;
6383 }
6384 goto broken_opd;
6385 }
6386
6387 if (rel->r_offset == offset + 24)
6388 offset += 24;
6389 else if (rel->r_offset != offset + 16)
6390 goto broken_opd;
6391 else if (rel + 1 < relend
6392 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6393 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6394 {
6395 offset += 16;
6396 cnt_16b++;
6397 }
6398 else if (rel + 2 < relend
6399 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6400 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6401 {
6402 offset += 24;
6403 rel += 1;
6404 }
6405 else
6406 goto broken_opd;
6407 }
6408
6409 add_aux_fields = non_overlapping && cnt_16b > 0;
6410
6411 if (need_edit || add_aux_fields)
6412 {
6413 Elf_Internal_Rela *write_rel;
6414 bfd_byte *rptr, *wptr;
6415 bfd_byte *new_contents = NULL;
6416 bfd_boolean skip;
6417 long opd_ent_size;
6418
6419 /* This seems a waste of time as input .opd sections are all
6420 zeros as generated by gcc, but I suppose there's no reason
6421 this will always be so. We might start putting something in
6422 the third word of .opd entries. */
6423 if ((sec->flags & SEC_IN_MEMORY) == 0)
6424 {
6425 bfd_byte *loc;
6426 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6427 {
6428 if (loc != NULL)
6429 free (loc);
6430 error_ret:
6431 if (local_syms != NULL
6432 && symtab_hdr->contents != (unsigned char *) local_syms)
6433 free (local_syms);
6434 if (elf_section_data (sec)->relocs != relstart)
6435 free (relstart);
6436 return FALSE;
6437 }
6438 sec->contents = loc;
6439 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6440 }
6441
6442 elf_section_data (sec)->relocs = relstart;
6443
6444 new_contents = sec->contents;
6445 if (add_aux_fields)
6446 {
6447 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6448 if (new_contents == NULL)
6449 return FALSE;
6450 need_pad = FALSE;
6451 }
6452 wptr = new_contents;
6453 rptr = sec->contents;
6454
6455 write_rel = relstart;
6456 skip = FALSE;
6457 offset = 0;
6458 opd_ent_size = 0;
6459 for (rel = relstart; rel < relend; rel++)
6460 {
6461 unsigned long r_symndx;
6462 asection *sym_sec;
6463 struct elf_link_hash_entry *h;
6464 Elf_Internal_Sym *sym;
6465
6466 r_symndx = ELF64_R_SYM (rel->r_info);
6467 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6468 r_symndx, ibfd))
6469 goto error_ret;
6470
6471 if (rel->r_offset == offset)
6472 {
6473 struct ppc_link_hash_entry *fdh = NULL;
6474
6475 /* See if the .opd entry is full 24 byte or
6476 16 byte (with fd_aux entry overlapped with next
6477 fd_func). */
6478 opd_ent_size = 24;
6479 if ((rel + 2 == relend && sec->size == offset + 16)
6480 || (rel + 3 < relend
6481 && rel[2].r_offset == offset + 16
6482 && rel[3].r_offset == offset + 24
6483 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6484 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6485 opd_ent_size = 16;
6486
6487 if (h != NULL
6488 && h->root.root.string[0] == '.')
6489 {
6490 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6491 ppc_hash_table (info));
6492 if (fdh != NULL
6493 && fdh->elf.root.type != bfd_link_hash_defined
6494 && fdh->elf.root.type != bfd_link_hash_defweak)
6495 fdh = NULL;
6496 }
6497
6498 skip = (sym_sec->owner != ibfd
6499 || sym_sec->output_section == bfd_abs_section_ptr);
6500 if (skip)
6501 {
6502 if (fdh != NULL && sym_sec->owner == ibfd)
6503 {
6504 /* Arrange for the function descriptor sym
6505 to be dropped. */
6506 fdh->elf.root.u.def.value = 0;
6507 fdh->elf.root.u.def.section = sym_sec;
6508 }
6509 opd_adjust[rel->r_offset / 8] = -1;
6510 }
6511 else
6512 {
6513 /* We'll be keeping this opd entry. */
6514
6515 if (fdh != NULL)
6516 {
6517 /* Redefine the function descriptor symbol to
6518 this location in the opd section. It is
6519 necessary to update the value here rather
6520 than using an array of adjustments as we do
6521 for local symbols, because various places
6522 in the generic ELF code use the value
6523 stored in u.def.value. */
6524 fdh->elf.root.u.def.value = wptr - new_contents;
6525 fdh->adjust_done = 1;
6526 }
6527
6528 /* Local syms are a bit tricky. We could
6529 tweak them as they can be cached, but
6530 we'd need to look through the local syms
6531 for the function descriptor sym which we
6532 don't have at the moment. So keep an
6533 array of adjustments. */
6534 opd_adjust[rel->r_offset / 8]
6535 = (wptr - new_contents) - (rptr - sec->contents);
6536
6537 if (wptr != rptr)
6538 memcpy (wptr, rptr, opd_ent_size);
6539 wptr += opd_ent_size;
6540 if (add_aux_fields && opd_ent_size == 16)
6541 {
6542 memset (wptr, '\0', 8);
6543 wptr += 8;
6544 }
6545 }
6546 rptr += opd_ent_size;
6547 offset += opd_ent_size;
6548 }
6549
6550 if (skip)
6551 {
6552 if (!NO_OPD_RELOCS
6553 && !info->relocatable
6554 && !dec_dynrel_count (rel->r_info, sec, info,
6555 NULL, h, sym_sec))
6556 goto error_ret;
6557 }
6558 else
6559 {
6560 /* We need to adjust any reloc offsets to point to the
6561 new opd entries. While we're at it, we may as well
6562 remove redundant relocs. */
6563 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
6564 if (write_rel != rel)
6565 memcpy (write_rel, rel, sizeof (*rel));
6566 ++write_rel;
6567 }
6568 }
6569
6570 sec->size = wptr - new_contents;
6571 sec->reloc_count = write_rel - relstart;
6572 if (add_aux_fields)
6573 {
6574 free (sec->contents);
6575 sec->contents = new_contents;
6576 }
6577
6578 /* Fudge the size too, as this is used later in
6579 elf_bfd_final_link if we are emitting relocs. */
6580 elf_section_data (sec)->rel_hdr.sh_size
6581 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6582 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
6583 some_edited = TRUE;
6584 }
6585 else if (elf_section_data (sec)->relocs != relstart)
6586 free (relstart);
6587
6588 if (local_syms != NULL
6589 && symtab_hdr->contents != (unsigned char *) local_syms)
6590 {
6591 if (!info->keep_memory)
6592 free (local_syms);
6593 else
6594 symtab_hdr->contents = (unsigned char *) local_syms;
6595 }
6596 }
6597
6598 if (some_edited)
6599 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6600
6601 /* If we are doing a final link and the last .opd entry is just 16 byte
6602 long, add a 8 byte padding after it. */
6603 if (need_pad != NULL && !info->relocatable)
6604 {
6605 bfd_byte *p;
6606
6607 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6608 {
6609 BFD_ASSERT (need_pad->size > 0);
6610
6611 p = bfd_malloc (need_pad->size + 8);
6612 if (p == NULL)
6613 return FALSE;
6614
6615 if (! bfd_get_section_contents (need_pad->owner, need_pad,
6616 p, 0, need_pad->size))
6617 return FALSE;
6618
6619 need_pad->contents = p;
6620 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6621 }
6622 else
6623 {
6624 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6625 if (p == NULL)
6626 return FALSE;
6627
6628 need_pad->contents = p;
6629 }
6630
6631 memset (need_pad->contents + need_pad->size, 0, 8);
6632 need_pad->size += 8;
6633 }
6634
6635 return TRUE;
6636 }
6637
6638 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
6639
6640 asection *
6641 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
6642 {
6643 struct ppc_link_hash_table *htab;
6644
6645 htab = ppc_hash_table (info);
6646 if (htab->tls_get_addr != NULL)
6647 {
6648 struct ppc_link_hash_entry *h = htab->tls_get_addr;
6649
6650 while (h->elf.root.type == bfd_link_hash_indirect
6651 || h->elf.root.type == bfd_link_hash_warning)
6652 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6653
6654 htab->tls_get_addr = h;
6655
6656 if (htab->tls_get_addr_fd == NULL
6657 && h->oh != NULL
6658 && h->oh->is_func_descriptor
6659 && (h->oh->elf.root.type == bfd_link_hash_defined
6660 || h->oh->elf.root.type == bfd_link_hash_defweak))
6661 htab->tls_get_addr_fd = h->oh;
6662 }
6663
6664 if (htab->tls_get_addr_fd != NULL)
6665 {
6666 struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6667
6668 while (h->elf.root.type == bfd_link_hash_indirect
6669 || h->elf.root.type == bfd_link_hash_warning)
6670 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6671
6672 htab->tls_get_addr_fd = h;
6673 }
6674
6675 return _bfd_elf_tls_setup (obfd, info);
6676 }
6677
6678 /* Run through all the TLS relocs looking for optimization
6679 opportunities. The linker has been hacked (see ppc64elf.em) to do
6680 a preliminary section layout so that we know the TLS segment
6681 offsets. We can't optimize earlier because some optimizations need
6682 to know the tp offset, and we need to optimize before allocating
6683 dynamic relocations. */
6684
6685 bfd_boolean
6686 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6687 {
6688 bfd *ibfd;
6689 asection *sec;
6690 struct ppc_link_hash_table *htab;
6691
6692 if (info->relocatable || info->shared)
6693 return TRUE;
6694
6695 htab = ppc_hash_table (info);
6696 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6697 {
6698 Elf_Internal_Sym *locsyms = NULL;
6699
6700 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
6701 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6702 {
6703 Elf_Internal_Rela *relstart, *rel, *relend;
6704 int expecting_tls_get_addr;
6705
6706 /* Read the relocations. */
6707 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
6708 info->keep_memory);
6709 if (relstart == NULL)
6710 return FALSE;
6711
6712 expecting_tls_get_addr = 0;
6713 relend = relstart + sec->reloc_count;
6714 for (rel = relstart; rel < relend; rel++)
6715 {
6716 enum elf_ppc64_reloc_type r_type;
6717 unsigned long r_symndx;
6718 struct elf_link_hash_entry *h;
6719 Elf_Internal_Sym *sym;
6720 asection *sym_sec;
6721 char *tls_mask;
6722 char tls_set, tls_clear, tls_type = 0;
6723 bfd_vma value;
6724 bfd_boolean ok_tprel, is_local;
6725
6726 r_symndx = ELF64_R_SYM (rel->r_info);
6727 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
6728 r_symndx, ibfd))
6729 {
6730 err_free_rel:
6731 if (elf_section_data (sec)->relocs != relstart)
6732 free (relstart);
6733 if (locsyms != NULL
6734 && (elf_tdata (ibfd)->symtab_hdr.contents
6735 != (unsigned char *) locsyms))
6736 free (locsyms);
6737 return FALSE;
6738 }
6739
6740 if (h != NULL)
6741 {
6742 if (h->root.type != bfd_link_hash_defined
6743 && h->root.type != bfd_link_hash_defweak)
6744 continue;
6745 value = h->root.u.def.value;
6746 }
6747 else
6748 /* Symbols referenced by TLS relocs must be of type
6749 STT_TLS. So no need for .opd local sym adjust. */
6750 value = sym->st_value;
6751
6752 ok_tprel = FALSE;
6753 is_local = FALSE;
6754 if (h == NULL
6755 || !h->def_dynamic)
6756 {
6757 is_local = TRUE;
6758 value += sym_sec->output_offset;
6759 value += sym_sec->output_section->vma;
6760 value -= htab->elf.tls_sec->vma;
6761 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6762 < (bfd_vma) 1 << 32);
6763 }
6764
6765 r_type = ELF64_R_TYPE (rel->r_info);
6766 switch (r_type)
6767 {
6768 case R_PPC64_GOT_TLSLD16:
6769 case R_PPC64_GOT_TLSLD16_LO:
6770 case R_PPC64_GOT_TLSLD16_HI:
6771 case R_PPC64_GOT_TLSLD16_HA:
6772 /* These relocs should never be against a symbol
6773 defined in a shared lib. Leave them alone if
6774 that turns out to be the case. */
6775 ppc64_tlsld_got (ibfd)->refcount -= 1;
6776 if (!is_local)
6777 continue;
6778
6779 /* LD -> LE */
6780 tls_set = 0;
6781 tls_clear = TLS_LD;
6782 tls_type = TLS_TLS | TLS_LD;
6783 expecting_tls_get_addr = 1;
6784 break;
6785
6786 case R_PPC64_GOT_TLSGD16:
6787 case R_PPC64_GOT_TLSGD16_LO:
6788 case R_PPC64_GOT_TLSGD16_HI:
6789 case R_PPC64_GOT_TLSGD16_HA:
6790 if (ok_tprel)
6791 /* GD -> LE */
6792 tls_set = 0;
6793 else
6794 /* GD -> IE */
6795 tls_set = TLS_TLS | TLS_TPRELGD;
6796 tls_clear = TLS_GD;
6797 tls_type = TLS_TLS | TLS_GD;
6798 expecting_tls_get_addr = 1;
6799 break;
6800
6801 case R_PPC64_GOT_TPREL16_DS:
6802 case R_PPC64_GOT_TPREL16_LO_DS:
6803 case R_PPC64_GOT_TPREL16_HI:
6804 case R_PPC64_GOT_TPREL16_HA:
6805 expecting_tls_get_addr = 0;
6806 if (ok_tprel)
6807 {
6808 /* IE -> LE */
6809 tls_set = 0;
6810 tls_clear = TLS_TPREL;
6811 tls_type = TLS_TLS | TLS_TPREL;
6812 break;
6813 }
6814 else
6815 continue;
6816
6817 case R_PPC64_REL14:
6818 case R_PPC64_REL14_BRTAKEN:
6819 case R_PPC64_REL14_BRNTAKEN:
6820 case R_PPC64_REL24:
6821 if (h != NULL
6822 && (h == &htab->tls_get_addr->elf
6823 || h == &htab->tls_get_addr_fd->elf))
6824 {
6825 if (!expecting_tls_get_addr
6826 && rel != relstart
6827 && ((ELF64_R_TYPE (rel[-1].r_info)
6828 == R_PPC64_TOC16)
6829 || (ELF64_R_TYPE (rel[-1].r_info)
6830 == R_PPC64_TOC16_LO)))
6831 {
6832 /* Check for toc tls entries. */
6833 char *toc_tls;
6834 int retval;
6835
6836 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
6837 rel - 1, ibfd);
6838 if (retval == 0)
6839 goto err_free_rel;
6840 if (toc_tls != NULL)
6841 expecting_tls_get_addr = retval > 1;
6842 }
6843
6844 if (expecting_tls_get_addr)
6845 {
6846 struct plt_entry *ent;
6847 for (ent = h->plt.plist; ent; ent = ent->next)
6848 if (ent->addend == 0)
6849 {
6850 if (ent->plt.refcount > 0)
6851 ent->plt.refcount -= 1;
6852 break;
6853 }
6854 }
6855 }
6856 expecting_tls_get_addr = 0;
6857 continue;
6858
6859 case R_PPC64_TPREL64:
6860 expecting_tls_get_addr = 0;
6861 if (ok_tprel)
6862 {
6863 /* IE -> LE */
6864 tls_set = TLS_EXPLICIT;
6865 tls_clear = TLS_TPREL;
6866 break;
6867 }
6868 else
6869 continue;
6870
6871 case R_PPC64_DTPMOD64:
6872 expecting_tls_get_addr = 0;
6873 if (rel + 1 < relend
6874 && (rel[1].r_info
6875 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
6876 && rel[1].r_offset == rel->r_offset + 8)
6877 {
6878 if (ok_tprel)
6879 /* GD -> LE */
6880 tls_set = TLS_EXPLICIT | TLS_GD;
6881 else
6882 /* GD -> IE */
6883 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
6884 tls_clear = TLS_GD;
6885 }
6886 else
6887 {
6888 if (!is_local)
6889 continue;
6890
6891 /* LD -> LE */
6892 tls_set = TLS_EXPLICIT;
6893 tls_clear = TLS_LD;
6894 }
6895 break;
6896
6897 default:
6898 expecting_tls_get_addr = 0;
6899 continue;
6900 }
6901
6902 if ((tls_set & TLS_EXPLICIT) == 0)
6903 {
6904 struct got_entry *ent;
6905
6906 /* Adjust got entry for this reloc. */
6907 if (h != NULL)
6908 ent = h->got.glist;
6909 else
6910 ent = elf_local_got_ents (ibfd)[r_symndx];
6911
6912 for (; ent != NULL; ent = ent->next)
6913 if (ent->addend == rel->r_addend
6914 && ent->owner == ibfd
6915 && ent->tls_type == tls_type)
6916 break;
6917 if (ent == NULL)
6918 abort ();
6919
6920 if (tls_set == 0)
6921 {
6922 /* We managed to get rid of a got entry. */
6923 if (ent->got.refcount > 0)
6924 ent->got.refcount -= 1;
6925 }
6926 }
6927 else
6928 {
6929 /* If we got rid of a DTPMOD/DTPREL reloc pair then
6930 we'll lose one or two dyn relocs. */
6931 if (!dec_dynrel_count (rel->r_info, sec, info,
6932 NULL, h, sym_sec))
6933 return FALSE;
6934
6935 if (tls_set == (TLS_EXPLICIT | TLS_GD))
6936 {
6937 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
6938 NULL, h, sym_sec))
6939 return FALSE;
6940 }
6941 }
6942
6943 *tls_mask |= tls_set;
6944 *tls_mask &= ~tls_clear;
6945 }
6946
6947 if (elf_section_data (sec)->relocs != relstart)
6948 free (relstart);
6949 }
6950
6951 if (locsyms != NULL
6952 && (elf_tdata (ibfd)->symtab_hdr.contents
6953 != (unsigned char *) locsyms))
6954 {
6955 if (!info->keep_memory)
6956 free (locsyms);
6957 else
6958 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
6959 }
6960 }
6961 return TRUE;
6962 }
6963
6964 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
6965 the values of any global symbols in a toc section that has been
6966 edited. Globals in toc sections should be a rarity, so this function
6967 sets a flag if any are found in toc sections other than the one just
6968 edited, so that futher hash table traversals can be avoided. */
6969
6970 struct adjust_toc_info
6971 {
6972 asection *toc;
6973 unsigned long *skip;
6974 bfd_boolean global_toc_syms;
6975 };
6976
6977 static bfd_boolean
6978 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
6979 {
6980 struct ppc_link_hash_entry *eh;
6981 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
6982
6983 if (h->root.type == bfd_link_hash_indirect)
6984 return TRUE;
6985
6986 if (h->root.type == bfd_link_hash_warning)
6987 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6988
6989 if (h->root.type != bfd_link_hash_defined
6990 && h->root.type != bfd_link_hash_defweak)
6991 return TRUE;
6992
6993 eh = (struct ppc_link_hash_entry *) h;
6994 if (eh->adjust_done)
6995 return TRUE;
6996
6997 if (eh->elf.root.u.def.section == toc_inf->toc)
6998 {
6999 unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7000 if (skip != (unsigned long) -1)
7001 eh->elf.root.u.def.value -= skip;
7002 else
7003 {
7004 (*_bfd_error_handler)
7005 (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7006 eh->elf.root.u.def.section = &bfd_abs_section;
7007 eh->elf.root.u.def.value = 0;
7008 }
7009 eh->adjust_done = 1;
7010 }
7011 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7012 toc_inf->global_toc_syms = TRUE;
7013
7014 return TRUE;
7015 }
7016
7017 /* Examine all relocs referencing .toc sections in order to remove
7018 unused .toc entries. */
7019
7020 bfd_boolean
7021 ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7022 {
7023 bfd *ibfd;
7024 struct adjust_toc_info toc_inf;
7025
7026 toc_inf.global_toc_syms = TRUE;
7027 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7028 {
7029 asection *toc, *sec;
7030 Elf_Internal_Shdr *symtab_hdr;
7031 Elf_Internal_Sym *local_syms;
7032 struct elf_link_hash_entry **sym_hashes;
7033 Elf_Internal_Rela *relstart, *rel;
7034 unsigned long *skip, *drop;
7035 unsigned char *used;
7036 unsigned char *keep, last, some_unused;
7037
7038 toc = bfd_get_section_by_name (ibfd, ".toc");
7039 if (toc == NULL
7040 || toc->size == 0
7041 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7042 || elf_discarded_section (toc))
7043 continue;
7044
7045 local_syms = NULL;
7046 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7047 sym_hashes = elf_sym_hashes (ibfd);
7048
7049 /* Look at sections dropped from the final link. */
7050 skip = NULL;
7051 relstart = NULL;
7052 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7053 {
7054 if (sec->reloc_count == 0
7055 || !elf_discarded_section (sec)
7056 || get_opd_info (sec)
7057 || (sec->flags & SEC_ALLOC) == 0
7058 || (sec->flags & SEC_DEBUGGING) != 0)
7059 continue;
7060
7061 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7062 if (relstart == NULL)
7063 goto error_ret;
7064
7065 /* Run through the relocs to see which toc entries might be
7066 unused. */
7067 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7068 {
7069 enum elf_ppc64_reloc_type r_type;
7070 unsigned long r_symndx;
7071 asection *sym_sec;
7072 struct elf_link_hash_entry *h;
7073 Elf_Internal_Sym *sym;
7074 bfd_vma val;
7075
7076 r_type = ELF64_R_TYPE (rel->r_info);
7077 switch (r_type)
7078 {
7079 default:
7080 continue;
7081
7082 case R_PPC64_TOC16:
7083 case R_PPC64_TOC16_LO:
7084 case R_PPC64_TOC16_HI:
7085 case R_PPC64_TOC16_HA:
7086 case R_PPC64_TOC16_DS:
7087 case R_PPC64_TOC16_LO_DS:
7088 break;
7089 }
7090
7091 r_symndx = ELF64_R_SYM (rel->r_info);
7092 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7093 r_symndx, ibfd))
7094 goto error_ret;
7095
7096 if (sym_sec != toc)
7097 continue;
7098
7099 if (h != NULL)
7100 val = h->root.u.def.value;
7101 else
7102 val = sym->st_value;
7103 val += rel->r_addend;
7104
7105 if (val >= toc->size)
7106 continue;
7107
7108 /* Anything in the toc ought to be aligned to 8 bytes.
7109 If not, don't mark as unused. */
7110 if (val & 7)
7111 continue;
7112
7113 if (skip == NULL)
7114 {
7115 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7116 if (skip == NULL)
7117 goto error_ret;
7118 }
7119
7120 skip[val >> 3] = 1;
7121 }
7122
7123 if (elf_section_data (sec)->relocs != relstart)
7124 free (relstart);
7125 }
7126
7127 if (skip == NULL)
7128 continue;
7129
7130 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7131 if (used == NULL)
7132 {
7133 error_ret:
7134 if (local_syms != NULL
7135 && symtab_hdr->contents != (unsigned char *) local_syms)
7136 free (local_syms);
7137 if (sec != NULL
7138 && relstart != NULL
7139 && elf_section_data (sec)->relocs != relstart)
7140 free (relstart);
7141 if (skip != NULL)
7142 free (skip);
7143 return FALSE;
7144 }
7145
7146 /* Now check all kept sections that might reference the toc. */
7147 for (sec = ibfd->sections;
7148 sec != NULL;
7149 /* Check the toc itself last. */
7150 sec = (sec == toc ? NULL
7151 : sec->next == toc && sec->next->next ? sec->next->next
7152 : sec->next == NULL ? toc
7153 : sec->next))
7154 {
7155 int repeat;
7156
7157 if (sec->reloc_count == 0
7158 || elf_discarded_section (sec)
7159 || get_opd_info (sec)
7160 || (sec->flags & SEC_ALLOC) == 0
7161 || (sec->flags & SEC_DEBUGGING) != 0)
7162 continue;
7163
7164 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7165 if (relstart == NULL)
7166 goto error_ret;
7167
7168 /* Mark toc entries referenced as used. */
7169 repeat = 0;
7170 do
7171 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7172 {
7173 enum elf_ppc64_reloc_type r_type;
7174 unsigned long r_symndx;
7175 asection *sym_sec;
7176 struct elf_link_hash_entry *h;
7177 Elf_Internal_Sym *sym;
7178 bfd_vma val;
7179
7180 r_type = ELF64_R_TYPE (rel->r_info);
7181 switch (r_type)
7182 {
7183 case R_PPC64_TOC16:
7184 case R_PPC64_TOC16_LO:
7185 case R_PPC64_TOC16_HI:
7186 case R_PPC64_TOC16_HA:
7187 case R_PPC64_TOC16_DS:
7188 case R_PPC64_TOC16_LO_DS:
7189 /* In case we're taking addresses of toc entries. */
7190 case R_PPC64_ADDR64:
7191 break;
7192
7193 default:
7194 continue;
7195 }
7196
7197 r_symndx = ELF64_R_SYM (rel->r_info);
7198 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7199 r_symndx, ibfd))
7200 {
7201 free (used);
7202 goto error_ret;
7203 }
7204
7205 if (sym_sec != toc)
7206 continue;
7207
7208 if (h != NULL)
7209 val = h->root.u.def.value;
7210 else
7211 val = sym->st_value;
7212 val += rel->r_addend;
7213
7214 if (val >= toc->size)
7215 continue;
7216
7217 /* For the toc section, we only mark as used if
7218 this entry itself isn't unused. */
7219 if (sec == toc
7220 && !used[val >> 3]
7221 && (used[rel->r_offset >> 3]
7222 || !skip[rel->r_offset >> 3]))
7223 /* Do all the relocs again, to catch reference
7224 chains. */
7225 repeat = 1;
7226
7227 used[val >> 3] = 1;
7228 }
7229 while (repeat);
7230 }
7231
7232 /* Merge the used and skip arrays. Assume that TOC
7233 doublewords not appearing as either used or unused belong
7234 to to an entry more than one doubleword in size. */
7235 for (drop = skip, keep = used, last = 0, some_unused = 0;
7236 drop < skip + (toc->size + 7) / 8;
7237 ++drop, ++keep)
7238 {
7239 if (*keep)
7240 {
7241 *drop = 0;
7242 last = 0;
7243 }
7244 else if (*drop)
7245 {
7246 some_unused = 1;
7247 last = 1;
7248 }
7249 else
7250 *drop = last;
7251 }
7252
7253 free (used);
7254
7255 if (some_unused)
7256 {
7257 bfd_byte *contents, *src;
7258 unsigned long off;
7259
7260 /* Shuffle the toc contents, and at the same time convert the
7261 skip array from booleans into offsets. */
7262 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7263 goto error_ret;
7264
7265 elf_section_data (toc)->this_hdr.contents = contents;
7266
7267 for (src = contents, off = 0, drop = skip;
7268 src < contents + toc->size;
7269 src += 8, ++drop)
7270 {
7271 if (*drop)
7272 {
7273 *drop = (unsigned long) -1;
7274 off += 8;
7275 }
7276 else if (off != 0)
7277 {
7278 *drop = off;
7279 memcpy (src - off, src, 8);
7280 }
7281 }
7282 toc->rawsize = toc->size;
7283 toc->size = src - contents - off;
7284
7285 if (toc->reloc_count != 0)
7286 {
7287 Elf_Internal_Rela *wrel;
7288 bfd_size_type sz;
7289
7290 /* Read toc relocs. */
7291 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7292 TRUE);
7293 if (relstart == NULL)
7294 goto error_ret;
7295
7296 /* Remove unused toc relocs, and adjust those we keep. */
7297 wrel = relstart;
7298 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7299 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7300 {
7301 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7302 wrel->r_info = rel->r_info;
7303 wrel->r_addend = rel->r_addend;
7304 ++wrel;
7305 }
7306 else if (!dec_dynrel_count (rel->r_info, toc, info,
7307 &local_syms, NULL, NULL))
7308 goto error_ret;
7309
7310 toc->reloc_count = wrel - relstart;
7311 sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7312 elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7313 BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7314 }
7315
7316 /* Adjust addends for relocs against the toc section sym. */
7317 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7318 {
7319 if (sec->reloc_count == 0
7320 || elf_discarded_section (sec))
7321 continue;
7322
7323 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7324 TRUE);
7325 if (relstart == NULL)
7326 goto error_ret;
7327
7328 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7329 {
7330 enum elf_ppc64_reloc_type r_type;
7331 unsigned long r_symndx;
7332 asection *sym_sec;
7333 struct elf_link_hash_entry *h;
7334 Elf_Internal_Sym *sym;
7335
7336 r_type = ELF64_R_TYPE (rel->r_info);
7337 switch (r_type)
7338 {
7339 default:
7340 continue;
7341
7342 case R_PPC64_TOC16:
7343 case R_PPC64_TOC16_LO:
7344 case R_PPC64_TOC16_HI:
7345 case R_PPC64_TOC16_HA:
7346 case R_PPC64_TOC16_DS:
7347 case R_PPC64_TOC16_LO_DS:
7348 case R_PPC64_ADDR64:
7349 break;
7350 }
7351
7352 r_symndx = ELF64_R_SYM (rel->r_info);
7353 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7354 r_symndx, ibfd))
7355 goto error_ret;
7356
7357 if (sym_sec != toc || h != NULL || sym->st_value != 0)
7358 continue;
7359
7360 rel->r_addend -= skip[rel->r_addend >> 3];
7361 }
7362 }
7363
7364 /* We shouldn't have local or global symbols defined in the TOC,
7365 but handle them anyway. */
7366 if (local_syms != NULL)
7367 {
7368 Elf_Internal_Sym *sym;
7369
7370 for (sym = local_syms;
7371 sym < local_syms + symtab_hdr->sh_info;
7372 ++sym)
7373 if (sym->st_shndx != SHN_UNDEF
7374 && (sym->st_shndx < SHN_LORESERVE
7375 || sym->st_shndx > SHN_HIRESERVE)
7376 && sym->st_value != 0
7377 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7378 {
7379 if (skip[sym->st_value >> 3] != (unsigned long) -1)
7380 sym->st_value -= skip[sym->st_value >> 3];
7381 else
7382 {
7383 (*_bfd_error_handler)
7384 (_("%s defined in removed toc entry"),
7385 bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7386 NULL));
7387 sym->st_value = 0;
7388 sym->st_shndx = SHN_ABS;
7389 }
7390 symtab_hdr->contents = (unsigned char *) local_syms;
7391 }
7392 }
7393
7394 /* Finally, adjust any global syms defined in the toc. */
7395 if (toc_inf.global_toc_syms)
7396 {
7397 toc_inf.toc = toc;
7398 toc_inf.skip = skip;
7399 toc_inf.global_toc_syms = FALSE;
7400 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7401 &toc_inf);
7402 }
7403 }
7404
7405 if (local_syms != NULL
7406 && symtab_hdr->contents != (unsigned char *) local_syms)
7407 {
7408 if (!info->keep_memory)
7409 free (local_syms);
7410 else
7411 symtab_hdr->contents = (unsigned char *) local_syms;
7412 }
7413 free (skip);
7414 }
7415
7416 return TRUE;
7417 }
7418
7419 /* Allocate space in .plt, .got and associated reloc sections for
7420 dynamic relocs. */
7421
7422 static bfd_boolean
7423 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7424 {
7425 struct bfd_link_info *info;
7426 struct ppc_link_hash_table *htab;
7427 asection *s;
7428 struct ppc_link_hash_entry *eh;
7429 struct ppc_dyn_relocs *p;
7430 struct got_entry *gent;
7431
7432 if (h->root.type == bfd_link_hash_indirect)
7433 return TRUE;
7434
7435 if (h->root.type == bfd_link_hash_warning)
7436 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7437
7438 info = (struct bfd_link_info *) inf;
7439 htab = ppc_hash_table (info);
7440
7441 if (htab->elf.dynamic_sections_created
7442 && h->dynindx != -1
7443 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
7444 {
7445 struct plt_entry *pent;
7446 bfd_boolean doneone = FALSE;
7447 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7448 if (pent->plt.refcount > 0)
7449 {
7450 /* If this is the first .plt entry, make room for the special
7451 first entry. */
7452 s = htab->plt;
7453 if (s->size == 0)
7454 s->size += PLT_INITIAL_ENTRY_SIZE;
7455
7456 pent->plt.offset = s->size;
7457
7458 /* Make room for this entry. */
7459 s->size += PLT_ENTRY_SIZE;
7460
7461 /* Make room for the .glink code. */
7462 s = htab->glink;
7463 if (s->size == 0)
7464 s->size += GLINK_CALL_STUB_SIZE;
7465 /* We need bigger stubs past index 32767. */
7466 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7467 s->size += 4;
7468 s->size += 2*4;
7469
7470 /* We also need to make an entry in the .rela.plt section. */
7471 s = htab->relplt;
7472 s->size += sizeof (Elf64_External_Rela);
7473 doneone = TRUE;
7474 }
7475 else
7476 pent->plt.offset = (bfd_vma) -1;
7477 if (!doneone)
7478 {
7479 h->plt.plist = NULL;
7480 h->needs_plt = 0;
7481 }
7482 }
7483 else
7484 {
7485 h->plt.plist = NULL;
7486 h->needs_plt = 0;
7487 }
7488
7489 eh = (struct ppc_link_hash_entry *) h;
7490 /* Run through the TLS GD got entries first if we're changing them
7491 to TPREL. */
7492 if ((eh->tls_mask & TLS_TPRELGD) != 0)
7493 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7494 if (gent->got.refcount > 0
7495 && (gent->tls_type & TLS_GD) != 0)
7496 {
7497 /* This was a GD entry that has been converted to TPREL. If
7498 there happens to be a TPREL entry we can use that one. */
7499 struct got_entry *ent;
7500 for (ent = h->got.glist; ent != NULL; ent = ent->next)
7501 if (ent->got.refcount > 0
7502 && (ent->tls_type & TLS_TPREL) != 0
7503 && ent->addend == gent->addend
7504 && ent->owner == gent->owner)
7505 {
7506 gent->got.refcount = 0;
7507 break;
7508 }
7509
7510 /* If not, then we'll be using our own TPREL entry. */
7511 if (gent->got.refcount != 0)
7512 gent->tls_type = TLS_TLS | TLS_TPREL;
7513 }
7514
7515 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7516 if (gent->got.refcount > 0)
7517 {
7518 bfd_boolean dyn;
7519
7520 /* Make sure this symbol is output as a dynamic symbol.
7521 Undefined weak syms won't yet be marked as dynamic,
7522 nor will all TLS symbols. */
7523 if (h->dynindx == -1
7524 && !h->forced_local)
7525 {
7526 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7527 return FALSE;
7528 }
7529
7530 if ((gent->tls_type & TLS_LD) != 0
7531 && !h->def_dynamic)
7532 {
7533 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
7534 continue;
7535 }
7536
7537 s = ppc64_elf_tdata (gent->owner)->got;
7538 gent->got.offset = s->size;
7539 s->size
7540 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
7541 dyn = htab->elf.dynamic_sections_created;
7542 if ((info->shared
7543 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7544 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7545 || h->root.type != bfd_link_hash_undefweak))
7546 ppc64_elf_tdata (gent->owner)->relgot->size
7547 += (gent->tls_type & eh->tls_mask & TLS_GD
7548 ? 2 * sizeof (Elf64_External_Rela)
7549 : sizeof (Elf64_External_Rela));
7550 }
7551 else
7552 gent->got.offset = (bfd_vma) -1;
7553
7554 if (eh->dyn_relocs == NULL)
7555 return TRUE;
7556
7557 /* In the shared -Bsymbolic case, discard space allocated for
7558 dynamic pc-relative relocs against symbols which turn out to be
7559 defined in regular objects. For the normal shared case, discard
7560 space for relocs that have become local due to symbol visibility
7561 changes. */
7562
7563 if (info->shared)
7564 {
7565 /* Relocs that use pc_count are those that appear on a call insn,
7566 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7567 generated via assembly. We want calls to protected symbols to
7568 resolve directly to the function rather than going via the plt.
7569 If people want function pointer comparisons to work as expected
7570 then they should avoid writing weird assembly. */
7571 if (SYMBOL_CALLS_LOCAL (info, h))
7572 {
7573 struct ppc_dyn_relocs **pp;
7574
7575 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
7576 {
7577 p->count -= p->pc_count;
7578 p->pc_count = 0;
7579 if (p->count == 0)
7580 *pp = p->next;
7581 else
7582 pp = &p->next;
7583 }
7584 }
7585
7586 /* Also discard relocs on undefined weak syms with non-default
7587 visibility. */
7588 if (eh->dyn_relocs != NULL
7589 && h->root.type == bfd_link_hash_undefweak)
7590 {
7591 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7592 eh->dyn_relocs = NULL;
7593
7594 /* Make sure this symbol is output as a dynamic symbol.
7595 Undefined weak syms won't yet be marked as dynamic. */
7596 else if (h->dynindx == -1
7597 && !h->forced_local)
7598 {
7599 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7600 return FALSE;
7601 }
7602 }
7603 }
7604 else if (ELIMINATE_COPY_RELOCS)
7605 {
7606 /* For the non-shared case, discard space for relocs against
7607 symbols which turn out to need copy relocs or are not
7608 dynamic. */
7609
7610 if (!h->non_got_ref
7611 && h->def_dynamic
7612 && !h->def_regular)
7613 {
7614 /* Make sure this symbol is output as a dynamic symbol.
7615 Undefined weak syms won't yet be marked as dynamic. */
7616 if (h->dynindx == -1
7617 && !h->forced_local)
7618 {
7619 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7620 return FALSE;
7621 }
7622
7623 /* If that succeeded, we know we'll be keeping all the
7624 relocs. */
7625 if (h->dynindx != -1)
7626 goto keep;
7627 }
7628
7629 eh->dyn_relocs = NULL;
7630
7631 keep: ;
7632 }
7633
7634 /* Finally, allocate space. */
7635 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7636 {
7637 asection *sreloc = elf_section_data (p->sec)->sreloc;
7638 sreloc->size += p->count * sizeof (Elf64_External_Rela);
7639 }
7640
7641 return TRUE;
7642 }
7643
7644 /* Find any dynamic relocs that apply to read-only sections. */
7645
7646 static bfd_boolean
7647 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
7648 {
7649 struct ppc_link_hash_entry *eh;
7650 struct ppc_dyn_relocs *p;
7651
7652 if (h->root.type == bfd_link_hash_warning)
7653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7654
7655 eh = (struct ppc_link_hash_entry *) h;
7656 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7657 {
7658 asection *s = p->sec->output_section;
7659
7660 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7661 {
7662 struct bfd_link_info *info = inf;
7663
7664 info->flags |= DF_TEXTREL;
7665
7666 /* Not an error, just cut short the traversal. */
7667 return FALSE;
7668 }
7669 }
7670 return TRUE;
7671 }
7672
7673 /* Set the sizes of the dynamic sections. */
7674
7675 static bfd_boolean
7676 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7677 struct bfd_link_info *info)
7678 {
7679 struct ppc_link_hash_table *htab;
7680 bfd *dynobj;
7681 asection *s;
7682 bfd_boolean relocs;
7683 bfd *ibfd;
7684
7685 htab = ppc_hash_table (info);
7686 dynobj = htab->elf.dynobj;
7687 if (dynobj == NULL)
7688 abort ();
7689
7690 if (htab->elf.dynamic_sections_created)
7691 {
7692 /* Set the contents of the .interp section to the interpreter. */
7693 if (info->executable)
7694 {
7695 s = bfd_get_section_by_name (dynobj, ".interp");
7696 if (s == NULL)
7697 abort ();
7698 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
7699 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7700 }
7701 }
7702
7703 /* Set up .got offsets for local syms, and space for local dynamic
7704 relocs. */
7705 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7706 {
7707 struct got_entry **lgot_ents;
7708 struct got_entry **end_lgot_ents;
7709 char *lgot_masks;
7710 bfd_size_type locsymcount;
7711 Elf_Internal_Shdr *symtab_hdr;
7712 asection *srel;
7713
7714 if (!is_ppc64_elf_target (ibfd->xvec))
7715 continue;
7716
7717 if (ppc64_tlsld_got (ibfd)->refcount > 0)
7718 {
7719 s = ppc64_elf_tdata (ibfd)->got;
7720 ppc64_tlsld_got (ibfd)->offset = s->size;
7721 s->size += 16;
7722 if (info->shared)
7723 {
7724 srel = ppc64_elf_tdata (ibfd)->relgot;
7725 srel->size += sizeof (Elf64_External_Rela);
7726 }
7727 }
7728 else
7729 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7730
7731 for (s = ibfd->sections; s != NULL; s = s->next)
7732 {
7733 struct ppc_dyn_relocs *p;
7734
7735 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
7736 {
7737 if (!bfd_is_abs_section (p->sec)
7738 && bfd_is_abs_section (p->sec->output_section))
7739 {
7740 /* Input section has been discarded, either because
7741 it is a copy of a linkonce section or due to
7742 linker script /DISCARD/, so we'll be discarding
7743 the relocs too. */
7744 }
7745 else if (p->count != 0)
7746 {
7747 srel = elf_section_data (p->sec)->sreloc;
7748 srel->size += p->count * sizeof (Elf64_External_Rela);
7749 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7750 info->flags |= DF_TEXTREL;
7751 }
7752 }
7753 }
7754
7755 lgot_ents = elf_local_got_ents (ibfd);
7756 if (!lgot_ents)
7757 continue;
7758
7759 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7760 locsymcount = symtab_hdr->sh_info;
7761 end_lgot_ents = lgot_ents + locsymcount;
7762 lgot_masks = (char *) end_lgot_ents;
7763 s = ppc64_elf_tdata (ibfd)->got;
7764 srel = ppc64_elf_tdata (ibfd)->relgot;
7765 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
7766 {
7767 struct got_entry *ent;
7768
7769 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7770 if (ent->got.refcount > 0)
7771 {
7772 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
7773 {
7774 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
7775 {
7776 ppc64_tlsld_got (ibfd)->offset = s->size;
7777 s->size += 16;
7778 if (info->shared)
7779 srel->size += sizeof (Elf64_External_Rela);
7780 }
7781 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
7782 }
7783 else
7784 {
7785 ent->got.offset = s->size;
7786 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
7787 {
7788 s->size += 16;
7789 if (info->shared)
7790 srel->size += 2 * sizeof (Elf64_External_Rela);
7791 }
7792 else
7793 {
7794 s->size += 8;
7795 if (info->shared)
7796 srel->size += sizeof (Elf64_External_Rela);
7797 }
7798 }
7799 }
7800 else
7801 ent->got.offset = (bfd_vma) -1;
7802 }
7803 }
7804
7805 /* Allocate global sym .plt and .got entries, and space for global
7806 sym dynamic relocs. */
7807 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
7808
7809 /* We now have determined the sizes of the various dynamic sections.
7810 Allocate memory for them. */
7811 relocs = FALSE;
7812 for (s = dynobj->sections; s != NULL; s = s->next)
7813 {
7814 if ((s->flags & SEC_LINKER_CREATED) == 0)
7815 continue;
7816
7817 if (s == htab->brlt || s == htab->relbrlt)
7818 /* These haven't been allocated yet; don't strip. */
7819 continue;
7820 else if (s == htab->got
7821 || s == htab->plt
7822 || s == htab->glink
7823 || s == htab->dynbss)
7824 {
7825 /* Strip this section if we don't need it; see the
7826 comment below. */
7827 }
7828 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
7829 {
7830 if (s->size != 0)
7831 {
7832 if (s != htab->relplt)
7833 relocs = TRUE;
7834
7835 /* We use the reloc_count field as a counter if we need
7836 to copy relocs into the output file. */
7837 s->reloc_count = 0;
7838 }
7839 }
7840 else
7841 {
7842 /* It's not one of our sections, so don't allocate space. */
7843 continue;
7844 }
7845
7846 if (s->size == 0)
7847 {
7848 /* If we don't need this section, strip it from the
7849 output file. This is mostly to handle .rela.bss and
7850 .rela.plt. We must create both sections in
7851 create_dynamic_sections, because they must be created
7852 before the linker maps input sections to output
7853 sections. The linker does that before
7854 adjust_dynamic_symbol is called, and it is that
7855 function which decides whether anything needs to go
7856 into these sections. */
7857 s->flags |= SEC_EXCLUDE;
7858 continue;
7859 }
7860
7861 if ((s->flags & SEC_HAS_CONTENTS) == 0)
7862 continue;
7863
7864 /* Allocate memory for the section contents. We use bfd_zalloc
7865 here in case unused entries are not reclaimed before the
7866 section's contents are written out. This should not happen,
7867 but this way if it does we get a R_PPC64_NONE reloc in .rela
7868 sections instead of garbage.
7869 We also rely on the section contents being zero when writing
7870 the GOT. */
7871 s->contents = bfd_zalloc (dynobj, s->size);
7872 if (s->contents == NULL)
7873 return FALSE;
7874 }
7875
7876 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7877 {
7878 if (!is_ppc64_elf_target (ibfd->xvec))
7879 continue;
7880
7881 s = ppc64_elf_tdata (ibfd)->got;
7882 if (s != NULL && s != htab->got)
7883 {
7884 if (s->size == 0)
7885 s->flags |= SEC_EXCLUDE;
7886 else
7887 {
7888 s->contents = bfd_zalloc (ibfd, s->size);
7889 if (s->contents == NULL)
7890 return FALSE;
7891 }
7892 }
7893 s = ppc64_elf_tdata (ibfd)->relgot;
7894 if (s != NULL)
7895 {
7896 if (s->size == 0)
7897 s->flags |= SEC_EXCLUDE;
7898 else
7899 {
7900 s->contents = bfd_zalloc (ibfd, s->size);
7901 if (s->contents == NULL)
7902 return FALSE;
7903 relocs = TRUE;
7904 s->reloc_count = 0;
7905 }
7906 }
7907 }
7908
7909 if (htab->elf.dynamic_sections_created)
7910 {
7911 /* Add some entries to the .dynamic section. We fill in the
7912 values later, in ppc64_elf_finish_dynamic_sections, but we
7913 must add the entries now so that we get the correct size for
7914 the .dynamic section. The DT_DEBUG entry is filled in by the
7915 dynamic linker and used by the debugger. */
7916 #define add_dynamic_entry(TAG, VAL) \
7917 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
7918
7919 if (info->executable)
7920 {
7921 if (!add_dynamic_entry (DT_DEBUG, 0))
7922 return FALSE;
7923 }
7924
7925 if (htab->plt != NULL && htab->plt->size != 0)
7926 {
7927 if (!add_dynamic_entry (DT_PLTGOT, 0)
7928 || !add_dynamic_entry (DT_PLTRELSZ, 0)
7929 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
7930 || !add_dynamic_entry (DT_JMPREL, 0)
7931 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
7932 return FALSE;
7933 }
7934
7935 if (NO_OPD_RELOCS)
7936 {
7937 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
7938 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
7939 return FALSE;
7940 }
7941
7942 if (relocs)
7943 {
7944 if (!add_dynamic_entry (DT_RELA, 0)
7945 || !add_dynamic_entry (DT_RELASZ, 0)
7946 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
7947 return FALSE;
7948
7949 /* If any dynamic relocs apply to a read-only section,
7950 then we need a DT_TEXTREL entry. */
7951 if ((info->flags & DF_TEXTREL) == 0)
7952 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
7953
7954 if ((info->flags & DF_TEXTREL) != 0)
7955 {
7956 if (!add_dynamic_entry (DT_TEXTREL, 0))
7957 return FALSE;
7958 }
7959 }
7960 }
7961 #undef add_dynamic_entry
7962
7963 return TRUE;
7964 }
7965
7966 /* Determine the type of stub needed, if any, for a call. */
7967
7968 static inline enum ppc_stub_type
7969 ppc_type_of_stub (asection *input_sec,
7970 const Elf_Internal_Rela *rel,
7971 struct ppc_link_hash_entry **hash,
7972 bfd_vma destination)
7973 {
7974 struct ppc_link_hash_entry *h = *hash;
7975 bfd_vma location;
7976 bfd_vma branch_offset;
7977 bfd_vma max_branch_offset;
7978 enum elf_ppc64_reloc_type r_type;
7979
7980 if (h != NULL)
7981 {
7982 struct ppc_link_hash_entry *fdh = h;
7983 if (fdh->oh != NULL
7984 && fdh->oh->is_func_descriptor)
7985 fdh = fdh->oh;
7986
7987 if (fdh->elf.dynindx != -1)
7988 {
7989 struct plt_entry *ent;
7990
7991 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
7992 if (ent->addend == rel->r_addend
7993 && ent->plt.offset != (bfd_vma) -1)
7994 {
7995 *hash = fdh;
7996 return ppc_stub_plt_call;
7997 }
7998 }
7999
8000 /* Here, we know we don't have a plt entry. If we don't have a
8001 either a defined function descriptor or a defined entry symbol
8002 in a regular object file, then it is pointless trying to make
8003 any other type of stub. */
8004 if (!((fdh->elf.root.type == bfd_link_hash_defined
8005 || fdh->elf.root.type == bfd_link_hash_defweak)
8006 && fdh->elf.root.u.def.section->output_section != NULL)
8007 && !((h->elf.root.type == bfd_link_hash_defined
8008 || h->elf.root.type == bfd_link_hash_defweak)
8009 && h->elf.root.u.def.section->output_section != NULL))
8010 return ppc_stub_none;
8011 }
8012
8013 /* Determine where the call point is. */
8014 location = (input_sec->output_offset
8015 + input_sec->output_section->vma
8016 + rel->r_offset);
8017
8018 branch_offset = destination - location;
8019 r_type = ELF64_R_TYPE (rel->r_info);
8020
8021 /* Determine if a long branch stub is needed. */
8022 max_branch_offset = 1 << 25;
8023 if (r_type != R_PPC64_REL24)
8024 max_branch_offset = 1 << 15;
8025
8026 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8027 /* We need a stub. Figure out whether a long_branch or plt_branch
8028 is needed later. */
8029 return ppc_stub_long_branch;
8030
8031 return ppc_stub_none;
8032 }
8033
8034 /* Build a .plt call stub. */
8035
8036 static inline bfd_byte *
8037 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
8038 {
8039 #define PPC_LO(v) ((v) & 0xffff)
8040 #define PPC_HI(v) (((v) >> 16) & 0xffff)
8041 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
8042
8043 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
8044 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
8045 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8046 if (PPC_HA (offset + 8) != PPC_HA (offset))
8047 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8048 offset += 8;
8049 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
8050 if (PPC_HA (offset + 8) != PPC_HA (offset))
8051 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
8052 offset += 8;
8053 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
8054 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8055 bfd_put_32 (obfd, BCTR, p), p += 4;
8056 return p;
8057 }
8058
8059 static bfd_boolean
8060 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8061 {
8062 struct ppc_stub_hash_entry *stub_entry;
8063 struct ppc_branch_hash_entry *br_entry;
8064 struct bfd_link_info *info;
8065 struct ppc_link_hash_table *htab;
8066 bfd_byte *loc;
8067 bfd_byte *p;
8068 unsigned int indx;
8069 struct plt_entry *ent;
8070 bfd_vma dest, off;
8071 int size;
8072
8073 /* Massage our args to the form they really have. */
8074 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8075 info = in_arg;
8076
8077 htab = ppc_hash_table (info);
8078
8079 /* Make a note of the offset within the stubs for this entry. */
8080 stub_entry->stub_offset = stub_entry->stub_sec->size;
8081 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
8082
8083 htab->stub_count[stub_entry->stub_type - 1] += 1;
8084 switch (stub_entry->stub_type)
8085 {
8086 case ppc_stub_long_branch:
8087 case ppc_stub_long_branch_r2off:
8088 /* Branches are relative. This is where we are going to. */
8089 off = dest = (stub_entry->target_value
8090 + stub_entry->target_section->output_offset
8091 + stub_entry->target_section->output_section->vma);
8092
8093 /* And this is where we are coming from. */
8094 off -= (stub_entry->stub_offset
8095 + stub_entry->stub_sec->output_offset
8096 + stub_entry->stub_sec->output_section->vma);
8097
8098 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8099 size = 4;
8100 else
8101 {
8102 bfd_vma r2off;
8103
8104 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8105 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8106 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8107 loc += 4;
8108 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8109 loc += 4;
8110 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8111 loc += 4;
8112 off -= 12;
8113 size = 16;
8114 }
8115 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
8116
8117 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8118 {
8119 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8120 stub_entry->root.string);
8121 htab->stub_error = TRUE;
8122 return FALSE;
8123 }
8124
8125 if (info->emitrelocations)
8126 {
8127 Elf_Internal_Rela *relocs, *r;
8128 struct bfd_elf_section_data *elfsec_data;
8129
8130 elfsec_data = elf_section_data (stub_entry->stub_sec);
8131 relocs = elfsec_data->relocs;
8132 if (relocs == NULL)
8133 {
8134 bfd_size_type relsize;
8135 relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8136 relocs = bfd_alloc (htab->stub_bfd, relsize);
8137 if (relocs == NULL)
8138 return FALSE;
8139 elfsec_data->relocs = relocs;
8140 elfsec_data->rel_hdr.sh_size = relsize;
8141 elfsec_data->rel_hdr.sh_entsize = 24;
8142 stub_entry->stub_sec->reloc_count = 0;
8143 }
8144 r = relocs + stub_entry->stub_sec->reloc_count;
8145 stub_entry->stub_sec->reloc_count += 1;
8146 r->r_offset = loc - stub_entry->stub_sec->contents;
8147 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8148 r->r_addend = dest;
8149 if (stub_entry->h != NULL)
8150 {
8151 struct elf_link_hash_entry **hashes;
8152 unsigned long symndx;
8153 struct ppc_link_hash_entry *h;
8154
8155 hashes = elf_sym_hashes (htab->stub_bfd);
8156 if (hashes == NULL)
8157 {
8158 bfd_size_type hsize;
8159
8160 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8161 hashes = bfd_zalloc (htab->stub_bfd, hsize);
8162 if (hashes == NULL)
8163 return FALSE;
8164 elf_sym_hashes (htab->stub_bfd) = hashes;
8165 htab->stub_globals = 1;
8166 }
8167 symndx = htab->stub_globals++;
8168 h = stub_entry->h;
8169 hashes[symndx] = &h->elf;
8170 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8171 if (h->oh != NULL && h->oh->is_func)
8172 h = h->oh;
8173 if (h->elf.root.u.def.section != stub_entry->target_section)
8174 /* H is an opd symbol. The addend must be zero. */
8175 r->r_addend = 0;
8176 else
8177 {
8178 off = (h->elf.root.u.def.value
8179 + h->elf.root.u.def.section->output_offset
8180 + h->elf.root.u.def.section->output_section->vma);
8181 r->r_addend -= off;
8182 }
8183 }
8184 }
8185 break;
8186
8187 case ppc_stub_plt_branch:
8188 case ppc_stub_plt_branch_r2off:
8189 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8190 stub_entry->root.string + 9,
8191 FALSE, FALSE);
8192 if (br_entry == NULL)
8193 {
8194 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
8195 stub_entry->root.string);
8196 htab->stub_error = TRUE;
8197 return FALSE;
8198 }
8199
8200 off = (stub_entry->target_value
8201 + stub_entry->target_section->output_offset
8202 + stub_entry->target_section->output_section->vma);
8203
8204 bfd_put_64 (htab->brlt->owner, off,
8205 htab->brlt->contents + br_entry->offset);
8206
8207 if (htab->relbrlt != NULL)
8208 {
8209 /* Create a reloc for the branch lookup table entry. */
8210 Elf_Internal_Rela rela;
8211 bfd_byte *rl;
8212
8213 rela.r_offset = (br_entry->offset
8214 + htab->brlt->output_offset
8215 + htab->brlt->output_section->vma);
8216 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8217 rela.r_addend = off;
8218
8219 rl = htab->relbrlt->contents;
8220 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8221 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
8222 }
8223
8224 off = (br_entry->offset
8225 + htab->brlt->output_offset
8226 + htab->brlt->output_section->vma
8227 - elf_gp (htab->brlt->output_section->owner)
8228 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8229
8230 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8231 {
8232 (*_bfd_error_handler)
8233 (_("linkage table error against `%s'"),
8234 stub_entry->root.string);
8235 bfd_set_error (bfd_error_bad_value);
8236 htab->stub_error = TRUE;
8237 return FALSE;
8238 }
8239
8240 indx = off;
8241 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8242 {
8243 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8244 loc += 4;
8245 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8246 size = 16;
8247 }
8248 else
8249 {
8250 bfd_vma r2off;
8251
8252 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8253 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8254 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
8255 loc += 4;
8256 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
8257 loc += 4;
8258 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
8259 loc += 4;
8260 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
8261 loc += 4;
8262 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
8263 size = 28;
8264 }
8265 loc += 4;
8266 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
8267 loc += 4;
8268 bfd_put_32 (htab->stub_bfd, BCTR, loc);
8269 break;
8270
8271 case ppc_stub_plt_call:
8272 /* Do the best we can for shared libraries built without
8273 exporting ".foo" for each "foo". This can happen when symbol
8274 versioning scripts strip all bar a subset of symbols. */
8275 if (stub_entry->h->oh != NULL
8276 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
8277 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
8278 {
8279 /* Point the symbol at the stub. There may be multiple stubs,
8280 we don't really care; The main thing is to make this sym
8281 defined somewhere. Maybe defining the symbol in the stub
8282 section is a silly idea. If we didn't do this, htab->top_id
8283 could disappear. */
8284 stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8285 stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8286 stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
8287 }
8288
8289 /* Now build the stub. */
8290 off = (bfd_vma) -1;
8291 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8292 if (ent->addend == stub_entry->addend)
8293 {
8294 off = ent->plt.offset;
8295 break;
8296 }
8297 if (off >= (bfd_vma) -2)
8298 abort ();
8299
8300 off &= ~ (bfd_vma) 1;
8301 off += (htab->plt->output_offset
8302 + htab->plt->output_section->vma
8303 - elf_gp (htab->plt->output_section->owner)
8304 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8305
8306 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
8307 {
8308 (*_bfd_error_handler)
8309 (_("linkage table error against `%s'"),
8310 stub_entry->h->elf.root.root.string);
8311 bfd_set_error (bfd_error_bad_value);
8312 htab->stub_error = TRUE;
8313 return FALSE;
8314 }
8315
8316 p = build_plt_stub (htab->stub_bfd, loc, off);
8317 size = p - loc;
8318 break;
8319
8320 default:
8321 BFD_FAIL ();
8322 return FALSE;
8323 }
8324
8325 stub_entry->stub_sec->size += size;
8326
8327 if (htab->emit_stub_syms)
8328 {
8329 struct elf_link_hash_entry *h;
8330 size_t len1, len2;
8331 char *name;
8332 const char *const stub_str[] = { "long_branch",
8333 "long_branch_r2off",
8334 "plt_branch",
8335 "plt_branch_r2off",
8336 "plt_call" };
8337
8338 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8339 len2 = strlen (stub_entry->root.string);
8340 name = bfd_malloc (len1 + len2 + 2);
8341 if (name == NULL)
8342 return FALSE;
8343 memcpy (name, stub_entry->root.string, 9);
8344 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8345 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8346 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
8347 if (h == NULL)
8348 return FALSE;
8349 if (h->root.type == bfd_link_hash_new)
8350 {
8351 h->root.type = bfd_link_hash_defined;
8352 h->root.u.def.section = stub_entry->stub_sec;
8353 h->root.u.def.value = stub_entry->stub_offset;
8354 h->ref_regular = 1;
8355 h->def_regular = 1;
8356 h->ref_regular_nonweak = 1;
8357 h->forced_local = 1;
8358 h->non_elf = 0;
8359 }
8360 }
8361
8362 return TRUE;
8363 }
8364
8365 /* As above, but don't actually build the stub. Just bump offset so
8366 we know stub section sizes, and select plt_branch stubs where
8367 long_branch stubs won't do. */
8368
8369 static bfd_boolean
8370 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
8371 {
8372 struct ppc_stub_hash_entry *stub_entry;
8373 struct bfd_link_info *info;
8374 struct ppc_link_hash_table *htab;
8375 bfd_vma off;
8376 int size;
8377
8378 /* Massage our args to the form they really have. */
8379 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
8380 info = in_arg;
8381
8382 htab = ppc_hash_table (info);
8383
8384 if (stub_entry->stub_type == ppc_stub_plt_call)
8385 {
8386 struct plt_entry *ent;
8387 off = (bfd_vma) -1;
8388 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8389 if (ent->addend == stub_entry->addend)
8390 {
8391 off = ent->plt.offset & ~(bfd_vma) 1;
8392 break;
8393 }
8394 if (off >= (bfd_vma) -2)
8395 abort ();
8396 off += (htab->plt->output_offset
8397 + htab->plt->output_section->vma
8398 - elf_gp (htab->plt->output_section->owner)
8399 - htab->stub_group[stub_entry->id_sec->id].toc_off);
8400
8401 size = PLT_CALL_STUB_SIZE;
8402 if (PPC_HA (off + 16) != PPC_HA (off))
8403 size += 4;
8404 }
8405 else
8406 {
8407 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8408 variants. */
8409 off = (stub_entry->target_value
8410 + stub_entry->target_section->output_offset
8411 + stub_entry->target_section->output_section->vma);
8412 off -= (stub_entry->stub_sec->size
8413 + stub_entry->stub_sec->output_offset
8414 + stub_entry->stub_sec->output_section->vma);
8415
8416 /* Reset the stub type from the plt variant in case we now
8417 can reach with a shorter stub. */
8418 if (stub_entry->stub_type >= ppc_stub_plt_branch)
8419 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8420
8421 size = 4;
8422 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8423 {
8424 off -= 12;
8425 size = 16;
8426 }
8427
8428 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
8429 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8430 {
8431 struct ppc_branch_hash_entry *br_entry;
8432
8433 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8434 stub_entry->root.string + 9,
8435 TRUE, FALSE);
8436 if (br_entry == NULL)
8437 {
8438 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
8439 stub_entry->root.string);
8440 htab->stub_error = TRUE;
8441 return FALSE;
8442 }
8443
8444 if (br_entry->iter != htab->stub_iteration)
8445 {
8446 br_entry->iter = htab->stub_iteration;
8447 br_entry->offset = htab->brlt->size;
8448 htab->brlt->size += 8;
8449
8450 if (htab->relbrlt != NULL)
8451 htab->relbrlt->size += sizeof (Elf64_External_Rela);
8452 }
8453
8454 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
8455 size = 16;
8456 if (stub_entry->stub_type != ppc_stub_plt_branch)
8457 size = 28;
8458 }
8459
8460 if (info->emitrelocations
8461 && (stub_entry->stub_type == ppc_stub_long_branch
8462 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
8463 stub_entry->stub_sec->reloc_count += 1;
8464 }
8465
8466 stub_entry->stub_sec->size += size;
8467 return TRUE;
8468 }
8469
8470 /* Set up various things so that we can make a list of input sections
8471 for each output section included in the link. Returns -1 on error,
8472 0 when no stubs will be needed, and 1 on success. */
8473
8474 int
8475 ppc64_elf_setup_section_lists (bfd *output_bfd,
8476 struct bfd_link_info *info,
8477 int no_multi_toc)
8478 {
8479 bfd *input_bfd;
8480 int top_id, top_index, id;
8481 asection *section;
8482 asection **input_list;
8483 bfd_size_type amt;
8484 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8485
8486 htab->no_multi_toc = no_multi_toc;
8487
8488 if (htab->brlt == NULL)
8489 return 0;
8490
8491 /* Find the top input section id. */
8492 for (input_bfd = info->input_bfds, top_id = 3;
8493 input_bfd != NULL;
8494 input_bfd = input_bfd->link_next)
8495 {
8496 for (section = input_bfd->sections;
8497 section != NULL;
8498 section = section->next)
8499 {
8500 if (top_id < section->id)
8501 top_id = section->id;
8502 }
8503 }
8504
8505 htab->top_id = top_id;
8506 amt = sizeof (struct map_stub) * (top_id + 1);
8507 htab->stub_group = bfd_zmalloc (amt);
8508 if (htab->stub_group == NULL)
8509 return -1;
8510
8511 /* Set toc_off for com, und, abs and ind sections. */
8512 for (id = 0; id < 3; id++)
8513 htab->stub_group[id].toc_off = TOC_BASE_OFF;
8514
8515 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
8516
8517 /* We can't use output_bfd->section_count here to find the top output
8518 section index as some sections may have been removed, and
8519 strip_excluded_output_sections doesn't renumber the indices. */
8520 for (section = output_bfd->sections, top_index = 0;
8521 section != NULL;
8522 section = section->next)
8523 {
8524 if (top_index < section->index)
8525 top_index = section->index;
8526 }
8527
8528 htab->top_index = top_index;
8529 amt = sizeof (asection *) * (top_index + 1);
8530 input_list = bfd_zmalloc (amt);
8531 htab->input_list = input_list;
8532 if (input_list == NULL)
8533 return -1;
8534
8535 return 1;
8536 }
8537
8538 /* The linker repeatedly calls this function for each TOC input section
8539 and linker generated GOT section. Group input bfds such that the toc
8540 within a group is less than 64k in size. Will break with cute linker
8541 scripts that play games with dot in the output toc section. */
8542
8543 void
8544 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
8545 {
8546 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8547
8548 if (!htab->no_multi_toc)
8549 {
8550 bfd_vma addr = isec->output_offset + isec->output_section->vma;
8551 bfd_vma off = addr - htab->toc_curr;
8552
8553 if (off + isec->size > 0x10000)
8554 htab->toc_curr = addr;
8555
8556 elf_gp (isec->owner) = (htab->toc_curr
8557 - elf_gp (isec->output_section->owner)
8558 + TOC_BASE_OFF);
8559 }
8560 }
8561
8562 /* Called after the last call to the above function. */
8563
8564 void
8565 ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
8566 {
8567 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8568
8569 htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8570
8571 /* toc_curr tracks the TOC offset used for code sections below in
8572 ppc64_elf_next_input_section. Start off at 0x8000. */
8573 htab->toc_curr = TOC_BASE_OFF;
8574 }
8575
8576 /* No toc references were found in ISEC. If the code in ISEC makes no
8577 calls, then there's no need to use toc adjusting stubs when branching
8578 into ISEC. Actually, indirect calls from ISEC are OK as they will
8579 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
8580 needed, and 2 if a cyclical call-graph was found but no other reason
8581 for a stub was detected. If called from the top level, a return of
8582 2 means the same as a return of 0. */
8583
8584 static int
8585 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
8586 {
8587 Elf_Internal_Rela *relstart, *rel;
8588 Elf_Internal_Sym *local_syms;
8589 int ret;
8590 struct ppc_link_hash_table *htab;
8591
8592 /* We know none of our code bearing sections will need toc stubs. */
8593 if ((isec->flags & SEC_LINKER_CREATED) != 0)
8594 return 0;
8595
8596 if (isec->size == 0)
8597 return 0;
8598
8599 if (isec->output_section == NULL)
8600 return 0;
8601
8602 /* Hack for linux kernel. .fixup contains branches, but only back to
8603 the function that hit an exception. */
8604 if (strcmp (isec->name, ".fixup") == 0)
8605 return 0;
8606
8607 if (isec->reloc_count == 0)
8608 return 0;
8609
8610 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8611 info->keep_memory);
8612 if (relstart == NULL)
8613 return -1;
8614
8615 /* Look for branches to outside of this section. */
8616 local_syms = NULL;
8617 ret = 0;
8618 htab = ppc_hash_table (info);
8619 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
8620 {
8621 enum elf_ppc64_reloc_type r_type;
8622 unsigned long r_symndx;
8623 struct elf_link_hash_entry *h;
8624 Elf_Internal_Sym *sym;
8625 asection *sym_sec;
8626 long *opd_adjust;
8627 bfd_vma sym_value;
8628 bfd_vma dest;
8629
8630 r_type = ELF64_R_TYPE (rel->r_info);
8631 if (r_type != R_PPC64_REL24
8632 && r_type != R_PPC64_REL14
8633 && r_type != R_PPC64_REL14_BRTAKEN
8634 && r_type != R_PPC64_REL14_BRNTAKEN)
8635 continue;
8636
8637 r_symndx = ELF64_R_SYM (rel->r_info);
8638 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8639 isec->owner))
8640 {
8641 ret = -1;
8642 break;
8643 }
8644
8645 /* Calls to dynamic lib functions go through a plt call stub
8646 that uses r2. Branches to undefined symbols might be a call
8647 using old-style dot symbols that can be satisfied by a plt
8648 call into a new-style dynamic library. */
8649 if (sym_sec == NULL)
8650 {
8651 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8652 if (eh != NULL
8653 && eh->oh != NULL
8654 && eh->oh->elf.plt.plist != NULL)
8655 {
8656 ret = 1;
8657 break;
8658 }
8659
8660 /* Ignore other undefined symbols. */
8661 continue;
8662 }
8663
8664 /* Assume branches to other sections not included in the link need
8665 stubs too, to cover -R and absolute syms. */
8666 if (sym_sec->output_section == NULL)
8667 {
8668 ret = 1;
8669 break;
8670 }
8671
8672 if (h == NULL)
8673 sym_value = sym->st_value;
8674 else
8675 {
8676 if (h->root.type != bfd_link_hash_defined
8677 && h->root.type != bfd_link_hash_defweak)
8678 abort ();
8679 sym_value = h->root.u.def.value;
8680 }
8681 sym_value += rel->r_addend;
8682
8683 /* If this branch reloc uses an opd sym, find the code section. */
8684 opd_adjust = get_opd_info (sym_sec);
8685 if (opd_adjust != NULL)
8686 {
8687 if (h == NULL)
8688 {
8689 long adjust;
8690
8691 adjust = opd_adjust[sym->st_value / 8];
8692 if (adjust == -1)
8693 /* Assume deleted functions won't ever be called. */
8694 continue;
8695 sym_value += adjust;
8696 }
8697
8698 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8699 if (dest == (bfd_vma) -1)
8700 continue;
8701 }
8702 else
8703 dest = (sym_value
8704 + sym_sec->output_offset
8705 + sym_sec->output_section->vma);
8706
8707 /* Ignore branch to self. */
8708 if (sym_sec == isec)
8709 continue;
8710
8711 /* If the called function uses the toc, we need a stub. */
8712 if (sym_sec->has_toc_reloc
8713 || sym_sec->makes_toc_func_call)
8714 {
8715 ret = 1;
8716 break;
8717 }
8718
8719 /* Assume any branch that needs a long branch stub might in fact
8720 need a plt_branch stub. A plt_branch stub uses r2. */
8721 else if (dest - (isec->output_offset
8722 + isec->output_section->vma
8723 + rel->r_offset) + (1 << 25) >= (2 << 25))
8724 {
8725 ret = 1;
8726 break;
8727 }
8728
8729 /* If calling back to a section in the process of being tested, we
8730 can't say for sure that no toc adjusting stubs are needed, so
8731 don't return zero. */
8732 else if (sym_sec->call_check_in_progress)
8733 ret = 2;
8734
8735 /* Branches to another section that itself doesn't have any TOC
8736 references are OK. Recursively call ourselves to check. */
8737 else if (sym_sec->id <= htab->top_id
8738 && htab->stub_group[sym_sec->id].toc_off == 0)
8739 {
8740 int recur;
8741
8742 /* Mark current section as indeterminate, so that other
8743 sections that call back to current won't be marked as
8744 known. */
8745 isec->call_check_in_progress = 1;
8746 recur = toc_adjusting_stub_needed (info, sym_sec);
8747 isec->call_check_in_progress = 0;
8748
8749 if (recur < 0)
8750 {
8751 /* An error. Exit. */
8752 ret = -1;
8753 break;
8754 }
8755 else if (recur <= 1)
8756 {
8757 /* Known result. Mark as checked and set section flag. */
8758 htab->stub_group[sym_sec->id].toc_off = 1;
8759 if (recur != 0)
8760 {
8761 sym_sec->makes_toc_func_call = 1;
8762 ret = 1;
8763 break;
8764 }
8765 }
8766 else
8767 {
8768 /* Unknown result. Continue checking. */
8769 ret = 2;
8770 }
8771 }
8772 }
8773
8774 if (local_syms != NULL
8775 && (elf_tdata (isec->owner)->symtab_hdr.contents
8776 != (unsigned char *) local_syms))
8777 free (local_syms);
8778 if (elf_section_data (isec)->relocs != relstart)
8779 free (relstart);
8780
8781 return ret;
8782 }
8783
8784 /* The linker repeatedly calls this function for each input section,
8785 in the order that input sections are linked into output sections.
8786 Build lists of input sections to determine groupings between which
8787 we may insert linker stubs. */
8788
8789 bfd_boolean
8790 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
8791 {
8792 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8793
8794 if ((isec->output_section->flags & SEC_CODE) != 0
8795 && isec->output_section->index <= htab->top_index)
8796 {
8797 asection **list = htab->input_list + isec->output_section->index;
8798 /* Steal the link_sec pointer for our list. */
8799 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
8800 /* This happens to make the list in reverse order,
8801 which is what we want. */
8802 PREV_SEC (isec) = *list;
8803 *list = isec;
8804 }
8805
8806 if (htab->multi_toc_needed)
8807 {
8808 /* If a code section has a function that uses the TOC then we need
8809 to use the right TOC (obviously). Also, make sure that .opd gets
8810 the correct TOC value for R_PPC64_TOC relocs that don't have or
8811 can't find their function symbol (shouldn't ever happen now). */
8812 if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8813 {
8814 if (elf_gp (isec->owner) != 0)
8815 htab->toc_curr = elf_gp (isec->owner);
8816 }
8817 else if (htab->stub_group[isec->id].toc_off == 0)
8818 {
8819 int ret = toc_adjusting_stub_needed (info, isec);
8820 if (ret < 0)
8821 return FALSE;
8822 else
8823 isec->makes_toc_func_call = ret & 1;
8824 }
8825 }
8826
8827 /* Functions that don't use the TOC can belong in any TOC group.
8828 Use the last TOC base. This happens to make _init and _fini
8829 pasting work. */
8830 htab->stub_group[isec->id].toc_off = htab->toc_curr;
8831 return TRUE;
8832 }
8833
8834 /* See whether we can group stub sections together. Grouping stub
8835 sections may result in fewer stubs. More importantly, we need to
8836 put all .init* and .fini* stubs at the beginning of the .init or
8837 .fini output sections respectively, because glibc splits the
8838 _init and _fini functions into multiple parts. Putting a stub in
8839 the middle of a function is not a good idea. */
8840
8841 static void
8842 group_sections (struct ppc_link_hash_table *htab,
8843 bfd_size_type stub_group_size,
8844 bfd_boolean stubs_always_before_branch)
8845 {
8846 asection **list = htab->input_list + htab->top_index;
8847 do
8848 {
8849 asection *tail = *list;
8850 while (tail != NULL)
8851 {
8852 asection *curr;
8853 asection *prev;
8854 bfd_size_type total;
8855 bfd_boolean big_sec;
8856 bfd_vma curr_toc;
8857
8858 curr = tail;
8859 total = tail->size;
8860 big_sec = total > stub_group_size;
8861 if (big_sec)
8862 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
8863 tail->owner, tail);
8864 curr_toc = htab->stub_group[tail->id].toc_off;
8865
8866 while ((prev = PREV_SEC (curr)) != NULL
8867 && ((total += curr->output_offset - prev->output_offset)
8868 < stub_group_size)
8869 && htab->stub_group[prev->id].toc_off == curr_toc)
8870 curr = prev;
8871
8872 /* OK, the size from the start of CURR to the end is less
8873 than stub_group_size and thus can be handled by one stub
8874 section. (or the tail section is itself larger than
8875 stub_group_size, in which case we may be toast.) We
8876 should really be keeping track of the total size of stubs
8877 added here, as stubs contribute to the final output
8878 section size. That's a little tricky, and this way will
8879 only break if stubs added make the total size more than
8880 2^25, ie. for the default stub_group_size, if stubs total
8881 more than 2097152 bytes, or nearly 75000 plt call stubs. */
8882 do
8883 {
8884 prev = PREV_SEC (tail);
8885 /* Set up this stub group. */
8886 htab->stub_group[tail->id].link_sec = curr;
8887 }
8888 while (tail != curr && (tail = prev) != NULL);
8889
8890 /* But wait, there's more! Input sections up to stub_group_size
8891 bytes before the stub section can be handled by it too.
8892 Don't do this if we have a really large section after the
8893 stubs, as adding more stubs increases the chance that
8894 branches may not reach into the stub section. */
8895 if (!stubs_always_before_branch && !big_sec)
8896 {
8897 total = 0;
8898 while (prev != NULL
8899 && ((total += tail->output_offset - prev->output_offset)
8900 < stub_group_size)
8901 && htab->stub_group[prev->id].toc_off == curr_toc)
8902 {
8903 tail = prev;
8904 prev = PREV_SEC (tail);
8905 htab->stub_group[tail->id].link_sec = curr;
8906 }
8907 }
8908 tail = prev;
8909 }
8910 }
8911 while (list-- != htab->input_list);
8912 free (htab->input_list);
8913 #undef PREV_SEC
8914 }
8915
8916 /* Determine and set the size of the stub section for a final link.
8917
8918 The basic idea here is to examine all the relocations looking for
8919 PC-relative calls to a target that is unreachable with a "bl"
8920 instruction. */
8921
8922 bfd_boolean
8923 ppc64_elf_size_stubs (bfd *output_bfd,
8924 struct bfd_link_info *info,
8925 bfd_signed_vma group_size,
8926 asection *(*add_stub_section) (const char *, asection *),
8927 void (*layout_sections_again) (void))
8928 {
8929 bfd_size_type stub_group_size;
8930 bfd_boolean stubs_always_before_branch;
8931 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8932
8933 /* Stash our params away. */
8934 htab->add_stub_section = add_stub_section;
8935 htab->layout_sections_again = layout_sections_again;
8936 stubs_always_before_branch = group_size < 0;
8937 if (group_size < 0)
8938 stub_group_size = -group_size;
8939 else
8940 stub_group_size = group_size;
8941 if (stub_group_size == 1)
8942 {
8943 /* Default values. */
8944 if (stubs_always_before_branch)
8945 {
8946 stub_group_size = 0x1e00000;
8947 if (htab->has_14bit_branch)
8948 stub_group_size = 0x7800;
8949 }
8950 else
8951 {
8952 stub_group_size = 0x1c00000;
8953 if (htab->has_14bit_branch)
8954 stub_group_size = 0x7000;
8955 }
8956 }
8957
8958 group_sections (htab, stub_group_size, stubs_always_before_branch);
8959
8960 while (1)
8961 {
8962 bfd *input_bfd;
8963 unsigned int bfd_indx;
8964 asection *stub_sec;
8965
8966 htab->stub_iteration += 1;
8967
8968 for (input_bfd = info->input_bfds, bfd_indx = 0;
8969 input_bfd != NULL;
8970 input_bfd = input_bfd->link_next, bfd_indx++)
8971 {
8972 Elf_Internal_Shdr *symtab_hdr;
8973 asection *section;
8974 Elf_Internal_Sym *local_syms = NULL;
8975
8976 if (!is_ppc64_elf_target (input_bfd->xvec))
8977 continue;
8978
8979 /* We'll need the symbol table in a second. */
8980 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
8981 if (symtab_hdr->sh_info == 0)
8982 continue;
8983
8984 /* Walk over each section attached to the input bfd. */
8985 for (section = input_bfd->sections;
8986 section != NULL;
8987 section = section->next)
8988 {
8989 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
8990
8991 /* If there aren't any relocs, then there's nothing more
8992 to do. */
8993 if ((section->flags & SEC_RELOC) == 0
8994 || section->reloc_count == 0)
8995 continue;
8996
8997 /* If this section is a link-once section that will be
8998 discarded, then don't create any stubs. */
8999 if (section->output_section == NULL
9000 || section->output_section->owner != output_bfd)
9001 continue;
9002
9003 /* Get the relocs. */
9004 internal_relocs
9005 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
9006 info->keep_memory);
9007 if (internal_relocs == NULL)
9008 goto error_ret_free_local;
9009
9010 /* Now examine each relocation. */
9011 irela = internal_relocs;
9012 irelaend = irela + section->reloc_count;
9013 for (; irela < irelaend; irela++)
9014 {
9015 enum elf_ppc64_reloc_type r_type;
9016 unsigned int r_indx;
9017 enum ppc_stub_type stub_type;
9018 struct ppc_stub_hash_entry *stub_entry;
9019 asection *sym_sec, *code_sec;
9020 bfd_vma sym_value;
9021 bfd_vma destination;
9022 bfd_boolean ok_dest;
9023 struct ppc_link_hash_entry *hash;
9024 struct ppc_link_hash_entry *fdh;
9025 struct elf_link_hash_entry *h;
9026 Elf_Internal_Sym *sym;
9027 char *stub_name;
9028 const asection *id_sec;
9029 long *opd_adjust;
9030
9031 r_type = ELF64_R_TYPE (irela->r_info);
9032 r_indx = ELF64_R_SYM (irela->r_info);
9033
9034 if (r_type >= R_PPC64_max)
9035 {
9036 bfd_set_error (bfd_error_bad_value);
9037 goto error_ret_free_internal;
9038 }
9039
9040 /* Only look for stubs on branch instructions. */
9041 if (r_type != R_PPC64_REL24
9042 && r_type != R_PPC64_REL14
9043 && r_type != R_PPC64_REL14_BRTAKEN
9044 && r_type != R_PPC64_REL14_BRNTAKEN)
9045 continue;
9046
9047 /* Now determine the call target, its name, value,
9048 section. */
9049 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9050 r_indx, input_bfd))
9051 goto error_ret_free_internal;
9052 hash = (struct ppc_link_hash_entry *) h;
9053
9054 ok_dest = FALSE;
9055 fdh = NULL;
9056 sym_value = 0;
9057 if (hash == NULL)
9058 {
9059 sym_value = sym->st_value;
9060 ok_dest = TRUE;
9061 }
9062 else if (hash->elf.root.type == bfd_link_hash_defined
9063 || hash->elf.root.type == bfd_link_hash_defweak)
9064 {
9065 sym_value = hash->elf.root.u.def.value;
9066 if (sym_sec->output_section != NULL)
9067 ok_dest = TRUE;
9068 }
9069 else if (hash->elf.root.type == bfd_link_hash_undefweak
9070 || hash->elf.root.type == bfd_link_hash_undefined)
9071 {
9072 /* Recognise an old ABI func code entry sym, and
9073 use the func descriptor sym instead if it is
9074 defined. */
9075 if (hash->elf.root.root.string[0] == '.'
9076 && (fdh = get_fdh (hash, htab)) != NULL)
9077 {
9078 if (fdh->elf.root.type == bfd_link_hash_defined
9079 || fdh->elf.root.type == bfd_link_hash_defweak)
9080 {
9081 sym_sec = fdh->elf.root.u.def.section;
9082 sym_value = fdh->elf.root.u.def.value;
9083 if (sym_sec->output_section != NULL)
9084 ok_dest = TRUE;
9085 }
9086 else
9087 fdh = NULL;
9088 }
9089 }
9090 else
9091 {
9092 bfd_set_error (bfd_error_bad_value);
9093 goto error_ret_free_internal;
9094 }
9095
9096 destination = 0;
9097 if (ok_dest)
9098 {
9099 sym_value += irela->r_addend;
9100 destination = (sym_value
9101 + sym_sec->output_offset
9102 + sym_sec->output_section->vma);
9103 }
9104
9105 code_sec = sym_sec;
9106 opd_adjust = get_opd_info (sym_sec);
9107 if (opd_adjust != NULL)
9108 {
9109 bfd_vma dest;
9110
9111 if (hash == NULL)
9112 {
9113 long adjust = opd_adjust[sym_value / 8];
9114 if (adjust == -1)
9115 continue;
9116 sym_value += adjust;
9117 }
9118 dest = opd_entry_value (sym_sec, sym_value,
9119 &code_sec, &sym_value);
9120 if (dest != (bfd_vma) -1)
9121 {
9122 destination = dest;
9123 if (fdh != NULL)
9124 {
9125 /* Fixup old ABI sym to point at code
9126 entry. */
9127 hash->elf.root.type = bfd_link_hash_defweak;
9128 hash->elf.root.u.def.section = code_sec;
9129 hash->elf.root.u.def.value = sym_value;
9130 }
9131 }
9132 }
9133
9134 /* Determine what (if any) linker stub is needed. */
9135 stub_type = ppc_type_of_stub (section, irela, &hash,
9136 destination);
9137
9138 if (stub_type != ppc_stub_plt_call)
9139 {
9140 /* Check whether we need a TOC adjusting stub.
9141 Since the linker pastes together pieces from
9142 different object files when creating the
9143 _init and _fini functions, it may be that a
9144 call to what looks like a local sym is in
9145 fact a call needing a TOC adjustment. */
9146 if (code_sec != NULL
9147 && code_sec->output_section != NULL
9148 && (htab->stub_group[code_sec->id].toc_off
9149 != htab->stub_group[section->id].toc_off)
9150 && (code_sec->has_toc_reloc
9151 || code_sec->makes_toc_func_call))
9152 stub_type = ppc_stub_long_branch_r2off;
9153 }
9154
9155 if (stub_type == ppc_stub_none)
9156 continue;
9157
9158 /* __tls_get_addr calls might be eliminated. */
9159 if (stub_type != ppc_stub_plt_call
9160 && hash != NULL
9161 && (hash == htab->tls_get_addr
9162 || hash == htab->tls_get_addr_fd)
9163 && section->has_tls_reloc
9164 && irela != internal_relocs)
9165 {
9166 /* Get tls info. */
9167 char *tls_mask;
9168
9169 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
9170 irela - 1, input_bfd))
9171 goto error_ret_free_internal;
9172 if (*tls_mask != 0)
9173 continue;
9174 }
9175
9176 /* Support for grouping stub sections. */
9177 id_sec = htab->stub_group[section->id].link_sec;
9178
9179 /* Get the name of this stub. */
9180 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9181 if (!stub_name)
9182 goto error_ret_free_internal;
9183
9184 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
9185 stub_name, FALSE, FALSE);
9186 if (stub_entry != NULL)
9187 {
9188 /* The proper stub has already been created. */
9189 free (stub_name);
9190 continue;
9191 }
9192
9193 stub_entry = ppc_add_stub (stub_name, section, htab);
9194 if (stub_entry == NULL)
9195 {
9196 free (stub_name);
9197 error_ret_free_internal:
9198 if (elf_section_data (section)->relocs == NULL)
9199 free (internal_relocs);
9200 error_ret_free_local:
9201 if (local_syms != NULL
9202 && (symtab_hdr->contents
9203 != (unsigned char *) local_syms))
9204 free (local_syms);
9205 return FALSE;
9206 }
9207
9208 stub_entry->stub_type = stub_type;
9209 stub_entry->target_value = sym_value;
9210 stub_entry->target_section = code_sec;
9211 stub_entry->h = hash;
9212 stub_entry->addend = irela->r_addend;
9213
9214 if (stub_entry->h != NULL)
9215 htab->stub_globals += 1;
9216 }
9217
9218 /* We're done with the internal relocs, free them. */
9219 if (elf_section_data (section)->relocs != internal_relocs)
9220 free (internal_relocs);
9221 }
9222
9223 if (local_syms != NULL
9224 && symtab_hdr->contents != (unsigned char *) local_syms)
9225 {
9226 if (!info->keep_memory)
9227 free (local_syms);
9228 else
9229 symtab_hdr->contents = (unsigned char *) local_syms;
9230 }
9231 }
9232
9233 /* We may have added some stubs. Find out the new size of the
9234 stub sections. */
9235 for (stub_sec = htab->stub_bfd->sections;
9236 stub_sec != NULL;
9237 stub_sec = stub_sec->next)
9238 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9239 {
9240 stub_sec->rawsize = stub_sec->size;
9241 stub_sec->size = 0;
9242 stub_sec->reloc_count = 0;
9243 }
9244
9245 htab->brlt->size = 0;
9246 if (htab->relbrlt != NULL)
9247 htab->relbrlt->size = 0;
9248
9249 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
9250
9251 for (stub_sec = htab->stub_bfd->sections;
9252 stub_sec != NULL;
9253 stub_sec = stub_sec->next)
9254 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9255 && stub_sec->rawsize != stub_sec->size)
9256 break;
9257
9258 /* Exit from this loop when no stubs have been added, and no stubs
9259 have changed size. */
9260 if (stub_sec == NULL)
9261 break;
9262
9263 /* Ask the linker to do its stuff. */
9264 (*htab->layout_sections_again) ();
9265 }
9266
9267 /* It would be nice to strip htab->brlt from the output if the
9268 section is empty, but it's too late. If we strip sections here,
9269 the dynamic symbol table is corrupted since the section symbol
9270 for the stripped section isn't written. */
9271
9272 return TRUE;
9273 }
9274
9275 /* Called after we have determined section placement. If sections
9276 move, we'll be called again. Provide a value for TOCstart. */
9277
9278 bfd_vma
9279 ppc64_elf_toc (bfd *obfd)
9280 {
9281 asection *s;
9282 bfd_vma TOCstart;
9283
9284 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9285 order. The TOC starts where the first of these sections starts. */
9286 s = bfd_get_section_by_name (obfd, ".got");
9287 if (s == NULL)
9288 s = bfd_get_section_by_name (obfd, ".toc");
9289 if (s == NULL)
9290 s = bfd_get_section_by_name (obfd, ".tocbss");
9291 if (s == NULL)
9292 s = bfd_get_section_by_name (obfd, ".plt");
9293 if (s == NULL)
9294 {
9295 /* This may happen for
9296 o references to TOC base (SYM@toc / TOC[tc0]) without a
9297 .toc directive
9298 o bad linker script
9299 o --gc-sections and empty TOC sections
9300
9301 FIXME: Warn user? */
9302
9303 /* Look for a likely section. We probably won't even be
9304 using TOCstart. */
9305 for (s = obfd->sections; s != NULL; s = s->next)
9306 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9307 == (SEC_ALLOC | SEC_SMALL_DATA))
9308 break;
9309 if (s == NULL)
9310 for (s = obfd->sections; s != NULL; s = s->next)
9311 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9312 == (SEC_ALLOC | SEC_SMALL_DATA))
9313 break;
9314 if (s == NULL)
9315 for (s = obfd->sections; s != NULL; s = s->next)
9316 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9317 break;
9318 if (s == NULL)
9319 for (s = obfd->sections; s != NULL; s = s->next)
9320 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9321 break;
9322 }
9323
9324 TOCstart = 0;
9325 if (s != NULL)
9326 TOCstart = s->output_section->vma + s->output_offset;
9327
9328 return TOCstart;
9329 }
9330
9331 /* Build all the stubs associated with the current output file.
9332 The stubs are kept in a hash table attached to the main linker
9333 hash table. This function is called via gldelf64ppc_finish. */
9334
9335 bfd_boolean
9336 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9337 struct bfd_link_info *info,
9338 char **stats)
9339 {
9340 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9341 asection *stub_sec;
9342 bfd_byte *p;
9343 int stub_sec_count = 0;
9344
9345 htab->emit_stub_syms = emit_stub_syms;
9346
9347 /* Allocate memory to hold the linker stubs. */
9348 for (stub_sec = htab->stub_bfd->sections;
9349 stub_sec != NULL;
9350 stub_sec = stub_sec->next)
9351 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9352 && stub_sec->size != 0)
9353 {
9354 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9355 if (stub_sec->contents == NULL)
9356 return FALSE;
9357 /* We want to check that built size is the same as calculated
9358 size. rawsize is a convenient location to use. */
9359 stub_sec->rawsize = stub_sec->size;
9360 stub_sec->size = 0;
9361 }
9362
9363 if (htab->plt != NULL)
9364 {
9365 unsigned int indx;
9366 bfd_vma plt0;
9367
9368 /* Build the .glink plt call stub. */
9369 plt0 = (htab->plt->output_section->vma
9370 + htab->plt->output_offset
9371 - (htab->glink->output_section->vma
9372 + htab->glink->output_offset
9373 + GLINK_CALL_STUB_SIZE));
9374 if (plt0 + 0x80008000 > 0xffffffff)
9375 {
9376 (*_bfd_error_handler) (_(".glink and .plt too far apart"));
9377 bfd_set_error (bfd_error_bad_value);
9378 return FALSE;
9379 }
9380
9381 if (htab->emit_stub_syms)
9382 {
9383 struct elf_link_hash_entry *h;
9384 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9385 if (h == NULL)
9386 return FALSE;
9387 if (h->root.type == bfd_link_hash_new)
9388 {
9389 h->root.type = bfd_link_hash_defined;
9390 h->root.u.def.section = htab->glink;
9391 h->root.u.def.value = 0;
9392 h->ref_regular = 1;
9393 h->def_regular = 1;
9394 h->ref_regular_nonweak = 1;
9395 h->forced_local = 1;
9396 h->non_elf = 0;
9397 }
9398 }
9399 p = htab->glink->contents;
9400 bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
9401 p += 4;
9402 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
9403 p += 4;
9404 bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
9405 p += 4;
9406 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
9407 p += 4;
9408 bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
9409 p += 4;
9410 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
9411 p += 4;
9412 bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
9413 p += 4;
9414 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
9415 p += 4;
9416 bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
9417 p += 4;
9418 bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
9419 p += 4;
9420 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
9421 p += 4;
9422 bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
9423 p += 4;
9424 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
9425 p += 4;
9426 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
9427 p += 4;
9428 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
9429 p += 4;
9430 bfd_put_32 (htab->glink->owner, BCTR, p);
9431 p += 4;
9432
9433 /* Build the .glink lazy link call stubs. */
9434 indx = 0;
9435 while (p < htab->glink->contents + htab->glink->size)
9436 {
9437 if (indx < 0x8000)
9438 {
9439 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9440 p += 4;
9441 }
9442 else
9443 {
9444 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9445 p += 4;
9446 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9447 p += 4;
9448 }
9449 bfd_put_32 (htab->glink->owner,
9450 B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
9451 indx++;
9452 p += 4;
9453 }
9454 htab->glink->rawsize = p - htab->glink->contents;
9455 }
9456
9457 if (htab->brlt->size != 0)
9458 {
9459 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
9460 htab->brlt->size);
9461 if (htab->brlt->contents == NULL)
9462 return FALSE;
9463 }
9464 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
9465 {
9466 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
9467 htab->relbrlt->size);
9468 if (htab->relbrlt->contents == NULL)
9469 return FALSE;
9470 }
9471
9472 /* Build the stubs as directed by the stub hash table. */
9473 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
9474
9475 for (stub_sec = htab->stub_bfd->sections;
9476 stub_sec != NULL;
9477 stub_sec = stub_sec->next)
9478 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9479 {
9480 stub_sec_count += 1;
9481 if (stub_sec->rawsize != stub_sec->size)
9482 break;
9483 }
9484
9485 if (stub_sec != NULL
9486 || htab->glink->rawsize != htab->glink->size)
9487 {
9488 htab->stub_error = TRUE;
9489 (*_bfd_error_handler) (_("stubs don't match calculated size"));
9490 }
9491
9492 if (htab->stub_error)
9493 return FALSE;
9494
9495 if (stats != NULL)
9496 {
9497 *stats = bfd_malloc (500);
9498 if (*stats == NULL)
9499 return FALSE;
9500
9501 sprintf (*stats, _("linker stubs in %u group%s\n"
9502 " branch %lu\n"
9503 " toc adjust %lu\n"
9504 " long branch %lu\n"
9505 " long toc adj %lu\n"
9506 " plt call %lu"),
9507 stub_sec_count,
9508 stub_sec_count == 1 ? "" : "s",
9509 htab->stub_count[ppc_stub_long_branch - 1],
9510 htab->stub_count[ppc_stub_long_branch_r2off - 1],
9511 htab->stub_count[ppc_stub_plt_branch - 1],
9512 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9513 htab->stub_count[ppc_stub_plt_call - 1]);
9514 }
9515 return TRUE;
9516 }
9517
9518 /* This function undoes the changes made by add_symbol_adjust. */
9519
9520 static bfd_boolean
9521 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9522 {
9523 struct ppc_link_hash_entry *eh;
9524
9525 if (h->root.type == bfd_link_hash_indirect)
9526 return TRUE;
9527
9528 if (h->root.type == bfd_link_hash_warning)
9529 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9530
9531 eh = (struct ppc_link_hash_entry *) h;
9532 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9533 return TRUE;
9534
9535 eh->elf.root.type = bfd_link_hash_undefined;
9536 return TRUE;
9537 }
9538
9539 void
9540 ppc64_elf_restore_symbols (struct bfd_link_info *info)
9541 {
9542 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9543 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9544 }
9545
9546 /* What to do when ld finds relocations against symbols defined in
9547 discarded sections. */
9548
9549 static unsigned int
9550 ppc64_elf_action_discarded (asection *sec)
9551 {
9552 if (strcmp (".opd", sec->name) == 0)
9553 return 0;
9554
9555 if (strcmp (".toc", sec->name) == 0)
9556 return 0;
9557
9558 if (strcmp (".toc1", sec->name) == 0)
9559 return 0;
9560
9561 return _bfd_elf_default_action_discarded (sec);
9562 }
9563
9564 /* The RELOCATE_SECTION function is called by the ELF backend linker
9565 to handle the relocations for a section.
9566
9567 The relocs are always passed as Rela structures; if the section
9568 actually uses Rel structures, the r_addend field will always be
9569 zero.
9570
9571 This function is responsible for adjust the section contents as
9572 necessary, and (if using Rela relocs and generating a
9573 relocatable output file) adjusting the reloc addend as
9574 necessary.
9575
9576 This function does not have to worry about setting the reloc
9577 address or the reloc symbol index.
9578
9579 LOCAL_SYMS is a pointer to the swapped in local symbols.
9580
9581 LOCAL_SECTIONS is an array giving the section in the input file
9582 corresponding to the st_shndx field of each local symbol.
9583
9584 The global hash table entry for the global symbols can be found
9585 via elf_sym_hashes (input_bfd).
9586
9587 When generating relocatable output, this function must handle
9588 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
9589 going to be the section symbol corresponding to the output
9590 section, which means that the addend must be adjusted
9591 accordingly. */
9592
9593 static bfd_boolean
9594 ppc64_elf_relocate_section (bfd *output_bfd,
9595 struct bfd_link_info *info,
9596 bfd *input_bfd,
9597 asection *input_section,
9598 bfd_byte *contents,
9599 Elf_Internal_Rela *relocs,
9600 Elf_Internal_Sym *local_syms,
9601 asection **local_sections)
9602 {
9603 struct ppc_link_hash_table *htab;
9604 Elf_Internal_Shdr *symtab_hdr;
9605 struct elf_link_hash_entry **sym_hashes;
9606 Elf_Internal_Rela *rel;
9607 Elf_Internal_Rela *relend;
9608 Elf_Internal_Rela outrel;
9609 bfd_byte *loc;
9610 struct got_entry **local_got_ents;
9611 bfd_vma TOCstart;
9612 bfd_boolean ret = TRUE;
9613 bfd_boolean is_opd;
9614 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
9615 bfd_boolean is_power4 = FALSE;
9616
9617 /* Initialize howto table if needed. */
9618 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9619 ppc_howto_init ();
9620
9621 htab = ppc_hash_table (info);
9622
9623 /* Don't relocate stub sections. */
9624 if (input_section->owner == htab->stub_bfd)
9625 return TRUE;
9626
9627 local_got_ents = elf_local_got_ents (input_bfd);
9628 TOCstart = elf_gp (output_bfd);
9629 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9630 sym_hashes = elf_sym_hashes (input_bfd);
9631 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
9632
9633 rel = relocs;
9634 relend = relocs + input_section->reloc_count;
9635 for (; rel < relend; rel++)
9636 {
9637 enum elf_ppc64_reloc_type r_type;
9638 bfd_vma addend, orig_addend;
9639 bfd_reloc_status_type r;
9640 Elf_Internal_Sym *sym;
9641 asection *sec;
9642 struct elf_link_hash_entry *h_elf;
9643 struct ppc_link_hash_entry *h;
9644 struct ppc_link_hash_entry *fdh;
9645 const char *sym_name;
9646 unsigned long r_symndx, toc_symndx;
9647 char tls_mask, tls_gd, tls_type;
9648 char sym_type;
9649 bfd_vma relocation;
9650 bfd_boolean unresolved_reloc;
9651 bfd_boolean warned;
9652 unsigned long insn, mask;
9653 struct ppc_stub_hash_entry *stub_entry;
9654 bfd_vma max_br_offset;
9655 bfd_vma from;
9656
9657 r_type = ELF64_R_TYPE (rel->r_info);
9658 r_symndx = ELF64_R_SYM (rel->r_info);
9659
9660 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9661 symbol of the previous ADDR64 reloc. The symbol gives us the
9662 proper TOC base to use. */
9663 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9664 && rel != relocs
9665 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9666 && is_opd)
9667 r_symndx = ELF64_R_SYM (rel[-1].r_info);
9668
9669 sym = NULL;
9670 sec = NULL;
9671 h_elf = NULL;
9672 sym_name = NULL;
9673 unresolved_reloc = FALSE;
9674 warned = FALSE;
9675 orig_addend = rel->r_addend;
9676
9677 if (r_symndx < symtab_hdr->sh_info)
9678 {
9679 /* It's a local symbol. */
9680 long *opd_adjust;
9681
9682 sym = local_syms + r_symndx;
9683 sec = local_sections[r_symndx];
9684 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
9685 sym_type = ELF64_ST_TYPE (sym->st_info);
9686 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
9687 opd_adjust = get_opd_info (sec);
9688 if (opd_adjust != NULL)
9689 {
9690 long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
9691 if (adjust == -1)
9692 relocation = 0;
9693 else
9694 {
9695 /* If this is a relocation against the opd section sym
9696 and we have edited .opd, adjust the reloc addend so
9697 that ld -r and ld --emit-relocs output is correct.
9698 If it is a reloc against some other .opd symbol,
9699 then the symbol value will be adjusted later. */
9700 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9701 rel->r_addend += adjust;
9702 else
9703 relocation += adjust;
9704 }
9705 }
9706 if (info->relocatable)
9707 continue;
9708 }
9709 else
9710 {
9711 if (info->relocatable)
9712 continue;
9713 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9714 r_symndx, symtab_hdr, sym_hashes,
9715 h_elf, sec, relocation,
9716 unresolved_reloc, warned);
9717 sym_name = h_elf->root.root.string;
9718 sym_type = h_elf->type;
9719 }
9720 h = (struct ppc_link_hash_entry *) h_elf;
9721
9722 /* TLS optimizations. Replace instruction sequences and relocs
9723 based on information we collected in tls_optimize. We edit
9724 RELOCS so that --emit-relocs will output something sensible
9725 for the final instruction stream. */
9726 tls_mask = 0;
9727 tls_gd = 0;
9728 toc_symndx = 0;
9729 if (IS_PPC64_TLS_RELOC (r_type))
9730 {
9731 if (h != NULL)
9732 tls_mask = h->tls_mask;
9733 else if (local_got_ents != NULL)
9734 {
9735 char *lgot_masks;
9736 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9737 tls_mask = lgot_masks[r_symndx];
9738 }
9739 if (tls_mask == 0 && r_type == R_PPC64_TLS)
9740 {
9741 /* Check for toc tls entries. */
9742 char *toc_tls;
9743
9744 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9745 rel, input_bfd))
9746 return FALSE;
9747
9748 if (toc_tls)
9749 tls_mask = *toc_tls;
9750 }
9751 }
9752
9753 /* Check that tls relocs are used with tls syms, and non-tls
9754 relocs are used with non-tls syms. */
9755 if (r_symndx != 0
9756 && r_type != R_PPC64_NONE
9757 && (h == NULL
9758 || h->elf.root.type == bfd_link_hash_defined
9759 || h->elf.root.type == bfd_link_hash_defweak)
9760 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9761 {
9762 if (r_type == R_PPC64_TLS && tls_mask != 0)
9763 /* R_PPC64_TLS is OK against a symbol in the TOC. */
9764 ;
9765 else
9766 (*_bfd_error_handler)
9767 (sym_type == STT_TLS
9768 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9769 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9770 input_bfd,
9771 input_section,
9772 (long) rel->r_offset,
9773 ppc64_elf_howto_table[r_type]->name,
9774 sym_name);
9775 }
9776
9777 /* Ensure reloc mapping code below stays sane. */
9778 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9779 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9780 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
9781 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9782 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9783 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9784 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
9785 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9786 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9787 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9788 abort ();
9789
9790 switch (r_type)
9791 {
9792 default:
9793 break;
9794
9795 case R_PPC64_TOC16:
9796 case R_PPC64_TOC16_LO:
9797 case R_PPC64_TOC16_DS:
9798 case R_PPC64_TOC16_LO_DS:
9799 {
9800 /* Check for toc tls entries. */
9801 char *toc_tls;
9802 int retval;
9803
9804 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9805 rel, input_bfd);
9806 if (retval == 0)
9807 return FALSE;
9808
9809 if (toc_tls)
9810 {
9811 tls_mask = *toc_tls;
9812 if (r_type == R_PPC64_TOC16_DS
9813 || r_type == R_PPC64_TOC16_LO_DS)
9814 {
9815 if (tls_mask != 0
9816 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
9817 goto toctprel;
9818 }
9819 else
9820 {
9821 /* If we found a GD reloc pair, then we might be
9822 doing a GD->IE transition. */
9823 if (retval == 2)
9824 {
9825 tls_gd = TLS_TPRELGD;
9826 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9827 goto tls_get_addr_check;
9828 }
9829 else if (retval == 3)
9830 {
9831 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9832 goto tls_get_addr_check;
9833 }
9834 }
9835 }
9836 }
9837 break;
9838
9839 case R_PPC64_GOT_TPREL16_DS:
9840 case R_PPC64_GOT_TPREL16_LO_DS:
9841 if (tls_mask != 0
9842 && (tls_mask & TLS_TPREL) == 0)
9843 {
9844 toctprel:
9845 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
9846 insn &= 31 << 21;
9847 insn |= 0x3c0d0000; /* addis 0,13,0 */
9848 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
9849 r_type = R_PPC64_TPREL16_HA;
9850 if (toc_symndx != 0)
9851 {
9852 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9853 /* We changed the symbol. Start over in order to
9854 get h, sym, sec etc. right. */
9855 rel--;
9856 continue;
9857 }
9858 else
9859 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9860 }
9861 break;
9862
9863 case R_PPC64_TLS:
9864 if (tls_mask != 0
9865 && (tls_mask & TLS_TPREL) == 0)
9866 {
9867 bfd_vma rtra;
9868 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
9869 if ((insn & ((0x3f << 26) | (31 << 11)))
9870 == ((31 << 26) | (13 << 11)))
9871 rtra = insn & ((1 << 26) - (1 << 16));
9872 else if ((insn & ((0x3f << 26) | (31 << 16)))
9873 == ((31 << 26) | (13 << 16)))
9874 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
9875 else
9876 abort ();
9877 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
9878 /* add -> addi. */
9879 insn = 14 << 26;
9880 else if ((insn & (31 << 1)) == 23 << 1
9881 && ((insn & (31 << 6)) < 14 << 6
9882 || ((insn & (31 << 6)) >= 16 << 6
9883 && (insn & (31 << 6)) < 24 << 6)))
9884 /* load and store indexed -> dform. */
9885 insn = (32 | ((insn >> 6) & 31)) << 26;
9886 else if ((insn & (31 << 1)) == 21 << 1
9887 && (insn & (0x1a << 6)) == 0)
9888 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
9889 insn = (((58 | ((insn >> 6) & 4)) << 26)
9890 | ((insn >> 6) & 1));
9891 else if ((insn & (31 << 1)) == 21 << 1
9892 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
9893 /* lwax -> lwa. */
9894 insn = (58 << 26) | 2;
9895 else
9896 abort ();
9897 insn |= rtra;
9898 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
9899 /* Was PPC64_TLS which sits on insn boundary, now
9900 PPC64_TPREL16_LO which is at insn+2. */
9901 rel->r_offset += 2;
9902 r_type = R_PPC64_TPREL16_LO;
9903 if (toc_symndx != 0)
9904 {
9905 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9906 /* We changed the symbol. Start over in order to
9907 get h, sym, sec etc. right. */
9908 rel--;
9909 continue;
9910 }
9911 else
9912 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9913 }
9914 break;
9915
9916 case R_PPC64_GOT_TLSGD16_HI:
9917 case R_PPC64_GOT_TLSGD16_HA:
9918 tls_gd = TLS_TPRELGD;
9919 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9920 goto tls_gdld_hi;
9921 break;
9922
9923 case R_PPC64_GOT_TLSLD16_HI:
9924 case R_PPC64_GOT_TLSLD16_HA:
9925 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9926 {
9927 tls_gdld_hi:
9928 if ((tls_mask & tls_gd) != 0)
9929 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
9930 + R_PPC64_GOT_TPREL16_DS);
9931 else
9932 {
9933 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
9934 rel->r_offset -= 2;
9935 r_type = R_PPC64_NONE;
9936 }
9937 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9938 }
9939 break;
9940
9941 case R_PPC64_GOT_TLSGD16:
9942 case R_PPC64_GOT_TLSGD16_LO:
9943 tls_gd = TLS_TPRELGD;
9944 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9945 goto tls_get_addr_check;
9946 break;
9947
9948 case R_PPC64_GOT_TLSLD16:
9949 case R_PPC64_GOT_TLSLD16_LO:
9950 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9951 {
9952 tls_get_addr_check:
9953 if (rel + 1 < relend)
9954 {
9955 enum elf_ppc64_reloc_type r_type2;
9956 unsigned long r_symndx2;
9957 struct elf_link_hash_entry *h2;
9958 bfd_vma insn1, insn2, insn3;
9959 bfd_vma offset;
9960
9961 /* The next instruction should be a call to
9962 __tls_get_addr. Peek at the reloc to be sure. */
9963 r_type2 = ELF64_R_TYPE (rel[1].r_info);
9964 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
9965 if (r_symndx2 < symtab_hdr->sh_info
9966 || (r_type2 != R_PPC64_REL14
9967 && r_type2 != R_PPC64_REL14_BRTAKEN
9968 && r_type2 != R_PPC64_REL14_BRNTAKEN
9969 && r_type2 != R_PPC64_REL24))
9970 break;
9971
9972 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
9973 while (h2->root.type == bfd_link_hash_indirect
9974 || h2->root.type == bfd_link_hash_warning)
9975 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
9976 if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
9977 && h2 != &htab->tls_get_addr_fd->elf))
9978 break;
9979
9980 /* OK, it checks out. Replace the call. */
9981 offset = rel[1].r_offset;
9982 insn1 = bfd_get_32 (output_bfd,
9983 contents + rel->r_offset - 2);
9984 insn3 = bfd_get_32 (output_bfd,
9985 contents + offset + 4);
9986 if ((tls_mask & tls_gd) != 0)
9987 {
9988 /* IE */
9989 insn1 &= (1 << 26) - (1 << 2);
9990 insn1 |= 58 << 26; /* ld */
9991 insn2 = 0x7c636a14; /* add 3,3,13 */
9992 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
9993 if ((tls_mask & TLS_EXPLICIT) == 0)
9994 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
9995 + R_PPC64_GOT_TPREL16_DS);
9996 else
9997 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
9998 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
9999 }
10000 else
10001 {
10002 /* LE */
10003 insn1 = 0x3c6d0000; /* addis 3,13,0 */
10004 insn2 = 0x38630000; /* addi 3,3,0 */
10005 if (tls_gd == 0)
10006 {
10007 /* Was an LD reloc. */
10008 r_symndx = 0;
10009 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10010 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10011 }
10012 else if (toc_symndx != 0)
10013 r_symndx = toc_symndx;
10014 r_type = R_PPC64_TPREL16_HA;
10015 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10016 rel[1].r_info = ELF64_R_INFO (r_symndx,
10017 R_PPC64_TPREL16_LO);
10018 rel[1].r_offset += 2;
10019 }
10020 if (insn3 == NOP
10021 || insn3 == CROR_151515 || insn3 == CROR_313131)
10022 {
10023 insn3 = insn2;
10024 insn2 = NOP;
10025 rel[1].r_offset += 4;
10026 }
10027 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
10028 bfd_put_32 (output_bfd, insn2, contents + offset);
10029 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
10030 if (tls_gd == 0 || toc_symndx != 0)
10031 {
10032 /* We changed the symbol. Start over in order
10033 to get h, sym, sec etc. right. */
10034 rel--;
10035 continue;
10036 }
10037 }
10038 }
10039 break;
10040
10041 case R_PPC64_DTPMOD64:
10042 if (rel + 1 < relend
10043 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10044 && rel[1].r_offset == rel->r_offset + 8)
10045 {
10046 if ((tls_mask & TLS_GD) == 0)
10047 {
10048 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10049 if ((tls_mask & TLS_TPRELGD) != 0)
10050 r_type = R_PPC64_TPREL64;
10051 else
10052 {
10053 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10054 r_type = R_PPC64_NONE;
10055 }
10056 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10057 }
10058 }
10059 else
10060 {
10061 if ((tls_mask & TLS_LD) == 0)
10062 {
10063 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
10064 r_type = R_PPC64_NONE;
10065 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10066 }
10067 }
10068 break;
10069
10070 case R_PPC64_TPREL64:
10071 if ((tls_mask & TLS_TPREL) == 0)
10072 {
10073 r_type = R_PPC64_NONE;
10074 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10075 }
10076 break;
10077 }
10078
10079 /* Handle other relocations that tweak non-addend part of insn. */
10080 insn = 0;
10081 max_br_offset = 1 << 25;
10082 addend = rel->r_addend;
10083 switch (r_type)
10084 {
10085 default:
10086 break;
10087
10088 /* Branch taken prediction relocations. */
10089 case R_PPC64_ADDR14_BRTAKEN:
10090 case R_PPC64_REL14_BRTAKEN:
10091 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
10092 /* Fall thru. */
10093
10094 /* Branch not taken prediction relocations. */
10095 case R_PPC64_ADDR14_BRNTAKEN:
10096 case R_PPC64_REL14_BRNTAKEN:
10097 insn |= bfd_get_32 (output_bfd,
10098 contents + rel->r_offset) & ~(0x01 << 21);
10099 /* Fall thru. */
10100
10101 case R_PPC64_REL14:
10102 max_br_offset = 1 << 15;
10103 /* Fall thru. */
10104
10105 case R_PPC64_REL24:
10106 /* Calls to functions with a different TOC, such as calls to
10107 shared objects, need to alter the TOC pointer. This is
10108 done using a linkage stub. A REL24 branching to these
10109 linkage stubs needs to be followed by a nop, as the nop
10110 will be replaced with an instruction to restore the TOC
10111 base pointer. */
10112 stub_entry = NULL;
10113 fdh = h;
10114 if (((h != NULL
10115 && (((fdh = h->oh) != NULL
10116 && fdh->elf.plt.plist != NULL)
10117 || (fdh = h)->elf.plt.plist != NULL))
10118 || (sec != NULL
10119 && sec->output_section != NULL
10120 && sec->id <= htab->top_id
10121 && (htab->stub_group[sec->id].toc_off
10122 != htab->stub_group[input_section->id].toc_off)))
10123 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
10124 rel, htab)) != NULL
10125 && (stub_entry->stub_type == ppc_stub_plt_call
10126 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10127 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
10128 {
10129 bfd_boolean can_plt_call = FALSE;
10130
10131 if (rel->r_offset + 8 <= input_section->size)
10132 {
10133 unsigned long nop;
10134 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10135 if (nop == NOP
10136 || nop == CROR_151515 || nop == CROR_313131)
10137 {
10138 bfd_put_32 (input_bfd, LD_R2_40R1,
10139 contents + rel->r_offset + 4);
10140 can_plt_call = TRUE;
10141 }
10142 }
10143
10144 if (!can_plt_call)
10145 {
10146 if (stub_entry->stub_type == ppc_stub_plt_call)
10147 {
10148 /* If this is a plain branch rather than a branch
10149 and link, don't require a nop. However, don't
10150 allow tail calls in a shared library as they
10151 will result in r2 being corrupted. */
10152 unsigned long br;
10153 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
10154 if (info->executable && (br & 1) == 0)
10155 can_plt_call = TRUE;
10156 else
10157 stub_entry = NULL;
10158 }
10159 else if (h != NULL
10160 && strcmp (h->elf.root.root.string,
10161 ".__libc_start_main") == 0)
10162 {
10163 /* Allow crt1 branch to go via a toc adjusting stub. */
10164 can_plt_call = TRUE;
10165 }
10166 else
10167 {
10168 if (strcmp (input_section->output_section->name,
10169 ".init") == 0
10170 || strcmp (input_section->output_section->name,
10171 ".fini") == 0)
10172 (*_bfd_error_handler)
10173 (_("%B(%A+0x%lx): automatic multiple TOCs "
10174 "not supported using your crt files; "
10175 "recompile with -mminimal-toc or upgrade gcc"),
10176 input_bfd,
10177 input_section,
10178 (long) rel->r_offset);
10179 else
10180 (*_bfd_error_handler)
10181 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
10182 "does not allow automatic multiple TOCs; "
10183 "recompile with -mminimal-toc or "
10184 "-fno-optimize-sibling-calls, "
10185 "or make `%s' extern"),
10186 input_bfd,
10187 input_section,
10188 (long) rel->r_offset,
10189 sym_name,
10190 sym_name);
10191 bfd_set_error (bfd_error_bad_value);
10192 ret = FALSE;
10193 }
10194 }
10195
10196 if (can_plt_call
10197 && stub_entry->stub_type == ppc_stub_plt_call)
10198 unresolved_reloc = FALSE;
10199 }
10200
10201 if (stub_entry == NULL
10202 && get_opd_info (sec) != NULL)
10203 {
10204 /* The branch destination is the value of the opd entry. */
10205 bfd_vma off = (relocation + addend
10206 - sec->output_section->vma
10207 - sec->output_offset);
10208 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10209 if (dest != (bfd_vma) -1)
10210 {
10211 relocation = dest;
10212 addend = 0;
10213 }
10214 }
10215
10216 /* If the branch is out of reach we ought to have a long
10217 branch stub. */
10218 from = (rel->r_offset
10219 + input_section->output_offset
10220 + input_section->output_section->vma);
10221
10222 if (stub_entry == NULL
10223 && (relocation + addend - from + max_br_offset
10224 >= 2 * max_br_offset)
10225 && r_type != R_PPC64_ADDR14_BRTAKEN
10226 && r_type != R_PPC64_ADDR14_BRNTAKEN)
10227 stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10228 htab);
10229
10230 if (stub_entry != NULL)
10231 {
10232 /* Munge up the value and addend so that we call the stub
10233 rather than the procedure directly. */
10234 relocation = (stub_entry->stub_offset
10235 + stub_entry->stub_sec->output_offset
10236 + stub_entry->stub_sec->output_section->vma);
10237 addend = 0;
10238 }
10239
10240 if (insn != 0)
10241 {
10242 if (is_power4)
10243 {
10244 /* Set 'a' bit. This is 0b00010 in BO field for branch
10245 on CR(BI) insns (BO == 001at or 011at), and 0b01000
10246 for branch on CTR insns (BO == 1a00t or 1a01t). */
10247 if ((insn & (0x14 << 21)) == (0x04 << 21))
10248 insn |= 0x02 << 21;
10249 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10250 insn |= 0x08 << 21;
10251 else
10252 break;
10253 }
10254 else
10255 {
10256 /* Invert 'y' bit if not the default. */
10257 if ((bfd_signed_vma) (relocation + addend - from) < 0)
10258 insn ^= 0x01 << 21;
10259 }
10260
10261 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
10262 }
10263
10264 /* NOP out calls to undefined weak functions.
10265 We can thus call a weak function without first
10266 checking whether the function is defined. */
10267 else if (h != NULL
10268 && h->elf.root.type == bfd_link_hash_undefweak
10269 && r_type == R_PPC64_REL24
10270 && relocation == 0
10271 && addend == 0)
10272 {
10273 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10274 continue;
10275 }
10276 break;
10277 }
10278
10279 /* Set `addend'. */
10280 tls_type = 0;
10281 switch (r_type)
10282 {
10283 default:
10284 (*_bfd_error_handler)
10285 (_("%B: unknown relocation type %d for symbol %s"),
10286 input_bfd, (int) r_type, sym_name);
10287
10288 bfd_set_error (bfd_error_bad_value);
10289 ret = FALSE;
10290 continue;
10291
10292 case R_PPC64_NONE:
10293 case R_PPC64_TLS:
10294 case R_PPC64_GNU_VTINHERIT:
10295 case R_PPC64_GNU_VTENTRY:
10296 continue;
10297
10298 /* GOT16 relocations. Like an ADDR16 using the symbol's
10299 address in the GOT as relocation value instead of the
10300 symbol's value itself. Also, create a GOT entry for the
10301 symbol and put the symbol value there. */
10302 case R_PPC64_GOT_TLSGD16:
10303 case R_PPC64_GOT_TLSGD16_LO:
10304 case R_PPC64_GOT_TLSGD16_HI:
10305 case R_PPC64_GOT_TLSGD16_HA:
10306 tls_type = TLS_TLS | TLS_GD;
10307 goto dogot;
10308
10309 case R_PPC64_GOT_TLSLD16:
10310 case R_PPC64_GOT_TLSLD16_LO:
10311 case R_PPC64_GOT_TLSLD16_HI:
10312 case R_PPC64_GOT_TLSLD16_HA:
10313 tls_type = TLS_TLS | TLS_LD;
10314 goto dogot;
10315
10316 case R_PPC64_GOT_TPREL16_DS:
10317 case R_PPC64_GOT_TPREL16_LO_DS:
10318 case R_PPC64_GOT_TPREL16_HI:
10319 case R_PPC64_GOT_TPREL16_HA:
10320 tls_type = TLS_TLS | TLS_TPREL;
10321 goto dogot;
10322
10323 case R_PPC64_GOT_DTPREL16_DS:
10324 case R_PPC64_GOT_DTPREL16_LO_DS:
10325 case R_PPC64_GOT_DTPREL16_HI:
10326 case R_PPC64_GOT_DTPREL16_HA:
10327 tls_type = TLS_TLS | TLS_DTPREL;
10328 goto dogot;
10329
10330 case R_PPC64_GOT16:
10331 case R_PPC64_GOT16_LO:
10332 case R_PPC64_GOT16_HI:
10333 case R_PPC64_GOT16_HA:
10334 case R_PPC64_GOT16_DS:
10335 case R_PPC64_GOT16_LO_DS:
10336 dogot:
10337 {
10338 /* Relocation is to the entry for this symbol in the global
10339 offset table. */
10340 asection *got;
10341 bfd_vma *offp;
10342 bfd_vma off;
10343 unsigned long indx = 0;
10344
10345 if (tls_type == (TLS_TLS | TLS_LD)
10346 && (h == NULL
10347 || !h->elf.def_dynamic))
10348 offp = &ppc64_tlsld_got (input_bfd)->offset;
10349 else
10350 {
10351 struct got_entry *ent;
10352
10353 if (h != NULL)
10354 {
10355 bfd_boolean dyn = htab->elf.dynamic_sections_created;
10356 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10357 &h->elf)
10358 || (info->shared
10359 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
10360 /* This is actually a static link, or it is a
10361 -Bsymbolic link and the symbol is defined
10362 locally, or the symbol was forced to be local
10363 because of a version file. */
10364 ;
10365 else
10366 {
10367 indx = h->elf.dynindx;
10368 unresolved_reloc = FALSE;
10369 }
10370 ent = h->elf.got.glist;
10371 }
10372 else
10373 {
10374 if (local_got_ents == NULL)
10375 abort ();
10376 ent = local_got_ents[r_symndx];
10377 }
10378
10379 for (; ent != NULL; ent = ent->next)
10380 if (ent->addend == orig_addend
10381 && ent->owner == input_bfd
10382 && ent->tls_type == tls_type)
10383 break;
10384 if (ent == NULL)
10385 abort ();
10386 offp = &ent->got.offset;
10387 }
10388
10389 got = ppc64_elf_tdata (input_bfd)->got;
10390 if (got == NULL)
10391 abort ();
10392
10393 /* The offset must always be a multiple of 8. We use the
10394 least significant bit to record whether we have already
10395 processed this entry. */
10396 off = *offp;
10397 if ((off & 1) != 0)
10398 off &= ~1;
10399 else
10400 {
10401 /* Generate relocs for the dynamic linker, except in
10402 the case of TLSLD where we'll use one entry per
10403 module. */
10404 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10405
10406 *offp = off | 1;
10407 if ((info->shared || indx != 0)
10408 && (h == NULL
10409 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10410 || h->elf.root.type != bfd_link_hash_undefweak))
10411 {
10412 outrel.r_offset = (got->output_section->vma
10413 + got->output_offset
10414 + off);
10415 outrel.r_addend = addend;
10416 if (tls_type & (TLS_LD | TLS_GD))
10417 {
10418 outrel.r_addend = 0;
10419 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
10420 if (tls_type == (TLS_TLS | TLS_GD))
10421 {
10422 loc = relgot->contents;
10423 loc += (relgot->reloc_count++
10424 * sizeof (Elf64_External_Rela));
10425 bfd_elf64_swap_reloca_out (output_bfd,
10426 &outrel, loc);
10427 outrel.r_offset += 8;
10428 outrel.r_addend = addend;
10429 outrel.r_info
10430 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10431 }
10432 }
10433 else if (tls_type == (TLS_TLS | TLS_DTPREL))
10434 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
10435 else if (tls_type == (TLS_TLS | TLS_TPREL))
10436 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10437 else if (indx == 0)
10438 {
10439 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10440
10441 /* Write the .got section contents for the sake
10442 of prelink. */
10443 loc = got->contents + off;
10444 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10445 loc);
10446 }
10447 else
10448 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
10449
10450 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
10451 {
10452 outrel.r_addend += relocation;
10453 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
10454 outrel.r_addend -= htab->elf.tls_sec->vma;
10455 }
10456 loc = relgot->contents;
10457 loc += (relgot->reloc_count++
10458 * sizeof (Elf64_External_Rela));
10459 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10460 }
10461
10462 /* Init the .got section contents here if we're not
10463 emitting a reloc. */
10464 else
10465 {
10466 relocation += addend;
10467 if (tls_type == (TLS_TLS | TLS_LD))
10468 relocation = 1;
10469 else if (tls_type != 0)
10470 {
10471 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
10472 if (tls_type == (TLS_TLS | TLS_TPREL))
10473 relocation += DTP_OFFSET - TP_OFFSET;
10474
10475 if (tls_type == (TLS_TLS | TLS_GD))
10476 {
10477 bfd_put_64 (output_bfd, relocation,
10478 got->contents + off + 8);
10479 relocation = 1;
10480 }
10481 }
10482
10483 bfd_put_64 (output_bfd, relocation,
10484 got->contents + off);
10485 }
10486 }
10487
10488 if (off >= (bfd_vma) -2)
10489 abort ();
10490
10491 relocation = got->output_offset + off;
10492
10493 /* TOC base (r2) is TOC start plus 0x8000. */
10494 addend = -TOC_BASE_OFF;
10495 }
10496 break;
10497
10498 case R_PPC64_PLT16_HA:
10499 case R_PPC64_PLT16_HI:
10500 case R_PPC64_PLT16_LO:
10501 case R_PPC64_PLT32:
10502 case R_PPC64_PLT64:
10503 /* Relocation is to the entry for this symbol in the
10504 procedure linkage table. */
10505
10506 /* Resolve a PLT reloc against a local symbol directly,
10507 without using the procedure linkage table. */
10508 if (h == NULL)
10509 break;
10510
10511 /* It's possible that we didn't make a PLT entry for this
10512 symbol. This happens when statically linking PIC code,
10513 or when using -Bsymbolic. Go find a match if there is a
10514 PLT entry. */
10515 if (htab->plt != NULL)
10516 {
10517 struct plt_entry *ent;
10518 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
10519 if (ent->addend == orig_addend
10520 && ent->plt.offset != (bfd_vma) -1)
10521 {
10522 relocation = (htab->plt->output_section->vma
10523 + htab->plt->output_offset
10524 + ent->plt.offset);
10525 unresolved_reloc = FALSE;
10526 }
10527 }
10528 break;
10529
10530 case R_PPC64_TOC:
10531 /* Relocation value is TOC base. */
10532 relocation = TOCstart;
10533 if (r_symndx == 0)
10534 relocation += htab->stub_group[input_section->id].toc_off;
10535 else if (unresolved_reloc)
10536 ;
10537 else if (sec != NULL && sec->id <= htab->top_id)
10538 relocation += htab->stub_group[sec->id].toc_off;
10539 else
10540 unresolved_reloc = TRUE;
10541 goto dodyn2;
10542
10543 /* TOC16 relocs. We want the offset relative to the TOC base,
10544 which is the address of the start of the TOC plus 0x8000.
10545 The TOC consists of sections .got, .toc, .tocbss, and .plt,
10546 in this order. */
10547 case R_PPC64_TOC16:
10548 case R_PPC64_TOC16_LO:
10549 case R_PPC64_TOC16_HI:
10550 case R_PPC64_TOC16_DS:
10551 case R_PPC64_TOC16_LO_DS:
10552 case R_PPC64_TOC16_HA:
10553 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
10554 break;
10555
10556 /* Relocate against the beginning of the section. */
10557 case R_PPC64_SECTOFF:
10558 case R_PPC64_SECTOFF_LO:
10559 case R_PPC64_SECTOFF_HI:
10560 case R_PPC64_SECTOFF_DS:
10561 case R_PPC64_SECTOFF_LO_DS:
10562 case R_PPC64_SECTOFF_HA:
10563 if (sec != NULL)
10564 addend -= sec->output_section->vma;
10565 break;
10566
10567 case R_PPC64_REL14:
10568 case R_PPC64_REL14_BRNTAKEN:
10569 case R_PPC64_REL14_BRTAKEN:
10570 case R_PPC64_REL24:
10571 break;
10572
10573 case R_PPC64_TPREL16:
10574 case R_PPC64_TPREL16_LO:
10575 case R_PPC64_TPREL16_HI:
10576 case R_PPC64_TPREL16_HA:
10577 case R_PPC64_TPREL16_DS:
10578 case R_PPC64_TPREL16_LO_DS:
10579 case R_PPC64_TPREL16_HIGHER:
10580 case R_PPC64_TPREL16_HIGHERA:
10581 case R_PPC64_TPREL16_HIGHEST:
10582 case R_PPC64_TPREL16_HIGHESTA:
10583 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10584 if (info->shared)
10585 /* The TPREL16 relocs shouldn't really be used in shared
10586 libs as they will result in DT_TEXTREL being set, but
10587 support them anyway. */
10588 goto dodyn;
10589 break;
10590
10591 case R_PPC64_DTPREL16:
10592 case R_PPC64_DTPREL16_LO:
10593 case R_PPC64_DTPREL16_HI:
10594 case R_PPC64_DTPREL16_HA:
10595 case R_PPC64_DTPREL16_DS:
10596 case R_PPC64_DTPREL16_LO_DS:
10597 case R_PPC64_DTPREL16_HIGHER:
10598 case R_PPC64_DTPREL16_HIGHERA:
10599 case R_PPC64_DTPREL16_HIGHEST:
10600 case R_PPC64_DTPREL16_HIGHESTA:
10601 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10602 break;
10603
10604 case R_PPC64_DTPMOD64:
10605 relocation = 1;
10606 addend = 0;
10607 goto dodyn;
10608
10609 case R_PPC64_TPREL64:
10610 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
10611 goto dodyn;
10612
10613 case R_PPC64_DTPREL64:
10614 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
10615 /* Fall thru */
10616
10617 /* Relocations that may need to be propagated if this is a
10618 dynamic object. */
10619 case R_PPC64_REL30:
10620 case R_PPC64_REL32:
10621 case R_PPC64_REL64:
10622 case R_PPC64_ADDR14:
10623 case R_PPC64_ADDR14_BRNTAKEN:
10624 case R_PPC64_ADDR14_BRTAKEN:
10625 case R_PPC64_ADDR16:
10626 case R_PPC64_ADDR16_DS:
10627 case R_PPC64_ADDR16_HA:
10628 case R_PPC64_ADDR16_HI:
10629 case R_PPC64_ADDR16_HIGHER:
10630 case R_PPC64_ADDR16_HIGHERA:
10631 case R_PPC64_ADDR16_HIGHEST:
10632 case R_PPC64_ADDR16_HIGHESTA:
10633 case R_PPC64_ADDR16_LO:
10634 case R_PPC64_ADDR16_LO_DS:
10635 case R_PPC64_ADDR24:
10636 case R_PPC64_ADDR32:
10637 case R_PPC64_ADDR64:
10638 case R_PPC64_UADDR16:
10639 case R_PPC64_UADDR32:
10640 case R_PPC64_UADDR64:
10641 /* r_symndx will be zero only for relocs against symbols
10642 from removed linkonce sections, or sections discarded by
10643 a linker script. */
10644 dodyn:
10645 if (r_symndx == 0)
10646 break;
10647 /* Fall thru. */
10648
10649 dodyn2:
10650 if ((input_section->flags & SEC_ALLOC) == 0)
10651 break;
10652
10653 if (NO_OPD_RELOCS && is_opd)
10654 break;
10655
10656 if ((info->shared
10657 && (h == NULL
10658 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10659 || h->elf.root.type != bfd_link_hash_undefweak)
10660 && (MUST_BE_DYN_RELOC (r_type)
10661 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
10662 || (ELIMINATE_COPY_RELOCS
10663 && !info->shared
10664 && h != NULL
10665 && h->elf.dynindx != -1
10666 && !h->elf.non_got_ref
10667 && h->elf.def_dynamic
10668 && !h->elf.def_regular))
10669 {
10670 Elf_Internal_Rela outrel;
10671 bfd_boolean skip, relocate;
10672 asection *sreloc;
10673 bfd_byte *loc;
10674 bfd_vma out_off;
10675
10676 /* When generating a dynamic object, these relocations
10677 are copied into the output file to be resolved at run
10678 time. */
10679
10680 skip = FALSE;
10681 relocate = FALSE;
10682
10683 out_off = _bfd_elf_section_offset (output_bfd, info,
10684 input_section, rel->r_offset);
10685 if (out_off == (bfd_vma) -1)
10686 skip = TRUE;
10687 else if (out_off == (bfd_vma) -2)
10688 skip = TRUE, relocate = TRUE;
10689 out_off += (input_section->output_section->vma
10690 + input_section->output_offset);
10691 outrel.r_offset = out_off;
10692 outrel.r_addend = rel->r_addend;
10693
10694 /* Optimize unaligned reloc use. */
10695 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10696 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10697 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10698 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10699 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10700 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10701 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10702 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10703 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10704
10705 if (skip)
10706 memset (&outrel, 0, sizeof outrel);
10707 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
10708 && !is_opd
10709 && r_type != R_PPC64_TOC)
10710 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
10711 else
10712 {
10713 /* This symbol is local, or marked to become local,
10714 or this is an opd section reloc which must point
10715 at a local function. */
10716 outrel.r_addend += relocation;
10717 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
10718 {
10719 if (is_opd && h != NULL)
10720 {
10721 /* Lie about opd entries. This case occurs
10722 when building shared libraries and we
10723 reference a function in another shared
10724 lib. The same thing happens for a weak
10725 definition in an application that's
10726 overridden by a strong definition in a
10727 shared lib. (I believe this is a generic
10728 bug in binutils handling of weak syms.)
10729 In these cases we won't use the opd
10730 entry in this lib. */
10731 unresolved_reloc = FALSE;
10732 }
10733 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10734
10735 /* We need to relocate .opd contents for ld.so.
10736 Prelink also wants simple and consistent rules
10737 for relocs. This make all RELATIVE relocs have
10738 *r_offset equal to r_addend. */
10739 relocate = TRUE;
10740 }
10741 else
10742 {
10743 long indx = 0;
10744
10745 if (bfd_is_abs_section (sec))
10746 ;
10747 else if (sec == NULL || sec->owner == NULL)
10748 {
10749 bfd_set_error (bfd_error_bad_value);
10750 return FALSE;
10751 }
10752 else
10753 {
10754 asection *osec;
10755
10756 osec = sec->output_section;
10757 indx = elf_section_data (osec)->dynindx;
10758
10759 /* We are turning this relocation into one
10760 against a section symbol, so subtract out
10761 the output section's address but not the
10762 offset of the input section in the output
10763 section. */
10764 outrel.r_addend -= osec->vma;
10765 }
10766
10767 outrel.r_info = ELF64_R_INFO (indx, r_type);
10768 }
10769 }
10770
10771 sreloc = elf_section_data (input_section)->sreloc;
10772 if (sreloc == NULL)
10773 abort ();
10774
10775 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10776 >= sreloc->size)
10777 abort ();
10778 loc = sreloc->contents;
10779 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
10780 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10781
10782 /* If this reloc is against an external symbol, it will
10783 be computed at runtime, so there's no need to do
10784 anything now. However, for the sake of prelink ensure
10785 that the section contents are a known value. */
10786 if (! relocate)
10787 {
10788 unresolved_reloc = FALSE;
10789 /* The value chosen here is quite arbitrary as ld.so
10790 ignores section contents except for the special
10791 case of .opd where the contents might be accessed
10792 before relocation. Choose zero, as that won't
10793 cause reloc overflow. */
10794 relocation = 0;
10795 addend = 0;
10796 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
10797 to improve backward compatibility with older
10798 versions of ld. */
10799 if (r_type == R_PPC64_ADDR64)
10800 addend = outrel.r_addend;
10801 /* Adjust pc_relative relocs to have zero in *r_offset. */
10802 else if (ppc64_elf_howto_table[r_type]->pc_relative)
10803 addend = (input_section->output_section->vma
10804 + input_section->output_offset
10805 + rel->r_offset);
10806 }
10807 }
10808 break;
10809
10810 case R_PPC64_COPY:
10811 case R_PPC64_GLOB_DAT:
10812 case R_PPC64_JMP_SLOT:
10813 case R_PPC64_RELATIVE:
10814 /* We shouldn't ever see these dynamic relocs in relocatable
10815 files. */
10816 /* Fall through. */
10817
10818 case R_PPC64_PLTGOT16:
10819 case R_PPC64_PLTGOT16_DS:
10820 case R_PPC64_PLTGOT16_HA:
10821 case R_PPC64_PLTGOT16_HI:
10822 case R_PPC64_PLTGOT16_LO:
10823 case R_PPC64_PLTGOT16_LO_DS:
10824 case R_PPC64_PLTREL32:
10825 case R_PPC64_PLTREL64:
10826 /* These ones haven't been implemented yet. */
10827
10828 (*_bfd_error_handler)
10829 (_("%B: relocation %s is not supported for symbol %s."),
10830 input_bfd,
10831 ppc64_elf_howto_table[r_type]->name, sym_name);
10832
10833 bfd_set_error (bfd_error_invalid_operation);
10834 ret = FALSE;
10835 continue;
10836 }
10837
10838 /* Do any further special processing. */
10839 switch (r_type)
10840 {
10841 default:
10842 break;
10843
10844 case R_PPC64_ADDR16_HA:
10845 case R_PPC64_ADDR16_HIGHERA:
10846 case R_PPC64_ADDR16_HIGHESTA:
10847 case R_PPC64_GOT16_HA:
10848 case R_PPC64_PLTGOT16_HA:
10849 case R_PPC64_PLT16_HA:
10850 case R_PPC64_TOC16_HA:
10851 case R_PPC64_SECTOFF_HA:
10852 case R_PPC64_TPREL16_HA:
10853 case R_PPC64_DTPREL16_HA:
10854 case R_PPC64_GOT_TLSGD16_HA:
10855 case R_PPC64_GOT_TLSLD16_HA:
10856 case R_PPC64_GOT_TPREL16_HA:
10857 case R_PPC64_GOT_DTPREL16_HA:
10858 case R_PPC64_TPREL16_HIGHER:
10859 case R_PPC64_TPREL16_HIGHERA:
10860 case R_PPC64_TPREL16_HIGHEST:
10861 case R_PPC64_TPREL16_HIGHESTA:
10862 case R_PPC64_DTPREL16_HIGHER:
10863 case R_PPC64_DTPREL16_HIGHERA:
10864 case R_PPC64_DTPREL16_HIGHEST:
10865 case R_PPC64_DTPREL16_HIGHESTA:
10866 /* It's just possible that this symbol is a weak symbol
10867 that's not actually defined anywhere. In that case,
10868 'sec' would be NULL, and we should leave the symbol
10869 alone (it will be set to zero elsewhere in the link). */
10870 if (sec != NULL)
10871 /* Add 0x10000 if sign bit in 0:15 is set.
10872 Bits 0:15 are not used. */
10873 addend += 0x8000;
10874 break;
10875
10876 case R_PPC64_ADDR16_DS:
10877 case R_PPC64_ADDR16_LO_DS:
10878 case R_PPC64_GOT16_DS:
10879 case R_PPC64_GOT16_LO_DS:
10880 case R_PPC64_PLT16_LO_DS:
10881 case R_PPC64_SECTOFF_DS:
10882 case R_PPC64_SECTOFF_LO_DS:
10883 case R_PPC64_TOC16_DS:
10884 case R_PPC64_TOC16_LO_DS:
10885 case R_PPC64_PLTGOT16_DS:
10886 case R_PPC64_PLTGOT16_LO_DS:
10887 case R_PPC64_GOT_TPREL16_DS:
10888 case R_PPC64_GOT_TPREL16_LO_DS:
10889 case R_PPC64_GOT_DTPREL16_DS:
10890 case R_PPC64_GOT_DTPREL16_LO_DS:
10891 case R_PPC64_TPREL16_DS:
10892 case R_PPC64_TPREL16_LO_DS:
10893 case R_PPC64_DTPREL16_DS:
10894 case R_PPC64_DTPREL16_LO_DS:
10895 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
10896 mask = 3;
10897 /* If this reloc is against an lq insn, then the value must be
10898 a multiple of 16. This is somewhat of a hack, but the
10899 "correct" way to do this by defining _DQ forms of all the
10900 _DS relocs bloats all reloc switches in this file. It
10901 doesn't seem to make much sense to use any of these relocs
10902 in data, so testing the insn should be safe. */
10903 if ((insn & (0x3f << 26)) == (56u << 26))
10904 mask = 15;
10905 if (((relocation + addend) & mask) != 0)
10906 {
10907 (*_bfd_error_handler)
10908 (_("%B: error: relocation %s not a multiple of %d"),
10909 input_bfd,
10910 ppc64_elf_howto_table[r_type]->name,
10911 mask + 1);
10912 bfd_set_error (bfd_error_bad_value);
10913 ret = FALSE;
10914 continue;
10915 }
10916 break;
10917 }
10918
10919 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
10920 because such sections are not SEC_ALLOC and thus ld.so will
10921 not process them. */
10922 if (unresolved_reloc
10923 && !((input_section->flags & SEC_DEBUGGING) != 0
10924 && h->elf.def_dynamic))
10925 {
10926 (*_bfd_error_handler)
10927 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
10928 input_bfd,
10929 input_section,
10930 (long) rel->r_offset,
10931 ppc64_elf_howto_table[(int) r_type]->name,
10932 h->elf.root.root.string);
10933 ret = FALSE;
10934 }
10935
10936 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
10937 input_bfd,
10938 input_section,
10939 contents,
10940 rel->r_offset,
10941 relocation,
10942 addend);
10943
10944 if (r != bfd_reloc_ok)
10945 {
10946 if (sym_name == NULL)
10947 sym_name = "(null)";
10948 if (r == bfd_reloc_overflow)
10949 {
10950 if (warned)
10951 continue;
10952 if (h != NULL
10953 && h->elf.root.type == bfd_link_hash_undefweak
10954 && ppc64_elf_howto_table[r_type]->pc_relative)
10955 {
10956 /* Assume this is a call protected by other code that
10957 detects the symbol is undefined. If this is the case,
10958 we can safely ignore the overflow. If not, the
10959 program is hosed anyway, and a little warning isn't
10960 going to help. */
10961
10962 continue;
10963 }
10964
10965 if (!((*info->callbacks->reloc_overflow)
10966 (info, (h ? &h->elf.root : NULL), sym_name,
10967 ppc64_elf_howto_table[r_type]->name,
10968 orig_addend, input_bfd, input_section, rel->r_offset)))
10969 return FALSE;
10970 }
10971 else
10972 {
10973 (*_bfd_error_handler)
10974 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
10975 input_bfd,
10976 input_section,
10977 (long) rel->r_offset,
10978 ppc64_elf_howto_table[r_type]->name,
10979 sym_name,
10980 (int) r);
10981 ret = FALSE;
10982 }
10983 }
10984 }
10985
10986 /* If we're emitting relocations, then shortly after this function
10987 returns, reloc offsets and addends for this section will be
10988 adjusted. Worse, reloc symbol indices will be for the output
10989 file rather than the input. Save a copy of the relocs for
10990 opd_entry_value. */
10991 if (is_opd && (info->emitrelocations || info->relocatable))
10992 {
10993 bfd_size_type amt;
10994 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
10995 rel = bfd_alloc (input_bfd, amt);
10996 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
10997 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
10998 if (rel == NULL)
10999 return FALSE;
11000 memcpy (rel, relocs, amt);
11001 }
11002 return ret;
11003 }
11004
11005 /* Adjust the value of any local symbols in opd sections. */
11006
11007 static bfd_boolean
11008 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11009 const char *name ATTRIBUTE_UNUSED,
11010 Elf_Internal_Sym *elfsym,
11011 asection *input_sec,
11012 struct elf_link_hash_entry *h)
11013 {
11014 long *opd_adjust, adjust;
11015 bfd_vma value;
11016
11017 if (h != NULL)
11018 return TRUE;
11019
11020 opd_adjust = get_opd_info (input_sec);
11021 if (opd_adjust == NULL)
11022 return TRUE;
11023
11024 value = elfsym->st_value - input_sec->output_offset;
11025 if (!info->relocatable)
11026 value -= input_sec->output_section->vma;
11027
11028 adjust = opd_adjust[value / 8];
11029 if (adjust == -1)
11030 elfsym->st_value = 0;
11031 else
11032 elfsym->st_value += adjust;
11033 return TRUE;
11034 }
11035
11036 /* Finish up dynamic symbol handling. We set the contents of various
11037 dynamic sections here. */
11038
11039 static bfd_boolean
11040 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11041 struct bfd_link_info *info,
11042 struct elf_link_hash_entry *h,
11043 Elf_Internal_Sym *sym)
11044 {
11045 struct ppc_link_hash_table *htab;
11046 bfd *dynobj;
11047 struct plt_entry *ent;
11048 Elf_Internal_Rela rela;
11049 bfd_byte *loc;
11050
11051 htab = ppc_hash_table (info);
11052 dynobj = htab->elf.dynobj;
11053
11054 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11055 if (ent->plt.offset != (bfd_vma) -1)
11056 {
11057 /* This symbol has an entry in the procedure linkage
11058 table. Set it up. */
11059
11060 if (htab->plt == NULL
11061 || htab->relplt == NULL
11062 || htab->glink == NULL)
11063 abort ();
11064
11065 /* Create a JMP_SLOT reloc to inform the dynamic linker to
11066 fill in the PLT entry. */
11067 rela.r_offset = (htab->plt->output_section->vma
11068 + htab->plt->output_offset
11069 + ent->plt.offset);
11070 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11071 rela.r_addend = ent->addend;
11072
11073 loc = htab->relplt->contents;
11074 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11075 * sizeof (Elf64_External_Rela));
11076 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11077 }
11078
11079 if (h->needs_copy)
11080 {
11081 Elf_Internal_Rela rela;
11082 bfd_byte *loc;
11083
11084 /* This symbol needs a copy reloc. Set it up. */
11085
11086 if (h->dynindx == -1
11087 || (h->root.type != bfd_link_hash_defined
11088 && h->root.type != bfd_link_hash_defweak)
11089 || htab->relbss == NULL)
11090 abort ();
11091
11092 rela.r_offset = (h->root.u.def.value
11093 + h->root.u.def.section->output_section->vma
11094 + h->root.u.def.section->output_offset);
11095 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11096 rela.r_addend = 0;
11097 loc = htab->relbss->contents;
11098 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
11099 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11100 }
11101
11102 /* Mark some specially defined symbols as absolute. */
11103 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11104 sym->st_shndx = SHN_ABS;
11105
11106 return TRUE;
11107 }
11108
11109 /* Used to decide how to sort relocs in an optimal manner for the
11110 dynamic linker, before writing them out. */
11111
11112 static enum elf_reloc_type_class
11113 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
11114 {
11115 enum elf_ppc64_reloc_type r_type;
11116
11117 r_type = ELF64_R_TYPE (rela->r_info);
11118 switch (r_type)
11119 {
11120 case R_PPC64_RELATIVE:
11121 return reloc_class_relative;
11122 case R_PPC64_JMP_SLOT:
11123 return reloc_class_plt;
11124 case R_PPC64_COPY:
11125 return reloc_class_copy;
11126 default:
11127 return reloc_class_normal;
11128 }
11129 }
11130
11131 /* Finish up the dynamic sections. */
11132
11133 static bfd_boolean
11134 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11135 struct bfd_link_info *info)
11136 {
11137 struct ppc_link_hash_table *htab;
11138 bfd *dynobj;
11139 asection *sdyn;
11140
11141 htab = ppc_hash_table (info);
11142 dynobj = htab->elf.dynobj;
11143 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11144
11145 if (htab->elf.dynamic_sections_created)
11146 {
11147 Elf64_External_Dyn *dyncon, *dynconend;
11148
11149 if (sdyn == NULL || htab->got == NULL)
11150 abort ();
11151
11152 dyncon = (Elf64_External_Dyn *) sdyn->contents;
11153 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
11154 for (; dyncon < dynconend; dyncon++)
11155 {
11156 Elf_Internal_Dyn dyn;
11157 asection *s;
11158
11159 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11160
11161 switch (dyn.d_tag)
11162 {
11163 default:
11164 continue;
11165
11166 case DT_PPC64_GLINK:
11167 s = htab->glink;
11168 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11169 /* We stupidly defined DT_PPC64_GLINK to be the start
11170 of glink rather than the first entry point, which is
11171 what ld.so needs, and now have a bigger stub to
11172 support automatic multiple TOCs. */
11173 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
11174 break;
11175
11176 case DT_PPC64_OPD:
11177 s = bfd_get_section_by_name (output_bfd, ".opd");
11178 if (s == NULL)
11179 continue;
11180 dyn.d_un.d_ptr = s->vma;
11181 break;
11182
11183 case DT_PPC64_OPDSZ:
11184 s = bfd_get_section_by_name (output_bfd, ".opd");
11185 if (s == NULL)
11186 continue;
11187 dyn.d_un.d_val = s->size;
11188 break;
11189
11190 case DT_PLTGOT:
11191 s = htab->plt;
11192 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11193 break;
11194
11195 case DT_JMPREL:
11196 s = htab->relplt;
11197 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
11198 break;
11199
11200 case DT_PLTRELSZ:
11201 dyn.d_un.d_val = htab->relplt->size;
11202 break;
11203
11204 case DT_RELASZ:
11205 /* Don't count procedure linkage table relocs in the
11206 overall reloc count. */
11207 s = htab->relplt;
11208 if (s == NULL)
11209 continue;
11210 dyn.d_un.d_val -= s->size;
11211 break;
11212
11213 case DT_RELA:
11214 /* We may not be using the standard ELF linker script.
11215 If .rela.plt is the first .rela section, we adjust
11216 DT_RELA to not include it. */
11217 s = htab->relplt;
11218 if (s == NULL)
11219 continue;
11220 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11221 continue;
11222 dyn.d_un.d_ptr += s->size;
11223 break;
11224 }
11225
11226 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
11227 }
11228 }
11229
11230 if (htab->got != NULL && htab->got->size != 0)
11231 {
11232 /* Fill in the first entry in the global offset table.
11233 We use it to hold the link-time TOCbase. */
11234 bfd_put_64 (output_bfd,
11235 elf_gp (output_bfd) + TOC_BASE_OFF,
11236 htab->got->contents);
11237
11238 /* Set .got entry size. */
11239 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
11240 }
11241
11242 if (htab->plt != NULL && htab->plt->size != 0)
11243 {
11244 /* Set .plt entry size. */
11245 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
11246 = PLT_ENTRY_SIZE;
11247 }
11248
11249 /* We need to handle writing out multiple GOT sections ourselves,
11250 since we didn't add them to DYNOBJ. We know dynobj is the first
11251 bfd. */
11252 while ((dynobj = dynobj->link_next) != NULL)
11253 {
11254 asection *s;
11255
11256 if (!is_ppc64_elf_target (dynobj->xvec))
11257 continue;
11258
11259 s = ppc64_elf_tdata (dynobj)->got;
11260 if (s != NULL
11261 && s->size != 0
11262 && s->output_section != bfd_abs_section_ptr
11263 && !bfd_set_section_contents (output_bfd, s->output_section,
11264 s->contents, s->output_offset,
11265 s->size))
11266 return FALSE;
11267 s = ppc64_elf_tdata (dynobj)->relgot;
11268 if (s != NULL
11269 && s->size != 0
11270 && s->output_section != bfd_abs_section_ptr
11271 && !bfd_set_section_contents (output_bfd, s->output_section,
11272 s->contents, s->output_offset,
11273 s->size))
11274 return FALSE;
11275 }
11276
11277 return TRUE;
11278 }
11279
11280 #include "elf64-target.h"
This page took 0.343612 seconds and 4 git commands to generate.