* elf64-ppc.c (TLS_GD_LD): Don't define..
[binutils.git] / bfd / elf64-ppc.c
blob8210af499a52b340aba92901eadf0f3a7ac21e66
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file is based on the 64-bit PowerPC ELF ABI. It is also based
23 on the file elf32-ppc.c. */
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "bfdlink.h"
28 #include "libbfd.h"
29 #include "elf-bfd.h"
30 #include "elf/ppc64.h"
31 #include "elf64-ppc.h"
33 static void ppc_howto_init
34 PARAMS ((void));
35 static reloc_howto_type *ppc64_elf_reloc_type_lookup
36 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
37 static void ppc64_elf_info_to_howto
38 PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
41 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
44 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
45 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
46 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
47 static bfd_reloc_status_type ppc64_elf_toc_reloc
48 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
49 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
50 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
51 static bfd_reloc_status_type ppc64_elf_toc64_reloc
52 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
53 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
54 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
55 static bfd_boolean ppc64_elf_object_p
56 PARAMS ((bfd *));
57 static bfd_boolean ppc64_elf_merge_private_bfd_data
58 PARAMS ((bfd *, bfd *));
59 static bfd_boolean ppc64_elf_new_section_hook
60 PARAMS ((bfd *, asection *));
63 /* The name of the dynamic interpreter. This is put in the .interp
64 section. */
65 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
67 /* The size in bytes of an entry in the procedure linkage table. */
68 #define PLT_ENTRY_SIZE 24
70 /* The initial size of the plt reserved for the dynamic linker. */
71 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
73 /* TOC base pointers offset from start of TOC. */
74 #define TOC_BASE_OFF 0x8000
76 /* Offset of tp and dtp pointers from start of TLS block. */
77 #define TP_OFFSET 0x7000
78 #define DTP_OFFSET 0x8000
80 /* .plt call stub instructions. */
81 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
82 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
83 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
84 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
85 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
86 /* ld %r11,xxx+16@l(%r12) */
87 #define BCTR 0x4e800420 /* bctr */
89 /* The normal stub is this size. */
90 #define PLT_CALL_STUB_SIZE (7*4)
92 /* But sometimes the .plt entry crosses a 64k boundary, and we need
93 to adjust the high word with this insn. */
94 #define ADDIS_R12_R12_1 0x3d8c0001 /* addis %r12,%r12,1 */
96 /* The .glink fixup call stub is the same as the .plt call stub, but
97 the first instruction restores r2, and the std is omitted. */
98 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
100 /* Always allow this much space. */
101 #define GLINK_CALL_STUB_SIZE (8*4)
103 /* Pad with this. */
104 #define NOP 0x60000000
106 /* Some other nops. */
107 #define CROR_151515 0x4def7b82
108 #define CROR_313131 0x4ffffb82
110 /* .glink entries for the first 32k functions are two instructions. */
111 #define LI_R0_0 0x38000000 /* li %r0,0 */
112 #define B_DOT 0x48000000 /* b . */
114 /* After that, we need two instructions to load the index, followed by
115 a branch. */
116 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
117 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
119 /* Instructions to save and restore floating point regs. */
120 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
121 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
122 #define BLR 0x4e800020 /* blr */
124 /* Since .opd is an array of descriptors and each entry will end up
125 with identical R_PPC64_RELATIVE relocs, there is really no need to
126 propagate .opd relocs; The dynamic linker should be taught to
127 relocate .opd without reloc entries. */
128 #ifndef NO_OPD_RELOCS
129 #define NO_OPD_RELOCS 0
130 #endif
132 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
134 /* Relocation HOWTO's. */
135 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
137 static reloc_howto_type ppc64_elf_howto_raw[] = {
138 /* This reloc does nothing. */
139 HOWTO (R_PPC64_NONE, /* type */
140 0, /* rightshift */
141 2, /* size (0 = byte, 1 = short, 2 = long) */
142 32, /* bitsize */
143 FALSE, /* pc_relative */
144 0, /* bitpos */
145 complain_overflow_dont, /* complain_on_overflow */
146 bfd_elf_generic_reloc, /* special_function */
147 "R_PPC64_NONE", /* name */
148 FALSE, /* partial_inplace */
149 0, /* src_mask */
150 0, /* dst_mask */
151 FALSE), /* pcrel_offset */
153 /* A standard 32 bit relocation. */
154 HOWTO (R_PPC64_ADDR32, /* type */
155 0, /* rightshift */
156 2, /* size (0 = byte, 1 = short, 2 = long) */
157 32, /* bitsize */
158 FALSE, /* pc_relative */
159 0, /* bitpos */
160 complain_overflow_bitfield, /* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_PPC64_ADDR32", /* name */
163 FALSE, /* partial_inplace */
164 0, /* src_mask */
165 0xffffffff, /* dst_mask */
166 FALSE), /* pcrel_offset */
168 /* An absolute 26 bit branch; the lower two bits must be zero.
169 FIXME: we don't check that, we just clear them. */
170 HOWTO (R_PPC64_ADDR24, /* type */
171 0, /* rightshift */
172 2, /* size (0 = byte, 1 = short, 2 = long) */
173 26, /* bitsize */
174 FALSE, /* pc_relative */
175 0, /* bitpos */
176 complain_overflow_bitfield, /* complain_on_overflow */
177 bfd_elf_generic_reloc, /* special_function */
178 "R_PPC64_ADDR24", /* name */
179 FALSE, /* partial_inplace */
180 0, /* src_mask */
181 0x03fffffc, /* dst_mask */
182 FALSE), /* pcrel_offset */
184 /* A standard 16 bit relocation. */
185 HOWTO (R_PPC64_ADDR16, /* type */
186 0, /* rightshift */
187 1, /* size (0 = byte, 1 = short, 2 = long) */
188 16, /* bitsize */
189 FALSE, /* pc_relative */
190 0, /* bitpos */
191 complain_overflow_bitfield, /* complain_on_overflow */
192 bfd_elf_generic_reloc, /* special_function */
193 "R_PPC64_ADDR16", /* name */
194 FALSE, /* partial_inplace */
195 0, /* src_mask */
196 0xffff, /* dst_mask */
197 FALSE), /* pcrel_offset */
199 /* A 16 bit relocation without overflow. */
200 HOWTO (R_PPC64_ADDR16_LO, /* type */
201 0, /* rightshift */
202 1, /* size (0 = byte, 1 = short, 2 = long) */
203 16, /* bitsize */
204 FALSE, /* pc_relative */
205 0, /* bitpos */
206 complain_overflow_dont,/* complain_on_overflow */
207 bfd_elf_generic_reloc, /* special_function */
208 "R_PPC64_ADDR16_LO", /* name */
209 FALSE, /* partial_inplace */
210 0, /* src_mask */
211 0xffff, /* dst_mask */
212 FALSE), /* pcrel_offset */
214 /* Bits 16-31 of an address. */
215 HOWTO (R_PPC64_ADDR16_HI, /* type */
216 16, /* rightshift */
217 1, /* size (0 = byte, 1 = short, 2 = long) */
218 16, /* bitsize */
219 FALSE, /* pc_relative */
220 0, /* bitpos */
221 complain_overflow_dont, /* complain_on_overflow */
222 bfd_elf_generic_reloc, /* special_function */
223 "R_PPC64_ADDR16_HI", /* name */
224 FALSE, /* partial_inplace */
225 0, /* src_mask */
226 0xffff, /* dst_mask */
227 FALSE), /* pcrel_offset */
229 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
230 bits, treated as a signed number, is negative. */
231 HOWTO (R_PPC64_ADDR16_HA, /* type */
232 16, /* rightshift */
233 1, /* size (0 = byte, 1 = short, 2 = long) */
234 16, /* bitsize */
235 FALSE, /* pc_relative */
236 0, /* bitpos */
237 complain_overflow_dont, /* complain_on_overflow */
238 ppc64_elf_ha_reloc, /* special_function */
239 "R_PPC64_ADDR16_HA", /* name */
240 FALSE, /* partial_inplace */
241 0, /* src_mask */
242 0xffff, /* dst_mask */
243 FALSE), /* pcrel_offset */
245 /* An absolute 16 bit branch; the lower two bits must be zero.
246 FIXME: we don't check that, we just clear them. */
247 HOWTO (R_PPC64_ADDR14, /* type */
248 0, /* rightshift */
249 2, /* size (0 = byte, 1 = short, 2 = long) */
250 16, /* bitsize */
251 FALSE, /* pc_relative */
252 0, /* bitpos */
253 complain_overflow_bitfield, /* complain_on_overflow */
254 bfd_elf_generic_reloc, /* special_function */
255 "R_PPC64_ADDR14", /* name */
256 FALSE, /* partial_inplace */
257 0, /* src_mask */
258 0x0000fffc, /* dst_mask */
259 FALSE), /* pcrel_offset */
261 /* An absolute 16 bit branch, for which bit 10 should be set to
262 indicate that the branch is expected to be taken. The lower two
263 bits must be zero. */
264 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
265 0, /* rightshift */
266 2, /* size (0 = byte, 1 = short, 2 = long) */
267 16, /* bitsize */
268 FALSE, /* pc_relative */
269 0, /* bitpos */
270 complain_overflow_bitfield, /* complain_on_overflow */
271 ppc64_elf_brtaken_reloc, /* special_function */
272 "R_PPC64_ADDR14_BRTAKEN",/* name */
273 FALSE, /* partial_inplace */
274 0, /* src_mask */
275 0x0000fffc, /* dst_mask */
276 FALSE), /* pcrel_offset */
278 /* An absolute 16 bit branch, for which bit 10 should be set to
279 indicate that the branch is not expected to be taken. The lower
280 two bits must be zero. */
281 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
282 0, /* rightshift */
283 2, /* size (0 = byte, 1 = short, 2 = long) */
284 16, /* bitsize */
285 FALSE, /* pc_relative */
286 0, /* bitpos */
287 complain_overflow_bitfield, /* complain_on_overflow */
288 ppc64_elf_brtaken_reloc, /* special_function */
289 "R_PPC64_ADDR14_BRNTAKEN",/* name */
290 FALSE, /* partial_inplace */
291 0, /* src_mask */
292 0x0000fffc, /* dst_mask */
293 FALSE), /* pcrel_offset */
295 /* A relative 26 bit branch; the lower two bits must be zero. */
296 HOWTO (R_PPC64_REL24, /* type */
297 0, /* rightshift */
298 2, /* size (0 = byte, 1 = short, 2 = long) */
299 26, /* bitsize */
300 TRUE, /* pc_relative */
301 0, /* bitpos */
302 complain_overflow_signed, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_PPC64_REL24", /* name */
305 FALSE, /* partial_inplace */
306 0, /* src_mask */
307 0x03fffffc, /* dst_mask */
308 TRUE), /* pcrel_offset */
310 /* A relative 16 bit branch; the lower two bits must be zero. */
311 HOWTO (R_PPC64_REL14, /* type */
312 0, /* rightshift */
313 2, /* size (0 = byte, 1 = short, 2 = long) */
314 16, /* bitsize */
315 TRUE, /* pc_relative */
316 0, /* bitpos */
317 complain_overflow_signed, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* special_function */
319 "R_PPC64_REL14", /* name */
320 FALSE, /* partial_inplace */
321 0, /* src_mask */
322 0x0000fffc, /* dst_mask */
323 TRUE), /* pcrel_offset */
325 /* A relative 16 bit branch. Bit 10 should be set to indicate that
326 the branch is expected to be taken. The lower two bits must be
327 zero. */
328 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
329 0, /* rightshift */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
331 16, /* bitsize */
332 TRUE, /* pc_relative */
333 0, /* bitpos */
334 complain_overflow_signed, /* complain_on_overflow */
335 ppc64_elf_brtaken_reloc, /* special_function */
336 "R_PPC64_REL14_BRTAKEN", /* name */
337 FALSE, /* partial_inplace */
338 0, /* src_mask */
339 0x0000fffc, /* dst_mask */
340 TRUE), /* pcrel_offset */
342 /* A relative 16 bit branch. Bit 10 should be set to indicate that
343 the branch is not expected to be taken. The lower two bits must
344 be zero. */
345 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
346 0, /* rightshift */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
348 16, /* bitsize */
349 TRUE, /* pc_relative */
350 0, /* bitpos */
351 complain_overflow_signed, /* complain_on_overflow */
352 ppc64_elf_brtaken_reloc, /* special_function */
353 "R_PPC64_REL14_BRNTAKEN",/* name */
354 FALSE, /* partial_inplace */
355 0, /* src_mask */
356 0x0000fffc, /* dst_mask */
357 TRUE), /* pcrel_offset */
359 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
360 symbol. */
361 HOWTO (R_PPC64_GOT16, /* type */
362 0, /* rightshift */
363 1, /* size (0 = byte, 1 = short, 2 = long) */
364 16, /* bitsize */
365 FALSE, /* pc_relative */
366 0, /* bitpos */
367 complain_overflow_signed, /* complain_on_overflow */
368 ppc64_elf_unhandled_reloc, /* special_function */
369 "R_PPC64_GOT16", /* name */
370 FALSE, /* partial_inplace */
371 0, /* src_mask */
372 0xffff, /* dst_mask */
373 FALSE), /* pcrel_offset */
375 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
376 the symbol. */
377 HOWTO (R_PPC64_GOT16_LO, /* type */
378 0, /* rightshift */
379 1, /* size (0 = byte, 1 = short, 2 = long) */
380 16, /* bitsize */
381 FALSE, /* pc_relative */
382 0, /* bitpos */
383 complain_overflow_dont, /* complain_on_overflow */
384 ppc64_elf_unhandled_reloc, /* special_function */
385 "R_PPC64_GOT16_LO", /* name */
386 FALSE, /* partial_inplace */
387 0, /* src_mask */
388 0xffff, /* dst_mask */
389 FALSE), /* pcrel_offset */
391 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
392 the symbol. */
393 HOWTO (R_PPC64_GOT16_HI, /* type */
394 16, /* rightshift */
395 1, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
397 FALSE, /* pc_relative */
398 0, /* bitpos */
399 complain_overflow_dont,/* complain_on_overflow */
400 ppc64_elf_unhandled_reloc, /* special_function */
401 "R_PPC64_GOT16_HI", /* name */
402 FALSE, /* partial_inplace */
403 0, /* src_mask */
404 0xffff, /* dst_mask */
405 FALSE), /* pcrel_offset */
407 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
408 the symbol. */
409 HOWTO (R_PPC64_GOT16_HA, /* type */
410 16, /* rightshift */
411 1, /* size (0 = byte, 1 = short, 2 = long) */
412 16, /* bitsize */
413 FALSE, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_dont,/* complain_on_overflow */
416 ppc64_elf_unhandled_reloc, /* special_function */
417 "R_PPC64_GOT16_HA", /* name */
418 FALSE, /* partial_inplace */
419 0, /* src_mask */
420 0xffff, /* dst_mask */
421 FALSE), /* pcrel_offset */
423 /* This is used only by the dynamic linker. The symbol should exist
424 both in the object being run and in some shared library. The
425 dynamic linker copies the data addressed by the symbol from the
426 shared library into the object, because the object being
427 run has to have the data at some particular address. */
428 HOWTO (R_PPC64_COPY, /* type */
429 0, /* rightshift */
430 0, /* this one is variable size */
431 0, /* 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_COPY", /* name */
437 FALSE, /* partial_inplace */
438 0, /* src_mask */
439 0, /* dst_mask */
440 FALSE), /* pcrel_offset */
442 /* Like R_PPC64_ADDR64, but used when setting global offset table
443 entries. */
444 HOWTO (R_PPC64_GLOB_DAT, /* type */
445 0, /* rightshift */
446 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
447 64, /* 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_GLOB_DAT", /* name */
453 FALSE, /* partial_inplace */
454 0, /* src_mask */
455 ONES (64), /* dst_mask */
456 FALSE), /* pcrel_offset */
458 /* Created by the link editor. Marks a procedure linkage table
459 entry for a symbol. */
460 HOWTO (R_PPC64_JMP_SLOT, /* type */
461 0, /* rightshift */
462 0, /* size (0 = byte, 1 = short, 2 = long) */
463 0, /* 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_JMP_SLOT", /* name */
469 FALSE, /* partial_inplace */
470 0, /* src_mask */
471 0, /* dst_mask */
472 FALSE), /* pcrel_offset */
474 /* Used only by the dynamic linker. When the object is run, this
475 doubleword64 is set to the load address of the object, plus the
476 addend. */
477 HOWTO (R_PPC64_RELATIVE, /* type */
478 0, /* rightshift */
479 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
480 64, /* bitsize */
481 FALSE, /* pc_relative */
482 0, /* bitpos */
483 complain_overflow_dont, /* complain_on_overflow */
484 bfd_elf_generic_reloc, /* special_function */
485 "R_PPC64_RELATIVE", /* name */
486 FALSE, /* partial_inplace */
487 0, /* src_mask */
488 ONES (64), /* dst_mask */
489 FALSE), /* pcrel_offset */
491 /* Like R_PPC64_ADDR32, but may be unaligned. */
492 HOWTO (R_PPC64_UADDR32, /* type */
493 0, /* rightshift */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
495 32, /* bitsize */
496 FALSE, /* pc_relative */
497 0, /* bitpos */
498 complain_overflow_bitfield, /* complain_on_overflow */
499 bfd_elf_generic_reloc, /* special_function */
500 "R_PPC64_UADDR32", /* name */
501 FALSE, /* partial_inplace */
502 0, /* src_mask */
503 0xffffffff, /* dst_mask */
504 FALSE), /* pcrel_offset */
506 /* Like R_PPC64_ADDR16, but may be unaligned. */
507 HOWTO (R_PPC64_UADDR16, /* type */
508 0, /* rightshift */
509 1, /* size (0 = byte, 1 = short, 2 = long) */
510 16, /* bitsize */
511 FALSE, /* pc_relative */
512 0, /* bitpos */
513 complain_overflow_bitfield, /* complain_on_overflow */
514 bfd_elf_generic_reloc, /* special_function */
515 "R_PPC64_UADDR16", /* name */
516 FALSE, /* partial_inplace */
517 0, /* src_mask */
518 0xffff, /* dst_mask */
519 FALSE), /* pcrel_offset */
521 /* 32-bit PC relative. */
522 HOWTO (R_PPC64_REL32, /* type */
523 0, /* rightshift */
524 2, /* size (0 = byte, 1 = short, 2 = long) */
525 32, /* bitsize */
526 TRUE, /* pc_relative */
527 0, /* bitpos */
528 /* FIXME: Verify. Was complain_overflow_bitfield. */
529 complain_overflow_signed, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_PPC64_REL32", /* name */
532 FALSE, /* partial_inplace */
533 0, /* src_mask */
534 0xffffffff, /* dst_mask */
535 TRUE), /* pcrel_offset */
537 /* 32-bit relocation to the symbol's procedure linkage table. */
538 HOWTO (R_PPC64_PLT32, /* type */
539 0, /* rightshift */
540 2, /* size (0 = byte, 1 = short, 2 = long) */
541 32, /* bitsize */
542 FALSE, /* pc_relative */
543 0, /* bitpos */
544 complain_overflow_bitfield, /* complain_on_overflow */
545 ppc64_elf_unhandled_reloc, /* special_function */
546 "R_PPC64_PLT32", /* name */
547 FALSE, /* partial_inplace */
548 0, /* src_mask */
549 0xffffffff, /* dst_mask */
550 FALSE), /* pcrel_offset */
552 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
553 FIXME: R_PPC64_PLTREL32 not supported. */
554 HOWTO (R_PPC64_PLTREL32, /* type */
555 0, /* rightshift */
556 2, /* size (0 = byte, 1 = short, 2 = long) */
557 32, /* bitsize */
558 TRUE, /* pc_relative */
559 0, /* bitpos */
560 complain_overflow_signed, /* complain_on_overflow */
561 bfd_elf_generic_reloc, /* special_function */
562 "R_PPC64_PLTREL32", /* name */
563 FALSE, /* partial_inplace */
564 0, /* src_mask */
565 0xffffffff, /* dst_mask */
566 TRUE), /* pcrel_offset */
568 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
569 the symbol. */
570 HOWTO (R_PPC64_PLT16_LO, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 FALSE, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_dont, /* complain_on_overflow */
577 ppc64_elf_unhandled_reloc, /* special_function */
578 "R_PPC64_PLT16_LO", /* name */
579 FALSE, /* partial_inplace */
580 0, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
584 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
585 the symbol. */
586 HOWTO (R_PPC64_PLT16_HI, /* type */
587 16, /* rightshift */
588 1, /* size (0 = byte, 1 = short, 2 = long) */
589 16, /* bitsize */
590 FALSE, /* pc_relative */
591 0, /* bitpos */
592 complain_overflow_dont, /* complain_on_overflow */
593 ppc64_elf_unhandled_reloc, /* special_function */
594 "R_PPC64_PLT16_HI", /* name */
595 FALSE, /* partial_inplace */
596 0, /* src_mask */
597 0xffff, /* dst_mask */
598 FALSE), /* pcrel_offset */
600 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
601 the symbol. */
602 HOWTO (R_PPC64_PLT16_HA, /* type */
603 16, /* rightshift */
604 1, /* size (0 = byte, 1 = short, 2 = long) */
605 16, /* bitsize */
606 FALSE, /* pc_relative */
607 0, /* bitpos */
608 complain_overflow_dont, /* complain_on_overflow */
609 ppc64_elf_unhandled_reloc, /* special_function */
610 "R_PPC64_PLT16_HA", /* name */
611 FALSE, /* partial_inplace */
612 0, /* src_mask */
613 0xffff, /* dst_mask */
614 FALSE), /* pcrel_offset */
616 /* 16-bit section relative relocation. */
617 HOWTO (R_PPC64_SECTOFF, /* type */
618 0, /* rightshift */
619 1, /* size (0 = byte, 1 = short, 2 = long) */
620 16, /* bitsize */
621 FALSE, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_bitfield, /* complain_on_overflow */
624 ppc64_elf_sectoff_reloc, /* special_function */
625 "R_PPC64_SECTOFF", /* name */
626 FALSE, /* partial_inplace */
627 0, /* src_mask */
628 0xffff, /* dst_mask */
629 FALSE), /* pcrel_offset */
631 /* Like R_PPC64_SECTOFF, but no overflow warning. */
632 HOWTO (R_PPC64_SECTOFF_LO, /* type */
633 0, /* rightshift */
634 1, /* size (0 = byte, 1 = short, 2 = long) */
635 16, /* bitsize */
636 FALSE, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_dont, /* complain_on_overflow */
639 ppc64_elf_sectoff_reloc, /* special_function */
640 "R_PPC64_SECTOFF_LO", /* name */
641 FALSE, /* partial_inplace */
642 0, /* src_mask */
643 0xffff, /* dst_mask */
644 FALSE), /* pcrel_offset */
646 /* 16-bit upper half section relative relocation. */
647 HOWTO (R_PPC64_SECTOFF_HI, /* type */
648 16, /* rightshift */
649 1, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
651 FALSE, /* pc_relative */
652 0, /* bitpos */
653 complain_overflow_dont, /* complain_on_overflow */
654 ppc64_elf_sectoff_reloc, /* special_function */
655 "R_PPC64_SECTOFF_HI", /* name */
656 FALSE, /* partial_inplace */
657 0, /* src_mask */
658 0xffff, /* dst_mask */
659 FALSE), /* pcrel_offset */
661 /* 16-bit upper half adjusted section relative relocation. */
662 HOWTO (R_PPC64_SECTOFF_HA, /* type */
663 16, /* rightshift */
664 1, /* size (0 = byte, 1 = short, 2 = long) */
665 16, /* bitsize */
666 FALSE, /* pc_relative */
667 0, /* bitpos */
668 complain_overflow_dont, /* complain_on_overflow */
669 ppc64_elf_sectoff_ha_reloc, /* special_function */
670 "R_PPC64_SECTOFF_HA", /* name */
671 FALSE, /* partial_inplace */
672 0, /* src_mask */
673 0xffff, /* dst_mask */
674 FALSE), /* pcrel_offset */
676 /* Like R_PPC64_REL24 without touching the two least significant bits. */
677 HOWTO (R_PPC64_REL30, /* type */
678 2, /* rightshift */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
680 30, /* bitsize */
681 TRUE, /* pc_relative */
682 0, /* bitpos */
683 complain_overflow_dont, /* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
685 "R_PPC64_REL30", /* name */
686 FALSE, /* partial_inplace */
687 0, /* src_mask */
688 0xfffffffc, /* dst_mask */
689 TRUE), /* pcrel_offset */
691 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
693 /* A standard 64-bit relocation. */
694 HOWTO (R_PPC64_ADDR64, /* type */
695 0, /* rightshift */
696 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
697 64, /* bitsize */
698 FALSE, /* pc_relative */
699 0, /* bitpos */
700 complain_overflow_dont, /* complain_on_overflow */
701 bfd_elf_generic_reloc, /* special_function */
702 "R_PPC64_ADDR64", /* name */
703 FALSE, /* partial_inplace */
704 0, /* src_mask */
705 ONES (64), /* dst_mask */
706 FALSE), /* pcrel_offset */
708 /* The bits 32-47 of an address. */
709 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
710 32, /* rightshift */
711 1, /* size (0 = byte, 1 = short, 2 = long) */
712 16, /* bitsize */
713 FALSE, /* pc_relative */
714 0, /* bitpos */
715 complain_overflow_dont, /* complain_on_overflow */
716 bfd_elf_generic_reloc, /* special_function */
717 "R_PPC64_ADDR16_HIGHER", /* name */
718 FALSE, /* partial_inplace */
719 0, /* src_mask */
720 0xffff, /* dst_mask */
721 FALSE), /* pcrel_offset */
723 /* The bits 32-47 of an address, plus 1 if the contents of the low
724 16 bits, treated as a signed number, is negative. */
725 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
726 32, /* rightshift */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
728 16, /* bitsize */
729 FALSE, /* pc_relative */
730 0, /* bitpos */
731 complain_overflow_dont, /* complain_on_overflow */
732 ppc64_elf_ha_reloc, /* special_function */
733 "R_PPC64_ADDR16_HIGHERA", /* name */
734 FALSE, /* partial_inplace */
735 0, /* src_mask */
736 0xffff, /* dst_mask */
737 FALSE), /* pcrel_offset */
739 /* The bits 48-63 of an address. */
740 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
741 48, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
744 FALSE, /* pc_relative */
745 0, /* bitpos */
746 complain_overflow_dont, /* complain_on_overflow */
747 bfd_elf_generic_reloc, /* special_function */
748 "R_PPC64_ADDR16_HIGHEST", /* name */
749 FALSE, /* partial_inplace */
750 0, /* src_mask */
751 0xffff, /* dst_mask */
752 FALSE), /* pcrel_offset */
754 /* The bits 48-63 of an address, plus 1 if the contents of the low
755 16 bits, treated as a signed number, is negative. */
756 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
757 48, /* rightshift */
758 1, /* size (0 = byte, 1 = short, 2 = long) */
759 16, /* bitsize */
760 FALSE, /* pc_relative */
761 0, /* bitpos */
762 complain_overflow_dont, /* complain_on_overflow */
763 ppc64_elf_ha_reloc, /* special_function */
764 "R_PPC64_ADDR16_HIGHESTA", /* name */
765 FALSE, /* partial_inplace */
766 0, /* src_mask */
767 0xffff, /* dst_mask */
768 FALSE), /* pcrel_offset */
770 /* Like ADDR64, but may be unaligned. */
771 HOWTO (R_PPC64_UADDR64, /* type */
772 0, /* rightshift */
773 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
774 64, /* bitsize */
775 FALSE, /* pc_relative */
776 0, /* bitpos */
777 complain_overflow_dont, /* complain_on_overflow */
778 bfd_elf_generic_reloc, /* special_function */
779 "R_PPC64_UADDR64", /* name */
780 FALSE, /* partial_inplace */
781 0, /* src_mask */
782 ONES (64), /* dst_mask */
783 FALSE), /* pcrel_offset */
785 /* 64-bit relative relocation. */
786 HOWTO (R_PPC64_REL64, /* type */
787 0, /* rightshift */
788 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
789 64, /* bitsize */
790 TRUE, /* pc_relative */
791 0, /* bitpos */
792 complain_overflow_dont, /* complain_on_overflow */
793 bfd_elf_generic_reloc, /* special_function */
794 "R_PPC64_REL64", /* name */
795 FALSE, /* partial_inplace */
796 0, /* src_mask */
797 ONES (64), /* dst_mask */
798 TRUE), /* pcrel_offset */
800 /* 64-bit relocation to the symbol's procedure linkage table. */
801 HOWTO (R_PPC64_PLT64, /* type */
802 0, /* rightshift */
803 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
804 64, /* bitsize */
805 FALSE, /* pc_relative */
806 0, /* bitpos */
807 complain_overflow_dont, /* complain_on_overflow */
808 ppc64_elf_unhandled_reloc, /* special_function */
809 "R_PPC64_PLT64", /* name */
810 FALSE, /* partial_inplace */
811 0, /* src_mask */
812 ONES (64), /* dst_mask */
813 FALSE), /* pcrel_offset */
815 /* 64-bit PC relative relocation to the symbol's procedure linkage
816 table. */
817 /* FIXME: R_PPC64_PLTREL64 not supported. */
818 HOWTO (R_PPC64_PLTREL64, /* type */
819 0, /* rightshift */
820 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
821 64, /* bitsize */
822 TRUE, /* pc_relative */
823 0, /* bitpos */
824 complain_overflow_dont, /* complain_on_overflow */
825 ppc64_elf_unhandled_reloc, /* special_function */
826 "R_PPC64_PLTREL64", /* name */
827 FALSE, /* partial_inplace */
828 0, /* src_mask */
829 ONES (64), /* dst_mask */
830 TRUE), /* pcrel_offset */
832 /* 16 bit TOC-relative relocation. */
834 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
835 HOWTO (R_PPC64_TOC16, /* type */
836 0, /* rightshift */
837 1, /* size (0 = byte, 1 = short, 2 = long) */
838 16, /* bitsize */
839 FALSE, /* pc_relative */
840 0, /* bitpos */
841 complain_overflow_signed, /* complain_on_overflow */
842 ppc64_elf_toc_reloc, /* special_function */
843 "R_PPC64_TOC16", /* name */
844 FALSE, /* partial_inplace */
845 0, /* src_mask */
846 0xffff, /* dst_mask */
847 FALSE), /* pcrel_offset */
849 /* 16 bit TOC-relative relocation without overflow. */
851 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
852 HOWTO (R_PPC64_TOC16_LO, /* type */
853 0, /* rightshift */
854 1, /* size (0 = byte, 1 = short, 2 = long) */
855 16, /* bitsize */
856 FALSE, /* pc_relative */
857 0, /* bitpos */
858 complain_overflow_dont, /* complain_on_overflow */
859 ppc64_elf_toc_reloc, /* special_function */
860 "R_PPC64_TOC16_LO", /* name */
861 FALSE, /* partial_inplace */
862 0, /* src_mask */
863 0xffff, /* dst_mask */
864 FALSE), /* pcrel_offset */
866 /* 16 bit TOC-relative relocation, high 16 bits. */
868 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
869 HOWTO (R_PPC64_TOC16_HI, /* type */
870 16, /* rightshift */
871 1, /* size (0 = byte, 1 = short, 2 = long) */
872 16, /* bitsize */
873 FALSE, /* pc_relative */
874 0, /* bitpos */
875 complain_overflow_dont, /* complain_on_overflow */
876 ppc64_elf_toc_reloc, /* special_function */
877 "R_PPC64_TOC16_HI", /* name */
878 FALSE, /* partial_inplace */
879 0, /* src_mask */
880 0xffff, /* dst_mask */
881 FALSE), /* pcrel_offset */
883 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
884 contents of the low 16 bits, treated as a signed number, is
885 negative. */
887 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
888 HOWTO (R_PPC64_TOC16_HA, /* type */
889 16, /* rightshift */
890 1, /* size (0 = byte, 1 = short, 2 = long) */
891 16, /* bitsize */
892 FALSE, /* pc_relative */
893 0, /* bitpos */
894 complain_overflow_dont, /* complain_on_overflow */
895 ppc64_elf_toc_ha_reloc, /* special_function */
896 "R_PPC64_TOC16_HA", /* name */
897 FALSE, /* partial_inplace */
898 0, /* src_mask */
899 0xffff, /* dst_mask */
900 FALSE), /* pcrel_offset */
902 /* 64-bit relocation; insert value of TOC base (.TOC.). */
904 /* R_PPC64_TOC 51 doubleword64 .TOC. */
905 HOWTO (R_PPC64_TOC, /* type */
906 0, /* rightshift */
907 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
908 64, /* bitsize */
909 FALSE, /* pc_relative */
910 0, /* bitpos */
911 complain_overflow_bitfield, /* complain_on_overflow */
912 ppc64_elf_toc64_reloc, /* special_function */
913 "R_PPC64_TOC", /* name */
914 FALSE, /* partial_inplace */
915 0, /* src_mask */
916 ONES (64), /* dst_mask */
917 FALSE), /* pcrel_offset */
919 /* Like R_PPC64_GOT16, but also informs the link editor that the
920 value to relocate may (!) refer to a PLT entry which the link
921 editor (a) may replace with the symbol value. If the link editor
922 is unable to fully resolve the symbol, it may (b) create a PLT
923 entry and store the address to the new PLT entry in the GOT.
924 This permits lazy resolution of function symbols at run time.
925 The link editor may also skip all of this and just (c) emit a
926 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
927 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
928 HOWTO (R_PPC64_PLTGOT16, /* type */
929 0, /* rightshift */
930 1, /* size (0 = byte, 1 = short, 2 = long) */
931 16, /* bitsize */
932 FALSE, /* pc_relative */
933 0, /* bitpos */
934 complain_overflow_signed, /* complain_on_overflow */
935 ppc64_elf_unhandled_reloc, /* special_function */
936 "R_PPC64_PLTGOT16", /* name */
937 FALSE, /* partial_inplace */
938 0, /* src_mask */
939 0xffff, /* dst_mask */
940 FALSE), /* pcrel_offset */
942 /* Like R_PPC64_PLTGOT16, but without overflow. */
943 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
944 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
945 0, /* rightshift */
946 1, /* size (0 = byte, 1 = short, 2 = long) */
947 16, /* bitsize */
948 FALSE, /* pc_relative */
949 0, /* bitpos */
950 complain_overflow_dont, /* complain_on_overflow */
951 ppc64_elf_unhandled_reloc, /* special_function */
952 "R_PPC64_PLTGOT16_LO", /* name */
953 FALSE, /* partial_inplace */
954 0, /* src_mask */
955 0xffff, /* dst_mask */
956 FALSE), /* pcrel_offset */
958 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
959 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
960 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
961 16, /* rightshift */
962 1, /* size (0 = byte, 1 = short, 2 = long) */
963 16, /* bitsize */
964 FALSE, /* pc_relative */
965 0, /* bitpos */
966 complain_overflow_dont, /* complain_on_overflow */
967 ppc64_elf_unhandled_reloc, /* special_function */
968 "R_PPC64_PLTGOT16_HI", /* name */
969 FALSE, /* partial_inplace */
970 0, /* src_mask */
971 0xffff, /* dst_mask */
972 FALSE), /* pcrel_offset */
974 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
975 1 if the contents of the low 16 bits, treated as a signed number,
976 is negative. */
977 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
978 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
979 16, /* rightshift */
980 1, /* size (0 = byte, 1 = short, 2 = long) */
981 16, /* bitsize */
982 FALSE, /* pc_relative */
983 0, /* bitpos */
984 complain_overflow_dont,/* complain_on_overflow */
985 ppc64_elf_unhandled_reloc, /* special_function */
986 "R_PPC64_PLTGOT16_HA", /* name */
987 FALSE, /* partial_inplace */
988 0, /* src_mask */
989 0xffff, /* dst_mask */
990 FALSE), /* pcrel_offset */
992 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
993 HOWTO (R_PPC64_ADDR16_DS, /* type */
994 0, /* rightshift */
995 1, /* size (0 = byte, 1 = short, 2 = long) */
996 16, /* bitsize */
997 FALSE, /* pc_relative */
998 0, /* bitpos */
999 complain_overflow_bitfield, /* complain_on_overflow */
1000 bfd_elf_generic_reloc, /* special_function */
1001 "R_PPC64_ADDR16_DS", /* name */
1002 FALSE, /* partial_inplace */
1003 0, /* src_mask */
1004 0xfffc, /* dst_mask */
1005 FALSE), /* pcrel_offset */
1007 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1008 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1009 0, /* rightshift */
1010 1, /* size (0 = byte, 1 = short, 2 = long) */
1011 16, /* bitsize */
1012 FALSE, /* pc_relative */
1013 0, /* bitpos */
1014 complain_overflow_dont,/* complain_on_overflow */
1015 bfd_elf_generic_reloc, /* special_function */
1016 "R_PPC64_ADDR16_LO_DS",/* name */
1017 FALSE, /* partial_inplace */
1018 0, /* src_mask */
1019 0xfffc, /* dst_mask */
1020 FALSE), /* pcrel_offset */
1022 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1023 HOWTO (R_PPC64_GOT16_DS, /* type */
1024 0, /* rightshift */
1025 1, /* size (0 = byte, 1 = short, 2 = long) */
1026 16, /* bitsize */
1027 FALSE, /* pc_relative */
1028 0, /* bitpos */
1029 complain_overflow_signed, /* complain_on_overflow */
1030 ppc64_elf_unhandled_reloc, /* special_function */
1031 "R_PPC64_GOT16_DS", /* name */
1032 FALSE, /* partial_inplace */
1033 0, /* src_mask */
1034 0xfffc, /* dst_mask */
1035 FALSE), /* pcrel_offset */
1037 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1038 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1039 0, /* rightshift */
1040 1, /* size (0 = byte, 1 = short, 2 = long) */
1041 16, /* bitsize */
1042 FALSE, /* pc_relative */
1043 0, /* bitpos */
1044 complain_overflow_dont, /* complain_on_overflow */
1045 ppc64_elf_unhandled_reloc, /* special_function */
1046 "R_PPC64_GOT16_LO_DS", /* name */
1047 FALSE, /* partial_inplace */
1048 0, /* src_mask */
1049 0xfffc, /* dst_mask */
1050 FALSE), /* pcrel_offset */
1052 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1053 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1054 0, /* rightshift */
1055 1, /* size (0 = byte, 1 = short, 2 = long) */
1056 16, /* bitsize */
1057 FALSE, /* pc_relative */
1058 0, /* bitpos */
1059 complain_overflow_dont, /* complain_on_overflow */
1060 ppc64_elf_unhandled_reloc, /* special_function */
1061 "R_PPC64_PLT16_LO_DS", /* name */
1062 FALSE, /* partial_inplace */
1063 0, /* src_mask */
1064 0xfffc, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1067 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1068 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1069 0, /* rightshift */
1070 1, /* size (0 = byte, 1 = short, 2 = long) */
1071 16, /* bitsize */
1072 FALSE, /* pc_relative */
1073 0, /* bitpos */
1074 complain_overflow_bitfield, /* complain_on_overflow */
1075 ppc64_elf_sectoff_reloc, /* special_function */
1076 "R_PPC64_SECTOFF_DS", /* name */
1077 FALSE, /* partial_inplace */
1078 0, /* src_mask */
1079 0xfffc, /* dst_mask */
1080 FALSE), /* pcrel_offset */
1082 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1083 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1084 0, /* rightshift */
1085 1, /* size (0 = byte, 1 = short, 2 = long) */
1086 16, /* bitsize */
1087 FALSE, /* pc_relative */
1088 0, /* bitpos */
1089 complain_overflow_dont, /* complain_on_overflow */
1090 ppc64_elf_sectoff_reloc, /* special_function */
1091 "R_PPC64_SECTOFF_LO_DS",/* name */
1092 FALSE, /* partial_inplace */
1093 0, /* src_mask */
1094 0xfffc, /* dst_mask */
1095 FALSE), /* pcrel_offset */
1097 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1098 HOWTO (R_PPC64_TOC16_DS, /* type */
1099 0, /* rightshift */
1100 1, /* size (0 = byte, 1 = short, 2 = long) */
1101 16, /* bitsize */
1102 FALSE, /* pc_relative */
1103 0, /* bitpos */
1104 complain_overflow_signed, /* complain_on_overflow */
1105 ppc64_elf_toc_reloc, /* special_function */
1106 "R_PPC64_TOC16_DS", /* name */
1107 FALSE, /* partial_inplace */
1108 0, /* src_mask */
1109 0xfffc, /* dst_mask */
1110 FALSE), /* pcrel_offset */
1112 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1113 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1114 0, /* rightshift */
1115 1, /* size (0 = byte, 1 = short, 2 = long) */
1116 16, /* bitsize */
1117 FALSE, /* pc_relative */
1118 0, /* bitpos */
1119 complain_overflow_dont, /* complain_on_overflow */
1120 ppc64_elf_toc_reloc, /* special_function */
1121 "R_PPC64_TOC16_LO_DS", /* name */
1122 FALSE, /* partial_inplace */
1123 0, /* src_mask */
1124 0xfffc, /* dst_mask */
1125 FALSE), /* pcrel_offset */
1127 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1128 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1129 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1130 0, /* rightshift */
1131 1, /* size (0 = byte, 1 = short, 2 = long) */
1132 16, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_signed, /* complain_on_overflow */
1136 ppc64_elf_unhandled_reloc, /* special_function */
1137 "R_PPC64_PLTGOT16_DS", /* name */
1138 FALSE, /* partial_inplace */
1139 0, /* src_mask */
1140 0xfffc, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1143 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1144 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1145 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1146 0, /* rightshift */
1147 1, /* size (0 = byte, 1 = short, 2 = long) */
1148 16, /* bitsize */
1149 FALSE, /* pc_relative */
1150 0, /* bitpos */
1151 complain_overflow_dont, /* complain_on_overflow */
1152 ppc64_elf_unhandled_reloc, /* special_function */
1153 "R_PPC64_PLTGOT16_LO_DS",/* name */
1154 FALSE, /* partial_inplace */
1155 0, /* src_mask */
1156 0xfffc, /* dst_mask */
1157 FALSE), /* pcrel_offset */
1159 /* Marker reloc for TLS. */
1160 HOWTO (R_PPC64_TLS,
1161 0, /* rightshift */
1162 2, /* size (0 = byte, 1 = short, 2 = long) */
1163 32, /* bitsize */
1164 FALSE, /* pc_relative */
1165 0, /* bitpos */
1166 complain_overflow_dont, /* complain_on_overflow */
1167 bfd_elf_generic_reloc, /* special_function */
1168 "R_PPC64_TLS", /* name */
1169 FALSE, /* partial_inplace */
1170 0, /* src_mask */
1171 0, /* dst_mask */
1172 FALSE), /* pcrel_offset */
1174 /* Computes the load module index of the load module that contains the
1175 definition of its TLS sym. */
1176 HOWTO (R_PPC64_DTPMOD64,
1177 0, /* rightshift */
1178 4, /* size (0 = byte, 1 = short, 2 = long) */
1179 64, /* bitsize */
1180 FALSE, /* pc_relative */
1181 0, /* bitpos */
1182 complain_overflow_dont, /* complain_on_overflow */
1183 ppc64_elf_unhandled_reloc, /* special_function */
1184 "R_PPC64_DTPMOD64", /* name */
1185 FALSE, /* partial_inplace */
1186 0, /* src_mask */
1187 ONES (64), /* dst_mask */
1188 FALSE), /* pcrel_offset */
1190 /* Computes a dtv-relative displacement, the difference between the value
1191 of sym+add and the base address of the thread-local storage block that
1192 contains the definition of sym, minus 0x8000. */
1193 HOWTO (R_PPC64_DTPREL64,
1194 0, /* rightshift */
1195 4, /* size (0 = byte, 1 = short, 2 = long) */
1196 64, /* bitsize */
1197 FALSE, /* pc_relative */
1198 0, /* bitpos */
1199 complain_overflow_dont, /* complain_on_overflow */
1200 ppc64_elf_unhandled_reloc, /* special_function */
1201 "R_PPC64_DTPREL64", /* name */
1202 FALSE, /* partial_inplace */
1203 0, /* src_mask */
1204 ONES (64), /* dst_mask */
1205 FALSE), /* pcrel_offset */
1207 /* A 16 bit dtprel reloc. */
1208 HOWTO (R_PPC64_DTPREL16,
1209 0, /* rightshift */
1210 1, /* size (0 = byte, 1 = short, 2 = long) */
1211 16, /* bitsize */
1212 FALSE, /* pc_relative */
1213 0, /* bitpos */
1214 complain_overflow_signed, /* complain_on_overflow */
1215 ppc64_elf_unhandled_reloc, /* special_function */
1216 "R_PPC64_DTPREL16", /* name */
1217 FALSE, /* partial_inplace */
1218 0, /* src_mask */
1219 0xffff, /* dst_mask */
1220 FALSE), /* pcrel_offset */
1222 /* Like DTPREL16, but no overflow. */
1223 HOWTO (R_PPC64_DTPREL16_LO,
1224 0, /* rightshift */
1225 1, /* size (0 = byte, 1 = short, 2 = long) */
1226 16, /* bitsize */
1227 FALSE, /* pc_relative */
1228 0, /* bitpos */
1229 complain_overflow_dont, /* complain_on_overflow */
1230 ppc64_elf_unhandled_reloc, /* special_function */
1231 "R_PPC64_DTPREL16_LO", /* name */
1232 FALSE, /* partial_inplace */
1233 0, /* src_mask */
1234 0xffff, /* dst_mask */
1235 FALSE), /* pcrel_offset */
1237 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1238 HOWTO (R_PPC64_DTPREL16_HI,
1239 16, /* rightshift */
1240 1, /* size (0 = byte, 1 = short, 2 = long) */
1241 16, /* bitsize */
1242 FALSE, /* pc_relative */
1243 0, /* bitpos */
1244 complain_overflow_dont, /* complain_on_overflow */
1245 ppc64_elf_unhandled_reloc, /* special_function */
1246 "R_PPC64_DTPREL16_HI", /* name */
1247 FALSE, /* partial_inplace */
1248 0, /* src_mask */
1249 0xffff, /* dst_mask */
1250 FALSE), /* pcrel_offset */
1252 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1253 HOWTO (R_PPC64_DTPREL16_HA,
1254 16, /* rightshift */
1255 1, /* size (0 = byte, 1 = short, 2 = long) */
1256 16, /* bitsize */
1257 FALSE, /* pc_relative */
1258 0, /* bitpos */
1259 complain_overflow_dont, /* complain_on_overflow */
1260 ppc64_elf_unhandled_reloc, /* special_function */
1261 "R_PPC64_DTPREL16_HA", /* name */
1262 FALSE, /* partial_inplace */
1263 0, /* src_mask */
1264 0xffff, /* dst_mask */
1265 FALSE), /* pcrel_offset */
1267 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1268 HOWTO (R_PPC64_DTPREL16_HIGHER,
1269 32, /* rightshift */
1270 1, /* size (0 = byte, 1 = short, 2 = long) */
1271 16, /* bitsize */
1272 FALSE, /* pc_relative */
1273 0, /* bitpos */
1274 complain_overflow_dont, /* complain_on_overflow */
1275 ppc64_elf_unhandled_reloc, /* special_function */
1276 "R_PPC64_DTPREL16_HIGHER", /* name */
1277 FALSE, /* partial_inplace */
1278 0, /* src_mask */
1279 0xffff, /* dst_mask */
1280 FALSE), /* pcrel_offset */
1282 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1283 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1284 32, /* rightshift */
1285 1, /* size (0 = byte, 1 = short, 2 = long) */
1286 16, /* bitsize */
1287 FALSE, /* pc_relative */
1288 0, /* bitpos */
1289 complain_overflow_dont, /* complain_on_overflow */
1290 ppc64_elf_unhandled_reloc, /* special_function */
1291 "R_PPC64_DTPREL16_HIGHERA", /* name */
1292 FALSE, /* partial_inplace */
1293 0, /* src_mask */
1294 0xffff, /* dst_mask */
1295 FALSE), /* pcrel_offset */
1297 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1298 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1299 48, /* rightshift */
1300 1, /* size (0 = byte, 1 = short, 2 = long) */
1301 16, /* bitsize */
1302 FALSE, /* pc_relative */
1303 0, /* bitpos */
1304 complain_overflow_dont, /* complain_on_overflow */
1305 ppc64_elf_unhandled_reloc, /* special_function */
1306 "R_PPC64_DTPREL16_HIGHEST", /* name */
1307 FALSE, /* partial_inplace */
1308 0, /* src_mask */
1309 0xffff, /* dst_mask */
1310 FALSE), /* pcrel_offset */
1312 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1313 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1314 48, /* rightshift */
1315 1, /* size (0 = byte, 1 = short, 2 = long) */
1316 16, /* bitsize */
1317 FALSE, /* pc_relative */
1318 0, /* bitpos */
1319 complain_overflow_dont, /* complain_on_overflow */
1320 ppc64_elf_unhandled_reloc, /* special_function */
1321 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1322 FALSE, /* partial_inplace */
1323 0, /* src_mask */
1324 0xffff, /* dst_mask */
1325 FALSE), /* pcrel_offset */
1327 /* Like DTPREL16, but for insns with a DS field. */
1328 HOWTO (R_PPC64_DTPREL16_DS,
1329 0, /* rightshift */
1330 1, /* size (0 = byte, 1 = short, 2 = long) */
1331 16, /* bitsize */
1332 FALSE, /* pc_relative */
1333 0, /* bitpos */
1334 complain_overflow_signed, /* complain_on_overflow */
1335 ppc64_elf_unhandled_reloc, /* special_function */
1336 "R_PPC64_DTPREL16_DS", /* name */
1337 FALSE, /* partial_inplace */
1338 0, /* src_mask */
1339 0xfffc, /* dst_mask */
1340 FALSE), /* pcrel_offset */
1342 /* Like DTPREL16_DS, but no overflow. */
1343 HOWTO (R_PPC64_DTPREL16_LO_DS,
1344 0, /* rightshift */
1345 1, /* size (0 = byte, 1 = short, 2 = long) */
1346 16, /* bitsize */
1347 FALSE, /* pc_relative */
1348 0, /* bitpos */
1349 complain_overflow_dont, /* complain_on_overflow */
1350 ppc64_elf_unhandled_reloc, /* special_function */
1351 "R_PPC64_DTPREL16_LO_DS", /* name */
1352 FALSE, /* partial_inplace */
1353 0, /* src_mask */
1354 0xfffc, /* dst_mask */
1355 FALSE), /* pcrel_offset */
1357 /* Computes a tp-relative displacement, the difference between the value of
1358 sym+add and the value of the thread pointer (r13). */
1359 HOWTO (R_PPC64_TPREL64,
1360 0, /* rightshift */
1361 4, /* size (0 = byte, 1 = short, 2 = long) */
1362 64, /* bitsize */
1363 FALSE, /* pc_relative */
1364 0, /* bitpos */
1365 complain_overflow_dont, /* complain_on_overflow */
1366 ppc64_elf_unhandled_reloc, /* special_function */
1367 "R_PPC64_TPREL64", /* name */
1368 FALSE, /* partial_inplace */
1369 0, /* src_mask */
1370 ONES (64), /* dst_mask */
1371 FALSE), /* pcrel_offset */
1373 /* A 16 bit tprel reloc. */
1374 HOWTO (R_PPC64_TPREL16,
1375 0, /* rightshift */
1376 1, /* size (0 = byte, 1 = short, 2 = long) */
1377 16, /* bitsize */
1378 FALSE, /* pc_relative */
1379 0, /* bitpos */
1380 complain_overflow_signed, /* complain_on_overflow */
1381 ppc64_elf_unhandled_reloc, /* special_function */
1382 "R_PPC64_TPREL16", /* name */
1383 FALSE, /* partial_inplace */
1384 0, /* src_mask */
1385 0xffff, /* dst_mask */
1386 FALSE), /* pcrel_offset */
1388 /* Like TPREL16, but no overflow. */
1389 HOWTO (R_PPC64_TPREL16_LO,
1390 0, /* rightshift */
1391 1, /* size (0 = byte, 1 = short, 2 = long) */
1392 16, /* bitsize */
1393 FALSE, /* pc_relative */
1394 0, /* bitpos */
1395 complain_overflow_dont, /* complain_on_overflow */
1396 ppc64_elf_unhandled_reloc, /* special_function */
1397 "R_PPC64_TPREL16_LO", /* name */
1398 FALSE, /* partial_inplace */
1399 0, /* src_mask */
1400 0xffff, /* dst_mask */
1401 FALSE), /* pcrel_offset */
1403 /* Like TPREL16_LO, but next higher group of 16 bits. */
1404 HOWTO (R_PPC64_TPREL16_HI,
1405 16, /* rightshift */
1406 1, /* size (0 = byte, 1 = short, 2 = long) */
1407 16, /* bitsize */
1408 FALSE, /* pc_relative */
1409 0, /* bitpos */
1410 complain_overflow_dont, /* complain_on_overflow */
1411 ppc64_elf_unhandled_reloc, /* special_function */
1412 "R_PPC64_TPREL16_HI", /* name */
1413 FALSE, /* partial_inplace */
1414 0, /* src_mask */
1415 0xffff, /* dst_mask */
1416 FALSE), /* pcrel_offset */
1418 /* Like TPREL16_HI, but adjust for low 16 bits. */
1419 HOWTO (R_PPC64_TPREL16_HA,
1420 16, /* rightshift */
1421 1, /* size (0 = byte, 1 = short, 2 = long) */
1422 16, /* bitsize */
1423 FALSE, /* pc_relative */
1424 0, /* bitpos */
1425 complain_overflow_dont, /* complain_on_overflow */
1426 ppc64_elf_unhandled_reloc, /* special_function */
1427 "R_PPC64_TPREL16_HA", /* name */
1428 FALSE, /* partial_inplace */
1429 0, /* src_mask */
1430 0xffff, /* dst_mask */
1431 FALSE), /* pcrel_offset */
1433 /* Like TPREL16_HI, but next higher group of 16 bits. */
1434 HOWTO (R_PPC64_TPREL16_HIGHER,
1435 32, /* rightshift */
1436 1, /* size (0 = byte, 1 = short, 2 = long) */
1437 16, /* bitsize */
1438 FALSE, /* pc_relative */
1439 0, /* bitpos */
1440 complain_overflow_dont, /* complain_on_overflow */
1441 ppc64_elf_unhandled_reloc, /* special_function */
1442 "R_PPC64_TPREL16_HIGHER", /* name */
1443 FALSE, /* partial_inplace */
1444 0, /* src_mask */
1445 0xffff, /* dst_mask */
1446 FALSE), /* pcrel_offset */
1448 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1449 HOWTO (R_PPC64_TPREL16_HIGHERA,
1450 32, /* rightshift */
1451 1, /* size (0 = byte, 1 = short, 2 = long) */
1452 16, /* bitsize */
1453 FALSE, /* pc_relative */
1454 0, /* bitpos */
1455 complain_overflow_dont, /* complain_on_overflow */
1456 ppc64_elf_unhandled_reloc, /* special_function */
1457 "R_PPC64_TPREL16_HIGHERA", /* name */
1458 FALSE, /* partial_inplace */
1459 0, /* src_mask */
1460 0xffff, /* dst_mask */
1461 FALSE), /* pcrel_offset */
1463 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1464 HOWTO (R_PPC64_TPREL16_HIGHEST,
1465 48, /* rightshift */
1466 1, /* size (0 = byte, 1 = short, 2 = long) */
1467 16, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_dont, /* complain_on_overflow */
1471 ppc64_elf_unhandled_reloc, /* special_function */
1472 "R_PPC64_TPREL16_HIGHEST", /* name */
1473 FALSE, /* partial_inplace */
1474 0, /* src_mask */
1475 0xffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1478 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1479 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1480 48, /* rightshift */
1481 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 16, /* bitsize */
1483 FALSE, /* pc_relative */
1484 0, /* bitpos */
1485 complain_overflow_dont, /* complain_on_overflow */
1486 ppc64_elf_unhandled_reloc, /* special_function */
1487 "R_PPC64_TPREL16_HIGHESTA", /* name */
1488 FALSE, /* partial_inplace */
1489 0, /* src_mask */
1490 0xffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
1493 /* Like TPREL16, but for insns with a DS field. */
1494 HOWTO (R_PPC64_TPREL16_DS,
1495 0, /* rightshift */
1496 1, /* size (0 = byte, 1 = short, 2 = long) */
1497 16, /* bitsize */
1498 FALSE, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_signed, /* complain_on_overflow */
1501 ppc64_elf_unhandled_reloc, /* special_function */
1502 "R_PPC64_TPREL16_DS", /* name */
1503 FALSE, /* partial_inplace */
1504 0, /* src_mask */
1505 0xfffc, /* dst_mask */
1506 FALSE), /* pcrel_offset */
1508 /* Like TPREL16_DS, but no overflow. */
1509 HOWTO (R_PPC64_TPREL16_LO_DS,
1510 0, /* rightshift */
1511 1, /* size (0 = byte, 1 = short, 2 = long) */
1512 16, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_dont, /* complain_on_overflow */
1516 ppc64_elf_unhandled_reloc, /* special_function */
1517 "R_PPC64_TPREL16_LO_DS", /* name */
1518 FALSE, /* partial_inplace */
1519 0, /* src_mask */
1520 0xfffc, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1523 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1524 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1525 to the first entry relative to the TOC base (r2). */
1526 HOWTO (R_PPC64_GOT_TLSGD16,
1527 0, /* rightshift */
1528 1, /* size (0 = byte, 1 = short, 2 = long) */
1529 16, /* bitsize */
1530 FALSE, /* pc_relative */
1531 0, /* bitpos */
1532 complain_overflow_signed, /* complain_on_overflow */
1533 ppc64_elf_unhandled_reloc, /* special_function */
1534 "R_PPC64_GOT_TLSGD16", /* name */
1535 FALSE, /* partial_inplace */
1536 0, /* src_mask */
1537 0xffff, /* dst_mask */
1538 FALSE), /* pcrel_offset */
1540 /* Like GOT_TLSGD16, but no overflow. */
1541 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1542 0, /* rightshift */
1543 1, /* size (0 = byte, 1 = short, 2 = long) */
1544 16, /* bitsize */
1545 FALSE, /* pc_relative */
1546 0, /* bitpos */
1547 complain_overflow_dont, /* complain_on_overflow */
1548 ppc64_elf_unhandled_reloc, /* special_function */
1549 "R_PPC64_GOT_TLSGD16_LO", /* name */
1550 FALSE, /* partial_inplace */
1551 0, /* src_mask */
1552 0xffff, /* dst_mask */
1553 FALSE), /* pcrel_offset */
1555 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1556 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1557 16, /* rightshift */
1558 1, /* size (0 = byte, 1 = short, 2 = long) */
1559 16, /* bitsize */
1560 FALSE, /* pc_relative */
1561 0, /* bitpos */
1562 complain_overflow_dont, /* complain_on_overflow */
1563 ppc64_elf_unhandled_reloc, /* special_function */
1564 "R_PPC64_GOT_TLSGD16_HI", /* name */
1565 FALSE, /* partial_inplace */
1566 0, /* src_mask */
1567 0xffff, /* dst_mask */
1568 FALSE), /* pcrel_offset */
1570 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1571 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1572 16, /* rightshift */
1573 1, /* size (0 = byte, 1 = short, 2 = long) */
1574 16, /* bitsize */
1575 FALSE, /* pc_relative */
1576 0, /* bitpos */
1577 complain_overflow_dont, /* complain_on_overflow */
1578 ppc64_elf_unhandled_reloc, /* special_function */
1579 "R_PPC64_GOT_TLSGD16_HA", /* name */
1580 FALSE, /* partial_inplace */
1581 0, /* src_mask */
1582 0xffff, /* dst_mask */
1583 FALSE), /* pcrel_offset */
1585 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1586 with values (sym+add)@dtpmod and zero, and computes the offset to the
1587 first entry relative to the TOC base (r2). */
1588 HOWTO (R_PPC64_GOT_TLSLD16,
1589 0, /* rightshift */
1590 1, /* size (0 = byte, 1 = short, 2 = long) */
1591 16, /* bitsize */
1592 FALSE, /* pc_relative */
1593 0, /* bitpos */
1594 complain_overflow_signed, /* complain_on_overflow */
1595 ppc64_elf_unhandled_reloc, /* special_function */
1596 "R_PPC64_GOT_TLSLD16", /* name */
1597 FALSE, /* partial_inplace */
1598 0, /* src_mask */
1599 0xffff, /* dst_mask */
1600 FALSE), /* pcrel_offset */
1602 /* Like GOT_TLSLD16, but no overflow. */
1603 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1604 0, /* rightshift */
1605 1, /* size (0 = byte, 1 = short, 2 = long) */
1606 16, /* bitsize */
1607 FALSE, /* pc_relative */
1608 0, /* bitpos */
1609 complain_overflow_dont, /* complain_on_overflow */
1610 ppc64_elf_unhandled_reloc, /* special_function */
1611 "R_PPC64_GOT_TLSLD16_LO", /* name */
1612 FALSE, /* partial_inplace */
1613 0, /* src_mask */
1614 0xffff, /* dst_mask */
1615 FALSE), /* pcrel_offset */
1617 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1618 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1619 16, /* rightshift */
1620 1, /* size (0 = byte, 1 = short, 2 = long) */
1621 16, /* bitsize */
1622 FALSE, /* pc_relative */
1623 0, /* bitpos */
1624 complain_overflow_dont, /* complain_on_overflow */
1625 ppc64_elf_unhandled_reloc, /* special_function */
1626 "R_PPC64_GOT_TLSLD16_HI", /* name */
1627 FALSE, /* partial_inplace */
1628 0, /* src_mask */
1629 0xffff, /* dst_mask */
1630 FALSE), /* pcrel_offset */
1632 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1633 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1634 16, /* rightshift */
1635 1, /* size (0 = byte, 1 = short, 2 = long) */
1636 16, /* bitsize */
1637 FALSE, /* pc_relative */
1638 0, /* bitpos */
1639 complain_overflow_dont, /* complain_on_overflow */
1640 ppc64_elf_unhandled_reloc, /* special_function */
1641 "R_PPC64_GOT_TLSLD16_HA", /* name */
1642 FALSE, /* partial_inplace */
1643 0, /* src_mask */
1644 0xffff, /* dst_mask */
1645 FALSE), /* pcrel_offset */
1647 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1648 the offset to the entry relative to the TOC base (r2). */
1649 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1650 0, /* rightshift */
1651 1, /* size (0 = byte, 1 = short, 2 = long) */
1652 16, /* bitsize */
1653 FALSE, /* pc_relative */
1654 0, /* bitpos */
1655 complain_overflow_signed, /* complain_on_overflow */
1656 ppc64_elf_unhandled_reloc, /* special_function */
1657 "R_PPC64_GOT_DTPREL16_DS", /* name */
1658 FALSE, /* partial_inplace */
1659 0, /* src_mask */
1660 0xfffc, /* dst_mask */
1661 FALSE), /* pcrel_offset */
1663 /* Like GOT_DTPREL16_DS, but no overflow. */
1664 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1665 0, /* rightshift */
1666 1, /* size (0 = byte, 1 = short, 2 = long) */
1667 16, /* bitsize */
1668 FALSE, /* pc_relative */
1669 0, /* bitpos */
1670 complain_overflow_dont, /* complain_on_overflow */
1671 ppc64_elf_unhandled_reloc, /* special_function */
1672 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1673 FALSE, /* partial_inplace */
1674 0, /* src_mask */
1675 0xfffc, /* dst_mask */
1676 FALSE), /* pcrel_offset */
1678 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1679 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1680 16, /* rightshift */
1681 1, /* size (0 = byte, 1 = short, 2 = long) */
1682 16, /* bitsize */
1683 FALSE, /* pc_relative */
1684 0, /* bitpos */
1685 complain_overflow_dont, /* complain_on_overflow */
1686 ppc64_elf_unhandled_reloc, /* special_function */
1687 "R_PPC64_GOT_DTPREL16_HI", /* name */
1688 FALSE, /* partial_inplace */
1689 0, /* src_mask */
1690 0xffff, /* dst_mask */
1691 FALSE), /* pcrel_offset */
1693 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1694 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1695 16, /* rightshift */
1696 1, /* size (0 = byte, 1 = short, 2 = long) */
1697 16, /* bitsize */
1698 FALSE, /* pc_relative */
1699 0, /* bitpos */
1700 complain_overflow_dont, /* complain_on_overflow */
1701 ppc64_elf_unhandled_reloc, /* special_function */
1702 "R_PPC64_GOT_DTPREL16_HA", /* name */
1703 FALSE, /* partial_inplace */
1704 0, /* src_mask */
1705 0xffff, /* dst_mask */
1706 FALSE), /* pcrel_offset */
1708 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1709 offset to the entry relative to the TOC base (r2). */
1710 HOWTO (R_PPC64_GOT_TPREL16_DS,
1711 0, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 16, /* bitsize */
1714 FALSE, /* pc_relative */
1715 0, /* bitpos */
1716 complain_overflow_signed, /* complain_on_overflow */
1717 ppc64_elf_unhandled_reloc, /* special_function */
1718 "R_PPC64_GOT_TPREL16_DS", /* name */
1719 FALSE, /* partial_inplace */
1720 0, /* src_mask */
1721 0xffff, /* dst_mask */
1722 FALSE), /* pcrel_offset */
1724 /* Like GOT_TPREL16_DS, but no overflow. */
1725 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1726 0, /* rightshift */
1727 1, /* size (0 = byte, 1 = short, 2 = long) */
1728 16, /* bitsize */
1729 FALSE, /* pc_relative */
1730 0, /* bitpos */
1731 complain_overflow_dont, /* complain_on_overflow */
1732 ppc64_elf_unhandled_reloc, /* special_function */
1733 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1734 FALSE, /* partial_inplace */
1735 0, /* src_mask */
1736 0xffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1739 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1740 HOWTO (R_PPC64_GOT_TPREL16_HI,
1741 16, /* rightshift */
1742 1, /* size (0 = byte, 1 = short, 2 = long) */
1743 16, /* bitsize */
1744 FALSE, /* pc_relative */
1745 0, /* bitpos */
1746 complain_overflow_dont, /* complain_on_overflow */
1747 ppc64_elf_unhandled_reloc, /* special_function */
1748 "R_PPC64_GOT_TPREL16_HI", /* name */
1749 FALSE, /* partial_inplace */
1750 0, /* src_mask */
1751 0xffff, /* dst_mask */
1752 FALSE), /* pcrel_offset */
1754 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1755 HOWTO (R_PPC64_GOT_TPREL16_HA,
1756 16, /* rightshift */
1757 1, /* size (0 = byte, 1 = short, 2 = long) */
1758 16, /* bitsize */
1759 FALSE, /* pc_relative */
1760 0, /* bitpos */
1761 complain_overflow_dont, /* complain_on_overflow */
1762 ppc64_elf_unhandled_reloc, /* special_function */
1763 "R_PPC64_GOT_TPREL16_HA", /* name */
1764 FALSE, /* partial_inplace */
1765 0, /* src_mask */
1766 0xffff, /* dst_mask */
1767 FALSE), /* pcrel_offset */
1769 /* GNU extension to record C++ vtable hierarchy. */
1770 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1771 0, /* rightshift */
1772 0, /* size (0 = byte, 1 = short, 2 = long) */
1773 0, /* bitsize */
1774 FALSE, /* pc_relative */
1775 0, /* bitpos */
1776 complain_overflow_dont, /* complain_on_overflow */
1777 NULL, /* special_function */
1778 "R_PPC64_GNU_VTINHERIT", /* name */
1779 FALSE, /* partial_inplace */
1780 0, /* src_mask */
1781 0, /* dst_mask */
1782 FALSE), /* pcrel_offset */
1784 /* GNU extension to record C++ vtable member usage. */
1785 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1786 0, /* rightshift */
1787 0, /* size (0 = byte, 1 = short, 2 = long) */
1788 0, /* bitsize */
1789 FALSE, /* pc_relative */
1790 0, /* bitpos */
1791 complain_overflow_dont, /* complain_on_overflow */
1792 NULL, /* special_function */
1793 "R_PPC64_GNU_VTENTRY", /* name */
1794 FALSE, /* partial_inplace */
1795 0, /* src_mask */
1796 0, /* dst_mask */
1797 FALSE), /* pcrel_offset */
1801 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1802 be done. */
1804 static void
1805 ppc_howto_init ()
1807 unsigned int i, type;
1809 for (i = 0;
1810 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1811 i++)
1813 type = ppc64_elf_howto_raw[i].type;
1814 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1815 / sizeof (ppc64_elf_howto_table[0])));
1816 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1820 static reloc_howto_type *
1821 ppc64_elf_reloc_type_lookup (abfd, code)
1822 bfd *abfd ATTRIBUTE_UNUSED;
1823 bfd_reloc_code_real_type code;
1825 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1827 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1828 /* Initialize howto table if needed. */
1829 ppc_howto_init ();
1831 switch ((int) code)
1833 default:
1834 return (reloc_howto_type *) NULL;
1836 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1837 break;
1838 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1839 break;
1840 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1841 break;
1842 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1843 break;
1844 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1845 break;
1846 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1847 break;
1848 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1849 break;
1850 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1851 break;
1852 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1853 break;
1854 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1855 break;
1856 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1857 break;
1858 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1859 break;
1860 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1861 break;
1862 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1863 break;
1864 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1865 break;
1866 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1867 break;
1868 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1869 break;
1870 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1871 break;
1872 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1873 break;
1874 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1875 break;
1876 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1877 break;
1878 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1879 break;
1880 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1881 break;
1882 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1883 break;
1884 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1885 break;
1886 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1887 break;
1888 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1889 break;
1890 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1891 break;
1892 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1893 break;
1894 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1895 break;
1896 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1897 break;
1898 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1899 break;
1900 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1901 break;
1902 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1903 break;
1904 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1905 break;
1906 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1907 break;
1908 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1909 break;
1910 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1911 break;
1912 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1913 break;
1914 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1915 break;
1916 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1917 break;
1918 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1919 break;
1920 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1921 break;
1922 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1923 break;
1924 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1925 break;
1926 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1927 break;
1928 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1929 break;
1930 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1931 break;
1932 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1933 break;
1934 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1935 break;
1936 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1937 break;
1938 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1939 break;
1940 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1941 break;
1942 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1943 break;
1944 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1945 break;
1946 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1947 break;
1948 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1949 break;
1950 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1951 break;
1952 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1953 break;
1954 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1955 break;
1956 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1957 break;
1958 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1959 break;
1960 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1961 break;
1962 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1963 break;
1964 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1965 break;
1966 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1967 break;
1968 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1969 break;
1970 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1971 break;
1972 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1973 break;
1974 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1975 break;
1976 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1977 break;
1978 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1979 break;
1980 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1981 break;
1982 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1983 break;
1984 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1985 break;
1986 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1987 break;
1988 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1989 break;
1990 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1991 break;
1992 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1993 break;
1994 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1995 break;
1996 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1997 break;
1998 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1999 break;
2000 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2001 break;
2002 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2003 break;
2004 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2005 break;
2006 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2007 break;
2008 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2009 break;
2010 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2011 break;
2012 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2013 break;
2014 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2015 break;
2016 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2017 break;
2018 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2019 break;
2020 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2021 break;
2022 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2023 break;
2024 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2025 break;
2026 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2027 break;
2028 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2029 break;
2030 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2031 break;
2032 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2033 break;
2034 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2035 break;
2036 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2037 break;
2040 return ppc64_elf_howto_table[(int) r];
2043 /* Set the howto pointer for a PowerPC ELF reloc. */
2045 static void
2046 ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
2047 bfd *abfd ATTRIBUTE_UNUSED;
2048 arelent *cache_ptr;
2049 Elf_Internal_Rela *dst;
2051 unsigned int type;
2053 /* Initialize howto table if needed. */
2054 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2055 ppc_howto_init ();
2057 type = ELF64_R_TYPE (dst->r_info);
2058 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2059 / sizeof (ppc64_elf_howto_table[0])));
2060 cache_ptr->howto = ppc64_elf_howto_table[type];
2063 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2065 static bfd_reloc_status_type
2066 ppc64_elf_ha_reloc (abfd, reloc_entry, symbol, data,
2067 input_section, output_bfd, error_message)
2068 bfd *abfd;
2069 arelent *reloc_entry;
2070 asymbol *symbol;
2071 PTR data;
2072 asection *input_section;
2073 bfd *output_bfd;
2074 char **error_message;
2076 /* If this is a relocatable link (output_bfd test tells us), just
2077 call the generic function. Any adjustment will be done at final
2078 link time. */
2079 if (output_bfd != NULL)
2080 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2081 input_section, output_bfd, error_message);
2083 /* Adjust the addend for sign extension of the low 16 bits.
2084 We won't actually be using the low 16 bits, so trashing them
2085 doesn't matter. */
2086 reloc_entry->addend += 0x8000;
2087 return bfd_reloc_continue;
2090 static bfd_reloc_status_type
2091 ppc64_elf_brtaken_reloc (abfd, reloc_entry, symbol, data,
2092 input_section, output_bfd, error_message)
2093 bfd *abfd;
2094 arelent *reloc_entry;
2095 asymbol *symbol;
2096 PTR data;
2097 asection *input_section;
2098 bfd *output_bfd;
2099 char **error_message;
2101 long insn;
2102 enum elf_ppc64_reloc_type r_type;
2103 bfd_size_type octets;
2104 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
2105 bfd_boolean is_power4 = FALSE;
2107 /* If this is a relocatable link (output_bfd test tells us), just
2108 call the generic function. Any adjustment will be done at final
2109 link time. */
2110 if (output_bfd != NULL)
2111 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2112 input_section, output_bfd, error_message);
2114 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2115 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2116 insn &= ~(0x01 << 21);
2117 r_type = (enum elf_ppc64_reloc_type) reloc_entry->howto->type;
2118 if (r_type == R_PPC64_ADDR14_BRTAKEN
2119 || r_type == R_PPC64_REL14_BRTAKEN)
2120 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2122 if (is_power4)
2124 /* Set 'a' bit. This is 0b00010 in BO field for branch
2125 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2126 for branch on CTR insns (BO == 1a00t or 1a01t). */
2127 if ((insn & (0x14 << 21)) == (0x04 << 21))
2128 insn |= 0x02 << 21;
2129 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2130 insn |= 0x08 << 21;
2131 else
2132 return bfd_reloc_continue;
2134 else
2136 bfd_vma target = 0;
2137 bfd_vma from;
2139 if (!bfd_is_com_section (symbol->section))
2140 target = symbol->value;
2141 target += symbol->section->output_section->vma;
2142 target += symbol->section->output_offset;
2143 target += reloc_entry->addend;
2145 from = (reloc_entry->address
2146 + input_section->output_offset
2147 + input_section->output_section->vma);
2149 /* Invert 'y' bit if not the default. */
2150 if ((bfd_signed_vma) (target - from) < 0)
2151 insn ^= 0x01 << 21;
2153 bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + octets);
2154 return bfd_reloc_continue;
2157 static bfd_reloc_status_type
2158 ppc64_elf_sectoff_reloc (abfd, reloc_entry, symbol, data,
2159 input_section, output_bfd, error_message)
2160 bfd *abfd;
2161 arelent *reloc_entry;
2162 asymbol *symbol;
2163 PTR data;
2164 asection *input_section;
2165 bfd *output_bfd;
2166 char **error_message;
2168 /* If this is a relocatable link (output_bfd test tells us), just
2169 call the generic function. Any adjustment will be done at final
2170 link time. */
2171 if (output_bfd != NULL)
2172 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2173 input_section, output_bfd, error_message);
2175 /* Subtract the symbol section base address. */
2176 reloc_entry->addend -= symbol->section->output_section->vma;
2177 return bfd_reloc_continue;
2180 static bfd_reloc_status_type
2181 ppc64_elf_sectoff_ha_reloc (abfd, reloc_entry, symbol, data,
2182 input_section, output_bfd, error_message)
2183 bfd *abfd;
2184 arelent *reloc_entry;
2185 asymbol *symbol;
2186 PTR data;
2187 asection *input_section;
2188 bfd *output_bfd;
2189 char **error_message;
2191 /* If this is a relocatable link (output_bfd test tells us), just
2192 call the generic function. Any adjustment will be done at final
2193 link time. */
2194 if (output_bfd != NULL)
2195 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2196 input_section, output_bfd, error_message);
2198 /* Subtract the symbol section base address. */
2199 reloc_entry->addend -= symbol->section->output_section->vma;
2201 /* Adjust the addend for sign extension of the low 16 bits. */
2202 reloc_entry->addend += 0x8000;
2203 return bfd_reloc_continue;
2206 static bfd_reloc_status_type
2207 ppc64_elf_toc_reloc (abfd, reloc_entry, symbol, data,
2208 input_section, output_bfd, error_message)
2209 bfd *abfd;
2210 arelent *reloc_entry;
2211 asymbol *symbol;
2212 PTR data;
2213 asection *input_section;
2214 bfd *output_bfd;
2215 char **error_message;
2217 bfd_vma TOCstart;
2219 /* If this is a relocatable link (output_bfd test tells us), just
2220 call the generic function. Any adjustment will be done at final
2221 link time. */
2222 if (output_bfd != NULL)
2223 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2224 input_section, output_bfd, error_message);
2226 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2227 if (TOCstart == 0)
2228 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2230 /* Subtract the TOC base address. */
2231 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2232 return bfd_reloc_continue;
2235 static bfd_reloc_status_type
2236 ppc64_elf_toc_ha_reloc (abfd, reloc_entry, symbol, data,
2237 input_section, output_bfd, error_message)
2238 bfd *abfd;
2239 arelent *reloc_entry;
2240 asymbol *symbol;
2241 PTR data;
2242 asection *input_section;
2243 bfd *output_bfd;
2244 char **error_message;
2246 bfd_vma TOCstart;
2248 /* If this is a relocatable link (output_bfd test tells us), just
2249 call the generic function. Any adjustment will be done at final
2250 link time. */
2251 if (output_bfd != NULL)
2252 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2253 input_section, output_bfd, error_message);
2255 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2256 if (TOCstart == 0)
2257 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2259 /* Subtract the TOC base address. */
2260 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2262 /* Adjust the addend for sign extension of the low 16 bits. */
2263 reloc_entry->addend += 0x8000;
2264 return bfd_reloc_continue;
2267 static bfd_reloc_status_type
2268 ppc64_elf_toc64_reloc (abfd, reloc_entry, symbol, data,
2269 input_section, output_bfd, error_message)
2270 bfd *abfd;
2271 arelent *reloc_entry;
2272 asymbol *symbol;
2273 PTR data;
2274 asection *input_section;
2275 bfd *output_bfd;
2276 char **error_message;
2278 bfd_vma TOCstart;
2279 bfd_size_type octets;
2281 /* If this is a relocatable link (output_bfd test tells us), just
2282 call the generic function. Any adjustment will be done at final
2283 link time. */
2284 if (output_bfd != NULL)
2285 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2286 input_section, output_bfd, error_message);
2288 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2289 if (TOCstart == 0)
2290 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2292 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2293 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2294 return bfd_reloc_ok;
2297 static bfd_reloc_status_type
2298 ppc64_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
2299 input_section, output_bfd, error_message)
2300 bfd *abfd;
2301 arelent *reloc_entry;
2302 asymbol *symbol;
2303 PTR data;
2304 asection *input_section;
2305 bfd *output_bfd;
2306 char **error_message;
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);
2315 if (error_message != NULL)
2317 static char buf[60];
2318 sprintf (buf, "generic linker can't handle %s",
2319 reloc_entry->howto->name);
2320 *error_message = buf;
2322 return bfd_reloc_dangerous;
2325 /* Fix bad default arch selected for a 64 bit input bfd when the
2326 default is 32 bit. */
2328 static bfd_boolean
2329 ppc64_elf_object_p (abfd)
2330 bfd *abfd;
2332 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2334 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2336 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2338 /* Relies on arch after 32 bit default being 64 bit default. */
2339 abfd->arch_info = abfd->arch_info->next;
2340 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2343 return TRUE;
2346 /* Merge backend specific data from an object file to the output
2347 object file when linking. */
2349 static bfd_boolean
2350 ppc64_elf_merge_private_bfd_data (ibfd, obfd)
2351 bfd *ibfd;
2352 bfd *obfd;
2354 /* Check if we have the same endianess. */
2355 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
2356 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
2357 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2359 const char *msg;
2361 if (bfd_big_endian (ibfd))
2362 msg = _("%s: compiled for a big endian system and target is little endian");
2363 else
2364 msg = _("%s: compiled for a little endian system and target is big endian");
2366 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
2368 bfd_set_error (bfd_error_wrong_format);
2369 return FALSE;
2372 return TRUE;
2375 struct _ppc64_elf_section_data
2377 struct bfd_elf_section_data elf;
2379 /* An array with one entry for each opd function descriptor. */
2380 union
2382 /* Points to the function code section for local opd entries. */
2383 asection **func_sec;
2384 /* After editing .opd, adjust references to opd local syms. */
2385 long *adjust;
2386 } opd;
2388 /* An array for toc sections, indexed by offset/8.
2389 Specifies the relocation symbol index used at a given toc offset. */
2390 unsigned *t_symndx;
2393 #define ppc64_elf_section_data(sec) \
2394 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2396 static bfd_boolean
2397 ppc64_elf_new_section_hook (abfd, sec)
2398 bfd *abfd;
2399 asection *sec;
2401 struct _ppc64_elf_section_data *sdata;
2402 bfd_size_type amt = sizeof (*sdata);
2404 sdata = (struct _ppc64_elf_section_data *) bfd_zalloc (abfd, amt);
2405 if (sdata == NULL)
2406 return FALSE;
2407 sec->used_by_bfd = (PTR) sdata;
2409 return _bfd_elf_new_section_hook (abfd, sec);
2412 /* The following functions are specific to the ELF linker, while
2413 functions above are used generally. Those named ppc64_elf_* are
2414 called by the main ELF linker code. They appear in this file more
2415 or less in the order in which they are called. eg.
2416 ppc64_elf_check_relocs is called early in the link process,
2417 ppc64_elf_finish_dynamic_sections is one of the last functions
2418 called.
2420 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2421 functions have both a function code symbol and a function descriptor
2422 symbol. A call to foo in a relocatable object file looks like:
2424 . .text
2425 . x:
2426 . bl .foo
2427 . nop
2429 The function definition in another object file might be:
2431 . .section .opd
2432 . foo: .quad .foo
2433 . .quad .TOC.@tocbase
2434 . .quad 0
2436 . .text
2437 . .foo: blr
2439 When the linker resolves the call during a static link, the branch
2440 unsurprisingly just goes to .foo and the .opd information is unused.
2441 If the function definition is in a shared library, things are a little
2442 different: The call goes via a plt call stub, the opd information gets
2443 copied to the plt, and the linker patches the nop.
2445 . x:
2446 . bl .foo_stub
2447 . ld 2,40(1)
2450 . .foo_stub:
2451 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
2452 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
2453 . std 2,40(1) # this is the general idea
2454 . ld 11,0(12)
2455 . ld 2,8(12)
2456 . mtctr 11
2457 . ld 11,16(12)
2458 . bctr
2460 . .section .plt
2461 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2463 The "reloc ()" notation is supposed to indicate that the linker emits
2464 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2465 copying.
2467 What are the difficulties here? Well, firstly, the relocations
2468 examined by the linker in check_relocs are against the function code
2469 sym .foo, while the dynamic relocation in the plt is emitted against
2470 the function descriptor symbol, foo. Somewhere along the line, we need
2471 to carefully copy dynamic link information from one symbol to the other.
2472 Secondly, the generic part of the elf linker will make .foo a dynamic
2473 symbol as is normal for most other backends. We need foo dynamic
2474 instead, at least for an application final link. However, when
2475 creating a shared library containing foo, we need to have both symbols
2476 dynamic so that references to .foo are satisfied during the early
2477 stages of linking. Otherwise the linker might decide to pull in a
2478 definition from some other object, eg. a static library. */
2480 /* The linker needs to keep track of the number of relocs that it
2481 decides to copy as dynamic relocs in check_relocs for each symbol.
2482 This is so that it can later discard them if they are found to be
2483 unnecessary. We store the information in a field extending the
2484 regular ELF linker hash table. */
2486 struct ppc_dyn_relocs
2488 struct ppc_dyn_relocs *next;
2490 /* The input section of the reloc. */
2491 asection *sec;
2493 /* Total number of relocs copied for the input section. */
2494 bfd_size_type count;
2496 /* Number of pc-relative relocs copied for the input section. */
2497 bfd_size_type pc_count;
2500 /* Track GOT entries needed for a given symbol. We might need more
2501 than one got entry per symbol. */
2502 struct got_entry
2504 struct got_entry *next;
2506 bfd_vma addend;
2508 union
2510 bfd_signed_vma refcount;
2511 bfd_vma offset;
2512 } got;
2514 char tls_type;
2517 /* The same for PLT. */
2518 struct plt_entry
2520 struct plt_entry *next;
2522 bfd_vma addend;
2524 union
2526 bfd_signed_vma refcount;
2527 bfd_vma offset;
2528 } plt;
2531 /* Of those relocs that might be copied as dynamic relocs, this macro
2532 selects those that must be copied when linking a shared library. */
2534 #define MUST_BE_DYN_RELOC(RTYPE) \
2535 ((RTYPE) != R_PPC64_REL32 \
2536 && (RTYPE) != R_PPC64_REL64 \
2537 && (RTYPE) != R_PPC64_REL30)
2539 /* Section name for stubs is the associated section name plus this
2540 string. */
2541 #define STUB_SUFFIX ".stub"
2543 /* Linker stubs.
2544 ppc_stub_long_branch:
2545 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2546 destination, but a 24 bit branch in a stub section will reach.
2547 . b dest
2549 ppc_stub_plt_branch:
2550 Similar to the above, but a 24 bit branch in the stub section won't
2551 reach its destination.
2552 . addis %r12,%r2,xxx@toc@ha
2553 . ld %r11,xxx@toc@l(%r12)
2554 . mtctr %r11
2555 . bctr
2557 ppc_stub_plt_call:
2558 Used to call a function in a shared library.
2559 . addis %r12,%r2,xxx@toc@ha
2560 . std %r2,40(%r1)
2561 . ld %r11,xxx+0@toc@l(%r12)
2562 . ld %r2,xxx+8@toc@l(%r12)
2563 . mtctr %r11
2564 . ld %r11,xxx+16@toc@l(%r12)
2565 . bctr
2568 enum ppc_stub_type {
2569 ppc_stub_none,
2570 ppc_stub_long_branch,
2571 ppc_stub_plt_branch,
2572 ppc_stub_plt_call
2575 struct ppc_stub_hash_entry {
2577 /* Base hash table entry structure. */
2578 struct bfd_hash_entry root;
2580 /* The stub section. */
2581 asection *stub_sec;
2583 /* Offset within stub_sec of the beginning of this stub. */
2584 bfd_vma stub_offset;
2586 /* Given the symbol's value and its section we can determine its final
2587 value when building the stubs (so the stub knows where to jump. */
2588 bfd_vma target_value;
2589 asection *target_section;
2591 enum ppc_stub_type stub_type;
2593 /* The symbol table entry, if any, that this was derived from. */
2594 struct ppc_link_hash_entry *h;
2596 /* And the reloc addend that this was derived from. */
2597 bfd_vma addend;
2599 /* Where this stub is being called from, or, in the case of combined
2600 stub sections, the first input section in the group. */
2601 asection *id_sec;
2604 struct ppc_branch_hash_entry {
2606 /* Base hash table entry structure. */
2607 struct bfd_hash_entry root;
2609 /* Offset within .branch_lt. */
2610 unsigned int offset;
2612 /* Generation marker. */
2613 unsigned int iter;
2616 struct ppc_link_hash_entry
2618 struct elf_link_hash_entry elf;
2620 /* A pointer to the most recently used stub hash entry against this
2621 symbol. */
2622 struct ppc_stub_hash_entry *stub_cache;
2624 /* Track dynamic relocs copied for this symbol. */
2625 struct ppc_dyn_relocs *dyn_relocs;
2627 /* Link between function code and descriptor symbols. */
2628 struct elf_link_hash_entry *oh;
2630 /* Flag function code and descriptor symbols. */
2631 unsigned int is_func:1;
2632 unsigned int is_func_descriptor:1;
2633 unsigned int is_entry:1;
2635 /* Contexts in which symbol is used in the GOT (or TOC).
2636 Linker optimization will result in various transformations. */
2637 #define TLS_TLS 1 /* Any TLS reloc. */
2638 #define TLS_GD 2 /* GD reloc. */
2639 #define TLS_LD 4 /* LD reloc. */
2640 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
2641 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
2642 #define TLS_EXPLICIT 32 /* Marks TOC section relocs. */
2643 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
2644 char tls_type;
2647 /* ppc64 ELF linker hash table. */
2649 struct ppc_link_hash_table
2651 struct elf_link_hash_table elf;
2653 /* The stub hash table. */
2654 struct bfd_hash_table stub_hash_table;
2656 /* Another hash table for plt_branch stubs. */
2657 struct bfd_hash_table branch_hash_table;
2659 /* Linker stub bfd. */
2660 bfd *stub_bfd;
2662 /* Linker call-backs. */
2663 asection * (*add_stub_section) PARAMS ((const char *, asection *));
2664 void (*layout_sections_again) PARAMS ((void));
2666 /* Array to keep track of which stub sections have been created, and
2667 information on stub grouping. */
2668 struct map_stub {
2669 /* This is the section to which stubs in the group will be attached. */
2670 asection *link_sec;
2671 /* The stub section. */
2672 asection *stub_sec;
2673 } *stub_group;
2675 /* Assorted information used by ppc64_elf_size_stubs. */
2676 int top_index;
2677 asection **input_list;
2679 /* Short-cuts to get to dynamic linker sections. */
2680 asection *sgot;
2681 asection *srelgot;
2682 asection *splt;
2683 asection *srelplt;
2684 asection *sdynbss;
2685 asection *srelbss;
2686 asection *sglink;
2687 asection *sfpr;
2688 asection *sbrlt;
2689 asection *srelbrlt;
2691 /* Short-cut to first output tls section. */
2692 asection *tls_sec;
2694 /* Shortcut to .__tls_get_addr. */
2695 struct elf_link_hash_entry *tls_get_addr;
2697 /* TLS local dynamic got entry handling. */
2698 union {
2699 bfd_signed_vma refcount;
2700 bfd_vma offset;
2701 } tlsld_got;
2703 /* Set on error. */
2704 unsigned int stub_error;
2706 /* Flag set when small branches are detected. Used to
2707 select suitable defaults for the stub group size. */
2708 unsigned int has_14bit_branch;
2710 /* Set if we detect a reference undefined weak symbol. */
2711 unsigned int have_undefweak;
2713 /* Incremented every time we size stubs. */
2714 unsigned int stub_iteration;
2716 /* Small local sym to section mapping cache. */
2717 struct sym_sec_cache sym_sec;
2720 static struct bfd_hash_entry *stub_hash_newfunc
2721 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2722 static struct bfd_hash_entry *branch_hash_newfunc
2723 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2724 static struct bfd_hash_entry *link_hash_newfunc
2725 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
2726 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
2727 PARAMS ((bfd *));
2728 static void ppc64_elf_link_hash_table_free
2729 PARAMS ((struct bfd_link_hash_table *));
2730 static char *ppc_stub_name
2731 PARAMS ((const asection *, const asection *,
2732 const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
2733 static struct ppc_stub_hash_entry *ppc_get_stub_entry
2734 PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
2735 const Elf_Internal_Rela *, struct ppc_link_hash_table *));
2736 static struct ppc_stub_hash_entry *ppc_add_stub
2737 PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
2738 static bfd_boolean create_linkage_sections
2739 PARAMS ((bfd *, struct bfd_link_info *));
2740 static bfd_boolean create_got_section
2741 PARAMS ((bfd *, struct bfd_link_info *));
2742 static bfd_boolean ppc64_elf_create_dynamic_sections
2743 PARAMS ((bfd *, struct bfd_link_info *));
2744 static void ppc64_elf_copy_indirect_symbol
2745 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
2746 struct elf_link_hash_entry *));
2747 static bfd_boolean update_local_sym_info
2748 PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, bfd_vma, int));
2749 static bfd_boolean update_plt_info
2750 PARAMS ((bfd *, struct ppc_link_hash_entry *, bfd_vma));
2751 static bfd_boolean ppc64_elf_check_relocs
2752 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2753 const Elf_Internal_Rela *));
2754 static asection * ppc64_elf_gc_mark_hook
2755 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
2756 struct elf_link_hash_entry *, Elf_Internal_Sym *));
2757 static bfd_boolean ppc64_elf_gc_sweep_hook
2758 PARAMS ((bfd *, struct bfd_link_info *, asection *,
2759 const Elf_Internal_Rela *));
2760 static bfd_boolean func_desc_adjust
2761 PARAMS ((struct elf_link_hash_entry *, PTR));
2762 static bfd_boolean ppc64_elf_func_desc_adjust
2763 PARAMS ((bfd *, struct bfd_link_info *));
2764 static bfd_boolean ppc64_elf_adjust_dynamic_symbol
2765 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2766 static void ppc64_elf_hide_symbol
2767 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
2768 static bfd_boolean get_sym_h
2769 PARAMS ((struct elf_link_hash_entry **, Elf_Internal_Sym **, asection **,
2770 char **, Elf_Internal_Sym **, unsigned long, bfd *));
2771 static int get_tls_type
2772 PARAMS ((char **, Elf_Internal_Sym **, const Elf_Internal_Rela *, bfd *));
2773 static bfd_boolean allocate_dynrelocs
2774 PARAMS ((struct elf_link_hash_entry *, PTR));
2775 static bfd_boolean readonly_dynrelocs
2776 PARAMS ((struct elf_link_hash_entry *, PTR));
2777 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2778 PARAMS ((const Elf_Internal_Rela *));
2779 static bfd_boolean ppc64_elf_size_dynamic_sections
2780 PARAMS ((bfd *, struct bfd_link_info *));
2781 static enum ppc_stub_type ppc_type_of_stub
2782 PARAMS ((asection *, const Elf_Internal_Rela *,
2783 struct ppc_link_hash_entry **, bfd_vma));
2784 static bfd_byte *build_plt_stub
2785 PARAMS ((bfd *, bfd_byte *, int, int));
2786 static bfd_boolean ppc_build_one_stub
2787 PARAMS ((struct bfd_hash_entry *, PTR));
2788 static bfd_boolean ppc_size_one_stub
2789 PARAMS ((struct bfd_hash_entry *, PTR));
2790 static void group_sections
2791 PARAMS ((struct ppc_link_hash_table *, bfd_size_type, bfd_boolean));
2792 static bfd_boolean ppc64_elf_relocate_section
2793 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2794 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2795 asection **));
2796 static bfd_boolean ppc64_elf_finish_dynamic_symbol
2797 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2798 Elf_Internal_Sym *));
2799 static bfd_boolean ppc64_elf_finish_dynamic_sections
2800 PARAMS ((bfd *, struct bfd_link_info *));
2802 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2804 #define ppc_hash_table(p) \
2805 ((struct ppc_link_hash_table *) ((p)->hash))
2807 #define ppc_stub_hash_lookup(table, string, create, copy) \
2808 ((struct ppc_stub_hash_entry *) \
2809 bfd_hash_lookup ((table), (string), (create), (copy)))
2811 #define ppc_branch_hash_lookup(table, string, create, copy) \
2812 ((struct ppc_branch_hash_entry *) \
2813 bfd_hash_lookup ((table), (string), (create), (copy)))
2815 /* Create an entry in the stub hash table. */
2817 static struct bfd_hash_entry *
2818 stub_hash_newfunc (entry, table, string)
2819 struct bfd_hash_entry *entry;
2820 struct bfd_hash_table *table;
2821 const char *string;
2823 /* Allocate the structure if it has not already been allocated by a
2824 subclass. */
2825 if (entry == NULL)
2827 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2828 if (entry == NULL)
2829 return entry;
2832 /* Call the allocation method of the superclass. */
2833 entry = bfd_hash_newfunc (entry, table, string);
2834 if (entry != NULL)
2836 struct ppc_stub_hash_entry *eh;
2838 /* Initialize the local fields. */
2839 eh = (struct ppc_stub_hash_entry *) entry;
2840 eh->stub_sec = NULL;
2841 eh->stub_offset = 0;
2842 eh->target_value = 0;
2843 eh->target_section = NULL;
2844 eh->stub_type = ppc_stub_none;
2845 eh->h = NULL;
2846 eh->id_sec = NULL;
2849 return entry;
2852 /* Create an entry in the branch hash table. */
2854 static struct bfd_hash_entry *
2855 branch_hash_newfunc (entry, table, string)
2856 struct bfd_hash_entry *entry;
2857 struct bfd_hash_table *table;
2858 const char *string;
2860 /* Allocate the structure if it has not already been allocated by a
2861 subclass. */
2862 if (entry == NULL)
2864 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2865 if (entry == NULL)
2866 return entry;
2869 /* Call the allocation method of the superclass. */
2870 entry = bfd_hash_newfunc (entry, table, string);
2871 if (entry != NULL)
2873 struct ppc_branch_hash_entry *eh;
2875 /* Initialize the local fields. */
2876 eh = (struct ppc_branch_hash_entry *) entry;
2877 eh->offset = 0;
2878 eh->iter = 0;
2881 return entry;
2884 /* Create an entry in a ppc64 ELF linker hash table. */
2886 static struct bfd_hash_entry *
2887 link_hash_newfunc (entry, table, string)
2888 struct bfd_hash_entry *entry;
2889 struct bfd_hash_table *table;
2890 const char *string;
2892 /* Allocate the structure if it has not already been allocated by a
2893 subclass. */
2894 if (entry == NULL)
2896 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2897 if (entry == NULL)
2898 return entry;
2901 /* Call the allocation method of the superclass. */
2902 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2903 if (entry != NULL)
2905 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2907 eh->stub_cache = NULL;
2908 eh->dyn_relocs = NULL;
2909 eh->oh = NULL;
2910 eh->is_func = 0;
2911 eh->is_func_descriptor = 0;
2912 eh->is_entry = 0;
2913 eh->tls_type = 0;
2916 return entry;
2919 /* Create a ppc64 ELF linker hash table. */
2921 static struct bfd_link_hash_table *
2922 ppc64_elf_link_hash_table_create (abfd)
2923 bfd *abfd;
2925 struct ppc_link_hash_table *htab;
2926 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2928 htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
2929 if (htab == NULL)
2930 return NULL;
2932 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2934 free (htab);
2935 return NULL;
2938 /* Init the stub hash table too. */
2939 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2940 return NULL;
2942 /* And the branch hash table. */
2943 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2944 return NULL;
2946 htab->stub_bfd = NULL;
2947 htab->add_stub_section = NULL;
2948 htab->layout_sections_again = NULL;
2949 htab->stub_group = NULL;
2950 htab->sgot = NULL;
2951 htab->srelgot = NULL;
2952 htab->splt = NULL;
2953 htab->srelplt = NULL;
2954 htab->sdynbss = NULL;
2955 htab->srelbss = NULL;
2956 htab->sglink = NULL;
2957 htab->sfpr = NULL;
2958 htab->sbrlt = NULL;
2959 htab->srelbrlt = NULL;
2960 htab->tls_sec = NULL;
2961 htab->tlsld_got.refcount = 0;
2962 htab->stub_error = 0;
2963 htab->has_14bit_branch = 0;
2964 htab->have_undefweak = 0;
2965 htab->stub_iteration = 0;
2966 htab->sym_sec.abfd = NULL;
2967 /* Initializing two fields of the union is just cosmetic. We really
2968 only care about glist, but when compiled on a 32-bit host the
2969 bfd_vma fields are larger. Setting the bfd_vma to zero makes
2970 debugger inspection of these fields look nicer. */
2971 htab->elf.init_refcount.refcount = 0;
2972 htab->elf.init_refcount.glist = NULL;
2973 htab->elf.init_offset.offset = 0;
2974 htab->elf.init_offset.glist = NULL;
2976 return &htab->elf.root;
2979 /* Free the derived linker hash table. */
2981 static void
2982 ppc64_elf_link_hash_table_free (hash)
2983 struct bfd_link_hash_table *hash;
2985 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2987 bfd_hash_table_free (&ret->stub_hash_table);
2988 bfd_hash_table_free (&ret->branch_hash_table);
2989 _bfd_generic_link_hash_table_free (hash);
2992 /* Build a name for an entry in the stub hash table. */
2994 static char *
2995 ppc_stub_name (input_section, sym_sec, h, rel)
2996 const asection *input_section;
2997 const asection *sym_sec;
2998 const struct ppc_link_hash_entry *h;
2999 const Elf_Internal_Rela *rel;
3001 char *stub_name;
3002 bfd_size_type len;
3004 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3005 offsets from a sym as a branch target? In fact, we could
3006 probably assume the addend is always zero. */
3007 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3009 if (h)
3011 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3012 stub_name = bfd_malloc (len);
3013 if (stub_name != NULL)
3015 sprintf (stub_name, "%08x_%s+%x",
3016 input_section->id & 0xffffffff,
3017 h->elf.root.root.string,
3018 (int) rel->r_addend & 0xffffffff);
3021 else
3023 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
3024 stub_name = bfd_malloc (len);
3025 if (stub_name != NULL)
3027 sprintf (stub_name, "%08x_%x:%x+%x",
3028 input_section->id & 0xffffffff,
3029 sym_sec->id & 0xffffffff,
3030 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3031 (int) rel->r_addend & 0xffffffff);
3034 return stub_name;
3037 /* Look up an entry in the stub hash. Stub entries are cached because
3038 creating the stub name takes a bit of time. */
3040 static struct ppc_stub_hash_entry *
3041 ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
3042 const asection *input_section;
3043 const asection *sym_sec;
3044 struct elf_link_hash_entry *hash;
3045 const Elf_Internal_Rela *rel;
3046 struct ppc_link_hash_table *htab;
3048 struct ppc_stub_hash_entry *stub_entry;
3049 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
3050 const asection *id_sec;
3052 /* If this input section is part of a group of sections sharing one
3053 stub section, then use the id of the first section in the group.
3054 Stub names need to include a section id, as there may well be
3055 more than one stub used to reach say, printf, and we need to
3056 distinguish between them. */
3057 id_sec = htab->stub_group[input_section->id].link_sec;
3059 if (h != NULL && h->stub_cache != NULL
3060 && h->stub_cache->h == h
3061 && h->stub_cache->id_sec == id_sec)
3063 stub_entry = h->stub_cache;
3065 else
3067 char *stub_name;
3069 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3070 if (stub_name == NULL)
3071 return NULL;
3073 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3074 stub_name, FALSE, FALSE);
3075 if (h != NULL)
3076 h->stub_cache = stub_entry;
3078 free (stub_name);
3081 return stub_entry;
3084 /* Add a new stub entry to the stub hash. Not all fields of the new
3085 stub entry are initialised. */
3087 static struct ppc_stub_hash_entry *
3088 ppc_add_stub (stub_name, section, htab)
3089 const char *stub_name;
3090 asection *section;
3091 struct ppc_link_hash_table *htab;
3093 asection *link_sec;
3094 asection *stub_sec;
3095 struct ppc_stub_hash_entry *stub_entry;
3097 link_sec = htab->stub_group[section->id].link_sec;
3098 stub_sec = htab->stub_group[section->id].stub_sec;
3099 if (stub_sec == NULL)
3101 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3102 if (stub_sec == NULL)
3104 size_t namelen;
3105 bfd_size_type len;
3106 char *s_name;
3108 namelen = strlen (link_sec->name);
3109 len = namelen + sizeof (STUB_SUFFIX);
3110 s_name = bfd_alloc (htab->stub_bfd, len);
3111 if (s_name == NULL)
3112 return NULL;
3114 memcpy (s_name, link_sec->name, namelen);
3115 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3116 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3117 if (stub_sec == NULL)
3118 return NULL;
3119 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3121 htab->stub_group[section->id].stub_sec = stub_sec;
3124 /* Enter this entry into the linker stub hash table. */
3125 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3126 TRUE, FALSE);
3127 if (stub_entry == NULL)
3129 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
3130 bfd_archive_filename (section->owner),
3131 stub_name);
3132 return NULL;
3135 stub_entry->stub_sec = stub_sec;
3136 stub_entry->stub_offset = 0;
3137 stub_entry->id_sec = link_sec;
3138 return stub_entry;
3141 /* Create sections for linker generated code. */
3143 static bfd_boolean
3144 create_linkage_sections (dynobj, info)
3145 bfd *dynobj;
3146 struct bfd_link_info *info;
3148 struct ppc_link_hash_table *htab;
3149 flagword flags;
3151 htab = ppc_hash_table (info);
3153 /* Create .sfpr for code to save and restore fp regs. */
3154 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3155 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3156 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
3157 if (htab->sfpr == NULL
3158 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
3159 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
3160 return FALSE;
3162 /* Create .glink for lazy dynamic linking support. */
3163 htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
3164 if (htab->sglink == NULL
3165 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
3166 || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
3167 return FALSE;
3169 /* Create .branch_lt for plt_branch stubs. */
3170 flags = (SEC_ALLOC | SEC_LOAD
3171 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3172 htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
3173 if (htab->sbrlt == NULL
3174 || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
3175 || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
3176 return FALSE;
3178 if (info->shared)
3180 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3181 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3182 htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
3183 if (!htab->srelbrlt
3184 || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
3185 || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
3186 return FALSE;
3188 return TRUE;
3191 /* Create .got and .rela.got sections in DYNOBJ, and set up
3192 shortcuts to them in our hash table. */
3194 static bfd_boolean
3195 create_got_section (dynobj, info)
3196 bfd *dynobj;
3197 struct bfd_link_info *info;
3199 struct ppc_link_hash_table *htab;
3201 if (! _bfd_elf_create_got_section (dynobj, info))
3202 return FALSE;
3204 htab = ppc_hash_table (info);
3205 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3206 if (!htab->sgot)
3207 abort ();
3209 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
3210 if (!htab->srelgot
3211 || ! bfd_set_section_flags (dynobj, htab->srelgot,
3212 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3213 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3214 | SEC_READONLY))
3215 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
3216 return FALSE;
3217 return TRUE;
3220 /* Create the dynamic sections, and set up shortcuts. */
3222 static bfd_boolean
3223 ppc64_elf_create_dynamic_sections (dynobj, info)
3224 bfd *dynobj;
3225 struct bfd_link_info *info;
3227 struct ppc_link_hash_table *htab;
3229 htab = ppc_hash_table (info);
3230 if (!htab->sgot && !create_got_section (dynobj, info))
3231 return FALSE;
3233 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3234 return FALSE;
3236 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
3237 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3238 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
3239 if (!info->shared)
3240 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
3242 if (!htab->splt || !htab->srelplt || !htab->sdynbss
3243 || (!info->shared && !htab->srelbss))
3244 abort ();
3246 return TRUE;
3249 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3251 static void
3252 ppc64_elf_copy_indirect_symbol (bed, dir, ind)
3253 struct elf_backend_data *bed ATTRIBUTE_UNUSED;
3254 struct elf_link_hash_entry *dir, *ind;
3256 struct ppc_link_hash_entry *edir, *eind;
3258 edir = (struct ppc_link_hash_entry *) dir;
3259 eind = (struct ppc_link_hash_entry *) ind;
3261 /* Copy over any dynamic relocs we may have on the indirect sym. */
3262 if (eind->dyn_relocs != NULL)
3264 if (edir->dyn_relocs != NULL)
3266 struct ppc_dyn_relocs **pp;
3267 struct ppc_dyn_relocs *p;
3269 if (eind->elf.root.type == bfd_link_hash_indirect)
3270 abort ();
3272 /* Add reloc counts against the weak sym to the strong sym
3273 list. Merge any entries against the same section. */
3274 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3276 struct ppc_dyn_relocs *q;
3278 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3279 if (q->sec == p->sec)
3281 q->pc_count += p->pc_count;
3282 q->count += p->count;
3283 *pp = p->next;
3284 break;
3286 if (q == NULL)
3287 pp = &p->next;
3289 *pp = edir->dyn_relocs;
3292 edir->dyn_relocs = eind->dyn_relocs;
3293 eind->dyn_relocs = NULL;
3296 edir->is_func |= eind->is_func;
3297 edir->is_func_descriptor |= eind->is_func_descriptor;
3298 edir->is_entry |= eind->is_entry;
3300 /* Copy down any references that we may have already seen to the
3301 symbol which just became indirect. */
3302 edir->elf.elf_link_hash_flags |=
3303 (eind->elf.elf_link_hash_flags
3304 & (ELF_LINK_HASH_REF_DYNAMIC
3305 | ELF_LINK_HASH_REF_REGULAR
3306 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3307 | ELF_LINK_NON_GOT_REF));
3309 /* If we were called to copy over info for a weak sym, that's all. */
3310 if (eind->elf.root.type != bfd_link_hash_indirect)
3311 return;
3313 /* Copy over got entries. */
3314 if (eind->elf.got.glist != NULL)
3316 if (edir->elf.got.glist != NULL)
3318 struct got_entry **entp;
3319 struct got_entry *ent;
3321 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3323 struct got_entry *dent;
3325 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3326 if (dent->addend == ent->addend
3327 && dent->tls_type == ent->tls_type)
3329 dent->got.refcount += ent->got.refcount;
3330 *entp = ent->next;
3331 break;
3333 if (dent == NULL)
3334 entp = &ent->next;
3336 *entp = edir->elf.got.glist;
3339 edir->elf.got.glist = eind->elf.got.glist;
3340 eind->elf.got.glist = NULL;
3343 /* And plt entries. */
3344 if (eind->elf.plt.plist != NULL)
3346 if (edir->elf.plt.plist != NULL)
3348 struct plt_entry **entp;
3349 struct plt_entry *ent;
3351 for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
3353 struct plt_entry *dent;
3355 for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
3356 if (dent->addend == ent->addend)
3358 dent->plt.refcount += ent->plt.refcount;
3359 *entp = ent->next;
3360 break;
3362 if (dent == NULL)
3363 entp = &ent->next;
3365 *entp = edir->elf.plt.plist;
3368 edir->elf.plt.plist = eind->elf.plt.plist;
3369 eind->elf.plt.plist = NULL;
3372 if (edir->elf.dynindx == -1)
3374 edir->elf.dynindx = eind->elf.dynindx;
3375 edir->elf.dynstr_index = eind->elf.dynstr_index;
3376 eind->elf.dynindx = -1;
3377 eind->elf.dynstr_index = 0;
3379 else
3380 BFD_ASSERT (eind->elf.dynindx == -1);
3383 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
3384 symbols undefined on the command-line. */
3386 bfd_boolean
3387 ppc64_elf_mark_entry_syms (info)
3388 struct bfd_link_info *info;
3390 struct ppc_link_hash_table *htab;
3391 struct bfd_sym_chain *sym;
3393 htab = ppc_hash_table (info);
3394 for (sym = info->gc_sym_list; sym; sym = sym->next)
3396 struct elf_link_hash_entry *h;
3398 h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
3399 if (h != NULL)
3400 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
3402 return TRUE;
3405 static bfd_boolean
3406 update_local_sym_info (abfd, symtab_hdr, r_symndx, r_addend, tls_type)
3407 bfd *abfd;
3408 Elf_Internal_Shdr *symtab_hdr;
3409 unsigned long r_symndx;
3410 bfd_vma r_addend;
3411 int tls_type;
3413 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
3414 char *local_got_tls_types;
3416 if (local_got_ents == NULL)
3418 bfd_size_type size = symtab_hdr->sh_info;
3420 size *= sizeof (*local_got_ents) + sizeof (char);
3421 local_got_ents = (struct got_entry **) bfd_zalloc (abfd, size);
3422 if (local_got_ents == NULL)
3423 return FALSE;
3424 elf_local_got_ents (abfd) = local_got_ents;
3427 if ((tls_type & TLS_EXPLICIT) == 0)
3429 struct got_entry *ent;
3431 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
3432 if (ent->addend == r_addend && ent->tls_type == tls_type)
3433 break;
3434 if (ent == NULL)
3436 bfd_size_type amt = sizeof (*ent);
3437 ent = (struct got_entry *) bfd_alloc (abfd, amt);
3438 if (ent == NULL)
3439 return FALSE;
3440 ent->next = local_got_ents[r_symndx];
3441 ent->addend = r_addend;
3442 ent->tls_type = tls_type;
3443 ent->got.refcount = 0;
3444 local_got_ents[r_symndx] = ent;
3446 ent->got.refcount += 1;
3449 local_got_tls_types = (char *) (local_got_ents + symtab_hdr->sh_info);
3450 local_got_tls_types[r_symndx] |= tls_type;
3451 return TRUE;
3454 static bfd_boolean
3455 update_plt_info (abfd, eh, addend)
3456 bfd *abfd;
3457 struct ppc_link_hash_entry *eh;
3458 bfd_vma addend;
3460 struct plt_entry *ent;
3462 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
3463 if (ent->addend == addend)
3464 break;
3465 if (ent == NULL)
3467 bfd_size_type amt = sizeof (*ent);
3468 ent = (struct plt_entry *) bfd_alloc (abfd, amt);
3469 if (ent == NULL)
3470 return FALSE;
3471 ent->next = eh->elf.plt.plist;
3472 ent->addend = addend;
3473 ent->plt.refcount = 0;
3474 eh->elf.plt.plist = ent;
3476 ent->plt.refcount += 1;
3477 eh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3478 eh->is_func = 1;
3479 return TRUE;
3482 /* Look through the relocs for a section during the first phase, and
3483 calculate needed space in the global offset table, procedure
3484 linkage table, and dynamic reloc sections. */
3486 static bfd_boolean
3487 ppc64_elf_check_relocs (abfd, info, sec, relocs)
3488 bfd *abfd;
3489 struct bfd_link_info *info;
3490 asection *sec;
3491 const Elf_Internal_Rela *relocs;
3493 struct ppc_link_hash_table *htab;
3494 Elf_Internal_Shdr *symtab_hdr;
3495 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
3496 const Elf_Internal_Rela *rel;
3497 const Elf_Internal_Rela *rel_end;
3498 asection *sreloc;
3499 asection **opd_sym_map;
3501 if (info->relocateable)
3502 return TRUE;
3504 htab = ppc_hash_table (info);
3505 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3507 sym_hashes = elf_sym_hashes (abfd);
3508 sym_hashes_end = (sym_hashes
3509 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
3510 - symtab_hdr->sh_info);
3512 sreloc = NULL;
3513 opd_sym_map = NULL;
3514 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
3516 /* Garbage collection needs some extra help with .opd sections.
3517 We don't want to necessarily keep everything referenced by
3518 relocs in .opd, as that would keep all functions. Instead,
3519 if we reference an .opd symbol (a function descriptor), we
3520 want to keep the function code symbol's section. This is
3521 easy for global symbols, but for local syms we need to keep
3522 information about the associated function section. Later, if
3523 edit_opd deletes entries, we'll use this array to adjust
3524 local syms in .opd. */
3525 union opd_info {
3526 asection *func_section;
3527 long entry_adjust;
3529 bfd_size_type amt;
3531 amt = sec->_raw_size * sizeof (union opd_info) / 24;
3532 opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
3533 if (opd_sym_map == NULL)
3534 return FALSE;
3535 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
3538 if (htab->elf.dynobj == NULL)
3539 htab->elf.dynobj = abfd;
3540 if (htab->sfpr == NULL
3541 && !create_linkage_sections (htab->elf.dynobj, info))
3542 return FALSE;
3544 rel_end = relocs + sec->reloc_count;
3545 for (rel = relocs; rel < rel_end; rel++)
3547 unsigned long r_symndx;
3548 struct elf_link_hash_entry *h;
3549 enum elf_ppc64_reloc_type r_type;
3550 int tls_type = 0;
3552 r_symndx = ELF64_R_SYM (rel->r_info);
3553 if (r_symndx < symtab_hdr->sh_info)
3554 h = NULL;
3555 else
3556 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3558 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
3559 switch (r_type)
3561 case R_PPC64_GOT_TLSLD16:
3562 case R_PPC64_GOT_TLSLD16_LO:
3563 case R_PPC64_GOT_TLSLD16_HI:
3564 case R_PPC64_GOT_TLSLD16_HA:
3565 htab->tlsld_got.refcount += 1;
3566 tls_type = TLS_TLS | TLS_LD;
3567 goto dogottls;
3569 case R_PPC64_GOT_TLSGD16:
3570 case R_PPC64_GOT_TLSGD16_LO:
3571 case R_PPC64_GOT_TLSGD16_HI:
3572 case R_PPC64_GOT_TLSGD16_HA:
3573 tls_type = TLS_TLS | TLS_GD;
3574 goto dogottls;
3576 case R_PPC64_GOT_TPREL16_DS:
3577 case R_PPC64_GOT_TPREL16_LO_DS:
3578 case R_PPC64_GOT_TPREL16_HI:
3579 case R_PPC64_GOT_TPREL16_HA:
3580 if (info->shared)
3581 info->flags |= DF_STATIC_TLS;
3582 tls_type = TLS_TLS | TLS_TPREL;
3583 goto dogottls;
3585 case R_PPC64_GOT_DTPREL16_DS:
3586 case R_PPC64_GOT_DTPREL16_LO_DS:
3587 case R_PPC64_GOT_DTPREL16_HI:
3588 case R_PPC64_GOT_DTPREL16_HA:
3589 tls_type = TLS_TLS | TLS_DTPREL;
3590 dogottls:
3591 sec->has_tls_reloc = 1;
3592 /* Fall thru */
3594 case R_PPC64_GOT16:
3595 case R_PPC64_GOT16_DS:
3596 case R_PPC64_GOT16_HA:
3597 case R_PPC64_GOT16_HI:
3598 case R_PPC64_GOT16_LO:
3599 case R_PPC64_GOT16_LO_DS:
3600 /* This symbol requires a global offset table entry. */
3601 if (htab->sgot == NULL
3602 && !create_got_section (htab->elf.dynobj, info))
3603 return FALSE;
3605 if (h != NULL)
3607 struct ppc_link_hash_entry *eh;
3608 struct got_entry *ent;
3610 eh = (struct ppc_link_hash_entry *) h;
3611 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
3612 if (ent->addend == rel->r_addend
3613 && ent->tls_type == tls_type)
3614 break;
3615 if (ent == NULL)
3617 bfd_size_type amt = sizeof (*ent);
3618 ent = (struct got_entry *) bfd_alloc (abfd, amt);
3619 if (ent == NULL)
3620 return FALSE;
3621 ent->next = eh->elf.got.glist;
3622 ent->addend = rel->r_addend;
3623 ent->tls_type = tls_type;
3624 ent->got.refcount = 0;
3625 eh->elf.got.glist = ent;
3627 ent->got.refcount += 1;
3628 eh->tls_type |= tls_type;
3630 else
3631 /* This is a global offset table entry for a local symbol. */
3632 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3633 rel->r_addend, tls_type))
3634 return FALSE;
3635 break;
3637 case R_PPC64_PLT16_HA:
3638 case R_PPC64_PLT16_HI:
3639 case R_PPC64_PLT16_LO:
3640 case R_PPC64_PLT32:
3641 case R_PPC64_PLT64:
3642 /* This symbol requires a procedure linkage table entry. We
3643 actually build the entry in adjust_dynamic_symbol,
3644 because this might be a case of linking PIC code without
3645 linking in any dynamic objects, in which case we don't
3646 need to generate a procedure linkage table after all. */
3647 if (h == NULL)
3649 /* It does not make sense to have a procedure linkage
3650 table entry for a local symbol. */
3651 bfd_set_error (bfd_error_bad_value);
3652 return FALSE;
3654 else
3655 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3656 rel->r_addend))
3657 return FALSE;
3658 break;
3660 /* The following relocations don't need to propagate the
3661 relocation if linking a shared object since they are
3662 section relative. */
3663 case R_PPC64_SECTOFF:
3664 case R_PPC64_SECTOFF_LO:
3665 case R_PPC64_SECTOFF_HI:
3666 case R_PPC64_SECTOFF_HA:
3667 case R_PPC64_SECTOFF_DS:
3668 case R_PPC64_SECTOFF_LO_DS:
3669 case R_PPC64_TOC16:
3670 case R_PPC64_TOC16_LO:
3671 case R_PPC64_TOC16_HI:
3672 case R_PPC64_TOC16_HA:
3673 case R_PPC64_TOC16_DS:
3674 case R_PPC64_TOC16_LO_DS:
3675 case R_PPC64_DTPREL16:
3676 case R_PPC64_DTPREL16_LO:
3677 case R_PPC64_DTPREL16_HI:
3678 case R_PPC64_DTPREL16_HA:
3679 case R_PPC64_DTPREL16_DS:
3680 case R_PPC64_DTPREL16_LO_DS:
3681 case R_PPC64_DTPREL16_HIGHER:
3682 case R_PPC64_DTPREL16_HIGHERA:
3683 case R_PPC64_DTPREL16_HIGHEST:
3684 case R_PPC64_DTPREL16_HIGHESTA:
3685 break;
3687 /* This relocation describes the C++ object vtable hierarchy.
3688 Reconstruct it for later use during GC. */
3689 case R_PPC64_GNU_VTINHERIT:
3690 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3691 return FALSE;
3692 break;
3694 /* This relocation describes which C++ vtable entries are actually
3695 used. Record for later use during GC. */
3696 case R_PPC64_GNU_VTENTRY:
3697 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3698 return FALSE;
3699 break;
3701 case R_PPC64_REL14:
3702 case R_PPC64_REL14_BRTAKEN:
3703 case R_PPC64_REL14_BRNTAKEN:
3704 htab->has_14bit_branch = 1;
3705 /* Fall through. */
3707 case R_PPC64_REL24:
3708 if (h != NULL
3709 && h->root.root.string[0] == '.'
3710 && h->root.root.string[1] != 0)
3712 /* We may need a .plt entry if the function this reloc
3713 refers to is in a shared lib. */
3714 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
3715 rel->r_addend))
3716 return FALSE;
3717 if (h == htab->tls_get_addr)
3718 sec->has_tls_reloc = 1;
3719 else if (strcmp (h->root.root.string, ".__tls_get_addr") == 0)
3721 htab->tls_get_addr = h;
3722 sec->has_tls_reloc = 1;
3725 break;
3727 case R_PPC64_TPREL64:
3728 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
3729 if (info->shared)
3730 info->flags |= DF_STATIC_TLS;
3731 goto dotlstoc;
3733 case R_PPC64_DTPMOD64:
3734 if (rel + 1 < rel_end
3735 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
3736 && rel[1].r_offset == rel->r_offset + 8)
3737 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
3738 else
3739 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
3740 goto dotlstoc;
3742 case R_PPC64_DTPREL64:
3743 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
3744 if (rel != relocs
3745 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
3746 && rel[-1].r_offset == rel->r_offset - 8)
3747 /* This is the second reloc of a dtpmod, dtprel pair.
3748 Don't mark with TLS_DTPREL. */
3749 goto dodyn;
3751 dotlstoc:
3752 sec->has_tls_reloc = 1;
3753 if (h != NULL)
3755 struct ppc_link_hash_entry *eh;
3756 eh = (struct ppc_link_hash_entry *) h;
3757 eh->tls_type |= tls_type;
3759 else
3760 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
3761 rel->r_addend, tls_type))
3762 return FALSE;
3764 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3766 /* One extra to simplify get_tls_type. */
3767 bfd_size_type amt = sec->_raw_size * sizeof (unsigned) / 8 + 1;
3768 ppc64_elf_section_data (sec)->t_symndx
3769 = (unsigned *) bfd_zalloc (abfd, amt);
3770 if (ppc64_elf_section_data (sec)->t_symndx == NULL)
3771 return FALSE;
3773 BFD_ASSERT (rel->r_offset % 8 == 0);
3774 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8] = r_symndx;
3776 /* Mark the second slot of a GD or LD entry.
3777 -1 to indicate GD and -2 to indicate LD. */
3778 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3779 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -1;
3780 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3781 ppc64_elf_section_data (sec)->t_symndx[rel->r_offset / 8 + 1] = -2;
3782 goto dodyn;
3784 case R_PPC64_TPREL16:
3785 case R_PPC64_TPREL16_LO:
3786 case R_PPC64_TPREL16_HI:
3787 case R_PPC64_TPREL16_HA:
3788 case R_PPC64_TPREL16_DS:
3789 case R_PPC64_TPREL16_LO_DS:
3790 case R_PPC64_TPREL16_HIGHER:
3791 case R_PPC64_TPREL16_HIGHERA:
3792 case R_PPC64_TPREL16_HIGHEST:
3793 case R_PPC64_TPREL16_HIGHESTA:
3794 if (info->shared)
3796 info->flags |= DF_STATIC_TLS;
3797 goto dodyn;
3799 break;
3801 case R_PPC64_ADDR64:
3802 if (opd_sym_map != NULL
3803 && h != NULL
3804 && h->root.root.string[0] == '.'
3805 && h->root.root.string[1] != 0)
3807 struct elf_link_hash_entry *fdh;
3809 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3810 FALSE, FALSE, FALSE);
3811 if (fdh != NULL)
3813 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3814 ((struct ppc_link_hash_entry *) fdh)->oh = h;
3815 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3816 ((struct ppc_link_hash_entry *) h)->oh = fdh;
3819 if (opd_sym_map != NULL
3820 && h == NULL
3821 && rel + 1 < rel_end
3822 && ((enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
3823 == R_PPC64_TOC))
3825 asection *s;
3827 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
3828 r_symndx);
3829 if (s == NULL)
3830 return FALSE;
3831 else if (s != sec)
3832 opd_sym_map[rel->r_offset / 24] = s;
3834 /* Fall through. */
3836 case R_PPC64_REL30:
3837 case R_PPC64_REL32:
3838 case R_PPC64_REL64:
3839 case R_PPC64_ADDR14:
3840 case R_PPC64_ADDR14_BRNTAKEN:
3841 case R_PPC64_ADDR14_BRTAKEN:
3842 case R_PPC64_ADDR16:
3843 case R_PPC64_ADDR16_DS:
3844 case R_PPC64_ADDR16_HA:
3845 case R_PPC64_ADDR16_HI:
3846 case R_PPC64_ADDR16_HIGHER:
3847 case R_PPC64_ADDR16_HIGHERA:
3848 case R_PPC64_ADDR16_HIGHEST:
3849 case R_PPC64_ADDR16_HIGHESTA:
3850 case R_PPC64_ADDR16_LO:
3851 case R_PPC64_ADDR16_LO_DS:
3852 case R_PPC64_ADDR24:
3853 case R_PPC64_ADDR32:
3854 case R_PPC64_UADDR16:
3855 case R_PPC64_UADDR32:
3856 case R_PPC64_UADDR64:
3857 case R_PPC64_TOC:
3858 /* Don't propagate .opd relocs. */
3859 if (NO_OPD_RELOCS && opd_sym_map != NULL)
3860 break;
3862 /* If we are creating a shared library, and this is a reloc
3863 against a global symbol, or a non PC relative reloc
3864 against a local symbol, then we need to copy the reloc
3865 into the shared library. However, if we are linking with
3866 -Bsymbolic, we do not need to copy a reloc against a
3867 global symbol which is defined in an object we are
3868 including in the link (i.e., DEF_REGULAR is set). At
3869 this point we have not seen all the input files, so it is
3870 possible that DEF_REGULAR is not set now but will be set
3871 later (it is never cleared). In case of a weak definition,
3872 DEF_REGULAR may be cleared later by a strong definition in
3873 a shared library. We account for that possibility below by
3874 storing information in the relocs_copied field of the hash
3875 table entry. A similar situation occurs when creating
3876 shared libraries and symbol visibility changes render the
3877 symbol local.
3879 If on the other hand, we are creating an executable, we
3880 may need to keep relocations for symbols satisfied by a
3881 dynamic library if we manage to avoid copy relocs for the
3882 symbol. */
3883 dodyn:
3884 if ((info->shared
3885 && (sec->flags & SEC_ALLOC) != 0
3886 && (MUST_BE_DYN_RELOC (r_type)
3887 || (h != NULL
3888 && (! info->symbolic
3889 || h->root.type == bfd_link_hash_defweak
3890 || (h->elf_link_hash_flags
3891 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
3892 || (!info->shared
3893 && (sec->flags & SEC_ALLOC) != 0
3894 && h != NULL
3895 && (h->root.type == bfd_link_hash_defweak
3896 || (h->elf_link_hash_flags
3897 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
3899 struct ppc_dyn_relocs *p;
3900 struct ppc_dyn_relocs **head;
3902 /* We must copy these reloc types into the output file.
3903 Create a reloc section in dynobj and make room for
3904 this reloc. */
3905 if (sreloc == NULL)
3907 const char *name;
3908 bfd *dynobj;
3910 name = (bfd_elf_string_from_elf_section
3911 (abfd,
3912 elf_elfheader (abfd)->e_shstrndx,
3913 elf_section_data (sec)->rel_hdr.sh_name));
3914 if (name == NULL)
3915 return FALSE;
3917 if (strncmp (name, ".rela", 5) != 0
3918 || strcmp (bfd_get_section_name (abfd, sec),
3919 name + 5) != 0)
3921 (*_bfd_error_handler)
3922 (_("%s: bad relocation section name `%s\'"),
3923 bfd_archive_filename (abfd), name);
3924 bfd_set_error (bfd_error_bad_value);
3927 dynobj = htab->elf.dynobj;
3928 sreloc = bfd_get_section_by_name (dynobj, name);
3929 if (sreloc == NULL)
3931 flagword flags;
3933 sreloc = bfd_make_section (dynobj, name);
3934 flags = (SEC_HAS_CONTENTS | SEC_READONLY
3935 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3936 if ((sec->flags & SEC_ALLOC) != 0)
3937 flags |= SEC_ALLOC | SEC_LOAD;
3938 if (sreloc == NULL
3939 || ! bfd_set_section_flags (dynobj, sreloc, flags)
3940 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
3941 return FALSE;
3943 elf_section_data (sec)->sreloc = sreloc;
3946 /* If this is a global symbol, we count the number of
3947 relocations we need for this symbol. */
3948 if (h != NULL)
3950 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
3952 else
3954 /* Track dynamic relocs needed for local syms too.
3955 We really need local syms available to do this
3956 easily. Oh well. */
3958 asection *s;
3959 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
3960 sec, r_symndx);
3961 if (s == NULL)
3962 return FALSE;
3964 head = ((struct ppc_dyn_relocs **)
3965 &elf_section_data (s)->local_dynrel);
3968 p = *head;
3969 if (p == NULL || p->sec != sec)
3971 p = ((struct ppc_dyn_relocs *)
3972 bfd_alloc (htab->elf.dynobj,
3973 (bfd_size_type) sizeof *p));
3974 if (p == NULL)
3975 return FALSE;
3976 p->next = *head;
3977 *head = p;
3978 p->sec = sec;
3979 p->count = 0;
3980 p->pc_count = 0;
3983 p->count += 1;
3984 if (!MUST_BE_DYN_RELOC (r_type))
3985 p->pc_count += 1;
3987 break;
3989 default:
3990 break;
3994 return TRUE;
3997 /* Return the section that should be marked against GC for a given
3998 relocation. */
4000 static asection *
4001 ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
4002 asection *sec;
4003 struct bfd_link_info *info ATTRIBUTE_UNUSED;
4004 Elf_Internal_Rela *rel;
4005 struct elf_link_hash_entry *h;
4006 Elf_Internal_Sym *sym;
4008 asection *rsec = NULL;
4010 if (h != NULL)
4012 enum elf_ppc64_reloc_type r_type;
4013 struct ppc_link_hash_entry *fdh;
4015 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
4016 switch (r_type)
4018 case R_PPC64_GNU_VTINHERIT:
4019 case R_PPC64_GNU_VTENTRY:
4020 break;
4022 default:
4023 switch (h->root.type)
4025 case bfd_link_hash_defined:
4026 case bfd_link_hash_defweak:
4027 fdh = (struct ppc_link_hash_entry *) h;
4029 /* Function descriptor syms cause the associated
4030 function code sym section to be marked. */
4031 if (fdh->is_func_descriptor)
4032 rsec = fdh->oh->root.u.def.section;
4034 /* Function entry syms return NULL if they are in .opd
4035 and are not ._start (or others undefined on the ld
4036 command line). Thus we avoid marking all function
4037 sections, as all functions are referenced in .opd. */
4038 else if ((fdh->oh != NULL
4039 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
4040 || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
4041 rsec = h->root.u.def.section;
4042 break;
4044 case bfd_link_hash_common:
4045 rsec = h->root.u.c.p->section;
4046 break;
4048 default:
4049 break;
4053 else
4055 asection **opd_sym_section;
4057 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
4058 opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
4059 if (opd_sym_section != NULL)
4060 rsec = opd_sym_section[sym->st_value / 24];
4061 else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
4062 rsec = NULL;
4065 return rsec;
4068 /* Update the .got, .plt. and dynamic reloc reference counts for the
4069 section being removed. */
4071 static bfd_boolean
4072 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
4073 bfd *abfd;
4074 struct bfd_link_info *info;
4075 asection *sec;
4076 const Elf_Internal_Rela *relocs;
4078 struct ppc_link_hash_table *htab;
4079 Elf_Internal_Shdr *symtab_hdr;
4080 struct elf_link_hash_entry **sym_hashes;
4081 struct got_entry **local_got_ents;
4082 const Elf_Internal_Rela *rel, *relend;
4084 elf_section_data (sec)->local_dynrel = NULL;
4086 htab = ppc_hash_table (info);
4087 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4088 sym_hashes = elf_sym_hashes (abfd);
4089 local_got_ents = elf_local_got_ents (abfd);
4091 relend = relocs + sec->reloc_count;
4092 for (rel = relocs; rel < relend; rel++)
4094 unsigned long r_symndx;
4095 enum elf_ppc64_reloc_type r_type;
4096 struct elf_link_hash_entry *h;
4097 char tls_type = 0;
4099 r_symndx = ELF64_R_SYM (rel->r_info);
4100 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
4101 switch (r_type)
4103 case R_PPC64_GOT_TLSLD16:
4104 case R_PPC64_GOT_TLSLD16_LO:
4105 case R_PPC64_GOT_TLSLD16_HI:
4106 case R_PPC64_GOT_TLSLD16_HA:
4107 htab->tlsld_got.refcount -= 1;
4108 tls_type = TLS_TLS | TLS_LD;
4109 goto dogot;
4111 case R_PPC64_GOT_TLSGD16:
4112 case R_PPC64_GOT_TLSGD16_LO:
4113 case R_PPC64_GOT_TLSGD16_HI:
4114 case R_PPC64_GOT_TLSGD16_HA:
4115 tls_type = TLS_TLS | TLS_GD;
4116 goto dogot;
4118 case R_PPC64_GOT_TPREL16_DS:
4119 case R_PPC64_GOT_TPREL16_LO_DS:
4120 case R_PPC64_GOT_TPREL16_HI:
4121 case R_PPC64_GOT_TPREL16_HA:
4122 tls_type = TLS_TLS | TLS_TPREL;
4123 goto dogot;
4125 case R_PPC64_GOT_DTPREL16_DS:
4126 case R_PPC64_GOT_DTPREL16_LO_DS:
4127 case R_PPC64_GOT_DTPREL16_HI:
4128 case R_PPC64_GOT_DTPREL16_HA:
4129 tls_type = TLS_TLS | TLS_DTPREL;
4130 goto dogot;
4132 case R_PPC64_GOT16:
4133 case R_PPC64_GOT16_DS:
4134 case R_PPC64_GOT16_HA:
4135 case R_PPC64_GOT16_HI:
4136 case R_PPC64_GOT16_LO:
4137 case R_PPC64_GOT16_LO_DS:
4138 dogot:
4140 struct got_entry *ent;
4142 if (r_symndx >= symtab_hdr->sh_info)
4144 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4145 ent = h->got.glist;
4147 else
4148 ent = local_got_ents[r_symndx];
4150 for (; ent != NULL; ent = ent->next)
4151 if (ent->addend == rel->r_addend
4152 && ent->tls_type == tls_type)
4153 break;
4154 if (ent == NULL)
4155 abort ();
4156 if (ent->got.refcount > 0)
4157 ent->got.refcount -= 1;
4159 break;
4161 case R_PPC64_PLT16_HA:
4162 case R_PPC64_PLT16_HI:
4163 case R_PPC64_PLT16_LO:
4164 case R_PPC64_PLT32:
4165 case R_PPC64_PLT64:
4166 case R_PPC64_REL14:
4167 case R_PPC64_REL14_BRNTAKEN:
4168 case R_PPC64_REL14_BRTAKEN:
4169 case R_PPC64_REL24:
4170 if (r_symndx >= symtab_hdr->sh_info)
4172 struct plt_entry *ent;
4174 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4175 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4176 if (ent->addend == rel->r_addend)
4177 break;
4178 if (ent == NULL)
4179 abort ();
4180 if (ent->plt.refcount > 0)
4181 ent->plt.refcount -= 1;
4183 break;
4185 case R_PPC64_REL30:
4186 case R_PPC64_REL32:
4187 case R_PPC64_REL64:
4188 if (r_symndx >= symtab_hdr->sh_info)
4190 struct ppc_link_hash_entry *eh;
4191 struct ppc_dyn_relocs **pp;
4192 struct ppc_dyn_relocs *p;
4194 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4195 eh = (struct ppc_link_hash_entry *) h;
4197 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4198 if (p->sec == sec)
4200 p->pc_count -= 1;
4201 p->count -= 1;
4202 if (p->count == 0)
4203 *pp = p->next;
4204 break;
4207 break;
4209 case R_PPC64_DTPMOD64:
4210 case R_PPC64_DTPREL64:
4211 case R_PPC64_TPREL64:
4212 case R_PPC64_ADDR14:
4213 case R_PPC64_ADDR14_BRNTAKEN:
4214 case R_PPC64_ADDR14_BRTAKEN:
4215 case R_PPC64_ADDR16:
4216 case R_PPC64_ADDR16_DS:
4217 case R_PPC64_ADDR16_HA:
4218 case R_PPC64_ADDR16_HI:
4219 case R_PPC64_ADDR16_HIGHER:
4220 case R_PPC64_ADDR16_HIGHERA:
4221 case R_PPC64_ADDR16_HIGHEST:
4222 case R_PPC64_ADDR16_HIGHESTA:
4223 case R_PPC64_ADDR16_LO:
4224 case R_PPC64_ADDR16_LO_DS:
4225 case R_PPC64_ADDR24:
4226 case R_PPC64_ADDR32:
4227 case R_PPC64_ADDR64:
4228 case R_PPC64_UADDR16:
4229 case R_PPC64_UADDR32:
4230 case R_PPC64_UADDR64:
4231 case R_PPC64_TOC:
4232 if (r_symndx >= symtab_hdr->sh_info)
4234 struct ppc_link_hash_entry *eh;
4235 struct ppc_dyn_relocs **pp;
4236 struct ppc_dyn_relocs *p;
4238 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4239 eh = (struct ppc_link_hash_entry *) h;
4241 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
4242 if (p->sec == sec)
4244 p->count -= 1;
4245 if (p->count == 0)
4246 *pp = p->next;
4247 break;
4250 break;
4252 default:
4253 break;
4256 return TRUE;
4259 /* Called via elf_link_hash_traverse to transfer dynamic linking
4260 information on function code symbol entries to their corresponding
4261 function descriptor symbol entries. */
4262 static bfd_boolean
4263 func_desc_adjust (h, inf)
4264 struct elf_link_hash_entry *h;
4265 PTR inf;
4267 struct bfd_link_info *info;
4268 struct ppc_link_hash_table *htab;
4269 struct plt_entry *ent;
4271 if (h->root.type == bfd_link_hash_indirect)
4272 return TRUE;
4274 if (h->root.type == bfd_link_hash_warning)
4275 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4277 info = (struct bfd_link_info *) inf;
4278 htab = ppc_hash_table (info);
4280 /* If this is a function code symbol, transfer dynamic linking
4281 information to the function descriptor symbol. */
4282 if (!((struct ppc_link_hash_entry *) h)->is_func)
4283 return TRUE;
4285 if (h->root.type == bfd_link_hash_undefweak
4286 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
4287 htab->have_undefweak = TRUE;
4289 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4290 if (ent->plt.refcount > 0)
4291 break;
4292 if (ent != NULL
4293 && h->root.root.string[0] == '.'
4294 && h->root.root.string[1] != '\0')
4296 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
4297 bfd_boolean force_local;
4299 /* Find the corresponding function descriptor symbol. Create it
4300 as undefined if necessary. */
4302 if (fdh == NULL)
4303 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
4304 FALSE, FALSE, TRUE);
4306 if (fdh == NULL
4307 && info->shared
4308 && (h->root.type == bfd_link_hash_undefined
4309 || h->root.type == bfd_link_hash_undefweak))
4311 bfd *abfd;
4312 asymbol *newsym;
4313 struct bfd_link_hash_entry *bh;
4315 abfd = h->root.u.undef.abfd;
4316 newsym = bfd_make_empty_symbol (abfd);
4317 newsym->name = h->root.root.string + 1;
4318 newsym->section = bfd_und_section_ptr;
4319 newsym->value = 0;
4320 newsym->flags = BSF_OBJECT;
4321 if (h->root.type == bfd_link_hash_undefweak)
4322 newsym->flags |= BSF_WEAK;
4324 bh = &fdh->root;
4325 if ( !(_bfd_generic_link_add_one_symbol
4326 (info, abfd, newsym->name, newsym->flags,
4327 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
4329 return FALSE;
4331 fdh = (struct elf_link_hash_entry *) bh;
4332 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
4335 if (fdh != NULL
4336 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
4337 && (info->shared
4338 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4339 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
4341 if (fdh->dynindx == -1)
4342 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
4343 return FALSE;
4344 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
4345 & (ELF_LINK_HASH_REF_REGULAR
4346 | ELF_LINK_HASH_REF_DYNAMIC
4347 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
4348 | ELF_LINK_NON_GOT_REF));
4349 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
4351 fdh->plt.plist = h->plt.plist;
4352 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
4354 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
4355 ((struct ppc_link_hash_entry *) fdh)->oh = h;
4356 ((struct ppc_link_hash_entry *) h)->oh = fdh;
4359 /* Now that the info is on the function descriptor, clear the
4360 function code sym info. Any function code syms for which we
4361 don't have a definition in a regular file, we force local.
4362 This prevents a shared library from exporting syms that have
4363 been imported from another library. Function code syms that
4364 are really in the library we must leave global to prevent the
4365 linker dragging in a definition from a static library. */
4366 force_local = (info->shared
4367 && ((h->elf_link_hash_flags
4368 & ELF_LINK_HASH_DEF_REGULAR) == 0
4369 || fdh == NULL
4370 || (fdh->elf_link_hash_flags
4371 & ELF_LINK_HASH_DEF_REGULAR) == 0
4372 || (fdh->elf_link_hash_flags
4373 & ELF_LINK_FORCED_LOCAL) != 0));
4374 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4377 return TRUE;
4380 #define MIN_SAVE_FPR 14
4381 #define MAX_SAVE_FPR 31
4383 /* Called near the start of bfd_elf_size_dynamic_sections. We use
4384 this hook to a) provide some gcc support functions, and b) transfer
4385 dynamic linking information gathered so far on function code symbol
4386 entries, to their corresponding function descriptor symbol entries. */
4387 static bfd_boolean
4388 ppc64_elf_func_desc_adjust (obfd, info)
4389 bfd *obfd ATTRIBUTE_UNUSED;
4390 struct bfd_link_info *info;
4392 struct ppc_link_hash_table *htab;
4393 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
4394 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
4395 unsigned int i;
4396 struct elf_link_hash_entry *h;
4397 bfd_byte *p;
4398 char sym[10];
4400 htab = ppc_hash_table (info);
4402 if (htab->sfpr == NULL)
4403 /* We don't have any relocs. */
4404 return TRUE;
4406 /* First provide any missing ._savef* and ._restf* functions. */
4407 memcpy (sym, "._savef14", 10);
4408 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4410 sym[7] = i / 10 + '0';
4411 sym[8] = i % 10 + '0';
4412 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4413 if (h != NULL
4414 && h->root.type == bfd_link_hash_undefined)
4416 if (lowest_savef > i)
4417 lowest_savef = i;
4418 h->root.type = bfd_link_hash_defined;
4419 h->root.u.def.section = htab->sfpr;
4420 h->root.u.def.value = (i - lowest_savef) * 4;
4421 h->type = STT_FUNC;
4422 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4423 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4427 memcpy (sym, "._restf14", 10);
4428 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
4430 sym[7] = i / 10 + '0';
4431 sym[8] = i % 10 + '0';
4432 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
4433 if (h != NULL
4434 && h->root.type == bfd_link_hash_undefined)
4436 if (lowest_restf > i)
4437 lowest_restf = i;
4438 h->root.type = bfd_link_hash_defined;
4439 h->root.u.def.section = htab->sfpr;
4440 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4441 + (i - lowest_restf) * 4);
4442 h->type = STT_FUNC;
4443 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
4444 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
4448 elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
4450 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
4451 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
4453 if (htab->sfpr->_raw_size == 0)
4455 if (!htab->have_undefweak)
4457 _bfd_strip_section_from_output (info, htab->sfpr);
4458 return TRUE;
4461 htab->sfpr->_raw_size = 4;
4464 p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
4465 if (p == NULL)
4466 return FALSE;
4467 htab->sfpr->contents = p;
4469 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
4471 unsigned int fpr = i << 21;
4472 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4473 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
4474 p += 4;
4476 if (lowest_savef <= MAX_SAVE_FPR)
4478 bfd_put_32 (htab->elf.dynobj, BLR, p);
4479 p += 4;
4482 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
4484 unsigned int fpr = i << 21;
4485 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
4486 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
4487 p += 4;
4489 if (lowest_restf <= MAX_SAVE_FPR
4490 || htab->sfpr->_raw_size == 4)
4492 bfd_put_32 (htab->elf.dynobj, BLR, p);
4495 return TRUE;
4498 /* Adjust a symbol defined by a dynamic object and referenced by a
4499 regular object. The current definition is in some section of the
4500 dynamic object, but we're not including those sections. We have to
4501 change the definition to something the rest of the link can
4502 understand. */
4504 static bfd_boolean
4505 ppc64_elf_adjust_dynamic_symbol (info, h)
4506 struct bfd_link_info *info;
4507 struct elf_link_hash_entry *h;
4509 struct ppc_link_hash_table *htab;
4510 struct ppc_link_hash_entry * eh;
4511 struct ppc_dyn_relocs *p;
4512 asection *s;
4513 unsigned int power_of_two;
4515 htab = ppc_hash_table (info);
4517 /* Deal with function syms. */
4518 if (h->type == STT_FUNC
4519 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
4521 /* Clear procedure linkage table information for any symbol that
4522 won't need a .plt entry. */
4523 struct plt_entry *ent;
4524 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
4525 if (ent->plt.refcount > 0)
4526 break;
4527 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
4528 || ent == NULL
4529 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4530 || (! info->shared
4531 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
4532 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
4534 h->plt.plist = NULL;
4535 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
4537 return TRUE;
4539 else
4540 h->plt.plist = NULL;
4542 /* If this is a weak symbol, and there is a real definition, the
4543 processor independent code will have arranged for us to see the
4544 real definition first, and we can just use the same value. */
4545 if (h->weakdef != NULL)
4547 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4548 || h->weakdef->root.type == bfd_link_hash_defweak);
4549 h->root.u.def.section = h->weakdef->root.u.def.section;
4550 h->root.u.def.value = h->weakdef->root.u.def.value;
4551 return TRUE;
4554 /* This is a reference to a symbol defined by a dynamic object which
4555 is not a function. */
4557 /* If we are creating a shared library, we must presume that the
4558 only references to the symbol are via the global offset table.
4559 For such cases we need not do anything here; the relocations will
4560 be handled correctly by relocate_section. */
4561 if (info->shared)
4562 return TRUE;
4564 /* If there are no references to this symbol that do not use the
4565 GOT, we don't need to generate a copy reloc. */
4566 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
4567 return TRUE;
4569 eh = (struct ppc_link_hash_entry *) h;
4570 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4572 s = p->sec->output_section;
4573 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4574 break;
4577 /* If we didn't find any dynamic relocs in read-only sections, then
4578 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
4579 if (p == NULL)
4581 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
4582 return TRUE;
4585 /* We must allocate the symbol in our .dynbss section, which will
4586 become part of the .bss section of the executable. There will be
4587 an entry for this symbol in the .dynsym section. The dynamic
4588 object will contain position independent code, so all references
4589 from the dynamic object to this symbol will go through the global
4590 offset table. The dynamic linker will use the .dynsym entry to
4591 determine the address it must put in the global offset table, so
4592 both the dynamic object and the regular object will refer to the
4593 same memory location for the variable. */
4595 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
4596 to copy the initial value out of the dynamic object and into the
4597 runtime process image. We need to remember the offset into the
4598 .rela.bss section we are going to use. */
4599 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4601 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
4602 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
4605 /* We need to figure out the alignment required for this symbol. I
4606 have no idea how ELF linkers handle this. */
4607 power_of_two = bfd_log2 (h->size);
4608 if (power_of_two > 4)
4609 power_of_two = 4;
4611 /* Apply the required alignment. */
4612 s = htab->sdynbss;
4613 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
4614 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
4616 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
4617 return FALSE;
4620 /* Define the symbol as being at this point in the section. */
4621 h->root.u.def.section = s;
4622 h->root.u.def.value = s->_raw_size;
4624 /* Increment the section size to make room for the symbol. */
4625 s->_raw_size += h->size;
4627 return TRUE;
4630 /* If given a function descriptor symbol, hide both the function code
4631 sym and the descriptor. */
4632 static void
4633 ppc64_elf_hide_symbol (info, h, force_local)
4634 struct bfd_link_info *info;
4635 struct elf_link_hash_entry *h;
4636 bfd_boolean force_local;
4638 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
4640 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
4642 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
4644 if (fh == NULL)
4646 const char *p, *q;
4647 struct ppc_link_hash_table *htab;
4648 char save;
4650 /* We aren't supposed to use alloca in BFD because on
4651 systems which do not have alloca the version in libiberty
4652 calls xmalloc, which might cause the program to crash
4653 when it runs out of memory. This function doesn't have a
4654 return status, so there's no way to gracefully return an
4655 error. So cheat. We know that string[-1] can be safely
4656 dereferenced; It's either a string in an ELF string
4657 table, or allocated in an objalloc structure. */
4659 p = h->root.root.string - 1;
4660 save = *p;
4661 *(char *) p = '.';
4662 htab = ppc_hash_table (info);
4663 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4664 *(char *) p = save;
4666 /* Unfortunately, if it so happens that the string we were
4667 looking for was allocated immediately before this string,
4668 then we overwrote the string terminator. That's the only
4669 reason the lookup should fail. */
4670 if (fh == NULL)
4672 q = h->root.root.string + strlen (h->root.root.string);
4673 while (q >= h->root.root.string && *q == *p)
4674 --q, --p;
4675 if (q < h->root.root.string && *p == '.')
4676 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
4678 if (fh != NULL)
4680 ((struct ppc_link_hash_entry *) h)->oh = fh;
4681 ((struct ppc_link_hash_entry *) fh)->oh = h;
4684 if (fh != NULL)
4685 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
4689 static bfd_boolean
4690 get_sym_h (hp, symp, symsecp, tlstypep, locsymsp, r_symndx, ibfd)
4691 struct elf_link_hash_entry **hp;
4692 Elf_Internal_Sym **symp;
4693 asection **symsecp;
4694 char **tlstypep;
4695 Elf_Internal_Sym **locsymsp;
4696 unsigned long r_symndx;
4697 bfd *ibfd;
4699 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4701 if (r_symndx >= symtab_hdr->sh_info)
4703 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
4704 struct elf_link_hash_entry *h;
4706 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4707 while (h->root.type == bfd_link_hash_indirect
4708 || h->root.type == bfd_link_hash_warning)
4709 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4711 if (hp != NULL)
4712 *hp = h;
4714 if (symp != NULL)
4715 *symp = NULL;
4717 if (symsecp != NULL)
4719 asection *symsec = NULL;
4720 if (h->root.type == bfd_link_hash_defined
4721 || h->root.type == bfd_link_hash_defweak)
4722 symsec = h->root.u.def.section;
4723 *symsecp = symsec;
4726 if (tlstypep != NULL)
4728 struct ppc_link_hash_entry *eh;
4730 eh = (struct ppc_link_hash_entry *) h;
4731 *tlstypep = &eh->tls_type;
4734 else
4736 Elf_Internal_Sym *sym;
4737 Elf_Internal_Sym *locsyms = *locsymsp;
4739 if (locsyms == NULL)
4741 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
4742 if (locsyms == NULL)
4743 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
4744 symtab_hdr->sh_info,
4745 0, NULL, NULL, NULL);
4746 if (locsyms == NULL)
4747 return FALSE;
4748 *locsymsp = locsyms;
4750 sym = locsyms + r_symndx;
4752 if (hp != NULL)
4753 *hp = NULL;
4755 if (symp != NULL)
4756 *symp = sym;
4758 if (symsecp != NULL)
4760 asection *symsec = NULL;
4761 if ((sym->st_shndx != SHN_UNDEF
4762 && sym->st_shndx < SHN_LORESERVE)
4763 || sym->st_shndx > SHN_HIRESERVE)
4764 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
4765 *symsecp = symsec;
4768 if (tlstypep != NULL)
4770 struct got_entry **lgot_ents;
4771 char *tlstype;
4773 tlstype = NULL;
4774 lgot_ents = elf_local_got_ents (ibfd);
4775 if (lgot_ents != NULL)
4777 char *lgot_types = (char *) (lgot_ents + symtab_hdr->sh_info);
4778 tlstype = &lgot_types[r_symndx];
4780 *tlstypep = tlstype;
4783 return TRUE;
4786 /* Returns TLS_TYPE for the given REL symbol. Function return is 0 on
4787 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
4788 type suitable for optimization, and 1 otherwise. */
4790 static int
4791 get_tls_type (tls_type, locsymsp, rel, ibfd)
4792 char **tls_type;
4793 Elf_Internal_Sym **locsymsp;
4794 const Elf_Internal_Rela *rel;
4795 bfd *ibfd;
4797 unsigned long r_symndx;
4798 unsigned int next_r;
4799 struct elf_link_hash_entry *h;
4800 Elf_Internal_Sym *sym;
4801 asection *sec;
4802 bfd_vma off;
4804 r_symndx = ELF64_R_SYM (rel->r_info);
4805 if (!get_sym_h (&h, &sym, &sec, tls_type, locsymsp, r_symndx, ibfd))
4806 return 0;
4808 if ((*tls_type != NULL && **tls_type != 0)
4809 || sec == NULL
4810 || ppc64_elf_section_data (sec)->t_symndx == NULL)
4811 return 1;
4813 /* Look inside a TOC section too. */
4814 if (h != NULL)
4816 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
4817 off = h->root.u.def.value;
4819 else
4820 off = sym->st_value;
4821 off += rel->r_addend;
4822 BFD_ASSERT (off % 8 == 0);
4823 r_symndx = ppc64_elf_section_data (sec)->t_symndx[off / 8];
4824 next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
4825 if (!get_sym_h (&h, &sym, &sec, tls_type, locsymsp, r_symndx, ibfd))
4826 return 0;
4827 if (h == NULL
4828 || h->root.type == bfd_link_hash_defined
4829 || h->root.type == bfd_link_hash_defweak)
4831 if (next_r == (unsigned) -1)
4832 return 2;
4833 if (next_r == (unsigned) -2
4834 && (h == NULL
4835 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
4836 return 3;
4838 return 1;
4841 bfd_boolean
4842 ppc64_elf_edit_opd (obfd, info)
4843 bfd *obfd;
4844 struct bfd_link_info *info;
4846 bfd *ibfd;
4848 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4850 asection *sec;
4851 Elf_Internal_Rela *relstart, *rel, *relend;
4852 Elf_Internal_Shdr *symtab_hdr;
4853 Elf_Internal_Sym *local_syms;
4854 struct elf_link_hash_entry **sym_hashes;
4855 bfd_vma offset;
4856 bfd_size_type amt;
4857 long *adjust;
4858 bfd_boolean need_edit;
4860 sec = bfd_get_section_by_name (ibfd, ".opd");
4861 if (sec == NULL)
4862 continue;
4864 amt = sec->_raw_size * sizeof (long) / 24;
4865 adjust = ppc64_elf_section_data (sec)->opd.adjust;
4866 if (adjust == NULL)
4868 /* Must be a ld -r link. ie. check_relocs hasn't been
4869 called. */
4870 adjust = (long *) bfd_zalloc (obfd, amt);
4871 ppc64_elf_section_data (sec)->opd.adjust = adjust;
4873 memset (adjust, 0, (size_t) amt);
4875 if (sec->output_section == bfd_abs_section_ptr)
4876 continue;
4878 /* Look through the section relocs. */
4879 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
4880 continue;
4882 local_syms = NULL;
4883 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4884 sym_hashes = elf_sym_hashes (ibfd);
4886 /* Read the relocations. */
4887 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
4888 (Elf_Internal_Rela *) NULL,
4889 info->keep_memory);
4890 if (relstart == NULL)
4891 return FALSE;
4893 /* First run through the relocs to check they are sane, and to
4894 determine whether we need to edit this opd section. */
4895 need_edit = FALSE;
4896 offset = 0;
4897 relend = relstart + sec->reloc_count;
4898 for (rel = relstart; rel < relend; rel++)
4900 enum elf_ppc64_reloc_type r_type;
4901 unsigned long r_symndx;
4902 asection *sym_sec;
4903 struct elf_link_hash_entry *h;
4904 Elf_Internal_Sym *sym;
4906 /* .opd contains a regular array of 24 byte entries. We're
4907 only interested in the reloc pointing to a function entry
4908 point. */
4909 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
4910 if (r_type == R_PPC64_TOC)
4911 continue;
4913 if (r_type != R_PPC64_ADDR64)
4915 (*_bfd_error_handler)
4916 (_("%s: unexpected reloc type %u in .opd section"),
4917 bfd_archive_filename (ibfd), r_type);
4918 need_edit = FALSE;
4919 break;
4922 if (rel + 1 >= relend)
4923 continue;
4924 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
4925 if (r_type != R_PPC64_TOC)
4926 continue;
4928 if (rel->r_offset != offset)
4930 /* If someone messes with .opd alignment then after a
4931 "ld -r" we might have padding in the middle of .opd.
4932 Also, there's nothing to prevent someone putting
4933 something silly in .opd with the assembler. No .opd
4934 optimization for them! */
4935 (*_bfd_error_handler)
4936 (_("%s: .opd is not a regular array of opd entries"),
4937 bfd_archive_filename (ibfd));
4938 need_edit = FALSE;
4939 break;
4942 r_symndx = ELF64_R_SYM (rel->r_info);
4943 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
4944 r_symndx, ibfd))
4945 goto error_free_rel;
4947 if (sym_sec == NULL || sym_sec->owner == NULL)
4949 const char *sym_name;
4950 if (h != NULL)
4951 sym_name = h->root.root.string;
4952 else
4953 sym_name = bfd_elf_local_sym_name (ibfd, sym);
4955 (*_bfd_error_handler)
4956 (_("%s: undefined sym `%s' in .opd section"),
4957 bfd_archive_filename (ibfd),
4958 sym_name);
4959 need_edit = FALSE;
4960 break;
4963 /* opd entries are always for functions defined in the
4964 current input bfd. If the symbol isn't defined in the
4965 input bfd, then we won't be using the function in this
4966 bfd; It must be defined in a linkonce section in another
4967 bfd, or is weak. It's also possible that we are
4968 discarding the function due to a linker script /DISCARD/,
4969 which we test for via the output_section. */
4970 if (sym_sec->owner != ibfd
4971 || sym_sec->output_section == bfd_abs_section_ptr)
4972 need_edit = TRUE;
4974 offset += 24;
4977 if (need_edit)
4979 Elf_Internal_Rela *write_rel;
4980 bfd_byte *rptr, *wptr;
4981 bfd_boolean skip;
4983 /* This seems a waste of time as input .opd sections are all
4984 zeros as generated by gcc, but I suppose there's no reason
4985 this will always be so. We might start putting something in
4986 the third word of .opd entries. */
4987 if ((sec->flags & SEC_IN_MEMORY) == 0)
4989 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
4990 if (loc == NULL
4991 || !bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
4992 sec->_raw_size))
4994 if (local_syms != NULL
4995 && symtab_hdr->contents != (unsigned char *) local_syms)
4996 free (local_syms);
4997 error_free_rel:
4998 if (elf_section_data (sec)->relocs != relstart)
4999 free (relstart);
5000 return FALSE;
5002 sec->contents = loc;
5003 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
5006 elf_section_data (sec)->relocs = relstart;
5008 wptr = sec->contents;
5009 rptr = sec->contents;
5010 write_rel = relstart;
5011 skip = FALSE;
5012 offset = 0;
5013 for (rel = relstart; rel < relend; rel++)
5015 if (rel->r_offset == offset)
5017 unsigned long r_symndx;
5018 asection *sym_sec;
5019 struct elf_link_hash_entry *h;
5020 Elf_Internal_Sym *sym;
5022 r_symndx = ELF64_R_SYM (rel->r_info);
5023 get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
5024 r_symndx, ibfd);
5026 skip = (sym_sec->owner != ibfd
5027 || sym_sec->output_section == bfd_abs_section_ptr);
5028 if (skip)
5030 if (h != NULL && sym_sec->owner == ibfd)
5032 /* Arrange for the function descriptor sym
5033 to be dropped. */
5034 struct ppc_link_hash_entry *fdh;
5035 struct ppc_link_hash_entry *fh;
5037 fh = (struct ppc_link_hash_entry *) h;
5038 fdh = (struct ppc_link_hash_entry *) fh->oh;
5039 if (fdh == NULL)
5041 const char *fd_name;
5042 struct ppc_link_hash_table *htab;
5044 fd_name = h->root.root.string + 1;
5045 htab = ppc_hash_table (info);
5046 fdh = (struct ppc_link_hash_entry *)
5047 elf_link_hash_lookup (&htab->elf, fd_name,
5048 FALSE, FALSE, FALSE);
5049 fdh->is_func_descriptor = 1;
5050 fdh->oh = &fh->elf;
5051 fh->is_func = 1;
5052 fh->oh = &fdh->elf;
5055 fdh->elf.root.u.def.value = 0;
5056 fdh->elf.root.u.def.section = sym_sec;
5059 else
5061 /* We'll be keeping this opd entry. */
5063 if (h != NULL)
5065 /* Redefine the function descriptor symbol
5066 to this location in the opd section.
5067 We've checked above that opd relocs are
5068 ordered. */
5069 struct ppc_link_hash_entry *fdh;
5070 struct ppc_link_hash_entry *fh;
5072 fh = (struct ppc_link_hash_entry *) h;
5073 fdh = (struct ppc_link_hash_entry *) fh->oh;
5074 if (fdh == NULL)
5076 const char *fd_name;
5077 struct ppc_link_hash_table *htab;
5079 fd_name = h->root.root.string + 1;
5080 htab = ppc_hash_table (info);
5081 fdh = (struct ppc_link_hash_entry *)
5082 elf_link_hash_lookup (&htab->elf, fd_name,
5083 FALSE, FALSE, FALSE);
5084 fdh->is_func_descriptor = 1;
5085 fdh->oh = &fh->elf;
5086 fh->is_func = 1;
5087 fh->oh = &fdh->elf;
5090 fdh->elf.root.u.def.value = wptr - sec->contents;
5092 else
5094 /* Local syms are a bit tricky. We could
5095 tweak them as they can be cached, but
5096 we'd need to look through the local syms
5097 for the function descriptor sym which we
5098 don't have at the moment. So keep an
5099 array of adjustments. */
5100 adjust[rel->r_offset / 24] = wptr - rptr;
5103 if (wptr != rptr)
5104 memcpy (wptr, rptr, 24);
5105 wptr += 24;
5107 rptr += 24;
5108 offset += 24;
5111 /* We need to adjust any reloc offsets to point to the
5112 new opd entries. While we're at it, we may as well
5113 remove redundant relocs. */
5114 if (!skip)
5116 rel->r_offset += wptr - rptr;
5117 if (write_rel != rel)
5118 memcpy (write_rel, rel, sizeof (*rel));
5119 ++write_rel;
5123 sec->_cooked_size = wptr - sec->contents;
5124 sec->reloc_count = write_rel - relstart;
5125 /* Fudge the size too, as this is used later in
5126 elf_bfd_final_link if we are emitting relocs. */
5127 elf_section_data (sec)->rel_hdr.sh_size
5128 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
5129 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
5131 else if (elf_section_data (sec)->relocs != relstart)
5132 free (relstart);
5134 if (local_syms != NULL
5135 && symtab_hdr->contents != (unsigned char *) local_syms)
5137 if (!info->keep_memory)
5138 free (local_syms);
5139 else
5140 symtab_hdr->contents = (unsigned char *) local_syms;
5144 return TRUE;
5147 /* Set htab->tls_sec. */
5149 bfd_boolean
5150 ppc64_elf_tls_setup (obfd, info)
5151 bfd *obfd;
5152 struct bfd_link_info *info;
5154 asection *tls;
5155 struct ppc_link_hash_table *htab;
5157 for (tls = obfd->sections; tls != NULL; tls = tls->next)
5158 if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
5159 == (SEC_THREAD_LOCAL | SEC_LOAD))
5160 break;
5162 htab = ppc_hash_table (info);
5163 htab->tls_sec = tls;
5164 return tls != NULL;
5167 /* Run through all the TLS relocs looking for optimization
5168 opportunities. The linker has been hacked (see ppc64elf.em) to do
5169 a preliminary section layout so that we know the TLS segment
5170 offsets. We can't optimize earlier because some optimizations need
5171 to know the tp offset, and we need to optimize before allocating
5172 dynamic relocations. */
5174 bfd_boolean
5175 ppc64_elf_tls_optimize (obfd, info)
5176 bfd *obfd ATTRIBUTE_UNUSED;
5177 struct bfd_link_info *info;
5179 bfd *ibfd;
5180 asection *sec;
5181 struct ppc_link_hash_table *htab;
5183 if (info->relocateable || info->shared)
5184 return TRUE;
5186 htab = ppc_hash_table (info);
5187 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5189 Elf_Internal_Sym *locsyms = NULL;
5191 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
5192 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
5194 Elf_Internal_Rela *relstart, *rel, *relend;
5195 int expecting_tls_get_addr;
5197 /* Read the relocations. */
5198 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
5199 (Elf_Internal_Rela *) NULL,
5200 info->keep_memory);
5201 if (relstart == NULL)
5202 return FALSE;
5204 expecting_tls_get_addr = 0;
5205 relend = relstart + sec->reloc_count;
5206 for (rel = relstart; rel < relend; rel++)
5208 enum elf_ppc64_reloc_type r_type;
5209 unsigned long r_symndx;
5210 struct elf_link_hash_entry *h;
5211 Elf_Internal_Sym *sym;
5212 asection *sym_sec;
5213 char *tls_type;
5214 char tls_set, tls_clear, got_tls_type = 0;
5215 bfd_vma value;
5216 bfd_boolean ok_tprel, is_local;
5218 r_symndx = ELF64_R_SYM (rel->r_info);
5219 if (!get_sym_h (&h, &sym, &sym_sec, &tls_type, &locsyms,
5220 r_symndx, ibfd))
5222 err_free_rel:
5223 if (elf_section_data (sec)->relocs != relstart)
5224 free (relstart);
5225 if (locsyms != NULL
5226 && (elf_tdata (ibfd)->symtab_hdr.contents
5227 != (unsigned char *) locsyms))
5228 free (locsyms);
5229 return FALSE;
5232 if (h != NULL)
5234 if (h->root.type != bfd_link_hash_defined
5235 && h->root.type != bfd_link_hash_defweak)
5236 continue;
5237 value = h->root.u.def.value;
5239 else
5240 value = sym->st_value;
5242 ok_tprel = FALSE;
5243 is_local = FALSE;
5244 if (h == NULL
5245 || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
5247 is_local = TRUE;
5248 value += sym_sec->output_offset;
5249 value += sym_sec->output_section->vma;
5250 value -= htab->tls_sec->vma;
5251 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
5252 < (bfd_vma) 1 << 32);
5255 r_type
5256 = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5257 switch (r_type)
5259 case R_PPC64_GOT_TLSLD16:
5260 case R_PPC64_GOT_TLSLD16_LO:
5261 case R_PPC64_GOT_TLSLD16_HI:
5262 case R_PPC64_GOT_TLSLD16_HA:
5263 /* These relocs should never be against a symbol
5264 defined in a shared lib. Leave them alone if
5265 that turns out to be the case. */
5266 if (!is_local)
5267 continue;
5269 htab->tlsld_got.refcount -= 1;
5270 /* LD -> LE */
5271 tls_set = 0;
5272 tls_clear = TLS_LD;
5273 got_tls_type = TLS_TLS | TLS_LD;
5274 expecting_tls_get_addr = 1;
5275 break;
5277 case R_PPC64_GOT_TLSGD16:
5278 case R_PPC64_GOT_TLSGD16_LO:
5279 case R_PPC64_GOT_TLSGD16_HI:
5280 case R_PPC64_GOT_TLSGD16_HA:
5281 if (ok_tprel)
5282 /* GD -> LE */
5283 tls_set = 0;
5284 else
5285 /* GD -> IE */
5286 tls_set = TLS_TLS | TLS_TPRELGD;
5287 tls_clear = TLS_GD;
5288 got_tls_type = TLS_TLS | TLS_GD;
5289 expecting_tls_get_addr = 1;
5290 break;
5292 case R_PPC64_GOT_TPREL16_DS:
5293 case R_PPC64_GOT_TPREL16_LO_DS:
5294 case R_PPC64_GOT_TPREL16_HI:
5295 case R_PPC64_GOT_TPREL16_HA:
5296 expecting_tls_get_addr = 0;
5297 if (ok_tprel)
5299 /* IE -> LE */
5300 tls_set = 0;
5301 tls_clear = TLS_TPREL;
5302 got_tls_type = TLS_TLS | TLS_TPREL;
5303 break;
5305 else
5306 continue;
5308 case R_PPC64_REL14:
5309 case R_PPC64_REL14_BRTAKEN:
5310 case R_PPC64_REL14_BRNTAKEN:
5311 case R_PPC64_REL24:
5312 if (h != NULL
5313 && h == htab->tls_get_addr)
5315 if (!expecting_tls_get_addr
5316 && rel != relstart
5317 && ((ELF64_R_TYPE (rel[-1].r_info)
5318 == R_PPC64_TOC16)
5319 || (ELF64_R_TYPE (rel[-1].r_info)
5320 == R_PPC64_TOC16_LO)))
5322 /* Check for toc tls entries. */
5323 char *toc_tls;
5324 int retval;
5326 retval = get_tls_type (&toc_tls, &locsyms,
5327 rel - 1, ibfd);
5328 if (retval == 0)
5329 goto err_free_rel;
5330 if (toc_tls != NULL)
5331 expecting_tls_get_addr = retval > 1;
5334 if (expecting_tls_get_addr)
5336 struct plt_entry *ent;
5337 for (ent = h->plt.plist; ent; ent = ent->next)
5338 if (ent->addend == 0)
5340 if (ent->plt.refcount > 0)
5341 ent->plt.refcount -= 1;
5342 break;
5346 expecting_tls_get_addr = 0;
5347 continue;
5349 case R_PPC64_TPREL64:
5350 expecting_tls_get_addr = 0;
5351 if (ok_tprel)
5353 /* IE -> LE */
5354 tls_set = TLS_EXPLICIT;
5355 tls_clear = TLS_TPREL;
5356 break;
5358 else
5359 continue;
5361 case R_PPC64_DTPMOD64:
5362 expecting_tls_get_addr = 0;
5363 if (rel + 1 < relend
5364 && (rel[1].r_info
5365 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
5366 && rel[1].r_offset == rel->r_offset + 8)
5368 if (ok_tprel)
5369 /* GD -> LE */
5370 tls_set = TLS_EXPLICIT | TLS_GD;
5371 else
5372 /* GD -> IE */
5373 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
5374 tls_clear = TLS_GD;
5376 else
5378 if (!is_local)
5379 continue;
5381 /* LD -> LE */
5382 tls_set = TLS_EXPLICIT;
5383 tls_clear = TLS_LD;
5385 break;
5387 default:
5388 expecting_tls_get_addr = 0;
5389 continue;
5392 if ((tls_set & TLS_EXPLICIT) == 0)
5394 struct got_entry *ent;
5396 /* Adjust got entry for this reloc. */
5397 if (h != NULL)
5398 ent = h->got.glist;
5399 else
5400 ent = elf_local_got_ents (ibfd)[r_symndx];
5402 for (; ent != NULL; ent = ent->next)
5403 if (ent->addend == rel->r_addend
5404 && ent->tls_type == got_tls_type)
5405 break;
5406 if (ent == NULL)
5407 abort ();
5409 if (tls_set == 0)
5411 /* We managed to get rid of a got entry. */
5412 if (ent->got.refcount > 0)
5413 ent->got.refcount -= 1;
5416 else if (h != NULL)
5418 struct ppc_link_hash_entry * eh;
5419 struct ppc_dyn_relocs **pp;
5420 struct ppc_dyn_relocs *p;
5422 /* Adjust dynamic relocs. */
5423 eh = (struct ppc_link_hash_entry *) h;
5424 for (pp = &eh->dyn_relocs;
5425 (p = *pp) != NULL;
5426 pp = &p->next)
5427 if (p->sec == sec)
5429 /* If we got rid of a DTPMOD/DTPREL reloc
5430 pair then we'll lose one or two dyn
5431 relocs. */
5432 if (tls_set == (TLS_EXPLICIT | TLS_GD))
5433 p->count -= 1;
5434 p->count -= 1;
5435 if (p->count == 0)
5436 *pp = p->next;
5437 break;
5441 *tls_type |= tls_set;
5442 *tls_type &= ~tls_clear;
5445 if (elf_section_data (sec)->relocs != relstart)
5446 free (relstart);
5449 if (locsyms != NULL
5450 && (elf_tdata (ibfd)->symtab_hdr.contents
5451 != (unsigned char *) locsyms))
5453 if (!info->keep_memory)
5454 free (locsyms);
5455 else
5456 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
5459 return TRUE;
5462 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
5463 will be called from elflink.h. If elflink.h doesn't call our
5464 finish_dynamic_symbol routine, we'll need to do something about
5465 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
5466 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
5467 ((DYN) \
5468 && ((INFO)->shared \
5469 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
5470 && ((H)->dynindx != -1 \
5471 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
5473 /* Allocate space in .plt, .got and associated reloc sections for
5474 dynamic relocs. */
5476 static bfd_boolean
5477 allocate_dynrelocs (h, inf)
5478 struct elf_link_hash_entry *h;
5479 PTR inf;
5481 struct bfd_link_info *info;
5482 struct ppc_link_hash_table *htab;
5483 asection *s;
5484 struct ppc_link_hash_entry *eh;
5485 struct ppc_dyn_relocs *p;
5486 struct got_entry *gent;
5488 if (h->root.type == bfd_link_hash_indirect)
5489 return TRUE;
5491 if (h->root.type == bfd_link_hash_warning)
5492 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5494 info = (struct bfd_link_info *) inf;
5495 htab = ppc_hash_table (info);
5497 if (htab->elf.dynamic_sections_created
5498 && h->dynindx != -1
5499 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
5501 struct plt_entry *pent;
5502 bfd_boolean doneone = FALSE;
5503 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
5504 if (pent->plt.refcount > 0)
5506 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
5508 /* If this is the first .plt entry, make room for the special
5509 first entry. */
5510 s = htab->splt;
5511 if (s->_raw_size == 0)
5512 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
5514 pent->plt.offset = s->_raw_size;
5516 /* Make room for this entry. */
5517 s->_raw_size += PLT_ENTRY_SIZE;
5519 /* Make room for the .glink code. */
5520 s = htab->sglink;
5521 if (s->_raw_size == 0)
5522 s->_raw_size += GLINK_CALL_STUB_SIZE;
5523 /* We need bigger stubs past index 32767. */
5524 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
5525 s->_raw_size += 4;
5526 s->_raw_size += 2*4;
5528 /* We also need to make an entry in the .rela.plt section. */
5529 s = htab->srelplt;
5530 s->_raw_size += sizeof (Elf64_External_Rela);
5531 doneone = TRUE;
5533 else
5534 pent->plt.offset = (bfd_vma) -1;
5535 if (!doneone)
5537 h->plt.plist = NULL;
5538 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5541 else
5543 h->plt.plist = NULL;
5544 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
5547 eh = (struct ppc_link_hash_entry *) h;
5548 /* Run through the TLS GD got entries first if we're changing them
5549 to TPREL. */
5550 if ((eh->tls_type & TLS_TPRELGD) != 0)
5551 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5552 if (gent->got.refcount > 0
5553 && (gent->tls_type & TLS_GD) != 0)
5555 /* This was a GD entry that has been converted to TPREL. If
5556 there happens to be a TPREL entry we can use that one. */
5557 struct got_entry *ent;
5558 for (ent = h->got.glist; ent != NULL; ent = ent->next)
5559 if (ent->got.refcount > 0
5560 && (ent->tls_type & TLS_TPREL) != 0
5561 && ent->addend == gent->addend)
5563 gent->got.refcount = 0;
5564 break;
5567 /* If not, then we'll be using our own TPREL entry. */
5568 if (gent->got.refcount != 0)
5569 gent->tls_type = TLS_TLS | TLS_TPREL;
5572 for (gent = h->got.glist; gent != NULL; gent = gent->next)
5573 if (gent->got.refcount > 0)
5575 bfd_boolean dyn;
5577 /* Make sure this symbol is output as a dynamic symbol.
5578 Undefined weak syms won't yet be marked as dynamic,
5579 nor will all TLS symbols. */
5580 if (h->dynindx == -1
5581 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5583 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5584 return FALSE;
5587 if ((gent->tls_type & TLS_LD) != 0)
5589 gent->got.offset = htab->tlsld_got.offset;
5590 continue;
5593 s = htab->sgot;
5594 gent->got.offset = s->_raw_size;
5595 s->_raw_size += (gent->tls_type & eh->tls_type & TLS_GD) ? 16 : 8;
5596 dyn = htab->elf.dynamic_sections_created;
5597 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
5598 htab->srelgot->_raw_size
5599 += (gent->tls_type & eh->tls_type & TLS_GD
5600 ? 2 * sizeof (Elf64_External_Rela)
5601 : sizeof (Elf64_External_Rela));
5603 else
5604 gent->got.offset = (bfd_vma) -1;
5606 if (eh->dyn_relocs == NULL)
5607 return TRUE;
5609 /* In the shared -Bsymbolic case, discard space allocated for
5610 dynamic pc-relative relocs against symbols which turn out to be
5611 defined in regular objects. For the normal shared case, discard
5612 space for relocs that have become local due to symbol visibility
5613 changes. */
5615 if (info->shared)
5617 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
5618 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
5619 || info->symbolic))
5621 struct ppc_dyn_relocs **pp;
5623 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5625 p->count -= p->pc_count;
5626 p->pc_count = 0;
5627 if (p->count == 0)
5628 *pp = p->next;
5629 else
5630 pp = &p->next;
5634 else
5636 /* For the non-shared case, discard space for relocs against
5637 symbols which turn out to need copy relocs or are not
5638 dynamic. */
5640 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5641 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5642 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
5643 || (htab->elf.dynamic_sections_created
5644 && (h->root.type == bfd_link_hash_undefweak
5645 || h->root.type == bfd_link_hash_undefined))))
5647 /* Make sure this symbol is output as a dynamic symbol.
5648 Undefined weak syms won't yet be marked as dynamic. */
5649 if (h->dynindx == -1
5650 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
5652 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
5653 return FALSE;
5656 /* If that succeeded, we know we'll be keeping all the
5657 relocs. */
5658 if (h->dynindx != -1)
5659 goto keep;
5662 eh->dyn_relocs = NULL;
5664 keep: ;
5667 /* Finally, allocate space. */
5668 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5670 asection *sreloc = elf_section_data (p->sec)->sreloc;
5671 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
5674 return TRUE;
5677 /* Find any dynamic relocs that apply to read-only sections. */
5679 static bfd_boolean
5680 readonly_dynrelocs (h, inf)
5681 struct elf_link_hash_entry *h;
5682 PTR inf;
5684 struct ppc_link_hash_entry *eh;
5685 struct ppc_dyn_relocs *p;
5687 if (h->root.type == bfd_link_hash_warning)
5688 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5690 eh = (struct ppc_link_hash_entry *) h;
5691 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5693 asection *s = p->sec->output_section;
5695 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5697 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5699 info->flags |= DF_TEXTREL;
5701 /* Not an error, just cut short the traversal. */
5702 return FALSE;
5705 return TRUE;
5708 /* Set the sizes of the dynamic sections. */
5710 static bfd_boolean
5711 ppc64_elf_size_dynamic_sections (output_bfd, info)
5712 bfd *output_bfd ATTRIBUTE_UNUSED;
5713 struct bfd_link_info *info;
5715 struct ppc_link_hash_table *htab;
5716 bfd *dynobj;
5717 asection *s;
5718 bfd_boolean relocs;
5719 bfd *ibfd;
5721 htab = ppc_hash_table (info);
5722 dynobj = htab->elf.dynobj;
5723 if (dynobj == NULL)
5724 abort ();
5726 if (htab->elf.dynamic_sections_created)
5728 /* Set the contents of the .interp section to the interpreter. */
5729 if (! info->shared)
5731 s = bfd_get_section_by_name (dynobj, ".interp");
5732 if (s == NULL)
5733 abort ();
5734 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
5735 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5739 if (htab->tlsld_got.refcount > 0)
5741 htab->tlsld_got.offset = htab->sgot->_raw_size;
5742 htab->sgot->_raw_size += 16;
5743 if (info->shared)
5744 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
5746 else
5747 htab->tlsld_got.offset = (bfd_vma) -1;
5749 /* Set up .got offsets for local syms, and space for local dynamic
5750 relocs. */
5751 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
5753 struct got_entry **lgot_ents;
5754 struct got_entry **end_lgot_ents;
5755 char *lgot_types;
5756 bfd_size_type locsymcount;
5757 Elf_Internal_Shdr *symtab_hdr;
5758 asection *srel;
5760 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
5761 continue;
5763 for (s = ibfd->sections; s != NULL; s = s->next)
5765 struct ppc_dyn_relocs *p;
5767 for (p = *((struct ppc_dyn_relocs **)
5768 &elf_section_data (s)->local_dynrel);
5769 p != NULL;
5770 p = p->next)
5772 if (!bfd_is_abs_section (p->sec)
5773 && bfd_is_abs_section (p->sec->output_section))
5775 /* Input section has been discarded, either because
5776 it is a copy of a linkonce section or due to
5777 linker script /DISCARD/, so we'll be discarding
5778 the relocs too. */
5780 else if (p->count != 0)
5782 srel = elf_section_data (p->sec)->sreloc;
5783 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
5784 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
5785 info->flags |= DF_TEXTREL;
5790 lgot_ents = elf_local_got_ents (ibfd);
5791 if (!lgot_ents)
5792 continue;
5794 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5795 locsymcount = symtab_hdr->sh_info;
5796 end_lgot_ents = lgot_ents + locsymcount;
5797 lgot_types = (char *) end_lgot_ents;
5798 s = htab->sgot;
5799 srel = htab->srelgot;
5800 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_types)
5802 struct got_entry *ent;
5804 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
5805 if (ent->got.refcount > 0)
5807 if ((ent->tls_type & *lgot_types & TLS_LD) != 0)
5809 if (htab->tlsld_got.offset == (bfd_vma) -1)
5811 htab->tlsld_got.offset = s->_raw_size;
5812 s->_raw_size += 16;
5813 if (info->shared)
5814 srel->_raw_size += sizeof (Elf64_External_Rela);
5816 ent->got.offset = htab->tlsld_got.offset;
5818 else
5820 ent->got.offset = s->_raw_size;
5821 if ((ent->tls_type & *lgot_types & TLS_GD) != 0)
5823 s->_raw_size += 16;
5824 if (info->shared)
5825 srel->_raw_size += 2 * sizeof (Elf64_External_Rela);
5827 else
5829 s->_raw_size += 8;
5830 if (info->shared)
5831 srel->_raw_size += sizeof (Elf64_External_Rela);
5835 else
5836 ent->got.offset = (bfd_vma) -1;
5840 /* Allocate global sym .plt and .got entries, and space for global
5841 sym dynamic relocs. */
5842 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
5844 /* We now have determined the sizes of the various dynamic sections.
5845 Allocate memory for them. */
5846 relocs = FALSE;
5847 for (s = dynobj->sections; s != NULL; s = s->next)
5849 if ((s->flags & SEC_LINKER_CREATED) == 0)
5850 continue;
5852 /* Reset _cooked_size since prelim layout will set it wrongly,
5853 and a non-zero _cooked_size sticks. */
5854 s->_cooked_size = 0;
5856 if (s == htab->sbrlt || s == htab->srelbrlt)
5857 /* These haven't been allocated yet; don't strip. */
5858 continue;
5859 else if (s == htab->splt
5860 || s == htab->sgot
5861 || s == htab->sglink)
5863 /* Strip this section if we don't need it; see the
5864 comment below. */
5866 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
5868 if (s->_raw_size == 0)
5870 /* If we don't need this section, strip it from the
5871 output file. This is mostly to handle .rela.bss and
5872 .rela.plt. We must create both sections in
5873 create_dynamic_sections, because they must be created
5874 before the linker maps input sections to output
5875 sections. The linker does that before
5876 adjust_dynamic_symbol is called, and it is that
5877 function which decides whether anything needs to go
5878 into these sections. */
5880 else
5882 if (s != htab->srelplt)
5883 relocs = TRUE;
5885 /* We use the reloc_count field as a counter if we need
5886 to copy relocs into the output file. */
5887 s->reloc_count = 0;
5890 else
5892 /* It's not one of our sections, so don't allocate space. */
5893 continue;
5896 if (s->_raw_size == 0)
5898 _bfd_strip_section_from_output (info, s);
5899 continue;
5902 /* .plt is in the bss section. We don't initialise it. */
5903 if ((s->flags & SEC_LOAD) == 0)
5904 continue;
5906 /* Allocate memory for the section contents. We use bfd_zalloc
5907 here in case unused entries are not reclaimed before the
5908 section's contents are written out. This should not happen,
5909 but this way if it does we get a R_PPC64_NONE reloc in .rela
5910 sections instead of garbage.
5911 We also rely on the section contents being zero when writing
5912 the GOT. */
5913 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
5914 if (s->contents == NULL)
5915 return FALSE;
5918 if (htab->elf.dynamic_sections_created)
5920 /* Add some entries to the .dynamic section. We fill in the
5921 values later, in ppc64_elf_finish_dynamic_sections, but we
5922 must add the entries now so that we get the correct size for
5923 the .dynamic section. The DT_DEBUG entry is filled in by the
5924 dynamic linker and used by the debugger. */
5925 #define add_dynamic_entry(TAG, VAL) \
5926 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
5928 if (!info->shared)
5930 if (!add_dynamic_entry (DT_DEBUG, 0))
5931 return FALSE;
5934 if (htab->splt != NULL && htab->splt->_raw_size != 0)
5936 if (!add_dynamic_entry (DT_PLTGOT, 0)
5937 || !add_dynamic_entry (DT_PLTRELSZ, 0)
5938 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5939 || !add_dynamic_entry (DT_JMPREL, 0)
5940 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
5941 return FALSE;
5944 if (NO_OPD_RELOCS)
5946 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
5947 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
5948 return FALSE;
5951 if (relocs)
5953 if (!add_dynamic_entry (DT_RELA, 0)
5954 || !add_dynamic_entry (DT_RELASZ, 0)
5955 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
5956 return FALSE;
5958 /* If any dynamic relocs apply to a read-only section,
5959 then we need a DT_TEXTREL entry. */
5960 if ((info->flags & DF_TEXTREL) == 0)
5961 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
5962 (PTR) info);
5964 if ((info->flags & DF_TEXTREL) != 0)
5966 if (!add_dynamic_entry (DT_TEXTREL, 0))
5967 return FALSE;
5971 #undef add_dynamic_entry
5973 return TRUE;
5976 /* Determine the type of stub needed, if any, for a call. */
5978 static INLINE enum ppc_stub_type
5979 ppc_type_of_stub (input_sec, rel, hash, destination)
5980 asection *input_sec;
5981 const Elf_Internal_Rela *rel;
5982 struct ppc_link_hash_entry **hash;
5983 bfd_vma destination;
5985 struct ppc_link_hash_entry *h = *hash;
5986 bfd_vma location;
5987 bfd_vma branch_offset;
5988 bfd_vma max_branch_offset;
5989 unsigned int r_type;
5991 if (h != NULL)
5993 if (h->oh != NULL
5994 && h->oh->dynindx != -1)
5996 struct plt_entry *ent;
5997 for (ent = h->oh->plt.plist; ent != NULL; ent = ent->next)
5998 if (ent->addend == rel->r_addend
5999 && ent->plt.offset != (bfd_vma) -1)
6001 *hash = (struct ppc_link_hash_entry *) h->oh;
6002 return ppc_stub_plt_call;
6006 if (h->elf.root.type == bfd_link_hash_undefweak
6007 || h->elf.root.type == bfd_link_hash_undefined)
6008 return ppc_stub_none;
6011 /* Determine where the call point is. */
6012 location = (input_sec->output_offset
6013 + input_sec->output_section->vma
6014 + rel->r_offset);
6016 branch_offset = destination - location;
6017 r_type = ELF64_R_TYPE (rel->r_info);
6019 /* Determine if a long branch stub is needed. */
6020 max_branch_offset = 1 << 25;
6021 if (r_type != (unsigned int) R_PPC64_REL24)
6022 max_branch_offset = 1 << 15;
6024 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
6025 /* We need a stub. Figure out whether a long_branch or plt_branch
6026 is needed later. */
6027 return ppc_stub_long_branch;
6029 return ppc_stub_none;
6032 /* Build a .plt call stub. */
6034 static bfd_byte *
6035 build_plt_stub (obfd, p, offset, glink)
6036 bfd *obfd;
6037 bfd_byte *p;
6038 int offset;
6039 int glink;
6041 #define PPC_LO(v) ((v) & 0xffff)
6042 #define PPC_HI(v) (((v) >> 16) & 0xffff)
6043 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
6045 if (glink)
6046 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
6047 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
6048 if (!glink)
6049 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
6050 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6051 if (PPC_HA (offset + 8) != PPC_HA (offset))
6052 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
6053 offset += 8;
6054 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
6055 if (PPC_HA (offset + 8) != PPC_HA (offset))
6056 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
6057 offset += 8;
6058 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
6059 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
6060 bfd_put_32 (obfd, BCTR, p), p += 4;
6061 return p;
6064 static bfd_boolean
6065 ppc_build_one_stub (gen_entry, in_arg)
6066 struct bfd_hash_entry *gen_entry;
6067 PTR in_arg;
6069 struct ppc_stub_hash_entry *stub_entry;
6070 struct ppc_branch_hash_entry *br_entry;
6071 struct bfd_link_info *info;
6072 struct ppc_link_hash_table *htab;
6073 asection *stub_sec;
6074 bfd *stub_bfd;
6075 bfd_byte *loc;
6076 bfd_byte *p;
6077 unsigned int indx;
6078 struct plt_entry *ent;
6079 bfd_vma off;
6080 int size;
6082 /* Massage our args to the form they really have. */
6083 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6084 info = (struct bfd_link_info *) in_arg;
6086 htab = ppc_hash_table (info);
6087 stub_sec = stub_entry->stub_sec;
6089 /* Make a note of the offset within the stubs for this entry. */
6090 stub_entry->stub_offset = stub_sec->_cooked_size;
6091 loc = stub_sec->contents + stub_entry->stub_offset;
6093 stub_bfd = stub_sec->owner;
6095 switch (stub_entry->stub_type)
6097 case ppc_stub_long_branch:
6098 /* Branches are relative. This is where we are going to. */
6099 off = (stub_entry->target_value
6100 + stub_entry->target_section->output_offset
6101 + stub_entry->target_section->output_section->vma);
6103 /* And this is where we are coming from. */
6104 off -= (stub_entry->stub_offset
6105 + stub_sec->output_offset
6106 + stub_sec->output_section->vma);
6108 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
6110 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
6111 size = 4;
6112 break;
6114 case ppc_stub_plt_branch:
6115 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6116 stub_entry->root.string + 9,
6117 FALSE, FALSE);
6118 if (br_entry == NULL)
6120 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
6121 stub_entry->root.string + 9);
6122 htab->stub_error = TRUE;
6123 return FALSE;
6126 off = (stub_entry->target_value
6127 + stub_entry->target_section->output_offset
6128 + stub_entry->target_section->output_section->vma);
6130 bfd_put_64 (htab->sbrlt->owner, off,
6131 htab->sbrlt->contents + br_entry->offset);
6133 if (info->shared)
6135 /* Create a reloc for the branch lookup table entry. */
6136 Elf_Internal_Rela rela;
6137 bfd_byte *loc;
6139 rela.r_offset = (br_entry->offset
6140 + htab->sbrlt->output_offset
6141 + htab->sbrlt->output_section->vma);
6142 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6143 rela.r_addend = off;
6145 loc = htab->srelbrlt->contents;
6146 loc += htab->srelbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
6147 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, loc);
6150 off = (br_entry->offset
6151 + htab->sbrlt->output_offset
6152 + htab->sbrlt->output_section->vma
6153 - elf_gp (htab->sbrlt->output_section->owner)
6154 - TOC_BASE_OFF);
6156 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
6158 (*_bfd_error_handler)
6159 (_("linkage table error against `%s'"),
6160 stub_entry->root.string);
6161 bfd_set_error (bfd_error_bad_value);
6162 htab->stub_error = TRUE;
6163 return FALSE;
6166 indx = off;
6167 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
6168 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
6169 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
6170 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
6171 size = 16;
6172 break;
6174 case ppc_stub_plt_call:
6175 /* Do the best we can for shared libraries built without
6176 exporting ".foo" for each "foo". This can happen when symbol
6177 versioning scripts strip all bar a subset of symbols. */
6178 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
6179 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
6181 /* Point the symbol at the stub. There may be multiple stubs,
6182 we don't really care; The main thing is to make this sym
6183 defined somewhere. */
6184 stub_entry->h->oh->root.type = bfd_link_hash_defined;
6185 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
6186 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
6189 /* Now build the stub. */
6190 off = (bfd_vma) -1;
6191 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6192 if (ent->addend == stub_entry->addend)
6194 off = ent->plt.offset;
6195 break;
6197 if (off >= (bfd_vma) -2)
6198 abort ();
6200 off &= ~ (bfd_vma) 1;
6201 off += (htab->splt->output_offset
6202 + htab->splt->output_section->vma
6203 - elf_gp (htab->splt->output_section->owner)
6204 - TOC_BASE_OFF);
6206 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
6208 (*_bfd_error_handler)
6209 (_("linkage table error against `%s'"),
6210 stub_entry->h->elf.root.root.string);
6211 bfd_set_error (bfd_error_bad_value);
6212 htab->stub_error = TRUE;
6213 return FALSE;
6216 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
6217 size = p - loc;
6218 break;
6220 default:
6221 BFD_FAIL ();
6222 return FALSE;
6225 stub_sec->_cooked_size += size;
6226 return TRUE;
6229 /* As above, but don't actually build the stub. Just bump offset so
6230 we know stub section sizes, and select plt_branch stubs where
6231 long_branch stubs won't do. */
6233 static bfd_boolean
6234 ppc_size_one_stub (gen_entry, in_arg)
6235 struct bfd_hash_entry *gen_entry;
6236 PTR in_arg;
6238 struct ppc_stub_hash_entry *stub_entry;
6239 struct ppc_link_hash_table *htab;
6240 bfd_vma off;
6241 int size;
6243 /* Massage our args to the form they really have. */
6244 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
6245 htab = (struct ppc_link_hash_table *) in_arg;
6247 if (stub_entry->stub_type == ppc_stub_plt_call)
6249 struct plt_entry *ent;
6250 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
6251 if (ent->addend == stub_entry->addend)
6253 off = ent->plt.offset & ~(bfd_vma) 1;
6254 break;
6256 if (ent == NULL)
6257 abort ();
6258 off += (htab->splt->output_offset
6259 + htab->splt->output_section->vma
6260 - elf_gp (htab->splt->output_section->owner)
6261 - TOC_BASE_OFF);
6263 size = 28;
6264 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
6265 size += 4;
6267 else
6269 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
6270 stub_entry->stub_type = ppc_stub_long_branch;
6271 size = 4;
6273 off = (stub_entry->target_value
6274 + stub_entry->target_section->output_offset
6275 + stub_entry->target_section->output_section->vma);
6276 off -= (stub_entry->stub_sec->_raw_size
6277 + stub_entry->stub_sec->output_offset
6278 + stub_entry->stub_sec->output_section->vma);
6280 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
6282 struct ppc_branch_hash_entry *br_entry;
6284 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
6285 stub_entry->root.string + 9,
6286 TRUE, FALSE);
6287 if (br_entry == NULL)
6289 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
6290 stub_entry->root.string + 9);
6291 htab->stub_error = TRUE;
6292 return FALSE;
6295 if (br_entry->iter != htab->stub_iteration)
6297 br_entry->iter = htab->stub_iteration;
6298 br_entry->offset = htab->sbrlt->_raw_size;
6299 htab->sbrlt->_raw_size += 8;
6301 stub_entry->stub_type = ppc_stub_plt_branch;
6302 size = 16;
6306 stub_entry->stub_sec->_raw_size += size;
6307 return TRUE;
6310 /* Set up various things so that we can make a list of input sections
6311 for each output section included in the link. Returns -1 on error,
6312 0 when no stubs will be needed, and 1 on success. */
6315 ppc64_elf_setup_section_lists (output_bfd, info)
6316 bfd *output_bfd;
6317 struct bfd_link_info *info;
6319 bfd *input_bfd;
6320 int top_id, top_index;
6321 asection *section;
6322 asection **input_list, **list;
6323 bfd_size_type amt;
6324 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6326 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
6327 || htab->sbrlt == NULL)
6328 return 0;
6330 /* Find the top input section id. */
6331 for (input_bfd = info->input_bfds, top_id = 0;
6332 input_bfd != NULL;
6333 input_bfd = input_bfd->link_next)
6335 for (section = input_bfd->sections;
6336 section != NULL;
6337 section = section->next)
6339 if (top_id < section->id)
6340 top_id = section->id;
6344 amt = sizeof (struct map_stub) * (top_id + 1);
6345 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
6346 if (htab->stub_group == NULL)
6347 return -1;
6349 /* We can't use output_bfd->section_count here to find the top output
6350 section index as some sections may have been removed, and
6351 _bfd_strip_section_from_output doesn't renumber the indices. */
6352 for (section = output_bfd->sections, top_index = 0;
6353 section != NULL;
6354 section = section->next)
6356 if (top_index < section->index)
6357 top_index = section->index;
6360 htab->top_index = top_index;
6361 amt = sizeof (asection *) * (top_index + 1);
6362 input_list = (asection **) bfd_malloc (amt);
6363 htab->input_list = input_list;
6364 if (input_list == NULL)
6365 return -1;
6367 /* For sections we aren't interested in, mark their entries with a
6368 value we can check later. */
6369 list = input_list + top_index;
6371 *list = bfd_abs_section_ptr;
6372 while (list-- != input_list);
6374 for (section = output_bfd->sections;
6375 section != NULL;
6376 section = section->next)
6378 if ((section->flags & SEC_CODE) != 0)
6379 input_list[section->index] = NULL;
6382 return 1;
6385 /* The linker repeatedly calls this function for each input section,
6386 in the order that input sections are linked into output sections.
6387 Build lists of input sections to determine groupings between which
6388 we may insert linker stubs. */
6390 void
6391 ppc64_elf_next_input_section (info, isec)
6392 struct bfd_link_info *info;
6393 asection *isec;
6395 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6397 if (isec->output_section->index <= htab->top_index)
6399 asection **list = htab->input_list + isec->output_section->index;
6400 if (*list != bfd_abs_section_ptr)
6402 /* Steal the link_sec pointer for our list. */
6403 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
6404 /* This happens to make the list in reverse order,
6405 which is what we want. */
6406 PREV_SEC (isec) = *list;
6407 *list = isec;
6412 /* See whether we can group stub sections together. Grouping stub
6413 sections may result in fewer stubs. More importantly, we need to
6414 put all .init* and .fini* stubs at the beginning of the .init or
6415 .fini output sections respectively, because glibc splits the
6416 _init and _fini functions into multiple parts. Putting a stub in
6417 the middle of a function is not a good idea. */
6419 static void
6420 group_sections (htab, stub_group_size, stubs_always_before_branch)
6421 struct ppc_link_hash_table *htab;
6422 bfd_size_type stub_group_size;
6423 bfd_boolean stubs_always_before_branch;
6425 asection **list = htab->input_list + htab->top_index;
6428 asection *tail = *list;
6429 if (tail == bfd_abs_section_ptr)
6430 continue;
6431 while (tail != NULL)
6433 asection *curr;
6434 asection *prev;
6435 bfd_size_type total;
6437 curr = tail;
6438 if (tail->_cooked_size)
6439 total = tail->_cooked_size;
6440 else
6441 total = tail->_raw_size;
6442 while ((prev = PREV_SEC (curr)) != NULL
6443 && ((total += curr->output_offset - prev->output_offset)
6444 < stub_group_size))
6445 curr = prev;
6447 /* OK, the size from the start of CURR to the end is less
6448 than stub_group_size and thus can be handled by one stub
6449 section. (or the tail section is itself larger than
6450 stub_group_size, in which case we may be toast.) We
6451 should really be keeping track of the total size of stubs
6452 added here, as stubs contribute to the final output
6453 section size. That's a little tricky, and this way will
6454 only break if stubs added make the total size more than
6455 2^25, ie. for the default stub_group_size, if stubs total
6456 more than 2834432 bytes, or over 100000 plt call stubs. */
6459 prev = PREV_SEC (tail);
6460 /* Set up this stub group. */
6461 htab->stub_group[tail->id].link_sec = curr;
6463 while (tail != curr && (tail = prev) != NULL);
6465 /* But wait, there's more! Input sections up to stub_group_size
6466 bytes before the stub section can be handled by it too. */
6467 if (!stubs_always_before_branch)
6469 total = 0;
6470 while (prev != NULL
6471 && ((total += tail->output_offset - prev->output_offset)
6472 < stub_group_size))
6474 tail = prev;
6475 prev = PREV_SEC (tail);
6476 htab->stub_group[tail->id].link_sec = curr;
6479 tail = prev;
6482 while (list-- != htab->input_list);
6483 free (htab->input_list);
6484 #undef PREV_SEC
6487 /* Determine and set the size of the stub section for a final link.
6489 The basic idea here is to examine all the relocations looking for
6490 PC-relative calls to a target that is unreachable with a "bl"
6491 instruction. */
6493 bfd_boolean
6494 ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
6495 add_stub_section, layout_sections_again)
6496 bfd *output_bfd;
6497 bfd *stub_bfd;
6498 struct bfd_link_info *info;
6499 bfd_signed_vma group_size;
6500 asection * (*add_stub_section) PARAMS ((const char *, asection *));
6501 void (*layout_sections_again) PARAMS ((void));
6503 bfd_size_type stub_group_size;
6504 bfd_boolean stubs_always_before_branch;
6505 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6507 /* Stash our params away. */
6508 htab->stub_bfd = stub_bfd;
6509 htab->add_stub_section = add_stub_section;
6510 htab->layout_sections_again = layout_sections_again;
6511 stubs_always_before_branch = group_size < 0;
6512 if (group_size < 0)
6513 stub_group_size = -group_size;
6514 else
6515 stub_group_size = group_size;
6516 if (stub_group_size == 1)
6518 /* Default values. */
6519 stub_group_size = 30720000;
6520 if (htab->has_14bit_branch)
6521 stub_group_size = 30000;
6524 group_sections (htab, stub_group_size, stubs_always_before_branch);
6526 while (1)
6528 bfd *input_bfd;
6529 unsigned int bfd_indx;
6530 asection *stub_sec;
6531 bfd_boolean stub_changed;
6533 htab->stub_iteration += 1;
6534 stub_changed = FALSE;
6536 for (input_bfd = info->input_bfds, bfd_indx = 0;
6537 input_bfd != NULL;
6538 input_bfd = input_bfd->link_next, bfd_indx++)
6540 Elf_Internal_Shdr *symtab_hdr;
6541 asection *section;
6542 Elf_Internal_Sym *local_syms = NULL;
6544 /* We'll need the symbol table in a second. */
6545 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6546 if (symtab_hdr->sh_info == 0)
6547 continue;
6549 /* Walk over each section attached to the input bfd. */
6550 for (section = input_bfd->sections;
6551 section != NULL;
6552 section = section->next)
6554 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
6556 /* If there aren't any relocs, then there's nothing more
6557 to do. */
6558 if ((section->flags & SEC_RELOC) == 0
6559 || section->reloc_count == 0)
6560 continue;
6562 /* If this section is a link-once section that will be
6563 discarded, then don't create any stubs. */
6564 if (section->output_section == NULL
6565 || section->output_section->owner != output_bfd)
6566 continue;
6568 /* Get the relocs. */
6569 internal_relocs
6570 = _bfd_elf64_link_read_relocs (input_bfd, section, NULL,
6571 (Elf_Internal_Rela *) NULL,
6572 info->keep_memory);
6573 if (internal_relocs == NULL)
6574 goto error_ret_free_local;
6576 /* Now examine each relocation. */
6577 irela = internal_relocs;
6578 irelaend = irela + section->reloc_count;
6579 for (; irela < irelaend; irela++)
6581 unsigned int r_type, r_indx;
6582 enum ppc_stub_type stub_type;
6583 struct ppc_stub_hash_entry *stub_entry;
6584 asection *sym_sec;
6585 bfd_vma sym_value;
6586 bfd_vma destination;
6587 struct ppc_link_hash_entry *hash;
6588 struct elf_link_hash_entry *h;
6589 Elf_Internal_Sym *sym;
6590 char *stub_name;
6591 const asection *id_sec;
6593 r_type = ELF64_R_TYPE (irela->r_info);
6594 r_indx = ELF64_R_SYM (irela->r_info);
6596 if (r_type >= (unsigned int) R_PPC64_max)
6598 bfd_set_error (bfd_error_bad_value);
6599 goto error_ret_free_internal;
6602 /* Only look for stubs on branch instructions. */
6603 if (r_type != (unsigned int) R_PPC64_REL24
6604 && r_type != (unsigned int) R_PPC64_REL14
6605 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
6606 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
6607 continue;
6609 /* Now determine the call target, its name, value,
6610 section. */
6611 destination = 0;
6612 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6613 r_indx, input_bfd))
6614 goto error_ret_free_internal;
6615 hash = (struct ppc_link_hash_entry *) h;
6617 if (hash == NULL)
6619 /* It's a local symbol. */
6620 sym_value = sym->st_value;
6621 destination = (sym_value + irela->r_addend
6622 + sym_sec->output_offset
6623 + sym_sec->output_section->vma);
6625 else
6627 /* It's an external symbol. */
6628 sym_value = 0;
6629 if (hash->elf.root.type == bfd_link_hash_defined
6630 || hash->elf.root.type == bfd_link_hash_defweak)
6632 sym_value = hash->elf.root.u.def.value;
6633 if (sym_sec->output_section != NULL)
6634 destination = (sym_value + irela->r_addend
6635 + sym_sec->output_offset
6636 + sym_sec->output_section->vma);
6638 else if (hash->elf.root.type == bfd_link_hash_undefweak)
6640 else if (hash->elf.root.type == bfd_link_hash_undefined)
6642 else
6644 bfd_set_error (bfd_error_bad_value);
6645 goto error_ret_free_internal;
6649 /* Determine what (if any) linker stub is needed. */
6650 stub_type = ppc_type_of_stub (section, irela, &hash,
6651 destination);
6652 if (stub_type == ppc_stub_none)
6653 continue;
6655 /* __tls_get_addr calls might be eliminated. */
6656 if (stub_type != ppc_stub_plt_call
6657 && hash != NULL
6658 && &hash->elf == htab->tls_get_addr
6659 && section->has_tls_reloc
6660 && irela != internal_relocs)
6662 /* Get tls info. */
6663 char *tls_type;
6665 if (!get_tls_type (&tls_type, &local_syms,
6666 irela - 1, input_bfd))
6667 goto error_ret_free_internal;
6668 if (*tls_type != 0)
6669 continue;
6672 /* Support for grouping stub sections. */
6673 id_sec = htab->stub_group[section->id].link_sec;
6675 /* Get the name of this stub. */
6676 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
6677 if (!stub_name)
6678 goto error_ret_free_internal;
6680 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
6681 stub_name, FALSE, FALSE);
6682 if (stub_entry != NULL)
6684 /* The proper stub has already been created. */
6685 free (stub_name);
6686 continue;
6689 stub_entry = ppc_add_stub (stub_name, section, htab);
6690 if (stub_entry == NULL)
6692 free (stub_name);
6693 error_ret_free_internal:
6694 if (elf_section_data (section)->relocs == NULL)
6695 free (internal_relocs);
6696 error_ret_free_local:
6697 if (local_syms != NULL
6698 && (symtab_hdr->contents
6699 != (unsigned char *) local_syms))
6700 free (local_syms);
6701 return FALSE;
6704 stub_entry->target_value = sym_value;
6705 stub_entry->target_section = sym_sec;
6706 stub_entry->stub_type = stub_type;
6707 stub_entry->h = hash;
6708 stub_entry->addend = irela->r_addend;
6709 stub_changed = TRUE;
6712 /* We're done with the internal relocs, free them. */
6713 if (elf_section_data (section)->relocs != internal_relocs)
6714 free (internal_relocs);
6717 if (local_syms != NULL
6718 && symtab_hdr->contents != (unsigned char *) local_syms)
6720 if (!info->keep_memory)
6721 free (local_syms);
6722 else
6723 symtab_hdr->contents = (unsigned char *) local_syms;
6727 if (!stub_changed)
6728 break;
6730 /* OK, we've added some stubs. Find out the new size of the
6731 stub sections. */
6732 for (stub_sec = htab->stub_bfd->sections;
6733 stub_sec != NULL;
6734 stub_sec = stub_sec->next)
6736 stub_sec->_raw_size = 0;
6737 stub_sec->_cooked_size = 0;
6739 htab->sbrlt->_raw_size = 0;
6740 htab->sbrlt->_cooked_size = 0;
6742 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
6744 /* Ask the linker to do its stuff. */
6745 (*htab->layout_sections_again) ();
6748 /* It would be nice to strip .branch_lt from the output if the
6749 section is empty, but it's too late. If we strip sections here,
6750 the dynamic symbol table is corrupted since the section symbol
6751 for the stripped section isn't written. */
6753 return TRUE;
6756 /* Called after we have determined section placement. If sections
6757 move, we'll be called again. Provide a value for TOCstart. */
6759 bfd_vma
6760 ppc64_elf_toc (obfd)
6761 bfd *obfd;
6763 asection *s;
6764 bfd_vma TOCstart;
6766 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
6767 order. The TOC starts where the first of these sections starts. */
6768 s = bfd_get_section_by_name (obfd, ".got");
6769 if (s == NULL)
6770 s = bfd_get_section_by_name (obfd, ".toc");
6771 if (s == NULL)
6772 s = bfd_get_section_by_name (obfd, ".tocbss");
6773 if (s == NULL)
6774 s = bfd_get_section_by_name (obfd, ".plt");
6775 if (s == NULL)
6777 /* This may happen for
6778 o references to TOC base (SYM@toc / TOC[tc0]) without a
6779 .toc directive
6780 o bad linker script
6781 o --gc-sections and empty TOC sections
6783 FIXME: Warn user? */
6785 /* Look for a likely section. We probably won't even be
6786 using TOCstart. */
6787 for (s = obfd->sections; s != NULL; s = s->next)
6788 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
6789 == (SEC_ALLOC | SEC_SMALL_DATA))
6790 break;
6791 if (s == NULL)
6792 for (s = obfd->sections; s != NULL; s = s->next)
6793 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
6794 == (SEC_ALLOC | SEC_SMALL_DATA))
6795 break;
6796 if (s == NULL)
6797 for (s = obfd->sections; s != NULL; s = s->next)
6798 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
6799 break;
6800 if (s == NULL)
6801 for (s = obfd->sections; s != NULL; s = s->next)
6802 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
6803 break;
6806 TOCstart = 0;
6807 if (s != NULL)
6808 TOCstart = s->output_section->vma + s->output_offset;
6810 return TOCstart;
6813 /* Build all the stubs associated with the current output file.
6814 The stubs are kept in a hash table attached to the main linker
6815 hash table. This function is called via gldelf64ppc_finish. */
6817 bfd_boolean
6818 ppc64_elf_build_stubs (info)
6819 struct bfd_link_info *info;
6821 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6822 asection *stub_sec;
6823 bfd_vma plt_r2;
6824 bfd_byte *p;
6826 for (stub_sec = htab->stub_bfd->sections;
6827 stub_sec != NULL;
6828 stub_sec = stub_sec->next)
6830 bfd_size_type size;
6832 /* Allocate memory to hold the linker stubs. */
6833 size = stub_sec->_raw_size;
6834 if (size != 0)
6836 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
6837 if (stub_sec->contents == NULL)
6838 return FALSE;
6840 stub_sec->_cooked_size = 0;
6843 if (htab->splt != NULL)
6845 unsigned int indx;
6847 /* Build the .glink plt call stub. */
6848 plt_r2 = (htab->splt->output_offset
6849 + htab->splt->output_section->vma
6850 - elf_gp (htab->splt->output_section->owner)
6851 - TOC_BASE_OFF);
6852 p = htab->sglink->contents;
6853 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
6854 while (p < htab->sglink->contents + GLINK_CALL_STUB_SIZE)
6856 bfd_put_32 (htab->sglink->owner, NOP, p);
6857 p += 4;
6860 /* Build the .glink lazy link call stubs. */
6861 indx = 0;
6862 while (p < htab->sglink->contents + htab->sglink->_raw_size)
6864 if (indx < 0x8000)
6866 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
6867 p += 4;
6869 else
6871 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
6872 p += 4;
6873 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
6874 p += 4;
6876 bfd_put_32 (htab->sglink->owner,
6877 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
6878 indx++;
6879 p += 4;
6881 htab->sglink->_cooked_size = p - htab->sglink->contents;
6884 if (htab->sbrlt->_raw_size != 0)
6886 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
6887 htab->sbrlt->_raw_size);
6888 if (htab->sbrlt->contents == NULL)
6889 return FALSE;
6892 /* Build the stubs as directed by the stub hash table. */
6893 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
6895 for (stub_sec = htab->stub_bfd->sections;
6896 stub_sec != NULL;
6897 stub_sec = stub_sec->next)
6899 if (stub_sec->_raw_size != stub_sec->_cooked_size)
6900 break;
6903 if (stub_sec != NULL
6904 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
6906 htab->stub_error = TRUE;
6907 (*_bfd_error_handler) (_("stubs don't match calculated size"));
6910 return !htab->stub_error;
6913 /* The RELOCATE_SECTION function is called by the ELF backend linker
6914 to handle the relocations for a section.
6916 The relocs are always passed as Rela structures; if the section
6917 actually uses Rel structures, the r_addend field will always be
6918 zero.
6920 This function is responsible for adjust the section contents as
6921 necessary, and (if using Rela relocs and generating a
6922 relocateable output file) adjusting the reloc addend as
6923 necessary.
6925 This function does not have to worry about setting the reloc
6926 address or the reloc symbol index.
6928 LOCAL_SYMS is a pointer to the swapped in local symbols.
6930 LOCAL_SECTIONS is an array giving the section in the input file
6931 corresponding to the st_shndx field of each local symbol.
6933 The global hash table entry for the global symbols can be found
6934 via elf_sym_hashes (input_bfd).
6936 When generating relocateable output, this function must handle
6937 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
6938 going to be the section symbol corresponding to the output
6939 section, which means that the addend must be adjusted
6940 accordingly. */
6942 static bfd_boolean
6943 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
6944 contents, relocs, local_syms, local_sections)
6945 bfd *output_bfd;
6946 struct bfd_link_info *info;
6947 bfd *input_bfd;
6948 asection *input_section;
6949 bfd_byte *contents;
6950 Elf_Internal_Rela *relocs;
6951 Elf_Internal_Sym *local_syms;
6952 asection **local_sections;
6954 struct ppc_link_hash_table *htab;
6955 Elf_Internal_Shdr *symtab_hdr;
6956 struct elf_link_hash_entry **sym_hashes;
6957 Elf_Internal_Rela *rel;
6958 Elf_Internal_Rela *relend;
6959 Elf_Internal_Rela outrel;
6960 bfd_byte *loc;
6961 struct got_entry **local_got_ents;
6962 bfd_vma TOCstart;
6963 bfd_boolean ret = TRUE;
6964 bfd_boolean is_opd;
6965 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
6966 bfd_boolean is_power4 = FALSE;
6968 if (info->relocateable)
6969 return TRUE;
6971 /* Initialize howto table if needed. */
6972 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
6973 ppc_howto_init ();
6975 htab = ppc_hash_table (info);
6976 if (info->shared && (htab->tlsld_got.offset & 1) == 0)
6978 outrel.r_offset = (htab->sgot->output_section->vma
6979 + htab->sgot->output_offset
6980 + htab->tlsld_got.offset);
6981 outrel.r_info = ELF64_R_INFO (0, R_PPC64_DTPMOD64);
6982 outrel.r_addend = 0;
6984 loc = htab->srelgot->contents;
6985 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
6986 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
6987 htab->tlsld_got.offset |= 1;
6990 local_got_ents = elf_local_got_ents (input_bfd);
6991 TOCstart = elf_gp (output_bfd);
6992 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
6993 sym_hashes = elf_sym_hashes (input_bfd);
6994 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
6996 rel = relocs;
6997 relend = relocs + input_section->reloc_count;
6998 for (; rel < relend; rel++)
7000 enum elf_ppc64_reloc_type r_type;
7001 bfd_vma addend;
7002 bfd_reloc_status_type r;
7003 Elf_Internal_Sym *sym;
7004 asection *sec;
7005 struct elf_link_hash_entry *h;
7006 struct elf_link_hash_entry *fdh;
7007 const char *sym_name;
7008 unsigned long r_symndx;
7009 char tls_mask, tls_gd, tls_type;
7010 bfd_vma relocation;
7011 bfd_boolean unresolved_reloc;
7012 bfd_boolean warned;
7013 long insn;
7014 struct ppc_stub_hash_entry *stub_entry;
7015 bfd_vma max_br_offset;
7016 bfd_vma from;
7018 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
7019 r_symndx = ELF64_R_SYM (rel->r_info);
7020 r = bfd_reloc_other;
7021 sym = (Elf_Internal_Sym *) 0;
7022 sec = (asection *) 0;
7023 h = (struct elf_link_hash_entry *) 0;
7024 sym_name = (const char *) 0;
7025 unresolved_reloc = FALSE;
7026 warned = FALSE;
7028 if (r_type == R_PPC64_TOC)
7030 /* Relocation value is TOC base. Symbol is ignored. */
7031 relocation = TOCstart + TOC_BASE_OFF;
7033 else if (r_symndx < symtab_hdr->sh_info)
7035 /* It's a local symbol. */
7036 sym = local_syms + r_symndx;
7037 sec = local_sections[r_symndx];
7038 sym_name = bfd_elf_local_sym_name (input_bfd, sym);
7039 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
7040 if (elf_section_data (sec) != NULL)
7042 long *opd_sym_adjust;
7044 opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
7045 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
7046 relocation += opd_sym_adjust[sym->st_value / 24];
7049 else
7051 /* It's a global symbol. */
7052 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7053 while (h->root.type == bfd_link_hash_indirect
7054 || h->root.type == bfd_link_hash_warning)
7055 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7056 sym_name = h->root.root.string;
7057 relocation = 0;
7058 if (h->root.type == bfd_link_hash_defined
7059 || h->root.type == bfd_link_hash_defweak)
7061 sec = h->root.u.def.section;
7062 if (sec->output_section == NULL)
7063 /* Set a flag that will be cleared later if we find a
7064 relocation value for this symbol. output_section
7065 is typically NULL for symbols satisfied by a shared
7066 library. */
7067 unresolved_reloc = TRUE;
7068 else
7069 relocation = (h->root.u.def.value
7070 + sec->output_section->vma
7071 + sec->output_offset);
7073 else if (h->root.type == bfd_link_hash_undefweak)
7075 else if (info->shared
7076 && (!info->symbolic || info->allow_shlib_undefined)
7077 && !info->no_undefined
7078 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
7080 else
7082 if (! ((*info->callbacks->undefined_symbol)
7083 (info, h->root.root.string, input_bfd, input_section,
7084 rel->r_offset, (!info->shared
7085 || info->no_undefined
7086 || ELF_ST_VISIBILITY (h->other)))))
7087 return FALSE;
7088 warned = TRUE;
7092 /* TLS optimizations. Replace instruction sequences and relocs
7093 based on information we collected in tls_optimize. We edit
7094 RELOCS so that --emit-relocs will output something sensible
7095 for the final instruction stream. */
7096 tls_mask = 0;
7097 tls_gd = 0;
7098 if (IS_TLS_RELOC (r_type))
7100 if (h != NULL)
7101 tls_mask = ((struct ppc_link_hash_entry *) h)->tls_type;
7102 else if (local_got_ents != NULL)
7104 char *lgot_types;
7105 lgot_types = (char *) (local_got_ents + symtab_hdr->sh_info);
7106 tls_mask = lgot_types[r_symndx];
7110 /* Ensure reloc mapping code below stays sane. */
7111 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
7112 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
7113 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
7114 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
7115 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
7116 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
7117 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
7118 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
7119 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
7120 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
7121 abort ();
7122 switch (r_type)
7124 default:
7125 break;
7127 case R_PPC64_TOC16:
7128 case R_PPC64_TOC16_LO:
7129 case R_PPC64_TOC16_DS:
7130 case R_PPC64_TOC16_LO_DS:
7132 /* Check for toc tls entries. */
7133 char *toc_tls;
7134 int retval;
7136 retval = get_tls_type (&toc_tls, &local_syms, rel, input_bfd);
7137 if (retval == 0)
7138 return FALSE;
7140 if (toc_tls)
7142 tls_mask = *toc_tls;
7143 if (r_type == R_PPC64_TOC16_DS
7144 || r_type == R_PPC64_TOC16_LO_DS)
7145 goto toctprel;
7146 else
7148 /* If we found a GD reloc pair, then we might be
7149 doing a GD->IE transition. */
7150 if (retval == 2)
7152 tls_gd = TLS_TPRELGD;
7153 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7154 goto tls_get_addr_check;
7156 else if (retval == 3)
7158 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7159 goto tls_get_addr_check;
7164 break;
7166 case R_PPC64_GOT_TPREL16_DS:
7167 case R_PPC64_GOT_TPREL16_LO_DS:
7168 toctprel:
7169 if (tls_mask != 0
7170 && (tls_mask & TLS_TPREL) == 0)
7172 bfd_vma insn;
7173 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
7174 insn &= 31 << 21;
7175 insn |= 0x3c0d0000; /* addis 0,13,0 */
7176 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
7177 r_type = R_PPC64_TPREL16_HA;
7178 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7180 break;
7182 case R_PPC64_TLS:
7183 if (tls_mask == 0)
7185 /* Check for toc tls entries. */
7186 char *toc_tls;
7188 if (!get_tls_type (&toc_tls, &local_syms, rel, input_bfd))
7189 return FALSE;
7191 if (toc_tls)
7192 tls_mask = *toc_tls;
7194 if (tls_mask != 0
7195 && (tls_mask & TLS_TPREL) == 0)
7197 bfd_vma insn, rtra;
7198 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
7199 if ((insn & ((31 << 26) | (31 << 11)))
7200 == ((31 << 26) | (13 << 11)))
7201 rtra = insn & ((1 << 26) - (1 << 16));
7202 else if ((insn & ((31 << 26) | (31 << 16)))
7203 == ((31 << 26) | (13 << 16)))
7204 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
7205 else
7206 abort ();
7207 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
7208 /* add -> addi. */
7209 insn = 14 << 26;
7210 else if ((insn & (31 << 1)) == 23 << 1
7211 && ((insn & (31 << 6)) < 14 << 6
7212 || ((insn & (31 << 6)) >= 16 << 6
7213 && (insn & (31 << 6)) < 24 << 6)))
7214 /* load and store indexed -> dform. */
7215 insn = (32 | ((insn >> 6) & 31)) << 26;
7216 else if ((insn & (31 << 1)) == 21 << 1
7217 && (insn & (0x1a << 6)) == 0)
7218 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
7219 insn = (((58 | ((insn >> 6) & 4)) << 26)
7220 | ((insn >> 6) & 1));
7221 else if ((insn & (31 << 1)) == 21 << 1
7222 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
7223 /* lwax -> lwa. */
7224 insn = (58 << 26) | 2;
7225 else
7226 abort ();
7227 insn |= rtra;
7228 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
7229 r_type = R_PPC64_TPREL16_LO;
7230 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7231 /* Was PPC64_TLS which sits on insn boundary, now
7232 PPC64_TPREL16_LO which is at insn+2. */
7233 rel->r_offset += 2;
7235 break;
7237 case R_PPC64_GOT_TLSGD16_HI:
7238 case R_PPC64_GOT_TLSGD16_HA:
7239 tls_gd = TLS_TPRELGD;
7240 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7241 goto tls_gdld_hi;
7242 break;
7244 case R_PPC64_GOT_TLSLD16_HI:
7245 case R_PPC64_GOT_TLSLD16_HA:
7246 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7248 tls_gdld_hi:
7249 if ((tls_mask & tls_gd) != 0)
7250 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7251 + R_PPC64_GOT_TPREL16_DS);
7252 else
7254 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
7255 rel->r_offset -= 2;
7256 r_type = R_PPC64_NONE;
7258 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7260 break;
7262 case R_PPC64_GOT_TLSGD16:
7263 case R_PPC64_GOT_TLSGD16_LO:
7264 tls_gd = TLS_TPRELGD;
7265 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
7266 goto tls_get_addr_check;
7267 break;
7269 case R_PPC64_GOT_TLSLD16:
7270 case R_PPC64_GOT_TLSLD16_LO:
7271 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
7273 tls_get_addr_check:
7274 if (rel + 1 < relend)
7276 enum elf_ppc64_reloc_type r_type2;
7277 unsigned long r_symndx2;
7278 struct elf_link_hash_entry *h2;
7279 bfd_vma insn1, insn2, insn3;
7280 bfd_vma offset;
7282 /* The next instruction should be a call to
7283 __tls_get_addr. Peek at the reloc to be sure. */
7284 r_type2
7285 = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel[1].r_info);
7286 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
7287 if (r_symndx2 < symtab_hdr->sh_info
7288 || (r_type2 != R_PPC64_REL14
7289 && r_type2 != R_PPC64_REL14_BRTAKEN
7290 && r_type2 != R_PPC64_REL14_BRNTAKEN
7291 && r_type2 != R_PPC64_REL24))
7292 break;
7294 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
7295 while (h2->root.type == bfd_link_hash_indirect
7296 || h2->root.type == bfd_link_hash_warning)
7297 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
7298 if (h2 == NULL || h2 != htab->tls_get_addr)
7299 break;
7301 /* OK, it checks out. Replace the call. */
7302 offset = rel[1].r_offset;
7303 insn1 = bfd_get_32 (output_bfd,
7304 contents + rel->r_offset - 2);
7305 insn3 = bfd_get_32 (output_bfd,
7306 contents + offset + 4);
7307 if ((tls_mask & tls_gd) != 0)
7309 /* IE */
7310 insn1 &= (1 << 26) - (1 << 2);
7311 insn1 |= 58 << 26; /* ld */
7312 insn2 = 0x7c636a14; /* add 3,3,13 */
7313 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
7314 if ((tls_mask & TLS_EXPLICIT) == 0)
7315 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
7316 + R_PPC64_GOT_TPREL16_DS);
7317 else
7318 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
7319 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7321 else
7323 /* LE */
7324 insn1 = 0x3c6d0000; /* addis 3,13,0 */
7325 insn2 = 0x38630000; /* addi 3,3,0 */
7326 if (tls_gd == 0)
7328 /* Was an LD reloc. */
7329 r_symndx = 0;
7330 rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
7331 rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
7333 r_type = R_PPC64_TPREL16_HA;
7334 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7335 rel[1].r_info = ELF64_R_INFO (r_symndx,
7336 R_PPC64_TPREL16_LO);
7337 rel[1].r_offset += 2;
7339 if (insn3 == NOP
7340 || insn3 == CROR_151515 || insn3 == CROR_313131)
7342 insn3 = insn2;
7343 insn2 = NOP;
7344 rel[1].r_offset += 4;
7346 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
7347 bfd_put_32 (output_bfd, insn2, contents + offset);
7348 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
7349 if (tls_gd == 0)
7351 /* We changed the symbol on an LD reloc. Start over
7352 in order to get h, sym, sec etc. right. */
7353 rel--;
7354 continue;
7358 break;
7360 case R_PPC64_DTPMOD64:
7361 if (rel + 1 < relend
7362 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
7363 && rel[1].r_offset == rel->r_offset + 8)
7365 if ((tls_mask & TLS_GD) == 0)
7367 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
7368 if ((tls_mask & TLS_TPRELGD) != 0)
7369 r_type = R_PPC64_TPREL64;
7370 else
7372 bfd_put_64 (output_bfd, (bfd_vma) 1,
7373 contents + rel->r_offset);
7374 r_type = R_PPC64_NONE;
7376 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7379 else
7381 if ((tls_mask & TLS_LD) == 0)
7383 bfd_put_64 (output_bfd, (bfd_vma) 1,
7384 contents + rel->r_offset);
7385 r_type = R_PPC64_NONE;
7386 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7389 break;
7391 case R_PPC64_TPREL64:
7392 if ((tls_mask & TLS_TPREL) == 0)
7394 r_type = R_PPC64_NONE;
7395 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
7397 break;
7400 /* Handle other relocations that tweak non-addend part of insn. */
7401 insn = 0;
7402 switch (r_type)
7404 default:
7405 break;
7407 /* Branch taken prediction relocations. */
7408 case R_PPC64_ADDR14_BRTAKEN:
7409 case R_PPC64_REL14_BRTAKEN:
7410 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
7411 /* Fall thru. */
7413 /* Branch not taken prediction relocations. */
7414 case R_PPC64_ADDR14_BRNTAKEN:
7415 case R_PPC64_REL14_BRNTAKEN:
7416 insn |= bfd_get_32 (output_bfd,
7417 contents + rel->r_offset) & ~(0x01 << 21);
7418 if (is_power4)
7420 /* Set 'a' bit. This is 0b00010 in BO field for branch
7421 on CR(BI) insns (BO == 001at or 011at), and 0b01000
7422 for branch on CTR insns (BO == 1a00t or 1a01t). */
7423 if ((insn & (0x14 << 21)) == (0x04 << 21))
7424 insn |= 0x02 << 21;
7425 else if ((insn & (0x14 << 21)) == (0x10 << 21))
7426 insn |= 0x08 << 21;
7427 else
7428 break;
7430 else
7432 from = (rel->r_offset
7433 + input_section->output_offset
7434 + input_section->output_section->vma);
7436 /* Invert 'y' bit if not the default. */
7437 if ((bfd_signed_vma) (relocation + rel->r_addend - from) < 0)
7438 insn ^= 0x01 << 21;
7441 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + rel->r_offset);
7442 break;
7444 case R_PPC64_REL24:
7445 /* A REL24 branching to a linkage function is followed by a
7446 nop. We replace the nop with a ld in order to restore
7447 the TOC base pointer. Only calls to shared objects need
7448 to alter the TOC base. These are recognized by their
7449 need for a PLT entry. */
7450 if (h != NULL
7451 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
7452 && fdh->plt.plist != NULL
7453 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
7454 rel, htab)) != NULL)
7456 bfd_boolean can_plt_call = 0;
7458 if (rel->r_offset + 8 <= input_section->_cooked_size)
7460 insn = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
7461 if (insn == NOP
7462 || insn == CROR_151515 || insn == CROR_313131)
7464 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
7465 contents + rel->r_offset + 4);
7466 can_plt_call = 1;
7470 if (!can_plt_call)
7472 /* If this is a plain branch rather than a branch
7473 and link, don't require a nop. */
7474 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
7475 if ((insn & 1) == 0)
7476 can_plt_call = 1;
7479 if (can_plt_call)
7481 relocation = (stub_entry->stub_offset
7482 + stub_entry->stub_sec->output_offset
7483 + stub_entry->stub_sec->output_section->vma);
7484 unresolved_reloc = FALSE;
7488 if (h != NULL
7489 && h->root.type == bfd_link_hash_undefweak
7490 && relocation == 0
7491 && rel->r_addend == 0)
7493 /* Tweak calls to undefined weak functions to point at a
7494 blr. We can thus call a weak function without first
7495 checking whether the function is defined. We have a
7496 blr at the end of .sfpr. */
7497 BFD_ASSERT (htab->sfpr->_raw_size != 0);
7498 relocation = (htab->sfpr->_raw_size - 4
7499 + htab->sfpr->output_offset
7500 + htab->sfpr->output_section->vma);
7501 from = (rel->r_offset
7502 + input_section->output_offset
7503 + input_section->output_section->vma);
7505 /* But let's not be silly about it. If the blr isn't in
7506 reach, just go to the next instruction. */
7507 if (relocation - from + (1 << 25) >= (1 << 26)
7508 || htab->sfpr->_raw_size == 0)
7509 relocation = from + 4;
7511 break;
7514 /* Set `addend'. */
7515 tls_type = 0;
7516 addend = rel->r_addend;
7517 switch (r_type)
7519 default:
7520 (*_bfd_error_handler)
7521 (_("%s: unknown relocation type %d for symbol %s"),
7522 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
7524 bfd_set_error (bfd_error_bad_value);
7525 ret = FALSE;
7526 continue;
7528 case R_PPC64_NONE:
7529 case R_PPC64_TLS:
7530 case R_PPC64_GNU_VTINHERIT:
7531 case R_PPC64_GNU_VTENTRY:
7532 continue;
7534 /* GOT16 relocations. Like an ADDR16 using the symbol's
7535 address in the GOT as relocation value instead of the
7536 symbol's value itself. Also, create a GOT entry for the
7537 symbol and put the symbol value there. */
7538 case R_PPC64_GOT_TLSGD16:
7539 case R_PPC64_GOT_TLSGD16_LO:
7540 case R_PPC64_GOT_TLSGD16_HI:
7541 case R_PPC64_GOT_TLSGD16_HA:
7542 tls_type = TLS_TLS | TLS_GD;
7543 goto dogot;
7545 case R_PPC64_GOT_TLSLD16:
7546 case R_PPC64_GOT_TLSLD16_LO:
7547 case R_PPC64_GOT_TLSLD16_HI:
7548 case R_PPC64_GOT_TLSLD16_HA:
7549 tls_type = TLS_TLS | TLS_LD;
7550 goto dogot;
7552 case R_PPC64_GOT_TPREL16_DS:
7553 case R_PPC64_GOT_TPREL16_LO_DS:
7554 case R_PPC64_GOT_TPREL16_HI:
7555 case R_PPC64_GOT_TPREL16_HA:
7556 tls_type = TLS_TLS | TLS_TPREL;
7557 goto dogot;
7559 case R_PPC64_GOT_DTPREL16_DS:
7560 case R_PPC64_GOT_DTPREL16_LO_DS:
7561 case R_PPC64_GOT_DTPREL16_HI:
7562 case R_PPC64_GOT_DTPREL16_HA:
7563 tls_type = TLS_TLS | TLS_DTPREL;
7564 goto dogot;
7566 case R_PPC64_GOT16:
7567 case R_PPC64_GOT16_LO:
7568 case R_PPC64_GOT16_HI:
7569 case R_PPC64_GOT16_HA:
7570 case R_PPC64_GOT16_DS:
7571 case R_PPC64_GOT16_LO_DS:
7572 dogot:
7574 /* Relocation is to the entry for this symbol in the global
7575 offset table. */
7576 struct got_entry *ent;
7577 bfd_vma off;
7578 unsigned long indx;
7580 if (htab->sgot == NULL)
7581 abort ();
7583 if (h != NULL)
7584 ent = h->got.glist;
7585 else
7587 if (local_got_ents == NULL)
7588 abort ();
7589 ent = local_got_ents[r_symndx];
7592 for (; ent != NULL; ent = ent->next)
7593 if (ent->addend == rel->r_addend
7594 && ent->tls_type == tls_type)
7595 break;
7596 if (ent == NULL)
7597 abort ();
7599 off = ent->got.offset;
7600 indx = 0;
7601 if (h != NULL)
7603 bfd_boolean dyn = htab->elf.dynamic_sections_created;
7604 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
7605 || (info->shared
7606 && (info->symbolic
7607 || h->dynindx == -1
7608 || (h->elf_link_hash_flags
7609 & ELF_LINK_FORCED_LOCAL))
7610 && (h->elf_link_hash_flags
7611 & ELF_LINK_HASH_DEF_REGULAR)))
7612 /* This is actually a static link, or it is a
7613 -Bsymbolic link and the symbol is defined
7614 locally, or the symbol was forced to be local
7615 because of a version file. */
7617 else
7619 indx = h->dynindx;
7620 unresolved_reloc = FALSE;
7624 /* The offset must always be a multiple of 8. We use the
7625 least significant bit to record whether we have already
7626 processed this entry. */
7627 if ((off & 1) != 0)
7628 off &= ~1;
7629 else
7631 /* Generate relocs for the dynamic linker, except in
7632 the case of TLSLD where we'll use one entry per
7633 module. */
7634 if ((info->shared || indx != 0)
7635 && tls_type != (TLS_TLS | TLS_LD))
7637 outrel.r_offset = (htab->sgot->output_section->vma
7638 + htab->sgot->output_offset
7639 + off);
7640 if (tls_type == (TLS_TLS | TLS_GD))
7642 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
7643 outrel.r_addend = 0;
7644 loc = htab->srelgot->contents;
7645 loc += (htab->srelgot->reloc_count++
7646 * sizeof (Elf64_External_Rela));
7647 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7648 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7649 outrel.r_offset += 8;
7651 else if (tls_type == (TLS_TLS | TLS_DTPREL))
7652 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
7653 else if (tls_type == (TLS_TLS | TLS_TPREL))
7654 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
7655 else if (indx == 0)
7656 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
7657 else
7658 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
7659 outrel.r_addend = rel->r_addend;
7660 if (indx == 0)
7661 outrel.r_addend += relocation;
7662 loc = htab->srelgot->contents;
7663 loc += (htab->srelgot->reloc_count++
7664 * sizeof (Elf64_External_Rela));
7665 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7668 /* Init the .got section contents if we're not
7669 emitting a reloc. */
7670 if (!(info->shared || indx != 0))
7673 relocation += ent->addend;
7674 if ((tls_type & TLS_TLS) != 0)
7676 relocation -= htab->tls_sec->vma + DTP_OFFSET;
7677 if ((tls_type & TLS_TPREL) != 0)
7678 relocation += DTP_OFFSET - TP_OFFSET;
7681 if ((tls_type & (TLS_GD | TLS_LD)) != 0)
7683 if ((tls_type & TLS_LD) != 0)
7684 relocation = - DTP_OFFSET;
7685 bfd_put_64 (output_bfd, relocation,
7686 htab->sgot->contents + off + 8);
7687 relocation = 1;
7689 bfd_put_64 (output_bfd, relocation,
7690 htab->sgot->contents + off);
7692 ent->got.offset |= 1;
7695 if (off >= (bfd_vma) -2)
7696 abort ();
7698 relocation = htab->sgot->output_offset + off;
7700 /* TOC base (r2) is TOC start plus 0x8000. */
7701 addend = - TOC_BASE_OFF;
7703 break;
7705 case R_PPC64_PLT16_HA:
7706 case R_PPC64_PLT16_HI:
7707 case R_PPC64_PLT16_LO:
7708 case R_PPC64_PLT32:
7709 case R_PPC64_PLT64:
7710 /* Relocation is to the entry for this symbol in the
7711 procedure linkage table. */
7713 /* Resolve a PLT reloc against a local symbol directly,
7714 without using the procedure linkage table. */
7715 if (h == NULL)
7716 break;
7718 /* It's possible that we didn't make a PLT entry for this
7719 symbol. This happens when statically linking PIC code,
7720 or when using -Bsymbolic. Go find a match if there is a
7721 PLT entry. */
7722 if (htab->splt != NULL)
7724 struct plt_entry *ent;
7725 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7726 if (ent->addend == rel->r_addend
7727 && ent->plt.offset != (bfd_vma) -1)
7729 relocation = (htab->splt->output_section->vma
7730 + htab->splt->output_offset
7731 + ent->plt.offset);
7732 unresolved_reloc = FALSE;
7735 break;
7737 /* TOC16 relocs. We want the offset relative to the TOC base,
7738 which is the address of the start of the TOC plus 0x8000.
7739 The TOC consists of sections .got, .toc, .tocbss, and .plt,
7740 in this order. */
7741 case R_PPC64_TOC16:
7742 case R_PPC64_TOC16_LO:
7743 case R_PPC64_TOC16_HI:
7744 case R_PPC64_TOC16_DS:
7745 case R_PPC64_TOC16_LO_DS:
7746 case R_PPC64_TOC16_HA:
7747 addend -= TOCstart + TOC_BASE_OFF;
7748 break;
7750 /* Relocate against the beginning of the section. */
7751 case R_PPC64_SECTOFF:
7752 case R_PPC64_SECTOFF_LO:
7753 case R_PPC64_SECTOFF_HI:
7754 case R_PPC64_SECTOFF_DS:
7755 case R_PPC64_SECTOFF_LO_DS:
7756 case R_PPC64_SECTOFF_HA:
7757 if (sec != (asection *) 0)
7758 addend -= sec->output_section->vma;
7759 break;
7761 case R_PPC64_REL14:
7762 case R_PPC64_REL14_BRNTAKEN:
7763 case R_PPC64_REL14_BRTAKEN:
7764 case R_PPC64_REL24:
7765 break;
7767 case R_PPC64_TPREL16:
7768 case R_PPC64_TPREL16_LO:
7769 case R_PPC64_TPREL16_HI:
7770 case R_PPC64_TPREL16_HA:
7771 case R_PPC64_TPREL16_DS:
7772 case R_PPC64_TPREL16_LO_DS:
7773 case R_PPC64_TPREL16_HIGHER:
7774 case R_PPC64_TPREL16_HIGHERA:
7775 case R_PPC64_TPREL16_HIGHEST:
7776 case R_PPC64_TPREL16_HIGHESTA:
7777 addend -= htab->tls_sec->vma + TP_OFFSET;
7778 if (info->shared)
7779 /* The TPREL16 relocs shouldn't really be used in shared
7780 libs as they will result in DT_TEXTREL being set, but
7781 support them anyway. */
7782 goto dodyn;
7783 break;
7785 case R_PPC64_DTPREL16:
7786 case R_PPC64_DTPREL16_LO:
7787 case R_PPC64_DTPREL16_HI:
7788 case R_PPC64_DTPREL16_HA:
7789 case R_PPC64_DTPREL16_DS:
7790 case R_PPC64_DTPREL16_LO_DS:
7791 case R_PPC64_DTPREL16_HIGHER:
7792 case R_PPC64_DTPREL16_HIGHERA:
7793 case R_PPC64_DTPREL16_HIGHEST:
7794 case R_PPC64_DTPREL16_HIGHESTA:
7795 addend -= htab->tls_sec->vma + DTP_OFFSET;
7796 break;
7798 case R_PPC64_TPREL64:
7799 addend -= htab->tls_sec->vma + TP_OFFSET;
7800 goto dodyn;
7802 case R_PPC64_DTPREL64:
7803 addend -= htab->tls_sec->vma + DTP_OFFSET;
7804 /* Fall thru */
7806 /* Relocations that may need to be propagated if this is a
7807 dynamic object. */
7808 case R_PPC64_DTPMOD64:
7809 case R_PPC64_REL30:
7810 case R_PPC64_REL32:
7811 case R_PPC64_REL64:
7812 case R_PPC64_ADDR14:
7813 case R_PPC64_ADDR14_BRNTAKEN:
7814 case R_PPC64_ADDR14_BRTAKEN:
7815 case R_PPC64_ADDR16:
7816 case R_PPC64_ADDR16_DS:
7817 case R_PPC64_ADDR16_HA:
7818 case R_PPC64_ADDR16_HI:
7819 case R_PPC64_ADDR16_HIGHER:
7820 case R_PPC64_ADDR16_HIGHERA:
7821 case R_PPC64_ADDR16_HIGHEST:
7822 case R_PPC64_ADDR16_HIGHESTA:
7823 case R_PPC64_ADDR16_LO:
7824 case R_PPC64_ADDR16_LO_DS:
7825 case R_PPC64_ADDR24:
7826 case R_PPC64_ADDR32:
7827 case R_PPC64_ADDR64:
7828 case R_PPC64_UADDR16:
7829 case R_PPC64_UADDR32:
7830 case R_PPC64_UADDR64:
7831 /* r_symndx will be zero only for relocs against symbols
7832 from removed linkonce sections, or sections discarded by
7833 a linker script. */
7834 dodyn:
7835 if (r_symndx == 0)
7836 break;
7837 /* Fall thru. */
7839 case R_PPC64_TOC:
7840 if ((input_section->flags & SEC_ALLOC) == 0)
7841 break;
7843 if (NO_OPD_RELOCS && is_opd)
7844 break;
7846 if ((info->shared
7847 && (MUST_BE_DYN_RELOC (r_type)
7848 || (h != NULL
7849 && h->dynindx != -1
7850 && (! info->symbolic
7851 || (h->elf_link_hash_flags
7852 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
7853 || (!info->shared
7854 && h != NULL
7855 && h->dynindx != -1
7856 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
7857 && (((h->elf_link_hash_flags
7858 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
7859 && (h->elf_link_hash_flags
7860 & ELF_LINK_HASH_DEF_REGULAR) == 0)
7861 || h->root.type == bfd_link_hash_undefweak
7862 || h->root.type == bfd_link_hash_undefined)))
7864 Elf_Internal_Rela outrel;
7865 bfd_boolean skip, relocate;
7866 asection *sreloc;
7867 bfd_byte *loc;
7869 /* When generating a dynamic object, these relocations
7870 are copied into the output file to be resolved at run
7871 time. */
7873 skip = FALSE;
7874 relocate = FALSE;
7876 outrel.r_offset =
7877 _bfd_elf_section_offset (output_bfd, info, input_section,
7878 rel->r_offset);
7879 if (outrel.r_offset == (bfd_vma) -1)
7880 skip = TRUE;
7881 else if (outrel.r_offset == (bfd_vma) -2)
7882 skip = TRUE, relocate = TRUE;
7883 outrel.r_offset += (input_section->output_section->vma
7884 + input_section->output_offset);
7885 outrel.r_addend = rel->r_addend;
7887 if (skip)
7888 memset (&outrel, 0, sizeof outrel);
7889 else if (h != NULL
7890 && h->dynindx != -1
7891 && !is_opd
7892 && (!MUST_BE_DYN_RELOC (r_type)
7893 || !info->shared
7894 || !info->symbolic
7895 || (h->elf_link_hash_flags
7896 & ELF_LINK_HASH_DEF_REGULAR) == 0))
7897 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
7898 else
7900 /* This symbol is local, or marked to become local,
7901 or this is an opd section reloc which must point
7902 at a local function. */
7903 outrel.r_addend += relocation;
7904 /* ??? why? */
7905 relocate = TRUE;
7906 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7908 if (is_opd && h != NULL)
7910 /* Lie about opd entries. This case occurs
7911 when building shared libraries and we
7912 reference a function in another shared
7913 lib. The same thing happens for a weak
7914 definition in an application that's
7915 overridden by a strong definition in a
7916 shared lib. (I believe this is a generic
7917 bug in binutils handling of weak syms.)
7918 In these cases we won't use the opd
7919 entry in this lib. */
7920 unresolved_reloc = FALSE;
7922 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
7924 else
7926 long indx = 0;
7928 if (bfd_is_abs_section (sec))
7930 else if (sec == NULL || sec->owner == NULL)
7932 bfd_set_error (bfd_error_bad_value);
7933 return FALSE;
7935 else
7937 asection *osec;
7939 osec = sec->output_section;
7940 indx = elf_section_data (osec)->dynindx;
7942 /* We are turning this relocation into one
7943 against a section symbol, so subtract out
7944 the output section's address but not the
7945 offset of the input section in the output
7946 section. */
7947 outrel.r_addend -= osec->vma;
7950 outrel.r_info = ELF64_R_INFO (indx, r_type);
7954 sreloc = elf_section_data (input_section)->sreloc;
7955 if (sreloc == NULL)
7956 abort ();
7958 loc = sreloc->contents;
7959 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
7960 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
7962 /* If this reloc is against an external symbol, it will
7963 be computed at runtime, so there's no need to do
7964 anything now. */
7965 if (! relocate)
7966 continue;
7968 break;
7970 case R_PPC64_COPY:
7971 case R_PPC64_GLOB_DAT:
7972 case R_PPC64_JMP_SLOT:
7973 case R_PPC64_RELATIVE:
7974 /* We shouldn't ever see these dynamic relocs in relocatable
7975 files. */
7976 /* Fall thru */
7978 case R_PPC64_PLTGOT16:
7979 case R_PPC64_PLTGOT16_DS:
7980 case R_PPC64_PLTGOT16_HA:
7981 case R_PPC64_PLTGOT16_HI:
7982 case R_PPC64_PLTGOT16_LO:
7983 case R_PPC64_PLTGOT16_LO_DS:
7984 case R_PPC64_PLTREL32:
7985 case R_PPC64_PLTREL64:
7986 /* These ones haven't been implemented yet. */
7988 (*_bfd_error_handler)
7989 (_("%s: Relocation %s is not supported for symbol %s."),
7990 bfd_archive_filename (input_bfd),
7991 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
7993 bfd_set_error (bfd_error_invalid_operation);
7994 ret = FALSE;
7995 continue;
7998 /* Do any further special processing. */
7999 switch (r_type)
8001 default:
8002 break;
8004 case R_PPC64_ADDR16_HA:
8005 case R_PPC64_ADDR16_HIGHERA:
8006 case R_PPC64_ADDR16_HIGHESTA:
8007 case R_PPC64_GOT16_HA:
8008 case R_PPC64_PLTGOT16_HA:
8009 case R_PPC64_PLT16_HA:
8010 case R_PPC64_TOC16_HA:
8011 case R_PPC64_SECTOFF_HA:
8012 case R_PPC64_TPREL16_HA:
8013 case R_PPC64_DTPREL16_HA:
8014 case R_PPC64_GOT_TLSGD16_HA:
8015 case R_PPC64_GOT_TLSLD16_HA:
8016 case R_PPC64_GOT_TPREL16_HA:
8017 case R_PPC64_GOT_DTPREL16_HA:
8018 case R_PPC64_TPREL16_HIGHER:
8019 case R_PPC64_TPREL16_HIGHERA:
8020 case R_PPC64_TPREL16_HIGHEST:
8021 case R_PPC64_TPREL16_HIGHESTA:
8022 case R_PPC64_DTPREL16_HIGHER:
8023 case R_PPC64_DTPREL16_HIGHERA:
8024 case R_PPC64_DTPREL16_HIGHEST:
8025 case R_PPC64_DTPREL16_HIGHESTA:
8026 /* It's just possible that this symbol is a weak symbol
8027 that's not actually defined anywhere. In that case,
8028 'sec' would be NULL, and we should leave the symbol
8029 alone (it will be set to zero elsewhere in the link). */
8030 if (sec != NULL)
8031 /* Add 0x10000 if sign bit in 0:15 is set. */
8032 addend += ((relocation + addend) & 0x8000) << 1;
8033 break;
8035 case R_PPC64_ADDR16_DS:
8036 case R_PPC64_ADDR16_LO_DS:
8037 case R_PPC64_GOT16_DS:
8038 case R_PPC64_GOT16_LO_DS:
8039 case R_PPC64_PLT16_LO_DS:
8040 case R_PPC64_SECTOFF_DS:
8041 case R_PPC64_SECTOFF_LO_DS:
8042 case R_PPC64_TOC16_DS:
8043 case R_PPC64_TOC16_LO_DS:
8044 case R_PPC64_PLTGOT16_DS:
8045 case R_PPC64_PLTGOT16_LO_DS:
8046 case R_PPC64_GOT_TPREL16_DS:
8047 case R_PPC64_GOT_TPREL16_LO_DS:
8048 case R_PPC64_GOT_DTPREL16_DS:
8049 case R_PPC64_GOT_DTPREL16_LO_DS:
8050 case R_PPC64_TPREL16_DS:
8051 case R_PPC64_TPREL16_LO_DS:
8052 case R_PPC64_DTPREL16_DS:
8053 case R_PPC64_DTPREL16_LO_DS:
8054 if (((relocation + addend) & 3) != 0)
8056 (*_bfd_error_handler)
8057 (_("%s: error: relocation %s not a multiple of 4"),
8058 bfd_archive_filename (input_bfd),
8059 ppc64_elf_howto_table[(int) r_type]->name);
8060 bfd_set_error (bfd_error_bad_value);
8061 ret = FALSE;
8062 continue;
8064 break;
8066 case R_PPC64_REL14:
8067 case R_PPC64_REL14_BRNTAKEN:
8068 case R_PPC64_REL14_BRTAKEN:
8069 max_br_offset = 1 << 15;
8070 goto branch_check;
8072 case R_PPC64_REL24:
8073 max_br_offset = 1 << 25;
8075 branch_check:
8076 /* If the branch is out of reach, then redirect the
8077 call to the local stub for this function. */
8078 from = (rel->r_offset
8079 + input_section->output_offset
8080 + input_section->output_section->vma);
8081 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
8082 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
8083 rel, htab)) != NULL)
8085 /* Munge up the value and addend so that we call the stub
8086 rather than the procedure directly. */
8087 relocation = (stub_entry->stub_offset
8088 + stub_entry->stub_sec->output_offset
8089 + stub_entry->stub_sec->output_section->vma);
8090 addend = 0;
8092 break;
8095 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
8096 because such sections are not SEC_ALLOC and thus ld.so will
8097 not process them. */
8098 if (unresolved_reloc
8099 && !((input_section->flags & SEC_DEBUGGING) != 0
8100 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
8102 (*_bfd_error_handler)
8103 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
8104 bfd_archive_filename (input_bfd),
8105 bfd_get_section_name (input_bfd, input_section),
8106 (long) rel->r_offset,
8107 h->root.root.string);
8108 ret = FALSE;
8111 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
8112 input_bfd,
8113 input_section,
8114 contents,
8115 rel->r_offset,
8116 relocation,
8117 addend);
8119 if (r != bfd_reloc_ok)
8121 const char *name;
8123 if (h != NULL)
8125 if (h->root.type == bfd_link_hash_undefweak
8126 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
8128 /* Assume this is a call protected by other code that
8129 detects the symbol is undefined. If this is the case,
8130 we can safely ignore the overflow. If not, the
8131 program is hosed anyway, and a little warning isn't
8132 going to help. */
8134 continue;
8137 name = h->root.root.string;
8139 else
8141 name = bfd_elf_local_sym_name (input_bfd, sym);
8142 if (name == NULL)
8143 continue;
8146 if (r == bfd_reloc_overflow)
8148 if (warned)
8149 continue;
8150 if (!((*info->callbacks->reloc_overflow)
8151 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
8152 rel->r_addend, input_bfd, input_section, rel->r_offset)))
8153 return FALSE;
8155 else
8157 (*_bfd_error_handler)
8158 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
8159 bfd_archive_filename (input_bfd),
8160 bfd_get_section_name (input_bfd, input_section),
8161 (long) rel->r_offset, name, (int) r);
8162 ret = FALSE;
8167 return ret;
8170 /* Finish up dynamic symbol handling. We set the contents of various
8171 dynamic sections here. */
8173 static bfd_boolean
8174 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
8175 bfd *output_bfd;
8176 struct bfd_link_info *info;
8177 struct elf_link_hash_entry *h;
8178 Elf_Internal_Sym *sym;
8180 struct ppc_link_hash_table *htab;
8181 bfd *dynobj;
8183 htab = ppc_hash_table (info);
8184 dynobj = htab->elf.dynobj;
8186 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
8188 struct plt_entry *ent;
8189 Elf_Internal_Rela rela;
8190 bfd_byte *loc;
8192 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
8193 if (ent->plt.offset != (bfd_vma) -1)
8195 /* This symbol has an entry in the procedure linkage
8196 table. Set it up. */
8198 if (htab->splt == NULL
8199 || htab->srelplt == NULL
8200 || htab->sglink == NULL)
8201 abort ();
8203 /* Create a JMP_SLOT reloc to inform the dynamic linker to
8204 fill in the PLT entry. */
8206 rela.r_offset = (htab->splt->output_section->vma
8207 + htab->splt->output_offset
8208 + ent->plt.offset);
8209 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
8210 rela.r_addend = ent->addend;
8212 loc = htab->srelplt->contents;
8213 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
8214 * sizeof (Elf64_External_Rela));
8215 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8219 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
8221 Elf_Internal_Rela rela;
8222 bfd_byte *loc;
8224 /* This symbol needs a copy reloc. Set it up. */
8226 if (h->dynindx == -1
8227 || (h->root.type != bfd_link_hash_defined
8228 && h->root.type != bfd_link_hash_defweak)
8229 || htab->srelbss == NULL)
8230 abort ();
8232 rela.r_offset = (h->root.u.def.value
8233 + h->root.u.def.section->output_section->vma
8234 + h->root.u.def.section->output_offset);
8235 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
8236 rela.r_addend = 0;
8237 loc = htab->srelbss->contents;
8238 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
8239 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
8242 /* Mark some specially defined symbols as absolute. */
8243 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
8244 sym->st_shndx = SHN_ABS;
8246 return TRUE;
8249 /* Used to decide how to sort relocs in an optimal manner for the
8250 dynamic linker, before writing them out. */
8252 static enum elf_reloc_type_class
8253 ppc64_elf_reloc_type_class (rela)
8254 const Elf_Internal_Rela *rela;
8256 enum elf_ppc64_reloc_type r_type;
8258 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rela->r_info);
8259 switch (r_type)
8261 case R_PPC64_RELATIVE:
8262 return reloc_class_relative;
8263 case R_PPC64_JMP_SLOT:
8264 return reloc_class_plt;
8265 case R_PPC64_COPY:
8266 return reloc_class_copy;
8267 default:
8268 return reloc_class_normal;
8272 /* Finish up the dynamic sections. */
8274 static bfd_boolean
8275 ppc64_elf_finish_dynamic_sections (output_bfd, info)
8276 bfd *output_bfd;
8277 struct bfd_link_info *info;
8279 struct ppc_link_hash_table *htab;
8280 bfd *dynobj;
8281 asection *sdyn;
8283 htab = ppc_hash_table (info);
8284 dynobj = htab->elf.dynobj;
8285 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
8287 if (htab->elf.dynamic_sections_created)
8289 Elf64_External_Dyn *dyncon, *dynconend;
8291 if (sdyn == NULL || htab->sgot == NULL)
8292 abort ();
8294 dyncon = (Elf64_External_Dyn *) sdyn->contents;
8295 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
8296 for (; dyncon < dynconend; dyncon++)
8298 Elf_Internal_Dyn dyn;
8299 asection *s;
8301 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
8303 switch (dyn.d_tag)
8305 default:
8306 continue;
8308 case DT_PPC64_GLINK:
8309 s = htab->sglink;
8310 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8311 break;
8313 case DT_PPC64_OPD:
8314 s = bfd_get_section_by_name (output_bfd, ".opd");
8315 if (s == NULL)
8316 continue;
8317 dyn.d_un.d_ptr = s->vma;
8318 break;
8320 case DT_PPC64_OPDSZ:
8321 s = bfd_get_section_by_name (output_bfd, ".opd");
8322 if (s == NULL)
8323 continue;
8324 dyn.d_un.d_val = s->_raw_size;
8325 break;
8327 case DT_PLTGOT:
8328 s = htab->splt;
8329 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8330 break;
8332 case DT_JMPREL:
8333 s = htab->srelplt;
8334 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
8335 break;
8337 case DT_PLTRELSZ:
8338 dyn.d_un.d_val = htab->srelplt->_raw_size;
8339 break;
8341 case DT_RELASZ:
8342 /* Don't count procedure linkage table relocs in the
8343 overall reloc count. */
8344 s = htab->srelplt;
8345 if (s == NULL)
8346 continue;
8347 dyn.d_un.d_val -= s->_raw_size;
8348 break;
8350 case DT_RELA:
8351 /* We may not be using the standard ELF linker script.
8352 If .rela.plt is the first .rela section, we adjust
8353 DT_RELA to not include it. */
8354 s = htab->srelplt;
8355 if (s == NULL)
8356 continue;
8357 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
8358 continue;
8359 dyn.d_un.d_ptr += s->_raw_size;
8360 break;
8363 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
8367 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
8369 /* Fill in the first entry in the global offset table.
8370 We use it to hold the link-time TOCbase. */
8371 bfd_put_64 (output_bfd,
8372 elf_gp (output_bfd) + TOC_BASE_OFF,
8373 htab->sgot->contents);
8375 /* Set .got entry size. */
8376 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
8379 if (htab->splt != NULL && htab->splt->_raw_size != 0)
8381 /* Set .plt entry size. */
8382 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
8383 = PLT_ENTRY_SIZE;
8386 return TRUE;
8389 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
8390 #define TARGET_LITTLE_NAME "elf64-powerpcle"
8391 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
8392 #define TARGET_BIG_NAME "elf64-powerpc"
8393 #define ELF_ARCH bfd_arch_powerpc
8394 #define ELF_MACHINE_CODE EM_PPC64
8395 #define ELF_MAXPAGESIZE 0x10000
8396 #define elf_info_to_howto ppc64_elf_info_to_howto
8398 #ifdef EM_CYGNUS_POWERPC
8399 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
8400 #endif
8402 #ifdef EM_PPC_OLD
8403 #define ELF_MACHINE_ALT2 EM_PPC_OLD
8404 #endif
8406 #define elf_backend_want_got_sym 0
8407 #define elf_backend_want_plt_sym 0
8408 #define elf_backend_plt_alignment 3
8409 #define elf_backend_plt_not_loaded 1
8410 #define elf_backend_got_symbol_offset 0
8411 #define elf_backend_got_header_size 8
8412 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
8413 #define elf_backend_can_gc_sections 1
8414 #define elf_backend_can_refcount 1
8415 #define elf_backend_rela_normal 1
8417 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
8418 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
8419 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
8420 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
8421 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
8423 #define elf_backend_object_p ppc64_elf_object_p
8424 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
8425 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
8426 #define elf_backend_check_relocs ppc64_elf_check_relocs
8427 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
8428 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
8429 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
8430 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
8431 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
8432 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
8433 #define elf_backend_relocate_section ppc64_elf_relocate_section
8434 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
8435 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
8436 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
8438 #include "elf64-target.h"