d8f0b5e4220285496c5726c731cae8ecc7118ec2
[binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004
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 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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_brtaken_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_toc_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc64_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52
53
54 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
55 #define TARGET_LITTLE_NAME "elf64-powerpcle"
56 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
57 #define TARGET_BIG_NAME "elf64-powerpc"
58 #define ELF_ARCH bfd_arch_powerpc
59 #define ELF_MACHINE_CODE EM_PPC64
60 #define ELF_MAXPAGESIZE 0x10000
61 #define elf_info_to_howto ppc64_elf_info_to_howto
62
63 #define elf_backend_want_got_sym 0
64 #define elf_backend_want_plt_sym 0
65 #define elf_backend_plt_alignment 3
66 #define elf_backend_plt_not_loaded 1
67 #define elf_backend_got_symbol_offset 0
68 #define elf_backend_got_header_size 8
69 #define elf_backend_can_gc_sections 1
70 #define elf_backend_can_refcount 1
71 #define elf_backend_rela_normal 1
72
73 #define bfd_elf64_mkobject ppc64_elf_mkobject
74 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
75 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
76 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
77 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
78 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
79
80 #define elf_backend_object_p ppc64_elf_object_p
81 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
82 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
83 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
84 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
85 #define elf_backend_check_relocs ppc64_elf_check_relocs
86 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
87 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
88 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
89 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
90 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
91 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
92 #define elf_backend_relocate_section ppc64_elf_relocate_section
93 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
94 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
95 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
96 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
97 #define elf_backend_special_sections ppc64_elf_special_sections
98
99 /* The name of the dynamic interpreter. This is put in the .interp
100 section. */
101 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
102
103 /* The size in bytes of an entry in the procedure linkage table. */
104 #define PLT_ENTRY_SIZE 24
105
106 /* The initial size of the plt reserved for the dynamic linker. */
107 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
108
109 /* TOC base pointers offset from start of TOC. */
110 #define TOC_BASE_OFF 0x8000
111
112 /* Offset of tp and dtp pointers from start of TLS block. */
113 #define TP_OFFSET 0x7000
114 #define DTP_OFFSET 0x8000
115
116 /* .plt call stub instructions. The normal stub is like this, but
117 sometimes the .plt entry crosses a 64k boundary and we need to
118 insert an addis to adjust r12. */
119 #define PLT_CALL_STUB_SIZE (7*4)
120 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
121 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
122 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
123 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
124 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
125 /* ld %r11,xxx+16@l(%r12) */
126 #define BCTR 0x4e800420 /* bctr */
127
128
129 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
130 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
131
132 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
133
134 /* glink call stub instructions. We enter with the index in R0, and the
135 address of glink entry in CTR. From that, we can calculate PLT0. */
136 #define GLINK_CALL_STUB_SIZE (16*4)
137 #define MFCTR_R12 0x7d8902a6 /* mfctr %r12 */
138 #define SLDI_R11_R0_3 0x780b1f24 /* sldi %r11,%r0,3 */
139 #define ADDIC_R2_R0_32K 0x34408000 /* addic. %r2,%r0,-32768 */
140 #define SUB_R12_R12_R11 0x7d8b6050 /* sub %r12,%r12,%r11 */
141 #define SRADI_R2_R2_63 0x7c42fe76 /* sradi %r2,%r2,63 */
142 #define SLDI_R11_R0_2 0x780b1764 /* sldi %r11,%r0,2 */
143 #define AND_R2_R2_R11 0x7c425838 /* and %r2,%r2,%r11 */
144 /* sub %r12,%r12,%r11 */
145 #define ADD_R12_R12_R2 0x7d8c1214 /* add %r12,%r12,%r2 */
146 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
147 /* ld %r11,xxx@l(%r12) */
148 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,xxx@l */
149 /* ld %r2,8(%r12) */
150 /* mtctr %r11 */
151 /* ld %r11,16(%r12) */
152 /* bctr */
153
154 /* Pad with this. */
155 #define NOP 0x60000000
156
157 /* Some other nops. */
158 #define CROR_151515 0x4def7b82
159 #define CROR_313131 0x4ffffb82
160
161 /* .glink entries for the first 32k functions are two instructions. */
162 #define LI_R0_0 0x38000000 /* li %r0,0 */
163 #define B_DOT 0x48000000 /* b . */
164
165 /* After that, we need two instructions to load the index, followed by
166 a branch. */
167 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
168 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
169
170 /* Instructions to save and restore floating point regs. */
171 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
172 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
173 #define BLR 0x4e800020 /* blr */
174
175 /* Since .opd is an array of descriptors and each entry will end up
176 with identical R_PPC64_RELATIVE relocs, there is really no need to
177 propagate .opd relocs; The dynamic linker should be taught to
178 relocate .opd without reloc entries. */
179 #ifndef NO_OPD_RELOCS
180 #define NO_OPD_RELOCS 0
181 #endif
182 \f
183 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
184
185 /* Relocation HOWTO's. */
186 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
187
188 static reloc_howto_type ppc64_elf_howto_raw[] = {
189 /* This reloc does nothing. */
190 HOWTO (R_PPC64_NONE, /* type */
191 0, /* rightshift */
192 2, /* size (0 = byte, 1 = short, 2 = long) */
193 32, /* bitsize */
194 FALSE, /* pc_relative */
195 0, /* bitpos */
196 complain_overflow_dont, /* complain_on_overflow */
197 bfd_elf_generic_reloc, /* special_function */
198 "R_PPC64_NONE", /* name */
199 FALSE, /* partial_inplace */
200 0, /* src_mask */
201 0, /* dst_mask */
202 FALSE), /* pcrel_offset */
203
204 /* A standard 32 bit relocation. */
205 HOWTO (R_PPC64_ADDR32, /* type */
206 0, /* rightshift */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
208 32, /* bitsize */
209 FALSE, /* pc_relative */
210 0, /* bitpos */
211 complain_overflow_bitfield, /* complain_on_overflow */
212 bfd_elf_generic_reloc, /* special_function */
213 "R_PPC64_ADDR32", /* name */
214 FALSE, /* partial_inplace */
215 0, /* src_mask */
216 0xffffffff, /* dst_mask */
217 FALSE), /* pcrel_offset */
218
219 /* An absolute 26 bit branch; the lower two bits must be zero.
220 FIXME: we don't check that, we just clear them. */
221 HOWTO (R_PPC64_ADDR24, /* type */
222 0, /* rightshift */
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 26, /* 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_ADDR24", /* name */
230 FALSE, /* partial_inplace */
231 0, /* src_mask */
232 0x03fffffc, /* dst_mask */
233 FALSE), /* pcrel_offset */
234
235 /* A standard 16 bit relocation. */
236 HOWTO (R_PPC64_ADDR16, /* type */
237 0, /* rightshift */
238 1, /* size (0 = byte, 1 = short, 2 = long) */
239 16, /* bitsize */
240 FALSE, /* pc_relative */
241 0, /* bitpos */
242 complain_overflow_bitfield, /* complain_on_overflow */
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_ADDR16", /* name */
245 FALSE, /* partial_inplace */
246 0, /* src_mask */
247 0xffff, /* dst_mask */
248 FALSE), /* pcrel_offset */
249
250 /* A 16 bit relocation without overflow. */
251 HOWTO (R_PPC64_ADDR16_LO, /* type */
252 0, /* rightshift */
253 1, /* size (0 = byte, 1 = short, 2 = long) */
254 16, /* bitsize */
255 FALSE, /* pc_relative */
256 0, /* bitpos */
257 complain_overflow_dont,/* complain_on_overflow */
258 bfd_elf_generic_reloc, /* special_function */
259 "R_PPC64_ADDR16_LO", /* name */
260 FALSE, /* partial_inplace */
261 0, /* src_mask */
262 0xffff, /* dst_mask */
263 FALSE), /* pcrel_offset */
264
265 /* Bits 16-31 of an address. */
266 HOWTO (R_PPC64_ADDR16_HI, /* type */
267 16, /* rightshift */
268 1, /* size (0 = byte, 1 = short, 2 = long) */
269 16, /* bitsize */
270 FALSE, /* pc_relative */
271 0, /* bitpos */
272 complain_overflow_dont, /* complain_on_overflow */
273 bfd_elf_generic_reloc, /* special_function */
274 "R_PPC64_ADDR16_HI", /* name */
275 FALSE, /* partial_inplace */
276 0, /* src_mask */
277 0xffff, /* dst_mask */
278 FALSE), /* pcrel_offset */
279
280 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
281 bits, treated as a signed number, is negative. */
282 HOWTO (R_PPC64_ADDR16_HA, /* 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 ppc64_elf_ha_reloc, /* special_function */
290 "R_PPC64_ADDR16_HA", /* name */
291 FALSE, /* partial_inplace */
292 0, /* src_mask */
293 0xffff, /* dst_mask */
294 FALSE), /* pcrel_offset */
295
296 /* An absolute 16 bit branch; the lower two bits must be zero.
297 FIXME: we don't check that, we just clear them. */
298 HOWTO (R_PPC64_ADDR14, /* type */
299 0, /* rightshift */
300 2, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
302 FALSE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_bitfield, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC64_ADDR14", /* name */
307 FALSE, /* partial_inplace */
308 0, /* src_mask */
309 0x0000fffc, /* dst_mask */
310 FALSE), /* pcrel_offset */
311
312 /* An absolute 16 bit branch, for which bit 10 should be set to
313 indicate that the branch is expected to be taken. The lower two
314 bits must be zero. */
315 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
316 0, /* rightshift */
317 2, /* size (0 = byte, 1 = short, 2 = long) */
318 16, /* bitsize */
319 FALSE, /* pc_relative */
320 0, /* bitpos */
321 complain_overflow_bitfield, /* complain_on_overflow */
322 ppc64_elf_brtaken_reloc, /* special_function */
323 "R_PPC64_ADDR14_BRTAKEN",/* name */
324 FALSE, /* partial_inplace */
325 0, /* src_mask */
326 0x0000fffc, /* dst_mask */
327 FALSE), /* pcrel_offset */
328
329 /* An absolute 16 bit branch, for which bit 10 should be set to
330 indicate that the branch is not expected to be taken. The lower
331 two bits must be zero. */
332 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
333 0, /* rightshift */
334 2, /* size (0 = byte, 1 = short, 2 = long) */
335 16, /* bitsize */
336 FALSE, /* pc_relative */
337 0, /* bitpos */
338 complain_overflow_bitfield, /* complain_on_overflow */
339 ppc64_elf_brtaken_reloc, /* special_function */
340 "R_PPC64_ADDR14_BRNTAKEN",/* name */
341 FALSE, /* partial_inplace */
342 0, /* src_mask */
343 0x0000fffc, /* dst_mask */
344 FALSE), /* pcrel_offset */
345
346 /* A relative 26 bit branch; the lower two bits must be zero. */
347 HOWTO (R_PPC64_REL24, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 26, /* bitsize */
351 TRUE, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_signed, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* special_function */
355 "R_PPC64_REL24", /* name */
356 FALSE, /* partial_inplace */
357 0, /* src_mask */
358 0x03fffffc, /* dst_mask */
359 TRUE), /* pcrel_offset */
360
361 /* A relative 16 bit branch; the lower two bits must be zero. */
362 HOWTO (R_PPC64_REL14, /* type */
363 0, /* rightshift */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
365 16, /* bitsize */
366 TRUE, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_signed, /* complain_on_overflow */
369 bfd_elf_generic_reloc, /* special_function */
370 "R_PPC64_REL14", /* name */
371 FALSE, /* partial_inplace */
372 0, /* src_mask */
373 0x0000fffc, /* dst_mask */
374 TRUE), /* pcrel_offset */
375
376 /* A relative 16 bit branch. Bit 10 should be set to indicate that
377 the branch is expected to be taken. The lower two bits must be
378 zero. */
379 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 16, /* bitsize */
383 TRUE, /* pc_relative */
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
386 ppc64_elf_brtaken_reloc, /* special_function */
387 "R_PPC64_REL14_BRTAKEN", /* name */
388 FALSE, /* partial_inplace */
389 0, /* src_mask */
390 0x0000fffc, /* dst_mask */
391 TRUE), /* pcrel_offset */
392
393 /* A relative 16 bit branch. Bit 10 should be set to indicate that
394 the branch is not expected to be taken. The lower two bits must
395 be zero. */
396 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
397 0, /* rightshift */
398 2, /* size (0 = byte, 1 = short, 2 = long) */
399 16, /* bitsize */
400 TRUE, /* pc_relative */
401 0, /* bitpos */
402 complain_overflow_signed, /* complain_on_overflow */
403 ppc64_elf_brtaken_reloc, /* special_function */
404 "R_PPC64_REL14_BRNTAKEN",/* name */
405 FALSE, /* partial_inplace */
406 0, /* src_mask */
407 0x0000fffc, /* dst_mask */
408 TRUE), /* pcrel_offset */
409
410 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
411 symbol. */
412 HOWTO (R_PPC64_GOT16, /* type */
413 0, /* rightshift */
414 1, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 FALSE, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_signed, /* complain_on_overflow */
419 ppc64_elf_unhandled_reloc, /* special_function */
420 "R_PPC64_GOT16", /* name */
421 FALSE, /* partial_inplace */
422 0, /* src_mask */
423 0xffff, /* dst_mask */
424 FALSE), /* pcrel_offset */
425
426 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
427 the symbol. */
428 HOWTO (R_PPC64_GOT16_LO, /* 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_dont, /* complain_on_overflow */
435 ppc64_elf_unhandled_reloc, /* special_function */
436 "R_PPC64_GOT16_LO", /* name */
437 FALSE, /* partial_inplace */
438 0, /* src_mask */
439 0xffff, /* dst_mask */
440 FALSE), /* pcrel_offset */
441
442 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
443 the symbol. */
444 HOWTO (R_PPC64_GOT16_HI, /* type */
445 16, /* 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_HI", /* name */
453 FALSE, /* partial_inplace */
454 0, /* src_mask */
455 0xffff, /* dst_mask */
456 FALSE), /* pcrel_offset */
457
458 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
459 the symbol. */
460 HOWTO (R_PPC64_GOT16_HA, /* 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_HA", /* name */
469 FALSE, /* partial_inplace */
470 0, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
473
474 /* This is used only by the dynamic linker. The symbol should exist
475 both in the object being run and in some shared library. The
476 dynamic linker copies the data addressed by the symbol from the
477 shared library into the object, because the object being
478 run has to have the data at some particular address. */
479 HOWTO (R_PPC64_COPY, /* type */
480 0, /* rightshift */
481 0, /* this one is variable size */
482 0, /* bitsize */
483 FALSE, /* pc_relative */
484 0, /* bitpos */
485 complain_overflow_dont, /* complain_on_overflow */
486 ppc64_elf_unhandled_reloc, /* special_function */
487 "R_PPC64_COPY", /* name */
488 FALSE, /* partial_inplace */
489 0, /* src_mask */
490 0, /* dst_mask */
491 FALSE), /* pcrel_offset */
492
493 /* Like R_PPC64_ADDR64, but used when setting global offset table
494 entries. */
495 HOWTO (R_PPC64_GLOB_DAT, /* type */
496 0, /* rightshift */
497 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
498 64, /* 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_GLOB_DAT", /* name */
504 FALSE, /* partial_inplace */
505 0, /* src_mask */
506 ONES (64), /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Created by the link editor. Marks a procedure linkage table
510 entry for a symbol. */
511 HOWTO (R_PPC64_JMP_SLOT, /* type */
512 0, /* rightshift */
513 0, /* size (0 = byte, 1 = short, 2 = long) */
514 0, /* 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_JMP_SLOT", /* name */
520 FALSE, /* partial_inplace */
521 0, /* src_mask */
522 0, /* dst_mask */
523 FALSE), /* pcrel_offset */
524
525 /* Used only by the dynamic linker. When the object is run, this
526 doubleword64 is set to the load address of the object, plus the
527 addend. */
528 HOWTO (R_PPC64_RELATIVE, /* type */
529 0, /* rightshift */
530 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
531 64, /* bitsize */
532 FALSE, /* pc_relative */
533 0, /* bitpos */
534 complain_overflow_dont, /* complain_on_overflow */
535 bfd_elf_generic_reloc, /* special_function */
536 "R_PPC64_RELATIVE", /* name */
537 FALSE, /* partial_inplace */
538 0, /* src_mask */
539 ONES (64), /* dst_mask */
540 FALSE), /* pcrel_offset */
541
542 /* Like R_PPC64_ADDR32, but may be unaligned. */
543 HOWTO (R_PPC64_UADDR32, /* type */
544 0, /* rightshift */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
546 32, /* bitsize */
547 FALSE, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_bitfield, /* complain_on_overflow */
550 bfd_elf_generic_reloc, /* special_function */
551 "R_PPC64_UADDR32", /* name */
552 FALSE, /* partial_inplace */
553 0, /* src_mask */
554 0xffffffff, /* dst_mask */
555 FALSE), /* pcrel_offset */
556
557 /* Like R_PPC64_ADDR16, but may be unaligned. */
558 HOWTO (R_PPC64_UADDR16, /* type */
559 0, /* rightshift */
560 1, /* size (0 = byte, 1 = short, 2 = long) */
561 16, /* bitsize */
562 FALSE, /* pc_relative */
563 0, /* bitpos */
564 complain_overflow_bitfield, /* complain_on_overflow */
565 bfd_elf_generic_reloc, /* special_function */
566 "R_PPC64_UADDR16", /* name */
567 FALSE, /* partial_inplace */
568 0, /* src_mask */
569 0xffff, /* dst_mask */
570 FALSE), /* pcrel_offset */
571
572 /* 32-bit PC relative. */
573 HOWTO (R_PPC64_REL32, /* type */
574 0, /* rightshift */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
576 32, /* bitsize */
577 TRUE, /* pc_relative */
578 0, /* bitpos */
579 /* FIXME: Verify. Was complain_overflow_bitfield. */
580 complain_overflow_signed, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_REL32", /* name */
583 FALSE, /* partial_inplace */
584 0, /* src_mask */
585 0xffffffff, /* dst_mask */
586 TRUE), /* pcrel_offset */
587
588 /* 32-bit relocation to the symbol's procedure linkage table. */
589 HOWTO (R_PPC64_PLT32, /* type */
590 0, /* rightshift */
591 2, /* size (0 = byte, 1 = short, 2 = long) */
592 32, /* bitsize */
593 FALSE, /* pc_relative */
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 ppc64_elf_unhandled_reloc, /* special_function */
597 "R_PPC64_PLT32", /* name */
598 FALSE, /* partial_inplace */
599 0, /* src_mask */
600 0xffffffff, /* dst_mask */
601 FALSE), /* pcrel_offset */
602
603 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
604 FIXME: R_PPC64_PLTREL32 not supported. */
605 HOWTO (R_PPC64_PLTREL32, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 32, /* bitsize */
609 TRUE, /* pc_relative */
610 0, /* bitpos */
611 complain_overflow_signed, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_PPC64_PLTREL32", /* name */
614 FALSE, /* partial_inplace */
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
617 TRUE), /* pcrel_offset */
618
619 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
620 the symbol. */
621 HOWTO (R_PPC64_PLT16_LO, /* type */
622 0, /* rightshift */
623 1, /* size (0 = byte, 1 = short, 2 = long) */
624 16, /* bitsize */
625 FALSE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_dont, /* complain_on_overflow */
628 ppc64_elf_unhandled_reloc, /* special_function */
629 "R_PPC64_PLT16_LO", /* name */
630 FALSE, /* partial_inplace */
631 0, /* src_mask */
632 0xffff, /* dst_mask */
633 FALSE), /* pcrel_offset */
634
635 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
636 the symbol. */
637 HOWTO (R_PPC64_PLT16_HI, /* type */
638 16, /* 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_HI", /* name */
646 FALSE, /* partial_inplace */
647 0, /* src_mask */
648 0xffff, /* dst_mask */
649 FALSE), /* pcrel_offset */
650
651 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
652 the symbol. */
653 HOWTO (R_PPC64_PLT16_HA, /* 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_HA", /* name */
662 FALSE, /* partial_inplace */
663 0, /* src_mask */
664 0xffff, /* dst_mask */
665 FALSE), /* pcrel_offset */
666
667 /* 16-bit section relative relocation. */
668 HOWTO (R_PPC64_SECTOFF, /* type */
669 0, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
672 FALSE, /* pc_relative */
673 0, /* bitpos */
674 complain_overflow_bitfield, /* complain_on_overflow */
675 ppc64_elf_sectoff_reloc, /* special_function */
676 "R_PPC64_SECTOFF", /* name */
677 FALSE, /* partial_inplace */
678 0, /* src_mask */
679 0xffff, /* dst_mask */
680 FALSE), /* pcrel_offset */
681
682 /* Like R_PPC64_SECTOFF, but no overflow warning. */
683 HOWTO (R_PPC64_SECTOFF_LO, /* type */
684 0, /* rightshift */
685 1, /* size (0 = byte, 1 = short, 2 = long) */
686 16, /* bitsize */
687 FALSE, /* pc_relative */
688 0, /* bitpos */
689 complain_overflow_dont, /* complain_on_overflow */
690 ppc64_elf_sectoff_reloc, /* special_function */
691 "R_PPC64_SECTOFF_LO", /* name */
692 FALSE, /* partial_inplace */
693 0, /* src_mask */
694 0xffff, /* dst_mask */
695 FALSE), /* pcrel_offset */
696
697 /* 16-bit upper half section relative relocation. */
698 HOWTO (R_PPC64_SECTOFF_HI, /* type */
699 16, /* rightshift */
700 1, /* size (0 = byte, 1 = short, 2 = long) */
701 16, /* bitsize */
702 FALSE, /* pc_relative */
703 0, /* bitpos */
704 complain_overflow_dont, /* complain_on_overflow */
705 ppc64_elf_sectoff_reloc, /* special_function */
706 "R_PPC64_SECTOFF_HI", /* name */
707 FALSE, /* partial_inplace */
708 0, /* src_mask */
709 0xffff, /* dst_mask */
710 FALSE), /* pcrel_offset */
711
712 /* 16-bit upper half adjusted section relative relocation. */
713 HOWTO (R_PPC64_SECTOFF_HA, /* type */
714 16, /* rightshift */
715 1, /* size (0 = byte, 1 = short, 2 = long) */
716 16, /* bitsize */
717 FALSE, /* pc_relative */
718 0, /* bitpos */
719 complain_overflow_dont, /* complain_on_overflow */
720 ppc64_elf_sectoff_ha_reloc, /* special_function */
721 "R_PPC64_SECTOFF_HA", /* name */
722 FALSE, /* partial_inplace */
723 0, /* src_mask */
724 0xffff, /* dst_mask */
725 FALSE), /* pcrel_offset */
726
727 /* Like R_PPC64_REL24 without touching the two least significant bits. */
728 HOWTO (R_PPC64_REL30, /* type */
729 2, /* rightshift */
730 2, /* size (0 = byte, 1 = short, 2 = long) */
731 30, /* bitsize */
732 TRUE, /* pc_relative */
733 0, /* bitpos */
734 complain_overflow_dont, /* complain_on_overflow */
735 bfd_elf_generic_reloc, /* special_function */
736 "R_PPC64_REL30", /* name */
737 FALSE, /* partial_inplace */
738 0, /* src_mask */
739 0xfffffffc, /* dst_mask */
740 TRUE), /* pcrel_offset */
741
742 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
743
744 /* A standard 64-bit relocation. */
745 HOWTO (R_PPC64_ADDR64, /* type */
746 0, /* rightshift */
747 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
748 64, /* bitsize */
749 FALSE, /* pc_relative */
750 0, /* bitpos */
751 complain_overflow_dont, /* complain_on_overflow */
752 bfd_elf_generic_reloc, /* special_function */
753 "R_PPC64_ADDR64", /* name */
754 FALSE, /* partial_inplace */
755 0, /* src_mask */
756 ONES (64), /* dst_mask */
757 FALSE), /* pcrel_offset */
758
759 /* The bits 32-47 of an address. */
760 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
761 32, /* rightshift */
762 1, /* size (0 = byte, 1 = short, 2 = long) */
763 16, /* bitsize */
764 FALSE, /* pc_relative */
765 0, /* bitpos */
766 complain_overflow_dont, /* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
768 "R_PPC64_ADDR16_HIGHER", /* name */
769 FALSE, /* partial_inplace */
770 0, /* src_mask */
771 0xffff, /* dst_mask */
772 FALSE), /* pcrel_offset */
773
774 /* The bits 32-47 of an address, plus 1 if the contents of the low
775 16 bits, treated as a signed number, is negative. */
776 HOWTO (R_PPC64_ADDR16_HIGHERA, /* 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 ppc64_elf_ha_reloc, /* special_function */
784 "R_PPC64_ADDR16_HIGHERA", /* name */
785 FALSE, /* partial_inplace */
786 0, /* src_mask */
787 0xffff, /* dst_mask */
788 FALSE), /* pcrel_offset */
789
790 /* The bits 48-63 of an address. */
791 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
792 48, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
795 FALSE, /* pc_relative */
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR16_HIGHEST", /* name */
800 FALSE, /* partial_inplace */
801 0, /* src_mask */
802 0xffff, /* dst_mask */
803 FALSE), /* pcrel_offset */
804
805 /* The bits 48-63 of an address, plus 1 if the contents of the low
806 16 bits, treated as a signed number, is negative. */
807 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* 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 ppc64_elf_ha_reloc, /* special_function */
815 "R_PPC64_ADDR16_HIGHESTA", /* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0xffff, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* Like ADDR64, but may be unaligned. */
822 HOWTO (R_PPC64_UADDR64, /* type */
823 0, /* rightshift */
824 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
825 64, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC64_UADDR64", /* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 ONES (64), /* dst_mask */
834 FALSE), /* pcrel_offset */
835
836 /* 64-bit relative relocation. */
837 HOWTO (R_PPC64_REL64, /* type */
838 0, /* rightshift */
839 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
840 64, /* bitsize */
841 TRUE, /* pc_relative */
842 0, /* bitpos */
843 complain_overflow_dont, /* complain_on_overflow */
844 bfd_elf_generic_reloc, /* special_function */
845 "R_PPC64_REL64", /* name */
846 FALSE, /* partial_inplace */
847 0, /* src_mask */
848 ONES (64), /* dst_mask */
849 TRUE), /* pcrel_offset */
850
851 /* 64-bit relocation to the symbol's procedure linkage table. */
852 HOWTO (R_PPC64_PLT64, /* type */
853 0, /* rightshift */
854 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
855 64, /* bitsize */
856 FALSE, /* pc_relative */
857 0, /* bitpos */
858 complain_overflow_dont, /* complain_on_overflow */
859 ppc64_elf_unhandled_reloc, /* special_function */
860 "R_PPC64_PLT64", /* name */
861 FALSE, /* partial_inplace */
862 0, /* src_mask */
863 ONES (64), /* dst_mask */
864 FALSE), /* pcrel_offset */
865
866 /* 64-bit PC relative relocation to the symbol's procedure linkage
867 table. */
868 /* FIXME: R_PPC64_PLTREL64 not supported. */
869 HOWTO (R_PPC64_PLTREL64, /* type */
870 0, /* rightshift */
871 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
872 64, /* bitsize */
873 TRUE, /* pc_relative */
874 0, /* bitpos */
875 complain_overflow_dont, /* complain_on_overflow */
876 ppc64_elf_unhandled_reloc, /* special_function */
877 "R_PPC64_PLTREL64", /* name */
878 FALSE, /* partial_inplace */
879 0, /* src_mask */
880 ONES (64), /* dst_mask */
881 TRUE), /* pcrel_offset */
882
883 /* 16 bit TOC-relative relocation. */
884
885 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
886 HOWTO (R_PPC64_TOC16, /* type */
887 0, /* rightshift */
888 1, /* size (0 = byte, 1 = short, 2 = long) */
889 16, /* bitsize */
890 FALSE, /* pc_relative */
891 0, /* bitpos */
892 complain_overflow_signed, /* complain_on_overflow */
893 ppc64_elf_toc_reloc, /* special_function */
894 "R_PPC64_TOC16", /* name */
895 FALSE, /* partial_inplace */
896 0, /* src_mask */
897 0xffff, /* dst_mask */
898 FALSE), /* pcrel_offset */
899
900 /* 16 bit TOC-relative relocation without overflow. */
901
902 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
903 HOWTO (R_PPC64_TOC16_LO, /* type */
904 0, /* rightshift */
905 1, /* size (0 = byte, 1 = short, 2 = long) */
906 16, /* bitsize */
907 FALSE, /* pc_relative */
908 0, /* bitpos */
909 complain_overflow_dont, /* complain_on_overflow */
910 ppc64_elf_toc_reloc, /* special_function */
911 "R_PPC64_TOC16_LO", /* name */
912 FALSE, /* partial_inplace */
913 0, /* src_mask */
914 0xffff, /* dst_mask */
915 FALSE), /* pcrel_offset */
916
917 /* 16 bit TOC-relative relocation, high 16 bits. */
918
919 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
920 HOWTO (R_PPC64_TOC16_HI, /* type */
921 16, /* rightshift */
922 1, /* size (0 = byte, 1 = short, 2 = long) */
923 16, /* bitsize */
924 FALSE, /* pc_relative */
925 0, /* bitpos */
926 complain_overflow_dont, /* complain_on_overflow */
927 ppc64_elf_toc_reloc, /* special_function */
928 "R_PPC64_TOC16_HI", /* name */
929 FALSE, /* partial_inplace */
930 0, /* src_mask */
931 0xffff, /* dst_mask */
932 FALSE), /* pcrel_offset */
933
934 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
935 contents of the low 16 bits, treated as a signed number, is
936 negative. */
937
938 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
939 HOWTO (R_PPC64_TOC16_HA, /* type */
940 16, /* rightshift */
941 1, /* size (0 = byte, 1 = short, 2 = long) */
942 16, /* bitsize */
943 FALSE, /* pc_relative */
944 0, /* bitpos */
945 complain_overflow_dont, /* complain_on_overflow */
946 ppc64_elf_toc_ha_reloc, /* special_function */
947 "R_PPC64_TOC16_HA", /* name */
948 FALSE, /* partial_inplace */
949 0, /* src_mask */
950 0xffff, /* dst_mask */
951 FALSE), /* pcrel_offset */
952
953 /* 64-bit relocation; insert value of TOC base (.TOC.). */
954
955 /* R_PPC64_TOC 51 doubleword64 .TOC. */
956 HOWTO (R_PPC64_TOC, /* type */
957 0, /* rightshift */
958 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
959 64, /* bitsize */
960 FALSE, /* pc_relative */
961 0, /* bitpos */
962 complain_overflow_bitfield, /* complain_on_overflow */
963 ppc64_elf_toc64_reloc, /* special_function */
964 "R_PPC64_TOC", /* name */
965 FALSE, /* partial_inplace */
966 0, /* src_mask */
967 ONES (64), /* dst_mask */
968 FALSE), /* pcrel_offset */
969
970 /* Like R_PPC64_GOT16, but also informs the link editor that the
971 value to relocate may (!) refer to a PLT entry which the link
972 editor (a) may replace with the symbol value. If the link editor
973 is unable to fully resolve the symbol, it may (b) create a PLT
974 entry and store the address to the new PLT entry in the GOT.
975 This permits lazy resolution of function symbols at run time.
976 The link editor may also skip all of this and just (c) emit a
977 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
978 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
979 HOWTO (R_PPC64_PLTGOT16, /* type */
980 0, /* rightshift */
981 1, /* size (0 = byte, 1 = short, 2 = long) */
982 16, /* bitsize */
983 FALSE, /* pc_relative */
984 0, /* bitpos */
985 complain_overflow_signed, /* complain_on_overflow */
986 ppc64_elf_unhandled_reloc, /* special_function */
987 "R_PPC64_PLTGOT16", /* name */
988 FALSE, /* partial_inplace */
989 0, /* src_mask */
990 0xffff, /* dst_mask */
991 FALSE), /* pcrel_offset */
992
993 /* Like R_PPC64_PLTGOT16, but without overflow. */
994 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
995 HOWTO (R_PPC64_PLTGOT16_LO, /* 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_dont, /* complain_on_overflow */
1002 ppc64_elf_unhandled_reloc, /* special_function */
1003 "R_PPC64_PLTGOT16_LO", /* name */
1004 FALSE, /* partial_inplace */
1005 0, /* src_mask */
1006 0xffff, /* dst_mask */
1007 FALSE), /* pcrel_offset */
1008
1009 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1010 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1011 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1012 16, /* 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_HI", /* 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, plus
1026 1 if the contents of the low 16 bits, treated as a signed number,
1027 is negative. */
1028 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1029 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1030 16, /* rightshift */
1031 1, /* size (0 = byte, 1 = short, 2 = long) */
1032 16, /* bitsize */
1033 FALSE, /* pc_relative */
1034 0, /* bitpos */
1035 complain_overflow_dont,/* complain_on_overflow */
1036 ppc64_elf_unhandled_reloc, /* special_function */
1037 "R_PPC64_PLTGOT16_HA", /* name */
1038 FALSE, /* partial_inplace */
1039 0, /* src_mask */
1040 0xffff, /* dst_mask */
1041 FALSE), /* pcrel_offset */
1042
1043 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1044 HOWTO (R_PPC64_ADDR16_DS, /* type */
1045 0, /* rightshift */
1046 1, /* size (0 = byte, 1 = short, 2 = long) */
1047 16, /* bitsize */
1048 FALSE, /* pc_relative */
1049 0, /* bitpos */
1050 complain_overflow_bitfield, /* complain_on_overflow */
1051 bfd_elf_generic_reloc, /* special_function */
1052 "R_PPC64_ADDR16_DS", /* name */
1053 FALSE, /* partial_inplace */
1054 0, /* src_mask */
1055 0xfffc, /* dst_mask */
1056 FALSE), /* pcrel_offset */
1057
1058 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1059 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1060 0, /* rightshift */
1061 1, /* size (0 = byte, 1 = short, 2 = long) */
1062 16, /* bitsize */
1063 FALSE, /* pc_relative */
1064 0, /* bitpos */
1065 complain_overflow_dont,/* complain_on_overflow */
1066 bfd_elf_generic_reloc, /* special_function */
1067 "R_PPC64_ADDR16_LO_DS",/* name */
1068 FALSE, /* partial_inplace */
1069 0, /* src_mask */
1070 0xfffc, /* dst_mask */
1071 FALSE), /* pcrel_offset */
1072
1073 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1074 HOWTO (R_PPC64_GOT16_DS, /* type */
1075 0, /* rightshift */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1077 16, /* bitsize */
1078 FALSE, /* pc_relative */
1079 0, /* bitpos */
1080 complain_overflow_signed, /* complain_on_overflow */
1081 ppc64_elf_unhandled_reloc, /* special_function */
1082 "R_PPC64_GOT16_DS", /* name */
1083 FALSE, /* partial_inplace */
1084 0, /* src_mask */
1085 0xfffc, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1087
1088 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1089 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1090 0, /* rightshift */
1091 1, /* size (0 = byte, 1 = short, 2 = long) */
1092 16, /* bitsize */
1093 FALSE, /* pc_relative */
1094 0, /* bitpos */
1095 complain_overflow_dont, /* complain_on_overflow */
1096 ppc64_elf_unhandled_reloc, /* special_function */
1097 "R_PPC64_GOT16_LO_DS", /* name */
1098 FALSE, /* partial_inplace */
1099 0, /* src_mask */
1100 0xfffc, /* dst_mask */
1101 FALSE), /* pcrel_offset */
1102
1103 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1104 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1105 0, /* rightshift */
1106 1, /* size (0 = byte, 1 = short, 2 = long) */
1107 16, /* bitsize */
1108 FALSE, /* pc_relative */
1109 0, /* bitpos */
1110 complain_overflow_dont, /* complain_on_overflow */
1111 ppc64_elf_unhandled_reloc, /* special_function */
1112 "R_PPC64_PLT16_LO_DS", /* name */
1113 FALSE, /* partial_inplace */
1114 0, /* src_mask */
1115 0xfffc, /* dst_mask */
1116 FALSE), /* pcrel_offset */
1117
1118 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1119 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1120 0, /* rightshift */
1121 1, /* size (0 = byte, 1 = short, 2 = long) */
1122 16, /* bitsize */
1123 FALSE, /* pc_relative */
1124 0, /* bitpos */
1125 complain_overflow_bitfield, /* complain_on_overflow */
1126 ppc64_elf_sectoff_reloc, /* special_function */
1127 "R_PPC64_SECTOFF_DS", /* name */
1128 FALSE, /* partial_inplace */
1129 0, /* src_mask */
1130 0xfffc, /* dst_mask */
1131 FALSE), /* pcrel_offset */
1132
1133 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1134 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1135 0, /* rightshift */
1136 1, /* size (0 = byte, 1 = short, 2 = long) */
1137 16, /* bitsize */
1138 FALSE, /* pc_relative */
1139 0, /* bitpos */
1140 complain_overflow_dont, /* complain_on_overflow */
1141 ppc64_elf_sectoff_reloc, /* special_function */
1142 "R_PPC64_SECTOFF_LO_DS",/* name */
1143 FALSE, /* partial_inplace */
1144 0, /* src_mask */
1145 0xfffc, /* dst_mask */
1146 FALSE), /* pcrel_offset */
1147
1148 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1149 HOWTO (R_PPC64_TOC16_DS, /* type */
1150 0, /* rightshift */
1151 1, /* size (0 = byte, 1 = short, 2 = long) */
1152 16, /* bitsize */
1153 FALSE, /* pc_relative */
1154 0, /* bitpos */
1155 complain_overflow_signed, /* complain_on_overflow */
1156 ppc64_elf_toc_reloc, /* special_function */
1157 "R_PPC64_TOC16_DS", /* name */
1158 FALSE, /* partial_inplace */
1159 0, /* src_mask */
1160 0xfffc, /* dst_mask */
1161 FALSE), /* pcrel_offset */
1162
1163 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1164 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1165 0, /* rightshift */
1166 1, /* size (0 = byte, 1 = short, 2 = long) */
1167 16, /* bitsize */
1168 FALSE, /* pc_relative */
1169 0, /* bitpos */
1170 complain_overflow_dont, /* complain_on_overflow */
1171 ppc64_elf_toc_reloc, /* special_function */
1172 "R_PPC64_TOC16_LO_DS", /* name */
1173 FALSE, /* partial_inplace */
1174 0, /* src_mask */
1175 0xfffc, /* dst_mask */
1176 FALSE), /* pcrel_offset */
1177
1178 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1179 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1180 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_signed, /* complain_on_overflow */
1187 ppc64_elf_unhandled_reloc, /* special_function */
1188 "R_PPC64_PLTGOT16_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1195 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1196 HOWTO (R_PPC64_PLTGOT16_LO_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_dont, /* complain_on_overflow */
1203 ppc64_elf_unhandled_reloc, /* special_function */
1204 "R_PPC64_PLTGOT16_LO_DS",/* name */
1205 FALSE, /* partial_inplace */
1206 0, /* src_mask */
1207 0xfffc, /* dst_mask */
1208 FALSE), /* pcrel_offset */
1209
1210 /* Marker reloc for TLS. */
1211 HOWTO (R_PPC64_TLS,
1212 0, /* rightshift */
1213 2, /* size (0 = byte, 1 = short, 2 = long) */
1214 32, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_dont, /* complain_on_overflow */
1218 bfd_elf_generic_reloc, /* special_function */
1219 "R_PPC64_TLS", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1224
1225 /* Computes the load module index of the load module that contains the
1226 definition of its TLS sym. */
1227 HOWTO (R_PPC64_DTPMOD64,
1228 0, /* rightshift */
1229 4, /* size (0 = byte, 1 = short, 2 = long) */
1230 64, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc64_elf_unhandled_reloc, /* special_function */
1235 "R_PPC64_DTPMOD64", /* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 ONES (64), /* dst_mask */
1239 FALSE), /* pcrel_offset */
1240
1241 /* Computes a dtv-relative displacement, the difference between the value
1242 of sym+add and the base address of the thread-local storage block that
1243 contains the definition of sym, minus 0x8000. */
1244 HOWTO (R_PPC64_DTPREL64,
1245 0, /* rightshift */
1246 4, /* size (0 = byte, 1 = short, 2 = long) */
1247 64, /* bitsize */
1248 FALSE, /* pc_relative */
1249 0, /* bitpos */
1250 complain_overflow_dont, /* complain_on_overflow */
1251 ppc64_elf_unhandled_reloc, /* special_function */
1252 "R_PPC64_DTPREL64", /* name */
1253 FALSE, /* partial_inplace */
1254 0, /* src_mask */
1255 ONES (64), /* dst_mask */
1256 FALSE), /* pcrel_offset */
1257
1258 /* A 16 bit dtprel reloc. */
1259 HOWTO (R_PPC64_DTPREL16,
1260 0, /* rightshift */
1261 1, /* size (0 = byte, 1 = short, 2 = long) */
1262 16, /* bitsize */
1263 FALSE, /* pc_relative */
1264 0, /* bitpos */
1265 complain_overflow_signed, /* complain_on_overflow */
1266 ppc64_elf_unhandled_reloc, /* special_function */
1267 "R_PPC64_DTPREL16", /* name */
1268 FALSE, /* partial_inplace */
1269 0, /* src_mask */
1270 0xffff, /* dst_mask */
1271 FALSE), /* pcrel_offset */
1272
1273 /* Like DTPREL16, but no overflow. */
1274 HOWTO (R_PPC64_DTPREL16_LO,
1275 0, /* rightshift */
1276 1, /* size (0 = byte, 1 = short, 2 = long) */
1277 16, /* bitsize */
1278 FALSE, /* pc_relative */
1279 0, /* bitpos */
1280 complain_overflow_dont, /* complain_on_overflow */
1281 ppc64_elf_unhandled_reloc, /* special_function */
1282 "R_PPC64_DTPREL16_LO", /* name */
1283 FALSE, /* partial_inplace */
1284 0, /* src_mask */
1285 0xffff, /* dst_mask */
1286 FALSE), /* pcrel_offset */
1287
1288 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1289 HOWTO (R_PPC64_DTPREL16_HI,
1290 16, /* rightshift */
1291 1, /* size (0 = byte, 1 = short, 2 = long) */
1292 16, /* bitsize */
1293 FALSE, /* pc_relative */
1294 0, /* bitpos */
1295 complain_overflow_dont, /* complain_on_overflow */
1296 ppc64_elf_unhandled_reloc, /* special_function */
1297 "R_PPC64_DTPREL16_HI", /* name */
1298 FALSE, /* partial_inplace */
1299 0, /* src_mask */
1300 0xffff, /* dst_mask */
1301 FALSE), /* pcrel_offset */
1302
1303 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1304 HOWTO (R_PPC64_DTPREL16_HA,
1305 16, /* rightshift */
1306 1, /* size (0 = byte, 1 = short, 2 = long) */
1307 16, /* bitsize */
1308 FALSE, /* pc_relative */
1309 0, /* bitpos */
1310 complain_overflow_dont, /* complain_on_overflow */
1311 ppc64_elf_unhandled_reloc, /* special_function */
1312 "R_PPC64_DTPREL16_HA", /* name */
1313 FALSE, /* partial_inplace */
1314 0, /* src_mask */
1315 0xffff, /* dst_mask */
1316 FALSE), /* pcrel_offset */
1317
1318 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1319 HOWTO (R_PPC64_DTPREL16_HIGHER,
1320 32, /* rightshift */
1321 1, /* size (0 = byte, 1 = short, 2 = long) */
1322 16, /* bitsize */
1323 FALSE, /* pc_relative */
1324 0, /* bitpos */
1325 complain_overflow_dont, /* complain_on_overflow */
1326 ppc64_elf_unhandled_reloc, /* special_function */
1327 "R_PPC64_DTPREL16_HIGHER", /* name */
1328 FALSE, /* partial_inplace */
1329 0, /* src_mask */
1330 0xffff, /* dst_mask */
1331 FALSE), /* pcrel_offset */
1332
1333 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1334 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1335 32, /* rightshift */
1336 1, /* size (0 = byte, 1 = short, 2 = long) */
1337 16, /* bitsize */
1338 FALSE, /* pc_relative */
1339 0, /* bitpos */
1340 complain_overflow_dont, /* complain_on_overflow */
1341 ppc64_elf_unhandled_reloc, /* special_function */
1342 "R_PPC64_DTPREL16_HIGHERA", /* name */
1343 FALSE, /* partial_inplace */
1344 0, /* src_mask */
1345 0xffff, /* dst_mask */
1346 FALSE), /* pcrel_offset */
1347
1348 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1349 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1350 48, /* rightshift */
1351 1, /* size (0 = byte, 1 = short, 2 = long) */
1352 16, /* bitsize */
1353 FALSE, /* pc_relative */
1354 0, /* bitpos */
1355 complain_overflow_dont, /* complain_on_overflow */
1356 ppc64_elf_unhandled_reloc, /* special_function */
1357 "R_PPC64_DTPREL16_HIGHEST", /* name */
1358 FALSE, /* partial_inplace */
1359 0, /* src_mask */
1360 0xffff, /* dst_mask */
1361 FALSE), /* pcrel_offset */
1362
1363 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1364 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1365 48, /* rightshift */
1366 1, /* size (0 = byte, 1 = short, 2 = long) */
1367 16, /* bitsize */
1368 FALSE, /* pc_relative */
1369 0, /* bitpos */
1370 complain_overflow_dont, /* complain_on_overflow */
1371 ppc64_elf_unhandled_reloc, /* special_function */
1372 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1373 FALSE, /* partial_inplace */
1374 0, /* src_mask */
1375 0xffff, /* dst_mask */
1376 FALSE), /* pcrel_offset */
1377
1378 /* Like DTPREL16, but for insns with a DS field. */
1379 HOWTO (R_PPC64_DTPREL16_DS,
1380 0, /* rightshift */
1381 1, /* size (0 = byte, 1 = short, 2 = long) */
1382 16, /* bitsize */
1383 FALSE, /* pc_relative */
1384 0, /* bitpos */
1385 complain_overflow_signed, /* complain_on_overflow */
1386 ppc64_elf_unhandled_reloc, /* special_function */
1387 "R_PPC64_DTPREL16_DS", /* name */
1388 FALSE, /* partial_inplace */
1389 0, /* src_mask */
1390 0xfffc, /* dst_mask */
1391 FALSE), /* pcrel_offset */
1392
1393 /* Like DTPREL16_DS, but no overflow. */
1394 HOWTO (R_PPC64_DTPREL16_LO_DS,
1395 0, /* rightshift */
1396 1, /* size (0 = byte, 1 = short, 2 = long) */
1397 16, /* bitsize */
1398 FALSE, /* pc_relative */
1399 0, /* bitpos */
1400 complain_overflow_dont, /* complain_on_overflow */
1401 ppc64_elf_unhandled_reloc, /* special_function */
1402 "R_PPC64_DTPREL16_LO_DS", /* name */
1403 FALSE, /* partial_inplace */
1404 0, /* src_mask */
1405 0xfffc, /* dst_mask */
1406 FALSE), /* pcrel_offset */
1407
1408 /* Computes a tp-relative displacement, the difference between the value of
1409 sym+add and the value of the thread pointer (r13). */
1410 HOWTO (R_PPC64_TPREL64,
1411 0, /* rightshift */
1412 4, /* size (0 = byte, 1 = short, 2 = long) */
1413 64, /* 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_TPREL64", /* name */
1419 FALSE, /* partial_inplace */
1420 0, /* src_mask */
1421 ONES (64), /* dst_mask */
1422 FALSE), /* pcrel_offset */
1423
1424 /* A 16 bit tprel reloc. */
1425 HOWTO (R_PPC64_TPREL16,
1426 0, /* rightshift */
1427 1, /* size (0 = byte, 1 = short, 2 = long) */
1428 16, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
1431 complain_overflow_signed, /* complain_on_overflow */
1432 ppc64_elf_unhandled_reloc, /* special_function */
1433 "R_PPC64_TPREL16", /* name */
1434 FALSE, /* partial_inplace */
1435 0, /* src_mask */
1436 0xffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438
1439 /* Like TPREL16, but no overflow. */
1440 HOWTO (R_PPC64_TPREL16_LO,
1441 0, /* rightshift */
1442 1, /* size (0 = byte, 1 = short, 2 = long) */
1443 16, /* bitsize */
1444 FALSE, /* pc_relative */
1445 0, /* bitpos */
1446 complain_overflow_dont, /* complain_on_overflow */
1447 ppc64_elf_unhandled_reloc, /* special_function */
1448 "R_PPC64_TPREL16_LO", /* name */
1449 FALSE, /* partial_inplace */
1450 0, /* src_mask */
1451 0xffff, /* dst_mask */
1452 FALSE), /* pcrel_offset */
1453
1454 /* Like TPREL16_LO, but next higher group of 16 bits. */
1455 HOWTO (R_PPC64_TPREL16_HI,
1456 16, /* rightshift */
1457 1, /* size (0 = byte, 1 = short, 2 = long) */
1458 16, /* bitsize */
1459 FALSE, /* pc_relative */
1460 0, /* bitpos */
1461 complain_overflow_dont, /* complain_on_overflow */
1462 ppc64_elf_unhandled_reloc, /* special_function */
1463 "R_PPC64_TPREL16_HI", /* name */
1464 FALSE, /* partial_inplace */
1465 0, /* src_mask */
1466 0xffff, /* dst_mask */
1467 FALSE), /* pcrel_offset */
1468
1469 /* Like TPREL16_HI, but adjust for low 16 bits. */
1470 HOWTO (R_PPC64_TPREL16_HA,
1471 16, /* rightshift */
1472 1, /* size (0 = byte, 1 = short, 2 = long) */
1473 16, /* bitsize */
1474 FALSE, /* pc_relative */
1475 0, /* bitpos */
1476 complain_overflow_dont, /* complain_on_overflow */
1477 ppc64_elf_unhandled_reloc, /* special_function */
1478 "R_PPC64_TPREL16_HA", /* name */
1479 FALSE, /* partial_inplace */
1480 0, /* src_mask */
1481 0xffff, /* dst_mask */
1482 FALSE), /* pcrel_offset */
1483
1484 /* Like TPREL16_HI, but next higher group of 16 bits. */
1485 HOWTO (R_PPC64_TPREL16_HIGHER,
1486 32, /* rightshift */
1487 1, /* size (0 = byte, 1 = short, 2 = long) */
1488 16, /* bitsize */
1489 FALSE, /* pc_relative */
1490 0, /* bitpos */
1491 complain_overflow_dont, /* complain_on_overflow */
1492 ppc64_elf_unhandled_reloc, /* special_function */
1493 "R_PPC64_TPREL16_HIGHER", /* name */
1494 FALSE, /* partial_inplace */
1495 0, /* src_mask */
1496 0xffff, /* dst_mask */
1497 FALSE), /* pcrel_offset */
1498
1499 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1500 HOWTO (R_PPC64_TPREL16_HIGHERA,
1501 32, /* rightshift */
1502 1, /* size (0 = byte, 1 = short, 2 = long) */
1503 16, /* bitsize */
1504 FALSE, /* pc_relative */
1505 0, /* bitpos */
1506 complain_overflow_dont, /* complain_on_overflow */
1507 ppc64_elf_unhandled_reloc, /* special_function */
1508 "R_PPC64_TPREL16_HIGHERA", /* name */
1509 FALSE, /* partial_inplace */
1510 0, /* src_mask */
1511 0xffff, /* dst_mask */
1512 FALSE), /* pcrel_offset */
1513
1514 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1515 HOWTO (R_PPC64_TPREL16_HIGHEST,
1516 48, /* rightshift */
1517 1, /* size (0 = byte, 1 = short, 2 = long) */
1518 16, /* bitsize */
1519 FALSE, /* pc_relative */
1520 0, /* bitpos */
1521 complain_overflow_dont, /* complain_on_overflow */
1522 ppc64_elf_unhandled_reloc, /* special_function */
1523 "R_PPC64_TPREL16_HIGHEST", /* name */
1524 FALSE, /* partial_inplace */
1525 0, /* src_mask */
1526 0xffff, /* dst_mask */
1527 FALSE), /* pcrel_offset */
1528
1529 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1530 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1531 48, /* rightshift */
1532 1, /* size (0 = byte, 1 = short, 2 = long) */
1533 16, /* bitsize */
1534 FALSE, /* pc_relative */
1535 0, /* bitpos */
1536 complain_overflow_dont, /* complain_on_overflow */
1537 ppc64_elf_unhandled_reloc, /* special_function */
1538 "R_PPC64_TPREL16_HIGHESTA", /* name */
1539 FALSE, /* partial_inplace */
1540 0, /* src_mask */
1541 0xffff, /* dst_mask */
1542 FALSE), /* pcrel_offset */
1543
1544 /* Like TPREL16, but for insns with a DS field. */
1545 HOWTO (R_PPC64_TPREL16_DS,
1546 0, /* rightshift */
1547 1, /* size (0 = byte, 1 = short, 2 = long) */
1548 16, /* bitsize */
1549 FALSE, /* pc_relative */
1550 0, /* bitpos */
1551 complain_overflow_signed, /* complain_on_overflow */
1552 ppc64_elf_unhandled_reloc, /* special_function */
1553 "R_PPC64_TPREL16_DS", /* name */
1554 FALSE, /* partial_inplace */
1555 0, /* src_mask */
1556 0xfffc, /* dst_mask */
1557 FALSE), /* pcrel_offset */
1558
1559 /* Like TPREL16_DS, but no overflow. */
1560 HOWTO (R_PPC64_TPREL16_LO_DS,
1561 0, /* rightshift */
1562 1, /* size (0 = byte, 1 = short, 2 = long) */
1563 16, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont, /* complain_on_overflow */
1567 ppc64_elf_unhandled_reloc, /* special_function */
1568 "R_PPC64_TPREL16_LO_DS", /* name */
1569 FALSE, /* partial_inplace */
1570 0, /* src_mask */
1571 0xfffc, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573
1574 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1575 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1576 to the first entry relative to the TOC base (r2). */
1577 HOWTO (R_PPC64_GOT_TLSGD16,
1578 0, /* rightshift */
1579 1, /* size (0 = byte, 1 = short, 2 = long) */
1580 16, /* bitsize */
1581 FALSE, /* pc_relative */
1582 0, /* bitpos */
1583 complain_overflow_signed, /* complain_on_overflow */
1584 ppc64_elf_unhandled_reloc, /* special_function */
1585 "R_PPC64_GOT_TLSGD16", /* name */
1586 FALSE, /* partial_inplace */
1587 0, /* src_mask */
1588 0xffff, /* dst_mask */
1589 FALSE), /* pcrel_offset */
1590
1591 /* Like GOT_TLSGD16, but no overflow. */
1592 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1593 0, /* rightshift */
1594 1, /* size (0 = byte, 1 = short, 2 = long) */
1595 16, /* bitsize */
1596 FALSE, /* pc_relative */
1597 0, /* bitpos */
1598 complain_overflow_dont, /* complain_on_overflow */
1599 ppc64_elf_unhandled_reloc, /* special_function */
1600 "R_PPC64_GOT_TLSGD16_LO", /* name */
1601 FALSE, /* partial_inplace */
1602 0, /* src_mask */
1603 0xffff, /* dst_mask */
1604 FALSE), /* pcrel_offset */
1605
1606 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1607 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1608 16, /* rightshift */
1609 1, /* size (0 = byte, 1 = short, 2 = long) */
1610 16, /* bitsize */
1611 FALSE, /* pc_relative */
1612 0, /* bitpos */
1613 complain_overflow_dont, /* complain_on_overflow */
1614 ppc64_elf_unhandled_reloc, /* special_function */
1615 "R_PPC64_GOT_TLSGD16_HI", /* name */
1616 FALSE, /* partial_inplace */
1617 0, /* src_mask */
1618 0xffff, /* dst_mask */
1619 FALSE), /* pcrel_offset */
1620
1621 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1622 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1623 16, /* rightshift */
1624 1, /* size (0 = byte, 1 = short, 2 = long) */
1625 16, /* bitsize */
1626 FALSE, /* pc_relative */
1627 0, /* bitpos */
1628 complain_overflow_dont, /* complain_on_overflow */
1629 ppc64_elf_unhandled_reloc, /* special_function */
1630 "R_PPC64_GOT_TLSGD16_HA", /* name */
1631 FALSE, /* partial_inplace */
1632 0, /* src_mask */
1633 0xffff, /* dst_mask */
1634 FALSE), /* pcrel_offset */
1635
1636 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1637 with values (sym+add)@dtpmod and zero, and computes the offset to the
1638 first entry relative to the TOC base (r2). */
1639 HOWTO (R_PPC64_GOT_TLSLD16,
1640 0, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1642 16, /* bitsize */
1643 FALSE, /* pc_relative */
1644 0, /* bitpos */
1645 complain_overflow_signed, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_GOT_TLSLD16", /* name */
1648 FALSE, /* partial_inplace */
1649 0, /* src_mask */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1652
1653 /* Like GOT_TLSLD16, but no overflow. */
1654 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1655 0, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 16, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_dont, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_GOT_TLSLD16_LO", /* name */
1663 FALSE, /* partial_inplace */
1664 0, /* src_mask */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667
1668 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1669 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1670 16, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1672 16, /* bitsize */
1673 FALSE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_dont, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_GOT_TLSLD16_HI", /* name */
1678 FALSE, /* partial_inplace */
1679 0, /* src_mask */
1680 0xffff, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1682
1683 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1684 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1685 16, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1687 16, /* bitsize */
1688 FALSE, /* pc_relative */
1689 0, /* bitpos */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_GOT_TLSLD16_HA", /* name */
1693 FALSE, /* partial_inplace */
1694 0, /* src_mask */
1695 0xffff, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1697
1698 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1699 the offset to the entry relative to the TOC base (r2). */
1700 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1701 0, /* rightshift */
1702 1, /* size (0 = byte, 1 = short, 2 = long) */
1703 16, /* bitsize */
1704 FALSE, /* pc_relative */
1705 0, /* bitpos */
1706 complain_overflow_signed, /* complain_on_overflow */
1707 ppc64_elf_unhandled_reloc, /* special_function */
1708 "R_PPC64_GOT_DTPREL16_DS", /* name */
1709 FALSE, /* partial_inplace */
1710 0, /* src_mask */
1711 0xfffc, /* dst_mask */
1712 FALSE), /* pcrel_offset */
1713
1714 /* Like GOT_DTPREL16_DS, but no overflow. */
1715 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1716 0, /* rightshift */
1717 1, /* size (0 = byte, 1 = short, 2 = long) */
1718 16, /* bitsize */
1719 FALSE, /* pc_relative */
1720 0, /* bitpos */
1721 complain_overflow_dont, /* complain_on_overflow */
1722 ppc64_elf_unhandled_reloc, /* special_function */
1723 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1724 FALSE, /* partial_inplace */
1725 0, /* src_mask */
1726 0xfffc, /* dst_mask */
1727 FALSE), /* pcrel_offset */
1728
1729 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1730 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1731 16, /* rightshift */
1732 1, /* size (0 = byte, 1 = short, 2 = long) */
1733 16, /* bitsize */
1734 FALSE, /* pc_relative */
1735 0, /* bitpos */
1736 complain_overflow_dont, /* complain_on_overflow */
1737 ppc64_elf_unhandled_reloc, /* special_function */
1738 "R_PPC64_GOT_DTPREL16_HI", /* name */
1739 FALSE, /* partial_inplace */
1740 0, /* src_mask */
1741 0xffff, /* dst_mask */
1742 FALSE), /* pcrel_offset */
1743
1744 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1745 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1746 16, /* rightshift */
1747 1, /* size (0 = byte, 1 = short, 2 = long) */
1748 16, /* bitsize */
1749 FALSE, /* pc_relative */
1750 0, /* bitpos */
1751 complain_overflow_dont, /* complain_on_overflow */
1752 ppc64_elf_unhandled_reloc, /* special_function */
1753 "R_PPC64_GOT_DTPREL16_HA", /* name */
1754 FALSE, /* partial_inplace */
1755 0, /* src_mask */
1756 0xffff, /* dst_mask */
1757 FALSE), /* pcrel_offset */
1758
1759 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1760 offset to the entry relative to the TOC base (r2). */
1761 HOWTO (R_PPC64_GOT_TPREL16_DS,
1762 0, /* rightshift */
1763 1, /* size (0 = byte, 1 = short, 2 = long) */
1764 16, /* bitsize */
1765 FALSE, /* pc_relative */
1766 0, /* bitpos */
1767 complain_overflow_signed, /* complain_on_overflow */
1768 ppc64_elf_unhandled_reloc, /* special_function */
1769 "R_PPC64_GOT_TPREL16_DS", /* name */
1770 FALSE, /* partial_inplace */
1771 0, /* src_mask */
1772 0xfffc, /* dst_mask */
1773 FALSE), /* pcrel_offset */
1774
1775 /* Like GOT_TPREL16_DS, but no overflow. */
1776 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1777 0, /* rightshift */
1778 1, /* size (0 = byte, 1 = short, 2 = long) */
1779 16, /* bitsize */
1780 FALSE, /* pc_relative */
1781 0, /* bitpos */
1782 complain_overflow_dont, /* complain_on_overflow */
1783 ppc64_elf_unhandled_reloc, /* special_function */
1784 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1785 FALSE, /* partial_inplace */
1786 0, /* src_mask */
1787 0xfffc, /* dst_mask */
1788 FALSE), /* pcrel_offset */
1789
1790 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1791 HOWTO (R_PPC64_GOT_TPREL16_HI,
1792 16, /* rightshift */
1793 1, /* size (0 = byte, 1 = short, 2 = long) */
1794 16, /* bitsize */
1795 FALSE, /* pc_relative */
1796 0, /* bitpos */
1797 complain_overflow_dont, /* complain_on_overflow */
1798 ppc64_elf_unhandled_reloc, /* special_function */
1799 "R_PPC64_GOT_TPREL16_HI", /* name */
1800 FALSE, /* partial_inplace */
1801 0, /* src_mask */
1802 0xffff, /* dst_mask */
1803 FALSE), /* pcrel_offset */
1804
1805 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1806 HOWTO (R_PPC64_GOT_TPREL16_HA,
1807 16, /* rightshift */
1808 1, /* size (0 = byte, 1 = short, 2 = long) */
1809 16, /* bitsize */
1810 FALSE, /* pc_relative */
1811 0, /* bitpos */
1812 complain_overflow_dont, /* complain_on_overflow */
1813 ppc64_elf_unhandled_reloc, /* special_function */
1814 "R_PPC64_GOT_TPREL16_HA", /* name */
1815 FALSE, /* partial_inplace */
1816 0, /* src_mask */
1817 0xffff, /* dst_mask */
1818 FALSE), /* pcrel_offset */
1819
1820 /* GNU extension to record C++ vtable hierarchy. */
1821 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1822 0, /* rightshift */
1823 0, /* size (0 = byte, 1 = short, 2 = long) */
1824 0, /* bitsize */
1825 FALSE, /* pc_relative */
1826 0, /* bitpos */
1827 complain_overflow_dont, /* complain_on_overflow */
1828 NULL, /* special_function */
1829 "R_PPC64_GNU_VTINHERIT", /* name */
1830 FALSE, /* partial_inplace */
1831 0, /* src_mask */
1832 0, /* dst_mask */
1833 FALSE), /* pcrel_offset */
1834
1835 /* GNU extension to record C++ vtable member usage. */
1836 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1837 0, /* rightshift */
1838 0, /* size (0 = byte, 1 = short, 2 = long) */
1839 0, /* bitsize */
1840 FALSE, /* pc_relative */
1841 0, /* bitpos */
1842 complain_overflow_dont, /* complain_on_overflow */
1843 NULL, /* special_function */
1844 "R_PPC64_GNU_VTENTRY", /* name */
1845 FALSE, /* partial_inplace */
1846 0, /* src_mask */
1847 0, /* dst_mask */
1848 FALSE), /* pcrel_offset */
1849 };
1850
1851 \f
1852 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1853 be done. */
1854
1855 static void
1856 ppc_howto_init (void)
1857 {
1858 unsigned int i, type;
1859
1860 for (i = 0;
1861 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1862 i++)
1863 {
1864 type = ppc64_elf_howto_raw[i].type;
1865 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1866 / sizeof (ppc64_elf_howto_table[0])));
1867 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1868 }
1869 }
1870
1871 static reloc_howto_type *
1872 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1873 bfd_reloc_code_real_type code)
1874 {
1875 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1876
1877 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1878 /* Initialize howto table if needed. */
1879 ppc_howto_init ();
1880
1881 switch (code)
1882 {
1883 default:
1884 return NULL;
1885
1886 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1887 break;
1888 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1889 break;
1890 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1891 break;
1892 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1893 break;
1894 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1895 break;
1896 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1897 break;
1898 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1899 break;
1900 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1901 break;
1902 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1903 break;
1904 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1905 break;
1906 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1907 break;
1908 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1909 break;
1910 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1911 break;
1912 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1913 break;
1914 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1915 break;
1916 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1917 break;
1918 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1919 break;
1920 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1921 break;
1922 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1923 break;
1924 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1925 break;
1926 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1927 break;
1928 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1929 break;
1930 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1931 break;
1932 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1933 break;
1934 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1935 break;
1936 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1937 break;
1938 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1939 break;
1940 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1941 break;
1942 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1943 break;
1944 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1945 break;
1946 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1947 break;
1948 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1949 break;
1950 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1951 break;
1952 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1953 break;
1954 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1955 break;
1956 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1957 break;
1958 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1959 break;
1960 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1961 break;
1962 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1963 break;
1964 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1965 break;
1966 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1967 break;
1968 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1969 break;
1970 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1971 break;
1972 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1973 break;
1974 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1975 break;
1976 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1977 break;
1978 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1979 break;
1980 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1981 break;
1982 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1983 break;
1984 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1985 break;
1986 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1987 break;
1988 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1989 break;
1990 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1991 break;
1992 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1993 break;
1994 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1995 break;
1996 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1997 break;
1998 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1999 break;
2000 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2001 break;
2002 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2003 break;
2004 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2005 break;
2006 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2007 break;
2008 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2009 break;
2010 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2011 break;
2012 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2013 break;
2014 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2015 break;
2016 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2017 break;
2018 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2019 break;
2020 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2021 break;
2022 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2023 break;
2024 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2025 break;
2026 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2027 break;
2028 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2029 break;
2030 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2031 break;
2032 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2033 break;
2034 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2035 break;
2036 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2037 break;
2038 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2039 break;
2040 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2041 break;
2042 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2043 break;
2044 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2045 break;
2046 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2047 break;
2048 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2049 break;
2050 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2051 break;
2052 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2053 break;
2054 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2055 break;
2056 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2057 break;
2058 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2059 break;
2060 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2061 break;
2062 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2063 break;
2064 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2065 break;
2066 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2067 break;
2068 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2069 break;
2070 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2071 break;
2072 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2073 break;
2074 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2075 break;
2076 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2077 break;
2078 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2079 break;
2080 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2081 break;
2082 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2083 break;
2084 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2085 break;
2086 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2087 break;
2088 }
2089
2090 return ppc64_elf_howto_table[r];
2091 };
2092
2093 /* Set the howto pointer for a PowerPC ELF reloc. */
2094
2095 static void
2096 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2097 Elf_Internal_Rela *dst)
2098 {
2099 unsigned int type;
2100
2101 /* Initialize howto table if needed. */
2102 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2103 ppc_howto_init ();
2104
2105 type = ELF64_R_TYPE (dst->r_info);
2106 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2107 / sizeof (ppc64_elf_howto_table[0])));
2108 cache_ptr->howto = ppc64_elf_howto_table[type];
2109 }
2110
2111 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2112
2113 static bfd_reloc_status_type
2114 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2115 void *data, asection *input_section,
2116 bfd *output_bfd, char **error_message)
2117 {
2118 /* If this is a relocatable link (output_bfd test tells us), just
2119 call the generic function. Any adjustment will be done at final
2120 link time. */
2121 if (output_bfd != NULL)
2122 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2123 input_section, output_bfd, error_message);
2124
2125 /* Adjust the addend for sign extension of the low 16 bits.
2126 We won't actually be using the low 16 bits, so trashing them
2127 doesn't matter. */
2128 reloc_entry->addend += 0x8000;
2129 return bfd_reloc_continue;
2130 }
2131
2132 static bfd_reloc_status_type
2133 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2134 void *data, asection *input_section,
2135 bfd *output_bfd, char **error_message)
2136 {
2137 long insn;
2138 enum elf_ppc64_reloc_type r_type;
2139 bfd_size_type octets;
2140 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2141 bfd_boolean is_power4 = FALSE;
2142
2143 /* If this is a relocatable link (output_bfd test tells us), just
2144 call the generic function. Any adjustment will be done at final
2145 link time. */
2146 if (output_bfd != NULL)
2147 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2148 input_section, output_bfd, error_message);
2149
2150 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2151 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2152 insn &= ~(0x01 << 21);
2153 r_type = reloc_entry->howto->type;
2154 if (r_type == R_PPC64_ADDR14_BRTAKEN
2155 || r_type == R_PPC64_REL14_BRTAKEN)
2156 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2157
2158 if (is_power4)
2159 {
2160 /* Set 'a' bit. This is 0b00010 in BO field for branch
2161 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2162 for branch on CTR insns (BO == 1a00t or 1a01t). */
2163 if ((insn & (0x14 << 21)) == (0x04 << 21))
2164 insn |= 0x02 << 21;
2165 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2166 insn |= 0x08 << 21;
2167 else
2168 return bfd_reloc_continue;
2169 }
2170 else
2171 {
2172 bfd_vma target = 0;
2173 bfd_vma from;
2174
2175 if (!bfd_is_com_section (symbol->section))
2176 target = symbol->value;
2177 target += symbol->section->output_section->vma;
2178 target += symbol->section->output_offset;
2179 target += reloc_entry->addend;
2180
2181 from = (reloc_entry->address
2182 + input_section->output_offset
2183 + input_section->output_section->vma);
2184
2185 /* Invert 'y' bit if not the default. */
2186 if ((bfd_signed_vma) (target - from) < 0)
2187 insn ^= 0x01 << 21;
2188 }
2189 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2190 return bfd_reloc_continue;
2191 }
2192
2193 static bfd_reloc_status_type
2194 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2195 void *data, asection *input_section,
2196 bfd *output_bfd, char **error_message)
2197 {
2198 /* If this is a relocatable link (output_bfd test tells us), just
2199 call the generic function. Any adjustment will be done at final
2200 link time. */
2201 if (output_bfd != NULL)
2202 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2203 input_section, output_bfd, error_message);
2204
2205 /* Subtract the symbol section base address. */
2206 reloc_entry->addend -= symbol->section->output_section->vma;
2207 return bfd_reloc_continue;
2208 }
2209
2210 static bfd_reloc_status_type
2211 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2212 void *data, asection *input_section,
2213 bfd *output_bfd, char **error_message)
2214 {
2215 /* If this is a relocatable link (output_bfd test tells us), just
2216 call the generic function. Any adjustment will be done at final
2217 link time. */
2218 if (output_bfd != NULL)
2219 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2220 input_section, output_bfd, error_message);
2221
2222 /* Subtract the symbol section base address. */
2223 reloc_entry->addend -= symbol->section->output_section->vma;
2224
2225 /* Adjust the addend for sign extension of the low 16 bits. */
2226 reloc_entry->addend += 0x8000;
2227 return bfd_reloc_continue;
2228 }
2229
2230 static bfd_reloc_status_type
2231 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2232 void *data, asection *input_section,
2233 bfd *output_bfd, char **error_message)
2234 {
2235 bfd_vma TOCstart;
2236
2237 /* If this is a relocatable link (output_bfd test tells us), just
2238 call the generic function. Any adjustment will be done at final
2239 link time. */
2240 if (output_bfd != NULL)
2241 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2242 input_section, output_bfd, error_message);
2243
2244 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2245 if (TOCstart == 0)
2246 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2247
2248 /* Subtract the TOC base address. */
2249 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2250 return bfd_reloc_continue;
2251 }
2252
2253 static bfd_reloc_status_type
2254 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2255 void *data, asection *input_section,
2256 bfd *output_bfd, char **error_message)
2257 {
2258 bfd_vma TOCstart;
2259
2260 /* If this is a relocatable link (output_bfd test tells us), just
2261 call the generic function. Any adjustment will be done at final
2262 link time. */
2263 if (output_bfd != NULL)
2264 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2265 input_section, output_bfd, error_message);
2266
2267 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2268 if (TOCstart == 0)
2269 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2270
2271 /* Subtract the TOC base address. */
2272 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2273
2274 /* Adjust the addend for sign extension of the low 16 bits. */
2275 reloc_entry->addend += 0x8000;
2276 return bfd_reloc_continue;
2277 }
2278
2279 static bfd_reloc_status_type
2280 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2281 void *data, asection *input_section,
2282 bfd *output_bfd, char **error_message)
2283 {
2284 bfd_vma TOCstart;
2285 bfd_size_type octets;
2286
2287 /* If this is a relocatable link (output_bfd test tells us), just
2288 call the generic function. Any adjustment will be done at final
2289 link time. */
2290 if (output_bfd != NULL)
2291 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2292 input_section, output_bfd, error_message);
2293
2294 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2295 if (TOCstart == 0)
2296 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2297
2298 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2299 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2300 return bfd_reloc_ok;
2301 }
2302
2303 static bfd_reloc_status_type
2304 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2305 void *data, asection *input_section,
2306 bfd *output_bfd, char **error_message)
2307 {
2308 /* If this is a relocatable link (output_bfd test tells us), just
2309 call the generic function. Any adjustment will be done at final
2310 link time. */
2311 if (output_bfd != NULL)
2312 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2313 input_section, output_bfd, error_message);
2314
2315 if (error_message != NULL)
2316 {
2317 static char buf[60];
2318 sprintf (buf, "generic linker can't handle %s",
2319 reloc_entry->howto->name);
2320 *error_message = buf;
2321 }
2322 return bfd_reloc_dangerous;
2323 }
2324
2325 struct ppc64_elf_obj_tdata
2326 {
2327 struct elf_obj_tdata elf;
2328
2329 /* Shortcuts to dynamic linker sections. */
2330 asection *got;
2331 asection *relgot;
2332
2333 /* TLS local dynamic got entry handling. Suppose for multiple GOT
2334 sections means we potentially need one of these for each input bfd. */
2335 union {
2336 bfd_signed_vma refcount;
2337 bfd_vma offset;
2338 } tlsld_got;
2339 };
2340
2341 #define ppc64_elf_tdata(bfd) \
2342 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2343
2344 #define ppc64_tlsld_got(bfd) \
2345 (&ppc64_elf_tdata (bfd)->tlsld_got)
2346
2347 /* Override the generic function because we store some extras. */
2348
2349 static bfd_boolean
2350 ppc64_elf_mkobject (bfd *abfd)
2351 {
2352 bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2353 abfd->tdata.any = bfd_zalloc (abfd, amt);
2354 if (abfd->tdata.any == NULL)
2355 return FALSE;
2356 return TRUE;
2357 }
2358
2359 /* Fix bad default arch selected for a 64 bit input bfd when the
2360 default is 32 bit. */
2361
2362 static bfd_boolean
2363 ppc64_elf_object_p (bfd *abfd)
2364 {
2365 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2366 {
2367 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2368
2369 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2370 {
2371 /* Relies on arch after 32 bit default being 64 bit default. */
2372 abfd->arch_info = abfd->arch_info->next;
2373 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2374 }
2375 }
2376 return TRUE;
2377 }
2378
2379 /* Support for core dump NOTE sections. */
2380
2381 static bfd_boolean
2382 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2383 {
2384 size_t offset, raw_size;
2385
2386 if (note->descsz != 504)
2387 return FALSE;
2388
2389 /* pr_cursig */
2390 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2391
2392 /* pr_pid */
2393 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2394
2395 /* pr_reg */
2396 offset = 112;
2397 raw_size = 384;
2398
2399 /* Make a ".reg/999" section. */
2400 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2401 raw_size, note->descpos + offset);
2402 }
2403
2404 static bfd_boolean
2405 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2406 {
2407 if (note->descsz != 136)
2408 return FALSE;
2409
2410 elf_tdata (abfd)->core_program
2411 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2412 elf_tdata (abfd)->core_command
2413 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2414
2415 return TRUE;
2416 }
2417
2418 /* Merge backend specific data from an object file to the output
2419 object file when linking. */
2420
2421 static bfd_boolean
2422 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
2423 {
2424 /* Check if we have the same endianess. */
2425 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2426 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2427 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2428 {
2429 const char *msg;
2430
2431 if (bfd_big_endian (ibfd))
2432 msg = _("%s: compiled for a big endian system "
2433 "and target is little endian");
2434 else
2435 msg = _("%s: compiled for a little endian system "
2436 "and target is big endian");
2437
2438 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
2439
2440 bfd_set_error (bfd_error_wrong_format);
2441 return FALSE;
2442 }
2443
2444 return TRUE;
2445 }
2446
2447 /* Add extra PPC sections. */
2448
2449 static struct bfd_elf_special_section const ppc64_elf_special_sections[]=
2450 {
2451 { ".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2452 { ".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2453 { ".plt", 4, 0, SHT_NOBITS, 0 },
2454 { ".toc", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2455 { ".toc1", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2456 { ".tocbss", 7, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2457 { NULL, 0, 0, 0, 0 }
2458 };
2459
2460 struct _ppc64_elf_section_data
2461 {
2462 struct bfd_elf_section_data elf;
2463
2464 /* An array with one entry for each opd function descriptor. */
2465 union
2466 {
2467 /* Points to the function code section for local opd entries. */
2468 asection **func_sec;
2469 /* After editing .opd, adjust references to opd local syms. */
2470 long *adjust;
2471 } opd;
2472
2473 /* An array for toc sections, indexed by offset/8.
2474 Specifies the relocation symbol index used at a given toc offset. */
2475 unsigned *t_symndx;
2476 };
2477
2478 #define ppc64_elf_section_data(sec) \
2479 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2480
2481 static bfd_boolean
2482 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2483 {
2484 struct _ppc64_elf_section_data *sdata;
2485 bfd_size_type amt = sizeof (*sdata);
2486
2487 sdata = bfd_zalloc (abfd, amt);
2488 if (sdata == NULL)
2489 return FALSE;
2490 sec->used_by_bfd = sdata;
2491
2492 return _bfd_elf_new_section_hook (abfd, sec);
2493 }
2494 \f
2495 /* The following functions are specific to the ELF linker, while
2496 functions above are used generally. Those named ppc64_elf_* are
2497 called by the main ELF linker code. They appear in this file more
2498 or less in the order in which they are called. eg.
2499 ppc64_elf_check_relocs is called early in the link process,
2500 ppc64_elf_finish_dynamic_sections is one of the last functions
2501 called.
2502
2503 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2504 functions have both a function code symbol and a function descriptor
2505 symbol. A call to foo in a relocatable object file looks like:
2506
2507 . .text
2508 . x:
2509 . bl .foo
2510 . nop
2511
2512 The function definition in another object file might be:
2513
2514 . .section .opd
2515 . foo: .quad .foo
2516 . .quad .TOC.@tocbase
2517 . .quad 0
2518 .
2519 . .text
2520 . .foo: blr
2521
2522 When the linker resolves the call during a static link, the branch
2523 unsurprisingly just goes to .foo and the .opd information is unused.
2524 If the function definition is in a shared library, things are a little
2525 different: The call goes via a plt call stub, the opd information gets
2526 copied to the plt, and the linker patches the nop.
2527
2528 . x:
2529 . bl .foo_stub
2530 . ld 2,40(1)
2531 .
2532 .
2533 . .foo_stub:
2534 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
2535 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
2536 . std 2,40(1) # this is the general idea
2537 . ld 11,0(12)
2538 . ld 2,8(12)
2539 . mtctr 11
2540 . ld 11,16(12)
2541 . bctr
2542 .
2543 . .section .plt
2544 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2545
2546 The "reloc ()" notation is supposed to indicate that the linker emits
2547 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2548 copying.
2549
2550 What are the difficulties here? Well, firstly, the relocations
2551 examined by the linker in check_relocs are against the function code
2552 sym .foo, while the dynamic relocation in the plt is emitted against
2553 the function descriptor symbol, foo. Somewhere along the line, we need
2554 to carefully copy dynamic link information from one symbol to the other.
2555 Secondly, the generic part of the elf linker will make .foo a dynamic
2556 symbol as is normal for most other backends. We need foo dynamic
2557 instead, at least for an application final link. However, when
2558 creating a shared library containing foo, we need to have both symbols
2559 dynamic so that references to .foo are satisfied during the early
2560 stages of linking. Otherwise the linker might decide to pull in a
2561 definition from some other object, eg. a static library. */
2562
2563 /* The linker needs to keep track of the number of relocs that it
2564 decides to copy as dynamic relocs in check_relocs for each symbol.
2565 This is so that it can later discard them if they are found to be
2566 unnecessary. We store the information in a field extending the
2567 regular ELF linker hash table. */
2568
2569 struct ppc_dyn_relocs
2570 {
2571 struct ppc_dyn_relocs *next;
2572
2573 /* The input section of the reloc. */
2574 asection *sec;
2575
2576 /* Total number of relocs copied for the input section. */
2577 bfd_size_type count;
2578
2579 /* Number of pc-relative relocs copied for the input section. */
2580 bfd_size_type pc_count;
2581 };
2582
2583 /* Track GOT entries needed for a given symbol. We might need more
2584 than one got entry per symbol. */
2585 struct got_entry
2586 {
2587 struct got_entry *next;
2588
2589 /* The symbol addend that we'll be placing in the GOT. */
2590 bfd_vma addend;
2591
2592 /* Unlike other ELF targets, we use separate GOT entries for the same
2593 symbol referenced from different input files. This is to support
2594 automatic multiple TOC/GOT sections, where the TOC base can vary
2595 from one input file to another.
2596
2597 Point to the BFD owning this GOT entry. */
2598 bfd *owner;
2599
2600 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2601 TLS_TPREL or TLS_DTPREL for tls entries. */
2602 char tls_type;
2603
2604 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2605 union
2606 {
2607 bfd_signed_vma refcount;
2608 bfd_vma offset;
2609 } got;
2610 };
2611
2612 /* The same for PLT. */
2613 struct plt_entry
2614 {
2615 struct plt_entry *next;
2616
2617 bfd_vma addend;
2618
2619 union
2620 {
2621 bfd_signed_vma refcount;
2622 bfd_vma offset;
2623 } plt;
2624 };
2625
2626 /* Of those relocs that might be copied as dynamic relocs, this macro
2627 selects those that must be copied when linking a shared library,
2628 even when the symbol is local. */
2629
2630 #define MUST_BE_DYN_RELOC(RTYPE) \
2631 ((RTYPE) != R_PPC64_REL32 \
2632 && (RTYPE) != R_PPC64_REL64 \
2633 && (RTYPE) != R_PPC64_REL30)
2634
2635 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2636 copying dynamic variables from a shared lib into an app's dynbss
2637 section, and instead use a dynamic relocation to point into the
2638 shared lib. With code that gcc generates, it's vital that this be
2639 enabled; In the PowerPC64 ABI, the address of a function is actually
2640 the address of a function descriptor, which resides in the .opd
2641 section. gcc uses the descriptor directly rather than going via the
2642 GOT as some other ABI's do, which means that initialized function
2643 pointers must reference the descriptor. Thus, a function pointer
2644 initialized to the address of a function in a shared library will
2645 either require a copy reloc, or a dynamic reloc. Using a copy reloc
2646 redefines the function descriptor symbol to point to the copy. This
2647 presents a problem as a plt entry for that function is also
2648 initialized from the function descriptor symbol and the copy reloc
2649 may not be initialized first. */
2650 #define ELIMINATE_COPY_RELOCS 1
2651
2652 /* Section name for stubs is the associated section name plus this
2653 string. */
2654 #define STUB_SUFFIX ".stub"
2655
2656 /* Linker stubs.
2657 ppc_stub_long_branch:
2658 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2659 destination, but a 24 bit branch in a stub section will reach.
2660 . b dest
2661
2662 ppc_stub_plt_branch:
2663 Similar to the above, but a 24 bit branch in the stub section won't
2664 reach its destination.
2665 . addis %r12,%r2,xxx@toc@ha
2666 . ld %r11,xxx@toc@l(%r12)
2667 . mtctr %r11
2668 . bctr
2669
2670 ppc_stub_plt_call:
2671 Used to call a function in a shared library.
2672 . addis %r12,%r2,xxx@toc@ha
2673 . std %r2,40(%r1)
2674 . ld %r11,xxx+0@toc@l(%r12)
2675 . ld %r2,xxx+8@toc@l(%r12)
2676 . mtctr %r11
2677 . ld %r11,xxx+16@toc@l(%r12)
2678 . bctr
2679
2680 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2681 code to adjust the value and save r2 to support multiple toc sections.
2682 A ppc_stub_long_branch with an r2 offset looks like:
2683 . std %r2,40(%r1)
2684 . addis %r2,%r2,off@ha
2685 . addi %r2,%r2,off@l
2686 . b dest
2687
2688 A ppc_stub_plt_branch with an r2 offset looks like:
2689 . std %r2,40(%r1)
2690 . addis %r12,%r2,xxx@toc@ha
2691 . ld %r11,xxx@toc@l(%r12)
2692 . addis %r2,%r2,off@ha
2693 . addi %r2,%r2,off@l
2694 . mtctr %r11
2695 . bctr
2696 */
2697
2698 enum ppc_stub_type {
2699 ppc_stub_none,
2700 ppc_stub_long_branch,
2701 ppc_stub_long_branch_r2off,
2702 ppc_stub_plt_branch,
2703 ppc_stub_plt_branch_r2off,
2704 ppc_stub_plt_call
2705 };
2706
2707 struct ppc_stub_hash_entry {
2708
2709 /* Base hash table entry structure. */
2710 struct bfd_hash_entry root;
2711
2712 enum ppc_stub_type stub_type;
2713
2714 /* The stub section. */
2715 asection *stub_sec;
2716
2717 /* Offset within stub_sec of the beginning of this stub. */
2718 bfd_vma stub_offset;
2719
2720 /* Given the symbol's value and its section we can determine its final
2721 value when building the stubs (so the stub knows where to jump. */
2722 bfd_vma target_value;
2723 asection *target_section;
2724
2725 /* The symbol table entry, if any, that this was derived from. */
2726 struct ppc_link_hash_entry *h;
2727
2728 /* And the reloc addend that this was derived from. */
2729 bfd_vma addend;
2730
2731 /* Where this stub is being called from, or, in the case of combined
2732 stub sections, the first input section in the group. */
2733 asection *id_sec;
2734 };
2735
2736 struct ppc_branch_hash_entry {
2737
2738 /* Base hash table entry structure. */
2739 struct bfd_hash_entry root;
2740
2741 /* Offset within .branch_lt. */
2742 unsigned int offset;
2743
2744 /* Generation marker. */
2745 unsigned int iter;
2746 };
2747
2748 struct ppc_link_hash_entry
2749 {
2750 struct elf_link_hash_entry elf;
2751
2752 /* A pointer to the most recently used stub hash entry against this
2753 symbol. */
2754 struct ppc_stub_hash_entry *stub_cache;
2755
2756 /* Track dynamic relocs copied for this symbol. */
2757 struct ppc_dyn_relocs *dyn_relocs;
2758
2759 /* Link between function code and descriptor symbols. */
2760 struct elf_link_hash_entry *oh;
2761
2762 /* Flag function code and descriptor symbols. */
2763 unsigned int is_func:1;
2764 unsigned int is_func_descriptor:1;
2765 unsigned int is_entry:1;
2766
2767 /* Whether global opd sym has been adjusted or not. */
2768 unsigned int adjust_done:1;
2769
2770 /* Contexts in which symbol is used in the GOT (or TOC).
2771 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
2772 corresponding relocs are encountered during check_relocs.
2773 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
2774 indicate the corresponding GOT entry type is not needed.
2775 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
2776 a TPREL one. We use a separate flag rather than setting TPREL
2777 just for convenience in distinguishing the two cases. */
2778 #define TLS_GD 1 /* GD reloc. */
2779 #define TLS_LD 2 /* LD reloc. */
2780 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
2781 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
2782 #define TLS_TLS 16 /* Any TLS reloc. */
2783 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
2784 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2785 char tls_mask;
2786 };
2787
2788 /* ppc64 ELF linker hash table. */
2789
2790 struct ppc_link_hash_table
2791 {
2792 struct elf_link_hash_table elf;
2793
2794 /* The stub hash table. */
2795 struct bfd_hash_table stub_hash_table;
2796
2797 /* Another hash table for plt_branch stubs. */
2798 struct bfd_hash_table branch_hash_table;
2799
2800 /* Linker stub bfd. */
2801 bfd *stub_bfd;
2802
2803 /* Linker call-backs. */
2804 asection * (*add_stub_section) (const char *, asection *);
2805 void (*layout_sections_again) (void);
2806
2807 /* Array to keep track of which stub sections have been created, and
2808 information on stub grouping. */
2809 struct map_stub {
2810 /* This is the section to which stubs in the group will be attached. */
2811 asection *link_sec;
2812 /* The stub section. */
2813 asection *stub_sec;
2814 /* Along with elf_gp, specifies the TOC pointer used in this group. */
2815 bfd_vma toc_off;
2816 } *stub_group;
2817
2818 /* Support for multiple toc sections. */
2819 unsigned int no_multi_toc;
2820 unsigned int multi_toc_needed;
2821
2822 /* Temp used when calculating TOC pointers. */
2823 bfd_vma toc_curr;
2824
2825 /* Highest input section id. */
2826 int top_id;
2827
2828 /* Highest output section index. */
2829 int top_index;
2830
2831 /* List of input sections for each output section. */
2832 asection **input_list;
2833
2834 /* Short-cuts to get to dynamic linker sections. */
2835 asection *got;
2836 asection *plt;
2837 asection *relplt;
2838 asection *dynbss;
2839 asection *relbss;
2840 asection *glink;
2841 asection *sfpr;
2842 asection *brlt;
2843 asection *relbrlt;
2844
2845 /* Shortcut to .__tls_get_addr. */
2846 struct elf_link_hash_entry *tls_get_addr;
2847
2848 /* Statistics. */
2849 unsigned long stub_count[ppc_stub_plt_call];
2850
2851 /* Set if we should emit symbols for stubs. */
2852 unsigned int emit_stub_syms;
2853
2854 /* Set on error. */
2855 unsigned int stub_error;
2856
2857 /* Flag set when small branches are detected. Used to
2858 select suitable defaults for the stub group size. */
2859 unsigned int has_14bit_branch;
2860
2861 /* Set if we detect a reference undefined weak symbol. */
2862 unsigned int have_undefweak;
2863
2864 /* Incremented every time we size stubs. */
2865 unsigned int stub_iteration;
2866
2867 /* Small local sym to section mapping cache. */
2868 struct sym_sec_cache sym_sec;
2869 };
2870
2871 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2872
2873 #define ppc_hash_table(p) \
2874 ((struct ppc_link_hash_table *) ((p)->hash))
2875
2876 #define ppc_stub_hash_lookup(table, string, create, copy) \
2877 ((struct ppc_stub_hash_entry *) \
2878 bfd_hash_lookup ((table), (string), (create), (copy)))
2879
2880 #define ppc_branch_hash_lookup(table, string, create, copy) \
2881 ((struct ppc_branch_hash_entry *) \
2882 bfd_hash_lookup ((table), (string), (create), (copy)))
2883
2884 /* Create an entry in the stub hash table. */
2885
2886 static struct bfd_hash_entry *
2887 stub_hash_newfunc (struct bfd_hash_entry *entry,
2888 struct bfd_hash_table *table,
2889 const char *string)
2890 {
2891 /* Allocate the structure if it has not already been allocated by a
2892 subclass. */
2893 if (entry == NULL)
2894 {
2895 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2896 if (entry == NULL)
2897 return entry;
2898 }
2899
2900 /* Call the allocation method of the superclass. */
2901 entry = bfd_hash_newfunc (entry, table, string);
2902 if (entry != NULL)
2903 {
2904 struct ppc_stub_hash_entry *eh;
2905
2906 /* Initialize the local fields. */
2907 eh = (struct ppc_stub_hash_entry *) entry;
2908 eh->stub_type = ppc_stub_none;
2909 eh->stub_sec = NULL;
2910 eh->stub_offset = 0;
2911 eh->target_value = 0;
2912 eh->target_section = NULL;
2913 eh->h = NULL;
2914 eh->id_sec = NULL;
2915 }
2916
2917 return entry;
2918 }
2919
2920 /* Create an entry in the branch hash table. */
2921
2922 static struct bfd_hash_entry *
2923 branch_hash_newfunc (struct bfd_hash_entry *entry,
2924 struct bfd_hash_table *table,
2925 const char *string)
2926 {
2927 /* Allocate the structure if it has not already been allocated by a
2928 subclass. */
2929 if (entry == NULL)
2930 {
2931 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2932 if (entry == NULL)
2933 return entry;
2934 }
2935
2936 /* Call the allocation method of the superclass. */
2937 entry = bfd_hash_newfunc (entry, table, string);
2938 if (entry != NULL)
2939 {
2940 struct ppc_branch_hash_entry *eh;
2941
2942 /* Initialize the local fields. */
2943 eh = (struct ppc_branch_hash_entry *) entry;
2944 eh->offset = 0;
2945 eh->iter = 0;
2946 }
2947
2948 return entry;
2949 }
2950
2951 /* Create an entry in a ppc64 ELF linker hash table. */
2952
2953 static struct bfd_hash_entry *
2954 link_hash_newfunc (struct bfd_hash_entry *entry,
2955 struct bfd_hash_table *table,
2956 const char *string)
2957 {
2958 /* Allocate the structure if it has not already been allocated by a
2959 subclass. */
2960 if (entry == NULL)
2961 {
2962 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2963 if (entry == NULL)
2964 return entry;
2965 }
2966
2967 /* Call the allocation method of the superclass. */
2968 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2969 if (entry != NULL)
2970 {
2971 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2972
2973 eh->stub_cache = NULL;
2974 eh->dyn_relocs = NULL;
2975 eh->oh = NULL;
2976 eh->is_func = 0;
2977 eh->is_func_descriptor = 0;
2978 eh->is_entry = 0;
2979 eh->adjust_done = 0;
2980 eh->tls_mask = 0;
2981 }
2982
2983 return entry;
2984 }
2985
2986 /* Create a ppc64 ELF linker hash table. */
2987
2988 static struct bfd_link_hash_table *
2989 ppc64_elf_link_hash_table_create (bfd *abfd)
2990 {
2991 struct ppc_link_hash_table *htab;
2992 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2993
2994 htab = bfd_zmalloc (amt);
2995 if (htab == NULL)
2996 return NULL;
2997
2998 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2999 {
3000 free (htab);
3001 return NULL;
3002 }
3003
3004 /* Init the stub hash table too. */
3005 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
3006 return NULL;
3007
3008 /* And the branch hash table. */
3009 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
3010 return NULL;
3011
3012 /* Initializing two fields of the union is just cosmetic. We really
3013 only care about glist, but when compiled on a 32-bit host the
3014 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3015 debugger inspection of these fields look nicer. */
3016 htab->elf.init_refcount.refcount = 0;
3017 htab->elf.init_refcount.glist = NULL;
3018 htab->elf.init_offset.offset = 0;
3019 htab->elf.init_offset.glist = NULL;
3020
3021 return &htab->elf.root;
3022 }
3023
3024 /* Free the derived linker hash table. */
3025
3026 static void
3027 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
3028 {
3029 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3030
3031 bfd_hash_table_free (&ret->stub_hash_table);
3032 bfd_hash_table_free (&ret->branch_hash_table);
3033 _bfd_generic_link_hash_table_free (hash);
3034 }
3035
3036 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3037
3038 void
3039 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3040 {
3041 struct ppc_link_hash_table *htab;
3042
3043 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3044
3045 /* Always hook our dynamic sections into the first bfd, which is the
3046 linker created stub bfd. This ensures that the GOT header is at
3047 the start of the output TOC section. */
3048 htab = ppc_hash_table (info);
3049 htab->stub_bfd = abfd;
3050 htab->elf.dynobj = abfd;
3051 }
3052
3053 /* Build a name for an entry in the stub hash table. */
3054
3055 static char *
3056 ppc_stub_name (const asection *input_section,
3057 const asection *sym_sec,
3058 const struct ppc_link_hash_entry *h,
3059 const Elf_Internal_Rela *rel)
3060 {
3061 char *stub_name;
3062 bfd_size_type len;
3063
3064 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3065 offsets from a sym as a branch target? In fact, we could
3066 probably assume the addend is always zero. */
3067 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3068
3069 if (h)
3070 {
3071 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3072 stub_name = bfd_malloc (len);
3073 if (stub_name != NULL)
3074 {
3075 sprintf (stub_name, "%08x.%s+%x",
3076 input_section->id & 0xffffffff,
3077 h->elf.root.root.string,
3078 (int) rel->r_addend & 0xffffffff);
3079 }
3080 }
3081 else
3082 {
3083 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3084 stub_name = bfd_malloc (len);
3085 if (stub_name != NULL)
3086 {
3087 sprintf (stub_name, "%08x.%x:%x+%x",
3088 input_section->id & 0xffffffff,
3089 sym_sec->id & 0xffffffff,
3090 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3091 (int) rel->r_addend & 0xffffffff);
3092 }
3093 }
3094 return stub_name;
3095 }
3096
3097 /* Look up an entry in the stub hash. Stub entries are cached because
3098 creating the stub name takes a bit of time. */
3099
3100 static struct ppc_stub_hash_entry *
3101 ppc_get_stub_entry (const asection *input_section,
3102 const asection *sym_sec,
3103 struct elf_link_hash_entry *hash,
3104 const Elf_Internal_Rela *rel,
3105 struct ppc_link_hash_table *htab)
3106 {
3107 struct ppc_stub_hash_entry *stub_entry;
3108 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3109 const asection *id_sec;
3110
3111 /* If this input section is part of a group of sections sharing one
3112 stub section, then use the id of the first section in the group.
3113 Stub names need to include a section id, as there may well be
3114 more than one stub used to reach say, printf, and we need to
3115 distinguish between them. */
3116 id_sec = htab->stub_group[input_section->id].link_sec;
3117
3118 if (h != NULL && h->stub_cache != NULL
3119 && h->stub_cache->h == h
3120 && h->stub_cache->id_sec == id_sec)
3121 {
3122 stub_entry = h->stub_cache;
3123 }
3124 else
3125 {
3126 char *stub_name;
3127
3128 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3129 if (stub_name == NULL)
3130 return NULL;
3131
3132 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3133 stub_name, FALSE, FALSE);
3134 if (h != NULL)
3135 h->stub_cache = stub_entry;
3136
3137 free (stub_name);
3138 }
3139
3140 return stub_entry;
3141 }
3142
3143 /* Add a new stub entry to the stub hash. Not all fields of the new
3144 stub entry are initialised. */
3145
3146 static struct ppc_stub_hash_entry *
3147 ppc_add_stub (const char *stub_name,
3148 asection *section,
3149 struct ppc_link_hash_table *htab)
3150 {
3151 asection *link_sec;
3152 asection *stub_sec;
3153 struct ppc_stub_hash_entry *stub_entry;
3154
3155 link_sec = htab->stub_group[section->id].link_sec;
3156 stub_sec = htab->stub_group[section->id].stub_sec;
3157 if (stub_sec == NULL)
3158 {
3159 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3160 if (stub_sec == NULL)
3161 {
3162 size_t namelen;
3163 bfd_size_type len;
3164 char *s_name;
3165
3166 namelen = strlen (link_sec->name);
3167 len = namelen + sizeof (STUB_SUFFIX);
3168 s_name = bfd_alloc (htab->stub_bfd, len);
3169 if (s_name == NULL)
3170 return NULL;
3171
3172 memcpy (s_name, link_sec->name, namelen);
3173 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3174 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3175 if (stub_sec == NULL)
3176 return NULL;
3177 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3178 }
3179 htab->stub_group[section->id].stub_sec = stub_sec;
3180 }
3181
3182 /* Enter this entry into the linker stub hash table. */
3183 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3184 TRUE, FALSE);
3185 if (stub_entry == NULL)
3186 {
3187 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3188 bfd_archive_filename (section->owner),
3189 stub_name);
3190 return NULL;
3191 }
3192
3193 stub_entry->stub_sec = stub_sec;
3194 stub_entry->stub_offset = 0;
3195 stub_entry->id_sec = link_sec;
3196 return stub_entry;
3197 }
3198
3199 /* Create sections for linker generated code. */
3200
3201 static bfd_boolean
3202 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3203 {
3204 struct ppc_link_hash_table *htab;
3205 flagword flags;
3206
3207 htab = ppc_hash_table (info);
3208
3209 /* Create .sfpr for code to save and restore fp regs. */
3210 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3211 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3212 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
3213 if (htab->sfpr == NULL
3214 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3215 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3216 return FALSE;
3217
3218 /* Create .glink for lazy dynamic linking support. */
3219 htab->glink = bfd_make_section_anyway (dynobj, ".glink");
3220 if (htab->glink == NULL
3221 || ! bfd_set_section_flags (dynobj, htab->glink, flags)
3222 || ! bfd_set_section_alignment (dynobj, htab->glink, 2))
3223 return FALSE;
3224
3225 /* Create .branch_lt for plt_branch stubs. */
3226 flags = (SEC_ALLOC | SEC_LOAD
3227 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3228 htab->brlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3229 if (htab->brlt == NULL
3230 || ! bfd_set_section_flags (dynobj, htab->brlt, flags)
3231 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
3232 return FALSE;
3233
3234 if (info->shared)
3235 {
3236 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3237 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3238 htab->relbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
3239 if (!htab->relbrlt
3240 || ! bfd_set_section_flags (dynobj, htab->relbrlt, flags)
3241 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3242 return FALSE;
3243 }
3244 return TRUE;
3245 }
3246
3247 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3248 not already done. */
3249
3250 static bfd_boolean
3251 create_got_section (bfd *abfd, struct bfd_link_info *info)
3252 {
3253 asection *got, *relgot;
3254 flagword flags;
3255 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3256
3257 if (!htab->got)
3258 {
3259 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3260 return FALSE;
3261
3262 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3263 if (!htab->got)
3264 abort ();
3265 }
3266
3267 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3268 | SEC_LINKER_CREATED);
3269
3270 got = bfd_make_section (abfd, ".got");
3271 if (!got
3272 || !bfd_set_section_flags (abfd, got, flags)
3273 || !bfd_set_section_alignment (abfd, got, 3))
3274 return FALSE;
3275
3276 relgot = bfd_make_section (abfd, ".rela.got");
3277 if (!relgot
3278 || ! bfd_set_section_flags (abfd, relgot, flags | SEC_READONLY)
3279 || ! bfd_set_section_alignment (abfd, relgot, 3))
3280 return FALSE;
3281
3282 ppc64_elf_tdata (abfd)->got = got;
3283 ppc64_elf_tdata (abfd)->relgot = relgot;
3284 return TRUE;
3285 }
3286
3287 /* Create the dynamic sections, and set up shortcuts. */
3288
3289 static bfd_boolean
3290 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
3291 {
3292 struct ppc_link_hash_table *htab;
3293
3294 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3295 return FALSE;
3296
3297 htab = ppc_hash_table (info);
3298 if (!htab->got)
3299 htab->got = bfd_get_section_by_name (dynobj, ".got");
3300 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3301 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3302 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3303 if (!info->shared)
3304 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3305
3306 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
3307 || (!info->shared && !htab->relbss))
3308 abort ();
3309
3310 return TRUE;
3311 }
3312
3313 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3314
3315 static void
3316 ppc64_elf_copy_indirect_symbol
3317 (const struct elf_backend_data *bed ATTRIBUTE_UNUSED,
3318 struct elf_link_hash_entry *dir,
3319 struct elf_link_hash_entry *ind)
3320 {
3321 struct ppc_link_hash_entry *edir, *eind;
3322 flagword mask;
3323
3324 edir = (struct ppc_link_hash_entry *) dir;
3325 eind = (struct ppc_link_hash_entry *) ind;
3326
3327 /* Copy over any dynamic relocs we may have on the indirect sym. */
3328 if (eind->dyn_relocs != NULL)
3329 {
3330 if (edir->dyn_relocs != NULL)
3331 {
3332 struct ppc_dyn_relocs **pp;
3333 struct ppc_dyn_relocs *p;
3334
3335 if (eind->elf.root.type == bfd_link_hash_indirect)
3336 abort ();
3337
3338 /* Add reloc counts against the weak sym to the strong sym
3339 list. Merge any entries against the same section. */
3340 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3341 {
3342 struct ppc_dyn_relocs *q;
3343
3344 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3345 if (q->sec == p->sec)
3346 {
3347 q->pc_count += p->pc_count;
3348 q->count += p->count;
3349 *pp = p->next;
3350 break;
3351 }
3352 if (q == NULL)
3353 pp = &p->next;
3354 }
3355 *pp = edir->dyn_relocs;
3356 }
3357
3358 edir->dyn_relocs = eind->dyn_relocs;
3359 eind->dyn_relocs = NULL;
3360 }
3361
3362 edir->is_func |= eind->is_func;
3363 edir->is_func_descriptor |= eind->is_func_descriptor;
3364 edir->is_entry |= eind->is_entry;
3365 edir->tls_mask |= eind->tls_mask;
3366
3367 mask = (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR
3368 | ELF_LINK_HASH_REF_REGULAR_NONWEAK | ELF_LINK_NON_GOT_REF
3369 | ELF_LINK_HASH_NEEDS_PLT);
3370 /* If called to transfer flags for a weakdef during processing
3371 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
3372 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
3373 if (ELIMINATE_COPY_RELOCS
3374 && eind->elf.root.type != bfd_link_hash_indirect
3375 && (edir->elf.elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
3376 mask &= ~ELF_LINK_NON_GOT_REF;
3377
3378 edir->elf.elf_link_hash_flags |= eind->elf.elf_link_hash_flags & mask;
3379
3380 /* If we were called to copy over info for a weak sym, that's all. */
3381 if (eind->elf.root.type != bfd_link_hash_indirect)
3382 return;
3383
3384 /* Copy over got entries that we may have already seen to the
3385 symbol which just became indirect. */
3386 if (eind->elf.got.glist != NULL)
3387 {
3388 if (edir->elf.got.glist != NULL)
3389 {
3390 struct got_entry **entp;
3391 struct got_entry *ent;
3392
3393 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3394 {
3395 struct got_entry *dent;
3396
3397 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3398 if (dent->addend == ent->addend
3399 && dent->owner == ent->owner
3400 && dent->tls_type == ent->tls_type)
3401 {
3402 dent->got.refcount += ent->got.refcount;
3403 *entp = ent->next;
3404 break;
3405 }
3406 if (dent == NULL)
3407 entp = &ent->next;
3408 }
3409 *entp = edir->elf.got.glist;
3410 }
3411
3412 edir->elf.got.glist = eind->elf.got.glist;
3413 eind->elf.got.glist = NULL;
3414 }
3415
3416 /* And plt entries. */
3417 if (eind->elf.plt.plist != NULL)
3418 {
3419 if (edir->elf.plt.plist != NULL)
3420 {
3421 struct plt_entry **entp;
3422 struct plt_entry *ent;
3423
3424 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3425 {
3426 struct plt_entry *dent;
3427
3428 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3429 if (dent->addend == ent->addend)
3430 {
3431 dent->plt.refcount += ent->plt.refcount;
3432 *entp = ent->next;
3433 break;
3434 }
3435 if (dent == NULL)
3436 entp = &ent->next;
3437 }
3438 *entp = edir->elf.plt.plist;
3439 }
3440
3441 edir->elf.plt.plist = eind->elf.plt.plist;
3442 eind->elf.plt.plist = NULL;
3443 }
3444
3445 if (edir->elf.dynindx == -1)
3446 {
3447 edir->elf.dynindx = eind->elf.dynindx;
3448 edir->elf.dynstr_index = eind->elf.dynstr_index;
3449 eind->elf.dynindx = -1;
3450 eind->elf.dynstr_index = 0;
3451 }
3452 else
3453 BFD_ASSERT (eind->elf.dynindx == -1);
3454 }
3455
3456 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3457 symbols undefined on the command-line. */
3458
3459 bfd_boolean
3460 ppc64_elf_mark_entry_syms (struct bfd_link_info *info)
3461 {
3462 struct ppc_link_hash_table *htab;
3463 struct bfd_sym_chain *sym;
3464
3465 htab = ppc_hash_table (info);
3466 for (sym = info->gc_sym_list; sym; sym = sym->next)
3467 {
3468 struct elf_link_hash_entry *h;
3469
3470 h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3471 if (h != NULL)
3472 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3473 }
3474 return TRUE;
3475 }
3476
3477 static bfd_boolean
3478 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
3479 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
3480 {
3481 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
3482 char *local_got_tls_masks;
3483
3484 if (local_got_ents == NULL)
3485 {
3486 bfd_size_type size = symtab_hdr->sh_info;
3487
3488 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
3489 local_got_ents = bfd_zalloc (abfd, size);
3490 if (local_got_ents == NULL)
3491 return FALSE;
3492 elf_local_got_ents (abfd) = local_got_ents;
3493 }
3494
3495 if ((tls_type & TLS_EXPLICIT) == 0)
3496 {
3497 struct got_entry *ent;
3498
3499 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3500 if (ent->addend == r_addend
3501 && ent->owner == abfd
3502 && ent->tls_type == tls_type)
3503 break;
3504 if (ent == NULL)
3505 {
3506 bfd_size_type amt = sizeof (*ent);
3507 ent = bfd_alloc (abfd, amt);
3508 if (ent == NULL)
3509 return FALSE;
3510 ent->next = local_got_ents[r_symndx];
3511 ent->addend = r_addend;
3512 ent->owner = abfd;
3513 ent->tls_type = tls_type;
3514 ent->got.refcount = 0;
3515 local_got_ents[r_symndx] = ent;
3516 }
3517 ent->got.refcount += 1;
3518 }
3519
3520 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
3521 local_got_tls_masks[r_symndx] |= tls_type;
3522 return TRUE;
3523 }
3524
3525 static bfd_boolean
3526 update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
3527 {
3528 struct plt_entry *ent;
3529
3530 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3531 if (ent->addend == addend)
3532 break;
3533 if (ent == NULL)
3534 {
3535 bfd_size_type amt = sizeof (*ent);
3536 ent = bfd_alloc (abfd, amt);
3537 if (ent == NULL)
3538 return FALSE;
3539 ent->next = eh->elf.plt.plist;
3540 ent->addend = addend;
3541 ent->plt.refcount = 0;
3542 eh->elf.plt.plist = ent;
3543 }
3544 ent->plt.refcount += 1;
3545 eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3546 eh->is_func = 1;
3547 return TRUE;
3548 }
3549
3550 /* Find the function descriptor hash entry from the given function code
3551 hash entry FH. Link the entries via their OH fields. */
3552 static struct ppc_link_hash_entry *
3553 get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
3554 {
3555 struct ppc_link_hash_entry *fdh = (struct ppc_link_hash_entry *) fh->oh;
3556
3557 if (fdh == NULL)
3558 {
3559 const char *fd_name = fh->elf.root.root.string + 1;
3560
3561 fdh = (struct ppc_link_hash_entry *)
3562 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
3563 if (fdh != NULL)
3564 {
3565 fdh->is_func_descriptor = 1;
3566 fdh->oh = &fh->elf;
3567 fh->is_func = 1;
3568 fh->oh = &fdh->elf;
3569 }
3570 }
3571
3572 return fdh;
3573 }
3574
3575 /* Look through the relocs for a section during the first phase, and
3576 calculate needed space in the global offset table, procedure
3577 linkage table, and dynamic reloc sections. */
3578
3579 static bfd_boolean
3580 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
3581 asection *sec, const Elf_Internal_Rela *relocs)
3582 {
3583 struct ppc_link_hash_table *htab;
3584 Elf_Internal_Shdr *symtab_hdr;
3585 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3586 const Elf_Internal_Rela *rel;
3587 const Elf_Internal_Rela *rel_end;
3588 asection *sreloc;
3589 asection **opd_sym_map;
3590
3591 if (info->relocatable)
3592 return TRUE;
3593
3594 /* Don't do anything special with non-loaded, non-alloced sections.
3595 In particular, any relocs in such sections should not affect GOT
3596 and PLT reference counting (ie. we don't allow them to create GOT
3597 or PLT entries), there's no possibility or desire to optimize TLS
3598 relocs, and there's not much point in propagating relocs to shared
3599 libs that the dynamic linker won't relocate. */
3600 if ((sec->flags & SEC_ALLOC) == 0)
3601 return TRUE;
3602
3603 htab = ppc_hash_table (info);
3604 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3605
3606 sym_hashes = elf_sym_hashes (abfd);
3607 sym_hashes_end = (sym_hashes
3608 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3609 - symtab_hdr->sh_info);
3610
3611 sreloc = NULL;
3612 opd_sym_map = NULL;
3613 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3614 {
3615 /* Garbage collection needs some extra help with .opd sections.
3616 We don't want to necessarily keep everything referenced by
3617 relocs in .opd, as that would keep all functions. Instead,
3618 if we reference an .opd symbol (a function descriptor), we
3619 want to keep the function code symbol's section. This is
3620 easy for global symbols, but for local syms we need to keep
3621 information about the associated function section. Later, if
3622 edit_opd deletes entries, we'll use this array to adjust
3623 local syms in .opd. */
3624 union opd_info {
3625 asection *func_section;
3626 long entry_adjust;
3627 };
3628 bfd_size_type amt;
3629
3630 amt = sec->_raw_size * sizeof (union opd_info) / 24;
3631 opd_sym_map = bfd_zalloc (abfd, amt);
3632 if (opd_sym_map == NULL)
3633 return FALSE;
3634 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
3635 }
3636
3637 if (htab->sfpr == NULL
3638 && !create_linkage_sections (htab->elf.dynobj, info))
3639 return FALSE;
3640
3641 rel_end = relocs + sec->reloc_count;
3642 for (rel = relocs; rel < rel_end; rel++)
3643 {
3644 unsigned long r_symndx;
3645 struct elf_link_hash_entry *h;
3646 enum elf_ppc64_reloc_type r_type;
3647 int tls_type = 0;
3648
3649 r_symndx = ELF64_R_SYM (rel->r_info);
3650 if (r_symndx < symtab_hdr->sh_info)
3651 h = NULL;
3652 else
3653 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3654
3655 r_type = ELF64_R_TYPE (rel->r_info);
3656 switch (r_type)
3657 {
3658 case R_PPC64_GOT_TLSLD16:
3659 case R_PPC64_GOT_TLSLD16_LO:
3660 case R_PPC64_GOT_TLSLD16_HI:
3661 case R_PPC64_GOT_TLSLD16_HA:
3662 ppc64_tlsld_got (abfd)->refcount += 1;
3663 tls_type = TLS_TLS | TLS_LD;
3664 goto dogottls;
3665
3666 case R_PPC64_GOT_TLSGD16:
3667 case R_PPC64_GOT_TLSGD16_LO:
3668 case R_PPC64_GOT_TLSGD16_HI:
3669 case R_PPC64_GOT_TLSGD16_HA:
3670 tls_type = TLS_TLS | TLS_GD;
3671 goto dogottls;
3672
3673 case R_PPC64_GOT_TPREL16_DS:
3674 case R_PPC64_GOT_TPREL16_LO_DS:
3675 case R_PPC64_GOT_TPREL16_HI:
3676 case R_PPC64_GOT_TPREL16_HA:
3677 if (info->shared)
3678 info->flags |= DF_STATIC_TLS;
3679 tls_type = TLS_TLS | TLS_TPREL;
3680 goto dogottls;
3681
3682 case R_PPC64_GOT_DTPREL16_DS:
3683 case R_PPC64_GOT_DTPREL16_LO_DS:
3684 case R_PPC64_GOT_DTPREL16_HI:
3685 case R_PPC64_GOT_DTPREL16_HA:
3686 tls_type = TLS_TLS | TLS_DTPREL;
3687 dogottls:
3688 sec->has_tls_reloc = 1;
3689 /* Fall thru */
3690
3691 case R_PPC64_GOT16:
3692 case R_PPC64_GOT16_DS:
3693 case R_PPC64_GOT16_HA:
3694 case R_PPC64_GOT16_HI:
3695 case R_PPC64_GOT16_LO:
3696 case R_PPC64_GOT16_LO_DS:
3697 /* This symbol requires a global offset table entry. */
3698 sec->has_gp_reloc = 1;
3699 if (ppc64_elf_tdata (abfd)->got == NULL
3700 && !create_got_section (abfd, info))
3701 return FALSE;
3702
3703 if (h != NULL)
3704 {
3705 struct ppc_link_hash_entry *eh;
3706 struct got_entry *ent;
3707
3708 eh = (struct ppc_link_hash_entry *) h;
3709 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3710 if (ent->addend == rel->r_addend
3711 && ent->owner == abfd
3712 && ent->tls_type == tls_type)
3713 break;
3714 if (ent == NULL)
3715 {
3716 bfd_size_type amt = sizeof (*ent);
3717 ent = bfd_alloc (abfd, amt);
3718 if (ent == NULL)
3719 return FALSE;
3720 ent->next = eh->elf.got.glist;
3721 ent->addend = rel->r_addend;
3722 ent->owner = abfd;
3723 ent->tls_type = tls_type;
3724 ent->got.refcount = 0;
3725 eh->elf.got.glist = ent;
3726 }
3727 ent->got.refcount += 1;
3728 eh->tls_mask |= tls_type;
3729 }
3730 else
3731 /* This is a global offset table entry for a local symbol. */
3732 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3733 rel->r_addend, tls_type))
3734 return FALSE;
3735 break;
3736
3737 case R_PPC64_PLT16_HA:
3738 case R_PPC64_PLT16_HI:
3739 case R_PPC64_PLT16_LO:
3740 case R_PPC64_PLT32:
3741 case R_PPC64_PLT64:
3742 /* This symbol requires a procedure linkage table entry. We
3743 actually build the entry in adjust_dynamic_symbol,
3744 because this might be a case of linking PIC code without
3745 linking in any dynamic objects, in which case we don't
3746 need to generate a procedure linkage table after all. */
3747 if (h == NULL)
3748 {
3749 /* It does not make sense to have a procedure linkage
3750 table entry for a local symbol. */
3751 bfd_set_error (bfd_error_bad_value);
3752 return FALSE;
3753 }
3754 else
3755 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3756 rel->r_addend))
3757 return FALSE;
3758 break;
3759
3760 /* The following relocations don't need to propagate the
3761 relocation if linking a shared object since they are
3762 section relative. */
3763 case R_PPC64_SECTOFF:
3764 case R_PPC64_SECTOFF_LO:
3765 case R_PPC64_SECTOFF_HI:
3766 case R_PPC64_SECTOFF_HA:
3767 case R_PPC64_SECTOFF_DS:
3768 case R_PPC64_SECTOFF_LO_DS:
3769 case R_PPC64_DTPREL16:
3770 case R_PPC64_DTPREL16_LO:
3771 case R_PPC64_DTPREL16_HI:
3772 case R_PPC64_DTPREL16_HA:
3773 case R_PPC64_DTPREL16_DS:
3774 case R_PPC64_DTPREL16_LO_DS:
3775 case R_PPC64_DTPREL16_HIGHER:
3776 case R_PPC64_DTPREL16_HIGHERA:
3777 case R_PPC64_DTPREL16_HIGHEST:
3778 case R_PPC64_DTPREL16_HIGHESTA:
3779 break;
3780
3781 /* Nor do these. */
3782 case R_PPC64_TOC16:
3783 case R_PPC64_TOC16_LO:
3784 case R_PPC64_TOC16_HI:
3785 case R_PPC64_TOC16_HA:
3786 case R_PPC64_TOC16_DS:
3787 case R_PPC64_TOC16_LO_DS:
3788 sec->has_gp_reloc = 1;
3789 break;
3790
3791 /* This relocation describes the C++ object vtable hierarchy.
3792 Reconstruct it for later use during GC. */
3793 case R_PPC64_GNU_VTINHERIT:
3794 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3795 return FALSE;
3796 break;
3797
3798 /* This relocation describes which C++ vtable entries are actually
3799 used. Record for later use during GC. */
3800 case R_PPC64_GNU_VTENTRY:
3801 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3802 return FALSE;
3803 break;
3804
3805 case R_PPC64_REL14:
3806 case R_PPC64_REL14_BRTAKEN:
3807 case R_PPC64_REL14_BRNTAKEN:
3808 htab->has_14bit_branch = 1;
3809 /* Fall through. */
3810
3811 case R_PPC64_REL24:
3812 if (h != NULL
3813 && h->root.root.string[0] == '.'
3814 && h->root.root.string[1] != 0)
3815 {
3816 /* We may need a .plt entry if the function this reloc
3817 refers to is in a shared lib. */
3818 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3819 rel->r_addend))
3820 return FALSE;
3821 if (h == htab->tls_get_addr)
3822 sec->has_tls_reloc = 1;
3823 else if ((strncmp (h->root.root.string, ".__tls_get_addr", 15)
3824 == 0)
3825 && (h->root.root.string[15] == 0
3826 || h->root.root.string[15] == '@'))
3827 {
3828 htab->tls_get_addr = h;
3829 sec->has_tls_reloc = 1;
3830 }
3831 }
3832 break;
3833
3834 case R_PPC64_TPREL64:
3835 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3836 if (info->shared)
3837 info->flags |= DF_STATIC_TLS;
3838 goto dotlstoc;
3839
3840 case R_PPC64_DTPMOD64:
3841 if (rel + 1 < rel_end
3842 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3843 && rel[1].r_offset == rel->r_offset + 8)
3844 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
3845 else
3846 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
3847 goto dotlstoc;
3848
3849 case R_PPC64_DTPREL64:
3850 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3851 if (rel != relocs
3852 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3853 && rel[-1].r_offset == rel->r_offset - 8)
3854 /* This is the second reloc of a dtpmod, dtprel pair.
3855 Don't mark with TLS_DTPREL. */
3856 goto dodyn;
3857
3858 dotlstoc:
3859 sec->has_tls_reloc = 1;
3860 if (h != NULL)
3861 {
3862 struct ppc_link_hash_entry *eh;
3863 eh = (struct ppc_link_hash_entry *) h;
3864 eh->tls_mask |= tls_type;
3865 }
3866 else
3867 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3868 rel->r_addend, tls_type))
3869 return FALSE;
3870
3871 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3872 {
3873 /* One extra to simplify get_tls_mask. */
3874 bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8 + 1;
3875 ppc64_elf_section_data (sec)->t_symndx = bfd_zalloc (abfd, amt);
3876 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3877 return FALSE;
3878 }
3879 BFD_ASSERT (rel->r_offset % 8 == 0);
3880 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
3881
3882 /* Mark the second slot of a GD or LD entry.
3883 -1 to indicate GD and -2 to indicate LD. */
3884 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3885 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
3886 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3887 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
3888 goto dodyn;
3889
3890 case R_PPC64_TPREL16:
3891 case R_PPC64_TPREL16_LO:
3892 case R_PPC64_TPREL16_HI:
3893 case R_PPC64_TPREL16_HA:
3894 case R_PPC64_TPREL16_DS:
3895 case R_PPC64_TPREL16_LO_DS:
3896 case R_PPC64_TPREL16_HIGHER:
3897 case R_PPC64_TPREL16_HIGHERA:
3898 case R_PPC64_TPREL16_HIGHEST:
3899 case R_PPC64_TPREL16_HIGHESTA:
3900 if (info->shared)
3901 {
3902 info->flags |= DF_STATIC_TLS;
3903 goto dodyn;
3904 }
3905 break;
3906
3907 case R_PPC64_ADDR64:
3908 if (opd_sym_map != NULL
3909 && h != NULL
3910 && h->root.root.string[0] == '.'
3911 && h->root.root.string[1] != 0)
3912 get_fdh ((struct ppc_link_hash_entry *) h, htab);
3913
3914 if (opd_sym_map != NULL
3915 && h == NULL
3916 && rel + 1 < rel_end
3917 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
3918 {
3919 asection *s;
3920
3921 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3922 r_symndx);
3923 if (s == NULL)
3924 return FALSE;
3925 else if (s != sec)
3926 opd_sym_map[rel->r_offset / 24] = s;
3927 }
3928 /* Fall through. */
3929
3930 case R_PPC64_REL30:
3931 case R_PPC64_REL32:
3932 case R_PPC64_REL64:
3933 case R_PPC64_ADDR14:
3934 case R_PPC64_ADDR14_BRNTAKEN:
3935 case R_PPC64_ADDR14_BRTAKEN:
3936 case R_PPC64_ADDR16:
3937 case R_PPC64_ADDR16_DS:
3938 case R_PPC64_ADDR16_HA:
3939 case R_PPC64_ADDR16_HI:
3940 case R_PPC64_ADDR16_HIGHER:
3941 case R_PPC64_ADDR16_HIGHERA:
3942 case R_PPC64_ADDR16_HIGHEST:
3943 case R_PPC64_ADDR16_HIGHESTA:
3944 case R_PPC64_ADDR16_LO:
3945 case R_PPC64_ADDR16_LO_DS:
3946 case R_PPC64_ADDR24:
3947 case R_PPC64_ADDR32:
3948 case R_PPC64_UADDR16:
3949 case R_PPC64_UADDR32:
3950 case R_PPC64_UADDR64:
3951 case R_PPC64_TOC:
3952 if (h != NULL && !info->shared)
3953 /* We may need a copy reloc. */
3954 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
3955
3956 /* Don't propagate .opd relocs. */
3957 if (NO_OPD_RELOCS && opd_sym_map != NULL)
3958 break;
3959
3960 /* If we are creating a shared library, and this is a reloc
3961 against a global symbol, or a non PC relative reloc
3962 against a local symbol, then we need to copy the reloc
3963 into the shared library. However, if we are linking with
3964 -Bsymbolic, we do not need to copy a reloc against a
3965 global symbol which is defined in an object we are
3966 including in the link (i.e., DEF_REGULAR is set). At
3967 this point we have not seen all the input files, so it is
3968 possible that DEF_REGULAR is not set now but will be set
3969 later (it is never cleared). In case of a weak definition,
3970 DEF_REGULAR may be cleared later by a strong definition in
3971 a shared library. We account for that possibility below by
3972 storing information in the dyn_relocs field of the hash
3973 table entry. A similar situation occurs when creating
3974 shared libraries and symbol visibility changes render the
3975 symbol local.
3976
3977 If on the other hand, we are creating an executable, we
3978 may need to keep relocations for symbols satisfied by a
3979 dynamic library if we manage to avoid copy relocs for the
3980 symbol. */
3981 dodyn:
3982 if ((info->shared
3983 && (MUST_BE_DYN_RELOC (r_type)
3984 || (h != NULL
3985 && (! info->symbolic
3986 || h->root.type == bfd_link_hash_defweak
3987 || (h->elf_link_hash_flags
3988 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3989 || (ELIMINATE_COPY_RELOCS
3990 && !info->shared
3991 && h != NULL
3992 && (h->root.type == bfd_link_hash_defweak
3993 || (h->elf_link_hash_flags
3994 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3995 {
3996 struct ppc_dyn_relocs *p;
3997 struct ppc_dyn_relocs **head;
3998
3999 /* We must copy these reloc types into the output file.
4000 Create a reloc section in dynobj and make room for
4001 this reloc. */
4002 if (sreloc == NULL)
4003 {
4004 const char *name;
4005 bfd *dynobj;
4006
4007 name = (bfd_elf_string_from_elf_section
4008 (abfd,
4009 elf_elfheader (abfd)->e_shstrndx,
4010 elf_section_data (sec)->rel_hdr.sh_name));
4011 if (name == NULL)
4012 return FALSE;
4013
4014 if (strncmp (name, ".rela", 5) != 0
4015 || strcmp (bfd_get_section_name (abfd, sec),
4016 name + 5) != 0)
4017 {
4018 (*_bfd_error_handler)
4019 (_("%s: bad relocation section name `%s\'"),
4020 bfd_archive_filename (abfd), name);
4021 bfd_set_error (bfd_error_bad_value);
4022 }
4023
4024 dynobj = htab->elf.dynobj;
4025 sreloc = bfd_get_section_by_name (dynobj, name);
4026 if (sreloc == NULL)
4027 {
4028 flagword flags;
4029
4030 sreloc = bfd_make_section (dynobj, name);
4031 flags = (SEC_HAS_CONTENTS | SEC_READONLY
4032 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4033 if ((sec->flags & SEC_ALLOC) != 0)
4034 flags |= SEC_ALLOC | SEC_LOAD;
4035 if (sreloc == NULL
4036 || ! bfd_set_section_flags (dynobj, sreloc, flags)
4037 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
4038 return FALSE;
4039 }
4040 elf_section_data (sec)->sreloc = sreloc;
4041 }
4042
4043 /* If this is a global symbol, we count the number of
4044 relocations we need for this symbol. */
4045 if (h != NULL)
4046 {
4047 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
4048 }
4049 else
4050 {
4051 /* Track dynamic relocs needed for local syms too.
4052 We really need local syms available to do this
4053 easily. Oh well. */
4054
4055 asection *s;
4056 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4057 sec, r_symndx);
4058 if (s == NULL)
4059 return FALSE;
4060
4061 head = ((struct ppc_dyn_relocs **)
4062 &elf_section_data (s)->local_dynrel);
4063 }
4064
4065 p = *head;
4066 if (p == NULL || p->sec != sec)
4067 {
4068 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
4069 if (p == NULL)
4070 return FALSE;
4071 p->next = *head;
4072 *head = p;
4073 p->sec = sec;
4074 p->count = 0;
4075 p->pc_count = 0;
4076 }
4077
4078 p->count += 1;
4079 if (!MUST_BE_DYN_RELOC (r_type))
4080 p->pc_count += 1;
4081 }
4082 break;
4083
4084 default:
4085 break;
4086 }
4087 }
4088
4089 return TRUE;
4090 }
4091
4092 /* Return the section that should be marked against GC for a given
4093 relocation. */
4094
4095 static asection *
4096 ppc64_elf_gc_mark_hook (asection *sec,
4097 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4098 Elf_Internal_Rela *rel,
4099 struct elf_link_hash_entry *h,
4100 Elf_Internal_Sym *sym)
4101 {
4102 asection *rsec = NULL;
4103
4104 if (h != NULL)
4105 {
4106 enum elf_ppc64_reloc_type r_type;
4107 struct ppc_link_hash_entry *fdh;
4108
4109 r_type = ELF64_R_TYPE (rel->r_info);
4110 switch (r_type)
4111 {
4112 case R_PPC64_GNU_VTINHERIT:
4113 case R_PPC64_GNU_VTENTRY:
4114 break;
4115
4116 default:
4117 switch (h->root.type)
4118 {
4119 case bfd_link_hash_defined:
4120 case bfd_link_hash_defweak:
4121 fdh = (struct ppc_link_hash_entry *) h;
4122
4123 /* Function descriptor syms cause the associated
4124 function code sym section to be marked. */
4125 if (fdh->is_func_descriptor)
4126 rsec = fdh->oh->root.u.def.section;
4127
4128 /* Function entry syms return NULL if they are in .opd
4129 and are not ._start (or others undefined on the ld
4130 command line). Thus we avoid marking all function
4131 sections, as all functions are referenced in .opd. */
4132 else if ((fdh->oh != NULL
4133 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
4134 || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
4135 rsec = h->root.u.def.section;
4136 break;
4137
4138 case bfd_link_hash_common:
4139 rsec = h->root.u.c.p->section;
4140 break;
4141
4142 default:
4143 break;
4144 }
4145 }
4146 }
4147 else
4148 {
4149 asection **opd_sym_section;
4150
4151 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4152 opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
4153 if (opd_sym_section != NULL)
4154 rsec = opd_sym_section[sym->st_value / 24];
4155 else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
4156 rsec = NULL;
4157 }
4158
4159 return rsec;
4160 }
4161
4162 /* Update the .got, .plt. and dynamic reloc reference counts for the
4163 section being removed. */
4164
4165 static bfd_boolean
4166 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
4167 asection *sec, const Elf_Internal_Rela *relocs)
4168 {
4169 struct ppc_link_hash_table *htab;
4170 Elf_Internal_Shdr *symtab_hdr;
4171 struct elf_link_hash_entry **sym_hashes;
4172 struct got_entry **local_got_ents;
4173 const Elf_Internal_Rela *rel, *relend;
4174
4175 if ((sec->flags & SEC_ALLOC) == 0)
4176 return TRUE;
4177
4178 elf_section_data (sec)->local_dynrel = NULL;
4179
4180 htab = ppc_hash_table (info);
4181 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4182 sym_hashes = elf_sym_hashes (abfd);
4183 local_got_ents = elf_local_got_ents (abfd);
4184
4185 relend = relocs + sec->reloc_count;
4186 for (rel = relocs; rel < relend; rel++)
4187 {
4188 unsigned long r_symndx;
4189 enum elf_ppc64_reloc_type r_type;
4190 struct elf_link_hash_entry *h = NULL;
4191 char tls_type = 0;
4192
4193 r_symndx = ELF64_R_SYM (rel->r_info);
4194 r_type = ELF64_R_TYPE (rel->r_info);
4195 if (r_symndx >= symtab_hdr->sh_info)
4196 {
4197 struct ppc_link_hash_entry *eh;
4198 struct ppc_dyn_relocs **pp;
4199 struct ppc_dyn_relocs *p;
4200
4201 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4202 eh = (struct ppc_link_hash_entry *) h;
4203
4204 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4205 if (p->sec == sec)
4206 {
4207 /* Everything must go for SEC. */
4208 *pp = p->next;
4209 break;
4210 }
4211 }
4212
4213 switch (r_type)
4214 {
4215 case R_PPC64_GOT_TLSLD16:
4216 case R_PPC64_GOT_TLSLD16_LO:
4217 case R_PPC64_GOT_TLSLD16_HI:
4218 case R_PPC64_GOT_TLSLD16_HA:
4219 ppc64_tlsld_got (abfd)->refcount -= 1;
4220 tls_type = TLS_TLS | TLS_LD;
4221 goto dogot;
4222
4223 case R_PPC64_GOT_TLSGD16:
4224 case R_PPC64_GOT_TLSGD16_LO:
4225 case R_PPC64_GOT_TLSGD16_HI:
4226 case R_PPC64_GOT_TLSGD16_HA:
4227 tls_type = TLS_TLS | TLS_GD;
4228 goto dogot;
4229
4230 case R_PPC64_GOT_TPREL16_DS:
4231 case R_PPC64_GOT_TPREL16_LO_DS:
4232 case R_PPC64_GOT_TPREL16_HI:
4233 case R_PPC64_GOT_TPREL16_HA:
4234 tls_type = TLS_TLS | TLS_TPREL;
4235 goto dogot;
4236
4237 case R_PPC64_GOT_DTPREL16_DS:
4238 case R_PPC64_GOT_DTPREL16_LO_DS:
4239 case R_PPC64_GOT_DTPREL16_HI:
4240 case R_PPC64_GOT_DTPREL16_HA:
4241 tls_type = TLS_TLS | TLS_DTPREL;
4242 goto dogot;
4243
4244 case R_PPC64_GOT16:
4245 case R_PPC64_GOT16_DS:
4246 case R_PPC64_GOT16_HA:
4247 case R_PPC64_GOT16_HI:
4248 case R_PPC64_GOT16_LO:
4249 case R_PPC64_GOT16_LO_DS:
4250 dogot:
4251 {
4252 struct got_entry *ent;
4253
4254 if (h != NULL)
4255 ent = h->got.glist;
4256 else
4257 ent = local_got_ents[r_symndx];
4258
4259 for (; ent != NULL; ent = ent->next)
4260 if (ent->addend == rel->r_addend
4261 && ent->owner == abfd
4262 && ent->tls_type == tls_type)
4263 break;
4264 if (ent == NULL)
4265 abort ();
4266 if (ent->got.refcount > 0)
4267 ent->got.refcount -= 1;
4268 }
4269 break;
4270
4271 case R_PPC64_PLT16_HA:
4272 case R_PPC64_PLT16_HI:
4273 case R_PPC64_PLT16_LO:
4274 case R_PPC64_PLT32:
4275 case R_PPC64_PLT64:
4276 case R_PPC64_REL14:
4277 case R_PPC64_REL14_BRNTAKEN:
4278 case R_PPC64_REL14_BRTAKEN:
4279 case R_PPC64_REL24:
4280 if (h != NULL)
4281 {
4282 struct plt_entry *ent;
4283
4284 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4285 if (ent->addend == rel->r_addend)
4286 break;
4287 if (ent == NULL)
4288 abort ();
4289 if (ent->plt.refcount > 0)
4290 ent->plt.refcount -= 1;
4291 }
4292 break;
4293
4294 default:
4295 break;
4296 }
4297 }
4298 return TRUE;
4299 }
4300
4301 /* Called via elf_link_hash_traverse to transfer dynamic linking
4302 information on function code symbol entries to their corresponding
4303 function descriptor symbol entries. */
4304 static bfd_boolean
4305 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
4306 {
4307 struct bfd_link_info *info;
4308 struct ppc_link_hash_table *htab;
4309 struct plt_entry *ent;
4310 struct ppc_link_hash_entry *fh;
4311 struct ppc_link_hash_entry *fdh;
4312 bfd_boolean force_local;
4313
4314 fh = (struct ppc_link_hash_entry *) h;
4315 if (fh->elf.root.type == bfd_link_hash_indirect)
4316 return TRUE;
4317
4318 if (fh->elf.root.type == bfd_link_hash_warning)
4319 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
4320
4321 info = inf;
4322 htab = ppc_hash_table (info);
4323
4324 /* If this is a function code symbol, transfer dynamic linking
4325 information to the function descriptor symbol. */
4326 if (!fh->is_func)
4327 return TRUE;
4328
4329 if (fh->elf.root.type == bfd_link_hash_undefweak
4330 && (fh->elf.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
4331 htab->have_undefweak = TRUE;
4332
4333 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
4334 if (ent->plt.refcount > 0)
4335 break;
4336 if (ent == NULL
4337 || fh->elf.root.root.string[0] != '.'
4338 || fh->elf.root.root.string[1] == '\0')
4339 return TRUE;
4340
4341 /* Find the corresponding function descriptor symbol. Create it
4342 as undefined if necessary. */
4343
4344 fdh = get_fdh (fh, htab);
4345 if (fdh != NULL)
4346 while (fdh->elf.root.type == bfd_link_hash_indirect
4347 || fdh->elf.root.type == bfd_link_hash_warning)
4348 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
4349
4350 if (fdh == NULL
4351 && info->shared
4352 && (fh->elf.root.type == bfd_link_hash_undefined
4353 || fh->elf.root.type == bfd_link_hash_undefweak))
4354 {
4355 bfd *abfd;
4356 asymbol *newsym;
4357 struct bfd_link_hash_entry *bh;
4358
4359 abfd = fh->elf.root.u.undef.abfd;
4360 newsym = bfd_make_empty_symbol (abfd);
4361 newsym->name = fh->elf.root.root.string + 1;
4362 newsym->section = bfd_und_section_ptr;
4363 newsym->value = 0;
4364 newsym->flags = BSF_OBJECT;
4365 if (fh->elf.root.type == bfd_link_hash_undefweak)
4366 newsym->flags |= BSF_WEAK;
4367
4368 bh = &fdh->elf.root;
4369 if ( !(_bfd_generic_link_add_one_symbol
4370 (info, abfd, newsym->name, newsym->flags,
4371 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
4372 {
4373 return FALSE;
4374 }
4375 fdh = (struct ppc_link_hash_entry *) bh;
4376 fdh->elf.elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4377 fdh->elf.size = 24;
4378 fdh->elf.type = STT_OBJECT;
4379 }
4380
4381 if (fdh != NULL
4382 && (fdh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4383 && (info->shared
4384 || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4385 || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
4386 || (fdh->elf.root.type == bfd_link_hash_undefweak
4387 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
4388 {
4389 if (fdh->elf.dynindx == -1)
4390 if (! bfd_elf64_link_record_dynamic_symbol (info, &fdh->elf))
4391 return FALSE;
4392 fdh->elf.elf_link_hash_flags
4393 |= (fh->elf.elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
4394 | ELF_LINK_HASH_REF_DYNAMIC
4395 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4396 | ELF_LINK_NON_GOT_REF));
4397 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
4398 {
4399 fdh->elf.plt.plist = fh->elf.plt.plist;
4400 fdh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4401 }
4402 fdh->is_func_descriptor = 1;
4403 fdh->oh = &fh->elf;
4404 fh->oh = &fdh->elf;
4405 }
4406
4407 /* Now that the info is on the function descriptor, clear the
4408 function code sym info. Any function code syms for which we
4409 don't have a definition in a regular file, we force local.
4410 This prevents a shared library from exporting syms that have
4411 been imported from another library. Function code syms that
4412 are really in the library we must leave global to prevent the
4413 linker dragging in a definition from a static library. */
4414 force_local
4415 = (info->shared
4416 && ((fh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4417 || fdh == NULL
4418 || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
4419 || (fdh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0));
4420 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
4421
4422 return TRUE;
4423 }
4424
4425 #define MIN_SAVE_FPR 14
4426 #define MAX_SAVE_FPR 31
4427
4428 /* Called near the start of bfd_elf_size_dynamic_sections. We use
4429 this hook to a) provide some gcc support functions, and b) transfer
4430 dynamic linking information gathered so far on function code symbol
4431 entries, to their corresponding function descriptor symbol entries. */
4432 static bfd_boolean
4433 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
4434 struct bfd_link_info *info)
4435 {
4436 struct ppc_link_hash_table *htab;
4437 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4438 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4439 unsigned int i;
4440 struct elf_link_hash_entry *h;
4441 bfd_byte *p;
4442 char sym[10];
4443
4444 htab = ppc_hash_table (info);
4445
4446 if (htab->sfpr == NULL)
4447 /* We don't have any relocs. */
4448 return TRUE;
4449
4450 /* First provide any missing ._savef* and ._restf* functions. */
4451 memcpy (sym, "._savef14", 10);
4452 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4453 {
4454 sym[7] = i / 10 + '0';
4455 sym[8] = i % 10 + '0';
4456 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4457 if (h != NULL
4458 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4459 {
4460 if (lowest_savef > i)
4461 lowest_savef = i;
4462 h->root.type = bfd_link_hash_defined;
4463 h->root.u.def.section = htab->sfpr;
4464 h->root.u.def.value = (i - lowest_savef) * 4;
4465 h->type = STT_FUNC;
4466 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4467 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
4468 }
4469 }
4470
4471 memcpy (sym, "._restf14", 10);
4472 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4473 {
4474 sym[7] = i / 10 + '0';
4475 sym[8] = i % 10 + '0';
4476 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4477 if (h != NULL
4478 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4479 {
4480 if (lowest_restf > i)
4481 lowest_restf = i;
4482 h->root.type = bfd_link_hash_defined;
4483 h->root.u.def.section = htab->sfpr;
4484 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4485 + (i - lowest_restf) * 4);
4486 h->type = STT_FUNC;
4487 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4488 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
4489 }
4490 }
4491
4492 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
4493
4494 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4495 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4496
4497 if (htab->sfpr->_raw_size == 0)
4498 {
4499 if (!htab->have_undefweak)
4500 {
4501 _bfd_strip_section_from_output (info, htab->sfpr);
4502 return TRUE;
4503 }
4504
4505 htab->sfpr->_raw_size = 4;
4506 }
4507
4508 p = bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4509 if (p == NULL)
4510 return FALSE;
4511 htab->sfpr->contents = p;
4512
4513 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4514 {
4515 unsigned int fpr = i << 21;
4516 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4517 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4518 p += 4;
4519 }
4520 if (lowest_savef <= MAX_SAVE_FPR)
4521 {
4522 bfd_put_32 (htab->elf.dynobj, BLR, p);
4523 p += 4;
4524 }
4525
4526 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4527 {
4528 unsigned int fpr = i << 21;
4529 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4530 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
4531 p += 4;
4532 }
4533 if (lowest_restf <= MAX_SAVE_FPR
4534 || htab->sfpr->_raw_size == 4)
4535 {
4536 bfd_put_32 (htab->elf.dynobj, BLR, p);
4537 }
4538
4539 return TRUE;
4540 }
4541
4542 /* Adjust a symbol defined by a dynamic object and referenced by a
4543 regular object. The current definition is in some section of the
4544 dynamic object, but we're not including those sections. We have to
4545 change the definition to something the rest of the link can
4546 understand. */
4547
4548 static bfd_boolean
4549 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4550 struct elf_link_hash_entry *h)
4551 {
4552 struct ppc_link_hash_table *htab;
4553 asection *s;
4554 unsigned int power_of_two;
4555
4556 htab = ppc_hash_table (info);
4557
4558 /* Deal with function syms. */
4559 if (h->type == STT_FUNC
4560 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4561 {
4562 /* Clear procedure linkage table information for any symbol that
4563 won't need a .plt entry. */
4564 struct plt_entry *ent;
4565 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4566 if (ent->plt.refcount > 0)
4567 break;
4568 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
4569 || ent == NULL
4570 || SYMBOL_CALLS_LOCAL (info, h)
4571 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
4572 && h->root.type == bfd_link_hash_undefweak))
4573 {
4574 h->plt.plist = NULL;
4575 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4576 }
4577 }
4578 else
4579 h->plt.plist = NULL;
4580
4581 /* If this is a weak symbol, and there is a real definition, the
4582 processor independent code will have arranged for us to see the
4583 real definition first, and we can just use the same value. */
4584 if (h->weakdef != NULL)
4585 {
4586 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4587 || h->weakdef->root.type == bfd_link_hash_defweak);
4588 h->root.u.def.section = h->weakdef->root.u.def.section;
4589 h->root.u.def.value = h->weakdef->root.u.def.value;
4590 if (ELIMINATE_COPY_RELOCS)
4591 h->elf_link_hash_flags
4592 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
4593 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
4594 return TRUE;
4595 }
4596
4597 /* If we are creating a shared library, we must presume that the
4598 only references to the symbol are via the global offset table.
4599 For such cases we need not do anything here; the relocations will
4600 be handled correctly by relocate_section. */
4601 if (info->shared)
4602 return TRUE;
4603
4604 /* If there are no references to this symbol that do not use the
4605 GOT, we don't need to generate a copy reloc. */
4606 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
4607 return TRUE;
4608
4609 if (ELIMINATE_COPY_RELOCS)
4610 {
4611 struct ppc_link_hash_entry * eh;
4612 struct ppc_dyn_relocs *p;
4613
4614 eh = (struct ppc_link_hash_entry *) h;
4615 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4616 {
4617 s = p->sec->output_section;
4618 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4619 break;
4620 }
4621
4622 /* If we didn't find any dynamic relocs in read-only sections, then
4623 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
4624 if (p == NULL)
4625 {
4626 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
4627 return TRUE;
4628 }
4629 }
4630
4631 if (h->plt.plist != NULL)
4632 {
4633 /* We should never get here, but unfortunately there are versions
4634 of gcc out there that improperly (for this ABI) put initialized
4635 function pointers, vtable refs and suchlike in read-only
4636 sections. Allow them to proceed, but warn that this might
4637 break at runtime. */
4638 (*_bfd_error_handler)
4639 (_("copy reloc against `%s' requires lazy plt linking; "
4640 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
4641 h->root.root.string);
4642 }
4643
4644 /* This is a reference to a symbol defined by a dynamic object which
4645 is not a function. */
4646
4647 /* We must allocate the symbol in our .dynbss section, which will
4648 become part of the .bss section of the executable. There will be
4649 an entry for this symbol in the .dynsym section. The dynamic
4650 object will contain position independent code, so all references
4651 from the dynamic object to this symbol will go through the global
4652 offset table. The dynamic linker will use the .dynsym entry to
4653 determine the address it must put in the global offset table, so
4654 both the dynamic object and the regular object will refer to the
4655 same memory location for the variable. */
4656
4657 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4658 to copy the initial value out of the dynamic object and into the
4659 runtime process image. We need to remember the offset into the
4660 .rela.bss section we are going to use. */
4661 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4662 {
4663 htab->relbss->_raw_size += sizeof (Elf64_External_Rela);
4664 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4665 }
4666
4667 /* We need to figure out the alignment required for this symbol. I
4668 have no idea how ELF linkers handle this. */
4669 power_of_two = bfd_log2 (h->size);
4670 if (power_of_two > 4)
4671 power_of_two = 4;
4672
4673 /* Apply the required alignment. */
4674 s = htab->dynbss;
4675 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4676 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
4677 {
4678 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
4679 return FALSE;
4680 }
4681
4682 /* Define the symbol as being at this point in the section. */
4683 h->root.u.def.section = s;
4684 h->root.u.def.value = s->_raw_size;
4685
4686 /* Increment the section size to make room for the symbol. */
4687 s->_raw_size += h->size;
4688
4689 return TRUE;
4690 }
4691
4692 /* If given a function descriptor symbol, hide both the function code
4693 sym and the descriptor. */
4694 static void
4695 ppc64_elf_hide_symbol (struct bfd_link_info *info,
4696 struct elf_link_hash_entry *h,
4697 bfd_boolean force_local)
4698 {
4699 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4700
4701 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4702 {
4703 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
4704
4705 if (fh == NULL)
4706 {
4707 const char *p, *q;
4708 struct ppc_link_hash_table *htab;
4709 char save;
4710
4711 /* We aren't supposed to use alloca in BFD because on
4712 systems which do not have alloca the version in libiberty
4713 calls xmalloc, which might cause the program to crash
4714 when it runs out of memory. This function doesn't have a
4715 return status, so there's no way to gracefully return an
4716 error. So cheat. We know that string[-1] can be safely
4717 dereferenced; It's either a string in an ELF string
4718 table, or allocated in an objalloc structure. */
4719
4720 p = h->root.root.string - 1;
4721 save = *p;
4722 *(char *) p = '.';
4723 htab = ppc_hash_table (info);
4724 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4725 *(char *) p = save;
4726
4727 /* Unfortunately, if it so happens that the string we were
4728 looking for was allocated immediately before this string,
4729 then we overwrote the string terminator. That's the only
4730 reason the lookup should fail. */
4731 if (fh == NULL)
4732 {
4733 q = h->root.root.string + strlen (h->root.root.string);
4734 while (q >= h->root.root.string && *q == *p)
4735 --q, --p;
4736 if (q < h->root.root.string && *p == '.')
4737 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4738 }
4739 if (fh != NULL)
4740 {
4741 ((struct ppc_link_hash_entry *) h)->oh = fh;
4742 ((struct ppc_link_hash_entry *) fh)->oh = h;
4743 }
4744 }
4745 if (fh != NULL)
4746 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4747 }
4748 }
4749
4750 static bfd_boolean
4751 get_sym_h (struct elf_link_hash_entry **hp, Elf_Internal_Sym **symp,
4752 asection **symsecp, char **tls_maskp, Elf_Internal_Sym **locsymsp,
4753 unsigned long r_symndx, bfd *ibfd)
4754 {
4755 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4756
4757 if (r_symndx >= symtab_hdr->sh_info)
4758 {
4759 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4760 struct elf_link_hash_entry *h;
4761
4762 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4763 while (h->root.type == bfd_link_hash_indirect
4764 || h->root.type == bfd_link_hash_warning)
4765 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4766
4767 if (hp != NULL)
4768 *hp = h;
4769
4770 if (symp != NULL)
4771 *symp = NULL;
4772
4773 if (symsecp != NULL)
4774 {
4775 asection *symsec = NULL;
4776 if (h->root.type == bfd_link_hash_defined
4777 || h->root.type == bfd_link_hash_defweak)
4778 symsec = h->root.u.def.section;
4779 *symsecp = symsec;
4780 }
4781
4782 if (tls_maskp != NULL)
4783 {
4784 struct ppc_link_hash_entry *eh;
4785
4786 eh = (struct ppc_link_hash_entry *) h;
4787 *tls_maskp = &eh->tls_mask;
4788 }
4789 }
4790 else
4791 {
4792 Elf_Internal_Sym *sym;
4793 Elf_Internal_Sym *locsyms = *locsymsp;
4794
4795 if (locsyms == NULL)
4796 {
4797 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4798 if (locsyms == NULL)
4799 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4800 symtab_hdr->sh_info,
4801 0, NULL, NULL, NULL);
4802 if (locsyms == NULL)
4803 return FALSE;
4804 *locsymsp = locsyms;
4805 }
4806 sym = locsyms + r_symndx;
4807
4808 if (hp != NULL)
4809 *hp = NULL;
4810
4811 if (symp != NULL)
4812 *symp = sym;
4813
4814 if (symsecp != NULL)
4815 {
4816 asection *symsec = NULL;
4817 if ((sym->st_shndx != SHN_UNDEF
4818 && sym->st_shndx < SHN_LORESERVE)
4819 || sym->st_shndx > SHN_HIRESERVE)
4820 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4821 *symsecp = symsec;
4822 }
4823
4824 if (tls_maskp != NULL)
4825 {
4826 struct got_entry **lgot_ents;
4827 char *tls_mask;
4828
4829 tls_mask = NULL;
4830 lgot_ents = elf_local_got_ents (ibfd);
4831 if (lgot_ents != NULL)
4832 {
4833 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
4834 tls_mask = &lgot_masks[r_symndx];
4835 }
4836 *tls_maskp = tls_mask;
4837 }
4838 }
4839 return TRUE;
4840 }
4841
4842 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
4843 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
4844 type suitable for optimization, and 1 otherwise. */
4845
4846 static int
4847 get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
4848 Elf_Internal_Sym **locsymsp,
4849 const Elf_Internal_Rela *rel, bfd *ibfd)
4850 {
4851 unsigned long r_symndx;
4852 int next_r;
4853 struct elf_link_hash_entry *h;
4854 Elf_Internal_Sym *sym;
4855 asection *sec;
4856 bfd_vma off;
4857
4858 r_symndx = ELF64_R_SYM (rel->r_info);
4859 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4860 return 0;
4861
4862 if ((*tls_maskp != NULL && **tls_maskp != 0)
4863 || sec == NULL
4864 || ppc64_elf_section_data (sec)->t_symndx == NULL)
4865 return 1;
4866
4867 /* Look inside a TOC section too. */
4868 if (h != NULL)
4869 {
4870 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4871 off = h->root.u.def.value;
4872 }
4873 else
4874 off = sym->st_value;
4875 off += rel->r_addend;
4876 BFD_ASSERT (off % 8 == 0);
4877 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
4878 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
4879 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
4880 return 0;
4881 if (toc_symndx != NULL)
4882 *toc_symndx = r_symndx;
4883 if ((h == NULL
4884 || ((h->root.type == bfd_link_hash_defined
4885 || h->root.type == bfd_link_hash_defweak)
4886 && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4887 && (next_r == -1 || next_r == -2))
4888 return 1 - next_r;
4889 return 1;
4890 }
4891
4892 /* Adjust all global syms defined in opd sections. In gcc generated
4893 code these will already have been done, but I suppose we have to
4894 cater for all sorts of hand written assembly. */
4895
4896 static bfd_boolean
4897 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
4898 {
4899 struct ppc_link_hash_entry *eh;
4900 asection *sym_sec;
4901 long *opd_adjust;
4902
4903 if (h->root.type == bfd_link_hash_indirect)
4904 return TRUE;
4905
4906 if (h->root.type == bfd_link_hash_warning)
4907 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4908
4909 if (h->root.type != bfd_link_hash_defined
4910 && h->root.type != bfd_link_hash_defweak)
4911 return TRUE;
4912
4913 eh = (struct ppc_link_hash_entry *) h;
4914 if (eh->adjust_done)
4915 return TRUE;
4916
4917 sym_sec = eh->elf.root.u.def.section;
4918 if (sym_sec != NULL
4919 && elf_section_data (sym_sec) != NULL
4920 && (opd_adjust = ppc64_elf_section_data (sym_sec)->opd.adjust) != NULL)
4921 {
4922 eh->elf.root.u.def.value += opd_adjust[eh->elf.root.u.def.value / 24];
4923 eh->adjust_done = 1;
4924 }
4925 return TRUE;
4926 }
4927
4928 /* Remove unused Official Procedure Descriptor entries. Currently we
4929 only remove those associated with functions in discarded link-once
4930 sections, or weakly defined functions that have been overridden. It
4931 would be possible to remove many more entries for statically linked
4932 applications. */
4933
4934 bfd_boolean
4935 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
4936 {
4937 bfd *ibfd;
4938 bfd_boolean some_edited = FALSE;
4939
4940 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4941 {
4942 asection *sec;
4943 Elf_Internal_Rela *relstart, *rel, *relend;
4944 Elf_Internal_Shdr *symtab_hdr;
4945 Elf_Internal_Sym *local_syms;
4946 struct elf_link_hash_entry **sym_hashes;
4947 bfd_vma offset;
4948 bfd_size_type amt;
4949 long *adjust;
4950 bfd_boolean need_edit;
4951
4952 sec = bfd_get_section_by_name (ibfd, ".opd");
4953 if (sec == NULL)
4954 continue;
4955
4956 amt = sec->_raw_size * sizeof (long) / 24;
4957 adjust = ppc64_elf_section_data (sec)->opd.adjust;
4958 if (adjust == NULL)
4959 {
4960 /* Must be a ld -r link. ie. check_relocs hasn't been
4961 called. */
4962 adjust = bfd_zalloc (obfd, amt);
4963 ppc64_elf_section_data (sec)->opd.adjust = adjust;
4964 }
4965 memset (adjust, 0, amt);
4966
4967 if (sec->output_section == bfd_abs_section_ptr)
4968 continue;
4969
4970 /* Look through the section relocs. */
4971 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4972 continue;
4973
4974 local_syms = NULL;
4975 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4976 sym_hashes = elf_sym_hashes (ibfd);
4977
4978 /* Read the relocations. */
4979 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
4980 info->keep_memory);
4981 if (relstart == NULL)
4982 return FALSE;
4983
4984 /* First run through the relocs to check they are sane, and to
4985 determine whether we need to edit this opd section. */
4986 need_edit = FALSE;
4987 offset = 0;
4988 relend = relstart + sec->reloc_count;
4989 for (rel = relstart; rel < relend; )
4990 {
4991 enum elf_ppc64_reloc_type r_type;
4992 unsigned long r_symndx;
4993 asection *sym_sec;
4994 struct elf_link_hash_entry *h;
4995 Elf_Internal_Sym *sym;
4996
4997 /* .opd contains a regular array of 24 byte entries. We're
4998 only interested in the reloc pointing to a function entry
4999 point. */
5000 if (rel->r_offset != offset
5001 || rel + 1 >= relend
5002 || (rel + 1)->r_offset != offset + 8)
5003 {
5004 /* If someone messes with .opd alignment then after a
5005 "ld -r" we might have padding in the middle of .opd.
5006 Also, there's nothing to prevent someone putting
5007 something silly in .opd with the assembler. No .opd
5008 optimization for them! */
5009 (*_bfd_error_handler)
5010 (_("%s: .opd is not a regular array of opd entries"),
5011 bfd_archive_filename (ibfd));
5012 need_edit = FALSE;
5013 break;
5014 }
5015
5016 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
5017 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
5018 {
5019 (*_bfd_error_handler)
5020 (_("%s: unexpected reloc type %u in .opd section"),
5021 bfd_archive_filename (ibfd), r_type);
5022 need_edit = FALSE;
5023 break;
5024 }
5025
5026 r_symndx = ELF64_R_SYM (rel->r_info);
5027 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5028 r_symndx, ibfd))
5029 goto error_ret;
5030
5031 if (sym_sec == NULL || sym_sec->owner == NULL)
5032 {
5033 const char *sym_name;
5034 if (h != NULL)
5035 sym_name = h->root.root.string;
5036 else
5037 sym_name = bfd_elf_local_sym_name (ibfd, sym);
5038
5039 (*_bfd_error_handler)
5040 (_("%s: undefined sym `%s' in .opd section"),
5041 bfd_archive_filename (ibfd),
5042 sym_name);
5043 need_edit = FALSE;
5044 break;
5045 }
5046
5047 /* opd entries are always for functions defined in the
5048 current input bfd. If the symbol isn't defined in the
5049 input bfd, then we won't be using the function in this
5050 bfd; It must be defined in a linkonce section in another
5051 bfd, or is weak. It's also possible that we are
5052 discarding the function due to a linker script /DISCARD/,
5053 which we test for via the output_section. */
5054 if (sym_sec->owner != ibfd
5055 || sym_sec->output_section == bfd_abs_section_ptr)
5056 need_edit = TRUE;
5057
5058 offset += 24;
5059 rel += 2;
5060 /* Allow for the possibility of a reloc on the third word. */
5061 if (rel < relend
5062 && rel->r_offset == offset - 8)
5063 rel += 1;
5064 }
5065
5066 if (need_edit)
5067 {
5068 Elf_Internal_Rela *write_rel;
5069 bfd_byte *rptr, *wptr;
5070 bfd_boolean skip;
5071
5072 /* This seems a waste of time as input .opd sections are all
5073 zeros as generated by gcc, but I suppose there's no reason
5074 this will always be so. We might start putting something in
5075 the third word of .opd entries. */
5076 if ((sec->flags & SEC_IN_MEMORY) == 0)
5077 {
5078 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
5079 if (loc == NULL
5080 || !bfd_get_section_contents (ibfd, sec, loc, 0,
5081 sec->_raw_size))
5082 {
5083 error_ret:
5084 if (local_syms != NULL
5085 && symtab_hdr->contents != (unsigned char *) local_syms)
5086 free (local_syms);
5087 if (elf_section_data (sec)->relocs != relstart)
5088 free (relstart);
5089 return FALSE;
5090 }
5091 sec->contents = loc;
5092 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
5093 }
5094
5095 elf_section_data (sec)->relocs = relstart;
5096
5097 wptr = sec->contents;
5098 rptr = sec->contents;
5099 write_rel = relstart;
5100 skip = FALSE;
5101 offset = 0;
5102 for (rel = relstart; rel < relend; rel++)
5103 {
5104 unsigned long r_symndx;
5105 asection *sym_sec;
5106 struct elf_link_hash_entry *h;
5107 Elf_Internal_Sym *sym;
5108
5109 r_symndx = ELF64_R_SYM (rel->r_info);
5110 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5111 r_symndx, ibfd))
5112 goto error_ret;
5113
5114 if (rel->r_offset == offset)
5115 {
5116 struct ppc_link_hash_entry *fdh = NULL;
5117 if (h != NULL)
5118 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
5119 ppc_hash_table (info));
5120
5121 skip = (sym_sec->owner != ibfd
5122 || sym_sec->output_section == bfd_abs_section_ptr);
5123 if (skip)
5124 {
5125 if (h != NULL && sym_sec->owner == ibfd)
5126 {
5127 /* Arrange for the function descriptor sym
5128 to be dropped. */
5129 fdh->elf.root.u.def.value = 0;
5130 fdh->elf.root.u.def.section = sym_sec;
5131 }
5132 }
5133 else
5134 {
5135 /* We'll be keeping this opd entry. */
5136
5137 if (h != NULL)
5138 {
5139 /* Redefine the function descriptor symbol to
5140 this location in the opd section. It is
5141 necessary to update the value here rather
5142 than using an array of adjustments as we do
5143 for local symbols, because various places
5144 in the generic ELF code use the value
5145 stored in u.def.value. */
5146 fdh->elf.root.u.def.value = wptr - sec->contents;
5147 fdh->adjust_done = 1;
5148 }
5149
5150 /* Local syms are a bit tricky. We could
5151 tweak them as they can be cached, but
5152 we'd need to look through the local syms
5153 for the function descriptor sym which we
5154 don't have at the moment. So keep an
5155 array of adjustments. */
5156 adjust[rel->r_offset / 24] = wptr - rptr;
5157
5158 if (wptr != rptr)
5159 memcpy (wptr, rptr, 24);
5160 wptr += 24;
5161 }
5162 rptr += 24;
5163 offset += 24;
5164 }
5165
5166 if (skip)
5167 {
5168 BFD_ASSERT (MUST_BE_DYN_RELOC (ELF64_R_TYPE (rel->r_info)));
5169 if (info->shared)
5170 {
5171 /* We won't be needing dynamic relocs here. */
5172 struct ppc_dyn_relocs **pp;
5173 struct ppc_dyn_relocs *p;
5174
5175 if (h != NULL)
5176 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5177 else if (sym_sec != NULL)
5178 pp = ((struct ppc_dyn_relocs **)
5179 &elf_section_data (sym_sec)->local_dynrel);
5180 else
5181 pp = ((struct ppc_dyn_relocs **)
5182 &elf_section_data (sec)->local_dynrel);
5183 while ((p = *pp) != NULL)
5184 {
5185 if (p->sec == sec)
5186 {
5187 p->count -= 1;
5188 if (p->count == 0)
5189 *pp = p->next;
5190 break;
5191 }
5192 pp = &p->next;
5193 }
5194 }
5195 }
5196 else
5197 {
5198 /* We need to adjust any reloc offsets to point to the
5199 new opd entries. While we're at it, we may as well
5200 remove redundant relocs. */
5201 rel->r_offset += wptr - rptr;
5202 if (write_rel != rel)
5203 memcpy (write_rel, rel, sizeof (*rel));
5204 ++write_rel;
5205 }
5206 }
5207
5208 sec->_cooked_size = wptr - sec->contents;
5209 sec->reloc_count = write_rel - relstart;
5210 /* Fudge the size too, as this is used later in
5211 elf_bfd_final_link if we are emitting relocs. */
5212 elf_section_data (sec)->rel_hdr.sh_size
5213 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5214 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
5215 some_edited = TRUE;
5216 }
5217 else if (elf_section_data (sec)->relocs != relstart)
5218 free (relstart);
5219
5220 if (local_syms != NULL
5221 && symtab_hdr->contents != (unsigned char *) local_syms)
5222 {
5223 if (!info->keep_memory)
5224 free (local_syms);
5225 else
5226 symtab_hdr->contents = (unsigned char *) local_syms;
5227 }
5228 }
5229
5230 if (some_edited)
5231 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
5232
5233 return TRUE;
5234 }
5235
5236 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
5237
5238 asection *
5239 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
5240 {
5241 struct ppc_link_hash_table *htab;
5242
5243 htab = ppc_hash_table (info);
5244 if (htab->tls_get_addr != NULL)
5245 {
5246 struct elf_link_hash_entry *h = htab->tls_get_addr;
5247
5248 while (h->root.type == bfd_link_hash_indirect
5249 || h->root.type == bfd_link_hash_warning)
5250 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5251
5252 htab->tls_get_addr = h;
5253 }
5254
5255 return _bfd_elf_tls_setup (obfd, info);
5256 }
5257
5258 /* Run through all the TLS relocs looking for optimization
5259 opportunities. The linker has been hacked (see ppc64elf.em) to do
5260 a preliminary section layout so that we know the TLS segment
5261 offsets. We can't optimize earlier because some optimizations need
5262 to know the tp offset, and we need to optimize before allocating
5263 dynamic relocations. */
5264
5265 bfd_boolean
5266 ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
5267 {
5268 bfd *ibfd;
5269 asection *sec;
5270 struct ppc_link_hash_table *htab;
5271
5272 if (info->relocatable || info->shared)
5273 return TRUE;
5274
5275 htab = ppc_hash_table (info);
5276 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5277 {
5278 Elf_Internal_Sym *locsyms = NULL;
5279
5280 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5281 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5282 {
5283 Elf_Internal_Rela *relstart, *rel, *relend;
5284 int expecting_tls_get_addr;
5285
5286 /* Read the relocations. */
5287 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
5288 info->keep_memory);
5289 if (relstart == NULL)
5290 return FALSE;
5291
5292 expecting_tls_get_addr = 0;
5293 relend = relstart + sec->reloc_count;
5294 for (rel = relstart; rel < relend; rel++)
5295 {
5296 enum elf_ppc64_reloc_type r_type;
5297 unsigned long r_symndx;
5298 struct elf_link_hash_entry *h;
5299 Elf_Internal_Sym *sym;
5300 asection *sym_sec;
5301 char *tls_mask;
5302 char tls_set, tls_clear, tls_type = 0;
5303 bfd_vma value;
5304 bfd_boolean ok_tprel, is_local;
5305
5306 r_symndx = ELF64_R_SYM (rel->r_info);
5307 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
5308 r_symndx, ibfd))
5309 {
5310 err_free_rel:
5311 if (elf_section_data (sec)->relocs != relstart)
5312 free (relstart);
5313 if (locsyms != NULL
5314 && (elf_tdata (ibfd)->symtab_hdr.contents
5315 != (unsigned char *) locsyms))
5316 free (locsyms);
5317 return FALSE;
5318 }
5319
5320 if (h != NULL)
5321 {
5322 if (h->root.type != bfd_link_hash_defined
5323 && h->root.type != bfd_link_hash_defweak)
5324 continue;
5325 value = h->root.u.def.value;
5326 }
5327 else
5328 {
5329 value = sym->st_value;
5330
5331 if (elf_section_data (sym_sec) != NULL)
5332 {
5333 long *adjust;
5334 adjust = ppc64_elf_section_data (sym_sec)->opd.adjust;
5335 if (adjust != NULL)
5336 value += adjust[value / 24];
5337 }
5338 }
5339
5340 ok_tprel = FALSE;
5341 is_local = FALSE;
5342 if (h == NULL
5343 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5344 {
5345 is_local = TRUE;
5346 value += sym_sec->output_offset;
5347 value += sym_sec->output_section->vma;
5348 value -= htab->elf.tls_sec->vma;
5349 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5350 < (bfd_vma) 1 << 32);
5351 }
5352
5353 r_type = ELF64_R_TYPE (rel->r_info);
5354 switch (r_type)
5355 {
5356 case R_PPC64_GOT_TLSLD16:
5357 case R_PPC64_GOT_TLSLD16_LO:
5358 case R_PPC64_GOT_TLSLD16_HI:
5359 case R_PPC64_GOT_TLSLD16_HA:
5360 /* These relocs should never be against a symbol
5361 defined in a shared lib. Leave them alone if
5362 that turns out to be the case. */
5363 ppc64_tlsld_got (ibfd)->refcount -= 1;
5364 if (!is_local)
5365 continue;
5366
5367 /* LD -> LE */
5368 tls_set = 0;
5369 tls_clear = TLS_LD;
5370 tls_type = TLS_TLS | TLS_LD;
5371 expecting_tls_get_addr = 1;
5372 break;
5373
5374 case R_PPC64_GOT_TLSGD16:
5375 case R_PPC64_GOT_TLSGD16_LO:
5376 case R_PPC64_GOT_TLSGD16_HI:
5377 case R_PPC64_GOT_TLSGD16_HA:
5378 if (ok_tprel)
5379 /* GD -> LE */
5380 tls_set = 0;
5381 else
5382 /* GD -> IE */
5383 tls_set = TLS_TLS | TLS_TPRELGD;
5384 tls_clear = TLS_GD;
5385 tls_type = TLS_TLS | TLS_GD;
5386 expecting_tls_get_addr = 1;
5387 break;
5388
5389 case R_PPC64_GOT_TPREL16_DS:
5390 case R_PPC64_GOT_TPREL16_LO_DS:
5391 case R_PPC64_GOT_TPREL16_HI:
5392 case R_PPC64_GOT_TPREL16_HA:
5393 expecting_tls_get_addr = 0;
5394 if (ok_tprel)
5395 {
5396 /* IE -> LE */
5397 tls_set = 0;
5398 tls_clear = TLS_TPREL;
5399 tls_type = TLS_TLS | TLS_TPREL;
5400 break;
5401 }
5402 else
5403 continue;
5404
5405 case R_PPC64_REL14:
5406 case R_PPC64_REL14_BRTAKEN:
5407 case R_PPC64_REL14_BRNTAKEN:
5408 case R_PPC64_REL24:
5409 if (h != NULL
5410 && h == htab->tls_get_addr)
5411 {
5412 if (!expecting_tls_get_addr
5413 && rel != relstart
5414 && ((ELF64_R_TYPE (rel[-1].r_info)
5415 == R_PPC64_TOC16)
5416 || (ELF64_R_TYPE (rel[-1].r_info)
5417 == R_PPC64_TOC16_LO)))
5418 {
5419 /* Check for toc tls entries. */
5420 char *toc_tls;
5421 int retval;
5422
5423 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
5424 rel - 1, ibfd);
5425 if (retval == 0)
5426 goto err_free_rel;
5427 if (toc_tls != NULL)
5428 expecting_tls_get_addr = retval > 1;
5429 }
5430
5431 if (expecting_tls_get_addr)
5432 {
5433 struct plt_entry *ent;
5434 for (ent = h->plt.plist; ent; ent = ent->next)
5435 if (ent->addend == 0)
5436 {
5437 if (ent->plt.refcount > 0)
5438 ent->plt.refcount -= 1;
5439 break;
5440 }
5441 }
5442 }
5443 expecting_tls_get_addr = 0;
5444 continue;
5445
5446 case R_PPC64_TPREL64:
5447 expecting_tls_get_addr = 0;
5448 if (ok_tprel)
5449 {
5450 /* IE -> LE */
5451 tls_set = TLS_EXPLICIT;
5452 tls_clear = TLS_TPREL;
5453 break;
5454 }
5455 else
5456 continue;
5457
5458 case R_PPC64_DTPMOD64:
5459 expecting_tls_get_addr = 0;
5460 if (rel + 1 < relend
5461 && (rel[1].r_info
5462 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5463 && rel[1].r_offset == rel->r_offset + 8)
5464 {
5465 if (ok_tprel)
5466 /* GD -> LE */
5467 tls_set = TLS_EXPLICIT | TLS_GD;
5468 else
5469 /* GD -> IE */
5470 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
5471 tls_clear = TLS_GD;
5472 }
5473 else
5474 {
5475 if (!is_local)
5476 continue;
5477
5478 /* LD -> LE */
5479 tls_set = TLS_EXPLICIT;
5480 tls_clear = TLS_LD;
5481 }
5482 break;
5483
5484 default:
5485 expecting_tls_get_addr = 0;
5486 continue;
5487 }
5488
5489 if ((tls_set & TLS_EXPLICIT) == 0)
5490 {
5491 struct got_entry *ent;
5492
5493 /* Adjust got entry for this reloc. */
5494 if (h != NULL)
5495 ent = h->got.glist;
5496 else
5497 ent = elf_local_got_ents (ibfd)[r_symndx];
5498
5499 for (; ent != NULL; ent = ent->next)
5500 if (ent->addend == rel->r_addend
5501 && ent->owner == ibfd
5502 && ent->tls_type == tls_type)
5503 break;
5504 if (ent == NULL)
5505 abort ();
5506
5507 if (tls_set == 0)
5508 {
5509 /* We managed to get rid of a got entry. */
5510 if (ent->got.refcount > 0)
5511 ent->got.refcount -= 1;
5512 }
5513 }
5514 else if (h != NULL)
5515 {
5516 struct ppc_link_hash_entry * eh;
5517 struct ppc_dyn_relocs **pp;
5518 struct ppc_dyn_relocs *p;
5519
5520 /* Adjust dynamic relocs. */
5521 eh = (struct ppc_link_hash_entry *) h;
5522 for (pp = &eh->dyn_relocs;
5523 (p = *pp) != NULL;
5524 pp = &p->next)
5525 if (p->sec == sec)
5526 {
5527 /* If we got rid of a DTPMOD/DTPREL reloc
5528 pair then we'll lose one or two dyn
5529 relocs. */
5530 if (tls_set == (TLS_EXPLICIT | TLS_GD))
5531 p->count -= 1;
5532 p->count -= 1;
5533 if (p->count == 0)
5534 *pp = p->next;
5535 break;
5536 }
5537 }
5538
5539 *tls_mask |= tls_set;
5540 *tls_mask &= ~tls_clear;
5541 }
5542
5543 if (elf_section_data (sec)->relocs != relstart)
5544 free (relstart);
5545 }
5546
5547 if (locsyms != NULL
5548 && (elf_tdata (ibfd)->symtab_hdr.contents
5549 != (unsigned char *) locsyms))
5550 {
5551 if (!info->keep_memory)
5552 free (locsyms);
5553 else
5554 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
5555 }
5556 }
5557 return TRUE;
5558 }
5559
5560 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
5561 will be called from elflink.h. If elflink.h doesn't call our
5562 finish_dynamic_symbol routine, we'll need to do something about
5563 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
5564 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
5565 ((DYN) \
5566 && ((SHARED) \
5567 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
5568 && ((H)->dynindx != -1 \
5569 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5570
5571 /* Allocate space in .plt, .got and associated reloc sections for
5572 dynamic relocs. */
5573
5574 static bfd_boolean
5575 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5576 {
5577 struct bfd_link_info *info;
5578 struct ppc_link_hash_table *htab;
5579 asection *s;
5580 struct ppc_link_hash_entry *eh;
5581 struct ppc_dyn_relocs *p;
5582 struct got_entry *gent;
5583
5584 if (h->root.type == bfd_link_hash_indirect)
5585 return TRUE;
5586
5587 if (h->root.type == bfd_link_hash_warning)
5588 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5589
5590 info = (struct bfd_link_info *) inf;
5591 htab = ppc_hash_table (info);
5592
5593 if (htab->elf.dynamic_sections_created
5594 && h->dynindx != -1
5595 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
5596 {
5597 struct plt_entry *pent;
5598 bfd_boolean doneone = FALSE;
5599 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5600 if (pent->plt.refcount > 0)
5601 {
5602 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5603
5604 /* If this is the first .plt entry, make room for the special
5605 first entry. */
5606 s = htab->plt;
5607 if (s->_raw_size == 0)
5608 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5609
5610 pent->plt.offset = s->_raw_size;
5611
5612 /* Make room for this entry. */
5613 s->_raw_size += PLT_ENTRY_SIZE;
5614
5615 /* Make room for the .glink code. */
5616 s = htab->glink;
5617 if (s->_raw_size == 0)
5618 s->_raw_size += GLINK_CALL_STUB_SIZE;
5619 /* We need bigger stubs past index 32767. */
5620 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5621 s->_raw_size += 4;
5622 s->_raw_size += 2*4;
5623
5624 /* We also need to make an entry in the .rela.plt section. */
5625 s = htab->relplt;
5626 s->_raw_size += sizeof (Elf64_External_Rela);
5627 doneone = TRUE;
5628 }
5629 else
5630 pent->plt.offset = (bfd_vma) -1;
5631 if (!doneone)
5632 {
5633 h->plt.plist = NULL;
5634 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5635 }
5636 }
5637 else
5638 {
5639 h->plt.plist = NULL;
5640 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5641 }
5642
5643 eh = (struct ppc_link_hash_entry *) h;
5644 /* Run through the TLS GD got entries first if we're changing them
5645 to TPREL. */
5646 if ((eh->tls_mask & TLS_TPRELGD) != 0)
5647 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5648 if (gent->got.refcount > 0
5649 && (gent->tls_type & TLS_GD) != 0)
5650 {
5651 /* This was a GD entry that has been converted to TPREL. If
5652 there happens to be a TPREL entry we can use that one. */
5653 struct got_entry *ent;
5654 for (ent = h->got.glist; ent != NULL; ent = ent->next)
5655 if (ent->got.refcount > 0
5656 && (ent->tls_type & TLS_TPREL) != 0
5657 && ent->addend == gent->addend
5658 && ent->owner == gent->owner)
5659 {
5660 gent->got.refcount = 0;
5661 break;
5662 }
5663
5664 /* If not, then we'll be using our own TPREL entry. */
5665 if (gent->got.refcount != 0)
5666 gent->tls_type = TLS_TLS | TLS_TPREL;
5667 }
5668
5669 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5670 if (gent->got.refcount > 0)
5671 {
5672 bfd_boolean dyn;
5673
5674 /* Make sure this symbol is output as a dynamic symbol.
5675 Undefined weak syms won't yet be marked as dynamic,
5676 nor will all TLS symbols. */
5677 if (h->dynindx == -1
5678 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5679 {
5680 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5681 return FALSE;
5682 }
5683
5684 if ((gent->tls_type & TLS_LD) != 0
5685 && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5686 {
5687 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
5688 continue;
5689 }
5690
5691 s = ppc64_elf_tdata (gent->owner)->got;
5692 gent->got.offset = s->_raw_size;
5693 s->_raw_size
5694 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
5695 dyn = htab->elf.dynamic_sections_created;
5696 if ((info->shared
5697 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
5698 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5699 || h->root.type != bfd_link_hash_undefweak))
5700 ppc64_elf_tdata (gent->owner)->relgot->_raw_size
5701 += (gent->tls_type & eh->tls_mask & TLS_GD
5702 ? 2 * sizeof (Elf64_External_Rela)
5703 : sizeof (Elf64_External_Rela));
5704 }
5705 else
5706 gent->got.offset = (bfd_vma) -1;
5707
5708 if (eh->dyn_relocs == NULL)
5709 return TRUE;
5710
5711 /* In the shared -Bsymbolic case, discard space allocated for
5712 dynamic pc-relative relocs against symbols which turn out to be
5713 defined in regular objects. For the normal shared case, discard
5714 space for relocs that have become local due to symbol visibility
5715 changes. */
5716
5717 if (info->shared)
5718 {
5719 /* Relocs that use pc_count are those that appear on a call insn,
5720 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
5721 generated via assembly. We want calls to protected symbols to
5722 resolve directly to the function rather than going via the plt.
5723 If people want function pointer comparisons to work as expected
5724 then they should avoid writing weird assembly. */
5725 if (SYMBOL_CALLS_LOCAL (info, h))
5726 {
5727 struct ppc_dyn_relocs **pp;
5728
5729 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5730 {
5731 p->count -= p->pc_count;
5732 p->pc_count = 0;
5733 if (p->count == 0)
5734 *pp = p->next;
5735 else
5736 pp = &p->next;
5737 }
5738 }
5739
5740 /* Also discard relocs on undefined weak syms with non-default
5741 visibility. */
5742 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5743 && h->root.type == bfd_link_hash_undefweak)
5744 eh->dyn_relocs = NULL;
5745 }
5746 else if (ELIMINATE_COPY_RELOCS)
5747 {
5748 /* For the non-shared case, discard space for relocs against
5749 symbols which turn out to need copy relocs or are not
5750 dynamic. */
5751
5752 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5753 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5754 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5755 {
5756 /* Make sure this symbol is output as a dynamic symbol.
5757 Undefined weak syms won't yet be marked as dynamic. */
5758 if (h->dynindx == -1
5759 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5760 {
5761 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5762 return FALSE;
5763 }
5764
5765 /* If that succeeded, we know we'll be keeping all the
5766 relocs. */
5767 if (h->dynindx != -1)
5768 goto keep;
5769 }
5770
5771 eh->dyn_relocs = NULL;
5772
5773 keep: ;
5774 }
5775
5776 /* Finally, allocate space. */
5777 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5778 {
5779 asection *sreloc = elf_section_data (p->sec)->sreloc;
5780 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5781 }
5782
5783 return TRUE;
5784 }
5785
5786 /* Find any dynamic relocs that apply to read-only sections. */
5787
5788 static bfd_boolean
5789 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5790 {
5791 struct ppc_link_hash_entry *eh;
5792 struct ppc_dyn_relocs *p;
5793
5794 if (h->root.type == bfd_link_hash_warning)
5795 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5796
5797 eh = (struct ppc_link_hash_entry *) h;
5798 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5799 {
5800 asection *s = p->sec->output_section;
5801
5802 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5803 {
5804 struct bfd_link_info *info = inf;
5805
5806 info->flags |= DF_TEXTREL;
5807
5808 /* Not an error, just cut short the traversal. */
5809 return FALSE;
5810 }
5811 }
5812 return TRUE;
5813 }
5814
5815 /* Set the sizes of the dynamic sections. */
5816
5817 static bfd_boolean
5818 ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5819 struct bfd_link_info *info)
5820 {
5821 struct ppc_link_hash_table *htab;
5822 bfd *dynobj;
5823 asection *s;
5824 bfd_boolean relocs;
5825 bfd *ibfd;
5826
5827 htab = ppc_hash_table (info);
5828 dynobj = htab->elf.dynobj;
5829 if (dynobj == NULL)
5830 abort ();
5831
5832 if (htab->elf.dynamic_sections_created)
5833 {
5834 /* Set the contents of the .interp section to the interpreter. */
5835 if (info->executable)
5836 {
5837 s = bfd_get_section_by_name (dynobj, ".interp");
5838 if (s == NULL)
5839 abort ();
5840 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5841 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5842 }
5843 }
5844
5845 /* Set up .got offsets for local syms, and space for local dynamic
5846 relocs. */
5847 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5848 {
5849 struct got_entry **lgot_ents;
5850 struct got_entry **end_lgot_ents;
5851 char *lgot_masks;
5852 bfd_size_type locsymcount;
5853 Elf_Internal_Shdr *symtab_hdr;
5854 asection *srel;
5855
5856 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5857 continue;
5858
5859 if (ppc64_tlsld_got (ibfd)->refcount > 0)
5860 {
5861 s = ppc64_elf_tdata (ibfd)->got;
5862 ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
5863 s->_raw_size += 16;
5864 if (info->shared)
5865 {
5866 srel = ppc64_elf_tdata (ibfd)->relgot;
5867 srel->_raw_size += sizeof (Elf64_External_Rela);
5868 }
5869 }
5870 else
5871 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
5872
5873 for (s = ibfd->sections; s != NULL; s = s->next)
5874 {
5875 struct ppc_dyn_relocs *p;
5876
5877 for (p = *((struct ppc_dyn_relocs **)
5878 &elf_section_data (s)->local_dynrel);
5879 p != NULL;
5880 p = p->next)
5881 {
5882 if (!bfd_is_abs_section (p->sec)
5883 && bfd_is_abs_section (p->sec->output_section))
5884 {
5885 /* Input section has been discarded, either because
5886 it is a copy of a linkonce section or due to
5887 linker script /DISCARD/, so we'll be discarding
5888 the relocs too. */
5889 }
5890 else if (p->count != 0)
5891 {
5892 srel = elf_section_data (p->sec)->sreloc;
5893 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
5894 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5895 info->flags |= DF_TEXTREL;
5896 }
5897 }
5898 }
5899
5900 lgot_ents = elf_local_got_ents (ibfd);
5901 if (!lgot_ents)
5902 continue;
5903
5904 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5905 locsymcount = symtab_hdr->sh_info;
5906 end_lgot_ents = lgot_ents + locsymcount;
5907 lgot_masks = (char *) end_lgot_ents;
5908 s = ppc64_elf_tdata (ibfd)->got;
5909 srel = ppc64_elf_tdata (ibfd)->relgot;
5910 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
5911 {
5912 struct got_entry *ent;
5913
5914 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5915 if (ent->got.refcount > 0)
5916 {
5917 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
5918 {
5919 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
5920 {
5921 ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
5922 s->_raw_size += 16;
5923 if (info->shared)
5924 srel->_raw_size += sizeof (Elf64_External_Rela);
5925 }
5926 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
5927 }
5928 else
5929 {
5930 ent->got.offset = s->_raw_size;
5931 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
5932 {
5933 s->_raw_size += 16;
5934 if (info->shared)
5935 srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5936 }
5937 else
5938 {
5939 s->_raw_size += 8;
5940 if (info->shared)
5941 srel->_raw_size += sizeof (Elf64_External_Rela);
5942 }
5943 }
5944 }
5945 else
5946 ent->got.offset = (bfd_vma) -1;
5947 }
5948 }
5949
5950 /* Allocate global sym .plt and .got entries, and space for global
5951 sym dynamic relocs. */
5952 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
5953
5954 /* We now have determined the sizes of the various dynamic sections.
5955 Allocate memory for them. */
5956 relocs = FALSE;
5957 for (s = dynobj->sections; s != NULL; s = s->next)
5958 {
5959 if ((s->flags & SEC_LINKER_CREATED) == 0)
5960 continue;
5961
5962 /* Reset _cooked_size since prelim layout will set it wrongly,
5963 and a non-zero _cooked_size sticks. */
5964 s->_cooked_size = 0;
5965
5966 if (s == htab->brlt || s == htab->relbrlt)
5967 /* These haven't been allocated yet; don't strip. */
5968 continue;
5969 else if (s == htab->got
5970 || s == htab->plt
5971 || s == htab->glink)
5972 {
5973 /* Strip this section if we don't need it; see the
5974 comment below. */
5975 }
5976 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5977 {
5978 if (s->_raw_size == 0)
5979 {
5980 /* If we don't need this section, strip it from the
5981 output file. This is mostly to handle .rela.bss and
5982 .rela.plt. We must create both sections in
5983 create_dynamic_sections, because they must be created
5984 before the linker maps input sections to output
5985 sections. The linker does that before
5986 adjust_dynamic_symbol is called, and it is that
5987 function which decides whether anything needs to go
5988 into these sections. */
5989 }
5990 else
5991 {
5992 if (s != htab->relplt)
5993 relocs = TRUE;
5994
5995 /* We use the reloc_count field as a counter if we need
5996 to copy relocs into the output file. */
5997 s->reloc_count = 0;
5998 }
5999 }
6000 else
6001 {
6002 /* It's not one of our sections, so don't allocate space. */
6003 continue;
6004 }
6005
6006 if (s->_raw_size == 0)
6007 {
6008 _bfd_strip_section_from_output (info, s);
6009 continue;
6010 }
6011
6012 /* .plt is in the bss section. We don't initialise it. */
6013 if (s == htab->plt)
6014 continue;
6015
6016 /* Allocate memory for the section contents. We use bfd_zalloc
6017 here in case unused entries are not reclaimed before the
6018 section's contents are written out. This should not happen,
6019 but this way if it does we get a R_PPC64_NONE reloc in .rela
6020 sections instead of garbage.
6021 We also rely on the section contents being zero when writing
6022 the GOT. */
6023 s->contents = bfd_zalloc (dynobj, s->_raw_size);
6024 if (s->contents == NULL)
6025 return FALSE;
6026 }
6027
6028 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6029 {
6030 s = ppc64_elf_tdata (ibfd)->got;
6031 if (s != NULL && s != htab->got)
6032 {
6033 s->_cooked_size = 0;
6034 if (s->_raw_size == 0)
6035 _bfd_strip_section_from_output (info, s);
6036 else
6037 {
6038 s->contents = bfd_zalloc (ibfd, s->_raw_size);
6039 if (s->contents == NULL)
6040 return FALSE;
6041 }
6042 }
6043 s = ppc64_elf_tdata (ibfd)->relgot;
6044 if (s != NULL)
6045 {
6046 s->_cooked_size = 0;
6047 if (s->_raw_size == 0)
6048 _bfd_strip_section_from_output (info, s);
6049 else
6050 {
6051 s->contents = bfd_zalloc (ibfd, s->_raw_size);
6052 if (s->contents == NULL)
6053 return FALSE;
6054 relocs = TRUE;
6055 s->reloc_count = 0;
6056 }
6057 }
6058 }
6059
6060 if (htab->elf.dynamic_sections_created)
6061 {
6062 /* Add some entries to the .dynamic section. We fill in the
6063 values later, in ppc64_elf_finish_dynamic_sections, but we
6064 must add the entries now so that we get the correct size for
6065 the .dynamic section. The DT_DEBUG entry is filled in by the
6066 dynamic linker and used by the debugger. */
6067 #define add_dynamic_entry(TAG, VAL) \
6068 bfd_elf64_add_dynamic_entry (info, (TAG), (VAL))
6069
6070 if (info->executable)
6071 {
6072 if (!add_dynamic_entry (DT_DEBUG, 0))
6073 return FALSE;
6074 }
6075
6076 if (htab->plt != NULL && htab->plt->_raw_size != 0)
6077 {
6078 if (!add_dynamic_entry (DT_PLTGOT, 0)
6079 || !add_dynamic_entry (DT_PLTRELSZ, 0)
6080 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
6081 || !add_dynamic_entry (DT_JMPREL, 0)
6082 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
6083 return FALSE;
6084 }
6085
6086 if (NO_OPD_RELOCS)
6087 {
6088 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
6089 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
6090 return FALSE;
6091 }
6092
6093 if (relocs)
6094 {
6095 if (!add_dynamic_entry (DT_RELA, 0)
6096 || !add_dynamic_entry (DT_RELASZ, 0)
6097 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
6098 return FALSE;
6099
6100 /* If any dynamic relocs apply to a read-only section,
6101 then we need a DT_TEXTREL entry. */
6102 if ((info->flags & DF_TEXTREL) == 0)
6103 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
6104
6105 if ((info->flags & DF_TEXTREL) != 0)
6106 {
6107 if (!add_dynamic_entry (DT_TEXTREL, 0))
6108 return FALSE;
6109 }
6110 }
6111 }
6112 #undef add_dynamic_entry
6113
6114 return TRUE;
6115 }
6116
6117 /* Determine the type of stub needed, if any, for a call. */
6118
6119 static inline enum ppc_stub_type
6120 ppc_type_of_stub (asection *input_sec,
6121 const Elf_Internal_Rela *rel,
6122 struct ppc_link_hash_entry **hash,
6123 bfd_vma destination)
6124 {
6125 struct ppc_link_hash_entry *h = *hash;
6126 bfd_vma location;
6127 bfd_vma branch_offset;
6128 bfd_vma max_branch_offset;
6129 enum elf_ppc64_reloc_type r_type;
6130
6131 if (h != NULL)
6132 {
6133 if (h->oh != NULL
6134 && h->oh->dynindx != -1)
6135 {
6136 struct plt_entry *ent;
6137 for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
6138 if (ent->addend == rel->r_addend
6139 && ent->plt.offset != (bfd_vma) -1)
6140 {
6141 *hash = (struct ppc_link_hash_entry *) h->oh;
6142 return ppc_stub_plt_call;
6143 }
6144 }
6145
6146 if (!(h->elf.root.type == bfd_link_hash_defined
6147 || h->elf.root.type == bfd_link_hash_defweak)
6148 || h->elf.root.u.def.section->output_section == NULL)
6149 return ppc_stub_none;
6150 }
6151
6152 /* Determine where the call point is. */
6153 location = (input_sec->output_offset
6154 + input_sec->output_section->vma
6155 + rel->r_offset);
6156
6157 branch_offset = destination - location;
6158 r_type = ELF64_R_TYPE (rel->r_info);
6159
6160 /* Determine if a long branch stub is needed. */
6161 max_branch_offset = 1 << 25;
6162 if (r_type != R_PPC64_REL24)
6163 max_branch_offset = 1 << 15;
6164
6165 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
6166 /* We need a stub. Figure out whether a long_branch or plt_branch
6167 is needed later. */
6168 return ppc_stub_long_branch;
6169
6170 return ppc_stub_none;
6171 }
6172
6173 /* Build a .plt call stub. */
6174
6175 static inline bfd_byte *
6176 build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
6177 {
6178 #define PPC_LO(v) ((v) & 0xffff)
6179 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6180 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6181
6182 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
6183 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
6184 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6185 if (PPC_HA (offset + 8) != PPC_HA (offset))
6186 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
6187 offset += 8;
6188 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
6189 if (PPC_HA (offset + 8) != PPC_HA (offset))
6190 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
6191 offset += 8;
6192 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
6193 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6194 bfd_put_32 (obfd, BCTR, p), p += 4;
6195 return p;
6196 }
6197
6198 static bfd_boolean
6199 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
6200 {
6201 struct ppc_stub_hash_entry *stub_entry;
6202 struct ppc_branch_hash_entry *br_entry;
6203 struct bfd_link_info *info;
6204 struct ppc_link_hash_table *htab;
6205 bfd_byte *loc;
6206 bfd_byte *p;
6207 unsigned int indx;
6208 struct plt_entry *ent;
6209 bfd_vma off;
6210 int size;
6211
6212 /* Massage our args to the form they really have. */
6213 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6214 info = in_arg;
6215
6216 htab = ppc_hash_table (info);
6217
6218 /* Make a note of the offset within the stubs for this entry. */
6219 stub_entry->stub_offset = stub_entry->stub_sec->_cooked_size;
6220 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
6221
6222 htab->stub_count[stub_entry->stub_type - 1] += 1;
6223 switch (stub_entry->stub_type)
6224 {
6225 case ppc_stub_long_branch:
6226 case ppc_stub_long_branch_r2off:
6227 /* Branches are relative. This is where we are going to. */
6228 off = (stub_entry->target_value
6229 + stub_entry->target_section->output_offset
6230 + stub_entry->target_section->output_section->vma);
6231
6232 /* And this is where we are coming from. */
6233 off -= (stub_entry->stub_offset
6234 + stub_entry->stub_sec->output_offset
6235 + stub_entry->stub_sec->output_section->vma);
6236
6237 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
6238 size = 4;
6239 else
6240 {
6241 bfd_vma r2off;
6242
6243 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6244 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6245 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
6246 loc += 4;
6247 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
6248 loc += 4;
6249 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
6250 loc += 4;
6251 off -= 12;
6252 size = 16;
6253 }
6254 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
6255
6256 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
6257 break;
6258
6259 case ppc_stub_plt_branch:
6260 case ppc_stub_plt_branch_r2off:
6261 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6262 stub_entry->root.string + 9,
6263 FALSE, FALSE);
6264 if (br_entry == NULL)
6265 {
6266 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6267 stub_entry->root.string + 9);
6268 htab->stub_error = TRUE;
6269 return FALSE;
6270 }
6271
6272 off = (stub_entry->target_value
6273 + stub_entry->target_section->output_offset
6274 + stub_entry->target_section->output_section->vma);
6275
6276 bfd_put_64 (htab->brlt->owner, off,
6277 htab->brlt->contents + br_entry->offset);
6278
6279 if (info->shared)
6280 {
6281 /* Create a reloc for the branch lookup table entry. */
6282 Elf_Internal_Rela rela;
6283 bfd_byte *rl;
6284
6285 rela.r_offset = (br_entry->offset
6286 + htab->brlt->output_offset
6287 + htab->brlt->output_section->vma);
6288 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6289 rela.r_addend = off;
6290
6291 rl = htab->relbrlt->contents;
6292 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6293 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
6294 }
6295
6296 off = (br_entry->offset
6297 + htab->brlt->output_offset
6298 + htab->brlt->output_section->vma
6299 - elf_gp (htab->brlt->output_section->owner)
6300 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6301
6302 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6303 {
6304 (*_bfd_error_handler)
6305 (_("linkage table error against `%s'"),
6306 stub_entry->root.string);
6307 bfd_set_error (bfd_error_bad_value);
6308 htab->stub_error = TRUE;
6309 return FALSE;
6310 }
6311
6312 indx = off;
6313 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
6314 {
6315 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
6316 loc += 4;
6317 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
6318 size = 16;
6319 }
6320 else
6321 {
6322 bfd_vma r2off;
6323
6324 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
6325 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6326 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
6327 loc += 4;
6328 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
6329 loc += 4;
6330 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
6331 loc += 4;
6332 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
6333 loc += 4;
6334 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
6335 size = 28;
6336 }
6337 loc += 4;
6338 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
6339 loc += 4;
6340 bfd_put_32 (htab->stub_bfd, BCTR, loc);
6341 break;
6342
6343 case ppc_stub_plt_call:
6344 /* Do the best we can for shared libraries built without
6345 exporting ".foo" for each "foo". This can happen when symbol
6346 versioning scripts strip all bar a subset of symbols. */
6347 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6348 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6349 {
6350 /* Point the symbol at the stub. There may be multiple stubs,
6351 we don't really care; The main thing is to make this sym
6352 defined somewhere. Maybe defining the symbol in the stub
6353 section is a silly idea. If we didn't do this, htab->top_id
6354 could disappear. */
6355 stub_entry->h->oh->root.type = bfd_link_hash_defined;
6356 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6357 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6358 }
6359
6360 /* Now build the stub. */
6361 off = (bfd_vma) -1;
6362 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6363 if (ent->addend == stub_entry->addend)
6364 {
6365 off = ent->plt.offset;
6366 break;
6367 }
6368 if (off >= (bfd_vma) -2)
6369 abort ();
6370
6371 off &= ~ (bfd_vma) 1;
6372 off += (htab->plt->output_offset
6373 + htab->plt->output_section->vma
6374 - elf_gp (htab->plt->output_section->owner)
6375 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6376
6377 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
6378 {
6379 (*_bfd_error_handler)
6380 (_("linkage table error against `%s'"),
6381 stub_entry->h->elf.root.root.string);
6382 bfd_set_error (bfd_error_bad_value);
6383 htab->stub_error = TRUE;
6384 return FALSE;
6385 }
6386
6387 p = build_plt_stub (htab->stub_bfd, loc, off);
6388 size = p - loc;
6389 break;
6390
6391 default:
6392 BFD_FAIL ();
6393 return FALSE;
6394 }
6395
6396 stub_entry->stub_sec->_cooked_size += size;
6397
6398 if (htab->emit_stub_syms
6399 && !(stub_entry->stub_type == ppc_stub_plt_call
6400 && stub_entry->h->oh->root.type == bfd_link_hash_defined
6401 && stub_entry->h->oh->root.u.def.section == stub_entry->stub_sec
6402 && stub_entry->h->oh->root.u.def.value == stub_entry->stub_offset))
6403 {
6404 struct elf_link_hash_entry *h;
6405 h = elf_link_hash_lookup (&htab->elf, stub_entry->root.string,
6406 TRUE, FALSE, FALSE);
6407 if (h == NULL)
6408 return FALSE;
6409 if (h->root.type == bfd_link_hash_new)
6410 {
6411 h->root.type = bfd_link_hash_defined;
6412 h->root.u.def.section = stub_entry->stub_sec;
6413 h->root.u.def.value = stub_entry->stub_offset;
6414 h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
6415 | ELF_LINK_HASH_DEF_REGULAR
6416 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
6417 | ELF_LINK_FORCED_LOCAL);
6418 }
6419 }
6420
6421 return TRUE;
6422 }
6423
6424 /* As above, but don't actually build the stub. Just bump offset so
6425 we know stub section sizes, and select plt_branch stubs where
6426 long_branch stubs won't do. */
6427
6428 static bfd_boolean
6429 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
6430 {
6431 struct ppc_stub_hash_entry *stub_entry;
6432 struct bfd_link_info *info;
6433 struct ppc_link_hash_table *htab;
6434 bfd_vma off;
6435 int size;
6436
6437 /* Massage our args to the form they really have. */
6438 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6439 info = in_arg;
6440
6441 htab = ppc_hash_table (info);
6442
6443 if (stub_entry->stub_type == ppc_stub_plt_call)
6444 {
6445 struct plt_entry *ent;
6446 off = (bfd_vma) -1;
6447 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6448 if (ent->addend == stub_entry->addend)
6449 {
6450 off = ent->plt.offset & ~(bfd_vma) 1;
6451 break;
6452 }
6453 if (off >= (bfd_vma) -2)
6454 abort ();
6455 off += (htab->plt->output_offset
6456 + htab->plt->output_section->vma
6457 - elf_gp (htab->plt->output_section->owner)
6458 - htab->stub_group[stub_entry->id_sec->id].toc_off);
6459
6460 size = PLT_CALL_STUB_SIZE;
6461 if (PPC_HA (off + 16) != PPC_HA (off))
6462 size += 4;
6463 }
6464 else
6465 {
6466 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
6467 variants. */
6468 off = (stub_entry->target_value
6469 + stub_entry->target_section->output_offset
6470 + stub_entry->target_section->output_section->vma);
6471 off -= (stub_entry->stub_sec->_raw_size
6472 + stub_entry->stub_sec->output_offset
6473 + stub_entry->stub_sec->output_section->vma);
6474
6475 /* Reset the stub type from the plt variant in case we now
6476 can reach with a shorter stub. */
6477 if (stub_entry->stub_type >= ppc_stub_plt_branch)
6478 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
6479
6480 size = 4;
6481 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
6482 {
6483 off -= 12;
6484 size = 16;
6485 }
6486
6487 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
6488 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6489 {
6490 struct ppc_branch_hash_entry *br_entry;
6491
6492 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6493 stub_entry->root.string + 9,
6494 TRUE, FALSE);
6495 if (br_entry == NULL)
6496 {
6497 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6498 stub_entry->root.string + 9);
6499 htab->stub_error = TRUE;
6500 return FALSE;
6501 }
6502
6503 if (br_entry->iter != htab->stub_iteration)
6504 {
6505 br_entry->iter = htab->stub_iteration;
6506 br_entry->offset = htab->brlt->_raw_size;
6507 htab->brlt->_raw_size += 8;
6508
6509 if (info->shared)
6510 htab->relbrlt->_raw_size += sizeof (Elf64_External_Rela);
6511 }
6512
6513 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
6514 size = 16;
6515 if (stub_entry->stub_type != ppc_stub_plt_branch)
6516 size = 28;
6517 }
6518 }
6519
6520 stub_entry->stub_sec->_raw_size += size;
6521 return TRUE;
6522 }
6523
6524 /* Set up various things so that we can make a list of input sections
6525 for each output section included in the link. Returns -1 on error,
6526 0 when no stubs will be needed, and 1 on success. */
6527
6528 int
6529 ppc64_elf_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
6530 {
6531 bfd *input_bfd;
6532 int top_id, top_index, id;
6533 asection *section;
6534 asection **input_list;
6535 bfd_size_type amt;
6536 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6537
6538 if (htab->brlt == NULL)
6539 return 0;
6540
6541 /* Find the top input section id. */
6542 for (input_bfd = info->input_bfds, top_id = 3;
6543 input_bfd != NULL;
6544 input_bfd = input_bfd->link_next)
6545 {
6546 for (section = input_bfd->sections;
6547 section != NULL;
6548 section = section->next)
6549 {
6550 if (top_id < section->id)
6551 top_id = section->id;
6552 }
6553 }
6554
6555 htab->top_id = top_id;
6556 amt = sizeof (struct map_stub) * (top_id + 1);
6557 htab->stub_group = bfd_zmalloc (amt);
6558 if (htab->stub_group == NULL)
6559 return -1;
6560
6561 /* Set toc_off for com, und, abs and ind sections. */
6562 for (id = 0; id < 3; id++)
6563 htab->stub_group[id].toc_off = TOC_BASE_OFF;
6564
6565 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
6566
6567 /* We can't use output_bfd->section_count here to find the top output
6568 section index as some sections may have been removed, and
6569 _bfd_strip_section_from_output doesn't renumber the indices. */
6570 for (section = output_bfd->sections, top_index = 0;
6571 section != NULL;
6572 section = section->next)
6573 {
6574 if (top_index < section->index)
6575 top_index = section->index;
6576 }
6577
6578 htab->top_index = top_index;
6579 amt = sizeof (asection *) * (top_index + 1);
6580 input_list = bfd_zmalloc (amt);
6581 htab->input_list = input_list;
6582 if (input_list == NULL)
6583 return -1;
6584
6585 return 1;
6586 }
6587
6588 /* The linker repeatedly calls this function for each TOC input section
6589 and linker generated GOT section. Group input bfds such that the toc
6590 within a group is less than 64k in size. Will break with cute linker
6591 scripts that play games with dot in the output toc section. */
6592
6593 void
6594 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
6595 {
6596 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6597
6598 if (!htab->no_multi_toc)
6599 {
6600 bfd_vma addr = isec->output_offset + isec->output_section->vma;
6601 bfd_vma off = addr - htab->toc_curr;
6602 if (off + isec->_raw_size > 0x10000)
6603 {
6604 htab->toc_curr = addr;
6605 htab->multi_toc_needed = 1;
6606 }
6607 elf_gp (isec->owner) = (htab->toc_curr
6608 - elf_gp (isec->output_section->owner)
6609 + TOC_BASE_OFF);
6610 }
6611 }
6612
6613 /* Called after the last call to the above function. */
6614
6615 void
6616 ppc64_elf_reinit_toc (bfd *output_bfd ATTRIBUTE_UNUSED,
6617 struct bfd_link_info *info)
6618 {
6619 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6620
6621 /* toc_curr tracks the TOC offset used for code sections below in
6622 ppc64_elf_next_input_section. Start off at 0x8000. */
6623 htab->toc_curr = TOC_BASE_OFF;
6624 }
6625
6626 /* No toc references were found in ISEC. If the code in ISEC makes no
6627 calls, then there's no need to use toc adjusting stubs when branching
6628 into ISEC. Actually, indirect calls from ISEC are OK as they will
6629 load r2. */
6630
6631 static int
6632 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
6633 {
6634 bfd_byte *contents;
6635 bfd_size_type i;
6636 int ret;
6637 int branch_ok;
6638
6639 /* We know none of our code bearing sections will need toc stubs. */
6640 if ((isec->flags & SEC_LINKER_CREATED) != 0)
6641 return 0;
6642
6643 if (isec->_raw_size == 0)
6644 return 0;
6645
6646 /* Hack for linux kernel. .fixup contains branches, but only back to
6647 the function that hit an exception. */
6648 branch_ok = strcmp (isec->name, ".fixup") == 0;
6649
6650 contents = elf_section_data (isec)->this_hdr.contents;
6651 if (contents == NULL)
6652 {
6653 contents = bfd_malloc (isec->_raw_size);
6654 if (contents == NULL)
6655 return -1;
6656 if (! bfd_get_section_contents (isec->owner, isec, contents,
6657 0, isec->_raw_size))
6658 {
6659 free (contents);
6660 return -1;
6661 }
6662 if (info->keep_memory)
6663 elf_section_data (isec)->this_hdr.contents = contents;
6664 }
6665
6666 /* Code scan, because we don't necessarily have relocs on calls to
6667 static functions. */
6668 ret = 0;
6669 for (i = 0; i < isec->_raw_size; i += 4)
6670 {
6671 unsigned long insn = bfd_get_32 (isec->owner, contents + i);
6672 /* Is this a branch? */
6673 if ((insn & (0x3f << 26)) == (18 << 26)
6674 /* If branch and link, it's a function call. */
6675 && ((insn & 1) != 0
6676 /* Sibling calls use a plain branch. I don't know a way
6677 of deciding whether a branch is really a sibling call. */
6678 || !branch_ok))
6679 {
6680 ret = 1;
6681 break;
6682 }
6683 }
6684
6685 if (elf_section_data (isec)->this_hdr.contents != contents)
6686 free (contents);
6687 return ret;
6688 }
6689
6690 /* The linker repeatedly calls this function for each input section,
6691 in the order that input sections are linked into output sections.
6692 Build lists of input sections to determine groupings between which
6693 we may insert linker stubs. */
6694
6695 bfd_boolean
6696 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
6697 {
6698 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6699 int ret;
6700
6701 if ((isec->output_section->flags & SEC_CODE) != 0
6702 && isec->output_section->index <= htab->top_index)
6703 {
6704 asection **list = htab->input_list + isec->output_section->index;
6705 /* Steal the link_sec pointer for our list. */
6706 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6707 /* This happens to make the list in reverse order,
6708 which is what we want. */
6709 PREV_SEC (isec) = *list;
6710 *list = isec;
6711 }
6712
6713 /* If a code section has a function that uses the TOC then we need
6714 to use the right TOC (obviously). Also, make sure that .opd gets
6715 the correct TOC value for R_PPC64_TOC relocs that don't have or
6716 can't find their function symbol (shouldn't ever happen now). */
6717 if (isec->has_gp_reloc || (isec->flags & SEC_CODE) == 0)
6718 {
6719 if (elf_gp (isec->owner) != 0)
6720 htab->toc_curr = elf_gp (isec->owner);
6721 }
6722 else if ((ret = toc_adjusting_stub_needed (info, isec)) < 0)
6723 return FALSE;
6724 else
6725 isec->has_gp_reloc = ret;
6726
6727 /* Functions that don't use the TOC can belong in any TOC group.
6728 Use the last TOC base. This happens to make _init and _fini
6729 pasting work. */
6730 htab->stub_group[isec->id].toc_off = htab->toc_curr;
6731 return TRUE;
6732 }
6733
6734 /* See whether we can group stub sections together. Grouping stub
6735 sections may result in fewer stubs. More importantly, we need to
6736 put all .init* and .fini* stubs at the beginning of the .init or
6737 .fini output sections respectively, because glibc splits the
6738 _init and _fini functions into multiple parts. Putting a stub in
6739 the middle of a function is not a good idea. */
6740
6741 static void
6742 group_sections (struct ppc_link_hash_table *htab,
6743 bfd_size_type stub_group_size,
6744 bfd_boolean stubs_always_before_branch)
6745 {
6746 asection **list = htab->input_list + htab->top_index;
6747 do
6748 {
6749 asection *tail = *list;
6750 while (tail != NULL)
6751 {
6752 asection *curr;
6753 asection *prev;
6754 bfd_size_type total;
6755 bfd_boolean big_sec;
6756 bfd_vma curr_toc;
6757
6758 curr = tail;
6759 if (tail->_cooked_size)
6760 total = tail->_cooked_size;
6761 else
6762 total = tail->_raw_size;
6763 big_sec = total >= stub_group_size;
6764 curr_toc = htab->stub_group[tail->id].toc_off;
6765
6766 while ((prev = PREV_SEC (curr)) != NULL
6767 && ((total += curr->output_offset - prev->output_offset)
6768 < stub_group_size)
6769 && htab->stub_group[prev->id].toc_off == curr_toc)
6770 curr = prev;
6771
6772 /* OK, the size from the start of CURR to the end is less
6773 than stub_group_size and thus can be handled by one stub
6774 section. (or the tail section is itself larger than
6775 stub_group_size, in which case we may be toast.) We
6776 should really be keeping track of the total size of stubs
6777 added here, as stubs contribute to the final output
6778 section size. That's a little tricky, and this way will
6779 only break if stubs added make the total size more than
6780 2^25, ie. for the default stub_group_size, if stubs total
6781 more than 2097152 bytes, or nearly 75000 plt call stubs. */
6782 do
6783 {
6784 prev = PREV_SEC (tail);
6785 /* Set up this stub group. */
6786 htab->stub_group[tail->id].link_sec = curr;
6787 }
6788 while (tail != curr && (tail = prev) != NULL);
6789
6790 /* But wait, there's more! Input sections up to stub_group_size
6791 bytes before the stub section can be handled by it too.
6792 Don't do this if we have a really large section after the
6793 stubs, as adding more stubs increases the chance that
6794 branches may not reach into the stub section. */
6795 if (!stubs_always_before_branch && !big_sec)
6796 {
6797 total = 0;
6798 while (prev != NULL
6799 && ((total += tail->output_offset - prev->output_offset)
6800 < stub_group_size)
6801 && htab->stub_group[prev->id].toc_off == curr_toc)
6802 {
6803 tail = prev;
6804 prev = PREV_SEC (tail);
6805 htab->stub_group[tail->id].link_sec = curr;
6806 }
6807 }
6808 tail = prev;
6809 }
6810 }
6811 while (list-- != htab->input_list);
6812 free (htab->input_list);
6813 #undef PREV_SEC
6814 }
6815
6816 /* Determine and set the size of the stub section for a final link.
6817
6818 The basic idea here is to examine all the relocations looking for
6819 PC-relative calls to a target that is unreachable with a "bl"
6820 instruction. */
6821
6822 bfd_boolean
6823 ppc64_elf_size_stubs (bfd *output_bfd,
6824 struct bfd_link_info *info,
6825 bfd_signed_vma group_size,
6826 asection *(*add_stub_section) (const char *, asection *),
6827 void (*layout_sections_again) (void))
6828 {
6829 bfd_size_type stub_group_size;
6830 bfd_boolean stubs_always_before_branch;
6831 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6832
6833 /* Stash our params away. */
6834 htab->add_stub_section = add_stub_section;
6835 htab->layout_sections_again = layout_sections_again;
6836 stubs_always_before_branch = group_size < 0;
6837 if (group_size < 0)
6838 stub_group_size = -group_size;
6839 else
6840 stub_group_size = group_size;
6841 if (stub_group_size == 1)
6842 {
6843 /* Default values. */
6844 if (stubs_always_before_branch)
6845 {
6846 stub_group_size = 0x1e00000;
6847 if (htab->has_14bit_branch)
6848 stub_group_size = 0x7800;
6849 }
6850 else
6851 {
6852 stub_group_size = 0x1c00000;
6853 if (htab->has_14bit_branch)
6854 stub_group_size = 0x7000;
6855 }
6856 }
6857
6858 group_sections (htab, stub_group_size, stubs_always_before_branch);
6859
6860 while (1)
6861 {
6862 bfd *input_bfd;
6863 unsigned int bfd_indx;
6864 asection *stub_sec;
6865 bfd_boolean stub_changed;
6866
6867 htab->stub_iteration += 1;
6868 stub_changed = FALSE;
6869
6870 for (input_bfd = info->input_bfds, bfd_indx = 0;
6871 input_bfd != NULL;
6872 input_bfd = input_bfd->link_next, bfd_indx++)
6873 {
6874 Elf_Internal_Shdr *symtab_hdr;
6875 asection *section;
6876 Elf_Internal_Sym *local_syms = NULL;
6877
6878 /* We'll need the symbol table in a second. */
6879 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6880 if (symtab_hdr->sh_info == 0)
6881 continue;
6882
6883 /* Walk over each section attached to the input bfd. */
6884 for (section = input_bfd->sections;
6885 section != NULL;
6886 section = section->next)
6887 {
6888 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6889
6890 /* If there aren't any relocs, then there's nothing more
6891 to do. */
6892 if ((section->flags & SEC_RELOC) == 0
6893 || section->reloc_count == 0)
6894 continue;
6895
6896 /* If this section is a link-once section that will be
6897 discarded, then don't create any stubs. */
6898 if (section->output_section == NULL
6899 || section->output_section->owner != output_bfd)
6900 continue;
6901
6902 /* Get the relocs. */
6903 internal_relocs
6904 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
6905 info->keep_memory);
6906 if (internal_relocs == NULL)
6907 goto error_ret_free_local;
6908
6909 /* Now examine each relocation. */
6910 irela = internal_relocs;
6911 irelaend = irela + section->reloc_count;
6912 for (; irela < irelaend; irela++)
6913 {
6914 enum elf_ppc64_reloc_type r_type;
6915 unsigned int r_indx;
6916 enum ppc_stub_type stub_type;
6917 struct ppc_stub_hash_entry *stub_entry;
6918 asection *sym_sec;
6919 bfd_vma sym_value;
6920 bfd_vma destination;
6921 struct ppc_link_hash_entry *hash;
6922 struct elf_link_hash_entry *h;
6923 Elf_Internal_Sym *sym;
6924 char *stub_name;
6925 const asection *id_sec;
6926
6927 r_type = ELF64_R_TYPE (irela->r_info);
6928 r_indx = ELF64_R_SYM (irela->r_info);
6929
6930 if (r_type >= R_PPC64_max)
6931 {
6932 bfd_set_error (bfd_error_bad_value);
6933 goto error_ret_free_internal;
6934 }
6935
6936 /* Only look for stubs on branch instructions. */
6937 if (r_type != R_PPC64_REL24
6938 && r_type != R_PPC64_REL14
6939 && r_type != R_PPC64_REL14_BRTAKEN
6940 && r_type != R_PPC64_REL14_BRNTAKEN)
6941 continue;
6942
6943 /* Now determine the call target, its name, value,
6944 section. */
6945 destination = 0;
6946 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6947 r_indx, input_bfd))
6948 goto error_ret_free_internal;
6949 hash = (struct ppc_link_hash_entry *) h;
6950
6951 if (hash == NULL)
6952 {
6953 /* It's a local symbol. */
6954 sym_value = sym->st_value;
6955 destination = (sym_value + irela->r_addend
6956 + sym_sec->output_offset
6957 + sym_sec->output_section->vma);
6958 }
6959 else
6960 {
6961 /* It's an external symbol. */
6962 sym_value = 0;
6963 if (hash->elf.root.type == bfd_link_hash_defined
6964 || hash->elf.root.type == bfd_link_hash_defweak)
6965 {
6966 sym_value = hash->elf.root.u.def.value;
6967 if (sym_sec->output_section != NULL)
6968 destination = (sym_value + irela->r_addend
6969 + sym_sec->output_offset
6970 + sym_sec->output_section->vma);
6971 }
6972 else if (hash->elf.root.type == bfd_link_hash_undefweak)
6973 ;
6974 else if (hash->elf.root.type == bfd_link_hash_undefined)
6975 ;
6976 else
6977 {
6978 bfd_set_error (bfd_error_bad_value);
6979 goto error_ret_free_internal;
6980 }
6981 }
6982
6983 /* Determine what (if any) linker stub is needed. */
6984 stub_type = ppc_type_of_stub (section, irela, &hash,
6985 destination);
6986
6987 if (stub_type != ppc_stub_plt_call)
6988 {
6989 /* Check whether we need a TOC adjusting stub.
6990 Since the linker pastes together pieces from
6991 different object files when creating the
6992 _init and _fini functions, it may be that a
6993 call to what looks like a local sym is in
6994 fact a call needing a TOC adjustment. */
6995 if (sym_sec != NULL
6996 && sym_sec->output_section != NULL
6997 && (htab->stub_group[sym_sec->id].toc_off
6998 != htab->stub_group[section->id].toc_off)
6999 && sym_sec->has_gp_reloc
7000 && section->has_gp_reloc)
7001 stub_type = ppc_stub_long_branch_r2off;
7002 }
7003
7004 if (stub_type == ppc_stub_none)
7005 continue;
7006
7007 /* __tls_get_addr calls might be eliminated. */
7008 if (stub_type != ppc_stub_plt_call
7009 && hash != NULL
7010 && &hash->elf == htab->tls_get_addr
7011 && section->has_tls_reloc
7012 && irela != internal_relocs)
7013 {
7014 /* Get tls info. */
7015 char *tls_mask;
7016
7017 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
7018 irela - 1, input_bfd))
7019 goto error_ret_free_internal;
7020 if (*tls_mask != 0)
7021 continue;
7022 }
7023
7024 /* Support for grouping stub sections. */
7025 id_sec = htab->stub_group[section->id].link_sec;
7026
7027 /* Get the name of this stub. */
7028 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
7029 if (!stub_name)
7030 goto error_ret_free_internal;
7031
7032 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
7033 stub_name, FALSE, FALSE);
7034 if (stub_entry != NULL)
7035 {
7036 /* The proper stub has already been created. */
7037 free (stub_name);
7038 continue;
7039 }
7040
7041 stub_entry = ppc_add_stub (stub_name, section, htab);
7042 if (stub_entry == NULL)
7043 {
7044 free (stub_name);
7045 error_ret_free_internal:
7046 if (elf_section_data (section)->relocs == NULL)
7047 free (internal_relocs);
7048 error_ret_free_local:
7049 if (local_syms != NULL
7050 && (symtab_hdr->contents
7051 != (unsigned char *) local_syms))
7052 free (local_syms);
7053 return FALSE;
7054 }
7055
7056 stub_entry->stub_type = stub_type;
7057 stub_entry->target_value = sym_value;
7058 stub_entry->target_section = sym_sec;
7059 stub_entry->h = hash;
7060 stub_entry->addend = irela->r_addend;
7061 stub_changed = TRUE;
7062 }
7063
7064 /* We're done with the internal relocs, free them. */
7065 if (elf_section_data (section)->relocs != internal_relocs)
7066 free (internal_relocs);
7067 }
7068
7069 if (local_syms != NULL
7070 && symtab_hdr->contents != (unsigned char *) local_syms)
7071 {
7072 if (!info->keep_memory)
7073 free (local_syms);
7074 else
7075 symtab_hdr->contents = (unsigned char *) local_syms;
7076 }
7077 }
7078
7079 if (!stub_changed)
7080 break;
7081
7082 /* OK, we've added some stubs. Find out the new size of the
7083 stub sections. */
7084 for (stub_sec = htab->stub_bfd->sections;
7085 stub_sec != NULL;
7086 stub_sec = stub_sec->next)
7087 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7088 {
7089 stub_sec->_raw_size = 0;
7090 stub_sec->_cooked_size = 0;
7091 }
7092 htab->brlt->_raw_size = 0;
7093 htab->brlt->_cooked_size = 0;
7094 if (info->shared)
7095 {
7096 htab->relbrlt->_raw_size = 0;
7097 htab->relbrlt->_cooked_size = 0;
7098 }
7099
7100 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
7101
7102 /* Ask the linker to do its stuff. */
7103 (*htab->layout_sections_again) ();
7104 }
7105
7106 /* It would be nice to strip .branch_lt from the output if the
7107 section is empty, but it's too late. If we strip sections here,
7108 the dynamic symbol table is corrupted since the section symbol
7109 for the stripped section isn't written. */
7110
7111 return TRUE;
7112 }
7113
7114 /* Called after we have determined section placement. If sections
7115 move, we'll be called again. Provide a value for TOCstart. */
7116
7117 bfd_vma
7118 ppc64_elf_toc (bfd *obfd)
7119 {
7120 asection *s;
7121 bfd_vma TOCstart;
7122
7123 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
7124 order. The TOC starts where the first of these sections starts. */
7125 s = bfd_get_section_by_name (obfd, ".got");
7126 if (s == NULL)
7127 s = bfd_get_section_by_name (obfd, ".toc");
7128 if (s == NULL)
7129 s = bfd_get_section_by_name (obfd, ".tocbss");
7130 if (s == NULL)
7131 s = bfd_get_section_by_name (obfd, ".plt");
7132 if (s == NULL)
7133 {
7134 /* This may happen for
7135 o references to TOC base (SYM@toc / TOC[tc0]) without a
7136 .toc directive
7137 o bad linker script
7138 o --gc-sections and empty TOC sections
7139
7140 FIXME: Warn user? */
7141
7142 /* Look for a likely section. We probably won't even be
7143 using TOCstart. */
7144 for (s = obfd->sections; s != NULL; s = s->next)
7145 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
7146 == (SEC_ALLOC | SEC_SMALL_DATA))
7147 break;
7148 if (s == NULL)
7149 for (s = obfd->sections; s != NULL; s = s->next)
7150 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
7151 == (SEC_ALLOC | SEC_SMALL_DATA))
7152 break;
7153 if (s == NULL)
7154 for (s = obfd->sections; s != NULL; s = s->next)
7155 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
7156 break;
7157 if (s == NULL)
7158 for (s = obfd->sections; s != NULL; s = s->next)
7159 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
7160 break;
7161 }
7162
7163 TOCstart = 0;
7164 if (s != NULL)
7165 TOCstart = s->output_section->vma + s->output_offset;
7166
7167 return TOCstart;
7168 }
7169
7170 /* Build all the stubs associated with the current output file.
7171 The stubs are kept in a hash table attached to the main linker
7172 hash table. This function is called via gldelf64ppc_finish. */
7173
7174 bfd_boolean
7175 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
7176 struct bfd_link_info *info,
7177 char **stats)
7178 {
7179 struct ppc_link_hash_table *htab = ppc_hash_table (info);
7180 asection *stub_sec;
7181 bfd_byte *p;
7182 int stub_sec_count = 0;
7183
7184 htab->emit_stub_syms = emit_stub_syms;
7185 for (stub_sec = htab->stub_bfd->sections;
7186 stub_sec != NULL;
7187 stub_sec = stub_sec->next)
7188 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7189 {
7190 bfd_size_type size;
7191
7192 /* Allocate memory to hold the linker stubs. */
7193 size = stub_sec->_raw_size;
7194 if (size != 0)
7195 {
7196 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
7197 if (stub_sec->contents == NULL)
7198 return FALSE;
7199 }
7200 stub_sec->_cooked_size = 0;
7201 }
7202
7203 if (htab->plt != NULL)
7204 {
7205 unsigned int indx;
7206 bfd_vma plt0;
7207
7208 /* Build the .glink plt call stub. */
7209 plt0 = (htab->plt->output_section->vma
7210 + htab->plt->output_offset
7211 - (htab->glink->output_section->vma
7212 + htab->glink->output_offset
7213 + GLINK_CALL_STUB_SIZE));
7214 if (plt0 + 0x80008000 > 0xffffffff)
7215 {
7216 (*_bfd_error_handler) (_(".glink and .plt too far apart"));
7217 bfd_set_error (bfd_error_bad_value);
7218 return FALSE;
7219 }
7220
7221 if (htab->emit_stub_syms)
7222 {
7223 struct elf_link_hash_entry *h;
7224 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
7225 if (h == NULL)
7226 return FALSE;
7227 if (h->root.type == bfd_link_hash_new)
7228 {
7229 h->root.type = bfd_link_hash_defined;
7230 h->root.u.def.section = htab->glink;
7231 h->root.u.def.value = 0;
7232 h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
7233 | ELF_LINK_HASH_DEF_REGULAR
7234 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
7235 | ELF_LINK_FORCED_LOCAL);
7236 }
7237 }
7238 p = htab->glink->contents;
7239 bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
7240 p += 4;
7241 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_3, p);
7242 p += 4;
7243 bfd_put_32 (htab->glink->owner, ADDIC_R2_R0_32K, p);
7244 p += 4;
7245 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
7246 p += 4;
7247 bfd_put_32 (htab->glink->owner, SRADI_R2_R2_63, p);
7248 p += 4;
7249 bfd_put_32 (htab->glink->owner, SLDI_R11_R0_2, p);
7250 p += 4;
7251 bfd_put_32 (htab->glink->owner, AND_R2_R2_R11, p);
7252 p += 4;
7253 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
7254 p += 4;
7255 bfd_put_32 (htab->glink->owner, ADD_R12_R12_R2, p);
7256 p += 4;
7257 bfd_put_32 (htab->glink->owner, ADDIS_R12_R12 | PPC_HA (plt0), p);
7258 p += 4;
7259 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | PPC_LO (plt0), p);
7260 p += 4;
7261 bfd_put_32 (htab->glink->owner, ADDI_R12_R12 | PPC_LO (plt0), p);
7262 p += 4;
7263 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
7264 p += 4;
7265 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
7266 p += 4;
7267 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
7268 p += 4;
7269 bfd_put_32 (htab->glink->owner, BCTR, p);
7270 p += 4;
7271
7272 /* Build the .glink lazy link call stubs. */
7273 indx = 0;
7274 while (p < htab->glink->contents + htab->glink->_raw_size)
7275 {
7276 if (indx < 0x8000)
7277 {
7278 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
7279 p += 4;
7280 }
7281 else
7282 {
7283 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
7284 p += 4;
7285 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
7286 p += 4;
7287 }
7288 bfd_put_32 (htab->glink->owner,
7289 B_DOT | ((htab->glink->contents - p) & 0x3fffffc), p);
7290 indx++;
7291 p += 4;
7292 }
7293 htab->glink->_cooked_size = p - htab->glink->contents;
7294 }
7295
7296 if (htab->brlt->_raw_size != 0)
7297 {
7298 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
7299 htab->brlt->_raw_size);
7300 if (htab->brlt->contents == NULL)
7301 return FALSE;
7302 }
7303 if (info->shared && htab->relbrlt->_raw_size != 0)
7304 {
7305 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
7306 htab->relbrlt->_raw_size);
7307 if (htab->relbrlt->contents == NULL)
7308 return FALSE;
7309 }
7310
7311 /* Build the stubs as directed by the stub hash table. */
7312 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
7313
7314 for (stub_sec = htab->stub_bfd->sections;
7315 stub_sec != NULL;
7316 stub_sec = stub_sec->next)
7317 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
7318 {
7319 stub_sec_count += 1;
7320 if (stub_sec->_raw_size != stub_sec->_cooked_size)
7321 break;
7322 }
7323
7324 if (stub_sec != NULL
7325 || htab->glink->_raw_size != htab->glink->_cooked_size)
7326 {
7327 htab->stub_error = TRUE;
7328 (*_bfd_error_handler) (_("stubs don't match calculated size"));
7329 }
7330
7331 if (htab->stub_error)
7332 return FALSE;
7333
7334 if (stats != NULL)
7335 {
7336 *stats = bfd_malloc (500);
7337 if (*stats == NULL)
7338 return FALSE;
7339
7340 sprintf (*stats, _("linker stubs in %u groups\n"
7341 " branch %lu\n"
7342 " toc adjust %lu\n"
7343 " long branch %lu\n"
7344 " long toc adj %lu\n"
7345 " plt call %lu"),
7346 stub_sec_count,
7347 htab->stub_count[ppc_stub_long_branch - 1],
7348 htab->stub_count[ppc_stub_long_branch_r2off - 1],
7349 htab->stub_count[ppc_stub_plt_branch - 1],
7350 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
7351 htab->stub_count[ppc_stub_plt_call - 1]);
7352 }
7353 return TRUE;
7354 }
7355
7356 /* The RELOCATE_SECTION function is called by the ELF backend linker
7357 to handle the relocations for a section.
7358
7359 The relocs are always passed as Rela structures; if the section
7360 actually uses Rel structures, the r_addend field will always be
7361 zero.
7362
7363 This function is responsible for adjust the section contents as
7364 necessary, and (if using Rela relocs and generating a
7365 relocatable output file) adjusting the reloc addend as
7366 necessary.
7367
7368 This function does not have to worry about setting the reloc
7369 address or the reloc symbol index.
7370
7371 LOCAL_SYMS is a pointer to the swapped in local symbols.
7372
7373 LOCAL_SECTIONS is an array giving the section in the input file
7374 corresponding to the st_shndx field of each local symbol.
7375
7376 The global hash table entry for the global symbols can be found
7377 via elf_sym_hashes (input_bfd).
7378
7379 When generating relocatable output, this function must handle
7380 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
7381 going to be the section symbol corresponding to the output
7382 section, which means that the addend must be adjusted
7383 accordingly. */
7384
7385 static bfd_boolean
7386 ppc64_elf_relocate_section (bfd *output_bfd,
7387 struct bfd_link_info *info,
7388 bfd *input_bfd,
7389 asection *input_section,
7390 bfd_byte *contents,
7391 Elf_Internal_Rela *relocs,
7392 Elf_Internal_Sym *local_syms,
7393 asection **local_sections)
7394 {
7395 struct ppc_link_hash_table *htab;
7396 Elf_Internal_Shdr *symtab_hdr;
7397 struct elf_link_hash_entry **sym_hashes;
7398 Elf_Internal_Rela *rel;
7399 Elf_Internal_Rela *relend;
7400 Elf_Internal_Rela outrel;
7401 bfd_byte *loc;
7402 struct got_entry **local_got_ents;
7403 bfd_vma TOCstart;
7404 bfd_boolean ret = TRUE;
7405 bfd_boolean is_opd;
7406 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
7407 bfd_boolean is_power4 = FALSE;
7408
7409 if (info->relocatable)
7410 return TRUE;
7411
7412 /* Initialize howto table if needed. */
7413 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
7414 ppc_howto_init ();
7415
7416 htab = ppc_hash_table (info);
7417 local_got_ents = elf_local_got_ents (input_bfd);
7418 TOCstart = elf_gp (output_bfd);
7419 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
7420 sym_hashes = elf_sym_hashes (input_bfd);
7421 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
7422
7423 rel = relocs;
7424 relend = relocs + input_section->reloc_count;
7425 for (; rel < relend; rel++)
7426 {
7427 enum elf_ppc64_reloc_type r_type;
7428 bfd_vma addend;
7429 bfd_reloc_status_type r;
7430 Elf_Internal_Sym *sym;
7431 asection *sec;
7432 struct elf_link_hash_entry *h;
7433 struct elf_link_hash_entry *fdh;
7434 const char *sym_name;
7435 unsigned long r_symndx, toc_symndx;
7436 char tls_mask, tls_gd, tls_type;
7437 char sym_type;
7438 bfd_vma relocation;
7439 bfd_boolean unresolved_reloc;
7440 bfd_boolean warned;
7441 unsigned long insn, mask;
7442 struct ppc_stub_hash_entry *stub_entry;
7443 bfd_vma max_br_offset;
7444 bfd_vma from;
7445
7446 r_type = ELF64_R_TYPE (rel->r_info);
7447 r_symndx = ELF64_R_SYM (rel->r_info);
7448
7449 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
7450 symbol of the previous ADDR64 reloc. The symbol gives us the
7451 proper TOC base to use. */
7452 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
7453 && rel != relocs
7454 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
7455 && is_opd)
7456 r_symndx = ELF64_R_SYM (rel[-1].r_info);
7457
7458 sym = NULL;
7459 sec = NULL;
7460 h = NULL;
7461 sym_name = NULL;
7462 unresolved_reloc = FALSE;
7463 warned = FALSE;
7464
7465 if (r_symndx < symtab_hdr->sh_info)
7466 {
7467 /* It's a local symbol. */
7468 sym = local_syms + r_symndx;
7469 sec = local_sections[r_symndx];
7470 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
7471 sym_type = ELF64_ST_TYPE (sym->st_info);
7472 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
7473 if (elf_section_data (sec) != NULL)
7474 {
7475 long *opd_sym_adjust;
7476
7477 opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
7478 if (opd_sym_adjust != NULL)
7479 relocation += opd_sym_adjust[sym->st_value / 24];
7480 }
7481 }
7482 else
7483 {
7484 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
7485 r_symndx, symtab_hdr, sym_hashes,
7486 h, sec, relocation,
7487 unresolved_reloc, warned);
7488 sym_name = h->root.root.string;
7489 sym_type = h->type;
7490 }
7491
7492 /* TLS optimizations. Replace instruction sequences and relocs
7493 based on information we collected in tls_optimize. We edit
7494 RELOCS so that --emit-relocs will output something sensible
7495 for the final instruction stream. */
7496 tls_mask = 0;
7497 tls_gd = 0;
7498 toc_symndx = 0;
7499 if (IS_PPC64_TLS_RELOC (r_type))
7500 {
7501 if (h != NULL)
7502 tls_mask = ((struct ppc_link_hash_entry *) h)->tls_mask;
7503 else if (local_got_ents != NULL)
7504 {
7505 char *lgot_masks;
7506 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
7507 tls_mask = lgot_masks[r_symndx];
7508 }
7509 if (tls_mask == 0 && r_type == R_PPC64_TLS)
7510 {
7511 /* Check for toc tls entries. */
7512 char *toc_tls;
7513
7514 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
7515 rel, input_bfd))
7516 return FALSE;
7517
7518 if (toc_tls)
7519 tls_mask = *toc_tls;
7520 }
7521 }
7522
7523 /* Check that tls relocs are used with tls syms, and non-tls
7524 relocs are used with non-tls syms. */
7525 if (r_symndx != 0
7526 && r_type != R_PPC64_NONE
7527 && (h == NULL
7528 || h->root.type == bfd_link_hash_defined
7529 || h->root.type == bfd_link_hash_defweak)
7530 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
7531 {
7532 if (r_type == R_PPC64_TLS && tls_mask != 0)
7533 /* R_PPC64_TLS is OK against a symbol in the TOC. */
7534 ;
7535 else
7536 (*_bfd_error_handler)
7537 (sym_type == STT_TLS
7538 ? _("%s(%s+0x%lx): %s used with TLS symbol %s")
7539 : _("%s(%s+0x%lx): %s used with non-TLS symbol %s"),
7540 bfd_archive_filename (input_bfd),
7541 input_section->name,
7542 (long) rel->r_offset,
7543 ppc64_elf_howto_table[r_type]->name,
7544 sym_name);
7545 }
7546
7547 /* Ensure reloc mapping code below stays sane. */
7548 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7549 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7550 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
7551 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7552 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7553 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7554 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
7555 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7556 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7557 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7558 abort ();
7559
7560 switch (r_type)
7561 {
7562 default:
7563 break;
7564
7565 case R_PPC64_TOC16:
7566 case R_PPC64_TOC16_LO:
7567 case R_PPC64_TOC16_DS:
7568 case R_PPC64_TOC16_LO_DS:
7569 {
7570 /* Check for toc tls entries. */
7571 char *toc_tls;
7572 int retval;
7573
7574 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
7575 rel, input_bfd);
7576 if (retval == 0)
7577 return FALSE;
7578
7579 if (toc_tls)
7580 {
7581 tls_mask = *toc_tls;
7582 if (r_type == R_PPC64_TOC16_DS
7583 || r_type == R_PPC64_TOC16_LO_DS)
7584 {
7585 if (tls_mask != 0
7586 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
7587 goto toctprel;
7588 }
7589 else
7590 {
7591 /* If we found a GD reloc pair, then we might be
7592 doing a GD->IE transition. */
7593 if (retval == 2)
7594 {
7595 tls_gd = TLS_TPRELGD;
7596 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7597 goto tls_get_addr_check;
7598 }
7599 else if (retval == 3)
7600 {
7601 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7602 goto tls_get_addr_check;
7603 }
7604 }
7605 }
7606 }
7607 break;
7608
7609 case R_PPC64_GOT_TPREL16_DS:
7610 case R_PPC64_GOT_TPREL16_LO_DS:
7611 if (tls_mask != 0
7612 && (tls_mask & TLS_TPREL) == 0)
7613 {
7614 toctprel:
7615 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7616 insn &= 31 << 21;
7617 insn |= 0x3c0d0000; /* addis 0,13,0 */
7618 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7619 r_type = R_PPC64_TPREL16_HA;
7620 if (toc_symndx != 0)
7621 {
7622 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
7623 /* We changed the symbol. Start over in order to
7624 get h, sym, sec etc. right. */
7625 rel--;
7626 continue;
7627 }
7628 else
7629 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7630 }
7631 break;
7632
7633 case R_PPC64_TLS:
7634 if (tls_mask != 0
7635 && (tls_mask & TLS_TPREL) == 0)
7636 {
7637 bfd_vma rtra;
7638 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7639 if ((insn & ((0x3f << 26) | (31 << 11)))
7640 == ((31 << 26) | (13 << 11)))
7641 rtra = insn & ((1 << 26) - (1 << 16));
7642 else if ((insn & ((0x3f << 26) | (31 << 16)))
7643 == ((31 << 26) | (13 << 16)))
7644 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7645 else
7646 abort ();
7647 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7648 /* add -> addi. */
7649 insn = 14 << 26;
7650 else if ((insn & (31 << 1)) == 23 << 1
7651 && ((insn & (31 << 6)) < 14 << 6
7652 || ((insn & (31 << 6)) >= 16 << 6
7653 && (insn & (31 << 6)) < 24 << 6)))
7654 /* load and store indexed -> dform. */
7655 insn = (32 | ((insn >> 6) & 31)) << 26;
7656 else if ((insn & (31 << 1)) == 21 << 1
7657 && (insn & (0x1a << 6)) == 0)
7658 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
7659 insn = (((58 | ((insn >> 6) & 4)) << 26)
7660 | ((insn >> 6) & 1));
7661 else if ((insn & (31 << 1)) == 21 << 1
7662 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7663 /* lwax -> lwa. */
7664 insn = (58 << 26) | 2;
7665 else
7666 abort ();
7667 insn |= rtra;
7668 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7669 /* Was PPC64_TLS which sits on insn boundary, now
7670 PPC64_TPREL16_LO which is at insn+2. */
7671 rel->r_offset += 2;
7672 r_type = R_PPC64_TPREL16_LO;
7673 if (toc_symndx != 0)
7674 {
7675 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
7676 /* We changed the symbol. Start over in order to
7677 get h, sym, sec etc. right. */
7678 rel--;
7679 continue;
7680 }
7681 else
7682 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7683 }
7684 break;
7685
7686 case R_PPC64_GOT_TLSGD16_HI:
7687 case R_PPC64_GOT_TLSGD16_HA:
7688 tls_gd = TLS_TPRELGD;
7689 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7690 goto tls_gdld_hi;
7691 break;
7692
7693 case R_PPC64_GOT_TLSLD16_HI:
7694 case R_PPC64_GOT_TLSLD16_HA:
7695 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7696 {
7697 tls_gdld_hi:
7698 if ((tls_mask & tls_gd) != 0)
7699 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7700 + R_PPC64_GOT_TPREL16_DS);
7701 else
7702 {
7703 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7704 rel->r_offset -= 2;
7705 r_type = R_PPC64_NONE;
7706 }
7707 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7708 }
7709 break;
7710
7711 case R_PPC64_GOT_TLSGD16:
7712 case R_PPC64_GOT_TLSGD16_LO:
7713 tls_gd = TLS_TPRELGD;
7714 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7715 goto tls_get_addr_check;
7716 break;
7717
7718 case R_PPC64_GOT_TLSLD16:
7719 case R_PPC64_GOT_TLSLD16_LO:
7720 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7721 {
7722 tls_get_addr_check:
7723 if (rel + 1 < relend)
7724 {
7725 enum elf_ppc64_reloc_type r_type2;
7726 unsigned long r_symndx2;
7727 struct elf_link_hash_entry *h2;
7728 bfd_vma insn1, insn2, insn3;
7729 bfd_vma offset;
7730
7731 /* The next instruction should be a call to
7732 __tls_get_addr. Peek at the reloc to be sure. */
7733 r_type2 = ELF64_R_TYPE (rel[1].r_info);
7734 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
7735 if (r_symndx2 < symtab_hdr->sh_info
7736 || (r_type2 != R_PPC64_REL14
7737 && r_type2 != R_PPC64_REL14_BRTAKEN
7738 && r_type2 != R_PPC64_REL14_BRNTAKEN
7739 && r_type2 != R_PPC64_REL24))
7740 break;
7741
7742 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
7743 while (h2->root.type == bfd_link_hash_indirect
7744 || h2->root.type == bfd_link_hash_warning)
7745 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
7746 if (h2 == NULL || h2 != htab->tls_get_addr)
7747 break;
7748
7749 /* OK, it checks out. Replace the call. */
7750 offset = rel[1].r_offset;
7751 insn1 = bfd_get_32 (output_bfd,
7752 contents + rel->r_offset - 2);
7753 insn3 = bfd_get_32 (output_bfd,
7754 contents + offset + 4);
7755 if ((tls_mask & tls_gd) != 0)
7756 {
7757 /* IE */
7758 insn1 &= (1 << 26) - (1 << 2);
7759 insn1 |= 58 << 26; /* ld */
7760 insn2 = 0x7c636a14; /* add 3,3,13 */
7761 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
7762 if ((tls_mask & TLS_EXPLICIT) == 0)
7763 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7764 + R_PPC64_GOT_TPREL16_DS);
7765 else
7766 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7767 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7768 }
7769 else
7770 {
7771 /* LE */
7772 insn1 = 0x3c6d0000; /* addis 3,13,0 */
7773 insn2 = 0x38630000; /* addi 3,3,0 */
7774 if (tls_gd == 0)
7775 {
7776 /* Was an LD reloc. */
7777 r_symndx = 0;
7778 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7779 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
7780 }
7781 else if (toc_symndx != 0)
7782 r_symndx = toc_symndx;
7783 r_type = R_PPC64_TPREL16_HA;
7784 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7785 rel[1].r_info = ELF64_R_INFO (r_symndx,
7786 R_PPC64_TPREL16_LO);
7787 rel[1].r_offset += 2;
7788 }
7789 if (insn3 == NOP
7790 || insn3 == CROR_151515 || insn3 == CROR_313131)
7791 {
7792 insn3 = insn2;
7793 insn2 = NOP;
7794 rel[1].r_offset += 4;
7795 }
7796 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
7797 bfd_put_32 (output_bfd, insn2, contents + offset);
7798 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7799 if (tls_gd == 0 || toc_symndx != 0)
7800 {
7801 /* We changed the symbol. Start over in order
7802 to get h, sym, sec etc. right. */
7803 rel--;
7804 continue;
7805 }
7806 }
7807 }
7808 break;
7809
7810 case R_PPC64_DTPMOD64:
7811 if (rel + 1 < relend
7812 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
7813 && rel[1].r_offset == rel->r_offset + 8)
7814 {
7815 if ((tls_mask & TLS_GD) == 0)
7816 {
7817 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
7818 if ((tls_mask & TLS_TPRELGD) != 0)
7819 r_type = R_PPC64_TPREL64;
7820 else
7821 {
7822 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
7823 r_type = R_PPC64_NONE;
7824 }
7825 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7826 }
7827 }
7828 else
7829 {
7830 if ((tls_mask & TLS_LD) == 0)
7831 {
7832 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
7833 r_type = R_PPC64_NONE;
7834 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7835 }
7836 }
7837 break;
7838
7839 case R_PPC64_TPREL64:
7840 if ((tls_mask & TLS_TPREL) == 0)
7841 {
7842 r_type = R_PPC64_NONE;
7843 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7844 }
7845 break;
7846 }
7847
7848 /* Handle other relocations that tweak non-addend part of insn. */
7849 insn = 0;
7850 switch (r_type)
7851 {
7852 default:
7853 break;
7854
7855 /* Branch taken prediction relocations. */
7856 case R_PPC64_ADDR14_BRTAKEN:
7857 case R_PPC64_REL14_BRTAKEN:
7858 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
7859 /* Fall thru. */
7860
7861 /* Branch not taken prediction relocations. */
7862 case R_PPC64_ADDR14_BRNTAKEN:
7863 case R_PPC64_REL14_BRNTAKEN:
7864 insn |= bfd_get_32 (output_bfd,
7865 contents + rel->r_offset) & ~(0x01 << 21);
7866 if (is_power4)
7867 {
7868 /* Set 'a' bit. This is 0b00010 in BO field for branch
7869 on CR(BI) insns (BO == 001at or 011at), and 0b01000
7870 for branch on CTR insns (BO == 1a00t or 1a01t). */
7871 if ((insn & (0x14 << 21)) == (0x04 << 21))
7872 insn |= 0x02 << 21;
7873 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7874 insn |= 0x08 << 21;
7875 else
7876 break;
7877 }
7878 else
7879 {
7880 from = (rel->r_offset
7881 + input_section->output_offset
7882 + input_section->output_section->vma);
7883
7884 /* Invert 'y' bit if not the default. */
7885 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7886 insn ^= 0x01 << 21;
7887 }
7888
7889 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7890 break;
7891
7892 case R_PPC64_REL24:
7893 /* Calls to functions with a different TOC, such as calls to
7894 shared objects, need to alter the TOC pointer. This is
7895 done using a linkage stub. A REL24 branching to these
7896 linkage stubs needs to be followed by a nop, as the nop
7897 will be replaced with an instruction to restore the TOC
7898 base pointer. */
7899 if (((h != NULL
7900 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
7901 && fdh->plt.plist != NULL)
7902 || ((fdh = h, sec) != NULL
7903 && sec->output_section != NULL
7904 && (htab->stub_group[sec->id].toc_off
7905 != htab->stub_group[input_section->id].toc_off)))
7906 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7907 rel, htab)) != NULL
7908 && (stub_entry->stub_type == ppc_stub_plt_call
7909 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
7910 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
7911 {
7912 bfd_boolean can_plt_call = 0;
7913
7914 if (rel->r_offset + 8 <= input_section->_cooked_size)
7915 {
7916 insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
7917 if (insn == NOP
7918 || insn == CROR_151515 || insn == CROR_313131)
7919 {
7920 bfd_put_32 (input_bfd, LD_R2_40R1,
7921 contents + rel->r_offset + 4);
7922 can_plt_call = 1;
7923 }
7924 }
7925
7926 if (!can_plt_call)
7927 {
7928 if (stub_entry->stub_type == ppc_stub_plt_call)
7929 {
7930 /* If this is a plain branch rather than a branch
7931 and link, don't require a nop. */
7932 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7933 if ((insn & 1) == 0)
7934 can_plt_call = 1;
7935 }
7936 else if (h != NULL
7937 && strcmp (h->root.root.string,
7938 ".__libc_start_main") == 0)
7939 {
7940 /* Allow crt1 branch to go via a toc adjusting stub. */
7941 can_plt_call = 1;
7942 }
7943 else
7944 {
7945 if (strcmp (input_section->output_section->name,
7946 ".init") == 0
7947 || strcmp (input_section->output_section->name,
7948 ".fini") == 0)
7949 (*_bfd_error_handler)
7950 (_("%s(%s+0x%lx): automatic multiple TOCs "
7951 "not supported using your crt files; "
7952 "recompile with -mminimal-toc or upgrade gcc"),
7953 bfd_archive_filename (input_bfd),
7954 input_section->name,
7955 (long) rel->r_offset);
7956 else
7957 (*_bfd_error_handler)
7958 (_("%s(%s+0x%lx): sibling call optimization to `%s' "
7959 "does not allow automatic multiple TOCs; "
7960 "recompile with -mminimal-toc or "
7961 "-fno-optimize-sibling-calls, "
7962 "or make `%s' extern"),
7963 bfd_archive_filename (input_bfd),
7964 input_section->name,
7965 (long) rel->r_offset,
7966 sym_name,
7967 sym_name);
7968 bfd_set_error (bfd_error_bad_value);
7969 ret = FALSE;
7970 }
7971 }
7972
7973 if (can_plt_call)
7974 {
7975 relocation = (stub_entry->stub_offset
7976 + stub_entry->stub_sec->output_offset
7977 + stub_entry->stub_sec->output_section->vma);
7978 if (stub_entry->stub_type == ppc_stub_plt_call)
7979 unresolved_reloc = FALSE;
7980 }
7981 }
7982
7983 if (h != NULL
7984 && h->root.type == bfd_link_hash_undefweak
7985 && relocation == 0
7986 && rel->r_addend == 0)
7987 {
7988 /* Tweak calls to undefined weak functions to point at a
7989 blr. We can thus call a weak function without first
7990 checking whether the function is defined. We have a
7991 blr at the end of .sfpr. */
7992 BFD_ASSERT (htab->sfpr->_raw_size != 0);
7993 relocation = (htab->sfpr->_raw_size - 4
7994 + htab->sfpr->output_offset
7995 + htab->sfpr->output_section->vma);
7996 from = (rel->r_offset
7997 + input_section->output_offset
7998 + input_section->output_section->vma);
7999
8000 /* But let's not be silly about it. If the blr isn't in
8001 reach, just go to the next instruction. */
8002 if (relocation - from + (1 << 25) >= (1 << 26)
8003 || htab->sfpr->_raw_size == 0)
8004 relocation = from + 4;
8005 }
8006 break;
8007 }
8008
8009 /* Set `addend'. */
8010 tls_type = 0;
8011 addend = rel->r_addend;
8012 switch (r_type)
8013 {
8014 default:
8015 (*_bfd_error_handler)
8016 (_("%s: unknown relocation type %d for symbol %s"),
8017 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
8018
8019 bfd_set_error (bfd_error_bad_value);
8020 ret = FALSE;
8021 continue;
8022
8023 case R_PPC64_NONE:
8024 case R_PPC64_TLS:
8025 case R_PPC64_GNU_VTINHERIT:
8026 case R_PPC64_GNU_VTENTRY:
8027 continue;
8028
8029 /* GOT16 relocations. Like an ADDR16 using the symbol's
8030 address in the GOT as relocation value instead of the
8031 symbol's value itself. Also, create a GOT entry for the
8032 symbol and put the symbol value there. */
8033 case R_PPC64_GOT_TLSGD16:
8034 case R_PPC64_GOT_TLSGD16_LO:
8035 case R_PPC64_GOT_TLSGD16_HI:
8036 case R_PPC64_GOT_TLSGD16_HA:
8037 tls_type = TLS_TLS | TLS_GD;
8038 goto dogot;
8039
8040 case R_PPC64_GOT_TLSLD16:
8041 case R_PPC64_GOT_TLSLD16_LO:
8042 case R_PPC64_GOT_TLSLD16_HI:
8043 case R_PPC64_GOT_TLSLD16_HA:
8044 tls_type = TLS_TLS | TLS_LD;
8045 goto dogot;
8046
8047 case R_PPC64_GOT_TPREL16_DS:
8048 case R_PPC64_GOT_TPREL16_LO_DS:
8049 case R_PPC64_GOT_TPREL16_HI:
8050 case R_PPC64_GOT_TPREL16_HA:
8051 tls_type = TLS_TLS | TLS_TPREL;
8052 goto dogot;
8053
8054 case R_PPC64_GOT_DTPREL16_DS:
8055 case R_PPC64_GOT_DTPREL16_LO_DS:
8056 case R_PPC64_GOT_DTPREL16_HI:
8057 case R_PPC64_GOT_DTPREL16_HA:
8058 tls_type = TLS_TLS | TLS_DTPREL;
8059 goto dogot;
8060
8061 case R_PPC64_GOT16:
8062 case R_PPC64_GOT16_LO:
8063 case R_PPC64_GOT16_HI:
8064 case R_PPC64_GOT16_HA:
8065 case R_PPC64_GOT16_DS:
8066 case R_PPC64_GOT16_LO_DS:
8067 dogot:
8068 {
8069 /* Relocation is to the entry for this symbol in the global
8070 offset table. */
8071 asection *got;
8072 bfd_vma *offp;
8073 bfd_vma off;
8074 unsigned long indx = 0;
8075
8076 if (tls_type == (TLS_TLS | TLS_LD)
8077 && (h == NULL
8078 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
8079 offp = &ppc64_tlsld_got (input_bfd)->offset;
8080 else
8081 {
8082 struct got_entry *ent;
8083
8084 if (h != NULL)
8085 {
8086 bfd_boolean dyn = htab->elf.dynamic_sections_created;
8087 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
8088 || (info->shared
8089 && SYMBOL_REFERENCES_LOCAL (info, h)))
8090 /* This is actually a static link, or it is a
8091 -Bsymbolic link and the symbol is defined
8092 locally, or the symbol was forced to be local
8093 because of a version file. */
8094 ;
8095 else
8096 {
8097 indx = h->dynindx;
8098 unresolved_reloc = FALSE;
8099 }
8100 ent = h->got.glist;
8101 }
8102 else
8103 {
8104 if (local_got_ents == NULL)
8105 abort ();
8106 ent = local_got_ents[r_symndx];
8107 }
8108
8109 for (; ent != NULL; ent = ent->next)
8110 if (ent->addend == rel->r_addend
8111 && ent->owner == input_bfd
8112 && ent->tls_type == tls_type)
8113 break;
8114 if (ent == NULL)
8115 abort ();
8116 offp = &ent->got.offset;
8117 }
8118
8119 got = ppc64_elf_tdata (input_bfd)->got;
8120 if (got == NULL)
8121 abort ();
8122
8123 /* The offset must always be a multiple of 8. We use the
8124 least significant bit to record whether we have already
8125 processed this entry. */
8126 off = *offp;
8127 if ((off & 1) != 0)
8128 off &= ~1;
8129 else
8130 {
8131 /* Generate relocs for the dynamic linker, except in
8132 the case of TLSLD where we'll use one entry per
8133 module. */
8134 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
8135
8136 *offp = off | 1;
8137 if ((info->shared || indx != 0)
8138 && (h == NULL
8139 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8140 || h->root.type != bfd_link_hash_undefweak))
8141 {
8142 outrel.r_offset = (got->output_section->vma
8143 + got->output_offset
8144 + off);
8145 outrel.r_addend = rel->r_addend;
8146 if (tls_type & (TLS_LD | TLS_GD))
8147 {
8148 outrel.r_addend = 0;
8149 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
8150 if (tls_type == (TLS_TLS | TLS_GD))
8151 {
8152 loc = relgot->contents;
8153 loc += (relgot->reloc_count++
8154 * sizeof (Elf64_External_Rela));
8155 bfd_elf64_swap_reloca_out (output_bfd,
8156 &outrel, loc);
8157 outrel.r_offset += 8;
8158 outrel.r_addend = rel->r_addend;
8159 outrel.r_info
8160 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
8161 }
8162 }
8163 else if (tls_type == (TLS_TLS | TLS_DTPREL))
8164 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
8165 else if (tls_type == (TLS_TLS | TLS_TPREL))
8166 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
8167 else if (indx == 0)
8168 {
8169 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
8170
8171 /* Write the .got section contents for the sake
8172 of prelink. */
8173 loc = got->contents + off;
8174 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
8175 loc);
8176 }
8177 else
8178 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
8179
8180 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
8181 {
8182 outrel.r_addend += relocation;
8183 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
8184 outrel.r_addend -= htab->elf.tls_sec->vma;
8185 }
8186 loc = relgot->contents;
8187 loc += (relgot->reloc_count++
8188 * sizeof (Elf64_External_Rela));
8189 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8190 }
8191
8192 /* Init the .got section contents here if we're not
8193 emitting a reloc. */
8194 else
8195 {
8196 relocation += rel->r_addend;
8197 if (tls_type == (TLS_TLS | TLS_LD))
8198 relocation = 1;
8199 else if (tls_type != 0)
8200 {
8201 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
8202 if (tls_type == (TLS_TLS | TLS_TPREL))
8203 relocation += DTP_OFFSET - TP_OFFSET;
8204
8205 if (tls_type == (TLS_TLS | TLS_GD))
8206 {
8207 bfd_put_64 (output_bfd, relocation,
8208 got->contents + off + 8);
8209 relocation = 1;
8210 }
8211 }
8212
8213 bfd_put_64 (output_bfd, relocation,
8214 got->contents + off);
8215 }
8216 }
8217
8218 if (off >= (bfd_vma) -2)
8219 abort ();
8220
8221 relocation = got->output_offset + off;
8222
8223 /* TOC base (r2) is TOC start plus 0x8000. */
8224 addend = -TOC_BASE_OFF;
8225 }
8226 break;
8227
8228 case R_PPC64_PLT16_HA:
8229 case R_PPC64_PLT16_HI:
8230 case R_PPC64_PLT16_LO:
8231 case R_PPC64_PLT32:
8232 case R_PPC64_PLT64:
8233 /* Relocation is to the entry for this symbol in the
8234 procedure linkage table. */
8235
8236 /* Resolve a PLT reloc against a local symbol directly,
8237 without using the procedure linkage table. */
8238 if (h == NULL)
8239 break;
8240
8241 /* It's possible that we didn't make a PLT entry for this
8242 symbol. This happens when statically linking PIC code,
8243 or when using -Bsymbolic. Go find a match if there is a
8244 PLT entry. */
8245 if (htab->plt != NULL)
8246 {
8247 struct plt_entry *ent;
8248 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8249 if (ent->addend == rel->r_addend
8250 && ent->plt.offset != (bfd_vma) -1)
8251 {
8252 relocation = (htab->plt->output_section->vma
8253 + htab->plt->output_offset
8254 + ent->plt.offset);
8255 unresolved_reloc = FALSE;
8256 }
8257 }
8258 break;
8259
8260 case R_PPC64_TOC:
8261 /* Relocation value is TOC base. */
8262 relocation = TOCstart;
8263 if (r_symndx == 0)
8264 relocation += htab->stub_group[input_section->id].toc_off;
8265 else if (unresolved_reloc)
8266 ;
8267 else if (sec != NULL && sec->id <= htab->top_id)
8268 relocation += htab->stub_group[sec->id].toc_off;
8269 else
8270 unresolved_reloc = TRUE;
8271 goto dodyn2;
8272
8273 /* TOC16 relocs. We want the offset relative to the TOC base,
8274 which is the address of the start of the TOC plus 0x8000.
8275 The TOC consists of sections .got, .toc, .tocbss, and .plt,
8276 in this order. */
8277 case R_PPC64_TOC16:
8278 case R_PPC64_TOC16_LO:
8279 case R_PPC64_TOC16_HI:
8280 case R_PPC64_TOC16_DS:
8281 case R_PPC64_TOC16_LO_DS:
8282 case R_PPC64_TOC16_HA:
8283 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
8284 break;
8285
8286 /* Relocate against the beginning of the section. */
8287 case R_PPC64_SECTOFF:
8288 case R_PPC64_SECTOFF_LO:
8289 case R_PPC64_SECTOFF_HI:
8290 case R_PPC64_SECTOFF_DS:
8291 case R_PPC64_SECTOFF_LO_DS:
8292 case R_PPC64_SECTOFF_HA:
8293 if (sec != NULL)
8294 addend -= sec->output_section->vma;
8295 break;
8296
8297 case R_PPC64_REL14:
8298 case R_PPC64_REL14_BRNTAKEN:
8299 case R_PPC64_REL14_BRTAKEN:
8300 case R_PPC64_REL24:
8301 break;
8302
8303 case R_PPC64_TPREL16:
8304 case R_PPC64_TPREL16_LO:
8305 case R_PPC64_TPREL16_HI:
8306 case R_PPC64_TPREL16_HA:
8307 case R_PPC64_TPREL16_DS:
8308 case R_PPC64_TPREL16_LO_DS:
8309 case R_PPC64_TPREL16_HIGHER:
8310 case R_PPC64_TPREL16_HIGHERA:
8311 case R_PPC64_TPREL16_HIGHEST:
8312 case R_PPC64_TPREL16_HIGHESTA:
8313 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
8314 if (info->shared)
8315 /* The TPREL16 relocs shouldn't really be used in shared
8316 libs as they will result in DT_TEXTREL being set, but
8317 support them anyway. */
8318 goto dodyn;
8319 break;
8320
8321 case R_PPC64_DTPREL16:
8322 case R_PPC64_DTPREL16_LO:
8323 case R_PPC64_DTPREL16_HI:
8324 case R_PPC64_DTPREL16_HA:
8325 case R_PPC64_DTPREL16_DS:
8326 case R_PPC64_DTPREL16_LO_DS:
8327 case R_PPC64_DTPREL16_HIGHER:
8328 case R_PPC64_DTPREL16_HIGHERA:
8329 case R_PPC64_DTPREL16_HIGHEST:
8330 case R_PPC64_DTPREL16_HIGHESTA:
8331 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
8332 break;
8333
8334 case R_PPC64_DTPMOD64:
8335 relocation = 1;
8336 addend = 0;
8337 goto dodyn;
8338
8339 case R_PPC64_TPREL64:
8340 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
8341 goto dodyn;
8342
8343 case R_PPC64_DTPREL64:
8344 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
8345 /* Fall thru */
8346
8347 /* Relocations that may need to be propagated if this is a
8348 dynamic object. */
8349 case R_PPC64_REL30:
8350 case R_PPC64_REL32:
8351 case R_PPC64_REL64:
8352 case R_PPC64_ADDR14:
8353 case R_PPC64_ADDR14_BRNTAKEN:
8354 case R_PPC64_ADDR14_BRTAKEN:
8355 case R_PPC64_ADDR16:
8356 case R_PPC64_ADDR16_DS:
8357 case R_PPC64_ADDR16_HA:
8358 case R_PPC64_ADDR16_HI:
8359 case R_PPC64_ADDR16_HIGHER:
8360 case R_PPC64_ADDR16_HIGHERA:
8361 case R_PPC64_ADDR16_HIGHEST:
8362 case R_PPC64_ADDR16_HIGHESTA:
8363 case R_PPC64_ADDR16_LO:
8364 case R_PPC64_ADDR16_LO_DS:
8365 case R_PPC64_ADDR24:
8366 case R_PPC64_ADDR32:
8367 case R_PPC64_ADDR64:
8368 case R_PPC64_UADDR16:
8369 case R_PPC64_UADDR32:
8370 case R_PPC64_UADDR64:
8371 /* r_symndx will be zero only for relocs against symbols
8372 from removed linkonce sections, or sections discarded by
8373 a linker script. */
8374 dodyn:
8375 if (r_symndx == 0)
8376 break;
8377 /* Fall thru. */
8378
8379 dodyn2:
8380 if ((input_section->flags & SEC_ALLOC) == 0)
8381 break;
8382
8383 if (NO_OPD_RELOCS && is_opd)
8384 break;
8385
8386 if ((info->shared
8387 && (h == NULL
8388 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8389 || h->root.type != bfd_link_hash_undefweak)
8390 && (MUST_BE_DYN_RELOC (r_type)
8391 || !SYMBOL_CALLS_LOCAL (info, h)))
8392 || (ELIMINATE_COPY_RELOCS
8393 && !info->shared
8394 && h != NULL
8395 && h->dynindx != -1
8396 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
8397 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
8398 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
8399 {
8400 Elf_Internal_Rela outrel;
8401 bfd_boolean skip, relocate;
8402 asection *sreloc;
8403 bfd_byte *loc;
8404 bfd_vma out_off;
8405
8406 /* When generating a dynamic object, these relocations
8407 are copied into the output file to be resolved at run
8408 time. */
8409
8410 skip = FALSE;
8411 relocate = FALSE;
8412
8413 out_off = _bfd_elf_section_offset (output_bfd, info,
8414 input_section, rel->r_offset);
8415 if (out_off == (bfd_vma) -1)
8416 skip = TRUE;
8417 else if (out_off == (bfd_vma) -2)
8418 skip = TRUE, relocate = TRUE;
8419 out_off += (input_section->output_section->vma
8420 + input_section->output_offset);
8421 outrel.r_offset = out_off;
8422 outrel.r_addend = rel->r_addend;
8423
8424 /* Optimize unaligned reloc use. */
8425 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
8426 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
8427 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
8428 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
8429 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
8430 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
8431 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
8432 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
8433 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
8434
8435 if (skip)
8436 memset (&outrel, 0, sizeof outrel);
8437 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
8438 && !is_opd
8439 && r_type != R_PPC64_TOC)
8440 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
8441 else
8442 {
8443 /* This symbol is local, or marked to become local,
8444 or this is an opd section reloc which must point
8445 at a local function. */
8446 outrel.r_addend += relocation;
8447 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
8448 {
8449 if (is_opd && h != NULL)
8450 {
8451 /* Lie about opd entries. This case occurs
8452 when building shared libraries and we
8453 reference a function in another shared
8454 lib. The same thing happens for a weak
8455 definition in an application that's
8456 overridden by a strong definition in a
8457 shared lib. (I believe this is a generic
8458 bug in binutils handling of weak syms.)
8459 In these cases we won't use the opd
8460 entry in this lib. */
8461 unresolved_reloc = FALSE;
8462 }
8463 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8464
8465 /* We need to relocate .opd contents for ld.so.
8466 Prelink also wants simple and consistent rules
8467 for relocs. This make all RELATIVE relocs have
8468 *r_offset equal to r_addend. */
8469 relocate = TRUE;
8470 }
8471 else
8472 {
8473 long indx = 0;
8474
8475 if (bfd_is_abs_section (sec))
8476 ;
8477 else if (sec == NULL || sec->owner == NULL)
8478 {
8479 bfd_set_error (bfd_error_bad_value);
8480 return FALSE;
8481 }
8482 else
8483 {
8484 asection *osec;
8485
8486 osec = sec->output_section;
8487 indx = elf_section_data (osec)->dynindx;
8488
8489 /* We are turning this relocation into one
8490 against a section symbol, so subtract out
8491 the output section's address but not the
8492 offset of the input section in the output
8493 section. */
8494 outrel.r_addend -= osec->vma;
8495 }
8496
8497 outrel.r_info = ELF64_R_INFO (indx, r_type);
8498 }
8499 }
8500
8501 sreloc = elf_section_data (input_section)->sreloc;
8502 if (sreloc == NULL)
8503 abort ();
8504
8505 loc = sreloc->contents;
8506 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
8507 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
8508
8509 /* If this reloc is against an external symbol, it will
8510 be computed at runtime, so there's no need to do
8511 anything now. However, for the sake of prelink ensure
8512 that the section contents are a known value. */
8513 if (! relocate)
8514 {
8515 unresolved_reloc = FALSE;
8516 /* The value chosen here is quite arbitrary as ld.so
8517 ignores section contents except for the special
8518 case of .opd where the contents might be accessed
8519 before relocation. Choose zero, as that won't
8520 cause reloc overflow. */
8521 relocation = 0;
8522 addend = 0;
8523 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
8524 to improve backward compatibility with older
8525 versions of ld. */
8526 if (r_type == R_PPC64_ADDR64)
8527 addend = outrel.r_addend;
8528 /* Adjust pc_relative relocs to have zero in *r_offset. */
8529 else if (ppc64_elf_howto_table[r_type]->pc_relative)
8530 addend = (input_section->output_section->vma
8531 + input_section->output_offset
8532 + rel->r_offset);
8533 }
8534 }
8535 break;
8536
8537 case R_PPC64_COPY:
8538 case R_PPC64_GLOB_DAT:
8539 case R_PPC64_JMP_SLOT:
8540 case R_PPC64_RELATIVE:
8541 /* We shouldn't ever see these dynamic relocs in relocatable
8542 files. */
8543 /* Fall through. */
8544
8545 case R_PPC64_PLTGOT16:
8546 case R_PPC64_PLTGOT16_DS:
8547 case R_PPC64_PLTGOT16_HA:
8548 case R_PPC64_PLTGOT16_HI:
8549 case R_PPC64_PLTGOT16_LO:
8550 case R_PPC64_PLTGOT16_LO_DS:
8551 case R_PPC64_PLTREL32:
8552 case R_PPC64_PLTREL64:
8553 /* These ones haven't been implemented yet. */
8554
8555 (*_bfd_error_handler)
8556 (_("%s: relocation %s is not supported for symbol %s."),
8557 bfd_archive_filename (input_bfd),
8558 ppc64_elf_howto_table[r_type]->name, sym_name);
8559
8560 bfd_set_error (bfd_error_invalid_operation);
8561 ret = FALSE;
8562 continue;
8563 }
8564
8565 /* Do any further special processing. */
8566 switch (r_type)
8567 {
8568 default:
8569 break;
8570
8571 case R_PPC64_ADDR16_HA:
8572 case R_PPC64_ADDR16_HIGHERA:
8573 case R_PPC64_ADDR16_HIGHESTA:
8574 case R_PPC64_GOT16_HA:
8575 case R_PPC64_PLTGOT16_HA:
8576 case R_PPC64_PLT16_HA:
8577 case R_PPC64_TOC16_HA:
8578 case R_PPC64_SECTOFF_HA:
8579 case R_PPC64_TPREL16_HA:
8580 case R_PPC64_DTPREL16_HA:
8581 case R_PPC64_GOT_TLSGD16_HA:
8582 case R_PPC64_GOT_TLSLD16_HA:
8583 case R_PPC64_GOT_TPREL16_HA:
8584 case R_PPC64_GOT_DTPREL16_HA:
8585 case R_PPC64_TPREL16_HIGHER:
8586 case R_PPC64_TPREL16_HIGHERA:
8587 case R_PPC64_TPREL16_HIGHEST:
8588 case R_PPC64_TPREL16_HIGHESTA:
8589 case R_PPC64_DTPREL16_HIGHER:
8590 case R_PPC64_DTPREL16_HIGHERA:
8591 case R_PPC64_DTPREL16_HIGHEST:
8592 case R_PPC64_DTPREL16_HIGHESTA:
8593 /* It's just possible that this symbol is a weak symbol
8594 that's not actually defined anywhere. In that case,
8595 'sec' would be NULL, and we should leave the symbol
8596 alone (it will be set to zero elsewhere in the link). */
8597 if (sec != NULL)
8598 /* Add 0x10000 if sign bit in 0:15 is set.
8599 Bits 0:15 are not used. */
8600 addend += 0x8000;
8601 break;
8602
8603 case R_PPC64_ADDR16_DS:
8604 case R_PPC64_ADDR16_LO_DS:
8605 case R_PPC64_GOT16_DS:
8606 case R_PPC64_GOT16_LO_DS:
8607 case R_PPC64_PLT16_LO_DS:
8608 case R_PPC64_SECTOFF_DS:
8609 case R_PPC64_SECTOFF_LO_DS:
8610 case R_PPC64_TOC16_DS:
8611 case R_PPC64_TOC16_LO_DS:
8612 case R_PPC64_PLTGOT16_DS:
8613 case R_PPC64_PLTGOT16_LO_DS:
8614 case R_PPC64_GOT_TPREL16_DS:
8615 case R_PPC64_GOT_TPREL16_LO_DS:
8616 case R_PPC64_GOT_DTPREL16_DS:
8617 case R_PPC64_GOT_DTPREL16_LO_DS:
8618 case R_PPC64_TPREL16_DS:
8619 case R_PPC64_TPREL16_LO_DS:
8620 case R_PPC64_DTPREL16_DS:
8621 case R_PPC64_DTPREL16_LO_DS:
8622 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
8623 mask = 3;
8624 /* If this reloc is against an lq insn, then the value must be
8625 a multiple of 16. This is somewhat of a hack, but the
8626 "correct" way to do this by defining _DQ forms of all the
8627 _DS relocs bloats all reloc switches in this file. It
8628 doesn't seem to make much sense to use any of these relocs
8629 in data, so testing the insn should be safe. */
8630 if ((insn & (0x3f << 26)) == (56u << 26))
8631 mask = 15;
8632 if (((relocation + addend) & mask) != 0)
8633 {
8634 (*_bfd_error_handler)
8635 (_("%s: error: relocation %s not a multiple of %d"),
8636 bfd_archive_filename (input_bfd),
8637 ppc64_elf_howto_table[r_type]->name,
8638 mask + 1);
8639 bfd_set_error (bfd_error_bad_value);
8640 ret = FALSE;
8641 continue;
8642 }
8643 break;
8644
8645 case R_PPC64_REL14:
8646 case R_PPC64_REL14_BRNTAKEN:
8647 case R_PPC64_REL14_BRTAKEN:
8648 max_br_offset = 1 << 15;
8649 goto branch_check;
8650
8651 case R_PPC64_REL24:
8652 max_br_offset = 1 << 25;
8653
8654 branch_check:
8655 /* If the branch is out of reach or the TOC register needs
8656 adjusting, then redirect the call to the local stub for
8657 this function. */
8658 from = (rel->r_offset
8659 + input_section->output_offset
8660 + input_section->output_section->vma);
8661 if ((relocation + addend - from + max_br_offset >= 2 * max_br_offset
8662 || (sec != NULL
8663 && sec->output_section != NULL
8664 && sec->id <= htab->top_id
8665 && (htab->stub_group[sec->id].toc_off
8666 != htab->stub_group[input_section->id].toc_off)))
8667 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
8668 rel, htab)) != NULL)
8669 {
8670 /* Munge up the value and addend so that we call the stub
8671 rather than the procedure directly. */
8672 relocation = (stub_entry->stub_offset
8673 + stub_entry->stub_sec->output_offset
8674 + stub_entry->stub_sec->output_section->vma);
8675 addend = 0;
8676 }
8677 break;
8678 }
8679
8680 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8681 because such sections are not SEC_ALLOC and thus ld.so will
8682 not process them. */
8683 if (unresolved_reloc
8684 && !((input_section->flags & SEC_DEBUGGING) != 0
8685 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
8686 {
8687 (*_bfd_error_handler)
8688 (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
8689 bfd_archive_filename (input_bfd),
8690 bfd_get_section_name (input_bfd, input_section),
8691 (long) rel->r_offset,
8692 ppc64_elf_howto_table[(int) r_type]->name,
8693 h->root.root.string);
8694 ret = FALSE;
8695 }
8696
8697 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
8698 input_bfd,
8699 input_section,
8700 contents,
8701 rel->r_offset,
8702 relocation,
8703 addend);
8704
8705 if (r != bfd_reloc_ok)
8706 {
8707 if (sym_name == NULL)
8708 sym_name = "(null)";
8709 if (r == bfd_reloc_overflow)
8710 {
8711 if (warned)
8712 continue;
8713 if (h != NULL
8714 && h->root.type == bfd_link_hash_undefweak
8715 && ppc64_elf_howto_table[r_type]->pc_relative)
8716 {
8717 /* Assume this is a call protected by other code that
8718 detects the symbol is undefined. If this is the case,
8719 we can safely ignore the overflow. If not, the
8720 program is hosed anyway, and a little warning isn't
8721 going to help. */
8722
8723 continue;
8724 }
8725
8726 if (!((*info->callbacks->reloc_overflow)
8727 (info, sym_name, ppc64_elf_howto_table[r_type]->name,
8728 rel->r_addend, input_bfd, input_section, rel->r_offset)))
8729 return FALSE;
8730 }
8731 else
8732 {
8733 (*_bfd_error_handler)
8734 (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
8735 bfd_archive_filename (input_bfd),
8736 bfd_get_section_name (input_bfd, input_section),
8737 (long) rel->r_offset,
8738 ppc64_elf_howto_table[r_type]->name,
8739 sym_name,
8740 (int) r);
8741 ret = FALSE;
8742 }
8743 }
8744 }
8745
8746 return ret;
8747 }
8748
8749 /* Adjust the value of any local symbols in opd sections. */
8750
8751 static bfd_boolean
8752 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
8753 const char *name ATTRIBUTE_UNUSED,
8754 Elf_Internal_Sym *elfsym,
8755 asection *input_sec,
8756 struct elf_link_hash_entry *h)
8757 {
8758 long *adjust;
8759 bfd_vma value;
8760
8761 if (h != NULL
8762 || input_sec == NULL
8763 || ppc64_elf_section_data (input_sec) == NULL
8764 || (adjust = ppc64_elf_section_data (input_sec)->opd.adjust) == NULL)
8765 return TRUE;
8766
8767 value = elfsym->st_value - input_sec->output_offset;
8768 if (!info->relocatable)
8769 value -= input_sec->output_section->vma;
8770
8771 elfsym->st_value += adjust[value / 24];
8772 return TRUE;
8773 }
8774
8775 /* Finish up dynamic symbol handling. We set the contents of various
8776 dynamic sections here. */
8777
8778 static bfd_boolean
8779 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
8780 struct bfd_link_info *info,
8781 struct elf_link_hash_entry *h,
8782 Elf_Internal_Sym *sym)
8783 {
8784 struct ppc_link_hash_table *htab;
8785 bfd *dynobj;
8786
8787 htab = ppc_hash_table (info);
8788 dynobj = htab->elf.dynobj;
8789
8790 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
8791 {
8792 struct plt_entry *ent;
8793 Elf_Internal_Rela rela;
8794 bfd_byte *loc;
8795
8796 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8797 if (ent->plt.offset != (bfd_vma) -1)
8798 {
8799 /* This symbol has an entry in the procedure linkage
8800 table. Set it up. */
8801
8802 if (htab->plt == NULL
8803 || htab->relplt == NULL
8804 || htab->glink == NULL)
8805 abort ();
8806
8807 /* Create a JMP_SLOT reloc to inform the dynamic linker to
8808 fill in the PLT entry. */
8809 rela.r_offset = (htab->plt->output_section->vma
8810 + htab->plt->output_offset
8811 + ent->plt.offset);
8812 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8813 rela.r_addend = ent->addend;
8814
8815 loc = htab->relplt->contents;
8816 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8817 * sizeof (Elf64_External_Rela));
8818 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8819 }
8820 }
8821
8822 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8823 {
8824 Elf_Internal_Rela rela;
8825 bfd_byte *loc;
8826
8827 /* This symbol needs a copy reloc. Set it up. */
8828
8829 if (h->dynindx == -1
8830 || (h->root.type != bfd_link_hash_defined
8831 && h->root.type != bfd_link_hash_defweak)
8832 || htab->relbss == NULL)
8833 abort ();
8834
8835 rela.r_offset = (h->root.u.def.value
8836 + h->root.u.def.section->output_section->vma
8837 + h->root.u.def.section->output_offset);
8838 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8839 rela.r_addend = 0;
8840 loc = htab->relbss->contents;
8841 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
8842 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8843 }
8844
8845 /* Mark some specially defined symbols as absolute. */
8846 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8847 sym->st_shndx = SHN_ABS;
8848
8849 return TRUE;
8850 }
8851
8852 /* Used to decide how to sort relocs in an optimal manner for the
8853 dynamic linker, before writing them out. */
8854
8855 static enum elf_reloc_type_class
8856 ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
8857 {
8858 enum elf_ppc64_reloc_type r_type;
8859
8860 r_type = ELF64_R_TYPE (rela->r_info);
8861 switch (r_type)
8862 {
8863 case R_PPC64_RELATIVE:
8864 return reloc_class_relative;
8865 case R_PPC64_JMP_SLOT:
8866 return reloc_class_plt;
8867 case R_PPC64_COPY:
8868 return reloc_class_copy;
8869 default:
8870 return reloc_class_normal;
8871 }
8872 }
8873
8874 /* Finish up the dynamic sections. */
8875
8876 static bfd_boolean
8877 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
8878 struct bfd_link_info *info)
8879 {
8880 struct ppc_link_hash_table *htab;
8881 bfd *dynobj;
8882 asection *sdyn;
8883
8884 htab = ppc_hash_table (info);
8885 dynobj = htab->elf.dynobj;
8886 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8887
8888 if (htab->elf.dynamic_sections_created)
8889 {
8890 Elf64_External_Dyn *dyncon, *dynconend;
8891
8892 if (sdyn == NULL || htab->got == NULL)
8893 abort ();
8894
8895 dyncon = (Elf64_External_Dyn *) sdyn->contents;
8896 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8897 for (; dyncon < dynconend; dyncon++)
8898 {
8899 Elf_Internal_Dyn dyn;
8900 asection *s;
8901
8902 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8903
8904 switch (dyn.d_tag)
8905 {
8906 default:
8907 continue;
8908
8909 case DT_PPC64_GLINK:
8910 s = htab->glink;
8911 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8912 /* We stupidly defined DT_PPC64_GLINK to be the start
8913 of glink rather than the first entry point, which is
8914 what ld.so needs, and now have a bigger stub to
8915 support automatic multiple TOCs. */
8916 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
8917 break;
8918
8919 case DT_PPC64_OPD:
8920 s = bfd_get_section_by_name (output_bfd, ".opd");
8921 if (s == NULL)
8922 continue;
8923 dyn.d_un.d_ptr = s->vma;
8924 break;
8925
8926 case DT_PPC64_OPDSZ:
8927 s = bfd_get_section_by_name (output_bfd, ".opd");
8928 if (s == NULL)
8929 continue;
8930 dyn.d_un.d_val = s->_raw_size;
8931 break;
8932
8933 case DT_PLTGOT:
8934 s = htab->plt;
8935 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8936 break;
8937
8938 case DT_JMPREL:
8939 s = htab->relplt;
8940 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8941 break;
8942
8943 case DT_PLTRELSZ:
8944 dyn.d_un.d_val = htab->relplt->_raw_size;
8945 break;
8946
8947 case DT_RELASZ:
8948 /* Don't count procedure linkage table relocs in the
8949 overall reloc count. */
8950 s = htab->relplt;
8951 if (s == NULL)
8952 continue;
8953 dyn.d_un.d_val -= s->_raw_size;
8954 break;
8955
8956 case DT_RELA:
8957 /* We may not be using the standard ELF linker script.
8958 If .rela.plt is the first .rela section, we adjust
8959 DT_RELA to not include it. */
8960 s = htab->relplt;
8961 if (s == NULL)
8962 continue;
8963 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8964 continue;
8965 dyn.d_un.d_ptr += s->_raw_size;
8966 break;
8967 }
8968
8969 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
8970 }
8971 }
8972
8973 if (htab->got != NULL && htab->got->_raw_size != 0)
8974 {
8975 /* Fill in the first entry in the global offset table.
8976 We use it to hold the link-time TOCbase. */
8977 bfd_put_64 (output_bfd,
8978 elf_gp (output_bfd) + TOC_BASE_OFF,
8979 htab->got->contents);
8980
8981 /* Set .got entry size. */
8982 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
8983 }
8984
8985 if (htab->plt != NULL && htab->plt->_raw_size != 0)
8986 {
8987 /* Set .plt entry size. */
8988 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
8989 = PLT_ENTRY_SIZE;
8990 }
8991
8992 /* We need to handle writing out multiple GOT sections ourselves,
8993 since we didn't add them to DYNOBJ. */
8994 while ((dynobj = dynobj->link_next) != NULL)
8995 {
8996 asection *s;
8997 s = ppc64_elf_tdata (dynobj)->got;
8998 if (s != NULL
8999 && s->_raw_size != 0
9000 && s->output_section != bfd_abs_section_ptr
9001 && !bfd_set_section_contents (output_bfd, s->output_section,
9002 s->contents, s->output_offset,
9003 s->_raw_size))
9004 return FALSE;
9005 s = ppc64_elf_tdata (dynobj)->relgot;
9006 if (s != NULL
9007 && s->_raw_size != 0
9008 && s->output_section != bfd_abs_section_ptr
9009 && !bfd_set_section_contents (output_bfd, s->output_section,
9010 s->contents, s->output_offset,
9011 s->_raw_size))
9012 return FALSE;
9013 }
9014
9015 return TRUE;
9016 }
9017
9018 #include "elf64-target.h"