* gas/cris/rd-dw2-1.d: Adjust for dwarf2 directory table change.
[binutils.git] / bfd / elf64-ppc.c
blob2ca139d807ffb328f679986a22eb110ce232a192
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 /* .plt call stub instructions. */
77 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
78 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
79 #define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
80 #define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
81 #define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
82 /* ld %r11,xxx+16@l(%r12) */
83 #define BCTR 0x4e800420 /* bctr */
85 /* The normal stub is this size. */
86 #define PLT_CALL_STUB_SIZE (7*4)
88 /* But sometimes the .plt entry crosses a 64k boundary, and we need
89 to adjust the high word with this insn. */
90 #define ADDIS_R12_R12_1 0x3d8c0001 /* addis %r12,%r12,1 */
92 /* The .glink fixup call stub is the same as the .plt call stub, but
93 the first instruction restores r2, and the std is omitted. */
94 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
96 /* Always allow this much space. */
97 #define GLINK_CALL_STUB_SIZE (8*4)
99 /* Pad with this. */
100 #define NOP 0x60000000
102 /* Some other nops. */
103 #define CROR_151515 0x4def7b82
104 #define CROR_313131 0x4ffffb82
106 /* .glink entries for the first 32k functions are two instructions. */
107 #define LI_R0_0 0x38000000 /* li %r0,0 */
108 #define B_DOT 0x48000000 /* b . */
110 /* After that, we need two instructions to load the index, followed by
111 a branch. */
112 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
113 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
115 /* Instructions to save and restore floating point regs. */
116 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
117 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
118 #define BLR 0x4e800020 /* blr */
120 /* Since .opd is an array of descriptors and each entry will end up
121 with identical R_PPC64_RELATIVE relocs, there is really no need to
122 propagate .opd relocs; The dynamic linker should be taught to
123 relocate .opd without reloc entries. */
124 #ifndef NO_OPD_RELOCS
125 #define NO_OPD_RELOCS 0
126 #endif
128 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
130 /* Relocation HOWTO's. */
131 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
133 static reloc_howto_type ppc64_elf_howto_raw[] = {
134 /* This reloc does nothing. */
135 HOWTO (R_PPC64_NONE, /* type */
136 0, /* rightshift */
137 0, /* size (0 = byte, 1 = short, 2 = long) */
138 8, /* bitsize */
139 FALSE, /* pc_relative */
140 0, /* bitpos */
141 complain_overflow_dont, /* complain_on_overflow */
142 bfd_elf_generic_reloc, /* special_function */
143 "R_PPC64_NONE", /* name */
144 FALSE, /* partial_inplace */
145 0, /* src_mask */
146 0, /* dst_mask */
147 FALSE), /* pcrel_offset */
149 /* A standard 32 bit relocation. */
150 HOWTO (R_PPC64_ADDR32, /* type */
151 0, /* rightshift */
152 2, /* size (0 = byte, 1 = short, 2 = long) */
153 32, /* bitsize */
154 FALSE, /* pc_relative */
155 0, /* bitpos */
156 complain_overflow_bitfield, /* complain_on_overflow */
157 bfd_elf_generic_reloc, /* special_function */
158 "R_PPC64_ADDR32", /* name */
159 FALSE, /* partial_inplace */
160 0, /* src_mask */
161 0xffffffff, /* dst_mask */
162 FALSE), /* pcrel_offset */
164 /* An absolute 26 bit branch; the lower two bits must be zero.
165 FIXME: we don't check that, we just clear them. */
166 HOWTO (R_PPC64_ADDR24, /* type */
167 0, /* rightshift */
168 2, /* size (0 = byte, 1 = short, 2 = long) */
169 26, /* bitsize */
170 FALSE, /* pc_relative */
171 0, /* bitpos */
172 complain_overflow_bitfield, /* complain_on_overflow */
173 bfd_elf_generic_reloc, /* special_function */
174 "R_PPC64_ADDR24", /* name */
175 FALSE, /* partial_inplace */
176 0, /* src_mask */
177 0x03fffffc, /* dst_mask */
178 FALSE), /* pcrel_offset */
180 /* A standard 16 bit relocation. */
181 HOWTO (R_PPC64_ADDR16, /* type */
182 0, /* rightshift */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
184 16, /* bitsize */
185 FALSE, /* pc_relative */
186 0, /* bitpos */
187 complain_overflow_bitfield, /* complain_on_overflow */
188 bfd_elf_generic_reloc, /* special_function */
189 "R_PPC64_ADDR16", /* name */
190 FALSE, /* partial_inplace */
191 0, /* src_mask */
192 0xffff, /* dst_mask */
193 FALSE), /* pcrel_offset */
195 /* A 16 bit relocation without overflow. */
196 HOWTO (R_PPC64_ADDR16_LO, /* type */
197 0, /* rightshift */
198 1, /* size (0 = byte, 1 = short, 2 = long) */
199 16, /* bitsize */
200 FALSE, /* pc_relative */
201 0, /* bitpos */
202 complain_overflow_dont,/* complain_on_overflow */
203 bfd_elf_generic_reloc, /* special_function */
204 "R_PPC64_ADDR16_LO", /* name */
205 FALSE, /* partial_inplace */
206 0, /* src_mask */
207 0xffff, /* dst_mask */
208 FALSE), /* pcrel_offset */
210 /* Bits 16-31 of an address. */
211 HOWTO (R_PPC64_ADDR16_HI, /* type */
212 16, /* rightshift */
213 1, /* size (0 = byte, 1 = short, 2 = long) */
214 16, /* bitsize */
215 FALSE, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_dont, /* complain_on_overflow */
218 bfd_elf_generic_reloc, /* special_function */
219 "R_PPC64_ADDR16_HI", /* name */
220 FALSE, /* partial_inplace */
221 0, /* src_mask */
222 0xffff, /* dst_mask */
223 FALSE), /* pcrel_offset */
225 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
226 bits, treated as a signed number, is negative. */
227 HOWTO (R_PPC64_ADDR16_HA, /* type */
228 16, /* rightshift */
229 1, /* size (0 = byte, 1 = short, 2 = long) */
230 16, /* bitsize */
231 FALSE, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_dont, /* complain_on_overflow */
234 ppc64_elf_ha_reloc, /* special_function */
235 "R_PPC64_ADDR16_HA", /* name */
236 FALSE, /* partial_inplace */
237 0, /* src_mask */
238 0xffff, /* dst_mask */
239 FALSE), /* pcrel_offset */
241 /* An absolute 16 bit branch; the lower two bits must be zero.
242 FIXME: we don't check that, we just clear them. */
243 HOWTO (R_PPC64_ADDR14, /* type */
244 0, /* rightshift */
245 2, /* size (0 = byte, 1 = short, 2 = long) */
246 16, /* bitsize */
247 FALSE, /* pc_relative */
248 0, /* bitpos */
249 complain_overflow_bitfield, /* complain_on_overflow */
250 bfd_elf_generic_reloc, /* special_function */
251 "R_PPC64_ADDR14", /* name */
252 FALSE, /* partial_inplace */
253 0, /* src_mask */
254 0x0000fffc, /* dst_mask */
255 FALSE), /* pcrel_offset */
257 /* An absolute 16 bit branch, for which bit 10 should be set to
258 indicate that the branch is expected to be taken. The lower two
259 bits must be zero. */
260 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
261 0, /* rightshift */
262 2, /* size (0 = byte, 1 = short, 2 = long) */
263 16, /* bitsize */
264 FALSE, /* pc_relative */
265 0, /* bitpos */
266 complain_overflow_bitfield, /* complain_on_overflow */
267 ppc64_elf_brtaken_reloc, /* special_function */
268 "R_PPC64_ADDR14_BRTAKEN",/* name */
269 FALSE, /* partial_inplace */
270 0, /* src_mask */
271 0x0000fffc, /* dst_mask */
272 FALSE), /* pcrel_offset */
274 /* An absolute 16 bit branch, for which bit 10 should be set to
275 indicate that the branch is not expected to be taken. The lower
276 two bits must be zero. */
277 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
278 0, /* rightshift */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
280 16, /* bitsize */
281 FALSE, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_bitfield, /* complain_on_overflow */
284 ppc64_elf_brtaken_reloc, /* special_function */
285 "R_PPC64_ADDR14_BRNTAKEN",/* name */
286 FALSE, /* partial_inplace */
287 0, /* src_mask */
288 0x0000fffc, /* dst_mask */
289 FALSE), /* pcrel_offset */
291 /* A relative 26 bit branch; the lower two bits must be zero. */
292 HOWTO (R_PPC64_REL24, /* type */
293 0, /* rightshift */
294 2, /* size (0 = byte, 1 = short, 2 = long) */
295 26, /* bitsize */
296 TRUE, /* pc_relative */
297 0, /* bitpos */
298 complain_overflow_signed, /* complain_on_overflow */
299 bfd_elf_generic_reloc, /* special_function */
300 "R_PPC64_REL24", /* name */
301 FALSE, /* partial_inplace */
302 0, /* src_mask */
303 0x03fffffc, /* dst_mask */
304 TRUE), /* pcrel_offset */
306 /* A relative 16 bit branch; the lower two bits must be zero. */
307 HOWTO (R_PPC64_REL14, /* type */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 16, /* bitsize */
311 TRUE, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_signed, /* complain_on_overflow */
314 bfd_elf_generic_reloc, /* special_function */
315 "R_PPC64_REL14", /* name */
316 FALSE, /* partial_inplace */
317 0, /* src_mask */
318 0x0000fffc, /* dst_mask */
319 TRUE), /* pcrel_offset */
321 /* A relative 16 bit branch. Bit 10 should be set to indicate that
322 the branch is expected to be taken. The lower two bits must be
323 zero. */
324 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
325 0, /* rightshift */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
328 TRUE, /* pc_relative */
329 0, /* bitpos */
330 complain_overflow_signed, /* complain_on_overflow */
331 ppc64_elf_brtaken_reloc, /* special_function */
332 "R_PPC64_REL14_BRTAKEN", /* name */
333 FALSE, /* partial_inplace */
334 0, /* src_mask */
335 0x0000fffc, /* dst_mask */
336 TRUE), /* pcrel_offset */
338 /* A relative 16 bit branch. Bit 10 should be set to indicate that
339 the branch is not expected to be taken. The lower two bits must
340 be zero. */
341 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
342 0, /* rightshift */
343 2, /* size (0 = byte, 1 = short, 2 = long) */
344 16, /* bitsize */
345 TRUE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_signed, /* complain_on_overflow */
348 ppc64_elf_brtaken_reloc, /* special_function */
349 "R_PPC64_REL14_BRNTAKEN",/* name */
350 FALSE, /* partial_inplace */
351 0, /* src_mask */
352 0x0000fffc, /* dst_mask */
353 TRUE), /* pcrel_offset */
355 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
356 symbol. */
357 HOWTO (R_PPC64_GOT16, /* type */
358 0, /* rightshift */
359 1, /* size (0 = byte, 1 = short, 2 = long) */
360 16, /* bitsize */
361 FALSE, /* pc_relative */
362 0, /* bitpos */
363 complain_overflow_signed, /* complain_on_overflow */
364 ppc64_elf_unhandled_reloc, /* special_function */
365 "R_PPC64_GOT16", /* name */
366 FALSE, /* partial_inplace */
367 0, /* src_mask */
368 0xffff, /* dst_mask */
369 FALSE), /* pcrel_offset */
371 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
372 the symbol. */
373 HOWTO (R_PPC64_GOT16_LO, /* type */
374 0, /* rightshift */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
376 16, /* bitsize */
377 FALSE, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_dont, /* complain_on_overflow */
380 ppc64_elf_unhandled_reloc, /* special_function */
381 "R_PPC64_GOT16_LO", /* name */
382 FALSE, /* partial_inplace */
383 0, /* src_mask */
384 0xffff, /* dst_mask */
385 FALSE), /* pcrel_offset */
387 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
388 the symbol. */
389 HOWTO (R_PPC64_GOT16_HI, /* type */
390 16, /* rightshift */
391 1, /* size (0 = byte, 1 = short, 2 = long) */
392 16, /* bitsize */
393 FALSE, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_dont,/* complain_on_overflow */
396 ppc64_elf_unhandled_reloc, /* special_function */
397 "R_PPC64_GOT16_HI", /* name */
398 FALSE, /* partial_inplace */
399 0, /* src_mask */
400 0xffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
403 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
404 the symbol. */
405 HOWTO (R_PPC64_GOT16_HA, /* type */
406 16, /* rightshift */
407 1, /* size (0 = byte, 1 = short, 2 = long) */
408 16, /* bitsize */
409 FALSE, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_dont,/* complain_on_overflow */
412 ppc64_elf_unhandled_reloc, /* special_function */
413 "R_PPC64_GOT16_HA", /* name */
414 FALSE, /* partial_inplace */
415 0, /* src_mask */
416 0xffff, /* dst_mask */
417 FALSE), /* pcrel_offset */
419 /* This is used only by the dynamic linker. The symbol should exist
420 both in the object being run and in some shared library. The
421 dynamic linker copies the data addressed by the symbol from the
422 shared library into the object, because the object being
423 run has to have the data at some particular address. */
424 HOWTO (R_PPC64_COPY, /* type */
425 0, /* rightshift */
426 0, /* this one is variable size */
427 0, /* bitsize */
428 FALSE, /* pc_relative */
429 0, /* bitpos */
430 complain_overflow_dont, /* complain_on_overflow */
431 ppc64_elf_unhandled_reloc, /* special_function */
432 "R_PPC64_COPY", /* name */
433 FALSE, /* partial_inplace */
434 0, /* src_mask */
435 0, /* dst_mask */
436 FALSE), /* pcrel_offset */
438 /* Like R_PPC64_ADDR64, but used when setting global offset table
439 entries. */
440 HOWTO (R_PPC64_GLOB_DAT, /* type */
441 0, /* rightshift */
442 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
443 64, /* bitsize */
444 FALSE, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_dont, /* complain_on_overflow */
447 ppc64_elf_unhandled_reloc, /* special_function */
448 "R_PPC64_GLOB_DAT", /* name */
449 FALSE, /* partial_inplace */
450 0, /* src_mask */
451 ONES (64), /* dst_mask */
452 FALSE), /* pcrel_offset */
454 /* Created by the link editor. Marks a procedure linkage table
455 entry for a symbol. */
456 HOWTO (R_PPC64_JMP_SLOT, /* type */
457 0, /* rightshift */
458 0, /* size (0 = byte, 1 = short, 2 = long) */
459 0, /* bitsize */
460 FALSE, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_dont, /* complain_on_overflow */
463 ppc64_elf_unhandled_reloc, /* special_function */
464 "R_PPC64_JMP_SLOT", /* name */
465 FALSE, /* partial_inplace */
466 0, /* src_mask */
467 0, /* dst_mask */
468 FALSE), /* pcrel_offset */
470 /* Used only by the dynamic linker. When the object is run, this
471 doubleword64 is set to the load address of the object, plus the
472 addend. */
473 HOWTO (R_PPC64_RELATIVE, /* type */
474 0, /* rightshift */
475 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
476 64, /* bitsize */
477 FALSE, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_dont, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* special_function */
481 "R_PPC64_RELATIVE", /* name */
482 FALSE, /* partial_inplace */
483 0, /* src_mask */
484 ONES (64), /* dst_mask */
485 FALSE), /* pcrel_offset */
487 /* Like R_PPC64_ADDR32, but may be unaligned. */
488 HOWTO (R_PPC64_UADDR32, /* type */
489 0, /* rightshift */
490 2, /* size (0 = byte, 1 = short, 2 = long) */
491 32, /* bitsize */
492 FALSE, /* pc_relative */
493 0, /* bitpos */
494 complain_overflow_bitfield, /* complain_on_overflow */
495 bfd_elf_generic_reloc, /* special_function */
496 "R_PPC64_UADDR32", /* name */
497 FALSE, /* partial_inplace */
498 0, /* src_mask */
499 0xffffffff, /* dst_mask */
500 FALSE), /* pcrel_offset */
502 /* Like R_PPC64_ADDR16, but may be unaligned. */
503 HOWTO (R_PPC64_UADDR16, /* type */
504 0, /* rightshift */
505 1, /* size (0 = byte, 1 = short, 2 = long) */
506 16, /* bitsize */
507 FALSE, /* pc_relative */
508 0, /* bitpos */
509 complain_overflow_bitfield, /* complain_on_overflow */
510 bfd_elf_generic_reloc, /* special_function */
511 "R_PPC64_UADDR16", /* name */
512 FALSE, /* partial_inplace */
513 0, /* src_mask */
514 0xffff, /* dst_mask */
515 FALSE), /* pcrel_offset */
517 /* 32-bit PC relative. */
518 HOWTO (R_PPC64_REL32, /* type */
519 0, /* rightshift */
520 2, /* size (0 = byte, 1 = short, 2 = long) */
521 32, /* bitsize */
522 TRUE, /* pc_relative */
523 0, /* bitpos */
524 /* FIXME: Verify. Was complain_overflow_bitfield. */
525 complain_overflow_signed, /* complain_on_overflow */
526 bfd_elf_generic_reloc, /* special_function */
527 "R_PPC64_REL32", /* name */
528 FALSE, /* partial_inplace */
529 0, /* src_mask */
530 0xffffffff, /* dst_mask */
531 TRUE), /* pcrel_offset */
533 /* 32-bit relocation to the symbol's procedure linkage table. */
534 HOWTO (R_PPC64_PLT32, /* type */
535 0, /* rightshift */
536 2, /* size (0 = byte, 1 = short, 2 = long) */
537 32, /* bitsize */
538 FALSE, /* pc_relative */
539 0, /* bitpos */
540 complain_overflow_bitfield, /* complain_on_overflow */
541 ppc64_elf_unhandled_reloc, /* special_function */
542 "R_PPC64_PLT32", /* name */
543 FALSE, /* partial_inplace */
544 0, /* src_mask */
545 0xffffffff, /* dst_mask */
546 FALSE), /* pcrel_offset */
548 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
549 FIXME: R_PPC64_PLTREL32 not supported. */
550 HOWTO (R_PPC64_PLTREL32, /* type */
551 0, /* rightshift */
552 2, /* size (0 = byte, 1 = short, 2 = long) */
553 32, /* bitsize */
554 TRUE, /* pc_relative */
555 0, /* bitpos */
556 complain_overflow_signed, /* complain_on_overflow */
557 bfd_elf_generic_reloc, /* special_function */
558 "R_PPC64_PLTREL32", /* name */
559 FALSE, /* partial_inplace */
560 0, /* src_mask */
561 0xffffffff, /* dst_mask */
562 TRUE), /* pcrel_offset */
564 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
565 the symbol. */
566 HOWTO (R_PPC64_PLT16_LO, /* type */
567 0, /* rightshift */
568 1, /* size (0 = byte, 1 = short, 2 = long) */
569 16, /* bitsize */
570 FALSE, /* pc_relative */
571 0, /* bitpos */
572 complain_overflow_dont, /* complain_on_overflow */
573 ppc64_elf_unhandled_reloc, /* special_function */
574 "R_PPC64_PLT16_LO", /* name */
575 FALSE, /* partial_inplace */
576 0, /* src_mask */
577 0xffff, /* dst_mask */
578 FALSE), /* pcrel_offset */
580 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
581 the symbol. */
582 HOWTO (R_PPC64_PLT16_HI, /* type */
583 16, /* rightshift */
584 1, /* size (0 = byte, 1 = short, 2 = long) */
585 16, /* bitsize */
586 FALSE, /* pc_relative */
587 0, /* bitpos */
588 complain_overflow_dont, /* complain_on_overflow */
589 ppc64_elf_unhandled_reloc, /* special_function */
590 "R_PPC64_PLT16_HI", /* name */
591 FALSE, /* partial_inplace */
592 0, /* src_mask */
593 0xffff, /* dst_mask */
594 FALSE), /* pcrel_offset */
596 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
597 the symbol. */
598 HOWTO (R_PPC64_PLT16_HA, /* type */
599 16, /* rightshift */
600 1, /* size (0 = byte, 1 = short, 2 = long) */
601 16, /* bitsize */
602 FALSE, /* pc_relative */
603 0, /* bitpos */
604 complain_overflow_dont, /* complain_on_overflow */
605 ppc64_elf_unhandled_reloc, /* special_function */
606 "R_PPC64_PLT16_HA", /* name */
607 FALSE, /* partial_inplace */
608 0, /* src_mask */
609 0xffff, /* dst_mask */
610 FALSE), /* pcrel_offset */
612 /* 16-bit section relative relocation. */
613 HOWTO (R_PPC64_SECTOFF, /* type */
614 0, /* rightshift */
615 1, /* size (0 = byte, 1 = short, 2 = long) */
616 16, /* bitsize */
617 FALSE, /* pc_relative */
618 0, /* bitpos */
619 complain_overflow_bitfield, /* complain_on_overflow */
620 ppc64_elf_sectoff_reloc, /* special_function */
621 "R_PPC64_SECTOFF", /* name */
622 FALSE, /* partial_inplace */
623 0, /* src_mask */
624 0xffff, /* dst_mask */
625 FALSE), /* pcrel_offset */
627 /* Like R_PPC64_SECTOFF, but no overflow warning. */
628 HOWTO (R_PPC64_SECTOFF_LO, /* type */
629 0, /* rightshift */
630 1, /* size (0 = byte, 1 = short, 2 = long) */
631 16, /* bitsize */
632 FALSE, /* pc_relative */
633 0, /* bitpos */
634 complain_overflow_dont, /* complain_on_overflow */
635 ppc64_elf_sectoff_reloc, /* special_function */
636 "R_PPC64_SECTOFF_LO", /* name */
637 FALSE, /* partial_inplace */
638 0, /* src_mask */
639 0xffff, /* dst_mask */
640 FALSE), /* pcrel_offset */
642 /* 16-bit upper half section relative relocation. */
643 HOWTO (R_PPC64_SECTOFF_HI, /* type */
644 16, /* rightshift */
645 1, /* size (0 = byte, 1 = short, 2 = long) */
646 16, /* bitsize */
647 FALSE, /* pc_relative */
648 0, /* bitpos */
649 complain_overflow_dont, /* complain_on_overflow */
650 ppc64_elf_sectoff_reloc, /* special_function */
651 "R_PPC64_SECTOFF_HI", /* name */
652 FALSE, /* partial_inplace */
653 0, /* src_mask */
654 0xffff, /* dst_mask */
655 FALSE), /* pcrel_offset */
657 /* 16-bit upper half adjusted section relative relocation. */
658 HOWTO (R_PPC64_SECTOFF_HA, /* type */
659 16, /* rightshift */
660 1, /* size (0 = byte, 1 = short, 2 = long) */
661 16, /* bitsize */
662 FALSE, /* pc_relative */
663 0, /* bitpos */
664 complain_overflow_dont, /* complain_on_overflow */
665 ppc64_elf_sectoff_ha_reloc, /* special_function */
666 "R_PPC64_SECTOFF_HA", /* name */
667 FALSE, /* partial_inplace */
668 0, /* src_mask */
669 0xffff, /* dst_mask */
670 FALSE), /* pcrel_offset */
672 /* Like R_PPC64_REL24 without touching the two least significant bits. */
673 HOWTO (R_PPC64_REL30, /* type */
674 2, /* rightshift */
675 2, /* size (0 = byte, 1 = short, 2 = long) */
676 30, /* bitsize */
677 TRUE, /* pc_relative */
678 0, /* bitpos */
679 complain_overflow_dont, /* complain_on_overflow */
680 bfd_elf_generic_reloc, /* special_function */
681 "R_PPC64_REL30", /* name */
682 FALSE, /* partial_inplace */
683 0, /* src_mask */
684 0xfffffffc, /* dst_mask */
685 TRUE), /* pcrel_offset */
687 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
689 /* A standard 64-bit relocation. */
690 HOWTO (R_PPC64_ADDR64, /* type */
691 0, /* rightshift */
692 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
693 64, /* bitsize */
694 FALSE, /* pc_relative */
695 0, /* bitpos */
696 complain_overflow_dont, /* complain_on_overflow */
697 bfd_elf_generic_reloc, /* special_function */
698 "R_PPC64_ADDR64", /* name */
699 FALSE, /* partial_inplace */
700 0, /* src_mask */
701 ONES (64), /* dst_mask */
702 FALSE), /* pcrel_offset */
704 /* The bits 32-47 of an address. */
705 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
706 32, /* rightshift */
707 1, /* size (0 = byte, 1 = short, 2 = long) */
708 16, /* bitsize */
709 FALSE, /* pc_relative */
710 0, /* bitpos */
711 complain_overflow_dont, /* complain_on_overflow */
712 bfd_elf_generic_reloc, /* special_function */
713 "R_PPC64_ADDR16_HIGHER", /* name */
714 FALSE, /* partial_inplace */
715 0, /* src_mask */
716 0xffff, /* dst_mask */
717 FALSE), /* pcrel_offset */
719 /* The bits 32-47 of an address, plus 1 if the contents of the low
720 16 bits, treated as a signed number, is negative. */
721 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
722 32, /* rightshift */
723 1, /* size (0 = byte, 1 = short, 2 = long) */
724 16, /* bitsize */
725 FALSE, /* pc_relative */
726 0, /* bitpos */
727 complain_overflow_dont, /* complain_on_overflow */
728 ppc64_elf_ha_reloc, /* special_function */
729 "R_PPC64_ADDR16_HIGHERA", /* name */
730 FALSE, /* partial_inplace */
731 0, /* src_mask */
732 0xffff, /* dst_mask */
733 FALSE), /* pcrel_offset */
735 /* The bits 48-63 of an address. */
736 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
737 48, /* rightshift */
738 1, /* size (0 = byte, 1 = short, 2 = long) */
739 16, /* bitsize */
740 FALSE, /* pc_relative */
741 0, /* bitpos */
742 complain_overflow_dont, /* complain_on_overflow */
743 bfd_elf_generic_reloc, /* special_function */
744 "R_PPC64_ADDR16_HIGHEST", /* name */
745 FALSE, /* partial_inplace */
746 0, /* src_mask */
747 0xffff, /* dst_mask */
748 FALSE), /* pcrel_offset */
750 /* The bits 48-63 of an address, plus 1 if the contents of the low
751 16 bits, treated as a signed number, is negative. */
752 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
753 48, /* rightshift */
754 1, /* size (0 = byte, 1 = short, 2 = long) */
755 16, /* bitsize */
756 FALSE, /* pc_relative */
757 0, /* bitpos */
758 complain_overflow_dont, /* complain_on_overflow */
759 ppc64_elf_ha_reloc, /* special_function */
760 "R_PPC64_ADDR16_HIGHESTA", /* name */
761 FALSE, /* partial_inplace */
762 0, /* src_mask */
763 0xffff, /* dst_mask */
764 FALSE), /* pcrel_offset */
766 /* Like ADDR64, but may be unaligned. */
767 HOWTO (R_PPC64_UADDR64, /* type */
768 0, /* rightshift */
769 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
770 64, /* bitsize */
771 FALSE, /* pc_relative */
772 0, /* bitpos */
773 complain_overflow_dont, /* complain_on_overflow */
774 bfd_elf_generic_reloc, /* special_function */
775 "R_PPC64_UADDR64", /* name */
776 FALSE, /* partial_inplace */
777 0, /* src_mask */
778 ONES (64), /* dst_mask */
779 FALSE), /* pcrel_offset */
781 /* 64-bit relative relocation. */
782 HOWTO (R_PPC64_REL64, /* type */
783 0, /* rightshift */
784 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
785 64, /* bitsize */
786 TRUE, /* pc_relative */
787 0, /* bitpos */
788 complain_overflow_dont, /* complain_on_overflow */
789 bfd_elf_generic_reloc, /* special_function */
790 "R_PPC64_REL64", /* name */
791 FALSE, /* partial_inplace */
792 0, /* src_mask */
793 ONES (64), /* dst_mask */
794 TRUE), /* pcrel_offset */
796 /* 64-bit relocation to the symbol's procedure linkage table. */
797 HOWTO (R_PPC64_PLT64, /* type */
798 0, /* rightshift */
799 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
800 64, /* bitsize */
801 FALSE, /* pc_relative */
802 0, /* bitpos */
803 complain_overflow_dont, /* complain_on_overflow */
804 ppc64_elf_unhandled_reloc, /* special_function */
805 "R_PPC64_PLT64", /* name */
806 FALSE, /* partial_inplace */
807 0, /* src_mask */
808 ONES (64), /* dst_mask */
809 FALSE), /* pcrel_offset */
811 /* 64-bit PC relative relocation to the symbol's procedure linkage
812 table. */
813 /* FIXME: R_PPC64_PLTREL64 not supported. */
814 HOWTO (R_PPC64_PLTREL64, /* type */
815 0, /* rightshift */
816 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
817 64, /* bitsize */
818 TRUE, /* pc_relative */
819 0, /* bitpos */
820 complain_overflow_dont, /* complain_on_overflow */
821 ppc64_elf_unhandled_reloc, /* special_function */
822 "R_PPC64_PLTREL64", /* name */
823 FALSE, /* partial_inplace */
824 0, /* src_mask */
825 ONES (64), /* dst_mask */
826 TRUE), /* pcrel_offset */
828 /* 16 bit TOC-relative relocation. */
830 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
831 HOWTO (R_PPC64_TOC16, /* type */
832 0, /* rightshift */
833 1, /* size (0 = byte, 1 = short, 2 = long) */
834 16, /* bitsize */
835 FALSE, /* pc_relative */
836 0, /* bitpos */
837 complain_overflow_signed, /* complain_on_overflow */
838 ppc64_elf_toc_reloc, /* special_function */
839 "R_PPC64_TOC16", /* name */
840 FALSE, /* partial_inplace */
841 0, /* src_mask */
842 0xffff, /* dst_mask */
843 FALSE), /* pcrel_offset */
845 /* 16 bit TOC-relative relocation without overflow. */
847 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
848 HOWTO (R_PPC64_TOC16_LO, /* type */
849 0, /* rightshift */
850 1, /* size (0 = byte, 1 = short, 2 = long) */
851 16, /* bitsize */
852 FALSE, /* pc_relative */
853 0, /* bitpos */
854 complain_overflow_dont, /* complain_on_overflow */
855 ppc64_elf_toc_reloc, /* special_function */
856 "R_PPC64_TOC16_LO", /* name */
857 FALSE, /* partial_inplace */
858 0, /* src_mask */
859 0xffff, /* dst_mask */
860 FALSE), /* pcrel_offset */
862 /* 16 bit TOC-relative relocation, high 16 bits. */
864 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
865 HOWTO (R_PPC64_TOC16_HI, /* type */
866 16, /* rightshift */
867 1, /* size (0 = byte, 1 = short, 2 = long) */
868 16, /* bitsize */
869 FALSE, /* pc_relative */
870 0, /* bitpos */
871 complain_overflow_dont, /* complain_on_overflow */
872 ppc64_elf_toc_reloc, /* special_function */
873 "R_PPC64_TOC16_HI", /* name */
874 FALSE, /* partial_inplace */
875 0, /* src_mask */
876 0xffff, /* dst_mask */
877 FALSE), /* pcrel_offset */
879 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
880 contents of the low 16 bits, treated as a signed number, is
881 negative. */
883 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
884 HOWTO (R_PPC64_TOC16_HA, /* type */
885 16, /* rightshift */
886 1, /* size (0 = byte, 1 = short, 2 = long) */
887 16, /* bitsize */
888 FALSE, /* pc_relative */
889 0, /* bitpos */
890 complain_overflow_dont, /* complain_on_overflow */
891 ppc64_elf_toc_ha_reloc, /* special_function */
892 "R_PPC64_TOC16_HA", /* name */
893 FALSE, /* partial_inplace */
894 0, /* src_mask */
895 0xffff, /* dst_mask */
896 FALSE), /* pcrel_offset */
898 /* 64-bit relocation; insert value of TOC base (.TOC.). */
900 /* R_PPC64_TOC 51 doubleword64 .TOC. */
901 HOWTO (R_PPC64_TOC, /* type */
902 0, /* rightshift */
903 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
904 64, /* bitsize */
905 FALSE, /* pc_relative */
906 0, /* bitpos */
907 complain_overflow_bitfield, /* complain_on_overflow */
908 ppc64_elf_toc64_reloc, /* special_function */
909 "R_PPC64_TOC", /* name */
910 FALSE, /* partial_inplace */
911 0, /* src_mask */
912 ONES (64), /* dst_mask */
913 FALSE), /* pcrel_offset */
915 /* Like R_PPC64_GOT16, but also informs the link editor that the
916 value to relocate may (!) refer to a PLT entry which the link
917 editor (a) may replace with the symbol value. If the link editor
918 is unable to fully resolve the symbol, it may (b) create a PLT
919 entry and store the address to the new PLT entry in the GOT.
920 This permits lazy resolution of function symbols at run time.
921 The link editor may also skip all of this and just (c) emit a
922 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
923 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
924 HOWTO (R_PPC64_PLTGOT16, /* type */
925 0, /* rightshift */
926 1, /* size (0 = byte, 1 = short, 2 = long) */
927 16, /* bitsize */
928 FALSE, /* pc_relative */
929 0, /* bitpos */
930 complain_overflow_signed, /* complain_on_overflow */
931 ppc64_elf_unhandled_reloc, /* special_function */
932 "R_PPC64_PLTGOT16", /* name */
933 FALSE, /* partial_inplace */
934 0, /* src_mask */
935 0xffff, /* dst_mask */
936 FALSE), /* pcrel_offset */
938 /* Like R_PPC64_PLTGOT16, but without overflow. */
939 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
940 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
941 0, /* rightshift */
942 1, /* size (0 = byte, 1 = short, 2 = long) */
943 16, /* bitsize */
944 FALSE, /* pc_relative */
945 0, /* bitpos */
946 complain_overflow_dont, /* complain_on_overflow */
947 ppc64_elf_unhandled_reloc, /* special_function */
948 "R_PPC64_PLTGOT16_LO", /* name */
949 FALSE, /* partial_inplace */
950 0, /* src_mask */
951 0xffff, /* dst_mask */
952 FALSE), /* pcrel_offset */
954 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
955 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
956 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
957 16, /* rightshift */
958 1, /* size (0 = byte, 1 = short, 2 = long) */
959 16, /* bitsize */
960 FALSE, /* pc_relative */
961 0, /* bitpos */
962 complain_overflow_dont, /* complain_on_overflow */
963 ppc64_elf_unhandled_reloc, /* special_function */
964 "R_PPC64_PLTGOT16_HI", /* name */
965 FALSE, /* partial_inplace */
966 0, /* src_mask */
967 0xffff, /* dst_mask */
968 FALSE), /* pcrel_offset */
970 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
971 1 if the contents of the low 16 bits, treated as a signed number,
972 is negative. */
973 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
974 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
975 16, /* rightshift */
976 1, /* size (0 = byte, 1 = short, 2 = long) */
977 16, /* bitsize */
978 FALSE, /* pc_relative */
979 0, /* bitpos */
980 complain_overflow_dont,/* complain_on_overflow */
981 ppc64_elf_unhandled_reloc, /* special_function */
982 "R_PPC64_PLTGOT16_HA", /* name */
983 FALSE, /* partial_inplace */
984 0, /* src_mask */
985 0xffff, /* dst_mask */
986 FALSE), /* pcrel_offset */
988 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
989 HOWTO (R_PPC64_ADDR16_DS, /* type */
990 0, /* rightshift */
991 1, /* size (0 = byte, 1 = short, 2 = long) */
992 16, /* bitsize */
993 FALSE, /* pc_relative */
994 0, /* bitpos */
995 complain_overflow_bitfield, /* complain_on_overflow */
996 bfd_elf_generic_reloc, /* special_function */
997 "R_PPC64_ADDR16_DS", /* name */
998 FALSE, /* partial_inplace */
999 0, /* src_mask */
1000 0xfffc, /* dst_mask */
1001 FALSE), /* pcrel_offset */
1003 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1004 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1005 0, /* rightshift */
1006 1, /* size (0 = byte, 1 = short, 2 = long) */
1007 16, /* bitsize */
1008 FALSE, /* pc_relative */
1009 0, /* bitpos */
1010 complain_overflow_dont,/* complain_on_overflow */
1011 bfd_elf_generic_reloc, /* special_function */
1012 "R_PPC64_ADDR16_LO_DS",/* name */
1013 FALSE, /* partial_inplace */
1014 0, /* src_mask */
1015 0xfffc, /* dst_mask */
1016 FALSE), /* pcrel_offset */
1018 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1019 HOWTO (R_PPC64_GOT16_DS, /* type */
1020 0, /* rightshift */
1021 1, /* size (0 = byte, 1 = short, 2 = long) */
1022 16, /* bitsize */
1023 FALSE, /* pc_relative */
1024 0, /* bitpos */
1025 complain_overflow_signed, /* complain_on_overflow */
1026 ppc64_elf_unhandled_reloc, /* special_function */
1027 "R_PPC64_GOT16_DS", /* name */
1028 FALSE, /* partial_inplace */
1029 0, /* src_mask */
1030 0xfffc, /* dst_mask */
1031 FALSE), /* pcrel_offset */
1033 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1034 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1035 0, /* rightshift */
1036 1, /* size (0 = byte, 1 = short, 2 = long) */
1037 16, /* bitsize */
1038 FALSE, /* pc_relative */
1039 0, /* bitpos */
1040 complain_overflow_dont, /* complain_on_overflow */
1041 ppc64_elf_unhandled_reloc, /* special_function */
1042 "R_PPC64_GOT16_LO_DS", /* name */
1043 FALSE, /* partial_inplace */
1044 0, /* src_mask */
1045 0xfffc, /* dst_mask */
1046 FALSE), /* pcrel_offset */
1048 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1049 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1050 0, /* rightshift */
1051 1, /* size (0 = byte, 1 = short, 2 = long) */
1052 16, /* bitsize */
1053 FALSE, /* pc_relative */
1054 0, /* bitpos */
1055 complain_overflow_dont, /* complain_on_overflow */
1056 ppc64_elf_unhandled_reloc, /* special_function */
1057 "R_PPC64_PLT16_LO_DS", /* name */
1058 FALSE, /* partial_inplace */
1059 0, /* src_mask */
1060 0xfffc, /* dst_mask */
1061 FALSE), /* pcrel_offset */
1063 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1064 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1065 0, /* rightshift */
1066 1, /* size (0 = byte, 1 = short, 2 = long) */
1067 16, /* bitsize */
1068 FALSE, /* pc_relative */
1069 0, /* bitpos */
1070 complain_overflow_bitfield, /* complain_on_overflow */
1071 ppc64_elf_sectoff_reloc, /* special_function */
1072 "R_PPC64_SECTOFF_DS", /* name */
1073 FALSE, /* partial_inplace */
1074 0, /* src_mask */
1075 0xfffc, /* dst_mask */
1076 FALSE), /* pcrel_offset */
1078 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1079 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1080 0, /* rightshift */
1081 1, /* size (0 = byte, 1 = short, 2 = long) */
1082 16, /* bitsize */
1083 FALSE, /* pc_relative */
1084 0, /* bitpos */
1085 complain_overflow_dont, /* complain_on_overflow */
1086 ppc64_elf_sectoff_reloc, /* special_function */
1087 "R_PPC64_SECTOFF_LO_DS",/* name */
1088 FALSE, /* partial_inplace */
1089 0, /* src_mask */
1090 0xfffc, /* dst_mask */
1091 FALSE), /* pcrel_offset */
1093 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1094 HOWTO (R_PPC64_TOC16_DS, /* type */
1095 0, /* rightshift */
1096 1, /* size (0 = byte, 1 = short, 2 = long) */
1097 16, /* bitsize */
1098 FALSE, /* pc_relative */
1099 0, /* bitpos */
1100 complain_overflow_signed, /* complain_on_overflow */
1101 ppc64_elf_toc_reloc, /* special_function */
1102 "R_PPC64_TOC16_DS", /* name */
1103 FALSE, /* partial_inplace */
1104 0, /* src_mask */
1105 0xfffc, /* dst_mask */
1106 FALSE), /* pcrel_offset */
1108 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1109 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1110 0, /* rightshift */
1111 1, /* size (0 = byte, 1 = short, 2 = long) */
1112 16, /* bitsize */
1113 FALSE, /* pc_relative */
1114 0, /* bitpos */
1115 complain_overflow_dont, /* complain_on_overflow */
1116 ppc64_elf_toc_reloc, /* special_function */
1117 "R_PPC64_TOC16_LO_DS", /* name */
1118 FALSE, /* partial_inplace */
1119 0, /* src_mask */
1120 0xfffc, /* dst_mask */
1121 FALSE), /* pcrel_offset */
1123 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1124 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1125 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1126 0, /* rightshift */
1127 1, /* size (0 = byte, 1 = short, 2 = long) */
1128 16, /* bitsize */
1129 FALSE, /* pc_relative */
1130 0, /* bitpos */
1131 complain_overflow_signed, /* complain_on_overflow */
1132 ppc64_elf_unhandled_reloc, /* special_function */
1133 "R_PPC64_PLTGOT16_DS", /* name */
1134 FALSE, /* partial_inplace */
1135 0, /* src_mask */
1136 0xfffc, /* dst_mask */
1137 FALSE), /* pcrel_offset */
1139 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1140 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1141 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1142 0, /* rightshift */
1143 1, /* size (0 = byte, 1 = short, 2 = long) */
1144 16, /* bitsize */
1145 FALSE, /* pc_relative */
1146 0, /* bitpos */
1147 complain_overflow_dont, /* complain_on_overflow */
1148 ppc64_elf_unhandled_reloc, /* special_function */
1149 "R_PPC64_PLTGOT16_LO_DS",/* name */
1150 FALSE, /* partial_inplace */
1151 0, /* src_mask */
1152 0xfffc, /* dst_mask */
1153 FALSE), /* pcrel_offset */
1155 /* GNU extension to record C++ vtable hierarchy. */
1156 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1157 0, /* rightshift */
1158 0, /* size (0 = byte, 1 = short, 2 = long) */
1159 0, /* bitsize */
1160 FALSE, /* pc_relative */
1161 0, /* bitpos */
1162 complain_overflow_dont, /* complain_on_overflow */
1163 NULL, /* special_function */
1164 "R_PPC64_GNU_VTINHERIT", /* name */
1165 FALSE, /* partial_inplace */
1166 0, /* src_mask */
1167 0, /* dst_mask */
1168 FALSE), /* pcrel_offset */
1170 /* GNU extension to record C++ vtable member usage. */
1171 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1172 0, /* rightshift */
1173 0, /* size (0 = byte, 1 = short, 2 = long) */
1174 0, /* bitsize */
1175 FALSE, /* pc_relative */
1176 0, /* bitpos */
1177 complain_overflow_dont, /* complain_on_overflow */
1178 NULL, /* special_function */
1179 "R_PPC64_GNU_VTENTRY", /* name */
1180 FALSE, /* partial_inplace */
1181 0, /* src_mask */
1182 0, /* dst_mask */
1183 FALSE), /* pcrel_offset */
1187 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
1188 be done. */
1190 static void
1191 ppc_howto_init ()
1193 unsigned int i, type;
1195 for (i = 0;
1196 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1197 i++)
1199 type = ppc64_elf_howto_raw[i].type;
1200 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1201 / sizeof (ppc64_elf_howto_table[0])));
1202 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1206 static reloc_howto_type *
1207 ppc64_elf_reloc_type_lookup (abfd, code)
1208 bfd *abfd ATTRIBUTE_UNUSED;
1209 bfd_reloc_code_real_type code;
1211 enum elf_ppc64_reloc_type ppc_reloc = R_PPC64_NONE;
1213 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1214 /* Initialize howto table if needed. */
1215 ppc_howto_init ();
1217 switch ((int) code)
1219 default:
1220 return (reloc_howto_type *) NULL;
1222 case BFD_RELOC_NONE: ppc_reloc = R_PPC64_NONE;
1223 break;
1224 case BFD_RELOC_32: ppc_reloc = R_PPC64_ADDR32;
1225 break;
1226 case BFD_RELOC_PPC_BA26: ppc_reloc = R_PPC64_ADDR24;
1227 break;
1228 case BFD_RELOC_16: ppc_reloc = R_PPC64_ADDR16;
1229 break;
1230 case BFD_RELOC_LO16: ppc_reloc = R_PPC64_ADDR16_LO;
1231 break;
1232 case BFD_RELOC_HI16: ppc_reloc = R_PPC64_ADDR16_HI;
1233 break;
1234 case BFD_RELOC_HI16_S: ppc_reloc = R_PPC64_ADDR16_HA;
1235 break;
1236 case BFD_RELOC_PPC_BA16: ppc_reloc = R_PPC64_ADDR14;
1237 break;
1238 case BFD_RELOC_PPC_BA16_BRTAKEN: ppc_reloc = R_PPC64_ADDR14_BRTAKEN;
1239 break;
1240 case BFD_RELOC_PPC_BA16_BRNTAKEN: ppc_reloc = R_PPC64_ADDR14_BRNTAKEN;
1241 break;
1242 case BFD_RELOC_PPC_B26: ppc_reloc = R_PPC64_REL24;
1243 break;
1244 case BFD_RELOC_PPC_B16: ppc_reloc = R_PPC64_REL14;
1245 break;
1246 case BFD_RELOC_PPC_B16_BRTAKEN: ppc_reloc = R_PPC64_REL14_BRTAKEN;
1247 break;
1248 case BFD_RELOC_PPC_B16_BRNTAKEN: ppc_reloc = R_PPC64_REL14_BRNTAKEN;
1249 break;
1250 case BFD_RELOC_16_GOTOFF: ppc_reloc = R_PPC64_GOT16;
1251 break;
1252 case BFD_RELOC_LO16_GOTOFF: ppc_reloc = R_PPC64_GOT16_LO;
1253 break;
1254 case BFD_RELOC_HI16_GOTOFF: ppc_reloc = R_PPC64_GOT16_HI;
1255 break;
1256 case BFD_RELOC_HI16_S_GOTOFF: ppc_reloc = R_PPC64_GOT16_HA;
1257 break;
1258 case BFD_RELOC_PPC_COPY: ppc_reloc = R_PPC64_COPY;
1259 break;
1260 case BFD_RELOC_PPC_GLOB_DAT: ppc_reloc = R_PPC64_GLOB_DAT;
1261 break;
1262 case BFD_RELOC_32_PCREL: ppc_reloc = R_PPC64_REL32;
1263 break;
1264 case BFD_RELOC_32_PLTOFF: ppc_reloc = R_PPC64_PLT32;
1265 break;
1266 case BFD_RELOC_32_PLT_PCREL: ppc_reloc = R_PPC64_PLTREL32;
1267 break;
1268 case BFD_RELOC_LO16_PLTOFF: ppc_reloc = R_PPC64_PLT16_LO;
1269 break;
1270 case BFD_RELOC_HI16_PLTOFF: ppc_reloc = R_PPC64_PLT16_HI;
1271 break;
1272 case BFD_RELOC_HI16_S_PLTOFF: ppc_reloc = R_PPC64_PLT16_HA;
1273 break;
1274 case BFD_RELOC_16_BASEREL: ppc_reloc = R_PPC64_SECTOFF;
1275 break;
1276 case BFD_RELOC_LO16_BASEREL: ppc_reloc = R_PPC64_SECTOFF_LO;
1277 break;
1278 case BFD_RELOC_HI16_BASEREL: ppc_reloc = R_PPC64_SECTOFF_HI;
1279 break;
1280 case BFD_RELOC_HI16_S_BASEREL: ppc_reloc = R_PPC64_SECTOFF_HA;
1281 break;
1282 case BFD_RELOC_CTOR: ppc_reloc = R_PPC64_ADDR64;
1283 break;
1284 case BFD_RELOC_64: ppc_reloc = R_PPC64_ADDR64;
1285 break;
1286 case BFD_RELOC_PPC64_HIGHER: ppc_reloc = R_PPC64_ADDR16_HIGHER;
1287 break;
1288 case BFD_RELOC_PPC64_HIGHER_S: ppc_reloc = R_PPC64_ADDR16_HIGHERA;
1289 break;
1290 case BFD_RELOC_PPC64_HIGHEST: ppc_reloc = R_PPC64_ADDR16_HIGHEST;
1291 break;
1292 case BFD_RELOC_PPC64_HIGHEST_S: ppc_reloc = R_PPC64_ADDR16_HIGHESTA;
1293 break;
1294 case BFD_RELOC_64_PCREL: ppc_reloc = R_PPC64_REL64;
1295 break;
1296 case BFD_RELOC_64_PLTOFF: ppc_reloc = R_PPC64_PLT64;
1297 break;
1298 case BFD_RELOC_64_PLT_PCREL: ppc_reloc = R_PPC64_PLTREL64;
1299 break;
1300 case BFD_RELOC_PPC_TOC16: ppc_reloc = R_PPC64_TOC16;
1301 break;
1302 case BFD_RELOC_PPC64_TOC16_LO: ppc_reloc = R_PPC64_TOC16_LO;
1303 break;
1304 case BFD_RELOC_PPC64_TOC16_HI: ppc_reloc = R_PPC64_TOC16_HI;
1305 break;
1306 case BFD_RELOC_PPC64_TOC16_HA: ppc_reloc = R_PPC64_TOC16_HA;
1307 break;
1308 case BFD_RELOC_PPC64_TOC: ppc_reloc = R_PPC64_TOC;
1309 break;
1310 case BFD_RELOC_PPC64_PLTGOT16: ppc_reloc = R_PPC64_PLTGOT16;
1311 break;
1312 case BFD_RELOC_PPC64_PLTGOT16_LO: ppc_reloc = R_PPC64_PLTGOT16_LO;
1313 break;
1314 case BFD_RELOC_PPC64_PLTGOT16_HI: ppc_reloc = R_PPC64_PLTGOT16_HI;
1315 break;
1316 case BFD_RELOC_PPC64_PLTGOT16_HA: ppc_reloc = R_PPC64_PLTGOT16_HA;
1317 break;
1318 case BFD_RELOC_PPC64_ADDR16_DS: ppc_reloc = R_PPC64_ADDR16_DS;
1319 break;
1320 case BFD_RELOC_PPC64_ADDR16_LO_DS: ppc_reloc = R_PPC64_ADDR16_LO_DS;
1321 break;
1322 case BFD_RELOC_PPC64_GOT16_DS: ppc_reloc = R_PPC64_GOT16_DS;
1323 break;
1324 case BFD_RELOC_PPC64_GOT16_LO_DS: ppc_reloc = R_PPC64_GOT16_LO_DS;
1325 break;
1326 case BFD_RELOC_PPC64_PLT16_LO_DS: ppc_reloc = R_PPC64_PLT16_LO_DS;
1327 break;
1328 case BFD_RELOC_PPC64_SECTOFF_DS: ppc_reloc = R_PPC64_SECTOFF_DS;
1329 break;
1330 case BFD_RELOC_PPC64_SECTOFF_LO_DS: ppc_reloc = R_PPC64_SECTOFF_LO_DS;
1331 break;
1332 case BFD_RELOC_PPC64_TOC16_DS: ppc_reloc = R_PPC64_TOC16_DS;
1333 break;
1334 case BFD_RELOC_PPC64_TOC16_LO_DS: ppc_reloc = R_PPC64_TOC16_LO_DS;
1335 break;
1336 case BFD_RELOC_PPC64_PLTGOT16_DS: ppc_reloc = R_PPC64_PLTGOT16_DS;
1337 break;
1338 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: ppc_reloc = R_PPC64_PLTGOT16_LO_DS;
1339 break;
1340 case BFD_RELOC_VTABLE_INHERIT: ppc_reloc = R_PPC64_GNU_VTINHERIT;
1341 break;
1342 case BFD_RELOC_VTABLE_ENTRY: ppc_reloc = R_PPC64_GNU_VTENTRY;
1343 break;
1346 return ppc64_elf_howto_table[(int) ppc_reloc];
1349 /* Set the howto pointer for a PowerPC ELF reloc. */
1351 static void
1352 ppc64_elf_info_to_howto (abfd, cache_ptr, dst)
1353 bfd *abfd ATTRIBUTE_UNUSED;
1354 arelent *cache_ptr;
1355 Elf_Internal_Rela *dst;
1357 unsigned int type;
1359 /* Initialize howto table if needed. */
1360 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1361 ppc_howto_init ();
1363 type = ELF64_R_TYPE (dst->r_info);
1364 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1365 / sizeof (ppc64_elf_howto_table[0])));
1366 cache_ptr->howto = ppc64_elf_howto_table[type];
1369 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1371 static bfd_reloc_status_type
1372 ppc64_elf_ha_reloc (abfd, reloc_entry, symbol, data,
1373 input_section, output_bfd, error_message)
1374 bfd *abfd;
1375 arelent *reloc_entry;
1376 asymbol *symbol;
1377 PTR data;
1378 asection *input_section;
1379 bfd *output_bfd;
1380 char **error_message;
1382 /* If this is a relocatable link (output_bfd test tells us), just
1383 call the generic function. Any adjustment will be done at final
1384 link time. */
1385 if (output_bfd != NULL)
1386 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1387 input_section, output_bfd, error_message);
1389 /* Adjust the addend for sign extension of the low 16 bits.
1390 We won't actually be using the low 16 bits, so trashing them
1391 doesn't matter. */
1392 reloc_entry->addend += 0x8000;
1393 return bfd_reloc_continue;
1396 static bfd_reloc_status_type
1397 ppc64_elf_brtaken_reloc (abfd, reloc_entry, symbol, data,
1398 input_section, output_bfd, error_message)
1399 bfd *abfd;
1400 arelent *reloc_entry;
1401 asymbol *symbol;
1402 PTR data;
1403 asection *input_section;
1404 bfd *output_bfd;
1405 char **error_message;
1407 long insn;
1408 enum elf_ppc64_reloc_type r_type;
1409 bfd_size_type octets;
1410 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
1411 bfd_boolean is_power4 = FALSE;
1413 /* If this is a relocatable link (output_bfd test tells us), just
1414 call the generic function. Any adjustment will be done at final
1415 link time. */
1416 if (output_bfd != NULL)
1417 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1418 input_section, output_bfd, error_message);
1420 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1421 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1422 insn &= ~(0x01 << 21);
1423 r_type = (enum elf_ppc64_reloc_type) reloc_entry->howto->type;
1424 if (r_type == R_PPC64_ADDR14_BRTAKEN
1425 || r_type == R_PPC64_REL14_BRTAKEN)
1426 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1428 if (is_power4)
1430 /* Set 'a' bit. This is 0b00010 in BO field for branch
1431 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1432 for branch on CTR insns (BO == 1a00t or 1a01t). */
1433 if ((insn & (0x14 << 21)) == (0x04 << 21))
1434 insn |= 0x02 << 21;
1435 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1436 insn |= 0x08 << 21;
1437 else
1438 return bfd_reloc_continue;
1440 else
1442 bfd_vma target = 0;
1443 bfd_vma from;
1445 if (!bfd_is_com_section (symbol->section))
1446 target = symbol->value;
1447 target += symbol->section->output_section->vma;
1448 target += symbol->section->output_offset;
1449 target += reloc_entry->addend;
1451 from = (reloc_entry->address
1452 + input_section->output_offset
1453 + input_section->output_section->vma);
1455 /* Invert 'y' bit if not the default. */
1456 if ((bfd_signed_vma) (target - from) < 0)
1457 insn ^= 0x01 << 21;
1459 bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + octets);
1460 return bfd_reloc_continue;
1463 static bfd_reloc_status_type
1464 ppc64_elf_sectoff_reloc (abfd, reloc_entry, symbol, data,
1465 input_section, output_bfd, error_message)
1466 bfd *abfd;
1467 arelent *reloc_entry;
1468 asymbol *symbol;
1469 PTR data;
1470 asection *input_section;
1471 bfd *output_bfd;
1472 char **error_message;
1474 /* If this is a relocatable link (output_bfd test tells us), just
1475 call the generic function. Any adjustment will be done at final
1476 link time. */
1477 if (output_bfd != NULL)
1478 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1479 input_section, output_bfd, error_message);
1481 /* Subtract the symbol section base address. */
1482 reloc_entry->addend -= symbol->section->output_section->vma;
1483 return bfd_reloc_continue;
1486 static bfd_reloc_status_type
1487 ppc64_elf_sectoff_ha_reloc (abfd, reloc_entry, symbol, data,
1488 input_section, output_bfd, error_message)
1489 bfd *abfd;
1490 arelent *reloc_entry;
1491 asymbol *symbol;
1492 PTR data;
1493 asection *input_section;
1494 bfd *output_bfd;
1495 char **error_message;
1497 /* If this is a relocatable link (output_bfd test tells us), just
1498 call the generic function. Any adjustment will be done at final
1499 link time. */
1500 if (output_bfd != NULL)
1501 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1502 input_section, output_bfd, error_message);
1504 /* Subtract the symbol section base address. */
1505 reloc_entry->addend -= symbol->section->output_section->vma;
1507 /* Adjust the addend for sign extension of the low 16 bits. */
1508 reloc_entry->addend += 0x8000;
1509 return bfd_reloc_continue;
1512 static bfd_reloc_status_type
1513 ppc64_elf_toc_reloc (abfd, reloc_entry, symbol, data,
1514 input_section, output_bfd, error_message)
1515 bfd *abfd;
1516 arelent *reloc_entry;
1517 asymbol *symbol;
1518 PTR data;
1519 asection *input_section;
1520 bfd *output_bfd;
1521 char **error_message;
1523 bfd_vma TOCstart;
1525 /* If this is a relocatable link (output_bfd test tells us), just
1526 call the generic function. Any adjustment will be done at final
1527 link time. */
1528 if (output_bfd != NULL)
1529 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1530 input_section, output_bfd, error_message);
1532 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1533 if (TOCstart == 0)
1534 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1536 /* Subtract the TOC base address. */
1537 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1538 return bfd_reloc_continue;
1541 static bfd_reloc_status_type
1542 ppc64_elf_toc_ha_reloc (abfd, reloc_entry, symbol, data,
1543 input_section, output_bfd, error_message)
1544 bfd *abfd;
1545 arelent *reloc_entry;
1546 asymbol *symbol;
1547 PTR data;
1548 asection *input_section;
1549 bfd *output_bfd;
1550 char **error_message;
1552 bfd_vma TOCstart;
1554 /* If this is a relocatable link (output_bfd test tells us), just
1555 call the generic function. Any adjustment will be done at final
1556 link time. */
1557 if (output_bfd != NULL)
1558 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1559 input_section, output_bfd, error_message);
1561 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1562 if (TOCstart == 0)
1563 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1565 /* Subtract the TOC base address. */
1566 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1568 /* Adjust the addend for sign extension of the low 16 bits. */
1569 reloc_entry->addend += 0x8000;
1570 return bfd_reloc_continue;
1573 static bfd_reloc_status_type
1574 ppc64_elf_toc64_reloc (abfd, reloc_entry, symbol, data,
1575 input_section, output_bfd, error_message)
1576 bfd *abfd;
1577 arelent *reloc_entry;
1578 asymbol *symbol;
1579 PTR data;
1580 asection *input_section;
1581 bfd *output_bfd;
1582 char **error_message;
1584 bfd_vma TOCstart;
1585 bfd_size_type octets;
1587 /* If this is a relocatable link (output_bfd test tells us), just
1588 call the generic function. Any adjustment will be done at final
1589 link time. */
1590 if (output_bfd != NULL)
1591 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1592 input_section, output_bfd, error_message);
1594 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1595 if (TOCstart == 0)
1596 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
1598 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
1599 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1600 return bfd_reloc_ok;
1603 static bfd_reloc_status_type
1604 ppc64_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
1605 input_section, output_bfd, error_message)
1606 bfd *abfd;
1607 arelent *reloc_entry;
1608 asymbol *symbol;
1609 PTR data;
1610 asection *input_section;
1611 bfd *output_bfd;
1612 char **error_message;
1614 /* If this is a relocatable link (output_bfd test tells us), just
1615 call the generic function. Any adjustment will be done at final
1616 link time. */
1617 if (output_bfd != NULL)
1618 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1619 input_section, output_bfd, error_message);
1621 if (error_message != NULL)
1623 static char buf[60];
1624 sprintf (buf, "generic linker can't handle %s",
1625 reloc_entry->howto->name);
1626 *error_message = buf;
1628 return bfd_reloc_dangerous;
1631 /* Fix bad default arch selected for a 64 bit input bfd when the
1632 default is 32 bit. */
1634 static bfd_boolean
1635 ppc64_elf_object_p (abfd)
1636 bfd *abfd;
1638 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
1640 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1642 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1644 /* Relies on arch after 32 bit default being 64 bit default. */
1645 abfd->arch_info = abfd->arch_info->next;
1646 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1649 return TRUE;
1652 /* Merge backend specific data from an object file to the output
1653 object file when linking. */
1655 static bfd_boolean
1656 ppc64_elf_merge_private_bfd_data (ibfd, obfd)
1657 bfd *ibfd;
1658 bfd *obfd;
1660 /* Check if we have the same endianess. */
1661 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1662 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
1663 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1665 const char *msg;
1667 if (bfd_big_endian (ibfd))
1668 msg = _("%s: compiled for a big endian system and target is little endian");
1669 else
1670 msg = _("%s: compiled for a little endian system and target is big endian");
1672 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
1674 bfd_set_error (bfd_error_wrong_format);
1675 return FALSE;
1678 return TRUE;
1681 struct _ppc64_elf_section_data
1683 struct bfd_elf_section_data elf;
1684 union
1686 asection **func_sec;
1687 long *adjust;
1688 } opd;
1691 #define ppc64_elf_section_data(sec) \
1692 ((struct _ppc64_elf_section_data *) (sec)->used_by_bfd)
1694 static bfd_boolean
1695 ppc64_elf_new_section_hook (abfd, sec)
1696 bfd *abfd;
1697 asection *sec;
1699 struct _ppc64_elf_section_data *sdata;
1700 bfd_size_type amt = sizeof (*sdata);
1702 sdata = (struct _ppc64_elf_section_data *) bfd_zalloc (abfd, amt);
1703 if (sdata == NULL)
1704 return FALSE;
1705 sec->used_by_bfd = (PTR) sdata;
1707 return _bfd_elf_new_section_hook (abfd, sec);
1710 /* The following functions are specific to the ELF linker, while
1711 functions above are used generally. Those named ppc64_elf_* are
1712 called by the main ELF linker code. They appear in this file more
1713 or less in the order in which they are called. eg.
1714 ppc64_elf_check_relocs is called early in the link process,
1715 ppc64_elf_finish_dynamic_sections is one of the last functions
1716 called.
1718 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
1719 functions have both a function code symbol and a function descriptor
1720 symbol. A call to foo in a relocatable object file looks like:
1722 . .text
1723 . x:
1724 . bl .foo
1725 . nop
1727 The function definition in another object file might be:
1729 . .section .opd
1730 . foo: .quad .foo
1731 . .quad .TOC.@tocbase
1732 . .quad 0
1734 . .text
1735 . .foo: blr
1737 When the linker resolves the call during a static link, the branch
1738 unsurprisingly just goes to .foo and the .opd information is unused.
1739 If the function definition is in a shared library, things are a little
1740 different: The call goes via a plt call stub, the opd information gets
1741 copied to the plt, and the linker patches the nop.
1743 . x:
1744 . bl .foo_stub
1745 . ld 2,40(1)
1748 . .foo_stub:
1749 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
1750 . addi 12,12,Lfoo@toc@l # is slightly optimised, but
1751 . std 2,40(1) # this is the general idea
1752 . ld 11,0(12)
1753 . ld 2,8(12)
1754 . mtctr 11
1755 . ld 11,16(12)
1756 . bctr
1758 . .section .plt
1759 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
1761 The "reloc ()" notation is supposed to indicate that the linker emits
1762 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
1763 copying.
1765 What are the difficulties here? Well, firstly, the relocations
1766 examined by the linker in check_relocs are against the function code
1767 sym .foo, while the dynamic relocation in the plt is emitted against
1768 the function descriptor symbol, foo. Somewhere along the line, we need
1769 to carefully copy dynamic link information from one symbol to the other.
1770 Secondly, the generic part of the elf linker will make .foo a dynamic
1771 symbol as is normal for most other backends. We need foo dynamic
1772 instead, at least for an application final link. However, when
1773 creating a shared library containing foo, we need to have both symbols
1774 dynamic so that references to .foo are satisfied during the early
1775 stages of linking. Otherwise the linker might decide to pull in a
1776 definition from some other object, eg. a static library. */
1778 /* The linker needs to keep track of the number of relocs that it
1779 decides to copy as dynamic relocs in check_relocs for each symbol.
1780 This is so that it can later discard them if they are found to be
1781 unnecessary. We store the information in a field extending the
1782 regular ELF linker hash table. */
1784 struct ppc_dyn_relocs
1786 struct ppc_dyn_relocs *next;
1788 /* The input section of the reloc. */
1789 asection *sec;
1791 /* Total number of relocs copied for the input section. */
1792 bfd_size_type count;
1794 /* Number of pc-relative relocs copied for the input section. */
1795 bfd_size_type pc_count;
1798 /* Of those relocs that might be copied as dynamic relocs, this macro
1799 selects between relative and absolute types. */
1801 #define IS_ABSOLUTE_RELOC(RTYPE) \
1802 ((RTYPE) != R_PPC64_REL32 \
1803 && (RTYPE) != R_PPC64_REL64 \
1804 && (RTYPE) != R_PPC64_REL30)
1806 /* Section name for stubs is the associated section name plus this
1807 string. */
1808 #define STUB_SUFFIX ".stub"
1810 /* Linker stubs.
1811 ppc_stub_long_branch:
1812 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
1813 destination, but a 24 bit branch in a stub section will reach.
1814 . b dest
1816 ppc_stub_plt_branch:
1817 Similar to the above, but a 24 bit branch in the stub section won't
1818 reach its destination.
1819 . addis %r12,%r2,xxx@toc@ha
1820 . ld %r11,xxx@toc@l(%r12)
1821 . mtctr %r11
1822 . bctr
1824 ppc_stub_plt_call:
1825 Used to call a function in a shared library.
1826 . addis %r12,%r2,xxx@toc@ha
1827 . std %r2,40(%r1)
1828 . ld %r11,xxx+0@toc@l(%r12)
1829 . ld %r2,xxx+8@toc@l(%r12)
1830 . mtctr %r11
1831 . ld %r11,xxx+16@toc@l(%r12)
1832 . bctr
1835 enum ppc_stub_type {
1836 ppc_stub_none,
1837 ppc_stub_long_branch,
1838 ppc_stub_plt_branch,
1839 ppc_stub_plt_call
1842 struct ppc_stub_hash_entry {
1844 /* Base hash table entry structure. */
1845 struct bfd_hash_entry root;
1847 /* The stub section. */
1848 asection *stub_sec;
1850 /* Offset within stub_sec of the beginning of this stub. */
1851 bfd_vma stub_offset;
1853 /* Given the symbol's value and its section we can determine its final
1854 value when building the stubs (so the stub knows where to jump. */
1855 bfd_vma target_value;
1856 asection *target_section;
1858 enum ppc_stub_type stub_type;
1860 /* The symbol table entry, if any, that this was derived from. */
1861 struct ppc_link_hash_entry *h;
1863 /* Where this stub is being called from, or, in the case of combined
1864 stub sections, the first input section in the group. */
1865 asection *id_sec;
1868 struct ppc_branch_hash_entry {
1870 /* Base hash table entry structure. */
1871 struct bfd_hash_entry root;
1873 /* Offset within .branch_lt. */
1874 unsigned int offset;
1876 /* Generation marker. */
1877 unsigned int iter;
1880 struct ppc_link_hash_entry
1882 struct elf_link_hash_entry elf;
1884 /* A pointer to the most recently used stub hash entry against this
1885 symbol. */
1886 struct ppc_stub_hash_entry *stub_cache;
1888 /* Track dynamic relocs copied for this symbol. */
1889 struct ppc_dyn_relocs *dyn_relocs;
1891 /* Link between function code and descriptor symbols. */
1892 struct elf_link_hash_entry *oh;
1894 /* Flag function code and descriptor symbols. */
1895 unsigned int is_func:1;
1896 unsigned int is_func_descriptor:1;
1897 unsigned int is_entry:1;
1900 /* ppc64 ELF linker hash table. */
1902 struct ppc_link_hash_table
1904 struct elf_link_hash_table elf;
1906 /* The stub hash table. */
1907 struct bfd_hash_table stub_hash_table;
1909 /* Another hash table for plt_branch stubs. */
1910 struct bfd_hash_table branch_hash_table;
1912 /* Linker stub bfd. */
1913 bfd *stub_bfd;
1915 /* Linker call-backs. */
1916 asection * (*add_stub_section) PARAMS ((const char *, asection *));
1917 void (*layout_sections_again) PARAMS ((void));
1919 /* Array to keep track of which stub sections have been created, and
1920 information on stub grouping. */
1921 struct map_stub {
1922 /* This is the section to which stubs in the group will be attached. */
1923 asection *link_sec;
1924 /* The stub section. */
1925 asection *stub_sec;
1926 } *stub_group;
1928 /* Assorted information used by ppc64_elf_size_stubs. */
1929 int top_index;
1930 asection **input_list;
1932 /* Short-cuts to get to dynamic linker sections. */
1933 asection *sgot;
1934 asection *srelgot;
1935 asection *splt;
1936 asection *srelplt;
1937 asection *sdynbss;
1938 asection *srelbss;
1939 asection *sglink;
1940 asection *sfpr;
1941 asection *sbrlt;
1942 asection *srelbrlt;
1944 /* Set on error. */
1945 unsigned int stub_error;
1947 /* Flag set when small branches are detected. Used to
1948 select suitable defaults for the stub group size. */
1949 unsigned int has_14bit_branch;
1951 /* Set if we detect a reference undefined weak symbol. */
1952 unsigned int have_undefweak;
1954 /* Incremented every time we size stubs. */
1955 unsigned int stub_iteration;
1957 /* Small local sym to section mapping cache. */
1958 struct sym_sec_cache sym_sec;
1961 static struct bfd_hash_entry *stub_hash_newfunc
1962 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
1963 static struct bfd_hash_entry *branch_hash_newfunc
1964 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
1965 static struct bfd_hash_entry *link_hash_newfunc
1966 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
1967 static struct bfd_link_hash_table *ppc64_elf_link_hash_table_create
1968 PARAMS ((bfd *));
1969 static void ppc64_elf_link_hash_table_free
1970 PARAMS ((struct bfd_link_hash_table *));
1971 static char *ppc_stub_name
1972 PARAMS ((const asection *, const asection *,
1973 const struct ppc_link_hash_entry *, const Elf_Internal_Rela *));
1974 static struct ppc_stub_hash_entry *ppc_get_stub_entry
1975 PARAMS ((const asection *, const asection *, struct elf_link_hash_entry *,
1976 const Elf_Internal_Rela *, struct ppc_link_hash_table *));
1977 static struct ppc_stub_hash_entry *ppc_add_stub
1978 PARAMS ((const char *, asection *, struct ppc_link_hash_table *));
1979 static bfd_boolean create_linkage_sections
1980 PARAMS ((bfd *, struct bfd_link_info *));
1981 static bfd_boolean create_got_section
1982 PARAMS ((bfd *, struct bfd_link_info *));
1983 static bfd_boolean ppc64_elf_create_dynamic_sections
1984 PARAMS ((bfd *, struct bfd_link_info *));
1985 static void ppc64_elf_copy_indirect_symbol
1986 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
1987 struct elf_link_hash_entry *));
1988 static bfd_boolean ppc64_elf_check_relocs
1989 PARAMS ((bfd *, struct bfd_link_info *, asection *,
1990 const Elf_Internal_Rela *));
1991 static asection * ppc64_elf_gc_mark_hook
1992 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
1993 struct elf_link_hash_entry *, Elf_Internal_Sym *));
1994 static bfd_boolean ppc64_elf_gc_sweep_hook
1995 PARAMS ((bfd *, struct bfd_link_info *, asection *,
1996 const Elf_Internal_Rela *));
1997 static bfd_boolean func_desc_adjust
1998 PARAMS ((struct elf_link_hash_entry *, PTR));
1999 static bfd_boolean ppc64_elf_func_desc_adjust
2000 PARAMS ((bfd *, struct bfd_link_info *));
2001 static bfd_boolean ppc64_elf_adjust_dynamic_symbol
2002 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
2003 static void ppc64_elf_hide_symbol
2004 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean));
2005 static bfd_boolean allocate_dynrelocs
2006 PARAMS ((struct elf_link_hash_entry *, PTR));
2007 static bfd_boolean readonly_dynrelocs
2008 PARAMS ((struct elf_link_hash_entry *, PTR));
2009 static enum elf_reloc_type_class ppc64_elf_reloc_type_class
2010 PARAMS ((const Elf_Internal_Rela *));
2011 static bfd_boolean ppc64_elf_size_dynamic_sections
2012 PARAMS ((bfd *, struct bfd_link_info *));
2013 static INLINE enum ppc_stub_type ppc_type_of_stub
2014 PARAMS ((asection *, const Elf_Internal_Rela *,
2015 struct ppc_link_hash_entry **, bfd_vma));
2016 static bfd_byte *build_plt_stub
2017 PARAMS ((bfd *, bfd_byte *, int, int));
2018 static bfd_boolean ppc_build_one_stub
2019 PARAMS ((struct bfd_hash_entry *, PTR));
2020 static bfd_boolean ppc_size_one_stub
2021 PARAMS ((struct bfd_hash_entry *, PTR));
2022 static void group_sections
2023 PARAMS ((struct ppc_link_hash_table *, bfd_size_type, bfd_boolean));
2024 static bfd_boolean ppc64_elf_relocate_section
2025 PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
2026 Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
2027 asection **));
2028 static bfd_boolean ppc64_elf_finish_dynamic_symbol
2029 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
2030 Elf_Internal_Sym *));
2031 static bfd_boolean ppc64_elf_finish_dynamic_sections
2032 PARAMS ((bfd *, struct bfd_link_info *));
2034 /* Get the ppc64 ELF linker hash table from a link_info structure. */
2036 #define ppc_hash_table(p) \
2037 ((struct ppc_link_hash_table *) ((p)->hash))
2039 #define ppc_stub_hash_lookup(table, string, create, copy) \
2040 ((struct ppc_stub_hash_entry *) \
2041 bfd_hash_lookup ((table), (string), (create), (copy)))
2043 #define ppc_branch_hash_lookup(table, string, create, copy) \
2044 ((struct ppc_branch_hash_entry *) \
2045 bfd_hash_lookup ((table), (string), (create), (copy)))
2047 /* Create an entry in the stub hash table. */
2049 static struct bfd_hash_entry *
2050 stub_hash_newfunc (entry, table, string)
2051 struct bfd_hash_entry *entry;
2052 struct bfd_hash_table *table;
2053 const char *string;
2055 /* Allocate the structure if it has not already been allocated by a
2056 subclass. */
2057 if (entry == NULL)
2059 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
2060 if (entry == NULL)
2061 return entry;
2064 /* Call the allocation method of the superclass. */
2065 entry = bfd_hash_newfunc (entry, table, string);
2066 if (entry != NULL)
2068 struct ppc_stub_hash_entry *eh;
2070 /* Initialize the local fields. */
2071 eh = (struct ppc_stub_hash_entry *) entry;
2072 eh->stub_sec = NULL;
2073 eh->stub_offset = 0;
2074 eh->target_value = 0;
2075 eh->target_section = NULL;
2076 eh->stub_type = ppc_stub_none;
2077 eh->h = NULL;
2078 eh->id_sec = NULL;
2081 return entry;
2084 /* Create an entry in the branch hash table. */
2086 static struct bfd_hash_entry *
2087 branch_hash_newfunc (entry, table, string)
2088 struct bfd_hash_entry *entry;
2089 struct bfd_hash_table *table;
2090 const char *string;
2092 /* Allocate the structure if it has not already been allocated by a
2093 subclass. */
2094 if (entry == NULL)
2096 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
2097 if (entry == NULL)
2098 return entry;
2101 /* Call the allocation method of the superclass. */
2102 entry = bfd_hash_newfunc (entry, table, string);
2103 if (entry != NULL)
2105 struct ppc_branch_hash_entry *eh;
2107 /* Initialize the local fields. */
2108 eh = (struct ppc_branch_hash_entry *) entry;
2109 eh->offset = 0;
2110 eh->iter = 0;
2113 return entry;
2116 /* Create an entry in a ppc64 ELF linker hash table. */
2118 static struct bfd_hash_entry *
2119 link_hash_newfunc (entry, table, string)
2120 struct bfd_hash_entry *entry;
2121 struct bfd_hash_table *table;
2122 const char *string;
2124 /* Allocate the structure if it has not already been allocated by a
2125 subclass. */
2126 if (entry == NULL)
2128 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
2129 if (entry == NULL)
2130 return entry;
2133 /* Call the allocation method of the superclass. */
2134 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
2135 if (entry != NULL)
2137 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
2139 eh->stub_cache = NULL;
2140 eh->dyn_relocs = NULL;
2141 eh->oh = NULL;
2142 eh->is_func = 0;
2143 eh->is_func_descriptor = 0;
2144 eh->is_entry = 0;
2147 return entry;
2150 /* Create a ppc64 ELF linker hash table. */
2152 static struct bfd_link_hash_table *
2153 ppc64_elf_link_hash_table_create (abfd)
2154 bfd *abfd;
2156 struct ppc_link_hash_table *htab;
2157 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
2159 htab = (struct ppc_link_hash_table *) bfd_malloc (amt);
2160 if (htab == NULL)
2161 return NULL;
2163 if (! _bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc))
2165 free (htab);
2166 return NULL;
2169 /* Init the stub hash table too. */
2170 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc))
2171 return NULL;
2173 /* And the branch hash table. */
2174 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc))
2175 return NULL;
2177 htab->stub_bfd = NULL;
2178 htab->add_stub_section = NULL;
2179 htab->layout_sections_again = NULL;
2180 htab->stub_group = NULL;
2181 htab->sgot = NULL;
2182 htab->srelgot = NULL;
2183 htab->splt = NULL;
2184 htab->srelplt = NULL;
2185 htab->sdynbss = NULL;
2186 htab->srelbss = NULL;
2187 htab->sglink = NULL;
2188 htab->sfpr = NULL;
2189 htab->sbrlt = NULL;
2190 htab->srelbrlt = NULL;
2191 htab->stub_error = 0;
2192 htab->has_14bit_branch = 0;
2193 htab->have_undefweak = 0;
2194 htab->stub_iteration = 0;
2195 htab->sym_sec.abfd = NULL;
2197 return &htab->elf.root;
2200 /* Free the derived linker hash table. */
2202 static void
2203 ppc64_elf_link_hash_table_free (hash)
2204 struct bfd_link_hash_table *hash;
2206 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
2208 bfd_hash_table_free (&ret->stub_hash_table);
2209 bfd_hash_table_free (&ret->branch_hash_table);
2210 _bfd_generic_link_hash_table_free (hash);
2213 /* Build a name for an entry in the stub hash table. */
2215 static char *
2216 ppc_stub_name (input_section, sym_sec, h, rel)
2217 const asection *input_section;
2218 const asection *sym_sec;
2219 const struct ppc_link_hash_entry *h;
2220 const Elf_Internal_Rela *rel;
2222 char *stub_name;
2223 bfd_size_type len;
2225 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
2226 offsets from a sym as a branch target? In fact, we could
2227 probably assume the addend is always zero. */
2228 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
2230 if (h)
2232 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
2233 stub_name = bfd_malloc (len);
2234 if (stub_name != NULL)
2236 sprintf (stub_name, "%08x_%s+%x",
2237 input_section->id & 0xffffffff,
2238 h->elf.root.root.string,
2239 (int) rel->r_addend & 0xffffffff);
2242 else
2244 len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
2245 stub_name = bfd_malloc (len);
2246 if (stub_name != NULL)
2248 sprintf (stub_name, "%08x_%x:%x+%x",
2249 input_section->id & 0xffffffff,
2250 sym_sec->id & 0xffffffff,
2251 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
2252 (int) rel->r_addend & 0xffffffff);
2255 return stub_name;
2258 /* Look up an entry in the stub hash. Stub entries are cached because
2259 creating the stub name takes a bit of time. */
2261 static struct ppc_stub_hash_entry *
2262 ppc_get_stub_entry (input_section, sym_sec, hash, rel, htab)
2263 const asection *input_section;
2264 const asection *sym_sec;
2265 struct elf_link_hash_entry *hash;
2266 const Elf_Internal_Rela *rel;
2267 struct ppc_link_hash_table *htab;
2269 struct ppc_stub_hash_entry *stub_entry;
2270 struct ppc_link_hash_entry *h = (struct ppc_link_hash_entry *) hash;
2271 const asection *id_sec;
2273 /* If this input section is part of a group of sections sharing one
2274 stub section, then use the id of the first section in the group.
2275 Stub names need to include a section id, as there may well be
2276 more than one stub used to reach say, printf, and we need to
2277 distinguish between them. */
2278 id_sec = htab->stub_group[input_section->id].link_sec;
2280 if (h != NULL && h->stub_cache != NULL
2281 && h->stub_cache->h == h
2282 && h->stub_cache->id_sec == id_sec)
2284 stub_entry = h->stub_cache;
2286 else
2288 char *stub_name;
2290 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
2291 if (stub_name == NULL)
2292 return NULL;
2294 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
2295 stub_name, FALSE, FALSE);
2296 if (h != NULL)
2297 h->stub_cache = stub_entry;
2299 free (stub_name);
2302 return stub_entry;
2305 /* Add a new stub entry to the stub hash. Not all fields of the new
2306 stub entry are initialised. */
2308 static struct ppc_stub_hash_entry *
2309 ppc_add_stub (stub_name, section, htab)
2310 const char *stub_name;
2311 asection *section;
2312 struct ppc_link_hash_table *htab;
2314 asection *link_sec;
2315 asection *stub_sec;
2316 struct ppc_stub_hash_entry *stub_entry;
2318 link_sec = htab->stub_group[section->id].link_sec;
2319 stub_sec = htab->stub_group[section->id].stub_sec;
2320 if (stub_sec == NULL)
2322 stub_sec = htab->stub_group[link_sec->id].stub_sec;
2323 if (stub_sec == NULL)
2325 size_t namelen;
2326 bfd_size_type len;
2327 char *s_name;
2329 namelen = strlen (link_sec->name);
2330 len = namelen + sizeof (STUB_SUFFIX);
2331 s_name = bfd_alloc (htab->stub_bfd, len);
2332 if (s_name == NULL)
2333 return NULL;
2335 memcpy (s_name, link_sec->name, namelen);
2336 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
2337 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
2338 if (stub_sec == NULL)
2339 return NULL;
2340 htab->stub_group[link_sec->id].stub_sec = stub_sec;
2342 htab->stub_group[section->id].stub_sec = stub_sec;
2345 /* Enter this entry into the linker stub hash table. */
2346 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
2347 TRUE, FALSE);
2348 if (stub_entry == NULL)
2350 (*_bfd_error_handler) (_("%s: cannot create stub entry %s"),
2351 bfd_archive_filename (section->owner),
2352 stub_name);
2353 return NULL;
2356 stub_entry->stub_sec = stub_sec;
2357 stub_entry->stub_offset = 0;
2358 stub_entry->id_sec = link_sec;
2359 return stub_entry;
2362 /* Create sections for linker generated code. */
2364 static bfd_boolean
2365 create_linkage_sections (dynobj, info)
2366 bfd *dynobj;
2367 struct bfd_link_info *info;
2369 struct ppc_link_hash_table *htab;
2370 flagword flags;
2372 htab = ppc_hash_table (info);
2374 /* Create .sfpr for code to save and restore fp regs. */
2375 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
2376 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2377 htab->sfpr = bfd_make_section_anyway (dynobj, ".sfpr");
2378 if (htab->sfpr == NULL
2379 || ! bfd_set_section_flags (dynobj, htab->sfpr, flags)
2380 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
2381 return FALSE;
2383 /* Create .glink for lazy dynamic linking support. */
2384 htab->sglink = bfd_make_section_anyway (dynobj, ".glink");
2385 if (htab->sglink == NULL
2386 || ! bfd_set_section_flags (dynobj, htab->sglink, flags)
2387 || ! bfd_set_section_alignment (dynobj, htab->sglink, 2))
2388 return FALSE;
2390 /* Create .branch_lt for plt_branch stubs. */
2391 flags = (SEC_ALLOC | SEC_LOAD
2392 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2393 htab->sbrlt = bfd_make_section_anyway (dynobj, ".branch_lt");
2394 if (htab->sbrlt == NULL
2395 || ! bfd_set_section_flags (dynobj, htab->sbrlt, flags)
2396 || ! bfd_set_section_alignment (dynobj, htab->sbrlt, 3))
2397 return FALSE;
2399 if (info->shared)
2401 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
2402 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2403 htab->srelbrlt = bfd_make_section_anyway (dynobj, ".rela.branch_lt");
2404 if (!htab->srelbrlt
2405 || ! bfd_set_section_flags (dynobj, htab->srelbrlt, flags)
2406 || ! bfd_set_section_alignment (dynobj, htab->srelbrlt, 3))
2407 return FALSE;
2409 return TRUE;
2412 /* Create .got and .rela.got sections in DYNOBJ, and set up
2413 shortcuts to them in our hash table. */
2415 static bfd_boolean
2416 create_got_section (dynobj, info)
2417 bfd *dynobj;
2418 struct bfd_link_info *info;
2420 struct ppc_link_hash_table *htab;
2422 if (! _bfd_elf_create_got_section (dynobj, info))
2423 return FALSE;
2425 htab = ppc_hash_table (info);
2426 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
2427 if (!htab->sgot)
2428 abort ();
2430 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
2431 if (!htab->srelgot
2432 || ! bfd_set_section_flags (dynobj, htab->srelgot,
2433 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
2434 | SEC_IN_MEMORY | SEC_LINKER_CREATED
2435 | SEC_READONLY))
2436 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
2437 return FALSE;
2438 return TRUE;
2441 /* Create the dynamic sections, and set up shortcuts. */
2443 static bfd_boolean
2444 ppc64_elf_create_dynamic_sections (dynobj, info)
2445 bfd *dynobj;
2446 struct bfd_link_info *info;
2448 struct ppc_link_hash_table *htab;
2450 htab = ppc_hash_table (info);
2451 if (!htab->sgot && !create_got_section (dynobj, info))
2452 return FALSE;
2454 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
2455 return FALSE;
2457 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
2458 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
2459 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
2460 if (!info->shared)
2461 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
2463 if (!htab->splt || !htab->srelplt || !htab->sdynbss
2464 || (!info->shared && !htab->srelbss))
2465 abort ();
2467 return TRUE;
2470 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2472 static void
2473 ppc64_elf_copy_indirect_symbol (bed, dir, ind)
2474 struct elf_backend_data *bed;
2475 struct elf_link_hash_entry *dir, *ind;
2477 struct ppc_link_hash_entry *edir, *eind;
2479 edir = (struct ppc_link_hash_entry *) dir;
2480 eind = (struct ppc_link_hash_entry *) ind;
2482 if (eind->dyn_relocs != NULL)
2484 if (edir->dyn_relocs != NULL)
2486 struct ppc_dyn_relocs **pp;
2487 struct ppc_dyn_relocs *p;
2489 if (ind->root.type == bfd_link_hash_indirect)
2490 abort ();
2492 /* Add reloc counts against the weak sym to the strong sym
2493 list. Merge any entries against the same section. */
2494 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2496 struct ppc_dyn_relocs *q;
2498 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2499 if (q->sec == p->sec)
2501 q->pc_count += p->pc_count;
2502 q->count += p->count;
2503 *pp = p->next;
2504 break;
2506 if (q == NULL)
2507 pp = &p->next;
2509 *pp = edir->dyn_relocs;
2512 edir->dyn_relocs = eind->dyn_relocs;
2513 eind->dyn_relocs = NULL;
2516 edir->is_func |= eind->is_func;
2517 edir->is_func_descriptor |= eind->is_func_descriptor;
2518 edir->is_entry |= eind->is_entry;
2520 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
2523 /* Set a flag, used by ppc64_elf_gc_mark_hook, on the entry symbol and
2524 symbols undefined on the command-line. */
2526 bfd_boolean
2527 ppc64_elf_mark_entry_syms (info)
2528 struct bfd_link_info *info;
2530 struct ppc_link_hash_table *htab;
2531 struct bfd_sym_chain *sym;
2533 htab = ppc_hash_table (info);
2534 for (sym = info->gc_sym_list; sym; sym = sym->next)
2536 struct elf_link_hash_entry *h;
2538 h = elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
2539 if (h != NULL)
2540 ((struct ppc_link_hash_entry *) h)->is_entry = 1;
2542 return TRUE;
2545 /* Look through the relocs for a section during the first phase, and
2546 calculate needed space in the global offset table, procedure
2547 linkage table, and dynamic reloc sections. */
2549 static bfd_boolean
2550 ppc64_elf_check_relocs (abfd, info, sec, relocs)
2551 bfd *abfd;
2552 struct bfd_link_info *info;
2553 asection *sec;
2554 const Elf_Internal_Rela *relocs;
2556 struct ppc_link_hash_table *htab;
2557 Elf_Internal_Shdr *symtab_hdr;
2558 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
2559 const Elf_Internal_Rela *rel;
2560 const Elf_Internal_Rela *rel_end;
2561 asection *sreloc;
2562 asection **opd_sym_map;
2564 if (info->relocateable)
2565 return TRUE;
2567 htab = ppc_hash_table (info);
2568 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2570 sym_hashes = elf_sym_hashes (abfd);
2571 sym_hashes_end = (sym_hashes
2572 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
2573 - symtab_hdr->sh_info);
2575 sreloc = NULL;
2576 opd_sym_map = NULL;
2577 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
2579 /* Garbage collection needs some extra help with .opd sections.
2580 We don't want to necessarily keep everything referenced by
2581 relocs in .opd, as that would keep all functions. Instead,
2582 if we reference an .opd symbol (a function descriptor), we
2583 want to keep the function code symbol's section. This is
2584 easy for global symbols, but for local syms we need to keep
2585 information about the associated function section. Later, if
2586 edit_opd deletes entries, we'll use this array to adjust
2587 local syms in .opd. */
2588 union opd_info {
2589 asection *func_section;
2590 long entry_adjust;
2592 bfd_size_type amt;
2594 amt = sec->_raw_size * sizeof (union opd_info) / 24;
2595 opd_sym_map = (asection **) bfd_zalloc (abfd, amt);
2596 if (opd_sym_map == NULL)
2597 return FALSE;
2598 ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
2601 if (htab->elf.dynobj == NULL)
2602 htab->elf.dynobj = abfd;
2603 if (htab->sfpr == NULL
2604 && !create_linkage_sections (htab->elf.dynobj, info))
2605 return FALSE;
2607 rel_end = relocs + sec->reloc_count;
2608 for (rel = relocs; rel < rel_end; rel++)
2610 unsigned long r_symndx;
2611 struct elf_link_hash_entry *h;
2612 enum elf_ppc64_reloc_type r_type;
2614 r_symndx = ELF64_R_SYM (rel->r_info);
2615 if (r_symndx < symtab_hdr->sh_info)
2616 h = NULL;
2617 else
2618 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2620 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
2621 switch (r_type)
2623 /* GOT16 relocations */
2624 case R_PPC64_GOT16:
2625 case R_PPC64_GOT16_DS:
2626 case R_PPC64_GOT16_HA:
2627 case R_PPC64_GOT16_HI:
2628 case R_PPC64_GOT16_LO:
2629 case R_PPC64_GOT16_LO_DS:
2631 /* This symbol requires a global offset table entry. */
2632 if (htab->sgot == NULL
2633 && !create_got_section (htab->elf.dynobj, info))
2634 return FALSE;
2636 if (h != NULL)
2638 h->got.refcount += 1;
2640 else
2642 bfd_signed_vma *local_got_refcounts;
2644 /* This is a global offset table entry for a local symbol. */
2645 local_got_refcounts = elf_local_got_refcounts (abfd);
2646 if (local_got_refcounts == NULL)
2648 bfd_size_type size;
2650 size = symtab_hdr->sh_info;
2651 size *= sizeof (bfd_signed_vma);
2652 local_got_refcounts = ((bfd_signed_vma *)
2653 bfd_zalloc (abfd, size));
2654 if (local_got_refcounts == NULL)
2655 return FALSE;
2656 elf_local_got_refcounts (abfd) = local_got_refcounts;
2658 local_got_refcounts[r_symndx] += 1;
2660 break;
2662 case R_PPC64_PLT16_HA:
2663 case R_PPC64_PLT16_HI:
2664 case R_PPC64_PLT16_LO:
2665 case R_PPC64_PLT32:
2666 case R_PPC64_PLT64:
2667 /* This symbol requires a procedure linkage table entry. We
2668 actually build the entry in adjust_dynamic_symbol,
2669 because this might be a case of linking PIC code without
2670 linking in any dynamic objects, in which case we don't
2671 need to generate a procedure linkage table after all. */
2672 if (h == NULL)
2674 /* It does not make sense to have a procedure linkage
2675 table entry for a local symbol. */
2676 bfd_set_error (bfd_error_bad_value);
2677 return FALSE;
2680 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2681 h->plt.refcount += 1;
2682 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2683 break;
2685 /* The following relocations don't need to propagate the
2686 relocation if linking a shared object since they are
2687 section relative. */
2688 case R_PPC64_SECTOFF:
2689 case R_PPC64_SECTOFF_LO:
2690 case R_PPC64_SECTOFF_HI:
2691 case R_PPC64_SECTOFF_HA:
2692 case R_PPC64_SECTOFF_DS:
2693 case R_PPC64_SECTOFF_LO_DS:
2694 case R_PPC64_TOC16:
2695 case R_PPC64_TOC16_LO:
2696 case R_PPC64_TOC16_HI:
2697 case R_PPC64_TOC16_HA:
2698 case R_PPC64_TOC16_DS:
2699 case R_PPC64_TOC16_LO_DS:
2700 break;
2702 /* This relocation describes the C++ object vtable hierarchy.
2703 Reconstruct it for later use during GC. */
2704 case R_PPC64_GNU_VTINHERIT:
2705 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2706 return FALSE;
2707 break;
2709 /* This relocation describes which C++ vtable entries are actually
2710 used. Record for later use during GC. */
2711 case R_PPC64_GNU_VTENTRY:
2712 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2713 return FALSE;
2714 break;
2716 case R_PPC64_REL14:
2717 case R_PPC64_REL14_BRTAKEN:
2718 case R_PPC64_REL14_BRNTAKEN:
2719 htab->has_14bit_branch = 1;
2720 /* Fall through. */
2722 case R_PPC64_REL24:
2723 if (h != NULL
2724 && h->root.root.string[0] == '.'
2725 && h->root.root.string[1] != 0)
2727 /* We may need a .plt entry if the function this reloc
2728 refers to is in a shared lib. */
2729 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
2730 h->plt.refcount += 1;
2731 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2733 break;
2735 case R_PPC64_ADDR64:
2736 if (opd_sym_map != NULL
2737 && h != NULL
2738 && h->root.root.string[0] == '.'
2739 && h->root.root.string[1] != 0)
2741 struct elf_link_hash_entry *fdh;
2743 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
2744 FALSE, FALSE, FALSE);
2745 if (fdh != NULL)
2747 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
2748 ((struct ppc_link_hash_entry *) fdh)->oh = h;
2749 ((struct ppc_link_hash_entry *) h)->is_func = 1;
2750 ((struct ppc_link_hash_entry *) h)->oh = fdh;
2753 if (opd_sym_map != NULL
2754 && h == NULL
2755 && rel + 1 < rel_end
2756 && ((enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info)
2757 == R_PPC64_TOC))
2759 asection *s;
2761 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
2762 r_symndx);
2763 if (s == NULL)
2764 return FALSE;
2765 else if (s != sec)
2766 opd_sym_map[rel->r_offset / 24] = s;
2768 /* Fall through. */
2770 case R_PPC64_REL30:
2771 case R_PPC64_REL32:
2772 case R_PPC64_REL64:
2773 case R_PPC64_ADDR14:
2774 case R_PPC64_ADDR14_BRNTAKEN:
2775 case R_PPC64_ADDR14_BRTAKEN:
2776 case R_PPC64_ADDR16:
2777 case R_PPC64_ADDR16_DS:
2778 case R_PPC64_ADDR16_HA:
2779 case R_PPC64_ADDR16_HI:
2780 case R_PPC64_ADDR16_HIGHER:
2781 case R_PPC64_ADDR16_HIGHERA:
2782 case R_PPC64_ADDR16_HIGHEST:
2783 case R_PPC64_ADDR16_HIGHESTA:
2784 case R_PPC64_ADDR16_LO:
2785 case R_PPC64_ADDR16_LO_DS:
2786 case R_PPC64_ADDR24:
2787 case R_PPC64_ADDR32:
2788 case R_PPC64_UADDR16:
2789 case R_PPC64_UADDR32:
2790 case R_PPC64_UADDR64:
2791 case R_PPC64_TOC:
2792 /* Don't propagate .opd relocs. */
2793 if (NO_OPD_RELOCS && opd_sym_map != NULL)
2794 break;
2796 /* If we are creating a shared library, and this is a reloc
2797 against a global symbol, or a non PC relative reloc
2798 against a local symbol, then we need to copy the reloc
2799 into the shared library. However, if we are linking with
2800 -Bsymbolic, we do not need to copy a reloc against a
2801 global symbol which is defined in an object we are
2802 including in the link (i.e., DEF_REGULAR is set). At
2803 this point we have not seen all the input files, so it is
2804 possible that DEF_REGULAR is not set now but will be set
2805 later (it is never cleared). In case of a weak definition,
2806 DEF_REGULAR may be cleared later by a strong definition in
2807 a shared library. We account for that possibility below by
2808 storing information in the relocs_copied field of the hash
2809 table entry. A similar situation occurs when creating
2810 shared libraries and symbol visibility changes render the
2811 symbol local.
2813 If on the other hand, we are creating an executable, we
2814 may need to keep relocations for symbols satisfied by a
2815 dynamic library if we manage to avoid copy relocs for the
2816 symbol. */
2817 if ((info->shared
2818 && (sec->flags & SEC_ALLOC) != 0
2819 && (IS_ABSOLUTE_RELOC (r_type)
2820 || (h != NULL
2821 && (! info->symbolic
2822 || h->root.type == bfd_link_hash_defweak
2823 || (h->elf_link_hash_flags
2824 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
2825 || (!info->shared
2826 && (sec->flags & SEC_ALLOC) != 0
2827 && h != NULL
2828 && (h->root.type == bfd_link_hash_defweak
2829 || (h->elf_link_hash_flags
2830 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
2832 struct ppc_dyn_relocs *p;
2833 struct ppc_dyn_relocs **head;
2835 /* We must copy these reloc types into the output file.
2836 Create a reloc section in dynobj and make room for
2837 this reloc. */
2838 if (sreloc == NULL)
2840 const char *name;
2841 bfd *dynobj;
2843 name = (bfd_elf_string_from_elf_section
2844 (abfd,
2845 elf_elfheader (abfd)->e_shstrndx,
2846 elf_section_data (sec)->rel_hdr.sh_name));
2847 if (name == NULL)
2848 return FALSE;
2850 if (strncmp (name, ".rela", 5) != 0
2851 || strcmp (bfd_get_section_name (abfd, sec),
2852 name + 5) != 0)
2854 (*_bfd_error_handler)
2855 (_("%s: bad relocation section name `%s\'"),
2856 bfd_archive_filename (abfd), name);
2857 bfd_set_error (bfd_error_bad_value);
2860 dynobj = htab->elf.dynobj;
2861 sreloc = bfd_get_section_by_name (dynobj, name);
2862 if (sreloc == NULL)
2864 flagword flags;
2866 sreloc = bfd_make_section (dynobj, name);
2867 flags = (SEC_HAS_CONTENTS | SEC_READONLY
2868 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
2869 if ((sec->flags & SEC_ALLOC) != 0)
2870 flags |= SEC_ALLOC | SEC_LOAD;
2871 if (sreloc == NULL
2872 || ! bfd_set_section_flags (dynobj, sreloc, flags)
2873 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
2874 return FALSE;
2876 elf_section_data (sec)->sreloc = sreloc;
2879 /* If this is a global symbol, we count the number of
2880 relocations we need for this symbol. */
2881 if (h != NULL)
2883 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
2885 else
2887 /* Track dynamic relocs needed for local syms too.
2888 We really need local syms available to do this
2889 easily. Oh well. */
2891 asection *s;
2892 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
2893 sec, r_symndx);
2894 if (s == NULL)
2895 return FALSE;
2897 head = ((struct ppc_dyn_relocs **)
2898 &elf_section_data (s)->local_dynrel);
2901 p = *head;
2902 if (p == NULL || p->sec != sec)
2904 p = ((struct ppc_dyn_relocs *)
2905 bfd_alloc (htab->elf.dynobj,
2906 (bfd_size_type) sizeof *p));
2907 if (p == NULL)
2908 return FALSE;
2909 p->next = *head;
2910 *head = p;
2911 p->sec = sec;
2912 p->count = 0;
2913 p->pc_count = 0;
2916 p->count += 1;
2917 if (!IS_ABSOLUTE_RELOC (r_type))
2918 p->pc_count += 1;
2920 break;
2922 default:
2923 break;
2927 return TRUE;
2930 /* Return the section that should be marked against GC for a given
2931 relocation. */
2933 static asection *
2934 ppc64_elf_gc_mark_hook (sec, info, rel, h, sym)
2935 asection *sec;
2936 struct bfd_link_info *info ATTRIBUTE_UNUSED;
2937 Elf_Internal_Rela *rel;
2938 struct elf_link_hash_entry *h;
2939 Elf_Internal_Sym *sym;
2941 asection *rsec = NULL;
2943 if (h != NULL)
2945 enum elf_ppc64_reloc_type r_type;
2946 struct ppc_link_hash_entry *fdh;
2948 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
2949 switch (r_type)
2951 case R_PPC64_GNU_VTINHERIT:
2952 case R_PPC64_GNU_VTENTRY:
2953 break;
2955 default:
2956 switch (h->root.type)
2958 case bfd_link_hash_defined:
2959 case bfd_link_hash_defweak:
2960 fdh = (struct ppc_link_hash_entry *) h;
2962 /* Function descriptor syms cause the associated
2963 function code sym section to be marked. */
2964 if (fdh->is_func_descriptor)
2965 rsec = fdh->oh->root.u.def.section;
2967 /* Function entry syms return NULL if they are in .opd
2968 and are not ._start (or others undefined on the ld
2969 command line). Thus we avoid marking all function
2970 sections, as all functions are referenced in .opd. */
2971 else if ((fdh->oh != NULL
2972 && ((struct ppc_link_hash_entry *) fdh->oh)->is_entry)
2973 || ppc64_elf_section_data (sec)->opd.func_sec == NULL)
2974 rsec = h->root.u.def.section;
2975 break;
2977 case bfd_link_hash_common:
2978 rsec = h->root.u.c.p->section;
2979 break;
2981 default:
2982 break;
2986 else
2988 asection **opd_sym_section;
2990 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
2991 opd_sym_section = ppc64_elf_section_data (rsec)->opd.func_sec;
2992 if (opd_sym_section != NULL)
2993 rsec = opd_sym_section[sym->st_value / 24];
2994 else if (ppc64_elf_section_data (sec)->opd.func_sec != NULL)
2995 rsec = NULL;
2998 return rsec;
3001 /* Update the .got, .plt. and dynamic reloc reference counts for the
3002 section being removed. */
3004 static bfd_boolean
3005 ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
3006 bfd *abfd;
3007 struct bfd_link_info *info ATTRIBUTE_UNUSED;
3008 asection *sec;
3009 const Elf_Internal_Rela *relocs;
3011 Elf_Internal_Shdr *symtab_hdr;
3012 struct elf_link_hash_entry **sym_hashes;
3013 bfd_signed_vma *local_got_refcounts;
3014 const Elf_Internal_Rela *rel, *relend;
3016 elf_section_data (sec)->local_dynrel = NULL;
3018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3019 sym_hashes = elf_sym_hashes (abfd);
3020 local_got_refcounts = elf_local_got_refcounts (abfd);
3022 relend = relocs + sec->reloc_count;
3023 for (rel = relocs; rel < relend; rel++)
3025 unsigned long r_symndx;
3026 enum elf_ppc64_reloc_type r_type;
3027 struct elf_link_hash_entry *h;
3029 r_symndx = ELF64_R_SYM (rel->r_info);
3030 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
3031 switch (r_type)
3033 case R_PPC64_GOT16:
3034 case R_PPC64_GOT16_DS:
3035 case R_PPC64_GOT16_HA:
3036 case R_PPC64_GOT16_HI:
3037 case R_PPC64_GOT16_LO:
3038 case R_PPC64_GOT16_LO_DS:
3039 if (r_symndx >= symtab_hdr->sh_info)
3041 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3042 if (h->got.refcount > 0)
3043 h->got.refcount--;
3045 else
3047 if (local_got_refcounts[r_symndx] > 0)
3048 local_got_refcounts[r_symndx]--;
3050 break;
3052 case R_PPC64_PLT16_HA:
3053 case R_PPC64_PLT16_HI:
3054 case R_PPC64_PLT16_LO:
3055 case R_PPC64_PLT32:
3056 case R_PPC64_PLT64:
3057 if (r_symndx >= symtab_hdr->sh_info)
3059 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3060 if (h->plt.refcount > 0)
3061 h->plt.refcount--;
3063 break;
3065 case R_PPC64_REL14:
3066 case R_PPC64_REL14_BRNTAKEN:
3067 case R_PPC64_REL14_BRTAKEN:
3068 case R_PPC64_REL24:
3069 if (r_symndx >= symtab_hdr->sh_info)
3071 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3072 if (h->plt.refcount > 0)
3073 h->plt.refcount--;
3075 break;
3077 case R_PPC64_REL30:
3078 case R_PPC64_REL32:
3079 case R_PPC64_REL64:
3080 if (r_symndx >= symtab_hdr->sh_info)
3082 struct ppc_link_hash_entry *eh;
3083 struct ppc_dyn_relocs **pp;
3084 struct ppc_dyn_relocs *p;
3086 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3087 eh = (struct ppc_link_hash_entry *) h;
3089 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3090 if (p->sec == sec)
3092 p->pc_count -= 1;
3093 p->count -= 1;
3094 if (p->count == 0)
3095 *pp = p->next;
3096 break;
3099 break;
3101 case R_PPC64_ADDR14:
3102 case R_PPC64_ADDR14_BRNTAKEN:
3103 case R_PPC64_ADDR14_BRTAKEN:
3104 case R_PPC64_ADDR16:
3105 case R_PPC64_ADDR16_DS:
3106 case R_PPC64_ADDR16_HA:
3107 case R_PPC64_ADDR16_HI:
3108 case R_PPC64_ADDR16_HIGHER:
3109 case R_PPC64_ADDR16_HIGHERA:
3110 case R_PPC64_ADDR16_HIGHEST:
3111 case R_PPC64_ADDR16_HIGHESTA:
3112 case R_PPC64_ADDR16_LO:
3113 case R_PPC64_ADDR16_LO_DS:
3114 case R_PPC64_ADDR24:
3115 case R_PPC64_ADDR32:
3116 case R_PPC64_ADDR64:
3117 case R_PPC64_UADDR16:
3118 case R_PPC64_UADDR32:
3119 case R_PPC64_UADDR64:
3120 case R_PPC64_TOC:
3121 if (r_symndx >= symtab_hdr->sh_info)
3123 struct ppc_link_hash_entry *eh;
3124 struct ppc_dyn_relocs **pp;
3125 struct ppc_dyn_relocs *p;
3127 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3128 eh = (struct ppc_link_hash_entry *) h;
3130 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
3131 if (p->sec == sec)
3133 p->count -= 1;
3134 if (p->count == 0)
3135 *pp = p->next;
3136 break;
3139 break;
3141 default:
3142 break;
3145 return TRUE;
3148 /* Called via elf_link_hash_traverse to transfer dynamic linking
3149 information on function code symbol entries to their corresponding
3150 function descriptor symbol entries. */
3151 static bfd_boolean
3152 func_desc_adjust (h, inf)
3153 struct elf_link_hash_entry *h;
3154 PTR inf;
3156 struct bfd_link_info *info;
3157 struct ppc_link_hash_table *htab;
3159 if (h->root.type == bfd_link_hash_indirect)
3160 return TRUE;
3162 if (h->root.type == bfd_link_hash_warning)
3163 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3165 info = (struct bfd_link_info *) inf;
3166 htab = ppc_hash_table (info);
3168 /* If this is a function code symbol, transfer dynamic linking
3169 information to the function descriptor symbol. */
3170 if (!((struct ppc_link_hash_entry *) h)->is_func)
3171 return TRUE;
3173 if (h->root.type == bfd_link_hash_undefweak
3174 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
3175 htab->have_undefweak = TRUE;
3177 if (h->plt.refcount > 0
3178 && h->root.root.string[0] == '.'
3179 && h->root.root.string[1] != '\0')
3181 struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
3182 bfd_boolean force_local;
3184 /* Find the corresponding function descriptor symbol. Create it
3185 as undefined if necessary. */
3187 if (fdh == NULL)
3188 fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
3189 FALSE, FALSE, TRUE);
3191 if (fdh == NULL
3192 && info->shared
3193 && (h->root.type == bfd_link_hash_undefined
3194 || h->root.type == bfd_link_hash_undefweak))
3196 bfd *abfd;
3197 asymbol *newsym;
3198 struct bfd_link_hash_entry *bh;
3200 abfd = h->root.u.undef.abfd;
3201 newsym = bfd_make_empty_symbol (abfd);
3202 newsym->name = h->root.root.string + 1;
3203 newsym->section = bfd_und_section_ptr;
3204 newsym->value = 0;
3205 newsym->flags = BSF_OBJECT;
3206 if (h->root.type == bfd_link_hash_undefweak)
3207 newsym->flags |= BSF_WEAK;
3209 bh = &fdh->root;
3210 if ( !(_bfd_generic_link_add_one_symbol
3211 (info, abfd, newsym->name, newsym->flags,
3212 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
3214 return FALSE;
3216 fdh = (struct elf_link_hash_entry *) bh;
3217 fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
3220 if (fdh != NULL
3221 && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
3222 && (info->shared
3223 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
3224 || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
3226 if (fdh->dynindx == -1)
3227 if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
3228 return FALSE;
3229 fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
3230 & (ELF_LINK_HASH_REF_REGULAR
3231 | ELF_LINK_HASH_REF_DYNAMIC
3232 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
3233 | ELF_LINK_NON_GOT_REF));
3234 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3236 fdh->plt.refcount = h->plt.refcount;
3237 fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
3239 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
3240 ((struct ppc_link_hash_entry *) fdh)->oh = h;
3241 ((struct ppc_link_hash_entry *) h)->oh = fdh;
3244 /* Now that the info is on the function descriptor, clear the
3245 function code sym info. Any function code syms for which we
3246 don't have a definition in a regular file, we force local.
3247 This prevents a shared library from exporting syms that have
3248 been imported from another library. Function code syms that
3249 are really in the library we must leave global to prevent the
3250 linker dragging in a definition from a static library. */
3251 force_local = (info->shared
3252 && ((h->elf_link_hash_flags
3253 & ELF_LINK_HASH_DEF_REGULAR) == 0
3254 || fdh == NULL
3255 || (fdh->elf_link_hash_flags
3256 & ELF_LINK_HASH_DEF_REGULAR) == 0
3257 || (fdh->elf_link_hash_flags
3258 & ELF_LINK_FORCED_LOCAL) != 0));
3259 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3262 return TRUE;
3265 #define MIN_SAVE_FPR 14
3266 #define MAX_SAVE_FPR 31
3268 /* Called near the start of bfd_elf_size_dynamic_sections. We use
3269 this hook to a) provide some gcc support functions, and b) transfer
3270 dynamic linking information gathered so far on function code symbol
3271 entries, to their corresponding function descriptor symbol entries. */
3272 static bfd_boolean
3273 ppc64_elf_func_desc_adjust (obfd, info)
3274 bfd *obfd ATTRIBUTE_UNUSED;
3275 struct bfd_link_info *info;
3277 struct ppc_link_hash_table *htab;
3278 unsigned int lowest_savef = MAX_SAVE_FPR + 2;
3279 unsigned int lowest_restf = MAX_SAVE_FPR + 2;
3280 unsigned int i;
3281 struct elf_link_hash_entry *h;
3282 bfd_byte *p;
3283 char sym[10];
3285 htab = ppc_hash_table (info);
3287 if (htab->sfpr == NULL)
3288 /* We don't have any relocs. */
3289 return TRUE;
3291 /* First provide any missing ._savef* and ._restf* functions. */
3292 memcpy (sym, "._savef14", 10);
3293 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3295 sym[7] = i / 10 + '0';
3296 sym[8] = i % 10 + '0';
3297 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
3298 if (h != NULL
3299 && h->root.type == bfd_link_hash_undefined)
3301 if (lowest_savef > i)
3302 lowest_savef = i;
3303 h->root.type = bfd_link_hash_defined;
3304 h->root.u.def.section = htab->sfpr;
3305 h->root.u.def.value = (i - lowest_savef) * 4;
3306 h->type = STT_FUNC;
3307 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3308 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3312 memcpy (sym, "._restf14", 10);
3313 for (i = MIN_SAVE_FPR; i <= MAX_SAVE_FPR; i++)
3315 sym[7] = i / 10 + '0';
3316 sym[8] = i % 10 + '0';
3317 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
3318 if (h != NULL
3319 && h->root.type == bfd_link_hash_undefined)
3321 if (lowest_restf > i)
3322 lowest_restf = i;
3323 h->root.type = bfd_link_hash_defined;
3324 h->root.u.def.section = htab->sfpr;
3325 h->root.u.def.value = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3326 + (i - lowest_restf) * 4);
3327 h->type = STT_FUNC;
3328 h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
3329 _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
3333 elf_link_hash_traverse (&htab->elf, func_desc_adjust, (PTR) info);
3335 htab->sfpr->_raw_size = ((MAX_SAVE_FPR + 2 - lowest_savef) * 4
3336 + (MAX_SAVE_FPR + 2 - lowest_restf) * 4);
3338 if (htab->sfpr->_raw_size == 0)
3340 if (!htab->have_undefweak)
3342 _bfd_strip_section_from_output (info, htab->sfpr);
3343 return TRUE;
3346 htab->sfpr->_raw_size = 4;
3349 p = (bfd_byte *) bfd_alloc (htab->elf.dynobj, htab->sfpr->_raw_size);
3350 if (p == NULL)
3351 return FALSE;
3352 htab->sfpr->contents = p;
3354 for (i = lowest_savef; i <= MAX_SAVE_FPR; i++)
3356 unsigned int fpr = i << 21;
3357 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3358 bfd_put_32 (htab->elf.dynobj, STFD_FR0_0R1 + fpr + stackoff, p);
3359 p += 4;
3361 if (lowest_savef <= MAX_SAVE_FPR)
3363 bfd_put_32 (htab->elf.dynobj, BLR, p);
3364 p += 4;
3367 for (i = lowest_restf; i <= MAX_SAVE_FPR; i++)
3369 unsigned int fpr = i << 21;
3370 unsigned int stackoff = (1 << 16) - (MAX_SAVE_FPR + 1 - i) * 8;
3371 bfd_put_32 (htab->elf.dynobj, LFD_FR0_0R1 + fpr + stackoff, p);
3372 p += 4;
3374 if (lowest_restf <= MAX_SAVE_FPR
3375 || htab->sfpr->_raw_size == 4)
3377 bfd_put_32 (htab->elf.dynobj, BLR, p);
3380 return TRUE;
3383 /* Adjust a symbol defined by a dynamic object and referenced by a
3384 regular object. The current definition is in some section of the
3385 dynamic object, but we're not including those sections. We have to
3386 change the definition to something the rest of the link can
3387 understand. */
3389 static bfd_boolean
3390 ppc64_elf_adjust_dynamic_symbol (info, h)
3391 struct bfd_link_info *info;
3392 struct elf_link_hash_entry *h;
3394 struct ppc_link_hash_table *htab;
3395 struct ppc_link_hash_entry * eh;
3396 struct ppc_dyn_relocs *p;
3397 asection *s;
3398 unsigned int power_of_two;
3400 htab = ppc_hash_table (info);
3402 /* Deal with function syms. */
3403 if (h->type == STT_FUNC
3404 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
3406 /* Clear procedure linkage table information for any symbol that
3407 won't need a .plt entry. */
3408 if (!((struct ppc_link_hash_entry *) h)->is_func_descriptor
3409 || h->plt.refcount <= 0
3410 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
3411 || (! info->shared
3412 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
3413 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0))
3415 h->plt.offset = (bfd_vma) -1;
3416 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3418 return TRUE;
3420 else
3421 h->plt.offset = (bfd_vma) -1;
3423 /* If this is a weak symbol, and there is a real definition, the
3424 processor independent code will have arranged for us to see the
3425 real definition first, and we can just use the same value. */
3426 if (h->weakdef != NULL)
3428 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
3429 || h->weakdef->root.type == bfd_link_hash_defweak);
3430 h->root.u.def.section = h->weakdef->root.u.def.section;
3431 h->root.u.def.value = h->weakdef->root.u.def.value;
3432 return TRUE;
3435 /* This is a reference to a symbol defined by a dynamic object which
3436 is not a function. */
3438 /* If we are creating a shared library, we must presume that the
3439 only references to the symbol are via the global offset table.
3440 For such cases we need not do anything here; the relocations will
3441 be handled correctly by relocate_section. */
3442 if (info->shared)
3443 return TRUE;
3445 /* If there are no references to this symbol that do not use the
3446 GOT, we don't need to generate a copy reloc. */
3447 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
3448 return TRUE;
3450 eh = (struct ppc_link_hash_entry *) h;
3451 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3453 s = p->sec->output_section;
3454 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3455 break;
3458 /* If we didn't find any dynamic relocs in read-only sections, then
3459 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3460 if (p == NULL)
3462 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
3463 return TRUE;
3466 /* We must allocate the symbol in our .dynbss section, which will
3467 become part of the .bss section of the executable. There will be
3468 an entry for this symbol in the .dynsym section. The dynamic
3469 object will contain position independent code, so all references
3470 from the dynamic object to this symbol will go through the global
3471 offset table. The dynamic linker will use the .dynsym entry to
3472 determine the address it must put in the global offset table, so
3473 both the dynamic object and the regular object will refer to the
3474 same memory location for the variable. */
3476 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
3477 to copy the initial value out of the dynamic object and into the
3478 runtime process image. We need to remember the offset into the
3479 .rela.bss section we are going to use. */
3480 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3482 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
3483 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
3486 /* We need to figure out the alignment required for this symbol. I
3487 have no idea how ELF linkers handle this. */
3488 power_of_two = bfd_log2 (h->size);
3489 if (power_of_two > 4)
3490 power_of_two = 4;
3492 /* Apply the required alignment. */
3493 s = htab->sdynbss;
3494 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
3495 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
3497 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
3498 return FALSE;
3501 /* Define the symbol as being at this point in the section. */
3502 h->root.u.def.section = s;
3503 h->root.u.def.value = s->_raw_size;
3505 /* Increment the section size to make room for the symbol. */
3506 s->_raw_size += h->size;
3508 return TRUE;
3511 /* If given a function descriptor symbol, hide both the function code
3512 sym and the descriptor. */
3513 static void
3514 ppc64_elf_hide_symbol (info, h, force_local)
3515 struct bfd_link_info *info;
3516 struct elf_link_hash_entry *h;
3517 bfd_boolean force_local;
3519 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
3521 if (((struct ppc_link_hash_entry *) h)->is_func_descriptor)
3523 struct elf_link_hash_entry *fh = ((struct ppc_link_hash_entry *) h)->oh;
3525 if (fh == NULL)
3527 const char *p, *q;
3528 struct ppc_link_hash_table *htab;
3529 char save;
3531 /* We aren't supposed to use alloca in BFD because on
3532 systems which do not have alloca the version in libiberty
3533 calls xmalloc, which might cause the program to crash
3534 when it runs out of memory. This function doesn't have a
3535 return status, so there's no way to gracefully return an
3536 error. So cheat. We know that string[-1] can be safely
3537 dereferenced; It's either a string in an ELF string
3538 table, or allocated in an objalloc structure. */
3540 p = h->root.root.string - 1;
3541 save = *p;
3542 *(char *) p = '.';
3543 htab = ppc_hash_table (info);
3544 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
3545 *(char *) p = save;
3547 /* Unfortunately, if it so happens that the string we were
3548 looking for was allocated immediately before this string,
3549 then we overwrote the string terminator. That's the only
3550 reason the lookup should fail. */
3551 if (fh == NULL)
3553 q = h->root.root.string + strlen (h->root.root.string);
3554 while (q >= h->root.root.string && *q == *p)
3555 --q, --p;
3556 if (q < h->root.root.string && *p == '.')
3557 fh = elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
3559 if (fh != NULL)
3561 ((struct ppc_link_hash_entry *) h)->oh = fh;
3562 ((struct ppc_link_hash_entry *) fh)->oh = h;
3565 if (fh != NULL)
3566 _bfd_elf_link_hash_hide_symbol (info, fh, force_local);
3570 bfd_boolean
3571 ppc64_elf_edit_opd (obfd, info)
3572 bfd *obfd;
3573 struct bfd_link_info *info;
3575 bfd *ibfd;
3576 unsigned int bfd_indx;
3578 for (bfd_indx = 0, ibfd = info->input_bfds;
3579 ibfd != NULL;
3580 ibfd = ibfd->link_next, bfd_indx++)
3582 asection *sec;
3583 Elf_Internal_Rela *relstart, *rel, *relend;
3584 Elf_Internal_Shdr *symtab_hdr;
3585 Elf_Internal_Sym *local_syms;
3586 struct elf_link_hash_entry **sym_hashes;
3587 bfd_vma offset;
3588 bfd_size_type amt;
3589 long *adjust;
3590 bfd_boolean need_edit;
3592 sec = bfd_get_section_by_name (ibfd, ".opd");
3593 if (sec == NULL)
3594 continue;
3596 amt = sec->_raw_size * sizeof (long) / 24;
3597 adjust = ppc64_elf_section_data (sec)->opd.adjust;
3598 if (adjust == NULL)
3600 /* Must be a ld -r link. ie. check_relocs hasn't been
3601 called. */
3602 adjust = (long *) bfd_zalloc (obfd, amt);
3603 ppc64_elf_section_data (sec)->opd.adjust = adjust;
3605 memset (adjust, 0, (size_t) amt);
3607 if (sec->output_section == bfd_abs_section_ptr)
3608 continue;
3610 /* Look through the section relocs. */
3611 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
3612 continue;
3614 local_syms = NULL;
3615 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3616 sym_hashes = elf_sym_hashes (ibfd);
3618 /* Read the relocations. */
3619 relstart = _bfd_elf64_link_read_relocs (ibfd, sec, (PTR) NULL,
3620 (Elf_Internal_Rela *) NULL,
3621 info->keep_memory);
3622 if (relstart == NULL)
3623 return FALSE;
3625 /* First run through the relocs to check they are sane, and to
3626 determine whether we need to edit this opd section. */
3627 need_edit = FALSE;
3628 offset = 0;
3629 relend = relstart + sec->reloc_count;
3630 for (rel = relstart; rel < relend; rel++)
3632 enum elf_ppc64_reloc_type r_type;
3633 unsigned long r_symndx;
3634 asection *sym_sec;
3635 struct elf_link_hash_entry *h;
3636 Elf_Internal_Sym *sym;
3638 /* .opd contains a regular array of 24 byte entries. We're
3639 only interested in the reloc pointing to a function entry
3640 point. */
3641 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
3642 if (r_type == R_PPC64_TOC)
3643 continue;
3645 if (r_type != R_PPC64_ADDR64)
3647 (*_bfd_error_handler)
3648 (_("%s: unexpected reloc type %u in .opd section"),
3649 bfd_archive_filename (ibfd), r_type);
3650 need_edit = FALSE;
3651 break;
3654 if (rel + 1 >= relend)
3655 continue;
3656 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE ((rel + 1)->r_info);
3657 if (r_type != R_PPC64_TOC)
3658 continue;
3660 if (rel->r_offset != offset)
3662 /* If someone messes with .opd alignment then after a
3663 "ld -r" we might have padding in the middle of .opd.
3664 Also, there's nothing to prevent someone putting
3665 something silly in .opd with the assembler. No .opd
3666 optimization for them! */
3667 (*_bfd_error_handler)
3668 (_("%s: .opd is not a regular array of opd entries"),
3669 bfd_archive_filename (ibfd));
3670 need_edit = FALSE;
3671 break;
3674 r_symndx = ELF64_R_SYM (rel->r_info);
3675 sym_sec = NULL;
3676 h = NULL;
3677 sym = NULL;
3678 if (r_symndx >= symtab_hdr->sh_info)
3680 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3681 while (h->root.type == bfd_link_hash_indirect
3682 || h->root.type == bfd_link_hash_warning)
3683 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3684 if (h->root.type == bfd_link_hash_defined
3685 || h->root.type == bfd_link_hash_defweak)
3686 sym_sec = h->root.u.def.section;
3688 else
3690 if (local_syms == NULL)
3692 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3693 if (local_syms == NULL)
3694 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
3695 symtab_hdr->sh_info, 0,
3696 NULL, NULL, NULL);
3697 if (local_syms == NULL)
3698 goto error_free_rel;
3700 sym = local_syms + r_symndx;
3701 if ((sym->st_shndx != SHN_UNDEF
3702 && sym->st_shndx < SHN_LORESERVE)
3703 || sym->st_shndx > SHN_HIRESERVE)
3704 sym_sec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
3707 if (sym_sec == NULL || sym_sec->owner == NULL)
3709 (*_bfd_error_handler)
3710 (_("%s: undefined sym `%s' in .opd section"),
3711 bfd_archive_filename (ibfd),
3712 h != NULL ? h->root.root.string : "<local symbol>");
3713 need_edit = FALSE;
3714 break;
3717 /* opd entries are always for functions defined in the
3718 current input bfd. If the symbol isn't defined in the
3719 input bfd, then we won't be using the function in this
3720 bfd; It must be defined in a linkonce section in another
3721 bfd, or is weak. It's also possible that we are
3722 discarding the function due to a linker script /DISCARD/,
3723 which we test for via the output_section. */
3724 if (sym_sec->owner != ibfd
3725 || sym_sec->output_section == bfd_abs_section_ptr)
3726 need_edit = TRUE;
3728 offset += 24;
3731 if (need_edit)
3733 Elf_Internal_Rela *write_rel;
3734 bfd_byte *rptr, *wptr;
3735 bfd_boolean skip;
3737 /* This seems a waste of time as input .opd sections are all
3738 zeros as generated by gcc, but I suppose there's no reason
3739 this will always be so. We might start putting something in
3740 the third word of .opd entries. */
3741 if ((sec->flags & SEC_IN_MEMORY) == 0)
3743 bfd_byte *loc = bfd_alloc (ibfd, sec->_raw_size);
3744 if (loc == NULL
3745 || !bfd_get_section_contents (ibfd, sec, loc, (bfd_vma) 0,
3746 sec->_raw_size))
3748 if (local_syms != NULL
3749 && symtab_hdr->contents != (unsigned char *) local_syms)
3750 free (local_syms);
3751 error_free_rel:
3752 if (elf_section_data (sec)->relocs != relstart)
3753 free (relstart);
3754 return FALSE;
3756 sec->contents = loc;
3757 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
3760 elf_section_data (sec)->relocs = relstart;
3762 wptr = sec->contents;
3763 rptr = sec->contents;
3764 write_rel = relstart;
3765 skip = FALSE;
3766 offset = 0;
3767 for (rel = relstart; rel < relend; rel++)
3769 if (rel->r_offset == offset)
3771 unsigned long r_symndx;
3772 asection *sym_sec;
3773 struct elf_link_hash_entry *h;
3774 Elf_Internal_Sym *sym;
3776 r_symndx = ELF64_R_SYM (rel->r_info);
3777 sym_sec = NULL;
3778 h = NULL;
3779 sym = NULL;
3780 if (r_symndx >= symtab_hdr->sh_info)
3782 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3783 while (h->root.type == bfd_link_hash_indirect
3784 || h->root.type == bfd_link_hash_warning)
3785 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3786 if (h->root.type == bfd_link_hash_defined
3787 || h->root.type == bfd_link_hash_defweak)
3788 sym_sec = h->root.u.def.section;
3790 else
3792 sym = local_syms + r_symndx;
3793 if ((sym->st_shndx != SHN_UNDEF
3794 && sym->st_shndx < SHN_LORESERVE)
3795 || sym->st_shndx > SHN_HIRESERVE)
3796 sym_sec = bfd_section_from_elf_index (ibfd,
3797 sym->st_shndx);
3800 skip = (sym_sec->owner != ibfd
3801 || sym_sec->output_section == bfd_abs_section_ptr);
3802 if (skip)
3804 if (h != NULL && sym_sec->owner == ibfd)
3806 /* Arrange for the function descriptor sym
3807 to be dropped. */
3808 struct ppc_link_hash_entry *fdh;
3809 struct ppc_link_hash_entry *fh;
3811 fh = (struct ppc_link_hash_entry *) h;
3812 fdh = (struct ppc_link_hash_entry *) fh->oh;
3813 if (fdh == NULL)
3815 const char *fd_name;
3816 struct ppc_link_hash_table *htab;
3818 fd_name = h->root.root.string + 1;
3819 htab = ppc_hash_table (info);
3820 fdh = (struct ppc_link_hash_entry *)
3821 elf_link_hash_lookup (&htab->elf, fd_name,
3822 FALSE, FALSE, FALSE);
3823 fdh->is_func_descriptor = 1;
3824 fdh->oh = &fh->elf;
3825 fh->is_func = 1;
3826 fh->oh = &fdh->elf;
3829 fdh->elf.root.u.def.value = 0;
3830 fdh->elf.root.u.def.section = sym_sec;
3833 else
3835 /* We'll be keeping this opd entry. */
3837 if (h != NULL)
3839 /* Redefine the function descriptor symbol
3840 to this location in the opd section.
3841 We've checked above that opd relocs are
3842 ordered. */
3843 struct ppc_link_hash_entry *fdh;
3844 struct ppc_link_hash_entry *fh;
3846 fh = (struct ppc_link_hash_entry *) h;
3847 fdh = (struct ppc_link_hash_entry *) fh->oh;
3848 if (fdh == NULL)
3850 const char *fd_name;
3851 struct ppc_link_hash_table *htab;
3853 fd_name = h->root.root.string + 1;
3854 htab = ppc_hash_table (info);
3855 fdh = (struct ppc_link_hash_entry *)
3856 elf_link_hash_lookup (&htab->elf, fd_name,
3857 FALSE, FALSE, FALSE);
3858 fdh->is_func_descriptor = 1;
3859 fdh->oh = &fh->elf;
3860 fh->is_func = 1;
3861 fh->oh = &fdh->elf;
3864 fdh->elf.root.u.def.value = wptr - sec->contents;
3866 else
3868 /* Local syms are a bit tricky. We could
3869 tweak them as they can be cached, but
3870 we'd need to look through the local syms
3871 for the function descriptor sym which we
3872 don't have at the moment. So keep an
3873 array of adjustments. */
3874 adjust[rel->r_offset / 24] = wptr - rptr;
3877 if (wptr != rptr)
3878 memcpy (wptr, rptr, 24);
3879 wptr += 24;
3881 rptr += 24;
3882 offset += 24;
3885 /* We need to adjust any reloc offsets to point to the
3886 new opd entries. While we're at it, we may as well
3887 remove redundant relocs. */
3888 if (!skip)
3890 rel->r_offset += wptr - rptr;
3891 if (write_rel != rel)
3892 memcpy (write_rel, rel, sizeof (*rel));
3893 ++write_rel;
3897 sec->_cooked_size = wptr - sec->contents;
3898 sec->reloc_count = write_rel - relstart;
3899 /* Fudge the size too, as this is used later in
3900 elf_bfd_final_link if we are emitting relocs. */
3901 elf_section_data (sec)->rel_hdr.sh_size
3902 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
3903 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
3905 else if (elf_section_data (sec)->relocs != relstart)
3906 free (relstart);
3908 if (local_syms != NULL
3909 && symtab_hdr->contents != (unsigned char *) local_syms)
3911 if (!info->keep_memory)
3912 free (local_syms);
3913 else
3914 symtab_hdr->contents = (unsigned char *) local_syms;
3918 return TRUE;
3921 /* This is the condition under which ppc64_elf_finish_dynamic_symbol
3922 will be called from elflink.h. If elflink.h doesn't call our
3923 finish_dynamic_symbol routine, we'll need to do something about
3924 initializing any .plt and .got entries in ppc64_elf_relocate_section. */
3925 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
3926 ((DYN) \
3927 && ((INFO)->shared \
3928 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
3929 && ((H)->dynindx != -1 \
3930 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
3932 /* Allocate space in .plt, .got and associated reloc sections for
3933 dynamic relocs. */
3935 static bfd_boolean
3936 allocate_dynrelocs (h, inf)
3937 struct elf_link_hash_entry *h;
3938 PTR inf;
3940 struct bfd_link_info *info;
3941 struct ppc_link_hash_table *htab;
3942 asection *s;
3943 struct ppc_link_hash_entry *eh;
3944 struct ppc_dyn_relocs *p;
3946 if (h->root.type == bfd_link_hash_indirect)
3947 return TRUE;
3949 if (h->root.type == bfd_link_hash_warning)
3950 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3952 info = (struct bfd_link_info *) inf;
3953 htab = ppc_hash_table (info);
3955 if (htab->elf.dynamic_sections_created
3956 && h->plt.refcount > 0
3957 && h->dynindx != -1)
3959 BFD_ASSERT (((struct ppc_link_hash_entry *) h)->is_func_descriptor);
3961 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
3963 /* If this is the first .plt entry, make room for the special
3964 first entry. */
3965 s = htab->splt;
3966 if (s->_raw_size == 0)
3967 s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
3969 h->plt.offset = s->_raw_size;
3971 /* Make room for this entry. */
3972 s->_raw_size += PLT_ENTRY_SIZE;
3974 /* Make room for the .glink code. */
3975 s = htab->sglink;
3976 if (s->_raw_size == 0)
3977 s->_raw_size += GLINK_CALL_STUB_SIZE;
3978 /* We need bigger stubs past index 32767. */
3979 if (s->_raw_size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
3980 s->_raw_size += 4;
3981 s->_raw_size += 2*4;
3983 /* We also need to make an entry in the .rela.plt section. */
3984 s = htab->srelplt;
3985 s->_raw_size += sizeof (Elf64_External_Rela);
3987 else
3989 h->plt.offset = (bfd_vma) -1;
3990 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3993 else
3995 h->plt.offset = (bfd_vma) -1;
3996 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
3999 if (h->got.refcount > 0)
4001 bfd_boolean dyn;
4003 /* Make sure this symbol is output as a dynamic symbol.
4004 Undefined weak syms won't yet be marked as dynamic. */
4005 if (h->dynindx == -1
4006 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4008 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
4009 return FALSE;
4012 s = htab->sgot;
4013 h->got.offset = s->_raw_size;
4014 s->_raw_size += 8;
4015 dyn = htab->elf.dynamic_sections_created;
4016 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
4017 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
4019 else
4020 h->got.offset = (bfd_vma) -1;
4022 eh = (struct ppc_link_hash_entry *) h;
4023 if (eh->dyn_relocs == NULL)
4024 return TRUE;
4026 /* In the shared -Bsymbolic case, discard space allocated for
4027 dynamic pc-relative relocs against symbols which turn out to be
4028 defined in regular objects. For the normal shared case, discard
4029 space for relocs that have become local due to symbol visibility
4030 changes. */
4032 if (info->shared)
4034 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
4035 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
4036 || info->symbolic))
4038 struct ppc_dyn_relocs **pp;
4040 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
4042 p->count -= p->pc_count;
4043 p->pc_count = 0;
4044 if (p->count == 0)
4045 *pp = p->next;
4046 else
4047 pp = &p->next;
4051 else
4053 /* For the non-shared case, discard space for relocs against
4054 symbols which turn out to need copy relocs or are not
4055 dynamic. */
4057 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
4058 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
4059 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
4060 || (htab->elf.dynamic_sections_created
4061 && (h->root.type == bfd_link_hash_undefweak
4062 || h->root.type == bfd_link_hash_undefined))))
4064 /* Make sure this symbol is output as a dynamic symbol.
4065 Undefined weak syms won't yet be marked as dynamic. */
4066 if (h->dynindx == -1
4067 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
4069 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
4070 return FALSE;
4073 /* If that succeeded, we know we'll be keeping all the
4074 relocs. */
4075 if (h->dynindx != -1)
4076 goto keep;
4079 eh->dyn_relocs = NULL;
4081 keep: ;
4084 /* Finally, allocate space. */
4085 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4087 asection *sreloc = elf_section_data (p->sec)->sreloc;
4088 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
4091 return TRUE;
4094 /* Find any dynamic relocs that apply to read-only sections. */
4096 static bfd_boolean
4097 readonly_dynrelocs (h, inf)
4098 struct elf_link_hash_entry *h;
4099 PTR inf;
4101 struct ppc_link_hash_entry *eh;
4102 struct ppc_dyn_relocs *p;
4104 if (h->root.type == bfd_link_hash_warning)
4105 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4107 eh = (struct ppc_link_hash_entry *) h;
4108 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4110 asection *s = p->sec->output_section;
4112 if (s != NULL && (s->flags & SEC_READONLY) != 0)
4114 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4116 info->flags |= DF_TEXTREL;
4118 /* Not an error, just cut short the traversal. */
4119 return FALSE;
4122 return TRUE;
4125 /* Set the sizes of the dynamic sections. */
4127 static bfd_boolean
4128 ppc64_elf_size_dynamic_sections (output_bfd, info)
4129 bfd *output_bfd ATTRIBUTE_UNUSED;
4130 struct bfd_link_info *info;
4132 struct ppc_link_hash_table *htab;
4133 bfd *dynobj;
4134 asection *s;
4135 bfd_boolean relocs;
4136 bfd *ibfd;
4138 htab = ppc_hash_table (info);
4139 dynobj = htab->elf.dynobj;
4140 if (dynobj == NULL)
4141 abort ();
4143 if (htab->elf.dynamic_sections_created)
4145 /* Set the contents of the .interp section to the interpreter. */
4146 if (! info->shared)
4148 s = bfd_get_section_by_name (dynobj, ".interp");
4149 if (s == NULL)
4150 abort ();
4151 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
4152 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4156 /* Set up .got offsets for local syms, and space for local dynamic
4157 relocs. */
4158 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
4160 bfd_signed_vma *local_got;
4161 bfd_signed_vma *end_local_got;
4162 bfd_size_type locsymcount;
4163 Elf_Internal_Shdr *symtab_hdr;
4164 asection *srel;
4166 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4167 continue;
4169 for (s = ibfd->sections; s != NULL; s = s->next)
4171 struct ppc_dyn_relocs *p;
4173 for (p = *((struct ppc_dyn_relocs **)
4174 &elf_section_data (s)->local_dynrel);
4175 p != NULL;
4176 p = p->next)
4178 if (!bfd_is_abs_section (p->sec)
4179 && bfd_is_abs_section (p->sec->output_section))
4181 /* Input section has been discarded, either because
4182 it is a copy of a linkonce section or due to
4183 linker script /DISCARD/, so we'll be discarding
4184 the relocs too. */
4186 else if (p->count != 0)
4188 srel = elf_section_data (p->sec)->sreloc;
4189 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
4190 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4191 info->flags |= DF_TEXTREL;
4196 local_got = elf_local_got_refcounts (ibfd);
4197 if (!local_got)
4198 continue;
4200 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4201 locsymcount = symtab_hdr->sh_info;
4202 end_local_got = local_got + locsymcount;
4203 s = htab->sgot;
4204 srel = htab->srelgot;
4205 for (; local_got < end_local_got; ++local_got)
4207 if (*local_got > 0)
4209 *local_got = s->_raw_size;
4210 s->_raw_size += 8;
4211 if (info->shared)
4212 srel->_raw_size += sizeof (Elf64_External_Rela);
4214 else
4215 *local_got = (bfd_vma) -1;
4219 /* Allocate global sym .plt and .got entries, and space for global
4220 sym dynamic relocs. */
4221 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
4223 /* We now have determined the sizes of the various dynamic sections.
4224 Allocate memory for them. */
4225 relocs = FALSE;
4226 for (s = dynobj->sections; s != NULL; s = s->next)
4228 if ((s->flags & SEC_LINKER_CREATED) == 0)
4229 continue;
4231 if (s == htab->sbrlt || s == htab->srelbrlt)
4232 /* These haven't been allocated yet; don't strip. */
4233 continue;
4234 else if (s == htab->splt
4235 || s == htab->sgot
4236 || s == htab->sglink)
4238 /* Strip this section if we don't need it; see the
4239 comment below. */
4241 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4243 if (s->_raw_size == 0)
4245 /* If we don't need this section, strip it from the
4246 output file. This is mostly to handle .rela.bss and
4247 .rela.plt. We must create both sections in
4248 create_dynamic_sections, because they must be created
4249 before the linker maps input sections to output
4250 sections. The linker does that before
4251 adjust_dynamic_symbol is called, and it is that
4252 function which decides whether anything needs to go
4253 into these sections. */
4255 else
4257 if (s != htab->srelplt)
4258 relocs = TRUE;
4260 /* We use the reloc_count field as a counter if we need
4261 to copy relocs into the output file. */
4262 s->reloc_count = 0;
4265 else
4267 /* It's not one of our sections, so don't allocate space. */
4268 continue;
4271 if (s->_raw_size == 0)
4273 _bfd_strip_section_from_output (info, s);
4274 continue;
4277 /* .plt is in the bss section. We don't initialise it. */
4278 if ((s->flags & SEC_LOAD) == 0)
4279 continue;
4281 /* Allocate memory for the section contents. We use bfd_zalloc
4282 here in case unused entries are not reclaimed before the
4283 section's contents are written out. This should not happen,
4284 but this way if it does, we get a R_PPC64_NONE reloc instead
4285 of garbage. */
4286 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
4287 if (s->contents == NULL)
4288 return FALSE;
4291 if (htab->elf.dynamic_sections_created)
4293 /* Add some entries to the .dynamic section. We fill in the
4294 values later, in ppc64_elf_finish_dynamic_sections, but we
4295 must add the entries now so that we get the correct size for
4296 the .dynamic section. The DT_DEBUG entry is filled in by the
4297 dynamic linker and used by the debugger. */
4298 #define add_dynamic_entry(TAG, VAL) \
4299 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
4301 if (!info->shared)
4303 if (!add_dynamic_entry (DT_DEBUG, 0))
4304 return FALSE;
4307 if (htab->splt != NULL && htab->splt->_raw_size != 0)
4309 if (!add_dynamic_entry (DT_PLTGOT, 0)
4310 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4311 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4312 || !add_dynamic_entry (DT_JMPREL, 0)
4313 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
4314 return FALSE;
4317 if (NO_OPD_RELOCS)
4319 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
4320 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
4321 return FALSE;
4324 if (relocs)
4326 if (!add_dynamic_entry (DT_RELA, 0)
4327 || !add_dynamic_entry (DT_RELASZ, 0)
4328 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
4329 return FALSE;
4331 /* If any dynamic relocs apply to a read-only section,
4332 then we need a DT_TEXTREL entry. */
4333 if ((info->flags & DF_TEXTREL) == 0)
4334 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
4335 (PTR) info);
4337 if ((info->flags & DF_TEXTREL) != 0)
4339 if (!add_dynamic_entry (DT_TEXTREL, 0))
4340 return FALSE;
4344 #undef add_dynamic_entry
4346 return TRUE;
4349 /* Determine the type of stub needed, if any, for a call. */
4351 static INLINE enum ppc_stub_type
4352 ppc_type_of_stub (input_sec, rel, hash, destination)
4353 asection *input_sec;
4354 const Elf_Internal_Rela *rel;
4355 struct ppc_link_hash_entry **hash;
4356 bfd_vma destination;
4358 struct ppc_link_hash_entry *h = *hash;
4359 bfd_vma location;
4360 bfd_vma branch_offset;
4361 bfd_vma max_branch_offset;
4362 unsigned int r_type;
4364 if (h != NULL)
4366 if (h->oh != NULL
4367 && h->oh->plt.offset != (bfd_vma) -1
4368 && h->oh->dynindx != -1)
4370 *hash = (struct ppc_link_hash_entry *) h->oh;
4371 return ppc_stub_plt_call;
4374 if (h->elf.root.type == bfd_link_hash_undefweak
4375 || h->elf.root.type == bfd_link_hash_undefined)
4376 return ppc_stub_none;
4379 /* Determine where the call point is. */
4380 location = (input_sec->output_offset
4381 + input_sec->output_section->vma
4382 + rel->r_offset);
4384 branch_offset = destination - location;
4385 r_type = ELF64_R_TYPE (rel->r_info);
4387 /* Determine if a long branch stub is needed. */
4388 max_branch_offset = 1 << 25;
4389 if (r_type != (unsigned int) R_PPC64_REL24)
4390 max_branch_offset = 1 << 15;
4392 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
4393 /* We need a stub. Figure out whether a long_branch or plt_branch
4394 is needed later. */
4395 return ppc_stub_long_branch;
4397 return ppc_stub_none;
4400 /* Build a .plt call stub. */
4402 static bfd_byte *
4403 build_plt_stub (obfd, p, offset, glink)
4404 bfd *obfd;
4405 bfd_byte *p;
4406 int offset;
4407 int glink;
4409 #define PPC_LO(v) ((v) & 0xffff)
4410 #define PPC_HI(v) (((v) >> 16) & 0xffff)
4411 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
4413 if (glink)
4414 bfd_put_32 (obfd, LD_R2_40R1, p), p += 4;
4415 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
4416 if (!glink)
4417 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
4418 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4419 if (PPC_HA (offset + 8) != PPC_HA (offset))
4420 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4421 offset += 8;
4422 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
4423 if (PPC_HA (offset + 8) != PPC_HA (offset))
4424 bfd_put_32 (obfd, ADDIS_R12_R12_1, p), p += 4;
4425 offset += 8;
4426 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
4427 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
4428 bfd_put_32 (obfd, BCTR, p), p += 4;
4429 return p;
4432 static bfd_boolean
4433 ppc_build_one_stub (gen_entry, in_arg)
4434 struct bfd_hash_entry *gen_entry;
4435 PTR in_arg;
4437 struct ppc_stub_hash_entry *stub_entry;
4438 struct ppc_branch_hash_entry *br_entry;
4439 struct bfd_link_info *info;
4440 struct ppc_link_hash_table *htab;
4441 asection *stub_sec;
4442 bfd *stub_bfd;
4443 bfd_byte *loc;
4444 bfd_byte *p;
4445 unsigned int indx;
4446 bfd_vma off;
4447 int size;
4449 /* Massage our args to the form they really have. */
4450 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4451 info = (struct bfd_link_info *) in_arg;
4453 htab = ppc_hash_table (info);
4454 stub_sec = stub_entry->stub_sec;
4456 /* Make a note of the offset within the stubs for this entry. */
4457 stub_entry->stub_offset = stub_sec->_cooked_size;
4458 loc = stub_sec->contents + stub_entry->stub_offset;
4460 stub_bfd = stub_sec->owner;
4462 switch (stub_entry->stub_type)
4464 case ppc_stub_long_branch:
4465 /* Branches are relative. This is where we are going to. */
4466 off = (stub_entry->target_value
4467 + stub_entry->target_section->output_offset
4468 + stub_entry->target_section->output_section->vma);
4470 /* And this is where we are coming from. */
4471 off -= (stub_entry->stub_offset
4472 + stub_sec->output_offset
4473 + stub_sec->output_section->vma);
4475 BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
4477 bfd_put_32 (stub_bfd, (bfd_vma) B_DOT | (off & 0x3fffffc), loc);
4478 size = 4;
4479 break;
4481 case ppc_stub_plt_branch:
4482 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4483 stub_entry->root.string + 9,
4484 FALSE, FALSE);
4485 if (br_entry == NULL)
4487 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
4488 stub_entry->root.string + 9);
4489 htab->stub_error = TRUE;
4490 return FALSE;
4493 off = (stub_entry->target_value
4494 + stub_entry->target_section->output_offset
4495 + stub_entry->target_section->output_section->vma);
4497 bfd_put_64 (htab->sbrlt->owner, off,
4498 htab->sbrlt->contents + br_entry->offset);
4500 if (info->shared)
4502 /* Create a reloc for the branch lookup table entry. */
4503 Elf_Internal_Rela rela;
4504 bfd_byte *loc;
4506 rela.r_offset = (br_entry->offset
4507 + htab->sbrlt->output_offset
4508 + htab->sbrlt->output_section->vma);
4509 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
4510 rela.r_addend = off;
4512 loc = htab->srelbrlt->contents;
4513 loc += htab->srelbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
4514 bfd_elf64_swap_reloca_out (htab->srelbrlt->owner, &rela, loc);
4517 off = (br_entry->offset
4518 + htab->sbrlt->output_offset
4519 + htab->sbrlt->output_section->vma
4520 - elf_gp (htab->sbrlt->output_section->owner)
4521 - TOC_BASE_OFF);
4523 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4525 (*_bfd_error_handler)
4526 (_("linkage table error against `%s'"),
4527 stub_entry->root.string);
4528 bfd_set_error (bfd_error_bad_value);
4529 htab->stub_error = TRUE;
4530 return FALSE;
4533 indx = off;
4534 bfd_put_32 (stub_bfd, (bfd_vma) ADDIS_R12_R2 | PPC_HA (indx), loc);
4535 bfd_put_32 (stub_bfd, (bfd_vma) LD_R11_0R12 | PPC_LO (indx), loc + 4);
4536 bfd_put_32 (stub_bfd, (bfd_vma) MTCTR_R11, loc + 8);
4537 bfd_put_32 (stub_bfd, (bfd_vma) BCTR, loc + 12);
4538 size = 16;
4539 break;
4541 case ppc_stub_plt_call:
4542 /* Do the best we can for shared libraries built without
4543 exporting ".foo" for each "foo". This can happen when symbol
4544 versioning scripts strip all bar a subset of symbols. */
4545 if (stub_entry->h->oh->root.type != bfd_link_hash_defined
4546 && stub_entry->h->oh->root.type != bfd_link_hash_defweak)
4548 /* Point the symbol at the stub. There may be multiple stubs,
4549 we don't really care; The main thing is to make this sym
4550 defined somewhere. */
4551 stub_entry->h->oh->root.type = bfd_link_hash_defined;
4552 stub_entry->h->oh->root.u.def.section = stub_entry->stub_sec;
4553 stub_entry->h->oh->root.u.def.value = stub_entry->stub_offset;
4556 /* Now build the stub. */
4557 off = stub_entry->h->elf.plt.offset;
4558 if (off >= (bfd_vma) -2)
4559 abort ();
4561 off &= ~ (bfd_vma) 1;
4562 off += (htab->splt->output_offset
4563 + htab->splt->output_section->vma
4564 - elf_gp (htab->splt->output_section->owner)
4565 - TOC_BASE_OFF);
4567 if (off + 0x80000000 > 0xffffffff || (off & 7) != 0)
4569 (*_bfd_error_handler)
4570 (_("linkage table error against `%s'"),
4571 stub_entry->h->elf.root.root.string);
4572 bfd_set_error (bfd_error_bad_value);
4573 htab->stub_error = TRUE;
4574 return FALSE;
4577 p = build_plt_stub (stub_bfd, loc, (int) off, 0);
4578 size = p - loc;
4579 break;
4581 default:
4582 BFD_FAIL ();
4583 return FALSE;
4586 stub_sec->_cooked_size += size;
4587 return TRUE;
4590 /* As above, but don't actually build the stub. Just bump offset so
4591 we know stub section sizes, and select plt_branch stubs where
4592 long_branch stubs won't do. */
4594 static bfd_boolean
4595 ppc_size_one_stub (gen_entry, in_arg)
4596 struct bfd_hash_entry *gen_entry;
4597 PTR in_arg;
4599 struct ppc_stub_hash_entry *stub_entry;
4600 struct ppc_link_hash_table *htab;
4601 bfd_vma off;
4602 int size;
4604 /* Massage our args to the form they really have. */
4605 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4606 htab = (struct ppc_link_hash_table *) in_arg;
4608 if (stub_entry->stub_type == ppc_stub_plt_call)
4610 off = stub_entry->h->elf.plt.offset & ~(bfd_vma) 1;
4611 off += (htab->splt->output_offset
4612 + htab->splt->output_section->vma
4613 - elf_gp (htab->splt->output_section->owner)
4614 - TOC_BASE_OFF);
4616 size = 28;
4617 if (PPC_HA ((int) off + 16) != PPC_HA ((int) off))
4618 size += 4;
4620 else
4622 /* ppc_stub_long_branch or ppc_stub_plt_branch. */
4623 stub_entry->stub_type = ppc_stub_long_branch;
4624 size = 4;
4626 off = (stub_entry->target_value
4627 + stub_entry->target_section->output_offset
4628 + stub_entry->target_section->output_section->vma);
4629 off -= (stub_entry->stub_sec->_raw_size
4630 + stub_entry->stub_sec->output_offset
4631 + stub_entry->stub_sec->output_section->vma);
4633 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
4635 struct ppc_branch_hash_entry *br_entry;
4637 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
4638 stub_entry->root.string + 9,
4639 TRUE, FALSE);
4640 if (br_entry == NULL)
4642 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
4643 stub_entry->root.string + 9);
4644 htab->stub_error = TRUE;
4645 return FALSE;
4648 if (br_entry->iter != htab->stub_iteration)
4650 br_entry->iter = htab->stub_iteration;
4651 br_entry->offset = htab->sbrlt->_raw_size;
4652 htab->sbrlt->_raw_size += 8;
4654 stub_entry->stub_type = ppc_stub_plt_branch;
4655 size = 16;
4659 stub_entry->stub_sec->_raw_size += size;
4660 return TRUE;
4663 /* Set up various things so that we can make a list of input sections
4664 for each output section included in the link. Returns -1 on error,
4665 0 when no stubs will be needed, and 1 on success. */
4668 ppc64_elf_setup_section_lists (output_bfd, info)
4669 bfd *output_bfd;
4670 struct bfd_link_info *info;
4672 bfd *input_bfd;
4673 int top_id, top_index;
4674 asection *section;
4675 asection **input_list, **list;
4676 bfd_size_type amt;
4677 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4679 if (htab->elf.root.creator->flavour != bfd_target_elf_flavour
4680 || htab->sbrlt == NULL)
4681 return 0;
4683 /* Find the top input section id. */
4684 for (input_bfd = info->input_bfds, top_id = 0;
4685 input_bfd != NULL;
4686 input_bfd = input_bfd->link_next)
4688 for (section = input_bfd->sections;
4689 section != NULL;
4690 section = section->next)
4692 if (top_id < section->id)
4693 top_id = section->id;
4697 amt = sizeof (struct map_stub) * (top_id + 1);
4698 htab->stub_group = (struct map_stub *) bfd_zmalloc (amt);
4699 if (htab->stub_group == NULL)
4700 return -1;
4702 /* We can't use output_bfd->section_count here to find the top output
4703 section index as some sections may have been removed, and
4704 _bfd_strip_section_from_output doesn't renumber the indices. */
4705 for (section = output_bfd->sections, top_index = 0;
4706 section != NULL;
4707 section = section->next)
4709 if (top_index < section->index)
4710 top_index = section->index;
4713 htab->top_index = top_index;
4714 amt = sizeof (asection *) * (top_index + 1);
4715 input_list = (asection **) bfd_malloc (amt);
4716 htab->input_list = input_list;
4717 if (input_list == NULL)
4718 return -1;
4720 /* For sections we aren't interested in, mark their entries with a
4721 value we can check later. */
4722 list = input_list + top_index;
4724 *list = bfd_abs_section_ptr;
4725 while (list-- != input_list);
4727 for (section = output_bfd->sections;
4728 section != NULL;
4729 section = section->next)
4731 if ((section->flags & SEC_CODE) != 0)
4732 input_list[section->index] = NULL;
4735 return 1;
4738 /* The linker repeatedly calls this function for each input section,
4739 in the order that input sections are linked into output sections.
4740 Build lists of input sections to determine groupings between which
4741 we may insert linker stubs. */
4743 void
4744 ppc64_elf_next_input_section (info, isec)
4745 struct bfd_link_info *info;
4746 asection *isec;
4748 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4750 if (isec->output_section->index <= htab->top_index)
4752 asection **list = htab->input_list + isec->output_section->index;
4753 if (*list != bfd_abs_section_ptr)
4755 /* Steal the link_sec pointer for our list. */
4756 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
4757 /* This happens to make the list in reverse order,
4758 which is what we want. */
4759 PREV_SEC (isec) = *list;
4760 *list = isec;
4765 /* See whether we can group stub sections together. Grouping stub
4766 sections may result in fewer stubs. More importantly, we need to
4767 put all .init* and .fini* stubs at the beginning of the .init or
4768 .fini output sections respectively, because glibc splits the
4769 _init and _fini functions into multiple parts. Putting a stub in
4770 the middle of a function is not a good idea. */
4772 static void
4773 group_sections (htab, stub_group_size, stubs_always_before_branch)
4774 struct ppc_link_hash_table *htab;
4775 bfd_size_type stub_group_size;
4776 bfd_boolean stubs_always_before_branch;
4778 asection **list = htab->input_list + htab->top_index;
4781 asection *tail = *list;
4782 if (tail == bfd_abs_section_ptr)
4783 continue;
4784 while (tail != NULL)
4786 asection *curr;
4787 asection *prev;
4788 bfd_size_type total;
4790 curr = tail;
4791 if (tail->_cooked_size)
4792 total = tail->_cooked_size;
4793 else
4794 total = tail->_raw_size;
4795 while ((prev = PREV_SEC (curr)) != NULL
4796 && ((total += curr->output_offset - prev->output_offset)
4797 < stub_group_size))
4798 curr = prev;
4800 /* OK, the size from the start of CURR to the end is less
4801 than stub_group_size and thus can be handled by one stub
4802 section. (or the tail section is itself larger than
4803 stub_group_size, in which case we may be toast.) We
4804 should really be keeping track of the total size of stubs
4805 added here, as stubs contribute to the final output
4806 section size. That's a little tricky, and this way will
4807 only break if stubs added make the total size more than
4808 2^25, ie. for the default stub_group_size, if stubs total
4809 more than 2834432 bytes, or over 100000 plt call stubs. */
4812 prev = PREV_SEC (tail);
4813 /* Set up this stub group. */
4814 htab->stub_group[tail->id].link_sec = curr;
4816 while (tail != curr && (tail = prev) != NULL);
4818 /* But wait, there's more! Input sections up to stub_group_size
4819 bytes before the stub section can be handled by it too. */
4820 if (!stubs_always_before_branch)
4822 total = 0;
4823 while (prev != NULL
4824 && ((total += tail->output_offset - prev->output_offset)
4825 < stub_group_size))
4827 tail = prev;
4828 prev = PREV_SEC (tail);
4829 htab->stub_group[tail->id].link_sec = curr;
4832 tail = prev;
4835 while (list-- != htab->input_list);
4836 free (htab->input_list);
4837 #undef PREV_SEC
4840 /* Determine and set the size of the stub section for a final link.
4842 The basic idea here is to examine all the relocations looking for
4843 PC-relative calls to a target that is unreachable with a "bl"
4844 instruction. */
4846 bfd_boolean
4847 ppc64_elf_size_stubs (output_bfd, stub_bfd, info, group_size,
4848 add_stub_section, layout_sections_again)
4849 bfd *output_bfd;
4850 bfd *stub_bfd;
4851 struct bfd_link_info *info;
4852 bfd_signed_vma group_size;
4853 asection * (*add_stub_section) PARAMS ((const char *, asection *));
4854 void (*layout_sections_again) PARAMS ((void));
4856 bfd_size_type stub_group_size;
4857 bfd_boolean stubs_always_before_branch;
4858 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4860 /* Stash our params away. */
4861 htab->stub_bfd = stub_bfd;
4862 htab->add_stub_section = add_stub_section;
4863 htab->layout_sections_again = layout_sections_again;
4864 stubs_always_before_branch = group_size < 0;
4865 if (group_size < 0)
4866 stub_group_size = -group_size;
4867 else
4868 stub_group_size = group_size;
4869 if (stub_group_size == 1)
4871 /* Default values. */
4872 stub_group_size = 30720000;
4873 if (htab->has_14bit_branch)
4874 stub_group_size = 30000;
4877 group_sections (htab, stub_group_size, stubs_always_before_branch);
4879 while (1)
4881 bfd *input_bfd;
4882 unsigned int bfd_indx;
4883 asection *stub_sec;
4884 bfd_boolean stub_changed;
4886 htab->stub_iteration += 1;
4887 stub_changed = FALSE;
4889 for (input_bfd = info->input_bfds, bfd_indx = 0;
4890 input_bfd != NULL;
4891 input_bfd = input_bfd->link_next, bfd_indx++)
4893 Elf_Internal_Shdr *symtab_hdr;
4894 asection *section;
4895 Elf_Internal_Sym *local_syms = NULL;
4897 /* We'll need the symbol table in a second. */
4898 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4899 if (symtab_hdr->sh_info == 0)
4900 continue;
4902 /* Walk over each section attached to the input bfd. */
4903 for (section = input_bfd->sections;
4904 section != NULL;
4905 section = section->next)
4907 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
4909 /* If there aren't any relocs, then there's nothing more
4910 to do. */
4911 if ((section->flags & SEC_RELOC) == 0
4912 || section->reloc_count == 0)
4913 continue;
4915 /* If this section is a link-once section that will be
4916 discarded, then don't create any stubs. */
4917 if (section->output_section == NULL
4918 || section->output_section->owner != output_bfd)
4919 continue;
4921 /* Get the relocs. */
4922 internal_relocs
4923 = _bfd_elf64_link_read_relocs (input_bfd, section, NULL,
4924 (Elf_Internal_Rela *) NULL,
4925 info->keep_memory);
4926 if (internal_relocs == NULL)
4927 goto error_ret_free_local;
4929 /* Now examine each relocation. */
4930 irela = internal_relocs;
4931 irelaend = irela + section->reloc_count;
4932 for (; irela < irelaend; irela++)
4934 unsigned int r_type, r_indx;
4935 enum ppc_stub_type stub_type;
4936 struct ppc_stub_hash_entry *stub_entry;
4937 asection *sym_sec;
4938 bfd_vma sym_value;
4939 bfd_vma destination;
4940 struct ppc_link_hash_entry *hash;
4941 char *stub_name;
4942 const asection *id_sec;
4944 r_type = ELF64_R_TYPE (irela->r_info);
4945 r_indx = ELF64_R_SYM (irela->r_info);
4947 if (r_type >= (unsigned int) R_PPC64_max)
4949 bfd_set_error (bfd_error_bad_value);
4950 goto error_ret_free_internal;
4953 /* Only look for stubs on branch instructions. */
4954 if (r_type != (unsigned int) R_PPC64_REL24
4955 && r_type != (unsigned int) R_PPC64_REL14
4956 && r_type != (unsigned int) R_PPC64_REL14_BRTAKEN
4957 && r_type != (unsigned int) R_PPC64_REL14_BRNTAKEN)
4958 continue;
4960 /* Now determine the call target, its name, value,
4961 section. */
4962 sym_sec = NULL;
4963 sym_value = 0;
4964 destination = 0;
4965 hash = NULL;
4966 if (r_indx < symtab_hdr->sh_info)
4968 /* It's a local symbol. */
4969 Elf_Internal_Sym *sym;
4970 Elf_Internal_Shdr *hdr;
4972 if (local_syms == NULL)
4974 local_syms
4975 = (Elf_Internal_Sym *) symtab_hdr->contents;
4976 if (local_syms == NULL)
4977 local_syms
4978 = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4979 symtab_hdr->sh_info, 0,
4980 NULL, NULL, NULL);
4981 if (local_syms == NULL)
4982 goto error_ret_free_internal;
4984 sym = local_syms + r_indx;
4985 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
4986 sym_sec = hdr->bfd_section;
4987 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
4988 sym_value = sym->st_value;
4989 destination = (sym_value + irela->r_addend
4990 + sym_sec->output_offset
4991 + sym_sec->output_section->vma);
4993 else
4995 /* It's an external symbol. */
4996 int e_indx;
4998 e_indx = r_indx - symtab_hdr->sh_info;
4999 hash = ((struct ppc_link_hash_entry *)
5000 elf_sym_hashes (input_bfd)[e_indx]);
5002 while (hash->elf.root.type == bfd_link_hash_indirect
5003 || hash->elf.root.type == bfd_link_hash_warning)
5004 hash = ((struct ppc_link_hash_entry *)
5005 hash->elf.root.u.i.link);
5007 if (hash->elf.root.type == bfd_link_hash_defined
5008 || hash->elf.root.type == bfd_link_hash_defweak)
5010 sym_sec = hash->elf.root.u.def.section;
5011 sym_value = hash->elf.root.u.def.value;
5012 if (sym_sec->output_section != NULL)
5013 destination = (sym_value + irela->r_addend
5014 + sym_sec->output_offset
5015 + sym_sec->output_section->vma);
5017 else if (hash->elf.root.type == bfd_link_hash_undefweak)
5019 else if (hash->elf.root.type == bfd_link_hash_undefined)
5021 else
5023 bfd_set_error (bfd_error_bad_value);
5024 goto error_ret_free_internal;
5028 /* Determine what (if any) linker stub is needed. */
5029 stub_type = ppc_type_of_stub (section, irela, &hash,
5030 destination);
5031 if (stub_type == ppc_stub_none)
5032 continue;
5034 /* Support for grouping stub sections. */
5035 id_sec = htab->stub_group[section->id].link_sec;
5037 /* Get the name of this stub. */
5038 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
5039 if (!stub_name)
5040 goto error_ret_free_internal;
5042 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
5043 stub_name, FALSE, FALSE);
5044 if (stub_entry != NULL)
5046 /* The proper stub has already been created. */
5047 free (stub_name);
5048 continue;
5051 stub_entry = ppc_add_stub (stub_name, section, htab);
5052 if (stub_entry == NULL)
5054 free (stub_name);
5055 error_ret_free_internal:
5056 if (elf_section_data (section)->relocs == NULL)
5057 free (internal_relocs);
5058 error_ret_free_local:
5059 if (local_syms != NULL
5060 && (symtab_hdr->contents
5061 != (unsigned char *) local_syms))
5062 free (local_syms);
5063 return FALSE;
5066 stub_entry->target_value = sym_value;
5067 stub_entry->target_section = sym_sec;
5068 stub_entry->stub_type = stub_type;
5069 stub_entry->h = hash;
5070 stub_changed = TRUE;
5073 /* We're done with the internal relocs, free them. */
5074 if (elf_section_data (section)->relocs != internal_relocs)
5075 free (internal_relocs);
5078 if (local_syms != NULL
5079 && symtab_hdr->contents != (unsigned char *) local_syms)
5081 if (!info->keep_memory)
5082 free (local_syms);
5083 else
5084 symtab_hdr->contents = (unsigned char *) local_syms;
5088 if (!stub_changed)
5089 break;
5091 /* OK, we've added some stubs. Find out the new size of the
5092 stub sections. */
5093 for (stub_sec = htab->stub_bfd->sections;
5094 stub_sec != NULL;
5095 stub_sec = stub_sec->next)
5097 stub_sec->_raw_size = 0;
5098 stub_sec->_cooked_size = 0;
5100 htab->sbrlt->_raw_size = 0;
5101 htab->sbrlt->_cooked_size = 0;
5103 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
5105 /* Ask the linker to do its stuff. */
5106 (*htab->layout_sections_again) ();
5109 /* It would be nice to strip .branch_lt from the output if the
5110 section is empty, but it's too late. If we strip sections here,
5111 the dynamic symbol table is corrupted since the section symbol
5112 for the stripped section isn't written. */
5114 return TRUE;
5117 /* Called after we have determined section placement. If sections
5118 move, we'll be called again. Provide a value for TOCstart. */
5120 bfd_vma
5121 ppc64_elf_toc (obfd)
5122 bfd *obfd;
5124 asection *s;
5125 bfd_vma TOCstart;
5127 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
5128 order. The TOC starts where the first of these sections starts. */
5129 s = bfd_get_section_by_name (obfd, ".got");
5130 if (s == NULL)
5131 s = bfd_get_section_by_name (obfd, ".toc");
5132 if (s == NULL)
5133 s = bfd_get_section_by_name (obfd, ".tocbss");
5134 if (s == NULL)
5135 s = bfd_get_section_by_name (obfd, ".plt");
5136 if (s == NULL)
5138 /* This may happen for
5139 o references to TOC base (SYM@toc / TOC[tc0]) without a
5140 .toc directive
5141 o bad linker script
5142 o --gc-sections and empty TOC sections
5144 FIXME: Warn user? */
5146 /* Look for a likely section. We probably won't even be
5147 using TOCstart. */
5148 for (s = obfd->sections; s != NULL; s = s->next)
5149 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
5150 == (SEC_ALLOC | SEC_SMALL_DATA))
5151 break;
5152 if (s == NULL)
5153 for (s = obfd->sections; s != NULL; s = s->next)
5154 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
5155 == (SEC_ALLOC | SEC_SMALL_DATA))
5156 break;
5157 if (s == NULL)
5158 for (s = obfd->sections; s != NULL; s = s->next)
5159 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
5160 break;
5161 if (s == NULL)
5162 for (s = obfd->sections; s != NULL; s = s->next)
5163 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
5164 break;
5167 TOCstart = 0;
5168 if (s != NULL)
5169 TOCstart = s->output_section->vma + s->output_offset;
5171 return TOCstart;
5174 /* Build all the stubs associated with the current output file.
5175 The stubs are kept in a hash table attached to the main linker
5176 hash table. This function is called via gldelf64ppc_finish. */
5178 bfd_boolean
5179 ppc64_elf_build_stubs (info)
5180 struct bfd_link_info *info;
5182 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5183 asection *stub_sec;
5184 bfd_vma plt_r2;
5185 bfd_byte *p;
5187 for (stub_sec = htab->stub_bfd->sections;
5188 stub_sec != NULL;
5189 stub_sec = stub_sec->next)
5191 bfd_size_type size;
5193 /* Allocate memory to hold the linker stubs. */
5194 size = stub_sec->_raw_size;
5195 if (size != 0)
5197 stub_sec->contents = (bfd_byte *) bfd_zalloc (htab->stub_bfd, size);
5198 if (stub_sec->contents == NULL)
5199 return FALSE;
5201 stub_sec->_cooked_size = 0;
5204 if (htab->splt != NULL)
5206 unsigned int indx;
5208 /* Build the .glink plt call stub. */
5209 plt_r2 = (htab->splt->output_offset
5210 + htab->splt->output_section->vma
5211 - elf_gp (htab->splt->output_section->owner)
5212 - TOC_BASE_OFF);
5213 p = htab->sglink->contents;
5214 p = build_plt_stub (htab->sglink->owner, p, (int) plt_r2, 1);
5215 while (p < htab->sglink->contents + GLINK_CALL_STUB_SIZE)
5217 bfd_put_32 (htab->sglink->owner, NOP, p);
5218 p += 4;
5221 /* Build the .glink lazy link call stubs. */
5222 indx = 0;
5223 while (p < htab->sglink->contents + htab->sglink->_raw_size)
5225 if (indx < 0x8000)
5227 bfd_put_32 (htab->sglink->owner, LI_R0_0 | indx, p);
5228 p += 4;
5230 else
5232 bfd_put_32 (htab->sglink->owner, LIS_R0_0 | PPC_HI (indx), p);
5233 p += 4;
5234 bfd_put_32 (htab->sglink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
5235 p += 4;
5237 bfd_put_32 (htab->sglink->owner,
5238 B_DOT | ((htab->sglink->contents - p) & 0x3fffffc), p);
5239 indx++;
5240 p += 4;
5242 htab->sglink->_cooked_size = p - htab->sglink->contents;
5245 if (htab->sbrlt->_raw_size != 0)
5247 htab->sbrlt->contents = (bfd_byte *) bfd_zalloc (htab->sbrlt->owner,
5248 htab->sbrlt->_raw_size);
5249 if (htab->sbrlt->contents == NULL)
5250 return FALSE;
5253 /* Build the stubs as directed by the stub hash table. */
5254 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5256 for (stub_sec = htab->stub_bfd->sections;
5257 stub_sec != NULL;
5258 stub_sec = stub_sec->next)
5260 if (stub_sec->_raw_size != stub_sec->_cooked_size)
5261 break;
5264 if (stub_sec != NULL
5265 || htab->sglink->_raw_size != htab->sglink->_cooked_size)
5267 htab->stub_error = TRUE;
5268 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5271 return !htab->stub_error;
5274 /* The RELOCATE_SECTION function is called by the ELF backend linker
5275 to handle the relocations for a section.
5277 The relocs are always passed as Rela structures; if the section
5278 actually uses Rel structures, the r_addend field will always be
5279 zero.
5281 This function is responsible for adjust the section contents as
5282 necessary, and (if using Rela relocs and generating a
5283 relocateable output file) adjusting the reloc addend as
5284 necessary.
5286 This function does not have to worry about setting the reloc
5287 address or the reloc symbol index.
5289 LOCAL_SYMS is a pointer to the swapped in local symbols.
5291 LOCAL_SECTIONS is an array giving the section in the input file
5292 corresponding to the st_shndx field of each local symbol.
5294 The global hash table entry for the global symbols can be found
5295 via elf_sym_hashes (input_bfd).
5297 When generating relocateable output, this function must handle
5298 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
5299 going to be the section symbol corresponding to the output
5300 section, which means that the addend must be adjusted
5301 accordingly. */
5303 static bfd_boolean
5304 ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
5305 contents, relocs, local_syms, local_sections)
5306 bfd *output_bfd;
5307 struct bfd_link_info *info;
5308 bfd *input_bfd;
5309 asection *input_section;
5310 bfd_byte *contents;
5311 Elf_Internal_Rela *relocs;
5312 Elf_Internal_Sym *local_syms;
5313 asection **local_sections;
5315 struct ppc_link_hash_table *htab;
5316 Elf_Internal_Shdr *symtab_hdr;
5317 struct elf_link_hash_entry **sym_hashes;
5318 Elf_Internal_Rela *rel;
5319 Elf_Internal_Rela *relend;
5320 bfd_vma *local_got_offsets;
5321 bfd_vma TOCstart;
5322 bfd_boolean ret = TRUE;
5323 bfd_boolean is_opd;
5324 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
5325 bfd_boolean is_power4 = FALSE;
5327 if (info->relocateable)
5328 return TRUE;
5330 /* Initialize howto table if needed. */
5331 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5332 ppc_howto_init ();
5334 htab = ppc_hash_table (info);
5335 local_got_offsets = elf_local_got_offsets (input_bfd);
5336 TOCstart = elf_gp (output_bfd);
5337 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5338 sym_hashes = elf_sym_hashes (input_bfd);
5339 is_opd = ppc64_elf_section_data (input_section)->opd.adjust != NULL;
5341 rel = relocs;
5342 relend = relocs + input_section->reloc_count;
5343 for (; rel < relend; rel++)
5345 enum elf_ppc64_reloc_type r_type;
5346 bfd_vma offset;
5347 bfd_vma addend;
5348 bfd_reloc_status_type r;
5349 Elf_Internal_Sym *sym;
5350 asection *sec;
5351 struct elf_link_hash_entry *h;
5352 struct elf_link_hash_entry *fdh;
5353 const char *sym_name;
5354 unsigned long r_symndx;
5355 bfd_vma relocation;
5356 bfd_boolean unresolved_reloc;
5357 bfd_boolean warned;
5358 long insn;
5359 struct ppc_stub_hash_entry *stub_entry;
5360 bfd_vma max_br_offset;
5361 bfd_vma from;
5363 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rel->r_info);
5364 r_symndx = ELF64_R_SYM (rel->r_info);
5365 offset = rel->r_offset;
5366 addend = rel->r_addend;
5367 r = bfd_reloc_other;
5368 sym = (Elf_Internal_Sym *) 0;
5369 sec = (asection *) 0;
5370 h = (struct elf_link_hash_entry *) 0;
5371 sym_name = (const char *) 0;
5372 unresolved_reloc = FALSE;
5373 warned = FALSE;
5375 if (r_type == R_PPC64_TOC)
5377 /* Relocation value is TOC base. Symbol is ignored. */
5378 relocation = TOCstart + TOC_BASE_OFF;
5380 else if (r_symndx < symtab_hdr->sh_info)
5382 /* It's a local symbol. */
5383 sym = local_syms + r_symndx;
5384 sec = local_sections[r_symndx];
5385 sym_name = "<local symbol>";
5387 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
5388 /* rel may have changed, update our copy of addend. */
5389 addend = rel->r_addend;
5391 if (elf_section_data (sec) != NULL)
5393 long *opd_sym_adjust;
5395 opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
5396 if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
5397 relocation += opd_sym_adjust[sym->st_value / 24];
5400 else
5402 /* It's a global symbol. */
5403 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5404 while (h->root.type == bfd_link_hash_indirect
5405 || h->root.type == bfd_link_hash_warning)
5406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5407 sym_name = h->root.root.string;
5408 relocation = 0;
5409 if (h->root.type == bfd_link_hash_defined
5410 || h->root.type == bfd_link_hash_defweak)
5412 sec = h->root.u.def.section;
5413 if (sec->output_section == NULL)
5414 /* Set a flag that will be cleared later if we find a
5415 relocation value for this symbol. output_section
5416 is typically NULL for symbols satisfied by a shared
5417 library. */
5418 unresolved_reloc = TRUE;
5419 else
5420 relocation = (h->root.u.def.value
5421 + sec->output_section->vma
5422 + sec->output_offset);
5424 else if (h->root.type == bfd_link_hash_undefweak)
5426 else if (info->shared
5427 && (!info->symbolic || info->allow_shlib_undefined)
5428 && !info->no_undefined
5429 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
5431 else
5433 if (! ((*info->callbacks->undefined_symbol)
5434 (info, h->root.root.string, input_bfd, input_section,
5435 offset, (!info->shared
5436 || info->no_undefined
5437 || ELF_ST_VISIBILITY (h->other)))))
5438 return FALSE;
5439 warned = TRUE;
5443 /* First handle relocations that tweak non-addend part of insn. */
5444 insn = 0;
5445 switch (r_type)
5447 default:
5448 break;
5450 /* Branch taken prediction relocations. */
5451 case R_PPC64_ADDR14_BRTAKEN:
5452 case R_PPC64_REL14_BRTAKEN:
5453 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
5454 /* Fall thru. */
5456 /* Branch not taken prediction relocations. */
5457 case R_PPC64_ADDR14_BRNTAKEN:
5458 case R_PPC64_REL14_BRNTAKEN:
5459 insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
5460 if (is_power4)
5462 /* Set 'a' bit. This is 0b00010 in BO field for branch
5463 on CR(BI) insns (BO == 001at or 011at), and 0b01000
5464 for branch on CTR insns (BO == 1a00t or 1a01t). */
5465 if ((insn & (0x14 << 21)) == (0x04 << 21))
5466 insn |= 0x02 << 21;
5467 else if ((insn & (0x14 << 21)) == (0x10 << 21))
5468 insn |= 0x08 << 21;
5469 else
5470 break;
5472 else
5474 from = (offset
5475 + input_section->output_offset
5476 + input_section->output_section->vma);
5478 /* Invert 'y' bit if not the default. */
5479 if ((bfd_signed_vma) (relocation + addend - from) < 0)
5480 insn ^= 0x01 << 21;
5483 bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
5484 break;
5486 case R_PPC64_REL24:
5487 /* A REL24 branching to a linkage function is followed by a
5488 nop. We replace the nop with a ld in order to restore
5489 the TOC base pointer. Only calls to shared objects need
5490 to alter the TOC base. These are recognized by their
5491 need for a PLT entry. */
5492 if (h != NULL
5493 && (fdh = ((struct ppc_link_hash_entry *) h)->oh) != NULL
5494 && fdh->plt.offset != (bfd_vma) -1
5495 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
5496 rel, htab)) != NULL)
5498 bfd_boolean can_plt_call = 0;
5500 if (offset + 8 <= input_section->_cooked_size)
5502 insn = bfd_get_32 (input_bfd, contents + offset + 4);
5503 if (insn == NOP
5504 || insn == CROR_151515 || insn == CROR_313131)
5506 bfd_put_32 (input_bfd, (bfd_vma) LD_R2_40R1,
5507 contents + offset + 4);
5508 can_plt_call = 1;
5512 if (!can_plt_call)
5514 /* If this is a plain branch rather than a branch
5515 and link, don't require a nop. */
5516 insn = bfd_get_32 (input_bfd, contents + offset);
5517 if ((insn & 1) == 0)
5518 can_plt_call = 1;
5521 if (can_plt_call)
5523 relocation = (stub_entry->stub_offset
5524 + stub_entry->stub_sec->output_offset
5525 + stub_entry->stub_sec->output_section->vma);
5526 addend = 0;
5527 unresolved_reloc = FALSE;
5531 if (h != NULL
5532 && h->root.type == bfd_link_hash_undefweak
5533 && relocation == 0
5534 && addend == 0)
5536 /* Tweak calls to undefined weak functions to point at a
5537 blr. We can thus call a weak function without first
5538 checking whether the function is defined. We have a
5539 blr at the end of .sfpr. */
5540 BFD_ASSERT (htab->sfpr->_raw_size != 0);
5541 relocation = (htab->sfpr->_raw_size - 4
5542 + htab->sfpr->output_offset
5543 + htab->sfpr->output_section->vma);
5544 from = (offset
5545 + input_section->output_offset
5546 + input_section->output_section->vma);
5548 /* But let's not be silly about it. If the blr isn't in
5549 reach, just go to the next instruction. */
5550 if (relocation - from + (1 << 25) >= (1 << 26)
5551 || htab->sfpr->_raw_size == 0)
5552 relocation = from + 4;
5554 break;
5557 /* Set `addend'. */
5558 switch (r_type)
5560 default:
5561 (*_bfd_error_handler)
5562 (_("%s: unknown relocation type %d for symbol %s"),
5563 bfd_archive_filename (input_bfd), (int) r_type, sym_name);
5565 bfd_set_error (bfd_error_bad_value);
5566 ret = FALSE;
5567 continue;
5569 case R_PPC64_NONE:
5570 case R_PPC64_GNU_VTINHERIT:
5571 case R_PPC64_GNU_VTENTRY:
5572 continue;
5574 /* GOT16 relocations. Like an ADDR16 using the symbol's
5575 address in the GOT as relocation value instead of the
5576 symbols value itself. Also, create a GOT entry for the
5577 symbol and put the symbol value there. */
5578 case R_PPC64_GOT16:
5579 case R_PPC64_GOT16_LO:
5580 case R_PPC64_GOT16_HI:
5581 case R_PPC64_GOT16_HA:
5582 case R_PPC64_GOT16_DS:
5583 case R_PPC64_GOT16_LO_DS:
5585 /* Relocation is to the entry for this symbol in the global
5586 offset table. */
5587 bfd_vma off;
5589 if (htab->sgot == NULL)
5590 abort ();
5592 if (h != NULL)
5594 bfd_boolean dyn;
5596 off = h->got.offset;
5597 dyn = htab->elf.dynamic_sections_created;
5598 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
5599 || (info->shared
5600 && (info->symbolic
5601 || h->dynindx == -1
5602 || (h->elf_link_hash_flags
5603 & ELF_LINK_FORCED_LOCAL))
5604 && (h->elf_link_hash_flags
5605 & ELF_LINK_HASH_DEF_REGULAR)))
5607 /* This is actually a static link, or it is a
5608 -Bsymbolic link and the symbol is defined
5609 locally, or the symbol was forced to be local
5610 because of a version file. We must initialize
5611 this entry in the global offset table. Since the
5612 offset must always be a multiple of 8, we use the
5613 least significant bit to record whether we have
5614 initialized it already.
5616 When doing a dynamic link, we create a .rel.got
5617 relocation entry to initialize the value. This
5618 is done in the finish_dynamic_symbol routine. */
5619 if ((off & 1) != 0)
5620 off &= ~1;
5621 else
5623 bfd_put_64 (output_bfd, relocation,
5624 htab->sgot->contents + off);
5625 h->got.offset |= 1;
5628 else
5629 unresolved_reloc = FALSE;
5631 else
5633 if (local_got_offsets == NULL)
5634 abort ();
5636 off = local_got_offsets[r_symndx];
5638 /* The offset must always be a multiple of 8. We use
5639 the least significant bit to record whether we have
5640 already processed this entry. */
5641 if ((off & 1) != 0)
5642 off &= ~1;
5643 else
5645 bfd_put_64 (output_bfd, relocation,
5646 htab->sgot->contents + off);
5648 if (info->shared)
5650 Elf_Internal_Rela outrel;
5651 bfd_byte *loc;
5653 /* We need to generate a R_PPC64_RELATIVE reloc
5654 for the dynamic linker. */
5655 outrel.r_offset = (htab->sgot->output_section->vma
5656 + htab->sgot->output_offset
5657 + off);
5658 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5659 outrel.r_addend = relocation;
5660 loc = htab->srelgot->contents;
5661 loc += (htab->srelgot->reloc_count++
5662 * sizeof (Elf64_External_Rela));
5663 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5666 local_got_offsets[r_symndx] |= 1;
5670 if (off >= (bfd_vma) -2)
5671 abort ();
5673 relocation = htab->sgot->output_offset + off;
5675 /* TOC base (r2) is TOC start plus 0x8000. */
5676 addend -= TOC_BASE_OFF;
5678 break;
5680 case R_PPC64_PLT16_HA:
5681 case R_PPC64_PLT16_HI:
5682 case R_PPC64_PLT16_LO:
5683 case R_PPC64_PLT32:
5684 case R_PPC64_PLT64:
5685 /* Relocation is to the entry for this symbol in the
5686 procedure linkage table. */
5688 /* Resolve a PLT reloc against a local symbol directly,
5689 without using the procedure linkage table. */
5690 if (h == NULL)
5691 break;
5693 if (h->plt.offset == (bfd_vma) -1
5694 || htab->splt == NULL)
5696 /* We didn't make a PLT entry for this symbol. This
5697 happens when statically linking PIC code, or when
5698 using -Bsymbolic. */
5699 break;
5702 relocation = (htab->splt->output_section->vma
5703 + htab->splt->output_offset
5704 + h->plt.offset);
5705 unresolved_reloc = FALSE;
5706 break;
5708 /* TOC16 relocs. We want the offset relative to the TOC base,
5709 which is the address of the start of the TOC plus 0x8000.
5710 The TOC consists of sections .got, .toc, .tocbss, and .plt,
5711 in this order. */
5712 case R_PPC64_TOC16:
5713 case R_PPC64_TOC16_LO:
5714 case R_PPC64_TOC16_HI:
5715 case R_PPC64_TOC16_DS:
5716 case R_PPC64_TOC16_LO_DS:
5717 case R_PPC64_TOC16_HA:
5718 addend -= TOCstart + TOC_BASE_OFF;
5719 break;
5721 /* Relocate against the beginning of the section. */
5722 case R_PPC64_SECTOFF:
5723 case R_PPC64_SECTOFF_LO:
5724 case R_PPC64_SECTOFF_HI:
5725 case R_PPC64_SECTOFF_DS:
5726 case R_PPC64_SECTOFF_LO_DS:
5727 case R_PPC64_SECTOFF_HA:
5728 if (sec != (asection *) 0)
5729 addend -= sec->output_section->vma;
5730 break;
5732 case R_PPC64_REL14:
5733 case R_PPC64_REL14_BRNTAKEN:
5734 case R_PPC64_REL14_BRTAKEN:
5735 case R_PPC64_REL24:
5736 break;
5738 /* Relocations that may need to be propagated if this is a
5739 dynamic object. */
5740 case R_PPC64_REL30:
5741 case R_PPC64_REL32:
5742 case R_PPC64_REL64:
5743 case R_PPC64_ADDR14:
5744 case R_PPC64_ADDR14_BRNTAKEN:
5745 case R_PPC64_ADDR14_BRTAKEN:
5746 case R_PPC64_ADDR16:
5747 case R_PPC64_ADDR16_DS:
5748 case R_PPC64_ADDR16_HA:
5749 case R_PPC64_ADDR16_HI:
5750 case R_PPC64_ADDR16_HIGHER:
5751 case R_PPC64_ADDR16_HIGHERA:
5752 case R_PPC64_ADDR16_HIGHEST:
5753 case R_PPC64_ADDR16_HIGHESTA:
5754 case R_PPC64_ADDR16_LO:
5755 case R_PPC64_ADDR16_LO_DS:
5756 case R_PPC64_ADDR24:
5757 case R_PPC64_ADDR32:
5758 case R_PPC64_ADDR64:
5759 case R_PPC64_UADDR16:
5760 case R_PPC64_UADDR32:
5761 case R_PPC64_UADDR64:
5762 /* r_symndx will be zero only for relocs against symbols
5763 from removed linkonce sections, or sections discarded by
5764 a linker script. */
5765 if (r_symndx == 0)
5766 break;
5767 /* Fall thru. */
5769 case R_PPC64_TOC:
5770 if ((input_section->flags & SEC_ALLOC) == 0)
5771 break;
5773 if (NO_OPD_RELOCS && is_opd)
5774 break;
5776 if ((info->shared
5777 && (IS_ABSOLUTE_RELOC (r_type)
5778 || (h != NULL
5779 && h->dynindx != -1
5780 && (! info->symbolic
5781 || (h->elf_link_hash_flags
5782 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
5783 || (!info->shared
5784 && h != NULL
5785 && h->dynindx != -1
5786 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
5787 && (((h->elf_link_hash_flags
5788 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
5789 && (h->elf_link_hash_flags
5790 & ELF_LINK_HASH_DEF_REGULAR) == 0)
5791 || h->root.type == bfd_link_hash_undefweak
5792 || h->root.type == bfd_link_hash_undefined)))
5794 Elf_Internal_Rela outrel;
5795 bfd_boolean skip, relocate;
5796 asection *sreloc;
5797 bfd_byte *loc;
5799 /* When generating a dynamic object, these relocations
5800 are copied into the output file to be resolved at run
5801 time. */
5803 skip = FALSE;
5804 relocate = FALSE;
5806 outrel.r_offset =
5807 _bfd_elf_section_offset (output_bfd, info, input_section,
5808 rel->r_offset);
5809 if (outrel.r_offset == (bfd_vma) -1)
5810 skip = TRUE;
5811 else if (outrel.r_offset == (bfd_vma) -2)
5812 skip = TRUE, relocate = TRUE;
5813 outrel.r_offset += (input_section->output_section->vma
5814 + input_section->output_offset);
5815 outrel.r_addend = addend;
5817 if (skip)
5818 memset (&outrel, 0, sizeof outrel);
5819 else if (h != NULL
5820 && h->dynindx != -1
5821 && !is_opd
5822 && (!IS_ABSOLUTE_RELOC (r_type)
5823 || !info->shared
5824 || !info->symbolic
5825 || (h->elf_link_hash_flags
5826 & ELF_LINK_HASH_DEF_REGULAR) == 0))
5827 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
5828 else
5830 /* This symbol is local, or marked to become local,
5831 or this is an opd section reloc which must point
5832 at a local function. */
5833 outrel.r_addend += relocation;
5834 relocate = TRUE;
5835 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5837 if (is_opd && h != NULL)
5839 /* Lie about opd entries. This case occurs
5840 when building shared libraries and we
5841 reference a function in another shared
5842 lib. The same thing happens for a weak
5843 definition in an application that's
5844 overridden by a strong definition in a
5845 shared lib. (I believe this is a generic
5846 bug in binutils handling of weak syms.)
5847 In these cases we won't use the opd
5848 entry in this lib. */
5849 unresolved_reloc = FALSE;
5851 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
5853 else
5855 long indx = 0;
5857 if (bfd_is_abs_section (sec))
5859 else if (sec == NULL || sec->owner == NULL)
5861 bfd_set_error (bfd_error_bad_value);
5862 return FALSE;
5864 else
5866 asection *osec;
5868 osec = sec->output_section;
5869 indx = elf_section_data (osec)->dynindx;
5871 /* We are turning this relocation into one
5872 against a section symbol, so subtract out
5873 the output section's address but not the
5874 offset of the input section in the output
5875 section. */
5876 outrel.r_addend -= osec->vma;
5879 outrel.r_info = ELF64_R_INFO (indx, r_type);
5883 sreloc = elf_section_data (input_section)->sreloc;
5884 if (sreloc == NULL)
5885 abort ();
5887 loc = sreloc->contents;
5888 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
5889 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
5891 /* If this reloc is against an external symbol, it will
5892 be computed at runtime, so there's no need to do
5893 anything now. */
5894 if (! relocate)
5895 continue;
5897 break;
5899 case R_PPC64_COPY:
5900 case R_PPC64_GLOB_DAT:
5901 case R_PPC64_JMP_SLOT:
5902 case R_PPC64_RELATIVE:
5903 /* We shouldn't ever see these dynamic relocs in relocatable
5904 files. */
5905 /* Fall thru */
5907 case R_PPC64_PLTGOT16:
5908 case R_PPC64_PLTGOT16_DS:
5909 case R_PPC64_PLTGOT16_HA:
5910 case R_PPC64_PLTGOT16_HI:
5911 case R_PPC64_PLTGOT16_LO:
5912 case R_PPC64_PLTGOT16_LO_DS:
5913 case R_PPC64_PLTREL32:
5914 case R_PPC64_PLTREL64:
5915 /* These ones haven't been implemented yet. */
5917 (*_bfd_error_handler)
5918 (_("%s: Relocation %s is not supported for symbol %s."),
5919 bfd_archive_filename (input_bfd),
5920 ppc64_elf_howto_table[(int) r_type]->name, sym_name);
5922 bfd_set_error (bfd_error_invalid_operation);
5923 ret = FALSE;
5924 continue;
5927 /* Do any further special processing. */
5928 switch (r_type)
5930 default:
5931 break;
5933 case R_PPC64_ADDR16_HA:
5934 case R_PPC64_ADDR16_HIGHERA:
5935 case R_PPC64_ADDR16_HIGHESTA:
5936 case R_PPC64_GOT16_HA:
5937 case R_PPC64_PLTGOT16_HA:
5938 case R_PPC64_PLT16_HA:
5939 case R_PPC64_TOC16_HA:
5940 case R_PPC64_SECTOFF_HA:
5941 /* It's just possible that this symbol is a weak symbol
5942 that's not actually defined anywhere. In that case,
5943 'sec' would be NULL, and we should leave the symbol
5944 alone (it will be set to zero elsewhere in the link). */
5945 if (sec != NULL)
5946 /* Add 0x10000 if sign bit in 0:15 is set. */
5947 addend += ((relocation + addend) & 0x8000) << 1;
5948 break;
5950 case R_PPC64_ADDR16_DS:
5951 case R_PPC64_ADDR16_LO_DS:
5952 case R_PPC64_GOT16_DS:
5953 case R_PPC64_GOT16_LO_DS:
5954 case R_PPC64_PLT16_LO_DS:
5955 case R_PPC64_SECTOFF_DS:
5956 case R_PPC64_SECTOFF_LO_DS:
5957 case R_PPC64_TOC16_DS:
5958 case R_PPC64_TOC16_LO_DS:
5959 case R_PPC64_PLTGOT16_DS:
5960 case R_PPC64_PLTGOT16_LO_DS:
5961 if (((relocation + addend) & 3) != 0)
5963 (*_bfd_error_handler)
5964 (_("%s: error: relocation %s not a multiple of 4"),
5965 bfd_archive_filename (input_bfd),
5966 ppc64_elf_howto_table[(int) r_type]->name);
5967 bfd_set_error (bfd_error_bad_value);
5968 ret = FALSE;
5969 continue;
5971 break;
5973 case R_PPC64_REL14:
5974 case R_PPC64_REL14_BRNTAKEN:
5975 case R_PPC64_REL14_BRTAKEN:
5976 max_br_offset = 1 << 15;
5977 goto branch_check;
5979 case R_PPC64_REL24:
5980 max_br_offset = 1 << 25;
5982 branch_check:
5983 /* If the branch is out of reach, then redirect the
5984 call to the local stub for this function. */
5985 from = (offset
5986 + input_section->output_offset
5987 + input_section->output_section->vma);
5988 if (relocation + addend - from + max_br_offset >= 2 * max_br_offset
5989 && (stub_entry = ppc_get_stub_entry (input_section, sec, h,
5990 rel, htab)) != NULL)
5992 /* Munge up the value and addend so that we call the stub
5993 rather than the procedure directly. */
5994 relocation = (stub_entry->stub_offset
5995 + stub_entry->stub_sec->output_offset
5996 + stub_entry->stub_sec->output_section->vma);
5997 addend = 0;
5999 break;
6002 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
6003 because such sections are not SEC_ALLOC and thus ld.so will
6004 not process them. */
6005 if (unresolved_reloc
6006 && !((input_section->flags & SEC_DEBUGGING) != 0
6007 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
6009 (*_bfd_error_handler)
6010 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
6011 bfd_archive_filename (input_bfd),
6012 bfd_get_section_name (input_bfd, input_section),
6013 (long) rel->r_offset,
6014 h->root.root.string);
6015 ret = FALSE;
6018 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
6019 input_bfd,
6020 input_section,
6021 contents,
6022 offset,
6023 relocation,
6024 addend);
6026 if (r != bfd_reloc_ok)
6028 const char *name;
6030 if (h != NULL)
6032 if (h->root.type == bfd_link_hash_undefweak
6033 && ppc64_elf_howto_table[(int) r_type]->pc_relative)
6035 /* Assume this is a call protected by other code that
6036 detects the symbol is undefined. If this is the case,
6037 we can safely ignore the overflow. If not, the
6038 program is hosed anyway, and a little warning isn't
6039 going to help. */
6041 continue;
6044 name = h->root.root.string;
6046 else
6048 name = bfd_elf_string_from_elf_section (input_bfd,
6049 symtab_hdr->sh_link,
6050 sym->st_name);
6051 if (name == NULL)
6052 continue;
6053 if (*name == '\0')
6054 name = bfd_section_name (input_bfd, sec);
6057 if (r == bfd_reloc_overflow)
6059 if (warned)
6060 continue;
6061 if (!((*info->callbacks->reloc_overflow)
6062 (info, name, ppc64_elf_howto_table[(int) r_type]->name,
6063 rel->r_addend, input_bfd, input_section, offset)))
6064 return FALSE;
6066 else
6068 (*_bfd_error_handler)
6069 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
6070 bfd_archive_filename (input_bfd),
6071 bfd_get_section_name (input_bfd, input_section),
6072 (long) rel->r_offset, name, (int) r);
6073 ret = FALSE;
6078 return ret;
6081 /* Finish up dynamic symbol handling. We set the contents of various
6082 dynamic sections here. */
6084 static bfd_boolean
6085 ppc64_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
6086 bfd *output_bfd;
6087 struct bfd_link_info *info;
6088 struct elf_link_hash_entry *h;
6089 Elf_Internal_Sym *sym;
6091 struct ppc_link_hash_table *htab;
6092 bfd *dynobj;
6094 htab = ppc_hash_table (info);
6095 dynobj = htab->elf.dynobj;
6097 if (h->plt.offset != (bfd_vma) -1
6098 && ((struct ppc_link_hash_entry *) h)->is_func_descriptor)
6100 Elf_Internal_Rela rela;
6101 bfd_byte *loc;
6103 /* This symbol has an entry in the procedure linkage table. Set
6104 it up. */
6106 if (htab->splt == NULL
6107 || htab->srelplt == NULL
6108 || htab->sglink == NULL)
6109 abort ();
6111 /* Create a JMP_SLOT reloc to inform the dynamic linker to
6112 fill in the PLT entry. */
6114 rela.r_offset = (htab->splt->output_section->vma
6115 + htab->splt->output_offset
6116 + h->plt.offset);
6117 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
6118 rela.r_addend = 0;
6120 loc = htab->srelplt->contents;
6121 loc += ((h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
6122 * sizeof (Elf64_External_Rela));
6123 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6126 if (h->got.offset != (bfd_vma) -1)
6128 Elf_Internal_Rela rela;
6129 bfd_byte *loc;
6131 /* This symbol has an entry in the global offset table. Set it
6132 up. */
6134 if (htab->sgot == NULL || htab->srelgot == NULL)
6135 abort ();
6137 rela.r_offset = (htab->sgot->output_section->vma
6138 + htab->sgot->output_offset
6139 + (h->got.offset &~ (bfd_vma) 1));
6141 /* If this is a static link, or it is a -Bsymbolic link and the
6142 symbol is defined locally or was forced to be local because
6143 of a version file, we just want to emit a RELATIVE reloc.
6144 The entry in the global offset table will already have been
6145 initialized in the relocate_section function. */
6146 if (info->shared
6147 && (info->symbolic
6148 || h->dynindx == -1
6149 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
6150 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
6152 BFD_ASSERT((h->got.offset & 1) != 0);
6153 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
6154 rela.r_addend = (h->root.u.def.value
6155 + h->root.u.def.section->output_section->vma
6156 + h->root.u.def.section->output_offset);
6158 else
6160 BFD_ASSERT ((h->got.offset & 1) == 0);
6161 bfd_put_64 (output_bfd, (bfd_vma) 0,
6162 htab->sgot->contents + h->got.offset);
6163 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_GLOB_DAT);
6164 rela.r_addend = 0;
6167 loc = htab->srelgot->contents;
6168 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
6169 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6172 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
6174 Elf_Internal_Rela rela;
6175 bfd_byte *loc;
6177 /* This symbol needs a copy reloc. Set it up. */
6179 if (h->dynindx == -1
6180 || (h->root.type != bfd_link_hash_defined
6181 && h->root.type != bfd_link_hash_defweak)
6182 || htab->srelbss == NULL)
6183 abort ();
6185 rela.r_offset = (h->root.u.def.value
6186 + h->root.u.def.section->output_section->vma
6187 + h->root.u.def.section->output_offset);
6188 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
6189 rela.r_addend = 0;
6190 loc = htab->srelbss->contents;
6191 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
6192 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
6195 /* Mark some specially defined symbols as absolute. */
6196 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
6197 sym->st_shndx = SHN_ABS;
6199 return TRUE;
6202 /* Used to decide how to sort relocs in an optimal manner for the
6203 dynamic linker, before writing them out. */
6205 static enum elf_reloc_type_class
6206 ppc64_elf_reloc_type_class (rela)
6207 const Elf_Internal_Rela *rela;
6209 enum elf_ppc64_reloc_type r_type;
6211 r_type = (enum elf_ppc64_reloc_type) ELF64_R_TYPE (rela->r_info);
6212 switch (r_type)
6214 case R_PPC64_RELATIVE:
6215 return reloc_class_relative;
6216 case R_PPC64_JMP_SLOT:
6217 return reloc_class_plt;
6218 case R_PPC64_COPY:
6219 return reloc_class_copy;
6220 default:
6221 return reloc_class_normal;
6225 /* Finish up the dynamic sections. */
6227 static bfd_boolean
6228 ppc64_elf_finish_dynamic_sections (output_bfd, info)
6229 bfd *output_bfd;
6230 struct bfd_link_info *info;
6232 struct ppc_link_hash_table *htab;
6233 bfd *dynobj;
6234 asection *sdyn;
6236 htab = ppc_hash_table (info);
6237 dynobj = htab->elf.dynobj;
6238 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6240 if (htab->elf.dynamic_sections_created)
6242 Elf64_External_Dyn *dyncon, *dynconend;
6244 if (sdyn == NULL || htab->sgot == NULL)
6245 abort ();
6247 dyncon = (Elf64_External_Dyn *) sdyn->contents;
6248 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
6249 for (; dyncon < dynconend; dyncon++)
6251 Elf_Internal_Dyn dyn;
6252 asection *s;
6254 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
6256 switch (dyn.d_tag)
6258 default:
6259 continue;
6261 case DT_PPC64_GLINK:
6262 s = htab->sglink;
6263 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6264 break;
6266 case DT_PPC64_OPD:
6267 s = bfd_get_section_by_name (output_bfd, ".opd");
6268 if (s == NULL)
6269 continue;
6270 dyn.d_un.d_ptr = s->vma;
6271 break;
6273 case DT_PPC64_OPDSZ:
6274 s = bfd_get_section_by_name (output_bfd, ".opd");
6275 if (s == NULL)
6276 continue;
6277 dyn.d_un.d_val = s->_raw_size;
6278 break;
6280 case DT_PLTGOT:
6281 s = htab->splt;
6282 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6283 break;
6285 case DT_JMPREL:
6286 s = htab->srelplt;
6287 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6288 break;
6290 case DT_PLTRELSZ:
6291 dyn.d_un.d_val = htab->srelplt->_raw_size;
6292 break;
6294 case DT_RELASZ:
6295 /* Don't count procedure linkage table relocs in the
6296 overall reloc count. */
6297 s = htab->srelplt;
6298 if (s == NULL)
6299 continue;
6300 dyn.d_un.d_val -= s->_raw_size;
6301 break;
6303 case DT_RELA:
6304 /* We may not be using the standard ELF linker script.
6305 If .rela.plt is the first .rela section, we adjust
6306 DT_RELA to not include it. */
6307 s = htab->srelplt;
6308 if (s == NULL)
6309 continue;
6310 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
6311 continue;
6312 dyn.d_un.d_ptr += s->_raw_size;
6313 break;
6316 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
6320 if (htab->sgot != NULL && htab->sgot->_raw_size != 0)
6322 /* Fill in the first entry in the global offset table.
6323 We use it to hold the link-time TOCbase. */
6324 bfd_put_64 (output_bfd,
6325 elf_gp (output_bfd) + TOC_BASE_OFF,
6326 htab->sgot->contents);
6328 /* Set .got entry size. */
6329 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize = 8;
6332 if (htab->splt != NULL && htab->splt->_raw_size != 0)
6334 /* Set .plt entry size. */
6335 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize
6336 = PLT_ENTRY_SIZE;
6339 return TRUE;
6342 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
6343 #define TARGET_LITTLE_NAME "elf64-powerpcle"
6344 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
6345 #define TARGET_BIG_NAME "elf64-powerpc"
6346 #define ELF_ARCH bfd_arch_powerpc
6347 #define ELF_MACHINE_CODE EM_PPC64
6348 #define ELF_MAXPAGESIZE 0x10000
6349 #define elf_info_to_howto ppc64_elf_info_to_howto
6351 #ifdef EM_CYGNUS_POWERPC
6352 #define ELF_MACHINE_ALT1 EM_CYGNUS_POWERPC
6353 #endif
6355 #ifdef EM_PPC_OLD
6356 #define ELF_MACHINE_ALT2 EM_PPC_OLD
6357 #endif
6359 #define elf_backend_want_got_sym 0
6360 #define elf_backend_want_plt_sym 0
6361 #define elf_backend_plt_alignment 3
6362 #define elf_backend_plt_not_loaded 1
6363 #define elf_backend_got_symbol_offset 0
6364 #define elf_backend_got_header_size 8
6365 #define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
6366 #define elf_backend_can_gc_sections 1
6367 #define elf_backend_can_refcount 1
6368 #define elf_backend_rela_normal 1
6370 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
6371 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
6372 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
6373 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
6374 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
6376 #define elf_backend_object_p ppc64_elf_object_p
6377 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
6378 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
6379 #define elf_backend_check_relocs ppc64_elf_check_relocs
6380 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
6381 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
6382 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
6383 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
6384 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
6385 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
6386 #define elf_backend_relocate_section ppc64_elf_relocate_section
6387 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
6388 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
6389 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
6391 #include "elf64-target.h"